31d65c9afdd1f5576f6edd5ddf318bef9fb4fad1
[citadel.git] / citadel / locate_host.c
1 /*
2  * $Id$
3  *
4  * locate the originating host
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <limits.h>
17 #include <netdb.h>
18 #include <string.h>
19 #include "citadel.h"
20 #include "server.h"
21 #include "serv_extensions.h"
22 #include "locate_host.h"
23 #include "sysdep_decls.h"
24 #include "config.h"
25 #include "tools.h"
26 #include "domain.h"
27
28 void locate_host(char *tbuf, size_t n,
29                 char *abuf, size_t na,
30                 const struct in_addr *addr)
31 {
32         struct hostent *ch;
33         const char *i;
34         char *j;
35         int a1, a2, a3, a4;
36         char address_string[SIZ];
37
38         lprintf(9, "locate_host() called\n");
39
40 #ifdef HAVE_NONREENTRANT_NETDB
41         begin_critical_section(S_NETDB);
42 #endif
43
44         i = (const char *) addr;
45         a1 = ((*i++) & 0xff);
46         a2 = ((*i++) & 0xff);
47         a3 = ((*i++) & 0xff);
48         a4 = ((*i++) & 0xff);
49         sprintf(address_string, "%d.%d.%d.%d", a1, a2, a3, a4);
50
51         if (abuf != NULL) {
52                 safestrncpy(abuf, address_string, na);
53         }
54
55         if ((ch = gethostbyaddr((const char *) addr,
56            sizeof(*addr), AF_INET)) == NULL) {
57 bad_dns:
58                 safestrncpy(tbuf, address_string, n);
59                 goto end;       /* because we might need to end the critical
60                                    section */
61         }
62         /* check if the forward DNS agrees; if not, they're spoofing */
63         j = strdoop(ch->h_name);
64         ch = gethostbyname(j);
65         phree(j);
66         if (ch == NULL)
67                 goto bad_dns;
68
69         /* check address for consistency */
70         for (; *ch->h_addr_list; ch->h_addr_list++)
71                 if (!memcmp(*ch->h_addr_list, addr,
72                             sizeof *addr)) {
73                         safestrncpy(tbuf, ch->h_name, 63);
74                         goto end;
75                 }
76         goto bad_dns;           /* they were spoofing. report a numeric IP
77                                    address. */
78
79       end:
80
81 #ifdef HAVE_NONREENTRANT_NETDB
82         end_critical_section(S_NETDB);
83 #endif
84
85         tbuf[63] = 0;
86         lprintf(9, "locate_host() exiting\n");
87 }
88
89
90 /*
91  * Check to see if a host is on some sort of spam list (RBL)
92  * If spammer, returns nonzero and places reason in 'message_to_spammer'
93  */
94 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
95 {
96         const char *i;
97         int a1, a2, a3, a4;
98         char tbuf[SIZ];
99         int rbl;
100         int num_rbl;
101         char rbl_domains[SIZ];
102
103         strcpy(message_to_spammer, "ok");
104
105         i = (const char *) addr;
106         a1 = ((*i++) & 0xff);
107         a2 = ((*i++) & 0xff);
108         a3 = ((*i++) & 0xff);
109         a4 = ((*i++) & 0xff);
110
111         /* See if we have any RBL domains configured */
112         num_rbl = get_hosts(rbl_domains, "rbl");
113         if (num_rbl < 1) return(0);
114
115         /* Try all configured RBL's */
116         for (rbl=0; rbl<num_rbl; ++rbl) {
117                 snprintf(tbuf, sizeof tbuf,
118                         "%d.%d.%d.%d.",
119                         a4, a3, a2, a1);
120                 extract(&tbuf[strlen(tbuf)], rbl_domains, rbl);
121
122                 if (gethostbyname(tbuf) != NULL) {
123                         strcpy(message_to_spammer,
124                                 "5.7.1 Message rejected due to "
125                                 "known spammer source IP address"
126                         );
127                         return(1);
128                 }
129         }
130
131         return(0);
132 }
133                         
134
135 /*
136  * Check to see if the client host is on some sort of spam list (RBL)
137  * If spammer, returns nonzero and places reason in 'message_to_spammer'
138  */
139 int rbl_check(char *message_to_spammer) {
140         struct sockaddr_in sin;
141         int len;        /* should be socklen_t but doesn't work on Macintosh */
142
143         if (!getpeername(CC->client_socket, (struct sockaddr *) &sin, &len)) {
144                 return(rbl_check_addr(&sin.sin_addr, message_to_spammer));
145         }
146         return(0);
147 }
148
149 /*
150  * Convert a host name to a dotted quad address. 
151  * Returns zero on success or nonzero on failure.
152  */
153 int hostname_to_dotted_quad(char *addr, char *host) {
154         struct hostent *ch;
155         const char *i;
156         int a1, a2, a3, a4;
157
158         ch = gethostbyname(host);
159         if (ch == NULL) {
160                 strcpy(addr, "0.0.0.0");
161                 return(1);
162         }
163
164         i = (const char *) ch->h_addr_list[0];
165         a1 = ((*i++) & 0xff);
166         a2 = ((*i++) & 0xff);
167         a3 = ((*i++) & 0xff);
168         a4 = ((*i++) & 0xff);
169         sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);
170         return(0);
171 }