File: blacklist.c

package info (click to toggle)
postfix-policyd 1.80-2.1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 520 kB
  • ctags: 218
  • sloc: ansic: 3,858; sql: 270; sh: 251; makefile: 129
file content (187 lines) | stat: -rw-r--r-- 5,739 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
#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: blacklist_check
 *  purpose: module to check if connecting host/network is blacklisted.
 *   return: 1=yes, 0=no
 */
int
blacklist_check(unsigned int fd)
{

  /* reset mysql_optarray[fd][0] before getting new value */
  mysql_optarray[fd][0]=0;

  if(DEBUG > 0)
    logmessage("DEBUG: fd: %d checking blacklist\n", fd);

  /* build up query & execute */
  snprintf(mysqlquery_array[fd], 512,
    "SELECT COUNT(*) FROM blacklist WHERE _blacklist='%s' OR _blacklist='%s' OR _blacklist='%s' OR _blacklist='%s'",
    host_array[fd][2], host_array[fd][3], host_array[fd][4], host_array[fd][5]);
  if(db_optquery(fd) == -1) return(db_failure(fd, "blacklist"));

  /* blacklisted */
  if(mysql_optarray[fd][0] >= 1)
  {
    if(DEBUG > 0)
    {
      logmessage("DEBUG: fd: %d blacklist found: %s\n", fd, host_array[fd][2]);
      logmessage("DEBUG: fd: %d bypassing other modules\n", fd);
    }

    logmessage("rcpt=%lu, blacklist=block, 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); /* found */
  }

  if(DEBUG > 0)
    logmessage("DEBUG: fd: %d blacklist not found: %s\n", fd, host_array[fd][2]);

  return (0);   /* not found */
}



/*
 * function: blacklist_sender_check
 *  purpose: check if the sender address/domain host is blacklisted
 *   return: 1=yes, 0=no
 */
int
blacklist_sender_check (unsigned int fd)
{
  /* reset mysql_optarray[fd][0] before getting new value */
  mysql_optarray[fd][0]=0;

  
  if(DEBUG > 0)
    logmessage("DEBUG: fd: %d checking blacklist sender/domain\n", fd);

  /* build up & execute query */
  snprintf(mysqlquery_array[fd], 512,
    "SELECT COUNT(*) FROM blacklist_sender WHERE _blacklist='@%s' OR _blacklist='%s'",
      host_array[fd][7], triplet_array[fd][1]);
  if(db_optquery(fd) == -1) return(db_failure(fd, "blacklist_sender"));

  /* blacklisted */
  if(mysql_optarray[fd][0] >= 1)
  {
    if(DEBUG > 0)
    {
      logmessage("DEBUG: fd: %d blacklist sender found: %s/%s\n", fd,
        host_array[fd][7], triplet_array[fd][1]);
      logmessage("DEBUG: fd: %d bypassing other modules\n", fd);
    }

    logmessage("rcpt=%lu, blacklist_sender=block, host=%s (%s), from=%s, to=%s, size=%s\n",
      rcpt_count,               /* recipient count      */
      host_array[fd][2],        /* ip address           */
      host_array[fd][0],        /* hostname             */
      triplet_array[fd][1],     /* from address         */
      triplet_array[fd][2],     /* recipient address    */
      triplet_array[fd][3]);    /* mail size            */

    return (1); /* found */
  }

  if(DEBUG > 0)
    logmessage("DEBUG: fd: %d blacklist_sender not found: %s %s\n",
      fd, host_array[fd][7], triplet_array[fd][1]);

  return (0);   /* not found */
}
 


/*
 * function: blacklist_dnsname_check
 *  purpose: check if the dns sender address/domain host is blacklisted
 *   return: 1=yes, 0=no
 */
int
blacklist_dnsname_check (unsigned int fd)
{
  /* reset mysql_optarray[fd][0] before getting new value */
  mysql_optarray[fd][0]=0;

  if(DEBUG > 0)
    logmessage("DEBUG: fd: %d checking blacklist dnsname\n", fd);

  /* save sql lookup if there is no hostname information */
  if(strcmp(host_array[fd][0], "unknown") == 0)
    goto end;
      
  /* build up & execute query */
  snprintf(mysqlquery_array[fd], 512,
    "SELECT COUNT(*) FROM blacklist_dnsname WHERE '%s' LIKE _blacklist",
      host_array[fd][0]);
  if(db_optquery(fd) == -1) return(db_failure(fd, "blacklist_dnsname"));

  /* blacklisted */
  if(mysql_optarray[fd][0] >= 1)
  {
    if(DEBUG > 0)
    {
      logmessage("DEBUG: fd: %d blacklist dnsname found: %s/%s\n", fd,
        host_array[fd][7], triplet_array[fd][1]);
      logmessage("DEBUG: fd: %d bypassing other modules\n", fd);
    }

    logmessage("rcpt=%lu, blacklist_dnsname=block, host=%s (%s), from=%s, to=%s, size=%s\n",
      rcpt_count,               /* recipient count      */
      host_array[fd][2],        /* ip address           */
      host_array[fd][0],        /* hostname             */
      triplet_array[fd][1],     /* from address         */
      triplet_array[fd][2],     /* recipient address    */
      triplet_array[fd][3]);    /* mail size            */
  
    return (1); /* found */
  }

end:
  
  if(DEBUG > 0)
    logmessage("DEBUG: fd: %d blacklist_sender not found: %s\n", fd, host_array[fd][2]);

  return (0);   /* not found */
}
  

/* EOF */