File: smtppass.c

package info (click to toggle)
clamsmtp 1.10-7
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,352 kB
  • ctags: 333
  • sloc: sh: 3,919; ansic: 3,286; makefile: 20
file content (2102 lines) | stat: -rw-r--r-- 61,074 bytes parent folder | download | duplicates (13)
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
/*
 * Copyright (c) 2004, Stefan Walter
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 *     * Redistributions of source code must retain the above 
 *       copyright notice, this list of conditions and the 
 *       following disclaimer.
 *     * 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.
 *     * The names of contributors to this software may not be 
 *       used to endorse or promote products derived from this 
 *       software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "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 THE 
 * COPYRIGHT OWNER OR CONTRIBUTORS 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.
 * 
 *
 * CONTRIBUTORS
 *  Stefan Walter <stef@memberwebs.com>
 *  Andreas Steinmetz <ast@domdv.de>
 *  Rubio Vaughan <rubio@passim.net>
 *  Olivier Beyssac <ob@r14.freenix.org>
 */ 

#define _GNU_SOURCE

#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <sys/stat.h>

#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
#include <signal.h>
#include <errno.h>
#include <stdarg.h>
#include <pwd.h>
#include <time.h>

#include "usuals.h"

#ifdef LINUX_TRANSPARENT_PROXY
#include <linux/netfilter_ipv4.h>
#endif

#include "compat.h"
#include "sock_any.h"
#include "stringx.h"
#include "sppriv.h"

/* -----------------------------------------------------------------------
 *  STRUCTURES
 */

typedef struct spthread
{
    pthread_t tid;      /* Written to by the main thread */
    int fd;             /* The file descriptor or -1 */
}
spthread_t;

/* -----------------------------------------------------------------------
 *  DATA
 */

#define CRLF                "\r\n"

#define SMTP_TOOLONG        "500 Line too long" CRLF
#define SMTP_STARTBUSY      "421 Server busy, too many connections" CRLF
#define SMTP_STARTFAILED    "421 Local Error, cannot start thread" CRLF
#define SMTP_DATAINTERMED   "354 Start mail input; end with <CRLF>.<CRLF>" CRLF
#define SMTP_FAILED         "451 Local Error" CRLF
#define SMTP_NOTSUPP        "502 Command not implemented" CRLF
#define SMTP_NOTAUTH        "554 Insufficient authorization" CRLF
#define SMTP_OK             "250 Ok" CRLF
#define SMTP_REJPREFIX      "550 Content Rejected; "

#define SMTP_DATA           "DATA" CRLF
#define SMTP_NOOP           "NOOP" CRLF
#define SMTP_RSET           "RSET" CRLF
#define SMTP_XCLIENT        "XCLIENT ADDR=%s" CRLF
#define SMTP_BANNER         "220 smtp.passthru" CRLF
#define SMTP_HELO_RSP       "250 smtp.passthru" CRLF
#define SMTP_EHLO_RSP       "250-smtp.passthru" CRLF
#define SMTP_FEAT_RSP       "250 XFILTERED" CRLF
#define SMTP_DELIMS         "\r\n\t :"
#define SMTP_MULTI_DELIMS   " -"

#define ESMTP_PIPELINE      "PIPELINING"
#define ESMTP_TLS           "STARTTLS"
#define ESMTP_CHUNK         "CHUNKING"
#define ESMTP_BINARY        "BINARYMIME"
#define ESMTP_CHECK         "CHECKPOINT"
#define ESMTP_XCLIENT       "XCLIENT"
#define ESMTP_XEXCH50       "XEXCH50" 

#define HELO_CMD            "HELO"
#define EHLO_CMD            "EHLO"
#define FROM_CMD            "MAIL FROM"
#define TO_CMD              "RCPT TO"
#define DATA_CMD            "DATA"
#define RSET_CMD            "RSET"
#define STARTTLS_CMD        "STARTTLS"
#define BDAT_CMD            "BDAT"
#define XCLIENT_CMD         "XCLIENT"
#define XFORWARD_CMD        "XFORWARD"

#define DATA_END_SIG        "." CRLF

#define DATA_RSP            "354"
#define OK_RSP              "250"
#define START_RSP           "220"

#define RCVD_HEADER         "Received:"

/* The set of delimiters that can be present between config and value */
#define CFG_DELIMS      	": \t"

/* Maximum length of the header argument */
#define MAX_HEADER_LENGTH 	1024

/*
 * asctime_r manpage: "stores the string in a user-supplied buffer of
 * length at least 26".  We'll need some more bytes to put timezone
 * information behind
 */
#define MAX_DATE_LENGTH 	64

#define LINE_TOO_LONG(l)    ((l) >= (SP_LINE_LENGTH - 2))

/* -----------------------------------------------------------------------
 *  CONFIGURATION OPTIONS
 * 
 * - Be sure that your configuration option needs to go into this 
 *   file. More likely it'll go into clamsmtpd.c 
 * - When adding configuration options follow the instructions in 
 *   clamsmtpd.c, except add option to spstate_t (sppriv.h) and parse in 
 *   sp_parse_option (below)
 */
 
#define CFG_MAXTHREADS      "MaxConnections"
#define CFG_TIMEOUT         "TimeOut"
#define CFG_OUTADDR         "OutAddress"
#define CFG_LISTENADDR      "Listen"
#define CFG_TRANSPARENT     "TransparentProxy"
#define CFG_DIRECTORY       "TempDirectory"
#define CFG_KEEPALIVES      "KeepAlives"
#define CFG_USER            "User"
#define CFG_PIDFILE         "PidFile"
#define CFG_XCLIENT         "XClient"

/* -----------------------------------------------------------------------
 *  DEFAULT SETTINGS
 */

#define DEFAULT_SOCKET  "10025"
#define DEFAULT_PORT    10025
#define DEFAULT_MAXTHREADS  64
#define DEFAULT_TIMEOUT   180
#define DEFAULT_KEEPALIVES 0

/* -----------------------------------------------------------------------
 *  GLOBALS
 */

spstate_t g_state;                          /* The state and configuration of the daemon */
unsigned int g_unique_id = 0x00100000;      /* For connection ids */
pthread_mutex_t g_mutex;                    /* The main mutex */
pthread_mutexattr_t g_mtxattr;
 
/* -----------------------------------------------------------------------
 *  FORWARD DECLARATIONS
 */

static void on_quit(int signal);
static void drop_privileges();
static void pid_file(int write);
static void connection_loop(int sock);
static void* thread_main(void* arg);
static int smtp_passthru(spctx_t* ctx);
static int make_connections(spctx_t* ctx, int client);
static int read_server_response(spctx_t* ctx);
static int parse_config_file(const char* configfile);
static char* parse_address(char* line);
static char* parse_xforward(char* line, const char* part);
static const char* get_successful_rsp(const char* line, int* cont);
static void do_server_noop(spctx_t* ctx);

/* Used externally in some cases */
int sp_parse_option(const char* name, const char* option);

/* ----------------------------------------------------------------------------------
 *  BASIC RUN FUNCTIONALITY 
 */

void sp_init(const char* name)
{
    int r;
    
    ASSERT(name);
    
    memset(&g_state, 0, sizeof(g_state));
    
    sp_message(NULL, LOG_DEBUG, "%s (%s)", name, VERSION);    
    
    /* Setup the defaults */
    g_state.debug_level = -1;
    g_state.max_threads = DEFAULT_MAXTHREADS;
    g_state.timeout.tv_sec = DEFAULT_TIMEOUT;
    g_state.keepalives = DEFAULT_KEEPALIVES;
    g_state.directory = _PATH_TMP;
    g_state.name = name;
    
    /* We need the default to parse into a useable form, so we do this: */
    r = sp_parse_option(CFG_LISTENADDR, DEFAULT_SOCKET);
    ASSERT(r == 1);
    
    /* Create the main mutex and condition variable */  
    if(pthread_mutexattr_init(&g_mtxattr) != 0 ||
#ifdef HAVE_ERR_MUTEX
       pthread_mutexattr_settype(&g_mtxattr, MUTEX_TYPE) ||
#endif       
       pthread_mutex_init(&g_mutex, &g_mtxattr) != 0)
        errx(1, "threading problem. can't create mutex or condition var");  
}
    
