limit the configurable default stack/guard size for threads
[musl.git] / src / passwd / nscd_query.c
blobd38e371bcda395855a4e9a125d4a75a115c76416
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"
10 static const struct {
11 short sun_family;
12 char sun_path[21];
13 } addr = {
14 AF_UNIX,
15 "/var/run/nscd/socket"
18 FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *swap)
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
28 struct msghdr msg = {
29 .msg_iov = (struct iovec[]){
30 {&req_buf, sizeof(req_buf)},
31 {(char*)key, strlen(key)+1}
33 .msg_iovlen = 2
35 int errno_save = errno;
37 *swap = 0;
38 retry:
39 memset(buf, 0, len);
40 buf[0] = NSCDVERSION;
42 fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
43 if (fd < 0) return NULL;
45 if(!(f = fdopen(fd, "r"))) {
46 close(fd);
47 return 0;
50 if (req_buf[2] > LOGIN_NAME_MAX)
51 return f;
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;
62 goto error;
65 if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0)
66 goto error;
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)
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]);
80 *swap = 1;
81 goto retry;
82 } else {
83 errno = EIO;
84 goto error;
88 if (*swap) {
89 for (i = 0; i < len/sizeof(buf[0]); i++) {
90 buf[i] = bswap_32(buf[i]);
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.
98 if(buf[0] != NSCDVERSION) {
99 errno = EIO;
100 goto error;
103 return f;
104 error:
105 fclose(f);
106 return 0;