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,
49 struct lsa_ObjectAttribute attr;
50 struct lsa_QosInfo qos;
51 uint16_t system_name = '\\';
54 init_lsa_sec_qos(&qos, 0xc, 2, 1, 0);
55 init_lsa_obj_attr(&attr,
63 init_lsa_obj_attr(&attr,
72 return rpccli_lsa_OpenPolicy(cli, mem_ctx,
79 /** Open a LSA policy handle
81 * @param cli Handle on an initialised SMB connection
84 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
85 TALLOC_CTX *mem_ctx, bool sec_qos,
86 uint32 des_access, POLICY_HND *pol)
88 struct lsa_ObjectAttribute attr;
89 struct lsa_QosInfo qos;
90 char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
93 init_lsa_sec_qos(&qos, 0xc, 2, 1, 0);
94 init_lsa_obj_attr(&attr,
102 init_lsa_obj_attr(&attr,
111 return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
118 /* Lookup a list of sids
120 * internal version withOUT memory allocation of the target arrays.
121 * this assumes suffciently sized arrays to store domains, names and types. */
123 static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
130 enum lsa_SidType *types)
132 NTSTATUS result = NT_STATUS_OK;
133 TALLOC_CTX *tmp_ctx = NULL;
135 struct lsa_SidArray sid_array;
136 struct lsa_RefDomainList *ref_domains = NULL;
137 struct lsa_TransNameArray lsa_names;
141 ZERO_STRUCT(lsa_names);
143 tmp_ctx = talloc_new(mem_ctx);
145 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
146 result = NT_STATUS_UNSUCCESSFUL;
150 sid_array.num_sids = num_sids;
151 sid_array.sids = TALLOC_ARRAY(mem_ctx, struct lsa_SidPtr, num_sids);
152 if (!sid_array.sids) {
153 return NT_STATUS_NO_MEMORY;
156 for (i = 0; i<num_sids; i++) {
157 sid_array.sids[i].sid = sid_dup_talloc(mem_ctx, &sids[i]);
158 if (!sid_array.sids[i].sid) {
159 return NT_STATUS_NO_MEMORY;
163 result = rpccli_lsa_LookupSids(cli, mem_ctx,
171 DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
172 nt_errstr(result), count));
174 if (!NT_STATUS_IS_OK(result) &&
175 !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
176 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
178 /* An actual error occured */
182 /* Return output parameters */
184 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
187 for (i = 0; i < num_sids; i++) {
190 (types)[i] = SID_NAME_UNKNOWN;
192 result = NT_STATUS_NONE_MAPPED;
196 for (i = 0; i < num_sids; i++) {
197 const char *name, *dom_name;
198 uint32_t dom_idx = lsa_names.names[i].sid_index;
200 /* Translate optimised name through domain index array */
202 if (dom_idx != 0xffffffff) {
204 dom_name = ref_domains->domains[dom_idx].name.string;
205 name = lsa_names.names[i].name.string;
207 (names)[i] = talloc_strdup(mem_ctx, name);
208 (domains)[i] = talloc_strdup(mem_ctx, dom_name);
209 (types)[i] = lsa_names.names[i].sid_type;
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(struct rpc_pipe_client *cli,
248 enum lsa_SidType **ptypes)
250 NTSTATUS result = NT_STATUS_OK;
252 int sids_processed = 0;
253 const DOM_SID *hunk_sids = sids;
256 enum lsa_SidType *hunk_types;
257 char **domains = NULL;
259 enum lsa_SidType *types = NULL;
262 if (!(domains = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
263 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
264 result = NT_STATUS_NO_MEMORY;
268 if (!(names = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
269 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
270 result = NT_STATUS_NO_MEMORY;
274 if (!(types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
275 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
276 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: 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 */
314 result = hunk_result;
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;
342 TALLOC_FREE(domains);
348 /** Lookup a list of names */
350 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
352 POLICY_HND *pol, int num_names,
354 const char ***dom_names,
357 enum lsa_SidType **types)
361 struct lsa_String *lsa_names = NULL;
362 struct lsa_RefDomainList *domains = NULL;
363 struct lsa_TransSidArray sid_array;
366 ZERO_STRUCT(sid_array);
368 lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
370 return NT_STATUS_NO_MEMORY;
373 for (i=0; i<num_names; i++) {
374 init_lsa_String(&lsa_names[i], names[i]);
377 result = rpccli_lsa_LookupNames(cli, mem_ctx,
386 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
387 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
389 /* An actual error occured */
394 /* Return output parameters */
397 result = NT_STATUS_NONE_MAPPED;
402 if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
403 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
404 result = NT_STATUS_NO_MEMORY;
408 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
409 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
410 result = NT_STATUS_NO_MEMORY;
414 if (dom_names != NULL) {
415 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
416 if (*dom_names == NULL) {
417 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
418 result = NT_STATUS_NO_MEMORY;
425 if (dom_names != NULL) {
430 for (i = 0; i < num_names; i++) {
431 uint32_t dom_idx = sid_array.sids[i].sid_index;
432 uint32_t dom_rid = sid_array.sids[i].rid;
433 DOM_SID *sid = &(*sids)[i];
435 /* Translate optimised sid through domain index array */
437 if (dom_idx == 0xffffffff) {
438 /* Nothing to do, this is unknown */
440 (*types)[i] = SID_NAME_UNKNOWN;
444 sid_copy(sid, domains->domains[dom_idx].sid);
446 if (dom_rid != 0xffffffff) {
447 sid_append_rid(sid, dom_rid);
450 (*types)[i] = sid_array.sids[i].sid_type;
452 if (dom_names == NULL) {
456 (*dom_names)[i] = domains->domains[dom_idx].name.string;
466 /** An example of how to use the routines in this file. Fetch a DOMAIN
467 sid. Does complete cli setup / teardown anonymously. */
469 bool fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
471 struct cli_state cli;
477 if(cli_initialise(&cli) == False) {
478 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
482 if(!resolve_name( remote_machine, &cli.dest_ip, 0x20)) {
483 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
487 if (!cli_connect(&cli, remote_machine, &cli.dest_ip)) {
488 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
489 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
493 if (!attempt_netbios_session_request(&cli, global_myname(), remote_machine, &cli.dest_ip)) {
494 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n",
499 cli.protocol = PROTOCOL_NT1;
501 if (!cli_negprot(&cli)) {
502 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
503 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
507 if (cli.protocol != PROTOCOL_NT1) {
508 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
514 * Do an anonymous session setup.
517 if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
518 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
519 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
523 if (!(cli.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
524 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
529 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
530 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
531 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
535 /* Fetch domain sid */
537 if (!cli_nt_session_open(&cli, PI_LSARPC)) {
538 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
542 result = cli_lsa_open_policy(&cli, cli.mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
543 if (!NT_STATUS_IS_OK(result)) {
544 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
545 nt_errstr(result) ));
549 result = cli_lsa_query_info_policy(&cli, cli.mem_ctx, &lsa_pol, 5, domain, psid);
550 if (!NT_STATUS_IS_OK(result)) {
551 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
552 nt_errstr(result) ));