summaryrefslogtreecommitdiff
path: root/source3/rpc_client/cli_samr.c
blob: eb9032f7ce7282cde1d049e7682982d4154784a7 (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
#define OLD_NTDOMAIN 1
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   NT Domain Authentication SMB / MSRPC client
   Copyright (C) Andrew Tridgell 1994-1997
   Copyright (C) Luke Kenneth Casson Leighton 1996-1997
   Copyright (C) Jeremy Allison 1999.
   
   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.
*/



#ifdef SYSLOG
#undef SYSLOG
#endif

#include "includes.h"

extern int DEBUGLEVEL;

/****************************************************************************
do a SAMR query user groups
****************************************************************************/
BOOL get_samr_query_usergroups(struct cli_state *cli, 
				POLICY_HND *pol_open_domain, uint32 user_rid,
				uint32 *num_groups, DOM_GID *gid)
{
	POLICY_HND pol_open_user;
	if (pol_open_domain == NULL || num_groups == NULL || gid == NULL)
		return False;

	/* send open domain (on user sid) */
	if (!do_samr_open_user(cli,
				pol_open_domain,
				0x02011b, user_rid,
				&pol_open_user))
	{
		return False;
	}

	/* send user groups query */
	if (!do_samr_query_usergroups(cli,
				&pol_open_user,
				num_groups, gid))
	{
		DEBUG(5,("do_samr_query_usergroups: error in query user groups\n"));
	}

	return do_samr_close(cli, &pol_open_user);
}

/****************************************************************************
do a SAMR query user info
****************************************************************************/
BOOL get_samr_query_userinfo(struct cli_state *cli, 
				POLICY_HND *pol_open_domain,
				uint32 info_level,
				uint32 user_rid, SAM_USER_INFO_21 *usr)
{
	POLICY_HND pol_open_user;
	if (pol_open_domain == NULL || usr == NULL)
		return False;

	memset((char *)usr, '\0', sizeof(*usr));

	/* send open domain (on user sid) */
	if (!do_samr_open_user(cli,
				pol_open_domain,
				0x02011b, user_rid,
				&pol_open_user))
	{
		return False;
	}

	/* send user info query */
	if (!do_samr_query_userinfo(cli,
				&pol_open_user,
				info_level, (void*)usr))
	{
		DEBUG(5,("do_samr_query_userinfo: error in query user info, level 0x%x\n",
		          info_level));
	}

	return do_samr_close(cli, &pol_open_user);
}

/****************************************************************************
do a SAMR change user password command
****************************************************************************/
BOOL do_samr_chgpasswd_user(struct cli_state *cli,
		char *srv_name, char *user_name,
		char nt_newpass[516], uchar nt_oldhash[16],
		char lm_newpass[516], uchar lm_oldhash[16])
{
	prs_struct data;
	prs_struct rdata;
	SAMR_Q_CHGPASSWD_USER q_e;
	SAMR_R_CHGPASSWD_USER r_e;

	/* create and send a MSRPC command with api SAMR_CHGPASSWD_USER */

	prs_init(&data, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
	prs_init(&rdata, 0, cli->mem_ctx, UNMARSHALL);

	DEBUG(4,("SAMR Change User Password. server:%s username:%s\n",
	        srv_name, user_name));

	init_samr_q_chgpasswd_user(&q_e, srv_name, user_name,
	                           nt_newpass, nt_oldhash,
	                           lm_newpass, lm_oldhash);

	/* turn parameters into data stream */
	if(!samr_io_q_chgpasswd_user("", &q_e, &data, 0)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	/* send the data on \PIPE\ */
	if (!rpc_api_pipe_req(cli, SAMR_CHGPASSWD_USER, &data, &rdata)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&data);

	if(!samr_io_r_chgpasswd_user("", &r_e, &rdata, 0)) {
		prs_mem_free(&rdata);
		return False;
	}

	if (r_e.status != 0) {
		/* report error code */
		DEBUG(0,("SAMR_R_CHGPASSWD_USER: %s\n", get_nt_error_msg(r_e.status)));
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&rdata);

	return True;
}

/****************************************************************************
do a SAMR unknown 0x38 command
****************************************************************************/
BOOL do_samr_unknown_38(struct cli_state *cli, char *srv_name)
{
	prs_struct data;
	prs_struct rdata;

	SAMR_Q_UNKNOWN_38 q_e;
	SAMR_R_UNKNOWN_38 r_e;

	/* create and send a MSRPC command with api SAMR_ENUM_DOM_USERS */

	prs_init(&data, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
	prs_init(&rdata, 0, cli->mem_ctx, UNMARSHALL);

	DEBUG(4,("SAMR Unknown 38 server:%s\n", srv_name));

	init_samr_q_unknown_38(&q_e, srv_name);

	/* turn parameters into data stream */
	if(!samr_io_q_unknown_38("", &q_e, &data, 0)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	/* send the data on \PIPE\ */
	if (!rpc_api_pipe_req(cli, SAMR_UNKNOWN_38, &data, &rdata)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&data);

	if(!samr_io_r_unknown_38("", &r_e, &rdata, 0)) {
		prs_mem_free(&rdata);
		return False;
	}

	if (r_e.status != 0) {
		/* report error code */
		DEBUG(0,("SAMR_R_UNKNOWN_38: %s\n", get_nt_error_msg(r_e.status)));
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&rdata);

	return True;
}

/****************************************************************************
do a SAMR unknown 0x8 command
****************************************************************************/
BOOL do_samr_query_dom_info(struct cli_state *cli, 
				POLICY_HND *domain_pol, uint16 switch_value)
{
	prs_struct data;
	prs_struct rdata;
	SAMR_Q_QUERY_DOMAIN_INFO q_e;
	SAMR_R_QUERY_DOMAIN_INFO r_e;

	if (domain_pol == NULL)
		return False;

	/* create and send a MSRPC command with api SAMR_ENUM_DOM_USERS */

	prs_init(&data, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
	prs_init(&rdata, 0, cli->mem_ctx, UNMARSHALL);

	DEBUG(4,("SAMR Unknown 8 switch:%d\n", switch_value));

	/* store the parameters */
	init_samr_q_query_dom_info(&q_e, domain_pol, switch_value);

	/* turn parameters into data stream */
	if(!samr_io_q_query_dom_info("", &q_e, &data, 0)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	/* send the data on \PIPE\ */
	if (!rpc_api_pipe_req(cli, SAMR_QUERY_DOMAIN_INFO, &data, &rdata)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&data);

	if(!samr_io_r_query_dom_info("", &r_e, &rdata, 0)) {
		prs_mem_free(&rdata);
		return False;
	}

	if (r_e.status != 0) {
		/* report error code */
		DEBUG(0,("SAMR_R_QUERY_DOMAIN_INFO: %s\n", get_nt_error_msg(r_e.status)));
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&rdata);

	return True;
}

/****************************************************************************
do a SAMR enumerate users
****************************************************************************/
BOOL do_samr_enum_dom_users(struct cli_state *cli, 
				POLICY_HND *pol, uint16 num_entries, uint16 unk_0,
				uint16 acb_mask, uint16 unk_1, uint32 size,
				struct acct_info **sam,
				int *num_sam_users)
{
	prs_struct data;
	prs_struct rdata;
	SAMR_Q_ENUM_DOM_USERS q_e;
	SAMR_R_ENUM_DOM_USERS r_e;
	int i;
	int name_idx = 0;

	if (pol == NULL || num_sam_users == NULL)
		return False;

	/* create and send a MSRPC command with api SAMR_ENUM_DOM_USERS */

	prs_init(&data, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
	prs_init(&rdata, 0, cli->mem_ctx, UNMARSHALL);

	DEBUG(4,("SAMR Enum SAM DB max size:%x\n", size));

	/* store the parameters */
	init_samr_q_enum_dom_users(&q_e, pol,
	                           num_entries, unk_0,
	                           acb_mask, unk_1, size);

	/* turn parameters into data stream */
	if(!samr_io_q_enum_dom_users("", &q_e, &data, 0)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	/* send the data on \PIPE\ */
	if (!rpc_api_pipe_req(cli, SAMR_ENUM_DOM_USERS, &data, &rdata)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&data);

	if(!samr_io_r_enum_dom_users("", &r_e, &rdata, 0)) {
		prs_mem_free(&rdata);
		return False;
	}

	if (r_e.status != 0) {
		/* report error code */
		DEBUG(0,("SAMR_R_ENUM_DOM_USERS: %s\n", get_nt_error_msg(r_e.status)));
		prs_mem_free(&rdata);
		return False;
	}

	*num_sam_users = r_e.num_entries2;
	if (*num_sam_users > MAX_SAM_ENTRIES) {
		*num_sam_users = MAX_SAM_ENTRIES;
		DEBUG(2,("do_samr_enum_dom_users: sam user entries limited to %d\n",
		          *num_sam_users));
	}

	*sam = (struct acct_info*) malloc(sizeof(struct acct_info) * (*num_sam_users));
				    
	if ((*sam) == NULL)
		*num_sam_users = 0;

	for (i = 0; i < *num_sam_users; i++) {
		(*sam)[i].smb_userid = r_e.sam[i].rid;
		if (r_e.sam[i].hdr_name.buffer) {
			char *acct_name = dos_unistrn2(r_e.uni_acct_name[name_idx].buffer,
			                           r_e.uni_acct_name[name_idx].uni_str_len);
			fstrcpy((*sam)[i].acct_name, acct_name);
			name_idx++;
		} else {
			memset((char *)(*sam)[i].acct_name, '\0', sizeof((*sam)[i].acct_name));
		}

		DEBUG(5,("do_samr_enum_dom_users: idx: %4d rid: %8x acct: %s\n",
		          i, (*sam)[i].smb_userid, (*sam)[i].acct_name));
	}

	prs_mem_free(&rdata  );

	return True;
}

/****************************************************************************
do a SAMR Connect
****************************************************************************/
BOOL do_samr_connect(struct cli_state *cli, 
				char *srv_name, uint32 unknown_0,
				POLICY_HND *connect_pol)
{
	prs_struct data;
	prs_struct rdata;
	SAMR_Q_CONNECT q_o;
	SAMR_R_CONNECT r_o;

	if (srv_name == NULL || connect_pol == NULL)
		return False;

	/* create and send a MSRPC command with api SAMR_CONNECT */

	prs_init(&data, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
	prs_init(&rdata, 0, cli->mem_ctx, UNMARSHALL);

	DEBUG(4,("SAMR Open Policy server:%s undoc value:%x\n",
				srv_name, unknown_0));

	/* store the parameters */
	init_samr_q_connect(&q_o, srv_name, unknown_0);

	/* turn parameters into data stream */
	if(!samr_io_q_connect("", &q_o,  &data, 0)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	/* send the data on \PIPE\ */
	if (!rpc_api_pipe_req(cli, SAMR_CONNECT, &data, &rdata)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&data);

	if(!samr_io_r_connect("", &r_o, &rdata, 0)) {
		prs_mem_free(&rdata);
		return False;
	}
		
	if (r_o.status != 0) {
		/* report error code */
		DEBUG(0,("SAMR_R_CONNECT: %s\n", get_nt_error_msg(r_o.status)));
		prs_mem_free(&rdata);
		return False;
	}

	memcpy(connect_pol, &r_o.connect_pol, sizeof(r_o.connect_pol));

	prs_mem_free(&rdata);

	return True;
}

/****************************************************************************
do a SAMR Open User
****************************************************************************/
BOOL do_samr_open_user(struct cli_state *cli, 
				POLICY_HND *pol, uint32 unk_0, uint32 rid, 
				POLICY_HND *user_pol)
{
	prs_struct data;
	prs_struct rdata;
	SAMR_Q_OPEN_USER q_o;
	SAMR_R_OPEN_USER r_o;

	if (pol == NULL || user_pol == NULL)
		return False;

	/* create and send a MSRPC command with api SAMR_OPEN_USER */

	prs_init(&data, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
	prs_init(&rdata, 0, cli->mem_ctx, UNMARSHALL);

	DEBUG(4,("SAMR Open User.  unk_0: %08x RID:%x\n",
	          unk_0, rid));

	/* store the parameters */
	init_samr_q_open_user(&q_o, pol, unk_0, rid);

	/* turn parameters into data stream */
	if(!samr_io_q_open_user("", &q_o,  &data, 0)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	/* send the data on \PIPE\ */
	if (!rpc_api_pipe_req(cli, SAMR_OPEN_USER, &data, &rdata)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&data);

	if(!samr_io_r_open_user("", &r_o, &rdata, 0)) {
		prs_mem_free(&rdata);
		return False;
	}
		
	if (r_o.status != 0) {
		/* report error code */
		DEBUG(0,("SAMR_R_OPEN_USER: %s\n", get_nt_error_msg(r_o.status)));
		prs_mem_free(&rdata);
		return False;
	}

	memcpy(user_pol, &r_o.user_pol, sizeof(r_o.user_pol));

	prs_mem_free(&rdata);

	return True;
}

/****************************************************************************
do a SAMR Open Domain
****************************************************************************/
BOOL do_samr_open_domain(struct cli_state *cli, 
				POLICY_HND *connect_pol, uint32 rid, DOM_SID *sid,
				POLICY_HND *domain_pol)
{
	prs_struct data;
	prs_struct rdata;
	pstring sid_str;
	SAMR_Q_OPEN_DOMAIN q_o;
	SAMR_R_OPEN_DOMAIN r_o;

	if (connect_pol == NULL || sid == NULL || domain_pol == NULL)
		return False;

	/* create and send a MSRPC command with api SAMR_OPEN_DOMAIN */

	prs_init(&data, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
	prs_init(&rdata, 0, cli->mem_ctx, UNMARSHALL);

	sid_to_string(sid_str, sid);
	DEBUG(4,("SAMR Open Domain.  SID:%s RID:%x\n", sid_str, rid));

	/* store the parameters */
	init_samr_q_open_domain(&q_o, connect_pol, rid, sid);

	/* turn parameters into data stream */
	if(!samr_io_q_open_domain("", &q_o,  &data, 0)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	/* send the data on \PIPE\ */
	if (!rpc_api_pipe_req(cli, SAMR_OPEN_DOMAIN, &data, &rdata)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&data);

	if(!samr_io_r_open_domain("", &r_o, &rdata, 0)) {
		prs_mem_free(&rdata);
		return False;
	}

	if (r_o.status != 0) {
		/* report error code */
		DEBUG(0,("SAMR_R_OPEN_DOMAIN: %s\n", get_nt_error_msg(r_o.status)));
		prs_mem_free(&rdata);
		return False;
	}

	memcpy(domain_pol, &r_o.domain_pol, sizeof(r_o.domain_pol));

	prs_mem_free(&rdata);

	return True;
}

/****************************************************************************
do a SAMR Query Unknown 12
****************************************************************************/
BOOL do_samr_query_unknown_12(struct cli_state *cli, 
				POLICY_HND *pol, uint32 rid, uint32 num_gids, uint32 *gids,
				uint32 *num_aliases,
				fstring als_names    [MAX_LOOKUP_SIDS],
				uint32  num_als_users[MAX_LOOKUP_SIDS])
{
	prs_struct data;
	prs_struct rdata;
	SAMR_Q_LOOKUP_RIDS q_o;
	SAMR_R_LOOKUP_RIDS r_o;

	if (pol == NULL || rid == 0 || num_gids == 0 || gids == NULL ||
	    num_aliases == NULL || als_names == NULL || num_als_users == NULL )
			return False;

	/* create and send a MSRPC command with api SAMR_UNKNOWN_12 */

	prs_init(&data, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
	prs_init(&rdata, 0, cli->mem_ctx, UNMARSHALL);

	DEBUG(4,("SAMR Query Unknown 12.\n"));

	/* store the parameters */
	init_samr_q_lookup_rids(&q_o, pol, rid, num_gids, gids);

	/* turn parameters into data stream */
	if(!samr_io_q_lookup_rids("", &q_o,  &data, 0)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	/* send the data on \PIPE\ */
	if (!rpc_api_pipe_req(cli, SAMR_LOOKUP_RIDS, &data, &rdata)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&data);

	if(!samr_io_r_lookup_rids("", &r_o, &rdata, 0)) {
		prs_mem_free(&rdata);
		return False;
	}
		
	if (r_o.status != 0) {
		/* report error code */
		DEBUG(0,("SAMR_R_UNKNOWN_12: %s\n", get_nt_error_msg(r_o.status)));
		prs_mem_free(&rdata);
		return False;
	}

	if (r_o.ptr_aliases != 0 && r_o.ptr_als_usrs != 0 &&
	    r_o.num_als_usrs1 == r_o.num_aliases1) {
		int i;

		*num_aliases = r_o.num_aliases1;

		for (i = 0; i < r_o.num_aliases1; i++) {
			fstrcpy(als_names[i], dos_unistrn2(r_o.uni_als_name[i].buffer,
						r_o.uni_als_name[i].uni_str_len));
		}
		for (i = 0; i < r_o.num_als_usrs1; i++) {
			num_als_users[i] = r_o.num_als_usrs[i];
		}
	} else if (r_o.ptr_aliases == 0 && r_o.ptr_als_usrs == 0) {
		*num_aliases = 0;
	} else {
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&rdata);

	return True;
}

/****************************************************************************
do a SAMR Query User Groups
****************************************************************************/
BOOL do_samr_query_usergroups(struct cli_state *cli, 
				POLICY_HND *pol, uint32 *num_groups, DOM_GID *gid)
{
	prs_struct data;
	prs_struct rdata;
	SAMR_Q_QUERY_USERGROUPS q_o;
	SAMR_R_QUERY_USERGROUPS r_o;

	if (pol == NULL || gid == NULL || num_groups == 0)
		return False;

	/* create and send a MSRPC command with api SAMR_QUERY_USERGROUPS */

	prs_init(&data, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
	prs_init(&rdata, 0, cli->mem_ctx, UNMARSHALL);

	DEBUG(4,("SAMR Query User Groups.\n"));

	/* store the parameters */
	init_samr_q_query_usergroups(&q_o, pol);

	/* turn parameters into data stream */
	if(!samr_io_q_query_usergroups("", &q_o,  &data, 0)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	/* send the data on \PIPE\ */
	if (!rpc_api_pipe_req(cli, SAMR_QUERY_USERGROUPS, &data, &rdata)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&data);

	/* get user info */
	r_o.gid = gid;

	if(!samr_io_r_query_usergroups("", &r_o, &rdata, 0)) {
		prs_mem_free(&rdata);
		return False;
	}
		
	if (r_o.status != 0) {
		/* report error code */
		DEBUG(0,("SAMR_R_QUERY_USERGROUPS: %s\n", get_nt_error_msg(r_o.status)));
		prs_mem_free(&rdata);
		return False;
	}

	*num_groups = r_o.num_entries;

	prs_mem_free(&rdata);

	return True;
}

/****************************************************************************
do a SAMR Query User Info
****************************************************************************/
BOOL do_samr_query_userinfo(struct cli_state *cli, 
				POLICY_HND *pol, uint16 switch_value, void* usr)
{
	prs_struct data;
	prs_struct rdata;
	SAMR_Q_QUERY_USERINFO q_o;
	SAMR_R_QUERY_USERINFO r_o;

	if (pol == NULL || usr == NULL || switch_value == 0)
		return False;

	/* create and send a MSRPC command with api SAMR_QUERY_USERINFO */

	prs_init(&data, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
	prs_init(&rdata, 0, cli->mem_ctx, UNMARSHALL);

	DEBUG(4,("SAMR Query User Info.  level: %d\n", switch_value));

	/* store the parameters */
	init_samr_q_query_userinfo(&q_o, pol, switch_value);

	/* turn parameters into data stream */
	if(!samr_io_q_query_userinfo("", &q_o,  &data, 0)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	/* send the data on \PIPE\ */
	if (!rpc_api_pipe_req(cli, SAMR_QUERY_USERINFO, &data, &rdata)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&data);

	/* get user info */
	r_o.info.id = usr;

	if(!samr_io_r_query_userinfo("", &r_o, &rdata, 0)) {
		prs_mem_free(&rdata);
		return False;
	}
		
	if (r_o.status != 0) {
		/* report error code */
		DEBUG(0,("SAMR_R_QUERY_USERINFO: %s\n", get_nt_error_msg(r_o.status)));
		prs_mem_free(&rdata);
		return False;
	}

	if (r_o.switch_value != switch_value) {
		DEBUG(0,("SAMR_R_QUERY_USERINFO: received incorrect level %d\n",
		          r_o.switch_value));
		prs_mem_free(&rdata);
		return False;
	}

	if (r_o.ptr == 0) {
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&rdata);

	return True;
}

/****************************************************************************
do a SAMR Close
****************************************************************************/
BOOL do_samr_close(struct cli_state *cli, POLICY_HND *hnd)
{
	prs_struct data;
	prs_struct rdata;
	SAMR_Q_CLOSE_HND q_c;
	SAMR_R_CLOSE_HND r_c;

	if (hnd == NULL)
		return False;

	prs_init(&data, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
	prs_init(&rdata, 0, cli->mem_ctx, UNMARSHALL);

	/* create and send a MSRPC command with api SAMR_CLOSE_HND */

	DEBUG(4,("SAMR Close\n"));

	/* store the parameters */
	init_samr_q_close_hnd(&q_c, hnd);

	/* turn parameters into data stream */
	if(!samr_io_q_close_hnd("", &q_c,  &data, 0)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	/* send the data on \PIPE\ */
	if (!rpc_api_pipe_req(cli, SAMR_CLOSE_HND, &data, &rdata)) {
		prs_mem_free(&data);
		prs_mem_free(&rdata);
		return False;
	}

	prs_mem_free(&data);

	if(!samr_io_r_close_hnd("", &r_c, &rdata, 0)) {
		prs_mem_free(&rdata);
		return False;
	}

	if (r_c.status != 0) {
		/* report error code */
		DEBUG(0,("SAMR_CLOSE_HND: %s\n", get_nt_error_msg(r_c.status)));
		prs_mem_free(&rdata);
		return False;
	}

	/* check that the returned policy handle is all zeros */

	if (IVAL(&r_c.pol.data1,0) || IVAL(&r_c.pol.data2,0) || SVAL(&r_c.pol.data3,0) ||
		SVAL(&r_c.pol.data4,0) || IVAL(r_c.pol.data5,0) || IVAL(r_c.pol.data5,4) ) {
			DEBUG(0,("SAMR_CLOSE_HND: non-zero handle returned\n"));
			prs_mem_free(&rdata);
			return False;
	}	

	prs_mem_free(&rdata);

	return True;
}

#undef OLD_NTDOMAIN