-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.sh
69 lines (56 loc) · 1.59 KB
/
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
#!/usr/bin/env bash
shlibs.user.groupname_from_gid() {
local gid="${1}"
getent group "${gid}" | cut -d : -f 1
}
shlibs.user.username_from_uid() {
local HOST_UID="${1:-0}"
getent passwd $HOST_UID | cut -d : -f 1
}
shlibs.user.add() {
local uid="${1:-1000}"
local gid="${2:-1000}"
local username="${3:-horst}"
local shell="${4:-/bin/sh}"
distribution_id=$(shlibs.os.distribution_id)
log_info "creating new user: $uid $gid $username $shell"
if [[ $(getent passwd "${uid}") ]]; then
log_warn "User '${uid}' still exists as '$(getent passwd "${uid}")'"
return
fi
if [ $(getent group "${gid}") ]; then
log_warn "Group '${gid}' still exists"
return
fi
case "$(shlibs.os.type.base)" in
linux)
shlibs.user.add.linux $uid $gid $username $shell
;;
*)
log_error "Unknown os detected."
;;
esac
}
shlibs.user.add.linux() {
local uid="${1:-1000}"
local gid="${2:-1000}"
local username="${3:-horst}"
local shell="${4:-/bin/sh}"
addgroup --gid "${gid}" "${username}"
groupname=$(shlibs.user.groupname_from_gid "${gid}")
if [ -z "${shell}" ]; then
adduser --uid "${uid}" --disabled-password --ingroup "${groupname}" --gecos "" "${username}"
else
adduser --uid "${uid}" --disabled-password --ingroup "${groupname}" --gecos "" "${username}" --shell "${shell}"
fi
}
# old versions
add_user() {
shlibs.user.add "$@"
}
get_username_from_uid() {
shlibs.user.username_from_uid "$@"
}
get_groupname() {
shlibs.user.groupname_from_gid "$@"
}