1# cluster-usermin-lib.pl 2# Common functions for managing usermin installs across a cluster 3 4BEGIN { push(@INC, ".."); }; 5use 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 12sub list_usermin_hosts 13{ 14local %smap = map { $_->{'id'}, $_ } &list_servers(); 15local $hdir = "$module_config_directory/hosts"; 16opendir(DIR, $hdir); 17local ($h, @rv); 18foreach $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 } 42closedir(DIR); 43return @rv; 44} 45 46# save_usermin_host(&host) 47sub save_usermin_host 48{ 49local $hdir = "$module_config_directory/hosts"; 50local %oldfile; 51mkdir($hdir, 0700); 52if (-d "$hdir/$_[0]->{'id'}") { 53 opendir(DIR, "$hdir/$_[0]->{'id'}"); 54 map { $oldfile{$_}++ } readdir(DIR); 55 closedir(DIR); 56 } 57else { 58 mkdir("$hdir/$_[0]->{'id'}", 0700); 59 } 60local $m; 61foreach $m (@{$_[0]->{'modules'}}) { 62 &write_file("$hdir/$_[0]->{'id'}/$m->{'dir'}.mod", $m); 63 delete($oldfile{"$m->{'dir'}.mod"}); 64 } 65foreach $m (@{$_[0]->{'themes'}}) { 66 &write_file("$hdir/$_[0]->{'id'}/$m->{'dir'}.theme", $m); 67 delete($oldfile{"$m->{'dir'}.theme"}); 68 } 69local %usermin = %{$_[0]}; 70delete($usermin{'modules'}); 71delete($usermin{'themes'}); 72delete($usermin{'id'}); 73&write_file("$hdir/$_[0]->{'id'}/usermin", \%usermin); 74delete($oldfile{"usermin"}); 75unlink(map { "$hdir/$_[0]->{'id'}/$_" } keys %oldfile); 76} 77 78# delete_usermin_host(&host) 79sub delete_usermin_host 80{ 81system("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 87sub list_servers 88{ 89local @servers = &servers::list_servers_sorted(); 90return ( &servers::this_server(), grep { $_->{'user'} } @servers ); 91} 92 93# server_name(&server) 94sub server_name 95{ 96return $_[0]->{'desc'} ? $_[0]->{'desc'} : $_[0]->{'host'}; 97} 98 99# all_modules(&hosts) 100sub all_modules 101{ 102local (%done, $u, %descc); 103local @uniq = grep { !$done{$_->{'dir'}}++ } map { @{$_->{'modules'}} } @{$_[0]}; 104map { $descc{$_->{'desc'}}++ } @uniq; 105foreach $u (@uniq) { 106 $u->{'desc'} .= " ($u->{'dir'})" if ($descc{$u->{'desc'}} > 1); 107 } 108return sort { $a->{'desc'} cmp $b->{'desc'} } @uniq; 109} 110 111# all_themes(&hosts) 112sub all_themes 113{ 114local %done; 115return sort { $a->{'desc'} cmp $b->{'desc'} } 116 grep { !$done{$_->{'dir'}}++ } 117 map { @{$_->{'themes'}} } @{$_[0]}; 118} 119 120# all_groups(&hosts) 121sub all_groups 122{ 123local %done; 124return sort { $a->{'name'} cmp $b->{'name'} } 125 grep { !$done{$_->{'name'}}++ } 126 map { @{$_->{'groups'}} } @{$_[0]}; 127} 128 129# all_users(&hosts) 130sub all_users 131{ 132local %done; 133return 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]) 139sub create_on_input 140{ 141local @hosts = &list_usermin_hosts(); 142local @servers = &list_servers(); 143if ($_[0]) { 144 print "<tr> <td><b>$_[0]</b></td>\n"; 145 print "<td>\n"; 146 } 147if ($_[3]) { 148 print "<select name=server size=5 multiple>\n"; 149 } 150else { 151 print "<select name=server>\n"; 152 } 153print "<option value=-1>$text{'user_all'}</option>\n"; 154print "<option value=-2>$text{'user_donthave'}</option>\n" if (!$_[1]); 155print "<option value=-3>$text{'user_have'}</option>\n" if (!$_[2]); 156local @groups = &servers::list_all_groups(\@servers); 157local $h; 158foreach $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'},"</option>\n"; 163 $gothost{$s->{'host'}}++; 164 } 165 } 166local $g; 167foreach $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'}),"</option>\n" if ($found); 174 } 175print "</select>\n"; 176if ($_[0]) { 177 print "</td> </tr>\n"; 178 } 179} 180 181# create_on_parse(prefix, &already, name, [no-print]) 182sub create_on_parse 183{ 184local @allhosts = &list_usermin_hosts(); 185local @servers = &list_servers(); 186local @hosts; 187local $server; 188foreach $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 } 228return &unique(@hosts); 229} 230 231# get_usermin_text() 232# Returns the text hash for Usermin, on this system 233sub get_usermin_text 234{ 235local %text; 236local %miniserv; 237&usermin::get_usermin_miniserv_config(\%miniserv); 238local $root = $miniserv{'root'}; 239local %ugconfig; 240&usermin::get_usermin_config(\%ugconfig); 241local $ol = $ugconfig{'overlang'}; 242local $o; 243local ($dir) = ($_[1] || "lang"); 244 245# Read global lang files 246foreach $o (@lang_order_list) { 247 local $ok = &read_file_cached("$root/$dir/$o", \%text); 248 return () if (!$ok && $o eq $default_lang); 249 } 250if ($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 257if ($_[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 } 270foreach $k (keys %text) { 271 $text{$k} =~ s/\$(\{([^\}]+)\}|([A-Za-z0-9\.\-\_]+))/text_subs($2 || $3,\%text)/ge; 272 } 273return %text; 274 275} 276 2771; 278 279