Coverage for /home/antoine/projects/xpra-git/dist/python3/lib64/python/xpra/server/auth/sqlite_auth.py : 67%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2# This file is part of Xpra.
3# Copyright (C) 2017-2020 Antoine Martin <antoine@xpra.org>
4# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
5# later version. See the file COPYING for details.
7import os
8import sys
10from xpra.util import parse_simple_dict
11from xpra.server.auth.sys_auth_base import log, parse_uid, parse_gid
12from xpra.server.auth.sqlauthbase import SQLAuthenticator, DatabaseUtilBase, run_dbutil
15class Authenticator(SQLAuthenticator):
17 def __init__(self, username, filename="sqlite.sdb", **kwargs):
18 super().__init__(username, **kwargs)
19 if filename and not os.path.isabs(filename):
20 exec_cwd = kwargs.get("exec_cwd", os.getcwd())
21 filename = os.path.join(exec_cwd, filename)
22 self.filename = filename
23 self.password_query = kwargs.pop("password_query", "SELECT password FROM users WHERE username=(?)")
24 self.sessions_query = kwargs.pop("sessions_query",
25 "SELECT uid, gid, displays, env_options, session_options "+
26 "FROM users WHERE username=(?) AND password=(?)")
27 self.authenticate_check = self.authenticate_hmac
29 def __repr__(self):
30 return "sqlite"
32 def db_cursor(self, *sqlargs):
33 if not os.path.exists(self.filename):
34 log.error("Error: sqlauth cannot find the database file '%s'", self.filename)
35 return None
36 import sqlite3
37 db = sqlite3.connect(self.filename)
38 db.row_factory = sqlite3.Row
39 cursor = db.cursor()
40 cursor.execute(*sqlargs)
41 log("db_cursor(%s)=%s", sqlargs, cursor)
42 return cursor
44 def parse_session_data(self, data):
45 try:
46 uid = parse_uid(data["uid"])
47 gid = parse_gid(data["gid"])
48 displays = []
49 env_options = {}
50 session_options = {}
51 if data["displays"]:
52 displays = [x.strip() for x in str(data["displays"]).split(",")]
53 if data["env_options"]:
54 env_options = parse_simple_dict(str(data["env_options"]), ";")
55 if data["session_options"]:
56 session_options=parse_simple_dict(str(data["session_options"]), ";")
57 except Exception as e:
58 log("get_sessions() error on row %s", data, exc_info=True)
59 log.error("Error: sqlauth database row parsing problem:")
60 log.error(" %s", e)
61 return None
62 return uid, gid, displays, env_options, session_options
65class SqliteDatabaseUtil(DatabaseUtilBase):
67 def __init__(self, uri):
68 super().__init__(uri)
69 import sqlite3
70 assert sqlite3.paramstyle=="qmark"
71 self.param = "?"
73 def exec_database_sql_script(self, cursor_cb, *sqlargs):
74 import sqlite3
75 db = sqlite3.connect(self.uri)
76 cursor = db.cursor()
77 log("%s.execute%s", cursor, sqlargs)
78 cursor.execute(*sqlargs)
79 if cursor_cb:
80 cursor_cb(cursor)
81 db.commit()
82 return cursor
84 def get_authenticator_class(self):
85 return Authenticator
88def main(argv):
89 return run_dbutil(SqliteDatabaseUtil, "filename", argv)
91if __name__ == "__main__":
92 sys.exit(main(sys.argv))