summaryrefslogtreecommitdiff
path: root/source3/rpc_server/srv_srvsvc_nt.c
blob: 7132f92c9363008d87a58e575b5a1d2bfd7991b8 (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
/* 
 *  Unix SMB/CIFS implementation.
 *  RPC Pipe client / server routines
 *  Copyright (C) Andrew Tridgell              1992-1997,
 *  Copyright (C) Jeremy Allison               2001.
 *  Copyright (C) Nigel Williams               2001.
 *  Copyright (C) Gerald (Jerry) Carter        2006.
 *  Copyright (C) Jelmer Vernooij			   2006.
 *  
 *  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.
 */

/* This is the implementation of the srvsvc pipe. */

#include "includes.h"

#define MAX_SERVER_DISK_ENTRIES 15

extern struct generic_mapping file_generic_mapping;
extern userdom_struct current_user_info;

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_RPC_SRV

/* Use for enumerating connections, pipes, & files */

struct file_enum_count {
	TALLOC_CTX *ctx;
	uint32 count;
	struct srvsvc_NetFileInfo3 *info;
};

struct sess_file_count {
	struct server_id pid;
	uid_t uid;
	int count;
};

/****************************************************************************
 Count the entries belonging to a service in the connection db.
****************************************************************************/

static int pipe_enum_fn( struct db_record *rec, void *p)
{
	struct pipe_open_rec prec;
	struct file_enum_count *fenum = (struct file_enum_count *)p;
 
	if (rec->value.dsize != sizeof(struct pipe_open_rec))
		return 0;

	memcpy(&prec, rec->value.dptr, sizeof(struct pipe_open_rec));
 
	if ( process_exists(prec.pid) ) {
		struct srvsvc_NetFileInfo3 *f;
		int i = fenum->count;
		pstring fullpath;
		
		snprintf( fullpath, sizeof(fullpath), "\\PIPE\\%s", prec.name );
		
		f = TALLOC_REALLOC_ARRAY( fenum->ctx, fenum->info, struct srvsvc_NetFileInfo3, i+1 );
		if ( !f ) {
			DEBUG(0,("conn_enum_fn: realloc failed for %d items\n", i+1));
			return 1;
		}

		fenum->info = f;
		
		fenum->info[i].fid = (uint32)((procid_to_pid(&prec.pid)<<16) & prec.pnum);
		fenum->info[i].permissions = (FILE_READ_DATA|FILE_WRITE_DATA);
		fenum->info[i].num_locks = 0;
		if (!(fenum->info[i].user = talloc_strdup(
			      fenum->ctx, uidtoname(prec.uid)))) {
			/* There's not much we can do here. */
			fenum->info[i].user = "";
		}
		if (!(fenum->info[i].path = talloc_strdup(
			      fenum->ctx, fullpath))) {
			/* There's not much we can do here. */
			fenum->info[i].path = "";
		}
			
		fenum->count++;
	}

	return 0;
}

/*******************************************************************
********************************************************************/

static WERROR net_enum_pipes( TALLOC_CTX *ctx, struct srvsvc_NetFileInfo3 **info, 
                              uint32 *count, uint32 *resume )
{
	struct file_enum_count fenum;

	fenum.ctx = ctx;
	fenum.info = *info;
	fenum.count = *count;

	if (connections_traverse(pipe_enum_fn, &fenum) == -1) {
		DEBUG(0,("net_enum_pipes: traverse of connections.tdb "
			 "failed\n"));
		return WERR_NOMEM;
	}
	
	*info  = fenum.info;
	*count = fenum.count;
	
	return WERR_OK;
}

/*******************************************************************
********************************************************************/

static void enum_file_fn( const struct share_mode_entry *e, 
                          const char *sharepath, const char *fname,
			  void *private_data )
{
	struct file_enum_count *fenum =
		(struct file_enum_count *)&private_data;
 
	/* If the pid was not found delete the entry from connections.tdb */

	if ( process_exists(e->pid) ) {
		struct srvsvc_NetFileInfo3 *f;
		int i = fenum->count;
		files_struct fsp;
		struct byte_range_lock *brl;
		int num_locks = 0;
		pstring fullpath;
		uint32 permissions;
		
		f = TALLOC_REALLOC_ARRAY( fenum->ctx, fenum->info, struct srvsvc_NetFileInfo3, i+1 );			
		if ( !f ) {
			DEBUG(0,("conn_enum_fn: realloc failed for %d items\n", i+1));
			return;
		}
		fenum->info = f;

		/* need to count the number of locks on a file */
		
		ZERO_STRUCT( fsp );		
		fsp.file_id = e->id;
		
		if ( (brl = brl_get_locks_readonly(NULL,&fsp)) != NULL ) {
			num_locks = brl->num_locks;
			TALLOC_FREE( brl );
		}
		
		if ( strcmp( fname, "." ) == 0 ) {
			pstr_sprintf( fullpath, "C:%s", sharepath );
		} else {
			pstr_sprintf( fullpath, "C:%s/%s", sharepath, fname );
		}
		string_replace( fullpath, '/', '\\' );
		
		/* mask out create (what ever that is) */
		permissions = e->share_access & (FILE_READ_DATA|FILE_WRITE_DATA);

		fenum->info[i].fid = e->share_file_id;
		fenum->info[i].permissions = permissions;
		fenum->info[i].num_locks = num_locks;
		if (!(fenum->info[i].user = talloc_strdup(
			      fenum->ctx, uidtoname(e->uid)))) {
			/* There's not much we can do here. */
			fenum->info[i].user = "";
		}
		if (!(fenum->info[i].path = talloc_strdup(
			      fenum->ctx, fullpath))) {
			/* There's not much we can do here. */
			fenum->info[i].path = "";
		}
			
		fenum->count++;
	}

	return;

}

/*******************************************************************
********************************************************************/

static WERROR net_enum_files( TALLOC_CTX *ctx, struct srvsvc_NetFileInfo3 **info, 
                              uint32 *count, uint32 *resume )
{
	struct file_enum_count f_enum_cnt;

	f_enum_cnt.ctx = ctx;
	f_enum_cnt.count = *count;
	f_enum_cnt.info = *info;
	
	share_mode_forall( enum_file_fn, (void *)&f_enum_cnt );
	
	*info  = f_enum_cnt.info;
	*count = f_enum_cnt.count;
	
	return WERR_OK;
}

/*******************************************************************
 Utility function to get the 'type' of a share from a share definition.
 ********************************************************************/
static uint32 get_share_type(const struct share_params *params)
{
	char *net_name = lp_servicename(params->service);
	int len_net_name = strlen(net_name);
	
	/* work out the share type */
	uint32 type = STYPE_DISKTREE;

	if (lp_print_ok(params->service))
		type = STYPE_PRINTQ;
	if (strequal(lp_fstype(params->service), "IPC"))
		type = STYPE_IPC;
	if (net_name[len_net_name-1] == '$')
		type |= STYPE_HIDDEN;

	return type;
}
	
/*******************************************************************
 Fill in a share info level 0 structure.
 ********************************************************************/

static void init_srv_share_info_0(pipes_struct *p, struct srvsvc_NetShareInfo0 *sh0,
				  const struct share_params *params)
{
	sh0->name = lp_servicename(params->service);
}

/*******************************************************************
 Fill in a share info level 1 structure.
 ********************************************************************/

static void init_srv_share_info_1(pipes_struct *p, struct srvsvc_NetShareInfo1 *sh1,
				  const struct share_params *params)
{
	connection_struct *conn = p->conn;

	sh1->comment = talloc_sub_advanced(p->mem_ctx, lp_servicename(SNUM(conn)),
				     conn->user, conn->connectpath, conn->gid,
				     get_current_username(),
				     current_user_info.domain,
				     lp_comment(params->service));

	sh1->name = lp_servicename(params->service);
	sh1->type = get_share_type(params);
}

/*******************************************************************
 Fill in a share info level 2 structure.
 ********************************************************************/

static void init_srv_share_info_2(pipes_struct *p, struct srvsvc_NetShareInfo2 *sh2,
				  const struct share_params *params)
{
	connection_struct *conn = p->conn;
	char *remark;
	char *path;
	int max_connections = lp_max_connections(params->service);
	uint32 max_uses = max_connections!=0 ? max_connections : 0xffffffff;
	int count = 0;
	char *net_name = lp_servicename(params->service);
	
	remark = talloc_sub_advanced(p->mem_ctx, lp_servicename(SNUM(conn)),
				     conn->user, conn->connectpath, conn->gid,
				     get_current_username(),
				     current_user_info.domain,
				     lp_comment(params->service));
	path = talloc_asprintf(p->mem_ctx, "C:%s",
			       lp_pathname(params->service));

	/*
	 * Change / to \\ so that win2k will see it as a valid path.  This was
	 * added to enable use of browsing in win2k add share dialog.
	 */ 

	string_replace(path, '/', '\\');

	count = count_current_connections( net_name, False  );
	sh2->name = net_name;
	sh2->type = get_share_type(params);
	sh2->comment = remark;
	sh2->permissions = 0;
	sh2->max_users = max_uses;
	sh2->current_users = count;
	sh2->path = path;
	sh2->password = "";
}

/*******************************************************************
 Map any generic bits to file specific bits.
********************************************************************/

static void map_generic_share_sd_bits(SEC_DESC *psd)
{
	int i;
	SEC_ACL *ps_dacl = NULL;

	if (!psd)
		return;

	ps_dacl = psd->dacl;
	if (!ps_dacl)
		return;

	for (i = 0; i < ps_dacl->num_aces; i++) {
		SEC_ACE *psa = &ps_dacl->aces[i];
		uint32 orig_mask = psa->access_mask;

		se_map_generic(&psa->access_mask, &file_generic_mapping);
		psa->access_mask |= orig_mask;
	}	
}

/*******************************************************************
 Fill in a share info level 501 structure.
********************************************************************/

static void init_srv_share_info_501(pipes_struct *p, struct srvsvc_NetShareInfo501 *sh501,
				    const struct share_params *params)
{
	connection_struct *conn = p->conn;
	char *remark;
	const char *net_name = lp_servicename(params->service);

	remark = talloc_sub_advanced(p->mem_ctx, lp_servicename(SNUM(conn)),
				     conn->user, conn->connectpath, conn->gid,
				     get_current_username(),
				     current_user_info.domain,
				     lp_comment(params->service));


	sh501->name = net_name;
	sh501->type = get_share_type(params);
	sh501->comment = remark;
	sh501->csc_policy = (lp_csc_policy(params->service) << 4);
}

/*******************************************************************
 Fill in a share info level 502 structure.
 ********************************************************************/

static void init_srv_share_info_502(pipes_struct *p, struct srvsvc_NetShareInfo502 *sh502,
				    const struct share_params *params)
{
	int max_connections = lp_max_connections(params->service);
	uint32 max_uses = max_connections!=0 ? max_connections : 0xffffffff;
	connection_struct *conn = p->conn;
	int count; 
	char *net_name;
	char *remark;
	char *path;
	SEC_DESC *sd;
	size_t sd_size;
	TALLOC_CTX *ctx = p->mem_ctx;


	ZERO_STRUCTP(sh502);

	net_name = lp_servicename(params->service);
	count = count_current_connections( net_name, False  );

	remark = talloc_sub_advanced(p->mem_ctx, lp_servicename(SNUM(conn)),
				     conn->user, conn->connectpath, conn->gid,
				     get_current_username(),
				     current_user_info.domain,
				     lp_comment(params->service));

	path = talloc_asprintf(p->mem_ctx, "C:%s",
			       lp_pathname(params->service));

	/*
	 * Change / to \\ so that win2k will see it as a valid path.  This was
	 * added to enable use of browsing in win2k add share dialog.
	 */ 

	string_replace(path, '/', '\\');

	sd = get_share_security(ctx, lp_servicename(params->service),
				&sd_size);

	sh502->name = net_name;
	sh502->type = get_share_type(params);
	sh502->comment = remark;
	sh502->path = path;
	sh502->password = "";
	sh502->sd = sd;
	sh502->permissions = 0;
	sh502->max_users = max_uses;
	sh502->current_users = count;
	sh502->unknown = 1;
}

/***************************************************************************
 Fill in a share info level 1004 structure.
 ***************************************************************************/

static void init_srv_share_info_1004(pipes_struct *p,
				     struct srvsvc_NetShareInfo1004* sh1004,
				     const struct share_params *params)
{
	connection_struct *conn = p->conn;
	char *remark;

	remark = talloc_sub_advanced(p->mem_ctx, lp_servicename(SNUM(conn)),
				     conn->user, conn->connectpath, conn->gid,
				     get_current_username(),
				     current_user_info.domain,
				     lp_comment(params->service));

	ZERO_STRUCTP(sh1004);

	sh1004->comment = remark;
}

/***************************************************************************
 Fill in a share info level 1005 structure.
 ***************************************************************************/

static void init_srv_share_info_1005(pipes_struct *p,
				     struct srvsvc_NetShareInfo1005* sh1005,
				     const struct share_params *params)
{
	sh1005->dfs_flags = 0;

	if(lp_host_msdfs() && lp_msdfs_root(params->service))
		sh1005->dfs_flags |= 
			SHARE_1005_IN_DFS | SHARE_1005_DFS_ROOT;
	sh1005->dfs_flags |= 
		lp_csc_policy(params->service) << SHARE_1005_CSC_POLICY_SHIFT;
}
/***************************************************************************
 Fill in a share info level 1006 structure.
 ***************************************************************************/

static void init_srv_share_info_1006(pipes_struct *p,
				     struct srvsvc_NetShareInfo1006* sh1006,
				     const struct share_params *params)
{
	sh1006->max_users = -1;
}

/***************************************************************************
 Fill in a share info level 1007 structure.
 ***************************************************************************/

static void init_srv_share_info_1007(pipes_struct *p,
				     struct srvsvc_NetShareInfo1007* sh1007,
				     const struct share_params *params)
{
	uint32 flags = 0;

	ZERO_STRUCTP(sh1007);
  
	sh1007->flags = flags;
	sh1007->alternate_directory_name = "";
}

/*******************************************************************
 Fill in a share info level 1501 structure.
 ********************************************************************/

static void init_srv_share_info_1501(pipes_struct *p,
				     struct sec_desc_buf *sh1501,
				     const struct share_params *params)
{
	SEC_DESC *sd;
	size_t sd_size;
	TALLOC_CTX *ctx = p->mem_ctx;

	ZERO_STRUCTP(sh1501);

	sd = get_share_security(ctx, lp_servicename(params->service),
				&sd_size);

	sh1501->sd = sd;
}

/*******************************************************************
 True if it ends in '$'.
 ********************************************************************/

static BOOL is_hidden_share(const struct share_params *params)
{
	const char *net_name = lp_servicename(params->service);

	return (net_name[strlen(net_name) - 1] == '$');
}

/*******************************************************************
 Fill in a share info structure.
 ********************************************************************/

static WERROR init_srv_share_info_ctr(pipes_struct *p,
				      union srvsvc_NetShareCtr *ctr,
				      uint32 info_level, uint32 *resume_hnd,
				      uint32 *total_entries, BOOL all_shares)
{
	TALLOC_CTX *ctx = p->mem_ctx;
	struct share_iterator *shares;
	struct share_params *share;
	WERROR result = WERR_NOMEM;

	DEBUG(5,("init_srv_share_info_ctr\n"));

	ZERO_STRUCTP(ctr);

	if (resume_hnd) {
		*resume_hnd = 0;
	}

	/* Ensure all the usershares are loaded. */
	become_root();
	load_usershare_shares();
	load_registry_shares();
	unbecome_root();

	*total_entries = 0;

	if (!(shares = share_list_all(ctx))) {
		DEBUG(5, ("Could not list shares\n"));
		return WERR_ACCESS_DENIED;
	}

	switch (info_level) {
	case 0:
		if (!(ctr->ctr0 = TALLOC_ZERO_P(
			      p->mem_ctx, struct srvsvc_NetShareCtr0))) {
			goto done;
		}
		break;
	case 1:
		if (!(ctr->ctr1 = TALLOC_ZERO_P(
			      p->mem_ctx, struct srvsvc_NetShareCtr1))) {
			goto done;
		}
		break;
	case 2:
		if (!(ctr->ctr2 = TALLOC_ZERO_P(
			      p->mem_ctx, struct srvsvc_NetShareCtr2))) {
			goto done;
		}
		break;
	case 501:
		if (!(ctr->ctr501 = TALLOC_ZERO_P(
			      p->mem_ctx, struct srvsvc_NetShareCtr501))) {
			goto done;
		}
		break;
	case 502:
		if (!(ctr->ctr502 = TALLOC_ZERO_P(
			      p->mem_ctx, struct srvsvc_NetShareCtr502))) {
			goto done;
		}
		break;
	case 1004:
		if (!(ctr->ctr1004 = TALLOC_ZERO_P(
			      p->mem_ctx, struct srvsvc_NetShareCtr1004))) {
			goto done;
		}
		break;
	case 1005:
		if (!(ctr->ctr1005 = TALLOC_ZERO_P(
			      p->mem_ctx, struct srvsvc_NetShareCtr1005))) {
			goto done;
		}
		break;
	case 1006:
		if (!(ctr->ctr1006 = TALLOC_ZERO_P(
			      p->mem_ctx, struct srvsvc_NetShareCtr1006))) {
			goto done;
		}
		break;
	case 1007:
		if (!(ctr->ctr1007 = TALLOC_ZERO_P(
			      p->mem_ctx, struct srvsvc_NetShareCtr1007))) {
			goto done;
		}
		break;
	case 1501:
		if (!(ctr->ctr1501 = TALLOC_ZERO_P(
			      p->mem_ctx, struct srvsvc_NetShareCtr1501))) {
			goto done;
		}
		break;
	default:
		DEBUG(5,("init_srv_share_info_ctr: unsupported switch "
			 "value %d\n", info_level));
		return WERR_UNKNOWN_LEVEL;
	}

	while ((share = next_share(shares)) != NULL) {
		if (!lp_browseable(share->service)) {
			continue;
		}
		if (!all_shares && is_hidden_share(share)) {
			continue;
		}

		switch (info_level) {
		case 0:
		{
			struct srvsvc_NetShareInfo0 i;
			init_srv_share_info_0(p, &i, share);
			ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo0, i,
				     &ctr->ctr0->array, &ctr->ctr0->count);
			if (ctr->ctr0->array == NULL) {
				return WERR_NOMEM;
			}
			*total_entries = ctr->ctr0->count;
			break;
		}

		case 1:
		{
			struct srvsvc_NetShareInfo1 i;
			init_srv_share_info_1(p, &i, share);
			ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo1, i,
				     &ctr->ctr1->array, &ctr->ctr1->count);
			if (ctr->ctr1->array == NULL) {
				return WERR_NOMEM;
			}
			*total_entries = ctr->ctr1->count;
			break;
		}

		case 2:
		{
			struct srvsvc_NetShareInfo2 i;
			init_srv_share_info_2(p, &i, share);
			ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo2, i,
				     &ctr->ctr2->array, &ctr->ctr2->count);
			if (ctr->ctr2->array == NULL) {
				return WERR_NOMEM;
			}
			*total_entries = ctr->ctr2->count;
			break;
		}

		case 501:
		{
			struct srvsvc_NetShareInfo501 i;
			init_srv_share_info_501(p, &i, share);
			ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo501, i,
				     &ctr->ctr501->array, &ctr->ctr501->count);
			if (ctr->ctr501->array == NULL) {
				return WERR_NOMEM;
			}
			*total_entries = ctr->ctr501->count;
			break;
		}

		case 502:
		{
			struct srvsvc_NetShareInfo502 i;
			init_srv_share_info_502(p, &i, share);
			ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo502, i,
				     &ctr->ctr502->array, &ctr->ctr502->count);
			if (ctr->ctr502->array == NULL) {
				return WERR_NOMEM;
			}
			*total_entries = ctr->ctr502->count;
			break;
		}

		/* here for completeness but not currently used with enum
		 * (1004 - 1501)*/
	
		case 1004:
		{
			struct srvsvc_NetShareInfo1004 i;
			init_srv_share_info_1004(p, &i, share);
			ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo1004, i,
				     &ctr->ctr1004->array, &ctr->ctr1004->count);
			if (ctr->ctr1004->array == NULL) {
				return WERR_NOMEM;
			}
			*total_entries = ctr->ctr1004->count;
			break;
		}

		case 1005:
		{
			struct srvsvc_NetShareInfo1005 i;
			init_srv_share_info_1005(p, &i, share);
			ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo1005, i,
				     &ctr->ctr1005->array, &ctr->ctr1005->count);
			if (ctr->ctr1005->array == NULL) {
				return WERR_NOMEM;
			}
			*total_entries = ctr->ctr1005->count;
			break;
		}

		case 1006:
		{
			struct srvsvc_NetShareInfo1006 i;
			init_srv_share_info_1006(p, &i, share);
			ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo1006, i,
				     &ctr->ctr1006->array, &ctr->ctr1006->count);
			if (ctr->ctr1006->array == NULL) {
				return WERR_NOMEM;
			}
			*total_entries = ctr->ctr1006->count;
			break;
		}

		case 1007:
		{
			struct srvsvc_NetShareInfo1007 i;
			init_srv_share_info_1007(p, &i, share);
			ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo1007, i,
				     &ctr->ctr1007->array, &ctr->ctr1007->count);
			if (ctr->ctr1007->array == NULL) {
				return WERR_NOMEM;
			}
			*total_entries = ctr->ctr1007->count;
			break;
		}

		case 1501:
		{
			struct sec_desc_buf i;
			init_srv_share_info_1501(p, &i, share);
			ADD_TO_ARRAY(ctx, struct sec_desc_buf, i,
				     &ctr->ctr1501->array, &ctr->ctr1501->count);
			if (ctr->ctr1501->array == NULL) {
				return WERR_NOMEM;
			}
			*total_entries = ctr->ctr1501->count;
			break;
		}
		}

		TALLOC_FREE(share);
	}

	result = WERR_OK;
 done:
	TALLOC_FREE(shares);
	return result;
}

