Browse Source

make yubilock compatible with gnome-screensaver

master
Marijn Kuijpers 4 years ago
parent
commit
423d290a77
  1. 1
      bin/config_system.ini
  2. 5
      bin/loginctl-unlock.sh
  3. 50
      bin/xscreensaver_yubilock.py
  4. 12
      install.sh

1
bin/config_system.ini

@ -4,3 +4,4 @@ remove_sudo_timestamp_when_locking = true @@ -4,3 +4,4 @@ remove_sudo_timestamp_when_locking = true
[HOSTCONFIG]
logfile = log.log
loglevel = 10
screensaver = gnome-screensaver

5
bin/loginctl-unlock.sh

@ -0,0 +1,5 @@ @@ -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"

50
bin/xscreensaver_yubilock.py

@ -24,6 +24,8 @@ home_dir = os.path.expanduser("~") @@ -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): @@ -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(): @@ -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__": @@ -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)

12
install.sh

@ -14,7 +14,7 @@ if [ `id -u` -ne 0 ]; then @@ -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" @@ -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"

Loading…
Cancel
Save