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

Diff of /eclass/user.eclass

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

Revision 1.15 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.15 2011/11/29 19:01:28 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)
10# Aaron Walker <ka0ttic@gentoo.org> (FreeBSD) 10# Aaron Walker <ka0ttic@gentoo.org> (FreeBSD)
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
16if [[ ${___ECLASS_ONCE_USER} != "recur -_+^+_- spank" ]] ; then
17___ECLASS_ONCE_USER="recur -_+^+_- spank"
15 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() {
181 einfo " - Home: ${ehome}" 184 einfo " - Home: ${ehome}"
182 opts+=( -d "${ehome}" ) 185 opts+=( -d "${ehome}" )
183 186
184 # handle groups 187 # handle groups
185 local egroups=$1; shift 188 local egroups=$1; shift
186 if [[ ! -z ${egroups} ]] ; then 189 local g egroups_arr
187 local oldifs=${IFS} 190 IFS="," read -r -a egroups_arr <<<"${egroups}"
191 shift
192 if [[ ${#egroups_arr[@]} -gt 0 ]] ; then
188 local defgroup="" exgroups="" 193 local defgroup exgroups
189
190 export IFS=","
191 for g in ${egroups} ; do 194 for g in "${egroups_arr[@]}" ; do
192 export IFS=${oldifs}
193 if [[ -z $(egetent group "${g}") ]] ; then 195 if [[ -z $(egetent group "${g}") ]] ; then
194 eerror "You must add group ${g} to the system first" 196 eerror "You must add group ${g} to the system first"
195 die "${g} is not a valid GID" 197 die "${g} is not a valid GID"
196 fi 198 fi
197 if [[ -z ${defgroup} ]] ; then 199 if [[ -z ${defgroup} ]] ; then
198 defgroup=${g} 200 defgroup=${g}
199 else 201 else
200 exgroups="${exgroups},${g}" 202 exgroups+=",${g}"
201 fi 203 fi
202 export IFS=","
203 done 204 done
204 export IFS=${oldifs}
205
206 opts+=( -g "${defgroup}" ) 205 opts+=( -g "${defgroup}" )
207 if [[ ! -z ${exgroups} ]] ; then 206 if [[ ! -z ${exgroups} ]] ; then
208 opts+=( -G "${exgroups:1}" ) 207 opts+=( -G "${exgroups:1}" )
209 fi 208 fi
210 else
211 egroups="(none)"
212 fi 209 fi
213 einfo " - Groups: ${egroups}" 210 einfo " - Groups: ${egroups:-(none)}"
214 211
215 # handle extra args 212 # handle extra args
216 if [[ $# -gt 0 ]] ; then 213 if [[ $# -gt 0 ]] ; then
217 die "extra arguments no longer supported; please file a bug" 214 die "extra arguments no longer supported; please file a bug"
218 else 215 else
228 dscl . create "/users/${euser}" uid ${euid} 225 dscl . create "/users/${euser}" uid ${euid}
229 dscl . create "/users/${euser}" shell "${eshell}" 226 dscl . create "/users/${euser}" shell "${eshell}"
230 dscl . create "/users/${euser}" home "${ehome}" 227 dscl . create "/users/${euser}" home "${ehome}"
231 dscl . create "/users/${euser}" realname "added by portage for ${PN}" 228 dscl . create "/users/${euser}" realname "added by portage for ${PN}"
232 ### Add the user to the groups specified 229 ### Add the user to the groups specified
233 local g oldifs=${IFS}
234 export IFS=","
235 for g in ${egroups} ; do 230 for g in "${egroups_arr[@]}" ; do
236 dscl . merge "/groups/${g}" users "${euser}" 231 dscl . merge "/groups/${g}" users "${euser}"
237 done 232 done
238 export IFS=${oldifs}
239 ;; 233 ;;
240 234
241 *-freebsd*|*-dragonfly*) 235 *-freebsd*|*-dragonfly*)
242 pw useradd "${euser}" "${opts[@]}" || die 236 pw useradd "${euser}" "${opts[@]}" || die
243 ;; 237 ;;
310 fi 304 fi
311 305
312 # Some targets need to find the next available GID manually 306 # Some targets need to find the next available GID manually
313 _enewgroup_next_gid() { 307 _enewgroup_next_gid() {
314 if [[ ${egid} == *[!0-9]* ]] ; then 308 if [[ ${egid} == *[!0-9]* ]] ; then
315 # Non numeric 309 # Non numeric
316 for ((egid = 101; egid <= 999; egid++)) ; do 310 for ((egid = 101; egid <= 999; egid++)) ; do
317 [[ -z $(egetent group ${egid}) ]] && break 311 [[ -z $(egetent group ${egid}) ]] && break
318 done 312 done
319 fi 313 fi
320 } 314 }
338 ;; 332 ;;
339 333
340 *) 334 *)
341 local opts 335 local opts
342 if [[ ${egid} == *[!0-9]* ]] ; then 336 if [[ ${egid} == *[!0-9]* ]] ; then
343 # Non numeric; let groupadd figure out a GID for us 337 # Non numeric; let groupadd figure out a GID for us
344 opts="" 338 opts=""
345 else 339 else
346 opts="-g ${egid}" 340 opts="-g ${egid}"
347 fi 341 fi
348 # 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
390 ;; 384 ;;
391 esac 385 esac
392 386
393 egetent passwd "$1" | cut -d: -f${pos} 387 egetent passwd "$1" | cut -d: -f${pos}
394} 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.15  
changed lines
  Added in v.1.21

  ViewVC Help
Powered by ViewVC 1.1.20