summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-12-16 21:41:57 +0100
committerAndreas Kling <kling@serenityos.org>2021-12-16 22:48:17 +0100
commitb38f8902d2e277e8cc8028add0eec946170854cd (patch)
tree6645daa07bf0f641aa2dc6c3f315bef128b1ff63 /Userland/Libraries
parentead9c36c92c4d0dcb973a4f0e8e35ee5ba360bbd (diff)
downloadserenity-b38f8902d2e277e8cc8028add0eec946170854cd.zip
LibCore+passwd+usermod: Make Core::Account::sync() return ErrorOr<void>
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibCore/Account.cpp67
-rw-r--r--Userland/Libraries/LibCore/Account.h6
2 files changed, 21 insertions, 52 deletions
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 <AK/Random.h>
#include <AK/ScopeGuard.h>
#include <LibCore/Account.h>
+#include <LibCore/System.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
@@ -218,7 +219,7 @@ Account::Account(const passwd& pwd, const spwd& spwd, Vector<gid_t> extra_gids)
{
}
-String Account::generate_passwd_file() const
+ErrorOr<String> 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<String> 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<void> 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<size_t>(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<size_t>(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<gid_t>& extra_gids() const { return m_extra_gids; }
- bool sync();
+ ErrorOr<void> sync();
private:
static ErrorOr<Account> from_passwd(passwd const&, spwd const&);
Account(const passwd& pwd, const spwd& spwd, Vector<gid_t> extra_gids);
- String generate_passwd_file() const;
+ ErrorOr<String> generate_passwd_file() const;
#ifndef AK_OS_BSD_GENERIC
- String generate_shadow_file() const;
+ ErrorOr<String> generate_shadow_file() const;
#endif
String m_username;