<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># open-linux-lib.pl
# NIS functions for caldera linux NIS client and server

$nis_config_dir = "/etc/nis";
$ypserv_conf = "/etc/ypserv.conf";
$pid_file = "/var/run/ypserv.pid";

# get_nis_support()
# Returns 0 for no support, 1 for client only, 2 for server and 3 for both
sub get_nis_support
{
local $rv;
$rv += 1 if (&amp;has_command("ypbind"));
$rv += 2 if (-x "/usr/libexec/nis/rpc.ypserv");
return $rv;
}

# get_client_config()
# Returns a hash ref containg details of the client's NIS settings
sub get_client_config
{
local $nis;
open(CONF, $config{'client_conf'});
while(&lt;CONF&gt;) {
	s/\r|\n//g;
	s/#.*$//g;
	if (/^\s*domain\s*(\S+)\s*broadcast/i) {
		$nis-&gt;{'domain'} = $1;
		$nis-&gt;{'broadcast'}++;
		}
	elsif (/^\s*domain\s*(\S+)\s*server\s*(\S+)/i) {
		$nis-&gt;{'domain'} = $1;
		push(@{$nis-&gt;{'servers'}}, $2);
		}
	elsif (/^\s*ypserver\s*(\S+)/) {
		push(@{$nis-&gt;{'servers'}}, $1);
		}
	}
close(CONF);
return $nis;
}

# save_client_config(&amp;config)
# Saves and applies the NIS client configuration in the give hash.
# Returns an error message if any, or undef on success.
sub save_client_config
{
# Save the config file
&amp;open_tempfile(CONF, "&gt;$config{'client_conf'}");
if ($_[0]-&gt;{'domain'}) {
	if ($_[0]-&gt;{'broadcast'}) {
		&amp;print_tempfile(CONF, "domain $_[0]-&gt;{'domain'} broadcast\n");
		}
	else {
		local @s = @{$_[0]-&gt;{'servers'}};
		&amp;print_tempfile(CONF, "domain $_[0]-&gt;{'domain'} server ",shift(@s),"\n");
		foreach $s (@s) {
			&amp;print_tempfile(CONF, "ypserver $s\n");
			}
		}
	}
&amp;close_tempfile(CONF);
if ($_[0]-&gt;{'domain'}) {
	&amp;init::enable_at_boot("nis-client");
	}
else {
	&amp;init::disable_at_boot("nis-client");
	}

# Apply by running the init script
local $init = &amp;init_script("nis-client");
&amp;system_logged("$init stop &gt;/dev/null 2&gt;&amp;1");
if ($_[0]-&gt;{'domain'}) {
	&amp;system_logged("domainname \"$_[0]-&gt;{'domain'}\"");
	local $out = &amp;backquote_logged("$init start 2&gt;&amp;1");
	if ($?) { return "&lt;pre&gt;$out&lt;/pre&gt;"; }
	$out = `ypwhich 2&gt;&amp;1`;
	if ($?) { return $text{'client_eypwhich'}; }
	}
else {
	&amp;system_logged("domainname '' &gt;/dev/null 2&gt;&amp;1");
	}
return undef;
}

