-
Notifications
You must be signed in to change notification settings - Fork 4
/
mkchroot-sshkeydirs.sh
executable file
·85 lines (75 loc) · 1.55 KB
/
mkchroot-sshkeydirs.sh
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
#!/bin/sh
#
# mkchroot-keydirs.sh - build rssh compatible /etc/passwd
#
progname=`basename $0`
Usage() {
echo "$progname [ CHROOTDIR ]"
exit 1
}
if [ $# -gt 1 ]; then
Usage
elif [ -n "$1" ]; then
CHROOTDIR=$1
else
CHROOTDIR=/chroot/
fi
# Sanitize CHROOTDIR to prevent confusion
CHROOTDIR="`readlink --canonicalize "$CHROOTDIR"`"
case $CHROOTDIR in
'')
echo "Error: blank CHROOTDIR" >&2
exit 1
;;
/|*:*|*' '*)
echo "Error: unallowed CHROOTDIR \"$CHROOTDIR\"" >&2
exit 1
;;
*:*)
esac
if [ ! -d $CHROOTDIR ]; then
echo "Error: non-existent \"$CHROOTDIR\"" >&2
exit 1
fi
function mksshdir() {
sshdir="$1"
uid="$2"
gid="$3"
if [ -e "$sshdir" ]; then
if [ ! -d "$sshdir" ]; then
echo "Error: $sshdir exists, but is not a directory, exiting" >&2
exit 1
fi
echo " $sshdir exists already"
#return 0
else
parentdir="`dirname $sshdir`"
if [ ! -d "$parentdir" ]; then
mksshdir "$parentdir" $uid $gid
else
echo " Creating directory: $sshdir"
install -d -o $uid -g $gid -m 0750 "$sshdir"
fi
fi
}
getent passwd | \
grep ":$CHROOTDIR" | \
while IFS=: read username passwd uid gid comment homedir shell debris; do
if [ -n "$debris" ]; then
echo "Error: user $username has invalid getent content, exiting" >&2
exit 1
fi
#echo CHROOTDIR: $CHROOTDIR
#echo homedir: $homedir
case $homedir in
"$CHROOTDIR" )
;;
${CHROOTDIR}/* )
;;
*)
continue
;;
esac
# Always set "root" as owner
mksshdir "$homedir" root "$gid"
done