Handle hostnames with upper-case letters
[webmin.git] / cluster-webmin / delete_user.cgi
1 #!/usr/local/bin/perl
2 # delete_user.cgi
3 # Delete a webmin user across all servers
4
5 require './cluster-webmin-lib.pl';
6 &ReadParse();
7 &ui_print_header(undef, $text{'udelete_title'}, "");
8 print "<b>",&text('udelete_doing', $in{'user'}),"</b><p>\n";
9
10 # Setup error handler for down hosts
11 sub user_error
12 {
13 $user_error_msg = join("", @_);
14 }
15 &remote_error_setup(\&user_error);
16
17 # Delete the user on all servers that have him
18 foreach $h (&list_webmin_hosts()) {
19         foreach $u (@{$h->{'users'}}) {
20                 if ($u->{'name'} eq $in{'user'}) {
21                         push(@hosts, $h);
22                         last;
23                         }
24                 }
25         }
26 @servers = &list_servers();
27 $p = 0;
28 foreach $h (@hosts) {
29         local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
30         local ($rh = "READ$p", $wh = "WRITE$p");
31         pipe($rh, $wh);
32         if (!fork()) {
33                 close($rh);
34                 &remote_foreign_require($s->{'host'}, "acl", "acl-lib.pl");
35                 if ($user_error_msg) {
36                         # Host is down
37                         print $wh &serialise_variable([ 0, $user_error_msg ]);
38                         exit;
39                         }
40
41                 # Delete the user
42                 &remote_foreign_call($s->{'host'}, "acl", "delete_user",
43                                      $in{'user'});
44                 $h->{'users'} = [ grep { $_->{'name'} ne $in{'user'} }
45                                        @{$h->{'users'}} ];
46
47                 # Remove from any groups
48                 foreach $g (@{$h->{'groups'}}) {
49                         local @mems = @{$g->{'members'}};
50                         local $i = &indexof($in{'user'}, @mems);
51                         if ($i >= 0) {
52                                 splice(@mems, $i, 1);
53                                 $g->{'members'} = \@mems;
54                                 &remote_foreign_call($s->{'host'}, "acl",
55                                                 "modify_group", $g->{'name'}, $g);
56                                 }
57                         }
58                 &save_webmin_host($h);
59
60                 # Restart the remote webmin
61                 print $wh &serialise_variable([ 1 ]);
62                 &remote_foreign_call($s->{'host'}, "acl", "restart_miniserv");
63                 exit;
64                 }
65         close($wh);
66         $p++;
67         }
68
69 # Read back the results
70 $p = 0;
71 foreach $h (@hosts) {
72         local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
73         local $d = &server_name($s);
74         local $rh = "READ$p";
75         local $line = <$rh>;
76         local $rv = &unserialise_variable($line);
77         close($rh);
78
79         if ($rv && $rv->[0] == 1) {
80                 # It worked
81                 print &text('udelete_success', $d),"<br>\n";
82                 }
83         else {
84                 # Something went wrong
85                 print &text('udelete_failed', $d, $rv->[1]),"<br>\n";
86                 }
87         $p++;
88         }
89
90 print "<p><b>$text{'udelete_done'}</b><p>\n";
91
92 &remote_finished();
93 &ui_print_footer("", $text{'index_return'});
94