Exit path cleanup for cli_samr_enum_dom_users()
[samba.git] / source / rpc_client / cli_samr.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4    Copyright (C) Tim Potter                        2000-2001,
5    Copyright (C) Andrew Tridgell              1992-1997,2000,
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1997,2000,
7    Copyright (C) Paul Ashton                       1997,2000,
8    Copyright (C) Elrond                                 2000,
9    Copyright (C) Rafal Szczesniak                       2002.
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "includes.h"
27
28 /* Connect to SAMR database */
29
30 NTSTATUS cli_samr_connect(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
31                           uint32 access_mask, POLICY_HND *connect_pol)
32 {
33         prs_struct qbuf, rbuf;
34         SAMR_Q_CONNECT q;
35         SAMR_R_CONNECT r;
36         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
37
38         ZERO_STRUCT(q);
39         ZERO_STRUCT(r);
40
41         /* Initialise parse structures */
42
43         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
44         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
45
46         /* Marshall data and send request */
47
48         init_samr_q_connect(&q, cli->desthost, access_mask);
49
50         if (!samr_io_q_connect("", &q, &qbuf, 0) ||
51             !rpc_api_pipe_req(cli, SAMR_CONNECT, &qbuf, &rbuf))
52                 goto done;
53
54         /* Unmarshall response */
55
56         if (!samr_io_r_connect("", &r, &rbuf, 0))
57                 goto done;
58
59         /* Return output parameters */
60
61         if (NT_STATUS_IS_OK(result = r.status)) {
62                 *connect_pol = r.connect_pol;
63 #ifdef __INSURE__
64                 connect_pol->marker = malloc(1);
65 #endif
66         }
67
68  done:
69         prs_mem_free(&qbuf);
70         prs_mem_free(&rbuf);
71
72         return result;
73 }
74
75 /* Connect to SAMR database */
76
77 NTSTATUS cli_samr_connect4(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
78                            uint32 access_mask, POLICY_HND *connect_pol)
79 {
80         prs_struct qbuf, rbuf;
81         SAMR_Q_CONNECT4 q;
82         SAMR_R_CONNECT4 r;
83         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
84
85         ZERO_STRUCT(q);
86         ZERO_STRUCT(r);
87
88         /* Initialise parse structures */
89
90         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
91         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
92
93         /* Marshall data and send request */
94
95         init_samr_q_connect4(&q, cli->desthost, access_mask);
96
97         if (!samr_io_q_connect4("", &q, &qbuf, 0) ||
98             !rpc_api_pipe_req(cli, SAMR_CONNECT4, &qbuf, &rbuf))
99                 goto done;
100
101         /* Unmarshall response */
102
103         if (!samr_io_r_connect4("", &r, &rbuf, 0))
104                 goto done;
105
106         /* Return output parameters */
107
108         if (NT_STATUS_IS_OK(result = r.status)) {
109                 *connect_pol = r.connect_pol;
110 #ifdef __INSURE__
111                 connect_pol->marker = malloc(1);
112 #endif
113         }
114
115  done:
116         prs_mem_free(&qbuf);
117         prs_mem_free(&rbuf);
118
119         return result;
120 }
121
122 /* Close SAMR handle */
123
124 NTSTATUS cli_samr_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
125                         POLICY_HND *connect_pol)
126 {
127         prs_struct qbuf, rbuf;
128         SAMR_Q_CLOSE_HND q;
129         SAMR_R_CLOSE_HND r;
130         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
131
132         ZERO_STRUCT(q);
133         ZERO_STRUCT(r);
134
135         /* Initialise parse structures */
136
137         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
138         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
139
140         /* Marshall data and send request */
141
142         init_samr_q_close_hnd(&q, connect_pol);
143
144         if (!samr_io_q_close_hnd("", &q, &qbuf, 0) ||
145             !rpc_api_pipe_req(cli, SAMR_CLOSE_HND, &qbuf, &rbuf))
146                 goto done;
147
148         /* Unmarshall response */
149
150         if (!samr_io_r_close_hnd("", &r, &rbuf, 0))
151                 goto done;
152
153         /* Return output parameters */
154
155         if (NT_STATUS_IS_OK(result = r.status)) {
156 #ifdef __INSURE__
157                 SAFE_FREE(connect_pol->marker);
158 #endif
159                 *connect_pol = r.pol;
160         }
161
162  done:
163         prs_mem_free(&qbuf);
164         prs_mem_free(&rbuf);
165
166         return result;
167 }
168
169 /* Open handle on a domain */
170
171 NTSTATUS cli_samr_open_domain(struct cli_state *cli, TALLOC_CTX *mem_ctx,
172                               POLICY_HND *connect_pol, uint32 access_mask, 
173                               const DOM_SID *domain_sid, POLICY_HND *domain_pol)
174 {
175         prs_struct qbuf, rbuf;
176         SAMR_Q_OPEN_DOMAIN q;
177         SAMR_R_OPEN_DOMAIN r;
178         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
179
180         ZERO_STRUCT(q);
181         ZERO_STRUCT(r);
182
183         /* Initialise parse structures */
184
185         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
186         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
187
188         /* Marshall data and send request */
189
190         init_samr_q_open_domain(&q, connect_pol, access_mask, domain_sid);
191
192         if (!samr_io_q_open_domain("", &q, &qbuf, 0) ||
193             !rpc_api_pipe_req(cli, SAMR_OPEN_DOMAIN, &qbuf, &rbuf))
194                 goto done;
195
196         /* Unmarshall response */
197
198         if (!samr_io_r_open_domain("", &r, &rbuf, 0))
199                 goto done;
200
201         /* Return output parameters */
202
203         if (NT_STATUS_IS_OK(result = r.status)) {
204                 *domain_pol = r.domain_pol;
205 #ifdef __INSURE__
206                 domain_pol->marker = malloc(1);
207 #endif
208         }
209
210  done:
211         prs_mem_free(&qbuf);
212         prs_mem_free(&rbuf);
213
214         return result;
215 }
216
217 /* Open handle on a user */
218
219 NTSTATUS cli_samr_open_user(struct cli_state *cli, TALLOC_CTX *mem_ctx,
220                             POLICY_HND *domain_pol, uint32 access_mask, 
221                             uint32 user_rid, POLICY_HND *user_pol)
222 {
223         prs_struct qbuf, rbuf;
224         SAMR_Q_OPEN_USER q;
225         SAMR_R_OPEN_USER r;
226         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
227
228         ZERO_STRUCT(q);
229         ZERO_STRUCT(r);
230
231         /* Initialise parse structures */
232
233         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
234         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
235
236         /* Marshall data and send request */
237
238         init_samr_q_open_user(&q, domain_pol, access_mask, user_rid);
239
240         if (!samr_io_q_open_user("", &q, &qbuf, 0) ||
241             !rpc_api_pipe_req(cli, SAMR_OPEN_USER, &qbuf, &rbuf))
242                 goto done;
243
244         /* Unmarshall response */
245
246         if (!samr_io_r_open_user("", &r, &rbuf, 0))
247                 goto done;
248
249         /* Return output parameters */
250
251         if (NT_STATUS_IS_OK(result = r.status)) {
252                 *user_pol = r.user_pol;
253 #ifdef __INSURE__
254                 user_pol->marker = malloc(1);
255 #endif
256         }
257
258  done:
259         prs_mem_free(&qbuf);
260         prs_mem_free(&rbuf);
261
262         return result;
263 }
264
265 /* Open handle on a group */
266
267 NTSTATUS cli_samr_open_group(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
268                              POLICY_HND *domain_pol, uint32 access_mask, 
269                              uint32 group_rid, POLICY_HND *group_pol)
270 {
271         prs_struct qbuf, rbuf;
272         SAMR_Q_OPEN_GROUP q;
273         SAMR_R_OPEN_GROUP r;
274         NTSTATUS result =  NT_STATUS_UNSUCCESSFUL;
275
276         ZERO_STRUCT(q);
277         ZERO_STRUCT(r);
278
279         /* Initialise parse structures */
280
281         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
282         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
283
284         /* Marshall data and send request */
285
286         init_samr_q_open_group(&q, domain_pol, access_mask, group_rid);
287
288         if (!samr_io_q_open_group("", &q, &qbuf, 0) ||
289             !rpc_api_pipe_req(cli, SAMR_OPEN_GROUP, &qbuf, &rbuf))
290                 goto done;
291
292         /* Unmarshall response */
293
294         if (!samr_io_r_open_group("", &r, &rbuf, 0))
295                 goto done;
296
297         /* Return output parameters */
298
299         if (NT_STATUS_IS_OK(result = r.status)) {
300                 *group_pol = r.pol;
301 #ifdef __INSURE__
302                 group_pol->marker = malloc(1);
303 #endif
304         }
305
306  done:
307         prs_mem_free(&qbuf);
308         prs_mem_free(&rbuf);
309
310         return result;
311 }
312
313 /* Query user info */
314
315 NTSTATUS cli_samr_query_userinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx,
316                                  POLICY_HND *user_pol, uint16 switch_value, 
317                                  SAM_USERINFO_CTR **ctr)
318 {
319         prs_struct qbuf, rbuf;
320         SAMR_Q_QUERY_USERINFO q;
321         SAMR_R_QUERY_USERINFO r;
322         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
323
324         ZERO_STRUCT(q);
325         ZERO_STRUCT(r);
326
327         /* Initialise parse structures */
328
329         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
330         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
331
332         /* Marshall data and send request */
333
334         init_samr_q_query_userinfo(&q, user_pol, switch_value);
335
336         if (!samr_io_q_query_userinfo("", &q, &qbuf, 0) ||
337             !rpc_api_pipe_req(cli, SAMR_QUERY_USERINFO, &qbuf, &rbuf))
338                 goto done;
339
340         /* Unmarshall response */
341
342         if (!samr_io_r_query_userinfo("", &r, &rbuf, 0))
343                 goto done;
344
345         /* Return output parameters */
346
347         result = r.status;
348         *ctr = r.ctr;
349
350  done:
351         prs_mem_free(&qbuf);
352         prs_mem_free(&rbuf);
353
354         return result;
355 }
356
357 /* Query group info */
358
359 NTSTATUS cli_samr_query_groupinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx,
360                                   POLICY_HND *group_pol, uint32 info_level, 
361                                   GROUP_INFO_CTR **ctr)
362 {
363         prs_struct qbuf, rbuf;
364         SAMR_Q_QUERY_GROUPINFO q;
365         SAMR_R_QUERY_GROUPINFO r;
366         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
367
368         ZERO_STRUCT(q);
369         ZERO_STRUCT(r);
370
371         /* Initialise parse structures */
372
373         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
374         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
375
376         /* Marshall data and send request */
377
378         init_samr_q_query_groupinfo(&q, group_pol, info_level);
379
380         if (!samr_io_q_query_groupinfo("", &q, &qbuf, 0) ||
381             !rpc_api_pipe_req(cli, SAMR_QUERY_GROUPINFO, &qbuf, &rbuf))
382                 goto done;
383
384         /* Unmarshall response */
385
386         if (!samr_io_r_query_groupinfo("", &r, &rbuf, 0))
387                 goto done;
388
389         *ctr = r.ctr;
390
391         /* Return output parameters */
392
393         result = r.status;
394
395  done:
396         prs_mem_free(&qbuf);
397         prs_mem_free(&rbuf);
398
399         return result;
400 }
401
402 /* Query user groups */
403
404 NTSTATUS cli_samr_query_usergroups(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
405                                    POLICY_HND *user_pol, uint32 *num_groups, 
406                                    DOM_GID **gid)
407 {
408         prs_struct qbuf, rbuf;
409         SAMR_Q_QUERY_USERGROUPS q;
410         SAMR_R_QUERY_USERGROUPS r;
411         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
412
413         ZERO_STRUCT(q);
414         ZERO_STRUCT(r);
415
416         /* Initialise parse structures */
417
418         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
419         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
420
421         /* Marshall data and send request */
422
423         init_samr_q_query_usergroups(&q, user_pol);
424
425         if (!samr_io_q_query_usergroups("", &q, &qbuf, 0) ||
426             !rpc_api_pipe_req(cli, SAMR_QUERY_USERGROUPS, &qbuf, &rbuf))
427                 goto done;
428
429         /* Unmarshall response */
430
431         if (!samr_io_r_query_usergroups("", &r, &rbuf, 0))
432                 goto done;
433
434         /* Return output parameters */
435
436         if (NT_STATUS_IS_OK(result = r.status)) {
437                 *num_groups = r.num_entries;
438                 *gid = r.gid;
439         }
440
441  done:
442         prs_mem_free(&qbuf);
443         prs_mem_free(&rbuf);
444
445         return result;
446 }
447
448 /* Query user aliases */
449
450 NTSTATUS cli_samr_query_useraliases(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
451                                    POLICY_HND *user_pol, uint32 num_sids, DOM_SID2 *sid,
452                                    uint32 *num_aliases, uint32 **als_rids)
453 {
454         prs_struct qbuf, rbuf;
455         SAMR_Q_QUERY_USERALIASES q;
456         SAMR_R_QUERY_USERALIASES r;
457         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
458         unsigned int ptr=1;
459         
460         ZERO_STRUCT(q);
461         ZERO_STRUCT(r);
462
463         /* Initialise parse structures */
464
465         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
466         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
467
468         /* Marshall data and send request */
469
470         init_samr_q_query_useraliases(&q, user_pol, num_sids, &ptr, sid);
471
472         if (!samr_io_q_query_useraliases("", &q, &qbuf, 0) ||
473             !rpc_api_pipe_req(cli, SAMR_QUERY_USERALIASES, &qbuf, &rbuf))
474                 goto done;
475
476         /* Unmarshall response */
477
478         if (!samr_io_r_query_useraliases("", &r, &rbuf, 0))
479                 goto done;
480
481         /* Return output parameters */
482
483         if (NT_STATUS_IS_OK(result = r.status)) {
484                 *num_aliases = r.num_entries;
485                 *als_rids = r.rid;
486         }
487
488  done:
489         prs_mem_free(&qbuf);
490         prs_mem_free(&rbuf);
491
492         return result;
493 }
494
495 /* Query user groups */
496
497 NTSTATUS cli_samr_query_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
498                                  POLICY_HND *group_pol, uint32 *num_mem, 
499                                  uint32 **rid, uint32 **attr)
500 {
501         prs_struct qbuf, rbuf;
502         SAMR_Q_QUERY_GROUPMEM q;
503         SAMR_R_QUERY_GROUPMEM r;
504         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
505
506         ZERO_STRUCT(q);
507         ZERO_STRUCT(r);
508
509         /* Initialise parse structures */
510
511         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
512         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
513
514         /* Marshall data and send request */
515
516         init_samr_q_query_groupmem(&q, group_pol);
517
518         if (!samr_io_q_query_groupmem("", &q, &qbuf, 0) ||
519             !rpc_api_pipe_req(cli, SAMR_QUERY_GROUPMEM, &qbuf, &rbuf))
520                 goto done;
521
522         /* Unmarshall response */
523
524         if (!samr_io_r_query_groupmem("", &r, &rbuf, 0))
525                 goto done;
526
527         /* Return output parameters */
528
529         if (NT_STATUS_IS_OK(result = r.status)) {
530                 *num_mem = r.num_entries;
531                 *rid = r.rid;
532                 *attr = r.attr;
533         }
534
535  done:
536         prs_mem_free(&qbuf);
537         prs_mem_free(&rbuf);
538
539         return result;
540 }
541
542 /**
543  * Enumerate domain users
544  *
545  * @param cli client state structure
546  * @param mem_ctx talloc context
547  * @param pol opened domain policy handle
548  * @param start_idx starting index of enumeration, returns context for
549                     next enumeration
550  * @param acb_mask account control bit mask (to enumerate some particular
551  *                 kind of accounts)
552  * @param size max acceptable size of response
553  * @param dom_users returned array of domain user names
554  * @param rids returned array of domain user RIDs
555  * @param num_dom_users numer returned entries
556  * 
557  * @return NTSTATUS returned in rpc response
558  **/
559 NTSTATUS cli_samr_enum_dom_users(struct cli_state *cli, TALLOC_CTX *mem_ctx,
560                                  POLICY_HND *pol, uint32 *start_idx, uint16 acb_mask,
561                                  uint32 size, char ***dom_users, uint32 **rids,
562                                  uint32 *num_dom_users)
563 {
564         prs_struct qbuf;
565         prs_struct rbuf;
566         SAMR_Q_ENUM_DOM_USERS q;
567         SAMR_R_ENUM_DOM_USERS r;
568         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
569         int i;
570         
571         ZERO_STRUCT(q);
572         ZERO_STRUCT(r);
573         
574         /* Initialise parse structures */
575
576         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
577         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
578         
579         /* Fill query structure with parameters */
580
581         init_samr_q_enum_dom_users(&q, pol, *start_idx, acb_mask, 0, size);
582         
583         if (!samr_io_q_enum_dom_users("", &q, &qbuf, 0) ||
584             !rpc_api_pipe_req(cli, SAMR_ENUM_DOM_USERS, &qbuf, &rbuf)) {
585                 goto done;
586         }
587
588         /* unpack received stream */
589
590         if(!samr_io_r_enum_dom_users("", &r, &rbuf, 0))
591                 goto done;
592         
593         /* return the data obtained in response */
594         if (!NT_STATUS_IS_OK(r.status) &&
595                 (NT_STATUS_EQUAL(r.status, STATUS_MORE_ENTRIES) ||
596                 NT_STATUS_EQUAL(r.status, NT_STATUS_NO_MORE_ENTRIES))) {
597                 return r.status;
598         }
599         
600         *start_idx = r.next_idx;
601         *num_dom_users = r.num_entries2;
602         result = r.status;
603
604         if (r.num_entries2) {
605                 /* allocate memory needed to return received data */    
606                 *rids = (uint32*)talloc(mem_ctx, sizeof(uint32) * r.num_entries2);
607                 if (!*rids) {
608                         DEBUG(0, ("Error in cli_samr_enum_dom_users(): out of memory\n"));
609                         return NT_STATUS_NO_MEMORY;
610                 }
611                 
612                 *dom_users = (char**)talloc(mem_ctx, sizeof(char*) * r.num_entries2);
613                 if (!*dom_users) {
614                         DEBUG(0, ("Error in cli_samr_enum_dom_users(): out of memory\n"));
615                         return NT_STATUS_NO_MEMORY;
616                 }
617                 
618                 /* fill output buffers with rpc response */
619                 for (i = 0; i < r.num_entries2; i++) {
620                         fstring conv_buf;
621                         
622                         (*rids)[i] = r.sam[i].rid;
623                         unistr2_to_ascii(conv_buf, &(r.uni_acct_name[i]), sizeof(conv_buf) - 1);
624                         (*dom_users)[i] = talloc_strdup(mem_ctx, conv_buf);
625                 }
626         }
627         
628 done:
629         prs_mem_free(&qbuf);
630         prs_mem_free(&rbuf);
631         
632         return result;
633 };
634
635 /* Enumerate domain groups */
636
637 NTSTATUS cli_samr_enum_dom_groups(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
638                                   POLICY_HND *pol, uint32 *start_idx, 
639                                   uint32 size, struct acct_info **dom_groups,
640                                   uint32 *num_dom_groups)
641 {
642         prs_struct qbuf, rbuf;
643         SAMR_Q_ENUM_DOM_GROUPS q;
644         SAMR_R_ENUM_DOM_GROUPS r;
645         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
646         uint32 name_idx, i;
647
648         ZERO_STRUCT(q);
649         ZERO_STRUCT(r);
650
651         /* Initialise parse structures */
652
653         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
654         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
655
656         /* Marshall data and send request */
657
658         init_samr_q_enum_dom_groups(&q, pol, *start_idx, size);
659
660         if (!samr_io_q_enum_dom_groups("", &q, &qbuf, 0) ||
661             !rpc_api_pipe_req(cli, SAMR_ENUM_DOM_GROUPS, &qbuf, &rbuf))
662                 goto done;
663
664         /* Unmarshall response */
665
666         if (!samr_io_r_enum_dom_groups("", &r, &rbuf, 0))
667                 goto done;
668
669         /* Return output parameters */
670
671         result = r.status;
672
673         if (!NT_STATUS_IS_OK(result) &&
674             NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES))
675                 goto done;
676
677         *num_dom_groups = r.num_entries2;
678
679         if (!((*dom_groups) = (struct acct_info *)
680               talloc(mem_ctx, sizeof(struct acct_info) * *num_dom_groups))) {
681                 result = NT_STATUS_UNSUCCESSFUL;
682                 goto done;
683         }
684
685         memset(*dom_groups, 0, sizeof(struct acct_info) * *num_dom_groups);
686
687         name_idx = 0;
688
689         for (i = 0; i < *num_dom_groups; i++) {
690
691                 (*dom_groups)[i].rid = r.sam[i].rid;
692
693                 if (r.sam[i].hdr_name.buffer) {
694                         unistr2_to_ascii((*dom_groups)[i].acct_name,
695                                          &r.uni_grp_name[name_idx],
696                                          sizeof(fstring) - 1);
697                         name_idx++;
698                 }
699
700                 *start_idx = r.next_idx;
701         }
702
703  done:
704         prs_mem_free(&qbuf);
705         prs_mem_free(&rbuf);
706
707         return result;
708 }
709
710 /* Enumerate domain groups */
711
712 NTSTATUS cli_samr_enum_als_groups(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
713                                   POLICY_HND *pol, uint32 *start_idx, 
714                                   uint32 size, struct acct_info **dom_groups,
715                                   uint32 *num_dom_groups)
716 {
717         prs_struct qbuf, rbuf;
718         SAMR_Q_ENUM_DOM_ALIASES q;
719         SAMR_R_ENUM_DOM_ALIASES r;
720         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
721         uint32 name_idx, i;
722
723         ZERO_STRUCT(q);
724         ZERO_STRUCT(r);
725
726         /* Initialise parse structures */
727
728         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
729         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
730
731         /* Marshall data and send request */
732
733         init_samr_q_enum_dom_aliases(&q, pol, *start_idx, size);
734
735         if (!samr_io_q_enum_dom_aliases("", &q, &qbuf, 0) ||
736             !rpc_api_pipe_req(cli, SAMR_ENUM_DOM_ALIASES, &qbuf, &rbuf)) {
737                 goto done;
738         }
739
740         /* Unmarshall response */
741
742         if (!samr_io_r_enum_dom_aliases("", &r, &rbuf, 0)) {
743                 goto done;
744         }
745
746         /* Return output parameters */
747
748         result = r.status;
749
750         if (!NT_STATUS_IS_OK(result) &&
751             NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES)) {
752                 goto done;
753         }
754
755         *num_dom_groups = r.num_entries2;
756
757         if (!((*dom_groups) = (struct acct_info *)
758               talloc(mem_ctx, sizeof(struct acct_info) * *num_dom_groups))) {
759                 result = NT_STATUS_UNSUCCESSFUL;
760                 goto done;
761         }
762
763         memset(*dom_groups, 0, sizeof(struct acct_info) * *num_dom_groups);
764
765         name_idx = 0;
766
767         for (i = 0; i < *num_dom_groups; i++) {
768
769                 (*dom_groups)[i].rid = r.sam[i].rid;
770
771                 if (r.sam[i].hdr_name.buffer) {
772                         unistr2_to_ascii((*dom_groups)[i].acct_name,
773                                          &r.uni_grp_name[name_idx],
774                                          sizeof(fstring) - 1);
775                         name_idx++;
776                 }
777
778                 *start_idx = r.next_idx;
779         }
780
781  done:
782         prs_mem_free(&qbuf);
783         prs_mem_free(&rbuf);
784
785         return result;
786 }
787
788 /* Query alias members */
789
790 NTSTATUS cli_samr_query_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
791                                  POLICY_HND *alias_pol, uint32 *num_mem, 
792                                  DOM_SID **sids)
793 {
794         prs_struct qbuf, rbuf;
795         SAMR_Q_QUERY_ALIASMEM q;
796         SAMR_R_QUERY_ALIASMEM r;
797         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
798         uint32 i;
799
800         ZERO_STRUCT(q);
801         ZERO_STRUCT(r);
802
803         /* Initialise parse structures */
804
805         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
806         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
807
808         /* Marshall data and send request */
809
810         init_samr_q_query_aliasmem(&q, alias_pol);
811
812         if (!samr_io_q_query_aliasmem("", &q, &qbuf, 0) ||
813             !rpc_api_pipe_req(cli, SAMR_QUERY_ALIASMEM, &qbuf, &rbuf)) {
814                 goto done;
815         }
816
817         /* Unmarshall response */
818
819         if (!samr_io_r_query_aliasmem("", &r, &rbuf, 0)) {
820                 goto done;
821         }
822
823         /* Return output parameters */
824
825         if (!NT_STATUS_IS_OK(result = r.status)) {
826                 goto done;
827         }
828
829         *num_mem = r.num_sids;
830
831         if (!(*sids = talloc(mem_ctx, sizeof(DOM_SID) * *num_mem))) {
832                 result = NT_STATUS_UNSUCCESSFUL;
833                 goto done;
834         }
835
836         for (i = 0; i < *num_mem; i++) {
837                 (*sids)[i] = r.sid[i].sid;
838         }
839
840  done:
841         prs_mem_free(&qbuf);
842         prs_mem_free(&rbuf);
843
844         return result;
845 }
846
847 /* Open handle on an alias */
848
849 NTSTATUS cli_samr_open_alias(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
850                              POLICY_HND *domain_pol, uint32 access_mask, 
851                              uint32 alias_rid, POLICY_HND *alias_pol)
852 {
853         prs_struct qbuf, rbuf;
854         SAMR_Q_OPEN_ALIAS q;
855         SAMR_R_OPEN_ALIAS r;
856         NTSTATUS result;
857
858         ZERO_STRUCT(q);
859         ZERO_STRUCT(r);
860
861         /* Initialise parse structures */
862
863         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
864         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
865
866         /* Marshall data and send request */
867
868         init_samr_q_open_alias(&q, domain_pol, access_mask, alias_rid);
869
870         if (!samr_io_q_open_alias("", &q, &qbuf, 0) ||
871             !rpc_api_pipe_req(cli, SAMR_OPEN_ALIAS, &qbuf, &rbuf)) {
872                 result = NT_STATUS_UNSUCCESSFUL;
873                 goto done;
874         }
875
876         /* Unmarshall response */
877
878         if (!samr_io_r_open_alias("", &r, &rbuf, 0)) {
879                 result = NT_STATUS_UNSUCCESSFUL;
880                 goto done;
881         }
882
883         /* Return output parameters */
884
885         if (NT_STATUS_IS_OK(result = r.status)) {
886                 *alias_pol = r.pol;
887 #ifdef __INSURE__
888                 alias_pol->marker = malloc(1);
889 #endif
890         }
891
892  done:
893         prs_mem_free(&qbuf);
894         prs_mem_free(&rbuf);
895
896         return result;
897 }
898
899 /* Query domain info */
900
901 NTSTATUS cli_samr_query_dom_info(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
902                                  POLICY_HND *domain_pol, uint16 switch_value,
903                                  SAM_UNK_CTR *ctr)
904 {
905         prs_struct qbuf, rbuf;
906         SAMR_Q_QUERY_DOMAIN_INFO q;
907         SAMR_R_QUERY_DOMAIN_INFO r;
908         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
909
910         ZERO_STRUCT(q);
911         ZERO_STRUCT(r);
912
913         /* Initialise parse structures */
914
915         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
916         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
917
918         /* Marshall data and send request */
919
920         init_samr_q_query_dom_info(&q, domain_pol, switch_value);
921
922         if (!samr_io_q_query_dom_info("", &q, &qbuf, 0) ||
923             !rpc_api_pipe_req(cli, SAMR_QUERY_DOMAIN_INFO, &qbuf, &rbuf)) {
924                 goto done;
925         }
926
927         /* Unmarshall response */
928
929         r.ctr = ctr;
930
931         if (!samr_io_r_query_dom_info("", &r, &rbuf, 0)) {
932                 goto done;
933         }
934
935         /* Return output parameters */
936
937         if (!NT_STATUS_IS_OK(result = r.status)) {
938                 goto done;
939         }
940
941  done:
942         prs_mem_free(&qbuf);
943         prs_mem_free(&rbuf);
944
945         return result;
946 }
947
948 /* This function returns the bizzare set of (max_entries, max_size) required
949    for the QueryDisplayInfo RPC to actually work against a domain controller
950    with large (10k and higher) numbers of users.  These values were 
951    obtained by inspection using ethereal and NT4 running User Manager. */
952
953 void get_query_dispinfo_params(int loop_count, uint32 *max_entries,
954                                uint32 *max_size)
955 {
956         switch(loop_count) {
957         case 0:
958                 *max_entries = 512;
959                 *max_size = 16383;
960                 break;
961         case 1:
962                 *max_entries = 1024;
963                 *max_size = 32766;
964                 break;
965         case 2:
966                 *max_entries = 2048;
967                 *max_size = 65532;
968                 break;
969         case 3:
970                 *max_entries = 4096;
971                 *max_size = 131064;
972                 break;
973         default:              /* loop_count >= 4 */
974                 *max_entries = 4096;
975                 *max_size = 131071;
976                 break;
977         }
978 }                    
979
980 /* Query display info */
981
982 NTSTATUS cli_samr_query_dispinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
983                                  POLICY_HND *domain_pol, uint32 *start_idx,
984                                  uint16 switch_value, uint32 *num_entries,
985                                  uint32 max_entries, uint32 max_size,
986                                  SAM_DISPINFO_CTR *ctr)
987 {
988         prs_struct qbuf, rbuf;
989         SAMR_Q_QUERY_DISPINFO q;
990         SAMR_R_QUERY_DISPINFO r;
991         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
992
993         ZERO_STRUCT(q);
994         ZERO_STRUCT(r);
995
996         /* Initialise parse structures */
997
998         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
999         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1000
1001         /* Marshall data and send request */
1002
1003         init_samr_q_query_dispinfo(&q, domain_pol, switch_value,
1004                                    *start_idx, max_entries, max_size);
1005
1006         if (!samr_io_q_query_dispinfo("", &q, &qbuf, 0) ||
1007             !rpc_api_pipe_req(cli, SAMR_QUERY_DISPINFO, &qbuf, &rbuf)) {
1008                 goto done;
1009         }
1010
1011         /* Unmarshall response */
1012
1013         r.ctr = ctr;
1014
1015         if (!samr_io_r_query_dispinfo("", &r, &rbuf, 0)) {
1016                 goto done;
1017         }
1018
1019         /* Return output parameters */
1020
1021         result = r.status;
1022
1023         if (!NT_STATUS_IS_OK(result) &&
1024             NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1025                 goto done;
1026         }
1027
1028         *num_entries = r.num_entries;
1029         *start_idx += r.num_entries;  /* No next_idx in this structure! */
1030
1031  done:
1032         prs_mem_free(&qbuf);
1033         prs_mem_free(&rbuf);
1034
1035         return result;
1036 }
1037
1038 /* Lookup rids.  Note that NT4 seems to crash if more than ~1000 rids are
1039    looked up in one packet. */
1040
1041 NTSTATUS cli_samr_lookup_rids(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1042                               POLICY_HND *domain_pol, uint32 flags,
1043                               uint32 num_rids, uint32 *rids, 
1044                               uint32 *num_names, char ***names,
1045                               uint32 **name_types)
1046 {
1047         prs_struct qbuf, rbuf;
1048         SAMR_Q_LOOKUP_RIDS q;
1049         SAMR_R_LOOKUP_RIDS r;
1050         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1051         uint32 i;
1052
1053         if (num_rids > 1000) {
1054                 DEBUG(2, ("cli_samr_lookup_rids: warning: NT4 can crash if "
1055                           "more than ~1000 rids are looked up at once.\n"));
1056         }
1057
1058         ZERO_STRUCT(q);
1059         ZERO_STRUCT(r);
1060
1061         /* Initialise parse structures */
1062
1063         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1064         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1065
1066         /* Marshall data and send request */
1067
1068         init_samr_q_lookup_rids(mem_ctx, &q, domain_pol, flags,
1069                                 num_rids, rids);
1070
1071         if (!samr_io_q_lookup_rids("", &q, &qbuf, 0) ||
1072             !rpc_api_pipe_req(cli, SAMR_LOOKUP_RIDS, &qbuf, &rbuf)) {
1073                 goto done;
1074         }
1075
1076         /* Unmarshall response */
1077
1078         if (!samr_io_r_lookup_rids("", &r, &rbuf, 0)) {
1079                 goto done;
1080         }
1081
1082         /* Return output parameters */
1083
1084         if (!NT_STATUS_IS_OK(result = r.status)) {
1085                 goto done;
1086         }
1087
1088         if (r.num_names1 == 0) {
1089                 *num_names = 0;
1090                 *names = NULL;
1091                 goto done;
1092         }
1093
1094         *num_names = r.num_names1;
1095         *names = talloc(mem_ctx, sizeof(char *) * r.num_names1);
1096         *name_types = talloc(mem_ctx, sizeof(uint32) * r.num_names1);
1097
1098         for (i = 0; i < r.num_names1; i++) {
1099                 fstring tmp;
1100
1101                 unistr2_to_ascii(tmp, &r.uni_name[i], sizeof(tmp) - 1);
1102                 (*names)[i] = talloc_strdup(mem_ctx, tmp);
1103                 (*name_types)[i] = r.type[i];
1104         }
1105
1106  done:
1107         prs_mem_free(&qbuf);
1108         prs_mem_free(&rbuf);
1109
1110         return result;
1111 }
1112
1113 /* Lookup names */
1114
1115 NTSTATUS cli_samr_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1116                                POLICY_HND *domain_pol, uint32 flags,
1117                                uint32 num_names, const char **names,
1118                                uint32 *num_rids, uint32 **rids,
1119                                uint32 **rid_types)
1120 {
1121         prs_struct qbuf, rbuf;
1122         SAMR_Q_LOOKUP_NAMES q;
1123         SAMR_R_LOOKUP_NAMES r;
1124         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1125         uint32 i;
1126
1127         ZERO_STRUCT(q);
1128         ZERO_STRUCT(r);
1129
1130         /* Initialise parse structures */
1131
1132         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1133         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1134
1135         /* Marshall data and send request */
1136
1137         init_samr_q_lookup_names(mem_ctx, &q, domain_pol, flags,
1138                                  num_names, names);
1139
1140         if (!samr_io_q_lookup_names("", &q, &qbuf, 0) ||
1141             !rpc_api_pipe_req(cli, SAMR_LOOKUP_NAMES, &qbuf, &rbuf)) {
1142                 goto done;
1143         }
1144
1145         /* Unmarshall response */
1146
1147         if (!samr_io_r_lookup_names("", &r, &rbuf, 0)) {
1148                 goto done;
1149         }
1150
1151         /* Return output parameters */
1152
1153         if (!NT_STATUS_IS_OK(result = r.status)) {
1154                 goto done;
1155         }
1156
1157         if (r.num_rids1 == 0) {
1158                 *num_rids = 0;
1159                 goto done;
1160         }
1161
1162         *num_rids = r.num_rids1;
1163         *rids = talloc(mem_ctx, sizeof(uint32) * r.num_rids1);
1164         *rid_types = talloc(mem_ctx, sizeof(uint32) * r.num_rids1);
1165
1166         for (i = 0; i < r.num_rids1; i++) {
1167                 (*rids)[i] = r.rids[i];
1168                 (*rid_types)[i] = r.types[i];
1169         }
1170
1171  done:
1172         prs_mem_free(&qbuf);
1173         prs_mem_free(&rbuf);
1174
1175         return result;
1176 }
1177
1178 /* Create a domain user */
1179
1180 NTSTATUS cli_samr_create_dom_user(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1181                                   POLICY_HND *domain_pol, const char *acct_name,
1182                                   uint32 acb_info, uint32 unknown, 
1183                                   POLICY_HND *user_pol, uint32 *rid)
1184 {
1185         prs_struct qbuf, rbuf;
1186         SAMR_Q_CREATE_USER q;
1187         SAMR_R_CREATE_USER r;
1188         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1189
1190         ZERO_STRUCT(q);
1191         ZERO_STRUCT(r);
1192
1193         /* Initialise parse structures */
1194
1195         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1196         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1197
1198         /* Marshall data and send request */
1199
1200         init_samr_q_create_user(&q, domain_pol, acct_name, acb_info, unknown);
1201
1202         if (!samr_io_q_create_user("", &q, &qbuf, 0) ||
1203             !rpc_api_pipe_req(cli, SAMR_CREATE_USER, &qbuf, &rbuf)) {
1204                 goto done;
1205         }
1206
1207         /* Unmarshall response */
1208
1209         if (!samr_io_r_create_user("", &r, &rbuf, 0)) {
1210                 goto done;
1211         }
1212
1213         /* Return output parameters */
1214
1215         if (!NT_STATUS_IS_OK(result = r.status)) {
1216                 goto done;
1217         }
1218
1219         if (user_pol)
1220                 *user_pol = r.user_pol;
1221
1222         if (rid)
1223                 *rid = r.user_rid;
1224
1225  done:
1226         prs_mem_free(&qbuf);
1227         prs_mem_free(&rbuf);
1228
1229         return result;
1230 }
1231
1232 /* Set userinfo */
1233
1234 NTSTATUS cli_samr_set_userinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1235                                POLICY_HND *user_pol, uint16 switch_value,
1236                                uchar sess_key[16], SAM_USERINFO_CTR *ctr)
1237 {
1238         prs_struct qbuf, rbuf;
1239         SAMR_Q_SET_USERINFO q;
1240         SAMR_R_SET_USERINFO r;
1241         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1242
1243         ZERO_STRUCT(q);
1244         ZERO_STRUCT(r);
1245
1246         /* Initialise parse structures */
1247
1248         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1249         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1250
1251         /* Marshall data and send request */
1252
1253         q.ctr = ctr;
1254
1255         init_samr_q_set_userinfo(&q, user_pol, sess_key, switch_value, 
1256                                  ctr->info.id);
1257
1258         if (!samr_io_q_set_userinfo("", &q, &qbuf, 0) ||
1259             !rpc_api_pipe_req(cli, SAMR_SET_USERINFO, &qbuf, &rbuf)) {
1260                 goto done;
1261         }
1262
1263         /* Unmarshall response */
1264
1265         if (!samr_io_r_set_userinfo("", &r, &rbuf, 0)) {
1266                 goto done;
1267         }
1268
1269         /* Return output parameters */
1270
1271         if (!NT_STATUS_IS_OK(result = r.status)) {
1272                 goto done;
1273         }
1274
1275  done:
1276         prs_mem_free(&qbuf);
1277         prs_mem_free(&rbuf);
1278
1279         return result;
1280 }
1281
1282 /* Set userinfo2 */
1283
1284 NTSTATUS cli_samr_set_userinfo2(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1285                                 POLICY_HND *user_pol, uint16 switch_value,
1286                                 uchar sess_key[16], SAM_USERINFO_CTR *ctr)
1287 {
1288         prs_struct qbuf, rbuf;
1289         SAMR_Q_SET_USERINFO2 q;
1290         SAMR_R_SET_USERINFO2 r;
1291         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1292
1293         ZERO_STRUCT(q);
1294         ZERO_STRUCT(r);
1295
1296         /* Initialise parse structures */
1297
1298         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1299         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1300
1301         /* Marshall data and send request */
1302
1303         init_samr_q_set_userinfo2(&q, user_pol, sess_key, switch_value, ctr);
1304
1305         if (!samr_io_q_set_userinfo2("", &q, &qbuf, 0) ||
1306             !rpc_api_pipe_req(cli, SAMR_SET_USERINFO2, &qbuf, &rbuf)) {
1307                 goto done;
1308         }
1309
1310         /* Unmarshall response */
1311
1312         if (!samr_io_r_set_userinfo2("", &r, &rbuf, 0)) {
1313                 goto done;
1314         }
1315
1316         /* Return output parameters */
1317
1318         if (!NT_STATUS_IS_OK(result = r.status)) {
1319                 goto done;
1320         }
1321
1322  done:
1323         prs_mem_free(&qbuf);
1324         prs_mem_free(&rbuf);
1325
1326         return result;
1327 }
1328
1329 /* Delete domain user */
1330
1331 NTSTATUS cli_samr_delete_dom_user(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1332                                   POLICY_HND *user_pol)
1333 {
1334         prs_struct qbuf, rbuf;
1335         SAMR_Q_DELETE_DOM_USER q;
1336         SAMR_R_DELETE_DOM_USER r;
1337         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1338
1339         ZERO_STRUCT(q);
1340         ZERO_STRUCT(r);
1341
1342         /* Initialise parse structures */
1343
1344         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1345         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1346
1347         /* Marshall data and send request */
1348
1349         init_samr_q_delete_dom_user(&q, user_pol);
1350
1351         if (!samr_io_q_delete_dom_user("", &q, &qbuf, 0) ||
1352             !rpc_api_pipe_req(cli, SAMR_DELETE_DOM_USER, &qbuf, &rbuf)) {
1353                 goto done;
1354         }
1355
1356         /* Unmarshall response */
1357
1358         if (!samr_io_r_delete_dom_user("", &r, &rbuf, 0)) {
1359                 goto done;
1360         }
1361
1362         /* Return output parameters */
1363
1364         result = r.status;
1365
1366  done:
1367         prs_mem_free(&qbuf);
1368         prs_mem_free(&rbuf);
1369
1370         return result;
1371 }
1372
1373 /* Query user security object */
1374
1375 NTSTATUS cli_samr_query_sec_obj(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1376                                  POLICY_HND *user_pol, uint16 switch_value, 
1377                                  TALLOC_CTX *ctx, SEC_DESC_BUF **sec_desc_buf)
1378 {
1379         prs_struct qbuf, rbuf;
1380         SAMR_Q_QUERY_SEC_OBJ q;
1381         SAMR_R_QUERY_SEC_OBJ r;
1382         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1383
1384         ZERO_STRUCT(q);
1385         ZERO_STRUCT(r);
1386
1387         /* Initialise parse structures */
1388
1389         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1390         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1391
1392         /* Marshall data and send request */
1393
1394         init_samr_q_query_sec_obj(&q, user_pol, switch_value);
1395
1396         if (!samr_io_q_query_sec_obj("", &q, &qbuf, 0) ||
1397             !rpc_api_pipe_req(cli, SAMR_QUERY_SEC_OBJECT, &qbuf, &rbuf)) {
1398                 goto done;
1399         }
1400
1401         /* Unmarshall response */
1402
1403         if (!samr_io_r_query_sec_obj("", &r, &rbuf, 0)) {
1404                 goto done;
1405         }
1406
1407         /* Return output parameters */
1408
1409         result = r.status;
1410         *sec_desc_buf=dup_sec_desc_buf(ctx, r.buf);
1411
1412  done:
1413         prs_mem_free(&qbuf);
1414         prs_mem_free(&rbuf);
1415
1416         return result;
1417 }
1418
1419 /* Get domain password info */
1420
1421 NTSTATUS cli_samr_get_dom_pwinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1422                                  uint16 *unk_0, uint16 *unk_1, uint16 *unk_2)
1423 {
1424         prs_struct qbuf, rbuf;
1425         SAMR_Q_GET_DOM_PWINFO q;
1426         SAMR_R_GET_DOM_PWINFO r;
1427         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1428
1429         ZERO_STRUCT(q);
1430         ZERO_STRUCT(r);
1431
1432         /* Initialise parse structures */
1433
1434         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1435         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1436
1437         /* Marshall data and send request */
1438
1439         init_samr_q_get_dom_pwinfo(&q, cli->desthost);
1440
1441         if (!samr_io_q_get_dom_pwinfo("", &q, &qbuf, 0) ||
1442             !rpc_api_pipe_req(cli, SAMR_GET_DOM_PWINFO, &qbuf, &rbuf))
1443                 goto done;
1444
1445         /* Unmarshall response */
1446
1447         if (!samr_io_r_get_dom_pwinfo("", &r, &rbuf, 0))
1448                 goto done;
1449
1450         /* Return output parameters */
1451
1452         result = r.status;
1453
1454         if (NT_STATUS_IS_OK(result)) {
1455                 if (unk_0)
1456                         *unk_0 = r.unk_0;
1457                 if (unk_1)
1458                         *unk_1 = r.unk_1;
1459                 if (unk_2)
1460                         *unk_2 = r.unk_2;
1461         }
1462
1463  done:
1464         prs_mem_free(&qbuf);
1465         prs_mem_free(&rbuf);
1466
1467         return result;
1468 }