<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># pap-lib.pl
# Functions for managing the mgetty configuration files

BEGIN { push(@INC, ".."); };
use WebminCore;
&amp;init_config();
%access = &amp;get_module_acl();

do 'secrets-lib.pl';

# mgetty_inittabs()
# Returns a list of inittab entries for mgetty, with options parsed
sub mgetty_inittabs
{
local @rv;
foreach $i (&amp;inittab::parse_inittab()) {
	if ($i-&gt;{'process'} =~ /^(\S*mgetty)\s*(.*)\s+((\/.*)?(tty|term|cua)\S+)(\s+(\S+))?$/) {
		$i-&gt;{'mgetty'} = $1;
		$i-&gt;{'args'} = $2;
		$i-&gt;{'tty'} = $3;
		$i-&gt;{'ttydefs'} = $7;
		if ($i-&gt;{'args'} =~ s/\s*-s\s+(\d+)//) {
			$i-&gt;{'speed'} = $1;
			}
		if ($i-&gt;{'args'} =~ s/\s*-r//) {
			$i-&gt;{'direct'} = 1;
			}
		if ($i-&gt;{'args'} =~ s/\s*-n\s+(\d+)//) {
			$i-&gt;{'rings'} = $1;
			}
		if ($i-&gt;{'args'} =~ s/\s*-D//) {
			$i-&gt;{'data'} = 1;
			}
		if ($i-&gt;{'args'} =~ s/\s*-F//) {
			$i-&gt;{'fax'} = 1;
			}
		if ($i-&gt;{'args'} =~ s/\s*-R\s+(\d+)//) {
			$i-&gt;{'back'} = $1;
			}
		if ($i-&gt;{'args'} =~ s/\s*-p\s+'([^']+)'// ||
		    $i-&gt;{'args'} =~ s/\s*-p\s+"([^"]+)"// ||
		    $i-&gt;{'args'} =~ s/\s*-p\s+(\S+)//) {
			$i-&gt;{'prompt'} = $1;
			}
		push(@rv, $i);
		}
	elsif ($i-&gt;{'process'} =~ /^(\S*vgetty)\s*(.*)\s+((\/.*)?tty\S+)/) {
		$i-&gt;{'vgetty'} = $1;
		$i-&gt;{'args'} = $2;
		$i-&gt;{'tty'} = $3;
		push(@rv, $i);
		}
	}
return @rv;
}

# parse_ppp_options(file)
sub parse_ppp_options
{
local @rv;
local $lnum = 0;
open(OPTS, $_[0]);
while(&lt;OPTS&gt;) {
	s/\r|\n//g;
	s/#.*$//g;
	if (/^([0-9\.]+):([0-9\.]+)/) {
		push(@rv, { 'local' =&gt; $1,
			    'remote' =&gt; $2,
			    'file' =&gt; $_[0],
			    'line' =&gt; $lnum,
			    'index' =&gt; scalar(@rv) });
		}
	elsif (/^(\S+)\s*(.*)/) {
		push(@rv, { 'name' =&gt; $1,
			    'value' =&gt; $2,
			    'file' =&gt; $_[0],
			    'line' =&gt; $lnum,
			    'index' =&gt; scalar(@rv) });
		}
	$lnum++;
	}
close(OPTS);
return @rv;
}

# find(name, &amp;config)
sub find
{
local @rv = grep { lc($_-&gt;{'name'}) eq lc($_[0]) } @{$_[1]};
return wantarray ? @rv : $rv[0];
}

# save_ppp_option(&amp;config, file, &amp;old|name, &amp;new)
sub save_ppp_option
{
local $ol = ref($_[2]) || !defined($_[2]) ? $_[2] : &amp;find($_[2], $_[0]);
local $nw = $_[3];
local $lref = &amp;read_file_lines($_[1]);
local $line;
if ($nw) {
	if ($nw-&gt;{'local'}) {
		$line = $nw-&gt;{'local'}.":".$nw-&gt;{'remote'};
		}
	else {
		$line = $nw-&gt;{'name'};
		$line .= " $nw-&gt;{'value'}" if ($nw-&gt;{'value'} ne "");
		}
	}
if ($ol &amp;&amp; $nw) {
	$lref-&gt;[$ol-&gt;{'line'}] = $line;
	}
elsif ($ol) {
	splice(@$lref, $ol-&gt;{'line'}, 1);
	local $c;
	foreach $c (@{$_[0]}) {
		$c-&gt;{'line'}-- if ($c-&gt;{'line'} &gt; $ol-&gt;{'line'});
		}
	}
elsif ($nw) {
	push(@$lref, $line);
	}
}

# parse_login_config()
# Parses the mgetty login options file into a list of users
sub parse_login_config
{
local @rv;
local $lnum = 0;
open(LOGIN, $config{'login_config'});
while(&lt;LOGIN&gt;) {
	s/\r|\n//g;
	s/#.*$//g;
	if (/^(\S+)\s+(\S+)\s+(\S+)\s+(.*)/) {
		push(@rv, { 'user' =&gt; $1,
			    'userid' =&gt; $2,
			    'utmp' =&gt; $3,
			    'program' =&gt; $4,
			    'line' =&gt; $lnum });
		}
	$lnum++;
	}
close(LOGIN);
return @rv;
}

# delete_login_config(&amp;config, &amp;login)
sub delete_login_config
{
local $lref = &amp;read_file_lines($config{'login_config'});
splice(@$lref, $_[1]-&gt;{'line'}, 1);
}

# create_login_config(&amp;config, &amp;login)
sub create_login_config
{
local ($star) = grep { $_-&gt;{'user'} eq '*' } @{$_[0]};
local $line = join("\t", $_[1]-&gt;{'user'}, $_[1]-&gt;{'userid'},
			 $_[1]-&gt;{'utmp'}, $_[1]-&gt;{'program'});
local $lref = &amp;read_file_lines($config{'login_config'});
if ($star) {
	splice(@$lref, $star-&gt;{'line'}, 0, $line);
	}
else {
	push(@$lref, $line);
	}
}

# parse_dialin_config()
# Parses the mgetty dialin file
sub parse_dialin_config
{
local @rv;
local $lnum = 0;
open(DIALIN, $config{'dialin_config'});
while(&lt;DIALIN&gt;) {
	s/\r|\n//g;
	s/#.*$//g;
	s/^\s+//;
	local $t;
	foreach $t (split(/[ \t,]+/, $_)) {
		local ($not) = ($t =~ s/^\!//);
		push(@rv, { 'number' =&gt; $t,
			    'not' =&gt; $not,
			    'index' =&gt; scalar(@rv),
			    'line' =&gt; $lnum });
		}
	$lnum++;
	}
close(DIALIN);
return @rv;
}

# create_dialin(&amp;dialin)
sub create_dialin
{
&amp;open_tempfile(DIALIN, "&gt;&gt;$config{'dialin_config'}");
&amp;print_tempfile(DIALIN, &amp;dialin_line($_[0])."\n");
&amp;close_tempfile(DIALIN);
}

# delete_dialin(&amp;dialin, &amp;config)
sub delete_dialin
{
local @same = grep { $_-&gt;{'line'} == $_[0]-&gt;{'line'} &amp;&amp; $_ ne $_[0] }
		   @{$_[1]};
if (@same) {
	&amp;replace_file_line($config{'dialin_config'}, $_[0]-&gt;{'line'},
			   join(" ", map { &amp;dialin_line($_) } @same)."\n");
	}
else {
	&amp;replace_file_line($config{'dialin_config'}, $_[0]-&gt;{'line'});
	}
}

# modify_dialin(&amp;dialin, &amp;config)
sub modify_dialin
{
local @same = grep { $_-&gt;{'line'} == $_[0]-&gt;{'line'} } @{$_[1]};
&amp;replace_file_line($config{'dialin_config'}, $_[0]-&gt;{'line'},
		   join(" ", map { &amp;dialin_line($_) } @same)."\n");
}

# swap_dialins(&amp;dialin1, &amp;dialin2, &amp;config)
sub swap_dialins
{
local $lref = &amp;read_file_lines($config{'dialin_config'});
local @same1 = grep { $_-&gt;{'line'} == $_[0]-&gt;{'line'} } @{$_[2]};
local @same2 = grep { $_-&gt;{'line'} == $_[1]-&gt;{'line'} } @{$_[2]};
local $idx1 = &amp;indexof($_[0], @same1);
local $idx2 = &amp;indexof($_[1], @same2);
if ($_[0]-&gt;{'line'} == $_[1]-&gt;{'line'}) {
	($same1[$idx1], $same1[$idx2]) = ($same1[$idx2], $same1[$idx1]);
	&amp;replace_file_line($config{'dialin_config'}, $_[0]-&gt;{'line'},
			   join(" ", map { &amp;dialin_line($_) } @same1)."\n");
	}
else {
	($same1[$idx1], $same2[$idx2]) = ($same2[$idx2], $same1[$idx1]);
	&amp;replace_file_line($config{'dialin_config'}, $_[0]-&gt;{'line'},
			   join(" ", map { &amp;dialin_line($_) } @same1)."\n");
	&amp;replace_file_line($config{'dialin_config'}, $_[1]-&gt;{'line'},
			   join(" ", map { &amp;dialin_line($_) } @same2)."\n");
	}
}

# dialin_line(&amp;dialin)
sub dialin_line
{
return ($_[0]-&gt;{'not'} ? "!" : "").$_[0]-&gt;{'number'};
}

# apply_mgetty()
# Apply the current serial port and mgetty configuration, or return an
# error message
sub apply_mgetty
{
local %iconfig = &amp;foreign_config("inittab");
local $out = &amp;backquote_logged("$iconfig{'telinit'} q 2&gt;&amp;1 &lt;/dev/null");
if ($?) {
	return "&lt;tt&gt;$out&lt;/tt&gt;";
	}
&amp;kill_byname_logged("mgetty", 'TERM');
return undef;
}

1;

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