/[gentoo-x86]/eclass/user.eclass
Gentoo

Contents of /eclass/user.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (show annotations) (download)
Thu Nov 3 15:29:39 2011 UTC (2 years, 10 months ago) by vapier
Branch: MAIN
Changes since 1.8: +43 -79 lines
clean up enew{user,group} and drop the [extra] argument as no one is using it

1 # Copyright 1999-2011 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-x86/eclass/user.eclass,v 1.8 2011/11/03 00:59:16 vapier Exp $
4
5 # @ECLASS: user.eclass
6 # @MAINTAINER:
7 # base-system@gentoo.org (Linux)
8 # Joe Jezak <josejx@gmail.com> (OS X)
9 # usata@gentoo.org (OS X)
10 # Aaron Walker <ka0ttic@gentoo.org> (FreeBSD)
11 # @BLURB: user management in ebuilds
12 # @DESCRIPTION:
13 # The user eclass contains a suite of functions that allow ebuilds
14 # to quickly make sure users in the installed system are sane.
15
16 # @FUNCTION: _assert_pkg_ebuild_phase
17 # @INTERNAL
18 # @USAGE: <calling func name>
19 _assert_pkg_ebuild_phase() {
20 case ${EBUILD_PHASE} in
21 unpack|prepare|configure|compile|test|install)
22 eerror "'$1()' called from '${EBUILD_PHASE}()' which is not a pkg_* function."
23 eerror "Package fails at QA and at life. Please file a bug."
24 die "Bad package! $1 is only for use in pkg_* functions!"
25 esac
26 }
27
28 # @FUNCTION: egetent
29 # @USAGE: <database> <key>
30 # @DESCRIPTION:
31 # Small wrapper for getent (Linux), nidump (< Mac OS X 10.5),
32 # dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup().
33 #
34 # Supported databases: group passwd
35 egetent() {
36 local db=$1 key=$2
37
38 [[ $# -ge 3 ]] && die "usage: egetent <database> <key>"
39
40 case ${db} in
41 passwd|group) ;;
42 *) die "sorry, database '${db}' not yet supported; file a bug" ;;
43 esac
44
45 case ${CHOST} in
46 *-darwin[678])
47 case ${key} in
48 *[!0-9]*) # Non numeric
49 nidump ${db} . | awk -F: "(\$1 ~ /^${key}\$/) {print;exit;}"
50 ;;
51 *) # Numeric
52 nidump ${db} . | awk -F: "(\$3 == ${key}) {print;exit;}"
53 ;;
54 esac
55 ;;
56 *-darwin*)
57 local mykey
58 case ${db} in
59 passwd) db="Users" mykey="UniqueID" ;;
60 group) db="Groups" mykey="PrimaryGroupID" ;;
61 esac
62
63 case ${key} in
64 *[!0-9]*) # Non numeric
65 dscl . -read /${db}/${key} 2>/dev/null |grep RecordName
66 ;;
67 *) # Numeric
68 dscl . -search /${db} ${mykey} ${key} 2>/dev/null
69 ;;
70 esac
71 ;;
72 *-freebsd*|*-dragonfly*)
73 case ${db} in
74 passwd) db="user" ;;
75 *) ;;
76 esac
77
78 # lookup by uid/gid
79 local opts
80 if [[ ${key} == [[:digit:]]* ]] ; then
81 [[ ${action} == "user" ]] && opts="-u" || opts="-g"
82 fi
83
84 pw show ${action} ${opts} "${key}" -q
85 ;;
86 *-netbsd*|*-openbsd*)
87 grep "${key}:\*:" /etc/${db}
88 ;;
89 *)
90 # ignore output if nscd doesn't exist, or we're not running as root
91 nscd -i "${db}" 2>/dev/null
92 getent "${db}" "${key}"
93 ;;
94 esac
95 }
96
97 # @FUNCTION: enewuser
98 # @USAGE: <user> [uid] [shell] [homedir] [groups]
99 # @DESCRIPTION:
100 # Same as enewgroup, you are not required to understand how to properly add
101 # a user to the system. The only required parameter is the username.
102 # Default uid is (pass -1 for this) next available, default shell is
103 # /bin/false, default homedir is /dev/null, and there are no default groups.
104 enewuser() {
105 _assert_pkg_ebuild_phase enewuser
106
107 # get the username
108 local euser=$1; shift
109 if [[ -z ${euser} ]] ; then
110 eerror "No username specified !"
111 die "Cannot call enewuser without a username"
112 fi
113
114 # lets see if the username already exists
115 if [[ -n $(egetent passwd "${euser}") ]] ; then
116 return 0
117 fi
118 einfo "Adding user '${euser}' to your system ..."
119
120 # options to pass to useradd
121 local opts=
122
123 # handle uid
124 local euid=$1; shift
125 if [[ -n ${euid} && ${euid} != -1 ]] ; then
126 if [[ ${euid} -gt 0 ]] ; then
127 if [[ -n $(egetent passwd ${euid}) ]] ; then
128 euid="next"
129 fi
130 else
131 eerror "Userid given but is not greater than 0 !"
132 die "${euid} is not a valid UID"
133 fi
134 else
135 euid="next"
136 fi
137 if [[ ${euid} == "next" ]] ; then
138 for ((euid = 101; euid <= 999; euid++)); do
139 [[ -z $(egetent passwd ${euid}) ]] && break
140 done
141 fi
142 opts+=" -u ${euid}"
143 einfo " - Userid: ${euid}"
144
145 # handle shell
146 local eshell=$1; shift
147 if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then
148 if [[ ! -e ${ROOT}${eshell} ]] ; then
149 eerror "A shell was specified but it does not exist !"
150 die "${eshell} does not exist in ${ROOT}"
151 fi
152 if [[ ${eshell} == */false || ${eshell} == */nologin ]] ; then
153 eerror "Do not specify ${eshell} yourself, use -1"
154 die "Pass '-1' as the shell parameter"
155 fi
156 else
157 for shell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do
158 [[ -x ${ROOT}${shell} ]] && break
159 done
160
161 if [[ ${shell} == "/dev/null" ]] ; then
162 eerror "Unable to identify the shell to use, proceeding with userland default."
163 case ${USERLAND} in
164 GNU) shell="/bin/false" ;;
165 BSD) shell="/sbin/nologin" ;;
166 Darwin) shell="/usr/sbin/nologin" ;;
167 *) die "Unable to identify the default shell for userland ${USERLAND}"
168 esac
169 fi
170
171 eshell=${shell}
172 fi
173 einfo " - Shell: ${eshell}"
174 opts+=" -s ${eshell}"
175
176 # handle homedir
177 local ehome=$1; shift
178 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then
179 ehome="/dev/null"
180 fi
181 einfo " - Home: ${ehome}"
182 opts+=" -d ${ehome}"
183
184 # handle groups
185 local egroups=$1; shift
186 if [[ ! -z ${egroups} ]] ; then
187 local oldifs=${IFS}
188 local defgroup="" exgroups=""
189
190 export IFS=","
191 for g in ${egroups} ; do
192 export IFS=${oldifs}
193 if [[ -z $(egetent group "${g}") ]] ; then
194 eerror "You must add group ${g} to the system first"
195 die "${g} is not a valid GID"
196 fi
197 if [[ -z ${defgroup} ]] ; then
198 defgroup=${g}
199 else
200 exgroups="${exgroups},${g}"
201 fi
202 export IFS=","
203 done
204 export IFS=${oldifs}
205
206 opts+=" -g ${defgroup}"
207 if [[ ! -z ${exgroups} ]] ; then
208 opts+=" -G ${exgroups:1}"
209 fi
210 else
211 egroups="(none)"
212 fi
213 einfo " - Groups: ${egroups}"
214
215 # handle extra args
216 if [[ $# -gt 0 ]] ; then
217 die "extra arguments no longer supported; please file a bug"
218 else
219 set -- -c "added by portage for ${PN}"
220 einfo " - Extra: $@"
221 fi
222
223 # add the user
224 local oldsandbox=${SANDBOX_ON}
225 export SANDBOX_ON="0"
226 case ${CHOST} in
227 *-darwin*)
228 ### Make the user
229 dscl . create /users/${euser} uid ${euid}
230 dscl . create /users/${euser} shell ${eshell}
231 dscl . create /users/${euser} home ${ehome}
232 dscl . create /users/${euser} realname "added by portage for ${PN}"
233 ### Add the user to the groups specified
234 local oldifs=${IFS}
235 export IFS=","
236 for g in ${egroups} ; do
237 dscl . merge /groups/${g} users ${euser}
238 done
239 export IFS=${oldifs}
240 ;;
241
242 *-freebsd*|*-dragonfly*)
243 pw useradd ${euser} ${opts} "$@" || die
244 ;;
245
246 *-netbsd*)
247 useradd ${opts} ${euser} "$@" || die
248 ;;
249
250 *-openbsd*)
251 # all ops the same, except the -g vs -g/-G ...
252 useradd -u ${euid} -s ${eshell} \
253 -d ${ehome} -g ${egroups} "$@" ${euser} || die
254 ;;
255
256 *)
257 useradd -r ${opts} "$@" ${euser} || die
258 ;;
259 esac
260
261 if [[ ! -e ${ROOT}/${ehome} ]] ; then
262 einfo " - Creating ${ehome} in ${ROOT}"
263 mkdir -p "${ROOT}/${ehome}"
264 chown ${euser} "${ROOT}/${ehome}"
265 chmod 755 "${ROOT}/${ehome}"
266 fi
267
268 export SANDBOX_ON=${oldsandbox}
269 }
270
271 # @FUNCTION: enewgroup
272 # @USAGE: <group> [gid]
273 # @DESCRIPTION:
274 # This function does not require you to understand how to properly add a
275 # group to the system. Just give it a group name to add and enewgroup will
276 # do the rest. You may specify the gid for the group or allow the group to
277 # allocate the next available one.
278 enewgroup() {
279 _assert_pkg_ebuild_phase enewgroup
280
281 # get the group
282 local egroup="$1"; shift
283 if [ -z "${egroup}" ]
284 then
285 eerror "No group specified !"
286 die "Cannot call enewgroup without a group"
287 fi
288
289 # see if group already exists
290 if [[ -n $(egetent group "${egroup}") ]]; then
291 return 0
292 fi
293 einfo "Adding group '${egroup}' to your system ..."
294
295 # options to pass to useradd
296 local opts=
297
298 # handle gid
299 local egid="$1"; shift
300 if [ ! -z "${egid}" ]
301 then
302 if [ "${egid}" -gt 0 ]
303 then
304 if [ -z "`egetent group ${egid}`" ]
305 then
306 if [[ "${CHOST}" == *-darwin* ]]; then
307 opts+=" ${egid}"
308 else
309 opts+=" -g ${egid}"
310 fi
311 else
312 egid="next available; requested gid taken"
313 fi
314 else
315 eerror "Groupid given but is not greater than 0 !"
316 die "${egid} is not a valid GID"
317 fi
318 else
319 egid="next available"
320 fi
321 einfo " - Groupid: ${egid}"
322
323 # handle extra
324 if [ $# -gt 0 ] ; then
325 die "extra arguments no longer supported; please file a bug"
326 fi
327
328 # add the group
329 local oldsandbox="${SANDBOX_ON}"
330 export SANDBOX_ON="0"
331 case ${CHOST} in
332 *-darwin*)
333 # If we need the next available
334 case ${egid} in
335 *[!0-9]*) # Non numeric
336 for ((egid = 101; egid <= 999; egid++)); do
337 [[ -z $(egetent group ${egid}) ]] && break
338 done
339 esac
340 dscl . create /groups/${egroup} gid ${egid}
341 dscl . create /groups/${egroup} passwd '*'
342 ;;
343
344 *-freebsd*|*-dragonfly*)
345 case ${egid} in
346 *[!0-9]*) # Non numeric
347 for ((egid = 101; egid <= 999; egid++)); do
348 [[ -z $(egetent group ${egid}) ]] && break
349 done
350 esac
351 pw groupadd ${egroup} -g ${egid} || die
352 ;;
353
354 *-netbsd*)
355 case ${egid} in
356 *[!0-9]*) # Non numeric
357 for ((egid = 101; egid <= 999; egid++)); do
358 [[ -z $(egetent group ${egid}) ]] && break
359 done
360 esac
361 groupadd -g ${egid} ${egroup} || die
362 ;;
363
364 *)
365 # We specify -r so that we get a GID in the system range from login.defs
366 groupadd -r ${opts} ${egroup} || die
367 ;;
368 esac
369 export SANDBOX_ON="${oldsandbox}"
370 }
371
372 # @FUNCTION: egethome
373 # @USAGE: <user>
374 # @DESCRIPTION:
375 # Gets the home directory for the specified user.
376 egethome() {
377 local pos
378
379 [[ $# -eq 1 ]] || die "usage: egethome <user>"
380
381 case ${CHOST} in
382 *-darwin*|*-freebsd*|*-dragonfly*)
383 pos=9
384 ;;
385 *) # Linux, NetBSD, OpenBSD, etc...
386 pos=6
387 ;;
388 esac
389
390 egetent passwd $1 | cut -d: -f${pos}
391 }
392
393 # @FUNCTION: egetshell
394 # @USAGE: <user>
395 # @DESCRIPTION:
396 # Gets the shell for the specified user.
397 egetshell() {
398 local pos
399
400 [[ $# -eq 1 ]] || die "usage: egetshell <user>"
401
402 case ${CHOST} in
403 *-darwin*|*-freebsd*|*-dragonfly*)
404 pos=10
405 ;;
406 *) # Linux, NetBSD, OpenBSD, etc...
407 pos=7
408 ;;
409 esac
410
411 egetent passwd "$1" | cut -d: -f${pos}
412 }

  ViewVC Help
Powered by ViewVC 1.1.20