2 Unix SMB/CIFS implementation.
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 3 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, see <http://www.gnu.org/licenses/>.
25 /** @defgroup lsa LSA - Local Security Architecture
34 * RPC client routines for the LSA RPC pipe. LSA means "local
35 * security authority", which is half of a password database.
38 /** Open a LSA policy handle
40 * @param cli Handle on an initialised SMB connection */
42 NTSTATUS
rpccli_lsa_open_policy(struct rpc_pipe_client
*cli
,
44 BOOL sec_qos
, uint32 des_access
,
47 prs_struct qbuf
, rbuf
;
56 /* Initialise input parameters */
59 init_lsa_sec_qos(&qos
, 2, 1, 0);
60 init_q_open_pol(&q
, '\\', 0, des_access
, &qos
);
62 init_q_open_pol(&q
, '\\', 0, des_access
, NULL
);
65 /* Marshall data and send request */
67 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_OPENPOLICY
,
72 NT_STATUS_UNSUCCESSFUL
);
74 /* Return output parameters */
78 if (NT_STATUS_IS_OK(result
)) {
85 /** Open a LSA policy handle
87 * @param cli Handle on an initialised SMB connection
90 NTSTATUS
rpccli_lsa_open_policy2(struct rpc_pipe_client
*cli
,
91 TALLOC_CTX
*mem_ctx
, BOOL sec_qos
,
92 uint32 des_access
, POLICY_HND
*pol
)
94 prs_struct qbuf
, rbuf
;
99 char *srv_name_slash
= talloc_asprintf(mem_ctx
, "\\\\%s", cli
->cli
->desthost
);
105 init_lsa_sec_qos(&qos
, 2, 1, 0);
106 init_q_open_pol2(&q
, srv_name_slash
, 0, des_access
, &qos
);
108 init_q_open_pol2(&q
, srv_name_slash
, 0, des_access
, NULL
);
111 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_OPENPOLICY2
,
116 NT_STATUS_UNSUCCESSFUL
);
118 /* Return output parameters */
122 if (NT_STATUS_IS_OK(result
)) {
129 /* Lookup a list of sids
131 * internal version withOUT memory allocation of the target arrays.
132 * this assumes suffciently sized arrays to store domains, names and types. */
134 static NTSTATUS
rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client
*cli
,
141 enum lsa_SidType
*types
)
143 prs_struct qbuf
, rbuf
;
147 NTSTATUS result
= NT_STATUS_OK
;
148 TALLOC_CTX
*tmp_ctx
= NULL
;
151 tmp_ctx
= talloc_new(mem_ctx
);
153 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
154 result
= NT_STATUS_UNSUCCESSFUL
;
161 init_q_lookup_sids(tmp_ctx
, &q
, pol
, num_sids
, sids
, 1);
167 CLI_DO_RPC( cli
, tmp_ctx
, PI_LSARPC
, LSA_LOOKUPSIDS
,
170 lsa_io_q_lookup_sids
,
171 lsa_io_r_lookup_sids
,
172 NT_STATUS_UNSUCCESSFUL
);
174 if (!NT_STATUS_IS_OK(r
.status
) &&
175 !NT_STATUS_EQUAL(r
.status
, STATUS_SOME_UNMAPPED
))
177 /* An actual error occured */
182 /* Return output parameters */
184 if (r
.mapped_count
== 0) {
185 for (i
= 0; i
< num_sids
; i
++) {
188 (types
)[i
] = SID_NAME_UNKNOWN
;
190 result
= NT_STATUS_NONE_MAPPED
;
194 for (i
= 0; i
< num_sids
; i
++) {
195 fstring name
, dom_name
;
196 uint32 dom_idx
= r
.names
.name
[i
].domain_idx
;
198 /* Translate optimised name through domain index array */
200 if (dom_idx
!= 0xffffffff) {
202 rpcstr_pull_unistr2_fstring(
203 dom_name
, &ref
.ref_dom
[dom_idx
].uni_dom_name
);
204 rpcstr_pull_unistr2_fstring(
205 name
, &r
.names
.uni_name
[i
]);
207 (names
)[i
] = talloc_strdup(mem_ctx
, name
);
208 (domains
)[i
] = talloc_strdup(mem_ctx
, dom_name
);
209 (types
)[i
] = (enum lsa_SidType
)r
.names
.name
[i
].sid_name_use
;
211 if (((names
)[i
] == NULL
) || ((domains
)[i
] == NULL
)) {
212 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
213 result
= NT_STATUS_UNSUCCESSFUL
;
220 (types
)[i
] = SID_NAME_UNKNOWN
;
225 TALLOC_FREE(tmp_ctx
);
229 /* Lookup a list of sids
231 * do it the right way: there is a limit (of 20480 for w2k3) entries
232 * returned by this call. when the sids list contains more entries,
233 * empty lists are returned. This version of lsa_lookup_sids passes
234 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
236 /* This constant defines the limit of how many sids to look up
237 * in one call (maximum). the limit from the server side is
238 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
239 #define LOOKUP_SIDS_HUNK_SIZE 1000
241 NTSTATUS
rpccli_lsa_lookup_sids_all(struct rpc_pipe_client
*cli
,
248 enum lsa_SidType
**types
)
250 NTSTATUS result
= NT_STATUS_OK
;
252 int sids_processed
= 0;
253 const DOM_SID
*hunk_sids
= sids
;
254 char **hunk_domains
= NULL
;
255 char **hunk_names
= NULL
;
256 enum lsa_SidType
*hunk_types
= NULL
;
259 if (!((*domains
) = TALLOC_ARRAY(mem_ctx
, char *, num_sids
))) {
260 DEBUG(0, ("rpccli_lsa_lookup_sids_all(): out of memory\n"));
261 result
= NT_STATUS_NO_MEMORY
;
265 if (!((*names
) = TALLOC_ARRAY(mem_ctx
, char *, num_sids
))) {
266 DEBUG(0, ("rpccli_lsa_lookup_sids_all(): out of memory\n"));
267 result
= NT_STATUS_NO_MEMORY
;
271 if (!((*types
) = TALLOC_ARRAY(mem_ctx
, enum lsa_SidType
, num_sids
))) {
272 DEBUG(0, ("rpccli_lsa_lookup_sids_all(): out of memory\n"));
273 result
= NT_STATUS_NO_MEMORY
;
282 sids_left
= num_sids
;
283 hunk_domains
= *domains
;
287 while (sids_left
> 0) {
289 NTSTATUS hunk_result
= NT_STATUS_OK
;
291 hunk_num_sids
= ((sids_left
> LOOKUP_SIDS_HUNK_SIZE
)
292 ? LOOKUP_SIDS_HUNK_SIZE
295 DEBUG(10, ("rpccli_lsa_lookup_sids_all: processing items "
298 sids_processed
+ hunk_num_sids
- 1,
301 hunk_result
= rpccli_lsa_lookup_sids_noalloc(cli
,
310 if (!NT_STATUS_IS_OK(hunk_result
) &&
311 !NT_STATUS_EQUAL(hunk_result
, STATUS_SOME_UNMAPPED
) &&
312 !NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
))
314 /* An actual error occured */
315 result
= hunk_result
;
319 /* adapt overall result */
320 if (( NT_STATUS_IS_OK(result
) &&
321 !NT_STATUS_IS_OK(hunk_result
))
323 ( NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) &&
324 !NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
)))
326 result
= STATUS_SOME_UNMAPPED
;
329 sids_left
-= hunk_num_sids
;
330 sids_processed
+= hunk_num_sids
; /* only used in DEBUG */
331 hunk_sids
+= hunk_num_sids
;
332 hunk_domains
+= hunk_num_sids
;
333 hunk_names
+= hunk_num_sids
;
334 hunk_types
+= hunk_num_sids
;
340 TALLOC_FREE(*domains
);
346 /** Lookup a list of sids */
348 NTSTATUS
rpccli_lsa_lookup_sids(struct rpc_pipe_client
*cli
,
350 POLICY_HND
*pol
, int num_sids
,
354 enum lsa_SidType
**types
)
356 prs_struct qbuf
, rbuf
;
360 NTSTATUS result
= NT_STATUS_OK
;
366 init_q_lookup_sids(mem_ctx
, &q
, pol
, num_sids
, sids
, 1);
372 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_LOOKUPSIDS
,
375 lsa_io_q_lookup_sids
,
376 lsa_io_r_lookup_sids
,
377 NT_STATUS_UNSUCCESSFUL
);
379 if (!NT_STATUS_IS_OK(r
.status
) &&
380 !NT_STATUS_EQUAL(r
.status
, STATUS_SOME_UNMAPPED
)) {
382 /* An actual error occured */
388 /* Return output parameters */
390 if (r
.mapped_count
== 0) {
391 result
= NT_STATUS_NONE_MAPPED
;
396 if (!((*domains
) = TALLOC_ARRAY(mem_ctx
, char *, num_sids
))) {
397 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
398 result
= NT_STATUS_NO_MEMORY
;
402 if (!((*names
) = TALLOC_ARRAY(mem_ctx
, char *, num_sids
))) {
403 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
404 result
= NT_STATUS_NO_MEMORY
;
408 if (!((*types
) = TALLOC_ARRAY(mem_ctx
, enum lsa_SidType
, num_sids
))) {
409 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
410 result
= NT_STATUS_NO_MEMORY
;
419 for (i
= 0; i
< num_sids
; i
++) {
420 fstring name
, dom_name
;
421 uint32 dom_idx
= r
.names
.name
[i
].domain_idx
;
423 /* Translate optimised name through domain index array */
425 if (dom_idx
!= 0xffffffff) {
427 rpcstr_pull_unistr2_fstring(
428 dom_name
, &ref
.ref_dom
[dom_idx
].uni_dom_name
);
429 rpcstr_pull_unistr2_fstring(
430 name
, &r
.names
.uni_name
[i
]);
432 (*names
)[i
] = talloc_strdup(mem_ctx
, name
);
433 (*domains
)[i
] = talloc_strdup(mem_ctx
, dom_name
);
434 (*types
)[i
] = (enum lsa_SidType
)r
.names
.name
[i
].sid_name_use
;
436 if (((*names
)[i
] == NULL
) || ((*domains
)[i
] == NULL
)) {
437 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
438 result
= NT_STATUS_UNSUCCESSFUL
;
444 (*domains
)[i
] = NULL
;
445 (*types
)[i
] = SID_NAME_UNKNOWN
;
454 /** Lookup a list of names */
456 NTSTATUS
rpccli_lsa_lookup_names(struct rpc_pipe_client
*cli
,
458 POLICY_HND
*pol
, int num_names
,
460 const char ***dom_names
,
463 enum lsa_SidType
**types
)
465 prs_struct qbuf
, rbuf
;
466 LSA_Q_LOOKUP_NAMES q
;
467 LSA_R_LOOKUP_NAMES r
;
478 init_q_lookup_names(mem_ctx
, &q
, pol
, num_names
, names
, level
);
480 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_LOOKUPNAMES
,
483 lsa_io_q_lookup_names
,
484 lsa_io_r_lookup_names
,
485 NT_STATUS_UNSUCCESSFUL
);
489 if (!NT_STATUS_IS_OK(result
) && NT_STATUS_V(result
) !=
490 NT_STATUS_V(STATUS_SOME_UNMAPPED
)) {
492 /* An actual error occured */
497 /* Return output parameters */
499 if (r
.mapped_count
== 0) {
500 result
= NT_STATUS_NONE_MAPPED
;
505 if (!((*sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, num_names
)))) {
506 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
507 result
= NT_STATUS_NO_MEMORY
;
511 if (!((*types
= TALLOC_ARRAY(mem_ctx
, enum lsa_SidType
, num_names
)))) {
512 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
513 result
= NT_STATUS_NO_MEMORY
;
517 if (dom_names
!= NULL
) {
518 *dom_names
= TALLOC_ARRAY(mem_ctx
, const char *, num_names
);
519 if (*dom_names
== NULL
) {
520 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
521 result
= NT_STATUS_NO_MEMORY
;
528 if (dom_names
!= NULL
) {
533 for (i
= 0; i
< num_names
; i
++) {
534 DOM_RID
*t_rids
= r
.dom_rid
;
535 uint32 dom_idx
= t_rids
[i
].rid_idx
;
536 uint32 dom_rid
= t_rids
[i
].rid
;
537 DOM_SID
*sid
= &(*sids
)[i
];
539 /* Translate optimised sid through domain index array */
541 if (dom_idx
== 0xffffffff) {
542 /* Nothing to do, this is unknown */
544 (*types
)[i
] = SID_NAME_UNKNOWN
;
548 sid_copy(sid
, &ref
.ref_dom
[dom_idx
].ref_dom
.sid
);
550 if (dom_rid
!= 0xffffffff) {
551 sid_append_rid(sid
, dom_rid
);
554 (*types
)[i
] = (enum lsa_SidType
)t_rids
[i
].type
;
556 if (dom_names
== NULL
) {
560 (*dom_names
)[i
] = rpcstr_pull_unistr2_talloc(
561 *dom_names
, &ref
.ref_dom
[dom_idx
].uni_dom_name
);
569 NTSTATUS
rpccli_lsa_query_info_policy_new(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
570 POLICY_HND
*pol
, uint16 info_class
,
573 prs_struct qbuf
, rbuf
;
581 init_q_query(&q
, pol
, info_class
);
583 CLI_DO_RPC(cli
, mem_ctx
, PI_LSARPC
, LSA_QUERYINFOPOLICY
,
588 NT_STATUS_UNSUCCESSFUL
);
592 if (!NT_STATUS_IS_OK(result
)) {
603 NTSTATUS
rpccli_lsa_query_info_policy2_new(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
604 POLICY_HND
*pol
, uint16 info_class
,
607 prs_struct qbuf
, rbuf
;
615 init_q_query2(&q
, pol
, info_class
);
617 CLI_DO_RPC(cli
, mem_ctx
, PI_LSARPC
, LSA_QUERYINFO2
,
620 lsa_io_q_query_info2
,
621 lsa_io_r_query_info2
,
622 NT_STATUS_UNSUCCESSFUL
);
626 if (!NT_STATUS_IS_OK(result
)) {
639 /** Query info policy
641 * @param domain_sid - returned remote server's domain sid */
643 NTSTATUS
rpccli_lsa_query_info_policy(struct rpc_pipe_client
*cli
,
645 POLICY_HND
*pol
, uint16 info_class
,
646 char **domain_name
, DOM_SID
**domain_sid
)
648 prs_struct qbuf
, rbuf
;
656 init_q_query(&q
, pol
, info_class
);
658 CLI_DO_RPC(cli
, mem_ctx
, PI_LSARPC
, LSA_QUERYINFOPOLICY
,
663 NT_STATUS_UNSUCCESSFUL
);
667 if (!NT_STATUS_IS_OK(result
)) {
671 /* Return output parameters */
673 switch (info_class
) {
676 if (domain_name
&& (r
.ctr
.info
.id3
.buffer_dom_name
!= 0)) {
677 *domain_name
= unistr2_tdup(mem_ctx
,
681 return NT_STATUS_NO_MEMORY
;
685 if (domain_sid
&& (r
.ctr
.info
.id3
.buffer_dom_sid
!= 0)) {
686 *domain_sid
= TALLOC_P(mem_ctx
, DOM_SID
);
688 return NT_STATUS_NO_MEMORY
;
690 sid_copy(*domain_sid
, &r
.ctr
.info
.id3
.dom_sid
.sid
);
697 if (domain_name
&& (r
.ctr
.info
.id5
.buffer_dom_name
!= 0)) {
698 *domain_name
= unistr2_tdup(mem_ctx
,
702 return NT_STATUS_NO_MEMORY
;
706 if (domain_sid
&& (r
.ctr
.info
.id5
.buffer_dom_sid
!= 0)) {
707 *domain_sid
= TALLOC_P(mem_ctx
, DOM_SID
);
709 return NT_STATUS_NO_MEMORY
;
711 sid_copy(*domain_sid
, &r
.ctr
.info
.id5
.dom_sid
.sid
);
716 DEBUG(3, ("unknown info class %d\n", info_class
));
725 /** Query info policy2
727 * @param domain_name - returned remote server's domain name
728 * @param dns_name - returned remote server's dns domain name
729 * @param forest_name - returned remote server's forest name
730 * @param domain_guid - returned remote server's domain guid
731 * @param domain_sid - returned remote server's domain sid */
733 NTSTATUS
rpccli_lsa_query_info_policy2(struct rpc_pipe_client
*cli
,
735 POLICY_HND
*pol
, uint16 info_class
,
736 char **domain_name
, char **dns_name
,
738 struct GUID
**domain_guid
,
739 DOM_SID
**domain_sid
)
741 prs_struct qbuf
, rbuf
;
744 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
746 if (info_class
!= 12)
752 init_q_query2(&q
, pol
, info_class
);
754 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_QUERYINFO2
,
757 lsa_io_q_query_info2
,
758 lsa_io_r_query_info2
,
759 NT_STATUS_UNSUCCESSFUL
);
763 if (!NT_STATUS_IS_OK(result
)) {
767 /* Return output parameters */
769 ZERO_STRUCTP(domain_guid
);
771 if (domain_name
&& r
.ctr
.info
.id12
.hdr_nb_dom_name
.buffer
) {
772 *domain_name
= unistr2_tdup(mem_ctx
,
776 return NT_STATUS_NO_MEMORY
;
779 if (dns_name
&& r
.ctr
.info
.id12
.hdr_dns_dom_name
.buffer
) {
780 *dns_name
= unistr2_tdup(mem_ctx
,
784 return NT_STATUS_NO_MEMORY
;
787 if (forest_name
&& r
.ctr
.info
.id12
.hdr_forest_name
.buffer
) {
788 *forest_name
= unistr2_tdup(mem_ctx
,
792 return NT_STATUS_NO_MEMORY
;
797 *domain_guid
= TALLOC_P(mem_ctx
, struct GUID
);
799 return NT_STATUS_NO_MEMORY
;
802 &r
.ctr
.info
.id12
.dom_guid
,
803 sizeof(struct GUID
));
806 if (domain_sid
&& r
.ctr
.info
.id12
.ptr_dom_sid
!= 0) {
807 *domain_sid
= TALLOC_P(mem_ctx
, DOM_SID
);
809 return NT_STATUS_NO_MEMORY
;
811 sid_copy(*domain_sid
,
812 &r
.ctr
.info
.id12
.dom_sid
.sid
);
820 NTSTATUS
rpccli_lsa_set_info_policy(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
821 POLICY_HND
*pol
, uint16 info_class
,
824 prs_struct qbuf
, rbuf
;
832 init_q_set(&q
, pol
, info_class
, ctr
);
834 CLI_DO_RPC(cli
, mem_ctx
, PI_LSARPC
, LSA_SETINFOPOLICY
,
839 NT_STATUS_UNSUCCESSFUL
);
843 if (!NT_STATUS_IS_OK(result
)) {
847 /* Return output parameters */
856 * Enumerate list of trusted domains
858 * @param cli client state (cli_state) structure of the connection
859 * @param mem_ctx memory context
860 * @param pol opened lsa policy handle
861 * @param enum_ctx enumeration context ie. index of first returned domain entry
862 * @param pref_num_domains preferred max number of entries returned in one response
863 * @param num_domains total number of trusted domains returned by response
864 * @param domain_names returned trusted domain names
865 * @param domain_sids returned trusted domain sids
867 * @return nt status code of response
870 NTSTATUS
rpccli_lsa_enum_trust_dom(struct rpc_pipe_client
*cli
,
872 POLICY_HND
*pol
, uint32
*enum_ctx
,
874 char ***domain_names
, DOM_SID
**domain_sids
)
876 prs_struct qbuf
, rbuf
;
877 LSA_Q_ENUM_TRUST_DOM in
;
878 LSA_R_ENUM_TRUST_DOM out
;
885 /* 64k is enough for about 2000 trusted domains */
887 init_q_enum_trust_dom(&in
, pol
, *enum_ctx
, 0x10000);
889 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_ENUMTRUSTDOM
,
892 lsa_io_q_enum_trust_dom
,
893 lsa_io_r_enum_trust_dom
,
894 NT_STATUS_UNSUCCESSFUL
);
897 /* check for an actual error */
899 if ( !NT_STATUS_IS_OK(out
.status
)
900 && !NT_STATUS_EQUAL(out
.status
, NT_STATUS_NO_MORE_ENTRIES
)
901 && !NT_STATUS_EQUAL(out
.status
, STATUS_MORE_ENTRIES
) )
906 /* Return output parameters */
908 *num_domains
= out
.count
;
909 *enum_ctx
= out
.enum_context
;
913 /* Allocate memory for trusted domain names and sids */
915 if ( !(*domain_names
= TALLOC_ARRAY(mem_ctx
, char *, out
.count
)) ) {
916 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
917 return NT_STATUS_NO_MEMORY
;
920 if ( !(*domain_sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, out
.count
)) ) {
921 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
922 return NT_STATUS_NO_MEMORY
;
925 /* Copy across names and sids */
927 for (i
= 0; i
< out
.count
; i
++) {
929 rpcstr_pull( tmp
, out
.domlist
->domains
[i
].name
.string
->buffer
,
930 sizeof(tmp
), out
.domlist
->domains
[i
].name
.length
, 0);
931 (*domain_names
)[i
] = talloc_strdup(mem_ctx
, tmp
);
933 sid_copy(&(*domain_sids
)[i
], &out
.domlist
->domains
[i
].sid
->sid
);
940 /** Enumerate privileges*/
942 NTSTATUS
rpccli_lsa_enum_privilege(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
943 POLICY_HND
*pol
, uint32
*enum_context
, uint32 pref_max_length
,
944 uint32
*count
, char ***privs_name
, uint32
**privs_high
, uint32
**privs_low
)
946 prs_struct qbuf
, rbuf
;
955 init_q_enum_privs(&q
, pol
, *enum_context
, pref_max_length
);
957 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_ENUM_PRIVS
,
962 NT_STATUS_UNSUCCESSFUL
);
966 if (!NT_STATUS_IS_OK(result
)) {
970 /* Return output parameters */
972 *enum_context
= r
.enum_context
;
976 if (!((*privs_name
= TALLOC_ARRAY(mem_ctx
, char *, r
.count
)))) {
977 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
978 result
= NT_STATUS_UNSUCCESSFUL
;
982 if (!((*privs_high
= TALLOC_ARRAY(mem_ctx
, uint32
, r
.count
)))) {
983 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
984 result
= NT_STATUS_UNSUCCESSFUL
;
988 if (!((*privs_low
= TALLOC_ARRAY(mem_ctx
, uint32
, r
.count
)))) {
989 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
990 result
= NT_STATUS_UNSUCCESSFUL
;
999 for (i
= 0; i
< r
.count
; i
++) {
1002 rpcstr_pull_unistr2_fstring( name
, &r
.privs
[i
].name
);
1004 (*privs_name
)[i
] = talloc_strdup(mem_ctx
, name
);
1006 (*privs_high
)[i
] = r
.privs
[i
].luid_high
;
1007 (*privs_low
)[i
] = r
.privs
[i
].luid_low
;
1015 /** Get privilege name */
1017 NTSTATUS
rpccli_lsa_get_dispname(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1018 POLICY_HND
*pol
, const char *name
,
1019 uint16 lang_id
, uint16 lang_id_sys
,
1020 fstring description
, uint16
*lang_id_desc
)
1022 prs_struct qbuf
, rbuf
;
1023 LSA_Q_PRIV_GET_DISPNAME q
;
1024 LSA_R_PRIV_GET_DISPNAME r
;
1030 init_lsa_priv_get_dispname(&q
, pol
, name
, lang_id
, lang_id_sys
);
1032 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_PRIV_GET_DISPNAME
,
1035 lsa_io_q_priv_get_dispname
,
1036 lsa_io_r_priv_get_dispname
,
1037 NT_STATUS_UNSUCCESSFUL
);
1041 if (!NT_STATUS_IS_OK(result
)) {
1045 /* Return output parameters */
1047 rpcstr_pull_unistr2_fstring(description
, &r
.desc
);
1048 *lang_id_desc
= r
.lang_id
;
1055 /** Enumerate list of SIDs */
1057 NTSTATUS
rpccli_lsa_enum_sids(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1058 POLICY_HND
*pol
, uint32
*enum_ctx
, uint32 pref_max_length
,
1059 uint32
*num_sids
, DOM_SID
**sids
)
1061 prs_struct qbuf
, rbuf
;
1062 LSA_Q_ENUM_ACCOUNTS q
;
1063 LSA_R_ENUM_ACCOUNTS r
;
1070 init_lsa_q_enum_accounts(&q
, pol
, *enum_ctx
, pref_max_length
);
1072 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_ENUM_ACCOUNTS
,
1075 lsa_io_q_enum_accounts
,
1076 lsa_io_r_enum_accounts
,
1077 NT_STATUS_UNSUCCESSFUL
);
1081 if (!NT_STATUS_IS_OK(result
)) {
1085 if (r
.sids
.num_entries
==0)
1088 /* Return output parameters */
1090 *sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, r
.sids
.num_entries
);
1092 DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
1093 result
= NT_STATUS_UNSUCCESSFUL
;
1097 /* Copy across names and sids */
1099 for (i
= 0; i
< r
.sids
.num_entries
; i
++) {
1100 sid_copy(&(*sids
)[i
], &r
.sids
.sid
[i
].sid
);
1103 *num_sids
= r
.sids
.num_entries
;
1104 *enum_ctx
= r
.enum_context
;
1111 /** Create a LSA user handle
1113 * @param cli Handle on an initialised SMB connection
1115 * FIXME: The code is actually identical to open account
1116 * TODO: Check and code what the function should exactly do
1120 NTSTATUS
rpccli_lsa_create_account(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1121 POLICY_HND
*dom_pol
, DOM_SID
*sid
, uint32 desired_access
,
1122 POLICY_HND
*user_pol
)
1124 prs_struct qbuf
, rbuf
;
1125 LSA_Q_CREATEACCOUNT q
;
1126 LSA_R_CREATEACCOUNT r
;
1132 /* Initialise input parameters */
1134 init_lsa_q_create_account(&q
, dom_pol
, sid
, desired_access
);
1136 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_CREATEACCOUNT
,
1139 lsa_io_q_create_account
,
1140 lsa_io_r_create_account
,
1141 NT_STATUS_UNSUCCESSFUL
);
1143 /* Return output parameters */
1147 if (NT_STATUS_IS_OK(result
)) {
1154 /** Open a LSA user handle
1156 * @param cli Handle on an initialised SMB connection */
1158 NTSTATUS
rpccli_lsa_open_account(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1159 POLICY_HND
*dom_pol
, DOM_SID
*sid
, uint32 des_access
,
1160 POLICY_HND
*user_pol
)
1162 prs_struct qbuf
, rbuf
;
1163 LSA_Q_OPENACCOUNT q
;
1164 LSA_R_OPENACCOUNT r
;
1170 /* Initialise input parameters */
1172 init_lsa_q_open_account(&q
, dom_pol
, sid
, des_access
);
1174 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_OPENACCOUNT
,
1177 lsa_io_q_open_account
,
1178 lsa_io_r_open_account
,
1179 NT_STATUS_UNSUCCESSFUL
);
1181 /* Return output parameters */
1185 if (NT_STATUS_IS_OK(result
)) {
1192 /** Enumerate user privileges
1194 * @param cli Handle on an initialised SMB connection */
1196 NTSTATUS
rpccli_lsa_enum_privsaccount(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1197 POLICY_HND
*pol
, uint32
*count
, LUID_ATTR
**set
)
1199 prs_struct qbuf
, rbuf
;
1200 LSA_Q_ENUMPRIVSACCOUNT q
;
1201 LSA_R_ENUMPRIVSACCOUNT r
;
1208 /* Initialise input parameters */
1210 init_lsa_q_enum_privsaccount(&q
, pol
);
1212 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_ENUMPRIVSACCOUNT
,
1215 lsa_io_q_enum_privsaccount
,
1216 lsa_io_r_enum_privsaccount
,
1217 NT_STATUS_UNSUCCESSFUL
);
1219 /* Return output parameters */
1223 if (!NT_STATUS_IS_OK(result
)) {
1230 if (!((*set
= TALLOC_ARRAY(mem_ctx
, LUID_ATTR
, r
.count
)))) {
1231 DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
1232 result
= NT_STATUS_UNSUCCESSFUL
;
1236 for (i
=0; i
<r
.count
; i
++) {
1237 (*set
)[i
].luid
.low
= r
.set
.set
[i
].luid
.low
;
1238 (*set
)[i
].luid
.high
= r
.set
.set
[i
].luid
.high
;
1239 (*set
)[i
].attr
= r
.set
.set
[i
].attr
;
1248 /** Get a privilege value given its name */
1250 NTSTATUS
rpccli_lsa_lookup_priv_value(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1251 POLICY_HND
*pol
, const char *name
, LUID
*luid
)
1253 prs_struct qbuf
, rbuf
;
1254 LSA_Q_LOOKUP_PRIV_VALUE q
;
1255 LSA_R_LOOKUP_PRIV_VALUE r
;
1261 /* Marshall data and send request */
1263 init_lsa_q_lookup_priv_value(&q
, pol
, name
);
1265 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_LOOKUPPRIVVALUE
,
1268 lsa_io_q_lookup_priv_value
,
1269 lsa_io_r_lookup_priv_value
,
1270 NT_STATUS_UNSUCCESSFUL
);
1274 if (!NT_STATUS_IS_OK(result
)) {
1278 /* Return output parameters */
1280 (*luid
).low
=r
.luid
.low
;
1281 (*luid
).high
=r
.luid
.high
;
1288 /** Query LSA security object */
1290 NTSTATUS
rpccli_lsa_query_secobj(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1291 POLICY_HND
*pol
, uint32 sec_info
,
1292 SEC_DESC_BUF
**psdb
)
1294 prs_struct qbuf
, rbuf
;
1295 LSA_Q_QUERY_SEC_OBJ q
;
1296 LSA_R_QUERY_SEC_OBJ r
;
1302 /* Marshall data and send request */
1304 init_q_query_sec_obj(&q
, pol
, sec_info
);
1306 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_QUERYSECOBJ
,
1309 lsa_io_q_query_sec_obj
,
1310 lsa_io_r_query_sec_obj
,
1311 NT_STATUS_UNSUCCESSFUL
);
1315 if (!NT_STATUS_IS_OK(result
)) {
1319 /* Return output parameters */
1330 /* Enumerate account rights This is similar to enum_privileges but
1331 takes a SID directly, avoiding the open_account call.
1334 NTSTATUS
rpccli_lsa_enum_account_rights(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1335 POLICY_HND
*pol
, DOM_SID
*sid
,
1336 uint32
*count
, char ***priv_names
)
1338 prs_struct qbuf
, rbuf
;
1339 LSA_Q_ENUM_ACCT_RIGHTS q
;
1340 LSA_R_ENUM_ACCT_RIGHTS r
;
1343 fstring
*privileges
;
1349 /* Marshall data and send request */
1350 init_q_enum_acct_rights(&q
, pol
, 2, sid
);
1352 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_ENUMACCTRIGHTS
,
1355 lsa_io_q_enum_acct_rights
,
1356 lsa_io_r_enum_acct_rights
,
1357 NT_STATUS_UNSUCCESSFUL
);
1361 if (!NT_STATUS_IS_OK(result
)) {
1371 privileges
= TALLOC_ARRAY( mem_ctx
, fstring
, *count
);
1372 names
= TALLOC_ARRAY( mem_ctx
, char *, *count
);
1374 if ((privileges
== NULL
) || (names
== NULL
)) {
1375 TALLOC_FREE(privileges
);
1377 return NT_STATUS_NO_MEMORY
;
1380 for ( i
=0; i
<*count
; i
++ ) {
1381 UNISTR4
*uni_string
= &r
.rights
->strings
[i
];
1383 if ( !uni_string
->string
)
1386 rpcstr_pull( privileges
[i
], uni_string
->string
->buffer
, sizeof(privileges
[i
]), -1, STR_TERMINATE
);
1388 /* now copy to the return array */
1389 names
[i
] = talloc_strdup( mem_ctx
, privileges
[i
] );
1392 *priv_names
= names
;
1401 /* add account rights to an account. */
1403 NTSTATUS
rpccli_lsa_add_account_rights(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1404 POLICY_HND
*pol
, DOM_SID sid
,
1405 uint32 count
, const char **privs_name
)
1407 prs_struct qbuf
, rbuf
;
1408 LSA_Q_ADD_ACCT_RIGHTS q
;
1409 LSA_R_ADD_ACCT_RIGHTS r
;
1415 /* Marshall data and send request */
1416 init_q_add_acct_rights(&q
, pol
, &sid
, count
, privs_name
);
1418 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_ADDACCTRIGHTS
,
1421 lsa_io_q_add_acct_rights
,
1422 lsa_io_r_add_acct_rights
,
1423 NT_STATUS_UNSUCCESSFUL
);
1427 if (!NT_STATUS_IS_OK(result
)) {
1436 /* remove account rights for an account. */
1438 NTSTATUS
rpccli_lsa_remove_account_rights(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1439 POLICY_HND
*pol
, DOM_SID sid
, BOOL removeall
,
1440 uint32 count
, const char **privs_name
)
1442 prs_struct qbuf
, rbuf
;
1443 LSA_Q_REMOVE_ACCT_RIGHTS q
;
1444 LSA_R_REMOVE_ACCT_RIGHTS r
;
1450 /* Marshall data and send request */
1451 init_q_remove_acct_rights(&q
, pol
, &sid
, removeall
?1:0, count
, privs_name
);
1453 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_REMOVEACCTRIGHTS
,
1456 lsa_io_q_remove_acct_rights
,
1457 lsa_io_r_remove_acct_rights
,
1458 NT_STATUS_UNSUCCESSFUL
);
1462 if (!NT_STATUS_IS_OK(result
)) {
1473 /** An example of how to use the routines in this file. Fetch a DOMAIN
1474 sid. Does complete cli setup / teardown anonymously. */
1476 BOOL
fetch_domain_sid( char *domain
, char *remote_machine
, DOM_SID
*psid
)
1478 extern pstring global_myname
;
1479 struct cli_state
*cli
;
1485 if((cli
= cli_initialise()) == NULL
) {
1486 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
1490 if(!resolve_name( remote_machine
, &cli
->dest_ip
, 0x20)) {
1491 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine
));
1495 if (!cli_connect(cli
, remote_machine
, &cli
->dest_ip
)) {
1496 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
1497 machine %s. Error was : %s.\n", remote_machine
, cli_errstr(cli
) ));
1501 if (!attempt_netbios_session_request(cli
, global_myname
, remote_machine
, &cli
->dest_ip
)) {
1502 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n",
1507 cli
->protocol
= PROTOCOL_NT1
;
1509 if (!cli_negprot(cli
)) {
1510 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
1511 Error was : %s.\n", remote_machine
, cli_errstr(cli
) ));
1515 if (cli
->protocol
!= PROTOCOL_NT1
) {
1516 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
1522 * Do an anonymous session setup.
1525 if (!cli_session_setup(cli
, "", "", 0, "", 0, "")) {
1526 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
1527 Error was : %s.\n", remote_machine
, cli_errstr(cli
) ));
1531 if (!(cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
)) {
1532 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
1537 if (!cli_send_tconX(cli
, "IPC$", "IPC", "", 1)) {
1538 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
1539 Error was : %s.\n", remote_machine
, cli_errstr(cli
) ));
1543 /* Fetch domain sid */
1545 if (!cli_nt_session_open(cli
, PI_LSARPC
)) {
1546 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
1550 result
= cli_lsa_open_policy(cli
, cli
->mem_ctx
, True
, SEC_RIGHTS_QUERY_VALUE
, &lsa_pol
);
1551 if (!NT_STATUS_IS_OK(result
)) {
1552 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
1553 nt_errstr(result
) ));
1557 result
= cli_lsa_query_info_policy(cli
, cli
->mem_ctx
, &lsa_pol
, 5, domain
, psid
);
1558 if (!NT_STATUS_IS_OK(result
)) {
1559 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
1560 nt_errstr(result
) ));
1574 NTSTATUS
rpccli_lsa_open_trusted_domain(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1575 POLICY_HND
*pol
, DOM_SID
*dom_sid
, uint32 access_mask
,
1576 POLICY_HND
*trustdom_pol
)
1578 prs_struct qbuf
, rbuf
;
1579 LSA_Q_OPEN_TRUSTED_DOMAIN q
;
1580 LSA_R_OPEN_TRUSTED_DOMAIN r
;
1586 /* Initialise input parameters */
1588 init_lsa_q_open_trusted_domain(&q
, pol
, dom_sid
, access_mask
);
1590 /* Marshall data and send request */
1592 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_OPENTRUSTDOM
,
1595 lsa_io_q_open_trusted_domain
,
1596 lsa_io_r_open_trusted_domain
,
1597 NT_STATUS_UNSUCCESSFUL
);
1599 /* Return output parameters */
1603 if (NT_STATUS_IS_OK(result
)) {
1604 *trustdom_pol
= r
.handle
;
1610 NTSTATUS
rpccli_lsa_query_trusted_domain_info(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1613 LSA_TRUSTED_DOMAIN_INFO
**info
)
1615 prs_struct qbuf
, rbuf
;
1616 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO q
;
1617 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r
;
1618 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1623 /* Marshall data and send request */
1625 init_q_query_trusted_domain_info(&q
, pol
, info_class
);
1627 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_QUERYTRUSTDOMINFO
,
1630 lsa_io_q_query_trusted_domain_info
,
1631 lsa_io_r_query_trusted_domain_info
,
1632 NT_STATUS_UNSUCCESSFUL
);
1636 if (!NT_STATUS_IS_OK(result
)) {
1646 NTSTATUS
rpccli_lsa_open_trusted_domain_by_name(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1647 POLICY_HND
*pol
, const char *name
, uint32 access_mask
,
1648 POLICY_HND
*trustdom_pol
)
1650 prs_struct qbuf
, rbuf
;
1651 LSA_Q_OPEN_TRUSTED_DOMAIN_BY_NAME q
;
1652 LSA_R_OPEN_TRUSTED_DOMAIN_BY_NAME r
;
1658 /* Initialise input parameters */
1660 init_lsa_q_open_trusted_domain_by_name(&q
, pol
, name
, access_mask
);
1662 /* Marshall data and send request */
1664 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_OPENTRUSTDOMBYNAME
,
1667 lsa_io_q_open_trusted_domain_by_name
,
1668 lsa_io_r_open_trusted_domain_by_name
,
1669 NT_STATUS_UNSUCCESSFUL
);
1671 /* Return output parameters */
1675 if (NT_STATUS_IS_OK(result
)) {
1676 *trustdom_pol
= r
.handle
;
1683 NTSTATUS
rpccli_lsa_query_trusted_domain_info_by_sid(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1685 uint16 info_class
, DOM_SID
*dom_sid
,
1686 LSA_TRUSTED_DOMAIN_INFO
**info
)
1688 prs_struct qbuf
, rbuf
;
1689 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO_BY_SID q
;
1690 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r
;
1691 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1696 /* Marshall data and send request */
1698 init_q_query_trusted_domain_info_by_sid(&q
, pol
, info_class
, dom_sid
);
1700 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_QUERYTRUSTDOMINFOBYSID
,
1703 lsa_io_q_query_trusted_domain_info_by_sid
,
1704 lsa_io_r_query_trusted_domain_info
,
1705 NT_STATUS_UNSUCCESSFUL
);
1709 if (!NT_STATUS_IS_OK(result
)) {
1720 NTSTATUS
rpccli_lsa_query_trusted_domain_info_by_name(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1722 uint16 info_class
, const char *domain_name
,
1723 LSA_TRUSTED_DOMAIN_INFO
**info
)
1725 prs_struct qbuf
, rbuf
;
1726 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO_BY_NAME q
;
1727 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r
;
1728 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1733 /* Marshall data and send request */
1735 init_q_query_trusted_domain_info_by_name(&q
, pol
, info_class
, domain_name
);
1737 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_QUERYTRUSTDOMINFOBYNAME
,
1740 lsa_io_q_query_trusted_domain_info_by_name
,
1741 lsa_io_r_query_trusted_domain_info
,
1742 NT_STATUS_UNSUCCESSFUL
);
1746 if (!NT_STATUS_IS_OK(result
)) {
1757 NTSTATUS
cli_lsa_query_domain_info_policy(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
1759 uint16 info_class
, LSA_DOM_INFO_UNION
**info
)
1761 prs_struct qbuf
, rbuf
;
1762 LSA_Q_QUERY_DOM_INFO_POLICY q
;
1763 LSA_R_QUERY_DOM_INFO_POLICY r
;
1764 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1769 /* Marshall data and send request */
1771 init_q_query_dom_info(&q
, pol
, info_class
);
1773 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_QUERYDOMINFOPOL
,
1776 lsa_io_q_query_dom_info
,
1777 lsa_io_r_query_dom_info
,
1778 NT_STATUS_UNSUCCESSFUL
);
1782 if (!NT_STATUS_IS_OK(result
)) {