int sp_run(const char* configfile, const char* pidfile, int dbg_level)
{
    int sock;
    int true = 1;
    
    ASSERT(configfile);
    ASSERT(g_state.name);
    
    if(!(dbg_level == -1 || dbg_level <= LOG_DEBUG))
        errx(2, "invalid debug log level (must be between 1 and 4)");  
    g_state.debug_level = dbg_level;
    g_state.pidfile = pidfile;
    
    /* Now parse the configuration file */
    if(parse_config_file(configfile) == -1)
    {
        /* 
         * We used to do a check here before whether it was the default
         * configuration file or not, but we can't do that any longer
         * as it comes from the app. Usually lack of a configuration 
         * file will cause the following checks to fail
         */
         warnx("configuration file not found: %s", configfile);
    }
    
    /* This option has no default, but is required ... */
    if(g_state.outname == NULL && !g_state.transparent)
        errx(2, "no " CFG_OUTADDR " specified.");
        
    /* ... unless we're in transparent proxy mode */
    else if(g_state.outname != NULL && g_state.transparent)
        warnx("the " CFG_OUTADDR " option will be ignored when " CFG_TRANSPARENT " is enabled");

    sp_messagex(NULL, LOG_DEBUG, "starting up (%s)...", VERSION);

    /* Drop privileges before daemonizing */
    drop_privileges();
    
    /* When set to this we daemonize */
    if(g_state.debug_level == -1)
    {
        /* Fork a daemon nicely here */
        if(daemon(0, 0) == -1)
        {
            sp_message(NULL, LOG_ERR, "couldn't run as daemon");
            exit(1);
        }
      
        sp_messagex(NULL, LOG_DEBUG, "running as a daemon");
        g_state.daemonized = 1;

        /* Open the system log */
        openlog(g_state.name, 0, LOG_MAIL);
    }
    
    /* Handle some signals */
    signal(SIGPIPE, SIG_IGN); 
    signal(SIGHUP, SIG_IGN);
    signal(SIGINT, on_quit);
    signal(SIGTERM, on_quit);

    siginterrupt(SIGINT, 1);
    siginterrupt(SIGTERM, 1);
    
    /* Create the socket */
    sock = socket(SANY_TYPE(g_state.listenaddr), SOCK_STREAM, 0);
    if(sock < 0)
    {
        sp_message(NULL, LOG_CRIT, "couldn't open socket");
        exit(1);
    }

    fcntl(sock, F_SETFD, fcntl(sock, F_GETFD, 0) | FD_CLOEXEC);    
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&true, sizeof(true));
    
    /* Unlink the socket file if it exists */
    if(SANY_TYPE(g_state.listenaddr) == AF_UNIX)
        unlink(g_state.listenname);

    if(bind(sock, &SANY_ADDR(g_state.listenaddr), SANY_LEN(g_state.listenaddr)) != 0)
    {
        sp_message(NULL, LOG_CRIT, "couldn't bind to address: %s", g_state.listenname);
        exit(1);
    }
    
    sp_messagex(NULL, LOG_DEBUG, "created socket: %s", g_state.listenname);
    
    /* Let 5 connections queue up */
    if(listen(sock, 5) != 0)
    {
        sp_message(NULL, LOG_CRIT, "couldn't listen on socket");
        exit(1);
    }
    
    pid_file(1);

    sp_messagex(NULL, LOG_DEBUG, "accepting connections");
    
    connection_loop(sock);

    pid_file(0);

    /* Our listen socket */
    close(sock);
            
    sp_messagex(NULL, LOG_DEBUG, "stopped processing");
    return 0;
}

void sp_quit()
{
    /* The handler sets the flag and this also interrupts io */
    kill(getpid(), SIGTERM);
}

int sp_is_quit()
{
    return g_state.quit ? 1 : 0;
}

void sp_done()
{
    /* Close the mutex */
    pthread_mutex_destroy(&g_mutex);
    pthread_mutexattr_destroy(&g_mtxattr);   
    
    if(g_state._p)
        free(g_state._p);

    memset(&g_state, 0, sizeof(g_state));
}

static void on_quit(int signal)
{
    g_state.quit = 1;
}

static void drop_privileges()
{
    char* t;
    struct passwd* pw;
    uid_t uid;
    
    if(g_state.user)
    {
        if(geteuid() != 0)
        {
            sp_messagex(NULL, LOG_WARNING, "must be started as root to switch to user: %s", g_state.user);
            return;
        }
        
        uid = strtol(g_state.user, &t, 10);
        if(!t[0]) /* successful parse */
            pw = getpwuid(uid);
        else  /* must be a name */
            pw = getpwnam(g_state.user);

        if(pw == NULL)            
            errx(1, "couldn't look up user: %s", g_state.user);
        
        if(setgid(pw->pw_gid) == -1 ||
           setuid(pw->pw_uid) == -1)
            err(1, "unable to switch to user: %s (uid %d, gid %d)", g_state.user, pw->pw_uid, pw->pw_gid);
            
        /* A paranoia check */
        if(setreuid(-1, 0) == 0)
            err(1, "unable to completely drop privileges");
            
        sp_messagex(NULL, LOG_DEBUG, "switched to user %s (uid %d, gid %d)", g_state.user, pw->pw_uid, pw->pw_gid);
    }

    if(geteuid() == 0)
        sp_messagex(NULL, LOG_WARNING, "running as root is NOT recommended");
}
  

static void pid_file(int write)
{
    if(!g_state.pidfile)
        return;
        
    if(write)
    {
        FILE* f = fopen(g_state.pidfile, "w");
        if(f == NULL)
        {
            sp_message(NULL, LOG_ERR, "couldn't open pid file: %s", g_state.pidfile);
        }
        else
        {  
            fprintf(f, "%d\n", (int)getpid());
  
            if(ferror(f))
                sp_message(NULL, LOG_ERR, "couldn't write to pid file: %s", g_state.pidfile);
            if(fclose(f) == EOF)
                sp_message(NULL, LOG_ERR, "couldn't write to pid file: %s", g_state.pidfile);
                
        }
        
        sp_messagex(NULL, LOG_DEBUG, "wrote pid file: %s", g_state.pidfile);
    }
    
    else
    {
        unlink(g_state.pidfile);
        sp_messagex(NULL, LOG_DEBUG, "removed pid file: %s", g_state.pidfile);
    }
} 

