Commit 9649237c authored by Jondy Zhao's avatar Jondy Zhao

useradd: support many other groups; try to grp_name if name doesn't exist.

parent d097597a
...@@ -62,7 +62,7 @@ fi ...@@ -62,7 +62,7 @@ fi
# Creates a new (unprivileged) user as specified by $1. # Creates a new (unprivileged) user as specified by $1.
# Useful for running services that do not require elevated privileges, # Useful for running services that do not require elevated privileges,
# or running servers like sshd in "privilege separation" mode. # or running servers like sshd in "privilege separation" mode.
# #
# Exits on catastrophic error # Exits on catastrophic error
# Returns 0 on total success # Returns 0 on total success
# Returns 1 on failure # Returns 1 on failure
...@@ -135,7 +135,7 @@ function create_unprivileged_user() ...@@ -135,7 +135,7 @@ function create_unprivileged_user()
# Routine: add_member_to_group # Routine: add_member_to_group
# Add a member $1 to a group $2, there is a special for slapos: # Add a member $1 to a group $2, there is a special for slapos:
# if $2 is not a group name, we'll try grp_$2 as group name # if $2 is not a group name, we'll try grp_$2 as group name
# #
# Exits on catastrophic error # Exits on catastrophic error
# Returns 0 on total success # Returns 0 on total success
# Returns 1 on failure # Returns 1 on failure
...@@ -150,10 +150,11 @@ function add_member_to_group() ...@@ -150,10 +150,11 @@ function add_member_to_group()
{ {
local name=$1 local name=$1
local grpname=$2 local grpname=$2
net localgroup $grpname > /dev/null 2>&1 || grpname="grp_$grpname"
net localgroup $grpname > /dev/null 2>&1 || return 1
if ! net localgroup $grpname | grep -q -e "^$name$" ; then if ! net localgroup $grpname | grep -q -e "^$name$" ; then
net localgroup $grpname $name /add net localgroup $grpname $name /add
fi fi
net localgroup $grpname | grep -q -e "^$name$"
} # === End of add_member_to_group() === # } # === End of add_member_to_group() === #
while getopts "Dd:g:G:p:s:r" opt ; do while getopts "Dd:g:G:p:s:r" opt ; do
...@@ -165,7 +166,7 @@ while getopts "Dd:g:G:p:s:r" opt ; do ...@@ -165,7 +166,7 @@ while getopts "Dd:g:G:p:s:r" opt ; do
USER_INIT_GROUP=$OPTARG USER_INIT_GROUP=$OPTARG
;; ;;
G) G)
USER_OTHER_GROUP=$OPTARG USER_OTHER_GROUP="$USER_OTHER_GROUP $OPTARG"
;; ;;
p) p)
USER_PASSWORD=$OPTARG USER_PASSWORD=$OPTARG
...@@ -194,11 +195,11 @@ create_unprivileged_user ${USER_NAME} $USER_HOME || ...@@ -194,11 +195,11 @@ create_unprivileged_user ${USER_NAME} $USER_HOME ||
(echo "Failed to create user ${USER_NAME}" ; exit 1) (echo "Failed to create user ${USER_NAME}" ; exit 1)
for grpname in ${USER_INIT_GROUP} ${USER_OTHER_GROUP} ; do for grpname in ${USER_INIT_GROUP} ${USER_OTHER_GROUP} ; do
[[ -z "$grpname" ]] && continue [[ -z "$grpname" ]] && continue
add_member_to_group ${USER_NAME} $grpname || add_member_to_group ${USER_NAME} $grpname ||
(echo "Failed to add ${USER_NAME} to group $group" ; exit 1) (echo "Failed to add ${USER_NAME} to group $group" ; exit 1)
done done
# net user "${USER_NAME}" /delete || # net user "${USER_NAME}" /delete ||
# (echo "Faild to delete user ${USER_NAME}" ; exit 1) # (echo "Faild to delete user ${USER_NAME}" ; exit 1)
# sed -i -e "/^${USER_NAME}/d" /etc/passwd # sed -i -e "/^${USER_NAME}/d" /etc/passwd
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment