]> jfr.im git - solanum.git/blob - authd/dns.c
Merge branch 'master' into authd-framework-2
[solanum.git] / authd / dns.c
1 /* authd/dns.c - authd DNS functions
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"
23 #include "res.h"
24
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)
36 {
37 struct dns_query *query = rb_malloc(sizeof(struct dns_query));
38 int g_type;
39
40 if(aftype == AF_INET)
41 {
42 query->type = QUERY_A;
43 g_type = T_A;
44 }
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;
61
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, DNSCB callback, void *data)
73 {
74 struct dns_query *query = rb_malloc(sizeof(struct dns_query));
75 int aftype;
76
77 if(!rb_inet_pton_sock(ip, (struct sockaddr *)&query->addr))
78 {
79 rb_free(query);
80 return NULL;
81 }
82
83 aftype = GET_SS_FAMILY(&query->addr);
84
85 if(aftype == AF_INET)
86 query->type = QUERY_PTR_A;
87 #ifdef RB_IPV6
88 else if(aftype == AF_INET6)
89 query->type = QUERY_PTR_AAAA;
90 #endif
91 else
92 {
93 rb_free(query);
94 return NULL;
95 }
96
97 query->id = query_count++;
98 query->callback = callback;
99 query->data = data;
100
101 query->query.ptr = query;
102 query->query.callback = handle_lookup_hostname_reply;
103
104 gethost_byaddr(&query->addr, &query->query);
105
106 return query;
107 }
108
109 /* Cancel a pending query */
110 void
111 cancel_query(struct dns_query *query)
112 {
113 query->callback = query->data = NULL;
114 }
115
116 /* Callback from gethost_byname_type */
117 static void
118 handle_lookup_ip_reply(void *data, struct DNSReply *reply)
119 {
120 struct dns_query *query = data;
121 char ip[64] = "*";
122
123 if(query == NULL)
124 /* Shouldn't happen */
125 exit(2);
126
127 if(reply == NULL)
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));
135 break;
136 #ifdef RB_IPV6
137 case QUERY_AAAA:
138 if(GET_SS_FAMILY(&reply->addr) == AF_INET6)
139 {
140 rb_inet_ntop_sock((struct sockaddr *)&reply->addr, ip, sizeof(ip));
141 if(ip[0] == ':')
142 {
143 memmove(&ip[1], ip, strlen(ip));
144 ip[0] = '0';
145 }
146 }
147 break;
148 #endif
149 default:
150 exit(3);
151 }
152
153 end:
154 if(query->callback)
155 query->callback(ip, ip[0] != '*', query->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
167 if(query == NULL)
168 /* Shouldn't happen */
169 exit(4);
170
171 if(reply == NULL)
172 goto end;
173
174 if(query->type == QUERY_PTR_A)
175 {
176 struct sockaddr_in *ip, *ip_fwd;
177 ip = (struct sockaddr_in *) &query->addr;
178 ip_fwd = (struct sockaddr_in *) &reply->addr;
179
180 if(ip->sin_addr.s_addr == ip_fwd->sin_addr.s_addr)
181 hostname = reply->h_name;
182 }
183 #ifdef RB_IPV6
184 else if(query->type == QUERY_PTR_AAAA)
185 {
186 struct sockaddr_in6 *ip, *ip_fwd;
187 ip = (struct sockaddr_in6 *) &query->addr;
188 ip_fwd = (struct sockaddr_in6 *) &reply->addr;
189
190 if(memcmp(&ip->sin6_addr, &ip_fwd->sin6_addr, sizeof(struct in6_addr)) == 0)
191 hostname = reply->h_name;
192 }
193 #endif
194 else
195 /* Shouldn't happen */
196 exit(5);
197 end:
198 if(query->callback)
199 query->callback(hostname, hostname != NULL, query->type, query->data);
200
201 rb_free(query);
202 }
203
204 static void
205 submit_dns_answer(const char *reply, bool status, query_type type, void *data)
206 {
207 char *id = data;
208
209 if(!id || type == QUERY_INVALID)
210 exit(6);
211
212 if(reply == NULL || status == false)
213 {
214 rb_helper_write(authd_helper, "E %s E %c *", id, type);
215 rb_free(id);
216 return;
217 }
218
219 rb_helper_write(authd_helper, "E %s O %c %s", id, type, reply);
220 rb_free(id);
221 }
222
223 void
224 resolve_dns(int parc, char *parv[])
225 {
226 char *id = rb_strdup(parv[1]);
227 char qtype = *parv[2];
228 char *record = parv[3];
229 int aftype = AF_INET;
230
231 switch(qtype)
232 {
233 #ifdef RB_IPV6
234 case '6':
235 aftype = AF_INET6;
236 #endif
237 case '4':
238 if(!lookup_ip(record, aftype, submit_dns_answer, id))
239 submit_dns_answer(NULL, false, qtype, NULL);
240 break;
241 #ifdef RB_IPV6
242 case 'S':
243 #endif
244 case 'R':
245 if(!lookup_hostname(record, submit_dns_answer, id))
246 submit_dns_answer(NULL, false, qtype, NULL);
247 break;
248 default:
249 exit(7);
250 }
251 }
252
253 void
254 enumerate_nameservers(const char *rid, const char letter)
255 {
256 char buf[(HOSTIPLEN + 1) * IRCD_MAXNS];
257 char *c = buf;
258 int i;
259
260 if (!irc_nscount)
261 {
262 /* Shouldn't happen */
263 rb_helper_write(authd_helper, "X %s %c NONAMESERVERS", rid, letter);
264 return;
265 }
266
267 for(i = 0; i < irc_nscount; i++)
268 {
269 char addr[HOSTIPLEN];
270
271 rb_inet_ntop_sock((struct sockaddr *)&irc_nsaddr_list[i], addr, sizeof(addr));
272
273 if (!addr[0])
274 {
275 /* Shouldn't happen */
276 rb_helper_write(authd_helper, "X %s %c INVALIDNAMESERVER", rid, letter);
277 return;
278 }
279
280 (void)snprintf(c, HOSTIPLEN + 1, "%s ", addr);
281 c += strlen(addr) + 1;
282 }
283
284 *(--c) = '\0';
285
286 rb_helper_write(authd_helper, "Y %s %c %s", rid, letter, buf);
287 }
288
289 void
290 reload_nameservers(const char letter)
291 {
292 /* Not a whole lot to it */
293 restart_resolver();
294 }