static void connection_loop(int sock)
{
    spthread_t* threads = NULL;
    int fd, i, x, r;

    /* Create the thread buffers */
    threads = (spthread_t*)calloc(g_state.max_threads, sizeof(spthread_t));
    if(!threads) 
    {
        sp_messagex(NULL, LOG_CRIT, "out of memory");
        return;
    }

    /* Now loop and accept the connections */
    while(!sp_is_quit())
    {
        fd = accept(sock, NULL, NULL);
        if(fd == -1)
        {
            switch(errno)
            {
            case EINTR:
            case EAGAIN:
                break;

            case ECONNABORTED:
                sp_message(NULL, LOG_ERR, "couldn't accept a connection");
                break;

            default:
                sp_message(NULL, LOG_ERR, "couldn't accept a connection");
                break;          
            };

            if(sp_is_quit())
                break;                

            continue;
        }

        /* Set timeouts on client */
        if(setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &(g_state.timeout), sizeof(g_state.timeout)) < 0 ||
           setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &(g_state.timeout), sizeof(g_state.timeout)) < 0)
            sp_message(NULL, LOG_DEBUG, "couldn't set timeouts on incoming connection");        

        fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);    

        /* Look for thread and also clean up others */
        for(i = 0; i < g_state.max_threads; i++)
        {
            /* Find a thread to run or clean up old threads */
            if(threads[i].tid != 0)
            {
                sp_lock();
                    x = threads[i].fd;
                sp_unlock();

                if(x == -1)
                {
                    sp_messagex(NULL, LOG_DEBUG, "cleaning up completed thread");
                    pthread_join(threads[i].tid, NULL);
                    threads[i].tid = 0;
                }
#ifdef _DEBUG
                else
                {
                    /* For debugging connection problems: */
                    sp_messagex(NULL, LOG_DEBUG, "active connection thread: %x", (int)threads[i].tid);
                }
#endif                    
            }

            /* Start a new thread if neccessary */
            if(fd != -1 && threads[i].tid == 0)
            {
                threads[i].fd = fd;
                r = pthread_create(&(threads[i].tid), NULL, thread_main, 
                                   (void*)(threads + i));
                if(r != 0)
                {
                    errno = r;
                    sp_message(NULL, LOG_ERR, "couldn't create thread");

                    write(fd, SMTP_STARTFAILED, KL(SMTP_STARTFAILED));
                    shutdown(fd, SHUT_RDWR);
                    close(fd);
                    fd = -1;
                    break;
                }

                sp_messagex(NULL, LOG_DEBUG, "created thread for connection");
                fd = -1;
                break;
            }
        }

        /* Check to make sure we have a thread */
        if(fd != -1)
        {
            sp_messagex(NULL, LOG_ERR, "too many connections open (max %d). sent busy response", g_state.max_threads);
            write(fd, SMTP_STARTBUSY, KL(SMTP_STARTBUSY));
            shutdown(fd, SHUT_RDWR);
            close(fd);
            fd = -1;
        }
    }

    sp_messagex(NULL, LOG_DEBUG, "waiting for threads to quit");

    /* Quit all threads here */
    for(i = 0; i < g_state.max_threads; i++)
    {
        /* Clean up quit threads */
        if(threads[i].tid != 0)
        {
            if(threads[i].fd != -1)
            {
                sp_lock();
                    fd = threads[i].fd;
                    threads[i].fd = -1;
                sp_unlock();
                
                shutdown(fd, SHUT_RDWR);
                close(fd);
            }
          
            sp_messagex(NULL, LOG_DEBUG, "cleaning up completed thread");
            pthread_join(threads[i].tid, NULL);
            threads[i].tid = 0;
        }
    }
    
    free(threads);
}

static spctx_t* init_thread(int fd)
{
    spctx_t* ctx;
    
    ctx = cb_new_context();
    if(ctx)
    {
        memset(ctx, 0, sizeof(*ctx));

        spio_init(&(ctx->server), "SERVER");
        spio_init(&(ctx->client), "CLIENT");
        
        sp_lock();
            /* Assign a unique id to the connection */
            ctx->id = g_unique_id++;
            
            /* We don't care about wraps, but we don't want zero */
            if(g_unique_id == 0)
                g_unique_id++;
        sp_unlock();    
            
        sp_messagex(ctx, LOG_DEBUG, "processing %d on thread %x", fd, (int)pthread_self());
        
        /* Connect to the outgoing server ... */
        if(make_connections(ctx, fd) == -1)
        {
            cb_del_context(ctx);
            ctx = NULL;
        }
    }            
    
    return ctx;
}

static void cleanup_context(spctx_t* ctx)
{
    ASSERT(ctx);

    if(ctx->cachefile)
    {
        fclose(ctx->cachefile);
        ctx->cachefile = NULL;
    }
        
    if(ctx->cachename[0])
    {
        unlink(ctx->cachename);
        ctx->cachename[0] = 0;
    }
    
    if(ctx->recipients)
    {
        free(ctx->recipients);
        ctx->recipients = NULL;
    }
    
    if(ctx->sender)
    {
        free(ctx->sender);
        ctx->sender = NULL;
    }
    
    if(ctx->xforwardaddr)
    {
        free(ctx->xforwardaddr);
        ctx->xforwardaddr = NULL;
    }
    
    if(ctx->xforwardhelo)
    {
    	free(ctx->xforwardhelo);
    	ctx->xforwardhelo = NULL;
    }
    
    ctx->logline[0] = 0;
}


static void done_thread(spctx_t* ctx)
{
    ASSERT(ctx);
     
    spio_disconnect(ctx, &(ctx->client));
    spio_disconnect(ctx, &(ctx->server));
    
    /* Clean up file stuff */
    cleanup_context(ctx);            
    cb_del_context(ctx);
}

static void* thread_main(void* arg)
{
    spthread_t* thread = (spthread_t*)arg;
    spctx_t* ctx = NULL;
    int processing = 0;
    int ret = 0;
    int fd;
    
    ASSERT(thread);

    siginterrupt(SIGINT, 1);
    siginterrupt(SIGTERM, 1);

    sp_lock();
        /* Get the client socket */
        fd = thread->fd;
    sp_unlock();    

    /* Sometimes we get to this point and then quit is noted */    
    if(sp_is_quit() || (ctx = init_thread(fd)) == NULL)
    {
        /* Special case. We don't have a context so clean up descriptor */
        close(fd);

        /* new_context() should have already logged reason */
        RETURN(-1);
    }

    /* call the processor */
    processing = 1;
    ret = smtp_passthru(ctx);
    
cleanup:

    if(ctx)
    {
        /* Let the client know about fatal errors */
        if(!processing && ret == -1 && spio_valid(&(ctx->client)))
           spio_write_data(ctx, &(ctx->client), SMTP_STARTFAILED);
    
        done_thread(ctx);
    }
    
    /* mark this as done */
    sp_lock();
        thread->fd = -1;
    sp_unlock();

    return (void*)(ret == 0 ? 0 : 1);
}

static int make_connections(spctx_t* ctx, int client)
{
    struct sockaddr_any peeraddr;
    struct sockaddr_any addr;
    struct sockaddr_any* outaddr;
    char buf[MAXPATHLEN];
    const char* outname;
    
    ASSERT(client != -1);

    /* Setup the incoming connection. This also fills in peeraddr for us */
    spio_attach(ctx, &(ctx->client), client, &peeraddr);
    sp_messagex(ctx, LOG_INFO, "accepted connection from: %s", ctx->client.peername);

    /* Create the server connection address */
    outaddr = &(g_state.outaddr); 
    outname = g_state.outname;
        
    /* For transparent proxying we have to discover the address to connect to */
    if(g_state.transparent)
    {
        memset(&addr, 0, sizeof(addr));
        SANY_LEN(addr) = sizeof(addr);
        
#ifdef LINUX_TRANSPARENT_PROXY
        if(getsockopt(ctx->client.fd, SOL_IP, SO_ORIGINAL_DST, &SANY_ADDR(addr), &SANY_LEN(addr)) == -1)
#else        
        if(getsockname(ctx->client.fd, &SANY_ADDR(addr), &SANY_LEN(addr)) == -1)
#endif
        {
            sp_message(ctx, LOG_ERR, "couldn't get source address for transparent proxying");
            return -1;
        }
        
        /* Check address types */
        if(sock_any_cmp(&addr, &peeraddr, SANY_OPT_NOPORT) == 0)
        {
            sp_messagex(ctx, LOG_ERR, "loop detected in transparent proxying");
            return -1;
        }
        
        outaddr = &addr;
    }
    
    /* Check for loopback option */
    else if(SANY_TYPE(*outaddr) == AF_INET && 
            outaddr->s.in.sin_addr.s_addr == 0)
    {
        /* Use the incoming IP as the default */
        memcpy(&addr, &(g_state.outaddr), sizeof(addr));
        memcpy(&(addr.s.in.sin_addr), &(peeraddr.s.in.sin_addr), sizeof(addr.s.in.sin_addr));
        outaddr = &addr;
    }
#ifdef HAVE_INET6        
    /* IPv6 loopback? */
    else if(SANY_TYPE(*outaddr) == AF_INET6 && 
            outaddr->s.in.in6.sin_addr.s_addr == 0)
    {
        /* Use the incoming IP as the default */
        memcpy(&addr, &(g_state.outaddr), sizeof(addr));
        memcpy(&(addr.s.in.sin6_addr), &(peeraddr.s.in.sin6_addr), sizeof(addr.s.in.sin6_addr));
        outaddr = &addr;
    }
#endif

    /* Not transparent proxy or loopback */
    else 
    {
        /* Resolve any DNS name again */
        if(sock_any_pton(g_state.outname, &addr, SANY_OPT_DEFPORT(25)) != -1)
            memcpy(&(g_state.outaddr), &addr, sizeof(g_state.outaddr));
        else
            sp_messagex(ctx, LOG_WARNING, "couldn't resolve " CFG_OUTADDR ": %s", g_state.outname);
    }
           
    /* Reparse name if needed */
    if(outaddr != &(g_state.outaddr))
    {
        if(sock_any_ntop(outaddr, buf, MAXPATHLEN, 0) != -1)
            outname = buf;
        else
            outname = "unknown";
    }
    
    /* Connect to the server */
    if(spio_connect(ctx, &(ctx->server), outaddr, outname) == -1)
        return -1;
        
    return 0;
}

/* ----------------------------------------------------------------------------------
 *  SMTP HANDLING
 */
 
static int smtp_passthru(spctx_t* ctx)
{
    char* t;
    const char* p;
    int r, cont, ret = 0;
    unsigned int mask;
    int neterror = 0;

    int first_rsp = 1;      /* The first 220 response from server to be filtered */
    int filter_host = 0;    /* Next response is 250 hostname, which we change */
  
    /* XCLIENT is for use in access control */
    int xclient_sup = 0;    /* Is XCLIENT supported? */
    int xclient_sent = 0;   /* Have we sent an XCLIENT command? */
        
    ASSERT(spio_valid(&(ctx->client)) &&
           spio_valid(&(ctx->server)));
           
    #define C_LINE  ctx->client.line
    #define S_LINE  ctx->server.line

    while(!sp_is_quit())
    {
        mask = spio_select(ctx, &(ctx->client), &(ctx->server), NULL);
        
        if(mask == ~0)
        {
            neterror = 1;
            RETURN(-1);
		}

        /* Client has data available, read a line and process */
        if(mask & 1)
        {
            if((r = spio_read_line(ctx, &(ctx->client), SPIO_DISCARD)) == -1)
                RETURN(-1);

            /* Client disconnected, we're done */
            if(r == 0)
                RETURN(0);

            /* We don't let clients send really long lines */
            if(LINE_TOO_LONG(r))
            {
                if(spio_write_data(ctx, &(ctx->client), SMTP_TOOLONG) == -1)
                    RETURN(-1);

                continue;
            }
            
            /* Only valid after EHLO or HELO commands */
            filter_host = 0;
            
            /* 
             * At this point we may want to send our XCLIENT. This is a per 
             * connection command. 
             */
            if(xclient_sup && !xclient_sent && g_state.xclient)
            {
                sp_messagex(ctx, LOG_DEBUG, "sending XCLIENT");
                
                if(spio_write_dataf(ctx, &(ctx->server), SMTP_XCLIENT, ctx->client.peername) == -1)
                    RETURN(-1);

                if(read_server_response(ctx) == -1)
                    RETURN(-1);
                
                if(!get_successful_rsp(S_LINE, NULL))
                    sp_messagex(ctx, LOG_WARNING, "server didn't accept XCLIENT");
                    
                xclient_sent = 1;
            }

            /* Handle the DATA section via our AV checker */
            if(is_first_word(C_LINE, DATA_CMD, KL(DATA_CMD)))
            {
                /* Send back the intermediate response to the client */
                if(spio_write_data(ctx, &(ctx->client), SMTP_DATAINTERMED) == -1)
                    RETURN(-1);
                    
                /* 
                 * Now go into scan mode. This also handles the eventual
                 * sending of the data to the server, making the av check
                 * transparent
                 */
                if(cb_check_data(ctx) == -1)
                    RETURN(-1);

                /* Print the log out for this email */
                sp_messagex(ctx, LOG_INFO, "%s", ctx->logline);
                    
                /* Done with that email */
                cleanup_context(ctx);

                /* Command handled */
                continue;
            }
                
            /*
             * We need our response to HELO and EHLO to be modified in order 
             * to prevent complaints about mail loops
             */
            else if(is_first_word(C_LINE, EHLO_CMD, KL(EHLO_CMD)))
            {
                /* EHLO can have multline responses so we set a flag */
                filter_host = 1;
            }
            
            /* 
             * We always support XCLIENT on a HELO type connection. We do this
             * for security reasons, so that a client can't get around filtering
             * by backing up one on the protocol.
             */                
            else if(is_first_word(C_LINE, HELO_CMD, KL(HELO_CMD)))
            {
                sp_messagex(ctx, LOG_DEBUG, "XCLIENT support assumed");
                xclient_sup = 1;
                
                /* Filter host as with EHLO above */
                filter_host = 1;
            }
            
            /* 
             * We don't like these commands. Filter them out. We should have
             * filtered out their service extensions earlier in the EHLO response.
             * This is just for errant clients.
             */
            else if(is_first_word(C_LINE, STARTTLS_CMD, KL(STARTTLS_CMD)) ||
                    is_first_word(C_LINE, BDAT_CMD, KL(BDAT_CMD)))
            {
                sp_messagex(ctx, LOG_DEBUG, "ESMTP feature not supported");
                
                if(spio_write_data(ctx, &(ctx->client), SMTP_NOTSUPP) == -1)
                    RETURN(-1);
                    
                /* Command handled */
                continue;
            }
                        
            /* 
             * For security reasons we're not about to forward any XCLIENTs
             * from our client through. This could lead to a client using our 
             * privileged IP address to change an audit trail or relay etc...
             */
            else if(is_first_word(C_LINE, XCLIENT_CMD, KL(XCLIENT_CMD)))
            {
                sp_messagex(ctx, LOG_WARNING, "client attempted use of privileged XCLIENT feature");
                
                if(spio_write_data(ctx, &(ctx->client), SMTP_NOTAUTH) == -1)
                    RETURN(-1);
                    
                /* Command handled */
                continue;
            }
            
            /* All other commands just get passed through to server */
            if(spio_write_data(ctx, &(ctx->server), C_LINE) == -1)
                RETURN(-1);

            continue;
        }

        /* Server has data available, read a line and forward */
        if(mask & 2)
        {
            if((r = spio_read_line(ctx, &(ctx->server), SPIO_DISCARD)) == -1)
                RETURN(-1);

            if(r == 0)
                RETURN(0);

            if(LINE_TOO_LONG(r))
                sp_messagex(ctx, LOG_WARNING, "SMTP response line too long. discarded extra");

            /* 
             * We intercept the first response we get from the server.
             * This allows us to change header so that it doesn't look
             * to the client server that we're in a wierd loop. 
             * 
             * In different situations using the local hostname or 
             * 'localhost' don't work because the receiving mail server
             * expects one of those to be its own name. We use 'clamsmtp'
             * instead. No properly configured server would have this 
             * as their domain name, and RFC 2821 allows us to use 
             * an arbitrary but identifying string.
             */
            if(first_rsp)
            {
                first_rsp = 0;

                if(is_first_word(S_LINE, START_RSP, KL(START_RSP)))
                {
                    sp_messagex(ctx, LOG_DEBUG, "intercepting initial response");

                    if(spio_write_data(ctx, &(ctx->client), SMTP_BANNER) == -1)
                        RETURN(-1);

                    /* Command handled */
                    continue;
                }
            }

            if((p = get_successful_rsp(S_LINE, &cont)) != NULL)
            {            
                /* 
                 * Certain mail servers (Postfix 1.x in particular) do a loop check 
                 * on the 250 response after a EHLO or HELO. This is where we
                 * filter that to prevent loopback errors.
                 */
                if(filter_host)
                {
                    /* Can have multi-line responses, and we want to be 
                     * sure to only replace the first one. */
                    filter_host = 0;
                
                    sp_messagex(ctx, LOG_DEBUG, "intercepting host response");

                    if(spio_write_data(ctx, &(ctx->client), 
                           cont ? SMTP_EHLO_RSP : SMTP_HELO_RSP) == -1)
                        RETURN(-1);      

                    /* A new email so cleanup */
                    cleanup_context(ctx);
                        
                    continue;
                }
                              
                /* 
                 * Filter out any EHLO responses that we can't or don't want
                 * to support. For example pipelining or TLS. 
                 */
                if(is_first_word(C_LINE, EHLO_CMD, KL(EHLO_CMD)))
                {
                    /* 
                     * On ESMTP connections we let the server tell us whether it
                     * wants XCLIENTs or not. (In contrast to old SMTP above).
                     */
                    if(is_first_word(p, ESMTP_XCLIENT, KL(ESMTP_XCLIENT)))
                    {
                        sp_messagex(ctx, LOG_DEBUG, "XCLIENT supported");
                        xclient_sup = 1;
                    }
                    
                    if(is_first_word(p, ESMTP_PIPELINE, KL(ESMTP_PIPELINE)) ||
                       is_first_word(p, ESMTP_TLS, KL(ESMTP_TLS)) ||
                       is_first_word(p, ESMTP_CHUNK, KL(ESMTP_CHUNK)) ||
                       is_first_word(p, ESMTP_BINARY, KL(ESMTP_BINARY)) ||
                       is_first_word(p, ESMTP_CHECK, KL(ESMTP_CHECK)) ||
                       is_first_word(p, ESMTP_XCLIENT, KL(ESMTP_XCLIENT)) ||
                       is_first_word(p, ESMTP_XEXCH50, KL(ESMTP_XEXCH50)))
                    {
                        sp_messagex(ctx, LOG_DEBUG, "filtered ESMTP feature: %s", trim_space((char*)p));
                        
                        /* 
                         * If this is the last line in the EHLO response we need
                         * to replace it with something else 
                         */
                        if(!cont)
                        {
                            if(spio_write_data(ctx, &(ctx->client), SMTP_FEAT_RSP) == -1)
                                RETURN(-1);                            
                        }
                        
                        continue;
                    }
                }

                /* MAIL FROM (that the server accepted) */
                if((r = check_first_word(C_LINE, FROM_CMD, KL(FROM_CMD), SMTP_DELIMS)) > 0)
                {
                    t = parse_address(C_LINE + r);
                    sp_add_log(ctx, "from=", t);

                    /* Make note of the sender for later */
                    ctx->sender = (char*)reallocf(ctx->sender, strlen(t) + 1);
                    if(ctx->sender)
                        strcpy(ctx->sender, t);
                }
                
                /* RCPT TO (that the server accepted) */
                else if((r = check_first_word(C_LINE, TO_CMD, KL(TO_CMD), SMTP_DELIMS)) > 0)
                {
                    t = parse_address(C_LINE + r);
                    sp_add_log(ctx, "to=", t);
                    
                    /* Make note of the recipient for later */
                    r = ctx->recipients ? strlen(ctx->recipients) : 0;
                    ctx->recipients = (char*)reallocf(ctx->recipients, r + strlen(t) + 2);
                    if(ctx->recipients)
                    {
                        /* Recipients are separated by lines */
                        if(r != 0)
                            strcat(ctx->recipients, "\n");
                        else
                            ctx->recipients[0] = 0;
                            
                        strcat(ctx->recipients, t);
                    }
                }
                            
                /*
                 * If the client sends an XFORWARD, and the server accepted it, 
                 * we store address for use in our forked process environment 
                 * variables (see sp_setup_forked). 
                 */
                else if(is_first_word(C_LINE, XFORWARD_CMD, KL(XFORWARD_CMD)))
                {
                    if((t = parse_xforward (C_LINE + KL(XFORWARD_CMD), "ADDR"))) 
                    {
                        ctx->xforwardaddr = (char*)reallocf(ctx->xforwardaddr, strlen(t) + 1);
                        if(ctx->xforwardaddr)
                            strcpy(ctx->xforwardaddr, t);
                    }
                    
                    if((t = parse_xforward (C_LINE + KL(XFORWARD_CMD), "HELO"))) 
                    {
                        ctx->xforwardhelo = (char*)reallocf(ctx->xforwardhelo, strlen(t) + 1);
                        if(ctx->xforwardhelo)
                            strcpy(ctx->xforwardhelo, t);
                    }
                    
                }
                
                /* RSET */
                else if(is_first_word(C_LINE, RSET_CMD, KL(RSET_CMD)))
                {
                    cleanup_context(ctx);
                }
            }
                            
            if(spio_write_data(ctx, &(ctx->client), S_LINE) == -1)
                RETURN(-1);

            continue;
        }        
    }
        
cleanup:

    if(!neterror && ret == -1 && spio_valid(&(ctx->client)))
       spio_write_data(ctx, &(ctx->client), SMTP_FAILED);
        
    return ret;
}

