sector-f-linux

Sector F Linux
git clone https://git.sectorflabs.com/sector-f-linux.git
Log | Files | Refs | README | LICENSE

proto-greeter-direct (3184B)


      1 #!/usr/bin/python3
      2 """The other design for ADR-0012's first account: the greeter asks AccountsService itself.
      3 
      4 No helper and no new SELinux domain of our own: the greeter, running as the greeter user in the
      5 display-manager domain, calls AccountsService's CreateUser and SetPassword over D-Bus, and polkit
      6 decides whether it may (a rule, /etc/polkit-1/rules.d/, that allows it only while no human account
      7 exists). Otherwise like proto-greeter: with the account made, it logs it in through greetd.
      8 Its steps go to /tmp/proto-direct.log, since the greeter's own stderr does not reach the journal.
      9 """
     10 import json
     11 import os
     12 import pwd
     13 import socket
     14 import struct
     15 import subprocess
     16 import sys
     17 
     18 USER, FULL, PASSWORD = "erin", "Erin Prototype", "sectorf-proto"
     19 LOG = "/tmp/proto-direct.log"
     20 ACCOUNTS = ["busctl", "--system", "call", "org.freedesktop.Accounts"]
     21 
     22 
     23 def log(*a):
     24     with open(LOG, "a") as f:
     25         f.write(" ".join(str(x) for x in a) + "\n")
     26 
     27 
     28 def humans():
     29     return [p.pw_name for p in pwd.getpwall()
     30             if 1000 <= p.pw_uid < 60000 and not p.pw_shell.endswith(("nologin", "false"))]
     31 
     32 
     33 def accounts(path, iface, method, sig, *args):
     34     r = subprocess.run(ACCOUNTS + [path, iface, method, sig, *map(str, args)],
     35                        capture_output=True, text=True)
     36     log(method, "-> rc", r.returncode, r.stdout.strip()[:100], "|", r.stderr.strip()[:240])
     37     return r
     38 
     39 
     40 def send(s, msg):
     41     body = json.dumps(msg).encode()
     42     s.sendall(struct.pack("=I", len(body)) + body)
     43 
     44 
     45 def recvn(s, n):
     46     buf = b""
     47     while len(buf) < n:
     48         chunk = s.recv(n - len(buf))
     49         if not chunk:
     50             raise EOFError("greetd closed the socket")
     51         buf += chunk
     52     return buf
     53 
     54 
     55 def recv(s):
     56     (n,) = struct.unpack("=I", recvn(s, 4))
     57     return json.loads(recvn(s, n))
     58 
     59 
     60 log("--- start; human accounts:", humans())
     61 if not humans():
     62     made = accounts("/org/freedesktop/Accounts", "org.freedesktop.Accounts", "CreateUser", "ssi", USER, FULL, 1)
     63     if made.returncode:
     64         sys.exit(1)
     65     uid = pwd.getpwnam(USER).pw_uid
     66     crypted = subprocess.run(["openssl", "passwd", "-6", "-stdin"], input=PASSWORD + "\n",
     67                              capture_output=True, text=True).stdout.strip()
     68     path = f"/org/freedesktop/Accounts/User{uid}"
     69     pw = accounts(path, "org.freedesktop.Accounts.User", "SetPassword", "ss", crypted, "")
     70     if pw.returncode:
     71         log("password refused: removing the account again")
     72         accounts("/org/freedesktop/Accounts", "org.freedesktop.Accounts", "DeleteUser", "xb", uid, "true")
     73         sys.exit(1)
     74 
     75 g = socket.socket(socket.AF_UNIX)
     76 g.connect(os.environ["GREETD_SOCK"])
     77 send(g, {"type": "create_session", "username": USER})
     78 r = recv(g)
     79 while r["type"] == "auth_message":
     80     secret = r["auth_message_type"] == "secret"
     81     send(g, {"type": "post_auth_message_response", "response": PASSWORD if secret else None})
     82     r = recv(g)
     83 if r["type"] != "success":
     84     log("login refused:", r)
     85     send(g, {"type": "cancel_session"})
     86     sys.exit(1)
     87 send(g, {"type": "start_session", "cmd": ["patina-session", "--session"], "env": []})
     88 r = recv(g)
     89 log("start_session:", r)
     90 sys.exit(0 if r["type"] == "success" else 1)