Skip to content

Commit 205e0c0

Browse files
authored
Merge pull request #6 from OS2borgerPC/5-add-system-scripts-needed-by-admin-site
issue #5 - add speciel admin-site system scripts. (for functionality)
2 parents e8c48d0 + 0ad2fe3 commit 205e0c0

8 files changed

+939
-0
lines changed

inactivity_suspend_after_time.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
title: "Aktiver dvale efter X minutters inaktivitet"
3+
parent: "Sikkerhed"
4+
source: scripts/inactivity_suspend_after_time.sh
5+
compatibility:
6+
- "22.04"
7+
- "BorgerPC"
8+
parameters:
9+
- name: "Aktiver?"
10+
type: "boolean"
11+
default: null
12+
mandatory: false
13+
- name: "Antal MINUTTER til dialog bliver vises"
14+
type: "int"
15+
default: null
16+
mandatory: true
17+
- name: "Antal MINUTTER til dvale aktiveres"
18+
type: "int"
19+
default: null
20+
mandatory: true
21+
- name: "Tekst som bliver vist i dialogboksen"
22+
type: "string"
23+
default: null
24+
mandatory: false
25+
- name: "Tekst som bliver vist på dialogboks knappen"
26+
type: "string"
27+
default: null
28+
mandatory: false
29+
metadata:
30+
uid: "suspend_after_time"
31+
---
32+
33+
## Beskrivelse
34+
35+
Vis en advarsel efter et antal minutters inaktivitet - ved fortsat inaktivitet logges brugeren automatisk ud og maskinen går i dvale.
36+
37+
BEMÆRK: USB-overvågning kan ikke være aktiv, mens en computer er i dvale. Vi anbefaler derfor ikke at anvende scriptet på computere, der står uden opsyn, medmindre man benytter de bure, der forhindrer borgere i at tilgå USB-enhederne.
38+
39+
YDERLIGERE BEMÆRKNING: Computere kan ikke tjekke ind på admin-sitet eller køre scripts, mens de er i dvale, og kan derfor komme til at stå som værende offline.
40+
41+
## Parametre
42+
Scriptet tager 5 inputparametre:
43+
1. Aktiver/deaktiver scriptet. Sæt hak i boksen for at aktivere scriptet og lad den stå tom for at deaktivere det.
44+
2. Antal MINUTTER computeren kan være inaktiv, før en advarsel vises.
45+
3. Antal MINUTTER computeren kan være inaktiv, før den lukkes ned.
46+
Det skal være længere tid end det første inputparameter, da advarslen ellers ikke vil blive set. Vi foreslår minimum 3 minutter længere.
47+
4. Teksten der vises i inaktivitetsadvarslen.
48+
Hvis feltet efterlades tomt bruges standardteksten "Du er inaktiv og logges ud om kort tid.."
49+
5. Teksten der vises på knappen i inaktivitetsadvarslen.
50+
Hvis feltet efterlades tomt bruges standardteksten "OK".
51+
52+
Eksempel:
53+
Med inputparametrene 5 og 10 vil der vises en advarsel efter 5 minutter, og hvis inaktiviteten så fortsætter 5 minutter derefter, vil brugeren blive logget af, og maskinen vil gå i dvale.
54+
55+
Hvis du ønsker linieskift i teksten, kan det gøres ved at skrive \n således:
56+
Linie1\nLinie2
57+
58+
Vi anbefaler at genstarte efter kørsel, for at være sikker på, det har taget effekt.
59+
60+
Dette script er blevet testet og virker på Ubuntu 22.04.

on_off_schedule_remove.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: "Tænd/Sluk tidsplaner - Fjern tidsplan"
3+
parent: "System"
4+
source: scripts/on_off_schedule_remove.sh
5+
compatibility:
6+
- "22.04"
7+
- "BorgerPC"
8+
metadata:
9+
uid: "wake_plan_remove"
10+
security: false
11+
hidden: true
12+
---
13+
14+
## Beskrivelse
15+
16+
Fjern tænd sluk tidsplan

on_off_schedule_set.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: "Tænd/Sluk tidsplaner - Opret tidsplan"
3+
parent: "System"
4+
source: scripts/on_off_schedule_set.sh
5+
parameters:
6+
- name: "Mandag tænd"
7+
type: "string"
8+
default: null
9+
mandatory: true
10+
- name: "Mandag sluk"
11+
type: "string"
12+
default: null
13+
mandatory: true
14+
- name: "Tirsdag tænd"
15+
type: "string"
16+
default: null
17+
mandatory: true
18+
- name: "Tirsdag sluk"
19+
type: "string"
20+
default: null
21+
mandatory: true
22+
- name: "Onsdag tænd"
23+
type: "string"
24+
default: null
25+
mandatory: true
26+
- name: "Onsdag sluk"
27+
type: "string"
28+
default: null
29+
mandatory: true
30+
- name: "Torsdag tænd"
31+
type: "string"
32+
default: null
33+
mandatory: true
34+
- name: "Torsdag sluk"
35+
type: "string"
36+
default: null
37+
mandatory: true
38+
- name: "Fredag tænd"
39+
type: "string"
40+
default: null
41+
mandatory: true
42+
- name: "Fredag sluk"
43+
type: "string"
44+
default: null
45+
mandatory: true
46+
- name: "Lørdag tænd"
47+
type: "string"
48+
default: null
49+
mandatory: true
50+
- name: "Lørdag sluk"
51+
type: "string"
52+
default: null
53+
mandatory: true
54+
- name: "Søndag tænd"
55+
type: "string"
56+
default: null
57+
mandatory: true
58+
- name: "Søndag sluk"
59+
type: "string"
60+
default: null
61+
mandatory: true
62+
- name: "Undtagelser: Lukkedage og Ændrede åbningstider"
63+
type: "string"
64+
default: null
65+
mandatory: true
66+
- name: "mode"
67+
type: "string"
68+
default: null
69+
mandatory: true
70+
compatibility:
71+
- "22.04"
72+
- "BorgerPC"
73+
metadata:
74+
uid: "wake_plan_set"
75+
security: false
76+
hidden: true
77+
---
78+
79+
## Beskrivelse
80+
81+
Opret tænd sluk tidsplan
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
#! /usr/bin/env sh
2+
3+
# DESCRIPTION
4+
#
5+
# This script will log out the user and suspend the PC after a given period of inactivity.
6+
# A configurable warning is shown before the user is logged out and the pc suspended.
7+
#
8+
# The script will also suspend the PC after the same period of inactivity on the login screen.
9+
# This second part of the script requires "lightdm_greeter_setup_scripts" to be run and enabled to take effect
10+
#
11+
# The script is designed to wake up the PC 1 minute before a potential scheduled shutdown (on/off-schedule)
12+
# so that it can be shut down as planned.
13+
# If no scheduled shutdown exists, it will suspend the PC until it is woken manually.
14+
#
15+
# This script and "inactivity_logout_after_time.sh" are mutually exclusive, and each of them
16+
# are written to overwrite each other, so whichever was the last of them run takes effect.
17+
#
18+
# PARAMETERS
19+
# 1. Checkbox. Enables/disables the script.
20+
# 2. Integer. How many minutes to wait before showing the warning dialog
21+
# 3. Integer. How many minutes to wait before logging out and suspending
22+
# 4. String. (optional) The text to be shown in the warning dialog. If no input is given, a default is used
23+
# 5. String. (optional) The text to be shown on the dialog button. If no input is given, a default is used
24+
25+
set -x
26+
27+
ENABLE=$1
28+
DIALOG_TIME_MINS=$2
29+
LOGOUT_TIME_MINS=$3
30+
DIALOG_TEXT=$4
31+
BUTTON_TEXT=$5
32+
33+
# Note: Currently these logs are never rotated, so they'll grow and grow
34+
SUSPEND_SCRIPT="/usr/share/os2borgerpc/bin/inactive_logout.sh"
35+
SUSPEND_SCRIPT_LOG="/usr/share/os2borgerpc/bin/inactive_logout.log"
36+
LIGHTDM_SUSPEND_SCRIPT="/etc/lightdm/greeter-setup-scripts/suspend_after_time.sh"
37+
LIGHTDM_SUSPEND_SCRIPT_LOG="/etc/lightdm/scriptlogs/suspend_after_time.log"
38+
LIGHTDM_GREETER_SETUP_SCRIPT="/etc/lightdm/greeter_setup_script.sh"
39+
LIGHTDM_GREETER_SCRIPTS_DIR="/etc/lightdm/greeter-setup-scripts"
40+
41+
# Stop Debconf from interrupting when interacting with the package system
42+
export DEBIAN_FRONTEND=noninteractive
43+
44+
error() {
45+
echo "$1"
46+
exit 1
47+
}
48+
49+
if get_os2borgerpc_config os2_product | grep --quiet kiosk; then
50+
error "Dette script er ikke designet til at blive anvendt på en kiosk-maskine."
51+
fi
52+
53+
# Handle deactivating inactivity suspend
54+
if [ "$ENABLE" = "False" ]; then
55+
rm --force $SUSPEND_SCRIPT $LIGHTDM_SUSPEND_SCRIPT $SUSPEND_SCRIPT_LOG $LIGHTDM_SUSPEND_SCRIPT_LOG
56+
OLDCRON="/tmp/oldcron"
57+
crontab -l > $OLDCRON
58+
if [ -f "$OLDCRON" ]; then
59+
sed --in-place "\@$SUSPEND_SCRIPT@d" $OLDCRON
60+
crontab $OLDCRON
61+
rm --force $OLDCRON
62+
fi
63+
exit
64+
fi
65+
66+
[ -z "$DIALOG_TIME_MINS" ] && error 'Please insert the time the user has to be inactive before dialog is shown.'
67+
[ -z "$LOGOUT_TIME_MINS" ] && error 'Please insert the time the user has to be inactive before being logged out.'
68+
[ "$DIALOG_TIME_MINS" -gt "$LOGOUT_TIME_MINS" ] && error 'Dialog time is greater than logout time and dialog will therefore not be shown. Edit dialog time!'
69+
[ -z "$DIALOG_TEXT" ] && DIALOG_TEXT="Du er inaktiv og bliver logget ud om kort tid..."
70+
[ -z "$BUTTON_TEXT" ] && BUTTON_TEXT="OK"
71+
72+
# xprintidle uses milliseconds, so convert the user inputted minutes to that
73+
LOGOUT_TIME_MS=$(( LOGOUT_TIME_MINS * 60 * 1000 ))
74+
DIALOG_TIME_MS=$(( DIALOG_TIME_MINS * 60 * 1000 ))
75+
76+
# Older versions of this script used sh, but our lightdm suspend script uses
77+
# bash specifics. Change it to run the script directly with whatever interpreter it has.
78+
# This requires ensuring that lightdm has execute permissions on all those scripts.
79+
chmod --recursive 700 $LIGHTDM_GREETER_SCRIPTS_DIR
80+
cat << EOF > $LIGHTDM_GREETER_SETUP_SCRIPT
81+
#!/bin/sh
82+
greeter_setup_scripts=\$(find $LIGHTDM_GREETER_SCRIPTS_DIR -mindepth 1)
83+
for file in \$greeter_setup_scripts
84+
do
85+
./"\$file" &
86+
done
87+
EOF
88+
89+
chmod 700 $LIGHTDM_GREETER_SETUP_SCRIPT
90+
91+
mkdir --parents "$(dirname $LIGHTDM_SUSPEND_SCRIPT)" "$(dirname $SUSPEND_SCRIPT_LOG)"
92+
93+
TIMEOUT_SECS=$((LOGOUT_TIME_MINS * 60))
94+
95+
cat << EOF > "$LIGHTDM_SUSPEND_SCRIPT"
96+
#!/usr/bin/env bash
97+
98+
LOG=$LIGHTDM_SUSPEND_SCRIPT_LOG
99+
100+
while :
101+
do
102+
echo "Starting sleep for $TIMEOUT_SECS seconds" >> \$LOG
103+
sleep $TIMEOUT_SECS
104+
echo "Sleep over" >> \$LOG
105+
if [ -z \$(users) ]; then
106+
echo "no active users, suspending" >> \$LOG
107+
# If the pc has a time plan, don't use systemctl suspend, but instead rtcwake -m mem,
108+
# which is functionally the same and allows the machine to wake up in time to be shut down
109+
# by the time plan
110+
re="([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) .+"
111+
if [[ \$(crontab -l | grep scheduled_off) =~ \$re ]]; then
112+
MINUTES=\${BASH_REMATCH[1]}
113+
HOURS=\${BASH_REMATCH[2]}
114+
DAY=\${BASH_REMATCH[3]}
115+
MONTH=\${BASH_REMATCH[4]}
116+
YEAR=\$(date +%Y)
117+
# wake up 1 minute before shut down
118+
MINM1P60=\$(( \$(( MINUTES - 1)) + 60))
119+
# Rounding minutes
120+
MINS=\$(( MINM1P60 % 60))
121+
HRCORR=\$(( 1 - \$(( MINM1P60 / 60))))
122+
HRS=\$(( HOURS - HRCORR))
123+
HRS=\$(( \$(( HRS + 24)) % 24))
124+
rtcwake -m mem --date "\$YEAR-\$MONTH-\$DAY \$HRS:\$MINS"
125+
else
126+
systemctl suspend
127+
fi
128+
else
129+
echo "should be logged in as \$(users) breaking loop" >> \$LOG
130+
break
131+
fi
132+
done
133+
134+
echo "exited loop" >> \$LOG
135+
exit 0
136+
EOF
137+
138+
chmod 700 $LIGHTDM_SUSPEND_SCRIPT
139+
140+
# Install xprintidle
141+
apt-get update --assume-yes
142+
143+
# Only try installing if it isn't already as otherwise it will exit with nonzero
144+
# and stop the script
145+
if ! dpkg --get-selections | grep -v deinstall | grep --quiet xprintidle; then
146+
if ! apt-get install --assume-yes xprintidle; then
147+
# apt install could fail due to debian frontend lock being unavailable
148+
# during automatic updates
149+
error "apt failed to install xprintidle"
150+
fi
151+
fi
152+
153+
# if line already added to crontab: skip
154+
if ! crontab -l | grep "$SUSPEND_SCRIPT"; then
155+
line="* * * * * $SUSPEND_SCRIPT"
156+
(crontab -l -u root; echo "$line") | crontab -u root -
157+
fi
158+
159+
# New auto_logout file, running as root
160+
cat <<- EOF > $SUSPEND_SCRIPT
161+
#!/usr/bin/env bash
162+
163+
# If the user is inactive for too long, a dialog will appear, warning the user that the session will end.
164+
# If the user do not touch the mouse or press any keyboard key the session will end.
165+
# Only have one dialog at a time, so remove preexisting ones.
166+
# Create a new message every time, in case someone didn't close it but
167+
# just put e.g. a browser in front, to ensure they or someone else gets a
168+
# new warning when/if inactivity is reached again
169+
170+
USER_DISPLAY=\$(who | grep -w 'user' | sed -rn 's/.*(:[0-9]*).*/\1/p')
171+
172+
# These are used by xprintidle
173+
export XAUTHORITY=/home/user/.Xauthority
174+
export DISPLAY=\$USER_DISPLAY
175+
su - user -c "DISPLAY=\$USER_DISPLAY xhost +localhost"
176+
177+
LOG=$SUSPEND_SCRIPT_LOG
178+
179+
echo $LOGOUT_TIME_MS \$(xprintidle) >> \$LOG
180+
181+
# If the pc has a time plan, don't use systemctl suspend, but instead rtcwake -m mem,
182+
# which is functionally the same and allows the machine to wake up in time to be shut down
183+
# by the time plan
184+
185+
if [ \$(xprintidle) -ge $LOGOUT_TIME_MS ]; then
186+
echo 'Logging user out' >> \$LOG
187+
pkill -KILL -u user
188+
echo 'suspending pc' >> \$LOG
189+
# If the pc has a time plan, don't use systemctl suspend, but instead rtcwake -m mem,
190+
# which is functionally the same and allows the machine to wake up in time to be shut down
191+
# by the time plan
192+
re="([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) .+"
193+
if [[ \$(crontab -l | grep scheduled_off) =~ \$re ]]; then
194+
MINUTES=\${BASH_REMATCH[1]}
195+
HOURS=\${BASH_REMATCH[2]}
196+
DAY=\${BASH_REMATCH[3]}
197+
MONTH=\${BASH_REMATCH[4]}
198+
YEAR=\$(date +%Y)
199+
# wake up 1 minute before shut down
200+
MINM1P60=\$(( \$(( MINUTES - 1)) + 60))
201+
# Rounding minutes
202+
MINS=\$(( MINM1P60 % 60))
203+
HRCORR=\$(( 1 - \$(( MINM1P60 / 60))))
204+
HRS=\$(( HOURS - HRCORR))
205+
HRS=\$(( \$(( HRS + 24)) % 24))
206+
# When run from the crontab, rtcwake needs the full path for some reason or it won't work
207+
/usr/sbin/rtcwake -m mem --date "\$YEAR-\$MONTH-\$DAY \$HRS:\$MINS"
208+
else
209+
systemctl suspend
210+
fi
211+
exit 0
212+
fi
213+
# if idle time is past the dialog time: show the dialog
214+
if [ \$(xprintidle) -ge $DIALOG_TIME_MS ]; then
215+
# Do spare the poor lives of potential other zenity windows.
216+
PID_ZENITY="\$(pgrep --full 'Inaktivitet')"
217+
if [ -n \$PID_ZENITY ]; then
218+
kill \$PID_ZENITY
219+
fi
220+
# echo 'Running zenity...' >> \$LOG
221+
# We use the --title to match against above
222+
zenity --warning --text="$DIALOG_TEXT" --ok-label="$BUTTON_TEXT" --no-wrap --display=\$USER_DISPLAY --title "Inaktivitet"
223+
fi
224+
EOF
225+
226+
chmod 700 $SUSPEND_SCRIPT

0 commit comments

Comments
 (0)