/* * Copyright (c) 2021, Jesse Buhagiar * Copyright (c) 2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include int main(int argc, char** argv) { Vector command; Core::ArgsParser args_parser; uid_t as_user_uid = 0; args_parser.add_option(as_user_uid, "User to execute as", nullptr, 'u', "UID"); args_parser.add_positional_argument(command, "Command to run at elevated privilege level", "command"); args_parser.parse(argc, argv); if (pledge("stdio rpath exec id tty", nullptr) < 0) { perror("pledge"); return 1; } if (seteuid(0) < 0) { perror("seteuid"); return 1; } // Fail gracefully if as_user_uid is invalid auto as_user_or_error = Core::Account::from_uid(as_user_uid); if (as_user_or_error.is_error()) { warnln("{}", as_user_or_error.error()); return 1; } // If the current user is not a superuser, make them authenticate themselves. if (auto uid = getuid()) { auto account_or_error = Core::Account::from_uid(uid); if (account_or_error.is_error()) { warnln("{}", account_or_error.error()); return 1; } auto const& account = account_or_error.value(); if (account.has_password()) { auto password_or_error = Core::get_password(); if (password_or_error.is_error()) { warnln("{}", password_or_error.error()); return 1; } auto const& password = password_or_error.value(); if (!account.authenticate(password.characters())) { warnln("Incorrect or disabled password."); return 1; } } } if (pledge("stdio rpath exec id", nullptr) < 0) { perror("pledge"); return 1; } if (setgid(0) < 0) { perror("setgid"); return 1; } if (setuid(as_user_uid) < 0) { perror("setuid"); return 1; } if (pledge("stdio rpath exec", nullptr) < 0) { perror("pledge"); return 1; } Vector arguments; for (auto const& arg : command) arguments.append(arg); arguments.append(nullptr); Vector environment_strings; if (auto* term = getenv("TERM")) environment_strings.append(String::formatted("TERM={}", term)); Vector environment; for (auto& item : environment_strings) environment.append(item.characters()); environment.append(nullptr); if (execvpe(command.at(0), const_cast(arguments.data()), const_cast(environment.data())) < 0) { perror("execvpe"); exit(1); } return 0; }