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 DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
175 nt_errstr(r.status), r.mapped_count));
177 if (!NT_STATUS_IS_OK(r.status) &&
178 !NT_STATUS_EQUAL(r.status, NT_STATUS_NONE_MAPPED) &&
179 !NT_STATUS_EQUAL(r.status, STATUS_SOME_UNMAPPED))
181 /* An actual error occured */
186 /* Return output parameters */
188 if (NT_STATUS_EQUAL(r.status, NT_STATUS_NONE_MAPPED) ||
189 (r.mapped_count == 0))
191 for (i = 0; i < num_sids; i++) {
194 (types)[i] = SID_NAME_UNKNOWN;
196 result = NT_STATUS_NONE_MAPPED;
200 for (i = 0; i < num_sids; i++) {
201 fstring name, dom_name;
202 uint32 dom_idx = r.names.name[i].domain_idx;
204 /* Translate optimised name through domain index array */
206 if (dom_idx != 0xffffffff) {
208 rpcstr_pull_unistr2_fstring(
209 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
210 rpcstr_pull_unistr2_fstring(
211 name, &r.names.uni_name[i]);
213 (names)[i] = talloc_strdup(mem_ctx, name);
214 (domains)[i] = talloc_strdup(mem_ctx, dom_name);
215 (types)[i] = (enum lsa_SidType)r.names.name[i].sid_name_use;
217 if (((names)[i] == NULL) || ((domains)[i] == NULL)) {
218 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
219 result = NT_STATUS_UNSUCCESSFUL;
226 (types)[i] = SID_NAME_UNKNOWN;
231 TALLOC_FREE(tmp_ctx);
235 /* Lookup a list of sids
237 * do it the right way: there is a limit (of 20480 for w2k3) entries
238 * returned by this call. when the sids list contains more entries,
239 * empty lists are returned. This version of lsa_lookup_sids passes
240 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
242 /* This constant defines the limit of how many sids to look up
243 * in one call (maximum). the limit from the server side is
244 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
245 #define LOOKUP_SIDS_HUNK_SIZE 1000
247 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
254 enum lsa_SidType **types)
256 NTSTATUS result = NT_STATUS_OK;
258 int sids_processed = 0;
259 const DOM_SID *hunk_sids = sids;
260 char **hunk_domains = NULL;
261 char **hunk_names = NULL;
262 enum lsa_SidType *hunk_types = NULL;
265 if (!((*domains) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
266 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
267 result = NT_STATUS_NO_MEMORY;
271 if (!((*names) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
272 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
273 result = NT_STATUS_NO_MEMORY;
277 if (!((*types) = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
278 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
279 result = NT_STATUS_NO_MEMORY;
288 sids_left = num_sids;
289 hunk_domains = *domains;
293 while (sids_left > 0) {
295 NTSTATUS hunk_result = NT_STATUS_OK;
297 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
298 ? LOOKUP_SIDS_HUNK_SIZE
301 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
304 sids_processed + hunk_num_sids - 1,
307 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
316 if (!NT_STATUS_IS_OK(hunk_result) &&
317 !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
318 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
320 /* An actual error occured */
321 result = hunk_result;
325 /* adapt overall result */
326 if (( NT_STATUS_IS_OK(result) &&
327 !NT_STATUS_IS_OK(hunk_result))
329 ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
330 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
332 result = STATUS_SOME_UNMAPPED;
335 sids_left -= hunk_num_sids;
336 sids_processed += hunk_num_sids; /* only used in DEBUG */
337 hunk_sids += hunk_num_sids;
338 hunk_domains += hunk_num_sids;
339 hunk_names += hunk_num_sids;
340 hunk_types += hunk_num_sids;
346 TALLOC_FREE(*domains);
352 /** Lookup a list of names */
354 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
356 POLICY_HND *pol, int num_names,
358 const char ***dom_names,
361 enum lsa_SidType **types)
363 prs_struct qbuf, rbuf;
364 LSA_Q_LOOKUP_NAMES q;
365 LSA_R_LOOKUP_NAMES r;
376 init_q_lookup_names(mem_ctx, &q, pol, num_names, names, level);
378 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPNAMES,
381 lsa_io_q_lookup_names,
382 lsa_io_r_lookup_names,
383 NT_STATUS_UNSUCCESSFUL);
387 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
388 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
390 /* An actual error occured */
395 /* Return output parameters */
397 if (r.mapped_count == 0) {
398 result = NT_STATUS_NONE_MAPPED;
403 if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
404 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
405 result = NT_STATUS_NO_MEMORY;
409 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
410 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
411 result = NT_STATUS_NO_MEMORY;
415 if (dom_names != NULL) {
416 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
417 if (*dom_names == NULL) {
418 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
419 result = NT_STATUS_NO_MEMORY;
426 if (dom_names != NULL) {
431 for (i = 0; i < num_names; i++) {
432 DOM_RID *t_rids = r.dom_rid;
433 uint32 dom_idx = t_rids[i].rid_idx;
434 uint32 dom_rid = t_rids[i].rid;
435 DOM_SID *sid = &(*sids)[i];
437 /* Translate optimised sid through domain index array */
439 if (dom_idx == 0xffffffff) {
440 /* Nothing to do, this is unknown */
442 (*types)[i] = SID_NAME_UNKNOWN;
446 sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
448 if (dom_rid != 0xffffffff) {
449 sid_append_rid(sid, dom_rid);
452 (*types)[i] = (enum lsa_SidType)t_rids[i].type;
454 if (dom_names == NULL) {
458 (*dom_names)[i] = rpcstr_pull_unistr2_talloc(
459 *dom_names, &ref.ref_dom[dom_idx].uni_dom_name);
467 NTSTATUS rpccli_lsa_query_info_policy_new(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
468 POLICY_HND *pol, uint16 info_class,
471 prs_struct qbuf, rbuf;
479 init_q_query(&q, pol, info_class);
481 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFOPOLICY,
486 NT_STATUS_UNSUCCESSFUL);
490 if (!NT_STATUS_IS_OK(result)) {
501 NTSTATUS rpccli_lsa_query_info_policy2_new(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
502 POLICY_HND *pol, uint16 info_class,
505 prs_struct qbuf, rbuf;
513 init_q_query2(&q, pol, info_class);
515 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFO2,
518 lsa_io_q_query_info2,
519 lsa_io_r_query_info2,
520 NT_STATUS_UNSUCCESSFUL);
524 if (!NT_STATUS_IS_OK(result)) {
537 /** Query info policy
539 * @param domain_sid - returned remote server's domain sid */
541 NTSTATUS rpccli_lsa_query_info_policy(struct rpc_pipe_client *cli,
543 POLICY_HND *pol, uint16 info_class,
544 char **domain_name, DOM_SID **domain_sid)
546 prs_struct qbuf, rbuf;
554 init_q_query(&q, pol, info_class);
556 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFOPOLICY,
561 NT_STATUS_UNSUCCESSFUL);
565 if (!NT_STATUS_IS_OK(result)) {
569 /* Return output parameters */
571 switch (info_class) {
574 if (domain_name && (r.ctr.info.id3.buffer_dom_name != 0)) {
575 *domain_name = unistr2_tdup(mem_ctx,
579 return NT_STATUS_NO_MEMORY;
583 if (domain_sid && (r.ctr.info.id3.buffer_dom_sid != 0)) {
584 *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
586 return NT_STATUS_NO_MEMORY;
588 sid_copy(*domain_sid, &r.ctr.info.id3.dom_sid.sid);
595 if (domain_name && (r.ctr.info.id5.buffer_dom_name != 0)) {
596 *domain_name = unistr2_tdup(mem_ctx,
600 return NT_STATUS_NO_MEMORY;
604 if (domain_sid && (r.ctr.info.id5.buffer_dom_sid != 0)) {
605 *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
607 return NT_STATUS_NO_MEMORY;
609 sid_copy(*domain_sid, &r.ctr.info.id5.dom_sid.sid);
614 DEBUG(3, ("unknown info class %d\n", info_class));
623 /** Query info policy2
625 * @param domain_name - returned remote server's domain name
626 * @param dns_name - returned remote server's dns domain name
627 * @param forest_name - returned remote server's forest name
628 * @param domain_guid - returned remote server's domain guid
629 * @param domain_sid - returned remote server's domain sid */
631 NTSTATUS rpccli_lsa_query_info_policy2(struct rpc_pipe_client *cli,
633 POLICY_HND *pol, uint16 info_class,
634 char **domain_name, char **dns_name,
636 struct GUID **domain_guid,
637 DOM_SID **domain_sid)
639 prs_struct qbuf, rbuf;
642 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
644 if (info_class != 12)
650 init_q_query2(&q, pol, info_class);
652 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYINFO2,
655 lsa_io_q_query_info2,
656 lsa_io_r_query_info2,
657 NT_STATUS_UNSUCCESSFUL);
661 if (!NT_STATUS_IS_OK(result)) {
665 /* Return output parameters */
667 ZERO_STRUCTP(domain_guid);
669 if (domain_name && r.ctr.info.id12.hdr_nb_dom_name.buffer) {
670 *domain_name = unistr2_tdup(mem_ctx,
674 return NT_STATUS_NO_MEMORY;
677 if (dns_name && r.ctr.info.id12.hdr_dns_dom_name.buffer) {
678 *dns_name = unistr2_tdup(mem_ctx,
682 return NT_STATUS_NO_MEMORY;
685 if (forest_name && r.ctr.info.id12.hdr_forest_name.buffer) {
686 *forest_name = unistr2_tdup(mem_ctx,
690 return NT_STATUS_NO_MEMORY;
695 *domain_guid = TALLOC_P(mem_ctx, struct GUID);
697 return NT_STATUS_NO_MEMORY;
700 &r.ctr.info.id12.dom_guid,
701 sizeof(struct GUID));
704 if (domain_sid && r.ctr.info.id12.ptr_dom_sid != 0) {
705 *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
707 return NT_STATUS_NO_MEMORY;
709 sid_copy(*domain_sid,
710 &r.ctr.info.id12.dom_sid.sid);
718 NTSTATUS rpccli_lsa_set_info_policy(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
719 POLICY_HND *pol, uint16 info_class,
722 prs_struct qbuf, rbuf;
730 init_q_set(&q, pol, info_class, ctr);
732 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_SETINFOPOLICY,
737 NT_STATUS_UNSUCCESSFUL);
741 if (!NT_STATUS_IS_OK(result)) {
745 /* Return output parameters */
754 * Enumerate list of trusted domains
756 * @param cli client state (cli_state) structure of the connection
757 * @param mem_ctx memory context
758 * @param pol opened lsa policy handle
759 * @param enum_ctx enumeration context ie. index of first returned domain entry
760 * @param pref_num_domains preferred max number of entries returned in one response
761 * @param num_domains total number of trusted domains returned by response
762 * @param domain_names returned trusted domain names
763 * @param domain_sids returned trusted domain sids
765 * @return nt status code of response
768 NTSTATUS rpccli_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
770 POLICY_HND *pol, uint32 *enum_ctx,
772 char ***domain_names, DOM_SID **domain_sids)
774 prs_struct qbuf, rbuf;
775 LSA_Q_ENUM_TRUST_DOM in;
776 LSA_R_ENUM_TRUST_DOM out;
783 /* 64k is enough for about 2000 trusted domains */
785 init_q_enum_trust_dom(&in, pol, *enum_ctx, 0x10000);
787 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMTRUSTDOM,
790 lsa_io_q_enum_trust_dom,
791 lsa_io_r_enum_trust_dom,
792 NT_STATUS_UNSUCCESSFUL );
795 /* check for an actual error */
797 if ( !NT_STATUS_IS_OK(out.status)
798 && !NT_STATUS_EQUAL(out.status, NT_STATUS_NO_MORE_ENTRIES)
799 && !NT_STATUS_EQUAL(out.status, STATUS_MORE_ENTRIES) )
804 /* Return output parameters */
806 *num_domains = out.count;
807 *enum_ctx = out.enum_context;
811 /* Allocate memory for trusted domain names and sids */
813 if ( !(*domain_names = TALLOC_ARRAY(mem_ctx, char *, out.count)) ) {
814 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
815 return NT_STATUS_NO_MEMORY;
818 if ( !(*domain_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, out.count)) ) {
819 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
820 return NT_STATUS_NO_MEMORY;
823 /* Copy across names and sids */
825 for (i = 0; i < out.count; i++) {
827 rpcstr_pull( tmp, out.domlist->domains[i].name.string->buffer,
828 sizeof(tmp), out.domlist->domains[i].name.length, 0);
829 (*domain_names)[i] = talloc_strdup(mem_ctx, tmp);
831 sid_copy(&(*domain_sids)[i], &out.domlist->domains[i].sid->sid );
838 /** Enumerate privileges*/
840 NTSTATUS rpccli_lsa_enum_privilege(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
841 POLICY_HND *pol, uint32 *enum_context, uint32 pref_max_length,
842 uint32 *count, char ***privs_name, uint32 **privs_high, uint32 **privs_low)
844 prs_struct qbuf, rbuf;
853 init_q_enum_privs(&q, pol, *enum_context, pref_max_length);
855 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUM_PRIVS,
860 NT_STATUS_UNSUCCESSFUL);
864 if (!NT_STATUS_IS_OK(result)) {
868 /* Return output parameters */
870 *enum_context = r.enum_context;
874 if (!((*privs_name = TALLOC_ARRAY(mem_ctx, char *, r.count)))) {
875 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
876 result = NT_STATUS_UNSUCCESSFUL;
880 if (!((*privs_high = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
881 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
882 result = NT_STATUS_UNSUCCESSFUL;
886 if (!((*privs_low = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
887 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
888 result = NT_STATUS_UNSUCCESSFUL;
897 for (i = 0; i < r.count; i++) {
900 rpcstr_pull_unistr2_fstring( name, &r.privs[i].name);
902 (*privs_name)[i] = talloc_strdup(mem_ctx, name);
904 (*privs_high)[i] = r.privs[i].luid_high;
905 (*privs_low)[i] = r.privs[i].luid_low;
913 /** Get privilege name */
915 NTSTATUS rpccli_lsa_get_dispname(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
916 POLICY_HND *pol, const char *name,
917 uint16 lang_id, uint16 lang_id_sys,
918 fstring description, uint16 *lang_id_desc)
920 prs_struct qbuf, rbuf;
921 LSA_Q_PRIV_GET_DISPNAME q;
922 LSA_R_PRIV_GET_DISPNAME r;
928 init_lsa_priv_get_dispname(&q, pol, name, lang_id, lang_id_sys);
930 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_PRIV_GET_DISPNAME,
933 lsa_io_q_priv_get_dispname,
934 lsa_io_r_priv_get_dispname,
935 NT_STATUS_UNSUCCESSFUL);
939 if (!NT_STATUS_IS_OK(result)) {
943 /* Return output parameters */
945 rpcstr_pull_unistr2_fstring(description , &r.desc);
946 *lang_id_desc = r.lang_id;
953 /** Enumerate list of SIDs */
955 NTSTATUS rpccli_lsa_enum_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
956 POLICY_HND *pol, uint32 *enum_ctx, uint32 pref_max_length,
957 uint32 *num_sids, DOM_SID **sids)
959 prs_struct qbuf, rbuf;
960 LSA_Q_ENUM_ACCOUNTS q;
961 LSA_R_ENUM_ACCOUNTS r;
968 init_lsa_q_enum_accounts(&q, pol, *enum_ctx, pref_max_length);
970 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUM_ACCOUNTS,
973 lsa_io_q_enum_accounts,
974 lsa_io_r_enum_accounts,
975 NT_STATUS_UNSUCCESSFUL);
979 if (!NT_STATUS_IS_OK(result)) {
983 if (r.sids.num_entries==0)
986 /* Return output parameters */
988 *sids = TALLOC_ARRAY(mem_ctx, DOM_SID, r.sids.num_entries);
990 DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
991 result = NT_STATUS_UNSUCCESSFUL;
995 /* Copy across names and sids */
997 for (i = 0; i < r.sids.num_entries; i++) {
998 sid_copy(&(*sids)[i], &r.sids.sid[i].sid);
1001 *num_sids= r.sids.num_entries;
1002 *enum_ctx = r.enum_context;
1009 /** Create a LSA user handle
1011 * @param cli Handle on an initialised SMB connection
1013 * FIXME: The code is actually identical to open account
1014 * TODO: Check and code what the function should exactly do
1018 NTSTATUS rpccli_lsa_create_account(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1019 POLICY_HND *dom_pol, DOM_SID *sid, uint32 desired_access,
1020 POLICY_HND *user_pol)
1022 prs_struct qbuf, rbuf;
1023 LSA_Q_CREATEACCOUNT q;
1024 LSA_R_CREATEACCOUNT r;
1030 /* Initialise input parameters */
1032 init_lsa_q_create_account(&q, dom_pol, sid, desired_access);
1034 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_CREATEACCOUNT,
1037 lsa_io_q_create_account,
1038 lsa_io_r_create_account,
1039 NT_STATUS_UNSUCCESSFUL);
1041 /* Return output parameters */
1045 if (NT_STATUS_IS_OK(result)) {
1052 /** Open a LSA user handle
1054 * @param cli Handle on an initialised SMB connection */
1056 NTSTATUS rpccli_lsa_open_account(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1057 POLICY_HND *dom_pol, DOM_SID *sid, uint32 des_access,
1058 POLICY_HND *user_pol)
1060 prs_struct qbuf, rbuf;
1061 LSA_Q_OPENACCOUNT q;
1062 LSA_R_OPENACCOUNT r;
1068 /* Initialise input parameters */
1070 init_lsa_q_open_account(&q, dom_pol, sid, des_access);
1072 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENACCOUNT,
1075 lsa_io_q_open_account,
1076 lsa_io_r_open_account,
1077 NT_STATUS_UNSUCCESSFUL);
1079 /* Return output parameters */
1083 if (NT_STATUS_IS_OK(result)) {
1090 /** Enumerate user privileges
1092 * @param cli Handle on an initialised SMB connection */
1094 NTSTATUS rpccli_lsa_enum_privsaccount(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1095 POLICY_HND *pol, uint32 *count, LUID_ATTR **set)
1097 prs_struct qbuf, rbuf;
1098 LSA_Q_ENUMPRIVSACCOUNT q;
1099 LSA_R_ENUMPRIVSACCOUNT r;
1106 /* Initialise input parameters */
1108 init_lsa_q_enum_privsaccount(&q, pol);
1110 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMPRIVSACCOUNT,
1113 lsa_io_q_enum_privsaccount,
1114 lsa_io_r_enum_privsaccount,
1115 NT_STATUS_UNSUCCESSFUL);
1117 /* Return output parameters */
1121 if (!NT_STATUS_IS_OK(result)) {
1128 if (!((*set = TALLOC_ARRAY(mem_ctx, LUID_ATTR, r.count)))) {
1129 DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
1130 result = NT_STATUS_UNSUCCESSFUL;
1134 for (i=0; i<r.count; i++) {
1135 (*set)[i].luid.low = r.set.set[i].luid.low;
1136 (*set)[i].luid.high = r.set.set[i].luid.high;
1137 (*set)[i].attr = r.set.set[i].attr;
1146 /** Get a privilege value given its name */
1148 NTSTATUS rpccli_lsa_lookup_priv_value(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1149 POLICY_HND *pol, const char *name, LUID *luid)
1151 prs_struct qbuf, rbuf;
1152 LSA_Q_LOOKUP_PRIV_VALUE q;
1153 LSA_R_LOOKUP_PRIV_VALUE r;
1159 /* Marshall data and send request */
1161 init_lsa_q_lookup_priv_value(&q, pol, name);
1163 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPPRIVVALUE,
1166 lsa_io_q_lookup_priv_value,
1167 lsa_io_r_lookup_priv_value,
1168 NT_STATUS_UNSUCCESSFUL);
1172 if (!NT_STATUS_IS_OK(result)) {
1176 /* Return output parameters */
1178 (*luid).low=r.luid.low;
1179 (*luid).high=r.luid.high;
1186 /** Query LSA security object */
1188 NTSTATUS rpccli_lsa_query_secobj(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1189 POLICY_HND *pol, uint32 sec_info,
1190 SEC_DESC_BUF **psdb)
1192 prs_struct qbuf, rbuf;
1193 LSA_Q_QUERY_SEC_OBJ q;
1194 LSA_R_QUERY_SEC_OBJ r;
1200 /* Marshall data and send request */
1202 init_q_query_sec_obj(&q, pol, sec_info);
1204 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYSECOBJ,
1207 lsa_io_q_query_sec_obj,
1208 lsa_io_r_query_sec_obj,
1209 NT_STATUS_UNSUCCESSFUL);
1213 if (!NT_STATUS_IS_OK(result)) {
1217 /* Return output parameters */
1228 /* Enumerate account rights This is similar to enum_privileges but
1229 takes a SID directly, avoiding the open_account call.
1232 NTSTATUS rpccli_lsa_enum_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1233 POLICY_HND *pol, DOM_SID *sid,
1234 uint32 *count, char ***priv_names)
1236 prs_struct qbuf, rbuf;
1237 LSA_Q_ENUM_ACCT_RIGHTS q;
1238 LSA_R_ENUM_ACCT_RIGHTS r;
1241 fstring *privileges;
1247 /* Marshall data and send request */
1248 init_q_enum_acct_rights(&q, pol, 2, sid);
1250 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMACCTRIGHTS,
1253 lsa_io_q_enum_acct_rights,
1254 lsa_io_r_enum_acct_rights,
1255 NT_STATUS_UNSUCCESSFUL);
1259 if (!NT_STATUS_IS_OK(result)) {
1269 privileges = TALLOC_ARRAY( mem_ctx, fstring, *count );
1270 names = TALLOC_ARRAY( mem_ctx, char *, *count );
1272 if ((privileges == NULL) || (names == NULL)) {
1273 TALLOC_FREE(privileges);
1275 return NT_STATUS_NO_MEMORY;
1278 for ( i=0; i<*count; i++ ) {
1279 UNISTR4 *uni_string = &r.rights->strings[i];
1281 if ( !uni_string->string )
1284 rpcstr_pull( privileges[i], uni_string->string->buffer, sizeof(privileges[i]), -1, STR_TERMINATE );
1286 /* now copy to the return array */
1287 names[i] = talloc_strdup( mem_ctx, privileges[i] );
1290 *priv_names = names;
1299 /* add account rights to an account. */
1301 NTSTATUS rpccli_lsa_add_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1302 POLICY_HND *pol, DOM_SID sid,
1303 uint32 count, const char **privs_name)
1305 prs_struct qbuf, rbuf;
1306 LSA_Q_ADD_ACCT_RIGHTS q;
1307 LSA_R_ADD_ACCT_RIGHTS r;
1313 /* Marshall data and send request */
1314 init_q_add_acct_rights(&q, pol, &sid, count, privs_name);
1316 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ADDACCTRIGHTS,
1319 lsa_io_q_add_acct_rights,
1320 lsa_io_r_add_acct_rights,
1321 NT_STATUS_UNSUCCESSFUL);
1325 if (!NT_STATUS_IS_OK(result)) {
1334 /* remove account rights for an account. */
1336 NTSTATUS rpccli_lsa_remove_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1337 POLICY_HND *pol, DOM_SID sid, BOOL removeall,
1338 uint32 count, const char **privs_name)
1340 prs_struct qbuf, rbuf;
1341 LSA_Q_REMOVE_ACCT_RIGHTS q;
1342 LSA_R_REMOVE_ACCT_RIGHTS r;
1348 /* Marshall data and send request */
1349 init_q_remove_acct_rights(&q, pol, &sid, removeall?1:0, count, privs_name);
1351 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_REMOVEACCTRIGHTS,
1354 lsa_io_q_remove_acct_rights,
1355 lsa_io_r_remove_acct_rights,
1356 NT_STATUS_UNSUCCESSFUL);
1360 if (!NT_STATUS_IS_OK(result)) {
1371 /** An example of how to use the routines in this file. Fetch a DOMAIN
1372 sid. Does complete cli setup / teardown anonymously. */
1374 BOOL fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
1376 extern pstring global_myname;
1377 struct cli_state *cli;
1383 if((cli = cli_initialise()) == NULL) {
1384 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
1388 if(!resolve_name( remote_machine, &cli->dest_ip, 0x20)) {
1389 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
1393 if (!cli_connect(cli, remote_machine, &cli->dest_ip)) {
1394 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
1395 machine %s. Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1399 if (!attempt_netbios_session_request(cli, global_myname, remote_machine, &cli->dest_ip)) {
1400 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n",
1405 cli->protocol = PROTOCOL_NT1;
1407 if (!cli_negprot(cli)) {
1408 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
1409 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1413 if (cli->protocol != PROTOCOL_NT1) {
1414 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
1420 * Do an anonymous session setup.
1423 if (!cli_session_setup(cli, "", "", 0, "", 0, "")) {
1424 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
1425 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1429 if (!(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
1430 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
1435 if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
1436 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
1437 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1441 /* Fetch domain sid */
1443 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1444 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
1448 result = cli_lsa_open_policy(cli, cli->mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
1449 if (!NT_STATUS_IS_OK(result)) {
1450 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
1451 nt_errstr(result) ));
1455 result = cli_lsa_query_info_policy(cli, cli->mem_ctx, &lsa_pol, 5, domain, psid);
1456 if (!NT_STATUS_IS_OK(result)) {
1457 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
1458 nt_errstr(result) ));
1472 NTSTATUS rpccli_lsa_open_trusted_domain(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1473 POLICY_HND *pol, DOM_SID *dom_sid, uint32 access_mask,
1474 POLICY_HND *trustdom_pol)
1476 prs_struct qbuf, rbuf;
1477 LSA_Q_OPEN_TRUSTED_DOMAIN q;
1478 LSA_R_OPEN_TRUSTED_DOMAIN r;
1484 /* Initialise input parameters */
1486 init_lsa_q_open_trusted_domain(&q, pol, dom_sid, access_mask);
1488 /* Marshall data and send request */
1490 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENTRUSTDOM,
1493 lsa_io_q_open_trusted_domain,
1494 lsa_io_r_open_trusted_domain,
1495 NT_STATUS_UNSUCCESSFUL);
1497 /* Return output parameters */
1501 if (NT_STATUS_IS_OK(result)) {
1502 *trustdom_pol = r.handle;
1508 NTSTATUS rpccli_lsa_query_trusted_domain_info(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1511 LSA_TRUSTED_DOMAIN_INFO **info)
1513 prs_struct qbuf, rbuf;
1514 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO q;
1515 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1516 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1521 /* Marshall data and send request */
1523 init_q_query_trusted_domain_info(&q, pol, info_class);
1525 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFO,
1528 lsa_io_q_query_trusted_domain_info,
1529 lsa_io_r_query_trusted_domain_info,
1530 NT_STATUS_UNSUCCESSFUL);
1534 if (!NT_STATUS_IS_OK(result)) {
1544 NTSTATUS rpccli_lsa_open_trusted_domain_by_name(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1545 POLICY_HND *pol, const char *name, uint32 access_mask,
1546 POLICY_HND *trustdom_pol)
1548 prs_struct qbuf, rbuf;
1549 LSA_Q_OPEN_TRUSTED_DOMAIN_BY_NAME q;
1550 LSA_R_OPEN_TRUSTED_DOMAIN_BY_NAME r;
1556 /* Initialise input parameters */
1558 init_lsa_q_open_trusted_domain_by_name(&q, pol, name, access_mask);
1560 /* Marshall data and send request */
1562 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENTRUSTDOMBYNAME,
1565 lsa_io_q_open_trusted_domain_by_name,
1566 lsa_io_r_open_trusted_domain_by_name,
1567 NT_STATUS_UNSUCCESSFUL);
1569 /* Return output parameters */
1573 if (NT_STATUS_IS_OK(result)) {
1574 *trustdom_pol = r.handle;
1581 NTSTATUS rpccli_lsa_query_trusted_domain_info_by_sid(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1583 uint16 info_class, DOM_SID *dom_sid,
1584 LSA_TRUSTED_DOMAIN_INFO **info)
1586 prs_struct qbuf, rbuf;
1587 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO_BY_SID q;
1588 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1589 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1594 /* Marshall data and send request */
1596 init_q_query_trusted_domain_info_by_sid(&q, pol, info_class, dom_sid);
1598 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFOBYSID,
1601 lsa_io_q_query_trusted_domain_info_by_sid,
1602 lsa_io_r_query_trusted_domain_info,
1603 NT_STATUS_UNSUCCESSFUL);
1607 if (!NT_STATUS_IS_OK(result)) {
1618 NTSTATUS rpccli_lsa_query_trusted_domain_info_by_name(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1620 uint16 info_class, const char *domain_name,
1621 LSA_TRUSTED_DOMAIN_INFO **info)
1623 prs_struct qbuf, rbuf;
1624 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO_BY_NAME q;
1625 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1626 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1631 /* Marshall data and send request */
1633 init_q_query_trusted_domain_info_by_name(&q, pol, info_class, domain_name);
1635 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFOBYNAME,
1638 lsa_io_q_query_trusted_domain_info_by_name,
1639 lsa_io_r_query_trusted_domain_info,
1640 NT_STATUS_UNSUCCESSFUL);
1644 if (!NT_STATUS_IS_OK(result)) {
1655 NTSTATUS cli_lsa_query_domain_info_policy(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1657 uint16 info_class, LSA_DOM_INFO_UNION **info)
1659 prs_struct qbuf, rbuf;
1660 LSA_Q_QUERY_DOM_INFO_POLICY q;
1661 LSA_R_QUERY_DOM_INFO_POLICY r;
1662 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1667 /* Marshall data and send request */
1669 init_q_query_dom_info(&q, pol, info_class);
1671 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYDOMINFOPOL,
1674 lsa_io_q_query_dom_info,
1675 lsa_io_r_query_dom_info,
1676 NT_STATUS_UNSUCCESSFUL);
1680 if (!NT_STATUS_IS_OK(result)) {