summaryrefslogtreecommitdiff
path: root/source3/utils/net_rap.c
blob: 605043734ccc203db14df52123531e2db1dbcd93 (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
/*
   Samba Unix/Linux SMB client library
   Distributed SMB/CIFS Server Management Utility
   Copyright (C) 2001 Steve French  (sfrench@us.ibm.com)
   Copyright (C) 2001 Jim McDonough (jmcd@us.ibm.com)
   Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
   Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)

   Originally written by Steve and Jim. Largely rewritten by tridge in
   November 2001.

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

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

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

#include "includes.h"
#include "utils/net.h"
#include "../librpc/gen_ndr/rap.h"

/* The following messages were for error checking that is not properly
   reported at the moment.  Which should be reinstated? */
#define ERRMSG_TARGET_WG_NOT_VALID      "\nTarget workgroup option not valid "\
					"except on net rap server command, ignored"
#define ERRMSG_INVALID_HELP_OPTION	"\nInvalid help option\n"

#define ERRMSG_BOTH_SERVER_IPADDRESS    "\nTarget server and IP address both "\
  "specified. Do not set both at the same time.  The target IP address was used\n"

static int errmsg_not_implemented(void)
{
	d_printf(_("\nNot implemented\n"));
	return 0;
}

int net_rap_file_usage(struct net_context *c, int argc, const char **argv)
{
	return net_file_usage(c, argc, argv);
}

/***************************************************************************
  list info on an open file
***************************************************************************/
static void file_fn(const char * pPath, const char * pUser, uint16 perms,
		    uint16 locks, uint32 id)
{
	d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
		 id, pUser, perms, locks, pPath);
}

static void one_file_fn(const char *pPath, const char *pUser, uint16 perms,
			uint16 locks, uint32 id)
{
	d_printf(_("File ID          %d\n"
		   "User name        %s\n"
		   "Locks            0x%-4.2x\n"
		   "Path             %s\n"
		   "Permissions      0x%x\n"),
		 id, pUser, locks, pPath, perms);
}


static int rap_file_close(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;
	if (argc == 0 || c->display_usage) {
		return net_rap_file_usage(c, argc, argv);
	}

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	ret = cli_NetFileClose(cli, atoi(argv[0]));
	cli_shutdown(cli);
	return ret;
}

static int rap_file_info(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;
	if (argc == 0 || c->display_usage)
		return net_rap_file_usage(c, argc, argv);

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	ret = cli_NetFileGetInfo(cli, atoi(argv[0]), one_file_fn);
	cli_shutdown(cli);
	return ret;
}

static int rap_file_user(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;

	if (argc == 0 || c->display_usage)
		return net_rap_file_usage(c, argc, argv);

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
		return -1;

	/* list open files */

	d_printf(_("\nEnumerating open files on remote server:\n\n"
		   "\nFileId  Opened by            Perms  Locks  Path \n"
		   "------  ---------            -----  -----  ---- \n"));
	ret = cli_NetFileEnum(cli, argv[0], NULL, file_fn);

	if (ret == -1)
		d_printf(_("\nOperation not supported by server!\n\n"));

	cli_shutdown(cli);
	return ret;
}

int net_rap_file(struct net_context *c, int argc, const char **argv)
{
	struct functable func[] = {
		{
			"close",
			rap_file_close,
			NET_TRANSPORT_RAP,
			N_("Close specified file on server"),
			N_("net rap file close\n"
			   "    Close specified file on server")
		},
		{
			"user",
			rap_file_user,
			NET_TRANSPORT_RAP,
			N_("List all files opened by username"),
			N_("net rap file user\n"
			   "    List all files opened by username")
		},
		{
			"info",
			rap_file_info,
			NET_TRANSPORT_RAP,
			N_("Display info about an opened file"),
			N_("net rap file info\n"
			   "    Display info about an opened file")
		},
		{NULL, NULL, 0, NULL, NULL}
	};

	if (argc == 0) {
		struct cli_state *cli;
		int ret;

		if (c->display_usage) {
			d_printf(_("Usage:\n"));
			d_printf(_("net rap file\n"
				   "    List all open files on rempte "
				   "server\n"));
			net_display_usage_from_functable(func);
			return 0;
		}

		if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                        return -1;

		/* list open files */

		d_printf(_("\nEnumerating open files on remote server:\n\n"
			   "\nFileId  Opened by            Perms  Locks  Path\n"
			   "------  ---------            -----  -----  ----\n"));
		ret = cli_NetFileEnum(cli, NULL, NULL, file_fn);

		if (ret == -1)
			d_printf(_("\nOperation not supported by server!\n\n"));

		cli_shutdown(cli);
		return ret;
	}

	return net_run_function(c, argc, argv, "net rap file", func);
}