/*******************************************************************
 fill in a sess info level 0 structure.
 ********************************************************************/

static void init_srv_sess_info_0(pipes_struct *p, struct srvsvc_NetSessCtr0 *ss0, uint32 *snum, uint32 *stot)
{
	struct sessionid *session_list;
	uint32 num_entries = 0;
	(*stot) = list_sessions(p->mem_ctx, &session_list);

	if (ss0 == NULL) {
		if (snum) {
			(*snum) = 0;
		}
		return;
	}

	DEBUG(5,("init_srv_sess_0_ss0\n"));

	ss0->array = TALLOC_ARRAY(p->mem_ctx, struct srvsvc_NetSessInfo0, *stot);

	if (snum) {
		for (; (*snum) < (*stot); (*snum)++) {
			ss0->array[num_entries].client = session_list[(*snum)].remote_machine;
			num_entries++;
		}

		ss0->count = num_entries;
		
		if ((*snum) >= (*stot)) {
			(*snum) = 0;
		}

	} else {
		ss0->array = NULL;
		ss0->count = 0;
	}
}

/*******************************************************************
********************************************************************/

static void sess_file_fn( const struct share_mode_entry *e, 
                          const char *sharepath, const char *fname,
			  void *private_data )
{
	struct sess_file_count *sess = (struct sess_file_count *)private_data;
 
	if ( procid_equal(&e->pid, &sess->pid) && (sess->uid == e->uid) ) {
		sess->count++;
	}
	
	return;
}