# show_server_config()
# Display a form for editing NIS server options
sub show_server_config
{
local @domains;
opendir(DIR, $nis_config_dir);
foreach $f (readdir(DIR)) {
	push(@domains, $f) if ($f !~ /^\./ &amp;&amp;
			       -r "$nis_config_dir/$f/.nisupdate.conf");
	}
closedir(DIR);
@domains = ( "" ) if (!@domains);

local $boot = &amp;init::action_status("nis-server");
print "&lt;tr&gt; &lt;td valign=top&gt;&lt;b&gt;$text{'server_boot'}&lt;/b&gt;&lt;/td&gt;\n";
printf "&lt;td valign=top&gt;&lt;input type=radio name=boot value=1 %s&gt; %s\n",
	$boot == 2 ? 'checked' : '', $text{'yes'};
printf "&lt;input type=radio name=boot value=0 %s&gt; %s&lt;/td&gt; &lt;/tr&gt;\n",
	$boot == 2 ? '' : 'checked', $text{'no'};

local $n = 0;
foreach $d (@domains) {
	print "&lt;tr&gt; &lt;td colspan=4&gt;&lt;hr&gt;&lt;/td&gt; &lt;/tr&gt;\n";
	print "&lt;input type=hidden name=old_$n value='$d'&gt;\n";
	print "&lt;tr&gt; &lt;td valign=top&gt;&lt;b&gt;$text{'server_domain'}&lt;/b&gt;&lt;/td&gt;\n";
	printf "&lt;td valign=top&gt;".
	       "&lt;input type=radio name=domain_def_$n value=1 %s&gt; %s\n",
		$d ? '' : 'checked', $text{'server_none'};
	printf "&lt;input type=radio name=domain_def_$n value=0 %s&gt;\n",
		$d ? 'checked' : '';
	print "&lt;input name=domain_$n size=30 value='$d'&gt;&lt;/td&gt;\n";

	local @conf = &amp;parse_nisupdate_conf(
		$d ? "$nis_config_dir/$d/.nisupdate.conf"
		   : "nisupdate.conf");
	print "&lt;td valign=top&gt;&lt;b&gt;$text{'server_tables'}&lt;/b&gt;&lt;/td&gt;\n";
	print "&lt;td&gt;&lt;select name=tables_$n size=6 multiple&gt;\n";
	foreach $c (@conf) {
		printf "&lt;option value=%s %s&gt;%s&lt;/option&gt;\n",
			$c-&gt;{'table'}, $c-&gt;{'active'} ? 'selected' : '',
			$c-&gt;{'table'};
		}
	print "&lt;/select&gt;&lt;/td&gt; &lt;/tr&gt;\n";

	$n++;
	}

}

# parse_server_config()
# Parse and save the NIS server options
sub parse_server_config
{
local ($n, $anydomains);
for($n=0; defined($in{"old_$n"}); $n++) {
	# Update the domain name directory
	$in{"domain_def_$n"} || $in{"domain_$n"} =~ /^[A-Za-z0-9\.\-]+$/ ||
		&amp;error(&amp;text('server_edomain', $in{"domain_$n"}));
	local $domain = $in{"domain_def_$n"} ? undef : $in{"domain_$n"};
	local $old = $in{"old_$n"};
	if (!$old &amp;&amp; !$domain) {
		# No domain before, and none chosen
		next;
		}
	elsif (!$old &amp;&amp; $domain) {
		# New domain added
		mkdir("$nis_config_dir/$domain", 0755);
		&amp;system_logged("cp nisupdate.conf ".
			       "$nis_config_dir/$domain/.nisupdate.conf");
		}
	elsif ($old &amp;&amp; !$domain) {
		# Domain taken away
		&amp;system_logged("rm -rf $nis_config_dir/$old");
		next;
		}
	elsif ($old ne $domain) {
		# Domain renamed
		&amp;rename_logged("$nis_config_dir/$old",
			       "$nis_config_dir/$domain");
		}
	$anydomains++;

	# Update the config file
	local $file = "$nis_config_dir/$domain/.nisupdate.conf";
	local @conf = &amp;parse_nisupdate_conf($file);
	local $lref = &amp;read_file_lines($file);
	local %table;
	map { $table{$_}++ } split(/\0/, $in{"tables_$n"});
	foreach $c (@conf) {
		if ($c-&gt;{'active'} &amp;&amp; !$table{$c-&gt;{'table'}}) {
			# Need to deactivate a table
			splice(@$lref, $c-&gt;{'line'},
			       $c-&gt;{'eline'} - $c-&gt;{'line'} + 1,
			       map { "#$_" } @{$c-&gt;{'data'}});
			}
		elsif (!$c-&gt;{'active'} &amp;&amp; $table{$c-&gt;{'table'}}) {
			# Need to activate a table
			splice(@$lref, $c-&gt;{'line'},
			       $c-&gt;{'eline'} - $c-&gt;{'line'} + 1,
			       @{$c-&gt;{'data'}});
			}
		}
	&amp;flush_file_lines();
	}

# Start the NIS server and rebuild maps if needed
if ($in{'boot'}) {
	&amp;init::enable_at_boot("nis-server");
	}
else {
	&amp;init::disable_at_boot("nis-server");
	}
local $init = &amp;init_script("nis-server");
&amp;system_logged("$init stop &gt;/dev/null 2&gt;&amp;1");
if ($anydomains &amp;&amp; $in{'boot'}) {
	&amp;system_logged("$init start &gt;/dev/null 2&gt;&amp;1");
	}
&amp;apply_table_changes();
}

