summaryrefslogtreecommitdiffstats
path: root/source/smbd/server.c
blob: a35c3a2fdc4e62707bcbd16b9f069fb6cde62f12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   Main SMB server routines
   Copyright (C) Andrew Tridgell 1992-1998
   
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "trans2.h"

pstring servicesf = CONFIGFILE;
extern pstring debugf;
extern pstring sesssetup_user;
extern fstring global_myworkgroup;
extern pstring global_myname;

char *InBuffer = NULL;
char *OutBuffer = NULL;
char *last_inbuf = NULL;

int am_parent = 1;
int atexit_set = 0;

/* the last message the was processed */
int last_message = -1;

/* a useful macro to debug the last message processed */
#define LAST_MESSAGE() smb_fn_name(last_message)

extern pstring scope;
extern int DEBUGLEVEL;
extern int case_default;
extern BOOL case_sensitive;
extern BOOL case_preserve;
extern BOOL use_mangled_map;
extern BOOL short_case_preserve;
extern BOOL case_mangle;
time_t smb_last_time=(time_t)0;

extern int smb_read_error;

extern pstring user_socket_options;

#ifdef DFS_AUTH
extern int dcelogin_atmost_once;
#endif /* DFS_AUTH */

/*
 * This is set on startup - it defines the SID for this
 * machine.
 */
extern DOM_SID global_machine_sid;

connection_struct Connections[MAX_CONNECTIONS];
files_struct Files[MAX_OPEN_FILES];

/*
 * Indirection for file fd's. Needed as POSIX locking
 * is based on file/process, not fd/process.
 */
file_fd_struct FileFd[MAX_OPEN_FILES];
int max_file_fd_used = 0;

extern int Protocol;

/* 
 * Size of data we can send to client. Set
 *  by the client for all protocols above CORE.
 *  Set by us for CORE protocol.
 */
int max_send = BUFFER_SIZE;
/*
 * Size of the data we can receive. Set by us.
 * Can be modified by the max xmit parameter.
 */
int max_recv = BUFFER_SIZE;

/* a fnum to use when chaining */
int chain_fnum = -1;

/* number of open connections */
static int num_connections_open = 0;

/* Oplock ipc UDP socket. */
int oplock_sock = -1;
uint16 oplock_port = 0;
/* Current number of oplocks we have outstanding. */
int32 global_oplocks_open = 0;

BOOL global_oplock_break = False;

extern fstring remote_machine;

extern pstring OriginalDir;

/* these can be set by some functions to override the error codes */
int unix_ERR_class=SMB_SUCCESS;
int unix_ERR_code=0;


extern int extra_time_offset;

extern pstring myhostname;

static int find_free_connection(int hash);

/* for readability... */
#define IS_DOS_READONLY(test_mode) (((test_mode) & aRONLY) != 0)
#define IS_DOS_DIR(test_mode) (((test_mode) & aDIR) != 0)
#define IS_DOS_ARCHIVE(test_mode) (((test_mode) & aARCH) != 0)
#define IS_DOS_SYSTEM(test_mode) (((test_mode) & aSYSTEM) != 0)
#define IS_DOS_HIDDEN(test_mode) (((test_mode) & aHIDDEN) != 0)

/****************************************************************************
  when exiting, take the whole family
****************************************************************************/
void  *dflt_sig(void)
{
  exit_server("caught signal");
  return 0; /* Keep -Wall happy :-) */
}
/****************************************************************************
  Send a SIGTERM to our process group.
*****************************************************************************/
void  killkids(void)
{
  if(am_parent) kill(0,SIGTERM);
}

/****************************************************************************
  change a dos mode to a unix mode
    base permission for files:
         everybody gets read bit set
         dos readonly is represented in unix by removing everyone's write bit
         dos archive is represented in unix by the user's execute bit
         dos system is represented in unix by the group's execute bit
         dos hidden is represented in unix by the other's execute bit
         Then apply create mask,
         then add force bits.
    base permission for directories:
         dos directory is represented in unix by unix's dir bit and the exec bit
         Then apply create mask,
         then add force bits.
****************************************************************************/
mode_t unix_mode(int cnum,int dosmode)
{
  mode_t result = (S_IRUSR | S_IRGRP | S_IROTH);

  if ( !IS_DOS_READONLY(dosmode) )
    result |= (S_IWUSR | S_IWGRP | S_IWOTH);
 
  if (IS_DOS_DIR(dosmode)) {
    /* We never make directories read only for the owner as under DOS a user
       can always create a file in a read-only directory. */
    result |= (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH | S_IWUSR);
    /* Apply directory mask */
    result &= lp_dir_mode(SNUM(cnum));
    /* Add in force bits */
    result |= lp_force_dir_mode(SNUM(cnum));
  } else { 
    if (MAP_ARCHIVE(cnum) && IS_DOS_ARCHIVE(dosmode))
      result |= S_IXUSR;

    if (MAP_SYSTEM(cnum) && IS_DOS_SYSTEM(dosmode))
      result |= S_IXGRP;
 
    if (MAP_HIDDEN(cnum) && IS_DOS_HIDDEN(dosmode))
      result |= S_IXOTH;  
 
    /* Apply mode mask */
    result &= lp_create_mode(SNUM(cnum));
    /* Add in force bits */
    result |= lp_force_create_mode(SNUM(cnum));
  }
  return(result);
}


/****************************************************************************
  change a unix mode to a dos mode
****************************************************************************/
int dos_mode(int cnum,char *path,struct stat *sbuf)
{
  int result = 0;
  extern struct current_user current_user;

  DEBUG(8,("dos_mode: %d %s\n", cnum, path));

  if (CAN_WRITE(cnum) && !lp_alternate_permissions(SNUM(cnum))) {
    if (!((sbuf->st_mode & S_IWOTH) ||
	  Connections[cnum].admin_user ||
	  ((sbuf->st_mode & S_IWUSR) && current_user.uid==sbuf->st_uid) ||
	  ((sbuf->st_mode & S_IWGRP) && 
	   in_group(sbuf->st_gid,current_user.gid,
		    current_user.ngroups,current_user.igroups))))
      result |= aRONLY;
  } else {
    if ((sbuf->st_mode & S_IWUSR) == 0)
      result |= aRONLY;
  }

  if (MAP_ARCHIVE(cnum) && ((sbuf->st_mode & S_IXUSR) != 0))
    result |= aARCH;

  if (MAP_SYSTEM(cnum) && ((sbuf->st_mode & S_IXGRP) != 0))
    result |= aSYSTEM;

  if (MAP_HIDDEN(cnum) && ((sbuf->st_mode & S_IXOTH) != 0))
    result |= aHIDDEN;   
  
  if (S_ISDIR(sbuf->st_mode))
    result = aDIR | (result & aRONLY);

#ifdef S_ISLNK
#if LINKS_READ_ONLY
  if (S_ISLNK(sbuf->st_mode) && S_ISDIR(sbuf->st_mode))
    result |= aRONLY;
#endif
#endif

  /* hide files with a name starting with a . */
  if (lp_hide_dot_files(SNUM(cnum)))
    {
      char *p = strrchr(path,'/');
      if (p)
	p++;
      else
	p = path;
      
      if (p[0] == '.' && p[1] != '.' && p[1] != 0)
	result |= aHIDDEN;
    }

  /* Optimization : Only call is_hidden_path if it's not already
     hidden. */
  if (!(result & aHIDDEN) && IS_HIDDEN_PATH(cnum,path))
  {
    result |= aHIDDEN;
  }

  DEBUG(8,("dos_mode returning "));

  if (result & aHIDDEN) DEBUG(8, ("h"));
  if (result & aRONLY ) DEBUG(8, ("r"));
  if (result & aSYSTEM) DEBUG(8, ("s"));
  if (result & aDIR   ) DEBUG(8, ("d"));
  if (result & aARCH  ) DEBUG(8, ("a"));

  DEBUG(8,("\n"));

  return(result);
}

/*******************************************************************
chmod a file - but preserve some bits
********************************************************************/
int dos_chmod(int cnum,char *fname,int dosmode,struct stat *st)
{
  struct stat st1;
  int mask=0;
  int tmp;
  int unixmode;

  if (!st) {
    st = &st1;
    if (sys_stat(fname,st)) return(-1);
  }

  if (S_ISDIR(st->st_mode)) dosmode |= aDIR;

  if (dos_mode(cnum,fname,st) == dosmode) return(0);

  unixmode = unix_mode(cnum,dosmode);

  /* preserve the s bits */
  mask |= (S_ISUID | S_ISGID);

  /* preserve the t bit */
#ifdef S_ISVTX
  mask |= S_ISVTX;
#endif

  /* possibly preserve the x bits */
  if (!MAP_ARCHIVE(cnum)) mask |= S_IXUSR;
  if (!MAP_SYSTEM(cnum)) mask |= S_IXGRP;
  if (!MAP_HIDDEN(cnum)) mask |= S_IXOTH;

  unixmode |= (st->st_mode & mask);

  /* if we previously had any r bits set then leave them alone */
  if ((tmp = st->st_mode & (S_IRUSR|S_IRGRP|S_IROTH))) {
    unixmode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
    unixmode |= tmp;
  }

  /* if we previously had any w bits set then leave them alone 
   if the new mode is not rdonly */
  if (!IS_DOS_READONLY(dosmode) &&
      (tmp = st->st_mode & (S_IWUSR|S_IWGRP|S_IWOTH))) {
    unixmode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
    unixmode |= tmp;
  }

  return(sys_chmod(fname,unixmode));
}

/*******************************************************************
Wrapper around sys_utime that possibly allows DOS semantics rather
than POSIX.
*******************************************************************/

int file_utime(int cnum, char *fname, struct utimbuf *times)
{
  extern struct current_user current_user;
  struct stat sb;
  int ret = -1;

  errno = 0;

  if(sys_utime(fname, times) == 0)
    return 0;

  if((errno != EPERM) && (errno != EACCES))
    return -1;

  if(!lp_dos_filetimes(SNUM(cnum)))
    return -1;

  /* We have permission (given by the Samba admin) to
     break POSIX semantics and allow a user to change
     the time on a file they don't own but can write to
     (as DOS does).
   */

  if(sys_stat(fname,&sb) != 0)
    return -1;

  /* Check if we have write access. */
  if (CAN_WRITE(cnum)) {
	  if (((sb.st_mode & S_IWOTH) ||
	       Connections[cnum].admin_user ||
	       ((sb.st_mode & S_IWUSR) && current_user.uid==sb.st_uid) ||
	       ((sb.st_mode & S_IWGRP) &&
		in_group(sb.st_gid,current_user.gid,
			 current_user.ngroups,current_user.igroups)))) {
		  /* We are allowed to become root and change the filetime. */
		  become_root(False);
		  ret = sys_utime(fname, times);
		  unbecome_root(False);
	  }
  }

  return ret;
}
  
/*******************************************************************
Change a filetime - possibly allowing DOS semantics.
*******************************************************************/

BOOL set_filetime(int cnum, char *fname, time_t mtime)
{
  struct utimbuf times;

  if (null_mtime(mtime)) return(True);

  times.modtime = times.actime = mtime;

  if (file_utime(cnum, fname, &times)) {
    DEBUG(4,("set_filetime(%s) failed: %s\n",fname,strerror(errno)));
  }
  
  return(True);
} 

/****************************************************************************
check if two filenames are equal

this needs to be careful about whether we are case sensitive
****************************************************************************/
static BOOL fname_equal(char *name1, char *name2)
{
  int l1 = strlen(name1);
  int l2 = strlen(name2);

  /* handle filenames ending in a single dot */
  if (l1-l2 == 1 && name1[l1-1] == '.' && lp_strip_dot())
    {
      BOOL ret;
      name1[l1-1] = 0;
      ret = fname_equal(name1,name2);
      name1[l1-1] = '.';
      return(ret);
    }

  if (l2-l1 == 1 && name2[l2-1] == '.' && lp_strip_dot())
    {
      BOOL ret;
      name2[l2-1] = 0;
      ret = fname_equal(name1,name2);
      name2[l2-1] = '.';
      return(ret);
    }

  /* now normal filename handling */
  if (case_sensitive)
    return(strcmp(name1,name2) == 0);

  return(strequal(name1,name2));
}


/****************************************************************************
mangle the 2nd name and check if it is then equal to the first name
****************************************************************************/
static BOOL mangled_equal(char *name1, char *name2)
{
  pstring tmpname;

  if (is_8_3(name2, True))
    return(False);

  pstrcpy(tmpname,name2);
  mangle_name_83(tmpname,sizeof(tmpname));

  return(strequal(name1,tmpname));
}


/****************************************************************************
scan a directory to find a filename, matching without case sensitivity

If the name looks like a mangled name then try via the mangling functions
****************************************************************************/
static BOOL scan_directory(char *path, char *name,int cnum,BOOL docache)
{
  void *cur_dir;
  char *dname;
  BOOL mangled;
  pstring name2;

  mangled = is_mangled(name);

  /* handle null paths */
  if (*path == 0)
    path = ".";

  if (docache && (dname = DirCacheCheck(path,name,SNUM(cnum)))) {
    pstrcpy(name, dname);	
    return(True);
  }      

  /*
   * The incoming name can be mangled, and if we de-mangle it
   * here it will not compare correctly against the filename (name2)
   * read from the directory and then mangled by the name_map_mangle()
   * call. We need to mangle both names or neither.
   * (JRA).
   */
  if (mangled)
    mangled = !check_mangled_cache( name );

  /* open the directory */
  if (!(cur_dir = OpenDir(cnum, path, True))) 
    {
      DEBUG(3,("scan dir didn't open dir [%s]\n",path));
      return(False);
    }

  /* now scan for matching names */
  while ((dname = ReadDirName(cur_dir))) 
    {
      if (*dname == '.' &&
	  (strequal(dname,".") || strequal(dname,"..")))
	continue;

      pstrcpy(name2,dname);
      if (!name_map_mangle(name2,False,SNUM(cnum))) continue;

      if ((mangled && mangled_equal(name,name2))
	  || fname_equal(name, name2))
	{
	  /* we've found the file, change it's name and return */
	  if (docache) DirCacheAdd(path,name,dname,SNUM(cnum));
	  pstrcpy(name, dname);
	  CloseDir(cur_dir);
	  return(True);
	}
    }

  CloseDir(cur_dir);
  return(False);
}

/****************************************************************************
This routine is called to convert names from the dos namespace to unix
namespace. It needs to handle any case conversions, mangling, format
changes etc.

We assume that we have already done a chdir() to the right "root" directory
for this service.

The function will return False if some part of the name except for the last
part cannot be resolved

If the saved_last_component != 0, then the unmodified last component
of the pathname is returned there. This is used in an exceptional
case in reply_mv (so far). If saved_last_component == 0 then nothing
is returned there.

The bad_path arg is set to True if the filename walk failed. This is
used to pick the correct error code to return between ENOENT and ENOTDIR
as Windows applications depend on ERRbadpath being returned if a component
of a pathname does not exist.
****************************************************************************/
BOOL unix_convert(char *name,int cnum,pstring saved_last_component, BOOL *bad_path)
{
  struct stat st;
  char *start, *end;
  pstring dirpath;
  int saved_errno;

  *dirpath = 0;
  *bad_path = False;

  if(saved_last_component)
    *saved_last_component = 0;

  /* convert to basic unix format - removing \ chars and cleaning it up */
  unix_format(name);
  unix_clean_name(name);

  /* names must be relative to the root of the service - trim any leading /.
   also trim trailing /'s */
  trim_string(name,"/","/");

  /*
   * Ensure saved_last_component is valid even if file exists.
   */
  if(saved_last_component) {
    end = strrchr(name, '/');
    if(end)
      pstrcpy(saved_last_component, end + 1);
    else
      pstrcpy(saved_last_component, name);
  }

  if (!case_sensitive && 
      (!case_preserve || (is_8_3(name, False) && !short_case_preserve)))
    strnorm(name);

  /* check if it's a printer file */
  if (Connections[cnum].printer)
    {
      if ((! *name) || strchr(name,'/') || !is_8_3(name, True))
	{
	  char *s;
	  fstring name2;
	  slprintf(name2,sizeof(name2)-1,"%.6s.XXXXXX",remote_machine);
	  /* sanitise the name */
	  for (s=name2 ; *s ; s++)
	    if (!issafe(*s)) *s = '_';
	  pstrcpy(name,(char *)mktemp(name2));	  
	}      
      return(True);
    }

  /* stat the name - if it exists then we are all done! */
  if (sys_stat(name,&st) == 0)
    return(True);

  saved_errno = errno;

  DEBUG(5,("unix_convert(%s,%d)\n",name,cnum));

  /* a special case - if we don't have any mangling chars and are case
     sensitive then searching won't help */
  if (case_sensitive && !is_mangled(name) && 
      !lp_strip_dot() && !use_mangled_map && (saved_errno != ENOENT))
    return(False);

  /* now we need to recursively match the name against the real 
     directory structure */

  start = name;
  while (strncmp(start,"./",2) == 0)
    start += 2;

  /* now match each part of the path name separately, trying the names
     as is first, then trying to scan the directory for matching names */
  for (;start;start = (end?end+1:(char *)NULL)) 
    {
      /* pinpoint the end of this section of the filename */
      end = strchr(start, '/');

      /* chop the name at this point */
      if (end) 	*end = 0;

      if(saved_last_component != 0)
        pstrcpy(saved_last_component, end ? end + 1 : start);

      /* check if the name exists up to this point */
      if (sys_stat(name, &st) == 0) 
	{
	  /* it exists. it must either be a directory or this must be
	     the last part of the path for it to be OK */
	  if (end && !(st.st_mode & S_IFDIR)) 
	    {
	      /* an intermediate part of the name isn't a directory */
	      DEBUG(5,("Not a dir %s\n",start));
	      *end = '/';
	      return(False);
	    }
	}
      else 
	{
	  pstring rest;

	  *rest = 0;

	  /* remember the rest of the pathname so it can be restored
	     later */
	  if (end) pstrcpy(rest,end+1);

	  /* try to find this part of the path in the directory */
	  if (strchr(start,'?') || strchr(start,'*') ||
	      !scan_directory(dirpath, start, cnum, end?True:False))
	    {
	      if (end) 
		{
		  /* an intermediate part of the name can't be found */
		  DEBUG(5,("Intermediate not found %s\n",start));
		  *end = '/';
                  /* We need to return the fact that the intermediate
                     name resolution failed. This is used to return an
                     error of ERRbadpath rather than ERRbadfile. Some
                     Windows applications depend on the difference between
                     these two errors.
                   */
                  *bad_path = True;
		  return(False);
		}
	      
	      /* just the last part of the name doesn't exist */
	      /* we may need to strupper() or strlower() it in case
		 this conversion is being used for file creation 
		 purposes */
	      /* if the filename is of mixed case then don't normalise it */
	      if (!case_preserve && 
		  (!strhasupper(start) || !strhaslower(start)))		
		strnorm(start);

	      /* check on the mangled stack to see if we can recover the 
		 base of the filename */
	      if (is_mangled(start))
		check_mangled_cache( start );

	      DEBUG(5,("New file %s\n",start));
	      return(True); 
	    }

	  /* restore the rest of the string */
	  if (end) 
	    {
	      pstrcpy(start+strlen(start)+1,rest);
	      end = start + strlen(start);
	    }
	}

      /* add to the dirpath that we have resolved so far */
      if (*dirpath) pstrcat(dirpath,"/");
      pstrcat(dirpath,start);

      /* restore the / that we wiped out earlier */
      if (end) *end = '/';
    }
  
  /* the name has been resolved */
  DEBUG(5,("conversion finished %s\n",name));
  return(True);
}


/****************************************************************************
normalise for DOS usage 
****************************************************************************/
static void disk_norm(int *bsize,int *dfree,int *dsize)
{
  /* check if the disk is beyond the max disk size */
  int maxdisksize = lp_maxdisksize();
  if (maxdisksize) {
    /* convert to blocks - and don't overflow */
    maxdisksize = ((maxdisksize*1024)/(*bsize))*1024;
    if (*dsize > maxdisksize) *dsize = maxdisksize;
    if (*dfree > maxdisksize) *dfree = maxdisksize-1; /* the -1 should stop 
							 applications getting 
							 div by 0 errors */
  }  

  while (*dfree > WORDMAX || *dsize > WORDMAX || *bsize < 512) 
    {
      *dfree /= 2;
      *dsize /= 2;
      *bsize *= 2;
      if (*bsize > WORDMAX )
	{
	  *bsize = WORDMAX;
	  if (*dsize > WORDMAX)
	    *dsize = WORDMAX;
	  if (*dfree >  WORDMAX)
	    *dfree = WORDMAX;
	  break;
	}
    }
}

