summaryrefslogtreecommitdiff
path: root/net/samba/files/adduser.sh
blob: 5120eb83c12692d55d07ec349553a612e59dcefc (plain)
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
#!/bin/sh
#
# $NetBSD: adduser.sh,v 1.2 2004/01/11 02:26:31 jlam Exp $
#
# This is an adduser script for NetBSD systems whose useradd(8) doesn't
# accept "$" in the username.  This script may be used for the
# "add machine script" in smb.conf.

awkprog="@AWK@"
catprog="@CAT@"
mktempprog="@MKTEMP@"
pwdmkdbprog="@PWD_MKDB@"
rmprog="@RM@"

progname=adduser

usage() {
	echo "Usage: $progname [-c comment] [-d homedir] [-n] [-r low..high]"
	echo "		[-s shell] -g gid name"
	exit 1
}

isnum() {
	case "$1" in
	0[0-9]*|*[!0-9]*)
		return 1
		;;
	esac
	return 0
}

doit=""
gid=""
minuid=1000
maxuid=60000
comment=""
homedir=""
shell=/sbin/nologin

while getopts c:d:g:nr:s: flag; do
	case $flag in
	c)	comment="${OPTARG}" ;;
	d)	homedir="${OPTARG}" ;;
	g)	gid="${OPTARG}" ;;
	n)	doit=":" ;;
	r)	minuid=${OPTARG%%..*}; maxuid=${OPTARG##*..} ;;
	s)	shell="${OPTARG}" ;;
	esac
done
shift `expr $OPTIND - 1`

if [ $# -lt 1 ]; then
	usage
fi

name="$1"
if [ -z "$gid" ]; then
	echo "$progname: \`-g gid' is a required option" 1>&2
	exit 1
fi
if ! isnum "$gid"; then
	echo "$progname: \`$gid' is not a valid gid" 1>&2
	exit 1
fi
if ! isnum "$minuid"; then
	echo "$progname: \`$minuid' is not a valid minimum uid" 1>&2
	exit 1
fi
if ! isnum "$maxuid"; then
	echo "$progname: \`$maxuid' is not a valid maximum uid" 1>&2
	exit 1
fi
if [ $minuid -gt $maxuid ]; then
	echo "$progname: \`$minuid..$maxuid' is not a valid range" 1>&2
	exit 1
fi
case "$comment" in
*:*)
	echo "$progname: \`$comment' is not a valid comment" 1>&2
	exit 1
	;;
esac
case "$homedir" in
*:*)
	echo "$progname: \`$homedir' is not a valid home directory" 1>&2
	exit 1
	;;
%*)
	# This is an unsubstituted variable (probably %H in smbd).
	# Silently change this to a proper default.
	#
	homedir="/nonexistent"
	;;
esac
case "$shell" in
*:*)
	echo "$progname: \`$shell' is not a valid shell" 1>&2
	exit 1
	;;
esac
case "$name" in
*:*)
	echo "$progname: \`$name' is not a valid username" 1>&2
	exit 1
	;;
esac

ptmp=`$mktempprog -q /etc/ptmp`
case "$ptmp" in
/etc/ptmp)
	;;
*)
	echo "$progname: can't create /etc/ptmp" 1>&2
	exit 1
	;;
esac

if [ ! -f /etc/master.passwd ]; then
	echo "$progname: /etc/master.passwd not found" 1>&2
	$rmprog -f $ptmp
	exit 1
fi

$catprog /etc/master.passwd >> $ptmp

uid=`									\
	$awkprog -v minuid=$minuid -v maxuid=maxuid '			\
		BEGIN { FS = ":" }					\
		{ seen_uids[$3] = 1 }					\
		END {							\
			uid = minuid;					\
			while (uid <= maxuid) {				\
				if (uid in seen_uids) {			\
					uid++;				\
					continue;			\
				}					\
				print uid;				\
				exit;					\
			}						\
			print -1;					\
		}' $ptmp						\
`
if [ $uid -lt 0 ]; then
	echo "$progname: no uid can be allocated in $minuid..$maxuid" 1>&2
	$rmprog -f $ptmp
	exit 1
fi

case "${name}" in
*$)	: ${comment:=${name%%[$]*} samba machine account} ;;
*)	: ${comment:=${name} samba user} ;;
esac
entry="${name}:*:${uid}:${gid}::0:0:${comment}:${homedir}:${shell}"

echo "$entry" >> $ptmp

$doit $pwdmkdbprog -p -u "${name}" $ptmp
rc=$?

$rmprog -f $ptmp
exit $rc