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

Diff of /eclass/user.eclass

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

Revision 1.4 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.4 2011/10/31 17:39:52 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 # ignore output if nscd doesn't exist, or we're not running as root 91 # ignore output if nscd doesn't exist, or we're not running as root
76 nscd -i "$1" 2>/dev/null 92 nscd -i "${db}" 2>/dev/null
77 getent "$1" "$2" 93 getent "${db}" "${key}"
78 ;; 94 ;;
79 esac 95 esac
80} 96}
81 97
82# @FUNCTION: enewuser 98# @FUNCTION: enewuser
83# @USAGE: <user> [uid] [shell] [homedir] [groups] [params] 99# @USAGE: <user> [uid] [shell] [homedir] [groups]
84# @DESCRIPTION: 100# @DESCRIPTION:
85# 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
86# 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.
87# 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
88# /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.
89# and default params sets the comment as 'added by portage for ${PN}'.
90enewuser() { 105enewuser() {
91 _assert_pkg_ebuild_phase enewuser 106 _assert_pkg_ebuild_phase enewuser
92 107
93 # get the username 108 # get the username
94 local euser=$1; shift 109 local euser=$1; shift
123 if [[ ${euid} == "next" ]] ; then 138 if [[ ${euid} == "next" ]] ; then
124 for ((euid = 101; euid <= 999; euid++)); do 139 for ((euid = 101; euid <= 999; euid++)); do
125 [[ -z $(egetent passwd ${euid}) ]] && break 140 [[ -z $(egetent passwd ${euid}) ]] && break
126 done 141 done
127 fi 142 fi
128 opts="${opts} -u ${euid}" 143 opts+=" -u ${euid}"
129 einfo " - Userid: ${euid}" 144 einfo " - Userid: ${euid}"
130 145
131 # handle shell 146 # handle shell
132 local eshell=$1; shift 147 local eshell=$1; shift
133 if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then 148 if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then
155 fi 170 fi
156 171
157 eshell=${shell} 172 eshell=${shell}
158 fi 173 fi
159 einfo " - Shell: ${eshell}" 174 einfo " - Shell: ${eshell}"
160 opts="${opts} -s ${eshell}" 175 opts+=" -s ${eshell}"
161 176
162 # handle homedir 177 # handle homedir
163 local ehome=$1; shift 178 local ehome=$1; shift
164 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then 179 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then
165 ehome="/dev/null" 180 ehome="/dev/null"
166 fi 181 fi
167 einfo " - Home: ${ehome}" 182 einfo " - Home: ${ehome}"
168 opts="${opts} -d ${ehome}" 183 opts+=" -d ${ehome}"
169 184
170 # handle groups 185 # handle groups
171 local egroups=$1; shift 186 local egroups=$1; shift
172 if [[ ! -z ${egroups} ]] ; then 187 if [[ ! -z ${egroups} ]] ; then
173 local oldifs=${IFS} 188 local oldifs=${IFS}
187 fi 202 fi
188 export IFS="," 203 export IFS=","
189 done 204 done
190 export IFS=${oldifs} 205 export IFS=${oldifs}
191 206
192 opts="${opts} -g ${defgroup}" 207 opts+=" -g ${defgroup}"
193 if [[ ! -z ${exgroups} ]] ; then 208 if [[ ! -z ${exgroups} ]] ; then
194 opts="${opts} -G ${exgroups:1}" 209 opts+=" -G ${exgroups:1}"
195 fi 210 fi
196 else 211 else
197 egroups="(none)" 212 egroups="(none)"
198 fi 213 fi
199 einfo " - Groups: ${egroups}" 214 einfo " - Groups: ${egroups}"
200 215
201 # 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
202 local oldsandbox=${SANDBOX_ON} 225 local oldsandbox=${SANDBOX_ON}
203 export SANDBOX_ON="0" 226 export SANDBOX_ON="0"
204 case ${CHOST} in 227 case ${CHOST} in
205 *-darwin*) 228 *-darwin*)
206 ### Make the user 229 ### Make the user
207 if [[ -z $@ ]] ; then
208 dscl . create /users/${euser} uid ${euid} 230 dscl . create /users/${euser} uid ${euid}
209 dscl . create /users/${euser} shell ${eshell} 231 dscl . create /users/${euser} shell ${eshell}
210 dscl . create /users/${euser} home ${ehome} 232 dscl . create /users/${euser} home ${ehome}
211 dscl . create /users/${euser} realname "added by portage for ${PN}" 233 dscl . create /users/${euser} realname "added by portage for ${PN}"
212 ### Add the user to the groups specified 234 ### Add the user to the groups specified
213 local oldifs=${IFS} 235 local oldifs=${IFS}
214 export IFS="," 236 export IFS=","
215 for g in ${egroups} ; do 237 for g in ${egroups} ; do
216 dscl . merge /groups/${g} users ${euser} 238 dscl . merge /groups/${g} users ${euser}
217 done 239 done
218 export IFS=${oldifs} 240 export IFS=${oldifs}
219 else
220 einfo "Extra options are not supported on Darwin yet"
221 einfo "Please report the ebuild along with the info below"
222 einfo "eextra: $@"
223 die "Required function missing"
224 fi
225 ;; 241 ;;
242
226 *-freebsd*|*-dragonfly*) 243 *-freebsd*|*-dragonfly*)
227 if [[ -z $@ ]] ; then
228 pw useradd ${euser} ${opts} \ 244 pw useradd ${euser} ${opts} "$@" || die
229 -c "added by portage for ${PN}" \
230 die "enewuser failed"
231 else
232 einfo " - Extra: $@"
233 pw useradd ${euser} ${opts} \
234 "$@" || die "enewuser failed"
235 fi
236 ;; 245 ;;
237 246
238 *-netbsd*) 247 *-netbsd*)
239 if [[ -z $@ ]] ; then
240 useradd ${opts} ${euser} || die "enewuser failed"
241 else
242 einfo " - Extra: $@"
243 useradd ${opts} ${euser} "$@" || die "enewuser failed" 248 useradd ${opts} ${euser} "$@" || die
244 fi
245 ;; 249 ;;
246 250
247 *-openbsd*) 251 *-openbsd*)
248 if [[ -z $@ ]] ; then 252 # all ops the same, except the -g vs -g/-G ...
249 useradd -u ${euid} -s ${eshell} \ 253 useradd -u ${euid} -s ${eshell} \
250 -d ${ehome} -c "Added by portage for ${PN}" \ 254 -d ${ehome} -g ${egroups} "$@" ${euser} || die
251 -g ${egroups} ${euser} || die "enewuser failed"
252 else
253 einfo " - Extra: $@"
254 useradd -u ${euid} -s ${eshell} \
255 -d ${ehome} -c "Added by portage for ${PN}" \
256 -g ${egroups} ${euser} "$@" || die "enewuser failed"
257 fi
258 ;; 255 ;;
259 256
260 *) 257 *)
261 if [[ -z $@ ]] ; then
262 useradd -r ${opts} \
263 -c "added by portage for ${PN}" \
264 ${euser} \
265 || die "enewuser failed"
266 else
267 einfo " - Extra: $@"
268 useradd -r ${opts} "$@" \ 258 useradd -r ${opts} "$@" ${euser} || die
269 ${euser} \
270 || die "enewuser failed"
271 fi
272 ;; 259 ;;
273 esac 260 esac
274 261
275 if [[ ! -e ${ROOT}/${ehome} ]] ; then 262 if [[ ! -e ${ROOT}/${ehome} ]] ; then
276 einfo " - Creating ${ehome} in ${ROOT}" 263 einfo " - Creating ${ehome} in ${ROOT}"
316 if [ "${egid}" -gt 0 ] 303 if [ "${egid}" -gt 0 ]
317 then 304 then
318 if [ -z "`egetent group ${egid}`" ] 305 if [ -z "`egetent group ${egid}`" ]
319 then 306 then
320 if [[ "${CHOST}" == *-darwin* ]]; then 307 if [[ "${CHOST}" == *-darwin* ]]; then
321 opts="${opts} ${egid}" 308 opts+=" ${egid}"
322 else 309 else
323 opts="${opts} -g ${egid}" 310 opts+=" -g ${egid}"
324 fi 311 fi
325 else 312 else
326 egid="next available; requested gid taken" 313 egid="next available; requested gid taken"
327 fi 314 fi
328 else 315 else
333 egid="next available" 320 egid="next available"
334 fi 321 fi
335 einfo " - Groupid: ${egid}" 322 einfo " - Groupid: ${egid}"
336 323
337 # handle extra 324 # handle extra
338 local eextra="$@" 325 if [ $# -gt 0 ] ; then
339 opts="${opts} ${eextra}" 326 die "extra arguments no longer supported; please file a bug"
327 fi
340 328
341 # add the group 329 # add the group
342 local oldsandbox="${SANDBOX_ON}" 330 local oldsandbox="${SANDBOX_ON}"
343 export SANDBOX_ON="0" 331 export SANDBOX_ON="0"
344 case ${CHOST} in 332 case ${CHOST} in
345 *-darwin*) 333 *-darwin*)
346 if [ ! -z "${eextra}" ];
347 then
348 einfo "Extra options are not supported on Darwin/OS X yet"
349 einfo "Please report the ebuild along with the info below"
350 einfo "eextra: ${eextra}"
351 die "Required function missing"
352 fi
353
354 # If we need the next available 334 # If we need the next available
355 case ${egid} in 335 case ${egid} in
356 *[!0-9]*) # Non numeric 336 *[!0-9]*) # Non numeric
357 for ((egid = 101; egid <= 999; egid++)); do 337 for ((egid = 101; egid <= 999; egid++)); do
358 [[ -z $(egetent group ${egid}) ]] && break 338 [[ -z $(egetent group ${egid}) ]] && break
367 *[!0-9]*) # Non numeric 347 *[!0-9]*) # Non numeric
368 for ((egid = 101; egid <= 999; egid++)); do 348 for ((egid = 101; egid <= 999; egid++)); do
369 [[ -z $(egetent group ${egid}) ]] && break 349 [[ -z $(egetent group ${egid}) ]] && break
370 done 350 done
371 esac 351 esac
372 pw groupadd ${egroup} -g ${egid} || die "enewgroup failed" 352 pw groupadd ${egroup} -g ${egid} || die
373 ;; 353 ;;
374 354
375 *-netbsd*) 355 *-netbsd*)
376 case ${egid} in 356 case ${egid} in
377 *[!0-9]*) # Non numeric 357 *[!0-9]*) # Non numeric
378 for ((egid = 101; egid <= 999; egid++)); do 358 for ((egid = 101; egid <= 999; egid++)); do
379 [[ -z $(egetent group ${egid}) ]] && break 359 [[ -z $(egetent group ${egid}) ]] && break
380 done 360 done
381 esac 361 esac
382 groupadd -g ${egid} ${egroup} || die "enewgroup failed" 362 groupadd -g ${egid} ${egroup} || die
383 ;; 363 ;;
384 364
385 *) 365 *)
386 # 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
387 groupadd -r ${opts} ${egroup} || die "enewgroup failed" 367 groupadd -r ${opts} ${egroup} || die
388 ;; 368 ;;
389 esac 369 esac
390 export SANDBOX_ON="${oldsandbox}" 370 export SANDBOX_ON="${oldsandbox}"
391} 371}
372
373# @FUNCTION: egethome
374# @USAGE: <user>
375# @DESCRIPTION:
376# Gets the home directory for the specified user.
377egethome() {
378 local pos
379
380 [[ $# -eq 1 ]] || die "usage: egethome <user>"
381
382 case ${CHOST} in
383 *-darwin*|*-freebsd*|*-dragonfly*)
384 pos=9
385 ;;
386 *) # Linux, NetBSD, OpenBSD, etc...
387 pos=6
388 ;;
389 esac
390
391 egetent passwd $1 | cut -d: -f${pos}
392}
393
394# @FUNCTION: egetshell
395# @USAGE: <user>
396# @DESCRIPTION:
397# Gets the shell for the specified user.
398egetshell() {
399 local pos
400
401 [[ $# -eq 1 ]] || die "usage: egetshell <user>"
402
403 case ${CHOST} in
404 *-darwin*|*-freebsd*|*-dragonfly*)
405 pos=10
406 ;;
407 *) # Linux, NetBSD, OpenBSD, etc...
408 pos=7
409 ;;
410 esac
411
412 egetent passwd "$1" | cut -d: -f${pos}
413}

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

  ViewVC Help
Powered by ViewVC 1.1.20