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

Diff of /eclass/user.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.2 Revision 1.11
1# Copyright 1999-2011 Gentoo Foundation 1# Copyright 1999-2011 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/user.eclass,v 1.2 2011/10/27 07:26:55 vapier Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/user.eclass,v 1.11 2011/11/26 06:42:07 vapier Exp $
4 4
5# @ECLASS: user.eclass 5# @ECLASS: user.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# base-system@gentoo.org (Linux) 7# base-system@gentoo.org (Linux)
8# Joe Jezak <josejx@gmail.com> (OS X) 8# Joe Jezak <josejx@gmail.com> (OS X)
16# @FUNCTION: _assert_pkg_ebuild_phase 16# @FUNCTION: _assert_pkg_ebuild_phase
17# @INTERNAL 17# @INTERNAL
18# @USAGE: <calling func name> 18# @USAGE: <calling func name>
19_assert_pkg_ebuild_phase() { 19_assert_pkg_ebuild_phase() {
20 case ${EBUILD_PHASE} in 20 case ${EBUILD_PHASE} in
21 unpack|prepare|configure|compile|test|install) 21 setup|preinst|postinst) ;;
22 *)
22 eerror "'$1()' called from '${EBUILD_PHASE}()' which is not a pkg_* function." 23 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 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 die "Bad package! $1 is only for use in pkg_* functions!"
25 esac 26 esac
26} 27}
28# @FUNCTION: egetent 29# @FUNCTION: egetent
29# @USAGE: <database> <key> 30# @USAGE: <database> <key>
30# @DESCRIPTION: 31# @DESCRIPTION:
31# Small wrapper for getent (Linux), nidump (< Mac OS X 10.5), 32# 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# dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup().
34#
35# Supported databases: group passwd
33egetent() { 36egetent() {
37 local db=$1 key=$2
38
39 [[ $# -ge 3 ]] && die "usage: egetent <database> <key>"
40
41 case ${db} in
42 passwd|group) ;;
43 *) die "sorry, database '${db}' not yet supported; file a bug" ;;
44 esac
45
34 case ${CHOST} in 46 case ${CHOST} in
35 *-darwin[678]) 47 *-darwin[678])
36 case "$2" in 48 case ${key} in
37 *[!0-9]*) # Non numeric 49 *[!0-9]*) # Non numeric
38 nidump $1 . | awk -F":" "{ if (\$1 ~ /^$2\$/) {print \$0;exit;} }" 50 nidump ${db} . | awk -F: "(\$1 ~ /^${key}\$/) {print;exit;}"
39 ;; 51 ;;
40 *) # Numeric 52 *) # Numeric
41 nidump $1 . | awk -F":" "{ if (\$3 == $2) {print \$0;exit;} }" 53 nidump ${db} . | awk -F: "(\$3 == ${key}) {print;exit;}"
42 ;; 54 ;;
43 esac 55 esac
44 ;; 56 ;;
45 *-darwin*) 57 *-darwin*)
46 local mytype=$1 58 local mykey
47 [[ "passwd" == $mytype ]] && mytype="Users"
48 [[ "group" == $mytype ]] && mytype="Groups"
49 case "$2" in 59 case ${db} in
60 passwd) db="Users" mykey="UniqueID" ;;
61 group) db="Groups" mykey="PrimaryGroupID" ;;
62 esac
63
64 case ${key} in
50 *[!0-9]*) # Non numeric 65 *[!0-9]*) # Non numeric
51 dscl . -read /$mytype/$2 2>/dev/null |grep RecordName 66 dscl . -read /${db}/${key} 2>/dev/null |grep RecordName
52 ;; 67 ;;
53 *) # Numeric 68 *) # Numeric
54 local mykey="UniqueID"
55 [[ $mytype == "Groups" ]] && mykey="PrimaryGroupID"
56 dscl . -search /$mytype $mykey $2 2>/dev/null 69 dscl . -search /${db} ${mykey} ${key} 2>/dev/null
57 ;; 70 ;;
58 esac 71 esac
59 ;; 72 ;;
60 *-freebsd*|*-dragonfly*) 73 *-freebsd*|*-dragonfly*)
61 local opts action="user" 74 case ${db} in
62 [[ $1 == "passwd" ]] || action="group" 75 passwd) db="user" ;;
76 *) ;;
77 esac
63 78
64 # lookup by uid/gid 79 # lookup by uid/gid
80 local opts
65 if [[ $2 == [[:digit:]]* ]] ; then 81 if [[ ${key} == [[:digit:]]* ]] ; then
66 [[ ${action} == "user" ]] && opts="-u" || opts="-g" 82 [[ ${db} == "user" ]] && opts="-u" || opts="-g"
67 fi 83 fi
68 84
69 pw show ${action} ${opts} "$2" -q 85 pw show ${db} ${opts} "${key}" -q
70 ;; 86 ;;
71 *-netbsd*|*-openbsd*) 87 *-netbsd*|*-openbsd*)
72 grep "$2:\*:" /etc/$1 88 grep "${key}:\*:" /etc/${db}
73 ;; 89 ;;
74 *) 90 *)
75 type -p nscd >& /dev/null && nscd -i "$1" 91 # ignore output if nscd doesn't exist, or we're not running as root
76 getent "$1" "$2" 92 nscd -i "${db}" 2>/dev/null
93 getent "${db}" "${key}"
77 ;; 94 ;;
78 esac 95 esac
79} 96}
80 97
81# @FUNCTION: enewuser 98# @FUNCTION: enewuser
82# @USAGE: <user> [uid] [shell] [homedir] [groups] [params] 99# @USAGE: <user> [uid] [shell] [homedir] [groups]
83# @DESCRIPTION: 100# @DESCRIPTION:
84# Same as enewgroup, you are not required to understand how to properly add 101# Same as enewgroup, you are not required to understand how to properly add
85# a user to the system. The only required parameter is the username. 102# a user to the system. The only required parameter is the username.
86# Default uid is (pass -1 for this) next available, default shell is 103# Default uid is (pass -1 for this) next available, default shell is
87# /bin/false, default homedir is /dev/null, there are no default groups, 104# /bin/false, default homedir is /dev/null, and there are no default groups.
88# and default params sets the comment as 'added by portage for ${PN}'.
89enewuser() { 105enewuser() {
90 _assert_pkg_ebuild_phase enewuser 106 _assert_pkg_ebuild_phase enewuser
91 107
92 # get the username 108 # get the username
93 local euser=$1; shift 109 local euser=$1; shift
122 if [[ ${euid} == "next" ]] ; then 138 if [[ ${euid} == "next" ]] ; then
123 for ((euid = 101; euid <= 999; euid++)); do 139 for ((euid = 101; euid <= 999; euid++)); do
124 [[ -z $(egetent passwd ${euid}) ]] && break 140 [[ -z $(egetent passwd ${euid}) ]] && break
125 done 141 done
126 fi 142 fi
127 opts="${opts} -u ${euid}" 143 opts+=" -u ${euid}"
128 einfo " - Userid: ${euid}" 144 einfo " - Userid: ${euid}"
129 145
130 # handle shell 146 # handle shell
131 local eshell=$1; shift 147 local eshell=$1; shift
132 if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then 148 if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then
154 fi 170 fi
155 171
156 eshell=${shell} 172 eshell=${shell}
157 fi 173 fi
158 einfo " - Shell: ${eshell}" 174 einfo " - Shell: ${eshell}"
159 opts="${opts} -s ${eshell}" 175 opts+=" -s ${eshell}"
160 176
161 # handle homedir 177 # handle homedir
162 local ehome=$1; shift 178 local ehome=$1; shift
163 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then 179 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then
164 ehome="/dev/null" 180 ehome="/dev/null"
165 fi 181 fi
166 einfo " - Home: ${ehome}" 182 einfo " - Home: ${ehome}"
167 opts="${opts} -d ${ehome}" 183 opts+=" -d ${ehome}"
168 184
169 # handle groups 185 # handle groups
170 local egroups=$1; shift 186 local egroups=$1; shift
171 if [[ ! -z ${egroups} ]] ; then 187 if [[ ! -z ${egroups} ]] ; then
172 local oldifs=${IFS} 188 local oldifs=${IFS}
186 fi 202 fi
187 export IFS="," 203 export IFS=","
188 done 204 done
189 export IFS=${oldifs} 205 export IFS=${oldifs}
190 206
191 opts="${opts} -g ${defgroup}" 207 opts+=" -g ${defgroup}"
192 if [[ ! -z ${exgroups} ]] ; then 208 if [[ ! -z ${exgroups} ]] ; then
193 opts="${opts} -G ${exgroups:1}" 209 opts+=" -G ${exgroups:1}"
194 fi 210 fi
195 else 211 else
196 egroups="(none)" 212 egroups="(none)"
197 fi 213 fi
198 einfo " - Groups: ${egroups}" 214 einfo " - Groups: ${egroups}"
199 215
200 # handle extra and add the user 216 # handle extra args
217 if [[ $# -gt 0 ]] ; then
218 die "extra arguments no longer supported; please file a bug"
219 else
220 set -- -c "added by portage for ${PN}"
221 einfo " - Extra: $@"
222 fi
223
224 # add the user
201 local oldsandbox=${SANDBOX_ON} 225 local oldsandbox=${SANDBOX_ON}
202 export SANDBOX_ON="0" 226 export SANDBOX_ON="0"
203 case ${CHOST} in 227 case ${CHOST} in
204 *-darwin*) 228 *-darwin*)
205 ### Make the user 229 ### Make the user
206 if [[ -z $@ ]] ; then
207 dscl . create /users/${euser} uid ${euid} 230 dscl . create /users/${euser} uid ${euid}
208 dscl . create /users/${euser} shell ${eshell} 231 dscl . create /users/${euser} shell ${eshell}
209 dscl . create /users/${euser} home ${ehome} 232 dscl . create /users/${euser} home ${ehome}
210 dscl . create /users/${euser} realname "added by portage for ${PN}" 233 dscl . create /users/${euser} realname "added by portage for ${PN}"
211 ### Add the user to the groups specified 234 ### Add the user to the groups specified
212 local oldifs=${IFS} 235 local oldifs=${IFS}
213 export IFS="," 236 export IFS=","
214 for g in ${egroups} ; do 237 for g in ${egroups} ; do
215 dscl . merge /groups/${g} users ${euser} 238 dscl . merge /groups/${g} users ${euser}
216 done 239 done
217 export IFS=${oldifs} 240 export IFS=${oldifs}
218 else
219 einfo "Extra options are not supported on Darwin yet"
220 einfo "Please report the ebuild along with the info below"
221 einfo "eextra: $@"
222 die "Required function missing"
223 fi
224 ;; 241 ;;
242
225 *-freebsd*|*-dragonfly*) 243 *-freebsd*|*-dragonfly*)
226 if [[ -z $@ ]] ; then
227 pw useradd ${euser} ${opts} \ 244 pw useradd ${euser} ${opts} "$@" || die
228 -c "added by portage for ${PN}" \
229 die "enewuser failed"
230 else
231 einfo " - Extra: $@"
232 pw useradd ${euser} ${opts} \
233 "$@" || die "enewuser failed"
234 fi
235 ;; 245 ;;
236 246
237 *-netbsd*) 247 *-netbsd*)
238 if [[ -z $@ ]] ; then
239 useradd ${opts} ${euser} || die "enewuser failed"
240 else
241 einfo " - Extra: $@"
242 useradd ${opts} ${euser} "$@" || die "enewuser failed" 248 useradd ${opts} ${euser} "$@" || die
243 fi
244 ;; 249 ;;
245 250
246 *-openbsd*) 251 *-openbsd*)
247 if [[ -z $@ ]] ; then 252 # all ops the same, except the -g vs -g/-G ...
248 useradd -u ${euid} -s ${eshell} \ 253 useradd -u ${euid} -s ${eshell} \
249 -d ${ehome} -c "Added by portage for ${PN}" \ 254 -d ${ehome} -g ${egroups} "$@" ${euser} || die
250 -g ${egroups} ${euser} || die "enewuser failed"
251 else
252 einfo " - Extra: $@"
253 useradd -u ${euid} -s ${eshell} \
254 -d ${ehome} -c "Added by portage for ${PN}" \
255 -g ${egroups} ${euser} "$@" || die "enewuser failed"
256 fi
257 ;; 255 ;;
258 256
259 *) 257 *)
260 if [[ -z $@ ]] ; then
261 useradd -r ${opts} \
262 -c "added by portage for ${PN}" \
263 ${euser} \
264 || die "enewuser failed"
265 else
266 einfo " - Extra: $@"
267 useradd -r ${opts} "$@" \ 258 useradd -r ${opts} "$@" ${euser} || die
268 ${euser} \
269 || die "enewuser failed"
270 fi
271 ;; 259 ;;
272 esac 260 esac
273 261
274 if [[ ! -e ${ROOT}/${ehome} ]] ; then 262 if [[ ! -e ${ROOT}/${ehome} ]] ; then
275 einfo " - Creating ${ehome} in ${ROOT}" 263 einfo " - Creating ${ehome} in ${ROOT}"
315 if [ "${egid}" -gt 0 ] 303 if [ "${egid}" -gt 0 ]
316 then 304 then
317 if [ -z "`egetent group ${egid}`" ] 305 if [ -z "`egetent group ${egid}`" ]
318 then 306 then
319 if [[ "${CHOST}" == *-darwin* ]]; then 307 if [[ "${CHOST}" == *-darwin* ]]; then
320 opts="${opts} ${egid}" 308 opts+=" ${egid}"
321 else 309 else
322 opts="${opts} -g ${egid}" 310 opts+=" -g ${egid}"
323 fi 311 fi
324 else 312 else
325 egid="next available; requested gid taken" 313 egid="next available; requested gid taken"
326 fi 314 fi
327 else 315 else
332 egid="next available" 320 egid="next available"
333 fi 321 fi
334 einfo " - Groupid: ${egid}" 322 einfo " - Groupid: ${egid}"
335 323
336 # handle extra 324 # handle extra
337 local eextra="$@" 325 if [ $# -gt 0 ] ; then
338 opts="${opts} ${eextra}" 326 die "extra arguments no longer supported; please file a bug"
327 fi
339 328
340 # add the group 329 # add the group
341 local oldsandbox="${SANDBOX_ON}" 330 local oldsandbox="${SANDBOX_ON}"
342 export SANDBOX_ON="0" 331 export SANDBOX_ON="0"
343 case ${CHOST} in 332 case ${CHOST} in
344 *-darwin*) 333 *-darwin*)
345 if [ ! -z "${eextra}" ];
346 then
347 einfo "Extra options are not supported on Darwin/OS X yet"
348 einfo "Please report the ebuild along with the info below"
349 einfo "eextra: ${eextra}"
350 die "Required function missing"
351 fi
352
353 # If we need the next available 334 # If we need the next available
354 case ${egid} in 335 case ${egid} in
355 *[!0-9]*) # Non numeric 336 *[!0-9]*) # Non numeric
356 for ((egid = 101; egid <= 999; egid++)); do 337 for ((egid = 101; egid <= 999; egid++)); do
357 [[ -z $(egetent group ${egid}) ]] && break 338 [[ -z $(egetent group ${egid}) ]] && break
366 *[!0-9]*) # Non numeric 347 *[!0-9]*) # Non numeric
367 for ((egid = 101; egid <= 999; egid++)); do 348 for ((egid = 101; egid <= 999; egid++)); do
368 [[ -z $(egetent group ${egid}) ]] && break 349 [[ -z $(egetent group ${egid}) ]] && break
369 done 350 done
370 esac 351 esac
371 pw groupadd ${egroup} -g ${egid} || die "enewgroup failed" 352 pw groupadd ${egroup} -g ${egid} || die
372 ;; 353 ;;
373 354
374 *-netbsd*) 355 *-netbsd*)
375 case ${egid} in 356 case ${egid} in
376 *[!0-9]*) # Non numeric 357 *[!0-9]*) # Non numeric
377 for ((egid = 101; egid <= 999; egid++)); do 358 for ((egid = 101; egid <= 999; egid++)); do
378 [[ -z $(egetent group ${egid}) ]] && break 359 [[ -z $(egetent group ${egid}) ]] && break
379 done 360 done
380 esac 361 esac
381 groupadd -g ${egid} ${egroup} || die "enewgroup failed" 362 groupadd -g ${egid} ${egroup} || die
382 ;; 363 ;;
383 364
384 *) 365 *)
385 # We specify -r so that we get a GID in the system range from login.defs 366 # We specify -r so that we get a GID in the system range from login.defs
386 groupadd -r ${opts} ${egroup} || die "enewgroup failed" 367 groupadd -r ${opts} ${egroup} || die
387 ;; 368 ;;
388 esac 369 esac
389 export SANDBOX_ON="${oldsandbox}" 370 export SANDBOX_ON="${oldsandbox}"
390} 371}
391 372
373# @FUNCTION: egethome
374# @USAGE: <user>
375# @DESCRIPTION:
392# Gets the home directory for the specified user 376# Gets the home directory for the specified user.
393# it's a wrap around egetent as the position of the home directory in the line
394# varies depending on the os used.
395#
396# To use that, inherit eutils, not portability!
397egethome() { 377egethome() {
398 local pos 378 local pos
379
380 [[ $# -eq 1 ]] || die "usage: egethome <user>"
399 381
400 case ${CHOST} in 382 case ${CHOST} in
401 *-darwin*|*-freebsd*|*-dragonfly*) 383 *-darwin*|*-freebsd*|*-dragonfly*)
402 pos=9 384 pos=9
403 ;; 385 ;;
407 esac 389 esac
408 390
409 egetent passwd $1 | cut -d: -f${pos} 391 egetent passwd $1 | cut -d: -f${pos}
410} 392}
411 393
394# @FUNCTION: egetshell
395# @USAGE: <user>
396# @DESCRIPTION:
412# Gets the shell for the specified user 397# Gets the shell for the specified user.
413# it's a wrap around egetent as the position of the home directory in the line
414# varies depending on the os used.
415#
416# To use that, inherit eutils, not portability!
417egetshell() { 398egetshell() {
418 local pos 399 local pos
400
401 [[ $# -eq 1 ]] || die "usage: egetshell <user>"
419 402
420 case ${CHOST} in 403 case ${CHOST} in
421 *-darwin*|*-freebsd*|*-dragonfly*) 404 *-darwin*|*-freebsd*|*-dragonfly*)
422 pos=10 405 pos=10
423 ;; 406 ;;
426 ;; 409 ;;
427 esac 410 esac
428 411
429 egetent passwd "$1" | cut -d: -f${pos} 412 egetent passwd "$1" | cut -d: -f${pos}
430} 413}
431
432# Returns true if specified user has a shell that precludes logins
433# on whichever operating system.
434is-login-disabled() {
435
436 case $(egetshell "$1") in
437 /bin/false|/usr/bin/false|/sbin/nologin|/usr/sbin/nologin)
438 return 0 ;;
439 *)
440 return 1 ;;
441 esac
442}

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.11

  ViewVC Help
Powered by ViewVC 1.1.20