diff --git a/src/ldaptool/search/config.py b/src/ldaptool/search/config.py index 438d980..16ba7ef 100644 --- a/src/ldaptool/search/config.py +++ b/src/ldaptool/search/config.py @@ -5,6 +5,7 @@ import dataclasses import getpass import os import os.path +import sys import shlex import subprocess import typing @@ -145,10 +146,18 @@ class Keepass(PasswordManager): def get_password(self, password_name: str) -> str: import pykeepass # already made sure it is avaiable above - password = getpass.getpass(f"KeePass password for database {self.database}: ") - kp = pykeepass.PyKeePass(self.database, password=password) + while True: + try: + password = getpass.getpass(f"KeePass password for database {self.database}: ") + kp = pykeepass.PyKeePass(self.database, password=password) + break + except pykeepass.exceptions.CredentialsError: + print("Invalid password", file=sys.stderr) + entry = kp.find_entries(username=password_name, first=True) - return entry.password # type: ignore + if entry: + return entry.password # type: ignore + @dataclasses.dataclass @@ -220,7 +229,16 @@ class Config: """ if realm.account is None: raise RuntimeError("Can't get password without acccount - should use kerberos instead") - if self.password_manager: - return self.password_manager.get_password(realm.password_name) - return getpass.getpass(f"Enter password for {realm.password_name}: ") + try: + if self.password_manager: + password = self.password_manager.get_password(realm.password_name) + else: + password = getpass.getpass(f"Enter password for {realm.password_name}: ") + except (KeyboardInterrupt, EOFError): + exit() + + if password: + return password + else: + raise SystemExit(f"No password found for {realm.password_name}")