-
Notifications
You must be signed in to change notification settings - Fork 7
/
linux-adduser
executable file
·218 lines (209 loc) · 6.7 KB
/
linux-adduser
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env bash
set -euo pipefail
YP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)"
source ${YP_DIR}/sh/common.inc.sh
# Debian/Ubuntu --help:
#
# adduser [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID]
# [--firstuid ID] [--lastuid ID] [--gecos GECOS] [--ingroup GROUP | --gid ID]
# [--disabled-password] [--disabled-login] [--add_extra_groups] USER
# Add a normal user
#
# adduser --system [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID]
# [--gecos GECOS] [--group | --ingroup GROUP | --gid ID] [--disabled-password]
# [--disabled-login] [--add_extra_groups] USER
# Add a system user
#- linux-adduser 1.0
## Usage: linux-adduser [OPTION] -- USER
## Add a normal or system user cross-platform.
## Arguments are a subset of the Debian/Ubuntu 'adduser'.
##
## --system Add sytem user instead. Normal user by default.
## --home Home directory.
## --shell Default shell.
## --no-create-home ?? Don't create the home directory if it doesn't exist.
## --uid User ID.
## --firstuid ??
## --lastuid ??
## --gecos Gecos field. Defaults to 'Linux user'.
## --group Primary group's name.
## --ingroup Primary group's name.
## --gid Primary group's ID.
## --disabled-password Disable password.
## --disabled-login ?? Disable login.
## --add_extra_groups ??
## --force-badname Don't check for bad names.
##
## -h, --help Display this help and exit
## -v, --version Output version information and exit
SYSTEM=
HOME=
SHELL=
# NO_CREATE_HOME=
# UID might be read-only
USER_ID=
# FIRSTUID=
# LASTUID=
GECOS=
INGROUP=
# GID might be read-only
GROUP_ID=
DISABLED_PASSWORD=
# DISABLED_LOGIN=
# ADD_EXTRA_GROUPS=
FORCE_BADNAME=
USER=
if { getopt --test >/dev/null 2>&1 && false; } || [[ "$?" = "4" ]] || false; then
ARGS=$(getopt -o hv -l help,version,system,home:,shell:,uid:,gecos:,group:,ingroup:,gid:,disabled-password,force-badname -n $(basename ${BASH_SOURCE[0]}) -- "$@") || sh_script_usage # editorconfig-checker-disable-line
eval set -- "${ARGS}"
fi
while [[ $# -gt 0 ]]; do
case "$1" in
--system)
SYSTEM=true
shift
;;
--home)
HOME=$2
shift 2
;;
--shell)
SHELL=$2
shift 2
;;
# --no-create-home)
# NO_CREATE_HOME=true
# shift
# ;;
--uid)
USER_ID=$2
shift 2
;;
# --firstuid)
# FIRSTUID=$2
# shift 2
# ;;
# --lastuid)
# LASTUID=$2
# shift 2
# ;;
--gecos)
GECOS=$2
shift 2
;;
--group|--ingroup)
INGROUP=$2
shift 2
;;
--gid)
GROUP_ID=$2
shift 2
;;
--disabled-password)
DISABLED_PASSWORD=true
shift
;;
# --disabled-login)
# DISABLED_LOGIN=true
# shift
# ;;
# --add_extra_groups)
# ADD_EXTRA_GROUPS=true
# shift
# ;;
--force-badname)
FORCE_BADNAME=true
shift
;;
-h|--help)
sh_script_usage
;;
-v|--version)
sh_script_version
;;
--)
shift
break
;;
-*)
sh_script_usage
;;
*)
break
;;
esac
done
# [[ $# -eq 0 ]] || sh_script_usage
USER="$1"
[[ -n "${USER}" ]] || {
echo_err "Please provide a USER name."
exit 1
}
case ${OS_SHORT}-${OS_RELEASE_ID}-${OS_RELEASE_VERSION_ID} in
linux-alpine-*)
ARGS=()
[[ "${SYSTEM}" != "true" ]] || ARGS+=("-S")
[[ -z "${HOME}" ]] || ARGS+=("-h" "${HOME}")
[[ -z "${SHELL}" ]] || ARGS+=("-s" "${SHELL}")
[[ -z "${USER_ID}" ]] || ARGS+=("-u" "${USER_ID}")
# [[ -z "${GECOS}" ]] || ARGS+=("-g" "${GECOS}")
ARGS+=("-g" "${GECOS}")
[[ -z "${INGROUP}" ]] || ARGS+=("-G" "${INGROUP}")
[[ -z "${GROUP_ID}" ]] || {
[[ -z "${INGROUP}" ]] || {
echo_err "Both '--gid ${GROUP_ID}' and '--group | --ingroup ${INGROUP}' given."
exit 1
}
ARGS+=("-G" "$(getenv group ${GROUP_ID} | cut -d":" -f1)")
}
[[ "${DISABLED_PASSWORD}" != "true" ]] || ARGS+=("-D")
[[ "${FORCE_BADNAME}" != "true" ]] || \
echo_warn "'--force-badname' is not supported. Trying anyway."
ARGS+=("${USER}")
exe ${YP_SUDO:-} /usr/sbin/adduser "${ARGS[@]}"
;;
linux-arch-*|linux-amzn-*|linux-centos-*|linux-rhel-*)
ARGS=()
ARGS+=("--create-home")
[[ "${SYSTEM}" != "true" ]] || ARGS+=("--system")
[[ -z "${HOME}" ]] || ARGS+=("--home-dir" "${HOME}")
[[ -z "${SHELL}" ]] || ARGS+=("--shell" "${SHELL}")
[[ -z "${USER_ID}" ]] || ARGS+=("--uid" "${USER_ID}")
# [[ -z "${GECOS}" ]] || ARGS+=("--comment" "${GECOS}")
ARGS+=("--comment" "${GECOS}")
[[ -z "${INGROUP}" ]] || ARGS+=("--gid" "${INGROUP}")
[[ -z "${GROUP_ID}" ]] || ARGS+=("--gid" "${GROUP_ID}")
[[ "${FORCE_BADNAME}" != "true" ]] || ARGS+=("--badnames")
ARGS+=("${USER}")
exe ${YP_SUDO:-} /usr/sbin/useradd "${ARGS[@]}"
[[ "${DISABLED_PASSWORD}" != "true" ]] || {
if [[ -x /usr/bin/passwd ]]; then
exe ${YP_SUDO:-} /usr/bin/passwd -l "${USER}"
elif [[ -x /usr/sbin/passwd ]]; then
exe ${YP_SUDO:-} /usr/sbin/passwd -l "${USER}"
else
echo_err "No /usr/bin/passwd nor /usr/sbin/passwd available."
exit 1
fi
}
;;
linux-debian-*|linux-ubuntu-*)
ARGS=()
[[ "${SYSTEM}" != "true" ]] || ARGS+=("--system")
[[ -z "${HOME}" ]] || ARGS+=("--home" "${HOME}")
[[ -z "${SHELL}" ]] || ARGS+=("--shell" "${SHELL}")
[[ -z "${USER_ID}" ]] || ARGS+=("--uid" "${USER_ID}")
# [[ -z "${GECOS}" ]] || ARGS+=("--gecos" "${GECOS}")
ARGS+=("--gecos" "${GECOS}")
[[ -z "${INGROUP}" ]] || ARGS+=("--ingroup" "${INGROUP}")
[[ -z "${GROUP_ID}" ]] || ARGS+=("--gid" "${GROUP_ID}")
[[ "${DISABLED_PASSWORD}" != "true" ]] || ARGS+=("--disabled-password")
[[ "${FORCE_BADNAME}" != "true" ]] || ARGS+=("--force-badname")
ARGS+=("${USER}")
exe ${YP_SUDO:-} /usr/sbin/adduser "${ARGS[@]}"
;;
*)
echo_err "${OS_SHORT}-${OS_RELEASE_ID}-${OS_RELEASE_VERSION_ID} is an unsupported OS for adding a user."
exit 1
;;
esac