e945ccf9f785da5f30de87acbc23942e8a31cd49
[citadel.git] / citadel / modules / c-ares-dns / serv_c-ares-dns.c
1 /*
2  * Copyright (c) 1998-2009 by the citadel.org team
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Inspired by NodeJS.org; thanks for the MX-Parser ;-)
19  */
20
21 #include "sysdep.h"
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <termios.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <pwd.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <syslog.h>
32
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
35 # include <time.h>
36 #else
37 # if HAVE_SYS_TIME_H
38 #  include <sys/time.h>
39 # else
40 #  include <time.h>
41 # endif
42 #endif
43 #include <sys/wait.h>
44 #include <ctype.h>
45 #include <string.h>
46 #include <limits.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50
51 #include <libcitadel.h>
52 #include "citadel.h"
53 #include "server.h"
54 #include "citserver.h"
55 #include "support.h"
56
57 #include "ctdl_module.h"
58 #include "event_client.h"
59
60
61 extern struct ev_loop *event_base;
62
63 void SockStateCb(void *data, int sock, int read, int write);
64
65
66 static void HostByAddrCb(void *data,
67                          int status,
68                          int timeouts,
69                          struct hostent *hostent)
70 {
71         AsyncIO *IO = data;
72 #ifdef DEBUG_CARES
73         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
74         EV_DNS_LOGT_STOP(DNS.timeout);
75 #endif
76         ev_timer_stop (event_base, &IO->DNS.timeout);
77
78         IO->DNS.Query->DNSStatus = status;
79         if  (status != ARES_SUCCESS) {
80                 StrBufPlain(IO->ErrMsg, ares_strerror(status), -1);
81                 return;
82         }
83         IO->DNS.Query->Data = hostent;
84 }
85
86 static void ParseAnswerA(AsyncIO *IO, unsigned char* abuf, int alen)
87 {
88         struct hostent* host;
89 #ifdef DEBUG_CARES
90         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
91 #endif
92
93         if (IO->DNS.Query->VParsedDNSReply != NULL)
94                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
95         IO->DNS.Query->VParsedDNSReply = NULL;
96
97         IO->DNS.Query->DNSStatus = ares_parse_a_reply(abuf,
98                                                       alen,
99                                                       &host,
100                                                       NULL,
101                                                       NULL);
102         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
103                 StrBufPlain(IO->ErrMsg,
104                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
105                 return;
106         }
107         IO->DNS.Query->VParsedDNSReply = host;
108         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
109 }
110
111
112 static void ParseAnswerAAAA(AsyncIO *IO, unsigned char* abuf, int alen)
113 {
114         struct hostent* host;
115 #ifdef DEBUG_CARES
116         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
117 #endif
118
119         if (IO->DNS.Query->VParsedDNSReply != NULL)
120                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
121         IO->DNS.Query->VParsedDNSReply = NULL;
122
123         IO->DNS.Query->DNSStatus = ares_parse_aaaa_reply(abuf,
124                                                          alen,
125                                                          &host,
126                                                          NULL,
127                                                          NULL);
128         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
129                 StrBufPlain(IO->ErrMsg,
130                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
131                 return;
132         }
133         IO->DNS.Query->VParsedDNSReply = host;
134         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
135 }
136
137
138 static void ParseAnswerCNAME(AsyncIO *IO, unsigned char* abuf, int alen)
139 {
140         struct hostent* host;
141
142 #ifdef DEBUG_CARES
143         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
144 #endif
145
146         if (IO->DNS.Query->VParsedDNSReply != NULL)
147                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
148         IO->DNS.Query->VParsedDNSReply = NULL;
149
150         IO->DNS.Query->DNSStatus = ares_parse_a_reply(abuf,
151                                                       alen,
152                                                       &host,
153                                                       NULL,
154                                                       NULL);
155         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
156                 StrBufPlain(IO->ErrMsg,
157                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
158                 return;
159         }
160
161         // a CNAME lookup always returns a single record but
162         IO->DNS.Query->VParsedDNSReply = host;
163         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
164 }
165
166
167 static void ParseAnswerMX(AsyncIO *IO, unsigned char* abuf, int alen)
168 {
169         struct ares_mx_reply *mx_out;
170 #ifdef DEBUG_CARES
171         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
172 #endif
173
174         if (IO->DNS.Query->VParsedDNSReply != NULL)
175                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
176         IO->DNS.Query->VParsedDNSReply = NULL;
177
178         IO->DNS.Query->DNSStatus = ares_parse_mx_reply(abuf, alen, &mx_out);
179         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
180                 StrBufPlain(IO->ErrMsg,
181                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
182                 return;
183         }
184
185         IO->DNS.Query->VParsedDNSReply = mx_out;
186         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_data;
187 }
188
189
190 static void ParseAnswerNS(AsyncIO *IO, unsigned char* abuf, int alen)
191 {
192         struct hostent* host;
193 #ifdef DEBUG_CARES
194         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
195 #endif
196
197         if (IO->DNS.Query->VParsedDNSReply != NULL)
198                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
199         IO->DNS.Query->VParsedDNSReply = NULL;
200
201         IO->DNS.Query->DNSStatus = ares_parse_ns_reply(abuf, alen, &host);
202         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
203                 StrBufPlain(IO->ErrMsg,
204                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
205                 return;
206         }
207         IO->DNS.Query->VParsedDNSReply = host;
208         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
209 }
210
211
212 static void ParseAnswerSRV(AsyncIO *IO, unsigned char* abuf, int alen)
213 {
214         struct ares_srv_reply *srv_out;
215 #ifdef DEBUG_CARES
216         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
217 #endif
218
219         if (IO->DNS.Query->VParsedDNSReply != NULL)
220                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
221         IO->DNS.Query->VParsedDNSReply = NULL;
222
223         IO->DNS.Query->DNSStatus = ares_parse_srv_reply(abuf, alen, &srv_out);
224         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
225                 StrBufPlain(IO->ErrMsg,
226                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
227                 return;
228         }
229
230         IO->DNS.Query->VParsedDNSReply = srv_out;
231         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_data;
232 }
233
234
235 static void ParseAnswerTXT(AsyncIO *IO, unsigned char* abuf, int alen)
236 {
237         struct ares_txt_reply *txt_out;
238 #ifdef DEBUG_CARES
239         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
240 #endif
241
242         if (IO->DNS.Query->VParsedDNSReply != NULL)
243                 IO->DNS.Query->DNSReplyFree(IO->DNS.Query->VParsedDNSReply);
244         IO->DNS.Query->VParsedDNSReply = NULL;
245
246         IO->DNS.Query->DNSStatus = ares_parse_txt_reply(abuf, alen, &txt_out);
247         if (IO->DNS.Query->DNSStatus != ARES_SUCCESS) {
248                 StrBufPlain(IO->ErrMsg,
249                             ares_strerror(IO->DNS.Query->DNSStatus), -1);
250                 return;
251         }
252         IO->DNS.Query->VParsedDNSReply = txt_out;
253         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_data;
254 }
255
256 void QueryCb(void *arg,
257              int status,
258              int timeouts,
259              unsigned char* abuf,
260              int alen)
261 {
262         AsyncIO *IO = arg;
263 #ifdef DEBUG_CARES
264         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
265         EV_DNS_LOGT_STOP(DNS.timeout);
266 #endif
267         ev_timer_stop (event_base, &IO->DNS.timeout);
268
269         IO->DNS.Query->DNSStatus = status;
270         if (status == ARES_SUCCESS)
271                 IO->DNS.Query->DNS_CB(arg, abuf, alen);
272         else {
273                 EV_syslog(LOG_DEBUG, "C-ARES: Failed by: %s error %s\n",
274                           __FUNCTION__,
275                           ares_strerror(status));
276                 StrBufPlain(IO->ErrMsg, ares_strerror(status), -1);
277                 IO->DNS.Query->DNSStatus = status;
278         }
279
280         ev_idle_init(&IO->unwind_stack,
281                      IO_postdns_callback);
282         IO->unwind_stack.data = IO;
283         EV_DNS_LOGT_INIT(unwind_stack);
284         EV_DNS_LOGT_START(unwind_stack);
285         ev_idle_start(event_base, &IO->unwind_stack);
286 }
287
288 void QueryCbDone(AsyncIO *IO)
289 {
290 #ifdef DEBUG_CARES
291         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
292         EV_DNS_LOGT_STOP(DNS.timeout);
293 #endif
294
295         ev_idle_stop(event_base, &IO->unwind_stack);
296 }
297
298 void DestructCAres(AsyncIO *IO)
299 {
300         ares_destroy_options(&IO->DNS.Options);
301 }
302
303
304 void InitC_ares_dns(AsyncIO *IO)
305 {
306         int optmask = 0;
307 #ifdef DEBUG_CARES
308         EV_syslog(LOG_DEBUG, "C-ARES: %s %p\n", __FUNCTION__, IO->DNS.Channel);
309 #endif
310
311         if (IO->DNS.Channel == NULL) {
312                 optmask |= ARES_OPT_SOCK_STATE_CB;
313                 IO->DNS.Options.sock_state_cb = SockStateCb;
314                 IO->DNS.Options.sock_state_cb_data = IO;
315                 ares_init_options(&IO->DNS.Channel, &IO->DNS.Options, optmask);
316         }
317         IO->DNS.Query->DNSStatus = 0;
318 }
319
320 static void
321 DNStimeouttrigger_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
322 {
323         AsyncIO *IO = watcher->data;
324         struct timeval tv, MaxTV;
325         struct timeval *NextTV;
326
327         memset(&MaxTV, 0, sizeof(MaxTV));
328         memset(&tv, 0, sizeof(tv));
329         MaxTV.tv_sec = 30;
330         NextTV = ares_timeout(IO->DNS.Channel, &MaxTV, &tv);
331
332         if ((NextTV->tv_sec != MaxTV.tv_sec) ||
333             (NextTV->tv_usec != MaxTV.tv_usec))
334         {
335                 fd_set readers, writers;
336 #ifdef DEBUG_CARES
337                 EV_syslog(LOG_DEBUG, "C-ARES: %s Timeout!\n", __FUNCTION__);
338 #endif
339                 FD_ZERO(&readers);
340                 FD_ZERO(&writers);
341                 ares_fds(IO->DNS.Channel, &readers, &writers);
342                 ares_process(IO->DNS.Channel, &readers, &writers);
343         }
344 }
345
346 void QueueGetHostByNameDone(void *Ctx,
347                             int status,
348                             int timeouts,
349                             struct hostent *hostent)
350 {
351         AsyncIO *IO = (AsyncIO *) Ctx;
352 #ifdef DEBUG_CARES
353         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
354 #endif
355
356         IO->DNS.Query->DNSStatus = status;
357         IO->DNS.Query->VParsedDNSReply = hostent;
358         IO->DNS.Query->DNSReplyFree = (FreeDNSReply) ares_free_hostent;
359
360         ev_idle_init(&IO->unwind_stack,
361                      IO_postdns_callback);
362         IO->unwind_stack.data = IO;
363         EV_DNS_LOGT_INIT(unwind_stack);
364         EV_DNS_LOGT_START(unwind_stack);
365         ev_idle_start(event_base, &IO->unwind_stack);
366 }
367
368 void QueueGetHostByName(AsyncIO *IO,
369                         const char *Hostname,
370                         DNSQueryParts *QueryParts,
371                         IO_CallBack PostDNS)
372 {
373 #ifdef DEBUG_CARES
374         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
375         IO->DNS.SourcePort = 0;
376 #endif
377
378         IO->DNS.Query = QueryParts;
379         IO->DNS.Query->PostDNS = PostDNS;
380
381         InitC_ares_dns(IO);
382
383         ev_timer_init(&IO->DNS.timeout, DNStimeouttrigger_callback, 10, 1);
384         EV_DNS_LOGT_INIT(DNS.timeout);
385         IO->DNS.timeout.data = IO;
386         ares_gethostbyname(IO->DNS.Channel,
387                            Hostname,
388                            AF_INET6, /* it falls back to ipv4 in doubt... */
389                            QueueGetHostByNameDone,
390                            IO);
391         EV_DNS_LOGT_START(DNS.timeout);
392         ev_timer_start(event_base, &IO->DNS.timeout);
393
394 }
395
396 int QueueQuery(ns_type Type,
397                const char *name,
398                AsyncIO *IO,
399                DNSQueryParts *QueryParts,
400                IO_CallBack PostDNS)
401 {
402         int length, family;
403         char address_b[sizeof(struct in6_addr)];
404
405 #ifdef DEBUG_CARES
406         IO->DNS.SourcePort = 0;
407 #endif
408
409         IO->DNS.Query = QueryParts;
410         IO->DNS.Query->PostDNS = PostDNS;
411
412         InitC_ares_dns(IO);
413
414         ev_timer_init(&IO->DNS.timeout, DNStimeouttrigger_callback, 10, 1);
415         IO->DNS.timeout.data = IO;
416         EV_DNS_LOGT_INIT(DNS.timeout);
417
418         switch(Type) {
419         case ns_t_a:
420                 IO->DNS.Query->DNS_CB = ParseAnswerA;
421                 break;
422
423         case ns_t_aaaa:
424                 IO->DNS.Query->DNS_CB = ParseAnswerAAAA;
425                 break;
426
427         case ns_t_mx:
428                 IO->DNS.Query->DNS_CB = ParseAnswerMX;
429                 break;
430
431         case ns_t_ns:
432                 IO->DNS.Query->DNS_CB = ParseAnswerNS;
433                 break;
434
435         case ns_t_txt:
436                 IO->DNS.Query->DNS_CB = ParseAnswerTXT;
437                 break;
438
439         case ns_t_srv:
440                 IO->DNS.Query->DNS_CB = ParseAnswerSRV;
441                 break;
442
443         case ns_t_cname:
444                 IO->DNS.Query->DNS_CB = ParseAnswerCNAME;
445                 break;
446
447         case ns_t_ptr:
448
449
450                 if (inet_pton(AF_INET, name, &address_b) == 1) {
451                         length = sizeof(struct in_addr);
452                         family = AF_INET;
453                 } else if (inet_pton(AF_INET6, name, &address_b) == 1) {
454                         length = sizeof(struct in6_addr);
455                         family = AF_INET6;
456                 } else {
457                         return -1;
458                 }
459
460                 ares_gethostbyaddr(IO->DNS.Channel,
461                                    address_b,
462                                    length,
463                                    family,
464                                    HostByAddrCb,
465                                    IO);
466                 EV_DNS_LOGT_START(DNS.timeout);
467                 ev_timer_start(event_base, &IO->DNS.timeout);
468 #ifdef DEBUG_CARES
469                 EV_syslog(LOG_DEBUG, "C-ARES: %s X1\n", __FUNCTION__);
470 #endif
471                 return 1;
472
473         default:
474 #ifdef DEBUG_CARES
475                 EV_syslog(LOG_DEBUG, "C-ARES: %sX2\n", __FUNCTION__);
476 #endif
477                 return 0;
478         }
479 #ifdef DEBUG_CARES
480         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
481 #endif
482         ares_query(IO->DNS.Channel, name, ns_c_in, Type, QueryCb, IO);
483         EV_DNS_LOGT_START(DNS.timeout);
484         ev_timer_start(event_base, &IO->DNS.timeout);
485         return 1;
486 }
487
488
489
490
491
492 /*****************************************************************************
493  *                      libev / c-ares integration                           *
494  *****************************************************************************/
495 static void DNS_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
496 {
497         AsyncIO *IO = watcher->data;
498
499 #ifdef DEBUG_CARES
500         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
501 #endif
502
503         ares_process_fd(IO->DNS.Channel,
504                         ARES_SOCKET_BAD,
505                         IO->DNS.send_event.fd);
506 }
507 static void DNS_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
508 {
509         AsyncIO *IO = watcher->data;
510
511 #ifdef DEBUG_CARES
512         EV_syslog(LOG_DEBUG, "C-ARES: %s\n", __FUNCTION__);
513 #endif
514
515         ares_process_fd(IO->DNS.Channel,
516                         IO->DNS.recv_event.fd,
517                         ARES_SOCKET_BAD);
518 }
519
520 void SockStateCb(void *data, int sock, int read, int write)
521 {
522         AsyncIO *IO = data;
523 /* already inside of the event queue. */
524 #ifdef DEBUG_CARES
525 {
526         struct sockaddr_in sin = {};
527         socklen_t slen;
528         slen = sizeof(sin);
529         if ((IO->DNS.SourcePort == 0) &&
530             (getsockname(sock, &sin, &slen) == 0))
531         {
532                 IO->DNS.SourcePort = ntohs(sin.sin_port);
533         }
534         EV_syslog(LOG_DEBUG, "C-ARES: %s %d|%d Sock %d port %hu\n",
535                   __FUNCTION__,
536                   read,
537                   write,
538                   sock,
539                   IO->DNS.SourcePort);
540 }
541 #endif
542
543         if (read) {
544                 if ((IO->DNS.recv_event.fd != sock) &&
545                     (IO->DNS.recv_event.fd != 0)) {
546                         EV_DNS_LOG_STOP(DNS.recv_event);
547                         ev_io_stop(event_base, &IO->DNS.recv_event);
548                 }
549                 IO->DNS.recv_event.fd = sock;
550                 ev_io_init(&IO->DNS.recv_event,
551                            DNS_recv_callback,
552                            IO->DNS.recv_event.fd,
553                            EV_READ);
554                 EV_DNS_LOG_INIT(DNS.recv_event);
555                 IO->DNS.recv_event.data = IO;
556                 EV_DNS_LOG_START(DNS.recv_event);
557                 ev_io_start(event_base, &IO->DNS.recv_event);
558         }
559         if (write) {
560                 if ((IO->DNS.send_event.fd != sock) &&
561                     (IO->DNS.send_event.fd != 0)) {
562                         EV_DNS_LOG_STOP(DNS.send_event);
563                         ev_io_stop(event_base, &IO->DNS.send_event);
564                 }
565                 IO->DNS.send_event.fd = sock;
566                 ev_io_init(&IO->DNS.send_event,
567                            DNS_send_callback,
568                            IO->DNS.send_event.fd,
569                            EV_WRITE);
570                 IO->DNS.send_event.data = IO;
571                 EV_DNS_LOG_INIT(DNS.send_event);
572                 EV_DNS_LOG_START(DNS.send_event);
573                 ev_io_start(event_base, &IO->DNS.send_event);
574         }
575         if ((read == 0) && (write == 0)) {
576                 EV_DNS_LOG_STOP(DNS.recv_event);
577                 EV_DNS_LOG_STOP(DNS.send_event);
578                 ev_io_stop(event_base, &IO->DNS.recv_event);
579                 ev_io_stop(event_base, &IO->DNS.send_event);
580         }
581 }
582
583 CTDL_MODULE_INIT(c_ares_client)
584 {
585         if (!threading)
586         {
587                 int r = ares_library_init(ARES_LIB_INIT_ALL);
588                 if (0 != r) {
589                         
590                 }
591         }
592         return "c-ares";
593 }