2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-09-11 19:53:25 +03:00
|
|
|
#include <AK/ScopeGuard.h>
|
2020-07-31 02:35:30 +03:00
|
|
|
#include <LibCore/Account.h>
|
2020-09-18 10:49:51 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-07-26 03:36:32 +03:00
|
|
|
#include <LibCore/GetPassword.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2019-02-22 01:35:07 +03:00
|
|
|
|
|
|
|
extern "C" int main(int, char**);
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2021-01-09 19:53:30 +03:00
|
|
|
if (pledge("stdio rpath tty exec id", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-11-08 16:52:33 +03:00
|
|
|
if (!isatty(STDIN_FILENO)) {
|
|
|
|
warnln("{}: standard in is not a terminal", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-08-05 21:26:32 +03:00
|
|
|
const char* user = nullptr;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_positional_argument(user, "User to switch to (defaults to user with UID 0)", "user", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.parse(argc, argv);
|
|
|
|
|
2021-01-10 00:14:20 +03:00
|
|
|
if (geteuid() != 0) {
|
|
|
|
warnln("Not running as root :(");
|
|
|
|
return 1;
|
|
|
|
}
|
2020-06-16 22:16:09 +03:00
|
|
|
|
2021-01-09 19:44:44 +03:00
|
|
|
auto account_or_error = (user)
|
2021-01-21 13:17:06 +03:00
|
|
|
? Core::Account::from_name(user)
|
|
|
|
: Core::Account::from_uid(0);
|
2020-07-31 02:35:30 +03:00
|
|
|
if (account_or_error.is_error()) {
|
2021-05-31 17:43:25 +03:00
|
|
|
warnln("Core::Account::from_name: {}", account_or_error.error());
|
2020-01-04 15:48:55 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-09 19:53:30 +03:00
|
|
|
if (pledge("stdio tty exec id", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-09 19:54:09 +03:00
|
|
|
const auto& account = account_or_error.value();
|
2020-07-31 02:35:30 +03:00
|
|
|
|
|
|
|
if (getuid() != 0 && account.has_password()) {
|
2020-07-26 03:36:32 +03:00
|
|
|
auto password = Core::get_password();
|
|
|
|
if (password.is_error()) {
|
2021-01-10 15:48:05 +03:00
|
|
|
warnln("{}", password.error());
|
2020-07-26 03:36:32 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-09-12 17:02:17 +03:00
|
|
|
if (!account.authenticate(password.value())) {
|
2021-01-10 00:14:20 +03:00
|
|
|
warnln("Incorrect or disabled password.");
|
2020-07-26 03:36:32 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 00:19:31 +03:00
|
|
|
if (pledge("stdio exec id", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-31 02:35:30 +03:00
|
|
|
if (!account.login()) {
|
|
|
|
perror("Core::Account::login");
|
2019-02-22 01:35:07 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2020-07-31 02:35:30 +03:00
|
|
|
|
2021-01-22 21:40:30 +03:00
|
|
|
if (pledge("stdio exec", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-31 02:35:30 +03:00
|
|
|
execl(account.shell().characters(), account.shell().characters(), nullptr);
|
2019-02-22 01:49:16 +03:00
|
|
|
perror("execl");
|
|
|
|
return 1;
|
2019-02-22 01:35:07 +03:00
|
|
|
}
|