Handle hostnames with upper-case letters
[webmin.git] / cluster-webmin / create_group.cgi
1 #!/usr/local/bin/perl
2 # create_group.cgi
3 # Create a new Webmin group across multiple servers
4
5 require './cluster-webmin-lib.pl';
6 &ReadParse();
7 &error_setup($text{'group_err1'});
8 @hosts = &list_webmin_hosts();
9
10 # Validate inputs
11 $in{'name'} =~ /^[A-z0-9\-\_\.]+$/ ||
12         &error(&text('user_ename', $in{'name'}));
13
14 # Setup error handler for down hosts
15 sub group_error
16 {
17 $group_error_msg = join("", @_);
18 }
19 &remote_error_setup(\&group_error);
20
21 # Work out which hosts to create on
22 &ui_print_header(undef, $text{'group_title1'}, "");
23 foreach $h (@hosts) {
24         local ($alr) = grep { $_->{'name'} eq $in{'name'} } @{$h->{'groups'}};
25         push(@already, $h) if ($alr);
26         }
27 @hosts = &create_on_parse('group_doing', \@already, $in{'name'});
28 foreach $h (@hosts) {
29         foreach $ug (@{$h->{'users'}}, @{$h->{'groups'}}) {
30                 $taken{$ug->{'name'}}++;
31                 }
32         }
33 $taken{$in{'name'}} && &error(&text('user_etaken', $in{'name'}));
34
35 @servers = &list_servers();
36 $p = 0;
37 foreach $h (@hosts) {
38         local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
39         local ($rh = "READ$p", $wh = "WRITE$p");
40         pipe($rh, $wh);
41         if (!fork()) {
42                 close($rh);
43                 &remote_foreign_require($s->{'host'}, "acl", "acl-lib.pl");
44                 if ($group_error_msg) {
45                         # Host is down
46                         print $wh &serialise_variable([ 0, $group_error_msg ]);
47                         exit;
48                         }
49
50                 # Create the group
51                 local %newgrp = ( 'name', $in{'name'} );
52                 local @mods = ( split(/\0/, $in{'mods1'}),
53                                 split(/\0/, $in{'mods2'}),
54                                 split(/\0/, $in{'mods3'}) );
55
56                 if ($in{'group'}) {
57                         # Add group to the chosen group
58                         ($group) = grep { $_->{'name'} eq $in{'group'} }
59                                         @{$h->{'groups'}};
60                         if (!$group) {
61                                 # Doesn't exist on this server
62                                 print $wh &serialise_variable(
63                                         [ 0, $text{'user_egroup'} ]);
64                                 exit;
65                                 }
66                         push(@{$group->{'members'}}, $newgrp{'name'});
67                         &remote_foreign_call($s->{'host'}, "acl", "modify_group",
68                                              $group->{'name'}, $group);
69
70                         # Add modules from group
71                         local @ownmods;
72                         foreach $m (@mods) {
73                                 push(@ownmods, $m)
74                                     if (&indexof($m, @{$group->{'modules'}}) < 0);
75                                 }
76                         @mods = &unique(@mods, @{$group->{'modules'}});
77                         $newgrp{'ownmods'} = \@ownmods;
78
79                         # Copy ACL files for group
80                         &remote_foreign_call($s->{'host'}, "acl", "copy_acl_files",
81                                              $group->{'name'}, $newgrp{'name'},
82                                              [ @{$group->{'modules'}}, "" ]);
83                         }
84
85                 $newgrp{'modules'} = \@mods;
86                 &remote_foreign_call($s->{'host'}, "acl", "create_group", \%newgrp);
87                 push(@{$h->{'groups'}}, \%newgrp);
88                 &save_webmin_host($h);
89
90                 # Restart the remote webmin
91                 print $wh &serialise_variable([ 1 ]);
92                 &remote_foreign_call($s->{'host'}, "acl", "restart_miniserv");
93                 exit;
94                 }
95         close($wh);
96         $p++;
97         }
98
99 # Read back the results
100 $p = 0;
101 foreach $h (@hosts) {
102         local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
103         local $d = &server_name($s);
104         local $rh = "READ$p";
105         local $line = <$rh>;
106         local $rv = &unserialise_variable($line);
107         close($rh);
108
109         if ($rv && $rv->[0] == 1) {
110                 # It worked
111                 print &text('group_success', $d),"<br>\n";
112                 }
113         else {
114                 # Something went wrong
115                 print &text('group_failed', $d, $rv->[1]),"<br>\n";
116                 }
117         $p++;
118         }
119
120 print "<p><b>$text{'group_done'}</b><p>\n";
121
122 &remote_finished();
123 &ui_print_footer("", $text{'index_return'});
124