| 1 | use std::io::Read; |
| 2 | use std::fs::File; |
| 3 | use std::fmt::{Display, Debug}; |
| 4 | use std::path::Path; |
| 5 | |
| 6 | use nix::unistd::getuid; |
| 7 | |
| 8 | |
| 9 | pub trait Explainable: Display + Debug { |
| 10 | fn explain(&self) -> String; |
| 11 | } |
| 12 | |
| 13 | pub fn exists(path: &Path) -> &'static str { |
| 14 | if path.exists() { |
| 15 | "exists" |
| 16 | } else { |
| 17 | "missing" |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | pub fn user() -> &'static str { |
| 22 | let uid = getuid(); |
| 23 | if u32::from(uid) == 0 { |
| 24 | let mut buf = String::with_capacity(100); |
| 25 | match File::open("/proc/self/uid_map") |
| 26 | .and_then(|mut f| f.read_to_string(&mut buf)) |
| 27 | { |
| 28 | Ok(_) => { |
| 29 | if buf == " 0 0 4294967295\n" { |
| 30 | "superuser" |
| 31 | } else { |
| 32 | "mapped-root" |
| 33 | } |
| 34 | } |
| 35 | Err(_) => { |
| 36 | "privileged" |
| 37 | } |
| 38 | } |
| 39 | } else { |
| 40 | "regular-user" |
| 41 | } |
| 42 | } |