add some boilerplate scripts
[hband-tools.git] / pam / pam_lastreboot
blob745592ba1c861b05a905b1b4ff76b898e4ecdc44
1 #!/bin/sh
3 PAM_SUCCESS=0
4 PAM_SYSTEM_ERR=4
5 PAM_PERM_DENIED=6
6 PAM_AUTH_ERR=7
7 PAM_IGNORE=25
9 #PAM_RHOST=
10 #PAM_SERVICE=login
11 #PAM_TTY=/dev/pts/9
12 #PAM_SERVICE=sshd
13 #PAM_TTY=ssh
14 #PAM_TYPE=open_session
15 #PAM_TYPE=close_session
16 #PAM_USER=joedoe
18 last_reboot()
20 last_wtmp_entry 7 reboot
22 last_wtmp_login()
24 last_wtmp_entry 1 "$1"
26 last_wtmp_entry()
28 local wtmp_field=$1
29 local wtmp_match=$2
31 for wtmp in $(echo /var/log/wtmp* | tr " " "\n" | sort -t. -k2n)
33 timestamp=$(
34 LD_PRELOAD=/usr/lib/yazzy-preload/openhelper.so \
35 OPENHELPER_FNMATCH='/var/log/wtmp.*.*' \
36 OPENHELPER_COMMAND='case "$OPENHELPER_FILE" in
37 *.xz) exec unxz -c "$OPENHELPER_FILE";;
38 *.gz) exec gunzip -c "$OPENHELPER_FILE";;
39 *.bz2) exec bunzip2 -c "$OPENHELPER_FILE";;
40 *) exit 95;;
41 esac' \
42 lastcsv -f "$wtmp" -n 1 "$wtmp_match" |\
43 wtmp_match=$wtmp_match \
44 awk -F';' '$'$wtmp_field'==ENVIRON["wtmp_match"]{print $5}'
46 if [ -n "$timestamp" ]
47 then
48 echo "$timestamp"
49 break
51 done
54 lastlogin=`last_wtmp_login "$PAM_USER"`
55 lastreboot=`last_reboot`
57 if [ -n "$lastlogin" ]
58 then
59 lastlogin=$lastlogin perl -MTime::Duration -e 'printf "Last login: %s\n", ago(time - $ENV{"lastlogin"});'
62 if expr "$*" : '.*\<force\>' >/dev/null || [ -z "$lastlogin" ] || [ "$lastlogin" -le "$lastreboot" ] 2>/dev/null
63 then
65 tput bold
66 tput setaf 3
67 echo "First login since last reboot: $(date -d @"$lastreboot")"
68 tput sgr0
69 ) 2>/dev/null
72 exit $PAM_SUCCESS