From 423d290a77e02f19ba63652d63a032cb3be9b02b Mon Sep 17 00:00:00 2001 From: Marijn Kuijpers Date: Tue, 22 Jun 2021 17:19:36 +0200 Subject: [PATCH] make yubilock compatible with gnome-screensaver --- bin/config_system.ini | 1 + bin/loginctl-unlock.sh | 5 ++++ bin/xscreensaver_yubilock.py | 50 ++++++++++++++++++++++++------------ install.sh | 12 ++++++++- 4 files changed, 51 insertions(+), 17 deletions(-) create mode 100755 bin/loginctl-unlock.sh diff --git a/bin/config_system.ini b/bin/config_system.ini index 5e6fbb8..2ba4110 100644 --- a/bin/config_system.ini +++ b/bin/config_system.ini @@ -4,3 +4,4 @@ remove_sudo_timestamp_when_locking = true [HOSTCONFIG] logfile = log.log loglevel = 10 +screensaver = gnome-screensaver diff --git a/bin/loginctl-unlock.sh b/bin/loginctl-unlock.sh new file mode 100755 index 0000000..2e55bfd --- /dev/null +++ b/bin/loginctl-unlock.sh @@ -0,0 +1,5 @@ +#! /bin/bash + +session_id=`loginctl list-sessions --no-legend | while read id rest; do loginctl show-session $id; done | grep 'Id=\|Type=' | sed -z 's/\nType=/ /g' | grep wayland | awk -F '[= ]' '{print $2}'` +loginctl unlock-session "$session_id" + diff --git a/bin/xscreensaver_yubilock.py b/bin/xscreensaver_yubilock.py index 453447e..1d0339c 100755 --- a/bin/xscreensaver_yubilock.py +++ b/bin/xscreensaver_yubilock.py @@ -24,6 +24,8 @@ home_dir = os.path.expanduser("~") config = ConfigParser() config.read([f"{script_dir}/config.ini", f"{home_dir}/.yubilock"]) +screensaver = config.get("HOSTCONFIG", "screensaver", fallback="xscreensaver")) + yubikey_serials = config["USERCONFIG"]["yubikey_serial"].split(',') # Convert stringlist to intlist for i, serial in enumerate(yubikey_serials): @@ -50,9 +52,14 @@ def execute(command: str, shell_on: bool = False, background: bool = False): return '' if out == b'' else out.decode('utf-8') def screensaver_running(): - graphic_program_instances = execute(f"{script_dir}/kill_screensaver_graphic_program.sh -d | grep graphic_processes | wc -l", shell_on=True) - if int(graphic_program_instances) > 0: return True - if execute(f"DISPLAY=:0 xscreensaver-command -time | grep 'screen locked' >/dev/null 2>&1", shell_on=True): return True + if screensaver == 'xscreensaver': + graphic_program_instances = execute(f"{script_dir}/kill_screensaver_graphic_program.sh -d | grep graphic_processes | wc -l", shell_on=True) + if int(graphic_program_instances) > 0: return True + if execute(f"DISPLAY=:0 xscreensaver-command -time | grep 'screen locked' >/dev/null 2>&1", shell_on=True): return True + elif screensaver == 'gnome-screensaver': + screensaver_state = execute("gnome-screensaver-command -q | tr -d '\n'", shell_on=True) + if screensaver_state == 'The screensaver is active': return True + if screensaver_state == 'The screensaver is inactive': return False return False def lock_screen(): @@ -60,23 +67,30 @@ def lock_screen(): return if config.getboolean('USERCONFIG', 'remove_sudo_timestamp_when_locking', fallback=True): execute('sudo -K', shell_on=True) - execute('DISPLAY=:0 xscreensaver-command -lock', shell_on=True) + + if screensaver == 'xscreensaver': + execute('DISPLAY=:0 xscreensaver-command -lock', shell_on=True) + elif screensaver == 'gnome-screensaver': + execute('gnome-screensaver-command -l', shell_on=True) return def unlock_screen(): if args.dummy : return - logger.info('xscreen process to be killed:') - xscreensaver_pid = execute(r'ps -A | grep -oPm 1 "\d{2,}(?=\s.+xscreensaver)" || echo null', - shell_on=True) + if screensaver == 'xscreensaver': + logger.info('xscreen process to be killed:') + xscreensaver_pid = execute(r'ps -A | grep -oPm 1 "\d{2,}(?=\s.+xscreensaver)" || echo null', + shell_on=True) - if xscreensaver_pid != 'null': - execute('kill %s' % xscreensaver_pid, shell_on=True) - execute(f"{script_dir}/kill_screensaver_graphic_program.sh", shell_on=True) + if xscreensaver_pid != 'null': + execute('kill %s' % xscreensaver_pid, shell_on=True) + execute(f"{script_dir}/kill_screensaver_graphic_program.sh", shell_on=True) - # restart xscreensaver process - execute('DISPLAY=:0 xscreensaver -no-splash&', shell_on=True, background = True) + # restart xscreensaver process + execute('DISPLAY=:0 xscreensaver -no-splash&', shell_on=True, background = True) + elif screensaver == 'gnome-screensaver': + execute(f"{script_dir}/loginctl-unlock.sh", shell_on=True, background = True) return def get_yubikey_serials() -> int: @@ -170,16 +184,20 @@ if __name__ == "__main__": setup_logger(config.get("HOSTCONFIG", "logfile", fallback="log.log")) - # start xscreensaver process - execute('DISPLAY=:0 xscreensaver -no-splash&', shell_on=True, background = True) + + if screensaver == 'xscreensaver': + # start xscreensaver process + execute('DISPLAY=:0 xscreensaver -no-splash&', shell_on=True, background = True) # Lock the machine if no key is inserted when the script is started update_lock_state() daemon(get_hid_event_monitor()) - # restart xscreensaver process before leaving - execute('DISPLAY=:0 xscreensaver -no-splash&', shell_on=True, background = True) + if screensaver == 'xscreensaver': + # restart xscreensaver process before leaving + execute('DISPLAY=:0 xscreensaver -no-splash&', shell_on=True, background = True) + sleep(0.2) sys.exit(0) diff --git a/install.sh b/install.sh index ae3d2cd..696d403 100755 --- a/install.sh +++ b/install.sh @@ -14,7 +14,7 @@ if [ `id -u` -ne 0 ]; then fi # Ask for user parameters -echo "This installer is meant to install the yubilock service for one user. Please specifiy for wich user you want to install xscreensaver-yubilock" +echo "This installer is meant to install the yubilock service for one user. Please specifiy for wich user you want to install yubilock" read -p 'Username: ' username userid=`id -u "$username" 2>/dev/null` || ( echo "$username is not a user on this system" && exit 1 ) [ "$userid" -lt 1000 ] && echo "User $username seems to be a systemuser (uid: $userid). Please specify a normal user." && exit 1 @@ -95,8 +95,18 @@ cp "$script_dir/bin/xscreensaver_yubilock.py" "$install_dir" cp "$script_dir/bin/uninstall.sh" "$install_dir" cp "$script_dir/bin/kill_screensaver_graphic_program.sh" "$install_dir" cp "$script_dir/bin/seconds_since_wakeup.sh" "$install_dir" +cp "$script_dir/bin/loginctl-unlock.sh" "$install_dir" cp "$script_dir/bin/config_system.ini" "$install_dir/config.ini" +if dpkg -l xscreensaver | grep 'ii.*xscreensaver' >/dev/null; then + screensaver='xscreensaver' +elif dpkg -l gnome-screensaver | grep 'ii.*gnome-screensaver' >/dev/null; then + screensaver='gnome-screensaver' +else + screensaver='none' +fi +sed -i "s+^screensaver.*+screensaver\ =\ $screensaver+g" "$install_dir/config.ini" + chown -R root:yubilock "$install_dir" chmod 771 "$install_dir"