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