| 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 | |
| 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) { |
| 44 | 		if (errno == EAFNOSUPPORT) { |
| 45 | 			f = fopen("/dev/null", "re"); |
| 46 | 			if (f) |
| 47 | 				errno = errno_save; |
| 48 | 			return f; |
| 49 | 		} |
| 50 | 		return 0; |
| 51 | 	} |
| 52 | |
| 53 | 	if(!(f = fdopen(fd, "r"))) { |
| 54 | 		close(fd); |
| 55 | 		return 0; |
| 56 | 	} |
| 57 | |
| 58 | 	if (req_buf[2] > LOGIN_NAME_MAX) |
| 59 | 		return f; |
| 60 | |
| 61 | 	if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { |
| 62 | 		/* If there isn't a running nscd we simulate a "not found" |
| 63 | 		 * result and the caller is responsible for calling |
| 64 | 		 * fclose on the (unconnected) socket. The value of |
| 65 | 		 * errno must be left unchanged in this case. */ |
| 66 | 		if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) { |
| 67 | 			errno = errno_save; |
| 68 | 			return f; |
| 69 | 		} |
| 70 | 		goto error; |
| 71 | 	} |
| 72 | |
| 73 | 	if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0) |
| 74 | 		goto error; |
| 75 | |
| 76 | 	if (!fread(buf, len, 1, f)) { |
| 77 | 		/* If the VERSION entry mismatches nscd will disconnect. The |
| 78 | 		 * most likely cause is that the endianness mismatched. So, we |
| 79 | 		 * byteswap and try once more. (if we already swapped, just |
| 80 | 		 * fail out) |
| 81 | 		 */ |
| 82 | 		if (ferror(f)) goto error; |
| 83 | 		if (!*swap) { |
| 84 | 			fclose(f); |
| 85 | 			for (i = 0; i < sizeof(req_buf)/sizeof(req_buf[0]); i++) { |
| 86 | 				req_buf[i] = bswap_32(req_buf[i]); |
| 87 | 			} |
| 88 | 			*swap = 1; |
| 89 | 			goto retry; |
| 90 | 		} else { |
| 91 | 			errno = EIO; |
| 92 | 			goto error; |
| 93 | 		} |
| 94 | 	} |
| 95 | |
| 96 | 	if (*swap) { |
| 97 | 		for (i = 0; i < len/sizeof(buf[0]); i++) { |
| 98 | 			buf[i] = bswap_32(buf[i]); |
| 99 | 		} |
| 100 | 	} |
| 101 | |
| 102 | 	/* The first entry in every nscd response is the version number. This |
| 103 | 	 * really shouldn't happen, and is evidence of some form of malformed |
| 104 | 	 * response. |
| 105 | 	 */ |
| 106 | 	if(buf[0] != NSCDVERSION) { |
| 107 | 		errno = EIO; |
| 108 | 		goto error; |
| 109 | 	} |
| 110 | |
| 111 | 	return f; |
| 112 | error: |
| 113 | 	fclose(f); |
| 114 | 	return 0; |
| 115 | } |