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