| 1 |
#! /bin/sh |
| 2 |
|
| 3 |
# COPYRIGHT AND LICENCE |
| 4 |
# |
| 5 |
# AUTHOR: R K Owen, Ph.D., <[email protected]> |
| 6 |
# of the National Energy Research Scientific Computing Center (NERSC), |
| 7 |
# a Division of the Lawrence Berkeley National Laboratory (LBL), |
| 8 |
# funded by the U.S. Department of Energy. |
| 9 |
# |
| 10 |
# Copyright (C) 2008 The Regents of the University of California |
| 11 |
# |
| 12 |
# This is free software; you can redistribute it |
| 13 |
# and/or modify it under the terms of the GNU General |
| 14 |
# Public License as published by the Free Software Foundation; |
| 15 |
# version 2.0 of the License. |
| 16 |
|
| 17 |
basedir=`dirname $0` |
| 18 |
[ x"$basedir" = x"" ] && basedir="." |
| 19 |
fname=`basename $0` |
| 20 |
PAMSCRIPTDIR=${PAMSCRIPTDIR:-$basedir/pam-script.d} |
| 21 |
|
| 22 |
goodperms () { |
| 23 |
local path="$1" |
| 24 |
stat_output=`/usr/bin/stat -c "%A:%u:%g" "$path"` |
| 25 |
if [ $? -ne 0 ]; then |
| 26 |
echo "$0: Could not stat path $path" 1>&2 |
| 27 |
return 1 |
| 28 |
fi |
| 29 |
|
| 30 |
owner=`echo $stat_output | /usr/bin/cut -d ':' -f 2` |
| 31 |
group=`echo $stat_output | /usr/bin/cut -d ':' -f 3` |
| 32 |
world_write_bit=${stat_output:8:1} |
| 33 |
|
| 34 |
if [ ${world_write_bit} != "-" -o "$owner" -ne 0 -o "$group" -ne 0 ]; then |
| 35 |
echo "$0: Unsafe permissions for path $path; Rejecting execution." 1>&2 |
| 36 |
return 1 |
| 37 |
fi |
| 38 |
} |
| 39 |
|
| 40 |
runscript () { |
| 41 |
script="$1" |
| 42 |
shift |
| 43 |
|
| 44 |
if [ ! -e "$script" ]; then |
| 45 |
return 0 |
| 46 |
fi |
| 47 |
|
| 48 |
goodperms "$script" || return 1 |
| 49 |
|
| 50 |
/bin/sh "$script" "$@" |
| 51 |
return $? |
| 52 |
} |
| 53 |
|
| 54 |
if [ x"$fname" != x"pam_script" ]; then |
| 55 |
# process files in pam-script.d/ |
| 56 |
mtype='' |
| 57 |
PAM_SCRIPT_STATUS=0 |
| 58 |
export PAM_SCRIPT_STATUS |
| 59 |
case $fname in |
| 60 |
*_auth) mtype='_auth' ;; |
| 61 |
*_acct) mtype='_acct' ;; |
| 62 |
*_passwd) mtype='_passwd' ;; |
| 63 |
*_ses_open) mtype='_ses_open' ;; |
| 64 |
*_ses_close) mtype='_ses_close' ;; |
| 65 |
esac |
| 66 |
|
| 67 |
goodperms "$PAMSCRIPTDIR" || exit $PAM_SCRIPT_STATUS |
| 68 |
|
| 69 |
for script in $PAMSCRIPTDIR/*$mtype; do |
| 70 |
runscript "$script" $@ || PAM_SCRIPT_STATUS=1 |
| 71 |
export PAM_SCRIPT_STATUS |
| 72 |
done |
| 73 |
exit $PAM_SCRIPT_STATUS |
| 74 |
else |
| 75 |
# do administrative things |
| 76 |
usage() { |
| 77 |
/bin/cat <<! |
| 78 |
|
| 79 |
$fname - administrative and driver script for pam-script.d |
| 80 |
|
| 81 |
usage: $fname [-h][-v][-x][{-s|-r} moduletype] base_pam_script |
| 82 |
-h this helpful info |
| 83 |
-v verbose |
| 84 |
-x create pam_script_* links instead |
| 85 |
-s moduletype Set the links to base_pam_scriptfile as |
| 86 |
a combination of auth,pass,ses_cl,ses_op |
| 87 |
or session for both ses_cl and ses_op |
| 88 |
or 'all' for all moduletypes ... use commas |
| 89 |
to separate the moduletypes |
| 90 |
-r moduletype Remove the same as above |
| 91 |
If neither -s or -r is given then just |
| 92 |
show the links |
| 93 |
|
| 94 |
base_pam_script which script to handle somehow |
| 95 |
|
| 96 |
Set PAMSCRIPTDIR if you want links in a directory other than |
| 97 |
$basedir/pam-script.d |
| 98 |
|
| 99 |
! |
| 100 |
} |
| 101 |
|
| 102 |
rmlink () { |
| 103 |
# $1 operation remove or link |
| 104 |
# $2 module type auth,acct,passwd,ses_open,ses_close |
| 105 |
# $3 basepamscript |
| 106 |
# $4 xpamscript |
| 107 |
|
| 108 |
if [ x$1 = xlink ]; then |
| 109 |
(cd $PAMSCRIPTDIR; |
| 110 |
[ -e $4_$2 ] || [ -e $3 ] && \ |
| 111 |
( [ $verbose = 1 ] && echo ln -s $3 $4_$2; |
| 112 |
ln -s $3 $4_$2)) |
| 113 |
elif [ x$1 = xremove ]; then |
| 114 |
(cd $PAMSCRIPTDIR; |
| 115 |
[ -e $4_$2 ] && \ |
| 116 |
( [ $verbose = 1 ] && echo rm -f $4_$2; |
| 117 |
rm -f $4_$2)) |
| 118 |
fi |
| 119 |
} |
| 120 |
|
| 121 |
TEMP=`getopt 'hvxs:r:' "$@"` |
| 122 |
eval set -- $TEMP |
| 123 |
verbose=0 |
| 124 |
op='list' |
| 125 |
mtype='' |
| 126 |
xbase='' |
| 127 |
|
| 128 |
while [ $# -gt 0 ]; do |
| 129 |
case "$1" in |
| 130 |
-h) usage |
| 131 |
exit |
| 132 |
;; |
| 133 |
-v) verbose=1 |
| 134 |
;; |
| 135 |
-x) xbase='pam_script' |
| 136 |
;; |
| 137 |
-s) op='link' |
| 138 |
shift |
| 139 |
mtype="$1" |
| 140 |
;; |
| 141 |
-r) op='remove' |
| 142 |
shift |
| 143 |
mtype="$1" |
| 144 |
;; |
| 145 |
--) shift |
| 146 |
break |
| 147 |
;; |
| 148 |
-*) echo invalid option "$1" |
| 149 |
usage |
| 150 |
exit |
| 151 |
;; |
| 152 |
*) shift |
| 153 |
break |
| 154 |
;; |
| 155 |
esac |
| 156 |
shift |
| 157 |
done |
| 158 |
|
| 159 |
pamfile=$1 |
| 160 |
if [ x"$pamfile" = x"" ]; then |
| 161 |
echo "Need to pass a pam script" |
| 162 |
usage |
| 163 |
exit |
| 164 |
fi |
| 165 |
[ x"$xbase" = x"" ] && xbase=$pamfile |
| 166 |
|
| 167 |
if [ $verbose = 1 ]; then |
| 168 |
echo PAMSCRIPTDIR=$PAMSCRIPTDIR |
| 169 |
echo base pam script=$pamfile |
| 170 |
echo link base=$xbase |
| 171 |
fi |
| 172 |
|
| 173 |
case $op in |
| 174 |
list) [ $verbose = 1 ] && echo listing |
| 175 |
ls -l $PAMSCRIPTDIR/$pamfile* 2>/dev/null |
| 176 |
;; |
| 177 |
link|remove) |
| 178 |
[ $verbose = 1 ] && echo $op |
| 179 |
eval set -- `echo $mtype | sed 's/,/ /g'` |
| 180 |
while [ $# -gt 0 ]; do |
| 181 |
case "$1" in |
| 182 |
auth) rmlink $op auth $pamfile $xbase |
| 183 |
;; |
| 184 |
acct) rmlink $op acct $pamfile $xbase |
| 185 |
;; |
| 186 |
passwd) rmlink $op passwd $pamfile $xbase |
| 187 |
;; |
| 188 |
ses_open) rmlink $op ses_open $pamfile $xbase |
| 189 |
;; |
| 190 |
ses_close) rmlink $op ses_close $pamfile $xbase |
| 191 |
;; |
| 192 |
session) rmlink $op ses_open $pamfile $xbase |
| 193 |
rmlink $op ses_close $pamfile $xbase |
| 194 |
;; |
| 195 |
all) rmlink $op auth $pamfile $xbase |
| 196 |
rmlink $op acct $pamfile $xbase |
| 197 |
rmlink $op passwd $pamfile $xbase |
| 198 |
rmlink $op ses_open $pamfile $xbase |
| 199 |
rmlink $op ses_close $pamfile $xbase |
| 200 |
;; |
| 201 |
esac |
| 202 |
shift |
| 203 |
done |
| 204 |
;; |
| 205 |
esac |
| 206 |
fi |
| 207 |
|
| 208 |
exit |