]>
Commit | Line | Data |
---|---|---|
399c6333 | 1 | /* authd/dns.c - authd DNS functions |
8cf45447 AC |
2 | * Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org> |
3 | * | |
4 | * Permission to use, copy, modify, and/or distribute this software for any | |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice is present in all copies. | |
7 | * | |
8 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
9 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
10 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
11 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
12 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
13 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
14 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
15 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
16 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
17 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
18 | * POSSIBILITY OF SUCH DAMAGE. | |
19 | */ | |
20 | ||
21 | #include "authd.h" | |
22 | #include "dns.h" | |
394b8dde | 23 | #include "res.h" |
8cf45447 | 24 | |
399c6333 EM |
25 | static void handle_lookup_ip_reply(void *data, struct DNSReply *reply); |
26 | static void handle_lookup_hostname_reply(void *data, struct DNSReply *reply); | |
27 | ||
28 | uint64_t query_count = 0; | |
29 | ||
30 | /* A bit different from ircd... you just get a dns_query object. | |
31 | * | |
32 | * It gets freed whenever the res code gets back to us. | |
33 | */ | |
34 | struct dns_query * | |
35 | lookup_ip(const char *host, int aftype, DNSCB callback, void *data) | |
8cf45447 | 36 | { |
399c6333 EM |
37 | struct dns_query *query = rb_malloc(sizeof(struct dns_query)); |
38 | int g_type; | |
8cf45447 | 39 | |
399c6333 | 40 | if(aftype == AF_INET) |
8cf45447 | 41 | { |
399c6333 EM |
42 | query->type = QUERY_A; |
43 | g_type = T_A; | |
8cf45447 | 44 | } |
399c6333 EM |
45 | #ifdef RB_IPV6 |
46 | else if(aftype == AF_INET6) | |
47 | { | |
48 | query->type = QUERY_AAAA; | |
49 | g_type = T_AAAA; | |
50 | } | |
51 | #endif | |
52 | else | |
53 | { | |
54 | rb_free(query); | |
55 | return NULL; | |
56 | } | |
57 | ||
58 | query->id = query_count++; | |
59 | query->callback = callback; | |
60 | query->data = data; | |
8cf45447 | 61 | |
399c6333 EM |
62 | query->query.ptr = query; |
63 | query->query.callback = handle_lookup_ip_reply; | |
64 | ||
65 | gethost_byname_type(host, &query->query, g_type); | |
66 | ||
67 | return query; | |
68 | } | |
69 | ||
70 | /* See lookup_ip's comment */ | |
71 | struct dns_query * | |
72 | lookup_hostname(const char *ip, int aftype, DNSCB callback, void *data) | |
73 | { | |
74 | struct dns_query *query = rb_malloc(sizeof(struct dns_query)); | |
75 | ||
76 | if(!rb_inet_pton_sock(ip, (struct sockaddr *)&query->addr)) | |
8cf45447 | 77 | { |
399c6333 EM |
78 | rb_free(query); |
79 | return NULL; | |
80 | } | |
81 | ||
82 | if(aftype == AF_INET) | |
83 | query->type = QUERY_PTR_A; | |
8cf45447 | 84 | #ifdef RB_IPV6 |
399c6333 EM |
85 | else if(aftype == AF_INET6) |
86 | query->type = QUERY_PTR_AAAA; | |
87 | #endif | |
88 | else | |
89 | { | |
90 | rb_free(query); | |
91 | return NULL; | |
92 | } | |
8cf45447 | 93 | |
399c6333 EM |
94 | query->id = query_count++; |
95 | query->callback = callback; | |
96 | query->data = data; | |
8cf45447 | 97 | |
399c6333 EM |
98 | query->query.ptr = query; |
99 | query->query.callback = handle_lookup_hostname_reply; | |
8cf45447 | 100 | |
399c6333 EM |
101 | gethost_byaddr(&query->addr, &query->query); |
102 | ||
103 | return query; | |
104 | } | |
105 | ||
45ac1e3c | 106 | /* Cancel a pending query */ |
540676fc | 107 | void |
45ac1e3c EM |
108 | cancel_query(struct dns_query *query) |
109 | { | |
110 | query->callback = query->data = NULL; | |
111 | } | |
112 | ||
399c6333 EM |
113 | /* Callback from gethost_byname_type */ |
114 | static void | |
115 | handle_lookup_ip_reply(void *data, struct DNSReply *reply) | |
116 | { | |
117 | struct dns_query *query = data; | |
118 | char ip[64] = "*"; | |
119 | query_type type = QUERY_INVALID; | |
120 | ||
121 | if(!query) | |
122 | /* Shouldn't happen */ | |
123 | exit(2); | |
124 | ||
125 | type = query->type; | |
126 | ||
127 | if(!reply) | |
128 | goto end; | |
129 | ||
130 | switch(query->type) | |
131 | { | |
132 | case QUERY_A: | |
133 | if(GET_SS_FAMILY(&reply->addr) == AF_INET) | |
134 | rb_inet_ntop_sock((struct sockaddr *)&reply->addr, ip, sizeof(ip)); | |
8cf45447 AC |
135 | break; |
136 | #ifdef RB_IPV6 | |
399c6333 EM |
137 | case QUERY_AAAA: |
138 | if(GET_SS_FAMILY(&reply->addr) == AF_INET6) | |
8cf45447 | 139 | { |
399c6333 EM |
140 | rb_inet_ntop_sock((struct sockaddr *)&reply->addr, ip, sizeof(ip)); |
141 | if(ip[0] == ':') | |
8cf45447 | 142 | { |
399c6333 EM |
143 | memmove(&ip[1], ip, strlen(ip)); |
144 | ip[0] = '0'; | |
8cf45447 AC |
145 | } |
146 | } | |
147 | break; | |
148 | #endif | |
149 | default: | |
399c6333 | 150 | exit(3); |
8cf45447 AC |
151 | } |
152 | ||
399c6333 EM |
153 | end: |
154 | if(query->callback) | |
155 | query->callback(ip, ip[0] != '*', type, query->data); | |
156 | ||
157 | rb_free(query); | |
158 | } | |
159 | ||
160 | /* Callback from gethost_byaddr */ | |
161 | static void | |
162 | handle_lookup_hostname_reply(void *data, struct DNSReply *reply) | |
163 | { | |
164 | struct dns_query *query = data; | |
165 | char *hostname = NULL; | |
166 | query_type type = QUERY_INVALID; | |
167 | ||
168 | if(!query) | |
169 | /* Shouldn't happen */ | |
170 | exit(4); | |
171 | ||
172 | type = query->type; | |
173 | ||
174 | if(!reply) | |
175 | goto end; | |
176 | ||
177 | if(query->type == QUERY_PTR_A) | |
178 | { | |
179 | struct sockaddr_in *ip, *ip_fwd; | |
180 | ip = (struct sockaddr_in *) &query->addr; | |
181 | ip_fwd = (struct sockaddr_in *) &reply->addr; | |
182 | ||
183 | if(ip->sin_addr.s_addr == ip_fwd->sin_addr.s_addr && strlen(reply->h_name) < 63) | |
184 | hostname = reply->h_name; | |
185 | } | |
186 | #ifdef RB_IPV6 | |
187 | else if(query->type == QUERY_PTR_AAAA) | |
188 | { | |
189 | struct sockaddr_in6 *ip, *ip_fwd; | |
190 | ip = (struct sockaddr_in6 *) &query->addr; | |
191 | ip_fwd = (struct sockaddr_in6 *) &reply->addr; | |
192 | ||
193 | if(memcmp(&ip->sin6_addr, &ip_fwd->sin6_addr, sizeof(struct in6_addr)) == 0 && strlen(reply->h_name) < 63) | |
194 | hostname = reply->h_name; | |
195 | } | |
196 | #endif | |
197 | else | |
198 | /* Shouldn't happen */ | |
199 | exit(5); | |
200 | end: | |
201 | if(query->callback) | |
202 | query->callback(hostname, hostname != NULL, query->type, query->data); | |
203 | ||
204 | rb_free(query); | |
205 | } | |
206 | ||
207 | static void | |
208 | submit_dns_answer(const char *reply, bool status, query_type type, void *data) | |
209 | { | |
210 | char *id = data; | |
211 | ||
212 | if(!id || type == QUERY_INVALID) | |
213 | exit(6); | |
214 | ||
215 | if(!reply || !status) | |
216 | { | |
217 | rb_helper_write(authd_helper, "E %s E %c *", id, type); | |
218 | rb_free(id); | |
219 | } | |
220 | ||
221 | rb_helper_write(authd_helper, "E %s O %c %s", id, type, reply); | |
222 | rb_free(id); | |
8cf45447 AC |
223 | } |
224 | ||
225 | void | |
226 | resolve_dns(int parc, char *parv[]) | |
227 | { | |
399c6333 EM |
228 | char *id = rb_strdup(parv[1]); |
229 | char qtype = *parv[2]; | |
230 | char *record = parv[3]; | |
231 | int aftype = AF_INET; | |
8cf45447 | 232 | |
399c6333 | 233 | switch(qtype) |
8cf45447 | 234 | { |
399c6333 | 235 | #ifdef RB_IPV6 |
8cf45447 | 236 | case '6': |
399c6333 EM |
237 | aftype = AF_INET6; |
238 | #endif | |
239 | case '4': | |
240 | if(!lookup_ip(record, aftype, submit_dns_answer, id)) | |
241 | submit_dns_answer(NULL, false, qtype, NULL); | |
8cf45447 | 242 | break; |
399c6333 | 243 | #ifdef RB_IPV6 |
8cf45447 | 244 | case 'S': |
399c6333 EM |
245 | aftype = AF_INET6; |
246 | #endif | |
247 | case 'R': | |
248 | if(!lookup_hostname(record, aftype, submit_dns_answer, id)) | |
249 | submit_dns_answer(NULL, false, qtype, NULL); | |
8cf45447 | 250 | break; |
399c6333 EM |
251 | default: |
252 | exit(7); | |
8cf45447 | 253 | } |
8cf45447 | 254 | } |
394b8dde EM |
255 | |
256 | void | |
257 | enumerate_nameservers(const char *rid, const char letter) | |
258 | { | |
259 | char buf[40 * IRCD_MAXNS]; /* Plenty */ | |
260 | char *c = buf; | |
261 | int i; | |
262 | ||
263 | if (!irc_nscount) | |
264 | { | |
265 | /* Shouldn't happen */ | |
266 | rb_helper_write(authd_helper, "X %s %c NONAMESERVERS", rid, letter); | |
267 | return; | |
268 | } | |
269 | ||
270 | for(i = 0; i < irc_nscount; i++) | |
271 | { | |
272 | char addr[40]; | |
273 | int ret; | |
274 | ||
275 | rb_inet_ntop_sock((struct sockaddr *)&irc_nsaddr_list[i], addr, sizeof(addr)); | |
276 | ||
277 | if (!addr[0]) | |
278 | { | |
279 | /* Shouldn't happen */ | |
280 | rb_helper_write(authd_helper, "X %s %c INVALIDNAMESERVER", rid, letter); | |
281 | return; | |
282 | } | |
283 | ||
284 | ret = snprintf(c, 40, "%s ", addr); | |
285 | c += (size_t)ret; | |
286 | } | |
287 | ||
288 | *(--c) = '\0'; | |
289 | ||
290 | rb_helper_write(authd_helper, "Y %s %c %s", rid, letter, buf); | |
291 | } | |
6445c1cf EM |
292 | |
293 | void | |
294 | reload_nameservers(const char letter) | |
295 | { | |
296 | /* Not a whole lot to it */ | |
297 | restart_resolver(); | |
298 | } |