summaryrefslogtreecommitdiffstats
path: root/libc-top-half/musl/src/passwd
diff options
context:
space:
mode:
Diffstat (limited to 'libc-top-half/musl/src/passwd')
-rw-r--r--libc-top-half/musl/src/passwd/fgetgrent.c12
-rw-r--r--libc-top-half/musl/src/passwd/fgetpwent.c12
-rw-r--r--libc-top-half/musl/src/passwd/fgetspent.c15
-rw-r--r--libc-top-half/musl/src/passwd/getgr_a.c169
-rw-r--r--libc-top-half/musl/src/passwd/getgr_r.c49
-rw-r--r--libc-top-half/musl/src/passwd/getgrent.c39
-rw-r--r--libc-top-half/musl/src/passwd/getgrent_a.c68
-rw-r--r--libc-top-half/musl/src/passwd/getgrouplist.c81
-rw-r--r--libc-top-half/musl/src/passwd/getpw_a.c142
-rw-r--r--libc-top-half/musl/src/passwd/getpw_r.c42
-rw-r--r--libc-top-half/musl/src/passwd/getpwent.c37
-rw-r--r--libc-top-half/musl/src/passwd/getpwent_a.c54
-rw-r--r--libc-top-half/musl/src/passwd/getspent.c14
-rw-r--r--libc-top-half/musl/src/passwd/getspnam.c18
-rw-r--r--libc-top-half/musl/src/passwd/getspnam_r.c125
-rw-r--r--libc-top-half/musl/src/passwd/lckpwdf.c11
-rw-r--r--libc-top-half/musl/src/passwd/nscd.h44
-rw-r--r--libc-top-half/musl/src/passwd/nscd_query.c115
-rw-r--r--libc-top-half/musl/src/passwd/putgrent.c17
-rw-r--r--libc-top-half/musl/src/passwd/putpwent.c10
-rw-r--r--libc-top-half/musl/src/passwd/putspent.c13
-rw-r--r--libc-top-half/musl/src/passwd/pwf.h15
22 files changed, 1102 insertions, 0 deletions
diff --git a/libc-top-half/musl/src/passwd/fgetgrent.c b/libc-top-half/musl/src/passwd/fgetgrent.c
new file mode 100644
index 0000000..7d045fd
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/fgetgrent.c
@@ -0,0 +1,12 @@
+#define _GNU_SOURCE
+#include "pwf.h"
+
+struct group *fgetgrent(FILE *f)
+{
+ static char *line, **mem;
+ static struct group gr;
+ struct group *res;
+ size_t size=0, nmem=0;
+ __getgrent_a(f, &gr, &line, &size, &mem, &nmem, &res);
+ return res;
+}
diff --git a/libc-top-half/musl/src/passwd/fgetpwent.c b/libc-top-half/musl/src/passwd/fgetpwent.c
new file mode 100644
index 0000000..fd472a0
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/fgetpwent.c
@@ -0,0 +1,12 @@
+#define _GNU_SOURCE
+#include "pwf.h"
+
+struct passwd *fgetpwent(FILE *f)
+{
+ static char *line;
+ static struct passwd pw;
+ size_t size=0;
+ struct passwd *res;
+ __getpwent_a(f, &pw, &line, &size, &res);
+ return res;
+}
diff --git a/libc-top-half/musl/src/passwd/fgetspent.c b/libc-top-half/musl/src/passwd/fgetspent.c
new file mode 100644
index 0000000..47473bd
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/fgetspent.c
@@ -0,0 +1,15 @@
+#include "pwf.h"
+#include <pthread.h>
+
+struct spwd *fgetspent(FILE *f)
+{
+ static char *line;
+ static struct spwd sp;
+ size_t size = 0;
+ struct spwd *res = 0;
+ int cs;
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+ if (getline(&line, &size, f) >= 0 && __parsespent(line, &sp) >= 0) res = &sp;
+ pthread_setcancelstate(cs, 0);
+ return res;
+}
diff --git a/libc-top-half/musl/src/passwd/getgr_a.c b/libc-top-half/musl/src/passwd/getgr_a.c
new file mode 100644
index 0000000..afeb1ec
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/getgr_a.c
@@ -0,0 +1,169 @@
+#include <pthread.h>
+#include <byteswap.h>
+#include <string.h>
+#include <unistd.h>
+#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[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) mem[0][++i] = ptr+1;
+ mem[0][i] = 0;
+
+ if (i != groupbuf[GRMEMCNT]) {
+ rv = EIO;
+ goto cleanup_f;
+ }
+ } else {
+ mem[0][0] = 0;
+ }
+
+ gr->gr_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/libc-top-half/musl/src/passwd/getgr_r.c b/libc-top-half/musl/src/passwd/getgr_r.c
new file mode 100644
index 0000000..f3e8f60
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/getgr_r.c
@@ -0,0 +1,49 @@
+#include "pwf.h"
+#include <pthread.h>
+
+#define FIX(x) (gr->gr_##x = gr->gr_##x-line+buf)
+
+static int getgr_r(const char *name, gid_t gid, struct group *gr, char *buf, size_t size, struct group **res)
+{
+ char *line = 0;
+ size_t len = 0;
+ char **mem = 0;
+ size_t nmem = 0;
+ int rv = 0;
+ size_t i;
+ int cs;
+
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+
+ rv = __getgr_a(name, gid, gr, &line, &len, &mem, &nmem, res);
+ if (*res && size < len + (nmem+1)*sizeof(char *) + 32) {
+ *res = 0;
+ rv = ERANGE;
+ }
+ if (*res) {
+ buf += (16-(uintptr_t)buf)%16;
+ gr->gr_mem = (void *)buf;
+ buf += (nmem+1)*sizeof(char *);
+ memcpy(buf, line, len);
+ FIX(name);
+ FIX(passwd);
+ for (i=0; mem[i]; i++)
+ gr->gr_mem[i] = mem[i]-line+buf;
+ gr->gr_mem[i] = 0;
+ }
+ free(mem);
+ free(line);
+ pthread_setcancelstate(cs, 0);
+ if (rv) errno = rv;
+ return rv;
+}
+
+int getgrnam_r(const char *name, struct group *gr, char *buf, size_t size, struct group **res)
+{
+ return getgr_r(name, 0, gr, buf, size, res);
+}
+
+int getgrgid_r(gid_t gid, struct group *gr, char *buf, size_t size, struct group **res)
+{
+ return getgr_r(0, gid, gr, buf, size, res);
+}
diff --git a/libc-top-half/musl/src/passwd/getgrent.c b/libc-top-half/musl/src/passwd/getgrent.c
new file mode 100644
index 0000000..835b9ab
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/getgrent.c
@@ -0,0 +1,39 @@
+#include "pwf.h"
+
+static FILE *f;
+static char *line, **mem;
+static struct group gr;
+
+void setgrent()
+{
+ if (f) fclose(f);
+ f = 0;
+}
+
+weak_alias(setgrent, endgrent);
+
+struct group *getgrent()
+{
+ struct group *res;
+ size_t size=0, nmem=0;
+ if (!f) f = fopen("/etc/group", "rbe");
+ if (!f) return 0;
+ __getgrent_a(f, &gr, &line, &size, &mem, &nmem, &res);
+ return res;
+}
+
+struct group *getgrgid(gid_t gid)
+{
+ struct group *res;
+ size_t size=0, nmem=0;
+ __getgr_a(0, gid, &gr, &line, &size, &mem, &nmem, &res);
+ return res;
+}
+
+struct group *getgrnam(const char *name)
+{
+ struct group *res;
+ size_t size=0, nmem=0;
+ __getgr_a(name, 0, &gr, &line, &size, &mem, &nmem, &res);
+ return res;
+}
diff --git a/libc-top-half/musl/src/passwd/getgrent_a.c b/libc-top-half/musl/src/passwd/getgrent_a.c
new file mode 100644
index 0000000..7fc389d
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/getgrent_a.c
@@ -0,0 +1,68 @@
+#include "pwf.h"
+#include <pthread.h>
+
+static unsigned atou(char **s)
+{
+ unsigned x;
+ for (x=0; **s-'0'<10U; ++*s) x=10*x+(**s-'0');
+ return x;
+}
+
+int __getgrent_a(FILE *f, struct group *gr, char **line, size_t *size, char ***mem, size_t *nmem, struct group **res)
+{
+ ssize_t l;
+ char *s, *mems;
+ size_t i;
+ int rv = 0;
+ int cs;
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+ for (;;) {
+ if ((l=getline(line, size, f)) < 0) {
+ rv = ferror(f) ? errno : 0;
+ free(*line);
+ *line = 0;
+ gr = 0;
+ goto end;
+ }
+ line[0][l-1] = 0;
+
+ s = line[0];
+ gr->gr_name = s++;
+ if (!(s = strchr(s, ':'))) continue;
+
+ *s++ = 0; gr->gr_passwd = s;
+ if (!(s = strchr(s, ':'))) continue;
+
+ *s++ = 0; gr->gr_gid = atou(&s);
+ if (*s != ':') continue;
+
+ *s++ = 0; mems = s;
+ break;
+ }
+
+ for (*nmem=!!*s; *s; s++)
+ if (*s==',') ++*nmem;
+ free(*mem);
+ *mem = calloc(sizeof(char *), *nmem+1);
+ if (!*mem) {
+ rv = errno;
+ free(*line);
+ *line = 0;
+ gr = 0;
+ goto end;
+ }
+ if (*mems) {
+ mem[0][0] = mems;
+ for (s=mems, i=0; *s; s++)
+ if (*s==',') *s++ = 0, mem[0][++i] = s;
+ mem[0][++i] = 0;
+ } else {
+ mem[0][0] = 0;
+ }
+ gr->gr_mem = *mem;
+end:
+ pthread_setcancelstate(cs, 0);
+ *res = gr;
+ if(rv) errno = rv;
+ return rv;
+}
diff --git a/libc-top-half/musl/src/passwd/getgrouplist.c b/libc-top-half/musl/src/passwd/getgrouplist.c
new file mode 100644
index 0000000..301824c
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/getgrouplist.c
@@ -0,0 +1,81 @@
+#define _GNU_SOURCE
+#include "pwf.h"
+#include <grp.h>
+#include <string.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <byteswap.h>
+#include <errno.h>
+#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;
+
+ 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);
+
+ f = fopen("/etc/group", "rbe");
+ if (!f && errno != ENOENT && errno != ENOTDIR)
+ goto cleanup;
+
+ 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];
+ }
+ }
+
+ ret = n > nlim ? -1 : n;
+ *ngroups = n;
+
+cleanup:
+ if (f) fclose(f);
+ free(nscdbuf);
+ free(buf);
+ free(mem);
+ return ret;
+}
diff --git a/libc-top-half/musl/src/passwd/getpw_a.c b/libc-top-half/musl/src/passwd/getpw_a.c
new file mode 100644
index 0000000..15a70c0
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/getpw_a.c
@@ -0,0 +1,142 @@
+#include <pthread.h>
+#include <byteswap.h>
+#include <string.h>
+#include <unistd.h>
+#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/libc-top-half/musl/src/passwd/getpw_r.c b/libc-top-half/musl/src/passwd/getpw_r.c
new file mode 100644
index 0000000..0c87ab0
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/getpw_r.c
@@ -0,0 +1,42 @@
+#include "pwf.h"
+#include <pthread.h>
+
+#define FIX(x) (pw->pw_##x = pw->pw_##x-line+buf)
+
+static int getpw_r(const char *name, uid_t uid, struct passwd *pw, char *buf, size_t size, struct passwd **res)
+{
+ char *line = 0;
+ size_t len = 0;
+ int rv = 0;
+ int cs;
+
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+
+ rv = __getpw_a(name, uid, pw, &line, &len, res);
+ if (*res && size < len) {
+ *res = 0;
+ rv = ERANGE;
+ }
+ if (*res) {
+ memcpy(buf, line, len);
+ FIX(name);
+ FIX(passwd);
+ FIX(gecos);
+ FIX(dir);
+ FIX(shell);
+ }
+ free(line);
+ pthread_setcancelstate(cs, 0);
+ if (rv) errno = rv;
+ return rv;
+}
+
+int getpwnam_r(const char *name, struct passwd *pw, char *buf, size_t size, struct passwd **res)
+{
+ return getpw_r(name, 0, pw, buf, size, res);
+}
+
+int getpwuid_r(uid_t uid, struct passwd *pw, char *buf, size_t size, struct passwd **res)
+{
+ return getpw_r(0, uid, pw, buf, size, res);
+}
diff --git a/libc-top-half/musl/src/passwd/getpwent.c b/libc-top-half/musl/src/passwd/getpwent.c
new file mode 100644
index 0000000..f2bd516
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/getpwent.c
@@ -0,0 +1,37 @@
+#include "pwf.h"
+
+static FILE *f;
+static char *line;
+static struct passwd pw;
+static size_t size;
+
+void setpwent()
+{
+ if (f) fclose(f);
+ f = 0;
+}
+
+weak_alias(setpwent, endpwent);
+
+struct passwd *getpwent()
+{
+ struct passwd *res;
+ if (!f) f = fopen("/etc/passwd", "rbe");
+ if (!f) return 0;
+ __getpwent_a(f, &pw, &line, &size, &res);
+ return res;
+}
+
+struct passwd *getpwuid(uid_t uid)
+{
+ struct passwd *res;
+ __getpw_a(0, uid, &pw, &line, &size, &res);
+ return res;
+}
+
+struct passwd *getpwnam(const char *name)
+{
+ struct passwd *res;
+ __getpw_a(name, 0, &pw, &line, &size, &res);
+ return res;
+}
diff --git a/libc-top-half/musl/src/passwd/getpwent_a.c b/libc-top-half/musl/src/passwd/getpwent_a.c
new file mode 100644
index 0000000..d1b4b53
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/getpwent_a.c
@@ -0,0 +1,54 @@
+#include "pwf.h"
+#include <pthread.h>
+
+static unsigned atou(char **s)
+{
+ unsigned x;
+ for (x=0; **s-'0'<10U; ++*s) x=10*x+(**s-'0');
+ return x;
+}
+
+int __getpwent_a(FILE *f, struct passwd *pw, char **line, size_t *size, struct passwd **res)
+{
+ ssize_t l;
+ char *s;
+ int rv = 0;
+ int cs;
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+ for (;;) {
+ if ((l=getline(line, size, f)) < 0) {
+ rv = ferror(f) ? errno : 0;
+ free(*line);
+ *line = 0;
+ pw = 0;
+ break;
+ }
+ line[0][l-1] = 0;
+
+ s = line[0];
+ pw->pw_name = s++;
+ if (!(s = strchr(s, ':'))) continue;
+
+ *s++ = 0; pw->pw_passwd = s;
+ if (!(s = strchr(s, ':'))) continue;
+
+ *s++ = 0; pw->pw_uid = atou(&s);
+ if (*s != ':') continue;
+
+ *s++ = 0; pw->pw_gid = atou(&s);
+ if (*s != ':') continue;
+
+ *s++ = 0; pw->pw_gecos = s;
+ if (!(s = strchr(s, ':'))) continue;
+
+ *s++ = 0; pw->pw_dir = s;
+ if (!(s = strchr(s, ':'))) continue;
+
+ *s++ = 0; pw->pw_shell = s;
+ break;
+ }
+ pthread_setcancelstate(cs, 0);
+ *res = pw;
+ if (rv) errno = rv;
+ return rv;
+}
diff --git a/libc-top-half/musl/src/passwd/getspent.c b/libc-top-half/musl/src/passwd/getspent.c
new file mode 100644
index 0000000..8574a48
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/getspent.c
@@ -0,0 +1,14 @@
+#include "pwf.h"
+
+void setspent()
+{
+}
+
+void endspent()
+{
+}
+
+struct spwd *getspent()
+{
+ return 0;
+}
diff --git a/libc-top-half/musl/src/passwd/getspnam.c b/libc-top-half/musl/src/passwd/getspnam.c
new file mode 100644
index 0000000..709b526
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/getspnam.c
@@ -0,0 +1,18 @@
+#include "pwf.h"
+
+#define LINE_LIM 256
+
+struct spwd *getspnam(const char *name)
+{
+ static struct spwd sp;
+ static char *line;
+ struct spwd *res;
+ int e;
+ int orig_errno = errno;
+
+ if (!line) line = malloc(LINE_LIM);
+ if (!line) return 0;
+ e = getspnam_r(name, &sp, line, LINE_LIM, &res);
+ errno = e ? e : orig_errno;
+ return res;
+}
diff --git a/libc-top-half/musl/src/passwd/getspnam_r.c b/libc-top-half/musl/src/passwd/getspnam_r.c
new file mode 100644
index 0000000..541e853
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/getspnam_r.c
@@ -0,0 +1,125 @@
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <ctype.h>
+#include <pthread.h>
+#include "pwf.h"
+
+/* This implementation support Openwall-style TCB passwords in place of
+ * traditional shadow, if the appropriate directories and files exist.
+ * Thus, it is careful to avoid following symlinks or blocking on fifos
+ * which a malicious user might create in place of his or her TCB shadow
+ * file. It also avoids any allocation to prevent memory-exhaustion
+ * attacks via huge TCB shadow files. */
+
+static long xatol(char **s)
+{
+ long x;
+ if (**s == ':' || **s == '\n') return -1;
+ for (x=0; **s-'0'<10U; ++*s) x=10*x+(**s-'0');
+ return x;
+}
+
+int __parsespent(char *s, struct spwd *sp)
+{
+ sp->sp_namp = s;
+ if (!(s = strchr(s, ':'))) return -1;
+ *s = 0;
+
+ sp->sp_pwdp = ++s;
+ if (!(s = strchr(s, ':'))) return -1;
+ *s = 0;
+
+ s++; sp->sp_lstchg = xatol(&s);
+ if (*s != ':') return -1;
+
+ s++; sp->sp_min = xatol(&s);
+ if (*s != ':') return -1;
+
+ s++; sp->sp_max = xatol(&s);
+ if (*s != ':') return -1;
+
+ s++; sp->sp_warn = xatol(&s);
+ if (*s != ':') return -1;
+
+ s++; sp->sp_inact = xatol(&s);
+ if (*s != ':') return -1;
+
+ s++; sp->sp_expire = xatol(&s);
+ if (*s != ':') return -1;
+
+ s++; sp->sp_flag = xatol(&s);
+ if (*s != '\n') return -1;
+ return 0;
+}
+
+static void cleanup(void *p)
+{
+ fclose(p);
+}
+
+int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t size, struct spwd **res)
+{
+ char path[20+NAME_MAX];
+ FILE *f = 0;
+ int rv = 0;
+ int fd;
+ size_t k, l = strlen(name);
+ int skip = 0;
+ int cs;
+ int orig_errno = errno;
+
+ *res = 0;
+
+ /* Disallow potentially-malicious user names */
+ if (*name=='.' || strchr(name, '/') || !l)
+ return errno = EINVAL;
+
+ /* Buffer size must at least be able to hold name, plus some.. */
+ if (size < l+100)
+ return errno = ERANGE;
+
+ /* Protect against truncation */
+ if (snprintf(path, sizeof path, "/etc/tcb/%s/shadow", name) >= sizeof path)
+ return errno = EINVAL;
+
+ fd = open(path, O_RDONLY|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC);
+ if (fd >= 0) {
+ struct stat st = { 0 };
+ errno = EINVAL;
+ if (fstat(fd, &st) || !S_ISREG(st.st_mode) || !(f = fdopen(fd, "rb"))) {
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+ close(fd);
+ pthread_setcancelstate(cs, 0);
+ return errno;
+ }
+ } else {
+ if (errno != ENOENT && errno != ENOTDIR)
+ return errno;
+ f = fopen("/etc/shadow", "rbe");
+ if (!f) {
+ if (errno != ENOENT && errno != ENOTDIR)
+ return errno;
+ return 0;
+ }
+ }
+
+ pthread_cleanup_push(cleanup, f);
+ while (fgets(buf, size, f) && (k=strlen(buf))>0) {
+ if (skip || strncmp(name, buf, l) || buf[l]!=':') {
+ skip = buf[k-1] != '\n';
+ continue;
+ }
+ if (buf[k-1] != '\n') {
+ rv = ERANGE;
+ break;
+ }
+
+ if (__parsespent(buf, sp) < 0) continue;
+ *res = sp;
+ break;
+ }
+ pthread_cleanup_pop(1);
+ errno = rv ? rv : orig_errno;
+ return rv;
+}
diff --git a/libc-top-half/musl/src/passwd/lckpwdf.c b/libc-top-half/musl/src/passwd/lckpwdf.c
new file mode 100644
index 0000000..2feda61
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/lckpwdf.c
@@ -0,0 +1,11 @@
+#include <shadow.h>
+
+int lckpwdf()
+{
+ return 0;
+}
+
+int ulckpwdf()
+{
+ return 0;
+}
diff --git a/libc-top-half/musl/src/passwd/nscd.h b/libc-top-half/musl/src/passwd/nscd.h
new file mode 100644
index 0000000..ae5aa8d
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/nscd.h
@@ -0,0 +1,44 @@
+#ifndef NSCD_H
+#define NSCD_H
+
+#include <stdint.h>
+
+#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/libc-top-half/musl/src/passwd/nscd_query.c b/libc-top-half/musl/src/passwd/nscd_query.c
new file mode 100644
index 0000000..dc3406b
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/nscd_query.c
@@ -0,0 +1,115 @@
+#include <sys/socket.h>
+#include <byteswap.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <limits.h>
+#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/libc-top-half/musl/src/passwd/putgrent.c b/libc-top-half/musl/src/passwd/putgrent.c
new file mode 100644
index 0000000..2a8257d
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/putgrent.c
@@ -0,0 +1,17 @@
+#define _GNU_SOURCE
+#include <grp.h>
+#include <stdio.h>
+
+int putgrent(const struct group *gr, FILE *f)
+{
+ int r;
+ size_t i;
+ flockfile(f);
+ if ((r = fprintf(f, "%s:%s:%u:", gr->gr_name, gr->gr_passwd, gr->gr_gid))<0) goto done;
+ if (gr->gr_mem) for (i=0; gr->gr_mem[i]; i++)
+ if ((r = fprintf(f, "%s%s", i?",":"", gr->gr_mem[i]))<0) goto done;
+ r = fputc('\n', f);
+done:
+ funlockfile(f);
+ return r<0 ? -1 : 0;
+}
diff --git a/libc-top-half/musl/src/passwd/putpwent.c b/libc-top-half/musl/src/passwd/putpwent.c
new file mode 100644
index 0000000..312b765
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/putpwent.c
@@ -0,0 +1,10 @@
+#define _GNU_SOURCE
+#include <pwd.h>
+#include <stdio.h>
+
+int putpwent(const struct passwd *pw, FILE *f)
+{
+ return fprintf(f, "%s:%s:%u:%u:%s:%s:%s\n",
+ pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
+ pw->pw_gecos, pw->pw_dir, pw->pw_shell)<0 ? -1 : 0;
+}
diff --git a/libc-top-half/musl/src/passwd/putspent.c b/libc-top-half/musl/src/passwd/putspent.c
new file mode 100644
index 0000000..55c41bb
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/putspent.c
@@ -0,0 +1,13 @@
+#include <shadow.h>
+#include <stdio.h>
+
+#define NUM(n) ((n) == -1 ? 0 : -1), ((n) == -1 ? 0 : (n))
+#define STR(s) ((s) ? (s) : "")
+
+int putspent(const struct spwd *sp, FILE *f)
+{
+ return fprintf(f, "%s:%s:%.*ld:%.*ld:%.*ld:%.*ld:%.*ld:%.*ld:%.*lu\n",
+ STR(sp->sp_namp), STR(sp->sp_pwdp), NUM(sp->sp_lstchg),
+ NUM(sp->sp_min), NUM(sp->sp_max), NUM(sp->sp_warn),
+ NUM(sp->sp_inact), NUM(sp->sp_expire), NUM(sp->sp_flag)) < 0 ? -1 : 0;
+}
diff --git a/libc-top-half/musl/src/passwd/pwf.h b/libc-top-half/musl/src/passwd/pwf.h
new file mode 100644
index 0000000..95bb6e0
--- /dev/null
+++ b/libc-top-half/musl/src/passwd/pwf.h
@@ -0,0 +1,15 @@
+#include <pwd.h>
+#include <grp.h>
+#include <shadow.h>
+#include <stdio.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+
+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);