• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <sys/socket.h>
2 #include <byteswap.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <limits.h>
8 #include "nscd.h"
9 
10 static const struct {
11 	short sun_family;
12 	char sun_path[21];
13 } addr = {
14 	AF_UNIX,
15 	"/var/run/nscd/socket"
16 };
17 
__nscd_query(int32_t req,const char * key,int32_t * buf,size_t len,int * swap)18 FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *swap)
19 {
20 	size_t i;
21 	int fd;
22 	FILE *f = 0;
23 	int32_t req_buf[REQ_LEN] = {
24 		NSCDVERSION,
25 		req,
26 		strnlen(key,LOGIN_NAME_MAX)+1
27 	};
28 	struct msghdr msg = {
29 		.msg_iov = (struct iovec[]){
30 			{&req_buf, sizeof(req_buf)},
31 			{(char*)key, strlen(key)+1}
32 		},
33 		.msg_iovlen = 2
34 	};
35 	int errno_save = errno;
36 
37 	*swap = 0;
38 retry:
39 	memset(buf, 0, len);
40 	buf[0] = NSCDVERSION;
41 
42 	fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
43 	if (fd < 0) return NULL;
44 
45 	if(!(f = fdopen(fd, "r"))) {
46 		close(fd);
47 		return 0;
48 	}
49 
50 	if (req_buf[2] > LOGIN_NAME_MAX)
51 		return f;
52 
53 	if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
54 		/* If there isn't a running nscd we simulate a "not found"
55 		 * result and the caller is responsible for calling
56 		 * fclose on the (unconnected) socket. The value of
57 		 * errno must be left unchanged in this case.  */
58 		if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) {
59 			errno = errno_save;
60 			return f;
61 		}
62 		goto error;
63 	}
64 
65 	if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0)
66 		goto error;
67 
68 	if (!fread(buf, len, 1, f)) {
69 		/* If the VERSION entry mismatches nscd will disconnect. The
70 		 * most likely cause is that the endianness mismatched. So, we
71 		 * byteswap and try once more. (if we already swapped, just
72 		 * fail out)
73 		 */
74 		if (ferror(f)) goto error;
75 		if (!*swap) {
76 			fclose(f);
77 			for (i = 0; i < sizeof(req_buf)/sizeof(req_buf[0]); i++) {
78 				req_buf[i] = bswap_32(req_buf[i]);
79 			}
80 			*swap = 1;
81 			goto retry;
82 		} else {
83 			errno = EIO;
84 			goto error;
85 		}
86 	}
87 
88 	if (*swap) {
89 		for (i = 0; i < len/sizeof(buf[0]); i++) {
90 			buf[i] = bswap_32(buf[i]);
91 		}
92 	}
93 
94 	/* The first entry in every nscd response is the version number. This
95 	 * really shouldn't happen, and is evidence of some form of malformed
96 	 * response.
97 	 */
98 	if(buf[0] != NSCDVERSION) {
99 		errno = EIO;
100 		goto error;
101 	}
102 
103 	return f;
104 error:
105 	fclose(f);
106 	return 0;
107 }
108