File: pam_script

package info (click to toggle)
libpam-script 1.1.9-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 224 kB
  • sloc: ansic: 374; perl: 348; sh: 179; makefile: 76
file content (208 lines) | stat: -rwxr-xr-x 4,494 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#! /bin/sh

# COPYRIGHT AND LICENCE
# 
# AUTHOR: R K Owen, Ph.D., <rkowen@nersc.gov>
# of the National Energy Research Scientific Computing Center (NERSC),
# a Division of the Lawrence Berkeley National Laboratory (LBL),
# funded by the U.S. Department of Energy.
# 
# Copyright (C) 2008 The Regents of the University of California
# 
# This is free software; you can redistribute it
# and/or modify it under the terms of the GNU General
# Public License as published by the Free Software Foundation;
# version 2.0 of the License.

basedir=`dirname $0`
[ x"$basedir" = x"" ] && basedir="."
fname=`basename $0`
PAMSCRIPTDIR=${PAMSCRIPTDIR:-$basedir/pam-script.d}

goodperms () {
	local path="$1"
	stat_output=`/usr/bin/stat -c "%A:%u:%g" "$path"`
	if [ $? -ne 0 ]; then
		echo "$0: Could not stat path $path" 1>&2
		return 1
	fi

	owner=`echo $stat_output | /usr/bin/cut -d ':' -f 2`
	group=`echo $stat_output | /usr/bin/cut -d ':' -f 3`
	world_write_bit=${stat_output:8:1}

	if [ ${world_write_bit} != "-" -o "$owner" -ne 0 -o "$group" -ne 0 ]; then
		echo "$0: Unsafe permissions for path $path; Rejecting execution." 1>&2
		return 1
	fi
}

runscript () {
	script="$1"
	shift

	if [ ! -e "$script" ]; then
		return 0
	fi

	goodperms "$script" || return 1

	/bin/sh "$script" "$@"
	return $?
}

if [ x"$fname" != x"pam_script" ]; then
	# process files in pam-script.d/
	mtype=''
	PAM_SCRIPT_STATUS=0
	export PAM_SCRIPT_STATUS
	case $fname in
	*_auth)		mtype='_auth' ;;
	*_acct)		mtype='_acct' ;;
	*_passwd)	mtype='_passwd' ;;
	*_ses_open)	mtype='_ses_open' ;;
	*_ses_close)	mtype='_ses_close' ;;
	esac

	goodperms "$PAMSCRIPTDIR" || exit $PAM_SCRIPT_STATUS

	for script in $PAMSCRIPTDIR/*$mtype; do
		runscript "$script" $@ || PAM_SCRIPT_STATUS=1
		export PAM_SCRIPT_STATUS
	done
	exit $PAM_SCRIPT_STATUS
else
	# do administrative things
usage() {
	/bin/cat <<!

$fname - administrative and driver script for pam-script.d

usage: $fname [-h][-v][-x][{-s|-r} moduletype] base_pam_script
	-h			this helpful info
	-v			verbose
	-x			create pam_script_* links instead
	-s moduletype		Set the links to base_pam_scriptfile as
				a combination of auth,pass,ses_cl,ses_op
				or session for both ses_cl and ses_op
				or 'all' for all moduletypes ... use commas
				to separate the moduletypes
	-r moduletype		Remove the same as above
				If neither -s or -r is given then just
				show the links

	base_pam_script		which script to handle somehow

Set PAMSCRIPTDIR if you want links in a directory other than
$basedir/pam-script.d

!
}

rmlink () {
	# $1 operation remove or link
	# $2 module type auth,acct,passwd,ses_open,ses_close
	# $3 basepamscript
	# $4 xpamscript

	if [ x$1 = xlink ]; then
		(cd $PAMSCRIPTDIR;
			[ -e $4_$2 ] || [ -e $3 ] && \
			( [ $verbose = 1 ] && echo ln -s $3 $4_$2;
			ln -s $3 $4_$2))
	elif [ x$1 = xremove ]; then
		(cd $PAMSCRIPTDIR;
			[ -e $4_$2 ] && \
			( [ $verbose = 1 ] && echo rm -f $4_$2;
			rm -f $4_$2))
	fi
}

	TEMP=`getopt 'hvxs:r:' "$@"`
	eval set -- $TEMP
	verbose=0
	op='list'
	mtype=''
	xbase=''

	while [ $# -gt 0 ]; do
		case	"$1" in
		-h)	usage
			exit
			;;
		-v)	verbose=1
			;;
		-x)	xbase='pam_script'
			;;
		-s)	op='link'
			shift
			mtype="$1"
			;;
		-r)	op='remove'
			shift
			mtype="$1"
			;;
		--)	shift
			break
			;;
		-*)	echo invalid option "$1"
			usage
			exit
			;;
		*)	shift
			break
			;;
		esac
		shift
	done

	pamfile=$1
	if [ x"$pamfile" = x"" ]; then
		echo "Need to pass a pam script"
		usage
		exit
	fi
	[ x"$xbase" = x"" ] && xbase=$pamfile

	if [ $verbose = 1 ]; then
		echo PAMSCRIPTDIR=$PAMSCRIPTDIR
		echo base pam script=$pamfile
		echo link base=$xbase
	fi

	case $op in
	list)	[ $verbose = 1 ] && echo listing
		ls -l $PAMSCRIPTDIR/$pamfile* 2>/dev/null
		;;
	link|remove)
		[ $verbose = 1 ] && echo $op
		eval set -- `echo $mtype | sed 's/,/ /g'`
		while [ $# -gt 0 ]; do
			case "$1" in
			auth)		rmlink $op auth $pamfile $xbase
					;;
			acct)		rmlink $op acct $pamfile $xbase
					;;
			passwd)		rmlink $op passwd $pamfile $xbase
					;;
			ses_open)	rmlink $op ses_open $pamfile $xbase
					;;
			ses_close)	rmlink $op ses_close $pamfile $xbase
					;;
			session)	rmlink $op ses_open $pamfile $xbase
					rmlink $op ses_close $pamfile $xbase
					;;
			all)		rmlink $op auth $pamfile $xbase
					rmlink $op acct $pamfile $xbase
					rmlink $op passwd $pamfile $xbase
					rmlink $op ses_open $pamfile $xbase
					rmlink $op ses_close $pamfile $xbase
					;;
			esac
			shift
		done
		;;
	esac
fi

exit