def __init__(self, pidfile, loglevel, logfile, server_address = 'localhost', server_port = 50000, agent_port = 50001, password = None, keyfile = None, tls = False, tls_dir = None): Daemon.__init__(self, pidfile) try: logdir = realpath(expanduser(getenv('XDG_DATA_HOME'))) except: logdir = realpath(expanduser('~/.local/share')) finally: logfile = join(logdir, 'keepassc', logfile) logging.basicConfig(format='[%(levelname)s] in %(filename)s:' '%(funcName)s at %(asctime)s\n%(message)s', level=loglevel, filename=logfile, filemode='a') self.lookup = { b'FIND': self.find, b'GET': self.get_db, b'GETC': self.get_credentials} self.server_address = (server_address, server_port) try: # Listen for commands self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.bind(("localhost", agent_port)) self.sock.listen(1) except OSError as err: print(err) logging.error(err.__str__()) sys.exit(1) else: logging.info('Agent socket created on localhost:'+ str(agent_port)) if tls_dir is not None: self.tls_dir = realpath(expanduser(tls_dir)).encode() else: self.tls_dir = b'' chdir("/var/empty") self.password = password # Agent is a daemon and cannot find the keyfile after run if keyfile is not None: with open(keyfile, "rb") as handler: self.keyfile = handler.read() handler.close() else: self.keyfile = b'' if tls is True: self.context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) self.context.verify_mode = ssl.CERT_REQUIRED self.context.load_verify_locations(tls_dir + "/cacert.pem") else: self.context = None #Handle SIGTERM signal.signal(signal.SIGTERM, self.handle_sigterm)
def __init__(self, pidfile, loglevel, logfile, address = None, port = 50002, db = None, password = None, keyfile = None, tls = False, tls_dir = None, tls_port = 50003, tls_req = False): Daemon.__init__(self, pidfile) try: logdir = realpath(expanduser(getenv('XDG_DATA_HOME'))) except: logdir = realpath(expanduser('~/.local/share')) finally: logfile = join(logdir, 'keepassc', logfile) logging.basicConfig(format='[%(levelname)s] in %(filename)s:' '%(funcName)s at %(asctime)s\n%(message)s', level=loglevel, filename=logfile, filemode='a') if db is None: print('Need a database path') sys.exit(1) self.db_path = realpath(expanduser(db)) # To use this idiom only once, I store the keyfile path # as a class attribute if keyfile is not None: keyfile = realpath(expanduser(keyfile)) else: keyfile = None chdir("/var/empty") try: self.db = KPDBv1(self.db_path, password, keyfile) self.db.load() except KPError as err: print(err) logging.error(err.__str__()) sys.exit(1) self.lookup = { b'FIND': self.find, b'GET': self.send_db, b'CHANGESECRET': self.change_password, b'NEWG': self.create_group, b'NEWE': self.create_entry, b'DELG': self.delete_group, b'DELE': self.delete_entry, b'MOVG': self.move_group, b'MOVE': self.move_entry, b'TITG': self.set_g_title, b'TITE': self.set_e_title, b'USER': self.set_e_user, b'URL': self.set_e_url, b'COMM': self.set_e_comment, b'PASS': self.set_e_pass, b'DATE': self.set_e_exp} self.sock = None self.net_sock = None self.tls_sock = None self.tls_req = tls_req if tls is True or tls_req is True: self.context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) cert = join(tls_dir, "servercert.pem") key = join(tls_dir, "serverkey.pem") self.context.load_cert_chain(certfile=cert, keyfile=key) else: self.context = None try: # Listen for commands self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.bind(("localhost", 50000)) self.sock.listen(5) except OSError as err: print(err) logging.error(err.__str__()) sys.exit(1) else: logging.info('Server socket created on localhost:50000') if self.tls_req is False and address is not None: try: # Listen for commands self.net_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.net_sock.bind((address, port)) self.net_sock.listen(5) except OSError as err: print(err) logging.error(err.__str__()) sys.exit(1) else: logging.info('Server socket created on '+address+':'+ str(port)) if self.context is not None and address is not None: try: # Listen for commands self.tls_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.tls_sock.bind((address, tls_port)) self.tls_sock.listen(5) except OSError as err: print(err) logging.error(err.__str__()) sys.exit(1) else: logging.info('TLS-Server socket created on '+address+':'+ str(tls_port)) #Handle SIGTERM signal.signal(signal.SIGTERM, self.handle_sigterm)
def __init__(self, pidfile, loglevel, logfile, address=None, port=50002, db=None, password=None, keyfile=None, tls=False, tls_dir=None, tls_port=50003, tls_req=False): Daemon.__init__(self, pidfile) try: logdir = realpath(expanduser(getenv('XDG_DATA_HOME'))) except: logdir = realpath(expanduser('~/.local/share')) finally: logfile = join(logdir, 'keepassc', logfile) logging.basicConfig(format='[%(levelname)s] in %(filename)s:' '%(funcName)s at %(asctime)s\n%(message)s', level=loglevel, filename=logfile, filemode='a') if db is None: print('Need a database path') sys.exit(1) self.db_path = realpath(expanduser(db)) # To use this idiom only once, I store the keyfile path # as a class attribute if keyfile is not None: keyfile = realpath(expanduser(keyfile)) else: keyfile = None chdir("/var/empty") try: self.db = KPDBv1(self.db_path, password, keyfile) self.db.load() except KPError as err: print(err) logging.error(err.__str__()) sys.exit(1) self.lookup = { b'FIND': self.find, b'GET': self.send_db, b'CHANGESECRET': self.change_password, b'NEWG': self.create_group, b'NEWE': self.create_entry, b'DELG': self.delete_group, b'DELE': self.delete_entry, b'MOVG': self.move_group, b'MOVE': self.move_entry, b'TITG': self.set_g_title, b'TITE': self.set_e_title, b'USER': self.set_e_user, b'URL': self.set_e_url, b'COMM': self.set_e_comment, b'PASS': self.set_e_pass, b'DATE': self.set_e_exp } self.sock = None self.net_sock = None self.tls_sock = None self.tls_req = tls_req if tls is True or tls_req is True: self.context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) cert = join(tls_dir, "servercert.pem") key = join(tls_dir, "serverkey.pem") self.context.load_cert_chain(certfile=cert, keyfile=key) else: self.context = None try: # Listen for commands self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.bind(("localhost", 50000)) self.sock.listen(5) except OSError as err: print(err) logging.error(err.__str__()) sys.exit(1) else: logging.info('Server socket created on localhost:50000') if self.tls_req is False and address is not None: try: # Listen for commands self.net_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.net_sock.bind((address, port)) self.net_sock.listen(5) except OSError as err: print(err) logging.error(err.__str__()) sys.exit(1) else: logging.info('Server socket created on ' + address + ':' + str(port)) if self.context is not None and address is not None: try: # Listen for commands self.tls_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.tls_sock.bind((address, tls_port)) self.tls_sock.listen(5) except OSError as err: print(err) logging.error(err.__str__()) sys.exit(1) else: logging.info('TLS-Server socket created on ' + address + ':' + str(tls_port)) #Handle SIGTERM signal.signal(signal.SIGTERM, self.handle_sigterm)
def __init__(self, pidfile, loglevel, logfile, server_address='localhost', server_port=50000, agent_port=50001, password=None, keyfile=None, tls=False, tls_dir=None): Daemon.__init__(self, pidfile) try: logdir = realpath(expanduser(getenv('XDG_DATA_HOME'))) except: logdir = realpath(expanduser('~/.local/share')) finally: logfile = join(logdir, 'keepassc', logfile) logging.basicConfig(format='[%(levelname)s] in %(filename)s:' '%(funcName)s at %(asctime)s\n%(message)s', level=loglevel, filename=logfile, filemode='a') self.lookup = { b'FIND': self.find, b'GET': self.get_db, b'GETC': self.get_credentials } self.server_address = (server_address, server_port) try: # Listen for commands self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.bind(("localhost", agent_port)) self.sock.listen(1) except OSError as err: print(err) logging.error(err.__str__()) sys.exit(1) else: logging.info('Agent socket created on localhost:' + str(agent_port)) if tls_dir is not None: self.tls_dir = realpath(expanduser(tls_dir)).encode() else: self.tls_dir = b'' chdir("/var/empty") self.password = password # Agent is a daemon and cannot find the keyfile after run if keyfile is not None: with open(keyfile, "rb") as handler: self.keyfile = handler.read() handler.close() else: self.keyfile = b'' if tls is True: self.context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) self.context.verify_mode = ssl.CERT_REQUIRED self.context.load_verify_locations(tls_dir + "/cacert.pem") else: self.context = None #Handle SIGTERM signal.signal(signal.SIGTERM, self.handle_sigterm)