/* -----------------------------------------------------------------------------
 *  SMTP PASSTHRU FUNCTIONS FOR DATA CHECK
 */

static char* parse_address(char* line)
{
    char* t;
    line = trim_start(line);
    
    /*
     * We parse out emails in the form of <blah@blah.com> 
     * as well as accept other addresses.
     */
    
    if(strncmp(line, "<>", 2) == 0)
        return("<>");

    if(line[0] == '<')
    {
        if((t = strchr(line, '>')) != NULL)
        {   
            *t = 0;
            line++;
            return line;
        }
    }
    
    return trim_end(line);
}

static char* parse_xforward(char* line, const char* part)
{
    char* t;
    char* e;

    t = strcasestr(line, part);
    if(!t)
        return NULL;

    /* equals sign and surrounding */
    t = trim_start(t + strlen(part));
    if(*t != '=')
        return NULL;
    t = trim_start(t + 1);
    if(!*t)
        return NULL;
    
    /* Find the end of the thingy */
    if(*t == '[')
    {
        t++;
        e = strchr(t, ']');
    }
    else
    {
        e = t + strcspn(t, " \t");
    }
    
    if(!e)
        return NULL;
    *e = 0;
    return t;
}

static const char* get_successful_rsp(const char* line, int* cont)
{
    /*
     * We check for both '250 xxx' type replies
     * and the continued response '250-xxxx' type
     */
     
    line = trim_start(line);
    
    if(line[0] == '2' && isdigit(line[1]) && isdigit(line[2]) &&
       (line[3] == ' ' || line[3] == '-'))
    {
        if(cont)
            *cont = (line[3] == '-');
        return line + 4;
    }
   
    return NULL;
}

void sp_add_log(spctx_t* ctx, char* prefix, char* line)
{
    char* t = ctx->logline;
    int l = strlen(t);
    int x;
    
    ASSERT(l <= SP_LOG_LINE_LEN);

    /* Add up necessary lengths */
    x = 2 + strlen(prefix) + strlen(line) + 1;

    if(l + x >= SP_LOG_LINE_LEN)
        l = SP_LOG_LINE_LEN - x;
        
    t += l;
    l = SP_LOG_LINE_LEN - l;    
    
    *t = 0;
    
    if(ctx->logline[0] != 0)
        strlcat(t, ", ", l);
        
    strlcat(t, prefix, l);
    
    /* Skip initial white space */
    line = trim_start(line);
        
    strlcat(t, line, l);
    
    /* Skip later white space */
    trim_end(t);    
}

