Handle hostnames with upper-case letters
[webmin.git] / nis / openmamba-linux-lib.pl
1 # openmaba-linux-lib.pl
2 # NIS functions for openmamba linux NIS client and server
3
4 $yp_makefile = "/var/yp/Makefile";
5 $ypserv_conf = "/etc/ypserv.conf";
6 $pid_file = "/var/run/ypserv.pid";
7
8 # get_nis_support()
9 # Returns 0 for no support, 1 for client only, 2 for server and 3 for both
10 sub get_nis_support
11 {
12 local $rv;
13 $rv += 1 if (&has_command("ypbind"));
14 $rv += 2 if (&has_command("ypserv"));
15 return $rv;
16 }
17
18 # get_client_config()
19 # Returns a hash ref containg details of the client's NIS settings
20 sub get_client_config
21 {
22 local $nis;
23 open(CONF, $config{'client_conf'});
24 while(<CONF>) {
25         s/\r|\n//g;
26         s/#.*$//g;
27         if (/^\s*domain\s*(\S+)\s*broadcast/i) {
28                 $nis->{'broadcast'}++;
29                 }
30         elsif (/^\s*domain\s*(\S+)\s*server\s*(\S+)/i) {
31                 push(@{$nis->{'servers'}}, $2);
32                 }
33         elsif (/^\s*ypserver\s*(\S+)/) {
34                 push(@{$nis->{'servers'}}, $1);
35                 }
36         }
37 close(CONF);
38 local %network;
39 &read_env_file("/etc/sysconfig/network", \%network);
40 $nis->{'domain'} = $network{'NISDOMAIN'};
41 return $nis;
42 }
43
44 # save_client_config(&config)
45 # Saves and applies the NIS client configuration in the give hash.
46 # Returns an error message if any, or undef on success.
47 sub save_client_config
48 {
49 # Save the config file
50 &open_tempfile(CONF, ">$config{'client_conf'}");
51 if ($_[0]->{'domain'}) {
52         if ($_[0]->{'broadcast'}) {
53                 &print_tempfile(CONF, "domain $_[0]->{'domain'} broadcast\n");
54                 }
55         else {
56                 local @s = @{$_[0]->{'servers'}};
57                 &print_tempfile(CONF, "domain $_[0]->{'domain'} server $s[0]\n");
58                 foreach $s (@s) {
59                         &print_tempfile(CONF, "ypserver $s\n");
60                         }
61                 }
62         }
63 &close_tempfile(CONF);
64 local %network;
65 &read_env_file("/etc/sysconfig/network", \%network);
66 if ($_[0]->{'domain'}) { $network{'NISDOMAIN'} = $_[0]->{'domain'}; }
67 else { delete($network{'NISDOMAIN'}); }
68 &write_env_file("/etc/sysconfig/network", \%network);
69 if ($_[0]->{'domain'}) {
70         &init::enable_at_boot("ypbind");
71         }
72 else {
73         &init::disable_at_boot("ypbind");
74         }
75
76 # Apply by setting the domainname and running the init script
77 &system_logged("domainname '$_[0]->{'domain'}' >/dev/null 2>&1");
78 local $init = &init_script("ypbind");
79 &system_logged("$init stop >/dev/null 2>&1");
80 if ($_[0]->{'domain'}) {
81         local $out = &backquote_logged("$init start 2>&1");
82         if ($?) { return "<pre>$out</pre>"; }
83         }
84 return undef;
85 }
86
87 @nis_files = ( "passwd", "shadow", "group", "gshadow", "adjunct",
88                "aliases", "ethers", "bootparams", "hosts", "networks",
89                "printcap", "protocols", "publickeys", "rpc", "services",
90                "netgroup", "netid", "amd_home", "auto_master", "auto_home",
91                "auto_local" );
92
93 @nis_tables = ( "passwd", "group", "hosts", "rpc", "services", "netid",
94                 "protocols", "netgrp", "mail", "shadow", "publickey","networks",
95                 "ethers", "bootparams", "printcap", "amd.home", "auto.master",
96                 "auto.home", "auto.local", "passwd.adjunct" );
97
98 # show_server_config()
99 # Display a form for editing NIS server options
100 sub show_server_config
101 {
102 local ($var, $rule) = &parse_yp_makefile();
103
104 local $boot = &init::action_status("ypserv");
105 print "<tr> <td><b>$text{'server_boot'}</b></td>\n";
106 printf "<td><input type=radio name=boot value=1 %s> %s\n",
107         $boot == 2 ? 'checked' : '', $text{'yes'};
108 printf "<input type=radio name=boot value=0 %s> %s</td>\n",
109         $boot == 2 ? '' : 'checked', $text{'no'};
110
111 local $dom = $var->{'LOCALDOMAIN'}->{'value'};
112 print "<td><b>$text{'server_domain'}</b></td>\n";
113 printf "<td><input type=radio name=domain_auto value=1 %s> %s\n",
114         $dom =~ /`.*domainname`/ ? 'checked' : '', $text{'server_domain_auto'};
115 printf "<input type=radio name=domain_auto value=0 %s>\n",
116         $dom =~ /`.*domainname`/ ? '' : 'checked';
117 printf "<input name=domain size=20 value='%s'></td> </tr>\n",
118         $dom =~ /`.*domainname`/ ? '' : $dom;
119
120 print "<tr> <td><b>$text{'server_type'}</b></td>\n";
121 printf "<td colspan=3><input type=radio name=type value=1 %s> %s\n",
122         $config{'slave'} ? '' : 'checked', $text{'server_master'};
123 printf "<input type=radio name=type value=0 %s> %s\n",
124         $config{'slave'} ? 'checked' : '', $text{'server_slave'};
125 printf "<input name=slave size=30 value='%s'></td> </tr>\n", $config{'slave'};
126
127 print "</table></td></tr></table><p>\n";
128 print "<table border width=100%>\n";
129 print "<tr $tb> <td><b>$text{'server_mheader'}</b></td> </tr>\n";
130 print "<tr $cb> <td><table width=100%>\n";
131
132 print "<tr> <td><b>$text{'server_dns'}</b></td>\n";
133 printf "<td><input type=radio name=b value='-b' %s> %s\n",
134         $var->{'B'}->{'value'} eq '-b' ? 'checked' : '', $text{'yes'};
135 printf "<input type=radio name=b value='' %s> %s</td>\n",
136         $var->{'B'}->{'value'} eq '-b' ? '' : 'checked', $text{'no'};
137
138 print "<td><b>$text{'server_push'}</b></td>\n";
139 printf "<td><input type=radio name=nopush value=false %s> %s\n",
140         $var->{'NOPUSH'}->{'value'} eq 'true' ? '' : 'checked', $text{'yes'};
141 printf "<input type=radio name=nopush value=true %s> %s</td> </tr>\n",
142         $var->{'NOPUSH'}->{'value'} eq 'true' ? 'checked' : '', $text{'no'};
143
144 local %inall;
145 map { $inall{$_}++ } split(/\s+/, $rule->{'all'}->{'value'});
146 print "<tr> <td rowspan=2 valign=top><b>$text{'server_tables'}</b></td>\n";
147 print "<td rowspan=2><select multiple size=5 name=tables>\n";
148 foreach $t (grep { $rule->{$_} } @nis_tables) {
149         printf "<option value=%s %s>%s\n",
150                 $t, $inall{$t} ? 'selected' : '', $t;
151         }
152 print "</select></td>\n";
153
154 print "<td><b>$text{'server_minuid'}</b></td>\n";
155 printf "<td><input name=minuid size=10 value='%s'></td> </tr>\n",
156         $var->{'MINUID'}->{'value'};
157
158 print "<td><b>$text{'server_mingid'}</b></td>\n";
159 printf "<td><input name=mingid size=10 value='%s'></td> </tr>\n",
160         $var->{'MINGID'}->{'value'};
161
162 print "<tr> <td><b>$text{'server_slaves'}</b></td>\n";
163 open(SLAVES, "/var/yp/ypservers");
164 while(<SLAVES>) {
165         s/\s//g;
166         push(@slaves, $_) if ($_);
167         }
168 close(SLAVES);
169 printf "<td colspan=3><input name=slaves size=60 value='%s'></td> </tr>\n",
170         join(" ", @slaves);
171
172 print "</table></td></tr></table><p>\n";
173 print "<table border width=100%>\n";
174 print "<tr $tb> <td><b>$text{'server_fheader'}</b></td> </tr>\n";
175 print "<tr $cb> <td><table width=100%>\n";
176
177 local $i = 0;
178 foreach $t (@nis_files) {
179         local $f = &expand_vars($var->{uc($t)}->{'value'}, $var);
180         next if (!$f);
181         print "<tr>\n" if ($i%2 == 0);
182         print "<td><b>",&text('server_file', $text{"desc_$t"} ? $text{"desc_$t"}
183                                                               : $t),"</b></td>\n";
184         print "<td><input name=$t size=30 value='$f'></td>\n";
185         print "</tr>\n" if ($i++%2 == 1);
186         }
187 }
188
189 # parse_server_config()
190 # Parse and save the NIS server options
191 sub parse_server_config
192 {
193 local ($var, $rule) = &parse_yp_makefile();
194 $in{'minuid'} =~ /^\d+$/ || &error($text{'server_eminuid'});
195 $in{'mingid'} =~ /^\d+$/ || &error($text{'server_emingid'});
196 $in{'domain_auto'} || $in{'domain'} =~ /^[A-Za-z0-9\.\-]+$/ ||
197         &error($text{'server_edomain'});
198 $in{'type'} || &to_ipaddress($in{'slave'}) ||
199         &to_ip6address($in{'slave'}) || &error($text{'server_eslave'});
200 &update_makefile($var->{'MINUID'}, $in{'minuid'});
201 &update_makefile($var->{'MINGID'}, $in{'mingid'});
202 &update_makefile($var->{'NOPUSH'}, $in{'nopush'});
203 &update_makefile($var->{'B'}, $in{'b'});
204 &update_makefile($var->{'LOCALDOMAIN'}, $in{'domain_auto'} ? "`domainname`"
205                                                            : $in{'domain'});
206 &update_makefile($rule->{'all'}, join(" ", split(/\0/, $in{'tables'})), "");
207
208 foreach $t (@nis_files) {
209         local $old = &expand_vars($var->{uc($t)}->{'value'}, $var);
210         next if (!$old);
211         if ($old ne $in{$t}) {
212                 $in{$t} =~ /\S/ || &error(&text('server_efile', $text{"desc_$t"}));
213                 &update_makefile($var->{uc($t)}, $in{$t});
214                 }
215         }
216
217 &open_tempfile(SLAVES, ">/var/yp/ypservers");
218 foreach $s (split(/\s+/, $in{'slaves'})) {
219         &print_tempfile(SLAVES, "$s\n");
220         }
221 &close_tempfile(SLAVES);
222
223 if ($in{'boot'}) {
224         &init::enable_at_boot("ypserv");
225         &init::enable_at_boot("yppasswdd");
226         }
227 else {
228         &init::disable_at_boot("ypserv");
229         &init::disable_at_boot("yppasswdd");
230         }
231 local $init1 = &init_script("ypserv");
232 local $init2 = &init_script("yppasswdd");
233 &system_logged("$init1 stop >/dev/null 2>&1");
234 &system_logged("$init2 stop >/dev/null 2>&1");
235 if ($in{'boot'}) {
236         &system_logged("$init1 start >/dev/null 2>&1");
237         &system_logged("$init2 start >/dev/null 2>&1");
238         }
239 &flush_file_lines();
240 if ($in{'type'}) {
241         # Master server
242         delete($config{'slave'});
243         &apply_table_changes() if ($in{'boot'});
244         }
245 else {
246         $out = &backquote_logged("/usr/lib/yp/ypinit -s $in{'slave'} 2>&1");
247         if ($?) { &error("<tt>$out</tt>"); }
248         $config{'slave'} = $in{'slave'};
249         }
250 &write_file("$module_config_directory/config", \%config);
251 }
252
253 # get_server_mode()
254 # Returns 0 if the NIS server is inactive, 1 if active as a master, or 2 if
255 # active as a slave.
256 sub get_server_mode
257 {
258 if (&init::action_status("ypserv") != 2) {
259         return 0;
260         }
261 elsif ($config{'slave'}) {
262         return 2;
263         }
264 else {
265         return 1;
266         }
267 }
268
269 # list_nis_tables()
270 # Returns a list of structures of all NIS tables
271 sub list_nis_tables
272 {
273 local ($var, $rule) = &parse_yp_makefile();
274 local @rv;
275 local $dom = $var->{'LOCALDOMAIN'}->{'value'};
276 chop($dom = `domainname`) if ($dom =~ /`.*domainname`/);
277 local %file;
278 map { $file{uc($_)} = &expand_vars($var->{uc($_)}->{'value'}, $var) } @nis_files;
279 local @all = split(/\s+/, $rule->{'all'}->{'value'});
280 foreach $t (@all) {
281         local $table = { 'table' => $t,
282                          'index' => scalar(@rv),
283                          'domain' => $dom };
284         if ($t eq "passwd") {
285                 if ($var->{'MERGE_PASSWD'}->{'value'} eq 'true') {
286                         $table->{'type'} = 'passwd_shadow';
287                         $table->{'files'} = [ $file{'PASSWD'}, $file{'SHADOW'} ];
288                         }
289                 elsif (&indexof('shadow', @all) >= 0) {
290                         # Show separate shadow and passwd tables as one table
291                         $table->{'type'} = 'passwd_shadow_full';
292                         $table->{'files'} = [ $file{'PASSWD'}, $file{'SHADOW'} ];
293                         @all = grep { $_ ne 'shadow' } @all;
294                         }
295                 else {
296                         $table->{'type'} = 'passwd';
297                         $table->{'files'} = [ $file{'PASSWD'} ];
298                         }
299                 }
300         elsif ($t eq "group") {
301                 if ($var->{'MERGE_GROUP'}->{'value'} eq 'true') {
302                         $table->{'type'} = 'group_shadow';
303                         $table->{'files'} = [ $file{'GROUP'}, $file{'GSHADOW'} ];
304                         }
305                 else {
306                         $table->{'type'} = 'group';
307                         $table->{'files'} = [ $file{'GROUP'} ];
308                         }
309                 }
310         elsif ($t eq "netgrp") {
311                 $table->{'type'} = "netgroup";
312                 $table->{'files'} = [ $file{'NETGROUP'} ];
313                 }
314         elsif ($t eq "mail") {
315                 $table->{'type'} = "aliases";
316                 $table->{'files'} = [ $file{'ALIASES'} ];
317                 }
318         else {
319                 $table->{'type'} = $t;
320                 local $fn = uc($t);
321                 $fn =~ s/\./_/g;
322                 $table->{'files'} = [ $file{$fn} || $fn ];
323                 }
324         push(@rv, $table);
325         }
326 return @rv;
327 }
328
329 # apply_table_changes()
330 # Do whatever is necessary for the table text files to be loaded into
331 # the NIS server
332 sub apply_table_changes
333 {
334 &system_logged("(cd /var/yp ; make) >/dev/null 2>&1 </dev/null");
335 }
336
337 sub extra_config_files
338 {
339 return ( "/var/yp/ypservers" );
340 }
341
342
343 1;
344