| 1 |
#! /usr/bin/perl |
| 2 |
# |
| 3 |
# May need to change the path for perl above |
| 4 |
# |
| 5 |
# Tally example |
| 6 |
# see the pam-tally module for details |
| 7 |
# |
| 8 |
my ($FILE,$deny,$lock_time, $unlock_time, $magic_root, $even_deny_root_account, |
| 9 |
$retval) |
| 10 |
= ("/tmp/pam-script-tally", 5, 5, 0, 0, 0, 0); |
| 11 |
my ($fmt) = ("%-12s %3d %s %s\n"); |
| 12 |
|
| 13 |
our ($opt_h,$opt_L,$opt_t,$opt_l,$opt_r); |
| 14 |
|
| 15 |
use Getopt::Std; |
| 16 |
getopts('hLt:l:r:'); |
| 17 |
|
| 18 |
$| = 1; # flush output |
| 19 |
$0 =~ s{^.*/}{}; # strip to basename |
| 20 |
|
| 21 |
&usage() if defined $opt_h; |
| 22 |
|
| 23 |
sub usage { |
| 24 |
print <<EOF |
| 25 |
$0 - pam-script example of pam-tally, which tracks |
| 26 |
the number of failed logins and will lock out the person |
| 27 |
if exceeds some threshold. |
| 28 |
|
| 29 |
usage: $0 [-h][-L][[-t|-l|-r] user] \ |
| 30 |
[deny=N][lock_time=S][unlock_time=S][magic_root][even_deny_root_account] |
| 31 |
where -h this usage info |
| 32 |
-L list all the users |
| 33 |
-t user tally entry for user |
| 34 |
-l user list entry for user |
| 35 |
-r user reset entry for user |
| 36 |
these only apply if invoked from pam-script |
| 37 |
deny=N number of failures before locking out |
| 38 |
unlock_time=S number of seconds to deny access after a failure |
| 39 |
magic_root don't tally the counter for root |
| 40 |
even_deny_root_account |
| 41 |
root is normally not locked out unless this is given |
| 42 |
|
| 43 |
EOF |
| 44 |
; |
| 45 |
print q{version: $Id: tally,v 1.5 2007/09/07 15:24:45 rkowen Exp $},"\n\n"; |
| 46 |
exit |
| 47 |
} |
| 48 |
|
| 49 |
use Fcntl; |
| 50 |
use SDBM_File; |
| 51 |
|
| 52 |
umask 000; |
| 53 |
tie (%tally, 'SDBM_File',$FILE, O_CREAT|O_RDWR, 0666) |
| 54 |
or die "Could not tie SDBM file '$FILE': $!; aborting"; |
| 55 |
|
| 56 |
if (defined $opt_L) { |
| 57 |
# show all users |
| 58 |
foreach my $k (sort {$a cmp $b} keys %tally) { |
| 59 |
my ($cnt, $sess, $auth) = &dget($tally{$k}); |
| 60 |
printf $fmt, $k, $cnt, &datetime($sess), &datetime($auth); |
| 61 |
} |
| 62 |
} elsif (defined $opt_t || defined $opt_l || defined $opt_r) { |
| 63 |
my ($ocnt,$user) = (0,undef); |
| 64 |
do {$ocnt++; $user = $opt_t;} if defined $opt_t; |
| 65 |
do {$ocnt++; $user = $opt_l;} if defined $opt_l; |
| 66 |
do {$ocnt++; $user = $opt_r;} if defined $opt_r; |
| 67 |
die "Only one -t,-l, or -r option should be given" |
| 68 |
if ($ocnt > 1); |
| 69 |
$tally{$user} = &dset(0,0,0) if ! exists $tally{$user}; |
| 70 |
|
| 71 |
my ($cnt,$sess,$auth) = &dget($tally{$user}); |
| 72 |
$cnt=0 if ! defined $cnt; |
| 73 |
$sess=0 if ! defined $sess; |
| 74 |
$auth=0 if ! defined $auth; |
| 75 |
if (defined $opt_t) { |
| 76 |
$tally{$user} = &dset($cnt+1, time, time); |
| 77 |
} elsif (defined $opt_r) { |
| 78 |
$tally{$user} = &dset(0, $sess, $auth); |
| 79 |
} elsif (defined $opt_l) { |
| 80 |
printf $fmt,$user,$cnt,&datetime($sess),&datetime($auth); |
| 81 |
} |
| 82 |
} elsif ($0 =~ /^pam_script_auth$/ || $0 =~ /^pam_script_acct$/) { |
| 83 |
foreach my $arg (@ARGV) { |
| 84 |
$deny = $1 if ($arg =~ /^deny=(\d+)/); |
| 85 |
$lock_time = $1 if ($arg =~ /^lock_time=(\d+)/); |
| 86 |
$unlock_time = $1 if ($arg =~ /^unlock_time=(\d+)/); |
| 87 |
$magic_root = 1 if ($arg =~ /^magic_root$/); |
| 88 |
$even_deny_root_account = 1 |
| 89 |
if ($arg =~ /^even_deny_root_account$/); |
| 90 |
} |
| 91 |
|
| 92 |
# grab some PAM env.vars. |
| 93 |
my $PAM_USER = $ENV{"PAM_USER"}; |
| 94 |
my ($cnt, $sess, $auth) = &dget($tally{$PAM_USER}); |
| 95 |
my $now = time; |
| 96 |
|
| 97 |
if ($0 =~ /^pam_script_auth$/) { |
| 98 |
# tally count |
| 99 |
$retval = 1 if $cnt && ($now < $auth + $lock_time); |
| 100 |
$cnt = 0 if ($cnt > $deny) && $unlock_time |
| 101 |
&& ($now > $auth + $unlock_time); |
| 102 |
$cnt++; |
| 103 |
$cnt-- if $magic_root && $PAM_USER =~ /^root$/; |
| 104 |
$retval = 1 if $cnt >= $deny; |
| 105 |
$retval = 0 if ! $even_deny_root_account |
| 106 |
&& $PAM_USER =~ /^root$/; |
| 107 |
$auth = $now; |
| 108 |
|
| 109 |
} elsif ($0 =~ /^pam_script_acct$/) { |
| 110 |
$sess = $now; |
| 111 |
$auth = $now; |
| 112 |
$cnt = 0; |
| 113 |
} |
| 114 |
$tally{$PAM_USER} = &dset($cnt,$sess,$auth); |
| 115 |
} |
| 116 |
|
| 117 |
untie %tally; |
| 118 |
exit $retval; |
| 119 |
|
| 120 |
sub datetime { |
| 121 |
my $time = shift; |
| 122 |
return "unset" if !$time; |
| 123 |
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) |
| 124 |
= localtime($time); |
| 125 |
return sprintf "%04d%02d%02d%02d%02d%02d", $year + 1900, $mon + 1, |
| 126 |
$mday, $hour, $min, $sec; |
| 127 |
} |
| 128 |
|
| 129 |
sub dset { |
| 130 |
my ($cnt,$sess,$auth) = @_; |
| 131 |
return pack "ill", $cnt, $sess, $auth; |
| 132 |
} |
| 133 |
|
| 134 |
sub dget { |
| 135 |
my $data = shift; |
| 136 |
return unpack "ill", $data; |
| 137 |
} |
| 138 |
|
| 139 |
1; |