2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2023-04-09 15:27:30 +02:00
|
|
|
#include <AK/Format.h>
|
2022-07-05 23:48:45 +02:00
|
|
|
#include <AK/ScopeGuard.h>
|
2021-02-12 05:40:57 +03:30
|
|
|
#include <AK/TemporaryChange.h>
|
2020-02-14 21:41:10 +01:00
|
|
|
#include <AK/Vector.h>
|
2021-05-14 16:32:57 +02:00
|
|
|
#include <errno.h>
|
2018-10-31 19:49:22 +01:00
|
|
|
#include <pwd.h>
|
|
|
|
|
#include <stdio.h>
|
2020-03-08 12:05:14 +01:00
|
|
|
#include <string.h>
|
2019-06-07 11:49:03 +02:00
|
|
|
#include <unistd.h>
|
2018-10-31 19:49:22 +01:00
|
|
|
|
2018-10-31 21:31:56 +01:00
|
|
|
extern "C" {
|
|
|
|
|
|
2020-08-25 17:21:45 +03:00
|
|
|
static FILE* s_stream = nullptr;
|
|
|
|
|
static unsigned s_line_number = 0;
|
2018-11-01 16:20:22 +01:00
|
|
|
|
2018-10-31 19:49:22 +01:00
|
|
|
void setpwent()
|
|
|
|
|
{
|
2020-08-25 17:21:45 +03:00
|
|
|
s_line_number = 0;
|
|
|
|
|
if (s_stream) {
|
|
|
|
|
rewind(s_stream);
|
2018-10-31 19:49:22 +01:00
|
|
|
} else {
|
2020-08-25 17:21:45 +03:00
|
|
|
s_stream = fopen("/etc/passwd", "r");
|
|
|
|
|
if (!s_stream) {
|
2018-10-31 21:31:56 +01:00
|
|
|
perror("open /etc/passwd");
|
|
|
|
|
}
|
2018-10-31 19:49:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void endpwent()
|
|
|
|
|
{
|
2020-08-25 17:21:45 +03:00
|
|
|
s_line_number = 0;
|
|
|
|
|
if (s_stream) {
|
|
|
|
|
fclose(s_stream);
|
|
|
|
|
s_stream = nullptr;
|
2018-10-31 19:49:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct passwd* getpwuid(uid_t uid)
|
|
|
|
|
{
|
|
|
|
|
setpwent();
|
2022-07-05 23:48:45 +02:00
|
|
|
ScopeGuard guard = [] { endpwent(); };
|
2018-10-31 19:49:22 +01:00
|
|
|
while (auto* pw = getpwent()) {
|
|
|
|
|
if (pw->pw_uid == uid)
|
|
|
|
|
return pw;
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
struct passwd* getpwnam(char const* name)
|
2018-10-31 19:49:22 +01:00
|
|
|
{
|
|
|
|
|
setpwent();
|
2022-07-05 23:48:45 +02:00
|
|
|
ScopeGuard guard = [] { endpwent(); };
|
2018-10-31 19:49:22 +01:00
|
|
|
while (auto* pw = getpwent()) {
|
|
|
|
|
if (!strcmp(pw->pw_name, name))
|
|
|
|
|
return pw;
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 07:17:23 +01:00
|
|
|
static bool parse_pwddb_entry(char* raw_line, struct passwd& passwd_entry)
|
2018-10-31 19:49:22 +01:00
|
|
|
{
|
2022-11-08 07:17:23 +01:00
|
|
|
size_t line_length = strlen(raw_line);
|
|
|
|
|
for (size_t i = 0; i < line_length; ++i) {
|
|
|
|
|
auto& ch = raw_line[i];
|
|
|
|
|
if (ch == '\r' || ch == '\n')
|
|
|
|
|
line_length = i;
|
|
|
|
|
if (ch == ':' || ch == '\r' || ch == '\n')
|
|
|
|
|
ch = '\0';
|
|
|
|
|
}
|
|
|
|
|
auto line = StringView { raw_line, line_length };
|
|
|
|
|
auto parts = line.split_view('\0', SplitBehavior::KeepEmpty);
|
2018-10-31 19:49:22 +01:00
|
|
|
if (parts.size() != 7) {
|
2021-05-01 12:01:54 +02:00
|
|
|
dbgln("getpwent(): Malformed entry on line {}", s_line_number);
|
2020-08-25 17:21:45 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 07:17:23 +01:00
|
|
|
auto& name = parts[0];
|
|
|
|
|
auto& passwd = parts[1];
|
2020-08-25 17:21:45 +03:00
|
|
|
auto& uid_string = parts[2];
|
|
|
|
|
auto& gid_string = parts[3];
|
2022-11-08 07:17:23 +01:00
|
|
|
auto& gecos = parts[4];
|
|
|
|
|
auto& dir = parts[5];
|
|
|
|
|
auto& shell = parts[6];
|
2020-08-25 17:21:45 +03:00
|
|
|
|
|
|
|
|
auto uid = uid_string.to_uint();
|
|
|
|
|
if (!uid.has_value()) {
|
2021-05-01 12:01:54 +02:00
|
|
|
dbgln("getpwent(): Malformed UID on line {}", s_line_number);
|
2020-08-25 17:21:45 +03:00
|
|
|
return false;
|
2018-10-31 19:49:22 +01:00
|
|
|
}
|
2020-08-25 17:21:45 +03:00
|
|
|
auto gid = gid_string.to_uint();
|
|
|
|
|
if (!gid.has_value()) {
|
2021-05-01 12:01:54 +02:00
|
|
|
dbgln("getpwent(): Malformed GID on line {}", s_line_number);
|
2020-08-25 17:21:45 +03:00
|
|
|
return false;
|
2018-10-31 19:49:22 +01:00
|
|
|
}
|
2020-08-25 17:21:45 +03:00
|
|
|
|
2022-11-08 07:17:23 +01:00
|
|
|
passwd_entry.pw_name = const_cast<char*>(name.characters_without_null_termination());
|
|
|
|
|
passwd_entry.pw_passwd = const_cast<char*>(passwd.characters_without_null_termination());
|
|
|
|
|
passwd_entry.pw_uid = uid.value();
|
|
|
|
|
passwd_entry.pw_gid = gid.value();
|
|
|
|
|
passwd_entry.pw_gecos = const_cast<char*>(gecos.characters_without_null_termination());
|
|
|
|
|
passwd_entry.pw_dir = const_cast<char*>(dir.characters_without_null_termination());
|
|
|
|
|
passwd_entry.pw_shell = const_cast<char*>(shell.characters_without_null_termination());
|
2020-08-25 17:21:45 +03:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct passwd* getpwent()
|
2022-11-08 07:17:23 +01:00
|
|
|
{
|
|
|
|
|
static struct passwd passwd_entry;
|
|
|
|
|
static char buffer[1024];
|
|
|
|
|
struct passwd* result;
|
|
|
|
|
if (getpwent_r(&passwd_entry, buffer, sizeof(buffer), &result) < 0)
|
|
|
|
|
return nullptr;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int getpwent_r(struct passwd* passwd_buf, char* buffer, size_t buffer_size, struct passwd** passwd_entry_ptr)
|
2020-08-25 17:21:45 +03:00
|
|
|
{
|
|
|
|
|
if (!s_stream)
|
|
|
|
|
setpwent();
|
|
|
|
|
|
|
|
|
|
while (true) {
|
2022-11-08 07:17:23 +01:00
|
|
|
if (!s_stream || feof(s_stream)) {
|
2022-11-18 09:52:22 +01:00
|
|
|
*passwd_entry_ptr = nullptr;
|
|
|
|
|
return ENOENT;
|
2022-11-08 07:17:23 +01:00
|
|
|
}
|
2020-08-25 17:21:45 +03:00
|
|
|
|
|
|
|
|
if (ferror(s_stream)) {
|
2022-11-18 09:52:22 +01:00
|
|
|
*passwd_entry_ptr = nullptr;
|
|
|
|
|
return ferror(s_stream);
|
2020-08-25 17:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++s_line_number;
|
2022-11-08 07:17:23 +01:00
|
|
|
char* s = fgets(buffer, buffer_size, s_stream);
|
2020-08-25 17:21:45 +03:00
|
|
|
|
2022-11-08 07:17:23 +01:00
|
|
|
if ((!s || !s[0]) && feof(s_stream)) {
|
|
|
|
|
*passwd_entry_ptr = nullptr;
|
2022-11-18 09:52:22 +01:00
|
|
|
return ENOENT;
|
2022-11-08 07:17:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strlen(s) == buffer_size - 1) {
|
2022-11-18 09:52:22 +01:00
|
|
|
*passwd_entry_ptr = nullptr;
|
|
|
|
|
return ERANGE;
|
2022-11-08 07:17:23 +01:00
|
|
|
}
|
2020-08-25 17:21:45 +03:00
|
|
|
|
2022-11-08 07:17:23 +01:00
|
|
|
if (parse_pwddb_entry(buffer, *passwd_buf)) {
|
|
|
|
|
*passwd_entry_ptr = passwd_buf;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-10-31 19:49:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-01-12 19:03:21 +11:00
|
|
|
|
2022-11-17 20:43:42 +01:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwnam.html
|
|
|
|
|
int getpwnam_r(char const* name, struct passwd* pwd, char* buffer, size_t bufsize, struct passwd** result)
|
2021-02-12 05:40:57 +03:30
|
|
|
{
|
|
|
|
|
setpwent();
|
2022-11-17 20:43:42 +01:00
|
|
|
for (;;) {
|
|
|
|
|
if (auto rc = getpwent_r(pwd, buffer, bufsize, result); rc != 0)
|
|
|
|
|
return rc;
|
|
|
|
|
if (strcmp(pwd->pw_name, name) == 0)
|
|
|
|
|
return 0;
|
2021-02-12 05:40:57 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-17 20:43:42 +01:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwuid.html
|
|
|
|
|
int getpwuid_r(uid_t uid, struct passwd* pwd, char* buffer, size_t bufsize, struct passwd** result)
|
2021-02-12 05:40:57 +03:30
|
|
|
{
|
|
|
|
|
setpwent();
|
2022-11-17 20:43:42 +01:00
|
|
|
for (;;) {
|
|
|
|
|
if (auto rc = getpwent_r(pwd, buffer, bufsize, result); rc != 0)
|
|
|
|
|
return rc;
|
|
|
|
|
if (pwd->pw_uid == uid)
|
|
|
|
|
return 0;
|
2021-02-12 05:40:57 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-12 19:03:21 +11:00
|
|
|
int putpwent(const struct passwd* p, FILE* stream)
|
|
|
|
|
{
|
2020-07-25 16:05:19 -06:00
|
|
|
if (!p || !stream || !p->pw_passwd || !p->pw_name || !p->pw_dir || !p->pw_gecos || !p->pw_shell) {
|
2020-01-12 19:03:21 +11:00
|
|
|
errno = EINVAL;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
auto is_valid_field = [](char const* str) {
|
2020-01-12 19:03:21 +11:00
|
|
|
return str && !strpbrk(str, ":\n");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!is_valid_field(p->pw_name) || !is_valid_field(p->pw_dir) || !is_valid_field(p->pw_gecos) || !is_valid_field(p->pw_shell)) {
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 16:05:19 -06:00
|
|
|
int nwritten = fprintf(stream, "%s:%s:%u:%u:%s,,,:%s:%s\n", p->pw_name, p->pw_passwd, p->pw_uid, p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);
|
2020-01-12 19:03:21 +11:00
|
|
|
if (!nwritten || nwritten < 0) {
|
|
|
|
|
errno = ferror(stream);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-10-31 21:31:56 +01:00
|
|
|
}
|