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.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
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 struct lsa_ObjectAttribute attr;
49 struct lsa_QosInfo qos;
50 uint16_t system_name = '\\';
53 init_lsa_sec_qos(&qos, 0xc, 2, 1, 0);
54 init_lsa_obj_attr(&attr,
62 init_lsa_obj_attr(&attr,
71 return rpccli_lsa_OpenPolicy(cli, mem_ctx,
78 /** Open a LSA policy handle
80 * @param cli Handle on an initialised SMB connection
83 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
84 TALLOC_CTX *mem_ctx, bool sec_qos,
85 uint32 des_access, POLICY_HND *pol)
87 struct lsa_ObjectAttribute attr;
88 struct lsa_QosInfo qos;
89 char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
92 init_lsa_sec_qos(&qos, 0xc, 2, 1, 0);
93 init_lsa_obj_attr(&attr,
101 init_lsa_obj_attr(&attr,
110 return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
117 /* Lookup a list of sids
119 * internal version withOUT memory allocation of the target arrays.
120 * this assumes suffciently sized arrays to store domains, names and types. */
122 static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
129 enum lsa_SidType *types)
131 NTSTATUS result = NT_STATUS_OK;
132 TALLOC_CTX *tmp_ctx = NULL;
134 struct lsa_SidArray sid_array;
135 struct lsa_RefDomainList *ref_domains = NULL;
136 struct lsa_TransNameArray lsa_names;
140 ZERO_STRUCT(lsa_names);
142 tmp_ctx = talloc_new(mem_ctx);
144 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
145 result = NT_STATUS_UNSUCCESSFUL;
149 sid_array.num_sids = num_sids;
150 sid_array.sids = TALLOC_ARRAY(mem_ctx, struct lsa_SidPtr, num_sids);
151 if (!sid_array.sids) {
152 return NT_STATUS_NO_MEMORY;
155 for (i = 0; i<num_sids; i++) {
156 sid_array.sids[i].sid = sid_dup_talloc(mem_ctx, &sids[i]);
157 if (!sid_array.sids[i].sid) {
158 return NT_STATUS_NO_MEMORY;
162 result = rpccli_lsa_LookupSids(cli, mem_ctx,
170 DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
171 nt_errstr(result), count));
173 if (!NT_STATUS_IS_OK(result) &&
174 !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
175 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
177 /* An actual error occured */
181 /* Return output parameters */
183 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
186 for (i = 0; i < num_sids; i++) {
189 (types)[i] = SID_NAME_UNKNOWN;
191 result = NT_STATUS_NONE_MAPPED;
195 for (i = 0; i < num_sids; i++) {
196 const char *name, *dom_name;
197 uint32_t dom_idx = lsa_names.names[i].sid_index;
199 /* Translate optimised name through domain index array */
201 if (dom_idx != 0xffffffff) {
203 dom_name = ref_domains->domains[dom_idx].name.string;
204 name = lsa_names.names[i].name.string;
206 (names)[i] = talloc_strdup(mem_ctx, name);
207 (domains)[i] = talloc_strdup(mem_ctx, dom_name);
208 (types)[i] = lsa_names.names[i].sid_type;
210 if (((names)[i] == NULL) || ((domains)[i] == NULL)) {
211 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): 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(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(): 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(): 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(): 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: 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;
339 TALLOC_FREE(*domains);
345 /** Lookup a list of names */
347 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
349 POLICY_HND *pol, int num_names,
351 const char ***dom_names,
354 enum lsa_SidType **types)
358 struct lsa_String *lsa_names = NULL;
359 struct lsa_RefDomainList *domains = NULL;
360 struct lsa_TransSidArray sid_array;
363 ZERO_STRUCT(sid_array);
365 lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
367 return NT_STATUS_NO_MEMORY;
370 for (i=0; i<num_names; i++) {
371 init_lsa_String(&lsa_names[i], names[i]);
374 result = rpccli_lsa_LookupNames(cli, mem_ctx,
383 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
384 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
386 /* An actual error occured */
391 /* Return output parameters */
394 result = NT_STATUS_NONE_MAPPED;
399 if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
400 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
401 result = NT_STATUS_NO_MEMORY;
405 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
406 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
407 result = NT_STATUS_NO_MEMORY;
411 if (dom_names != NULL) {
412 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
413 if (*dom_names == NULL) {
414 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
415 result = NT_STATUS_NO_MEMORY;
422 if (dom_names != NULL) {
427 for (i = 0; i < num_names; i++) {
428 uint32_t dom_idx = sid_array.sids[i].sid_index;
429 uint32_t dom_rid = sid_array.sids[i].rid;
430 DOM_SID *sid = &(*sids)[i];
432 /* Translate optimised sid through domain index array */
434 if (dom_idx == 0xffffffff) {
435 /* Nothing to do, this is unknown */
437 (*types)[i] = SID_NAME_UNKNOWN;
441 sid_copy(sid, domains->domains[dom_idx].sid);
443 if (dom_rid != 0xffffffff) {
444 sid_append_rid(sid, dom_rid);
447 (*types)[i] = sid_array.sids[i].sid_type;
449 if (dom_names == NULL) {
453 (*dom_names)[i] = domains->domains[dom_idx].name.string;
463 /** An example of how to use the routines in this file. Fetch a DOMAIN
464 sid. Does complete cli setup / teardown anonymously. */
466 bool fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
468 struct cli_state cli;
474 if(cli_initialise(&cli) == False) {
475 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
479 if(!resolve_name( remote_machine, &cli.dest_ip, 0x20)) {
480 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
484 if (!cli_connect(&cli, remote_machine, &cli.dest_ip)) {
485 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
486 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
490 if (!attempt_netbios_session_request(&cli, global_myname(), remote_machine, &cli.dest_ip)) {
491 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n",
496 cli.protocol = PROTOCOL_NT1;
498 if (!cli_negprot(&cli)) {
499 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
500 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
504 if (cli.protocol != PROTOCOL_NT1) {
505 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
511 * Do an anonymous session setup.
514 if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
515 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
516 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
520 if (!(cli.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
521 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
526 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
527 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
528 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
532 /* Fetch domain sid */
534 if (!cli_nt_session_open(&cli, PI_LSARPC)) {
535 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
539 result = cli_lsa_open_policy(&cli, cli.mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
540 if (!NT_STATUS_IS_OK(result)) {
541 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
542 nt_errstr(result) ));
546 result = cli_lsa_query_info_policy(&cli, cli.mem_ctx, &lsa_pol, 5, domain, psid);
547 if (!NT_STATUS_IS_OK(result)) {
548 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
549 nt_errstr(result) ));