int sp_read_data(spctx_t* ctx, const char** data)
{
    int r;
    
    ASSERT(ctx);
    ASSERT(data);
    
    *data = NULL;
    
    switch(r = spio_read_line(ctx, &(ctx->client), SPIO_QUIET))
    {
    case 0:
        sp_messagex(ctx, LOG_ERR, "unexpected end of data from client");
        return -1;
    case -1:
        /* Message already printed */
        return -1;
    };
    
    if(g_state.keepalives > 0)
    {
        /* 
         * During this time we're just reading from the client. If we haven't
         * had any interaction with the server recently then send something 
         * to let it know we're still around.
         */
        if((ctx->server.last_action + g_state.keepalives) < time(NULL))
            do_server_noop(ctx);
    }
    
    if(ctx->_crlf && strcmp(ctx->client.line, DATA_END_SIG) == 0)
        return 0;
        
    /* Check if this line ended with a CRLF */
    ctx->_crlf = (strcmp(CRLF, ctx->client.line + (r - KL(CRLF))) == 0);
    *data = ctx->client.line;
    return r;
}

int sp_write_data(spctx_t* ctx, const char* buf, int len)
{
    int r = 0; 

    ASSERT(ctx);
    
    /* When a null buffer close the cache file */
    if(!buf)
    {
        if(ctx->cachefile)
        {          
            if(fclose(ctx->cachefile) == EOF)
            {
                sp_message(ctx, LOG_ERR, "couldn't write to cache file: %s", ctx->cachename);
                r = -1;
            }

            ctx->cachefile = NULL;
        }
        
        return r;
    }
    
    /* Make sure we have a file open */
    if(!ctx->cachefile)
    {
        int tfd;

        /* Make sure afore mentioned file is gone */
        if(ctx->cachename[0])
            unlink(ctx->cachename);
        
        snprintf(ctx->cachename, MAXPATHLEN, "%s/%s.XXXXXX", 
                 g_state.directory, g_state.name);
        
        if((tfd = mkstemp(ctx->cachename)) == -1 ||
           (ctx->cachefile = fdopen(tfd, "w")) == NULL)
        {
            if(tfd != -1)
                close(tfd);
                
            sp_message(ctx, LOG_ERR, "couldn't open cache file");
            return -1;
        }

        fcntl(tfd, F_SETFD, fcntl(tfd, F_GETFD, 0) | FD_CLOEXEC);    
        sp_messagex(ctx, LOG_DEBUG, "created cache file: %s", ctx->cachename);
    }
    
    fwrite(buf, 1, len, ctx->cachefile);
    
    if(ferror(ctx->cachefile))
    {
        sp_message(ctx, LOG_ERR, "couldn't write to cache file: %s", ctx->cachename);
        return -1;
    }    

    return len;    
}

int sp_cache_data(spctx_t* ctx)
{
    int r, count = 0;
    const char* data;
    
    while((r = sp_read_data(ctx, &data)) != 0)
    {
        if(r < 0)
            return -1;  /* Message already printed */
            
        count += r;
            
        if((r = sp_write_data(ctx, data, r)) < 0)
            return -1;  /* Message already printed */
    }
    
    /* End the caching */
    if(sp_write_data(ctx, NULL, 0) < 0)
        return -1;
        
    sp_messagex(ctx, LOG_DEBUG, "wrote %d bytes to cache", count);
    return count;   
}

/* Important: |date| should be at least MAX_DATE_LENGTH long */
static void make_date(spctx_t* ctx, char* date)
{
    size_t date_len;
    struct tm t2;
    time_t t;

    /* Get a basic date like: 'Wed Jun 30 21:49:08 1993' */    
    if(time(&t) == (time_t)-1 || 
       !localtime_r(&t, &t2) || 
       !asctime_r(&t2, date))
    {
        sp_message(ctx, LOG_WARNING, "unable to get date for header");
        date[0] = 0;
        return;
    }
      
    trim_end(date);
    date_len = strlen(date);

    {
#ifdef HAVE_TM_GMTOFF
        time_t timezone = t2.tm_gmtoff;
        const char *tzname[2] = { t2.tm_zone, t2.tm_zone };

        snprintf(date + date_len, MAX_DATE_LENGTH - date_len, " %+03d%02d (%s)", 
                 (int)(timezone / 3600), (int)(timezone % 3600),
                 tzname[t2.tm_isdst ? 1 : 0]);
#else
        /* Apparently Solaris needs this nasty hack.... */
        #define DAY_MIN         (24 * HOUR_MIN)
        #define HOUR_MIN        60
        #define MIN_SEC         60

        struct tm gmt;
        struct tm *lt;
        int off;

        gmt = *gmtime(&t);
        lt = localtime(&t);
        off = (lt->tm_hour - gmt.tm_hour) * HOUR_MIN + lt->tm_min - gmt.tm_min;

        if (lt->tm_year < gmt.tm_year)
            off -= DAY_MIN;
        else if (lt->tm_year > gmt.tm_year)
            off += DAY_MIN;
        else if (lt->tm_yday < gmt.tm_yday)
            off -= DAY_MIN;
        else if (lt->tm_yday > gmt.tm_yday)
            off += DAY_MIN;
        if (lt->tm_sec <= gmt.tm_sec - MIN_SEC)
            off -= 1;
        else if (lt->tm_sec >= gmt.tm_sec + MIN_SEC)
            off += 1;

        snprintf(date + date_len, MAX_DATE_LENGTH - date_len,
                 " %+03d%02d (%s)", (int)(off / HOUR_MIN), (int)(abs(off) % HOUR_MIN),
                 tzname[lt->tm_isdst ? 1 : 0]);
#endif    
    }
    
    /* Break it off just in case */
    date[MAX_DATE_LENGTH - 1] = 0;
}

/* Important: |header| should be a buffer of MAX_HEADER_LENGTH */
static int make_header(spctx_t* ctx, const char* format_str, char* header)
{
    char date[MAX_DATE_LENGTH];
    int remaining, l; 
    const char* f;
    char* p;

    date[0] = 0;
    remaining = MAX_HEADER_LENGTH - 1;
    p = header;
    
    /* Parse the format string and replace special characters with our data */
    for(f = format_str; *f && remaining > 0; f++)
    {
        /* A backslash escapes certain characters */
        if(f[0] == '\\' && f[1] != 0)
        {
            switch(*(++f))
            {
            case 'r':
                *p = '\r';
                break;
            case 'n':
                *p = '\n';
                break;
            case 't':
                *p = '\t';
                break;
            default:
                *p = *f;
                break;
            }
            
            ++p;
            --remaining;
        }
        
        /* 
         * Special symbols:
         *    %i: client's IP
         *    %l: server's IP
         *    %d: date
         */
        else if(f[0] == '%' && f[1] != 0)
        {
            switch(*(++f)) 
            {
            case 'i':
                l = strlen(ctx->client.peername);
                strncpy(p, ctx->client.peername, remaining);
                remaining -= l;
                p += l;
                break;
            case 'l':
                l = strlen(ctx->client.localname);
                strncpy(p, ctx->client.localname, remaining);
                remaining -= l;
                p += l;
                break;
            case 'd':
                if(date[0] == 0)
                    make_date(ctx, date);
                l = strlen(date);
                strncpy(p, date, remaining);
                remaining -= l;
                p += l;
                break;
            case '%':
				*p = '%';            
				++p;
				break;
            default:
                sp_messagex(ctx, LOG_WARNING, "invalid header symbol: %%%c", *f);
                break;
            };			
        }
        
        else
        {
            *(p++) = *f;
            remaining--;
        }
    }
    
    if((p + 1) < (header + MAX_HEADER_LENGTH))
        p[1] = 0;
    header[MAX_HEADER_LENGTH - 1] = 0;
    l = p - header;
    return l >= MAX_HEADER_LENGTH ? MAX_HEADER_LENGTH - 1 : l; 
}

