2021-04-14 02:23:55 +00:00
|
|
|
/*
|
2021-04-16 14:55:05 +00:00
|
|
|
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
|
2021-05-30 20:06:28 +00:00
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2021-04-14 02:23:55 +00:00
|
|
|
*
|
2021-04-16 14:55:05 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-04-14 02:23:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/Account.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/GetPassword.h>
|
2021-12-16 18:18:37 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-04-14 02:23:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-12-16 18:18:37 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-04-14 02:23:55 +00:00
|
|
|
{
|
2021-05-30 20:06:28 +00:00
|
|
|
Vector<char const*> command;
|
2021-04-14 02:23:55 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2021-08-16 02:58:34 +00:00
|
|
|
uid_t as_user_uid = 0;
|
|
|
|
args_parser.add_option(as_user_uid, "User to execute as", nullptr, 'u', "UID");
|
2021-04-14 02:23:55 +00:00
|
|
|
args_parser.add_positional_argument(command, "Command to run at elevated privilege level", "command");
|
2021-12-16 18:18:37 +00:00
|
|
|
args_parser.parse(arguments);
|
2021-04-14 02:23:55 +00:00
|
|
|
|
2021-12-16 18:18:37 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath exec id tty"));
|
2021-04-14 02:23:55 +00:00
|
|
|
|
2021-12-16 18:18:37 +00:00
|
|
|
TRY(Core::System::seteuid(0));
|
2021-04-16 14:55:05 +00:00
|
|
|
|
2021-12-16 18:18:37 +00:00
|
|
|
auto as_user = TRY(Core::Account::from_uid(as_user_uid));
|
2021-08-16 02:58:34 +00:00
|
|
|
|
2021-05-30 20:06:28 +00:00
|
|
|
// If the current user is not a superuser, make them authenticate themselves.
|
|
|
|
if (auto uid = getuid()) {
|
2021-12-16 18:18:37 +00:00
|
|
|
auto account = TRY(Core::Account::from_uid(uid));
|
2021-05-30 20:06:28 +00:00
|
|
|
if (account.has_password()) {
|
2021-12-16 18:18:37 +00:00
|
|
|
auto password = TRY(Core::get_password());
|
|
|
|
if (!account.authenticate(password))
|
|
|
|
return Error::from_string_literal("Incorrect or disabled password."sv);
|
2021-05-30 20:06:28 +00:00
|
|
|
}
|
2021-04-14 02:23:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 18:18:37 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath exec id"));
|
2021-04-14 02:23:55 +00:00
|
|
|
|
2021-12-16 18:18:37 +00:00
|
|
|
TRY(Core::System::setgid(0));
|
|
|
|
TRY(Core::System::setuid(as_user_uid));
|
2021-04-16 14:55:05 +00:00
|
|
|
|
2021-12-16 18:18:37 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath exec"));
|
2021-04-14 02:23:55 +00:00
|
|
|
|
2021-12-16 18:18:37 +00:00
|
|
|
Vector<char const*> exec_arguments;
|
2021-05-30 20:06:28 +00:00
|
|
|
for (auto const& arg : command)
|
2021-12-16 18:18:37 +00:00
|
|
|
exec_arguments.append(arg);
|
|
|
|
exec_arguments.append(nullptr);
|
2021-04-16 14:55:05 +00:00
|
|
|
|
2021-05-30 20:06:28 +00:00
|
|
|
Vector<String> environment_strings;
|
|
|
|
if (auto* term = getenv("TERM"))
|
|
|
|
environment_strings.append(String::formatted("TERM={}", term));
|
2021-04-14 02:23:55 +00:00
|
|
|
|
2021-12-16 18:18:37 +00:00
|
|
|
Vector<char const*> exec_environment;
|
2021-05-30 20:06:28 +00:00
|
|
|
for (auto& item : environment_strings)
|
2021-12-16 18:18:37 +00:00
|
|
|
exec_environment.append(item.characters());
|
|
|
|
exec_environment.append(nullptr);
|
2021-04-14 02:23:55 +00:00
|
|
|
|
2021-12-16 18:18:37 +00:00
|
|
|
if (execvpe(command.at(0), const_cast<char**>(exec_arguments.data()), const_cast<char**>(exec_environment.data())) < 0) {
|
2021-04-14 02:23:55 +00:00
|
|
|
perror("execvpe");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|