From 368d5b62c33d14ed619b22bae7e09e2973fdf627 Mon Sep 17 00:00:00 2001 From: Michael Pereira Neves Date: Mon, 10 Nov 2014 11:25:53 -0200 Subject: [local-account] code review --- .../opt/openslx/scripts/systemd-create_users | 73 ++++++++++++---------- 1 file changed, 41 insertions(+), 32 deletions(-) diff --git a/server/modules/local_accounts/opt/openslx/scripts/systemd-create_users b/server/modules/local_accounts/opt/openslx/scripts/systemd-create_users index 3ac554b5..5cabd4fd 100755 --- a/server/modules/local_accounts/opt/openslx/scripts/systemd-create_users +++ b/server/modules/local_accounts/opt/openslx/scripts/systemd-create_users @@ -2,56 +2,65 @@ . /opt/openslx/config || { echo "Could not source config!"; exit 23; } +#generate new user with useradd and insert password in /etc/shadow if exists create_user(){ -# generate the new lines that will be merged into the /etc/{passwd,shadow,group} files - unset IFS - if useradd -s /bin/bash -m $1 -K UID_MIN=1000 -K GID_MIN=1000; then # if the users doesn't exists, create him without the password - if [ -n "$2" ]; then - sed -i "s#^$1:[^:]*:#$1:$2:#" "/etc/shadow" # set the password in the /etc/shadow file - fi + local username="$1" + local password="$2" + uset IFS + + #if the users doesn't exists, create him without the password + if useradd -s /bin/bash -m "$username" -K UID_MIN=1000 -K GID_MIN=1000; then + if [ -n "$password" ]; then + #set the password in the /etc/shadow file + sed -i "s#^${username}:[^:]*:#${username}:${password}:#" "/etc/shadow" + fi else - echo 'user $1 already exists' - if [ -n "$2" ] && [ $(grep ^$1: /etc/shadow | cut -d ':' -f2) != $2 ]; then # if the user already exists, check if the password has changed - echo "changing password to the new one" - sed -i "s#^$1:[^:]*:#$1:$2:#" "/etc/shadow" # set the password in the /etc/shadow file + echo 'user ${username} already exists' + #if the user already exists, check if the password has changed + if [ -n "$password" ] && [ $(grep ^${username}: /etc/shadow | cut -d ':' -f2) != "$password" ]; then + echo "User password changed, updating /etc/shadow to new one" + #set the password in the /etc/shadow file + sed -i "s#^${username}:[^:]*:#${username}:${password}:#" "/etc/shadow" fi fi } -mount | grep "/home" > /dev/null -if [ $? -eq 0 ]; then - # check if the /home partition exists +# check if the /home partition exists +if mount | grep "/home" > /dev/null; then echo "/home partition found" - if useradd -s /bin/bash -m openslx -K UID_MIN=1000 -K GID_MIN=1000; then # try to create the 'openslx' user, will fail if it already exists + + #try to create the 'openslx' user in whose home dir backups and patch files will be stored + if useradd -s /bin/bash -m openslx -K UID_MIN=1000 -K GID_MIN=1000; then echo "user openslx created" - sed -i "s#^openslx:[^:]*:#openslx:$OPENSLX_PASS:#" "/etc/shadow" # set the password in the /etc/shadow file + #set the password in the /etc/shadow file + sed -i "s#^openslx:[^:]*:#openslx:$OPENSLX_PASS:#" "/etc/shadow" fi - for line in $SLX_USERS; do # create the accounts specified in the SLX_USERS config. - IFS=, - set $line + #create the accounts specified in the SLX_USERS config. + for line in $SLX_USERS; do + IFS=, + set $line create_user $1 $2 done - + + #patch passwd, shadow and group with changes the local admin made in that machine for file in passwd shadow group; do #backup files before patching to save slxbox state - echo "Backing up /etc/$file at /home/openslx/.$file.backup" - cp /etc/$file /home/openslx/.$file.backup + echo "Backing up /etc/${file} at /home/openslx/.${file}.backup" + cp /etc/${file} /home/openslx/.${file}.backup - # apply patch of users created by the admin in the last session. - if [ -e /home/openslx/.$file.patch ]; then - patch /etc/$file < /home/openslx/.$file.patch + #apply patch of users created by the admin in the last session. + if [ -e /home/openslx/.${file}.patch ]; then + patch /etc/${file} < /home/openslx/.${file}.patch fi done -else # if no /home partition was found, will create the user but won't do the patch and backup. + +else + #if no /home partition was found, will create the user but won't do the patch and backup. echo "No /home partition found on hdd. Creating non permanent users from slxbox." - if useradd -s /bin/bash -m openslx -K UID_MIN=1000 -K GID_MIN=1000; then # try to create the 'openslx' user, will fail if it already exists - echo "user openslx created" - sed -i "s#^openslx:[^:]*:#openslx:$OPENSLX_PASS:#" "/etc/shadow" # set the password in the /etc/shadow file - fi for line in $SLX_USERS; do # create the accounts specified in the SLX_USERS config. - IFS=, - set $line + IFS=, + set $line create_user $1 $2 done -fi \ No newline at end of file +fi -- cgit v1.2.3-55-g7522