sys-user.eclass: use a custom user-creation function.
authorMichael Orlitzky <michael@orlitzky.com>
Mon, 7 Aug 2017 21:10:24 +0000 (17:10 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 7 Aug 2017 21:10:24 +0000 (17:10 -0400)
eclass/sys-user.eclass

index 8417212dc67bdace6dbc82d5aea41f2796dcfcd3..35d20ed050a5ce48a4390c1e3c5663e570c62cda 100644 (file)
@@ -23,14 +23,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 +48,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,28 +78,36 @@ 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}")
+       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_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_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 (( "${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"
@@ -140,26 +150,19 @@ sys-user_src_configure() {
                        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}")
-
-                       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
                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() {
@@ -182,12 +185,7 @@ sys-user_pkg_preinst() {
                # 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
@@ -223,10 +221,8 @@ 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.
+               usermod --append --groups "${SYS_USER_GROUPS}" \
                        || die "failed to append groups to existing user ${SYS_USER_NAME}"
        fi
 }
@@ -242,12 +238,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
-                  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
 }