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 */
318 /* adapt overall result */
319 if (( NT_STATUS_IS_OK(result) &&
320 !NT_STATUS_IS_OK(hunk_result))
322 ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
323 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
325 result = STATUS_SOME_UNMAPPED;
328 sids_left -= hunk_num_sids;
329 sids_processed += hunk_num_sids; /* only used in DEBUG */
330 hunk_sids += hunk_num_sids;
331 hunk_domains += hunk_num_sids;
332 hunk_names += hunk_num_sids;
333 hunk_types += hunk_num_sids;
340 /** Lookup a list of sids */
342 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
344 POLICY_HND *pol, int num_sids,
348 enum lsa_SidType **types)
350 prs_struct qbuf, rbuf;
354 NTSTATUS result = NT_STATUS_OK;
360 init_q_lookup_sids(mem_ctx, &q, pol, num_sids, sids, 1);
366 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPSIDS,
369 lsa_io_q_lookup_sids,
370 lsa_io_r_lookup_sids,
371 NT_STATUS_UNSUCCESSFUL );
373 if (!NT_STATUS_IS_OK(r.status) &&
374 !NT_STATUS_EQUAL(r.status, STATUS_SOME_UNMAPPED)) {
376 /* An actual error occured */
382 /* Return output parameters */
384 if (r.mapped_count == 0) {
385 result = NT_STATUS_NONE_MAPPED;
390 if (!((*domains) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
391 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
392 result = NT_STATUS_NO_MEMORY;
396 if (!((*names) = 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 (!((*types) = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
403 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
404 result = NT_STATUS_NO_MEMORY;
413 for (i = 0; i < num_sids; i++) {
414 fstring name, dom_name;
415 uint32 dom_idx = r.names.name[i].domain_idx;
417 /* Translate optimised name through domain index array */
419 if (dom_idx != 0xffffffff) {
421 rpcstr_pull_unistr2_fstring(
422 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
423 rpcstr_pull_unistr2_fstring(
424 name, &r.names.uni_name[i]);
426 (*names)[i] = talloc_strdup(mem_ctx, name);
427 (*domains)[i] = talloc_strdup(mem_ctx, dom_name);
428 (*types)[i] = (enum lsa_SidType)r.names.name[i].sid_name_use;
430 if (((*names)[i] == NULL) || ((*domains)[i] == NULL)) {
431 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
432 result = NT_STATUS_UNSUCCESSFUL;
438 (*domains)[i] = NULL;
439 (*types)[i] = SID_NAME_UNKNOWN;
448 /** Lookup a list of names */
450 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
452 POLICY_HND *pol, int num_names,
454 const char ***dom_names,
457 enum lsa_SidType **types)
459 prs_struct qbuf, rbuf;
460 LSA_Q_LOOKUP_NAMES q;
461 LSA_R_LOOKUP_NAMES r;
472 init_q_lookup_names(mem_ctx, &q, pol, num_names, names, level);
474 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPNAMES,
477 lsa_io_q_lookup_names,
478 lsa_io_r_lookup_names,
479 NT_STATUS_UNSUCCESSFUL);
483 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
484 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
486 /* An actual error occured */
491 /* Return output parameters */
493 if (r.mapped_count == 0) {
494 result = NT_STATUS_NONE_MAPPED;
499 if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
500 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
501 result = NT_STATUS_NO_MEMORY;
505 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
506 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
507 result = NT_STATUS_NO_MEMORY;
511 if (dom_names != NULL) {
512 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
513 if (*dom_names == NULL) {
514 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
515 result = NT_STATUS_NO_MEMORY;
522 if (dom_names != NULL) {
527 for (i = 0; i < num_names; i++) {
528 DOM_RID *t_rids = r.dom_rid;
529 uint32 dom_idx = t_rids[i].rid_idx;
530 uint32 dom_rid = t_rids[i].rid;
531 DOM_SID *sid = &(*sids)[i];
533 /* Translate optimised sid through domain index array */
535 if (dom_idx == 0xffffffff) {
536 /* Nothing to do, this is unknown */
538 (*types)[i] = SID_NAME_UNKNOWN;
542 sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
544 if (dom_rid != 0xffffffff) {
545 sid_append_rid(sid, dom_rid);
548 (*types)[i] = (enum lsa_SidType)t_rids[i].type;
550 if (dom_names == NULL) {
554 (*dom_names)[i] = rpcstr_pull_unistr2_talloc(
555 *dom_names, &ref.ref_dom[dom_idx].uni_dom_name);
563 NTSTATUS rpccli_lsa_query_info_policy_new(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
564 POLICY_HND *pol, uint16 info_class,
567 prs_struct qbuf, rbuf;
575 init_q_query(&q, pol, info_class);
577 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFOPOLICY,
582 NT_STATUS_UNSUCCESSFUL);
586 if (!NT_STATUS_IS_OK(result)) {
597 NTSTATUS rpccli_lsa_query_info_policy2_new(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
598 POLICY_HND *pol, uint16 info_class,
601 prs_struct qbuf, rbuf;
609 init_q_query2(&q, pol, info_class);
611 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFO2,
614 lsa_io_q_query_info2,
615 lsa_io_r_query_info2,
616 NT_STATUS_UNSUCCESSFUL);
620 if (!NT_STATUS_IS_OK(result)) {
633 /** Query info policy
635 * @param domain_sid - returned remote server's domain sid */
637 NTSTATUS rpccli_lsa_query_info_policy(struct rpc_pipe_client *cli,
639 POLICY_HND *pol, uint16 info_class,
640 char **domain_name, DOM_SID **domain_sid)
642 prs_struct qbuf, rbuf;
650 init_q_query(&q, pol, info_class);
652 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFOPOLICY,
657 NT_STATUS_UNSUCCESSFUL);
661 if (!NT_STATUS_IS_OK(result)) {
665 /* Return output parameters */
667 switch (info_class) {
670 if (domain_name && (r.ctr.info.id3.buffer_dom_name != 0)) {
671 *domain_name = unistr2_tdup(mem_ctx,
675 return NT_STATUS_NO_MEMORY;
679 if (domain_sid && (r.ctr.info.id3.buffer_dom_sid != 0)) {
680 *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
682 return NT_STATUS_NO_MEMORY;
684 sid_copy(*domain_sid, &r.ctr.info.id3.dom_sid.sid);
691 if (domain_name && (r.ctr.info.id5.buffer_dom_name != 0)) {
692 *domain_name = unistr2_tdup(mem_ctx,
696 return NT_STATUS_NO_MEMORY;
700 if (domain_sid && (r.ctr.info.id5.buffer_dom_sid != 0)) {
701 *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
703 return NT_STATUS_NO_MEMORY;
705 sid_copy(*domain_sid, &r.ctr.info.id5.dom_sid.sid);
710 DEBUG(3, ("unknown info class %d\n", info_class));
719 /** Query info policy2
721 * @param domain_name - returned remote server's domain name
722 * @param dns_name - returned remote server's dns domain name
723 * @param forest_name - returned remote server's forest name
724 * @param domain_guid - returned remote server's domain guid
725 * @param domain_sid - returned remote server's domain sid */
727 NTSTATUS rpccli_lsa_query_info_policy2(struct rpc_pipe_client *cli,
729 POLICY_HND *pol, uint16 info_class,
730 char **domain_name, char **dns_name,
732 struct GUID **domain_guid,
733 DOM_SID **domain_sid)
735 prs_struct qbuf, rbuf;
738 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
740 if (info_class != 12)
746 init_q_query2(&q, pol, info_class);
748 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYINFO2,
751 lsa_io_q_query_info2,
752 lsa_io_r_query_info2,
753 NT_STATUS_UNSUCCESSFUL);
757 if (!NT_STATUS_IS_OK(result)) {
761 /* Return output parameters */
763 ZERO_STRUCTP(domain_guid);
765 if (domain_name && r.ctr.info.id12.hdr_nb_dom_name.buffer) {
766 *domain_name = unistr2_tdup(mem_ctx,
770 return NT_STATUS_NO_MEMORY;
773 if (dns_name && r.ctr.info.id12.hdr_dns_dom_name.buffer) {
774 *dns_name = unistr2_tdup(mem_ctx,
778 return NT_STATUS_NO_MEMORY;
781 if (forest_name && r.ctr.info.id12.hdr_forest_name.buffer) {
782 *forest_name = unistr2_tdup(mem_ctx,
786 return NT_STATUS_NO_MEMORY;
791 *domain_guid = TALLOC_P(mem_ctx, struct GUID);
793 return NT_STATUS_NO_MEMORY;
796 &r.ctr.info.id12.dom_guid,
797 sizeof(struct GUID));
800 if (domain_sid && r.ctr.info.id12.ptr_dom_sid != 0) {
801 *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
803 return NT_STATUS_NO_MEMORY;
805 sid_copy(*domain_sid,
806 &r.ctr.info.id12.dom_sid.sid);
814 NTSTATUS rpccli_lsa_set_info_policy(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
815 POLICY_HND *pol, uint16 info_class,
818 prs_struct qbuf, rbuf;
826 init_q_set(&q, pol, info_class, ctr);
828 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_SETINFOPOLICY,
833 NT_STATUS_UNSUCCESSFUL);
837 if (!NT_STATUS_IS_OK(result)) {
841 /* Return output parameters */
850 * Enumerate list of trusted domains
852 * @param cli client state (cli_state) structure of the connection
853 * @param mem_ctx memory context
854 * @param pol opened lsa policy handle
855 * @param enum_ctx enumeration context ie. index of first returned domain entry
856 * @param pref_num_domains preferred max number of entries returned in one response
857 * @param num_domains total number of trusted domains returned by response
858 * @param domain_names returned trusted domain names
859 * @param domain_sids returned trusted domain sids
861 * @return nt status code of response
864 NTSTATUS rpccli_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
866 POLICY_HND *pol, uint32 *enum_ctx,
868 char ***domain_names, DOM_SID **domain_sids)
870 prs_struct qbuf, rbuf;
871 LSA_Q_ENUM_TRUST_DOM in;
872 LSA_R_ENUM_TRUST_DOM out;
879 /* 64k is enough for about 2000 trusted domains */
881 init_q_enum_trust_dom(&in, pol, *enum_ctx, 0x10000);
883 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMTRUSTDOM,
886 lsa_io_q_enum_trust_dom,
887 lsa_io_r_enum_trust_dom,
888 NT_STATUS_UNSUCCESSFUL );
891 /* check for an actual error */
893 if ( !NT_STATUS_IS_OK(out.status)
894 && !NT_STATUS_EQUAL(out.status, NT_STATUS_NO_MORE_ENTRIES)
895 && !NT_STATUS_EQUAL(out.status, STATUS_MORE_ENTRIES) )
900 /* Return output parameters */
902 *num_domains = out.count;
903 *enum_ctx = out.enum_context;
907 /* Allocate memory for trusted domain names and sids */
909 if ( !(*domain_names = TALLOC_ARRAY(mem_ctx, char *, out.count)) ) {
910 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
911 return NT_STATUS_NO_MEMORY;
914 if ( !(*domain_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, out.count)) ) {
915 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
916 return NT_STATUS_NO_MEMORY;
919 /* Copy across names and sids */
921 for (i = 0; i < out.count; i++) {
923 rpcstr_pull( tmp, out.domlist->domains[i].name.string->buffer,
924 sizeof(tmp), out.domlist->domains[i].name.length, 0);
925 (*domain_names)[i] = talloc_strdup(mem_ctx, tmp);
927 sid_copy(&(*domain_sids)[i], &out.domlist->domains[i].sid->sid );
934 /** Enumerate privileges*/
936 NTSTATUS rpccli_lsa_enum_privilege(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
937 POLICY_HND *pol, uint32 *enum_context, uint32 pref_max_length,
938 uint32 *count, char ***privs_name, uint32 **privs_high, uint32 **privs_low)
940 prs_struct qbuf, rbuf;
949 init_q_enum_privs(&q, pol, *enum_context, pref_max_length);
951 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUM_PRIVS,
956 NT_STATUS_UNSUCCESSFUL);
960 if (!NT_STATUS_IS_OK(result)) {
964 /* Return output parameters */
966 *enum_context = r.enum_context;
970 if (!((*privs_name = TALLOC_ARRAY(mem_ctx, char *, r.count)))) {
971 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
972 result = NT_STATUS_UNSUCCESSFUL;
976 if (!((*privs_high = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
977 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
978 result = NT_STATUS_UNSUCCESSFUL;
982 if (!((*privs_low = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
983 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
984 result = NT_STATUS_UNSUCCESSFUL;
993 for (i = 0; i < r.count; i++) {
996 rpcstr_pull_unistr2_fstring( name, &r.privs[i].name);
998 (*privs_name)[i] = talloc_strdup(mem_ctx, name);
1000 (*privs_high)[i] = r.privs[i].luid_high;
1001 (*privs_low)[i] = r.privs[i].luid_low;
1009 /** Get privilege name */
1011 NTSTATUS rpccli_lsa_get_dispname(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1012 POLICY_HND *pol, const char *name,
1013 uint16 lang_id, uint16 lang_id_sys,
1014 fstring description, uint16 *lang_id_desc)
1016 prs_struct qbuf, rbuf;
1017 LSA_Q_PRIV_GET_DISPNAME q;
1018 LSA_R_PRIV_GET_DISPNAME r;
1024 init_lsa_priv_get_dispname(&q, pol, name, lang_id, lang_id_sys);
1026 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_PRIV_GET_DISPNAME,
1029 lsa_io_q_priv_get_dispname,
1030 lsa_io_r_priv_get_dispname,
1031 NT_STATUS_UNSUCCESSFUL);
1035 if (!NT_STATUS_IS_OK(result)) {
1039 /* Return output parameters */
1041 rpcstr_pull_unistr2_fstring(description , &r.desc);
1042 *lang_id_desc = r.lang_id;
1049 /** Enumerate list of SIDs */
1051 NTSTATUS rpccli_lsa_enum_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1052 POLICY_HND *pol, uint32 *enum_ctx, uint32 pref_max_length,
1053 uint32 *num_sids, DOM_SID **sids)
1055 prs_struct qbuf, rbuf;
1056 LSA_Q_ENUM_ACCOUNTS q;
1057 LSA_R_ENUM_ACCOUNTS r;
1064 init_lsa_q_enum_accounts(&q, pol, *enum_ctx, pref_max_length);
1066 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUM_ACCOUNTS,
1069 lsa_io_q_enum_accounts,
1070 lsa_io_r_enum_accounts,
1071 NT_STATUS_UNSUCCESSFUL);
1075 if (!NT_STATUS_IS_OK(result)) {
1079 if (r.sids.num_entries==0)
1082 /* Return output parameters */
1084 *sids = TALLOC_ARRAY(mem_ctx, DOM_SID, r.sids.num_entries);
1086 DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
1087 result = NT_STATUS_UNSUCCESSFUL;
1091 /* Copy across names and sids */
1093 for (i = 0; i < r.sids.num_entries; i++) {
1094 sid_copy(&(*sids)[i], &r.sids.sid[i].sid);
1097 *num_sids= r.sids.num_entries;
1098 *enum_ctx = r.enum_context;
1105 /** Create a LSA user handle
1107 * @param cli Handle on an initialised SMB connection
1109 * FIXME: The code is actually identical to open account
1110 * TODO: Check and code what the function should exactly do
1114 NTSTATUS rpccli_lsa_create_account(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1115 POLICY_HND *dom_pol, DOM_SID *sid, uint32 desired_access,
1116 POLICY_HND *user_pol)
1118 prs_struct qbuf, rbuf;
1119 LSA_Q_CREATEACCOUNT q;
1120 LSA_R_CREATEACCOUNT r;
1126 /* Initialise input parameters */
1128 init_lsa_q_create_account(&q, dom_pol, sid, desired_access);
1130 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_CREATEACCOUNT,
1133 lsa_io_q_create_account,
1134 lsa_io_r_create_account,
1135 NT_STATUS_UNSUCCESSFUL);
1137 /* Return output parameters */
1141 if (NT_STATUS_IS_OK(result)) {
1148 /** Open a LSA user handle
1150 * @param cli Handle on an initialised SMB connection */
1152 NTSTATUS rpccli_lsa_open_account(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1153 POLICY_HND *dom_pol, DOM_SID *sid, uint32 des_access,
1154 POLICY_HND *user_pol)
1156 prs_struct qbuf, rbuf;
1157 LSA_Q_OPENACCOUNT q;
1158 LSA_R_OPENACCOUNT r;
1164 /* Initialise input parameters */
1166 init_lsa_q_open_account(&q, dom_pol, sid, des_access);
1168 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENACCOUNT,
1171 lsa_io_q_open_account,
1172 lsa_io_r_open_account,
1173 NT_STATUS_UNSUCCESSFUL);
1175 /* Return output parameters */
1179 if (NT_STATUS_IS_OK(result)) {
1186 /** Enumerate user privileges
1188 * @param cli Handle on an initialised SMB connection */
1190 NTSTATUS rpccli_lsa_enum_privsaccount(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1191 POLICY_HND *pol, uint32 *count, LUID_ATTR **set)
1193 prs_struct qbuf, rbuf;
1194 LSA_Q_ENUMPRIVSACCOUNT q;
1195 LSA_R_ENUMPRIVSACCOUNT r;
1202 /* Initialise input parameters */
1204 init_lsa_q_enum_privsaccount(&q, pol);
1206 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMPRIVSACCOUNT,
1209 lsa_io_q_enum_privsaccount,
1210 lsa_io_r_enum_privsaccount,
1211 NT_STATUS_UNSUCCESSFUL);
1213 /* Return output parameters */
1217 if (!NT_STATUS_IS_OK(result)) {
1224 if (!((*set = TALLOC_ARRAY(mem_ctx, LUID_ATTR, r.count)))) {
1225 DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
1226 result = NT_STATUS_UNSUCCESSFUL;
1230 for (i=0; i<r.count; i++) {
1231 (*set)[i].luid.low = r.set.set[i].luid.low;
1232 (*set)[i].luid.high = r.set.set[i].luid.high;
1233 (*set)[i].attr = r.set.set[i].attr;
1242 /** Get a privilege value given its name */
1244 NTSTATUS rpccli_lsa_lookup_priv_value(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1245 POLICY_HND *pol, const char *name, LUID *luid)
1247 prs_struct qbuf, rbuf;
1248 LSA_Q_LOOKUP_PRIV_VALUE q;
1249 LSA_R_LOOKUP_PRIV_VALUE r;
1255 /* Marshall data and send request */
1257 init_lsa_q_lookup_priv_value(&q, pol, name);
1259 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPPRIVVALUE,
1262 lsa_io_q_lookup_priv_value,
1263 lsa_io_r_lookup_priv_value,
1264 NT_STATUS_UNSUCCESSFUL);
1268 if (!NT_STATUS_IS_OK(result)) {
1272 /* Return output parameters */
1274 (*luid).low=r.luid.low;
1275 (*luid).high=r.luid.high;
1282 /** Query LSA security object */
1284 NTSTATUS rpccli_lsa_query_secobj(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1285 POLICY_HND *pol, uint32 sec_info,
1286 SEC_DESC_BUF **psdb)
1288 prs_struct qbuf, rbuf;
1289 LSA_Q_QUERY_SEC_OBJ q;
1290 LSA_R_QUERY_SEC_OBJ r;
1296 /* Marshall data and send request */
1298 init_q_query_sec_obj(&q, pol, sec_info);
1300 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYSECOBJ,
1303 lsa_io_q_query_sec_obj,
1304 lsa_io_r_query_sec_obj,
1305 NT_STATUS_UNSUCCESSFUL);
1309 if (!NT_STATUS_IS_OK(result)) {
1313 /* Return output parameters */
1324 /* Enumerate account rights This is similar to enum_privileges but
1325 takes a SID directly, avoiding the open_account call.
1328 NTSTATUS rpccli_lsa_enum_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1329 POLICY_HND *pol, DOM_SID *sid,
1330 uint32 *count, char ***priv_names)
1332 prs_struct qbuf, rbuf;
1333 LSA_Q_ENUM_ACCT_RIGHTS q;
1334 LSA_R_ENUM_ACCT_RIGHTS r;
1337 fstring *privileges;
1343 /* Marshall data and send request */
1344 init_q_enum_acct_rights(&q, pol, 2, sid);
1346 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMACCTRIGHTS,
1349 lsa_io_q_enum_acct_rights,
1350 lsa_io_r_enum_acct_rights,
1351 NT_STATUS_UNSUCCESSFUL);
1355 if (!NT_STATUS_IS_OK(result)) {
1365 privileges = TALLOC_ARRAY( mem_ctx, fstring, *count );
1366 names = TALLOC_ARRAY( mem_ctx, char *, *count );
1368 if ((privileges == NULL) || (names == NULL)) {
1369 TALLOC_FREE(privileges);
1371 return NT_STATUS_NO_MEMORY;
1374 for ( i=0; i<*count; i++ ) {
1375 UNISTR4 *uni_string = &r.rights->strings[i];
1377 if ( !uni_string->string )
1380 rpcstr_pull( privileges[i], uni_string->string->buffer, sizeof(privileges[i]), -1, STR_TERMINATE );
1382 /* now copy to the return array */
1383 names[i] = talloc_strdup( mem_ctx, privileges[i] );
1386 *priv_names = names;
1395 /* add account rights to an account. */
1397 NTSTATUS rpccli_lsa_add_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1398 POLICY_HND *pol, DOM_SID sid,
1399 uint32 count, const char **privs_name)
1401 prs_struct qbuf, rbuf;
1402 LSA_Q_ADD_ACCT_RIGHTS q;
1403 LSA_R_ADD_ACCT_RIGHTS r;
1409 /* Marshall data and send request */
1410 init_q_add_acct_rights(&q, pol, &sid, count, privs_name);
1412 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ADDACCTRIGHTS,
1415 lsa_io_q_add_acct_rights,
1416 lsa_io_r_add_acct_rights,
1417 NT_STATUS_UNSUCCESSFUL);
1421 if (!NT_STATUS_IS_OK(result)) {
1430 /* remove account rights for an account. */
1432 NTSTATUS rpccli_lsa_remove_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1433 POLICY_HND *pol, DOM_SID sid, BOOL removeall,
1434 uint32 count, const char **privs_name)
1436 prs_struct qbuf, rbuf;
1437 LSA_Q_REMOVE_ACCT_RIGHTS q;
1438 LSA_R_REMOVE_ACCT_RIGHTS r;
1444 /* Marshall data and send request */
1445 init_q_remove_acct_rights(&q, pol, &sid, removeall?1:0, count, privs_name);
1447 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_REMOVEACCTRIGHTS,
1450 lsa_io_q_remove_acct_rights,
1451 lsa_io_r_remove_acct_rights,
1452 NT_STATUS_UNSUCCESSFUL);
1456 if (!NT_STATUS_IS_OK(result)) {
1467 /** An example of how to use the routines in this file. Fetch a DOMAIN
1468 sid. Does complete cli setup / teardown anonymously. */
1470 BOOL fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
1472 extern pstring global_myname;
1473 struct cli_state *cli;
1479 if((cli = cli_initialise()) == NULL) {
1480 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
1484 if(!resolve_name( remote_machine, &cli->dest_ip, 0x20)) {
1485 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
1489 if (!cli_connect(cli, remote_machine, &cli->dest_ip)) {
1490 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
1491 machine %s. Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1495 if (!attempt_netbios_session_request(cli, global_myname, remote_machine, &cli->dest_ip)) {
1496 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n",
1501 cli->protocol = PROTOCOL_NT1;
1503 if (!cli_negprot(cli)) {
1504 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
1505 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1509 if (cli->protocol != PROTOCOL_NT1) {
1510 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
1516 * Do an anonymous session setup.
1519 if (!cli_session_setup(cli, "", "", 0, "", 0, "")) {
1520 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
1521 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1525 if (!(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
1526 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
1531 if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
1532 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
1533 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1537 /* Fetch domain sid */
1539 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1540 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
1544 result = cli_lsa_open_policy(cli, cli->mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
1545 if (!NT_STATUS_IS_OK(result)) {
1546 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
1547 nt_errstr(result) ));
1551 result = cli_lsa_query_info_policy(cli, cli->mem_ctx, &lsa_pol, 5, domain, psid);
1552 if (!NT_STATUS_IS_OK(result)) {
1553 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
1554 nt_errstr(result) ));
1568 NTSTATUS rpccli_lsa_open_trusted_domain(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1569 POLICY_HND *pol, DOM_SID *dom_sid, uint32 access_mask,
1570 POLICY_HND *trustdom_pol)
1572 prs_struct qbuf, rbuf;
1573 LSA_Q_OPEN_TRUSTED_DOMAIN q;
1574 LSA_R_OPEN_TRUSTED_DOMAIN r;
1580 /* Initialise input parameters */
1582 init_lsa_q_open_trusted_domain(&q, pol, dom_sid, access_mask);
1584 /* Marshall data and send request */
1586 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENTRUSTDOM,
1589 lsa_io_q_open_trusted_domain,
1590 lsa_io_r_open_trusted_domain,
1591 NT_STATUS_UNSUCCESSFUL);
1593 /* Return output parameters */
1597 if (NT_STATUS_IS_OK(result)) {
1598 *trustdom_pol = r.handle;
1604 NTSTATUS rpccli_lsa_query_trusted_domain_info(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1607 LSA_TRUSTED_DOMAIN_INFO **info)
1609 prs_struct qbuf, rbuf;
1610 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO q;
1611 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1612 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1617 /* Marshall data and send request */
1619 init_q_query_trusted_domain_info(&q, pol, info_class);
1621 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFO,
1624 lsa_io_q_query_trusted_domain_info,
1625 lsa_io_r_query_trusted_domain_info,
1626 NT_STATUS_UNSUCCESSFUL);
1630 if (!NT_STATUS_IS_OK(result)) {
1640 NTSTATUS rpccli_lsa_open_trusted_domain_by_name(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1641 POLICY_HND *pol, const char *name, uint32 access_mask,
1642 POLICY_HND *trustdom_pol)
1644 prs_struct qbuf, rbuf;
1645 LSA_Q_OPEN_TRUSTED_DOMAIN_BY_NAME q;
1646 LSA_R_OPEN_TRUSTED_DOMAIN_BY_NAME r;
1652 /* Initialise input parameters */
1654 init_lsa_q_open_trusted_domain_by_name(&q, pol, name, access_mask);
1656 /* Marshall data and send request */
1658 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENTRUSTDOMBYNAME,
1661 lsa_io_q_open_trusted_domain_by_name,
1662 lsa_io_r_open_trusted_domain_by_name,
1663 NT_STATUS_UNSUCCESSFUL);
1665 /* Return output parameters */
1669 if (NT_STATUS_IS_OK(result)) {
1670 *trustdom_pol = r.handle;
1677 NTSTATUS rpccli_lsa_query_trusted_domain_info_by_sid(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1679 uint16 info_class, DOM_SID *dom_sid,
1680 LSA_TRUSTED_DOMAIN_INFO **info)
1682 prs_struct qbuf, rbuf;
1683 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO_BY_SID q;
1684 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1685 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1690 /* Marshall data and send request */
1692 init_q_query_trusted_domain_info_by_sid(&q, pol, info_class, dom_sid);
1694 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFOBYSID,
1697 lsa_io_q_query_trusted_domain_info_by_sid,
1698 lsa_io_r_query_trusted_domain_info,
1699 NT_STATUS_UNSUCCESSFUL);
1703 if (!NT_STATUS_IS_OK(result)) {
1714 NTSTATUS rpccli_lsa_query_trusted_domain_info_by_name(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1716 uint16 info_class, const char *domain_name,
1717 LSA_TRUSTED_DOMAIN_INFO **info)
1719 prs_struct qbuf, rbuf;
1720 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO_BY_NAME q;
1721 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1722 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1727 /* Marshall data and send request */
1729 init_q_query_trusted_domain_info_by_name(&q, pol, info_class, domain_name);
1731 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFOBYNAME,
1734 lsa_io_q_query_trusted_domain_info_by_name,
1735 lsa_io_r_query_trusted_domain_info,
1736 NT_STATUS_UNSUCCESSFUL);
1740 if (!NT_STATUS_IS_OK(result)) {
1751 NTSTATUS cli_lsa_query_domain_info_policy(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1753 uint16 info_class, LSA_DOM_INFO_UNION **info)
1755 prs_struct qbuf, rbuf;
1756 LSA_Q_QUERY_DOM_INFO_POLICY q;
1757 LSA_R_QUERY_DOM_INFO_POLICY r;
1758 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1763 /* Marshall data and send request */
1765 init_q_query_dom_info(&q, pol, info_class);
1767 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYDOMINFOPOL,
1770 lsa_io_q_query_dom_info,
1771 lsa_io_r_query_dom_info,
1772 NT_STATUS_UNSUCCESSFUL);
1776 if (!NT_STATUS_IS_OK(result)) {