summaryrefslogtreecommitdiff
path: root/usr/src/lib/smbsrv/libmlsvc/common/mlsvc_srvsvc.c
blob: 26cd32ac5d302b6c4a11efd9b6c617a6f386f36d (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Server Service RPC (SRVSVC) server-side interface definition.
 * The server service provides a remote administration interface.
 *
 * This service uses NERR/Win32 error codes rather than NT status
 * values.
 */

#include <sys/errno.h>
#include <unistd.h>
#include <netdb.h>
#include <strings.h>
#include <time.h>
#include <tzfile.h>
#include <time.h>
#include <thread.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <smbsrv/smb_fsd.h>
#include <smbsrv/libsmb.h>
#include <smbsrv/libmlsvc.h>
#include <smbsrv/lmerr.h>
#include <smbsrv/nterror.h>
#include <smbsrv/nmpipes.h>
#include <smbsrv/cifs.h>
#include <smbsrv/netrauth.h>
#include <smbsrv/mlsvc.h>
#include <smbsrv/mlsvc_util.h>
#include <smbsrv/ndl/srvsvc.ndl>
#include <smbsrv/smb_common_door.h>

#define	SV_TYPE_SENT_BY_ME (SV_TYPE_WORKSTATION | SV_TYPE_SERVER | SV_TYPE_NT)

static DWORD mlsvc_NetSessionEnumLevel0(struct mslm_infonres *, DWORD,
    struct mlrpc_xaction *);
static DWORD mlsvc_NetSessionEnumLevel1(struct mslm_infonres *, DWORD,
    struct mlrpc_xaction *);
static DWORD mlsvc_NetShareEnumLevel0(struct mslm_infonres *, DWORD,
    struct mlrpc_xaction *, char);
static DWORD mlsvc_NetShareEnumLevel1(struct mslm_infonres *, DWORD,
    struct mlrpc_xaction *, char);
static DWORD mlsvc_NetShareEnumLevel2(struct mslm_infonres *, DWORD,
    struct mlrpc_xaction *, char);
static DWORD mlsvc_NetShareEnumLevel502(struct mslm_infonres *, DWORD,
    struct mlrpc_xaction *, char);
static DWORD mlsvc_NetShareEnumCommon(struct mlrpc_xaction *, DWORD,
    int, lmshare_info_t *, void *);
static int srvsvc_is_poweruser(struct mlrpc_xaction *);

static char empty_string[1];

static mlrpc_stub_table_t srvsvc_stub_table[];

static mlrpc_service_t srvsvc_service = {
	"SRVSVC",			/* name */
	"Server services",		/* desc */
	"\\srvsvc",			/* endpoint */
	PIPE_NTSVCS,			/* sec_addr_port */
	"4b324fc8-1670-01d3-12785a47bf6ee188", 3,	/* abstract */
	"8a885d04-1ceb-11c9-9fe808002b104860", 2,	/* transfer */
	0,				/* no bind_instance_size */
	0,				/* no bind_req() */
	0,				/* no unbind_and_close() */
	0,				/* use generic_call_stub() */
	&TYPEINFO(srvsvc_interface),	/* interface ti */
	srvsvc_stub_table		/* stub_table */
};

/*
 * srvsvc_fix_comment
 *
 * The parser sometimes has problems with empty strings so we
 * need to ensure that the comment field has something in it.
 */
static inline char *
srvsvc_fix_comment(char *original, char *alternative)
{
	if (original == 0 || strlen(original) == 0)
		return (alternative);

	return (original);
}

/*
 * srvsvc_share_mkpath
 *
 * Create the share path required by the share enum calls. This function
 * creates the path in a MLRPC heap buffer ready for use by the caller.
 *
 * Some Windows over-the-wire backup applications do not work unless a
 * drive letter is present in the share path. We don't care about the
 * drive letter since the path is fully qualified with the volume name.
 * We can try using drive B since by default that letter isn't assigned
 * and even if it conflicts, we should still be okay with the fully
 * qualified path.
 *
 * Windows clients seem to be mostly okay with the forward slash in
 * share paths but they cannot handle one immediately after the drive
 * letter, i.e. D:/. For consistency we convert all the slashes in
 * the path.
 *
 * Returns a pointer to a heap buffer containing the share path, which
 * could be a null pointer if the heap allocation fails.
 */
static char *
srvsvc_share_mkpath(struct mlrpc_xaction *mxa, char *path)
{
	char tmpbuf[MAXPATHLEN];
	char *p;

	if (strlen(path) == 0)
		return (MLRPC_HEAP_STRSAVE(mxa, path));

	/* strip the volume from the path (/vol1/home -> /home) */
	p = strchr(path[0] == '/' ? &path[1] : path, '/');

	(void) snprintf(tmpbuf, MAXPATHLEN, "%c:%s", 'B'
	    /* vattr.drive_letter */, p == NULL ? "/": p);
	(void) strsubst(tmpbuf, '/', '\\');

	return (MLRPC_HEAP_STRSAVE(mxa, tmpbuf));
}

/*
 * srvsvc_add_autohome
 *
 * Add the autohome share for the user to the shares' list
 * if autohome is enabled the share is not a permanent share.
 */
static int
srvsvc_add_autohome(struct mlrpc_xaction *mxa, char *username, DWORD i,
    int level, char *infop)
{
	lmshare_info_t si;
	DWORD status;

	if ((lmshare_getinfo(username, &si) == NERR_Success) &&
	    (si.mode & LMSHRM_TRANS)) {
		status = mlsvc_NetShareEnumCommon(mxa, i, level, &si,
		    (void *)infop);
		if (status == ERROR_SUCCESS)
			i++;
	}

	return (i);
}

/*
 * srvsvc_initialize
 *
 * This function registers the SRVSVC RPC interface with the RPC runtime
 * library. It must be called in order to use either the client side
 * or the server side functions.
 */
void
srvsvc_initialize(void)
{
	(void) mlrpc_register_service(&srvsvc_service);
}

/*
 * srvsvc_s_NetConnectEnum
 *
 * Under construction. This is just enough to get the interface working.
 * Current level 0 and level 1 connection info are supported.
 *
 * Level 1 request is made by 'srvmgr' (Server Manager)
 * utility of NT Server part of NT Domain to MLRPC server
 * while double click of share info icon. These values
 * are currectly virtual to MLRPC client and does't
 * reflect the real state of server.
 */
static int
srvsvc_s_NetConnectEnum(void *arg, struct mlrpc_xaction *mxa)
{
	struct mslm_NetConnectEnum *param = arg;
	struct mslm_NetConnectInfoBuf0 *ci0;
	struct mslm_NetConnectInfoBuf1 *ci1;
	DWORD status;

	status = ERROR_SUCCESS;
	switch (param->info.level) {
	case 0:
		ci0 = MLRPC_HEAP_NEW(mxa, struct mslm_NetConnectInfoBuf0);
		if (ci0 == 0) {
			status = ERROR_NOT_ENOUGH_MEMORY;
			break;
		}
		ci0->coni0_id = 0x17;

		param->info.ru.info0
		    = MLRPC_HEAP_NEW(mxa, struct mslm_NetConnectInfo0);

		if (param->info.ru.info0 == 0) {
			status = ERROR_NOT_ENOUGH_MEMORY;
			break;
		}
		param->info.ru.info0->ci0 = ci0;
		param->info.ru.info0->entries_read = 1;

		param->total_entries = 1;
		param->resume_handle = 0;
		break;

	case 1:
		ci1 = MLRPC_HEAP_NEW(mxa, struct mslm_NetConnectInfoBuf1);
		if (ci1 == 0) {
			status = ERROR_NOT_ENOUGH_MEMORY;
			break;
		}
		ci1->coni1_id = 0x17;
		ci1->coni1_type = STYPE_IPC;
		ci1->coni1_num_opens = 1;
		ci1->coni1_num_users = 1;
		ci1->coni1_time = 16;
		ci1->coni1_username =
		    (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, "Administrator");

		ci1->coni1_netname =
		    (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, "IPC$");

		param->info.ru.info1 = MLRPC_HEAP_NEW(mxa,
		    struct mslm_NetConnectInfo1);

		if (param->info.ru.info1 == 0) {
			status = ERROR_NOT_ENOUGH_MEMORY;
			break;
		}
		param->info.ru.info1->ci1 = ci1;
		param->info.ru.info1->entries_read = 1;

		param->total_entries = 1;
		param->resume_handle = 0;
		break;

	default:
		status = ERROR_ACCESS_DENIED;
		break;
	}

	if (status != ERROR_SUCCESS)
		bzero(param, sizeof (struct mslm_NetConnectEnum));

	param->status = status;
	return (MLRPC_DRC_OK);
}


/*
 * srvsvc_s_NetFileEnum
 *
 * Under construction. The values used here are fictional values and
 * bear no relation to any real values, living or otherwise. I just
 * made them up to get the interface working.
 */
static int
srvsvc_s_NetFileEnum(void *arg, struct mlrpc_xaction *mxa)
{
	struct mslm_NetFileEnum *param = arg;
	struct mslm_NetFileInfoBuf3 *fi3;

	if (param->info.switch_value != 3) {
		bzero(param, sizeof (struct mslm_NetFileEnum));
		param->status = ERROR_INVALID_LEVEL;
		return (MLRPC_DRC_OK);
	}

	fi3 = MLRPC_HEAP_NEW(mxa, struct mslm_NetFileInfoBuf3);
	if (fi3 == 0) {
		bzero(param, sizeof (struct mslm_NetFileEnum));
		param->status = ERROR_NOT_ENOUGH_MEMORY;
		return (MLRPC_DRC_OK);
	}

	fi3->fi3_id = 0xF5;
	fi3->fi3_permissions = 0x23;
	fi3->fi3_num_locks = 0;
	fi3->fi3_pathname =
	    (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, "\\PIPE\\srvsvc");

	fi3->fi3_username =
	    (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, "Administrator");

	param->info.ru.info3 = MLRPC_HEAP_NEW(mxa, struct mslm_NetFileInfo3);
	if (param->info.ru.info3 == 0) {
		bzero(param, sizeof (struct mslm_NetFileEnum));
		param->status = ERROR_NOT_ENOUGH_MEMORY;
		return (MLRPC_DRC_OK);
	}

	param->info.ru.info3->fi3 = fi3;
	param->info.ru.info3->entries_read = 1;
	param->total_entries = 1;

	if (param->resume_handle)
		*param->resume_handle = 0x5F;

	param->status = ERROR_SUCCESS;
	return (MLRPC_DRC_OK);
}


/*
 * srvsvc_s_NetFileClose
 *
 * Under construction. This is just enough to get the interface working.
 */
/*ARGSUSED*/
static int
srvsvc_s_NetFileClose(void *arg, struct mlrpc_xaction *mxa)
{
	struct mslm_NetFileClose *param = arg;

	bzero(param, sizeof (struct mslm_NetFileClose));
	param->status = ERROR_SUCCESS;
	return (MLRPC_DRC_OK);
}


/*
 * srvsvc_s_NetShareGetInfo
 *
 * This call is made by Windows2000 to get share information. There are
 * probably other information levels but these are the only ones I've
 * seen so far.
 *
 * Returns Win32 error codes.
 */
static int
srvsvc_s_NetShareGetInfo(void *arg, struct mlrpc_xaction *mxa)
{
	struct mlsm_NetShareGetInfo *param = arg;
	struct mslm_NetShareGetInfo0 *info0;
	struct mslm_NetShareGetInfo1 *info1;
	struct mslm_NetShareGetInfo2 *info2;
	struct mslm_NetShareGetInfo502 *info502;
	struct mslm_NetShareGetInfo1005 *info1005;
	struct lmshare_info si;
	char shr_comment[LMSHR_COMMENT_MAX];
	DWORD status;

	status = lmshare_getinfo((char *)param->netname, &si);
	if (status != NERR_Success) {
		if (strcasecmp((const char *)param->netname, "IPC$") == 0) {
			/*
			 * Windows clients don't send the \\PIPE path for IPC$.
			 */
			(void) memset(&si, 0, sizeof (lmshare_info_t));
			(void) strcpy(si.share_name, "IPC$");
			(void) strcpy(si.comment, "Remote IPC");
			si.stype = (int)(STYPE_IPC | STYPE_SPECIAL);
		} else {
			bzero(param, sizeof (struct mlsm_NetShareGetInfo));
			param->status = status;
			return (MLRPC_DRC_OK);
		}
	}

	if (si.comment && strlen(si.comment))
		(void) snprintf(shr_comment, sizeof (shr_comment), "%s %s",
		    si.directory, si.comment);
	else
		(void) strcpy(shr_comment, si.directory);

	status = ERROR_SUCCESS;

	switch (param->level) {
	case 0:
		info0 = MLRPC_HEAP_NEW(mxa, struct mslm_NetShareGetInfo0);
		if (info0 == 0) {

			status = ERROR_NOT_ENOUGH_MEMORY;
			break;
		}
		info0->shi0_netname
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, si.share_name);

		param->result.ru.info0 = info0;
		break;

	case 1:
		info1 = MLRPC_HEAP_NEW(mxa, struct mslm_NetShareGetInfo1);
		if (info1 == 0) {
			status = ERROR_NOT_ENOUGH_MEMORY;
			break;
		}
		info1->shi1_netname =
		    (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, si.share_name);
		info1->shi1_comment =
		    (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, shr_comment);
		info1->shi1_type = si.stype;
		param->result.ru.info1 = info1;
		break;

	case 2:
		info2 = MLRPC_HEAP_NEW(mxa, struct mslm_NetShareGetInfo2);
		if (info2 == 0) {
			status = ERROR_NOT_ENOUGH_MEMORY;
			break;
		}
		info2->shi2_netname =
		    (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, si.share_name);
		info2->shi2_comment =
		    (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, shr_comment);
		info2->shi2_path =
		    (unsigned char *)srvsvc_share_mkpath(mxa, si.directory);
		info2->shi2_passwd = 0;
		info2->shi2_type = si.stype;
		info2->shi2_permissions = 0;
		info2->shi2_max_uses = SHI_USES_UNLIMITED;
		info2->shi2_current_uses = 0;
		param->result.ru.info2 = info2;
		break;

	case 1005:
		info1005 = MLRPC_HEAP_NEW(mxa, struct mslm_NetShareGetInfo1005);
		if (info1005 == 0) {

			status = ERROR_NOT_ENOUGH_MEMORY;
			break;
		}
		info1005->shi1005_flags = 0;
		param->result.ru.info1005 = info1005;
		break;

	case 502:
		/*
		 * Level 502 provides level 2 information plus a
		 * security descriptor. We don't support security
		 * descriptors on shares yet.
		 */
		info502 = MLRPC_HEAP_NEW(mxa, struct mslm_NetShareGetInfo502);
		if (info502 == 0) {
			status = ERROR_NOT_ENOUGH_MEMORY;
			break;
		}
		info502->shi502_netname =
		    (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, si.share_name);
		info502->shi502_comment =
		    (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, shr_comment);
		info502->shi502_path =
		    (unsigned char *)srvsvc_share_mkpath(mxa, si.directory);
		info502->shi502_passwd = 0;
		info502->shi502_type = si.stype;
		info502->shi502_permissions = 0;
		info502->shi502_max_uses = SHI_USES_UNLIMITED;
		info502->shi502_current_uses = 0;
		info502->shi502_reserved = 0;
		info502->shi502_security_descriptor = 0;
		param->result.ru.info502 = info502;
		break;

	default:
		status = ERROR_ACCESS_DENIED;
		break;
	}

	if (status != ERROR_SUCCESS)
		bzero(param, sizeof (struct mlsm_NetShareGetInfo));
	else
		param->result.switch_value = param->level;

	param->status = status;
	return (MLRPC_DRC_OK);
}