int net_rap_share_usage(struct net_context *c, int argc, const char **argv)
{
	return net_share_usage(c, argc, argv);
}

static void long_share_fn(const char *share_name, uint32 type,
			  const char *comment, void *state)
{
	d_printf("%-12s %-8.8s %-50s\n",
		 share_name, net_share_type_str(type), comment);
}

static void share_fn(const char *share_name, uint32 type,
		     const char *comment, void *state)
{
	d_printf("%s\n", share_name);
}

static int rap_share_delete(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;

	if (argc == 0 || c->display_usage) {
		return net_rap_share_usage(c, argc, argv);
	}

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	ret = cli_NetShareDelete(cli, argv[0]);
	cli_shutdown(cli);
	return ret;
}

static int rap_share_add(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;

	struct rap_share_info_2 sinfo;
	char *p;
	char *sharename;

	if (argc == 0 || c->display_usage) {
		return net_rap_share_usage(c, argc, argv);
	}

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	sharename = SMB_STRDUP(argv[0]);
	p = strchr(sharename, '=');
	if (p == NULL) {
		d_printf(_("Server path not specified\n"));
		SAFE_FREE(sharename);
		return net_rap_share_usage(c, argc, argv);
	}
	*p = 0;
	strlcpy((char *)sinfo.share_name, sharename, sizeof(sinfo.share_name));
	sinfo.reserved1 = '\0';
	sinfo.share_type = 0;
	sinfo.comment = smb_xstrdup(c->opt_comment);
	sinfo.perms = 0;
	sinfo.maximum_users = c->opt_maxusers;
	sinfo.active_users = 0;
	sinfo.path = p+1;
	memset(sinfo.password, '\0', sizeof(sinfo.password));
	sinfo.reserved2 = '\0';

	ret = cli_NetShareAdd(cli, &sinfo);
	cli_shutdown(cli);
	SAFE_FREE(sharename);
	return ret;
}


int net_rap_share(struct net_context *c, int argc, const char **argv)
{
	struct functable func[] = {
		{
			"delete",
			rap_share_delete,
			NET_TRANSPORT_RAP,
			N_("Delete a share from server"),
			N_("net rap share delete\n"
			   "    Delete a share from server")
		},
		{
			"close",
			rap_share_delete,
			NET_TRANSPORT_RAP,
			N_("Delete a share from server"),
			N_("net rap share close\n"
			   "    Delete a share from server\n"
			   "    Alias for net rap share delete")
		},
		{
			"add",
			rap_share_add,
			NET_TRANSPORT_RAP,
			N_("Add a share to server"),
			N_("net rap share add\n"
			   "    Add a share to server")
		},
		{NULL, NULL, 0, NULL, NULL}
	};

	if (argc == 0) {
		struct cli_state *cli;
		int ret;

		if (c->display_usage) {
			d_printf(_("Usage:\n"));
			d_printf(_("net rap share\n"
				   "    List all shares on remote server\n"));
			net_display_usage_from_functable(func);
			return 0;
		}

		if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
			return -1;

		if (c->opt_long_list_entries) {
			d_printf(_(
	"\nEnumerating shared resources (exports) on remote server:\n\n"
	"\nShare name   Type     Description\n"
	"----------   ----     -----------\n"));
			ret = cli_RNetShareEnum(cli, long_share_fn, NULL);
		} else {
			ret = cli_RNetShareEnum(cli, share_fn, NULL);
		}
		cli_shutdown(cli);
		return ret;
	}

	return net_run_function(c, argc, argv, "net rap share", func);
}

int net_rap_session_usage(struct net_context *c, int argc, const char **argv)
{
	d_printf(_(
	 "\nnet rap session [misc. options] [targets]"
	 "\n\tenumerates all active SMB/CIFS sessions on target server\n"));
	d_printf(_(
	 "\nnet rap session DELETE <client_name> [misc. options] [targets] \n"
	 "\tor"
	 "\nnet rap session CLOSE <client_name> [misc. options] [targets]"
	 "\n\tDeletes (closes) a session from specified client to server\n"));
	d_printf(_(
	"\nnet rap session INFO <client_name>"
	"\n\tEnumerates all open files in specified session\n"));

	net_common_flags_usage(c, argc, argv);
	return -1;
}

