mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 13:38:11 +00:00
su: Port to LibMain :^)
This commit is contained in:
parent
411c696057
commit
e5a5091b6f
2 changed files with 17 additions and 48 deletions
|
@ -128,7 +128,7 @@ target_link_libraries(run-tests LibRegex)
|
||||||
target_link_libraries(shot LibGUI)
|
target_link_libraries(shot LibGUI)
|
||||||
target_link_libraries(sql LibLine LibSQL LibIPC)
|
target_link_libraries(sql LibLine LibSQL LibIPC)
|
||||||
target_link_libraries(stat LibMain)
|
target_link_libraries(stat LibMain)
|
||||||
target_link_libraries(su LibCrypt)
|
target_link_libraries(su LibCrypt LibMain)
|
||||||
target_link_libraries(tar LibArchive LibCompress)
|
target_link_libraries(tar LibArchive LibCompress)
|
||||||
target_link_libraries(telws LibProtocol LibLine)
|
target_link_libraries(telws LibProtocol LibLine)
|
||||||
target_link_libraries(test-crypto LibCrypto LibTLS LibLine)
|
target_link_libraries(test-crypto LibCrypto LibTLS LibLine)
|
||||||
|
|
|
@ -8,76 +8,45 @@
|
||||||
#include <LibCore/Account.h>
|
#include <LibCore/Account.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/GetPassword.h>
|
#include <LibCore/GetPassword.h>
|
||||||
|
#include <LibCore/System.h>
|
||||||
|
#include <LibMain/Main.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
extern "C" int main(int, char**);
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
|
||||||
{
|
{
|
||||||
if (pledge("stdio rpath tty exec id", nullptr) < 0) {
|
TRY(Core::System::pledge("stdio rpath tty exec id"));
|
||||||
perror("pledge");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isatty(STDIN_FILENO)) {
|
if (!TRY(Core::System::isatty(STDIN_FILENO)))
|
||||||
warnln("{}: standard in is not a terminal", argv[0]);
|
return Error::from_string_literal("Standard input is not a terminal");
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* user = nullptr;
|
const char* user = nullptr;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
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);
|
args_parser.add_positional_argument(user, "User to switch to (defaults to user with UID 0)", "user", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
if (geteuid() != 0) {
|
if (geteuid() != 0)
|
||||||
warnln("Not running as root :(");
|
return Error::from_string_literal("Not running as root :(");
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto account_or_error = (user)
|
auto account = TRY(user ? Core::Account::from_name(user) : Core::Account::from_uid(0));
|
||||||
? Core::Account::from_name(user)
|
|
||||||
: Core::Account::from_uid(0);
|
|
||||||
if (account_or_error.is_error()) {
|
|
||||||
warnln("Core::Account::from_name: {}", account_or_error.error());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pledge("stdio tty exec id", nullptr) < 0) {
|
TRY(Core::System::pledge("stdio tty exec id"));
|
||||||
perror("pledge");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto& account = account_or_error.value();
|
|
||||||
|
|
||||||
if (getuid() != 0 && account.has_password()) {
|
if (getuid() != 0 && account.has_password()) {
|
||||||
auto password = Core::get_password();
|
auto password = TRY(Core::get_password());
|
||||||
if (password.is_error()) {
|
if (!account.authenticate(password))
|
||||||
warnln("{}", password.error());
|
return Error::from_string_literal("Incorrect or disabled password.");
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!account.authenticate(password.value())) {
|
|
||||||
warnln("Incorrect or disabled password.");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pledge("stdio exec id", nullptr) < 0) {
|
TRY(Core::System::pledge("stdio exec id"));
|
||||||
perror("pledge");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!account.login()) {
|
if (!account.login()) {
|
||||||
perror("Core::Account::login");
|
perror("Core::Account::login");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pledge("stdio exec", nullptr) < 0) {
|
TRY(Core::System::pledge("stdio exec"));
|
||||||
perror("pledge");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
execl(account.shell().characters(), account.shell().characters(), nullptr);
|
execl(account.shell().characters(), account.shell().characters(), nullptr);
|
||||||
perror("execl");
|
perror("execl");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue