source: code/dns.c@ 96a2e31

Last change on this file since 96a2e31 was e68221b, checked in by Florian Obser <florian@…>, 11 years ago

Check that the reverse resolved hostname resolves back to the
connecting IP.

  • Property mode set to 100644
File size: 3.8 KB
Line 
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
35void dns_done_host(struct asr_result *, void *);
36void dns_done_reverse(struct asr_result *, void *);
37int cmp_addr(struct sockaddr *, struct sockaddr *);
38
39extern int dodns;
40
41void
42dns_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 (ar->ar_gai_errno == 0) {
49 if (strncmp(is->hostname, "localhost",
50 sizeof "localhost" - 1) == 0)
51 strlcpy(is->host, "unknown", ICB_MAXHOSTLEN);
52 else if (strlen(is->hostname) < ICB_MAXHOSTLEN) {
53 for (res = ar->ar_addrinfo; res; res = res->ai_next) {
54 if (cmp_addr(res->ai_addr, (struct sockaddr *)
55 &is->ss) == 0) {
56 strlcpy(is->host, is->hostname,
57 ICB_MAXHOSTLEN);
58 found = 1;
59 break;
60 }
61 }
62 if (!found)
63 icbd_log(is, LOG_WARNING, "hostname %s does "
64 "not resolve back to connecting ip %s",
65 is->hostname, is->host);
66 }
67 } else
68 icbd_log(is, LOG_WARNING, "dns resolution failed: %s",
69 gai_strerror(ar->ar_gai_errno));
70
71 if (ar->ar_addrinfo)
72 freeaddrinfo(ar->ar_addrinfo);
73
74 if (ISSETF(is->flags, ICB_SF_PENDINGDROP)) {
75 free(is);
76 return;
77 }
78
79 CLRF(is->flags, ICB_SF_DNSINPROGRESS);
80}
81
82void
83dns_done_reverse(struct asr_result *ar, void *arg)
84{
85 struct icb_session *is = arg;
86 struct asr_query *as;
87 struct addrinfo hints;
88
89 if (ISSETF(is->flags, ICB_SF_PENDINGDROP)) {
90 free(is);
91 return;
92 }
93
94 if (ar->ar_gai_errno == 0) {
95 icbd_log(is, LOG_DEBUG, "reverse dns resolved %s to %s",
96 is->host, is->hostname);
97 /* try to verify that it resolves back */
98 memset(&hints, 0, sizeof(hints));
99 hints.ai_family = PF_UNSPEC;
100 as = getaddrinfo_async(is->hostname, NULL, &hints, NULL);
101 event_asr_run(as, dns_done_host, is);
102 } else {
103 icbd_log(is, LOG_WARNING, "reverse dns resolution failed: %s",
104 gai_strerror(ar->ar_gai_errno));
105 CLRF(is->flags, ICB_SF_DNSINPROGRESS);
106 }
107}
108
109int
110cmp_addr(struct sockaddr *a, struct sockaddr *b)
111{
112 if (a->sa_family != b->sa_family)
113 return (a->sa_family - b->sa_family);
114
115 if (a->sa_family == AF_INET)
116 return (((struct sockaddr_in *)a)->sin_addr.s_addr -
117 ((struct sockaddr_in *)b)->sin_addr.s_addr);
118
119 if (a->sa_family == AF_INET6)
120 return (memcmp(&((struct sockaddr_in6 *)a)->sin6_addr,
121 &((struct sockaddr_in6 *)b)->sin6_addr,
122 sizeof (struct in6_addr)));
123
124 return -1;
125
126}
127
128void
129dns_resolve(struct icb_session *is)
130{
131 struct asr_query *as;
132
133 if (!dodns)
134 return;
135
136 SETF(is->flags, ICB_SF_DNSINPROGRESS);
137
138 if (verbose)
139 icbd_log(is, LOG_DEBUG, "resolving: %s", is->host);
140
141 as = getnameinfo_async((struct sockaddr *)&is->ss,
142 ((struct sockaddr *)&is->ss)->sa_len, is->hostname,
143 sizeof is->hostname, NULL, 0, NI_NOFQDN, NULL);
144 event_asr_run(as, dns_done_reverse, is);
145}
146
Note: See TracBrowser for help on using the repository browser.