/*
 * srvsvc_s_NetShareSetInfo
 *
 * This call is made by SrvMgr to set share information.
 * Always returns ERROR_ACCESS_DENIED for now.
 *
 * Returns Win32 error codes.
 */
static int
srvsvc_s_NetShareSetInfo(void *arg, struct mlrpc_xaction *mxa)
{
	struct mlsm_NetShareSetInfo *param = arg;

	(void) memset(param, 0, sizeof (struct mlsm_NetShareSetInfo));
	param->parm_err_ptr = (DWORD)(uintptr_t)MLRPC_HEAP_MALLOC(mxa,
	    sizeof (DWORD));
	param->parm_err = 0;

	smb_config_rdlock();
	if (smb_config_getyorn(SMB_CI_SRVSVC_SHRSET_ENABLE) != 0)
		param->status = ERROR_SUCCESS;
	else
		param->status = ERROR_ACCESS_DENIED;
	smb_config_unlock();

	return (MLRPC_DRC_OK);
}

/*
 * srvsvc_s_NetSessionEnum
 *
 * Level 1 request is made by the 'srvmgr' (Server Manager) utility on
 * NT Server when the user info icon is selected.
 *
 * Return Values
 * If the function succeeds, the return value is NERR_Success.
 * If the function fails, the return value can be one of the following
 * error codes:
 *
 * ERROR_ACCESS_DENIED      The user does not have access to the requested
 *                          information.
 * ERROR_INVALID_LEVEL      The value specified for the level parameter is
 *                          invalid.
 * ERROR_INVALID_PARAMETER  The specified parameter is invalid.
 * ERROR_MORE_DATA	    More entries are available. Specify a large
 *                          enough buffer to receive all entries.
 * ERROR_NOT_ENOUGH_MEMORY  Insufficient memory is available.
 * NERR_ClientNameNotFound  A session does not exist with the computer
 *                          name.
 * NERR_InvalidComputer     The computer name is invalid.
 * NERR_UserNotFound        The user name could not be found.
 */
