Handle hostnames with upper-case letters
[webmin.git] / cluster-webmin / sync.cgi
1 #!/usr/local/bin/perl
2 # sync.cgi
3 # Create missing users and groups on servers
4
5 require './cluster-webmin-lib.pl';
6 &ReadParse();
7 @hosts = &list_webmin_hosts();
8 @servers = &list_servers();
9 &ui_print_header(undef, $text{'sync_title'}, "");
10
11 # Work out which hosts
12 @phosts = &create_on_parse(undef, undef, undef, 1);
13
14 # Build lists of all users and group
15 foreach $h (@hosts) {
16         local ($serv) = grep { $_->{'id'} == $h->{'id'} } @servers;
17         foreach $u (@{$h->{'users'}}) {
18                 if (!$doneuser{$u->{'name'}}++) {
19                         push(@ulist, $u);
20                         $u->{'source'} = $serv;
21                         }
22                 }
23         foreach $g (@{$h->{'groups'}}) {
24                 if (!$donegroup{$g->{'name'}}++) {
25                         push(@glist, $g);
26                         $g->{'source'} = $serv;
27                         }
28                 }
29         }
30
31 # Find users and groups to sync
32 if ($in{'users_mode'} == 1) {
33         @usync = @ulist;
34         }
35 elsif ($in{'users_mode'} == 2) {
36         map { $usel{$_}++ } split(/\s+/, $in{'usel'});
37         @usync = grep { $usel{$_->{'name'}} } @ulist;
38         }
39 elsif ($in{'users_mode'} == 3) {
40         map { $unot{$_}++ } split(/\s+/, $in{'unot'});
41         @usync = grep { !$unot{$_->{'name'}} } @ulist;
42         }
43
44 if ($in{'groups_mode'} == 1) {
45         @gsync = @glist;
46         }
47 elsif ($in{'groups_mode'} == 2) {
48         map { $gsel{$_}++ } split(/\s+/, $in{'gsel'});
49         @gsync = grep { $gsel{$_->{'name'}} } @glist;
50         }
51 elsif ($in{'groups_mode'} == 3) {
52         map { $gnot{$_}++ } split(/\s+/, $in{'gnot'});
53         @gsync = grep { !$gnot{$_->{'name'}} } @glist;
54         }
55
56 # Setup error handler for down hosts
57 sub add_error
58 {
59 $add_error_msg = join("", @_);
60 }
61 &remote_error_setup(\&add_error);
62
63 # Sync on chosen hosts
64 foreach $host (@phosts) {
65         $add_error_msg = undef;
66         local ($serv) = grep { $_->{'id'} == $host->{'id'} } @servers;
67         print "<b>",&text('sync_on', &server_name($serv)),"</b><p>\n";
68         print "<ul>\n";
69
70         # Find missing users and groups
71         local (%usync, %gsync);
72         map { $usync{$_->{'name'}}++ } @{$host->{'users'}};
73         map { $gsync{$_->{'name'}}++ } @{$host->{'groups'}};
74         local @uneed = grep { !$usync{$_->{'name'}} } @usync;
75         local @gneed = grep { !$gsync{$_->{'name'}} } @gsync;
76         if (@uneed || @gneed) {
77                 &remote_foreign_require($serv->{'host'},
78                                         "acl", "acl-lib.pl");
79                 $done_require{$serv->{'host'}}++;
80                 if ($add_error_msg) {
81                         # Host is down!
82                         print "$add_error_msg<p>\n";
83                         print "</ul>\n";
84                         next;
85                         }
86
87                 # Create missing users
88                 foreach $u (@uneed) {
89                         # Create the user
90                         print &text('sync_ucreate', $u->{'name'}),"<br>\n";
91                         if (!$in{'test'}) {
92                                 &remote_foreign_call($serv->{'host'},
93                                         "acl", "create_user", $u);
94                                 push(@{$host->{'users'}}, $u);
95                                 }
96                         print "$text{'refresh_done'}<p>\n";
97
98                         # Copy .acl files
99                         print &text('sync_acl', $u->{'name'}),"<br>\n";
100                         &copy_acls_for($u, $serv, "user");
101                         print "$text{'refresh_done'}<p>\n";
102                         }
103
104                 # Create missing groups
105                 foreach $g (@gneed) {
106                         print &text('sync_gcreate', $g->{'group'}),"<br>\n";
107                         if (!$in{'test'}) {
108                                 &remote_foreign_call($serv->{'host'},
109                                         "acl", "create_group", $g);
110                                 push(@{$host->{'groups'}}, $g);
111                                 }
112                         print "$text{'refresh_done'}<p>\n";
113
114                         print &text('sync_acl', $g->{'name'}),"<br>\n";
115                         &copy_acls_for($g, $serv, "group");
116                         print "$text{'refresh_done'}<p>\n";
117                         }
118
119                 # Restart webmin
120                 print "$text{'sync_restart'}<br>\n";
121                 if (!$in{'test'}) {
122                         &remote_foreign_call($serv->{'host'}, "acl",
123                                              "reload_miniserv");
124                         }
125                 print "$text{'refresh_done'}<p>\n";
126
127                 # Update in local list
128                 &save_webmin_host($host);
129                 }
130         else {
131                 print "$text{'sync_insync'}<p>\n";
132                 }
133         print "</ul>\n";
134         }
135 &webmin_log("sync", undef, undef, \%in);
136
137 &ui_print_footer("", $text{'index_return'});
138
139 # copy_acls_for(&user|&group, &server)
140 # Copy all .acl files from the server on which a user or group does exist
141 # to the given server
142 sub copy_acls_for
143 {
144 local $source = $_[0]->{'source'};
145 &remote_foreign_require($source->{'host'}, "acl", "acl-lib.pl")
146         if (!$done_require{$source->{'host'}}++);
147 local $suffix = $_[2] eq "user" ? "module_acl" : "group_module_acl";
148 foreach $m (@{$_[0]->{'modules'}}) {
149         local %acl = &remote_foreign_call($source->{'host'}, "acl",
150                                           "get_".$suffix, $_[0]->{'name'},$m);
151         if (%acl) {
152                 &remote_foreign_call($_[1]->{'host'}, "acl", "save_".$suffix,
153                                      \%acl, $_[0]->{'name'}, $m);
154                 }
155         }
156 }
157