static void list_sessions_func(char *wsname, char *username, uint16 conns,
			uint16 opens, uint16 users, uint32 sess_time,
			uint32 idle_time, uint32 user_flags, char *clitype)
{
	int hrs = idle_time / 3600;
	int min = (idle_time / 60) % 60;
	int sec = idle_time % 60;

	d_printf("\\\\%-18.18s %-20.20s %-18.18s %5d %2.2d:%2.2d:%2.2d\n",
		 wsname, username, clitype, opens, hrs, min, sec);
}

static void display_session_func(const char *wsname, const char *username,
				 uint16 conns, uint16 opens, uint16 users,
				 uint32 sess_time, uint32 idle_time,
				 uint32 user_flags, const char *clitype)
{
	int ihrs = idle_time / 3600;
	int imin = (idle_time / 60) % 60;
	int isec = idle_time % 60;
	int shrs = sess_time / 3600;
	int smin = (sess_time / 60) % 60;
	int ssec = sess_time % 60;
	d_printf(_("User name       %-20.20s\n"
		   "Computer        %-20.20s\n"
		   "Guest logon     %-20.20s\n"
		   "Client Type     %-40.40s\n"
		   "Sess time       %2.2d:%2.2d:%2.2d\n"
		   "Idle time       %2.2d:%2.2d:%2.2d\n"),
		 username, wsname,
		 (user_flags&0x0)?_("yes"):_("no"), clitype,
		 shrs, smin, ssec, ihrs, imin, isec);
}

static void display_conns_func(uint16 conn_id, uint16 conn_type, uint16 opens,
			       uint16 users, uint32 conn_time,
			       const char *username, const char *netname)
{
	d_printf("%-14.14s %-8.8s %5d\n",
		 netname, net_share_type_str(conn_type), opens);
}

static int rap_session_info(struct net_context *c, int argc, const char **argv)
{
	const char *sessname;
	struct cli_state *cli;
	int ret;

	if (argc == 0 || c->display_usage)
                return net_rap_session_usage(c, argc, argv);

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	sessname = argv[0];

	ret = cli_NetSessionGetInfo(cli, sessname, display_session_func);
	if (ret < 0) {
		cli_shutdown(cli);
                return ret;
	}

	d_printf(_("Share name     Type     # Opens\n-------------------------"
		   "-----------------------------------------------------\n"));
	ret = cli_NetConnectionEnum(cli, sessname, display_conns_func);
	cli_shutdown(cli);
	return ret;
}

static int rap_session_delete(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;

	if (argc == 0 || c->display_usage)
                return net_rap_session_usage(c, argc, argv);

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	ret = cli_NetSessionDel(cli, argv[0]);
	cli_shutdown(cli);
	return ret;
}

int net_rap_session(struct net_context *c, int argc, const char **argv)
{
	struct functable func[] = {
		{
			"info",
			rap_session_info,
			NET_TRANSPORT_RAP,
			N_("Display information about session"),
			N_("net rap session info\n"
			   "    Display information about session")
		},
		{
			"delete",
			rap_session_delete,
			NET_TRANSPORT_RAP,
			N_("Close specified session"),
			N_("net rap session delete\n"
			   "    Close specified session\n"
			   "    Alias for net rap session close")
		},
		{
			"close",
			rap_session_delete,
			NET_TRANSPORT_RAP,
			N_("Close specified session"),
			N_("net rap session close\n"
			   "    Close specified session")
		},
		{NULL, NULL, 0, NULL, NULL}
	};

	if (argc == 0) {
		struct cli_state *cli;
		int ret;

		if (c->display_usage) {
			d_printf(_("Usage:\n"));
			d_printf(_("net rap session\n"
				   "    List all open sessions on remote "
				   "server\n"));
			net_display_usage_from_functable(func);
			return 0;
		}

		if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
			return -1;

		d_printf(_("Computer             User name            "
			   "Client Type        Opens Idle time\n"
			   "------------------------------------------"
			   "------------------------------------\n"));
		ret = cli_NetSessionEnum(cli, list_sessions_func);

		cli_shutdown(cli);
		return ret;
	}

	return net_run_function(c, argc, argv, "net rap session", func);
}

/****************************************************************************
list a server name
****************************************************************************/
static void display_server_func(const char *name, uint32 m,
				const char *comment, void * reserved)
{
	d_printf("\t%-16.16s     %s\n", name, comment);
}

