File: expand.c

package info (click to toggle)
exim 3.36-16sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,736 kB
  • ctags: 3,572
  • sloc: ansic: 52,492; sh: 1,158; perl: 577; makefile: 341
file content (2767 lines) | stat: -rw-r--r-- 84,595 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
/*************************************************
*     Exim - an Internet mail transport agent    *
*************************************************/

/* Copyright (c) University of Cambridge 1995 - 2002 */
/* See the file NOTICE for conditions of use and distribution. */


/* Functions for handling string expansion. */


#include "exim.h"

#ifdef STAND_ALONE
#ifndef SUPPORT_CRYPTEQ
#define SUPPORT_CRYPTEQ
#endif
#endif

#ifdef SUPPORT_CRYPTEQ
#ifdef CRYPT_H
#include <crypt.h>
#endif
#endif


/* Recursively called function */

static char *expand_string_internal(char *, BOOL, char **, BOOL);



/*************************************************
*            Local statics and tables            *
*************************************************/

/* Type for main variable table */

typedef struct {
  char *name;
  int   type;
  void *value;
} var_entry;

/* Type for entries pointing to address/length pairs. Not currently
in use. */

typedef struct {
  char **address;
  int  *length;
} alblock;

/* Types of table entry */

enum {
  vtype_int,            /* value is address of int */
  vtype_filter_int,     /* ditto, but recognized only when filtering */
  vtype_string,         /* value is address of string */
  vtype_stringptr,      /* value is address of pointer to string */
  vtype_msgbody,        /* as stringptr, but read when first required */
  vtype_msgbody_end,    /* ditto, the end of the message */
  vtype_msgheaders,     /* the message's headers */
  vtype_localpart,      /* extract local part from string */
  vtype_domain,         /* extract domain from string */
  vtype_recipients,     /* extract recipients from recipients list */
                        /* (enabled only during system filtering */
  vtype_todbsdin,       /* value not used; generate BSD inbox tod */
  vtype_todf,           /* value not used; generate full tod */
  vtype_todl,           /* value not used; generate log tod */
  vtype_reply           /* value not used; get reply from headers */
  };

/* This table must be kept in alphabetical order. */

static var_entry var_table[] = {
  { "address_file",        vtype_stringptr,   &address_file },
  { "address_pipe",        vtype_stringptr,   &address_pipe },
  { "authenticated_id",    vtype_stringptr,   &authenticated_id },
  { "authenticated_sender",vtype_stringptr,   &authenticated_sender },
  { "body_linecount",      vtype_int,         &body_linecount },
  { "caller_gid",          vtype_int,         &real_gid },
  { "caller_uid",          vtype_int,         &real_uid },
  { "compile_date",        vtype_stringptr,   &version_date },
  { "compile_number",      vtype_stringptr,   &version_cnumber },
  { "domain",              vtype_stringptr,   &deliver_domain },
  { "domain_data",         vtype_stringptr,   &domain_data },
  { "errmsg_recipient",    vtype_stringptr,   &errmsg_recipient },
  { "home",                vtype_stringptr,   &deliver_home },
  { "host",                vtype_stringptr,   &deliver_host },
  { "host_address",        vtype_stringptr,   &deliver_host_address },
  { "host_lookup_failed",  vtype_int,         &host_lookup_failed },
  { "interface_address",   vtype_stringptr,   &interface_address },
  { "key",                 vtype_stringptr,   &lookup_key },
  { "local_part",          vtype_stringptr,   &deliver_localpart },
  { "local_part_data",     vtype_stringptr,   &local_part_data },
  { "local_part_prefix",   vtype_stringptr,   &deliver_localpart_prefix },
  { "local_part_suffix",   vtype_stringptr,   &deliver_localpart_suffix },
  { "localhost_number",    vtype_int,         &host_number },
  { "message_age",         vtype_int,         &message_age },
  { "message_body",        vtype_msgbody,     &message_body },
  { "message_body_end",    vtype_msgbody_end, &message_body_end },
  { "message_body_size",   vtype_int,         &message_body_size },
  { "message_headers",     vtype_msgheaders,  NULL },
  { "message_id",          vtype_stringptr,   &message_id },
  { "message_precedence",  vtype_stringptr,   &message_precedence },
  { "message_size",        vtype_int,         &message_size },
  { "n0",                  vtype_filter_int,  &filter_n[0] },
  { "n1",                  vtype_filter_int,  &filter_n[1] },
  { "n2",                  vtype_filter_int,  &filter_n[2] },
  { "n3",                  vtype_filter_int,  &filter_n[3] },
  { "n4",                  vtype_filter_int,  &filter_n[4] },
  { "n5",                  vtype_filter_int,  &filter_n[5] },
  { "n6",                  vtype_filter_int,  &filter_n[6] },
  { "n7",                  vtype_filter_int,  &filter_n[7] },
  { "n8",                  vtype_filter_int,  &filter_n[8] },
  { "n9",                  vtype_filter_int,  &filter_n[9] },
  { "original_domain",     vtype_stringptr,   &deliver_domain_orig },
  { "original_local_part", vtype_stringptr,   &deliver_localpart_orig },
  { "originator_gid",      vtype_int,         &originator_gid },
  { "originator_uid",      vtype_int,         &originator_uid },
  { "parent_domain",       vtype_stringptr,   &deliver_domain_parent },
  { "parent_local_part",   vtype_stringptr,   &deliver_localpart_parent },
  { "primary_hostname",    vtype_stringptr,   &primary_hostname },
  { "prohibition_reason",  vtype_stringptr,   &prohibition_reason },
  { "qualify_domain",      vtype_stringptr,   &qualify_domain_sender },
  { "qualify_recipient",   vtype_stringptr,   &qualify_domain_recipient },
  { "rbl_domain",          vtype_stringptr,   &rbl_domain },
  { "rbl_text",            vtype_stringptr,   &rbl_msg_buffer },
  { "received_for",        vtype_stringptr,   &received_for },
  { "received_protocol",   vtype_stringptr,   &received_protocol },
  { "recipients",          vtype_recipients,  NULL },
  { "recipients_count",    vtype_int,         &recipients_count },
  { "reply_address",       vtype_reply,       NULL },
  { "return_path",         vtype_stringptr,   &return_path },
  { "return_size_limit",   vtype_int,         &return_size_limit },
  { "route_option",        vtype_stringptr,   &route_option },
  { "self_hostname",       vtype_stringptr,   &self_hostname },
  { "sender_address",      vtype_stringptr,   &sender_address },
  { "sender_address_domain", vtype_domain,    &sender_address },
  { "sender_address_local_part", vtype_localpart, &sender_address },
  { "sender_fullhost",     vtype_stringptr,   &sender_fullhost },
  { "sender_helo_name",    vtype_stringptr,   &sender_helo_name },
  { "sender_host_address", vtype_stringptr,   &sender_host_address },
  { "sender_host_authenticated",vtype_stringptr, &sender_host_authenticated },
  { "sender_host_name",    vtype_stringptr,   &sender_host_name },
  { "sender_host_port",    vtype_int,         &sender_host_port },
  { "sender_ident",        vtype_stringptr,   &sender_ident },
  { "sender_rcvhost",      vtype_stringptr,   &sender_rcvhost },
  { "sn0",                 vtype_filter_int,  &filter_sn[0] },
  { "sn1",                 vtype_filter_int,  &filter_sn[1] },
  { "sn2",                 vtype_filter_int,  &filter_sn[2] },
  { "sn3",                 vtype_filter_int,  &filter_sn[3] },
  { "sn4",                 vtype_filter_int,  &filter_sn[4] },
  { "sn5",                 vtype_filter_int,  &filter_sn[5] },
  { "sn6",                 vtype_filter_int,  &filter_sn[6] },
  { "sn7",                 vtype_filter_int,  &filter_sn[7] },
  { "sn8",                 vtype_filter_int,  &filter_sn[8] },
  { "sn9",                 vtype_filter_int,  &filter_sn[9] },
  { "spool_directory",     vtype_stringptr,   &spool_directory },
  { "thisaddress",         vtype_stringptr,   &filter_thisaddress },
#ifdef SUPPORT_TLS
  { "tls_cipher",          vtype_stringptr,   &tls_cipher },
  { "tls_peerdn",          vtype_stringptr,   &tls_peerdn },
#endif
  { "tod_bsdinbox",        vtype_todbsdin,    NULL },
  { "tod_full",            vtype_todf,        NULL },
  { "tod_log",             vtype_todl,        NULL },
  { "value",               vtype_stringptr,   &lookup_value },
  { "version_number",      vtype_stringptr,   &version_string },
  { "warnmsg_delay",       vtype_stringptr,   &warnmsg_delay },
  { "warnmsg_recipient",   vtype_stringptr,   &warnmsg_recipients },
  { "warnmsg_recipients",  vtype_stringptr,   &warnmsg_recipients }
};

static int var_table_size = sizeof(var_table)/sizeof(var_entry);
static char var_buffer[256];
static BOOL malformed_header;

/* For textual hashes */

static char *hashcodes = "abcdefghijklmnopqrtsuvwxyz"
                         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                         "0123456789";

/* For numeric hashes */

static unsigned int prime[] = {
  2,   3,   5,   7,  11,  13,  17,  19,  23,  29,
 31,  37,  41,  43,  47,  53,  59,  61,  67,  71,
 73,  79,  83,  89,  97, 101, 103, 107, 109, 113};



/*************************************************
*          Check a condition string              *
*************************************************/

/* This function is called to expand a string, and test the result for a "true"
or "false" value. Failure of the expansion is panic-worthy, unless forced or
the result of a lookup defer. All store used by the function can be released on
exit.

Arguments:
  condition     the condition string
  m1            text to be incorporated in panic error
  m2            ditto

Returns:        TRUE if condition is met, FALSE if not
*/

BOOL
expand_check_condition(char *condition, char *m1, char *m2)
{
int rc;
void *reset_point = store_get(0);
char *ss = expand_string(condition);
if (ss == NULL && !expand_string_forcedfail && !search_find_defer)
  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to expand condition\"%s\" "
    "for %s %s: %s", condition, m1, m2, expand_string_message);
if (ss == NULL) return FALSE;
rc = ss[0] != 0 && strcmp(ss, "0") != 0 && strcmpic(ss, "no") != 0 &&
  strcmpic(ss, "false") != 0;
