mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 18:57:35 +00:00
LibCore: Fix building the library on macOS
This commit is contained in:
parent
1635942951
commit
f18895c0d6
3 changed files with 50 additions and 8 deletions
|
@ -6,7 +6,9 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <bits/FILE.h>
|
#ifndef AK_OS_MACOS
|
||||||
|
# include <bits/FILE.h>
|
||||||
|
#endif
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
@ -24,6 +26,7 @@ struct spwd {
|
||||||
unsigned long int sp_flag;
|
unsigned long int sp_flag;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#ifndef AK_OS_MACOS
|
||||||
struct spwd* getspent();
|
struct spwd* getspent();
|
||||||
void setspent();
|
void setspent();
|
||||||
void endspent();
|
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 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);
|
int sgetspent_r(const char* s, struct spwd* spbuf, char* buf, size_t buflen, struct spwd** spbufp);
|
||||||
|
#endif
|
||||||
|
|
||||||
__END_DECLS
|
__END_DECLS
|
||||||
|
|
|
@ -9,11 +9,17 @@
|
||||||
#include <AK/ScopeGuard.h>
|
#include <AK/ScopeGuard.h>
|
||||||
#include <LibCore/Account.h>
|
#include <LibCore/Account.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <crypt.h>
|
#ifndef AK_OS_MACOS
|
||||||
|
# include <crypt.h>
|
||||||
|
#endif
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <grp.h>
|
#include <grp.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <shadow.h>
|
#ifndef AK_OS_MACOS
|
||||||
|
# include <shadow.h>
|
||||||
|
#else
|
||||||
|
# include <LibC/shadow.h>
|
||||||
|
#endif
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.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));
|
Account account(pwd, spwd, get_gids(pwd.pw_name));
|
||||||
endpwent();
|
endpwent();
|
||||||
|
#ifndef AK_OS_MACOS
|
||||||
endspent();
|
endspent();
|
||||||
|
#endif
|
||||||
return account;
|
return account;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,6 +74,7 @@ Result<Account, String> Account::from_name(const char* username)
|
||||||
|
|
||||||
return String(strerror(errno));
|
return String(strerror(errno));
|
||||||
}
|
}
|
||||||
|
#ifndef AK_OS_MACOS
|
||||||
auto* spwd = getspnam(username);
|
auto* spwd = getspnam(username);
|
||||||
if (!spwd) {
|
if (!spwd) {
|
||||||
if (errno == 0)
|
if (errno == 0)
|
||||||
|
@ -73,6 +82,12 @@ Result<Account, String> Account::from_name(const char* username)
|
||||||
|
|
||||||
return String(strerror(errno));
|
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);
|
return from_passwd(*pwd, *spwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,6 +101,7 @@ Result<Account, String> Account::from_uid(uid_t uid)
|
||||||
|
|
||||||
return String(strerror(errno));
|
return String(strerror(errno));
|
||||||
}
|
}
|
||||||
|
#ifndef AK_OS_MACOS
|
||||||
auto* spwd = getspnam(pwd->pw_name);
|
auto* spwd = getspnam(pwd->pw_name);
|
||||||
if (!spwd) {
|
if (!spwd) {
|
||||||
if (errno == 0)
|
if (errno == 0)
|
||||||
|
@ -93,6 +109,12 @@ Result<Account, String> Account::from_uid(uid_t uid)
|
||||||
|
|
||||||
return String(strerror(errno));
|
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);
|
return from_passwd(*pwd, *spwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,6 +215,7 @@ String Account::generate_passwd_file() const
|
||||||
return builder.to_string();
|
return builder.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef AK_OS_MACOS
|
||||||
String Account::generate_shadow_file() const
|
String Account::generate_shadow_file() const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
|
@ -228,18 +251,21 @@ String Account::generate_shadow_file() const
|
||||||
|
|
||||||
return builder.to_string();
|
return builder.to_string();
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
bool Account::sync()
|
bool Account::sync()
|
||||||
{
|
{
|
||||||
auto new_passwd_file_content = generate_passwd_file();
|
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();
|
auto new_shadow_file_content = generate_shadow_file();
|
||||||
|
VERIFY(!new_shadow_file_content.is_null());
|
||||||
if (new_passwd_file_content.is_null() || new_shadow_file_content.is_null()) {
|
#endif
|
||||||
VERIFY_NOT_REACHED();
|
|
||||||
}
|
|
||||||
|
|
||||||
char new_passwd_name[] = "/etc/passwd.XXXXXX";
|
char new_passwd_name[] = "/etc/passwd.XXXXXX";
|
||||||
|
#ifndef AK_OS_MACOS
|
||||||
char new_shadow_name[] = "/etc/shadow.XXXXXX";
|
char new_shadow_name[] = "/etc/shadow.XXXXXX";
|
||||||
|
#endif
|
||||||
|
|
||||||
{
|
{
|
||||||
auto new_passwd_fd = mkstemp(new_passwd_name);
|
auto new_passwd_fd = mkstemp(new_passwd_name);
|
||||||
|
@ -248,12 +274,14 @@ bool Account::sync()
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); };
|
ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); };
|
||||||
|
#ifndef AK_OS_MACOS
|
||||||
auto new_shadow_fd = mkstemp(new_shadow_name);
|
auto new_shadow_fd = mkstemp(new_shadow_name);
|
||||||
if (new_shadow_fd < 0) {
|
if (new_shadow_fd < 0) {
|
||||||
perror("mkstemp");
|
perror("mkstemp");
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
ScopeGuard new_shadow_fd_guard = [new_shadow_fd] { close(new_shadow_fd); };
|
ScopeGuard new_shadow_fd_guard = [new_shadow_fd] { close(new_shadow_fd); };
|
||||||
|
#endif
|
||||||
|
|
||||||
if (fchmod(new_passwd_fd, 0644) < 0) {
|
if (fchmod(new_passwd_fd, 0644) < 0) {
|
||||||
perror("fchmod");
|
perror("fchmod");
|
||||||
|
@ -267,12 +295,14 @@ bool Account::sync()
|
||||||
}
|
}
|
||||||
VERIFY(static_cast<size_t>(nwritten) == new_passwd_file_content.length());
|
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());
|
nwritten = write(new_shadow_fd, new_shadow_file_content.characters(), new_shadow_file_content.length());
|
||||||
if (nwritten < 0) {
|
if (nwritten < 0) {
|
||||||
perror("write");
|
perror("write");
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
VERIFY(static_cast<size_t>(nwritten) == new_shadow_file_content.length());
|
VERIFY(static_cast<size_t>(nwritten) == new_shadow_file_content.length());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rename(new_passwd_name, "/etc/passwd") < 0) {
|
if (rename(new_passwd_name, "/etc/passwd") < 0) {
|
||||||
|
@ -280,10 +310,12 @@ bool Account::sync()
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef AK_OS_MACOS
|
||||||
if (rename(new_shadow_name, "/etc/shadow") < 0) {
|
if (rename(new_shadow_name, "/etc/shadow") < 0) {
|
||||||
perror("Failed to install new /etc/shadow");
|
perror("Failed to install new /etc/shadow");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
// FIXME: Sync extra groups.
|
// FIXME: Sync extra groups.
|
||||||
|
|
|
@ -11,7 +11,11 @@
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <shadow.h>
|
#ifndef AK_OS_MACOS
|
||||||
|
# include <shadow.h>
|
||||||
|
#else
|
||||||
|
# include <LibC/shadow.h>
|
||||||
|
#endif
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
@ -52,7 +56,9 @@ private:
|
||||||
Account(const passwd& pwd, const spwd& spwd, Vector<gid_t> extra_gids);
|
Account(const passwd& pwd, const spwd& spwd, Vector<gid_t> extra_gids);
|
||||||
|
|
||||||
String generate_passwd_file() const;
|
String generate_passwd_file() const;
|
||||||
|
#ifndef AK_OS_MACOS
|
||||||
String generate_shadow_file() const;
|
String generate_shadow_file() const;
|
||||||
|
#endif
|
||||||
|
|
||||||
String m_username;
|
String m_username;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue