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