-
Notifications
You must be signed in to change notification settings - Fork 4
/
add-session-user.sh
75 lines (61 loc) · 2.1 KB
/
add-session-user.sh
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
67
68
69
70
71
72
73
74
75
#!/bin/sh
export USER_CONFIG_PATH=${USER_CONFIG_PATH:-'/userconfig'}
export USER_DATA_PATH=${USER_DATA_PATH:-'/userdata'}
export USER_SUDO=${USER_SUDO:-false}
terminate=false
if [ "x$SESSION_USER" = "x" ]; then
>&2 echo "[$(date)][ERROR] No SESSION_USER specified."
terminate=true
fi
if [ ! -d "$USER_CONFIG_PATH" ]; then
>&2 echo "[$(date)][ERROR] No User Config directory found at $USER_CONFIG_PATH."
terminate=true
fi
if [ ! -d "$USER_DATA_PATH" ]; then
>&2 echo "[$(date)][ERROR] No User Data directory found at $USER_DATA_PATH."
terminate=true
fi
if [ $terminate = true ]; then
exit 1
fi
if [ ! -f "$USER_CONFIG_PATH/uid" ]; then
>&2 echo "[$(date)][ERROR] uid not found at $USER_CONFIG_PATH/uid."
terminate=true
else
uid=$(cat "$USER_CONFIG_PATH/uid")
fi
if [ ! -f "$USER_CONFIG_PATH/gid" ]; then
>&2 echo "[$(date)][ERROR] gid not found at $USER_CONFIG_PATH/gid."
terminate=true
else
gid=$(cat "$USER_CONFIG_PATH/gid")
fi
if [ $terminate = true ]; then
exit 1
fi
if [ ! -f "$USER_CONFIG_PATH/home" ]; then
>&2 echo "[$(date)][WARNING] User home not specified. Defaulting to /home."
home="/home"
else
home=$(cat "$USER_CONFIG_PATH/home")
fi
if [ ! -f "$USER_CONFIG_PATH/shell" ]; then
>&2 echo "[$(date)][WARNING] User shell not specified. Defaulting to /bin/sh."
shell="/bin/sh"
else
shell=$(cat "$USER_CONFIG_PATH/shell")
fi
cp /etc/passwd "$USER_DATA_PATH/passwd"
cp /etc/group "$USER_DATA_PATH/group"
if grep -q -E "$SESSION_USER:x:" "$USER_DATA_PATH/passwd"; then
>&2 echo "[$(date)][WARNING] $SESSION_USER already found in local passwd file. User will not be added."
else
echo "[$(date)][INFO] Adding \"$SESSION_USER:x:$uid:$gid:$SESSION_USER:$home:$shell\" to passwd file."
echo "$SESSION_USER:x:$uid:$gid:$SESSION_USER:$home:$shell" >> "$USER_DATA_PATH/passwd"
fi
if grep -q -E "^$SESSION_USER:x:" "$USER_DATA_PATH/group"; then
>&2 echo "[$(date)][WARNING] $SESSION_USER group already found in local group file. Group will not be added."
else
echo "[$(date)][INFO] Adding \"$SESSION_USER:x:$gid:\" to group file."
echo "$SESSION_USER:x:$gid:" >> "$USER_DATA_PATH/group"
fi