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

$yp_makefile = "/var/yp/Makefile";
$ypserv_conf = "/etc/ypserv.conf";
$pid_file = "/var/run/ypserv.pid";
$default_domain = "/etc/defaultdomain";

# 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 (&amp;has_command("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;{'broadcast'}++;
		}
	elsif (/^\s*domain\s*(\S+)\s*server\s*(\S+)/i) {
		push(@{$nis-&gt;{'servers'}}, $2);
		}
	elsif (/^\s*ypserver\s*(\S+)/) {
		push(@{$nis-&gt;{'servers'}}, $1);
		}
	}
close(CONF);
if (open(DOMAIN, $default_domain)) {
	chop($nis-&gt;{'domain'} = &lt;DOMAIN&gt;);
	close(DOMAIN);
	}
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 $s[0]\n");
		foreach $s (@s) {
			&amp;print_tempfile(CONF, "ypserver $s\n");
			}
		}
	}
&amp;close_tempfile(CONF);
if ($_[0]-&gt;{'domain'}) {
	&amp;open_tempfile(DOMAIN, "&gt;$default_domain");
	&amp;print_tempfile(DOMAIN, $_[0]-&gt;{'domain'},"\n");
	&amp;close_tempfile(DOMAIN);
	}
else {
	unlink($default_domain);
	}

# Apply by setting the domainname and running the init script
&amp;system_logged("nisdomainname '$_[0]-&gt;{'domain'}' &gt;/dev/null 2&gt;&amp;1");
if ($_[0]-&gt;{'domain'}) {
	local $out = &amp;backquote_logged("ypbind 2&gt;&amp;1");
	if ($?) { return "&lt;pre&gt;$out&lt;/pre&gt;"; }
	}
return undef;
}

@nis_files = ( "passwd", "shadow", "group", "gshadow", "adjunct",
	       "aliases", "ethers", "bootparams", "hosts", "networks",
	       "printcap", "protocols", "publickeys", "rpc", "services",
	       "netgroup", "netid", "auto_master", "auto_home", "auto_local" );

@nis_tables = ( "passwd", "group", "hosts", "rpc", "services", "netid",
		"protocols", "netgrp", "mail", "shadow", "publickey", "networks",
		"ethers", "bootparams", "printcap", "amd.home", "auto.master",
		"auto.home", "auto.local", "passwd.adjunct" );