store_reset(reset_point);
return rc;
}



/*************************************************
*             Pick out a name from a string      *
*************************************************/

/* If the name is too long, it is silently truncated.

Arguments:
  name      points to a buffer into which to put the name
  max       is the length of the buffer
  s         points to the first alphabetic character of the name
  extras    chars other than alphanumerics to permit

Returns:    pointer to the first character after the name

Note: The test for *s != 0 in the while loop is necessary because
strchr() yields non-NULL if the character is zero (which is not something
I expected). */

static char *
read_name(char *name, int max, char *s, char *extras)
{
int ptr = 0;
while (*s != 0 && (isalnum((uschar)*s) || strchr(extras, *s) != NULL))
  {
  if (ptr < max-1) name[ptr++] = *s;
  s++;
  }
name[ptr] = 0;
return s;
}



/*************************************************
*     Pick out the rest of a header name         *
*************************************************/

/* A variable name starting $header_ (or just $h_ for those who like
abbreviations) might not be the complete header name because headers can
contain any printing characters in their names, except ':'. This function is
called to read the rest of the name, chop h[eader]_ off the front, and put ':'
on the end, if the name was terminated by white space.

Arguments:
  name      points to a buffer in which the name read so far exists
  max       is the length of the buffer
  s         points to the first character after the name so far, i.e. the
            first non-alphameric character after $header_xxxxx

Returns:    a pointer to the first character after the header name
*/

static char *
read_header_name(char *name, int max, char *s)
{
int prelen = strchr(name, '_') - name + 1;
int ptr = strlen(name) - prelen;
if (ptr > 0) memmove(name, name+prelen, ptr);
while (mac_isgraph(*s) && *s != ':')
  {
  if (ptr < max-1) name[ptr++] = *s;
  s++;
  }
if (*s == ':') s++;
name[ptr++] = ':';
name[ptr] = 0;
return s;
}



/*************************************************
*           Pick out a number from a string      *
*************************************************/

/* Arguments:
  n     points to an integer into which to put the number
  s     points to the first digit of the number

Returns:  a pointer to the character after the last digit
*/

static char *
read_number(int *n, char *s)
{
*n = 0;
while (isdigit((uschar)*s)) *n = *n * 10 + (*s++ - '0');
return s;
}



/*************************************************
*        Extract keyed subfield from a string    *
*************************************************/

/* This function is also used by the search routines when given a multi-level
key to search for. The yield is in dynamic store; NULL means that the key
was not found.

Arguments:
  key       points to the name of the subkey
  s         points to the string from which to extract the subfield

Returns:    NULL if the subfield was not found, or
            a pointer to the subfield's data
*/

char *
expand_getkeyed(char *key, char *s)
{
int length = (int)strlen(key);
while (isspace((uschar)*s)) s++;

/* Loop to search for the key */

while (*s != 0)
  {
  int subkeylength, datalength;
  char *data;
  char *subkey = s;

  while (*s != 0 && *s != '=' && !isspace((uschar)*s)) s++;
  subkeylength = s - subkey;

  while (isspace((uschar)*s)) s++;
  if (*s == '=') while (isspace((uschar)(*(++s))));

  /* For now, just find the end of the data field - interpret quoted
  string later if it is actually wanted. */

  data = s;
  if (*s == '\"')
    {
    while (*(++s) != 0 && *s != '\"')
      {
      if (*s == '\\' && s[1] != 0) s++;
      }
    if (*s == '\"') s++;
    }
  else while (*s != 0 && !isspace((uschar)*s)) s++;
  datalength = s - data;

  /* If keys match, set up the subfield as the yield and return. If
  the value is quoted, interpret the string (which cannot be longer than
  the original). */

  if (length == subkeylength && strncmp(key, subkey, length) == 0)
    {
    char *yield = store_get(datalength + 1);

    if (*data == '\"')
      {
      int ch, i;
      for (i = 0;;)
        {
        ch = *(++data);
        if (ch == 0 || ch == '\"') break;
        if (ch == '\\') ch = string_interpret_escape(&data);
        yield[i++] = ch;
        }
      yield[i] = 0;
      }

    /* Not a quoted string */
    else
      {
      strncpy(yield, data, datalength);
      yield[datalength] = 0;
      }
    return yield;
    }

  /* Move on to next subkey */

  while (isspace((uschar)*s)) s++;
  }

return NULL;
}




/*************************************************
*   Extract numbered subfield from string        *
*************************************************/

/* Extracts a numbered field from a string that is divided by tokens - for
example a line from /etc/passwd is divided by colon characters.  First field is
numbered one.  Returns NULL if the field number is < 0, or if there are
insufficient tokens in the string

***WARNING***
Modifies final argument - this is a dynamically generated string, so that's OK.

Arguments:
  field       number of field to be extracted,
                first field = 1, whole string = 0
  separators  characters that are used to break string into tokens
  s           points to the string from which to extract the subfield

Returns:      NULL if the field was not found,
              a pointer to the field's data inside s (modified to add 0)
*/

static char *
expand_gettokened (int field, char *separators, char *s)
{
int sep = 1;
char *fieldtext = NULL;

/* Invalid field number returns NULL; zero field number returns the whole
subject string. */

if (field < 0) return NULL;
if (field == 0) return s;

/* Search for the required field; create it in place. */

while (field-- > 0)
  {
  size_t len;
  if (sep == 0) return NULL;   /* Previous field was end of string */
  fieldtext = s;
  len = strcspn(s, separators);
  sep = s[len];
  s[len] = 0;
  s += len + 1;
  }

return fieldtext;
}




/*************************************************
*               Find value of a variable         *
*************************************************/

/* The table of variables is kept in alphabetic order, so we
can search it using a binary chop. The "choplen" variable is
nothing to do with the binary chop. It can be set non-zero to
cause chars at the end of the returned string to be disregarded.
It should already be zero on entry.

Arguments:
  name        the name of the variable being sought
  choplen     a pointer to an int which is set if characters at the end
              of the returned data are to be ignored (typically '\n' at the
              end of header lines); it should normally be set zero before
              calling this function

Returns:      NULL if the variable does not exist, or
              a pointer to the variable's contents
*/

static char *
find_variable(char *name, int *choplen)
{
int first = 0;
int last = var_table_size;

while (last > first)
  {
  header_line *h;
  char *s, *domain;
  char **ss;
  int middle = (first + last)/2;
  int c = strcmp(name, var_table[middle].name);

  if (c == 0) switch (var_table[middle].type)
    {
    case vtype_filter_int:
    if (!filter_running) return NULL;
    /* Fall through */

    case vtype_int:
    sprintf(var_buffer, "%d", *(int *)(var_table[middle].value)); /* Integer */
    return var_buffer;

    case vtype_string:                         /* String */
    return (char *)(var_table[middle].value);

    case vtype_stringptr:                      /* Pointer to string */
    s = *((char **)(var_table[middle].value));
    return (s == NULL)? "" : s;

    case vtype_localpart:                      /* Get local part from address */
    s = *((char **)(var_table[middle].value));
    if (s == NULL) return "";
    domain = strrchr(s, '@');
    if (domain == NULL) return s;
    if (domain - s > sizeof(var_buffer) - 1)
      log_write(0, LOG_PANIC_DIE, "local part longer than %d in string "
        "expansion", sizeof(var_buffer));
    strncpy(var_buffer, s, domain - s);
    var_buffer[domain - s] = 0;
    return var_buffer;

    case vtype_domain:                         /* Get domain from address */
    s = *((char **)(var_table[middle].value));
    if (s == NULL) return "";
    domain = strrchr(s, '@');
    return (domain == NULL)? "" : domain + 1;

    case vtype_msgheaders:
      {
      int ptr = 0;
      int size = 100;
      header_line *h;
      s = store_get(size);
      for (h = header_list; h != NULL && size < 64 * 1024; h = h->next)
        {
        if (h->type == '*') continue;
        s = string_cat(s, &size, &ptr, h->text, h->slen);
        }
      s[ptr] = 0;
      }
    return s;

    case vtype_msgbody:                        /* Pointer to msgbody string */
    case vtype_msgbody_end:                    /* Ditto, the end of the msg */
    ss = (char **)(var_table[middle].value);
    if (*ss == NULL && deliver_datafile >= 0)  /* Read body when needed */
      {
      char *body;
      int start_offset = DATA_START_OFFSET;
      int len = message_body_visible;
      if (len > message_size) len = message_size;
      *ss = body = store_malloc(len+1);
      body[0] = 0;
      if (var_table[middle].type == vtype_msgbody_end)
        {
        struct stat statbuf;
        if (fstat(deliver_datafile, &statbuf) == 0)
          {
          start_offset = statbuf.st_size - len;
          if (start_offset < DATA_START_OFFSET)
            start_offset = DATA_START_OFFSET;
          }
        }
      lseek(deliver_datafile, start_offset, SEEK_SET);
      len = read(deliver_datafile, body, len);
      if (len >= 0) body[len] = 0;
      while (len > 0)
        {
        if (body[--len] == '\n' || body[len] == 0) body[len] = ' ';
        }
      }
    return (*ss == NULL)? "" : *ss;

    case vtype_todbsdin:                       /* BSD inbox time of day */
    return tod_stamp(tod_bsdin);

    case vtype_todf:                           /* Full time of day */
    return tod_stamp(tod_full);

    case vtype_todl:                           /* Log format time of day */
    return tod_stamp(tod_log);

    case vtype_reply:                          /* Get reply address */
    s = NULL;
    for (h = header_list; h != NULL; h = h->next)
      {
      if (h->type == htype_replyto) s = strchr(h->text, ':') + 1;
      if (h->type == htype_from && s == NULL) s = strchr(h->text, ':') + 1;
      }
    if (s == NULL) return "";
    while (isspace((uschar)*s)) s++;

    /* Disregard final \n in header (but note that isspace() might already
    have skipped over it if the header is empty). */

    if (choplen != NULL && *s != 0) *choplen = 1;
    return s;

    /* A recipients list is available only during system message filtering
    or if there is only one recipient, but not elsewhere. */

    case vtype_recipients:

    if (!enable_dollar_recipients) return NULL; else
      {
      int size = 128;
      int ptr = 0;
      int i;
      s = store_get(size);
      for (i = 0; i < recipients_count; i++)
        {
        if (i != 0) s = string_cat(s, &size, &ptr, ", ", 2);
        s = string_cat(s, &size, &ptr, recipients_list[i].address,
          (int)strlen(recipients_list[i].address));
        }
      s[ptr] = 0;                             /* string_cat leaves room */
      }
    return s;
    }

  else if (c > 0) first = middle + 1;
  else last = middle;
  }

return NULL;
}




