From: Michael Orlitzky Date: Mon, 7 Aug 2017 21:50:35 +0000 (-0400) Subject: sys-user.eclass: add and utilize a sys-user_getname() function. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=07170b53c083ebec0c95f9e1f0ede6681fc60277;p=mjo-overlay.git sys-user.eclass: add and utilize a sys-user_getname() function. --- diff --git a/eclass/sys-user.eclass b/eclass/sys-user.eclass index 9042f06..090a5ad 100644 --- a/eclass/sys-user.eclass +++ b/eclass/sys-user.eclass @@ -77,13 +77,19 @@ sys-user_src_compile() { :; } sys-user_src_test() { :; } sys-user_getuid() { - [[ $# -eq 1 ]] || die "usage: sys-user_getuid " - # Output the real UID of the given user, or the empty string if the # user does not exist on the system. + [[ $# -eq 1 ]] || die "usage: sys-user_getuid " echo $(id --real --user "${1}") } +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: @@ -136,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 @@ -178,8 +184,16 @@ sys-user_src_configure() { fi fi - # TODO: if the requested (unimportant) UID is not available, we - # should set SYS_USER_UID to the empty string. + 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 + fi # The "useradd" and "usermod" tools expect a comma-separated list, # so change our spaces to commas. Having duplicates in the list is