/****************************************************************************
  return number of 1K blocks available on a path and total number 
****************************************************************************/
int disk_free(char *path,int *bsize,int *dfree,int *dsize)
{
  char *df_command = lp_dfree_command();
  int dfree_retval;
#ifdef QUOTAS
  int dfreeq_retval;
  int dfreeq = 0;
  int bsizeq = *bsize;
  int dsizeq = *dsize;
#endif

#ifndef NO_STATFS
#ifdef USE_STATVFS
  struct statvfs fs;
#else
#ifdef ULTRIX
  struct fs_data fs;
#else
  struct statfs fs;
#endif
#endif
#endif

  /* possibly use system() to get the result */
  if (df_command && *df_command)
    {
      int ret;
      pstring syscmd;
      pstring outfile;
	  
      slprintf(outfile,sizeof(outfile)-1, "%s/dfree.smb.%d",tmpdir(),(int)getpid());
      slprintf(syscmd,sizeof(syscmd)-1,"%s %s",df_command,path);
      standard_sub_basic(syscmd);

      ret = smbrun(syscmd,outfile,False);
      DEBUG(3,("Running the command `%s' gave %d\n",syscmd,ret));
	  
      {
        FILE *f = fopen(outfile,"r");	
        *dsize = 0;
        *dfree = 0;
        *bsize = 1024;
        if (f)
          {
            fscanf(f,"%d %d %d",dsize,dfree,bsize);
            fclose(f);
          }
        else
          DEBUG(0,("Can't open %s\n",outfile));
      }
	  
      unlink(outfile);
      disk_norm(bsize,dfree,dsize);
      dfree_retval = ((*bsize)/1024)*(*dfree);
#ifdef QUOTAS
      /* Ensure we return the min value between the users quota and
         what's free on the disk. Thanks to Albrecht Gebhardt 
         <albrecht.gebhardt@uni-klu.ac.at> for this fix.
      */
      if (disk_quotas(path, &bsizeq, &dfreeq, &dsizeq))
        {
          disk_norm(&bsizeq, &dfreeq, &dsizeq);
          dfreeq_retval = ((bsizeq)/1024)*(dfreeq);
          dfree_retval =  ( dfree_retval < dfreeq_retval ) ? 
                           dfree_retval : dfreeq_retval ;
          /* maybe dfree and dfreeq are calculated using different bsizes 
             so convert dfree from bsize into bsizeq */
          /* avoid overflows due to multiplication, so do not:
                *dfree = ((*dfree) * (*bsize)) / (bsizeq); 
             bsize and bsizeq are powers of 2 so its better to
             to divide them getting a multiplication or division factor
             for dfree. Rene Nieuwenhuizen (07-10-1997) */
          if (*bsize >= bsizeq) 
            *dfree = *dfree * (*bsize / bsizeq);
          else 
            *dfree = *dfree / (bsizeq / *bsize);
          *dfree = ( *dfree < dfreeq ) ? *dfree : dfreeq ; 
          *bsize = bsizeq;
          *dsize = dsizeq;
        }
#endif
      return(dfree_retval);
    }

#ifdef NO_STATFS
  DEBUG(1,("Warning - no statfs function\n"));
  return(1);
#else
#ifdef STATFS4
  if (statfs(path,&fs,sizeof(fs),0) != 0)
#else
#ifdef USE_STATVFS
    if (statvfs(path, &fs))
#else
#ifdef STATFS3
      if (statfs(path,&fs,sizeof(fs)) == -1)	 
#else
	if (statfs(path,&fs) == -1)
#endif /* STATFS3 */
#endif /* USE_STATVFS */
#endif /* STATFS4 */
	  {
	    DEBUG(3,("dfree call failed code errno=%d\n",errno));
	    *bsize = 1024;
	    *dfree = 1;
	    *dsize = 1;
	    return(((*bsize)/1024)*(*dfree));
	  }

#ifdef ULTRIX
  *bsize = 1024;
  *dfree = fs.fd_req.bfree;
  *dsize = fs.fd_req.btot;
#else
#ifdef USE_STATVFS
  *bsize = fs.f_frsize;
#else
#ifdef USE_F_FSIZE
  /* eg: osf1 has f_fsize = fundamental filesystem block size, 
     f_bsize = optimal transfer block size (MX: 94-04-19) */
  *bsize = fs.f_fsize;
#else
  *bsize = fs.f_bsize;
#endif /* STATFS3 */
#endif /* USE_STATVFS */

#ifdef STATFS4
  *dfree = fs.f_bfree;
#else
  *dfree = fs.f_bavail;
#endif /* STATFS4 */
  *dsize = fs.f_blocks;
#endif /* ULTRIX */

#if defined(SCO) || defined(ISC) || defined(MIPS)
  *bsize = 512;
#endif

/* handle rediculous bsize values - some OSes are broken */
if ((*bsize) < 512 || (*bsize)>0xFFFF) *bsize = 1024;

  disk_norm(bsize,dfree,dsize);

  if (*bsize < 256)
    *bsize = 512;
  if ((*dsize)<1)
    {
      DEBUG(0,("dfree seems to be broken on your system\n"));
      *dsize = 20*1024*1024/(*bsize);
      *dfree = MAX(1,*dfree);
    }
  dfree_retval = ((*bsize)/1024)*(*dfree);
#ifdef QUOTAS
  /* Ensure we return the min value between the users quota and
     what's free on the disk. Thanks to Albrecht Gebhardt 
     <albrecht.gebhardt@uni-klu.ac.at> for this fix.
  */
  if (disk_quotas(path, &bsizeq, &dfreeq, &dsizeq))
    {
      disk_norm(&bsizeq, &dfreeq, &dsizeq);
      dfreeq_retval = ((bsizeq)/1024)*(dfreeq);
      dfree_retval = ( dfree_retval < dfreeq_retval ) ? 
                       dfree_retval : dfreeq_retval ;
      /* maybe dfree and dfreeq are calculated using different bsizes 
         so convert dfree from bsize into bsizeq */
      /* avoid overflows due to multiplication, so do not:
              *dfree = ((*dfree) * (*bsize)) / (bsizeq); 
       bsize and bsizeq are powers of 2 so its better to
       to divide them getting a multiplication or division factor
       for dfree. Rene Nieuwenhuizen (07-10-1997) */
      if (*bsize >= bsizeq)
        *dfree = *dfree * (*bsize / bsizeq);
      else
        *dfree = *dfree / (bsizeq / *bsize);
      *dfree = ( *dfree < dfreeq ) ? *dfree : dfreeq ;
      *bsize = bsizeq;
      *dsize = dsizeq;
    }
#endif
  return(dfree_retval);
#endif
}


/****************************************************************************
wrap it to get filenames right
****************************************************************************/
int sys_disk_free(char *path,int *bsize,int *dfree,int *dsize)
{
  return(disk_free(dos_to_unix(path,False),bsize,dfree,dsize));
}



/****************************************************************************
check a filename - possibly caling reducename

This is called by every routine before it allows an operation on a filename.
It does any final confirmation necessary to ensure that the filename is
a valid one for the user to access.
****************************************************************************/
BOOL check_name(char *name,int cnum)
{
  BOOL ret;

  errno = 0;

  if( IS_VETO_PATH(cnum, name)) 
    {
      DEBUG(5,("file path name %s vetoed\n",name));
      return(0);
    }

  ret = reduce_name(name,Connections[cnum].connectpath,lp_widelinks(SNUM(cnum)));

  /* Check if we are allowing users to follow symlinks */
  /* Patch from David Clerc <David.Clerc@cui.unige.ch>
     University of Geneva */

#ifdef S_ISLNK
  if (!lp_symlinks(SNUM(cnum)))
    {
      struct stat statbuf;
      if ( (sys_lstat(name,&statbuf) != -1) &&
          (S_ISLNK(statbuf.st_mode)) )
        {
          DEBUG(3,("check_name: denied: file path name %s is a symlink\n",name));
          ret=0; 
        }
    }
#endif

  if (!ret)
    DEBUG(5,("check_name on %s failed\n",name));

  return(ret);
}

/****************************************************************************
check a filename - possibly caling reducename
****************************************************************************/
static void check_for_pipe(char *fname)
{
  /* special case of pipe opens */
  char s[10];
  StrnCpy(s,fname,9);
  strlower(s);
  if (strstr(s,"pipe/"))
    {
      DEBUG(3,("Rejecting named pipe open for %s\n",fname));
      unix_ERR_class = ERRSRV;
      unix_ERR_code = ERRaccess;
    }
}

/****************************************************************************
fd support routines - attempt to do a sys_open
****************************************************************************/
static int fd_attempt_open(char *fname, int flags, int mode)
{
  int fd = sys_open(fname,flags,mode);

  /* Fix for files ending in '.' */
  if((fd == -1) && (errno == ENOENT) &&
     (strchr(fname,'.')==NULL))
    {
      pstrcat(fname,".");
      fd = sys_open(fname,flags,mode);
    }

#if (defined(ENAMETOOLONG) && defined(HAVE_PATHCONF))
  if ((fd == -1) && (errno == ENAMETOOLONG))
    {
      int max_len;
      char *p = strrchr(fname, '/');

      if (p == fname)   /* name is "/xxx" */
        {
          max_len = pathconf("/", _PC_NAME_MAX);
          p++;
        }
      else if ((p == NULL) || (p == fname))
        {
          p = fname;
          max_len = pathconf(".", _PC_NAME_MAX);
        }
      else
        {
          *p = '\0';
          max_len = pathconf(fname, _PC_NAME_MAX);
          *p = '/';
          p++;
        }
      if (strlen(p) > max_len)
        {
          char tmp = p[max_len];

          p[max_len] = '\0';
          if ((fd = sys_open(fname,flags,mode)) == -1)
            p[max_len] = tmp;
        }
    }
#endif
  return fd;
}

/****************************************************************************
fd support routines - attempt to find an already open file by dev
and inode - increments the ref_count of the returned file_fd_struct *.
****************************************************************************/
static file_fd_struct *fd_get_already_open(struct stat *sbuf)
{
  int i;
  file_fd_struct *fd_ptr;

  if(sbuf == 0)
    return 0;

  for(i = 0; i <= max_file_fd_used; i++) {
    fd_ptr = &FileFd[i];
    if((fd_ptr->ref_count > 0) &&
       (((uint32)sbuf->st_dev) == fd_ptr->dev) &&
       (((uint32)sbuf->st_ino) == fd_ptr->inode)) {
      fd_ptr->ref_count++;
      DEBUG(3,
       ("Re-used file_fd_struct %d, dev = %x, inode = %x, ref_count = %d\n",
        i, fd_ptr->dev, fd_ptr->inode, fd_ptr->ref_count));
      return fd_ptr;
    }
  }
  return 0;
}

/****************************************************************************
fd support routines - attempt to find a empty slot in the FileFd array.
Increments the ref_count of the returned entry.
****************************************************************************/
static file_fd_struct *fd_get_new(void)
{
  int i;
  file_fd_struct *fd_ptr;

  for(i = 0; i < MAX_OPEN_FILES; i++) {
    fd_ptr = &FileFd[i];
    if(fd_ptr->ref_count == 0) {
      fd_ptr->dev = (uint32)-1;
      fd_ptr->inode = (uint32)-1;
      fd_ptr->fd = -1;
      fd_ptr->fd_readonly = -1;
      fd_ptr->fd_writeonly = -1;
      fd_ptr->real_open_flags = -1;
      fd_ptr->ref_count++;
      /* Increment max used counter if neccessary, cuts down
	 on search time when re-using */
      if(i > max_file_fd_used)
        max_file_fd_used = i;
      DEBUG(3,("Allocated new file_fd_struct %d, dev = %x, inode = %x\n",
               i, fd_ptr->dev, fd_ptr->inode));
      return fd_ptr;
    }
  }
  DEBUG(1,("ERROR! Out of file_fd structures - perhaps increase MAX_OPEN_FILES?\n"));
  return 0;
}

/****************************************************************************
fd support routines - attempt to re-open an already open fd as O_RDWR.
Save the already open fd (we cannot close due to POSIX file locking braindamage.
****************************************************************************/
static void fd_attempt_reopen(char *fname, int mode, file_fd_struct *fd_ptr)
{
  int fd = sys_open( fname, O_RDWR, mode);

  if(fd == -1)
    return;

  if(fd_ptr->real_open_flags == O_RDONLY)
    fd_ptr->fd_readonly = fd_ptr->fd;
  if(fd_ptr->real_open_flags == O_WRONLY)
    fd_ptr->fd_writeonly = fd_ptr->fd;

  fd_ptr->fd = fd;
  fd_ptr->real_open_flags = O_RDWR;
}

/****************************************************************************
fd support routines - attempt to close the file referenced by this fd.
Decrements the ref_count and returns it.
****************************************************************************/
static int fd_attempt_close(file_fd_struct *fd_ptr)
{
  DEBUG(3,("fd_attempt_close on file_fd_struct %d, fd = %d, dev = %x, inode = %x, open_flags = %d, ref_count = %d.\n",
	   fd_ptr - &FileFd[0],
	   fd_ptr->fd, fd_ptr->dev, fd_ptr->inode,
	   fd_ptr->real_open_flags,
	   fd_ptr->ref_count));
  if(fd_ptr->ref_count > 0) {
    fd_ptr->ref_count--;
    if(fd_ptr->ref_count == 0) {
      if(fd_ptr->fd != -1)
        close(fd_ptr->fd);
      if(fd_ptr->fd_readonly != -1)
	close(fd_ptr->fd_readonly);
      if(fd_ptr->fd_writeonly != -1)
	close(fd_ptr->fd_writeonly);
      fd_ptr->fd = -1;
      fd_ptr->fd_readonly = -1;
      fd_ptr->fd_writeonly = -1;
      fd_ptr->real_open_flags = -1;
      fd_ptr->dev = (uint32)-1;
      fd_ptr->inode = (uint32)-1;
    }
  } 
 return fd_ptr->ref_count;
}

