validate_sudo_password

Function validate_sudo_password 

Source
pub fn validate_sudo_password(password: &str) -> Result<bool, String>
Expand description

What: Validate a sudo password without executing any command.

Inputs:

  • password: Password to validate.

Output:

  • Ok(true) if password is valid, Ok(false) if invalid, or Err(String) on error.

ยงErrors

  • Returns Err if the validation command cannot be executed (e.g., sudo not available).

Details:

  • First invalidates cached sudo credentials with sudo -k to ensure fresh validation.
  • Then executes printf '%s\n' '<password>' | sudo -S -v to test password validity.
  • Uses printf instead of echo for more reliable password handling.
  • Uses sudo -v which validates credentials without executing a command.
  • Returns Ok(true) if password is valid, Ok(false) if invalid.
  • Handles errors appropriately (e.g., if sudo is not available).