File: parse.c

package info (click to toggle)
sma 1.4-3.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 620 kB
  • sloc: ansic: 6,291; sh: 163; perl: 148; makefile: 67
file content (1049 lines) | stat: -rw-r--r-- 23,933 bytes parent folder | download | duplicates (4)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
/*
 * sma -- Sendmail log analyser
 *
 * Copyright (c) 2000 - 2003 Jarkko Turkulainen. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY JARKKO TURKULAINEN ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL JARKKO TURKULAINEN BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Date: 2003/04/27 15:43:33 $
 */

#include "sma.h"
#include <ctype.h>

/* Delivery status flags */
#define STATUS_SENT	1
#define STATUS_DEF	2	
#define STATUS_USER	3
#define STATUS_QUEUE	4
#define STATUS_HOST	5
#define STATUS_SERV	6
#define STATUS_OTHER	7	

/* Message ID flags: */
#define NOT_DELIVERED	0
#define FIRST_DELIVERY	1
#define DELIVERED	2

/* Message ID checking mode */
#define KEEP	0
#define REMOVE	1

/* Action flags */
#define	ACTION_FROM	0
#define	ACTION_TO	1	
#define	ACTION_SYSERR	2
#define	ACTION_RULESET	3
#define	ACTION_DAEMON	4
#define	ACTION_DATAB	5
#define	ACTION_DSN	6
#define	ACTION_USER	7
#define ACTION_UNKNOWN	100

/* ACTION_SYSERR flags */
#define	OTHER		0
#define	HOPCOUNT	1
#define	LOOP		2

#ifdef USE_REGEXP
	int check_filter(const char *, regex_t *, const char *);
#else
	int check_filter(const char *, const char *);
#endif

/*
 * Do parsing for each input file
 */
