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