static int net_rap_server_name(struct net_context *c, int argc, const char *argv[])
{
	struct cli_state *cli;
	char *name;

	if (c->display_usage) {
		d_printf("%s\n%s",
			 _("Usage:"),
			 _("net rap server name\n"
			   "    Get the name of the server\n"));
		return 0;
	}

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	if (!cli_get_server_name(NULL, cli, &name)) {
		d_fprintf(stderr, _("cli_get_server_name failed\n"));
		cli_shutdown(cli);
		return -1;
	}

	d_printf(_("Server name = %s\n"), name);

	TALLOC_FREE(name);
	cli_shutdown(cli);
	return 0;
}

static int net_rap_server_domain(struct net_context *c, int argc,
				 const char **argv)
{
	struct cli_state *cli;
	int ret;

	if (c->display_usage) {
		d_printf("%s\n%s",
			 _("Usage:"),
			 _("net rap server domain\n"
			   "    Enumerate servers in this domain/workgroup\n"));
		return 0;
	}

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	d_printf(_("\nEnumerating servers in this domain or workgroup: \n\n"
		   "\tServer name          Server description\n"
		   "\t-------------        ----------------------------\n"));

	ret = cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL,
				display_server_func,NULL);
	cli_shutdown(cli);
	return ret;
}

int net_rap_server(struct net_context *c, int argc, const char **argv)
{
	struct functable func[] = {
		{
			"name",
			net_rap_server_name,
			NET_TRANSPORT_RAP,
			N_("Get the name of the server"),
			N_("net rap server name\n"
			   "    Get the name of the server")
		},
		{
			"domain",
			net_rap_server_domain,
			NET_TRANSPORT_RAP,
			N_("Get the servers in this domain/workgroup"),
			N_("net rap server domain\n"
			   "    Get the servers in this domain/workgroup")
		},
		{NULL, NULL, 0, NULL, NULL}
	};

	/* smb4k uses 'net [rap|rpc] server domain' to query servers in a domain */
	/* Fall through for 'domain', any other forms will cause to show usage message */
	return net_run_function(c, argc, argv, "net rap server", func);

}

int net_rap_domain_usage(struct net_context *c, int argc, const char **argv)
{
	d_printf(_("net rap domain [misc. options] [target]\n\tlists the"
		   " domains or workgroups visible on the current network\n"));

	net_common_flags_usage(c, argc, argv);
	return -1;
}

int net_rap_domain(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;

	if (c->display_usage)
		return net_rap_domain_usage(c, argc, argv);

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	d_printf(_("\nEnumerating domains:\n\n"
		   "\tDomain name          Server name of Browse Master\n"
		   "\t-------------        ----------------------------\n"));

	ret = cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
				display_server_func,NULL);
	cli_shutdown(cli);
	return ret;
}

int net_rap_printq_usage(struct net_context *c, int argc, const char **argv)
{
	d_printf(_(
	 "net rap printq [misc. options] [targets]\n"
	 "\tor\n"
	 "net rap printq info [<queue_name>] [misc. options] [targets]\n"
	 "\tlists the specified queue and jobs on the target server.\n"
	 "\tIf the queue name is not specified, all queues are listed.\n\n"));
	d_printf(_(
	 "net rap printq delete [<queue name>] [misc. options] [targets]\n"
	 "\tdeletes the specified job number on the target server, or the\n"
	 "\tprinter queue if no job number is specified\n"));

	net_common_flags_usage(c, argc, argv);

	return -1;
}

static void enum_queue(const char *queuename, uint16 pri, uint16 start,
		       uint16 until, const char *sep, const char *pproc,
		       const char *dest, const char *qparms,
		       const char *qcomment, uint16 status, uint16 jobcount)
{
	d_printf(_("%-17.17s Queue %5d jobs                      "),
		 queuename, jobcount);

	switch (status) {
	case 0:
		d_printf(_("*Printer Active*\n"));
		break;
	case 1:
		d_printf(_("*Printer Paused*\n"));
		break;
	case 2:
		d_printf(_("*Printer error*\n"));
		break;
	case 3:
		d_printf(_("*Delete Pending*\n"));
		break;
	default:
		d_printf(_("**UNKNOWN STATUS**\n"));
	}
}

