summaryrefslogtreecommitdiffstats
path: root/server/modules/local_accounts/opt/openslx/scripts/systemd-create_users
blob: 3ac554b5a04644d567e8ae2ae31f6e0945e3e590 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/ash

. /opt/openslx/config || { echo "Could not source config!"; exit 23; }

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
  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
    fi
  fi
}

mount | grep "/home" > /dev/null
if [ $? -eq 0 ]; then
  # check if the /home partition exists
  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
    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
    create_user $1 $2
  done
  
  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

    # 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.
  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
    create_user $1 $2
  done
fi