Serenity Operating System
at master 50 lines 1.7 kB view raw
1/* 2 * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com> 3 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include <LibCore/Account.h> 9#include <LibCore/ArgsParser.h> 10#include <LibCore/GetPassword.h> 11#include <LibCore/System.h> 12#include <LibMain/Main.h> 13#include <unistd.h> 14 15ErrorOr<int> serenity_main(Main::Arguments arguments) 16{ 17 Vector<StringView> command; 18 Core::ArgsParser args_parser; 19 uid_t as_user_uid = 0; 20 bool preserve_env = false; 21 args_parser.set_stop_on_first_non_option(true); 22 args_parser.add_option(as_user_uid, "User to execute as", nullptr, 'u', "UID"); 23 args_parser.add_option(preserve_env, "Preserve user environment when running command", "preserve-env", 'E'); 24 args_parser.add_positional_argument(command, "Command to run at elevated privilege level", "command"); 25 args_parser.parse(arguments); 26 27 TRY(Core::System::pledge("stdio rpath exec id tty")); 28 29 TRY(Core::System::seteuid(0)); 30 31 auto as_user = TRY(Core::Account::from_uid(as_user_uid)); 32 33 // If the current user is not a superuser, make them authenticate themselves. 34 if (auto uid = getuid()) { 35 auto account = TRY(Core::Account::from_uid(uid)); 36 if (account.has_password()) { 37 auto password = TRY(Core::get_password()); 38 if (!account.authenticate(password)) 39 return Error::from_string_literal("Incorrect or disabled password."); 40 } 41 } 42 43 TRY(Core::System::pledge("stdio rpath exec id")); 44 45 TRY(as_user.login()); 46 47 TRY(Core::System::pledge("stdio rpath exec")); 48 TRY(Core::System::exec_command(command, preserve_env)); 49 return 0; 50}