/****************************************************************************
open a file
****************************************************************************/
static void open_file(int fnum,int cnum,char *fname1,int flags,int mode, struct stat *sbuf)
{
  extern struct current_user current_user;
  pstring fname;
  struct stat statbuf;
  file_fd_struct *fd_ptr;
  files_struct *fsp = &Files[fnum];
  int accmode = (flags & (O_RDONLY | O_WRONLY | O_RDWR));

  fsp->open = False;
  fsp->fd_ptr = 0;
  fsp->granted_oplock = False;
  errno = EPERM;

  pstrcpy(fname,fname1);

  /* check permissions */

  /*
   * This code was changed after seeing a client open request 
   * containing the open mode of (DENY_WRITE/read-only) with
   * the 'create if not exist' bit set. The previous code
   * would fail to open the file read only on a read-only share
   * as it was checking the flags parameter  directly against O_RDONLY,
   * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
   * JRA.
   */

  if (!CAN_WRITE(cnum) && !Connections[cnum].printer) {
    /* It's a read-only share - fail if we wanted to write. */
    if(accmode != O_RDONLY) {
      DEBUG(3,("Permission denied opening %s\n",fname));
      check_for_pipe(fname);
      return;
    }
    else if(flags & O_CREAT) {
      /* We don't want to write - but we must make sure that O_CREAT
         doesn't create the file if we have write access into the
         directory.
       */
      flags &= ~O_CREAT;
    }
  }

  /* this handles a bug in Win95 - it doesn't say to create the file when it 
     should */
  if (Connections[cnum].printer)
    flags |= O_CREAT;

/*
  if (flags == O_WRONLY)
    DEBUG(3,("Bug in client? Set O_WRONLY without O_CREAT\n"));
*/

  /*
   * Ensure we have a valid struct stat so we can search the
   * open fd table.
   */
  if(sbuf == 0) {
    if(stat(fname, &statbuf) < 0) {
      if(errno != ENOENT) {
        DEBUG(3,("Error doing stat on file %s (%s)\n",
                 fname,strerror(errno)));

        check_for_pipe(fname);
        return;
      }
      sbuf = 0;
    } else {
      sbuf = &statbuf;
    }
  }

  /*
   * Check to see if we have this file already
   * open. If we do, just use the already open fd and increment the
   * reference count (fd_get_already_open increments the ref_count).
   */
  if((fd_ptr = fd_get_already_open(sbuf))!= 0) {

    /* File was already open. */
    if((flags & O_CREAT) && (flags & O_EXCL)) {
      fd_ptr->ref_count--;
      errno = EEXIST;
      return;
    }

    /* 
     * If not opened O_RDWR try
     * and do that here - a chmod may have been done
     * between the last open and now. 
     */
    if(fd_ptr->real_open_flags != O_RDWR)
      fd_attempt_reopen(fname, mode, fd_ptr);

    /*
     * Ensure that if we wanted write access
     * it has been opened for write, and if we wanted read it
     * was open for read. 
     */
    if(((accmode == O_WRONLY) && (fd_ptr->real_open_flags == O_RDONLY)) ||
       ((accmode == O_RDONLY) && (fd_ptr->real_open_flags == O_WRONLY)) ||
       ((accmode == O_RDWR) && (fd_ptr->real_open_flags != O_RDWR))) {
      DEBUG(3,("Error opening (already open for flags=%d) file %s (%s) (flags=%d)\n",
               fd_ptr->real_open_flags, fname,strerror(EACCES),flags));
      check_for_pipe(fname);
      fd_ptr->ref_count--;
      return;
    }

  } else {
    int open_flags;
    /* We need to allocate a new file_fd_struct (this increments the
       ref_count). */
    if((fd_ptr = fd_get_new()) == 0)
      return;
    /*
     * Whatever the requested flags, attempt read/write access,
     * as we don't know what flags future file opens may require.
     * If this fails, try again with the required flags. 
     * Even if we open read/write when only read access was 
     * requested the setting of the can_write flag in
     * the file_struct will protect us from errant
     * write requests. We never need to worry about O_APPEND
     * as this is not set anywhere in Samba.
     */
    fd_ptr->real_open_flags = O_RDWR;
    /* Set the flags as needed without the read/write modes. */
    open_flags = flags & ~(O_RDWR|O_WRONLY|O_RDONLY);
    fd_ptr->fd = fd_attempt_open(fname, open_flags|O_RDWR, mode);
    /*
     * On some systems opening a file for R/W access on a read only
     * filesystems sets errno to EROFS.
     */
#ifdef EROFS
    if((fd_ptr->fd == -1) && ((errno == EACCES) || (errno == EROFS))) {
#else /* No EROFS */
    if((fd_ptr->fd == -1) && (errno == EACCES)) {
#endif /* EROFS */
      if(flags & O_WRONLY) {
        fd_ptr->fd = fd_attempt_open(fname, open_flags|O_WRONLY, mode);
        fd_ptr->real_open_flags = O_WRONLY;
      } else {
	fd_ptr->fd = fd_attempt_open(fname, open_flags|O_RDONLY, mode);
        fd_ptr->real_open_flags = O_RDONLY;
      }
    }
  }

  if ((fd_ptr->fd >=0) && 
      Connections[cnum].printer && lp_minprintspace(SNUM(cnum))) {
    pstring dname;
    int dum1,dum2,dum3;
    char *p;
    pstrcpy(dname,fname);
    p = strrchr(dname,'/');
    if (p) *p = 0;
    if (sys_disk_free(dname,&dum1,&dum2,&dum3) < 
	lp_minprintspace(SNUM(cnum))) {
      fd_attempt_close(fd_ptr);
      fsp->fd_ptr = 0;
      if(fd_ptr->ref_count == 0)
        sys_unlink(fname);
      errno = ENOSPC;
      return;
    }
  }
    
  if (fd_ptr->fd < 0)
    {
      DEBUG(3,("Error opening file %s (%s) (flags=%d)\n",
	       fname,strerror(errno),flags));
      /* Ensure the ref_count is decremented. */
      fd_attempt_close(fd_ptr);
      check_for_pipe(fname);
      return;
    }

  if (fd_ptr->fd >= 0)
    {
      if(sbuf == 0) {
        /* Do the fstat */
        if(fstat(fd_ptr->fd, &statbuf) == -1) {
          /* Error - backout !! */
          DEBUG(3,("Error doing fstat on fd %d, file %s (%s)\n",
                   fd_ptr->fd, fname,strerror(errno)));
          /* Ensure the ref_count is decremented. */
          fd_attempt_close(fd_ptr);
          return;
        }
        sbuf = &statbuf;
      }
      /* Set the correct entries in fd_ptr. */
      fd_ptr->dev = (uint32)sbuf->st_dev;
      fd_ptr->inode = (uint32)sbuf->st_ino;

      fsp->fd_ptr = fd_ptr;
      Connections[cnum].num_files_open++;
      fsp->mode = sbuf->st_mode;
      GetTimeOfDay(&fsp->open_time);
      fsp->vuid = current_user.vuid;
      fsp->size = 0;
      fsp->pos = -1;
      fsp->open = True;
      fsp->mmap_ptr = NULL;
      fsp->mmap_size = 0;
      fsp->can_lock = True;
      fsp->can_read = ((flags & O_WRONLY)==0);
      fsp->can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
      fsp->share_mode = 0;
      fsp->print_file = Connections[cnum].printer;
      fsp->modified = False;
      fsp->granted_oplock = False;
      fsp->sent_oplock_break = False;
      fsp->cnum = cnum;
      string_set(&fsp->name,dos_to_unix(fname,False));
      fsp->wbmpx_ptr = NULL;      

      /*
       * If the printer is marked as postscript output a leading
       * file identifier to ensure the file is treated as a raw
       * postscript file.
       * This has a similar effect as CtrlD=0 in WIN.INI file.
       * tim@fsg.com 09/06/94
       */
      if (fsp->print_file && POSTSCRIPT(cnum) && 
	  fsp->can_write) 
	{
	  DEBUG(3,("Writing postscript line\n"));
	  write_file(fnum,"%!\n",3);
	}
      
      DEBUG(2,("%s %s opened file %s read=%s write=%s (numopen=%d fnum=%d)\n",
	       timestring(),Connections[cnum].user,fname,
	       BOOLSTR(fsp->can_read),BOOLSTR(fsp->can_write),
	       Connections[cnum].num_files_open,fnum));

    }

#if USE_MMAP
  /* mmap it if read-only */
  if (!fsp->can_write)
    {
      fsp->mmap_size = file_size(fname);
      fsp->mmap_ptr = (char *)mmap(NULL,fsp->mmap_size,
					  PROT_READ,MAP_SHARED,fsp->fd_ptr->fd,0);

      if (fsp->mmap_ptr == (char *)-1 || !fsp->mmap_ptr)
	{
	  DEBUG(3,("Failed to mmap() %s - %s\n",fname,strerror(errno)));
	  fsp->mmap_ptr = NULL;
	}
    }
#endif
}

/*******************************************************************
sync a file
********************************************************************/
void sync_file(int fnum)
{
#ifndef NO_FSYNC
  fsync(Files[fnum].fd_ptr->fd);
#endif
}

/****************************************************************************
run a file if it is a magic script
****************************************************************************/
static void check_magic(int fnum,int cnum)
{
  if (!*lp_magicscript(SNUM(cnum)))
    return;

  DEBUG(5,("checking magic for %s\n",Files[fnum].name));

  {
    char *p;
    if (!(p = strrchr(Files[fnum].name,'/')))
      p = Files[fnum].name;
    else
      p++;

    if (!strequal(lp_magicscript(SNUM(cnum)),p))
      return;
  }

  {
    int ret;
    pstring magic_output;
    pstring fname;
    pstrcpy(fname,Files[fnum].name);

    if (*lp_magicoutput(SNUM(cnum)))
      pstrcpy(magic_output,lp_magicoutput(SNUM(cnum)));
    else
      slprintf(magic_output,sizeof(fname)-1, "%s.out",fname);

    chmod(fname,0755);
    ret = smbrun(fname,magic_output,False);
    DEBUG(3,("Invoking magic command %s gave %d\n",fname,ret));
    unlink(fname);
  }
}


/****************************************************************************
close a file - possibly invalidating the read prediction

If normal_close is 1 then this came from a normal SMBclose (or equivalent)
operation otherwise it came as the result of some other operation such as
the closing of the connection. In the latter case printing and
magic scripts are not run
****************************************************************************/
void close_file(int fnum, BOOL normal_close)
{
  files_struct *fs_p = &Files[fnum];
  int cnum = fs_p->cnum;
  uint32 dev = fs_p->fd_ptr->dev;
  uint32 inode = fs_p->fd_ptr->inode;
  int token;

  Files[fnum].reserved = False;

#if USE_READ_PREDICTION
  invalidate_read_prediction(fs_p->fd_ptr->fd);
#endif

  fs_p->open = False;
  Connections[cnum].num_files_open--;
  if(fs_p->wbmpx_ptr) 
  {
    free((char *)fs_p->wbmpx_ptr);
    fs_p->wbmpx_ptr = NULL;
  }

#if USE_MMAP
  if(fs_p->mmap_ptr) 
  {
    munmap(fs_p->mmap_ptr,fs_p->mmap_size);
    fs_p->mmap_ptr = NULL;
  }
#endif

  if (lp_share_modes(SNUM(cnum)))
  {
    lock_share_entry( cnum, dev, inode, &token);
    del_share_mode(token, fnum);
  }

  fd_attempt_close(fs_p->fd_ptr);

  if (lp_share_modes(SNUM(cnum)))
    unlock_share_entry( cnum, dev, inode, token);

  /* NT uses smbclose to start a print - weird */
  if (normal_close && fs_p->print_file)
    print_file(fnum);

  /* check for magic scripts */
  if (normal_close)
    check_magic(fnum,cnum);

  if(fs_p->granted_oplock == True)
    global_oplocks_open--;

  fs_p->sent_oplock_break = False;

  DEBUG(2,("%s %s closed file %s (numopen=%d)\n",
	   timestring(),Connections[cnum].user,fs_p->name,
	   Connections[cnum].num_files_open));

  if (fs_p->name) {
	  string_free(&fs_p->name);
  }

  /* we will catch bugs faster by zeroing this structure */
  memset(fs_p, 0, sizeof(*fs_p));
}

enum {AFAIL,AREAD,AWRITE,AALL};

/*******************************************************************
reproduce the share mode access table
********************************************************************/
static int access_table(int new_deny,int old_deny,int old_mode,
			int share_pid,char *fname)
{
  if (new_deny == DENY_ALL || old_deny == DENY_ALL) return(AFAIL);

  if (new_deny == DENY_DOS || old_deny == DENY_DOS) {
    int pid = getpid();
    if (old_deny == new_deny && share_pid == pid) 
	return(AALL);    

    if (old_mode == 0) return(AREAD);

    /* the new smbpub.zip spec says that if the file extension is
       .com, .dll, .exe or .sym then allow the open. I will force
       it to read-only as this seems sensible although the spec is
       a little unclear on this. */
    if ((fname = strrchr(fname,'.'))) {
      if (strequal(fname,".com") ||
	  strequal(fname,".dll") ||
	  strequal(fname,".exe") ||
	  strequal(fname,".sym"))
	return(AREAD);
    }

    return(AFAIL);
  }

  switch (new_deny) 
    {
    case DENY_WRITE:
      if (old_deny==DENY_WRITE && old_mode==0) return(AREAD);
      if (old_deny==DENY_READ && old_mode==0) return(AWRITE);
      if (old_deny==DENY_NONE && old_mode==0) return(AALL);
      return(AFAIL);
    case DENY_READ:
      if (old_deny==DENY_WRITE && old_mode==1) return(AREAD);
      if (old_deny==DENY_READ && old_mode==1) return(AWRITE);
      if (old_deny==DENY_NONE && old_mode==1) return(AALL);
      return(AFAIL);
    case DENY_NONE:
      if (old_deny==DENY_WRITE) return(AREAD);
      if (old_deny==DENY_READ) return(AWRITE);
      if (old_deny==DENY_NONE) return(AALL);
      return(AFAIL);      
    }
  return(AFAIL);      
}

/*******************************************************************
check if the share mode on a file allows it to be deleted or unlinked
return True if sharing doesn't prevent the operation
********************************************************************/
BOOL check_file_sharing(int cnum,char *fname, BOOL rename_op)
{
  int i;
  int ret = False;
  share_mode_entry *old_shares = 0;
  int num_share_modes;
  struct stat sbuf;
  int token;
  int pid = getpid();
  uint32 dev, inode;

  if(!lp_share_modes(SNUM(cnum)))
    return True;

  if (stat(fname,&sbuf) == -1) return(True);

  dev = (uint32)sbuf.st_dev;
  inode = (uint32)sbuf.st_ino;

  lock_share_entry(cnum, dev, inode, &token);
  num_share_modes = get_share_modes(cnum, token, dev, inode, &old_shares);

  /*
   * Check if the share modes will give us access.
   */

  if(num_share_modes != 0)
  {
    BOOL broke_oplock;

    do
    {

      broke_oplock = False;
      for(i = 0; i < num_share_modes; i++)
      {
        share_mode_entry *share_entry = &old_shares[i];

        /* 
         * Break oplocks before checking share modes. See comment in
         * open_file_shared for details. 
         * Check if someone has an oplock on this file. If so we must 
         * break it before continuing. 
         */
        if(share_entry->op_type & BATCH_OPLOCK)
        {

          /*
           * It appears that the NT redirector may have a bug, in that
           * it tries to do an SMBmv on a file that it has open with a
           * batch oplock, and then fails to respond to the oplock break
           * request. This only seems to occur when the client is doing an
           * SMBmv to the smbd it is using - thus we try and detect this
           * condition by checking if the file being moved is open and oplocked by
           * this smbd process, and then not sending the oplock break in this
           * special case. If the file was open with a deny mode that 
           * prevents the move the SMBmv will fail anyway with a share
           * violation error. JRA.
           */
          if(rename_op && (share_entry->pid == pid))
          {
            DEBUG(0,("check_file_sharing: NT redirector workaround - rename attempted on \
batch oplocked file %s, dev = %x, inode = %x\n", fname, dev, inode));
            /* 
             * This next line is a test that allows the deny-mode
             * processing to be skipped. This seems to be needed as
             * NT insists on the rename succeeding (in Office 9x no less !).
             * This should be removed as soon as (a) MS fix the redirector
             * bug or (b) NT SMB support in Samba makes NT not issue the
             * call (as is my fervent hope). JRA.
             */ 
            continue;
          }
          else
          {
            DEBUG(5,("check_file_sharing: breaking oplock (%x) on file %s, \
dev = %x, inode = %x\n", share_entry->op_type, fname, dev, inode));

            /* Oplock break.... */
            unlock_share_entry(cnum, dev, inode, token);
            if(request_oplock_break(share_entry, dev, inode) == False)
            {
              free((char *)old_shares);
              DEBUG(0,("check_file_sharing: FAILED when breaking oplock (%x) on file %s, \
dev = %x, inode = %x\n", old_shares[i].op_type, fname, dev, inode));
              return False;
            }
            lock_share_entry(cnum, dev, inode, &token);
            broke_oplock = True;
            break;
          }
        }

        /* someone else has a share lock on it, check to see 
           if we can too */
        if ((share_entry->share_mode != DENY_DOS) || (share_entry->pid != pid))
          goto free_and_exit;

      } /* end for */

      if(broke_oplock)
      {
        free((char *)old_shares);
        num_share_modes = get_share_modes(cnum, token, dev, inode, &old_shares);
      }
    } while(broke_oplock);
  }

  /* XXXX exactly what share mode combinations should be allowed for
     deleting/renaming? */
  /* If we got here then either there were no share modes or
     all share modes were DENY_DOS and the pid == getpid() */
  ret = True;

free_and_exit:

  unlock_share_entry(cnum, dev, inode, token);
  if(old_shares != NULL)
    free((char *)old_shares);
  return(ret);
}

/****************************************************************************
  C. Hoch 11/22/95
  Helper for open_file_shared. 
  Truncate a file after checking locking; close file if locked.
  **************************************************************************/
static void truncate_unless_locked(int fnum, int cnum, int token, 
				   BOOL *share_locked)
{
  if (Files[fnum].can_write){
    if (is_locked(fnum,cnum,0x3FFFFFFF,0)){
      /* If share modes are in force for this connection we
         have the share entry locked. Unlock it before closing. */
      if (*share_locked && lp_share_modes(SNUM(cnum)))
        unlock_share_entry( cnum, Files[fnum].fd_ptr->dev, 
                            Files[fnum].fd_ptr->inode, token);
      close_file(fnum,False);   
      /* Share mode no longer locked. */
      *share_locked = False;
      errno = EACCES;
      unix_ERR_class = ERRDOS;
      unix_ERR_code = ERRlock;
    }
    else
      ftruncate(Files[fnum].fd_ptr->fd,0); 
  }
}

/****************************************************************************
check if we can open a file with a share mode
****************************************************************************/
int check_share_mode( share_mode_entry *share, int deny_mode, char *fname,
                      BOOL fcbopen, int *flags)
{
  int old_open_mode = share->share_mode &0xF;
  int old_deny_mode = (share->share_mode >>4)&7;

  if (old_deny_mode > 4 || old_open_mode > 2)
  {
    DEBUG(0,("Invalid share mode found (%d,%d,%d) on file %s\n",
               deny_mode,old_deny_mode,old_open_mode,fname));
    return False;
  }

  {
    int access_allowed = access_table(deny_mode,old_deny_mode,old_open_mode,
                                share->pid,fname);

    if ((access_allowed == AFAIL) ||
        (!fcbopen && (access_allowed == AREAD && *flags == O_RDWR)) ||
        (access_allowed == AREAD && *flags == O_WRONLY) ||
        (access_allowed == AWRITE && *flags == O_RDONLY))
    {
      DEBUG(2,("Share violation on file (%d,%d,%d,%d,%s,fcbopen = %d, flags = %d) = %d\n",
                deny_mode,old_deny_mode,old_open_mode,
                share->pid,fname, fcbopen, *flags, access_allowed));
      return False;
    }

    if (access_allowed == AREAD)
      *flags = O_RDONLY;

    if (access_allowed == AWRITE)
      *flags = O_WRONLY;

  }
  return True;
}

/****************************************************************************
open a file with a share mode
****************************************************************************/
void open_file_shared(int fnum,int cnum,char *fname,int share_mode,int ofun,
		      int mode,int oplock_request, int *Access,int *action)
{
  files_struct *fs_p = &Files[fnum];
  int flags=0;
  int flags2=0;
  int deny_mode = (share_mode>>4)&7;
  struct stat sbuf;
  BOOL file_existed = file_exist(fname,&sbuf);
  BOOL share_locked = False;
  BOOL fcbopen = False;
  int token;
  uint32 dev = 0;
  uint32 inode = 0;
  int num_share_modes = 0;

  fs_p->open = False;
  fs_p->fd_ptr = 0;

  /* this is for OS/2 EAs - try and say we don't support them */
  if (strstr(fname,".+,;=[].")) 
  {
    unix_ERR_class = ERRDOS;
    /* OS/2 Workplace shell fix may be main code stream in a later release. */ 
#if 1 /* OS2_WPS_FIX - Recent versions of OS/2 need this. */
    unix_ERR_code = ERRcannotopen;
#else /* OS2_WPS_FIX */
    unix_ERR_code = ERROR_EAS_NOT_SUPPORTED;
#endif /* OS2_WPS_FIX */

    return;
  }

  if ((ofun & 0x3) == 0 && file_existed)  
  {
    errno = EEXIST;
    return;
  }
      
  if (ofun & 0x10)
    flags2 |= O_CREAT;
  if ((ofun & 0x3) == 2)
    flags2 |= O_TRUNC;

  /* note that we ignore the append flag as 
     append does not mean the same thing under dos and unix */

  switch (share_mode&0xF)
  {
    case 1: 
      flags = O_WRONLY; 
      break;
    case 0xF: 
      fcbopen = True;
      flags = O_RDWR; 
      break;
    case 2: 
      flags = O_RDWR; 
      break;
    default:
      flags = O_RDONLY;
      break;
  }

#if defined(O_SYNC)
  if (share_mode&(1<<14)) {
	  flags2 |= O_SYNC;
  }
#endif /* O_SYNC */
  
  if (flags != O_RDONLY && file_existed && 
      (!CAN_WRITE(cnum) || IS_DOS_READONLY(dos_mode(cnum,fname,&sbuf)))) 
  {
    if (!fcbopen) 
    {
      errno = EACCES;
      return;
    }
    flags = O_RDONLY;
  }

  if (deny_mode > DENY_NONE && deny_mode!=DENY_FCB) 
  {
    DEBUG(2,("Invalid deny mode %d on file %s\n",deny_mode,fname));
    errno = EINVAL;
    return;
  }

  if (deny_mode == DENY_FCB) deny_mode = DENY_DOS;

  if (lp_share_modes(SNUM(cnum))) 
  {
    int i;
    share_mode_entry *old_shares = 0;

    if (file_existed)
    {
      dev = (uint32)sbuf.st_dev;
      inode = (uint32)sbuf.st_ino;
      lock_share_entry(cnum, dev, inode, &token);
      share_locked = True;
      num_share_modes = get_share_modes(cnum, token, dev, inode, &old_shares);
    }

    /*
     * Check if the share modes will give us access.
     */

    if(share_locked && (num_share_modes != 0))
    {
      BOOL broke_oplock;

      do
      {

        broke_oplock = False;
        for(i = 0; i < num_share_modes; i++)
        {
          share_mode_entry *share_entry = &old_shares[i];

          /* 
           * By observation of NetBench, oplocks are broken *before* share
           * modes are checked. This allows a file to be closed by the client
           * if the share mode would deny access and the client has an oplock. 
           * Check if someone has an oplock on this file. If so we must break 
           * it before continuing. 
           */
          if(share_entry->op_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
          {

            DEBUG(5,("open_file_shared: breaking oplock (%x) on file %s, \
dev = %x, inode = %x\n", share_entry->op_type, fname, dev, inode));

            /* Oplock break.... */
            unlock_share_entry(cnum, dev, inode, token);
            if(request_oplock_break(share_entry, dev, inode) == False)
            {
              free((char *)old_shares);
              DEBUG(0,("open_file_shared: FAILED when breaking oplock (%x) on file %s, \
dev = %x, inode = %x\n", old_shares[i].op_type, fname, dev, inode));
              errno = EACCES;
              unix_ERR_class = ERRDOS;
              unix_ERR_code = ERRbadshare;
              return;
            }
            lock_share_entry(cnum, dev, inode, &token);
            broke_oplock = True;
            break;
          }

          /* someone else has a share lock on it, check to see 
             if we can too */
          if(check_share_mode(share_entry, deny_mode, fname, fcbopen, &flags) == False)
          {
            free((char *)old_shares);
            unlock_share_entry(cnum, dev, inode, token);
            errno = EACCES;
            unix_ERR_class = ERRDOS;
            unix_ERR_code = ERRbadshare;
            return;
          }

        } /* end for */

        if(broke_oplock)
        {
          free((char *)old_shares);
          num_share_modes = get_share_modes(cnum, token, dev, inode, &old_shares);
        }
      } while(broke_oplock);
    }

    if(old_shares != 0)
      free((char *)old_shares);
  }

  DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n",
	   flags,flags2,mode));

  open_file(fnum,cnum,fname,flags|(flags2&~(O_TRUNC)),mode,file_existed ? &sbuf : 0);
  if (!fs_p->open && flags==O_RDWR && errno!=ENOENT && fcbopen) 
  {
    flags = O_RDONLY;
    open_file(fnum,cnum,fname,flags,mode,file_existed ? &sbuf : 0 );
  }

  if (fs_p->open) 
  {
    int open_mode=0;

    if((share_locked == False) && lp_share_modes(SNUM(cnum)))
    {
      /* We created the file - thus we must now lock the share entry before creating it. */
      dev = fs_p->fd_ptr->dev;
      inode = fs_p->fd_ptr->inode;
      lock_share_entry(cnum, dev, inode, &token);
      share_locked = True;
    }

    switch (flags) 
    {
      case O_RDONLY:
        open_mode = 0;
        break;
      case O_RDWR:
        open_mode = 2;
        break;
      case O_WRONLY:
        open_mode = 1;
        break;
    }

    fs_p->share_mode = (deny_mode<<4) | open_mode;

    if (Access)
      (*Access) = open_mode;

    if (action) 
    {
      if (file_existed && !(flags2 & O_TRUNC)) *action = 1;
      if (!file_existed) *action = 2;
      if (file_existed && (flags2 & O_TRUNC)) *action = 3;
    }
    /* We must create the share mode entry before truncate as
       truncate can fail due to locking and have to close the
       file (which expects the share_mode_entry to be there).
     */
    if (lp_share_modes(SNUM(cnum)))
    {
      uint16 port = 0;
      /* JRA. Currently this only services Exlcusive and batch
         oplocks (no other opens on this file). This needs to
         be extended to level II oplocks (multiple reader
         oplocks). */

      if(oplock_request && (num_share_modes == 0) && lp_oplocks(SNUM(cnum)) && 
	      !IS_VETO_OPLOCK_PATH(cnum,fname))
      {
        fs_p->granted_oplock = True;
        fs_p->sent_oplock_break = False;
        global_oplocks_open++;
        port = oplock_port;

        DEBUG(5,("open_file_shared: granted oplock (%x) on file %s, \
dev = %x, inode = %x\n", oplock_request, fname, dev, inode));

      }
      else
      {
        port = 0;
        oplock_request = 0;
      }
      set_share_mode(token, fnum, port, oplock_request);
    }

    if ((flags2&O_TRUNC) && file_existed)
      truncate_unless_locked(fnum,cnum,token,&share_locked);
  }

  if (share_locked && lp_share_modes(SNUM(cnum)))
    unlock_share_entry( cnum, dev, inode, token);
}

