File: loadparm.c

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

   Largely re-written by Andrew Tridgell, September 1994

   Copyright (C) Simo Sorce 2001
   Copyright (C) Alexander Bokovoy 2002
   Copyright (C) Stefan (metze) Metzmacher 2002
   Copyright (C) Jim McDonough (jmcd@us.ibm.com)  2003.
   Copyright (C) James Myers 2003 <myersjj@samba.org>
   Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
   Copyright (C) Andrew Bartlett 2011-2012

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 *  Load parameters.
 *
 *  This module provides suitable callback functions for the params
 *  module. It builds the internal table of service details which is
 *  then used by the rest of the server.
 *
 * To add a parameter:
 *
 * 1) add it to the global or service structure definition
 * 2) add it to the parm_table
 * 3) add it to the list of available functions (eg: using FN_GLOBAL_STRING())
 * 4) If it's a global then initialise it in init_globals. If a local
 *    (ie. service) parameter then initialise it in the sDefault structure
 *
 *
 * Notes:
 *   The configuration file is processed sequentially for speed. It is NOT
 *   accessed randomly as happens in 'real' Windows. For this reason, there
 *   is a fair bit of sequence-dependent code here - ie., code which assumes
 *   that certain things happen before others. In particular, the code which
 *   happens at the boundary between sections is delicately poised, so be
 *   careful!
 *
 */

#include "includes.h"
#include "version.h"
#include "dynconfig/dynconfig.h"
#include "system/time.h"
#include "system/locale.h"
#include "system/network.h" /* needed for TCP_NODELAY */
#include "../lib/util/dlinklist.h"
#include "lib/param/param.h"
#include "lib/param/loadparm.h"
#include "auth/gensec/gensec.h"
#include "lib/param/s3_param.h"
#include "lib/util/bitmap.h"
#include "libcli/smb/smb_constants.h"
#include "tdb.h"
#include "librpc/gen_ndr/nbt.h"
#include "libds/common/roles.h"

#ifdef HAVE_HTTPCONNECTENCRYPT
#include <cups/http.h>
#endif

#define standard_sub_basic talloc_strdup

#include "lib/param/param_global.h"

struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
{
	return lp_ctx->sDefault;
}

/**
 * Convenience routine to grab string parameters into temporary memory
 * and run standard_sub_basic on them.
 *
 * The buffers can be written to by
 * callers without affecting the source string.
 */

static const char *lpcfg_string(const char *s)
{
#if 0  /* until REWRITE done to make thread-safe */
	size_t len = s ? strlen(s) : 0;
	char *ret;
#endif

	/* The follow debug is useful for tracking down memory problems
	   especially if you have an inner loop that is calling a lp_*()
	   function that returns a string.  Perhaps this debug should be
	   present all the time? */

#if 0
	DEBUG(10, ("lpcfg_string(%s)\n", s));
#endif

#if 0  /* until REWRITE done to make thread-safe */
	if (!lp_talloc)
		lp_talloc = talloc_init("lp_talloc");

	ret = talloc_array(lp_talloc, char, len + 100);	/* leave room for substitution */

	if (!ret)
		return NULL;

	if (!s)
		*ret = 0;
	else
		strlcpy(ret, s, len);

	if (trim_string(ret, "\"", "\"")) {
		if (strchr(ret,'"') != NULL)
			strlcpy(ret, s, len);
	}

	standard_sub_basic(ret,len+100);
	return (ret);
#endif
	return s;
}

/*
   In this section all the functions that are used to access the
   parameters from the rest of the program are defined
*/

/*
 * the creation of separate lpcfg_*() and lp_*() functions is to allow
 * for code compatibility between existing Samba4 and Samba3 code.
 */

/* this global context supports the lp_*() function varients */
static struct loadparm_context *global_loadparm_context;

#define FN_GLOBAL_STRING(fn_name,var_name) \
 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
	 if (lp_ctx == NULL) return NULL;				\
	 if (lp_ctx->s3_fns) {						\
		 return lp_ctx->globals->var_name ? lp_ctx->s3_fns->lp_string(ctx, lp_ctx->globals->var_name) : talloc_strdup(ctx, ""); \
	 }								\
	 return lp_ctx->globals->var_name ? talloc_strdup(ctx, lpcfg_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
}

#define FN_GLOBAL_CONST_STRING(fn_name,var_name)				\
 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
	if (lp_ctx == NULL) return NULL;				\
	return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
}

#define FN_GLOBAL_LIST(fn_name,var_name)				\
 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
	 if (lp_ctx == NULL) return NULL;				\
	 return lp_ctx->globals->var_name;				\
 }

#define FN_GLOBAL_BOOL(fn_name,var_name) \
 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
	 if (lp_ctx == NULL) return false;				\
	 return lp_ctx->globals->var_name;				\
}

#define FN_GLOBAL_INTEGER(fn_name,var_name) \
 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
	 return lp_ctx->globals->var_name;				\
 }

/* Local parameters don't need the ->s3_fns because the struct
 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
 * hook */
#define FN_LOCAL_STRING(fn_name,val) \
 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
					struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
	 return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
 }

#define FN_LOCAL_CONST_STRING(fn_name,val) \
 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
					struct loadparm_service *sDefault) { \
	 return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
 }

#define FN_LOCAL_LIST(fn_name,val) \
 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
					 struct loadparm_service *sDefault) {\
	 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
 }

#define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)

#define FN_LOCAL_BOOL(fn_name,val) \
 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
				 struct loadparm_service *sDefault) {	\
	 return((service != NULL)? service->val : sDefault->val); \
 }

#define FN_LOCAL_INTEGER(fn_name,val) \
 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
				struct loadparm_service *sDefault) {	\
	 return((service != NULL)? service->val : sDefault->val); \
 }

#define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)

#define FN_LOCAL_CHAR(fn_name,val) \
 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
				struct loadparm_service *sDefault) {	\
	 return((service != NULL)? service->val : sDefault->val); \
 }

#define FN_LOCAL_PARM_CHAR(fn_name,val) FN_LOCAL_CHAR(fn_name, val)

#include "lib/param/param_functions.c"

/* These functions cannot be auto-generated */
FN_LOCAL_BOOL(autoloaded, autoloaded)
FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)

/* local prototypes */
static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
					const char *pszServiceName);
static bool do_section(const char *pszSectionName, void *);
static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
				const char *pszParmName, const char *pszParmValue);
static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
				       struct loadparm_service *service,
				       const char *pszParmName,
				       const char *pszParmValue, int flags);

/* The following are helper functions for parametrical options support. */
/* It returns a pointer to parametrical option value if it exists or NULL otherwise */
/* Actual parametrical functions are quite simple */
struct parmlist_entry *get_parametric_helper(struct loadparm_service *service,
					     const char *type, const char *option,
					     struct parmlist_entry *global_opts)
{
	size_t type_len = strlen(type);
	size_t option_len = strlen(option);
	char param_key[type_len + option_len + 2];
	struct parmlist_entry *data = NULL;

	snprintf(param_key, sizeof(param_key), "%s:%s", type, option);

	/*
	 * Try to fetch the option from the data.
	 */
	if (service != NULL) {
		data = service->param_opt;
		while (data != NULL) {
			if (strwicmp(data->key, param_key) == 0) {
				return data;
			}
			data = data->next;
		}
	}

	/*
	 * Fall back to fetching from the globals.
	 */
	data = global_opts;
	while (data != NULL) {
		if (strwicmp(data->key, param_key) == 0) {
			return data;
		}
		data = data->next;
	}

	return NULL;
}

const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
			      struct loadparm_service *service,
			      const char *type, const char *option)
{
	struct parmlist_entry *data;

	if (lp_ctx == NULL)
		return NULL;

	data = get_parametric_helper(service,
				     type, option, lp_ctx->globals->param_opt);

	if (data == NULL) {
		return NULL;
	} else {
		return data->value;
	}
}


/**
 * convenience routine to return int parameters.
 */
int lp_int(const char *s)
{

	if (!s || !*s) {
		DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
		return -1;
	}

	return strtol(s, NULL, 0);
}

/**
 * convenience routine to return unsigned long parameters.
 */
unsigned long lp_ulong(const char *s)
{

	if (!s || !*s) {
		DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
		return -1;
	}

	return strtoul(s, NULL, 0);
}

/**
 * convenience routine to return unsigned long long parameters.
 */
unsigned long long lp_ulonglong(const char *s)
{

	if (!s || !*s) {
		DEBUG(0, ("lp_ulonglong(%s): is called with NULL!\n", s));
		return -1;
	}

	return strtoull(s, NULL, 0);
}

/**
 * convenience routine to return unsigned long parameters.
 */
static long lp_long(const char *s)
{

	if (!s) {
		DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
		return -1;
	}

	return strtol(s, NULL, 0);
}

/**
 * convenience routine to return unsigned long parameters.
 */
static double lp_double(const char *s)
{

	if (!s) {
		DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
		return -1;
	}

	return strtod(s, NULL);
}

/**
 * convenience routine to return boolean parameters.
 */
bool lp_bool(const char *s)
{
	bool ret = false;

	if (!s || !*s) {
		DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
		return false;
	}

	if (!set_boolean(s, &ret)) {
		DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
		return false;
	}

	return ret;
}

/**
 * Return parametric option from a given service. Type is a part of option before ':'
 * Parametric option has following syntax: 'Type: option = value'
 * Returned value is allocated in 'lp_talloc' context
 */

const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
			      struct loadparm_service *service, const char *type,
			      const char *option)
{
	const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);

	if (value)
		return lpcfg_string(value);

	return NULL;
}

/**
 * Return parametric option from a given service. Type is a part of option before ':'
 * Parametric option has following syntax: 'Type: option = value'
 * Returned value is allocated in 'lp_talloc' context
 */

const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
				    struct loadparm_context *lp_ctx,
				    struct loadparm_service *service,
				    const char *type,
				    const char *option, const char *separator)
{
	const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);

	if (value != NULL) {
		char **l = str_list_make(mem_ctx, value, separator);
		return discard_const_p(const char *, l);
	}

	return NULL;
}

/**
 * Return parametric option from a given service. Type is a part of option before ':'
 * Parametric option has following syntax: 'Type: option = value'
 */

