Initial import
[samba] / 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) Rafal Szczesniak                       2002.
7    Copyright (C) Jeremy Allison                         2005.
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 /* Connect to SAMR database */
27
28 NTSTATUS rpccli_samr_connect(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
29                              uint32 access_mask, POLICY_HND *connect_pol)
30 {
31         prs_struct qbuf, rbuf;
32         SAMR_Q_CONNECT q;
33         SAMR_R_CONNECT r;
34         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
35
36         DEBUG(10,("cli_samr_connect to %s\n", cli->cli->desthost));
37
38         ZERO_STRUCT(q);
39         ZERO_STRUCT(r);
40
41         /* Marshall data and send request */
42
43         init_samr_q_connect(&q, cli->cli->desthost, access_mask);
44
45         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CONNECT,
46                 q, r,
47                 qbuf, rbuf,
48                 samr_io_q_connect,
49                 samr_io_r_connect,
50                 NT_STATUS_UNSUCCESSFUL); 
51         /* Return output parameters */
52
53         if (NT_STATUS_IS_OK(result = r.status)) {
54                 *connect_pol = r.connect_pol;
55 #ifdef __INSURE__
56                 connect_pol->marker = malloc(1);
57 #endif
58         }
59
60         return result;
61 }
62
63 /* Connect to SAMR database */
64
65 NTSTATUS rpccli_samr_connect4(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
66                            uint32 access_mask, POLICY_HND *connect_pol)
67 {
68         prs_struct qbuf, rbuf;
69         SAMR_Q_CONNECT4 q;
70         SAMR_R_CONNECT4 r;
71         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
72
73         ZERO_STRUCT(q);
74         ZERO_STRUCT(r);
75
76         /* Marshall data and send request */
77
78         init_samr_q_connect4(&q, cli->cli->desthost, access_mask);
79
80         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CONNECT4,
81                 q, r,
82                 qbuf, rbuf,
83                 samr_io_q_connect4,
84                 samr_io_r_connect4,
85                 NT_STATUS_UNSUCCESSFUL); 
86
87         /* Return output parameters */
88
89         if (NT_STATUS_IS_OK(result = r.status)) {
90                 *connect_pol = r.connect_pol;
91 #ifdef __INSURE__
92                 connect_pol->marker = malloc(1);
93 #endif
94         }
95
96         return result;
97 }
98
99 /* Close SAMR handle */
100
101 NTSTATUS rpccli_samr_close(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
102                            POLICY_HND *connect_pol)
103 {
104         prs_struct qbuf, rbuf;
105         SAMR_Q_CLOSE_HND q;
106         SAMR_R_CLOSE_HND r;
107         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
108
109         DEBUG(10,("cli_samr_close\n"));
110
111         ZERO_STRUCT(q);
112         ZERO_STRUCT(r);
113
114         /* Marshall data and send request */
115
116         init_samr_q_close_hnd(&q, connect_pol);
117
118         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CLOSE_HND,
119                 q, r,
120                 qbuf, rbuf,
121                 samr_io_q_close_hnd,
122                 samr_io_r_close_hnd,
123                 NT_STATUS_UNSUCCESSFUL); 
124
125         /* Return output parameters */
126
127         if (NT_STATUS_IS_OK(result = r.status)) {
128 #ifdef __INSURE__
129                 SAFE_FREE(connect_pol->marker);
130 #endif
131                 *connect_pol = r.pol;
132         }
133
134         return result;
135 }
136
137 /* Open handle on a domain */
138
139 NTSTATUS rpccli_samr_open_domain(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
140                                  POLICY_HND *connect_pol, uint32 access_mask, 
141                                  const DOM_SID *domain_sid,
142                                  POLICY_HND *domain_pol)
143 {
144         prs_struct qbuf, rbuf;
145         SAMR_Q_OPEN_DOMAIN q;
146         SAMR_R_OPEN_DOMAIN r;
147         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
148
149         DEBUG(10,("cli_samr_open_domain with sid %s\n", sid_string_static(domain_sid) ));
150
151         ZERO_STRUCT(q);
152         ZERO_STRUCT(r);
153
154         /* Marshall data and send request */
155
156         init_samr_q_open_domain(&q, connect_pol, access_mask, domain_sid);
157
158         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_OPEN_DOMAIN,
159                 q, r,
160                 qbuf, rbuf,
161                 samr_io_q_open_domain,
162                 samr_io_r_open_domain,
163                 NT_STATUS_UNSUCCESSFUL); 
164
165         /* Return output parameters */
166
167         if (NT_STATUS_IS_OK(result = r.status)) {
168                 *domain_pol = r.domain_pol;
169 #ifdef __INSURE__
170                 domain_pol->marker = malloc(1);
171 #endif
172         }
173
174         return result;
175 }
176
177 NTSTATUS rpccli_samr_open_user(struct rpc_pipe_client *cli,
178                                TALLOC_CTX *mem_ctx,
179                                POLICY_HND *domain_pol, uint32 access_mask, 
180                                uint32 user_rid, POLICY_HND *user_pol)
181 {
182         prs_struct qbuf, rbuf;
183         SAMR_Q_OPEN_USER q;
184         SAMR_R_OPEN_USER r;
185         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
186
187         DEBUG(10,("cli_samr_open_user with rid 0x%x\n", user_rid ));
188
189         ZERO_STRUCT(q);
190         ZERO_STRUCT(r);
191
192         /* Marshall data and send request */
193
194         init_samr_q_open_user(&q, domain_pol, access_mask, user_rid);
195
196         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_OPEN_USER,
197                 q, r,
198                 qbuf, rbuf,
199                 samr_io_q_open_user,
200                 samr_io_r_open_user,
201                 NT_STATUS_UNSUCCESSFUL); 
202
203         /* Return output parameters */
204
205         if (NT_STATUS_IS_OK(result = r.status)) {
206                 *user_pol = r.user_pol;
207 #ifdef __INSURE__
208                 user_pol->marker = malloc(1);
209 #endif
210         }
211
212         return result;
213 }
214
215 /* Open handle on a group */
216
217 NTSTATUS rpccli_samr_open_group(struct rpc_pipe_client *cli,
218                                 TALLOC_CTX *mem_ctx, 
219                                 POLICY_HND *domain_pol, uint32 access_mask, 
220                                 uint32 group_rid, POLICY_HND *group_pol)
221 {
222         prs_struct qbuf, rbuf;
223         SAMR_Q_OPEN_GROUP q;
224         SAMR_R_OPEN_GROUP r;
225         NTSTATUS result =  NT_STATUS_UNSUCCESSFUL;
226
227         DEBUG(10,("cli_samr_open_group with rid 0x%x\n", group_rid ));
228
229         ZERO_STRUCT(q);
230         ZERO_STRUCT(r);
231
232         /* Marshall data and send request */
233
234         init_samr_q_open_group(&q, domain_pol, access_mask, group_rid);
235
236         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_OPEN_GROUP,
237                 q, r,
238                 qbuf, rbuf,
239                 samr_io_q_open_group,
240                 samr_io_r_open_group,
241                 NT_STATUS_UNSUCCESSFUL); 
242
243         /* Return output parameters */
244
245         if (NT_STATUS_IS_OK(result = r.status)) {
246                 *group_pol = r.pol;
247 #ifdef __INSURE__
248                 group_pol->marker = malloc(1);
249 #endif
250         }
251
252         return result;
253 }
254
255 /* Create domain group */
256
257 NTSTATUS rpccli_samr_create_dom_group(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
258                                    POLICY_HND *domain_pol,
259                                    const char *group_name,
260                                    uint32 access_mask, POLICY_HND *group_pol)
261 {
262         prs_struct qbuf, rbuf;
263         SAMR_Q_CREATE_DOM_GROUP q;
264         SAMR_R_CREATE_DOM_GROUP r;
265         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
266
267         DEBUG(10,("cli_samr_create_dom_group\n"));
268
269         ZERO_STRUCT(q);
270         ZERO_STRUCT(r);
271
272         /* Marshall data and send request */
273
274         init_samr_q_create_dom_group(&q, domain_pol, group_name, access_mask);
275
276         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CREATE_DOM_GROUP,
277                 q, r,
278                 qbuf, rbuf,
279                 samr_io_q_create_dom_group,
280                 samr_io_r_create_dom_group,
281                 NT_STATUS_UNSUCCESSFUL); 
282
283         /* Return output parameters */
284
285         result = r.status;
286
287         if (NT_STATUS_IS_OK(result))
288                 *group_pol = r.pol;
289
290         return result;
291 }
292
293 /* Add a domain group member */
294
295 NTSTATUS rpccli_samr_add_groupmem(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
296                                POLICY_HND *group_pol, uint32 rid)
297 {
298         prs_struct qbuf, rbuf;
299         SAMR_Q_ADD_GROUPMEM q;
300         SAMR_R_ADD_GROUPMEM r;
301         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
302
303         DEBUG(10,("cli_samr_add_groupmem\n"));
304
305         ZERO_STRUCT(q);
306         ZERO_STRUCT(r);
307
308         /* Marshall data and send request */
309
310         init_samr_q_add_groupmem(&q, group_pol, rid);
311
312         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_ADD_GROUPMEM,
313                 q, r,
314                 qbuf, rbuf,
315                 samr_io_q_add_groupmem,
316                 samr_io_r_add_groupmem,
317                 NT_STATUS_UNSUCCESSFUL); 
318
319         /* Return output parameters */
320
321         result = r.status;
322
323         return result;
324 }
325
326 /* Delete a domain group member */
327
328 NTSTATUS rpccli_samr_del_groupmem(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
329                                POLICY_HND *group_pol, uint32 rid)
330 {
331         prs_struct qbuf, rbuf;
332         SAMR_Q_DEL_GROUPMEM q;
333         SAMR_R_DEL_GROUPMEM r;
334         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
335
336         DEBUG(10,("cli_samr_del_groupmem\n"));
337
338         ZERO_STRUCT(q);
339         ZERO_STRUCT(r);
340
341         /* Marshall data and send request */
342
343         init_samr_q_del_groupmem(&q, group_pol, rid);
344
345         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_DEL_GROUPMEM,
346                 q, r,
347                 qbuf, rbuf,
348                 samr_io_q_del_groupmem,
349                 samr_io_r_del_groupmem,
350                 NT_STATUS_UNSUCCESSFUL); 
351
352         /* Return output parameters */
353
354         result = r.status;
355
356         return result;
357 }
358
359 /* Query user info */
360
361 NTSTATUS rpccli_samr_query_userinfo(struct rpc_pipe_client *cli,
362                                     TALLOC_CTX *mem_ctx,
363                                     POLICY_HND *user_pol, uint16 switch_value, 
364                                     SAM_USERINFO_CTR **ctr)
365 {
366         prs_struct qbuf, rbuf;
367         SAMR_Q_QUERY_USERINFO q;
368         SAMR_R_QUERY_USERINFO r;
369         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
370
371         DEBUG(10,("cli_samr_query_userinfo\n"));
372
373         ZERO_STRUCT(q);
374         ZERO_STRUCT(r);
375
376         /* Marshall data and send request */
377
378         init_samr_q_query_userinfo(&q, user_pol, switch_value);
379
380         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_USERINFO,
381                 q, r,
382                 qbuf, rbuf,
383                 samr_io_q_query_userinfo,
384                 samr_io_r_query_userinfo,
385                 NT_STATUS_UNSUCCESSFUL); 
386
387         /* Return output parameters */
388
389         result = r.status;
390         *ctr = r.ctr;
391
392         return result;
393 }
394
395 /* Set group info */
396
397 NTSTATUS rpccli_samr_set_groupinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
398                                 POLICY_HND *group_pol, GROUP_INFO_CTR *ctr)
399 {
400         prs_struct qbuf, rbuf;
401         SAMR_Q_SET_GROUPINFO q;
402         SAMR_R_SET_GROUPINFO r;
403         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
404
405         DEBUG(10,("cli_samr_set_groupinfo\n"));
406
407         ZERO_STRUCT(q);
408         ZERO_STRUCT(r);
409
410         /* Marshall data and send request */
411
412         init_samr_q_set_groupinfo(&q, group_pol, ctr);
413
414         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_SET_GROUPINFO,
415                 q, r,
416                 qbuf, rbuf,
417                 samr_io_q_set_groupinfo,
418                 samr_io_r_set_groupinfo,
419                 NT_STATUS_UNSUCCESSFUL); 
420
421         /* Return output parameters */
422
423         result = r.status;
424
425         return result;
426 }
427
428 /* Query group info */
429
430 NTSTATUS rpccli_samr_query_groupinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
431                                   POLICY_HND *group_pol, uint32 info_level, 
432                                   GROUP_INFO_CTR **ctr)
433 {
434         prs_struct qbuf, rbuf;
435         SAMR_Q_QUERY_GROUPINFO q;
436         SAMR_R_QUERY_GROUPINFO r;
437         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
438
439         DEBUG(10,("cli_samr_query_groupinfo\n"));
440
441         ZERO_STRUCT(q);
442         ZERO_STRUCT(r);
443
444         /* Marshall data and send request */
445
446         init_samr_q_query_groupinfo(&q, group_pol, info_level);
447
448         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_GROUPINFO,
449                 q, r,
450                 qbuf, rbuf,
451                 samr_io_q_query_groupinfo,
452                 samr_io_r_query_groupinfo,
453                 NT_STATUS_UNSUCCESSFUL); 
454
455         *ctr = r.ctr;
456
457         /* Return output parameters */
458
459         result = r.status;
460
461         return result;
462 }
463
464 /* Query user groups */
465
466 NTSTATUS rpccli_samr_query_usergroups(struct rpc_pipe_client *cli,
467                                       TALLOC_CTX *mem_ctx, 
468                                       POLICY_HND *user_pol,
469                                       uint32 *num_groups, 
470                                       DOM_GID **gid)
471 {
472         prs_struct qbuf, rbuf;
473         SAMR_Q_QUERY_USERGROUPS q;
474         SAMR_R_QUERY_USERGROUPS r;
475         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
476
477         DEBUG(10,("cli_samr_query_usergroups\n"));
478
479         ZERO_STRUCT(q);
480         ZERO_STRUCT(r);
481
482         /* Marshall data and send request */
483
484         init_samr_q_query_usergroups(&q, user_pol);
485
486         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_USERGROUPS,
487                 q, r,
488                 qbuf, rbuf,
489                 samr_io_q_query_usergroups,
490                 samr_io_r_query_usergroups,
491                 NT_STATUS_UNSUCCESSFUL); 
492
493         /* Return output parameters */
494
495         if (NT_STATUS_IS_OK(result = r.status)) {
496                 *num_groups = r.num_entries;
497                 *gid = r.gid;
498         }
499
500         return result;
501 }
502
503 /* Set alias info */
504
505 NTSTATUS rpccli_samr_set_aliasinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
506                                 POLICY_HND *alias_pol, ALIAS_INFO_CTR *ctr)
507 {
508         prs_struct qbuf, rbuf;
509         SAMR_Q_SET_ALIASINFO q;
510         SAMR_R_SET_ALIASINFO r;
511         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
512
513         DEBUG(10,("cli_samr_set_aliasinfo\n"));
514
515         ZERO_STRUCT(q);
516         ZERO_STRUCT(r);
517
518         /* Marshall data and send request */
519
520         init_samr_q_set_aliasinfo(&q, alias_pol, ctr);
521
522         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_SET_ALIASINFO,
523                 q, r,
524                 qbuf, rbuf,
525                 samr_io_q_set_aliasinfo,
526                 samr_io_r_set_aliasinfo,
527                 NT_STATUS_UNSUCCESSFUL); 
528
529         /* Return output parameters */
530
531         result = r.status;
532
533         return result;
534 }
535
536 /* Query user aliases */
537
538 NTSTATUS rpccli_samr_query_useraliases(struct rpc_pipe_client *cli,
539                                        TALLOC_CTX *mem_ctx, 
540                                        POLICY_HND *dom_pol, uint32 num_sids,
541                                        DOM_SID2 *sid,
542                                        uint32 *num_aliases, uint32 **als_rids)
543 {
544         prs_struct qbuf, rbuf;
545         SAMR_Q_QUERY_USERALIASES q;
546         SAMR_R_QUERY_USERALIASES r;
547         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
548         int i;
549         uint32 *sid_ptrs;
550         
551         DEBUG(10,("cli_samr_query_useraliases\n"));
552
553         ZERO_STRUCT(q);
554         ZERO_STRUCT(r);
555
556         sid_ptrs = TALLOC_ARRAY(mem_ctx, uint32, num_sids);
557         if (sid_ptrs == NULL)
558                 return NT_STATUS_NO_MEMORY;
559
560         for (i=0; i<num_sids; i++)
561                 sid_ptrs[i] = 1;
562
563         /* Marshall data and send request */
564
565         init_samr_q_query_useraliases(&q, dom_pol, num_sids, sid_ptrs, sid);
566
567         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_USERALIASES,
568                 q, r,
569                 qbuf, rbuf,
570                 samr_io_q_query_useraliases,
571                 samr_io_r_query_useraliases,
572                 NT_STATUS_UNSUCCESSFUL); 
573
574         /* Return output parameters */
575
576         if (NT_STATUS_IS_OK(result = r.status)) {
577                 *num_aliases = r.num_entries;
578                 *als_rids = r.rid;
579         }
580
581         return result;
582 }
583
584 /* Query user groups */
585
586 NTSTATUS rpccli_samr_query_groupmem(struct rpc_pipe_client *cli,
587                                     TALLOC_CTX *mem_ctx,
588                                     POLICY_HND *group_pol, uint32 *num_mem, 
589                                     uint32 **rid, uint32 **attr)
590 {
591         prs_struct qbuf, rbuf;
592         SAMR_Q_QUERY_GROUPMEM q;
593         SAMR_R_QUERY_GROUPMEM r;
594         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
595
596         DEBUG(10,("cli_samr_query_groupmem\n"));
597
598         ZERO_STRUCT(q);
599         ZERO_STRUCT(r);
600
601         /* Marshall data and send request */
602
603         init_samr_q_query_groupmem(&q, group_pol);
604
605         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_GROUPMEM,
606                 q, r,
607                 qbuf, rbuf,
608                 samr_io_q_query_groupmem,
609                 samr_io_r_query_groupmem,
610                 NT_STATUS_UNSUCCESSFUL); 
611
612         /* Return output parameters */
613
614         if (NT_STATUS_IS_OK(result = r.status)) {
615                 *num_mem = r.num_entries;
616                 *rid = r.rid;
617                 *attr = r.attr;
618         }
619
620         return result;
621 }
622
623 /**
624  * Enumerate domain users
625  *
626  * @param cli client state structure
627  * @param mem_ctx talloc context
628  * @param pol opened domain policy handle
629  * @param start_idx starting index of enumeration, returns context for
630                     next enumeration
631  * @param acb_mask account control bit mask (to enumerate some particular
632  *                 kind of accounts)
633  * @param size max acceptable size of response
634  * @param dom_users returned array of domain user names
635  * @param rids returned array of domain user RIDs
636  * @param num_dom_users numer returned entries
637  * 
638  * @return NTSTATUS returned in rpc response
639  **/
640
641 NTSTATUS rpccli_samr_enum_dom_users(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
642                                  POLICY_HND *pol, uint32 *start_idx, uint16 acb_mask,
643                                  uint32 size, char ***dom_users, uint32 **rids,
644                                  uint32 *num_dom_users)
645 {
646         prs_struct qbuf;
647         prs_struct rbuf;
648         SAMR_Q_ENUM_DOM_USERS q;
649         SAMR_R_ENUM_DOM_USERS r;
650         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
651         int i;
652         
653         DEBUG(10,("cli_samr_enum_dom_users starting at index %u\n", (unsigned int)*start_idx));
654
655         ZERO_STRUCT(q);
656         ZERO_STRUCT(r);
657         
658         /* always init this */
659         *num_dom_users = 0;
660         
661         /* Fill query structure with parameters */
662
663         init_samr_q_enum_dom_users(&q, pol, *start_idx, acb_mask, 0, size);
664         
665         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_ENUM_DOM_USERS,
666                 q, r,
667                 qbuf, rbuf,
668                 samr_io_q_enum_dom_users,
669                 samr_io_r_enum_dom_users,
670                 NT_STATUS_UNSUCCESSFUL); 
671
672         result = r.status;
673
674         if (!NT_STATUS_IS_OK(result) &&
675             NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES))
676                 goto done;
677         
678         *start_idx = r.next_idx;
679         *num_dom_users = r.num_entries2;
680
681         if (r.num_entries2) {
682                 /* allocate memory needed to return received data */    
683                 *rids = TALLOC_ARRAY(mem_ctx, uint32, r.num_entries2);
684                 if (!*rids) {
685                         DEBUG(0, ("Error in cli_samr_enum_dom_users(): out of memory\n"));
686                         return NT_STATUS_NO_MEMORY;
687                 }
688                 
689                 *dom_users = TALLOC_ARRAY(mem_ctx, char*, r.num_entries2);
690                 if (!*dom_users) {
691                         DEBUG(0, ("Error in cli_samr_enum_dom_users(): out of memory\n"));
692                         return NT_STATUS_NO_MEMORY;
693                 }
694                 
695                 /* fill output buffers with rpc response */
696                 for (i = 0; i < r.num_entries2; i++) {
697                         fstring conv_buf;
698                         
699                         (*rids)[i] = r.sam[i].rid;
700                         unistr2_to_ascii(conv_buf, &(r.uni_acct_name[i]), sizeof(conv_buf) - 1);
701                         (*dom_users)[i] = talloc_strdup(mem_ctx, conv_buf);
702                 }
703         }
704         
705 done:
706         return result;
707 }
708
709 /* Enumerate domain groups */
710
711 NTSTATUS rpccli_samr_enum_dom_groups(struct rpc_pipe_client *cli,
712                                      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_GROUPS q;
719         SAMR_R_ENUM_DOM_GROUPS r;
720         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
721         uint32 name_idx, i;
722
723         DEBUG(10,("cli_samr_enum_dom_groups starting at index %u\n", (unsigned int)*start_idx));
724
725         ZERO_STRUCT(q);
726         ZERO_STRUCT(r);
727
728         /* Marshall data and send request */
729
730         init_samr_q_enum_dom_groups(&q, pol, *start_idx, size);
731
732         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_ENUM_DOM_GROUPS,
733                 q, r,
734                 qbuf, rbuf,
735                 samr_io_q_enum_dom_groups,
736                 samr_io_r_enum_dom_groups,
737                 NT_STATUS_UNSUCCESSFUL); 
738
739         /* Return output parameters */
740
741         result = r.status;
742
743         if (!NT_STATUS_IS_OK(result) &&
744             NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES))
745                 goto done;
746
747         *num_dom_groups = r.num_entries2;
748
749         if (*num_dom_groups == 0)
750                 goto done;
751
752         if (!((*dom_groups) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_dom_groups))) {
753                 result = NT_STATUS_NO_MEMORY;
754                 goto done;
755         }
756
757         memset(*dom_groups, 0, sizeof(struct acct_info) * (*num_dom_groups));
758
759         name_idx = 0;
760
761         for (i = 0; i < *num_dom_groups; i++) {
762
763                 (*dom_groups)[i].rid = r.sam[i].rid;
764
765                 if (r.sam[i].hdr_name.buffer) {
766                         unistr2_to_ascii((*dom_groups)[i].acct_name,
767                                          &r.uni_grp_name[name_idx],
768                                          sizeof(fstring) - 1);
769                         name_idx++;
770                 }
771
772                 *start_idx = r.next_idx;
773         }
774
775  done:
776         return result;
777 }
778
779 /* Enumerate domain groups */
780
781 NTSTATUS rpccli_samr_enum_als_groups(struct rpc_pipe_client *cli,
782                                      TALLOC_CTX *mem_ctx, 
783                                      POLICY_HND *pol, uint32 *start_idx, 
784                                      uint32 size, struct acct_info **dom_aliases,
785                                      uint32 *num_dom_aliases)
786 {
787         prs_struct qbuf, rbuf;
788         SAMR_Q_ENUM_DOM_ALIASES q;
789         SAMR_R_ENUM_DOM_ALIASES r;
790         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
791         uint32 name_idx, i;
792
793         DEBUG(10,("cli_samr_enum_als_groups starting at index %u\n", (unsigned int)*start_idx));
794
795         ZERO_STRUCT(q);
796         ZERO_STRUCT(r);
797
798         /* Marshall data and send request */
799
800         init_samr_q_enum_dom_aliases(&q, pol, *start_idx, size);
801
802         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_ENUM_DOM_ALIASES,
803                 q, r,
804                 qbuf, rbuf,
805                 samr_io_q_enum_dom_aliases,
806                 samr_io_r_enum_dom_aliases,
807                 NT_STATUS_UNSUCCESSFUL); 
808
809         /* Return output parameters */
810
811         result = r.status;
812
813         if (!NT_STATUS_IS_OK(result) &&
814             NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES)) {
815                 goto done;
816         }
817
818         *num_dom_aliases = r.num_entries2;
819
820         if (*num_dom_aliases == 0)
821                 goto done;
822
823         if (!((*dom_aliases) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_dom_aliases))) {
824                 result = NT_STATUS_NO_MEMORY;
825                 goto done;
826         }
827
828         memset(*dom_aliases, 0, sizeof(struct acct_info) * *num_dom_aliases);
829
830         name_idx = 0;
831
832         for (i = 0; i < *num_dom_aliases; i++) {
833
834                 (*dom_aliases)[i].rid = r.sam[i].rid;
835
836                 if (r.sam[i].hdr_name.buffer) {
837                         unistr2_to_ascii((*dom_aliases)[i].acct_name,
838                                          &r.uni_grp_name[name_idx],
839                                          sizeof(fstring) - 1);
840                         name_idx++;
841                 }
842
843                 *start_idx = r.next_idx;
844         }
845
846  done:
847         return result;
848 }
849
850 /* Query alias members */
851
852 NTSTATUS rpccli_samr_query_aliasmem(struct rpc_pipe_client *cli,
853                                     TALLOC_CTX *mem_ctx,
854                                     POLICY_HND *alias_pol, uint32 *num_mem, 
855                                     DOM_SID **sids)
856 {
857         prs_struct qbuf, rbuf;
858         SAMR_Q_QUERY_ALIASMEM q;
859         SAMR_R_QUERY_ALIASMEM r;
860         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
861         uint32 i;
862
863         DEBUG(10,("cli_samr_query_aliasmem\n"));
864
865         ZERO_STRUCT(q);
866         ZERO_STRUCT(r);
867
868         /* Marshall data and send request */
869
870         init_samr_q_query_aliasmem(&q, alias_pol);
871
872         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_ALIASMEM,
873                 q, r,
874                 qbuf, rbuf,
875                 samr_io_q_query_aliasmem,
876                 samr_io_r_query_aliasmem,
877                 NT_STATUS_UNSUCCESSFUL); 
878
879         /* Return output parameters */
880
881         if (!NT_STATUS_IS_OK(result = r.status)) {
882                 goto done;
883         }
884
885         *num_mem = r.num_sids;
886
887         if (*num_mem == 0) {
888                 *sids = NULL;
889                 result = NT_STATUS_OK;
890                 goto done;
891         }
892
893         if (!(*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *num_mem))) {
894                 result = NT_STATUS_UNSUCCESSFUL;
895                 goto done;
896         }
897
898         for (i = 0; i < *num_mem; i++) {
899                 (*sids)[i] = r.sid[i].sid;
900         }
901
902  done:
903         return result;
904 }
905
906 /* Open handle on an alias */
907
908 NTSTATUS rpccli_samr_open_alias(struct rpc_pipe_client *cli,
909                                 TALLOC_CTX *mem_ctx, 
910                                 POLICY_HND *domain_pol, uint32 access_mask, 
911                                 uint32 alias_rid, POLICY_HND *alias_pol)
912 {
913         prs_struct qbuf, rbuf;
914         SAMR_Q_OPEN_ALIAS q;
915         SAMR_R_OPEN_ALIAS r;
916         NTSTATUS result;
917
918         DEBUG(10,("cli_samr_open_alias with rid 0x%x\n", alias_rid));
919
920         ZERO_STRUCT(q);
921         ZERO_STRUCT(r);
922
923         /* Marshall data and send request */
924
925         init_samr_q_open_alias(&q, domain_pol, access_mask, alias_rid);
926
927         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_OPEN_ALIAS,
928                 q, r,
929                 qbuf, rbuf,
930                 samr_io_q_open_alias,
931                 samr_io_r_open_alias,
932                 NT_STATUS_UNSUCCESSFUL); 
933
934         /* Return output parameters */
935
936         if (NT_STATUS_IS_OK(result = r.status)) {
937                 *alias_pol = r.pol;
938 #ifdef __INSURE__
939                 alias_pol->marker = malloc(1);
940 #endif
941         }
942
943         return result;
944 }
945
946 /* Create an alias */
947
948 NTSTATUS rpccli_samr_create_dom_alias(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
949                                    POLICY_HND *domain_pol, const char *name,
950                                    POLICY_HND *alias_pol)
951 {
952         prs_struct qbuf, rbuf;
953         SAMR_Q_CREATE_DOM_ALIAS q;
954         SAMR_R_CREATE_DOM_ALIAS r;
955         NTSTATUS result;
956
957         DEBUG(10,("cli_samr_create_dom_alias named %s\n", name));
958
959         ZERO_STRUCT(q);
960         ZERO_STRUCT(r);
961
962         /* Marshall data and send request */
963
964         init_samr_q_create_dom_alias(&q, domain_pol, name);
965
966         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CREATE_DOM_ALIAS,
967                 q, r,
968                 qbuf, rbuf,
969                 samr_io_q_create_dom_alias,
970                 samr_io_r_create_dom_alias,
971                 NT_STATUS_UNSUCCESSFUL); 
972
973         /* Return output parameters */
974
975         if (NT_STATUS_IS_OK(result = r.status)) {
976                 *alias_pol = r.alias_pol;
977         }
978
979         return result;
980 }
981
982 /* Add an alias member */
983
984 NTSTATUS rpccli_samr_add_aliasmem(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
985                                POLICY_HND *alias_pol, DOM_SID *member)
986 {
987         prs_struct qbuf, rbuf;
988         SAMR_Q_ADD_ALIASMEM q;
989         SAMR_R_ADD_ALIASMEM r;
990         NTSTATUS result;
991
992         DEBUG(10,("cli_samr_add_aliasmem"));
993
994         ZERO_STRUCT(q);
995         ZERO_STRUCT(r);
996
997         /* Marshall data and send request */
998
999         init_samr_q_add_aliasmem(&q, alias_pol, member);
1000
1001         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_ADD_ALIASMEM,
1002                 q, r,
1003                 qbuf, rbuf,
1004                 samr_io_q_add_aliasmem,
1005                 samr_io_r_add_aliasmem,
1006                 NT_STATUS_UNSUCCESSFUL); 
1007
1008         result = r.status;
1009
1010         return result;
1011 }
1012
1013 /* Delete an alias member */
1014
1015 NTSTATUS rpccli_samr_del_aliasmem(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
1016                                POLICY_HND *alias_pol, DOM_SID *member)
1017 {
1018         prs_struct qbuf, rbuf;
1019         SAMR_Q_DEL_ALIASMEM q;
1020         SAMR_R_DEL_ALIASMEM r;
1021         NTSTATUS result;
1022
1023         DEBUG(10,("cli_samr_del_aliasmem"));
1024
1025         ZERO_STRUCT(q);
1026         ZERO_STRUCT(r);
1027
1028         /* Marshall data and send request */
1029
1030         init_samr_q_del_aliasmem(&q, alias_pol, member);
1031
1032         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_DEL_ALIASMEM,
1033                 q, r,
1034                 qbuf, rbuf,
1035                 samr_io_q_del_aliasmem,
1036                 samr_io_r_del_aliasmem,
1037                 NT_STATUS_UNSUCCESSFUL); 
1038
1039         result = r.status;
1040
1041         return result;
1042 }
1043
1044 /* Query alias info */
1045
1046 NTSTATUS rpccli_samr_query_alias_info(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1047                                    POLICY_HND *alias_pol, uint16 switch_value,
1048                                    ALIAS_INFO_CTR *ctr)
1049 {
1050         prs_struct qbuf, rbuf;
1051         SAMR_Q_QUERY_ALIASINFO q;
1052         SAMR_R_QUERY_ALIASINFO r;
1053         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1054
1055         DEBUG(10,("cli_samr_query_alias_info\n"));
1056
1057         ZERO_STRUCT(q);
1058         ZERO_STRUCT(r);
1059
1060         /* Marshall data and send request */
1061
1062         init_samr_q_query_aliasinfo(&q, alias_pol, switch_value);
1063
1064         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_ALIASINFO,
1065                 q, r,
1066                 qbuf, rbuf,
1067                 samr_io_q_query_aliasinfo,
1068                 samr_io_r_query_aliasinfo,
1069                 NT_STATUS_UNSUCCESSFUL); 
1070
1071         /* Return output parameters */
1072
1073         if (!NT_STATUS_IS_OK(result = r.status)) {
1074                 goto done;
1075         }
1076
1077         *ctr = *r.ctr;
1078
1079   done:
1080
1081         return result;
1082 }
1083
1084 /* Query domain info */
1085
1086 NTSTATUS rpccli_samr_query_dom_info(struct rpc_pipe_client *cli,
1087                                     TALLOC_CTX *mem_ctx, 
1088                                     POLICY_HND *domain_pol,
1089                                     uint16 switch_value,
1090                                     SAM_UNK_CTR *ctr)
1091 {
1092         prs_struct qbuf, rbuf;
1093         SAMR_Q_QUERY_DOMAIN_INFO q;
1094         SAMR_R_QUERY_DOMAIN_INFO r;
1095         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1096
1097         DEBUG(10,("cli_samr_query_dom_info\n"));
1098
1099         ZERO_STRUCT(q);
1100         ZERO_STRUCT(r);
1101
1102         /* Marshall data and send request */
1103
1104         init_samr_q_query_dom_info(&q, domain_pol, switch_value);
1105
1106         r.ctr = ctr;
1107
1108         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_DOMAIN_INFO,
1109                 q, r,
1110                 qbuf, rbuf,
1111                 samr_io_q_query_dom_info,
1112                 samr_io_r_query_dom_info,
1113                 NT_STATUS_UNSUCCESSFUL); 
1114
1115         /* Return output parameters */
1116
1117         if (!NT_STATUS_IS_OK(result = r.status)) {
1118                 goto done;
1119         }
1120
1121  done:
1122
1123         return result;
1124 }
1125
1126 /* User change password */
1127
1128 NTSTATUS rpccli_samr_chgpasswd_user(struct rpc_pipe_client *cli,
1129                                     TALLOC_CTX *mem_ctx, 
1130                                     const char *username, 
1131                                     const char *newpassword, 
1132                                     const char *oldpassword )
1133 {
1134         prs_struct qbuf, rbuf;
1135         SAMR_Q_CHGPASSWD_USER q;
1136         SAMR_R_CHGPASSWD_USER r;
1137         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1138
1139         uchar new_nt_password[516];
1140         uchar new_lm_password[516];
1141         uchar old_nt_hash[16];
1142         uchar old_lanman_hash[16];
1143         uchar old_nt_hash_enc[16];
1144         uchar old_lanman_hash_enc[16];
1145
1146         uchar new_nt_hash[16];
1147         uchar new_lanman_hash[16];
1148
1149         char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
1150
1151         DEBUG(10,("rpccli_samr_chgpasswd_user\n"));
1152
1153         ZERO_STRUCT(q);
1154         ZERO_STRUCT(r);
1155
1156         /* Calculate the MD4 hash (NT compatible) of the password */
1157         E_md4hash(oldpassword, old_nt_hash);
1158         E_md4hash(newpassword, new_nt_hash);
1159
1160         if (lp_client_lanman_auth() 
1161             && E_deshash(newpassword, new_lanman_hash) 
1162             && E_deshash(oldpassword, old_lanman_hash)) {
1163                 /* E_deshash returns false for 'long' passwords (> 14
1164                    DOS chars).  This allows us to match Win2k, which
1165                    does not store a LM hash for these passwords (which
1166                    would reduce the effective password length to 14) */
1167
1168                 encode_pw_buffer(new_lm_password, newpassword, STR_UNICODE);
1169
1170                 SamOEMhash( new_lm_password, old_nt_hash, 516);
1171                 E_old_pw_hash( new_nt_hash, old_lanman_hash, old_lanman_hash_enc);
1172         } else {
1173                 ZERO_STRUCT(new_lm_password);
1174                 ZERO_STRUCT(old_lanman_hash_enc);
1175         }
1176
1177         encode_pw_buffer(new_nt_password, newpassword, STR_UNICODE);
1178         
1179         SamOEMhash( new_nt_password, old_nt_hash, 516);
1180         E_old_pw_hash( new_nt_hash, old_nt_hash, old_nt_hash_enc);
1181
1182         /* Marshall data and send request */
1183
1184         init_samr_q_chgpasswd_user(&q, srv_name_slash, username, 
1185                                    new_nt_password, 
1186                                    old_nt_hash_enc, 
1187                                    new_lm_password,
1188                                    old_lanman_hash_enc);
1189
1190         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CHGPASSWD_USER,
1191                 q, r,
1192                 qbuf, rbuf,
1193                 samr_io_q_chgpasswd_user,
1194                 samr_io_r_chgpasswd_user,
1195                 NT_STATUS_UNSUCCESSFUL); 
1196
1197         /* Return output parameters */
1198
1199         if (!NT_STATUS_IS_OK(result = r.status)) {
1200                 goto done;
1201         }
1202
1203  done:
1204
1205         return result;
1206 }
1207
1208 /* change password 3 */
1209
1210 NTSTATUS rpccli_samr_chgpasswd3(struct rpc_pipe_client *cli,
1211                                 TALLOC_CTX *mem_ctx, 
1212                                 const char *username, 
1213                                 const char *newpassword, 
1214                                 const char *oldpassword,
1215                                 SAM_UNK_INFO_1 **info,
1216                                 SAMR_CHANGE_REJECT **reject)
1217 {
1218         prs_struct qbuf, rbuf;
1219         SAMR_Q_CHGPASSWD3 q;
1220         SAMR_R_CHGPASSWD3 r;
1221         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1222
1223         uchar new_nt_password[516];
1224         uchar new_lm_password[516];
1225         uchar old_nt_hash[16];
1226         uchar old_lanman_hash[16];
1227         uchar old_nt_hash_enc[16];
1228         uchar old_lanman_hash_enc[16];
1229
1230         uchar new_nt_hash[16];
1231         uchar new_lanman_hash[16];
1232
1233         char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
1234
1235         DEBUG(10,("rpccli_samr_chgpasswd3\n"));
1236
1237         ZERO_STRUCT(q);
1238         ZERO_STRUCT(r);
1239
1240         *info = NULL;
1241         *reject = NULL;
1242
1243         /* Calculate the MD4 hash (NT compatible) of the password */
1244         E_md4hash(oldpassword, old_nt_hash);
1245         E_md4hash(newpassword, new_nt_hash);
1246
1247         if (lp_client_lanman_auth() 
1248             && E_deshash(newpassword, new_lanman_hash) 
1249             && E_deshash(oldpassword, old_lanman_hash)) {
1250                 /* E_deshash returns false for 'long' passwords (> 14
1251                    DOS chars).  This allows us to match Win2k, which
1252                    does not store a LM hash for these passwords (which
1253                    would reduce the effective password length to 14) */
1254
1255                 encode_pw_buffer(new_lm_password, newpassword, STR_UNICODE);
1256
1257                 SamOEMhash( new_lm_password, old_nt_hash, 516);
1258                 E_old_pw_hash( new_nt_hash, old_lanman_hash, old_lanman_hash_enc);
1259         } else {
1260                 ZERO_STRUCT(new_lm_password);
1261                 ZERO_STRUCT(old_lanman_hash_enc);
1262         }
1263
1264         encode_pw_buffer(new_nt_password, newpassword, STR_UNICODE);
1265         
1266         SamOEMhash( new_nt_password, old_nt_hash, 516);
1267         E_old_pw_hash( new_nt_hash, old_nt_hash, old_nt_hash_enc);
1268
1269         /* Marshall data and send request */
1270
1271         init_samr_q_chgpasswd3(&q, srv_name_slash, username, 
1272                                new_nt_password, 
1273                                old_nt_hash_enc, 
1274                                new_lm_password,
1275                                old_lanman_hash_enc);
1276
1277         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CHGPASSWD3,
1278                 q, r,
1279                 qbuf, rbuf,
1280                 samr_io_q_chgpasswd3,
1281                 samr_io_r_chgpasswd3,
1282                 NT_STATUS_UNSUCCESSFUL); 
1283
1284         /* Return output parameters */
1285
1286         if (!NT_STATUS_IS_OK(result = r.status)) {
1287                 *info = &r.info;
1288                 *reject = &r.reject;
1289                 goto done;
1290         }
1291
1292  done:
1293
1294         return result;
1295 }
1296
1297 /* This function returns the bizzare set of (max_entries, max_size) required
1298    for the QueryDisplayInfo RPC to actually work against a domain controller
1299    with large (10k and higher) numbers of users.  These values were 
1300    obtained by inspection using ethereal and NT4 running User Manager. */
1301
1302 void get_query_dispinfo_params(int loop_count, uint32 *max_entries,
1303                                uint32 *max_size)
1304 {
1305         switch(loop_count) {
1306         case 0:
1307                 *max_entries = 512;
1308                 *max_size = 16383;
1309                 break;
1310         case 1:
1311                 *max_entries = 1024;
1312                 *max_size = 32766;
1313                 break;
1314         case 2:
1315                 *max_entries = 2048;
1316                 *max_size = 65532;
1317                 break;
1318         case 3:
1319                 *max_entries = 4096;
1320                 *max_size = 131064;
1321                 break;
1322         default:              /* loop_count >= 4 */
1323                 *max_entries = 4096;
1324                 *max_size = 131071;
1325                 break;
1326         }
1327 }                    
1328
1329 /* Query display info */
1330
1331 NTSTATUS rpccli_samr_query_dispinfo(struct rpc_pipe_client *cli,
1332                                     TALLOC_CTX *mem_ctx, 
1333                                     POLICY_HND *domain_pol, uint32 *start_idx,
1334                                     uint16 switch_value, uint32 *num_entries,
1335                                     uint32 max_entries, uint32 max_size,
1336                                     SAM_DISPINFO_CTR *ctr)
1337 {
1338         prs_struct qbuf, rbuf;
1339         SAMR_Q_QUERY_DISPINFO q;
1340         SAMR_R_QUERY_DISPINFO r;
1341         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1342
1343         DEBUG(10,("cli_samr_query_dispinfo for start_idx = %u\n", *start_idx));
1344
1345         ZERO_STRUCT(q);
1346         ZERO_STRUCT(r);
1347
1348         *num_entries = 0;
1349
1350         /* Marshall data and send request */
1351
1352         init_samr_q_query_dispinfo(&q, domain_pol, switch_value,
1353                                    *start_idx, max_entries, max_size);
1354
1355         r.ctr = ctr;
1356
1357         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_DISPINFO,
1358                 q, r,
1359                 qbuf, rbuf,
1360                 samr_io_q_query_dispinfo,
1361                 samr_io_r_query_dispinfo,
1362                 NT_STATUS_UNSUCCESSFUL); 
1363
1364         /* Return output parameters */
1365
1366         result = r.status;
1367
1368         if (!NT_STATUS_IS_OK(result) &&
1369             NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1370                 goto done;
1371         }
1372
1373         *num_entries = r.num_entries;
1374         *start_idx += r.num_entries;  /* No next_idx in this structure! */
1375
1376  done:
1377         return result;
1378 }
1379
1380 /* Lookup rids.  Note that NT4 seems to crash if more than ~1000 rids are
1381    looked up in one packet. */
1382
1383 NTSTATUS rpccli_samr_lookup_rids(struct rpc_pipe_client *cli,
1384                                  TALLOC_CTX *mem_ctx, 
1385                                  POLICY_HND *domain_pol,
1386                                  uint32 num_rids, uint32 *rids, 
1387                                  uint32 *num_names, char ***names,
1388                                  uint32 **name_types)
1389 {
1390         prs_struct qbuf, rbuf;
1391         SAMR_Q_LOOKUP_RIDS q;
1392         SAMR_R_LOOKUP_RIDS r;
1393         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1394         uint32 i;
1395
1396         DEBUG(10,("cli_samr_lookup_rids\n"));
1397
1398         if (num_rids > 1000) {
1399                 DEBUG(2, ("cli_samr_lookup_rids: warning: NT4 can crash if "
1400                           "more than ~1000 rids are looked up at once.\n"));
1401         }
1402
1403         ZERO_STRUCT(q);
1404         ZERO_STRUCT(r);
1405
1406         /* Marshall data and send request */
1407
1408         init_samr_q_lookup_rids(mem_ctx, &q, domain_pol, 1000, num_rids, rids);
1409
1410         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_LOOKUP_RIDS,
1411                 q, r,
1412                 qbuf, rbuf,
1413                 samr_io_q_lookup_rids,
1414                 samr_io_r_lookup_rids,
1415                 NT_STATUS_UNSUCCESSFUL); 
1416
1417         /* Return output parameters */
1418
1419         result = r.status;
1420
1421         if (!NT_STATUS_IS_OK(result) &&
1422             !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
1423                 goto done;
1424
1425         if (r.num_names1 == 0) {
1426                 *num_names = 0;
1427                 *names = NULL;
1428                 goto done;
1429         }
1430
1431         *num_names = r.num_names1;
1432         *names = TALLOC_ARRAY(mem_ctx, char *, r.num_names1);
1433         *name_types = TALLOC_ARRAY(mem_ctx, uint32, r.num_names1);
1434
1435         for (i = 0; i < r.num_names1; i++) {
1436                 fstring tmp;
1437
1438                 unistr2_to_ascii(tmp, &r.uni_name[i], sizeof(tmp) - 1);
1439                 (*names)[i] = talloc_strdup(mem_ctx, tmp);
1440                 (*name_types)[i] = r.type[i];
1441         }
1442
1443  done:
1444
1445         return result;
1446 }
1447
1448 /* Lookup names */
1449
1450 NTSTATUS rpccli_samr_lookup_names(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
1451                                POLICY_HND *domain_pol, uint32 flags,
1452                                uint32 num_names, const char **names,
1453                                uint32 *num_rids, uint32 **rids,
1454                                uint32 **rid_types)
1455 {
1456         prs_struct qbuf, rbuf;
1457         SAMR_Q_LOOKUP_NAMES q;
1458         SAMR_R_LOOKUP_NAMES r;
1459         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1460         uint32 i;
1461
1462         DEBUG(10,("cli_samr_lookup_names\n"));
1463
1464         ZERO_STRUCT(q);
1465         ZERO_STRUCT(r);
1466
1467         /* Marshall data and send request */
1468
1469         init_samr_q_lookup_names(mem_ctx, &q, domain_pol, flags,
1470                                  num_names, names);
1471
1472         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_LOOKUP_NAMES,
1473                 q, r,
1474                 qbuf, rbuf,
1475                 samr_io_q_lookup_names,
1476                 samr_io_r_lookup_names,
1477                 NT_STATUS_UNSUCCESSFUL); 
1478
1479         /* Return output parameters */
1480
1481         if (!NT_STATUS_IS_OK(result = r.status)) {
1482                 goto done;
1483         }
1484
1485         if (r.num_rids1 == 0) {
1486                 *num_rids = 0;
1487                 goto done;
1488         }
1489
1490         *num_rids = r.num_rids1;
1491         *rids = TALLOC_ARRAY(mem_ctx, uint32, r.num_rids1);
1492         *rid_types = TALLOC_ARRAY(mem_ctx, uint32, r.num_rids1);
1493
1494         for (i = 0; i < r.num_rids1; i++) {
1495                 (*rids)[i] = r.rids[i];
1496                 (*rid_types)[i] = r.types[i];
1497         }
1498
1499  done:
1500
1501         return result;
1502 }
1503
1504 /* Create a domain user */
1505
1506 NTSTATUS rpccli_samr_create_dom_user(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
1507                                   POLICY_HND *domain_pol, const char *acct_name,
1508                                   uint32 acb_info, uint32 unknown, 
1509                                   POLICY_HND *user_pol, uint32 *rid)
1510 {
1511         prs_struct qbuf, rbuf;
1512         SAMR_Q_CREATE_USER q;
1513         SAMR_R_CREATE_USER r;
1514         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1515
1516         DEBUG(10,("cli_samr_create_dom_user %s\n", acct_name));
1517
1518         ZERO_STRUCT(q);
1519         ZERO_STRUCT(r);
1520
1521         /* Marshall data and send request */
1522
1523         init_samr_q_create_user(&q, domain_pol, acct_name, acb_info, unknown);
1524
1525         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CREATE_USER,
1526                 q, r,
1527                 qbuf, rbuf,
1528                 samr_io_q_create_user,
1529                 samr_io_r_create_user,
1530                 NT_STATUS_UNSUCCESSFUL); 
1531
1532         /* Return output parameters */
1533
1534         if (!NT_STATUS_IS_OK(result = r.status)) {
1535                 goto done;
1536         }
1537
1538         if (user_pol)
1539                 *user_pol = r.user_pol;
1540
1541         if (rid)
1542                 *rid = r.user_rid;
1543
1544  done:
1545
1546         return result;
1547 }
1548
1549 /* Set userinfo */
1550
1551 NTSTATUS rpccli_samr_set_userinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
1552                                POLICY_HND *user_pol, uint16 switch_value,
1553                                DATA_BLOB *sess_key, SAM_USERINFO_CTR *ctr)
1554 {
1555         prs_struct qbuf, rbuf;
1556         SAMR_Q_SET_USERINFO q;
1557         SAMR_R_SET_USERINFO r;
1558         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1559
1560         DEBUG(10,("cli_samr_set_userinfo\n"));
1561
1562         ZERO_STRUCT(q);
1563         ZERO_STRUCT(r);
1564
1565         if (!sess_key->length) {
1566                 DEBUG(1, ("No user session key\n"));
1567                 return NT_STATUS_NO_USER_SESSION_KEY;
1568         }
1569
1570         /* Initialise parse structures */
1571
1572         prs_init(&qbuf, RPC_MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1573         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1574
1575         /* Marshall data and send request */
1576
1577         q.ctr = ctr;
1578
1579         init_samr_q_set_userinfo(&q, user_pol, sess_key, switch_value, 
1580                                  ctr->info.id);
1581
1582         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_SET_USERINFO,
1583                 q, r,
1584                 qbuf, rbuf,
1585                 samr_io_q_set_userinfo,
1586                 samr_io_r_set_userinfo,
1587                 NT_STATUS_UNSUCCESSFUL); 
1588
1589         /* Return output parameters */
1590
1591         if (!NT_STATUS_IS_OK(result = r.status)) {
1592                 goto done;
1593         }
1594
1595  done:
1596
1597         return result;
1598 }
1599
1600 /* Set userinfo2 */
1601
1602 NTSTATUS rpccli_samr_set_userinfo2(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
1603                                 POLICY_HND *user_pol, uint16 switch_value,
1604                                 DATA_BLOB *sess_key, SAM_USERINFO_CTR *ctr)
1605 {
1606         prs_struct qbuf, rbuf;
1607         SAMR_Q_SET_USERINFO2 q;
1608         SAMR_R_SET_USERINFO2 r;
1609         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1610
1611         DEBUG(10,("cli_samr_set_userinfo2\n"));
1612
1613         if (!sess_key->length) {
1614                 DEBUG(1, ("No user session key\n"));
1615                 return NT_STATUS_NO_USER_SESSION_KEY;
1616         }
1617
1618         ZERO_STRUCT(q);
1619         ZERO_STRUCT(r);
1620
1621         /* Marshall data and send request */
1622
1623         init_samr_q_set_userinfo2(&q, user_pol, sess_key, switch_value, ctr);
1624
1625         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_SET_USERINFO2,
1626                 q, r,
1627                 qbuf, rbuf,
1628                 samr_io_q_set_userinfo2,
1629                 samr_io_r_set_userinfo2,
1630                 NT_STATUS_UNSUCCESSFUL); 
1631
1632         /* Return output parameters */
1633
1634         if (!NT_STATUS_IS_OK(result = r.status)) {
1635                 goto done;
1636         }
1637
1638  done:
1639
1640         return result;
1641 }
1642
1643 /* Delete domain group */
1644
1645 NTSTATUS rpccli_samr_delete_dom_group(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
1646                                   POLICY_HND *group_pol)
1647 {
1648         prs_struct qbuf, rbuf;
1649         SAMR_Q_DELETE_DOM_GROUP q;
1650         SAMR_R_DELETE_DOM_GROUP r;
1651         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1652
1653         DEBUG(10,("cli_samr_delete_dom_group\n"));
1654
1655         ZERO_STRUCT(q);
1656         ZERO_STRUCT(r);
1657
1658         /* Marshall data and send request */
1659
1660         init_samr_q_delete_dom_group(&q, group_pol);
1661
1662         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_DELETE_DOM_GROUP,
1663                 q, r,
1664                 qbuf, rbuf,
1665                 samr_io_q_delete_dom_group,
1666                 samr_io_r_delete_dom_group,
1667                 NT_STATUS_UNSUCCESSFUL); 
1668
1669         /* Return output parameters */
1670
1671         result = r.status;
1672
1673         return result;
1674 }
1675
1676 /* Delete domain alias */
1677
1678 NTSTATUS rpccli_samr_delete_dom_alias(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
1679                                   POLICY_HND *alias_pol)
1680 {
1681         prs_struct qbuf, rbuf;
1682         SAMR_Q_DELETE_DOM_ALIAS q;
1683         SAMR_R_DELETE_DOM_ALIAS r;
1684         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1685
1686         DEBUG(10,("cli_samr_delete_dom_alias\n"));
1687
1688         ZERO_STRUCT(q);
1689         ZERO_STRUCT(r);
1690
1691         /* Marshall data and send request */
1692
1693         init_samr_q_delete_dom_alias(&q, alias_pol);
1694
1695         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_DELETE_DOM_ALIAS,
1696                 q, r,
1697                 qbuf, rbuf,
1698                 samr_io_q_delete_dom_alias,
1699                 samr_io_r_delete_dom_alias,
1700                 NT_STATUS_UNSUCCESSFUL); 
1701
1702         /* Return output parameters */
1703
1704         result = r.status;
1705
1706         return result;
1707 }
1708
1709 /* Delete domain user */
1710
1711 NTSTATUS rpccli_samr_delete_dom_user(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
1712                                   POLICY_HND *user_pol)
1713 {
1714         prs_struct qbuf, rbuf;
1715         SAMR_Q_DELETE_DOM_USER q;
1716         SAMR_R_DELETE_DOM_USER r;
1717         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1718
1719         DEBUG(10,("cli_samr_delete_dom_user\n"));
1720
1721         ZERO_STRUCT(q);
1722         ZERO_STRUCT(r);
1723
1724         /* Marshall data and send request */
1725
1726         init_samr_q_delete_dom_user(&q, user_pol);
1727
1728         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_DELETE_DOM_USER,
1729                 q, r,
1730                 qbuf, rbuf,
1731                 samr_io_q_delete_dom_user,
1732                 samr_io_r_delete_dom_user,
1733                 NT_STATUS_UNSUCCESSFUL); 
1734
1735         /* Return output parameters */
1736
1737         result = r.status;
1738
1739         return result;
1740 }
1741
1742 /* Remove foreign SID */
1743
1744 NTSTATUS rpccli_samr_remove_sid_foreign_domain(struct rpc_pipe_client *cli, 
1745                                             TALLOC_CTX *mem_ctx, 
1746                                             POLICY_HND *user_pol,
1747                                             DOM_SID *sid)
1748 {
1749         prs_struct qbuf, rbuf;
1750         SAMR_Q_REMOVE_SID_FOREIGN_DOMAIN q;
1751         SAMR_R_REMOVE_SID_FOREIGN_DOMAIN r;
1752         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1753
1754         DEBUG(10,("cli_samr_remove_sid_foreign_domain\n"));
1755
1756         ZERO_STRUCT(q);
1757         ZERO_STRUCT(r);
1758
1759         /* Marshall data and send request */
1760
1761         init_samr_q_remove_sid_foreign_domain(&q, user_pol, sid);
1762
1763         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_REMOVE_SID_FOREIGN_DOMAIN,
1764                 q, r,
1765                 qbuf, rbuf,
1766                 samr_io_q_remove_sid_foreign_domain,
1767                 samr_io_r_remove_sid_foreign_domain,
1768                 NT_STATUS_UNSUCCESSFUL); 
1769
1770         /* Return output parameters */
1771
1772         result = r.status;
1773
1774         return result;
1775 }
1776
1777 /* Query user security object */
1778
1779 NTSTATUS rpccli_samr_query_sec_obj(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1780                                  POLICY_HND *user_pol, uint16 switch_value, 
1781                                  TALLOC_CTX *ctx, SEC_DESC_BUF **sec_desc_buf)
1782 {
1783         prs_struct qbuf, rbuf;
1784         SAMR_Q_QUERY_SEC_OBJ q;
1785         SAMR_R_QUERY_SEC_OBJ r;
1786         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1787
1788         DEBUG(10,("cli_samr_query_sec_obj\n"));
1789
1790         ZERO_STRUCT(q);
1791         ZERO_STRUCT(r);
1792
1793         /* Marshall data and send request */
1794
1795         init_samr_q_query_sec_obj(&q, user_pol, switch_value);
1796
1797         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_SEC_OBJECT,
1798                 q, r,
1799                 qbuf, rbuf,
1800                 samr_io_q_query_sec_obj,
1801                 samr_io_r_query_sec_obj,
1802                 NT_STATUS_UNSUCCESSFUL); 
1803
1804         /* Return output parameters */
1805
1806         result = r.status;
1807         *sec_desc_buf=dup_sec_desc_buf(ctx, r.buf);
1808
1809         return result;
1810 }
1811
1812 /* Get domain password info */
1813
1814 NTSTATUS rpccli_samr_get_dom_pwinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1815                                  uint16 *min_pwd_length, uint32 *password_properties)
1816 {
1817         prs_struct qbuf, rbuf;
1818         SAMR_Q_GET_DOM_PWINFO q;
1819         SAMR_R_GET_DOM_PWINFO r;
1820         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1821
1822         DEBUG(10,("cli_samr_get_dom_pwinfo\n"));
1823
1824         ZERO_STRUCT(q);
1825         ZERO_STRUCT(r);
1826
1827         /* Marshall data and send request */
1828
1829         init_samr_q_get_dom_pwinfo(&q, cli->cli->desthost);
1830
1831         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_GET_DOM_PWINFO,
1832                 q, r,
1833                 qbuf, rbuf,
1834                 samr_io_q_get_dom_pwinfo,
1835                 samr_io_r_get_dom_pwinfo,
1836                 NT_STATUS_UNSUCCESSFUL); 
1837
1838         /* Return output parameters */
1839
1840         result = r.status;
1841
1842         if (NT_STATUS_IS_OK(result)) {
1843                 if (min_pwd_length)
1844                         *min_pwd_length = r.min_pwd_length;
1845                 if (password_properties)
1846                         *password_properties = r.password_properties;
1847         }
1848
1849         return result;
1850 }
1851
1852 /* Lookup Domain Name */
1853
1854 NTSTATUS rpccli_samr_lookup_domain(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1855                                 POLICY_HND *user_pol, char *domain_name, 
1856                                 DOM_SID *sid)
1857 {
1858         prs_struct qbuf, rbuf;
1859         SAMR_Q_LOOKUP_DOMAIN q;
1860         SAMR_R_LOOKUP_DOMAIN r;
1861         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1862
1863         DEBUG(10,("cli_samr_lookup_domain\n"));
1864
1865         ZERO_STRUCT(q);
1866         ZERO_STRUCT(r);
1867
1868         /* Marshall data and send request */
1869
1870         init_samr_q_lookup_domain(&q, user_pol, domain_name);
1871
1872         CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_LOOKUP_DOMAIN,
1873                 q, r,
1874                 qbuf, rbuf,
1875                 samr_io_q_lookup_domain,
1876                 samr_io_r_lookup_domain,
1877                 NT_STATUS_UNSUCCESSFUL); 
1878
1879         /* Return output parameters */
1880
1881         result = r.status;
1882
1883         if (NT_STATUS_IS_OK(result))
1884                 sid_copy(sid, &r.dom_sid.sid);
1885
1886         return result;
1887 }