-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcqgreylist.c
492 lines (386 loc) · 11.7 KB
/
cqgreylist.c
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* Version 0.2
* Copyright (c) 2006-2012 Zaid Amireh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
/*
* Change anything you want here
*/
/* RFC 2821 specifies the timeout for recieving a command to at least 5 mins */
#define TIMEOUT 300
/* specify the greylisting time in which to not accept mail from a sender */
#define GREY_SECONDS 60
#define UNUSED(x) (void)(x)
static const char* hostname = "your.fqdn.com";
static const char* message = "you are greylisted try again";
static const char* base_directory = "/var/qmail/cqgreylist/";
/*
* End of user editable parameters
*/
/* 512 chars according to RFC 2821 section 4.5.3 */
#define BUFFER_SIZE 512
#define FALSE 0
#define TRUE 1
void first_octet( char* ip, char** octet );
int is_greylisted( char* ip );
int is_backtoosoon( char* ip );
void negotiate_and_reject( const char* message );
void hand_to_smtpd( char* smtpd_line );
void create_file( char* ip );
void clean_exit( int retval );
void parse_args( int argc, char* argv[], char** cmd_line );
void timeout( int sig_num );
char* smtpd_line = NULL;
char* buffer = NULL;
char* cmd = NULL;
int
main( int argc, char* argv[] ){
char* remote_ip = NULL;
char* relay_client = NULL;
char* whitelisted = NULL;
/* get the environment variables */
remote_ip = getenv( "TCPREMOTEIP" );
/*
* sanity check, remove if you badly need performance
*/
if( remote_ip == NULL ){
/* this shouldnt happen */
fprintf( stderr, "environment variable TCPREMOTEIP not set - exiting, " );
clean_exit( -1 );
}
parse_args( argc, argv, &smtpd_line );
relay_client = getenv("RELAYCLIENT");
whitelisted = getenv("WHITELISTED");
if( (relay_client != NULL) || (whitelisted != NULL) ){
#ifdef DEBUG
fprintf( stderr, "accepted mail for relay client or it is whitelisted, " );
#endif
/* if its a relay client then we handle to smtpd and exit cleanly */
hand_to_smtpd( smtpd_line );
}
if( is_greylisted( remote_ip ) == TRUE ){
if( is_backtoosoon( remote_ip ) == TRUE )
negotiate_and_reject( message );
else
hand_to_smtpd( smtpd_line );
}
else{
create_file( remote_ip );
negotiate_and_reject( message );
}
return -1;
}
int
is_greylisted( char* ip ){
char *octet = NULL;
int retval = FALSE;
char *dir_name = NULL;
int dir_name_size = 0;
DIR *dir_handler = NULL;
first_octet( ip, &octet );
/* check to see if a directory exists */
dir_name_size = strlen(base_directory) + strlen(octet);
dir_name = malloc ((dir_name_size + 1) * sizeof(char));
memset (dir_name, 0x0, dir_name_size + 1);
strcat (dir_name, base_directory);
strncat (dir_name, octet, dir_name_size - strlen(base_directory) );
strcat (dir_name, "\0");
dir_handler = opendir( dir_name );
if( dir_handler == NULL ){
if( errno == ENOENT ){ /* directory does not exist, create it */
if( 0 == mkdir( dir_name, 0700 ) ){
#ifdef DEBUG
fprintf( stderr, "Created directory %s , ", dir_name );
#endif
}
else{
fprintf( stderr, "Could not create directory %s - deferring till you correct this, ", dir_name );
}
}
else{
/* TODO: handle other errors here */
fprintf( stderr, "permissions or other errors for %s - deferring till you correct this, ", dir_name );
}
}
else{
#ifdef DEBUG
fprintf( stderr, "Directory %s already exists - opened, ", dir_name );
#endif
closedir( dir_handler );
retval = TRUE;
}
/* if its still FALSE then an error happened up and we don't need to check for a file */
if( retval != FALSE ){
char* file_name = NULL;
int file_name_size = 0;
/* check if the file exists */
struct stat file_stat;
memset( &file_stat, 0, sizeof(file_stat) );
file_name_size = dir_name_size + 1 /*"/"*/ + strlen(ip);
file_name = malloc ((file_name_size + 1) * sizeof(char));
memset (file_name, 0x0, file_name_size + 1);
strcat (file_name, dir_name);
strcat (file_name, "/");
strncat (file_name, ip, file_name_size - dir_name_size - 1 );
strcat (file_name, "\0");
if( stat( file_name, &file_stat ) == 0 ){
/* this host is grey listed, continue and return TRUE */
retval = TRUE;
}
else{ /* couldnt stat the file */
#ifdef DEBUG
fprintf( stderr, "Could not stat file %s , ", file_name );
#endif
retval = FALSE;
}
if ( file_name ) free( file_name );
}
/* free */
if ( octet ) free( octet );
if ( dir_name ) free( dir_name );
return retval;
}
int
is_backtoosoon( char* ip ){
char *octet = NULL;
int retval = TRUE;
int dir_name_size = 0;
int file_name_size = 0;
char *file_name = NULL;
struct stat file_stat;
first_octet( ip, &octet );
/* check if the file exists */
memset( &file_stat, 0, sizeof(file_stat) );
dir_name_size = strlen(base_directory) + strlen(octet);
file_name_size = dir_name_size + 1 /*"/"*/ + strlen(ip);
file_name = malloc ((file_name_size + 1) * sizeof(char));
memset (file_name, 0x0, file_name_size + 1);
strcat (file_name, base_directory);
strncat (file_name, octet, dir_name_size - strlen(base_directory) );
strcat (file_name, "/");
strncat (file_name, ip, file_name_size - dir_name_size - 1 );
strcat (file_name, "\0");
if( stat( file_name, &file_stat ) == 0 ){
/* file exists, do our stuff */
if( difftime( time(NULL), file_stat.st_mtime ) > GREY_SECONDS ){
/* been trying for the specified time, let him through */
#ifdef DEBUG
fprintf( stderr, "Allowing %s , ", ip );
#endif
retval = FALSE;
}
else{ /* you came back too soon */
#ifdef DEBUG
fprintf( stderr, "%s Back too soon , ", ip );
#endif
}
}
else{ /* couldnt stat the file, highly unlikely */
fprintf( stderr, "Could not stat file %s - I think you should check it out, ", file_name );
/* create the file */
}
/* free */
if ( octet ) free( octet );
if ( file_name ) free( file_name );
return retval;
}
void
parse_args( int argc, char* argv[], char** line ){
int length = 0;
int i = 0;
for( i=1; i<argc; i++ ){
length += strlen( argv[i] );
}
*line = malloc( (length + argc/*For the number of spaces*/ + 1) * sizeof(char));
memset( *line, 0, length + argc/*For the number of spaces*/ + 1 );
for( i=1; i<argc; i++ ){
strcat( *line, argv[i] );
strcat( *line, " " );
}
return;
}
void
negotiate_and_reject( const char* message ){
typedef struct
{
char name[5];
char response[64];
} command;
command commands[11] =
{
{ "HELO" , "" },
{ "EHLO" , "250 hello\r\n"},
{ "MAIL" , "250 ok\r\n"},
{ "RCPT" , ""},
{ "DATA" , ""},
{ "RSET" , "250 ready\r\n"},
{ "VRFY" , "502 not implemented\r\n"},
{ "EXPN" , "502 not implemented\r\n"},
{ "HELP" , "502 not implemented\r\n"},
{ "NOOP" , "250 noop\r\n"},
{ "QUIT" , ""}
};
int break_loop = FALSE;
int matched = FALSE;
#ifdef DEBUG
fprintf( stderr, "starting negotiation, " );
#endif
/* set the timeout signal handler */
signal( SIGALRM, timeout );
snprintf( commands[0].response, sizeof(commands[0].response), "250 %s\r\n", hostname );
commands[0].response[sizeof(commands[0].response)-1] = 0;
snprintf( commands[3].response, sizeof(commands[3].response), "450 Rcpt to <> - %s\r\n", message );
commands[3].response[sizeof(commands[3].response)-1] = 0;
snprintf( commands[4].response, sizeof(commands[4].response), "451 %s\r\n", message );
commands[4].response[sizeof(commands[4].response)-1] = 0;
snprintf( commands[10].response, sizeof(commands[10].response), "221 %s Bye\r\n", hostname );
commands[10].response[sizeof(commands[10].response)-1] = 0;
buffer = malloc( BUFFER_SIZE + 1 * sizeof (char));
memset( buffer, 0, BUFFER_SIZE + 1);
cmd = malloc( 5 * sizeof(char));
memset( cmd, 0, 5 );
/* send the greeting */
printf( "220 %s cqgreylist - reconnect later\r\n", hostname );
fflush( NULL );
alarm( TIMEOUT ); /* send us an alarm after the time specified */
while( (fgets( buffer, BUFFER_SIZE, stdin ) != NULL) && break_loop != TRUE ){
int i = 0;
alarm( 0 );
matched = FALSE;
/* extract the cmd */
memset( cmd, 0, sizeof(cmd) );
memcpy( cmd, buffer, 4 );
if( strncasecmp( cmd, "QUIT" , sizeof(cmd) ) == 0 ){
printf( "%s", commands[10].response );
fflush( NULL );
break_loop = TRUE;
matched = TRUE;
break;
}
for( i=0; i<10 && matched == FALSE; i++){
if( strncasecmp( cmd, commands[i].name, sizeof(cmd) ) == 0 ){
printf( "%s", commands[i].response );
fflush( NULL );
matched = TRUE;
break;
}
} /* end of for loop */
if( matched == FALSE ){
printf( "502 not implemented\r\n" );
fflush( NULL );
}
alarm( TIMEOUT );
} /* end of while loop */
/* free */
if ( cmd ) free( cmd );
if ( buffer ) free( buffer );
clean_exit( 0 );
return;
}
void
hand_to_smtpd( char* smtpd_line ){
#ifdef DEBUG
fprintf( stderr, "executing %s , ", smtpd_line );
#endif
if( system( smtpd_line ) == -1 ){
fprintf( stderr, "could not execute next stage %s , ", smtpd_line );
}
/* nothing else to do, exit program */
clean_exit( 0 );
return;
}
void
create_file( char* ip ){
char *octet = NULL;
int dir_name_size = 0;
int file_name_size = 0;
char *file_name = NULL;
FILE *fh = NULL;
first_octet( ip, &octet );
dir_name_size = strlen(base_directory) + strlen(octet);
file_name_size = dir_name_size + 1 /*"/"*/ + strlen(ip);
file_name = malloc ((file_name_size + 1) * sizeof(char));
memset (file_name, 0x0, file_name_size + 1);
strcat (file_name, base_directory);
strncat (file_name, octet, dir_name_size - strlen(base_directory) );
strcat (file_name, "/");
strncat (file_name, ip, file_name_size - dir_name_size - 1 );
strcat (file_name, "\0");
fh = fopen( file_name, "w" );
if( fh == NULL ){
fprintf( stderr, "could not create file %s - this is bad, ", file_name );
}
else{
#ifdef DEBUG
fprintf( stderr, "created file %s , ", file_name );
#endif
fclose( fh );
}
/* free */
if ( octet ) free( octet );
if (file_name ) free( file_name );
return;
}
void
first_octet( char* ip, char** pointer ){
struct in_addr bin_ip;
memset( &bin_ip, 0, sizeof(bin_ip) );
/* TODO: need to check for bad IP format */
inet_aton( ip, &bin_ip );
*pointer = malloc( 4 * sizeof(char)); /* the string can never be longer than 4 */
memset( *pointer, 0, 4 );
sprintf( *pointer, "%d", 0x000000FF & bin_ip.s_addr );
/* just to make sure we don't overflow */
/*pointer[3] = 0;*/
return;
}
void
clean_exit( int retval ){
/* free */
if ( smtpd_line ) free( smtpd_line );
exit( retval );
}
void
timeout( int sig_num ){
UNUSED(sig_num);
printf( "421 %s timeout\r\n", hostname );
/* free */
if ( buffer ) free( buffer );
if ( cmd ) free( cmd );
clean_exit( -1 );
return;
}