<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># pam-lib.pl
# Functions for manipulating the PAM services file(s)

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

# get_pam_config()
# Returns a list of services and their modules
sub get_pam_config
{
local @rv;
local @ignore = split(/\s+/, $config{'ignore'});
opendir(DIR, &amp;translate_filename($config{'pam_dir'}));
FILE: foreach $f (readdir(DIR)) {
	next if ($f =~ /^\./);
	foreach $i (@ignore) {
		next FILE if ($f =~ /^$i$/i);
		}
	local $serv = { 'name' =&gt; $f,
			'file' =&gt; "$config{'pam_dir'}/$f",
			'index' =&gt; scalar(@rv) };
	local $lnum = 0;
	&amp;open_tempfile(FILE, $serv-&gt;{'file'});
	while(&lt;FILE&gt;) {
		s/\r|\n//g;
		if (/^\s*#+\s*description:\s*([A-Za-z].*)/i &amp;&amp;
		    !$serv-&gt;{'mods'}) {
			$serv-&gt;{'desc'} = $1;
			}
		s/#.*$//g;
		if (/^\s*\@include\s+(\S+)/) {
			# Special include line
			local $mod = { 'include' =&gt; $1,
				       'line' =&gt; $lnum,
				       'index' =&gt; @{$serv-&gt;{'mods'}}+0 };
			push(@{$serv-&gt;{'mods'}}, $mod);
			}
		elsif (/^\s*(\S+)\s+\[([^\]]*)\]\s+(\S+)\s*(.*)$/) {
			# Line with special rules .. ignore for now
			}
		elsif (/^\s*(\S+)\s+(\S+)\s+(\S+)\s*(.*)$/) {
			# Regular line
			local $mod = { 'type' =&gt; $1,   'control' =&gt; $2,
				       'module' =&gt; $3, 'args' =&gt; $4,
				       'line' =&gt; $lnum,
				       'index' =&gt; @{$serv-&gt;{'mods'}}+0 };
			push(@{$serv-&gt;{'mods'}}, $mod);
			}
		$lnum++;
		}
	&amp;close_tempfile(FILE);
	push(@rv, $serv);
	}
closedir(DIR);
return @rv;
}

# create_module(service, &amp;module)
# Add a PAM module to some service
sub create_module
{
local $lref = &amp;read_file_lines("$config{'pam_dir'}/$_[0]");
push(@$lref, &amp;module_line($_[1]));
&amp;flush_file_lines();
}

# modify_module(service, &amp;module)
# Update a PAM module in some service
sub modify_module
{
local $lref = &amp;read_file_lines("$config{'pam_dir'}/$_[0]");
splice(@$lref, $_[1]-&gt;{'line'}, 1, &amp;module_line($_[1]));
&amp;flush_file_lines();
}

# delete_module(service, &amp;module)
# Delete a PAM module from some service
sub delete_module
{
local $lref = &amp;read_file_lines("$config{'pam_dir'}/$_[0]");
splice(@$lref, $_[1]-&gt;{'line'}, 1);
&amp;flush_file_lines();
}

# swap_modules(service, &amp;module1, &amp;module2)
# Swap two PAM module entries in a service
sub swap_modules
{
local $lref = &amp;read_file_lines("$config{'pam_dir'}/$_[0]");
local $line = $lref-&gt;[$_[1]-&gt;{'line'}];
$lref-&gt;[$_[1]-&gt;{'line'}] = $lref-&gt;[$_[2]-&gt;{'line'}];
$lref-&gt;[$_[2]-&gt;{'line'}] = $line;
&amp;flush_file_lines();
}

# module_line(&amp;module)
# Returns text for a PAM module line
sub module_line
{
if ($_[0]-&gt;{'include'}) {
	# Special include line
	return "\@include ".$_[0]-&gt;{'include'};
	}
else {
	# A regular module
	local $l = join("\t", $_[0]-&gt;{'type'}, $_[0]-&gt;{'control'},
			      $_[0]-&gt;{'module'});
	$l .= "\t$_[0]-&gt;{'args'}" if ($_[0]-&gt;{'args'});
	return $l;
	}
}

# list_modules()
# Returns a list of all PAM shared libraries
sub list_modules
{
local (@rv, %done, %hasmod);
foreach $d (map { glob($_) } split(/\s+/, $config{'lib_dirs'})) {
	opendir(DIR, &amp;translate_filename($d));
	foreach $f (sort { $a cmp $b } readdir(DIR)) {
		local @st = stat(&amp;translate_filename("$d/$f"));
		push(@rv, $f) if (!$done{$st[1]}++ &amp;&amp; $f =~ /^pam_.*\.so$/);
		$hasmod{$f}++ if ($f =~ /^pam_.*\.so$/);
		}
	closedir(DIR);
	}
foreach $q (split(/\s+/, $config{'mod_equiv'})) {
	local ($q1, $q2) = split(/=/, $q);
	if ($hasmod{$q2}) {
		@rv = grep { $_ ne $q1 } @rv;
		}
	}
return sort { $a cmp $b } &amp;unique(@rv);
}

# include_style(&amp;pam)
# Returns 1 if includes are done with pam_stack.so, 2 if done with include
# lines, 3 if done with @include, 0 if not supported
sub include_style
{
local ($pam) = @_;
local @allmods = map { @{$_-&gt;{'mods'}} } @$pam;
local ($atinc) = grep { $_-&gt;{'include'} } @allmods;
local ($inc) = grep { $_-&gt;{'control'} eq 'include' } @allmods;
local ($stack) = grep { $_ eq "pam_stack.so" } &amp;list_modules();
return $atinc ? 3 : $inc ? 2 : $stack ? 1 : 0;
}

1;

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