From 6499699d6deae040d8487dc65feb56ddda868ed0 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Sat, 15 Jan 2022 18:30:36 +0300 Subject: [PATCH] useradd: Port to LibMain --- Userland/Utilities/CMakeLists.txt | 1 + Userland/Utilities/useradd.cpp | 39 ++++++++++++++++++------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 53cf91a5df1..12cee6e4dfe 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -197,6 +197,7 @@ target_link_libraries(uniq LibMain) target_link_libraries(unzip LibArchive LibCompress LibMain) target_link_libraries(update-cpp-test-results LibCpp LibCore LibMain) target_link_libraries(uptime LibMain) +target_link_libraries(useradd LibMain) target_link_libraries(userdel LibMain) target_link_libraries(usermod LibMain) target_link_libraries(utmpupdate LibMain) diff --git a/Userland/Utilities/useradd.cpp b/Userland/Utilities/useradd.cpp index 3b6b50db245..3d31d9376eb 100644 --- a/Userland/Utilities/useradd.cpp +++ b/Userland/Utilities/useradd.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2019-2020, Jesse Buhagiar * Copyright (c) 2021, Brandon Pruitt + * Copyright (c) 2022, Umut İnan Erdoğan * * SPDX-License-Identifier: BSD-2-Clause */ @@ -9,6 +10,8 @@ #include #include #include +#include +#include #include #include #include @@ -24,12 +27,9 @@ constexpr uid_t BASE_UID = 1000; constexpr gid_t USERS_GID = 100; constexpr const char* DEFAULT_SHELL = "/bin/sh"; -int main(int argc, char** argv) +ErrorOr serenity_main(Main::Arguments arguments) { - if (pledge("stdio wpath rpath cpath chown", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(Core::System::pledge("stdio wpath rpath cpath chown")); const char* home_path = nullptr; int uid = 0; @@ -50,7 +50,7 @@ int main(int argc, char** argv) args_parser.add_option(gecos, "GECOS name of the new user", "gecos", 'n', "general-info"); args_parser.add_positional_argument(username, "Login user identity (username)", "login"); - args_parser.parse(argc, argv); + args_parser.parse(arguments); // Let's run a quick sanity check on username if (strpbrk(username, "\\/!@#$%^&*()~+=`:\n")) { @@ -64,7 +64,8 @@ int main(int argc, char** argv) return 1; } - if (getpwnam(username)) { + auto passwd = TRY(Core::System::getpwnam(username)); + if (passwd.has_value()) { warnln("user {} already exists!", username); return 1; } @@ -76,13 +77,16 @@ int main(int argc, char** argv) // First, let's sort out the uid for the user if (uid > 0) { - if (getpwuid(static_cast(uid))) { + auto pwd = TRY(Core::System::getpwuid(static_cast(uid))); + if (pwd.has_value()) { warnln("uid {} already exists!", uid); return 4; } - } else { - for (uid = BASE_UID; getpwuid(static_cast(uid)); uid++) { + for (uid = BASE_UID;; uid++) { + auto pwd = TRY(Core::System::getpwuid(static_cast(uid))); + if (!pwd.has_value()) + break; } } @@ -110,19 +114,22 @@ int main(int argc, char** argv) home = home_path; if (create_home_dir) { - if (mkdir(home.characters(), 0700) < 0) { - auto saved_errno = errno; - warnln("Failed to create directory {}: {}", home, strerror(saved_errno)); + auto mkdir_error = Core::System::mkdir(home, 0700); + if (mkdir_error.is_error()) { + int code = mkdir_error.release_error().code(); + warnln("Failed to create directory {}: {}", home, strerror(code)); return 12; } - if (chown(home.characters(), static_cast(uid), static_cast(gid)) < 0) { - warnln("Failed to change owner of {} to {}:{}: {}", home, uid, gid, strerror(errno)); + auto chown_error = Core::System::chown(home, static_cast(uid), static_cast(gid)); + if (chown_error.is_error()) { + int code = chown_error.release_error().code(); + warnln("Failed to change owner of {} to {}:{}: {}", home, uid, gid, strerror(code)); if (rmdir(home.characters()) < 0) { warnln("Failed to remove directory {}: {}", home, strerror(errno)); - return 12; } + return 12; } }