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