Use gids for id management and isolate home directories.
This commit is contained in:
parent
5cbdab9da8
commit
5ec220d0a6
1 changed files with 38 additions and 5 deletions
|
|
@ -15,28 +15,45 @@ if [ ! -w "$ttyPath" ]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Create fd for the tty in variable con
|
||||||
if ! exec {con}<>"$ttyPath"; then
|
if ! exec {con}<>"$ttyPath"; then
|
||||||
echo >&2 "Cannot open device $ttyPath"
|
echo >&2 "Cannot open device $ttyPath"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Temporary file for logging error messages, clear tty and signal ready
|
||||||
temperr=$(mktemp)
|
temperr=$(mktemp)
|
||||||
clear >/dev/tty1
|
clear >/dev/tty1
|
||||||
echo >&${con} "220 Hello"
|
echo >&${con} "220 Hello"
|
||||||
|
|
||||||
|
# This script uses the (shared) home directory as "dictonary" for
|
||||||
|
# synchronizing the username and the uid between hosts.
|
||||||
|
#
|
||||||
|
# Every user has a directory with his username. The directory is
|
||||||
|
# owned by root to prevent changes of access rights by the user.
|
||||||
|
# The uid and gid of the directory are equal. Thus the name of the
|
||||||
|
# directory and the id from the group ownership also provide the
|
||||||
|
# association between the username and the uid.
|
||||||
|
|
||||||
|
# Add the user with name $1 to the host's "user database". This
|
||||||
|
# may not be invoked concurrently.
|
||||||
createUser() {
|
createUser() {
|
||||||
local missing=$1
|
local missing=$1
|
||||||
local uid
|
local uid
|
||||||
local userHome="/home/$missing"
|
local userHome="/home/$missing"
|
||||||
local createOpts=""
|
local createOpts=""
|
||||||
|
|
||||||
|
# Retrieve or create the uid for the username
|
||||||
if [ -d "$userHome" ]; then
|
if [ -d "$userHome" ]; then
|
||||||
uid=$(ls -ldn "$userHome" | head -n 1 | awk '{print $3}')
|
# If a home directory exists, use the id from the group ownership as uid
|
||||||
|
uid=$(ls -ldn "$userHome" | head -n 1 | awk '{print $4}')
|
||||||
createOpts="--no-create-home"
|
createOpts="--no-create-home"
|
||||||
else
|
else
|
||||||
uid=$(ls -ln "/home" | tail -n +2 | awk '{print $3}' | sort | tail -1)
|
# Else get the maximum of all ids from the group ownership +1
|
||||||
|
uid=$(ls -ln "/home" | tail -n +2 | awk '{print $4}' | sort | tail -1)
|
||||||
uid=$(( $uid + 1 ))
|
uid=$(( $uid + 1 ))
|
||||||
if [ $uid -lt 1000 ]; then
|
if [ $uid -lt 1100 ]; then
|
||||||
uid=1000
|
uid=1100
|
||||||
fi
|
fi
|
||||||
createOpts="--create-home"
|
createOpts="--create-home"
|
||||||
fi
|
fi
|
||||||
|
|
@ -44,33 +61,45 @@ createUser() {
|
||||||
useradd $missing -u $uid -g $uid $createOpts
|
useradd $missing -u $uid -g $uid $createOpts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Login the user, i.e. create a desktopn for the user.
|
||||||
doLogin() {
|
doLogin() {
|
||||||
user=$1
|
user=$1
|
||||||
if [ "$user" = "root" ]; then
|
if [ "$user" = "root" ]; then
|
||||||
echo >&${con} "504 Won't log in root"
|
echo >&${con} "504 Won't log in root"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Check if this user is already logged in on tty1
|
||||||
curUser=$(loginctl -j | jq -r '.[] | select(.tty=="tty1") | .user')
|
curUser=$(loginctl -j | jq -r '.[] | select(.tty=="tty1") | .user')
|
||||||
if [ "$curUser" = "$user" ]; then
|
if [ "$curUser" = "$user" ]; then
|
||||||
echo >&${con} "201 User already logged in"
|
echo >&${con} "201 User already logged in"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Terminate a running desktop (fail safe)
|
||||||
attemptLogout
|
attemptLogout
|
||||||
|
|
||||||
|
# Check if username is known on this host. If not, create user
|
||||||
uid=$(id -u ${user} 2>/dev/null)
|
uid=$(id -u ${user} 2>/dev/null)
|
||||||
if [ $? != 0 ]; then
|
if [ $? != 0 ]; then
|
||||||
( flock 200
|
( flock 200
|
||||||
createUser ${user}
|
createUser ${user}
|
||||||
) 200>/home/.gen-uid-lock
|
) 200>/home/.gen-uid-lock
|
||||||
|
|
||||||
|
# This should now work, else something went wrong
|
||||||
uid=$(id -u ${user} 2>/dev/null)
|
uid=$(id -u ${user} 2>/dev/null)
|
||||||
if [ $? != 0 ]; then
|
if [ $? != 0 ]; then
|
||||||
echo >&${con} "451 Cannot determine uid"
|
echo >&${con} "451 Cannot determine uid"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Start the desktop for the user
|
||||||
systemd-run 2>$temperr \
|
systemd-run 2>$temperr \
|
||||||
--unit vmop-user-desktop --uid=$uid --gid=$uid \
|
--unit vmop-user-desktop --uid=$uid --gid=$uid \
|
||||||
--working-directory="/home/$user" -p TTYPath=/dev/tty1 \
|
--working-directory="/home/$user" -p TTYPath=/dev/tty1 \
|
||||||
-p PAMName=login -p StandardInput=tty -p StandardOutput=journal \
|
-p PAMName=login -p StandardInput=tty -p StandardOutput=journal \
|
||||||
|
-p Conflicts="gdm.service getty@tty1.service" \
|
||||||
-E XDG_RUNTIME_DIR="/run/user/$uid" \
|
-E XDG_RUNTIME_DIR="/run/user/$uid" \
|
||||||
-p ExecStartPre="/usr/bin/chvt 1" \
|
-p ExecStartPre="/usr/bin/chvt 1" \
|
||||||
dbus-run-session -- gnome-shell --display-server --wayland
|
dbus-run-session -- gnome-shell --display-server --wayland
|
||||||
|
|
@ -81,6 +110,8 @@ doLogin() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Attempt to log out a user currently using tty1. This is an intermediate
|
||||||
|
# operation that can be invoked from other operations
|
||||||
attemptLogout() {
|
attemptLogout() {
|
||||||
systemctl status vmop-user-desktop > /dev/null 2>&1
|
systemctl status vmop-user-desktop > /dev/null 2>&1
|
||||||
if [ $? = 0 ]; then
|
if [ $? = 0 ]; then
|
||||||
|
|
@ -93,6 +124,8 @@ attemptLogout() {
|
||||||
echo >&${con} "102 Desktop stopped"
|
echo >&${con} "102 Desktop stopped"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Log out any user currently using tty1. This is invoked when executing
|
||||||
|
# the logout command and therefore sends back a 2xx return code.
|
||||||
doLogout() {
|
doLogout() {
|
||||||
attemptLogout
|
attemptLogout
|
||||||
echo >&${con} "202 User logged out"
|
echo >&${con} "202 User logged out"
|
||||||
|
|
@ -101,7 +134,7 @@ doLogout() {
|
||||||
while read line <&${con}; do
|
while read line <&${con}; do
|
||||||
case $line in
|
case $line in
|
||||||
"login "*) IFS=' ' read -ra args <<< "$line"; doLogin ${args[1]};;
|
"login "*) IFS=' ' read -ra args <<< "$line"; doLogin ${args[1]};;
|
||||||
logout) doLogout;;
|
"logout") doLogout;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue