summaryrefslogtreecommitdiffabout
path: root/pmult/pmult.c
blob: 92cfc8444593e564503d726af629490f1f6e0880 (plain)
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
/* This file is part of Mailfromd.
   Copyright (C) 2008 Sergey Poznyakoff

   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 3, 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, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <syslog.h>
#include <mailutils/mailutils.h>
#include <mailutils/daemon.h>
#include <mailutils/syslog.h>
#include <mailutils/libargp.h>
#include <sysexits.h>
#include <pthread.h>
#include <gacopyz.h>
#include "xalloc.h"
#include "libmf.h"

/* FIXME */
#include <inttypes.h>
const char *mu_umaxtostr (unsigned slot, uintmax_t val);

#include "sm/error.h"
#include "sm/smreplycodes.h"
#include "sm/pmilter.h"
#include "sm/pmfdef.h"
#include "sm/pmfapi.h"

#include "pdbg.h"

const char *program_version = "pmult (" PACKAGE_STRING ")";
const char *package_bugreport = "<" PACKAGE_BUGREPORT ">";

char *portspec; /* Communication socket */
int log_to_stderr;  /* Use stderr for logging */
char *log_tag; /* override mu_log_tag */
mu_log_level_t debug_level; /* Debug verbosity level */
mu_debug_t pmult_debug; /* Debugging object */
static pthread_mutex_t pmult_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
char *pidfile;          /* pidfile name */
int no_sig_handler;     /* Disable signal handler */

struct pmult_client
{
  int type; /* Type, unused so far */
  char *name;
  struct timeval timeout[GACOPYZ_TO_COUNT];
  char *url;
  int logmask;
};

/* List of configured clients: */
mu_list_t /* of struct pmult_client */ client_list;

enum pmult_msg_state
  {
    pmult_msg_state_initial,
    pmult_msg_state_headers,
    pmult_msg_state_cr1,
    pmult_msg_state_crlf1,
    pmult_msg_state_cr2,
    pmult_msg_state_body
  };

struct pmult_priv_data
{
  mu_debug_t debug;
  mu_list_t /* of gacopyz_srv_t */ srvlist;
  pthread_mutex_t mutex;
  char *seid;
  char *seid_c;
  char *taid;
  unsigned nrcpt;
  unsigned nbadrcpts;
  mu_stream_t hdrstream;
  enum pmult_msg_state state;
  mu_header_t hdr;
};

#define PRIV_SEID(p) ((p)->seid ? (p)->seid : "")
#define PRIV_SEID_C(p) ((p)->seid_c ? (p)->seid_c : pmult_seid_c (p))
#define PRIV_LOCK(p) pthread_mutex_lock (&(p)->mutex)
#define PRIV_UNLOCK(p) pthread_mutex_unlock (&(p)->mutex)

static char *
pmult_seid_c (struct pmult_priv_data *p)
{
  if (p->seid)
    {
      p->seid_c = malloc (strlen (p->seid) + 2);
      if (p->seid_c)
	{
	  strcpy (p->seid_c, p->seid);
	  strcat (p->seid_c, ":");
	  return p->seid_c;
	}
    }
  return "";
}


static pthread_mutex_t pmult_mutex = PTHREAD_MUTEX_INITIALIZER;
#define protect() pthread_mutex_lock (&pmult_mutex)
#define unprotect() pthread_mutex_unlock (&pmult_mutex)


/* Logging */
void
log_setup (int want_stderr)
{
  mu_debug_t debug;

  mu_diag_get_debug (&debug);

  if (log_tag)
    mu_log_tag = log_tag;
  
  if (!want_stderr)
    {
      openlog (MU_LOG_TAG (), LOG_PID, mu_log_facility);
      gacopyz_set_logger (gacopyz_syslog_log_printer);
      mu_debug_set_print (debug, mu_diag_syslog_printer, NULL);
      mu_debug_default_printer = mu_debug_syslog_printer;
    }
  else
    {
      gacopyz_set_logger (gacopyz_stderr_log_printer);
      mu_debug_default_printer = mu_debug_stderr_printer;
    }
}
  
static int
stderr_closed_p()
{
  int fd = dup(0);
  if (fd < 0)
    return 1;
  close(fd);
  return fd <= 2;
}


static int
_cb_client_type (mu_debug_t debug, void *data, char *arg)
{
  if (strcmp (arg, "milter") == 0)
    /* dobrze */;
  else if (strcmp (arg, "pmilter") == 0)
    mu_cfg_format_error (debug, MU_DEBUG_ERROR,
			 _("client type %s is not supported yet"),
			 arg);
  else
    mu_cfg_format_error (debug, MU_DEBUG_ERROR,
			 _("unknown client type %s"),
			 arg);
  return 0;
}
  
static int
cb_timeout (struct timeval *pt, mu_debug_t debug, char *arg)
{
  const char *endp;
  time_t t;
  if (parse_time_interval (arg, &t, &endp))
    {
      mu_cfg_format_error (debug, MU_DEBUG_ERROR,
			   _("unrecognized time format (near `%s')"),
			   endp);
      return 1;
    }
  pt->tv_usec = 0;
  pt->tv_sec = t;
  return 0;
}

static int
_cb_write_timeout (mu_debug_t debug, void *data, char *arg)
{
  struct pmult_client *clt = data;
  return cb_timeout (&clt->timeout[GACOPYZ_TO_WRITE], debug, arg);
}

static int
_cb_read_timeout (mu_debug_t debug, void *data, char *arg)
{
  struct pmult_client *clt = data;
  return cb_timeout (&clt->timeout[GACOPYZ_TO_READ], debug, arg);
}

static int
_cb_eom_timeout (mu_debug_t debug, void *data, char *arg)
{
  struct pmult_client *clt = data;
  return cb_timeout (&clt->timeout[GACOPYZ_TO_EOM], debug, arg);
}

static int
_cb_connect_timeout (mu_debug_t debug, void *data, char *arg)
{
  struct pmult_client *clt = data;
  return cb_timeout (&clt->timeout[GACOPYZ_TO_CONNECT], debug, arg);
}

static int
_cb_log_level (mu_debug_t debug, void *data, char *arg)
{
  struct pmult_client *clt = data;
  int argc, i;
  char **argv;
  int rc;
  
  rc = mu_argcv_get_np (arg, strlen (arg), ",", NULL, 0, &argc, &argv, NULL);
  if (rc)
    {
      mu_cfg_format_error (debug, MU_DEBUG_ERROR,
			   "mu_argcv_get: %s", mu_strerror (rc));
      return 1;
    }
  
  for (i = 0; i < argc; i++)
    {
      char *p = argv[i];
      int lev;
      int revert = 0;
      int upto = 0;
      
      if (*p == '!')
	{
	  p++;
	  revert = 1;
	}
      if (*p == '<')
	{
	  p++;
	  upto = 1;
	}
		  
      lev = gacopyz_string_to_log_level (p);
      if (lev == -1)
	{
	  mu_cfg_format_error (debug, MU_DEBUG_ERROR,
			       _("Invalid log level: %s"), arg);
	  return 1;
	}
      
      if (revert)
	{
	  if (upto)
	    clt->logmask &= ~SMI_LOG_UPTO (lev);
	  else
	    clt->logmask &= ~SMI_LOG_FROM (lev);
	}
      else
	{
	  if (upto)
	    clt->logmask |= SMI_LOG_UPTO (lev);
	  else
	    clt->logmask |= SMI_LOG_FROM (lev);
	}
    }
  
  mu_argcv_free (argc, argv);
  return 0;
}
  
struct mu_cfg_param client_cfg_param[] = {
  { "type", mu_cfg_callback, NULL, 0, _cb_client_type,
    N_("Set remote milter type. Only `milter' is understood so far."),
    N_("{milter [version: number]|pmilter}") },
  { "url", mu_cfg_string, NULL, mu_offsetof(struct pmult_client, url), NULL,
    N_("Set remote client URL.") },
  { "write-timeout", mu_cfg_callback, NULL, 0, _cb_write_timeout,
    N_("Set write timeout."),
    N_("time") },
  { "read-timeout", mu_cfg_callback, NULL, 0, _cb_read_timeout,
    N_("Set read timeout."),
    N_("time") },
  { "eom-timeout", mu_cfg_callback, NULL, 0, _cb_eom_timeout,
    N_("Set timeout for EOM."),
    N_("time") },
  { "connect-timeout", mu_cfg_callback, NULL, 0, _cb_connect_timeout,
    N_("Set connect timeout."),
    N_("time") },
  { "log-level", mu_cfg_callback, NULL, 0, _cb_log_level,
    N_("Set log verbosity level. Arg is a list of items separated by commas "
       "or whitespace.  Each item is a log level optionally prefixed with `!' "
       "to indicate `any level except this', or '<', meaning `all levels up "
       "to and including this'.  Log levels in order of increasing priority "
       "are: debug, info, warn, err, fatal."),
    N_("arg: list") },
  { NULL }
};  

static int
_cb_portspec (mu_debug_t debug, void *data, char *arg)
{
  char **pptr = data, *ptr;
  char *proto = NULL;
  char *port = NULL;
  char *path = NULL;
  size_t len;
  
  if (gacopyz_parse_connection (arg, &proto, &port, &path))
    {
      mu_cfg_format_error (debug, MU_DEBUG_ERROR, _("Invalid URL: %s"), arg);
      return 1;
    }

  if (!proto)
    {
      if (port)
	{
	  mu_cfg_format_error (debug, MU_DEBUG_ERROR,
			       _("Invalid URL: %s"), arg);
	  return 1;
	}
      ptr = xstrdup (path);
    }
  else
    {
      len = strlen (proto) + 1
	    + (port ? strlen (port) + 1 : 0) + strlen (path) + 1;
      ptr = xmalloc (len);
      strcpy (ptr, proto);
      strcat (ptr, ":");
      if (port)
	{
	  strcat (ptr, port);
	  strcat (ptr, "@");
	}
      strcat (ptr, path);
    }
  free (path);
  free (port);
  free (proto);
  *pptr = ptr;
  return 0;
}

static int
_cb_debug (mu_debug_t debug, void *data, char *arg)
{
  int rc;

  rc = mu_debug_level_from_string (arg, &debug_level, debug);
  if (rc)
    return 0;
  if (!pmult_debug)
    mu_debug_create (&pmult_debug, NULL);
  mu_debug_set_level (pmult_debug, debug_level);
  return 0;
}

struct mu_cfg_param pmult_cfg_param[] = {
  { "listen", mu_cfg_callback, &portspec, 0, _cb_portspec,
    N_("Listen for milter requests on the given URL."),
    N_("url") },
  { "client", mu_cfg_section },
  { "debug", mu_cfg_callback, NULL, 0, _cb_debug,
    N_("Set debug verbosity level.") },
  { "pidfile", mu_cfg_string, &pidfile, 0, NULL,
    N_("Write PID to this file.") },
  { NULL }
};

static void
pmult_client_free (struct pmult_client *clt)
{
  free (clt->name);
  free (clt);
}

static int
client_block_begin (mu_debug_t debug, char *name, void **section_data)
{
  extern struct timeval default_gacopyz_timeout[GACOPYZ_TO_COUNT];
  struct pmult_client *clt = xcalloc (1, sizeof *clt);
  clt->name = name ? xstrdup (name) : "";
  memcpy (clt->timeout, default_gacopyz_timeout, sizeof clt->timeout);
  *section_data = clt;
  return 0;
}

static int
client_block_end (mu_debug_t debug, struct pmult_client *clt)
{
  gacopyz_srv_t gsrv;
  int rc = gacopyz_srv_create (&gsrv, clt->name, clt->url, clt->logmask);
  if (rc)
    {
      pmult_client_free (clt);
      return 1; /* FIXME: error message */
    }
  
  gacopyz_srv_destroy (&gsrv);
  if (!client_list)
    MU_ASSERT (mu_list_create (&client_list));
  mu_list_append (client_list, clt);
  return 0;
}

static int
client_section_parser (enum mu_cfg_section_stage stage,
		       const mu_cfg_node_t *node,
		       const char *section_label, void **section_data,
		       void *call_data,
		       mu_cfg_tree_t *tree)
{
  switch (stage)
    {
    case mu_cfg_section_start:
      return client_block_begin (tree->debug, node->tag_label, section_data);

    case mu_cfg_section_end:
      return client_block_end (tree->debug,
			       (struct pmult_client *)*section_data);
    }
  return 0;
}
  
static void
pmult_cfg_init ()
{
  struct mu_cfg_section *section;

  if (mu_create_canned_section ("client", &section))
    exit (EX_SOFTWARE);
  section->parser = client_section_parser;
  section->label = N_("ident");
  mu_cfg_section_add_params (section, client_cfg_param);
}


/* Command line parsing */

static char doc[] = N_("pmult -- pmilter multiplexer");
static char args_doc[] = "";

enum {
  OPTION_URL = 256,
  OPTION_SYSLOG,
  OPTION_LOG_TAG,
  OPTION_NO_SIGNAL_HANDLER,
};

static struct argp_option options[] = {
  { "url", OPTION_URL, N_("URL"), 0,
    N_("Listen on the given URL"), 0 },
  { "syslog", OPTION_SYSLOG, NULL, 0,
    N_("Log to syslog (default)"), },
  { "stderr", 's', NULL, 0,
    N_("Log to stderr"), },
  { "log-tag", OPTION_LOG_TAG, N_("STRING"), 0,
    N_("Set the identifier used in syslog messages to STRING"),  },
  { "debug", 'x', N_("LEVEL"), 0,
    N_("Set debug verbosity level.") },
  { "no-signal-handler", OPTION_NO_SIGNAL_HANDLER, NULL, 0,
    N_("Disable signal handling in the main thread (use for debugging).") },
  { NULL }
};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  static struct mu_argp_node_list lst;
  
  switch (key)
    {
    case OPTION_URL:
      mu_argp_node_list_new (&lst, "listen", arg);
      break;

    case OPTION_SYSLOG:
      log_to_stderr = 0;
      break;

    case OPTION_NO_SIGNAL_HANDLER:
      no_sig_handler = 1;
      break;
      
    case 's':
      log_to_stderr = 1;
      break;

    case 'x':
      mu_argp_node_list_new (&lst, "debug", arg);
      break;
      
    case OPTION_LOG_TAG:
      log_tag = arg;
      break;
      
    case ARGP_KEY_INIT:
      mu_argp_node_list_init (&lst);
      break;

    case ARGP_KEY_FINI:
      mu_argp_node_list_finish (&lst, NULL, NULL);
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static const char *capa[] = {
  "common",
  "debug",
  "logging",
  NULL
};

static struct argp argp = {
  options,
  parse_opt,
  args_doc,
  doc,
  NULL,
  NULL,
  NULL
};


#define SM_ONERROR_ACTION(expr,onerr) do                        \
 {                                                              \
   sm_ret_T ret = expr;                                         \
   if (sm_is_err (ret))                                         \
     {                                                          \
       char *cp = smerr2txt (ret);                              \
       if (cp)                                                  \
         mu_error ("%s:%d: " #expr " failed: %s",               \
		   __FILE__, __LINE__, cp);                     \
       else                                                     \
         mu_error ("%s:%d: " #expr " failed: %#x",              \
		   __FILE__, __LINE__, ret);                    \
       onerr;                                                   \
     }                                                          \
  }                                                             \
while (0) 

#define SM_VERBOSE(expr) SM_ONERROR_ACTION(expr,)
#define SM_CHECK(expr) SM_ONERROR_ACTION(expr,return ret)
#define SM_ASSERT(expr) SM_ONERROR_ACTION(expr, exit (EX_CONFIG))


#define MSG_TEMPFAIL "451 4.3.2 Please try again later\r\n"
#define MSG_REJECT "550 5.7.1 Command rejected\r\n"
#define MSG_SHUTDOWN "421 4.7.0 pmult closing connection\r\n"

const char *
hdrcommname (int rc)
{
  switch (rc)
    {
    case SMFIR_CHGHEADER:
      return "chgheader";
      
    case SMFIR_ADDHEADER:
      return "addheader";
      
    case SMFIR_INSHEADER:
      return "insheader";
    }
  return "unknown";
}

static int
get_mod_index (struct pmult_priv_data *p, const char *header_name,
	       size_t pos, size_t *pidx)
{
  size_t i, count;
  const char *name;
	  
  if (mu_header_get_field_count (p->hdr, &count))
    return 1;
  for (i = 1; i <= count; i++)
    {
      if (mu_header_sget_field_name (p->hdr, i, &name))
	continue;
      if (strcmp (name, header_name) == 0 && --pos == 0)
	{
	  *pidx = i - 1;
	  return 0;
	}
    }
  return 1;
}

static int
cb_reply (gacopyz_srv_t gsrv, int cmd, int rcmd, void *data)
{
  char *buf;
  size_t size;
  char *cp;
  char *header;
  uint type;
  uint32_t idx = 0;
  pmse_ctx_P pmse_ctx = data;
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  
  switch (rcmd)
    {
    case SMFIR_CHGHEADER:
    case SMFIR_ADDHEADER:
    case SMFIR_INSHEADER:
      gacopyz_srv_reply (gsrv, &buf, &size);
      if (rcmd != SMFIR_ADDHEADER)
	{
	  idx = ntohl (*(uint32_t*)buf);
	  buf += sizeof (uint32_t);
	}
      cp = memchr (buf, 0, size);
      if (!cp)
	{
	  mu_diag_output (MU_DIAG_NOTICE, "Malformed CHGHEADER command");
	  break;
	}
      cp++;
      PMU_DEBUG4 (p->debug, MU_DEBUG_TRACE2, 		
		  "%s=%u '%s','%s'\n",
		  hdrcommname (rcmd), idx, buf, cp);

      header = malloc (size + 5);
      if (!header)
	{
	  mu_error ("%s", mu_strerror (ENOMEM));
	  return SMTP_R_ACCEPT;
	}
      strcpy (header, buf);
      strcat (header, ": ");
      strcat (header, cp);
      strcat (header, "\r\n");
      
      switch (rcmd)
	{
	case SMFIR_CHGHEADER:
	  {
	    /* Emulate milter semantics */
	    size_t index;
	    
	    type = (*cp == 0) ? SM_HDRMOD_T_REMOVE : SM_HDRMOD_T_REPLACE;
	    if (get_mod_index (p, buf, idx, &index))
	      {
		PMU_DEBUG1 (p->debug, MU_DEBUG_TRACE1,
			    "no such header: %s\n", buf);
		return 0;
	      }
	    idx = index;
	  }
	  break;
	  
	case SMFIR_ADDHEADER:
 	  type = SM_HDRMOD_T_APPEND;
	  break;
	  
	case SMFIR_INSHEADER:
	  type = SM_HDRMOD_T_INSERT;
	}
      
      SM_VERBOSE (sm_pmfi_hdr_mod (pmse_ctx, type, idx, header));
      free (header);
    }
  return 0;
}

#define CRLF "\r\n"

static char *
trailcrlf (char **pbuf)
{
  char *buf = *pbuf;
  char *retp = NULL;
  int len;
  
  if (!buf)
    *pbuf = CRLF;
  else if ((len = strlen (buf)) < 2 || memcmp (buf + len - 2, CRLF, 2))
    {
      retp = malloc (len + 3);
      if (!retp)
	{
	  mu_error ("%s", mu_strerror (ENOMEM));
	  *pbuf = MSG_TEMPFAIL;
	}
      strcat (strcpy (retp, buf), CRLF);
      *pbuf = retp;
    }
  return retp;
}

static int
pmult_std_reply (struct pmult_priv_data *p, pmse_ctx_P pmse_ctx,
		 gacopyz_srv_t gsrv, int rc,
		 const char *ident, const char *arg)
{
  char *buf, *tmp;
  size_t size;
  int status;
    
  if (!arg)
    arg = "";
  
  switch (rc)
    {
    case SMFIR_CONTINUE:
      break;
      
    case SMFIR_REPLYCODE:
      gacopyz_srv_reply (gsrv, &buf, &size);
      PMU_DEBUG3 (p->debug, MU_DEBUG_TRACE2, 		
		  "%s=%s, reply=%s\n",
		  ident, arg, buf);
      tmp = trailcrlf (&buf);
      SM_VERBOSE (sm_pmfi_setreply (pmse_ctx, tmp));
      status = (buf[0] == '4') ? SMTP_R_TEMP : SMTP_R_PERM;
      free (tmp);
      return status;
      
    case SMFIR_REJECT:
      PMU_DEBUG3 (p->debug, MU_DEBUG_TRACE2,
		  "%s=%s, reply=%s\n",
		  ident, arg, MSG_REJECT);
      SM_VERBOSE (sm_pmfi_setreply (pmse_ctx, MSG_REJECT));
      return SMTP_R_PERM;

    case SMFIR_DISCARD:
      PMU_DEBUG2 (p->debug, MU_DEBUG_TRACE2,
		  "%s=%s, discard\n",
		  ident, arg);
      return SMTP_R_DISCARD;

    case SMFIR_TEMPFAIL:
      PMU_DEBUG3 (p->debug, MU_DEBUG_TRACE2,
		  "%s=%s, reply=%s\n",
		  ident, arg, MSG_TEMPFAIL);
      SM_VERBOSE (sm_pmfi_setreply (pmse_ctx, MSG_TEMPFAIL));
      return SMTP_R_TEMP;

    case SMFIR_SHUTDOWN:
      PMU_DEBUG3 (p->debug, MU_DEBUG_TRACE2,
		  "%s=%s, reply=%s\n",
		  ident, arg, MSG_SHUTDOWN);
      SM_VERBOSE (sm_pmfi_setreply (pmse_ctx, MSG_SHUTDOWN));
      return SMTP_R_SSD;

    case SMFIR_ADDRCPT:
      gacopyz_srv_reply (gsrv, &buf, &size);
      PMU_DEBUG3 (p->debug, MU_DEBUG_TRACE2, 		
		  "%s=%s, addrcpt=%s\n",
		  ident, arg, buf);
      SM_VERBOSE (sm_pmfi_rcpt_add (pmse_ctx, buf, NULL));
      break;

    case SMFIR_DELRCPT:
      gacopyz_srv_reply (gsrv, &buf, &size);
      PMU_DEBUG3 (p->debug, MU_DEBUG_TRACE2, 		
		  "%s=%s, delrcpt=%s\n",
		  ident, arg, buf);
      /* FIXME: Index is always 0. Should it be 1? */
      SM_VERBOSE (sm_pmfi_rcpt_del (pmse_ctx, buf, 0));
      break;

    case SMFIR_CHGHEADER:
    case SMFIR_ADDHEADER:
    case SMFIR_INSHEADER:
      break;
      
    case SMFIR_REPLBODY:
      /* FIXME */

    default:
      if (isascii (rc) && isprint (rc))
	mu_diag_output (MU_DIAG_WARNING, _("Unsupported reply code: %c (%d)"),
			rc, rc);
      else
	mu_diag_output (MU_DIAG_WARNING, _("Unsupported reply code: (%d)"),
			rc);
    }
  return SMTP_R_CONT;
}

typedef sfsistat_T (*pmult_runfun_t) (pmse_ctx_P, gacopyz_srv_t, void *);

static sfsistat_T
pmult_runlist0 (struct pmult_priv_data *p, pmult_runfun_t runfun,
		pmse_ctx_P pmse_ctx, void *data, char **macros)
{
  sfsistat_T rc;
  mu_iterator_t itr = NULL;

  mu_list_get_iterator (p->srvlist, &itr);

  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
       mu_iterator_next (itr))
    {
      int i;
      
      gacopyz_srv_t gsrv;

      mu_iterator_current (itr, (void**)&gsrv);

      if (macros)
	for (i = 0; macros[i]; i += 2)
	  gacopyz_srv_define_macro (gsrv, macros[i], macros[i+1]);
      
      rc = runfun (pmse_ctx, gsrv, data);
      if (rc != SMTP_R_CONT && rc != SMTP_R_OK)
	break;
    }

  mu_iterator_destroy (&itr);
  return rc;
}

static sfsistat_T
pmult_runlist (struct pmult_priv_data *p, pmult_runfun_t runfun,
	       pmse_ctx_P pmse_ctx, void *data, char **macros)
{
  sfsistat_T rc;
  PRIV_LOCK (p);
  rc = pmult_runlist0 (p, runfun, pmse_ctx, data, macros);
  PRIV_UNLOCK (p);
  return rc;
}

void
pmult_free (struct pmult_priv_data *p)
{
  if (!p)
    return;
  
  mu_list_destroy (&p->srvlist);

  free (p->taid);
  free (p->seid);
  free (p->seid_c);

  mu_stream_destroy (&p->hdrstream, NULL);
  mu_header_destroy (&p->hdr, mu_header_get_owner (p->hdr));
  mu_debug_destroy (&p->debug, NULL);
  pthread_mutex_destroy (&p->mutex);
  free (p);
}

void
pmult_clear (struct pmult_priv_data *p)
{
  PRIV_LOCK (p);
  mu_stream_destroy (&p->hdrstream, NULL);
  mu_header_destroy (&p->hdr, mu_header_get_owner (p->hdr));
  p->state = pmult_msg_state_initial;
  p->nrcpt = 0;
  p->nbadrcpts = 0;
  PRIV_UNLOCK (p);
}

void
pmult_shutdown (pmse_ctx_P pmse_ctx, struct pmult_priv_data *p)
{
  mu_iterator_t itr;

  if (!p)
    return;
  
  PRIV_LOCK (p);
  mu_list_get_iterator (p->srvlist, &itr);

  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
       mu_iterator_next (itr))
    {
      gacopyz_srv_t gsrv;

      mu_iterator_current (itr, (void**)&gsrv);
      gacopyz_srv_quit (gsrv);
      gacopyz_srv_close (gsrv);
      gacopyz_srv_destroy (&gsrv);
    }
  mu_iterator_destroy (&itr);
  sm_pmfi_set_ctx_se (pmse_ctx, NULL);
  PRIV_UNLOCK (p);
  pmult_free (p);
}


static void
version (FILE *stream, struct argp_state *state)
{
  mailfromd_version ("pmult", stream);
}


static sm_ret_T
pmult_negotiate (pmss_ctx_P pmss_ctx,
		 uint32_t srv_cap, uint32_t srv_fct, uint32_t srv_feat,
		 uint32_t srv_misc, uint32_t *pm_cap, uint32_t *pm_fct,
		 uint32_t *pm_feat, uint32_t *pm_misc)
{
  SM_CHECK (sm_pmfi_setmaclist (pmss_ctx, PM_SMST_CONNECT, PMM_SEID, PMM_END));
  SM_CHECK (sm_pmfi_setmaclist (pmss_ctx, PM_SMST_MAIL, PMM_MAIL_TAID,
				PMM_END));
  return SM_SUCCESS;

}

static sfsistat_T
pmult_connect (pmse_ctx_P pmse_ctx, const char *hostname,
	       sm_sockaddr_T *hostaddr)
{
  mu_iterator_t itr;
  struct pmult_priv_data *p;
  mu_debug_t dbg = NULL;
  int status = SMTP_R_CONT;
  int rc;
  char *tmp = NULL;
  char *client_addr = NULL, *client_port = NULL;
  
  if (debug_level)
    {
      mu_debug_create (&dbg, NULL);
      mu_debug_set_level (dbg, debug_level);
    }
  
  if (mu_debug_check_level (dbg, MU_DEBUG_TRACE1))
    {
      char *p = mu_sockaddr_to_astr (&hostaddr->sa, sizeof *hostaddr);
      __PMU_DEBUG2 (dbg, MU_DEBUG_TRACE1,
		    "Connect from: %s, address %s\n", hostname, p);
      free (p);
    }
    
  p = calloc (1, sizeof *p);
  if (!p)
    {
      mu_error ("%s: accept", mu_strerror (ENOMEM));
      mu_debug_destroy (&dbg, NULL);
      return SMTP_R_ACCEPT;
    }
  p->debug = dbg;
  pthread_mutex_init (&p->mutex, NULL);

  sm_pmfi_getmac (pmse_ctx, PMM_SEID, &tmp);
  p->seid = strdup (tmp);

  rc = mu_list_create (&p->srvlist);
  if (rc)
    {
      mu_error ("%smu_list_create: %s", PRIV_SEID_C (p), mu_strerror (rc));
      mu_debug_destroy (&dbg, NULL);
      free (p);
      return SMTP_R_ACCEPT;
    }

  protect ();
  mu_list_get_iterator (client_list, &itr);

  client_addr = strdup (inet_ntoa (hostaddr->sin.sin_addr));
  client_port = strdup (mu_umaxtostr (0, ntohs (hostaddr->sin.sin_port))); 
  unprotect ();
  
  PRIV_LOCK (p);
  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
       mu_iterator_next (itr))
    {
      struct pmult_client *clt;
      gacopyz_srv_t gsrv;

      mu_iterator_current (itr, (void**)&clt);
      gacopyz_srv_create (&gsrv, clt->name, clt->url, clt->logmask);
      gacopyz_srv_set_callback (gsrv, cb_reply);
      gacopyz_srv_set_callback_data (gsrv, pmse_ctx);
      gacopyz_srv_set_all_timeouts (gsrv, clt->timeout);
      if (gacopyz_srv_connect (gsrv) != MI_SUCCESS)
	{
	  mu_error (_("%sFailed to connect to %s (milter %s)"),
		    PRIV_SEID_C (p), clt->url, clt->name);
	  gacopyz_srv_destroy (&gsrv);
	  continue;
	}
      gacopyz_srv_negotiate (gsrv);
      gacopyz_srv_define_macro (gsrv, "r", "SMTP");
      gacopyz_srv_define_macro (gsrv, "i", PRIV_SEID (p));
      
      if (client_addr)
	gacopyz_srv_define_macro (gsrv, "client_addr", client_addr);
      gacopyz_srv_define_macro (gsrv, "client_name", hostname);
      if (client_port)
	gacopyz_srv_define_macro (gsrv, "client_port", client_port);
      /* FIXME: client_ptr, client_resolve */
      
      rc = gacopyz_srv_conn (gsrv, hostname, &hostaddr->sa);
      status = pmult_std_reply (p, pmse_ctx, gsrv, rc, "connect", hostname);
      if (status != SMTP_R_CONT)
	break;
      mu_list_append (p->srvlist, gsrv);
    }

  free (client_addr);
  free (client_port);
  
  protect ();
  mu_iterator_destroy (&itr);
  unprotect ();

  PRIV_UNLOCK (p);
  if (status == SMTP_R_CONT)
    sm_pmfi_set_ctx_se (pmse_ctx, p);
  else
    pmult_free (p);
  return status;
}

static sm_ret_T
pmult_close (pmse_ctx_P pmse_ctx)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  if (p)
    {
      PMU_DEBUG1 (p->debug, MU_DEBUG_TRACE1, "%sClosing connection\n",
		  PRIV_SEID_C (p));
      pmult_shutdown (pmse_ctx, p);
    }
  return SM_SUCCESS;
}

static sfsistat_T
rf_helo (pmse_ctx_P pmse_ctx, gacopyz_srv_t gsrv, void *data)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  const char *helohost = data;
  int rc = gacopyz_srv_helo (gsrv, helohost);
  return pmult_std_reply (p, pmse_ctx, gsrv, rc, "helo", helohost);
}

static sfsistat_T
pmult_helo (pmse_ctx_P pmse_ctx, const char *helohost, bool ehlo)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  char *kv[3];

  PMU_DEBUG2 (p->debug, MU_DEBUG_TRACE1, "%sHELO %s\n",
	      PRIV_SEID_C (p), helohost);
  kv[0] = "s";
  kv[1] = (char*) helohost;
  kv[2] = NULL;
  return pmult_runlist (p, rf_helo, pmse_ctx, (void*) helohost, kv);
}


struct env_arg
{
  char *ident;
  int (*srvfun) (gacopyz_srv_t,  char **);
  char **argv;
};

/* Common for envmail and envrcpt */
static sfsistat_T
rf_envfun (pmse_ctx_P pmse_ctx, gacopyz_srv_t gsrv, void *data)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  struct env_arg *x = data;
  int rc = x->srvfun (gsrv, x->argv);
  return pmult_std_reply (p, pmse_ctx, gsrv, rc, x->ident, x->argv[0]);
}
  
static sfsistat_T
pmult_mail (pmse_ctx_P pmse_ctx, const char *mail, char **argv)
{
  int rc, i, n;
  mu_address_t addr;
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  char *kv[9];
  struct env_arg x;
  sfsistat_T status;
  
  if (mu_debug_check_level (p->debug, MU_DEBUG_TRACE1))
    {
      int i;
      __PMU_DEBUG2 (p->debug, MU_DEBUG_TRACE1, "%sMAIL FROM: %s",
		    PRIV_SEID_C (p), mail);
      if (argv)
	{
	  for (i = 0; argv[i]; i++)
	    __PMU_DEBUG1 (p->debug, MU_DEBUG_TRACE1, " %s", argv[i]);
	}
      PMU_DEBUG (p->debug, MU_DEBUG_TRACE1, "\n");
    }

  /* Fill in the macro array */
  kv[0] = "f";
  rc = mu_address_create (&addr, mail);
  if (rc)
    kv[1] = strdup (mail);
  else
    {
      mu_address_aget_email (addr, 1, &kv[1]);
      mu_address_destroy (&addr);
    }
  kv[2] = "ntries";
  kv[3] = "0";
  kv[4] = "nrcpts";
  kv[5] = "0";
  kv[6] = "nbadrcpts";
  kv[7] = "0";
  kv[8] = NULL;

  /* Count arguments and allocate env array */
  if (argv)
    {
      for (n = 0; argv[n]; n++)
	;
    }
  else
    n = 0;
  
  x.argv = calloc (n + 2, sizeof (x.argv[0]));
  if (!x.argv)
    {
      mu_error ("%spmult_mail: %s", PRIV_SEID_C (p), mu_strerror (ENOMEM));
      free (kv[1]);
      return SMTP_R_CONT;
    }
  x.argv[0] = (char *) mail;
  for (i = 0; i < n; i++)
    x.argv[i+1] = argv[i];
  x.argv[i+1] = NULL;

  x.ident = "envfrom";
  x.srvfun = gacopyz_srv_envfrom;
  status = pmult_runlist (p, rf_envfun, pmse_ctx, &x, kv);
  free (kv[1]);
  free (x.argv);
  return status;
}

int
parse_email_addr (const char *arg, char **psender, char **addr, char **host)
{
  size_t len;
  const char *p = arg, *q;
  if (*p == '<')
    {
      len = strlen (p);
      if (p[len-1] != '>')
	return 1;
      p++;
      *psender = malloc (len - 1);
      if (*psender)
	{
	  memcpy (*psender, p, len - 2);
	  (*psender)[len - 2] = 0;
	}
    }
  else
    *psender = strdup (arg);
  if (!*psender)
    return 1;
  p = *psender;
  q = strchr (p, '@');
  if (q)
    len = q - p;
  else
    len = strlen (p);
  *addr = malloc (len + 1);
  if (!*addr)
    {
      free (*psender);
      return 1;
    }
  memcpy (*addr, p, len);
  (*addr)[len] = 0;
  if (q)
    q++;
  else
    q = "localhost";
  *host = strdup (q);
  if (!*host)
    {
      free (*psender);
      free (*addr);
      return 1;
    }
  return 0;
}

static sfsistat_T
pmult_rcpt (pmse_ctx_P pmse_ctx, const char *rcpt, char **argv)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  char *sender, *addr, *host;
  char *kv[9];
  int i, n;
  struct env_arg x;
  sfsistat_T status;
  char buf1[128], buf2[128];
  
  if (mu_debug_check_level (p->debug, MU_DEBUG_TRACE1))
    {
      int i;
      __PMU_DEBUG2 (p->debug, MU_DEBUG_TRACE1, "%sRCPT TO: %s",
		    PRIV_SEID_C (p), rcpt);
      if (argv)
	{
	  for (i = 0; argv[i]; i++)
	    __PMU_DEBUG1 (p->debug, MU_DEBUG_TRACE1, " %s", argv[i]);
	}
      PMU_DEBUG (p->debug, MU_DEBUG_TRACE1, "\n");
    }

  p->nrcpt++;
  if (parse_email_addr (rcpt, &sender, &addr, &host))
    {
      PMU_DEBUG2 (p->debug, MU_DEBUG_ERROR, "%sbad recipient address %s",
		  PRIV_SEID_C (p), rcpt);
      p->nbadrcpts++;
      return SMTP_R_CONT; /* eh? */
    }

  /* Populate macros array */
  snprintf (buf1, sizeof buf1, "%u", p->nrcpt);
  snprintf (buf2, sizeof buf2, "%u", p->nbadrcpts);
  kv[0] = "nrcpts";
  kv[1] = buf1;
  kv[2] = "nbadrcpts";
  kv[3] = buf2;
  kv[4] = "rcpt_host";
  kv[5] = host;
  kv[6] = "rcpt_addr";
  kv[7] = addr;
  kv[8] = NULL;

  /* Count arguments and allocate env array */
  
  if (argv)
    {
      for (n = 0; argv[n]; n++)
	;
    }
  else
    n = 0;
  
  x.argv = calloc (n + 2, sizeof (x.argv[0]));
  if (!x.argv)
    {
      mu_error ("%spmult_rcpt: %s", PRIV_SEID_C (p), mu_strerror (ENOMEM));
      return SMTP_R_CONT;
    }
  
  x.argv[0] = (char *) rcpt;
  for (i = 0; i < n; i++)
    x.argv[i+1] = argv[i];
  x.argv[i+1] = NULL;

  x.ident = "envrcpt";
  x.srvfun = gacopyz_srv_envrcpt;
  status = pmult_runlist (p, rf_envfun, pmse_ctx, &x, kv);

  free (x.argv);
  free (sender);
  free (addr);
  free (host);
  
  return status;
}


static sfsistat_T
rf_data (pmse_ctx_P pmse_ctx, gacopyz_srv_t gsrv, void *data)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  int rc = gacopyz_srv_data (gsrv);
  return pmult_std_reply (p, pmse_ctx, gsrv, rc, "data", "");
}

static sfsistat_T
pmult_data (pmse_ctx_P pmse_ctx)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  PMU_DEBUG1 (p->debug, MU_DEBUG_TRACE1, "%sDATA\n", PRIV_SEID_C (p));
  return pmult_runlist (p, rf_data, pmse_ctx, NULL, NULL);
}


static sfsistat_T
pmult_unknown (pmse_ctx_P pmse_ctx, const char *cmd)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  PMU_DEBUG2 (p->debug, MU_DEBUG_TRACE1, "%sUnknown command %s\n",
	      PRIV_SEID_C (p), cmd);
  return SMTP_R_CONT;
}


static sfsistat_T
rf_abort (pmse_ctx_P pmse_ctx, gacopyz_srv_t gsrv, void *data)
{
  gacopyz_srv_abort (gsrv);
  return SMTP_R_CONT;
}

static sm_ret_T
pmult_abort (pmse_ctx_P pmse_ctx)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  if (p)
    {
      PMU_DEBUG1 (p->debug, MU_DEBUG_TRACE1, "%sABORT\n", PRIV_SEID_C (p));
      pmult_runlist (p, rf_abort, pmse_ctx, NULL, NULL);
      pmult_clear (p);
    }
  return SM_SUCCESS;
}


struct body_chunk
{
  const char *ident;
  int (*srvfun) (gacopyz_srv_t, unsigned char *, size_t);
  size_t size;
  char *buf;
};

static sfsistat_T
rf_body (pmse_ctx_P pmse_ctx, gacopyz_srv_t gsrv, void *data)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  struct body_chunk *bc = data;
  int rc = bc->srvfun (gsrv, bc->buf, bc->size);
  sfsistat_T status = pmult_std_reply (p, pmse_ctx, gsrv, rc, bc->ident, NULL);
  /* Meta1 won't call EOM if _msg handler returns SMT_R_CONT. Why?
     I don't know neither care. */
  return status == SMTP_R_CONT ? SMTP_R_OK : status;
}

static int
collect_headers (struct pmult_priv_data *p, unsigned char *buf, size_t len,
		 size_t *poff)
{
  int rc;
  size_t off = 0;

  if (p->state == pmult_msg_state_initial)
    {
      mu_memory_stream_create (&p->hdrstream, NULL, MU_STREAM_NO_CHECK);
      p->state = pmult_msg_state_headers;
    }

  for (;;)
    {
      if (p->state == pmult_msg_state_headers)
	{
	  for (; off < len; off++)
	    if (buf[off] == '\r')
	      {
		off++;
		p->state = pmult_msg_state_cr1;
		break;
	      }
	}

      if (off == len)
	break;
      
      if (p->state == pmult_msg_state_cr1 && buf[off] == '\n')
	{
	  p->state = pmult_msg_state_crlf1;
	  off++;
	}

      if (off == len)
	break;
      
      if (p->state == pmult_msg_state_crlf1)
	{
	  if (buf[off] == '\r')
	    {
	      p->state = pmult_msg_state_cr2;
	      off++;
	    }
	  else
	    p->state = pmult_msg_state_headers;
	}

      if (off == len)
	break;

      if (p->state == pmult_msg_state_cr2 && buf[off] == '\n')
	{
	  p->state = pmult_msg_state_body;
	  off++;
	  break;
	}
    }

  rc = mu_stream_sequential_write (p->hdrstream, buf, off);
  if (rc)
    {
      mu_error ("%smu_stream_sequential_write: %s",
		PRIV_SEID_C (p), mu_strerror (rc));
      return 1; 
    }
  *poff = off;
  return 0;
}

struct header
{
  const char *name;
  const char *value;
};

static sfsistat_T
rf_header (pmse_ctx_P pmse_ctx, gacopyz_srv_t gsrv, void *data)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  struct header *hp = data;
  int rc = gacopyz_srv_header (gsrv, (char*)hp->name, (char*)hp->value);
  pmult_std_reply (p, pmse_ctx, gsrv, rc, "header", NULL);
  /* FIXME: do I need to analyze its return? */
  return SMTP_R_OK;
}

static sfsistat_T
rf_eoh (pmse_ctx_P pmse_ctx, gacopyz_srv_t gsrv, void *data)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  int rc = gacopyz_srv_eoh (gsrv);
  pmult_std_reply (p, pmse_ctx, gsrv, rc, "eoh", NULL);
  /* FIXME: do I need to analyze its return? */
  return SMTP_R_OK;
}

static int
process_headers (pmse_ctx_P pmse_ctx, struct pmult_priv_data *p)
{
  mu_off_t size;
  size_t count, i;
  int rc;
  mu_transport_t tbuf;

  /* FIXME-MU: This could be largely simplified if mu_header_t had
     a _set_stream method. */
  rc = mu_stream_size (p->hdrstream, &size);
  if (rc)
    {
      mu_error (_("%sCannot get the size of the header stream: %s"),
		PRIV_SEID_C (p), mu_strerror (rc));
      return 1;
    }
  mu_stream_get_transport (p->hdrstream, &tbuf);
  rc = mu_header_create (&p->hdr, (char*) tbuf, size, NULL);
  if (rc)
    {
      mu_error (_("%sCannot create header: %s"),
		PRIV_SEID_C (p), mu_strerror (rc));
      return 1;
    }

  /* FIXME-MU: mu_header_get_iterator would be in place here. */
  mu_header_get_field_count (p->hdr, &count);
  for (i = 1; i <= count; i++)
    {
      struct header h;
      if (mu_header_sget_field_name (p->hdr, i, &h.name)
	  || mu_header_sget_field_value (p->hdr, i, &h.value))
	continue;
      pmult_runlist0 (p, rf_header, pmse_ctx, &h, NULL);
    }
  pmult_runlist0 (p, rf_eoh, pmse_ctx, NULL, NULL);

  return 0;
}

#define EOM_MARK "\r\n.\r\n"
#define EOM_MARK_LEN (sizeof (EOM_MARK) - 1)

static sfsistat_T
pmult_msg_handler (pmse_ctx_P pmse_ctx, unsigned char *buf, size_t len)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  struct body_chunk bc;

  if (p)
    {
      if (mu_debug_check_level (p->debug, MU_DEBUG_TRACE7))
	__PMU_DEBUG4 (p->debug, MU_DEBUG_TRACE1, "%sBODY %lu %.*s\n",
		      PRIV_SEID_C (p), (unsigned long) len, (int) len, buf);
      else
	PMU_DEBUG2 (p->debug, MU_DEBUG_TRACE1, "%sBODY %lu\n",
		    PRIV_SEID_C (p), (unsigned long) len);
    }
  else
    return SMTP_R_OK;

  if (p->state != pmult_msg_state_body)
    {
      size_t off;
      int rc = collect_headers (p, buf, len, &off);
      if (rc)
	return SMTP_R_ACCEPT; /* Better safe than sorry. */
      if (p->state != pmult_msg_state_body)
	return SMTP_R_OK; /* See comment to rf_body */
      if (process_headers (pmse_ctx, p))
	return SMTP_R_ACCEPT;
      len -= off;
      buf += off;
    }

  /* FIXME: This is not enough, the marker can be split between two
     successive calls. */
  if (len >= EOM_MARK_LEN
      && memcmp (buf + len - EOM_MARK_LEN, EOM_MARK, EOM_MARK_LEN) == 0)
    len -= 3;
  
  bc.ident = "body";
  bc.srvfun = gacopyz_srv_body;
  bc.size = len;
  bc.buf = buf;
  return pmult_runlist0 (p, rf_body, pmse_ctx, &bc, NULL);
}

static sfsistat_T
pmult_msg (pmse_ctx_P pmse_ctx, unsigned char *buf, size_t len)
{
  sfsistat_T rc;
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  PRIV_LOCK (p);
  rc = pmult_msg_handler (pmse_ctx, buf, len);
  PRIV_UNLOCK (p);
  return rc;
}

static sfsistat_T
pmult_eom (pmse_ctx_P pmse_ctx)
{
  struct pmult_priv_data *p = sm_pmfi_get_ctx_se (pmse_ctx);
  struct body_chunk bc;
  sfsistat_T rc;
  
  PMU_DEBUG1 (p->debug, MU_DEBUG_TRACE1, "%sEOM\n", PRIV_SEID_C (p));
  bc.ident = "eom";
  bc.srvfun = gacopyz_srv_eom;
  bc.size = 0;
  bc.buf = "";
  rc = pmult_runlist (p, rf_body, pmse_ctx, &bc, NULL);
  pmult_clear (p);
  return rc;
}


static sm_ret_T
pmult_signal (pmg_ctx_P pmg_ctx, int sig)
{
  mu_diag_output (MU_DIAG_INFO, _("Got signal %d"), sig);
  return SM_SUCCESS;
}


static pmilter_T pmilter = {
  "pmult",
  LPMILTER_VERSION,
  SM_SCAP_PM_ALL,
  0,
  0,
  0,
  pmult_negotiate,
  pmult_connect,
  pmult_helo,
  pmult_mail,
  pmult_rcpt,
  pmult_data,
  pmult_msg,
  pmult_eom,
  pmult_abort,
  pmult_close,
  pmult_unknown,
  pmult_signal
};

void *
main_thread (void *p)
{
  pmg_ctx_P pmg_ctx = p;
  mu_diag_output (MU_DIAG_INFO, _("%s starting"), program_version); 
  SM_ASSERT (sm_pmfi_start (pmg_ctx, &pmilter));
  mu_diag_output (MU_DIAG_INFO, _("%s terminated"), program_version); 
  exit (EX_OK);
}

int
wait_for_signal ()
{
  sigset_t set;
  int rc;
  
  sigemptyset (&set);
  sigaddset (&set, SIGHUP);
  sigaddset (&set, SIGTERM);
  sigaddset (&set, SIGSEGV);
  sigaddset (&set, SIGABRT);
  /* sigaddset (&set, SIGINT); */

  rc = pthread_sigmask (SIG_BLOCK, &set, NULL);
  if (rc)
    {
      mu_error (_("Failed to set up signals: %s"),
		mu_strerror (errno));
      return EX_SOFTWARE;
    }

  sigwait (&set, &rc);
  return EX_OK;
}

int
main (int argc, char **argv)
{
  int rc;
  size_t count;
  pmg_ctx_P pmg_ctx;
  uint32_t major, minor, patchlevel;
  pthread_t tid;
  
  mf_init_nls ();
  if (!program_invocation_short_name)
    program_invocation_short_name = argv[0];
  argp_program_version_hook = version;
  /* Set default logging */
  log_setup (!stderr_closed_p ());

  pmult_cfg_init ();
  
  mu_argp_init (program_version, package_bugreport);
  rc = mu_app_init (&argp, capa, pmult_cfg_param, argc, argv, 0, NULL, NULL);
  if (rc)
    exit (EX_CONFIG);

  log_setup (log_to_stderr);

  if (!portspec)
    {
      mu_error (_("URL to listen on was not specified."));
      exit (EX_CONFIG);
    }

  if (!client_list || mu_list_count (client_list, &count) || count == 0)
    {
      mu_error (_("No clients configured."));
      exit (EX_CONFIG);
    }

  SM_ASSERT (sm_pmfi_init (&pmg_ctx));
  SM_ASSERT (sm_pmfi_version (pmg_ctx, &major, &minor, &patchlevel));
  if (major != LPMILTER_VERSION_MAJOR)
    {
      mu_error (_("Version mismatch: compile_time=%d, run_time=%d"),
		LPMILTER_VERSION_MAJOR, major);
      exit (EX_CONFIG);
    }

  SM_ASSERT (sm_pmfi_setconn (pmg_ctx, portspec));
  SM_ASSERT (sm_pmfi_set_ctx_g (pmg_ctx, NULL));

  if (pidfile)
    {
      rc = mu_daemon_create_pidfile (pidfile);
      if (rc)
	mu_error (_("Cannot create PID file `%s': %s"),
		  pidfile, mu_strerror (rc));
    }

  if (no_sig_handler)
    main_thread (pmg_ctx);
  else
    {
      rc = pthread_create (&tid, NULL, main_thread, pmg_ctx);
      if (rc)
	{
	  mu_error (_("Cannot create main thread: %s"),
		    mu_strerror (errno));
	  exit (EX_SOFTWARE);
	}
  
      return wait_for_signal ();
    }
}

void
xalloc_die ()
{
  mu_error ("not enough memory");
  abort ();
}

/*
 Local Variables:
 c-file-style: "gnu"
 End:
*/
/* EOF */

Return to:

Send suggestions and report system problems to the System administrator.