diff --git a/Userland/Libraries/LibC/shadow.h b/Userland/Libraries/LibC/shadow.h index 3c77ee5ee7..ac58c0af40 100644 --- a/Userland/Libraries/LibC/shadow.h +++ b/Userland/Libraries/LibC/shadow.h @@ -6,7 +6,9 @@ #pragma once -#include +#ifndef AK_OS_MACOS +# include +#endif #include #include @@ -24,6 +26,7 @@ struct spwd { unsigned long int sp_flag; }; +#ifndef AK_OS_MACOS struct spwd* getspent(); void setspent(); void endspent(); @@ -35,5 +38,6 @@ int getspnam_r(const char* name, struct spwd* spbuf, char* buf, size_t buflen, s int fgetspent_r(FILE* fp, struct spwd* spbuf, char* buf, size_t buflen, struct spwd** spbufp); int sgetspent_r(const char* s, struct spwd* spbuf, char* buf, size_t buflen, struct spwd** spbufp); +#endif __END_DECLS diff --git a/Userland/Libraries/LibCore/Account.cpp b/Userland/Libraries/LibCore/Account.cpp index c86fb1d6f0..92d03fb5a0 100644 --- a/Userland/Libraries/LibCore/Account.cpp +++ b/Userland/Libraries/LibCore/Account.cpp @@ -9,11 +9,17 @@ #include #include #include -#include +#ifndef AK_OS_MACOS +# include +#endif #include #include #include -#include +#ifndef AK_OS_MACOS +# include +#else +# include +#endif #include #include #include @@ -52,7 +58,9 @@ Result Account::from_passwd(const passwd& pwd, const spwd& spwd { Account account(pwd, spwd, get_gids(pwd.pw_name)); endpwent(); +#ifndef AK_OS_MACOS endspent(); +#endif return account; } @@ -66,6 +74,7 @@ Result Account::from_name(const char* username) return String(strerror(errno)); } +#ifndef AK_OS_MACOS auto* spwd = getspnam(username); if (!spwd) { if (errno == 0) @@ -73,6 +82,12 @@ Result Account::from_name(const char* username) return String(strerror(errno)); } +#else + spwd spwd_dummy = {}; + spwd_dummy.sp_namp = const_cast(username); + spwd_dummy.sp_pwdp = const_cast(""); + auto* spwd = &spwd_dummy; +#endif return from_passwd(*pwd, *spwd); } @@ -86,6 +101,7 @@ Result Account::from_uid(uid_t uid) return String(strerror(errno)); } +#ifndef AK_OS_MACOS auto* spwd = getspnam(pwd->pw_name); if (!spwd) { if (errno == 0) @@ -93,6 +109,12 @@ Result Account::from_uid(uid_t uid) return String(strerror(errno)); } +#else + spwd spwd_dummy = {}; + spwd_dummy.sp_namp = pwd->pw_name; + spwd_dummy.sp_pwdp = const_cast(""); + auto* spwd = &spwd_dummy; +#endif return from_passwd(*pwd, *spwd); } @@ -193,6 +215,7 @@ String Account::generate_passwd_file() const return builder.to_string(); } +#ifndef AK_OS_MACOS String Account::generate_shadow_file() const { StringBuilder builder; @@ -228,18 +251,21 @@ String Account::generate_shadow_file() const return builder.to_string(); } +#endif bool Account::sync() { auto new_passwd_file_content = generate_passwd_file(); + VERIFY(!new_passwd_file_content.is_null()); +#ifndef AK_OS_MACOS auto new_shadow_file_content = generate_shadow_file(); - - if (new_passwd_file_content.is_null() || new_shadow_file_content.is_null()) { - VERIFY_NOT_REACHED(); - } + VERIFY(!new_shadow_file_content.is_null()); +#endif char new_passwd_name[] = "/etc/passwd.XXXXXX"; +#ifndef AK_OS_MACOS char new_shadow_name[] = "/etc/shadow.XXXXXX"; +#endif { auto new_passwd_fd = mkstemp(new_passwd_name); @@ -248,12 +274,14 @@ bool Account::sync() VERIFY_NOT_REACHED(); } ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); }; +#ifndef AK_OS_MACOS auto new_shadow_fd = mkstemp(new_shadow_name); if (new_shadow_fd < 0) { perror("mkstemp"); VERIFY_NOT_REACHED(); } ScopeGuard new_shadow_fd_guard = [new_shadow_fd] { close(new_shadow_fd); }; +#endif if (fchmod(new_passwd_fd, 0644) < 0) { perror("fchmod"); @@ -267,12 +295,14 @@ bool Account::sync() } VERIFY(static_cast(nwritten) == new_passwd_file_content.length()); +#ifndef AK_OS_MACOS nwritten = write(new_shadow_fd, new_shadow_file_content.characters(), new_shadow_file_content.length()); if (nwritten < 0) { perror("write"); VERIFY_NOT_REACHED(); } VERIFY(static_cast(nwritten) == new_shadow_file_content.length()); +#endif } if (rename(new_passwd_name, "/etc/passwd") < 0) { @@ -280,10 +310,12 @@ bool Account::sync() return false; } +#ifndef AK_OS_MACOS if (rename(new_shadow_name, "/etc/shadow") < 0) { perror("Failed to install new /etc/shadow"); return false; } +#endif return true; // FIXME: Sync extra groups. diff --git a/Userland/Libraries/LibCore/Account.h b/Userland/Libraries/LibCore/Account.h index c97dae424b..0b945d8095 100644 --- a/Userland/Libraries/LibCore/Account.h +++ b/Userland/Libraries/LibCore/Account.h @@ -11,7 +11,11 @@ #include #include #include -#include +#ifndef AK_OS_MACOS +# include +#else +# include +#endif #include namespace Core { @@ -52,7 +56,9 @@ private: Account(const passwd& pwd, const spwd& spwd, Vector extra_gids); String generate_passwd_file() const; +#ifndef AK_OS_MACOS String generate_shadow_file() const; +#endif String m_username;