|
|
|
@ -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) |
|
|
|
|