view previous-work/more_control_helpers/sbin/add_package_user @ 4:fe56b4698cf8

add install-init, and refine pkg.pl.
author Robert McIntyre <rlm@mit.edu>
date Tue, 08 Jan 2013 15:41:29 +0000
parents d6bef198ae71
children
line wrap: on
line source
1 #!/bin/bash
2 # Copyright (c) 2004 Matthias S. Benkmann <article AT winterdrache DOT de>
3 # You may do everything with this code except misrepresent its origin.
4 # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
6 #Package user home directories will be located under this directory
7 homebase=/usr/src
9 #Contents of following directory are copied into home directory when creating
10 #a new package user (existing files will not be overwritten)
11 skel=/etc/pkgusr/skel-package
13 if [ $# -lt 7 ]; then
14 echo 1>&2 'USAGE: '
15 echo 1>&2 'add_package_user <description> <name> <minuid> <maxuid> \'
16 echo 1>&2 ' <group> <mingid> <maxgid> [-d <home>]'
17 echo 1>&2
18 echo 1>&2 'If a user account called <name> exists, a message will be printed and'
19 echo 1>&2 'everything will be left as-is. If a user account called <name> does not'
20 echo 1>&2 'exist, one will be created.'
21 echo 1>&2 'The account'"'"'s primary group will be <group> and the /etc/passwd'
22 echo 1>&2 'description field will be set to <description>. If a group called <group>'
23 echo 1>&2 'does not already exist, one will be created.'
24 echo 1>&2 'The new account will get the "install" group as a supplementary group. If'
25 echo 1>&2 'a group named "install" does not exist, one will be created.'
26 echo 1>&2
27 echo 1>&2 '<description> needs to be a valid string for the /etc/passwd description'
28 echo 1>&2 ' field. This means, among other things, that it must not contain ":".'
29 echo 1>&2 ' Don'"'"'t forget to properly quote <description> if it contains spaces or'
30 echo 1>&2 ' other characters interpreted by the shell!'
31 echo 1>&2
32 echo 1>&2 '<minuid>(incl.) and <maxuid>(excl.) determine the numeric range from which'
33 echo 1>&2 ' the new account'"'"'s UID will be picked in the following way:'
34 echo 1>&2
35 echo 1>&2 ' 1. If the range contains no unused UID => Exit with error.'
36 echo 1>&2 ' 2. If <maxuid>-1 is still unused, find the greatest UID from the range'
37 echo 1>&2 ' that is used and pick the number after that.'
38 echo 1>&2 ' 3. If <maxuid>-1 is in use, pick the first unused number from the range.'
39 echo 1>&2
40 echo 1>&2 '<mingid>(incl.) and <maxgid>(excl.) determine the numeric range from which'
41 echo 1>&2 ' to pick the GID for group <group> and/or group "install", if it needs to be'
42 echo 1>&2 ' created. The process for picking the GID is the same as that for the UID.'
43 echo 1>&2 ''
44 echo 1>&2 '<home> specifies the new user'"'"'s home directory. If it is not provided,'
45 echo 1>&2 ' it will default to '"$homebase/<name> ."
46 echo 1>&2 ' If the home directory does not exist yet it will be created, otherwise'
47 echo 1>&2 ' the existing directory will be recursively chown'"'"'ed to the new user.'
48 echo 1>&2 ' The home directory will be populated with a copy of the contents of'
49 echo 1>&2 " $skel, but pre-existing files in the home directory"
50 echo 1>&2 ' will not be overwritten. Note that symlinks will be copied as symlinks!'
51 echo 1>&2 ''
52 exit 1
53 fi
55 grpfile=/etc/group
56 passwd=/etc/passwd
60 description=$1
61 name=$2
62 minuid=$3
63 maxuid=$4
64 gname=$5
65 mingid=$6
66 maxgid=$7
67 home=$homebase/$name
69 set -- "$@" _eNd_OF_lisT_
70 while [ "$1" != "_eNd_OF_lisT_" ]; do
71 case "$1" in
72 -d) shift 1
73 if [ "$1" = "_eNd_OF_lisT_" ]; then
74 echo 1>&2 "-d directory name missing!"
75 exit 1
76 fi
77 home="$1"
78 shift 1
79 ;;
80 *) temp="$1"
81 shift 1
82 set -- "$@" "$temp"
83 ;;
84 esac
85 done
86 shift 1 #throw away _eNd_OF_lisT_
88 if [ $UID -ne 0 ]; then echo Please run this script as root. ; exit 1; fi
90 #test if user already exists
91 grep "^$name:.*" $passwd
92 if [ $? -eq 0 ]; then
93 echo 'Package user does already exist! Do su '$name' to do maintenance work.'
94 exit 1
95 fi
97 #test if minuid, maxuid, mingid and maxgid are integers, otherwise error
98 error=0
99 expr ${minuid} + 1 2>/dev/null 1>&2 || error=1
100 expr ${maxuid} + 1 2>/dev/null 1>&2 || error=1
101 expr ${mingid} + 1 2>/dev/null 1>&2 || error=1
102 expr ${maxgid} + 1 2>/dev/null 1>&2 || error=1
104 if [ $error -eq 1 ]; then
105 echo Error: Illegal numeric value!
106 exit 1
107 fi
109 if [ $minuid -ge $maxuid ]; then
110 echo 'Error: minuid must be less than maxuid !'
111 exit 1
112 fi
114 if [ $mingid -ge $maxgid ]; then
115 echo 'Error: mingid must be less than maxgid !'
116 exit 1
117 fi
120 uidlist=`cut -d : -f 3 $passwd | sort -n`
122 #find last used UID within range
123 u=0
124 for i in $uidlist
125 do
126 if [ $i -ge $maxuid ]; then break; fi
127 if [ $i -ge $minuid ]; then u=$i; fi
128 done
130 #if no UID from the range is used, pick the first, otherwise pick the one
131 #immediately following the last UID in use.
132 if [ $u -eq 0 ]; then u=$minuid; else u=`expr $u + 1`; fi
134 #if the UID determined above is >= maxuid (i.e. illegal)
135 #then we look for the first unused uid in the range.
136 if [ $u -ge $maxuid ]; then
137 u=$minuid
138 for i in $uidlist
139 do
140 if [ $u -eq $i ]; then u=`expr $u + 1` ; fi
141 if [ $i -ge $maxuid ]; then break; fi
142 done
144 if [ $u -ge $maxuid ]; then
145 echo Error: UID range is full!
146 exit 1
147 fi
148 fi
150 echo Will create user $name with uid: $u
152 unset uidlist
154 #############################################################################
155 # group
156 #############################################################################
158 #execute the following for gname and "install" to get gids for those 2 groups
160 g=0
161 creategroup=0
162 for group in install $gname
163 do
164 oldg=$g #save gid from previous run
165 createinstall=$creategroup
166 creategroup=0
168 #test if group already exists and extract gid if so
169 g=`grep ^${group}:.\* $grpfile | cut -d : -f 3 -`
171 #if group does not exist, then check range for a free gid
172 if [ z$g = z ]; then
173 creategroup=1
175 gidlist=`cut -d : -f 3 $grpfile | sort -n`
177 #find last used GID within range
178 g=0
179 for i in $gidlist
180 do
181 if [ $i -ge $maxgid ]; then break; fi
182 if [ $i -ge $mingid ]; then g=$i; fi
183 done
185 #if no GID from the range is used, pick the first, otherwise pick the one
186 #immediately following the last GID in use.
187 if [ $g -eq 0 ]; then g=$mingid; else g=`expr $g + 1`; fi
189 #don't reuse gid from previous run
190 if [ $g -eq $oldg ]; then g=`expr $g + 1`; fi
192 #if the GID determined above is >= maxgid (i.e. illegal)
193 #then we look for the first unused gid in the range.
194 if [ $g -ge $maxgid ]; then
195 g=$mingid
196 for i in $gidlist
197 do
198 if [ $g -eq $i ]; then g=`expr $g + 1` ; fi
199 if [ $g -eq $oldg ]; then g=`expr $g + 1` ; fi
200 if [ $i -ge $maxgid ]; then break; fi
201 done
203 if [ $g -ge $maxgid ]; then
204 echo Error: GID range is full!
205 exit 1
206 fi
207 fi
208 fi
209 done
211 unset gidlist
213 if [ $createinstall -eq 1 ]; then
214 echo Creating group install with gid $oldg
215 groupadd -g $oldg install || exit 1
216 else
217 echo Group install has gid $oldg
218 fi
219 if [ $creategroup -eq 1 ]; then
220 echo Creating group $gname with gid $g
221 groupadd -g $g $gname || exit 1
222 else
223 echo Group $gname has gid $g
224 fi
228 useradd -c "${description}" -d ${home} -g ${gname} -G install \
229 -s /bin/bash -u ${u} ${name} || exit 1
231 mkdir -p $home || exit 1
233 yes n|cp -ai -R ${skel}/{[^.],.[^.],..?}* ${home} 2>/dev/null >/dev/null
235 cd ${home}
236 chown --recursive ${u}:${g} .
239 exit 0