1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
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 */
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 /******************************************************************************/
33 * Copyright © 2007 Pierre Habouzit
50 TCBDB *awl_db, *obj_db;
63 int greylist_initialize(const char *directory, const char *prefix)
68 snprintf(path, sizeof(path), "%s/%swhitelist.db", directory, prefix);
69 cfg.awl_db = tcbdbnew();
70 if (!tcbdbopen(cfg.awl_db, path, BDBOWRITER | BDBOCREAT)) {
77 snprintf(path, sizeof(path), "%s/%sgreylist.db", directory, prefix);
78 cfg.obj_db = tcbdbnew();
79 if (!tcbdbopen(cfg.obj_db, path, BDBOWRITER | BDBOCREAT)) {
92 static void greylist_shutdown(void)
95 tcbdbsync(cfg.awl_db);
100 tcbdbsync(cfg.obj_db);
101 tcbdbdel(cfg.obj_db);
105 module_exit(greylist_shutdown);
107 const char *sender_normalize(const char *sender, char *buf, int len)
109 const char *at = strchr(sender, '@');
110 int rpos = 0, wpos = 0, userlen;
115 /* strip extension used for VERP or alike */
116 userlen = ((char *)memchr(sender, '+', at - sender) ?: at) - sender;
118 while (rpos < userlen) {
121 while (isdigit(sender[rpos + count]) && rpos + count < userlen)
123 if (count && !isalnum(sender[rpos + count])) {
124 /* replace \<\d+\> with '#' */
125 wpos += m_strputc(buf + wpos, len - wpos, '#');
129 while (isalnum(sender[rpos + count]) && rpos + count < userlen)
131 while (!isalnum(sender[rpos + count]) && rpos + count < userlen)
133 wpos += m_strncpy(buf + wpos, len - wpos, sender + rpos, count);
137 wpos += m_strputc(buf + wpos, len - wpos, '#');
138 wpos += m_strcpy(buf + wpos, len - wpos, at + 1);
143 c_net(const char *c_addr, const char *c_name, char *cnet, int cnetlen)
148 if (cfg.lookup_by_host)
151 if (!(dot = strchr(c_addr, '.')))
153 if (!(dot = strchr(dot + 1, '.')))
157 if (!(dot = strchr(dot, '.')) || dot - p > 3)
159 m_strncpy(ip2, sizeof(ip2), p, dot - p);
162 if (!(dot = strchr(dot, '.')) || dot - p > 3)
164 m_strncpy(ip3, sizeof(ip3), p, dot - p);
166 /* skip if contains the last two ip numbers in the hostname,
167 we assume it's a pool of dialup of a provider */
168 if (strstr(c_name, ip2) && strstr(c_name, ip3))
171 m_strncpy(cnet, cnetlen, c_addr, dot - c_addr);
175 bool try_greylist(const char *sender, const char *c_addr,
176 const char *c_name, const char *rcpt)
178 char sbuf[BUFSIZ], cnet[64], key[BUFSIZ];
181 time_t now = time(NULL);
182 struct obj_entry oent = { now, now };
183 struct awl_entry aent = { 0, 0 };
185 int len, klen, c_addrlen = strlen(c_addr);
189 res = tcbdbget3(cfg.awl_db, c_addr, c_addrlen, &len);
190 if (res && len == sizeof(aent)) {
191 memcpy(&aent, res, len);
193 if (aent.count > cfg.awl_limit) {
194 if (now < aent.last + 3600)
200 klen = snprintf(key, sizeof(key), "%s/%s/%s",
201 c_net(c_addr, c_name, cnet, sizeof(cnet)),
202 sender_normalize(sender, sbuf, sizeof(sbuf)), rcpt);
203 klen = MIN(klen, ssizeof(key) - 1);
205 res = tcbdbget3(cfg.obj_db, key, klen, &len);
206 if (res && len == sizeof(oent)) {
207 memcpy(&oent, res, len);
210 if (oent.last - oent.first < cfg.delay && now - oent.first > cfg.window) {
214 tcbdbput(cfg.obj_db, key, klen, &oent, sizeof(oent));
215 if (oent.first + cfg.delay < now) {
220 tcbdbput(cfg.awl_db, c_addr, c_addrlen, &aent, sizeof(aent));