| 1 | use core::fmt; |
| 2 | |
| 3 | use crate::sudoers::{ |
| 4 | ast::{Authenticate, RunAs, Tag}, |
| 5 | tokens::ChDir, |
| 6 | }; |
| 7 | |
| 8 | use super::Entry; |
| 9 | |
| 10 | pub struct Verbose<'a>(pub Entry<'a>); |
| 11 | |
| 12 | impl fmt::Display for Verbose<'_> { |
| 13 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 14 | let Self(Entry { |
| 15 | run_as, |
| 16 | cmd_specs, |
| 17 | cmd_alias, |
| 18 | }) = self; |
| 19 | |
| 20 | let root_runas = super::root_runas(); |
| 21 | let run_as = run_as.unwrap_or(&root_runas); |
| 22 | |
| 23 | let mut last_tag = None; |
| 24 | for (tag, cmd_spec) in cmd_specs { |
| 25 | if last_tag != Some(tag) { |
| 26 | let is_first_iteration = last_tag.is_none(); |
| 27 | if !is_first_iteration { |
| 28 | f.write_str("\n")?; |
| 29 | } |
| 30 | |
| 31 | write_entry_header(run_as, f)?; |
| 32 | write_tag(f, tag)?; |
| 33 | write!(f, "\n {}", xlat!("Commands:"))?; |
| 34 | } |
| 35 | last_tag = Some(tag); |
| 36 | |
| 37 | f.write_str("\n\t")?; |
| 38 | super::write_spec(f, cmd_spec, cmd_alias.iter().rev(), true, "\n\t")?; |
| 39 | } |
| 40 | |
| 41 | Ok(()) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | fn write_entry_header(run_as: &RunAs, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 46 | write!(f, "\n{}", xlat!("Sudoers entry:"))?; |
| 47 | |
| 48 | write_users(run_as, f)?; |
| 49 | write_groups(run_as, f) |
| 50 | } |
| 51 | |
| 52 | fn write_users(run_as: &RunAs, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 53 | // TRANSLATORS: This is sudo-specific jargon. |
| 54 | write!(f, "\n {}: ", xlat!("RunAsUsers"))?; |
| 55 | super::write_users(run_as, f) |
| 56 | } |
| 57 | |
| 58 | fn write_groups(run_as: &RunAs, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 59 | if run_as.groups.is_empty() { |
| 60 | return Ok(()); |
| 61 | } |
| 62 | |
| 63 | // TRANSLATORS: This is sudo-specific jargon. |
| 64 | write!(f, "\n {}: ", xlat!("RunAsGroups"))?; |
| 65 | super::write_groups(run_as, f) |
| 66 | } |
| 67 | |
| 68 | fn write_tag(f: &mut fmt::Formatter, tag: &Tag) -> fmt::Result { |
| 69 | if tag.authenticate != Authenticate::None { |
| 70 | write!(f, "\n {}: ", xlat!("Options"))?; |
| 71 | if tag.authenticate != Authenticate::Passwd { |
| 72 | f.write_str("!")?; |
| 73 | } |
| 74 | f.write_str("authenticate")?; |
| 75 | } |
| 76 | |
| 77 | if let Some(cwd) = &tag.cwd { |
| 78 | // TRANSLATORS: This is sudo-specific jargon. |
| 79 | write!(f, "\n {}: ", xlat!("Cwd"))?; |
| 80 | match cwd { |
| 81 | ChDir::Path(path) => write!(f, "{}", path.display())?, |
| 82 | ChDir::Any => f.write_str("*")?, |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | Ok(()) |
| 87 | } |