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
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
|
/*++
/* NAME
/* smtpd_check 3
/* SUMMARY
/* SMTP client request filtering
/* SYNOPSIS
/* #include "smtpd.h"
/* #include "smtpd_check.h"
/*
/* void smtpd_check_init()
/*
/* int smtpd_check_addr(sender, address, smtputf8)
/* const char *sender;
/* const char *address;
/* int smtputf8;
/*
/* char *smtpd_check_rewrite(state)
/* SMTPD_STATE *state;
/*
/* char *smtpd_check_client(state)
/* SMTPD_STATE *state;
/*
/* char *smtpd_check_helo(state, helohost)
/* SMTPD_STATE *state;
/* char *helohost;
/*
/* char *smtpd_check_mail(state, sender)
/* SMTPD_STATE *state;
/* char *sender;
/*
/* char *smtpd_check_rcpt(state, recipient)
/* SMTPD_STATE *state;
/* char *recipient;
/*
/* char *smtpd_check_etrn(state, destination)
/* SMTPD_STATE *state;
/* char *destination;
/*
/* char *smtpd_check_data(state)
/* SMTPD_STATE *state;
/*
/* char *smtpd_check_eod(state)
/* SMTPD_STATE *state;
/*
/* char *smtpd_check_size(state, size)
/* SMTPD_STATE *state;
/* off_t size;
/*
/* char *smtpd_check_queue(state)
/* SMTPD_STATE *state;
/* DESCRIPTION
/* This module implements additional checks on SMTP client requests.
/* A client request is validated in the context of the session state.
/* The result is either an error response (including the numerical
/* code) or the result is a null pointer in case of success.
/*
/* smtpd_check_init() initializes. This function should be called
/* once during the process life time.
/*
/* smtpd_check_addr() sanity checks an email address and returns
/* non-zero in case of badness. The sender argument provides sender
/* context for address resolution and caching, or a null pointer
/* if information is unavailable.
/*
/* smtpd_check_rewrite() should be called before opening a queue
/* file or proxy connection, in order to establish the proper
/* header address rewriting context.
/*
/* Each of the following routines scrutinizes the argument passed to
/* an SMTP command such as HELO, MAIL FROM, RCPT TO, or scrutinizes
/* the initial client connection request. The administrator can
/* specify what restrictions apply.
/*
/* Restrictions are specified via configuration parameters named
/* \fIsmtpd_{client,helo,sender,recipient}_restrictions.\fR Each
/* configuration parameter specifies a list of zero or more
/* restrictions that are applied in the order as specified.
/* .PP
/* smtpd_check_client() validates the client host name or address.
/* Relevant configuration parameters:
/* .IP smtpd_client_restrictions
/* Restrictions on the names or addresses of clients that may connect
/* to this SMTP server.
/* .PP
/* smtpd_check_helo() validates the hostname provided with the
/* HELO/EHLO commands. Relevant configuration parameters:
/* .IP smtpd_helo_restrictions
/* Restrictions on the hostname that is sent with the HELO/EHLO
/* command.
/* .PP
/* smtpd_check_mail() validates the sender address provided with
/* a MAIL FROM request. Relevant configuration parameters:
/* .IP smtpd_sender_restrictions
/* Restrictions on the sender address that is sent with the MAIL FROM
/* command.
/* .PP
/* smtpd_check_rcpt() validates the recipient address provided
/* with an RCPT TO request. Relevant configuration parameters:
/* .IP smtpd_recipient_restrictions
/* Restrictions on the recipient address that is sent with the RCPT
/* TO command.
/* .IP local_recipient_maps
/* Tables of user names (not addresses) that exist in $mydestination.
/* Mail for local users not in these tables is rejected.
/* .PP
/* smtpd_check_etrn() validates the domain name provided with the
/* ETRN command, and other client-provided information. Relevant
/* configuration parameters:
/* .IP smtpd_etrn_restrictions
/* Restrictions on the hostname that is sent with the HELO/EHLO
/* command.
/* .PP
/* smtpd_check_size() checks if a message with the given size can
/* be received (zero means that the message size is unknown). The
/* message is rejected when
/* the message size exceeds the non-zero bound specified with the
/* \fImessage_size_limit\fR configuration parameter. This is a
/* permanent error.
/*
/* smtpd_check_queue() checks the available queue file system
/* space. The message is rejected when:
/* .IP \(bu
/* The available queue file system space is less than the amount
/* specified with the \fImin_queue_free\fR configuration parameter.
/* This is a temporary error.
/* .IP \(bu
/* The available queue file system space is less than twice the
/* message size limit. This is a temporary error.
/* .PP
/* smtpd_check_data() enforces generic restrictions after the
/* client has sent the DATA command.
/*
/* smtpd_check_eod() enforces generic restrictions after the
/* client has sent the END-OF-DATA command.
/*
/* Arguments:
/* .IP name
/* The client hostname, or \fIunknown\fR.
/* .IP addr
/* The client address.
/* .IP helohost
/* The hostname given with the HELO command.
/* .IP sender
/* The sender address given with the MAIL FROM command.
/* .IP recipient
/* The recipient address given with the RCPT TO or VRFY command.
/* .IP size
/* The message size given with the MAIL FROM command (zero if unknown).
/* BUGS
/* Policies like these should not be hard-coded in C, but should
/* be user-programmable instead.
/* SEE ALSO
/* namadr_list(3) host access control
/* domain_list(3) domain access control
/* fsspace(3) free file system space
/* LICENSE
/* .ad
/* .fi
/* The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/* Wietse Venema
/* IBM T.J. Watson Research
/* P.O. Box 704
/* Yorktown Heights, NY 10598, USA
/*
/* Wietse Venema
/* Google, Inc.
/* 111 8th Avenue
/* New York, NY 10011, USA
/*
/* TLS support originally by:
/* Lutz Jaenicke
/* BTU Cottbus
/* Allgemeine Elektrotechnik
/* Universitaetsplatz 3-4
/* D-03044 Cottbus, Germany
/*--*/
/* System library. */
#include <sys_defs.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <netdb.h>
#include <setjmp.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#ifdef STRCASECMP_IN_STRINGS_H
#include <strings.h>
#endif
/* Utility library. */
#include <msg.h>
#include <vstring.h>
#include <split_at.h>
#include <fsspace.h>
#include <stringops.h>
#include <valid_hostname.h>
#include <argv.h>
#include <mymalloc.h>
#include <dict.h>
#include <htable.h>
#include <ctable.h>
#include <mac_expand.h>
#include <attr_clnt.h>
#include <myaddrinfo.h>
#include <inet_proto.h>
#include <ip_match.h>
#include <valid_utf8_hostname.h>
#include <midna_domain.h>
#include <mynetworks.h>
/* DNS library. */
#include <dns.h>
/* Global library. */
#include <string_list.h>
#include <namadr_list.h>
#include <domain_list.h>
#include <mail_params.h>
#include <resolve_clnt.h>
#include <mail_error.h>
#include <resolve_local.h>
#include <own_inet_addr.h>
#include <mail_conf.h>
#include <maps.h>
#include <mail_addr_find.h>
#include <match_parent_style.h>
#include <strip_addr.h>
#include <cleanup_user.h>
#include <record.h>
#include <rec_type.h>
#include <mail_proto.h>
#include <mail_addr.h>
#include <verify_clnt.h>
#include <input_transp.h>
#include <is_header.h>
#include <valid_mailhost_addr.h>
#include <dsn_util.h>
#include <conv_time.h>
#include <xtext.h>
#include <smtp_stream.h>
#include <attr_override.h>
/* Application-specific. */
#include "smtpd.h"
#include "smtpd_sasl_glue.h"
#include "smtpd_check.h"
#include "smtpd_dsn_fix.h"
#include "smtpd_resolve.h"
#include "smtpd_expand.h"
/*
* Eject seat in case of parsing problems.
*/
static jmp_buf smtpd_check_buf;
/*
* Results of restrictions. Errors are negative; see dict.h.
*/
#define SMTPD_CHECK_DUNNO 0 /* indifferent */
#define SMTPD_CHECK_OK 1 /* explicitly permit */
#define SMTPD_CHECK_REJECT 2 /* explicitly reject */
/*
* Intermediate results. These are static to avoid unnecessary stress on the
* memory manager routines.
*/
static VSTRING *error_text;
static CTABLE *smtpd_rbl_cache;
static CTABLE *smtpd_rbl_byte_cache;
/*
* Pre-opened SMTP recipient maps so we can reject mail for unknown users.
* XXX This does not belong here and will eventually become part of the
* trivial-rewrite resolver.
*/
static MAPS *local_rcpt_maps;
static MAPS *send_canon_maps;
static MAPS *rcpt_canon_maps;
static MAPS *canonical_maps;
static MAPS *virt_alias_maps;
static MAPS *virt_mailbox_maps;
static MAPS *relay_rcpt_maps;
#ifdef TEST
static STRING_LIST *virt_alias_doms;
static STRING_LIST *virt_mailbox_doms;
#endif
/*
* Response templates for various rbl domains.
*/
static MAPS *rbl_reply_maps;
/*
* Pre-opened sender to login name mapping.
*/
static MAPS *smtpd_sender_login_maps;
/*
* Pre-opened access control lists.
*/
static DOMAIN_LIST *relay_domains;
static NAMADR_LIST *mynetworks_curr;
static NAMADR_LIST *mynetworks_new;
static NAMADR_LIST *perm_mx_networks;
#ifdef USE_TLS
static MAPS *relay_ccerts;
#endif
/*
* How to do parent domain wildcard matching, if any.
*/
static int access_parent_style;
/*
* Pre-parsed restriction lists.
*/
static ARGV *client_restrctions;
static ARGV *helo_restrctions;
static ARGV *mail_restrctions;
static ARGV *relay_restrctions;
static ARGV *fake_relay_restrctions;
static ARGV *rcpt_restrctions;
static ARGV *etrn_restrctions;
static ARGV *data_restrctions;
static ARGV *eod_restrictions;
static HTABLE *smtpd_rest_classes;
static HTABLE *policy_clnt_table;
static HTABLE *map_command_table;
static ARGV *local_rewrite_clients;
/*
* The routine that recursively applies restrictions.
*/
static int generic_checks(SMTPD_STATE *, ARGV *, const char *, const char *, const char *);
/*
* Recipient table check.
*/
static int check_sender_rcpt_maps(SMTPD_STATE *, const char *);
static int check_recipient_rcpt_maps(SMTPD_STATE *, const char *);
static int check_rcpt_maps(SMTPD_STATE *, const char *, const char *,
const char *);
/*
* Tempfail actions;
*/
static int unk_name_tf_act;
static int unk_addr_tf_act;
static int unv_rcpt_tf_act;
static int unv_from_tf_act;
/*
* Optional permit logging.
*/
static STRING_LIST *smtpd_acl_perm_log;
/*
* YASLM.
*/
#define STR vstring_str
#define CONST_STR(x) ((const char *) vstring_str(x))
#define UPDATE_STRING(ptr,val) { if (ptr) myfree(ptr); ptr = mystrdup(val); }
/*
* If some decision can't be made due to a temporary error, then change
* other decisions into deferrals.
*
* XXX Deferrals can be postponed only with restrictions that are based on
* client-specified information: this restricts their use to parameters
* given in HELO, MAIL FROM, RCPT TO commands.
*
* XXX Deferrals must not be postponed after client hostname lookup failure.
* The reason is that the effect of access tables may depend on whether a
* client hostname is available or not. Thus, the reject_unknown_client
* restriction must defer immediately when lookup fails, otherwise incorrect
* results happen with:
*
* reject_unknown_client, hostname-based white-list, reject
*
* XXX With warn_if_reject, don't raise the defer_if_permit flag when a
* reject-style restriction fails. Instead, log the warning for the
* resulting defer message.
*
* XXX With warn_if_reject, do raise the defer_if_reject flag when a
* permit-style restriction fails. Otherwise, we could reject legitimate
* mail.
*/
static int PRINTFLIKE(5, 6) defer_if(SMTPD_DEFER *, int, int, const char *, const char *,...);
static int PRINTFLIKE(5, 6) smtpd_check_reject(SMTPD_STATE *, int, int, const char *, const char *,...);
#define DEFER_IF_REJECT2(state, class, code, dsn, fmt, a1, a2) \
defer_if(&(state)->defer_if_reject, (class), (code), (dsn), (fmt), (a1), (a2))
#define DEFER_IF_REJECT3(state, class, code, dsn, fmt, a1, a2, a3) \
defer_if(&(state)->defer_if_reject, (class), (code), (dsn), (fmt), (a1), (a2), (a3))
#define DEFER_IF_REJECT4(state, class, code, dsn, fmt, a1, a2, a3, a4) \
defer_if(&(state)->defer_if_reject, (class), (code), (dsn), (fmt), (a1), (a2), (a3), (a4))
/*
* The following choose between DEFER_IF_PERMIT (only if warn_if_reject is
* turned off) and plain DEFER. See tempfail_actions[] below for the mapping
* from names to numeric action code.
*/
#define DEFER_ALL_ACT 0
#define DEFER_IF_PERMIT_ACT 1
#define DEFER_IF_PERMIT2(type, state, class, code, dsn, fmt, a1, a2) \
(((state)->warn_if_reject == 0 && (type) != 0) ? \
defer_if(&(state)->defer_if_permit, (class), (code), (dsn), (fmt), (a1), (a2)) \
: \
smtpd_check_reject((state), (class), (code), (dsn), (fmt), (a1), (a2)))
#define DEFER_IF_PERMIT3(type, state, class, code, dsn, fmt, a1, a2, a3) \
(((state)->warn_if_reject == 0 && (type) != 0) ? \
defer_if(&(state)->defer_if_permit, (class), (code), (dsn), (fmt), (a1), (a2), (a3)) \
: \
smtpd_check_reject((state), (class), (code), (dsn), (fmt), (a1), (a2), (a3)))
#define DEFER_IF_PERMIT4(type, state, class, code, dsn, fmt, a1, a2, a3, a4) \
(((state)->warn_if_reject == 0 && (type) != 0) ? \
defer_if(&(state)->defer_if_permit, (class), (code), (dsn), (fmt), (a1), (a2), (a3), (a4)) \
: \
smtpd_check_reject((state), (class), (code), (dsn), (fmt), (a1), (a2), (a3), (a4)))
/*
* Cached RBL lookup state.
*/
typedef struct {
char *txt; /* TXT content or NULL */
DNS_RR *a; /* A records */
} SMTPD_RBL_STATE;
static void *rbl_pagein(const char *, void *);
static void rbl_pageout(void *, void *);
static void *rbl_byte_pagein(const char *, void *);
static void rbl_byte_pageout(void *, void *);
/*
* Context for RBL $name expansion.
*/
typedef struct {
SMTPD_STATE *state; /* general state */
char *domain; /* query domain */
const char *what; /* rejected value */
const char *class; /* name of rejected value */
const char *txt; /* randomly selected trimmed TXT rr */
} SMTPD_RBL_EXPAND_CONTEXT;
/*
* Multiplication factor for free space check. Free space must be at least
* smtpd_space_multf * message_size_limit.
*/
double smtpd_space_multf = 1.5;
/*
* SMTPD policy client. Most attributes are ATTR_CLNT attributes.
*/
typedef struct {
ATTR_CLNT *client; /* client handle */
char *def_action; /* default action */
char *policy_context; /* context of policy request */
} SMTPD_POLICY_CLNT;
/*
* Table-driven parsing of main.cf parameter overrides for specific policy
* clients. We derive the override names from the corresponding main.cf
* parameter names by skipping the redundant "smtpd_policy_service_" prefix.
*/
static ATTR_OVER_TIME time_table[] = {
21 + (const char *) VAR_SMTPD_POLICY_TMOUT, DEF_SMTPD_POLICY_TMOUT, 0, 1, 0,
21 + (const char *) VAR_SMTPD_POLICY_IDLE, DEF_SMTPD_POLICY_IDLE, 0, 1, 0,
21 + (const char *) VAR_SMTPD_POLICY_TTL, DEF_SMTPD_POLICY_TTL, 0, 1, 0,
21 + (const char *) VAR_SMTPD_POLICY_TRY_DELAY, DEF_SMTPD_POLICY_TRY_DELAY, 0, 1, 0,
0,
};
static ATTR_OVER_INT int_table[] = {
21 + (const char *) VAR_SMTPD_POLICY_REQ_LIMIT, 0, 0, 0,
21 + (const char *) VAR_SMTPD_POLICY_TRY_LIMIT, 0, 1, 0,
0,
};
static ATTR_OVER_STR str_table[] = {
21 + (const char *) VAR_SMTPD_POLICY_DEF_ACTION, 0, 1, 0,
21 + (const char *) VAR_SMTPD_POLICY_CONTEXT, 0, 1, 0,
0,
};
#define link_override_table_to_variable(table, var) \
do { table[var##_offset].target = &var; } while (0)
#define smtpd_policy_tmout_offset 0
#define smtpd_policy_idle_offset 1
#define smtpd_policy_ttl_offset 2
#define smtpd_policy_try_delay_offset 3
#define smtpd_policy_req_limit_offset 0
#define smtpd_policy_try_limit_offset 1
#define smtpd_policy_def_action_offset 0
#define smtpd_policy_context_offset 1
/* policy_client_register - register policy service endpoint */
static void policy_client_register(const char *name)
{
static const char myname[] = "policy_client_register";
SMTPD_POLICY_CLNT *policy_client;
char *saved_name = 0;
const char *policy_name = 0;
char *cp;
const char *sep = CHARS_COMMA_SP;
const char *parens = CHARS_BRACE;
char *err;
if (policy_clnt_table == 0)
policy_clnt_table = htable_create(1);
if (htable_find(policy_clnt_table, name) == 0) {
/*
* Allow per-service overrides for main.cf global settings.
*/
int smtpd_policy_tmout = var_smtpd_policy_tmout;
int smtpd_policy_idle = var_smtpd_policy_idle;
int smtpd_policy_ttl = var_smtpd_policy_ttl;
int smtpd_policy_try_delay = var_smtpd_policy_try_delay;
int smtpd_policy_req_limit = var_smtpd_policy_req_limit;
int smtpd_policy_try_limit = var_smtpd_policy_try_limit;
const char *smtpd_policy_def_action = var_smtpd_policy_def_action;
const char *smtpd_policy_context = var_smtpd_policy_context;
link_override_table_to_variable(time_table, smtpd_policy_tmout);
link_override_table_to_variable(time_table, smtpd_policy_idle);
link_override_table_to_variable(time_table, smtpd_policy_ttl);
link_override_table_to_variable(time_table, smtpd_policy_try_delay);
link_override_table_to_variable(int_table, smtpd_policy_req_limit);
link_override_table_to_variable(int_table, smtpd_policy_try_limit);
link_override_table_to_variable(str_table, smtpd_policy_def_action);
link_override_table_to_variable(str_table, smtpd_policy_context);
if (*name == parens[0]) {
cp = saved_name = mystrdup(name);
if ((err = extpar(&cp, parens, EXTPAR_FLAG_NONE)) != 0)
msg_fatal("policy service syntax error: %s", cp);
if ((policy_name = mystrtok(&cp, sep)) == 0)
msg_fatal("empty policy service: \"%s\"", name);
attr_override(cp, sep, parens,
CA_ATTR_OVER_TIME_TABLE(time_table),
CA_ATTR_OVER_INT_TABLE(int_table),
CA_ATTR_OVER_STR_TABLE(str_table),
CA_ATTR_OVER_END);
} else {
policy_name = name;
}
if (msg_verbose)
msg_info("%s: name=\"%s\" default_action=\"%s\" max_idle=%d "
"max_ttl=%d request_limit=%d retry_delay=%d "
"timeout=%d try_limit=%d policy_context=\"%s\"",
myname, policy_name, smtpd_policy_def_action,
smtpd_policy_idle, smtpd_policy_ttl,
smtpd_policy_req_limit, smtpd_policy_try_delay,
smtpd_policy_tmout, smtpd_policy_try_limit,
smtpd_policy_context);
/*
* Create the client.
*/
policy_client = (SMTPD_POLICY_CLNT *) mymalloc(sizeof(*policy_client));
policy_client->client = attr_clnt_create(policy_name,
smtpd_policy_tmout,
smtpd_policy_idle,
smtpd_policy_ttl);
attr_clnt_control(policy_client->client,
ATTR_CLNT_CTL_REQ_LIMIT, smtpd_policy_req_limit,
ATTR_CLNT_CTL_TRY_LIMIT, smtpd_policy_try_limit,
ATTR_CLNT_CTL_TRY_DELAY, smtpd_policy_try_delay,
ATTR_CLNT_CTL_END);
policy_client->def_action = mystrdup(smtpd_policy_def_action);
policy_client->policy_context = mystrdup(smtpd_policy_context);
htable_enter(policy_clnt_table, name, (void *) policy_client);
if (saved_name)
myfree(saved_name);
}
}
/* command_map_register - register access table for maps lookup */
static void command_map_register(const char *name)
{
MAPS *maps;
if (map_command_table == 0)
map_command_table = htable_create(1);
if (htable_find(map_command_table, name) == 0) {
maps = maps_create(name, name, DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX
| DICT_FLAG_UTF8_REQUEST);
(void) htable_enter(map_command_table, name, (void *) maps);
}
}
/* smtpd_check_parse - pre-parse restrictions */
static ARGV *smtpd_check_parse(int flags, const char *checks)
{
char *saved_checks = mystrdup(checks);
ARGV *argv = argv_alloc(1);
char *bp = saved_checks;
char *name;
char *last = 0;
/*
* Pre-parse the restriction list, and open any dictionaries that we
* encounter. Dictionaries must be opened before entering the chroot
* jail.
*/
#define SMTPD_CHECK_PARSE_POLICY (1<<0)
#define SMTPD_CHECK_PARSE_MAPS (1<<1)
#define SMTPD_CHECK_PARSE_ALL (~0)
while ((name = mystrtokq(&bp, CHARS_COMMA_SP, CHARS_BRACE)) != 0) {
argv_add(argv, name, (char *) 0);
if ((flags & SMTPD_CHECK_PARSE_POLICY)
&& last && strcasecmp(last, CHECK_POLICY_SERVICE) == 0)
policy_client_register(name);
else if ((flags & SMTPD_CHECK_PARSE_MAPS) && strchr(name, ':') != 0) {
command_map_register(name);
}
last = name;
}
argv_terminate(argv);
/*
* Cleanup.
*/
myfree(saved_checks);
return (argv);
}
#ifndef TEST
/* has_required - make sure required restriction is present */
static int has_required(ARGV *restrictions, const char **required)
{
char **rest;
const char **reqd;
ARGV *expansion;
/*
* Recursively check list membership.
*/
for (rest = restrictions->argv; *rest; rest++) {
if (strcasecmp(*rest, WARN_IF_REJECT) == 0 && rest[1] != 0) {
rest += 1;
continue;
}
if (strcasecmp(*rest, PERMIT_ALL) == 0) {
if (rest[1] != 0)
msg_warn("restriction `%s' after `%s' is ignored",
rest[1], rest[0]);
return (0);
}
for (reqd = required; *reqd; reqd++)
if (strcasecmp(*rest, *reqd) == 0)
return (1);
/* XXX This lookup operation should not be case-sensitive. */
if ((expansion = (ARGV *) htable_find(smtpd_rest_classes, *rest)) != 0)
if (has_required(expansion, required))
return (1);
}
return (0);
}
/* fail_required - handle failure to use required restriction */
static void fail_required(const char *name, const char **required)
{
const char *myname = "fail_required";
const char **reqd;
VSTRING *example;
/*
* Sanity check.
*/
if (required[0] == 0)
msg_panic("%s: null required list", myname);
/*
* Go bust.
*/
example = vstring_alloc(10);
for (reqd = required; *reqd; reqd++)
vstring_sprintf_append(example, "%s%s", *reqd,
reqd[1] == 0 ? "" : reqd[2] == 0 ? " or " : ", ");
msg_fatal("in parameter %s, specify at least one working instance of: %s",
name, STR(example));
}
#endif
/* smtpd_check_init - initialize once during process lifetime */
void smtpd_check_init(void)
{
char *saved_classes;
const char *name;
const char *value;
char *cp;
#ifndef TEST
static const char *rcpt_required[] = {
REJECT_UNAUTH_DEST,
DEFER_UNAUTH_DEST,
REJECT_ALL,
DEFER_ALL,
DEFER_IF_PERMIT,
CHECK_RELAY_DOMAINS,
0,
};
#endif
static NAME_CODE tempfail_actions[] = {
DEFER_ALL, DEFER_ALL_ACT,
DEFER_IF_PERMIT, DEFER_IF_PERMIT_ACT,
0, -1,
};
/*
* Pre-open access control lists before going to jail.
*/
mynetworks_curr =
namadr_list_init(VAR_MYNETWORKS, MATCH_FLAG_RETURN
| match_parent_style(VAR_MYNETWORKS), var_mynetworks);
mynetworks_new =
namadr_list_init(VAR_MYNETWORKS, MATCH_FLAG_RETURN
| match_parent_style(VAR_MYNETWORKS), mynetworks_host());
relay_domains =
domain_list_init(VAR_RELAY_DOMAINS,
match_parent_style(VAR_RELAY_DOMAINS),
var_relay_domains);
perm_mx_networks =
namadr_list_init(VAR_PERM_MX_NETWORKS, MATCH_FLAG_RETURN
| match_parent_style(VAR_PERM_MX_NETWORKS),
var_perm_mx_networks);
#ifdef USE_TLS
relay_ccerts = maps_create(VAR_RELAY_CCERTS, var_smtpd_relay_ccerts,
DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX);
#endif
/*
* Pre-parse and pre-open the recipient maps.
*/
local_rcpt_maps = maps_create(VAR_LOCAL_RCPT_MAPS, var_local_rcpt_maps,
DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX
| DICT_FLAG_UTF8_REQUEST);
send_canon_maps = maps_create(VAR_SEND_CANON_MAPS, var_send_canon_maps,
DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX
| DICT_FLAG_UTF8_REQUEST);
rcpt_canon_maps = maps_create(VAR_RCPT_CANON_MAPS, var_rcpt_canon_maps,
DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX
| DICT_FLAG_UTF8_REQUEST);
canonical_maps = maps_create(VAR_CANONICAL_MAPS, var_canonical_maps,
DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX
| DICT_FLAG_UTF8_REQUEST);
virt_alias_maps = maps_create(VAR_VIRT_ALIAS_MAPS, var_virt_alias_maps,
DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX
| DICT_FLAG_UTF8_REQUEST);
virt_mailbox_maps = maps_create(VAR_VIRT_MAILBOX_MAPS,
var_virt_mailbox_maps,
DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX
| DICT_FLAG_UTF8_REQUEST);
relay_rcpt_maps = maps_create(VAR_RELAY_RCPT_MAPS, var_relay_rcpt_maps,
DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX
| DICT_FLAG_UTF8_REQUEST);
#ifdef TEST
virt_alias_doms = string_list_init(VAR_VIRT_ALIAS_DOMS, MATCH_FLAG_NONE,
var_virt_alias_doms);
virt_mailbox_doms = string_list_init(VAR_VIRT_MAILBOX_DOMS, MATCH_FLAG_NONE,
var_virt_mailbox_doms);
#endif
access_parent_style = match_parent_style(SMTPD_ACCESS_MAPS);
/*
* Templates for RBL rejection replies.
*/
rbl_reply_maps = maps_create(VAR_RBL_REPLY_MAPS, var_rbl_reply_maps,
DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX
| DICT_FLAG_UTF8_REQUEST);
/*
* Sender to login name mapping.
*/
smtpd_sender_login_maps = maps_create(VAR_SMTPD_SND_AUTH_MAPS,
var_smtpd_snd_auth_maps,
DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX
| DICT_FLAG_UTF8_REQUEST);
/*
* error_text is used for returning error responses.
*/
error_text = vstring_alloc(10);
/*
* Initialize the resolved address cache. Note: the cache persists across
* SMTP sessions so we cannot make it dependent on session state.
*/
smtpd_resolve_init(100);
/*
* Initialize the RBL lookup cache. Note: the cache persists across SMTP
* sessions so we cannot make it dependent on session state.
*/
smtpd_rbl_cache = ctable_create(100, rbl_pagein, rbl_pageout, (void *) 0);
smtpd_rbl_byte_cache = ctable_create(1000, rbl_byte_pagein,
rbl_byte_pageout, (void *) 0);
/*
* Pre-parse the restriction lists. At the same time, pre-open tables
* before going to jail.
*/
client_restrctions = smtpd_check_parse(SMTPD_CHECK_PARSE_ALL,
var_client_checks);
helo_restrctions = smtpd_check_parse(SMTPD_CHECK_PARSE_ALL,
var_helo_checks);
mail_restrctions = smtpd_check_parse(SMTPD_CHECK_PARSE_ALL,
var_mail_checks);
relay_restrctions = smtpd_check_parse(SMTPD_CHECK_PARSE_ALL,
var_relay_checks);
if (warn_compat_break_relay_restrictions)
fake_relay_restrctions = smtpd_check_parse(SMTPD_CHECK_PARSE_ALL,
FAKE_RELAY_CHECKS);
rcpt_restrctions = smtpd_check_parse(SMTPD_CHECK_PARSE_ALL,
var_rcpt_checks);
etrn_restrctions = smtpd_check_parse(SMTPD_CHECK_PARSE_ALL,
var_etrn_checks);
data_restrctions = smtpd_check_parse(SMTPD_CHECK_PARSE_ALL,
var_data_checks);
eod_restrictions = smtpd_check_parse(SMTPD_CHECK_PARSE_ALL,
var_eod_checks);
/*
* Parse the pre-defined restriction classes.
*/
smtpd_rest_classes = htable_create(1);
if (*var_rest_classes) {
cp = saved_classes = mystrdup(var_rest_classes);
while ((name = mystrtok(&cp, CHARS_COMMA_SP)) != 0) {
if ((value = mail_conf_lookup_eval(name)) == 0 || *value == 0)
msg_fatal("restriction class `%s' needs a definition", name);
/* XXX This store operation should not be case-sensitive. */
htable_enter(smtpd_rest_classes, name,
(void *) smtpd_check_parse(SMTPD_CHECK_PARSE_ALL,
value));
}
myfree(saved_classes);
}
/*
* This is the place to specify definitions for complex restrictions such
* as check_relay_domains in terms of more elementary restrictions.
*/
#if 0
htable_enter(smtpd_rest_classes, "check_relay_domains",
smtpd_check_parse(SMTPD_CHECK_PARSE_ALL,
"permit_mydomain reject_unauth_destination"));
#endif
htable_enter(smtpd_rest_classes, REJECT_SENDER_LOGIN_MISMATCH,
(void *) smtpd_check_parse(SMTPD_CHECK_PARSE_ALL,
REJECT_AUTH_SENDER_LOGIN_MISMATCH
" " REJECT_UNAUTH_SENDER_LOGIN_MISMATCH));
/*
* People screw up the relay restrictions too often. Require that they
* list at least one restriction that rejects mail by default. We allow
* relay restrictions to be empty for sites that require backwards
* compatibility.
*/
#ifndef TEST
if (!has_required(rcpt_restrctions, rcpt_required)
&& !has_required(relay_restrctions, rcpt_required))
fail_required(VAR_RELAY_CHECKS " or " VAR_RCPT_CHECKS, rcpt_required);
#endif
/*
* Local rewrite policy.
*/
local_rewrite_clients = smtpd_check_parse(SMTPD_CHECK_PARSE_MAPS,
var_local_rwr_clients);
/*
* Tempfail_actions.
*
* XXX This name-to-number mapping should be encapsulated in a separate
* mail_conf_name_code.c module.
*/
if ((unk_name_tf_act = name_code(tempfail_actions, NAME_CODE_FLAG_NONE,
var_unk_name_tf_act)) < 0)
msg_fatal("bad configuration: %s = %s",
VAR_UNK_NAME_TF_ACT, var_unk_name_tf_act);
if ((unk_addr_tf_act = name_code(tempfail_actions, NAME_CODE_FLAG_NONE,
var_unk_addr_tf_act)) < 0)
msg_fatal("bad configuration: %s = %s",
VAR_UNK_ADDR_TF_ACT, var_unk_addr_tf_act);
if ((unv_rcpt_tf_act = name_code(tempfail_actions, NAME_CODE_FLAG_NONE,
var_unv_rcpt_tf_act)) < 0)
msg_fatal("bad configuration: %s = %s",
VAR_UNV_RCPT_TF_ACT, var_unv_rcpt_tf_act);
if ((unv_from_tf_act = name_code(tempfail_actions, NAME_CODE_FLAG_NONE,
var_unv_from_tf_act)) < 0)
msg_fatal("bad configuration: %s = %s",
VAR_UNV_FROM_TF_ACT, var_unv_from_tf_act);
if (msg_verbose) {
msg_info("%s = %s", VAR_UNK_NAME_TF_ACT, tempfail_actions[unk_name_tf_act].name);
msg_info("%s = %s", VAR_UNK_ADDR_TF_ACT, tempfail_actions[unk_addr_tf_act].name);
msg_info("%s = %s", VAR_UNV_RCPT_TF_ACT, tempfail_actions[unv_rcpt_tf_act].name);
msg_info("%s = %s", VAR_UNV_FROM_TF_ACT, tempfail_actions[unv_from_tf_act].name);
}
/*
* Optional permit logging.
*/
smtpd_acl_perm_log = string_list_init(VAR_SMTPD_ACL_PERM_LOG,
MATCH_FLAG_RETURN,
var_smtpd_acl_perm_log);
}
/* log_whatsup - log as much context as we have */
static void log_whatsup(SMTPD_STATE *state, const char *whatsup,
const char *text)
{
VSTRING *buf = vstring_alloc(100);
vstring_sprintf(buf, "%s: %s: %s from %s: %s;",
state->queue_id ? state->queue_id : "NOQUEUE",
whatsup, state->where, state->namaddr, text);
if (state->sender)
vstring_sprintf_append(buf, " from=<%s>", state->sender);
if (state->recipient)
vstring_sprintf_append(buf, " to=<%s>", state->recipient);
if (state->protocol)
vstring_sprintf_append(buf, " proto=%s", state->protocol);
if (state->helo_name)
vstring_sprintf_append(buf, " helo=<%s>", state->helo_name);
msg_info("%s", STR(buf));
vstring_free(buf);
}
/* smtpd_acl_permit - permit request with optional logging */
static int PRINTFLIKE(5, 6) smtpd_acl_permit(SMTPD_STATE *state,
const char *action,
const char *reply_class,
const char *reply_name,
const char *format,...)
{
const char myname[] = "smtpd_acl_permit";
va_list ap;
const char *whatsup;
#ifdef notdef
#define NO_PRINT_ARGS ""
#else
#define NO_PRINT_ARGS "%s", ""
#endif
/*
* First, find out if (and how) this permit action should be logged.
*/
if (msg_verbose)
msg_info("%s: checking %s settings", myname, VAR_SMTPD_ACL_PERM_LOG);
if (state->defer_if_permit.active) {
/* This action is overruled. Do not log. */
whatsup = 0;
} else if (string_list_match(smtpd_acl_perm_log, action) != 0) {
/* This is not a test. Logging is enabled. */
whatsup = "permit";
} else {
/* This is not a test. Logging is disabled. */
whatsup = 0;
}
if (whatsup != 0) {
vstring_sprintf(error_text, "action=%s for %s=%s",
action, reply_class, reply_name);
if (format && *format) {
vstring_strcat(error_text, " ");
va_start(ap, format);
vstring_vsprintf_append(error_text, format, ap);
va_end(ap);
}
log_whatsup(state, whatsup, STR(error_text));
} else {
if (msg_verbose)
msg_info("%s: %s: no match", myname, VAR_SMTPD_ACL_PERM_LOG);
}
return (SMTPD_CHECK_OK);
}
/* smtpd_check_reject - do the boring things that must be done */
static int smtpd_check_reject(SMTPD_STATE *state, int error_class,
int code, const char *dsn,
const char *format,...)
{
va_list ap;
int warn_if_reject;
const char *whatsup;
/*
* Do not reject mail if we were asked to warn only. However,
* configuration/software/data errors cannot be converted into warnings.
*/
if (state->warn_if_reject && error_class != MAIL_ERROR_SOFTWARE
&& error_class != MAIL_ERROR_RESOURCE
&& error_class != MAIL_ERROR_DATA) {
warn_if_reject = 1;
whatsup = "reject_warning";
} else {
warn_if_reject = 0;
whatsup = "reject";
}
/*
* Update the error class mask, and format the response. XXX What about
* multi-line responses? For now we cheat and send whitespace.
*
* Format the response before complaining about configuration errors, so
* that we can show the error in context.
*/
state->error_mask |= error_class;
vstring_sprintf(error_text, "%d %s ", code, dsn);
va_start(ap, format);
vstring_vsprintf_append(error_text, format, ap);
va_end(ap);
/*
* Validate the response, that is, the response must begin with a
* three-digit status code, and the first digit must be 4 or 5. If the
* response is bad, log a warning and send a generic response instead.
*/
if (code < 400 || code > 599) {
msg_warn("SMTP reply code configuration error: %s", STR(error_text));
vstring_strcpy(error_text, "450 4.7.1 Service unavailable");
}
if (!dsn_valid(STR(error_text) + 4)) {
msg_warn("DSN detail code configuration error: %s", STR(error_text));
vstring_strcpy(error_text, "450 4.7.1 Service unavailable");
}
/*
* Ensure RFC compliance. We could do this inside smtpd_chat_reply() and
* switch to multi-line for long replies.
*/
vstring_truncate(error_text, 510);
printable(STR(error_text), ' ');
/*
* Force this rejection into deferral because of some earlier temporary
* error that may have prevented us from accepting mail, and report the
* earlier problem instead.
*/
if (!warn_if_reject && state->defer_if_reject.active && STR(error_text)[0] == '5') {
state->warn_if_reject = state->defer_if_reject.active = 0;
return (smtpd_check_reject(state, state->defer_if_reject.class,
state->defer_if_reject.code,
STR(state->defer_if_reject.dsn),
"%s", STR(state->defer_if_reject.reason)));
}
/*
* Soft bounce safety net.
*
* XXX The code below also appears in the Postfix SMTP server reply output
* routine. It is duplicated here in order to avoid discrepancies between
* the reply codes that are shown in "reject" logging and the reply codes
* that are actually sent to the SMTP client.
*
* Implementing the soft_bounce safety net in the SMTP server reply output
* routine has the advantage that it covers all 5xx replies, including
* SMTP protocol or syntax errors, which makes soft_bounce great for
* non-destructive tests (especially by people who are paranoid about
* losing mail).
*
* We could eliminate the code duplication and implement the soft_bounce
* safety net only in the code below. But then the safety net would cover
* the UCE restrictions only. This would be at odds with documentation
* which says soft_bounce changes all 5xx replies into 4xx ones.
*/
if (var_soft_bounce && STR(error_text)[0] == '5')
STR(error_text)[0] = '4';
/*
* In any case, enforce consistency between the SMTP code and DSN code.
* SMTP has the higher precedence since it came here first.
*/
STR(error_text)[4] = STR(error_text)[0];
/*
* Log what is happening. When the sysadmin discards policy violation
* postmaster notices, this may be the only trace left that service was
* rejected. Print the request, client name/address, and response.
*/
log_whatsup(state, whatsup, STR(error_text));
return (warn_if_reject ? 0 : SMTPD_CHECK_REJECT);
}
/* defer_if - prepare to change our mind */
static int defer_if(SMTPD_DEFER *defer, int error_class,
int code, const char *dsn,
const char *fmt,...)
{
va_list ap;
/*
* Keep the first reason for this type of deferral, to minimize
* confusion.
*/
if (defer->active == 0) {
defer->active = 1;
defer->class = error_class;
defer->code = code;
if (defer->dsn == 0)
defer->dsn = vstring_alloc(10);
vstring_strcpy(defer->dsn, dsn);
if (defer->reason == 0)
defer->reason = vstring_alloc(10);
va_start(ap, fmt);
vstring_vsprintf(defer->reason, fmt, ap);
va_end(ap);
}
return (SMTPD_CHECK_DUNNO);
}
/* reject_dict_retry - reject with temporary failure if dict lookup fails */
static NORETURN reject_dict_retry(SMTPD_STATE *state, const char *reply_name)
{
longjmp(smtpd_check_buf, smtpd_check_reject(state, MAIL_ERROR_DATA,
451, "4.3.0",
"<%s>: Temporary lookup failure",
reply_name));
}
/* reject_server_error - reject with temporary failure after non-dict error */
static NORETURN reject_server_error(SMTPD_STATE *state)
{
longjmp(smtpd_check_buf, smtpd_check_reject(state, MAIL_ERROR_SOFTWARE,
451, "4.3.5",
"Server configuration error"));
}
/* check_mail_addr_find - reject with temporary failure if dict lookup fails */
static const char *check_mail_addr_find(SMTPD_STATE *state,
const char *reply_name,
MAPS *maps, const char *key,
char **ext)
{
const char *result;
if ((result = mail_addr_find(maps, key, ext)) != 0 || maps->error == 0)
return (result);
if (maps->error == DICT_ERR_RETRY)
/* Warning is already logged. */
reject_dict_retry(state, reply_name);
else
reject_server_error(state);
}
/* reject_unknown_reverse_name - fail if reverse client hostname is unknown */
static int reject_unknown_reverse_name(SMTPD_STATE *state)
{
const char *myname = "reject_unknown_reverse_name";
if (msg_verbose)
msg_info("%s: %s", myname, state->reverse_name);
if (state->reverse_name_status != SMTPD_PEER_CODE_OK)
return (smtpd_check_reject(state, MAIL_ERROR_POLICY,
state->reverse_name_status == SMTPD_PEER_CODE_PERM ?
var_unk_client_code : 450, "4.7.1",
"Client host rejected: cannot find your reverse hostname, [%s]",
state->addr));
return (SMTPD_CHECK_DUNNO);
}
/* reject_unknown_client - fail if client hostname is unknown */
static int reject_unknown_client(SMTPD_STATE *state)
{
const char *myname = "reject_unknown_client";
if (msg_verbose)
msg_info("%s: %s %s", myname, state->name, state->addr);
/* RFC 7372: Email Authentication Status Codes. */
if (state->name_status != SMTPD_PEER_CODE_OK)
return (smtpd_check_reject(state, MAIL_ERROR_POLICY,
state->name_status >= SMTPD_PEER_CODE_PERM ?
var_unk_client_code : 450, "4.7.25",
"Client host rejected: cannot find your hostname, [%s]",
state->addr));
return (SMTPD_CHECK_DUNNO);
}
/* reject_plaintext_session - fail if session is not encrypted */
static int reject_plaintext_session(SMTPD_STATE *state)
{
const char *myname = "reject_plaintext_session";
if (msg_verbose)
msg_info("%s: %s %s", myname, state->name, state->addr);
#ifdef USE_TLS
if (state->tls_context == 0)
#endif
return (smtpd_check_reject(state, MAIL_ERROR_POLICY,
var_plaintext_code, "4.7.1",
"Session encryption is required"));
return (SMTPD_CHECK_DUNNO);
}
/* permit_inet_interfaces - succeed if client my own address */
static int permit_inet_interfaces(SMTPD_STATE *state)
{
const char *myname = "permit_inet_interfaces";
if (msg_verbose)
msg_info("%s: %s %s", myname, state->name, state->addr);
if (own_inet_addr((struct sockaddr *) &(state->sockaddr)))
/* Permit logging in generic_checks() only. */
return (SMTPD_CHECK_OK);
return (SMTPD_CHECK_DUNNO);
}
/* permit_mynetworks - succeed if client is in a trusted network */
static int permit_mynetworks(SMTPD_STATE *state)
{
const char *myname = "permit_mynetworks";
if (msg_verbose)
msg_info("%s: %s %s", myname, state->name, state->addr);
if (namadr_list_match(mynetworks_curr, state->name, state->addr)) {
if (warn_compat_break_mynetworks_style
&& !namadr_list_match(mynetworks_new, state->name, state->addr))
msg_info("using backwards-compatible default setting "
VAR_MYNETWORKS_STYLE "=%s to permit request from "
"client \"%s\"", var_mynetworks_style, state->namaddr);
/* Permit logging in generic_checks() only. */
return (SMTPD_CHECK_OK);
} else if (mynetworks_curr->error == 0)
return (SMTPD_CHECK_DUNNO);
else
return (mynetworks_curr->error);
}
/* dup_if_truncate - save hostname and truncate if it ends in dot */
static char *dup_if_truncate(char *name)
{
ssize_t len;
char *result;
/*
* Truncate hostnames ending in dot but not dot-dot.
*
* XXX This should not be distributed all over the code. Problem is,
* addresses can enter the system via multiple paths: networks, local
* forward/alias/include files, even as the result of address rewriting.
*/
if ((len = strlen(name)) > 1
&& name[len - 1] == '.'
&& name[len - 2] != '.') {
result = mystrndup(name, len - 1);
} else
result = name;
return (result);
}
/* reject_invalid_hostaddr - fail if host address is incorrect */
static int reject_invalid_hostaddr(SMTPD_STATE *state, char *addr,
char *reply_name, char *reply_class)
{
const char *myname = "reject_invalid_hostaddr";
ssize_t len;
char *test_addr;
int stat;
if (msg_verbose)
msg_info("%s: %s", myname, addr);
if (addr[0] == '[' && (len = strlen(addr)) > 2 && addr[len - 1] == ']') {
test_addr = mystrndup(addr + 1, len - 2);
} else
test_addr = addr;
/*
* Validate the address.
*/
if (!valid_mailhost_addr(test_addr, DONT_GRIPE))
stat = smtpd_check_reject(state, MAIL_ERROR_POLICY,
var_bad_name_code, "5.5.2",
"<%s>: %s rejected: invalid ip address",
reply_name, reply_class);
else
stat = SMTPD_CHECK_DUNNO;
/*
* Cleanup.
*/
if (test_addr != addr)
myfree(test_addr);
return (stat);
}
/* reject_invalid_hostname - fail if host/domain syntax is incorrect */
static int reject_invalid_hostname(SMTPD_STATE *state, char *name,
char *reply_name, char *reply_class)
{
const char *myname = "reject_invalid_hostname";
char *test_name;
int stat;
if (msg_verbose)
msg_info("%s: %s", myname, name);
/*
* Truncate hostnames ending in dot but not dot-dot.
*/
test_name = dup_if_truncate(name);
/*
* Validate the HELO/EHLO hostname. Fix 20140706: EAI not allowed here.
*/
if (!valid_hostname(test_name, DONT_GRIPE)
&& !valid_hostaddr(test_name, DONT_GRIPE)) /* XXX back compat */
stat = smtpd_check_reject(state, MAIL_ERROR_POLICY,
var_bad_name_code, "5.5.2",
"<%s>: %s rejected: Invalid name",
reply_name, reply_class);
else
stat = SMTPD_CHECK_DUNNO;
/*
* Cleanup.
*/
if (test_name != name)
myfree(test_name);
return (stat);
}
/* reject_non_fqdn_hostname - fail if host name is not in fqdn form */
static int reject_non_fqdn_hostname(SMTPD_STATE *state, char *name,
char *reply_name, char *reply_class)
{
const char *myname = "reject_non_fqdn_hostname";
char *test_name;
int stat;
if (msg_verbose)
msg_info("%s: %s", myname, name);
/*
* Truncate hostnames ending in dot but not dot-dot.
*/
test_name = dup_if_truncate(name);
/*
* Validate the hostname. For backwards compatibility, permit non-ASCII
* names only when the client requested SMTPUTF8 support.
*/
if (valid_utf8_hostname(state->flags & SMTPD_FLAG_SMTPUTF8,
test_name, DONT_GRIPE) == 0 || strchr(test_name, '.') == 0)
stat = smtpd_check_reject(state, MAIL_ERROR_POLICY,
var_non_fqdn_code, "5.5.2",
"<%s>: %s rejected: need fully-qualified hostname",
reply_name, reply_class);
else
stat = SMTPD_CHECK_DUNNO;
/*
* Cleanup.
*/
if (test_name != name)
myfree(test_name);
return (stat);
}
/* reject_unknown_hostname - fail if name has no A, AAAA or MX record */
static int reject_unknown_hostname(SMTPD_STATE *state, char *name,
char *reply_name, char *reply_class)
{
const char *myname = "reject_unknown_hostname";
int dns_status;
DNS_RR *dummy;
if (msg_verbose)
msg_info("%s: %s", myname, name);
#ifdef T_AAAA
#define RR_ADDR_TYPES T_A, T_AAAA
#else
#define RR_ADDR_TYPES T_A
#endif
dns_status = dns_lookup_l(name, 0, &dummy, (VSTRING *) 0,
(VSTRING *) 0, DNS_REQ_FLAG_STOP_OK,
RR_ADDR_TYPES, T_MX, 0);
if (dummy)
dns_rr_free(dummy);
/* Allow MTA names to have nullMX records. */
if (dns_status != DNS_OK && dns_status != DNS_NULLMX) {
if (dns_status == DNS_POLICY) {
msg_warn("%s: address or MX lookup error: %s",
name, "DNS reply filter drops all results");
return (SMTPD_CHECK_DUNNO);
}
if (dns_status != DNS_RETRY)
return (smtpd_check_reject(state, MAIL_ERROR_POLICY,
var_unk_name_code, "4.7.1",
"<%s>: %s rejected: %s",
reply_name, reply_class,
dns_status == DNS_INVAL ?
"Malformed DNS server reply" :
"Host not found"));
else
return (DEFER_IF_PERMIT2(unk_name_tf_act, state, MAIL_ERROR_POLICY,
450, "4.7.1",
"<%s>: %s rejected: Host not found",
reply_name, reply_class));
}
return (SMTPD_CHECK_DUNNO);
}
/* reject_unknown_mailhost - fail if name has no A, AAAA or MX record */
static int reject_unknown_mailhost(SMTPD_STATE *state, const char *name,
const char *reply_name, const char *reply_class)
{
const char *myname = "reject_unknown_mailhost";
int dns_status;
DNS_RR *dummy;
const char *aname;
if (msg_verbose)
msg_info("%s: %s", myname, name);
/*
* Fix 20140924: convert domain to ASCII.
*/
#ifndef NO_EAI
if (!allascii(name) && (aname = midna_domain_to_ascii(name)) != 0) {
if (msg_verbose)
msg_info("%s asciified to %s", name, aname);
name = aname;
}
#endif
#define MAILHOST_LOOKUP_FLAGS \
(DNS_REQ_FLAG_STOP_OK | DNS_REQ_FLAG_STOP_INVAL | \
DNS_REQ_FLAG_STOP_NULLMX | DNS_REQ_FLAG_STOP_MX_POLICY)
dns_status = dns_lookup_l(name, 0, &dummy, (VSTRING *) 0,
(VSTRING *) 0, MAILHOST_LOOKUP_FLAGS,
T_MX, RR_ADDR_TYPES, 0);
if (dummy)
dns_rr_free(dummy);
if (dns_status != DNS_OK) { /* incl. DNS_INVAL */
if (dns_status == DNS_POLICY) {
msg_warn("%s: MX or address lookup error: %s",
name, "DNS reply filter drops all results");
return (SMTPD_CHECK_DUNNO);
}
if (dns_status == DNS_NULLMX)
return (smtpd_check_reject(state, MAIL_ERROR_POLICY,
strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
550 : 556,
strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
"4.7.27" : "4.1.10",
"<%s>: %s rejected: Domain %s "
"does not accept mail (nullMX)",
reply_name, reply_class, name));
if (dns_status != DNS_RETRY)
return (smtpd_check_reject(state, MAIL_ERROR_POLICY,
var_unk_addr_code,
strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
"4.1.8" : "4.1.2",
"<%s>: %s rejected: %s",
reply_name, reply_class,
dns_status == DNS_INVAL ?
"Malformed DNS server reply" :
"Domain not found"));
else
return (DEFER_IF_PERMIT2(unk_addr_tf_act, state, MAIL_ERROR_POLICY,
450, strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
"4.1.8" : "4.1.2",
"<%s>: %s rejected: Domain not found",
reply_name, reply_class));
}
return (SMTPD_CHECK_DUNNO);
}
static int permit_auth_destination(SMTPD_STATE *state, char *recipient);
/* permit_tls_clientcerts - OK/DUNNO for message relaying, or set dict_errno */
static int permit_tls_clientcerts(SMTPD_STATE *state, int permit_all_certs)
{
#ifdef USE_TLS
const char *found = 0;
if (!state->tls_context)
return SMTPD_CHECK_DUNNO;
if (TLS_CERT_IS_TRUSTED(state->tls_context) && permit_all_certs) {
if (msg_verbose)
msg_info("Relaying allowed for all verified client certificates");
/* Permit logging in generic_checks() only. */
return (SMTPD_CHECK_OK);
}
/*
* When directly checking the fingerprint, it is OK if the issuing CA is
* not trusted.
*/
if (TLS_CERT_IS_PRESENT(state->tls_context)) {
int i;
char *prints[2];
prints[0] = state->tls_context->peer_cert_fprint;
prints[1] = state->tls_context->peer_pkey_fprint;
/* After lookup error, leave relay_ccerts->error at non-zero value. */
for (i = 0; i < 2; ++i) {
found = maps_find(relay_ccerts, prints[i], DICT_FLAG_NONE);
if (found != 0) {
if (msg_verbose)
msg_info("Relaying allowed for certified client: %s", found);
/* Permit logging in generic_checks() only. */
return (SMTPD_CHECK_OK);
} else if (relay_ccerts->error != 0) {
msg_warn("relay_clientcerts: lookup error for fingerprint '%s', "
"pkey fingerprint %s", prints[0], prints[1]);
return (relay_ccerts->error);
}
}
if (msg_verbose)
msg_info("relay_clientcerts: No match for fingerprint '%s', "
"pkey fingerprint %s", prints[0], prints[1]);
}
#endif
return (SMTPD_CHECK_DUNNO);
}
/* check_relay_domains - OK/FAIL for message relaying */
static int check_relay_domains(SMTPD_STATE *state, char *recipient,
char *reply_name, char *reply_class)
{
const char *myname = "check_relay_domains";
#if 1
static int once;
if (once == 0) {
once = 1;
msg_warn("support for restriction \"%s\" will be removed from %s; "
"use \"%s\" instead",
CHECK_RELAY_DOMAINS, var_mail_name, REJECT_UNAUTH_DEST);
}
#endif
if (msg_verbose)
msg_info("%s: %s", myname, recipient);
/*
* Permit if the client matches the relay_domains list.
*/
if (domain_list_match(relay_domains, state->name)) {
if (warn_compat_break_relay_domains)
msg_info("using backwards-compatible default setting "
VAR_RELAY_DOMAINS "=$mydestination to permit "
"request from client \"%s\"", state->name);
return (SMTPD_CHECK_OK);
}
/*
* Permit authorized destinations.
*/
if (permit_auth_destination(state, recipient) == SMTPD_CHECK_OK)
return (SMTPD_CHECK_OK);
/*
* Deny relaying between sites that both are not in relay_domains.
*/
return (smtpd_check_reject(state, MAIL_ERROR_POLICY,
var_relay_code, "5.7.1",
"<%s>: %s rejected: Relay access denied",
reply_name, reply_class));
}
/* permit_auth_destination - OK for message relaying */
static int permit_auth_destination(SMTPD_STATE *state, char *recipient)
{
const char *myname = "permit_auth_destination";
const RESOLVE_REPLY *reply;
const char *domain;
if (msg_verbose)
msg_info("%s: %s", myname, recipient);
/*
* Resolve the address.
*/
reply = smtpd_resolve_addr(state->sender, recipient);
if (reply->flags & RESOLVE_FLAG_FAIL)
reject_dict_retry(state, recipient);
/*
* Handle special case that is not supposed to happen.
*/
if ((domain = strrchr(CONST_STR(reply->recipient), '@')) == 0)
return (SMTPD_CHECK_OK);
domain += 1;
/*
* Skip source-routed non-local or virtual mail (uncertain destination).
*/
if (var_allow_untrust_route == 0 && (reply->flags & RESOLVE_FLAG_ROUTED))
return (SMTPD_CHECK_DUNNO);
/*
* Permit final delivery: the destination matches mydestination,
* virtual_alias_domains, or virtual_mailbox_domains.
*/
if (reply->flags & RESOLVE_CLASS_FINAL)
return (SMTPD_CHECK_OK);
/*
* Permit if the destination matches the relay_domains list.
*/
if (reply->flags & RESOLVE_CLASS_RELAY) {
if (warn_compat_break_relay_domains)
msg_info("using backwards-compatible default setting "
VAR_RELAY_DOMAINS "=$mydestination to accept mail "
"for domain \"%s\"", domain);
return (SMTPD_CHECK_OK);
}
/*
* Skip when not matched
*/
return (SMTPD_CHECK_DUNNO);
}
/* reject_unauth_destination - FAIL for message relaying */
static int reject_unauth_destination(SMTPD_STATE *state, char *recipient,
int reply_code, const char *reply_dsn)
{
const char *myname = "reject_unauth_destination";
if (msg_verbose)
msg_info("%s: %s", myname, recipient);
/*
* Skip authorized destination.
*/
if (permit_auth_destination(state, recipient) == SMTPD_CHECK_OK)
return (SMTPD_CHECK_DUNNO);
/*
* Reject relaying to sites that are not listed in relay_domains.
*/
return (smtpd_check_reject(state, MAIL_ERROR_POLICY,
reply_code, reply_dsn,
"<%s>: Relay access denied",
recipient));
}
/* reject_unauth_pipelining - reject improper use of SMTP command pipelining */
static int reject_unauth_pipelining(SMTPD_STATE *state,
const char *reply_name, const char *reply_class)
{
const char *myname = "reject_unauth_pipelining";
if (msg_verbose)
msg_info("%s: %s", myname, state->where);
if (state->flags & SMTPD_FLAG_ILL_PIPELINING)
return (smtpd_check_reject(state, MAIL_ERROR_PROTOCOL,
503, "5.5.0",
"<%s>: %s rejected: Improper use of SMTP command pipelining",
reply_name, reply_class));
return (SMTPD_CHECK_DUNNO);
}
/* all_auth_mx_addr - match host addresses against permit_mx_backup_networks */
static int all_auth_mx_addr(SMTPD_STATE *state, char *host,
const char *reply_name, const char *reply_class)
{
const char *myname = "all_auth_mx_addr";
MAI_HOSTADDR_STR hostaddr;
DNS_RR *rr;
DNS_RR *addr_list;
int dns_status;
if (msg_verbose)
msg_info("%s: host %s", myname, host);
/*
* If we can't lookup the host, defer.
*/
#define NOPE 0
#define YUP 1
/*
* Verify that all host addresses are within permit_mx_backup_networks.
*/
dns_status = dns_lookup_v(host, 0, &addr_list, (VSTRING *) 0, (VSTRING *) 0,
DNS_REQ_FLAG_NONE, inet_proto_info()->dns_atype_list);
/* DNS_NULLMX is not applicable here. */
if (dns_status != DNS_OK) { /* incl. DNS_INVAL */
DEFER_IF_REJECT4(state, MAIL_ERROR_POLICY,
450, "4.4.4",
"<%s>: %s rejected: Unable to look up host "
"%s as mail exchanger: %s",
reply_name, reply_class, host,
dns_status == DNS_POLICY ?
"DNS reply filter policy" : dns_strerror(h_errno));
return (NOPE);
}
for (rr = addr_list; rr != 0; rr = rr->next) {
if (dns_rr_to_pa(rr, &hostaddr) == 0) {
msg_warn("%s: skipping record type %s for host %s: %m",
myname, dns_strtype(rr->type), host);
continue;
}
if (msg_verbose)
msg_info("%s: checking: %s", myname, hostaddr.buf);
if (!namadr_list_match(perm_mx_networks, host, hostaddr.buf)) {
if (perm_mx_networks->error == 0) {
/*
* Reject: at least one IP address is not listed in
* permit_mx_backup_networks.
*/
if (msg_verbose)
msg_info("%s: address %s for %s does not match %s",
myname, hostaddr.buf, host, VAR_PERM_MX_NETWORKS);
} else {
msg_warn("%s: %s lookup error for address %s for %s",
myname, VAR_PERM_MX_NETWORKS, hostaddr.buf, host);
DEFER_IF_REJECT3(state, MAIL_ERROR_POLICY,
450, "4.4.4",
"<%s>: %s rejected: Unable to verify host %s as mail exchanger",
reply_name, reply_class, host);
}
dns_rr_free(addr_list);
return (NOPE);
}
}
dns_rr_free(addr_list);
return (YUP);
}
/* has_my_addr - see if this host name lists one of my network addresses */
static int has_my_addr(SMTPD_STATE *state, const char *host,
const char *reply_name, const char *reply_class)
{
const char *myname = "has_my_addr";
struct addrinfo *res;
struct addrinfo *res0;
int aierr;
MAI_HOSTADDR_STR hostaddr;
INET_PROTO_INFO *proto_info = inet_proto_info();
if (msg_verbose)
msg_info("%s: host %s", myname, host);
/*
* If we can't lookup the host, defer rather than reject.
*/
#define YUP 1
#define NOPE 0
aierr = hostname_to_sockaddr(host, (char *) 0, 0, &res0);
if (aierr) {
DEFER_IF_REJECT4(state, MAIL_ERROR_POLICY,
450, "4.4.4",
"<%s>: %s rejected: Unable to look up mail exchanger host %s: %s",
reply_name, reply_class, host, MAI_STRERROR(aierr));
return (NOPE);
}
#define HAS_MY_ADDR_RETURN(x) { freeaddrinfo(res0); return (x); }
for (res = res0; res != 0; res = res->ai_next) {
if (strchr((char *) proto_info->sa_family_list, res->ai_family) == 0) {
if (msg_verbose)
msg_info("skipping address family %d for host %s",
res->ai_family, host);
continue;
}
if (msg_verbose) {
SOCKADDR_TO_HOSTADDR(res->ai_addr, res->ai_addrlen,
&hostaddr, (MAI_SERVPORT_STR *) 0, 0);
msg_info("%s: addr %s", myname, hostaddr.buf);
}
if (own_inet_addr(res->ai_addr))
HAS_MY_ADDR_RETURN(YUP);
if (proxy_inet_addr(res->ai_addr))
HAS_MY_ADDR_RETURN(YUP);
}
if (msg_verbose)
msg_info("%s: host %s: no match", myname, host);
HAS_MY_ADDR_RETURN(NOPE);
}
/* i_am_mx - is this machine listed as MX relay */
static int i_am_mx(SMTPD_STATE *state, DNS_RR *mx_list,
const char *reply_name, const char *reply_class)
{
const char *myname = "i_am_mx";
DNS_RR *mx;
/*
* Compare hostnames first. Only if no name match is found, go through
* the trouble of host address lookups.
*/
for (mx = mx_list; mx != 0; mx = mx->next) {
if (msg_verbose)
msg_info("%s: resolve hostname: %s", myname, (char *) mx->data);
if (resolve_local((char *) mx->data) > 0)
return (YUP);
/* if no match or error, match interface addresses instead. */
}
/*
* Argh. Do further DNS lookups and match interface addresses.
*/
for (mx = mx_list; mx != 0; mx = mx->next) {
if (msg_verbose)
msg_info("%s: address lookup: %s", myname, (char *) mx->data);
if (has_my_addr(state, (char *) mx->data, reply_name, reply_class))
return (YUP);
}
/*
* This machine is not listed as MX relay.
*/
if (msg_verbose)
msg_info("%s: I am not listed as MX relay", myname);
return (NOPE);
}
/* permit_mx_primary - authorize primary MX relays */
static int permit_mx_primary(SMTPD_STATE *state, DNS_RR *mx_list,
const char *reply_name, const char *reply_class)
{
const char *myname = "permit_mx_primary";
DNS_RR *mx;
if (msg_verbose)
msg_info("%s", myname);
/*
* See if each best MX host has all IP addresses in
* permit_mx_backup_networks.
*/
for (mx = mx_list; mx != 0; mx = mx->next) {
if (!all_auth_mx_addr(state, (char *) mx->data, reply_name, reply_class))
return (NOPE);
}
/*
* All IP addresses of the best MX hosts are within
* permit_mx_backup_networks.
*/
return (YUP);
}
/* permit_mx_backup - permit use of me as MX backup for recipient domain */
static int permit_mx_backup(SMTPD_STATE *state, const char *recipient,
const char *reply_name, const char *reply_class)
{
const char *myname = "permit_mx_backup";
const RESOLVE_REPLY *reply;
const char *domain;
const char *adomain;
DNS_RR *mx_list;
DNS_RR *middle;
DNS_RR *rest;
int dns_status;
if (msg_verbose)
msg_info("%s: %s", myname, recipient);
/*
* Resolve the address.
*/
reply = smtpd_resolve_addr(state->sender, recipient);
if (reply->flags & RESOLVE_FLAG_FAIL)
reject_dict_retry(state, recipient);
/*
* For backwards compatibility, emulate permit_auth_destination. However,
* old permit_mx_backup implementations allow source routing with local
* address class.
*/
if ((domain = strrchr(CONST_STR(reply->recipient), '@')) == 0)
return (SMTPD_CHECK_OK);
domain += 1;
#if 0
if (reply->flags & RESOLVE_CLASS_LOCAL)
return (SMTPD_CHECK_OK);
#endif
if (var_allow_untrust_route == 0 && (reply->flags & RESOLVE_FLAG_ROUTED))
return (SMTPD_CHECK_DUNNO);
if (reply->flags & RESOLVE_CLASS_FINAL)
return (SMTPD_CHECK_OK);
if (reply->flags & RESOLVE_CLASS_RELAY) {
if (warn_compat_break_relay_domains)
msg_info("using backwards-compatible default setting "
VAR_RELAY_DOMAINS "=$mydestination to accept mail "
"for domain \"%s\"", domain);
return (SMTPD_CHECK_OK);
}
if (msg_verbose)
msg_info("%s: not local: %s", myname, recipient);
/*
* Skip numerical forms that didn't match the local system.
*/
if (domain[0] == '[' && domain[strlen(domain) - 1] == ']')
return (SMTPD_CHECK_DUNNO);
/*
* Fix 20140924: convert domain to ASCII.
*/
#ifndef NO_EAI
if (!allascii(domain) && (adomain = midna_domain_to_ascii(domain)) != 0) {
if (msg_verbose)
msg_info("%s asciified to %s", domain, adomain);
domain = adomain;
}
#endif
/*
* Look up the list of MX host names for this domain. If no MX host is
* found, perhaps it is a CNAME for the local machine. Clients aren't
* supposed to send CNAMEs in SMTP commands, but it happens anyway. If we
* can't look up the destination, play safe and turn reject into defer.
*/
dns_status = dns_lookup(domain, T_MX, 0, &mx_list,
(VSTRING *) 0, (VSTRING *) 0);
#if 0
if (dns_status == DNS_NOTFOUND)
return (has_my_addr(state, domain, reply_name, reply_class) ?
SMTPD_CHECK_OK : SMTPD_CHECK_DUNNO);
#endif
if (dns_status != DNS_OK) { /* incl. DNS_INVAL */
/* We don't special-case DNS_NULLMX. */
if (dns_status == DNS_RETRY || dns_status == DNS_POLICY)
DEFER_IF_REJECT3(state, MAIL_ERROR_POLICY,
450, "4.4.4",
"<%s>: %s rejected: Unable to look up mail "
"exchanger information: %s",
reply_name, reply_class, dns_status == DNS_POLICY ?
"DNS reply filter policy" : dns_strerror(h_errno));
return (SMTPD_CHECK_DUNNO);
}
/*
* Separate MX list into primaries and backups.
*/
mx_list = dns_rr_sort(mx_list, dns_rr_compare_pref_any);
for (middle = mx_list; /* see below */ ; middle = rest) {
rest = middle->next;
if (rest == 0)
break;
if (rest->pref != mx_list->pref) {
middle->next = 0;
break;
}
}
/* postcondition: middle->next = 0, rest may be 0. */
#define PERMIT_MX_BACKUP_RETURN(x) do { \
middle->next = rest; \
dns_rr_free(mx_list); \
return (x); \
} while (0)
/*
* First, see if we match any of the primary MX servers.
*/
if (i_am_mx(state, mx_list, reply_name, reply_class))
PERMIT_MX_BACKUP_RETURN(SMTPD_CHECK_DUNNO);
/*
* Then, see if we match any of the backup MX servers.
*/
if (rest == 0 || !i_am_mx(state, rest, reply_name, reply_class))
PERMIT_MX_BACKUP_RETURN(SMTPD_CHECK_DUNNO);
/*
* Optionally, see if the primary MX hosts are in a restricted list of
* networks.
*/
if (*var_perm_mx_networks
&& !permit_mx_primary(state, mx_list, reply_name, reply_class))
PERMIT_MX_BACKUP_RETURN(SMTPD_CHECK_DUNNO);
/*
* The destination passed all requirements.
*/
PERMIT_MX_BACKUP_RETURN(SMTPD_CHECK_OK);
}
/* reject_non_fqdn_address - fail if address is not in fqdn form */
static int reject_non_fqdn_address(SMTPD_STATE *state, char *addr,
char *reply_name, char *reply_class)
{
const char *myname = "reject_non_fqdn_address";
char *domain;
char *test_dom;
int stat;
if (msg_verbose)
msg_info("%s: %s", myname, addr);
/*
* Locate the domain information.
*/
if ((domain = strrchr(addr, '@')) != 0)
domain++;
else
domain = "";
/*
* Skip forms that we can't handle yet.
*/
if (domain[0] == '[' && domain[strlen(domain) - 1] == ']')
return (SMTPD_CHECK_DUNNO);
/*
* Truncate names ending in dot but not dot-dot.
*/
test_dom = dup_if_truncate(domain);
/*
* Validate the domain. For backwards compatibility, permit non-ASCII
* names only when the client requested SMTPUTF8 support.
*/
if (!*test_dom || !valid_utf8_hostname(state->flags & SMTPD_FLAG_SMTPUTF8,
test_dom, DONT_GRIPE) || !strchr(test_dom, '.'))
stat = smtpd_check_reject(state, MAIL_ERROR_POLICY,
var_non_fqdn_code, "4.5.2",
"<%s>: %s rejected: need fully-qualified address",
reply_name, reply_class);
else
stat = SMTPD_CHECK_DUNNO;
/*
* Cleanup.
*/
if (test_dom != domain)
myfree(test_dom);
return (stat);
}
/* reject_unknown_address - fail if address does not resolve */
static int reject_unknown_address(SMTPD_STATE *state, const char *addr,
const char *reply_name, const char *reply_class)
{
const char *myname = "reject_unknown_address";
const RESOLVE_REPLY *reply;
const char *domain;
if (msg_verbose)
msg_info("%s: %s", myname, addr);
/*
* Resolve the address.
*/
reply = smtpd_resolve_addr(strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
state->recipient : state->sender, addr);
if (reply->flags & RESOLVE_FLAG_FAIL)
reject_dict_retry(state, addr);
/*
* Skip local destinations and non-DNS forms.
*/
if ((domain = strrchr(CONST_STR(reply->recipient), '@')) == 0)
return (SMTPD_CHECK_DUNNO);
domain += 1;
if (reply->flags & RESOLVE_CLASS_FINAL)
return (SMTPD_CHECK_DUNNO);
if (domain[0] == '[' && domain[strlen(domain) - 1] == ']')
return (SMTPD_CHECK_DUNNO);
/*
* Look up the name in the DNS.
*/
return (reject_unknown_mailhost(state, domain, reply_name, reply_class));
}
/* reject_unverified_address - fail if address bounces */
static int reject_unverified_address(SMTPD_STATE *state, const char *addr,
const char *reply_name, const char *reply_class,
int unv_addr_dcode, int unv_addr_rcode,
int unv_addr_tf_act,
const char *alt_reply)
{
const char *myname = "reject_unverified_address";
VSTRING *why = vstring_alloc(10);
int rqst_status = SMTPD_CHECK_DUNNO;
int rcpt_status;
int verify_status;
int count;
int reject_code = 0;
if (msg_verbose)
msg_info("%s: %s", myname, addr);
/*
* Verify the address. Don't waste too much of their or our time.
*/
for (count = 0; /* see below */ ; /* see below */ ) {
verify_status = verify_clnt_query(addr, &rcpt_status, why);
if (verify_status != VRFY_STAT_OK || rcpt_status != DEL_RCPT_STAT_TODO)
break;
if (++count >= var_verify_poll_count)
break;
sleep(var_verify_poll_delay);
}
if (verify_status != VRFY_STAT_OK) {
msg_warn("%s service failure", var_verify_service);
rqst_status =
DEFER_IF_PERMIT2(unv_addr_tf_act, state, MAIL_ERROR_POLICY,
450, strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
SND_DSN : "4.1.1",
"<%s>: %s rejected: address verification problem",
reply_name, reply_class);
} else {
switch (rcpt_status) {
default:
msg_warn("unknown address verification status %d", rcpt_status);
break;
case DEL_RCPT_STAT_TODO:
case DEL_RCPT_STAT_DEFER:
reject_code = unv_addr_dcode;
break;
case DEL_RCPT_STAT_OK:
break;
case DEL_RCPT_STAT_BOUNCE:
reject_code = unv_addr_rcode;
break;
}
if (reject_code >= 400 && *alt_reply)
vstring_strcpy(why, alt_reply);
switch (reject_code / 100) {
case 2:
break;
case 4:
rqst_status =
DEFER_IF_PERMIT3(unv_addr_tf_act, state, MAIL_ERROR_POLICY,
reject_code,
strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
SND_DSN : "4.1.1",
"<%s>: %s rejected: unverified address: %.250s",
reply_name, reply_class, STR(why));
break;
default:
if (reject_code != 0)
rqst_status =
smtpd_check_reject(state, MAIL_ERROR_POLICY,
reject_code,
strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
SND_DSN : "4.1.1",
"<%s>: %s rejected: undeliverable address: %s",
reply_name, reply_class, STR(why));
break;
}
}
vstring_free(why);
return (rqst_status);
}
/* can_delegate_action - can we delegate this to the cleanup server */
#ifndef TEST
static int not_in_client_helo(SMTPD_STATE *, const char *, const char *, const char *);
static int can_delegate_action(SMTPD_STATE *state, const char *table,
const char *action, const char *reply_class)
{
/*
* If we're not using the cleanup server, then there is no way that we
* can support actions such as FILTER or HOLD that are delegated to the
* cleanup server.
*/
if (USE_SMTPD_PROXY(state)) {
msg_warn("access table %s: with %s specified, action %s is unavailable",
table, VAR_SMTPD_PROXY_FILT, action);
return (0);
}
/*
* ETRN does not receive mail so we can't store queue file records.
*/
if (strcmp(state->where, SMTPD_CMD_ETRN) == 0) {
msg_warn("access table %s: action %s is unavailable in %s",
table, action, VAR_ETRN_CHECKS);
return (0);
}
return (not_in_client_helo(state, table, action, reply_class));
}
/* not_in_client_helo - not in client or helo restriction context */
static int not_in_client_helo(SMTPD_STATE *state, const char *table,
const char *action,
const char *unused_reply_class)
{
/*
* If delay_reject=no, then client and helo restrictions take effect
* immediately, outside any particular mail transaction context. For
* example, rejecting HELO does not affect subsequent mail deliveries.
* Thus, if delay_reject=no, client and helo actions such as FILTER or
* HOLD also should not affect subsequent mail deliveries. Hmm...
*
* XXX If the MAIL FROM command is rejected then we have to reset access map
* side effects such as FILTER.
*/
if (state->sender == 0) {
msg_warn("access table %s: with %s=%s, "
"action %s is always skipped in %s or %s restrictions",
table, VAR_SMTPD_DELAY_REJECT, CONFIG_BOOL_NO,
action, SMTPD_NAME_CLIENT, SMTPD_NAME_HELO);
/* XXX What about ETRN? */
return (0);
}
return (1);
}
#endif
/* check_table_result - translate table lookup result into pass/reject */
static int check_table_result(SMTPD_STATE *state, const char *table,
const char *value, const char *datum,
const char *reply_name,
const char *reply_class,
const char *def_acl)
{
const char *myname = "check_table_result";
int code;
ARGV *restrictions;
jmp_buf savebuf;
int status;
const char *cmd_text;
int cmd_len;
static char def_dsn[] = "5.7.1";
DSN_SPLIT dp;
static VSTRING *buf;
#ifdef DELAY_ACTION
int defer_delay;
#endif
if (buf == 0)
buf = vstring_alloc(10);
/*
* Parse into command and text. Do not change the input.
*/
cmd_text = value + strcspn(value, " \t");
cmd_len = cmd_text - value;
vstring_strncpy(buf, value, cmd_len);
while (*cmd_text && ISSPACE(*cmd_text))
cmd_text++;
if (msg_verbose)
msg_info("%s: %s %s %s", myname, table, value, datum);
#define STREQUAL(x,y,l) (strncasecmp((x), (y), (l)) == 0 && (y)[l] == 0)
/*
* DUNNO means skip this table. Silently ignore optional text.
*/
if (STREQUAL(value, "DUNNO", cmd_len))
return (SMTPD_CHECK_DUNNO);
/*
* REJECT means NO. Use optional text or generate a generic error
* response.
*/
if (STREQUAL(value, "REJECT", cmd_len)) {
dsn_split(&dp, "5.7.1", cmd_text);
return (smtpd_check_reject(state, MAIL_ERROR_POLICY,
var_map_reject_code,
smtpd_dsn_fix(DSN_STATUS(dp.dsn),
reply_class),
"<%s>: %s rejected: %s",
reply_name, reply_class,
*dp.text ? dp.text : "Access denied"));
}
/*
* DEFER means "try again". Use optional text or generate a generic error
* response.
*/
if (STREQUAL(value, "DEFER", cmd_len)) {
dsn_split(&dp, "4.7.1", cmd_text);
return (smtpd_check_reject(state, MAIL_ERROR_POLICY,
var_map_defer_code,
smtpd_dsn_fix(DSN_STATUS(dp.dsn),
reply_class),
"<%s>: %s rejected: %s",
reply_name, reply_class,
*dp.text ? dp.text : "Access denied"));
}
#ifndef SHUT_RDWR
#define SHUT_RDWR 2
#endif
/*
* HANGUP. Text is optional. Drop the connection without sending any
* reply.
*
* Note: this is an unsupported test feature. No attempt is made to maintain
* compatibility between successive versions.
*/
if (STREQUAL(value, "HANGUP", cmd_len)) {
shutdown(vstream_fileno(state->client), SHUT_RDWR);
log_whatsup(state, "hangup", cmd_text);
vstream_longjmp(state->client, SMTP_ERR_QUIET);
}
/*
* INFO. Text is optional.
*/
if (STREQUAL(value, "INFO", cmd_len)) {
log_whatsup(state, "info", cmd_text);
return (SMTPD_CHECK_DUNNO);
}
/*
* WARN. Text is optional.
*/
if (STREQUAL(value, "WARN", cmd_len)) {
log_whatsup(state, "warn", cmd_text);
return (SMTPD_CHECK_DUNNO);
}
/*
* FILTER means deliver to content filter. But we may still change our
* mind, and reject/discard the message for other reasons.
*/
if (STREQUAL(value, "FILTER", cmd_len)) {
#ifndef TEST
if (can_delegate_action(state, table, "FILTER", reply_class) == 0)
return (SMTPD_CHECK_DUNNO);
#endif
if (*cmd_text == 0) {
msg_warn("access table %s entry \"%s\" has FILTER entry without value",
table, datum);
return (SMTPD_CHECK_DUNNO);
} else if (strchr(cmd_text, ':') == 0) {
msg_warn("access table %s entry \"%s\" requires transport:destination",
table, datum);
return (SMTPD_CHECK_DUNNO);
} else {
vstring_sprintf(error_text, "<%s>: %s triggers FILTER %s",
reply_name, reply_class, cmd_text);
log_whatsup(state, "filter", STR(error_text));
#ifndef TEST
UPDATE_STRING(state->saved_filter, cmd_text);
#endif
return (SMTPD_CHECK_DUNNO);
}
}
/*
* HOLD means deliver later. But we may still change our mind, and
* reject/discard the message for other reasons.
*/
if (STREQUAL(value, "HOLD", cmd_len)) {
#ifndef TEST
if (can_delegate_action(state, table, "HOLD", reply_class) == 0
|| (state->saved_flags & CLEANUP_FLAG_HOLD))
return (SMTPD_CHECK_DUNNO);
#endif
vstring_sprintf(error_text, "<%s>: %s %s", reply_name, reply_class,
*cmd_text ? cmd_text : "triggers HOLD action");
log_whatsup(state, "hold", STR(error_text));
#ifndef TEST
state->saved_flags |= CLEANUP_FLAG_HOLD;
#endif
return (SMTPD_CHECK_DUNNO);
}
/*
* DELAY means deliver later. But we may still change our mind, and
* reject/discard the message for other reasons.
*
* This feature is deleted because it has too many problems. 1) It does not
* work on some remote file systems; 2) mail will be delivered anyway
* with "sendmail -q" etc.; 3) while the mail is queued it bogs down the
* deferred queue scan with huge amounts of useless disk I/O operations.
*/
#ifdef DELAY_ACTION
if (STREQUAL(value, "DELAY", cmd_len)) {
#ifndef TEST
if (can_delegate_action(state, table, "DELAY", reply_class) == 0)
return (SMTPD_CHECK_DUNNO);
#endif
if (*cmd_text == 0) {
msg_warn("access table %s entry \"%s\" has DELAY entry without value",
table, datum);
return (SMTPD_CHECK_DUNNO);
}
if (conv_time(cmd_text, &defer_delay, 's') == 0) {
msg_warn("access table %s entry \"%s\" has invalid DELAY argument \"%s\"",
table, datum, cmd_text);
return (SMTPD_CHECK_DUNNO);
}
vstring_sprintf(error_text, "<%s>: %s %s", reply_name, reply_class,
*cmd_text ? cmd_text : "triggers DELAY action");
log_whatsup(state, "delay", STR(error_text));
#ifndef TEST
state->saved_delay = defer_delay;
#endif
return (SMTPD_CHECK_DUNNO);
}
#endif
/*
* DISCARD means silently discard and claim successful delivery.
*/
if (STREQUAL(value, "DISCARD", cmd_len)) {
#ifndef TEST
if (can_delegate_action(state, table, "DISCARD", reply_class) == 0)
return (SMTPD_CHECK_DUNNO);
#endif
vstring_sprintf(error_text, "<%s>: %s %s", reply_name, reply_class,
*cmd_text ? cmd_text : "triggers DISCARD action");
log_whatsup(state, "discard", STR(error_text));
#ifndef TEST
state->saved_flags |= CLEANUP_FLAG_DISCARD;
state->discard = 1;
#endif
return (smtpd_acl_permit(state, STR(buf), reply_class, reply_name,
"from %s", table));
}
/*
* REDIRECT means deliver to designated recipient. But we may still
* change our mind, and reject/discard the message for other reasons.
*/
if (STREQUAL(value, "REDIRECT", cmd_len)) {
#ifndef TEST
if (can_delegate_action(state, table, "REDIRECT", reply_class) == 0)
return (SMTPD_CHECK_DUNNO);
#endif
if (strchr(cmd_text, '@') == 0) {
msg_warn("access table %s entry \"%s\" requires user@domain target",
table, datum);
return (SMTPD_CHECK_DUNNO);
} else {
vstring_sprintf(error_text, "<%s>: %s triggers REDIRECT %s",
reply_name, reply_class, cmd_text);
log_whatsup(state, "redirect", STR(error_text));
#ifndef TEST
UPDATE_STRING(state->saved_redirect, cmd_text);
#endif
return (SMTPD_CHECK_DUNNO);
}
}
/*
* BCC means deliver to designated recipient. But we may still change our
* mind, and reject/discard the message for other reasons.
*/
if (STREQUAL(value, "BCC", cmd_len)) {
#ifndef TEST
if (can_delegate_action(state, table, "BCC", reply_class) == 0)
return (SMTPD_CHECK_DUNNO);
#endif
if (strchr(cmd_text, '@') == 0) {
msg_warn("access table %s entry \"%s\" requires user@domain target",
table, datum);
return (SMTPD_CHECK_DUNNO);
} else {
vstring_sprintf(error_text, "<%s>: %s triggers BCC %s",
reply_name, reply_class, cmd_text);
log_whatsup(state, "bcc", STR(error_text));
#ifndef TEST
if (state->saved_bcc == 0)
state->saved_bcc = argv_alloc(1);
argv_add(state->saved_bcc, cmd_text, (char *) 0);
#endif
return (SMTPD_CHECK_DUNNO);
}
}
/*
* DEFER_IF_PERMIT changes "permit" into "maybe". Use optional text or
* generate a generic error response.
*/
if (STREQUAL(value, DEFER_IF_PERMIT, cmd_len)) {
dsn_split(&dp, "4.7.1", cmd_text);
return (DEFER_IF_PERMIT3(DEFER_IF_PERMIT_ACT, state, MAIL_ERROR_POLICY,
var_map_defer_code,
smtpd_dsn_fix(DSN_STATUS(dp.dsn), reply_class),
"<%s>: %s rejected: %s",
reply_name, reply_class,
*dp.text ? dp.text : "Service unavailable"));
}
/*
* DEFER_IF_REJECT changes "reject" into "maybe". Use optional text or
* generate a generic error response.
*/
if (STREQUAL(value, DEFER_IF_REJECT, cmd_len)) {
dsn_split(&dp, "4.7.1", cmd_text);
DEFER_IF_REJECT3(state, MAIL_ERROR_POLICY,
var_map_defer_code,
smtpd_dsn_fix(DSN_STATUS(dp.dsn), reply_class),
"<%s>: %s rejected: %s",
reply_name, reply_class,
*dp.text ? dp.text : "Service unavailable");
return (SMTPD_CHECK_DUNNO);
}
/*
* PREPEND prepends the specified message header text.
*/
if (STREQUAL(value, "PREPEND", cmd_len)) {
#ifndef TEST
/* XXX what about ETRN. */
if (not_in_client_helo(state, table, "PREPEND", reply_class) == 0)
return (SMTPD_CHECK_DUNNO);
#endif
if (strcmp(state->where, SMTPD_AFTER_EOM) == 0) {
msg_warn("access table %s: action PREPEND must be used before %s",
table, VAR_EOD_CHECKS);
return (SMTPD_CHECK_DUNNO);
}
if (*cmd_text == 0 || is_header(cmd_text) == 0) {
msg_warn("access table %s entry \"%s\" requires header: text",
table, datum);
return (SMTPD_CHECK_DUNNO);
} else {
if (state->prepend == 0)
state->prepend = argv_alloc(1);
argv_add(state->prepend, cmd_text, (char *) 0);
return (SMTPD_CHECK_DUNNO);
}
}
/*
* All-numeric result probably means OK - some out-of-band authentication
* mechanism uses this as time stamp.
*/
if (alldig(value))
return (smtpd_acl_permit(state, STR(buf), reply_class, reply_name,
"from %s", table));
/*
* 4xx or 5xx means NO as well. smtpd_check_reject() will validate the
* response status code.
*
* If the caller specifies an RFC 3463 enhanced status code, put it
* immediately after the SMTP status code as described in RFC 2034.
*/
if (cmd_len == 3 && *cmd_text
&& (value[0] == '4' || value[0] == '5')
&& ISDIGIT(value[1]) && ISDIGIT(value[2])) {
code = atoi(value);
def_dsn[0] = value[0];
dsn_split(&dp, def_dsn, cmd_text);
return (smtpd_check_reject(state, MAIL_ERROR_POLICY,
code,
smtpd_dsn_fix(DSN_STATUS(dp.dsn),
reply_class),
"<%s>: %s rejected: %s",
reply_name, reply_class,
*dp.text ? dp.text : "Access denied"));
}
/*
* OK or RELAY means YES. Ignore trailing text.
*/
if (STREQUAL(value, "OK", cmd_len) || STREQUAL(value, "RELAY", cmd_len))
return (smtpd_acl_permit(state, STR(buf), reply_class, reply_name,
"from %s", table));
/*
* Unfortunately, maps must be declared ahead of time so they can be
* opened before we go to jail. We could insist that the RHS can only
* contain a pre-defined restriction class name, but that would be too
* restrictive. Instead we warn if an access table references any map.
*
* XXX Don't use passwd files or address rewriting maps as access tables.
*/
if (strchr(value, ':') != 0) {
msg_warn("access table %s has entry with lookup table: %s",
table, value);
msg_warn("do not specify lookup tables inside SMTPD access maps");
msg_warn("define a restriction class and specify its name instead.");
reject_server_error(state);
}
/*
* Don't get carried away with recursion.
*/
if (state->recursion > 100) {
msg_warn("access table %s entry %s causes unreasonable recursion",
table, value);
reject_server_error(state);
}
/*
* Recursively evaluate the restrictions given in the right-hand side. In
* the dark ages, an empty right-hand side meant OK. Make some
* discouraging comments.
*
* XXX Jump some hoops to avoid a minute memory leak in case of a file
* configuration error.
*/
#define ADDROF(x) ((char *) &(x))
restrictions = argv_splitq(value, CHARS_COMMA_SP, CHARS_BRACE);
memcpy(ADDROF(savebuf), ADDROF(smtpd_check_buf), sizeof(savebuf));
status = setjmp(smtpd_check_buf);
if (status != 0) {
argv_free(restrictions);
memcpy(ADDROF(smtpd_check_buf), ADDROF(savebuf),
sizeof(smtpd_check_buf));
longjmp(smtpd_check_buf, status);
}
if (restrictions->argc == 0) {
msg_warn("access table %s entry %s has empty value",
table, value);
status = SMTPD_CHECK_OK;
} else {
status = generic_checks(state, restrictions, reply_name,
reply_class, def_acl);
}
argv_free(restrictions);
memcpy(ADDROF(smtpd_check_buf), ADDROF(savebuf), sizeof(smtpd_check_buf));
return (status);
}
/* check_access - table lookup without substring magic */
static int check_access(SMTPD_STATE *state, const char *table, const char *name,
int flags, int *found, const char *reply_name,
const char *reply_class, const char *def_acl)
{
const char *myname = "check_access";
const char *value;
MAPS *maps;
#define CHK_ACCESS_RETURN(x,y) \
{ *found = y; return(x); }
#define FULL 0
#define PARTIAL DICT_FLAG_FIXED
#define FOUND 1
#define MISSED 0
if (msg_verbose)
msg_info("%s: %s", myname, name);
if ((maps = (MAPS *) htable_find(map_command_table, table)) == 0) {
msg_warn("%s: unexpected dictionary: %s", myname, table);
value = "451 4.3.5 Server configuration error";
CHK_ACCESS_RETURN(check_table_result(state, table, value, name,
reply_name, reply_class,
def_acl), FOUND);
}
if ((value = maps_find(maps, name, flags)) != 0)
CHK_ACCESS_RETURN(check_table_result(state, table, value, name,
reply_name, reply_class,
def_acl), FOUND);
if (maps->error != 0) {
/* Warning is already logged. */
value = "451 4.3.5 Server configuration error";
CHK_ACCESS_RETURN(check_table_result(state, table, value, name,
reply_name, reply_class,
def_acl), FOUND);
}
CHK_ACCESS_RETURN(SMTPD_CHECK_DUNNO, MISSED);
}
/* check_domain_access - domainname-based table lookup */
static int check_domain_access(SMTPD_STATE *state, const char *table,
const char *domain, int flags,
int *found, const char *reply_name,
const char *reply_class,
const char *def_acl)
{
const char *myname = "check_domain_access";
const char *name;
const char *next;
const char *value;
MAPS *maps;
int maybe_numerical = 1;
if (msg_verbose)
msg_info("%s: %s", myname, domain);
/*
* Try the name and its parent domains. Including top-level domains.
*
* Helo names can end in ".". The test below avoids lookups of the empty
* key, because Berkeley DB cannot deal with it. [Victor Duchovni, Morgan
* Stanley].
*
* TODO(wietse) move to mail_domain_find library module.
*/
#define CHK_DOMAIN_RETURN(x,y) { *found = y; return(x); }
if ((maps = (MAPS *) htable_find(map_command_table, table)) == 0) {
msg_warn("%s: unexpected dictionary: %s", myname, table);
value = "451 4.3.5 Server configuration error";
CHK_DOMAIN_RETURN(check_table_result(state, table, value,
domain, reply_name, reply_class,
def_acl), FOUND);
}
for (name = domain; *name != 0; name = next) {
if ((value = maps_find(maps, name, flags)) != 0)
CHK_DOMAIN_RETURN(check_table_result(state, table, value,
domain, reply_name, reply_class,
def_acl), FOUND);
if (maps->error != 0) {
/* Warning is already logged. */
value = "451 4.3.5 Server configuration error";
CHK_DOMAIN_RETURN(check_table_result(state, table, value,
domain, reply_name, reply_class,
def_acl), FOUND);
}
/* Don't apply subdomain magic to numerical hostnames. */
if (maybe_numerical
&& (maybe_numerical = valid_hostaddr(domain, DONT_GRIPE)) != 0)
break;
if ((next = strchr(name + 1, '.')) == 0)
break;
if (access_parent_style == MATCH_FLAG_PARENT)
next += 1;
flags = PARTIAL;
}
CHK_DOMAIN_RETURN(SMTPD_CHECK_DUNNO, MISSED);
}
/* check_addr_access - address-based table lookup */
static int check_addr_access(SMTPD_STATE *state, const char *table,
const char *address, int flags,
int *found, const char *reply_name,
const char *reply_class,
const char *def_acl)
{
const char *myname = "check_addr_access";
char *addr;
const char *value;
MAPS *maps;
int delim;
if (msg_verbose)
msg_info("%s: %s", myname, address);
/*
* Try the address and its parent networks.
*
* TODO(wietse) move to mail_ipaddr_find library module.
*/
#define CHK_ADDR_RETURN(x,y) { *found = y; return(x); }
addr = STR(vstring_strcpy(error_text, address));
#ifdef HAS_IPV6
if (strchr(addr, ':') != 0)
delim = ':';
else
#endif
delim = '.';
if ((maps = (MAPS *) htable_find(map_command_table, table)) == 0) {
msg_warn("%s: unexpected dictionary: %s", myname, table);
value = "451 4.3.5 Server configuration error";
CHK_ADDR_RETURN(check_table_result(state, table, value, address,
reply_name, reply_class,
def_acl), FOUND);
}
do {
if ((value = maps_find(maps, addr, flags)) != 0)
CHK_ADDR_RETURN(check_table_result(state, table, value, address,
reply_name, reply_class,
def_acl), FOUND);
if (maps->error != 0) {
/* Warning is already logged. */
value = "451 4.3.5 Server configuration error";
CHK_ADDR_RETURN(check_table_result(state, table, value, address,
reply_name, reply_class,
def_acl), FOUND);
}
flags = PARTIAL;
} while (split_at_right(addr, delim));
CHK_ADDR_RETURN(SMTPD_CHECK_DUNNO, MISSED);
}
/* check_namadr_access - OK/FAIL based on host name/address lookup */
static int check_namadr_access(SMTPD_STATE *state, const char *table,
const char *name, const char *addr,
int flags, int *found,
const char *reply_name,
const char *reply_class,
const char *def_acl)
{
const char *myname = "check_namadr_access";
int status;
if (msg_verbose)
msg_info("%s: name %s addr %s", myname, name, addr);
/*
* Look up the host name, or parent domains thereof. XXX A domain
* wildcard may pre-empt a more specific address table entry.
*/
if ((status = check_domain_access(state, table, name, flags,
found, reply_name, reply_class,
def_acl)) != 0 || *found)
return (status);
/*
* Look up the network address, or parent networks thereof.
*/
if ((status = check_addr_access(state, table, addr, flags,
found, reply_name, reply_class,
def_acl)) != 0 || *found)
return (status);
/*
* Undecided when the host was not found.
*/
return (SMTPD_CHECK_DUNNO);
}
/* check_server_access - access control by server host name or address */
static int check_server_access(SMTPD_STATE *state, const char *table,
const char *name,
int type,
const char *reply_name,
const char *reply_class,
const char *def_acl)
{
const char *myname = "check_server_access";
const char *domain;
const char *adomain;
int dns_status;
DNS_RR *server_list;
DNS_RR *server;
int found = 0;
MAI_HOSTADDR_STR addr_string;
int aierr;
struct addrinfo *res0;
struct addrinfo *res;
int status;
INET_PROTO_INFO *proto_info;
/*
* Sanity check.
*/
if (type != T_MX && type != T_NS && type != T_A
#ifdef HAS_IPV6
&& type != T_AAAA
#endif
)
msg_panic("%s: unexpected resource type \"%s\" in request",
myname, dns_strtype(type));
if (msg_verbose)
msg_info("%s: %s %s", myname, dns_strtype(type), name);
/*
* Skip over local-part.
*/
if ((domain = strrchr(name, '@')) != 0)
domain += 1;
else
domain = name;
/*
* Treat an address literal as its own MX server, just like we treat a
* name without MX record as its own MX server. There is, however, no
* applicable NS server equivalent.
*/
if (*domain == '[') {
char *saved_addr;
const char *bare_addr;
ssize_t len;
if (type != T_A && type != T_MX)
return (SMTPD_CHECK_DUNNO);
len = strlen(domain);
if (domain[len - 1] != ']')
return (SMTPD_CHECK_DUNNO);
/* Memory leak alert: no early returns after this point. */
saved_addr = mystrndup(domain + 1, len - 2);
if ((bare_addr = valid_mailhost_addr(saved_addr, DONT_GRIPE)) == 0)
status = SMTPD_CHECK_DUNNO;
else
status = check_addr_access(state, table, bare_addr, FULL,
&found, reply_name, reply_class,
def_acl);
myfree(saved_addr);
return (status);
}
/*
* Fix 20140924: convert domain to ASCII.
*/
#ifndef NO_EAI
if (!allascii(domain) && (adomain = midna_domain_to_ascii(domain)) != 0) {
if (msg_verbose)
msg_info("%s asciified to %s", domain, adomain);
domain = adomain;
}
#endif
/*
* If the request is type A or AAAA, fabricate an MX record that points
* to the domain name itself, and skip name-based access control.
*
* If the domain name does not exist then we apply no restriction.
*
* If the domain name exists but no MX record exists, fabricate an MX record
* that points to the domain name itself.
*
* If the domain name exists but no NS record exists, look up parent domain
* NS records.
*
* XXX 20150707 Work around broken DNS servers that reply with NXDOMAIN
* instead of "no data".
*/
if (type == T_A
#ifdef HAS_IPV6
|| type == T_AAAA
#endif
) {
server_list = dns_rr_create(domain, domain, T_MX, C_IN, 0, 0,
domain, strlen(domain) + 1);
} else {
dns_status = dns_lookup(domain, type, 0, &server_list,
(VSTRING *) 0, (VSTRING *) 0);
if (dns_status == DNS_NULLMX)
return (SMTPD_CHECK_DUNNO);
if (dns_status == DNS_NOTFOUND /* Not: h_errno == NO_DATA */ ) {
if (type == T_MX) {
server_list = dns_rr_create(domain, domain, type, C_IN, 0, 0,
domain, strlen(domain) + 1);
dns_status = DNS_OK;
} else if (type == T_NS /* && h_errno == NO_DATA */ ) {
while ((domain = strchr(domain, '.')) != 0 && domain[1]) {
domain += 1;
dns_status = dns_lookup(domain, type, 0, &server_list,
(VSTRING *) 0, (VSTRING *) 0);
if (dns_status != DNS_NOTFOUND /* || h_errno != NO_DATA */ )
break;
}
}
}
if (dns_status != DNS_OK) {
msg_warn("Unable to look up %s host for %s: %s", dns_strtype(type),
domain && domain[1] ? domain : name, dns_status == DNS_POLICY ?
"DNS reply filter policy" : dns_strerror(h_errno));
return (SMTPD_CHECK_DUNNO);
}
}
/*
* No bare returns after this point or we have a memory leak.
*/
#define CHECK_SERVER_RETURN(x) { dns_rr_free(server_list); return(x); }
/*
* Check the hostnames first, then the addresses.
*/
proto_info = inet_proto_info();
for (server = server_list; server != 0; server = server->next) {
if (msg_verbose)
msg_info("%s: %s hostname check: %s",
myname, dns_strtype(type), (char *) server->data);
if (valid_hostaddr((char *) server->data, DONT_GRIPE)) {
if ((status = check_addr_access(state, table, (char *) server->data,
FULL, &found, reply_name, reply_class,
def_acl)) != 0 || found)
CHECK_SERVER_RETURN(status);
continue;
}
if (type != T_A && type != T_AAAA
&& ((status = check_domain_access(state, table, (char *) server->data,
FULL, &found, reply_name, reply_class,
def_acl)) != 0 || found))
CHECK_SERVER_RETURN(status);
if ((aierr = hostname_to_sockaddr((char *) server->data,
(char *) 0, 0, &res0)) != 0) {
if (type != T_A && type != T_AAAA)
msg_warn("Unable to look up %s host %s for %s %s: %s",
dns_strtype(type), (char *) server->data,
reply_class, reply_name, MAI_STRERROR(aierr));
continue;
}
/* Now we must also free the addrinfo result. */
if (msg_verbose)
msg_info("%s: %s host address check: %s",
myname, dns_strtype(type), (char *) server->data);
for (res = res0; res != 0; res = res->ai_next) {
if (strchr((char *) proto_info->sa_family_list, res->ai_family) == 0) {
if (msg_verbose)
msg_info("skipping address family %d for host %s",
res->ai_family, server->data);
continue;
}
SOCKADDR_TO_HOSTADDR(res->ai_addr, res->ai_addrlen,
&addr_string, (MAI_SERVPORT_STR *) 0, 0);
status = check_addr_access(state, table, addr_string.buf, FULL,
&found, reply_name, reply_class,
def_acl);
if (status != 0 || found) {
freeaddrinfo(res0); /* 200412 */
CHECK_SERVER_RETURN(status);
}
}
freeaddrinfo(res0); /* 200412 */
}
CHECK_SERVER_RETURN(SMTPD_CHECK_DUNNO);
}
/* check_ccert_access - access for TLS clients by certificate fingerprint */
static int check_ccert_access(SMTPD_STATE *state, const char *table,
const char *def_acl)
{
int result = SMTPD_CHECK_DUNNO;
#ifdef USE_TLS
const char *myname = "check_ccert_access";
int found;
/*
* When directly checking the fingerprint, it is OK if the issuing CA is
* not trusted.
*/
if (TLS_CERT_IS_PRESENT(state->tls_context)) {
int i;
char *prints[2];
prints[0] = state->tls_context->peer_cert_fprint;
prints[1] = state->tls_context->peer_pkey_fprint;
for (i = 0; i < 2; ++i) {
if (msg_verbose)
msg_info("%s: %s", myname, prints[i]);
/*
* Regexp tables don't make sense for certificate fingerprints.
* That may be so, but we can't ignore the entire
* check_ccert_access request without logging a warning.
*
* Log the peer CommonName when access is denied. Non-printable
* characters will be neutered by smtpd_check_reject(). The SMTP
* client name and address are always syslogged as part of a
* "reject" event.
*/
result = check_access(state, table, prints[i],
DICT_FLAG_NONE, &found,
state->tls_context->peer_CN,
SMTPD_NAME_CCERT, def_acl);
if (result != SMTPD_CHECK_DUNNO)
break;
}
}
#endif
return (result);
}
/* check_sasl_access - access by SASL user name */
#ifdef USE_SASL_AUTH
static int check_sasl_access(SMTPD_STATE *state, const char *table,
const char *def_acl)
{
int result;
int unused_found;
char *sane_username = printable(mystrdup(state->sasl_username), '_');
result = check_access(state, table, state->sasl_username,
DICT_FLAG_NONE, &unused_found, sane_username,
SMTPD_NAME_SASL_USER, def_acl);
myfree(sane_username);
return (result);
}
#endif
/* check_mail_access - OK/FAIL based on mail address lookup */
static int check_mail_access(SMTPD_STATE *state, const char *table,
const char *addr, int *found,
const char *reply_name,
const char *reply_class,
const char *def_acl)
{
const char *myname = "check_mail_access";
const RESOLVE_REPLY *reply;
const char *value;
int lookup_strategy;
int status;
MAPS *maps;
if (msg_verbose)
msg_info("%s: %s", myname, addr);
/*
* Resolve the address.
*/
reply = smtpd_resolve_addr(strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
state->recipient : state->sender, addr);
if (reply->flags & RESOLVE_FLAG_FAIL)
reject_dict_retry(state, addr);
/*
* Garbage in, garbage out. Every address from rewrite_clnt_internal()
* and from resolve_clnt_query() must be fully qualified.
*/
if (strrchr(CONST_STR(reply->recipient), '@') == 0) {
msg_warn("%s: no @domain in address: %s", myname,
CONST_STR(reply->recipient));
return (0);
}
/*
* Source-routed (non-local or virtual) recipient addresses are too
* suspicious for returning an "OK" result. The complicated expression
* below was brought to you by the keyboard of Victor Duchovni, Morgan
* Stanley and hacked up a bit by Wietse.
*/
#define SUSPICIOUS(reply, reply_class) \
(var_allow_untrust_route == 0 \
&& (reply->flags & RESOLVE_FLAG_ROUTED) \
&& strcmp(reply_class, SMTPD_NAME_RECIPIENT) == 0)
/*
* Look up user+foo@domain if the address has an extension, user@domain
* otherwise.
*/
lookup_strategy = MA_FIND_FULL | MA_FIND_NOEXT | MA_FIND_DOMAIN
| MA_FIND_LOCALPART_AT
| (access_parent_style == MATCH_FLAG_PARENT ?
MA_FIND_PDMS : MA_FIND_PDDMDS);
if ((maps = (MAPS *) htable_find(map_command_table, table)) == 0) {
msg_warn("%s: unexpected dictionary: %s", myname, table);
value = "451 4.3.5 Server configuration error";
return (check_table_result(state, table, value,
CONST_STR(reply->recipient),
reply_name, reply_class,
def_acl));
}
if ((value = mail_addr_find_strategy(maps, CONST_STR(reply->recipient),
(char **) 0, lookup_strategy)) != 0) {
*found = 1;
status = check_table_result(state, table, value,
CONST_STR(reply->recipient),
reply_name, reply_class, def_acl);
return (status == SMTPD_CHECK_OK && SUSPICIOUS(reply, reply_class) ?
SMTPD_CHECK_DUNNO : status);
} else if (maps->error != 0) {
/* Warning is already logged. */
value = "451 4.3.5 Server configuration error";
return (check_table_result(state, table, value,
CONST_STR(reply->recipient),
reply_name, reply_class,
def_acl));
}
/*
* Undecided when no match found.
*/
return (SMTPD_CHECK_DUNNO);
}
/* Support for different DNSXL lookup results. */
static SMTPD_RBL_STATE dnsxl_stat_soft[1];
#define SMTPD_DNSXL_STAT_SOFT(dnsxl_res) ((dnsxl_res) == dnsxl_stat_soft)
#define SMTPD_DNXSL_STAT_HARD(dnsxl_res) ((dnsxl_res) == 0)
#define SMTPD_DNSXL_STAT_OK(dnsxl_res) \
!(SMTPD_DNXSL_STAT_HARD(dnsxl_res) || SMTPD_DNSXL_STAT_SOFT(dnsxl_res))
/* rbl_pagein - look up an RBL lookup result */
static void *rbl_pagein(const char *query, void *unused_context)
{
DNS_RR *txt_list;
VSTRING *why;
int dns_status;
SMTPD_RBL_STATE *rbl = 0;
DNS_RR *addr_list;
DNS_RR *rr;
DNS_RR *next;
VSTRING *buf;
int space_left;
/*
* Do the query. If the DNS lookup produces no definitive reply, give the
* requestor the benefit of the doubt. We can't block all email simply
* because an RBL server is unavailable.
*
* Don't do this for AAAA records. Yet.
*/
why = vstring_alloc(10);
dns_status = dns_lookup(query, T_A, 0, &addr_list, (VSTRING *) 0, why);
if (dns_status != DNS_OK && dns_status != DNS_NOTFOUND) {
msg_warn("%s: RBL lookup error: %s", query, STR(why));
rbl = dnsxl_stat_soft;
}
vstring_free(why);
if (dns_status != DNS_OK)
return ((void *) rbl);
/*
* Save the result. Yes, we cache negative results as well as positive
* results. Concatenate multiple TXT records, up to some limit.
*/
#define RBL_TXT_LIMIT 500
rbl = (SMTPD_RBL_STATE *) mymalloc(sizeof(*rbl));
dns_status = dns_lookup(query, T_TXT, 0, &txt_list,
(VSTRING *) 0, (VSTRING *) 0);
if (dns_status == DNS_OK) {
buf = vstring_alloc(1);
space_left = RBL_TXT_LIMIT;
for (rr = txt_list; rr != 0 && space_left > 0; rr = next) {
vstring_strncat(buf, rr->data, (int) rr->data_len > space_left ?
space_left : rr->data_len);
space_left = RBL_TXT_LIMIT - VSTRING_LEN(buf);
next = rr->next;
if (next && space_left > 3) {
vstring_strcat(buf, " / ");
space_left -= 3;
}
}
rbl->txt = vstring_export(buf);
dns_rr_free(txt_list);
} else {
if (dns_status == DNS_POLICY)
msg_warn("%s: TXT lookup error: %s",
query, "DNS reply filter drops all results");
rbl->txt = 0;
}
rbl->a = addr_list;
return ((void *) rbl);
}
/* rbl_pageout - discard an RBL lookup result */
static void rbl_pageout(void *data, void *unused_context)
{
SMTPD_RBL_STATE *rbl = (SMTPD_RBL_STATE *) data;
if (SMTPD_DNSXL_STAT_OK(rbl)) {
if (rbl->txt)
myfree(rbl->txt);
if (rbl->a)
dns_rr_free(rbl->a);
myfree((void *) rbl);
}
}
/* rbl_byte_pagein - parse RBL reply pattern, save byte codes */
static void *rbl_byte_pagein(const char *query, void *unused_context)
{
VSTRING *byte_codes = vstring_alloc(100);
char *saved_query = mystrdup(query);
char *saved_byte_codes;
char *err;
if ((err = ip_match_parse(byte_codes, saved_query)) != 0)
msg_fatal("RBL reply error: %s", err);
saved_byte_codes = ip_match_save(byte_codes);
myfree(saved_query);
vstring_free(byte_codes);
return (saved_byte_codes);
}
/* rbl_byte_pageout - discard parsed RBL reply byte codes */
static void rbl_byte_pageout(void *data, void *unused_context)
{
myfree(data);
}
/* rbl_expand_lookup - RBL specific $name expansion */
static const char *rbl_expand_lookup(const char *name, int mode,
void *context)
{
SMTPD_RBL_EXPAND_CONTEXT *rbl_exp = (SMTPD_RBL_EXPAND_CONTEXT *) context;
SMTPD_STATE *state = rbl_exp->state;
#define STREQ(x,y) (*(x) == *(y) && strcmp((x), (y)) == 0)
if (state->expand_buf == 0)
state->expand_buf = vstring_alloc(10);
if (msg_verbose > 1)
msg_info("rbl_expand_lookup: ${%s}", name);
/*
* Be sure to return NULL only for non-existent names.
*/
if (STREQ(name, MAIL_ATTR_RBL_CODE)) {
vstring_sprintf(state->expand_buf, "%d", var_maps_rbl_code);
return (STR(state->expand_buf));
} else if (STREQ(name, MAIL_ATTR_RBL_DOMAIN)) {
return (rbl_exp->domain);
} else if (STREQ(name, MAIL_ATTR_RBL_REASON)) {
return (rbl_exp->txt);
} else if (STREQ(name, MAIL_ATTR_RBL_TXT)) {/* LaMont compat */
return (rbl_exp->txt);
} else if (STREQ(name, MAIL_ATTR_RBL_WHAT)) {
return (rbl_exp->what);
} else if (STREQ(name, MAIL_ATTR_RBL_CLASS)) {
return (rbl_exp->class);
} else {
return (smtpd_expand_lookup(name, mode, (void *) state));
}
}
/* rbl_reject_reply - format reply after RBL reject */
static int rbl_reject_reply(SMTPD_STATE *state, const SMTPD_RBL_STATE *rbl,
const char *rbl_domain,
const char *what,
const char *reply_class)
{
const char *myname = "rbl_reject_reply";
VSTRING *why = 0;
const char *template = 0;
SMTPD_RBL_EXPAND_CONTEXT rbl_exp;
int result;
DSN_SPLIT dp;
int code;
/*
* Use the server-specific reply template or use the default one.
*/
if (*var_rbl_reply_maps) {
template = maps_find(rbl_reply_maps, rbl_domain, DICT_FLAG_NONE);
if (rbl_reply_maps->error)
reject_server_error(state);
}
why = vstring_alloc(100);
rbl_exp.state = state;
rbl_exp.domain = mystrdup(rbl_domain);
(void) split_at(rbl_exp.domain, '=');
rbl_exp.what = what;
rbl_exp.class = reply_class;
rbl_exp.txt = (rbl->txt == 0 ? "" : rbl->txt);
for (;;) {
if (template == 0)
template = var_def_rbl_reply;
if (mac_expand(why, template, MAC_EXP_FLAG_NONE,
STR(smtpd_expand_filter), rbl_expand_lookup,
(void *) &rbl_exp) == 0)
break;
if (template == var_def_rbl_reply)
msg_fatal("%s: bad default rbl reply template: %s",
myname, var_def_rbl_reply);
msg_warn("%s: bad rbl reply template for domain %s: %s",
myname, rbl_domain, template);
template = 0; /* pretend not found */
}
/*
* XXX Impedance mis-match.
*
* Validate the response, that is, the response must begin with a
* three-digit status code, and the first digit must be 4 or 5. If the
* response is bad, log a warning and send a generic response instead.
*/
if ((STR(why)[0] != '4' && STR(why)[0] != '5')
|| !ISDIGIT(STR(why)[1]) || !ISDIGIT(STR(why)[2])
|| STR(why)[3] != ' ') {
msg_warn("rbl response code configuration error: %s", STR(why));
result = smtpd_check_reject(state, MAIL_ERROR_POLICY,
450, "4.7.1", "Service unavailable");
} else {
code = atoi(STR(why));
dsn_split(&dp, "4.7.1", STR(why) + 4);
result = smtpd_check_reject(state, MAIL_ERROR_POLICY,
code,
smtpd_dsn_fix(DSN_STATUS(dp.dsn),
reply_class),
"%s", *dp.text ?
dp.text : "Service unavailable");
}
/*
* Clean up.
*/
myfree(rbl_exp.domain);
vstring_free(why);
return (result);
}
/* rbl_match_addr - match address list */
static int rbl_match_addr(SMTPD_RBL_STATE *rbl, const char *byte_codes)
{
const char *myname = "rbl_match_addr";
DNS_RR *rr;
for (rr = rbl->a; rr != 0; rr = rr->next) {
if (rr->type == T_A) {
if (ip_match_execute(byte_codes, rr->data))
return (1);
} else {
msg_warn("%s: skipping record type %s for query %s",
myname, dns_strtype(rr->type), rr->qname);
}
}
return (0);
}
/* find_dnsxl_addr - look up address in DNSXL */
static const SMTPD_RBL_STATE *find_dnsxl_addr(SMTPD_STATE *state,
const char *rbl_domain,
const char *addr)
{
const char *myname = "find_dnsxl_addr";
ARGV *octets;
VSTRING *query;
int i;
SMTPD_RBL_STATE *rbl;
const char *reply_addr;
const char *byte_codes;
struct addrinfo *res;
unsigned char *ipv6_addr;
query = vstring_alloc(100);
/*
* Reverse the client IPV6 address, represented as 32 hexadecimal
* nibbles. We use the binary address to avoid tricky code. Asking for an
* AAAA record makes no sense here. Just like with IPv4 we use the lookup
* result as a bit mask, not as an IP address.
*/
#ifdef HAS_IPV6
if (valid_ipv6_hostaddr(addr, DONT_GRIPE)) {
if (hostaddr_to_sockaddr(addr, (char *) 0, 0, &res) != 0
|| res->ai_family != PF_INET6)
msg_fatal("%s: unable to convert address %s", myname, addr);
ipv6_addr = (unsigned char *) &SOCK_ADDR_IN6_ADDR(res->ai_addr);
for (i = sizeof(SOCK_ADDR_IN6_ADDR(res->ai_addr)) - 1; i >= 0; i--)
vstring_sprintf_append(query, "%x.%x.",
ipv6_addr[i] & 0xf, ipv6_addr[i] >> 4);
freeaddrinfo(res);
} else
#endif
/*
* Reverse the client IPV4 address, represented as four decimal octet
* values. We use the textual address for convenience.
*/
{
octets = argv_split(addr, ".");
for (i = octets->argc - 1; i >= 0; i--) {
vstring_strcat(query, octets->argv[i]);
vstring_strcat(query, ".");
}
argv_free(octets);
}
/*
* Tack on the RBL domain name and query the DNS for an A record.
*/
vstring_strcat(query, rbl_domain);
reply_addr = split_at(STR(query), '=');
rbl = (SMTPD_RBL_STATE *) ctable_locate(smtpd_rbl_cache, STR(query));
if (reply_addr != 0)
byte_codes = ctable_locate(smtpd_rbl_byte_cache, reply_addr);
/*
* If the record exists, match the result address.
*/
if (SMTPD_DNSXL_STAT_OK(rbl) && reply_addr != 0
&& !rbl_match_addr(rbl, byte_codes))
rbl = 0;
vstring_free(query);
return (rbl);
}
/* reject_rbl_addr - reject address in real-time blackhole list */
static int reject_rbl_addr(SMTPD_STATE *state, const char *rbl_domain,
const char *addr, const char *reply_class)
{
const char *myname = "reject_rbl_addr";
const SMTPD_RBL_STATE *rbl;
if (msg_verbose)
msg_info("%s: %s %s", myname, reply_class, addr);
rbl = find_dnsxl_addr(state, rbl_domain, addr);
if (!SMTPD_DNSXL_STAT_OK(rbl)) {
return (SMTPD_CHECK_DUNNO);
} else {
return (rbl_reject_reply(state, rbl, rbl_domain, addr, reply_class));
}
}
/* permit_dnswl_addr - permit address in DNSWL */
static int permit_dnswl_addr(SMTPD_STATE *state, const char *dnswl_domain,
const char *addr, const char *reply_class)
{
const char *myname = "permit_dnswl_addr";
const SMTPD_RBL_STATE *dnswl_result;
if (msg_verbose)
msg_info("%s: %s", myname, addr);
/* Safety: don't whitelist unauthorized recipients. */
if (strcmp(state->where, SMTPD_CMD_RCPT) == 0 && state->recipient != 0
&& permit_auth_destination(state, state->recipient) != SMTPD_CHECK_OK)
return (SMTPD_CHECK_DUNNO);
dnswl_result = find_dnsxl_addr(state, dnswl_domain, addr);
if (SMTPD_DNXSL_STAT_HARD(dnswl_result)) {
return (SMTPD_CHECK_DUNNO);
} else if (SMTPD_DNSXL_STAT_SOFT(dnswl_result)) {
/* XXX: Make configurable as dnswl_tempfail_action. */
DEFER_IF_REJECT3(state, MAIL_ERROR_POLICY,
450, "4.7.1",
"<%s>: %s rejected: %s",
addr, reply_class,
"Service unavailable");
return (SMTPD_CHECK_DUNNO);
} else if (SMTPD_DNSXL_STAT_OK(dnswl_result)) {
return (SMTPD_CHECK_OK);
} else {
/* Future proofing, in case find_dnsxl_addr() result is changed. */
msg_panic("%s: find_dnsxl_addr API failure", myname);
}
}
/* find_dnsxl_domain - reject if domain in real-time blackhole list */
static const SMTPD_RBL_STATE *find_dnsxl_domain(SMTPD_STATE *state,
const char *rbl_domain, const char *what)
{
VSTRING *query;
SMTPD_RBL_STATE *rbl;
const char *domain;
const char *reply_addr;
const char *byte_codes;
const char *suffix;
const char *adomain;
/*
* Extract the domain, tack on the RBL domain name and query the DNS for
* an A record.
*/
if ((domain = strrchr(what, '@')) != 0) {
domain += 1;
if (domain[0] == '[')
return (SMTPD_CHECK_DUNNO);
} else
domain = what;
/*
* XXX Some Spamhaus RHSBL rejects lookups with "No IP queries" even if
* the name has an alphanumerical prefix. We play safe, and skip both
* RHSBL and RHSWL queries for names ending in a numerical suffix.
*/
if (domain[0] == 0)
return (SMTPD_CHECK_DUNNO);
suffix = strrchr(domain, '.');
if (alldig(suffix == 0 ? domain : suffix + 1))
return (SMTPD_CHECK_DUNNO);
/*
* Fix 20140706: convert domain to ASCII.
*/
#ifndef NO_EAI
if (!allascii(domain) && (adomain = midna_domain_to_ascii(domain)) != 0) {
if (msg_verbose)
msg_info("%s asciified to %s", domain, adomain);
domain = adomain;
}
#endif
if (domain[0] == 0 || valid_hostname(domain, DONT_GRIPE) == 0)
return (SMTPD_CHECK_DUNNO);
query = vstring_alloc(100);
vstring_sprintf(query, "%s.%s", domain, rbl_domain);
reply_addr = split_at(STR(query), '=');
rbl = (SMTPD_RBL_STATE *) ctable_locate(smtpd_rbl_cache, STR(query));
if (reply_addr != 0)
byte_codes = ctable_locate(smtpd_rbl_byte_cache, reply_addr);
/*
* If the record exists, match the result address.
*/
if (SMTPD_DNSXL_STAT_OK(rbl) && reply_addr != 0
&& !rbl_match_addr(rbl, byte_codes))
rbl = 0;
vstring_free(query);
return (rbl);
}
/* reject_rbl_domain - reject if domain in real-time blackhole list */
static int reject_rbl_domain(SMTPD_STATE *state, const char *rbl_domain,
const char *what, const char *reply_class)
{
const char *myname = "reject_rbl_domain";
const SMTPD_RBL_STATE *rbl;
if (msg_verbose)
msg_info("%s: %s %s", myname, rbl_domain, what);
rbl = find_dnsxl_domain(state, rbl_domain, what);
if (!SMTPD_DNSXL_STAT_OK(rbl)) {
return (SMTPD_CHECK_DUNNO);
} else {
return (rbl_reject_reply(state, rbl, rbl_domain, what, reply_class));
}
}
/* permit_dnswl_domain - permit domain in DNSWL */
static int permit_dnswl_domain(SMTPD_STATE *state, const char *dnswl_domain,
const char *what, const char *reply_class)
{
const char *myname = "permit_dnswl_domain";
const SMTPD_RBL_STATE *dnswl_result;
if (msg_verbose)
msg_info("%s: %s", myname, what);
/* Safety: don't whitelist unauthorized recipients. */
if (strcmp(state->where, SMTPD_CMD_RCPT) == 0 && state->recipient != 0
&& permit_auth_destination(state, state->recipient) != SMTPD_CHECK_OK)
return (SMTPD_CHECK_DUNNO);
dnswl_result = find_dnsxl_domain(state, dnswl_domain, what);
if (SMTPD_DNXSL_STAT_HARD(dnswl_result)) {
return (SMTPD_CHECK_DUNNO);
} else if (SMTPD_DNSXL_STAT_SOFT(dnswl_result)) {
/* XXX: Make configurable as rhswl_tempfail_action. */
DEFER_IF_REJECT3(state, MAIL_ERROR_POLICY,
450, "4.7.1",
"<%s>: %s rejected: %s",
what, reply_class,
"Service unavailable");
return (SMTPD_CHECK_DUNNO);
} else if (SMTPD_DNSXL_STAT_OK(dnswl_result)) {
return (SMTPD_CHECK_OK);
} else {
/* Future proofing, in case find_dnsxl_addr() result is changed. */
msg_panic("%s: find_dnsxl_addr API failure", myname);
}
}
/* reject_maps_rbl - reject if client address in real-time blackhole list */
static int reject_maps_rbl(SMTPD_STATE *state)
{
const char *myname = "reject_maps_rbl";
char *saved_domains = mystrdup(var_maps_rbl_domains);
char *bp = saved_domains;
char *rbl_domain;
int result = SMTPD_CHECK_DUNNO;
static int warned;
if (msg_verbose)
msg_info("%s: %s", myname, state->addr);
if (warned == 0) {
warned++;
msg_warn("support for restriction \"%s\" will be removed from %s; "
"use \"%s domain-name\" instead",
REJECT_MAPS_RBL, var_mail_name, REJECT_RBL_CLIENT);
}
while ((rbl_domain = mystrtok(&bp, CHARS_COMMA_SP)) != 0) {
result = reject_rbl_addr(state, rbl_domain, state->addr,
SMTPD_NAME_CLIENT);
if (result != SMTPD_CHECK_DUNNO)
break;
}
/*
* Clean up.
*/
myfree(saved_domains);
return (result);
}
#ifdef USE_SASL_AUTH
/* reject_auth_sender_login_mismatch - logged in client must own sender address */
static int reject_auth_sender_login_mismatch(SMTPD_STATE *state, const char *sender, int allow_unknown_sender)
{
const RESOLVE_REPLY *reply;
const char *owners;
char *saved_owners;
char *cp;
char *name;
int found = 0;
#define ALLOW_UNKNOWN_SENDER 1
#define FORBID_UNKNOWN_SENDER 0
/*
* Reject if the client is logged in and does not own the sender address.
*/
if (smtpd_sender_login_maps && state->sasl_username) {
reply = smtpd_resolve_addr(state->recipient, sender);
if (reply->flags & RESOLVE_FLAG_FAIL)
reject_dict_retry(state, sender);
if ((owners = check_mail_addr_find(state, sender, smtpd_sender_login_maps,
STR(reply->recipient), (char **) 0)) != 0) {
cp = saved_owners = mystrdup(owners);
while ((name = mystrtok(&cp, CHARS_COMMA_SP)) != 0) {
if (strcasecmp_utf8(state->sasl_username, name) == 0) {
found = 1;
break;
}
}
myfree(saved_owners);
} else if (allow_unknown_sender)
return (SMTPD_CHECK_DUNNO);
if (!found)
return (smtpd_check_reject(state, MAIL_ERROR_POLICY, 553, "5.7.1",
"<%s>: Sender address rejected: not owned by user %s",
sender, state->sasl_username));
}
return (SMTPD_CHECK_DUNNO);
}
/* reject_unauth_sender_login_mismatch - sender requires client is logged in */
static int reject_unauth_sender_login_mismatch(SMTPD_STATE *state, const char *sender)
{
const RESOLVE_REPLY *reply;
/*
* Reject if the client is not logged in and the sender address has an
* owner.
*/
if (smtpd_sender_login_maps && !state->sasl_username) {
reply = smtpd_resolve_addr(state->recipient, sender);
if (reply->flags & RESOLVE_FLAG_FAIL)
reject_dict_retry(state, sender);
if (check_mail_addr_find(state, sender, smtpd_sender_login_maps,
STR(reply->recipient), (char **) 0) != 0)
return (smtpd_check_reject(state, MAIL_ERROR_POLICY, 553, "5.7.1",
"<%s>: Sender address rejected: not logged in", sender));
}
return (SMTPD_CHECK_DUNNO);
}
#endif
/* valid_utf8_action - validate UTF-8 policy server response */
static int valid_utf8_action(const char *server, const char *action)
{
int retval;
if ((retval = valid_utf8_string(action, strlen(action))) == 0)
msg_warn("malformed UTF-8 in policy server %s response: \"%s\"",
server, action);
return (retval);
}
/* check_policy_service - check delegated policy service */
static int check_policy_service(SMTPD_STATE *state, const char *server,
const char *reply_name, const char *reply_class,
const char *def_acl)
{
static VSTRING *action = 0;
SMTPD_POLICY_CLNT *policy_clnt;
#ifdef USE_TLS
VSTRING *subject_buf;
VSTRING *issuer_buf;
const char *subject;
const char *issuer;
#endif
int ret;
/*
* Sanity check.
*/
if (!policy_clnt_table
|| (policy_clnt = (SMTPD_POLICY_CLNT *)
htable_find(policy_clnt_table, server)) == 0)
msg_panic("check_policy_service: no client endpoint for server %s",
server);
/*
* Initialize.
*/
if (action == 0)
action = vstring_alloc(10);
#ifdef USE_TLS
#define ENCODE_CN(coded_CN, coded_CN_buf, CN) do { \
if (!TLS_CERT_IS_TRUSTED(state->tls_context) || *(CN) == 0) { \
coded_CN_buf = 0; \
coded_CN = ""; \
} else { \
coded_CN_buf = vstring_alloc(strlen(CN) + 1); \
xtext_quote(coded_CN_buf, CN, ""); \
coded_CN = STR(coded_CN_buf); \
} \
} while (0);
ENCODE_CN(subject, subject_buf, state->tls_context->peer_CN);
ENCODE_CN(issuer, issuer_buf, state->tls_context->issuer_CN);
#endif
if (attr_clnt_request(policy_clnt->client,
ATTR_FLAG_NONE, /* Query attributes. */
SEND_ATTR_STR(MAIL_ATTR_REQ, "smtpd_access_policy"),
SEND_ATTR_STR(MAIL_ATTR_PROTO_STATE,
STREQ(state->where, SMTPD_CMD_BDAT) ?
SMTPD_CMD_DATA : state->where),
SEND_ATTR_STR(MAIL_ATTR_ACT_PROTO_NAME, state->protocol),
SEND_ATTR_STR(MAIL_ATTR_ACT_CLIENT_ADDR, state->addr),
SEND_ATTR_STR(MAIL_ATTR_ACT_CLIENT_NAME, state->name),
SEND_ATTR_STR(MAIL_ATTR_ACT_CLIENT_PORT, state->port),
SEND_ATTR_STR(MAIL_ATTR_ACT_REVERSE_CLIENT_NAME,
state->reverse_name),
SEND_ATTR_STR(MAIL_ATTR_ACT_SERVER_ADDR,
state->dest_addr),
SEND_ATTR_STR(MAIL_ATTR_ACT_SERVER_PORT,
state->dest_port),
SEND_ATTR_STR(MAIL_ATTR_ACT_HELO_NAME,
state->helo_name ? state->helo_name : ""),
SEND_ATTR_STR(MAIL_ATTR_SENDER,
state->sender ? state->sender : ""),
SEND_ATTR_STR(MAIL_ATTR_RECIP,
state->recipient ? state->recipient : ""),
SEND_ATTR_INT(MAIL_ATTR_RCPT_COUNT,
((strcasecmp(state->where, SMTPD_CMD_DATA) == 0) ||
(strcasecmp(state->where, SMTPD_CMD_BDAT) == 0) ||
(strcasecmp(state->where, SMTPD_AFTER_EOM) == 0)) ?
state->rcpt_count : 0),
SEND_ATTR_STR(MAIL_ATTR_QUEUEID,
state->queue_id ? state->queue_id : ""),
SEND_ATTR_STR(MAIL_ATTR_INSTANCE,
STR(state->instance)),
SEND_ATTR_LONG(MAIL_ATTR_SIZE,
(unsigned long) (state->act_size > 0 ?
state->act_size : state->msg_size)),
SEND_ATTR_STR(MAIL_ATTR_ETRN_DOMAIN,
state->etrn_name ? state->etrn_name : ""),
SEND_ATTR_STR(MAIL_ATTR_STRESS, var_stress),
#ifdef USE_SASL_AUTH
SEND_ATTR_STR(MAIL_ATTR_SASL_METHOD,
state->sasl_method ? state->sasl_method : ""),
SEND_ATTR_STR(MAIL_ATTR_SASL_USERNAME,
state->sasl_username ? state->sasl_username : ""),
SEND_ATTR_STR(MAIL_ATTR_SASL_SENDER,
state->sasl_sender ? state->sasl_sender : ""),
#endif
#ifdef USE_TLS
#define IF_ENCRYPTED(x, y) ((state->tls_context && ((x) != 0)) ? (x) : (y))
SEND_ATTR_STR(MAIL_ATTR_CCERT_SUBJECT, subject),
SEND_ATTR_STR(MAIL_ATTR_CCERT_ISSUER, issuer),
/*
* When directly checking the fingerprint, it is OK if the issuing CA is
* not trusted.
*/
SEND_ATTR_STR(MAIL_ATTR_CCERT_CERT_FPRINT,
IF_ENCRYPTED(state->tls_context->peer_cert_fprint, "")),
SEND_ATTR_STR(MAIL_ATTR_CCERT_PKEY_FPRINT,
IF_ENCRYPTED(state->tls_context->peer_pkey_fprint, "")),
SEND_ATTR_STR(MAIL_ATTR_CRYPTO_PROTOCOL,
IF_ENCRYPTED(state->tls_context->protocol, "")),
SEND_ATTR_STR(MAIL_ATTR_CRYPTO_CIPHER,
IF_ENCRYPTED(state->tls_context->cipher_name, "")),
SEND_ATTR_INT(MAIL_ATTR_CRYPTO_KEYSIZE,
IF_ENCRYPTED(state->tls_context->cipher_usebits, 0)),
#endif
SEND_ATTR_STR(MAIL_ATTR_POL_CONTEXT,
policy_clnt->policy_context),
ATTR_TYPE_END,
ATTR_FLAG_MISSING, /* Reply attributes. */
RECV_ATTR_STR(MAIL_ATTR_ACTION, action),
ATTR_TYPE_END) != 1
|| (var_smtputf8_enable && valid_utf8_action(server, STR(action)) == 0)) {
NOCLOBBER static int nesting_level = 0;
jmp_buf savebuf;
int status;
/*
* Safety to prevent recursive execution of the default action.
*/
nesting_level += 1;
memcpy(ADDROF(savebuf), ADDROF(smtpd_check_buf), sizeof(savebuf));
status = setjmp(smtpd_check_buf);
if (status != 0) {
nesting_level -= 1;
memcpy(ADDROF(smtpd_check_buf), ADDROF(savebuf),
sizeof(smtpd_check_buf));
longjmp(smtpd_check_buf, status);
}
ret = check_table_result(state, server, nesting_level == 1 ?
policy_clnt->def_action :
DEF_SMTPD_POLICY_DEF_ACTION,
"policy query", reply_name,
reply_class, def_acl);
nesting_level -= 1;
memcpy(ADDROF(smtpd_check_buf), ADDROF(savebuf),
sizeof(smtpd_check_buf));
} else {
/*
* XXX This produces bogus error messages when the reply is
* malformed.
*/
ret = check_table_result(state, server, STR(action),
"policy query", reply_name,
reply_class, def_acl);
}
#ifdef USE_TLS
if (subject_buf)
vstring_free(subject_buf);
if (issuer_buf)
vstring_free(issuer_buf);
#endif
return (ret);
}
/* is_map_command - restriction has form: check_xxx_access type:name */
static int is_map_command(SMTPD_STATE *state, const char *name,
const char *command, char ***argp)
{
/*
* This is a three-valued function: (a) this is not a check_xxx_access
* command, (b) this is a malformed check_xxx_access command, (c) this is
* a well-formed check_xxx_access command. That's too clumsy for function
* result values, so we use regular returns for (a) and (c), and use long
* jumps for the error case (b).
*/
if (strcasecmp(name, command) != 0) {
return (0);
} else if (*(*argp + 1) == 0 || strchr(*(*argp += 1), ':') == 0) {
msg_warn("restriction %s: bad argument \"%s\": need maptype:mapname",
command, **argp);
reject_server_error(state);
} else {
return (1);
}
}
/* forbid_whitelist - disallow whitelisting */
static void forbid_whitelist(SMTPD_STATE *state, const char *name,
int status, const char *target)
{
if (state->discard == 0 && status == SMTPD_CHECK_OK) {
msg_warn("restriction %s returns OK for %s", name, target);
msg_warn("this is not allowed for security reasons");
msg_warn("use DUNNO instead of OK if you want to make an exception");
reject_server_error(state);
}
}
/* generic_checks - generic restrictions */
static int generic_checks(SMTPD_STATE *state, ARGV *restrictions,
const char *reply_name,
const char *reply_class,
const char *def_acl)
{
const char *myname = "generic_checks";
char **cpp;
const char *name;
int status = 0;
ARGV *list;
int found;
int saved_recursion = state->recursion++;
if (msg_verbose)
msg_info(">>> START %s RESTRICTIONS <<<", reply_class);
for (cpp = restrictions->argv; (name = *cpp) != 0; cpp++) {
if (state->discard != 0)
break;
if (msg_verbose)
msg_info("%s: name=%s", myname, name);
/*
* Pseudo restrictions.
*/
if (strcasecmp(name, WARN_IF_REJECT) == 0) {
if (state->warn_if_reject == 0)
state->warn_if_reject = state->recursion;
continue;
}
/*
* Spoof the is_map_command() routine, so that we do not have to make
* special cases for the implicit short-hand access map notation.
*/
#define NO_DEF_ACL 0
if (strchr(name, ':') != 0) {
if (def_acl == NO_DEF_ACL) {
msg_warn("specify one of (%s, %s, %s, %s, %s, %s) before %s restriction \"%s\"",
CHECK_CLIENT_ACL, CHECK_REVERSE_CLIENT_ACL, CHECK_HELO_ACL, CHECK_SENDER_ACL,
CHECK_RECIP_ACL, CHECK_ETRN_ACL, reply_class, name);
reject_server_error(state);
}
name = def_acl;
cpp -= 1;
}
/*
* Generic restrictions.
*/
if (strcasecmp(name, PERMIT_ALL) == 0) {
status = smtpd_acl_permit(state, name, reply_class,
reply_name, NO_PRINT_ARGS);
if (status == SMTPD_CHECK_OK && cpp[1] != 0)
msg_warn("restriction `%s' after `%s' is ignored",
cpp[1], PERMIT_ALL);
} else if (strcasecmp(name, DEFER_ALL) == 0) {
status = smtpd_check_reject(state, MAIL_ERROR_POLICY,
var_defer_code, "4.3.2",
"<%s>: %s rejected: Try again later",
reply_name, reply_class);
if (cpp[1] != 0 && state->warn_if_reject == 0)
msg_warn("restriction `%s' after `%s' is ignored",
cpp[1], DEFER_ALL);
} else if (strcasecmp(name, REJECT_ALL) == 0) {
status = smtpd_check_reject(state, MAIL_ERROR_POLICY,
var_reject_code, "5.7.1",
"<%s>: %s rejected: Access denied",
reply_name, reply_class);
if (cpp[1] != 0 && state->warn_if_reject == 0)
msg_warn("restriction `%s' after `%s' is ignored",
cpp[1], REJECT_ALL);
} else if (strcasecmp(name, REJECT_UNAUTH_PIPE) == 0) {
status = reject_unauth_pipelining(state, reply_name, reply_class);
} else if (strcasecmp(name, CHECK_POLICY_SERVICE) == 0) {
if (cpp[1] == 0 || strchr(cpp[1], ':') == 0) {
msg_warn("restriction %s must be followed by transport:server",
CHECK_POLICY_SERVICE);
reject_server_error(state);
} else
status = check_policy_service(state, *++cpp, reply_name,
reply_class, def_acl);
} else if (strcasecmp(name, DEFER_IF_PERMIT) == 0) {
status = DEFER_IF_PERMIT2(DEFER_IF_PERMIT_ACT,
state, MAIL_ERROR_POLICY,
450, "4.7.0",
"<%s>: %s rejected: defer_if_permit requested",
reply_name, reply_class);
} else if (strcasecmp(name, DEFER_IF_REJECT) == 0) {
DEFER_IF_REJECT2(state, MAIL_ERROR_POLICY,
450, "4.7.0",
"<%s>: %s rejected: defer_if_reject requested",
reply_name, reply_class);
} else if (strcasecmp(name, SLEEP) == 0) {
if (cpp[1] == 0 || alldig(cpp[1]) == 0) {
msg_warn("restriction %s must be followed by number", SLEEP);
reject_server_error(state);
} else
sleep(atoi(*++cpp));
} else if (strcasecmp(name, REJECT_PLAINTEXT_SESSION) == 0) {
status = reject_plaintext_session(state);
}
/*
* Client name/address restrictions.
*/
else if (strcasecmp(name, REJECT_UNKNOWN_CLIENT_HOSTNAME) == 0
|| strcasecmp(name, REJECT_UNKNOWN_CLIENT) == 0) {
status = reject_unknown_client(state);
} else if (strcasecmp(name, REJECT_UNKNOWN_REVERSE_HOSTNAME) == 0) {
status = reject_unknown_reverse_name(state);
} else if (strcasecmp(name, PERMIT_INET_INTERFACES) == 0) {
status = permit_inet_interfaces(state);
if (status == SMTPD_CHECK_OK)
status = smtpd_acl_permit(state, name, SMTPD_NAME_CLIENT,
state->namaddr, NO_PRINT_ARGS);
} else if (strcasecmp(name, PERMIT_MYNETWORKS) == 0) {
status = permit_mynetworks(state);
if (status == SMTPD_CHECK_OK)
status = smtpd_acl_permit(state, name, SMTPD_NAME_CLIENT,
state->namaddr, NO_PRINT_ARGS);
} else if (is_map_command(state, name, CHECK_CLIENT_ACL, &cpp)) {
status = check_namadr_access(state, *cpp, state->name, state->addr,
FULL, &found, state->namaddr,
SMTPD_NAME_CLIENT, def_acl);
} else if (is_map_command(state, name, CHECK_REVERSE_CLIENT_ACL, &cpp)) {
status = check_namadr_access(state, *cpp, state->reverse_name, state->addr,
FULL, &found, state->reverse_name,
SMTPD_NAME_REV_CLIENT, def_acl);
forbid_whitelist(state, name, status, state->reverse_name);
} else if (strcasecmp(name, REJECT_MAPS_RBL) == 0) {
status = reject_maps_rbl(state);
} else if (strcasecmp(name, REJECT_RBL_CLIENT) == 0
|| strcasecmp(name, REJECT_RBL) == 0) {
if (cpp[1] == 0)
msg_warn("restriction %s requires domain name argument", name);
else
status = reject_rbl_addr(state, *(cpp += 1), state->addr,
SMTPD_NAME_CLIENT);
} else if (strcasecmp(name, PERMIT_DNSWL_CLIENT) == 0) {
if (cpp[1] == 0)
msg_warn("restriction %s requires domain name argument", name);
else {
status = permit_dnswl_addr(state, *(cpp += 1), state->addr,
SMTPD_NAME_CLIENT);
if (status == SMTPD_CHECK_OK)
status = smtpd_acl_permit(state, name, SMTPD_NAME_CLIENT,
state->namaddr, NO_PRINT_ARGS);
}
} else if (strcasecmp(name, REJECT_RHSBL_CLIENT) == 0) {
if (cpp[1] == 0)
msg_warn("restriction %s requires domain name argument",
name);
else {
cpp += 1;
if (strcasecmp(state->name, "unknown") != 0)
status = reject_rbl_domain(state, *cpp, state->name,
SMTPD_NAME_CLIENT);
}
} else if (strcasecmp(name, PERMIT_RHSWL_CLIENT) == 0) {
if (cpp[1] == 0)
msg_warn("restriction %s requires domain name argument",
name);
else {
cpp += 1;
if (strcasecmp(state->name, "unknown") != 0) {
status = permit_dnswl_domain(state, *cpp, state->name,
SMTPD_NAME_CLIENT);
if (status == SMTPD_CHECK_OK)
status = smtpd_acl_permit(state, name,
SMTPD_NAME_CLIENT, state->namaddr, NO_PRINT_ARGS);
}
}
} else if (strcasecmp(name, REJECT_RHSBL_REVERSE_CLIENT) == 0) {
if (cpp[1] == 0)
msg_warn("restriction %s requires domain name argument",
name);
else {
cpp += 1;
if (strcasecmp(state->reverse_name, "unknown") != 0)
status = reject_rbl_domain(state, *cpp, state->reverse_name,
SMTPD_NAME_REV_CLIENT);
}
} else if (is_map_command(state, name, CHECK_CCERT_ACL, &cpp)) {
status = check_ccert_access(state, *cpp, def_acl);
} else if (is_map_command(state, name, CHECK_SASL_ACL, &cpp)) {
#ifdef USE_SASL_AUTH
if (var_smtpd_sasl_enable) {
if (state->sasl_username && state->sasl_username[0])
status = check_sasl_access(state, *cpp, def_acl);
} else
#endif
msg_warn("restriction `%s' ignored: no SASL support", name);
} else if (is_map_command(state, name, CHECK_CLIENT_NS_ACL, &cpp)) {
if (strcasecmp(state->name, "unknown") != 0) {
status = check_server_access(state, *cpp, state->name,
T_NS, state->namaddr,
SMTPD_NAME_CLIENT, def_acl);
forbid_whitelist(state, name, status, state->name);
}
} else if (is_map_command(state, name, CHECK_CLIENT_MX_ACL, &cpp)) {
if (strcasecmp(state->name, "unknown") != 0) {
status = check_server_access(state, *cpp, state->name,
T_MX, state->namaddr,
SMTPD_NAME_CLIENT, def_acl);
forbid_whitelist(state, name, status, state->name);
}
} else if (is_map_command(state, name, CHECK_CLIENT_A_ACL, &cpp)) {
if (strcasecmp(state->name, "unknown") != 0) {
status = check_server_access(state, *cpp, state->name,
T_A, state->namaddr,
SMTPD_NAME_CLIENT, def_acl);
forbid_whitelist(state, name, status, state->name);
}
} else if (is_map_command(state, name, CHECK_REVERSE_CLIENT_NS_ACL, &cpp)) {
if (strcasecmp(state->reverse_name, "unknown") != 0) {
status = check_server_access(state, *cpp, state->reverse_name,
T_NS, state->reverse_name,
SMTPD_NAME_REV_CLIENT, def_acl);
forbid_whitelist(state, name, status, state->reverse_name);
}
} else if (is_map_command(state, name, CHECK_REVERSE_CLIENT_MX_ACL, &cpp)) {
if (strcasecmp(state->reverse_name, "unknown") != 0) {
status = check_server_access(state, *cpp, state->reverse_name,
T_MX, state->reverse_name,
SMTPD_NAME_REV_CLIENT, def_acl);
forbid_whitelist(state, name, status, state->reverse_name);
}
} else if (is_map_command(state, name, CHECK_REVERSE_CLIENT_A_ACL, &cpp)) {
if (strcasecmp(state->reverse_name, "unknown") != 0) {
status = check_server_access(state, *cpp, state->reverse_name,
T_A, state->reverse_name,
SMTPD_NAME_REV_CLIENT, def_acl);
forbid_whitelist(state, name, status, state->reverse_name);
}
}
/*
* HELO/EHLO parameter restrictions.
*/
else if (is_map_command(state, name, CHECK_HELO_ACL, &cpp)) {
if (state->helo_name)
status = check_domain_access(state, *cpp, state->helo_name,
FULL, &found, state->helo_name,
SMTPD_NAME_HELO, def_acl);
} else if (strcasecmp(name, REJECT_INVALID_HELO_HOSTNAME) == 0
|| strcasecmp(name, REJECT_INVALID_HOSTNAME) == 0) {
if (state->helo_name) {
if (*state->helo_name != '[')
status = reject_invalid_hostname(state, state->helo_name,
state->helo_name, SMTPD_NAME_HELO);
else
status = reject_invalid_hostaddr(state, state->helo_name,
state->helo_name, SMTPD_NAME_HELO);
}
} else if (strcasecmp(name, REJECT_UNKNOWN_HELO_HOSTNAME) == 0
|| strcasecmp(name, REJECT_UNKNOWN_HOSTNAME) == 0) {
if (state->helo_name) {
if (*state->helo_name != '[')
status = reject_unknown_hostname(state, state->helo_name,
state->helo_name, SMTPD_NAME_HELO);
else
status = reject_invalid_hostaddr(state, state->helo_name,
state->helo_name, SMTPD_NAME_HELO);
}
} else if (strcasecmp(name, PERMIT_NAKED_IP_ADDR) == 0) {
msg_warn("restriction %s is deprecated. Use %s or %s instead",
PERMIT_NAKED_IP_ADDR, PERMIT_MYNETWORKS, PERMIT_SASL_AUTH);
if (state->helo_name) {
if (state->helo_name[strspn(state->helo_name, "0123456789.:")] == 0
&& (status = reject_invalid_hostaddr(state, state->helo_name,
state->helo_name, SMTPD_NAME_HELO)) == 0)
status = smtpd_acl_permit(state, name, SMTPD_NAME_HELO,
state->helo_name, NO_PRINT_ARGS);
}
} else if (is_map_command(state, name, CHECK_HELO_NS_ACL, &cpp)) {
if (state->helo_name) {
status = check_server_access(state, *cpp, state->helo_name,
T_NS, state->helo_name,
SMTPD_NAME_HELO, def_acl);
forbid_whitelist(state, name, status, state->helo_name);
}
} else if (is_map_command(state, name, CHECK_HELO_MX_ACL, &cpp)) {
if (state->helo_name) {
status = check_server_access(state, *cpp, state->helo_name,
T_MX, state->helo_name,
SMTPD_NAME_HELO, def_acl);
forbid_whitelist(state, name, status, state->helo_name);
}
} else if (is_map_command(state, name, CHECK_HELO_A_ACL, &cpp)) {
if (state->helo_name) {
status = check_server_access(state, *cpp, state->helo_name,
T_A, state->helo_name,
SMTPD_NAME_HELO, def_acl);
forbid_whitelist(state, name, status, state->helo_name);
}
} else if (strcasecmp(name, REJECT_NON_FQDN_HELO_HOSTNAME) == 0
|| strcasecmp(name, REJECT_NON_FQDN_HOSTNAME) == 0) {
if (state->helo_name) {
if (*state->helo_name != '[')
status = reject_non_fqdn_hostname(state, state->helo_name,
state->helo_name, SMTPD_NAME_HELO);
else
status = reject_invalid_hostaddr(state, state->helo_name,
state->helo_name, SMTPD_NAME_HELO);
}
} else if (strcasecmp(name, REJECT_RHSBL_HELO) == 0) {
if (cpp[1] == 0)
msg_warn("restriction %s requires domain name argument",
name);
else {
cpp += 1;
if (state->helo_name)
status = reject_rbl_domain(state, *cpp, state->helo_name,
SMTPD_NAME_HELO);
}
}
/*
* Sender mail address restrictions.
*/
else if (is_map_command(state, name, CHECK_SENDER_ACL, &cpp)) {
if (state->sender && *state->sender)
status = check_mail_access(state, *cpp, state->sender,
&found, state->sender,
SMTPD_NAME_SENDER, def_acl);
if (state->sender && !*state->sender)
status = check_access(state, *cpp, var_smtpd_null_key, FULL,
&found, state->sender,
SMTPD_NAME_SENDER, def_acl);
} else if (strcasecmp(name, REJECT_UNKNOWN_ADDRESS) == 0) {
if (state->sender && *state->sender)
status = reject_unknown_address(state, state->sender,
state->sender, SMTPD_NAME_SENDER);
} else if (strcasecmp(name, REJECT_UNKNOWN_SENDDOM) == 0) {
if (state->sender && *state->sender)
status = reject_unknown_address(state, state->sender,
state->sender, SMTPD_NAME_SENDER);
} else if (strcasecmp(name, REJECT_UNVERIFIED_SENDER) == 0) {
if (state->sender && *state->sender)
status = reject_unverified_address(state, state->sender,
state->sender, SMTPD_NAME_SENDER,
var_unv_from_dcode, var_unv_from_rcode,
unv_from_tf_act,
var_unv_from_why);
} else if (strcasecmp(name, REJECT_NON_FQDN_SENDER) == 0) {
if (state->sender && *state->sender)
status = reject_non_fqdn_address(state, state->sender,
state->sender, SMTPD_NAME_SENDER);
} else if (strcasecmp(name, REJECT_AUTH_SENDER_LOGIN_MISMATCH) == 0) {
#ifdef USE_SASL_AUTH
if (var_smtpd_sasl_enable) {
if (state->sender && *state->sender)
status = reject_auth_sender_login_mismatch(state,
state->sender, FORBID_UNKNOWN_SENDER);
} else
#endif
msg_warn("restriction `%s' ignored: no SASL support", name);
} else if (strcasecmp(name, REJECT_KNOWN_SENDER_LOGIN_MISMATCH) == 0) {
#ifdef USE_SASL_AUTH
if (var_smtpd_sasl_enable) {
if (state->sender && *state->sender) {
if (state->sasl_username)
status = reject_auth_sender_login_mismatch(state,
state->sender, ALLOW_UNKNOWN_SENDER);
else
status = reject_unauth_sender_login_mismatch(state, state->sender);
}
} else
#endif
msg_warn("restriction `%s' ignored: no SASL support", name);
} else if (strcasecmp(name, REJECT_UNAUTH_SENDER_LOGIN_MISMATCH) == 0) {
#ifdef USE_SASL_AUTH
if (var_smtpd_sasl_enable) {
if (state->sender && *state->sender)
status = reject_unauth_sender_login_mismatch(state, state->sender);
} else
#endif
msg_warn("restriction `%s' ignored: no SASL support", name);
} else if (is_map_command(state, name, CHECK_SENDER_NS_ACL, &cpp)) {
if (state->sender && *state->sender) {
status = check_server_access(state, *cpp, state->sender,
T_NS, state->sender,
SMTPD_NAME_SENDER, def_acl);
forbid_whitelist(state, name, status, state->sender);
}
} else if (is_map_command(state, name, CHECK_SENDER_MX_ACL, &cpp)) {
if (state->sender && *state->sender) {
status = check_server_access(state, *cpp, state->sender,
T_MX, state->sender,
SMTPD_NAME_SENDER, def_acl);
forbid_whitelist(state, name, status, state->sender);
}
} else if (is_map_command(state, name, CHECK_SENDER_A_ACL, &cpp)) {
if (state->sender && *state->sender) {
status = check_server_access(state, *cpp, state->sender,
T_A, state->sender,
SMTPD_NAME_SENDER, def_acl);
forbid_whitelist(state, name, status, state->sender);
}
} else if (strcasecmp(name, REJECT_RHSBL_SENDER) == 0) {
if (cpp[1] == 0)
msg_warn("restriction %s requires domain name argument", name);
else {
cpp += 1;
if (state->sender && *state->sender)
status = reject_rbl_domain(state, *cpp, state->sender,
SMTPD_NAME_SENDER);
}
} else if (strcasecmp(name, REJECT_UNLISTED_SENDER) == 0) {
if (state->sender && *state->sender)
status = check_sender_rcpt_maps(state, state->sender);
}
/*
* Recipient mail address restrictions.
*/
else if (is_map_command(state, name, CHECK_RECIP_ACL, &cpp)) {
if (state->recipient)
status = check_mail_access(state, *cpp, state->recipient,
&found, state->recipient,
SMTPD_NAME_RECIPIENT, def_acl);
} else if (strcasecmp(name, PERMIT_MX_BACKUP) == 0) {
if (state->recipient) {
status = permit_mx_backup(state, state->recipient,
state->recipient, SMTPD_NAME_RECIPIENT);
if (status == SMTPD_CHECK_OK)
status = smtpd_acl_permit(state, name, SMTPD_NAME_RECIPIENT,
state->recipient, NO_PRINT_ARGS);
}
} else if (strcasecmp(name, PERMIT_AUTH_DEST) == 0) {
if (state->recipient) {
status = permit_auth_destination(state, state->recipient);
if (status == SMTPD_CHECK_OK)
status = smtpd_acl_permit(state, name, SMTPD_NAME_RECIPIENT,
state->recipient, NO_PRINT_ARGS);
}
} else if (strcasecmp(name, REJECT_UNAUTH_DEST) == 0) {
if (state->recipient)
status = reject_unauth_destination(state, state->recipient,
var_relay_code, "5.7.1");
} else if (strcasecmp(name, DEFER_UNAUTH_DEST) == 0) {
if (state->recipient)
status = reject_unauth_destination(state, state->recipient,
var_relay_code - 100, "4.7.1");
} else if (strcasecmp(name, CHECK_RELAY_DOMAINS) == 0) {
if (state->recipient)
status = check_relay_domains(state, state->recipient,
state->recipient, SMTPD_NAME_RECIPIENT);
if (status == SMTPD_CHECK_OK)
status = smtpd_acl_permit(state, name, SMTPD_NAME_RECIPIENT,
state->recipient, NO_PRINT_ARGS);
if (cpp[1] != 0 && state->warn_if_reject == 0)
msg_warn("restriction `%s' after `%s' is ignored",
cpp[1], CHECK_RELAY_DOMAINS);
} else if (strcasecmp(name, PERMIT_SASL_AUTH) == 0) {
#ifdef USE_SASL_AUTH
if (smtpd_sasl_is_active(state)) {
status = permit_sasl_auth(state,
SMTPD_CHECK_OK, SMTPD_CHECK_DUNNO);
if (status == SMTPD_CHECK_OK)
status = smtpd_acl_permit(state, name, SMTPD_NAME_CLIENT,
state->namaddr, NO_PRINT_ARGS);
}
#endif
} else if (strcasecmp(name, PERMIT_TLS_ALL_CLIENTCERTS) == 0) {
status = permit_tls_clientcerts(state, 1);
if (status == SMTPD_CHECK_OK)
status = smtpd_acl_permit(state, name, SMTPD_NAME_CLIENT,
state->namaddr, NO_PRINT_ARGS);
} else if (strcasecmp(name, PERMIT_TLS_CLIENTCERTS) == 0) {
status = permit_tls_clientcerts(state, 0);
if (status == SMTPD_CHECK_OK)
status = smtpd_acl_permit(state, name, SMTPD_NAME_CLIENT,
state->namaddr, NO_PRINT_ARGS);
} else if (strcasecmp(name, REJECT_UNKNOWN_RCPTDOM) == 0) {
if (state->recipient)
status = reject_unknown_address(state, state->recipient,
state->recipient, SMTPD_NAME_RECIPIENT);
} else if (strcasecmp(name, REJECT_NON_FQDN_RCPT) == 0) {
if (state->recipient)
status = reject_non_fqdn_address(state, state->recipient,
state->recipient, SMTPD_NAME_RECIPIENT);
} else if (is_map_command(state, name, CHECK_RECIP_NS_ACL, &cpp)) {
if (state->recipient && *state->recipient) {
status = check_server_access(state, *cpp, state->recipient,
T_NS, state->recipient,
SMTPD_NAME_RECIPIENT, def_acl);
forbid_whitelist(state, name, status, state->recipient);
}
} else if (is_map_command(state, name, CHECK_RECIP_MX_ACL, &cpp)) {
if (state->recipient && *state->recipient) {
status = check_server_access(state, *cpp, state->recipient,
T_MX, state->recipient,
SMTPD_NAME_RECIPIENT, def_acl);
forbid_whitelist(state, name, status, state->recipient);
}
} else if (is_map_command(state, name, CHECK_RECIP_A_ACL, &cpp)) {
if (state->recipient && *state->recipient) {
status = check_server_access(state, *cpp, state->recipient,
T_A, state->recipient,
SMTPD_NAME_RECIPIENT, def_acl);
forbid_whitelist(state, name, status, state->recipient);
}
} else if (strcasecmp(name, REJECT_RHSBL_RECIPIENT) == 0) {
if (cpp[1] == 0)
msg_warn("restriction %s requires domain name argument", name);
else {
cpp += 1;
if (state->recipient)
status = reject_rbl_domain(state, *cpp, state->recipient,
SMTPD_NAME_RECIPIENT);
}
} else if (strcasecmp(name, CHECK_RCPT_MAPS) == 0
|| strcasecmp(name, REJECT_UNLISTED_RCPT) == 0) {
if (state->recipient && *state->recipient)
status = check_recipient_rcpt_maps(state, state->recipient);
} else if (strcasecmp(name, REJECT_MUL_RCPT_BOUNCE) == 0) {
if (state->sender && *state->sender == 0 && state->rcpt_count
> (strcmp(state->where, SMTPD_CMD_RCPT) != 0))
status = smtpd_check_reject(state, MAIL_ERROR_POLICY,
var_mul_rcpt_code, "5.5.3",
"<%s>: %s rejected: Multi-recipient bounce",
reply_name, reply_class);
} else if (strcasecmp(name, REJECT_UNVERIFIED_RECIP) == 0) {
if (state->recipient && *state->recipient)
status = reject_unverified_address(state, state->recipient,
state->recipient, SMTPD_NAME_RECIPIENT,
var_unv_rcpt_dcode, var_unv_rcpt_rcode,
unv_rcpt_tf_act,
var_unv_rcpt_why);
}
/*
* ETRN domain name restrictions.
*/
else if (is_map_command(state, name, CHECK_ETRN_ACL, &cpp)) {
if (state->etrn_name)
status = check_domain_access(state, *cpp, state->etrn_name,
FULL, &found, state->etrn_name,
SMTPD_NAME_ETRN, def_acl);
}
/*
* User-defined restriction class.
*/
else if ((list = (ARGV *) htable_find(smtpd_rest_classes, name)) != 0) {
status = generic_checks(state, list, reply_name,
reply_class, def_acl);
}
/*
* Error: undefined restriction name.
*/
else {
msg_warn("unknown smtpd restriction: \"%s\"", name);
reject_server_error(state);
}
if (msg_verbose)
msg_info("%s: name=%s status=%d", myname, name, status);
if (status < 0) {
if (status == DICT_ERR_RETRY)
reject_dict_retry(state, reply_name);
else
reject_server_error(state);
}
if (state->warn_if_reject >= state->recursion)
state->warn_if_reject = 0;
if (status != 0)
break;
if (state->defer_if_permit.active && state->defer_if_reject.active)
break;
}
if (msg_verbose)
msg_info(">>> END %s RESTRICTIONS <<<", reply_class);
state->recursion = saved_recursion;
/* In case the list terminated with one or more warn_if_mumble. */
if (state->warn_if_reject >= state->recursion)
state->warn_if_reject = 0;
return (status);
}
/* smtpd_check_addr - address sanity check */
int smtpd_check_addr(const char *sender, const char *addr, int smtputf8)
{
const RESOLVE_REPLY *resolve_reply;
const char *myname = "smtpd_check_addr";
const char *domain;
if (msg_verbose)
msg_info("%s: addr=%s", myname, addr);
/*
* Catch syntax errors early on if we can, but be prepared to re-compute
* the result later when the cache fills up with lots of recipients, at
* which time errors can still happen.
*/
if (addr == 0 || *addr == 0)
return (0);
resolve_reply = smtpd_resolve_addr(sender, addr);
if (resolve_reply->flags & RESOLVE_FLAG_ERROR)
return (-1);
/*
* Backwards compatibility: if the client does not request SMTPUTF8
* support, then behave like Postfix < 3.0 trivial-rewrite, and don't
* allow non-ASCII email domains. Historically, Postfix does not reject
* UTF8 etc. in the address localpart.
*/
if (smtputf8 == 0
&& (domain = strrchr(STR(resolve_reply->recipient), '@')) != 0
&& *(domain += 1) != 0 && !allascii(domain))
return (-1);
return (0);
}
/* smtpd_check_rewrite - choose address qualification context */
char *smtpd_check_rewrite(SMTPD_STATE *state)
{
const char *myname = "smtpd_check_rewrite";
int status;
char **cpp;
MAPS *maps;
char *name;
/*
* We don't use generic_checks() because it produces results that aren't
* applicable such as DEFER or REJECT.
*/
for (cpp = local_rewrite_clients->argv; *cpp != 0; cpp++) {
if (msg_verbose)
msg_info("%s: trying: %s", myname, *cpp);
status = SMTPD_CHECK_DUNNO;
if (strchr(name = *cpp, ':') != 0) {
name = CHECK_ADDR_MAP;
cpp -= 1;
}
if (strcasecmp(name, PERMIT_INET_INTERFACES) == 0) {
status = permit_inet_interfaces(state);
} else if (strcasecmp(name, PERMIT_MYNETWORKS) == 0) {
status = permit_mynetworks(state);
} else if (is_map_command(state, name, CHECK_ADDR_MAP, &cpp)) {
if ((maps = (MAPS *) htable_find(map_command_table, *cpp)) == 0)
msg_panic("%s: dictionary not found: %s", myname, *cpp);
if (maps_find(maps, state->addr, 0) != 0)
status = SMTPD_CHECK_OK;
else if (maps->error != 0) {
/* Warning is already logged. */
status = maps->error;
}
} else if (strcasecmp(name, PERMIT_SASL_AUTH) == 0) {
#ifdef USE_SASL_AUTH
if (smtpd_sasl_is_active(state))
status = permit_sasl_auth(state, SMTPD_CHECK_OK,
SMTPD_CHECK_DUNNO);
#endif
} else if (strcasecmp(name, PERMIT_TLS_ALL_CLIENTCERTS) == 0) {
status = permit_tls_clientcerts(state, 1);
} else if (strcasecmp(name, PERMIT_TLS_CLIENTCERTS) == 0) {
status = permit_tls_clientcerts(state, 0);
} else {
msg_warn("parameter %s: invalid request: %s",
VAR_LOC_RWR_CLIENTS, name);
continue;
}
if (status < 0) {
if (status == DICT_ERR_RETRY) {
state->error_mask |= MAIL_ERROR_RESOURCE;
log_whatsup(state, "reject",
"451 4.3.0 Temporary lookup error");
return ("451 4.3.0 Temporary lookup error");
} else {
state->error_mask |= MAIL_ERROR_SOFTWARE;
log_whatsup(state, "reject",
"451 4.3.5 Server configuration error");
return ("451 4.3.5 Server configuration error");
}
}
if (status == SMTPD_CHECK_OK) {
state->rewrite_context = MAIL_ATTR_RWR_LOCAL;
return (0);
}
}
state->rewrite_context = MAIL_ATTR_RWR_REMOTE;
return (0);
}
/* smtpd_check_client - validate client name or address */
char *smtpd_check_client(SMTPD_STATE *state)
{
int status;
/*
* Initialize.
*/
if (state->name == 0 || state->addr == 0)
return (0);
#define SMTPD_CHECK_RESET() { \
state->recursion = 0; \
state->warn_if_reject = 0; \
state->defer_if_reject.active = 0; \
}
/*
* Reset the defer_if_permit flag.
*/
state->defer_if_permit.active = 0;
/*
* Apply restrictions in the order as specified.
*/
SMTPD_CHECK_RESET();
status = setjmp(smtpd_check_buf);
if (status == 0 && client_restrctions->argc)
status = generic_checks(state, client_restrctions, state->namaddr,
SMTPD_NAME_CLIENT, CHECK_CLIENT_ACL);
state->defer_if_permit_client = state->defer_if_permit.active;
return (status == SMTPD_CHECK_REJECT ? STR(error_text) : 0);
}
/* smtpd_check_helo - validate HELO hostname */
char *smtpd_check_helo(SMTPD_STATE *state, char *helohost)
{
int status;
char *saved_helo;
/*
* Initialize.
*/
if (helohost == 0)
return (0);
/*
* Minor kluge so that we can delegate work to the generic routine and so
* that we can syslog the recipient with the reject messages.
*/
#define SMTPD_CHECK_PUSH(backup, current, new) { \
backup = current; \
current = (new ? mystrdup(new) : 0); \
}
#define SMTPD_CHECK_POP(current, backup) { \
if (current) myfree(current); \
current = backup; \
}
SMTPD_CHECK_PUSH(saved_helo, state->helo_name, helohost);
#define SMTPD_CHECK_HELO_RETURN(x) { \
SMTPD_CHECK_POP(state->helo_name, saved_helo); \
return (x); \
}
/*
* Restore the defer_if_permit flag to its value before HELO/EHLO, and do
* not set the flag when it was already raised by a previous protocol
* stage.
*/
state->defer_if_permit.active = state->defer_if_permit_client;
/*
* Apply restrictions in the order as specified.
*/
SMTPD_CHECK_RESET();
status = setjmp(smtpd_check_buf);
if (status == 0 && helo_restrctions->argc)
status = generic_checks(state, helo_restrctions, state->helo_name,
SMTPD_NAME_HELO, CHECK_HELO_ACL);
state->defer_if_permit_helo = state->defer_if_permit.active;
SMTPD_CHECK_HELO_RETURN(status == SMTPD_CHECK_REJECT ? STR(error_text) : 0);
}
/* smtpd_check_mail - validate sender address, driver */
char *smtpd_check_mail(SMTPD_STATE *state, char *sender)
{
int status;
char *saved_sender;
/*
* Initialize.
*/
if (sender == 0)
return (0);
/*
* Minor kluge so that we can delegate work to the generic routine and so
* that we can syslog the recipient with the reject messages.
*/
SMTPD_CHECK_PUSH(saved_sender, state->sender, sender);
#define SMTPD_CHECK_MAIL_RETURN(x) { \
SMTPD_CHECK_POP(state->sender, saved_sender); \
return (x); \
}
/*
* Restore the defer_if_permit flag to its value before MAIL FROM, and do
* not set the flag when it was already raised by a previous protocol
* stage. The client may skip the helo/ehlo.
*/
state->defer_if_permit.active = state->defer_if_permit_client
| state->defer_if_permit_helo;
state->sender_rcptmap_checked = 0;
/*
* Apply restrictions in the order as specified.
*/
SMTPD_CHECK_RESET();
status = setjmp(smtpd_check_buf);
if (status == 0 && mail_restrctions->argc)
status = generic_checks(state, mail_restrctions, sender,
SMTPD_NAME_SENDER, CHECK_SENDER_ACL);
state->defer_if_permit_sender = state->defer_if_permit.active;
/*
* If the "reject_unlisted_sender" restriction still needs to be applied,
* validate the sender here.
*/
if (var_smtpd_rej_unl_from
&& status != SMTPD_CHECK_REJECT && state->sender_rcptmap_checked == 0
&& state->discard == 0 && *sender)
status = check_sender_rcpt_maps(state, sender);
SMTPD_CHECK_MAIL_RETURN(status == SMTPD_CHECK_REJECT ? STR(error_text) : 0);
}
/* smtpd_check_rcpt - validate recipient address, driver */
char *smtpd_check_rcpt(SMTPD_STATE *state, char *recipient)
{
int status;
char *saved_recipient;
char *err;
ARGV *restrctions[2];
int n;
/*
* Initialize.
*/
if (recipient == 0)
return (0);
/*
* XXX 2821: Section 3.6 requires that "postmaster" be accepted even when
* specified without a fully qualified domain name.
*/
if (strcasecmp(recipient, "postmaster") == 0)
return (0);
/*
* Minor kluge so that we can delegate work to the generic routine and so
* that we can syslog the recipient with the reject messages.
*/
SMTPD_CHECK_PUSH(saved_recipient, state->recipient, recipient);
#define SMTPD_CHECK_RCPT_RETURN(x) { \
SMTPD_CHECK_POP(state->recipient, saved_recipient); \
return (x); \
}
/*
* The "check_recipient_maps" restriction is relevant only when
* responding to RCPT TO or VRFY.
*/
state->recipient_rcptmap_checked = 0;
/*
* Apply delayed restrictions.
*/
if (var_smtpd_delay_reject)
if ((err = smtpd_check_client(state)) != 0
|| (err = smtpd_check_helo(state, state->helo_name)) != 0
|| (err = smtpd_check_mail(state, state->sender)) != 0)
SMTPD_CHECK_RCPT_RETURN(err);
/*
* Restore the defer_if_permit flag to its value before RCPT TO, and do
* not set the flag when it was already raised by a previous protocol
* stage.
*/
state->defer_if_permit.active = state->defer_if_permit_sender;
/*
* Apply restrictions in the order as specified. We allow relay
* restrictions to be empty, for sites that require backwards
* compatibility.
*
* If compatibility_level < 1 and smtpd_relay_restrictions is left at its
* default value, find out if the new smtpd_relay_restrictions default
* value would block the request, without logging REJECT messages.
* Approach: evaluate fake relay restrictions (permit_mynetworks,
* permit_sasl_authenticated, permit_auth_destination) and log a warning
* if the result is DUNNO instead of OK, i.e. a reject_unauth_destinatin
* at the end would have blocked the request.
*/
SMTPD_CHECK_RESET();
restrctions[0] = rcpt_restrctions;
restrctions[1] = warn_compat_break_relay_restrictions ?
fake_relay_restrctions : relay_restrctions;
for (n = 0; n < 2; n++) {
status = setjmp(smtpd_check_buf);
if (status == 0 && restrctions[n]->argc)
status = generic_checks(state, restrctions[n],
recipient, SMTPD_NAME_RECIPIENT, CHECK_RECIP_ACL);
if (n == 1 && warn_compat_break_relay_restrictions
&& status == SMTPD_CHECK_DUNNO) {
msg_info("using backwards-compatible default setting \""
VAR_RELAY_CHECKS " = (empty)\" to avoid \"Relay "
"access denied\" error for recipient \"%s\" from "
"client \"%s\"", state->recipient, state->namaddr);
}
if (status == SMTPD_CHECK_REJECT)
break;
}
/*
* Force permission into deferral when some earlier temporary error may
* have prevented us from rejecting mail, and report the earlier problem.
*/
if (status != SMTPD_CHECK_REJECT && state->defer_if_permit.active)
status = smtpd_check_reject(state, state->defer_if_permit.class,
state->defer_if_permit.code,
STR(state->defer_if_permit.dsn),
"%s", STR(state->defer_if_permit.reason));
/*
* If the "reject_unlisted_recipient" restriction still needs to be
* applied, validate the recipient here.
*/
if (var_smtpd_rej_unl_rcpt
&& status != SMTPD_CHECK_REJECT
&& state->recipient_rcptmap_checked == 0
&& state->discard == 0)
status = check_recipient_rcpt_maps(state, recipient);
SMTPD_CHECK_RCPT_RETURN(status == SMTPD_CHECK_REJECT ? STR(error_text) : 0);
}
/* smtpd_check_etrn - validate ETRN request */
char *smtpd_check_etrn(SMTPD_STATE *state, char *domain)
{
int status;
char *saved_etrn_name;
char *err;
/*
* Initialize.
*/
if (domain == 0)
return (0);
/*
* Minor kluge so that we can delegate work to the generic routine and so
* that we can syslog the recipient with the reject messages.
*/
SMTPD_CHECK_PUSH(saved_etrn_name, state->etrn_name, domain);
#define SMTPD_CHECK_ETRN_RETURN(x) { \
SMTPD_CHECK_POP(state->etrn_name, saved_etrn_name); \
return (x); \
}
/*
* Apply delayed restrictions.
*/
if (var_smtpd_delay_reject)
if ((err = smtpd_check_client(state)) != 0
|| (err = smtpd_check_helo(state, state->helo_name)) != 0)
SMTPD_CHECK_ETRN_RETURN(err);
/*
* Restore the defer_if_permit flag to its value before ETRN, and do not
* set the flag when it was already raised by a previous protocol stage.
* The client may skip the helo/ehlo.
*/
state->defer_if_permit.active = state->defer_if_permit_client
| state->defer_if_permit_helo;
/*
* Apply restrictions in the order as specified.
*/
SMTPD_CHECK_RESET();
status = setjmp(smtpd_check_buf);
if (status == 0 && etrn_restrctions->argc)
status = generic_checks(state, etrn_restrctions, domain,
SMTPD_NAME_ETRN, CHECK_ETRN_ACL);
/*
* Force permission into deferral when some earlier temporary error may
* have prevented us from rejecting mail, and report the earlier problem.
*/
if (status != SMTPD_CHECK_REJECT && state->defer_if_permit.active)
status = smtpd_check_reject(state, state->defer_if_permit.class,
state->defer_if_permit.code,
STR(state->defer_if_permit.dsn),
"%s", STR(state->defer_if_permit.reason));
SMTPD_CHECK_ETRN_RETURN(status == SMTPD_CHECK_REJECT ? STR(error_text) : 0);
}
/* check_recipient_rcpt_maps - generic_checks() recipient table check */
static int check_recipient_rcpt_maps(SMTPD_STATE *state, const char *recipient)
{
/*
* Duplicate suppression. There's an implicit check_recipient_maps
* restriction at the end of all recipient restrictions.
*/
if (smtpd_input_transp_mask & INPUT_TRANSP_UNKNOWN_RCPT)
return (0);
if (state->recipient_rcptmap_checked == 1)
return (0);
if (state->warn_if_reject == 0)
/* We really validate the recipient address. */
state->recipient_rcptmap_checked = 1;
return (check_rcpt_maps(state, state->sender, recipient,
SMTPD_NAME_RECIPIENT));
}
/* check_sender_rcpt_maps - generic_checks() sender table check */
static int check_sender_rcpt_maps(SMTPD_STATE *state, const char *sender)
{
/*
* Duplicate suppression. There's an implicit check_sender_maps
* restriction at the end of all sender restrictions.
*/
if (smtpd_input_transp_mask & INPUT_TRANSP_UNKNOWN_RCPT)
return (0);
if (state->sender_rcptmap_checked == 1)
return (0);
if (state->warn_if_reject == 0)
/* We really validate the sender address. */
state->sender_rcptmap_checked = 1;
return (check_rcpt_maps(state, state->recipient, sender,
SMTPD_NAME_SENDER));
}
/* check_rcpt_maps - generic_checks() interface for recipient table check */
static int check_rcpt_maps(SMTPD_STATE *state, const char *sender,
const char *recipient,
const char *reply_class)
{
const RESOLVE_REPLY *reply;
DSN_SPLIT dp;
if (msg_verbose)
msg_info(">>> CHECKING %s VALIDATION MAPS <<<", reply_class);
/*
* Resolve the address.
*/
reply = smtpd_resolve_addr(sender, recipient);
if (reply->flags & RESOLVE_FLAG_FAIL)
reject_dict_retry(state, recipient);
/*
* Make complex expressions more readable?
*/
#define MATCH(map, rcpt) \
check_mail_addr_find(state, recipient, map, rcpt, (char **) 0)
#define NOMATCH(map, rcpt) (MATCH(map, rcpt) == 0)
/*
* XXX We assume the recipient address is OK if it matches a canonical
* map or virtual alias map. Eventually, the address resolver should give
* us the final resolved recipient address, and the SMTP server should
* write the final resolved recipient address to the output record
* stream. See also the next comment block on recipients in virtual alias
* domains.
*/
if (MATCH(rcpt_canon_maps, CONST_STR(reply->recipient))
|| (strcmp(reply_class, SMTPD_NAME_SENDER) == 0
&& MATCH(send_canon_maps, CONST_STR(reply->recipient)))
|| MATCH(canonical_maps, CONST_STR(reply->recipient))
|| MATCH(virt_alias_maps, CONST_STR(reply->recipient)))
return (0);
/*
* At this point, anything that resolves to the error mailer is known to
* be undeliverable.
*
* XXX Until the address resolver does final address resolution, known and
* unknown recipients in virtual alias domains will both resolve to
* "error:user unknown".
*/
if (strcmp(STR(reply->transport), MAIL_SERVICE_ERROR) == 0) {
dsn_split(&dp, strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
"5.1.0" : "5.1.1", STR(reply->nexthop));
return (smtpd_check_reject(state, MAIL_ERROR_BOUNCE,
(reply->flags & RESOLVE_CLASS_ALIAS) ?
var_virt_alias_code : 550,
smtpd_dsn_fix(DSN_STATUS(dp.dsn),
reply_class),
"<%s>: %s rejected: %s",
recipient, reply_class,
dp.text));
}
if (strcmp(STR(reply->transport), MAIL_SERVICE_RETRY) == 0) {
dsn_split(&dp, strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
"4.1.0" : "4.1.1", STR(reply->nexthop));
return (smtpd_check_reject(state, MAIL_ERROR_BOUNCE, 450,
smtpd_dsn_fix(DSN_STATUS(dp.dsn),
reply_class),
"<%s>: %s rejected: %s",
recipient, reply_class,
dp.text));
}
/*
* Search the recipient lookup tables of the respective address class.
*
* XXX Use the less expensive maps_find() (built-in case folding) instead of
* the baroque mail_addr_find(). But then we have to strip the domain and
* deal with address extensions ourselves.
*
* XXX But that would break sites that use the virtual delivery agent for
* local delivery, because the virtual delivery agent requires
* user@domain style addresses in its user database.
*/
#define MATCH_LEFT(l, r, n) \
(strncasecmp_utf8((l), (r), (n)) == 0 && (r)[n] == '@')
switch (reply->flags & RESOLVE_CLASS_MASK) {
/*
* Reject mail to unknown addresses in local domains (domains that
* match $mydestination or ${proxy,inet}_interfaces).
*/
case RESOLVE_CLASS_LOCAL:
if (*var_local_rcpt_maps
/* Generated by bounce, absorbed by qmgr. */
&& !MATCH_LEFT(var_double_bounce_sender, CONST_STR(reply->recipient),
strlen(var_double_bounce_sender))
/* Absorbed by qmgr. */
&& !MATCH_LEFT(MAIL_ADDR_POSTMASTER, CONST_STR(reply->recipient),
strlen(MAIL_ADDR_POSTMASTER))
/* Generated by bounce. */
&& !MATCH_LEFT(MAIL_ADDR_MAIL_DAEMON, CONST_STR(reply->recipient),
strlen(MAIL_ADDR_MAIL_DAEMON))
&& NOMATCH(local_rcpt_maps, CONST_STR(reply->recipient)))
return (smtpd_check_reject(state, MAIL_ERROR_BOUNCE,
var_local_rcpt_code,
strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
"5.1.0" : "5.1.1",
"<%s>: %s rejected: User unknown%s",
recipient, reply_class,
var_show_unk_rcpt_table ?
" in local recipient table" : ""));
break;
/*
* Reject mail to unknown addresses in virtual mailbox domains.
*/
case RESOLVE_CLASS_VIRTUAL:
if (*var_virt_mailbox_maps
&& NOMATCH(virt_mailbox_maps, CONST_STR(reply->recipient)))
return (smtpd_check_reject(state, MAIL_ERROR_BOUNCE,
var_virt_mailbox_code,
strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
"5.1.0" : "5.1.1",
"<%s>: %s rejected: User unknown%s",
recipient, reply_class,
var_show_unk_rcpt_table ?
" in virtual mailbox table" : ""));
break;
/*
* Reject mail to unknown addresses in relay domains.
*/
case RESOLVE_CLASS_RELAY:
if (*var_relay_rcpt_maps
&& NOMATCH(relay_rcpt_maps, CONST_STR(reply->recipient)))
return (smtpd_check_reject(state, MAIL_ERROR_BOUNCE,
var_relay_rcpt_code,
strcmp(reply_class, SMTPD_NAME_SENDER) == 0 ?
"5.1.0" : "5.1.1",
"<%s>: %s rejected: User unknown%s",
recipient, reply_class,
var_show_unk_rcpt_table ?
" in relay recipient table" : ""));
if (warn_compat_break_relay_domains)
msg_info("using backwards-compatible default setting "
VAR_RELAY_DOMAINS "=$mydestination to accept mail "
"for address \"%s\"", recipient);
break;
}
/*
* Accept all other addresses - including addresses that passed the above
* tests because of some table lookup problem.
*/
return (0);
}
/* smtpd_check_size - check optional SIZE parameter value */
char *smtpd_check_size(SMTPD_STATE *state, off_t size)
{
int status;
/*
* Return here in case of serious trouble.
*/
SMTPD_CHECK_RESET();
if ((status = setjmp(smtpd_check_buf)) != 0)
return (status == SMTPD_CHECK_REJECT ? STR(error_text) : 0);
/*
* Check against file size limit.
*/
if (var_message_limit > 0 && size > var_message_limit) {
(void) smtpd_check_reject(state, MAIL_ERROR_POLICY,
552, "5.3.4",
"Message size exceeds fixed limit");
return (STR(error_text));
}
return (0);
}
/* smtpd_check_queue - check queue space */
char *smtpd_check_queue(SMTPD_STATE *state)
{
const char *myname = "smtpd_check_queue";
struct fsspace fsbuf;
int status;
/*
* Return here in case of serious trouble.
*/
SMTPD_CHECK_RESET();
if ((status = setjmp(smtpd_check_buf)) != 0)
return (status == SMTPD_CHECK_REJECT ? STR(error_text) : 0);
/*
* Avoid overflow/underflow when comparing message size against available
* space.
*/
#define BLOCKS(x) ((x) / fsbuf.block_size)
fsspace(".", &fsbuf);
if (msg_verbose)
msg_info("%s: blocks %lu avail %lu min_free %lu msg_size_limit %lu",
myname,
(unsigned long) fsbuf.block_size,
(unsigned long) fsbuf.block_free,
(unsigned long) var_queue_minfree,
(unsigned long) var_message_limit);
if (BLOCKS(var_queue_minfree) >= fsbuf.block_free
|| BLOCKS(var_message_limit) >= fsbuf.block_free / smtpd_space_multf) {
(void) smtpd_check_reject(state, MAIL_ERROR_RESOURCE,
452, "4.3.1",
"Insufficient system storage");
msg_warn("not enough free space in mail queue: %lu bytes < "
"%g*message size limit",
(unsigned long) fsbuf.block_free * fsbuf.block_size,
smtpd_space_multf);
return (STR(error_text));
}
return (0);
}
/* smtpd_check_data - check DATA command */
char *smtpd_check_data(SMTPD_STATE *state)
{
int status;
char *NOCLOBBER saved_recipient;
/*
* Minor kluge so that we can delegate work to the generic routine. We
* provide no recipient information in the case of multiple recipients,
* This restriction applies to all recipients alike, and logging only one
* of them would be misleading.
*/
if (state->rcpt_count > 1) {
saved_recipient = state->recipient;
state->recipient = 0;
}
/*
* Reset the defer_if_permit flag. This is necessary when some recipients
* were accepted but the last one was rejected.
*/
state->defer_if_permit.active = 0;
/*
* Apply restrictions in the order as specified.
*
* XXX We cannot specify a default target for a bare access map.
*/
SMTPD_CHECK_RESET();
status = setjmp(smtpd_check_buf);
if (status == 0 && data_restrctions->argc)
status = generic_checks(state, data_restrctions,
SMTPD_CMD_DATA, SMTPD_NAME_DATA, NO_DEF_ACL);
/*
* Force permission into deferral when some earlier temporary error may
* have prevented us from rejecting mail, and report the earlier problem.
*/
if (status != SMTPD_CHECK_REJECT && state->defer_if_permit.active)
status = smtpd_check_reject(state, state->defer_if_permit.class,
state->defer_if_permit.code,
STR(state->defer_if_permit.dsn),
"%s", STR(state->defer_if_permit.reason));
if (state->rcpt_count > 1)
state->recipient = saved_recipient;
return (status == SMTPD_CHECK_REJECT ? STR(error_text) : 0);
}
/* smtpd_check_eod - check end-of-data command */
char *smtpd_check_eod(SMTPD_STATE *state)
{
int status;
char *NOCLOBBER saved_recipient;
/*
* Minor kluge so that we can delegate work to the generic routine. We
* provide no recipient information in the case of multiple recipients,
* This restriction applies to all recipients alike, and logging only one
* of them would be misleading.
*/
if (state->rcpt_count > 1) {
saved_recipient = state->recipient;
state->recipient = 0;
}
/*
* Reset the defer_if_permit flag. This is necessary when some recipients
* were accepted but the last one was rejected.
*/
state->defer_if_permit.active = 0;
/*
* Apply restrictions in the order as specified.
*
* XXX We cannot specify a default target for a bare access map.
*/
SMTPD_CHECK_RESET();
status = setjmp(smtpd_check_buf);
if (status == 0 && eod_restrictions->argc)
status = generic_checks(state, eod_restrictions,
SMTPD_CMD_EOD, SMTPD_NAME_EOD, NO_DEF_ACL);
/*
* Force permission into deferral when some earlier temporary error may
* have prevented us from rejecting mail, and report the earlier problem.
*/
if (status != SMTPD_CHECK_REJECT && state->defer_if_permit.active)
status = smtpd_check_reject(state, state->defer_if_permit.class,
state->defer_if_permit.code,
STR(state->defer_if_permit.dsn),
"%s", STR(state->defer_if_permit.reason));
if (state->rcpt_count > 1)
state->recipient = saved_recipient;
return (status == SMTPD_CHECK_REJECT ? STR(error_text) : 0);
}
#ifdef TEST
/*
* Test program to try out all these restrictions without having to go live.
* This is not entirely stand-alone, as it requires access to the Postfix
* rewrite/resolve service. This is just for testing code, not for debugging
* configuration files.
*/
#include <stdlib.h>
#include <msg_vstream.h>
#include <vstring_vstream.h>
#include <mail_conf.h>
#include <rewrite_clnt.h>
#include <dns.h>
#include <smtpd_chat.h>
int smtpd_input_transp_mask;
/*
* Dummies. These are never set.
*/
char *var_client_checks = "";
char *var_helo_checks = "";
char *var_mail_checks = "";
char *var_relay_checks = "";
char *var_rcpt_checks = "";
char *var_etrn_checks = "";
char *var_data_checks = "";
char *var_eod_checks = "";
char *var_relay_domains = "";
char *var_smtpd_uproxy_proto = "";
int var_smtpd_uproxy_tmout = 0;
#ifdef USE_TLS
char *var_relay_ccerts = "";
#endif
char *var_mynetworks = "";
char *var_notify_classes = "";
char *var_smtpd_policy_def_action = "";
char *var_smtpd_policy_context = "";
/*
* String-valued configuration parameters.
*/
char *var_maps_rbl_domains;
char *var_myorigin;
char *var_mydest;
char *var_inet_interfaces;
char *var_proxy_interfaces;
char *var_rcpt_delim;
char *var_rest_classes;
char *var_alias_maps;
char *var_send_canon_maps;
char *var_rcpt_canon_maps;
char *var_canonical_maps;
char *var_virt_alias_maps;
char *var_virt_alias_doms;
char *var_virt_mailbox_maps;
char *var_virt_mailbox_doms;
char *var_local_rcpt_maps;
char *var_perm_mx_networks;
char *var_par_dom_match;
char *var_smtpd_null_key;
char *var_smtpd_snd_auth_maps;
char *var_double_bounce_sender;
char *var_rbl_reply_maps;
char *var_smtpd_exp_filter;
char *var_def_rbl_reply;
char *var_relay_rcpt_maps;
char *var_verify_sender;
char *var_smtpd_sasl_opts;
char *var_local_rwr_clients;
char *var_smtpd_relay_ccerts;
char *var_unv_from_why;
char *var_unv_rcpt_why;
char *var_stress;
char *var_unk_name_tf_act;
char *var_unk_addr_tf_act;
char *var_unv_rcpt_tf_act;
char *var_unv_from_tf_act;
char *var_smtpd_acl_perm_log;
typedef struct {
char *name;
char *defval;
char **target;
} STRING_TABLE;
#undef DEF_VIRT_ALIAS_MAPS
#define DEF_VIRT_ALIAS_MAPS ""
#undef DEF_LOCAL_RCPT_MAPS
#define DEF_LOCAL_RCPT_MAPS ""
static const STRING_TABLE string_table[] = {
VAR_MAPS_RBL_DOMAINS, DEF_MAPS_RBL_DOMAINS, &var_maps_rbl_domains,
VAR_MYORIGIN, DEF_MYORIGIN, &var_myorigin,
VAR_MYDEST, DEF_MYDEST, &var_mydest,
VAR_INET_INTERFACES, DEF_INET_INTERFACES, &var_inet_interfaces,
VAR_PROXY_INTERFACES, DEF_PROXY_INTERFACES, &var_proxy_interfaces,
VAR_RCPT_DELIM, DEF_RCPT_DELIM, &var_rcpt_delim,
VAR_REST_CLASSES, DEF_REST_CLASSES, &var_rest_classes,
VAR_ALIAS_MAPS, DEF_ALIAS_MAPS, &var_alias_maps,
VAR_SEND_CANON_MAPS, DEF_SEND_CANON_MAPS, &var_send_canon_maps,
VAR_RCPT_CANON_MAPS, DEF_RCPT_CANON_MAPS, &var_rcpt_canon_maps,
VAR_CANONICAL_MAPS, DEF_CANONICAL_MAPS, &var_canonical_maps,
VAR_VIRT_ALIAS_MAPS, DEF_VIRT_ALIAS_MAPS, &var_virt_alias_maps,
VAR_VIRT_ALIAS_DOMS, DEF_VIRT_ALIAS_DOMS, &var_virt_alias_doms,
VAR_VIRT_MAILBOX_MAPS, DEF_VIRT_MAILBOX_MAPS, &var_virt_mailbox_maps,
VAR_VIRT_MAILBOX_DOMS, DEF_VIRT_MAILBOX_DOMS, &var_virt_mailbox_doms,
VAR_LOCAL_RCPT_MAPS, DEF_LOCAL_RCPT_MAPS, &var_local_rcpt_maps,
VAR_PERM_MX_NETWORKS, DEF_PERM_MX_NETWORKS, &var_perm_mx_networks,
VAR_PAR_DOM_MATCH, DEF_PAR_DOM_MATCH, &var_par_dom_match,
VAR_SMTPD_SND_AUTH_MAPS, DEF_SMTPD_SND_AUTH_MAPS, &var_smtpd_snd_auth_maps,
VAR_SMTPD_NULL_KEY, DEF_SMTPD_NULL_KEY, &var_smtpd_null_key,
VAR_DOUBLE_BOUNCE, DEF_DOUBLE_BOUNCE, &var_double_bounce_sender,
VAR_RBL_REPLY_MAPS, DEF_RBL_REPLY_MAPS, &var_rbl_reply_maps,
VAR_SMTPD_EXP_FILTER, DEF_SMTPD_EXP_FILTER, &var_smtpd_exp_filter,
VAR_DEF_RBL_REPLY, DEF_DEF_RBL_REPLY, &var_def_rbl_reply,
VAR_RELAY_RCPT_MAPS, DEF_RELAY_RCPT_MAPS, &var_relay_rcpt_maps,
VAR_VERIFY_SENDER, DEF_VERIFY_SENDER, &var_verify_sender,
VAR_MAIL_NAME, DEF_MAIL_NAME, &var_mail_name,
VAR_SMTPD_SASL_OPTS, DEF_SMTPD_SASL_OPTS, &var_smtpd_sasl_opts,
VAR_LOC_RWR_CLIENTS, DEF_LOC_RWR_CLIENTS, &var_local_rwr_clients,
VAR_RELAY_CCERTS, DEF_RELAY_CCERTS, &var_smtpd_relay_ccerts,
VAR_UNV_FROM_WHY, DEF_UNV_FROM_WHY, &var_unv_from_why,
VAR_UNV_RCPT_WHY, DEF_UNV_RCPT_WHY, &var_unv_rcpt_why,
VAR_STRESS, DEF_STRESS, &var_stress,
/* XXX Can't use ``$name'' type default values below. */
VAR_UNK_NAME_TF_ACT, DEF_REJECT_TMPF_ACT, &var_unk_name_tf_act,
VAR_UNK_ADDR_TF_ACT, DEF_REJECT_TMPF_ACT, &var_unk_addr_tf_act,
VAR_UNV_RCPT_TF_ACT, DEF_REJECT_TMPF_ACT, &var_unv_rcpt_tf_act,
VAR_UNV_FROM_TF_ACT, DEF_REJECT_TMPF_ACT, &var_unv_from_tf_act,
/* XXX Can't use ``$name'' type default values above. */
VAR_SMTPD_ACL_PERM_LOG, DEF_SMTPD_ACL_PERM_LOG, &var_smtpd_acl_perm_log,
VAR_SMTPD_DNS_RE_FILTER, DEF_SMTPD_DNS_RE_FILTER, &var_smtpd_dns_re_filter,
0,
};
/* string_init - initialize string parameters */
static void string_init(void)
{
const STRING_TABLE *sp;
for (sp = string_table; sp->name; sp++)
sp->target[0] = mystrdup(sp->defval);
}
/* string_update - update string parameter */
static int string_update(char **argv)
{
const STRING_TABLE *sp;
for (sp = string_table; sp->name; sp++) {
if (strcasecmp(argv[0], sp->name) == 0) {
myfree(sp->target[0]);
sp->target[0] = mystrdup(argv[1]);
return (1);
}
}
return (0);
}
/*
* Integer parameters.
*/
int var_queue_minfree; /* XXX use off_t */
typedef struct {
char *name;
int defval;
int *target;
} INT_TABLE;
int var_unk_client_code;
int var_bad_name_code;
int var_unk_name_code;
int var_unk_addr_code;
int var_relay_code;
int var_maps_rbl_code;
int var_map_reject_code;
int var_map_defer_code;
int var_reject_code;
int var_defer_code;
int var_non_fqdn_code;
int var_smtpd_delay_reject;
int var_allow_untrust_route;
int var_mul_rcpt_code;
int var_unv_from_rcode;
int var_unv_from_dcode;
int var_unv_rcpt_rcode;
int var_unv_rcpt_dcode;
int var_local_rcpt_code;
int var_relay_rcpt_code;
int var_virt_mailbox_code;
int var_virt_alias_code;
int var_show_unk_rcpt_table;
int var_verify_poll_count;
int var_verify_poll_delay;
int var_smtpd_policy_tmout;
int var_smtpd_policy_idle;
int var_smtpd_policy_ttl;
int var_smtpd_policy_req_limit;
int var_smtpd_policy_try_limit;
int var_smtpd_policy_try_delay;
int var_smtpd_rej_unl_from;
int var_smtpd_rej_unl_rcpt;
int var_plaintext_code;
bool var_smtpd_peername_lookup;
bool var_smtpd_client_port_log;
char *var_smtpd_dns_re_filter;
#define int_table test_int_table
static const INT_TABLE int_table[] = {
"msg_verbose", 0, &msg_verbose,
VAR_UNK_CLIENT_CODE, DEF_UNK_CLIENT_CODE, &var_unk_client_code,
VAR_BAD_NAME_CODE, DEF_BAD_NAME_CODE, &var_bad_name_code,
VAR_UNK_NAME_CODE, DEF_UNK_NAME_CODE, &var_unk_name_code,
VAR_UNK_ADDR_CODE, DEF_UNK_ADDR_CODE, &var_unk_addr_code,
VAR_RELAY_CODE, DEF_RELAY_CODE, &var_relay_code,
VAR_MAPS_RBL_CODE, DEF_MAPS_RBL_CODE, &var_maps_rbl_code,
VAR_MAP_REJECT_CODE, DEF_MAP_REJECT_CODE, &var_map_reject_code,
VAR_MAP_DEFER_CODE, DEF_MAP_DEFER_CODE, &var_map_defer_code,
VAR_REJECT_CODE, DEF_REJECT_CODE, &var_reject_code,
VAR_DEFER_CODE, DEF_DEFER_CODE, &var_defer_code,
VAR_NON_FQDN_CODE, DEF_NON_FQDN_CODE, &var_non_fqdn_code,
VAR_SMTPD_DELAY_REJECT, DEF_SMTPD_DELAY_REJECT, &var_smtpd_delay_reject,
VAR_ALLOW_UNTRUST_ROUTE, DEF_ALLOW_UNTRUST_ROUTE, &var_allow_untrust_route,
VAR_MUL_RCPT_CODE, DEF_MUL_RCPT_CODE, &var_mul_rcpt_code,
VAR_UNV_FROM_RCODE, DEF_UNV_FROM_RCODE, &var_unv_from_rcode,
VAR_UNV_FROM_DCODE, DEF_UNV_FROM_DCODE, &var_unv_from_dcode,
VAR_UNV_RCPT_RCODE, DEF_UNV_RCPT_RCODE, &var_unv_rcpt_rcode,
VAR_UNV_RCPT_DCODE, DEF_UNV_RCPT_DCODE, &var_unv_rcpt_dcode,
VAR_LOCAL_RCPT_CODE, DEF_LOCAL_RCPT_CODE, &var_local_rcpt_code,
VAR_RELAY_RCPT_CODE, DEF_RELAY_RCPT_CODE, &var_relay_rcpt_code,
VAR_VIRT_ALIAS_CODE, DEF_VIRT_ALIAS_CODE, &var_virt_alias_code,
VAR_VIRT_MAILBOX_CODE, DEF_VIRT_MAILBOX_CODE, &var_virt_mailbox_code,
VAR_SHOW_UNK_RCPT_TABLE, DEF_SHOW_UNK_RCPT_TABLE, &var_show_unk_rcpt_table,
VAR_VERIFY_POLL_COUNT, 3, &var_verify_poll_count,
VAR_SMTPD_REJ_UNL_FROM, DEF_SMTPD_REJ_UNL_FROM, &var_smtpd_rej_unl_from,
VAR_SMTPD_REJ_UNL_RCPT, DEF_SMTPD_REJ_UNL_RCPT, &var_smtpd_rej_unl_rcpt,
VAR_PLAINTEXT_CODE, DEF_PLAINTEXT_CODE, &var_plaintext_code,
VAR_SMTPD_PEERNAME_LOOKUP, DEF_SMTPD_PEERNAME_LOOKUP, &var_smtpd_peername_lookup,
VAR_SMTPD_CLIENT_PORT_LOG, DEF_SMTPD_CLIENT_PORT_LOG, &var_smtpd_client_port_log,
0,
};
/* int_init - initialize int parameters */
static void int_init(void)
{
const INT_TABLE *sp;
for (sp = int_table; sp->name; sp++)
sp->target[0] = sp->defval;
}
/* int_update - update int parameter */
static int int_update(char **argv)
{
const INT_TABLE *ip;
for (ip = int_table; ip->name; ip++) {
if (strcasecmp(argv[0], ip->name) == 0) {
if (!ISDIGIT(*argv[1]))
msg_fatal("bad number: %s %s", ip->name, argv[1]);
ip->target[0] = atoi(argv[1]);
return (1);
}
}
return (0);
}
/*
* Restrictions.
*/
typedef struct {
char *name;
ARGV **target;
} REST_TABLE;
static const REST_TABLE rest_table[] = {
"client_restrictions", &client_restrctions,
"helo_restrictions", &helo_restrctions,
"sender_restrictions", &mail_restrctions,
"relay_restrictions", &relay_restrctions,
"recipient_restrictions", &rcpt_restrctions,
"etrn_restrictions", &etrn_restrctions,
0,
};
/* rest_update - update restriction */
static int rest_update(char **argv)
{
const REST_TABLE *rp;
for (rp = rest_table; rp->name; rp++) {
if (strcasecmp(rp->name, argv[0]) == 0) {
argv_free(rp->target[0]);
rp->target[0] = smtpd_check_parse(SMTPD_CHECK_PARSE_ALL, argv[1]);
return (1);
}
}
return (0);
}
/* rest_class - (re)define a restriction class */
static void rest_class(char *class)
{
char *cp = class;
char *name;
HTABLE_INFO *entry;
if (smtpd_rest_classes == 0)
smtpd_rest_classes = htable_create(1);
if ((name = mystrtok(&cp, CHARS_COMMA_SP)) == 0)
msg_panic("rest_class: null class name");
if ((entry = htable_locate(smtpd_rest_classes, name)) != 0)
argv_free((ARGV *) entry->value);
else
entry = htable_enter(smtpd_rest_classes, name, (void *) 0);
entry->value = (void *) smtpd_check_parse(SMTPD_CHECK_PARSE_ALL, cp);
}
/* resolve_clnt_init - initialize reply */
void resolve_clnt_init(RESOLVE_REPLY *reply)
{
reply->flags = 0;
reply->transport = vstring_alloc(100);
reply->nexthop = vstring_alloc(100);
reply->recipient = vstring_alloc(100);
}
void resolve_clnt_free(RESOLVE_REPLY *reply)
{
vstring_free(reply->transport);
vstring_free(reply->nexthop);
vstring_free(reply->recipient);
}
bool var_smtpd_sasl_enable = 0;
#ifdef USE_SASL_AUTH
/* smtpd_sasl_activate - stub */
void smtpd_sasl_activate(SMTPD_STATE *state, const char *opts_name,
const char *opts_var)
{
msg_panic("smtpd_sasl_activate was called");
}
/* smtpd_sasl_deactivate - stub */
void smtpd_sasl_deactivate(SMTPD_STATE *state)
{
msg_panic("smtpd_sasl_deactivate was called");
}
/* permit_sasl_auth - stub */
int permit_sasl_auth(SMTPD_STATE *state, int ifyes, int ifnot)
{
return (ifnot);
}
/* smtpd_sasl_state_init - the real deal */
void smtpd_sasl_state_init(SMTPD_STATE *state)
{
state->sasl_username = 0;
state->sasl_method = 0;
state->sasl_sender = 0;
}
#endif
/* verify_clnt_query - stub */
int verify_clnt_query(const char *addr, int *addr_status, VSTRING *why)
{
*addr_status = DEL_RCPT_STAT_OK;
return (VRFY_STAT_OK);
}
/* rewrite_clnt_internal - stub */
VSTRING *rewrite_clnt_internal(const char *context, const char *addr,
VSTRING *result)
{
if (addr == STR(result))
msg_panic("rewrite_clnt_internal: result clobbers input");
if (*addr && strchr(addr, '@') == 0)
msg_fatal("%s: address rewriting is disabled", addr);
vstring_strcpy(result, addr);
return (result);
}
/* resolve_clnt_query - stub */
void resolve_clnt(const char *class, const char *unused_sender, const char *addr,
RESOLVE_REPLY *reply)
{
const char *domain;
int rc;
if (addr == CONST_STR(reply->recipient))
msg_panic("resolve_clnt_query: result clobbers input");
if (strchr(addr, '%'))
msg_fatal("%s: address rewriting is disabled", addr);
if ((domain = strrchr(addr, '@')) == 0)
msg_fatal("%s: unqualified address", addr);
domain += 1;
if ((rc = resolve_local(domain)) > 0) {
reply->flags = RESOLVE_CLASS_LOCAL;
vstring_strcpy(reply->transport, MAIL_SERVICE_LOCAL);
vstring_strcpy(reply->nexthop, domain);
} else if (rc < 0) {
reply->flags = RESOLVE_FLAG_FAIL;
} else if (string_list_match(virt_alias_doms, domain)) {
reply->flags = RESOLVE_CLASS_ALIAS;
vstring_strcpy(reply->transport, MAIL_SERVICE_ERROR);
vstring_strcpy(reply->nexthop, "user unknown");
} else if (virt_alias_doms->error) {
reply->flags = RESOLVE_FLAG_FAIL;
} else if (string_list_match(virt_mailbox_doms, domain)) {
reply->flags = RESOLVE_CLASS_VIRTUAL;
vstring_strcpy(reply->transport, MAIL_SERVICE_VIRTUAL);
vstring_strcpy(reply->nexthop, domain);
} else if (virt_mailbox_doms->error) {
reply->flags = RESOLVE_FLAG_FAIL;
} else if (domain_list_match(relay_domains, domain)) {
reply->flags = RESOLVE_CLASS_RELAY;
vstring_strcpy(reply->transport, MAIL_SERVICE_RELAY);
vstring_strcpy(reply->nexthop, domain);
} else if (relay_domains->error) {
reply->flags = RESOLVE_FLAG_FAIL;
} else {
reply->flags = RESOLVE_CLASS_DEFAULT;
vstring_strcpy(reply->transport, MAIL_SERVICE_SMTP);
vstring_strcpy(reply->nexthop, domain);
}
vstring_strcpy(reply->recipient, addr);
}
/* smtpd_chat_reset - stub */
void smtpd_chat_reset(SMTPD_STATE *unused_state)
{
}
/* usage - scream and terminate */
static NORETURN usage(char *myname)
{
msg_fatal("usage: %s", myname);
}
int main(int argc, char **argv)
{
VSTRING *buf = vstring_alloc(100);
SMTPD_STATE state;
ARGV *args;
char *bp;
char *resp;
char *addr;
/*
* Initialization. Use dummies for client information.
*/
msg_vstream_init(argv[0], VSTREAM_ERR);
if (argc != 1)
usage(argv[0]);
string_init();
int_init();
smtpd_check_init();
smtpd_expand_init();
(void) inet_proto_init(argv[0], INET_PROTO_NAME_IPV4);
smtpd_state_init(&state, VSTREAM_IN, "smtpd");
state.queue_id = "<queue id>";
/*
* Main loop: update config parameters or test the client, helo, sender
* and recipient restrictions.
*/
while (vstring_fgets_nonl(buf, VSTREAM_IN) != 0) {
/*
* Tokenize the command. Note, the comma is not a separator, so that
* restriction lists can be entered as comma-separated lists.
*/
bp = STR(buf);
if (!isatty(0)) {
vstream_printf(">>> %s\n", bp);
vstream_fflush(VSTREAM_OUT);
}
if (*bp == '#')
continue;
if (*bp == '!') {
vstream_printf("exit %d\n", system(bp + 1));
continue;
}
args = argv_splitq(bp, CHARS_SPACE, CHARS_BRACE);
/*
* Recognize the command.
*/
resp = "bad command";
switch (args->argc) {
/*
* Emtpy line.
*/
case 0:
argv_free(args);
continue;
/*
* Special case: rewrite context.
*/
case 1:
if (strcasecmp(args->argv[0], "rewrite") == 0) {
resp = smtpd_check_rewrite(&state);
break;
}
/*
* Other parameter-less commands.
*/
if (strcasecmp(args->argv[0], "flush_dnsxl_cache") == 0) {
if (smtpd_rbl_cache) {
ctable_free(smtpd_rbl_cache);
ctable_free(smtpd_rbl_byte_cache);
}
smtpd_rbl_cache = ctable_create(100, rbl_pagein,
rbl_pageout, (void *) 0);
smtpd_rbl_byte_cache = ctable_create(1000, rbl_byte_pagein,
rbl_byte_pageout, (void *) 0);
resp = 0;
break;
}
/*
* Special case: client identity.
*/
case 4:
case 3:
if (strcasecmp(args->argv[0], "client") == 0) {
state.where = SMTPD_AFTER_CONNECT;
UPDATE_STRING(state.name, args->argv[1]);
UPDATE_STRING(state.reverse_name, args->argv[1]);
UPDATE_STRING(state.addr, args->argv[2]);
if (args->argc == 4)
state.name_status =
state.reverse_name_status =
atoi(args->argv[3]);
else if (strcmp(state.name, "unknown") == 0)
state.name_status =
state.reverse_name_status =
SMTPD_PEER_CODE_TEMP;
else
state.name_status =
state.reverse_name_status =
SMTPD_PEER_CODE_OK;
if (state.namaddr)
myfree(state.namaddr);
state.namaddr = concatenate(state.name, "[", state.addr,
"]", (char *) 0);
resp = smtpd_check_client(&state);
}
break;
/*
* Try config settings.
*/
#define UPDATE_MAPS(ptr, var, val, lock) \
{ if (ptr) maps_free(ptr); ptr = maps_create(var, val, lock); }
#define UPDATE_LIST(ptr, var, val) \
{ if (ptr) string_list_free(ptr); \
ptr = string_list_init(var, MATCH_FLAG_NONE, val); }
case 2:
if (strcasecmp(args->argv[0], VAR_MYDEST) == 0) {
UPDATE_STRING(var_mydest, args->argv[1]);
resolve_local_init();
smtpd_resolve_init(100);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_VIRT_ALIAS_MAPS) == 0) {
UPDATE_STRING(var_virt_alias_maps, args->argv[1]);
UPDATE_MAPS(virt_alias_maps, VAR_VIRT_ALIAS_MAPS,
var_virt_alias_maps, DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX | DICT_FLAG_UTF8_REQUEST);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_VIRT_ALIAS_DOMS) == 0) {
UPDATE_STRING(var_virt_alias_doms, args->argv[1]);
UPDATE_LIST(virt_alias_doms, VAR_VIRT_ALIAS_DOMS,
var_virt_alias_doms);
smtpd_resolve_init(100);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_VIRT_MAILBOX_MAPS) == 0) {
UPDATE_STRING(var_virt_mailbox_maps, args->argv[1]);
UPDATE_MAPS(virt_mailbox_maps, VAR_VIRT_MAILBOX_MAPS,
var_virt_mailbox_maps, DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX | DICT_FLAG_UTF8_REQUEST);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_VIRT_MAILBOX_DOMS) == 0) {
UPDATE_STRING(var_virt_mailbox_doms, args->argv[1]);
UPDATE_LIST(virt_mailbox_doms, VAR_VIRT_MAILBOX_DOMS,
var_virt_mailbox_doms);
smtpd_resolve_init(100);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_LOCAL_RCPT_MAPS) == 0) {
UPDATE_STRING(var_local_rcpt_maps, args->argv[1]);
UPDATE_MAPS(local_rcpt_maps, VAR_LOCAL_RCPT_MAPS,
var_local_rcpt_maps, DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX | DICT_FLAG_UTF8_REQUEST);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_RELAY_RCPT_MAPS) == 0) {
UPDATE_STRING(var_relay_rcpt_maps, args->argv[1]);
UPDATE_MAPS(relay_rcpt_maps, VAR_RELAY_RCPT_MAPS,
var_relay_rcpt_maps, DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX | DICT_FLAG_UTF8_REQUEST);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_CANONICAL_MAPS) == 0) {
UPDATE_STRING(var_canonical_maps, args->argv[1]);
UPDATE_MAPS(canonical_maps, VAR_CANONICAL_MAPS,
var_canonical_maps, DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX | DICT_FLAG_UTF8_REQUEST);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_SEND_CANON_MAPS) == 0) {
UPDATE_STRING(var_send_canon_maps, args->argv[1]);
UPDATE_MAPS(send_canon_maps, VAR_SEND_CANON_MAPS,
var_send_canon_maps, DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX | DICT_FLAG_UTF8_REQUEST);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_RCPT_CANON_MAPS) == 0) {
UPDATE_STRING(var_rcpt_canon_maps, args->argv[1]);
UPDATE_MAPS(rcpt_canon_maps, VAR_RCPT_CANON_MAPS,
var_rcpt_canon_maps, DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX | DICT_FLAG_UTF8_REQUEST);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_RBL_REPLY_MAPS) == 0) {
UPDATE_STRING(var_rbl_reply_maps, args->argv[1]);
UPDATE_MAPS(rbl_reply_maps, VAR_RBL_REPLY_MAPS,
var_rbl_reply_maps, DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX | DICT_FLAG_UTF8_REQUEST);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_MYNETWORKS) == 0) {
/* NOT: UPDATE_STRING */
namadr_list_free(mynetworks_curr);
mynetworks_curr =
namadr_list_init(VAR_MYNETWORKS, MATCH_FLAG_RETURN
| match_parent_style(VAR_MYNETWORKS),
args->argv[1]);
smtpd_resolve_init(100);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_RELAY_DOMAINS) == 0) {
/* NOT: UPDATE_STRING */
domain_list_free(relay_domains);
relay_domains =
domain_list_init(VAR_RELAY_DOMAINS,
match_parent_style(VAR_RELAY_DOMAINS),
args->argv[1]);
smtpd_resolve_init(100);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_PERM_MX_NETWORKS) == 0) {
UPDATE_STRING(var_perm_mx_networks, args->argv[1]);
domain_list_free(perm_mx_networks);
perm_mx_networks =
namadr_list_init(VAR_PERM_MX_NETWORKS, MATCH_FLAG_RETURN
| match_parent_style(VAR_PERM_MX_NETWORKS),
args->argv[1]);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_SMTPD_DNS_RE_FILTER) == 0) {
/* NOT: UPDATE_STRING */
dns_rr_filter_compile(VAR_SMTPD_DNS_RE_FILTER, args->argv[1]);
resp = 0;
break;
}
#ifdef USE_TLS
if (strcasecmp(args->argv[0], VAR_RELAY_CCERTS) == 0) {
UPDATE_STRING(var_smtpd_relay_ccerts, args->argv[1]);
UPDATE_MAPS(relay_ccerts, VAR_RELAY_CCERTS,
var_smtpd_relay_ccerts, DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX);
resp = 0;
}
#endif
if (strcasecmp(args->argv[0], "restriction_class") == 0) {
rest_class(args->argv[1]);
resp = 0;
break;
}
if (strcasecmp(args->argv[0], VAR_LOC_RWR_CLIENTS) == 0) {
UPDATE_STRING(var_local_rwr_clients, args->argv[1]);
argv_free(local_rewrite_clients);
local_rewrite_clients = smtpd_check_parse(SMTPD_CHECK_PARSE_MAPS,
var_local_rwr_clients);
}
if (int_update(args->argv)
|| string_update(args->argv)
|| rest_update(args->argv)) {
resp = 0;
break;
}
/*
* Try restrictions.
*/
#define TRIM_ADDR(src, res) { \
if (*(res = src) == '<') { \
res += strlen(res) - 1; \
if (*res == '>') \
*res = 0; \
res = src + 1; \
} \
}
if (strcasecmp(args->argv[0], "helo") == 0) {
state.where = "HELO";
resp = smtpd_check_helo(&state, args->argv[1]);
UPDATE_STRING(state.helo_name, args->argv[1]);
} else if (strcasecmp(args->argv[0], "mail") == 0) {
state.where = "MAIL";
TRIM_ADDR(args->argv[1], addr);
UPDATE_STRING(state.sender, addr);
resp = smtpd_check_mail(&state, addr);
} else if (strcasecmp(args->argv[0], "rcpt") == 0) {
state.where = "RCPT";
TRIM_ADDR(args->argv[1], addr);
resp = smtpd_check_rcpt(&state, addr);
#ifdef USE_TLS
} else if (strcasecmp(args->argv[0], "fingerprint") == 0) {
if (state.tls_context == 0) {
state.tls_context =
(TLS_SESS_STATE *) mymalloc(sizeof(*state.tls_context));
memset((void *) state.tls_context, 0,
sizeof(*state.tls_context));
state.tls_context->peer_cert_fprint =
state.tls_context->peer_pkey_fprint = 0;
}
state.tls_context->peer_status |= TLS_CERT_FLAG_PRESENT;
UPDATE_STRING(state.tls_context->peer_cert_fprint,
args->argv[1]);
state.tls_context->peer_pkey_fprint =
state.tls_context->peer_cert_fprint;
resp = "OK";
break;
#endif
}
break;
/*
* Show commands.
*/
default:
if (strcasecmp(args->argv[0], "check_rewrite") == 0) {
smtpd_check_rewrite(&state);
resp = state.rewrite_context;
break;
}
resp = "Commands...\n\
client <name> <address> [<code>]\n\
helo <hostname>\n\
sender <address>\n\
recipient <address>\n\
check_rewrite\n\
msg_verbose <level>\n\
client_restrictions <restrictions>\n\
helo_restrictions <restrictions>\n\
sender_restrictions <restrictions>\n\
recipient_restrictions <restrictions>\n\
restriction_class name,<restrictions>\n\
flush_dnsxl_cache\n\
\n\
Note: no address rewriting \n";
break;
}
vstream_printf("%s\n", resp ? resp : "OK");
vstream_fflush(VSTREAM_OUT);
argv_free(args);
}
vstring_free(buf);
smtpd_state_reset(&state);
#define FREE_STRING(s) { if (s) myfree(s); }
FREE_STRING(state.helo_name);
FREE_STRING(state.sender);
#ifdef USE_TLS
if (state.tls_context) {
FREE_STRING(state.tls_context->peer_cert_fprint);
myfree((void *) state.tls_context);
}
#endif
exit(0);
}
#endif
|