1 | /*
|
---|
2 | * Copyright (c) 2014 Mike Belopuhov
|
---|
3 | * Copyright (c) 2014 Eric Faurot <eric@faurot.net>
|
---|
4 | *
|
---|
5 | * Permission to use, copy, modify, and distribute this software for any
|
---|
6 | * purpose with or without fee is hereby granted, provided that the above
|
---|
7 | * copyright notice and this permission notice appear in all copies.
|
---|
8 | *
|
---|
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
---|
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
---|
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
---|
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
---|
13 | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
|
---|
14 | * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
---|
15 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <sys/types.h>
|
---|
19 | #include <sys/socket.h>
|
---|
20 | #include <sys/time.h>
|
---|
21 | #include <netinet/in.h>
|
---|
22 | #include <netdb.h>
|
---|
23 | #include <errno.h>
|
---|
24 | #include <event.h>
|
---|
25 | #include <resolv.h>
|
---|
26 | #include <stdlib.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include <syslog.h>
|
---|
29 |
|
---|
30 | #include <asr.h>
|
---|
31 |
|
---|
32 | #include "icb.h"
|
---|
33 | #include "icbd.h"
|
---|
34 |
|
---|
35 | void dns_done_host(struct asr_result *, void *);
|
---|
36 | void dns_done_reverse(struct asr_result *, void *);
|
---|
37 | int cmp_addr(struct sockaddr *, struct sockaddr *);
|
---|
38 |
|
---|
39 | extern int dodns;
|
---|
40 |
|
---|
41 | void
|
---|
42 | dns_done_host(struct asr_result *ar, void *arg)
|
---|
43 | {
|
---|
44 | struct icb_session *is = arg;
|
---|
45 | struct addrinfo *res;
|
---|
46 | int found = 0;
|
---|
47 |
|
---|
48 | if (ISSETF(is->flags, ICB_SF_PENDINGDROP)) {
|
---|
49 | if (ar->ar_addrinfo)
|
---|
50 | freeaddrinfo(ar->ar_addrinfo);
|
---|
51 | free(is);
|
---|
52 | return;
|
---|
53 | }
|
---|
54 |
|
---|
55 | if (ar->ar_gai_errno == 0) {
|
---|
56 | if (strncmp(is->hostname, "localhost",
|
---|
57 | sizeof "localhost" - 1) == 0)
|
---|
58 | strlcpy(is->host, "unknown", ICB_MAXHOSTLEN);
|
---|
59 | else if (strlen(is->hostname) < ICB_MAXHOSTLEN) {
|
---|
60 | for (res = ar->ar_addrinfo; res; res = res->ai_next) {
|
---|
61 | if (cmp_addr(res->ai_addr, (struct sockaddr *)
|
---|
62 | &is->ss) == 0) {
|
---|
63 | strlcpy(is->host, is->hostname,
|
---|
64 | ICB_MAXHOSTLEN);
|
---|
65 | found = 1;
|
---|
66 | break;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | if (!found)
|
---|
70 | icbd_log(is, LOG_WARNING, "hostname %s does "
|
---|
71 | "not resolve back to connecting ip %s",
|
---|
72 | is->hostname, is->host);
|
---|
73 | }
|
---|
74 | } else
|
---|
75 | icbd_log(is, LOG_DEBUG, "dns resolution failed: %s",
|
---|
76 | gai_strerror(ar->ar_gai_errno));
|
---|
77 |
|
---|
78 | if (ar->ar_addrinfo)
|
---|
79 | freeaddrinfo(ar->ar_addrinfo);
|
---|
80 |
|
---|
81 | CLRF(is->flags, ICB_SF_DNSINPROGRESS);
|
---|
82 | }
|
---|
83 |
|
---|
84 | void
|
---|
85 | dns_done_reverse(struct asr_result *ar, void *arg)
|
---|
86 | {
|
---|
87 | struct icb_session *is = arg;
|
---|
88 | struct asr_query *as;
|
---|
89 | struct addrinfo hints;
|
---|
90 |
|
---|
91 | if (ISSETF(is->flags, ICB_SF_PENDINGDROP)) {
|
---|
92 | free(is);
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (ar->ar_gai_errno == 0) {
|
---|
97 | icbd_log(is, LOG_DEBUG, "reverse dns resolved %s to %s",
|
---|
98 | is->host, is->hostname);
|
---|
99 | /* try to verify that it resolves back */
|
---|
100 | memset(&hints, 0, sizeof(hints));
|
---|
101 | hints.ai_family = PF_UNSPEC;
|
---|
102 | as = getaddrinfo_async(is->hostname, NULL, &hints, NULL);
|
---|
103 | event_asr_run(as, dns_done_host, is);
|
---|
104 | } else {
|
---|
105 | icbd_log(is, LOG_DEBUG, "reverse dns resolution failed: %s",
|
---|
106 | gai_strerror(ar->ar_gai_errno));
|
---|
107 | CLRF(is->flags, ICB_SF_DNSINPROGRESS);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | int
|
---|
112 | cmp_addr(struct sockaddr *a, struct sockaddr *b)
|
---|
113 | {
|
---|
114 | if (a->sa_family != b->sa_family)
|
---|
115 | return (a->sa_family - b->sa_family);
|
---|
116 |
|
---|
117 | if (a->sa_family == AF_INET)
|
---|
118 | return (((struct sockaddr_in *)a)->sin_addr.s_addr -
|
---|
119 | ((struct sockaddr_in *)b)->sin_addr.s_addr);
|
---|
120 |
|
---|
121 | if (a->sa_family == AF_INET6)
|
---|
122 | return (memcmp(&((struct sockaddr_in6 *)a)->sin6_addr,
|
---|
123 | &((struct sockaddr_in6 *)b)->sin6_addr,
|
---|
124 | sizeof (struct in6_addr)));
|
---|
125 |
|
---|
126 | return -1;
|
---|
127 | }
|
---|
128 |
|
---|
129 | void
|
---|
130 | dns_resolve(struct icb_session *is)
|
---|
131 | {
|
---|
132 | struct asr_query *as;
|
---|
133 |
|
---|
134 | if (!dodns)
|
---|
135 | return;
|
---|
136 |
|
---|
137 | SETF(is->flags, ICB_SF_DNSINPROGRESS);
|
---|
138 |
|
---|
139 | if (verbose)
|
---|
140 | icbd_log(is, LOG_DEBUG, "resolving: %s", is->host);
|
---|
141 |
|
---|
142 | as = getnameinfo_async((struct sockaddr *)&is->ss,
|
---|
143 | ((struct sockaddr *)&is->ss)->sa_len, is->hostname,
|
---|
144 | sizeof is->hostname, NULL, 0, NI_NOFQDN, NULL);
|
---|
145 | event_asr_run(as, dns_done_reverse, is);
|
---|
146 | }
|
---|
147 |
|
---|