///| Turning a request into words: what is asked, for which command, by whom. /// /// polkit's own message for run0 is "Authentication is required to start transient unit /// 'run-p15802-i1990.service'.": true, but it says nothing a person can judge. run0 asks /// systemd for a transient unit, systemd asks polkit, and polkit tells us run0's pid (the /// subject, `polkit.subject-pid`; the unit name `run-p-i.service` carries it too). From /// run0's command line we can show what it will run. pkexec puts the command in the details. ///| A process's command line (NUL-separated /proc//cmdline), empty when it is gone. pub fn proc_cmdline(pid : Int) -> Array[String] { if pid <= 0 { return [] } split_cmdline(@sys.read_proc_bytes("/proc/" + pid.to_string() + "/cmdline")) } ///| Split NUL-separated arguments. pub fn split_cmdline(b : Bytes) -> Array[String] { let out : Array[String] = [] let cur : Array[Byte] = [] for i = 0; i < b.length(); i = i + 1 { if b[i] == b'\x00' { out.push(@utf8.decode_lossy(Bytes::from_array(cur))) cur.clear() } else { cur.push(b[i]) } } if cur.length() > 0 { out.push(@utf8.decode_lossy(Bytes::from_array(cur))) } out } ///| A process's name (/proc//comm) and parent pid (/proc//stat field 4). pub fn proc_name_parent(pid : Int) -> (String, Int) { if pid <= 0 { return ("", 0) } let comm = @utf8.decode_lossy(@sys.read_proc_bytes("/proc/" + pid.to_string() + "/comm")).trim(chars="\n ").to_string() let stat = @utf8.decode_lossy(@sys.read_proc_bytes("/proc/" + pid.to_string() + "/stat")) // "pid (comm) S ppid ...": the name can hold spaces and parentheses, so read after the last ')'. let ppid = match stat.rev_find(")") { Some(i) => { let rest = stat[i + 1:].to_string().trim(chars=" ").to_string().split(" ").to_array() if rest.length() >= 2 { parse_uint(rest[1].to_string()).unwrap_or(0) } else { 0 } } None => 0 } (comm, ppid) } ///| The last part of a path. pub fn basename(p : String) -> String { match p.rev_find("/") { Some(i) => p[i + 1:].to_string() None => p } } ///| `run0`'s options that take a separate value (`-u root`, `--setenv FOO=1`). let run0_value_opts : Array[String] = [ "-u", "--user", "-g", "--group", "-D", "--chdir", "--setenv", "-p", "--property", "--description", "--slice", "--unit", "--nice", "--machine", "--background", "--shell-prompt-prefix", "--area", ] ///| What a `run0` (or `systemd-run`) command line runs: (target user, the command's arguments). /// The user is "root" unless -u/--user says otherwise; no arguments means a login shell. pub fn parse_run0(argv : Array[String]) -> (String, Array[String]) { let mut user = "root" let mut i = 1 while i < argv.length() { let a = argv[i] if a == "--" { i = i + 1 break } if !a.has_prefix("-") || a == "-" { break } if a.has_prefix("--user=") { user = a[7:].to_string() } else if a.has_prefix("-u") && a.length() > 2 { user = a[2:].to_string() } else if run0_value_opts.contains(a) { if (a == "-u" || a == "--user") && i + 1 < argv.length() { user = argv[i + 1] } i = i + 1 } i = i + 1 } let cmd : Array[String] = [] while i < argv.length() { cmd.push(argv[i]) i = i + 1 } (user, cmd) } ///| An argument as a shell would need it written: bare when safe, else in single quotes. pub fn shell_quote(a : String) -> String { if a == "" { return "''" } let mut safe = true for c in a { if !(c.is_ascii_alphabetic() || c.is_ascii_digit() || "-_./=:,+@%".contains_char(c)) { safe = false break } } if safe { a } else { "'" + a.replace_all(old="'", new="'\\''") + "'" } } ///| A command line for display. pub fn command_text(argv : Array[String]) -> String { argv.map(shell_quote).join(" ") } ///| The pid in a run0 unit name `run-p-i.service` (or `run-u.service`: none). pub fn run0_unit_pid(unit : String) -> Int? { if !unit.has_prefix("run-p") { return None } let rest = unit[5:].to_string() match rest.find("-") { Some(i) => parse_uint(rest[:i].to_string()) None => None } } ///| The quoted name in a message like "... transient unit 'run-p15802-i1990.service'.". pub fn quoted_in(message : String) -> String { match message.find("'") { Some(a) => { let rest = message[a + 1:].to_string() match rest.find("'") { Some(b) => rest[:b].to_string() None => "" } } None => "" } } ///| How the dialog presents a request. pub(all) struct Description { /// The headline ("Run a command as root", or polkit's message for other actions). title : String /// One line under it: what allowing does. subtitle : String /// The command that will run, written as a shell would ("" when it isn't a command). command : String /// The command's program name (for its app icon), "" when none. program : String /// Label/value rows for the Details section. details : Array[(String, String)] } ///| Who asked: the subject process, then its parent ("run0 (pid 15802) from bash"). fn requester(pid : Int) -> String { if pid <= 0 { return "" } let (name, ppid) = proc_name_parent(pid) if name == "" { return "pid " + pid.to_string() } let (pname, _) = proc_name_parent(ppid) name + " (pid " + pid.to_string() + ")" + (if pname != "" && ppid > 1 { ", started from " + pname } else { "" }) } ///| Words for `req`. `cmdline` reads a process's arguments (tests pass their own). pub fn describe(req : AuthRequest, cmdline? : (Int) -> Array[String] = proc_cmdline, who? : (Int) -> String = requester) -> Description { let subject = parse_uint(req.detail("polkit.subject-pid")).unwrap_or(0) let caller = parse_uint(req.detail("polkit.caller-pid")).unwrap_or(0) let unit = if req.detail("unit") != "" { req.detail("unit") } else { quoted_in(req.message) } let details : Array[(String, String)] = [] let add = fn(k : String, v : String) { if v != "" { details.push((k, v)) } } // run0 / systemd-run: a transient unit for a process we can read. let run0_pid = if req.action_id == "org.freedesktop.systemd1.manage-units" { match run0_unit_pid(unit) { Some(p) => p None => if unit.has_prefix("run-") { subject } else { 0 } } } else { 0 } let run0_argv = if run0_pid > 0 { let a = cmdline(if subject > 0 { subject } else { run0_pid }) if a.length() > 0 { a } else { cmdline(run0_pid) } } else { [] } let tool = if run0_argv.length() > 0 { basename(run0_argv[0]) } else { "" } if tool == "run0" || tool == "systemd-run" { let (user, cmd) = parse_run0(run0_argv) let target = if user == "root" { "root" } else { user } let shell = cmd.length() == 0 add("Requested by", who(if subject > 0 { subject } else { run0_pid })) add("Runs as", target) add("Unit", unit) add("Action", req.action_id) return { title: if shell { "Open a shell as " + target } else { "Run a command as " + target }, subtitle: tool + " needs your password to " + (if shell { "start a " + target + " shell." } else { "run this as " + target + ":" }), command: command_text(cmd), program: if shell { "" } else { basename(cmd[0]) }, details, } } // pkexec: the details carry the command. if req.action_id == "org.freedesktop.policykit.exec" || req.detail("command_line") != "" { let user = if req.detail("user") != "" { req.detail("user") } else { "root" } let line = req.detail("command_line") let program = basename(if req.detail("program") != "" { req.detail("program") } else { line.split(" ").to_array().get(0).map(v => v.to_string()).unwrap_or("") }) add("Requested by", who(if subject > 0 { subject } else { caller })) add("Runs as", user) add("Action", req.action_id) return { title: "Run a program as " + user, subtitle: "pkexec needs your password to run this as " + user + ":", command: line, program, details, } } // Anything else: polkit's message is the best we have. add("Requested by", who(if subject > 0 { subject } else { caller })) add("Unit", req.detail("unit")) add("Action", req.action_id) { title: if req.message != "" { req.message } else { "Authentication required" }, subtitle: "An app wants permission for a system action. Enter your password to allow it.", command: "", program: "", details, } }