2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-04-02 12:32:47 +00:00
|
|
|
* Copyright (c) 2022, Undefine <undefine@undefine.pl>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-07-30 23:35:30 +00:00
|
|
|
#include <LibCore/Account.h>
|
2020-09-18 07:49:51 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2024-02-07 16:06:58 +00:00
|
|
|
#include <LibCore/Environment.h>
|
2020-07-26 00:36:32 +00:00
|
|
|
#include <LibCore/GetPassword.h>
|
2021-12-16 18:34:21 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <unistd.h>
|
2019-02-21 22:35:07 +00:00
|
|
|
|
2021-12-16 18:34:21 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-02-21 22:35:07 +00:00
|
|
|
{
|
2021-12-16 18:34:21 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath tty exec id"));
|
2021-01-09 16:53:30 +00:00
|
|
|
|
2022-06-10 19:09:38 +00:00
|
|
|
StringView first_positional;
|
|
|
|
StringView second_positional;
|
2023-10-10 11:30:58 +00:00
|
|
|
StringView command;
|
2022-06-10 19:09:38 +00:00
|
|
|
bool simulate_login = false;
|
2020-08-05 18:26:32 +00:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2022-06-10 19:09:38 +00:00
|
|
|
args_parser.add_positional_argument(first_positional, "See --login", "-", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.add_positional_argument(second_positional, "User to switch to (defaults to user with UID 0)", "user", Core::ArgsParser::Required::No);
|
2023-02-06 17:51:19 +00:00
|
|
|
args_parser.add_option(command, "Command to execute", "command", 'c', "command");
|
2022-06-10 19:09:38 +00:00
|
|
|
args_parser.add_option(simulate_login, "Simulate login", "login", 'l');
|
2021-12-16 18:34:21 +00:00
|
|
|
args_parser.parse(arguments);
|
2020-08-05 18:26:32 +00:00
|
|
|
|
2022-06-10 19:09:38 +00:00
|
|
|
StringView user = first_positional;
|
|
|
|
|
|
|
|
if (first_positional == '-') {
|
|
|
|
simulate_login = true;
|
|
|
|
user = second_positional;
|
|
|
|
}
|
|
|
|
|
2021-12-16 18:34:21 +00:00
|
|
|
if (geteuid() != 0)
|
|
|
|
return Error::from_string_literal("Not running as root :(");
|
2020-06-16 19:16:09 +00:00
|
|
|
|
2022-06-10 18:06:06 +00:00
|
|
|
auto account = TRY(user.is_empty() ? Core::Account::from_uid(0) : Core::Account::from_name(user));
|
2020-01-04 12:48:55 +00:00
|
|
|
|
2022-06-10 19:09:38 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath tty exec id"));
|
2020-07-30 23:35:30 +00:00
|
|
|
|
|
|
|
if (getuid() != 0 && account.has_password()) {
|
2023-02-06 17:55:43 +00:00
|
|
|
if (!TRY(Core::System::isatty(STDIN_FILENO)))
|
|
|
|
return Error::from_string_literal("Standard input is not a terminal");
|
|
|
|
|
2021-12-16 18:34:21 +00:00
|
|
|
auto password = TRY(Core::get_password());
|
|
|
|
if (!account.authenticate(password))
|
|
|
|
return Error::from_string_literal("Incorrect or disabled password.");
|
2020-07-26 00:36:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 19:09:38 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath exec id"));
|
2021-01-09 21:19:31 +00:00
|
|
|
|
2022-07-09 12:53:04 +00:00
|
|
|
TRY(account.login());
|
2020-07-30 23:35:30 +00:00
|
|
|
|
2022-06-10 19:09:38 +00:00
|
|
|
if (simulate_login)
|
|
|
|
TRY(Core::System::chdir(account.home_directory()));
|
|
|
|
|
2021-12-16 18:34:21 +00:00
|
|
|
TRY(Core::System::pledge("stdio exec"));
|
2021-01-22 18:40:30 +00:00
|
|
|
|
2024-02-07 16:06:58 +00:00
|
|
|
TRY(Core::Environment::set("HOME"sv, account.home_directory(), Core::Environment::Overwrite::Yes));
|
2022-04-02 12:32:47 +00:00
|
|
|
|
2023-02-06 17:51:19 +00:00
|
|
|
if (command.is_null()) {
|
|
|
|
TRY(Core::System::exec(account.shell(), Array<StringView, 1> { account.shell().view() }, Core::System::SearchInPath::No));
|
|
|
|
} else {
|
2023-10-10 11:30:58 +00:00
|
|
|
TRY(Core::System::exec(account.shell(), Array<StringView, 3> { account.shell().view(), "-c"sv, command }, Core::System::SearchInPath::No));
|
2023-02-06 17:51:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 22:49:16 +00:00
|
|
|
return 1;
|
2019-02-21 22:35:07 +00:00
|
|
|
}
|