# show_server_config()
# Display a form for editing NIS server options
sub show_server_config
{
local ($var, $rule) = &amp;parse_yp_makefile();

local @pid = &amp;find_byname("ypserv");
local $boot = @pid &gt; 0;
print "&lt;tr&gt; &lt;td&gt;&lt;b&gt;$text{'server_boot'}&lt;/b&gt;&lt;/td&gt;\n";
printf "&lt;td&gt;&lt;input type=radio name=boot value=1 %s&gt; %s\n",
	$boot ? 'checked' : '', $text{'yes'};
printf "&lt;input type=radio name=boot value=0 %s&gt; %s&lt;/td&gt;\n",
	$boot ? '' : 'checked', $text{'no'};

local $dom = $var-&gt;{'LOCALDOMAIN'}-&gt;{'value'};
print "&lt;td&gt;&lt;b&gt;$text{'server_domain'}&lt;/b&gt;&lt;/td&gt;\n";
printf "&lt;td&gt;&lt;input type=radio name=domain_auto value=1 %s&gt; %s\n",
	$dom =~ /`.*domainname`/ ? 'checked' : '', $text{'server_domain_auto'};
printf "&lt;input type=radio name=domain_auto value=0 %s&gt;\n",
	$dom =~ /`.*domainname`/ ? '' : 'checked';
printf "&lt;input name=domain size=20 value='%s'&gt;&lt;/td&gt; &lt;/tr&gt;\n",
	$dom =~ /`.*domainname`/ ? '' : $dom;

print "&lt;tr&gt; &lt;td&gt;&lt;b&gt;$text{'server_type'}&lt;/b&gt;&lt;/td&gt;\n";
printf "&lt;td colspan=3&gt;&lt;input type=radio name=type value=1 %s&gt; %s\n",
	$config{'slave'} ? '' : 'checked', $text{'server_master'};
printf "&lt;input type=radio name=type value=0 %s&gt; %s\n",
	$config{'slave'} ? 'checked' : '', $text{'server_slave'};
printf "&lt;input name=slave size=30 value='%s'&gt;&lt;/td&gt; &lt;/tr&gt;\n", $config{'slave'};

print "&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;\n";
print "&lt;table border width=100%&gt;\n";
print "&lt;tr $tb&gt; &lt;td&gt;&lt;b&gt;$text{'server_mheader'}&lt;/b&gt;&lt;/td&gt; &lt;/tr&gt;\n";
print "&lt;tr $cb&gt; &lt;td&gt;&lt;table width=100%&gt;\n";

print "&lt;tr&gt; &lt;td&gt;&lt;b&gt;$text{'server_dns'}&lt;/b&gt;&lt;/td&gt;\n";
printf "&lt;td&gt;&lt;input type=radio name=b value='-b' %s&gt; %s\n",
	$var-&gt;{'B'}-&gt;{'value'} eq '-b' ? 'checked' : '', $text{'yes'};
printf "&lt;input type=radio name=b value='' %s&gt; %s&lt;/td&gt;\n",
	$var-&gt;{'B'}-&gt;{'value'} eq '-b' ? '' : 'checked', $text{'no'};

print "&lt;td&gt;&lt;b&gt;$text{'server_push'}&lt;/b&gt;&lt;/td&gt;\n";
printf "&lt;td&gt;&lt;input type=radio name=nopush value=false %s&gt; %s\n",
	$var-&gt;{'NOPUSH'}-&gt;{'value'} eq 'true' ? '' : 'checked', $text{'yes'};
printf "&lt;input type=radio name=nopush value=true %s&gt; %s&lt;/td&gt; &lt;/tr&gt;\n",
	$var-&gt;{'NOPUSH'}-&gt;{'value'} eq 'true' ? 'checked' : '', $text{'no'};

local %inall;
map { $inall{$_}++ } split(/\s+/, $rule-&gt;{'all'}-&gt;{'value'});
print "&lt;tr&gt; &lt;td rowspan=2 valign=top&gt;&lt;b&gt;$text{'server_tables'}&lt;/b&gt;&lt;/td&gt;\n";
print "&lt;td rowspan=2&gt;&lt;select multiple size=5 name=tables&gt;\n";
foreach $t (grep { $rule-&gt;{$_} } @nis_tables) {
	printf "&lt;option value=%s %s&gt;%s\n",
		$t, $inall{$t} ? 'selected' : '', $t;
	}
print "&lt;/select&gt;&lt;/td&gt;\n";

print "&lt;td&gt;&lt;b&gt;$text{'server_minuid'}&lt;/b&gt;&lt;/td&gt;\n";
printf "&lt;td&gt;&lt;input name=minuid size=10 value='%s'&gt;&lt;/td&gt; &lt;/tr&gt;\n",
	$var-&gt;{'MINUID'}-&gt;{'value'};

print "&lt;td&gt;&lt;b&gt;$text{'server_mingid'}&lt;/b&gt;&lt;/td&gt;\n";
printf "&lt;td&gt;&lt;input name=mingid size=10 value='%s'&gt;&lt;/td&gt; &lt;/tr&gt;\n",
	$var-&gt;{'MINGID'}-&gt;{'value'};

print "&lt;tr&gt; &lt;td&gt;&lt;b&gt;$text{'server_slaves'}&lt;/b&gt;&lt;/td&gt;\n";
open(SLAVES, "/var/yp/ypservers");
while(&lt;SLAVES&gt;) {
	s/\s//g;
	push(@slaves, $_) if ($_);
	}
close(SLAVES);
printf "&lt;td colspan=3&gt;&lt;input name=slaves size=60 value='%s'&gt;&lt;/td&gt; &lt;/tr&gt;\n",
	join(" ", @slaves);

print "&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;\n";
print "&lt;table border width=100%&gt;\n";
print "&lt;tr $tb&gt; &lt;td&gt;&lt;b&gt;$text{'server_fheader'}&lt;/b&gt;&lt;/td&gt; &lt;/tr&gt;\n";
print "&lt;tr $cb&gt; &lt;td&gt;&lt;table width=100%&gt;\n";

local $i = 0;
foreach $t (@nis_files) {
	local $f = &amp;expand_vars($var-&gt;{uc($t)}-&gt;{'value'}, $var);
	next if (!$f);
	print "&lt;tr&gt;\n" if ($i%2 == 0);
	print "&lt;td&gt;&lt;b&gt;",&amp;text('server_file', $text{"desc_$t"} ? $text{"desc_$t"}
							      : $t),"&lt;/b&gt;&lt;/td&gt;\n";
	print "&lt;td&gt;&lt;input name=$t size=30 value='$f'&gt;&lt;/td&gt;\n";
	print "&lt;/tr&gt;\n" if ($i++%2 == 1);
	}
}

