Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : Winbind daemon connection manager
5 :
6 : Copyright (C) Tim Potter 2001
7 : Copyright (C) Andrew Bartlett 2002
8 : Copyright (C) Gerald (Jerry) Carter 2003-2005.
9 : Copyright (C) Volker Lendecke 2004-2005
10 : Copyright (C) Jeremy Allison 2006
11 :
12 : This program is free software; you can redistribute it and/or modify
13 : it under the terms of the GNU General Public License as published by
14 : the Free Software Foundation; either version 3 of the License, or
15 : (at your option) any later version.
16 :
17 : This program is distributed in the hope that it will be useful,
18 : but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 : GNU General Public License for more details.
21 :
22 : You should have received a copy of the GNU General Public License
23 : along with this program. If not, see <http://www.gnu.org/licenses/>.
24 : */
25 :
26 : /*
27 : We need to manage connections to domain controllers without having to
28 : mess up the main winbindd code with other issues. The aim of the
29 : connection manager is to:
30 :
31 : - make connections to domain controllers and cache them
32 : - re-establish connections when networks or servers go down
33 : - centralise the policy on connection timeouts, domain controller
34 : selection etc
35 : - manage re-entrancy for when winbindd becomes able to handle
36 : multiple outstanding rpc requests
37 :
38 : Why not have connection management as part of the rpc layer like tng?
39 : Good question. This code may morph into libsmb/rpc_cache.c or something
40 : like that but at the moment it's simply staying as part of winbind. I
41 : think the TNG architecture of forcing every user of the rpc layer to use
42 : the connection caching system is a bad idea. It should be an optional
43 : method of using the routines.
44 :
45 : The TNG design is quite good but I disagree with some aspects of the
46 : implementation. -tpot
47 :
48 : */
49 :
50 : /*
51 : TODO:
52 :
53 : - I'm pretty annoyed by all the make_nmb_name() stuff. It should be
54 : moved down into another function.
55 :
56 : - Take care when destroying cli_structs as they can be shared between
57 : various sam handles.
58 :
59 : */
60 :
61 : #include "includes.h"
62 : #include "winbindd.h"
63 : #include "libsmb/namequery.h"
64 : #include "../libcli/auth/libcli_auth.h"
65 : #include "../librpc/gen_ndr/ndr_netlogon_c.h"
66 : #include "rpc_client/cli_pipe.h"
67 : #include "rpc_client/cli_netlogon.h"
68 : #include "../librpc/gen_ndr/ndr_samr_c.h"
69 : #include "../librpc/gen_ndr/ndr_lsa_c.h"
70 : #include "rpc_client/cli_lsarpc.h"
71 : #include "../librpc/gen_ndr/ndr_dssetup_c.h"
72 : #include "libads/sitename_cache.h"
73 : #include "libsmb/libsmb.h"
74 : #include "libsmb/clidgram.h"
75 : #include "ads.h"
76 : #include "secrets.h"
77 : #include "../libcli/security/security.h"
78 : #include "passdb.h"
79 : #include "messages.h"
80 : #include "auth/gensec/gensec.h"
81 : #include "../libcli/smb/smbXcli_base.h"
82 : #include "libcli/auth/netlogon_creds_cli.h"
83 : #include "auth.h"
84 : #include "rpc_server/rpc_ncacn_np.h"
85 : #include "auth/credentials/credentials.h"
86 : #include "lib/param/param.h"
87 : #include "lib/gencache.h"
88 : #include "lib/util/string_wrappers.h"
89 : #include "lib/global_contexts.h"
90 : #include "librpc/gen_ndr/ndr_winbind_c.h"
91 :
92 : #undef DBGC_CLASS
93 : #define DBGC_CLASS DBGC_WINBIND
94 :
95 : struct dc_name_ip {
96 : fstring name;
97 : struct sockaddr_storage ss;
98 : };
99 :
100 : extern struct winbindd_methods reconnect_methods;
101 :
102 : static NTSTATUS init_dc_connection_network(struct winbindd_domain *domain, bool need_rw_dc);
103 : static void set_dc_type_and_flags( struct winbindd_domain *domain );
104 : static bool set_dc_type_and_flags_trustinfo( struct winbindd_domain *domain );
105 : static bool get_dcs(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
106 : struct dc_name_ip **dcs, int *num_dcs,
107 : uint32_t request_flags);
108 :
109 1490 : void winbind_msg_domain_offline(struct messaging_context *msg_ctx,
110 : void *private_data,
111 : uint32_t msg_type,
112 : struct server_id server_id,
113 : DATA_BLOB *data)
114 : {
115 1490 : const char *domain_name = (const char *)data->data;
116 : struct winbindd_domain *domain;
117 :
118 1490 : domain = find_domain_from_name_noinit(domain_name);
119 1490 : if (domain == NULL) {
120 0 : DBG_DEBUG("Domain %s not found!\n", domain_name);
121 0 : return;
122 : }
123 :
124 1490 : DBG_DEBUG("Domain %s was %s, change to offline now.\n",
125 : domain_name,
126 : domain->online ? "online" : "offline");
127 :
128 1490 : domain->online = false;
129 : }
130 :
131 66 : void winbind_msg_domain_online(struct messaging_context *msg_ctx,
132 : void *private_data,
133 : uint32_t msg_type,
134 : struct server_id server_id,
135 : DATA_BLOB *data)
136 : {
137 66 : const char *domain_name = (const char *)data->data;
138 : struct winbindd_domain *domain;
139 :
140 66 : domain = find_domain_from_name_noinit(domain_name);
141 66 : if (domain == NULL) {
142 0 : return;
143 : }
144 :
145 66 : SMB_ASSERT(wb_child_domain() == NULL);
146 :
147 66 : DBG_DEBUG("Domain %s was %s, marking as online now!\n",
148 : domain_name,
149 : domain->online ? "online" : "offline");
150 :
151 66 : domain->online = true;
152 : }
153 :
154 : /****************************************************************
155 : Set domain offline and also add handler to put us back online
156 : if we detect a DC.
157 : ****************************************************************/
158 :
159 0 : void set_domain_offline(struct winbindd_domain *domain)
160 : {
161 0 : pid_t parent_pid = getppid();
162 :
163 0 : DEBUG(10,("set_domain_offline: called for domain %s\n",
164 : domain->name ));
165 :
166 0 : if (domain->internal) {
167 0 : DEBUG(3,("set_domain_offline: domain %s is internal - logic error.\n",
168 : domain->name ));
169 0 : return;
170 : }
171 :
172 0 : domain->online = False;
173 :
174 : /* Offline domains are always initialized. They're
175 : re-initialized when they go back online. */
176 :
177 0 : domain->initialized = True;
178 :
179 : /* Send a message to the parent that the domain is offline. */
180 0 : if (parent_pid > 1 && !domain->internal) {
181 0 : messaging_send_buf(global_messaging_context(),
182 : pid_to_procid(parent_pid),
183 : MSG_WINBIND_DOMAIN_OFFLINE,
184 0 : (uint8_t *)domain->name,
185 0 : strlen(domain->name) + 1);
186 : }
187 :
188 : /* Send an offline message to the idmap child when our
189 : primary domain goes offline */
190 0 : if ( domain->primary ) {
191 0 : pid_t idmap_pid = idmap_child_pid();
192 :
193 0 : if (idmap_pid != 0) {
194 0 : messaging_send_buf(global_messaging_context(),
195 : pid_to_procid(idmap_pid),
196 : MSG_WINBIND_OFFLINE,
197 0 : (const uint8_t *)domain->name,
198 0 : strlen(domain->name)+1);
199 : }
200 : }
201 :
202 0 : return;
203 : }
204 :
205 : /****************************************************************
206 : Set domain online - if allowed.
207 : ****************************************************************/
208 :
209 4 : static void set_domain_online(struct winbindd_domain *domain)
210 : {
211 4 : pid_t parent_pid = getppid();
212 :
213 4 : DEBUG(10,("set_domain_online: called for domain %s\n",
214 : domain->name ));
215 :
216 4 : if (domain->internal) {
217 0 : DEBUG(3,("set_domain_online: domain %s is internal - logic error.\n",
218 : domain->name ));
219 0 : return;
220 : }
221 :
222 4 : if (get_global_winbindd_state_offline()) {
223 0 : DEBUG(10,("set_domain_online: domain %s remaining globally offline\n",
224 : domain->name ));
225 0 : return;
226 : }
227 :
228 4 : winbindd_set_locator_kdc_envs(domain);
229 :
230 : /* If we are waiting to get a krb5 ticket, trigger immediately. */
231 4 : ccache_regain_all_now();
232 :
233 : /* Ok, we're out of any startup mode now... */
234 4 : domain->startup = False;
235 :
236 4 : if (domain->online == False) {
237 : /* We were offline - now we're online. We default to
238 : using the MS-RPC backend if we started offline,
239 : and if we're going online for the first time we
240 : should really re-initialize the backends and the
241 : checks to see if we're talking to an AD or NT domain.
242 : */
243 :
244 2 : domain->initialized = False;
245 :
246 : /* 'reconnect_methods' is the MS-RPC backend. */
247 2 : if (domain->backend == &reconnect_methods) {
248 0 : domain->backend = NULL;
249 : }
250 : }
251 :
252 4 : domain->online = True;
253 :
254 : /* Send a message to the parent that the domain is online. */
255 4 : if (parent_pid > 1 && !domain->internal) {
256 6 : messaging_send_buf(global_messaging_context(),
257 : pid_to_procid(parent_pid),
258 : MSG_WINBIND_DOMAIN_ONLINE,
259 4 : (uint8_t *)domain->name,
260 4 : strlen(domain->name) + 1);
261 : }
262 :
263 : /* Send an online message to the idmap child when our
264 : primary domain comes online */
265 :
266 4 : if ( domain->primary ) {
267 2 : pid_t idmap_pid = idmap_child_pid();
268 :
269 2 : if (idmap_pid != 0) {
270 3 : messaging_send_buf(global_messaging_context(),
271 : pid_to_procid(idmap_pid),
272 : MSG_WINBIND_ONLINE,
273 2 : (const uint8_t *)domain->name,
274 2 : strlen(domain->name)+1);
275 : }
276 : }
277 :
278 4 : return;
279 : }
280 :
281 : /****************************************************************
282 : Requested to set a domain online.
283 : ****************************************************************/
284 :
285 0 : void set_domain_online_request(struct winbindd_domain *domain)
286 : {
287 : NTSTATUS status;
288 :
289 0 : SMB_ASSERT(wb_child_domain() || idmap_child());
290 :
291 0 : DEBUG(10,("set_domain_online_request: called for domain %s\n",
292 : domain->name ));
293 :
294 0 : if (get_global_winbindd_state_offline()) {
295 0 : DEBUG(10,("set_domain_online_request: domain %s remaining globally offline\n",
296 : domain->name ));
297 0 : return;
298 : }
299 :
300 0 : if (domain->internal) {
301 0 : DEBUG(10, ("set_domain_online_request: Internal domains are "
302 : "always online\n"));
303 0 : return;
304 : }
305 :
306 : /*
307 : * This call takes care of setting the online flag to true if we
308 : * connected, or tell the parent to ping us back if false. Bypasses
309 : * online check so always does network calls.
310 : */
311 0 : status = init_dc_connection_network(domain, true);
312 0 : DBG_DEBUG("init_dc_connection_network(), returned %s, called for "
313 : "domain %s (online = %s)\n",
314 : nt_errstr(status),
315 : domain->name,
316 : domain->online ? "true" : "false");
317 : }
318 :
319 : /****************************************************************
320 : Add -ve connection cache entries for domain and realm.
321 : ****************************************************************/
322 :
323 0 : static void winbind_add_failed_connection_entry(
324 : const struct winbindd_domain *domain,
325 : const char *server,
326 : NTSTATUS result)
327 : {
328 0 : add_failed_connection_entry(domain->name, server, result);
329 : /* If this was the saf name for the last thing we talked to,
330 : remove it. */
331 0 : saf_delete(domain->name);
332 0 : if (domain->alt_name != NULL) {
333 0 : add_failed_connection_entry(domain->alt_name, server, result);
334 0 : saf_delete(domain->alt_name);
335 : }
336 0 : winbindd_unset_locator_kdc_env(domain);
337 0 : }
338 :
339 : /* Choose between anonymous or authenticated connections. We need to use
340 : an authenticated connection if DCs have the RestrictAnonymous registry
341 : entry set > 0, or the "Additional restrictions for anonymous
342 : connections" set in the win2k Local Security Policy.
343 :
344 : Caller to free() result in domain, username, password
345 : */
346 :
347 0 : static void cm_get_ipc_userpass(char **username, char **domain, char **password)
348 : {
349 0 : *username = (char *)secrets_fetch(SECRETS_AUTH_USER, NULL);
350 0 : *domain = (char *)secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
351 0 : *password = (char *)secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
352 :
353 0 : if (*username && **username) {
354 :
355 0 : if (!*domain || !**domain)
356 0 : *domain = smb_xstrdup(lp_workgroup());
357 :
358 0 : if (!*password || !**password)
359 0 : *password = smb_xstrdup("");
360 :
361 0 : DEBUG(3, ("cm_get_ipc_userpass: Retrieved auth-user from secrets.tdb [%s\\%s]\n",
362 : *domain, *username));
363 :
364 : } else {
365 0 : DEBUG(3, ("cm_get_ipc_userpass: No auth-user defined\n"));
366 0 : *username = smb_xstrdup("");
367 0 : *domain = smb_xstrdup("");
368 0 : *password = smb_xstrdup("");
369 : }
370 0 : }
371 :
372 0 : static NTSTATUS cm_get_ipc_credentials(TALLOC_CTX *mem_ctx,
373 : struct cli_credentials **_creds)
374 : {
375 :
376 0 : TALLOC_CTX *frame = talloc_stackframe();
377 0 : NTSTATUS status = NT_STATUS_INTERNAL_ERROR;
378 : struct loadparm_context *lp_ctx;
379 0 : char *username = NULL;
380 0 : char *netbios_domain = NULL;
381 0 : char *password = NULL;
382 0 : struct cli_credentials *creds = NULL;
383 : bool ok;
384 :
385 0 : cm_get_ipc_userpass(&username, &netbios_domain, &password);
386 :
387 0 : lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
388 0 : if (lp_ctx == NULL) {
389 0 : DEBUG(1, ("loadparm_init_s3 failed\n"));
390 0 : status = NT_STATUS_INTERNAL_ERROR;
391 0 : goto fail;
392 : }
393 :
394 0 : creds = cli_credentials_init(mem_ctx);
395 0 : if (creds == NULL) {
396 0 : status = NT_STATUS_NO_MEMORY;
397 0 : goto fail;
398 : }
399 :
400 0 : ok = cli_credentials_set_conf(creds, lp_ctx);
401 0 : if (!ok) {
402 0 : status = NT_STATUS_INTERNAL_ERROR;
403 0 : goto fail;
404 : }
405 :
406 0 : cli_credentials_set_kerberos_state(creds,
407 : CRED_USE_KERBEROS_DISABLED,
408 : CRED_SPECIFIED);
409 :
410 0 : ok = cli_credentials_set_domain(creds, netbios_domain, CRED_SPECIFIED);
411 0 : if (!ok) {
412 0 : status = NT_STATUS_NO_MEMORY;
413 0 : goto fail;
414 : }
415 :
416 0 : ok = cli_credentials_set_username(creds, username, CRED_SPECIFIED);
417 0 : if (!ok) {
418 0 : status = NT_STATUS_NO_MEMORY;
419 0 : goto fail;
420 : }
421 :
422 0 : ok = cli_credentials_set_password(creds, password, CRED_SPECIFIED);
423 0 : if (!ok) {
424 0 : status = NT_STATUS_NO_MEMORY;
425 0 : goto fail;
426 : }
427 :
428 0 : *_creds = creds;
429 0 : creds = NULL;
430 0 : status = NT_STATUS_OK;
431 0 : fail:
432 0 : TALLOC_FREE(creds);
433 0 : SAFE_FREE(username);
434 0 : SAFE_FREE(netbios_domain);
435 0 : SAFE_FREE(password);
436 0 : TALLOC_FREE(frame);
437 0 : return status;
438 : }
439 :
440 0 : static bool cm_is_ipc_credentials(struct cli_credentials *creds)
441 : {
442 0 : TALLOC_CTX *frame = talloc_stackframe();
443 0 : char *ipc_account = NULL;
444 0 : char *ipc_domain = NULL;
445 0 : char *ipc_password = NULL;
446 0 : const char *creds_account = NULL;
447 0 : const char *creds_domain = NULL;
448 0 : const char *creds_password = NULL;
449 0 : bool ret = false;
450 :
451 0 : cm_get_ipc_userpass(&ipc_account, &ipc_domain, &ipc_password);
452 :
453 0 : creds_account = cli_credentials_get_username(creds);
454 0 : creds_domain = cli_credentials_get_domain(creds);
455 0 : creds_password = cli_credentials_get_password(creds);
456 :
457 0 : if (!strequal(ipc_domain, creds_domain)) {
458 0 : goto done;
459 : }
460 :
461 0 : if (!strequal(ipc_account, creds_account)) {
462 0 : goto done;
463 : }
464 :
465 0 : if (!strcsequal(ipc_password, creds_password)) {
466 0 : goto done;
467 : }
468 :
469 0 : ret = true;
470 0 : done:
471 0 : SAFE_FREE(ipc_account);
472 0 : SAFE_FREE(ipc_domain);
473 0 : SAFE_FREE(ipc_password);
474 0 : TALLOC_FREE(frame);
475 0 : return ret;
476 : }
477 :
478 2 : static bool get_dc_name_via_netlogon(struct winbindd_domain *domain,
479 : fstring dcname,
480 : struct sockaddr_storage *dc_ss,
481 : uint32_t request_flags)
482 : {
483 2 : struct winbindd_domain *our_domain = NULL;
484 2 : struct rpc_pipe_client *netlogon_pipe = NULL;
485 : NTSTATUS result;
486 : WERROR werr;
487 : TALLOC_CTX *mem_ctx;
488 : unsigned int orig_timeout;
489 2 : const char *tmp = NULL;
490 : const char *p;
491 : struct dcerpc_binding_handle *b;
492 :
493 : /* Hmmmm. We can only open one connection to the NETLOGON pipe at the
494 : * moment.... */
495 :
496 2 : if (IS_DC) {
497 0 : return False;
498 : }
499 :
500 2 : if (domain->primary) {
501 0 : return False;
502 : }
503 :
504 2 : our_domain = find_our_domain();
505 :
506 2 : if ((mem_ctx = talloc_init("get_dc_name_via_netlogon")) == NULL) {
507 0 : return False;
508 : }
509 :
510 2 : result = cm_connect_netlogon(our_domain, &netlogon_pipe);
511 2 : if (!NT_STATUS_IS_OK(result)) {
512 0 : talloc_destroy(mem_ctx);
513 0 : return False;
514 : }
515 :
516 2 : b = netlogon_pipe->binding_handle;
517 :
518 : /* This call can take a long time - allow the server to time out.
519 : 35 seconds should do it. */
520 :
521 2 : orig_timeout = rpccli_set_timeout(netlogon_pipe, 35000);
522 :
523 2 : if (our_domain->active_directory) {
524 2 : struct netr_DsRGetDCNameInfo *domain_info = NULL;
525 :
526 : /*
527 : * TODO request flags are not respected in the server
528 : * (and in some cases, like REQUIRE_PDC, causes an error)
529 : */
530 3 : result = dcerpc_netr_DsRGetDCName(b,
531 : mem_ctx,
532 2 : our_domain->dcname,
533 2 : domain->name,
534 : NULL,
535 : NULL,
536 : request_flags|DS_RETURN_DNS_NAME,
537 : &domain_info,
538 : &werr);
539 2 : if (NT_STATUS_IS_OK(result) && W_ERROR_IS_OK(werr)) {
540 3 : tmp = talloc_strdup(
541 2 : mem_ctx, domain_info->dc_unc);
542 2 : if (tmp == NULL) {
543 0 : DEBUG(0, ("talloc_strdup failed\n"));
544 0 : talloc_destroy(mem_ctx);
545 2 : return false;
546 : }
547 2 : if (domain->alt_name == NULL) {
548 0 : domain->alt_name = talloc_strdup(domain,
549 0 : domain_info->domain_name);
550 0 : if (domain->alt_name == NULL) {
551 0 : DEBUG(0, ("talloc_strdup failed\n"));
552 0 : talloc_destroy(mem_ctx);
553 0 : return false;
554 : }
555 : }
556 2 : if (domain->forest_name == NULL) {
557 3 : domain->forest_name = talloc_strdup(domain,
558 2 : domain_info->forest_name);
559 2 : if (domain->forest_name == NULL) {
560 2 : DEBUG(0, ("talloc_strdup failed\n"));
561 2 : talloc_destroy(mem_ctx);
562 2 : return false;
563 : }
564 : }
565 : }
566 : } else {
567 0 : result = dcerpc_netr_GetAnyDCName(b, mem_ctx,
568 0 : our_domain->dcname,
569 0 : domain->name,
570 : &tmp,
571 : &werr);
572 : }
573 :
574 : /* And restore our original timeout. */
575 0 : rpccli_set_timeout(netlogon_pipe, orig_timeout);
576 :
577 0 : if (!NT_STATUS_IS_OK(result)) {
578 0 : DEBUG(10,("dcerpc_netr_GetAnyDCName failed: %s\n",
579 : nt_errstr(result)));
580 0 : talloc_destroy(mem_ctx);
581 0 : return false;
582 : }
583 :
584 0 : if (!W_ERROR_IS_OK(werr)) {
585 0 : DEBUG(10,("dcerpc_netr_GetAnyDCName failed: %s\n",
586 : win_errstr(werr)));
587 0 : talloc_destroy(mem_ctx);
588 0 : return false;
589 : }
590 :
591 : /* dcerpc_netr_GetAnyDCName gives us a name with \\ */
592 0 : p = strip_hostname(tmp);
593 :
594 0 : fstrcpy(dcname, p);
595 :
596 0 : talloc_destroy(mem_ctx);
597 :
598 0 : DEBUG(10,("dcerpc_netr_GetAnyDCName returned %s\n", dcname));
599 :
600 0 : if (!resolve_name(dcname, dc_ss, 0x20, true)) {
601 0 : return False;
602 : }
603 :
604 0 : return True;
605 : }
606 :
607 : /**
608 : * Helper function to assemble trust password and account name
609 : */
610 6 : static NTSTATUS get_trust_credentials(struct winbindd_domain *domain,
611 : TALLOC_CTX *mem_ctx,
612 : bool netlogon,
613 : struct cli_credentials **_creds)
614 : {
615 6 : const struct winbindd_domain *creds_domain = NULL;
616 : struct cli_credentials *creds;
617 : NTSTATUS status;
618 6 : bool force_machine_account = false;
619 :
620 : /* If we are a DC and this is not our own domain */
621 :
622 6 : if (!domain->active_directory) {
623 2 : if (!netlogon) {
624 : /*
625 : * For non active directory domains
626 : * we can only use NTLMSSP for SMB.
627 : *
628 : * But the trust account is not allowed
629 : * to use SMB with NTLMSSP.
630 : */
631 2 : force_machine_account = true;
632 : }
633 : }
634 :
635 6 : if (IS_DC && !force_machine_account) {
636 0 : creds_domain = domain;
637 : } else {
638 6 : creds_domain = find_our_domain();
639 6 : if (creds_domain == NULL) {
640 0 : return NT_STATUS_INVALID_SERVER_STATE;
641 : }
642 : }
643 :
644 6 : status = pdb_get_trust_credentials(creds_domain->name,
645 6 : creds_domain->alt_name,
646 : mem_ctx,
647 : &creds);
648 6 : if (!NT_STATUS_IS_OK(status)) {
649 0 : goto ipc_fallback;
650 : }
651 :
652 6 : if (creds_domain != domain) {
653 : /*
654 : * We can only use schannel against a direct trust
655 : */
656 2 : cli_credentials_set_secure_channel_type(creds,
657 : SEC_CHAN_NULL);
658 : }
659 :
660 6 : *_creds = creds;
661 6 : return NT_STATUS_OK;
662 :
663 0 : ipc_fallback:
664 0 : if (netlogon) {
665 0 : return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
666 : }
667 :
668 0 : status = cm_get_ipc_credentials(mem_ctx, &creds);
669 0 : if (!NT_STATUS_IS_OK(status)) {
670 0 : return status;
671 : }
672 :
673 0 : *_creds = creds;
674 0 : return NT_STATUS_OK;
675 : }
676 :
677 : /************************************************************************
678 : Given a fd with a just-connected TCP connection to a DC, open a connection
679 : to the pipe.
680 : ************************************************************************/
681 :
682 4 : static NTSTATUS cm_prepare_connection(struct winbindd_domain *domain,
683 : const int sockfd,
684 : const char *controller,
685 : struct cli_state **cli,
686 : bool *retry)
687 : {
688 4 : bool try_ipc_auth = false;
689 4 : const char *machine_principal = NULL;
690 4 : const char *machine_realm = NULL;
691 4 : const char *machine_account = NULL;
692 4 : const char *machine_domain = NULL;
693 4 : int flags = 0;
694 4 : struct cli_credentials *creds = NULL;
695 :
696 : struct named_mutex *mutex;
697 :
698 4 : NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
699 : NTSTATUS tmp_status;
700 4 : NTSTATUS tcon_status = NT_STATUS_NETWORK_NAME_DELETED;
701 :
702 4 : enum smb_signing_setting smb_sign_client_connections = lp_client_ipc_signing();
703 :
704 4 : if (IS_AD_DC) {
705 0 : if (domain->secure_channel_type == SEC_CHAN_NULL) {
706 : /*
707 : * Make sure we don't even try to
708 : * connect to a foreign domain
709 : * without a direct outbound trust.
710 : */
711 0 : return NT_STATUS_NO_TRUST_LSA_SECRET;
712 : }
713 :
714 : /*
715 : * As AD DC we only use netlogon and lsa
716 : * using schannel over an anonymous transport
717 : * (ncacn_ip_tcp or ncacn_np).
718 : *
719 : * Currently we always establish the SMB connection,
720 : * even if we don't use it, because we later use ncacn_ip_tcp.
721 : *
722 : * As we won't use the SMB connection there's no
723 : * need to try kerberos. And NT4 domains expect
724 : * an anonymous IPC$ connection anyway.
725 : */
726 0 : smb_sign_client_connections = SMB_SIGNING_OFF;
727 : }
728 :
729 4 : if (smb_sign_client_connections == SMB_SIGNING_DEFAULT) {
730 : /*
731 : * If we are connecting to our own AD domain, require
732 : * smb signing to disrupt MITM attacks
733 : */
734 0 : if (domain->primary && lp_security() == SEC_ADS) {
735 0 : smb_sign_client_connections = SMB_SIGNING_REQUIRED;
736 : /*
737 : * If we are in or are an AD domain and connecting to another
738 : * AD domain in our forest
739 : * then require smb signing to disrupt MITM attacks
740 : */
741 0 : } else if ((lp_security() == SEC_ADS)
742 0 : && domain->active_directory
743 0 : && (domain->domain_trust_attribs
744 0 : & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST)) {
745 0 : smb_sign_client_connections = SMB_SIGNING_REQUIRED;
746 : }
747 : }
748 :
749 4 : DEBUG(10,("cm_prepare_connection: connecting to DC %s for domain %s\n",
750 : controller, domain->name ));
751 :
752 4 : *retry = True;
753 :
754 4 : mutex = grab_named_mutex(talloc_tos(), controller,
755 : WINBIND_SERVER_MUTEX_WAIT_TIME);
756 4 : if (mutex == NULL) {
757 0 : close(sockfd);
758 0 : DEBUG(0,("cm_prepare_connection: mutex grab failed for %s\n",
759 : controller));
760 0 : result = NT_STATUS_POSSIBLE_DEADLOCK;
761 0 : goto done;
762 : }
763 :
764 4 : *cli = cli_state_create(NULL, sockfd, controller,
765 : smb_sign_client_connections, flags);
766 4 : if (*cli == NULL) {
767 0 : close(sockfd);
768 0 : DEBUG(1, ("Could not cli_initialize\n"));
769 0 : result = NT_STATUS_NO_MEMORY;
770 0 : goto done;
771 : }
772 :
773 4 : cli_set_timeout(*cli, 10000); /* 10 seconds */
774 :
775 4 : set_socket_options(sockfd, lp_socket_options());
776 :
777 4 : result = smbXcli_negprot((*cli)->conn, (*cli)->timeout,
778 4 : lp_client_ipc_min_protocol(),
779 4 : lp_client_ipc_max_protocol());
780 :
781 4 : if (!NT_STATUS_IS_OK(result)) {
782 0 : DEBUG(1, ("cli_negprot failed: %s\n", nt_errstr(result)));
783 0 : goto done;
784 : }
785 :
786 8 : if (smbXcli_conn_protocol((*cli)->conn) >= PROTOCOL_NT1 &&
787 4 : smb1cli_conn_capabilities((*cli)->conn) & CAP_EXTENDED_SECURITY) {
788 4 : try_ipc_auth = true;
789 0 : } else if (smbXcli_conn_protocol((*cli)->conn) >= PROTOCOL_SMB2_02) {
790 0 : try_ipc_auth = true;
791 0 : } else if (smb_sign_client_connections == SMB_SIGNING_REQUIRED) {
792 : /*
793 : * If we are forcing on SMB signing, then we must
794 : * require authentication unless this is a one-way
795 : * trust, and we have no stored user/password
796 : */
797 0 : try_ipc_auth = true;
798 : }
799 :
800 4 : if (IS_AD_DC) {
801 : /*
802 : * As AD DC we only use netlogon and lsa
803 : * using schannel over an anonymous transport
804 : * (ncacn_ip_tcp or ncacn_np).
805 : *
806 : * Currently we always establish the SMB connection,
807 : * even if we don't use it, because we later use ncacn_ip_tcp.
808 : *
809 : * As we won't use the SMB connection there's no
810 : * need to try kerberos. And NT4 domains expect
811 : * an anonymous IPC$ connection anyway.
812 : */
813 0 : try_ipc_auth = false;
814 : }
815 :
816 4 : if (try_ipc_auth) {
817 4 : result = get_trust_credentials(domain, talloc_tos(), false, &creds);
818 4 : if (!NT_STATUS_IS_OK(result)) {
819 0 : DEBUG(1, ("get_trust_credentials(%s) failed: %s\n",
820 : domain->name, nt_errstr(result)));
821 0 : goto done;
822 : }
823 : } else {
824 : /*
825 : * Without SPNEGO or NTLMSSP (perhaps via SMB2) we
826 : * would try and authentication with our machine
827 : * account password and fail. This is very rare in
828 : * the modern world however
829 : */
830 0 : creds = cli_credentials_init_anon(talloc_tos());
831 0 : if (creds == NULL) {
832 0 : result = NT_STATUS_NO_MEMORY;
833 0 : DEBUG(1, ("cli_credentials_init_anon(%s) failed: %s\n",
834 : domain->name, nt_errstr(result)));
835 0 : goto done;
836 : }
837 : }
838 :
839 4 : machine_principal = cli_credentials_get_principal(creds,
840 : talloc_tos());
841 4 : machine_realm = cli_credentials_get_realm(creds);
842 4 : machine_account = cli_credentials_get_username(creds);
843 4 : machine_domain = cli_credentials_get_domain(creds);
844 :
845 4 : DEBUG(5, ("connecting to %s (%s, %s) with account [%s\\%s] principal "
846 : "[%s] and realm [%s]\n",
847 : controller, domain->name, domain->alt_name,
848 : machine_domain, machine_account,
849 : machine_principal, machine_realm));
850 :
851 4 : if (cli_credentials_is_anonymous(creds)) {
852 0 : goto anon_fallback;
853 : }
854 :
855 4 : winbindd_set_locator_kdc_envs(domain);
856 :
857 4 : result = cli_session_setup_creds(*cli, creds);
858 4 : if (NT_STATUS_IS_OK(result)) {
859 4 : goto session_setup_done;
860 : }
861 :
862 0 : DEBUG(1, ("authenticated session setup to %s using %s failed with %s\n",
863 : controller,
864 : cli_credentials_get_unparsed_name(creds, talloc_tos()),
865 : nt_errstr(result)));
866 :
867 : /*
868 : * If we are not going to validate the connection
869 : * with SMB signing, then allow us to fall back to
870 : * anonymous
871 : */
872 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)
873 0 : || NT_STATUS_EQUAL(result, NT_STATUS_TRUSTED_DOMAIN_FAILURE)
874 0 : || NT_STATUS_EQUAL(result, NT_STATUS_INVALID_ACCOUNT_NAME)
875 0 : || NT_STATUS_EQUAL(result, NT_STATUS_INVALID_COMPUTER_NAME)
876 0 : || NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_DOMAIN)
877 0 : || NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS)
878 0 : || NT_STATUS_EQUAL(result, NT_STATUS_LOGON_FAILURE))
879 : {
880 0 : if (!cm_is_ipc_credentials(creds)) {
881 0 : goto ipc_fallback;
882 : }
883 :
884 0 : if (smb_sign_client_connections == SMB_SIGNING_REQUIRED) {
885 0 : goto done;
886 : }
887 :
888 0 : goto anon_fallback;
889 : }
890 :
891 0 : goto done;
892 :
893 0 : ipc_fallback:
894 0 : TALLOC_FREE(creds);
895 0 : tmp_status = cm_get_ipc_credentials(talloc_tos(), &creds);
896 0 : if (!NT_STATUS_IS_OK(tmp_status)) {
897 0 : result = tmp_status;
898 0 : goto done;
899 : }
900 :
901 0 : if (cli_credentials_is_anonymous(creds)) {
902 0 : goto anon_fallback;
903 : }
904 :
905 0 : machine_account = cli_credentials_get_username(creds);
906 0 : machine_domain = cli_credentials_get_domain(creds);
907 :
908 0 : DEBUG(5, ("connecting to %s from %s using NTLMSSP with username "
909 : "[%s]\\[%s]\n", controller, lp_netbios_name(),
910 : machine_domain, machine_account));
911 :
912 0 : result = cli_session_setup_creds(*cli, creds);
913 0 : if (NT_STATUS_IS_OK(result)) {
914 0 : goto session_setup_done;
915 : }
916 :
917 0 : DEBUG(1, ("authenticated session setup to %s using %s failed with %s\n",
918 : controller,
919 : cli_credentials_get_unparsed_name(creds, talloc_tos()),
920 : nt_errstr(result)));
921 :
922 : /*
923 : * If we are not going to validate the connection
924 : * with SMB signing, then allow us to fall back to
925 : * anonymous
926 : */
927 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)
928 0 : || NT_STATUS_EQUAL(result, NT_STATUS_TRUSTED_DOMAIN_FAILURE)
929 0 : || NT_STATUS_EQUAL(result, NT_STATUS_INVALID_ACCOUNT_NAME)
930 0 : || NT_STATUS_EQUAL(result, NT_STATUS_INVALID_COMPUTER_NAME)
931 0 : || NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_DOMAIN)
932 0 : || NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS)
933 0 : || NT_STATUS_EQUAL(result, NT_STATUS_LOGON_FAILURE))
934 : {
935 0 : goto anon_fallback;
936 : }
937 :
938 0 : goto done;
939 :
940 0 : anon_fallback:
941 0 : TALLOC_FREE(creds);
942 :
943 0 : if (smb_sign_client_connections == SMB_SIGNING_REQUIRED) {
944 0 : goto done;
945 : }
946 :
947 : /* Fall back to anonymous connection, this might fail later */
948 0 : DEBUG(5,("cm_prepare_connection: falling back to anonymous "
949 : "connection for DC %s\n",
950 : controller ));
951 :
952 0 : result = cli_session_setup_anon(*cli);
953 0 : if (NT_STATUS_IS_OK(result)) {
954 0 : DEBUG(5, ("Connected anonymously\n"));
955 0 : goto session_setup_done;
956 : }
957 :
958 0 : DEBUG(1, ("anonymous session setup to %s failed with %s\n",
959 : controller, nt_errstr(result)));
960 :
961 : /* We can't session setup */
962 0 : goto done;
963 :
964 4 : session_setup_done:
965 4 : TALLOC_FREE(creds);
966 :
967 : /*
968 : * This should be a short term hack until
969 : * dynamic re-authentication is implemented.
970 : *
971 : * See Bug 9175 - winbindd doesn't recover from
972 : * NT_STATUS_NETWORK_SESSION_EXPIRED
973 : */
974 4 : if (smbXcli_conn_protocol((*cli)->conn) >= PROTOCOL_SMB2_02) {
975 4 : smbXcli_session_set_disconnect_expired((*cli)->smb2.session);
976 : }
977 :
978 4 : result = cli_tree_connect(*cli, "IPC$", "IPC", NULL);
979 4 : if (!NT_STATUS_IS_OK(result)) {
980 0 : DEBUG(1,("failed tcon_X with %s\n", nt_errstr(result)));
981 0 : goto done;
982 : }
983 4 : tcon_status = result;
984 :
985 : /* cache the server name for later connections */
986 :
987 4 : saf_store(domain->name, controller);
988 4 : if (domain->alt_name) {
989 4 : saf_store(domain->alt_name, controller);
990 : }
991 :
992 4 : winbindd_set_locator_kdc_envs(domain);
993 :
994 4 : TALLOC_FREE(mutex);
995 4 : *retry = False;
996 :
997 4 : result = NT_STATUS_OK;
998 :
999 4 : done:
1000 4 : TALLOC_FREE(mutex);
1001 4 : TALLOC_FREE(creds);
1002 :
1003 4 : if (NT_STATUS_IS_OK(result)) {
1004 4 : result = tcon_status;
1005 : }
1006 :
1007 4 : if (!NT_STATUS_IS_OK(result)) {
1008 0 : DEBUG(1, ("Failed to prepare SMB connection to %s: %s\n",
1009 : controller, nt_errstr(result)));
1010 0 : winbind_add_failed_connection_entry(domain, controller, result);
1011 0 : if ((*cli) != NULL) {
1012 0 : cli_shutdown(*cli);
1013 0 : *cli = NULL;
1014 : }
1015 : }
1016 :
1017 4 : return result;
1018 : }
1019 :
1020 : /*******************************************************************
1021 : Add a dcname and sockaddr_storage pair to the end of a dc_name_ip
1022 : array.
1023 :
1024 : Keeps the list unique by not adding duplicate entries.
1025 :
1026 : @param[in] mem_ctx talloc memory context to allocate from
1027 : @param[in] domain_name domain of the DC
1028 : @param[in] dcname name of the DC to add to the list
1029 : @param[in] pss Internet address and port pair to add to the list
1030 : @param[in,out] dcs array of dc_name_ip structures to add to
1031 : @param[in,out] num_dcs number of dcs returned in the dcs array
1032 : @return true if the list was added to, false otherwise
1033 : *******************************************************************/
1034 :
1035 8 : static bool add_one_dc_unique(TALLOC_CTX *mem_ctx, const char *domain_name,
1036 : const char *dcname, struct sockaddr_storage *pss,
1037 : struct dc_name_ip **dcs, int *num)
1038 : {
1039 8 : int i = 0;
1040 :
1041 8 : if (!NT_STATUS_IS_OK(check_negative_conn_cache(domain_name, dcname))) {
1042 0 : DEBUG(10, ("DC %s was in the negative conn cache\n", dcname));
1043 0 : return False;
1044 : }
1045 :
1046 : /* Make sure there's no duplicates in the list */
1047 12 : for (i=0; i<*num; i++)
1048 8 : if (sockaddr_equal(
1049 8 : (struct sockaddr *)(void *)&(*dcs)[i].ss,
1050 : (struct sockaddr *)(void *)pss))
1051 4 : return False;
1052 :
1053 4 : *dcs = talloc_realloc(mem_ctx, *dcs, struct dc_name_ip, (*num)+1);
1054 :
1055 4 : if (*dcs == NULL)
1056 0 : return False;
1057 :
1058 4 : fstrcpy((*dcs)[*num].name, dcname);
1059 4 : (*dcs)[*num].ss = *pss;
1060 4 : *num += 1;
1061 4 : return True;
1062 : }
1063 :
1064 4 : static bool add_sockaddr_to_array(TALLOC_CTX *mem_ctx,
1065 : struct sockaddr_storage *pss, uint16_t port,
1066 : struct sockaddr_storage **addrs, int *num)
1067 : {
1068 4 : *addrs = talloc_realloc(mem_ctx, *addrs, struct sockaddr_storage, (*num)+1);
1069 :
1070 4 : if (*addrs == NULL) {
1071 0 : *num = 0;
1072 0 : return False;
1073 : }
1074 :
1075 4 : (*addrs)[*num] = *pss;
1076 4 : set_sockaddr_port((struct sockaddr *)&(*addrs)[*num], port);
1077 :
1078 4 : *num += 1;
1079 4 : return True;
1080 : }
1081 :
1082 : #ifdef HAVE_ADS
1083 4 : static bool dcip_check_name_ads(const struct winbindd_domain *domain,
1084 : struct samba_sockaddr *sa,
1085 : uint32_t request_flags,
1086 : TALLOC_CTX *mem_ctx,
1087 : char **namep)
1088 : {
1089 4 : TALLOC_CTX *tmp_ctx = talloc_stackframe();
1090 4 : char *name = NULL;
1091 4 : ADS_STRUCT *ads = NULL;
1092 : ADS_STATUS ads_status;
1093 : char addr[INET6_ADDRSTRLEN];
1094 :
1095 4 : print_sockaddr(addr, sizeof(addr), &sa->u.ss);
1096 :
1097 4 : ads = ads_init(tmp_ctx,
1098 4 : domain->alt_name,
1099 4 : domain->name,
1100 : addr,
1101 : ADS_SASL_PLAIN);
1102 4 : if (ads == NULL) {
1103 0 : ads_status = ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
1104 0 : goto out;
1105 : }
1106 4 : ads->auth.flags |= ADS_AUTH_NO_BIND;
1107 4 : ads->config.flags |= request_flags;
1108 4 : ads->server.no_fallback = true;
1109 :
1110 4 : ads_status = ads_connect(ads);
1111 4 : if (!ADS_ERR_OK(ads_status)) {
1112 0 : goto out;
1113 : }
1114 :
1115 : /* We got a cldap packet. */
1116 4 : name = talloc_strdup(tmp_ctx, ads->config.ldap_server_name);
1117 4 : if (name == NULL) {
1118 0 : ads_status = ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
1119 0 : goto out;
1120 : }
1121 4 : namecache_store(name, 0x20, 1, sa);
1122 :
1123 4 : DBG_DEBUG("CLDAP flags = 0x%"PRIx32"\n", ads->config.flags);
1124 :
1125 4 : if (domain->primary && (ads->config.flags & NBT_SERVER_KDC)) {
1126 2 : if (ads_closest_dc(ads)) {
1127 2 : char *sitename = sitename_fetch(tmp_ctx,
1128 2 : ads->config.realm);
1129 :
1130 : /* We're going to use this KDC for this realm/domain.
1131 : If we are using sites, then force the krb5 libs
1132 : to use this KDC. */
1133 :
1134 2 : create_local_private_krb5_conf_for_domain(domain->alt_name,
1135 2 : domain->name,
1136 : sitename,
1137 2 : &sa->u.ss);
1138 :
1139 2 : TALLOC_FREE(sitename);
1140 : } else {
1141 : /* use an off site KDC */
1142 0 : create_local_private_krb5_conf_for_domain(domain->alt_name,
1143 0 : domain->name,
1144 : NULL,
1145 0 : &sa->u.ss);
1146 : }
1147 2 : winbindd_set_locator_kdc_envs(domain);
1148 :
1149 : /* Ensure we contact this DC also. */
1150 2 : saf_store(domain->name, name);
1151 2 : saf_store(domain->alt_name, name);
1152 : }
1153 :
1154 4 : *namep = talloc_move(mem_ctx, &name);
1155 :
1156 4 : out:
1157 4 : TALLOC_FREE(tmp_ctx);
1158 :
1159 4 : return ADS_ERR_OK(ads_status) ? true : false;
1160 : }
1161 : #endif
1162 :
1163 : /*******************************************************************
1164 : convert an ip to a name
1165 : For an AD Domain, it checks the requirements of the request flags.
1166 : *******************************************************************/
1167 :
1168 4 : static bool dcip_check_name(TALLOC_CTX *mem_ctx,
1169 : const struct winbindd_domain *domain,
1170 : struct sockaddr_storage *pss,
1171 : char **name, uint32_t request_flags)
1172 : {
1173 4 : struct samba_sockaddr sa = {0};
1174 4 : uint32_t nt_version = NETLOGON_NT_VERSION_1;
1175 : NTSTATUS status;
1176 : const char *dc_name;
1177 : fstring nbtname;
1178 : #ifdef HAVE_ADS
1179 4 : bool is_ad_domain = false;
1180 : #endif
1181 4 : bool ok = sockaddr_storage_to_samba_sockaddr(&sa, pss);
1182 4 : if (!ok) {
1183 0 : return false;
1184 : }
1185 :
1186 : #ifdef HAVE_ADS
1187 : /* For active directory servers, try to get the ldap server name.
1188 : None of these failures should be considered critical for now */
1189 :
1190 4 : if ((lp_security() == SEC_ADS) && (domain->alt_name != NULL)) {
1191 4 : is_ad_domain = true;
1192 0 : } else if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
1193 0 : is_ad_domain = domain->active_directory;
1194 : }
1195 :
1196 4 : if (is_ad_domain) {
1197 4 : return dcip_check_name_ads(domain,
1198 : &sa,
1199 : request_flags,
1200 : mem_ctx,
1201 : name);
1202 : }
1203 : #endif
1204 :
1205 0 : {
1206 0 : size_t len = strlen(lp_netbios_name());
1207 0 : char my_acct_name[len+2];
1208 :
1209 0 : snprintf(my_acct_name,
1210 : sizeof(my_acct_name),
1211 : "%s$",
1212 : lp_netbios_name());
1213 :
1214 0 : status = nbt_getdc(global_messaging_context(), 10, &sa.u.ss,
1215 0 : domain->name, &domain->sid,
1216 : my_acct_name, ACB_WSTRUST,
1217 : nt_version, mem_ctx, &nt_version,
1218 : &dc_name, NULL);
1219 : }
1220 0 : if (NT_STATUS_IS_OK(status)) {
1221 0 : *name = talloc_strdup(mem_ctx, dc_name);
1222 0 : if (*name == NULL) {
1223 0 : return false;
1224 : }
1225 0 : namecache_store(*name, 0x20, 1, &sa);
1226 0 : return True;
1227 : }
1228 :
1229 : /* try node status request */
1230 :
1231 0 : if (name_status_find(domain->name, 0x1c, 0x20, &sa.u.ss, nbtname) ) {
1232 0 : namecache_store(nbtname, 0x20, 1, &sa);
1233 :
1234 0 : if (name != NULL) {
1235 0 : *name = talloc_strdup(mem_ctx, nbtname);
1236 0 : if (*name == NULL) {
1237 0 : return false;
1238 : }
1239 : }
1240 :
1241 0 : return true;
1242 : }
1243 0 : return False;
1244 : }
1245 :
1246 : /*******************************************************************
1247 : Retrieve a list of IP addresses for domain controllers.
1248 :
1249 : The array is sorted in the preferred connection order.
1250 :
1251 : @param[in] mem_ctx talloc memory context to allocate from
1252 : @param[in] domain domain to retrieve DCs for
1253 : @param[out] dcs array of dcs that will be returned
1254 : @param[out] num_dcs number of dcs returned in the dcs array
1255 : @return always true
1256 : *******************************************************************/
1257 :
1258 2 : static bool get_dcs(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
1259 : struct dc_name_ip **dcs, int *num_dcs,
1260 : uint32_t request_flags)
1261 : {
1262 : fstring dcname;
1263 : struct sockaddr_storage ss;
1264 2 : struct samba_sockaddr *sa_list = NULL;
1265 2 : size_t salist_size = 0;
1266 : size_t i;
1267 : bool is_our_domain;
1268 2 : enum security_types sec = (enum security_types)lp_security();
1269 :
1270 2 : is_our_domain = strequal(domain->name, lp_workgroup());
1271 :
1272 : /* If not our domain, get the preferred DC, by asking our primary DC */
1273 2 : if ( !is_our_domain
1274 2 : && get_dc_name_via_netlogon(domain, dcname, &ss, request_flags)
1275 0 : && add_one_dc_unique(mem_ctx, domain->name, dcname, &ss, dcs,
1276 : num_dcs) )
1277 : {
1278 : char addr[INET6_ADDRSTRLEN];
1279 0 : print_sockaddr(addr, sizeof(addr), &ss);
1280 0 : DEBUG(10, ("Retrieved DC %s at %s via netlogon\n",
1281 : dcname, addr));
1282 0 : return True;
1283 : }
1284 :
1285 2 : if ((sec == SEC_ADS) && (domain->alt_name != NULL)) {
1286 2 : char *sitename = NULL;
1287 :
1288 : /* We need to make sure we know the local site before
1289 : doing any DNS queries, as this will restrict the
1290 : get_sorted_dc_list() call below to only fetching
1291 : DNS records for the correct site. */
1292 :
1293 : /* Find any DC to get the site record.
1294 : We deliberately don't care about the
1295 : return here. */
1296 :
1297 2 : get_dc_name(domain->name, domain->alt_name, dcname, &ss);
1298 :
1299 2 : sitename = sitename_fetch(mem_ctx, domain->alt_name);
1300 2 : if (sitename) {
1301 :
1302 : /* Do the site-specific AD dns lookup first. */
1303 2 : (void)get_sorted_dc_list(mem_ctx,
1304 2 : domain->alt_name,
1305 : sitename,
1306 : &sa_list,
1307 : &salist_size,
1308 : true);
1309 :
1310 : /* Add ips to the DC array. We don't look up the name
1311 : of the DC in this function, but we fill in the char*
1312 : of the ip now to make the failed connection cache
1313 : work */
1314 6 : for ( i=0; i<salist_size; i++ ) {
1315 : char addr[INET6_ADDRSTRLEN];
1316 4 : print_sockaddr(addr, sizeof(addr),
1317 4 : &sa_list[i].u.ss);
1318 6 : add_one_dc_unique(mem_ctx,
1319 4 : domain->name,
1320 : addr,
1321 4 : &sa_list[i].u.ss,
1322 : dcs,
1323 : num_dcs);
1324 : }
1325 :
1326 2 : TALLOC_FREE(sa_list);
1327 2 : TALLOC_FREE(sitename);
1328 2 : salist_size = 0;
1329 : }
1330 :
1331 : /* Now we add DCs from the main AD DNS lookup. */
1332 2 : (void)get_sorted_dc_list(mem_ctx,
1333 2 : domain->alt_name,
1334 : NULL,
1335 : &sa_list,
1336 : &salist_size,
1337 : true);
1338 :
1339 6 : for ( i=0; i<salist_size; i++ ) {
1340 : char addr[INET6_ADDRSTRLEN];
1341 4 : print_sockaddr(addr, sizeof(addr),
1342 4 : &sa_list[i].u.ss);
1343 6 : add_one_dc_unique(mem_ctx,
1344 4 : domain->name,
1345 : addr,
1346 4 : &sa_list[i].u.ss,
1347 : dcs,
1348 : num_dcs);
1349 : }
1350 :
1351 2 : TALLOC_FREE(sa_list);
1352 2 : salist_size = 0;
1353 : }
1354 :
1355 : /* Try standard netbios queries if no ADS and fall back to DNS queries
1356 : * if alt_name is available */
1357 2 : if (*num_dcs == 0) {
1358 0 : (void)get_sorted_dc_list(mem_ctx,
1359 0 : domain->name,
1360 : NULL,
1361 : &sa_list,
1362 : &salist_size,
1363 : false);
1364 0 : if (salist_size == 0) {
1365 0 : if (domain->alt_name != NULL) {
1366 0 : (void)get_sorted_dc_list(mem_ctx,
1367 0 : domain->alt_name,
1368 : NULL,
1369 : &sa_list,
1370 : &salist_size,
1371 : true);
1372 : }
1373 : }
1374 :
1375 0 : for ( i=0; i<salist_size; i++ ) {
1376 : char addr[INET6_ADDRSTRLEN];
1377 0 : print_sockaddr(addr, sizeof(addr),
1378 0 : &sa_list[i].u.ss);
1379 0 : add_one_dc_unique(mem_ctx,
1380 0 : domain->name,
1381 : addr,
1382 0 : &sa_list[i].u.ss,
1383 : dcs,
1384 : num_dcs);
1385 : }
1386 :
1387 0 : TALLOC_FREE(sa_list);
1388 0 : salist_size = 0;
1389 : }
1390 :
1391 2 : return True;
1392 : }
1393 :
1394 : /*******************************************************************
1395 : Find and make a connection to a DC in the given domain.
1396 :
1397 : @param[in] mem_ctx talloc memory context to allocate from
1398 : @param[in] domain domain to find a dc in
1399 : @param[out] dcname NetBIOS or FQDN of DC that's connected to
1400 : @param[out] pss DC Internet address and port
1401 : @param[out] fd fd of the open socket connected to the newly found dc
1402 : @return true when a DC connection is made, false otherwise
1403 : *******************************************************************/
1404 :
1405 2 : static bool find_new_dc(TALLOC_CTX *mem_ctx,
1406 : struct winbindd_domain *domain,
1407 : char **dcname, struct sockaddr_storage *pss, int *fd,
1408 : uint32_t request_flags)
1409 : {
1410 2 : struct dc_name_ip *dcs = NULL;
1411 2 : int num_dcs = 0;
1412 :
1413 2 : const char **dcnames = NULL;
1414 2 : size_t num_dcnames = 0;
1415 :
1416 2 : struct sockaddr_storage *addrs = NULL;
1417 2 : int num_addrs = 0;
1418 :
1419 : int i;
1420 : size_t fd_index;
1421 :
1422 : NTSTATUS status;
1423 :
1424 2 : *fd = -1;
1425 :
1426 2 : again:
1427 2 : if (!get_dcs(mem_ctx, domain, &dcs, &num_dcs, request_flags) || (num_dcs == 0))
1428 0 : return False;
1429 :
1430 6 : for (i=0; i<num_dcs; i++) {
1431 :
1432 4 : if (!add_string_to_array(mem_ctx, dcs[i].name,
1433 : &dcnames, &num_dcnames)) {
1434 0 : return False;
1435 : }
1436 4 : if (!add_sockaddr_to_array(mem_ctx, &dcs[i].ss, TCP_SMB_PORT,
1437 : &addrs, &num_addrs)) {
1438 0 : return False;
1439 : }
1440 : }
1441 :
1442 2 : if ((num_dcnames == 0) || (num_dcnames != num_addrs))
1443 0 : return False;
1444 :
1445 2 : if ((addrs == NULL) || (dcnames == NULL))
1446 0 : return False;
1447 :
1448 2 : status = smbsock_any_connect(addrs, dcnames, NULL, NULL, NULL,
1449 : num_addrs, 0, 10, fd, &fd_index, NULL);
1450 2 : if (!NT_STATUS_IS_OK(status)) {
1451 0 : for (i=0; i<num_dcs; i++) {
1452 : char ab[INET6_ADDRSTRLEN];
1453 0 : print_sockaddr(ab, sizeof(ab), &dcs[i].ss);
1454 0 : DEBUG(10, ("find_new_dc: smbsock_any_connect failed for "
1455 : "domain %s address %s. Error was %s\n",
1456 : domain->name, ab, nt_errstr(status) ));
1457 0 : winbind_add_failed_connection_entry(domain,
1458 0 : dcs[i].name, NT_STATUS_UNSUCCESSFUL);
1459 : }
1460 0 : return False;
1461 : }
1462 :
1463 2 : *pss = addrs[fd_index];
1464 :
1465 2 : if (*dcnames[fd_index] != '\0' && !is_ipaddress(dcnames[fd_index])) {
1466 : /* Ok, we've got a name for the DC */
1467 0 : *dcname = talloc_strdup(mem_ctx, dcnames[fd_index]);
1468 0 : if (*dcname == NULL) {
1469 0 : return false;
1470 : }
1471 0 : return true;
1472 : }
1473 :
1474 : /* Try to figure out the name */
1475 2 : if (dcip_check_name(mem_ctx, domain, pss, dcname, request_flags)) {
1476 2 : return True;
1477 : }
1478 :
1479 : /* We can not continue without the DC's name */
1480 0 : winbind_add_failed_connection_entry(domain, dcs[fd_index].name,
1481 0 : NT_STATUS_UNSUCCESSFUL);
1482 :
1483 : /* Throw away all arrays as we're doing this again. */
1484 0 : TALLOC_FREE(dcs);
1485 0 : num_dcs = 0;
1486 :
1487 0 : TALLOC_FREE(dcnames);
1488 0 : num_dcnames = 0;
1489 :
1490 0 : TALLOC_FREE(addrs);
1491 0 : num_addrs = 0;
1492 :
1493 0 : if (*fd != -1) {
1494 0 : close(*fd);
1495 0 : *fd = -1;
1496 : }
1497 :
1498 0 : goto again;
1499 : }
1500 :
1501 6 : static char *current_dc_key(TALLOC_CTX *mem_ctx, const char *domain_name)
1502 : {
1503 6 : return talloc_asprintf_strupper_m(mem_ctx, "CURRENT_DCNAME/%s",
1504 : domain_name);
1505 : }
1506 :
1507 4 : static void store_current_dc_in_gencache(const char *domain_name,
1508 : const char *dc_name,
1509 : struct cli_state *cli)
1510 : {
1511 : char addr[INET6_ADDRSTRLEN];
1512 4 : char *key = NULL;
1513 4 : char *value = NULL;
1514 :
1515 4 : if (!cli_state_is_connected(cli)) {
1516 0 : return;
1517 : }
1518 :
1519 4 : print_sockaddr(addr, sizeof(addr),
1520 : smbXcli_conn_remote_sockaddr(cli->conn));
1521 :
1522 4 : key = current_dc_key(talloc_tos(), domain_name);
1523 4 : if (key == NULL) {
1524 0 : goto done;
1525 : }
1526 :
1527 4 : value = talloc_asprintf(talloc_tos(), "%s %s", addr, dc_name);
1528 4 : if (value == NULL) {
1529 0 : goto done;
1530 : }
1531 :
1532 4 : gencache_set(key, value, 0x7fffffff);
1533 4 : done:
1534 4 : TALLOC_FREE(value);
1535 4 : TALLOC_FREE(key);
1536 : }
1537 :
1538 2 : bool fetch_current_dc_from_gencache(TALLOC_CTX *mem_ctx,
1539 : const char *domain_name,
1540 : char **p_dc_name, char **p_dc_ip)
1541 : {
1542 : char *key, *p;
1543 2 : char *value = NULL;
1544 2 : bool ret = false;
1545 2 : char *dc_name = NULL;
1546 2 : char *dc_ip = NULL;
1547 :
1548 2 : key = current_dc_key(talloc_tos(), domain_name);
1549 2 : if (key == NULL) {
1550 0 : goto done;
1551 : }
1552 2 : if (!gencache_get(key, mem_ctx, &value, NULL)) {
1553 0 : goto done;
1554 : }
1555 2 : p = strchr(value, ' ');
1556 2 : if (p == NULL) {
1557 0 : goto done;
1558 : }
1559 2 : dc_ip = talloc_strndup(mem_ctx, value, p - value);
1560 2 : if (dc_ip == NULL) {
1561 0 : goto done;
1562 : }
1563 2 : dc_name = talloc_strdup(mem_ctx, p+1);
1564 2 : if (dc_name == NULL) {
1565 0 : goto done;
1566 : }
1567 :
1568 2 : if (p_dc_ip != NULL) {
1569 2 : *p_dc_ip = dc_ip;
1570 2 : dc_ip = NULL;
1571 : }
1572 2 : if (p_dc_name != NULL) {
1573 2 : *p_dc_name = dc_name;
1574 2 : dc_name = NULL;
1575 : }
1576 2 : ret = true;
1577 2 : done:
1578 2 : TALLOC_FREE(dc_name);
1579 2 : TALLOC_FREE(dc_ip);
1580 2 : TALLOC_FREE(key);
1581 2 : TALLOC_FREE(value);
1582 2 : return ret;
1583 : }
1584 :
1585 0 : NTSTATUS wb_open_internal_pipe(TALLOC_CTX *mem_ctx,
1586 : const struct ndr_interface_table *table,
1587 : struct rpc_pipe_client **ret_pipe)
1588 : {
1589 0 : struct rpc_pipe_client *cli = NULL;
1590 0 : const struct auth_session_info *session_info = NULL;
1591 0 : NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1592 :
1593 :
1594 0 : session_info = get_session_info_system();
1595 0 : SMB_ASSERT(session_info != NULL);
1596 :
1597 0 : status = rpc_pipe_open_local_np(
1598 : mem_ctx, table, NULL, NULL, NULL, NULL, session_info, &cli);
1599 0 : if (!NT_STATUS_IS_OK(status)) {
1600 0 : return status;
1601 : }
1602 :
1603 0 : if (ret_pipe) {
1604 0 : *ret_pipe = cli;
1605 : }
1606 :
1607 0 : return NT_STATUS_OK;
1608 : }
1609 :
1610 4 : static NTSTATUS cm_open_connection(struct winbindd_domain *domain,
1611 : struct winbindd_cm_conn *new_conn,
1612 : bool need_rw_dc)
1613 : {
1614 : TALLOC_CTX *mem_ctx;
1615 : NTSTATUS result;
1616 : char *saf_servername;
1617 : int retries;
1618 4 : uint32_t request_flags = need_rw_dc ? DS_WRITABLE_REQUIRED : 0;
1619 :
1620 4 : if ((mem_ctx = talloc_init("cm_open_connection")) == NULL) {
1621 0 : set_domain_offline(domain);
1622 0 : return NT_STATUS_NO_MEMORY;
1623 : }
1624 :
1625 4 : saf_servername = saf_fetch(mem_ctx, domain->name );
1626 :
1627 : /* we have to check the server affinity cache here since
1628 : later we select a DC based on response time and not preference */
1629 :
1630 : /* Check the negative connection cache
1631 : before talking to it. It going down may have
1632 : triggered the reconnection. */
1633 :
1634 4 : if (saf_servername && NT_STATUS_IS_OK(check_negative_conn_cache(domain->name, saf_servername))) {
1635 : struct sockaddr_storage ss;
1636 2 : char *dcname = NULL;
1637 2 : bool resolved = true;
1638 :
1639 2 : DEBUG(10, ("cm_open_connection: saf_servername is '%s' for domain %s\n",
1640 : saf_servername, domain->name));
1641 :
1642 : /* convert an ip address to a name */
1643 2 : if (is_ipaddress(saf_servername)) {
1644 0 : if (!interpret_string_addr(&ss, saf_servername,
1645 : AI_NUMERICHOST)) {
1646 0 : TALLOC_FREE(mem_ctx);
1647 0 : return NT_STATUS_UNSUCCESSFUL;
1648 : }
1649 : } else {
1650 2 : if (!resolve_name(saf_servername, &ss, 0x20, true)) {
1651 0 : resolved = false;
1652 : }
1653 : }
1654 :
1655 2 : if (resolved && dcip_check_name(mem_ctx, domain, &ss, &dcname, request_flags)) {
1656 2 : domain->dcname = talloc_strdup(domain,
1657 : dcname);
1658 2 : if (domain->dcname == NULL) {
1659 0 : TALLOC_FREE(mem_ctx);
1660 0 : return NT_STATUS_NO_MEMORY;
1661 : }
1662 :
1663 2 : domain->dcaddr = ss;
1664 : } else {
1665 0 : winbind_add_failed_connection_entry(domain, saf_servername,
1666 0 : NT_STATUS_UNSUCCESSFUL);
1667 : }
1668 : }
1669 :
1670 6 : for (retries = 0; retries < 3; retries++) {
1671 4 : int fd = -1;
1672 4 : bool retry = False;
1673 4 : char *dcname = NULL;
1674 :
1675 4 : result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1676 :
1677 4 : DEBUG(10, ("cm_open_connection: dcname is '%s' for domain %s\n",
1678 : domain->dcname ? domain->dcname : "", domain->name));
1679 :
1680 5 : if (domain->dcname != NULL &&
1681 2 : NT_STATUS_IS_OK(check_negative_conn_cache(domain->name,
1682 : domain->dcname)))
1683 : {
1684 : NTSTATUS status;
1685 :
1686 2 : status = smbsock_connect(&domain->dcaddr, 0,
1687 : NULL, -1, NULL, -1,
1688 : &fd, NULL, 10);
1689 2 : if (!NT_STATUS_IS_OK(status)) {
1690 0 : fd = -1;
1691 : }
1692 : }
1693 :
1694 5 : if ((fd == -1) &&
1695 2 : !find_new_dc(mem_ctx, domain, &dcname, &domain->dcaddr, &fd, request_flags))
1696 : {
1697 : /* This is the one place where we will
1698 : set the global winbindd offline state
1699 : to true, if a "WINBINDD_OFFLINE" entry
1700 : is found in the winbindd cache. */
1701 0 : set_global_winbindd_state_offline();
1702 2 : break;
1703 : }
1704 4 : if (dcname != NULL) {
1705 2 : talloc_free(domain->dcname);
1706 :
1707 2 : domain->dcname = talloc_move(domain, &dcname);
1708 2 : if (domain->dcname == NULL) {
1709 0 : result = NT_STATUS_NO_MEMORY;
1710 0 : break;
1711 : }
1712 : }
1713 :
1714 4 : new_conn->cli = NULL;
1715 :
1716 4 : result = cm_prepare_connection(domain, fd, domain->dcname,
1717 : &new_conn->cli, &retry);
1718 4 : if (!NT_STATUS_IS_OK(result)) {
1719 : /* Don't leak the smb connection socket */
1720 0 : if (fd != -1) {
1721 0 : close(fd);
1722 0 : fd = -1;
1723 : }
1724 : }
1725 :
1726 4 : if (!retry)
1727 4 : break;
1728 : }
1729 :
1730 4 : if (NT_STATUS_IS_OK(result)) {
1731 4 : bool seal_pipes = true;
1732 :
1733 4 : winbindd_set_locator_kdc_envs(domain);
1734 :
1735 4 : if (domain->online == False) {
1736 : /* We're changing state from offline to online. */
1737 2 : set_global_winbindd_state_online();
1738 : }
1739 4 : set_domain_online(domain);
1740 :
1741 : /*
1742 : * Much as I hate global state, this seems to be the point
1743 : * where we can be certain that we have a proper connection to
1744 : * a DC. wbinfo --dc-info needs that information, store it in
1745 : * gencache with a looong timeout. This will need revisiting
1746 : * once we start to connect to multiple DCs, wbcDcInfo is
1747 : * already prepared for that.
1748 : */
1749 4 : store_current_dc_in_gencache(domain->name, domain->dcname,
1750 : new_conn->cli);
1751 :
1752 4 : seal_pipes = lp_winbind_sealed_pipes();
1753 6 : seal_pipes = lp_parm_bool(-1, "winbind sealed pipes",
1754 4 : domain->name,
1755 : seal_pipes);
1756 :
1757 4 : if (seal_pipes) {
1758 4 : new_conn->auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1759 : } else {
1760 0 : new_conn->auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1761 : }
1762 : } else {
1763 : /* Ensure we setup the retry handler. */
1764 0 : set_domain_offline(domain);
1765 : }
1766 :
1767 4 : talloc_destroy(mem_ctx);
1768 4 : return result;
1769 : }
1770 :
1771 : /* Close down all open pipes on a connection. */
1772 :
1773 10 : void invalidate_cm_connection(struct winbindd_domain *domain)
1774 : {
1775 : NTSTATUS result;
1776 10 : struct winbindd_cm_conn *conn = &domain->conn;
1777 :
1778 10 : domain->sequence_number = DOM_SEQUENCE_NONE;
1779 10 : domain->last_seq_check = 0;
1780 10 : domain->last_status = NT_STATUS_SERVER_DISABLED;
1781 :
1782 : /* We're closing down a possibly dead
1783 : connection. Don't have impossibly long (10s) timeouts. */
1784 :
1785 10 : if (conn->cli) {
1786 0 : cli_set_timeout(conn->cli, 1000); /* 1 second. */
1787 : }
1788 :
1789 10 : if (conn->samr_pipe != NULL) {
1790 0 : if (is_valid_policy_hnd(&conn->sam_connect_handle)) {
1791 0 : dcerpc_samr_Close(conn->samr_pipe->binding_handle,
1792 : talloc_tos(),
1793 : &conn->sam_connect_handle,
1794 : &result);
1795 : }
1796 0 : TALLOC_FREE(conn->samr_pipe);
1797 : /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1798 0 : if (conn->cli) {
1799 0 : cli_set_timeout(conn->cli, 500);
1800 : }
1801 : }
1802 :
1803 10 : if (conn->lsa_pipe != NULL) {
1804 0 : if (is_valid_policy_hnd(&conn->lsa_policy)) {
1805 0 : dcerpc_lsa_Close(conn->lsa_pipe->binding_handle,
1806 : talloc_tos(),
1807 : &conn->lsa_policy,
1808 : &result);
1809 : }
1810 0 : TALLOC_FREE(conn->lsa_pipe);
1811 : /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1812 0 : if (conn->cli) {
1813 0 : cli_set_timeout(conn->cli, 500);
1814 : }
1815 : }
1816 :
1817 10 : if (conn->lsa_pipe_tcp != NULL) {
1818 0 : if (is_valid_policy_hnd(&conn->lsa_policy)) {
1819 0 : dcerpc_lsa_Close(conn->lsa_pipe_tcp->binding_handle,
1820 : talloc_tos(),
1821 : &conn->lsa_policy,
1822 : &result);
1823 : }
1824 0 : TALLOC_FREE(conn->lsa_pipe_tcp);
1825 : /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1826 0 : if (conn->cli) {
1827 0 : cli_set_timeout(conn->cli, 500);
1828 : }
1829 : }
1830 :
1831 10 : if (conn->netlogon_pipe != NULL) {
1832 0 : TALLOC_FREE(conn->netlogon_pipe);
1833 : /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1834 0 : if (conn->cli) {
1835 0 : cli_set_timeout(conn->cli, 500);
1836 : }
1837 : }
1838 :
1839 10 : conn->auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1840 10 : TALLOC_FREE(conn->netlogon_creds_ctx);
1841 :
1842 10 : if (conn->cli) {
1843 0 : cli_shutdown(conn->cli);
1844 : }
1845 :
1846 10 : conn->cli = NULL;
1847 10 : }
1848 :
1849 2 : void close_conns_after_fork(void)
1850 : {
1851 : struct winbindd_domain *domain;
1852 : struct winbindd_cli_state *cli_state;
1853 :
1854 8 : for (domain = domain_list(); domain; domain = domain->next) {
1855 : /*
1856 : * first close the low level SMB TCP connection
1857 : * so that we don't generate any SMBclose
1858 : * requests in invalidate_cm_connection()
1859 : */
1860 6 : if (cli_state_is_connected(domain->conn.cli)) {
1861 0 : smbXcli_conn_disconnect(domain->conn.cli->conn, NT_STATUS_OK);
1862 : }
1863 :
1864 6 : invalidate_cm_connection(domain);
1865 : }
1866 :
1867 4 : for (cli_state = winbindd_client_list();
1868 0 : cli_state != NULL;
1869 0 : cli_state = cli_state->next) {
1870 0 : if (cli_state->sock >= 0) {
1871 0 : close(cli_state->sock);
1872 0 : cli_state->sock = -1;
1873 : }
1874 : }
1875 2 : }
1876 :
1877 14 : static bool connection_ok(struct winbindd_domain *domain)
1878 : {
1879 : bool ok;
1880 :
1881 14 : ok = cli_state_is_connected(domain->conn.cli);
1882 14 : if (!ok) {
1883 6 : DEBUG(3, ("connection_ok: Connection to %s for domain %s is not connected\n",
1884 : domain->dcname, domain->name));
1885 6 : return False;
1886 : }
1887 :
1888 8 : if (!domain->online) {
1889 0 : DEBUG(3, ("connection_ok: Domain %s is offline\n", domain->name));
1890 0 : return False;
1891 : }
1892 :
1893 8 : return True;
1894 : }
1895 :
1896 : /* Initialize a new connection up to the RPC BIND.
1897 : Bypass online status check so always does network calls. */
1898 :
1899 10 : static NTSTATUS init_dc_connection_network(struct winbindd_domain *domain, bool need_rw_dc)
1900 : {
1901 : NTSTATUS result;
1902 10 : bool skip_connection = domain->internal;
1903 10 : if (need_rw_dc && domain->rodc) {
1904 0 : skip_connection = false;
1905 : }
1906 :
1907 : /* Internal connections never use the network. */
1908 10 : if (dom_sid_equal(&domain->sid, &global_sid_Builtin)) {
1909 0 : return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1910 : }
1911 :
1912 : /* Still ask the internal LSA and SAMR server about the local domain */
1913 10 : if (skip_connection || connection_ok(domain)) {
1914 6 : if (!domain->initialized) {
1915 0 : set_dc_type_and_flags(domain);
1916 : }
1917 6 : return NT_STATUS_OK;
1918 : }
1919 :
1920 4 : invalidate_cm_connection(domain);
1921 :
1922 4 : if (!domain->primary && !domain->initialized) {
1923 : /*
1924 : * Before we connect to a trust, work out if it is an
1925 : * AD domain by asking our own domain.
1926 : */
1927 2 : set_dc_type_and_flags_trustinfo(domain);
1928 : }
1929 :
1930 4 : result = cm_open_connection(domain, &domain->conn, need_rw_dc);
1931 :
1932 4 : if (NT_STATUS_IS_OK(result) && !domain->initialized) {
1933 2 : set_dc_type_and_flags(domain);
1934 : }
1935 :
1936 4 : return result;
1937 : }
1938 :
1939 16 : NTSTATUS init_dc_connection(struct winbindd_domain *domain, bool need_rw_dc)
1940 : {
1941 16 : if (dom_sid_equal(&domain->sid, &global_sid_Builtin)) {
1942 6 : return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1943 : }
1944 :
1945 10 : SMB_ASSERT(wb_child_domain() || idmap_child());
1946 :
1947 10 : return init_dc_connection_network(domain, need_rw_dc);
1948 : }
1949 :
1950 8 : static NTSTATUS init_dc_connection_rpc(struct winbindd_domain *domain, bool need_rw_dc)
1951 : {
1952 : NTSTATUS status;
1953 :
1954 8 : status = init_dc_connection(domain, need_rw_dc);
1955 8 : if (!NT_STATUS_IS_OK(status)) {
1956 0 : return status;
1957 : }
1958 :
1959 8 : if (!domain->internal && domain->conn.cli == NULL) {
1960 : /* happens for trusted domains without inbound trust */
1961 0 : return NT_STATUS_TRUSTED_DOMAIN_FAILURE;
1962 : }
1963 :
1964 8 : return NT_STATUS_OK;
1965 : }
1966 :
1967 : /******************************************************************************
1968 : Set the trust flags (direction and forest location) for a domain
1969 : ******************************************************************************/
1970 :
1971 4 : static bool set_dc_type_and_flags_trustinfo( struct winbindd_domain *domain )
1972 : {
1973 : struct winbindd_domain *our_domain;
1974 4 : NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1975 : WERROR werr;
1976 : struct netr_DomainTrustList trusts;
1977 : int i;
1978 4 : uint32_t flags = (NETR_TRUST_FLAG_IN_FOREST |
1979 : NETR_TRUST_FLAG_OUTBOUND |
1980 : NETR_TRUST_FLAG_INBOUND);
1981 : struct rpc_pipe_client *cli;
1982 4 : TALLOC_CTX *mem_ctx = NULL;
1983 : struct dcerpc_binding_handle *b;
1984 :
1985 4 : if (IS_DC) {
1986 : /*
1987 : * On a DC we loaded all trusts
1988 : * from configuration and never learn
1989 : * new domains.
1990 : */
1991 0 : return true;
1992 : }
1993 :
1994 4 : DEBUG(5, ("set_dc_type_and_flags_trustinfo: domain %s\n", domain->name ));
1995 :
1996 : /* Our primary domain doesn't need to worry about trust flags.
1997 : Force it to go through the network setup */
1998 4 : if ( domain->primary ) {
1999 0 : return False;
2000 : }
2001 :
2002 4 : mem_ctx = talloc_stackframe();
2003 4 : our_domain = find_our_domain();
2004 4 : if (our_domain->internal) {
2005 0 : result = init_dc_connection(our_domain, false);
2006 0 : if (!NT_STATUS_IS_OK(result)) {
2007 0 : DEBUG(3,("set_dc_type_and_flags_trustinfo: "
2008 : "Not able to make a connection to our domain: %s\n",
2009 : nt_errstr(result)));
2010 0 : TALLOC_FREE(mem_ctx);
2011 0 : return false;
2012 : }
2013 : }
2014 :
2015 : /* This won't work unless our domain is AD */
2016 4 : if ( !our_domain->active_directory ) {
2017 0 : TALLOC_FREE(mem_ctx);
2018 0 : return False;
2019 : }
2020 :
2021 4 : if (our_domain->internal) {
2022 0 : result = wb_open_internal_pipe(mem_ctx, &ndr_table_netlogon, &cli);
2023 4 : } else if (!connection_ok(our_domain)) {
2024 2 : DEBUG(3,("set_dc_type_and_flags_trustinfo: "
2025 : "No connection to our domain!\n"));
2026 2 : TALLOC_FREE(mem_ctx);
2027 2 : return False;
2028 : } else {
2029 2 : result = cm_connect_netlogon(our_domain, &cli);
2030 : }
2031 :
2032 2 : if (!NT_STATUS_IS_OK(result)) {
2033 0 : DEBUG(5, ("set_dc_type_and_flags_trustinfo: Could not open "
2034 : "a connection to %s for PIPE_NETLOGON (%s)\n",
2035 : domain->name, nt_errstr(result)));
2036 0 : TALLOC_FREE(mem_ctx);
2037 0 : return False;
2038 : }
2039 2 : b = cli->binding_handle;
2040 :
2041 : /* Use DsEnumerateDomainTrusts to get us the trust direction and type. */
2042 2 : result = dcerpc_netr_DsrEnumerateDomainTrusts(b, mem_ctx,
2043 2 : cli->desthost,
2044 : flags,
2045 : &trusts,
2046 : &werr);
2047 2 : if (!NT_STATUS_IS_OK(result)) {
2048 0 : DEBUG(0,("set_dc_type_and_flags_trustinfo: "
2049 : "failed to query trusted domain list: %s\n",
2050 : nt_errstr(result)));
2051 0 : TALLOC_FREE(mem_ctx);
2052 0 : return false;
2053 : }
2054 2 : if (!W_ERROR_IS_OK(werr)) {
2055 0 : DEBUG(0,("set_dc_type_and_flags_trustinfo: "
2056 : "failed to query trusted domain list: %s\n",
2057 : win_errstr(werr)));
2058 0 : TALLOC_FREE(mem_ctx);
2059 0 : return false;
2060 : }
2061 :
2062 : /* Now find the domain name and get the flags */
2063 :
2064 2 : for ( i=0; i<trusts.count; i++ ) {
2065 2 : if ( strequal( domain->name, trusts.array[i].netbios_name) ) {
2066 2 : domain->domain_flags = trusts.array[i].trust_flags;
2067 2 : domain->domain_type = trusts.array[i].trust_type;
2068 2 : domain->domain_trust_attribs = trusts.array[i].trust_attributes;
2069 :
2070 2 : if ( domain->domain_type == LSA_TRUST_TYPE_UPLEVEL )
2071 2 : domain->active_directory = True;
2072 :
2073 : /* This flag is only set if the domain is *our*
2074 : primary domain and the primary domain is in
2075 : native mode */
2076 :
2077 2 : domain->native_mode = (domain->domain_flags & NETR_TRUST_FLAG_NATIVE);
2078 :
2079 2 : DEBUG(5, ("set_dc_type_and_flags_trustinfo: domain %s is %sin "
2080 : "native mode.\n", domain->name,
2081 : domain->native_mode ? "" : "NOT "));
2082 :
2083 2 : DEBUG(5,("set_dc_type_and_flags_trustinfo: domain %s is %s"
2084 : "running active directory.\n", domain->name,
2085 : domain->active_directory ? "" : "NOT "));
2086 :
2087 2 : domain->can_do_ncacn_ip_tcp = domain->active_directory;
2088 :
2089 2 : domain->initialized = True;
2090 :
2091 2 : break;
2092 : }
2093 : }
2094 :
2095 2 : TALLOC_FREE(mem_ctx);
2096 :
2097 2 : return domain->initialized;
2098 : }
2099 :
2100 : /******************************************************************************
2101 : We can 'sense' certain things about the DC by it's replies to certain
2102 : questions.
2103 :
2104 : This tells us if this particular remote server is Active Directory, and if it
2105 : is native mode.
2106 : ******************************************************************************/
2107 :
2108 0 : static void set_dc_type_and_flags_connect( struct winbindd_domain *domain )
2109 : {
2110 : NTSTATUS status, result;
2111 : WERROR werr;
2112 0 : TALLOC_CTX *mem_ctx = NULL;
2113 0 : struct rpc_pipe_client *cli = NULL;
2114 : struct policy_handle pol;
2115 : union dssetup_DsRoleInfo info;
2116 0 : union lsa_PolicyInformation *lsa_info = NULL;
2117 :
2118 0 : if (!domain->internal && !connection_ok(domain)) {
2119 0 : return;
2120 : }
2121 :
2122 0 : mem_ctx = talloc_init("set_dc_type_and_flags on domain %s\n",
2123 : domain->name);
2124 0 : if (!mem_ctx) {
2125 0 : DEBUG(1, ("set_dc_type_and_flags_connect: talloc_init() failed\n"));
2126 0 : return;
2127 : }
2128 :
2129 0 : DEBUG(5, ("set_dc_type_and_flags_connect: domain %s\n", domain->name ));
2130 :
2131 0 : if (domain->internal) {
2132 0 : status = wb_open_internal_pipe(mem_ctx,
2133 : &ndr_table_dssetup,
2134 : &cli);
2135 : } else {
2136 0 : status = cli_rpc_pipe_open_noauth(domain->conn.cli,
2137 : &ndr_table_dssetup,
2138 : &cli);
2139 : }
2140 :
2141 0 : if (!NT_STATUS_IS_OK(status)) {
2142 0 : DEBUG(5, ("set_dc_type_and_flags_connect: Could not bind to "
2143 : "PI_DSSETUP on domain %s: (%s)\n",
2144 : domain->name, nt_errstr(status)));
2145 :
2146 : /* if this is just a non-AD domain we need to continue
2147 : * identifying so that we can in the end return with
2148 : * domain->initialized = True - gd */
2149 :
2150 0 : goto no_dssetup;
2151 : }
2152 :
2153 0 : status = dcerpc_dssetup_DsRoleGetPrimaryDomainInformation(cli->binding_handle, mem_ctx,
2154 : DS_ROLE_BASIC_INFORMATION,
2155 : &info,
2156 : &werr);
2157 0 : TALLOC_FREE(cli);
2158 :
2159 0 : if (NT_STATUS_IS_OK(status)) {
2160 0 : result = werror_to_ntstatus(werr);
2161 : }
2162 0 : if (!NT_STATUS_IS_OK(status)) {
2163 0 : DEBUG(5, ("set_dc_type_and_flags_connect: rpccli_ds_getprimarydominfo "
2164 : "on domain %s failed: (%s)\n",
2165 : domain->name, nt_errstr(status)));
2166 :
2167 : /* older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for
2168 : * every opcode on the DSSETUP pipe, continue with
2169 : * no_dssetup mode here as well to get domain->initialized
2170 : * set - gd */
2171 :
2172 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
2173 0 : goto no_dssetup;
2174 : }
2175 :
2176 0 : TALLOC_FREE(mem_ctx);
2177 0 : return;
2178 : }
2179 :
2180 0 : if ((info.basic.flags & DS_ROLE_PRIMARY_DS_RUNNING) &&
2181 0 : !(info.basic.flags & DS_ROLE_PRIMARY_DS_MIXED_MODE)) {
2182 0 : domain->native_mode = True;
2183 : } else {
2184 0 : domain->native_mode = False;
2185 : }
2186 :
2187 0 : no_dssetup:
2188 0 : if (domain->internal) {
2189 0 : status = wb_open_internal_pipe(mem_ctx,
2190 : &ndr_table_lsarpc,
2191 : &cli);
2192 : } else {
2193 0 : status = cli_rpc_pipe_open_noauth(domain->conn.cli,
2194 : &ndr_table_lsarpc, &cli);
2195 : }
2196 0 : if (!NT_STATUS_IS_OK(status)) {
2197 0 : DEBUG(5, ("set_dc_type_and_flags_connect: Could not bind to "
2198 : "PI_LSARPC on domain %s: (%s)\n",
2199 : domain->name, nt_errstr(status)));
2200 0 : TALLOC_FREE(cli);
2201 0 : TALLOC_FREE(mem_ctx);
2202 0 : return;
2203 : }
2204 :
2205 0 : status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
2206 : SEC_FLAG_MAXIMUM_ALLOWED, &pol);
2207 :
2208 0 : if (NT_STATUS_IS_OK(status)) {
2209 : /* This particular query is exactly what Win2k clients use
2210 : to determine that the DC is active directory */
2211 0 : status = dcerpc_lsa_QueryInfoPolicy2(cli->binding_handle, mem_ctx,
2212 : &pol,
2213 : LSA_POLICY_INFO_DNS,
2214 : &lsa_info,
2215 : &result);
2216 : }
2217 :
2218 0 : if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2219 0 : domain->active_directory = True;
2220 :
2221 0 : if (lsa_info->dns.name.string) {
2222 0 : if (!strequal(domain->name, lsa_info->dns.name.string))
2223 : {
2224 0 : DEBUG(1, ("set_dc_type_and_flags_connect: DC "
2225 : "for domain %s claimed it was a DC "
2226 : "for domain %s, refusing to "
2227 : "initialize\n",
2228 : domain->name,
2229 : lsa_info->dns.name.string));
2230 0 : TALLOC_FREE(cli);
2231 0 : TALLOC_FREE(mem_ctx);
2232 0 : return;
2233 : }
2234 0 : talloc_free(domain->name);
2235 0 : domain->name = talloc_strdup(domain,
2236 0 : lsa_info->dns.name.string);
2237 0 : if (domain->name == NULL) {
2238 0 : goto done;
2239 : }
2240 : }
2241 :
2242 0 : if (lsa_info->dns.dns_domain.string) {
2243 0 : if (domain->alt_name != NULL &&
2244 0 : !strequal(domain->alt_name,
2245 0 : lsa_info->dns.dns_domain.string))
2246 : {
2247 0 : DEBUG(1, ("set_dc_type_and_flags_connect: DC "
2248 : "for domain %s (%s) claimed it was "
2249 : "a DC for domain %s, refusing to "
2250 : "initialize\n",
2251 : domain->alt_name, domain->name,
2252 : lsa_info->dns.dns_domain.string));
2253 0 : TALLOC_FREE(cli);
2254 0 : TALLOC_FREE(mem_ctx);
2255 0 : return;
2256 : }
2257 0 : talloc_free(domain->alt_name);
2258 0 : domain->alt_name =
2259 0 : talloc_strdup(domain,
2260 0 : lsa_info->dns.dns_domain.string);
2261 0 : if (domain->alt_name == NULL) {
2262 0 : goto done;
2263 : }
2264 : }
2265 :
2266 : /* See if we can set some domain trust flags about
2267 : ourself */
2268 :
2269 0 : if (lsa_info->dns.dns_forest.string) {
2270 0 : talloc_free(domain->forest_name);
2271 0 : domain->forest_name =
2272 0 : talloc_strdup(domain,
2273 0 : lsa_info->dns.dns_forest.string);
2274 0 : if (domain->forest_name == NULL) {
2275 0 : goto done;
2276 : }
2277 :
2278 0 : if (strequal(domain->forest_name, domain->alt_name)) {
2279 0 : domain->domain_flags |= NETR_TRUST_FLAG_TREEROOT;
2280 : }
2281 : }
2282 :
2283 0 : if (lsa_info->dns.sid) {
2284 0 : if (!is_null_sid(&domain->sid) &&
2285 0 : !dom_sid_equal(&domain->sid,
2286 0 : lsa_info->dns.sid))
2287 : {
2288 : struct dom_sid_buf buf1, buf2;
2289 0 : DEBUG(1, ("set_dc_type_and_flags_connect: DC "
2290 : "for domain %s (%s) claimed it was "
2291 : "a DC for domain %s, refusing to "
2292 : "initialize\n",
2293 : dom_sid_str_buf(&domain->sid, &buf1),
2294 : domain->name,
2295 : dom_sid_str_buf(lsa_info->dns.sid,
2296 : &buf2)));
2297 0 : TALLOC_FREE(cli);
2298 0 : TALLOC_FREE(mem_ctx);
2299 0 : return;
2300 : }
2301 0 : sid_copy(&domain->sid, lsa_info->dns.sid);
2302 : }
2303 : } else {
2304 0 : domain->active_directory = False;
2305 :
2306 0 : status = rpccli_lsa_open_policy(cli, mem_ctx, True,
2307 : SEC_FLAG_MAXIMUM_ALLOWED,
2308 : &pol);
2309 :
2310 0 : if (!NT_STATUS_IS_OK(status)) {
2311 0 : goto done;
2312 : }
2313 :
2314 0 : status = dcerpc_lsa_QueryInfoPolicy(cli->binding_handle, mem_ctx,
2315 : &pol,
2316 : LSA_POLICY_INFO_ACCOUNT_DOMAIN,
2317 : &lsa_info,
2318 : &result);
2319 0 : if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2320 :
2321 0 : if (lsa_info->account_domain.name.string) {
2322 0 : if (!strequal(domain->name,
2323 0 : lsa_info->account_domain.name.string))
2324 : {
2325 0 : DEBUG(1,
2326 : ("set_dc_type_and_flags_connect: "
2327 : "DC for domain %s claimed it was"
2328 : " a DC for domain %s, refusing "
2329 : "to initialize\n", domain->name,
2330 : lsa_info->
2331 : account_domain.name.string));
2332 0 : TALLOC_FREE(cli);
2333 0 : TALLOC_FREE(mem_ctx);
2334 0 : return;
2335 : }
2336 0 : talloc_free(domain->name);
2337 0 : domain->name =
2338 0 : talloc_strdup(domain,
2339 0 : lsa_info->account_domain.name.string);
2340 : }
2341 :
2342 0 : if (lsa_info->account_domain.sid) {
2343 0 : if (!is_null_sid(&domain->sid) &&
2344 0 : !dom_sid_equal(&domain->sid,
2345 0 : lsa_info->account_domain.sid))
2346 : {
2347 : struct dom_sid_buf buf1, buf2;
2348 0 : DEBUG(1,
2349 : ("set_dc_type_and_flags_connect: "
2350 : "DC for domain %s (%s) claimed "
2351 : "it was a DC for domain %s, "
2352 : "refusing to initialize\n",
2353 : dom_sid_str_buf(
2354 : &domain->sid, &buf1),
2355 : domain->name,
2356 : dom_sid_str_buf(
2357 : lsa_info->account_domain.sid,
2358 : &buf2)));
2359 0 : TALLOC_FREE(cli);
2360 0 : TALLOC_FREE(mem_ctx);
2361 0 : return;
2362 : }
2363 0 : sid_copy(&domain->sid, lsa_info->account_domain.sid);
2364 : }
2365 : }
2366 : }
2367 0 : done:
2368 :
2369 0 : DEBUG(5, ("set_dc_type_and_flags_connect: domain %s is %sin native mode.\n",
2370 : domain->name, domain->native_mode ? "" : "NOT "));
2371 :
2372 0 : DEBUG(5,("set_dc_type_and_flags_connect: domain %s is %srunning active directory.\n",
2373 : domain->name, domain->active_directory ? "" : "NOT "));
2374 :
2375 0 : domain->can_do_ncacn_ip_tcp = domain->active_directory;
2376 :
2377 0 : TALLOC_FREE(cli);
2378 :
2379 0 : TALLOC_FREE(mem_ctx);
2380 :
2381 0 : domain->initialized = True;
2382 : }
2383 :
2384 : /**********************************************************************
2385 : Set the domain_flags (trust attributes, domain operating modes, etc...
2386 : ***********************************************************************/
2387 :
2388 2 : static void set_dc_type_and_flags( struct winbindd_domain *domain )
2389 : {
2390 2 : if (IS_DC) {
2391 : /*
2392 : * On a DC we loaded all trusts
2393 : * from configuration and never learn
2394 : * new domains.
2395 : */
2396 0 : return;
2397 : }
2398 :
2399 : /* we always have to contact our primary domain */
2400 :
2401 2 : if ( domain->primary || domain->internal) {
2402 0 : DEBUG(10,("set_dc_type_and_flags: setting up flags for "
2403 : "primary or internal domain\n"));
2404 0 : set_dc_type_and_flags_connect( domain );
2405 0 : return;
2406 : }
2407 :
2408 : /* Use our DC to get the information if possible */
2409 :
2410 2 : if ( !set_dc_type_and_flags_trustinfo( domain ) ) {
2411 : /* Otherwise, fallback to contacting the
2412 : domain directly */
2413 0 : set_dc_type_and_flags_connect( domain );
2414 : }
2415 :
2416 2 : return;
2417 : }
2418 :
2419 :
2420 :
2421 : /**********************************************************************
2422 : ***********************************************************************/
2423 :
2424 0 : static NTSTATUS cm_get_schannel_creds(struct winbindd_domain *domain,
2425 : struct netlogon_creds_cli_context **ppdc)
2426 : {
2427 0 : NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2428 : struct rpc_pipe_client *netlogon_pipe;
2429 :
2430 0 : *ppdc = NULL;
2431 :
2432 0 : if ((!IS_DC) && (!domain->primary)) {
2433 0 : return NT_STATUS_TRUSTED_DOMAIN_FAILURE;
2434 : }
2435 :
2436 0 : if (domain->conn.netlogon_creds_ctx != NULL) {
2437 0 : *ppdc = domain->conn.netlogon_creds_ctx;
2438 0 : return NT_STATUS_OK;
2439 : }
2440 :
2441 0 : result = cm_connect_netlogon_secure(domain, &netlogon_pipe, ppdc);
2442 0 : if (!NT_STATUS_IS_OK(result)) {
2443 0 : return result;
2444 : }
2445 :
2446 0 : return NT_STATUS_OK;
2447 : }
2448 :
2449 0 : NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
2450 : bool need_rw_dc,
2451 : struct rpc_pipe_client **cli, struct policy_handle *sam_handle)
2452 : {
2453 : struct winbindd_cm_conn *conn;
2454 : NTSTATUS status, result;
2455 : struct netlogon_creds_cli_context *p_creds;
2456 0 : struct cli_credentials *creds = NULL;
2457 0 : bool retry = false; /* allow one retry attempt for expired session */
2458 0 : const char *remote_name = NULL;
2459 0 : const struct sockaddr_storage *remote_sockaddr = NULL;
2460 0 : bool sealed_pipes = true;
2461 0 : bool strong_key = true;
2462 :
2463 0 : if (sid_check_is_our_sam(&domain->sid)) {
2464 0 : if (domain->rodc == false || need_rw_dc == false) {
2465 0 : return open_internal_samr_conn(mem_ctx, domain, cli, sam_handle);
2466 : }
2467 : }
2468 :
2469 0 : if (IS_AD_DC) {
2470 : /*
2471 : * In theory we should not use SAMR within
2472 : * winbindd at all, but that's a larger task to
2473 : * remove this and avoid breaking existing
2474 : * setups.
2475 : *
2476 : * At least as AD DC we have the restriction
2477 : * to avoid SAMR against trusted domains,
2478 : * as there're no existing setups.
2479 : */
2480 0 : return NT_STATUS_REQUEST_NOT_ACCEPTED;
2481 : }
2482 :
2483 0 : retry:
2484 0 : status = init_dc_connection_rpc(domain, need_rw_dc);
2485 0 : if (!NT_STATUS_IS_OK(status)) {
2486 0 : return status;
2487 : }
2488 :
2489 0 : conn = &domain->conn;
2490 :
2491 0 : if (rpccli_is_connected(conn->samr_pipe)) {
2492 0 : goto done;
2493 : }
2494 :
2495 0 : TALLOC_FREE(conn->samr_pipe);
2496 :
2497 : /*
2498 : * No SAMR pipe yet. Attempt to get an NTLMSSP SPNEGO authenticated
2499 : * sign and sealed pipe using the machine account password by
2500 : * preference. If we can't - try schannel, if that fails, try
2501 : * anonymous.
2502 : */
2503 :
2504 0 : result = get_trust_credentials(domain, talloc_tos(), false, &creds);
2505 0 : if (!NT_STATUS_IS_OK(result)) {
2506 0 : DEBUG(10, ("cm_connect_sam: No user available for "
2507 : "domain %s, trying schannel\n", domain->name));
2508 0 : goto schannel;
2509 : }
2510 :
2511 0 : if (cli_credentials_is_anonymous(creds)) {
2512 0 : goto anonymous;
2513 : }
2514 :
2515 0 : remote_name = smbXcli_conn_remote_name(conn->cli->conn);
2516 0 : remote_sockaddr = smbXcli_conn_remote_sockaddr(conn->cli->conn);
2517 :
2518 : /*
2519 : * We have an authenticated connection. Use a SPNEGO
2520 : * authenticated SAMR pipe with sign & seal.
2521 : */
2522 0 : status = cli_rpc_pipe_open_with_creds(conn->cli,
2523 : &ndr_table_samr,
2524 : NCACN_NP,
2525 : DCERPC_AUTH_TYPE_SPNEGO,
2526 : conn->auth_level,
2527 : remote_name,
2528 : remote_sockaddr,
2529 : creds,
2530 : &conn->samr_pipe);
2531 :
2532 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)
2533 0 : && !retry) {
2534 0 : invalidate_cm_connection(domain);
2535 0 : retry = true;
2536 0 : goto retry;
2537 : }
2538 :
2539 0 : if (!NT_STATUS_IS_OK(status)) {
2540 0 : DEBUG(10,("cm_connect_sam: failed to connect to SAMR "
2541 : "pipe for domain %s using NTLMSSP "
2542 : "authenticated pipe: user %s. Error was "
2543 : "%s\n", domain->name,
2544 : cli_credentials_get_unparsed_name(creds, talloc_tos()),
2545 : nt_errstr(status)));
2546 0 : goto schannel;
2547 : }
2548 :
2549 0 : DEBUG(10,("cm_connect_sam: connected to SAMR pipe for "
2550 : "domain %s using NTLMSSP authenticated "
2551 : "pipe: user %s\n", domain->name,
2552 : cli_credentials_get_unparsed_name(creds, talloc_tos())));
2553 :
2554 0 : status = dcerpc_samr_Connect2(conn->samr_pipe->binding_handle, mem_ctx,
2555 0 : conn->samr_pipe->desthost,
2556 : SEC_FLAG_MAXIMUM_ALLOWED,
2557 : &conn->sam_connect_handle,
2558 : &result);
2559 :
2560 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_IO_DEVICE_ERROR) && !retry) {
2561 0 : invalidate_cm_connection(domain);
2562 0 : TALLOC_FREE(conn->samr_pipe);
2563 0 : retry = true;
2564 0 : goto retry;
2565 : }
2566 :
2567 0 : if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2568 0 : goto open_domain;
2569 : }
2570 0 : if (NT_STATUS_IS_OK(status)) {
2571 0 : status = result;
2572 : }
2573 :
2574 0 : DEBUG(10,("cm_connect_sam: ntlmssp-sealed dcerpc_samr_Connect2 "
2575 : "failed for domain %s, error was %s. Trying schannel\n",
2576 : domain->name, nt_errstr(status) ));
2577 0 : TALLOC_FREE(conn->samr_pipe);
2578 :
2579 0 : schannel:
2580 :
2581 : /* Fall back to schannel if it's a W2K pre-SP1 box. */
2582 :
2583 0 : status = cm_get_schannel_creds(domain, &p_creds);
2584 0 : if (!NT_STATUS_IS_OK(status)) {
2585 : /* If this call fails - conn->cli can now be NULL ! */
2586 0 : DEBUG(10, ("cm_connect_sam: Could not get schannel auth info "
2587 : "for domain %s (error %s), trying anon\n",
2588 : domain->name,
2589 : nt_errstr(status) ));
2590 0 : goto anonymous;
2591 : }
2592 0 : TALLOC_FREE(creds);
2593 0 : status = cli_rpc_pipe_open_schannel_with_creds(
2594 : conn->cli, &ndr_table_samr, NCACN_NP, p_creds,
2595 : remote_name,
2596 : remote_sockaddr,
2597 : &conn->samr_pipe);
2598 :
2599 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)
2600 0 : && !retry) {
2601 0 : invalidate_cm_connection(domain);
2602 0 : retry = true;
2603 0 : goto retry;
2604 : }
2605 :
2606 0 : if (!NT_STATUS_IS_OK(status)) {
2607 0 : DEBUG(10,("cm_connect_sam: failed to connect to SAMR pipe for "
2608 : "domain %s using schannel. Error was %s\n",
2609 : domain->name, nt_errstr(status) ));
2610 0 : goto anonymous;
2611 : }
2612 0 : DEBUG(10,("cm_connect_sam: connected to SAMR pipe for domain %s using "
2613 : "schannel.\n", domain->name ));
2614 :
2615 0 : status = dcerpc_samr_Connect2(conn->samr_pipe->binding_handle, mem_ctx,
2616 0 : conn->samr_pipe->desthost,
2617 : SEC_FLAG_MAXIMUM_ALLOWED,
2618 : &conn->sam_connect_handle,
2619 : &result);
2620 :
2621 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_IO_DEVICE_ERROR) && !retry) {
2622 0 : invalidate_cm_connection(domain);
2623 0 : TALLOC_FREE(conn->samr_pipe);
2624 0 : retry = true;
2625 0 : goto retry;
2626 : }
2627 :
2628 0 : if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2629 0 : goto open_domain;
2630 : }
2631 0 : if (NT_STATUS_IS_OK(status)) {
2632 0 : status = result;
2633 : }
2634 0 : DEBUG(10,("cm_connect_sam: schannel-sealed dcerpc_samr_Connect2 failed "
2635 : "for domain %s, error was %s. Trying anonymous\n",
2636 : domain->name, nt_errstr(status) ));
2637 0 : TALLOC_FREE(conn->samr_pipe);
2638 :
2639 0 : anonymous:
2640 :
2641 0 : sealed_pipes = lp_winbind_sealed_pipes();
2642 0 : sealed_pipes = lp_parm_bool(-1, "winbind sealed pipes",
2643 0 : domain->name,
2644 : sealed_pipes);
2645 0 : strong_key = lp_require_strong_key();
2646 0 : strong_key = lp_parm_bool(-1, "require strong key",
2647 0 : domain->name,
2648 : strong_key);
2649 :
2650 : /* Finally fall back to anonymous. */
2651 0 : if (sealed_pipes || strong_key) {
2652 0 : status = NT_STATUS_DOWNGRADE_DETECTED;
2653 0 : DEBUG(1, ("Unwilling to make SAMR connection to domain %s "
2654 : "without connection level security, "
2655 : "must set 'winbind sealed pipes:%s = false' and "
2656 : "'require strong key:%s = false' to proceed: %s\n",
2657 : domain->name, domain->name, domain->name,
2658 : nt_errstr(status)));
2659 0 : goto done;
2660 : }
2661 0 : status = cli_rpc_pipe_open_noauth(conn->cli, &ndr_table_samr,
2662 : &conn->samr_pipe);
2663 :
2664 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)
2665 0 : && !retry) {
2666 0 : invalidate_cm_connection(domain);
2667 0 : retry = true;
2668 0 : goto retry;
2669 : }
2670 :
2671 0 : if (!NT_STATUS_IS_OK(status)) {
2672 0 : goto done;
2673 : }
2674 :
2675 0 : status = dcerpc_samr_Connect2(conn->samr_pipe->binding_handle, mem_ctx,
2676 0 : conn->samr_pipe->desthost,
2677 : SEC_FLAG_MAXIMUM_ALLOWED,
2678 : &conn->sam_connect_handle,
2679 : &result);
2680 :
2681 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_IO_DEVICE_ERROR) && !retry) {
2682 0 : invalidate_cm_connection(domain);
2683 0 : TALLOC_FREE(conn->samr_pipe);
2684 0 : retry = true;
2685 0 : goto retry;
2686 : }
2687 :
2688 0 : if (!NT_STATUS_IS_OK(status)) {
2689 0 : DEBUG(10,("cm_connect_sam: rpccli_samr_Connect2 failed "
2690 : "for domain %s Error was %s\n",
2691 : domain->name, nt_errstr(status) ));
2692 0 : goto done;
2693 : }
2694 0 : if (!NT_STATUS_IS_OK(result)) {
2695 0 : status = result;
2696 0 : DEBUG(10,("cm_connect_sam: dcerpc_samr_Connect2 failed "
2697 : "for domain %s Error was %s\n",
2698 : domain->name, nt_errstr(result)));
2699 0 : goto done;
2700 : }
2701 :
2702 0 : open_domain:
2703 0 : status = dcerpc_samr_OpenDomain(conn->samr_pipe->binding_handle,
2704 : mem_ctx,
2705 : &conn->sam_connect_handle,
2706 : SEC_FLAG_MAXIMUM_ALLOWED,
2707 : &domain->sid,
2708 : &conn->sam_domain_handle,
2709 : &result);
2710 0 : if (!NT_STATUS_IS_OK(status)) {
2711 0 : goto done;
2712 : }
2713 :
2714 0 : status = result;
2715 0 : done:
2716 :
2717 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
2718 : /*
2719 : * if we got access denied, we might just have no access rights
2720 : * to talk to the remote samr server server (e.g. when we are a
2721 : * PDC and we are connecting a w2k8 pdc via an interdomain
2722 : * trust). In that case do not invalidate the whole connection
2723 : * stack
2724 : */
2725 0 : TALLOC_FREE(conn->samr_pipe);
2726 0 : ZERO_STRUCT(conn->sam_domain_handle);
2727 0 : return status;
2728 0 : } else if (!NT_STATUS_IS_OK(status)) {
2729 0 : invalidate_cm_connection(domain);
2730 0 : return status;
2731 : }
2732 :
2733 0 : *cli = conn->samr_pipe;
2734 0 : *sam_handle = conn->sam_domain_handle;
2735 0 : return status;
2736 : }
2737 :
2738 : /**********************************************************************
2739 : open an schanneld ncacn_ip_tcp connection to LSA
2740 : ***********************************************************************/
2741 :
2742 0 : static NTSTATUS cm_connect_lsa_tcp(struct winbindd_domain *domain,
2743 : TALLOC_CTX *mem_ctx,
2744 : struct rpc_pipe_client **cli)
2745 : {
2746 : struct winbindd_cm_conn *conn;
2747 0 : struct netlogon_creds_cli_context *p_creds = NULL;
2748 : NTSTATUS status;
2749 0 : const char *remote_name = NULL;
2750 0 : const struct sockaddr_storage *remote_sockaddr = NULL;
2751 :
2752 0 : DEBUG(10,("cm_connect_lsa_tcp\n"));
2753 :
2754 0 : status = init_dc_connection_rpc(domain, false);
2755 0 : if (!NT_STATUS_IS_OK(status)) {
2756 0 : return status;
2757 : }
2758 :
2759 0 : conn = &domain->conn;
2760 :
2761 : /*
2762 : * rpccli_is_connected handles more error cases
2763 : */
2764 0 : if (rpccli_is_connected(conn->lsa_pipe_tcp) &&
2765 0 : conn->lsa_pipe_tcp->transport->transport == NCACN_IP_TCP &&
2766 0 : conn->lsa_pipe_tcp->auth->auth_level >= DCERPC_AUTH_LEVEL_INTEGRITY) {
2767 0 : goto done;
2768 : }
2769 :
2770 0 : TALLOC_FREE(conn->lsa_pipe_tcp);
2771 :
2772 0 : status = cm_get_schannel_creds(domain, &p_creds);
2773 0 : if (!NT_STATUS_IS_OK(status)) {
2774 0 : goto done;
2775 : }
2776 :
2777 0 : remote_name = smbXcli_conn_remote_name(conn->cli->conn);
2778 0 : remote_sockaddr = smbXcli_conn_remote_sockaddr(conn->cli->conn);
2779 :
2780 0 : status = cli_rpc_pipe_open_schannel_with_creds(
2781 : conn->cli,
2782 : &ndr_table_lsarpc,
2783 : NCACN_IP_TCP,
2784 : p_creds,
2785 : remote_name,
2786 : remote_sockaddr,
2787 : &conn->lsa_pipe_tcp);
2788 0 : if (!NT_STATUS_IS_OK(status)) {
2789 0 : DEBUG(10,("cli_rpc_pipe_open_schannel_with_key failed: %s\n",
2790 : nt_errstr(status)));
2791 0 : goto done;
2792 : }
2793 :
2794 0 : done:
2795 0 : if (!NT_STATUS_IS_OK(status)) {
2796 0 : TALLOC_FREE(conn->lsa_pipe_tcp);
2797 0 : return status;
2798 : }
2799 :
2800 0 : *cli = conn->lsa_pipe_tcp;
2801 :
2802 0 : return status;
2803 : }
2804 :
2805 0 : NTSTATUS cm_connect_lsa(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
2806 : struct rpc_pipe_client **cli, struct policy_handle *lsa_policy)
2807 : {
2808 : struct winbindd_cm_conn *conn;
2809 0 : NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2810 : struct netlogon_creds_cli_context *p_creds;
2811 0 : struct cli_credentials *creds = NULL;
2812 0 : bool retry = false; /* allow one retry attempt for expired session */
2813 0 : const char *remote_name = NULL;
2814 0 : const struct sockaddr_storage *remote_sockaddr = NULL;
2815 0 : bool sealed_pipes = true;
2816 0 : bool strong_key = true;
2817 :
2818 0 : retry:
2819 0 : result = init_dc_connection_rpc(domain, false);
2820 0 : if (!NT_STATUS_IS_OK(result))
2821 0 : return result;
2822 :
2823 0 : conn = &domain->conn;
2824 :
2825 0 : if (rpccli_is_connected(conn->lsa_pipe)) {
2826 0 : goto done;
2827 : }
2828 :
2829 0 : TALLOC_FREE(conn->lsa_pipe);
2830 :
2831 0 : if (IS_AD_DC) {
2832 : /*
2833 : * Make sure we only use schannel as AD DC.
2834 : */
2835 0 : goto schannel;
2836 : }
2837 :
2838 0 : result = get_trust_credentials(domain, talloc_tos(), false, &creds);
2839 0 : if (!NT_STATUS_IS_OK(result)) {
2840 0 : DEBUG(10, ("cm_connect_lsa: No user available for "
2841 : "domain %s, trying schannel\n", domain->name));
2842 0 : goto schannel;
2843 : }
2844 :
2845 0 : if (cli_credentials_is_anonymous(creds)) {
2846 0 : goto anonymous;
2847 : }
2848 :
2849 0 : remote_name = smbXcli_conn_remote_name(conn->cli->conn);
2850 0 : remote_sockaddr = smbXcli_conn_remote_sockaddr(conn->cli->conn);
2851 :
2852 : /*
2853 : * We have an authenticated connection. Use a SPNEGO
2854 : * authenticated LSA pipe with sign & seal.
2855 : */
2856 0 : result = cli_rpc_pipe_open_with_creds
2857 : (conn->cli, &ndr_table_lsarpc, NCACN_NP,
2858 : DCERPC_AUTH_TYPE_SPNEGO,
2859 : conn->auth_level,
2860 : remote_name,
2861 : remote_sockaddr,
2862 : creds,
2863 : &conn->lsa_pipe);
2864 :
2865 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_NETWORK_SESSION_EXPIRED)
2866 0 : && !retry) {
2867 0 : invalidate_cm_connection(domain);
2868 0 : retry = true;
2869 0 : goto retry;
2870 : }
2871 :
2872 0 : if (!NT_STATUS_IS_OK(result)) {
2873 0 : DEBUG(10,("cm_connect_lsa: failed to connect to LSA pipe for "
2874 : "domain %s using NTLMSSP authenticated pipe: user "
2875 : "%s. Error was %s. Trying schannel.\n",
2876 : domain->name,
2877 : cli_credentials_get_unparsed_name(creds, talloc_tos()),
2878 : nt_errstr(result)));
2879 0 : goto schannel;
2880 : }
2881 :
2882 0 : DEBUG(10,("cm_connect_lsa: connected to LSA pipe for domain %s using "
2883 : "NTLMSSP authenticated pipe: user %s\n",
2884 : domain->name, cli_credentials_get_unparsed_name(creds, talloc_tos())));
2885 :
2886 0 : result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
2887 : SEC_FLAG_MAXIMUM_ALLOWED,
2888 : &conn->lsa_policy);
2889 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_IO_DEVICE_ERROR) && !retry) {
2890 0 : invalidate_cm_connection(domain);
2891 0 : TALLOC_FREE(conn->lsa_pipe);
2892 0 : retry = true;
2893 0 : goto retry;
2894 : }
2895 :
2896 0 : if (NT_STATUS_IS_OK(result)) {
2897 0 : goto done;
2898 : }
2899 :
2900 0 : DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
2901 : "schannel\n"));
2902 :
2903 0 : TALLOC_FREE(conn->lsa_pipe);
2904 :
2905 0 : schannel:
2906 :
2907 : /* Fall back to schannel if it's a W2K pre-SP1 box. */
2908 :
2909 0 : result = cm_get_schannel_creds(domain, &p_creds);
2910 0 : if (!NT_STATUS_IS_OK(result)) {
2911 : /* If this call fails - conn->cli can now be NULL ! */
2912 0 : DEBUG(10, ("cm_connect_lsa: Could not get schannel auth info "
2913 : "for domain %s (error %s), trying anon\n",
2914 : domain->name,
2915 : nt_errstr(result) ));
2916 0 : goto anonymous;
2917 : }
2918 :
2919 0 : TALLOC_FREE(creds);
2920 0 : result = cli_rpc_pipe_open_schannel_with_creds(
2921 : conn->cli, &ndr_table_lsarpc, NCACN_NP, p_creds,
2922 : remote_name,
2923 : remote_sockaddr,
2924 : &conn->lsa_pipe);
2925 :
2926 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_NETWORK_SESSION_EXPIRED)
2927 0 : && !retry) {
2928 0 : invalidate_cm_connection(domain);
2929 0 : retry = true;
2930 0 : goto retry;
2931 : }
2932 :
2933 0 : if (!NT_STATUS_IS_OK(result)) {
2934 0 : DEBUG(10,("cm_connect_lsa: failed to connect to LSA pipe for "
2935 : "domain %s using schannel. Error was %s\n",
2936 : domain->name, nt_errstr(result) ));
2937 0 : goto anonymous;
2938 : }
2939 0 : DEBUG(10,("cm_connect_lsa: connected to LSA pipe for domain %s using "
2940 : "schannel.\n", domain->name ));
2941 :
2942 0 : result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
2943 : SEC_FLAG_MAXIMUM_ALLOWED,
2944 : &conn->lsa_policy);
2945 :
2946 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_IO_DEVICE_ERROR) && !retry) {
2947 0 : invalidate_cm_connection(domain);
2948 0 : TALLOC_FREE(conn->lsa_pipe);
2949 0 : retry = true;
2950 0 : goto retry;
2951 : }
2952 :
2953 0 : if (NT_STATUS_IS_OK(result)) {
2954 0 : goto done;
2955 : }
2956 :
2957 0 : if (IS_AD_DC) {
2958 : /*
2959 : * Make sure we only use schannel as AD DC.
2960 : */
2961 0 : goto done;
2962 : }
2963 :
2964 0 : DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
2965 : "anonymous\n"));
2966 :
2967 0 : TALLOC_FREE(conn->lsa_pipe);
2968 :
2969 0 : anonymous:
2970 :
2971 0 : if (IS_AD_DC) {
2972 : /*
2973 : * Make sure we only use schannel as AD DC.
2974 : */
2975 0 : goto done;
2976 : }
2977 :
2978 0 : sealed_pipes = lp_winbind_sealed_pipes();
2979 0 : sealed_pipes = lp_parm_bool(-1, "winbind sealed pipes",
2980 0 : domain->name,
2981 : sealed_pipes);
2982 0 : strong_key = lp_require_strong_key();
2983 0 : strong_key = lp_parm_bool(-1, "require strong key",
2984 0 : domain->name,
2985 : strong_key);
2986 :
2987 : /* Finally fall back to anonymous. */
2988 0 : if (sealed_pipes || strong_key) {
2989 0 : result = NT_STATUS_DOWNGRADE_DETECTED;
2990 0 : DEBUG(1, ("Unwilling to make LSA connection to domain %s "
2991 : "without connection level security, "
2992 : "must set 'winbind sealed pipes:%s = false' and "
2993 : "'require strong key:%s = false' to proceed: %s\n",
2994 : domain->name, domain->name, domain->name,
2995 : nt_errstr(result)));
2996 0 : goto done;
2997 : }
2998 :
2999 0 : result = cli_rpc_pipe_open_noauth(conn->cli,
3000 : &ndr_table_lsarpc,
3001 : &conn->lsa_pipe);
3002 :
3003 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_NETWORK_SESSION_EXPIRED)
3004 0 : && !retry) {
3005 0 : invalidate_cm_connection(domain);
3006 0 : retry = true;
3007 0 : goto retry;
3008 : }
3009 :
3010 0 : if (!NT_STATUS_IS_OK(result)) {
3011 0 : goto done;
3012 : }
3013 :
3014 0 : result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
3015 : SEC_FLAG_MAXIMUM_ALLOWED,
3016 : &conn->lsa_policy);
3017 :
3018 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_IO_DEVICE_ERROR) && !retry) {
3019 0 : invalidate_cm_connection(domain);
3020 0 : TALLOC_FREE(conn->lsa_pipe);
3021 0 : retry = true;
3022 0 : goto retry;
3023 : }
3024 :
3025 0 : done:
3026 0 : if (!NT_STATUS_IS_OK(result)) {
3027 0 : invalidate_cm_connection(domain);
3028 0 : return result;
3029 : }
3030 :
3031 0 : *cli = conn->lsa_pipe;
3032 0 : *lsa_policy = conn->lsa_policy;
3033 0 : return result;
3034 : }
3035 :
3036 : /****************************************************************************
3037 : Open a LSA connection to a DC, suiteable for LSA lookup calls.
3038 : ****************************************************************************/
3039 :
3040 0 : NTSTATUS cm_connect_lsat(struct winbindd_domain *domain,
3041 : TALLOC_CTX *mem_ctx,
3042 : struct rpc_pipe_client **cli,
3043 : struct policy_handle *lsa_policy)
3044 : {
3045 : NTSTATUS status;
3046 :
3047 0 : if (domain->can_do_ncacn_ip_tcp) {
3048 0 : status = cm_connect_lsa_tcp(domain, mem_ctx, cli);
3049 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
3050 0 : NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR) ||
3051 0 : NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_ACCESS_DENIED)) {
3052 0 : invalidate_cm_connection(domain);
3053 0 : status = cm_connect_lsa_tcp(domain, mem_ctx, cli);
3054 : }
3055 0 : if (NT_STATUS_IS_OK(status)) {
3056 0 : return status;
3057 : }
3058 :
3059 : /*
3060 : * we tried twice to connect via ncan_ip_tcp and schannel and
3061 : * failed - maybe it is a trusted domain we can't connect to ?
3062 : * do not try tcp next time - gd
3063 : *
3064 : * This also prevents NETLOGON over TCP
3065 : */
3066 0 : domain->can_do_ncacn_ip_tcp = false;
3067 : }
3068 :
3069 0 : status = cm_connect_lsa(domain, mem_ctx, cli, lsa_policy);
3070 :
3071 0 : return status;
3072 : }
3073 :
3074 : /****************************************************************************
3075 : Open the netlogon pipe to this DC.
3076 : ****************************************************************************/
3077 :
3078 4 : static NTSTATUS cm_connect_netlogon_transport(struct winbindd_domain *domain,
3079 : enum dcerpc_transport_t transport,
3080 : struct rpc_pipe_client **cli)
3081 : {
3082 4 : struct messaging_context *msg_ctx = global_messaging_context();
3083 : struct winbindd_cm_conn *conn;
3084 : NTSTATUS result;
3085 : enum netr_SchannelType sec_chan_type;
3086 4 : struct cli_credentials *creds = NULL;
3087 :
3088 4 : *cli = NULL;
3089 :
3090 4 : if (IS_AD_DC) {
3091 0 : if (domain->secure_channel_type == SEC_CHAN_NULL) {
3092 : /*
3093 : * Make sure we don't even try to
3094 : * connect to a foreign domain
3095 : * without a direct outbound trust.
3096 : */
3097 0 : return NT_STATUS_NO_TRUST_LSA_SECRET;
3098 : }
3099 : }
3100 :
3101 4 : result = init_dc_connection_rpc(domain, domain->rodc);
3102 4 : if (!NT_STATUS_IS_OK(result)) {
3103 0 : return result;
3104 : }
3105 :
3106 4 : conn = &domain->conn;
3107 :
3108 4 : if (rpccli_is_connected(conn->netlogon_pipe)) {
3109 2 : *cli = conn->netlogon_pipe;
3110 2 : return NT_STATUS_OK;
3111 : }
3112 :
3113 2 : TALLOC_FREE(conn->netlogon_pipe);
3114 2 : TALLOC_FREE(conn->netlogon_creds_ctx);
3115 :
3116 2 : result = get_trust_credentials(domain, talloc_tos(), true, &creds);
3117 2 : if (!NT_STATUS_IS_OK(result)) {
3118 0 : DBG_DEBUG("No user available for domain %s when trying "
3119 : "schannel\n", domain->name);
3120 0 : return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3121 : }
3122 :
3123 2 : if (cli_credentials_is_anonymous(creds)) {
3124 0 : DBG_WARNING("get_trust_credential only gave anonymous for %s, "
3125 : "unable to make get NETLOGON credentials\n",
3126 : domain->name);
3127 0 : return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3128 : }
3129 :
3130 2 : sec_chan_type = cli_credentials_get_secure_channel_type(creds);
3131 2 : if (sec_chan_type == SEC_CHAN_NULL) {
3132 0 : const char *remote_name =
3133 0 : smbXcli_conn_remote_name(conn->cli->conn);
3134 0 : const struct sockaddr_storage *remote_sockaddr =
3135 0 : smbXcli_conn_remote_sockaddr(conn->cli->conn);
3136 :
3137 0 : if (transport == NCACN_IP_TCP) {
3138 0 : DBG_NOTICE("get_secure_channel_type gave SEC_CHAN_NULL "
3139 : "for %s, deny NCACN_IP_TCP and let the "
3140 : "caller fallback to NCACN_NP.\n",
3141 : domain->name);
3142 0 : return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3143 : }
3144 :
3145 0 : DBG_NOTICE("get_secure_channel_type gave SEC_CHAN_NULL for %s, "
3146 : "fallback to noauth on NCACN_NP.\n",
3147 : domain->name);
3148 :
3149 0 : result = cli_rpc_pipe_open_noauth_transport(
3150 : conn->cli,
3151 : transport,
3152 : &ndr_table_netlogon,
3153 : remote_name,
3154 : remote_sockaddr,
3155 : &conn->netlogon_pipe);
3156 0 : if (!NT_STATUS_IS_OK(result)) {
3157 0 : invalidate_cm_connection(domain);
3158 0 : return result;
3159 : }
3160 :
3161 0 : *cli = conn->netlogon_pipe;
3162 0 : return NT_STATUS_OK;
3163 : }
3164 :
3165 3 : result = rpccli_create_netlogon_creds_ctx(creds,
3166 2 : domain->dcname,
3167 : msg_ctx,
3168 : domain,
3169 : &conn->netlogon_creds_ctx);
3170 2 : if (!NT_STATUS_IS_OK(result)) {
3171 0 : DEBUG(1, ("rpccli_create_netlogon_creds failed for %s, "
3172 : "unable to create NETLOGON credentials: %s\n",
3173 : domain->name, nt_errstr(result)));
3174 0 : return result;
3175 : }
3176 :
3177 3 : result = rpccli_connect_netlogon(
3178 : conn->cli, transport,
3179 2 : conn->netlogon_creds_ctx, conn->netlogon_force_reauth, creds,
3180 : &conn->netlogon_pipe);
3181 2 : conn->netlogon_force_reauth = false;
3182 2 : if (!NT_STATUS_IS_OK(result)) {
3183 0 : DBG_DEBUG("rpccli_connect_netlogon failed: %s\n",
3184 : nt_errstr(result));
3185 0 : return result;
3186 : }
3187 :
3188 2 : *cli = conn->netlogon_pipe;
3189 2 : return NT_STATUS_OK;
3190 : }
3191 :
3192 : /****************************************************************************
3193 : Open a NETLOGON connection to a DC, suiteable for SamLogon calls.
3194 : ****************************************************************************/
3195 :
3196 4 : NTSTATUS cm_connect_netlogon(struct winbindd_domain *domain,
3197 : struct rpc_pipe_client **cli)
3198 : {
3199 : NTSTATUS status;
3200 :
3201 4 : status = init_dc_connection_rpc(domain, domain->rodc);
3202 4 : if (!NT_STATUS_IS_OK(status)) {
3203 0 : return status;
3204 : }
3205 :
3206 4 : if (domain->active_directory && domain->can_do_ncacn_ip_tcp) {
3207 4 : status = cm_connect_netlogon_transport(domain, NCACN_IP_TCP, cli);
3208 6 : if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
3209 6 : NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR) ||
3210 4 : NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_ACCESS_DENIED)) {
3211 0 : invalidate_cm_connection(domain);
3212 0 : status = cm_connect_netlogon_transport(domain, NCACN_IP_TCP, cli);
3213 : }
3214 4 : if (NT_STATUS_IS_OK(status)) {
3215 4 : return status;
3216 : }
3217 :
3218 : /*
3219 : * we tried twice to connect via ncan_ip_tcp and schannel and
3220 : * failed - maybe it is a trusted domain we can't connect to ?
3221 : * do not try tcp next time - gd
3222 : *
3223 : * This also prevents LSA over TCP
3224 : */
3225 0 : domain->can_do_ncacn_ip_tcp = false;
3226 : }
3227 :
3228 0 : status = cm_connect_netlogon_transport(domain, NCACN_NP, cli);
3229 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
3230 : /*
3231 : * SMB2 session expired, needs reauthentication. Drop
3232 : * connection and retry.
3233 : */
3234 0 : invalidate_cm_connection(domain);
3235 0 : status = cm_connect_netlogon_transport(domain, NCACN_NP, cli);
3236 : }
3237 :
3238 0 : return status;
3239 : }
3240 :
3241 0 : NTSTATUS cm_connect_netlogon_secure(struct winbindd_domain *domain,
3242 : struct rpc_pipe_client **cli,
3243 : struct netlogon_creds_cli_context **ppdc)
3244 : {
3245 : NTSTATUS status;
3246 :
3247 0 : if (domain->secure_channel_type == SEC_CHAN_NULL) {
3248 0 : return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3249 : }
3250 :
3251 0 : status = cm_connect_netlogon(domain, cli);
3252 0 : if (!NT_STATUS_IS_OK(status)) {
3253 0 : return status;
3254 : }
3255 :
3256 0 : if (domain->conn.netlogon_creds_ctx == NULL) {
3257 0 : return NT_STATUS_TRUSTED_DOMAIN_FAILURE;
3258 : }
3259 :
3260 0 : *ppdc = domain->conn.netlogon_creds_ctx;
3261 0 : return NT_STATUS_OK;
3262 : }
3263 :
3264 0 : void winbind_msg_ip_dropped(struct messaging_context *msg_ctx,
3265 : void *private_data,
3266 : uint32_t msg_type,
3267 : struct server_id server_id,
3268 : DATA_BLOB *data)
3269 : {
3270 : struct winbindd_domain *domain;
3271 0 : char *freeit = NULL;
3272 : char *addr;
3273 :
3274 0 : if ((data == NULL)
3275 0 : || (data->data == NULL)
3276 0 : || (data->length == 0)
3277 0 : || (data->data[data->length-1] != '\0')) {
3278 0 : DEBUG(1, ("invalid msg_ip_dropped message: not a valid "
3279 : "string\n"));
3280 0 : return;
3281 : }
3282 :
3283 0 : addr = (char *)data->data;
3284 0 : DEBUG(10, ("IP %s dropped\n", addr));
3285 :
3286 0 : if (!is_ipaddress(addr)) {
3287 : char *slash;
3288 : /*
3289 : * Some code sends us ip addresses with the /netmask
3290 : * suffix
3291 : */
3292 0 : slash = strchr(addr, '/');
3293 0 : if (slash == NULL) {
3294 0 : DEBUG(1, ("invalid msg_ip_dropped message: %s",
3295 : addr));
3296 0 : return;
3297 : }
3298 0 : freeit = talloc_strndup(talloc_tos(), addr, slash-addr);
3299 0 : if (freeit == NULL) {
3300 0 : DEBUG(1, ("talloc failed\n"));
3301 0 : return;
3302 : }
3303 0 : addr = freeit;
3304 0 : DEBUG(10, ("Stripped /netmask to IP %s\n", addr));
3305 : }
3306 :
3307 0 : for (domain = domain_list(); domain != NULL; domain = domain->next) {
3308 : char sockaddr[INET6_ADDRSTRLEN];
3309 :
3310 0 : if (!cli_state_is_connected(domain->conn.cli)) {
3311 0 : continue;
3312 : }
3313 :
3314 0 : print_sockaddr(sockaddr, sizeof(sockaddr),
3315 0 : smbXcli_conn_local_sockaddr(domain->conn.cli->conn));
3316 :
3317 0 : if (strequal(sockaddr, addr)) {
3318 0 : smbXcli_conn_disconnect(domain->conn.cli->conn, NT_STATUS_OK);
3319 : }
3320 : }
3321 0 : TALLOC_FREE(freeit);
3322 : }
3323 :
3324 0 : void winbind_msg_disconnect_dc(struct messaging_context *msg_ctx,
3325 : void *private_data,
3326 : uint32_t msg_type,
3327 : struct server_id server_id,
3328 : DATA_BLOB *data)
3329 : {
3330 : struct winbindd_domain *domain;
3331 :
3332 0 : for (domain = domain_list(); domain; domain = domain->next) {
3333 0 : if (domain->internal) {
3334 0 : continue;
3335 : }
3336 0 : invalidate_cm_connection(domain);
3337 : }
3338 0 : }
|