/*************************************************
*          Find the value of a header            *
*************************************************/

/*
Arguments:
  name       the name of the header, without the leading $header_ or $h_

Returns:     NULL if the header does not exist, else
             a pointer to the header's contents; truncated to 16K if there
             lots that got concatenated
*/

static char *
find_header(char *name)
{
int len = (int)strlen(name);
int ptr = -1;
int size = 0;
char *yield = NULL;
header_line *h;

for (h = header_list; h != NULL && size < 64 * 1024; h = h->next)
  {
  if (h->type != htype_old && h->text != NULL)  /* NULL => Received: placeholder */
    {
    if (len <= h->slen && strncmpic(name, h->text, len) == 0)
      {
      /* If this is the first relevant header, just point to its data. Often
      there is only one, so this is efficient. */

      if (yield == NULL)
        {
        yield = h->text + len;
        while (isspace((uschar)*yield)) yield++;
        }

      /* If this is not the first header, see if it's the second, and in that
      case start off an extensible string. Otherwise add to the string using
      string_cat(), which tries to extend the block rather than copying if
      it can. */

      else if (ptr < 0)
        {
        char *newyield;
        size = (int)strlen(yield) + h->slen + 100;
        newyield = store_get(size);
        strcpy(newyield, yield);
        strcat(newyield, h->text + len);
        yield = newyield;
        ptr = (int)strlen(yield);
        yield[ptr] = 0;
        }

      /* Third or subsequent header */

      else
        {
        yield = string_cat(yield, &size, &ptr, h->text +len, h->slen - len);
        yield[ptr] = 0;
        }
      }
    }
  }
return yield;
}




/*************************************************
*        Read and evaluate a condition           *
*************************************************/

/*
Arguments:
  s        points to the start of the condition text
  yield    points to a BOOL to hold the result of the condition test;
           if NULL, we are just reading through a condition that is
           part of an "or" combination to check syntax, or in a state
           where the answer isn't required

Returns:   a pointer to the first character after the condition, or
           NULL after an error
*/

static char *
eval_condition(char *s, BOOL *yield)
{
BOOL testfor = TRUE;
char name[256];

for (;;)
  {
  while (isspace((uschar)*s)) s++;
  if (*s == '!') { testfor = !testfor; s++; } else break;
  }

/* Numeric comparisons are symbolic */

if (*s == '=' || *s == '>' || *s == '<')
  {
  int p = 0;
  name[p++] = *s++;
  if (*s == '=')
    {
    name[p++] = '=';
    s++;
    }
  name[p] = 0;
  }

/* All other conditions are named */

else s = read_name(name, 256, s, "_");

/* If we haven't read a name, it means some non-alpha character is first. */

if (name[0] == 0)
  {
  expand_string_message = string_sprintf("condition name expected, "
    "but found \"%.16s\"", s);
  return NULL;
  }

/* def: tests for a non-zero or non-NULL variable, or for an existing
header */

if (strcmp(name, "def") == 0 && *s == ':')
  {
  char *value;

  s = read_name(name, 256, s+1, "_");

  /* Test for a header's existence */

  if (strncmp(name, "header_", 7) == 0 || strncmp(name, "h_", 2) == 0)
    {
    s = read_header_name(name, 256, s);
    value = find_header(name);
    if (yield != NULL) *yield = (value != NULL) == testfor;
    }

  /* Test for a variable's having a non-empty value. */

  else
    {
    value = find_variable(name, NULL);
    if (value == NULL)
      {
      expand_string_message = (name[0] == 0)?
        string_sprintf("variable name omitted after \"def:\"") :
        string_sprintf("unknown variable \"%s\" after \"def:\"", name);
      return NULL;
      }
    if (yield != NULL)
      *yield = (value[0] != 0 && strcmp(value, "0") != 0) == testfor;
    }

  return s;
  }

/* first_delivery tests for first delivery attempt */

else if (strcmp(name, "first_delivery") == 0)
  {
  if (yield != NULL) *yield = deliver_firsttime == testfor;
  return s;
  }

/* queue_running tests for any process started by a queue runner */

else if (strcmp(name, "queue_running") == 0)
  {
  if (yield != NULL) *yield = (queue_run_pid != (pid_t)0) == testfor;
  return s;
  }

/* exists: tests for file existence
      pam: does PAM authentication
*/

else if (strcmp(name, "exists") == 0
        #ifdef SUPPORT_PAM
        || strcmp(name, "pam") == 0
        #endif /* SUPPORT_PAM */
        )
  {
  char *sub;
  struct stat statbuf;
  while (isspace((uschar)*s)) s++;
  if (*s != '{') goto COND_FAILED_CURLY_START;
  sub = expand_string_internal(s+1, TRUE, &s, yield == NULL);
  if (sub == NULL) return NULL;
  if (*s++ != '}') goto COND_FAILED_CURLY_END;
  if (yield != NULL)
    {
    #ifdef SUPPORT_PAM
    if (name[0] == 'p')
      {
      int rc = auth_call_pam(sub, &expand_string_message);
      if (rc == ERROR) return NULL;
      *yield = (rc == OK) == testfor;
      }
    else
    #endif /* SUPPORT_PAM */
      {
      if (expand_forbid_exists)
        {
        expand_string_message = "File existence tests are not permitted";
        return NULL;
        }
      *yield = (stat(sub, &statbuf) == 0) == testfor;
      }
    }
  return s;
  }

/* eq:   tests for string equality
match:   does a regular expression match and sets up the numerical
           variables if it succeeds
crypteq: encrypts plaintext and compares against an encrypted text, using
           crypt() as is done for passwords
symbolic operators for numeric testing
*/


else if (strcmp(name, "eq") == 0 ||
         strcmp(name, "match") == 0 ||
         strcmp(name, "crypteq") == 0 ||
         !isalpha((uschar)name[0]))
  {
  int i;
  int roffset;
  int num[2];
  pcre *re;
  const char *rerror;
  char *sub[2];

  for (i = 0; i < 2; i++)
    {
    while (isspace((uschar)*s)) s++;
    if (*s != '{')
      {
      if (i == 0) goto COND_FAILED_CURLY_START;
      expand_string_message = string_sprintf("missing 2nd string in {} "
        "after \"%s\"", name);
      return NULL;
      }
    sub[i] = expand_string_internal(s+1, TRUE, &s, yield == NULL);
    if (sub[i] == NULL) return NULL;
    if (*s++ != '}') goto COND_FAILED_CURLY_END;

    /* Convert to numerical if required */

    if (!isalpha((uschar)name[0]))
      {
      char *endptr;
      num[i] = (int)strtol((const char *)sub[i], &endptr, 10);
      if (tolower(*endptr) == 'k')
        {
        num[i] *= 1024;
        endptr++;
        }
      else if (tolower(*endptr) == 'm')
        {
        num[i] *= 1024*1024;
        endptr++;
        }
      if (*endptr != 0)
        {
        expand_string_message = string_sprintf("\"%s\" is not a number",
          sub[i]);
        return NULL;
        }
      }
    }

  /* Result not required */

  if (yield == NULL) return s;

  /* Do an appropriate comparison */

  switch(name[0])
    {
    case '=':
    *yield = (num[0] == num[1]) == testfor;
    break;

    case '>':
    *yield = ((name[1] == '=')? (num[0] >= num[1]) : (num[0] > num[1]))
      == testfor;
    break;

    case '<':
    *yield = ((name[1] == '=')? (num[0] <= num[1]) : (num[0] < num[1]))
      == testfor;
    break;

    case 'e':   /* Straight text comparison */
    *yield = (strcmp(sub[0], sub[1]) == 0) == testfor;
    break;

    case 'm':   /* Regular expression match */
    re = pcre_compile(sub[1], PCRE_COPT, &rerror, &roffset, NULL);
    if (re == NULL)
      {
      expand_string_message = string_sprintf("regular expression error in "
        "\"%s\": %s at offset %d", sub[1], rerror, roffset);
      return NULL;
      }
    *yield = regex_match_and_setup(re, sub[0], 0, -1) == testfor;
    break;

    /* Various "encrypted" comparisons. If the second string starts with
    "{" then an encryption type is given. Default to crypt(). */

    case 'c':
    #ifdef SUPPORT_CRYPTEQ
    if (strncmp(sub[1], "{md5}", 5) == 0)
      {
      md5 base;
      uschar digest[16];
      md5_start(&base);
      md5_end(&base, (uschar *)sub[0], (int)strlen(sub[0]), digest);
      *yield = (strcmp(auth_b64encode((char *)digest, 16), sub[1]+5) == 0)
        == testfor;
      }
    else   /* {crypt} and non-{ start use crypt(). */
      {
      if (strncmp(sub[1], "{crypt}", 7) == 0) sub[1] += 7;
      else if (sub[1][0] == '{')
        {
        expand_string_message = string_sprintf("unknown encryption mechanism "
          "in \"%s\"", sub[1]);
        return NULL;
        }

      /* If the encrypted string contains fewer than two characters (for the
      salt), force failure. Otherwise we get false positives: with an empty
      string the yield of crypt() is an empty string! */

      *yield = (strlen(sub[1]) < 2)? !testfor :
        (strcmp(crypt(CS sub[0], CS sub[1]), sub[1]) == 0) == testfor;
      }
    break;
    #else
    expand_string_message = "support for \"crypteq\" not compiled";
    return NULL;
    #endif
    }

  return s;
  }

/* and/or: computes logical and/or of several conditions */

else if (strcmp(name, "or") == 0 || strcmp(name, "and") == 0)
  {
  BOOL temp;
  BOOL *ptr = (yield == NULL)? NULL : &temp;
  BOOL and = strcmp(name, "and") == 0;
  int comb = and;

  while (isspace((uschar)*s)) s++;
  if (*s++ != '{') goto COND_FAILED_CURLY_START;

  for (;;)
    {
    while (isspace((uschar)*s)) s++;
    if (*s == '}') break;
    if (*s != '{')
      {
      expand_string_message = string_sprintf("subcondition in {} expected "
        "inside \"%s{...}\" condition", name);
      return NULL;
      }
    s = eval_condition(s+1, ptr);
    if (s == NULL) return NULL;
    while (isspace((uschar)*s)) s++;
    if (*s++ != '}')
      {
      expand_string_message = string_sprintf("missing } at end of condition "
        "inside \"%s\" group", name);
      return NULL;
      }
    if (yield != NULL)
      {
      if (and)
        {
        comb &= temp;
        if (!comb) ptr = NULL;  /* once false, don't evaluate any more */
        }
      else
        {
        comb |= temp;
        if (comb) ptr = NULL;   /* once true, don't evaluate any more */
        }
      }
    }

  if (yield != NULL) *yield = (comb == testfor);
  return ++s;
  }

/* Unknown type of condition */

expand_string_message = string_sprintf("unknown condition \"%s\"", name);
return NULL;

/* Missing braces at start and end of data */

COND_FAILED_CURLY_START:
expand_string_message = string_sprintf("missing { after \"%s\"", name);
return NULL;

COND_FAILED_CURLY_END:
expand_string_message = string_sprintf("missing } at end of \"%s\" condition",
  name);
return NULL;
}




/*************************************************
*            Read and expand n substrings        *
*************************************************/

/* This function is called to read and expand argument substrings for various
expansion items.

Arguments:
  sub        points to vector of pointers to set
  n          number of substrings
  sptr       points to current string pointer
  skipping   the skipping flag

Returns:     0 OK; string pointer updated
             1 curly bracketing error
             2 other error
*/

static int
read_subs(char **sub, int n, char **sptr, BOOL skipping)
{
int i;
char *s = *sptr;

while (isspace((uschar)*s)) s++;
for (i = 0; i < n; i++)
  {
  if (*s != '{') return 1;
  sub[i] = expand_string_internal(s+1, TRUE, &s, skipping);
  if (sub[i] == NULL) return 2;
  while (isspace((uschar)*s)) s++;
  if (*s++ != '}') return 1;
  while (isspace((uschar)*s)) s++;
  }
if (*s++ != '}') return 1;

*sptr = s;
return 0;
}




/*************************************************
*          Save numerical variables              *
*************************************************/

/* This function is called from items such as "if" that want to preserve and
restore the numbered variables.

Arguments:
  save_expand_string    points to an array of pointers to set
  save_expand_nlength   points to an array of ints for the lengths

Returns:                the value of expand max to save
*/

static int
save_expand_strings(char **save_expand_nstring, int *save_expand_nlength)
{
int i;
for (i = 0; i <= expand_nmax; i++)
  {
  save_expand_nstring[i] = expand_nstring[i];
  save_expand_nlength[i] = expand_nlength[i];
  }
return expand_nmax;
}



/*************************************************
*           Restore numerical variables          *
*************************************************/

/* This function restored saved values of numerical strings.

Arguments:
  save_expand_nmax      the number of strings to restore
  save_expand_string    points to an array of pointers
  save_expand_nlength   points to an array of ints

Returns:                nothing
*/

static void
restore_expand_strings(int save_expand_nmax, char **save_expand_nstring,
  int *save_expand_nlength)
{
int i;
expand_nmax = save_expand_nmax;
for (i = 0; i <= expand_nmax; i++)
  {
  expand_nstring[i] = save_expand_nstring[i];
  expand_nlength[i] = save_expand_nlength[i];
  }
}





/*************************************************
*            Handle yes/no substrings            *
*************************************************/

/* This function is used by ${if}, ${lookup} and ${extract} to handle the
alternative substrings that depend on whether or not the condition was true,
or the lookup or extraction succeeded. The substrings always have to be
expanded, to check their syntax, but "skipping" is set when the result is not
needed - this avoids unnecessary nested lookups.

Arguments:
  skipping       TRUE if we were skipping when this item was reached
  yes            TRUE if the first string is to be used, else use the second
  errmsg         NULL, or an additional message for failure
  save_lookup    a value to put back into lookup_value before the 2nd expansion
  sptr           points to the input string pointer
  yieldptr       points to the output string pointer
  sizeptr        points to the output string size
  ptrptr         points to the output string pointer
  type           "lookup" or "if" or "extract", for error message

Returns:         0 OK; lookup_value has been reset to save_lookup
                 1 expansion failed
                 2 expansion failed because of bracketing error
*/

static int
process_yesno(BOOL skipping, BOOL yes, char *errmsg, char *save_lookup, char
  **sptr, char **yieldptr, int *sizeptr, int *ptrptr, char *type)
{
int rc = 0;
char *s = *sptr;    /* Local value */
char *sub1, *sub2;

/* If there are no following strings, we substitute the contents of $value for
lookups and for extractions in the success case. In the fail case, nothing is
substituted. In the case of "if", lack of following strings is an error. */

while (isspace((uschar)*s)) s++;
if (*s == '}')
  {
  if (type[0] == 'i') goto FAILED_CURLY;
  if (yes)
    *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, lookup_value,
      (int)strlen(lookup_value));
  lookup_value = save_lookup;
  s++;
  goto RETURN;
  }

/* Expand the first substring. Forced failures are noticed only if we actually
want this string. Set skipping in the call in the fail case (this will always
be the case if we were already skipping). */

sub1 = expand_string_internal(s+1, TRUE, &s, !yes);
if (sub1 == NULL && (yes || !expand_string_forcedfail)) goto FAILED;
expand_string_forcedfail = FALSE;
if (*s++ != '}') goto FAILED_CURLY;

/* If we want the first string, add it to the output */

if (yes)
  *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, sub1, (int)strlen(sub1));

/* If this is called from a lookup or an extract, we want to restore $value to
what it was at the start of the item, so that it has this value during the
second string expansion. For the call from "if" to this function, save_lookup
is set to lookup_value, so that this statement does nothing. */

lookup_value = save_lookup;

/* There now follows either another substring, or "fail", or nothing. This
time, forced failures are noticed only if we want the second string. We must
set skipping in the nested call if we don't want this string, or if we were
already skipping. */

while (isspace((uschar)*s)) s++;
if (*s == '{')
  {
  sub2 = expand_string_internal(s+1, TRUE, &s, yes || skipping);
  if (sub2 == NULL && (!yes || !expand_string_forcedfail)) goto FAILED;
  expand_string_forcedfail = FALSE;
  if (*s++ != '}') goto FAILED_CURLY;

  /* If we want the second string, add it to the output */

  if (!yes)
    *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, sub2, (int)strlen(sub2));
  }

/* If there is no second string, but the word "fail" is present when the use of
the second string is wanted, set a flag indicating it was a forced failure
rather than a syntactic error. Swallow the terminating } in case this is nested
inside another lookup or if or extract. */

else if (*s != '}')
  {
  char name[256];
  s = read_name(name, sizeof(name), s, "_");
  if (strcmp(name, "fail") == 0)
    {
    if (!yes && !skipping)
      {
      while (isspace((uschar)*s)) s++;
      if (*s++ != '}') goto FAILED_CURLY;
      expand_string_message =
        string_sprintf("\"%s\" failed and \"fail\" requested%s%s", type,
          (errmsg == NULL || errmsg[0] == 0)? "" : ": ",
          (errmsg == NULL)? "" : errmsg);
      expand_string_forcedfail = TRUE;
      goto FAILED;
      }
    }
  else
    {
    expand_string_message =
      string_sprintf("syntax error in \"%s\" item - \"fail\" expected", type);
    goto FAILED;
    }
  }

/* All we have to do now is to check on the final closing brace. */

while (isspace((uschar)*s)) s++;
if (*s++ == '}') goto RETURN;

/* Get here if there is a bracketing failure */

FAILED_CURLY:
rc++;

/* Get here for other failures */

FAILED:
rc++;

/* Update the input pointer value before returning */

RETURN:
*sptr = s;
return rc;
}






/*************************************************
*                 Expand string                  *
*************************************************/

/* Returns either an unchanged string, or the expanded string in stacking pool
store. Interpreted sequences are:

   \...                    normal escaping rules
   $name                   substitutes the variable
   ${name}                 ditto
   ${op:string}            operates on the string value
   ${extract {key} {string}}
                           extracts keyed substring; null if not found
   ${extract {field} {separator} {string}}
                           extracts numbered field from separated string;
                           null if not found; field 0 is the whole string

   Either form of extract may now have two additional arguments; these are
   used in the success/failure cases, as for ${if} and ${lookup}.

   ${if cond {s1} {s2}}    conditional; the yielded string is expanded
                           {s2} can be replaced by "fail" or be omitted
   ${perl{sub}{a1}...      call Perl (if configured)
   ${sg {source}{s1}{s2}}  global substitution
   ${tr {source}{s1}{s2}}  translate characters

One-key+file lookups:
   ${lookup{key}search-type{file}{found}{not-found}}
                           the key, file, & strings are expanded; $value
                           is available in {found}, and {not-found} can be
                           replaced by "fail" or be omitted.
                           The key can in fact consist of mainkey:subkey, in
                           which case a subfield is extracted from the found
                           string, which must consist of key=value pairs.

Database-query lookups:
   ${lookup search-type{query}{found}{not-found}}
                           the query and strings are expanded; $value available
                           and {not-found} can be replaced by "fail" or be
                           omitted.

Operators:
   domain                  extracts domain from an address
   escape                  escapes non-printing characters
   expand                  expands the string one more time
   hash_<n>_<m>            hash the string, making one that is of length n,
                             using m (default 26) characters from hashcodes
   nhash_<n>_<m>           hash the string into one or two numerical values,
                           using a div/mod scheme to produce a string of the
                           form "a/b" where 0 <= a < n-1 and 0 <= b <= m-1.
                           If <m> is omitted, just give one number.
   md5                     yields the md5 hash of the argument
   lc                      lowercase the string
   uc                      uppercase the string
   length_<n>              take first n chars only
   l_<n>                   ditto
   local_part              extracts local part from an address
   mask                    masks an IP address
   quote                   quote the argument if it contains anything other
                             than letters, digits, underscores, dots, & hyphens;
                             quoting means putting inside "" and \-quoting any
                             \ or " in the string
   quote_xxx               quote the argument by calling the quoting function
                             of the xxx lookup
   rxquote                 regular expression quote: any non-alphameric is
                             quoted with \
   substr_<m>_<n>          substring n chars from offset m
   s_<m>_<n>               ditto
                             negative offset works from the rhs
                             omitted length => rest (either to left or right)

Conditions:
   !cond                   negates condition
   def:variable            variable is defined and not empty
   def:$h_xxx              header xxx exists
   exists {string}         file exists
   match {string}{re}      regular expression match
   eq {string1}{string2}   strings are equal, case included
   crypteq{s1}{s2}         encrypt s1, compare with s2
                           s2 may be of the form {crypt}text or {md5}text
   pam {data}              PAM authentication (if configured in)
   == {num1}{num2}         numbers are equal
   > >= < <=               similar numeric comparisons
   or {{cond1}{cond2}...}  as it says
   and {{cond1}{cond2}...} ditto

We use an internal routine recursively to handle embedded substrings. The
external function follows. The yield is NULL if the expansion failed, and there
are two cases: if something collapsed syntactically, or if "fail" was given
as the action on a lookup failure. These can be distinguised by looking at the
variable expand_string_forcedfail, which is TRUE in the latter case.

The skipping flag is set true when expanding a substring that isn't actually
going to be used (after "if" or "lookup") and it prevents lookups from
happening lower down.

Store usage: At start, a store block of the length of the input plus 64
is obtained. This is expanded as necessary by string_cat(), which might have to
get a new block, or might be able to expand the original. At the end of the
function we can release any store above that portion of the yield block that
was actually used. In many cases this will be optimal.

Arguments:
  string         the string to be expanded
  ket_ends       true if expansion is to stop at }
  left           if not NULL, a pointer to the first character after the
                 expansion is placed here (typically used with ket_ends)
  skipping       TRUE for recursive calls when the value isn't actually going
                 to be used (to allow for optimisation)

Returns:         NULL if expansion fails:
                   expand_string_forcedfail is set TRUE if failure was forced
                   expand_string_message contains a textual error message
                 a pointer to the expanded string on success
*/

static char *
expand_string_internal(char *string, BOOL ket_ends, char **left, BOOL skipping)
{
int ptr = 0;
int size = (int)strlen(string) + 64;
char *s = string;
char *yield = store_get(size);
char *save_expand_nstring[EXPAND_MAXN+1];
int save_expand_nlength[EXPAND_MAXN+1];

expand_string_forcedfail = FALSE;
expand_string_message = "";

while (*s != 0)
  {
  char *value;
  char name[256];

  /* \ escapes the next character, which must exist, or else
  the expansion fails. */

  if (*s == '\\')
    {
    char ch[1];
    if (s[1] == 0)
      {
      expand_string_message = "\\ at end of string";
      goto EXPAND_FAILED;
      }
    ch[0] = string_interpret_escape(&s);
    s++;
    yield = string_cat(yield, &size, &ptr, ch, 1);
    continue;
    }

  /* Anything other than $ is just copied verbatim, unless we are
  looking for a terminating } character. */

  if (ket_ends && *s == '}') break;

  if (*s != '$')
    {
    yield = string_cat(yield, &size, &ptr, s++, 1);
    continue;
    }

  /* No { after the $ - must be a plain name or a number for string
  match variable. There has to be a fudge for variables that are the
  names of header fields preceded by "$header_" because header field
  names can contain any printing characters except space and colon.
  For those that don't like typing this much, "$h_" is a synonym for
  "$header_". A non-existent header yields a NULL value; nothing is
  inserted. */

  if (isalpha((uschar)(*(++s))))
    {
    int choplen = 0;
    s = read_name(name, sizeof(name), s, "_");
    if (strncmp(name, "header_", 7) == 0 || strncmp(name, "h_", 2) == 0)
      {
      s = read_header_name(name, sizeof(name), s);
      value = find_header(name);
      choplen = 1;

      /* If we didn't find the header, and the header contains a closing brace
      characters, this may be a user error where the terminating colon
      has been omitted. Set a flag to adjust the error message. */

      if (value == NULL && strchr(name, '}') != NULL) malformed_header = TRUE;
      }
    else
      {
      value = find_variable(name, &choplen);
      if (value == NULL)
        {
        expand_string_message =
          string_sprintf("unknown variable name \"%s\"", name);
        goto EXPAND_FAILED;
        }
      }
    if (value != NULL)
      {
      int len = (int)strlen(value) - choplen;
      if (len < 0) len = 0;
      yield = string_cat(yield, &size, &ptr, value, len);
      }
    continue;
    }

  if (isdigit((uschar)*s))
    {
    int n;
    s = read_number(&n, s);
    if (n >= 0 && n <= expand_nmax)
      yield = string_cat(yield, &size, &ptr, expand_nstring[n],
        expand_nlength[n]);
    continue;
    }

  /* Otherwise, if there's no '{' after $ it's an error. */

  if (*s != '{')
    {
    expand_string_message = "$ not followed by letter, digit, or {";
    goto EXPAND_FAILED;
    }

  /* After { there can be various things, but they all start with
  an initial word, except for a number for a string match variable. */

  if (isdigit((uschar)(*(++s))))
    {
    int n;
    s = read_number(&n, s);
    if (*s++ != '}')
      {
      expand_string_message = "} expected after number";
      goto EXPAND_FAILED;
      }
    if (n >= 0 && n <= expand_nmax)
      yield = string_cat(yield, &size, &ptr, expand_nstring[n],
        expand_nlength[n]);
    continue;
    }

  if (!isalpha((uschar)*s))
    {
    expand_string_message = "letter or digit expected after ${";
    goto EXPAND_FAILED;
    }

  /* Allow "-" in names to cater for substrings with negative
  arguments. Since we are checking for known names after { this is
  OK. */

  s = read_name(name, sizeof(name), s, "_-");

  /* Handle conditionals - preserve the values of the numerical expansion
  variables in case they get changed by a regular expression match in the
  condition. If not, they retain their external settings. At the end
  of this "if" section, they get restored to their previous values. */

  if (strcmp(name, "if") == 0)
    {
    BOOL cond = FALSE;
    int save_expand_nmax =
      save_expand_strings(save_expand_nstring, save_expand_nlength);

    s = eval_condition(s, skipping? NULL : &cond);
    if (s == NULL) goto EXPAND_FAILED;  /* message already set */

    /* The handling of "yes" and "no" result strings is now in a separate
    function that is also used by ${lookup} and ${extract}. */

    switch(process_yesno(
             skipping,                     /* were previously skipping */
             cond,                         /* success/failure indicator */
             NULL,                         /* message for failure */
             lookup_value,                 /* value to reset for string2 */
             &s,                           /* input pointer */
             &yield,                       /* output pointer */
             &size,                        /* output size */
             &ptr,                         /* output current point */
             "if"))                        /* condition type */
      {
      case 1: goto EXPAND_FAILED;          /* when all is well, the */
      case 2: goto EXPAND_FAILED_CURLY;    /* returned value is 0 */
      }

    /* Restore external setting of expansion variables for continuation
    at this level. */

    restore_expand_strings(save_expand_nmax, save_expand_nstring,
      save_expand_nlength);
    continue;
    }

  /* Handle database lookups unless locked out. If "skipping" is TRUE, we are
  expanding an internal string that isn't actually going to be used. All we
  need to do is check the syntax, so don't do a lookup at all. Preserve the
  values of the numerical expansion variables in case they get changed by a
  partial lookup. If not, they retain their external settings. At the end
  of this "lookup" section, they get restored to their previous values. */

  if (strcmp(name, "lookup") == 0)
    {
    int stype;
    int pv = -1;
    int expand_setup = 0;
    char *ss, *key, *filename;
    char *lookup_errmsg = NULL;
    char *save_lookup_value = lookup_value;
    int save_expand_nmax =
      save_expand_strings(save_expand_nstring, save_expand_nlength);

    if (expand_forbid_lookup)
      {
      expand_string_message = "File lookups are not permitted";
      goto EXPAND_FAILED;
      }

    /* Get the key we are to look up for single-key+file style lookups.
    Otherwise set the key NULL pro-tem. */

    while (isspace((uschar)*s)) s++;
    if (*s == '{')
      {
      key = expand_string_internal(s+1, TRUE, &s, skipping);
      if (key == NULL) goto EXPAND_FAILED;
      if (*s++ != '}') goto EXPAND_FAILED_CURLY;
      while (isspace((uschar)*s)) s++;
      }
    else key = NULL;

    /* Find out the type of database */

    if (!isalpha((uschar)*s))
      {
      expand_string_message = "missing lookup type";
      goto EXPAND_FAILED;
      }
    s = read_name(name, sizeof(name), s, "-_");
    ss = name;
    while (isspace((uschar)*s)) s++;

    /* Handle request for partial lookup */

    if (strncmp(ss, "partial", 7) == 0)
      {
      ss += 7;
      if (isdigit ((uschar)*ss))
        {
        pv = 0;
        while (isdigit((uschar)*ss)) pv = pv*10 + *ss++ - '0';
        }
      else pv = 2;
      if (*ss++ != '-')
        {
        expand_string_message = string_sprintf("unknown lookup type \"%s\"",
          name);
        goto EXPAND_FAILED;
        }
      }

    /* If a single-key lookup type is followed by "*" it requests looking
    for a default "*" entry if all else fails. If it is followed by "*@" it
    requests replacing everying before @ by * first. These are independent of
    partial matching, but are encoded by adding 1024 and 2048 to the partial
    match value. */

    if (strncmp(s, "*@", 2) == 0)
      {
      s += 2;
      while (isspace((uschar)*s)) s++;
      pv += 1024 + 2048;
      }
    else if (*s == '*')
      {
      s++;
      while (isspace((uschar)*s)) s++;
      pv += 1024;
      }

    /* Now check for the individual search type. Only those that are actually
    in the binary are valid. */

    stype = search_findtype(ss, &expand_string_message);
    if (stype < 0) goto EXPAND_FAILED;

    /* Check that a key was provided for those lookup types that need it,
    and was not supplied for those that use the query style, and that
    "partial" was provided only for a non-query lookup. */

    if (!mac_islookup(stype, lookup_querystyle))
      {
      if (key == NULL)
        {
        expand_string_message = string_sprintf("missing {key} for single-"
          "key \"%s\" lookup", name);
        goto EXPAND_FAILED;
        }
      }
    else
      {
      if (pv >= 0)
        {
        expand_string_message = string_sprintf("\"partial\" is not permitted "
          "for lookup type \"%s\"", ss);
        goto EXPAND_FAILED;
        }

      if (key != NULL)
        {
        expand_string_message = string_sprintf("a single key was given for "
          "lookup type \"%s\", which is not a single-key lookup type", name);
        goto EXPAND_FAILED;
        }
      }

    /* Get the next string in brackets and expand it. It is the file name for
    single-key+file lookups, and the whole query otherwise. */

    if (*s != '{') goto EXPAND_FAILED_CURLY;
    filename = expand_string_internal(s+1, TRUE, &s, skipping);
    if (filename == NULL) goto EXPAND_FAILED;
    if (*s++ != '}') goto EXPAND_FAILED_CURLY;
    while (isspace((uschar)*s)) s++;

    /* If this isn't a single-key+file lookup, re-arrange the variables
    to be appropriate for the search_ functions. */

    if (key == NULL)
      {
      key = filename;
      filename = NULL;
      }

    /* If skipping, don't do the next bit - just lookup_value == NULL, as if
    the entry was not found. Note that there is no search_close() function.
    Files are left open in case of re-use. At suitable places in higher logic,
    search_tidyup() is called to tidy all open files. This can save opening
    the same file several times. However, files may also get closed when others
    are opened, if too many are open at once. The rule is that a handle should
    not be used after a second search_open().

    Request that a partial search sets up $1 and maybe $2 by passing
    expand_setup containing zero. If its value changes, reset expand_nmax,
    since new variables will have been set. Note that at the end of this
    "lookup" section, the old numeric variables are restored. */

    if (skipping)
      lookup_value = NULL;
    else
      {
      void *handle = search_open(filename, stype, 0, NULL, NULL,
        &expand_string_message);
      if (handle == NULL) goto EXPAND_FAILED;
      lookup_value = search_find(handle, filename, key, pv, &expand_setup,
        &lookup_errmsg);
      if (search_find_defer)
        {
        expand_string_message =
          string_sprintf("lookup of \"%s\" gave DEFER: %s", key, lookup_errmsg);
        goto EXPAND_FAILED;
        }
      if (expand_setup > 0) expand_nmax = expand_setup;
      }

    /* The handling of "yes" and "no" result strings is now in a separate
    function that is also used by ${if} and ${extract}. */

    switch(process_yesno(
             skipping,                     /* were previously skipping */
             lookup_value != NULL,         /* success/failure indicator */
             lookup_errmsg,                /* message for failure */
             save_lookup_value,            /* value to reset for string2 */
             &s,                           /* input pointer */
             &yield,                       /* output pointer */
             &size,                        /* output size */
             &ptr,                         /* output current point */
             "lookup"))                    /* condition type */
      {
      case 1: goto EXPAND_FAILED;          /* when all is well, the */
      case 2: goto EXPAND_FAILED_CURLY;    /* returned value is 0 */
      }

    /* Restore external setting of expansion variables for carrying on
    at this level, and continue. */

    restore_expand_strings(save_expand_nmax, save_expand_nstring,
      save_expand_nlength);
    continue;
    }

  /* If Perl support is configured, handle calling embedded perl subroutines,
  unless locked out at this time. Syntax is ${perl{sub}} or ${perl{sub}{arg}}
  or ${perl{sub}{arg1}{arg2}} or up to a maximum of EXIM_PERL_MAX_ARGS
  arguments (defined below). */

  #ifdef EXIM_PERL
  #define EXIM_PERL_MAX_ARGS 8

  if (strcmp(name, "perl") == 0)
    {
    int i = 0;
    char *sub_name;
    char *sub_arg[EXIM_PERL_MAX_ARGS + 1];
    char *new_yield;

    if (expand_forbid_perl)
      {
      expand_string_message = "Perl calls are not permitted";
      goto EXPAND_FAILED;
      }

    while (isspace((uschar)*s)) s++;
    if (*s != '{') goto EXPAND_FAILED_CURLY;
    sub_name = expand_string_internal(s+1, TRUE, &s, skipping);
    if (sub_name == NULL) goto EXPAND_FAILED;
    while (isspace((uschar)*s)) s++;
    if (*s++ != '}') goto EXPAND_FAILED_CURLY;

    while (isspace((uschar)*s)) s++;

    while (*s == '{')
      {
      if (i == EXIM_PERL_MAX_ARGS)
        {
        expand_string_message =
          string_sprintf("Too many arguments passed to Perl subroutine \"%s\" "
            "(max is %d)", sub_name, EXIM_PERL_MAX_ARGS);
        goto EXPAND_FAILED;
        }
      sub_arg[i] = expand_string_internal(s+1, TRUE, &s, skipping);
      if (sub_arg[i++] == NULL) goto EXPAND_FAILED;
      while (isspace((uschar)*s)) s++;
      if (*s++ != '}') goto EXPAND_FAILED_CURLY;
      while (isspace((uschar)*s)) s++;
      }

    if (*s++ != '}') goto EXPAND_FAILED_CURLY;
    sub_arg[i] = 0;

    /* If skipping, we don't actually do anything */

    if (skipping) continue;

    /* Start the interpreter if necessary */

    if (!opt_perl_started)
      {
      char *initerror;
      if (opt_perl_startup == NULL)
        {
        expand_string_message = "A setting of perl_startup is needed when "
          "using the Perl interpreter";
        goto EXPAND_FAILED;
        }
      DEBUG(9) debug_printf("Starting Perl interpreter\n");
      initerror = init_perl(opt_perl_startup);
      if (initerror != NULL)
        {
        expand_string_message =
          string_sprintf("error in perl_startup code: %s\n", initerror);
        goto EXPAND_FAILED;
        }
      opt_perl_started = TRUE;
      }

    /* Call the function */

    new_yield = call_perl_cat(yield, &size, &ptr, &expand_string_message,
      sub_name, sub_arg);

    /* NULL yield indicates failure; if the message pointer has been set to
    NULL, the yield was undef, indicating a forced failure. Otherwise the
    message will indicate some kind of Perl error. */

    if (new_yield == NULL)
      {
      if (expand_string_message == NULL)
        {
        expand_string_message =
          string_sprintf("Perl subroutine \"%s\" returned undef to force "
            "failure", sub_name);
        expand_string_forcedfail = TRUE;
        }
      goto EXPAND_FAILED;
      }

    /* Yield succeeded. Ensure forcedfail is unset, just in case it got
    set during a callback from Perl. */

    expand_string_forcedfail = FALSE;
    yield = new_yield;
    continue;
    }
  #endif /* EXIM_PERL */

  /* Handle character translation for "tr" */

  if (strcmp(name, "tr") == 0)
    {
    int oldptr = ptr;
    int o2m;
    char *sub[3];

    switch(read_subs(sub, 3, &s, skipping))
      {
      case 1: goto EXPAND_FAILED_CURLY;
      case 2: goto EXPAND_FAILED;
      }

    yield = string_cat(yield, &size, &ptr, sub[0], (int)strlen(sub[0]));
    o2m = (int)strlen(sub[2]) - 1;

    if (o2m >= 0) for (; oldptr < ptr; oldptr++)
      {
      char *m = strrchr(sub[1], yield[oldptr]);
      if (m != NULL)
        {
        int o = m - sub[1];
        yield[oldptr] = sub[2][(o < o2m)? o : o2m];
        }
      }

    continue;
    }

  /* Handle global substitution for "sg" - like Perl's s/xxx/yyy/g operator.
  We have to save the numerical variables and restore them afterwards. */

  if (strcmp(name, "sg") == 0)
    {
    pcre *re;
    int moffset, moffsetextra, slen;
    int roffset;
    int emptyopt;
    const char *rerror;
    char *subject;
    char *sub[3];
    int save_expand_nmax =
      save_expand_strings(save_expand_nstring, save_expand_nlength);

    switch(read_subs(sub, 3, &s, skipping))
      {
      case 1: goto EXPAND_FAILED_CURLY;
      case 2: goto EXPAND_FAILED;
      }

    /* Compile the regular expression */

    re = pcre_compile(sub[1], PCRE_COPT, &rerror, &roffset, NULL);
    if (re == NULL)
      {
      expand_string_message = string_sprintf("regular expression error in "
        "\"%s\": %s at offset %d", sub[1], rerror, roffset);
      goto EXPAND_FAILED;
      }

    /* Now run a loop to do the substitutions as often as necessary. It ends
    when there are no more matches. Take care over matches of the null string;
    do the same thing as Perl does. */

    subject = sub[0];
    slen = (int)strlen(sub[0]);
    moffset = moffsetextra = 0;
    emptyopt = 0;

    for (;;)
      {
      int ovector[3*(EXPAND_MAXN+1)];
      int n = pcre_exec(re, NULL, subject, slen, moffset + moffsetextra,
        PCRE_EOPT | emptyopt, ovector, sizeof(ovector)/sizeof(int));
      int nn;
      char *insert;

      /* No match - if we previously set PCRE_NOTEMPTY after a null match, this
      is not necessarily the end. We want to repeat the match from one
      character further along, but leaving the basic offset the same (for
      copying below). We can't be at the end of the string - that was checked
      before setting PCRE_NOTEMPTY. If PCRE_NOTEMPTY is not set, we are
      finished; copy the remaining string and end the loop. */

      if (n < 0)
        {
        if (emptyopt != 0)
          {
          moffsetextra = 1;
          emptyopt = 0;
          continue;
          }
        yield = string_cat(yield, &size, &ptr, subject+moffset, slen-moffset);
        break;
        }

      /* Match - set up for expanding the replacement. */

      if (n == 0) n = EXPAND_MAXN + 1;
      expand_nmax = 0;
      for (nn = 0; nn < n*2; nn += 2)
        {
        expand_nstring[expand_nmax] = subject + ovector[nn];
        expand_nlength[expand_nmax++] = ovector[nn+1] - ovector[nn];
        }
      expand_nmax--;

      /* Copy the characters before the match, plus the expanded insertion. */

      yield = string_cat(yield, &size, &ptr, subject + moffset,
        ovector[0] - moffset);
      insert = expand_string(sub[2]);
      if (insert == NULL) goto EXPAND_FAILED;
      yield = string_cat(yield, &size, &ptr, insert, (int)strlen(insert));

      moffset = ovector[1];
      moffsetextra = 0;
      emptyopt = 0;

      /* If we have matched an empty string, first check to see if we are at
      the end of the subject. If so, the loop is over. Otherwise, mimic
      what Perl's /g options does. This turns out to be rather cunning. First
      we set PCRE_NOTEMPTY and PCRE_ANCHORED and try the match a non-empty
      string at the same point. If this fails (picked up above) we advance to
      the next character. */

      if (ovector[0] == ovector[1])
        {
        if (ovector[0] == slen) break;
        emptyopt = PCRE_NOTEMPTY | PCRE_ANCHORED;
        }
      }

    /* All done - restore numerical variables. */

    restore_expand_strings(save_expand_nmax, save_expand_nstring,
      save_expand_nlength);
    continue;
    }

  /* Handle keyed and numbered substring extraction. If the first argument
  consists entirely of digits, then a numerical extraction is assumed. */

  if (strcmp(name, "extract") == 0)
    {
    int i;
    int j = 2;
    int field_number = -1;
    char *save_lookup_value = lookup_value;
    char *sub[3];
    int save_expand_nmax =
      save_expand_strings(save_expand_nstring, save_expand_nlength);

    /* Read the arguments */

    for (i = 0; i < j; i++)
      {
      while (isspace((uschar)*s)) s++;
      if (*s == '{')
        {
        sub[i] = expand_string_internal(s+1, TRUE, &s, skipping);
        if (sub[i] == NULL) goto EXPAND_FAILED;
        if (*s++ != '}') goto EXPAND_FAILED_CURLY;

        /* The first argument must not be empty; if it consists entirely of
        digits, this is a numerical extraction, and we expect 3 arguments. */

        if (i == 0)
          {
          int x = 0;
          char *p = sub[0];

          if (*p == 0)
            {
            expand_string_message = "first argument of \"expand\" must not "
              "be empty";
            goto EXPAND_FAILED;
            }

          while (*p != 0 && isdigit((uschar)*p)) x = x * 10 + *p++ - '0';
          if (*p == 0)
            {
            field_number = x;
            j = 3;
            }
          }
        }
      else goto EXPAND_FAILED_CURLY;
      }

    /* Extract either the numbered or the keyed substring into $value. If
    skipping, just pretend the extraction failed. */

    lookup_value = skipping? NULL : (field_number >= 0)?
      expand_gettokened(field_number, sub[1], sub[2]) :
      expand_getkeyed(sub[0], sub[1]);

    /* If no string follows, $value gets substituted; otherwise there can
    be yes/no strings, as for lookup or if. */

    switch(process_yesno(
             skipping,                     /* were previously skipping */
             lookup_value != NULL,         /* success/failure indicator */
             NULL,                         /* message for failure */
             save_lookup_value,            /* value to reset for string2 */
             &s,                           /* input pointer */
             &yield,                       /* output pointer */
             &size,                        /* output size */
             &ptr,                         /* output current point */
             "extract"))                   /* condition type */
      {
      case 1: goto EXPAND_FAILED;          /* when all is well, the */
      case 2: goto EXPAND_FAILED_CURLY;    /* returned value is 0 */
      }

    /* All done - restore numerical variables. */

    restore_expand_strings(save_expand_nmax, save_expand_nstring,
      save_expand_nlength);

    continue;
    }

  /* Handle various operations on a subsequent string */

  if (*s == ':')
    {
    char *sub = expand_string_internal(s+1, TRUE, &s, skipping);
    if (sub == NULL) goto EXPAND_FAILED;
    s++;

    /* expand expands another time */

    if (strcmp(name, "expand") == 0)
      {
      char *expanded = expand_string_internal(sub, FALSE, NULL, skipping);
      if (expanded == NULL)
        {
        expand_string_message =
          string_sprintf("internal expansion of \"%s\" failed: %s", sub,
            expand_string_message);
        goto EXPAND_FAILED;
        }
      yield = string_cat(yield, &size, &ptr, expanded, (int)strlen(expanded));
      continue;
      }

    /* lc lowercases */

    if (strcmp(name, "lc") == 0)
      {
      int count = 0;
      char *t = sub - 1;
      while (*(++t) != 0) { *t = tolower(*t); count++; }
      yield = string_cat(yield, &size, &ptr, sub, count);
      continue;
      }

    /* uc lowercases */

    if (strcmp(name, "uc") == 0)
      {
      int count = 0;
      char *t = sub - 1;
      while (*(++t) != 0) { *t = toupper(*t); count++; }
      yield = string_cat(yield, &size, &ptr, sub, count);
      continue;
      }

    /* md5 yields the md5 hash */

    if (strcmp(name, "md5") == 0)
      {
      md5 base;
      uschar digest[16];
      int j;
      char st[33];
      md5_start(&base);
      md5_end(&base, sub, (int)strlen(sub), digest);
      for(j = 0; j < 16; j++) sprintf(st+2*j, "%02x", digest[j]);
      yield = string_cat(yield, &size, &ptr, st, (int)strlen(st));
      continue;
      }

    /* mask applies a mask to an IP address; for example the result of
    ${mask:131.111.10.206/28} is 131.111.10.192/28. */

    if (strcmp(name, "mask") == 0)
      {
      int count;
      char *endptr;
      int binary[4];
      int mask, maskoffset;
      int type = string_is_ip_address(sub, &maskoffset);
      char buffer[64];

      if (type == 0)
        {
        expand_string_message = string_sprintf("\"%s\" is not an IP address",
         sub);
        goto EXPAND_FAILED;
        }

      if (maskoffset == 0)
        {
        expand_string_message = string_sprintf("missing mask value in \"%s\"",
          sub);
        goto EXPAND_FAILED;
        }

      /* The call to string_is_ip_address will have turned the '/' separator
      into a null, and pointed maskoffset there. */

      mask = strtol(sub + maskoffset + 1, &endptr, 10);

      if (*endptr != 0 || mask < 0 || mask > ((type == 4)? 32 : 128))
        {
        sub[maskoffset] = '/';
        expand_string_message = string_sprintf("mask value too big in \"%s\"",
          sub);
        goto EXPAND_FAILED;
        }

      /* Convert the address to binary integer(s) and apply the mask */

      count = host_aton(sub, binary);
      host_mask(count, binary, mask);

      /* Convert to masked textual format and add to output. */

      yield = string_cat(yield, &size, &ptr, buffer,
        host_nmtoa(count, binary, mask, buffer));
      continue;
      }

    /* local_part and domain split up an address; parse fail yields null */

    if (strcmp(name, "local_part") == 0 || strcmp(name, "domain") == 0)
      {
      char *error;
      int start, end, domain;
      char *t = parse_extract_address(sub, &error, &start, &end, &domain,
        FALSE);
      if (t != NULL)
        {
        if (name[0] == 'l')
          {
          if (domain != 0) end = start + domain - 1;
          yield = string_cat(yield, &size, &ptr, sub+start, end-start);
          }
        else if (domain != 0)
          {
          domain += start;
          yield = string_cat(yield, &size, &ptr, sub+domain, end-domain);
          }
        }
      continue;
      }

    /* quote sticks it in quotes if it contains anything other than
    alphamerics, underscore, dot, or hyphen. */

    if (strcmp(name, "quote") == 0)
      {
      BOOL needs_quote = FALSE;
      char *t = sub - 1;
      while (*(++t) != 0)
        {
        if (*t != '_' && *t != '-' && *t != '.' && !isalnum((uschar)*t))
          { needs_quote = TRUE; break; }
        }

      if (needs_quote)
        {
        yield = string_cat(yield, &size, &ptr, "\"", 1);
        t = sub - 1;
        while (*(++t) != 0)
          {
          if (*t == '\\' || *t == '"')
            yield = string_cat(yield, &size, &ptr, "\\", 1);
          yield = string_cat(yield, &size, &ptr, t, 1);
          }
        yield = string_cat(yield, &size, &ptr, "\"", 1);
        }
      else yield = string_cat(yield, &size, &ptr, sub, (int)strlen(sub));
      continue;
      }

    /* quote_xxx calls the quoting function of the xxx lookup */

    if (strncmp(name, "quote_", 6) == 0)
      {
      int n = search_findtype(name+6, &expand_string_message);
      if (n < 0) goto EXPAND_FAILED;
      if (lookup_list[n].quote != NULL) sub = (lookup_list[n].quote)(sub);
      yield = string_cat(yield, &size, &ptr, sub, (int)strlen(sub));
      continue;
      }

    /* rx quote sticks in \ before any non-alphameric character so that
    the insertion works in a regular expression. */

    if (strcmp(name, "rxquote") == 0)
      {
      char *t = sub - 1;
      while (*(++t) != 0)
        {
        if (!isalnum((uschar)*t))
          yield = string_cat(yield, &size, &ptr, "\\", 1);
        yield = string_cat(yield, &size, &ptr, t, 1);
        }
      continue;
      }

    /* escape turns all non-printing characters into escape sequences. */

    if (strcmp(name, "escape") == 0)
      {
      char *t = string_printing(sub);
      yield = string_cat(yield, &size, &ptr, t, (int)strlen(t));
      continue;
      }

    /* length_n or l_n takes just the first n characters or the whole string,
    whichever is the shorter;

    substr_m_n, and s_m_n take n characters from offset m; negative m take
    from the end; l_n is synonymous with s_0_n. If n is omitted in substr it
    takes the rest, either to the right or to the left.

    hash_n or h_n makes a hash of length n from the string, yielding n
    characters from the set a-z; hash_n_m makes a hash of length n, but
    uses m characters from the set a-zA-Z0-9.

    nhash_n returns a single number between 0 and n-1 (in text form), while
    nhash_n_m returns a div/mod hash as two numbers "a/b". The first lies
    between 0 and n-1 and the second between 0 and m-1. */

    if (strncmp(name, "length_", 7) == 0 || strncmp(name, "l_", 2) == 0 ||
        strncmp(name, "substr_", 7) == 0 || strncmp(name, "s_", 2) == 0 ||
        strncmp(name, "hash_",   5) == 0 || strncmp(name, "h_", 2) == 0 ||
        strncmp(name, "nhash_",  6) == 0 || strncmp(name, "nh_",3) == 0)
      {
      int sign = 1;
      int value1 = 0;
      int value2 = -1;
      int *pn;
      int sublen = (int)strlen(sub);
      char *num = strchr(name, '_') + 1;

      /* "length" has only one argument, effectively being synonymous with
      substr_0_n. */

      if (name[0] == 'l')
        {
        pn = &value2;
        value2 = 0;
        }

      /* The others have one or two arguments; for "substr" the first may be
      negative. The second being negative means "not supplied". */

      else
        {
        pn = &value1;
        if (name[0] == 's' && *num == '-') { sign = -1; num++; }
        }

      /* Read up to two numbers, separated by underscores */

      while (*num != 0)
        {
        if (*num == '_' && pn == &value1)
          {
          pn = &value2;
          value2 = 0;
          num++;
          }
        else if (!isdigit((uschar)*num))
          {
          expand_string_message =
            string_sprintf("non-digit after underscore in \"%s\"", name);
          goto EXPAND_FAILED;
          }
        else *pn = (*pn)*10 + *num++ - '0';
        }
      value1 *= sign;

      /* For a text hash, the first argument is the length, and the
      second is the number of characters to use, defaulting to 26. */

      if (name[0] == 'h')
        {
        if (value2 < 0) value2 = 26;
        else if (value2 > (int)strlen(hashcodes))
          {
          expand_string_message =
            string_sprintf("hash char count too big in \"%s\"", name);
          goto EXPAND_FAILED;
          }

        /* Calculate the hash text. We know it is shorter than the original
        string, so can safely place it in sub[]. */

        if (value1 < sublen)
          {
          int c;
          int i = 0;
          int j = value1;
          while ((c = (uschar)(sub[j])) != 0)
            {
            int shift = (c + j++) & 7;
            sub[i] ^= (c << shift) | (c >> (8-shift));
            if (++i >= value1) i = 0;
            }
          for (i = 0; i < value1; i++)
            sub[i] = hashcodes[(uschar)(sub[i]) % value2];
          }
        else value1 = sublen;

        yield = string_cat(yield, &size, &ptr, sub, value1);
        continue;
        }

      /* Numeric hash. The first characters of the string are treated
      as most important, and get the highest prime numbers. */

      if (name[0] == 'n')
        {
        char *s = sub;
        int i = 0;
        unsigned long int total = 0; /* no overflow */

        while (*s != 0)
          {
          if (i == 0) i = sizeof(prime)/sizeof(int) - 1;
          total += prime[i--] * (unsigned int)(*s++);
          }

        /* If value2 is unset, just compute one number */

        if (value2 < 0)
          {
          s = string_sprintf("%d", total % value1);
          }

        /* Otherwise do a div/mod hash */

        else
          {
          total = total % (value1 * value2);
          s = string_sprintf("%d/%d", total/value2, total % value2);
          }

        yield = string_cat(yield, &size, &ptr, s, (int)strlen(s));
        continue;
        }

      /* Otherwise we are handling a substring or subbit operation, with value1
      the offset and value2 the length. A negative offset positions from the
      rhs. (For "length" and "bits" the offset is always 0.) */

      if (value1 < 0)    /* substr only */
        {
        value1 += sublen;

        /* If the position is before the start, skip to the start, unless
        length is unset, in which case the substring is null. */

        if (value1 < 0)
          {
          if (value2 < 0) value2 = 0; else value2 += value1;
          value1 = 0;
          }

        /* Otherwise an unset length => characters before the value1 */

        else if (value2 < 0)
          {
          value2 = value1;
          value1 = 0;
          }
        }

      /* For a non-negative offset, no length means "rest"; just set
      it to the maximum. */

      else if (value2 < 0) value2 = sublen;

      /* For a substring operation, cut length down to maximum possible for
      the offset value, and get the required characters. */

      if (sublen < value1 + value2) value2 = sublen - value1;
      if (value2 > 0)
        yield = string_cat(yield, &size, &ptr, sub + value1, value2);
      continue;
      }

    /* Unknown operator */

    expand_string_message =
      string_sprintf("unknown expansion operator \"%s\"", name);
    goto EXPAND_FAILED;
    }

  /* Handle a plain name */

  if (*s++ == '}')
    {
    int choplen = 0;
    value = find_variable(name, &choplen);
    if (value == NULL)
      {
      expand_string_message =
        string_sprintf("unknown variable in \"${%s}\"", name);
      goto EXPAND_FAILED;
      }
    yield = string_cat(yield, &size, &ptr, value,
      (int)strlen(value) - choplen);
    continue;
    }

  /* Else there's something wrong */

  expand_string_message =
    string_sprintf("\"${%s\" is not a known operator (or a } is missing "
    "in a variable reference)", name);
  goto EXPAND_FAILED;
  }

/* If we hit the end of the string when ket_ends is set, there is a missing
terminating brace. */

if (ket_ends && *s == 0)
  {
  expand_string_message = malformed_header?
    "missing } at end of string - could be header name not terminated by colon"
    :
    "missing } at end of string";
  goto EXPAND_FAILED;
  }

/* Expansion succeeded; add a terminating zero, and if left != NULL, return a
pointer to the terminator. */

yield[ptr] = 0;
if (left != NULL) *left = s;

/* Any stacking store that was used above the final string is no longer needed.
In many cases the final string will be the one that was got at the start and so
there will be optimal store usage. */

store_reset(yield + ptr + 1);
return yield;

/* This is the failure exit: easiest to program with a goto. We still need
to update the pointer to the terminator, for cases of nested calls with "fail".
*/

EXPAND_FAILED_CURLY:
expand_string_message = malformed_header?
  "missing or misplaced { or } - could be header name not terminated by colon"
  :
  "missing or misplaced { or }";

EXPAND_FAILED:
if (left != NULL) *left = s;
store_reset(yield);
return NULL;
}


