File: cleanup.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 (193 lines) | stat: -rw-r--r-- 5,688 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
#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 ANY 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
 *
 *
 *
 */

int
main(int argc, char **argv)
{
  int c;

  if(argc < 2)
    usage(argv[0]);

  while ((c = getopt(argc,argv,":c:")) != EOF)
  { 
    switch(c)
    { 
      case 'c':
        configpath=optarg;
        read_conf(1);
        break;
        
      default:
        usage(argv[0]);
    }
  }

  logmessage("clean up process starting: %s %s\n", PROJECT, VERSION);
      
  /* connect to mysql */
#if defined(MYSQL_VERSION_ID) && MYSQL_VERSION_ID >= 40000
  mysql_server_init(0, NULL, NULL);
#endif
  mysql = db_connect(MYSQLDBASE);

  /* store current time */
  timenow=gettime();
  mysql_timeout=600;

  /* clean up greylisting */
  if(GREYLISTING==1)
  {
    /* expunge validated triplets */
    logmessage("expiring validated records older than %d days (%d)\n",
      (TRIPLET_AUTH_TIMEOUT / 86400), timenow - TRIPLET_AUTH_TIMEOUT);
    
    /* build up & execute query */
    snprintf(mysqlquery_array[0], 512,
      "DELETE QUICK FROM triplet WHERE _datelast < %d LIMIT 100000",
      timenow - TRIPLET_AUTH_TIMEOUT);
    if(db_deletequery(0) == -1) exit(-1);

    /* expunge unvalidated triplets */
    logmessage("expiring unvalidated records older than %d days (%d)\n",
      (TRIPLET_UNAUTH_TIMEOUT / 86400), timenow - TRIPLET_UNAUTH_TIMEOUT);

    /* build up & execute query */
    snprintf(mysqlquery_array[0], 512,
      "DELETE QUICK FROM triplet WHERE _datenew < %d AND _count = 0 LIMIT 100000",
      timenow - TRIPLET_UNAUTH_TIMEOUT);
    if(db_deletequery(0) == -1) exit(-1);
  }


  /* clean up autoblacklisted hosts */
  if(BLACKLISTING == 1)
  {
    /* expunge blacklist entries that have expired */
    logmessage("expiring blacklisted records (%d)\n", timenow);

    /* build up & execute query */
    snprintf(mysqlquery_array[0], 512,
      "DELETE QUICK FROM blacklist WHERE _expire <= %d \
       AND _expire != 0 LIMIT 100000",
      timenow);
    if(db_deletequery(0) == -1) exit(-1);
  }

  
  /* clean up auto whitelisted hosts */
  if(AUTO_WHITE_LISTING == 1)
  {
    /* expunge whitelist entries that have expired */
    logmessage("expiring autowhitelisted records older than %d days (%d)\n",
      (AUTO_WHITELIST_EXPIRE / 86400), timenow);

    /* build up & execute query */
    snprintf(mysqlquery_array[0], 512,
      "DELETE QUICK FROM whitelist WHERE _expire <= %d AND \
       _expire != 0 LIMIT 100000",
      timenow);
    if(db_deletequery(0) == -1) exit(-1);
  }


  /* clean up helo information */
  if(HELO_CHECK == 1)
  {
    /* expunge helo entries that have expired */
    logmessage("expiring helo records older than %d days (%d)\n",
      (HELO_AUTO_EXPIRE / 86400), timenow);

    /* build up & execute query */
    snprintf(mysqlquery_array[0], 512,
      "DELETE QUICK FROM helo WHERE _expire <= %d AND _expire != 0 LIMIT 100000",
      timenow);
    if(db_deletequery(0) == -1) exit(-1);
  }


  /* clean up expired throttlesender information */
  if((SENDERTHROTTLE == 1) && (SENDER_INACTIVE_EXPIRE))
  {
    logmessage("expiring throttlesender records older than %d days (%d)\n",
      (SENDER_INACTIVE_EXPIRE / 86400), timenow - SENDER_INACTIVE_EXPIRE);

    /* build up & execute query */
    snprintf(mysqlquery_array[0], 512,
      "DELETE QUICK FROM throttle WHERE _date < %d",
      timenow - SENDER_INACTIVE_EXPIRE);
    if(db_deletequery(0) == -1) exit(-1);


    logmessage("expiring throttlesender instances older than 1 hour (%d)\n",
      timenow - 3600);

    /* build up & execute query */
    snprintf(mysqlquery_array[0], 512,
      "DELETE QUICK FROM throttle_from_instance WHERE _expire < %d",
      timenow - 3600);
    if(db_deletequery(0) == -1) exit(-1);
  }


  /* clean up expired throttlerecipients information */
  if((RECIPIENTTHROTTLE == 1) && (RECIPIENT_INACTIVE_EXPIRE))
  {
    logmessage("expiring throttlerecipient records older than %d days (%d)\n",
      (RECIPIENT_INACTIVE_EXPIRE / 86400), timenow - RECIPIENT_INACTIVE_EXPIRE);

     /* build up & execute query */
    snprintf(mysqlquery_array[0], 512,
      "DELETE QUICK FROM throttle_rcpt WHERE _date < %d",
      timenow - RECIPIENT_INACTIVE_EXPIRE);
    if(db_deletequery(0) == -1) exit(-1);
  }


  /* clean up expired optin / optout policies */
  if(TRAINING_POLICY_TIMEOUT != 0)
  {
    /* clean up expired training policies */
    logmessage("expiring training policies records older than %d days (%d)\n",
      (TRAINING_POLICY_TIMEOUT / 86400), timenow - TRAINING_POLICY_TIMEOUT);

    /* build up & execute query */
    snprintf(mysqlquery_array[0], 512,
      "DELETE QUICK FROM policy_training WHERE _expire < %d and _expire != 0",
      timenow - TRAINING_POLICY_TIMEOUT);
    if(db_deletequery(0) == -1) exit(-1);
  }

  mysql_close(mysql);
    
  return (0);
}
 
/* EOF */