/*******************************************************************
********************************************************************/

static int net_count_files( uid_t uid, struct server_id pid )
{
	struct sess_file_count s_file_cnt;

	s_file_cnt.count = 0;
	s_file_cnt.uid = uid;
	s_file_cnt.pid = pid;
	
	share_mode_forall( sess_file_fn, (void *)&s_file_cnt );
	
	return s_file_cnt.count;
}

/*******************************************************************
 fill in a sess info level 1 structure.
 ********************************************************************/

static void init_srv_sess_info_1(pipes_struct *p, struct srvsvc_NetSessCtr1 *ss1, uint32 *snum, uint32 *stot)
{
	struct sessionid *session_list;
	uint32 num_entries = 0;
	time_t now = time(NULL);

	if ( !snum ) {
		ss1->count = 0;
		ss1->array = NULL;
		
		(*stot) = 0;

		return;
	}
	
	if (ss1 == NULL) {
		if (snum != NULL)
			(*snum) = 0;
		return;
	}

	(*stot) = list_sessions(p->mem_ctx, &session_list);

	ss1->array = TALLOC_ARRAY(p->mem_ctx, struct srvsvc_NetSessInfo1, *stot);
	
	for (; (*snum) < (*stot); (*snum)++) {
		uint32 num_files;
		uint32 connect_time;
		struct passwd *pw = sys_getpwnam(session_list[*snum].username);
		BOOL guest;
			
		if ( !pw ) {
			DEBUG(10,("init_srv_sess_info_1: failed to find owner: %s\n",
				session_list[*snum].username));
			continue;
		}
				
		connect_time = (uint32)(now - session_list[*snum].connect_start);
		num_files = net_count_files(pw->pw_uid, session_list[*snum].pid);
		guest = strequal( session_list[*snum].username, lp_guestaccount() );
					
		if (!(ss1->array[num_entries].client = talloc_strdup(
			      ss1->array, session_list[*snum].remote_machine))) {
			ss1->array[num_entries].client = "";
		}
		if (!(ss1->array[num_entries].user = talloc_strdup(
			      ss1->array, session_list[*snum].username))) {
			ss1->array[num_entries].user = "";
		}
		ss1->array[num_entries].num_open = num_files;
		ss1->array[num_entries].time = connect_time;
		ss1->array[num_entries].idle_time = 0;
		ss1->array[num_entries].user_flags = guest;

		num_entries++;
	}

	ss1->count = num_entries;
	
	if ((*snum) >= (*stot)) {
		(*snum) = 0;
	}
}

/*******************************************************************
 makes a SRV_R_NET_SESS_ENUM structure.
********************************************************************/

static WERROR init_srv_sess_info_ctr(pipes_struct *p, union srvsvc_NetSessCtr *ctr,
				int switch_value, uint32 *resume_hnd, uint32 *total_entries)
{
	WERROR status = WERR_OK;
	DEBUG(5,("init_srv_sess_info_ctr: %d\n", __LINE__));

	switch (switch_value) {
	case 0:
		ctr->ctr0 = talloc(p->mem_ctx, struct srvsvc_NetSessCtr0);
		init_srv_sess_info_0(p, ctr->ctr0, resume_hnd, total_entries);
		break;
	case 1:
		ctr->ctr1 = talloc(p->mem_ctx, struct srvsvc_NetSessCtr1);
		init_srv_sess_info_1(p, ctr->ctr1, resume_hnd, total_entries);
		break;
	default:
		DEBUG(5,("init_srv_sess_info_ctr: unsupported switch value %d\n", switch_value));
		if (resume_hnd != NULL)
			(*resume_hnd) = 0;
		(*total_entries) = 0;
		ctr->ctr0 = NULL;
		status = WERR_UNKNOWN_LEVEL;
		break;
	}

	return status;
}

/*******************************************************************
 fill in a conn info level 0 structure.
 ********************************************************************/

static void init_srv_conn_info_0(pipes_struct *p, struct srvsvc_NetConnCtr0 *ss0, uint32 *snum, uint32 *stot)
{
	uint32 num_entries = 0;
	(*stot) = 1;

	if (ss0 == NULL) {
		if (snum != NULL)
			(*snum) = 0;
		return;
	}

	DEBUG(5,("init_srv_conn_0_ss0\n"));

	if (snum) {
		ss0->array = TALLOC_ARRAY(p->mem_ctx, struct srvsvc_NetConnInfo0, *stot);
		for (; (*snum) < (*stot); (*snum)++) {

			ss0->array[num_entries].conn_id = (*stot);

			/* move on to creating next connection */
			/* move on to creating next conn */
			num_entries++;
		}

		ss0->count = num_entries;
		
		if ((*snum) >= (*stot)) {
			(*snum) = 0;
		}

	} else {
		ss0->array = NULL;
		ss0->count = 0;

		(*stot) = 0;
	}
}

/*******************************************************************
 fill in a conn info level 1 structure.
 ********************************************************************/

