1 #include <sys/socket.h>
15 "/var/run/nscd/socket"
18 FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *swap)
23 int32_t req_buf[REQ_LEN] = {
26 strnlen(key,LOGIN_NAME_MAX)+1
29 .msg_iov = (struct iovec[]){
30 {&req_buf, sizeof(req_buf)},
31 {(char*)key, strlen(key)+1}
41 fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
42 if (fd < 0) return NULL;
44 if(!(f = fdopen(fd, "r"))) {
49 if (req_buf[2] > LOGIN_NAME_MAX)
52 if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
53 /* If there isn't a running nscd we return -1 to indicate that
54 * that is precisely what happened
56 if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT)
61 if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0)
64 if (!fread(buf, len, 1, f)) {
65 /* If the VERSION entry mismatches nscd will disconnect. The
66 * most likely cause is that the endianness mismatched. So, we
67 * byteswap and try once more. (if we already swapped, just
70 if (ferror(f)) goto error;
73 for (i = 0; i < sizeof(req_buf)/sizeof(req_buf[0]); i++) {
74 req_buf[i] = bswap_32(req_buf[i]);
85 for (i = 0; i < len/sizeof(buf[0]); i++) {
86 buf[i] = bswap_32(buf[i]);
90 /* The first entry in every nscd response is the version number. This
91 * really shouldn't happen, and is evidence of some form of malformed
94 if(buf[0] != NSCDVERSION) {