int lpcfg_parm_int(struct loadparm_context *lp_ctx,
		   struct loadparm_service *service, const char *type,
		   const char *option, int default_v)
{
	const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);

	if (value)
		return lp_int(value);

	return default_v;
}

/**
 * Return parametric option from a given service. Type is a part of
 * option before ':'.
 * Parametric option has following syntax: 'Type: option = value'.
 */

int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
		  struct loadparm_service *service, const char *type,
		  const char *option, int default_v)
{
	uint64_t bval;

	const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);

	if (value && conv_str_size_error(value, &bval)) {
		if (bval <= INT_MAX) {
			return (int)bval;
		}
	}

	return default_v;
}

/**
 * Return parametric option from a given service.
 * Type is a part of option before ':'
 * Parametric option has following syntax: 'Type: option = value'
 */
unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
			    struct loadparm_service *service, const char *type,
			    const char *option, unsigned long default_v)
{
	const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);

	if (value)
		return lp_ulong(value);

	return default_v;
}

/**
 * Return parametric option from a given service.
 * Type is a part of option before ':'
 * Parametric option has following syntax: 'Type: option = value'
 */
unsigned long long lpcfg_parm_ulonglong(struct loadparm_context *lp_ctx,
					struct loadparm_service *service,
					const char *type, const char *option,
					unsigned long long default_v)
{
	const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);

	if (value) {
		return lp_ulonglong(value);
	}

	return default_v;
}

long lpcfg_parm_long(struct loadparm_context *lp_ctx,
		     struct loadparm_service *service, const char *type,
		     const char *option, long default_v)
{
	const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);

	if (value)
		return lp_long(value);

	return default_v;
}

double lpcfg_parm_double(struct loadparm_context *lp_ctx,
		      struct loadparm_service *service, const char *type,
		      const char *option, double default_v)
{
	const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);

	if (value != NULL)
		return lp_double(value);

	return default_v;
}

/**
 * Return parametric option from a given service. Type is a part of option before ':'
 * Parametric option has following syntax: 'Type: option = value'
 */

bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
		     struct loadparm_service *service, const char *type,
		     const char *option, bool default_v)
{
	const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);

	if (value != NULL)
		return lp_bool(value);

	return default_v;
}


/* this is used to prevent lots of mallocs of size 1 */
static const char lpcfg_string_empty[] = "";

/**
 Free a string value.
**/
void lpcfg_string_free(char **s)
{
	if (s == NULL) {
		return;
	}
	if (*s == lpcfg_string_empty) {
		*s = NULL;
		return;
	}
	TALLOC_FREE(*s);
}

/**
 * Set a string value, deallocating any existing space, and allocing the space
 * for the string
 */
bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
{
	lpcfg_string_free(dest);

	if ((src == NULL) || (*src == '\0')) {
		*dest = discard_const_p(char, lpcfg_string_empty);
		return true;
	}

	*dest = talloc_strdup(mem_ctx, src);
	if ((*dest) == NULL) {
		DEBUG(0,("Out of memory in string_set\n"));
		return false;
	}

	return true;
}

/**
 * Set a string value, deallocating any existing space, and allocing the space
 * for the string
 */
bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
{
	lpcfg_string_free(dest);

	if ((src == NULL) || (*src == '\0')) {
		*dest = discard_const_p(char, lpcfg_string_empty);
		return true;
	}

	*dest = strupper_talloc(mem_ctx, src);
	if ((*dest) == NULL) {
		DEBUG(0,("Out of memory in string_set_upper\n"));
		return false;
	}

	return true;
}



/**
 * Add a new service to the services array initialising it with the given
 * service.
 */

struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
					   const struct loadparm_service *pservice,
					   const char *name)
{
	int i;
	int num_to_alloc = lp_ctx->iNumServices + 1;
	struct parmlist_entry *data, *pdata;

	if (lp_ctx->s3_fns != NULL) {
		smb_panic("Add a service should not be called on an s3 loadparm ctx");
	}

	if (pservice == NULL) {
		pservice = lp_ctx->sDefault;
	}

	/* it might already exist */
	if (name) {
		struct loadparm_service *service = lpcfg_getservicebyname(lp_ctx,
								    name);
		if (service != NULL) {
			/* Clean all parametric options for service */
			/* They will be added during parsing again */
			data = service->param_opt;
			while (data) {
				pdata = data->next;
				talloc_free(data);
				data = pdata;
			}
			service->param_opt = NULL;
			return service;
		}
	}

	/* find an invalid one */
	for (i = 0; i < lp_ctx->iNumServices; i++)
		if (lp_ctx->services[i] == NULL)
			break;

	/* if not, then create one */
	if (i == lp_ctx->iNumServices) {
		struct loadparm_service **tsp;

		tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);

		if (!tsp) {
			DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
			return NULL;
		} else {
			lp_ctx->services = tsp;
			lp_ctx->services[lp_ctx->iNumServices] = NULL;
		}

		lp_ctx->iNumServices++;
	}

	lp_ctx->services[i] = talloc_zero(lp_ctx->services, struct loadparm_service);
	if (lp_ctx->services[i] == NULL) {
		DEBUG(0,("lpcfg_add_service: out of memory!\n"));
		return NULL;
	}
	copy_service(lp_ctx->services[i], pservice, NULL);
	if (name != NULL)
		lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
	return lp_ctx->services[i];
}

/**
 * Add a new home service, with the specified home directory, defaults coming
 * from service ifrom.
 */

bool lpcfg_add_home(struct loadparm_context *lp_ctx,
		 const char *pszHomename,
		 struct loadparm_service *default_service,
		 const char *user, const char *pszHomedir)
{
	struct loadparm_service *service;

	service = lpcfg_add_service(lp_ctx, default_service, pszHomename);

	if (service == NULL)
		return false;

	if (!(*(default_service->path))
	    || strequal(default_service->path, lp_ctx->sDefault->path)) {
		service->path = talloc_strdup(service, pszHomedir);
	} else {
		service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
	}

	if (!(*(service->comment))) {
		service->comment = talloc_asprintf(service, "Home directory of %s", user);
	}
	service->available = default_service->available;
	service->browseable = default_service->browseable;

	DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
		  pszHomename, user, service->path));

	return true;
}

/**
 * Add a new printer service, with defaults coming from service iFrom.
 */

bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
		       const char *pszPrintername,
		       struct loadparm_service *default_service)
{
	const char *comment = "From Printcap";
	struct loadparm_service *service;
	service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);

	if (service == NULL)
		return false;

	/* note that we do NOT default the availability flag to True - */
	/* we take it from the default service passed. This allows all */
	/* dynamic printers to be disabled by disabling the [printers] */
	/* entry (if/when the 'available' keyword is implemented!).    */

	/* the printer name is set to the service name. */
	lpcfg_string_set(service, &service->_printername, pszPrintername);
	lpcfg_string_set(service, &service->comment, comment);
	service->browseable = default_service->browseable;
	/* Printers cannot be read_only. */
	service->read_only = false;
	/* Printer services must be printable. */
	service->printable = true;

	DEBUG(3, ("adding printer service %s\n", pszPrintername));

	return true;
}

/**
 * Map a parameter's string representation to something we can use.
 * Returns False if the parameter string is not recognised, else TRUE.
 */

int lpcfg_map_parameter(const char *pszParmName)
{
	int iIndex;

	for (iIndex = 0; parm_table[iIndex].label; iIndex++)
		if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
			return iIndex;

	/* Warn only if it isn't parametric option */
	if (strchr(pszParmName, ':') == NULL)
		DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
	/* We do return 'fail' for parametric options as well because they are
	   stored in different storage
	 */
	return -1;
}


/**
  return the parameter structure for a parameter
*/
struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
{
	int num = lpcfg_map_parameter(name);

	if (num < 0) {
		return NULL;
	}

	return &parm_table[num];
}

/**
  return the parameter pointer for a parameter
*/
void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
		  struct loadparm_service *service, struct parm_struct *parm)
{
	if (lp_ctx->s3_fns) {
		return lp_ctx->s3_fns->get_parm_ptr(service, parm);
	}

	if (service == NULL) {
		if (parm->p_class == P_LOCAL)
			return ((char *)lp_ctx->sDefault)+parm->offset;
		else if (parm->p_class == P_GLOBAL)
			return ((char *)lp_ctx->globals)+parm->offset;
		else return NULL;
	} else {
		return ((char *)service) + parm->offset;
	}
}

/**
  return the parameter pointer for a parameter
*/
bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
{
	int parmnum;

	parmnum = lpcfg_map_parameter(name);
	if (parmnum == -1) return false;

	return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
}

/**
 * Find a service by name. Otherwise works like get_service.
 */

static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
					const char *pszServiceName)
{
	int iService;

	if (lp_ctx->s3_fns) {
		return lp_ctx->s3_fns->get_service(pszServiceName);
	}

	for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
		if (lp_ctx->services[iService] != NULL &&
		    strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
			return lp_ctx->services[iService];
		}

	return NULL;
}

/**
 * Add a parametric option to a parmlist_entry,
 * replacing old value, if already present.
 */
void set_param_opt(TALLOC_CTX *mem_ctx,
		   struct parmlist_entry **opt_list,
		   const char *opt_name,
		   const char *opt_value,
		   unsigned priority)
{
	struct parmlist_entry *new_opt, *opt;

	opt = *opt_list;

	/* Traverse destination */
	while (opt) {
		/* If we already have same option, override it */
		if (strwicmp(opt->key, opt_name) == 0) {
			if ((opt->priority & FLAG_CMDLINE) &&
			    !(priority & FLAG_CMDLINE)) {
				/* it's been marked as not to be
				   overridden */
				return;
			}
			TALLOC_FREE(opt->list);
			lpcfg_string_set(opt, &opt->value, opt_value);
			opt->priority = priority;
			return;
		}
		opt = opt->next;
	}

	new_opt = talloc_pooled_object(
		mem_ctx, struct parmlist_entry,
		2, strlen(opt_name) + 1 + strlen(opt_value) + 1);
	if (new_opt == NULL) {
		smb_panic("OOM");
	}
	new_opt->key = NULL;
	lpcfg_string_set(new_opt, &new_opt->key, opt_name);
	new_opt->value = NULL;
	lpcfg_string_set(new_opt, &new_opt->value, opt_value);

	new_opt->list = NULL;
	new_opt->priority = priority;
	DLIST_ADD(*opt_list, new_opt);
}

/**
 * Copy a service structure to another.
 * If pcopymapDest is NULL then copy all fields
 */

void copy_service(struct loadparm_service *pserviceDest,
		  const struct loadparm_service *pserviceSource,
		  struct bitmap *pcopymapDest)
{
	int i;
	bool bcopyall = (pcopymapDest == NULL);
	struct parmlist_entry *data;

	for (i = 0; parm_table[i].label; i++)
		if (parm_table[i].p_class == P_LOCAL &&
		    (bcopyall || bitmap_query(pcopymapDest, i))) {
			const void *src_ptr =
				((const char *)pserviceSource) + parm_table[i].offset;
			void *dest_ptr =
				((char *)pserviceDest) + parm_table[i].offset;

			switch (parm_table[i].type) {
				case P_BOOL:
				case P_BOOLREV:
					*(bool *)dest_ptr = *(const bool *)src_ptr;
					break;

				case P_INTEGER:
				case P_BYTES:
				case P_OCTAL:
				case P_ENUM:
					*(int *)dest_ptr = *(const int *)src_ptr;
					break;

				case P_CHAR:
					*(char *)dest_ptr = *(const char *)src_ptr;
					break;

				case P_STRING:
					lpcfg_string_set(pserviceDest,
						   (char **)dest_ptr,
						   *(const char * const *)src_ptr);
					break;

				case P_USTRING:
					lpcfg_string_set_upper(pserviceDest,
							 (char **)dest_ptr,
							 *(const char * const *)src_ptr);
					break;
				case P_CMDLIST:
				case P_LIST:
					TALLOC_FREE(*((char ***)dest_ptr));
					*(char ***)dest_ptr = str_list_copy(pserviceDest,
									    *discard_const_p(const char **, src_ptr));
					break;
				default:
					break;
			}
		}

	if (bcopyall) {
		init_copymap(pserviceDest);
		if (pserviceSource->copymap)
			bitmap_copy(pserviceDest->copymap,
				    pserviceSource->copymap);
	}

	for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
		set_param_opt(pserviceDest, &pserviceDest->param_opt,
			      data->key, data->value, data->priority);
	}
}

/**
 * Check a service for consistency. Return False if the service is in any way
 * incomplete or faulty, else True.
 */
bool lpcfg_service_ok(struct loadparm_service *service)
{
	bool bRetval;

	bRetval = true;
	if (service->szService[0] == '\0') {
		DEBUG(0, ("The following message indicates an internal error:\n"));
		DEBUG(0, ("No service name in service entry.\n"));
		bRetval = false;
	}

	/* The [printers] entry MUST be printable. I'm all for flexibility, but */
	/* I can't see why you'd want a non-printable printer service...        */
	if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
		if (!service->printable) {
			DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
			       service->szService));
			service->printable = true;
		}
		/* [printers] service must also be non-browsable. */
		if (service->browseable)
			service->browseable = false;
	}

	if (service->path[0] == '\0' &&
	    strwicmp(service->szService, HOMES_NAME) != 0 &&
	    service->msdfs_proxy[0] == '\0')
	{
		DEBUG(0, ("WARNING: No path in service %s - making it unavailable!\n",
			service->szService));
		service->available = false;
	}

	if (!service->available)
		DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
			  service->szService));

	return bRetval;
}


/*******************************************************************
 Keep a linked list of all config files so we know when one has changed
 it's date and needs to be reloaded.
********************************************************************/

void add_to_file_list(TALLOC_CTX *mem_ctx, struct file_lists **list,
			     const char *fname, const char *subfname)
{
	struct file_lists *f = *list;

	while (f) {
		if (f->name && !strcmp(f->name, fname))
			break;
		f = f->next;
	}

	if (!f) {
		f = talloc(mem_ctx, struct file_lists);
		if (!f)
			goto fail;
		f->next = *list;
		f->name = talloc_strdup(f, fname);
		if (!f->name) {
			TALLOC_FREE(f);
			goto fail;
		}
		f->subfname = talloc_strdup(f, subfname);
		if (!f->subfname) {
			TALLOC_FREE(f);
			goto fail;
		}
		*list = f;
		f->modtime = file_modtime(subfname);
	} else {
		time_t t = file_modtime(subfname);
		if (t)
			f->modtime = t;
	}
	return;

fail:
	DEBUG(0, ("Unable to add file to file list: %s\n", fname));

}

/*******************************************************************
 Check if a config file has changed date.
********************************************************************/
bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
{
	struct file_lists *f;
	DEBUG(6, ("lpcfg_file_list_changed()\n"));

	for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
		char *n2;
		time_t mod_time;

		n2 = standard_sub_basic(lp_ctx, f->name);

		DEBUGADD(6, ("file %s -> %s  last mod_time: %s\n",
			     f->name, n2, ctime(&f->modtime)));

		mod_time = file_modtime(n2);

		if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
			DEBUGADD(6, ("file %s modified: %s\n", n2,
				  ctime(&mod_time)));
			f->modtime = mod_time;
			talloc_free(f->subfname);
			f->subfname = talloc_strdup(f, n2);
			TALLOC_FREE(n2);
			return true;
		}
		TALLOC_FREE(n2);
	}
	return false;
}

/*
 * set the value for a P_ENUM
 */
bool lp_set_enum_parm( struct parm_struct *parm, const char *pszParmValue,
                              int *ptr )
{
	int i;

	for (i = 0; parm->enum_list[i].name; i++) {
		if (strwicmp(pszParmValue, parm->enum_list[i].name) == 0) {
			*ptr = parm->enum_list[i].value;
			return true;
		}
	}
	DEBUG(0, ("WARNING: Ignoring invalid value '%s' for parameter '%s'\n",
		  pszParmValue, parm->label));
	return false;
}


/***************************************************************************
 Handle the "realm" parameter
***************************************************************************/

bool handle_realm(struct loadparm_context *lp_ctx, struct loadparm_service *service,
		  const char *pszParmValue, char **ptr)
{
	char *upper;
	char *lower;

	upper = strupper_talloc(lp_ctx, pszParmValue);
	if (upper == NULL) {
		return false;
	}

	lower = strlower_talloc(lp_ctx, pszParmValue);
	if (lower == NULL) {
		TALLOC_FREE(upper);
		return false;
	}

	lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->realm_original, pszParmValue);
	lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->realm, upper);
	lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->dnsdomain, lower);

	return true;
}

/***************************************************************************
 Handle the include operation.
***************************************************************************/

bool handle_include(struct loadparm_context *lp_ctx, struct loadparm_service *service,
			   const char *pszParmValue, char **ptr)
{
	char *fname;
	const char *substitution_variable_substring;
	char next_char;

	if (lp_ctx->s3_fns) {
		return lp_ctx->s3_fns->lp_include(lp_ctx, service, pszParmValue, ptr);
	}

	fname = standard_sub_basic(lp_ctx, pszParmValue);

	add_to_file_list(lp_ctx, &lp_ctx->file_lists, pszParmValue, fname);

	lpcfg_string_set(lp_ctx, ptr, fname);

	if (file_exist(fname))
		return pm_process(fname, do_section, lpcfg_do_parameter, lp_ctx);

       /*
        * If the file doesn't exist, we check that it isn't due to variable
        * substitution
        */
	substitution_variable_substring = strchr(fname, '%');

	if (substitution_variable_substring != NULL) {
		next_char = substitution_variable_substring[1];
		if ((next_char >= 'a' && next_char <= 'z')
		    || (next_char >= 'A' && next_char <= 'Z')) {
			DEBUG(2, ("Tried to load %s but variable substitution in "
				 "filename, ignoring file.\n", fname));
			return true;
		}
	}

	DEBUG(2, ("Can't find include file %s\n", fname));

	return false;
}

/***************************************************************************
 Handle the interpretation of the copy parameter.
***************************************************************************/

bool handle_copy(struct loadparm_context *lp_ctx, struct loadparm_service *service,
			const char *pszParmValue, char **ptr)
{
	bool bRetval;
	struct loadparm_service *serviceTemp = NULL;

	bRetval = false;

	DEBUG(3, ("Copying service from service %s\n", pszParmValue));

	serviceTemp = lpcfg_getservicebyname(lp_ctx, pszParmValue);

	if (service == NULL) {
		DEBUG(0, ("Unable to copy service - invalid service destination.\n"));
		return false;
	}

	if (serviceTemp != NULL) {
		if (serviceTemp == service) {
			DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
		} else {
			copy_service(service,
				     serviceTemp,
				     service->copymap);
			lpcfg_string_set(service, ptr, pszParmValue);

			bRetval = true;
		}
	} else {
		DEBUG(0, ("Unable to copy service - source not found: %s\n",
			  pszParmValue));
		bRetval = false;
	}

	return bRetval;
}

bool handle_debug_list(struct loadparm_context *lp_ctx, struct loadparm_service *service,
			const char *pszParmValue, char **ptr)
{
	lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);

	return debug_parse_levels(pszParmValue);
}

bool handle_logfile(struct loadparm_context *lp_ctx, struct loadparm_service *service,
		    const char *pszParmValue, char **ptr)
{
	if (lp_ctx->s3_fns == NULL) {
		debug_set_logfile(pszParmValue);
	}

	lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);

	return true;
}

/*
 * These special charset handling methods only run in the source3 code.
 */

bool handle_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
			const char *pszParmValue, char **ptr)
{
	if (lp_ctx->s3_fns) {
		if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
			global_iconv_handle = smb_iconv_handle_reinit(NULL,
							lpcfg_dos_charset(lp_ctx),
							lpcfg_unix_charset(lp_ctx),
							true, global_iconv_handle);
		}

	}
	return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);

}

bool handle_dos_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
			const char *pszParmValue, char **ptr)
{
	bool is_utf8 = false;
	size_t len = strlen(pszParmValue);

	if (lp_ctx->s3_fns) {
		if (len == 4 || len == 5) {
			/* Don't use StrCaseCmp here as we don't want to
			   initialize iconv. */
			if ((toupper_m(pszParmValue[0]) == 'U') &&
			    (toupper_m(pszParmValue[1]) == 'T') &&
			    (toupper_m(pszParmValue[2]) == 'F')) {
				if (len == 4) {
					if (pszParmValue[3] == '8') {
						is_utf8 = true;
					}
				} else {
					if (pszParmValue[3] == '-' &&
					    pszParmValue[4] == '8') {
						is_utf8 = true;
					}
				}
			}
		}

		if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
			if (is_utf8) {
				DEBUG(0,("ERROR: invalid DOS charset: 'dos charset' must not "
					"be UTF8, using (default value) %s instead.\n",
					DEFAULT_DOS_CHARSET));
				pszParmValue = DEFAULT_DOS_CHARSET;
			}
			global_iconv_handle = smb_iconv_handle_reinit(NULL,
							lpcfg_dos_charset(lp_ctx),
							lpcfg_unix_charset(lp_ctx),
							true, global_iconv_handle);
		}
	}

	return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
}

bool handle_printing(struct loadparm_context *lp_ctx, struct loadparm_service *service,
			    const char *pszParmValue, char **ptr)
{
	static int parm_num = -1;

	if (parm_num == -1) {
		parm_num = lpcfg_map_parameter("printing");
	}

	if (!lp_set_enum_parm(&parm_table[parm_num], pszParmValue, (int*)ptr)) {
		return false;
	}

	if (lp_ctx->s3_fns) {
		if (service == NULL) {
			init_printer_values(lp_ctx, lp_ctx->globals->ctx, lp_ctx->sDefault);
		} else {
			init_printer_values(lp_ctx, service, service);
		}
	}

	return true;
}

bool handle_ldap_debug_level(struct loadparm_context *lp_ctx, struct loadparm_service *service,
			     const char *pszParmValue, char **ptr)
{
	lp_ctx->globals->ldap_debug_level = lp_int(pszParmValue);

	if (lp_ctx->s3_fns) {
		lp_ctx->s3_fns->init_ldap_debugging();
	}
	return true;
}

bool handle_netbios_aliases(struct loadparm_context *lp_ctx, struct loadparm_service *service,
			    const char *pszParmValue, char **ptr)
{
	TALLOC_FREE(lp_ctx->globals->netbios_aliases);
	lp_ctx->globals->netbios_aliases = str_list_make_v3_const(lp_ctx->globals->ctx,
								  pszParmValue, NULL);

	if (lp_ctx->s3_fns) {
		return lp_ctx->s3_fns->set_netbios_aliases(lp_ctx->globals->netbios_aliases);
	}
	return true;
}

/*
 * idmap related parameters
 */

bool handle_idmap_backend(struct loadparm_context *lp_ctx, struct loadparm_service *service,
			  const char *pszParmValue, char **ptr)
{
	if (lp_ctx->s3_fns) {
		lp_do_parameter_parametric(lp_ctx, service, "idmap config * : backend",
					   pszParmValue, 0);
	}

	return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
}

bool handle_idmap_uid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
		      const char *pszParmValue, char **ptr)
{
	if (lp_ctx->s3_fns) {
		lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
					   pszParmValue, 0);
	}

	return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
}

bool handle_idmap_gid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
		      const char *pszParmValue, char **ptr)
{
	if (lp_ctx->s3_fns) {
		lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
					   pszParmValue, 0);
	}

	return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
}

bool handle_smb_ports(struct loadparm_context *lp_ctx, struct loadparm_service *service,
		      const char *pszParmValue, char **ptr)
{
	static int parm_num = -1;
	int i;
	const char **list;

	if (!pszParmValue || !*pszParmValue) {
		return false;
	}

	if (parm_num == -1) {
		parm_num = lpcfg_map_parameter("smb ports");
		if (parm_num == -1) {
			return false;
		}
	}

	if(!set_variable_helper(lp_ctx->globals->ctx, parm_num, ptr, "smb ports",
			       	pszParmValue)) {
		return false;
	}

	list = lp_ctx->globals->smb_ports;
	if (list == NULL) {
		return false;
	}

	/* Check that each port is a valid integer and within range */
	for (i = 0; list[i] != NULL; i++) {
		char *end = NULL;
		int port = 0;
		port = strtol(list[i], &end, 10);
		if (*end != '\0' || port <= 0 || port > 65535) {
			TALLOC_FREE(list);
			return false;
		}
	}

	return true;
}

bool handle_smb2_max_credits(struct loadparm_context *lp_ctx,
			     struct loadparm_service *service,
			     const char *pszParmValue, char **ptr)
{
	int value = lp_int(pszParmValue);

	if (value <= 0) {
		value = DEFAULT_SMB2_MAX_CREDITS;
	}

	*(int *)ptr = value;

	return true;
}

bool handle_cups_encrypt(struct loadparm_context *lp_ctx,
			 struct loadparm_service *service,
			 const char *pszParmValue, char **ptr)
{
	int result = 0;
#ifdef HAVE_HTTPCONNECTENCRYPT
	int value = lp_int(pszParmValue);

	switch (value) {
		case Auto:
			result = HTTP_ENCRYPT_REQUIRED;
			break;
		case true:
			result = HTTP_ENCRYPT_ALWAYS;
			break;
		case false:
			result = HTTP_ENCRYPT_NEVER;
			break;
		default:
			result = 0;
			break;
	}
#endif
	*(int *)ptr = result;

	return true;
}

/***************************************************************************
 Initialise a copymap.
***************************************************************************/

/**
 * Initializes service copymap
 * Note: pservice *must* be valid TALLOC_CTX
 */
void init_copymap(struct loadparm_service *pservice)
{
	int i;

	TALLOC_FREE(pservice->copymap);

	pservice->copymap = bitmap_talloc(pservice, num_parameters());
	if (!pservice->copymap) {
		DEBUG(0,
		      ("Couldn't allocate copymap!! (size %d)\n",
		       (int)num_parameters()));
	} else {
		for (i = 0; i < num_parameters(); i++) {
			bitmap_set(pservice->copymap, i);
		}
	}
}

/**
 * Process a parametric option
 */
static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
				       struct loadparm_service *service,
				       const char *pszParmName,
				       const char *pszParmValue, int flags)
{
	struct parmlist_entry **data;
	char *name;
	TALLOC_CTX *mem_ctx;

	while (isspace((unsigned char)*pszParmName)) {
		pszParmName++;
	}

	name = strlower_talloc(lp_ctx, pszParmName);
	if (!name) return false;

	if (service == NULL) {
		data = &lp_ctx->globals->param_opt;
		/**
		 * s3 code cannot deal with parametric options stored on the globals ctx.
		 */
		if (lp_ctx->s3_fns != NULL) {
			mem_ctx = NULL;
		} else {
			mem_ctx = lp_ctx->globals->ctx;
		}
	} else {
		data = &service->param_opt;
		mem_ctx = service;
	}

	set_param_opt(mem_ctx, data, name, pszParmValue, flags);

	talloc_free(name);

	return true;
}

static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
			 const char *pszParmName, const char *pszParmValue)
{
	int i;

	/* switch on the type of variable it is */
	switch (parm_table[parmnum].type)
	{
		case P_BOOL: {
			bool b;
			if (!set_boolean(pszParmValue, &b)) {
				DEBUG(0, ("set_variable_helper(%s): value is not "
					  "boolean!\n", pszParmValue));
				return false;
			}
			*(bool *)parm_ptr = b;
			}
			break;

		case P_BOOLREV: {
			bool b;
			if (!set_boolean(pszParmValue, &b)) {
				DEBUG(0, ("set_variable_helper(%s): value is not "
					  "boolean!\n", pszParmValue));
				return false;
			}
			*(bool *)parm_ptr = !b;
			}
			break;

		case P_INTEGER:
			*(int *)parm_ptr = lp_int(pszParmValue);
			break;

		case P_CHAR:
			*(char *)parm_ptr = *pszParmValue;
			break;

		case P_OCTAL:
			i = sscanf(pszParmValue, "%o", (int *)parm_ptr);
			if ( i != 1 ) {
				DEBUG ( 0, ("Invalid octal number %s\n", pszParmName ));
				return false;
			}
			break;

		case P_BYTES:
		{
			uint64_t val;
			if (conv_str_size_error(pszParmValue, &val)) {
				if (val <= INT_MAX) {
					*(int *)parm_ptr = (int)val;
					break;
				}
			}

			DEBUG(0, ("set_variable_helper(%s): value is not "
			          "a valid size specifier!\n", pszParmValue));
			return false;
		}

		case P_CMDLIST:
			TALLOC_FREE(*(char ***)parm_ptr);
			*(char ***)parm_ptr = str_list_make_v3(mem_ctx,
							pszParmValue, NULL);
			break;

		case P_LIST:
		{
			char **new_list = str_list_make_v3(mem_ctx,
							pszParmValue, NULL);
			if (new_list == NULL) {
				break;
			}

			for (i=0; new_list[i]; i++) {
				if (*(const char ***)parm_ptr != NULL &&
				    new_list[i][0] == '+' &&
				    new_list[i][1])
				{
					if (!str_list_check(*(const char ***)parm_ptr,
							    &new_list[i][1])) {
						*(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
											 &new_list[i][1]);
					}
				} else if (*(const char ***)parm_ptr != NULL &&
					   new_list[i][0] == '-' &&
					   new_list[i][1])
				{
					str_list_remove(*(const char ***)parm_ptr,
							&new_list[i][1]);
				} else {
					if (i != 0) {
						DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
							  pszParmName, pszParmValue));
						return false;
					}
					*(char ***)parm_ptr = new_list;
					break;
				}
			}
			break;
		}

		case P_STRING:
			lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
			break;

		case P_USTRING:
			lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
			break;

		case P_ENUM:
			if (!lp_set_enum_parm(&parm_table[parmnum], pszParmValue, (int*)parm_ptr)) {
				return false;
			}
			break;

	}

	return true;

}

static bool set_variable(TALLOC_CTX *mem_ctx, struct loadparm_service *service,
			 int parmnum, void *parm_ptr,
			 const char *pszParmName, const char *pszParmValue,
			 struct loadparm_context *lp_ctx, bool on_globals)
{
	int i;
	bool ok;

	/* if it is a special case then go ahead */
	if (parm_table[parmnum].special) {
		ok = parm_table[parmnum].special(lp_ctx, service, pszParmValue,
						  (char **)parm_ptr);
	} else {
		ok = set_variable_helper(mem_ctx, parmnum, parm_ptr,
					 pszParmName, pszParmValue);
	}

	if (!ok) {
		return false;
	}

	if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
		lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
		/* we have to also unset FLAG_DEFAULT on aliases */
		for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
			lp_ctx->flags[i] &= ~FLAG_DEFAULT;
		}
		for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset;i++) {
			lp_ctx->flags[i] &= ~FLAG_DEFAULT;
		}
	}
	return true;
}


bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
			       const char *pszParmName, const char *pszParmValue)
{
	int parmnum = lpcfg_map_parameter(pszParmName);
	void *parm_ptr;

	if (parmnum < 0) {
		if (strchr(pszParmName, ':')) {
			return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
		}
		DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
		return true;
	}

	/* if the flag has been set on the command line, then don't allow override,
	   but don't report an error */
	if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
		return true;
	}

	if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
		DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
			  pszParmName));
	}

	parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);

	return set_variable(lp_ctx->globals->ctx, NULL, parmnum, parm_ptr,
			    pszParmName, pszParmValue, lp_ctx, true);
}

bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
				struct loadparm_service *service,
				const char *pszParmName, const char *pszParmValue)
{
	void *parm_ptr;
	int i;
	int parmnum = lpcfg_map_parameter(pszParmName);

	if (parmnum < 0) {
		if (strchr(pszParmName, ':')) {
			return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
		}
		DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
		return true;
	}

	/* if the flag has been set on the command line, then don't allow override,
	   but don't report an error */
	if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
		return true;
	}

	if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
		DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
			  pszParmName));
	}

	if (parm_table[parmnum].p_class == P_GLOBAL) {
		DEBUG(0,
		      ("Global parameter %s found in service section!\n",
		       pszParmName));
		return true;
	}
	parm_ptr = ((char *)service) + parm_table[parmnum].offset;

	if (!service->copymap)
		init_copymap(service);

	/* this handles the aliases - set the copymap for other
	 * entries with the same data pointer */
	for (i = 0; parm_table[i].label; i++)
		if (parm_table[i].offset == parm_table[parmnum].offset &&
		    parm_table[i].p_class == parm_table[parmnum].p_class)
			bitmap_clear(service->copymap, i);

	return set_variable(service, service, parmnum, parm_ptr, pszParmName,
			    pszParmValue, lp_ctx, false);
}

/**
 * Process a parameter.
 */

bool lpcfg_do_parameter(const char *pszParmName, const char *pszParmValue,
			 void *userdata)
{
	struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;

	if (lp_ctx->bInGlobalSection)
		return lpcfg_do_global_parameter(lp_ctx, pszParmName,
					      pszParmValue);
	else
		return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
						  pszParmName, pszParmValue);
}

/*
  variable argument do parameter
*/
bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
				const char *pszParmName, const char *fmt, ...)
{
	char *s;
	bool ret;
	va_list ap;

	va_start(ap, fmt);
	s = talloc_vasprintf(NULL, fmt, ap);
	va_end(ap);
	ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
	talloc_free(s);
	return ret;
}


/*
  set a parameter from the commandline - this is called from command line parameter
  parsing code. It sets the parameter then marks the parameter as unable to be modified
  by smb.conf processing
*/
bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
		       const char *pszParmValue)
{
	int parmnum;
	int i;

	while (isspace((unsigned char)*pszParmValue)) pszParmValue++;

	parmnum = lpcfg_map_parameter(pszParmName);

	if (parmnum < 0 && strchr(pszParmName, ':')) {
		/* set a parametric option */
		bool ok;
		ok = lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
						pszParmValue, FLAG_CMDLINE);
		if (lp_ctx->s3_fns != NULL) {
			if (ok) {
				lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
			}
		}
		return ok;
	}

	if (parmnum < 0) {
		DEBUG(0,("Unknown option '%s'\n", pszParmName));
		return false;
	}

	/* reset the CMDLINE flag in case this has been called before */
	lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;

	if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
		return false;
	}

	lp_ctx->flags[parmnum] |= FLAG_CMDLINE;

	/* we have to also set FLAG_CMDLINE on aliases */
	for (i=parmnum-1;
	     i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
	     parm_table[i].offset == parm_table[parmnum].offset;
	     i--) {
		lp_ctx->flags[i] |= FLAG_CMDLINE;
	}
	for (i=parmnum+1;
	     i<num_parameters() &&
	     parm_table[i].p_class == parm_table[parmnum].p_class &&
	     parm_table[i].offset == parm_table[parmnum].offset;
	     i++) {
		lp_ctx->flags[i] |= FLAG_CMDLINE;
	}

	if (lp_ctx->s3_fns != NULL) {
		lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
	}

	return true;
}

/*
  set a option from the commandline in 'a=b' format. Use to support --option
*/
bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
{
	char *p, *s;
	bool ret;

	s = talloc_strdup(NULL, option);
	if (!s) {
		return false;
	}

	p = strchr(s, '=');
	if (!p) {
		talloc_free(s);
		return false;
	}

	*p = 0;

	ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
	talloc_free(s);
	return ret;
}


#define BOOLSTR(b) ((b) ? "Yes" : "No")

/**
 * Print a parameter of the specified type.
 */

void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
{
	/* For the seperation of lists values that we print below */
	const char *list_sep = ", ";
	int i;
	switch (p->type)
	{
		case P_ENUM:
			for (i = 0; p->enum_list[i].name; i++) {
				if (*(int *)ptr == p->enum_list[i].value) {
					fprintf(f, "%s",
						p->enum_list[i].name);
					break;
				}
			}
			break;

		case P_BOOL:
			fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
			break;

		case P_BOOLREV:
			fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
			break;

		case P_INTEGER:
		case P_BYTES:
			fprintf(f, "%d", *(int *)ptr);
			break;

		case P_CHAR:
			fprintf(f, "%c", *(char *)ptr);
			break;

		case P_OCTAL: {
			int val = *(int *)ptr; 
			if (val == -1) {
				fprintf(f, "-1");
			} else {
				fprintf(f, "0%03o", val);
			}
			break;
		}

		case P_CMDLIST:
			list_sep = " ";
			/* fall through */
		case P_LIST:
			if ((char ***)ptr && *(char ***)ptr) {
				char **list = *(char ***)ptr;
				for (; *list; list++) {
					/* surround strings with whitespace in double quotes */
					if (*(list+1) == NULL) {
						/* last item, no extra separator */
						list_sep = "";
					}
					if ( strchr_m( *list, ' ' ) ) {
						fprintf(f, "\"%s\"%s", *list, list_sep);
					} else {
						fprintf(f, "%s%s", *list, list_sep);
					}
				}
			}
			break;

		case P_STRING:
		case P_USTRING:
			if (*(char **)ptr) {
				fprintf(f, "%s", *(char **)ptr);
			}
			break;
	}
}

/**
 * Check if two parameters are equal.
 */

static bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
{
	switch (type) {
		case P_BOOL:
		case P_BOOLREV:
			return (*((bool *)ptr1) == *((bool *)ptr2));

		case P_INTEGER:
		case P_ENUM:
		case P_OCTAL:
		case P_BYTES:
			return (*((int *)ptr1) == *((int *)ptr2));

		case P_CHAR:
			return (*((char *)ptr1) == *((char *)ptr2));

		case P_LIST:
		case P_CMDLIST:
			return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);

		case P_STRING:
		case P_USTRING:
		{
			char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
			if (p1 && !*p1)
				p1 = NULL;
			if (p2 && !*p2)
				p2 = NULL;
			return (p1 == p2 || strequal(p1, p2));
		}
	}
	return false;
}

/**
 * Process a new section (service).
 *
 * At this stage all sections are services.
 * Later we'll have special sections that permit server parameters to be set.
 * Returns True on success, False on failure.
 */

static bool do_section(const char *pszSectionName, void *userdata)
{
	struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
	bool bRetval;
	bool isglobal;

	if (lp_ctx->s3_fns != NULL) {
		return lp_ctx->s3_fns->do_section(pszSectionName, lp_ctx);
	}

	isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
			 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));

	bRetval = false;

	/* if we've just struck a global section, note the fact. */
	lp_ctx->bInGlobalSection = isglobal;

	/* check for multiple global sections */
	if (lp_ctx->bInGlobalSection) {
		DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
		return true;
	}

	/* if we have a current service, tidy it up before moving on */
	bRetval = true;

	if (lp_ctx->currentService != NULL)
		bRetval = lpcfg_service_ok(lp_ctx->currentService);

	/* if all is still well, move to the next record in the services array */
	if (bRetval) {
		/* We put this here to avoid an odd message order if messages are */
		/* issued by the post-processing of a previous section. */
		DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));

		if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
								   pszSectionName))
		    == NULL) {
			DEBUG(0, ("Failed to add a new service\n"));
			return false;
		}
	}

	return bRetval;
}


/**
 * Determine if a particular base parameter is currently set to the default value.
 */

static bool is_default(void *base_structure, int i)
{
	void *def_ptr = ((char *)base_structure) + parm_table[i].offset;
	switch (parm_table[i].type) {
		case P_CMDLIST:
		case P_LIST:
			return str_list_equal((const char * const *)parm_table[i].def.lvalue,
					      *(const char * const **)def_ptr);
		case P_STRING:
		case P_USTRING:
			return strequal(parm_table[i].def.svalue,
					*(char **)def_ptr);
		case P_BOOL:
		case P_BOOLREV:
			return parm_table[i].def.bvalue ==
				*(bool *)def_ptr;
		case P_INTEGER:
		case P_CHAR:
		case P_OCTAL:
		case P_BYTES:
		case P_ENUM:
			return parm_table[i].def.ivalue ==
				*(int *)def_ptr;
	}
	return false;
}

/**
 *Display the contents of the global structure.
 */

void lpcfg_dump_globals(struct loadparm_context *lp_ctx, FILE *f,
			 bool show_defaults)
{
	int i;
	struct parmlist_entry *data;

	fprintf(f, "# Global parameters\n[global]\n");

	for (i = 0; parm_table[i].label; i++) {
		if (parm_table[i].p_class != P_GLOBAL) {
			continue;
		}

		if (parm_table[i].flags & FLAG_SYNONYM) {
			continue;
		}

		if (!show_defaults) {
			if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
				continue;
			}

			if (is_default(lp_ctx->globals, i)) {
				continue;
			}
		}

		fprintf(f, "\t%s = ", parm_table[i].label);
		lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
		fprintf(f, "\n");
	}
	if (lp_ctx->globals->param_opt != NULL) {
		for (data = lp_ctx->globals->param_opt; data;
		     data = data->next) {
			if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
				continue;
			}
			fprintf(f, "\t%s = %s\n", data->key, data->value);
		}
        }

}

/**
 * Display the contents of a single services record.
 */

void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
			  unsigned int *flags, bool show_defaults)
{
	int i;
	struct parmlist_entry *data;

	if (pService != sDefault)
		fprintf(f, "\n[%s]\n", pService->szService);

	for (i = 0; parm_table[i].label; i++) {
		if (parm_table[i].p_class != P_LOCAL) {
			continue;
		}

		if (parm_table[i].flags & FLAG_SYNONYM) {
			continue;
		}

		if (*parm_table[i].label == '-') {
			continue;
		}

		if (pService == sDefault) {
			if (!show_defaults) {
				if (flags && (flags[i] & FLAG_DEFAULT)) {
					continue;
				}

				if (is_default(sDefault, i)) {
					continue;
				}
			}
		} else {
			bool equal;

			equal = lpcfg_equal_parameter(parm_table[i].type,
						      ((char *)pService) +
						      parm_table[i].offset,
						      ((char *)sDefault) +
						      parm_table[i].offset);
			if (equal) {
				continue;
			}
		}

		fprintf(f, "\t%s = ", parm_table[i].label);
		lpcfg_print_parameter(&parm_table[i],
				((char *)pService) + parm_table[i].offset, f);
		fprintf(f, "\n");
	}
	if (pService->param_opt != NULL) {
		for (data = pService->param_opt; data; data = data->next) {
			if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
				continue;
			}
			fprintf(f, "\t%s = %s\n", data->key, data->value);
		}
        }
}

bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
			    struct loadparm_service *service,
			    const char *parm_name, FILE * f)
{
	struct parm_struct *parm;
	void *ptr;
	char *local_parm_name;
	char *parm_opt;
	const char *parm_opt_value;

	/* check for parametrical option */
	local_parm_name = talloc_strdup(lp_ctx, parm_name);
	if (local_parm_name == NULL) {
		return false;
	}

	parm_opt = strchr( local_parm_name, ':');

	if (parm_opt) {
		*parm_opt = '\0';
		parm_opt++;
		if (strlen(parm_opt)) {
			parm_opt_value = lpcfg_parm_string(lp_ctx, service,
				local_parm_name, parm_opt);
			if (parm_opt_value) {
				fprintf(f, "%s\n", parm_opt_value);
				return true;
			}
		}
		return false;
	}

	/* parameter is not parametric, search the table */
	parm = lpcfg_parm_struct(lp_ctx, parm_name);
	if (!parm) {
		return false;
	}

	if (service != NULL && parm->p_class == P_GLOBAL) {
		return false;
	}

	ptr = lpcfg_parm_ptr(lp_ctx, service,parm);

	lpcfg_print_parameter(parm, ptr, f);
	fprintf(f, "\n");
	return true;
}

/**
 * Auto-load some home services.
 */
static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
				    const char *str)
{
	return;
}

/***************************************************************************
 Initialise the sDefault parameter structure for the printer values.
***************************************************************************/

void init_printer_values(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx,
			 struct loadparm_service *pService)
{
	/* choose defaults depending on the type of printing */
	switch (pService->printing) {
		case PRINT_BSD:
		case PRINT_AIX:
		case PRINT_LPRNT:
		case PRINT_LPROS2:
			lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
			lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
			lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
			break;

		case PRINT_LPRNG:
		case PRINT_PLP:
			lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
			lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
			lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
			lpcfg_string_set(ctx, &pService->queuepause_command, "lpc stop '%p'");
			lpcfg_string_set(ctx, &pService->queueresume_command, "lpc start '%p'");
			lpcfg_string_set(ctx, &pService->lppause_command, "lpc hold '%p' %j");
			lpcfg_string_set(ctx, &pService->lpresume_command, "lpc release '%p' %j");
			break;

		case PRINT_CUPS:
		case PRINT_IPRINT:
			/* set the lpq command to contain the destination printer
			   name only.  This is used by cups_queue_get() */
			lpcfg_string_set(ctx, &pService->lpq_command, "%p");
			lpcfg_string_set(ctx, &pService->lprm_command, "");
			lpcfg_string_set(ctx, &pService->print_command, "");
			lpcfg_string_set(ctx, &pService->lppause_command, "");
			lpcfg_string_set(ctx, &pService->lpresume_command, "");
			lpcfg_string_set(ctx, &pService->queuepause_command, "");
			lpcfg_string_set(ctx, &pService->queueresume_command, "");
			break;

		case PRINT_SYSV:
		case PRINT_HPUX:
			lpcfg_string_set(ctx, &pService->lpq_command, "lpstat -o%p");
			lpcfg_string_set(ctx, &pService->lprm_command, "cancel %p-%j");
			lpcfg_string_set(ctx, &pService->print_command, "lp -c -d%p %s; rm %s");
			lpcfg_string_set(ctx, &pService->queuepause_command, "disable %p");
			lpcfg_string_set(ctx, &pService->queueresume_command, "enable %p");
#ifndef HPUX
			lpcfg_string_set(ctx, &pService->lppause_command, "lp -i %p-%j -H hold");
			lpcfg_string_set(ctx, &pService->lpresume_command, "lp -i %p-%j -H resume");
#endif /* HPUX */
			break;

		case PRINT_QNX:
			lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P%p");
			lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P%p %j");
			lpcfg_string_set(ctx, &pService->print_command, "lp -r -P%p %s");
			break;

#if defined(DEVELOPER) || defined(ENABLE_SELFTEST)

	case PRINT_TEST:
	case PRINT_VLP: {
		const char *tdbfile;
		TALLOC_CTX *tmp_ctx = talloc_new(ctx);
		const char *tmp;

		tmp = lpcfg_parm_string(lp_ctx, NULL, "vlp", "tdbfile");
		if (tmp == NULL) {
			tmp = "/tmp/vlp.tdb";
		}

		tdbfile = talloc_asprintf(tmp_ctx, "tdbfile=%s", tmp);
		if (tdbfile == NULL) {
			tdbfile="tdbfile=/tmp/vlp.tdb";
		}

		tmp = talloc_asprintf(tmp_ctx, "vlp %s print %%p %%s",
				      tdbfile);
		lpcfg_string_set(ctx, &pService->print_command,
			   tmp ? tmp : "vlp print %p %s");

		tmp = talloc_asprintf(tmp_ctx, "vlp %s lpq %%p",
				      tdbfile);
		lpcfg_string_set(ctx, &pService->lpq_command,
			   tmp ? tmp : "vlp lpq %p");

		tmp = talloc_asprintf(tmp_ctx, "vlp %s lprm %%p %%j",
				      tdbfile);
		lpcfg_string_set(ctx, &pService->lprm_command,
			   tmp ? tmp : "vlp lprm %p %j");

		tmp = talloc_asprintf(tmp_ctx, "vlp %s lppause %%p %%j",
				      tdbfile);
		lpcfg_string_set(ctx, &pService->lppause_command,
			   tmp ? tmp : "vlp lppause %p %j");

		tmp = talloc_asprintf(tmp_ctx, "vlp %s lpresume %%p %%j",
				      tdbfile);
		lpcfg_string_set(ctx, &pService->lpresume_command,
			   tmp ? tmp : "vlp lpresume %p %j");

		tmp = talloc_asprintf(tmp_ctx, "vlp %s queuepause %%p",
				      tdbfile);
		lpcfg_string_set(ctx, &pService->queuepause_command,
			   tmp ? tmp : "vlp queuepause %p");

		tmp = talloc_asprintf(tmp_ctx, "vlp %s queueresume %%p",
				      tdbfile);
		lpcfg_string_set(ctx, &pService->queueresume_command,
			   tmp ? tmp : "vlp queueresume %p");
		TALLOC_FREE(tmp_ctx);

		break;
	}
#endif /* DEVELOPER */

	}
}

/**
 * Unload unused services.
 */

void lpcfg_killunused(struct loadparm_context *lp_ctx,
		   struct smbsrv_connection *smb,
		   bool (*snumused) (struct smbsrv_connection *, int))
{
	int i;

	if (lp_ctx->s3_fns != NULL) {
		smb_panic("Cannot be used from an s3 loadparm ctx");
	}

	for (i = 0; i < lp_ctx->iNumServices; i++) {
		if (lp_ctx->services[i] == NULL)
			continue;

		if (!snumused || !snumused(smb, i)) {
			talloc_free(lp_ctx->services[i]);
			lp_ctx->services[i] = NULL;
		}
	}
}


static int lpcfg_destructor(struct loadparm_context *lp_ctx)
{
	struct parmlist_entry *data;

	if (lp_ctx->refuse_free) {
		/* someone is trying to free the
		   global_loadparm_context.
		   We can't allow that. */
		return -1;
	}

	if (lp_ctx->globals->param_opt != NULL) {
		struct parmlist_entry *next;
		for (data = lp_ctx->globals->param_opt; data; data=next) {
			next = data->next;
			if (data->priority & FLAG_CMDLINE) continue;
			DLIST_REMOVE(lp_ctx->globals->param_opt, data);
			talloc_free(data);
		}
	}

	return 0;
}

