fix negative response and non-response handling for dns queries
[musl] / src / network / lookup_name.c
1 #include <sys/socket.h>
2 #include <netinet/in.h>
3 #include <netdb.h>
4 #include <arpa/inet.h>
5 #include <ctype.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <fcntl.h>
9 #include "lookup.h"
10 #include "stdio_impl.h"
11 #include "syscall.h"
12
13 static int is_valid_hostname(const char *host)
14 {
15         const unsigned char *s;
16         if (strnlen(host, 256)-1 > 254 || mbstowcs(0, host, 0) > 255) return 0;
17         for (s=(void *)host; *s>=0x80 || *s=='.' || *s=='-' || isalnum(*s); s++);
18         return !*s;
19 }
20
21 static int name_from_null(struct address buf[static 2], const char *name, int family, int flags)
22 {
23         int cnt = 0;
24         if (name) return 0;
25         if (flags & AI_PASSIVE) {
26                 if (family != AF_INET6)
27                         buf[cnt++] = (struct address){ .family = AF_INET };
28                 if (family != AF_INET)
29                         buf[cnt++] = (struct address){ .family = AF_INET6 };
30         } else {
31                 if (family != AF_INET6)
32                         buf[cnt++] = (struct address){ .family = AF_INET, .addr = { 127,0,0,1 } };
33                 if (family != AF_INET)
34                         buf[cnt++] = (struct address){ .family = AF_INET6, .addr = { [15] = 1 } };
35         }
36         return cnt;
37 }
38
39 int __inet_aton(const char *, struct in_addr *);
40
41 static int name_from_numeric(struct address buf[static 1], const char *name, int family)
42 {
43         struct in_addr a4;
44         struct in6_addr a6;
45         if (family != AF_INET6 && __inet_aton(name, &a4)>0) {
46                 memcpy(&buf[0].addr, &a4, sizeof a4);
47                 buf[0].family = AF_INET;
48                 return 1;
49         }
50         if (family != AF_INET && inet_pton(AF_INET6, name, &a6)>0) {
51                 memcpy(&buf[0].addr, &a6, sizeof a6);
52                 buf[0].family = AF_INET6;
53                 return 1;
54         }
55         return 0;
56 }
57
58 static int name_from_hosts(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
59 {
60         char line[512];
61         size_t l = strlen(name);
62         int cnt = 0;
63         unsigned char _buf[1032];
64         FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
65         if (!f) return 0;
66         while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
67                 char *p, *z;
68
69                 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
70                 for(p=line+1; (p=strstr(p, name)) &&
71                         (!isspace(p[-1]) || !isspace(p[l])); p++);
72                 if (!p) continue;
73
74                 /* Isolate IP address to parse */
75                 for (p=line; *p && !isspace(*p); p++);
76                 *p++ = 0;
77                 if (name_from_numeric(buf+cnt, line, family))
78                         cnt++;
79
80                 /* Extract first name as canonical name */
81                 for (; *p && isspace(*p); p++);
82                 for (z=p; *z && !isspace(*z); z++);
83                 *z = 0;
84                 if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
85         }
86         __fclose_ca(f);
87         return cnt;
88 }
89
90 struct dpc_ctx {
91         struct address *addrs;
92         char *canon;
93         int cnt;
94 };
95
96 int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, int, const void *), void *);
97 int __dn_expand(const unsigned char *, const unsigned char *, const unsigned char *, char *, int);
98 int __res_mkquery(int, const char *, int, int, const unsigned char *, int, const unsigned char*, unsigned char *, int);
99 int __res_msend(int, const unsigned char *const *, const int *, unsigned char *const *, int *, int);
100
101 #define RR_A 1
102 #define RR_CNAME 5
103 #define RR_AAAA 28
104
105 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
106 {
107         char tmp[256];
108         struct dpc_ctx *ctx = c;
109         switch (rr) {
110         case RR_A:
111                 if (len != 4) return -1;
112                 ctx->addrs[ctx->cnt].family = AF_INET;
113                 memcpy(ctx->addrs[ctx->cnt++].addr, data, 4);
114                 break;
115         case RR_AAAA:
116                 if (len != 16) return -1;
117                 ctx->addrs[ctx->cnt].family = AF_INET6;
118                 memcpy(ctx->addrs[ctx->cnt++].addr, data, 16);
119                 break;
120         case RR_CNAME:
121                 if (__dn_expand(packet, (const unsigned char *)packet + 512,
122                     data, tmp, sizeof tmp) > 0 && is_valid_hostname(tmp))
123                         strcpy(ctx->canon, tmp);
124                 break;
125         }
126         return 0;
127 }
128
129 static int name_from_dns(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
130 {
131         unsigned char qbuf[2][280], abuf[2][512];
132         const unsigned char *qp[2] = { qbuf[0], qbuf[1] };
133         unsigned char *ap[2] = { abuf[0], abuf[1] };
134         int qlens[2], alens[2];
135         int i, nq = 0;
136         struct dpc_ctx ctx = { .addrs = buf, .canon = canon };
137
138         if (family != AF_INET6) {
139                 qlens[nq] = __res_mkquery(0, name, 1, RR_A, 0, 0, 0,
140                         qbuf[nq], sizeof *qbuf);
141                 nq++;
142         }
143         if (family != AF_INET) {
144                 qlens[nq] = __res_mkquery(0, name, 1, RR_AAAA, 0, 0, 0,
145                         qbuf[nq], sizeof *qbuf);
146                 nq++;
147         }
148
149         if (__res_msend(nq, qp, qlens, ap, alens, sizeof *abuf) < 0) return EAI_SYSTEM;
150
151         for (i=0; i<nq; i++)
152                 __dns_parse(abuf[i], alens[i], dns_parse_callback, &ctx);
153
154         if (ctx.cnt) return ctx.cnt;
155         if (alens[0] < 4 || (abuf[0][3] & 15) == 2) return EAI_AGAIN;
156         if ((abuf[0][3] & 15) == 3) return EAI_NONAME;
157         return EAI_FAIL;
158 }
159
160 int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags)
161 {
162         int cnt = 0, i, j;
163
164         *canon = 0;
165         if (name) {
166                 size_t l;
167                 if ((l = strnlen(name, 256))-1 > 254)
168                         return EAI_NONAME;
169                 memcpy(canon, name, l+1);
170         }
171
172         /* Procedurally, a request for v6 addresses with the v4-mapped
173          * flag set is like a request for unspecified family, followed
174          * by filtering of the results. */
175         if (flags & AI_V4MAPPED) {
176                 if (family == AF_INET6) family = AF_UNSPEC;
177                 else flags -= AI_V4MAPPED;
178         }
179
180         /* Try each backend until there's at least one result. */
181         cnt = name_from_null(buf, name, family, flags);
182         if (cnt<=0) cnt = name_from_numeric(buf, name, family);
183         if (cnt<=0 && !(flags & AI_NUMERICHOST)) {
184                 cnt = name_from_hosts(buf, canon, name, family);
185                 if (cnt<=0) cnt = name_from_dns(buf, canon, name, family);
186         }
187         if (cnt<=0) return cnt ? cnt : EAI_NONAME;
188
189         /* Filter/transform results for v4-mapped lookup, if requested. */
190         if (flags & AI_V4MAPPED) {
191                 if (!(flags & AI_ALL)) {
192                         /* If any v6 results exist, remove v4 results. */
193                         for (i=0; i<cnt && buf[i].family != AF_INET6; i++);
194                         if (i<cnt) {
195                                 for (j=0; i<cnt; i++) {
196                                         if (buf[i].family == AF_INET6)
197                                                 buf[j++] = buf[i];
198                                 }
199                                 cnt = i = j;
200                         }
201                 }
202                 /* Translate any remaining v4 results to v6 */
203                 for (i=0; i<cnt; i++) {
204                         if (buf[i].family != AF_INET) continue;
205                         memcpy(buf[i].addr+12, buf[i].addr, 4);
206                         memcpy(buf[i].addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
207                         buf[i].scopeid = 0;
208                         buf[i].family = AF_INET6;
209                 }
210         }
211
212         return cnt;
213 }