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