/* This is the external function call. Do a quick check for any expansion
metacharacters, and if there are none, just return the input string.

Argument: the string to be expanded
Returns:  the expanded string, or NULL if expansion failed; if failure was
          due to a lookup deferring, search_find_defer will be TRUE
*/

char *
expand_string(char *string)
{
search_find_defer = FALSE;
malformed_header = FALSE;
return (strpbrk(string, "$\\") == NULL)? string :
  expand_string_internal(string, FALSE, NULL, FALSE);
}



/*************************************************
*              Expand and copy                   *
*************************************************/

/* Now and again we want to expand a string and be sure that the result is in a
new bit of store. This function does that.

Argument: the string to be expanded
Returns:  the expanded string, always in a new bit of store, or NULL
*/

char *
expand_string_copy(char *string)
{
char *yield = expand_string(string);
if (yield == string) yield = string_copy(string);
return yield;
}



/*************************************************
*               Expand or panic                  *
*************************************************/

/* Sometimes Exim can't continue if an expansion fails. This function ensures
that it panics.

Arguments:
  string     the string to be expanded
  text1      ) two texts to be placed in the
  text2      ) error message on failure

Returns:     the successfully expanded string
*/

char *
expand_string_panic(char *string, char *text1, char *text2)
{
char *yield;
yield = expand_string(string);
if (yield == NULL)
  log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s %s: failed to expand \"%s\" %s",
    text1, text2, string, expand_string_message);
