r15152: Fix a case when target is offline. Jerry, this needs to be in 3.0.23pre1
[Samba.git] / source / rpc_client / cli_samr.c
blob07b876cc22c4bc6a50c0b66e2c9adbf755703134
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.
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.
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.
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.
24 #include "includes.h"
26 /* Connect to SAMR database */
28 NTSTATUS rpccli_samr_connect(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
29 uint32 access_mask, POLICY_HND *connect_pol)
31 prs_struct qbuf, rbuf;
32 SAMR_Q_CONNECT q;
33 SAMR_R_CONNECT r;
34 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
36 DEBUG(10,("cli_samr_connect to %s\n", cli->cli->desthost));
38 ZERO_STRUCT(q);
39 ZERO_STRUCT(r);
41 /* Marshall data and send request */
43 init_samr_q_connect(&q, cli->cli->desthost, access_mask);
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 */
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
60 return result;
63 /* Connect to SAMR database */
65 NTSTATUS rpccli_samr_connect4(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
66 uint32 access_mask, POLICY_HND *connect_pol)
68 prs_struct qbuf, rbuf;
69 SAMR_Q_CONNECT4 q;
70 SAMR_R_CONNECT4 r;
71 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
73 ZERO_STRUCT(q);
74 ZERO_STRUCT(r);
76 /* Marshall data and send request */
78 init_samr_q_connect4(&q, cli->cli->desthost, access_mask);
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);
87 /* Return output parameters */
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
96 return result;
99 /* Close SAMR handle */
101 NTSTATUS rpccli_samr_close(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
102 POLICY_HND *connect_pol)
104 prs_struct qbuf, rbuf;
105 SAMR_Q_CLOSE_HND q;
106 SAMR_R_CLOSE_HND r;
107 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
109 DEBUG(10,("cli_samr_close\n"));
111 ZERO_STRUCT(q);
112 ZERO_STRUCT(r);
114 /* Marshall data and send request */
116 init_samr_q_close_hnd(&q, connect_pol);
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);
125 /* Return output parameters */
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;
134 return result;
137 /* Open handle on a domain */
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)
144 prs_struct qbuf, rbuf;
145 SAMR_Q_OPEN_DOMAIN q;
146 SAMR_R_OPEN_DOMAIN r;
147 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
149 DEBUG(10,("cli_samr_open_domain with sid %s\n", sid_string_static(domain_sid) ));
151 ZERO_STRUCT(q);
152 ZERO_STRUCT(r);
154 /* Marshall data and send request */
156 init_samr_q_open_domain(&q, connect_pol, access_mask, domain_sid);
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);
165 /* Return output parameters */
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
174 return result;
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)
182 prs_struct qbuf, rbuf;
183 SAMR_Q_OPEN_USER q;
184 SAMR_R_OPEN_USER r;
185 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
187 DEBUG(10,("cli_samr_open_user with rid 0x%x\n", user_rid ));
189 ZERO_STRUCT(q);
190 ZERO_STRUCT(r);
192 /* Marshall data and send request */
194 init_samr_q_open_user(&q, domain_pol, access_mask, user_rid);
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);
203 /* Return output parameters */
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
212 return result;
215 /* Open handle on a group */
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)
222 prs_struct qbuf, rbuf;
223 SAMR_Q_OPEN_GROUP q;
224 SAMR_R_OPEN_GROUP r;
225 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
227 DEBUG(10,("cli_samr_open_group with rid 0x%x\n", group_rid ));
229 ZERO_STRUCT(q);
230 ZERO_STRUCT(r);
232 /* Marshall data and send request */
234 init_samr_q_open_group(&q, domain_pol, access_mask, group_rid);
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);
243 /* Return output parameters */
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
252 return result;
255 /* Create domain group */
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)
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;
267 DEBUG(10,("cli_samr_create_dom_group\n"));
269 ZERO_STRUCT(q);
270 ZERO_STRUCT(r);
272 /* Marshall data and send request */
274 init_samr_q_create_dom_group(&q, domain_pol, group_name, access_mask);
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);
283 /* Return output parameters */
285 result = r.status;
287 if (NT_STATUS_IS_OK(result))
288 *group_pol = r.pol;
290 return result;
293 /* Add a domain group member */
295 NTSTATUS rpccli_samr_add_groupmem(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
296 POLICY_HND *group_pol, uint32 rid)
298 prs_struct qbuf, rbuf;
299 SAMR_Q_ADD_GROUPMEM q;
300 SAMR_R_ADD_GROUPMEM r;
301 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
303 DEBUG(10,("cli_samr_add_groupmem\n"));
305 ZERO_STRUCT(q);
306 ZERO_STRUCT(r);
308 /* Marshall data and send request */
310 init_samr_q_add_groupmem(&q, group_pol, rid);
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);
319 /* Return output parameters */
321 result = r.status;
323 return result;
326 /* Delete a domain group member */
328 NTSTATUS rpccli_samr_del_groupmem(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
329 POLICY_HND *group_pol, uint32 rid)
331 prs_struct qbuf, rbuf;
332 SAMR_Q_DEL_GROUPMEM q;
333 SAMR_R_DEL_GROUPMEM r;
334 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
336 DEBUG(10,("cli_samr_del_groupmem\n"));
338 ZERO_STRUCT(q);
339 ZERO_STRUCT(r);
341 /* Marshall data and send request */
343 init_samr_q_del_groupmem(&q, group_pol, rid);
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);
352 /* Return output parameters */
354 result = r.status;
356 return result;
359 /* Query user info */
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)
367 prs_struct qbuf, rbuf;
368 SAMR_Q_QUERY_USERINFO q;
369 SAMR_R_QUERY_USERINFO r;
370 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
372 DEBUG(10,("cli_samr_query_userinfo\n"));
374 ZERO_STRUCT(q);
375 ZERO_STRUCT(r);
377 /* Marshall data and send request */
379 init_samr_q_query_userinfo(&q, user_pol, switch_value);
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);
388 /* Return output parameters */
390 result = r.status;
391 *ctr = r.ctr;
393 return result;
396 /* Set group info */
398 NTSTATUS rpccli_samr_set_groupinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
399 POLICY_HND *group_pol, GROUP_INFO_CTR *ctr)
401 prs_struct qbuf, rbuf;
402 SAMR_Q_SET_GROUPINFO q;
403 SAMR_R_SET_GROUPINFO r;
404 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
406 DEBUG(10,("cli_samr_set_groupinfo\n"));
408 ZERO_STRUCT(q);
409 ZERO_STRUCT(r);
411 /* Marshall data and send request */
413 init_samr_q_set_groupinfo(&q, group_pol, ctr);
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);
422 /* Return output parameters */
424 result = r.status;
426 return result;
429 /* Query group info */
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)
435 prs_struct qbuf, rbuf;
436 SAMR_Q_QUERY_GROUPINFO q;
437 SAMR_R_QUERY_GROUPINFO r;
438 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
440 DEBUG(10,("cli_samr_query_groupinfo\n"));
442 ZERO_STRUCT(q);
443 ZERO_STRUCT(r);
445 /* Marshall data and send request */
447 init_samr_q_query_groupinfo(&q, group_pol, info_level);
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);
456 *ctr = r.ctr;
458 /* Return output parameters */
460 result = r.status;
462 return result;
465 /* Query user groups */
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)
473 prs_struct qbuf, rbuf;
474 SAMR_Q_QUERY_USERGROUPS q;
475 SAMR_R_QUERY_USERGROUPS r;
476 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
478 DEBUG(10,("cli_samr_query_usergroups\n"));
480 ZERO_STRUCT(q);
481 ZERO_STRUCT(r);
483 /* Marshall data and send request */
485 init_samr_q_query_usergroups(&q, user_pol);
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);
494 /* Return output parameters */
496 if (NT_STATUS_IS_OK(result = r.status)) {
497 *num_groups = r.num_entries;
498 *gid = r.gid;
501 return result;
504 /* Set alias info */
506 NTSTATUS rpccli_samr_set_aliasinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
507 POLICY_HND *alias_pol, ALIAS_INFO_CTR *ctr)
509 prs_struct qbuf, rbuf;
510 SAMR_Q_SET_ALIASINFO q;
511 SAMR_R_SET_ALIASINFO r;
512 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
514 DEBUG(10,("cli_samr_set_aliasinfo\n"));
516 ZERO_STRUCT(q);
517 ZERO_STRUCT(r);
519 /* Marshall data and send request */
521 init_samr_q_set_aliasinfo(&q, alias_pol, ctr);
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);
530 /* Return output parameters */
532 result = r.status;
534 return result;
537 /* Query user aliases */
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)
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;
552 DEBUG(10,("cli_samr_query_useraliases\n"));
554 ZERO_STRUCT(q);
555 ZERO_STRUCT(r);
557 sid_ptrs = TALLOC_ARRAY(mem_ctx, uint32, num_sids);
558 if (sid_ptrs == NULL)
559 return NT_STATUS_NO_MEMORY;
561 for (i=0; i<num_sids; i++)
562 sid_ptrs[i] = 1;
564 /* Marshall data and send request */
566 init_samr_q_query_useraliases(&q, dom_pol, num_sids, sid_ptrs, sid);
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);
575 /* Return output parameters */
577 if (NT_STATUS_IS_OK(result = r.status)) {
578 *num_aliases = r.num_entries;
579 *als_rids = r.rid;
582 return result;
585 /* Query user groups */
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)
592 prs_struct qbuf, rbuf;
593 SAMR_Q_QUERY_GROUPMEM q;
594 SAMR_R_QUERY_GROUPMEM r;
595 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
597 DEBUG(10,("cli_samr_query_groupmem\n"));
599 ZERO_STRUCT(q);
600 ZERO_STRUCT(r);
602 /* Marshall data and send request */
604 init_samr_q_query_groupmem(&q, group_pol);
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);
613 /* Return output parameters */
615 if (NT_STATUS_IS_OK(result = r.status)) {
616 *num_mem = r.num_entries;
617 *rid = r.rid;
618 *attr = r.attr;
621 return result;
625 * Enumerate domain users
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
639 * @return NTSTATUS returned in rpc response
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)
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;
654 DEBUG(10,("cli_samr_enum_dom_users starting at index %u\n", (unsigned int)*start_idx));
656 ZERO_STRUCT(q);
657 ZERO_STRUCT(r);
659 /* always init this */
660 *num_dom_users = 0;
662 /* Fill query structure with parameters */
664 init_samr_q_enum_dom_users(&q, pol, *start_idx, acb_mask, size);
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);
673 result = r.status;
675 if (!NT_STATUS_IS_OK(result) &&
676 NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES))
677 goto done;
679 *start_idx = r.next_idx;
680 *num_dom_users = r.num_entries2;
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;
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;
696 /* fill output buffers with rpc response */
697 for (i = 0; i < r.num_entries2; i++) {
698 fstring conv_buf;
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);
706 done:
707 return result;
710 /* Enumerate domain groups */
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)
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;
724 DEBUG(10,("cli_samr_enum_dom_groups starting at index %u\n", (unsigned int)*start_idx));
726 ZERO_STRUCT(q);
727 ZERO_STRUCT(r);
729 /* Marshall data and send request */
731 init_samr_q_enum_dom_groups(&q, pol, *start_idx, size);
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);
740 /* Return output parameters */
742 result = r.status;
744 if (!NT_STATUS_IS_OK(result) &&
745 NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES))
746 goto done;
748 *num_dom_groups = r.num_entries2;
750 if (*num_dom_groups == 0)
751 goto done;
753 if (!((*dom_groups) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_dom_groups))) {
754 result = NT_STATUS_NO_MEMORY;
755 goto done;
758 memset(*dom_groups, 0, sizeof(struct acct_info) * (*num_dom_groups));
760 name_idx = 0;
762 for (i = 0; i < *num_dom_groups; i++) {
764 (*dom_groups)[i].rid = r.sam[i].rid;
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++;
773 *start_idx = r.next_idx;
776 done:
777 return result;
780 /* Enumerate domain groups */
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)
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;
794 DEBUG(10,("cli_samr_enum_als_groups starting at index %u\n", (unsigned int)*start_idx));
796 ZERO_STRUCT(q);
797 ZERO_STRUCT(r);
799 /* Marshall data and send request */
801 init_samr_q_enum_dom_aliases(&q, pol, *start_idx, size);
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);
810 /* Return output parameters */
812 result = r.status;
814 if (!NT_STATUS_IS_OK(result) &&
815 NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES)) {
816 goto done;
819 *num_dom_aliases = r.num_entries2;
821 if (*num_dom_aliases == 0)
822 goto done;
824 if (!((*dom_aliases) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_dom_aliases))) {
825 result = NT_STATUS_NO_MEMORY;
826 goto done;
829 memset(*dom_aliases, 0, sizeof(struct acct_info) * *num_dom_aliases);
831 name_idx = 0;
833 for (i = 0; i < *num_dom_aliases; i++) {
835 (*dom_aliases)[i].rid = r.sam[i].rid;
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++;
844 *start_idx = r.next_idx;
847 done:
848 return result;
851 /* Query alias members */
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)
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;
864 DEBUG(10,("cli_samr_query_aliasmem\n"));
866 ZERO_STRUCT(q);
867 ZERO_STRUCT(r);
869 /* Marshall data and send request */
871 init_samr_q_query_aliasmem(&q, alias_pol);
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);
880 /* Return output parameters */
882 if (!NT_STATUS_IS_OK(result = r.status)) {
883 goto done;
886 *num_mem = r.num_sids;
888 if (*num_mem == 0) {
889 *sids = NULL;
890 result = NT_STATUS_OK;
891 goto done;
894 if (!(*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *num_mem))) {
895 result = NT_STATUS_UNSUCCESSFUL;
896 goto done;
899 for (i = 0; i < *num_mem; i++) {
900 (*sids)[i] = r.sid[i].sid;
903 done:
904 return result;
907 /* Open handle on an alias */
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)
914 prs_struct qbuf, rbuf;
915 SAMR_Q_OPEN_ALIAS q;
916 SAMR_R_OPEN_ALIAS r;
917 NTSTATUS result;
919 DEBUG(10,("cli_samr_open_alias with rid 0x%x\n", alias_rid));
921 ZERO_STRUCT(q);
922 ZERO_STRUCT(r);
924 /* Marshall data and send request */
926 init_samr_q_open_alias(&q, domain_pol, access_mask, alias_rid);
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);
935 /* Return output parameters */
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
944 return result;
947 /* Create an alias */
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)
953 prs_struct qbuf, rbuf;
954 SAMR_Q_CREATE_DOM_ALIAS q;
955 SAMR_R_CREATE_DOM_ALIAS r;
956 NTSTATUS result;
958 DEBUG(10,("cli_samr_create_dom_alias named %s\n", name));
960 ZERO_STRUCT(q);
961 ZERO_STRUCT(r);
963 /* Marshall data and send request */
965 init_samr_q_create_dom_alias(&q, domain_pol, name);
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);
974 /* Return output parameters */
976 if (NT_STATUS_IS_OK(result = r.status)) {
977 *alias_pol = r.alias_pol;
980 return result;
983 /* Add an alias member */
985 NTSTATUS rpccli_samr_add_aliasmem(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
986 POLICY_HND *alias_pol, DOM_SID *member)
988 prs_struct qbuf, rbuf;
989 SAMR_Q_ADD_ALIASMEM q;
990 SAMR_R_ADD_ALIASMEM r;
991 NTSTATUS result;
993 DEBUG(10,("cli_samr_add_aliasmem"));
995 ZERO_STRUCT(q);
996 ZERO_STRUCT(r);
998 /* Marshall data and send request */
1000 init_samr_q_add_aliasmem(&q, alias_pol, member);
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);
1009 result = r.status;
1011 return result;
1014 /* Delete an alias member */
1016 NTSTATUS rpccli_samr_del_aliasmem(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1017 POLICY_HND *alias_pol, DOM_SID *member)
1019 prs_struct qbuf, rbuf;
1020 SAMR_Q_DEL_ALIASMEM q;
1021 SAMR_R_DEL_ALIASMEM r;
1022 NTSTATUS result;
1024 DEBUG(10,("cli_samr_del_aliasmem"));
1026 ZERO_STRUCT(q);
1027 ZERO_STRUCT(r);
1029 /* Marshall data and send request */
1031 init_samr_q_del_aliasmem(&q, alias_pol, member);
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);
1040 result = r.status;
1042 return result;
1045 /* Query alias info */
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)
1051 prs_struct qbuf, rbuf;
1052 SAMR_Q_QUERY_ALIASINFO q;
1053 SAMR_R_QUERY_ALIASINFO r;
1054 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1056 DEBUG(10,("cli_samr_query_alias_info\n"));
1058 ZERO_STRUCT(q);
1059 ZERO_STRUCT(r);
1061 /* Marshall data and send request */
1063 init_samr_q_query_aliasinfo(&q, alias_pol, switch_value);
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);
1072 /* Return output parameters */
1074 if (!NT_STATUS_IS_OK(result = r.status)) {
1075 goto done;
1078 *ctr = *r.ctr;
1080 done:
1082 return result;
1085 /* Query domain info */
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)
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;
1098 DEBUG(10,("cli_samr_query_dom_info\n"));
1100 ZERO_STRUCT(q);
1101 ZERO_STRUCT(r);
1103 /* Marshall data and send request */
1105 init_samr_q_query_dom_info(&q, domain_pol, switch_value);
1107 r.ctr = ctr;
1109 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_DOMAIN_INFO,
1110 q, r,
1111 qbuf, rbuf,
1112 samr_io_q_query_dom_info,
1113 samr_io_r_query_dom_info,
1114 NT_STATUS_UNSUCCESSFUL);
1116 /* Return output parameters */
1118 if (!NT_STATUS_IS_OK(result = r.status)) {
1119 goto done;
1122 done:
1124 return result;
1127 /* Set domain info */
1129 NTSTATUS rpccli_samr_set_domain_info(struct rpc_pipe_client *cli,
1130 TALLOC_CTX *mem_ctx,
1131 POLICY_HND *domain_pol,
1132 uint16 switch_value,
1133 SAM_UNK_CTR *ctr)
1135 prs_struct qbuf, rbuf;
1136 SAMR_Q_SET_DOMAIN_INFO q;
1137 SAMR_R_SET_DOMAIN_INFO r;
1138 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1140 DEBUG(10,("cli_samr_set_domain_info\n"));
1142 ZERO_STRUCT(q);
1143 ZERO_STRUCT(r);
1145 /* Marshall data and send request */
1147 init_samr_q_set_domain_info(&q, domain_pol, switch_value, ctr);
1149 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_SET_DOMAIN_INFO,
1150 q, r,
1151 qbuf, rbuf,
1152 samr_io_q_set_domain_info,
1153 samr_io_r_set_domain_info,
1154 NT_STATUS_UNSUCCESSFUL);
1156 /* Return output parameters */
1158 if (!NT_STATUS_IS_OK(result = r.status)) {
1159 goto done;
1162 done:
1164 return result;
1167 /* User change password */
1169 NTSTATUS rpccli_samr_chgpasswd_user(struct rpc_pipe_client *cli,
1170 TALLOC_CTX *mem_ctx,
1171 const char *username,
1172 const char *newpassword,
1173 const char *oldpassword )
1175 prs_struct qbuf, rbuf;
1176 SAMR_Q_CHGPASSWD_USER q;
1177 SAMR_R_CHGPASSWD_USER r;
1178 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1180 uchar new_nt_password[516];
1181 uchar new_lm_password[516];
1182 uchar old_nt_hash[16];
1183 uchar old_lanman_hash[16];
1184 uchar old_nt_hash_enc[16];
1185 uchar old_lanman_hash_enc[16];
1187 uchar new_nt_hash[16];
1188 uchar new_lanman_hash[16];
1190 char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
1192 DEBUG(10,("rpccli_samr_chgpasswd_user\n"));
1194 ZERO_STRUCT(q);
1195 ZERO_STRUCT(r);
1197 /* Calculate the MD4 hash (NT compatible) of the password */
1198 E_md4hash(oldpassword, old_nt_hash);
1199 E_md4hash(newpassword, new_nt_hash);
1201 if (lp_client_lanman_auth()
1202 && E_deshash(newpassword, new_lanman_hash)
1203 && E_deshash(oldpassword, old_lanman_hash)) {
1204 /* E_deshash returns false for 'long' passwords (> 14
1205 DOS chars). This allows us to match Win2k, which
1206 does not store a LM hash for these passwords (which
1207 would reduce the effective password length to 14) */
1209 encode_pw_buffer(new_lm_password, newpassword, STR_UNICODE);
1211 SamOEMhash( new_lm_password, old_nt_hash, 516);
1212 E_old_pw_hash( new_nt_hash, old_lanman_hash, old_lanman_hash_enc);
1213 } else {
1214 ZERO_STRUCT(new_lm_password);
1215 ZERO_STRUCT(old_lanman_hash_enc);
1218 encode_pw_buffer(new_nt_password, newpassword, STR_UNICODE);
1220 SamOEMhash( new_nt_password, old_nt_hash, 516);
1221 E_old_pw_hash( new_nt_hash, old_nt_hash, old_nt_hash_enc);
1223 /* Marshall data and send request */
1225 init_samr_q_chgpasswd_user(&q, srv_name_slash, username,
1226 new_nt_password,
1227 old_nt_hash_enc,
1228 new_lm_password,
1229 old_lanman_hash_enc);
1231 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CHGPASSWD_USER,
1232 q, r,
1233 qbuf, rbuf,
1234 samr_io_q_chgpasswd_user,
1235 samr_io_r_chgpasswd_user,
1236 NT_STATUS_UNSUCCESSFUL);
1238 /* Return output parameters */
1240 if (!NT_STATUS_IS_OK(result = r.status)) {
1241 goto done;
1244 done:
1246 return result;
1249 /* change password 3 */
1251 NTSTATUS rpccli_samr_chgpasswd3(struct rpc_pipe_client *cli,
1252 TALLOC_CTX *mem_ctx,
1253 const char *username,
1254 const char *newpassword,
1255 const char *oldpassword,
1256 SAM_UNK_INFO_1 *info,
1257 SAMR_CHANGE_REJECT *reject)
1259 prs_struct qbuf, rbuf;
1260 SAMR_Q_CHGPASSWD_USER3 q;
1261 SAMR_R_CHGPASSWD_USER3 r;
1263 uchar new_nt_password[516];
1264 uchar new_lm_password[516];
1265 uchar old_nt_hash[16];
1266 uchar old_lanman_hash[16];
1267 uchar old_nt_hash_enc[16];
1268 uchar old_lanman_hash_enc[16];
1270 uchar new_nt_hash[16];
1271 uchar new_lanman_hash[16];
1273 char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
1275 DEBUG(10,("rpccli_samr_chgpasswd_user3\n"));
1277 ZERO_STRUCT(q);
1278 ZERO_STRUCT(r);
1280 /* Calculate the MD4 hash (NT compatible) of the password */
1281 E_md4hash(oldpassword, old_nt_hash);
1282 E_md4hash(newpassword, new_nt_hash);
1284 if (lp_client_lanman_auth()
1285 && E_deshash(newpassword, new_lanman_hash)
1286 && E_deshash(oldpassword, old_lanman_hash)) {
1287 /* E_deshash returns false for 'long' passwords (> 14
1288 DOS chars). This allows us to match Win2k, which
1289 does not store a LM hash for these passwords (which
1290 would reduce the effective password length to 14) */
1292 encode_pw_buffer(new_lm_password, newpassword, STR_UNICODE);
1294 SamOEMhash( new_lm_password, old_nt_hash, 516);
1295 E_old_pw_hash( new_nt_hash, old_lanman_hash, old_lanman_hash_enc);
1296 } else {
1297 ZERO_STRUCT(new_lm_password);
1298 ZERO_STRUCT(old_lanman_hash_enc);
1301 encode_pw_buffer(new_nt_password, newpassword, STR_UNICODE);
1303 SamOEMhash( new_nt_password, old_nt_hash, 516);
1304 E_old_pw_hash( new_nt_hash, old_nt_hash, old_nt_hash_enc);
1306 /* Marshall data and send request */
1308 init_samr_q_chgpasswd_user3(&q, srv_name_slash, username,
1309 new_nt_password,
1310 old_nt_hash_enc,
1311 new_lm_password,
1312 old_lanman_hash_enc);
1313 r.info = info;
1314 r.reject = reject;
1316 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CHGPASSWD_USER3,
1317 q, r,
1318 qbuf, rbuf,
1319 samr_io_q_chgpasswd_user3,
1320 samr_io_r_chgpasswd_user3,
1321 NT_STATUS_UNSUCCESSFUL);
1323 /* Return output parameters */
1325 return r.status;
1328 /* This function returns the bizzare set of (max_entries, max_size) required
1329 for the QueryDisplayInfo RPC to actually work against a domain controller
1330 with large (10k and higher) numbers of users. These values were
1331 obtained by inspection using ethereal and NT4 running User Manager. */
1333 void get_query_dispinfo_params(int loop_count, uint32 *max_entries,
1334 uint32 *max_size)
1336 switch(loop_count) {
1337 case 0:
1338 *max_entries = 512;
1339 *max_size = 16383;
1340 break;
1341 case 1:
1342 *max_entries = 1024;
1343 *max_size = 32766;
1344 break;
1345 case 2:
1346 *max_entries = 2048;
1347 *max_size = 65532;
1348 break;
1349 case 3:
1350 *max_entries = 4096;
1351 *max_size = 131064;
1352 break;
1353 default: /* loop_count >= 4 */
1354 *max_entries = 4096;
1355 *max_size = 131071;
1356 break;
1360 /* Query display info */
1362 NTSTATUS rpccli_samr_query_dispinfo(struct rpc_pipe_client *cli,
1363 TALLOC_CTX *mem_ctx,
1364 POLICY_HND *domain_pol, uint32 *start_idx,
1365 uint16 switch_value, uint32 *num_entries,
1366 uint32 max_entries, uint32 max_size,
1367 SAM_DISPINFO_CTR *ctr)
1369 prs_struct qbuf, rbuf;
1370 SAMR_Q_QUERY_DISPINFO q;
1371 SAMR_R_QUERY_DISPINFO r;
1372 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1374 DEBUG(10,("cli_samr_query_dispinfo for start_idx = %u\n", *start_idx));
1376 ZERO_STRUCT(q);
1377 ZERO_STRUCT(r);
1379 *num_entries = 0;
1381 /* Marshall data and send request */
1383 init_samr_q_query_dispinfo(&q, domain_pol, switch_value,
1384 *start_idx, max_entries, max_size);
1386 r.ctr = ctr;
1388 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_DISPINFO,
1389 q, r,
1390 qbuf, rbuf,
1391 samr_io_q_query_dispinfo,
1392 samr_io_r_query_dispinfo,
1393 NT_STATUS_UNSUCCESSFUL);
1395 /* Return output parameters */
1397 result = r.status;
1399 if (!NT_STATUS_IS_OK(result) &&
1400 NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1401 goto done;
1404 *num_entries = r.num_entries;
1405 *start_idx += r.num_entries; /* No next_idx in this structure! */
1407 done:
1408 return result;
1411 /* Lookup rids. Note that NT4 seems to crash if more than ~1000 rids are
1412 looked up in one packet. */
1414 NTSTATUS rpccli_samr_lookup_rids(struct rpc_pipe_client *cli,
1415 TALLOC_CTX *mem_ctx,
1416 POLICY_HND *domain_pol,
1417 uint32 num_rids, uint32 *rids,
1418 uint32 *num_names, char ***names,
1419 uint32 **name_types)
1421 prs_struct qbuf, rbuf;
1422 SAMR_Q_LOOKUP_RIDS q;
1423 SAMR_R_LOOKUP_RIDS r;
1424 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1425 uint32 i;
1427 DEBUG(10,("cli_samr_lookup_rids\n"));
1429 if (num_rids > 1000) {
1430 DEBUG(2, ("cli_samr_lookup_rids: warning: NT4 can crash if "
1431 "more than ~1000 rids are looked up at once.\n"));
1434 ZERO_STRUCT(q);
1435 ZERO_STRUCT(r);
1437 /* Marshall data and send request */
1439 init_samr_q_lookup_rids(mem_ctx, &q, domain_pol, 1000, num_rids, rids);
1441 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_LOOKUP_RIDS,
1442 q, r,
1443 qbuf, rbuf,
1444 samr_io_q_lookup_rids,
1445 samr_io_r_lookup_rids,
1446 NT_STATUS_UNSUCCESSFUL);
1448 /* Return output parameters */
1450 result = r.status;
1452 if (!NT_STATUS_IS_OK(result) &&
1453 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
1454 goto done;
1456 if (r.num_names1 == 0) {
1457 *num_names = 0;
1458 *names = NULL;
1459 goto done;
1462 *num_names = r.num_names1;
1463 *names = TALLOC_ARRAY(mem_ctx, char *, r.num_names1);
1464 *name_types = TALLOC_ARRAY(mem_ctx, uint32, r.num_names1);
1466 for (i = 0; i < r.num_names1; i++) {
1467 fstring tmp;
1469 unistr2_to_ascii(tmp, &r.uni_name[i], sizeof(tmp) - 1);
1470 (*names)[i] = talloc_strdup(mem_ctx, tmp);
1471 (*name_types)[i] = r.type[i];
1474 done:
1476 return result;
1479 /* Lookup names */
1481 NTSTATUS rpccli_samr_lookup_names(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1482 POLICY_HND *domain_pol, uint32 flags,
1483 uint32 num_names, const char **names,
1484 uint32 *num_rids, uint32 **rids,
1485 uint32 **rid_types)
1487 prs_struct qbuf, rbuf;
1488 SAMR_Q_LOOKUP_NAMES q;
1489 SAMR_R_LOOKUP_NAMES r;
1490 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1491 uint32 i;
1493 DEBUG(10,("cli_samr_lookup_names\n"));
1495 ZERO_STRUCT(q);
1496 ZERO_STRUCT(r);
1498 /* Marshall data and send request */
1500 init_samr_q_lookup_names(mem_ctx, &q, domain_pol, flags,
1501 num_names, names);
1503 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_LOOKUP_NAMES,
1504 q, r,
1505 qbuf, rbuf,
1506 samr_io_q_lookup_names,
1507 samr_io_r_lookup_names,
1508 NT_STATUS_UNSUCCESSFUL);
1510 /* Return output parameters */
1512 if (!NT_STATUS_IS_OK(result = r.status)) {
1513 goto done;
1516 if (r.num_rids1 == 0) {
1517 *num_rids = 0;
1518 goto done;
1521 *num_rids = r.num_rids1;
1522 *rids = TALLOC_ARRAY(mem_ctx, uint32, r.num_rids1);
1523 *rid_types = TALLOC_ARRAY(mem_ctx, uint32, r.num_rids1);
1525 for (i = 0; i < r.num_rids1; i++) {
1526 (*rids)[i] = r.rids[i];
1527 (*rid_types)[i] = r.types[i];
1530 done:
1532 return result;
1535 /* Create a domain user */
1537 NTSTATUS rpccli_samr_create_dom_user(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1538 POLICY_HND *domain_pol, const char *acct_name,
1539 uint32 acb_info, uint32 unknown,
1540 POLICY_HND *user_pol, uint32 *rid)
1542 prs_struct qbuf, rbuf;
1543 SAMR_Q_CREATE_USER q;
1544 SAMR_R_CREATE_USER r;
1545 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1547 DEBUG(10,("cli_samr_create_dom_user %s\n", acct_name));
1549 ZERO_STRUCT(q);
1550 ZERO_STRUCT(r);
1552 /* Marshall data and send request */
1554 init_samr_q_create_user(&q, domain_pol, acct_name, acb_info, unknown);
1556 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CREATE_USER,
1557 q, r,
1558 qbuf, rbuf,
1559 samr_io_q_create_user,
1560 samr_io_r_create_user,
1561 NT_STATUS_UNSUCCESSFUL);
1563 /* Return output parameters */
1565 if (!NT_STATUS_IS_OK(result = r.status)) {
1566 goto done;
1569 if (user_pol)
1570 *user_pol = r.user_pol;
1572 if (rid)
1573 *rid = r.user_rid;
1575 done:
1577 return result;
1580 /* Set userinfo */
1582 NTSTATUS rpccli_samr_set_userinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1583 const POLICY_HND *user_pol, uint16 switch_value,
1584 DATA_BLOB *sess_key, SAM_USERINFO_CTR *ctr)
1586 prs_struct qbuf, rbuf;
1587 SAMR_Q_SET_USERINFO q;
1588 SAMR_R_SET_USERINFO r;
1589 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1591 DEBUG(10,("cli_samr_set_userinfo\n"));
1593 ZERO_STRUCT(q);
1594 ZERO_STRUCT(r);
1596 if (!sess_key->length) {
1597 DEBUG(1, ("No user session key\n"));
1598 return NT_STATUS_NO_USER_SESSION_KEY;
1601 /* Initialise parse structures */
1603 prs_init(&qbuf, RPC_MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1604 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1606 /* Marshall data and send request */
1608 q.ctr = ctr;
1610 init_samr_q_set_userinfo(&q, user_pol, sess_key, switch_value,
1611 ctr->info.id);
1613 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_SET_USERINFO,
1614 q, r,
1615 qbuf, rbuf,
1616 samr_io_q_set_userinfo,
1617 samr_io_r_set_userinfo,
1618 NT_STATUS_UNSUCCESSFUL);
1620 /* Return output parameters */
1622 if (!NT_STATUS_IS_OK(result = r.status)) {
1623 goto done;
1626 done:
1628 return result;
1631 /* Set userinfo2 */
1633 NTSTATUS rpccli_samr_set_userinfo2(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1634 const POLICY_HND *user_pol, uint16 switch_value,
1635 DATA_BLOB *sess_key, SAM_USERINFO_CTR *ctr)
1637 prs_struct qbuf, rbuf;
1638 SAMR_Q_SET_USERINFO2 q;
1639 SAMR_R_SET_USERINFO2 r;
1640 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1642 DEBUG(10,("cli_samr_set_userinfo2\n"));
1644 if (!sess_key->length) {
1645 DEBUG(1, ("No user session key\n"));
1646 return NT_STATUS_NO_USER_SESSION_KEY;
1649 ZERO_STRUCT(q);
1650 ZERO_STRUCT(r);
1652 /* Marshall data and send request */
1654 init_samr_q_set_userinfo2(&q, user_pol, sess_key, switch_value, ctr);
1656 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_SET_USERINFO2,
1657 q, r,
1658 qbuf, rbuf,
1659 samr_io_q_set_userinfo2,
1660 samr_io_r_set_userinfo2,
1661 NT_STATUS_UNSUCCESSFUL);
1663 /* Return output parameters */
1665 if (!NT_STATUS_IS_OK(result = r.status)) {
1666 goto done;
1669 done:
1671 return result;
1674 /* Delete domain group */
1676 NTSTATUS rpccli_samr_delete_dom_group(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1677 POLICY_HND *group_pol)
1679 prs_struct qbuf, rbuf;
1680 SAMR_Q_DELETE_DOM_GROUP q;
1681 SAMR_R_DELETE_DOM_GROUP r;
1682 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1684 DEBUG(10,("cli_samr_delete_dom_group\n"));
1686 ZERO_STRUCT(q);
1687 ZERO_STRUCT(r);
1689 /* Marshall data and send request */
1691 init_samr_q_delete_dom_group(&q, group_pol);
1693 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_DELETE_DOM_GROUP,
1694 q, r,
1695 qbuf, rbuf,
1696 samr_io_q_delete_dom_group,
1697 samr_io_r_delete_dom_group,
1698 NT_STATUS_UNSUCCESSFUL);
1700 /* Return output parameters */
1702 result = r.status;
1704 return result;
1707 /* Delete domain alias */
1709 NTSTATUS rpccli_samr_delete_dom_alias(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1710 POLICY_HND *alias_pol)
1712 prs_struct qbuf, rbuf;
1713 SAMR_Q_DELETE_DOM_ALIAS q;
1714 SAMR_R_DELETE_DOM_ALIAS r;
1715 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1717 DEBUG(10,("cli_samr_delete_dom_alias\n"));
1719 ZERO_STRUCT(q);
1720 ZERO_STRUCT(r);
1722 /* Marshall data and send request */
1724 init_samr_q_delete_dom_alias(&q, alias_pol);
1726 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_DELETE_DOM_ALIAS,
1727 q, r,
1728 qbuf, rbuf,
1729 samr_io_q_delete_dom_alias,
1730 samr_io_r_delete_dom_alias,
1731 NT_STATUS_UNSUCCESSFUL);
1733 /* Return output parameters */
1735 result = r.status;
1737 return result;
1740 /* Delete domain user */
1742 NTSTATUS rpccli_samr_delete_dom_user(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1743 POLICY_HND *user_pol)
1745 prs_struct qbuf, rbuf;
1746 SAMR_Q_DELETE_DOM_USER q;
1747 SAMR_R_DELETE_DOM_USER r;
1748 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1750 DEBUG(10,("cli_samr_delete_dom_user\n"));
1752 ZERO_STRUCT(q);
1753 ZERO_STRUCT(r);
1755 /* Marshall data and send request */
1757 init_samr_q_delete_dom_user(&q, user_pol);
1759 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_DELETE_DOM_USER,
1760 q, r,
1761 qbuf, rbuf,
1762 samr_io_q_delete_dom_user,
1763 samr_io_r_delete_dom_user,
1764 NT_STATUS_UNSUCCESSFUL);
1766 /* Return output parameters */
1768 result = r.status;
1770 return result;
1773 /* Remove foreign SID */
1775 NTSTATUS rpccli_samr_remove_sid_foreign_domain(struct rpc_pipe_client *cli,
1776 TALLOC_CTX *mem_ctx,
1777 POLICY_HND *user_pol,
1778 DOM_SID *sid)
1780 prs_struct qbuf, rbuf;
1781 SAMR_Q_REMOVE_SID_FOREIGN_DOMAIN q;
1782 SAMR_R_REMOVE_SID_FOREIGN_DOMAIN r;
1783 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1785 DEBUG(10,("cli_samr_remove_sid_foreign_domain\n"));
1787 ZERO_STRUCT(q);
1788 ZERO_STRUCT(r);
1790 /* Marshall data and send request */
1792 init_samr_q_remove_sid_foreign_domain(&q, user_pol, sid);
1794 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_REMOVE_SID_FOREIGN_DOMAIN,
1795 q, r,
1796 qbuf, rbuf,
1797 samr_io_q_remove_sid_foreign_domain,
1798 samr_io_r_remove_sid_foreign_domain,
1799 NT_STATUS_UNSUCCESSFUL);
1801 /* Return output parameters */
1803 result = r.status;
1805 return result;
1808 /* Query user security object */
1810 NTSTATUS rpccli_samr_query_sec_obj(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1811 POLICY_HND *user_pol, uint32 sec_info,
1812 TALLOC_CTX *ctx, SEC_DESC_BUF **sec_desc_buf)
1814 prs_struct qbuf, rbuf;
1815 SAMR_Q_QUERY_SEC_OBJ q;
1816 SAMR_R_QUERY_SEC_OBJ r;
1817 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1819 DEBUG(10,("cli_samr_query_sec_obj\n"));
1821 ZERO_STRUCT(q);
1822 ZERO_STRUCT(r);
1824 /* Marshall data and send request */
1826 init_samr_q_query_sec_obj(&q, user_pol, sec_info);
1828 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_SEC_OBJECT,
1829 q, r,
1830 qbuf, rbuf,
1831 samr_io_q_query_sec_obj,
1832 samr_io_r_query_sec_obj,
1833 NT_STATUS_UNSUCCESSFUL);
1835 /* Return output parameters */
1837 result = r.status;
1838 *sec_desc_buf=dup_sec_desc_buf(ctx, r.buf);
1840 return result;
1843 /* Set user security object */
1845 NTSTATUS rpccli_samr_set_sec_obj(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1846 POLICY_HND *user_pol, uint32 sec_info,
1847 SEC_DESC_BUF *sec_desc_buf)
1849 prs_struct qbuf, rbuf;
1850 SAMR_Q_SET_SEC_OBJ q;
1851 SAMR_R_SET_SEC_OBJ r;
1852 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1854 DEBUG(10,("cli_samr_set_sec_obj\n"));
1856 ZERO_STRUCT(q);
1857 ZERO_STRUCT(r);
1859 /* Marshall data and send request */
1861 init_samr_q_set_sec_obj(&q, user_pol, sec_info, sec_desc_buf);
1863 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_SET_SEC_OBJECT,
1864 q, r,
1865 qbuf, rbuf,
1866 samr_io_q_set_sec_obj,
1867 samr_io_r_set_sec_obj,
1868 NT_STATUS_UNSUCCESSFUL);
1870 /* Return output parameters */
1872 result = r.status;
1874 return result;
1878 /* Get domain password info */
1880 NTSTATUS rpccli_samr_get_dom_pwinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1881 uint16 *min_pwd_length, uint32 *password_properties)
1883 prs_struct qbuf, rbuf;
1884 SAMR_Q_GET_DOM_PWINFO q;
1885 SAMR_R_GET_DOM_PWINFO r;
1886 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1888 DEBUG(10,("cli_samr_get_dom_pwinfo\n"));
1890 ZERO_STRUCT(q);
1891 ZERO_STRUCT(r);
1893 /* Marshall data and send request */
1895 init_samr_q_get_dom_pwinfo(&q, cli->cli->desthost);
1897 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_GET_DOM_PWINFO,
1898 q, r,
1899 qbuf, rbuf,
1900 samr_io_q_get_dom_pwinfo,
1901 samr_io_r_get_dom_pwinfo,
1902 NT_STATUS_UNSUCCESSFUL);
1904 /* Return output parameters */
1906 result = r.status;
1908 if (NT_STATUS_IS_OK(result)) {
1909 if (min_pwd_length)
1910 *min_pwd_length = r.min_pwd_length;
1911 if (password_properties)
1912 *password_properties = r.password_properties;
1915 return result;
1918 /* Get domain password info */
1920 NTSTATUS rpccli_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1921 POLICY_HND *pol, uint16 *min_pwd_length,
1922 uint32 *password_properties, uint32 *unknown1)
1924 prs_struct qbuf, rbuf;
1925 SAMR_Q_GET_USRDOM_PWINFO q;
1926 SAMR_R_GET_USRDOM_PWINFO r;
1927 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1929 DEBUG(10,("cli_samr_get_usrdom_pwinfo\n"));
1931 ZERO_STRUCT(q);
1932 ZERO_STRUCT(r);
1934 /* Marshall data and send request */
1936 init_samr_q_get_usrdom_pwinfo(&q, pol);
1938 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_GET_USRDOM_PWINFO,
1939 q, r,
1940 qbuf, rbuf,
1941 samr_io_q_get_usrdom_pwinfo,
1942 samr_io_r_get_usrdom_pwinfo,
1943 NT_STATUS_UNSUCCESSFUL);
1945 /* Return output parameters */
1947 result = r.status;
1949 if (NT_STATUS_IS_OK(result)) {
1950 if (min_pwd_length)
1951 *min_pwd_length = r.min_pwd_length;
1952 if (password_properties)
1953 *password_properties = r.password_properties;
1954 if (unknown1)
1955 *unknown1 = r.unknown_1;
1958 return result;
1962 /* Lookup Domain Name */
1964 NTSTATUS rpccli_samr_lookup_domain(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1965 POLICY_HND *user_pol, char *domain_name,
1966 DOM_SID *sid)
1968 prs_struct qbuf, rbuf;
1969 SAMR_Q_LOOKUP_DOMAIN q;
1970 SAMR_R_LOOKUP_DOMAIN r;
1971 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1973 DEBUG(10,("cli_samr_lookup_domain\n"));
1975 ZERO_STRUCT(q);
1976 ZERO_STRUCT(r);
1978 /* Marshall data and send request */
1980 init_samr_q_lookup_domain(&q, user_pol, domain_name);
1982 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_LOOKUP_DOMAIN,
1983 q, r,
1984 qbuf, rbuf,
1985 samr_io_q_lookup_domain,
1986 samr_io_r_lookup_domain,
1987 NT_STATUS_UNSUCCESSFUL);
1989 /* Return output parameters */
1991 result = r.status;
1993 if (NT_STATUS_IS_OK(result))
1994 sid_copy(sid, &r.dom_sid.sid);
1996 return result;