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

$binding_dir = "/var/yp/binding";
$yp_makefile = "/var/yp/Makefile";

# 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 (-x "/usr/lib/netsvc/yp/ypbind");
$rv += 2 if (-x "/usr/lib/netsvc/yp/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(DOM, "/etc/defaultdomain");
chop($nis-&gt;{'domain'} = &lt;DOM&gt;);
close(DOM);
if ($nis-&gt;{'domain'}) {
	if (open(SRV, "$binding_dir/$nis-&gt;{'domain'}/ypservers")) {
		while(&lt;SRV&gt;) {
			s/\r|\n//g;
			push(@{$nis-&gt;{'servers'}}, $_);
			}
		close(SRV);
		}
	else {
		$nis-&gt;{'broadcast'} = 1;
		}
	}
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
{
if ($_[0]-&gt;{'domain'}) {
	# Check if the servers are in /etc/hosts
	local @s = @{$_[0]-&gt;{'servers'}};
	&amp;foreign_require("net", "net-lib.pl");
	foreach $s (@s) {
		local $found = 0;
		foreach $h (&amp;foreign_call("net", "list_hosts")) {
			$found++ if (&amp;indexof($s, @{$h-&gt;{'hosts'}}) &gt;= 0);
			}
		return &amp;text("client_ehosts", $s) if (!$found);
		}

	# Write the files
	&amp;open_tempfile(DOM, "&gt;/etc/defaultdomain");
	&amp;print_tempfile(DOM, $_[0]-&gt;{'domain'},"\n");
	&amp;close_tempfile(DOM);
	mkdir("$binding_dir/$_[0]-&gt;{'domain'}", 0755);
	if (@s) {
		&amp;open_tempfile(SRV,"&gt;$binding_dir/$_[0]-&gt;{'domain'}/ypservers");
		foreach $s (@s) {
			&amp;print_tempfile(SRV, "$s\n");
			}
		&amp;close_tempfile(SRV);
		}
	else {
		unlink("$binding_dir/$_[0]-&gt;{'domain'}/ypservers");
		}
	}
else {
	unlink("/etc/defaultdomain");
	}

# Apply by running ypstop and ypstart
&amp;system_logged("(/usr/lib/netsvc/yp/ypstop || /etc/init.d/yp stop) &gt;/dev/null 2&gt;&amp;1");
&amp;system_logged("domainname \"$_[0]-&gt;{'domain'}\" &gt;/dev/null 2&gt;&amp;1");
&amp;system_logged("(/usr/lib/netsvc/yp/ypstart || /etc/init.d/yp start) &gt;/dev/null 2&gt;&amp;1");
sleep(2);
if ($_[0]-&gt;{'domain'}) {
	local $out = `ypwhich 2&gt;&amp;1`;
	if ($? || $out =~ /not\s+bound/i || $out =~ /can't\s+communicate/i) {
		system("(/usr/lib/netsvc/yp/ypstop || /etc/init.d/yp stop) &gt;/dev/null 2&gt;&amp;1");
		return $text{'client_eypwhich'};
		}
	}
}

@nis_tables = ( "passwd", "group", "hosts", "ethers", "networks", "rpc",
	        "services", "protocols", "netgroup", "bootparams", "aliases",
		"publickey", "netid", "netmasks", "c2secure", "timezone",
		"auto.master", "auto.home" );

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

print "&lt;tr&gt; &lt;td&gt;&lt;b&gt;$text{'server_boot'}&lt;/b&gt;&lt;/td&gt; &lt;td&gt;\n";
if ($dom &amp;&amp; -d "/var/yp/$dom") {
	print "&lt;i&gt;$text{'server_already'}&lt;/i&gt;\n";
	}
else {
	print "&lt;input type=radio name=boot value=1 &gt; $text{'yes'}\n";
	print "&lt;input type=radio name=boot value=0 checked &gt; $text{'no'}\n";
	}
print "&lt;/td&gt;\n";

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_def value=1 %s&gt; %s\n",
	$dom ? '' : 'checked', $text{'server_none'};
printf "&lt;input type=radio name=domain_def value=0 %s&gt;\n",
	$dom ? 'checked' : '';
printf "&lt;input name=domain size=35 value='%s'&gt;&lt;/td&gt; &lt;/tr&gt;\n", $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;tr&gt; &lt;td colspan=4&gt;&lt;i&gt;$text{'server_solaris'}&lt;/i&gt;&lt;/td&gt; &lt;/tr&gt;\n";

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";

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

print "&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; &lt;/tr&gt;\n",
	$var-&gt;{'B'}-&gt;{'value'} eq '-b' ? '' : 'checked', $text{'no'};

print "&lt;tr&gt; &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='\"\"' %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'};

print "&lt;tr&gt; &lt;td&gt;&lt;b&gt;$text{'server_dir'}&lt;/b&gt;&lt;/td&gt;\n";
printf "&lt;td&gt;&lt;input name=dir size=30 value='%s'&gt; %s&lt;/td&gt; &lt;/tr&gt;\n",
	$var-&gt;{'DIR'}-&gt;{'value'}, &amp;file_chooser_button("dir", 0);

print "&lt;tr&gt; &lt;td&gt;&lt;b&gt;$text{'server_pwdir'}&lt;/b&gt;&lt;/td&gt;\n";
printf "&lt;td&gt;&lt;input name=pwdir size=30 value='%s'&gt; %s&lt;/td&gt; &lt;/tr&gt;\n",
	$var-&gt;{'PWDIR'}-&gt;{'value'}, &amp;file_chooser_button("pwdir", 0);
}

