]> jfr.im git - solanum.git/blob - authd/dns.c
authd/dns: remove magic number
[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 "notice.h"
24 #include "res.h"
25
26 static void handle_lookup_ip_reply(void *data, struct DNSReply *reply);
27 static void handle_lookup_hostname_reply(void *data, struct DNSReply *reply);
28
29 uint64_t query_count = 0;
30
31 /* A bit different from ircd... you just get a dns_query object.
32 *
33 * It gets freed whenever the res code gets back to us.
34 */
35 struct dns_query *
36 lookup_ip(const char *host, int aftype, DNSCB callback, void *data)
37 {
38 struct dns_query *query = rb_malloc(sizeof(struct dns_query));
39 int g_type;
40
41 if(aftype == AF_INET)
42 {
43 query->type = QUERY_A;
44 g_type = T_A;
45 }
46 #ifdef RB_IPV6
47 else if(aftype == AF_INET6)
48 {
49 query->type = QUERY_AAAA;
50 g_type = T_AAAA;
51 }
52 #endif
53 else
54 {
55 rb_free(query);
56 return NULL;
57 }
58
59 query->id = query_count++;
60 query->callback = callback;
61 query->data = data;
62
63 query->query.ptr = query;
64 query->query.callback = handle_lookup_ip_reply;
65
66 gethost_byname_type(host, &query->query, g_type);
67
68 return query;
69 }
70
71 /* See lookup_ip's comment */
72 struct dns_query *
73 lookup_hostname(const char *ip, DNSCB callback, void *data)
74 {
75 struct dns_query *query = rb_malloc(sizeof(struct dns_query));
76 int aftype;
77
78 if(!rb_inet_pton_sock(ip, (struct sockaddr *)&query->addr))
79 {
80 rb_free(query);
81 return NULL;
82 }
83
84 aftype = GET_SS_FAMILY(&query->addr);
85
86 if(aftype == AF_INET)
87 query->type = QUERY_PTR_A;
88 #ifdef RB_IPV6
89 else if(aftype == AF_INET6)
90 query->type = QUERY_PTR_AAAA;
91 #endif
92 else
93 {
94 rb_free(query);
95 return NULL;
96 }
97
98 query->id = query_count++;
99 query->callback = callback;
100 query->data = data;
101
102 query->query.ptr = query;
103 query->query.callback = handle_lookup_hostname_reply;
104
105 gethost_byaddr(&query->addr, &query->query);
106
107 return query;
108 }
109
110 /* Cancel a pending query */
111 void
112 cancel_query(struct dns_query *query)
113 {
114 query->callback = query->data = NULL;
115 }
116
117 /* Callback from gethost_byname_type */
118 static void
119 handle_lookup_ip_reply(void *data, struct DNSReply *reply)
120 {
121 struct dns_query *query = data;
122 char ip[HOSTIPLEN] = "*";
123
124 if(query == NULL)
125 /* Shouldn't happen */
126 exit(2);
127
128 if(reply == NULL)
129 goto end;
130
131 switch(query->type)
132 {
133 case QUERY_A:
134 if(GET_SS_FAMILY(&reply->addr) == AF_INET)
135 rb_inet_ntop_sock((struct sockaddr *)&reply->addr, ip, sizeof(ip));
136 break;
137 #ifdef RB_IPV6
138 case QUERY_AAAA:
139 if(GET_SS_FAMILY(&reply->addr) == AF_INET6)
140 {
141 rb_inet_ntop_sock((struct sockaddr *)&reply->addr, ip, sizeof(ip));
142 if(ip[0] == ':')
143 {
144 memmove(&ip[1], ip, strlen(ip));
145 ip[0] = '0';
146 }
147 }
148 break;
149 #endif
150 default:
151 exit(3);
152 }
153
154 end:
155 if(query->callback)
156 query->callback(ip, ip[0] != '*', query->type, query->data);
157
158 rb_free(query);
159 }
160
161 /* Callback from gethost_byaddr */
162 static void
163 handle_lookup_hostname_reply(void *data, struct DNSReply *reply)
164 {
165 struct dns_query *query = data;
166 char *hostname = NULL;
167
168 if(query == NULL)
169 /* Shouldn't happen */
170 exit(4);
171
172 if(reply == NULL)
173 goto end;
174
175 if(query->type == QUERY_PTR_A)
176 {
177 struct sockaddr_in *ip, *ip_fwd;
178 ip = (struct sockaddr_in *) &query->addr;
179 ip_fwd = (struct sockaddr_in *) &reply->addr;
180
181 if(ip->sin_addr.s_addr == ip_fwd->sin_addr.s_addr)
182 hostname = reply->h_name;
183 }
184 #ifdef RB_IPV6
185 else if(query->type == QUERY_PTR_AAAA)
186 {
187 struct sockaddr_in6 *ip, *ip_fwd;
188 ip = (struct sockaddr_in6 *) &query->addr;
189 ip_fwd = (struct sockaddr_in6 *) &reply->addr;
190
191 if(memcmp(&ip->sin6_addr, &ip_fwd->sin6_addr, sizeof(struct in6_addr)) == 0)
192 hostname = reply->h_name;
193 }
194 #endif
195 else
196 /* Shouldn't happen */
197 exit(5);
198 end:
199 if(query->callback)
200 query->callback(hostname, hostname != NULL, query->type, query->data);
201
202 rb_free(query);
203 }
204
205 static void
206 submit_dns_answer(const char *reply, bool status, query_type type, void *data)
207 {
208 char *id = data;
209
210 if(!id || type == QUERY_INVALID)
211 exit(6);
212
213 if(reply == NULL || status == false)
214 {
215 rb_helper_write(authd_helper, "E %s E %c *", id, type);
216 rb_free(id);
217 return;
218 }
219
220 rb_helper_write(authd_helper, "E %s O %c %s", id, type, reply);
221 rb_free(id);
222 }
223
224 void
225 handle_resolve_dns(int parc, char *parv[])
226 {
227 char *id = rb_strdup(parv[1]);
228 char qtype = *parv[2];
229 char *record = parv[3];
230 int aftype = AF_INET;
231
232 switch(qtype)
233 {
234 #ifdef RB_IPV6
235 case '6':
236 aftype = AF_INET6;
237 #endif
238 case '4':
239 if(!lookup_ip(record, aftype, submit_dns_answer, id))
240 submit_dns_answer(NULL, false, qtype, NULL);
241 break;
242 #ifdef RB_IPV6
243 case 'S':
244 #endif
245 case 'R':
246 if(!lookup_hostname(record, submit_dns_answer, id))
247 submit_dns_answer(NULL, false, qtype, NULL);
248 break;
249 default:
250 exit(7);
251 }
252 }
253
254 void
255 enumerate_nameservers(uint32_t rid, const char letter)
256 {
257 char buf[(HOSTIPLEN + 1) * IRCD_MAXNS];
258 size_t s = 0;
259
260 if (!irc_nscount)
261 {
262 /* Shouldn't happen */
263 stats_error(rid, letter, "NONAMESERVERS");
264 return;
265 }
266
267 for(int i = 0; i < irc_nscount; i++)
268 {
269 char addr[HOSTIPLEN];
270 size_t addrlen;
271
272 rb_inet_ntop_sock((struct sockaddr *)&irc_nsaddr_list[i], addr, sizeof(addr));
273
274 if (!addr[0])
275 {
276 /* Shouldn't happen */
277 stats_error(rid, letter, "INVALIDNAMESERVER");
278 return;
279 }
280
281 addrlen = strlen(addr) + 1;
282 (void)snprintf(&buf[s], sizeof(buf) - s, "%s ", addr);
283 s += addrlen;
284 }
285
286 if(s > 0)
287 buf[--s] = '\0';
288
289 stats_result(rid, letter, "%s", buf);
290 }
291
292 void
293 reload_nameservers(const char letter)
294 {
295 /* Not a whole lot to it */
296 restart_resolver();
297 }