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