struct defaults_hook_data {
	const char *name;
	lpcfg_defaults_hook hook;
	struct defaults_hook_data *prev, *next;
} *defaults_hooks = NULL;


bool lpcfg_register_defaults_hook(const char *name, lpcfg_defaults_hook hook)
{
	struct defaults_hook_data *hook_data = talloc(talloc_autofree_context(),
												  struct defaults_hook_data);
	hook_data->name = talloc_strdup(hook_data, name);
	hook_data->hook = hook;
	DLIST_ADD(defaults_hooks, hook_data);
	return false;
}

/**
 * Initialise the global parameter structure.
 *
 * Note that most callers should use loadparm_init_global() instead
 */
struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
{
	int i;
	char *myname;
	struct loadparm_context *lp_ctx;
	struct parmlist_entry *parm;
	char *logfile;
	struct defaults_hook_data *defaults_hook;

	lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
	if (lp_ctx == NULL)
		return NULL;

	talloc_set_destructor(lp_ctx, lpcfg_destructor);
	lp_ctx->bInGlobalSection = true;
	lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
	/* This appears odd, but globals in s3 isn't a pointer */
	lp_ctx->globals->ctx = lp_ctx->globals;
	lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
	lp_ctx->flags = talloc_zero_array(lp_ctx, unsigned int, num_parameters());

	lp_ctx->sDefault->max_print_jobs = 1000;
	lp_ctx->sDefault->available = true;
	lp_ctx->sDefault->browseable = true;
	lp_ctx->sDefault->read_only = true;
	lp_ctx->sDefault->map_archive = true;
	lp_ctx->sDefault->strict_locking = true;
	lp_ctx->sDefault->oplocks = true;
	lp_ctx->sDefault->create_mask = 0744;
	lp_ctx->sDefault->force_create_mode = 0000;
	lp_ctx->sDefault->directory_mask = 0755;
	lp_ctx->sDefault->force_directory_mode = 0000;

	DEBUG(3, ("Initialising global parameters\n"));

	for (i = 0; parm_table[i].label; i++) {
		if ((parm_table[i].type == P_STRING ||
		     parm_table[i].type == P_USTRING) &&
		    !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
			TALLOC_CTX *parent_mem;
			char **r;
			if (parm_table[i].p_class == P_LOCAL) {
				parent_mem = lp_ctx->sDefault;
				r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
			} else {
				parent_mem = lp_ctx->globals;
				r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
			}
			lpcfg_string_set(parent_mem, r, "");
		}
	}

	logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
	lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
	talloc_free(logfile);

	lpcfg_do_global_parameter(lp_ctx, "log level", "0");

	lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
	lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
	lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
	lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
	lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
	lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
	lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
	lpcfg_do_global_parameter(lp_ctx, "debug class", "No");

	lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");

	lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
	lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
	lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");

	/* options that can be set on the command line must be initialised via
	   the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
#ifdef TCP_NODELAY
	lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
#endif
	lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
	myname = get_myname(lp_ctx);
	lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
	talloc_free(myname);
	lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");

	lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");

	lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
	lpcfg_do_global_parameter(lp_ctx, "max connections", "0");

	lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
	lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns");
	lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "true");
	/* the winbind method for domain controllers is for both RODC
	   auth forwarding and for trusted domains */
	lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
	lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");

	/* This hive should be dynamically generated by Samba using
	   data from the sam, but for the moment leave it in a tdb to
	   keep regedt32 from popping up an annoying dialog. */
	lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");

	/* using UTF8 by default allows us to support all chars */
	lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");

	/* Use codepage 850 as a default for the dos character set */
	lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");

	/*
	 * Allow the default PASSWD_CHAT to be overridden in local.h.
	 */
	lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);

	lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
	lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
	lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
	lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
	lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);

	lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
	lpcfg_do_global_parameter_var(lp_ctx, "server string",
				   "Samba %s", SAMBA_VERSION_STRING);

	lpcfg_do_global_parameter(lp_ctx, "password server", "*");

	lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
	lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
	lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");

	lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
	lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
	lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
	lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
	lpcfg_do_global_parameter(lp_ctx, "client max protocol", "default");
	lpcfg_do_global_parameter(lp_ctx, "client ipc min protocol", "default");
	lpcfg_do_global_parameter(lp_ctx, "client ipc max protocol", "default");
	lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
	lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
	lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
	lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
	lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
	lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
	lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");

	lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
	lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
	lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
	lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
	lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
	lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
	lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "False");
	lpcfg_do_global_parameter(lp_ctx, "RawNTLMv2Auth", "False");
	lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");

	lpcfg_do_global_parameter(lp_ctx, "allow dcerpc auth level connect", "False");

	lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");

	lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
	lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");

	lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
	lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");

	lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
	lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
	lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
	lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
	lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
	lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
	lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
	lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
	lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
					"%s/samba_kcc", dyn_SCRIPTSBINDIR);
	lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
	lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%D/%U");

	lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
	lpcfg_do_global_parameter(lp_ctx, "client ipc signing", "default");
	lpcfg_do_global_parameter(lp_ctx, "server signing", "default");

	lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");

	lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");

	lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
	lpcfg_do_global_parameter_var(lp_ctx, "nbt port", "%d", NBT_NAME_SERVICE_PORT);
	lpcfg_do_global_parameter_var(lp_ctx, "dgram port", "%d", NBT_DGRAM_SERVICE_PORT);
	lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
	lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
	lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
	lpcfg_do_global_parameter(lp_ctx, "web port", "901");

	lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");

	lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
	lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");

	lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
	lpcfg_do_global_parameter(lp_ctx, "tls verify peer", "as_strict_as_possible");
	lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
	lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
	lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
	lpcfg_do_global_parameter(lp_ctx, "tls priority", "NORMAL:-VERS-SSL3.0");
	lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");

	lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
	lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");

        lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
        lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");

	lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");

	lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");

	lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");

	lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");

	lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");

	lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");

	lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");

	lpcfg_do_global_parameter(lp_ctx, "locking", "True");

	lpcfg_do_global_parameter(lp_ctx, "block size", "1024");

	lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");

	lpcfg_do_global_parameter(lp_ctx, "change notify", "True");

	lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");

	lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");

	lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");

	lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");

	lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");

	lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);

	lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");

	lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");

	lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");

	lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");

	lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);

	lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");

	lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");

	lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");

	lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");

	lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");

	lpcfg_do_global_parameter(lp_ctx, "browse list", "True");

	lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");

	lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);

	lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");

	lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");

	lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");

	lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");

	lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");

	lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");

	lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");

	lpcfg_do_global_parameter(lp_ctx, "winbind request timeout", "60");

	lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");

	lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");

	lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");

	lpcfg_do_global_parameter(lp_ctx, "smbd profiling level", "off");

	lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");

	lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");

	lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");

	lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");

	lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1000");

	lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");

	lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");

	lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");

	lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");

	lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");

	lpcfg_do_global_parameter(lp_ctx, "os level", "20");

	lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");

	lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");

	lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");

	lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");

	lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");

	lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");

	lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");

	lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");

	lpcfg_do_global_parameter(lp_ctx, "client ldap sasl wrapping", "sign");

	lpcfg_do_global_parameter(lp_ctx, "ldap server require strong auth", "yes");

	lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");

	lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");

	lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");

	lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "0");

	lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");

	lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");

	lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);

	lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);

	lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");

	lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");

	lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");

	lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");

	lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");

	lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");

	lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");

	lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");

	lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");

	lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");

	lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");

	lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");

	lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");

	lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");

	lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");

	lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");

	lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");

	lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");

	lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");

	lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");

	lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");

	lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");

	lpcfg_do_global_parameter(lp_ctx, "usershare max shares", "100");

	lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");

	lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");

	lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());

#ifdef DEVELOPER
	lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
#endif

	lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());

	lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");

	lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");

	lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");

	lpcfg_do_global_parameter(lp_ctx, "aio max threads", "100");

	lpcfg_do_global_parameter(lp_ctx, "smb2 leases", "yes");

	/* Allow modules to adjust defaults */
	for (defaults_hook = defaults_hooks; defaults_hook;
		 defaults_hook = defaults_hook->next) {
		bool ret;

		ret = defaults_hook->hook(lp_ctx);
		if (!ret) {
			DEBUG(1, ("Defaults hook %s failed to run.",
					  defaults_hook->name));
			talloc_free(lp_ctx);
			return NULL;
		}
	}

	for (i = 0; parm_table[i].label; i++) {
		if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
			lp_ctx->flags[i] |= FLAG_DEFAULT;
		}
	}

	for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
		if (!(parm->priority & FLAG_CMDLINE)) {
			parm->priority |= FLAG_DEFAULT;
		}
	}

	for (parm=lp_ctx->sDefault->param_opt; parm; parm=parm->next) {
		if (!(parm->priority & FLAG_CMDLINE)) {
			parm->priority |= FLAG_DEFAULT;
		}
	}

	return lp_ctx;
}

/**
 * Initialise the global parameter structure.
 */
struct loadparm_context *loadparm_init_global(bool load_default)
{
	if (global_loadparm_context == NULL) {
		global_loadparm_context = loadparm_init(NULL);
	}
	if (global_loadparm_context == NULL) {
		return NULL;
	}
	global_loadparm_context->global = true;
	if (load_default && !global_loadparm_context->loaded) {
		lpcfg_load_default(global_loadparm_context);
	}
	global_loadparm_context->refuse_free = true;
	return global_loadparm_context;
}

/**
 * Initialise the global parameter structure.
 */
struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx, 
					  const struct loadparm_s3_helpers *s3_fns)
{
	struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
	if (!loadparm_context) {
		return NULL;
	}
	loadparm_context->s3_fns = s3_fns;
	loadparm_context->globals = s3_fns->globals;
	loadparm_context->flags = s3_fns->flags;

	return loadparm_context;
}

const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
{
	return lp_ctx->szConfigFile;
}

const char *lp_default_path(void)
{
    if (getenv("SMB_CONF_PATH"))
        return getenv("SMB_CONF_PATH");
    else
        return dyn_CONFIGFILE;
}

/**
 * Update the internal state of a loadparm context after settings 
 * have changed.
 */