void
parse(FILE *fp, const char *file) {

	/* input buffer */
	char line[2048];

	/* plenty of pointers for line splitting: */
	static const char *env_rec[128];
	char *head[64];
	char *info[64];

	/* collecting */
	char *action, *dsn, *idptr;
	char *messageid = NULL;
	const char *tmphost;
	int logformat, hcount, icount;
	int status = 0, idsize = 0, gf, pf = 0, actn = 0, sunos = 0; 
	int actflag, year, mon, day, hh, min, sec;
	struct msgid *tmsgid = NULL;

	/* general purpose: */
	const char *p = NULL;
	const char *s = NULL;
	int i, k, lcount;

	/* status and ruleset pointers */
	const char *rulestr = NULL;
	const char *relstr = NULL;
	const char *statstr = NULL;

	/* temporary pointer to host struct: */
	struct host *hptr;

	/* Start read file line by line: */
	lcount = 0;
	while (fgets(line, 2048, fp)) {
		lcount++;

		/* null-terminate: */
		line[strlen(line)-1] = '\0';
		memset(head, 0, 64);
		memset(info, 0, 64);

		/* split line initially with ',': */
		info[0] = line;
		for (i = 1, k = 0; line[k] != '\0'; )  {
			if (line[k] == ',') {
				line[k] = '\0';
				k++;
				if (line[k] == ' ')
					k++;
				info[i] = &line[k];
				i++;
			} else
				k++;
		}
		icount = i;

		/* split first info with ' ': */
		head[0] = info[0];
		for (i = 1, k = 0; info[0][k] != '\0'; )  {
			if (info[0][k] == ' ') {
				info[0][k] = '\0';
				k++;
				while(info[0][k] == ' ')
					k++;
				head[i] = &info[0][k];
				i++;
			} else
				k++;
		}
		hcount = i;

		/*
		 * Initial sanity check:
		 * Try to figure out the input file format
		 * Currently supported are syslog and NT
		 *
		 * Sendmail for NT
		 * Assume that the second field is "sendmail" ..
		 * This may be wrong, don't know NT version enough.
		 */
		idptr = NULL; action = NULL; actn = 0; dsn = NULL;
		if (head[5] && !strncmp("sendmail", head[2], 7)) {
			logformat = 1;
			if (sscanf(head[1], "%d:%d:%d", &hh, &min, &sec) != 3) {
				if (!qflag) fprintf(stderr, 
					  "%s: not NT log, skipping line %d, "
					  "file %s\n", pname, lcount, file);
				continue;
			}
			if (sscanf(head[0], "%d/%d/%d",&mon,&day,&year) != 3) {
				if (!qflag) fprintf(stderr, 
					  "%s: not NT log, skipping line %d, "
					  "file %s\n", pname, lcount, file);
				continue;
			}
			/* We assume things fixed with NT log.. */
			if (hcount >= 6) {
				idptr = head[4];
				action = head[5];
				actn = 5; dsn = head[6];
				mon--;
			}
		/*
		 * Normal syslog
		 */
		} else {
			logformat = 0;
			if (!head[5] || (Lchar && strncmp(Lchar, head[4],
				strlen(Lchar)))) {
				if (!qflag) fprintf(stderr, 
					  "%s: not syslog, skipping line %d, "
					  "file %s\n", pname, lcount, file);
				continue;
			}
			if (sscanf(head[2], "%d:%d:%d", &hh, &min, &sec) != 3) {
				if (!qflag) fprintf(stderr, 
					  "%s: not syslog, skipping line %d, "
					  "file %s\n", pname, lcount, file);
				continue;
			}
			day = atoi(head[1]);
			mon = conv_mon(head[0]);
			
			/* SunOS 5.x with ID generation enabled: */
			if (!strncmp("[ID", head[5], 3)) {
				sunos = 3;
			} else sunos = 0;
			/* 
			 * Now, we run through the first info-vector (head)
			 * and try to find useful keywords. If found, copy
			 * the pointers as idptr, action and dsn. Action
			 * flags are later set according to these keywords.
			 */
			for (k = 6+sunos; k < hcount; k++ ) {
				/* "normal" delivery lines */
				if (!strncmp("daemon", head[k], 6) ||
				    !strncmp("ruleset", head[k], 7) || 
				    !strncmp("to=", head[k], 3) || 
				    !strncmp("from=", head[k], 5) || 
				    !strncmp("SYSERR", head[k], 6) || 
				    !strncmp("database", head[k], 8)) {
					actn = k;
					idptr = head[k-1];
					action = head[k];
					break;

				/* DSN, and other strange stuff: */
				} else if (!strncmp("DSN", head[k], 3) ||
				    !strncmp("User", head[k], 4) || 
				    !strncmp("postmaster", head[k],10) || 
				    !strncmp("return", head[k], 6)) {
					idptr = head[k-2];
					action = head[k-1];
					dsn = head[k];
					break;
				}
			}
		}
		/* 
		 * Permitted actions are defined here.
		 *
		 * We test the action, dsn, idptr and other fields for
		 * keywords and set the flag actflag according to a
		 * defined action. These actions are later performed
		 * with a switch(actflag) statement.
		 *
		 */
		actflag = ACTION_UNKNOWN;
		if (idptr && !strncmp("ruleset", idptr, 7)) {
			actflag = ACTION_RULESET;
		} else if (action && !strncmp("ruleset", action, 7)) {
			actflag = ACTION_RULESET;
		} else if (action && !strncmp("from=", action, 5)) {
			actflag = ACTION_FROM;
		} else if (action && !strncmp("to=", action, 3)) {
			actflag = ACTION_TO;
		} else if (action && !strncmp("SYSERR", action, 6)) {
			actflag = ACTION_SYSERR;
		} else if (action && !strncmp("daemon", action, 6)) {
			actflag = ACTION_DAEMON;
		} else if (action && !strncmp("database", action, 8)) {
			actflag = ACTION_DATAB;
		} else if (dsn && (!strncmp("User", dsn, 4))) {
			actflag = ACTION_USER;
		} else if (dsn) {
			actflag = ACTION_DSN;
		} else continue;

		/*
		 * Initialize the host structure
		 * If the hostname is not given, use syslog or 
		 * (in case of NT) default string
		 */
		if (Hchar)
			tmphost = Hchar;
		else if (logformat)
			tmphost = HOSTNAME;
		else
			tmphost = head[3];
		hptr = init_host(tmphost);

		/* 
		 * Rejected before SMTP "MAIL FROM"
		 * This should be under switch(actflag) but isn't really
		 * a delivery, so we test it here..
		 */
		if (!strncmp("ruleset", idptr, 7)) {
                        hptr->rule++;
			/* Find the reject-field: */
			for (k = 1; k < icount; k++ ) {
				if (!strncmp("relay=", info[k], 6))
					relstr = get_relay_name(info[k]+6);
				else if (!strncmp("reject=", info[k], 7))
					rulestr = info[k]+7;
			}
			if (!rulestr) continue;
			if (!relstr) relstr = BOUNCE_HOST;
			if (rsnum) update_ruleset(hptr, rulestr, relstr);
			continue;
		}

		/* Take the current time: */
		hptr->cdtime = (time_t)conv_time(mon, day, hh, min, sec);

		/* Check if we are allowed to proceed: */
		if (sstime && (hptr->cdtime < sstime))
			continue;
		if (eetime && (hptr->cdtime > eetime))
			continue;

		/* is the host brand new? */
		if (hptr->fhost == 0) {
			/* Make the first time as ftime, cdtime and ltime: */
			hptr->fhost = 1;
			hptr->ftime = hptr->cdtime;
			hptr->ltime = hptr->ftime;
		}

		/* Check ftime and ltime: */
		if (hptr->cdtime > hptr->ltime)
			hptr->ltime = hptr->cdtime;
		if (hptr->cdtime < hptr->ftime)
			hptr->ftime = hptr->cdtime;

		/* Calculate weekday: */
		hptr->lday = (localtime(&hptr->cdtime))->tm_wday;

		/* Debug */
		if (vflag && (actflag == ACTION_DSN || actflag == ACTION_FROM ||
		    actflag == ACTION_TO || actflag == ACTION_RULESET ||
		    actflag == ACTION_USER)) {
			fprintf(stderr, "\n<< DEBUG >> file %s, line %d\n",
				file, lcount);
			fprintf(stderr, "  msg id=%s\n", idptr);
		}

		/* 
		 * Switch the possible actions
		 * DSN stuff
		 * These include DSN, postmaster notify, return to sender...
		 */
		switch(actflag) {
		case ACTION_DSN:
			if (vflag) fprintf(stderr, "  DSN: %s\n", idptr);

			/* Find the message ID */
			tmsgid = get_msgid(hptr, idptr);
			if (tmsgid == NULL)
				continue;
			/* Remove it */
			remove_msgid(hptr, idptr);

			/* This is a completely new mail */
			p = bechar;
			s = BOUNCE_HOST;

			/*
			 * Sender Envelope and Relay filtering
			 */
#ifdef USE_REGEXP
			if (check_filter(sef, &csef, p) &&
				check_filter(srf, &csrf, s)) {
#else
			if (check_filter(sef, p) && check_filter(srf, s)) {
#endif
				/* Message ID */
				update_msgid(hptr, p, s, action, hh, 
					day, 1, 200, "<DSN>");
			/* Nope, we are filtered: */
			} else
				continue;
			break;

		/* User unkown: */
		case ACTION_USER:
			if (vflag) fprintf(stderr, 
			  "  User unkown: %s\n", idptr);
			update_omsgid(hptr, idptr, 0);
			break;

		/* "from" envelope:  */
		case ACTION_FROM:
			p = NULL;
			s = NULL;

			/* check omsgidtab */
			if (check_omsgid(hptr, idptr))
				continue;

			/* fetch the name: */
			p = get_name(action+5);
			if (vflag) fprintf(stderr, "  from=%s\n", p);

			/* follow on: */
			for (k = 1; k < icount; k++) {

				/* size of the message: */
				if (!strncmp("size", info[k], 4)) {
					hptr->lsize = atoi(info[k]+5);

				/* msgid */
				} else if (!strncmp("msgid", info[k], 5)) {
					messageid = info[k]+6;

				/* input relay */
				} else if (!strncmp("relay", info[k], 5)) {
					s = get_relay_name(info[k]+6);

				/* no of recipients to follow */
				} else if (!strncmp("nrcpts", info[k], 6))
					idsize = atoi(info[k]+7);
			}
			/* Check relay */
			if (!s) s = BOUNCE_HOST;
			if (vflag) fprintf(stderr, "  relay=%s\n", s);

			/* Initialize messageid */
			if (!messageid) messageid = "";

			/*
			 * Sender Envelope and Relay filtering
			 */
#ifdef USE_REGEXP
			if (check_filter(sef, &csef, p) &&
				check_filter(srf, &csrf, s)) {
#else
			if (check_filter(sef, p) && check_filter(srf, s)) {
#endif
				update_msgid(hptr, p, s, idptr, hh, day,
					idsize, hptr->lsize, messageid);
			/* Nope, we are filtered: */
			} else
				continue;
			break;

		/* "to" envelpe: */
		case ACTION_TO:
			p = NULL;
			s = NULL;

			/* Check msgidtab */
			tmsgid = get_msgid(hptr, idptr);
			if (tmsgid == NULL)
				/* Bogus address or (from=) side filter */
				continue;
			hptr->lsize = tmsgid->size;

			/* Check delivery flag: */
			if (tmsgid->flag == NOT_DELIVERED)
				/* This is a fresh one: */
				tmsgid->flag = FIRST_DELIVERY;

			/* fetch name and make entry: */
			i = 0;
			memset(env_rec, 0, (size_t)sizeof(char) * 128);
			p = get_name(action+3);
			env_rec[i] = p;

			for (k = 1; k < icount; k++) {
				if (!strncmp("delay", info[k], 5) ||
				    !strncmp("ctladdr", info[k], 7))
					break;
				else 
					/* more recipients: */
					env_rec[++i] = get_name(info[k]);
			}
			/* now for the rest of the cases: */
			for (; k < icount; k++ ) {
				if (!strncmp("stat", info[k], 4)) {
					statstr = info[k]+5;
					if (!strncmp("Sent", info[k]+5, 4) ||
					    !strncmp("sent", info[k]+5, 4))
						status = STATUS_SENT;
					else if (!strncmp("Deferred", 
					  info[k]+5, 8)) 
						status = STATUS_DEF;
					else if (!strncmp("User", 
					  info[k]+5, 4))
						status = STATUS_USER;
					else if (!strncmp("Host", 
					  info[k]+5, 4))
						status = STATUS_HOST;
					else if (!strncmp("queued", 
					  info[k]+5, 6))
						status = STATUS_QUEUE;
					else if (!strncmp("Service",
					  info[k]+5, 7))
						status = STATUS_SERV;
					else
						status = STATUS_OTHER;

				} else if (!strncmp("relay", info[k], 5)) {

					/* output relay */
					s = get_relay_name(info[k]+6);
				}
			} 
			/* If no relay host, make as bouncehost: */
			if (!s) s = BOUNCE_HOST;
			if (vflag) fprintf(stderr, "  relay=%s\n", s);

			i = 0;
			gf = 0;
			while (env_rec[i]) {
				if (vflag) fprintf(stderr, 
					"  to=%s\n", env_rec[i]);
				/*
				 * Recipient Envelope and Relay filtering
				 */
#ifdef USE_REGEXP
				if (check_filter(ref, &cref, env_rec[i]) && 
				  check_filter(rrf, &crrf, s)) {
					gf = 1;
					pf = 1;
#else
				if (check_filter(ref, env_rec[i]) && 
				  check_filter(rrf, s)) {
					gf = 1;
					pf = 1;
#endif
				} else pf = 0;
				if (pf) {
					/*
					 * Update output side stats if
					 * the format is ASCII or HTML:
					 */
					if (Oflag != FORMAT_CLOG) {
						if (status == STATUS_SENT) {
							update_out(hptr,
							  env_rec[i],
							  hptr->lsize);
							if (epnum)
							update_envpair(hptr,
							  tmsgid->sender,
							  env_rec[i],
							  hptr->lsize);
						} else {
							update_out(hptr, 
							  env_rec[i], 0);
							if (epnum)
							update_envpair(hptr,
							  tmsgid->sender,
							  env_rec[i], 0);
						}
						hptr->onum++;
					/* Custom log: */
					} else if (!clsflag || (clsflag &&
						   (status == STATUS_SENT))) {
						printclog(
							hptr->cdtime,
							tmphost,
							(int)hptr->lsize,
							tmsgid->msgid,
							tmsgid->sender,
							tmsgid->relay,
							env_rec[i], s,
							(status == STATUS_SENT)
							  ? 1 : 0);
					}
					/* Checkout msgid */
					check_msgid(hptr, idptr, KEEP);
				}
				i++;
			}
			/* Remove msgid if not ASCII or HTML: */
			if (Oflag == FORMAT_CLOG) {
				if (gf) {
					if (status == STATUS_SENT)
					  check_msgid(hptr, idptr, REMOVE);
				} else
					/* Remove useless message ID: */
					remove_msgid(hptr, idptr);

				continue;
			}

			/* 
			 * Update global stats only once per delivery
			 */
			if (gf) {
				/* Update output relay: */
				hptr->ronum++;
				if (status == STATUS_SENT) {
					update_rout(hptr, s, hptr->lsize);
					/* update relay pairs */
					if (rpnum) update_relpair(hptr,
					  tmsgid->relay,
					  s, hptr->lsize);
				} else {
					update_rout(hptr, s, 0);
					if (rpnum) update_relpair(hptr,
					  tmsgid->relay, s, 0);
				}

				/* Update the global stats: */
				hptr->gonum++;
				hptr->ohh[hh]++;
				hptr->odd[hptr->lday]++;
				if (status == STATUS_SENT)
					hptr->osize += hptr->lsize;

				/* Check status flag: */
				if (status == STATUS_SENT) {
					hptr->sent++;
					if (stnum) update_status(hptr, "Sent");
				} else {
					if (stnum) update_status(hptr, statstr);
					switch(status) {
						case STATUS_DEF:
							hptr->defe++;
							break;
						case STATUS_HOST:
							hptr->hunk++;
							break;
						case STATUS_USER:
							hptr->uunk++;
							break;
						case STATUS_QUEUE:
							hptr->queu++;
							break;
						case STATUS_SERV:
							hptr->service++;
							break;
						case STATUS_OTHER:
							hptr->other++;
							break;
					}
				}

				/*
				 * If this is a first delivery,  update
				 * input side statistics:
				 */
				if (tmsgid->flag == FIRST_DELIVERY) {
					/* Add envelope: */	
					hptr->inum++;
					update_in(hptr, tmsgid->sender, 
						hptr->lsize);

					/* Add relay: */
					hptr->rinum++;
					update_rin(hptr, tmsgid->relay, 
						hptr->lsize);

					/* Update statistics: */
					hptr->ihh[tmsgid->hh]++;
					hptr->idd[hptr->lday]++;
					hptr->size += hptr->lsize;
					hptr->isize += hptr->lsize;

					tmsgid->flag = DELIVERED;
				}

				/* 
				 * If the message was sent, remove useless
				 * message ID from the chain
				 */
				if (status == STATUS_SENT)
					check_msgid(hptr, idptr, REMOVE);

			} else
				/* Remove useless message ID: */
				remove_msgid(hptr, idptr);
			break;

		/* SYSERR: */
		case ACTION_SYSERR:
			if (vflag) {
				fprintf(stderr, "\n<< DEBUG >> file %s, line %d\n",
					file, lcount);
				fprintf(stderr, "  SYSERR");
			}
			for (i = 0, k = actn; k < hcount; k++) {

				/* Too many hops: */
				if (!strncmp("Too", head[k], 3)) {
					if (!head[k+2])
						continue;
					if (!strncmp("many", head[k+1], 4) &&
					    !strncmp("hops", head[k+2], 4))
						i = HOPCOUNT;

				/* Local configuration error: */
				} else if (!strncmp("config", head[k], 6)) {
					if (!head[k+1])
						continue;
					if (!strncmp("error:", head[k+1], 6))
						i = LOOP;
				} 
				/* Other SYSERR (i = 0)*/

			}
			switch(i) {
				case HOPCOUNT:
					if (vflag)
					  fprintf(stderr, " (hop count)\n");
					hptr->hopc++;
					break;
				case LOOP:
					if (vflag)
					  fprintf(stderr, " (mail loop)\n");
					hptr->lcerror++;
					break;
				case OTHER:
					if (vflag)
					  fprintf(stderr, " (other)\n");
					hptr->oserror++;
					break;
			}
			break;

		/* ruleset based rejection: */
		case ACTION_RULESET:
			if (vflag) fprintf(stderr, "  ruleset: %s\n", idptr);
			update_omsgid(hptr, idptr, 0);
			hptr->rule++;
			
			/* Find the reject-field: */
			for (k = 1; k < icount; k++ ) {
				if (!strncmp("relay=", info[k], 6))
					relstr = get_relay_name(info[k]+6);
				else if (!strncmp("reject=", info[k], 7))
					rulestr = info[k]+7;
			}
			if (!rulestr) continue;
			if (!relstr) relstr = BOUNCE_HOST;
			if (rsnum) update_ruleset(hptr, rulestr, relstr);
			break;

		/* sendmail daemon started: */
		case ACTION_DAEMON:
			if (vflag) {
				fprintf(stderr, "\n<< DEBUG >> file %s, line %d\n",
					file, lcount);
				fprintf(stderr, "  daemon restart\n");
			}
			hptr->dstart++;
			break;

		/* alias database rebuild: */
		case ACTION_DATAB:
			if (vflag) {
				fprintf(stderr, "\n<< DEBUG >> file %s, line %d\n",
					file, lcount);
				fprintf(stderr, "  alias table rebuilt\n");
			}	
			hptr->alias++;
			break;

		default:
			continue;
		}
	}
}

/*
 * get_name - strip sender/recipient name/domain.
 */

const char *
get_name(char *name) {
	char *tmp, *tmp2;
	int i;

	/* downcase everything if requested */
	if (dcaddrflag) {
		for ( i=0; name[i]; i++) {
			name[i] = tolower(name[i]);
		}
	}
	/* null-sender? */
	if (!strncmp("<>", name, 2))
		return("<>");

	/* find the separating '@'-char: */
	while (*name == '<' || *name == '"' || *name == '\'')
		name++;
	tmp = tmp2 = name;

	while (*name++ != '@' && *name)
		;
	if (!*name)
		name = tmp;

	/* find the first char of the return pointer: */
	if (dflag)
		tmp = name;
	else {
		while (name != tmp2) {
			if ((*name == '<') || (*name == '"') ||
			  (*name == '\'') || (*name == ' ')) {
				name++;
				break;
			} else
				name--;
		}
		tmp = name;
	}

	/* cut the string: */
	while (*name) {
		if ((*name == '>') || (*name == '"') ||
		  (*name == '\'') || (*name == ' ')) {
			*name = '\0';
			break;
		} else
			name++;
	}
	return tmp;
}

/* get relay name: */
const char *
get_relay_name(char *name) {
	char *tmp;

	tmp = name;
	/* 
	 * Search for space or (in case of identd)
	 * '@'-char. Make the return string point
	 * to next char.
	 */
	while (*name) {
		if (*name == '@') {
			tmp = name + 1;
			break;
		}
		name++;
	}
	if (!*name)
		name = tmp;

	/*
	 * Do not take the IP-address in brackets
	 */
	while (*name) {
		if ((*name == ' ') || (*name == ',')) {
			*name = '\0';
			break;
		} else
			name++;
	}
	if (*(name-1) == '.')
		*(name-1) = '\0';

	return tmp;
}

#ifdef USE_REGEXP
int
check_filter(const char *s, regex_t *rgp, const char *text) {

	if (!s)
		return(1);
	else if (*s == '!') {
		if (!regexec(rgp, text, 0, NULL, 0))
			return(0);
		else
			return(1);
	} else if (!regexec(rgp, text, 0, NULL, 0))
		return(1);
	else
		return(0);
}
#else
int
check_filter(const char *s, const char *text) {

	if (!s)
		return(1);
	else if (*s == '!') {
		if (strstr(text, s+1))
			return(0);
		else
			return(1);
	} else if (strstr(text, s))
		return(1);
	else
		return(0);
}
#endif

/*
 * Printing routine for Custom Log format
 */
void
printclog(time_t sma_time, const char *host, int size, const char *id,
	const char *from, const char *frelay, const char *to,
	const char *trelay, int status) {

	/* time struct: */
	struct tm *tmptime;

	/* Month tab */
	const char *montab[] = {
		"Jan", "Feb", "Mar", "Apr", "May",
		"Jun", "Jul", "Aug", "Sep", "Oct",
		"Nov", "Dec"
	};

	/* Temporary pointer */
	const char *frm;

	/* default format: */
	if (!cfchar) {
	    fprintf(ofp, "%d %s %d %s %s %s %s %d\n",
		(int)sma_time, host, size, from, frelay, to, trelay, status);

	/* We have the format string */
	} else {
		/* Break down the time struct: */
		tmptime = localtime(&sma_time);

		for (frm = cfchar; *frm != '\0'; frm++)
		switch (*frm) {
			/*
			 * If the char after '%'-char is something we know
			 * print it, otherwise print literally
			 */
			case '%':
				if (*(frm+1))
					switch (*(frm+1)) {
						case 'd':
							fprintf(ofp,"%.2d",
							  tmptime->tm_mday);
							frm++;
							break;
						case 'D':
							fprintf(ofp,"%s",
							  stripn(asctime(tmptime)));
							frm++;
							break;
						case 'f':
							fprintf(ofp,"%s", from);
							frm++;
							break;
						case 'F':
							fprintf(ofp, "%s",
								frelay);
							frm++;
							break;
						case 'h':
							fprintf(ofp,"%.2d",
							  tmptime->tm_hour);
							frm++;
							break;
						case 'H':
							fprintf(ofp,"%s", host);
							frm++;
							break;
						case 'i':
							fprintf(ofp,"%s", id);
							frm++;
							break;
						case 'm':
							fprintf(ofp,"%.2d",
							  tmptime->tm_mon+1);
							frm++;
							break;
						case 'n':
							fprintf(ofp,"%.2d",
							  tmptime->tm_min);
							frm++;
							break;
						case 'M':
							fprintf(ofp,"%s",
							   montab[tmptime->tm_mon]);
							frm++;
							break;
						case 's':
							fprintf(ofp,"%.2d",
							  tmptime->tm_sec);
							frm++;
							break;
						case 'S':
							fprintf(ofp,"%d",
								status);
							frm++;
							break;
						case 't':
							fprintf(ofp,"%s", to);
							frm++;
							break;
						case 'T':
							fprintf(ofp,"%s",
								trelay);
							frm++;
							break;
						case 'U':
							fprintf(ofp,"%d",
								(int)sma_time);
							frm++;
							break;
						case 'z':
							fprintf(ofp,"%d", size);
							frm++;
							break;
						case 'y':
							fprintf(ofp,"%d",
							  tmptime->tm_year + 1900);
							frm++;
							break;
						case '%':
							fputc('%', ofp);	
							frm++;
							break;
						default:
							fputc('%', ofp);
							break;
					}
				break;

			/* backslashed special characters: */
			case '\\':
				if (*(frm+1))
					switch (*(frm+1)) {
						case 'n':
							fputc('\n', ofp);
							frm++;
							break;
						case 't':
							fputc('\t', ofp);
							frm++;
							break;
						case '\\':
							fputc('\\', ofp);	
							frm++;
							break;
						default:
							fputc(*(frm+1), ofp);
							frm++;
							break;
					}
				break;
			/*
			 * If not flag or any other special char, copy the
			 * format string character to stream:
			 */
			default:
				fputc(*frm, ofp);
				break;
		}
	/* End line always with newline: */
	fputc('\n', ofp);
	/* Flush output stream */
	fflush(ofp);
	}
}