From 49d1e7f93129cdcc2ab0cc91832b8a29ccd1570d Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sun, 15 Mar 2015 23:33:59 -0400 Subject: 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. --- src/passwd/nscd_query.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'src/passwd/nscd_query.c') diff --git a/src/passwd/nscd_query.c b/src/passwd/nscd_query.c index f8d0fc13..55ccc0a8 100644 --- a/src/passwd/nscd_query.c +++ b/src/passwd/nscd_query.c @@ -32,32 +32,34 @@ FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int * .msg_iovlen = 2 }; - if (strlen(key) > INT32_MAX - 1) { - return (FILE*)-1; - } - *swap = 0; retry: + memset(buf, 0, len); + buf[0] = NSCDVERSION; fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); 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 there isn't a running nscd we return -1 to indicate that * that is precisely what happened */ - if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) { - close(fd); - return (FILE *)-1; - } + if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) + return f; goto error; } if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0) goto error; - if(!(f = fdopen(fd, "r"))) 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 @@ -95,6 +97,6 @@ retry: return f; error: - if (f) fclose(f); else close(fd); + fclose(f); return 0; } -- cgit 1.4.1