summaryrefslogtreecommitdiff
path: root/source3/rpcclient/cmd_samr.c
blob: d6d1ff9edb9d956f594269627c780eb11fb59fde (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
/* 
   Unix SMB/Netbios implementation.
   Version 2.2
   RPC pipe client

   Copyright (C) Andrew Tridgell              1992-2000,
   Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
   Copyright (C) Elrond                            2000,
   Copyright (C) Tim Potter                        2000

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

extern DOM_SID domain_sid;

/****************************************************************************
 display sam_user_info_21 structure
 ****************************************************************************/
static void display_sam_user_info_21(SAM_USER_INFO_21 *usr)
{
	fstring temp;

	unistr2_to_ascii(temp, &usr->uni_user_name, sizeof(temp)-1);
	printf("\tUser Name   :\t%s\n", temp);
	
	unistr2_to_ascii(temp, &usr->uni_full_name, sizeof(temp)-1);
	printf("\tFull Name   :\t%s\n", temp);
	
	unistr2_to_ascii(temp, &usr->uni_home_dir, sizeof(temp)-1);
	printf("\tHome Drive  :\t%s\n", temp);
	
	unistr2_to_ascii(temp, &usr->uni_dir_drive, sizeof(temp)-1);
	printf("\tDir Drive   :\t%s\n", temp);
	
	unistr2_to_ascii(temp, &usr->uni_profile_path, sizeof(temp)-1);
	printf("\tProfile Path:\t%s\n", temp);
	
	unistr2_to_ascii(temp, &usr->uni_logon_script, sizeof(temp)-1);
	printf("\tLogon Script:\t%s\n", temp);
	
	unistr2_to_ascii(temp, &usr->uni_acct_desc, sizeof(temp)-1);
	printf("\tDescription :\t%s\n", temp);
	
	unistr2_to_ascii(temp, &usr->uni_workstations, sizeof(temp)-1);
	printf("\tWorkstations:\t%s\n", temp);
	
	unistr2_to_ascii(temp, &usr->uni_unknown_str, sizeof(temp)-1);
	printf("\tUnknown Str :\t%s\n", temp);
	
	unistr2_to_ascii(temp, &usr->uni_munged_dial, sizeof(temp)-1);
	printf("\tRemote Dial :\t%s\n", temp);
	
	printf("\tLogon Time               :\t%s\n", 
	       http_timestring(nt_time_to_unix(&usr->logon_time)));
	printf("\tLogoff Time              :\t%s\n", 
	       http_timestring(nt_time_to_unix(&usr->logoff_time)));
	printf("\tKickoff Time             :\t%s\n", 
	       http_timestring(nt_time_to_unix(&usr->kickoff_time)));
	printf("\tPassword last set Time   :\t%s\n", 
	       http_timestring(nt_time_to_unix(&usr->pass_last_set_time)));
	printf("\tPassword can change Time :\t%s\n", 
	       http_timestring(nt_time_to_unix(&usr->pass_can_change_time)));
	printf("\tPassword must change Time:\t%s\n", 
	       http_timestring(nt_time_to_unix(&usr->pass_must_change_time)));
	
	printf("\tunknown_2[0..31]...\n"); /* user passwords? */
	
	printf("\tuser_rid :\t%x\n"  , usr->user_rid ); /* User ID */
	printf("\tgroup_rid:\t%x\n"  , usr->group_rid); /* Group ID */
	printf("\tacb_info :\t%04x\n", usr->acb_info ); /* Account Control Info */
	
	printf("\tunknown_3:\t%08x\n", usr->unknown_3); /* 0x00ff ffff */
	printf("\tlogon_divs:\t%d\n", usr->logon_divs); /* 0x0000 00a8 which is 168 which is num hrs in a week */
	printf("\tunknown_5:\t%08x\n", usr->unknown_5); /* 0x0002 0000 */
	
	printf("\tpadding1[0..7]...\n");
	
	if (usr->ptr_logon_hrs) {
		printf("\tlogon_hrs[0..%d]...\n", usr->logon_hrs.len);
	}
}

static void display_sam_unk_info_2(SAM_UNK_INFO_2 *info2)
{
	fstring name;

	unistr2_to_ascii(name, &info2->uni_domain, sizeof(name) - 1); 
	printf("Domain:\t%s\n", name);

	unistr2_to_ascii(name, &info2->uni_server, sizeof(name) - 1); 
	printf("Server:\t%s\n", name);

	printf("Total Users:\t%d\n", info2->num_domain_usrs);
	printf("Total Groups:\t%d\n", info2->num_domain_grps);
	printf("Total Aliases:\t%d\n", info2->num_local_grps);
	
	printf("Sequence No:\t%d\n", info2->seq_num);
	
	printf("Unknown 0:\t0x%x\n", info2->unknown_0);
	printf("Unknown 1:\t0x%x\n", info2->unknown_1);
	printf("Unknown 2:\t0x%x\n", info2->unknown_2);
	printf("Unknown 3:\t0x%x\n", info2->unknown_3);
	printf("Unknown 4:\t0x%x\n", info2->unknown_4);
	printf("Unknown 5:\t0x%x\n", info2->unknown_5);
	printf("Unknown 6:\t0x%x\n", info2->unknown_6);
}

void display_sam_info_1(SAM_ENTRY1 *e1, SAM_STR1 *s1)
{
	fstring tmp;

	printf("RID: 0x%x ", e1->rid_user);
	
	unistr2_to_ascii(tmp, &s1->uni_acct_name, sizeof(tmp)-1);
	printf("Account: %s\t", tmp);

	unistr2_to_ascii(tmp, &s1->uni_full_name, sizeof(tmp)-1);
	printf("Name: %s\t", tmp);

	unistr2_to_ascii(tmp, &s1->uni_acct_desc, sizeof(tmp)-1);
	printf("Desc: %s\n", tmp);
}

/**********************************************************************
 * Query user information 
 */
static uint32 cmd_samr_query_user(struct cli_state *cli, int argc, char **argv) 
{
	POLICY_HND connect_pol, domain_pol, user_pol;
	uint32 	result = NT_STATUS_UNSUCCESSFUL, 
		info_level = 21;
	BOOL 	got_connect_pol = False, 
		got_domain_pol = False,
		got_user_pol = False;
	SAM_USERINFO_CTR *user_ctr;
	fstring			server;
	TALLOC_CTX		*mem_ctx;
	uint32 user_rid;
	
	
	if (argc != 2) {
		printf("Usage: %s rid\n", argv[0]);
		return 0;
	}
	
	sscanf(argv[1], "%i", &user_rid);

	if (!(mem_ctx=talloc_init()))
	{
		DEBUG(0,("cmd_samr_query_user: talloc_init returned NULL!\n"));
		return NT_STATUS_UNSUCCESSFUL;
	}

	fetch_domain_sid(cli);

	/* Initialise RPC connection */
	if (!cli_nt_session_open (cli, PIPE_SAMR)) {
		fprintf (stderr, "Could not initialize samr pipe!\n");
		return NT_STATUS_UNSUCCESSFUL;
	}
	
	slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
	strupper (server);
	
	if ((result = cli_samr_connect(cli, mem_ctx, server, MAXIMUM_ALLOWED_ACCESS,
				       &connect_pol)) !=
	    NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_connect_pol = True;

	if ((result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
					   MAXIMUM_ALLOWED_ACCESS,
					   &domain_sid, &domain_pol))
	     != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_domain_pol = True;

	if ((result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
					 MAXIMUM_ALLOWED_ACCESS,
					 user_rid, &user_pol))
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_user_pol = True;

	ZERO_STRUCT(user_ctr);

	if ((result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol, 
					      info_level, &user_ctr)) 
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	display_sam_user_info_21(user_ctr->info.id21);

done:
	if (got_user_pol) cli_samr_close(cli, mem_ctx, &user_pol);
	if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
	if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);

	cli_nt_session_close(cli);
	talloc_destroy(mem_ctx);

	return result;
}

/****************************************************************************
 display group info
 ****************************************************************************/
static void display_group_info1(GROUP_INFO1 *info1)
{
	fstring temp;

	unistr2_to_ascii(temp, &info1->uni_acct_name, sizeof(temp)-1);
	printf("\tGroup Name:\t%s\n", temp);
	unistr2_to_ascii(temp, &info1->uni_acct_desc, sizeof(temp)-1);
	printf("\tDescription:\t%s\n", temp);
	printf("\tunk1:%d\n", info1->unknown_1);
	printf("\tNum Members:%d\n", info1->num_members);
}

/****************************************************************************
 display group info
 ****************************************************************************/
static void display_group_info4(GROUP_INFO4 *info4)
{
	fstring desc;

	unistr2_to_ascii(desc, &info4->uni_acct_desc, sizeof(desc)-1);
	printf("\tGroup Description:%s\n", desc);
}

/****************************************************************************
 display sam sync structure
 ****************************************************************************/
static void display_group_info_ctr(GROUP_INFO_CTR *ctr)
{
	switch (ctr->switch_value1) {
	    case 1: {
		    display_group_info1(&ctr->group.info1);
		    break;
	    }
	    case 4: {
		    display_group_info4(&ctr->group.info4);
		    break;
	    }
	}
}

/***********************************************************************
 * Query group information 
 */
static uint32 cmd_samr_query_group(struct cli_state *cli, int argc, char **argv) 
{
	POLICY_HND connect_pol, domain_pol, group_pol;
	uint32 result = NT_STATUS_UNSUCCESSFUL, info_level = 1;
	BOOL got_connect_pol = False, got_domain_pol = False,
		got_group_pol = False;
	GROUP_INFO_CTR group_ctr;
	fstring			server;	
	TALLOC_CTX		*mem_ctx;
	uint32 group_rid;
	
	if (argc != 2) {
		printf("Usage: %s rid\n", argv[0]);
		return 0;
	}

	group_rid = atoi(argv[1]);

	if (!(mem_ctx=talloc_init())) {
		DEBUG(0,("cmd_samr_query_group: talloc_init returned NULL!\n"));
		return NT_STATUS_UNSUCCESSFUL;
	}

	fetch_domain_sid(cli);

	/* Initialise RPC connection */
	if (!cli_nt_session_open (cli, PIPE_SAMR)) {
		fprintf (stderr, "Could not initialize samr pipe!\n");
		return NT_STATUS_UNSUCCESSFUL;
	}
	
	slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
	strupper (server);

	if ((result = cli_samr_connect(cli, mem_ctx, server, MAXIMUM_ALLOWED_ACCESS,
				       &connect_pol)) !=
	    NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_connect_pol = True;

	if ((result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
					   MAXIMUM_ALLOWED_ACCESS,
					   &domain_sid, &domain_pol))
	     != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_domain_pol = True;

	if ((result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
					  MAXIMUM_ALLOWED_ACCESS,
					  group_rid, &group_pol))
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_group_pol = True;

	ZERO_STRUCT(group_ctr);

	if ((result = cli_samr_query_groupinfo(cli, mem_ctx, &group_pol, 
					       info_level, &group_ctr)) 
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	display_group_info_ctr(&group_ctr);

done:
	if (got_group_pol) cli_samr_close(cli, mem_ctx, &group_pol);
	if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
	if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);

	cli_nt_session_close(cli);
	talloc_destroy(mem_ctx);

	return result;
}

/* Query groups a user is a member of */

static uint32 cmd_samr_query_usergroups(struct cli_state *cli, int argc, char **argv) 
{
	POLICY_HND 		connect_pol, 
				domain_pol, 
				user_pol;
	uint32 			result = NT_STATUS_UNSUCCESSFUL;
	BOOL 			got_connect_pol = False, 
				got_domain_pol = False,
				got_user_pol = False;
	uint32 			num_groups, 
				user_rid;
	DOM_GID 		*user_gids;
	int 			i;
	fstring			server;
	TALLOC_CTX		*mem_ctx;
	
	if (argc != 2) {
		printf("Usage: %s rid\n", argv[0]);
		return 0;
	}

	if (!(mem_ctx=talloc_init()))
	{
		DEBUG(0,("cmd_samr_query_usergroups: talloc_init returned NULL!\n"));
		return NT_STATUS_UNSUCCESSFUL;
	}

	sscanf(argv[1], "%i", &user_rid);

	fetch_domain_sid(cli);

	/* Initialise RPC connection */
	if (!cli_nt_session_open (cli, PIPE_SAMR)) {
		fprintf (stderr, "Could not initialize samr pipe!\n");
		return NT_STATUS_UNSUCCESSFUL;
	}

	slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
	strupper (server);
		
	if ((result = cli_samr_connect(cli, mem_ctx, server, MAXIMUM_ALLOWED_ACCESS,
				       &connect_pol)) !=
	    NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_connect_pol = True;

	if ((result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
					   MAXIMUM_ALLOWED_ACCESS,
					   &domain_sid, &domain_pol))
	     != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_domain_pol = True;

	if ((result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
					 MAXIMUM_ALLOWED_ACCESS,
					 user_rid, &user_pol))
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_user_pol = True;

	if ((result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
						&num_groups, &user_gids))
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	for (i = 0; i < num_groups; i++) {
		printf("\tgroup rid:[0x%x] attr:[0x%x]\n", 
		       user_gids[i].g_rid, user_gids[i].attr);
	}

 done:
	if (got_user_pol) cli_samr_close(cli, mem_ctx, &user_pol);
	if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
	if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);

	cli_nt_session_close(cli);
	talloc_destroy(mem_ctx);

	return result;
}

/* Query members of a group */

static uint32 cmd_samr_query_groupmem(struct cli_state *cli, int argc, char **argv) 
{
	POLICY_HND connect_pol, domain_pol, group_pol;
	uint32 result = NT_STATUS_UNSUCCESSFUL;
	BOOL 	got_connect_pol = False, 
		got_domain_pol = False,
		got_group_pol = False;
	uint32 num_members, *group_rids, *group_attrs, group_rid;
	int i;
	fstring			server;
	TALLOC_CTX		*mem_ctx;
	
	if (argc != 2) {
		printf("Usage: %s rid\n", argv[0]);
		return 0;
	}

	if (!(mem_ctx=talloc_init()))
	{
		DEBUG(0,("cmd_samr_query_groupmem: talloc_init returned NULL!\n"));
		return NT_STATUS_UNSUCCESSFUL;
	}

	sscanf(argv[1], "%i", &group_rid);

	fetch_domain_sid(cli);

	/* Initialise RPC connection */
	if (!cli_nt_session_open (cli, PIPE_SAMR)) {
		fprintf (stderr, "Could not initialize samr pipe!\n");
		return NT_STATUS_UNSUCCESSFUL;
	}

	slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
	strupper (server);

	if ((result = cli_samr_connect(cli, mem_ctx, server, MAXIMUM_ALLOWED_ACCESS,
				       &connect_pol)) !=
	    NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_connect_pol = True;

	if ((result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
					   MAXIMUM_ALLOWED_ACCESS,
					   &domain_sid, &domain_pol))
	     != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_domain_pol = True;

	if ((result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
					  MAXIMUM_ALLOWED_ACCESS,
					  group_rid, &group_pol))
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_group_pol = True;

	if ((result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
					      &num_members, &group_rids,
					      &group_attrs))
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	for (i = 0; i < num_members; i++) {
		printf("\trid:[0x%x] attr:[0x%x]\n", group_rids[i],
		       group_attrs[i]);
	}

 done:
	if (got_group_pol) cli_samr_close(cli, mem_ctx, &group_pol);
	if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
	if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);

	cli_nt_session_close(cli);
	talloc_destroy(mem_ctx);

	return result;
}

/* Enumerate domain groups */

static uint32 cmd_samr_enum_dom_groups(struct cli_state *cli, int argc, 
				       char **argv) 
{
	POLICY_HND connect_pol, domain_pol;
	uint32 result = NT_STATUS_UNSUCCESSFUL;
	BOOL got_connect_pol = False, got_domain_pol = False;
	TALLOC_CTX *mem_ctx;
	fstring server;
	uint32 start_idx, size, num_dom_groups, i;
	struct acct_info *dom_groups;

	if (argc != 1) {
		printf("Usage: %s\n", argv[0]);
		return 0;
	}

	if (!(mem_ctx = talloc_init())) {
		DEBUG(0, ("cmd_samr_enum_dom_groups: talloc_init returned "
			  "NULL!\n"));
		return NT_STATUS_UNSUCCESSFUL;
	}

	fetch_domain_sid(cli);

	/* Initialise RPC connection */

	if (!cli_nt_session_open (cli, PIPE_SAMR)) {
		fprintf (stderr, "Could not initialize samr pipe!\n");
		return NT_STATUS_UNSUCCESSFUL;
	}

	slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
	strupper(server);

	/* Get sam policy handle */

	if ((result = cli_samr_connect(cli, mem_ctx, server, 
				       MAXIMUM_ALLOWED_ACCESS, 
				       &connect_pol)) !=
	    NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_connect_pol = True;

	/* Get domain policy handle */

	if ((result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
					   MAXIMUM_ALLOWED_ACCESS,
					   &domain_sid, &domain_pol))
	     != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_domain_pol = True;

	/* Enumerate domain groups */

	start_idx = 0;
	size = 0xffff;

	result = cli_samr_enum_dom_groups(cli, mem_ctx, &domain_pol,
					  &start_idx, size,
					  &dom_groups, &num_dom_groups);

	for (i = 0; i < num_dom_groups; i++)
		printf("group:[%s] rid:[0x%x]\n", dom_groups[i].acct_name,
		       dom_groups[i].rid);

 done:
	if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
	if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);

	cli_nt_session_close(cli);
	talloc_destroy(mem_ctx);

	return result;
}

/* Query alias membership */

static uint32 cmd_samr_query_aliasmem(struct cli_state *cli, int argc, 
				      char **argv) 
{
	POLICY_HND connect_pol, domain_pol, alias_pol;
	BOOL got_connect_pol = False, got_domain_pol = False,
		got_alias_pol = False;
	TALLOC_CTX *mem_ctx;
	uint32 result = NT_STATUS_UNSUCCESSFUL, alias_rid, num_members, i;
	DOM_SID *alias_sids;

	fstring server;

	if (argc != 2) {
		printf("Usage: %s rid\n", argv[0]);
		return 0;
	}

	if (!(mem_ctx=talloc_init())) {
		DEBUG(0,("cmd_samr_query_aliasmem: talloc_init() "
			 "returned NULL!\n"));
		return NT_STATUS_UNSUCCESSFUL;
	}

	sscanf(argv[1], "%i", &alias_rid);

	/* Initialise RPC connection */

	fetch_domain_sid(cli);

	if (!cli_nt_session_open (cli, PIPE_SAMR)) {
		fprintf (stderr, "Could not initialize samr pipe!\n");
		return NT_STATUS_UNSUCCESSFUL;
	}

	/* Open SAMR handle */

	slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
	strupper(server);

	if ((result = cli_samr_connect(cli, mem_ctx, server, 
				       MAXIMUM_ALLOWED_ACCESS, 
				       &connect_pol)) != 
	    NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_connect_pol = True;

	/* Open handle on domain */

	if ((result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
					   MAXIMUM_ALLOWED_ACCESS,
					   &domain_sid, &domain_pol))
	     != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_domain_pol = True;

	/* Open handle on alias */

	if ((result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
					  MAXIMUM_ALLOWED_ACCESS,
					  alias_rid, &alias_pol))
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_alias_pol = True;

	if ((result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
					      &num_members, &alias_sids))
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	for (i = 0; i < num_members; i++) {
		fstring sid_str;

		sid_to_string(sid_str, &alias_sids[i]);
		printf("\tsid:[%s]\n", sid_str);
	}

 done:
	if (got_alias_pol) cli_samr_close(cli, mem_ctx, &alias_pol);
	if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
	if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);

	cli_nt_session_close(cli);
	talloc_destroy(mem_ctx);

	return result;
}

/* Query display info */

static uint32 cmd_samr_query_dispinfo(struct cli_state *cli, int argc, 
				      char **argv) 
{
	POLICY_HND connect_pol, domain_pol;
	uint32 result = NT_STATUS_UNSUCCESSFUL;
	BOOL got_connect_pol = False, got_domain_pol = False;
	TALLOC_CTX *mem_ctx;
	fstring server;
	uint32 start_idx, max_entries, num_entries, i;
	uint16 info_level = 1;
	SAM_DISPINFO_CTR ctr;
	SAM_DISPINFO_1 info1;

	if (argc != 1) {
		printf("Usage: %s\n", argv[0]);
		return 0;
	}

	if (!(mem_ctx = talloc_init())) {
		DEBUG(0, ("cmd_samr_query_dispinfo: talloc_init returned "
			  "NULL!\n"));
		return NT_STATUS_UNSUCCESSFUL;
	}

	fetch_domain_sid(cli);

	/* Initialise RPC connection */

	if (!cli_nt_session_open (cli, PIPE_SAMR)) {
		fprintf (stderr, "Could not initialize samr pipe!\n");
		return NT_STATUS_UNSUCCESSFUL;
	}

	slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
	strupper(server);

	/* Get sam policy handle */

	if ((result = cli_samr_connect(cli, mem_ctx, server, 
				       MAXIMUM_ALLOWED_ACCESS, 
				       &connect_pol)) 
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_connect_pol = True;

	/* Get domain policy handle */

	if ((result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
					   MAXIMUM_ALLOWED_ACCESS, 
					   &domain_sid, &domain_pol))
	     != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_domain_pol = True;

	/* Query display info */

	start_idx = 0;
	max_entries = 250;

	ZERO_STRUCT(ctr);
	ZERO_STRUCT(info1);

	ctr.sam.info1 = &info1;

	result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
					 &start_idx, info_level,
					 &num_entries, max_entries, &ctr);

	for (i = 0; i < num_entries; i++) {
		display_sam_info_1(&ctr.sam.info1->sam[i],
				   &ctr.sam.info1->str[i]);
	}

 done:
	if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
	if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);

	cli_nt_session_close(cli);
	talloc_destroy(mem_ctx);

	return result;
}

/* Query domain info */

static uint32 cmd_samr_query_dominfo(struct cli_state *cli, int argc, 
				     char **argv) 
{
	POLICY_HND connect_pol, domain_pol;
	uint32 result = NT_STATUS_UNSUCCESSFUL;
	BOOL got_connect_pol = False, got_domain_pol = False;
	TALLOC_CTX *mem_ctx;
	fstring server;
	uint16 switch_value = 2;
	SAM_UNK_CTR ctr;

	if (argc > 2) {
		printf("Usage: %s [infolevel\n", argv[0]);
		return 0;
	}

	if (argc == 2)
		switch_value = atoi(argv[1]);

	if (!(mem_ctx = talloc_init())) {
		DEBUG(0, ("cmd_samr_query_dispinfo: talloc_init returned "
			  "NULL!\n"));
		return NT_STATUS_UNSUCCESSFUL;
	}

	fetch_domain_sid(cli);

	/* Initialise RPC connection */

	if (!cli_nt_session_open (cli, PIPE_SAMR)) {
		fprintf (stderr, "Could not initialize samr pipe!\n");
		return NT_STATUS_UNSUCCESSFUL;
	}

	slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
	strupper(server);

	/* Get sam policy handle */

	if ((result = cli_samr_connect(cli, mem_ctx, server, 
				       MAXIMUM_ALLOWED_ACCESS, 
				       &connect_pol)) 
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_connect_pol = True;

	/* Get domain policy handle */

	if ((result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
					   MAXIMUM_ALLOWED_ACCESS,
					   &domain_sid, &domain_pol))
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	got_domain_pol = True;

	/* Query domain info */

	if ((result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
					      switch_value, &ctr))
	    != NT_STATUS_NOPROBLEMO) {
		goto done;
	}

	/* Display domain info */

	switch (switch_value) {
	case 2:
		display_sam_unk_info_2(&ctr.info.inf2);
		break;
	default:
		printf("cannot display domain info for switch value %d\n",
		       switch_value);
		break;
	}

 done:
	if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
	if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);

	cli_nt_session_close(cli);
	talloc_destroy(mem_ctx);

	return result;
}

/* List of commands exported by this module */

struct cmd_set samr_commands[] = {
	{ "SAMR", 		NULL,		 		"" },

	{ "queryuser", 		cmd_samr_query_user, 		"Query user info" },
	{ "querygroup", 	cmd_samr_query_group, 		"Query group info" },
	{ "queryusergroups", 	cmd_samr_query_usergroups, 	"Query user groups" },
	{ "querygroupmem", 	cmd_samr_query_groupmem, 	"Query group membership" },
	{ "queryaliasmem", 	cmd_samr_query_aliasmem, 	"Query alias membership" },
	{ "querydispinfo", 	cmd_samr_query_dispinfo, 	"Query display info" },
	{ "querydominfo", 	cmd_samr_query_dominfo, 	"Query domain info" },
	{ "enumdomgroups",      cmd_samr_enum_dom_groups,       "Enumerate domain groups" },

	{ NULL, NULL, NULL }
};