# get_server_mode()
# Returns 0 if the NIS server is inactive, 1 if active as a master, or 2 if
# active as a slave.
sub get_server_mode
{
local $boot = &amp;init::action_status("nis-server");
local $dc;
opendir(DIR, $nis_config_dir);
foreach $f (readdir(DIR)) {
	$dc++ if ($f !~ /^\./ &amp;&amp; -r "$nis_config_dir/$f/.nisupdate.conf");
	}
closedir(DIR);
if ($boot != 2 || !$dc) {
	return 0;
	}
else {
	return 1;
	}
}

# parse_nisupdate_conf(file)
sub parse_nisupdate_conf
{
local @rv;
local $lnum = 0;
open(CONF, $_[0]);
while(&lt;CONF&gt;) {
	s/\r|\n//g;
	if (/^\s*(#*)(\s*\$rule{['"]([^"']+)['"]}.*)/) {
		local $text = $2;
		local $table = { 'table' =&gt; $3,
				 'active' =&gt; $1 eq '',
				 'data' =&gt; [ $2 ],
				 'line' =&gt; $lnum,
			 	 'eline' =&gt; $lnum };
		while(!/;\s*$/) {
			($_ = &lt;CONF&gt;) || last;
			s/^\s*#+//; s/\r|\n//g;
			push(@{$table-&gt;{'data'}}, $_);
			$text .= " $_";
			$lnum++;
			$table-&gt;{'eline'} = $lnum;
			}
		$table-&gt;{'value'} = $2 if ($text =~ /\$rule{['"]([^"']+)['"]}\s*=\s*["']([^"']+)["']/);
		push(@rv, $table);
		}
	$lnum++;
	}
close(CONF);
return @rv;
}

# list_nis_tables()
# Returns a list of structures of all NIS tables
sub list_nis_tables
{
local @rv;
opendir(DIR, $nis_config_dir);
foreach $d (readdir(DIR)) {
	push(@domains, $d) if ($d !~ /^\./ &amp;&amp;
			       -r "$nis_config_dir/$d/.nisupdate.conf");
	}
closedir(DIR);
foreach $d (@domains) {
	local @conf = &amp;parse_nisupdate_conf(
			"$nis_config_dir/$d/.nisupdate.conf");
	foreach $t (@conf) {
		next if (!$t-&gt;{'active'});
		local $table = { 'table' =&gt; $t-&gt;{'table'},
				 'domain' =&gt; $d,
				 'index' =&gt; scalar(@rv) };
		if ($t-&gt;{'value'} =~ /^(\S+)\s+(\S+)/) {
			$table-&gt;{'files'} = [ map { "$nis_config_dir/$d/$_" }
					 	  split(/,/, $2) ];
			}
		if ($t-&gt;{'table'} eq 'passwd') {
			$table-&gt;{'type'} = 'passwd_shadow';
			}
		elsif ($t-&gt;{'table'} eq 'services') {
			$table-&gt;{'type'} = 'services2';
			}
		else {
			$table-&gt;{'type'} = $t-&gt;{'table'};
			}
		push(@rv, $table);
		}
	}
return @rv;
}

# apply_table_changes()
# Do whatever is necessary for the table text files to be loaded into
# the NIS server
sub apply_table_changes
{
&amp;system_logged("(cd /var/yp ; make) &gt;/dev/null 2&gt;&amp;1 &lt;/dev/null");
}

sub extra_config_files
{
local ($f, @rv);
opendir(DIR, $nis_config_dir);
foreach $f (readdir(DIR)) {
        push(@rv, "$nis_config_dir/$f/.nisupdate.conf") if ($f !~ /^\./);
        }
closedir(DIR);
push(@rv, "$nis_config_dir/nisupdate.conf");
return grep { -r $_ } @rv;
}

1;

</pre></body></html>