2 # NIS functions for aix, based on the solaris library
4 $binding_dir = "/var/yp/binding";
5 $yp_makefile = "/var/yp/Makefile";
8 # Returns 0 for no support, 1 for client only, 2 for server and 3 for both
14 $result=`lssrc -s ypbind |grep -q active && echo ok`;
16 $rv += 1 if ($result == "ok");
18 $result=`lssrc -s ypserv |grep -q active && echo ok`;
19 $rv += 2 if ($result == "ok");
24 # Returns a hash ref containg details of the client's NIS settings
28 open(DOM, "/usr/bin/domainname |") ||die("could not run /usr/bin/domainname : $!");
29 chomp($nis->{'domain'} = <DOM>);
31 if ($nis->{'domain'}) {
32 if (open(SRV, "$binding_dir/$nis->{'domain'}/ypservers")) {
35 push(@{$nis->{'servers'}}, $_);
40 $nis->{'broadcast'} = 1;
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 if ($_[0]->{'domain'}) {
52 # Check if the servers are in /etc/hosts
53 local @s = @{$_[0]->{'servers'}};
54 &foreign_require("net", "net-lib.pl");
57 foreach $h (&foreign_call("net", "list_hosts")) {
58 $found++ if (&indexof($s, @{$h->{'hosts'}}) >= 0);
60 return &text("client_ehosts", $s) if (!$found);
64 &logged_system("/usr/bin/domainname \"$_[0]->{'domain'}\"");
65 mkdir("$binding_dir/$_[0]->{'domain'}", 0755);
67 &open_tempfile(SRV, ">$binding_dir/$_[0]->{'domain'}/ypservers");
69 &print_tempfile(SRV, "$s\n");
74 unlink("$binding_dir/$_[0]->{'domain'}/ypservers");
78 &logged_system("/usr/bin/domainname ''");
81 # Apply by running ypstop and ypstart
82 &system_logged("(stopsrc -s ypserv ; /usr/bin/domainname \"$_[0]->{'domain'}\" ; startsrc -s ypserv ) > /dev/null 2>&1 ");
84 if ($_[0]->{'domain'}) {
85 local $out = `ypwhich 2>&1`;
86 if ($? || $out =~ /not\s+bound/i || $out =~ /can't\s+communicate/i) {
87 system("(stopsrc -s ypserv) >/dev/null 2>&1");
88 return $text{'client_eypwhich'};
93 @nis_tables = ( "passwd", "group", "hosts", "ethers", "networks", "rpc",
94 "services", "protocols", "netgroup", "bootparams", "aliases",
95 "publickey", "netid", "netmasks", "c2secure", "timezone",
96 "auto.master", "auto.home" );
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();
103 local $dom = `domainname`; chop($dom);
105 print "<tr> <td><b>$text{'server_boot'}</b></td> <td>\n";
106 if ($dom && -d "/var/yp/$dom") {
107 print "<i>$text{'server_already'}</i>\n";
110 print "<input type=radio name=boot value=1 > $text{'yes'}\n";
111 print "<input type=radio name=boot value=0 checked > $text{'no'}\n";
115 print "<td><b>$text{'server_domain'}</b></td>\n";
116 printf "<td><input type=radio name=domain_def value=1 %s> %s\n",
117 $dom ? '' : 'checked', $text{'server_none'};
118 printf "<input type=radio name=domain_def value=0 %s>\n",
119 $dom ? 'checked' : '';
120 printf "<input name=domain size=35 value='%s'></td> </tr>\n", $dom;
122 print "<tr> <td><b>$text{'server_type'}</b></td>\n";
123 printf "<td colspan=3><input type=radio name=type value=1 %s> %s\n",
124 $config{'slave'} ? '' : 'checked', $text{'server_master'};
125 printf "<input type=radio name=type value=0 %s> %s\n",
126 $config{'slave'} ? 'checked' : '', $text{'server_slave'};
127 printf "<input name=slave size=30 value='%s'></td> </tr>\n", $config{'slave'};
129 print "<tr> <td colspan=4><i>$text{'server_aix'}</i></td> </tr>\n";
131 print "</table></td></tr></table><p>\n";
132 print "<table border width=100%>\n";
133 print "<tr $tb> <td><b>$text{'server_mheader'}</b></td> </tr>\n";
134 print "<tr $cb> <td><table width=100%>\n";
137 local @all = split(/\s+/, $rule->{'all'}->{'value'});
138 map { $inall{$_}++ } @all;
139 print "<tr> <td rowspan=4 valign=top><b>$text{'server_tables'}</b></td>\n";
140 print "<td rowspan=4><select multiple size=6 name=tables>\n";
141 foreach $t (&unique(@nis_tables, @all)) {
142 printf "<option value=%s %s>%s\n",
143 $t, $inall{$t} ? 'selected' : '', $t;
145 print "</select></td>\n";
147 print "<td><b>$text{'server_dns'}</b></td>\n";
148 printf "<td><input type=radio name=b value='-b' %s> %s\n",
149 $var->{'B'}->{'value'} eq '-b' ? 'checked' : '', $text{'yes'};
150 printf "<input type=radio name=b value='' %s> %s</td> </tr>\n",
151 $var->{'B'}->{'value'} eq '-b' ? '' : 'checked', $text{'no'};
153 print "<tr> <td><b>$text{'server_push'}</b></td>\n";
154 printf "<td><input type=radio name=nopush value='\"\"' %s> %s\n",
155 $var->{'NOPUSH'}->{'value'} eq 'true' ? '' : 'checked', $text{'yes'};
156 printf "<input type=radio name=nopush value=true %s> %s</td> </tr>\n",
157 $var->{'NOPUSH'}->{'value'} eq 'true' ? 'checked' : '', $text{'no'};
159 print "<tr> <td><b>$text{'server_dir'}</b></td>\n";
160 printf "<td><input name=dir size=30 value='%s'> %s</td> </tr>\n",
161 $var->{'DIR'}->{'value'}, &file_chooser_button("dir", 0);
163 print "<tr> <td><b>$text{'server_pwdir'}</b></td>\n";
164 printf "<td><input name=pwdir size=30 value='%s'> %s</td> </tr>\n",
165 $var->{'PWDIR'}->{'value'}, &file_chooser_button("pwdir", 0);
168 # parse_server_config()
169 # Parse and save the NIS server options
170 sub parse_server_config
172 local ($var, $rule) = &parse_yp_makefile();
173 $in{'domain_def'} || $in{'domain'} =~ /^[A-Za-z0-9\.\-\_]+$/ ||
174 &error(&text('server_edomain', $in{'domain'}));
175 if ($in{'boot'} && $in{'domain_def'}) {
176 &error($text{'server_ebootdom'});
178 $in{'type'} || &to_ipaddress($in{'slave'}) ||
179 &to_ip6address($in{'slave'}) || &error($text{'server_eslave'});
180 -d $in{'dir'} || &error($text{'server_edir'});
181 -d $in{'pwdir'} || &error($text{'server_epwdir'});
182 &update_makefile($var->{'NOPUSH'}, $in{'nopush'});
183 &update_makefile($var->{'B'}, $in{'b'});
184 &update_makefile($rule->{'all'}, join(" ", split(/\0/, $in{'tables'})), "");
185 &update_makefile($var->{'DIR'}, $in{'dir'});
186 &update_makefile($var->{'PWDIR'}, $in{'pwdir'});
189 if ($in{'domain_def'}) {
190 &system_logged("domainname \"\" >/dev/null 2>&1");
193 local $old = `domainname`; chop($old);
194 &system_logged("chypdom -B \"$in{'domain'}\"");
195 &system_logged("domainname \"$in{'domain'}\" >/dev/null 2>&1");
197 # Create the domain directory
198 mkdir("/var/yp/$in{'domain'}", 0755);
199 &system_logged("rm -f /var/yp/*.time"); # force a remake
205 delete($config{'slave'});
206 &apply_table_changes()
207 if (!$in{'domain_def'} && -d "/var/yp/$in{'domain'}");
210 local $temp = &transname();
211 open(TEMP, ">$temp");
214 $out = &backquote_logged("/usr/sbin/ypinit -s $in{'slave'} <$temp 2>&1");
216 if ($?) { &error("<tt>$out</tt>"); }
217 $config{'slave'} = $in{'slave'};
219 &write_file("$module_config_directory/config", \%config);
220 &system_logged("stopsrc -g yp >/dev/null 2>&1");
221 &system_logged("startsrc -g yp >/dev/null 2>&1");
225 # Returns 0 if the NIS server is inactive, 1 if active as a master, or 2 if
229 local $dom = `domainname`; chop($dom);
230 return !$dom ? 0 : $config{'slave'} ? 2 : 1;
233 # parse_yp_makefile()
234 # Returns hashes of makefile variables and rules
235 sub parse_yp_makefile
237 # First parse joined lines
239 local (@lines, $llast);
240 open(MAKE, $yp_makefile);
243 local $slash = (s/\\$//);
246 $llast->{'value'} .= " $_";
247 $llast->{'eline'} = $lnum;
250 push(@lines, { 'value' => $_,
254 $llast = $slash ? $lines[$#lines] : undef;
259 # Then look for variables and rules
260 local ($i, %var, %rule);
261 for($i=0; $i<@lines; $i++) {
262 if ($lines[$i]->{'value'} =~ /^\s*(\S+)\s*=\s*(.*)/) {
264 $var{$1} = { 'name' => $1,
267 'line' => $lines[$i]->{'line'},
268 'eline' => $lines[$i]->{'eline'} };
270 elsif ($lines[$i]->{'value'} =~ /^\s*(\S+):\s*(.*)/) {
271 # Found a makefile rule
272 $rule{$1} = { 'name' => $1,
275 'code' => $lines[$i+1]->{'value'},
276 'line' => $lines[$i]->{'line'},
277 'eline' => $lines[$i+1]->{'eline'} };
281 return ( \%var, \%rule );
284 # expand_vars(string, &vars)
288 while($rv =~ /^(.*)\$\(([A-Za-z0-9_]+)\)(.*)$/) {
289 $rv = $1.$_[1]->{$2}->{'value'}.$3;
294 # update_makefile(&old, value, [value]);
297 local $lref = &read_file_lines($yp_makefile);
299 if ($_[0]->{'type'} == 0) {
300 @n = ( "$_[0]->{'name'} = $_[1]" );
303 @n = ( "$_[0]->{'name'}: $_[1]", $_[2] );
305 splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1, @n);
308 # apply_table_changes()
309 # Do whatever is necessary for the table text files to be loaded into
311 sub apply_table_changes
313 &system_logged("(cd /var/yp ; /usr/bin/make) >/dev/null 2>&1 </dev/null");
317 # Returns a list of structures of all NIS tables
321 local ($var, $rule) = &parse_yp_makefile();
322 local $dom = `domainname`; chop($dom);
323 local @all = split(/\s+/, $rule->{'all'}->{'value'});
325 local $table = { 'table' => $t,
326 'index' => scalar(@rv),
328 local $rt = $rule->{"$t.time"};
329 local @files = split(/\s+/, $rt->{'value'});
330 @files = map { &expand_vars($_, $var) } @files;
331 $table->{'files'} = \@files;
332 $table->{'type'} = $t eq 'passwd' && @files > 1 ? 'passwd_shadow' :
339 sub show_server_security
343 sub parse_server_security
345 &system_logged("stopsrc -g yp >/dev/null 2>&1");
346 &system_logged("startsrc -g yp >/dev/null 2>&1");