fix stack protector crashes on x32 & powerpc due to misplaced TLS canary
[musl] / src / passwd / nscd_query.c
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
36         *swap = 0;
37 retry:
38         memset(buf, 0, len);
39         buf[0] = NSCDVERSION;
40
41         fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
42         if (fd < 0) return NULL;
43
44         if(!(f = fdopen(fd, "r"))) {
45                 close(fd);
46                 return 0;
47         }
48
49         if (req_buf[2] > LOGIN_NAME_MAX)
50                 return f;
51
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
55                  */
56                 if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT)
57                         return f;
58                 goto error;
59         }
60
61         if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0)
62                 goto error;
63
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
68                  * fail out)
69                  */
70                 if (ferror(f)) goto error;
71                 if (!*swap) {
72                         fclose(f);
73                         for (i = 0; i < sizeof(req_buf)/sizeof(req_buf[0]); i++) {
74                                 req_buf[i] = bswap_32(req_buf[i]);
75                         }
76                         *swap = 1;
77                         goto retry;
78                 } else {
79                         errno = EIO;
80                         goto error;
81                 }
82         }
83
84         if (*swap) {
85                 for (i = 0; i < len/sizeof(buf[0]); i++) {
86                         buf[i] = bswap_32(buf[i]);
87                 }
88         }
89
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
92          * response.
93          */
94         if(buf[0] != NSCDVERSION) {
95                 errno = EIO;
96                 goto error;
97         }
98
99         return f;
100 error:
101         fclose(f);
102         return 0;
103 }