/[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.12
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.12 2011/11/26 06:45:38 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
202 local oldsandbox=${SANDBOX_ON} 217 if [[ $# -gt 0 ]] ; then
203 export SANDBOX_ON="0" 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
204 case ${CHOST} in 225 case ${CHOST} in
205 *-darwin*) 226 *-darwin*)
206 ### Make the user 227 ### Make the user
207 if [[ -z $@ ]] ; then
208 dscl . create /users/${euser} uid ${euid} 228 dscl . create /users/${euser} uid ${euid}
209 dscl . create /users/${euser} shell ${eshell} 229 dscl . create /users/${euser} shell ${eshell}
210 dscl . create /users/${euser} home ${ehome} 230 dscl . create /users/${euser} home ${ehome}
211 dscl . create /users/${euser} realname "added by portage for ${PN}" 231 dscl . create /users/${euser} realname "added by portage for ${PN}"
212 ### Add the user to the groups specified 232 ### Add the user to the groups specified
213 local oldifs=${IFS} 233 local oldifs=${IFS}
214 export IFS="," 234 export IFS=","
215 for g in ${egroups} ; do 235 for g in ${egroups} ; do
216 dscl . merge /groups/${g} users ${euser} 236 dscl . merge /groups/${g} users ${euser}
217 done 237 done
218 export IFS=${oldifs} 238 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 ;; 239 ;;
240
226 *-freebsd*|*-dragonfly*) 241 *-freebsd*|*-dragonfly*)
227 if [[ -z $@ ]] ; then
228 pw useradd ${euser} ${opts} \ 242 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 ;; 243 ;;
237 244
238 *-netbsd*) 245 *-netbsd*)
239 if [[ -z $@ ]] ; then
240 useradd ${opts} ${euser} || die "enewuser failed"
241 else
242 einfo " - Extra: $@"
243 useradd ${opts} ${euser} "$@" || die "enewuser failed" 246 useradd ${opts} ${euser} "$@" || die
244 fi
245 ;; 247 ;;
246 248
247 *-openbsd*) 249 *-openbsd*)
248 if [[ -z $@ ]] ; then 250 # all ops the same, except the -g vs -g/-G ...
249 useradd -u ${euid} -s ${eshell} \ 251 useradd -u ${euid} -s ${eshell} \
250 -d ${ehome} -c "Added by portage for ${PN}" \ 252 -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 ;; 253 ;;
259 254
260 *) 255 *)
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} "$@" \ 256 useradd -r ${opts} "$@" ${euser} || die
269 ${euser} \
270 || die "enewuser failed"
271 fi
272 ;; 257 ;;
273 esac 258 esac
274 259
275 if [[ ! -e ${ROOT}/${ehome} ]] ; then 260 if [[ ! -e ${ROOT}/${ehome} ]] ; then
276 einfo " - Creating ${ehome} in ${ROOT}" 261 einfo " - Creating ${ehome} in ${ROOT}"
277 mkdir -p "${ROOT}/${ehome}" 262 mkdir -p "${ROOT}/${ehome}"
278 chown ${euser} "${ROOT}/${ehome}" 263 chown ${euser} "${ROOT}/${ehome}"
279 chmod 755 "${ROOT}/${ehome}" 264 chmod 755 "${ROOT}/${ehome}"
280 fi 265 fi
281
282 export SANDBOX_ON=${oldsandbox}
283} 266}
284 267
285# @FUNCTION: enewgroup 268# @FUNCTION: enewgroup
286# @USAGE: <group> [gid] 269# @USAGE: <group> [gid]
287# @DESCRIPTION: 270# @DESCRIPTION:
316 if [ "${egid}" -gt 0 ] 299 if [ "${egid}" -gt 0 ]
317 then 300 then
318 if [ -z "`egetent group ${egid}`" ] 301 if [ -z "`egetent group ${egid}`" ]
319 then 302 then
320 if [[ "${CHOST}" == *-darwin* ]]; then 303 if [[ "${CHOST}" == *-darwin* ]]; then
321 opts="${opts} ${egid}" 304 opts+=" ${egid}"
322 else 305 else
323 opts="${opts} -g ${egid}" 306 opts+=" -g ${egid}"
324 fi 307 fi
325 else 308 else
326 egid="next available; requested gid taken" 309 egid="next available; requested gid taken"
327 fi 310 fi
328 else 311 else
333 egid="next available" 316 egid="next available"
334 fi 317 fi
335 einfo " - Groupid: ${egid}" 318 einfo " - Groupid: ${egid}"
336 319
337 # handle extra 320 # handle extra
338 local eextra="$@" 321 if [ $# -gt 0 ] ; then
339 opts="${opts} ${eextra}" 322 die "extra arguments no longer supported; please file a bug"
323 fi
340 324
341 # add the group 325 # add the group
342 local oldsandbox="${SANDBOX_ON}"
343 export SANDBOX_ON="0"
344 case ${CHOST} in 326 case ${CHOST} in
345 *-darwin*) 327 *-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 328 # If we need the next available
355 case ${egid} in 329 case ${egid} in
356 *[!0-9]*) # Non numeric 330 *[!0-9]*) # Non numeric
357 for ((egid = 101; egid <= 999; egid++)); do 331 for ((egid = 101; egid <= 999; egid++)); do
358 [[ -z $(egetent group ${egid}) ]] && break 332 [[ -z $(egetent group ${egid}) ]] && break
367 *[!0-9]*) # Non numeric 341 *[!0-9]*) # Non numeric
368 for ((egid = 101; egid <= 999; egid++)); do 342 for ((egid = 101; egid <= 999; egid++)); do
369 [[ -z $(egetent group ${egid}) ]] && break 343 [[ -z $(egetent group ${egid}) ]] && break
370 done 344 done
371 esac 345 esac
372 pw groupadd ${egroup} -g ${egid} || die "enewgroup failed" 346 pw groupadd ${egroup} -g ${egid} || die
373 ;; 347 ;;
374 348
375 *-netbsd*) 349 *-netbsd*)
376 case ${egid} in 350 case ${egid} in
377 *[!0-9]*) # Non numeric 351 *[!0-9]*) # Non numeric
378 for ((egid = 101; egid <= 999; egid++)); do 352 for ((egid = 101; egid <= 999; egid++)); do
379 [[ -z $(egetent group ${egid}) ]] && break 353 [[ -z $(egetent group ${egid}) ]] && break
380 done 354 done
381 esac 355 esac
382 groupadd -g ${egid} ${egroup} || die "enewgroup failed" 356 groupadd -g ${egid} ${egroup} || die
383 ;; 357 ;;
384 358
385 *) 359 *)
386 # We specify -r so that we get a GID in the system range from login.defs 360 # We specify -r so that we get a GID in the system range from login.defs
387 groupadd -r ${opts} ${egroup} || die "enewgroup failed" 361 groupadd -r ${opts} ${egroup} || die
388 ;; 362 ;;
389 esac 363 esac
390 export SANDBOX_ON="${oldsandbox}"
391} 364}
365
366# @FUNCTION: egethome
367# @USAGE: <user>
368# @DESCRIPTION:
369# Gets the home directory for the specified user.
370egethome() {
371 local pos
372
373 [[ $# -eq 1 ]] || die "usage: egethome <user>"
374
375 case ${CHOST} in
376 *-darwin*|*-freebsd*|*-dragonfly*)
377 pos=9
378 ;;
379 *) # Linux, NetBSD, OpenBSD, etc...
380 pos=6
381 ;;
382 esac
383
384 egetent passwd $1 | cut -d: -f${pos}
385}
386
387# @FUNCTION: egetshell
388# @USAGE: <user>
389# @DESCRIPTION:
390# Gets the shell for the specified user.
391egetshell() {
392 local pos
393
394 [[ $# -eq 1 ]] || die "usage: egetshell <user>"
395
396 case ${CHOST} in
397 *-darwin*|*-freebsd*|*-dragonfly*)
398 pos=10
399 ;;
400 *) # Linux, NetBSD, OpenBSD, etc...
401 pos=7
402 ;;
403 esac
404
405 egetent passwd "$1" | cut -d: -f${pos}
406}

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

  ViewVC Help
Powered by ViewVC 1.1.20