static int
srvsvc_s_NetSessionEnum(void *arg, struct mlrpc_xaction *mxa)
{
	struct mslm_NetSessionEnum *param = arg;
	struct mslm_infonres *infonres;
	DWORD status;
	DWORD n_sessions;

	infonres = MLRPC_HEAP_NEW(mxa, struct mslm_infonres);
	if (infonres == 0) {
		bzero(param, sizeof (struct mslm_NetSessionEnum));
		param->status = ERROR_NOT_ENOUGH_MEMORY;
		return (MLRPC_DRC_OK);
	}

	infonres->entriesread = 0;
	infonres->entries = 0;
	param->result.level = param->level;
	param->result.bufptr.p = infonres;
	param->total_entries = 1;
	param->status = ERROR_SUCCESS;

	n_sessions = (DWORD) smb_dwncall_user_num();

	switch (param->level) {
	case 0:
		status = mlsvc_NetSessionEnumLevel0(infonres, n_sessions, mxa);
		break;

	case 1:
		status = mlsvc_NetSessionEnumLevel1(infonres, n_sessions, mxa);
		break;

	default:
		status = ERROR_INVALID_LEVEL;
		break;
	}

	if (status != 0) {
		bzero(param, sizeof (struct mslm_NetSessionEnum));
		param->status = status;
		return (MLRPC_DRC_OK);
	}

	param->resume_handle = 0;
	param->total_entries = infonres->entriesread;
	param->status = status;
	return (MLRPC_DRC_OK);
}

/*
 * mlsvc_NetSessionEnumLevel0
 *
 * Build the level 0 session information.
 */
/*ARGSUSED*/
static DWORD
mlsvc_NetSessionEnumLevel0(struct mslm_infonres *infonres, DWORD n_sessions,
    struct mlrpc_xaction *mxa)
{
	struct mslm_SESSION_INFO_0 *info0;
	smb_dr_ulist_t *ulist;
	smb_dr_user_ctx_t *user;
	char *workstation;
	char ipaddr_buf[INET_ADDRSTRLEN];
	int i, offset, cnt, total;

	info0 = MLRPC_HEAP_NEWN(mxa, struct mslm_SESSION_INFO_0, n_sessions);
	if (info0 == 0)
		return (ERROR_NOT_ENOUGH_MEMORY);

	ulist = malloc(sizeof (smb_dr_ulist_t));
	if (!ulist)
		return (ERROR_NOT_ENOUGH_MEMORY);

	for (total = 0, offset = 0;
	    (cnt = smb_dwncall_get_users(offset, ulist)) > 0;
	    offset += cnt) {
		for (i = 0; i < cnt && total < n_sessions; i++, total++) {
			user = &ulist->dul_users[i];
			/*
			 * Ignore local tokens (IP address is zero).
			 */
			if (user->du_ipaddr == 0) {
				total--;
				smb_dr_ulist_free(ulist);
				ulist = malloc(sizeof (smb_dr_ulist_t));
				if (!ulist)
					return (ERROR_NOT_ENOUGH_MEMORY);
				continue;
			}

			if ((workstation = user->du_workstation) == 0) {
				(void) inet_ntop(AF_INET,
				    (char *)&user->du_ipaddr, ipaddr_buf,
				    sizeof (ipaddr_buf));
				workstation = ipaddr_buf;
			}

			info0[total].sesi0_cname = MLRPC_HEAP_STRSAVE(mxa,
			    workstation);
			if (info0[total].sesi0_cname == 0) {
				smb_dr_ulist_free(ulist);
				return (ERROR_NOT_ENOUGH_MEMORY);
			}

		}
		smb_dr_ulist_free(ulist);
		ulist = malloc(sizeof (smb_dr_ulist_t));
		if (!ulist)
			return (ERROR_NOT_ENOUGH_MEMORY);

	}

