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 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /** @defgroup lsa LSA - Local Security Architecture
35 * RPC client routines for the LSA RPC pipe. LSA means "local
36 * security authority", which is half of a password database.
39 /** Open a LSA policy handle
41 * @param cli Handle on an initialised SMB connection */
43 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
45 BOOL sec_qos, uint32 des_access,
48 prs_struct qbuf, rbuf;
57 /* Initialise input parameters */
60 init_lsa_sec_qos(&qos, 2, 1, 0);
61 init_q_open_pol(&q, '\\', 0, des_access, &qos);
63 init_q_open_pol(&q, '\\', 0, des_access, NULL);
66 /* Marshall data and send request */
68 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENPOLICY,
73 NT_STATUS_UNSUCCESSFUL );
75 /* Return output parameters */
79 if (NT_STATUS_IS_OK(result)) {
86 /** Open a LSA policy handle
88 * @param cli Handle on an initialised SMB connection
91 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
92 TALLOC_CTX *mem_ctx, BOOL sec_qos,
93 uint32 des_access, POLICY_HND *pol)
95 prs_struct qbuf, rbuf;
100 char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
106 init_lsa_sec_qos(&qos, 2, 1, 0);
107 init_q_open_pol2(&q, srv_name_slash, 0, des_access, &qos);
109 init_q_open_pol2(&q, srv_name_slash, 0, des_access, NULL);
112 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENPOLICY2,
117 NT_STATUS_UNSUCCESSFUL );
119 /* Return output parameters */
123 if (NT_STATUS_IS_OK(result)) {
130 /* Lookup a list of sids
132 * internal version withOUT memory allocation of the target arrays.
133 * this assumes suffciently sized arrays to store domains, names and types. */
135 static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
142 enum lsa_SidType *types)
144 prs_struct qbuf, rbuf;
148 LSA_TRANS_NAME_ENUM t_names;
149 NTSTATUS result = NT_STATUS_OK;
150 TALLOC_CTX *tmp_ctx = NULL;
153 tmp_ctx = talloc_new(mem_ctx);
155 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
156 result = NT_STATUS_UNSUCCESSFUL;
163 init_q_lookup_sids(tmp_ctx, &q, pol, num_sids, sids, 1);
166 ZERO_STRUCT(t_names);
171 CLI_DO_RPC( cli, tmp_ctx, PI_LSARPC, LSA_LOOKUPSIDS,
174 lsa_io_q_lookup_sids,
175 lsa_io_r_lookup_sids,
176 NT_STATUS_UNSUCCESSFUL );
178 if (!NT_STATUS_IS_OK(r.status) &&
179 !NT_STATUS_EQUAL(r.status, STATUS_SOME_UNMAPPED))
181 /* An actual error occured */
186 /* Return output parameters */
188 if (r.mapped_count == 0) {
189 result = NT_STATUS_NONE_MAPPED;
193 for (i = 0; i < num_sids; i++) {
194 fstring name, dom_name;
195 uint32 dom_idx = t_names.name[i].domain_idx;
197 /* Translate optimised name through domain index array */
199 if (dom_idx != 0xffffffff) {
201 rpcstr_pull_unistr2_fstring(
202 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
203 rpcstr_pull_unistr2_fstring(
204 name, &t_names.uni_name[i]);
206 (names)[i] = talloc_strdup(mem_ctx, name);
207 (domains)[i] = talloc_strdup(mem_ctx, dom_name);
208 (types)[i] = (enum lsa_SidType)t_names.name[i].sid_name_use;
210 if (((names)[i] == NULL) || ((domains)[i] == NULL)) {
211 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
212 result = NT_STATUS_UNSUCCESSFUL;
219 (types)[i] = SID_NAME_UNKNOWN;
224 TALLOC_FREE(tmp_ctx);
228 /* Lookup a list of sids
230 * do it the right way: there is a limit (of 20480 for w2k3) entries
231 * returned by this call. when the sids list contains more entries,
232 * empty lists are returned. This version of lsa_lookup_sids passes
233 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
235 /* This constant defines the limit of how many sids to look up
236 * in one call (maximum). the limit from the server side is
237 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
238 #define LOOKUP_SIDS_HUNK_SIZE 1000
240 NTSTATUS rpccli_lsa_lookup_sids_all(struct rpc_pipe_client *cli,
247 enum lsa_SidType **types)
249 NTSTATUS result = NT_STATUS_OK;
251 int sids_processed = 0;
252 const DOM_SID *hunk_sids = sids;
253 char **hunk_domains = NULL;
254 char **hunk_names = NULL;
255 enum lsa_SidType *hunk_types = NULL;
258 if (!((*domains) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
259 DEBUG(0, ("rpccli_lsa_lookup_sids_all(): out of memory\n"));
260 result = NT_STATUS_NO_MEMORY;
264 if (!((*names) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
265 DEBUG(0, ("rpccli_lsa_lookup_sids_all(): out of memory\n"));
266 result = NT_STATUS_NO_MEMORY;
270 if (!((*types) = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
271 DEBUG(0, ("rpccli_lsa_lookup_sids_all(): out of memory\n"));
272 result = NT_STATUS_NO_MEMORY;
281 sids_left = num_sids;
282 hunk_domains = *domains;
286 while (sids_left > 0) {
288 NTSTATUS hunk_result = NT_STATUS_OK;
290 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
291 ? LOOKUP_SIDS_HUNK_SIZE
294 DEBUG(10, ("rpccli_lsa_lookup_sids_all: processing items "
297 sids_processed + hunk_num_sids - 1,
300 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
309 if (!NT_STATUS_IS_OK(hunk_result) &&
310 !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
311 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
313 /* An actual error occured */
317 /* adapt overall result */
318 if (( NT_STATUS_IS_OK(result) &&
319 !NT_STATUS_IS_OK(hunk_result))
321 ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
322 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
324 result = STATUS_SOME_UNMAPPED;
327 sids_left -= hunk_num_sids;
328 sids_processed += hunk_num_sids; /* only used in DEBUG */
329 hunk_sids += hunk_num_sids;
330 hunk_domains += hunk_num_sids;
331 hunk_names += hunk_num_sids;
332 hunk_types += hunk_num_sids;
339 /** Lookup a list of sids */
341 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
343 POLICY_HND *pol, int num_sids,
347 enum lsa_SidType **types)
349 prs_struct qbuf, rbuf;
353 LSA_TRANS_NAME_ENUM t_names;
354 NTSTATUS result = NT_STATUS_OK;
360 init_q_lookup_sids(mem_ctx, &q, pol, num_sids, sids, 1);
363 ZERO_STRUCT(t_names);
368 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPSIDS,
371 lsa_io_q_lookup_sids,
372 lsa_io_r_lookup_sids,
373 NT_STATUS_UNSUCCESSFUL );
375 if (!NT_STATUS_IS_OK(r.status) &&
376 !NT_STATUS_EQUAL(r.status, STATUS_SOME_UNMAPPED)) {
378 /* An actual error occured */
384 /* Return output parameters */
386 if (r.mapped_count == 0) {
387 result = NT_STATUS_NONE_MAPPED;
392 if (!((*domains) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
393 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
394 result = NT_STATUS_NO_MEMORY;
398 if (!((*names) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
399 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
400 result = NT_STATUS_NO_MEMORY;
404 if (!((*types) = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
405 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
406 result = NT_STATUS_NO_MEMORY;
415 for (i = 0; i < num_sids; i++) {
416 fstring name, dom_name;
417 uint32 dom_idx = t_names.name[i].domain_idx;
419 /* Translate optimised name through domain index array */
421 if (dom_idx != 0xffffffff) {
423 rpcstr_pull_unistr2_fstring(
424 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
425 rpcstr_pull_unistr2_fstring(
426 name, &t_names.uni_name[i]);
428 (*names)[i] = talloc_strdup(mem_ctx, name);
429 (*domains)[i] = talloc_strdup(mem_ctx, dom_name);
430 (*types)[i] = (enum lsa_SidType)t_names.name[i].sid_name_use;
432 if (((*names)[i] == NULL) || ((*domains)[i] == NULL)) {
433 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
434 result = NT_STATUS_UNSUCCESSFUL;
440 (*domains)[i] = NULL;
441 (*types)[i] = SID_NAME_UNKNOWN;
450 /** Lookup a list of names */
452 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
454 POLICY_HND *pol, int num_names,
456 const char ***dom_names,
458 enum lsa_SidType **types)
460 prs_struct qbuf, rbuf;
461 LSA_Q_LOOKUP_NAMES q;
462 LSA_R_LOOKUP_NAMES r;
473 init_q_lookup_names(mem_ctx, &q, pol, num_names, names);
475 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPNAMES,
478 lsa_io_q_lookup_names,
479 lsa_io_r_lookup_names,
480 NT_STATUS_UNSUCCESSFUL);
484 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
485 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
487 /* An actual error occured */
492 /* Return output parameters */
494 if (r.mapped_count == 0) {
495 result = NT_STATUS_NONE_MAPPED;
500 if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
501 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
502 result = NT_STATUS_NO_MEMORY;
506 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
507 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
508 result = NT_STATUS_NO_MEMORY;
512 if (dom_names != NULL) {
513 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
514 if (*dom_names == NULL) {
515 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
516 result = NT_STATUS_NO_MEMORY;
523 if (dom_names != NULL) {
528 for (i = 0; i < num_names; i++) {
529 DOM_RID *t_rids = r.dom_rid;
530 uint32 dom_idx = t_rids[i].rid_idx;
531 uint32 dom_rid = t_rids[i].rid;
532 DOM_SID *sid = &(*sids)[i];
534 /* Translate optimised sid through domain index array */
536 if (dom_idx == 0xffffffff) {
537 /* Nothing to do, this is unknown */
539 (*types)[i] = SID_NAME_UNKNOWN;
543 sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
545 if (dom_rid != 0xffffffff) {
546 sid_append_rid(sid, dom_rid);
549 (*types)[i] = (enum lsa_SidType)t_rids[i].type;
551 if (dom_names == NULL) {
555 (*dom_names)[i] = rpcstr_pull_unistr2_talloc(
556 *dom_names, &ref.ref_dom[dom_idx].uni_dom_name);
564 NTSTATUS rpccli_lsa_query_info_policy_new(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
565 POLICY_HND *pol, uint16 info_class,
568 prs_struct qbuf, rbuf;
576 init_q_query(&q, pol, info_class);
578 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFOPOLICY,
583 NT_STATUS_UNSUCCESSFUL);
587 if (!NT_STATUS_IS_OK(result)) {
598 NTSTATUS rpccli_lsa_query_info_policy2_new(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
599 POLICY_HND *pol, uint16 info_class,
602 prs_struct qbuf, rbuf;
610 init_q_query2(&q, pol, info_class);
612 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFO2,
615 lsa_io_q_query_info2,
616 lsa_io_r_query_info2,
617 NT_STATUS_UNSUCCESSFUL);
621 if (!NT_STATUS_IS_OK(result)) {
634 /** Query info policy
636 * @param domain_sid - returned remote server's domain sid */
638 NTSTATUS rpccli_lsa_query_info_policy(struct rpc_pipe_client *cli,
640 POLICY_HND *pol, uint16 info_class,
641 char **domain_name, DOM_SID **domain_sid)
643 prs_struct qbuf, rbuf;
651 init_q_query(&q, pol, info_class);
653 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFOPOLICY,
658 NT_STATUS_UNSUCCESSFUL);
662 if (!NT_STATUS_IS_OK(result)) {
666 /* Return output parameters */
668 switch (info_class) {
671 if (domain_name && (r.ctr.info.id3.buffer_dom_name != 0)) {
672 *domain_name = unistr2_tdup(mem_ctx,
676 return NT_STATUS_NO_MEMORY;
680 if (domain_sid && (r.ctr.info.id3.buffer_dom_sid != 0)) {
681 *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
683 return NT_STATUS_NO_MEMORY;
685 sid_copy(*domain_sid, &r.ctr.info.id3.dom_sid.sid);
692 if (domain_name && (r.ctr.info.id5.buffer_dom_name != 0)) {
693 *domain_name = unistr2_tdup(mem_ctx,
697 return NT_STATUS_NO_MEMORY;
701 if (domain_sid && (r.ctr.info.id5.buffer_dom_sid != 0)) {
702 *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
704 return NT_STATUS_NO_MEMORY;
706 sid_copy(*domain_sid, &r.ctr.info.id5.dom_sid.sid);
711 DEBUG(3, ("unknown info class %d\n", info_class));
720 /** Query info policy2
722 * @param domain_name - returned remote server's domain name
723 * @param dns_name - returned remote server's dns domain name
724 * @param forest_name - returned remote server's forest name
725 * @param domain_guid - returned remote server's domain guid
726 * @param domain_sid - returned remote server's domain sid */
728 NTSTATUS rpccli_lsa_query_info_policy2(struct rpc_pipe_client *cli,
730 POLICY_HND *pol, uint16 info_class,
731 char **domain_name, char **dns_name,
733 struct GUID **domain_guid,
734 DOM_SID **domain_sid)
736 prs_struct qbuf, rbuf;
739 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
741 if (info_class != 12)
747 init_q_query2(&q, pol, info_class);
749 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYINFO2,
752 lsa_io_q_query_info2,
753 lsa_io_r_query_info2,
754 NT_STATUS_UNSUCCESSFUL);
758 if (!NT_STATUS_IS_OK(result)) {
762 /* Return output parameters */
764 ZERO_STRUCTP(domain_guid);
766 if (domain_name && r.ctr.info.id12.hdr_nb_dom_name.buffer) {
767 *domain_name = unistr2_tdup(mem_ctx,
771 return NT_STATUS_NO_MEMORY;
774 if (dns_name && r.ctr.info.id12.hdr_dns_dom_name.buffer) {
775 *dns_name = unistr2_tdup(mem_ctx,
779 return NT_STATUS_NO_MEMORY;
782 if (forest_name && r.ctr.info.id12.hdr_forest_name.buffer) {
783 *forest_name = unistr2_tdup(mem_ctx,
787 return NT_STATUS_NO_MEMORY;
792 *domain_guid = TALLOC_P(mem_ctx, struct GUID);
794 return NT_STATUS_NO_MEMORY;
797 &r.ctr.info.id12.dom_guid,
798 sizeof(struct GUID));
801 if (domain_sid && r.ctr.info.id12.ptr_dom_sid != 0) {
802 *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
804 return NT_STATUS_NO_MEMORY;
806 sid_copy(*domain_sid,
807 &r.ctr.info.id12.dom_sid.sid);
815 NTSTATUS rpccli_lsa_set_info_policy(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
816 POLICY_HND *pol, uint16 info_class,
819 prs_struct qbuf, rbuf;
827 init_q_set(&q, pol, info_class, ctr);
829 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_SETINFOPOLICY,
834 NT_STATUS_UNSUCCESSFUL);
838 if (!NT_STATUS_IS_OK(result)) {
842 /* Return output parameters */
851 * Enumerate list of trusted domains
853 * @param cli client state (cli_state) structure of the connection
854 * @param mem_ctx memory context
855 * @param pol opened lsa policy handle
856 * @param enum_ctx enumeration context ie. index of first returned domain entry
857 * @param pref_num_domains preferred max number of entries returned in one response
858 * @param num_domains total number of trusted domains returned by response
859 * @param domain_names returned trusted domain names
860 * @param domain_sids returned trusted domain sids
862 * @return nt status code of response
865 NTSTATUS rpccli_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
867 POLICY_HND *pol, uint32 *enum_ctx,
869 char ***domain_names, DOM_SID **domain_sids)
871 prs_struct qbuf, rbuf;
872 LSA_Q_ENUM_TRUST_DOM in;
873 LSA_R_ENUM_TRUST_DOM out;
880 /* 64k is enough for about 2000 trusted domains */
882 init_q_enum_trust_dom(&in, pol, *enum_ctx, 0x10000);
884 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMTRUSTDOM,
887 lsa_io_q_enum_trust_dom,
888 lsa_io_r_enum_trust_dom,
889 NT_STATUS_UNSUCCESSFUL );
892 /* check for an actual error */
894 if ( !NT_STATUS_IS_OK(out.status)
895 && !NT_STATUS_EQUAL(out.status, NT_STATUS_NO_MORE_ENTRIES)
896 && !NT_STATUS_EQUAL(out.status, STATUS_MORE_ENTRIES) )
901 /* Return output parameters */
903 *num_domains = out.count;
904 *enum_ctx = out.enum_context;
908 /* Allocate memory for trusted domain names and sids */
910 if ( !(*domain_names = TALLOC_ARRAY(mem_ctx, char *, out.count)) ) {
911 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
912 return NT_STATUS_NO_MEMORY;
915 if ( !(*domain_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, out.count)) ) {
916 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
917 return NT_STATUS_NO_MEMORY;
920 /* Copy across names and sids */
922 for (i = 0; i < out.count; i++) {
924 rpcstr_pull( tmp, out.domlist->domains[i].name.string->buffer,
925 sizeof(tmp), out.domlist->domains[i].name.length, 0);
926 (*domain_names)[i] = talloc_strdup(mem_ctx, tmp);
928 sid_copy(&(*domain_sids)[i], &out.domlist->domains[i].sid->sid );
935 /** Enumerate privileges*/
937 NTSTATUS rpccli_lsa_enum_privilege(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
938 POLICY_HND *pol, uint32 *enum_context, uint32 pref_max_length,
939 uint32 *count, char ***privs_name, uint32 **privs_high, uint32 **privs_low)
941 prs_struct qbuf, rbuf;
950 init_q_enum_privs(&q, pol, *enum_context, pref_max_length);
952 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUM_PRIVS,
957 NT_STATUS_UNSUCCESSFUL);
961 if (!NT_STATUS_IS_OK(result)) {
965 /* Return output parameters */
967 *enum_context = r.enum_context;
971 if (!((*privs_name = TALLOC_ARRAY(mem_ctx, char *, r.count)))) {
972 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
973 result = NT_STATUS_UNSUCCESSFUL;
977 if (!((*privs_high = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
978 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
979 result = NT_STATUS_UNSUCCESSFUL;
983 if (!((*privs_low = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
984 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
985 result = NT_STATUS_UNSUCCESSFUL;
994 for (i = 0; i < r.count; i++) {
997 rpcstr_pull_unistr2_fstring( name, &r.privs[i].name);
999 (*privs_name)[i] = talloc_strdup(mem_ctx, name);
1001 (*privs_high)[i] = r.privs[i].luid_high;
1002 (*privs_low)[i] = r.privs[i].luid_low;
1010 /** Get privilege name */
1012 NTSTATUS rpccli_lsa_get_dispname(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1013 POLICY_HND *pol, const char *name,
1014 uint16 lang_id, uint16 lang_id_sys,
1015 fstring description, uint16 *lang_id_desc)
1017 prs_struct qbuf, rbuf;
1018 LSA_Q_PRIV_GET_DISPNAME q;
1019 LSA_R_PRIV_GET_DISPNAME r;
1025 init_lsa_priv_get_dispname(&q, pol, name, lang_id, lang_id_sys);
1027 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_PRIV_GET_DISPNAME,
1030 lsa_io_q_priv_get_dispname,
1031 lsa_io_r_priv_get_dispname,
1032 NT_STATUS_UNSUCCESSFUL);
1036 if (!NT_STATUS_IS_OK(result)) {
1040 /* Return output parameters */
1042 rpcstr_pull_unistr2_fstring(description , &r.desc);
1043 *lang_id_desc = r.lang_id;
1050 /** Enumerate list of SIDs */
1052 NTSTATUS rpccli_lsa_enum_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1053 POLICY_HND *pol, uint32 *enum_ctx, uint32 pref_max_length,
1054 uint32 *num_sids, DOM_SID **sids)
1056 prs_struct qbuf, rbuf;
1057 LSA_Q_ENUM_ACCOUNTS q;
1058 LSA_R_ENUM_ACCOUNTS r;
1065 init_lsa_q_enum_accounts(&q, pol, *enum_ctx, pref_max_length);
1067 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUM_ACCOUNTS,
1070 lsa_io_q_enum_accounts,
1071 lsa_io_r_enum_accounts,
1072 NT_STATUS_UNSUCCESSFUL);
1076 if (!NT_STATUS_IS_OK(result)) {
1080 if (r.sids.num_entries==0)
1083 /* Return output parameters */
1085 *sids = TALLOC_ARRAY(mem_ctx, DOM_SID, r.sids.num_entries);
1087 DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
1088 result = NT_STATUS_UNSUCCESSFUL;
1092 /* Copy across names and sids */
1094 for (i = 0; i < r.sids.num_entries; i++) {
1095 sid_copy(&(*sids)[i], &r.sids.sid[i].sid);
1098 *num_sids= r.sids.num_entries;
1099 *enum_ctx = r.enum_context;
1106 /** Create a LSA user handle
1108 * @param cli Handle on an initialised SMB connection
1110 * FIXME: The code is actually identical to open account
1111 * TODO: Check and code what the function should exactly do
1115 NTSTATUS rpccli_lsa_create_account(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1116 POLICY_HND *dom_pol, DOM_SID *sid, uint32 desired_access,
1117 POLICY_HND *user_pol)
1119 prs_struct qbuf, rbuf;
1120 LSA_Q_CREATEACCOUNT q;
1121 LSA_R_CREATEACCOUNT r;
1127 /* Initialise input parameters */
1129 init_lsa_q_create_account(&q, dom_pol, sid, desired_access);
1131 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_CREATEACCOUNT,
1134 lsa_io_q_create_account,
1135 lsa_io_r_create_account,
1136 NT_STATUS_UNSUCCESSFUL);
1138 /* Return output parameters */
1142 if (NT_STATUS_IS_OK(result)) {
1149 /** Open a LSA user handle
1151 * @param cli Handle on an initialised SMB connection */
1153 NTSTATUS rpccli_lsa_open_account(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1154 POLICY_HND *dom_pol, DOM_SID *sid, uint32 des_access,
1155 POLICY_HND *user_pol)
1157 prs_struct qbuf, rbuf;
1158 LSA_Q_OPENACCOUNT q;
1159 LSA_R_OPENACCOUNT r;
1165 /* Initialise input parameters */
1167 init_lsa_q_open_account(&q, dom_pol, sid, des_access);
1169 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENACCOUNT,
1172 lsa_io_q_open_account,
1173 lsa_io_r_open_account,
1174 NT_STATUS_UNSUCCESSFUL);
1176 /* Return output parameters */
1180 if (NT_STATUS_IS_OK(result)) {
1187 /** Enumerate user privileges
1189 * @param cli Handle on an initialised SMB connection */
1191 NTSTATUS rpccli_lsa_enum_privsaccount(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1192 POLICY_HND *pol, uint32 *count, LUID_ATTR **set)
1194 prs_struct qbuf, rbuf;
1195 LSA_Q_ENUMPRIVSACCOUNT q;
1196 LSA_R_ENUMPRIVSACCOUNT r;
1203 /* Initialise input parameters */
1205 init_lsa_q_enum_privsaccount(&q, pol);
1207 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMPRIVSACCOUNT,
1210 lsa_io_q_enum_privsaccount,
1211 lsa_io_r_enum_privsaccount,
1212 NT_STATUS_UNSUCCESSFUL);
1214 /* Return output parameters */
1218 if (!NT_STATUS_IS_OK(result)) {
1225 if (!((*set = TALLOC_ARRAY(mem_ctx, LUID_ATTR, r.count)))) {
1226 DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
1227 result = NT_STATUS_UNSUCCESSFUL;
1231 for (i=0; i<r.count; i++) {
1232 (*set)[i].luid.low = r.set.set[i].luid.low;
1233 (*set)[i].luid.high = r.set.set[i].luid.high;
1234 (*set)[i].attr = r.set.set[i].attr;
1243 /** Get a privilege value given its name */
1245 NTSTATUS rpccli_lsa_lookup_priv_value(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1246 POLICY_HND *pol, const char *name, LUID *luid)
1248 prs_struct qbuf, rbuf;
1249 LSA_Q_LOOKUP_PRIV_VALUE q;
1250 LSA_R_LOOKUP_PRIV_VALUE r;
1256 /* Marshall data and send request */
1258 init_lsa_q_lookup_priv_value(&q, pol, name);
1260 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPPRIVVALUE,
1263 lsa_io_q_lookup_priv_value,
1264 lsa_io_r_lookup_priv_value,
1265 NT_STATUS_UNSUCCESSFUL);
1269 if (!NT_STATUS_IS_OK(result)) {
1273 /* Return output parameters */
1275 (*luid).low=r.luid.low;
1276 (*luid).high=r.luid.high;
1283 /** Query LSA security object */
1285 NTSTATUS rpccli_lsa_query_secobj(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1286 POLICY_HND *pol, uint32 sec_info,
1287 SEC_DESC_BUF **psdb)
1289 prs_struct qbuf, rbuf;
1290 LSA_Q_QUERY_SEC_OBJ q;
1291 LSA_R_QUERY_SEC_OBJ r;
1297 /* Marshall data and send request */
1299 init_q_query_sec_obj(&q, pol, sec_info);
1301 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYSECOBJ,
1304 lsa_io_q_query_sec_obj,
1305 lsa_io_r_query_sec_obj,
1306 NT_STATUS_UNSUCCESSFUL);
1310 if (!NT_STATUS_IS_OK(result)) {
1314 /* Return output parameters */
1325 /* Enumerate account rights This is similar to enum_privileges but
1326 takes a SID directly, avoiding the open_account call.
1329 NTSTATUS rpccli_lsa_enum_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1330 POLICY_HND *pol, DOM_SID *sid,
1331 uint32 *count, char ***priv_names)
1333 prs_struct qbuf, rbuf;
1334 LSA_Q_ENUM_ACCT_RIGHTS q;
1335 LSA_R_ENUM_ACCT_RIGHTS r;
1338 fstring *privileges;
1344 /* Marshall data and send request */
1345 init_q_enum_acct_rights(&q, pol, 2, sid);
1347 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMACCTRIGHTS,
1350 lsa_io_q_enum_acct_rights,
1351 lsa_io_r_enum_acct_rights,
1352 NT_STATUS_UNSUCCESSFUL);
1356 if (!NT_STATUS_IS_OK(result)) {
1366 privileges = TALLOC_ARRAY( mem_ctx, fstring, *count );
1367 names = TALLOC_ARRAY( mem_ctx, char *, *count );
1369 if ((privileges == NULL) || (names == NULL)) {
1370 TALLOC_FREE(privileges);
1372 return NT_STATUS_NO_MEMORY;
1375 for ( i=0; i<*count; i++ ) {
1376 UNISTR4 *uni_string = &r.rights->strings[i];
1378 if ( !uni_string->string )
1381 rpcstr_pull( privileges[i], uni_string->string->buffer, sizeof(privileges[i]), -1, STR_TERMINATE );
1383 /* now copy to the return array */
1384 names[i] = talloc_strdup( mem_ctx, privileges[i] );
1387 *priv_names = names;
1396 /* add account rights to an account. */
1398 NTSTATUS rpccli_lsa_add_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1399 POLICY_HND *pol, DOM_SID sid,
1400 uint32 count, const char **privs_name)
1402 prs_struct qbuf, rbuf;
1403 LSA_Q_ADD_ACCT_RIGHTS q;
1404 LSA_R_ADD_ACCT_RIGHTS r;
1410 /* Marshall data and send request */
1411 init_q_add_acct_rights(&q, pol, &sid, count, privs_name);
1413 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ADDACCTRIGHTS,
1416 lsa_io_q_add_acct_rights,
1417 lsa_io_r_add_acct_rights,
1418 NT_STATUS_UNSUCCESSFUL);
1422 if (!NT_STATUS_IS_OK(result)) {
1431 /* remove account rights for an account. */
1433 NTSTATUS rpccli_lsa_remove_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1434 POLICY_HND *pol, DOM_SID sid, BOOL removeall,
1435 uint32 count, const char **privs_name)
1437 prs_struct qbuf, rbuf;
1438 LSA_Q_REMOVE_ACCT_RIGHTS q;
1439 LSA_R_REMOVE_ACCT_RIGHTS r;
1445 /* Marshall data and send request */
1446 init_q_remove_acct_rights(&q, pol, &sid, removeall?1:0, count, privs_name);
1448 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_REMOVEACCTRIGHTS,
1451 lsa_io_q_remove_acct_rights,
1452 lsa_io_r_remove_acct_rights,
1453 NT_STATUS_UNSUCCESSFUL);
1457 if (!NT_STATUS_IS_OK(result)) {
1468 /** An example of how to use the routines in this file. Fetch a DOMAIN
1469 sid. Does complete cli setup / teardown anonymously. */
1471 BOOL fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
1473 extern pstring global_myname;
1474 struct cli_state *cli;
1480 if((cli = cli_initialise()) == NULL) {
1481 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
1485 if(!resolve_name( remote_machine, &cli->dest_ip, 0x20)) {
1486 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
1490 if (!cli_connect(cli, remote_machine, &cli->dest_ip)) {
1491 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
1492 machine %s. Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1496 if (!attempt_netbios_session_request(cli, global_myname, remote_machine, &cli->dest_ip)) {
1497 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n",
1502 cli->protocol = PROTOCOL_NT1;
1504 if (!cli_negprot(cli)) {
1505 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
1506 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1510 if (cli->protocol != PROTOCOL_NT1) {
1511 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
1517 * Do an anonymous session setup.
1520 if (!cli_session_setup(cli, "", "", 0, "", 0, "")) {
1521 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
1522 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1526 if (!(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
1527 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
1532 if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
1533 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
1534 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1538 /* Fetch domain sid */
1540 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1541 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
1545 result = cli_lsa_open_policy(cli, cli->mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
1546 if (!NT_STATUS_IS_OK(result)) {
1547 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
1548 nt_errstr(result) ));
1552 result = cli_lsa_query_info_policy(cli, cli->mem_ctx, &lsa_pol, 5, domain, psid);
1553 if (!NT_STATUS_IS_OK(result)) {
1554 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
1555 nt_errstr(result) ));
1569 NTSTATUS rpccli_lsa_open_trusted_domain(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1570 POLICY_HND *pol, DOM_SID *dom_sid, uint32 access_mask,
1571 POLICY_HND *trustdom_pol)
1573 prs_struct qbuf, rbuf;
1574 LSA_Q_OPEN_TRUSTED_DOMAIN q;
1575 LSA_R_OPEN_TRUSTED_DOMAIN r;
1581 /* Initialise input parameters */
1583 init_lsa_q_open_trusted_domain(&q, pol, dom_sid, access_mask);
1585 /* Marshall data and send request */
1587 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENTRUSTDOM,
1590 lsa_io_q_open_trusted_domain,
1591 lsa_io_r_open_trusted_domain,
1592 NT_STATUS_UNSUCCESSFUL);
1594 /* Return output parameters */
1598 if (NT_STATUS_IS_OK(result)) {
1599 *trustdom_pol = r.handle;
1605 NTSTATUS rpccli_lsa_query_trusted_domain_info(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1608 LSA_TRUSTED_DOMAIN_INFO **info)
1610 prs_struct qbuf, rbuf;
1611 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO q;
1612 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1613 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1618 /* Marshall data and send request */
1620 init_q_query_trusted_domain_info(&q, pol, info_class);
1622 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFO,
1625 lsa_io_q_query_trusted_domain_info,
1626 lsa_io_r_query_trusted_domain_info,
1627 NT_STATUS_UNSUCCESSFUL);
1631 if (!NT_STATUS_IS_OK(result)) {
1641 NTSTATUS rpccli_lsa_open_trusted_domain_by_name(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1642 POLICY_HND *pol, const char *name, uint32 access_mask,
1643 POLICY_HND *trustdom_pol)
1645 prs_struct qbuf, rbuf;
1646 LSA_Q_OPEN_TRUSTED_DOMAIN_BY_NAME q;
1647 LSA_R_OPEN_TRUSTED_DOMAIN_BY_NAME r;
1653 /* Initialise input parameters */
1655 init_lsa_q_open_trusted_domain_by_name(&q, pol, name, access_mask);
1657 /* Marshall data and send request */
1659 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENTRUSTDOMBYNAME,
1662 lsa_io_q_open_trusted_domain_by_name,
1663 lsa_io_r_open_trusted_domain_by_name,
1664 NT_STATUS_UNSUCCESSFUL);
1666 /* Return output parameters */
1670 if (NT_STATUS_IS_OK(result)) {
1671 *trustdom_pol = r.handle;
1678 NTSTATUS rpccli_lsa_query_trusted_domain_info_by_sid(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1680 uint16 info_class, DOM_SID *dom_sid,
1681 LSA_TRUSTED_DOMAIN_INFO **info)
1683 prs_struct qbuf, rbuf;
1684 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO_BY_SID q;
1685 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1686 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1691 /* Marshall data and send request */
1693 init_q_query_trusted_domain_info_by_sid(&q, pol, info_class, dom_sid);
1695 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFOBYSID,
1698 lsa_io_q_query_trusted_domain_info_by_sid,
1699 lsa_io_r_query_trusted_domain_info,
1700 NT_STATUS_UNSUCCESSFUL);
1704 if (!NT_STATUS_IS_OK(result)) {
1715 NTSTATUS rpccli_lsa_query_trusted_domain_info_by_name(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1717 uint16 info_class, const char *domain_name,
1718 LSA_TRUSTED_DOMAIN_INFO **info)
1720 prs_struct qbuf, rbuf;
1721 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO_BY_NAME q;
1722 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1723 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1728 /* Marshall data and send request */
1730 init_q_query_trusted_domain_info_by_name(&q, pol, info_class, domain_name);
1732 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFOBYNAME,
1735 lsa_io_q_query_trusted_domain_info_by_name,
1736 lsa_io_r_query_trusted_domain_info,
1737 NT_STATUS_UNSUCCESSFUL);
1741 if (!NT_STATUS_IS_OK(result)) {
1752 NTSTATUS cli_lsa_query_domain_info_policy(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1754 uint16 info_class, LSA_DOM_INFO_UNION **info)
1756 prs_struct qbuf, rbuf;
1757 LSA_Q_QUERY_DOM_INFO_POLICY q;
1758 LSA_R_QUERY_DOM_INFO_POLICY r;
1759 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1764 /* Marshall data and send request */
1766 init_q_query_dom_info(&q, pol, info_class);
1768 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYDOMINFOPOL,
1771 lsa_io_q_query_dom_info,
1772 lsa_io_r_query_dom_info,
1773 NT_STATUS_UNSUCCESSFUL);
1777 if (!NT_STATUS_IS_OK(result)) {