<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># linux-lib.pl
# Functions for reading linux format last output

# passfiles_type()
# Returns 0 for old-style passwords (/etc/passwd only), 1 for FreeBSD-style
# (/etc/master.passwd) and 2 for SysV (/etc/passwd &amp; /etc/shadow)
sub passfiles_type
{
return &amp;password_file($config{'shadow_file'}) ? 2 : 0;
}

# groupfiles_type()
# Returns 0 for normal group file (/etc/group only) and 2 for shadowed
# (/etc/group and /etc/gshadow)
sub groupfiles_type
{
return &amp;password_file($config{'gshadow_file'}) ? 2 : 0;
}

# open_last_command(handle, user)
sub open_last_command
{
local ($fh, $user) = @_;
local $quser = quotemeta($user);
open($fh, "(last -F $quser || last $quser) |");
}

# read_last_line(handle)
# Parses a line of output from last into an array of
#  user, tty, host, login, logout, period
sub read_last_line
{
$fh = $_[0];
while(1) {
	chop($line = &lt;$fh&gt;);
	if (!$line) { return (); }
	if ($line =~ /system boot/) { next; }
	if ($line =~ /^(\S+)\s+(\S+)\s+(\S+)?\s+(\S+\s+\S+\s+\d+\s+\d+:\d+)\s+\-\s+(\S+)\s+\((\d+:\d+)\)/) {
		# jcameron  pts/0  fudu Thu Feb 22 09:47 - 10:15
		return ($1, $2, $3, $4, $5 eq "down" ? "Shutdown" : $5, $6);
		}
	elsif ($line =~ /^(\S+)\s+(\S+)\s+(\S+)?\s+(\S+\s+\S+\s+\d+\s+\d+:\d+:\d+\s+\d+)\s+\-\s+(\S+\s+\S+\s+\d+\s+\d+:\d+:\d+\s+\d+)\s+\((\d+:\d+)\)/) {
		# jcameron  pts/0  fudu Thu Feb 22 09:47 - 10:15
		return ($1, $2, $3, $4, $5 eq "down" ? "Shutdown" : $5, $6);
		}
	elsif ($line =~ /^(\S+)\s+(\S+)\s+(\S+)?\s+(\S+\s+\S+\s+\d+\s+\d+:\d+)\s+still/) {
		# root  pts/0  fudu  Fri Feb 23 18:46  still logged in   
		return ($1, $2, $3, $4);
		}
	}
}

# os_most_recent_logins()
# Returns hash ref from username to the most recent login as time string
sub os_most_recent_logins
{
my %rv;
&amp;clean_language();
open(LASTLOG, "lastlog |");
while(&lt;LASTLOG&gt;) {
	s/\r|\n//g;
	if (/^(\S+)/) {
		my $user = $1;
		if (/((\S+)\s+(\S+)\s+\d+\s+(\d+):(\d+):(\d+)\s+([\-\+]\d+)\s+(\d+))/) {
			# Have a date to parse
			$rv{$user} = $1;
			}
		else {
			$rv{$user} = undef;
			}
		}
	}
close(LASTLOG);
&amp;reset_environment();
return \%rv;
}

# logged_in_users()
# Returns a list of hashes containing details of logged-in users
sub logged_in_users
{
local @rv;
open(WHO, "who |");
while(&lt;WHO&gt;) {
	if (/^(\S+)\s+(\S+)\s+(\S+\s+\d+\s+\d+:\d+)\s+(\((\S+)\))?/) {
		push(@rv, { 'user' =&gt; $1, 'tty' =&gt; $2,
			    'when' =&gt; $3, 'from' =&gt; $5 });
		}
	}
close(WHO);
return @rv;
}

# use_md5()
# Returns 1 if pam is set up to use MD5 encryption, 2 for blowfish, 3 for SHA512
sub use_md5
{
if (defined($use_md5_cache)) {
	# Don't re-look this up
	return $use_md5_cache;
	}
local $md5 = 0;
if (&amp;foreign_check("pam")) {
	# Use the PAM module if we can
	&amp;foreign_require("pam", "pam-lib.pl");
	local @conf = &amp;foreign_call("pam", "get_pam_config");
	local ($svc) = grep { $_-&gt;{'name'} eq 'passwd' } @conf;
	LOOP:
	foreach my $m (@{$svc-&gt;{'mods'}}) {
		if ($m-&gt;{'type'} eq 'password') {
			if ($m-&gt;{'args'} =~ /md5/) {
				$md5 = 1;
				}
			elsif ($m-&gt;{'args'} =~ /sha512/) {
				$md5 = 3;
				}
			elsif ($m-&gt;{'args'} =~ /blowfish/) {
				$md5 = 2;
				}
			elsif ($m-&gt;{'module'} =~ /pam_stack\.so/ &amp;&amp;
			       $m-&gt;{'args'} =~ /service=(\S+)/) {
				# Referred to another service!
				($svc) = grep { $_-&gt;{'name'} eq $1 } @conf;
				if ($svc) { goto LOOP }
				else { last; }
				}
                        elsif ($m-&gt;{'control'} eq 'include') {
                                # Include another section
                                ($svc) = grep { $_-&gt;{'name'} eq $m-&gt;{'module'} }
					      @conf;
                                if ($svc) { goto LOOP }
                                else { last; }
                                }
			}
		elsif ($m-&gt;{'include'}) {
			# Include another section, with @ syntax
			($svc) = grep { $_-&gt;{'name'} eq $m-&gt;{'include'} } @conf;
			if ($svc) { goto LOOP }
			else { last; }
			}
		}
	}
if (!$md5 &amp;&amp; &amp;open_readfile(PAM, "/etc/pam.d/passwd")) {
	# Otherwise try to check the PAM file directly
	while(&lt;PAM&gt;) {
		s/#.*$//g;
		if (/^password.*md5/) { $md5 = 1; }
		elsif (/^password.*blowfish/) { $md5 = 2; }
		elsif (/^password.*sha512/) { $md5 = 3; }
		}
	close(PAM);
	}
if (!$md5 &amp;&amp; (&amp;open_readfile(PAM, "/etc/pam.d/common-password") ||
	      &amp;open_readfile(PAM, "/etc/pam.d/system-auth"))) {
	# Then try reading common password config file
	while(&lt;PAM&gt;) {
		s/#.*$//g;
		if (/^password.*md5/) { $md5 = 1; }
		elsif (/^password.*blowfish/) { $md5 = 2; }
		elsif (/^password.*sha512/) { $md5 = 3; }
		}
	close(PAM);
	}
if (&amp;open_readfile(DEFS, "/etc/login.defs")) {
	# The login.defs file is used on debian sometimes
	while(&lt;DEFS&gt;) {
		s/#.*$//g;
		$md5 = 1 if (/MD5_CRYPT_ENAB\s+yes/i);
		}
	close(DEFS);
	}
$use_md5_cache = $md5;
return $md5;
}

1;
</pre></body></html>