# parse_server_config()
# Parse and save the NIS server options
sub parse_server_config
{
local ($var, $rule) = &amp;parse_yp_makefile();
$in{'domain_def'} || $in{'domain'} =~ /^[A-Za-z0-9\.\-\_]+$/ ||
	&amp;error(&amp;text('server_edomain', $in{'domain'}));
if ($in{'boot'} &amp;&amp; $in{'domain_def'}) {
	&amp;error($text{'server_ebootdom'});
	}
$in{'type'} || &amp;to_ipaddress($in{'slave'}) ||
	&amp;to_ip6address($in{'slave'}) || &amp;error($text{'server_eslave'});
-d $in{'dir'} || &amp;error($text{'server_edir'});
-d $in{'pwdir'} || &amp;error($text{'server_epwdir'});
&amp;update_makefile($var-&gt;{'NOPUSH'}, $in{'nopush'});
&amp;update_makefile($var-&gt;{'B'}, $in{'b'});
&amp;update_makefile($rule-&gt;{'all'}, join(" ", split(/\0/, $in{'tables'})), "");
&amp;update_makefile($var-&gt;{'DIR'}, $in{'dir'});
&amp;update_makefile($var-&gt;{'PWDIR'}, $in{'pwdir'});
&amp;flush_file_lines();

if ($in{'domain_def'}) {
	unlink("/etc/defaultdomain");
	&amp;system_logged("domainname \"\" &gt;/dev/null 2&gt;&amp;1");
	}
else {
	local $old = `domainname`; chop($old);
	&amp;open_tempfile(DOM, "&gt;/etc/defaultdomain");
	&amp;print_tempfile(DOM, "$in{'domain'}\n");
	&amp;close_tempfile(DOM);
	&amp;system_logged("domainname \"$in{'domain'}\" &gt;/dev/null 2&gt;&amp;1");
	if ($in{'boot'}) {
		# Create the domain directory
		mkdir("/var/yp/$in{'domain'}", 0755);
		&amp;system_logged("rm -f /var/yp/*.time");	# force a remake
		}
	}

if ($in{'type'}) {
	# Master server
	delete($config{'slave'});
	&amp;apply_table_changes()
		if (!$in{'domain_def'} &amp;&amp; -d "/var/yp/$in{'domain'}");
	}
else {
	local $temp = &amp;transname();
	open(TEMP, "&gt;$temp");
	print TEMP "n\ny\n";
	close(TEMP);
	$out = &amp;backquote_logged("/usr/sbin/ypinit -s $in{'slave'} &lt;$temp 2&gt;&amp;1");
	unlink($temp);
	if ($?) { &amp;error("&lt;tt&gt;$out&lt;/tt&gt;"); }
	$config{'slave'} = $in{'slave'};
	}
&amp;write_file("$module_config_directory/config", \%config);
&amp;system_logged("(/usr/lib/netsvc/yp/ypstop || /etc/init.d/yp stop) &gt;/dev/null 2&gt;&amp;1");
&amp;system_logged("(/usr/lib/netsvc/yp/ypstart || /etc/init.d/yp start) &gt;/dev/null 2&gt;&amp;1");
}

# 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 $dom = `domainname`; chop($dom);
return !$dom ? 0 : $config{'slave'} ? 2 : 1;
}

