LibCore+passwd+usermod: Make Core::Account::sync() return ErrorOr<void>
This commit is contained in:
parent
ead9c36c92
commit
b38f8902d2
Notes:
sideshowbarker
2024-07-17 22:41:20 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/b38f8902d2e
4 changed files with 25 additions and 61 deletions
|
@ -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.
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -92,11 +92,8 @@ ErrorOr<int> 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;
|
||||
}
|
||||
|
|
|
@ -134,10 +134,8 @@ ErrorOr<int> 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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue