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