]> gitweb.michael.orlitzky.com - mjo-overlay.git/blobdiff - eclass/sys-user.eclass
sys-user.eclass: add and utilize a sys-user_getname() function.
[mjo-overlay.git] / eclass / sys-user.eclass
index 35d20ed050a5ce48a4390c1e3c5663e570c62cda..090a5ad50b5bb7c47ee709cfb140c12df146223e 100644 (file)
@@ -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
@@ -78,9 +79,17 @@ 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.
+       [[ $# -eq 1 ]] || die "usage: sys-user_getuid <username>"
        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 <uid>"
+       echo $(egetent passwd "${1}" | cut -f1 -d':')
+}
+
 sys-user_create() {
        # Create the user whose information is contained in the following
        # variables:
@@ -102,6 +111,22 @@ sys-user_create() {
 }
 
 
+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() {
        # Sanity checks that would otherwise run code in global scope.
        #
@@ -117,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
@@ -159,6 +184,17 @@ 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
+       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.
@@ -173,11 +209,13 @@ 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() {
@@ -221,8 +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.
-               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
 }