static void init_srv_conn_info_1(pipes_struct *p, struct srvsvc_NetConnCtr1 *ss1, uint32 *snum, uint32 *stot)
{
	uint32 num_entries = 0;
	(*stot) = 1;

	if (ss1 == NULL) {
		if (snum != NULL)
			(*snum) = 0;
		return;
	}

	DEBUG(5,("init_srv_conn_1_ss1\n"));

	if (snum) {
		ss1->array = TALLOC_ARRAY(p->mem_ctx, struct srvsvc_NetConnInfo1, *stot);
		for (; (*snum) < (*stot); (*snum)++) {
			ss1->array[num_entries].conn_id = (*stot);
			ss1->array[num_entries].conn_type = 0x3;
			ss1->array[num_entries].num_open = 1;
			ss1->array[num_entries].num_users = 1;
			ss1->array[num_entries].conn_time = 3;
			ss1->array[num_entries].user = "dummy_user";
			ss1->array[num_entries].share = "IPC$";

			/* move on to creating next connection */
			/* move on to creating next conn */
			num_entries++;
		}

		ss1->count = num_entries;

		if ((*snum) >= (*stot)) {
			(*snum) = 0;
		}

	} else {
		ss1->count = 0;
		ss1->array = NULL;
		
		(*stot) = 0;
	}
}

/*******************************************************************
 makes a SRV_R_NET_CONN_ENUM structure.
********************************************************************/

static WERROR init_srv_conn_info_ctr(pipes_struct *p, union srvsvc_NetConnCtr *ctr,
				int switch_value, uint32 *resume_hnd, uint32 *total_entries)
{
	WERROR status = WERR_OK;
	DEBUG(5,("init_srv_conn_info_ctr: %d\n", __LINE__));

	switch (switch_value) {
	case 0:
		init_srv_conn_info_0(p, ctr->ctr0, resume_hnd, total_entries);
		break;
	case 1:
		init_srv_conn_info_1(p, ctr->ctr1, resume_hnd, total_entries);
		break;
	default:
		DEBUG(5,("init_srv_conn_info_ctr: unsupported switch value %d\n", switch_value));
		ctr->ctr0 = NULL;
		(*resume_hnd) = 0;
		(*total_entries) = 0;
		status = WERR_UNKNOWN_LEVEL;
		break;
	}

	return status;
}

/*******************************************************************
 makes a SRV_R_NET_FILE_ENUM structure.
********************************************************************/

static WERROR net_file_enum_3(pipes_struct *p, union srvsvc_NetFileCtr *ctr, uint32 *resume_hnd, uint32 *num_entries )
{
	TALLOC_CTX *ctx = get_talloc_ctx();
	WERROR status;

	/* TODO -- Windows enumerates 
	   (b) active pipes
	   (c) open directories and files */

	ctr->ctr3 = TALLOC_ZERO_P(p->mem_ctx, struct srvsvc_NetFileCtr3);
	
	status = net_enum_files( ctx, &ctr->ctr3->array, num_entries, resume_hnd );
	if ( !W_ERROR_IS_OK(status))
		return status;
		
	status = net_enum_pipes( ctx, &ctr->ctr3->array, num_entries, resume_hnd );
	if ( !W_ERROR_IS_OK(status))
		return status;

	ctr->ctr3->count = *num_entries;
	
	return WERR_OK;
}

/*******************************************************************
*******************************************************************/

WERROR _srvsvc_NetFileEnum(pipes_struct *p, struct srvsvc_NetFileEnum *r)
{
	switch ( *r->in.level ) {
	case 3:
		return net_file_enum_3(p, r->in.ctr, r->in.resume_handle, r->out.totalentries );	
	default:
		return WERR_UNKNOWN_LEVEL;
	}
	
	return WERR_OK;
}

/*******************************************************************
net server get info
********************************************************************/

WERROR _srvsvc_NetSrvGetInfo(pipes_struct *p, struct srvsvc_NetSrvGetInfo *r)
{
	WERROR status = WERR_OK;

	ZERO_STRUCTP(r->out.info);

	DEBUG(5,("srv_net_srv_get_info: %d\n", __LINE__));

	if (!pipe_access_check(p)) {
		DEBUG(3, ("access denied to srv_net_srv_get_info\n"));
		return WERR_ACCESS_DENIED;
	}

	switch (r->in.level) {

		/* Technically level 102 should only be available to
		   Administrators but there isn't anything super-secret
		   here, as most of it is made up. */

	case 102:
		r->out.info->info102 = TALLOC_ZERO_P(p->mem_ctx, struct srvsvc_NetSrvInfo102);

		r->out.info->info102->platform_id = 500;
		r->out.info->info102->version_major = lp_major_announce_version();
		r->out.info->info102->version_minor = lp_minor_announce_version();
		r->out.info->info102->server_name = global_myname(); 
		r->out.info->info102->server_type = lp_default_server_announce();
		r->out.info->info102->userpath = "C:\\";
		r->out.info->info102->licenses = 10000;
		r->out.info->info102->anndelta = 3000;
		r->out.info->info102->disc = 0xf;
		r->out.info->info102->users = 0xffffffff;
		r->out.info->info102->hidden = 0;
		r->out.info->info102->announce = 240;
		r->out.info->info102->comment = lp_serverstring();
		break;
	case 101:
		r->out.info->info101 = TALLOC_ZERO_P(p->mem_ctx, struct srvsvc_NetSrvInfo101);
			r->out.info->info101->platform_id = 500;
			r->out.info->info101->server_name = global_myname();
			r->out.info->info101->version_major = lp_major_announce_version();
			r->out.info->info101->version_minor = lp_minor_announce_version();
			r->out.info->info101->server_type = lp_default_server_announce();
			r->out.info->info101->comment = lp_serverstring();
		break;
	case 100:
		r->out.info->info100 = TALLOC_ZERO_P(p->mem_ctx, struct srvsvc_NetSrvInfo100);
		r->out.info->info100->platform_id = 500;
		r->out.info->info100->server_name = global_myname();
		break;
	default:
		return WERR_UNKNOWN_LEVEL;
		break;
	}

	DEBUG(5,("srv_net_srv_get_info: %d\n", __LINE__));

	return status;
}

/*******************************************************************
net server set info
********************************************************************/

WERROR _srvsvc_NetSrvSetInfo(pipes_struct *p, struct srvsvc_NetSrvSetInfo *r)
{
	/* Set up the net server set info structure. */
	if (r->out.parm_error) {
		*r->out.parm_error = 0;
	}
	return WERR_OK;
}

/*******************************************************************
net conn enum
********************************************************************/

WERROR _srvsvc_NetConnEnum(pipes_struct *p, struct srvsvc_NetConnEnum *r)
{
	DEBUG(5,("srv_net_conn_enum: %d\n", __LINE__));

	ZERO_STRUCTP(r->out.ctr);

	/* set up the */
	return init_srv_conn_info_ctr(p, r->out.ctr, *r->in.level, r->in.resume_handle, r->out.totalentries);
}

/*******************************************************************
net sess enum
********************************************************************/

WERROR _srvsvc_NetSessEnum(pipes_struct *p, struct srvsvc_NetSessEnum *r)
{
	DEBUG(5,("_srv_net_sess_enum: %d\n", __LINE__));

	ZERO_STRUCTP(r->out.ctr);

	/* set up the */
	return init_srv_sess_info_ctr(p, r->out.ctr,
				*r->in.level, 
				r->in.resume_handle,
				r->out.totalentries);
}

/*******************************************************************
net sess del
********************************************************************/

WERROR _srvsvc_NetSessDel(pipes_struct *p, struct srvsvc_NetSessDel *r)
{
	struct sessionid *session_list;
	int num_sessions, snum;
	WERROR status;

	char *machine = talloc_strdup(p->mem_ctx, r->in.server_unc);

	/* strip leading backslashes if any */
	while (machine[0] == '\\') {
		memmove(machine, &machine[1], strlen(machine));
	}

	num_sessions = list_sessions(p->mem_ctx, &session_list);

	DEBUG(5,("_srv_net_sess_del: %d\n", __LINE__));

	status = WERR_ACCESS_DENIED;

	/* fail out now if you are not root or not a domain admin */

	if ((p->pipe_user.ut.uid != sec_initial_uid()) && 
		( ! nt_token_check_domain_rid(p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS))) {

		goto done;
	}

	for (snum = 0; snum < num_sessions; snum++) {

		if ((strequal(session_list[snum].username, r->in.user) || r->in.user[0] == '\0' ) &&
		     strequal(session_list[snum].remote_machine, machine)) {
			NTSTATUS ntstat;

			ntstat = messaging_send(smbd_messaging_context(),
						session_list[snum].pid,
						MSG_SHUTDOWN, &data_blob_null);
		
			if (NT_STATUS_IS_OK(ntstat))
				status = WERR_OK;
		}
	}

	DEBUG(5,("_srv_net_sess_del: %d\n", __LINE__));

done:
	return status;
}

/*******************************************************************
 Net share enum all.
********************************************************************/

WERROR _srvsvc_NetShareEnumAll(pipes_struct *p, struct srvsvc_NetShareEnumAll *r)
{
	DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));

	if (!pipe_access_check(p)) {
		DEBUG(3, ("access denied to srv_net_share_enum_all\n"));
		return WERR_ACCESS_DENIED;
	}

	/* Create the list of shares for the response. */
	return init_srv_share_info_ctr(p, r->out.ctr, *r->in.level,
					      r->in.resume_handle, r->out.totalentries, True);
}

/*******************************************************************
 Net share enum.
********************************************************************/

