| 1 | use std::{ |
| 2 | borrow::Cow, |
| 3 | ffi::{OsStr, OsString}, |
| 4 | ops::ControlFlow, |
| 5 | path::Path, |
| 6 | }; |
| 7 | |
| 8 | use crate::{ |
| 9 | common::{Context, DisplayOsStr, Error}, |
| 10 | sudo::cli::SudoListOptions, |
| 11 | sudoers::{Authorization, ListRequest, Request, Sudoers}, |
| 12 | system::User, |
| 13 | }; |
| 14 | |
| 15 | use super::auth_and_update_record_file; |
| 16 | |
| 17 | pub(in crate::sudo) fn run_list(cmd_opts: SudoListOptions) -> Result<(), Error> { |
| 18 | let verbose_list_mode = cmd_opts.list.is_verbose(); |
| 19 | let other_user = cmd_opts |
| 20 | .other_user |
| 21 | .as_ref() |
| 22 | .map(|username| { |
| 23 | User::from_name(username.as_cstr())? |
| 24 | .ok_or_else(|| Error::UserNotFound(username.clone().into())) |
| 25 | }) |
| 26 | .transpose()?; |
| 27 | |
| 28 | let original_command = cmd_opts.positional_args.first().cloned(); |
| 29 | |
| 30 | let mut sudoers = super::read_sudoers()?; |
| 31 | |
| 32 | let context = Context::from_list_opts(cmd_opts, &mut sudoers)?; |
| 33 | |
| 34 | if auth_invoking_user(&context, &mut sudoers, &original_command, &other_user)?.is_break() { |
| 35 | return Ok(()); |
| 36 | } |
| 37 | |
| 38 | if let Some(original_command) = original_command { |
| 39 | check_sudo_command_perms(&original_command, context, &other_user, &mut sudoers)?; |
| 40 | } else { |
| 41 | let inspected_user = other_user.as_ref().unwrap_or(&context.current_user); |
| 42 | let mut matching_entries = sudoers |
| 43 | .matching_entries(inspected_user, &context.hostname) |
| 44 | .peekable(); |
| 45 | |
| 46 | if matching_entries.peek().is_some() { |
| 47 | xlat_println!( |
| 48 | "User {user} may run the following commands on {hostname}:", |
| 49 | user = inspected_user.name, |
| 50 | hostname = context.hostname |
| 51 | ); |
| 52 | |
| 53 | for entry in matching_entries { |
| 54 | if verbose_list_mode { |
| 55 | let entry = entry.verbose(); |
| 56 | println_ignore_io_error!("{entry}"); |
| 57 | } else { |
| 58 | println_ignore_io_error!("{entry}"); |
| 59 | } |
| 60 | } |
| 61 | } else { |
| 62 | xlat_println!( |
| 63 | "User {user} is not allowed to run sudo on {hostname}.", |
| 64 | user = inspected_user.name, |
| 65 | hostname = context.hostname, |
| 66 | ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | Ok(()) |
| 71 | } |
| 72 | |
| 73 | fn auth_invoking_user( |
| 74 | context: &Context, |
| 75 | sudoers: &mut Sudoers, |
| 76 | original_command: &Option<OsString>, |
| 77 | other_user: &Option<User>, |
| 78 | ) -> Result<ControlFlow<(), ()>, Error> { |
| 79 | let inspected_user = other_user.as_ref().unwrap_or(&context.current_user); |
| 80 | |
| 81 | let list_request = ListRequest { |
| 82 | inspected_user, |
| 83 | target_user: &context.target_user, |
| 84 | target_group: &context.target_group, |
| 85 | }; |
| 86 | match sudoers.check_list_permission(&*context.current_user, &context.hostname, list_request) { |
| 87 | Authorization::Allowed(auth, ()) => { |
| 88 | auth_and_update_record_file(context, auth)?; |
| 89 | Ok(ControlFlow::Continue(())) |
| 90 | } |
| 91 | |
| 92 | Authorization::Forbidden => { |
| 93 | let command = if other_user.is_none() { |
| 94 | "sudo".into() |
| 95 | } else { |
| 96 | format_list_command(original_command) |
| 97 | }; |
| 98 | |
| 99 | Err(Error::NotAllowed { |
| 100 | username: context.current_user.name.clone(), |
| 101 | command, |
| 102 | hostname: context.hostname.clone(), |
| 103 | other_user: other_user.as_ref().map(|user| &user.name).cloned(), |
| 104 | }) |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | fn check_sudo_command_perms( |
| 110 | original_command: &OsStr, |
| 111 | context: Context, |
| 112 | other_user: &Option<User>, |
| 113 | sudoers: &mut Sudoers, |
| 114 | ) -> Result<(), Error> { |
| 115 | let user = other_user.as_ref().unwrap_or(&context.current_user); |
| 116 | |
| 117 | let request = Request { |
| 118 | user: &context.target_user, |
| 119 | group: &context.target_group, |
| 120 | command: &context.command.command, |
| 121 | arguments: &context.command.arguments, |
| 122 | }; |
| 123 | |
| 124 | let judgement = sudoers.check(user, &context.hostname, request); |
| 125 | |
| 126 | if let Authorization::Forbidden = judgement.authorization() { |
| 127 | return Err(Error::Silent); |
| 128 | } else { |
| 129 | if !context.command.resolved { |
| 130 | return Err(Error::CommandNotFound(context.command.command)); |
| 131 | } |
| 132 | let command_is_relative_path = original_command.as_encoded_bytes().contains(&b'/') |
| 133 | && !Path::new(&original_command).is_absolute(); |
| 134 | let command = if command_is_relative_path { |
| 135 | original_command |
| 136 | } else { |
| 137 | let resolved_command = &context.command.command; |
| 138 | resolved_command.as_os_str() |
| 139 | }; |
| 140 | |
| 141 | if context.command.arguments.is_empty() { |
| 142 | println_ignore_io_error!("{}", DisplayOsStr(command)); |
| 143 | } else { |
| 144 | println_ignore_io_error!( |
| 145 | "{} {}", |
| 146 | DisplayOsStr(command), |
| 147 | DisplayOsStr(&context.command.arguments.join(OsStr::new(" "))), |
| 148 | ); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | Ok(()) |
| 153 | } |
| 154 | |
| 155 | fn format_list_command(original_command: &Option<OsString>) -> Cow<'static, str> { |
| 156 | if let Some(original_command) = original_command { |
| 157 | format!("list {}", DisplayOsStr(original_command)).into() |
| 158 | } else { |
| 159 | "list".into() |
| 160 | } |
| 161 | } |