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