]> jfr.im git - solanum.git/blame - authd/dns.c
s_auth: remove trailing whitespace at end of usernames.
[solanum.git] / authd / dns.c
CommitLineData
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
25static void handle_lookup_ip_reply(void *data, struct DNSReply *reply);
26static void handle_lookup_hostname_reply(void *data, struct DNSReply *reply);
27
28uint64_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 */
34struct dns_query *
35lookup_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 */
71struct dns_query *
72lookup_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 107void
45ac1e3c
EM
108cancel_query(struct dns_query *query)
109{
110 query->callback = query->data = NULL;
111}
112
399c6333
EM
113/* Callback from gethost_byname_type */
114static void
115handle_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);
e23126c8 124
399c6333
EM
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
153end:
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 */
161static void
162handle_lookup_hostname_reply(void *data, struct DNSReply *reply)
163{
164 struct dns_query *query = data;
165 char *hostname = NULL;
399c6333
EM
166
167 if(!query)
168 /* Shouldn't happen */
169 exit(4);
170
399c6333
EM
171 if(!reply)
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 && strlen(reply->h_name) < 63)
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 && strlen(reply->h_name) < 63)
191 hostname = reply->h_name;
192 }
193#endif
194 else
195 /* Shouldn't happen */
196 exit(5);
197end:
198 if(query->callback)
199 query->callback(hostname, hostname != NULL, query->type, query->data);
200
201 rb_free(query);
202}
203
204static void
205submit_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 || !status)
213 {
214 rb_helper_write(authd_helper, "E %s E %c *", id, type);
215 rb_free(id);
1d9925cf 216 return;
399c6333
EM
217 }
218
219 rb_helper_write(authd_helper, "E %s O %c %s", id, type, reply);
220 rb_free(id);
8cf45447
AC
221}
222
223void
224resolve_dns(int parc, char *parv[])
225{
399c6333
EM
226 char *id = rb_strdup(parv[1]);
227 char qtype = *parv[2];
228 char *record = parv[3];
229 int aftype = AF_INET;
8cf45447 230
399c6333 231 switch(qtype)
8cf45447 232 {
399c6333 233#ifdef RB_IPV6
8cf45447 234 case '6':
399c6333
EM
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);
8cf45447 240 break;
399c6333 241#ifdef RB_IPV6
8cf45447 242 case 'S':
399c6333
EM
243 aftype = AF_INET6;
244#endif
245 case 'R':
246 if(!lookup_hostname(record, aftype, submit_dns_answer, id))
247 submit_dns_answer(NULL, false, qtype, NULL);
8cf45447 248 break;
399c6333
EM
249 default:
250 exit(7);
8cf45447 251 }
8cf45447 252}
394b8dde
EM
253
254void
255enumerate_nameservers(const char *rid, const char letter)
256{
257 char buf[40 * IRCD_MAXNS]; /* Plenty */
258 char *c = buf;
259 int i;
260
261 if (!irc_nscount)
262 {
263 /* Shouldn't happen */
264 rb_helper_write(authd_helper, "X %s %c NONAMESERVERS", rid, letter);
265 return;
266 }
267
268 for(i = 0; i < irc_nscount; i++)
269 {
270 char addr[40];
271 int ret;
272
273 rb_inet_ntop_sock((struct sockaddr *)&irc_nsaddr_list[i], addr, sizeof(addr));
274
275 if (!addr[0])
276 {
277 /* Shouldn't happen */
278 rb_helper_write(authd_helper, "X %s %c INVALIDNAMESERVER", rid, letter);
279 return;
280 }
281
282 ret = snprintf(c, 40, "%s ", addr);
283 c += (size_t)ret;
284 }
285
286 *(--c) = '\0';
287
288 rb_helper_write(authd_helper, "Y %s %c %s", rid, letter, buf);
289}
6445c1cf
EM
290
291void
292reload_nameservers(const char letter)
293{
294 /* Not a whole lot to it */
295 restart_resolver();
296}