2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-04-02 09:32:47 -03:00
|
|
|
* Copyright (c) 2022, Undefine <undefine@undefine.pl>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-09-11 13:53:25 -03:00
|
|
|
#include <AK/ScopeGuard.h>
|
2020-07-30 20:35:30 -03:00
|
|
|
#include <LibCore/Account.h>
|
2020-09-18 04:49:51 -03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-07-25 21:36:32 -03:00
|
|
|
#include <LibCore/GetPassword.h>
|
2021-12-16 15:34:21 -03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
|
#include <LibMain/Main.h>
|
2019-06-07 06:49:31 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
2019-02-21 19:35:07 -03:00
|
|
|
|
2021-12-16 15:34:21 -03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-02-21 19:35:07 -03:00
|
|
|
{
|
2021-12-16 15:34:21 -03:00
|
|
|
TRY(Core::System::pledge("stdio rpath tty exec id"));
|
2021-01-09 13:53:30 -03:00
|
|
|
|
2021-12-16 15:34:21 -03:00
|
|
|
if (!TRY(Core::System::isatty(STDIN_FILENO)))
|
|
|
|
|
return Error::from_string_literal("Standard input is not a terminal");
|
2020-11-08 10:52:33 -03:00
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
char const* user = nullptr;
|
2020-08-05 15:26:32 -03:00
|
|
|
|
|
|
|
|
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);
|
2021-12-16 15:34:21 -03:00
|
|
|
args_parser.parse(arguments);
|
2020-08-05 15:26:32 -03:00
|
|
|
|
2021-12-16 15:34:21 -03:00
|
|
|
if (geteuid() != 0)
|
|
|
|
|
return Error::from_string_literal("Not running as root :(");
|
2020-06-16 16:16:09 -03:00
|
|
|
|
2021-12-16 15:34:21 -03:00
|
|
|
auto account = TRY(user ? Core::Account::from_name(user) : Core::Account::from_uid(0));
|
2020-01-04 09:48:55 -03:00
|
|
|
|
2021-12-16 15:34:21 -03:00
|
|
|
TRY(Core::System::pledge("stdio tty exec id"));
|
2020-07-30 20:35:30 -03:00
|
|
|
|
|
|
|
|
if (getuid() != 0 && account.has_password()) {
|
2021-12-16 15:34:21 -03:00
|
|
|
auto password = TRY(Core::get_password());
|
|
|
|
|
if (!account.authenticate(password))
|
|
|
|
|
return Error::from_string_literal("Incorrect or disabled password.");
|
2020-07-25 21:36:32 -03:00
|
|
|
}
|
|
|
|
|
|
2021-12-16 15:34:21 -03:00
|
|
|
TRY(Core::System::pledge("stdio exec id"));
|
2021-01-09 18:19:31 -03:00
|
|
|
|
2020-07-30 20:35:30 -03:00
|
|
|
if (!account.login()) {
|
|
|
|
|
perror("Core::Account::login");
|
2019-02-21 19:35:07 -03:00
|
|
|
return 1;
|
|
|
|
|
}
|
2020-07-30 20:35:30 -03:00
|
|
|
|
2021-12-16 15:34:21 -03:00
|
|
|
TRY(Core::System::pledge("stdio exec"));
|
2021-01-22 15:40:30 -03:00
|
|
|
|
2022-04-02 09:32:47 -03:00
|
|
|
TRY(Core::System::setenv("HOME"sv, account.home_directory(), true));
|
|
|
|
|
|
2020-07-30 20:35:30 -03:00
|
|
|
execl(account.shell().characters(), account.shell().characters(), nullptr);
|
2019-02-21 19:49:16 -03:00
|
|
|
perror("execl");
|
|
|
|
|
return 1;
|
2019-02-21 19:35:07 -03:00
|
|
|
}
|