-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
152 lines (119 loc) · 3.92 KB
/
main.py
File metadata and controls
152 lines (119 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import sys
import os
import pam
import subprocess
import time
import pwd
from PyQt5.QtCore import QObject, pyqtSlot, QThread, pyqtSignal
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
# os.environ["QML_DEBUG"] = "1"
app = QGuiApplication(sys.argv)
xsessions = "/usr/share/xsessions"
waylandsessions = "/usr/share/wayland-sessions"
# session_dirs = [xsessions, waylandsessions]
class Backend(QObject): # wth do I call this??
def __init__(self):
super().__init__()
# just set initilise the variables for use
self.selected_session = None
self.session_server = 'x11'
@pyqtSlot(result='QVariantMap')
def get_sessions(self):
sessions = {}
# all this nesting is probably bad xd, my bad gng...
# anyway, fetches all xsessions content
if os.path.exists(xsessions):
for file in os.listdir(xsessions):
if file.endswith(".desktop"):
with open(os.path.join(xsessions, file), "r") as f:
name, cmd = None, None
for line in f:
if line.startswith("Name="):
name = line.strip().split("=", 1)[1]
elif line.startswith("Exec="):
cmd = line.strip().split("=", 1)[1]
if name and cmd:
break
if name and cmd:
sessions[name] = {
"exec": cmd,
"server": "x11"
}
if os.path.exists(waylandsessions):
for file in os.listdir(waylandsessions):
if file.endswith(".desktop"):
with open(os.path.join(waylandsessions, file), "r") as f:
name, cmd = None, None
for line in f:
if line.startswith("Name="):
name = line.strip().split("=", 1)[1]
elif line.startswith("Exec="):
cmd = line.strip().split("=", 1)[1]
if name and cmd:
break
if name and cmd:
sessions[name] = {
"exec": cmd,
"server": "wayland"
}
return sessions
@pyqtSlot(str)
def select_session(self, name, server):
self.selected_session = name
self.session_server = "x11"
authFinished = pyqtSignal(bool, str)
@pyqtSlot(str, str)
def auth_user(self, username, password):
self.worker = AuthWorker(username, password)
self.worker.result.connect(self.authFinished)
self.worker.start()
def start_session(username, password):
if not self.selected_session:
print("No session selected")
return
sessions = self.get_sessions()
session = sessions.get(self.selected_session)
server = session['server']
# needed to actual start a PAM session
subprocess.Popen(['login', '-f', username])
# setting group and user id
os.setgid(pwd.getpwnam(username).pw_gid)
os.setuid(pwd.getpwnam(username).pw_uid)
env = os.environ.copy()
env["HOME"] = pwd.getpwnam(username).pw_dir
env["USER"] = username
env["LOGNAME"] = username
env["SHELL"] = pwd.getpwnam(username).pw_shell
env["XDG_RUNTIME_DIR"] = "/run/user/" + pwd.getpwnam(username).pw_uid
if server == 'x11':
env['XDG_SESSION_DIR'] = 'x11'
elif server == 'wayland': # ik i couldve written 'else' but hey xd
env['XDG_SESSION_DIR'] = 'wayland'
# replaces DM with your session, u probably wont be able to return but thats fine for now ( probably )
os.execle('/bin/sh', 'sh', '-c', session_cmd, env)
class AuthWorker(QThread):
result = pyqtSignal(bool, str)
def __init__(self, username, password):
super().__init__()
self.username = username
self.password = password
def run(self):
try:
res = pam.authenticate(self.username, self.password)
self.result.emit(res, "")
time.sleep(2)
# print('login')
except Exception as e:
self.result.emit(False, str(e))
engine = QQmlApplicationEngine()
engine.quit.connect(app.quit)
# engine.rootContext().setContextProperty("backend", Backend())
backend = Backend()
engine.rootContext().setContextProperty("backend", backend)
engine.load('layouts/main.qml')
print(Backend.get_sessions(0))
# print(Backend.get_sessions(0).get('Openbox')['server']) # little test
root = engine.rootObjects()[0]
root.showFullScreen()
sys.exit(app.exec())