return yield;
}



/*************************************************
**************************************************
*             Stand-alone test program           *
**************************************************
*************************************************/

#ifdef STAND_ALONE


BOOL
regex_match_and_setup(pcre *re, char *subject, int options, int setup)
{
int ovector[3*(EXPAND_MAXN+1)];
int n = pcre_exec(re, NULL, subject, (int)strlen(subject), 0, PCRE_EOPT|options,
  ovector, sizeof(ovector)/sizeof(int));
BOOL yield = n >= 0;
if (n == 0) n = EXPAND_MAXN + 1;
if (yield)
  {
  int nn;
  expand_nmax = (setup < 0)? 0 : setup + 1;
  for (nn = (setup < 0)? 0 : 2; nn < n*2; nn += 2)
    {
    expand_nstring[expand_nmax] = subject + ovector[nn];
    expand_nlength[expand_nmax++] = ovector[nn+1] - ovector[nn];
    }
  expand_nmax--;
  }
return yield;
}


int main(int argc, char **argv)
{
int i;
char buffer[1024];

debug_level = 1;
debug_file = stderr;
debug_fd = fileno(debug_file);
debug_trace_memory = 0;
big_buffer = malloc(big_buffer_size);

for (i = 1; i < argc; i++)
  {
  if (argv[i][0] == '+')
    {
    debug_trace_memory = 2;
    argv[i]++;
    }
  if (isdigit(argv[i][0]))
    debug_level = atoi(argv[i]);
  else
    if (strspn(argv[i], "abcdefghijklmnopqrtsuvwxyz0123456789-.:/") ==
        (int)strlen(argv[i]))
      {
      #ifdef LOOKUP_LDAP
      ldap_default_servers = argv[i];
      #endif
      #ifdef LOOKUP_MYSQL
      mysql_servers = argv[i];
      #endif
      #ifdef LOOKUP_PGSQL
      pgsql_servers = argv[i];
      #endif
      }
  #ifdef EXIM_PERL
  else opt_perl_startup = argv[i];
  #endif
  }

printf("Testing string expansion: debug_level = %d\n\n", debug_level);

expand_nstring[1] = "string 1....";
expand_nlength[1] = 8;
expand_nmax = 1;

#ifdef EXIM_PERL
if (opt_perl_startup != NULL)
  {
  char *errstr;
  printf("Starting Perl interpreter\n");
  errstr = init_perl(opt_perl_startup);
  if (errstr != NULL)
    {
    printf("** error in perl_startup code: %s\n", errstr);
    return EXIT_FAILURE;
    }
  }
#endif /* EXIM_PERL */

while (fgets(buffer, sizeof(buffer), stdin) != NULL)
  {
  void *reset_point = store_get(0);
  char *yield = expand_string(buffer);
  if (yield != NULL)
    {
    printf("%s\n", yield);
    store_reset(reset_point);
    }
  else
    {
    if (search_find_defer) printf("search_find deferred\n");
    printf("Failed: %s\n", expand_string_message);
    if (expand_string_forcedfail) printf("Forced failure\n");
    printf("\n");
    }
  }

search_tidyup();

return 0;
}

#endif

/* End of expand.c */