///| What polkit asks the agent to authenticate (BeginAuthentication), and who could answer. ///| A user who can authenticate: uid, login name and real name (from the passwd database). pub(all) struct AuthUser { uid : Int name : String real_name : String } derive(Eq, Debug) ///| One BeginAuthentication call. pub(all) struct AuthRequest { action_id : String /// polkit's translated message ("Authentication is required to ..."). message : String icon_name : String details : Map[String, String] cookie : String /// The users that may answer (unix-user identities, and the members of unix-group ones). users : Array[AuthUser] } ///| A detail by key, "" when absent. pub fn AuthRequest::detail(self : AuthRequest, key : String) -> String { self.details.get(key).unwrap_or("") } ///| A passwd/group database line split on ':'. fn fields(line : String) -> Array[String] { line.split(":").map(v => v.to_string()).to_array() } ///| Parse /etc/passwd text: uid -> (name, real name). The real name is GECOS up to its first ','. pub fn parse_passwd(text : String) -> Map[Int, (String, String)] { let out : Map[Int, (String, String)] = {} for raw in text.split("\n") { let f = fields(raw.to_string()) if f.length() < 5 { continue } let uid = match parse_uint(f[2]) { Some(v) => v None => continue } let gecos = match f[4].find(",") { Some(i) => f[4][:i].to_string() None => f[4] } if !out.contains(uid) { out.set(uid, (f[0], gecos)) } } out } ///| Parse /etc/group text: gid -> member names. pub fn parse_group(text : String) -> Map[Int, Array[String]] { let out : Map[Int, Array[String]] = {} for raw in text.split("\n") { let f = fields(raw.to_string()) if f.length() < 4 { continue } let gid = match parse_uint(f[2]) { Some(v) => v None => continue } let members = f[3].split(",").map(v => v.to_string().trim(chars=" ").to_string()).filter(v => v != "").to_array() out.set(gid, members) } out } ///| A decimal number. fn parse_uint(s : String) -> Int? { if s.length() == 0 || s.length() > 10 { return None } let mut v = 0 for c in s { if c < '0' || c > '9' { return None } v = v * 10 + (c.to_int() - '0'.to_int()) } Some(v) } ///| The users polkit's identities (a(sa{sv}): ("unix-user", {uid}) or ("unix-group", {gid})) /// stand for, in order, without repeats; a group stands for its members. `self_uid` (the user /// at the screen) is listed first when it is among them, as other agents do. pub fn users_for_identities( identities : Array[@network.DValue], passwd : Map[Int, (String, String)], groups : Map[Int, Array[String]], self_uid : Int, ) -> Array[AuthUser] { let out : Array[AuthUser] = [] let by_name : Map[String, Int] = {} for uid, e in passwd { by_name.set(e.0, uid) } let add = fn(uid : Int) { if out.iter().any(u => u.uid == uid) { return } let (name, real_name) = match passwd.get(uid) { Some(e) => e None => (uid.to_string(), "") } out.push({ uid, name, real_name }) } let num = fn(v : @network.DValue) -> Int? { match v { Variant(inner) => match inner { U32(x) => Some(x.reinterpret_as_int()) I32(x) => Some(x) _ => None } U32(x) => Some(x.reinterpret_as_int()) I32(x) => Some(x) _ => None } } for id in identities { match id { Struct([Str(kind), Arr(_, entries)]) => for e in entries { match e { Entry(Str(k), v) => match (kind, k, num(v)) { ("unix-user", "uid", Some(uid)) => add(uid) ("unix-group", "gid", Some(gid)) => for m in groups.get(gid).unwrap_or([]) { match by_name.get(m) { Some(uid) => add(uid) None => () } } _ => () } _ => () } } _ => () } } // The user at the screen first. match out.search_by(u => u.uid == self_uid) { Some(i) if i > 0 => { let me = out.remove(i) out.insert(0, me) } _ => () } out } ///| A string-to-string dict (a{ss}) as a map. pub fn string_dict(v : @network.DValue) -> Map[String, String] { let out : Map[String, String] = {} match v { Arr(_, entries) => for e in entries { match e { Entry(Str(k), Str(x)) => out.set(k, x) _ => () } } _ => () } out } ///| Parse real UID from `/proc//status` ("Uid:\treal\teffective..."); -1 when unknown. pub fn uid_from_status(text : String) -> Int { for line in text.split("\n") { let l = line.to_string() if l.has_prefix("Uid:") { let rest = l[4:].to_string().replace_all(old="\t", new=" ") let parts = rest.split(" ").map(v => v.to_string()).filter(v => v != "").to_array() if parts.length() > 0 { return parse_uint(parts[0]).unwrap_or(-1) } } } -1 } ///| Our uid, from /proc/self/status ("Uid:\treal\teffective..."); -1 when unknown. pub fn own_uid() -> Int { uid_from_status(@utf8.decode_lossy(@sys.read_proc_bytes("/proc/self/status"))) } ///| The request BeginAuthentication's arguments describe (s s s a{ss} s a(sa{sv})); None when /// they don't match. Users come from /etc/passwd and /etc/group. pub fn AuthRequest::from_args(args : Array[@network.DValue]) -> AuthRequest? { match args { [Str(action_id), Str(message), Str(icon_name), details, Str(cookie), Arr(_, ids)] => { let passwd = parse_passwd(@utf8.decode_lossy(@sys.read_file_bytes("/etc/passwd"))) let groups = parse_group(@utf8.decode_lossy(@sys.read_file_bytes("/etc/group"))) let me = own_uid() // Users outside /etc/passwd (systemd-homed, LDAP): the one at the screen is $USER. if me >= 0 && !passwd.contains(me) && @sys.getenv("USER") != "" { passwd.set(me, (@sys.getenv("USER"), "")) } let users = users_for_identities(ids, passwd, groups, me) Some({ action_id, message, icon_name, details: string_dict(details), cookie, users }) } _ => None } }