su: Don't ask root for passwords.

root can su to any user without needing any password.
Same behavior as passwd for changing passwords.
This commit is contained in:
Augustin Cavalier
2025-07-02 17:20:01 -04:00
parent b58c23c7c6
commit 27e99e3a64
+16 -12
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2017, Haiku, Inc. All Rights Reserved.
* Copyright 2017-2025, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
@@ -75,20 +75,24 @@ main(int argc, char *argv[])
openlog(kProgramName, 0, LOG_AUTH);
status_t status = B_ERROR;
struct passwd* passwd = NULL;
status = authenticate_user("password: ", user, &passwd, NULL,
kRetries, false);
if (status < B_OK || !passwd) {
if (passwd != NULL)
syslog(LOG_NOTICE, "su failed for \"%s\"", passwd->pw_name);
else
syslog(LOG_NOTICE, "su attempt for non-existent user \"%s\"", user);
struct passwd* passwd = getpwnam(user);
if (passwd == NULL) {
syslog(LOG_NOTICE, "su attempt for non-existent user \"%s\"", user);
fprintf(stderr, "su: user \"%s\" does not exist\n", user);
exit(1);
}
// if not root, the user needs to authenticate
status_t status = B_ERROR;
if (getuid() != 0) {
status = authenticate_user("password: ", passwd, NULL, kRetries, false);
if (status < B_OK) {
syslog(LOG_NOTICE, "su failed for \"%s\"", passwd->pw_name);
exit(1);
}
}
// setup environment for the user
status = setup_environment(passwd, true, false);