/****************************************************************************
seek a file. Try to avoid the seek if possible
****************************************************************************/
int seek_file(int fnum,uint32 pos)
{
  uint32 offset = 0;
  if (Files[fnum].print_file && POSTSCRIPT(Files[fnum].cnum))
    offset = 3;

  Files[fnum].pos = (int)(lseek(Files[fnum].fd_ptr->fd,pos+offset,SEEK_SET) 
                                  - offset);
  return(Files[fnum].pos);
}

/****************************************************************************
read from a file
****************************************************************************/
int read_file(int fnum,char *data,uint32 pos,int n)
{
  int ret=0,readret;

#if USE_READ_PREDICTION
  if (!Files[fnum].can_write)
    {
      ret = read_predict(Files[fnum].fd_ptr->fd,pos,data,NULL,n);

      data += ret;
      n -= ret;
      pos += ret;
    }
#endif

#if USE_MMAP
  if (Files[fnum].mmap_ptr)
    {
      int num = (Files[fnum].mmap_size > pos) ? (Files[fnum].mmap_size - pos) : -1;
      num = MIN(n,num);
      if (num > 0)
	{
	  memcpy(data,Files[fnum].mmap_ptr+pos,num);
	  data += num;
	  pos += num;
	  n -= num;
	  ret += num;
	}
    }
#endif

  if (n <= 0)
    return(ret);

  if (seek_file(fnum,pos) != pos)
    {
      DEBUG(3,("Failed to seek to %d\n",pos));
      return(ret);
    }
  
  if (n > 0) {
    readret = read(Files[fnum].fd_ptr->fd,data,n);
    if (readret > 0) ret += readret;
  }

  return(ret);
}


/****************************************************************************
write to a file
****************************************************************************/
int write_file(int fnum,char *data,int n)
{
  if (!Files[fnum].can_write) {
    errno = EPERM;
    return(0);
  }

  if (!Files[fnum].modified) {
    struct stat st;
    Files[fnum].modified = True;
    if (fstat(Files[fnum].fd_ptr->fd,&st) == 0) {
      int dosmode = dos_mode(Files[fnum].cnum,Files[fnum].name,&st);
      if (MAP_ARCHIVE(Files[fnum].cnum) && !IS_DOS_ARCHIVE(dosmode)) {	
	dos_chmod(Files[fnum].cnum,Files[fnum].name,dosmode | aARCH,&st);
      }
    }  
  }

  return(write_data(Files[fnum].fd_ptr->fd,data,n));
}


/****************************************************************************
load parameters specific to a connection/service
****************************************************************************/
BOOL become_service(int cnum,BOOL do_chdir)
{
  extern char magic_char;
  static int last_cnum = -1;
  int snum;

  if (!OPEN_CNUM(cnum))
    {
      last_cnum = -1;
      return(False);
    }

  Connections[cnum].lastused = smb_last_time;

  snum = SNUM(cnum);
  
  if (do_chdir &&
      ChDir(Connections[cnum].connectpath) != 0 &&
      ChDir(Connections[cnum].origpath) != 0)
    {
      DEBUG(0,("%s chdir (%s) failed cnum=%d\n",timestring(),
	    Connections[cnum].connectpath,cnum));     
      return(False);
    }

  if (cnum == last_cnum)
    return(True);

  last_cnum = cnum;

  case_default = lp_defaultcase(snum);
  case_preserve = lp_preservecase(snum);
  short_case_preserve = lp_shortpreservecase(snum);
  case_mangle = lp_casemangle(snum);
  case_sensitive = lp_casesensitive(snum);
  magic_char = lp_magicchar(snum);
  use_mangled_map = (*lp_mangled_map(snum) ? True:False);
  return(True);
}


/****************************************************************************
  find a service entry
****************************************************************************/
int find_service(char *service)
{
   int iService;

   string_sub(service,"\\","/");

   iService = lp_servicenumber(service);

   /* now handle the special case of a home directory */
   if (iService < 0)
   {
      char *phome_dir = get_home_dir(service);
      DEBUG(3,("checking for home directory %s gave %s\n",service,
	    phome_dir?phome_dir:"(NULL)"));
      if (phome_dir)
      {   
	 int iHomeService;
	 if ((iHomeService = lp_servicenumber(HOMES_NAME)) >= 0)
	 {
	    lp_add_home(service,iHomeService,phome_dir);
	    iService = lp_servicenumber(service);
	 }
      }
   }

   /* If we still don't have a service, attempt to add it as a printer. */
   if (iService < 0)
   {
      int iPrinterService;

      if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0)
      {
         char *pszTemp;

         DEBUG(3,("checking whether %s is a valid printer name...\n", service));
         pszTemp = PRINTCAP;
         if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp))
         {
            DEBUG(3,("%s is a valid printer name\n", service));
            DEBUG(3,("adding %s as a printer service\n", service));
            lp_add_printer(service,iPrinterService);
            iService = lp_servicenumber(service);
            if (iService < 0)
               DEBUG(0,("failed to add %s as a printer service!\n", service));
         }
         else
            DEBUG(3,("%s is not a valid printer name\n", service));
      }
   }

   /* just possibly it's a default service? */
   if (iService < 0) 
     {
       char *pdefservice = lp_defaultservice();
       if (pdefservice && *pdefservice && !strequal(pdefservice,service)) {
         /*
          * We need to do a local copy here as lp_defaultservice() 
          * returns one of the rotating lp_string buffers that
          * could get overwritten by the recursive find_service() call
          * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
          */
         pstring defservice;
         pstrcpy(defservice, pdefservice);
	 iService = find_service(defservice);
	 if (iService >= 0) {
	   string_sub(service,"_","/");
	   iService = lp_add_service(service,iService);
	 }
       }
     }

   if (iService >= 0)
      if (!VALID_SNUM(iService))
      {
         DEBUG(0,("Invalid snum %d for %s\n",iService,service));
	 iService = -1;
      }

   if (iService < 0)
      DEBUG(3,("find_service() failed to find service %s\n", service));

   return (iService);
}


/****************************************************************************
  create an error packet from a cached error.
****************************************************************************/
int cached_error_packet(char *inbuf,char *outbuf,int fnum,int line)
{
  write_bmpx_struct *wbmpx = Files[fnum].wbmpx_ptr;

  int32 eclass = wbmpx->wr_errclass;
  int32 err = wbmpx->wr_error;

  /* We can now delete the auxiliary struct */
  free((char *)wbmpx);
  Files[fnum].wbmpx_ptr = NULL;
  return error_packet(inbuf,outbuf,eclass,err,line);
}


struct
{
  int unixerror;
  int smbclass;
  int smbcode;
} unix_smb_errmap[] =
{
  {EPERM,ERRDOS,ERRnoaccess},
  {EACCES,ERRDOS,ERRnoaccess},
  {ENOENT,ERRDOS,ERRbadfile},
  {ENOTDIR,ERRDOS,ERRbadpath},
  {EIO,ERRHRD,ERRgeneral},
  {EBADF,ERRSRV,ERRsrverror},
  {EINVAL,ERRSRV,ERRsrverror},
  {EEXIST,ERRDOS,ERRfilexists},
  {ENFILE,ERRDOS,ERRnofids},
  {EMFILE,ERRDOS,ERRnofids},
  {ENOSPC,ERRHRD,ERRdiskfull},
#ifdef EDQUOT
  {EDQUOT,ERRHRD,ERRdiskfull},
#endif
#ifdef ENOTEMPTY
  {ENOTEMPTY,ERRDOS,ERRnoaccess},
#endif
#ifdef EXDEV
  {EXDEV,ERRDOS,ERRdiffdevice},
#endif
  {EROFS,ERRHRD,ERRnowrite},
  {0,0,0}
};

/****************************************************************************
  create an error packet from errno
****************************************************************************/
int unix_error_packet(char *inbuf,char *outbuf,int def_class,uint32 def_code,int line)
{
  int eclass=def_class;
  int ecode=def_code;
  int i=0;

  if (unix_ERR_class != SMB_SUCCESS)
    {
      eclass = unix_ERR_class;
      ecode = unix_ERR_code;
      unix_ERR_class = SMB_SUCCESS;
      unix_ERR_code = 0;
    }
  else
    {
      while (unix_smb_errmap[i].smbclass != 0)
      {
	    if (unix_smb_errmap[i].unixerror == errno)
	    {
	      eclass = unix_smb_errmap[i].smbclass;
	      ecode = unix_smb_errmap[i].smbcode;
	      break;
	    }
	  i++;
      }
    }

  return(error_packet(inbuf,outbuf,eclass,ecode,line));
}


/****************************************************************************
  create an error packet. Normally called using the ERROR() macro
****************************************************************************/
int error_packet(char *inbuf,char *outbuf,int error_class,uint32 error_code,int line)
{
  int outsize = set_message(outbuf,0,0,True);
  int cmd = CVAL(inbuf,smb_com);
  int flgs2 = SVAL(outbuf,smb_flg2); 

  if ((flgs2 & FLAGS2_32_BIT_ERROR_CODES) == FLAGS2_32_BIT_ERROR_CODES)
  {
    SIVAL(outbuf,smb_rcls,error_code);
    
    DEBUG(3,("%s 32 bit error packet at line %d cmd=%d (%s) eclass=%08x [%s]\n",
           timestring(), line, cmd, smb_fn_name(cmd), error_code, smb_errstr(outbuf)));
  }
  else
  {
    CVAL(outbuf,smb_rcls) = error_class;
    SSVAL(outbuf,smb_err,error_code);  
    DEBUG(3,("%s error packet at line %d cmd=%d (%s) eclass=%d ecode=%d\n",
	   timestring(),
	   line,
	   (int)CVAL(inbuf,smb_com),
	   smb_fn_name(CVAL(inbuf,smb_com)),
	   error_class,
	   error_code));

  }
  
  if (errno != 0)
    DEBUG(3,("error string = %s\n",strerror(errno)));
  
  return(outsize);
}


#ifndef SIGCLD_IGNORE
/****************************************************************************
this prevents zombie child processes
****************************************************************************/
static int sig_cld(void)
{
  static int depth = 0;
  if (depth != 0)
    {
      DEBUG(0,("ERROR: Recursion in sig_cld? Perhaps you need `#define USE_WAITPID'?\n"));
      depth=0;
      return(0);
    }
  depth++;

  BlockSignals(True,SIGCLD);
  DEBUG(5,("got SIGCLD\n"));

#ifdef USE_WAITPID
  while (sys_waitpid((pid_t)-1,(int *)NULL, WNOHANG) > 0);
#endif

  /* Stop zombies */
  /* Stevens, Adv. Unix Prog. says that on system V you must call
     wait before reinstalling the signal handler, because the kernel
     calls the handler from within the signal-call when there is a
     child that has exited. This would lead to an infinite recursion
     if done vice versa. */
        
#ifndef DONT_REINSTALL_SIG
#ifdef SIGCLD_IGNORE
  signal(SIGCLD, SIG_IGN);  
#else
  signal(SIGCLD, SIGNAL_CAST sig_cld);
#endif
#endif

#ifndef USE_WAITPID
  while (wait3(WAIT3_CAST1 NULL, WNOHANG, WAIT3_CAST2 NULL) > 0);
#endif
  depth--;
  BlockSignals(False,SIGCLD);
  return 0;
}
#endif

/****************************************************************************
  this is called when the client exits abruptly
  **************************************************************************/
static int sig_pipe(void)
{
	struct cli_state *cli;
	BlockSignals(True,SIGPIPE);

	if ((cli = server_client()) && cli->initialised) {
		DEBUG(3,("lost connection to password server\n"));
		cli_shutdown(cli);
#ifndef DONT_REINSTALL_SIG
		signal(SIGPIPE, SIGNAL_CAST sig_pipe);
#endif
		BlockSignals(False,SIGPIPE);
		return 0;
	}

	exit_server("Got sigpipe\n");
	return(0);
}

/****************************************************************************
  open the socket communication
****************************************************************************/
static BOOL open_sockets(BOOL is_daemon,int port)
{
  extern int Client;

  if (is_daemon)
  {
    int num_interfaces = iface_count();
    int fd_listenset[FD_SETSIZE];
    fd_set listen_set;
    int s;
    int i;

    /* Stop zombies */
#ifdef SIGCLD_IGNORE
    signal(SIGCLD, SIG_IGN);
#else
    signal(SIGCLD, SIGNAL_CAST sig_cld);
#endif

    if(atexit_set == 0)
      atexit(killkids);

    FD_ZERO(&listen_set);

    if(lp_interfaces() && lp_bind_interfaces_only())
    {
       /* We have been given an interfaces line, and been 
          told to only bind to those interfaces. Create a
          socket per interface and bind to only these.
        */

      if(num_interfaces > FD_SETSIZE)
      {
        DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
max can be %d\n", num_interfaces, FD_SETSIZE));
        return False;
      }

      /* Now open a listen socket for each of the interfaces. */
      for(i = 0; i < num_interfaces; i++)
      {
        struct in_addr *ifip = iface_n_ip(i);

        if(ifip == NULL)
        {
          DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
          continue;
        }
        s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr);
        if(s == -1)
          return False;
        /* ready to listen */
        if (listen(s, 5) == -1) 
        {
          DEBUG(0,("listen: %s\n",strerror(errno)));
          close(s);
          return False;
        }
        FD_SET(s,&listen_set);
      }
    }
    else
    {
      /* Just bind to 0.0.0.0 - accept connections from anywhere. */
      num_interfaces = 1;

      /* open an incoming socket */
      s = open_socket_in(SOCK_STREAM, port, 0,interpret_addr(lp_socket_address()));
      if (s == -1)
        return(False);

      /* ready to listen */
      if (listen(s, 5) == -1) 
      {
        DEBUG(0,("open_sockets: listen: %s\n",strerror(errno)));
        close(s);
        return False;
      }

      fd_listenset[0] = s;
      FD_SET(s,&listen_set);
    }      

    /* now accept incoming connections - forking a new process
       for each incoming connection */
    DEBUG(2,("waiting for a connection\n"));
    while (1)
    {
      fd_set lfds;
      int num;

      memcpy((char *)&lfds, (char *)&listen_set, sizeof(listen_set));

      num = sys_select(&lfds,NULL);

      if (num == -1 && errno == EINTR)
        continue;

      /* Find the sockets that are read-ready - accept on these. */
      for( ; num > 0; num--)
      {
        struct sockaddr addr;
        int in_addrlen = sizeof(addr);

        s = -1;
        for(i = 0; i < num_interfaces; i++)
        {
          if(FD_ISSET(fd_listenset[i],&lfds))
          {
            s = fd_listenset[i];
            /* Clear this so we don't look at it again. */
            FD_CLR(fd_listenset[i],&lfds);
            break;
          }
        }

        Client = accept(s,&addr,&in_addrlen);

        if (Client == -1 && errno == EINTR)
          continue;

        if (Client == -1)
        {
          DEBUG(0,("open_sockets: accept: %s\n",strerror(errno)));
          continue;
        }

#ifdef NO_FORK_DEBUG
#ifndef NO_SIGNAL_TEST
        signal(SIGPIPE, SIGNAL_CAST sig_pipe);
        signal(SIGCLD, SIGNAL_CAST SIG_DFL);
#endif /* NO_SIGNAL_TEST */
        return True;
#else /* NO_FORK_DEBUG */
        if (Client != -1 && fork()==0)
        {
          /* Child code ... */

#ifndef NO_SIGNAL_TEST
          signal(SIGPIPE, SIGNAL_CAST sig_pipe);
          signal(SIGCLD, SIGNAL_CAST SIG_DFL);
#endif /* NO_SIGNAL_TEST */
          /* close the listening socket(s) */
          for(i = 0; i < num_interfaces; i++)
            close(fd_listenset[i]);

          /* close our standard file descriptors */
          close_low_fds();
          am_parent = 0;
  
          set_socket_options(Client,"SO_KEEPALIVE");
          set_socket_options(Client,user_socket_options);

          /* Reset global variables in util.c so that
             client substitutions will be done correctly
             in the process.
           */
          reset_globals_after_fork();
          return True; 
        }
        close(Client); /* The parent doesn't need this socket */
#endif /* NO_FORK_DEBUG */
      } /* end for num */
    } /* end while 1 */
  } /* end if is_daemon */
  else
  {
    /* Started from inetd. fd 0 is the socket. */
    /* We will abort gracefully when the client or remote system 
       goes away */
#ifndef NO_SIGNAL_TEST
    signal(SIGPIPE, SIGNAL_CAST sig_pipe);
#endif
    Client = dup(0);

    /* close our standard file descriptors */
    close_low_fds();

    set_socket_options(Client,"SO_KEEPALIVE");
    set_socket_options(Client,user_socket_options);
  }

  return True;
}

/****************************************************************************
  process an smb from the client - split out from the process() code so
  it can be used by the oplock break code.
****************************************************************************/

static void process_smb(char *inbuf, char *outbuf)
{
  extern int Client;
  static int trans_num;
  int msg_type = CVAL(inbuf,0);
  int32 len = smb_len(inbuf);
  int nread = len + 4;

  if (trans_num == 0) {
	  /* on the first packet, check the global hosts allow/ hosts
	     deny parameters before doing any parsing of the packet
	     passed to us by the client.  This prevents attacks on our
	     parsing code from hosts not in the hosts allow list */
	  if (!check_access(-1)) {
		  /* send a negative session response "not listining on calling
		   name" */
		  static unsigned char buf[5] = {0x83, 0, 0, 1, 0x81};
		  DEBUG(1,("%s Connection denied from %s\n",
			   timestring(),client_addr(Client)));
		  send_smb(Client,(char *)buf);
		  exit_server("connection denied");
	  }
  }

  DEBUG(6,("got message type 0x%x of len 0x%x\n",msg_type,len));
  DEBUG(3,("%s Transaction %d of length %d\n",timestring(),trans_num,nread));

#ifdef WITH_VTP
  if(trans_num == 1 && VT_Check(inbuf)) 
  {
    VT_Process();
    return;
  }
#endif

  if (msg_type == 0)
    show_msg(inbuf);
  else if(msg_type == 0x85)
    return; /* Keepalive packet. */

  nread = construct_reply(inbuf,outbuf,nread,max_send);
      
  if(nread > 0) 
  {
    if (CVAL(outbuf,0) == 0)
      show_msg(outbuf);
	
    if (nread != smb_len(outbuf) + 4) 
    {
      DEBUG(0,("ERROR: Invalid message response size! %d %d\n",
                 nread, smb_len(outbuf)));
    }
    else
      send_smb(Client,outbuf);
  }
  trans_num++;
}

/****************************************************************************
  open the oplock IPC socket communication
****************************************************************************/
static BOOL open_oplock_ipc(void)
{
  struct sockaddr_in sock_name;
  int len = sizeof(sock_name);

  DEBUG(3,("open_oplock_ipc: opening loopback UDP socket.\n"));

  /* Open a lookback UDP socket on a random port. */
  oplock_sock = open_socket_in(SOCK_DGRAM, 0, 0, htonl(INADDR_LOOPBACK));
  if (oplock_sock == -1)
  {
    DEBUG(0,("open_oplock_ipc: Failed to get local UDP socket for \
address %x. Error was %s\n", htonl(INADDR_LOOPBACK), strerror(errno)));
    oplock_port = 0;
    return(False);
  }

  /* Find out the transient UDP port we have been allocated. */
  if(getsockname(oplock_sock, (struct sockaddr *)&sock_name, &len)<0)
  {
    DEBUG(0,("open_oplock_ipc: Failed to get local UDP port. Error was %s\n",
            strerror(errno)));
    close(oplock_sock);
    oplock_sock = -1;
    oplock_port = 0;
    return False;
  }
  oplock_port = ntohs(sock_name.sin_port);

  DEBUG(3,("open_oplock ipc: pid = %d, oplock_port = %u\n", 
            getpid(), oplock_port));

  return True;
}

/****************************************************************************
  process an oplock break message.
****************************************************************************/
static BOOL process_local_message(int sock, char *buffer, int buf_size)
{
  int32 msg_len;
  uint16 from_port;
  char *msg_start;

  msg_len = IVAL(buffer,UDP_CMD_LEN_OFFSET);
  from_port = SVAL(buffer,UDP_CMD_PORT_OFFSET);

  msg_start = &buffer[UDP_CMD_HEADER_LEN];

  DEBUG(5,("process_local_message: Got a message of length %d from port (%d)\n", 
            msg_len, from_port));

  /* Switch on message command - currently OPLOCK_BREAK_CMD is the
     only valid request. */

  switch(SVAL(msg_start,UDP_MESSAGE_CMD_OFFSET))
  {
    case OPLOCK_BREAK_CMD:
      /* Ensure that the msg length is correct. */
      if(msg_len != OPLOCK_BREAK_MSG_LEN)
      {
        DEBUG(0,("process_local_message: incorrect length for OPLOCK_BREAK_CMD (was %d, \
should be %d).\n", msg_len, OPLOCK_BREAK_MSG_LEN));
        return False;
      }
      {
        uint32 remotepid = IVAL(msg_start,OPLOCK_BREAK_PID_OFFSET);
        uint32 dev = IVAL(msg_start,OPLOCK_BREAK_DEV_OFFSET);
        uint32 inode = IVAL(msg_start, OPLOCK_BREAK_INODE_OFFSET);
        struct timeval tval;
        struct sockaddr_in toaddr;

        tval.tv_sec = IVAL(msg_start, OPLOCK_BREAK_SEC_OFFSET);
        tval.tv_usec = IVAL(msg_start, OPLOCK_BREAK_USEC_OFFSET);

        DEBUG(5,("process_local_message: oplock break request from \
pid %d, port %d, dev = %x, inode = %x\n", remotepid, from_port, dev, inode));

        /*
         * If we have no record of any currently open oplocks,
         * it's not an error, as a close command may have
         * just been issued on the file that was oplocked.
         * Just return success in this case.
         */

        if(global_oplocks_open != 0)
        {
          if(oplock_break(dev, inode, &tval) == False)
          {
            DEBUG(0,("process_local_message: oplock break failed - \
not returning udp message.\n"));
            return False;
          }
        }
        else
        {
          DEBUG(3,("process_local_message: oplock break requested with no outstanding \
oplocks. Returning success.\n"));
        }

        /* Send the message back after OR'ing in the 'REPLY' bit. */
        SSVAL(msg_start,UDP_MESSAGE_CMD_OFFSET,OPLOCK_BREAK_CMD | CMD_REPLY);
  
        bzero((char *)&toaddr,sizeof(toaddr));
        toaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        toaddr.sin_port = htons(from_port);
        toaddr.sin_family = AF_INET;

        if(sendto( sock, msg_start, OPLOCK_BREAK_MSG_LEN, 0,
                (struct sockaddr *)&toaddr, sizeof(toaddr)) < 0) 
        {
          DEBUG(0,("process_local_message: sendto process %d failed. Errno was %s\n",
                    remotepid, strerror(errno)));
          return False;
        }

        DEBUG(5,("process_local_message: oplock break reply sent to \
pid %d, port %d, for file dev = %x, inode = %x\n", remotepid, 
                from_port, dev, inode));

      }
      break;
    /* 
     * Keep this as a debug case - eventually we can remove it.
     */
    case 0x8001:
      DEBUG(0,("process_local_message: Received unsolicited break \
reply - dumping info.\n"));

      if(msg_len != OPLOCK_BREAK_MSG_LEN)
      {
        DEBUG(0,("process_local_message: ubr: incorrect length for reply \
(was %d, should be %d).\n", msg_len, OPLOCK_BREAK_MSG_LEN));
        return False;
      }

      {
        uint32 remotepid = IVAL(msg_start,OPLOCK_BREAK_PID_OFFSET);
        uint32 dev = IVAL(msg_start,OPLOCK_BREAK_DEV_OFFSET);
        uint32 inode = IVAL(msg_start, OPLOCK_BREAK_INODE_OFFSET);

        DEBUG(0,("process_local_message: unsolicited oplock break reply from \
pid %d, port %d, dev = %x, inode = %x\n", remotepid, from_port, dev, inode));

       }
       return False;

    default:
      DEBUG(0,("process_local_message: unknown UDP message command code (%x) - ignoring.\n",
                (unsigned int)SVAL(msg_start,0)));
      return False;
  }
  return True;
}

/****************************************************************************
 Process an oplock break directly.
****************************************************************************/
BOOL oplock_break(uint32 dev, uint32 inode, struct timeval *tval)
{
  extern int Client;
  char *inbuf = NULL;
  char *outbuf = NULL;
  files_struct *fsp = NULL;
  int fnum;
  time_t start_time;
  BOOL shutdown_server = False;

  DEBUG(3,("%s oplock_break: called for dev = %x, inode = %x. Current \
global_oplocks_open = %d\n", timestring(), dev, inode, global_oplocks_open));

  /* We need to search the file open table for the
     entry containing this dev and inode, and ensure
     we have an oplock on it. */
  for( fnum = 0; fnum < MAX_OPEN_FILES; fnum++)
  {
    if(OPEN_FNUM(fnum))
    {
      if((Files[fnum].fd_ptr->dev == dev) && (Files[fnum].fd_ptr->inode == inode) &&
         (Files[fnum].open_time.tv_sec == tval->tv_sec) && 
         (Files[fnum].open_time.tv_usec == tval->tv_usec)) {
	      fsp = &Files[fnum];
	      break;
      }
    }
  }

  if(fsp == NULL)
  {
    /* The file could have been closed in the meantime - return success. */
    DEBUG(0,("%s oplock_break: cannot find open file with dev = %x, inode = %x (fnum = %d) \
allowing break to succeed.\n", timestring(), dev, inode, fnum));
    return True;
  }

  /* Ensure we have an oplock on the file */

  /* There is a potential race condition in that an oplock could
     have been broken due to another udp request, and yet there are
     still oplock break messages being sent in the udp message
     queue for this file. So return true if we don't have an oplock,
     as we may have just freed it.
   */

  if(!fsp->granted_oplock)
  {
    DEBUG(0,("%s oplock_break: file %s (fnum = %d, dev = %x, inode = %x) has no oplock. Allowing break to succeed regardless.\n", timestring(), fsp->name, fnum, dev, inode));
    return True;
  }

  /* mark the oplock break as sent - we don't want to send twice! */
  if (fsp->sent_oplock_break)
  {
    DEBUG(0,("%s oplock_break: ERROR: oplock_break already sent for file %s (fnum = %d, dev = %x, inode = %x)\n", timestring(), fsp->name, fnum, dev, inode));

    /* We have to fail the open here as we cannot send another oplock break on this
       file whilst we are awaiting a response from the client - neither can we
       allow another open to succeed while we are waiting for the client. */
    return False;
  }

  /* Now comes the horrid part. We must send an oplock break to the client,
     and then process incoming messages until we get a close or oplock release.
     At this point we know we need a new inbuf/outbuf buffer pair.
     We cannot use these staticaly as we may recurse into here due to
     messages crossing on the wire.
   */

  if((inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN))==NULL)
  {
    DEBUG(0,("oplock_break: malloc fail for input buffer.\n"));
    return False;
  }

  if((outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN))==NULL)
  {
    DEBUG(0,("oplock_break: malloc fail for output buffer.\n"));
    free(inbuf);
    inbuf = NULL;
    return False;
  }

  /* Prepare the SMBlockingX message. */
  bzero(outbuf,smb_size);
  set_message(outbuf,8,0,True);

  SCVAL(outbuf,smb_com,SMBlockingX);
  SSVAL(outbuf,smb_tid,fsp->cnum);
  SSVAL(outbuf,smb_pid,0xFFFF);
  SSVAL(outbuf,smb_uid,0);
  SSVAL(outbuf,smb_mid,0xFFFF);
  SCVAL(outbuf,smb_vwv0,0xFF);
  SSVAL(outbuf,smb_vwv2,fnum);
  SCVAL(outbuf,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
  /* Change this when we have level II oplocks. */
  SCVAL(outbuf,smb_vwv3+1,OPLOCKLEVEL_NONE);
 
  send_smb(Client, outbuf);

  /* Remember we just sent an oplock break on this file. */
  fsp->sent_oplock_break = True;

  /* We need this in case a readraw crosses on the wire. */
  global_oplock_break = True;
 
  /* Process incoming messages. */

  /* JRA - If we don't get a break from the client in OPLOCK_BREAK_TIMEOUT
     seconds we should just die.... */

  start_time = time(NULL);

  while(OPEN_FNUM(fnum) && fsp->granted_oplock)
  {
    if(receive_smb(Client,inbuf,OPLOCK_BREAK_TIMEOUT * 1000) == False)
    {
      /*
       * Die if we got an error.
       */

      if (smb_read_error == READ_EOF)
        DEBUG(0,("%s oplock_break: end of file from client\n", timestring()));
 
      if (smb_read_error == READ_ERROR)
        DEBUG(0,("%s oplock_break: receive_smb error (%s)\n",
                  timestring(), strerror(errno)));

      if (smb_read_error == READ_TIMEOUT)
        DEBUG(0,("%s oplock_break: receive_smb timed out after %d seconds.\n",
                  timestring(), OPLOCK_BREAK_TIMEOUT));

      DEBUG(0,("%s oplock_break failed for file %s (fnum = %d, dev = %x, \
inode = %x).\n", timestring(), fsp->name, fnum, dev, inode));
      shutdown_server = True;
      break;
    }

    /*
     * There are certain SMB requests that we shouldn't allow
     * to recurse. opens, renames and deletes are the obvious
     * ones. This is handled in the switch_message() function.
     * If global_oplock_break is set they will push the packet onto
     * the pending smb queue and return -1 (no reply).
     * JRA.
     */

    process_smb(inbuf, outbuf);

    /*
     * Die if we go over the time limit.
     */

    if((time(NULL) - start_time) > OPLOCK_BREAK_TIMEOUT)
    {
      DEBUG(0,("%s oplock_break: no break received from client within \
%d seconds.\n", timestring(), OPLOCK_BREAK_TIMEOUT));
      DEBUG(0,("%s oplock_break failed for file %s (fnum = %d, dev = %x, \
inode = %x).\n", timestring(), fsp->name, fnum, dev, inode));
      shutdown_server = True;
      break;
    }
  }

  /* Free the buffers we've been using to recurse. */
  free(inbuf);
  free(outbuf);

  /* We need this in case a readraw crossed on the wire. */
  if(global_oplock_break)
    global_oplock_break = False;

  /*
   * If the client did not respond we must die.
   */

  if(shutdown_server)
  {
    DEBUG(0,("%s oplock_break: client failure in break - shutting down this smbd.\n",
          timestring()));
    close_sockets();
    close(oplock_sock);
    exit_server("oplock break failure");
  }

  if(OPEN_FNUM(fnum))
  {
    /* The lockingX reply will have removed the oplock flag 
       from the sharemode. */
    /* Paranoia.... */
    fsp->granted_oplock = False;
    fsp->sent_oplock_break = False;
    global_oplocks_open--;
  }

  /* Santity check - remove this later. JRA */
  if(global_oplocks_open < 0)
  {
    DEBUG(0,("oplock_break: global_oplocks_open < 0 (%d). PANIC ERROR\n",
              global_oplocks_open));
    exit_server("oplock_break: global_oplocks_open < 0");
  }

  DEBUG(3,("%s oplock_break: returning success for fnum = %d, dev = %x, inode = %x. Current \
global_oplocks_open = %d\n", timestring(), fnum, dev, inode, global_oplocks_open));

  return True;
}

/****************************************************************************
Send an oplock break message to another smbd process. If the oplock is held 
by the local smbd then call the oplock break function directly.
****************************************************************************/

BOOL request_oplock_break(share_mode_entry *share_entry, 
                          uint32 dev, uint32 inode)
{
  char op_break_msg[OPLOCK_BREAK_MSG_LEN];
  struct sockaddr_in addr_out;
  int pid = getpid();
  time_t start_time;
  int time_left;

  if(pid == share_entry->pid)
  {
    /* We are breaking our own oplock, make sure it's us. */
    if(share_entry->op_port != oplock_port)
    {
      DEBUG(0,("request_oplock_break: corrupt share mode entry - pid = %d, port = %d \
should be %d\n", pid, share_entry->op_port, oplock_port));
      return False;
    }

    DEBUG(5,("request_oplock_break: breaking our own oplock\n"));

    /* Call oplock break direct. */
    return oplock_break(dev, inode, &share_entry->time);
  }

  /* We need to send a OPLOCK_BREAK_CMD message to the
     port in the share mode entry. */

  SSVAL(op_break_msg,UDP_MESSAGE_CMD_OFFSET,OPLOCK_BREAK_CMD);
  SIVAL(op_break_msg,OPLOCK_BREAK_PID_OFFSET,pid);
  SIVAL(op_break_msg,OPLOCK_BREAK_DEV_OFFSET,dev);
  SIVAL(op_break_msg,OPLOCK_BREAK_INODE_OFFSET,inode);
  SIVAL(op_break_msg,OPLOCK_BREAK_SEC_OFFSET,(uint32)share_entry->time.tv_sec);
  SIVAL(op_break_msg,OPLOCK_BREAK_USEC_OFFSET,(uint32)share_entry->time.tv_usec);

  /* set the address and port */
  bzero((char *)&addr_out,sizeof(addr_out));
  addr_out.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  addr_out.sin_port = htons( share_entry->op_port );
  addr_out.sin_family = AF_INET;
   
  DEBUG(3,("%s request_oplock_break: sending a oplock break message to pid %d on port %d \
for dev = %x, inode = %x\n", timestring(), share_entry->pid, share_entry->op_port, dev, inode));

  if(sendto(oplock_sock,op_break_msg,OPLOCK_BREAK_MSG_LEN,0,
         (struct sockaddr *)&addr_out,sizeof(addr_out)) < 0)
  {
    DEBUG(0,("%s request_oplock_break: failed when sending a oplock break message \
to pid %d on port %d for dev = %x, inode = %x. Error was %s\n",
         timestring(), share_entry->pid, share_entry->op_port, dev, inode,
         strerror(errno)));
    return False;
  }

  /*
   * Now we must await the oplock broken message coming back
   * from the target smbd process. Timeout if it fails to
   * return in (OPLOCK_BREAK_TIMEOUT + OPLOCK_BREAK_TIMEOUT_FUDGEFACTOR) seconds.
   * While we get messages that aren't ours, loop.
   */

  start_time = time(NULL);
  time_left = OPLOCK_BREAK_TIMEOUT+OPLOCK_BREAK_TIMEOUT_FUDGEFACTOR;

  while(time_left >= 0)
  {
    char op_break_reply[UDP_CMD_HEADER_LEN+OPLOCK_BREAK_MSG_LEN];
    int32 reply_msg_len;
    uint16 reply_from_port;
    char *reply_msg_start;

    if(receive_local_message(oplock_sock, op_break_reply, sizeof(op_break_reply),
               time_left ? time_left * 1000 : 1) == False)
    {
      if(smb_read_error == READ_TIMEOUT)
      {
        DEBUG(0,("%s request_oplock_break: no response received to oplock break request to \
pid %d on port %d for dev = %x, inode = %x\n", timestring(), share_entry->pid, 
                           share_entry->op_port, dev, inode));
        /*
         * This is a hack to make handling of failing clients more robust.
         * If a oplock break response message is not received in the timeout
         * period we may assume that the smbd servicing that client holding
         * the oplock has died and the client changes were lost anyway, so
         * we should continue to try and open the file.
         */
        break;
      }
      else
        DEBUG(0,("%s request_oplock_break: error in response received to oplock break request to \
pid %d on port %d for dev = %x, inode = %x. Error was (%s).\n", timestring, share_entry->pid, 
                         share_entry->op_port, dev, inode, strerror(errno)));
      return False;
    }

    reply_msg_len = IVAL(op_break_reply,UDP_CMD_LEN_OFFSET);
    reply_from_port = SVAL(op_break_reply,UDP_CMD_PORT_OFFSET);

    reply_msg_start = &op_break_reply[UDP_CMD_HEADER_LEN];

    if(reply_msg_len != OPLOCK_BREAK_MSG_LEN)
    {
      /* Ignore it. */
      DEBUG(0,("%s request_oplock_break: invalid message length received. Ignoring\n",
             timestring()));
      continue;
    }

    /*
     * Test to see if this is the reply we are awaiting.
     */

    if((SVAL(reply_msg_start,UDP_MESSAGE_CMD_OFFSET) & CMD_REPLY) &&
       (reply_from_port == share_entry->op_port) && 
       (memcmp(&reply_msg_start[OPLOCK_BREAK_PID_OFFSET], 
               &op_break_msg[OPLOCK_BREAK_PID_OFFSET],
               OPLOCK_BREAK_MSG_LEN - OPLOCK_BREAK_PID_OFFSET) == 0))
    {
      /*
       * This is the reply we've been waiting for.
       */
      break;
    }
    else
    {
      /*
       * This is another message - probably a break request.
       * Process it to prevent potential deadlock.
       * Note that the code in switch_message() prevents
       * us from recursing into here as any SMB requests
       * we might process that would cause another oplock
       * break request to be made will be queued.
       * JRA.
       */

      process_local_message(oplock_sock, op_break_reply, sizeof(op_break_reply));
    }

    time_left -= (time(NULL) - start_time);
  }

  DEBUG(3,("%s request_oplock_break: broke oplock.\n", timestring()));

  return True;
}

/****************************************************************************
Get the next SMB packet, doing the local message processing automatically.
****************************************************************************/

BOOL receive_next_smb(int smbfd, int oplockfd, char *inbuf, int bufsize, int timeout)
{
  BOOL got_smb = False;
  BOOL ret;

  do
  {
    ret = receive_message_or_smb(smbfd,oplockfd,inbuf,bufsize,
                                 timeout,&got_smb);

    if(ret && !got_smb)
    {
      /* Deal with oplock break requests from other smbd's. */
      process_local_message(oplock_sock, inbuf, bufsize);
      continue;
    }

    if(ret && (CVAL(inbuf,0) == 0x85))
    {
      /* Keepalive packet. */
      got_smb = False;
    }

  }
  while(ret && !got_smb);

  return ret;
}

/****************************************************************************
check if a snum is in use
****************************************************************************/
BOOL snum_used(int snum)
{
  int i;
  for (i=0;i<MAX_CONNECTIONS;i++)
    if (OPEN_CNUM(i) && (SNUM(i) == snum))
      return(True);
  return(False);
}

/****************************************************************************
  reload the services file
  **************************************************************************/
BOOL reload_services(BOOL test)
{
  BOOL ret;

  if (lp_loaded())
    {
      pstring fname;
      pstrcpy(fname,lp_configfile());
      if (file_exist(fname,NULL) && !strcsequal(fname,servicesf))
	{
	  pstrcpy(servicesf,fname);
	  test = False;
	}
    }

  reopen_logs();

  if (test && !lp_file_list_changed())
    return(True);

  lp_killunused(snum_used);

  ret = lp_load(servicesf,False,False,True);

  /* perhaps the config filename is now set */
  if (!test)
    reload_services(True);

  reopen_logs();

  load_interfaces();

  {
    extern int Client;
    if (Client != -1) {      
      set_socket_options(Client,"SO_KEEPALIVE");
      set_socket_options(Client,user_socket_options);
    }
  }

  reset_mangled_cache();

  /* this forces service parameters to be flushed */
  become_service(-1,True);

  return(ret);
}



/****************************************************************************
this prevents zombie child processes
****************************************************************************/
static int sig_hup(void)
{
  BlockSignals(True,SIGHUP);
  DEBUG(0,("Got SIGHUP\n"));
  reload_services(False);
#ifndef DONT_REINSTALL_SIG
  signal(SIGHUP,SIGNAL_CAST sig_hup);
#endif
  BlockSignals(False,SIGHUP);
  return(0);
}