	infonres->entriesread = total;
	infonres->entries = info0;
	return (ERROR_SUCCESS);
}


/*
 * mlsvc_NetSessionEnumLevel1
 *
 * Build the level 1 session information.
 */
/*ARGSUSED*/
static DWORD
mlsvc_NetSessionEnumLevel1(struct mslm_infonres *infonres, DWORD n_sessions,
    struct mlrpc_xaction *mxa)
{
	struct mslm_SESSION_INFO_1 *info1;
	smb_dr_ulist_t *ulist;
	smb_dr_user_ctx_t *user;
	char *workstation;
	char *account;
	char ipaddr_buf[INET_ADDRSTRLEN];
	int i, offset, cnt, total;

	info1 = MLRPC_HEAP_NEWN(mxa, struct mslm_SESSION_INFO_1, n_sessions);
	if (info1 == 0)
		return (ERROR_NOT_ENOUGH_MEMORY);

	ulist = malloc(sizeof (smb_dr_ulist_t));
	if (!ulist)
		return (ERROR_NOT_ENOUGH_MEMORY);

	for (total = 0, offset = 0;
	    (cnt = smb_dwncall_get_users(offset, ulist)) > 0;
	    offset += cnt) {
		for (i = 0; i < cnt && total < n_sessions; i++, total++) {
			user = &ulist->dul_users[i];
			/*
			 * Ignore local user_ctxs (IP address is zero).
			 */
			if (user->du_ipaddr == 0) {
				total--;
				smb_dr_ulist_free(ulist);
				ulist = malloc(sizeof (smb_dr_ulist_t));
				if (!ulist)
					return (ERROR_NOT_ENOUGH_MEMORY);
				continue;
			}

			if ((workstation = user->du_workstation) == 0) {
				(void) inet_ntop(AF_INET,
				    (char *)&user->du_ipaddr,
				    ipaddr_buf, sizeof (ipaddr_buf));
				workstation = ipaddr_buf;
			}

			if ((account = user->du_account) == 0)
				account = "Unknown";

			info1[total].sesi1_cname = MLRPC_HEAP_STRSAVE(mxa,
			    workstation);
			info1[total].sesi1_uname = MLRPC_HEAP_STRSAVE(mxa,
			    account);

			if (info1[total].sesi1_cname == 0 ||
			    info1[total].sesi1_uname == 0) {
				smb_dr_ulist_free(ulist);
				return (ERROR_NOT_ENOUGH_MEMORY);
			}

			info1[total].sesi1_nopens = 1;
			info1[total].sesi1_time = time(0) -
			    user->du_logon_time;
			info1[total].sesi1_itime = 0;
			info1[total].sesi1_uflags =
			    (user->du_flags & SMB_ATF_GUEST) ? SESS_GUEST : 0;
		}
		smb_dr_ulist_free(ulist);
		ulist = malloc(sizeof (smb_dr_ulist_t));
		if (!ulist)
			return (ERROR_NOT_ENOUGH_MEMORY);
	}

	infonres->entriesread = total;
	infonres->entries = info1;
	return (ERROR_SUCCESS);
}

/*
 * srvsvc_s_NetSessionDel
 *
 * Ends a network session between a server and a workstation.
 * On NT only members of the Administrators or Account Operators
 * local groups are permitted to use NetSessionDel.
 *
 * Return Values
 * If the function succeeds, the return value is NERR_Success/
 * ERROR_SUCCESS. If the function fails, the return value can be
 * one of the following error codes:
 *
 * ERROR_ACCESS_DENIED 		The user does not have access to the
 * 							requested information.
 * ERROR_INVALID_PARAMETER	The specified parameter is invalid.
 * ERROR_NOT_ENOUGH_MEMORY	Insufficient memory is available.
 * NERR_ClientNameNotFound	A session does not exist with that
 *                          computer name.
 */
static int
srvsvc_s_NetSessionDel(void *arg, struct mlrpc_xaction *mxa)
{
	struct mslm_NetSessionDel *param = arg;

	if (srvsvc_is_poweruser(mxa) == 0) {
		param->status = ERROR_ACCESS_DENIED;
		return (MLRPC_DRC_OK);
	}

	param->status = ERROR_ACCESS_DENIED;
	return (MLRPC_DRC_OK);
}

/*
 * SRVSVC NetServerGetInfo
 *
 *	IN	LPTSTR servername,
 *	IN	DWORD level,
 *	OUT	union switch(level) {
 *		case 100:	mslm_SERVER_INFO_100 *p100;
 *		case 101:	mslm_SERVER_INFO_101 *p101;
 *		case 102:	mslm_SERVER_INFO_102 *p102;
 *		default:	char *nullptr;
 *		} bufptr,
 *	OUT	DWORD status
 */
static int
srvsvc_s_NetServerGetInfo(void *arg, struct mlrpc_xaction *mxa)
{
	struct mslm_NetServerGetInfo *param = arg;
	struct mslm_SERVER_INFO_100 *info100;
	struct mslm_SERVER_INFO_101 *info101;
	struct mslm_SERVER_INFO_102 *info102;
	char *sys_comment;
	char hostname[MAXHOSTNAMELEN];

	if (smb_gethostname(hostname, MAXHOSTNAMELEN, 1) != 0) {
netservergetinfo_no_memory:
		bzero(param, sizeof (struct mslm_NetServerGetInfo));
		return (ERROR_NOT_ENOUGH_MEMORY);
	}

	smb_config_rdlock();
	sys_comment = smb_config_getstr(SMB_CI_SYS_CMNT);
	sys_comment = srvsvc_fix_comment(sys_comment, " ");
	smb_config_unlock();

	switch (param->level) {
	case 100:
		info100 = MLRPC_HEAP_NEW(mxa, struct mslm_SERVER_INFO_100);
		if (info100 == 0)
			goto netservergetinfo_no_memory;

		bzero(info100, sizeof (struct mslm_SERVER_INFO_100));
		info100->sv100_platform_id = SV_PLATFORM_ID_NT;
		info100->sv100_name
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, hostname);

		if (info100->sv100_name == 0)
			goto netservergetinfo_no_memory;

		param->result.bufptr.bufptr100 = info100;
		break;

	case 101:
		info101 = MLRPC_HEAP_NEW(mxa, struct mslm_SERVER_INFO_101);
		if (info101 == 0)
			goto netservergetinfo_no_memory;

		bzero(info101, sizeof (struct mslm_SERVER_INFO_101));
		info101->sv101_platform_id = SV_PLATFORM_ID_NT;
		info101->sv101_version_major = 4;
		info101->sv101_version_minor = 0;
		info101->sv101_type = SV_TYPE_SENT_BY_ME;
		info101->sv101_name
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, hostname);

		info101->sv101_comment
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, sys_comment);

		if (info101->sv101_name == 0 || info101->sv101_comment == 0)
			goto netservergetinfo_no_memory;

		param->result.bufptr.bufptr101 = info101;
		break;

	case 102:
		info102 = MLRPC_HEAP_NEW(mxa, struct mslm_SERVER_INFO_102);
		if (info102 == 0)
			goto netservergetinfo_no_memory;

		bzero(info102, sizeof (struct mslm_SERVER_INFO_102));
		info102->sv102_platform_id = SV_PLATFORM_ID_NT;
		info102->sv102_version_major = 4;
		info102->sv102_version_minor = 0;
		info102->sv102_type = SV_TYPE_SENT_BY_ME;
		info102->sv102_name
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, hostname);

		info102->sv102_comment
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, sys_comment);

		/*
		 * The following level 102 fields are defaulted to zero
		 * by virtue of the call to bzero above.
		 *
		 * sv102_users
		 * sv102_disc
		 * sv102_hidden
		 * sv102_announce
		 * sv102_anndelta
		 * sv102_licenses
		 * sv102_userpath
		 */
		if (info102->sv102_name == 0 || info102->sv102_comment == 0)
			goto netservergetinfo_no_memory;

		param->result.bufptr.bufptr102 = info102;
		break;

	default:
		bzero(&param->result,
		    sizeof (struct mslm_NetServerGetInfo_result));
		param->status = ERROR_ACCESS_DENIED;
		return (MLRPC_DRC_OK);
	}

	param->result.level = param->level;
	param->status = (ERROR_SUCCESS);
	return (MLRPC_DRC_OK);
}

