Handle hostnames with upper-case letters
[webmin.git] / cluster-usermin / sync.cgi
1 #!/usr/local/bin/perl
2 # sync.cgi
3 # Create missing users and groups on servers
4
5 require './cluster-usermin-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                         print &text('sync_acl', $u->{'name'}),"<br>\n";
99                         &copy_acls_for($u, $serv);
100                         print "$text{'refresh_done'}<p>\n";
101                         }
102
103                 # Create missing groups
104                 foreach $g (@gneed) {
105                         print &text('sync_gcreate', $g->{'group'}),"<br>\n";
106                         if (!$in{'test'}) {
107                                 &remote_foreign_call($serv->{'host'},
108                                         "acl", "create_group", $g);
109                                 push(@{$host->{'groups'}}, $g);
110                                 }
111                         print "$text{'refresh_done'}<p>\n";
112
113                         print &text('sync_acl', $g->{'name'}),"<br>\n";
114                         &copy_acls_for($g, $serv);
115                         print "$text{'refresh_done'}<p>\n";
116                         }
117
118                 # Restart webmin
119                 print "$text{'sync_restart'}<br>\n";
120                 if (!$in{'test'}) {
121                         &remote_foreign_call($serv->{'host'}, "acl",
122                                              "restart_miniserv");
123                         }
124                 print "$text{'refresh_done'}<p>\n";
125
126                 # Update in local list
127                 &save_webmin_host($host);
128                 }
129         else {
130                 print "$text{'sync_insync'}<p>\n";
131                 }
132         print "</ul>\n";
133         }
134 &webmin_log("sync", undef, undef, \%in);
135
136 &ui_print_footer("", $text{'index_return'});
137
138 # copy_acls_for(&user|&group, &server)
139 # Copy all .acl files from the server on which a user or group does exist
140 # to the given server
141 sub copy_acls_for
142 {
143 local $source = $_[0]->{'source'};
144 &remote_foreign_require($source->{'host'}, "acl", "acl-lib.pl")
145         if (!$done_require{$source->{'host'}}++);
146 foreach $m (@{$_[0]->{'modules'}}) {
147         local %acl = &remote_foreign_call($source->{'host'}, "acl",
148                                           "get_module_acl", $_[0]->{'name'},$m);
149         if (%acl) {
150                 &remote_foreign_call($_[1]->{'host'}, "acl", "save_module_acl",
151                                      \%acl, $_[0]->{'name'}, $m);
152                 }
153         }
154 }
155