From f384a1ded1770f8227cbd1feb857028dd0533b94 Mon Sep 17 00:00:00 2001 From: sisungo Date: Tue, 27 Oct 2026 15:45:33 +0000 Subject: [PATCH] feat: support accountd new group-list protocol Signed-off-by: sisungo --- src/passwd/account_client.h | 258 +++++++++++++++++++++++++++++++++--- src/passwd/getgr_a.c | 179 ------------------------- src/passwd/getgrouplist.c | 108 ++++++--------- src/passwd/getpw_a.c | 142 -------------------- src/passwd/nscd.h | 44 ------ src/passwd/nscd_query.c | 115 ---------------- src/passwd/pwf.h | 2 - 7 files changed, 283 insertions(+), 565 deletions(-) delete mode 100644 src/passwd/getgr_a.c delete mode 100644 src/passwd/getpw_a.c delete mode 100644 src/passwd/nscd.h delete mode 100644 src/passwd/nscd_query.c diff --git a/src/passwd/account_client.h b/src/passwd/account_client.h index 1644b2b9..64ff7b61 100644 --- a/src/passwd/account_client.h +++ b/src/passwd/account_client.h @@ -47,11 +47,101 @@ static inline void *account_allocate_pool(struct account_memory_pool *pool, 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"); @@ -82,6 +172,12 @@ static inline int account_recordpw( 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); @@ -103,14 +199,114 @@ static inline int account_recordpw( 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 @@ -119,6 +315,9 @@ static inline int account_recordgr( 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) { @@ -137,14 +336,35 @@ static inline int account_recordgr( 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)(struct json_value_s*, void*, struct account_memory_pool*); +typedef int (*pwgr_parser_t)(int, struct json_value_s*, void*, struct account_memory_pool*); static inline int account_getpwgrkey( int fd, @@ -182,7 +402,7 @@ static inline int account_getpwgrkey( continue; } if (strcmp(it->name->string, "value") == 0) { - status = parser(it->value, pwgr, pool); + status = parser(fd, it->value, pwgr, pool); if (status != 0) goto not_found; continue; } @@ -373,14 +593,15 @@ static inline int account_getpwent( ) { int status; - 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); - REQ(status == 0); - *no += 1; - - return 0; + 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( @@ -391,14 +612,15 @@ static inline int account_getgrent( ) { int status; - 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); - REQ(status == 0); - *no += 1; - - return 0; + 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 */ diff --git a/src/passwd/getgr_a.c b/src/passwd/getgr_a.c deleted file mode 100644 index 84550404..00000000 --- a/src/passwd/getgr_a.c +++ /dev/null @@ -1,179 +0,0 @@ -#include -#include -#include -#include -#include "pwf.h" -#include "nscd.h" - -static char *itoa(char *p, uint32_t x) -{ - // number of digits in a uint32_t + NUL - p += 11; - *--p = 0; - do { - *--p = '0' + x % 10; - x /= 10; - } while (x); - return p; -} - -int __getgr_a(const char *name, gid_t gid, struct group *gr, char **buf, size_t *size, char ***mem, size_t *nmem, struct group **res) -{ - FILE *f; - int rv = 0; - int cs; - - *res = 0; - - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - f = fopen("/etc/group", "rbe"); - if (!f) { - rv = errno; - goto done; - } - - while (!(rv = __getgrent_a(f, gr, buf, size, mem, nmem, res)) && *res) { - if (name && !strcmp(name, (*res)->gr_name) - || !name && (*res)->gr_gid == gid) { - break; - } - } - fclose(f); - - if (!*res && (rv == 0 || rv == ENOENT || rv == ENOTDIR)) { - int32_t req = name ? GETGRBYNAME : GETGRBYGID; - int32_t i; - const char *key; - int32_t groupbuf[GR_LEN] = {0}; - size_t len = 0; - size_t grlist_len = 0; - char gidbuf[11] = {0}; - int swap = 0; - char *ptr; - - if (name) { - key = name; - } else { - if (gid < 0 || gid > UINT32_MAX) { - rv = 0; - goto done; - } - key = itoa(gidbuf, gid); - } - - f = __nscd_query(req, key, groupbuf, sizeof groupbuf, &swap); - if (!f) { rv = errno; goto done; } - - if (!groupbuf[GRFOUND]) { rv = 0; goto cleanup_f; } - - if (!groupbuf[GRNAMELEN] || !groupbuf[GRPASSWDLEN]) { - rv = EIO; - goto cleanup_f; - } - - if (groupbuf[GRMEMCNT] > (size_t)(INT32_MAX-1)) { - rv = ENOMEM; - goto cleanup_f; - } - if (groupbuf[GRNAMELEN] > SIZE_MAX - groupbuf[GRPASSWDLEN]) { - rv = ENOMEM; - goto cleanup_f; - } - len = groupbuf[GRNAMELEN] + groupbuf[GRPASSWDLEN]; - - for (i = 0; i < groupbuf[GRMEMCNT]; i++) { - uint32_t name_len; - if (fread(&name_len, sizeof name_len, 1, f) < 1) { - rv = ferror(f) ? errno : EIO; - goto cleanup_f; - } - if (swap) { - name_len = bswap_32(name_len); - } - if (name_len > SIZE_MAX - grlist_len - || name_len > SIZE_MAX - len) { - rv = ENOMEM; - goto cleanup_f; - } - len += name_len; - grlist_len += name_len; - } - - if (len > *size || !*buf) { - char *tmp = realloc(*buf, len); - if (!tmp) { - rv = errno; - goto cleanup_f; - } - *buf = tmp; - *size = len; - } - - if (!fread(*buf, len, 1, f)) { - rv = ferror(f) ? errno : EIO; - goto cleanup_f; - } - - if (groupbuf[GRMEMCNT] + 1 > *nmem) { - if (groupbuf[GRMEMCNT] + 1 > SIZE_MAX/sizeof(char*)) { - rv = ENOMEM; - goto cleanup_f; - } - char **tmp = realloc(*mem, (groupbuf[GRMEMCNT]+1)*sizeof(char*)); - if (!tmp) { - rv = errno; - goto cleanup_f; - } - *mem = tmp; - *nmem = groupbuf[GRMEMCNT] + 1; - } - - if (groupbuf[GRMEMCNT]) { - mem[0][0] = *buf + groupbuf[GRNAMELEN] + groupbuf[GRPASSWDLEN]; - for (ptr = mem[0][0], i = 0; ptr != mem[0][0]+grlist_len; ptr++) - if (!*ptr) - if (igr_name = *buf; - gr->gr_passwd = gr->gr_name + groupbuf[GRNAMELEN]; - gr->gr_gid = groupbuf[GRGID]; - gr->gr_mem = *mem; - - if (gr->gr_passwd[-1] - || gr->gr_passwd[groupbuf[GRPASSWDLEN]-1]) { - rv = EIO; - goto cleanup_f; - } - - if (name && strcmp(name, gr->gr_name) - || !name && gid != gr->gr_gid) { - rv = EIO; - goto cleanup_f; - } - - *res = gr; - -cleanup_f: - fclose(f); - goto done; - } - -done: - pthread_setcancelstate(cs, 0); - if (rv) errno = rv; - return rv; -} diff --git a/src/passwd/getgrouplist.c b/src/passwd/getgrouplist.c index 301824ce..fdf9f8f7 100644 --- a/src/passwd/getgrouplist.c +++ b/src/passwd/getgrouplist.c @@ -1,81 +1,59 @@ #define _GNU_SOURCE -#include "pwf.h" #include #include -#include #include #include #include #include -#include "nscd.h" int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups) { - int rv, nlim, ret = -1; - ssize_t i, n = 1; - struct group gr; - struct group *res; - FILE *f; - int swap = 0; - int32_t resp[INITGR_LEN]; - uint32_t *nscdbuf = 0; - char *buf = 0; - char **mem = 0; - size_t nmem = 0; - size_t size; - nlim = *ngroups; - if (nlim >= 1) *groups++ = gid; + if (!user || !ngroups) + return -1; - f = __nscd_query(GETINITGR, user, resp, sizeof resp, &swap); - if (!f) goto cleanup; - if (resp[INITGRFOUND]) { - nscdbuf = calloc(resp[INITGRNGRPS], sizeof(uint32_t)); - if (!nscdbuf) goto cleanup; - size_t nbytes = sizeof(*nscdbuf)*resp[INITGRNGRPS]; - if (nbytes && !fread(nscdbuf, nbytes, 1, f)) { - if (!ferror(f)) errno = EIO; - goto cleanup; - } - if (swap) { - for (i = 0; i < resp[INITGRNGRPS]; i++) - nscdbuf[i] = bswap_32(nscdbuf[i]); - } - } - fclose(f); + size_t capacity = *ngroups > 0 ? (size_t)*ngroups : 0; + size_t count = 0; - f = fopen("/etc/group", "rbe"); - if (!f && errno != ENOENT && errno != ENOTDIR) - goto cleanup; + if (groups && count < capacity) + groups[count] = gid; + count++; - if (f) { - while (!(rv = __getgrent_a(f, &gr, &buf, &size, &mem, &nmem, &res)) && res) { - if (nscdbuf) - for (i=0; i < resp[INITGRNGRPS]; i++) { - if (nscdbuf[i] == gr.gr_gid) nscdbuf[i] = gid; - } - for (i=0; gr.gr_mem[i] && strcmp(user, gr.gr_mem[i]); i++); - if (!gr.gr_mem[i]) continue; - if (++n <= nlim) *groups++ = gr.gr_gid; - } - if (rv) { - errno = rv; - goto cleanup; - } - } - if (nscdbuf) { - for(i=0; i < resp[INITGRNGRPS]; i++) { - if (nscdbuf[i] != gid) - if(++n <= nlim) *groups++ = nscdbuf[i]; - } - } + errno = 0; + setgrent(); - ret = n > nlim ? -1 : n; - *ngroups = n; + struct group *gr; + while ((gr = getgrent()) != NULL) { + if (gr->gr_gid == gid) + continue; -cleanup: - if (f) fclose(f); - free(nscdbuf); - free(buf); - free(mem); - return ret; + int is_member = 0; + for (char **m = gr->gr_mem; m && *m; m++) { + if (strcmp(*m, user) == 0) { + is_member = 1; + break; + } + } + if (!is_member) + continue; + + if (groups && count < capacity) + groups[count] = gr->gr_gid; + count++; + } + + int saved_errno = errno; + endgrent(); + + if (saved_errno != 0) { + return -1; + } + + *ngroups = (int)count; + + if (count > capacity) { + errno = ERANGE; + return -1; + } + + return (int)count; } diff --git a/src/passwd/getpw_a.c b/src/passwd/getpw_a.c deleted file mode 100644 index 15a70c03..00000000 --- a/src/passwd/getpw_a.c +++ /dev/null @@ -1,142 +0,0 @@ -#include -#include -#include -#include -#include "pwf.h" -#include "nscd.h" - -static char *itoa(char *p, uint32_t x) -{ - // number of digits in a uint32_t + NUL - p += 11; - *--p = 0; - do { - *--p = '0' + x % 10; - x /= 10; - } while (x); - return p; -} - -int __getpw_a(const char *name, uid_t uid, struct passwd *pw, char **buf, size_t *size, struct passwd **res) -{ - FILE *f; - int cs; - int rv = 0; - - *res = 0; - - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - - f = fopen("/etc/passwd", "rbe"); - if (!f) { - rv = errno; - goto done; - } - - while (!(rv = __getpwent_a(f, pw, buf, size, res)) && *res) { - if (name && !strcmp(name, (*res)->pw_name) - || !name && (*res)->pw_uid == uid) - break; - } - fclose(f); - - if (!*res && (rv == 0 || rv == ENOENT || rv == ENOTDIR)) { - int32_t req = name ? GETPWBYNAME : GETPWBYUID; - const char *key; - int32_t passwdbuf[PW_LEN] = {0}; - size_t len = 0; - char uidbuf[11] = {0}; - - if (name) { - key = name; - } else { - /* uid outside of this range can't be queried with the - * nscd interface, but might happen if uid_t ever - * happens to be a larger type (this is not true as of - * now) - */ - if(uid < 0 || uid > UINT32_MAX) { - rv = 0; - goto done; - } - key = itoa(uidbuf, uid); - } - - f = __nscd_query(req, key, passwdbuf, sizeof passwdbuf, (int[]){0}); - if (!f) { rv = errno; goto done; } - - if(!passwdbuf[PWFOUND]) { rv = 0; goto cleanup_f; } - - /* A zero length response from nscd is invalid. We ignore - * invalid responses and just report an error, rather than - * trying to do something with them. - */ - if (!passwdbuf[PWNAMELEN] || !passwdbuf[PWPASSWDLEN] - || !passwdbuf[PWGECOSLEN] || !passwdbuf[PWDIRLEN] - || !passwdbuf[PWSHELLLEN]) { - rv = EIO; - goto cleanup_f; - } - - if ((passwdbuf[PWNAMELEN]|passwdbuf[PWPASSWDLEN] - |passwdbuf[PWGECOSLEN]|passwdbuf[PWDIRLEN] - |passwdbuf[PWSHELLLEN]) >= SIZE_MAX/8) { - rv = ENOMEM; - goto cleanup_f; - } - - len = passwdbuf[PWNAMELEN] + passwdbuf[PWPASSWDLEN] - + passwdbuf[PWGECOSLEN] + passwdbuf[PWDIRLEN] - + passwdbuf[PWSHELLLEN]; - - if (len > *size || !*buf) { - char *tmp = realloc(*buf, len); - if (!tmp) { - rv = errno; - goto cleanup_f; - } - *buf = tmp; - *size = len; - } - - if (!fread(*buf, len, 1, f)) { - rv = ferror(f) ? errno : EIO; - goto cleanup_f; - } - - pw->pw_name = *buf; - pw->pw_passwd = pw->pw_name + passwdbuf[PWNAMELEN]; - pw->pw_gecos = pw->pw_passwd + passwdbuf[PWPASSWDLEN]; - pw->pw_dir = pw->pw_gecos + passwdbuf[PWGECOSLEN]; - pw->pw_shell = pw->pw_dir + passwdbuf[PWDIRLEN]; - pw->pw_uid = passwdbuf[PWUID]; - pw->pw_gid = passwdbuf[PWGID]; - - /* Don't assume that nscd made sure to null terminate strings. - * It's supposed to, but malicious nscd should be ignored - * rather than causing a crash. - */ - if (pw->pw_passwd[-1] || pw->pw_gecos[-1] || pw->pw_dir[-1] - || pw->pw_shell[passwdbuf[PWSHELLLEN]-1]) { - rv = EIO; - goto cleanup_f; - } - - if (name && strcmp(name, pw->pw_name) - || !name && uid != pw->pw_uid) { - rv = EIO; - goto cleanup_f; - } - - - *res = pw; -cleanup_f: - fclose(f); - goto done; - } - -done: - pthread_setcancelstate(cs, 0); - if (rv) errno = rv; - return rv; -} diff --git a/src/passwd/nscd.h b/src/passwd/nscd.h deleted file mode 100644 index ae5aa8d5..00000000 --- a/src/passwd/nscd.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef NSCD_H -#define NSCD_H - -#include - -#define NSCDVERSION 2 -#define GETPWBYNAME 0 -#define GETPWBYUID 1 -#define GETGRBYNAME 2 -#define GETGRBYGID 3 -#define GETINITGR 15 - -#define REQVERSION 0 -#define REQTYPE 1 -#define REQKEYLEN 2 -#define REQ_LEN 3 - -#define PWVERSION 0 -#define PWFOUND 1 -#define PWNAMELEN 2 -#define PWPASSWDLEN 3 -#define PWUID 4 -#define PWGID 5 -#define PWGECOSLEN 6 -#define PWDIRLEN 7 -#define PWSHELLLEN 8 -#define PW_LEN 9 - -#define GRVERSION 0 -#define GRFOUND 1 -#define GRNAMELEN 2 -#define GRPASSWDLEN 3 -#define GRGID 4 -#define GRMEMCNT 5 -#define GR_LEN 6 - -#define INITGRVERSION 0 -#define INITGRFOUND 1 -#define INITGRNGRPS 2 -#define INITGR_LEN 3 - -hidden FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *swap); - -#endif diff --git a/src/passwd/nscd_query.c b/src/passwd/nscd_query.c deleted file mode 100644 index dc3406b8..00000000 --- a/src/passwd/nscd_query.c +++ /dev/null @@ -1,115 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include "nscd.h" - -static const struct { - short sun_family; - char sun_path[21]; -} addr = { - AF_UNIX, - "/var/run/nscd/socket" -}; - -FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *swap) -{ - size_t i; - int fd; - FILE *f = 0; - int32_t req_buf[REQ_LEN] = { - NSCDVERSION, - req, - strnlen(key,LOGIN_NAME_MAX)+1 - }; - struct msghdr msg = { - .msg_iov = (struct iovec[]){ - {&req_buf, sizeof(req_buf)}, - {(char*)key, strlen(key)+1} - }, - .msg_iovlen = 2 - }; - int errno_save = errno; - - *swap = 0; -retry: - memset(buf, 0, len); - buf[0] = NSCDVERSION; - - fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); - if (fd < 0) { - if (errno == EAFNOSUPPORT) { - f = fopen("/dev/null", "re"); - if (f) - errno = errno_save; - return f; - } - return 0; - } - - if(!(f = fdopen(fd, "r"))) { - close(fd); - return 0; - } - - if (req_buf[2] > LOGIN_NAME_MAX) - return f; - - if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { - /* If there isn't a running nscd we simulate a "not found" - * result and the caller is responsible for calling - * fclose on the (unconnected) socket. The value of - * errno must be left unchanged in this case. */ - if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) { - errno = errno_save; - return f; - } - goto error; - } - - if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0) - goto error; - - if (!fread(buf, len, 1, f)) { - /* If the VERSION entry mismatches nscd will disconnect. The - * most likely cause is that the endianness mismatched. So, we - * byteswap and try once more. (if we already swapped, just - * fail out) - */ - if (ferror(f)) goto error; - if (!*swap) { - fclose(f); - for (i = 0; i < sizeof(req_buf)/sizeof(req_buf[0]); i++) { - req_buf[i] = bswap_32(req_buf[i]); - } - *swap = 1; - goto retry; - } else { - errno = EIO; - goto error; - } - } - - if (*swap) { - for (i = 0; i < len/sizeof(buf[0]); i++) { - buf[i] = bswap_32(buf[i]); - } - } - - /* The first entry in every nscd response is the version number. This - * really shouldn't happen, and is evidence of some form of malformed - * response. - */ - if(buf[0] != NSCDVERSION) { - errno = EIO; - goto error; - } - - return f; -error: - fclose(f); - return 0; -} diff --git a/src/passwd/pwf.h b/src/passwd/pwf.h index 95bb6e05..16fca371 100644 --- a/src/passwd/pwf.h +++ b/src/passwd/pwf.h @@ -9,7 +9,5 @@ #include hidden int __getpwent_a(FILE *f, struct passwd *pw, char **line, size_t *size, struct passwd **res); -hidden int __getpw_a(const char *name, uid_t uid, struct passwd *pw, char **buf, size_t *size, struct passwd **res); hidden int __getgrent_a(FILE *f, struct group *gr, char **line, size_t *size, char ***mem, size_t *nmem, struct group **res); -hidden int __getgr_a(const char *name, gid_t gid, struct group *gr, char **buf, size_t *size, char ***mem, size_t *nmem, struct group **res); hidden int __parsespent(char *s, struct spwd *sp);