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/>.
26 #include "../librpc/gen_ndr/cli_lsa.h"
27 #include "rpc_client/cli_lsarpc.h"
28 #include "rpc_client/init_lsa.h"
29 #include "../libcli/security/security.h"
31 /** @defgroup lsa LSA - Local Security Architecture
40 * RPC client routines for the LSA RPC pipe. LSA means "local
41 * security authority", which is half of a password database.
44 /** Open a LSA policy handle
46 * @param cli Handle on an initialised SMB connection */
48 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
50 bool sec_qos, uint32 des_access,
51 struct policy_handle *pol)
53 struct lsa_ObjectAttribute attr;
54 struct lsa_QosInfo qos;
55 uint16_t system_name = '\\';
63 qos.impersonation_level = 2;
65 qos.effective_only = 0;
70 return rpccli_lsa_OpenPolicy(cli, mem_ctx,
77 /** Open a LSA policy handle
79 * @param cli Handle on an initialised SMB connection
82 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
83 TALLOC_CTX *mem_ctx, bool sec_qos,
84 uint32 des_access, struct policy_handle *pol)
86 struct lsa_ObjectAttribute attr;
87 struct lsa_QosInfo qos;
95 qos.impersonation_level = 2;
97 qos.effective_only = 0;
102 return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
109 /* Lookup a list of sids
111 * internal version withOUT memory allocation of the target arrays.
112 * this assumes suffciently sized arrays to store domains, names and types. */
114 static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
116 struct policy_handle *pol,
118 const struct dom_sid *sids,
121 enum lsa_SidType *types,
122 bool use_lookupsids3)
124 NTSTATUS result = NT_STATUS_OK;
125 TALLOC_CTX *tmp_ctx = NULL;
127 struct lsa_SidArray sid_array;
128 struct lsa_RefDomainList *ref_domains = NULL;
129 struct lsa_TransNameArray lsa_names;
133 ZERO_STRUCT(lsa_names);
135 tmp_ctx = talloc_new(mem_ctx);
137 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
138 result = NT_STATUS_UNSUCCESSFUL;
142 sid_array.num_sids = num_sids;
143 sid_array.sids = TALLOC_ARRAY(mem_ctx, struct lsa_SidPtr, num_sids);
144 if (!sid_array.sids) {
145 return NT_STATUS_NO_MEMORY;
148 for (i = 0; i<num_sids; i++) {
149 sid_array.sids[i].sid = dom_sid_dup(mem_ctx, &sids[i]);
150 if (!sid_array.sids[i].sid) {
151 return NT_STATUS_NO_MEMORY;
155 if (use_lookupsids3) {
156 struct lsa_TransNameArray2 lsa_names2;
159 ZERO_STRUCT(lsa_names2);
161 result = rpccli_lsa_LookupSids3(cli, mem_ctx,
170 if (!NT_STATUS_IS_ERR(result)) {
171 lsa_names.count = lsa_names2.count;
172 lsa_names.names = talloc_array(mem_ctx, struct lsa_TranslatedName, lsa_names.count);
173 if (!lsa_names.names) {
174 return NT_STATUS_NO_MEMORY;
176 for (n=0; n < lsa_names.count; n++) {
177 lsa_names.names[n].sid_type = lsa_names2.names[n].sid_type;
178 lsa_names.names[n].name = lsa_names2.names[n].name;
179 lsa_names.names[n].sid_index = lsa_names2.names[n].sid_index;
184 result = rpccli_lsa_LookupSids(cli, mem_ctx,
193 DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
194 nt_errstr(result), count));
196 if (!NT_STATUS_IS_OK(result) &&
197 !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
198 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
200 /* An actual error occured */
204 /* Return output parameters */
206 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
209 for (i = 0; i < num_sids; i++) {
212 (types)[i] = SID_NAME_UNKNOWN;
214 result = NT_STATUS_NONE_MAPPED;
218 for (i = 0; i < num_sids; i++) {
219 const char *name, *dom_name;
220 uint32_t dom_idx = lsa_names.names[i].sid_index;
222 /* Translate optimised name through domain index array */
224 if (dom_idx != 0xffffffff) {
226 dom_name = ref_domains->domains[dom_idx].name.string;
227 name = lsa_names.names[i].name.string;
230 (names)[i] = talloc_strdup(names, name);
231 if ((names)[i] == NULL) {
232 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
233 result = NT_STATUS_UNSUCCESSFUL;
239 domains[i] = talloc_strdup(domains,
240 dom_name ? dom_name : "");
241 (types)[i] = lsa_names.names[i].sid_type;
242 if (((domains)[i] == NULL)) {
243 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
244 result = NT_STATUS_UNSUCCESSFUL;
251 (types)[i] = SID_NAME_UNKNOWN;
256 TALLOC_FREE(tmp_ctx);
260 /* Lookup a list of sids
262 * do it the right way: there is a limit (of 20480 for w2k3) entries
263 * returned by this call. when the sids list contains more entries,
264 * empty lists are returned. This version of lsa_lookup_sids passes
265 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
267 /* This constant defines the limit of how many sids to look up
268 * in one call (maximum). the limit from the server side is
269 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
270 #define LOOKUP_SIDS_HUNK_SIZE 1000
272 static NTSTATUS rpccli_lsa_lookup_sids_generic(struct rpc_pipe_client *cli,
274 struct policy_handle *pol,
276 const struct dom_sid *sids,
279 enum lsa_SidType **ptypes,
280 bool use_lookupsids3)
282 NTSTATUS result = NT_STATUS_OK;
284 int sids_processed = 0;
285 const struct dom_sid *hunk_sids = sids;
288 enum lsa_SidType *hunk_types;
289 char **domains = NULL;
291 enum lsa_SidType *types = NULL;
294 if (!(domains = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
295 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
296 result = NT_STATUS_NO_MEMORY;
300 if (!(names = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
301 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
302 result = NT_STATUS_NO_MEMORY;
306 if (!(types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
307 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
308 result = NT_STATUS_NO_MEMORY;
313 sids_left = num_sids;
314 hunk_domains = domains;
318 while (sids_left > 0) {
320 NTSTATUS hunk_result = NT_STATUS_OK;
322 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
323 ? LOOKUP_SIDS_HUNK_SIZE
326 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
329 sids_processed + hunk_num_sids - 1,
332 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
342 if (!NT_STATUS_IS_OK(hunk_result) &&
343 !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
344 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
346 /* An actual error occured */
347 result = hunk_result;
351 /* adapt overall result */
352 if (( NT_STATUS_IS_OK(result) &&
353 !NT_STATUS_IS_OK(hunk_result))
355 ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
356 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
358 result = STATUS_SOME_UNMAPPED;
361 sids_left -= hunk_num_sids;
362 sids_processed += hunk_num_sids; /* only used in DEBUG */
363 hunk_sids += hunk_num_sids;
364 hunk_domains += hunk_num_sids;
365 hunk_names += hunk_num_sids;
366 hunk_types += hunk_num_sids;
375 TALLOC_FREE(domains);
381 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
383 struct policy_handle *pol,
385 const struct dom_sid *sids,
388 enum lsa_SidType **ptypes)
390 return rpccli_lsa_lookup_sids_generic(cli, mem_ctx, pol, num_sids, sids,
391 pdomains, pnames, ptypes, false);
394 NTSTATUS rpccli_lsa_lookup_sids3(struct rpc_pipe_client *cli,
396 struct policy_handle *pol,
398 const struct dom_sid *sids,
401 enum lsa_SidType **ptypes)
403 return rpccli_lsa_lookup_sids_generic(cli, mem_ctx, pol, num_sids, sids,
404 pdomains, pnames, ptypes, true);
407 /** Lookup a list of names */
409 static NTSTATUS rpccli_lsa_lookup_names_generic(struct rpc_pipe_client *cli,
411 struct policy_handle *pol, int num_names,
413 const char ***dom_names,
415 struct dom_sid **sids,
416 enum lsa_SidType **types,
417 bool use_lookupnames4)
421 struct lsa_String *lsa_names = NULL;
422 struct lsa_RefDomainList *domains = NULL;
423 struct lsa_TransSidArray sid_array;
424 struct lsa_TransSidArray3 sid_array3;
427 ZERO_STRUCT(sid_array);
428 ZERO_STRUCT(sid_array3);
430 lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
432 return NT_STATUS_NO_MEMORY;
435 for (i=0; i<num_names; i++) {
436 init_lsa_String(&lsa_names[i], names[i]);
439 if (use_lookupnames4) {
440 result = rpccli_lsa_LookupNames4(cli, mem_ctx,
450 result = rpccli_lsa_LookupNames(cli, mem_ctx,
460 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
461 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
463 /* An actual error occured */
468 /* Return output parameters */
471 result = NT_STATUS_NONE_MAPPED;
476 if (!((*sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, num_names)))) {
477 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
478 result = NT_STATUS_NO_MEMORY;
482 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
483 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
484 result = NT_STATUS_NO_MEMORY;
488 if (dom_names != NULL) {
489 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
490 if (*dom_names == NULL) {
491 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
492 result = NT_STATUS_NO_MEMORY;
499 if (dom_names != NULL) {
504 for (i = 0; i < num_names; i++) {
506 struct dom_sid *sid = &(*sids)[i];
508 if (use_lookupnames4) {
509 dom_idx = sid_array3.sids[i].sid_index;
510 (*types)[i] = sid_array3.sids[i].sid_type;
512 dom_idx = sid_array.sids[i].sid_index;
513 (*types)[i] = sid_array.sids[i].sid_type;
516 /* Translate optimised sid through domain index array */
518 if (dom_idx == 0xffffffff) {
519 /* Nothing to do, this is unknown */
521 (*types)[i] = SID_NAME_UNKNOWN;
525 if (use_lookupnames4) {
526 sid_copy(sid, sid_array3.sids[i].sid);
528 sid_copy(sid, domains->domains[dom_idx].sid);
530 if (sid_array.sids[i].rid != 0xffffffff) {
531 sid_append_rid(sid, sid_array.sids[i].rid);
535 if (dom_names == NULL) {
539 (*dom_names)[i] = domains->domains[dom_idx].name.string;
547 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
549 struct policy_handle *pol, int num_names,
551 const char ***dom_names,
553 struct dom_sid **sids,
554 enum lsa_SidType **types)
556 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
557 names, dom_names, level, sids,
561 NTSTATUS rpccli_lsa_lookup_names4(struct rpc_pipe_client *cli,
563 struct policy_handle *pol, int num_names,
565 const char ***dom_names,
567 struct dom_sid **sids,
568 enum lsa_SidType **types)
570 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
571 names, dom_names, level, sids,