simplify nscd lookup code for alt passwd/group backends

previously, a sentinel value of (FILE *)-1 was used to inform the
caller of __nscd_query that nscd is not in use. aside from being an
ugly hack, this resulted in duplicate code paths for two logically
equivalent cases: no nscd, and "not found" result from nscd.

now, __nscd_query simply skips closing the socket and returns a valid
FILE pointer when nscd is not in use, and produces a fake "not found"
response header. the caller is then responsible for closing the socket
just like it would do if it had gotten a real "not found" response.
This commit is contained in:
Rich Felker
2015-03-15 23:33:59 -04:00
parent 2894a44b40
commit 49d1e7f931
4 changed files with 15 additions and 15 deletions

View File

@ -63,7 +63,6 @@ int __getgr_a(const char *name, gid_t gid, struct group *gr, char **buf, size_t
f = __nscd_query(req, key, groupbuf, sizeof groupbuf, &swap); f = __nscd_query(req, key, groupbuf, sizeof groupbuf, &swap);
if (!f) { rv = errno; goto done; } if (!f) { rv = errno; goto done; }
if (f == (FILE*)-1) { rv = 0; goto done; }
if (!groupbuf[GRFOUND]) { rv = 0; goto cleanup_f; } if (!groupbuf[GRFOUND]) { rv = 0; goto cleanup_f; }

View File

@ -28,7 +28,7 @@ int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
f = __nscd_query(GETINITGR, user, resp, sizeof resp, &swap); f = __nscd_query(GETINITGR, user, resp, sizeof resp, &swap);
if (!f) goto cleanup; if (!f) goto cleanup;
if (f != (FILE*)-1 && resp[INITGRFOUND]) { if (resp[INITGRFOUND]) {
nscdbuf = calloc(resp[INITGRNGRPS], sizeof(uint32_t)); nscdbuf = calloc(resp[INITGRNGRPS], sizeof(uint32_t));
if (!nscdbuf) goto cleanup; if (!nscdbuf) goto cleanup;
if (!fread(nscdbuf, sizeof(*nscdbuf)*resp[INITGRNGRPS], 1, f)) { if (!fread(nscdbuf, sizeof(*nscdbuf)*resp[INITGRNGRPS], 1, f)) {
@ -40,7 +40,7 @@ int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
nscdbuf[i] = bswap_32(nscdbuf[i]); nscdbuf[i] = bswap_32(nscdbuf[i]);
} }
} }
if (f != (FILE*)-1) fclose(f); fclose(f);
f = fopen("/etc/group", "rbe"); f = fopen("/etc/group", "rbe");
if (!f && errno != ENOENT && errno != ENOTDIR) if (!f && errno != ENOENT && errno != ENOTDIR)

View File

@ -64,7 +64,6 @@ int __getpw_a(const char *name, uid_t uid, struct passwd *pw, char **buf, size_t
f = __nscd_query(req, key, passwdbuf, sizeof passwdbuf, (int[]){0}); f = __nscd_query(req, key, passwdbuf, sizeof passwdbuf, (int[]){0});
if (!f) { rv = errno; goto done; } if (!f) { rv = errno; goto done; }
if (f == (FILE*)-1) { rv = 0; goto done; }
if(!passwdbuf[PWFOUND]) { rv = 0; goto cleanup_f; } if(!passwdbuf[PWFOUND]) { rv = 0; goto cleanup_f; }

View File

@ -32,32 +32,34 @@ FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *
.msg_iovlen = 2 .msg_iovlen = 2
}; };
if (strlen(key) > INT32_MAX - 1) {
return (FILE*)-1;
}
*swap = 0; *swap = 0;
retry: retry:
memset(buf, 0, len);
buf[0] = NSCDVERSION;
fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd < 0) return NULL; if (fd < 0) return NULL;
if(!(f = fdopen(fd, "r"))) {
close(fd);
return 0;
}
if (strlen(key) > INT32_MAX - 1)
return f;
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
/* If there isn't a running nscd we return -1 to indicate that /* If there isn't a running nscd we return -1 to indicate that
* that is precisely what happened * that is precisely what happened
*/ */
if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) { if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT)
close(fd); return f;
return (FILE *)-1;
}
goto error; goto error;
} }
if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0) if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0)
goto error; goto error;
if(!(f = fdopen(fd, "r"))) goto error;
if (!fread(buf, len, 1, f)) { if (!fread(buf, len, 1, f)) {
/* If the VERSION entry mismatches nscd will disconnect. The /* If the VERSION entry mismatches nscd will disconnect. The
* most likely cause is that the endianness mismatched. So, we * most likely cause is that the endianness mismatched. So, we
@ -95,6 +97,6 @@ retry:
return f; return f;
error: error:
if (f) fclose(f); else close(fd); fclose(f);
return 0; return 0;
} }