X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fpasswd%2Fgetspnam_r.c;h=f4d7b35e18ce8f1c871051887a9fecc14733006e;hb=1a70198b3e5a7866bf4c62d090d8a8e28b12521a;hp=9e29aa9cf78d782d4c17a93d7e2f9042fba44ab9;hpb=18bca575107d80ca81241d1429ded62918c5bd2e;p=musl diff --git a/src/passwd/getspnam_r.c b/src/passwd/getspnam_r.c index 9e29aa9c..f4d7b35e 100644 --- a/src/passwd/getspnam_r.c +++ b/src/passwd/getspnam_r.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "pwf.h" /* This implementation support Openwall-style TCB passwords in place of @@ -16,6 +17,11 @@ static long xatol(const char *s) return isdigit(*s) ? atol(s) : -1; } +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]; @@ -25,6 +31,7 @@ int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t size, struct size_t k, l = strlen(name); char *s; int skip = 0; + int cs; *res = 0; @@ -39,19 +46,22 @@ int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t size, struct if (snprintf(path, sizeof path, "/etc/tcb/%s/shadow", name) >= sizeof path) return EINVAL; - fd = open(path, O_RDONLY|O_NOFOLLOW|O_NONBLOCK); + 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 { - f = fopen("/etc/shadow", "rb"); + f = fopen("/etc/shadow", "rbe"); if (!f) return errno; } + pthread_cleanup_push(cleanup, f); while (fgets(buf, size, f) && (k=strlen(buf))>0) { if (skip || strncmp(name, buf, l)) { skip = buf[k-1] != '\n'; @@ -92,6 +102,6 @@ int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t size, struct *res = sp; break; } - fclose(f); + pthread_cleanup_pop(1); return rv; }