1 /* authd/dns.c - authd DNS functions
2 * Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org>
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.
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.
25 static void handle_lookup_ip_reply(void *data
, struct DNSReply
*reply
);
26 static void handle_lookup_hostname_reply(void *data
, struct DNSReply
*reply
);
28 uint64_t query_count
= 0;
30 /* A bit different from ircd... you just get a dns_query object.
32 * It gets freed whenever the res code gets back to us.
35 lookup_ip(const char *host
, int aftype
, DNSCB callback
, void *data
)
37 struct dns_query
*query
= rb_malloc(sizeof(struct dns_query
));
42 query
->type
= QUERY_A
;
46 else if(aftype
== AF_INET6
)
48 query
->type
= QUERY_AAAA
;
58 query
->id
= query_count
++;
59 query
->callback
= callback
;
62 query
->query
.ptr
= query
;
63 query
->query
.callback
= handle_lookup_ip_reply
;
65 gethost_byname_type(host
, &query
->query
, g_type
);
70 /* See lookup_ip's comment */
72 lookup_hostname(const char *ip
, int aftype
, DNSCB callback
, void *data
)
74 struct dns_query
*query
= rb_malloc(sizeof(struct dns_query
));
76 if(!rb_inet_pton_sock(ip
, (struct sockaddr
*)&query
->addr
))
83 query
->type
= QUERY_PTR_A
;
85 else if(aftype
== AF_INET6
)
86 query
->type
= QUERY_PTR_AAAA
;
94 query
->id
= query_count
++;
95 query
->callback
= callback
;
98 query
->query
.ptr
= query
;
99 query
->query
.callback
= handle_lookup_hostname_reply
;
101 gethost_byaddr(&query
->addr
, &query
->query
);
106 /* Callback from gethost_byname_type */
108 handle_lookup_ip_reply(void *data
, struct DNSReply
*reply
)
110 struct dns_query
*query
= data
;
112 query_type type
= QUERY_INVALID
;
115 /* Shouldn't happen */
126 if(GET_SS_FAMILY(&reply
->addr
) == AF_INET
)
127 rb_inet_ntop_sock((struct sockaddr
*)&reply
->addr
, ip
, sizeof(ip
));
131 if(GET_SS_FAMILY(&reply
->addr
) == AF_INET6
)
133 rb_inet_ntop_sock((struct sockaddr
*)&reply
->addr
, ip
, sizeof(ip
));
136 memmove(&ip
[1], ip
, strlen(ip
));
148 query
->callback(ip
, ip
[0] != '*', type
, query
->data
);
153 /* Callback from gethost_byaddr */
155 handle_lookup_hostname_reply(void *data
, struct DNSReply
*reply
)
157 struct dns_query
*query
= data
;
158 char *hostname
= NULL
;
159 query_type type
= QUERY_INVALID
;
162 /* Shouldn't happen */
170 if(query
->type
== QUERY_PTR_A
)
172 struct sockaddr_in
*ip
, *ip_fwd
;
173 ip
= (struct sockaddr_in
*) &query
->addr
;
174 ip_fwd
= (struct sockaddr_in
*) &reply
->addr
;
176 if(ip
->sin_addr
.s_addr
== ip_fwd
->sin_addr
.s_addr
&& strlen(reply
->h_name
) < 63)
177 hostname
= reply
->h_name
;
180 else if(query
->type
== QUERY_PTR_AAAA
)
182 struct sockaddr_in6
*ip
, *ip_fwd
;
183 ip
= (struct sockaddr_in6
*) &query
->addr
;
184 ip_fwd
= (struct sockaddr_in6
*) &reply
->addr
;
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
;
191 /* Shouldn't happen */
195 query
->callback(hostname
, hostname
!= NULL
, query
->type
, query
->data
);
201 submit_dns_answer(const char *reply
, bool status
, query_type type
, void *data
)
205 if(!id
|| type
== QUERY_INVALID
)
208 if(!reply
|| !status
)
210 rb_helper_write(authd_helper
, "E %s E %c *", id
, type
);
214 rb_helper_write(authd_helper
, "E %s O %c %s", id
, type
, reply
);
219 resolve_dns(int parc
, char *parv
[])
221 char *id
= rb_strdup(parv
[1]);
222 char qtype
= *parv
[2];
223 char *record
= parv
[3];
224 int aftype
= AF_INET
;
233 if(!lookup_ip(record
, aftype
, submit_dns_answer
, id
))
234 submit_dns_answer(NULL
, false, qtype
, NULL
);
241 if(!lookup_hostname(record
, aftype
, submit_dns_answer
, id
))
242 submit_dns_answer(NULL
, false, qtype
, NULL
);
250 enumerate_nameservers(const char *rid
, const char letter
)
252 char buf
[40 * IRCD_MAXNS
]; /* Plenty */
258 /* Shouldn't happen */
259 rb_helper_write(authd_helper
, "X %s %c NONAMESERVERS", rid
, letter
);
263 for(i
= 0; i
< irc_nscount
; i
++)
268 rb_inet_ntop_sock((struct sockaddr
*)&irc_nsaddr_list
[i
], addr
, sizeof(addr
));
272 /* Shouldn't happen */
273 rb_helper_write(authd_helper
, "X %s %c INVALIDNAMESERVER", rid
, letter
);
277 ret
= snprintf(c
, 40, "%s ", addr
);
283 rb_helper_write(authd_helper
, "Y %s %c %s", rid
, letter
, buf
);
287 reload_nameservers(const char letter
)
289 /* Not a whole lot to it */