Mercurial > pkg
comparison previous-work/more_control_helpers/sbin/add_package_user @ 1:d6bef198ae71
add work by Matthias S. Benkmann which is the inspiration for this project.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 08 Jan 2013 11:45:01 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:0b7a589f6e9a | 1:d6bef198ae71 |
---|---|
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! | |
5 | |
6 #Package user home directories will be located under this directory | |
7 homebase=/usr/src | |
8 | |
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 | |
12 | |
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 | |
54 | |
55 grpfile=/etc/group | |
56 passwd=/etc/passwd | |
57 | |
58 | |
59 | |
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 | |
68 | |
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_ | |
87 | |
88 if [ $UID -ne 0 ]; then echo Please run this script as root. ; exit 1; fi | |
89 | |
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 | |
96 | |
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 | |
103 | |
104 if [ $error -eq 1 ]; then | |
105 echo Error: Illegal numeric value! | |
106 exit 1 | |
107 fi | |
108 | |
109 if [ $minuid -ge $maxuid ]; then | |
110 echo 'Error: minuid must be less than maxuid !' | |
111 exit 1 | |
112 fi | |
113 | |
114 if [ $mingid -ge $maxgid ]; then | |
115 echo 'Error: mingid must be less than maxgid !' | |
116 exit 1 | |
117 fi | |
118 | |
119 | |
120 uidlist=`cut -d : -f 3 $passwd | sort -n` | |
121 | |
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 | |
129 | |
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 | |
133 | |
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 | |
143 | |
144 if [ $u -ge $maxuid ]; then | |
145 echo Error: UID range is full! | |
146 exit 1 | |
147 fi | |
148 fi | |
149 | |
150 echo Will create user $name with uid: $u | |
151 | |
152 unset uidlist | |
153 | |
154 ############################################################################# | |
155 # group | |
156 ############################################################################# | |
157 | |
158 #execute the following for gname and "install" to get gids for those 2 groups | |
159 | |
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 | |
167 | |
168 #test if group already exists and extract gid if so | |
169 g=`grep ^${group}:.\* $grpfile | cut -d : -f 3 -` | |
170 | |
171 #if group does not exist, then check range for a free gid | |
172 if [ z$g = z ]; then | |
173 creategroup=1 | |
174 | |
175 gidlist=`cut -d : -f 3 $grpfile | sort -n` | |
176 | |
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 | |
184 | |
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 | |
188 | |
189 #don't reuse gid from previous run | |
190 if [ $g -eq $oldg ]; then g=`expr $g + 1`; fi | |
191 | |
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 | |
202 | |
203 if [ $g -ge $maxgid ]; then | |
204 echo Error: GID range is full! | |
205 exit 1 | |
206 fi | |
207 fi | |
208 fi | |
209 done | |
210 | |
211 unset gidlist | |
212 | |
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 | |
225 | |
226 | |
227 | |
228 useradd -c "${description}" -d ${home} -g ${gname} -G install \ | |
229 -s /bin/bash -u ${u} ${name} || exit 1 | |
230 | |
231 mkdir -p $home || exit 1 | |
232 | |
233 yes n|cp -ai -R ${skel}/{[^.],.[^.],..?}* ${home} 2>/dev/null >/dev/null | |
234 | |
235 cd ${home} | |
236 chown --recursive ${u}:${g} . | |
237 | |
238 | |
239 exit 0 |