2 # NIS functions for redhat linux NIS client and server
4 $yp_makefile = "/var/yp/Makefile";
5 $ypserv_conf = "/etc/ypserv.conf";
6 $pid_file = "/var/run/ypserv.pid";
7 $default_domain = "/etc/defaultdomain";
10 # Returns 0 for no support, 1 for client only, 2 for server and 3 for both
14 $rv += 1 if (&has_command("ypbind"));
15 $rv += 2 if (&has_command("ypserv"));
20 # Returns a hash ref containg details of the client's NIS settings
24 open(CONF, $config{'client_conf'});
28 if (/^\s*domain\s*(\S+)\s*broadcast/i) {
29 $nis->{'broadcast'}++;
31 elsif (/^\s*domain\s*(\S+)\s*server\s*(\S+)/i) {
32 push(@{$nis->{'servers'}}, $2);
34 elsif (/^\s*ypserver\s*(\S+)/) {
35 push(@{$nis->{'servers'}}, $1);
39 if (open(DOMAIN, $default_domain)) {
40 chop($nis->{'domain'} = <DOMAIN>);
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
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");
58 local @s = @{$_[0]->{'servers'}};
59 &print_tempfile(CONF,"domain $_[0]->{'domain'} server $s[0]\n");
61 &print_tempfile(CONF, "ypserver $s\n");
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);
72 unlink($default_domain);
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>"; }
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" );
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" );
94 # show_server_config()
95 # Display a form for editing NIS server options
96 sub show_server_config
98 local ($var, $rule) = &parse_yp_makefile();
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'};
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;
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'};
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";
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'};
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'};
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;
149 print "</select></td>\n";
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'};
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'};
159 print "<tr> <td><b>$text{'server_slaves'}</b></td>\n";
160 open(SLAVES, "/var/yp/ypservers");
163 push(@slaves, $_) if ($_);
166 printf "<td colspan=3><input name=slaves size=60 value='%s'></td> </tr>\n",
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";
175 foreach $t (@nis_files) {
176 local $f = &expand_vars($var->{uc($t)}->{'value'}, $var);
178 print "<tr>\n" if ($i%2 == 0);
179 print "<td><b>",&text('server_file', $text{"desc_$t"} ? $text{"desc_$t"}
181 print "<td><input name=$t size=30 value='$f'></td>\n";
182 print "</tr>\n" if ($i++%2 == 1);
186 # parse_server_config()
187 # Parse and save the NIS server options
188 sub parse_server_config
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`"
203 &update_makefile($rule->{'all'}, join(" ", split(/\0/, $in{'tables'})), "");
205 foreach $t (@nis_files) {
206 local $old = &expand_vars($var->{uc($t)}->{'value'}, $var);
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});
214 &open_tempfile(SLAVES, ">/var/yp/ypservers");
215 foreach $s (split(/\s+/, $in{'slaves'})) {
216 &print_tempfile(SLAVES, "$s\n");
218 &close_tempfile(SLAVES);
220 local @pid = &find_byname("ypserv");
222 # Start ypserv if not running
224 &system_logged("/usr/sbin/ypserv >/dev/null 2>&1");
230 &kill_logged('TERM', @pid);
236 delete($config{'slave'});
237 &apply_table_changes() if ($in{'boot'});
240 $out = &backquote_logged("/usr/lib/yp/ypinit -s $in{'slave'} 2>&1");
241 if ($?) { &error("<tt>$out</tt>"); }
242 $config{'slave'} = $in{'slave'};
244 &write_file("$module_config_directory/config", \%config);
248 # Returns 0 if the NIS server is inactive, 1 if active as a master, or 2 if
252 local @pid = &find_byname("ypserv");
256 elsif ($config{'slave'}) {
265 # Returns a list of structures of all NIS tables
268 local ($var, $rule) = &parse_yp_makefile();
270 local $dom = $var->{'LOCALDOMAIN'}->{'value'};
271 chop($dom = `domainname`) if ($dom =~ /`.*domainname`/);
273 map { $file{uc($_)} = &expand_vars($var->{uc($_)}->{'value'}, $var) } @nis_files;
274 local @all = split(/\s+/, $rule->{'all'}->{'value'});
276 local $table = { 'table' => $t,
277 'index' => scalar(@rv),
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'} ];
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;
291 $table->{'type'} = 'passwd';
292 $table->{'files'} = [ $file{'PASSWD'} ];
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'} ];
301 $table->{'type'} = 'group';
302 $table->{'files'} = [ $file{'GROUP'} ];
305 elsif ($t eq "netgrp") {
306 $table->{'type'} = "netgroup";
307 $table->{'files'} = [ $file{'NETGROUP'} ];
309 elsif ($t eq "mail") {
310 $table->{'type'} = "aliases";
311 $table->{'files'} = [ $file{'ALIASES'} ];
314 $table->{'type'} = $t;
315 $table->{'files'} = [ $file{uc($t)} ];
322 # apply_table_changes()
323 # Do whatever is necessary for the table text files to be loaded into
325 sub apply_table_changes
327 &system_logged("(cd /var/yp ; make) >/dev/null 2>&1 </dev/null");
330 sub extra_config_files
332 return ( "/etc/defaultdomain", "/var/yp/ypservers" );