/*
 * NetRemoteTOD
 *
 * Returns information about the time of day on this server.
 *
 * typedef struct _TIME_OF_DAY_INFO {
 *	DWORD tod_elapsedt;  // seconds since 00:00:00 January 1 1970 GMT
 *	DWORD tod_msecs;     // arbitrary milliseconds (since reset)
 *	DWORD tod_hours;     // current hour [0-23]
 *	DWORD tod_mins;      // current minute [0-59]
 *	DWORD tod_secs;      // current second [0-59]
 *	DWORD tod_hunds;     // current hundredth (0.01) second [0-99]
 *	LONG tod_timezone;   // time zone of the server
 *	DWORD tod_tinterval; // clock tick time interval
 *	DWORD tod_day;       // day of the month [1-31]
 *	DWORD tod_month;     // month of the year [1-12]
 *	DWORD tod_year;      // current year
 *	DWORD tod_weekday;   // day of the week since sunday [0-6]
 * } TIME_OF_DAY_INFO;
 *
 * The time zone of the server is calculated in minutes from Greenwich
 * Mean Time (GMT). For time zones west of Greenwich, the value is
 * positive; for time zones east of Greenwich, the value is negative.
 * A value of -1 indicates that the time zone is undefined.
 *
 * The clock tick value represents a resolution of one ten-thousandth
 * (0.0001) second.
 */
static int
srvsvc_s_NetRemoteTOD(void *arg, struct mlrpc_xaction *mxa)
{
	struct mslm_NetRemoteTOD *param = arg;
	struct mslm_TIME_OF_DAY_INFO *tod;
	struct timeval		time_val;
	struct tm		tm;

	(void) gettimeofday(&time_val, 0);
	(void) gmtime_r(&time_val.tv_sec, &tm);

	tod = MLRPC_HEAP_NEW(mxa, struct mslm_TIME_OF_DAY_INFO);
	if (tod == NULL) {
		bzero(param, sizeof (struct mslm_NetRemoteTOD));
		return (ERROR_NOT_ENOUGH_MEMORY);
	}

	tod->tod_elapsedt = time_val.tv_sec;
	tod->tod_msecs = time_val.tv_usec;
	tod->tod_hours = tm.tm_hour;
	tod->tod_mins = tm.tm_min;
	tod->tod_secs = tm.tm_sec;
	tod->tod_hunds = 0;
	tod->tod_tinterval = 1000;
	tod->tod_day = tm.tm_mday;
	tod->tod_month = tm.tm_mon+1;
	tod->tod_year = tm.tm_year+1900;
	tod->tod_weekday = tm.tm_wday;

	(void) localtime_r(&time_val.tv_sec, &tm);

	param->bufptr = tod;
	param->status = ERROR_SUCCESS;
	return (MLRPC_DRC_OK);
}

/*
 * srvsvc_s_NetNameValidate
 *
 * Perform name validation.
 * I've observed that the Computer Management Windows Application
 * always send this request with type=0x09 and the flags=0 when
 * attempting to validate a share name.
 *
 * The share name is consider invalid if it contains any of the
 * following character (as mentioned in MSDN article #236388).
 *
 * " / \ [ ] : | < > + ; , ? * =
 *
 *
 * For now, if the type is other than 0x09, return access denied.
 *
 * Returns Win32 error codes.
 */
/*ARGSUSED*/
static int
srvsvc_s_NetNameValidate(void *arg, struct mlrpc_xaction *mxa)
{
	struct mslm_NetNameValidate *param = arg;

	switch (param->type) {
	case 0x09:
		param->status = lmshare_is_valid((char *)param->pathname) ?
		    ERROR_SUCCESS : ERROR_INVALID_NAME;
		break;

	default:
		param->status = ERROR_ACCESS_DENIED;
		break;
	}

	return (MLRPC_DRC_OK);
}

/*
 * srvsvc_s_NetShareAdd
 *
 * Add a new share. We support info levels 2 and 502 but ignore the
 * security descriptor in level 502 requests. Only the administrator,
 * or a member of the domain administrators group, is allowed to add
 * shares.
 *
 * This interface is used by the rmtshare command from the NT resource
 * kit. Rmtshare allows a client to add or remove shares on a server
 * from the client's command line.
 *
 * Note that we don't support security descriptors on a share. If the
 * /grant is used, the share will be created but the subsequent attempt
 * to manipulate the security descriptor (NetShareGetInfo) will fail.
 * Similarly for the /remove option.
 *
 * Returns Win32 error codes.
 */
