X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mjo-overlay.git;a=blobdiff_plain;f=eclass%2Fsys-user.eclass;h=090a5ad50b5bb7c47ee709cfb140c12df146223e;hp=95417c79a8516ec4f53031cfe6c442d43e3eb77f;hb=07170b53c083ebec0c95f9e1f0ede6681fc60277;hpb=06ebf3a15fd51455f6c62ef075602a5d6d189be7 diff --git a/eclass/sys-user.eclass b/eclass/sys-user.eclass index 95417c7..090a5ad 100644 --- a/eclass/sys-user.eclass +++ b/eclass/sys-user.eclass @@ -9,6 +9,7 @@ # This eclass does most of the work for the sys-user/ packages that # supply system user accounts. +# Needed for egetshell and egethome. inherit user EXPORT_FUNCTIONS pkg_pretend src_unpack src_configure src_compile src_install src_test pkg_preinst pkg_postinst pkg_prerm @@ -23,14 +24,16 @@ SYS_USER_NAME="${PN}" # @ECLASS-VARIABLE: SYS_USER_GROUPS # @DESCRIPTION: -# etc. +# A space-separated list of groups that the user will belong to. +# Dependencies on the appropriate sys-group packages are generated +# automatically. : ${SYS_USER_GROUPS:=${PN}} # @ECLASS-VARIABLE: SYS_USER_UID # @REQUIRED # @DESCRIPTION: -# etc. (use -1 to get next available using user.eclass) -[[ -z "${SYS_USER_UID}" ]] && die "SYS_USER_UID must be set" +# etc. + # @ECLASS-VARIABLE: SYS_USER_UID_IMPORTANT # @REQUIRED @@ -46,13 +49,13 @@ SLOT="0/${SYS_USER_UID}" # @ECLASS-VARIABLE: SYS_USER_HOME # @DESCRIPTION: -# etc. (use -1 to get user.eclass default) -: ${SYS_USER_HOME:=-1} +# etc. +: ${SYS_USER_HOME:=/home/${SYS_USER_NAME}} # @ECLASS-VARIABLE: SYS_USER_SHELL # @DESCRIPTION: -# etc. (use -1 to get user.eclass default) -: ${SYS_USER_SHELL:=-1} +# etc. +: ${SYS_USER_SHELL:=/bin/false} case ${EAPI} in 6) ;; @@ -76,20 +79,52 @@ sys-user_src_test() { :; } sys-user_getuid() { # Output the real UID of the given user, or the empty string if the # user does not exist on the system. - local username="${1}" - echo $(id --real --user "${username}") + [[ $# -eq 1 ]] || die "usage: sys-user_getuid " + echo $(id --real --user "${1}") } -sys-user_next_uid() { - local euid; - for (( euid = 101; euid <= 999; euid++ )); do - [[ -z $(egetent passwd "${euid}") ]] && break - done - if (( "${euid}" == 999 )); then - die "out of available UIDs!" - else - echo "${euid}" - fi +sys-user_getname() { + # Output the username associated with the given UID, or the empty string + # if the given UID is still available. + [[ $# -eq 1 ]] || die "usage: sys-user_getname " + echo $(egetent passwd "${1}" | cut -f1 -d':') +} + +sys-user_create() { + # Create the user whose information is contained in the following + # variables: + # + # * SYS_USER_NAME + # * SYS_USER_UID + # * SYS_USER_SHELL + # * SYS_USER_HOME + # * SYS_USER_GROUPS + # + # We don't create a group with the same name; that should be the + # job of the matching sys-group package. + useradd --no-user-group \ + ${SYS_USER_UID:+--uid }"${SYS_USER_UID}" \ + ${SYS_USER_GROUPS:+--groups }"${SYS_USER_GROUPS}" \ + --shell "${SYS_USER_SHELL}" \ + --home-dir "${SYS_USER_HOME}" \ + "${SYS_USER_NAME}" +} + + +sys-user_modify() { + # Modify the existing user named $SYS_USER_NAME to match the values + # contained in the following variables: + # + # * SYS_USER_UID + # * SYS_USER_SHELL + # * SYS_USER_HOME + # * SYS_USER_GROUPS + # + usermod ${SYS_USER_UID:+--uid }"${SYS_USER_UID}" \ + ${SYS_USER_GROUPS:+--append --groups }"${SYS_USER_GROUPS}" \ + --shell "${SYS_USER_SHELL}" \ + --home-dir "${SYS_USER_HOME}" \ + "${SYS_USER_NAME}" } sys-user_pkg_pretend() { @@ -97,7 +132,7 @@ sys-user_pkg_pretend() { # # First ensure that the user didn't say his UID is important and # then fail to specify one. - if (( "${SYS_USER_UID}" == -1 )) && + if [[ -z "${SYS_USER_UID}" ]] && [[ "${SYS_USER_UID_IMPORTANT}" == "true" ]]; then # Don't make no damn sense. die "arbitrary UID requested with SYS_USER_UID_IMPORTANT=true" @@ -107,7 +142,7 @@ sys-user_pkg_pretend() { if [[ "${SYS_USER_UID_IMPORTANT}" == "true" ]]; then # Ok, the UID is important. Make sure nobody else has it. Or # rather, nobody else *with a different username* has it. - local oldname=$(egetent passwd "${SYS_USER_UID}" | cut -f1 -d':') + local oldname=$(sys-user_getname "${SYS_USER_UID}") if [[ "${SYS_USER_NAME}" != "${oldname}" ]]; then die "important UID ${SYS_USER_UID} already belongs to ${oldname}" fi @@ -115,8 +150,8 @@ sys-user_pkg_pretend() { # Finally, ensure that this username doesn't already exist with # another UID if its UID is supposedly important. - if [[ -n $(egetent passwd "${SYS_USER_NAME}") ]]; then - local olduid=$(sys-user_getuid "${SYS_USER_NAME}") + local olduid=$(sys-user_getuid "${SYS_USER_NAME}") + if [[ -n "${olduid}" ]]; then if [[ "${SYS_USER_UID_IMPORTANT}" == "true" ]] && \ [[ "${SYS_USER_UID}" != "${olduid}" ]]; then # The UID is important and specified, but there is already a @@ -127,7 +162,8 @@ sys-user_pkg_pretend() { } sys-user_src_configure() { - if [[ -n $(egetent passwd "${SYS_USER_NAME}") ]]; then + local current_uid=$(sys-user_getuid "${SYS_USER_NAME}") + if [[ -n "${current_uid}" ]]; then # UPGRADE PATH: This user already exists, so if the eclass # consumer doesn't care about some settings, we can reuse the # pre-existing ones. @@ -136,29 +172,33 @@ sys-user_src_configure() { # prevents us from incrementing the UID on a reinstall, and doing # so would break most packages that need a system user to exist. if [[ "${SYS_USER_UID_IMPORTANT}" != "true" ]]; then - SYS_USER_UID=$(sys-user_getuid "${SYS_USER_NAME}") + SYS_USER_UID="${current_uid}" fi - if (( "${SYS_USER_HOME}" == -1 )); then + if [[ -z "${SYS_USER_HOME}" ]]; then SYS_USER_HOME=$(egethome "${SYS_USER_NAME}") fi - if (( "${SYS_USER_SHELL}" == -1 )); then + if [[ -z "${SYS_USER_SHELL}" ]]; then SYS_USER_SHELL=$(egetshell "${SYS_USER_NAME}") + fi + fi - if [[ ${SYS_USER_SHELL} == */false ]] || \ - [[ ${SYS_USER_SHELL} == */nologin ]]; then - # WHYYYYY? enewuser complains if we try to set a default - # shell explicitly. - SYS_USER_SHELL="-1" - fi + if [[ -n "${SYS_USER_UID}" ]]; then + # A specific UID was requested. + local current_name=$(sys-user_getname "${SYS_USER_UID}") + if [[ "${current_name}" != "${SYS_USER_NAME}" ]]; then + # This UID is already taken by another user, but this + # specific UID was not important (we checked in + # pkg_pretend), so fall back to an arbitrary one. + SYS_USER_UID="" fi - elif (( "${SYS_USER_UID}" == -1 )); then - # There is no pre-existing user (i.e. this isn't along the - # upgrade path), and the consumer says he doesn't care about the - # UID, so pick the next one. - SYS_USER_UID=$(sys-user_next_uid) fi + + # The "useradd" and "usermod" tools expect a comma-separated list, + # so change our spaces to commas. Having duplicates in the list is + # not a problem for those two tools. + SYS_USER_GROUPS="${SYS_USER_GROUPS// /,}" } sys-user_src_install() { @@ -169,24 +209,21 @@ sys-user_src_install() { # be possible. # # Beware, this only works if SYS_USER_UID is guaranteed to have a - # real UID and not, for example, -1. That is taken care of in - # src_configure() for now. - touch "${T}/${SYS_USER_UID}" || die - insinto "/var/lib/sys-user" - doins "${T}/${SYS_USER_UID}" + # real UID and not, for example, -1. + # + # TODO: this is a problem now! + # + #touch "${T}/${SYS_USER_UID}" || die + #insinto "/var/lib/sys-user" + #doins "${T}/${SYS_USER_UID}" } sys-user_pkg_preinst() { - if [[ -z $(egetent passwd "${SYS_USER_NAME}") ]]; then + if [[ -z $(sys-user_getuid "${SYS_USER_NAME}") ]]; then # The user does not already exist. This is the nice and easy # case because no matter how we got here, we want to go ahead # and create the (new) user. - enewuser "${SYS_USER_NAME}" \ - "${SYS_USER_UID}" \ - "${SYS_USER_SHELL}" \ - "${SYS_USER_HOME}" \ - "${SYS_USER_GROUPS}" \ - || die "failed to add user ${SYS_USER_NAME}" + sys-user_create || die "failed to add user ${SYS_USER_NAME}" elif [[ -n "${REPLACING_VERSIONS}" ]]; then # # This case is done in pkg_postint() to avoid clobbering a @@ -222,10 +259,9 @@ sys-user_pkg_preinst() { fi # The user already exists, so all we have left to do is to try - # to append SYS_USER_GROUPS to the existing groups. The "usermod" - # tool expects a comma-separated list, so change our spaces to - # commas. This does succeed if you append duplicates. - usermod --append --groups "${SYS_USER_GROUPS// /,}" \ + # to append SYS_USER_GROUPS to the existing groups. The home + # dir, shell, and uid should all match already. + sys-user_modify \ || die "failed to append groups to existing user ${SYS_USER_NAME}" fi } @@ -238,21 +274,16 @@ sys-user_pkg_postinst() { # that this new version is going to add. At this point, in our # pkg_postinst(), the old version's pkg_prerm() phase should have # already happened. - if [[ -n $(egetent passwd "${SYS_USER_NAME}") ]]; then + if [[ -n $(sys-user_getuid "${SYS_USER_NAME}") ]]; then die "User ${SYS_USER_NAME} already exists during an upgrade." else - enewuser "${SYS_USER_NAME}" \ - "${SYS_USER_UID}" \ - "${SYS_USER_SHELL}" \ - "${SYS_USER_HOME}" \ - "${SYS_USER_GROUPS}" \ - || die "failed to add user ${SYS_USER_NAME}" + sys-user_create || die "failed to add user ${SYS_USER_NAME}" fi fi } sys-user_pkg_prerm() { - if [[ -z $(egetent passwd "${SYS_USER_NAME}") ]]; then + if [[ -z $(sys-user_getuid "${SYS_USER_NAME}") ]]; then # We have successfully done nothing. ewarn "Tried to remove nonexistent user ${SYS_USER_NAME}." else