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";
9 # Returns 0 for no support, 1 for client only, 2 for server and 3 for both
13 $rv += 1 if (&has_command("ypbind"));
14 $rv += 2 if (&has_command("ypserv"));
19 # Returns a hash ref containg details of the client's NIS settings
23 open(CONF, $config{'client_conf'});
27 if (/^\s*domain\s*(\S+)\s*broadcast/i) {
28 $nis->{'broadcast'}++;
30 elsif (/^\s*domain\s*(\S+)\s*server\s*(\S+)/i) {
31 push(@{$nis->{'servers'}}, $2);
33 elsif (/^\s*ypserver\s*(\S+)/) {
34 push(@{$nis->{'servers'}}, $1);
39 &read_env_file("/etc/sysconfig/network", \%network);
40 $nis->{'domain'} = $network{'NISDOMAIN'};
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
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");
56 local @s = @{$_[0]->{'servers'}};
57 &print_tempfile(CONF,"domain $_[0]->{'domain'} server $s[0]\n");
59 &print_tempfile(CONF, "ypserver $s\n");
63 &close_tempfile(CONF);
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");
73 &init::disable_at_boot("ypbind");
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>"; }
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",
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" );
98 # show_server_config()
99 # Display a form for editing NIS server options
100 sub show_server_config
102 local ($var, $rule) = &parse_yp_makefile();
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'};
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;
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'};
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";
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'};
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'};
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;
152 print "</select></td>\n";
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'};
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'};
162 print "<tr> <td><b>$text{'server_slaves'}</b></td>\n";
163 open(SLAVES, "/var/yp/ypservers");
166 push(@slaves, $_) if ($_);
169 printf "<td colspan=3><input name=slaves size=60 value='%s'></td> </tr>\n",
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";
178 foreach $t (@nis_files) {
179 local $f = &expand_vars($var->{uc($t)}->{'value'}, $var);
181 print "<tr>\n" if ($i%2 == 0);
182 print "<td><b>",&text('server_file', $text{"desc_$t"} ? $text{"desc_$t"}
184 print "<td><input name=$t size=30 value='$f'></td>\n";
185 print "</tr>\n" if ($i++%2 == 1);
189 # parse_server_config()
190 # Parse and save the NIS server options
191 sub parse_server_config
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`"
206 &update_makefile($rule->{'all'}, join(" ", split(/\0/, $in{'tables'})), "");
208 foreach $t (@nis_files) {
209 local $old = &expand_vars($var->{uc($t)}->{'value'}, $var);
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});
217 &open_tempfile(SLAVES, ">/var/yp/ypservers");
218 foreach $s (split(/\s+/, $in{'slaves'})) {
219 &print_tempfile(SLAVES, "$s\n");
221 &close_tempfile(SLAVES);
224 &init::enable_at_boot("ypserv");
225 &init::enable_at_boot("yppasswdd");
228 &init::disable_at_boot("ypserv");
229 &init::disable_at_boot("yppasswdd");
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");
236 &system_logged("$init1 start >/dev/null 2>&1");
237 &system_logged("$init2 start >/dev/null 2>&1");
242 delete($config{'slave'});
243 &apply_table_changes() if ($in{'boot'});
246 $out = &backquote_logged("/usr/lib/yp/ypinit -s $in{'slave'} 2>&1");
247 if ($?) { &error("<tt>$out</tt>"); }
248 $config{'slave'} = $in{'slave'};
250 &write_file("$module_config_directory/config", \%config);
254 # Returns 0 if the NIS server is inactive, 1 if active as a master, or 2 if
258 if (&init::action_status("ypserv") != 2) {
261 elsif ($config{'slave'}) {
270 # Returns a list of structures of all NIS tables
273 local ($var, $rule) = &parse_yp_makefile();
275 local $dom = $var->{'LOCALDOMAIN'}->{'value'};
276 chop($dom = `domainname`) if ($dom =~ /`.*domainname`/);
278 map { $file{uc($_)} = &expand_vars($var->{uc($_)}->{'value'}, $var) } @nis_files;
279 local @all = split(/\s+/, $rule->{'all'}->{'value'});
281 local $table = { 'table' => $t,
282 'index' => scalar(@rv),
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'} ];
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;
296 $table->{'type'} = 'passwd';
297 $table->{'files'} = [ $file{'PASSWD'} ];
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'} ];
306 $table->{'type'} = 'group';
307 $table->{'files'} = [ $file{'GROUP'} ];
310 elsif ($t eq "netgrp") {
311 $table->{'type'} = "netgroup";
312 $table->{'files'} = [ $file{'NETGROUP'} ];
314 elsif ($t eq "mail") {
315 $table->{'type'} = "aliases";
316 $table->{'files'} = [ $file{'ALIASES'} ];
319 $table->{'type'} = $t;
322 $table->{'files'} = [ $file{$fn} || $fn ];
329 # apply_table_changes()
330 # Do whatever is necessary for the table text files to be loaded into
332 sub apply_table_changes
334 &system_logged("(cd /var/yp ; make) >/dev/null 2>&1 </dev/null");
337 sub extra_config_files
339 return ( "/etc/sysconfig/network", "/var/yp/ypservers" );