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

Diff of /eclass/user.eclass

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

Revision 1.14 Revision 1.21
1# Copyright 1999-2011 Gentoo Foundation 1# Copyright 1999-2012 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.14 2011/11/26 07:20:31 vapier Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/user.eclass,v 1.21 2012/06/22 18:57:33 axs 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)
11# @BLURB: user management in ebuilds 11# @BLURB: user management in ebuilds
12# @DESCRIPTION: 12# @DESCRIPTION:
13# The user eclass contains a suite of functions that allow ebuilds 13# The user eclass contains a suite of functions that allow ebuilds
14# to quickly make sure users in the installed system are sane. 14# to quickly make sure users in the installed system are sane.
15 15
16if [[ ${___ECLASS_ONCE_USER} != "recur -_+^+_- spank" ]] ; then
17___ECLASS_ONCE_USER="recur -_+^+_- spank"
18
16# @FUNCTION: _assert_pkg_ebuild_phase 19# @FUNCTION: _assert_pkg_ebuild_phase
17# @INTERNAL 20# @INTERNAL
18# @USAGE: <calling func name> 21# @USAGE: <calling func name>
19_assert_pkg_ebuild_phase() { 22_assert_pkg_ebuild_phase() {
20 case ${EBUILD_PHASE} in 23 case ${EBUILD_PHASE} in
21 setup|preinst|postinst) ;; 24 setup|preinst|postinst) ;;
22 *) 25 *)
23 eerror "'$1()' called from '${EBUILD_PHASE}()' which is not a pkg_* function." 26 eerror "'$1()' called from '${EBUILD_PHASE}' phase which is not OK:"
27 eerror "You may only call from pkg_{setup,preinst,postinst} functions."
24 eerror "Package fails at QA and at life. Please file a bug." 28 eerror "Package fails at QA and at life. Please file a bug."
25 die "Bad package! $1 is only for use in pkg_* functions!" 29 die "Bad package! $1 is only for use in some pkg_* functions!"
26 esac 30 esac
27} 31}
28 32
29# @FUNCTION: egetent 33# @FUNCTION: egetent
30# @USAGE: <database> <key> 34# @USAGE: <database> <key>
101# Same as enewgroup, you are not required to understand how to properly add 105# Same as enewgroup, you are not required to understand how to properly add
102# a user to the system. The only required parameter is the username. 106# a user to the system. The only required parameter is the username.
103# Default uid is (pass -1 for this) next available, default shell is 107# Default uid is (pass -1 for this) next available, default shell is
104# /bin/false, default homedir is /dev/null, and there are no default groups. 108# /bin/false, default homedir is /dev/null, and there are no default groups.
105enewuser() { 109enewuser() {
106 _assert_pkg_ebuild_phase enewuser 110 _assert_pkg_ebuild_phase ${FUNCNAME}
107 111
108 # get the username 112 # get the username
109 local euser=$1; shift 113 local euser=$1; shift
110 if [[ -z ${euser} ]] ; then 114 if [[ -z ${euser} ]] ; then
111 eerror "No username specified !" 115 eerror "No username specified !"
180 einfo " - Home: ${ehome}" 184 einfo " - Home: ${ehome}"
181 opts+=( -d "${ehome}" ) 185 opts+=( -d "${ehome}" )
182 186
183 # handle groups 187 # handle groups
184 local egroups=$1; shift 188 local egroups=$1; shift
185 if [[ ! -z ${egroups} ]] ; then 189 local g egroups_arr
186 local oldifs=${IFS} 190 IFS="," read -r -a egroups_arr <<<"${egroups}"
191 shift
192 if [[ ${#egroups_arr[@]} -gt 0 ]] ; then
187 local defgroup="" exgroups="" 193 local defgroup exgroups
188
189 export IFS=","
190 for g in ${egroups} ; do 194 for g in "${egroups_arr[@]}" ; do
191 export IFS=${oldifs}
192 if [[ -z $(egetent group "${g}") ]] ; then 195 if [[ -z $(egetent group "${g}") ]] ; then
193 eerror "You must add group ${g} to the system first" 196 eerror "You must add group ${g} to the system first"
194 die "${g} is not a valid GID" 197 die "${g} is not a valid GID"
195 fi 198 fi
196 if [[ -z ${defgroup} ]] ; then 199 if [[ -z ${defgroup} ]] ; then
197 defgroup=${g} 200 defgroup=${g}
198 else 201 else
199 exgroups="${exgroups},${g}" 202 exgroups+=",${g}"
200 fi 203 fi
201 export IFS=","
202 done 204 done
203 export IFS=${oldifs}
204
205 opts+=( -g "${defgroup}" ) 205 opts+=( -g "${defgroup}" )
206 if [[ ! -z ${exgroups} ]] ; then 206 if [[ ! -z ${exgroups} ]] ; then
207 opts+=( -G "${exgroups:1}" ) 207 opts+=( -G "${exgroups:1}" )
208 fi 208 fi
209 else
210 egroups="(none)"
211 fi 209 fi
212 einfo " - Groups: ${egroups}" 210 einfo " - Groups: ${egroups:-(none)}"
213 211
214 # handle extra args 212 # handle extra args
215 if [[ $# -gt 0 ]] ; then 213 if [[ $# -gt 0 ]] ; then
216 die "extra arguments no longer supported; please file a bug" 214 die "extra arguments no longer supported; please file a bug"
217 else 215 else
227 dscl . create "/users/${euser}" uid ${euid} 225 dscl . create "/users/${euser}" uid ${euid}
228 dscl . create "/users/${euser}" shell "${eshell}" 226 dscl . create "/users/${euser}" shell "${eshell}"
229 dscl . create "/users/${euser}" home "${ehome}" 227 dscl . create "/users/${euser}" home "${ehome}"
230 dscl . create "/users/${euser}" realname "added by portage for ${PN}" 228 dscl . create "/users/${euser}" realname "added by portage for ${PN}"
231 ### Add the user to the groups specified 229 ### Add the user to the groups specified
232 local g oldifs=${IFS}
233 export IFS=","
234 for g in ${egroups} ; do 230 for g in "${egroups_arr[@]}" ; do
235 dscl . merge "/groups/${g}" users "${euser}" 231 dscl . merge "/groups/${g}" users "${euser}"
236 done 232 done
237 export IFS=${oldifs}
238 ;; 233 ;;
239 234
240 *-freebsd*|*-dragonfly*) 235 *-freebsd*|*-dragonfly*)
241 pw useradd "${euser}" "${opts[@]}" || die 236 pw useradd "${euser}" "${opts[@]}" || die
242 ;; 237 ;;
270# This function does not require you to understand how to properly add a 265# This function does not require you to understand how to properly add a
271# group to the system. Just give it a group name to add and enewgroup will 266# group to the system. Just give it a group name to add and enewgroup will
272# do the rest. You may specify the gid for the group or allow the group to 267# do the rest. You may specify the gid for the group or allow the group to
273# allocate the next available one. 268# allocate the next available one.
274enewgroup() { 269enewgroup() {
275 _assert_pkg_ebuild_phase enewgroup 270 _assert_pkg_ebuild_phase ${FUNCNAME}
276 271
277 # get the group 272 # get the group
278 local egroup=$1; shift 273 local egroup=$1; shift
279 if [[ -z ${egroup} ]] ; then 274 if [[ -z ${egroup} ]] ; then
280 eerror "No group specified !" 275 eerror "No group specified !"
309 fi 304 fi
310 305
311 # Some targets need to find the next available GID manually 306 # Some targets need to find the next available GID manually
312 _enewgroup_next_gid() { 307 _enewgroup_next_gid() {
313 if [[ ${egid} == *[!0-9]* ]] ; then 308 if [[ ${egid} == *[!0-9]* ]] ; then
314 # Non numeric 309 # Non numeric
315 for ((egid = 101; egid <= 999; egid++)) ; do 310 for ((egid = 101; egid <= 999; egid++)) ; do
316 [[ -z $(egetent group ${egid}) ]] && break 311 [[ -z $(egetent group ${egid}) ]] && break
317 done 312 done
318 fi 313 fi
319 } 314 }
337 ;; 332 ;;
338 333
339 *) 334 *)
340 local opts 335 local opts
341 if [[ ${egid} == *[!0-9]* ]] ; then 336 if [[ ${egid} == *[!0-9]* ]] ; then
342 # Non numeric; let groupadd figure out a GID for us 337 # Non numeric; let groupadd figure out a GID for us
343 opts="" 338 opts=""
344 else 339 else
345 opts="-g ${egid}" 340 opts="-g ${egid}"
346 fi 341 fi
347 # We specify -r so that we get a GID in the system range from login.defs 342 # We specify -r so that we get a GID in the system range from login.defs
389 ;; 384 ;;
390 esac 385 esac
391 386
392 egetent passwd "$1" | cut -d: -f${pos} 387 egetent passwd "$1" | cut -d: -f${pos}
393} 388}
389
390# @FUNCTION: esethome
391# @USAGE: <user> <homedir>
392# @DESCRIPTION:
393# Update the home directory in a platform-agnostic way.
394# Required parameters is the username and the new home directory.
395# Specify -1 if you want to set home to the enewuser default
396# of /dev/null.
397# If the new home directory does not exist, it is created.
398# Any previously existing home directory is NOT moved.
399esethome() {
400 _assert_pkg_ebuild_phase ${FUNCNAME}
401
402 # get the username
403 local euser=$1; shift
404 if [[ -z ${euser} ]] ; then
405 eerror "No username specified !"
406 die "Cannot call esethome without a username"
407 fi
408
409 # lets see if the username already exists
410 if [[ -z $(egetent passwd "${euser}") ]] ; then
411 ewarn "User does not exist, cannot set home dir -- skipping."
412 return 1
413 fi
414
415 # handle homedir
416 local ehome=$1; shift
417 if [[ -z ${ehome} ]] ; then
418 eerror "No home directory specified !"
419 die "Cannot call esethome without a home directory or '-1'"
420 fi
421
422 if [[ ${ehome} == "-1" ]] ; then
423 ehome="/dev/null"
424 fi
425 einfo " - Home: ${ehome}"
426
427 # ensure home directory exists, otherwise update will fail
428 if [[ ! -e ${ROOT}/${ehome} ]] ; then
429 einfo " - Creating ${ehome} in ${ROOT}"
430 mkdir -p "${ROOT}/${ehome}"
431 chown "${euser}" "${ROOT}/${ehome}"
432 chmod 755 "${ROOT}/${ehome}"
433 fi
434
435 # update the home directory
436 case ${CHOST} in
437 *-darwin*)
438 dscl . change "/users/${euser}" home "${ehome}"
439 ;;
440
441 *-freebsd*|*-dragonfly*)
442 pw usermod "${euser}" -d "${ehome}" && return 0
443 [[ $? == 8 ]] && eerror "${euser} is in use, cannot update home"
444 eerror "There was an error when attempting to update the home directory for ${euser}"
445 eerror "Please update it manually on your system:"
446 eerror "\t pw usermod \"${euser}\" -d \"${ehome}\""
447 ;;
448
449 *)
450 usermod -d "${ehome}" "${euser}" && return 0
451 [[ $? == 8 ]] && eerror "${euser} is in use, cannot update home"
452 eerror "There was an error when attempting to update the home directory for ${euser}"
453 eerror "Please update it manually on your system (as root):"
454 eerror "\t usermod -d \"${ehome}\" \"${euser}\""
455 ;;
456 esac
457}
458
459fi

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.21

  ViewVC Help
Powered by ViewVC 1.1.20