diff options
-rw-r--r-- | remote/includes/useradd.inc | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/remote/includes/useradd.inc b/remote/includes/useradd.inc index 29a9c39a..5d7d405d 100644 --- a/remote/includes/useradd.inc +++ b/remote/includes/useradd.inc @@ -39,6 +39,24 @@ generate_uid() echo ${_UID} } +# Echo gid of given user if existent, nothing otherwise +get_gid_for_user() +{ + [ $# -ne 1 ] && perror "get_gid_for_user fail. want 1 argument." + [ -z "${_PASSWD}" ] && perror "passwd file not set." + local _GID=$(grep -E "^$1:[^:]*:[^:]*:[0-9]+:" "${_PASSWD}" | head -1 | awk -F ':' '{print $3}') + echo ${_GID} +} + +# Echo group name of given gid, nothing if non-existent +get_group_for_gid() +{ + [ $# -ne 1 ] && perror "get_group_for_gid fail. want 1 argument." + [ -z "${_GROUP}" ] && perror "group file not set." + local _NAME=$(grep -E "^[^:]*:[^:]*:$1:" "${_GROUP}" | head -1 | awk -F ':' '{print $1}') + echo ${_NAME} +} + # Generate a GID for a given GROUPNAME. Return existing GID if possible, generate new one otherwise generate_gid() { @@ -64,7 +82,7 @@ generate_gid() add_user() { [ -z "${TARGET_BUILD_DIR}" ] && perror "add_user: TARGET_BUILD_DIR not set" - if [ -z $USER -a $# -eq 0 ] + if [ -z "${USER}" -a $# -eq 0 ] then pwarning " ** add_user usage **" pwarning "add_user <username>" @@ -82,7 +100,7 @@ add_user() { if [ "x$1" != "x" ] then local USER=$1 - local GROUP=$1 + local GROUP="" local USERID="" local GROUPID="" local USERHOME="" @@ -93,7 +111,9 @@ add_user() { if ! [[ $USER =~ $NAME_REGEX ]]; then perror "Invalid username: $USER" fi - [ "x$GROUP" = "x" ] && local GROUP=$USER + [ -z "$GROUPID" ] && local GROUPID=$(get_gid_for_user "${USER}") + [ -z "$GROUP" -a -n "$GROUPID" ] && local GROUP=$(get_group_for_gid "${GROUPID}") + [ -z "$GROUP" ] && local GROUP=$USER GROUP=$(trim "$GROUP") if ! [[ $GROUP =~ $NAME_REGEX ]]; then perror "Invalid group: $GROUP" @@ -101,16 +121,16 @@ add_user() { [ "x$USERID" = "x" ] && local USERID=$(generate_uid "${USER}") USERID=$(trim "$USERID") [ "$USERID" -lt "0" -o "$USERID" -gt "65535" ] && perror "Invalid userid: $USERID" - [ "x$GROUPID" = "x" ] && local GROUPID=$(generate_gid "${GROUP}" "${USERID}") + [ -z "$GROUPID" ] && local GROUPID=$(generate_gid "${GROUP}" "${USERID}") GROUPID=$(trim "$GROUPID") [ "$GROUPID" -lt "0" -o "$GROUPID" -gt "65535" ] && perror "Invalid groupid: $GROUPID" # all required variables have been set # does the desired username already exist? if so, check if UID matches, otherwise bail out local _UID=$(grep -E "^${USER}:[^:]*:[0-9]+:" "${_PASSWD}" | head -1 | awk -F ':' '{print $3}') - [ ! -z "${_UID}" ] && [ "x${_UID}" != "x${USERID}" ] && perror "User ${USER}(${USERID}) already exists with UID ${_UID}" + [ -n "${_UID}" ] && [ "x${_UID}" != "x${USERID}" ] && perror "User ${USER}(${USERID}) already exists with UID ${_UID}" # do the same for the group local _GID=$(grep -E "^${GROUP}:[^:]*:[0-9]+:" "${_GROUP}" | head -1 | awk -F ':' '{print $3}') - [ ! -z "${_GID}" ] && [ "x${_GID}" != "x${GROUPID}" ] && perror "Group ${GROUP}(${GROUPID}) already exists with GID ${_GID}" + [ -n "${_GID}" ] && [ "x${_GID}" != "x${GROUPID}" ] && perror "Group ${GROUP}(${GROUPID}) already exists with GID ${_GID}" # if user already exists, check if he is in another group than the one requested. if so, bail out # (TODO: don't bail out and add user to the new group) if [ ! -z "${_UID}" ] @@ -122,12 +142,12 @@ add_user() { if [ -z "${_UID}" ] then local _TEST=$(grep -E "^[^:]+:[^:]*:${USERID}:" "${_PASSWD}") - [ ! -z "${_TEST}" ] && perror "Cannot add $USER - desired UID $USERID already in use." + [ -n "${_TEST}" ] && perror "Cannot add $USER - desired UID $USERID already in use." fi if [ -z "${_GID}" ] then local _TEST=$(grep -E "^[^:]+:[^:]*:${GROUPID}:" "${_GROUP}") - [ ! -z "${_TEST}" ] && perror "Cannot add $GROUP - desired GID $GROUPID already in use." + [ -n "${_TEST}" ] && perror "Cannot add $GROUP - desired GID $GROUPID already in use." fi [ -z "${USERHOME}" ] && local USERHOME=/nonexistent [ -z "${USERSHELL}" ] && local USERSHELL=/bin/false |