Skip to content

Commit 79de554

Browse files
author
Andreas Poulsen
committed
Merge branch 'feature/56040_rewrite_desktop_toggle_writable_to_support_language_variability' into 'master'
Rewrite desktop_toggle_writable to support language variability See merge request os2borgerpc/os2borgerpc-scripts!216
2 parents 2068734 + 2399ff1 commit 79de554

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

os2borgerpc/sikkerhed/desktop_toggle_writable.sh

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,57 @@ set -x
1111
# Why not use a .config/autostart file? Because the user isn't allowed to chown to root
1212
# ...even if they are the current owner.
1313

14+
# chattr on DESKTOP is to prevent mv'ing DESKTOP to another name, and then creating a new one
15+
# which they DO have write permissions to
16+
# Another option considered was chowning /home/user itself (not recursively),
17+
# but then login didn't work. (maybe due to .xauthority?)
18+
1419
if get_os2borgerpc_config os2_product | grep --quiet kiosk; then
1520
echo "Dette script er ikke designet til at blive anvendt på en kiosk-maskine."
1621
exit 1
1722
fi
1823

19-
USER="user"
20-
DESKTOP="Skrivebord"
24+
USERNAME="user"
25+
# Determine the name of the user desktop directory. This is done via xdg-user-dir,
26+
# which checks the /home/user/.config/user-dirs.dirs file. To ensure this file exists,
27+
# we run xdg-user-dirs-update, which generates it based on the environment variable
28+
# LANG. This variable is empty in lightdm so we first export it
29+
# based on the value stored in /etc/default/locale
30+
export "$(grep LANG= /etc/default/locale)"
31+
runuser -u $USERNAME xdg-user-dirs-update
32+
DESKTOP=$(basename "$(runuser -u $USERNAME xdg-user-dir DESKTOP)")
2133
USER_CLEANUP=/usr/share/os2borgerpc/bin/user-cleanup.bash
22-
SET_USER_DESKTOP_MUTABLE="chattr -i /home/$USER/$DESKTOP"
23-
SET_USER_DESKTOP_ROOT_OWNED="chown -R root:user /home/$USER/$DESKTOP"
24-
# This is to prevent mv'ing Skrivebord to another name, and then creating a new one
25-
# which they DO have write permissions to
26-
# Another option considered was chowning /home/user itself (not recursively),
27-
# but then login didn't work. (maybe due to .xauthority?)
28-
SET_USER_DESKTOP_IMMUTABLE="chattr +i /home/$USER/$DESKTOP"
34+
COMMENT="# Make the desktop read only to user"
2935

3036
ACTIVATE=$1
3137

3238
make_desktop_writable() {
33-
sed -i "\@$SET_USER_DESKTOP_MUTABLE@d" $USER_CLEANUP
34-
sed -i "\@$SET_USER_DESKTOP_ROOT_OWNED@d" $USER_CLEANUP
35-
sed -i "\@$SET_USER_DESKTOP_IMMUTABLE@d" $USER_CLEANUP
36-
chattr -i /home/$USER/$DESKTOP
39+
# All of the matched lines are deleted. This function thus serves to undo write access removal
40+
# shellcheck disable=SC2016
41+
sed --in-place --expression "/chattr [-+]i/d" --expression "/chown -R root:/d" \
42+
--expression "/$COMMENT/d" --expression '/runuser/d' --expression '/export/d' $USER_CLEANUP
43+
chattr -i "$DESKTOP"
3744
}
3845

39-
# Make sure that /home/.skjult/Skrivebord exists as otherwise this script will not work correctly
40-
mkdir --parents /home/.skjult/Skrivebord
46+
# Make sure that DESKTOP dir exists under .skjult as otherwise this script will not work correctly
47+
mkdir --parents "/home/.skjult/$(basename "$DESKTOP")"
4148

42-
# Undo write access removal.
43-
# We always do this to prevent adding the same lines multiple times (idempotency)
49+
# Undo write access removal - always do this to prevent adding the same lines multiple times (idempotency)
4450
make_desktop_writable
4551

4652
if [ "$ACTIVATE" = 'True' ]; then
47-
# Temporarily set it mutable before copying new files in, as otherwise that will fail
48-
sed -i "/USERNAME=\"user\"/a $SET_USER_DESKTOP_MUTABLE" $USER_CLEANUP
53+
# Prepend temporarily setting DESKTOP mutable before copying new files in, as otherwise that will fail
54+
# We first determine the name of the user desktop directory as before
55+
sed -i "/USERNAME=\"$USERNAME\"/a \
56+
export \$(grep LANG= \/etc\/default\/locale)\n\
57+
runuser -u $USERNAME xdg-user-dirs-update\n\
58+
DESKTOP=\$(runuser -u $USERNAME xdg-user-dir DESKTOP)\n\
59+
chattr -i \$DESKTOP" $USER_CLEANUP
60+
61+
# Append setting the more restrictive permissions
4962
cat <<- EOF >> $USER_CLEANUP
50-
$SET_USER_DESKTOP_ROOT_OWNED
51-
$SET_USER_DESKTOP_IMMUTABLE
63+
$COMMENT
64+
chown -R root:\$USERNAME \$DESKTOP
65+
chattr +i \$DESKTOP
5266
EOF
5367
fi

0 commit comments

Comments
 (0)