int sp_done_data(spctx_t* ctx, const char *headertmpl)
{
    FILE* file = 0;
    int ret = 0;
    char *line;    
    char header[MAX_HEADER_LENGTH] = "";
    size_t header_len, line_len;
    int header_prepend = 0;
    ssize_t rc;

    ASSERT(ctx->cachename[0]);  /* Must still be around */
    ASSERT(!ctx->cachefile);    /* File must be closed */
    
    memset(header, 0, sizeof(header));

    /* Alloc line buffer */
    line_len = SP_LINE_LENGTH;
    if((line = (char *)malloc(line_len)) == NULL)
        RETURN(-1);

    /* Open the file */
    file = fopen(ctx->cachename, "r");
    if(file == NULL)
    {
        sp_message(ctx, LOG_ERR, "couldn't open cache file: %s", ctx->cachename);
        RETURN(-1);
    }
        
    /* Ask the server for permission to send data */
    if(spio_write_data(ctx, &(ctx->server), SMTP_DATA) == -1)
        RETURN(-1);
    
    if(read_server_response(ctx) == -1)
        RETURN(-1);

    /* If server returns an error then tell the client */
    if(!is_first_word(ctx->server.line, DATA_RSP, KL(DATA_RSP)))
    {
        if(spio_write_data(ctx, &(ctx->client), ctx->server.line) == -1)
            RETURN(-1);
            
        sp_messagex(ctx, LOG_DEBUG, "server refused data transfer");
            
        RETURN(0);
    }

    sp_messagex(ctx, LOG_DEBUG, "sending from cache file: %s", ctx->cachename);
    
    if(headertmpl)
    {
        header_len = make_header(ctx, headertmpl, header);
        if(is_first_word(RCVD_HEADER, header, KL(RCVD_HEADER)))
            header_prepend = 1;
    }


    /* If we have to prepend the header, do it */
    if(header[0] != '\0' && header_prepend)
    {
        if(spio_write_data_raw(ctx, &(ctx->server), (unsigned char*)header, header_len) == -1 ||
           spio_write_data_raw(ctx, &(ctx->server), (unsigned char*)CRLF, KL(CRLF)) == -1)
            RETURN(-1);
        header[0] = '\0';
    }

    /* Transfer actual file data */    
    while((rc = getline(&line, &line_len, file)) != -1)
    {
        /* 
         * If the line is <CRLF>.<CRLF> we need to change it so that 
         * it doesn't end the email. We do this by adding a space. 
         * This won't occur much in clamsmtpd, but proxsmtpd might 
         * have filters that accidentally put this in.
         */
        if(strcmp(line, "." CRLF) == 0)
            strncpy(line, ". " CRLF, SP_LINE_LENGTH);
      
        if(header[0] != '\0')
        {
            /* 
             * The first blank line we see means the headers are done.
             * At this point we add in our virus checked header.
             */
            if(is_blank_line(line))
            {
                if(spio_write_data_raw(ctx, &(ctx->server), (unsigned char*)header, header_len) == -1 ||
                   spio_write_data_raw(ctx, &(ctx->server), (unsigned char*)CRLF, KL(CRLF)) == -1)
                    RETURN(-1);
                header[0] = '\0';
            }
        }
        
        if(spio_write_data_raw(ctx, &(ctx->server), (unsigned char*)line, rc) == -1)
            RETURN(-1);
    }
    
    if(ferror(file))
        sp_message(ctx, LOG_ERR, "error reading cache file: %s", ctx->cachename);
      
    if(ferror(file) || spio_write_data(ctx, &(ctx->server), DATA_END_SIG) == -1)
    {
        /* Tell the client it went wrong */
        spio_write_data(ctx, &(ctx->client), SMTP_FAILED);
        RETURN(-1);
    }

    sp_messagex(ctx, LOG_DEBUG, "sent email data");    
    
    /* Okay read the response from the server and echo it to the client */
    if(read_server_response(ctx) == -1)
        RETURN(-1);
        
    if(spio_write_data(ctx, &(ctx->client), ctx->server.line) == -1)
        RETURN(-1);
        
cleanup:
    
	if(line)
		free(line); 
    if(file)
        fclose(file); /* read-only so no error check */
    
    return ret;
}

int sp_fail_data(spctx_t* ctx, const char* smtp_status)
{
    char buf[256 + KL(SMTP_REJPREFIX) + KL(CRLF) + 1];
    char* t = NULL;
    int len, x;
    int pref = 0;
    int crlf = 0; 
    
    if(smtp_status == NULL)
        smtp_status = SMTP_FAILED;
    
    x = strtol(smtp_status, &t, 10);
    len = strlen(smtp_status); 

    /* We need 3 digits and CRLF at the end for a premade SMTP message */
    if(x == 0 || t != smtp_status + 3)
        pref = 1;
    
    /* We need a CRLF at the end */
    if(strcmp(smtp_status + (len - KL(CRLF)), CRLF) != 0)
        crlf = 1;
        
    if(pref || crlf)
    {
        /* Note that we truncate long lines */
        snprintf(buf, sizeof(buf), "%s%.256s%s", pref ? SMTP_REJPREFIX : "", 
                    smtp_status, crlf ? CRLF : "");
        buf[sizeof(buf) - 1] = 0;
        smtp_status = buf;
    }
    
    if(spio_write_data(ctx, &(ctx->client), smtp_status) == -1)
        return -1;

     /* Tell the server to forget about the current message */
     if(spio_write_data(ctx, &(ctx->server), SMTP_RSET) == -1 || 
        read_server_response(ctx) == -1)
         return -1;
        
    return 0;
}

static int read_server_response(spctx_t* ctx)
{
    int r;
    
    /* Read response line from the server */
    if((r = spio_read_line(ctx, &(ctx->server), SPIO_DISCARD)) == -1)
        return -1;

    if(r == 0)
    {
        sp_messagex(ctx, LOG_ERR, "server disconnected unexpectedly");
        
        /* Tell the client it went wrong */
        spio_write_data(ctx, &(ctx->client), SMTP_FAILED);
        return 0;
    }

    if(LINE_TOO_LONG(r))
        sp_messagex(ctx, LOG_WARNING, "SMTP response line too long. discarded extra");
        
    return 0;
}

static void do_server_noop(spctx_t* ctx)
{
    if(spio_valid(&(ctx->server)))
    {
        if(spio_write_data(ctx, &(ctx->server), SMTP_NOOP) != -1)
            spio_read_line(ctx, &(ctx->server), SPIO_DISCARD);
    }
}
 
void sp_setup_forked(spctx_t* ctx, int file)
{
    /* Signals we've messed with */
    signal(SIGPIPE, SIG_DFL); 
    signal(SIGHUP,  SIG_DFL);
    signal(SIGINT,  SIG_DFL);
    signal(SIGTERM, SIG_DFL);

    siginterrupt(SIGINT, 0);
    siginterrupt(SIGTERM, 0);
    
    if(ctx->sender)
        setenv("SENDER", ctx->sender, 1);
        
    if(ctx->recipients)
        setenv("RECIPIENTS", ctx->recipients, 1);
    
    if(file && ctx->cachename[0])
        setenv("EMAIL", ctx->cachename, 1);

    if(spio_valid(&(ctx->client)))
        setenv("CLIENT", ctx->client.peername, 1);
    
    if(ctx->xforwardaddr)
        setenv("REMOTE", ctx->xforwardaddr, 1);
        
    if(ctx->xforwardhelo)
        setenv("REMOTE_HELO", ctx->xforwardhelo, 1);
    
    if(spio_valid(&(ctx->server)))
        setenv("SERVER", ctx->server.peername, 1);
    
    setenv("TMPDIR", g_state.directory, 1);
}


/* ----------------------------------------------------------------------------------
 *  LOGGING
 */
 
const char kMsgDelimiter[] = ": ";
#define MAX_MSGLEN  1024

