| 1 | #![forbid(unsafe_code)] |
| 2 | |
| 3 | use craterust-analyzer cargo sudo-rs 0.2.14 crate/::commonrust-analyzer cargo sudo-rs 0.2.14 common/::errorrust-analyzer cargo sudo-rs 0.2.14 common/error/::Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#; |
| 4 | use craterust-analyzer cargo sudo-rs 0.2.14 crate/::logrust-analyzer cargo sudo-rs 0.2.14 log/::user_warnrust-analyzer cargo sudo-rs 0.2.14 log/user_warn!; |
| 5 | use craterust-analyzer cargo sudo-rs 0.2.14 crate/::pamrust-analyzer cargo sudo-rs 0.2.14 pam/::{PamContextrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#, PamErrorrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#, PamErrorTyperust-analyzer cargo sudo-rs 0.2.14 pam/error/PamErrorType#}; |
| 6 | use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::termrust-analyzer cargo sudo-rs 0.2.14 system/term/::current_tty_namerust-analyzer cargo sudo-rs 0.2.14 system/term/current_tty_name().; |
| 7 | |
| 8 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/; |
| 9 | |
| 10 | use clirust-analyzer cargo sudo-rs 0.2.14 su/cli/::SuActionrust-analyzer cargo sudo-rs 0.2.14 su/cli/SuAction#; |
| 11 | use contextrust-analyzer cargo sudo-rs 0.2.14 su/context/::SuContextrust-analyzer cargo sudo-rs 0.2.14 su/context/SuContext#; |
| 12 | use helprust-analyzer cargo sudo-rs 0.2.14 su/help/::{USAGE_MSGrust-analyzer cargo sudo-rs 0.2.14 su/help/USAGE_MSG., long_help_messagerust-analyzer cargo sudo-rs 0.2.14 su/help/long_help_message().}; |
| 13 | |
| 14 | use selfrust-analyzer cargo sudo-rs 0.2.14 su/::clirust-analyzer cargo sudo-rs 0.2.14 su/cli/::SuRunOptionsrust-analyzer cargo sudo-rs 0.2.14 su/cli/SuRunOptions#; |
| 15 | |
| 16 | mod clirust-analyzer cargo sudo-rs 0.2.14 su/cli/; |
| 17 | mod contextrust-analyzer cargo sudo-rs 0.2.14 su/context/; |
| 18 | mod helprust-analyzer cargo sudo-rs 0.2.14 su/help/; |
| 19 | |
| 20 | const DEFAULT_USERrust-analyzer cargo sudo-rs 0.2.14 su/DEFAULT_USER.: &str = "root"; |
| 21 | const VERSIONrust-analyzer cargo sudo-rs 0.2.14 su/VERSION.: &str = envrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/env!!("CARGO_PKG_VERSION"); |
| 22 | |
| 23 | fn authenticaterust-analyzer cargo sudo-rs 0.2.14 su/authenticate().(requesting_user: &str, user: &str, login: bool) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<PamContextrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#, Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#> { |
| 24 | // FIXME make it configurable by the packager |
| 25 | let context = if login && cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_os = "linux") { |
| 26 | "su-l" |
| 27 | } else { |
| 28 | "su" |
| 29 | }; |
| 30 | let use_stdin = true; |
| 31 | let mut pam = PamContextrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#::new_clirust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]new_cli().( |
| 32 | "su", |
| 33 | context, |
| 34 | false, |
| 35 | use_stdin, |
| 36 | false, |
| 37 | false, |
| 38 | false, |
| 39 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 40 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(user), |
| 41 | )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 42 | pam.set_requesting_userrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]set_requesting_user().(requesting_user)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 43 | |
| 44 | // attempt to set the TTY this session is communicating on |
| 45 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(pam_tty) = current_tty_namerust-analyzer cargo sudo-rs 0.2.14 system/term/current_tty_name().() { |
| 46 | pam.set_ttyrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]set_tty().(&pam_tty)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 47 | } |
| 48 | |
| 49 | pam.mark_silentrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]mark_silent().(true); |
| 50 | pam.mark_allow_null_auth_tokenrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]mark_allow_null_auth_token().(true); |
| 51 | |
| 52 | let mut max_tries = 3; |
| 53 | let mut current_try = 0; |
| 54 | |
| 55 | loop { |
| 56 | current_try rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`AddAssign<Self>`]add_assign().1; |
| 57 | match pam.authenticaterust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]authenticate().(user) { |
| 58 | // there was no error, so authentication succeeded |
| 59 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(_) => break, |
| 60 | |
| 61 | // maxtries was reached, pam does not allow any more tries |
| 62 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PamErrorrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#::Pamrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#Pam#(PamErrorTyperust-analyzer cargo sudo-rs 0.2.14 pam/error/PamErrorType#::MaxTriesrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamErrorType#MaxTries#)) => { |
| 63 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::MaxAuthAttemptsrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#MaxAuthAttempts#(current_try)); |
| 64 | } |
| 65 | |
| 66 | // there was an authentication error, we can retry |
| 67 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PamErrorrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#::Pamrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#Pam#(PamErrorTyperust-analyzer cargo sudo-rs 0.2.14 pam/error/PamErrorType#::AuthErrorrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamErrorType#AuthError#)) => { |
| 68 | max_tries rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`SubAssign<Self>`]sub_assign().1; |
| 69 | if max_tries rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().0 { |
| 70 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::MaxAuthAttemptsrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#MaxAuthAttempts#(current_try)); |
| 71 | } else { |
| 72 | user_warnrust-analyzer cargo sudo-rs 0.2.14 log/user_warn!!("Authentication failed, try again."); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // there was another pam error, return the error |
| 77 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => { |
| 78 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | pam.validate_account_or_change_auth_tokenrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]validate_account_or_change_auth_token().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 84 | pam.open_sessionrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]open_session().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 85 | |
| 86 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(pam) |
| 87 | } |
| 88 | |
| 89 | fn runrust-analyzer cargo sudo-rs 0.2.14 su/run().(options: SuRunOptionsrust-analyzer cargo sudo-rs 0.2.14 su/cli/SuRunOptions#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#> { |
| 90 | // lookup user and build context object |
| 91 | let context = SuContextrust-analyzer cargo sudo-rs 0.2.14 su/context/SuContext#::from_envrust-analyzer cargo sudo-rs 0.2.14 su/context/impl#[SuContext]from_env().(options)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 92 | |
| 93 | // authenticate the target user |
| 94 | let mut pam: PamContextrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext# = authenticaterust-analyzer cargo sudo-rs 0.2.14 su/authenticate().( |
| 95 | &context.requesting_userrust-analyzer cargo sudo-rs 0.2.14 su/context/SuContext#requesting_user..namerust-analyzer cargo sudo-rs 0.2.14 system/User#name., |
| 96 | &context.userrust-analyzer cargo sudo-rs 0.2.14 su/context/SuContext#user..namerust-analyzer cargo sudo-rs 0.2.14 system/User#name., |
| 97 | context.optionsrust-analyzer cargo sudo-rs 0.2.14 su/context/SuContext#options..loginrust-analyzer cargo sudo-rs 0.2.14 su/cli/SuRunOptions#login., |
| 98 | )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 99 | |
| 100 | // su in all cases uses PAM (pam_getenvlist(3)) to do the |
| 101 | // final environment modification. Command-line options such as |
| 102 | // --login and --preserve-environment affect the environment before |
| 103 | // it is modified by PAM. |
| 104 | let mut environment = context.environmentrust-analyzer cargo sudo-rs 0.2.14 su/context/SuContext#environment..clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][Clone]clone().(); |
| 105 | environment.extendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][`Extend<(K, V)>`]extend().(pam.envrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]env().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 106 | |
| 107 | // run command and return corresponding exit code |
| 108 | let command_exit_reason = craterust-analyzer cargo sudo-rs 0.2.14 crate/::execrust-analyzer cargo sudo-rs 0.2.14 exec/::run_commandrust-analyzer cargo sudo-rs 0.2.14 exec/run_command().(context.as_run_optionsrust-analyzer cargo sudo-rs 0.2.14 su/context/impl#[SuContext]as_run_options().(), environment); |
| 109 | |
| 110 | pam.close_sessionrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]close_session().(); |
| 111 | |
| 112 | match command_exit_reason?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..exit_processrust-analyzer cargo sudo-rs 0.2.14 exec/impl#[ExitReason]exit_process().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). {} |
| 113 | } |
| 114 | |
| 115 | pub fn mainrust-analyzer cargo sudo-rs 0.2.14 su/main().() { |
| 116 | craterust-analyzer cargo sudo-rs 0.2.14 crate/::logrust-analyzer cargo sudo-rs 0.2.14 log/::SudoLoggerrust-analyzer cargo sudo-rs 0.2.14 log/SudoLogger#::newrust-analyzer cargo sudo-rs 0.2.14 log/impl#[SudoLogger]new().("su: ").into_global_loggerrust-analyzer cargo sudo-rs 0.2.14 log/impl#[SudoLogger]into_global_logger().(); |
| 117 | |
| 118 | let action = match SuActionrust-analyzer cargo sudo-rs 0.2.14 su/cli/SuAction#::from_envrust-analyzer cargo sudo-rs 0.2.14 su/cli/impl#[SuAction]from_env().() { |
| 119 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(action) => action, |
| 120 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error) => { |
| 121 | println_ignore_io_errorrust-analyzer cargo sudo-rs 0.2.14 macros/println_ignore_io_error!!("su: {error}\n{USAGE_MSG}"); |
| 122 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(1); |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | match action { |
| 127 | SuActionrust-analyzer cargo sudo-rs 0.2.14 su/cli/SuAction#::Helprust-analyzer cargo sudo-rs 0.2.14 su/cli/SuAction#Help#(_) => { |
| 128 | println_ignore_io_errorrust-analyzer cargo sudo-rs 0.2.14 macros/println_ignore_io_error!!("{}", long_help_messagerust-analyzer cargo sudo-rs 0.2.14 su/help/long_help_message().()); |
| 129 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(0); |
| 130 | } |
| 131 | SuActionrust-analyzer cargo sudo-rs 0.2.14 su/cli/SuAction#::Versionrust-analyzer cargo sudo-rs 0.2.14 su/cli/SuAction#Version#(_) => { |
| 132 | eprintln_ignore_io_errorrust-analyzer cargo sudo-rs 0.2.14 macros/eprintln_ignore_io_error!!("su-rs {VERSION}"); |
| 133 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(0); |
| 134 | } |
| 135 | SuActionrust-analyzer cargo sudo-rs 0.2.14 su/cli/SuAction#::Runrust-analyzer cargo sudo-rs 0.2.14 su/cli/SuAction#Run#(options) => match runrust-analyzer cargo sudo-rs 0.2.14 su/run().(options) { |
| 136 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::CommandNotFoundrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#CommandNotFound#(c)) => { |
| 137 | eprintln_ignore_io_errorrust-analyzer cargo sudo-rs 0.2.14 macros/eprintln_ignore_io_error!!("su: {}", Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::CommandNotFoundrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#CommandNotFound#(c)); |
| 138 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(127); |
| 139 | } |
| 140 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::InvalidCommandrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#InvalidCommand#(c)) => { |
| 141 | eprintln_ignore_io_errorrust-analyzer cargo sudo-rs 0.2.14 macros/eprintln_ignore_io_error!!("su: {}", Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::InvalidCommandrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#InvalidCommand#(c)); |
| 142 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(126); |
| 143 | } |
| 144 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error) => { |
| 145 | eprintln_ignore_io_errorrust-analyzer cargo sudo-rs 0.2.14 macros/eprintln_ignore_io_error!!("su: {error}"); |
| 146 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(1); |
| 147 | } |
| 148 | _ => {} |
| 149 | }, |
| 150 | }; |
| 151 | } |