936f0e2d3a8abceee5a11c5c170a456622e5cca2
[apps/pfixtools.git] / postlicyd / 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 "common.h"
39 #include "str.h"
40
41
42 typedef struct greylist_config_t {
43     unsigned lookup_by_host : 1;
44     unsigned no_sender      : 1;
45     unsigned no_recipient   : 1;
46     int delay;
47     int retry_window;
48     int client_awl;
49     int max_age;
50
51     TCBDB *awl_db;
52     TCBDB *obj_db;
53 } greylist_config_t;
54
55 #define GREYLIST_INIT { .lookup_by_host = false,       \
56                         .no_sender = false,            \
57                         .no_recipient = false,         \
58                         .delay = 300,                  \
59                         .retry_window = 2 * 24 * 3600, \
60                         .client_awl = 5,               \
61                         .max_age = 35 * 3600,          \
62                         .awl_db = NULL,                \
63                         .obj_db = NULL }
64
65 struct awl_entry {
66     int32_t count;
67     time_t  last;
68 };
69
70 struct obj_entry {
71     time_t first;
72     time_t last;
73 };
74
75 static inline bool greylist_check_awlentry(const greylist_config_t *config,
76                                            struct awl_entry *aent, time_t now)
77 {
78     return !(config->max_age > 0 && now - aent->last > config->max_age);
79 }
80
81 static inline bool greylist_check_object(const greylist_config_t *config,
82                                          const struct obj_entry *oent, time_t now)
83 {
84     return !((config->max_age > 0 && now - oent->last > config->max_age)
85              || (oent->last - oent->first < config->delay
86                  && now - oent->last > config->retry_window));
87 }
88
89 typedef bool (*db_entry_checker_t)(const greylist_config_t *, const void *, time_t);
90
91 static TCBDB *greylist_db_get(const greylist_config_t *config,
92                               const char *path, bool cleanup,
93                               size_t entry_len, db_entry_checker_t check)
94 {
95     TCBDB *awl_db, *tmp_db;
96     time_t now = time(NULL);
97
98     /* Rebuild a new database after removing too old entries.
99      */
100     if (cleanup && config->max_age > 0) {
101         uint32_t old_count = 0;
102         uint32_t new_count = 0;
103         bool replace = false;
104         bool trashable = false;
105         char tmppath[PATH_MAX];
106         snprintf(tmppath, PATH_MAX, "%s.tmp", path);
107
108         info("database cleanup started");
109         awl_db = tcbdbnew();
110         if (tcbdbopen(awl_db, path, BDBOREADER)) {
111             tmp_db = tcbdbnew();
112             if (tcbdbopen(tmp_db, tmppath, BDBOWRITER | BDBOCREAT | BDBOTRUNC)) {
113                 BDBCUR *cur = tcbdbcurnew(awl_db);
114                 TCXSTR *key, *value;
115
116                 key = tcxstrnew();
117                 value = tcxstrnew();
118                 if (tcbdbcurfirst(cur)) {
119                     replace = true;
120                     do {
121                         tcxstrclear(key);
122                         tcxstrclear(value);
123                         (void)tcbdbcurrec(cur, key, value);
124
125                         if ((size_t)tcxstrsize(value) == entry_len
126                             && check(config, tcxstrptr(value), now)) {
127                             tcbdbput(tmp_db, tcxstrptr(key), tcxstrsize(key),
128                                      tcxstrptr(value), entry_len);
129                             ++new_count;
130                         }
131                         ++old_count;
132                     } while (tcbdbcurnext(cur));
133                 }
134                 tcxstrdel(key);
135                 tcxstrdel(value);
136                 tcbdbcurdel(cur);
137                 tcbdbsync(tmp_db);
138             } else {
139                 warn("cannot run database cleanup: can't open destination database: %s",
140                      tcbdberrmsg(tcbdbecode(awl_db)));
141             }
142             tcbdbdel(tmp_db);
143         } else {
144             int ecode = tcbdbecode(awl_db);
145             warn("can not open database: %s", tcbdberrmsg(ecode));
146             trashable = ecode != TCENOPERM && ecode != TCEOPEN && ecode != TCENOFILE && ecode != TCESUCCESS;
147         }
148         tcbdbdel(awl_db);
149
150         /** Cleanup successful, replace the old database with the new one.
151          */
152         if (trashable) {
153             info("database cleanup finished: database was corrupted, create a new one");
154             unlink(path);
155         } else if (replace) {
156             info("database cleanup finished: before %u entries, after %d entries",
157                    old_count, new_count);
158             unlink(path);
159             if (rename(tmppath, path) != 0) {
160                 UNIXERR("rename");
161                 return NULL;
162             }
163         } else {
164             unlink(tmppath);
165             info("database cleanup finished: nothing to do, %u entries", new_count);
166         }
167     }
168
169     /* Effectively open the database.
170      */
171     awl_db = tcbdbnew();
172     if (!tcbdbopen(awl_db, path, BDBOWRITER | BDBOCREAT)) {
173         err("can not open database: %s", tcbdberrmsg(tcbdbecode(awl_db)));
174         tcbdbdel(awl_db);
175         return NULL;
176     }
177     return awl_db;
178 }
179
180
181 static bool greylist_initialize(greylist_config_t *config,
182                                 const char *directory, const char *prefix)
183 {
184     char path[PATH_MAX];
185
186     if (config->client_awl) {
187         snprintf(path, sizeof(path), "%s/%swhitelist.db", directory, prefix);
188         info("loading auto-whitelist database");
189         config->awl_db = greylist_db_get(config, path, true,
190                                          sizeof(struct awl_entry),
191                                          (db_entry_checker_t)(greylist_check_awlentry));
192         if (config->awl_db == NULL) {
193             return false;
194         }
195     }
196
197     snprintf(path, sizeof(path), "%s/%sgreylist.db", directory, prefix);
198     info("loading greylist database");
199     config->obj_db = greylist_db_get(config, path, true,
200                                      sizeof(struct obj_entry),
201                                      (db_entry_checker_t)(greylist_check_object));
202     if (config->obj_db == NULL) {
203         if (config->awl_db) {
204             tcbdbdel(config->awl_db);
205             config->awl_db = NULL;
206         }
207         return false;
208     }
209
210     return true;
211 }
212
213 static void greylist_shutdown(greylist_config_t *config)
214 {
215     if (config->awl_db) {
216         tcbdbsync(config->awl_db);
217         tcbdbdel(config->awl_db);
218         config->awl_db = NULL;
219     }
220     if (config->obj_db) {
221         tcbdbsync(config->obj_db);
222         tcbdbdel(config->obj_db);
223         config->obj_db = NULL;
224     }
225 }
226
227 static const char *sender_normalize(const char *sender, char *buf, int len)
228 {
229     const char *at = strchr(sender, '@');
230     int rpos = 0, wpos = 0, userlen;
231
232     if (!at)
233         return sender;
234
235     /* strip extension used for VERP or alike */
236     userlen = ((char *)memchr(sender, '+', at - sender) ?: at) - sender;
237
238     while (rpos < userlen) {
239         int count = 0;
240
241         while (isdigit(sender[rpos + count]) && rpos + count < userlen)
242             count++;
243         if (count && !isalnum(sender[rpos + count])) {
244             /* replace \<\d+\> with '#' */
245             wpos += m_strputc(buf + wpos, len - wpos, '#');
246             rpos += count;
247             count = 0;
248         }
249         while (isalnum(sender[rpos + count]) && rpos + count < userlen)
250             count++;
251         while (!isalnum(sender[rpos + count]) && rpos + count < userlen)
252             count++;
253         wpos += m_strncpy(buf + wpos, len - wpos, sender + rpos, count);
254         rpos += count;
255     }
256
257     wpos += m_strputc(buf + wpos, len - wpos, '#');
258     wpos += m_strcpy(buf + wpos, len - wpos, at + 1);
259     return buf;
260 }
261
262 static const char *c_net(const greylist_config_t *config,
263                          const char *c_addr, const char *c_name,
264                          char *cnet, int cnetlen)
265 {
266     char ip2[4], ip3[4];
267     const char *dot, *p;
268
269     if (config->lookup_by_host)
270         return c_addr;
271
272     if (!(dot = strchr(c_addr, '.')))
273         return c_addr;
274     if (!(dot = strchr(dot + 1, '.')))
275         return c_addr;
276
277     p = ++dot;
278     if (!(dot = strchr(dot, '.')) || dot - p > 3)
279         return c_addr;
280     m_strncpy(ip2, sizeof(ip2), p, dot - p);
281
282     p = ++dot;
283     if (!(dot = strchr(dot, '.')) || dot - p > 3)
284         return c_addr;
285     m_strncpy(ip3, sizeof(ip3), p, dot - p);
286
287     /* skip if contains the last two ip numbers in the hostname,
288        we assume it's a pool of dialup of a provider */
289     if (strstr(c_name, ip2) && strstr(c_name, ip3))
290         return c_addr;
291
292     m_strncpy(cnet, cnetlen, c_addr, dot - c_addr);
293     return cnet;
294 }
295
296
297 static bool try_greylist(const greylist_config_t *config,
298                          const char *sender, const char *c_addr,
299                          const char *c_name, const char *rcpt)
300 {
301 #define INCR_AWL                                              \
302     aent.count++;                                             \
303     aent.last = now;                                          \
304     debug("whitelist entry for %.*s updated, count %d",       \
305           c_addrlen, c_addr, aent.count);                     \
306     tcbdbput(config->awl_db, c_addr, c_addrlen, &aent,        \
307              sizeof(aent));
308
309     char sbuf[BUFSIZ], cnet[64], key[BUFSIZ];
310     const void *res;
311
312     time_t now = time(NULL);
313     struct obj_entry oent = { now, now };
314     struct awl_entry aent = { 0, 0 };
315
316     int len, klen, c_addrlen = strlen(c_addr);
317
318     /* Auto whitelist clients.
319      */
320     if (config->client_awl) {
321         res = tcbdbget3(config->awl_db, c_addr, c_addrlen, &len);
322         if (res && len == sizeof(aent)) {
323             memcpy(&aent, res, len);
324             debug("client %.*s has a whitelist entry, count is %d",
325                   c_addrlen, c_addr, aent.count);
326         }
327
328         if (!greylist_check_awlentry(config, &aent, now)) {
329             aent.count = 0;
330             aent.last  = 0;
331             debug("client %.*s whitelist entry too old",
332                   c_addrlen, c_addr);
333         }
334
335         /* Whitelist if count is enough.
336          */
337         if (aent.count >= config->client_awl) {
338             debug("client %.*s whitelisted", c_addrlen, c_addr);
339             if (now < aent.last + 3600) {
340                 INCR_AWL
341             }
342
343             /* OK.
344              */
345             return true;
346         }
347     }
348
349     /* Lookup.
350      */
351     klen = snprintf(key, sizeof(key), "%s/%s/%s",
352                     c_net(config, c_addr, c_name, cnet, sizeof(cnet)),
353                     config->no_sender ? "" : sender_normalize(sender, sbuf, sizeof(sbuf)),
354                     config->no_recipient ? "" : rcpt);
355     klen = MIN(klen, ssizeof(key) - 1);
356
357     res = tcbdbget3(config->obj_db, key, klen, &len);
358     if (res && len == sizeof(oent)) {
359         memcpy(&oent, res, len);
360         debug("found a greylist entry for %.*s", klen, key);
361     }
362
363     /* Discard stored first-seen if it is the first retrial and
364      * it is beyong the retry window and too old entries.
365      */
366     if (!greylist_check_object(config, &oent, now)) {
367         oent.first = now;
368         debug("invalid retry for %.*s: %s", klen, key,
369               (config->max_age > 0 && now - oent.last > config->max_age) ?
370                   "too old entry"
371                 : (oent.last - oent.first < config->delay ?
372                   "retry too early" : "retry too late" ));
373     }
374
375     /* Update.
376      */
377     oent.last = now;
378     tcbdbput(config->obj_db, key, klen, &oent, sizeof(oent));
379
380     /* Auto whitelist clients:
381      *  algorithm:
382      *    - on successful entry in the greylist db of a triplet:
383      *        - client not whitelisted yet ? -> increase count
384      *                                       -> withelist if count > limit
385      *        - client whitelisted already ? -> update last-seen timestamp.
386      */
387     if (oent.first + config->delay < now) {
388         debug("valid retry for %.*s", klen, key);
389         if (config->client_awl) {
390             INCR_AWL
391         }
392
393         /* OK
394          */
395         return true;
396     }
397
398     /* DUNNO
399      */
400     return false;
401 }
402
403
404 /* postlicyd filter declaration */
405
406 #include "filter.h"
407
408 static greylist_config_t *greylist_config_new(void)
409 {
410     const greylist_config_t g = GREYLIST_INIT;
411     greylist_config_t *config = p_new(greylist_config_t, 1);
412     *config = g;
413     return config;
414 }
415
416 static void greylist_config_delete(greylist_config_t **config)
417 {
418     if (*config) {
419         greylist_shutdown(*config);
420         p_delete(config);
421     }
422 }
423
424 static bool greylist_filter_constructor(filter_t *filter)
425 {
426     const char* path   = NULL;
427     const char* prefix = NULL;
428     greylist_config_t *config = greylist_config_new();
429
430 #define PARSE_CHECK(Expr, Str, ...)                                            \
431     if (!(Expr)) {                                                             \
432         err(Str, ##__VA_ARGS__);                                               \
433         greylist_config_delete(&config);                                       \
434         return false;                                                          \
435     }
436
437     foreach (filter_param_t *param, filter->params) {
438         switch (param->type) {
439           FILTER_PARAM_PARSE_STRING(PATH,   path);
440           FILTER_PARAM_PARSE_STRING(PREFIX, prefix);
441           FILTER_PARAM_PARSE_BOOLEAN(LOOKUP_BY_HOST, config->lookup_by_host);
442           FILTER_PARAM_PARSE_BOOLEAN(NO_SENDER, config->no_sender);
443           FILTER_PARAM_PARSE_BOOLEAN(NO_RECIPIENT, config->no_recipient);
444           FILTER_PARAM_PARSE_INT(RETRY_WINDOW, config->retry_window);
445           FILTER_PARAM_PARSE_INT(CLIENT_AWL,   config->client_awl);
446           FILTER_PARAM_PARSE_INT(DELAY,        config->delay);
447           FILTER_PARAM_PARSE_INT(MAX_AGE,      config->max_age);
448
449           default: break;
450         }
451     }}
452
453     PARSE_CHECK(path, "path to greylist db not given");
454     PARSE_CHECK(greylist_initialize(config, path, prefix ? prefix : ""),
455                 "can not load greylist database");
456
457     filter->data = config;
458     return true;
459 }
460
461 static void greylist_filter_destructor(filter_t *filter)
462 {
463     greylist_config_t *data = filter->data;
464     greylist_config_delete(&data);
465     filter->data = data;
466 }
467
468 static filter_result_t greylist_filter(const filter_t *filter,
469                                        const query_t *query,
470                                        filter_context_t *context)
471 {
472     const greylist_config_t *config = filter->data;
473     if (!config->no_recipient && query->state != SMTP_RCPT) {
474         warn("greylisting on recipient only works as smtpd_recipient_restrictions");
475         return HTK_ABORT;
476     }
477     if (!config->no_sender && query->state < SMTP_MAIL) {
478         warn("greylisting on sender must be performed after (or at) MAIL TO");
479         return HTK_ABORT;
480     }
481
482     return try_greylist(config, query->sender, query->client_address,
483                         query->client_name, query->recipient) ?
484            HTK_WHITELIST : HTK_GREYLIST;
485 }
486
487 static int greylist_init(void)
488 {
489     filter_type_t type =  filter_register("greylist", greylist_filter_constructor,
490                                           greylist_filter_destructor,
491                                           greylist_filter, NULL, NULL);
492     /* Hooks.
493      */
494     (void)filter_hook_register(type, "abort");
495     (void)filter_hook_register(type, "error");
496     (void)filter_hook_register(type, "greylist");
497     (void)filter_hook_register(type, "whitelist");
498
499     /* Parameters.
500      */
501     (void)filter_param_register(type, "lookup_by_host");
502     (void)filter_param_register(type, "no_sender");
503     (void)filter_param_register(type, "no_recipient");
504     (void)filter_param_register(type, "delay");
505     (void)filter_param_register(type, "retry_window");
506     (void)filter_param_register(type, "client_awl");
507     (void)filter_param_register(type, "max_age");
508     (void)filter_param_register(type, "path");
509     (void)filter_param_register(type, "prefix");
510     return 0;
511 }
512 module_init(greylist_init)