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.
27 #include "rpc_client.h"
29 /** @defgroup lsa LSA - Local Security Architecture
38 * RPC client routines for the LSA RPC pipe. LSA means "local
39 * security authority", which is half of a password database.
42 /** Open a LSA policy handle
44 * @param cli Handle on an initialised SMB connection */
46 NTSTATUS
cli_lsa_open_policy(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
47 BOOL sec_qos
, uint32 des_access
, POLICY_HND
*pol
)
49 prs_struct qbuf
, rbuf
;
58 /* Initialise parse structures */
60 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
61 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
63 /* Initialise input parameters */
66 init_lsa_sec_qos(&qos
, 2, 1, 0);
67 init_q_open_pol(&q
, '\\', 0, des_access
, &qos
);
69 init_q_open_pol(&q
, '\\', 0, des_access
, NULL
);
72 /* Marshall data and send request */
74 if (!lsa_io_q_open_pol("", &q
, &qbuf
, 0) ||
75 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_OPENPOLICY
, &qbuf
, &rbuf
)) {
76 result
= NT_STATUS_UNSUCCESSFUL
;
80 /* Unmarshall response */
82 if (!lsa_io_r_open_pol("", &r
, &rbuf
, 0)) {
83 result
= NT_STATUS_UNSUCCESSFUL
;
87 /* Return output parameters */
89 if (NT_STATUS_IS_OK(result
= r
.status
)) {
92 pol
->marker
= MALLOC(1);
103 /** Open a LSA policy handle
105 * @param cli Handle on an initialised SMB connection
108 NTSTATUS
cli_lsa_open_policy2(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
109 BOOL sec_qos
, uint32 des_access
, POLICY_HND
*pol
)
111 prs_struct qbuf
, rbuf
;
120 /* Initialise parse structures */
122 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
123 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
125 /* Initialise input parameters */
128 init_lsa_sec_qos(&qos
, 2, 1, 0);
129 init_q_open_pol2(&q
, cli
->srv_name_slash
, 0, des_access
,
132 init_q_open_pol2(&q
, cli
->srv_name_slash
, 0, des_access
,
136 /* Marshall data and send request */
138 if (!lsa_io_q_open_pol2("", &q
, &qbuf
, 0) ||
139 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_OPENPOLICY2
, &qbuf
, &rbuf
)) {
140 result
= NT_STATUS_UNSUCCESSFUL
;
144 /* Unmarshall response */
146 if (!lsa_io_r_open_pol2("", &r
, &rbuf
, 0)) {
147 result
= NT_STATUS_UNSUCCESSFUL
;
151 /* Return output parameters */
153 if (NT_STATUS_IS_OK(result
= r
.status
)) {
156 pol
->marker
= (char *)malloc(1);
167 /** Close a LSA policy handle */
169 NTSTATUS
cli_lsa_close(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
172 prs_struct qbuf
, rbuf
;
180 /* Initialise parse structures */
182 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
183 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
185 /* Marshall data and send request */
187 init_lsa_q_close(&q
, pol
);
189 if (!lsa_io_q_close("", &q
, &qbuf
, 0) ||
190 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_CLOSE
, &qbuf
, &rbuf
)) {
191 result
= NT_STATUS_UNSUCCESSFUL
;
195 /* Unmarshall response */
197 if (!lsa_io_r_close("", &r
, &rbuf
, 0)) {
198 result
= NT_STATUS_UNSUCCESSFUL
;
202 /* Return output parameters */
204 if (NT_STATUS_IS_OK(result
= r
.status
)) {
206 SAFE_FREE(pol
->marker
);
218 /** Lookup a list of sids */
220 NTSTATUS
cli_lsa_lookup_sids(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
221 POLICY_HND
*pol
, int num_sids
, const DOM_SID
*sids
,
222 char ***domains
, char ***names
, uint32
**types
)
224 prs_struct qbuf
, rbuf
;
228 LSA_TRANS_NAME_ENUM t_names
;
235 /* Initialise parse structures */
237 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
238 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
240 /* Marshall data and send request */
242 init_q_lookup_sids(mem_ctx
, &q
, pol
, num_sids
, sids
, 1);
244 if (!lsa_io_q_lookup_sids("", &q
, &qbuf
, 0) ||
245 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_LOOKUPSIDS
, &qbuf
, &rbuf
)) {
246 result
= NT_STATUS_UNSUCCESSFUL
;
250 /* Unmarshall response */
253 ZERO_STRUCT(t_names
);
258 if (!lsa_io_r_lookup_sids("", &r
, &rbuf
, 0)) {
259 result
= NT_STATUS_UNSUCCESSFUL
;
265 if (!NT_STATUS_IS_OK(result
) &&
266 NT_STATUS_V(result
) != NT_STATUS_V(STATUS_SOME_UNMAPPED
)) {
268 /* An actual error occured */
273 /* Return output parameters */
275 if (r
.mapped_count
== 0) {
276 result
= NT_STATUS_NONE_MAPPED
;
280 if (!((*domains
) = TALLOC_ARRAY(mem_ctx
, char *, num_sids
))) {
281 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
282 result
= NT_STATUS_UNSUCCESSFUL
;
286 if (!((*names
) = TALLOC_ARRAY(mem_ctx
, char *, num_sids
))) {
287 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
288 result
= NT_STATUS_UNSUCCESSFUL
;
292 if (!((*types
) = TALLOC_ARRAY(mem_ctx
, uint32
, num_sids
))) {
293 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
294 result
= NT_STATUS_UNSUCCESSFUL
;
298 for (i
= 0; i
< num_sids
; i
++) {
299 fstring name
, dom_name
;
300 uint32 dom_idx
= t_names
.name
[i
].domain_idx
;
302 /* Translate optimised name through domain index array */
304 if (dom_idx
!= 0xffffffff) {
306 rpcstr_pull_unistr2_fstring(
307 dom_name
, &ref
.ref_dom
[dom_idx
].uni_dom_name
);
308 rpcstr_pull_unistr2_fstring(
309 name
, &t_names
.uni_name
[i
]);
311 (*names
)[i
] = talloc_strdup(mem_ctx
, name
);
312 (*domains
)[i
] = talloc_strdup(mem_ctx
, dom_name
);
313 (*types
)[i
] = t_names
.name
[i
].sid_name_use
;
315 if (((*names
)[i
] == NULL
) || ((*domains
)[i
] == NULL
)) {
316 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
317 result
= NT_STATUS_UNSUCCESSFUL
;
323 (*domains
)[i
] = NULL
;
324 (*types
)[i
] = SID_NAME_UNKNOWN
;
335 /** Lookup a list of names */
337 NTSTATUS
cli_lsa_lookup_names(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
338 POLICY_HND
*pol
, int num_names
,
339 const char **names
, DOM_SID
**sids
,
342 prs_struct qbuf
, rbuf
;
343 LSA_Q_LOOKUP_NAMES q
;
344 LSA_R_LOOKUP_NAMES r
;
352 /* Initialise parse structures */
354 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
355 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
357 /* Marshall data and send request */
359 init_q_lookup_names(mem_ctx
, &q
, pol
, num_names
, names
);
361 if (!lsa_io_q_lookup_names("", &q
, &qbuf
, 0) ||
362 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_LOOKUPNAMES
, &qbuf
, &rbuf
)) {
363 result
= NT_STATUS_UNSUCCESSFUL
;
367 /* Unmarshall response */
372 if (!lsa_io_r_lookup_names("", &r
, &rbuf
, 0)) {
373 result
= NT_STATUS_UNSUCCESSFUL
;
379 if (!NT_STATUS_IS_OK(result
) && NT_STATUS_V(result
) !=
380 NT_STATUS_V(STATUS_SOME_UNMAPPED
)) {
382 /* An actual error occured */
387 /* Return output parameters */
389 if (r
.mapped_count
== 0) {
390 result
= NT_STATUS_NONE_MAPPED
;
394 if (!((*sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, num_names
)))) {
395 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
396 result
= NT_STATUS_UNSUCCESSFUL
;
400 if (!((*types
= TALLOC_ARRAY(mem_ctx
, uint32
, num_names
)))) {
401 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
402 result
= NT_STATUS_UNSUCCESSFUL
;
406 for (i
= 0; i
< num_names
; i
++) {
407 DOM_RID2
*t_rids
= r
.dom_rid
;
408 uint32 dom_idx
= t_rids
[i
].rid_idx
;
409 uint32 dom_rid
= t_rids
[i
].rid
;
410 DOM_SID
*sid
= &(*sids
)[i
];
412 /* Translate optimised sid through domain index array */
414 if (dom_idx
!= 0xffffffff) {
416 sid_copy(sid
, &ref
.ref_dom
[dom_idx
].ref_dom
.sid
);
418 if (dom_rid
!= 0xffffffff) {
419 sid_append_rid(sid
, dom_rid
);
422 (*types
)[i
] = t_rids
[i
].type
;
425 (*types
)[i
] = SID_NAME_UNKNOWN
;
436 /** Query info policy
438 * @param domain_sid - returned remote server's domain sid */
440 NTSTATUS
cli_lsa_query_info_policy(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
441 POLICY_HND
*pol
, uint16 info_class
,
442 char **domain_name
, DOM_SID
**domain_sid
)
444 prs_struct qbuf
, rbuf
;
452 /* Initialise parse structures */
454 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
455 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
457 /* Marshall data and send request */
459 init_q_query(&q
, pol
, info_class
);
461 if (!lsa_io_q_query("", &q
, &qbuf
, 0) ||
462 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_QUERYINFOPOLICY
, &qbuf
, &rbuf
)) {
463 result
= NT_STATUS_UNSUCCESSFUL
;
467 /* Unmarshall response */
469 if (!lsa_io_r_query("", &r
, &rbuf
, 0)) {
470 result
= NT_STATUS_UNSUCCESSFUL
;
474 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
478 /* Return output parameters */
480 switch (info_class
) {
483 if (domain_name
&& (r
.dom
.id3
.buffer_dom_name
!= 0)) {
484 *domain_name
= unistr2_tdup(mem_ctx
,
489 if (domain_sid
&& (r
.dom
.id3
.buffer_dom_sid
!= 0)) {
490 *domain_sid
= TALLOC_P(mem_ctx
, DOM_SID
);
492 sid_copy(*domain_sid
, &r
.dom
.id3
.dom_sid
.sid
);
500 if (domain_name
&& (r
.dom
.id5
.buffer_dom_name
!= 0)) {
501 *domain_name
= unistr2_tdup(mem_ctx
,
506 if (domain_sid
&& (r
.dom
.id5
.buffer_dom_sid
!= 0)) {
507 *domain_sid
= TALLOC_P(mem_ctx
, DOM_SID
);
509 sid_copy(*domain_sid
, &r
.dom
.id5
.dom_sid
.sid
);
515 DEBUG(3, ("unknown info class %d\n", info_class
));
526 /** Query info policy2
528 * @param domain_name - returned remote server's domain name
529 * @param dns_name - returned remote server's dns domain name
530 * @param forest_name - returned remote server's forest name
531 * @param domain_guid - returned remote server's domain guid
532 * @param domain_sid - returned remote server's domain sid */
534 NTSTATUS
cli_lsa_query_info_policy2(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
535 POLICY_HND
*pol
, uint16 info_class
,
536 char **domain_name
, char **dns_name
,
537 char **forest_name
, struct uuid
**domain_guid
,
538 DOM_SID
**domain_sid
)
540 prs_struct qbuf
, rbuf
;
543 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
545 if (info_class
!= 12)
551 /* Initialise parse structures */
553 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
554 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
556 /* Marshall data and send request */
558 init_q_query2(&q
, pol
, info_class
);
560 if (!lsa_io_q_query_info2("", &q
, &qbuf
, 0) ||
561 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_QUERYINFO2
, &qbuf
, &rbuf
)) {
562 result
= NT_STATUS_UNSUCCESSFUL
;
566 /* Unmarshall response */
568 if (!lsa_io_r_query_info2("", &r
, &rbuf
, 0)) {
569 result
= NT_STATUS_UNSUCCESSFUL
;
573 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
577 /* Return output parameters */
579 ZERO_STRUCTP(domain_guid
);
581 if (domain_name
&& r
.info
.dns_dom_info
.hdr_nb_dom_name
.buffer
) {
582 *domain_name
= unistr2_tdup(mem_ctx
,
586 if (dns_name
&& r
.info
.dns_dom_info
.hdr_dns_dom_name
.buffer
) {
587 *dns_name
= unistr2_tdup(mem_ctx
,
591 if (forest_name
&& r
.info
.dns_dom_info
.hdr_forest_name
.buffer
) {
592 *forest_name
= unistr2_tdup(mem_ctx
,
598 *domain_guid
= TALLOC_P(mem_ctx
, struct uuid
);
600 &r
.info
.dns_dom_info
.dom_guid
,
601 sizeof(struct uuid
));
604 if (domain_sid
&& r
.info
.dns_dom_info
.ptr_dom_sid
!= 0) {
605 *domain_sid
= TALLOC_P(mem_ctx
, DOM_SID
);
607 sid_copy(*domain_sid
,
608 &r
.info
.dns_dom_info
.dom_sid
.sid
);
620 * Enumerate list of trusted domains
622 * @param cli client state (cli_state) structure of the connection
623 * @param mem_ctx memory context
624 * @param pol opened lsa policy handle
625 * @param enum_ctx enumeration context ie. index of first returned domain entry
626 * @param pref_num_domains preferred max number of entries returned in one response
627 * @param num_domains total number of trusted domains returned by response
628 * @param domain_names returned trusted domain names
629 * @param domain_sids returned trusted domain sids
631 * @return nt status code of response
634 NTSTATUS
cli_lsa_enum_trust_dom(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
635 POLICY_HND
*pol
, uint32
*enum_ctx
,
637 char ***domain_names
, DOM_SID
**domain_sids
)
639 prs_struct qbuf
, rbuf
;
640 LSA_Q_ENUM_TRUST_DOM in
;
641 LSA_R_ENUM_TRUST_DOM out
;
649 /* 64k is enough for about 2000 trusted domains */
651 init_q_enum_trust_dom(&in
, pol
, *enum_ctx
, 0x10000);
653 CLI_DO_RPC( cli
, mem_ctx
, PI_LSARPC
, LSA_ENUMTRUSTDOM
,
656 lsa_io_q_enum_trust_dom
,
657 lsa_io_r_enum_trust_dom
,
658 NT_STATUS_UNSUCCESSFUL
);
661 /* check for an actual error */
663 if ( !NT_STATUS_IS_OK(out
.status
)
664 && !NT_STATUS_EQUAL(out
.status
, NT_STATUS_NO_MORE_ENTRIES
)
665 && !NT_STATUS_EQUAL(out
.status
, STATUS_MORE_ENTRIES
) )
670 /* Return output parameters */
672 *num_domains
= out
.count
;
673 *enum_ctx
= out
.enum_context
;
677 /* Allocate memory for trusted domain names and sids */
679 if ( !(*domain_names
= TALLOC_ARRAY(mem_ctx
, char *, out
.count
)) ) {
680 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
681 return NT_STATUS_NO_MEMORY
;
684 if ( !(*domain_sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, out
.count
)) ) {
685 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
686 return NT_STATUS_NO_MEMORY
;
689 /* Copy across names and sids */
691 for (i
= 0; i
< out
.count
; i
++) {
693 rpcstr_pull( tmp
, out
.domlist
->domains
[i
].name
.string
->buffer
,
694 sizeof(tmp
), out
.domlist
->domains
[i
].name
.length
, 0);
695 (*domain_names
)[i
] = talloc_strdup(mem_ctx
, tmp
);
697 sid_copy(&(*domain_sids
)[i
], &out
.domlist
->domains
[i
].sid
->sid
);
705 /** Enumerate privileges*/
707 NTSTATUS
cli_lsa_enum_privilege(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
708 POLICY_HND
*pol
, uint32
*enum_context
, uint32 pref_max_length
,
709 uint32
*count
, char ***privs_name
, uint32
**privs_high
, uint32
**privs_low
)
711 prs_struct qbuf
, rbuf
;
720 /* Initialise parse structures */
722 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
723 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
725 /* Marshall data and send request */
727 init_q_enum_privs(&q
, pol
, *enum_context
, pref_max_length
);
729 if (!lsa_io_q_enum_privs("", &q
, &qbuf
, 0) ||
730 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_ENUM_PRIVS
, &qbuf
, &rbuf
)) {
731 result
= NT_STATUS_UNSUCCESSFUL
;
735 /* Unmarshall response */
737 if (!lsa_io_r_enum_privs("", &r
, &rbuf
, 0)) {
738 result
= NT_STATUS_UNSUCCESSFUL
;
742 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
746 /* Return output parameters */
748 *enum_context
= r
.enum_context
;
751 if (!((*privs_name
= TALLOC_ARRAY(mem_ctx
, char *, r
.count
)))) {
752 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
753 result
= NT_STATUS_UNSUCCESSFUL
;
757 if (!((*privs_high
= TALLOC_ARRAY(mem_ctx
, uint32
, r
.count
)))) {
758 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
759 result
= NT_STATUS_UNSUCCESSFUL
;
763 if (!((*privs_low
= TALLOC_ARRAY(mem_ctx
, uint32
, r
.count
)))) {
764 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
765 result
= NT_STATUS_UNSUCCESSFUL
;
769 for (i
= 0; i
< r
.count
; i
++) {
772 rpcstr_pull_unistr2_fstring( name
, &r
.privs
[i
].name
);
774 (*privs_name
)[i
] = talloc_strdup(mem_ctx
, name
);
776 (*privs_high
)[i
] = r
.privs
[i
].luid_high
;
777 (*privs_low
)[i
] = r
.privs
[i
].luid_low
;
787 /** Get privilege name */
789 NTSTATUS
cli_lsa_get_dispname(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
790 POLICY_HND
*pol
, const char *name
,
791 uint16 lang_id
, uint16 lang_id_sys
,
792 fstring description
, uint16
*lang_id_desc
)
794 prs_struct qbuf
, rbuf
;
795 LSA_Q_PRIV_GET_DISPNAME q
;
796 LSA_R_PRIV_GET_DISPNAME r
;
802 /* Initialise parse structures */
804 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
805 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
807 /* Marshall data and send request */
809 init_lsa_priv_get_dispname(&q
, pol
, name
, lang_id
, lang_id_sys
);
811 if (!lsa_io_q_priv_get_dispname("", &q
, &qbuf
, 0) ||
812 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_PRIV_GET_DISPNAME
, &qbuf
, &rbuf
)) {
813 result
= NT_STATUS_UNSUCCESSFUL
;
817 /* Unmarshall response */
819 if (!lsa_io_r_priv_get_dispname("", &r
, &rbuf
, 0)) {
820 result
= NT_STATUS_UNSUCCESSFUL
;
824 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
828 /* Return output parameters */
830 rpcstr_pull_unistr2_fstring(description
, &r
.desc
);
831 *lang_id_desc
= r
.lang_id
;
840 /** Enumerate list of SIDs */
842 NTSTATUS
cli_lsa_enum_sids(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
843 POLICY_HND
*pol
, uint32
*enum_ctx
, uint32 pref_max_length
,
844 uint32
*num_sids
, DOM_SID
**sids
)
846 prs_struct qbuf
, rbuf
;
847 LSA_Q_ENUM_ACCOUNTS q
;
848 LSA_R_ENUM_ACCOUNTS r
;
855 /* Initialise parse structures */
857 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
858 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
860 /* Marshall data and send request */
862 init_lsa_q_enum_accounts(&q
, pol
, *enum_ctx
, pref_max_length
);
864 if (!lsa_io_q_enum_accounts("", &q
, &qbuf
, 0) ||
865 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_ENUM_ACCOUNTS
, &qbuf
, &rbuf
)) {
866 result
= NT_STATUS_UNSUCCESSFUL
;
870 /* Unmarshall response */
872 if (!lsa_io_r_enum_accounts("", &r
, &rbuf
, 0)) {
873 result
= NT_STATUS_UNSUCCESSFUL
;
879 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
883 if (r
.sids
.num_entries
==0)
886 /* Return output parameters */
888 *sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, r
.sids
.num_entries
);
890 DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
891 result
= NT_STATUS_UNSUCCESSFUL
;
895 /* Copy across names and sids */
897 for (i
= 0; i
< r
.sids
.num_entries
; i
++) {
898 sid_copy(&(*sids
)[i
], &r
.sids
.sid
[i
].sid
);
901 *num_sids
= r
.sids
.num_entries
;
902 *enum_ctx
= r
.enum_context
;
911 /** Create a LSA user handle
913 * @param cli Handle on an initialised SMB connection
915 * FIXME: The code is actually identical to open account
916 * TODO: Check and code what the function should exactly do
920 NTSTATUS
cli_lsa_create_account(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
921 POLICY_HND
*dom_pol
, DOM_SID
*sid
, uint32 desired_access
,
922 POLICY_HND
*user_pol
)
924 prs_struct qbuf
, rbuf
;
925 LSA_Q_CREATEACCOUNT q
;
926 LSA_R_CREATEACCOUNT r
;
932 /* Initialise parse structures */
934 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
935 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
937 /* Initialise input parameters */
939 init_lsa_q_create_account(&q
, dom_pol
, sid
, desired_access
);
941 /* Marshall data and send request */
943 if (!lsa_io_q_create_account("", &q
, &qbuf
, 0) ||
944 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_CREATEACCOUNT
, &qbuf
, &rbuf
)) {
945 result
= NT_STATUS_UNSUCCESSFUL
;
949 /* Unmarshall response */
951 if (!lsa_io_r_create_account("", &r
, &rbuf
, 0)) {
952 result
= NT_STATUS_UNSUCCESSFUL
;
956 /* Return output parameters */
958 if (NT_STATUS_IS_OK(result
= r
.status
)) {
969 /** Open a LSA user handle
971 * @param cli Handle on an initialised SMB connection */
973 NTSTATUS
cli_lsa_open_account(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
974 POLICY_HND
*dom_pol
, DOM_SID
*sid
, uint32 des_access
,
975 POLICY_HND
*user_pol
)
977 prs_struct qbuf
, rbuf
;
985 /* Initialise parse structures */
987 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
988 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
990 /* Initialise input parameters */
992 init_lsa_q_open_account(&q
, dom_pol
, sid
, des_access
);
994 /* Marshall data and send request */
996 if (!lsa_io_q_open_account("", &q
, &qbuf
, 0) ||
997 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_OPENACCOUNT
, &qbuf
, &rbuf
)) {
998 result
= NT_STATUS_UNSUCCESSFUL
;
1002 /* Unmarshall response */
1004 if (!lsa_io_r_open_account("", &r
, &rbuf
, 0)) {
1005 result
= NT_STATUS_UNSUCCESSFUL
;
1009 /* Return output parameters */
1011 if (NT_STATUS_IS_OK(result
= r
.status
)) {
1016 prs_mem_free(&qbuf
);
1017 prs_mem_free(&rbuf
);
1022 /** Enumerate user privileges
1024 * @param cli Handle on an initialised SMB connection */
1026 NTSTATUS
cli_lsa_enum_privsaccount(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1027 POLICY_HND
*pol
, uint32
*count
, LUID_ATTR
**set
)
1029 prs_struct qbuf
, rbuf
;
1030 LSA_Q_ENUMPRIVSACCOUNT q
;
1031 LSA_R_ENUMPRIVSACCOUNT r
;
1038 /* Initialise parse structures */
1040 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1041 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1043 /* Initialise input parameters */
1045 init_lsa_q_enum_privsaccount(&q
, pol
);
1047 /* Marshall data and send request */
1049 if (!lsa_io_q_enum_privsaccount("", &q
, &qbuf
, 0) ||
1050 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_ENUMPRIVSACCOUNT
, &qbuf
, &rbuf
)) {
1051 result
= NT_STATUS_UNSUCCESSFUL
;
1055 /* Unmarshall response */
1057 if (!lsa_io_r_enum_privsaccount("", &r
, &rbuf
, 0)) {
1058 result
= NT_STATUS_UNSUCCESSFUL
;
1062 /* Return output parameters */
1064 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
1071 if (!((*set
= TALLOC_ARRAY(mem_ctx
, LUID_ATTR
, r
.count
)))) {
1072 DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
1073 result
= NT_STATUS_UNSUCCESSFUL
;
1077 for (i
=0; i
<r
.count
; i
++) {
1078 (*set
)[i
].luid
.low
= r
.set
.set
[i
].luid
.low
;
1079 (*set
)[i
].luid
.high
= r
.set
.set
[i
].luid
.high
;
1080 (*set
)[i
].attr
= r
.set
.set
[i
].attr
;
1085 prs_mem_free(&qbuf
);
1086 prs_mem_free(&rbuf
);
1091 /** Get a privilege value given its name */
1093 NTSTATUS
cli_lsa_lookup_priv_value(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1094 POLICY_HND
*pol
, const char *name
, LUID
*luid
)
1096 prs_struct qbuf
, rbuf
;
1097 LSA_Q_LOOKUP_PRIV_VALUE q
;
1098 LSA_R_LOOKUP_PRIV_VALUE r
;
1104 /* Initialise parse structures */
1106 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1107 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1109 /* Marshall data and send request */
1111 init_lsa_q_lookup_priv_value(&q
, pol
, name
);
1113 if (!lsa_io_q_lookup_priv_value("", &q
, &qbuf
, 0) ||
1114 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_LOOKUPPRIVVALUE
, &qbuf
, &rbuf
)) {
1115 result
= NT_STATUS_UNSUCCESSFUL
;
1119 /* Unmarshall response */
1121 if (!lsa_io_r_lookup_priv_value("", &r
, &rbuf
, 0)) {
1122 result
= NT_STATUS_UNSUCCESSFUL
;
1126 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
1130 /* Return output parameters */
1132 (*luid
).low
=r
.luid
.low
;
1133 (*luid
).high
=r
.luid
.high
;
1136 prs_mem_free(&qbuf
);
1137 prs_mem_free(&rbuf
);
1142 /** Query LSA security object */
1144 NTSTATUS
cli_lsa_query_secobj(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1145 POLICY_HND
*pol
, uint32 sec_info
,
1146 SEC_DESC_BUF
**psdb
)
1148 prs_struct qbuf
, rbuf
;
1149 LSA_Q_QUERY_SEC_OBJ q
;
1150 LSA_R_QUERY_SEC_OBJ r
;
1156 /* Initialise parse structures */
1158 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1159 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1161 /* Marshall data and send request */
1163 init_q_query_sec_obj(&q
, pol
, sec_info
);
1165 if (!lsa_io_q_query_sec_obj("", &q
, &qbuf
, 0) ||
1166 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_QUERYSECOBJ
, &qbuf
, &rbuf
)) {
1167 result
= NT_STATUS_UNSUCCESSFUL
;
1171 /* Unmarshall response */
1173 if (!lsa_io_r_query_sec_obj("", &r
, &rbuf
, 0)) {
1174 result
= NT_STATUS_UNSUCCESSFUL
;
1178 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
1182 /* Return output parameters */
1188 prs_mem_free(&qbuf
);
1189 prs_mem_free(&rbuf
);
1195 /* Enumerate account rights This is similar to enum_privileges but
1196 takes a SID directly, avoiding the open_account call.
1199 NTSTATUS
cli_lsa_enum_account_rights(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1200 POLICY_HND
*pol
, DOM_SID
*sid
,
1201 uint32
*count
, char ***priv_names
)
1203 prs_struct qbuf
, rbuf
;
1204 LSA_Q_ENUM_ACCT_RIGHTS q
;
1205 LSA_R_ENUM_ACCT_RIGHTS r
;
1208 fstring
*privileges
;
1214 /* Initialise parse structures */
1216 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1217 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1219 /* Marshall data and send request */
1220 init_q_enum_acct_rights(&q
, pol
, 2, sid
);
1222 if (!lsa_io_q_enum_acct_rights("", &q
, &qbuf
, 0) ||
1223 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_ENUMACCTRIGHTS
, &qbuf
, &rbuf
)) {
1224 result
= NT_STATUS_UNSUCCESSFUL
;
1228 if (!lsa_io_r_enum_acct_rights("", &r
, &rbuf
, 0)) {
1229 result
= NT_STATUS_UNSUCCESSFUL
;
1233 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
1243 privileges
= TALLOC_ARRAY( mem_ctx
, fstring
, *count
);
1244 names
= TALLOC_ARRAY( mem_ctx
, char *, *count
);
1246 for ( i
=0; i
<*count
; i
++ ) {
1247 UNISTR4
*uni_string
= &r
.rights
->strings
[i
];
1249 if ( !uni_string
->string
)
1252 rpcstr_pull( privileges
[i
], uni_string
->string
->buffer
, sizeof(privileges
[i
]), -1, STR_TERMINATE
);
1254 /* now copy to the return array */
1255 names
[i
] = talloc_strdup( mem_ctx
, privileges
[i
] );
1258 *priv_names
= names
;
1267 /* add account rights to an account. */
1269 NTSTATUS
cli_lsa_add_account_rights(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1270 POLICY_HND
*pol
, DOM_SID sid
,
1272 uint32 count
, const char **privs_name
)
1274 prs_struct qbuf
, rbuf
;
1275 LSA_Q_ADD_ACCT_RIGHTS q
;
1276 LSA_R_ADD_ACCT_RIGHTS r
;
1281 /* Initialise parse structures */
1282 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1283 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1285 /* Marshall data and send request */
1286 init_q_add_acct_rights(&q
, pol
, &sid
, count
, privs_name
);
1288 if (!lsa_io_q_add_acct_rights("", &q
, &qbuf
, 0) ||
1289 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_ADDACCTRIGHTS
, &qbuf
, &rbuf
)) {
1290 result
= NT_STATUS_UNSUCCESSFUL
;
1294 /* Unmarshall response */
1296 if (!lsa_io_r_add_acct_rights("", &r
, &rbuf
, 0)) {
1297 result
= NT_STATUS_UNSUCCESSFUL
;
1301 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
1310 /* remove account rights for an account. */
1312 NTSTATUS
cli_lsa_remove_account_rights(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1313 POLICY_HND
*pol
, DOM_SID sid
, BOOL removeall
,
1314 uint32 count
, const char **privs_name
)
1316 prs_struct qbuf
, rbuf
;
1317 LSA_Q_REMOVE_ACCT_RIGHTS q
;
1318 LSA_R_REMOVE_ACCT_RIGHTS r
;
1323 /* Initialise parse structures */
1324 prs_init(&qbuf
, MAX_PDU_FRAG_LEN
, mem_ctx
, MARSHALL
);
1325 prs_init(&rbuf
, 0, mem_ctx
, UNMARSHALL
);
1327 /* Marshall data and send request */
1328 init_q_remove_acct_rights(&q
, pol
, &sid
, removeall
?1:0, count
, privs_name
);
1330 if (!lsa_io_q_remove_acct_rights("", &q
, &qbuf
, 0) ||
1331 !rpc_api_pipe_req(cli
, PI_LSARPC
, LSA_REMOVEACCTRIGHTS
, &qbuf
, &rbuf
)) {
1332 result
= NT_STATUS_UNSUCCESSFUL
;
1336 /* Unmarshall response */
1338 if (!lsa_io_r_remove_acct_rights("", &r
, &rbuf
, 0)) {
1339 result
= NT_STATUS_UNSUCCESSFUL
;
1343 if (!NT_STATUS_IS_OK(result
= r
.status
)) {
1354 /** An example of how to use the routines in this file. Fetch a DOMAIN
1355 sid. Does complete cli setup / teardown anonymously. */
1357 BOOL
fetch_domain_sid( char *domain
, char *remote_machine
, DOM_SID
*psid
)
1359 extern pstring global_myname
;
1360 struct cli_state cli
;
1366 if(cli_initialise(&cli
) == False
) {
1367 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
1371 if(!resolve_name( remote_machine
, &cli
.dest_ip
, 0x20)) {
1372 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine
));
1376 if (!cli_connect(&cli
, remote_machine
, &cli
.dest_ip
)) {
1377 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
1378 machine %s. Error was : %s.\n", remote_machine
, cli_errstr(&cli
) ));
1382 if (!attempt_netbios_session_request(&cli
, global_myname
, remote_machine
, &cli
.dest_ip
)) {
1383 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n",
1388 cli
.protocol
= PROTOCOL_NT1
;
1390 if (!cli_negprot(&cli
)) {
1391 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
1392 Error was : %s.\n", remote_machine
, cli_errstr(&cli
) ));
1396 if (cli
.protocol
!= PROTOCOL_NT1
) {
1397 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
1403 * Do an anonymous session setup.
1406 if (!cli_session_setup(&cli
, "", "", 0, "", 0, "")) {
1407 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
1408 Error was : %s.\n", remote_machine
, cli_errstr(&cli
) ));
1412 if (!(cli
.sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
)) {
1413 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
1418 if (!cli_send_tconX(&cli
, "IPC$", "IPC", "", 1)) {
1419 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
1420 Error was : %s.\n", remote_machine
, cli_errstr(&cli
) ));
1424 /* Fetch domain sid */
1426 if (!cli_nt_session_open(&cli
, PI_LSARPC
)) {
1427 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
1431 result
= cli_lsa_open_policy(&cli
, cli
.mem_ctx
, True
, SEC_RIGHTS_QUERY_VALUE
, &lsa_pol
);
1432 if (!NT_STATUS_IS_OK(result
)) {
1433 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
1434 nt_errstr(result
) ));
1438 result
= cli_lsa_query_info_policy(&cli
, cli
.mem_ctx
, &lsa_pol
, 5, domain
, psid
);
1439 if (!NT_STATUS_IS_OK(result
)) {
1440 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
1441 nt_errstr(result
) ));