5 kx #!/bin/bash
5 kx #
5 kx # Copyright 1995 Hrvoje Dogan, Croatia.
5 kx # Copyright 2002-2004, 2008, 2009, 2010 Stuart Winter, Surrey, England, UK.
5 kx # Copyright 2004, 2008-2010 Slackware Linux, Inc., Concord, CA, USA
5 kx # Copyright 2012 Patrick J. Volkerding, Sebeka, MN, USA
5 kx # All rights reserved.
5 kx #
5 kx # Redistribution and use of this script, with or without modification, is
5 kx # permitted provided that the following conditions are met:
5 kx #
5 kx # 1. Redistributions of this script must retain the above copyright
5 kx # notice, this list of conditions and the following disclaimer.
5 kx #
5 kx # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
5 kx # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
5 kx # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
5 kx # EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
5 kx # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5 kx # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
5 kx # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
5 kx # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
5 kx # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
5 kx # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5 kx #
5 kx #
5 kx ##########################################################################
5 kx # Program: /usr/sbin/adduser
5 kx # Purpose: Interactive front end to /usr/sbin/useradd for Slackware Linux
5 kx # Author : Stuart Winter <mozes@slackware.com>
5 kx # Based on the original Slackware adduser by Hrvoje Dogan
5 kx # with modifications by Patrick Volkerding
5 kx # Version: 1.16
5 kx ##########################################################################
5 kx # Usage..: adduser [<new_user_name>]
5 kx ##########################################################################
5 kx # History #
5 kx ###########
5 kx # v1.17 - 2019-04-01
5 kx # * Re-invoking input requests when human error causes failure. <ttk>
5 kx # qv: https://www.linuxquestions.org/questions/slackware-14/adduser-shell-script-error-4175650984/
5 kx # v1.16 - 2018-07-22
5 kx # * Added input group. <pjv>
5 kx # v1.15 - 2012-09-13
5 kx # * Added scanner group, which may be required by third party drivers.
5 kx # v1.14 - 2012-08-24
5 kx # * Added lp group, which is now required for scanning. <pjv>
5 kx # v1.13 - 13/01/10
5 kx # * Fixed bug that removed underscore characters from UNIX group names.
5 kx # Thanks to mRgOBLIN for the report and Jim Hawkins for the fix. <sw>
5 kx # v1.12 - 21/07/09
5 kx # * Adjusted the search of /etc/passwd to exclude the NIS inclusion
5 kx # string. Thanks to Dominik L. Borkowski.
5 kx # v1.11 - 04/06/09
5 kx # * Add power and netdev to the suggested group list
5 kx # v1.10 - 24/03/08
5 kx # * To facilitate use of the automatic mounting features of HAL,
5 kx # allow the admin to easily add users to the default groups:
5 kx # audio,cdrom,video,plugdev,floppy.
5 kx # The default is not to add new users to these groups.
5 kx # And by the way, this script is "useradd from Slackware" not
5 kx # "superadduser from Gentoo" ;-)
5 kx # v1.09 - 07/06/04
5 kx # * Added standard Slackware script licence to the head of this file.
5 kx # v1.08 - 25/04/04
5 kx # * Disallow user names that begin with a numeric because useradd
5 kx # (from shadow v4.03) does not allow them. <sw>
5 kx # v1.07 - 07/03/03
5 kx # * When supplying a null string for the uid (meaning 'Choose next available'),
5 kx # if there were file names in the range 'a-z' in the pwd then the
5 kx # egrep command considered these files rather than the null string.
5 kx # The egrep expression is now in quotes.
5 kx # Reported & fixed by Vadim O. Ustiansky <sw>
5 kx # v1.06 - 31/03/03
5 kx # * Ask to chown user.group the home directory if it already exists.
5 kx # This helps reduce later confusion when adding users whose home dir
5 kx # already exists (mounted partition for example) and is owned
5 kx # by a user other than the user to which the directory is being
5 kx # assigned as home. Default is not to chown.
5 kx # Brought to my attention by mRgOBLIN. <sw>
5 kx # v1.05 - 04/01/03
5 kx # * Advise & prevent users from creating logins with '.' characters
5 kx # in the user name. <sw>
5 kx # * Made pending account creation info look neater <sw>
5 kx # v1.04 - 09/06/02
5 kx # * Catered for shadow-4.0.3's 'useradd' binary that no longer
5 kx # will let you create a user that has any uppercase chars in it
5 kx # This was reported on the userlocal.org forums
5 kx # by 'xcp' - thanks. <sw,pjv>
5 kx # v1.03 - 20/05/02
5 kx # * Support 'broken' (null lines in) /etc/passwd and
5 kx # /etc/group files <sw>
5 kx # * For recycling UIDs (default still 'off'), we now look in
5 kx # /etc/login.defs for the UID_MIN value and use it
5 kx # If not found then default to 1000 <sw>
5 kx # v1.02 - 10/04/02
5 kx # * Fix user-specified UID bug. <pjv>
5 kx # v1.01 - 23/03/02
5 kx # * Match Slackware indenting style, simplify. <pjv>
5 kx # v1.00 - 22/03/02
5 kx # * Created
5 kx #######################################################################
5 kx
5 kx # Path to files
5 kx pfile=/etc/passwd
5 kx gfile=/etc/group
5 kx sfile=/etc/shells
5 kx
5 kx # Paths to binaries
5 kx useradd=/usr/sbin/useradd
5 kx chfn=/usr/bin/chfn
5 kx passwd=/usr/bin/passwd
5 kx chmod=/bin/chmod
5 kx
5 kx # Defaults
5 kx defhome=/home
5 kx defshell=/bin/bash
5 kx defchmod=711 # home dir permissions - may be preferable to use 701, however.
5 kx defgroup=users
5 kx AGID="audio cdrom floppy input lp netdev plugdev power scanner video" # additional groups for desktop users
5 kx
5 kx # Determine what the minimum UID is (for UID recycling)
5 kx # (we ignore it if it's not at the beginning of the line (i.e. commented out with #))
5 kx export recycleUIDMIN="$(grep ^UID_MIN /etc/login.defs | awk '{print $2}' 2>/dev/null)"
5 kx # If we couldn't find it, set it to the default of 1000
5 kx if [ -z "$recycleUIDMIN" ]; then
5 kx export recycleUIDMIN=1000 # this is the default from Slackware's /etc/login.defs
5 kx fi
5 kx
5 kx
5 kx # This setting enables the 'recycling' of older unused UIDs.
5 kx # When you userdel a user, it removes it from passwd and shadow but it will
5 kx # never get used again unless you specify it expliticly -- useradd (appears to) just
5 kx # look at the last line in passwd and increment the uid. I like the idea of
5 kx # recycling uids but you may have very good reasons not to (old forgotten
5 kx # confidential files still on the system could then be owned by this new user).
5 kx # We'll set this to no because this is what the original adduser shell script
5 kx # did and it's what users expect.
5 kx recycleuids=no
5 kx
5 kx # Function to read keyboard input.
5 kx # bash1 is broken (even ash will take read -ep!), so we work around
5 kx # it (even though bash1 is no longer supported on Slackware).
5 kx function get_input() {
5 kx local output
5 kx if [ "`echo $BASH_VERSION | cut -b1`" = "1" ]; then
5 kx echo -n "${1} " >&2 # fudge for use with bash v1
5 kx read output
5 kx else # this should work with any other /bin/sh
5 kx read -ep "${1} " output
5 kx fi
5 kx echo $output
5 kx }
5 kx
5 kx # Function to display the account info
5 kx function display () {
5 kx local goose
5 kx goose="$(echo $2 | cut -d ' ' -f 2-)" # lop off the prefixed argument useradd needs
5 kx echo -n "$1 "
5 kx # If it's null then display the 'other' information
5 kx if [ -z "$goose" -a ! -z "$3" ]; then
5 kx echo "$3"
5 kx else
5 kx echo "$goose"
5 kx fi
5 kx }
5 kx
5 kx # Function to check whether groups exist in the /etc/group file
5 kx function check_group () {
5 kx local got_error group
5 kx if [ ! -z "$@" ]; then
5 kx for group in $@ ; do
5 kx local uid_not_named="" uid_not_num=""
5 kx grep -v "$^" $gfile | awk -F: '{print $1}' | grep "^${group}$" >/dev/null 2>&1 || uid_not_named=yes
5 kx grep -v "$^" $gfile | awk -F: '{print $3}' | grep "^${group}$" >/dev/null 2>&1 || uid_not_num=yes
5 kx if [ ! -z "$uid_not_named" -a ! -z "$uid_not_num" ]; then
5 kx echo "- Group '$group' does not exist"
5 kx got_error=yes
5 kx fi
5 kx done
5 kx fi
5 kx # Return exit code of 1 if at least one of the groups didn't exist
5 kx if [ ! -z "$got_error" ]; then
5 kx return 1
5 kx fi
5 kx }
5 kx
5 kx #: Read the login name for the new user :#
5 kx #
5 kx # Remember that most Mail Transfer Agents are case independant, so having
5 kx # 'uSer' and 'user' may cause confusion/things to break. Because of this,
5 kx # useradd from shadow-4.0.3 no longer accepts usernames containing uppercase,
5 kx # and we must reject them, too.
5 kx
5 kx # Set the login variable to the command line param
5 kx echo
5 kx LOGIN="$1"
5 kx needinput=yes
5 kx while [ ! -z $needinput ]; do
5 kx if [ -z "$LOGIN" ]; then
5 kx while [ -z "$LOGIN" ]; do
5 kx LOGIN="$(get_input "Login name for new user []:")"
5 kx done
5 kx fi
5 kx grep "^${LOGIN}:" $pfile >/dev/null 2>&1 # ensure it's not already used
5 kx if [ $? -eq 0 ]; then
5 kx echo "- User '$LOGIN' already exists; please choose another"
5 kx unset LOGIN
5 kx elif [ ! -z "$( echo $LOGIN | grep "^[0-9]" )" ]; then
5 kx echo "- User names cannot begin with a number; please choose another"
5 kx unset LOGIN
5 kx elif [ ! "$LOGIN" = "`echo $LOGIN | tr A-Z a-z`" ]; then # useradd does not allow uppercase
5 kx echo "- User '$LOGIN' contains illegal characters (uppercase); please choose another"
5 kx unset LOGIN
5 kx elif [ ! -z "$( echo $LOGIN | grep '\.' )" ]; then
5 kx echo "- User '$LOGIN' contains illegal characters (period/dot); please choose another"
5 kx unset LOGIN
5 kx else
5 kx unset needinput
5 kx fi
5 kx done
5 kx
5 kx # Display the user name passed from the shell if it hasn't changed
5 kx if [ "$1" = "$LOGIN" ]; then
5 kx echo "Login name for new user: $LOGIN"
5 kx fi
5 kx
5 kx #: Get the UID for the user & ensure it's not already in use :#
5 kx #
5 kx # Whilst we _can_ allow users with identical UIDs, it's not a 'good thing' because
5 kx # when you change password for the uid, it finds the first match in /etc/passwd
5 kx # which isn't necessarily the correct user
5 kx #
5 kx echo
5 kx needinput=yes
5 kx while [ ! -z "$needinput" ]; do
5 kx _UID="$(get_input "User ID ('UID') [ defaults to next available ]:")"
5 kx egrep -v "^$|^\+" $pfile | awk -F: '{print $3}' | grep "^${_UID}$" >/dev/null 2>&1
5 kx if [ $? -eq 0 ]; then
5 kx echo "- That UID is already in use; please choose another"
5 kx elif [ ! -z "$(echo $_UID | egrep '[A-Za-z]')" ]; then
5 kx echo "- UIDs are numerics only"
5 kx else
5 kx unset needinput
5 kx fi
5 kx done
5 kx # If we were given a UID, then syntax up the variable to pass to useradd
5 kx if [ ! -z "$_UID" ]; then
5 kx U_ID="-u ${_UID}"
5 kx else
5 kx # Will we be recycling UIDs?
5 kx if [ "$recycleuids" = "yes" ]; then
5 kx U_ID="-u $(awk -F: '{uid[$3]=1} END { for (i=ENVIRON["recycleUIDMIN"];i in uid;i++);print i}' $pfile)"
5 kx fi
5 kx fi
5 kx
5 kx #: Get the initial group for the user & ensure it exists :#
5 kx #
5 kx # We check /etc/group for both the text version and the group ID number
5 kx echo
5 kx needinput=yes
5 kx while [ ! -z "$needinput" ]; do
5 kx GID="$(get_input "Initial group [ ${defgroup} ]:")"
5 kx check_group "$GID"
5 kx if [ $? -gt 0 ]; then
5 kx echo "- Please choose another"
5 kx else
5 kx unset needinput
5 kx fi
5 kx done
5 kx # Syntax the variable ready for useradd
5 kx if [ -z "$GID" ]; then
5 kx GID="-g ${defgroup}"
5 kx else
5 kx GID="-g ${GID}"
5 kx fi
5 kx
5 kx #: Get additional groups for the user :#
5 kx #
5 kx echo "Additional UNIX groups:"
5 kx echo
5 kx echo "Users can belong to additional UNIX groups on the system."
5 kx echo "For local users using graphical desktop login managers such"
5 kx echo "as XDM/KDM, users may need to be members of additional groups"
5 kx echo "to access the full functionality of removable media devices."
5 kx echo
5 kx echo "* Security implications *"
5 kx echo "Please be aware that by adding users to additional groups may"
5 kx echo "potentially give access to the removable media of other users."
5 kx echo
5 kx echo "If you are creating a new user for remote shell access only,"
5 kx echo "users do not need to belong to any additional groups as standard,"
5 kx echo "so you may press ENTER at the next prompt."
5 kx echo
5 kx needinput=yes
5 kx while [ ! -z "$needinput" ]; do
5 kx history -c
5 kx history -s "$AGID"
5 kx echo "Press ENTER to continue without adding any additional groups"
5 kx echo "Or press the UP arrow key to add/select/edit additional groups"
5 kx AGID="$(get_input ": " | sed 's/[^A-Za-z0-9 _]//g;s/ */ /g;s/^ $//g' )"
5 kx if [ ! -z "$AGID" ]; then
5 kx check_group "$AGID" # check all groups at once (treated as N # of params)
5 kx if [ $? -gt 0 ]; then
5 kx echo "- Please re-enter the group(s)"
5 kx echo
5 kx else
5 kx unset needinput # we found all groups specified
5 kx AGID="-G $(echo $AGID | tr ' ' ,)" # useradd takes comma delimited groups
5 kx fi
5 kx else
5 kx unset needinput # we don't *have* to have additional groups
5 kx fi
5 kx done
5 kx
5 kx #: Get the new user's home dir :#
5 kx #
5 kx echo
5 kx needinput=yes
5 kx while [ ! -z "$needinput" ]; do
5 kx HME="$(get_input "Home directory [ ${defhome}/${LOGIN} ]")"
5 kx if [ -z "$HME" ]; then
5 kx HME="${defhome}/${LOGIN}"
5 kx fi
5 kx # Warn the user if the home dir already exists
5 kx if [ -d "$HME" ]; then
5 kx echo "- Warning: '$HME' already exists !"
5 kx getyn="$(get_input " Do you wish to change the home directory path ? (Y/n) ")"
5 kx if [ "$(echo $getyn | grep -i "n")" ]; then
5 kx unset needinput
5 kx # You're most likely going to only do this if you have the dir *mounted* for this user's $HOME
5 kx getyn="$(get_input " Do you want to chown $LOGIN.$( echo $GID | awk '{print $2}') $HME ? (y/N) ")"
5 kx if [ "$(echo $getyn | grep -i "y")" ]; then
5 kx CHOWNHOMEDIR=$HME # set this to the home directory
5 kx fi
5 kx fi
5 kx else
5 kx unset needinput
5 kx fi
5 kx done
5 kx HME="-d ${HME}"
5 kx
5 kx #: Get the new user's shell :#
5 kx echo
5 kx needinput=yes
5 kx while [ ! -z "$needinput" ]; do
5 kx unset got_error
5 kx SHL="$(get_input "Shell [ ${defshell} ]")"
5 kx if [ -z "$SHL" ]; then
5 kx SHL="${defshell}"
5 kx fi
5 kx # Warn the user if the shell doesn't exist in /etc/shells or as a file
5 kx if [ -z "$(grep "^${SHL}$" $sfile)" ]; then
5 kx echo "- Warning: ${SHL} is not in ${sfile} (potential problem using FTP)"
5 kx got_error=yes
5 kx fi
5 kx if [ ! -f "$SHL" ]; then
5 kx echo "- Warning: ${SHL} does not exist as a file"
5 kx got_error=yes
5 kx fi
5 kx if [ ! -z "$got_error" ]; then
5 kx getyn="$(get_input " Do you wish to change the shell ? (Y/n) ")"
5 kx if [ "$(echo $getyn | grep -i "n")" ]; then
5 kx unset needinput
5 kx fi
5 kx else
5 kx unset needinput
5 kx fi
5 kx done
5 kx SHL="-s ${SHL}"
5 kx
5 kx #: Get the expiry date :#
5 kx echo
5 kx needinput=yes
5 kx while [ ! -z "$needinput" ]; do
5 kx EXP="$(get_input "Expiry date (YYYY-MM-DD) []:")"
5 kx if [ ! -z "$EXP" ]; then
5 kx # Check to see whether the expiry date is in the valid format
5 kx if [ -z "$(echo "$EXP" | grep "^[[:digit:]]\{4\}[-]\?[[:digit:]]\{2\}[-]\?[[:digit:]]\{2\}$")" ]; then
5 kx echo "- That is not a valid expiration date"
5 kx else
5 kx unset needinput
5 kx EXP="-e ${EXP}"
5 kx fi
5 kx else
5 kx unset needinput
5 kx fi
5 kx done
5 kx
5 kx # Display the info about the new impending account
5 kx echo
5 kx echo "New account will be created as follows:"
5 kx echo
5 kx echo "---------------------------------------"
5 kx display "Login name.......: " "$LOGIN"
5 kx display "UID..............: " "$_UID" "[ Next available ]"
5 kx display "Initial group....: " "$GID"
5 kx display "Additional groups: " "$AGID" "[ None ]"
5 kx display "Home directory...: " "$HME"
5 kx display "Shell............: " "$SHL"
5 kx display "Expiry date......: " "$EXP" "[ Never ]"
5 kx echo
5 kx
5 kx echo "This is it... if you want to bail out, hit Control-C. Otherwise, press"
5 kx echo "ENTER to go ahead and make the account."
5 kx read junk
5 kx
5 kx echo
5 kx echo "Creating new account..."
5 kx echo
5 kx echo
5 kx
5 kx # Add the account to the system
5 kx CMD="$useradd "$HME" -m "$EXP" "$U_ID" "$GID" "$AGID" "$SHL" "$LOGIN""
5 kx $CMD
5 kx
5 kx if [ $? -gt 0 ]; then
5 kx echo "- Error running useradd command -- account not created!"
5 kx echo "(cmd: $CMD)"
5 kx exit 1
5 kx fi
5 kx
5 kx # chown the home dir? We can only do this once the useradd has
5 kx # completed otherwise the user name doesn't exist.
5 kx if [ ! -z "${CHOWNHOMEDIR}" ]; then
5 kx chown "$LOGIN"."$( echo $GID | awk '{print $2}')" "${CHOWNHOMEDIR}"
5 kx fi
5 kx
5 kx # Set the finger information
5 kx $chfn "$LOGIN"
5 kx while [ $? -gt 0 ]; do
5 kx echo "- Warning: an error occurred while setting finger information."
5 kx echo " Please try again."
5 kx $chfn "$LOGIN"
5 kx done
5 kx
5 kx # Set a password
5 kx $passwd "$LOGIN"
5 kx while [ $? -gt 0 ]; do
5 kx echo "- Warning: An error occured while setting the password for"
5 kx echo " this account. Please try again."
5 kx $passwd "$LOGIN"
5 kx done
5 kx
5 kx # If it was created (it should have been!), set the permissions for that user's dir
5 kx HME="$(echo "$HME" | awk '{print $2}')" # We have to remove the -g prefix
5 kx if [ -d "$HME" ]; then
5 kx $chmod $defchmod "$HME"
5 kx fi
5 kx
5 kx echo
5 kx echo
5 kx echo "Account setup complete."
5 kx exit 0