-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch_user
executable file
·48 lines (42 loc) · 1.28 KB
/
switch_user
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
#!/usr/bin/env sh
set -e
DYNAMIC_USER='docker_alias_dyn_user'
switchToDynamicUser() {
userExists() {
id "$1" > /dev/null 2>&1
}
if [ "$UID_HOST" = "" ]; then
print "UID_HOST not present!"
fi
USERNAME="$1"
if userExists "$UID_HOST"; then
modifyUser "$(id -nu "$UID_HOST")" 978654321 978654321
modifyUser "$USERNAME" "$UID_HOST" "$UID_HOST"
else
HOME="/home/$DYNAMIC_USER"
mkdir "$HOME"
useradd "$USERNAME" -d "$HOME" --no-log-init -u "$UID_HOST"
chown -R $USERNAME:$USERNAME $HOME
fi
}
modifyUser() {
_USERNAME=$1
_NEWUID=$2
_NEWGID=$3
if ! userExists "$_USERNAME"; then
echo "User does not exist: '$_USERNAME'"
exit 1
fi
_GROUPNAME=$(id -gn "$_USERNAME")
_OLD_UID=$(id -u "$_USERNAME")
_OLD_GID=$(id -g "$_USERNAME")
_HOME="$(eval echo ~_USERNAME)"
userdel "$_USERNAME"
useradd "$_USERNAME" -d "$_HOME" --no-log-init -u "$_NEWUID"
groupmod -g "$_NEWGID" "$_GROUPNAME"
find "$_HOME" -ignore_readdir_race -user "$_OLDUID" -exec chown -h "$_NEWUID" {} \;
find "$_HOME" -ignore_readdir_race -group "$_OLDGID" -exec chgrp -h "$_NEWGID" {} \;
usermod -g "$_NEWGID" "$_USERNAME"
}
switchToDynamicUser $DYNAMIC_USER
runuser -u "$DYNAMIC_USER" -- $@