/****************************************************************************
  make a connection to a service
****************************************************************************/
int make_connection(char *service,char *user,char *password, int pwlen, char *dev,uint16 vuid)
{
  int cnum;
  int snum;
  struct passwd *pass = NULL;
  connection_struct *pcon;
  BOOL guest = False;
  BOOL force = False;

  strlower(service);

  snum = find_service(service);
  if (snum < 0)
    {
      extern int Client;
      if (strequal(service,"IPC$"))
	{	  
	  DEBUG(3,("%s refusing IPC connection\n",timestring()));
	  return(-3);
	}

      DEBUG(0,("%s %s (%s) couldn't find service %s\n",timestring(),remote_machine,client_addr(Client),service));      
      return(-2);
    }

  if (strequal(service,HOMES_NAME))
    {
      if (*user && Get_Pwnam(user,True))
	return(make_connection(user,user,password,pwlen,dev,vuid));

      if(lp_security() != SEC_SHARE)
      {
        if (validated_username(vuid))
        {
          pstrcpy(user,validated_username(vuid));
          return(make_connection(user,user,password,pwlen,dev,vuid));
        }
      }
      else
      {
        /*
         * Security = share. Try with sesssetup_user as the username.
         */
        if(*sesssetup_user)
        {
          pstrcpy(user,sesssetup_user);
          return(make_connection(user,user,password,pwlen,dev,vuid));
        }
      }
    }

  if (!lp_snum_ok(snum) || !check_access(snum)) {    
    return(-4);
  }

  /* you can only connect to the IPC$ service as an ipc device */
  if (strequal(service,"IPC$"))
    pstrcpy(dev,"IPC");

  if (*dev == '?' || !*dev)
    {
      if (lp_print_ok(snum))
	pstrcpy(dev,"LPT1:");
      else
	pstrcpy(dev,"A:");
    }

  /* if the request is as a printer and you can't print then refuse */
  strupper(dev);
  if (!lp_print_ok(snum) && (strncmp(dev,"LPT",3) == 0)) {
    DEBUG(1,("Attempt to connect to non-printer as a printer\n"));
    return(-6);
  }

  /* lowercase the user name */
  strlower(user);

  /* add it as a possible user name */
  add_session_user(service);

  /* shall we let them in? */
  if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid))
    {
      DEBUG(2,("%s invalid username/password for %s\n",timestring(),service));
      return(-1);
    }
  
  cnum = find_free_connection(str_checksum(service) + str_checksum(user));
  if (cnum < 0)
    {
      DEBUG(0,("%s couldn't find free connection\n",timestring()));      
      return(-1);
    }

  pcon = &Connections[cnum];
  bzero((char *)pcon,sizeof(*pcon));

  /* find out some info about the user */
  pass = Get_Pwnam(user,True);

  if (pass == NULL)
    {
      DEBUG(0,("%s couldn't find account %s\n",timestring(),user)); 
      return(-7);
    }

  pcon->read_only = lp_readonly(snum);

  {
    pstring list;
    StrnCpy(list,lp_readlist(snum),sizeof(pstring)-1);
    string_sub(list,"%S",service);

    if (user_in_list(user,list))
      pcon->read_only = True;

    StrnCpy(list,lp_writelist(snum),sizeof(pstring)-1);
    string_sub(list,"%S",service);

    if (user_in_list(user,list))
      pcon->read_only = False;    
  }

  /* admin user check */

  /* JRA - original code denied admin user if the share was
     marked read_only. Changed as I don't think this is needed,
     but old code left in case there is a problem here.
   */
  if (user_in_list(user,lp_admin_users(snum)) 
#if 0
      && !pcon->read_only)
#else
      )
#endif
    {
      pcon->admin_user = True;
      DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
    }
  else
    pcon->admin_user = False;
    
  pcon->force_user = force;
  pcon->vuid = vuid;
  pcon->uid = pass->pw_uid;
  pcon->gid = pass->pw_gid;
  pcon->num_files_open = 0;
  pcon->lastused = time(NULL);
  pcon->service = snum;
  pcon->used = True;
  pcon->printer = (strncmp(dev,"LPT",3) == 0);
  pcon->ipc = (strncmp(dev,"IPC",3) == 0);
  pcon->dirptr = NULL;
  pcon->veto_list = NULL;
  pcon->hide_list = NULL;
  pcon->veto_oplock_list = NULL;
  string_set(&pcon->dirpath,"");
  string_set(&pcon->user,user);

#if HAVE_GETGRNAM 
  if (*lp_force_group(snum))
    {
      struct group *gptr;
      pstring gname;

      StrnCpy(gname,lp_force_group(snum),sizeof(pstring)-1);
      /* default service may be a group name 		*/
      string_sub(gname,"%S",service);
      gptr = (struct group *)getgrnam(gname);

      if (gptr)
	{
	  pcon->gid = gptr->gr_gid;
	  DEBUG(3,("Forced group %s\n",gname));
	}
      else
	DEBUG(1,("Couldn't find group %s\n",gname));
    }
#endif

  if (*lp_force_user(snum))
    {
      struct passwd *pass2;
      fstring fuser;
      fstrcpy(fuser,lp_force_user(snum));
      pass2 = (struct passwd *)Get_Pwnam(fuser,True);
      if (pass2)
	{
	  pcon->uid = pass2->pw_uid;
	  string_set(&pcon->user,fuser);
	  fstrcpy(user,fuser);
	  pcon->force_user = True;
	  DEBUG(3,("Forced user %s\n",fuser));	  
	}
      else
	DEBUG(1,("Couldn't find user %s\n",fuser));
    }

  {
    pstring s;
    pstrcpy(s,lp_pathname(snum));
    standard_sub(cnum,s);
    string_set(&pcon->connectpath,s);
    DEBUG(3,("Connect path is %s\n",s));
  }

  /* groups stuff added by ih */
  pcon->ngroups = 0;
  pcon->igroups = NULL;
  pcon->groups = NULL;
  pcon->attrs = NULL;

  if (!IS_IPC(cnum))
    {
      /* Find all the groups this uid is in and store them. Used by become_user() */
      setup_groups(pcon->user,pcon->uid,pcon->gid,
                  &pcon->ngroups,&pcon->igroups,&pcon->groups,&pcon->attrs);
      
      /* check number of connections */
      if (!claim_connection(cnum,
			    lp_servicename(SNUM(cnum)),
			    lp_max_connections(SNUM(cnum)),False))
	{
	  DEBUG(1,("too many connections - rejected\n"));
	  return(-8);
	}  

      if (lp_status(SNUM(cnum)))
	claim_connection(cnum,"STATUS.",MAXSTATUS,False);
    } /* IS_IPC */

  pcon->open = True;

  /* execute any "root preexec = " line */
  if (*lp_rootpreexec(SNUM(cnum)))
    {
      pstring cmd;
      pstrcpy(cmd,lp_rootpreexec(SNUM(cnum)));
      standard_sub(cnum,cmd);
      DEBUG(5,("cmd=%s\n",cmd));
      smbrun(cmd,NULL,False);
    }

  if (!become_user(&Connections[cnum], cnum,pcon->vuid))
    {
      DEBUG(0,("Can't become connected user!\n"));
      pcon->open = False;
      if (!IS_IPC(cnum)) {
	yield_connection(cnum,
			 lp_servicename(SNUM(cnum)),
			 lp_max_connections(SNUM(cnum)));
	if (lp_status(SNUM(cnum))) yield_connection(cnum,"STATUS.",MAXSTATUS);
      }
      return(-1);
    }

  if (ChDir(pcon->connectpath) != 0)
    {
      DEBUG(0,("Can't change directory to %s (%s)\n",
	       pcon->connectpath,strerror(errno)));
      pcon->open = False;
      unbecome_user();
      if (!IS_IPC(cnum)) {
	yield_connection(cnum,
			 lp_servicename(SNUM(cnum)),
			 lp_max_connections(SNUM(cnum)));
	if (lp_status(SNUM(cnum))) yield_connection(cnum,"STATUS.",MAXSTATUS);
      }
      return(-5);      
    }

  string_set(&pcon->origpath,pcon->connectpath);

#if SOFTLINK_OPTIMISATION
  /* resolve any soft links early */
  {
    pstring s;
    pstrcpy(s,pcon->connectpath);
    GetWd(s);
    string_set(&pcon->connectpath,s);
    ChDir(pcon->connectpath);
  }
#endif

  num_connections_open++;
  add_session_user(user);
  
  /* execute any "preexec = " line */
  if (*lp_preexec(SNUM(cnum)))
    {
      pstring cmd;
      pstrcpy(cmd,lp_preexec(SNUM(cnum)));
      standard_sub(cnum,cmd);
      smbrun(cmd,NULL,False);
    }
  
  /* we've finished with the sensitive stuff */
  unbecome_user();

  /* Add veto/hide lists */
  if (!IS_IPC(cnum) && !IS_PRINT(cnum))
  {
    set_namearray( &pcon->veto_list, lp_veto_files(SNUM(cnum)));
    set_namearray( &pcon->hide_list, lp_hide_files(SNUM(cnum)));
    set_namearray( &pcon->veto_oplock_list, lp_veto_oplocks(SNUM(cnum)));
  }

  {
    extern int Client;
    DEBUG(IS_IPC(cnum)?3:1,("%s %s (%s) connect to service %s as user %s (uid=%d,gid=%d) (pid %d)\n",
			    timestring(),
			    remote_machine,
			    client_addr(Client),
			    lp_servicename(SNUM(cnum)),user,
			    pcon->uid,
			    pcon->gid,
			    (int)getpid()));
  }

  return(cnum);
}

/****************************************************************************
  Attempt to break an oplock on a file (if oplocked).
  Returns True if the file was closed as a result of
  the oplock break, False otherwise.
  Used as a last ditch attempt to free a space in the 
  file table when we have run out.
****************************************************************************/

static BOOL attempt_close_oplocked_file(files_struct *fp)
{

  DEBUG(5,("attempt_close_oplocked_file: checking file %s.\n", fp->name));

  if (fp->open && fp->granted_oplock && !fp->sent_oplock_break) {

    /* Try and break the oplock. */
    file_fd_struct *fsp = fp->fd_ptr;
    if(oplock_break( fsp->dev, fsp->inode, &fp->open_time)) {
      if(!fp->open) /* Did the oplock break close the file ? */
        return True;
    }
  }

  return False;
}

/****************************************************************************
  find first available file slot
****************************************************************************/
int find_free_file(void )
{
	int i;
	static int first_file;

	/* we want to give out file handles differently on each new
	   connection because of a common bug in MS clients where they try to
	   reuse a file descriptor from an earlier smb connection. This code
	   increases the chance that the errant client will get an error rather
	   than causing corruption */
	if (first_file == 0) {
		first_file = (getpid() ^ (int)time(NULL)) % MAX_OPEN_FILES;
		if (first_file == 0) first_file = 1;
	}

	if (first_file >= MAX_OPEN_FILES)
		first_file = 1;

	for (i=first_file;i<MAX_OPEN_FILES;i++)
		if (!Files[i].open && !Files[i].reserved) {
			memset(&Files[i], 0, sizeof(Files[i]));
			first_file = i+1;
			Files[i].reserved = True;
			return(i);
		}

	/* returning a file handle of 0 is a bad idea - so we start at 1 */
	for (i=1;i<first_file;i++)
		if (!Files[i].open && !Files[i].reserved) {
			memset(&Files[i], 0, sizeof(Files[i]));
			first_file = i+1;
			Files[i].reserved = True;
			return(i);
		}

        /* 
         * Before we give up, go through the open files 
         * and see if there are any files opened with a
         * batch oplock. If so break the oplock and then
         * re-use that entry (if it becomes closed).
         * This may help as NT/95 clients tend to keep
         * files batch oplocked for quite a long time
         * after they have finished with them.
         */
        for (i=first_file;i<MAX_OPEN_FILES;i++) {
          if(attempt_close_oplocked_file( &Files[i])) {
            memset(&Files[i], 0, sizeof(Files[i]));
            first_file = i+1;
            Files[i].reserved = True;
            return(i);
          }
        }

        for (i=1;i<MAX_OPEN_FILES;i++) {
          if(attempt_close_oplocked_file( &Files[i])) {
            memset(&Files[i], 0, sizeof(Files[i]));
            first_file = i+1;
            Files[i].reserved = True;
            return(i);
          }
        }

	DEBUG(1,("ERROR! Out of file structures - perhaps increase MAX_OPEN_FILES?\n"));
	return(-1);
}

/****************************************************************************
  find first available connection slot, starting from a random position.
The randomisation stops problems with the server dieing and clients
thinking the server is still available.
****************************************************************************/
static int find_free_connection(int hash )
{
  int i;
  BOOL used=False;
  hash = (hash % (MAX_CONNECTIONS-2))+1;

 again:

  for (i=hash+1;i!=hash;)
    {
      if (!Connections[i].open && Connections[i].used == used) 
	{
	  DEBUG(3,("found free connection number %d\n",i));
	  return(i);
	}
      i++;
      if (i == MAX_CONNECTIONS)
	i = 1;
    }

  if (!used)
    {
      used = !used;
      goto again;
    }

  DEBUG(1,("ERROR! Out of connection structures\n"));
  return(-1);
}


/****************************************************************************
reply for the core protocol
****************************************************************************/
int reply_corep(char *outbuf)
{
  int outsize = set_message(outbuf,1,0,True);

  Protocol = PROTOCOL_CORE;

  return outsize;
}


/****************************************************************************
reply for the coreplus protocol
****************************************************************************/
int reply_coreplus(char *outbuf)
{
  int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
  int outsize = set_message(outbuf,13,0,True);
  SSVAL(outbuf,smb_vwv5,raw); /* tell redirector we support
				 readbraw and writebraw (possibly) */
  CVAL(outbuf,smb_flg) = 0x81; /* Reply, SMBlockread, SMBwritelock supported */
  SSVAL(outbuf,smb_vwv1,0x1); /* user level security, don't encrypt */	

  Protocol = PROTOCOL_COREPLUS;

  return outsize;
}


/****************************************************************************
reply for the lanman 1.0 protocol
****************************************************************************/
int reply_lanman1(char *outbuf)
{
  int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
  int secword=0;
  BOOL doencrypt = SMBENCRYPT();
  time_t t = time(NULL);

  if (lp_security()>=SEC_USER) secword |= 1;
  if (doencrypt) secword |= 2;

  set_message(outbuf,13,doencrypt?8:0,True);
  SSVAL(outbuf,smb_vwv1,secword); 
  /* Create a token value and add it to the outgoing packet. */
  if (doencrypt) 
    generate_next_challenge(smb_buf(outbuf));

  Protocol = PROTOCOL_LANMAN1;

  CVAL(outbuf,smb_flg) = 0x81; /* Reply, SMBlockread, SMBwritelock supported */
  SSVAL(outbuf,smb_vwv2,max_recv);
  SSVAL(outbuf,smb_vwv3,lp_maxmux()); /* maxmux */
  SSVAL(outbuf,smb_vwv4,1);
  SSVAL(outbuf,smb_vwv5,raw); /* tell redirector we support
				 readbraw writebraw (possibly) */
  SIVAL(outbuf,smb_vwv6,getpid());
  SSVAL(outbuf,smb_vwv10, TimeDiff(t)/60);

  put_dos_date(outbuf,smb_vwv8,t);

  return (smb_len(outbuf)+4);
}


/****************************************************************************
reply for the lanman 2.0 protocol
****************************************************************************/
int reply_lanman2(char *outbuf)
{
  int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
  int secword=0;
  BOOL doencrypt = SMBENCRYPT();
  time_t t = time(NULL);
  struct cli_state *cli = NULL;
  char cryptkey[8];
  char crypt_len = 0;

  if (lp_security() == SEC_SERVER) {
	  cli = server_cryptkey();
  }

  if (cli) {
	  DEBUG(3,("using password server validation\n"));
	  doencrypt = ((cli->sec_mode & 2) != 0);
  }

  if (lp_security()>=SEC_USER) secword |= 1;
  if (doencrypt) secword |= 2;

  if (doencrypt) {
	  crypt_len = 8;
	  if (!cli) {
		  generate_next_challenge(cryptkey);
	  } else {
		  memcpy(cryptkey, cli->cryptkey, 8);
		  set_challenge(cli->cryptkey);
	  }
  }

  set_message(outbuf,13,crypt_len,True);
  SSVAL(outbuf,smb_vwv1,secword); 
  SIVAL(outbuf,smb_vwv6,getpid());
  if (doencrypt) 
	  memcpy(smb_buf(outbuf), cryptkey, 8);

  Protocol = PROTOCOL_LANMAN2;

  CVAL(outbuf,smb_flg) = 0x81; /* Reply, SMBlockread, SMBwritelock supported */
  SSVAL(outbuf,smb_vwv2,max_recv);
  SSVAL(outbuf,smb_vwv3,lp_maxmux()); 
  SSVAL(outbuf,smb_vwv4,1);
  SSVAL(outbuf,smb_vwv5,raw); /* readbraw and/or writebraw */
  SSVAL(outbuf,smb_vwv10, TimeDiff(t)/60);
  put_dos_date(outbuf,smb_vwv8,t);

  return (smb_len(outbuf)+4);
}


/****************************************************************************
reply for the nt protocol
****************************************************************************/
int reply_nt1(char *outbuf)
{
  /* dual names + lock_and_read + nt SMBs + remote API calls */
  int capabilities = CAP_NT_FIND|CAP_LOCK_AND_READ|CAP_RPC_REMOTE_APIS;
/*
  other valid capabilities which we may support at some time...
                     CAP_LARGE_FILES|CAP_NT_SMBS|CAP_RPC_REMOTE_APIS;
                     CAP_LARGE_READX|CAP_STATUS32|CAP_LEVEL_II_OPLOCKS;
 */

  int secword=0;
  BOOL doencrypt = SMBENCRYPT();
  time_t t = time(NULL);
  int data_len;
  struct cli_state *cli = NULL;
  char cryptkey[8];
  char crypt_len = 0;

  if (lp_security() == SEC_SERVER) {
	  cli = server_cryptkey();
  }

  if (cli) {
	  DEBUG(3,("using password server validation\n"));
	  doencrypt = ((cli->sec_mode & 2) != 0);
  }

  if (doencrypt) {
	  crypt_len = 8;
	  if (!cli) {
		  generate_next_challenge(cryptkey);
	  } else {
		  memcpy(cryptkey, cli->cryptkey, 8);
		  set_challenge(cli->cryptkey);
	  }
  }

  if (lp_readraw() && lp_writeraw()) {
	  capabilities |= CAP_RAW_MODE;
  }

  if (lp_security() >= SEC_USER) secword |= 1;
  if (doencrypt) secword |= 2;

  /* decide where (if) to put the encryption challenge, and
     follow it with the OEM'd domain name
   */
  data_len = crypt_len + strlen(global_myworkgroup) + 1;

  set_message(outbuf,17,data_len,True);
  pstrcpy(smb_buf(outbuf)+crypt_len, global_myworkgroup);

  CVAL(outbuf,smb_vwv1) = secword;
  SSVALS(outbuf,smb_vwv16+1,crypt_len);
  if (doencrypt) 
	  memcpy(smb_buf(outbuf), cryptkey, 8);

  Protocol = PROTOCOL_NT1;

  SSVAL(outbuf,smb_vwv1+1,lp_maxmux()); /* maxmpx */
  SSVAL(outbuf,smb_vwv2+1,1); /* num vcs */
  SIVAL(outbuf,smb_vwv3+1,0xffff); /* max buffer. LOTS! */
  SIVAL(outbuf,smb_vwv5+1,0x10000); /* raw size. full 64k */
  SIVAL(outbuf,smb_vwv7+1,getpid()); /* session key */
  SIVAL(outbuf,smb_vwv9+1,capabilities); /* capabilities */
  put_long_date(outbuf+smb_vwv11+1,t);
  SSVALS(outbuf,smb_vwv15+1,TimeDiff(t)/60);
  SSVAL(outbuf,smb_vwv17,data_len); /* length of challenge+domain strings */

  return (smb_len(outbuf)+4);
}

/* these are the protocol lists used for auto architecture detection:

WinNT 3.51:
protocol [PC NETWORK PROGRAM 1.0]
protocol [XENIX CORE]
protocol [MICROSOFT NETWORKS 1.03]
protocol [LANMAN1.0]
protocol [Windows for Workgroups 3.1a]
protocol [LM1.2X002]
protocol [LANMAN2.1]
protocol [NT LM 0.12]

Win95:
protocol [PC NETWORK PROGRAM 1.0]
protocol [XENIX CORE]
protocol [MICROSOFT NETWORKS 1.03]
protocol [LANMAN1.0]
protocol [Windows for Workgroups 3.1a]
protocol [LM1.2X002]
protocol [LANMAN2.1]
protocol [NT LM 0.12]

OS/2:
protocol [PC NETWORK PROGRAM 1.0]
protocol [XENIX CORE]
protocol [LANMAN1.0]
protocol [LM1.2X002]
protocol [LANMAN2.1]
*/

/*
  * Modified to recognize the architecture of the remote machine better.
  *
  * This appears to be the matrix of which protocol is used by which
  * MS product.
       Protocol                       WfWg    Win95   WinNT  OS/2
       PC NETWORK PROGRAM 1.0          1       1       1      1
       XENIX CORE                                      2      2
       MICROSOFT NETWORKS 3.0          2       2       
       DOS LM1.2X002                   3       3       
       MICROSOFT NETWORKS 1.03                         3
       DOS LANMAN2.1                   4       4       
       LANMAN1.0                                       4      3
       Windows for Workgroups 3.1a     5       5       5
       LM1.2X002                                       6      4
       LANMAN2.1                                       7      5
       NT LM 0.12                              6       8
  *
  *  tim@fsg.com 09/29/95
  */
  
#define ARCH_WFWG     0x3      /* This is a fudge because WfWg is like Win95 */
#define ARCH_WIN95    0x2
#define	ARCH_OS2      0xC      /* Again OS/2 is like NT */
#define ARCH_WINNT    0x8
#define ARCH_SAMBA    0x10
 
#define ARCH_ALL      0x1F
 
/* List of supported protocols, most desired first */
struct {
  char *proto_name;
  char *short_name;
  int (*proto_reply_fn)(char *);
  int protocol_level;
} supported_protocols[] = {
  {"NT LANMAN 1.0",           "NT1",      reply_nt1,      PROTOCOL_NT1},
  {"NT LM 0.12",              "NT1",      reply_nt1,      PROTOCOL_NT1},
  {"LM1.2X002",               "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
  {"Samba",                   "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
  {"DOS LM1.2X002",           "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
  {"LANMAN1.0",               "LANMAN1",  reply_lanman1,  PROTOCOL_LANMAN1},
  {"MICROSOFT NETWORKS 3.0",  "LANMAN1",  reply_lanman1,  PROTOCOL_LANMAN1},
  {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
  {"PC NETWORK PROGRAM 1.0",  "CORE",     reply_corep,    PROTOCOL_CORE}, 
  {NULL,NULL},
};


/****************************************************************************
  reply to a negprot
****************************************************************************/
static int reply_negprot(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
{
  int outsize = set_message(outbuf,1,0,True);
  int Index=0;
  int choice= -1;
  int protocol;
  char *p;
  int bcc = SVAL(smb_buf(inbuf),-2);
  int arch = ARCH_ALL;

  p = smb_buf(inbuf)+1;
  while (p < (smb_buf(inbuf) + bcc))
    { 
      Index++;
      DEBUG(3,("Requested protocol [%s]\n",p));
      if (strcsequal(p,"Windows for Workgroups 3.1a"))
	arch &= ( ARCH_WFWG | ARCH_WIN95 | ARCH_WINNT );
      else if (strcsequal(p,"DOS LM1.2X002"))
	arch &= ( ARCH_WFWG | ARCH_WIN95 );
      else if (strcsequal(p,"DOS LANMAN2.1"))
	arch &= ( ARCH_WFWG | ARCH_WIN95 );
      else if (strcsequal(p,"NT LM 0.12"))
	arch &= ( ARCH_WIN95 | ARCH_WINNT );
      else if (strcsequal(p,"LANMAN2.1"))
	arch &= ( ARCH_WINNT | ARCH_OS2 );
      else if (strcsequal(p,"LM1.2X002"))
	arch &= ( ARCH_WINNT | ARCH_OS2 );
      else if (strcsequal(p,"MICROSOFT NETWORKS 1.03"))
	arch &= ARCH_WINNT;
      else if (strcsequal(p,"XENIX CORE"))
	arch &= ( ARCH_WINNT | ARCH_OS2 );
      else if (strcsequal(p,"Samba")) {
	arch = ARCH_SAMBA;
	break;
      }
 
      p += strlen(p) + 2;
    }
    
  switch ( arch ) {
  case ARCH_SAMBA:
    set_remote_arch(RA_SAMBA);
    break;
  case ARCH_WFWG:
    set_remote_arch(RA_WFWG);
    break;
  case ARCH_WIN95:
    set_remote_arch(RA_WIN95);
    break;
  case ARCH_WINNT:
    set_remote_arch(RA_WINNT);
    break;
  case ARCH_OS2:
    set_remote_arch(RA_OS2);
    break;
  default:
    set_remote_arch(RA_UNKNOWN);
    break;
  }
 
  /* possibly reload - change of architecture */
  reload_services(True);      
    
  /* a special case to stop password server loops */
  if (Index == 1 && strequal(remote_machine,myhostname) && 
      (lp_security()==SEC_SERVER || lp_security()==SEC_DOMAIN))
    exit_server("Password server loop!");
  
  /* Check for protocols, most desirable first */
  for (protocol = 0; supported_protocols[protocol].proto_name; protocol++)
    {
      p = smb_buf(inbuf)+1;
      Index = 0;
      if (lp_maxprotocol() >= supported_protocols[protocol].protocol_level)
	while (p < (smb_buf(inbuf) + bcc))
	  { 
	    if (strequal(p,supported_protocols[protocol].proto_name))
	      choice = Index;
	    Index++;
	    p += strlen(p) + 2;
	  }
      if(choice != -1)
	break;
    }
  
  SSVAL(outbuf,smb_vwv0,choice);
  if(choice != -1) {
    extern fstring remote_proto;
    fstrcpy(remote_proto,supported_protocols[protocol].short_name);
    reload_services(True);          
    outsize = supported_protocols[protocol].proto_reply_fn(outbuf);
    DEBUG(3,("Selected protocol %s\n",supported_protocols[protocol].proto_name));
  }
  else {
    DEBUG(0,("No protocol supported !\n"));
  }
  SSVAL(outbuf,smb_vwv0,choice);
  
  DEBUG(5,("%s negprot index=%d\n",timestring(),choice));

  return(outsize);
}


/****************************************************************************
close all open files for a connection
****************************************************************************/
static void close_open_files(int cnum)
{
  int i;
  for (i=0;i<MAX_OPEN_FILES;i++)
    if( Files[i].cnum == cnum && Files[i].open) {
      close_file(i,False);
    }
}



/****************************************************************************
close a cnum
****************************************************************************/
void close_cnum(int cnum, uint16 vuid)
{
  extern int Client;
  DirCacheFlush(SNUM(cnum));

  unbecome_user();

  if (!OPEN_CNUM(cnum))
    {
      DEBUG(0,("Can't close cnum %d\n",cnum));
      return;
    }

  DEBUG(IS_IPC(cnum)?3:1,("%s %s (%s) closed connection to service %s\n",
			  timestring(),
			  remote_machine,client_addr(Client),
			  lp_servicename(SNUM(cnum))));

  yield_connection(cnum,
		   lp_servicename(SNUM(cnum)),
		   lp_max_connections(SNUM(cnum)));

  if (lp_status(SNUM(cnum)))
    yield_connection(cnum,"STATUS.",MAXSTATUS);

  close_open_files(cnum);
  dptr_closecnum(cnum);

  /* execute any "postexec = " line */
  if (*lp_postexec(SNUM(cnum)) && become_user(&Connections[cnum], cnum,vuid))
    {
      pstring cmd;
      pstrcpy(cmd,lp_postexec(SNUM(cnum)));
      standard_sub(cnum,cmd);
      smbrun(cmd,NULL,False);
      unbecome_user();
    }

  unbecome_user();
  /* execute any "root postexec = " line */
  if (*lp_rootpostexec(SNUM(cnum)))
    {
      pstring cmd;
      pstrcpy(cmd,lp_rootpostexec(SNUM(cnum)));
      standard_sub(cnum,cmd);
      smbrun(cmd,NULL,False);
    }

  Connections[cnum].open = False;
  num_connections_open--;
  if (Connections[cnum].ngroups && Connections[cnum].groups)
    {
      if (Connections[cnum].igroups != (int *)Connections[cnum].groups)
	free(Connections[cnum].groups);
      free(Connections[cnum].igroups);
      Connections[cnum].groups = NULL;
      Connections[cnum].igroups = NULL;
      Connections[cnum].ngroups = 0;
    }

  free_namearray(Connections[cnum].veto_list);
  free_namearray(Connections[cnum].hide_list);
  free_namearray(Connections[cnum].veto_oplock_list);

  string_set(&Connections[cnum].user,"");
  string_set(&Connections[cnum].dirpath,"");
  string_set(&Connections[cnum].connectpath,"");
}



#if DUMP_CORE
/*******************************************************************
prepare to dump a core file - carefully!
********************************************************************/
static BOOL dump_core(void)
{
  char *p;
  pstring dname;
  pstrcpy(dname,debugf);
  if ((p=strrchr(dname,'/'))) *p=0;
  pstrcat(dname,"/corefiles");
  mkdir(dname,0700);
  sys_chown(dname,getuid(),getgid());
  chmod(dname,0700);
  if (chdir(dname)) return(False);
  umask(~(0700));

#ifndef NO_GETRLIMIT
#ifdef RLIMIT_CORE
  {
    struct rlimit rlp;
    getrlimit(RLIMIT_CORE, &rlp);
    rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
    setrlimit(RLIMIT_CORE, &rlp);
    getrlimit(RLIMIT_CORE, &rlp);
    DEBUG(3,("Core limits now %d %d\n",rlp.rlim_cur,rlp.rlim_max));
  }
#endif
#endif


  DEBUG(0,("Dumping core in %s\n",dname));
  return(True);
}
#endif

/****************************************************************************
exit the server
****************************************************************************/
void exit_server(char *reason)
{
  static int firsttime=1;
  int i;

  if (!firsttime) exit(0);
  firsttime = 0;

  unbecome_user();
  DEBUG(2,("Closing connections\n"));
  for (i=0;i<MAX_CONNECTIONS;i++)
    if (Connections[i].open)
      close_cnum(i,(uint16)-1);
#ifdef DFS_AUTH
  if (dcelogin_atmost_once)
    dfs_unlogin();
#endif
  if (!reason) {   
    int oldlevel = DEBUGLEVEL;
    DEBUGLEVEL = 10;
    DEBUG(0,("Last message was %s\n",smb_fn_name(last_message)));
    if (last_inbuf)
      show_msg(last_inbuf);
    DEBUGLEVEL = oldlevel;
    DEBUG(0,("===============================================================\n"));
#if DUMP_CORE
    if (dump_core()) return;
#endif
  }    

  locking_end();

  DEBUG(3,("%s Server exit  (%s)\n",timestring(),reason?reason:""));
  exit(0);
}

/****************************************************************************
do some standard substitutions in a string
****************************************************************************/
void standard_sub(int cnum,char *str)
{
  if (VALID_CNUM(cnum)) {
    char *p, *s, *home;

    for ( s=str ; (p=strchr(s, '%')) != NULL ; s=p ) {
      switch (*(p+1)) {
        case 'H' : if ((home = get_home_dir(Connections[cnum].user))!=NULL)
                     string_sub(p,"%H",home);
                   else
                     p += 2;
                   break;
        case 'P' : string_sub(p,"%P",Connections[cnum].connectpath); break;
        case 'S' : string_sub(p,"%S",lp_servicename(Connections[cnum].service)); break;
        case 'g' : string_sub(p,"%g",gidtoname(Connections[cnum].gid)); break;
        case 'u' : string_sub(p,"%u",Connections[cnum].user); break;
	/* 
         * Patch from jkf@soton.ac.uk
         * Left the %N (NIS server name) in standard_sub_basic as it
         * is a feature for logon servers, hence uses the username.
	 * The %p (NIS server path) code is here as it is used
	 * instead of the default "path =" string in [homes] and so
	 * needs the service name, not the username. 
         */
	case 'p' : string_sub(p,"%p",automount_path(lp_servicename(Connections[cnum].service))); break;
        case '\0' : p++; break; /* don't run off the end of the string */
        default  : p+=2; break;
      }
    }
  }
  standard_sub_basic(str);
}

/*
These flags determine some of the permissions required to do an operation 

Note that I don't set NEED_WRITE on some write operations because they
are used by some brain-dead clients when printing, and I don't want to
force write permissions on print services.
*/
#define AS_USER (1<<0)
#define NEED_WRITE (1<<1)
#define TIME_INIT (1<<2)
#define CAN_IPC (1<<3)
#define AS_GUEST (1<<5)
#define QUEUE_IN_OPLOCK (1<<6)

/* 
   define a list of possible SMB messages and their corresponding
   functions. Any message that has a NULL function is unimplemented -
   please feel free to contribute implementations!
*/
struct smb_message_struct
{
  int code;
  char *name;
  int (*fn)(char *, char *, int, int);
  int flags;
#if PROFILING
  unsigned long time;
#endif
}
 smb_messages[] = {

    /* CORE PROTOCOL */

   {SMBnegprot,"SMBnegprot",reply_negprot,0},
   {SMBtcon,"SMBtcon",reply_tcon,0},
   {SMBtdis,"SMBtdis",reply_tdis,0},
   {SMBexit,"SMBexit",reply_exit,0},
   {SMBioctl,"SMBioctl",reply_ioctl,0},
   {SMBecho,"SMBecho",reply_echo,0},
   {SMBsesssetupX,"SMBsesssetupX",reply_sesssetup_and_X,0},
   {SMBtconX,"SMBtconX",reply_tcon_and_X,0},
   {SMBulogoffX, "SMBulogoffX", reply_ulogoffX, 0}, /* ulogoff doesn't give a valid TID */
   {SMBgetatr,"SMBgetatr",reply_getatr,AS_USER},
   {SMBsetatr,"SMBsetatr",reply_setatr,AS_USER | NEED_WRITE},
   {SMBchkpth,"SMBchkpth",reply_chkpth,AS_USER},
   {SMBsearch,"SMBsearch",reply_search,AS_USER},
   {SMBopen,"SMBopen",reply_open,AS_USER | QUEUE_IN_OPLOCK },

   /* note that SMBmknew and SMBcreate are deliberately overloaded */   
   {SMBcreate,"SMBcreate",reply_mknew,AS_USER},
   {SMBmknew,"SMBmknew",reply_mknew,AS_USER}, 

   {SMBunlink,"SMBunlink",reply_unlink,AS_USER | NEED_WRITE | QUEUE_IN_OPLOCK},
   {SMBread,"SMBread",reply_read,AS_USER},
   {SMBwrite,"SMBwrite",reply_write,AS_USER},
   {SMBclose,"SMBclose",reply_close,AS_USER | CAN_IPC},
   {SMBmkdir,"SMBmkdir",reply_mkdir,AS_USER | NEED_WRITE},
   {SMBrmdir,"SMBrmdir",reply_rmdir,AS_USER | NEED_WRITE},
   {SMBdskattr,"SMBdskattr",reply_dskattr,AS_USER},
   {SMBmv,"SMBmv",reply_mv,AS_USER | NEED_WRITE | QUEUE_IN_OPLOCK},

   /* this is a Pathworks specific call, allowing the 
      changing of the root path */
   {pSETDIR,"pSETDIR",reply_setdir,AS_USER}, 

   {SMBlseek,"SMBlseek",reply_lseek,AS_USER},
   {SMBflush,"SMBflush",reply_flush,AS_USER},
   {SMBctemp,"SMBctemp",reply_ctemp,AS_USER | QUEUE_IN_OPLOCK },
   {SMBsplopen,"SMBsplopen",reply_printopen,AS_USER | QUEUE_IN_OPLOCK },
   {SMBsplclose,"SMBsplclose",reply_printclose,AS_USER},
   {SMBsplretq,"SMBsplretq",reply_printqueue,AS_USER|AS_GUEST},
   {SMBsplwr,"SMBsplwr",reply_printwrite,AS_USER},
   {SMBlock,"SMBlock",reply_lock,AS_USER},
   {SMBunlock,"SMBunlock",reply_unlock,AS_USER},
   
   /* CORE+ PROTOCOL FOLLOWS */
   
   {SMBreadbraw,"SMBreadbraw",reply_readbraw,AS_USER},
   {SMBwritebraw,"SMBwritebraw",reply_writebraw,AS_USER},
   {SMBwriteclose,"SMBwriteclose",reply_writeclose,AS_USER},
   {SMBlockread,"SMBlockread",reply_lockread,AS_USER},
   {SMBwriteunlock,"SMBwriteunlock",reply_writeunlock,AS_USER},
   
   /* LANMAN1.0 PROTOCOL FOLLOWS */
   
   {SMBreadBmpx,"SMBreadBmpx",reply_readbmpx,AS_USER},
   {SMBreadBs,"SMBreadBs",NULL,AS_USER},
   {SMBwriteBmpx,"SMBwriteBmpx",reply_writebmpx,AS_USER},
   {SMBwriteBs,"SMBwriteBs",reply_writebs,AS_USER},
   {SMBwritec,"SMBwritec",NULL,AS_USER},
   {SMBsetattrE,"SMBsetattrE",reply_setattrE,AS_USER | NEED_WRITE},
   {SMBgetattrE,"SMBgetattrE",reply_getattrE,AS_USER},
   {SMBtrans,"SMBtrans",reply_trans,AS_USER | CAN_IPC},
   {SMBtranss,"SMBtranss",NULL,AS_USER | CAN_IPC},
   {SMBioctls,"SMBioctls",NULL,AS_USER},
   {SMBcopy,"SMBcopy",reply_copy,AS_USER | NEED_WRITE | QUEUE_IN_OPLOCK },
   {SMBmove,"SMBmove",NULL,AS_USER | NEED_WRITE | QUEUE_IN_OPLOCK },
   
   {SMBopenX,"SMBopenX",reply_open_and_X,AS_USER | CAN_IPC | QUEUE_IN_OPLOCK },
   {SMBreadX,"SMBreadX",reply_read_and_X,AS_USER | CAN_IPC },
   {SMBwriteX,"SMBwriteX",reply_write_and_X,AS_USER},
   {SMBlockingX,"SMBlockingX",reply_lockingX,AS_USER},
   
   {SMBffirst,"SMBffirst",reply_search,AS_USER},
   {SMBfunique,"SMBfunique",reply_search,AS_USER},
   {SMBfclose,"SMBfclose",reply_fclose,AS_USER},

   /* LANMAN2.0 PROTOCOL FOLLOWS */
   {SMBfindnclose, "SMBfindnclose", reply_findnclose, AS_USER},
   {SMBfindclose, "SMBfindclose", reply_findclose,AS_USER},
   {SMBtrans2, "SMBtrans2", reply_trans2, AS_USER },
   {SMBtranss2, "SMBtranss2", reply_transs2, AS_USER},

   /* messaging routines */
   {SMBsends,"SMBsends",reply_sends,AS_GUEST},
   {SMBsendstrt,"SMBsendstrt",reply_sendstrt,AS_GUEST},
   {SMBsendend,"SMBsendend",reply_sendend,AS_GUEST},
   {SMBsendtxt,"SMBsendtxt",reply_sendtxt,AS_GUEST},

   /* NON-IMPLEMENTED PARTS OF THE CORE PROTOCOL */
   
   {SMBsendb,"SMBsendb",NULL,AS_GUEST},
   {SMBfwdname,"SMBfwdname",NULL,AS_GUEST},
   {SMBcancelf,"SMBcancelf",NULL,AS_GUEST},
   {SMBgetmac,"SMBgetmac",NULL,AS_GUEST}
 };

/****************************************************************************
return a string containing the function name of a SMB command
****************************************************************************/
char *smb_fn_name(int type)
{
  static char *unknown_name = "SMBunknown";
  static int num_smb_messages = 
    sizeof(smb_messages) / sizeof(struct smb_message_struct);
  int match;

  for (match=0;match<num_smb_messages;match++)
    if (smb_messages[match].code == type)
      break;

  if (match == num_smb_messages)
    return(unknown_name);

  return(smb_messages[match].name);
}


/****************************************************************************
do a switch on the message type, and return the response size
****************************************************************************/
static int switch_message(int type,char *inbuf,char *outbuf,int size,int bufsize)
{
  static int pid= -1;
  int outsize = 0;
  static int num_smb_messages = 
    sizeof(smb_messages) / sizeof(struct smb_message_struct);
  int match;

#if PROFILING
  struct timeval msg_start_time;
  struct timeval msg_end_time;
  static unsigned long total_time = 0;

  GetTimeOfDay(&msg_start_time);
#endif

  if (pid == -1)
    pid = getpid();

  errno = 0;
  last_message = type;

  /* make sure this is an SMB packet */
  if (strncmp(smb_base(inbuf),"\377SMB",4) != 0)
    {
      DEBUG(2,("Non-SMB packet of length %d\n",smb_len(inbuf)));
      return(-1);
    }

  for (match=0;match<num_smb_messages;match++)
    if (smb_messages[match].code == type)
      break;

  if (match == num_smb_messages)
    {
      DEBUG(0,("Unknown message type %d!\n",type));
      outsize = reply_unknown(inbuf,outbuf);
    }
  else
    {
      DEBUG(3,("switch message %s (pid %d)\n",smb_messages[match].name,pid));

      if(global_oplock_break && (smb_messages[match].flags & QUEUE_IN_OPLOCK))
      {
        /* 
         * Queue this message as we are the process of an oplock break.
         */

        DEBUG(2,("%s: switch_message: queueing message due to being in oplock break state.\n",
               timestring() ));

        push_smb_message( inbuf, size);
        return -1;
      }          

      if (smb_messages[match].fn)
	{
	  int cnum = SVAL(inbuf,smb_tid);
	  int flags = smb_messages[match].flags;
          static uint16 last_session_tag = UID_FIELD_INVALID;
          /* In share mode security we must ignore the vuid. */
	  uint16 session_tag = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID : SVAL(inbuf,smb_uid);
          /* Ensure this value is replaced in the incoming packet. */
          SSVAL(inbuf,smb_uid,session_tag);

          /*
           * Ensure the correct username is in sesssetup_user.
           * This is a really ugly bugfix for problems with
           * multiple session_setup_and_X's being done and
           * allowing %U and %G substitutions to work correctly.
           * There is a reason this code is done here, don't
           * move it unless you know what you're doing... :-).
           * JRA.
           */
          if(session_tag != last_session_tag ) {
            user_struct *vuser = NULL;

            last_session_tag = session_tag;
            if(session_tag != UID_FIELD_INVALID)
              vuser = get_valid_user_struct(session_tag);           
            if(vuser != NULL)
              pstrcpy( sesssetup_user, vuser->requested_name);
          }

	  /* does this protocol need to be run as root? */
	  if (!(flags & AS_USER))
	    unbecome_user();

	  /* does this protocol need to be run as the connected user? */
	  if ((flags & AS_USER) && !become_user(&Connections[cnum], cnum,session_tag)) {
	    if (flags & AS_GUEST) 
	      flags &= ~AS_USER;
	    else
	      return(ERROR(ERRSRV,ERRinvnid));
	  }
	  /* this code is to work around a bug is MS client 3 without
	     introducing a security hole - it needs to be able to do
	     print queue checks as guest if it isn't logged in properly */
	  if (flags & AS_USER)
	    flags &= ~AS_GUEST;

	  /* does it need write permission? */
	  if ((flags & NEED_WRITE) && !CAN_WRITE(cnum))
	    return(ERROR(ERRSRV,ERRaccess));

	  /* ipc services are limited */
	  if (IS_IPC(cnum) && (flags & AS_USER) && !(flags & CAN_IPC))
	    return(ERROR(ERRSRV,ERRaccess));	    

	  /* load service specific parameters */
	  if (OPEN_CNUM(cnum) && !become_service(cnum,(flags & AS_USER)?True:False))
	    return(ERROR(ERRSRV,ERRaccess));

	  /* does this protocol need to be run as guest? */
	  if ((flags & AS_GUEST) && (!become_guest() || !check_access(-1)))
	    return(ERROR(ERRSRV,ERRaccess));

	  last_inbuf = inbuf;

	  outsize = smb_messages[match].fn(inbuf,outbuf,size,bufsize);
	}
      else
	{
	  outsize = reply_unknown(inbuf,outbuf);
	}
    }

#if PROFILING
  GetTimeOfDay(&msg_end_time);
  if (!(smb_messages[match].flags & TIME_INIT))
    {
      smb_messages[match].time = 0;
      smb_messages[match].flags |= TIME_INIT;
    }
  {
    unsigned long this_time =     
      (msg_end_time.tv_sec - msg_start_time.tv_sec)*1e6 +
	(msg_end_time.tv_usec - msg_start_time.tv_usec);
    smb_messages[match].time += this_time;
    total_time += this_time;
  }
  DEBUG(2,("TIME %s  %d usecs   %g pct\n",
	   smb_fn_name(type),smb_messages[match].time,
	(100.0*smb_messages[match].time) / total_time));
#endif

  return(outsize);
}


/****************************************************************************
  construct a chained reply and add it to the already made reply
  **************************************************************************/
int chain_reply(char *inbuf,char *outbuf,int size,int bufsize)
{
  static char *orig_inbuf;
  static char *orig_outbuf;
  int smb_com1, smb_com2 = CVAL(inbuf,smb_vwv0);
  unsigned smb_off2 = SVAL(inbuf,smb_vwv1);
  char *inbuf2, *outbuf2;
  int outsize2;
  char inbuf_saved[smb_wct];
  char outbuf_saved[smb_wct];
  extern int chain_size;
  int wct = CVAL(outbuf,smb_wct);
  int outsize = smb_size + 2*wct + SVAL(outbuf,smb_vwv0+2*wct);

  /* maybe its not chained */
  if (smb_com2 == 0xFF) {
    CVAL(outbuf,smb_vwv0) = 0xFF;
    return outsize;
  }

  if (chain_size == 0) {
    /* this is the first part of the chain */
    orig_inbuf = inbuf;
    orig_outbuf = outbuf;
  }

  /* we need to tell the client where the next part of the reply will be */
  SSVAL(outbuf,smb_vwv1,smb_offset(outbuf+outsize,outbuf));
  CVAL(outbuf,smb_vwv0) = smb_com2;

  /* remember how much the caller added to the chain, only counting stuff
     after the parameter words */
  chain_size += outsize - smb_wct;

  /* work out pointers into the original packets. The
     headers on these need to be filled in */
  inbuf2 = orig_inbuf + smb_off2 + 4 - smb_wct;
  outbuf2 = orig_outbuf + SVAL(outbuf,smb_vwv1) + 4 - smb_wct;

  /* remember the original command type */
  smb_com1 = CVAL(orig_inbuf,smb_com);

  /* save the data which will be overwritten by the new headers */
  memcpy(inbuf_saved,inbuf2,smb_wct);
  memcpy(outbuf_saved,outbuf2,smb_wct);

  /* give the new packet the same header as the last part of the SMB */
  memmove(inbuf2,inbuf,smb_wct);

  /* create the in buffer */
  CVAL(inbuf2,smb_com) = smb_com2;

  /* create the out buffer */
  bzero(outbuf2,smb_size);
  set_message(outbuf2,0,0,True);
  CVAL(outbuf2,smb_com) = CVAL(inbuf2,smb_com);
  
  memcpy(outbuf2+4,inbuf2+4,4);
  CVAL(outbuf2,smb_rcls) = SMB_SUCCESS;
  CVAL(outbuf2,smb_reh) = 0;
  CVAL(outbuf2,smb_flg) = 0x80 | (CVAL(inbuf2,smb_flg) & 0x8); /* bit 7 set 
								  means a reply */
  SSVAL(outbuf2,smb_flg2,1); /* say we support long filenames */
  SSVAL(outbuf2,smb_err,SMB_SUCCESS);
  SSVAL(outbuf2,smb_tid,SVAL(inbuf2,smb_tid));
  SSVAL(outbuf2,smb_pid,SVAL(inbuf2,smb_pid));
  SSVAL(outbuf2,smb_uid,SVAL(inbuf2,smb_uid));
  SSVAL(outbuf2,smb_mid,SVAL(inbuf2,smb_mid));

  DEBUG(3,("Chained message\n"));
  show_msg(inbuf2);

  /* process the request */
  outsize2 = switch_message(smb_com2,inbuf2,outbuf2,size-chain_size,
			    bufsize-chain_size);

  /* copy the new reply and request headers over the old ones, but
     preserve the smb_com field */
  memmove(orig_outbuf,outbuf2,smb_wct);
  CVAL(orig_outbuf,smb_com) = smb_com1;

  /* restore the saved data, being careful not to overwrite any
   data from the reply header */
  memcpy(inbuf2,inbuf_saved,smb_wct);
  {
    int ofs = smb_wct - PTR_DIFF(outbuf2,orig_outbuf);
    if (ofs < 0) ofs = 0;
    memmove(outbuf2+ofs,outbuf_saved+ofs,smb_wct-ofs);
  }

  return outsize2;
}



/****************************************************************************
  construct a reply to the incoming packet
****************************************************************************/
int construct_reply(char *inbuf,char *outbuf,int size,int bufsize)
{
  int type = CVAL(inbuf,smb_com);
  int outsize = 0;
  int msg_type = CVAL(inbuf,0);
  extern int chain_size;

  smb_last_time = time(NULL);

  chain_size = 0;
  chain_fnum = -1;
  reset_chain_pnum();

  bzero(outbuf,smb_size);

  if (msg_type != 0)
    return(reply_special(inbuf,outbuf));  

  CVAL(outbuf,smb_com) = CVAL(inbuf,smb_com);
  set_message(outbuf,0,0,True);
  
  memcpy(outbuf+4,inbuf+4,4);
  CVAL(outbuf,smb_rcls) = SMB_SUCCESS;
  CVAL(outbuf,smb_reh) = 0;
  CVAL(outbuf,smb_flg) = 0x80 | (CVAL(inbuf,smb_flg) & 0x8); /* bit 7 set 
							     means a reply */
  SSVAL(outbuf,smb_flg2,1); /* say we support long filenames */
  SSVAL(outbuf,smb_err,SMB_SUCCESS);
  SSVAL(outbuf,smb_tid,SVAL(inbuf,smb_tid));
  SSVAL(outbuf,smb_pid,SVAL(inbuf,smb_pid));
  SSVAL(outbuf,smb_uid,SVAL(inbuf,smb_uid));
  SSVAL(outbuf,smb_mid,SVAL(inbuf,smb_mid));

  outsize = switch_message(type,inbuf,outbuf,size,bufsize);

  outsize += chain_size;

  if(outsize > 4)
    smb_setlen(outbuf,outsize - 4);
  return(outsize);
}

/****************************************************************************
  process commands from the client
****************************************************************************/
static void process(void)
{
  extern int Client;

  InBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
  OutBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
  if ((InBuffer == NULL) || (OutBuffer == NULL)) 
    return;

  InBuffer += SMB_ALIGNMENT;
  OutBuffer += SMB_ALIGNMENT;

#if PRIME_NMBD
  DEBUG(3,("priming nmbd\n"));
  {
    struct in_addr ip;
    ip = *interpret_addr2("localhost");
    if (zero_ip(ip)) ip = *interpret_addr2("127.0.0.1");
    *OutBuffer = 0;
    send_one_packet(OutBuffer,1,ip,NMB_PORT,SOCK_DGRAM);
  }
#endif    

  /* re-initialise the timezone */
  TimeInit();

  while (True)
  {
    int deadtime = lp_deadtime()*60;
    int counter;
    int last_keepalive=0;
    int service_load_counter = 0;
    BOOL got_smb = False;

    if (deadtime <= 0)
      deadtime = DEFAULT_SMBD_TIMEOUT;

#if USE_READ_PREDICTION
    if (lp_readprediction())
      do_read_prediction();
#endif

    errno = 0;      

    for (counter=SMBD_SELECT_LOOP; 
          !receive_message_or_smb(Client,oplock_sock,
                      InBuffer,BUFFER_SIZE,SMBD_SELECT_LOOP*1000,&got_smb); 
          counter += SMBD_SELECT_LOOP)
    {
      int i;
      time_t t;
      BOOL allidle = True;
      extern int keepalive;

      if (counter > 365 * 3600) /* big number of seconds. */
      {
        counter = 0;
        service_load_counter = 0;
      }

      if (smb_read_error == READ_EOF) 
      {
        DEBUG(3,("end of file from client\n"));
        return;
      }

      if (smb_read_error == READ_ERROR) 
      {
        DEBUG(3,("receive_smb error (%s) exiting\n",
                  strerror(errno)));
        return;
      }

      t = time(NULL);

      /* become root again if waiting */
      unbecome_user();

      /* check for smb.conf reload */
      if (counter >= service_load_counter + SMBD_RELOAD_CHECK)
      {
        service_load_counter = counter;

        /* reload services, if files have changed. */
        reload_services(True);
      }

      /* automatic timeout if all connections are closed */      
      if (num_connections_open==0 && counter >= IDLE_CLOSED_TIMEOUT) 
      {
        DEBUG(2,("%s Closing idle connection\n",timestring()));
        return;
      }

      if (keepalive && (counter-last_keepalive)>keepalive) 
      {
	      struct cli_state *cli = server_client();
	      if (!send_keepalive(Client)) { 
		      DEBUG(2,("%s Keepalive failed - exiting\n",timestring()));
		      return;
	      }	    
	      /* also send a keepalive to the password server if its still
		 connected */
	      if (cli && cli->initialised)
		      send_keepalive(cli->fd);
	      last_keepalive = counter;
      }

      /* check for connection timeouts */
      for (i=0;i<MAX_CONNECTIONS;i++)
        if (Connections[i].open)
        {
          /* close dirptrs on connections that are idle */
          if ((t-Connections[i].lastused)>DPTR_IDLE_TIMEOUT)
            dptr_idlecnum(i);

          if (Connections[i].num_files_open > 0 ||
                     (t-Connections[i].lastused)<deadtime)
            allidle = False;
        }

      if (allidle && num_connections_open>0) 
      {
        DEBUG(2,("%s Closing idle connection 2\n",timestring()));
        return;
      }
    }

    if(got_smb)
      process_smb(InBuffer, OutBuffer);
    else
      process_local_message(oplock_sock, InBuffer, BUFFER_SIZE);
  }
}


/****************************************************************************
  initialise connect, service and file structs
****************************************************************************/
static void init_structs(void )
{
  int i;
  get_myname(myhostname,NULL);

  /*
   * Set the machine NETBIOS name if not already
   * set from the config file.
   */

  if (!*global_myname)
  {
    char *p;
    fstrcpy( global_myname, myhostname );
    p = strchr( global_myname, '.' );
    if (p) 
      *p = 0;
  }
  strupper( global_myname );

  for (i=0;i<MAX_CONNECTIONS;i++)
    {
      Connections[i].open = False;
      Connections[i].num_files_open=0;
      Connections[i].lastused=0;
      Connections[i].used=False;
      string_init(&Connections[i].user,"");
      string_init(&Connections[i].dirpath,"");
      string_init(&Connections[i].connectpath,"");
      string_init(&Connections[i].origpath,"");
    }

  for (i=0;i<MAX_OPEN_FILES;i++)
    {
      Files[i].open = False;
      string_init(&Files[i].name,"");
    }

  for (i=0;i<MAX_OPEN_FILES;i++)
    {
      file_fd_struct *fd_ptr = &FileFd[i];
      fd_ptr->ref_count = 0;
      fd_ptr->dev = (int32)-1;
      fd_ptr->inode = (int32)-1;
      fd_ptr->fd = -1;
      fd_ptr->fd_readonly = -1;
      fd_ptr->fd_writeonly = -1;
      fd_ptr->real_open_flags = -1;
    }

  /* for RPC pipes */
  init_rpc_pipe_hnd();

  /* for LSA handles */
  init_lsa_policy_hnd();

  init_dptrs();
}

/****************************************************************************
usage on the program
****************************************************************************/
static void usage(char *pname)
{
  DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));

  printf("Usage: %s [-D] [-p port] [-d debuglevel] [-l log basename] [-s services file]\n",pname);
  printf("Version %s\n",VERSION);
  printf("\t-D                    become a daemon\n");
  printf("\t-p port               listen on the specified port\n");
  printf("\t-d debuglevel         set the debuglevel\n");
  printf("\t-l log basename.      Basename for log/debug files\n");
  printf("\t-s services file.     Filename of services file\n");
  printf("\t-P                    passive only\n");
  printf("\t-a                    overwrite log file, don't append\n");
  printf("\n");
}


/****************************************************************************
  main program
****************************************************************************/
 int main(int argc,char *argv[])
{
  extern BOOL append_log;
  /* shall I run as a daemon */
  BOOL is_daemon = False;
  int port = SMB_PORT;
  int opt;
  extern char *optarg;

#ifdef NEED_AUTH_PARAMETERS
  set_auth_parameters(argc,argv);
#endif

#ifdef SecureWare
  setluid(0);
#endif

  append_log = True;

  TimeInit();

  pstrcpy(debugf,SMBLOGFILE);  

  pstrcpy(remote_machine, "smb");

  setup_logging(argv[0],False);

  charset_initialise();

  /* make absolutely sure we run as root - to handle cases where people
     are crazy enough to have it setuid */
#ifdef USE_SETRES
  setresuid(0,0,0);
#else
  setuid(0);
  seteuid(0);
  setuid(0);
  seteuid(0);
#endif

  fault_setup((void (*)(void *))exit_server);
  signal(SIGTERM , SIGNAL_CAST dflt_sig);

  /* we want total control over the permissions on created files,
     so set our umask to 0 */
  umask(0);

  GetWd(OriginalDir);

  init_uid();

  /* this is for people who can't start the program correctly */
  while (argc > 1 && (*argv[1] != '-'))
    {
      argv++;
      argc--;
    }

  while ((opt = getopt(argc, argv, "O:i:l:s:d:Dp:hPaf:")) != EOF)
    switch (opt)
      {
      case 'O':
	pstrcpy(user_socket_options,optarg);
	break;
      case 'i':
	pstrcpy(scope,optarg);
	break;
      case 'P':
	{
	  extern BOOL passive;
	  passive = True;
	}
	break;	
      case 's':
	pstrcpy(servicesf,optarg);
	break;
      case 'l':
	pstrcpy(debugf,optarg);
	break;
      case 'a':
	{
	  extern BOOL append_log;
	  append_log = !append_log;
	}
	break;
      case 'D':
	is_daemon = True;
	break;
      case 'd':
	if (*optarg == 'A')
	  DEBUGLEVEL = 10000;
	else
	  DEBUGLEVEL = atoi(optarg);
	break;
      case 'p':
	port = atoi(optarg);
	break;
      case 'h':
	usage(argv[0]);
	exit(0);
	break;
      default:
	usage(argv[0]);
	exit(1);
      }

  reopen_logs();

  DEBUG(2,("%s smbd version %s started\n",timestring(),VERSION));
  DEBUG(2,("Copyright Andrew Tridgell 1992-1997\n"));

#ifndef NO_GETRLIMIT
#ifdef RLIMIT_NOFILE
  {
    struct rlimit rlp;
    getrlimit(RLIMIT_NOFILE, &rlp);
    /*
     * Set the fd limit to be MAX_OPEN_FILES + 10 to account for the
     * extra fd we need to read directories, as well as the log files
     * and standard handles etc.
     */
    rlp.rlim_cur = (MAX_OPEN_FILES+10>rlp.rlim_max)? rlp.rlim_max:MAX_OPEN_FILES+10;
    setrlimit(RLIMIT_NOFILE, &rlp);
    getrlimit(RLIMIT_NOFILE, &rlp);
    DEBUG(3,("Maximum number of open files per session is %d\n",rlp.rlim_cur));
  }
#endif
#endif

  
  DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
	getuid(),getgid(),geteuid(),getegid()));

  if (sizeof(uint16) < 2 || sizeof(uint32) < 4)
    {
      DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
      exit(1);
    }

  init_structs();

  if (!reload_services(False))
    return(-1);	

  codepage_initialise(lp_client_code_page());

  pstrcpy(global_myworkgroup, lp_workgroup());

  if(!pdb_generate_machine_sid())
  {
    DEBUG(0,("ERROR: Samba cannot get a machine SID.\n"));
    exit(1);
  }

#ifndef NO_SIGNAL_TEST
  signal(SIGHUP,SIGNAL_CAST sig_hup);
#endif

  /* Setup the signals that allow the debug log level
     to by dynamically changed. */
 
  /* If we are using the malloc debug code we can't use
     SIGUSR1 and SIGUSR2 to do debug level changes. */

#ifndef MEM_MAN
#if defined(SIGUSR1)
  signal( SIGUSR1, SIGNAL_CAST sig_usr1 );
#endif /* SIGUSR1 */
   
#if defined(SIGUSR2)
  signal( SIGUSR2, SIGNAL_CAST sig_usr2 );
#endif /* SIGUSR2 */
#endif /* MEM_MAN */

  DEBUG(3,("%s loaded services\n",timestring()));

  if (!is_daemon && !is_a_socket(0))
    {
      DEBUG(0,("standard input is not a socket, assuming -D option\n"));
      is_daemon = True;
    }

  if (is_daemon)
    {
      DEBUG(3,("%s becoming a daemon\n",timestring()));
      become_daemon();
    }

  if (!directory_exist(lp_lockdir(), NULL)) {
	  mkdir(lp_lockdir(), 0755);
  }

  if (is_daemon) {
	  pidfile_create("smbd");
  }

  if (!open_sockets(is_daemon,port))
    exit(1);

  if (!locking_init(0))
    exit(1);

  /* possibly reload the services file. */
  reload_services(True);

  max_recv = MIN(lp_maxxmit(),BUFFER_SIZE);

  if (*lp_rootdir())
    {
      if (sys_chroot(lp_rootdir()) == 0)
	DEBUG(2,("%s changed root to %s\n",timestring(),lp_rootdir()));
    }

  /* Setup the oplock IPC socket. */
  if(!open_oplock_ipc())
    exit(1);

  process();
  close_sockets();

  exit_server("normal exit");
  return(0);
}