commit:     2108f8e38104fe8a10825c568d7c18fe1ed6e3fd
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Thu Jun  6 14:26:46 2019 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Thu Jun 20 08:16:45 2019 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=2108f8e3

user.eclass: Introduce esetshell

Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>

 eclass/user.eclass | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/eclass/user.eclass b/eclass/user.eclass
index 54d7a3fdbe2..3bd381b0c08 100644
--- a/eclass/user.eclass
+++ b/eclass/user.eclass
@@ -465,4 +465,65 @@ esethome() {
        esac
 }
 
+# @FUNCTION: esetshell
+# @USAGE: <user> <shell>
+# @DESCRIPTION:
+# Update the shell in a platform-agnostic way.
+# Required parameters is the username and the new shell.
+# Specify -1 if you want to set shell to platform-specific nologin.
+esetshell() {
+       _assert_pkg_ebuild_phase ${FUNCNAME}
+
+       # get the username
+       local euser=$1; shift
+       if [[ -z ${euser} ]] ; then
+               eerror "No username specified !"
+               die "Cannot call esetshell without a username"
+       fi
+
+       # lets see if the username already exists
+       if [[ -z $(egetent passwd "${euser}") ]] ; then
+               ewarn "User does not exist, cannot set shell -- skipping."
+               return 1
+       fi
+
+       # handle shell
+       local eshell=$1; shift
+       if [[ -z ${eshell} ]] ; then
+               eerror "No shell specified !"
+               die "Cannot call esetshell without a shell or '-1'"
+       fi
+
+       if [[ ${eshell} == "-1" ]] ; then
+               eshell=$(user_get_nologin)
+       fi
+
+       # exit with no message if shell is up to date
+       if [[ $(egetshell "${euser}") == ${eshell} ]]; then
+               return 0
+       fi
+
+       einfo "Updating shell for user '${euser}' ..."
+       einfo " - Shell: ${eshell}"
+
+       # update the shell
+       case ${CHOST} in
+       *-freebsd*|*-dragonfly*)
+               pw usermod "${euser}" -s "${eshell}" && return 0
+               [[ $? == 8 ]] && eerror "${euser} is in use, cannot update 
shell"
+               eerror "There was an error when attempting to update the shell 
for ${euser}"
+               eerror "Please update it manually on your system:"
+               eerror "\t pw usermod \"${euser}\" -s \"${eshell}\""
+               ;;
+
+       *)
+               usermod -s "${eshell}" "${euser}" && return 0
+               [[ $? == 8 ]] && eerror "${euser} is in use, cannot update 
shell"
+               eerror "There was an error when attempting to update the shell 
for ${euser}"
+               eerror "Please update it manually on your system (as root):"
+               eerror "\t usermod -s \"${eshell}\" \"${euser}\""
+               ;;
+       esac
+}
+
 fi

Reply via email to