-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain
More file actions
executable file
·64 lines (52 loc) · 1.87 KB
/
main
File metadata and controls
executable file
·64 lines (52 loc) · 1.87 KB
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
#!/usr/bin/env bash
test -z "$(type -p)" || { echo bash required; exit 1; }
set -Eeuo pipefail
source "${RRCONFINC}"
# command line argument, if specified, has preference
test $# -eq 1 && ADDUSER_NAME="${1}"
: "${ADDUSER_NAME:-}"
if [[ -z "${ADDUSER_NAME}" ]]; then
fail "Missing username to add."
fi
# defaults
: "${ADDUSER_UIDFORCE:=}"
: "${ADDUSER_UID:=}"
: "${ADDUSER_GIDFORCE:=}"
: "${ADDUSER_GID:=}"
: "${ADDUSER_GROUP:=}"
: "${ADDUSER_SHELL:="/bin/bash"}"
: "${ADDUSER_GECOS:="${ADDUSER_NAME}"}"
: "${ADDUSER_HOME:="/home/${ADDUSER_NAME}"}"
# if forced, ensure requested UID/GID has forced value
test -n "${ADDUSER_UIDFORCE}" &&
ADDUSER_UID="${ADDUSER_UIDFORCE}"
test -n "${ADDUSER_GIDFORCE}" &&
ADDUSER_GID="${ADDUSER_GIDFORCE}"
# determine default group and GID
DEFAULT_GROUP="${ADDUSER_GROUP:-${ADDUSER_NAME}}"
DEFAULT_GID="${ADDUSER_GID:-}"
getent passwd "${ADDUSER_NAME}" >/dev/null 2>&1 || {
# the user does not exist
test -z "${ADDUSER_UID}" &&
# no UID requested - add by name
exec adduser "${ADDUSER_NAME}" \
--shell "${ADDUSER_SHELL}" \
--gecos "${ADDUSER_GECOS}" \
--home "${ADDUSER_HOME}" \
--disabled-password \
${DEFAULT_GID:+--gid "${DEFAULT_GID}"} # use GID if provided
# use requested UID to create user
exec adduser --uid "${ADDUSER_UID}" "${ADDUSER_NAME}" \
--shell "${ADDUSER_SHELL}" \
--gecos "${ADDUSER_GECOS}" \
--home "${ADDUSER_HOME}" \
--disabled-password \
${DEFAULT_GID:+--gid "${DEFAULT_GID}"} # use GID if provided
}
# user exists
test -z "${ADDUSER_UID}" && exit 0 # no UID requested or forced
currentuid="$(getent passwd "${ADDUSER_NAME}" | awk -F: '{print$3}')"
test "${ADDUSER_UID}" -eq "${currentuid}" && exit 0
logv "User ${ADDUSER_NAME} already exists with UID=${currentuid}"
test -z "${ADDUSER_UIDFORCE}" && exit 0
fail "Cannot change user ${ADDUSER_NAME} UID to ${ADDUSER_UIDFORCE}"