From a9b4878da992e972a2b2ea95e7540db9e3d7eaa4 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 7 Aug 2017 18:15:04 -0400 Subject: [PATCH] sys-user.eclass: make SYS_USER_UID required and try to modify on upgrades. --- eclass/sys-user.eclass | 58 +++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/eclass/sys-user.eclass b/eclass/sys-user.eclass index 090a5ad..8f32c23 100644 --- a/eclass/sys-user.eclass +++ b/eclass/sys-user.eclass @@ -32,8 +32,11 @@ SYS_USER_NAME="${PN}" # @ECLASS-VARIABLE: SYS_USER_UID # @REQUIRED # @DESCRIPTION: -# etc. - +# This should be set to the "fixed" UID that your user should have. +# We may have to fall back to an arbitrary UID, but you still need +# to specify a real, valid UID here. At the very least because our +# SLOT variable needs it. +[[ -z "${SYS_USER_UID}" ]] && die "SYS_USER_UID must be set" # @ECLASS-VARIABLE: SYS_USER_UID_IMPORTANT # @REQUIRED @@ -129,14 +132,6 @@ sys-user_modify() { sys-user_pkg_pretend() { # Sanity checks that would otherwise run code in global scope. - # - # First ensure that the user didn't say his UID is important and - # then fail to specify one. - 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" - fi # Next ensure that no other username owns an important UID. if [[ "${SYS_USER_UID_IMPORTANT}" == "true" ]]; then @@ -184,15 +179,12 @@ sys-user_src_configure() { fi 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 + 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. + unset SYS_USER_UID fi # The "useradd" and "usermod" tools expect a comma-separated list, @@ -209,13 +201,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. + # real UID and not be e.g. the empty string. # - # TODO: this is a problem now! + # Our sys-user_create() function makes sure to set SYS_USER_UID to + # something useful, and the only place that sys-user_create() is + # called from is sys-user_pkg_preinst(), which takes place before + # this sys-user_src_install(). # - #touch "${T}/${SYS_USER_UID}" || die - #insinto "/var/lib/sys-user" - #doins "${T}/${SYS_USER_UID}" + # The other way that SYS_USER_UID could be empty is during an + # upgrade; however, if you're doing an upgrade and the new UID isn't + # important, then you'll get the same old UID that exists on the + # system from the old package. That old UID is assigned to SYS_USER_UID + # in sys-user_src_configure(), so that case is handled too. + touch "${T}/${SYS_USER_UID}" || die + insinto "/var/lib/sys-user" + doins "${T}/${SYS_USER_UID}" } sys-user_pkg_preinst() { @@ -277,7 +277,7 @@ sys-user_pkg_postinst() { if [[ -n $(sys-user_getuid "${SYS_USER_NAME}") ]]; then die "User ${SYS_USER_NAME} already exists during an upgrade." else - sys-user_create || die "failed to add user ${SYS_USER_NAME}" + sys-user_modify || die "failed to add user ${SYS_USER_NAME}" fi fi } @@ -286,9 +286,15 @@ sys-user_pkg_prerm() { if [[ -z $(sys-user_getuid "${SYS_USER_NAME}") ]]; then # We have successfully done nothing. ewarn "Tried to remove nonexistent user ${SYS_USER_NAME}." - else + elif [[ -z "${REPLACING_VERSIONS}" ]]; then + # The user to remove exists, and this is not an upgrade, so + # we really do remove him. userdel "${SYS_USER_NAME}" || \ die "failed to remove user ${SYS_USER_NAME}" einfo "Removed user ${SYS_USER_NAME} from the system." + + # The missing case: if the user exista and this is an upgrade, + # we leave the user alone to be modified in + # sys-user_pkg_postinst(). fi } -- 2.44.2