Handle hostnames with upper-case letters
[webmin.git] / cluster-useradmin / cluster-useradmin-lib.pl
1 # cluster-useradmin-lib.pl
2 # common functions for managing users across a cluster
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6 &init_config();
7 &foreign_require("servers", "servers-lib.pl");
8 %useradmin_text = &load_language("useradmin");
9 %text = ( %useradmin_text, %text );
10 %uconfig = &foreign_config("useradmin");
11
12 # list_useradmin_hosts()
13 # Returns a list of all hosts whose users are being managed by this module
14 sub list_useradmin_hosts
15 {
16 local %smap = map { $_->{'id'}, $_ } &list_servers();
17 local $hdir = "$module_config_directory/hosts";
18 opendir(DIR, $hdir);
19 local ($h, @rv);
20 foreach $h (readdir(DIR)) {
21         next if ($h eq "." || $h eq ".." || !-d "$hdir/$h");
22         local %host = ( 'id', $h );
23         next if (!$smap{$h});   # underlying server was deleted
24         opendir(UDIR, "$hdir/$h");
25         foreach $f (readdir(UDIR)) {
26                 if ($f =~ /^(\S+)\.user$/) {
27                         local %user;
28                         &read_file("$hdir/$h/$f", \%user);
29                         push(@{$host{'users'}}, \%user);
30                         }
31                 elsif ($f =~ /^(\S+)\.group$/) {
32                         local %group;
33                         &read_file("$hdir/$h/$f", \%group);
34                         push(@{$host{'groups'}}, \%group);
35                         }
36                 }
37         closedir(UDIR);
38         push(@rv, \%host);
39         }
40 closedir(DIR);
41 return @rv;
42 }
43
44 # save_useradmin_host(&host)
45 sub save_useradmin_host
46 {
47 local $hdir = "$module_config_directory/hosts";
48 local %oldfile;
49 mkdir($hdir, 0700);
50 if (-d "$hdir/$_[0]->{'id'}") {
51         opendir(DIR, "$hdir/$_[0]->{'id'}");
52         map { $oldfile{$_}++ } readdir(DIR);
53         closedir(DIR);
54         }
55 else {
56         mkdir("$hdir/$_[0]->{'id'}", 0700);
57         }
58 foreach $u (@{$_[0]->{'users'}}) {
59         &write_file("$hdir/$_[0]->{'id'}/$u->{'user'}.user", $u);
60         delete($oldfile{"$u->{'user'}.user"});
61         }
62 foreach $g (@{$_[0]->{'groups'}}) {
63         &write_file("$hdir/$_[0]->{'id'}/$g->{'group'}.group", $g);
64         delete($oldfile{"$g->{'group'}.group"});
65         }
66 unlink(map { "$hdir/$_[0]->{'id'}/$_" } keys %oldfile);
67 }
68
69 # delete_useradmin_host(&host)
70 sub delete_useradmin_host
71 {
72 system("rm -rf '$module_config_directory/hosts/$_[0]->{'id'}'");
73 }
74
75 # list_servers()
76 # Returns a list of all servers from the webmin servers module that can be
77 # managed, plus this server
78 sub list_servers
79 {
80 local @servers = &servers::list_servers_sorted();
81 return ( &servers::this_server(), grep { $_->{'user'} } @servers );
82 }
83
84 # auto_home_dir(base, username)
85 # Returns an automatically generated home directory, and creates needed
86 # parent dirs
87 sub auto_home_dir
88 {
89 if ($uconfig{'home_style'} == 0) {
90         return $_[0]."/".$_[1];
91         }
92 elsif ($uconfig{'home_style'} == 1) {
93         return $_[0]."/".substr($_[1], 0, 1)."/".$_[1];
94         }
95 elsif ($uconfig{'home_style'} == 2) {
96         return $_[0]."/".substr($_[1], 0, 1)."/".
97                substr($_[1], 0, 2)."/".$_[1];
98         }
99 elsif ($uconfig{'home_style'} == 3) {
100         return $_[0]."/".substr($_[1], 0, 1)."/".
101                substr($_[1], 1, 1)."/".$_[1];
102         }
103 }
104
105 # server_name(&server)
106 sub server_name
107 {
108 return $_[0]->{'desc'} ? $_[0]->{'desc'} : $_[0]->{'host'};
109 }
110
111 # supports_gothers(&server)
112 # Returns 1 if some server supports group syncing, 0 if not
113 sub supports_gothers
114 {
115 local $vers = $remote_server_version{$_[0]->{'host'}} ||
116               &remote_foreign_call($_[0]->{'host'}, "useradmin",
117                                   "get_webmin_version");
118 return $vers >= 1.090;
119 }
120
121 # create_on_input(desc, [no-donthave], [multiple])
122 sub create_on_input
123 {
124 local @hosts = &list_useradmin_hosts();
125 local @servers = &list_servers();
126 if ($_[0]) {
127         print "<tr> <td><b>$_[0]</b></td>\n";
128         print "<td colspan=2>\n";
129         }
130 if ($_[2]) {
131         print "<select name=server size=5 multiple>\n";
132         }
133 else {
134         print "<select name=server>\n";
135         }
136 print "<option value=-1>$text{'uedit_all'}\n";
137 print "<option value=-2>$text{'uedit_donthave'}\n" if (!$_[1]);
138 local @groups = &servers::list_all_groups(\@servers);
139 local $h;
140 foreach $h (@hosts) {
141         local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
142         if ($s) {
143                 print "<option value='$s->{'id'}'>",
144                         $s->{'desc'} ? $s->{'desc'} : $s->{'host'},"\n";
145                 $gothost{$s->{'host'}}++;
146                 }
147         }
148 local $g;
149 foreach $g (@groups) {
150         local ($found, $m);
151         foreach $m (@{$g->{'members'}}) {
152                 ($found++, last) if ($gothost{$m});
153                 }
154         print "<option value='group_$g->{'name'}'>",
155                 &text('uedit_group', $g->{'name'}),"\n" if ($found);
156         }
157 print "</select>\n";
158 if ($_[0]) {
159         print "</td> </tr>\n";
160         }
161 }
162
163 # create_on_parse(prefix, &already, name, [no-print])
164 sub create_on_parse
165 {
166 local @allhosts = &list_useradmin_hosts();
167 local @servers = &list_servers();
168 local @hosts;
169 local $server;
170 foreach $server (split(/\0/, $in{'server'})) {
171         if ($server == -2) {
172                 # Check who has it already
173                 local %already = map { $_->{'id'}, 1 } @{$_[1]};
174                 push(@hosts, grep { !$already{$_->{'id'}} } @allhosts);
175                 print "<b>",&text($_[0].'3', $_[2]),"</b><p>\n" if (!$_[3]);
176                 }
177         elsif ($server =~ /^group_(.*)/) {
178                 # Install on members of some group
179                 local ($group) = grep { $_->{'name'} eq $1 }
180                                       &servers::list_all_groups(\@servers);
181                 push(@hosts, grep { local $hid = $_->{'id'};
182                                 local ($s) = grep { $_->{'id'} == $hid } @servers;
183                                 &indexof($s->{'host'}, @{$group->{'members'}}) >= 0 }
184                               @allhosts);
185                 print "<b>",&text($_[0].'4', $_[2], $group->{'name'}),
186                       "</b><p>\n" if (!$_[3]);
187                 }
188         elsif ($server != -1) {
189                 # Just install on one host
190                 local ($onehost) = grep { $_->{'id'} == $server } @allhosts;
191                 push(@hosts, $onehost);
192                 local ($s) = grep { $_->{'id'} == $onehost->{'id'} } @servers;
193                 print "<b>",&text($_[0].'5', $_[2],
194                                   &server_name($s)),"</b><p>\n" if (!$_[3]);
195                 }
196         else {
197                 # Installing on every host
198                 push(@hosts, @allhosts);
199                 print "<b>",&text($_[0], join(" ", @names)),
200                       "</b><p>\n" if (!$_[3]);
201                 }
202         }
203 return &unique(@hosts);
204 }
205
206 1;
207