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