This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[nivanova/samba-autobuild/.git] / source3 / 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         result = r.status;
594
595         if (!NT_STATUS_IS_OK(result) &&
596             NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES))
597                 goto done;
598         
599         *start_idx = r.next_idx;
600         *num_dom_users = r.num_entries2;
601
602         if (r.num_entries2) {
603                 /* allocate memory needed to return received data */    
604                 *rids = (uint32*)talloc(mem_ctx, sizeof(uint32) * r.num_entries2);
605                 if (!*rids) {
606                         DEBUG(0, ("Error in cli_samr_enum_dom_users(): out of memory\n"));
607                         return NT_STATUS_NO_MEMORY;
608                 }
609                 
610                 *dom_users = (char**)talloc(mem_ctx, sizeof(char*) * r.num_entries2);
611                 if (!*dom_users) {
612                         DEBUG(0, ("Error in cli_samr_enum_dom_users(): out of memory\n"));
613                         return NT_STATUS_NO_MEMORY;
614                 }
615                 
616                 /* fill output buffers with rpc response */
617                 for (i = 0; i < r.num_entries2; i++) {
618                         fstring conv_buf;
619                         
620                         (*rids)[i] = r.sam[i].rid;
621                         unistr2_to_ascii(conv_buf, &(r.uni_acct_name[i]), sizeof(conv_buf) - 1);
622                         (*dom_users)[i] = talloc_strdup(mem_ctx, conv_buf);
623                 }
624         }
625         
626 done:
627         prs_mem_free(&qbuf);
628         prs_mem_free(&rbuf);
629         
630         return result;
631 };
632
633 /* Enumerate domain groups */
634
635 NTSTATUS cli_samr_enum_dom_groups(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
636                                   POLICY_HND *pol, uint32 *start_idx, 
637                                   uint32 size, struct acct_info **dom_groups,
638                                   uint32 *num_dom_groups)
639 {
640         prs_struct qbuf, rbuf;
641         SAMR_Q_ENUM_DOM_GROUPS q;
642         SAMR_R_ENUM_DOM_GROUPS r;
643         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
644         uint32 name_idx, i;
645
646         ZERO_STRUCT(q);
647         ZERO_STRUCT(r);
648
649         /* Initialise parse structures */
650
651         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
652         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
653
654         /* Marshall data and send request */
655
656         init_samr_q_enum_dom_groups(&q, pol, *start_idx, size);
657
658         if (!samr_io_q_enum_dom_groups("", &q, &qbuf, 0) ||
659             !rpc_api_pipe_req(cli, SAMR_ENUM_DOM_GROUPS, &qbuf, &rbuf))
660                 goto done;
661
662         /* Unmarshall response */
663
664         if (!samr_io_r_enum_dom_groups("", &r, &rbuf, 0))
665                 goto done;
666
667         /* Return output parameters */
668
669         result = r.status;
670
671         if (!NT_STATUS_IS_OK(result) &&
672             NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES))
673                 goto done;
674
675         *num_dom_groups = r.num_entries2;
676
677         if (!((*dom_groups) = (struct acct_info *)
678               talloc(mem_ctx, sizeof(struct acct_info) * *num_dom_groups))) {
679                 result = NT_STATUS_UNSUCCESSFUL;
680                 goto done;
681         }
682
683         memset(*dom_groups, 0, sizeof(struct acct_info) * *num_dom_groups);
684
685         name_idx = 0;
686
687         for (i = 0; i < *num_dom_groups; i++) {
688
689                 (*dom_groups)[i].rid = r.sam[i].rid;
690
691                 if (r.sam[i].hdr_name.buffer) {
692                         unistr2_to_ascii((*dom_groups)[i].acct_name,
693                                          &r.uni_grp_name[name_idx],
694                                          sizeof(fstring) - 1);
695                         name_idx++;
696                 }
697
698                 *start_idx = r.next_idx;
699         }
700
701  done:
702         prs_mem_free(&qbuf);
703         prs_mem_free(&rbuf);
704
705         return result;
706 }
707
708 /* Enumerate domain groups */
709
710 NTSTATUS cli_samr_enum_als_groups(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
711                                   POLICY_HND *pol, uint32 *start_idx, 
712                                   uint32 size, struct acct_info **dom_groups,
713                                   uint32 *num_dom_groups)
714 {
715         prs_struct qbuf, rbuf;
716         SAMR_Q_ENUM_DOM_ALIASES q;
717         SAMR_R_ENUM_DOM_ALIASES r;
718         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
719         uint32 name_idx, i;
720
721         ZERO_STRUCT(q);
722         ZERO_STRUCT(r);
723
724         /* Initialise parse structures */
725
726         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
727         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
728
729         /* Marshall data and send request */
730
731         init_samr_q_enum_dom_aliases(&q, pol, *start_idx, size);
732
733         if (!samr_io_q_enum_dom_aliases("", &q, &qbuf, 0) ||
734             !rpc_api_pipe_req(cli, SAMR_ENUM_DOM_ALIASES, &qbuf, &rbuf)) {
735                 goto done;
736         }
737
738         /* Unmarshall response */
739
740         if (!samr_io_r_enum_dom_aliases("", &r, &rbuf, 0)) {
741                 goto done;
742         }
743
744         /* Return output parameters */
745
746         result = r.status;
747
748         if (!NT_STATUS_IS_OK(result) &&
749             NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES)) {
750                 goto done;
751         }
752
753         *num_dom_groups = r.num_entries2;
754
755         if (!((*dom_groups) = (struct acct_info *)
756               talloc(mem_ctx, sizeof(struct acct_info) * *num_dom_groups))) {
757                 result = NT_STATUS_UNSUCCESSFUL;
758                 goto done;
759         }
760
761         memset(*dom_groups, 0, sizeof(struct acct_info) * *num_dom_groups);
762
763         name_idx = 0;
764
765         for (i = 0; i < *num_dom_groups; i++) {
766
767                 (*dom_groups)[i].rid = r.sam[i].rid;
768
769                 if (r.sam[i].hdr_name.buffer) {
770                         unistr2_to_ascii((*dom_groups)[i].acct_name,
771                                          &r.uni_grp_name[name_idx],
772                                          sizeof(fstring) - 1);
773                         name_idx++;
774                 }
775
776                 *start_idx = r.next_idx;
777         }
778
779  done:
780         prs_mem_free(&qbuf);
781         prs_mem_free(&rbuf);
782
783         return result;
784 }
785
786 /* Query alias members */
787
788 NTSTATUS cli_samr_query_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
789                                  POLICY_HND *alias_pol, uint32 *num_mem, 
790                                  DOM_SID **sids)
791 {
792         prs_struct qbuf, rbuf;
793         SAMR_Q_QUERY_ALIASMEM q;
794         SAMR_R_QUERY_ALIASMEM r;
795         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
796         uint32 i;
797
798         ZERO_STRUCT(q);
799         ZERO_STRUCT(r);
800
801         /* Initialise parse structures */
802
803         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
804         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
805
806         /* Marshall data and send request */
807
808         init_samr_q_query_aliasmem(&q, alias_pol);
809
810         if (!samr_io_q_query_aliasmem("", &q, &qbuf, 0) ||
811             !rpc_api_pipe_req(cli, SAMR_QUERY_ALIASMEM, &qbuf, &rbuf)) {
812                 goto done;
813         }
814
815         /* Unmarshall response */
816
817         if (!samr_io_r_query_aliasmem("", &r, &rbuf, 0)) {
818                 goto done;
819         }
820
821         /* Return output parameters */
822
823         if (!NT_STATUS_IS_OK(result = r.status)) {
824                 goto done;
825         }
826
827         *num_mem = r.num_sids;
828
829         if (!(*sids = talloc(mem_ctx, sizeof(DOM_SID) * *num_mem))) {
830                 result = NT_STATUS_UNSUCCESSFUL;
831                 goto done;
832         }
833
834         for (i = 0; i < *num_mem; i++) {
835                 (*sids)[i] = r.sid[i].sid;
836         }
837
838  done:
839         prs_mem_free(&qbuf);
840         prs_mem_free(&rbuf);
841
842         return result;
843 }
844
845 /* Open handle on an alias */
846
847 NTSTATUS cli_samr_open_alias(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
848                              POLICY_HND *domain_pol, uint32 access_mask, 
849                              uint32 alias_rid, POLICY_HND *alias_pol)
850 {
851         prs_struct qbuf, rbuf;
852         SAMR_Q_OPEN_ALIAS q;
853         SAMR_R_OPEN_ALIAS r;
854         NTSTATUS result;
855
856         ZERO_STRUCT(q);
857         ZERO_STRUCT(r);
858
859         /* Initialise parse structures */
860
861         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
862         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
863
864         /* Marshall data and send request */
865
866         init_samr_q_open_alias(&q, domain_pol, access_mask, alias_rid);
867
868         if (!samr_io_q_open_alias("", &q, &qbuf, 0) ||
869             !rpc_api_pipe_req(cli, SAMR_OPEN_ALIAS, &qbuf, &rbuf)) {
870                 result = NT_STATUS_UNSUCCESSFUL;
871                 goto done;
872         }
873
874         /* Unmarshall response */
875
876         if (!samr_io_r_open_alias("", &r, &rbuf, 0)) {
877                 result = NT_STATUS_UNSUCCESSFUL;
878                 goto done;
879         }
880
881         /* Return output parameters */
882
883         if (NT_STATUS_IS_OK(result = r.status)) {
884                 *alias_pol = r.pol;
885 #ifdef __INSURE__
886                 alias_pol->marker = malloc(1);
887 #endif
888         }
889
890  done:
891         prs_mem_free(&qbuf);
892         prs_mem_free(&rbuf);
893
894         return result;
895 }
896
897 /* Query domain info */
898
899 NTSTATUS cli_samr_query_dom_info(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
900                                  POLICY_HND *domain_pol, uint16 switch_value,
901                                  SAM_UNK_CTR *ctr)
902 {
903         prs_struct qbuf, rbuf;
904         SAMR_Q_QUERY_DOMAIN_INFO q;
905         SAMR_R_QUERY_DOMAIN_INFO r;
906         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
907
908         ZERO_STRUCT(q);
909         ZERO_STRUCT(r);
910
911         /* Initialise parse structures */
912
913         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
914         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
915
916         /* Marshall data and send request */
917
918         init_samr_q_query_dom_info(&q, domain_pol, switch_value);
919
920         if (!samr_io_q_query_dom_info("", &q, &qbuf, 0) ||
921             !rpc_api_pipe_req(cli, SAMR_QUERY_DOMAIN_INFO, &qbuf, &rbuf)) {
922                 goto done;
923         }
924
925         /* Unmarshall response */
926
927         r.ctr = ctr;
928
929         if (!samr_io_r_query_dom_info("", &r, &rbuf, 0)) {
930                 goto done;
931         }
932
933         /* Return output parameters */
934
935         if (!NT_STATUS_IS_OK(result = r.status)) {
936                 goto done;
937         }
938
939  done:
940         prs_mem_free(&qbuf);
941         prs_mem_free(&rbuf);
942
943         return result;
944 }
945
946 /* This function returns the bizzare set of (max_entries, max_size) required
947    for the QueryDisplayInfo RPC to actually work against a domain controller
948    with large (10k and higher) numbers of users.  These values were 
949    obtained by inspection using ethereal and NT4 running User Manager. */
950
951 void get_query_dispinfo_params(int loop_count, uint32 *max_entries,
952                                uint32 *max_size)
953 {
954         switch(loop_count) {
955         case 0:
956                 *max_entries = 512;
957                 *max_size = 16383;
958                 break;
959         case 1:
960                 *max_entries = 1024;
961                 *max_size = 32766;
962                 break;
963         case 2:
964                 *max_entries = 2048;
965                 *max_size = 65532;
966                 break;
967         case 3:
968                 *max_entries = 4096;
969                 *max_size = 131064;
970                 break;
971         default:              /* loop_count >= 4 */
972                 *max_entries = 4096;
973                 *max_size = 131071;
974                 break;
975         }
976 }                    
977
978 /* Query display info */
979
980 NTSTATUS cli_samr_query_dispinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
981                                  POLICY_HND *domain_pol, uint32 *start_idx,
982                                  uint16 switch_value, uint32 *num_entries,
983                                  uint32 max_entries, uint32 max_size,
984                                  SAM_DISPINFO_CTR *ctr)
985 {
986         prs_struct qbuf, rbuf;
987         SAMR_Q_QUERY_DISPINFO q;
988         SAMR_R_QUERY_DISPINFO r;
989         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
990
991         ZERO_STRUCT(q);
992         ZERO_STRUCT(r);
993
994         /* Initialise parse structures */
995
996         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
997         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
998
999         /* Marshall data and send request */
1000
1001         init_samr_q_query_dispinfo(&q, domain_pol, switch_value,
1002                                    *start_idx, max_entries, max_size);
1003
1004         if (!samr_io_q_query_dispinfo("", &q, &qbuf, 0) ||
1005             !rpc_api_pipe_req(cli, SAMR_QUERY_DISPINFO, &qbuf, &rbuf)) {
1006                 goto done;
1007         }
1008
1009         /* Unmarshall response */
1010
1011         r.ctr = ctr;
1012
1013         if (!samr_io_r_query_dispinfo("", &r, &rbuf, 0)) {
1014                 goto done;
1015         }
1016
1017         /* Return output parameters */
1018
1019         result = r.status;
1020
1021         if (!NT_STATUS_IS_OK(result) &&
1022             NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1023                 goto done;
1024         }
1025
1026         *num_entries = r.num_entries;
1027         *start_idx += r.num_entries;  /* No next_idx in this structure! */
1028
1029  done:
1030         prs_mem_free(&qbuf);
1031         prs_mem_free(&rbuf);
1032
1033         return result;
1034 }
1035
1036 /* Lookup rids.  Note that NT4 seems to crash if more than ~1000 rids are
1037    looked up in one packet. */
1038
1039 NTSTATUS cli_samr_lookup_rids(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1040                               POLICY_HND *domain_pol, uint32 flags,
1041                               uint32 num_rids, uint32 *rids, 
1042                               uint32 *num_names, char ***names,
1043                               uint32 **name_types)
1044 {
1045         prs_struct qbuf, rbuf;
1046         SAMR_Q_LOOKUP_RIDS q;
1047         SAMR_R_LOOKUP_RIDS r;
1048         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1049         uint32 i;
1050
1051         if (num_rids > 1000) {
1052                 DEBUG(2, ("cli_samr_lookup_rids: warning: NT4 can crash if "
1053                           "more than ~1000 rids are looked up at once.\n"));
1054         }
1055
1056         ZERO_STRUCT(q);
1057         ZERO_STRUCT(r);
1058
1059         /* Initialise parse structures */
1060
1061         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1062         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1063
1064         /* Marshall data and send request */
1065
1066         init_samr_q_lookup_rids(mem_ctx, &q, domain_pol, flags,
1067                                 num_rids, rids);
1068
1069         if (!samr_io_q_lookup_rids("", &q, &qbuf, 0) ||
1070             !rpc_api_pipe_req(cli, SAMR_LOOKUP_RIDS, &qbuf, &rbuf)) {
1071                 goto done;
1072         }
1073
1074         /* Unmarshall response */
1075
1076         if (!samr_io_r_lookup_rids("", &r, &rbuf, 0)) {
1077                 goto done;
1078         }
1079
1080         /* Return output parameters */
1081
1082         if (!NT_STATUS_IS_OK(result = r.status)) {
1083                 goto done;
1084         }
1085
1086         if (r.num_names1 == 0) {
1087                 *num_names = 0;
1088                 *names = NULL;
1089                 goto done;
1090         }
1091
1092         *num_names = r.num_names1;
1093         *names = talloc(mem_ctx, sizeof(char *) * r.num_names1);
1094         *name_types = talloc(mem_ctx, sizeof(uint32) * r.num_names1);
1095
1096         for (i = 0; i < r.num_names1; i++) {
1097                 fstring tmp;
1098
1099                 unistr2_to_ascii(tmp, &r.uni_name[i], sizeof(tmp) - 1);
1100                 (*names)[i] = talloc_strdup(mem_ctx, tmp);
1101                 (*name_types)[i] = r.type[i];
1102         }
1103
1104  done:
1105         prs_mem_free(&qbuf);
1106         prs_mem_free(&rbuf);
1107
1108         return result;
1109 }
1110
1111 /* Lookup names */
1112
1113 NTSTATUS cli_samr_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1114                                POLICY_HND *domain_pol, uint32 flags,
1115                                uint32 num_names, const char **names,
1116                                uint32 *num_rids, uint32 **rids,
1117                                uint32 **rid_types)
1118 {
1119         prs_struct qbuf, rbuf;
1120         SAMR_Q_LOOKUP_NAMES q;
1121         SAMR_R_LOOKUP_NAMES r;
1122         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1123         uint32 i;
1124
1125         ZERO_STRUCT(q);
1126         ZERO_STRUCT(r);
1127
1128         /* Initialise parse structures */
1129
1130         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1131         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1132
1133         /* Marshall data and send request */
1134
1135         init_samr_q_lookup_names(mem_ctx, &q, domain_pol, flags,
1136                                  num_names, names);
1137
1138         if (!samr_io_q_lookup_names("", &q, &qbuf, 0) ||
1139             !rpc_api_pipe_req(cli, SAMR_LOOKUP_NAMES, &qbuf, &rbuf)) {
1140                 goto done;
1141         }
1142
1143         /* Unmarshall response */
1144
1145         if (!samr_io_r_lookup_names("", &r, &rbuf, 0)) {
1146                 goto done;
1147         }
1148
1149         /* Return output parameters */
1150
1151         if (!NT_STATUS_IS_OK(result = r.status)) {
1152                 goto done;
1153         }
1154
1155         if (r.num_rids1 == 0) {
1156                 *num_rids = 0;
1157                 goto done;
1158         }
1159
1160         *num_rids = r.num_rids1;
1161         *rids = talloc(mem_ctx, sizeof(uint32) * r.num_rids1);
1162         *rid_types = talloc(mem_ctx, sizeof(uint32) * r.num_rids1);
1163
1164         for (i = 0; i < r.num_rids1; i++) {
1165                 (*rids)[i] = r.rids[i];
1166                 (*rid_types)[i] = r.types[i];
1167         }
1168
1169  done:
1170         prs_mem_free(&qbuf);
1171         prs_mem_free(&rbuf);
1172
1173         return result;
1174 }
1175
1176 /* Create a domain user */
1177
1178 NTSTATUS cli_samr_create_dom_user(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1179                                   POLICY_HND *domain_pol, const char *acct_name,
1180                                   uint32 acb_info, uint32 unknown, 
1181                                   POLICY_HND *user_pol, uint32 *rid)
1182 {
1183         prs_struct qbuf, rbuf;
1184         SAMR_Q_CREATE_USER q;
1185         SAMR_R_CREATE_USER r;
1186         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1187
1188         ZERO_STRUCT(q);
1189         ZERO_STRUCT(r);
1190
1191         /* Initialise parse structures */
1192
1193         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1194         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1195
1196         /* Marshall data and send request */
1197
1198         init_samr_q_create_user(&q, domain_pol, acct_name, acb_info, unknown);
1199
1200         if (!samr_io_q_create_user("", &q, &qbuf, 0) ||
1201             !rpc_api_pipe_req(cli, SAMR_CREATE_USER, &qbuf, &rbuf)) {
1202                 goto done;
1203         }
1204
1205         /* Unmarshall response */
1206
1207         if (!samr_io_r_create_user("", &r, &rbuf, 0)) {
1208                 goto done;
1209         }
1210
1211         /* Return output parameters */
1212
1213         if (!NT_STATUS_IS_OK(result = r.status)) {
1214                 goto done;
1215         }
1216
1217         if (user_pol)
1218                 *user_pol = r.user_pol;
1219
1220         if (rid)
1221                 *rid = r.user_rid;
1222
1223  done:
1224         prs_mem_free(&qbuf);
1225         prs_mem_free(&rbuf);
1226
1227         return result;
1228 }
1229
1230 /* Set userinfo */
1231
1232 NTSTATUS cli_samr_set_userinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1233                                POLICY_HND *user_pol, uint16 switch_value,
1234                                uchar sess_key[16], SAM_USERINFO_CTR *ctr)
1235 {
1236         prs_struct qbuf, rbuf;
1237         SAMR_Q_SET_USERINFO q;
1238         SAMR_R_SET_USERINFO r;
1239         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1240
1241         ZERO_STRUCT(q);
1242         ZERO_STRUCT(r);
1243
1244         /* Initialise parse structures */
1245
1246         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1247         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1248
1249         /* Marshall data and send request */
1250
1251         q.ctr = ctr;
1252
1253         init_samr_q_set_userinfo(&q, user_pol, sess_key, switch_value, 
1254                                  ctr->info.id);
1255
1256         if (!samr_io_q_set_userinfo("", &q, &qbuf, 0) ||
1257             !rpc_api_pipe_req(cli, SAMR_SET_USERINFO, &qbuf, &rbuf)) {
1258                 goto done;
1259         }
1260
1261         /* Unmarshall response */
1262
1263         if (!samr_io_r_set_userinfo("", &r, &rbuf, 0)) {
1264                 goto done;
1265         }
1266
1267         /* Return output parameters */
1268
1269         if (!NT_STATUS_IS_OK(result = r.status)) {
1270                 goto done;
1271         }
1272
1273  done:
1274         prs_mem_free(&qbuf);
1275         prs_mem_free(&rbuf);
1276
1277         return result;
1278 }
1279
1280 /* Set userinfo2 */
1281
1282 NTSTATUS cli_samr_set_userinfo2(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1283                                 POLICY_HND *user_pol, uint16 switch_value,
1284                                 uchar sess_key[16], SAM_USERINFO_CTR *ctr)
1285 {
1286         prs_struct qbuf, rbuf;
1287         SAMR_Q_SET_USERINFO2 q;
1288         SAMR_R_SET_USERINFO2 r;
1289         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1290
1291         ZERO_STRUCT(q);
1292         ZERO_STRUCT(r);
1293
1294         /* Initialise parse structures */
1295
1296         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1297         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1298
1299         /* Marshall data and send request */
1300
1301         init_samr_q_set_userinfo2(&q, user_pol, sess_key, switch_value, ctr);
1302
1303         if (!samr_io_q_set_userinfo2("", &q, &qbuf, 0) ||
1304             !rpc_api_pipe_req(cli, SAMR_SET_USERINFO2, &qbuf, &rbuf)) {
1305                 goto done;
1306         }
1307
1308         /* Unmarshall response */
1309
1310         if (!samr_io_r_set_userinfo2("", &r, &rbuf, 0)) {
1311                 goto done;
1312         }
1313
1314         /* Return output parameters */
1315
1316         if (!NT_STATUS_IS_OK(result = r.status)) {
1317                 goto done;
1318         }
1319
1320  done:
1321         prs_mem_free(&qbuf);
1322         prs_mem_free(&rbuf);
1323
1324         return result;
1325 }
1326
1327 /* Delete domain user */
1328
1329 NTSTATUS cli_samr_delete_dom_user(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1330                                   POLICY_HND *user_pol)
1331 {
1332         prs_struct qbuf, rbuf;
1333         SAMR_Q_DELETE_DOM_USER q;
1334         SAMR_R_DELETE_DOM_USER r;
1335         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1336
1337         ZERO_STRUCT(q);
1338         ZERO_STRUCT(r);
1339
1340         /* Initialise parse structures */
1341
1342         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1343         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1344
1345         /* Marshall data and send request */
1346
1347         init_samr_q_delete_dom_user(&q, user_pol);
1348
1349         if (!samr_io_q_delete_dom_user("", &q, &qbuf, 0) ||
1350             !rpc_api_pipe_req(cli, SAMR_DELETE_DOM_USER, &qbuf, &rbuf)) {
1351                 goto done;
1352         }
1353
1354         /* Unmarshall response */
1355
1356         if (!samr_io_r_delete_dom_user("", &r, &rbuf, 0)) {
1357                 goto done;
1358         }
1359
1360         /* Return output parameters */
1361
1362         result = r.status;
1363
1364  done:
1365         prs_mem_free(&qbuf);
1366         prs_mem_free(&rbuf);
1367
1368         return result;
1369 }
1370
1371 /* Query user security object */
1372
1373 NTSTATUS cli_samr_query_sec_obj(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1374                                  POLICY_HND *user_pol, uint16 switch_value, 
1375                                  TALLOC_CTX *ctx, SEC_DESC_BUF **sec_desc_buf)
1376 {
1377         prs_struct qbuf, rbuf;
1378         SAMR_Q_QUERY_SEC_OBJ q;
1379         SAMR_R_QUERY_SEC_OBJ r;
1380         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1381
1382         ZERO_STRUCT(q);
1383         ZERO_STRUCT(r);
1384
1385         /* Initialise parse structures */
1386
1387         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1388         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1389
1390         /* Marshall data and send request */
1391
1392         init_samr_q_query_sec_obj(&q, user_pol, switch_value);
1393
1394         if (!samr_io_q_query_sec_obj("", &q, &qbuf, 0) ||
1395             !rpc_api_pipe_req(cli, SAMR_QUERY_SEC_OBJECT, &qbuf, &rbuf)) {
1396                 goto done;
1397         }
1398
1399         /* Unmarshall response */
1400
1401         if (!samr_io_r_query_sec_obj("", &r, &rbuf, 0)) {
1402                 goto done;
1403         }
1404
1405         /* Return output parameters */
1406
1407         result = r.status;
1408         *sec_desc_buf=dup_sec_desc_buf(ctx, r.buf);
1409
1410  done:
1411         prs_mem_free(&qbuf);
1412         prs_mem_free(&rbuf);
1413
1414         return result;
1415 }
1416
1417 /* Get domain password info */
1418
1419 NTSTATUS cli_samr_get_dom_pwinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1420                                  uint16 *unk_0, uint16 *unk_1, uint16 *unk_2)
1421 {
1422         prs_struct qbuf, rbuf;
1423         SAMR_Q_GET_DOM_PWINFO q;
1424         SAMR_R_GET_DOM_PWINFO r;
1425         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1426
1427         ZERO_STRUCT(q);
1428         ZERO_STRUCT(r);
1429
1430         /* Initialise parse structures */
1431
1432         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1433         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1434
1435         /* Marshall data and send request */
1436
1437         init_samr_q_get_dom_pwinfo(&q, cli->desthost);
1438
1439         if (!samr_io_q_get_dom_pwinfo("", &q, &qbuf, 0) ||
1440             !rpc_api_pipe_req(cli, SAMR_GET_DOM_PWINFO, &qbuf, &rbuf))
1441                 goto done;
1442
1443         /* Unmarshall response */
1444
1445         if (!samr_io_r_get_dom_pwinfo("", &r, &rbuf, 0))
1446                 goto done;
1447
1448         /* Return output parameters */
1449
1450         result = r.status;
1451
1452         if (NT_STATUS_IS_OK(result)) {
1453                 if (unk_0)
1454                         *unk_0 = r.unk_0;
1455                 if (unk_1)
1456                         *unk_1 = r.unk_1;
1457                 if (unk_2)
1458                         *unk_2 = r.unk_2;
1459         }
1460
1461  done:
1462         prs_mem_free(&qbuf);
1463         prs_mem_free(&rbuf);
1464
1465         return result;
1466 }