Files
semi-libc/src/passwd/account_client.h
T
2026-10-27 15:45:33 +00:00

627 lines
17 KiB
C

#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include "spp.h"
#include "json.h"
#ifndef _ACCOUNT_CLIENT
#define _ACCOUNT_CLIENT
#define REQ2(condition, err) if (!(condition)) { errno = err; return -1; }
#define REQ(condition) REQ2(condition, EPERM)
#define REQP2(condition, err) if (!(condition)) { errno = err; return NULL; }
#define REQP(condition) REQP2(condition, EPERM)
#define ITEROBJ(obj, it) for (struct json_object_element_s *it = obj->start; it != NULL; it = it->next)
#define DECLPOOL(pool, buf, size) struct account_memory_pool pool = { .ptr = buf, .len = size, .cursor = 0 }
#ifdef __linux__
static inline int account_connect() {
return spp_connect_linux("verified:org.semilabs.os/accountd");
}
#else
static inline int account_connect() {
return spp_connect_unix("/var/run/accountd.sock");
}
#endif /* __linux__ */
struct account_memory_pool {
void *ptr;
size_t cursor;
size_t len;
};
static inline void *account_allocate_pool(struct account_memory_pool *pool, size_t size) {
if (!pool || !pool->ptr || size == 0) {
return NULL;
}
if (pool->cursor + size > pool->len) {
return NULL;
}
void *result = (char *)pool->ptr + pool->cursor;
pool->cursor += size;
return result;
}
static inline int account_set_pw_gid(
int fd,
const char *user_uuid,
struct passwd *passwd
) {
int status;
const char *tmpl = "{\"method\": \"GetUserGroups\", \"params\": {\"user\": \"%s\"}}";
int need = snprintf(NULL, 0, tmpl, user_uuid);
REQ(need > 0);
char *req = malloc(need + 1);
REQ2(req, ENOMEM);
snprintf(req, need + 1, tmpl, user_uuid);
status = spp_send(fd, req);
free(req);
REQ(status == 0);
const char *reply_str = spp_recv(fd);
REQ(reply_str);
struct json_value_s *reply = json_parse(reply_str, strlen(reply_str));
spp_free((void*)reply_str);
REQ(reply);
struct json_array_s *groups = NULL;
struct json_object_s *obj = json_value_as_object(reply);
if (!obj) { free(reply); errno = EPERM; return -1; }
ITEROBJ(obj, it) {
if (strcmp(it->name->string, "success") == 0) {
if (json_value_is_false(it->value)) { free(reply); errno = ENOENT; return -1; }
continue;
}
if (strcmp(it->name->string, "value") == 0) {
groups = json_value_as_array(it->value);
continue;
}
}
if (!groups) { free(reply); errno = EPERM; return -1; }
for (struct json_array_element_s *e = groups->start; e; e = e->next) {
struct json_string_s *u = json_value_as_string(e->value);
if (!u) continue;
const char *gtmpl = "{\"method\": \"GetGroupInfo\", \"params\": {\"uuid\": \"%s\"}}";
int gneed = snprintf(NULL, 0, gtmpl, u->string);
if (gneed <= 0) continue;
char *greq = malloc(gneed + 1);
if (!greq) { free(reply); errno = ENOMEM; return -1; }
snprintf(greq, gneed + 1, gtmpl, u->string);
status = spp_send(fd, greq);
free(greq);
if (status != 0) continue;
const char *gstr = spp_recv(fd);
if (!gstr) { free(reply); errno = EIO; return -1; }
struct json_value_s *greply = json_parse(gstr, strlen(gstr));
spp_free((void*)gstr);
if (!greply) { free(reply); errno = EPERM; return -1; }
struct json_object_s *gobj = json_value_as_object(greply);
struct json_object_s *gval = NULL;
if (gobj) ITEROBJ(gobj, gi) {
if (strcmp(gi->name->string, "value") == 0) {
gval = json_value_as_object(gi->value);
break;
}
}
int found = 0;
if (gval) ITEROBJ(gval, fi) {
if (strcmp(fi->name->string, "host_gid") == 0) {
struct json_number_s *n = json_value_as_number(fi->value);
if (n) {
passwd->pw_gid = (gid_t)atoi(n->number);
found = 1;
}
break;
}
}
free(greply);
if (found) { free(reply); return 0; }
}
free(reply);
errno = ENOENT;
return -1;
}
static inline int account_recordpw(
int fd,
struct json_value_s *root,
struct passwd *passwd,
struct account_memory_pool *pool
) {
const char *uuid = NULL;
int has_uid = 0;
passwd->pw_passwd = account_allocate_pool(pool, 2);
REQ2(passwd->pw_passwd, ERANGE);
strcpy(passwd->pw_passwd, "x");
passwd->pw_gecos = account_allocate_pool(pool, 1);
REQ2(passwd->pw_gecos, ERANGE);
strcpy(passwd->pw_gecos, "");
struct json_object_s *object = json_value_as_object(root);
REQ(object);
ITEROBJ(object, it) {
const char *name = it->name->string;
struct json_value_s *value = it->value;
if (strcmp(name, "username") == 0) {
struct json_string_s *username = json_value_as_string(value);
REQ(username);
passwd->pw_name = account_allocate_pool(pool, username->string_size + 1);
REQ2(passwd->pw_name, ERANGE);
strcpy(passwd->pw_name, username->string);
continue;
}
if (strcmp(name, "home_directory") == 0) {
struct json_string_s *homedir = json_value_as_string(value);
REQ(homedir);
passwd->pw_dir = account_allocate_pool(pool, homedir->string_size + 1);
REQ2(passwd->pw_dir, ERANGE);
strcpy(passwd->pw_dir, homedir->string);
continue;
}
if (strcmp(name, "uuid") == 0) {
struct json_string_s *u = json_value_as_string(value);
REQ(u);
uuid = u->string;
continue;
}
if (strcmp(name, "defaults") == 0) {
struct json_object_s *defaults = json_value_as_object(value);
REQ(defaults);
ITEROBJ(defaults, def) {
name = def->name->string;
value = def->value;
if (strcmp(name, "CliLoginShell") == 0) {
struct json_string_s *shell = json_value_as_string(value);
REQ(shell);
passwd->pw_shell = account_allocate_pool(pool, shell->string_size + 1);
REQ2(passwd->pw_shell, ERANGE);
strcpy(passwd->pw_shell, shell->string);
continue;
}
}
continue;
}
if (strcmp(name, "host_uid") == 0) {
struct json_number_s *uid = json_value_as_number(value);
REQ(uid);
passwd->pw_uid = atoi(uid->number);
has_uid = 1;
continue;
}
}
if (uuid) {
REQ(account_set_pw_gid(fd, uuid, passwd) == 0);
} else {
passwd->pw_gid = 0;
}
if (!has_uid) {
errno = EAGAIN;
return -1;
}
return 0;
}
static inline int account_getpwuuid(
int fd,
const char *uuid,
struct passwd *passwd,
struct account_memory_pool *pool
);
static inline int account_fill_gr_members(
int fd,
const char *group_uuid,
struct group *group,
struct account_memory_pool *pool
) {
int status;
const char *request_template =
"{\"method\": \"GetGroupMembers\", \"params\": {\"group\": \"%s\"}}";
int request_need = snprintf(NULL, 0, request_template, group_uuid);
REQ(request_need > 0);
char *request_json = malloc(request_need + 1);
REQ2(request_json, ENOMEM);
snprintf(request_json, request_need + 1, request_template, group_uuid);
status = spp_send(fd, request_json);
free(request_json);
REQ(status == 0);
const char *reply_str = spp_recv(fd);
REQ(reply_str);
struct json_value_s *reply = json_parse(reply_str, strlen(reply_str));
spp_free((void*)reply_str);
REQ(reply);
struct json_array_s *members = NULL;
struct json_object_s *reply_object = json_value_as_object(reply);
if (!reply_object) goto format_error;
ITEROBJ(reply_object, it) {
if (strcmp(it->name->string, "success") == 0) {
if (json_value_is_false(it->value)) goto not_found;
continue;
}
if (strcmp(it->name->string, "value") == 0) {
members = json_value_as_array(it->value);
continue;
}
}
if (!members) goto format_error;
size_t count = 0;
for (struct json_array_element_s *e = members->start; e; e = e->next) {
count++;
}
char **names = account_allocate_pool(pool, (count + 1) * sizeof(char *));
if (!names) goto range_error;
size_t i = 0;
for (struct json_array_element_s *e = members->start; e; e = e->next) {
struct json_string_s *member_uuid = json_value_as_string(e->value);
if (!member_uuid) continue;
struct passwd pw;
memset(&pw, 0, sizeof(pw));
status = account_getpwuuid(fd, member_uuid->string, &pw, pool);
if (status == 0 && pw.pw_name) {
names[i++] = pw.pw_name;
}
}
names[i] = NULL;
group->gr_mem = names;
free(reply);
return 0;
range_error:
errno = ERANGE;
goto fail;
format_error:
errno = EPERM;
goto fail;
not_found:
errno = ENOENT;
fail:
free(reply);
return -1;
}
static inline int account_recordgr(
int fd,
struct json_value_s *root,
struct group *group,
struct account_memory_pool *pool
) {
group->gr_passwd = account_allocate_pool(pool, 2);
REQ2(group->gr_passwd, ERANGE);
strcpy(group->gr_passwd, "x");
const char *uuid = NULL;
int has_gid = 0;
struct json_object_s *object = json_value_as_object(root);
REQ(object);
ITEROBJ(object, it) {
const char *name = it->name->string;
struct json_value_s *value = it->value;
if (strcmp(name, "groupname") == 0) {
struct json_string_s *groupname = json_value_as_string(value);
REQ(groupname);
group->gr_name = account_allocate_pool(pool, groupname->string_size + 1);
REQ2(group->gr_name, ERANGE);
strcpy(group->gr_name, groupname->string);
continue;
}
if (strcmp(name, "host_gid") == 0) {
struct json_number_s *gid = json_value_as_number(value);
REQ(gid);
group->gr_gid = atoi(gid->number);
has_gid = 1;
continue;
}
if (strcmp(name, "uuid") == 0) {
struct json_string_s *u = json_value_as_string(value);
REQ(u);
uuid = u->string;
continue;
}
}
if (uuid) {
REQ(account_fill_gr_members(fd, uuid, group, pool) == 0);
} else {
char **empty = account_allocate_pool(pool, sizeof(char *));
REQ2(empty, ERANGE);
empty[0] = NULL;
group->gr_mem = empty;
}
if (!has_gid) {
errno = EAGAIN;
return -1;
}
return 0;
}
typedef int (*pwgr_parser_t)(int, struct json_value_s*, void*, struct account_memory_pool*);
static inline int account_getpwgrkey(
int fd,
const char *method,
const char *key,
const char *valueborder,
const char *name,
pwgr_parser_t parser,
void *pwgr,
struct account_memory_pool *pool
) {
int status;
const char *request_template = "{\"method\": \"%s\", \"params\": {\"%s\": %s%s%s}}";
int request_need = snprintf(NULL, 0, request_template, method, key, valueborder, name, valueborder);
REQ(request_need > 0);
char *request_json = malloc(request_need + 1);
REQ2(request_json, ENOMEM);
snprintf(request_json, request_need + 1, request_template, method, key, valueborder, name, valueborder);
status = spp_send(fd, request_json);
free(request_json);
REQ(status == 0);
const char *reply_str = spp_recv(fd);
REQ(reply_str);
struct json_value_s *reply = json_parse(reply_str, strlen(reply_str));
spp_free((void*)reply_str);
REQ(reply);
struct json_object_s *reply_object = json_value_as_object(reply);
if (!reply_object) goto format_error;
ITEROBJ(reply_object, it) {
if (strcmp(it->name->string, "success") == 0) {
if (json_value_is_false(it->value)) goto not_found;
continue;
}
if (strcmp(it->name->string, "value") == 0) {
status = parser(fd, it->value, pwgr, pool);
if (status != 0) goto not_found;
continue;
}
}
free(reply);
return 0;
format_error:
errno = EPERM;
not_found:
free(reply);
return -1;
}
static inline char *account_list_uuid_next(
int fd,
const char *method,
unsigned int no
) {
int status;
const char *request_template = "{\"method\": \"%s\", \"params\": {\"start\": %u, \"len\": 1}}";
int request_need = snprintf(NULL, 0, request_template, method, no);
REQP(request_need > 0);
char *request_json = malloc(request_need + 1);
REQP2(request_json, ENOMEM);
snprintf(request_json, request_need + 1, request_template, method, no);
status = spp_send(fd, request_json);
free(request_json);
REQP(status == 0);
const char *reply_str = spp_recv(fd);
REQP(reply_str);
struct json_value_s *reply = json_parse(reply_str, strlen(reply_str));
spp_free((void*)reply_str);
REQP(reply);
struct json_object_s *reply_object = json_value_as_object(reply);
if (!reply_object) goto format_error;
ITEROBJ(reply_object, it) {
if (strcmp(it->name->string, "success") == 0) {
if (json_value_is_false(it->value)) goto not_found;
continue;
}
if (strcmp(it->name->string, "value") == 0) {
struct json_array_s *list = json_value_as_array(it->value);
if (!list) goto not_found;
struct json_array_element_s *first = list->start;
if (!first) goto not_found;
struct json_string_s *item = json_value_as_string(first->value);
if (!item) goto format_error;
return strdup(item->string);
}
}
free(reply);
return NULL;
format_error:
errno = EPERM;
not_found:
free(reply);
return NULL;
}
static inline int account_getpwuid(
int fd,
uid_t uid,
struct passwd *passwd,
struct account_memory_pool *pool
) {
char uid_str[32];
int len = snprintf(uid_str, sizeof(uid_str), "%u", (unsigned int)uid);
REQ(len > 0 && (size_t)len < sizeof(uid_str));
return account_getpwgrkey(
fd,
"GetUserInfo",
"host_uid",
"",
uid_str,
(pwgr_parser_t)account_recordpw,
passwd,
pool
);
}
static inline int account_getpwnam(
int fd,
const char *name,
struct passwd *passwd,
struct account_memory_pool *pool
) {
return account_getpwgrkey(
fd,
"GetUserInfo",
"username",
"\"",
name,
(pwgr_parser_t)account_recordpw,
passwd,
pool
);
}
static inline int account_getpwuuid(
int fd,
const char *uuid,
struct passwd *passwd,
struct account_memory_pool *pool
) {
return account_getpwgrkey(
fd,
"GetUserInfo",
"uuid",
"\"",
uuid,
(pwgr_parser_t)account_recordpw,
passwd,
pool
);
}
static inline int account_getgrgid(
int fd,
gid_t gid,
struct group *group,
struct account_memory_pool *pool
) {
char gid_str[32];
int len = snprintf(gid_str, sizeof(gid_str), "%u", (unsigned int)gid);
REQ(len > 0 && (size_t)len < sizeof(gid_str));
return account_getpwgrkey(
fd,
"GetGroupInfo",
"host_gid",
"",
gid_str,
(pwgr_parser_t)account_recordgr,
group,
pool
);
}
static inline int account_getgrnam(
int fd,
const char *name,
struct group *group,
struct account_memory_pool *pool
) {
return account_getpwgrkey(
fd,
"GetGroupInfo",
"groupname",
"\"",
name,
(pwgr_parser_t)account_recordgr,
group,
pool
);
}
static inline int account_getgruuid(
int fd,
const char *uuid,
struct group *group,
struct account_memory_pool *pool
) {
return account_getpwgrkey(
fd,
"GetGroupInfo",
"uuid",
"\"",
uuid,
(pwgr_parser_t)account_recordgr,
group,
pool
);
}
static inline int account_getpwent(
int fd,
unsigned int *no,
struct passwd *passwd,
struct account_memory_pool *pool
) {
int status;
for (;;) {
char *next_uuid = account_list_uuid_next(fd, "ListUser", *no);
REQ2(next_uuid, errno);
status = account_getpwuuid(fd, next_uuid, passwd, pool);
free(next_uuid);
*no += 1;
if (status == 0) return 0;
if (errno != EAGAIN) return -1;
}
}
static inline int account_getgrent(
int fd,
unsigned int *no,
struct group *group,
struct account_memory_pool *pool
) {
int status;
for (;;) {
char *next_uuid = account_list_uuid_next(fd, "ListGroup", *no);
REQ2(next_uuid, errno);
status = account_getgruuid(fd, next_uuid, group, pool);
free(next_uuid);
*no += 1;
if (status == 0) return 0;
if (errno != EAGAIN) return -1;
}
}
#endif /* _ACCOUNT_CLIENT */