WERROR _srvsvc_NetShareEnum(pipes_struct *p, struct srvsvc_NetShareEnum *r)
{
	DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));

	if (!pipe_access_check(p)) {
		DEBUG(3, ("access denied to srv_net_share_enum\n"));
		return WERR_ACCESS_DENIED;
	}

	/* Create the list of shares for the response. */
	return init_srv_share_info_ctr(p, r->out.ctr, *r->in.level,
					      r->in.resume_handle, r->out.totalentries, False);
}

/*******************************************************************
 Net share get info.
********************************************************************/

WERROR _srvsvc_NetShareGetInfo(pipes_struct *p, struct srvsvc_NetShareGetInfo *r)
{
	const struct share_params *params;

	params = get_share_params(p->mem_ctx, r->in.share_name);

	if (params != NULL) {
		switch (r->in.level) {
		case 0:
			r->out.info->info0 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo0);
			init_srv_share_info_0(p, r->out.info->info0, params);
			break;
		case 1:
			r->out.info->info1 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo1);
			init_srv_share_info_1(p, r->out.info->info1, params);
			break;
		case 2:
			r->out.info->info2 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo2);
			init_srv_share_info_2(p, r->out.info->info2, params);
			break;
		case 501:
			r->out.info->info501 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo501);
			init_srv_share_info_501(p, r->out.info->info501, params);
			break;
		case 502:
			r->out.info->info502 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo502);
			init_srv_share_info_502(p, r->out.info->info502, params);
			break;

			/* here for completeness */
		case 1004:
			r->out.info->info1004 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo1004);
			init_srv_share_info_1004(p, r->out.info->info1004, params);
			break;
		case 1005:
			r->out.info->info1005 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo1005);
			init_srv_share_info_1005(p, r->out.info->info1005, params);
			break;

			/* here for completeness 1006 - 1501 */
		case 1006:
			r->out.info->info1006 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo1006);
			init_srv_share_info_1006(p, r->out.info->info1006,
						 params);
			break;
		case 1007:
			r->out.info->info1007 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo1007);
			init_srv_share_info_1007(p, r->out.info->info1007,
						 params);
			break;
		case 1501:
			r->out.info->info1501 = talloc(p->mem_ctx, struct sec_desc_buf);
			init_srv_share_info_1501(p, r->out.info->info1501,
						 params);
			break;
		default:
			DEBUG(5,("init_srv_net_share_get_info: unsupported "
				 "switch value %d\n", r->in.level));
			return WERR_UNKNOWN_LEVEL;
			break;
		}
	} else {
		return WERR_INVALID_NAME;
	}

	return WERR_OK;
}

/*******************************************************************
 Check a given DOS pathname is valid for a share.
********************************************************************/

char *valid_share_pathname(char *dos_pathname)
{
	char *ptr;

	/* Convert any '\' paths to '/' */
	unix_format(dos_pathname);
	unix_clean_name(dos_pathname);

	/* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
	ptr = dos_pathname;
	if (strlen(dos_pathname) > 2 && ptr[1] == ':' && ptr[0] != '/')
		ptr += 2;

	/* Only absolute paths allowed. */
	if (*ptr != '/')
		return NULL;

	return ptr;
}

static void setval_helper(struct registry_key *key, const char *name,
			  const char *value, WERROR *err)
{
	struct registry_value val;

	if (!W_ERROR_IS_OK(*err)) {
		return;
	}

	ZERO_STRUCT(val);
	val.type = REG_SZ;
	val.v.sz.str = CONST_DISCARD(char *, value);
	val.v.sz.len = strlen(value)+1;

	*err = reg_setvalue(key, name, &val);
}

static WERROR add_share(const char *share_name, const char *path,
			const char *comment, uint32 max_connections,
			const struct nt_user_token *token,
			BOOL is_disk_op)
{
	if (lp_add_share_cmd() && *lp_add_share_cmd()) {
		char *command;
		int ret;

		if (asprintf(&command, "%s \"%s\" \"%s\" \"%s\" \"%s\" %d",
			     lp_add_share_cmd(), dyn_CONFIGFILE, share_name,
			     path, comment, max_connections) == -1) {
			return WERR_NOMEM;
		}

		DEBUG(10,("add_share: Running [%s]\n", command ));

		/********* BEGIN SeDiskOperatorPrivilege BLOCK *********/
	
		if ( is_disk_op )
			become_root();

		if ( (ret = smbrun(command, NULL)) == 0 ) {
			/* Tell everyone we updated smb.conf. */
			message_send_all(smbd_messaging_context(),
					 MSG_SMB_CONF_UPDATED,
					 NULL, 0, NULL);
		}

		if ( is_disk_op )
			unbecome_root();
		
		/********* END SeDiskOperatorPrivilege BLOCK *********/

		DEBUG(3,("_srv_net_share_add: Running [%s] returned (%d)\n",
			 command, ret ));

		/*
		 * No fallback to registry shares, the user did define a add
		 * share command, so fail here.
		 */

		SAFE_FREE(command);
		return (ret == 0) ? WERR_OK : WERR_ACCESS_DENIED;
	}

	if (lp_registry_shares()) {
		char *keyname;
		struct registry_key *key;
		enum winreg_CreateAction action;
		WERROR err;
		TALLOC_CTX *mem_ctx;

		if (!(keyname = talloc_asprintf(NULL, "%s\\%s", KEY_SMBCONF,
						share_name))) {
			return WERR_NOMEM;
		}

		mem_ctx = (TALLOC_CTX *)keyname;

		err = reg_create_path(mem_ctx, keyname, REG_KEY_WRITE,
				      is_disk_op ? get_root_nt_token():token,
				      &action, &key);

		if (action != REG_CREATED_NEW_KEY) {
			err = WERR_ALREADY_EXISTS;
		}

		if (!W_ERROR_IS_OK(err)) {
			TALLOC_FREE(mem_ctx);
			return err;
		}

		setval_helper(key, "path", path, &err);
		if ((comment != NULL) && (comment[0] != '\0')) {
			setval_helper(key, "comment", comment, &err);
		}
		if (max_connections != 0) {
			char tmp[16];
			snprintf(tmp, sizeof(tmp), "%d", max_connections);
			setval_helper(key, "max connections", tmp, &err);
		}

		if (!W_ERROR_IS_OK(err)) {
			/*
			 * Hmmmm. We'd need transactions on the registry to
			 * get this right....
			 */
			reg_delete_path(is_disk_op ? get_root_nt_token():token,
					keyname);
		}
		TALLOC_FREE(mem_ctx);
		return err;
	}

	return WERR_ACCESS_DENIED;
}

static WERROR delete_share(const char *sharename, 
			   const struct nt_user_token *token,
			   BOOL is_disk_op)
{
	if (lp_delete_share_cmd() && *lp_delete_share_cmd()) {
		char *command;
		int ret;

		if (asprintf(&command, "%s \"%s\" \"%s\"",
			     lp_delete_share_cmd(), dyn_CONFIGFILE,
			     sharename)) {
			return WERR_NOMEM;
		}

		DEBUG(10,("delete_share: Running [%s]\n", command ));

		/********* BEGIN SeDiskOperatorPrivilege BLOCK *********/
	
		if ( is_disk_op )
			become_root();

		if ( (ret = smbrun(command, NULL)) == 0 ) {
			/* Tell everyone we updated smb.conf. */
			message_send_all(smbd_messaging_context(),
					 MSG_SMB_CONF_UPDATED,
					 NULL, 0, NULL);
		}

		if ( is_disk_op )
			unbecome_root();

		/********* END SeDiskOperatorPrivilege BLOCK *********/

		SAFE_FREE(command);

		DEBUG(3,("_srv_net_share_del: Running [%s] returned (%d)\n",
			 command, ret ));
		return (ret == 0) ? WERR_OK : WERR_ACCESS_DENIED;
	}

	if (lp_registry_shares()) {
		char *keyname;
		WERROR err;

		if (asprintf(&keyname, "%s\\%s", KEY_SMBCONF,
			     sharename) == -1) {
			return WERR_NOMEM;
		}

		err = reg_delete_path(is_disk_op ? get_root_nt_token():token,
				      keyname);
		SAFE_FREE(keyname);
		return err;
	}

	return WERR_ACCESS_DENIED;
}

static WERROR change_share(const char *share_name, const char *path,
			   const char *comment, uint32 max_connections,
			   const struct nt_user_token *token,
			   BOOL is_disk_op)
{
	if (lp_change_share_cmd() && *lp_change_share_cmd()) {
		char *command;
		int ret;

		if (asprintf(&command, "%s \"%s\" \"%s\" \"%s\" \"%s\" %d",
			     lp_change_share_cmd(), dyn_CONFIGFILE, share_name,
			     path, comment, max_connections) == -1) {
			return WERR_NOMEM;
		}

		DEBUG(10,("_srv_net_share_set_info: Running [%s]\n", command));
				
		/********* BEGIN SeDiskOperatorPrivilege BLOCK *********/
	
		if ( is_disk_op )
			become_root();
			
		if ( (ret = smbrun(command, NULL)) == 0 ) {
			/* Tell everyone we updated smb.conf. */
			message_send_all(smbd_messaging_context(),
					 MSG_SMB_CONF_UPDATED,
					 NULL, 0, NULL);
		}
		
		if ( is_disk_op )
			unbecome_root();
			
		/********* END SeDiskOperatorPrivilege BLOCK *********/

		DEBUG(3,("_srv_net_share_set_info: Running [%s] returned "
			 "(%d)\n", command, ret ));

		SAFE_FREE(command);

		return (ret == 0) ? WERR_OK : WERR_ACCESS_DENIED;
	}

	if (lp_registry_shares()) {
		char *keyname;
		struct registry_key *key;
		WERROR err;
		TALLOC_CTX *mem_ctx;

		if (!(keyname = talloc_asprintf(NULL, "%s\\%s", KEY_SMBCONF,
						share_name))) {
			return WERR_NOMEM;
		}

		mem_ctx = (TALLOC_CTX *)keyname;

		err = reg_open_path(mem_ctx, keyname, REG_KEY_WRITE,
				    is_disk_op ? get_root_nt_token():token,
				    &key);
		if (!W_ERROR_IS_OK(err)) {
			TALLOC_FREE(mem_ctx);
			return err;
		}

		setval_helper(key, "path", path, &err);

		reg_deletevalue(key, "comment");
		if ((comment != NULL) && (comment[0] != '\0')) {
			setval_helper(key, "comment", comment, &err);
		}

		reg_deletevalue(key, "max connections");
		if (max_connections != 0) {
			char tmp[16];
			snprintf(tmp, sizeof(tmp), "%d", max_connections);
			setval_helper(key, "max connections", tmp, &err);
		}

		TALLOC_FREE(mem_ctx);
		return err;
	}		

	return WERR_ACCESS_DENIED;
}

