Last active
August 1, 2024 13:52
-
-
Save innerand/405025e7fbae1b270025666418655d8b to your computer and use it in GitHub Desktop.
Auto Unlock (login, sleep, sceenlock) KeePassXC Database for Gnome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Re-Open KeePassXC Database | |
# | |
# - Requests password of a KeePassXC database from the system keyring and opens | |
# it | |
# - Listens to DBUS LockedHint signals. Closes the database on lock, reopens it | |
# on unlock | |
# | |
# | |
# Copyright (c) 2021 Innerlohninger Andreas. All rights reserved. | |
# | |
# This work is released under the terms of the MIT license. | |
# For a copy, see <https://opensource.org/licenses/MIT> | |
# Configuration ---------------------------------------------------------------- | |
# Path to kdbx Database | |
KDBX='<path to kdbx database>.kdbx' | |
# Secret tool password entry of database | |
# might be added with "secret-tool store --label='{label}' {attribute} {value}" | |
SECTOOL_ATTRIBUTE='<attribute>' | |
SECTOOL_VALUE='<value>' | |
# Variables -------------------------------------------------------------------- | |
PID="0" | |
# Script ----------------------------------------------------------------------- | |
# Starts keepassxc and stores its PID, exits with error if already running | |
function unlock_database { | |
if pidof "keepassxc" > /dev/null; then | |
echo "Another instance of keepassxc is already running!" | |
exit 1 | |
fi | |
eval "secret-tool lookup $SECTOOL_ATTRIBUTE $SECTOOL_VALUE | | |
/usr/bin/keepassxc --pw-stdin $KDBX &> /dev/null &" | |
PID=$! | |
} | |
# Initial unlock | |
unlock_database | |
# Listen for LockedHint | |
gdbus monitor -y -d org.freedesktop.login1 | | |
while read x; do | |
case "$x" in | |
*"boolean true"*) # Session locked, close database | |
kill $PID | |
;; | |
*"boolean false"*) # Session unlocked re-open database | |
unlock_database | |
;; | |
esac | |
done | |
## ~/.config/systemd/user/unlock_kpxc.service | |
# | |
# [Unit] | |
# Description=Unlock KPXC Database | |
# | |
# [Service] | |
# Type=simple | |
# TimeoutStartSec=1 | |
# ExecStart=<path to>/unlock_kpxc.sh | |
# | |
# [Install] | |
# WantedBy=default.target | |
# | |
## ~/.config/autostart/unlock_kpxc.desktop | |
# | |
# [Desktop Entry] | |
# Encoding=UTF-8 | |
# Exec=systemctl --user start unlock_kpxc.service | |
# Name=Unlock KPXC | |
# Comment=Unlock KPXC Database | |
# Terminal=false | |
# OnlyShowIn=GNOME | |
# Type=Application | |
# StartupNotify=false | |
# X-GNOME-Autostart-enabled=true | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment