c95569dcf7cdb7a2d0adf7cfa08d4b32f16c7482
[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, const struct in_addr *addr)
29 {
30         struct hostent *ch;
31         const char *i;
32         char *j;
33         int a1, a2, a3, a4;
34
35         lprintf(9, "locate_host() called\n");
36
37 #ifdef HAVE_NONREENTRANT_NETDB
38         begin_critical_section(S_NETDB);
39 #endif
40
41         if ((ch = gethostbyaddr((const char *) addr, sizeof(*addr), AF_INET)) ==
42             NULL) {
43               bad_dns:
44                 i = (const char *) addr;
45                 a1 = ((*i++) & 0xff);
46                 a2 = ((*i++) & 0xff);
47                 a3 = ((*i++) & 0xff);
48                 a4 = ((*i++) & 0xff);
49                 snprintf(tbuf, n, "%d.%d.%d.%d", a1, a2, a3, a4);
50                 goto end;       /* because we might need to end the critical
51                                    section */
52         }
53         /* check if the forward DNS agrees; if not, they're spoofing */
54         j = strdoop(ch->h_name);
55         ch = gethostbyname(j);
56         phree(j);
57         if (ch == NULL)
58                 goto bad_dns;
59
60         /* check address for consistency */
61         for (; *ch->h_addr_list; ch->h_addr_list++)
62                 if (!memcmp(*ch->h_addr_list, addr,
63                             sizeof *addr)) {
64                         safestrncpy(tbuf, ch->h_name, 63);
65                         goto end;
66                 }
67         goto bad_dns;           /* they were spoofing. report a numeric IP
68                                    address. */
69
70       end:
71
72 #ifdef HAVE_NONREENTRANT_NETDB
73         end_critical_section(S_NETDB);
74 #endif
75
76         tbuf[63] = 0;
77         lprintf(9, "locate_host() exiting\n");
78 }
79
80
81 /*
82  * Check to see if a host is on some sort of spam list (RBL)
83  * If spammer, returns nonzero and places reason in 'message_to_spammer'
84  */
85 int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
86 {
87         const char *i;
88         int a1, a2, a3, a4;
89         char tbuf[SIZ];
90         int rbl;
91         int num_rbl;
92         char rbl_domains[SIZ];
93
94         strcpy(message_to_spammer, "ok");
95
96         i = (const char *) addr;
97         a1 = ((*i++) & 0xff);
98         a2 = ((*i++) & 0xff);
99         a3 = ((*i++) & 0xff);
100         a4 = ((*i++) & 0xff);
101
102         /* See if we have any RBL domains configured */
103         num_rbl = get_hosts(rbl_domains, "rbl");
104         if (num_rbl < 1) return(0);
105
106         /* Try all configured RBL's */
107         for (rbl=0; rbl<num_rbl; ++rbl) {
108                 snprintf(tbuf, sizeof tbuf,
109                         "%d.%d.%d.%d.",
110                         a4, a3, a2, a1);
111                 extract(&tbuf[strlen(tbuf)], rbl_domains, rbl);
112
113                 if (gethostbyname(tbuf) != NULL) {
114                         strcpy(message_to_spammer,
115                                 "5.7.1 Message rejected due to "
116                                 "known spammer source IP address"
117                         );
118                         return(1);
119                 }
120         }
121
122         return(0);
123 }
124                         
125
126 /*
127  * Check to see if the client host is on some sort of spam list (RBL)
128  * If spammer, returns nonzero and places reason in 'message_to_spammer'
129  */
130 int rbl_check(char *message_to_spammer) {
131         struct sockaddr_in sin;
132         int len;        /* should be socklen_t but doesn't work on Macintosh */
133
134         if (!getpeername(CC->client_socket, (struct sockaddr *) &sin, &len)) {
135                 return(rbl_check_addr(&sin.sin_addr, message_to_spammer));
136         }
137         return(0);
138 }