/*******************************************************************
 Net share set info. Modify share details.
********************************************************************/

WERROR _srvsvc_NetShareSetInfo(pipes_struct *p, struct srvsvc_NetShareSetInfo *r)
{
	pstring comment;
	pstring pathname;
	int type;
	int snum;
	char *path;
	SEC_DESC *psd = NULL;
	SE_PRIV se_diskop = SE_DISK_OPERATOR;
	BOOL is_disk_op = False;
	int max_connections = 0;
	fstring tmp_share_name;

	DEBUG(5,("_srv_net_share_set_info: %d\n", __LINE__));

	if (r->out.parm_error) {
		*r->out.parm_error = 0;
	}

	if ( strequal(r->in.share_name,"IPC$") 
		|| ( lp_enable_asu_support() && strequal(r->in.share_name,"ADMIN$") )
		|| strequal(r->in.share_name,"global") )
	{
		return WERR_ACCESS_DENIED;
	}

	fstrcpy(tmp_share_name, r->in.share_name);
	snum = find_service(tmp_share_name);

	/* Does this share exist ? */
	if (snum < 0)
		return WERR_NET_NAME_NOT_FOUND;

	/* No change to printer shares. */
	if (lp_print_ok(snum))
		return WERR_ACCESS_DENIED;

	is_disk_op = user_has_privileges( p->pipe_user.nt_user_token,
					  &se_diskop );
	
	/* fail out now if you are not root and not a disk op */
	
	if ( p->pipe_user.ut.uid != sec_initial_uid() && !is_disk_op )
		return WERR_ACCESS_DENIED;

	switch (r->in.level) {
	case 1:
		pstrcpy(pathname, lp_pathname(snum));
		pstrcpy(comment, r->in.info.info1->comment);
		type = r->in.info.info1->type;
		psd = NULL;
		break;
	case 2:
		pstrcpy(comment, r->in.info.info2->comment);
		pstrcpy(pathname, r->in.info.info2->path);
		type = r->in.info.info2->type;
		max_connections = (r->in.info.info2->max_users == 0xffffffff) ?
			0 : r->in.info.info2->max_users;
		psd = NULL;
		break;
	case 502:
		pstrcpy(comment, r->in.info.info502->comment);
		pstrcpy(pathname, r->in.info.info502->path);
		type = r->in.info.info502->type;
		psd = r->in.info.info502->sd;
		map_generic_share_sd_bits(psd);
		break;
	case 1004:
		pstrcpy(pathname, lp_pathname(snum));
		pstrcpy(comment, r->in.info.info1004->comment);
		type = STYPE_DISKTREE;
		break;
	case 1005:
                /* XP re-sets the csc policy even if it wasn't changed by the
		   user, so we must compare it to see if it's what is set in
		   smb.conf, so that we can contine other ops like setting
		   ACLs on a share */
		if (((r->in.info.info1005->dfs_flags &
		      SHARE_1005_CSC_POLICY_MASK) >>
		     SHARE_1005_CSC_POLICY_SHIFT) == lp_csc_policy(snum))
			return WERR_OK;
		else {
			DEBUG(3, ("_srv_net_share_set_info: client is trying "
				  "to change csc policy from the network; "
				  "must be done with smb.conf\n"));
			return WERR_ACCESS_DENIED;
		}
	case 1006:
	case 1007:
		return WERR_ACCESS_DENIED;
	case 1501:
		pstrcpy(pathname, lp_pathname(snum));
		pstrcpy(comment, lp_comment(snum));
		psd = r->in.info.info1501->sd;
		map_generic_share_sd_bits(psd);
		type = STYPE_DISKTREE;
		break;
	default:
		DEBUG(5,("_srv_net_share_set_info: unsupported switch value "
			 "%d\n", r->in.level));
		return WERR_UNKNOWN_LEVEL;
	}

	/* We can only modify disk shares. */
	if (type != STYPE_DISKTREE)
		return WERR_ACCESS_DENIED;
		
	/* Check if the pathname is valid. */
	if (!(path = valid_share_pathname( pathname )))
		return WERR_OBJECT_PATH_INVALID;

	/* Ensure share name, pathname and comment don't contain '"'
	 * characters. */
	string_replace(tmp_share_name, '"', ' ');
	string_replace(path, '"', ' ');
	string_replace(comment, '"', ' ');

	DEBUG(10,("_srv_net_share_set_info: change share command = %s\n",
		  lp_change_share_cmd() ? lp_change_share_cmd() : "NULL" ));

	/* Only call modify function if something changed. */
	
	if (strcmp(path, lp_pathname(snum))
	    || strcmp(comment, lp_comment(snum)) 
	    || (lp_max_connections(snum) != max_connections) ) {
		WERROR err;

		err = change_share(tmp_share_name, path, comment,
				   max_connections, p->pipe_user.nt_user_token,
				   is_disk_op);

		if (!W_ERROR_IS_OK(err)) {
			return err;
		}
	}

	/* Replace SD if changed. */
	if (psd) {
		SEC_DESC *old_sd;
		size_t sd_size;

		old_sd = get_share_security(p->mem_ctx, lp_servicename(snum),
					    &sd_size);

		if (old_sd && !sec_desc_equal(old_sd, psd)) {
			if (!set_share_security(r->in.share_name, psd)) {
				DEBUG(0,("_srv_net_share_set_info: Failed to "
					 "change security info in share %s.\n",
					 r->in.share_name ));
			}
		}
	}
			
	DEBUG(5,("_srv_net_share_set_info: %d\n", __LINE__));

	return WERR_OK;
}


/*******************************************************************
 Net share add. Call 'add_share_command "sharename" "pathname" 
 "comment" "max connections = "
********************************************************************/

WERROR _srvsvc_NetShareAdd(pipes_struct *p, struct srvsvc_NetShareAdd *r)
{
	pstring share_name;
	pstring comment;
	pstring pathname;
	char *path;
	int type;
	SEC_DESC *psd = NULL;
	SE_PRIV se_diskop = SE_DISK_OPERATOR;
	BOOL is_disk_op;
	uint32 max_connections = 0;
	WERROR err;

	DEBUG(5,("_srv_net_share_add: %d\n", __LINE__));

	if (r->out.parm_error) {
		*r->out.parm_error = 0;
	}

	is_disk_op = user_has_privileges( p->pipe_user.nt_user_token,
					  &se_diskop );

	if (p->pipe_user.ut.uid != sec_initial_uid()  && !is_disk_op ) 
		return WERR_ACCESS_DENIED;

	switch (r->in.level) {
	case 0:
		/* No path. Not enough info in a level 0 to do anything. */
		return WERR_ACCESS_DENIED;
	case 1:
		/* Not enough info in a level 1 to do anything. */
		return WERR_ACCESS_DENIED;
	case 2:
		pstrcpy(share_name, r->in.info.info2->name);
		pstrcpy(comment, r->in.info.info2->comment);
		pstrcpy(pathname, r->in.info.info2->path);
		max_connections = (r->in.info.info2->max_users == 0xffffffff) ?
			0 : r->in.info.info2->max_users;
		type = r->in.info.info2->type;
		break;
	case 501:
		/* No path. Not enough info in a level 501 to do anything. */
		return WERR_ACCESS_DENIED;
	case 502:
		pstrcpy(share_name, r->in.info.info502->name);
		pstrcpy(comment, r->in.info.info502->comment);
		pstrcpy(pathname, r->in.info.info502->path);
		type = r->in.info.info502->type;
		psd = r->in.info.info502->sd;
		map_generic_share_sd_bits(psd);
		break;

		/* none of the following contain share names.  NetShareAdd
		 * does not have a separate parameter for the share name */ 

	case 1004:
	case 1005:
	case 1006:
	case 1007:
		return WERR_ACCESS_DENIED;
	case 1501:
		/* DFS only level. */
		return WERR_ACCESS_DENIED;
	default:
		DEBUG(5,("_srv_net_share_add: unsupported switch value %d\n",
			 r->in.level));
		return WERR_UNKNOWN_LEVEL;
	}

	/* check for invalid share names */

	if ( !validate_net_name( share_name, INVALID_SHARENAME_CHARS,
				 sizeof(share_name) ) ) {
		DEBUG(5,("_srv_net_name_validate: Bad sharename \"%s\"\n",
			 share_name));
		return WERR_INVALID_NAME;
	}

	if ( strequal(share_name,"IPC$") || strequal(share_name,"global")
	     || ( lp_enable_asu_support() && strequal(share_name,"ADMIN$") ) )
	{
		return WERR_ACCESS_DENIED;
	}

	if (get_share_params(p->mem_ctx, share_name) != NULL) {
		/* Share already exists. */
		return WERR_ALREADY_EXISTS;
	}

	/* We can only add disk shares. */
	if (type != STYPE_DISKTREE)
		return WERR_ACCESS_DENIED;
		
	/* Check if the pathname is valid. */
	if (!(path = valid_share_pathname( pathname )))
		return WERR_OBJECT_PATH_INVALID;

	/* Ensure share name, pathname and comment don't contain '"'
	 * characters. */

	string_replace(share_name, '"', ' ');
	string_replace(path, '"', ' ');
	string_replace(comment, '"', ' ');

	err = add_share(share_name, path, comment, max_connections,
			p->pipe_user.nt_user_token, is_disk_op);

	if (!W_ERROR_IS_OK(err)) {
		return err;
	}

	if (psd) {
		if (!set_share_security(share_name, psd)) {
			DEBUG(0,("_srv_net_share_add: Failed to add security "
				 "info to share %s.\n", share_name ));
		}
	}

	/*
	 * We don't call reload_services() here, the message will
	 * cause this to be done before the next packet is read
	 * from the client. JRA.
	 */

	DEBUG(5,("_srv_net_share_add: %d\n", __LINE__));

	return WERR_OK;
}