# parse_server_config()
# Parse and save the NIS server options
sub parse_server_config
{
local ($var, $rule) = &amp;parse_yp_makefile();
$in{'minuid'} =~ /^\d+$/ || &amp;error($text{'server_eminuid'});
$in{'mingid'} =~ /^\d+$/ || &amp;error($text{'server_emingid'});
$in{'domain_auto'} || $in{'domain'} =~ /^[A-Za-z0-9\.\-]+$/ ||
	&amp;error($text{'server_edomain'});
$in{'type'} || &amp;to_ipaddress($in{'slave'}) ||
	&amp;to_ip6address($in{'slave'}) || &amp;error($text{'server_eslave'});
&amp;update_makefile($var-&gt;{'MINUID'}, $in{'minuid'});
&amp;update_makefile($var-&gt;{'MINGID'}, $in{'mingid'});
&amp;update_makefile($var-&gt;{'NOPUSH'}, $in{'nopush'});
&amp;update_makefile($var-&gt;{'B'}, $in{'b'});
&amp;update_makefile($var-&gt;{'LOCALDOMAIN'}, $in{'domain_auto'} ? "`domainname`"
							   : $in{'domain'});
&amp;update_makefile($rule-&gt;{'all'}, join(" ", split(/\0/, $in{'tables'})), "");

foreach $t (@nis_files) {
	local $old = &amp;expand_vars($var-&gt;{uc($t)}-&gt;{'value'}, $var);
	next if (!$old);
	if ($old ne $in{$t}) {
		$in{$t} =~ /\S/ || &amp;error(&amp;text('server_efile', $text{"desc_$t"}));
		&amp;update_makefile($var-&gt;{uc($t)}, $in{$t});
		}
	}

&amp;open_tempfile(SLAVES, "&gt;/var/yp/ypservers");
foreach $s (split(/\s+/, $in{'slaves'})) {
	&amp;print_tempfile(SLAVES, "$s\n");
	}
&amp;close_tempfile(SLAVES);

local @pid = &amp;find_byname("ypserv");
if ($in{'boot'}) {
	# Start ypserv if not running
	if (@pid == 0) {
		&amp;system_logged("/usr/sbin/ypserv &gt;/dev/null 2&gt;&amp;1");
		}
	}
else {
	# Stop if running
	if (@pid &gt; 0) {
		&amp;kill_logged('TERM', @pid);
		}
	}
&amp;flush_file_lines();
if ($in{'type'}) {
	# Master server
	delete($config{'slave'});
	&amp;apply_table_changes() if ($in{'boot'});
	}
else {
	$out = &amp;backquote_logged("/usr/lib/yp/ypinit -s $in{'slave'} 2&gt;&amp;1");
	if ($?) { &amp;error("&lt;tt&gt;$out&lt;/tt&gt;"); }
	$config{'slave'} = $in{'slave'};
	}
&amp;write_file("$module_config_directory/config", \%config);
}

# 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 @pid = &amp;find_byname("ypserv");
if (@pid == 0) {
	return 0;
	}
elsif ($config{'slave'}) {
	return 2;
	}
else {
	return 1;
	}
}

# list_nis_tables()
# Returns a list of structures of all NIS tables
sub list_nis_tables
{
local ($var, $rule) = &amp;parse_yp_makefile();
local @rv;
local $dom = $var-&gt;{'LOCALDOMAIN'}-&gt;{'value'};
chop($dom = `domainname`) if ($dom =~ /`.*domainname`/);
local %file;
map { $file{uc($_)} = &amp;expand_vars($var-&gt;{uc($_)}-&gt;{'value'}, $var) } @nis_files;
local @all = split(/\s+/, $rule-&gt;{'all'}-&gt;{'value'});
foreach $t (@all) {
	local $table = { 'table' =&gt; $t,
		         'index' =&gt; scalar(@rv),
		         'domain' =&gt; $dom };
	if ($t eq "passwd") {
		if ($var-&gt;{'MERGE_PASSWD'}-&gt;{'value'} eq 'true') {
			$table-&gt;{'type'} = 'passwd_shadow';
			$table-&gt;{'files'} = [ $file{'PASSWD'}, $file{'SHADOW'} ];
			}
		elsif (&amp;indexof('shadow', @all) &gt;= 0) {
			# Show separate shadow and passwd tables as one table
			$table-&gt;{'type'} = 'passwd_shadow_full';
			$table-&gt;{'files'} = [ $file{'PASSWD'}, $file{'SHADOW'} ];
			@all = grep { $_ ne 'shadow' } @all;
			}
		else {
			$table-&gt;{'type'} = 'passwd';
			$table-&gt;{'files'} = [ $file{'PASSWD'} ];
			}
		}
	elsif ($t eq "group") {
		if ($var-&gt;{'MERGE_GROUP'}-&gt;{'value'} eq 'true') {
			$table-&gt;{'type'} = 'group_shadow';
			$table-&gt;{'files'} = [ $file{'GROUP'}, $file{'GSHADOW'} ];
			}
		else {
			$table-&gt;{'type'} = 'group';
			$table-&gt;{'files'} = [ $file{'GROUP'} ];
			}
		}
	elsif ($t eq "netgrp") {
		$table-&gt;{'type'} = "netgroup";
		$table-&gt;{'files'} = [ $file{'NETGROUP'} ];
		}
	elsif ($t eq "mail") {
		$table-&gt;{'type'} = "aliases";
		$table-&gt;{'files'} = [ $file{'ALIASES'} ];
		}
	else {
		$table-&gt;{'type'} = $t;
		$table-&gt;{'files'} = [ $file{uc($t)} ];
		}
	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
{
return ( "/etc/defaultdomain", "/var/yp/ypservers" );
}

1;

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