static void enum_jobs(uint16 jobid, const char *ownername,
		      const char *notifyname, const char *datatype,
		      const char *jparms, uint16 pos, uint16 status,
		      const char *jstatus, unsigned int submitted, unsigned int jobsize,
		      const char *comment)
{
	d_printf("     %-23.23s %5d %9d            ",
		 ownername, jobid, jobsize);
	switch (status) {
	case 0:
		d_printf(_("Waiting\n"));
		break;
	case 1:
		d_printf(_("Held in queue\n"));
		break;
	case 2:
		d_printf(_("Spooling\n"));
		break;
	case 3:
		d_printf(_("Printing\n"));
		break;
	default:
		d_printf(_("**UNKNOWN STATUS**\n"));
	}
}

#define PRINTQ_ENUM_DISPLAY \
    _("Print queues at \\\\%s\n\n"\
      "Name                         Job #      Size            Status\n\n"\
      "------------------------------------------------------------------"\
      "-------------\n")

static int rap_printq_info(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;

	if (argc == 0 || c->display_usage)
                return net_rap_printq_usage(c, argc, argv);

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	d_printf(PRINTQ_ENUM_DISPLAY, cli->desthost); /* list header */
	ret = cli_NetPrintQGetInfo(cli, argv[0], enum_queue, enum_jobs);
	cli_shutdown(cli);
	return ret;
}

static int rap_printq_delete(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;

	if (argc == 0 || c->display_usage)
                return net_rap_printq_usage(c, argc, argv);

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	ret = cli_printjob_del(cli, atoi(argv[0]));
	cli_shutdown(cli);
	return ret;
}

int net_rap_printq(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;

	struct functable func[] = {
		{
			"info",
			rap_printq_info,
			NET_TRANSPORT_RAP,
			N_("Display info about print queues and jobs"),
			N_("net rap printq info [queue]\n"
			   "    Display info about print jobs in queue.\n"
			   "    If queue is not specified, all queues are "
			   "listed")
		},
		{
			"delete",
			rap_printq_delete,
			NET_TRANSPORT_RAP,
			N_("Delete print job(s)"),
			N_("net rap printq delete\n"
			   "    Delete print job(s)")
		},
		{NULL, NULL, 0, NULL, NULL}
	};

	if (argc == 0) {
		if (c->display_usage) {
			d_printf(_("Usage:\n"));
			d_printf(_("net rap printq\n"
				   "    List the print queue\n"));
			net_display_usage_from_functable(func);
			return 0;
		}

		if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
			return -1;

		d_printf(PRINTQ_ENUM_DISPLAY, cli->desthost); /* list header */
		ret = cli_NetPrintQEnum(cli, enum_queue, enum_jobs);
		cli_shutdown(cli);
		return ret;
	}

	return net_run_function(c, argc, argv, "net rap printq", func);
}

static int net_rap_user_usage(struct net_context *c, int argc, const char **argv)
{
	return net_user_usage(c, argc, argv);
}

static void user_fn(const char *user_name, void *state)
{
	d_printf("%-21.21s\n", user_name);
}

static void long_user_fn(const char *user_name, const char *comment,
			 const char * home_dir, const char * logon_script,
			 void *state)
{
	d_printf("%-21.21s %s\n",
		 user_name, comment);
}

static void group_member_fn(const char *user_name, void *state)
{
	d_printf("%-21.21s\n", user_name);
}

static int rap_user_delete(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;

	if (argc == 0 || c->display_usage) {
                return net_rap_user_usage(c, argc, argv);
	}

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	ret = cli_NetUserDelete(cli, argv[0]);
	cli_shutdown(cli);
	return ret;
}

static int rap_user_add(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;
	struct rap_user_info_1 userinfo;

	if (argc == 0 || c->display_usage) {
                return net_rap_user_usage(c, argc, argv);
	}

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	safe_strcpy((char *)userinfo.user_name, argv[0], sizeof(userinfo.user_name)-1);
	if (c->opt_flags == -1)
                c->opt_flags = 0x21;

	userinfo.userflags = c->opt_flags;
	userinfo.reserved1 = '\0';
        userinfo.comment = smb_xstrdup(c->opt_comment ? c->opt_comment : "");
	userinfo.priv = 1;
	userinfo.home_dir = NULL;
	userinfo.logon_script = NULL;
	userinfo.passwrd[0] = '\0';

	ret = cli_NetUserAdd(cli, &userinfo);

	cli_shutdown(cli);
	return ret;
}

