forked from qmail/simscan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simscan.c
2402 lines (2098 loc) · 61.3 KB
/
simscan.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id: simscan.c,v 1.8 2009/12/13 04:45:39 xen0phage Exp $
* Copyright (C) 2004-2005 Inter7 Internet Technologies, Inc.
*
* 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
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include "config.h"
#include "cdb/cdb.h"
#ifdef ENABLE_REGEX
#include <pcre.h>
#endif
/* qmail-queue error codes */
#define EXIT_0 0 /* Success */
#define EXIT_11 11 /* address too long */
#define EXIT_454 54 /* unable to read message or envelope */
#define EXIT_400 71 /* temporary refusal to accept */
#define EXIT_500 31 /* permenent refusal to accept message SMTP: 5XX code */
#define EXIT_MSG 82 /* exit with custom error message */
#define EXIT_91 91 /* Envelope format error. */
/*
51 Out of memory.
52 Timeout.
53 Write error; e.g., disk full.
54 Unable to read the message or envelope.
55 Unable to read a configuration file. (Not used by qmail-queue.)
56 Problem making a network connection from this host. (Not used by
qmail-queue.)
61 Problem with the qmail home directory.
62 Problem with the queue directory.
63 Problem with queue/pid.
64 Problem with queue/mess.
65 Problem with queue/intd.
66 Problem with queue/todo.
71 Mail server temporarily refuses to send the message to any recipi-
ents. (Not used by qmail-queue.)
72 Connection to mail server timed out. (Not used by qmail-queue.)
73 Connection to mail server rejected. (Not used by qmail-queue.)
74 Connection to mail server succeeded, but communication failed.
81 Internal bug; e.g., segmentation fault.
91 Envelope format error.
*/
#if HAVE_STRSEP!=1
char *strsep (char **pp, char *delim);
#endif
#ifdef QUARANTINEDIR
void quarantine_msg(char *message_name);
#endif
#define MAX_DSPAM_ARGS 20
char *dspam_args[MAX_DSPAM_ARGS];
#define MAX_SPAMC_ARGS 20
char *spamc_args[MAX_SPAMC_ARGS];
/* Global work buffers */
#define BUFFER_SIZE 2048
char buffer[BUFFER_SIZE];
char message_name[BUFFER_SIZE+10];
char workdir[BUFFER_SIZE];
char unique_ext[BUFFER_SIZE];
void format_dir(char *workdir);
void exit_clean(int error_code);
int fd_move(int to,int from);
int fd_copy(int to,int from);
int remove_files(char *dir);
int str_rstr(register char *h,register char *n);
char *replace(char *string, char *oldpiece, char *newpiece);
int DebugFlag = 0;
int DebugFiles = 0;
int size_limit = 250000;
/* --stdout is required for reading virus names */
char *viri_args[] = { "clamdscan", "--stdout", message_name, NULL };
/* To/From address processing globals */
#define MAX_RCPT_TO 255
#define MAX_EMAIL 500
char addr_name[BUFFER_SIZE+5];
char *addr_buff;
int MaxRcptTo;
char MailFrom[MAX_EMAIL];
char Subject[BUFFER_SIZE];
char RcptTo[MAX_RCPT_TO][MAX_EMAIL];
int PerDomainHits = 0;
float PDHits;
int cdb_seek(int fd, char *key, unsigned int len, uint32 *dlen);
char *qmail_queue = QMAILQUEUE;
void lowerit(char *input);
/* Per Domain globals */
#ifdef ENABLE_PER_DOMAIN
#define MAXDOMAINS 100
#define MAXDOMLEN 256
#define PER_DOMAIN_TOKENS " =\t\n\r,\"'"
#define MAIL_EXT_TOKENS "-"
int PerDomainClam;
int PerDomainSpam;
int PerDomainTrophie;
int PerDomainSpamPassthru;
int MaxDomains;
char Domains[MAXDOMAINS][MAXDOMLEN];
void set_per_domain();
void init_per_domain();
void per_domain_lookup( char *key );
void per_domain_email_lookup (char *email);
#endif
/* Customer Smtp reject message globals */
#ifdef ENABLE_CUSTOM_SMTP_REJECT
char RejectMsg[BUFFER_SIZE+100];
#endif
/* Generic virus scanner globals */
#ifdef VIRUSSCANNER
char VirusName[BUFFER_SIZE];
int FoundVirus=0;
#endif
/* attachment and virusscanner need ripmime */
#if (VIRUSSCANNER==1 || ENABLE_ATTACH==1) && DO_RIPMIME==1
int run_ripmime();
#endif
/* Trophie virus scanner globals */
#ifdef ENABLE_TROPHIE
int check_trophie();
#endif
/* ClamAntiVirus globals */
#ifdef ENABLE_CLAMAV
int InClamHeaders;
int check_clam();
int is_clam(char *clambuf);
void log_clam(char *clambuf);
#endif
/* Attachment scanning globals */
#ifdef ENABLE_ATTACH
#define ATTACH_TOKENS " :\t\n\r"
#define MAX_ATTACH 100
#define MAX_ATTACH_LINE 100
char AttachName[256];
char bk_attachments[MAX_ATTACH][MAX_ATTACH_LINE];
int MaxAttach=0;
int check_attach();
int init_attach();
#endif
float SpamProbability=0.0;
float DSpamConf=0.0;
/* DSPAM scanning globals */
#if defined(ENABLE_DSPAM)
int InHeaders;
int IsSpam;
char spam_message_name[BUFFER_SIZE+10];
int check_dspam();
int is_dspam(char *spambuf);
#endif
float SpamHits;
float SAReqHits;
/* Spam scanning globals */
#ifdef ENABLE_SPAM
char spamuser[BUFFER_SIZE];
int InHeaders;
int IsSpam;
char spam_message_name[BUFFER_SIZE+10];
int check_spam();
int is_spam(char *spambuf);
#endif
struct timeval start,stop;
double utime;
#define SECS(tv) (tv.tv_sec + tv.tv_usec / 1000000.0)
/* write a received line */
#ifdef ENABLE_RECEIVED
char runned_scanners[MAX_EMAIL]="";
void add_run_scanner(char *key);
struct tm *tm;
static char monthname[12][4] = {
"Jan","Feb","Mar","Apr","May","Jun"
,"Jul","Aug","Sep","Oct","Nov","Dec"};
#endif
#ifdef ENABLE_REGEX
#define MAX_REGEX 100
#define MAX_REGEX_LINE 500
int numRegex=0;
char regexs[MAX_REGEX][MAX_REGEX_LINE];
pcre *comp_regexs[MAX_REGEX];
int check_regex();
void init_regex(char *);
#endif
void log_message( char *state, char *subject, int spam );
int main(int argc, char **argv)
{
#ifdef HAS_ULIMIT_NPROC
struct rlimit limits;
#endif
static int fd;
int ret;
int fd_per;
size_t tmpread;
size_t msgsize;
char *tmpstr;
int pim[2];
int qstat;
int pid;
int i = 0;
int gotrcpt = 0;
int gotfrom = 0;
/* print out version information if requested */
if ( argc > 1 && strcmp(argv[1],"-v" )==0 ) {
printf("simscan version: %s\n", VERSION);
exit(0);
}
#ifdef HAS_ULIMIT_NPROC
/* Set ulimits to prevent hangs if it forks too many processes */
getrlimit(RLIMIT_NPROC, &limits);
limits.rlim_cur = 1024;
setrlimit(RLIMIT_NPROC, &limits);
#endif
/* get the starttime of the process */
gettimeofday(&start,(struct timezone *) 0);
/* Set the debug flag if environment variable set */
if ( (tmpstr=getenv("SIMSCAN_DEBUG"))!=NULL ) {
DebugFlag = atoi(tmpstr);
}
/* Set the "leave temp files alone" flag */
if ( (tmpstr=getenv("SIMSCAN_DEBUG_FILES"))!=NULL ) {
DebugFiles = atoi(tmpstr);
}
#ifdef ENABLE_ATTACH
init_attach();
#endif
#ifdef ENABLE_PER_DOMAIN
init_per_domain();
#endif
/* format the new directory name */
format_dir(workdir);
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: starting: work dir: %s\n", getppid(), workdir);
}
/* create the working directory, allow group access too */
umask(027);
if ( mkdir(workdir, 0750) == -1 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: error making work dir, exit 400, errno: %d\n",
getppid(), errno);
}
_exit(EXIT_400);
}
/* change to the new working directory */
if ( chdir(workdir) != 0 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: error changing directory to workdir errno: %d\n",
getppid(), errno);
}
exit_clean(EXIT_400);
}
/* open a msg file to hold the email */
snprintf(message_name, sizeof(message_name), "msg.%s", unique_ext);
if ( (fd=open(message_name, O_WRONLY|O_CREAT|O_TRUNC,0644)) ==- 1) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: error opening msg file %s errnostr: %d\n",
getppid(), message_name, errno);
}
exit_clean(EXIT_400);
}
/* read the email into the new file */
msgsize = 0;
while( (ret = read(0, buffer, sizeof(buffer))) > 0 ) {
msgsize += ret;
if ( write(fd, buffer,ret) == -1 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: error writing msg error: %d\n", getppid(), errno);
}
/* on a write error close the file so we can remove the directory */
close(fd);
exit_clean(EXIT_400);
}
}
/* close the file */
if ( close(fd) == -1 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: error closing email file errno: %d\n", getppid(), errno);
}
exit_clean(EXIT_400);
}
/* if we had a read error, exit with deferral message */
if ( ret < 0 ) exit_clean(EXIT_400);
/* open a msg file to hold the email addrs */
snprintf(addr_name, sizeof(addr_name), "addr.%s", unique_ext);
/* open the addr_name file */
if ( (fd_per=open(addr_name, O_WRONLY|O_CREAT|O_TRUNC,0644)) ==- 1) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: error opening addr name: %s\n", getppid(), addr_name);
}
exit_clean(EXIT_400);
}
/* read/write in the email addresses */
addr_buff = calloc(sizeof(char),MAX_EMAIL);
if ( addr_buff == NULL ) exit_clean(EXIT_400);
MaxRcptTo = 0;
memset(RcptTo,0,sizeof(RcptTo));
memset(MailFrom,0,sizeof(MailFrom));
errno = 0;
while( (tmpread=read(1,addr_buff+i,1))>0 ) {
write(fd_per,addr_buff+i, 1);
if (addr_buff[i++]!=0 && i<MAX_EMAIL)
continue;
/* one line received */
i=0;
if (addr_buff[0] == '\0') {
// \0\0 is end of envelop list
if (!gotrcpt) {
// end of list without Recipient? Hm.. seems error
free (addr_buff);
close (fd_per);
exit_clean(EXIT_91);
}
break;
}
if (addr_buff[0] == 'F') {
if (gotfrom) {
// only one MAIL FROM can be
free (addr_buff);
close (fd_per);
exit_clean(EXIT_91);
}
strncpy(MailFrom, &addr_buff[1], sizeof(MailFrom)-1);
lowerit(MailFrom);
gotfrom = 1;
if ( DebugFlag > 3 )
fprintf(stderr, "simscan:[%d]: F envelope is %s\n", getppid(), MailFrom);
}
if (addr_buff[0] == 'T') {
if (MaxRcptTo<MAX_RCPT_TO) {
strncpy(RcptTo[MaxRcptTo], &addr_buff[1], MAX_EMAIL-1);
lowerit(RcptTo[MaxRcptTo]);
gotrcpt = 1;
MaxRcptTo ++;
if ( DebugFlag > 3 )
fprintf(stderr, "simscan:[%d]: T%d envelope is %s\n", getppid(), MaxRcptTo, RcptTo[MaxRcptTo-1]);
}
}
}
free (addr_buff);
if (tmpread <= 0 && errno != 0) {
// error or unexpected EOF
close (fd_per);
exit_clean(EXIT_454);
}
if ( MailFrom[0] == 0 && RcptTo[0][0] == 0 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: no envelope information, deferred exit\n", getppid());
}
exit_clean(EXIT_454);
}
/* close the addr file */
if ( close(fd_per) == -1 ) {
exit_clean(EXIT_400);
}
#if defined(ENABLE_DSPAM)
if (getenv("RELAYCLIENT")==0) {
for (i =0; i<MaxRcptTo; ++i){
if ( strncmp("spam@", RcptTo[i], 5) == 0 || strncmp("nospam@", RcptTo[i], 7) == 0 ) {
#ifdef ENABLE_CUSTOM_SMTP_REJECT
snprintf(RejectMsg,sizeof(RejectMsg),
"DYou are not authorized to send email to this address");
write(4,RejectMsg, strlen(RejectMsg));
exit_clean(EXIT_MSG);
#else
exit_clean(EXIT_500);
#endif
}
}
}
#endif
#ifdef ENABLE_PER_DOMAIN
/* setup the per domain values for checking virus or spam */
set_per_domain();
#endif
#ifdef ENABLE_REGEX
/* check for regexs to block */
if ( check_regex() == 2 ) {
log_message("REGEX", VirusName, 0);
#ifdef ENABLE_CUSTOM_SMTP_REJECT
snprintf(RejectMsg,sizeof(RejectMsg),
"DYour email was rejected because it matches a filter (%s)",
VirusName );
write(4,RejectMsg, strlen(RejectMsg));
exit_clean(EXIT_MSG);
#else
exit_clean(EXIT_500);
#endif
}
#endif
#if defined(ENABLE_DSPAM)
/* The following code is copied from the Spamassassin check below, with the proper adjustments */
/* re-open the file read only */
if ( (fd = open(message_name, O_RDONLY)) == -1 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: spam can not open file: %s\n", getppid(), message_name);
}
exit_clean(EXIT_400);
}
/* set the standard input to be the new file */
if ( fd_move(0,fd) == -1 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: spam could not fd_move\n", getppid());
}
exit_clean(EXIT_400);
}
/* optionally check for spam with DSPAM */
snprintf(spam_message_name, sizeof(spam_message_name), "spamc.msg.%s", unique_ext);
IsSpam = 0;
ret = check_dspam();
switch ( ret ) {
/* spamassassin not enabled for this domain */
case 2:
/* re-open the message file file read only */
/* do nothing, message_name gets openend in any case*/
break;
/* spam detected, refuse message */
case 1:
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: DSPAM reported message as being SPAM\n", getppid());
}
close(fd);
#ifdef QUARANTINEDIR
quarantine_msg(spam_message_name);
/* put message in quarantine */
#endif
#ifdef ENABLE_DROPMSG
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: droping the message\n", getppid());
}
exit_clean(EXIT_0);
/* Drop the message, returning success to sender. */
#else
#ifdef ENABLE_CUSTOM_SMTP_REJECT
snprintf(RejectMsg,sizeof(RejectMsg), "DYour email is considered spam (%.4f probability)", SpamProbability );
write(4,RejectMsg, strlen(RejectMsg));
exit_clean(EXIT_MSG);
#else
exit_clean(EXIT_500);
#endif
#endif
break;
/* dspam processed message and no spam detected */
case 0:
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: DSPAM reported message as NOT being SPAM\n", getppid());
}
/* open the spam file read only */
strncpy(message_name,spam_message_name,BUFFER_SIZE);
break;
/* errors , return temporary error */
default:
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: check_dspam had an error ret: %d\n", getppid(), ret);
}
close(fd);
exit_clean(EXIT_400);
}
#endif
#ifdef ENABLE_SPAM
/* attachments-size-limit patch */
if ( DebugFlag > 0 ) fprintf(stderr, "simscan: size limit is %d bytes\n", size_limit);
if (msgsize >= size_limit) {
// if ( DebugFlag > 0 ) { // now logging also when debug is off
fprintf(stderr, "simscan: big file (%lu bytes); size limit is %d bytes; skipping SpamAssassin\n",
(unsigned long) msgsize, size_limit);
// }
} else {
/* re-open the file read only */
if ( (fd = open(message_name, O_RDONLY)) == -1 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan: spam can not open file: %s\n", message_name);
}
exit_clean(EXIT_400);
}
/* set the standard input to be the new file */
if ( fd_move(0,fd) == -1 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan: spam could not fd_move\n");
}
exit_clean(EXIT_400);
}
/* optionally check for spam with spamassassin */
snprintf(spam_message_name, sizeof(spam_message_name), "spamc.msg.%s", unique_ext);
ret = check_spam();
switch ( ret ) {
/* spamassassin not enabled for this domain */
case 2:
/* re-open the message file file read only */
/* do nothing, message_name gets openend in any case*/
break;
/* spam detected, refuse message */
case 1:
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan: check_spam detected spam refuse message\n");
}
close(fd);
#ifdef QUARANTINEDIR
quarantine_msg(message_name);
/* put message in quarantine */
#endif
#ifdef ENABLE_DROPMSG
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan: droping the message\n");
}
exit_clean(EXIT_0);
/* Drop the message, returning success to sender. */
#else
#ifdef ENABLE_CUSTOM_SMTP_REJECT
snprintf(RejectMsg,sizeof(RejectMsg),
"DYour email is considered spam (%.2f spam-hits)", SpamHits );
write(4,RejectMsg, strlen(RejectMsg));
exit_clean(EXIT_MSG);
#else
exit_clean(EXIT_500);
#endif
#endif
break;
/* spamassassin processed message and no spam detected */
case 0:
/* open the spam file read only */
// strncpy(message_name,spam_message_name,BUFFER_SIZE);
memcpy(message_name,spam_message_name,BUFFER_SIZE);
break;
/* errors , return temporary error */
default:
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan: check_spam had an error ret: %d\n", ret);
}
close(fd);
exit_clean(EXIT_400);
}
}
#endif
#if (VIRUSSCANNER==1 || ENABLE_ATTACH==1) && DO_RIPMIME==1
/* break the email msg into mime parts */
if ( run_ripmime() != 0 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: ripmime error\n", getppid());
}
exit_clean(EXIT_400);
}
#endif
#ifdef ENABLE_ATTACH
/* check for attachments to block */
if ( check_attach() > 0 ) {
log_message("ATTACH", AttachName, 0);
#ifdef ENABLE_CUSTOM_SMTP_REJECT
snprintf(RejectMsg,sizeof(RejectMsg),
"DYour email was rejected because it contains a bad attachment: %s",
AttachName );
write(4,RejectMsg, strlen(RejectMsg));
exit_clean(EXIT_MSG);
#else
exit_clean(EXIT_500);
#endif
}
#endif
#ifdef ENABLE_CLAMAV
/* Run ClamAntiVirus, exit on errors */
if (FoundVirus == 0) {
ret = check_clam();
switch ( ret ) {
case -2:
if ( DebugFlag > 0 ) { fprintf(stderr, "simscan:[%d]: clamdscan disabled\n", getppid()); }
break;
case -1:
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: fatal error executing clamdscan\n", getppid());
}
exit_clean(EXIT_400);
break;
case 1:
FoundVirus=1;
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: clamdscan detected a virus\n", getppid());
}
break;
case 2:
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: fatal error executing clamdscan\n", getppid());
}
exit_clean(EXIT_400);
break;
default:
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: normal clamdscan return code: %d\n", getppid(), ret);
}
break;
}
}
#endif
#ifdef ENABLE_TROPHIE
if (FoundVirus == 0) {
ret = check_trophie();
switch(ret) {
case 0:
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: trophie found no virus\n", getppid());
}
break;
case 1:
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: trophie found virus\n", getppid());
}
FoundVirus=1;
break;
case -1:
/* disabled */
if ( DebugFlag > 0 ) { fprintf(stderr, "simscan:[%d]: trophie disabled\n", getppid()); }
break;
default:
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: some temp. error occured with trophie\n", getppid());
}
exit_clean(EXIT_400);
}
}
#endif
#ifdef VIRUSSCANNER
/* check for viri, return error if found or a temporary problem */
if ( FoundVirus == 1 ) {
#ifdef ENABLE_DROPMSG
log_message("VIRUS DROPPED", VirusName, 0);
/* Drop the message, returning success to sender. */
exit_clean(EXIT_0);
#else
log_message("VIRUS", VirusName, 0);
#ifdef ENABLE_CUSTOM_SMTP_REJECT
snprintf(RejectMsg,sizeof(RejectMsg),
"DYour email was rejected because it contains the %s virus",
VirusName );
write(4,RejectMsg, strlen(RejectMsg));
exit_clean(EXIT_MSG);
#else
exit_clean(EXIT_500);
#endif
#endif
}
#endif
/* re-open the file read only */
if ( (fd = open(message_name, O_RDONLY)) == -1 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: could not re-open file: %s\n", getppid(), message_name);
}
exit_clean(EXIT_400);
}
/* re-open the address read only */
if ( (fd_per = open(addr_name, O_RDONLY)) == -1 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: could not re-open address: %s\n", getppid(), addr_name);
}
exit_clean(EXIT_400);
}
/* set the standard input to be the new file */
if ( fd_move(1,fd_per) == -1 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: could not fd_move\n", getppid());
}
exit_clean(EXIT_400);
}
if ( DebugFlag > 0 ) fprintf(stderr, "simscan:[%d]: done, execing qmail-queue\n", getppid());
if ( pipe(pim) != 0 ) return(-1);
/* fork qmail-queue */
switch(pid = vfork()) {
case -1:
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: error forking qmail-queue\n", getppid());
}
close(pim[0]);
close(pim[1]);
exit_clean(EXIT_400);
case 0:
close(pim[1]);
dup2(pim[0],0);
execl(qmail_queue, qmail_queue, (char *)NULL);
_exit(111);
}
close(pim[0]);
#ifdef ENABLE_RECEIVED
gettimeofday(&stop,(struct timezone *) 0);
utime=SECS(stop)-SECS(start);
tm = gmtime(&start.tv_sec);
snprintf(buffer,sizeof(buffer), "Received: (simscan %s ppid %d pid %d t %.4fs)\n"
" (scanners: %s); %02d %s %04d %02d:%02d:%02d -0000\n",
VERSION, getppid(), getpid(), utime, runned_scanners[0] ? runned_scanners : "none",
tm->tm_mday,monthname[tm->tm_mon],tm->tm_year,tm->tm_hour,tm->tm_min,tm->tm_sec);
if ( write(pim[1], buffer,strlen(buffer)) == -1 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: error writing received line\n", getppid());
}
exit_clean(EXIT_400);
}
#endif
/* write the message to qmail-queue */
while( (ret = read(fd, buffer, sizeof(buffer))) > 0 ) {
if ( write(pim[1], buffer,ret) == -1 ) {
if ( DebugFlag > 0 ) {
fprintf(stderr,
"simscan:[%d]: error writing msg to qmail-queue error: %d\n", getppid(), errno);
}
exit_clean(EXIT_400);
}
}
close(pim[1]);
close(fd);
/* wait for qmail-queue to finish */
if (waitpid(pid,&qstat, 0) == -1) {
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: error forking qmail-queue (back in simscan)\n", getppid());
}
exit_clean(EXIT_400);
}
/* hand the email to the qmail-queue */
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: qmail-queue exited %d\n", getppid(), WEXITSTATUS(qstat));
}
/* remove the working files */
if ( DebugFiles < 2 ) {
if ( remove_files(workdir) == -1 ) {
exit_clean(EXIT_400);
}
}
/* pass qmail-queue's exit status on */
_exit(WEXITSTATUS(qstat));
/* suppress warning messages */
return(0);
}
void lowerit(char *input)
{
while(*input!=0) {
if ( isupper((u_char)*input) ) {
*input = (u_char)tolower((u_char)*input);
}
++input;
}
}
/*
* move a file descriptor
*/
int fd_move(int to,int from)
{
if (to == from) return 0;
if (fd_copy(to,from) == -1) return -1;
close(from);
return 0;
}
/*
* copy a file descriptor
*/
int fd_copy(int to,int from)
{
if (to == from) return 0;
if (fcntl(from,F_GETFL,0) == -1) return -1;
close(to);
if (fcntl(from,F_DUPFD,to) == -1) return -1;
return 0;
}
#ifdef ENABLE_RECEIVED
void add_run_scanner(char *key){
int ret;
int fd;
uint32 dlen;
unsigned int keylen;
char tmpbuf[256];
char *data;
if ( DebugFlag > 1 ) fprintf(stderr, "simscan:[%d]: cdb looking up version %s\n", getppid(), key);
snprintf(tmpbuf,sizeof(tmpbuf), "%s/simversions.cdb", CONTROLDIR);
if ( (fd = open(tmpbuf, O_RDONLY)) == -1 ) {
return;
}
keylen = strlen(key);
ret = cdb_seek(fd,key,keylen,&dlen);
if ( ret <= 0 ) {
close(fd);
return;
}
data = calloc(sizeof(char),dlen+1);
if ( data == NULL ) exit_clean(EXIT_400);
ret = read(fd,data,dlen);
close(fd);
snprintf(runned_scanners+strlen(runned_scanners),MAX_EMAIL-strlen(runned_scanners)," %s: %s",key,data);
if ( DebugFlag > 2 ) fprintf(stderr, "simscan:[%d]: runned_scanners is %s\n", getppid(), runned_scanners);
if ( DebugFlag > 2 ) fprintf(stderr, "simscan:[%d]: found %s\n", getppid(), data);
}
#endif
#if (VIRUSSCANNER || ENABLE_ATTACH) && DO_RIPMIME==1
/*
* break the email into mime parts for scanning
*/
int run_ripmime()
{
int pid;
int rmstat;
/* fork ripmime */
switch(pid = vfork()) {
case -1:
return(-1);
case 0:
close(1);
close(2);
execl(RIPMIME, "ripmime", "--disable-qmail-bounce",
"-i", message_name, "-d", workdir, NULL );
_exit(-1);
}
/* wait for ripmime to finish */
if (waitpid(pid,&rmstat, 0) == -1) {
return(-1);
}
/* check if the child died on a signal */
if ( WIFSIGNALED(rmstat) ) return(-1);
/* if it exited okay, return the status */
if ( WIFEXITED(rmstat) ) {
return(WEXITSTATUS(rmstat));
}
/* should not reach here */
return(-1);
}
#endif
/*
* scan for viri
*/
#ifdef ENABLE_CLAMAV
int check_clam()
{
int pid;
int rmstat;
int pim[2];
int file_count;
#ifdef ENABLE_PER_DOMAIN
if ( PerDomainClam == 0 ) return(-2);
#endif
if ( DebugFlag > 0 ) {
fprintf(stderr, "simscan:[%d]: calling clamdscan\n", getppid());
}
if ( pipe(pim) != 0 ) return(-1);
/* fork clamdscan */
switch(pid = vfork()) {
case -1:
close(pim[0]);
close(pim[1]);
return(-1);
case 0:
close(pim[0]);
dup2(pim[1],1);
close(pim[1]);
execve(CLAMDSCAN, viri_args, 0);
_exit(-1);
}
close(pim[1]);
dup2(pim[0],0);
close(pim[0]);
InClamHeaders = 1;
memset(buffer,0,sizeof(buffer));
while((file_count=read(0,buffer,BUFFER_SIZE))>0) {