diff --git a/Userland/Libraries/LibCore/Account.cpp b/Userland/Libraries/LibCore/Account.cpp index 62b6f27944..c7636c26aa 100644 --- a/Userland/Libraries/LibCore/Account.cpp +++ b/Userland/Libraries/LibCore/Account.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -218,7 +219,7 @@ Account::Account(const passwd& pwd, const spwd& spwd, Vector extra_gids) { } -String Account::generate_passwd_file() const +ErrorOr Account::generate_passwd_file() const { StringBuilder builder; @@ -244,16 +245,14 @@ String Account::generate_passwd_file() const } endpwent(); - if (errno) { - dbgln("errno was non-zero after generating new passwd file."); - return {}; - } + if (errno) + return Error::from_errno(errno); return builder.to_string(); } #ifndef AK_OS_BSD_GENERIC -String Account::generate_shadow_file() const +ErrorOr Account::generate_shadow_file() const { StringBuilder builder; @@ -287,22 +286,18 @@ String Account::generate_shadow_file() const } endspent(); - if (errno) { - dbgln("errno was non-zero after generating new passwd file."); - return {}; - } + if (errno) + return Error::from_errno(errno); return builder.to_string(); } #endif -bool Account::sync() +ErrorOr Account::sync() { - auto new_passwd_file_content = generate_passwd_file(); - VERIFY(!new_passwd_file_content.is_null()); + auto new_passwd_file_content = TRY(generate_passwd_file()); #ifndef AK_OS_BSD_GENERIC - auto new_shadow_file_content = generate_shadow_file(); - VERIFY(!new_shadow_file_content.is_null()); + auto new_shadow_file_content = TRY(generate_shadow_file()); #endif char new_passwd_name[] = "/etc/passwd.XXXXXX"; @@ -311,56 +306,30 @@ bool Account::sync() #endif { - auto new_passwd_fd = mkstemp(new_passwd_name); - if (new_passwd_fd < 0) { - perror("mkstemp"); - VERIFY_NOT_REACHED(); - } + auto new_passwd_fd = TRY(Core::System::mkstemp(new_passwd_name)); ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); }; #ifndef AK_OS_BSD_GENERIC - auto new_shadow_fd = mkstemp(new_shadow_name); - if (new_shadow_fd < 0) { - perror("mkstemp"); - VERIFY_NOT_REACHED(); - } + auto new_shadow_fd = TRY(Core::System::mkstemp(new_shadow_name)); ScopeGuard new_shadow_fd_guard = [new_shadow_fd] { close(new_shadow_fd); }; #endif - if (fchmod(new_passwd_fd, 0644) < 0) { - perror("fchmod"); - VERIFY_NOT_REACHED(); - } + TRY(Core::System::fchmod(new_passwd_fd, 0644)); - auto nwritten = write(new_passwd_fd, new_passwd_file_content.characters(), new_passwd_file_content.length()); - if (nwritten < 0) { - perror("write"); - VERIFY_NOT_REACHED(); - } + auto nwritten = TRY(Core::System::write(new_passwd_fd, new_passwd_file_content.bytes())); VERIFY(static_cast(nwritten) == new_passwd_file_content.length()); #ifndef AK_OS_BSD_GENERIC - nwritten = write(new_shadow_fd, new_shadow_file_content.characters(), new_shadow_file_content.length()); - if (nwritten < 0) { - perror("write"); - VERIFY_NOT_REACHED(); - } + nwritten = TRY(Core::System::write(new_shadow_fd, new_shadow_file_content.bytes())); VERIFY(static_cast(nwritten) == new_shadow_file_content.length()); #endif } - if (rename(new_passwd_name, "/etc/passwd") < 0) { - perror("Failed to install new /etc/passwd"); - return false; - } - + TRY(Core::System::rename(new_passwd_name, "/etc/passwd")); #ifndef AK_OS_BSD_GENERIC - if (rename(new_shadow_name, "/etc/shadow") < 0) { - perror("Failed to install new /etc/shadow"); - return false; - } + TRY(Core::System::rename(new_shadow_name, "/etc/shadow")); #endif - return true; + return {}; // FIXME: Sync extra groups. } diff --git a/Userland/Libraries/LibCore/Account.h b/Userland/Libraries/LibCore/Account.h index d60bf45088..cbf3c41f69 100644 --- a/Userland/Libraries/LibCore/Account.h +++ b/Userland/Libraries/LibCore/Account.h @@ -64,16 +64,16 @@ public: const String& shell() const { return m_shell; } const Vector& extra_gids() const { return m_extra_gids; } - bool sync(); + ErrorOr sync(); private: static ErrorOr from_passwd(passwd const&, spwd const&); Account(const passwd& pwd, const spwd& spwd, Vector extra_gids); - String generate_passwd_file() const; + ErrorOr generate_passwd_file() const; #ifndef AK_OS_BSD_GENERIC - String generate_shadow_file() const; + ErrorOr generate_shadow_file() const; #endif String m_username; diff --git a/Userland/Utilities/passwd.cpp b/Userland/Utilities/passwd.cpp index 0ee194af8d..f186982069 100644 --- a/Userland/Utilities/passwd.cpp +++ b/Userland/Utilities/passwd.cpp @@ -92,11 +92,8 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(Core::System::pledge("stdio wpath rpath cpath fattr")); - if (!target_account.sync()) { - perror("Core::Account::Sync"); - } else { - outln("Password for user {} successfully updated.", target_account.username()); - } + TRY(target_account.sync()); + outln("Password for user {} successfully updated.", target_account.username()); return 0; } diff --git a/Userland/Utilities/usermod.cpp b/Userland/Utilities/usermod.cpp index b9d91e4ece..eb6122fabd 100644 --- a/Userland/Utilities/usermod.cpp +++ b/Userland/Utilities/usermod.cpp @@ -134,10 +134,8 @@ ErrorOr serenity_main(Main::Arguments arguments) } TRY(Core::System::pledge("stdio wpath rpath cpath fattr")); - if (!target_account.sync()) { - perror("Core::Account::Sync"); - return 1; - } + + TRY(target_account.sync()); return 0; }