static int rap_user_info(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;
	if (argc == 0 || c->display_usage) {
                return net_rap_user_usage(c, argc, argv);
	}

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	ret = cli_NetUserGetGroups(cli, argv[0], group_member_fn, NULL);
	cli_shutdown(cli);
	return ret;
}

int net_rap_user(struct net_context *c, int argc, const char **argv)
{
	int ret = -1;
	struct functable func[] = {
		{
			"add",
			rap_user_add,
			NET_TRANSPORT_RAP,
			N_("Add specified user"),
			N_("net rap user add\n"
			   "    Add specified user")
		},
		{
			"info",
			rap_user_info,
			NET_TRANSPORT_RAP,
			N_("List domain groups of specified user"),
			N_("net rap user info\n"
			   "    List domain groups of specified user")

		},
		{
			"delete",
			rap_user_delete,
			NET_TRANSPORT_RAP,
			N_("Remove specified user"),
			N_("net rap user delete\n"
			   "    Remove specified user")
		},
		{NULL, NULL, 0, NULL, NULL}
	};

	if (argc == 0) {
		struct cli_state *cli;
		if (c->display_usage) {
			d_printf(_("Usage:\n"));
			d_printf(_("net rap user\n"
				   "    List all users\n"));
			net_display_usage_from_functable(func);
			return 0;
		}

		if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                        goto done;
		if (c->opt_long_list_entries) {
			d_printf(_("\nUser name             Comment"
				   "\n-----------------------------\n"));
			ret = cli_RNetUserEnum(cli, long_user_fn, NULL);
			cli_shutdown(cli);
			goto done;
		}
		ret = cli_RNetUserEnum0(cli, user_fn, NULL);
		cli_shutdown(cli);
		goto done;
	}

	ret = net_run_function(c, argc, argv, "net rap user", func);
 done:
	if (ret != 0) {
		DEBUG(1, (_("Net user returned: %d\n"), ret));
	}
	return ret;
}


int net_rap_group_usage(struct net_context *c, int argc, const char **argv)
{
	return net_group_usage(c, argc, argv);
}

static void long_group_fn(const char *group_name, const char *comment,
			  void *state)
{
	d_printf("%-21.21s %s\n", group_name, comment);
}

static void group_fn(const char *group_name, void *state)
{
	d_printf("%-21.21s\n", group_name);
}

static int rap_group_delete(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;
	if (argc == 0 || c->display_usage) {
                return net_rap_group_usage(c, argc, argv);
	}

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	ret = cli_NetGroupDelete(cli, argv[0]);
	cli_shutdown(cli);
	return ret;
}

static int rap_group_add(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;
	struct rap_group_info_1 grinfo;

	if (argc == 0 || c->display_usage) {
                return net_rap_group_usage(c, argc, argv);
	}

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	/* BB check for length 21 or smaller explicitly ? BB */
	safe_strcpy((char *)grinfo.group_name, argv[0], sizeof(grinfo.group_name)-1);
	grinfo.reserved1 = '\0';
	grinfo.comment = smb_xstrdup(c->opt_comment ? c->opt_comment : "");

	ret = cli_NetGroupAdd(cli, &grinfo);
	cli_shutdown(cli);
	return ret;
}

int net_rap_group(struct net_context *c, int argc, const char **argv)
{
	struct functable func[] = {
		{
			"add",
			rap_group_add,
			NET_TRANSPORT_RAP,
			N_("Add specified group"),
			N_("net rap group add\n"
			   "    Add specified group")
		},
		{
			"delete",
			rap_group_delete,
			NET_TRANSPORT_RAP,
			N_("Delete specified group"),
			N_("net rap group delete\n"
			   "    Delete specified group")
		},
		{NULL, NULL, 0, NULL, NULL}
	};

	if (argc == 0) {
		struct cli_state *cli;
		int ret;
		if (c->display_usage) {
			d_printf(_("Usage:\n"));
			d_printf(_("net rap group\n"
				   "    List all groups\n"));
			net_display_usage_from_functable(func);
			return 0;
		}

		if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                        return -1;
		if (c->opt_long_list_entries) {
			d_printf(_("Group name            Comment\n"
			           "-----------------------------\n"));
			ret = cli_RNetGroupEnum(cli, long_group_fn, NULL);
			cli_shutdown(cli);
			return ret;
		}
		ret = cli_RNetGroupEnum0(cli, group_fn, NULL);
		cli_shutdown(cli);
		return ret;
	}

	return net_run_function(c, argc, argv, "net rap group", func);
}

