fix off-by-one in checking hostname length in new resolver backend
[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 #include "__dns.h"
13
14 static int is_valid_hostname(const char *host)
15 {
16         const unsigned char *s;
17         if (strnlen(host, 256)-1 > 254 || mbstowcs(0, host, 0) > 255) return 0;
18         for (s=(void *)host; *s>=0x80 || *s=='.' || *s=='-' || isalnum(*s); s++);
19         return !*s;
20 }
21
22 static int name_from_null(struct address buf[static 2], const char *name, int family, int flags)
23 {
24         int cnt = 0;
25         if (name) return 0;
26         if (flags & AI_PASSIVE) {
27                 if (family != AF_INET6)
28                         buf[cnt++] = (struct address){ .family = AF_INET };
29                 if (family != AF_INET)
30                         buf[cnt++] = (struct address){ .family = AF_INET6 };
31         } else {
32                 if (family != AF_INET6)
33                         buf[cnt++] = (struct address){ .family = AF_INET, .addr = { 127,0,0,1 } };
34                 if (family != AF_INET)
35                         buf[cnt++] = (struct address){ .family = AF_INET6, .addr = { [15] = 1 } };
36         }
37         return cnt;
38 }
39
40 static int name_from_numeric(struct address buf[static 1], const char *name, int family)
41 {
42         struct in_addr a4;
43         struct in6_addr a6;
44         if (family != AF_INET6 && inet_aton(name, &a4)>0) {
45                 memcpy(&buf[0].addr, &a4, sizeof a4);
46                 buf[0].family = AF_INET;
47                 return 1;
48         }
49         if (family != AF_INET && inet_pton(AF_INET6, name, &a6)>0) {
50                 memcpy(&buf[0].addr, &a6, sizeof a6);
51                 buf[0].family = AF_INET6;
52                 return 1;
53         }
54         return 0;
55 }
56
57 static int name_from_hosts(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
58 {
59         char line[512];
60         size_t l = strlen(name);
61         int cnt = 0;
62         unsigned char _buf[1032];
63         FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
64         if (!f) return 0;
65         while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
66                 char *p, *z;
67
68                 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
69                 for(p=line+1; (p=strstr(p, name)) &&
70                         (!isspace(p[-1]) || !isspace(p[l])); p++);
71                 if (!p) continue;
72
73                 /* Isolate IP address to parse */
74                 for (p=line; *p && !isspace(*p); p++);
75                 *p++ = 0;
76                 if (name_from_numeric(buf+cnt, line, family))
77                         cnt++;
78
79                 /* Extract first name as canonical name */
80                 for (; *p && isspace(*p); p++);
81                 for (z=p; *z && !isspace(*z); z++);
82                 *z = 0;
83                 if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
84         }
85         __fclose_ca(f);
86         return cnt;
87 }
88
89 static int name_from_dns(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
90 {
91         unsigned char reply[1024] = { 0 }, *p = reply;
92         char tmp[256];
93         int i, cnt = 0;
94
95         /* Perform one or more DNS queries for host */
96         int result = __dns_query(reply, name, family, 0);
97         if (result < 0) return result;
98
99         for (i=0; i<result; i++) {
100                 if (family != AF_INET6) {
101                         int j = __dns_get_rr(&buf[cnt].addr, sizeof *buf, 4, MAXADDRS-cnt, p, RR_A, 0);
102                         while (j--) buf[cnt++].family = AF_INET;
103                 }
104                 if (family != AF_INET) {
105                         int j = __dns_get_rr(&buf[cnt].addr, sizeof *buf, 16, MAXADDRS-cnt, p, RR_AAAA, 0);
106                         while (j--) buf[cnt++].family = AF_INET6;
107                 }
108                 p += 512;
109         }
110         __dns_get_rr(tmp, 0, 256, 1, reply, RR_CNAME, 1);
111         if (is_valid_hostname(tmp)) strcpy(canon, tmp);
112         return cnt;
113 }
114
115 int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags)
116 {
117         int cnt = 0, i, j;
118
119         *canon = 0;
120         if (name) {
121                 size_t l;
122                 if ((l = strnlen(name, 256))-1 > 254)
123                         return EAI_NONAME;
124                 memcpy(canon, name, l+1);
125         }
126
127         /* Procedurally, a request for v6 addresses with the v4-mapped
128          * flag set is like a request for unspecified family, followed
129          * by filtering of the results. */
130         if (flags & AI_V4MAPPED) {
131                 if (family == AF_INET6) family = AF_UNSPEC;
132                 else flags -= AI_V4MAPPED;
133         }
134
135         /* Try each backend until there's at least one result. */
136         cnt = name_from_null(buf, name, family, flags);
137         if (cnt<=0) cnt = name_from_numeric(buf, name, family);
138         if (cnt<=0 && !(flags & AI_NUMERICHOST)) {
139                 cnt = name_from_hosts(buf, canon, name, family);
140                 if (cnt<=0) cnt = name_from_dns(buf, canon, name, family);
141         }
142         if (cnt<=0) return cnt ? cnt : EAI_NONAME;
143
144         /* Filter/transform results for v4-mapped lookup, if requested. */
145         if (flags & AI_V4MAPPED) {
146                 if (!(flags & AI_ALL)) {
147                         /* If any v6 results exist, remove v4 results. */
148                         for (i=0; i<cnt && buf[i].family != AF_INET6; i++);
149                         if (i<cnt) {
150                                 for (j=0; i<cnt; i++) {
151                                         if (buf[i].family == AF_INET6)
152                                                 buf[j++] = buf[i];
153                                 }
154                                 cnt = i = j;
155                         }
156                 }
157                 /* Translate any remaining v4 results to v6 */
158                 for (i=0; i<cnt; i++) {
159                         if (buf[i].family != AF_INET) continue;
160                         memcpy(buf[i].addr+12, buf[i].addr, 4);
161                         memcpy(buf[i].addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
162                         buf[i].scopeid = 0;
163                         buf[i].family = AF_INET6;
164                 }
165         }
166
167         return cnt;
168 }