static int
srvsvc_s_NetShareAdd(void *arg, struct mlrpc_xaction *mxa)
{
	static DWORD parm_err = 0;
	DWORD parm_stat;
	struct mslm_NetShareAdd *param = arg;
	smb_dr_user_ctx_t *user_ctx;
	struct mslm_SHARE_INFO_2 *info2;
	struct lmshare_info si;
	char realpath[MAXPATHLEN];

	user_ctx = mxa->context->user_ctx;

	if (srvsvc_is_poweruser(mxa) == 0) {
		bzero(param, sizeof (struct mslm_NetShareAdd));
		param->status = ERROR_ACCESS_DENIED;
		return (MLRPC_DRC_OK);
	}

	switch (param->level) {
	case 2:
		info2 = param->info.un.info2;
		break;

	case 502:
		info2 = (struct mslm_SHARE_INFO_2 *)param->info.un.info502;
		break;

	default:
		bzero(param, sizeof (struct mslm_NetShareAdd));
		param->status = ERROR_ACCESS_DENIED;
		return (MLRPC_DRC_OK);
	}

	if (info2->shi2_netname == 0 || info2->shi2_path == 0) {
		bzero(param, sizeof (struct mslm_NetShareAdd));
		param->status = NERR_NetNameNotFound;
		return (MLRPC_DRC_OK);
	}

	if (lmshare_is_restricted((char *)info2->shi2_netname)) {
		bzero(param, sizeof (struct mslm_NetShareAdd));
		param->status = ERROR_ACCESS_DENIED;
		return (MLRPC_DRC_OK);
	}

	if (info2->shi2_remark == 0)
		info2->shi2_remark = (unsigned char *)"";

	/*
	 * Derive the real path which will be stored in the
	 * directory field of the lmshare_info_t structure
	 * from the path field in this RPC request.
	 */
	parm_stat = lmshare_get_realpath((const char *)info2->shi2_path,
	    realpath, MAXPATHLEN);

	if (parm_stat != NERR_Success) {
		bzero(param, sizeof (struct mslm_NetShareAdd));
		param->status = parm_stat;
		param->parm_err
		    = (user_ctx->du_native_os == NATIVE_OS_WIN95) ?
		    0 : &parm_err;
		return (MLRPC_DRC_OK);
	}

	(void) memset(&si, 0, sizeof (lmshare_info_t));
	(void) strlcpy(si.share_name, (const char *)info2->shi2_netname,
	    MAXNAMELEN);

	(void) strlcpy(si.directory, realpath, MAXPATHLEN);
	(void) strlcpy(si.comment, (const char *)info2->shi2_remark,
	    LMSHR_COMMENT_MAX);

	si.mode = LMSHRM_PERM;

	param->status = lmshare_add(&si, 1);
	param->parm_err = (user_ctx->du_native_os == NATIVE_OS_WIN95) ?
	    0 : &parm_err;
	return (MLRPC_DRC_OK);
}

/*
 * srvsvc_is_poweruser
 *
 * Check whether or not the specified user has power-user privileges,
 * i.e. is a member of the Domain Admins, Administrators or Power
 * Users groups. This is typically required for operations such as
 * adding/deleting shares.
 *
 * Returns 1 if the user is a power user, otherwise returns 0.
 */
static int
srvsvc_is_poweruser(struct mlrpc_xaction *mxa)
{
	smb_dr_user_ctx_t *user = mxa->context->user_ctx;

	return ((user->du_flags & SMB_ATF_ADMIN) ||
	    (user->du_flags & SMB_ATF_POWERUSER));
}

/*
 * srvsvc_s_NetShareEnum
 *
 * Request for various levels of information about our shares.
 * Level 0: just the share names.
 * Level 1: the share name, the share type and the comment field.
 * Level 2: everything that we know about the shares.
 */
static int
srvsvc_s_NetShareEnum(void *arg, struct mlrpc_xaction *mxa)
{
	struct mslm_NetShareEnum *param = arg;
	struct mslm_infonres *infonres;
	DWORD status;
	DWORD n_shares;

	infonres = MLRPC_HEAP_NEW(mxa, struct mslm_infonres);
	if (infonres == 0) {
		bzero(param, sizeof (struct mslm_NetShareEnum));
		param->status = ERROR_NOT_ENOUGH_MEMORY;
		return (MLRPC_DRC_OK);
	}

	infonres->entriesread = 0;
	infonres->entries = 0;
	param->result.level = param->level;
	param->result.bufptr.p = infonres;
	param->totalentries = 1; /* NT stream hint value: prefmaxlen? */
	param->status = ERROR_SUCCESS;

	n_shares = lmshare_num_shares();

	switch (param->level) {
	case 0:
		status = mlsvc_NetShareEnumLevel0(infonres, n_shares, mxa, 0);
		break;

	case 1:
		status = mlsvc_NetShareEnumLevel1(infonres, n_shares, mxa, 0);
		break;

	case 2:
		status = mlsvc_NetShareEnumLevel2(infonres, n_shares, mxa, 0);
		break;

	case 502:
		status = mlsvc_NetShareEnumLevel502(infonres, n_shares, mxa, 0);
		break;

	default:
		status = ERROR_INVALID_PARAMETER;
		break;
	}

	if (status != 0) {
		bzero(param, sizeof (struct mslm_NetShareEnum));
		param->status = status;
		return (MLRPC_DRC_OK);
	}

	param->resume_handle = 0;
	param->totalentries = infonres->entriesread;
	param->status = status;
	return (MLRPC_DRC_OK);
}


/*
 * srvsvc_s_NetShareEnumSticky
 *
 * Request for various levels of information about our shares.
 * Level 0: just the share names.
 * Level 1: the share name, the share type and the comment field.
 * Level 2: everything that we know about the shares.
 *
 * NetShareEnumSticky is the same as NetShareEnum except that hidden
 * shares are not returned. This call was apparently added due to a
 * bug in the NT implementation of NetShareEnum - it didn't process
 * the resume handle correctly so that attempts to enumerate large
 * share lists resulted in an infinite loop.
 */
static int
srvsvc_s_NetShareEnumSticky(void *arg, struct mlrpc_xaction *mxa)
{
	struct mslm_NetShareEnum *param = arg;
	struct mslm_infonres *infonres;
	DWORD resume_handle;
	DWORD status;
	DWORD n_shares;

	infonres = MLRPC_HEAP_NEW(mxa, struct mslm_infonres);
	if (infonres == 0) {
		bzero(param, sizeof (struct mslm_NetShareEnum));
		param->status = ERROR_NOT_ENOUGH_MEMORY;
		return (MLRPC_DRC_OK);
	}

	infonres->entriesread = 0;
	infonres->entries = 0;
	param->result.level = param->level;
	param->result.bufptr.p = infonres;
	param->totalentries = 1; /* NT stream hint value: prefmaxlen? */
	param->status = ERROR_SUCCESS;

	n_shares = lmshare_num_shares();

	if (param->resume_handle)
		resume_handle = *param->resume_handle;
	else
		resume_handle = 0;

	switch (param->level) {
	case 0:
		status = mlsvc_NetShareEnumLevel0(infonres, n_shares, mxa, 1);
		break;

	case 1:
		status = mlsvc_NetShareEnumLevel1(infonres, n_shares, mxa, 1);
		break;

	case 2:
		status = mlsvc_NetShareEnumLevel2(infonres, n_shares, mxa, 1);
		break;

	case 502:
		status = mlsvc_NetShareEnumLevel502(infonres, n_shares, mxa, 1);
		break;

	default:
		status = ERROR_INVALID_PARAMETER;
		break;
	}

	if (status != 0) {
		bzero(param, sizeof (struct mslm_NetShareEnum));
		param->status = status;
		return (MLRPC_DRC_OK);
	}

	if (param->resume_handle)
		*param->resume_handle = resume_handle;
	param->totalentries = infonres->entriesread;
	param->status = status;
	return (MLRPC_DRC_OK);
}



/*
 * mlsvc_NetShareEnumLevel0
 *
 * Build the level 0 share information. The list should have been built
 * before we got here so all we have to do is copy the share names to
 * the response heap and setup the infonres values.
 */
