File: spamass-milter.cpp

package info (click to toggle)
spamass-milter 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 1,040 kB
  • sloc: sh: 3,797; cpp: 1,731; awk: 312; ansic: 134; makefile: 61
file content (2310 lines) | stat: -rw-r--r-- 59,699 bytes parent folder | download
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
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
// 
//
//  $Id: spamass-milter.cpp,v 1.100 2014/08/15 02:46:50 kovert Exp $
//
//  SpamAss-Milter 
//    - a rather trivial SpamAssassin Sendmail Milter plugin
//
//  for information about SpamAssassin please see
//                        http://www.spamassassin.org
//
//  for information about Sendmail please see
//                        http://www.sendmail.org
//
//  Copyright (c) 2002 Georg C. F. Greve <greve@gnu.org>,
//   all rights maintained by FSF Europe e.V., 
//   Villa Vogelsang, Antonienallee 1, 45279 Essen, Germany
//

// {{{ License, Contact, Notes & Includes 

//   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
//
//   Contact:
//            Michael Brown <michaelb@opentext.com>
//

// Notes:
//
//  The libmilter for sendmail works callback-oriented, so if you have no
//  experience with event-driven programming, the following may be hard for
//  you to understand.
//
//  The code should be reasonably thread-safe. No guarantees, though.
//
//  This program roughly does the following steps:
//
//   1. register filter with libmilter & set up socket
//   2. register the callback functions defined in this file
//    -- wait for mail to show up --
//   3. start spamc client
//   4. assemble mail since libmilter passes it in pieces and put
//      these parts in the output pipe to spamc.
//   5. when the mail is complete, close the pipe.
//   6. read output from spamc, close input pipe and clean up PID
//   7. check for the flags affected by SpamAssassin and set/change
//      them accordingly
//   8. replace the body with the one provided by SpamAssassin if the
//      mail was rated spam, unless -m is specified
//   9. free all temporary data
//   10. tell sendmail to let the mail to go on (default) or be discarded
//    -- wait for mail to show up -- (restart at 3)
//

// Includes  
#include "config.h"

#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <sysexits.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
#include <signal.h>
#include <pthread.h>
#ifdef HAVE_POLL_H
#include <poll.h>
#else
#include "subst_poll.h"
#endif
#include <errno.h>
#include <netdb.h>

// C++ includes
#include <cstdio>
#include <cstddef>
#include <csignal>
#include <string>
#include <iostream>

#ifdef  __cplusplus
extern "C" {
#endif

#include "libmilter/mfapi.h"
//#include "libmilter/mfdef.h"

#if !HAVE_DECL_STRSEP
char *strsep(char **stringp, const char *delim);
#endif 

#if !HAVE_DECL_DAEMON
int daemon(int nochdir, int noclose);
#endif 

#ifdef  __cplusplus
}
#endif

#include "spamass-milter.h"

#ifdef WITH_DMALLOC
#include "dmalloc.h"
#endif

#ifndef INADDR_LOOPBACK
#define INADDR_LOOPBACK 0x7F000001
#endif

// }}} 

static const char Id[] = "$Id: spamass-milter.cpp,v 1.100 2014/08/15 02:46:50 kovert Exp $";

static char FilterName[] = "SpamAssassin";

struct smfiDesc smfilter =
  {
    FilterName, // filter name
    SMFI_VERSION,   // version code -- leave untouched
    SMFIF_ADDHDRS|SMFIF_CHGHDRS|SMFIF_CHGBODY,  // flags
    mlfi_connect, // info filter callback
    mlfi_helo, // HELO filter callback
    mlfi_envfrom, // envelope sender filter callback
    mlfi_envrcpt, // envelope recipient filter callback
    mlfi_header, // header filter callback
    mlfi_eoh, // end of header callback
    mlfi_body, // body filter callback
    mlfi_eom, // end of message callback
    mlfi_abort, // message aborted callback
    mlfi_close, // connection cleanup callback
  };

const char *const debugstrings[] = {
	"ALL", "FUNC", "POLL", "UORI", "STR", "MISC", "NET", "SPAMC", "RCPT",
	"COPY",
	NULL
};

int flag_debug = (1<<D_ALWAYS);
bool flag_reject = false;
int reject_score = -1;
bool dontmodifyspam = false;    // Don't modify/add body or spam results headers
bool dontmodify = false;        // Don't add SA headers, ever.
bool flag_sniffuser = false;
char *defaultuser;				/* Username to send to spamc if there are multiple recipients */
char *defaultdomain;			/* Domain to append if incoming address has none */
char *path_to_sendmail = (char *) SENDMAIL;
char *spamdhost;
char *rejecttext = NULL;				/* If we reject a mail, then use this text */
char *rejectcode = NULL;				/* If we reject a mail, then use code */
struct networklist ignorenets;
int spamc_argc;
char **spamc_argv;
bool flag_bucket = false;
bool flag_bucket_only = false;
char *spambucket;
bool flag_full_email = false;		/* pass full email address to spamc */
bool flag_expand = false;	/* alias/virtusertable expansion */
bool ignore_authenticated_senders = false;
bool warnedmacro = false;	/* have we logged that we couldn't fetch a macro? */
bool auth = false;		/* don't scan authenticated users */

// {{{ main()

int
main(int argc, char* argv[])
{
   int c, err = 0;
   const char *args = "afd:mMp:P:r:u:D:i:Ib:B:e:xS:R:C:";
   char *sock = NULL;
   bool dofork = false;
   char *pidfilename = NULL;
   FILE *pidfile = NULL;

#ifdef HAVE_VERBOSE_TERMINATE_HANDLER
	std::set_terminate (__gnu_cxx::__verbose_terminate_handler);
#endif

    openlog("spamass-milter", LOG_PID, LOG_MAIL);


    /* Process command line options */
    while ((c = getopt(argc, argv, args)) != -1) {
        switch (c) {
            case 'a':
                auth = true;
                break;
            case 'f':
                dofork = true;
                break;
            case 'd':
                parse_debuglevel(optarg);
                break;
            case 'D':
                spamdhost = strdup(optarg);
                break;
            case 'e':
                flag_full_email = true;
                defaultdomain = strdup(optarg);
                break;
            case 'i':
                debug(D_MISC, "Parsing ignore list");
                parse_networklist(optarg, &ignorenets);
                break;
			case 'I':
				debug(D_MISC, "Ignore authenticated senders");
				ignore_authenticated_senders = true;
				break;
            case 'm':
                dontmodifyspam = true;
                smfilter.xxfi_flags &= ~SMFIF_CHGBODY;
                break;
            case 'M':
                dontmodify = true;
                dontmodifyspam = true;
                smfilter.xxfi_flags &= ~(SMFIF_CHGBODY|SMFIF_CHGHDRS);
                break;
            case 'p':
                sock = strdup(optarg);
                break;
            case 'P':
                pidfilename = strdup(optarg);
                break;
            case 'r':
                flag_reject = true;
                reject_score = atoi(optarg);
                break;
            case 'S':
                path_to_sendmail = strdup(optarg);
                break;
            case 'C':
                rejectcode = strdup (optarg);
                break;
            case 'R':
                rejecttext = strdup (optarg);
                break;
            case 'u':
                flag_sniffuser = true;
                defaultuser = strdup(optarg);
                break;
            case 'b':
            case 'B':
                if (flag_bucket)
                {
                    fprintf(stderr, "Can only have one -b or -B flag\n");
                    err = 1;
                    break;
                }
                flag_bucket = true;
                if (c == 'b')
                {
                    flag_bucket_only = true;
                    smfilter.xxfi_flags |= SMFIF_DELRCPT; // May delete recipients
                }
                // we will modify the recipient list; if spamc returns
                // indicating that this mail is spam, the message will be
                // sent to <optarg>@localhost
                smfilter.xxfi_flags |= SMFIF_ADDRCPT; // May add recipients
                // XXX we should probably verify that optarg is vaguely sane
                spambucket = strdup( optarg );
                break;
            case 'x':
                flag_expand = true;
                break;
            case '?':
                err = 1;
                break;
        }
    }

   if (flag_full_email && !flag_sniffuser)
   {
   	  fprintf(stderr, "-e flag requires -u\n");
      err=1;
   }

   /* remember the remainer of the arguments so we can pass them to spamc */
   spamc_argc = argc - optind;
   spamc_argv = argv + optind;

   if (!sock || err) {
      cout << PACKAGE_NAME << " - Version " << PACKAGE_VERSION << endl;
      cout << "SpamAssassin Sendmail Milter Plugin" << endl;
      cout << "Usage: spamass-milter -p socket [-b|-B bucket] [-d xx[,yy...]] [-D host]" << endl;
      cout << "                      [-e defaultdomain] [-f] [-i networks] [-I] [-m] [-M]" << endl;
      cout << "                      [-P pidfile] [-r nn] [-u defaultuser] [-x] [-a]" << endl;
      cout << "                      [-C rejectcode] [ -R rejectmsg ]" << endl;
      cout << "                      [-- spamc args ]" << endl;
      cout << "   -p socket: path to create socket" << endl;
      cout << "   -b bucket: redirect spam to this mail address.  The orignal" << endl;
      cout << "          recipient(s) will not receive anything." << endl;
      cout << "   -B bucket: add this mail address as a BCC recipient of spam." << endl;
      cout << "   -C RejectCode: using this Reject Code." << endl;
      cout << "   -d xx[,yy ...]: set debug flags.  Logs to syslog" << endl;
      cout << "   -D host: connect to spamd at remote host (deprecated)" << endl;
      cout << "   -e defaultdomain: pass full email address to spamc instead of just\n"
              "          username.  Uses 'defaultdomain' if there was none" << endl;
      cout << "   -f: fork into background" << endl;
      cout << "   -i: skip (ignore) checks from these IPs or netblocks" << endl;
      cout << "          example: -i 192.168.12.5,10.0.0.0/8,172.16.0.0/255.255.0.0" << endl;
      cout << "   -I: skip (ignore) checks if sender is authenticated" << endl;
      cout << "   -m: don't modify body, Content-type: or Subject:" << endl;
      cout << "   -M: don't modify the message at all" << endl;
      cout << "   -P pidfile: Put processid in pidfile" << endl;
      cout << "   -r nn: reject messages with a score >= nn with an SMTP error.\n"
              "          use -1 to reject any messages tagged by SA." << endl;
      cout << "   -R RejectText: using this Reject Text." << endl;
      cout << "   -u defaultuser: pass the recipient's username to spamc.\n"
              "          Uses 'defaultuser' if there are multiple recipients." << endl;
      cout << "   -x: pass email address through alias and virtusertable expansion." << endl;
      cout << "   -a: don't scan messages over an authenticated connection." << endl;
      cout << "   -- spamc args: pass the remaining flags to spamc." << endl;
              
      exit(EX_USAGE);
   }

    /* Set standard reject text */
    if (rejecttext == NULL) {
        rejecttext = strdup ("Blocked by SpamAssassin");
    }
    if (rejectcode == NULL) {
        rejectcode = strdup ("5.7.1");
    }

    if (pidfilename)
    {
        unlink(pidfilename);
        pidfile = fopen(pidfilename,"w");
        if (!pidfile)
        {
            fprintf(stderr, "Could not open pidfile: %s\n", strerror(errno));
            exit(1);
        }
        /* leave the file open through the fork, since we don't know our pid
           yet
        */
    }


    if (dofork == true) 
    {
        if (daemon(0, 0) == -1)
        {
            fprintf(stderr, "daemon() failed: %s\n", strerror(errno));
            exit(1);
        }
    }
    
    if (pidfile)
    {
        fprintf(pidfile, "%ld\n", (long)getpid());
        fclose(pidfile);
        pidfile = NULL;
    }    
    
   {
      struct stat junk;
      if (stat(sock,&junk) == 0) unlink(sock);
   }

   (void) smfi_setconn(sock);
	if (smfi_register(smfilter) == MI_FAILURE) {
		fprintf(stderr, "smfi_register failed\n");
		exit(EX_UNAVAILABLE);
	} else {
      debug(D_MISC, "smfi_register succeeded");
   }
	debug(D_ALWAYS, "spamass-milter %s starting", PACKAGE_VERSION);
	err = smfi_main();
	debug(D_ALWAYS, "spamass-milter %s exiting", PACKAGE_VERSION);
	if (pidfilename)
		unlink(pidfilename);
	return err;
}

// }}}

/* Update a header if SA changes it, or add it if it is new. */
void update_or_insert(SpamAssassin* assassin, SMFICTX* ctx, string oldstring, t_setter setter, const char *header )
{
	string::size_type eoh1 = assassin->d().find("\n\n");
	string::size_type eoh2 = assassin->d().find("\n\r\n");
	string::size_type eoh = ( eoh1 < eoh2 ? eoh1 : eoh2 );

	string newstring;
	string::size_type oldsize;

	debug(D_UORI, "u_or_i: looking at <%s>", header);
	debug(D_UORI, "u_or_i: oldstring: <%s>", oldstring.c_str());

	newstring = retrieve_field(assassin->d().substr(0, eoh), header);
	debug(D_UORI, "u_or_i: newstring: <%s>", newstring.c_str());

	oldsize = callsetter(*assassin,setter)(newstring);
      
	if (!dontmodify)
	{
		if (newstring != oldstring)
		{
			/* change if old one was present, append if non-null */
			char* cstr = const_cast<char*>(newstring.c_str());
			if (oldsize > 0)
			{
				debug(D_UORI, "u_or_i: changing");
				smfi_chgheader(ctx, const_cast<char*>(header), 1, newstring.size() > 0 ? 
					cstr : NULL );
			} else if (newstring.size() > 0)
			{
				debug(D_UORI, "u_or_i: inserting");
				smfi_addheader(ctx, const_cast<char*>(header), cstr);
			}
		} else
		{
			debug(D_UORI, "u_or_i: no change");
		}
	}
}

// {{{ Assassinate

//
// implement the changes suggested by SpamAssassin for the mail.  Returns
// the milter error code.
int 
assassinate(SMFICTX* ctx, SpamAssassin* assassin)
{
  // find end of header (eol in last line of header)
  // and beginning of body
  string::size_type eoh1 = assassin->d().find("\n\n");
  string::size_type eoh2 = assassin->d().find("\n\r\n");
  string::size_type eoh = (eoh1 < eoh2) ? eoh1 : eoh2;
  string::size_type bob = assassin->d().find_first_not_of("\r\n", eoh);

  if (bob == string::npos)
  	bob = assassin->d().size();

  update_or_insert(assassin, ctx, assassin->spam_flag(), &SpamAssassin::set_spam_flag, "X-Spam-Flag");
  update_or_insert(assassin, ctx, assassin->spam_status(), &SpamAssassin::set_spam_status, "X-Spam-Status");

  /* Summarily reject the message if SA tagged it, or if we have a minimum
     score, reject if it exceeds that score. */
  if (flag_reject)
  {
	bool do_reject = false;
	if (reject_score == -1 && !assassin->spam_flag().empty())
		do_reject = true;
	if (reject_score != -1)
	{
		int score, rv;
		const char *spam_status = assassin->spam_status().c_str();
		/* SA 3.0 uses the keyword "score" */
		rv = sscanf(spam_status,"%*s score=%d", &score);
		if (rv != 1)
		{
			/* SA 2.x uses the keyword "hits" */
			rv = sscanf(spam_status,"%*s hits=%d", &score);
		}
		if (rv != 1)
			debug(D_ALWAYS, "Could not extract score from <%s>", spam_status);
		else 
		{
			debug(D_MISC, "SA score: %d", score);
			if (score >= reject_score)
				do_reject = true;
		}
	}
	if (do_reject)
	{
		debug(D_MISC, "Rejecting");
		smfi_setreply(ctx, const_cast<char*>("550"), rejectcode, rejecttext);


		if (flag_bucket)
		{
			/* If we also want a copy of the spam, shell out to sendmail and
			   send another copy.  The milter API will not let you send the
			   message AND return a failure code to the sender, so this is
			   the only way to do it. */
			char *popen_argv[3];
			FILE *p;
			pid_t pid;

			popen_argv[0] = path_to_sendmail;
			popen_argv[1] = spambucket;
			popen_argv[2] = NULL;
			
			debug(D_COPY, "calling %s %s", path_to_sendmail, spambucket);
			p = popenv(popen_argv, "w", &pid);
			if (!p)
			{
				debug(D_COPY, "popenv failed(%s).  Will not send a copy to spambucket", strerror(errno));
			} else
			{
				// Send message provided by SpamAssassin
				fwrite(assassin->d().c_str(), assassin->d().size(), 1, p);
				fclose(p); p = NULL;
				waitpid(pid, NULL, 0);
			}
		}
		return SMFIS_REJECT;
	}
  }

  /* Drop the message into the spam bucket if it's spam */
  if ( flag_bucket ) {
        if (!assassin->spam_flag().empty()) {
          // first, add the spambucket address
          if ( smfi_addrcpt( ctx, spambucket ) != MI_SUCCESS ) {
                throw string( "Failed to add spambucket to recipients" );
          }
          if (flag_bucket_only) {
                // Move recipients to a non-active header, one at a
                // time. Note, this may generate multiple X-Spam-Orig-To
                // headers, but that's okay.
                while( !assassin->recipients.empty()) {
                  if ( smfi_addheader( ctx, const_cast<char *>("X-Spam-Orig-To"), (char *)assassin->recipients.front().c_str()) != MI_SUCCESS ) {
                        throw string( "Failed to save recipient" );
                  }

                  // It's not 100% important that this succeeds, so we'll just warn on failure rather than bailing out.
                  if ( smfi_delrcpt( ctx, (char *)assassin->recipients.front().c_str()) != MI_SUCCESS ) {
                        // throw_error really just logs a warning as opposed to actually throw()ing
                        debug(D_ALWAYS, "Failed to remove recipient %s from the envelope", assassin->recipients.front().c_str() );
                  }
                  assassin->recipients.pop_front();
                }
          }
        }
  }

  update_or_insert(assassin, ctx, assassin->spam_report(), &SpamAssassin::set_spam_report, "X-Spam-Report");
  update_or_insert(assassin, ctx, assassin->spam_prev_content_type(), &SpamAssassin::set_spam_prev_content_type, "X-Spam-Prev-Content-Type");
  update_or_insert(assassin, ctx, assassin->spam_level(), &SpamAssassin::set_spam_level, "X-Spam-Level");
  update_or_insert(assassin, ctx, assassin->spam_checker_version(), &SpamAssassin::set_spam_checker_version, "X-Spam-Checker-Version");

  // 
  // If SpamAssassin thinks it is spam, replace
  //  Subject:
  //  Content-Type:
  //  <Body>
  // 
  //  However, only issue the header replacement calls if the content has
  //  actually changed. If SA didn't change subject or content-type, don't
  //  replace here unnecessarily.
  if (!dontmodifyspam && assassin->spam_flag().size()>0)
    {
	  update_or_insert(assassin, ctx, assassin->subject(), &SpamAssassin::set_subject, "Subject");
	  update_or_insert(assassin, ctx, assassin->content_type(), &SpamAssassin::set_content_type, "Content-Type");

      // Replace body with the one SpamAssassin provided
      string::size_type body_size = assassin->d().size() - bob;
      string body=assassin->d().substr(bob, string::npos);
      if ( smfi_replacebody(ctx, (unsigned char *)body.c_str(), body_size) == MI_FAILURE )
	throw string("error. could not replace body.");
      
    }

  return SMFIS_CONTINUE;
}

// retrieve the content of a specific field in the header
// and return it.
string
old_retrieve_field(const string& header, const string& field)
{
  // look for beginning of content
  string::size_type pos = find_nocase(header, "\n" + field + ": ");

  // return empty string if not found
  if (pos == string::npos)
  {
    debug(D_STR, "r_f: failed");
    return string("");
  }

  // look for end of field name
  pos = find_nocase(header, " ", pos) + 1;
  
  string::size_type pos2(pos);

  // is field empty? 
  if (pos2 == find_nocase(header, "\n", pos2))
    return string("");

  // look for end of content
  do {

    pos2 = find_nocase(header, "\n", pos2+1);

  }
  while ( pos2 < string::npos &&
	  isspace(header[pos2+1]) );

  return header.substr(pos, pos2-pos);

}

// retrieve the content of a specific field in the header
// and return it.
string
retrieve_field(const string& header, const string& field)
{
  // Find the field
  string::size_type field_start = string::npos;
  string::size_type field_end = string::npos;
  string::size_type idx = 0;

  while( field_start == string::npos ) {
	idx = find_nocase( header, field + ":", idx );

	// no match
	if ( idx == string::npos ) {
	  return string( "" );
	}

	// The string we've found needs to be either at the start of the
	// headers string, or immediately following a "\n"
	if ( idx != 0 ) {
	  if ( header[ idx - 1 ] != '\n' ) {
		idx++; // so we don't get stuck in an infinite loop
		continue; // loop around again
	  }
	}

	field_start = idx;
  }

  // A mail field starts just after the header. Ideally, there's a
  // space, but it's possible that there isn't.
  field_start += field.length() + 1;
  if ( field_start < ( header.length() - 1 ) && header[ field_start ] == ' ' ) {
	field_start++;
  }

  // See if there's anything left, to shortcut the rest of the
  // function.
  if ( field_start == header.length() - 1 ) {
	return string( "" );
  }

  // The field continues to the end of line. If the start of the next
  // line is whitespace, then the field continues.
  idx = field_start;
  while( field_end == string::npos ) {
	idx = header.find( "\n", idx );

	// if we don't find a "\n", gobble everything to the end of the headers
	if ( idx == string::npos ) {
	  field_end = header.length();
	} else {
	  // check the next character
	  if (( idx + 1 ) < header.length() && ( isspace( header[ idx + 1 ] ))) {
		idx ++; // whitespace found, so wrap to the next line
	  } else {
		field_end = idx;
	  }
	}
  }

  /* if the header line ends in \r\n, don't return the \r */
  if (header[field_end-1] == '\r')
  	field_end--;

  string data = header.substr( field_start, field_end - field_start );
  
  /* Replace all CRLF pairs with LF */
  idx = 0;
  while ( (idx = data.find("\r\n", idx)) != string::npos )
  {
  	data.replace(idx,2,"\n");
  }

  return data;
}


// }}}

// {{{ MLFI callbacks

//
// Gets called once when a client connects to sendmail
//
// gets the originating IP address and checks it against the ignore list
// if it isn't in the list, store the IP in a structure and store a 
// pointer to it in the private data area.
//
sfsistat 
mlfi_connect(SMFICTX * ctx, char *hostname, _SOCK_ADDR * hostaddr)
{
	struct context *sctx;
	int rv;

	debug(D_FUNC, "mlfi_connect: enter");

	/* allocate a structure to store the IP address (and SA object) in */
	sctx = (struct context *)malloc(sizeof(*sctx));
	if (!hostaddr)
	{
		static struct sockaddr_in localhost;
		
		/* not a socket; probably a local user calling sendmail directly */
		/* set to 127.0.0.1 */
		strcpy(sctx->connect_ip, "127.0.0.1");
		localhost.sin_family = AF_INET;
		localhost.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
		hostaddr = (struct sockaddr*) &localhost;
	} else
	{
		getnameinfo(hostaddr, sizeof(struct sockaddr_in6),
		            sctx->connect_ip, 63, NULL, 0, NI_NUMERICHOST);
		debug(D_FUNC, "Remote address: %s", sctx->connect_ip);
	}
	sctx->assassin = NULL;
	sctx->helo = NULL;
	
	/* store a pointer to it with setpriv */
	rv = smfi_setpriv(ctx, sctx);
	if (rv != MI_SUCCESS)
	{
		debug(D_ALWAYS, "smfi_setpriv failed!");
		return SMFIS_TEMPFAIL;
	}
	/* debug(D_ALWAYS, "ZZZ set private context to %p", sctx); */

	//debug(D_FUNC, "sctx->connect_ip: `%d'", sctx->connect_ip.sin_family);

	if (ip_in_networklist(hostaddr, &ignorenets))
	{
		debug(D_NET, "%s is in our ignore list - accepting message",
		      sctx->connect_ip);
		debug(D_FUNC, "mlfi_connect: exit ignore");
		return SMFIS_ACCEPT;
	}
	
	// Tell Milter to continue
	debug(D_FUNC, "mlfi_connect: exit");

	return SMFIS_CONTINUE;
}

//
// Gets called on every "HELO"
//
// stores the result in the context structure
//
sfsistat mlfi_helo(SMFICTX * ctx, char * helohost)
{
	struct context *sctx = (struct context*)smfi_getpriv(ctx);
	if (sctx->helo)
		free(sctx->helo);
	sctx->helo = strdup(helohost);

	return SMFIS_CONTINUE;
}

//
// Gets called first for all messages
//
// creates SpamAssassin object and makes pointer to it
// private data of this filter process
//
sfsistat
mlfi_envfrom(SMFICTX* ctx, char** envfrom)
{
  SpamAssassin* assassin;
  struct context *sctx = (struct context *)smfi_getpriv(ctx);

  if (sctx == NULL)
  {
    debug(D_ALWAYS, "smfi_getpriv failed!");
    return SMFIS_TEMPFAIL;
  }
  /* debug(D_ALWAYS, "ZZZ got private context %p", sctx); */

  if (auth) {
    const char *auth_type = smfi_getsymval(ctx,  
        const_cast<char *>("{auth_type}"));

    if (auth_type) {
      debug(D_MISC, "auth_type=%s", auth_type);
      return SMFIS_ACCEPT;
    }
  }

  if (ignore_authenticated_senders)
  {
    char *auth_authen;

    auth_authen = smfi_getsymval(ctx, "{auth_authen}");
    debug(D_MISC, "auth_authen=%s", auth_authen ?: "<unauthenticated>");

    if (auth_authen)
    {
      debug(D_MISC, "sender authenticated (%s) - accepting message",
	    auth_authen);
      debug(D_FUNC, "mlfi_envfrom: exit ignore");
      return SMFIS_ACCEPT;
    }
  }

  debug(D_FUNC, "mlfi_envfrom: enter");
  try {
    // launch new SpamAssassin
    assassin=new SpamAssassin;
  } catch (string& problem)
    {
      throw_error(problem);
      return SMFIS_TEMPFAIL;
    };
  
  assassin->set_connectip(string(sctx->connect_ip));

  // Store a pointer to the assassin object in our context struct
  sctx->assassin = assassin;

  // remember the MAIL FROM address
  assassin->set_from(string(envfrom[0]));
  
  // tell Milter to continue
  debug(D_FUNC, "mlfi_envfrom: exit");

  return SMFIS_CONTINUE;
}

//
// Gets called once for each recipient
//
// stores the first recipient in the spamassassin object and
// stores all addresses and the number thereof (some redundancy)
//


sfsistat
mlfi_envrcpt(SMFICTX* ctx, char** envrcpt)
{
	struct context *sctx = (struct context*)smfi_getpriv(ctx);
	SpamAssassin* assassin = sctx->assassin;
	FILE *p;

	debug(D_FUNC, "mlfi_envrcpt: enter");

	if (flag_expand)
	{
		/* open a pipe to sendmail so we can do address expansion */

		char buf[1024];
		char *popen_argv[4];
		pid_t pid;
		
		popen_argv[0] = path_to_sendmail;
		popen_argv[1] = (char *)"-bv";
		popen_argv[2] = envrcpt[0];
		popen_argv[3] = NULL;

		debug(D_RCPT, "calling %s -bv %s", path_to_sendmail, envrcpt[0]);

		p = popenv(popen_argv, "r", &pid);
		if (!p)
		{
			debug(D_RCPT, "popenv failed(%s).  Will not expand aliases", strerror(errno));
			assassin->expandedrcpt.push_back(envrcpt[0]);
		} else
		{
			while (fgets(buf, sizeof(buf), p) != NULL)
			{
				int i = strlen(buf);
				/* strip trailing EOLs */
				while (i > 0 && buf[i - 1] <= ' ')
					i--;
				buf[i] = '\0';
				debug(D_RCPT, "sendmail output: %s", buf);
				/*	From a quick scan of the sendmail source, a valid email
					address gets printed via either
					    "deliverable: mailer %s, host %s, user %s"
					or  "deliverable: mailer %s, user %s"
				*/
				if (strstr(buf, "... deliverable: mailer "))
				{
					char *p=strstr(buf,", user ");
					/* anything after ", user " is the email address */
					debug(D_RCPT, "user: %s", p+7);
					assassin->expandedrcpt.push_back(p+7);
				}
			}
			fclose(p); p = NULL;
			waitpid(pid, NULL, 0);
		}
	} else
	{
		assassin->expandedrcpt.push_back(envrcpt[0]);
	}	
	debug(D_RCPT, "Total of %d actual recipients", (int)assassin->expandedrcpt.size());

	if (assassin->numrcpt() == 0)
	{
		/* Send the envelope headers as X-Envelope-From: and
		   X-Envelope-To: so that SpamAssassin can use them in its
		   whitelist checks.  Also forge as complete a dummy
		   Received: header as possible because SA gets a lot of
		   info from it.
		   
			HReceived: $?sfrom $s $.$?_($?s$|from $.$_)
				$.$?{auth_type}(authenticated$?{auth_ssf} bits=${auth_ssf}$.)
				$.by $j ($v/$Z)$?r with $r$. id $i$?{tls_version}
				(version=${tls_version} cipher=${cipher} bits=${cipher_bits} verify=${verify})$.$?u
				for $u; $|;
				$.$b$?g
				(envelope-from $g)$.
		   
		*/
		const char *macro_b, *macro_i, *macro_j, *macro_r,
		           *macro_s, *macro_v, *macro_Z, *macro__,
		           *macro_auth_type, *macro_auth_ssf;
		char date[32];

		/* RFC 822 date. */
		macro_b = smfi_getsymval(ctx, const_cast<char *>("b"));
		if (!macro_b)                                  
		{
			time_t tval;
			time(&tval);
			strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", localtime(&tval));
			macro_b = date;
			warnmacro("b", "ENVRCPT");
		}

		/* queue ID */
		macro_i = smfi_getsymval(ctx, const_cast<char *>("i"));
		if (!macro_i)
		{
			macro_i = "unknown";
			warnmacro("i", "ENVRCPT");
		}

		/* FQDN of this site */
		macro_j = smfi_getsymval(ctx, const_cast<char *>("j"));
		if (!macro_j)
		{
			macro_j = "localhost";
			warnmacro("j", "ENVRCPT");
		}

		/* Protocol used to receive the message */
		macro_r = smfi_getsymval(ctx, const_cast<char *>("r"));
		if (!macro_r)
		{
			macro_r = "SMTP";
			warnmacro("r", "ENVRCPT");
		}

		/* Authenticated bits Information */
		macro_auth_type = smfi_getsymval(ctx, "{auth_type}");
		if (!macro_auth_type)
		{
			macro_auth_type = "";
			warnmacro("auth_type", "ENVRCPT");
		}

		macro_auth_ssf = smfi_getsymval(ctx, "{auth_ssf}");
		if (!macro_auth_ssf)
		{
			macro_auth_ssf = "";
			warnmacro("auth_ssf", "ENVRCPT");
		}

		/* Sendmail currently cannot pass us the {s} macro, but
		   I do not know why.  Leave this in for the day sendmail is
		   fixed.  Until that day, use the value remembered by
		   mlfi_helo()
		*/
		macro_s = smfi_getsymval(ctx, const_cast<char *>("s"));
		if (!macro_s)
			macro_s = sctx->helo;
		if (!macro_s)
			macro_s = "nohelo";

		/* Sendmail binary version */
		macro_v = smfi_getsymval(ctx, const_cast<char *>("v"));
		if (!macro_v)
		{
			macro_v = "8.13.0";
			warnmacro("v", "ENVRCPT");
		}

		/* Sendmail .cf version */
		macro_Z = smfi_getsymval(ctx, const_cast<char *>("Z"));
		if (!macro_Z)
		{
			macro_Z = "8.13.0";
			warnmacro("Z", "ENVRCPT");
		}

		/* Validated sending site's address */
		macro__ = smfi_getsymval(ctx, const_cast<char *>("_"));
		if (!macro__)
		{
			macro__ = "unknown";
			warnmacro("_", "ENVRCPT");
		}

		assassin->output((string)"X-Envelope-From: "+assassin->from()+"\r\n");
		assassin->output((string)"X-Envelope-To: "+envrcpt[0]+"\r\n");

		string rec_header;
		rec_header=(string)"Received: from "+macro_s+" ("+macro__+")\r\n\t";
		if (strlen(macro_auth_type)!=0) {
			rec_header+=(string)"(authenticated";
			if (strlen(macro_auth_ssf)!=0) {
				rec_header+=(string)" bits="+macro_auth_ssf;
			}
			rec_header+=(string)")\r\n\t";
		}
		rec_header+=(string)"by "+macro_j+" ("+macro_v+"/"+macro_Z+") with "+
			macro_r+" id "+macro_i+"\r\n\t"+
			macro_b+"\r\n\t"+
			"(envelope-from "+assassin->from()+");\r\n";

		debug(D_SPAMC, "Received header for spamc: %s", rec_header.c_str());
		assassin->output(rec_header);

	} else
		assassin->output((string)"X-Envelope-To: "+envrcpt[0]+"\r\n");

	/* increment RCPT TO: count */
	assassin->set_numrcpt();

	/* If we expanded to at least one user and we haven't recorded one yet,
	   record the first one */
	if (!assassin->expandedrcpt.empty() && (assassin->rcpt().size() == 0))
	{
		debug(D_RCPT, "remembering %s for spamc", assassin->expandedrcpt.front().c_str());
		assassin->set_rcpt(assassin->expandedrcpt.front());
	}

	debug(D_RCPT, "remembering recipient %s", envrcpt[0]);
	assassin->recipients.push_back( envrcpt[0] ); // XXX verify that this worked

	debug(D_FUNC, "mlfi_envrcpt: exit");

	return SMFIS_CONTINUE;
}

//
// Gets called repeatedly for all header fields
//
// assembles the headers and passes them on to the SpamAssassin client
// through the pipe.
//
// only exception: SpamAssassin header fields (X-Spam-*) get suppressed
// but are being stored in the SpamAssassin element.
//
// this function also starts the connection with the SPAMC program the
// first time it is called.
//

sfsistat
mlfi_header(SMFICTX* ctx, char* headerf, char* headerv)
{
  SpamAssassin* assassin = ((struct context *)smfi_getpriv(ctx))->assassin;
  debug(D_FUNC, "mlfi_header: enter");

  // Check if the SPAMC program has already been run, if not we run it.
  if ( !(assassin->connected) )
     {
       try {
         assassin->connected = 1; // SPAMC is getting ready to run
         assassin->Connect();
       } 
       catch (string& problem) {
         throw_error(problem);
         ((struct context *)smfi_getpriv(ctx))->assassin=NULL;
         delete assassin;
         debug(D_FUNC, "mlfi_header: exit error connect");
         return SMFIS_TEMPFAIL;
       };
     }

  // Is it a "X-Spam-" header field?
  if ( cmp_nocase_partial("X-Spam-", headerf) == 0 )
    {
      int suppress = 1;
      // memorize content of old fields

      if ( cmp_nocase_partial("X-Spam-Status", headerf) == 0 )
	assassin->set_spam_status(headerv);
      else if ( cmp_nocase_partial("X-Spam-Flag", headerf) == 0 )
	assassin->set_spam_flag(headerv);
      else if ( cmp_nocase_partial("X-Spam-Report", headerf) == 0 )
	assassin->set_spam_report(headerv);
      else if ( cmp_nocase_partial("X-Spam-Prev-Content-Type", headerf) == 0 )
	assassin->set_spam_prev_content_type(headerv);
      else if ( cmp_nocase_partial("X-Spam-Level", headerf) == 0 )
	assassin->set_spam_level(headerv);
      else if ( cmp_nocase_partial("X-Spam-Checker-Version", headerf) == 0 )
	assassin->set_spam_checker_version(headerv);
      else
      {
      	/* Hm. X-Spam header, but not one we recognize.  Pass it through. */
      	suppress = 0;
      }
      
      if (suppress)
      {
	debug(D_FUNC, "mlfi_header: suppress");
	return SMFIS_CONTINUE;
      }
    }

  // Content-Type: will be stored if present
  if ( cmp_nocase_partial("Content-Type", headerf) == 0 )
    assassin->set_content_type(headerv);

  // Subject: should be stored
  if ( cmp_nocase_partial("Subject", headerf) == 0 )
    assassin->set_subject(headerv);

  // assemble header to be written to SpamAssassin
  string header = string(headerf) + ": " + headerv + "\r\n";
 
  try {
    // write to SpamAssassin client
    assassin->output(header.c_str(),header.size());
  } catch (string& problem)
    {
      throw_error(problem);
      ((struct context *)smfi_getpriv(ctx))->assassin=NULL;
      delete assassin;
      debug(D_FUNC, "mlfi_header: exit error output");
      return SMFIS_TEMPFAIL;
    };
  
  // go on...
  debug(D_FUNC, "mlfi_header: exit");

  return SMFIS_CONTINUE;
}

// 
// Gets called once when the header is finished.
//
// writes empty line to SpamAssassin client to separate
// headers from body.
//
sfsistat
mlfi_eoh(SMFICTX* ctx)
{
  SpamAssassin* assassin = ((struct context *)smfi_getpriv(ctx))->assassin;

  debug(D_FUNC, "mlfi_eoh: enter");

  // Check if the SPAMC program has already been run, if not we run it.
  if ( !(assassin->connected) )
     {
       try {
         assassin->connected = 1; // SPAMC is getting ready to run
         assassin->Connect();
       } 
       catch (string& problem) {
         throw_error(problem);
         ((struct context *)smfi_getpriv(ctx))->assassin=NULL;
         delete assassin;

         debug(D_FUNC, "mlfi_eoh: exit error connect");
         return SMFIS_TEMPFAIL;
       };
     }

  try {
    // add blank line between header and body
    assassin->output("\r\n",2);
  } catch (string& problem)
    {
      throw_error(problem);
      ((struct context *)smfi_getpriv(ctx))->assassin=NULL;
      delete assassin;
  
      debug(D_FUNC, "mlfi_eoh: exit error output");
      return SMFIS_TEMPFAIL;
    };
  
  // go on...

  debug(D_FUNC, "mlfi_eoh: exit");
  return SMFIS_CONTINUE;
}

//
// Gets called repeatedly to transmit the body
//
// writes everything directly to SpamAssassin client
//
sfsistat
mlfi_body(SMFICTX* ctx, u_char *bodyp, size_t bodylen)
{
  debug(D_FUNC, "mlfi_body: enter");
  SpamAssassin* assassin = ((struct context *)smfi_getpriv(ctx))->assassin;

 
  try {
    assassin->output(bodyp, bodylen);
  } catch (string& problem)
    {
      throw_error(problem);
      ((struct context *)smfi_getpriv(ctx))->assassin=NULL;
      delete assassin;
      debug(D_FUNC, "mlfi_body: exit error");
      return SMFIS_TEMPFAIL;
    };

  // go on...
  debug(D_FUNC, "mlfi_body: exit");
  return SMFIS_CONTINUE;
}

//
// Gets called once at the end of mail processing
//
// tells SpamAssassin client that the mail is complete
// through EOF and then modifies the mail accordingly by
// calling the "assassinate" function
//
sfsistat
mlfi_eom(SMFICTX* ctx)
{
  SpamAssassin* assassin = ((struct context *)smfi_getpriv(ctx))->assassin;
  int milter_status;
 
  debug(D_FUNC, "mlfi_eom: enter");
  try {

    // close output pipe to signal EOF to SpamAssassin
    assassin->close_output();

    // read what the Assassin is telling us
    assassin->input();

    milter_status = assassinate(ctx, assassin);

    // now cleanup the element.
    ((struct context *)smfi_getpriv(ctx))->assassin=NULL;
    delete assassin;

  } catch (string& problem)
    {
      throw_error(problem);
      ((struct context *)smfi_getpriv(ctx))->assassin=NULL;
      delete assassin;
      debug(D_FUNC, "mlfi_eom: exit error");
      return SMFIS_TEMPFAIL;
    };
  
  // go on...
  debug(D_FUNC, "mlfi_eom: exit");
  return milter_status;
}

//
// Gets called on session-basis. This keeps things nice & quiet.
//
sfsistat
mlfi_close(SMFICTX* ctx)
{
  struct context *sctx;
  debug(D_FUNC, "mlfi_close");
  
  sctx = (struct context*)smfi_getpriv(ctx);
  if (sctx == NULL)
    return SMFIS_ACCEPT;

  if (sctx->helo)
  	free(sctx->helo);
  free(sctx);
  smfi_setpriv(ctx, NULL);
  
  return SMFIS_ACCEPT;
}

//
// Gets called when things are being aborted.
//
// kills the SpamAssassin object, its destructor should
// take care of everything.
//
sfsistat
mlfi_abort(SMFICTX* ctx)
{
  SpamAssassin* assassin = ((struct context *)smfi_getpriv(ctx))->assassin;

  debug(D_FUNC, "mlfi_abort");
  ((struct context *)smfi_getpriv(ctx))->assassin=NULL;
  delete assassin;

  return SMFIS_ACCEPT;
}

// }}}

// {{{ SpamAssassin Class

//
// This is a new constructor for the SpamAssassin object.  It simply 
// initializes two variables.  The original constructor has been
// renamed to Connect().
//
SpamAssassin::SpamAssassin():
  error(false),
  running(false),
  connected(false),
  _numrcpt(0)
{
}


SpamAssassin::~SpamAssassin()
{ 
	if (connected) 
	{
		// close all pipes that are still open
		if (pipe_io[0][0] > -1)	close(pipe_io[0][0]);
		if (pipe_io[0][1] > -1)	close(pipe_io[0][1]);
		if (pipe_io[1][0] > -1)	close(pipe_io[1][0]);
		if (pipe_io[1][1] > -1)	close(pipe_io[1][1]);

		// child still running?
		if (running)
		{
			// make sure the pid is valid
			if (pid > 0) {
				// slaughter child
				kill(pid, SIGKILL);

				// wait for child to terminate
				int status;
				waitpid(pid, &status, 0);
			}
		}
    }

	// Clean up the recip list. Might be overkill, but it's good housekeeping.
	while( !recipients.empty()) 
	{
		recipients.pop_front();
	}
	// Clean up the recip list. Might be overkill, but it's good housekeeping.
	while( !expandedrcpt.empty()) 
	{
		expandedrcpt.pop_front();
	}
}

//
// This is the old SpamAssassin constructor.  It has been renamed Connect(),
// and is now called at the beginning of the mlfi_header() function.
//

void SpamAssassin::Connect()
{
  // set up pipes for in- and output
  if (pipe(pipe_io[0]))
    throw string(string("pipe error: ")+string(strerror(errno)));
  if (pipe(pipe_io[1]))
    throw string(string("pipe error: ")+string(strerror(errno)));

  // now execute SpamAssassin client for contact with SpamAssassin spamd

  // start child process
  switch(pid = fork())
    {
    case -1:
      // forking trouble. throw error.
      throw string(string("fork error: ")+string(strerror(errno)));
      break;
    case 0:
      // +++ CHILD +++
      
      // close unused pipes
      close(pipe_io[1][0]);
      close(pipe_io[0][1]);

      // redirect stdin(0), stdout(1) and stderr(2)
      dup2(pipe_io[0][0],0);
      dup2(pipe_io[1][1],1);
      dup2(pipe_io[1][1],2);

      closeall(3);

      // execute spamc 
      // absolute path (determined in autoconf) 
      // should be a little more secure
      // XXX arbitrary 100-argument max
      int argc = 0;
      char** argv = (char**) malloc(100*sizeof(char*));
      argv[argc++] = strdup(SPAMC);
      if (flag_sniffuser) 
      {
        argv[argc++] = strdup("-u");
        if ( expandedrcpt.size() != 1 )
        {
          // More (or less?) than one recipient, so we pass the default
          // username to SPAMC.  This way special rules can be defined for
          // multi recipient messages.
          debug(D_RCPT, "%d recipients; spamc gets default username %s", (int)expandedrcpt.size(), defaultuser);
          argv[argc++] = defaultuser; 
        } else
        { 
          // There is only 1 recipient so we pass the username
          // (converted to lowercase) to SPAMC.  Don't worry about 
          // freeing this memory as we're exec()ing anyhow.
          if (flag_full_email)
            argv[argc] = strlwr(strdup(full_user().c_str())); 
          else
            argv[argc] = strlwr(strdup(local_user().c_str())); 

          debug(D_RCPT, "spamc gets %s", argv[argc]);
         
          argc++;
        }
      }
      if (spamdhost) 
      {
        argv[argc++] = strdup("-d");
        argv[argc++] = spamdhost;
      }
      if (spamc_argc)
      {
      	memcpy(argv+argc, spamc_argv, spamc_argc * sizeof(char *));
      	argc += spamc_argc;
      }
      argv[argc++] = 0;

      execvp(argv[0] , argv); // does not return!

      // execution failed
      throw_error(string("execution error: ")+string(strerror(errno)));
      _exit(1);
      break;
    }

  // +++ PARENT +++

  // close unused pipes
  close(pipe_io[0][0]);
  close(pipe_io[1][1]);
  pipe_io[0][0]=-1;
  pipe_io[1][1]=-1;

  // mark the pipes non-blocking
  if(fcntl(pipe_io[0][1], F_SETFL, O_NONBLOCK) == -1)
     throw string(string("Cannot set pipe01 nonblocking: ")+string(strerror(errno)));
#if 0  /* don't really need to make the sink pipe nonblocking */
  if(fcntl(pipe_io[1][0], F_SETFL, O_NONBLOCK) == -1)
     throw string(string("Cannot set pipe10 nonblocking: ")+string(strerror(errno)));
#endif

  // we have to assume the client is running now.
  running=true;

  /* If we have any buffered output, write it now. */
  if (outputbuffer.size())
  {
    output(outputbuffer);
    outputbuffer="";
  }
}

// write to SpamAssassin
void
SpamAssassin::output(const void* buffer, long size)
{
  debug(D_FUNC, "::output enter");

  debug(D_SPAMC, "output \"%*.*s\"", (int)size, (int)size, (char *)buffer);

  // if there are problems, fail.
  if (error)
    throw string("tried output despite problems. failed.");

  /* If we haven't launched spamc yet, just store the data */
  if (!connected)
  {
	/* Silly C++ can't tell the difference between 
		(const char*, string::size_type) and
		(string::size_type, char), so we have to cast the parameters.
	*/
  	outputbuffer.append((const char *)buffer,(string::size_type)size);
  	debug(D_FUNC, "::output exit1");
  	return;
  }

  // send to SpamAssassin
  long total = 0;
  long wsize = 0;
  string reason;
  int status;
  do {
	struct pollfd fds[2];
	int nfds = 2, nready;
	fds[0].fd = pipe_io[0][1];
	fds[0].events = POLLOUT;
	fds[1].fd = pipe_io[1][0];
	fds[1].events = POLLIN;

	debug(D_POLL, "polling fds %d and %d", pipe_io[0][1], pipe_io[1][0]);
	nready = poll(fds, nfds, 1000);
	if (nready == -1)
		throw("poll failed");

	debug(D_POLL, "poll returned %d, fd0=%d, fd1=%d", nready, fds[0].revents, fds[1].revents);

	if (fds[1].revents & (POLLERR|POLLNVAL|POLLHUP))
	{
		throw string("poll says my read pipe is busted");
	}

	if (fds[0].revents & (POLLERR|POLLNVAL|POLLHUP))
	{
		throw string("poll says my write pipe is busted");
	}

	if (fds[1].revents & POLLIN)
	{
		debug(D_POLL, "poll says I can read");
		read_pipe();
	}

	if (fds[0].revents & POLLOUT)
	{
		debug(D_POLL, "poll says I can write");
		switch(wsize = write(pipe_io[0][1], (char *)buffer + total, size - total))
		{
		  case -1:
			if (errno == EAGAIN)
				continue;
			reason = string(strerror(errno));

			// close the pipes
			close(pipe_io[0][1]);
			close(pipe_io[1][0]);
			pipe_io[0][1]=-1;	
			pipe_io[1][0]=-1;	

			// Slaughter child
			kill(pid, SIGKILL);

			// set flags
			error = true;
			running = false;
	
			// wait until child is dead
			waitpid(pid, &status, 0);

			throw string(string("write error: ")+reason);	
			break;
	      default:
			total += wsize;
			debug(D_POLL, "wrote %ld bytes", wsize);
			break;
		}
	}
  } while ( total < size );

  debug(D_FUNC, "::output exit2");
}

void SpamAssassin::output(const void* buffer)
{
	output(buffer, strlen((const char *)buffer));
}

void SpamAssassin::output(string buffer)
{
	output(buffer.c_str(), buffer.size());
}

// close output pipe
void
SpamAssassin::close_output()
{
  if(close(pipe_io[0][1]))
    throw string(string("close error: ")+string(strerror(errno)));
  pipe_io[0][1]=-1;
}

void
SpamAssassin::input()
{
  debug(D_FUNC, "::input enter");
  // if the child has exited or we experienced an error, return
  // immediately.
  if (!running || error)
  {
    debug(D_FUNC, "::input exit1");
    return;
  }

  // keep reading from input pipe until it is empty
  empty_and_close_pipe();
  
  // that's it, we're through
  running = false;

  // wait until child is dead
  int status;
  if(waitpid(pid, &status, 0)<0)
    {
      error = true;
      throw string(string("waitpid error: ")+string(strerror(errno)));
    }; 
	debug(D_FUNC, "::input exit2");
}

//
// return reference to mail
//
string& 
SpamAssassin::d()
{
  return mail;
}

//
// get values of the different SpamAssassin fields
//
string& 
SpamAssassin::spam_status()
{
  return x_spam_status;
}

string& 
SpamAssassin::spam_flag()
{
  return x_spam_flag;
}

string& 
SpamAssassin::spam_report()
{
  return x_spam_report;
}

string& 
SpamAssassin::spam_prev_content_type()
{
  return x_spam_prev_content_type;
}

string& 
SpamAssassin::spam_checker_version()
{
  return x_spam_checker_version;
}

string& 
SpamAssassin::spam_level()
{
  return x_spam_level;
}

string& 
SpamAssassin::content_type()
{
  return _content_type;
}

string& 
SpamAssassin::subject()
{
  return _subject;
}

string&
SpamAssassin::rcpt()
{
  return _rcpt;
}

string&
SpamAssassin::from()
{
  return _from;
}

string&
SpamAssassin::connectip()
{
  return _connectip;
}


string
SpamAssassin::local_user()
{
  // assuming we have a recipient in the form: <username@somehost.somedomain>
  // (angle brackets optional) we return 'username'
  if (_rcpt[0] == '<')
    return _rcpt.substr(1, _rcpt.find_first_of("@+")-1);
  else
  	return _rcpt.substr(0, _rcpt.find_first_of("@+"));
}

string
SpamAssassin::full_user()
{
  string name;
  // assuming we have a recipient in the form: <username@somehost.somedomain>
  // (angle brackets optional) we return 'username@somehost.somedomain'
  if (_rcpt[0] == '<')
    name = _rcpt.substr(1, _rcpt.find('>')-1);
  else
  	name = _rcpt;
  if(name.find('@') == string::npos)
  {
    /* if the name had no domain part (local delivery), append the default one */
    name = name + "@" + defaultdomain;
  }
  return name;
}

int
SpamAssassin::numrcpt()
{
  return _numrcpt;
}

int
SpamAssassin::set_numrcpt()
{
  _numrcpt++;
  return _numrcpt;
}

int
SpamAssassin::set_numrcpt(const int val)
{
  _numrcpt = val;
  return _numrcpt;
}

//
// set the values of the different SpamAssassin
// fields in our element. Returns former size of field
//
string::size_type
SpamAssassin::set_spam_status(const string& val)
{
  string::size_type old = x_spam_status.size();
  x_spam_status = val;
  return (old);
}

string::size_type
SpamAssassin::set_spam_flag(const string& val)
{
  string::size_type old = x_spam_flag.size();
  x_spam_flag = val;
  return (old);
}

string::size_type
SpamAssassin::set_spam_report(const string& val)
{
  string::size_type old = x_spam_report.size();
  x_spam_report = val;
  return (old);
}

string::size_type
SpamAssassin::set_spam_prev_content_type(const string& val)
{
  string::size_type old = x_spam_prev_content_type.size();
  x_spam_prev_content_type = val;
  return (old);
}

string::size_type
SpamAssassin::set_spam_checker_version(const string& val)
{
  string::size_type old = x_spam_checker_version.size();
  x_spam_checker_version = val;
  return (old);
}

string::size_type
SpamAssassin::set_spam_level(const string& val)
{
  string::size_type old = x_spam_level.size();
  x_spam_level = val;
  return (old);
}

string::size_type
SpamAssassin::set_content_type(const string& val)
{
  string::size_type old = _content_type.size();
  _content_type = val;
  return (old);
}

string::size_type
SpamAssassin::set_subject(const string& val)
{
  string::size_type old = _subject.size();
  _subject = val;
  return (old);
}

string::size_type
SpamAssassin::set_rcpt(const string& val)
{
  string::size_type old = _rcpt.size();
  _rcpt = val;
  return (old);  
}

string::size_type
SpamAssassin::set_from(const string& val)
{
  string::size_type old = _from.size();
  _from = val;
  return (old);  
}

string::size_type
SpamAssassin::set_connectip(const string& val)
{
  string::size_type old = _connectip.size();
  _connectip = val;
  return (old);  
}

//
// Read available output from SpamAssassin client
//
int
SpamAssassin::read_pipe()
{
	long size;
	int  status;
	char iobuff[1024];
	string reason;

	debug(D_FUNC, "::read_pipe enter");

	if (pipe_io[1][0] == -1)
	{
		debug(D_FUNC, "::read_pipe exit - shouldn't have been called?");
		return 0;
	}

	size = read(pipe_io[1][0], iobuff, 1024);

	if (size < 0)
    {
		// Error. 
		reason = string(strerror(errno));
		
		// Close remaining pipe.
		close(pipe_io[1][0]);
		pipe_io[1][0] = -1;
	
		// Slaughter child
		kill(pid, SIGKILL);
	
		// set flags
		error = true;
		running = false;
	
		// wait until child is dead
		waitpid(pid, &status, 0);
	
		// throw the error message that caused this trouble
		throw string(string("read error: ")+reason);
	} else if ( size == 0 )
	{

		// EOF. Close the pipe
		if(close(pipe_io[1][0]))
			throw string(string("close error: ")+string(strerror(errno)));
		pipe_io[1][0] = -1;
	
	} else
	{ 
		// append to mail buffer 
		mail.append(iobuff, size);
		debug(D_POLL, "read %ld bytes", size);
		debug(D_SPAMC, "input  \"%*.*s\"", (int)size, (int)size, iobuff);
	}
	debug(D_FUNC, "::read_pipe exit");
	return size;
}

//
// Read all output from SpamAssassin client
// and close the pipe
//
void
SpamAssassin::empty_and_close_pipe()
{
	debug(D_FUNC, "::empty_and_close_pipe enter");
	while (read_pipe())
		;
	debug(D_FUNC, "::empty_and_close_pipe exit");
}

// }}}

// {{{ Some small subroutines without much relation to functionality

// output error message to syslog facility
void
throw_error(const string& errmsg)
{
  if (errmsg.c_str())
    syslog(LOG_ERR, "Thrown error: %s", errmsg.c_str());
  else
    syslog(LOG_ERR, "Unknown error");
}

/* Takes a comma or space-delimited string of debug tokens and sets the
   appropriate bits in flag_debug.  "all" sets all the bits.
*/
void parse_debuglevel(char* string)
{
	char *token;

	/* make a copy so we don't overwrite argv[] */
	string = strdup(string);

	/* handle the old numeric values too */
	switch(atoi(string))
	{
		case 3:
			flag_debug |= (1<<D_UORI) | (1<<D_STR);
		case 2:
			flag_debug |= (1<<D_POLL);
		case 1:
			flag_debug |= (1<<D_MISC) | (1<<D_FUNC);
			debug(D_ALWAYS, "Setting debug level to 0x%0x", flag_debug);
			free(string);
			return;
		default:
			break;
	}

	while ((token = strsep(&string, ", ")))
	{
		int i;
		for (i=0; debugstrings[i]; i++)
		{
			if(strcasecmp(token, "ALL")==0)
			{
				flag_debug = (1<<D_MAX)-1;
				break;
			}
			if(strcasecmp(token, debugstrings[i])==0)
			{
				flag_debug |= (1<<i);
				break;
			}
		}

		if (!debugstrings[i])
		{
			fprintf(stderr, "Invalid debug token \"%s\"\n", token);
			exit(1);
		}
	}
	debug(D_ALWAYS, "Setting debug level to 0x%0x", flag_debug);
	free(string);
}

/*
   Output a line to syslog using print format, but only if the appropriate
   debug level is set.  The D_ALWAYS level is always enabled.
*/
void debug(enum debuglevel level, const char* fmt, ...)
{
	if ((1<<level) & flag_debug)
	{
#if defined(HAVE_VSYSLOG)
		va_list vl;
		va_start(vl, fmt);
		vsyslog(LOG_ERR, fmt, vl);
		va_end(vl);
#else
#if defined(HAVE_VASPRINTF)
		char *buf;
#else
		char buf[1024];
#endif
		va_list vl;
		va_start(vl, fmt);
#if defined(HAVE_VASPRINTF)
		vasprintf(&buf, fmt, vl);
#else
#if defined(HAVE_VSNPRINTF)
		vsnprintf(buf, sizeof(buf)-1, fmt, vl);
#else
		/* XXX possible buffer overflow here; be careful what you pass to debug() */
		vsprintf(buf, fmt, vl);
#endif
#endif
		va_end(vl);
		syslog(LOG_ERR, "%s", buf);
#if defined(HAVE_VASPRINTF)
		free(buf);
#endif 
#endif /* vsyslog */
	}
}

// case-insensitive search 
string::size_type 
find_nocase(const string& array, const string& pattern, string::size_type start)
{
  string::size_type pos(start);

  while (pos < array.size())
    {
      string::size_type ctr = 0;

      while( (pos+ctr) < array.size() &&
	     toupper(array[pos+ctr]) == toupper(pattern[ctr]) )
	{
	  ++ctr;
	  if (ctr == pattern.size())
	  {
	    debug(D_STR, "f_nc: <%s><%s>: hit", array.c_str(), pattern.c_str());
	    return pos;
	  }
	};
      
      ++pos;
    };

  debug(D_STR, "f_nc: <%s><%s>: nohit", array.c_str(), pattern.c_str());
  return string::npos;
}

// compare case-insensitive
int
cmp_nocase_partial(const string& s, const string& s2)
{
  string::const_iterator p=s.begin();
  string::const_iterator p2=s2.begin();

  while ( p != s.end() && p2 <= s2.end() ) {
    if (toupper(*p) != toupper(*p2))
    {
      debug(D_STR, "c_nc_p: <%s><%s> : miss", s.c_str(), s2.c_str());
      return (toupper(*p) < toupper(*p2)) ? -1 : 1;
    }
    ++p;
    ++p2;
  };

  debug(D_STR, "c_nc_p: <%s><%s> : hit", s.c_str(), s2.c_str());
  return 0;

}

/* closeall() - close all FDs >= a specified value */ 
void closeall(int fd) 
{
	int fdlimit = sysconf(_SC_OPEN_MAX); 
	while (fd < fdlimit) 
		close(fd++); 
}

void parse_networklist(char *string, struct networklist *list)
{
	char *token;

	/* make a copy so we don't overwrite argv[] */
	string = strdup(string);

	while ((token = strsep(&string, ", ")))
	{
		char *tnet = strsep(&token, "/");
		char *tmask = token;
		struct in_addr net;
		struct in6_addr net6;

		if (list->num_nets % 10 == 0)
			list->nets = (union net*)realloc(list->nets, sizeof(*list->nets) * (list->num_nets + 10));

		if (inet_pton(AF_INET, tnet, &net))
		{
			struct in_addr mask;
			
			if (tmask)
			{
				if (strchr(tmask, '.') == NULL)
				{
					/* CIDR */
					unsigned int bits;
					int ret;
					ret = sscanf(tmask, "%u", &bits);
					if (ret != 1 || bits > 32)
					{
						fprintf(stderr,"%s: bad CIDR value", tmask);
						exit(1);
					}
					mask.s_addr = htonl(~((1L << (32 - bits)) - 1) & 0xffffffff);
				} else if (!inet_pton(AF_INET6, tmask, &mask))
				{
					fprintf(stderr, "Could not parse \"%s\" as a netmask\n", tmask);
					exit(1);
				}
			} else
				mask.s_addr = 0xffffffff;

			{
				char *snet = strdup(inet_ntoa(net));
				debug(D_MISC, "Adding %s/%s to network list", snet, inet_ntoa(mask));
				free(snet);
			}

			net.s_addr = net.s_addr & mask.s_addr;
			list->nets[list->num_nets].net4.af = AF_INET;
			list->nets[list->num_nets].net4.network = net;
			list->nets[list->num_nets].net4.netmask = mask;
			list->num_nets++;
		} else if (inet_pton(AF_INET6, tnet, &net6))
		{
			int mask;
			
			if (tmask)
			{
				if (sscanf(tmask, "%d", &mask) != 1 || mask > 128)
				{
					fprintf(stderr,"%s: bad CIDR value", tmask);
					exit(1);
				}
			} else
				mask = 128;
			
			list->nets[list->num_nets].net6.af = AF_INET6;
			list->nets[list->num_nets].net6.network = net6;
			list->nets[list->num_nets].net6.netmask = mask;
			list->num_nets++;
		} else
		{
			fprintf(stderr, "Could not parse \"%s\" as a network\n", tnet);
			exit(1);
		}

	}
	free(string);
}

int ip_in_networklist(struct sockaddr *addr, struct networklist *list)
{
	int i;

	if (list->num_nets == 0)
		return 0;
	
	//debug(D_NET, "Checking %s against:", inet_ntoa(ip));
	for (i = 0; i < list->num_nets; i++)
	{
		if (list->nets[i].net.af == AF_INET && addr->sa_family == AF_INET)
		{
			struct in_addr ip = ((struct sockaddr_in *)addr)->sin_addr;
			
			debug(D_NET, "%s", inet_ntoa(list->nets[i].net4.network));
			debug(D_NET, "/%s", inet_ntoa(list->nets[i].net4.netmask));
			if ((ip.s_addr & list->nets[i].net4.netmask.s_addr) == list->nets[i].net4.network.s_addr)
			{
				debug(D_NET, "Hit!");
				return 1;
			}
		} else if (list->nets[i].net.af == AF_INET6 && addr->sa_family == AF_INET6)
		{
			u_int8_t *ip = ((struct sockaddr_in6 *)addr)->sin6_addr.s6_addr;
			int mask, j;
			
			mask = list->nets[i].net6.netmask;
			for (j = 0; j < 16 && mask > 0; j++, mask -= 8)
			{
				unsigned char bytemask;
				
				bytemask = (mask < 8) ? ~((1L << (8 - mask)) - 1) : 0xff;
				
				if ((ip[j] & bytemask) != (list->nets[i].net6.network.s6_addr[j] & bytemask))
					break;
			}
			
			if (mask <= 0)
			{
				debug(D_NET, "Hit!");
				return 1;
			}
		}
	}

	return 0;
}

char *strlwr(char *str)
{
    char *s = str;
    while (*s)
    {
        *s = tolower(*s);
        s++;
    }
    return str;
}

/* Log a message about missing milter macros, but only the first time */
void warnmacro(const char *macro, const char *scope)
{
	if (warnedmacro)
		return;
	debug(D_ALWAYS, "Could not retrieve sendmail macro \"%s\"!.  Please add it to confMILTER_MACROS_%s for better spamassassin results",
		macro, scope);
	warnedmacro = true;
}

/*
   untrusted-argument-safe popen function - only supports "r" and "w" modes
   for simplicity, and always reads stdout and stderr in "r" mode.  Call
   fclose to close the FILE, and waitpid to reap the child process (pid).
*/
FILE *popenv(char *const argv[], const char *type, pid_t *pid)
{
	FILE *iop;
	int pdes[2];
	int save_errno;

	if ((*type != 'r' && *type != 'w') || type[1])
	{
		errno = EINVAL;
		return (NULL);
	}
	if (pipe(pdes) < 0)
		return (NULL);
	switch (*pid = fork()) {
	
	case -1:			/* Error. */
		save_errno = errno;
		(void)close(pdes[0]);
		(void)close(pdes[1]);
		errno = save_errno;
		return (NULL);
		/* NOTREACHED */
	case 0:				/* Child. */
		if (*type == 'r') {
			/*
			 * The dup2() to STDIN_FILENO is repeated to avoid
			 * writing to pdes[1], which might corrupt the
			 * parent's copy.  This isn't good enough in
			 * general, since the exit() is no return, so
			 * the compiler is free to corrupt all the local
			 * variables.
			 */
			(void)close(pdes[0]);
			(void)dup2(pdes[1], STDOUT_FILENO);
			(void)dup2(pdes[1], STDERR_FILENO);
			if (pdes[1] != STDOUT_FILENO && pdes[1] != STDERR_FILENO) {
				(void)close(pdes[1]);
			} 
		} else {
			if (pdes[0] != STDIN_FILENO) {
				(void)dup2(pdes[0], STDIN_FILENO);
				(void)close(pdes[0]);
			}
			(void)close(pdes[1]);
		}
		execv(argv[0], argv);
		exit(127);
		/* NOTREACHED */
	}

	/* Parent; assume fdopen can't fail. */
	if (*type == 'r') {
		iop = fdopen(pdes[0], type);
		(void)close(pdes[1]);
	} else {
		iop = fdopen(pdes[1], type);
		(void)close(pdes[0]);
	}

	return (iop);
}

// }}}
// vim6:ai:noexpandtab