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.
8 Copyright (C) Michael Adam 2007.
9 Copyright (C) Guenther Deschner 2008.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** @defgroup lsa LSA - Local Security Architecture
36 * RPC client routines for the LSA RPC pipe. LSA means "local
37 * security authority", which is half of a password database.
40 /** Open a LSA policy handle
42 * @param cli Handle on an initialised SMB connection */
44 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
46 bool sec_qos, uint32 des_access,
47 struct policy_handle *pol)
49 struct lsa_ObjectAttribute attr;
50 struct lsa_QosInfo qos;
51 uint16_t system_name = '\\';
59 qos.impersonation_level = 2;
61 qos.effective_only = 0;
66 return rpccli_lsa_OpenPolicy(cli, mem_ctx,
73 /** Open a LSA policy handle
75 * @param cli Handle on an initialised SMB connection
78 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
79 TALLOC_CTX *mem_ctx, bool sec_qos,
80 uint32 des_access, struct policy_handle *pol)
82 struct lsa_ObjectAttribute attr;
83 struct lsa_QosInfo qos;
91 qos.impersonation_level = 2;
93 qos.effective_only = 0;
98 return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
105 /* Lookup a list of sids
107 * internal version withOUT memory allocation of the target arrays.
108 * this assumes suffciently sized arrays to store domains, names and types. */
110 static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
112 struct policy_handle *pol,
117 enum lsa_SidType *types,
118 bool use_lookupsids3)
120 NTSTATUS result = NT_STATUS_OK;
121 TALLOC_CTX *tmp_ctx = NULL;
123 struct lsa_SidArray sid_array;
124 struct lsa_RefDomainList *ref_domains = NULL;
125 struct lsa_TransNameArray lsa_names;
129 ZERO_STRUCT(lsa_names);
131 tmp_ctx = talloc_new(mem_ctx);
133 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
134 result = NT_STATUS_UNSUCCESSFUL;
138 sid_array.num_sids = num_sids;
139 sid_array.sids = TALLOC_ARRAY(mem_ctx, struct lsa_SidPtr, num_sids);
140 if (!sid_array.sids) {
141 return NT_STATUS_NO_MEMORY;
144 for (i = 0; i<num_sids; i++) {
145 sid_array.sids[i].sid = sid_dup_talloc(mem_ctx, &sids[i]);
146 if (!sid_array.sids[i].sid) {
147 return NT_STATUS_NO_MEMORY;
151 if (use_lookupsids3) {
152 struct lsa_TransNameArray2 lsa_names2;
155 ZERO_STRUCT(lsa_names2);
157 result = rpccli_lsa_LookupSids3(cli, mem_ctx,
166 if (!NT_STATUS_IS_ERR(result)) {
167 lsa_names.count = lsa_names2.count;
168 lsa_names.names = talloc_array(mem_ctx, struct lsa_TranslatedName, lsa_names.count);
169 if (!lsa_names.names) {
170 return NT_STATUS_NO_MEMORY;
172 for (n=0; n < lsa_names.count; n++) {
173 lsa_names.names[n].sid_type = lsa_names2.names[n].sid_type;
174 lsa_names.names[n].name = lsa_names2.names[n].name;
175 lsa_names.names[n].sid_index = lsa_names2.names[n].sid_index;
180 result = rpccli_lsa_LookupSids(cli, mem_ctx,
189 DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
190 nt_errstr(result), count));
192 if (!NT_STATUS_IS_OK(result) &&
193 !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
194 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
196 /* An actual error occured */
200 /* Return output parameters */
202 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
205 for (i = 0; i < num_sids; i++) {
208 (types)[i] = SID_NAME_UNKNOWN;
210 result = NT_STATUS_NONE_MAPPED;
214 for (i = 0; i < num_sids; i++) {
215 const char *name, *dom_name;
216 uint32_t dom_idx = lsa_names.names[i].sid_index;
218 /* Translate optimised name through domain index array */
220 if (dom_idx != 0xffffffff) {
222 dom_name = ref_domains->domains[dom_idx].name.string;
223 name = lsa_names.names[i].name.string;
226 (names)[i] = talloc_strdup(mem_ctx, name);
227 if ((names)[i] == NULL) {
228 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
229 result = NT_STATUS_UNSUCCESSFUL;
235 (domains)[i] = talloc_strdup(mem_ctx, dom_name);
236 (types)[i] = lsa_names.names[i].sid_type;
237 if (((domains)[i] == NULL)) {
238 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
239 result = NT_STATUS_UNSUCCESSFUL;
246 (types)[i] = SID_NAME_UNKNOWN;
251 TALLOC_FREE(tmp_ctx);
255 /* Lookup a list of sids
257 * do it the right way: there is a limit (of 20480 for w2k3) entries
258 * returned by this call. when the sids list contains more entries,
259 * empty lists are returned. This version of lsa_lookup_sids passes
260 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
262 /* This constant defines the limit of how many sids to look up
263 * in one call (maximum). the limit from the server side is
264 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
265 #define LOOKUP_SIDS_HUNK_SIZE 1000
267 static NTSTATUS rpccli_lsa_lookup_sids_generic(struct rpc_pipe_client *cli,
269 struct policy_handle *pol,
274 enum lsa_SidType **ptypes,
275 bool use_lookupsids3)
277 NTSTATUS result = NT_STATUS_OK;
279 int sids_processed = 0;
280 const DOM_SID *hunk_sids = sids;
283 enum lsa_SidType *hunk_types;
284 char **domains = NULL;
286 enum lsa_SidType *types = NULL;
289 if (!(domains = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
290 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
291 result = NT_STATUS_NO_MEMORY;
295 if (!(names = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
296 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
297 result = NT_STATUS_NO_MEMORY;
301 if (!(types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
302 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
303 result = NT_STATUS_NO_MEMORY;
308 sids_left = num_sids;
309 hunk_domains = domains;
313 while (sids_left > 0) {
315 NTSTATUS hunk_result = NT_STATUS_OK;
317 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
318 ? LOOKUP_SIDS_HUNK_SIZE
321 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
324 sids_processed + hunk_num_sids - 1,
327 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
337 if (!NT_STATUS_IS_OK(hunk_result) &&
338 !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
339 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
341 /* An actual error occured */
342 result = hunk_result;
346 /* adapt overall result */
347 if (( NT_STATUS_IS_OK(result) &&
348 !NT_STATUS_IS_OK(hunk_result))
350 ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
351 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
353 result = STATUS_SOME_UNMAPPED;
356 sids_left -= hunk_num_sids;
357 sids_processed += hunk_num_sids; /* only used in DEBUG */
358 hunk_sids += hunk_num_sids;
359 hunk_domains += hunk_num_sids;
360 hunk_names += hunk_num_sids;
361 hunk_types += hunk_num_sids;
370 TALLOC_FREE(domains);
376 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
378 struct policy_handle *pol,
383 enum lsa_SidType **ptypes)
385 return rpccli_lsa_lookup_sids_generic(cli, mem_ctx, pol, num_sids, sids,
386 pdomains, pnames, ptypes, false);
389 NTSTATUS rpccli_lsa_lookup_sids3(struct rpc_pipe_client *cli,
391 struct policy_handle *pol,
396 enum lsa_SidType **ptypes)
398 return rpccli_lsa_lookup_sids_generic(cli, mem_ctx, pol, num_sids, sids,
399 pdomains, pnames, ptypes, true);
402 /** Lookup a list of names */
404 static NTSTATUS rpccli_lsa_lookup_names_generic(struct rpc_pipe_client *cli,
406 struct policy_handle *pol, int num_names,
408 const char ***dom_names,
411 enum lsa_SidType **types,
412 bool use_lookupnames4)
416 struct lsa_String *lsa_names = NULL;
417 struct lsa_RefDomainList *domains = NULL;
418 struct lsa_TransSidArray sid_array;
419 struct lsa_TransSidArray3 sid_array3;
422 ZERO_STRUCT(sid_array);
423 ZERO_STRUCT(sid_array3);
425 lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
427 return NT_STATUS_NO_MEMORY;
430 for (i=0; i<num_names; i++) {
431 init_lsa_String(&lsa_names[i], names[i]);
434 if (use_lookupnames4) {
435 result = rpccli_lsa_LookupNames4(cli, mem_ctx,
445 result = rpccli_lsa_LookupNames(cli, mem_ctx,
455 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
456 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
458 /* An actual error occured */
463 /* Return output parameters */
466 result = NT_STATUS_NONE_MAPPED;
471 if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
472 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
473 result = NT_STATUS_NO_MEMORY;
477 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
478 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
479 result = NT_STATUS_NO_MEMORY;
483 if (dom_names != NULL) {
484 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
485 if (*dom_names == NULL) {
486 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
487 result = NT_STATUS_NO_MEMORY;
494 if (dom_names != NULL) {
499 for (i = 0; i < num_names; i++) {
501 DOM_SID *sid = &(*sids)[i];
503 if (use_lookupnames4) {
504 dom_idx = sid_array3.sids[i].sid_index;
505 (*types)[i] = sid_array3.sids[i].sid_type;
507 dom_idx = sid_array.sids[i].sid_index;
508 (*types)[i] = sid_array.sids[i].sid_type;
511 /* Translate optimised sid through domain index array */
513 if (dom_idx == 0xffffffff) {
514 /* Nothing to do, this is unknown */
516 (*types)[i] = SID_NAME_UNKNOWN;
520 if (use_lookupnames4) {
521 sid_copy(sid, sid_array3.sids[i].sid);
523 sid_copy(sid, domains->domains[dom_idx].sid);
525 if (sid_array.sids[i].rid != 0xffffffff) {
526 sid_append_rid(sid, sid_array.sids[i].rid);
530 if (dom_names == NULL) {
534 (*dom_names)[i] = domains->domains[dom_idx].name.string;
542 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
544 struct policy_handle *pol, int num_names,
546 const char ***dom_names,
549 enum lsa_SidType **types)
551 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
552 names, dom_names, level, sids,
556 NTSTATUS rpccli_lsa_lookup_names4(struct rpc_pipe_client *cli,
558 struct policy_handle *pol, int num_names,
560 const char ***dom_names,
563 enum lsa_SidType **types)
565 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
566 names, dom_names, level, sids,