source: code/dns.c@ e68221b

Last change on this file since e68221b 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
RevLine 
[cd7b81d]1/*
[626f420]2 * Copyright (c) 2014 Mike Belopuhov
[dab4135]3 * Copyright (c) 2014 Eric Faurot <eric@faurot.net>
[cd7b81d]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
[e87ab6d]18#include <sys/types.h>
[cd7b81d]19#include <sys/socket.h>
20#include <sys/time.h>
21#include <netinet/in.h>
[ee0e95f]22#include <netdb.h>
[cd7b81d]23#include <errno.h>
[e87ab6d]24#include <event.h>
25#include <resolv.h>
[cd7b81d]26#include <stdlib.h>
[b4049f9]27#include <string.h>
[cd7b81d]28#include <syslog.h>
[e87ab6d]29
[ee0e95f]30#include <asr.h>
[cd7b81d]31
32#include "icb.h"
33#include "icbd.h"
34
[9440414]35void dns_done_host(struct asr_result *, void *);
36void dns_done_reverse(struct asr_result *, void *);
[e68221b]37int cmp_addr(struct sockaddr *, struct sockaddr *);
[460786f]38
39extern int dodns;
40
[e87ab6d]41void
[9440414]42dns_done_host(struct asr_result *ar, void *arg)
[cd7b81d]43{
[e87ab6d]44 struct icb_session *is = arg;
[e68221b]45 struct addrinfo *res;
46 int found = 0;
[e87ab6d]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);
[e68221b]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 }
[e87ab6d]67 } else
[e80f9fc]68 icbd_log(is, LOG_WARNING, "dns resolution failed: %s",
[e87ab6d]69 gai_strerror(ar->ar_gai_errno));
[120eedd]70
[e68221b]71 if (ar->ar_addrinfo)
72 freeaddrinfo(ar->ar_addrinfo);
73
[120eedd]74 if (ISSETF(is->flags, ICB_SF_PENDINGDROP)) {
75 free(is);
76 return;
77 }
78
79 CLRF(is->flags, ICB_SF_DNSINPROGRESS);
[cd7b81d]80}
81
82void
[9440414]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
[120eedd]89 if (ISSETF(is->flags, ICB_SF_PENDINGDROP)) {
90 free(is);
91 return;
92 }
93
[9440414]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);
[120eedd]102 } else {
[9440414]103 icbd_log(is, LOG_WARNING, "reverse dns resolution failed: %s",
104 gai_strerror(ar->ar_gai_errno));
[120eedd]105 CLRF(is->flags, ICB_SF_DNSINPROGRESS);
106 }
[9440414]107}
108
[e68221b]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
[9440414]128void
[e68221b]129dns_resolve(struct icb_session *is)
[cd7b81d]130{
[ee0e95f]131 struct asr_query *as;
[cd7b81d]132
[e87ab6d]133 if (!dodns)
[cd7b81d]134 return;
135
[120eedd]136 SETF(is->flags, ICB_SF_DNSINPROGRESS);
137
[cd7b81d]138 if (verbose)
[e80f9fc]139 icbd_log(is, LOG_DEBUG, "resolving: %s", is->host);
[cd7b81d]140
[e68221b]141 as = getnameinfo_async((struct sockaddr *)&is->ss,
142 ((struct sockaddr *)&is->ss)->sa_len, is->hostname,
[e87ab6d]143 sizeof is->hostname, NULL, 0, NI_NOFQDN, NULL);
[9440414]144 event_asr_run(as, dns_done_reverse, is);
[cd7b81d]145}
[9440414]146
Note: See TracBrowser for help on using the repository browser.