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

BEGIN { push(@INC, ".."); };
use WebminCore;
&amp;init_config();
if (-r "$module_root_directory/$gconfig{'os_type'}-$gconfig{'os_version'}-lib.pl") {
	do "$gconfig{'os_type'}-$gconfig{'os_version'}-lib.pl";
	}
else {
	do "$gconfig{'os_type'}-lib.pl";
	}
if ($gconfig{'os_type'} =~ /-linux$/) {
	do "linux-lib.pl";
	}
&amp;foreign_require("init", "init-lib.pl");

# init_script(action)
# Returns the full path to some init script
sub init_script
{
local %iconfig = &amp;foreign_config("init");
return "$iconfig{'init_dir'}/$_[0]";
}

# get_nsswitch_conf()
# Parses lines of nsswitch.conf into an array
sub get_nsswitch_conf
{
local @rv;
open(SWITCH, $config{'nsswitch_conf'});
while(&lt;SWITCH&gt;) {
	s/\r|\n//g;
	s/#.*$//g;
	if (/^\s*(\S+):\s*(.*)/) {
		local $sw = { 'service' =&gt; $1,
			      'order' =&gt; $2 };
		push(@rv, $sw);
		}
	}
close(SWITCH);
return @rv;
}

# save_nsswitch(service, order)
# Updates the line for some service in nsswitch.conf
sub save_nsswitch
{
local $lref = &amp;read_file_lines($config{'nsswitch_conf'});
foreach $l (@$lref) {
	if ($l =~ /^\s*(\S+):/ &amp;&amp; $1 eq $_[0]) {
		$l = "$_[0]:\t$_[1]";
		last;
		}
	}
}

# table_edit_setup(table, line, splitter)
# Returns &amp;table, &amp;lnums, line1, line2, ...
sub table_edit_setup
{
local @tables = &amp;list_nis_tables();
local $t = $tables[$_[0]];
return ( $t ) if (!defined($_[1]));
local @lnums = ( $_[1] );
local $lref = &amp;read_file_lines($t-&gt;{'files'}-&gt;[0]);
local @lines = ( [ split($_[2], $lref-&gt;[$_[1]]) ] );
local $i;
for($i=1; $t-&gt;{'files'}-&gt;[$i]; $i++) {
	local $lref2 = &amp;read_file_lines($t-&gt;{'files'}-&gt;[$i]);
	local $lnum = 0;
	foreach $l (@$lref2) {
		local @line2 = split($_[2], $l);
		if ($line2[0] eq $lines[0]-&gt;[0]) {
			push(@lnums, $lnum);
			push(@lines, \@line2);
			last;
			}
		$lnum++;
		}
	}
return ($t, \@lnums, @lines);
}

# table_add(&amp;table, separator, &amp;record, ...)
# Adds a record to an NIS table
sub table_add
{
local $i = 2;
foreach $f (@{$_[0]-&gt;{'files'}}) {
	local $lref = &amp;read_file_lines($f);
	push(@$lref, join($_[1], @{$_[$i++]}));
	}
&amp;flush_file_lines();
}

# table_delete(&amp;table, &amp;lnums)
# Delete a record from an NIS table
sub table_delete
{
local $i = 0;
foreach $f (@{$_[0]-&gt;{'files'}}) {
	local $lref = &amp;read_file_lines($f);
	splice(@$lref, $_[1]-&gt;[$i], 1);
	$i++;
	}
&amp;flush_file_lines();
}

# table_update(&amp;table, &amp;lnums, separator, &amp;record, ...)
# Modify a record in an NIS table
sub table_update
{
local $i = 0;
foreach $f (@{$_[0]-&gt;{'files'}}) {
	local $lref = &amp;read_file_lines($f);
	splice(@$lref, $_[1]-&gt;[$i], 1, join($_[2], @{$_[$i+3]}));
	$i++;
	}
&amp;flush_file_lines();
}

# date_input(day, month, year, prefix)
sub date_input
{
print "&lt;input name=$_[3]d size=3 value='$_[0]'&gt;";
print "/&lt;select name=$_[3]m&gt;\n";
local $m;
foreach $m (1..12) {
	printf "&lt;option value=%d %s&gt;%s&lt;/option&gt;\n",
		$m, $_[1] eq $m ? 'selected' : '', $text{"smonth_$m"};
	}
print "&lt;/select&gt;";
print "/&lt;input name=$_[3]y size=5 value='$_[2]'&gt;";
print &amp;date_chooser_button("$_[3]d", "$_[3]m", "$_[3]y");
}

# parse_ypserv_conf()
# Returns &amp;opts, &amp;maps
sub parse_ypserv_conf
{
local (%opts, @hosts);
local $lnum = 0;
open(CONF, $ypserv_conf);
while(&lt;CONF&gt;) {
	s/\r|\n//g;
	s/#.*$//;
	if (/^\s*([^:\s]+):\s*(yes|no)/) {
		# Found an option
		$opts{$1} = { 'name' =&gt; $1,
			      'value' =&gt; $2 eq 'yes' ? 1 : 0,
			      'line' =&gt; $lnum };
		}
	elsif (/^\s*([^:\s]+)\s*:\s*([^:\s]+)\s*:\s*([^:\s]+)\s*:\s*(none|port|mangle|deny)(\/mangle(:(\d+))?)?/) {
		# Found a host and domain line (new format)
		push(@hosts, { 'host' =&gt; $1,
			       'domain' =&gt; $2,
			       'map' =&gt; $3,
			       'sec' =&gt; $4,
			       'mangle' =&gt; $5 ? 1 : 0,
			       'field' =&gt; $7,
			       'line' =&gt; $lnum } );
		}
	elsif (/^\s*([^:\s]+)\s*:\s*([^:\s]+)\s*:\s*([^:\s]+)(\s*:\s*([^:\s]+))?(\s*:\s*([^:\s]+))?/) {
		# Found a host line (old format)
		push(@hosts, { 'host' =&gt; $1,
			       'map' =&gt; $2,
			       'sec' =&gt; $3,
			       'mangle' =&gt; $5 eq 'yes' ? 1 : 0,
			       'field' =&gt; $7 eq '' ? 2 : $7,
			       'line' =&gt; $lnum } );
		}
	$lnum++;
	}
close(CONF);
return (\%opts, \@hosts);
}

# 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*\+=\s*(.*)/) {
		# Adding to a variable
		if ($var{$1}) {
			$var{$1}-&gt;{'value'} .= ' '.$2;
			}
		}
	elsif ($lines[$i]-&gt;{'value'} =~ /^\s*(\S+):\s*(.*)/) {
		# Found a makefile rule
		$rule{$1} = { 'name' =&gt; $1,
			      'value' =&gt; $2,
			      'type' =&gt; 1,
			      'line' =&gt; $lines[$i]-&gt;{'line'},
			      'eline' =&gt; $lines[$i]-&gt;{'eline'} };
		if ($lines[$i+1]-&gt;{'value'} =~ /^\s+/) {
			$rule{$1}-&gt;{'code'} = $lines[$i+1]-&gt;{'value'};
			$rule{$1}-&gt;{'eline'} = $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_]+)\)(.*)$/) {
#	if (substr($_[1]-&gt;{$2}-&gt;{'value'}, 0, 7) eq '$(shell') {
#		$rv = $1."\0(".$2.")".$3;
#		}
#	else {
		$rv = $1.$_[1]-&gt;{$2}-&gt;{'value'}.$3;
#		}
	}
#$rv =~ s/\0/\$/g;
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);
}

1;

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