# parse_yp_makefile()
# Returns hashes of makefile variables and rules
sub parse_yp_makefile
{
# First parse joined lines
local $lnum = 0;
local (@lines, $llast);
open(MAKE, $yp_makefile);
while(&lt;MAKE&gt;) {
	s/\r|\n//g;
	local $slash = (s/\\$//);
	s/#.*$//;
	if ($llast) {
		$llast-&gt;{'value'} .= " $_";
		$llast-&gt;{'eline'} = $lnum;
		}
	else {
		push(@lines, { 'value' =&gt; $_,
			       'line' =&gt; $lnum,
			       'eline' =&gt; $lnum });
		}
	$llast = $slash ? $lines[$#lines] : undef;
	$lnum++;
	}
close(MAKE);

# Then look for variables and rules
local ($i, %var, %rule);
for($i=0; $i&lt;@lines; $i++) {
	if ($lines[$i]-&gt;{'value'} =~ /^\s*(\S+)\s*=\s*(.*)/) {
		# Found a variable
		$var{$1} = { 'name' =&gt; $1,
			     'value' =&gt; $2,
			     'type' =&gt; 0,
			     'line' =&gt; $lines[$i]-&gt;{'line'},
			     'eline' =&gt; $lines[$i]-&gt;{'eline'} };
		}
	elsif ($lines[$i]-&gt;{'value'} =~ /^\s*(\S+):\s*(.*)/) {
		# Found a makefile rule
		$rule{$1} = { 'name' =&gt; $1,
			      'value' =&gt; $2,
			      'type' =&gt; 1,
			      'code' =&gt; $lines[$i+1]-&gt;{'value'},
			      'line' =&gt; $lines[$i]-&gt;{'line'},
			      'eline' =&gt; $lines[$i+1]-&gt;{'eline'} };
		$i++;
		}
	}
return ( \%var, \%rule );
}

# expand_vars(string, &amp;vars)
sub expand_vars
{
local $rv = $_[0];
while($rv =~ /^(.*)\$\(([A-Za-z0-9_]+)\)(.*)$/) {
	$rv = $1.$_[1]-&gt;{$2}-&gt;{'value'}.$3;
	}
return $rv;
}

# update_makefile(&amp;old, value, [value]);
sub update_makefile
{
local $lref = &amp;read_file_lines($yp_makefile);
local @n;
if ($_[0]-&gt;{'type'} == 0) {
	@n = ( "$_[0]-&gt;{'name'} = $_[1]" );
	}
else {
	@n = ( "$_[0]-&gt;{'name'}: $_[1]", $_[2] );
	}
splice(@$lref, $_[0]-&gt;{'line'}, $_[0]-&gt;{'eline'} - $_[0]-&gt;{'line'} + 1, @n);
}

# apply_table_changes()
# Do whatever is necessary for the table text files to be loaded into
# the NIS server
sub apply_table_changes
{
if ($0 =~ /save_(\S+).cgi/) {
	# Try to make just one table
	local $table = $1;
	local $ex = &amp;system_logged("(cd /var/yp ; /usr/ccs/bin/make $table) &gt;/dev/null 2&gt;&amp;1 &lt;/dev/null");
	return if (!$ex);
	}
&amp;system_logged("(cd /var/yp ; /usr/ccs/bin/make) &gt;/dev/null 2&gt;&amp;1 &lt;/dev/null");
}

# list_nis_tables()
# Returns a list of structures of all NIS tables
sub list_nis_tables
{
local @rv;
local ($var, $rule) = &amp;parse_yp_makefile();
local $dom = `domainname`; chop($dom);
local @all = split(/\s+/, $rule-&gt;{'all'}-&gt;{'value'});
foreach $t (@all) {
	local $table = { 'table' =&gt; $t,
		         'index' =&gt; scalar(@rv),
		         'domain' =&gt; $dom };
	local $rt = $rule-&gt;{"$t.time"};
	local @files = split(/\s+/, $rt-&gt;{'value'});
	@files = map { &amp;expand_vars($_, $var) } @files;
	$table-&gt;{'files'} = \@files;
	$table-&gt;{'type'} = $t eq 'passwd' &amp;&amp; @files &gt; 1 ? 'passwd_shadow' :
			   $t;
	push(@rv, $table);
	}
return @rv;
}

sub show_server_security
{
}

sub parse_server_security
{
&amp;system_logged("(/usr/lib/netsvc/yp/ypstop || /etc/init.d/yp stop) &gt;/dev/null 2&gt;&amp;1");
&amp;system_logged("(/usr/lib/netsvc/yp/ypstart || /etc/init.d/yp start) &gt;/dev/null 2&gt;&amp;1");
}

sub extra_config_files
{
local @rv = ( "/etc/defaultdomain" );
local $conf = &amp;get_client_config();
if ($conf-&gt;{'domain'}) {
	push(@rv, "$binding_dir/$conf-&gt;{'domain'}/ypservers");
	}
return @rv;
}

1;

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