int net_rap_groupmember_usage(struct net_context *c, int argc, const char **argv)
{
	d_printf(_(
	 "net rap groupmember LIST <group> [misc. options] [targets]"
	 "\n\t Enumerate users in a group\n"
	 "\nnet rap groupmember DELETE <group> <user> [misc. options] "
	 "[targets]\n\t Delete specified user from specified group\n"
	 "\nnet rap groupmember ADD <group> <user> [misc. options] [targets]"
	 "\n\t Add specified user to specified group\n"));

	net_common_flags_usage(c, argc, argv);
	return -1;
}


static int rap_groupmember_add(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;
	if (argc != 2 || c->display_usage) {
                return net_rap_groupmember_usage(c, argc, argv);
	}

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	ret = cli_NetGroupAddUser(cli, argv[0], argv[1]);
	cli_shutdown(cli);
	return ret;
}

static int rap_groupmember_delete(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;
	if (argc != 2 || c->display_usage) {
                return net_rap_groupmember_usage(c, argc, argv);
	}

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	ret = cli_NetGroupDelUser(cli, argv[0], argv[1]);
	cli_shutdown(cli);
	return ret;
}

static int rap_groupmember_list(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;
	if (argc == 0 || c->display_usage) {
                return net_rap_groupmember_usage(c, argc, argv);
	}

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	ret = cli_NetGroupGetUsers(cli, argv[0], group_member_fn, NULL );
	cli_shutdown(cli);
	return ret;
}

int net_rap_groupmember(struct net_context *c, int argc, const char **argv)
{
	struct functable func[] = {
		{
			"add",
			rap_groupmember_add,
			NET_TRANSPORT_RAP,
			N_("Add specified user to group"),
			N_("net rap groupmember add\n"
			   "    Add specified user to group")
		},
		{
			"list",
			rap_groupmember_list,
			NET_TRANSPORT_RAP,
			N_("List users in group"),
			N_("net rap groupmember list\n"
			   "    List users in group")
		},
		{
			"delete",
			rap_groupmember_delete,
			NET_TRANSPORT_RAP,
			N_("Remove user from group"),
			N_("net rap groupmember delete\n"
			   "    Remove user from group")
		},
		{NULL, NULL, 0, NULL, NULL}
	};

	return net_run_function(c, argc, argv, "net rap groupmember", func);
}

int net_rap_validate_usage(struct net_context *c, int argc, const char **argv)
{
	d_printf(_("net rap validate <username> [password]\n"
		   "\tValidate user and password to check whether they"
		   " can access target server or domain\n"));

	net_common_flags_usage(c, argc, argv);
	return -1;
}

int net_rap_validate(struct net_context *c, int argc, const char **argv)
{
	return errmsg_not_implemented();
}

int net_rap_service_usage(struct net_context *c, int argc, const char **argv)
{
	d_printf(_("net rap service [misc. options] [targets] \n"
		 "\tlists all running service daemons on target server\n"));
	d_printf(_("\nnet rap service START <name> [service startup arguments]"
		 " [misc. options] [targets]"
		 "\n\tStart named service on remote server\n"));
	d_printf(_("\nnet rap service STOP <name> [misc. options] [targets]\n"
		 "\n\tStop named service on remote server\n"));

	net_common_flags_usage(c, argc, argv);
	return -1;
}

static int rap_service_start(struct net_context *c, int argc, const char **argv)
{
	return errmsg_not_implemented();
}

static int rap_service_stop(struct net_context *c, int argc, const char **argv)
{
	return errmsg_not_implemented();
}

static void service_fn(const char *service_name, const char *dummy,
		       void *state)
{
	d_printf("%-21.21s\n", service_name);
}

int net_rap_service(struct net_context *c, int argc, const char **argv)
{
	struct functable func[] = {
		{
			"start",
			rap_service_start,
			NET_TRANSPORT_RAP,
			N_("Start service on remote server"),
			N_("net rap service start\n"
			   "    Start service on remote server")
		},
		{
			"stop",
			rap_service_stop,
			NET_TRANSPORT_RAP,
			N_("Stop named serve on remote server"),
			N_("net rap service stop\n"
			   "    Stop named serve on remote server")
		},
		{NULL, NULL, 0, NULL, NULL}
	};

	if (argc == 0) {
		struct cli_state *cli;
		int ret;
		if (c->display_usage) {
			d_printf(_("Usage:\n"));
			d_printf(_("net rap service\n"
				   "    List services on remote server\n"));
			net_display_usage_from_functable(func);
			return 0;
		}

		if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
			return -1;

		if (c->opt_long_list_entries) {
			d_printf(_("Service name          Comment\n"
		                   "-----------------------------\n"));
			ret = cli_RNetServiceEnum(cli, long_group_fn, NULL);
		}
		ret = cli_RNetServiceEnum(cli, service_fn, NULL);
		cli_shutdown(cli);
		return ret;
	}

	return net_run_function(c, argc, argv, "net rap service", func);
}

int net_rap_password_usage(struct net_context *c, int argc, const char **argv)
{
	d_printf(_(
	 "net rap password <user> <oldpwo> <newpw> [misc. options] [target]\n"
	 "\tchanges the password for the specified user at target\n"));

	return -1;
}


int net_rap_password(struct net_context *c, int argc, const char **argv)
{
	struct cli_state *cli;
	int ret;

	if (argc < 3 || c->display_usage)
                return net_rap_password_usage(c, argc, argv);

	if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
                return -1;

	/* BB Add check for password lengths? */
	ret = cli_oem_change_password(cli, argv[0], argv[2], argv[1]);
	cli_shutdown(cli);
	return ret;
}

int net_rap_admin_usage(struct net_context *c, int argc, const char **argv)
{
	d_printf(_(
   "net rap admin <remote command> [cmd args [env]] [misc. options] [targets]"
   "\n\texecutes a remote command on an os/2 target server\n"));

	return -1;
}


int net_rap_admin(struct net_context *c, int argc, const char **argv)
{
	return errmsg_not_implemented();
}

/* Entry-point for all the RAP functions. */

int net_rap(struct net_context *c, int argc, const char **argv)
{
	struct functable func[] = {
		{
			"file",
			net_rap_file,
			NET_TRANSPORT_RAP,
			N_("List open files"),
			N_("net rap file\n"
			   "    List open files")
		},
		{
			"share",
			net_rap_share,
			NET_TRANSPORT_RAP,
			N_("List shares exported by server"),
			N_("net rap share\n"
			   "    List shares exported by server")
		},
		{
			"session",
			net_rap_session,
			NET_TRANSPORT_RAP,
			N_("List open sessions"),
			N_("net rap session\n"
			   "    List open sessions")
		},
		{
			"server",
			net_rap_server,
			NET_TRANSPORT_RAP,
			N_("List servers in workgroup"),
			N_("net rap server\n"
			   "    List servers in domain/workgroup")
		},
		{
			"domain",
			net_rap_domain,
			NET_TRANSPORT_RAP,
			N_("List domains in network"),
			N_("net rap domain\n"
			   "    List domains in network")
		},
		{
			"printq",
			net_rap_printq,
			NET_TRANSPORT_RAP,
			N_("List printer queues on server"),
			N_("net rap printq\n"
			   "    List printer queues on server")
		},
		{
			"user",
			net_rap_user,
			NET_TRANSPORT_RAP,
			N_("List users"),
			N_("net rap user\n"
			   "    List users")
		},
		{
			"group",
			net_rap_group,
			NET_TRANSPORT_RAP,
			N_("List user groups"),
			N_("net rap group\n"
			   "    List user groups")
		},
		{
			"validate",
			net_rap_validate,
			NET_TRANSPORT_RAP,
			N_("Check username/password"),
			N_("net rap validate\n"
			   "    Check username/password")
		},
		{
			"groupmember",
			net_rap_groupmember,
			NET_TRANSPORT_RAP,
			N_("List/modify group memberships"),
			N_("net rap groupmember\n"
			   "    List/modify group memberships")
		},
		{
			"admin",
			net_rap_admin,
			NET_TRANSPORT_RAP,
			N_("Execute commands on remote OS/2"),
			N_("net rap admin\n"
			   "    Execute commands on remote OS/2")
		},
		{
			"service",
			net_rap_service,
			NET_TRANSPORT_RAP,
			N_("Start/stop remote service"),
			N_("net rap service\n"
			   "    Start/stop remote service")
		},
		{
			"password",
			net_rap_password,
			NET_TRANSPORT_RAP,
			N_("Change user password"),
			N_("net rap password\n"
			   "    Change user password")
		},
		{NULL, NULL, 0, NULL, NULL}
	};

	return net_run_function(c, argc, argv, "net rap", func);
}