static DWORD
mlsvc_NetShareEnumLevel0(struct mslm_infonres *infonres, DWORD n_shares,
    struct mlrpc_xaction *mxa, char sticky)
{
	struct mslm_SHARE_INFO_0 *info0;
	lmshare_iterator_t *iterator;
	lmshare_info_t *si;
	DWORD i;
	DWORD status;
	smb_dr_user_ctx_t *user_ctx = mxa->context->user_ctx;

	info0 = MLRPC_HEAP_NEWN(mxa, struct mslm_SHARE_INFO_0, n_shares);
	if (info0 == 0) {
		status = ERROR_NOT_ENOUGH_MEMORY;
		return (status);
	}

	iterator = lmshare_open_iterator(LMSHRM_ALL);
	if (iterator == NULL) {
		status = ERROR_NOT_ENOUGH_MEMORY;
		return (status);
	}

	i = 0;
	while ((si = lmshare_iterate(iterator)) != 0) {
		if (sticky && (si->stype & STYPE_SPECIAL))
			continue;

		if (smb_is_autohome(si))
			continue;

		status = mlsvc_NetShareEnumCommon(mxa, i, 0, si,
		    (void *)info0);

		if (status != ERROR_SUCCESS)
			break;

		i++;
	}

	i = srvsvc_add_autohome(mxa, user_ctx->du_account, i, 0, (char *)info0);

	lmshare_close_iterator(iterator);

	infonres->entriesread = i;
	infonres->entries = info0;
	return (ERROR_SUCCESS);
}


/*
 * mlsvc_NetShareEnumLevel1
 *
 * Build the level 1 share information. The list should have been built
 * before we arrived here so all we have to do is copy the share info
 * to the response heap and setup the infonres values. The only thing
 * to be aware of here is that there are minor difference between the
 * various share types.
 */
static DWORD
mlsvc_NetShareEnumLevel1(struct mslm_infonres *infonres, DWORD n_shares,
    struct mlrpc_xaction *mxa, char sticky)
{
	struct mslm_SHARE_INFO_1 *info1;
	lmshare_iterator_t *iterator;
	lmshare_info_t *si;
	DWORD i;
	smb_dr_user_ctx_t *user_ctx = mxa->context->user_ctx;

	info1 = MLRPC_HEAP_NEWN(mxa, struct mslm_SHARE_INFO_1, n_shares);
	if (info1 == 0)
		return (ERROR_NOT_ENOUGH_MEMORY);

	iterator = lmshare_open_iterator(LMSHRM_ALL);
	if (iterator == NULL)
		return (ERROR_NOT_ENOUGH_MEMORY);

	i = 0;
	while ((si = lmshare_iterate(iterator)) != 0) {
		if (sticky && (si->stype & STYPE_SPECIAL))
			continue;

		if (smb_is_autohome(si))
			continue;

		if (mlsvc_NetShareEnumCommon(mxa, i, 1, si,
		    (void *)info1) != ERROR_SUCCESS)
			break;
		i++;
	}

	i = srvsvc_add_autohome(mxa, user_ctx->du_account, i, 1, (char *)info1);

	lmshare_close_iterator(iterator);

	infonres->entriesread = i;
	infonres->entries = info1;
	return (ERROR_SUCCESS);
}

/*
 * mlsvc_NetShareEnumLevel2
 *
 * Build the level 2 share information. The list should have been built
 * before we arrived here so all we have to do is copy the share info
 * to the response heap and setup the infonres values. The only thing
 * to be aware of here is that there are minor difference between the
 * various share types.
 */
static DWORD
mlsvc_NetShareEnumLevel2(struct mslm_infonres *infonres, DWORD n_shares,
    struct mlrpc_xaction *mxa, char sticky)
{
	struct mslm_SHARE_INFO_2 *info2;
	lmshare_iterator_t *iterator;
	lmshare_info_t *si;
	DWORD i;
	smb_dr_user_ctx_t *user_ctx = mxa->context->user_ctx;

	info2 = MLRPC_HEAP_NEWN(mxa, struct mslm_SHARE_INFO_2, n_shares);
	if (info2 == 0)
		return (ERROR_NOT_ENOUGH_MEMORY);

	iterator = lmshare_open_iterator(LMSHRM_ALL);
	if (iterator == NULL)
		return (ERROR_NOT_ENOUGH_MEMORY);

	i = 0;
	while ((si = lmshare_iterate(iterator)) != 0) {
		if (sticky && (si->stype & STYPE_SPECIAL))
			continue;

		if (smb_is_autohome(si))
			continue;

		if (mlsvc_NetShareEnumCommon(mxa, i, 2, si,
		    (void *)info2) != ERROR_SUCCESS)
			break;
		i++;
	}

	i = srvsvc_add_autohome(mxa, user_ctx->du_account, i, 2, (char *)info2);

	lmshare_close_iterator(iterator);
	infonres->entriesread = i;
	infonres->entries = info2;
	return (ERROR_SUCCESS);
}

/*
 * mlsvc_NetShareEnumLevel502
 *
 * Build the level 502 share information. This is the same as level 2
 * but with a security descriptor in the share structure. We don't
 * support SD's on shares so we can just set that field to zero. See
 * mlsvc_NetShareEnumLevel2 for more information.
 */
static DWORD
mlsvc_NetShareEnumLevel502(struct mslm_infonres *infonres, DWORD n_shares,
    struct mlrpc_xaction *mxa, char sticky)
{
	struct mslm_SHARE_INFO_502 *info502;
	lmshare_iterator_t *iterator;
	lmshare_info_t *si;
	DWORD i;
	smb_dr_user_ctx_t *user_ctx = mxa->context->user_ctx;

	info502 = MLRPC_HEAP_NEWN(mxa, struct mslm_SHARE_INFO_502, n_shares);

	if (info502 == 0)
		return (ERROR_NOT_ENOUGH_MEMORY);

	iterator = lmshare_open_iterator(LMSHRM_ALL);
	if (iterator == NULL)
		return (ERROR_NOT_ENOUGH_MEMORY);

	i = 0;
	while ((si = lmshare_iterate(iterator)) != 0) {
		if (sticky && (si->stype & STYPE_SPECIAL))
			continue;

		if (smb_is_autohome(si))
			continue;

		if (mlsvc_NetShareEnumCommon(
		    mxa, i, 502, si, (void *)info502) != ERROR_SUCCESS)
			break;
		i++;
	}

	i = srvsvc_add_autohome(mxa, user_ctx->du_account, i, 502,
	    (char *)info502);

	lmshare_close_iterator(iterator);
	infonres->entriesread = i;
	infonres->entries = info502;
	return (ERROR_SUCCESS);
}

/*
 * mlsvc_NetShareEnumCommon
 *
 * Build the levels 0, 1, 2 and 502 share information. This function
 * is called by the various NetShareEnum levels for each share. If
 * we cannot build the share data for some reason, we return an error
 * but the actual value of the error is not important to the caller.
 * The caller just needs to know not to include this info in the RPC
 * response.
 *
 * Returns:
 *	ERROR_SUCCESS
 *	ERROR_NOT_ENOUGH_MEMORY
 *	ERROR_INVALID_LEVEL
 */