/*******************************************************************
 Net share delete. Call "delete share command" with the share name as
 a parameter.
********************************************************************/

WERROR _srvsvc_NetShareDel(pipes_struct *p, struct srvsvc_NetShareDel *r)
{
	struct share_params *params;
	SE_PRIV se_diskop = SE_DISK_OPERATOR;
	BOOL is_disk_op;
	WERROR err;

	DEBUG(5,("_srv_net_share_del: %d\n", __LINE__));

	if ( strequal(r->in.share_name, "IPC$") 
	     || ( lp_enable_asu_support() && strequal(r->in.share_name,"ADMIN$") )
	     || strequal(r->in.share_name, "global") )
	{
		return WERR_ACCESS_DENIED;
	}

	if (!(params = get_share_params(p->mem_ctx, r->in.share_name))) {
		return WERR_NO_SUCH_SHARE;
	}

	/* No change to printer shares. */
	if (lp_print_ok(params->service))
		return WERR_ACCESS_DENIED;

	is_disk_op = user_has_privileges( p->pipe_user.nt_user_token,
					  &se_diskop );

	if (p->pipe_user.ut.uid != sec_initial_uid()  && !is_disk_op ) 
		return WERR_ACCESS_DENIED;

	err = delete_share(lp_servicename(params->service),
			   p->pipe_user.nt_user_token, is_disk_op);

	if (!W_ERROR_IS_OK(err)) {
		return err;
	}

	/* Delete the SD in the database. */
	delete_share_security(params);

	lp_killservice(params->service);

	return WERR_OK;
}

WERROR _srvsvc_NetShareDelSticky(pipes_struct *p, struct srvsvc_NetShareDelSticky *r)
{
	struct srvsvc_NetShareDel s;

	DEBUG(5,("_srv_net_share_del_stick: %d\n", __LINE__));

	s.in.server_unc = r->in.server_unc;
	s.in.share_name = r->in.share_name;
	s.in.reserved = r->in.reserved;

	return _srvsvc_NetShareDel(p, &s);
}

/*******************************************************************
time of day
********************************************************************/

WERROR _srvsvc_NetRemoteTOD(pipes_struct *p, struct srvsvc_NetRemoteTOD *r)
{
	struct tm *t;
	time_t unixdate = time(NULL);
	WERROR status = WERR_OK;

	/* We do this call first as if we do it *after* the gmtime call
	   it overwrites the pointed-to values. JRA */

	uint32 zone = get_time_zone(unixdate)/60;

	DEBUG(5,("_srv_net_remote_tod: %d\n", __LINE__));

	t = gmtime(&unixdate);

	/* set up the */
	r->out.info->elapsed = unixdate;
	r->out.info->msecs = 0;
	r->out.info->hours = t->tm_hour;
	r->out.info->mins = t->tm_min;
	r->out.info->secs = t->tm_sec;
	r->out.info->hunds = 0;
	r->out.info->timezone = zone;
	r->out.info->tinterval = 10000;
	r->out.info->day = t->tm_mday;
	r->out.info->month = t->tm_mon + 1;
	r->out.info->year = 1900+t->tm_year;
	r->out.info->weekday = t->tm_wday;
	
	DEBUG(5,("_srv_net_remote_tod: %d\n", __LINE__));

	return status;
}

/***********************************************************************************
 Win9x NT tools get security descriptor.
***********************************************************************************/

WERROR _srvsvc_NetGetFileSecurity(pipes_struct *p, struct srvsvc_NetGetFileSecurity *r)
{
	SEC_DESC *psd = NULL;
	size_t sd_size;
	DATA_BLOB null_pw;
	files_struct *fsp = NULL;
	SMB_STRUCT_STAT st;
	NTSTATUS nt_status;
	connection_struct *conn = NULL;
	BOOL became_user = False; 
	WERROR status = WERR_OK;
	pstring tmp_file;

	ZERO_STRUCT(st);


	/* Null password is ok - we are already an authenticated user... */
	null_pw = data_blob_null;

	become_root();
	conn = make_connection(r->in.share, null_pw, "A:", p->pipe_user.vuid, &nt_status);
	unbecome_root();

	if (conn == NULL) {
		DEBUG(3,("_srv_net_file_query_secdesc: Unable to connect to %s\n", r->in.share));
		status = ntstatus_to_werror(nt_status);
		goto error_exit;
	}

	if (!become_user(conn, conn->vuid)) {
		DEBUG(0,("_srv_net_file_query_secdesc: Can't become connected user!\n"));
		status = WERR_ACCESS_DENIED;
		goto error_exit;
	}
	became_user = True;

	pstrcpy(tmp_file, r->in.file);
	nt_status = unix_convert(conn, tmp_file, False, NULL, &st);
	if (!NT_STATUS_IS_OK(nt_status)) {
		DEBUG(3,("_srv_net_file_query_secdesc: bad pathname %s\n", r->in.file));
		status = WERR_ACCESS_DENIED;
		goto error_exit;
	}

	nt_status = check_name(conn, r->in.file);
	if (!NT_STATUS_IS_OK(nt_status)) {
		DEBUG(3,("_srv_net_file_query_secdesc: can't access %s\n", r->in.file));
		status = WERR_ACCESS_DENIED;
		goto error_exit;
	}

	nt_status = open_file_stat(conn, r->in.file, &st, &fsp);
	if (!NT_STATUS_IS_OK(nt_status)) {
		/* Perhaps it is a directory */
		if (NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_IS_A_DIRECTORY))
			nt_status = open_directory(conn, r->in.file, &st,
					READ_CONTROL_ACCESS,
					FILE_SHARE_READ|FILE_SHARE_WRITE,
					FILE_OPEN,
					0,
					FILE_ATTRIBUTE_DIRECTORY,
					NULL, &fsp);

		if (!NT_STATUS_IS_OK(nt_status)) {
			DEBUG(3,("_srv_net_file_query_secdesc: Unable to open file %s\n", r->in.file));
			status = WERR_ACCESS_DENIED;
			goto error_exit;
		}
	}

	sd_size = SMB_VFS_GET_NT_ACL(fsp, fsp->fsp_name, (OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION), &psd);

	if (sd_size == 0) {
		DEBUG(3,("_srv_net_file_query_secdesc: Unable to get NT ACL for file %s\n", r->in.file));
		status = WERR_ACCESS_DENIED;
		goto error_exit;
	}

	r->out.sd_buf->sd_size= sd_size;
	r->out.sd_buf->sd = psd;

	psd->dacl->revision = (uint16) NT4_ACL_REVISION;

	close_file(fsp, NORMAL_CLOSE);
	unbecome_user();
	close_cnum(conn, p->pipe_user.vuid);
	return status;

error_exit:

	if(fsp) {
		close_file(fsp, NORMAL_CLOSE);
	}

	if (became_user)
		unbecome_user();

	if (conn) 
		close_cnum(conn, p->pipe_user.vuid);

	return status;
}

/***********************************************************************************
 Win9x NT tools set security descriptor.
***********************************************************************************/

