| 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.17 2011/11/29 19:32:23 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 | |
|
|
16 | if [[ ${___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() { |
| … | |
… | |
| 381 | ;; |
384 | ;; |
| 382 | esac |
385 | esac |
| 383 | |
386 | |
| 384 | egetent passwd "$1" | cut -d: -f${pos} |
387 | egetent passwd "$1" | cut -d: -f${pos} |
| 385 | } |
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. |
|
|
399 | esethome() { |
|
|
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 | |
|
|
459 | fi |