LibCore: Fix building the library on macOS

This commit is contained in:
Gunnar Beutner 2021-05-01 13:08:25 +02:00 committed by Andreas Kling
parent 1635942951
commit f18895c0d6
Notes: sideshowbarker 2024-07-18 18:49:23 +09:00
3 changed files with 50 additions and 8 deletions

View file

@ -6,7 +6,9 @@
#pragma once
#include <bits/FILE.h>
#ifndef AK_OS_MACOS
# include <bits/FILE.h>
#endif
#include <sys/cdefs.h>
#include <sys/types.h>
@ -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

View file

@ -9,11 +9,17 @@
#include <AK/ScopeGuard.h>
#include <LibCore/Account.h>
#include <LibCore/File.h>
#include <crypt.h>
#ifndef AK_OS_MACOS
# include <crypt.h>
#endif
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <shadow.h>
#ifndef AK_OS_MACOS
# include <shadow.h>
#else
# include <LibC/shadow.h>
#endif
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
@ -52,7 +58,9 @@ Result<Account, String> 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, String> 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, String> Account::from_name(const char* username)
return String(strerror(errno));
}
#else
spwd spwd_dummy = {};
spwd_dummy.sp_namp = const_cast<char*>(username);
spwd_dummy.sp_pwdp = const_cast<char*>("");
auto* spwd = &spwd_dummy;
#endif
return from_passwd(*pwd, *spwd);
}
@ -86,6 +101,7 @@ Result<Account, String> 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, String> 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<char*>("");
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<size_t>(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<size_t>(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.

View file

@ -11,7 +11,11 @@
#include <AK/Types.h>
#include <AK/Vector.h>
#include <pwd.h>
#include <shadow.h>
#ifndef AK_OS_MACOS
# include <shadow.h>
#else
# include <LibC/shadow.h>
#endif
#include <sys/types.h>
namespace Core {
@ -52,7 +56,9 @@ private:
Account(const passwd& pwd, const spwd& spwd, Vector<gid_t> extra_gids);
String generate_passwd_file() const;
#ifndef AK_OS_MACOS
String generate_shadow_file() const;
#endif
String m_username;