summaryrefslogtreecommitdiffstats
path: root/server/modules/local_accounts/opt/openslx/scripts/systemd-local_accounts
blob: 5cabd4fd4318e1bf0fab98d055b4b37c75ca11f7 (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
58
59
60
61
62
63
64
65
66
#!/bin/ash

. /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(){
  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 ${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
}

# check if the /home partition exists
if mount | grep "/home" > /dev/null; then
  echo "/home partition found"

  #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"
    #set the password in the /etc/shadow file
    sed -i "s#^openslx:[^:]*:#openslx:$OPENSLX_PASS:#" "/etc/shadow"
  fi

  #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

    #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."
  for line in $SLX_USERS; do 	# create the accounts specified in the SLX_USERS config.
    IFS=,
    set $line
    create_user $1 $2
  done
fi