static bool lpcfg_update(struct loadparm_context *lp_ctx)
{
	struct debug_settings settings;
	TALLOC_CTX *tmp_ctx;

	tmp_ctx = talloc_new(lp_ctx);
	if (tmp_ctx == NULL) {
		return false;
	}

	lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));

	if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
		lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
	}

	if (!lp_ctx->global) {
		TALLOC_FREE(tmp_ctx);
		return true;
	}

	panic_action = lp_ctx->globals->panic_action;

	reload_charcnv(lp_ctx);

	ZERO_STRUCT(settings);
	/* Add any more debug-related smb.conf parameters created in
	 * future here */
	settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
	settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
	settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
	settings.debug_pid = lp_ctx->globals->debug_pid;
	settings.debug_uid = lp_ctx->globals->debug_uid;
	settings.debug_class = lp_ctx->globals->debug_class;
	debug_set_settings(&settings, lp_ctx->globals->logging,
			   lp_ctx->globals->syslog,
			   lp_ctx->globals->syslog_only);

	/* FIXME: This is a bit of a hack, but we can't use a global, since 
	 * not everything that uses lp also uses the socket library */
	if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
		setenv("SOCKET_TESTNONBLOCK", "1", 1);
	} else {
		unsetenv("SOCKET_TESTNONBLOCK");
	}

	TALLOC_FREE(tmp_ctx);
	return true;
}

bool lpcfg_load_default(struct loadparm_context *lp_ctx)
{
    const char *path;

    path = lp_default_path();

    if (!file_exist(path)) {
	    /* We allow the default smb.conf file to not exist, 
	     * basically the equivalent of an empty file. */
	    return lpcfg_update(lp_ctx);
    }

    return lpcfg_load(lp_ctx, path);
}

/**
 * Load the services array from the services file.
 *
 * Return True on success, False on failure.
 */
bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
{
	char *n2;
	bool bRetval;

	filename = talloc_strdup(lp_ctx, filename);

	lp_ctx->szConfigFile = filename;

	if (lp_ctx->s3_fns) {
		return lp_ctx->s3_fns->load(filename);
	}

	lp_ctx->bInGlobalSection = true;
	n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
	DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));

	add_to_file_list(lp_ctx, &lp_ctx->file_lists, lp_ctx->szConfigFile, n2);

	/* We get sections first, so have to start 'behind' to make up */
	lp_ctx->currentService = NULL;
	bRetval = pm_process(n2, do_section, lpcfg_do_parameter, lp_ctx);

	/* finish up the last section */
	DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
	if (bRetval)
		if (lp_ctx->currentService != NULL)
			bRetval = lpcfg_service_ok(lp_ctx->currentService);

	bRetval = bRetval && lpcfg_update(lp_ctx);

	/* we do this unconditionally, so that it happens even
	   for a missing smb.conf */
	reload_charcnv(lp_ctx);

	if (bRetval == true) {
		/* set this up so that any child python tasks will
		   find the right smb.conf */
		setenv("SMB_CONF_PATH", filename, 1);

		/* set the context used by the lp_*() function
		   varients */
		global_loadparm_context = lp_ctx;
		lp_ctx->loaded = true;
	}

	return bRetval;
}

/**
 * Return the max number of services.
 */

int lpcfg_numservices(struct loadparm_context *lp_ctx)
{
	if (lp_ctx->s3_fns) {
		return lp_ctx->s3_fns->get_numservices();
	}

	return lp_ctx->iNumServices;
}

/**
 * Display the contents of the services array in human-readable form.
 */

void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
	     int maxtoprint)
{
	int iService;

	if (lp_ctx->s3_fns) {
		lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
		return;
	}

	lpcfg_dump_globals(lp_ctx, f, show_defaults);

	lpcfg_dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags, show_defaults);

	for (iService = 0; iService < maxtoprint; iService++)
		lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
}

/**
 * Display the contents of one service in human-readable form.
 */
void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
{
	if (service != NULL) {
		if (service->szService[0] == '\0')
			return;
		lpcfg_dump_a_service(service, sDefault, f, NULL, show_defaults);
	}
}

struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
					    int snum)
{
	if (lp_ctx->s3_fns) {
		return lp_ctx->s3_fns->get_servicebynum(snum);
	}

	return lp_ctx->services[snum];
}

struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
				    const char *service_name)
{
	int iService;
        char *serviceName;

	if (lp_ctx->s3_fns) {
		return lp_ctx->s3_fns->get_service(service_name);
	}

	for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
		if (lp_ctx->services[iService] &&
		    lp_ctx->services[iService]->szService) {
			/*
			 * The substitution here is used to support %U is
			 * service names
			 */
			serviceName = standard_sub_basic(
					lp_ctx->services[iService],
					lp_ctx->services[iService]->szService);
			if (strequal(serviceName, service_name)) {
				talloc_free(serviceName);
				return lp_ctx->services[iService];
			}
			talloc_free(serviceName);
		}
	}

	DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
	return NULL;
}

const char *lpcfg_servicename(const struct loadparm_service *service)
{
	return lpcfg_string((const char *)service->szService);
}

/**
 * A useful volume label function.
 */
const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
{
	const char *ret;
	ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
				       service->volume : sDefault->volume));
	if (!*ret)
		return lpcfg_servicename(service);
	return ret;
}

/**
 * Return the correct printer name.
 */
const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
{
	const char *ret;
	ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
				       service->_printername : sDefault->_printername));
	if (ret == NULL || (ret != NULL && *ret == '\0'))
		ret = lpcfg_servicename(service);

	return ret;
}


/**
 * Return the max print jobs per queue.
 */
int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
{
	int maxjobs = lpcfg_max_print_jobs(service, sDefault);

	if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
		maxjobs = PRINT_MAX_JOBID - 1;

	return maxjobs;
}

struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
{
	if (lp_ctx == NULL) {
		return get_iconv_handle();
	}
	return lp_ctx->iconv_handle;
}

_PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
{
	struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
	if (!lp_ctx->global) {
		return;
	}

	if (old_ic == NULL) {
		old_ic = global_iconv_handle;
	}
	lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
	global_iconv_handle = lp_ctx->iconv_handle;
}

_PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
{
	return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
}

_PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
{
	return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
}

_PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
{
	return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
}

_PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
{
	return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
}

_PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
{
	return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
}

struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
{
	struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
	if (settings == NULL)
		return NULL;
	SMB_ASSERT(lp_ctx != NULL);
	settings->lp_ctx = talloc_reference(settings, lp_ctx);
	settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
	return settings;
}

int lpcfg_server_role(struct loadparm_context *lp_ctx)
{
	int domain_master = lpcfg__domain_master(lp_ctx);

	return lp_find_server_role(lpcfg__server_role(lp_ctx),
				   lpcfg__security(lp_ctx),
				   lpcfg__domain_logons(lp_ctx),
				   (domain_master == true) ||
				   (domain_master == Auto));
}

int lpcfg_security(struct loadparm_context *lp_ctx)
{
	return lp_find_security(lpcfg__server_role(lp_ctx),
				lpcfg__security(lp_ctx));
}

int lpcfg_client_max_protocol(struct loadparm_context *lp_ctx)
{
	int client_max_protocol = lpcfg__client_max_protocol(lp_ctx);
	if (client_max_protocol == PROTOCOL_DEFAULT) {
		return PROTOCOL_NT1;
	}
	return client_max_protocol;
}

int lpcfg_client_ipc_min_protocol(struct loadparm_context *lp_ctx)
{
	int client_ipc_min_protocol = lpcfg__client_ipc_min_protocol(lp_ctx);
	if (client_ipc_min_protocol == PROTOCOL_DEFAULT) {
		client_ipc_min_protocol = lpcfg_client_min_protocol(lp_ctx);
	}
	if (client_ipc_min_protocol < PROTOCOL_NT1) {
		return PROTOCOL_NT1;
	}
	return client_ipc_min_protocol;
}

int lpcfg_client_ipc_max_protocol(struct loadparm_context *lp_ctx)
{
	int client_ipc_max_protocol = lpcfg__client_ipc_max_protocol(lp_ctx);
	if (client_ipc_max_protocol == PROTOCOL_DEFAULT) {
		return PROTOCOL_LATEST;
	}
	if (client_ipc_max_protocol < PROTOCOL_NT1) {
		return PROTOCOL_NT1;
	}
	return client_ipc_max_protocol;
}

int lpcfg_client_ipc_signing(struct loadparm_context *lp_ctx)
{
	int client_ipc_signing = lpcfg__client_ipc_signing(lp_ctx);
	if (client_ipc_signing == SMB_SIGNING_DEFAULT) {
		return SMB_SIGNING_REQUIRED;
	}
	return client_ipc_signing;
}

bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
{
	bool allowed = true;
	enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);

	*mandatory = false;

	if (signing_setting == SMB_SIGNING_DEFAULT) {
		/*
		 * If we are a domain controller, SMB signing is
		 * really important, as it can prevent a number of
		 * attacks on communications between us and the
		 * clients
		 *
		 * However, it really sucks (no sendfile, CPU
		 * overhead) performance-wise when used on a
		 * file server, so disable it by default
		 * on non-DCs
		 */

		if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
			signing_setting = SMB_SIGNING_REQUIRED;
		} else {
			signing_setting = SMB_SIGNING_OFF;
		}
	}

	switch (signing_setting) {
	case SMB_SIGNING_REQUIRED:
		*mandatory = true;
		break;
	case SMB_SIGNING_DESIRED:
	case SMB_SIGNING_IF_REQUIRED:
		break;
	case SMB_SIGNING_OFF:
		allowed = false;
		break;
	case SMB_SIGNING_DEFAULT:
	case SMB_SIGNING_IPC_DEFAULT:
		smb_panic(__location__);
		break;
	}

	return allowed;
}

int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
{
	const char *base;

	if (name == NULL) {
		return 0;
	}

	base = strrchr_m(name, '/');
	if (base != NULL) {
		base += 1;
	} else {
		base = name;
	}
	return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);

}

int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
{
	if (!lpcfg_use_mmap(lp_ctx)) {
		tdb_flags |= TDB_NOMMAP;
	}
	return tdb_flags;
}