static DWORD
mlsvc_NetShareEnumCommon(struct mlrpc_xaction *mxa, DWORD i, int level,
    lmshare_info_t *si, void *infop)
{
	struct mslm_SHARE_INFO_0 *info0;
	struct mslm_SHARE_INFO_1 *info1;
	struct mslm_SHARE_INFO_2 *info2;
	struct mslm_SHARE_INFO_502 *info502;
	char shr_comment[LMSHR_COMMENT_MAX];

	if ((si->stype & STYPE_MASK) == STYPE_IPC) {
		/*
		 * Windows clients don't send the \\PIPE path for IPC$.
		 */
		si->directory[0] = '\0';
		(void) strcpy(si->comment, "Remote IPC");
	}

	if (si->comment && strlen(si->comment))
		(void) snprintf(shr_comment, sizeof (shr_comment), "%s (%s)",
		    si->directory, si->comment);
	else
		(void) strcpy(shr_comment, si->directory);

	switch (level) {
	case 0:
		info0 = (struct mslm_SHARE_INFO_0 *)infop;
		info0[i].shi0_netname
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, si->share_name);

		if (info0[i].shi0_netname == 0)
			return (ERROR_NOT_ENOUGH_MEMORY);
		break;

	case 1:
		info1 = (struct mslm_SHARE_INFO_1 *)infop;
		info1[i].shi1_netname
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, si->share_name);

		info1[i].shi1_remark
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, shr_comment);

		info1[i].shi1_type = si->stype;

		if (!info1[i].shi1_netname || !info1[i].shi1_remark)
			return (ERROR_NOT_ENOUGH_MEMORY);
		break;

	case 2:
		info2 = (struct mslm_SHARE_INFO_2 *)infop;
		info2[i].shi2_netname
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, si->share_name);

		info2[i].shi2_remark
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, shr_comment);

		info2[i].shi2_path
		    = (unsigned char *)srvsvc_share_mkpath(mxa, si->directory);

		info2[i].shi2_type = si->stype;
		info2[i].shi2_permissions = 0;
		info2[i].shi2_max_uses = SHI_USES_UNLIMITED;
		info2[i].shi2_current_uses = 0;
		info2[i].shi2_passwd
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, empty_string);

		if (!info2[i].shi2_netname || !info2[i].shi2_remark ||
		    !info2[i].shi2_passwd || !info2[i].shi2_path)
			return (ERROR_NOT_ENOUGH_MEMORY);

		break;

	case 502:
		info502 = (struct mslm_SHARE_INFO_502 *)infop;
		info502[i].shi502_netname
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, si->share_name);

		info502[i].shi502_remark
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, shr_comment);

		info502[i].shi502_path
		    = (unsigned char *)srvsvc_share_mkpath(mxa, si->directory);

		info502[i].shi502_type = si->stype;
		info502[i].shi502_permissions = 0;
		info502[i].shi502_max_uses = SHI_USES_UNLIMITED;
		info502[i].shi502_current_uses = 0;
		info502[i].shi502_passwd
		    = (unsigned char *)MLRPC_HEAP_STRSAVE(mxa, empty_string);

		info502[i].shi502_reserved = 0;
		info502[i].shi502_security_descriptor = 0;

		if (!info502[i].shi502_netname || !info502[i].shi502_remark ||
		    !info502[i].shi502_passwd || !info502[i].shi502_path)
			return (ERROR_NOT_ENOUGH_MEMORY);
		break;

	default:
		return (ERROR_INVALID_LEVEL);
	}

	return (ERROR_SUCCESS);
}

/*
 * srvsvc_s_NetShareDel
 *
 * Delete a share. Only the administrator, or a member of the domain
 * administrators group, is allowed to delete shares.
 *
 * This interface is used by the rmtshare command from the NT resource
 * kit. Rmtshare allows a client to add or remove shares on a server
 * from the client's command line.
 *
 * Returns Win32 error codes.
 */
static int
srvsvc_s_NetShareDel(void *arg, struct mlrpc_xaction *mxa)
{
	struct mslm_NetShareDel *param = arg;

	if (srvsvc_is_poweruser(mxa) == 0 ||
	    lmshare_is_restricted((char *)param->netname)) {
		param->status = ERROR_ACCESS_DENIED;
		return (MLRPC_DRC_OK);
	}

	param->status = lmshare_delete((char *)param->netname, 1);
	return (MLRPC_DRC_OK);
}

/*
 * srvsvc_s_NetGetFileSecurity
 *
 * Get security descriptor of the requested file/folder
 *
 * Right now, just returns ERROR_ACCESS_DENIED, because we cannot
 * get the requested SD here in MLRPC code.
 */
/*ARGSUSED*/
static int
srvsvc_s_NetGetFileSecurity(void *arg, struct mlrpc_xaction *mxa)
{
	struct mslm_NetGetFileSecurity *param = arg;

	param->length = 0;
	param->status = ERROR_ACCESS_DENIED;
	return (MLRPC_DRC_OK);
}

/*
 * srvsvc_s_NetSetFileSecurity
 *
 * Set the given security descriptor for the requested file/folder
 *
 * Right now, just returns ERROR_ACCESS_DENIED, because we cannot
 * set the requested SD here in MLRPC code.
 */
/*ARGSUSED*/
static int
srvsvc_s_NetSetFileSecurity(void *arg, struct mlrpc_xaction *mxa)
{
	struct mslm_NetSetFileSecurity *param = arg;

	param->status = ERROR_ACCESS_DENIED;
	return (MLRPC_DRC_OK);
}

static mlrpc_stub_table_t srvsvc_stub_table[] = {
	{ srvsvc_s_NetConnectEnum,	SRVSVC_OPNUM_NetConnectEnum },
	{ srvsvc_s_NetFileEnum,		SRVSVC_OPNUM_NetFileEnum },
	{ srvsvc_s_NetFileClose,	SRVSVC_OPNUM_NetFileClose },
	{ srvsvc_s_NetShareGetInfo,	SRVSVC_OPNUM_NetShareGetInfo },
	{ srvsvc_s_NetShareSetInfo,	SRVSVC_OPNUM_NetShareSetInfo },
	{ srvsvc_s_NetSessionEnum,	SRVSVC_OPNUM_NetSessionEnum },
	{ srvsvc_s_NetSessionDel,	SRVSVC_OPNUM_NetSessionDel },
	{ srvsvc_s_NetServerGetInfo,	SRVSVC_OPNUM_NetServerGetInfo },
	{ srvsvc_s_NetRemoteTOD,	SRVSVC_OPNUM_NetRemoteTOD },
	{ srvsvc_s_NetNameValidate,	SRVSVC_OPNUM_NetNameValidate },
	{ srvsvc_s_NetShareAdd,		SRVSVC_OPNUM_NetShareAdd },
	{ srvsvc_s_NetShareDel,		SRVSVC_OPNUM_NetShareDel },
	{ srvsvc_s_NetShareEnum,	SRVSVC_OPNUM_NetShareEnum },
	{ srvsvc_s_NetShareEnumSticky,	SRVSVC_OPNUM_NetShareEnumSticky },
	{ srvsvc_s_NetGetFileSecurity,	SRVSVC_OPNUM_NetGetFileSecurity },
	{ srvsvc_s_NetSetFileSecurity,	SRVSVC_OPNUM_NetSetFileSecurity },
	{0}
};