Begin work on the greylist module.
[apps/pfixtools.git] / greylist.c
1 /******************************************************************************/
2 /*          pfixtools: a collection of postfix related tools                  */
3 /*          ~~~~~~~~~                                                         */
4 /*  ________________________________________________________________________  */
5 /*                                                                            */
6 /*  Redistribution and use in source and binary forms, with or without        */
7 /*  modification, are permitted provided that the following conditions        */
8 /*  are met:                                                                  */
9 /*                                                                            */
10 /*  1. Redistributions of source code must retain the above copyright         */
11 /*     notice, this list of conditions and the following disclaimer.          */
12 /*  2. Redistributions in binary form must reproduce the above copyright      */
13 /*     notice, this list of conditions and the following disclaimer in the    */
14 /*     documentation and/or other materials provided with the distribution.   */
15 /*  3. The names of its contributors may not be used to endorse or promote    */
16 /*     products derived from this software without specific prior written     */
17 /*     permission.                                                            */
18 /*                                                                            */
19 /*  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND   */
20 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE     */
21 /*  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        */
22 /*  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS    */
23 /*  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR    */
24 /*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF      */
25 /*  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  */
26 /*  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN   */
27 /*  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   */
28 /*  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF    */
29 /*  THE POSSIBILITY OF SUCH DAMAGE.                                           */
30 /******************************************************************************/
31
32 /*
33  * Copyright © 2007 Pierre Habouzit
34  */
35
36 #include <tcbdb.h>
37
38 #include "greylist.h"
39 #include "str.h"
40
41 static struct {
42     int do_awl;
43     int awl_count;
44     bool lookup_by_host;
45
46     int delay;
47     int retry_window;
48
49     TCBDB *awl_db, *obj_db;
50 } cfg;
51
52 struct awl_entry {
53     int32_t count;
54     time_t  last;
55 };
56
57 struct obj_entry {
58     time_t first;
59     time_t last;
60 };
61
62 const char *sender_normalize(const char *sender, char *buf, int len)
63 {
64     const char *at = strchr(sender, '@');
65     int rpos = 0, wpos = 0, userlen;
66
67     if (!at)
68         return sender;
69
70     /* strip extension used for VERP or alike */
71     userlen = ((char *)memchr(sender, '+', at - sender) ?: at) - sender;
72
73     while (rpos < userlen) {
74         int count = 0;
75
76         while (isdigit(sender[rpos + count]) && rpos + count < userlen)
77             count++;
78         if (count && !isalnum(sender[rpos + count])) {
79             /* replace \<\d+\> with '#' */
80             wpos += m_strputc(buf + wpos, len - wpos, '#');
81             rpos += count;
82             count = 0;
83         }
84         while (isalnum(sender[rpos + count]) && rpos + count < userlen)
85             count++;
86         while (!isalnum(sender[rpos + count]) && rpos + count < userlen)
87             count++;
88         wpos += m_strncpy(buf + wpos, len - wpos, sender + rpos, count);
89         rpos += count;
90     }
91
92     wpos += m_strputc(buf + wpos, len - wpos, '#');
93     wpos += m_strcpy(buf + wpos, len - wpos, at + 1);
94     return buf;
95 }
96
97 static const char *
98 c_net(const char *c_addr, const char *c_name, char *cnet, int cnetlen)
99 {
100     char ip2[4], ip3[4];
101     const char *dot, *p;
102
103     if (cfg.lookup_by_host)
104         return c_addr;
105
106     if (!(dot = strchr(c_addr, '.')))
107         return c_addr;
108     if (!(dot = strchr(dot + 1, '.')))
109         return c_addr;
110
111     p = ++dot;
112     if (!(dot = strchr(dot, '.')) || dot - p > 3)
113         return c_addr;
114     m_strncpy(ip2, sizeof(ip2), p, dot - p);
115
116     p = ++dot;
117     if (!(dot = strchr(dot, '.')) || dot - p > 3)
118         return c_addr;
119     m_strncpy(ip3, sizeof(ip3), p, dot - p);
120
121     /* skip if contains the last two ip numbers in the hostname,
122        we assume it's a pool of dialup of a provider */
123     if (strstr(c_name, ip2) && strstr(c_name, ip3))
124         return c_addr;
125
126     m_strncpy(cnet, cnetlen, c_addr, dot - c_addr);
127     return cnet;
128 }
129
130 bool try_greylist(const char *sender, const char *c_addr,
131                   const char *c_name, const char *rcpt)
132 {
133     char sbuf[BUFSIZ], cnet[64], key[BUFSIZ];
134     const void *res;
135
136     time_t now = time(NULL);
137     struct obj_entry oent = { now, now };
138     struct awl_entry aent = { 0, 0 };
139
140     int len, klen, c_addrlen = strlen(c_addr);
141
142
143     if (cfg.do_awl) {
144         res = tcbdbget3(cfg.awl_db, c_addr, c_addrlen, &len);
145         if (res && len == sizeof(aent)) {
146             memcpy(&aent, res, len);
147         }
148         if (aent.count > cfg.awl_count) {
149             if (now < aent.last + 3600)
150                 goto incr_aent;
151             return true;
152         }
153     }
154
155     klen = snprintf(key, sizeof(key), "%s/%s/%s",
156                     c_net(c_addr, c_name, cnet, sizeof(cnet)),
157                     sender_normalize(sender, sbuf, sizeof(sbuf)), rcpt);
158     klen = MIN(klen, ssizeof(key) - 1);
159
160     res = tcbdbget3(cfg.obj_db, key, klen, &len);
161     if (res && len == sizeof(oent)) {
162         memcpy(&oent, res, len);
163     }
164
165     if (oent.last - oent.first < cfg.delay
166     &&  now - oent.first > cfg.retry_window)
167     {
168         oent.first = now;
169     }
170     oent.last = now;
171     tcbdbput(cfg.obj_db, key, klen, &oent, sizeof(oent));
172     if (oent.first + cfg.delay < now) {
173         if (cfg.do_awl) {
174           incr_aent:
175             aent.count++;
176             aent.last = now;
177             tcbdbput(cfg.awl_db, c_addr, c_addrlen, &aent, sizeof(aent));
178         }
179         return true;
180     }
181     return false;
182 }