static void vmessage(spctx_t* ctx, int level, int err, 
                     const char* msg, va_list ap)
{
    char buf[MAX_MSGLEN];
    int e = errno;

    if(g_state.daemonized)
    {
        if(level >= LOG_DEBUG)
            return;
    }
    else
    {                   
        if(g_state.debug_level < level)
            return;
    }
       
    ASSERT(msg);

    if(ctx)
        snprintf(buf, MAX_MSGLEN, "%06X: %s%s", ctx->id, msg, err ? ": " : "");
    else
        snprintf(buf, MAX_MSGLEN, "%s%s", msg, err ? ": " : "");
            
    if(err)
    {
        /* strerror_r doesn't want to work for us for some reason
        len = strlen(buf);
        strerror_r(e, buf + len, MAX_MSGLEN - len); */
            
        sp_lock();
            strncat(buf, strerror(e), MAX_MSGLEN);
        sp_unlock();
    }

    /* As a precaution */
    buf[MAX_MSGLEN - 1] = 0;
  
    /* Either to syslog or stderr */
    if(g_state.daemonized)
        vsyslog(level, buf, ap);
    else
        vwarnx(buf, ap);  
}

void sp_messagex(spctx_t* ctx, int level, const char* msg, ...)
{
    va_list ap;
    
    va_start(ap, msg);
    vmessage(ctx, level, 0, msg, ap);
    va_end(ap);
}

void sp_message(spctx_t* ctx, int level, const char* msg, ...)
{
    va_list ap;
    
    va_start(ap, msg);
    vmessage(ctx, level, 1, msg, ap);
    va_end(ap);
}


/* -----------------------------------------------------------------------
 * LOCKING
 */
 
void sp_lock()
{
    int r;
  
#ifdef _DEBUG
    int wait = 0;
#endif
  
#ifdef _DEBUG
    r = pthread_mutex_trylock(&g_mutex);
    if(r == EBUSY)
    {
        wait = 1;
        sp_message(NULL, LOG_DEBUG, "thread will block: %d", pthread_self());
        r = pthread_mutex_lock(&g_mutex);
    }

#else
    r = pthread_mutex_lock(&g_mutex);
  
#endif  
  
    if(r != 0)
    {
        errno = r;
        sp_message(NULL, LOG_CRIT, "threading problem. couldn't lock mutex");
    }
  
#ifdef _DEBUG
    else if(wait)
    {
        sp_message(NULL, LOG_DEBUG, "thread unblocked: %d", pthread_self());
    }
#endif    
}
    
void sp_unlock()
{
    int r = pthread_mutex_unlock(&g_mutex);
    if(r != 0)
    {
        errno = r;
        sp_message(NULL, LOG_CRIT, "threading problem. couldn't unlock mutex");
    }
}

/* -----------------------------------------------------------------------------
 * CONFIG FILE
 */
    
int sp_parse_option(const char* name, const char* value)
{
    char* t;
    int ret = 0;
    
    if(strcasecmp(CFG_MAXTHREADS, name) == 0)
    {
        g_state.max_threads = strtol(value, &t, 10);
        if(*t || g_state.max_threads <= 1 || g_state.max_threads >= 1024) 
            errx(2, "invalid setting: " CFG_MAXTHREADS " (must be between 1 and 1024)");
        ret = 1;
    }
        
    else if(strcasecmp(CFG_TIMEOUT, name) == 0)
    {
        g_state.timeout.tv_sec = strtol(value, &t, 10);
        if(*t || g_state.timeout.tv_sec <= 0) 
            errx(2, "invalid setting: " CFG_TIMEOUT);
        ret = 1;
    }
    
    else if(strcasecmp(CFG_KEEPALIVES, name) == 0)
    {
        g_state.keepalives = strtol(value, &t, 10);
        if(*t || g_state.keepalives < 0)
            errx(2, "invalid setting: " CFG_KEEPALIVES);
        ret = 1;
    }
    
    else if(strcasecmp(CFG_XCLIENT, name) == 0)
    {
        if((g_state.xclient = strtob(value)) == -1)
            errx(2, "invalid value for " CFG_XCLIENT);
        ret = 1;
    }
            
    else if(strcasecmp(CFG_OUTADDR, name) == 0)
    {
        if(sock_any_pton(value, &(g_state.outaddr), SANY_OPT_DEFPORT(25)) == -1)
            errx(2, "invalid " CFG_OUTADDR " socket name or ip: %s", value);    
        g_state.outname = value;
        ret = 1;
    }
        
    else if(strcasecmp(CFG_LISTENADDR, name) == 0)
    {
        if(sock_any_pton(value, &(g_state.listenaddr), SANY_OPT_DEFANY | SANY_OPT_DEFPORT(DEFAULT_PORT)) == -1)
            errx(2, "invalid " CFG_LISTENADDR " socket name or ip: %s", value);        
        g_state.listenname = value;
        ret = 1;
    }
            
    else if(strcasecmp(CFG_TRANSPARENT, name) == 0)
    {
        if((g_state.transparent = strtob(value)) == -1)
            errx(2, "invalid value for " CFG_TRANSPARENT);            
        ret = 1;
    }
 
    else if(strcasecmp(CFG_DIRECTORY, name) == 0)
    {
        if(strlen(value) == 0)
            errx(2, "invalid setting: " CFG_DIRECTORY);   
        g_state.directory = value;
        ret = 1;
    }
    
    else if(strcasecmp(CFG_USER, name) == 0)
    {
        if(strlen(value) == 0)
            errx(2, "invalid setting: " CFG_USER);
        g_state.user = value;
        ret = 1;
    }
    
    else if(strcasecmp(CFG_PIDFILE, name) == 0)
    {
        if(g_state.pidfile != NULL)
            sp_messagex(NULL, LOG_WARNING, "ignoring pid file specified on the command line. ");

        if(strlen(value) == 0)
            g_state.pidfile = NULL;
        else
            g_state.pidfile = value;
        ret = 1;
    }

    /* Always pass through to program */    
    if(cb_parse_option(name, value) == 1)
        ret = 1;
        
    return ret;
}
    
static int parse_config_file(const char* configfile)
{
    FILE* f = NULL;
    long len;
    char* p;
    char* t;
    char* n;
    
    ASSERT(configfile); 
    ASSERT(!g_state._p);
    
    f = fopen(configfile, "r");
    if(f == NULL)
    {
        /* Soft errors when default config file and not found */
        if((errno == ENOENT || errno == ENOTDIR))
            return -1;
        else
            err(1, "couldn't open config file: %s", configfile);
    }
    
    /* Figure out size */
    if(fseek(f, 0, SEEK_END) == -1 || (len = ftell(f)) == -1 || fseek(f, 0, SEEK_SET) == -1)
        err(1, "couldn't seek config file: %s", configfile);
        
    if((g_state._p = (char*)malloc(len + 2)) == NULL)
        errx(1, "out of memory");

    /* And read in one block */
    if(fread(g_state._p, 1, len, f) != len)
        err(1, "couldn't read config file: %s", configfile);
        
    fclose(f);
    sp_messagex(NULL, LOG_DEBUG, "read config file: %s", configfile); 
    
    /* Double null terminate the data */
    p = g_state._p;
    p[len] =  '\n';
    p[len + 1] = 0;
   
    n = g_state._p;
    
    /* Go through lines and process them */
    while((t = strchr(n, '\n')) != NULL)
    {
        *t = 0;
        p = n; /* Do this before cleaning below */
        n = t + 1;
        
        p = trim_start(p);
        
        /* Comments and empty lines */
        if(*p == 0 || *p == '#')
            continue;
            
        /* Look for the break between name: value */
        t = strchr(p, ':');
        if(t == NULL)
            errx(2, "invalid config line: %s", p);
            
        /* Null terminate and split value part */
        *t = 0;
        t++;
        
        t = trim_space(t);
        p = trim_space(p);
        
        /* Pass it through our options parsers */
        if(sp_parse_option(p, t) == 0)
        
            /* If not recognized then it's invalid */
            errx(2, "invalid config line: %s", p);            
            
        sp_messagex(NULL, LOG_DEBUG, "parsed option: %s: %s", p, t);
    }
    
    return 0;
}