[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / rpc_client / cli_samr.c
blob444011edc5d5642b93e36a341b3b94589a5fb288
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 if (num_sids) {
558 sid_ptrs = TALLOC_ARRAY(mem_ctx, uint32, num_sids);
559 if (sid_ptrs == NULL)
560 return NT_STATUS_NO_MEMORY;
561 } else {
562 sid_ptrs = NULL;
565 for (i=0; i<num_sids; i++)
566 sid_ptrs[i] = 1;
568 /* Marshall data and send request */
570 init_samr_q_query_useraliases(&q, dom_pol, num_sids, sid_ptrs, sid);
572 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_USERALIASES,
573 q, r,
574 qbuf, rbuf,
575 samr_io_q_query_useraliases,
576 samr_io_r_query_useraliases,
577 NT_STATUS_UNSUCCESSFUL);
579 /* Return output parameters */
581 if (NT_STATUS_IS_OK(result = r.status)) {
582 *num_aliases = r.num_entries;
583 *als_rids = r.rid;
586 return result;
589 /* Query user groups */
591 NTSTATUS rpccli_samr_query_groupmem(struct rpc_pipe_client *cli,
592 TALLOC_CTX *mem_ctx,
593 POLICY_HND *group_pol, uint32 *num_mem,
594 uint32 **rid, uint32 **attr)
596 prs_struct qbuf, rbuf;
597 SAMR_Q_QUERY_GROUPMEM q;
598 SAMR_R_QUERY_GROUPMEM r;
599 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
601 DEBUG(10,("cli_samr_query_groupmem\n"));
603 ZERO_STRUCT(q);
604 ZERO_STRUCT(r);
606 /* Marshall data and send request */
608 init_samr_q_query_groupmem(&q, group_pol);
610 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_GROUPMEM,
611 q, r,
612 qbuf, rbuf,
613 samr_io_q_query_groupmem,
614 samr_io_r_query_groupmem,
615 NT_STATUS_UNSUCCESSFUL);
617 /* Return output parameters */
619 if (NT_STATUS_IS_OK(result = r.status)) {
620 *num_mem = r.num_entries;
621 *rid = r.rid;
622 *attr = r.attr;
625 return result;
629 * Enumerate domain users
631 * @param cli client state structure
632 * @param mem_ctx talloc context
633 * @param pol opened domain policy handle
634 * @param start_idx starting index of enumeration, returns context for
635 next enumeration
636 * @param acb_mask account control bit mask (to enumerate some particular
637 * kind of accounts)
638 * @param size max acceptable size of response
639 * @param dom_users returned array of domain user names
640 * @param rids returned array of domain user RIDs
641 * @param num_dom_users numer returned entries
643 * @return NTSTATUS returned in rpc response
646 NTSTATUS rpccli_samr_enum_dom_users(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
647 POLICY_HND *pol, uint32 *start_idx, uint32 acb_mask,
648 uint32 size, char ***dom_users, uint32 **rids,
649 uint32 *num_dom_users)
651 prs_struct qbuf;
652 prs_struct rbuf;
653 SAMR_Q_ENUM_DOM_USERS q;
654 SAMR_R_ENUM_DOM_USERS r;
655 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
656 int i;
658 DEBUG(10,("cli_samr_enum_dom_users starting at index %u\n", (unsigned int)*start_idx));
660 ZERO_STRUCT(q);
661 ZERO_STRUCT(r);
663 /* always init this */
664 *num_dom_users = 0;
666 /* Fill query structure with parameters */
668 init_samr_q_enum_dom_users(&q, pol, *start_idx, acb_mask, size);
670 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_ENUM_DOM_USERS,
671 q, r,
672 qbuf, rbuf,
673 samr_io_q_enum_dom_users,
674 samr_io_r_enum_dom_users,
675 NT_STATUS_UNSUCCESSFUL);
677 result = r.status;
679 if (!NT_STATUS_IS_OK(result) &&
680 NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES))
681 goto done;
683 *start_idx = r.next_idx;
684 *num_dom_users = r.num_entries2;
686 if (r.num_entries2) {
687 /* allocate memory needed to return received data */
688 *rids = TALLOC_ARRAY(mem_ctx, uint32, r.num_entries2);
689 if (!*rids) {
690 DEBUG(0, ("Error in cli_samr_enum_dom_users(): out of memory\n"));
691 return NT_STATUS_NO_MEMORY;
694 *dom_users = TALLOC_ARRAY(mem_ctx, char*, r.num_entries2);
695 if (!*dom_users) {
696 DEBUG(0, ("Error in cli_samr_enum_dom_users(): out of memory\n"));
697 return NT_STATUS_NO_MEMORY;
700 /* fill output buffers with rpc response */
701 for (i = 0; i < r.num_entries2; i++) {
702 fstring conv_buf;
704 (*rids)[i] = r.sam[i].rid;
705 unistr2_to_ascii(conv_buf, &(r.uni_acct_name[i]), sizeof(conv_buf) - 1);
706 (*dom_users)[i] = talloc_strdup(mem_ctx, conv_buf);
710 done:
711 return result;
714 /* Enumerate domain groups */
716 NTSTATUS rpccli_samr_enum_dom_groups(struct rpc_pipe_client *cli,
717 TALLOC_CTX *mem_ctx,
718 POLICY_HND *pol, uint32 *start_idx,
719 uint32 size, struct acct_info **dom_groups,
720 uint32 *num_dom_groups)
722 prs_struct qbuf, rbuf;
723 SAMR_Q_ENUM_DOM_GROUPS q;
724 SAMR_R_ENUM_DOM_GROUPS r;
725 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
726 uint32 name_idx, i;
728 DEBUG(10,("cli_samr_enum_dom_groups starting at index %u\n", (unsigned int)*start_idx));
730 ZERO_STRUCT(q);
731 ZERO_STRUCT(r);
733 /* Marshall data and send request */
735 init_samr_q_enum_dom_groups(&q, pol, *start_idx, size);
737 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_ENUM_DOM_GROUPS,
738 q, r,
739 qbuf, rbuf,
740 samr_io_q_enum_dom_groups,
741 samr_io_r_enum_dom_groups,
742 NT_STATUS_UNSUCCESSFUL);
744 /* Return output parameters */
746 result = r.status;
748 if (!NT_STATUS_IS_OK(result) &&
749 NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES))
750 goto done;
752 *num_dom_groups = r.num_entries2;
754 if (*num_dom_groups == 0)
755 goto done;
757 if (!((*dom_groups) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_dom_groups))) {
758 result = NT_STATUS_NO_MEMORY;
759 goto done;
762 memset(*dom_groups, 0, sizeof(struct acct_info) * (*num_dom_groups));
764 name_idx = 0;
766 for (i = 0; i < *num_dom_groups; i++) {
768 (*dom_groups)[i].rid = r.sam[i].rid;
770 if (r.sam[i].hdr_name.buffer) {
771 unistr2_to_ascii((*dom_groups)[i].acct_name,
772 &r.uni_grp_name[name_idx],
773 sizeof(fstring) - 1);
774 name_idx++;
777 *start_idx = r.next_idx;
780 done:
781 return result;
784 /* Enumerate domain groups */
786 NTSTATUS rpccli_samr_enum_als_groups(struct rpc_pipe_client *cli,
787 TALLOC_CTX *mem_ctx,
788 POLICY_HND *pol, uint32 *start_idx,
789 uint32 size, struct acct_info **dom_aliases,
790 uint32 *num_dom_aliases)
792 prs_struct qbuf, rbuf;
793 SAMR_Q_ENUM_DOM_ALIASES q;
794 SAMR_R_ENUM_DOM_ALIASES r;
795 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
796 uint32 name_idx, i;
798 DEBUG(10,("cli_samr_enum_als_groups starting at index %u\n", (unsigned int)*start_idx));
800 ZERO_STRUCT(q);
801 ZERO_STRUCT(r);
803 /* Marshall data and send request */
805 init_samr_q_enum_dom_aliases(&q, pol, *start_idx, size);
807 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_ENUM_DOM_ALIASES,
808 q, r,
809 qbuf, rbuf,
810 samr_io_q_enum_dom_aliases,
811 samr_io_r_enum_dom_aliases,
812 NT_STATUS_UNSUCCESSFUL);
814 /* Return output parameters */
816 result = r.status;
818 if (!NT_STATUS_IS_OK(result) &&
819 NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES)) {
820 goto done;
823 *num_dom_aliases = r.num_entries2;
825 if (*num_dom_aliases == 0)
826 goto done;
828 if (!((*dom_aliases) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_dom_aliases))) {
829 result = NT_STATUS_NO_MEMORY;
830 goto done;
833 memset(*dom_aliases, 0, sizeof(struct acct_info) * *num_dom_aliases);
835 name_idx = 0;
837 for (i = 0; i < *num_dom_aliases; i++) {
839 (*dom_aliases)[i].rid = r.sam[i].rid;
841 if (r.sam[i].hdr_name.buffer) {
842 unistr2_to_ascii((*dom_aliases)[i].acct_name,
843 &r.uni_grp_name[name_idx],
844 sizeof(fstring) - 1);
845 name_idx++;
848 *start_idx = r.next_idx;
851 done:
852 return result;
855 /* Query alias members */
857 NTSTATUS rpccli_samr_query_aliasmem(struct rpc_pipe_client *cli,
858 TALLOC_CTX *mem_ctx,
859 POLICY_HND *alias_pol, uint32 *num_mem,
860 DOM_SID **sids)
862 prs_struct qbuf, rbuf;
863 SAMR_Q_QUERY_ALIASMEM q;
864 SAMR_R_QUERY_ALIASMEM r;
865 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
866 uint32 i;
868 DEBUG(10,("cli_samr_query_aliasmem\n"));
870 ZERO_STRUCT(q);
871 ZERO_STRUCT(r);
873 /* Marshall data and send request */
875 init_samr_q_query_aliasmem(&q, alias_pol);
877 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_ALIASMEM,
878 q, r,
879 qbuf, rbuf,
880 samr_io_q_query_aliasmem,
881 samr_io_r_query_aliasmem,
882 NT_STATUS_UNSUCCESSFUL);
884 /* Return output parameters */
886 if (!NT_STATUS_IS_OK(result = r.status)) {
887 goto done;
890 *num_mem = r.num_sids;
892 if (*num_mem == 0) {
893 *sids = NULL;
894 result = NT_STATUS_OK;
895 goto done;
898 if (!(*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *num_mem))) {
899 result = NT_STATUS_UNSUCCESSFUL;
900 goto done;
903 for (i = 0; i < *num_mem; i++) {
904 (*sids)[i] = r.sid[i].sid;
907 done:
908 return result;
911 /* Open handle on an alias */
913 NTSTATUS rpccli_samr_open_alias(struct rpc_pipe_client *cli,
914 TALLOC_CTX *mem_ctx,
915 POLICY_HND *domain_pol, uint32 access_mask,
916 uint32 alias_rid, POLICY_HND *alias_pol)
918 prs_struct qbuf, rbuf;
919 SAMR_Q_OPEN_ALIAS q;
920 SAMR_R_OPEN_ALIAS r;
921 NTSTATUS result;
923 DEBUG(10,("cli_samr_open_alias with rid 0x%x\n", alias_rid));
925 ZERO_STRUCT(q);
926 ZERO_STRUCT(r);
928 /* Marshall data and send request */
930 init_samr_q_open_alias(&q, domain_pol, access_mask, alias_rid);
932 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_OPEN_ALIAS,
933 q, r,
934 qbuf, rbuf,
935 samr_io_q_open_alias,
936 samr_io_r_open_alias,
937 NT_STATUS_UNSUCCESSFUL);
939 /* Return output parameters */
941 if (NT_STATUS_IS_OK(result = r.status)) {
942 *alias_pol = r.pol;
943 #ifdef __INSURE__
944 alias_pol->marker = malloc(1);
945 #endif
948 return result;
951 /* Create an alias */
953 NTSTATUS rpccli_samr_create_dom_alias(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
954 POLICY_HND *domain_pol, const char *name,
955 POLICY_HND *alias_pol)
957 prs_struct qbuf, rbuf;
958 SAMR_Q_CREATE_DOM_ALIAS q;
959 SAMR_R_CREATE_DOM_ALIAS r;
960 NTSTATUS result;
962 DEBUG(10,("cli_samr_create_dom_alias named %s\n", name));
964 ZERO_STRUCT(q);
965 ZERO_STRUCT(r);
967 /* Marshall data and send request */
969 init_samr_q_create_dom_alias(&q, domain_pol, name);
971 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CREATE_DOM_ALIAS,
972 q, r,
973 qbuf, rbuf,
974 samr_io_q_create_dom_alias,
975 samr_io_r_create_dom_alias,
976 NT_STATUS_UNSUCCESSFUL);
978 /* Return output parameters */
980 if (NT_STATUS_IS_OK(result = r.status)) {
981 *alias_pol = r.alias_pol;
984 return result;
987 /* Add an alias member */
989 NTSTATUS rpccli_samr_add_aliasmem(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
990 POLICY_HND *alias_pol, DOM_SID *member)
992 prs_struct qbuf, rbuf;
993 SAMR_Q_ADD_ALIASMEM q;
994 SAMR_R_ADD_ALIASMEM r;
995 NTSTATUS result;
997 DEBUG(10,("cli_samr_add_aliasmem"));
999 ZERO_STRUCT(q);
1000 ZERO_STRUCT(r);
1002 /* Marshall data and send request */
1004 init_samr_q_add_aliasmem(&q, alias_pol, member);
1006 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_ADD_ALIASMEM,
1007 q, r,
1008 qbuf, rbuf,
1009 samr_io_q_add_aliasmem,
1010 samr_io_r_add_aliasmem,
1011 NT_STATUS_UNSUCCESSFUL);
1013 result = r.status;
1015 return result;
1018 /* Delete an alias member */
1020 NTSTATUS rpccli_samr_del_aliasmem(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1021 POLICY_HND *alias_pol, DOM_SID *member)
1023 prs_struct qbuf, rbuf;
1024 SAMR_Q_DEL_ALIASMEM q;
1025 SAMR_R_DEL_ALIASMEM r;
1026 NTSTATUS result;
1028 DEBUG(10,("cli_samr_del_aliasmem"));
1030 ZERO_STRUCT(q);
1031 ZERO_STRUCT(r);
1033 /* Marshall data and send request */
1035 init_samr_q_del_aliasmem(&q, alias_pol, member);
1037 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_DEL_ALIASMEM,
1038 q, r,
1039 qbuf, rbuf,
1040 samr_io_q_del_aliasmem,
1041 samr_io_r_del_aliasmem,
1042 NT_STATUS_UNSUCCESSFUL);
1044 result = r.status;
1046 return result;
1049 /* Query alias info */
1051 NTSTATUS rpccli_samr_query_alias_info(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1052 POLICY_HND *alias_pol, uint16 switch_value,
1053 ALIAS_INFO_CTR *ctr)
1055 prs_struct qbuf, rbuf;
1056 SAMR_Q_QUERY_ALIASINFO q;
1057 SAMR_R_QUERY_ALIASINFO r;
1058 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1060 DEBUG(10,("cli_samr_query_alias_info\n"));
1062 ZERO_STRUCT(q);
1063 ZERO_STRUCT(r);
1065 /* Marshall data and send request */
1067 init_samr_q_query_aliasinfo(&q, alias_pol, switch_value);
1069 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_ALIASINFO,
1070 q, r,
1071 qbuf, rbuf,
1072 samr_io_q_query_aliasinfo,
1073 samr_io_r_query_aliasinfo,
1074 NT_STATUS_UNSUCCESSFUL);
1076 /* Return output parameters */
1078 if (!NT_STATUS_IS_OK(result = r.status)) {
1079 goto done;
1082 *ctr = *r.ctr;
1084 done:
1086 return result;
1089 /* Query domain info */
1091 NTSTATUS rpccli_samr_query_dom_info(struct rpc_pipe_client *cli,
1092 TALLOC_CTX *mem_ctx,
1093 POLICY_HND *domain_pol,
1094 uint16 switch_value,
1095 SAM_UNK_CTR *ctr)
1097 prs_struct qbuf, rbuf;
1098 SAMR_Q_QUERY_DOMAIN_INFO q;
1099 SAMR_R_QUERY_DOMAIN_INFO r;
1100 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1102 DEBUG(10,("cli_samr_query_dom_info\n"));
1104 ZERO_STRUCT(q);
1105 ZERO_STRUCT(r);
1107 /* Marshall data and send request */
1109 init_samr_q_query_domain_info(&q, domain_pol, switch_value);
1111 r.ctr = ctr;
1113 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_DOMAIN_INFO,
1114 q, r,
1115 qbuf, rbuf,
1116 samr_io_q_query_domain_info,
1117 samr_io_r_query_domain_info,
1118 NT_STATUS_UNSUCCESSFUL);
1120 /* Return output parameters */
1122 if (!NT_STATUS_IS_OK(result = r.status)) {
1123 goto done;
1126 done:
1128 return result;
1131 /* Query domain info2 */
1133 NTSTATUS rpccli_samr_query_dom_info2(struct rpc_pipe_client *cli,
1134 TALLOC_CTX *mem_ctx,
1135 POLICY_HND *domain_pol,
1136 uint16 switch_value,
1137 SAM_UNK_CTR *ctr)
1139 prs_struct qbuf, rbuf;
1140 SAMR_Q_QUERY_DOMAIN_INFO2 q;
1141 SAMR_R_QUERY_DOMAIN_INFO2 r;
1142 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1144 DEBUG(10,("cli_samr_query_dom_info2\n"));
1146 ZERO_STRUCT(q);
1147 ZERO_STRUCT(r);
1149 /* Marshall data and send request */
1151 init_samr_q_query_domain_info2(&q, domain_pol, switch_value);
1153 r.ctr = ctr;
1155 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_DOMAIN_INFO2,
1156 q, r,
1157 qbuf, rbuf,
1158 samr_io_q_query_domain_info2,
1159 samr_io_r_query_domain_info2,
1160 NT_STATUS_UNSUCCESSFUL);
1162 /* Return output parameters */
1164 if (!NT_STATUS_IS_OK(result = r.status)) {
1165 goto done;
1168 done:
1170 return result;
1173 /* Set domain info */
1175 NTSTATUS rpccli_samr_set_domain_info(struct rpc_pipe_client *cli,
1176 TALLOC_CTX *mem_ctx,
1177 POLICY_HND *domain_pol,
1178 uint16 switch_value,
1179 SAM_UNK_CTR *ctr)
1181 prs_struct qbuf, rbuf;
1182 SAMR_Q_SET_DOMAIN_INFO q;
1183 SAMR_R_SET_DOMAIN_INFO r;
1184 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1186 DEBUG(10,("cli_samr_set_domain_info\n"));
1188 ZERO_STRUCT(q);
1189 ZERO_STRUCT(r);
1191 /* Marshall data and send request */
1193 init_samr_q_set_domain_info(&q, domain_pol, switch_value, ctr);
1195 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_SET_DOMAIN_INFO,
1196 q, r,
1197 qbuf, rbuf,
1198 samr_io_q_set_domain_info,
1199 samr_io_r_set_domain_info,
1200 NT_STATUS_UNSUCCESSFUL);
1202 /* Return output parameters */
1204 if (!NT_STATUS_IS_OK(result = r.status)) {
1205 goto done;
1208 done:
1210 return result;
1213 /* User change password */
1215 NTSTATUS rpccli_samr_chgpasswd_user(struct rpc_pipe_client *cli,
1216 TALLOC_CTX *mem_ctx,
1217 const char *username,
1218 const char *newpassword,
1219 const char *oldpassword )
1221 prs_struct qbuf, rbuf;
1222 SAMR_Q_CHGPASSWD_USER q;
1223 SAMR_R_CHGPASSWD_USER r;
1224 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1226 uchar new_nt_password[516];
1227 uchar new_lm_password[516];
1228 uchar old_nt_hash[16];
1229 uchar old_lanman_hash[16];
1230 uchar old_nt_hash_enc[16];
1231 uchar old_lanman_hash_enc[16];
1233 uchar new_nt_hash[16];
1234 uchar new_lanman_hash[16];
1236 char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
1238 DEBUG(10,("rpccli_samr_chgpasswd_user\n"));
1240 ZERO_STRUCT(q);
1241 ZERO_STRUCT(r);
1243 /* Calculate the MD4 hash (NT compatible) of the password */
1244 E_md4hash(oldpassword, old_nt_hash);
1245 E_md4hash(newpassword, new_nt_hash);
1247 if (lp_client_lanman_auth()
1248 && E_deshash(newpassword, new_lanman_hash)
1249 && E_deshash(oldpassword, old_lanman_hash)) {
1250 /* E_deshash returns false for 'long' passwords (> 14
1251 DOS chars). This allows us to match Win2k, which
1252 does not store a LM hash for these passwords (which
1253 would reduce the effective password length to 14) */
1255 encode_pw_buffer(new_lm_password, newpassword, STR_UNICODE);
1257 SamOEMhash( new_lm_password, old_nt_hash, 516);
1258 E_old_pw_hash( new_nt_hash, old_lanman_hash, old_lanman_hash_enc);
1259 } else {
1260 ZERO_STRUCT(new_lm_password);
1261 ZERO_STRUCT(old_lanman_hash_enc);
1264 encode_pw_buffer(new_nt_password, newpassword, STR_UNICODE);
1266 SamOEMhash( new_nt_password, old_nt_hash, 516);
1267 E_old_pw_hash( new_nt_hash, old_nt_hash, old_nt_hash_enc);
1269 /* Marshall data and send request */
1271 init_samr_q_chgpasswd_user(&q, srv_name_slash, username,
1272 new_nt_password,
1273 old_nt_hash_enc,
1274 new_lm_password,
1275 old_lanman_hash_enc);
1277 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CHGPASSWD_USER,
1278 q, r,
1279 qbuf, rbuf,
1280 samr_io_q_chgpasswd_user,
1281 samr_io_r_chgpasswd_user,
1282 NT_STATUS_UNSUCCESSFUL);
1284 /* Return output parameters */
1286 if (!NT_STATUS_IS_OK(result = r.status)) {
1287 goto done;
1290 done:
1292 return result;
1295 /* User change password given blobs */
1297 NTSTATUS rpccli_samr_chng_pswd_auth_crap(struct rpc_pipe_client *cli,
1298 TALLOC_CTX *mem_ctx,
1299 const char *username,
1300 DATA_BLOB new_nt_password,
1301 DATA_BLOB old_nt_hash_enc,
1302 DATA_BLOB new_lm_password,
1303 DATA_BLOB old_lm_hash_enc)
1305 prs_struct qbuf, rbuf;
1306 SAMR_Q_CHGPASSWD_USER q;
1307 SAMR_R_CHGPASSWD_USER r;
1308 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1310 char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
1312 DEBUG(10,("rpccli_samr_chng_pswd_auth_crap\n"));
1314 ZERO_STRUCT(q);
1315 ZERO_STRUCT(r);
1317 /* Marshall data and send request */
1319 init_samr_q_chgpasswd_user(&q, srv_name_slash, username,
1320 new_nt_password.data,
1321 old_nt_hash_enc.data,
1322 new_lm_password.data,
1323 old_lm_hash_enc.data);
1325 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CHGPASSWD_USER,
1326 q, r,
1327 qbuf, rbuf,
1328 samr_io_q_chgpasswd_user,
1329 samr_io_r_chgpasswd_user,
1330 NT_STATUS_UNSUCCESSFUL);
1332 /* Return output parameters */
1334 if (!NT_STATUS_IS_OK(result = r.status)) {
1335 goto done;
1338 done:
1340 return result;
1344 /* change password 3 */
1346 NTSTATUS rpccli_samr_chgpasswd3(struct rpc_pipe_client *cli,
1347 TALLOC_CTX *mem_ctx,
1348 const char *username,
1349 const char *newpassword,
1350 const char *oldpassword,
1351 SAM_UNK_INFO_1 *info,
1352 SAMR_CHANGE_REJECT *reject)
1354 prs_struct qbuf, rbuf;
1355 SAMR_Q_CHGPASSWD_USER3 q;
1356 SAMR_R_CHGPASSWD_USER3 r;
1358 uchar new_nt_password[516];
1359 uchar new_lm_password[516];
1360 uchar old_nt_hash[16];
1361 uchar old_lanman_hash[16];
1362 uchar old_nt_hash_enc[16];
1363 uchar old_lanman_hash_enc[16];
1365 uchar new_nt_hash[16];
1366 uchar new_lanman_hash[16];
1368 char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
1370 DEBUG(10,("rpccli_samr_chgpasswd_user3\n"));
1372 ZERO_STRUCT(q);
1373 ZERO_STRUCT(r);
1375 /* Calculate the MD4 hash (NT compatible) of the password */
1376 E_md4hash(oldpassword, old_nt_hash);
1377 E_md4hash(newpassword, new_nt_hash);
1379 if (lp_client_lanman_auth()
1380 && E_deshash(newpassword, new_lanman_hash)
1381 && E_deshash(oldpassword, old_lanman_hash)) {
1382 /* E_deshash returns false for 'long' passwords (> 14
1383 DOS chars). This allows us to match Win2k, which
1384 does not store a LM hash for these passwords (which
1385 would reduce the effective password length to 14) */
1387 encode_pw_buffer(new_lm_password, newpassword, STR_UNICODE);
1389 SamOEMhash( new_lm_password, old_nt_hash, 516);
1390 E_old_pw_hash( new_nt_hash, old_lanman_hash, old_lanman_hash_enc);
1391 } else {
1392 ZERO_STRUCT(new_lm_password);
1393 ZERO_STRUCT(old_lanman_hash_enc);
1396 encode_pw_buffer(new_nt_password, newpassword, STR_UNICODE);
1398 SamOEMhash( new_nt_password, old_nt_hash, 516);
1399 E_old_pw_hash( new_nt_hash, old_nt_hash, old_nt_hash_enc);
1401 /* Marshall data and send request */
1403 init_samr_q_chgpasswd_user3(&q, srv_name_slash, username,
1404 new_nt_password,
1405 old_nt_hash_enc,
1406 new_lm_password,
1407 old_lanman_hash_enc);
1408 r.info = info;
1409 r.reject = reject;
1411 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CHGPASSWD_USER3,
1412 q, r,
1413 qbuf, rbuf,
1414 samr_io_q_chgpasswd_user3,
1415 samr_io_r_chgpasswd_user3,
1416 NT_STATUS_UNSUCCESSFUL);
1418 /* Return output parameters */
1420 return r.status;
1423 /* This function returns the bizzare set of (max_entries, max_size) required
1424 for the QueryDisplayInfo RPC to actually work against a domain controller
1425 with large (10k and higher) numbers of users. These values were
1426 obtained by inspection using ethereal and NT4 running User Manager. */
1428 void get_query_dispinfo_params(int loop_count, uint32 *max_entries,
1429 uint32 *max_size)
1431 switch(loop_count) {
1432 case 0:
1433 *max_entries = 512;
1434 *max_size = 16383;
1435 break;
1436 case 1:
1437 *max_entries = 1024;
1438 *max_size = 32766;
1439 break;
1440 case 2:
1441 *max_entries = 2048;
1442 *max_size = 65532;
1443 break;
1444 case 3:
1445 *max_entries = 4096;
1446 *max_size = 131064;
1447 break;
1448 default: /* loop_count >= 4 */
1449 *max_entries = 4096;
1450 *max_size = 131071;
1451 break;
1455 /* Query display info */
1457 NTSTATUS rpccli_samr_query_dispinfo(struct rpc_pipe_client *cli,
1458 TALLOC_CTX *mem_ctx,
1459 POLICY_HND *domain_pol, uint32 *start_idx,
1460 uint16 switch_value, uint32 *num_entries,
1461 uint32 max_entries, uint32 max_size,
1462 SAM_DISPINFO_CTR *ctr)
1464 prs_struct qbuf, rbuf;
1465 SAMR_Q_QUERY_DISPINFO q;
1466 SAMR_R_QUERY_DISPINFO r;
1467 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1469 DEBUG(10,("cli_samr_query_dispinfo for start_idx = %u\n", *start_idx));
1471 ZERO_STRUCT(q);
1472 ZERO_STRUCT(r);
1474 *num_entries = 0;
1476 /* Marshall data and send request */
1478 init_samr_q_query_dispinfo(&q, domain_pol, switch_value,
1479 *start_idx, max_entries, max_size);
1481 r.ctr = ctr;
1483 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_DISPINFO,
1484 q, r,
1485 qbuf, rbuf,
1486 samr_io_q_query_dispinfo,
1487 samr_io_r_query_dispinfo,
1488 NT_STATUS_UNSUCCESSFUL);
1490 /* Return output parameters */
1492 result = r.status;
1494 if (!NT_STATUS_IS_OK(result) &&
1495 NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1496 goto done;
1499 *num_entries = r.num_entries;
1500 *start_idx += r.num_entries; /* No next_idx in this structure! */
1502 done:
1503 return result;
1506 /* Lookup rids. Note that NT4 seems to crash if more than ~1000 rids are
1507 looked up in one packet. */
1509 NTSTATUS rpccli_samr_lookup_rids(struct rpc_pipe_client *cli,
1510 TALLOC_CTX *mem_ctx,
1511 POLICY_HND *domain_pol,
1512 uint32 num_rids, uint32 *rids,
1513 uint32 *num_names, char ***names,
1514 uint32 **name_types)
1516 prs_struct qbuf, rbuf;
1517 SAMR_Q_LOOKUP_RIDS q;
1518 SAMR_R_LOOKUP_RIDS r;
1519 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1520 uint32 i;
1522 DEBUG(10,("cli_samr_lookup_rids\n"));
1524 if (num_rids > 1000) {
1525 DEBUG(2, ("cli_samr_lookup_rids: warning: NT4 can crash if "
1526 "more than ~1000 rids are looked up at once.\n"));
1529 ZERO_STRUCT(q);
1530 ZERO_STRUCT(r);
1532 /* Marshall data and send request */
1534 init_samr_q_lookup_rids(mem_ctx, &q, domain_pol, 1000, num_rids, rids);
1536 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_LOOKUP_RIDS,
1537 q, r,
1538 qbuf, rbuf,
1539 samr_io_q_lookup_rids,
1540 samr_io_r_lookup_rids,
1541 NT_STATUS_UNSUCCESSFUL);
1543 /* Return output parameters */
1545 result = r.status;
1547 if (!NT_STATUS_IS_OK(result) &&
1548 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
1549 goto done;
1551 if (r.num_names1 == 0) {
1552 *num_names = 0;
1553 *names = NULL;
1554 goto done;
1557 *num_names = r.num_names1;
1558 *names = TALLOC_ARRAY(mem_ctx, char *, r.num_names1);
1559 *name_types = TALLOC_ARRAY(mem_ctx, uint32, r.num_names1);
1561 if ((*names == NULL) || (*name_types == NULL)) {
1562 TALLOC_FREE(*names);
1563 TALLOC_FREE(*name_types);
1564 return NT_STATUS_NO_MEMORY;
1567 for (i = 0; i < r.num_names1; i++) {
1568 fstring tmp;
1570 unistr2_to_ascii(tmp, &r.uni_name[i], sizeof(tmp) - 1);
1571 (*names)[i] = talloc_strdup(mem_ctx, tmp);
1572 (*name_types)[i] = r.type[i];
1575 done:
1577 return result;
1580 /* Lookup names */
1582 NTSTATUS rpccli_samr_lookup_names(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1583 POLICY_HND *domain_pol, uint32 flags,
1584 uint32 num_names, const char **names,
1585 uint32 *num_rids, uint32 **rids,
1586 uint32 **rid_types)
1588 prs_struct qbuf, rbuf;
1589 SAMR_Q_LOOKUP_NAMES q;
1590 SAMR_R_LOOKUP_NAMES r;
1591 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1592 uint32 i;
1594 DEBUG(10,("cli_samr_lookup_names\n"));
1596 ZERO_STRUCT(q);
1597 ZERO_STRUCT(r);
1599 /* Marshall data and send request */
1601 init_samr_q_lookup_names(mem_ctx, &q, domain_pol, flags,
1602 num_names, names);
1604 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_LOOKUP_NAMES,
1605 q, r,
1606 qbuf, rbuf,
1607 samr_io_q_lookup_names,
1608 samr_io_r_lookup_names,
1609 NT_STATUS_UNSUCCESSFUL);
1611 /* Return output parameters */
1613 if (!NT_STATUS_IS_OK(result = r.status)) {
1614 goto done;
1617 if (r.num_rids1 == 0) {
1618 *num_rids = 0;
1619 goto done;
1622 *num_rids = r.num_rids1;
1623 *rids = TALLOC_ARRAY(mem_ctx, uint32, r.num_rids1);
1624 *rid_types = TALLOC_ARRAY(mem_ctx, uint32, r.num_rids1);
1626 if ((*rids == NULL) || (*rid_types == NULL)) {
1627 TALLOC_FREE(*rids);
1628 TALLOC_FREE(*rid_types);
1629 return NT_STATUS_NO_MEMORY;
1632 for (i = 0; i < r.num_rids1; i++) {
1633 (*rids)[i] = r.rids[i];
1634 (*rid_types)[i] = r.types[i];
1637 done:
1639 return result;
1642 /* Create a domain user */
1644 NTSTATUS rpccli_samr_create_dom_user(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1645 POLICY_HND *domain_pol, const char *acct_name,
1646 uint32 acb_info, uint32 unknown,
1647 POLICY_HND *user_pol, uint32 *rid)
1649 prs_struct qbuf, rbuf;
1650 SAMR_Q_CREATE_USER q;
1651 SAMR_R_CREATE_USER r;
1652 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1654 DEBUG(10,("cli_samr_create_dom_user %s\n", acct_name));
1656 ZERO_STRUCT(q);
1657 ZERO_STRUCT(r);
1659 /* Marshall data and send request */
1661 init_samr_q_create_user(&q, domain_pol, acct_name, acb_info, unknown);
1663 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_CREATE_USER,
1664 q, r,
1665 qbuf, rbuf,
1666 samr_io_q_create_user,
1667 samr_io_r_create_user,
1668 NT_STATUS_UNSUCCESSFUL);
1670 /* Return output parameters */
1672 if (!NT_STATUS_IS_OK(result = r.status)) {
1673 goto done;
1676 if (user_pol)
1677 *user_pol = r.user_pol;
1679 if (rid)
1680 *rid = r.user_rid;
1682 done:
1684 return result;
1687 /* Set userinfo */
1689 NTSTATUS rpccli_samr_set_userinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1690 const POLICY_HND *user_pol, uint16 switch_value,
1691 DATA_BLOB *sess_key, SAM_USERINFO_CTR *ctr)
1693 prs_struct qbuf, rbuf;
1694 SAMR_Q_SET_USERINFO q;
1695 SAMR_R_SET_USERINFO r;
1696 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1698 DEBUG(10,("cli_samr_set_userinfo\n"));
1700 ZERO_STRUCT(q);
1701 ZERO_STRUCT(r);
1703 if (!sess_key->length) {
1704 DEBUG(1, ("No user session key\n"));
1705 return NT_STATUS_NO_USER_SESSION_KEY;
1708 /* Initialise parse structures */
1710 prs_init(&qbuf, RPC_MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1711 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1713 /* Marshall data and send request */
1715 q.ctr = ctr;
1717 init_samr_q_set_userinfo(&q, user_pol, sess_key, switch_value,
1718 ctr->info.id);
1720 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_SET_USERINFO,
1721 q, r,
1722 qbuf, rbuf,
1723 samr_io_q_set_userinfo,
1724 samr_io_r_set_userinfo,
1725 NT_STATUS_UNSUCCESSFUL);
1727 /* Return output parameters */
1729 if (!NT_STATUS_IS_OK(result = r.status)) {
1730 goto done;
1733 done:
1735 return result;
1738 /* Set userinfo2 */
1740 NTSTATUS rpccli_samr_set_userinfo2(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1741 const POLICY_HND *user_pol, uint16 switch_value,
1742 DATA_BLOB *sess_key, SAM_USERINFO_CTR *ctr)
1744 prs_struct qbuf, rbuf;
1745 SAMR_Q_SET_USERINFO2 q;
1746 SAMR_R_SET_USERINFO2 r;
1747 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1749 DEBUG(10,("cli_samr_set_userinfo2\n"));
1751 if (!sess_key->length) {
1752 DEBUG(1, ("No user session key\n"));
1753 return NT_STATUS_NO_USER_SESSION_KEY;
1756 ZERO_STRUCT(q);
1757 ZERO_STRUCT(r);
1759 /* Marshall data and send request */
1761 init_samr_q_set_userinfo2(&q, user_pol, sess_key, switch_value, ctr);
1763 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_SET_USERINFO2,
1764 q, r,
1765 qbuf, rbuf,
1766 samr_io_q_set_userinfo2,
1767 samr_io_r_set_userinfo2,
1768 NT_STATUS_UNSUCCESSFUL);
1770 /* Return output parameters */
1772 if (!NT_STATUS_IS_OK(result = r.status)) {
1773 goto done;
1776 done:
1778 return result;
1781 /* Delete domain group */
1783 NTSTATUS rpccli_samr_delete_dom_group(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1784 POLICY_HND *group_pol)
1786 prs_struct qbuf, rbuf;
1787 SAMR_Q_DELETE_DOM_GROUP q;
1788 SAMR_R_DELETE_DOM_GROUP r;
1789 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1791 DEBUG(10,("cli_samr_delete_dom_group\n"));
1793 ZERO_STRUCT(q);
1794 ZERO_STRUCT(r);
1796 /* Marshall data and send request */
1798 init_samr_q_delete_dom_group(&q, group_pol);
1800 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_DELETE_DOM_GROUP,
1801 q, r,
1802 qbuf, rbuf,
1803 samr_io_q_delete_dom_group,
1804 samr_io_r_delete_dom_group,
1805 NT_STATUS_UNSUCCESSFUL);
1807 /* Return output parameters */
1809 result = r.status;
1811 return result;
1814 /* Delete domain alias */
1816 NTSTATUS rpccli_samr_delete_dom_alias(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1817 POLICY_HND *alias_pol)
1819 prs_struct qbuf, rbuf;
1820 SAMR_Q_DELETE_DOM_ALIAS q;
1821 SAMR_R_DELETE_DOM_ALIAS r;
1822 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1824 DEBUG(10,("cli_samr_delete_dom_alias\n"));
1826 ZERO_STRUCT(q);
1827 ZERO_STRUCT(r);
1829 /* Marshall data and send request */
1831 init_samr_q_delete_dom_alias(&q, alias_pol);
1833 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_DELETE_DOM_ALIAS,
1834 q, r,
1835 qbuf, rbuf,
1836 samr_io_q_delete_dom_alias,
1837 samr_io_r_delete_dom_alias,
1838 NT_STATUS_UNSUCCESSFUL);
1840 /* Return output parameters */
1842 result = r.status;
1844 return result;
1847 /* Delete domain user */
1849 NTSTATUS rpccli_samr_delete_dom_user(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1850 POLICY_HND *user_pol)
1852 prs_struct qbuf, rbuf;
1853 SAMR_Q_DELETE_DOM_USER q;
1854 SAMR_R_DELETE_DOM_USER r;
1855 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1857 DEBUG(10,("cli_samr_delete_dom_user\n"));
1859 ZERO_STRUCT(q);
1860 ZERO_STRUCT(r);
1862 /* Marshall data and send request */
1864 init_samr_q_delete_dom_user(&q, user_pol);
1866 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_DELETE_DOM_USER,
1867 q, r,
1868 qbuf, rbuf,
1869 samr_io_q_delete_dom_user,
1870 samr_io_r_delete_dom_user,
1871 NT_STATUS_UNSUCCESSFUL);
1873 /* Return output parameters */
1875 result = r.status;
1877 return result;
1880 /* Remove foreign SID */
1882 NTSTATUS rpccli_samr_remove_sid_foreign_domain(struct rpc_pipe_client *cli,
1883 TALLOC_CTX *mem_ctx,
1884 POLICY_HND *user_pol,
1885 DOM_SID *sid)
1887 prs_struct qbuf, rbuf;
1888 SAMR_Q_REMOVE_SID_FOREIGN_DOMAIN q;
1889 SAMR_R_REMOVE_SID_FOREIGN_DOMAIN r;
1890 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1892 DEBUG(10,("cli_samr_remove_sid_foreign_domain\n"));
1894 ZERO_STRUCT(q);
1895 ZERO_STRUCT(r);
1897 /* Marshall data and send request */
1899 init_samr_q_remove_sid_foreign_domain(&q, user_pol, sid);
1901 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_REMOVE_SID_FOREIGN_DOMAIN,
1902 q, r,
1903 qbuf, rbuf,
1904 samr_io_q_remove_sid_foreign_domain,
1905 samr_io_r_remove_sid_foreign_domain,
1906 NT_STATUS_UNSUCCESSFUL);
1908 /* Return output parameters */
1910 result = r.status;
1912 return result;
1915 /* Query user security object */
1917 NTSTATUS rpccli_samr_query_sec_obj(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1918 POLICY_HND *user_pol, uint32 sec_info,
1919 TALLOC_CTX *ctx, SEC_DESC_BUF **sec_desc_buf)
1921 prs_struct qbuf, rbuf;
1922 SAMR_Q_QUERY_SEC_OBJ q;
1923 SAMR_R_QUERY_SEC_OBJ r;
1924 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1926 DEBUG(10,("cli_samr_query_sec_obj\n"));
1928 ZERO_STRUCT(q);
1929 ZERO_STRUCT(r);
1931 /* Marshall data and send request */
1933 init_samr_q_query_sec_obj(&q, user_pol, sec_info);
1935 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_QUERY_SEC_OBJECT,
1936 q, r,
1937 qbuf, rbuf,
1938 samr_io_q_query_sec_obj,
1939 samr_io_r_query_sec_obj,
1940 NT_STATUS_UNSUCCESSFUL);
1942 /* Return output parameters */
1944 result = r.status;
1945 *sec_desc_buf=dup_sec_desc_buf(ctx, r.buf);
1947 return result;
1950 /* Set user security object */
1952 NTSTATUS rpccli_samr_set_sec_obj(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1953 POLICY_HND *user_pol, uint32 sec_info,
1954 SEC_DESC_BUF *sec_desc_buf)
1956 prs_struct qbuf, rbuf;
1957 SAMR_Q_SET_SEC_OBJ q;
1958 SAMR_R_SET_SEC_OBJ r;
1959 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1961 DEBUG(10,("cli_samr_set_sec_obj\n"));
1963 ZERO_STRUCT(q);
1964 ZERO_STRUCT(r);
1966 /* Marshall data and send request */
1968 init_samr_q_set_sec_obj(&q, user_pol, sec_info, sec_desc_buf);
1970 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_SET_SEC_OBJECT,
1971 q, r,
1972 qbuf, rbuf,
1973 samr_io_q_set_sec_obj,
1974 samr_io_r_set_sec_obj,
1975 NT_STATUS_UNSUCCESSFUL);
1977 /* Return output parameters */
1979 result = r.status;
1981 return result;
1985 /* Get domain password info */
1987 NTSTATUS rpccli_samr_get_dom_pwinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1988 uint16 *min_pwd_length, uint32 *password_properties)
1990 prs_struct qbuf, rbuf;
1991 SAMR_Q_GET_DOM_PWINFO q;
1992 SAMR_R_GET_DOM_PWINFO r;
1993 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1995 DEBUG(10,("cli_samr_get_dom_pwinfo\n"));
1997 ZERO_STRUCT(q);
1998 ZERO_STRUCT(r);
2000 /* Marshall data and send request */
2002 init_samr_q_get_dom_pwinfo(&q, cli->cli->desthost);
2004 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_GET_DOM_PWINFO,
2005 q, r,
2006 qbuf, rbuf,
2007 samr_io_q_get_dom_pwinfo,
2008 samr_io_r_get_dom_pwinfo,
2009 NT_STATUS_UNSUCCESSFUL);
2011 /* Return output parameters */
2013 result = r.status;
2015 if (NT_STATUS_IS_OK(result)) {
2016 if (min_pwd_length)
2017 *min_pwd_length = r.min_pwd_length;
2018 if (password_properties)
2019 *password_properties = r.password_properties;
2022 return result;
2025 /* Get domain password info */
2027 NTSTATUS rpccli_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
2028 POLICY_HND *pol, uint16 *min_pwd_length,
2029 uint32 *password_properties, uint32 *unknown1)
2031 prs_struct qbuf, rbuf;
2032 SAMR_Q_GET_USRDOM_PWINFO q;
2033 SAMR_R_GET_USRDOM_PWINFO r;
2034 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2036 DEBUG(10,("cli_samr_get_usrdom_pwinfo\n"));
2038 ZERO_STRUCT(q);
2039 ZERO_STRUCT(r);
2041 /* Marshall data and send request */
2043 init_samr_q_get_usrdom_pwinfo(&q, pol);
2045 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_GET_USRDOM_PWINFO,
2046 q, r,
2047 qbuf, rbuf,
2048 samr_io_q_get_usrdom_pwinfo,
2049 samr_io_r_get_usrdom_pwinfo,
2050 NT_STATUS_UNSUCCESSFUL);
2052 /* Return output parameters */
2054 result = r.status;
2056 if (NT_STATUS_IS_OK(result)) {
2057 if (min_pwd_length)
2058 *min_pwd_length = r.min_pwd_length;
2059 if (password_properties)
2060 *password_properties = r.password_properties;
2061 if (unknown1)
2062 *unknown1 = r.unknown_1;
2065 return result;
2069 /* Lookup Domain Name */
2071 NTSTATUS rpccli_samr_lookup_domain(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
2072 POLICY_HND *user_pol, char *domain_name,
2073 DOM_SID *sid)
2075 prs_struct qbuf, rbuf;
2076 SAMR_Q_LOOKUP_DOMAIN q;
2077 SAMR_R_LOOKUP_DOMAIN r;
2078 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2080 DEBUG(10,("cli_samr_lookup_domain\n"));
2082 ZERO_STRUCT(q);
2083 ZERO_STRUCT(r);
2085 /* Marshall data and send request */
2087 init_samr_q_lookup_domain(&q, user_pol, domain_name);
2089 CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_LOOKUP_DOMAIN,
2090 q, r,
2091 qbuf, rbuf,
2092 samr_io_q_lookup_domain,
2093 samr_io_r_lookup_domain,
2094 NT_STATUS_UNSUCCESSFUL);
2096 /* Return output parameters */
2098 result = r.status;
2100 if (NT_STATUS_IS_OK(result))
2101 sid_copy(sid, &r.dom_sid.sid);
2103 return result;