2 Unix SMB/CIFS implementation.
4 Copyright (C) Tim Potter 2000-2001,
5 Copyright (C) Andrew Tridgell 1992-1997,2000,
6 Copyright (C) Luke Kenneth Casson Leighton 1996-1997,2000,
7 Copyright (C) Paul Ashton 1997,2000,
8 Copyright (C) Elrond 2000,
9 Copyright (C) Rafal Szczesniak 2002.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 /* Connect to SAMR database */
30 NTSTATUS
cli_samr_connect(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
31 uint32 access_mask
, POLICY_HND
*connect_pol
)
33 prs_struct qbuf
, rbuf
;
36 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
38 DEBUG(10,("cli_samr_connect to %s\n", cli
->desthost
));
43 /* Initialise parse structures */
45 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
46 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
48 /* Marshall data and send request */
50 init_samr_q_connect(&q
, cli
->desthost
, access_mask
);
52 if (!samr_io_q_connect("", &q
, &qbuf
, 0) ||
53 !rpc_api_pipe_req(cli
, SAMR_CONNECT
, &qbuf
, &rbuf
))
56 /* Unmarshall response */
58 if (!samr_io_r_connect("", &r
, &rbuf
, 0))
61 /* Return output parameters */
63 if (NT_STATUS_IS_OK(result
= r
.status
)) {
64 *connect_pol
= r
.connect_pol
;
66 connect_pol
->marker
= malloc(1);
77 /* Connect to SAMR database */
79 NTSTATUS
cli_samr_connect4(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
80 uint32 access_mask
, POLICY_HND
*connect_pol
)
82 prs_struct qbuf
, rbuf
;
85 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
87 DEBUG(10,("cli_samr_connect4 to %s\n", cli
->desthost
));
92 /* Initialise parse structures */
94 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
95 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
97 /* Marshall data and send request */
99 init_samr_q_connect4(&q
, cli
->desthost
, access_mask
);
101 if (!samr_io_q_connect4("", &q
, &qbuf
, 0) ||
102 !rpc_api_pipe_req(cli
, SAMR_CONNECT4
, &qbuf
, &rbuf
))
105 /* Unmarshall response */
107 if (!samr_io_r_connect4("", &r
, &rbuf
, 0))
110 /* Return output parameters */
112 if (NT_STATUS_IS_OK(result
= r
.status
)) {
113 *connect_pol
= r
.connect_pol
;
115 connect_pol
->marker
= malloc(1);
126 /* Close SAMR handle */
128 NTSTATUS
cli_samr_close(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
129 POLICY_HND
*connect_pol
)
131 prs_struct qbuf
, rbuf
;
134 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
136 DEBUG(10,("cli_samr_close\n"));
141 /* Initialise parse structures */
143 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
144 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
146 /* Marshall data and send request */
148 init_samr_q_close_hnd(&q
, connect_pol
);
150 if (!samr_io_q_close_hnd("", &q
, &qbuf
, 0) ||
151 !rpc_api_pipe_req(cli
, SAMR_CLOSE_HND
, &qbuf
, &rbuf
))
154 /* Unmarshall response */
156 if (!samr_io_r_close_hnd("", &r
, &rbuf
, 0))
159 /* Return output parameters */
161 if (NT_STATUS_IS_OK(result
= r
.status
)) {
163 SAFE_FREE(connect_pol
->marker
);
165 *connect_pol
= r
.pol
;
175 /* Open handle on a domain */
177 NTSTATUS
cli_samr_open_domain(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
178 POLICY_HND
*connect_pol
, uint32 access_mask
,
179 const DOM_SID
*domain_sid
, POLICY_HND
*domain_pol
)
181 prs_struct qbuf
, rbuf
;
182 SAMR_Q_OPEN_DOMAIN q
;
183 SAMR_R_OPEN_DOMAIN r
;
184 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
186 DEBUG(10,("cli_samr_open_domain with sid %s\n", sid_string_static(domain_sid
) ));
191 /* Initialise parse structures */
193 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
194 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
196 /* Marshall data and send request */
198 init_samr_q_open_domain(&q
, connect_pol
, access_mask
, domain_sid
);
200 if (!samr_io_q_open_domain("", &q
, &qbuf
, 0) ||
201 !rpc_api_pipe_req(cli
, SAMR_OPEN_DOMAIN
, &qbuf
, &rbuf
))
204 /* Unmarshall response */
206 if (!samr_io_r_open_domain("", &r
, &rbuf
, 0))
209 /* Return output parameters */
211 if (NT_STATUS_IS_OK(result
= r
.status
)) {
212 *domain_pol
= r
.domain_pol
;
214 domain_pol
->marker
= malloc(1);
225 /* Open handle on a user */
227 NTSTATUS
cli_samr_open_user(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
228 POLICY_HND
*domain_pol
, uint32 access_mask
,
229 uint32 user_rid
, POLICY_HND
*user_pol
)
231 prs_struct qbuf
, rbuf
;
234 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
236 DEBUG(10,("cli_samr_open_user with rid 0x%x\n", user_rid
));
241 /* Initialise parse structures */
243 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
244 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
246 /* Marshall data and send request */
248 init_samr_q_open_user(&q
, domain_pol
, access_mask
, user_rid
);
250 if (!samr_io_q_open_user("", &q
, &qbuf
, 0) ||
251 !rpc_api_pipe_req(cli
, SAMR_OPEN_USER
, &qbuf
, &rbuf
))
254 /* Unmarshall response */
256 if (!samr_io_r_open_user("", &r
, &rbuf
, 0))
259 /* Return output parameters */
261 if (NT_STATUS_IS_OK(result
= r
.status
)) {
262 *user_pol
= r
.user_pol
;
264 user_pol
->marker
= malloc(1);
275 /* Open handle on a group */
277 NTSTATUS
cli_samr_open_group(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
278 POLICY_HND
*domain_pol
, uint32 access_mask
,
279 uint32 group_rid
, POLICY_HND
*group_pol
)
281 prs_struct qbuf
, rbuf
;
284 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
286 DEBUG(10,("cli_samr_open_group with rid 0x%x\n", group_rid
));
291 /* Initialise parse structures */
293 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
294 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
296 /* Marshall data and send request */
298 init_samr_q_open_group(&q
, domain_pol
, access_mask
, group_rid
);
300 if (!samr_io_q_open_group("", &q
, &qbuf
, 0) ||
301 !rpc_api_pipe_req(cli
, SAMR_OPEN_GROUP
, &qbuf
, &rbuf
))
304 /* Unmarshall response */
306 if (!samr_io_r_open_group("", &r
, &rbuf
, 0))
309 /* Return output parameters */
311 if (NT_STATUS_IS_OK(result
= r
.status
)) {
314 group_pol
->marker
= malloc(1);
325 /* Query user info */
327 NTSTATUS
cli_samr_query_userinfo(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
328 POLICY_HND
*user_pol
, uint16 switch_value
,
329 SAM_USERINFO_CTR
**ctr
)
331 prs_struct qbuf
, rbuf
;
332 SAMR_Q_QUERY_USERINFO q
;
333 SAMR_R_QUERY_USERINFO r
;
334 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
336 DEBUG(10,("cli_samr_query_userinfo\n"));
341 /* Initialise parse structures */
343 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
344 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
346 /* Marshall data and send request */
348 init_samr_q_query_userinfo(&q
, user_pol
, switch_value
);
350 if (!samr_io_q_query_userinfo("", &q
, &qbuf
, 0) ||
351 !rpc_api_pipe_req(cli
, SAMR_QUERY_USERINFO
, &qbuf
, &rbuf
))
354 /* Unmarshall response */
356 if (!samr_io_r_query_userinfo("", &r
, &rbuf
, 0))
359 /* Return output parameters */
371 /* Query group info */
373 NTSTATUS
cli_samr_query_groupinfo(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
374 POLICY_HND
*group_pol
, uint32 info_level
,
375 GROUP_INFO_CTR
**ctr
)
377 prs_struct qbuf
, rbuf
;
378 SAMR_Q_QUERY_GROUPINFO q
;
379 SAMR_R_QUERY_GROUPINFO r
;
380 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
382 DEBUG(10,("cli_samr_query_groupinfo\n"));
387 /* Initialise parse structures */
389 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
390 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
392 /* Marshall data and send request */
394 init_samr_q_query_groupinfo(&q
, group_pol
, info_level
);
396 if (!samr_io_q_query_groupinfo("", &q
, &qbuf
, 0) ||
397 !rpc_api_pipe_req(cli
, SAMR_QUERY_GROUPINFO
, &qbuf
, &rbuf
))
400 /* Unmarshall response */
402 if (!samr_io_r_query_groupinfo("", &r
, &rbuf
, 0))
407 /* Return output parameters */
418 /* Query user groups */
420 NTSTATUS
cli_samr_query_usergroups(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
421 POLICY_HND
*user_pol
, uint32
*num_groups
,
424 prs_struct qbuf
, rbuf
;
425 SAMR_Q_QUERY_USERGROUPS q
;
426 SAMR_R_QUERY_USERGROUPS r
;
427 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
429 DEBUG(10,("cli_samr_query_usergroups\n"));
434 /* Initialise parse structures */
436 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
437 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
439 /* Marshall data and send request */
441 init_samr_q_query_usergroups(&q
, user_pol
);
443 if (!samr_io_q_query_usergroups("", &q
, &qbuf
, 0) ||
444 !rpc_api_pipe_req(cli
, SAMR_QUERY_USERGROUPS
, &qbuf
, &rbuf
))
447 /* Unmarshall response */
449 if (!samr_io_r_query_usergroups("", &r
, &rbuf
, 0))
452 /* Return output parameters */
454 if (NT_STATUS_IS_OK(result
= r
.status
)) {
455 *num_groups
= r
.num_entries
;
466 /* Query user aliases */
468 NTSTATUS
cli_samr_query_useraliases(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
469 POLICY_HND
*user_pol
, uint32 num_sids
, DOM_SID2
*sid
,
470 uint32
*num_aliases
, uint32
**als_rids
)
472 prs_struct qbuf
, rbuf
;
473 SAMR_Q_QUERY_USERALIASES q
;
474 SAMR_R_QUERY_USERALIASES r
;
475 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
478 DEBUG(10,("cli_samr_query_useraliases\n"));
483 /* Initialise parse structures */
485 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
486 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
488 /* Marshall data and send request */
490 init_samr_q_query_useraliases(&q
, user_pol
, num_sids
, &ptr
, sid
);
492 if (!samr_io_q_query_useraliases("", &q
, &qbuf
, 0) ||
493 !rpc_api_pipe_req(cli
, SAMR_QUERY_USERALIASES
, &qbuf
, &rbuf
))
496 /* Unmarshall response */
498 if (!samr_io_r_query_useraliases("", &r
, &rbuf
, 0))
501 /* Return output parameters */
503 if (NT_STATUS_IS_OK(result
= r
.status
)) {
504 *num_aliases
= r
.num_entries
;
515 /* Query user groups */
517 NTSTATUS
cli_samr_query_groupmem(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
518 POLICY_HND
*group_pol
, uint32
*num_mem
,
519 uint32
**rid
, uint32
**attr
)
521 prs_struct qbuf
, rbuf
;
522 SAMR_Q_QUERY_GROUPMEM q
;
523 SAMR_R_QUERY_GROUPMEM r
;
524 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
526 DEBUG(10,("cli_samr_query_groupmem\n"));
531 /* Initialise parse structures */
533 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
534 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
536 /* Marshall data and send request */
538 init_samr_q_query_groupmem(&q
, group_pol
);
540 if (!samr_io_q_query_groupmem("", &q
, &qbuf
, 0) ||
541 !rpc_api_pipe_req(cli
, SAMR_QUERY_GROUPMEM
, &qbuf
, &rbuf
))
544 /* Unmarshall response */
546 if (!samr_io_r_query_groupmem("", &r
, &rbuf
, 0))
549 /* Return output parameters */
551 if (NT_STATUS_IS_OK(result
= r
.status
)) {
552 *num_mem
= r
.num_entries
;
565 * Enumerate domain users
567 * @param cli client state structure
568 * @param mem_ctx talloc context
569 * @param pol opened domain policy handle
570 * @param start_idx starting index of enumeration, returns context for
572 * @param acb_mask account control bit mask (to enumerate some particular
574 * @param size max acceptable size of response
575 * @param dom_users returned array of domain user names
576 * @param rids returned array of domain user RIDs
577 * @param num_dom_users numer returned entries
579 * @return NTSTATUS returned in rpc response
581 NTSTATUS
cli_samr_enum_dom_users(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
582 POLICY_HND
*pol
, uint32
*start_idx
, uint16 acb_mask
,
583 uint32 size
, char ***dom_users
, uint32
**rids
,
584 uint32
*num_dom_users
)
588 SAMR_Q_ENUM_DOM_USERS q
;
589 SAMR_R_ENUM_DOM_USERS r
;
590 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
593 DEBUG(10,("cli_samr_enum_dom_users starting at index %u\n", (unsigned int)*start_idx
));
598 /* always init this */
601 /* Initialise parse structures */
603 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
604 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
606 /* Fill query structure with parameters */
608 init_samr_q_enum_dom_users(&q
, pol
, *start_idx
, acb_mask
, 0, size
);
610 if (!samr_io_q_enum_dom_users("", &q
, &qbuf
, 0) ||
611 !rpc_api_pipe_req(cli
, SAMR_ENUM_DOM_USERS
, &qbuf
, &rbuf
)) {
615 /* unpack received stream */
617 if(!samr_io_r_enum_dom_users("", &r
, &rbuf
, 0))
622 if (!NT_STATUS_IS_OK(result
) &&
623 NT_STATUS_V(result
) != NT_STATUS_V(STATUS_MORE_ENTRIES
))
626 *start_idx
= r
.next_idx
;
627 *num_dom_users
= r
.num_entries2
;
629 if (r
.num_entries2
) {
630 /* allocate memory needed to return received data */
631 *rids
= (uint32
*)talloc(mem_ctx
, sizeof(uint32
) * r
.num_entries2
);
633 DEBUG(0, ("Error in cli_samr_enum_dom_users(): out of memory\n"));
634 return NT_STATUS_NO_MEMORY
;
637 *dom_users
= (char**)talloc(mem_ctx
, sizeof(char*) * r
.num_entries2
);
639 DEBUG(0, ("Error in cli_samr_enum_dom_users(): out of memory\n"));
640 return NT_STATUS_NO_MEMORY
;
643 /* fill output buffers with rpc response */
644 for (i
= 0; i
< r
.num_entries2
; i
++) {
647 (*rids
)[i
] = r
.sam
[i
].rid
;
648 unistr2_to_ascii(conv_buf
, &(r
.uni_acct_name
[i
]), sizeof(conv_buf
) - 1);
649 (*dom_users
)[i
] = talloc_strdup(mem_ctx
, conv_buf
);
660 /* Enumerate domain groups */
662 NTSTATUS
cli_samr_enum_dom_groups(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
663 POLICY_HND
*pol
, uint32
*start_idx
,
664 uint32 size
, struct acct_info
**dom_groups
,
665 uint32
*num_dom_groups
)
667 prs_struct qbuf
, rbuf
;
668 SAMR_Q_ENUM_DOM_GROUPS q
;
669 SAMR_R_ENUM_DOM_GROUPS r
;
670 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
673 DEBUG(10,("cli_samr_enum_dom_groups starting at index %u\n", (unsigned int)*start_idx
));
678 /* Initialise parse structures */
680 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
681 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
683 /* Marshall data and send request */
685 init_samr_q_enum_dom_groups(&q
, pol
, *start_idx
, size
);
687 if (!samr_io_q_enum_dom_groups("", &q
, &qbuf
, 0) ||
688 !rpc_api_pipe_req(cli
, SAMR_ENUM_DOM_GROUPS
, &qbuf
, &rbuf
))
691 /* Unmarshall response */
693 if (!samr_io_r_enum_dom_groups("", &r
, &rbuf
, 0))
696 /* Return output parameters */
700 if (!NT_STATUS_IS_OK(result
) &&
701 NT_STATUS_V(result
) != NT_STATUS_V(STATUS_MORE_ENTRIES
))
704 *num_dom_groups
= r
.num_entries2
;
706 if (*num_dom_groups
== 0)
709 if (!((*dom_groups
) = (struct acct_info
*)
710 talloc(mem_ctx
, sizeof(struct acct_info
) * *num_dom_groups
))) {
711 result
= NT_STATUS_NO_MEMORY
;
715 memset(*dom_groups
, 0, sizeof(struct acct_info
) * (*num_dom_groups
));
719 for (i
= 0; i
< *num_dom_groups
; i
++) {
721 (*dom_groups
)[i
].rid
= r
.sam
[i
].rid
;
723 if (r
.sam
[i
].hdr_name
.buffer
) {
724 unistr2_to_ascii((*dom_groups
)[i
].acct_name
,
725 &r
.uni_grp_name
[name_idx
],
726 sizeof(fstring
) - 1);
730 *start_idx
= r
.next_idx
;
740 /* Enumerate domain groups */
742 NTSTATUS
cli_samr_enum_als_groups(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
743 POLICY_HND
*pol
, uint32
*start_idx
,
744 uint32 size
, struct acct_info
**dom_aliases
,
745 uint32
*num_dom_aliases
)
747 prs_struct qbuf
, rbuf
;
748 SAMR_Q_ENUM_DOM_ALIASES q
;
749 SAMR_R_ENUM_DOM_ALIASES r
;
750 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
753 DEBUG(10,("cli_samr_enum_als_groups starting at index %u\n", (unsigned int)*start_idx
));
758 /* Initialise parse structures */
760 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
761 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
763 /* Marshall data and send request */
765 init_samr_q_enum_dom_aliases(&q
, pol
, *start_idx
, size
);
767 if (!samr_io_q_enum_dom_aliases("", &q
, &qbuf
, 0) ||
768 !rpc_api_pipe_req(cli
, SAMR_ENUM_DOM_ALIASES
, &qbuf
, &rbuf
)) {
772 /* Unmarshall response */
774 if (!samr_io_r_enum_dom_aliases("", &r
, &rbuf
, 0)) {
778 /* Return output parameters */
782 if (!NT_STATUS_IS_OK(result
) &&
783 NT_STATUS_V(result
) != NT_STATUS_V(STATUS_MORE_ENTRIES
)) {
787 *num_dom_aliases
= r
.num_entries2
;
789 if (*num_dom_aliases
== 0)
792 if (!((*dom_aliases
) = (struct acct_info
*)
793 talloc(mem_ctx
, sizeof(struct acct_info
) * *num_dom_aliases
))) {
794 result
= NT_STATUS_NO_MEMORY
;
798 memset(*dom_aliases
, 0, sizeof(struct acct_info
) * *num_dom_aliases
);
802 for (i
= 0; i
< *num_dom_aliases
; i
++) {
804 (*dom_aliases
)[i
].rid
= r
.sam
[i
].rid
;
806 if (r
.sam
[i
].hdr_name
.buffer
) {
807 unistr2_to_ascii((*dom_aliases
)[i
].acct_name
,
808 &r
.uni_grp_name
[name_idx
],
809 sizeof(fstring
) - 1);
813 *start_idx
= r
.next_idx
;
823 /* Query alias members */
825 NTSTATUS
cli_samr_query_aliasmem(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
826 POLICY_HND
*alias_pol
, uint32
*num_mem
,
829 prs_struct qbuf
, rbuf
;
830 SAMR_Q_QUERY_ALIASMEM q
;
831 SAMR_R_QUERY_ALIASMEM r
;
832 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
835 DEBUG(10,("cli_samr_query_aliasmem\n"));
840 /* Initialise parse structures */
842 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
843 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
845 /* Marshall data and send request */
847 init_samr_q_query_aliasmem(&q
, alias_pol
);
849 if (!samr_io_q_query_aliasmem("", &q
, &qbuf
, 0) ||
850 !rpc_api_pipe_req(cli
, SAMR_QUERY_ALIASMEM
, &qbuf
, &rbuf
)) {
854 /* Unmarshall response */
856 if (!samr_io_r_query_aliasmem("", &r
, &rbuf
, 0)) {
860 /* Return output parameters */
862 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
866 *num_mem
= r
.num_sids
;
868 if (!(*sids
= talloc(mem_ctx
, sizeof(DOM_SID
) * *num_mem
))) {
869 result
= NT_STATUS_UNSUCCESSFUL
;
873 for (i
= 0; i
< *num_mem
; i
++) {
874 (*sids
)[i
] = r
.sid
[i
].sid
;
884 /* Open handle on an alias */
886 NTSTATUS
cli_samr_open_alias(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
887 POLICY_HND
*domain_pol
, uint32 access_mask
,
888 uint32 alias_rid
, POLICY_HND
*alias_pol
)
890 prs_struct qbuf
, rbuf
;
895 DEBUG(10,("cli_samr_open_alias with rid 0x%x\n", alias_rid
));
900 /* Initialise parse structures */
902 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
903 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
905 /* Marshall data and send request */
907 init_samr_q_open_alias(&q
, domain_pol
, access_mask
, alias_rid
);
909 if (!samr_io_q_open_alias("", &q
, &qbuf
, 0) ||
910 !rpc_api_pipe_req(cli
, SAMR_OPEN_ALIAS
, &qbuf
, &rbuf
)) {
911 result
= NT_STATUS_UNSUCCESSFUL
;
915 /* Unmarshall response */
917 if (!samr_io_r_open_alias("", &r
, &rbuf
, 0)) {
918 result
= NT_STATUS_UNSUCCESSFUL
;
922 /* Return output parameters */
924 if (NT_STATUS_IS_OK(result
= r
.status
)) {
927 alias_pol
->marker
= malloc(1);
938 /* Query alias info */
940 NTSTATUS
cli_samr_query_alias_info(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
941 POLICY_HND
*alias_pol
, uint16 switch_value
,
944 prs_struct qbuf
, rbuf
;
945 SAMR_Q_QUERY_ALIASINFO q
;
946 SAMR_R_QUERY_ALIASINFO r
;
947 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
949 DEBUG(10,("cli_samr_query_dom_info\n"));
954 /* Initialise parse structures */
956 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
957 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
959 /* Marshall data and send request */
961 init_samr_q_query_aliasinfo(&q
, alias_pol
, switch_value
);
963 if (!samr_io_q_query_aliasinfo("", &q
, &qbuf
, 0) ||
964 !rpc_api_pipe_req(cli
, SAMR_QUERY_ALIASINFO
, &qbuf
, &rbuf
)) {
968 /* Unmarshall response */
970 if (!samr_io_r_query_aliasinfo("", &r
, &rbuf
, 0)) {
974 /* Return output parameters */
976 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
989 /* Query domain info */
991 NTSTATUS
cli_samr_query_dom_info(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
992 POLICY_HND
*domain_pol
, uint16 switch_value
,
995 prs_struct qbuf
, rbuf
;
996 SAMR_Q_QUERY_DOMAIN_INFO q
;
997 SAMR_R_QUERY_DOMAIN_INFO r
;
998 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1000 DEBUG(10,("cli_samr_query_dom_info\n"));
1005 /* Initialise parse structures */
1007 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1008 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1010 /* Marshall data and send request */
1012 init_samr_q_query_dom_info(&q
, domain_pol
, switch_value
);
1014 if (!samr_io_q_query_dom_info("", &q
, &qbuf
, 0) ||
1015 !rpc_api_pipe_req(cli
, SAMR_QUERY_DOMAIN_INFO
, &qbuf
, &rbuf
)) {
1019 /* Unmarshall response */
1023 if (!samr_io_r_query_dom_info("", &r
, &rbuf
, 0)) {
1027 /* Return output parameters */
1029 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
1034 prs_mem_free(&qbuf
);
1035 prs_mem_free(&rbuf
);
1040 /* This function returns the bizzare set of (max_entries, max_size) required
1041 for the QueryDisplayInfo RPC to actually work against a domain controller
1042 with large (10k and higher) numbers of users. These values were
1043 obtained by inspection using ethereal and NT4 running User Manager. */
1045 void get_query_dispinfo_params(int loop_count
, uint32
*max_entries
,
1048 switch(loop_count
) {
1054 *max_entries
= 1024;
1058 *max_entries
= 2048;
1062 *max_entries
= 4096;
1065 default: /* loop_count >= 4 */
1066 *max_entries
= 4096;
1072 /* Query display info */
1074 NTSTATUS
cli_samr_query_dispinfo(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1075 POLICY_HND
*domain_pol
, uint32
*start_idx
,
1076 uint16 switch_value
, uint32
*num_entries
,
1077 uint32 max_entries
, uint32 max_size
,
1078 SAM_DISPINFO_CTR
*ctr
)
1080 prs_struct qbuf
, rbuf
;
1081 SAMR_Q_QUERY_DISPINFO q
;
1082 SAMR_R_QUERY_DISPINFO r
;
1083 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1085 DEBUG(10,("cli_samr_query_dispinfo for start_idx = %u\n", *start_idx
));
1092 /* Initialise parse structures */
1094 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1095 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1097 /* Marshall data and send request */
1099 init_samr_q_query_dispinfo(&q
, domain_pol
, switch_value
,
1100 *start_idx
, max_entries
, max_size
);
1102 if (!samr_io_q_query_dispinfo("", &q
, &qbuf
, 0) ||
1103 !rpc_api_pipe_req(cli
, SAMR_QUERY_DISPINFO
, &qbuf
, &rbuf
)) {
1107 /* Unmarshall response */
1111 if (!samr_io_r_query_dispinfo("", &r
, &rbuf
, 0)) {
1115 /* Return output parameters */
1119 if (!NT_STATUS_IS_OK(result
) &&
1120 NT_STATUS_V(result
) != NT_STATUS_V(STATUS_MORE_ENTRIES
)) {
1124 *num_entries
= r
.num_entries
;
1125 *start_idx
+= r
.num_entries
; /* No next_idx in this structure! */
1128 prs_mem_free(&qbuf
);
1129 prs_mem_free(&rbuf
);
1134 /* Lookup rids. Note that NT4 seems to crash if more than ~1000 rids are
1135 looked up in one packet. */
1137 NTSTATUS
cli_samr_lookup_rids(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1138 POLICY_HND
*domain_pol
, uint32 flags
,
1139 uint32 num_rids
, uint32
*rids
,
1140 uint32
*num_names
, char ***names
,
1141 uint32
**name_types
)
1143 prs_struct qbuf
, rbuf
;
1144 SAMR_Q_LOOKUP_RIDS q
;
1145 SAMR_R_LOOKUP_RIDS r
;
1146 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1149 DEBUG(10,("cli_samr_lookup_rids\n"));
1151 if (num_rids
> 1000) {
1152 DEBUG(2, ("cli_samr_lookup_rids: warning: NT4 can crash if "
1153 "more than ~1000 rids are looked up at once.\n"));
1159 /* Initialise parse structures */
1161 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1162 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1164 /* Marshall data and send request */
1166 init_samr_q_lookup_rids(mem_ctx
, &q
, domain_pol
, flags
,
1169 if (!samr_io_q_lookup_rids("", &q
, &qbuf
, 0) ||
1170 !rpc_api_pipe_req(cli
, SAMR_LOOKUP_RIDS
, &qbuf
, &rbuf
)) {
1174 /* Unmarshall response */
1176 if (!samr_io_r_lookup_rids("", &r
, &rbuf
, 0)) {
1180 /* Return output parameters */
1182 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
1186 if (r
.num_names1
== 0) {
1192 *num_names
= r
.num_names1
;
1193 *names
= talloc(mem_ctx
, sizeof(char *) * r
.num_names1
);
1194 *name_types
= talloc(mem_ctx
, sizeof(uint32
) * r
.num_names1
);
1196 for (i
= 0; i
< r
.num_names1
; i
++) {
1199 unistr2_to_ascii(tmp
, &r
.uni_name
[i
], sizeof(tmp
) - 1);
1200 (*names
)[i
] = talloc_strdup(mem_ctx
, tmp
);
1201 (*name_types
)[i
] = r
.type
[i
];
1205 prs_mem_free(&qbuf
);
1206 prs_mem_free(&rbuf
);
1213 NTSTATUS
cli_samr_lookup_names(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1214 POLICY_HND
*domain_pol
, uint32 flags
,
1215 uint32 num_names
, const char **names
,
1216 uint32
*num_rids
, uint32
**rids
,
1219 prs_struct qbuf
, rbuf
;
1220 SAMR_Q_LOOKUP_NAMES q
;
1221 SAMR_R_LOOKUP_NAMES r
;
1222 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1225 DEBUG(10,("cli_samr_lookup_names\n"));
1230 /* Initialise parse structures */
1232 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1233 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1235 /* Marshall data and send request */
1237 init_samr_q_lookup_names(mem_ctx
, &q
, domain_pol
, flags
,
1240 if (!samr_io_q_lookup_names("", &q
, &qbuf
, 0) ||
1241 !rpc_api_pipe_req(cli
, SAMR_LOOKUP_NAMES
, &qbuf
, &rbuf
)) {
1245 /* Unmarshall response */
1247 if (!samr_io_r_lookup_names("", &r
, &rbuf
, 0)) {
1251 /* Return output parameters */
1253 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
1257 if (r
.num_rids1
== 0) {
1262 *num_rids
= r
.num_rids1
;
1263 *rids
= talloc(mem_ctx
, sizeof(uint32
) * r
.num_rids1
);
1264 *rid_types
= talloc(mem_ctx
, sizeof(uint32
) * r
.num_rids1
);
1266 for (i
= 0; i
< r
.num_rids1
; i
++) {
1267 (*rids
)[i
] = r
.rids
[i
];
1268 (*rid_types
)[i
] = r
.types
[i
];
1272 prs_mem_free(&qbuf
);
1273 prs_mem_free(&rbuf
);
1278 /* Create a domain user */
1280 NTSTATUS
cli_samr_create_dom_user(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1281 POLICY_HND
*domain_pol
, const char *acct_name
,
1282 uint32 acb_info
, uint32 unknown
,
1283 POLICY_HND
*user_pol
, uint32
*rid
)
1285 prs_struct qbuf
, rbuf
;
1286 SAMR_Q_CREATE_USER q
;
1287 SAMR_R_CREATE_USER r
;
1288 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1290 DEBUG(10,("cli_samr_create_dom_user %s\n", acct_name
));
1295 /* Initialise parse structures */
1297 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1298 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1300 /* Marshall data and send request */
1302 init_samr_q_create_user(&q
, domain_pol
, acct_name
, acb_info
, unknown
);
1304 if (!samr_io_q_create_user("", &q
, &qbuf
, 0) ||
1305 !rpc_api_pipe_req(cli
, SAMR_CREATE_USER
, &qbuf
, &rbuf
)) {
1309 /* Unmarshall response */
1311 if (!samr_io_r_create_user("", &r
, &rbuf
, 0)) {
1315 /* Return output parameters */
1317 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
1322 *user_pol
= r
.user_pol
;
1328 prs_mem_free(&qbuf
);
1329 prs_mem_free(&rbuf
);
1336 NTSTATUS
cli_samr_set_userinfo(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1337 POLICY_HND
*user_pol
, uint16 switch_value
,
1338 uchar sess_key
[16], SAM_USERINFO_CTR
*ctr
)
1340 prs_struct qbuf
, rbuf
;
1341 SAMR_Q_SET_USERINFO q
;
1342 SAMR_R_SET_USERINFO r
;
1343 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1345 DEBUG(10,("cli_samr_set_userinfo\n"));
1350 /* Initialise parse structures */
1352 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1353 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1355 /* Marshall data and send request */
1359 init_samr_q_set_userinfo(&q
, user_pol
, sess_key
, switch_value
,
1362 if (!samr_io_q_set_userinfo("", &q
, &qbuf
, 0) ||
1363 !rpc_api_pipe_req(cli
, SAMR_SET_USERINFO
, &qbuf
, &rbuf
)) {
1367 /* Unmarshall response */
1369 if (!samr_io_r_set_userinfo("", &r
, &rbuf
, 0)) {
1373 /* Return output parameters */
1375 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
1380 prs_mem_free(&qbuf
);
1381 prs_mem_free(&rbuf
);
1388 NTSTATUS
cli_samr_set_userinfo2(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1389 POLICY_HND
*user_pol
, uint16 switch_value
,
1390 uchar sess_key
[16], SAM_USERINFO_CTR
*ctr
)
1392 prs_struct qbuf
, rbuf
;
1393 SAMR_Q_SET_USERINFO2 q
;
1394 SAMR_R_SET_USERINFO2 r
;
1395 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1397 DEBUG(10,("cli_samr_set_userinfo2\n"));
1402 /* Initialise parse structures */
1404 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1405 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1407 /* Marshall data and send request */
1409 init_samr_q_set_userinfo2(&q
, user_pol
, sess_key
, switch_value
, ctr
);
1411 if (!samr_io_q_set_userinfo2("", &q
, &qbuf
, 0) ||
1412 !rpc_api_pipe_req(cli
, SAMR_SET_USERINFO2
, &qbuf
, &rbuf
)) {
1416 /* Unmarshall response */
1418 if (!samr_io_r_set_userinfo2("", &r
, &rbuf
, 0)) {
1422 /* Return output parameters */
1424 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
1429 prs_mem_free(&qbuf
);
1430 prs_mem_free(&rbuf
);
1435 /* Delete domain user */
1437 NTSTATUS
cli_samr_delete_dom_user(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1438 POLICY_HND
*user_pol
)
1440 prs_struct qbuf
, rbuf
;
1441 SAMR_Q_DELETE_DOM_USER q
;
1442 SAMR_R_DELETE_DOM_USER r
;
1443 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1445 DEBUG(10,("cli_samr_delete_dom_user\n"));
1450 /* Initialise parse structures */
1452 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1453 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1455 /* Marshall data and send request */
1457 init_samr_q_delete_dom_user(&q
, user_pol
);
1459 if (!samr_io_q_delete_dom_user("", &q
, &qbuf
, 0) ||
1460 !rpc_api_pipe_req(cli
, SAMR_DELETE_DOM_USER
, &qbuf
, &rbuf
)) {
1464 /* Unmarshall response */
1466 if (!samr_io_r_delete_dom_user("", &r
, &rbuf
, 0)) {
1470 /* Return output parameters */
1475 prs_mem_free(&qbuf
);
1476 prs_mem_free(&rbuf
);
1481 /* Query user security object */
1483 NTSTATUS
cli_samr_query_sec_obj(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1484 POLICY_HND
*user_pol
, uint16 switch_value
,
1485 TALLOC_CTX
*ctx
, SEC_DESC_BUF
**sec_desc_buf
)
1487 prs_struct qbuf
, rbuf
;
1488 SAMR_Q_QUERY_SEC_OBJ q
;
1489 SAMR_R_QUERY_SEC_OBJ r
;
1490 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1492 DEBUG(10,("cli_samr_query_sec_obj\n"));
1497 /* Initialise parse structures */
1499 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1500 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1502 /* Marshall data and send request */
1504 init_samr_q_query_sec_obj(&q
, user_pol
, switch_value
);
1506 if (!samr_io_q_query_sec_obj("", &q
, &qbuf
, 0) ||
1507 !rpc_api_pipe_req(cli
, SAMR_QUERY_SEC_OBJECT
, &qbuf
, &rbuf
)) {
1511 /* Unmarshall response */
1513 if (!samr_io_r_query_sec_obj("", &r
, &rbuf
, 0)) {
1517 /* Return output parameters */
1520 *sec_desc_buf
=dup_sec_desc_buf(ctx
, r
.buf
);
1523 prs_mem_free(&qbuf
);
1524 prs_mem_free(&rbuf
);
1529 /* Get domain password info */
1531 NTSTATUS
cli_samr_get_dom_pwinfo(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1532 uint16
*unk_0
, uint16
*unk_1
, uint16
*unk_2
)
1534 prs_struct qbuf
, rbuf
;
1535 SAMR_Q_GET_DOM_PWINFO q
;
1536 SAMR_R_GET_DOM_PWINFO r
;
1537 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1539 DEBUG(10,("cli_samr_get_dom_pwinfo\n"));
1544 /* Initialise parse structures */
1546 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1547 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1549 /* Marshall data and send request */
1551 init_samr_q_get_dom_pwinfo(&q
, cli
->desthost
);
1553 if (!samr_io_q_get_dom_pwinfo("", &q
, &qbuf
, 0) ||
1554 !rpc_api_pipe_req(cli
, SAMR_GET_DOM_PWINFO
, &qbuf
, &rbuf
))
1557 /* Unmarshall response */
1559 if (!samr_io_r_get_dom_pwinfo("", &r
, &rbuf
, 0))
1562 /* Return output parameters */
1566 if (NT_STATUS_IS_OK(result
)) {
1576 prs_mem_free(&qbuf
);
1577 prs_mem_free(&rbuf
);
1582 /* Lookup Domain Name */
1584 NTSTATUS
cli_samr_lookup_domain(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1585 POLICY_HND
*user_pol
, char *domain_name
,
1588 prs_struct qbuf
, rbuf
;
1589 SAMR_Q_LOOKUP_DOMAIN q
;
1590 SAMR_R_LOOKUP_DOMAIN r
;
1591 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1593 DEBUG(10,("cli_samr_lookup_domain\n"));
1598 /* Initialise parse structures */
1600 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1601 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1603 /* Marshall data and send request */
1605 init_samr_q_lookup_domain(&q
, user_pol
, domain_name
);
1607 if (!samr_io_q_lookup_domain("", &q
, &qbuf
, 0) ||
1608 !rpc_api_pipe_req(cli
, SAMR_LOOKUP_DOMAIN
, &qbuf
, &rbuf
))
1611 /* Unmarshall response */
1613 if (!samr_io_r_lookup_domain("", &r
, &rbuf
, 0))
1616 /* Return output parameters */
1620 if (NT_STATUS_IS_OK(result
))
1621 sid_copy(sid
, &r
.dom_sid
.sid
);
1624 prs_mem_free(&qbuf
);
1625 prs_mem_free(&rbuf
);