WERROR _srvsvc_NetSetFileSecurity(pipes_struct *p, struct srvsvc_NetSetFileSecurity *r)
{
	BOOL ret;
	DATA_BLOB null_pw;
	files_struct *fsp = NULL;
	SMB_STRUCT_STAT st;
	NTSTATUS nt_status;
	connection_struct *conn = NULL;
	BOOL became_user = False;
	WERROR status = WERR_OK;
	pstring tmp_file;

	ZERO_STRUCT(st);

	/* Null password is ok - we are already an authenticated user... */
	null_pw = data_blob_null;

	become_root();
	conn = make_connection(r->in.share, null_pw, "A:", p->pipe_user.vuid, &nt_status);
	unbecome_root();

	if (conn == NULL) {
		DEBUG(3,("_srv_net_file_set_secdesc: Unable to connect to %s\n", r->in.share));
		status = ntstatus_to_werror(nt_status);
		goto error_exit;
	}

	if (!become_user(conn, conn->vuid)) {
		DEBUG(0,("_srv_net_file_set_secdesc: Can't become connected user!\n"));
		status = WERR_ACCESS_DENIED;
		goto error_exit;
	}
	became_user = True;

	pstrcpy(tmp_file, r->in.file);
	nt_status = unix_convert(conn, tmp_file, False, NULL, &st);
	if (!NT_STATUS_IS_OK(nt_status)) {
		DEBUG(3,("_srv_net_file_set_secdesc: bad pathname %s\n", r->in.file));
		status = WERR_ACCESS_DENIED;
		goto error_exit;
	}

	nt_status = check_name(conn, r->in.file);
	if (!NT_STATUS_IS_OK(nt_status)) {
		DEBUG(3,("_srv_net_file_set_secdesc: can't access %s\n", r->in.file));
		status = WERR_ACCESS_DENIED;
		goto error_exit;
	}


	nt_status = open_file_stat(conn, r->in.file, &st, &fsp);

	if (!NT_STATUS_IS_OK(nt_status)) {
		/* Perhaps it is a directory */
		if (NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_IS_A_DIRECTORY))
			nt_status = open_directory(conn, r->in.file, &st,
						FILE_READ_ATTRIBUTES,
						FILE_SHARE_READ|FILE_SHARE_WRITE,
						FILE_OPEN,
						0,
						FILE_ATTRIBUTE_DIRECTORY,
						NULL, &fsp);

		if (!NT_STATUS_IS_OK(nt_status)) {
			DEBUG(3,("_srv_net_file_set_secdesc: Unable to open file %s\n", r->in.file));
			status = WERR_ACCESS_DENIED;
			goto error_exit;
		}
	}

	ret = SMB_VFS_SET_NT_ACL(fsp, fsp->fsp_name, r->in.securityinformation, r->in.sd_buf.sd);

	if (ret == False) {
		DEBUG(3,("_srv_net_file_set_secdesc: Unable to set NT ACL on file %s\n", r->in.file));
		status = WERR_ACCESS_DENIED;
		goto error_exit;
	}

	close_file(fsp, NORMAL_CLOSE);
	unbecome_user();
	close_cnum(conn, p->pipe_user.vuid);
	return status;

error_exit:

	if(fsp) {
		close_file(fsp, NORMAL_CLOSE);
	}

	if (became_user) {
		unbecome_user();
	}

	if (conn) {
		close_cnum(conn, p->pipe_user.vuid);
	}

	return status;
}

/***********************************************************************************
 It may be that we want to limit users to creating shares on certain areas of the UNIX file area.
 We could define areas by mapping Windows style disks to points on the UNIX directory hierarchy.
 These disks would the disks listed by this function.
 Users could then create shares relative to these disks.  Watch out for moving these disks around.
 "Nigel Williams" <nigel@veritas.com>.
***********************************************************************************/

static const char *server_disks[] = {"C:"};

static uint32 get_server_disk_count(void)
{
	return sizeof(server_disks)/sizeof(server_disks[0]);
}

static uint32 init_server_disk_enum(uint32 *resume)
{
	uint32 server_disk_count = get_server_disk_count();

	/*resume can be an offset into the list for now*/

	if(*resume & 0x80000000)
		*resume = 0;

	if(*resume > server_disk_count)
		*resume = server_disk_count;

	return server_disk_count - *resume;
}

static const char *next_server_disk_enum(uint32 *resume)
{
	const char *disk;

	if(init_server_disk_enum(resume) == 0)
		return NULL;

	disk = server_disks[*resume];

	(*resume)++;

	DEBUG(10, ("next_server_disk_enum: reporting disk %s. resume handle %d.\n", disk, *resume));

	return disk;
}

WERROR _srvsvc_NetDiskEnum(pipes_struct *p, struct srvsvc_NetDiskEnum *r)
{
	uint32 i;
	const char *disk_name;

	WERROR status = WERR_OK;

	*r->out.totalentries = init_server_disk_enum(r->in.resume_handle);
	r->out.info->count = 0;

	if(!(r->out.info->disks =  TALLOC_ARRAY(p->mem_ctx, struct srvsvc_NetDiskInfo0, MAX_SERVER_DISK_ENTRIES))) {
		return WERR_NOMEM;
	}

	/*allow one struct srvsvc_NetDiskInfo0 for null terminator*/

	for(i = 0; i < MAX_SERVER_DISK_ENTRIES -1 && (disk_name = next_server_disk_enum(r->in.resume_handle)); i++) {

		r->out.info->count++;
		(*r->out.totalentries)++;

		/*copy disk name into a unicode string*/

		r->out.info->disks[i].disk = disk_name; 
	}

	/* add a terminating null string.  Is this there if there is more data to come? */

	r->out.info->count++;
	(*r->out.totalentries)++;

	r->out.info->disks[i].disk = "";

	return status;
}

/********************************************************************
********************************************************************/

WERROR _srvsvc_NetNameValidate(pipes_struct *p, struct srvsvc_NetNameValidate *r)
{
	int len;

	if ((r->in.flags != 0x0) && (r->in.flags != 0x80000000)) {
		return WERR_INVALID_PARAM;
	}

	switch ( r->in.name_type ) {
	case 0x9:
		len = strlen_m(r->in.name);

		if ((r->in.flags == 0x0) && (len > 81)) {
			DEBUG(5,("_srv_net_name_validate: share name too long (%s > 81 chars)\n", r->in.name));
			return WERR_INVALID_NAME;
		}
		if ((r->in.flags == 0x80000000) && (len > 13)) {
			DEBUG(5,("_srv_net_name_validate: share name too long (%s > 13 chars)\n", r->in.name));
			return WERR_INVALID_NAME;
		}

		if ( ! validate_net_name( r->in.name, INVALID_SHARENAME_CHARS, sizeof(r->in.name) ) ) {
			DEBUG(5,("_srv_net_name_validate: Bad sharename \"%s\"\n", r->in.name));
			return WERR_INVALID_NAME;
		}
		break;

	default:
		return WERR_UNKNOWN_LEVEL;
	}

	return WERR_OK;
}


/********************************************************************
********************************************************************/

WERROR _srvsvc_NetFileClose(pipes_struct *p, struct srvsvc_NetFileClose *r)
{
	return WERR_ACCESS_DENIED;
}

WERROR _srvsvc_NetCharDevEnum(pipes_struct *p, struct srvsvc_NetCharDevEnum *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetCharDevGetInfo(pipes_struct *p, struct srvsvc_NetCharDevGetInfo *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetCharDevControl(pipes_struct *p, struct srvsvc_NetCharDevControl *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetCharDevQEnum(pipes_struct *p, struct srvsvc_NetCharDevQEnum *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetCharDevQGetInfo(pipes_struct *p, struct srvsvc_NetCharDevQGetInfo *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetCharDevQSetInfo(pipes_struct *p, struct srvsvc_NetCharDevQSetInfo *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetCharDevQPurge(pipes_struct *p, struct srvsvc_NetCharDevQPurge *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetCharDevQPurgeSelf(pipes_struct *p, struct srvsvc_NetCharDevQPurgeSelf *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetFileGetInfo(pipes_struct *p, struct srvsvc_NetFileGetInfo *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetShareCheck(pipes_struct *p, struct srvsvc_NetShareCheck *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetServerStatisticsGet(pipes_struct *p, struct srvsvc_NetServerStatisticsGet *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetTransportAdd(pipes_struct *p, struct srvsvc_NetTransportAdd *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetTransportEnum(pipes_struct *p, struct srvsvc_NetTransportEnum *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetTransportDel(pipes_struct *p, struct srvsvc_NetTransportDel *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetSetServiceBits(pipes_struct *p, struct srvsvc_NetSetServiceBits *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetPathType(pipes_struct *p, struct srvsvc_NetPathType *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetPathCanonicalize(pipes_struct *p, struct srvsvc_NetPathCanonicalize *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetPathCompare(pipes_struct *p, struct srvsvc_NetPathCompare *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NETRPRNAMECANONICALIZE(pipes_struct *p, struct srvsvc_NETRPRNAMECANONICALIZE *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetPRNameCompare(pipes_struct *p, struct srvsvc_NetPRNameCompare *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetShareDelStart(pipes_struct *p, struct srvsvc_NetShareDelStart *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetShareDelCommit(pipes_struct *p, struct srvsvc_NetShareDelCommit *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetServerTransportAddEx(pipes_struct *p, struct srvsvc_NetServerTransportAddEx *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NetServerSetServiceBitsEx(pipes_struct *p, struct srvsvc_NetServerSetServiceBitsEx *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NETRDFSGETVERSION(pipes_struct *p, struct srvsvc_NETRDFSGETVERSION *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NETRDFSCREATELOCALPARTITION(pipes_struct *p, struct srvsvc_NETRDFSCREATELOCALPARTITION *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NETRDFSDELETELOCALPARTITION(pipes_struct *p, struct srvsvc_NETRDFSDELETELOCALPARTITION *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NETRDFSSETLOCALVOLUMESTATE(pipes_struct *p, struct srvsvc_NETRDFSSETLOCALVOLUMESTATE *R)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NETRDFSSETSERVERINFO(pipes_struct *p, struct srvsvc_NETRDFSSETSERVERINFO *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NETRDFSCREATEEXITPOINT(pipes_struct *p, struct srvsvc_NETRDFSCREATEEXITPOINT *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NETRDFSDELETEEXITPOINT(pipes_struct *p, struct srvsvc_NETRDFSDELETEEXITPOINT *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NETRSERVERTRANSPORTDELEX(pipes_struct *p, struct srvsvc_NETRSERVERTRANSPORTDELEX *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NETRDFSMANAGERREPORTSITEINFO(pipes_struct *p, struct srvsvc_NETRDFSMANAGERREPORTSITEINFO *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NETRDFSMODIFYPREFIX(pipes_struct *p, struct srvsvc_NETRDFSMODIFYPREFIX *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _srvsvc_NETRDFSFIXLOCALVOLUME(pipes_struct *p, struct srvsvc_NETRDFSFIXLOCALVOLUME *r)
{
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}