File: greylist.c

package info (click to toggle)
postfix-policyd 1.82-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 520 kB
  • ctags: 220
  • sloc: ansic: 3,875; sql: 270; sh: 252; makefile: 127
file content (319 lines) | stat: -rw-r--r-- 12,167 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "policyd.h"


/*
 *
 *
 *                           Policy Daemon
 *
 *  policy daemon is used in conjuction with postfix to combat spam.
 *
 *  Copyright (C) 2004 Cami Sardinha (cami@mweb.co.za)
 *
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the  GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the License, or (at your
 *  option) any later version.
 *
 *  This program  is  distributed  in the hope that  it will be useful, but
 *  WITHOUT  WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free  Software Foundation Inc.,
 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *
 */


/*
 * function: greylist_check
 *  purpose: check if triplet exists in mysql
 *   return: 0 for new record, 1 for update
 */
int
greylist_check(unsigned int fd)
{

  if(DEBUG > 0)
    logmessage("DEBUG: fd: %d checking greylist\n", fd);
  
  /* set sane defaults */
  mysql_array[fd][0] = -2;
  mysql_optarray[fd][0] = OPTINOUTALL;
  
  /* opt-in/opt-out? */
  if(OPTINOUT == 1)
  {

    /* build up & execute query */
    snprintf(mysqlquery_array[fd], 512,
      "SELECT _optin FROM policy WHERE _rcpt='%s' OR _rcpt='@%s' ORDER BY _priority DESC LIMIT 1",
      triplet_array[fd][2], host_array[fd][9]);
    if(db_optquery(fd) == -1) return(db_failure(fd, "greylist"));

    /* user is opted out */
    if(mysql_optarray[fd][0] == 0)
    {
      logmessage("rcpt=%lu, greylist=optout, host=%s (%s), from=%s, to=%s, size=%s\n",
        rcpt_count,                       /* recipient count      */
        host_array[fd][2],                /* host address         */
        host_array[fd][0],                /* hostname             */
        triplet_array[fd][1],             /* sender               */
        triplet_array[fd][2],             /* recipient            */
        triplet_array[fd][3]);            /* size                 */

      return (0);
    }
  }


  /* domain or mail address is in training mode? */
  if(TRAINING_POLICY_TIMEOUT != 0)
  {
    /* build up & execute query */
    snprintf(mysqlquery_array[fd], 512,
      "SELECT COUNT(*) FROM policy_training WHERE _rcpt='%s' OR _rcpt='@%s'",
      triplet_array[fd][2], host_array[fd][9]);
    if(db_optquery(fd) == -1) return(db_failure(fd, "greylist"));

    /* training policy is activated for domain or email address */
    if(mysql_optarray[fd][0] >= 1)
      mysql_optarray[fd][0] = 2;
  }


  /* build up & execute query */
  snprintf(mysqlquery_array[fd], 512,
    "SELECT _count,_datenew,_datelast FROM triplet WHERE _host='%s' AND _from='%s' AND _rcpt='%s'",
      triplet_array[fd][0], triplet_array[fd][1], triplet_array[fd][2]);
  if(db_doquery(fd) == -1) return(db_failure(fd, "greylist"));

  /* update the greylist xheader */
  if(GREYLIST_X_HEADER==1)
    snprintf(xgreylist_array[fd], 128, "%s host: %s count: %d size: %s\n\n",
      POSTFIX_X_HEADER, host_array[fd][2], mysql_array[fd][0], triplet_array[fd][3]);

  
  /* triplet not found in greylist database */
  if(mysql_array[fd][0]==-2)
  {
    /* build up & execute query */
    snprintf(mysqlquery_array[fd], 512,
      "INSERT DELAYED INTO triplet (_datenew,_datelast,_host,_from,_rcpt) VALUES (%d,%d,'%s','%s','%s')",
      timenow, timenow, triplet_array[fd][0], triplet_array[fd][1], triplet_array[fd][2]);
    if(db_doquery(fd) == -1) return(db_failure(fd, "greylist"));

    /* auto black listing is enabled */
    if(AUTO_BLACK_LISTING == 1)
    {
      /* perform query to see how many triplets there are for a host/network */
      /* build up & execute query */
      snprintf(mysqlquery_array[fd], 512,
        "SELECT COUNT(*) FROM triplet WHERE _host='%s' AND _count = 0",
          triplet_array[fd][0]);
      if(db_doquery(fd) == -1) return(db_failure(fd, "greylist"));

      if(DEBUG > 0)
        logmessage("DEBUG: fd: %d unauth triplet: %d\n", fd, mysql_array[fd][0]);

      /* host has more than allowed number of unauthenticated triplets */
      if(mysql_array[fd][0] >= AUTO_BLACKLIST_NUMBER)
      {
        int expire=0;
      
        /* never auto expire blacklist? */
        if(AUTO_BLACKLIST_EXPIRE > 0)
          expire=timenow+AUTO_BLACKLIST_EXPIRE;

        /* blacklist netblock /24 */
        if(BLACKLIST_NETBLOCK==1)
        { /* blacklist netblock */
          snprintf(mysqlquery_array[fd], 512,
            "INSERT DELAYED INTO blacklist (_blacklist,_description,_expire) VALUES ('%s.%%','# autoblacklisted', %d)",
             triplet_array[fd][0], expire);
        } else { /* blacklist host ip */
          snprintf(mysqlquery_array[fd], 512,
            "INSERT DELAYED INTO blacklist (_blacklist,_description,_expire) VALUES ('%s','# autoblacklisted',%d)",
             host_array[fd][2], expire);
        }
        /* execute query */
        if(db_doquery(fd) == -1) return(db_failure(fd, "greylist"));
      
        logmessage("rcpt=%lu, greylist=abl, host=%s (%s), from=%s, to=%s, size=%s, expire=%d\n",
          rcpt_count,                       /* recipient count      */
          host_array[fd][2],                /* host address         */
          host_array[fd][0],                /* hostname             */
          triplet_array[fd][1],             /* sender               */
          triplet_array[fd][2],             /* recipient            */
          triplet_array[fd][3],             /* size                 */
          expire);                          /* expiry date          */

        /* build up & execute query */
        snprintf(mysqlquery_array[fd], 512,
          "DELETE QUICK from triplet WHERE _host='%s'", triplet_array[fd][0]);
        if(db_doquery(fd) == -1) return(db_failure(fd, "greylist"));

        /* reject */
        return (-1);
      }
    }
      

    /* not in training mode, reject if this is the first attempt */
    if((TRAINING_MODE == 0) && (mysql_optarray[fd][0] != 2))
    {
      logmessage("rcpt=%lu, greylist=new, host=%s (%s), from=%s, to=%s, size=%s\n",
        rcpt_count,                       /* recipient count      */
        host_array[fd][2],                /* host address         */
        host_array[fd][0],                /* hostname             */
        triplet_array[fd][1],             /* sender               */
        triplet_array[fd][2],             /* recipient            */
        triplet_array[fd][3]);            /* size                 */
      
      /* reject */
      return (-1);
    }
    
    /* in training mode, always accept */
    if((TRAINING_MODE == 1) || (mysql_optarray[fd][0] == 2))
    {
      logmessage("rcpt=%lu, greylist=new_train, host=%s (%s), from=%s, to=%s, size=%s\n",
        rcpt_count,
        host_array[fd][2],      /* host */
        host_array[fd][0],      /* hostname */
        triplet_array[fd][1],   /* from */
        triplet_array[fd][2],   /* rcpt */
        triplet_array[fd][3]    /* size */
      );

      /* accept */
      return (0);
    }
    
  } else { /* triplet exists in database */
    
    /* has TRIPLET_TIME expired since triplet creation? */
    if(timenow < (unsigned int)(mysql_array[fd][1]+TRIPLET_TIME))
    {
      /* not in training mode */
      if((TRAINING_MODE == 0) && (mysql_optarray[fd][0] != 2))
      {
        logmessage("rcpt=%lu, greylist=abuse, host=%s (%s), from=%s, to=%s, size=%s\n", 
          rcpt_count,                       /* recipient count      */
          host_array[fd][2],                /* host address         */
          host_array[fd][0],                /* hostname             */
          triplet_array[fd][1],             /* sender               */
          triplet_array[fd][2],             /* recipient            */
          triplet_array[fd][3]);            /* size                 */

        return (-1);
      }
    }
    
    
    /* implement autowhitelisting */
    if(AUTO_WHITE_LISTING==1) 
    {
      /* expire auto-whitelisted hosts if enabled */
      int expire=0;
      if(AUTO_WHITELIST_EXPIRE > 0)
        expire=timenow+AUTO_WHITELIST_EXPIRE;

      /* save an sql lookup if awl == 1 */
      if(AUTO_WHITELIST_NUMBER == 1)
        goto awl;

      /* check how many auth triplets there are for a host/network */
      /* build up & execute query */
      snprintf(mysqlquery_array[fd], 512,
        "SELECT COUNT(*) FROM triplet WHERE _host='%s' AND _count > 0",
          triplet_array[fd][0]);
      if(db_doquery(fd) == -1) return(db_failure(fd, "greylist"));

      if(DEBUG > 0)
        logmessage("DEBUG: fd: %d whitelist result: %d\n", fd, mysql_array[fd][0]);

      if(mysql_array[fd][0] >= AUTO_WHITELIST_NUMBER)
        goto awl;       /* auto whitelist network/host */
      else
        goto nawl;      /* dont auto white list network */

awl:

      if(AUTO_WHITELIST_NETBLOCK==1)
      {
        /* build up & execute query */
        snprintf(mysqlquery_array[fd], 512,
          "INSERT DELAYED INTO whitelist (_whitelist,_description,_expire) VALUES ('%s.%%','# autowhitelisted host',%d)",
          triplet_array[fd][0], expire);
      } else {
        /* build up & execute query */
        snprintf(mysqlquery_array[fd], 512,
          "INSERT DELAYED INTO whitelist (_whitelist,_description,_expire) VALUES ('%s','# autowhitelisted host',%d)",
          host_array[fd][2], expire);
      }
      if(db_doquery(fd) == -1) return(db_failure(fd, "greylist"));

      /* build up & execute query */
      snprintf(mysqlquery_array[fd], 512,
        "DELETE QUICK from triplet WHERE _host='%s'", triplet_array[fd][0]);
      if(db_doquery(fd) == -1) return(db_failure(fd, "greylist"));

      logmessage("rcpt=%lu, greylist=awl, host=%s (%s), from=%s, to=%s, size=%s, expire=%d\n",
        rcpt_count,                       /* recipient count      */
        host_array[fd][2],                /* host address         */
        host_array[fd][0],                /* hostname             */
        triplet_array[fd][1],             /* sender               */
        triplet_array[fd][2],             /* recipient            */
        triplet_array[fd][3],             /* size                 */
        expire);                          /* expiry date          */

      return(0);
    }
    
nawl:

    /* build up & execute query */
    snprintf(mysqlquery_array[fd], 512,
      "UPDATE triplet SET _datelast='%d',_count=_count+1 WHERE _host='%s' AND _from='%s' AND _rcpt='%s'",
      timenow, triplet_array[fd][0], triplet_array[fd][1], triplet_array[fd][2]);
    if(db_doquery(fd) == -1) return(db_failure(fd, "greylist"));
    
    /* training mode */
    if((TRAINING_MODE == 0) && (mysql_optarray[fd][0] != 2))
    {
      /* yes, it has.. update */
      logmessage("rcpt=%lu, greylist=update, host=%s (%s), from=%s, to=%s, size=%s\n",
        rcpt_count,                       /* recipient count      */
        host_array[fd][2],                /* host address         */
        host_array[fd][0],                /* hostname             */
        triplet_array[fd][1],             /* sender               */
        triplet_array[fd][2],             /* recipient            */
        triplet_array[fd][3]);            /* size                 */
    }
    
    /* training mode */
    if((TRAINING_MODE == 1) || (mysql_optarray[fd][0] == 2))
    {
      /* yes, it has.. update */
      logmessage("rcpt=%lu, greylist=update_train, host=%s (%s), from=%s, to=%s, size=%s\n",
        rcpt_count,                       /* recipient count      */
        host_array[fd][2],                /* host address         */
        host_array[fd][0],                /* hostname             */
        triplet_array[fd][1],             /* sender               */
        triplet_array[fd][2],             /* recipient            */
        triplet_array[fd][3]);            /* size                 */
    }

    return (0);
  }

  return (0); /* never reached */
}
 
/* EOF */