Handle hostnames with upper-case letters
[webmin.git] / cluster-webmin / cluster-webmin-lib.pl
1 # cluster-webmin-lib.pl
2 # Common functions for managing webmin installs across a cluster
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6 &init_config();
7 &foreign_require("servers", "servers-lib.pl");
8
9 # list_webmin_hosts()
10 # Returns a list of all hosts whose webmin modules are being managed
11 sub list_webmin_hosts
12 {
13 local %smap = map { $_->{'id'}, $_ } &list_servers();
14 local $hdir = "$module_config_directory/hosts";
15 opendir(DIR, $hdir);
16 local ($h, @rv);
17 foreach $h (readdir(DIR)) {
18         next if ($h eq "." || $h eq ".." || !-d "$hdir/$h");
19         local %host = ( 'id', $h );
20         next if (!$smap{$h});   # underlying server was deleted
21         local $f;
22         opendir(MDIR, "$hdir/$h");
23         foreach $f (readdir(MDIR)) {
24                 if ($f =~ /^(\S+)\.mod$/) {
25                         local %mod;
26                         &read_file("$hdir/$h/$f", \%mod);
27                         push(@{$host{'modules'}}, \%mod);
28                         }
29                 elsif ($f =~ /^(\S+)\.theme$/) {
30                         local %theme;
31                         &read_file("$hdir/$h/$f", \%theme);
32                         push(@{$host{'themes'}}, \%theme);
33                         }
34                 elsif ($f =~ /^(\S+)\.user$/) {
35                         local %user;
36                         &read_file("$hdir/$h/$f", \%user);
37                         $user{'modules'} = [ split(/\s+/, $user{'modules'}) ];
38                         $user{'ownmods'} = [ split(/\s+/, $user{'ownmods'}) ];
39                         push(@{$host{'users'}}, \%user);
40                         }
41                 elsif ($f =~ /^(\S+)\.group$/) {
42                         local %group;
43                         &read_file("$hdir/$h/$f", \%group);
44                         $group{'modules'} = [ split(/\s+/, $group{'modules'}) ];
45                         $group{'ownmods'} = [ split(/\s+/, $group{'ownmods'}) ];
46                         $group{'members'} = [ split(/\s+/, $group{'members'}) ];
47                         push(@{$host{'groups'}}, \%group);
48                         }
49                 elsif ($f eq "webmin") {
50                         &read_file("$hdir/$h/$f", \%host);
51                         }
52                 }
53         closedir(MDIR);
54         push(@rv, \%host);
55         }
56 closedir(DIR);
57 return @rv;
58 }
59
60 # save_webmin_host(&host)
61 sub save_webmin_host
62 {
63 local $hdir = "$module_config_directory/hosts";
64 local %oldfile;
65 mkdir($hdir, 0700);
66 if (-d "$hdir/$_[0]->{'id'}") {
67         opendir(DIR, "$hdir/$_[0]->{'id'}");
68         map { $oldfile{$_}++ } readdir(DIR);
69         closedir(DIR);
70         }
71 else {
72         mkdir("$hdir/$_[0]->{'id'}", 0700);
73         }
74 local $m;
75 foreach $m (@{$_[0]->{'modules'}}) {
76         &write_file("$hdir/$_[0]->{'id'}/$m->{'dir'}.mod", $m);
77         delete($oldfile{"$m->{'dir'}.mod"});
78         }
79 foreach $m (@{$_[0]->{'themes'}}) {
80         &write_file("$hdir/$_[0]->{'id'}/$m->{'dir'}.theme", $m);
81         delete($oldfile{"$m->{'dir'}.theme"});
82         }
83 foreach $m (@{$_[0]->{'users'}}) {
84         local %u = %$m;
85         $u{'modules'} = join(" ", @{$u{'modules'}});
86         $u{'ownmods'} = join(" ", @{$u{'ownmods'}});
87         &write_file("$hdir/$_[0]->{'id'}/$u{'name'}.user", \%u);
88         delete($oldfile{"$u{'name'}.user"});
89         }
90 foreach $m (@{$_[0]->{'groups'}}) {
91         local %g = %$m;
92         $g{'modules'} = join(" ", @{$g{'modules'}});
93         $g{'ownmods'} = join(" ", @{$g{'ownmods'}});
94         $g{'members'} = join(" ", @{$g{'members'}});
95         &write_file("$hdir/$_[0]->{'id'}/$g{'name'}.group", \%g);
96         delete($oldfile{"$g{'name'}.group"});
97         }
98 local %webmin = %{$_[0]};
99 delete($webmin{'modules'});
100 delete($webmin{'themes'});
101 delete($webmin{'users'});
102 delete($webmin{'groups'});
103 delete($webmin{'id'});
104 &write_file("$hdir/$_[0]->{'id'}/webmin", \%webmin);
105 delete($oldfile{"webmin"});
106 unlink(map { "$hdir/$_[0]->{'id'}/$_" } keys %oldfile);
107 }
108
109 # delete_webmin_host(&host)
110 sub delete_webmin_host
111 {
112 system("rm -rf '$module_config_directory/hosts/$_[0]->{'id'}'");
113 }
114
115 # list_servers()
116 # Returns a list of all servers from the webmin servers module that can be
117 # managed, plus this server
118 sub list_servers
119 {
120 local @servers = &servers::list_servers_sorted();
121 return ( &servers::this_server(), grep { $_->{'user'} } @servers );
122 }
123
124 # server_name(&server)
125 sub server_name
126 {
127 return $_[0]->{'desc'} ? $_[0]->{'desc'} : $_[0]->{'host'};
128 }
129
130 # all_modules(&hosts)
131 sub all_modules
132 {
133 local (%done, $u, %descc);
134 local @uniq = grep { !$done{$_->{'dir'}}++ } map { @{$_->{'modules'}} } @{$_[0]};
135 map { $descc{$_->{'desc'}}++ } @uniq;
136 foreach $u (@uniq) {
137         $u->{'desc'} .= " ($u->{'dir'})" if ($descc{$u->{'desc'}} > 1);
138         }
139 return sort { $a->{'desc'} cmp $b->{'desc'} } @uniq;
140 }
141
142 # all_themes(&hosts)
143 sub all_themes
144 {
145 local %done;
146 return sort { $a->{'desc'} cmp $b->{'desc'} }
147         grep { !$done{$_->{'dir'}}++ }
148          map { @{$_->{'themes'}} } @{$_[0]};
149 }
150
151 # all_groups(&hosts)
152 sub all_groups
153 {
154 local %done;
155 return sort { $a->{'name'} cmp $b->{'name'} }
156         grep { !$done{$_->{'name'}}++ }
157          map { @{$_->{'groups'}} } @{$_[0]};
158 }
159
160 # all_users(&hosts)
161 sub all_users
162 {
163 local %done;
164 return sort { $a->{'name'} cmp $b->{'name'} }
165         grep { !$done{$_->{'name'}}++ }
166          map { @{$_->{'users'}} } @{$_[0]};
167 }
168
169 # create_on_input(desc, [no-donthave], [no-have], [multiple])
170 sub create_on_input
171 {
172 local @hosts = &list_webmin_hosts();
173 local @servers = &list_servers();
174 if ($_[0]) {
175         print "<tr> <td><b>$_[0]</b></td>\n";
176         print "<td>\n";
177         }
178 if ($_[3]) {
179         print "<select name=server size=5 multiple>\n";
180         }
181 else {
182         print "<select name=server>\n";
183         }
184 print "<option value=-1>$text{'user_all'}\n";
185 print "<option value=-2>$text{'user_donthave'}\n" if (!$_[1]);
186 print "<option value=-3>$text{'user_have'}\n" if (!$_[2]);
187 local @groups = &servers::list_all_groups(\@servers);
188 local $h;
189 foreach $h (@hosts) {
190         local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
191         if ($s) {
192                 print "<option value='$s->{'id'}'>",
193                         $s->{'desc'} ? $s->{'desc'} : $s->{'host'},"\n";
194                 $gothost{$s->{'host'}}++;
195                 }
196         }
197 local $g;
198 foreach $g (@groups) {
199         local ($found, $m);
200         foreach $m (@{$g->{'members'}}) {
201                 ($found++, last) if ($gothost{$m});
202                 }
203         print "<option value='group_$g->{'name'}'>",
204                 &text('user_ofgroup', $g->{'name'}),"\n" if ($found);
205         }
206 print "</select>\n";
207 if ($_[0]) {
208         print "</td> </tr>\n";
209         }
210 }
211
212 # create_on_parse(prefix, &already, name, [no-print])
213 sub create_on_parse
214 {
215 local @allhosts = &list_webmin_hosts();
216 local @servers = &list_servers();
217 local @hosts;
218 local $server;
219 foreach $server (split(/\0/, $in{'server'})) {
220         if ($server == -2) {
221                 # Install on hosts that don't have it
222                 local %already = map { $_->{'id'}, 1 } @{$_[1]};
223                 push(@hosts, grep { !$already{$_->{'id'}} } @allhosts);
224                 print "<b>",&text($_[0].'3', $_[2]),"</b><p>\n" if (!$_[3]);
225                 }
226         elsif ($server == -3) {
227                 # Install on hosts that do have it
228                 local %already = map { $_->{'id'}, 1 } @{$_[1]};
229                 push(@hosts, grep { $already{$_->{'id'}} } @allhosts);
230                 print "<b>",&text($_[0].'6', $_[2]),"</b><p>\n" if (!$_[3]);
231                 }
232         elsif ($server =~ /^group_(.*)/) {
233                 # Install on members of some group
234                 local ($group) = grep { $_->{'name'} eq $1 }
235                                       &servers::list_all_groups(\@servers);
236                 push(@hosts, grep { local $hid = $_->{'id'};
237                                 local ($s) = grep { $_->{'id'} == $hid } @servers;
238                                 &indexof($s->{'host'}, @{$group->{'members'}}) >= 0 }
239                               @allhosts);
240                 print "<b>",&text($_[0].'4', $_[2], $group->{'name'}),
241                       "</b><p>\n" if (!$_[3]);
242                 }
243         elsif ($server != -1) {
244                 # Just install on one host
245                 local ($onehost) = grep { $_->{'id'} == $server }
246                                         @allhosts;
247                 push(@hosts, $onehost);
248                 local ($s) = grep { $_->{'id'} == $onehost->{'id'} } @servers;
249                 print "<b>",&text($_[0].'5', $_[2],
250                                   &server_name($s)),"</b><p>\n" if (!$_[3]);
251                 }
252         else {
253                 # Installing on every host
254                 push(@hosts, @allhosts);
255                 print "<b>",&text($_[0], join(" ", @names)),
256                       "</b><p>\n" if (!$_[3]);
257                 }
258         }
259 return &unique(@hosts);
260 }
261
262 1;
263