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 136 : 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 136 : const char *domain_name = (const char *)data->data;
116 0 : struct winbindd_domain *domain;
117 :
118 136 : domain = find_domain_from_name_noinit(domain_name);
119 136 : if (domain == NULL) {
120 0 : DBG_DEBUG("Domain %s not found!\n", domain_name);
121 0 : return;
122 : }
123 :
124 136 : DBG_DEBUG("Domain %s was %s, change to offline now.\n",
125 : domain_name,
126 : domain->online ? "online" : "offline");
127 :
128 136 : domain->online = false;
129 : }
130 :
131 47 : 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 47 : const char *domain_name = (const char *)data->data;
138 0 : struct winbindd_domain *domain;
139 :
140 47 : domain = find_domain_from_name_noinit(domain_name);
141 47 : if (domain == NULL) {
142 0 : return;
143 : }
144 :
145 47 : SMB_ASSERT(wb_child_domain() == NULL);
146 :
147 47 : DBG_DEBUG("Domain %s was %s, marking as online now!\n",
148 : domain_name,
149 : domain->online ? "online" : "offline");
150 :
151 47 : 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 4 : 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 2 : 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 0 : 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 0 : 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 0 : 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 0 : NTSTATUS result;
486 0 : WERROR werr;
487 0 : TALLOC_CTX *mem_ctx;
488 0 : unsigned int orig_timeout;
489 2 : const char *tmp = NULL;
490 0 : const char *p;
491 0 : 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 2 : 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 4 : 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 4 : 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 0 : struct cli_credentials *creds;
617 0 : 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 0 : struct named_mutex *mutex;
697 :
698 4 : NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
699 0 : 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 : close(sockfd);
712 0 : return NT_STATUS_NO_TRUST_LSA_SECRET;
713 : }
714 :
715 : /*
716 : * As AD DC we only use netlogon and lsa
717 : * using schannel over an anonymous transport
718 : * (ncacn_ip_tcp or ncacn_np).
719 : *
720 : * Currently we always establish the SMB connection,
721 : * even if we don't use it, because we later use ncacn_ip_tcp.
722 : *
723 : * As we won't use the SMB connection there's no
724 : * need to try kerberos. And NT4 domains expect
725 : * an anonymous IPC$ connection anyway.
726 : */
727 0 : smb_sign_client_connections = SMB_SIGNING_OFF;
728 : }
729 :
730 4 : if (smb_sign_client_connections == SMB_SIGNING_DEFAULT) {
731 : /*
732 : * If we are connecting to our own AD domain, require
733 : * smb signing to disrupt MITM attacks
734 : */
735 0 : if (domain->primary && lp_security() == SEC_ADS) {
736 0 : smb_sign_client_connections = SMB_SIGNING_REQUIRED;
737 : /*
738 : * If we are in or are an AD domain and connecting to another
739 : * AD domain in our forest
740 : * then require smb signing to disrupt MITM attacks
741 : */
742 0 : } else if ((lp_security() == SEC_ADS)
743 0 : && domain->active_directory
744 0 : && (domain->domain_trust_attribs
745 0 : & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST)) {
746 0 : smb_sign_client_connections = SMB_SIGNING_REQUIRED;
747 : }
748 : }
749 :
750 4 : DEBUG(10,("cm_prepare_connection: connecting to DC %s for domain %s\n",
751 : controller, domain->name ));
752 :
753 4 : *retry = True;
754 :
755 4 : mutex = grab_named_mutex(talloc_tos(), controller,
756 : WINBIND_SERVER_MUTEX_WAIT_TIME);
757 4 : if (mutex == NULL) {
758 0 : close(sockfd);
759 0 : DEBUG(0,("cm_prepare_connection: mutex grab failed for %s\n",
760 : controller));
761 0 : result = NT_STATUS_POSSIBLE_DEADLOCK;
762 0 : goto done;
763 : }
764 :
765 : /*
766 : * cm_prepare_connection() is responsible that sockfd does not leak.
767 : * Once cli_state_create() returns with success, the
768 : * smbXcli_conn_destructor() makes sure that close(sockfd) is finally
769 : * called. Till that, close(sockfd) must be called on every unsuccessful
770 : * return.
771 : */
772 4 : *cli = cli_state_create(NULL, sockfd, controller,
773 : smb_sign_client_connections, flags);
774 4 : if (*cli == NULL) {
775 0 : close(sockfd);
776 0 : DEBUG(1, ("Could not cli_initialize\n"));
777 0 : result = NT_STATUS_NO_MEMORY;
778 0 : goto done;
779 : }
780 :
781 4 : cli_set_timeout(*cli, 10000); /* 10 seconds */
782 :
783 4 : set_socket_options(sockfd, lp_socket_options());
784 :
785 4 : result = smbXcli_negprot((*cli)->conn,
786 4 : (*cli)->timeout,
787 4 : lp_client_ipc_min_protocol(),
788 4 : lp_client_ipc_max_protocol(),
789 : NULL,
790 : NULL,
791 : NULL);
792 :
793 4 : if (!NT_STATUS_IS_OK(result)) {
794 0 : DEBUG(1, ("cli_negprot failed: %s\n", nt_errstr(result)));
795 0 : goto done;
796 : }
797 :
798 8 : if (smbXcli_conn_protocol((*cli)->conn) >= PROTOCOL_NT1 &&
799 4 : smb1cli_conn_capabilities((*cli)->conn) & CAP_EXTENDED_SECURITY) {
800 4 : try_ipc_auth = true;
801 0 : } else if (smbXcli_conn_protocol((*cli)->conn) >= PROTOCOL_SMB2_02) {
802 0 : try_ipc_auth = true;
803 0 : } else if (smb_sign_client_connections == SMB_SIGNING_REQUIRED) {
804 : /*
805 : * If we are forcing on SMB signing, then we must
806 : * require authentication unless this is a one-way
807 : * trust, and we have no stored user/password
808 : */
809 0 : try_ipc_auth = true;
810 : }
811 :
812 4 : if (IS_AD_DC) {
813 : /*
814 : * As AD DC we only use netlogon and lsa
815 : * using schannel over an anonymous transport
816 : * (ncacn_ip_tcp or ncacn_np).
817 : *
818 : * Currently we always establish the SMB connection,
819 : * even if we don't use it, because we later use ncacn_ip_tcp.
820 : *
821 : * As we won't use the SMB connection there's no
822 : * need to try kerberos. And NT4 domains expect
823 : * an anonymous IPC$ connection anyway.
824 : */
825 0 : try_ipc_auth = false;
826 : }
827 :
828 4 : if (try_ipc_auth) {
829 4 : result = get_trust_credentials(domain, talloc_tos(), false, &creds);
830 4 : if (!NT_STATUS_IS_OK(result)) {
831 0 : DEBUG(1, ("get_trust_credentials(%s) failed: %s\n",
832 : domain->name, nt_errstr(result)));
833 0 : goto done;
834 : }
835 : } else {
836 : /*
837 : * Without SPNEGO or NTLMSSP (perhaps via SMB2) we
838 : * would try and authentication with our machine
839 : * account password and fail. This is very rare in
840 : * the modern world however
841 : */
842 0 : creds = cli_credentials_init_anon(talloc_tos());
843 0 : if (creds == NULL) {
844 0 : result = NT_STATUS_NO_MEMORY;
845 0 : DEBUG(1, ("cli_credentials_init_anon(%s) failed: %s\n",
846 : domain->name, nt_errstr(result)));
847 0 : goto done;
848 : }
849 : }
850 :
851 4 : machine_principal = cli_credentials_get_principal(creds,
852 : talloc_tos());
853 4 : machine_realm = cli_credentials_get_realm(creds);
854 4 : machine_account = cli_credentials_get_username(creds);
855 4 : machine_domain = cli_credentials_get_domain(creds);
856 :
857 4 : DEBUG(5, ("connecting to %s (%s, %s) with account [%s\\%s] principal "
858 : "[%s] and realm [%s]\n",
859 : controller, domain->name, domain->alt_name,
860 : machine_domain, machine_account,
861 : machine_principal, machine_realm));
862 :
863 4 : if (cli_credentials_is_anonymous(creds)) {
864 0 : goto anon_fallback;
865 : }
866 :
867 4 : winbindd_set_locator_kdc_envs(domain);
868 :
869 4 : result = cli_session_setup_creds(*cli, creds);
870 4 : if (NT_STATUS_IS_OK(result)) {
871 4 : goto session_setup_done;
872 : }
873 :
874 0 : DEBUG(1, ("authenticated session setup to %s using %s failed with %s\n",
875 : controller,
876 : cli_credentials_get_unparsed_name(creds, talloc_tos()),
877 : nt_errstr(result)));
878 :
879 : /*
880 : * If we are not going to validate the connection
881 : * with SMB signing, then allow us to fall back to
882 : * anonymous
883 : */
884 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)
885 0 : || NT_STATUS_EQUAL(result, NT_STATUS_TRUSTED_DOMAIN_FAILURE)
886 0 : || NT_STATUS_EQUAL(result, NT_STATUS_INVALID_ACCOUNT_NAME)
887 0 : || NT_STATUS_EQUAL(result, NT_STATUS_INVALID_COMPUTER_NAME)
888 0 : || NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_DOMAIN)
889 0 : || NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS)
890 0 : || NT_STATUS_EQUAL(result, NT_STATUS_LOGON_FAILURE))
891 : {
892 0 : if (!cm_is_ipc_credentials(creds)) {
893 0 : goto ipc_fallback;
894 : }
895 :
896 0 : if (smb_sign_client_connections == SMB_SIGNING_REQUIRED) {
897 0 : goto done;
898 : }
899 :
900 0 : goto anon_fallback;
901 : }
902 :
903 0 : goto done;
904 :
905 0 : ipc_fallback:
906 0 : TALLOC_FREE(creds);
907 0 : tmp_status = cm_get_ipc_credentials(talloc_tos(), &creds);
908 0 : if (!NT_STATUS_IS_OK(tmp_status)) {
909 0 : result = tmp_status;
910 0 : goto done;
911 : }
912 :
913 0 : if (cli_credentials_is_anonymous(creds)) {
914 0 : goto anon_fallback;
915 : }
916 :
917 0 : machine_account = cli_credentials_get_username(creds);
918 0 : machine_domain = cli_credentials_get_domain(creds);
919 :
920 0 : DEBUG(5, ("connecting to %s from %s using NTLMSSP with username "
921 : "[%s]\\[%s]\n", controller, lp_netbios_name(),
922 : machine_domain, machine_account));
923 :
924 0 : result = cli_session_setup_creds(*cli, creds);
925 0 : if (NT_STATUS_IS_OK(result)) {
926 0 : goto session_setup_done;
927 : }
928 :
929 0 : DEBUG(1, ("authenticated session setup to %s using %s failed with %s\n",
930 : controller,
931 : cli_credentials_get_unparsed_name(creds, talloc_tos()),
932 : nt_errstr(result)));
933 :
934 : /*
935 : * If we are not going to validate the connection
936 : * with SMB signing, then allow us to fall back to
937 : * anonymous
938 : */
939 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)
940 0 : || NT_STATUS_EQUAL(result, NT_STATUS_TRUSTED_DOMAIN_FAILURE)
941 0 : || NT_STATUS_EQUAL(result, NT_STATUS_INVALID_ACCOUNT_NAME)
942 0 : || NT_STATUS_EQUAL(result, NT_STATUS_INVALID_COMPUTER_NAME)
943 0 : || NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_DOMAIN)
944 0 : || NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS)
945 0 : || NT_STATUS_EQUAL(result, NT_STATUS_LOGON_FAILURE))
946 : {
947 0 : goto anon_fallback;
948 : }
949 :
950 0 : goto done;
951 :
952 0 : anon_fallback:
953 0 : TALLOC_FREE(creds);
954 :
955 0 : if (smb_sign_client_connections == SMB_SIGNING_REQUIRED) {
956 0 : goto done;
957 : }
958 :
959 : /* Fall back to anonymous connection, this might fail later */
960 0 : DEBUG(5,("cm_prepare_connection: falling back to anonymous "
961 : "connection for DC %s\n",
962 : controller ));
963 :
964 0 : result = cli_session_setup_anon(*cli);
965 0 : if (NT_STATUS_IS_OK(result)) {
966 0 : DEBUG(5, ("Connected anonymously\n"));
967 0 : goto session_setup_done;
968 : }
969 :
970 0 : DEBUG(1, ("anonymous session setup to %s failed with %s\n",
971 : controller, nt_errstr(result)));
972 :
973 : /* We can't session setup */
974 0 : goto done;
975 :
976 4 : session_setup_done:
977 4 : TALLOC_FREE(creds);
978 :
979 : /*
980 : * This should be a short term hack until
981 : * dynamic re-authentication is implemented.
982 : *
983 : * See Bug 9175 - winbindd doesn't recover from
984 : * NT_STATUS_NETWORK_SESSION_EXPIRED
985 : */
986 4 : if (smbXcli_conn_protocol((*cli)->conn) >= PROTOCOL_SMB2_02) {
987 4 : smbXcli_session_set_disconnect_expired((*cli)->smb2.session);
988 : }
989 :
990 4 : result = cli_tree_connect(*cli, "IPC$", "IPC", NULL);
991 4 : if (!NT_STATUS_IS_OK(result)) {
992 0 : DEBUG(1,("failed tcon_X with %s\n", nt_errstr(result)));
993 0 : goto done;
994 : }
995 4 : tcon_status = result;
996 :
997 : /* cache the server name for later connections */
998 :
999 4 : saf_store(domain->name, controller);
1000 4 : if (domain->alt_name) {
1001 4 : saf_store(domain->alt_name, controller);
1002 : }
1003 :
1004 4 : winbindd_set_locator_kdc_envs(domain);
1005 :
1006 4 : TALLOC_FREE(mutex);
1007 4 : *retry = False;
1008 :
1009 4 : result = NT_STATUS_OK;
1010 :
1011 4 : done:
1012 4 : TALLOC_FREE(mutex);
1013 4 : TALLOC_FREE(creds);
1014 :
1015 4 : if (NT_STATUS_IS_OK(result)) {
1016 4 : result = tcon_status;
1017 : }
1018 :
1019 4 : if (!NT_STATUS_IS_OK(result)) {
1020 0 : DEBUG(1, ("Failed to prepare SMB connection to %s: %s\n",
1021 : controller, nt_errstr(result)));
1022 0 : winbind_add_failed_connection_entry(domain, controller, result);
1023 0 : if ((*cli) != NULL) {
1024 0 : cli_shutdown(*cli);
1025 0 : *cli = NULL;
1026 : }
1027 : }
1028 :
1029 4 : return result;
1030 : }
1031 :
1032 : /*******************************************************************
1033 : Add a dcname and sockaddr_storage pair to the end of a dc_name_ip
1034 : array.
1035 :
1036 : Keeps the list unique by not adding duplicate entries.
1037 :
1038 : @param[in] mem_ctx talloc memory context to allocate from
1039 : @param[in] domain_name domain of the DC
1040 : @param[in] dcname name of the DC to add to the list
1041 : @param[in] pss Internet address and port pair to add to the list
1042 : @param[in,out] dcs array of dc_name_ip structures to add to
1043 : @param[in,out] num_dcs number of dcs returned in the dcs array
1044 : @return true if the list was added to, false otherwise
1045 : *******************************************************************/
1046 :
1047 8 : static bool add_one_dc_unique(TALLOC_CTX *mem_ctx, const char *domain_name,
1048 : const char *dcname, struct sockaddr_storage *pss,
1049 : struct dc_name_ip **dcs, int *num)
1050 : {
1051 8 : int i = 0;
1052 :
1053 8 : if (!NT_STATUS_IS_OK(check_negative_conn_cache(domain_name, dcname))) {
1054 0 : DEBUG(10, ("DC %s was in the negative conn cache\n", dcname));
1055 0 : return False;
1056 : }
1057 :
1058 : /* Make sure there's no duplicates in the list */
1059 12 : for (i=0; i<*num; i++)
1060 8 : if (sockaddr_equal(
1061 8 : (struct sockaddr *)(void *)&(*dcs)[i].ss,
1062 : (struct sockaddr *)(void *)pss))
1063 4 : return False;
1064 :
1065 4 : *dcs = talloc_realloc(mem_ctx, *dcs, struct dc_name_ip, (*num)+1);
1066 :
1067 4 : if (*dcs == NULL)
1068 0 : return False;
1069 :
1070 4 : fstrcpy((*dcs)[*num].name, dcname);
1071 4 : (*dcs)[*num].ss = *pss;
1072 4 : *num += 1;
1073 4 : return True;
1074 : }
1075 :
1076 4 : static bool add_sockaddr_to_array(TALLOC_CTX *mem_ctx,
1077 : struct sockaddr_storage *pss, uint16_t port,
1078 : struct sockaddr_storage **addrs, int *num)
1079 : {
1080 4 : *addrs = talloc_realloc(mem_ctx, *addrs, struct sockaddr_storage, (*num)+1);
1081 :
1082 4 : if (*addrs == NULL) {
1083 0 : *num = 0;
1084 0 : return False;
1085 : }
1086 :
1087 4 : (*addrs)[*num] = *pss;
1088 4 : set_sockaddr_port((struct sockaddr *)&(*addrs)[*num], port);
1089 :
1090 4 : *num += 1;
1091 4 : return True;
1092 : }
1093 :
1094 : #ifdef HAVE_ADS
1095 4 : static bool dcip_check_name_ads(const struct winbindd_domain *domain,
1096 : struct samba_sockaddr *sa,
1097 : uint32_t request_flags,
1098 : TALLOC_CTX *mem_ctx,
1099 : char **namep)
1100 : {
1101 4 : TALLOC_CTX *tmp_ctx = talloc_stackframe();
1102 4 : char *name = NULL;
1103 4 : ADS_STRUCT *ads = NULL;
1104 0 : ADS_STATUS ads_status;
1105 0 : char addr[INET6_ADDRSTRLEN];
1106 :
1107 4 : print_sockaddr(addr, sizeof(addr), &sa->u.ss);
1108 4 : D_DEBUG("Trying to figure out the DC name for domain '%s' at IP '%s'.\n",
1109 : domain->name,
1110 : addr);
1111 :
1112 4 : ads = ads_init(tmp_ctx,
1113 4 : domain->alt_name,
1114 4 : domain->name,
1115 : addr,
1116 : ADS_SASL_PLAIN);
1117 4 : if (ads == NULL) {
1118 0 : ads_status = ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
1119 0 : goto out;
1120 : }
1121 4 : ads->auth.flags |= ADS_AUTH_NO_BIND;
1122 4 : ads->config.flags |= request_flags;
1123 4 : ads->server.no_fallback = true;
1124 :
1125 4 : ads_status = ads_connect(ads);
1126 4 : if (!ADS_ERR_OK(ads_status)) {
1127 0 : goto out;
1128 : }
1129 :
1130 : /* We got a cldap packet. */
1131 4 : name = talloc_strdup(tmp_ctx, ads->config.ldap_server_name);
1132 4 : if (name == NULL) {
1133 0 : ads_status = ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
1134 0 : goto out;
1135 : }
1136 4 : namecache_store(name, 0x20, 1, sa);
1137 :
1138 4 : DBG_DEBUG("CLDAP flags = 0x%"PRIx32"\n", ads->config.flags);
1139 :
1140 4 : if (domain->primary && (ads->config.flags & NBT_SERVER_KDC)) {
1141 2 : if (ads_closest_dc(ads)) {
1142 2 : char *sitename = sitename_fetch(tmp_ctx,
1143 : ads->config.realm);
1144 :
1145 : /* We're going to use this KDC for this realm/domain.
1146 : If we are using sites, then force the krb5 libs
1147 : to use this KDC. */
1148 :
1149 2 : create_local_private_krb5_conf_for_domain(domain->alt_name,
1150 2 : domain->name,
1151 : sitename,
1152 2 : &sa->u.ss);
1153 :
1154 2 : TALLOC_FREE(sitename);
1155 : } else {
1156 : /* use an off site KDC */
1157 0 : create_local_private_krb5_conf_for_domain(domain->alt_name,
1158 0 : domain->name,
1159 : NULL,
1160 0 : &sa->u.ss);
1161 : }
1162 2 : winbindd_set_locator_kdc_envs(domain);
1163 :
1164 : /* Ensure we contact this DC also. */
1165 2 : saf_store(domain->name, name);
1166 2 : saf_store(domain->alt_name, name);
1167 : }
1168 :
1169 4 : D_DEBUG("DC name for domain '%s' at IP '%s' is '%s'\n",
1170 : domain->name,
1171 : addr,
1172 : name);
1173 4 : *namep = talloc_move(mem_ctx, &name);
1174 :
1175 4 : out:
1176 4 : TALLOC_FREE(tmp_ctx);
1177 :
1178 4 : return ADS_ERR_OK(ads_status) ? true : false;
1179 : }
1180 : #endif
1181 :
1182 : /*******************************************************************
1183 : convert an ip to a name
1184 : For an AD Domain, it checks the requirements of the request flags.
1185 : *******************************************************************/
1186 :
1187 4 : static bool dcip_check_name(TALLOC_CTX *mem_ctx,
1188 : const struct winbindd_domain *domain,
1189 : struct sockaddr_storage *pss,
1190 : char **name, uint32_t request_flags)
1191 : {
1192 4 : struct samba_sockaddr sa = {0};
1193 4 : uint32_t nt_version = NETLOGON_NT_VERSION_1;
1194 0 : NTSTATUS status;
1195 0 : const char *dc_name;
1196 0 : fstring nbtname;
1197 : #ifdef HAVE_ADS
1198 4 : bool is_ad_domain = false;
1199 : #endif
1200 4 : bool ok = sockaddr_storage_to_samba_sockaddr(&sa, pss);
1201 4 : if (!ok) {
1202 0 : return false;
1203 : }
1204 :
1205 : #ifdef HAVE_ADS
1206 : /* For active directory servers, try to get the ldap server name.
1207 : None of these failures should be considered critical for now */
1208 :
1209 4 : if ((lp_security() == SEC_ADS) && (domain->alt_name != NULL)) {
1210 4 : is_ad_domain = true;
1211 0 : } else if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
1212 0 : is_ad_domain = domain->active_directory;
1213 : }
1214 :
1215 4 : if (is_ad_domain) {
1216 4 : return dcip_check_name_ads(domain,
1217 : &sa,
1218 : request_flags,
1219 : mem_ctx,
1220 : name);
1221 : }
1222 : #endif
1223 :
1224 0 : {
1225 0 : size_t len = strlen(lp_netbios_name());
1226 0 : char my_acct_name[len+2];
1227 :
1228 0 : snprintf(my_acct_name,
1229 : sizeof(my_acct_name),
1230 : "%s$",
1231 : lp_netbios_name());
1232 :
1233 0 : status = nbt_getdc(global_messaging_context(), 10, &sa.u.ss,
1234 0 : domain->name, &domain->sid,
1235 : my_acct_name, ACB_WSTRUST,
1236 : nt_version, mem_ctx, &nt_version,
1237 : &dc_name, NULL);
1238 : }
1239 0 : if (NT_STATUS_IS_OK(status)) {
1240 0 : *name = talloc_strdup(mem_ctx, dc_name);
1241 0 : if (*name == NULL) {
1242 0 : return false;
1243 : }
1244 0 : namecache_store(*name, 0x20, 1, &sa);
1245 0 : return True;
1246 : }
1247 :
1248 : /* try node status request */
1249 :
1250 0 : if (name_status_find(domain->name, 0x1c, 0x20, &sa.u.ss, nbtname) ) {
1251 0 : namecache_store(nbtname, 0x20, 1, &sa);
1252 :
1253 0 : if (name != NULL) {
1254 0 : *name = talloc_strdup(mem_ctx, nbtname);
1255 0 : if (*name == NULL) {
1256 0 : return false;
1257 : }
1258 : }
1259 :
1260 0 : return true;
1261 : }
1262 0 : return False;
1263 : }
1264 :
1265 : /*******************************************************************
1266 : Retrieve a list of IP addresses for domain controllers.
1267 :
1268 : The array is sorted in the preferred connection order.
1269 :
1270 : @param[in] mem_ctx talloc memory context to allocate from
1271 : @param[in] domain domain to retrieve DCs for
1272 : @param[out] dcs array of dcs that will be returned
1273 : @param[out] num_dcs number of dcs returned in the dcs array
1274 : @return always true
1275 : *******************************************************************/
1276 :
1277 2 : static bool get_dcs(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
1278 : struct dc_name_ip **dcs, int *num_dcs,
1279 : uint32_t request_flags)
1280 : {
1281 0 : fstring dcname;
1282 0 : struct sockaddr_storage ss;
1283 2 : struct samba_sockaddr *sa_list = NULL;
1284 2 : size_t salist_size = 0;
1285 0 : size_t i;
1286 0 : bool is_our_domain;
1287 2 : enum security_types sec = (enum security_types)lp_security();
1288 :
1289 2 : is_our_domain = strequal(domain->name, lp_workgroup());
1290 :
1291 : /* If not our domain, get the preferred DC, by asking our primary DC */
1292 2 : if ( !is_our_domain
1293 2 : && get_dc_name_via_netlogon(domain, dcname, &ss, request_flags)
1294 0 : && add_one_dc_unique(mem_ctx, domain->name, dcname, &ss, dcs,
1295 : num_dcs) )
1296 : {
1297 0 : char addr[INET6_ADDRSTRLEN];
1298 0 : print_sockaddr(addr, sizeof(addr), &ss);
1299 0 : DEBUG(10, ("Retrieved DC %s at %s via netlogon\n",
1300 : dcname, addr));
1301 0 : return True;
1302 : }
1303 :
1304 2 : if ((sec == SEC_ADS) && (domain->alt_name != NULL)) {
1305 2 : char *sitename = NULL;
1306 :
1307 : /* We need to make sure we know the local site before
1308 : doing any DNS queries, as this will restrict the
1309 : get_sorted_dc_list() call below to only fetching
1310 : DNS records for the correct site. */
1311 :
1312 : /* Find any DC to get the site record.
1313 : We deliberately don't care about the
1314 : return here. */
1315 :
1316 2 : get_dc_name(domain->name, domain->alt_name, dcname, &ss);
1317 :
1318 2 : sitename = sitename_fetch(mem_ctx, domain->alt_name);
1319 2 : if (sitename) {
1320 :
1321 : /* Do the site-specific AD dns lookup first. */
1322 2 : (void)get_sorted_dc_list(mem_ctx,
1323 2 : domain->alt_name,
1324 : sitename,
1325 : &sa_list,
1326 : &salist_size,
1327 : true);
1328 :
1329 : /* Add ips to the DC array. We don't look up the name
1330 : of the DC in this function, but we fill in the char*
1331 : of the ip now to make the failed connection cache
1332 : work */
1333 6 : for ( i=0; i<salist_size; i++ ) {
1334 0 : char addr[INET6_ADDRSTRLEN];
1335 4 : print_sockaddr(addr, sizeof(addr),
1336 4 : &sa_list[i].u.ss);
1337 4 : add_one_dc_unique(mem_ctx,
1338 4 : domain->name,
1339 : addr,
1340 4 : &sa_list[i].u.ss,
1341 : dcs,
1342 : num_dcs);
1343 : }
1344 :
1345 2 : TALLOC_FREE(sa_list);
1346 2 : TALLOC_FREE(sitename);
1347 2 : salist_size = 0;
1348 : }
1349 :
1350 : /* Now we add DCs from the main AD DNS lookup. */
1351 2 : (void)get_sorted_dc_list(mem_ctx,
1352 2 : domain->alt_name,
1353 : NULL,
1354 : &sa_list,
1355 : &salist_size,
1356 : true);
1357 :
1358 6 : for ( i=0; i<salist_size; i++ ) {
1359 0 : char addr[INET6_ADDRSTRLEN];
1360 4 : print_sockaddr(addr, sizeof(addr),
1361 4 : &sa_list[i].u.ss);
1362 4 : add_one_dc_unique(mem_ctx,
1363 4 : domain->name,
1364 : addr,
1365 4 : &sa_list[i].u.ss,
1366 : dcs,
1367 : num_dcs);
1368 : }
1369 :
1370 2 : TALLOC_FREE(sa_list);
1371 2 : salist_size = 0;
1372 : }
1373 :
1374 : /* Try standard netbios queries if no ADS and fall back to DNS queries
1375 : * if alt_name is available */
1376 2 : if (*num_dcs == 0) {
1377 0 : (void)get_sorted_dc_list(mem_ctx,
1378 0 : domain->name,
1379 : NULL,
1380 : &sa_list,
1381 : &salist_size,
1382 : false);
1383 0 : if (salist_size == 0) {
1384 0 : if (domain->alt_name != NULL) {
1385 0 : (void)get_sorted_dc_list(mem_ctx,
1386 0 : domain->alt_name,
1387 : NULL,
1388 : &sa_list,
1389 : &salist_size,
1390 : true);
1391 : }
1392 : }
1393 :
1394 0 : for ( i=0; i<salist_size; i++ ) {
1395 0 : char addr[INET6_ADDRSTRLEN];
1396 0 : print_sockaddr(addr, sizeof(addr),
1397 0 : &sa_list[i].u.ss);
1398 0 : add_one_dc_unique(mem_ctx,
1399 0 : domain->name,
1400 : addr,
1401 0 : &sa_list[i].u.ss,
1402 : dcs,
1403 : num_dcs);
1404 : }
1405 :
1406 0 : TALLOC_FREE(sa_list);
1407 0 : salist_size = 0;
1408 : }
1409 :
1410 2 : return True;
1411 : }
1412 :
1413 4 : static bool connect_preferred_dc(TALLOC_CTX *mem_ctx,
1414 : struct winbindd_domain *domain,
1415 : uint32_t request_flags,
1416 : int *fd)
1417 : {
1418 4 : char *saf_servername = NULL;
1419 0 : NTSTATUS status;
1420 0 : bool ok;
1421 :
1422 : /*
1423 : * We have to check the server affinity cache here since later we select
1424 : * a DC based on response time and not preference.
1425 : */
1426 4 : if (domain->force_dc) {
1427 0 : saf_servername = domain->dcname;
1428 : } else {
1429 4 : saf_servername = saf_fetch(mem_ctx, domain->name);
1430 : }
1431 :
1432 : /*
1433 : * Check the negative connection cache before talking to it. It going
1434 : * down may have triggered the reconnection.
1435 : */
1436 4 : if (saf_servername != NULL) {
1437 2 : status = check_negative_conn_cache(domain->name,
1438 : saf_servername);
1439 2 : if (!NT_STATUS_IS_OK(status)) {
1440 0 : saf_servername = NULL;
1441 : }
1442 : }
1443 :
1444 4 : if (saf_servername != NULL) {
1445 2 : DBG_DEBUG("saf_servername is '%s' for domain %s\n",
1446 : saf_servername, domain->name);
1447 :
1448 : /* convert an ip address to a name */
1449 2 : if (is_ipaddress(saf_servername)) {
1450 0 : ok = interpret_string_addr(&domain->dcaddr,
1451 : saf_servername,
1452 : AI_NUMERICHOST);
1453 0 : if (!ok) {
1454 0 : return false;
1455 : }
1456 : } else {
1457 2 : ok = resolve_name(saf_servername,
1458 : &domain->dcaddr,
1459 : 0x20,
1460 : true);
1461 2 : if (!ok) {
1462 0 : goto fail;
1463 : }
1464 : }
1465 :
1466 2 : TALLOC_FREE(domain->dcname);
1467 2 : ok = dcip_check_name(domain,
1468 : domain,
1469 : &domain->dcaddr,
1470 : &domain->dcname,
1471 : request_flags);
1472 2 : if (!ok) {
1473 0 : goto fail;
1474 : }
1475 : }
1476 :
1477 4 : if (domain->dcname == NULL) {
1478 2 : return false;
1479 : }
1480 :
1481 2 : status = check_negative_conn_cache(domain->name, domain->dcname);
1482 2 : if (!NT_STATUS_IS_OK(status)) {
1483 0 : return false;
1484 : }
1485 :
1486 2 : status = smbsock_connect(&domain->dcaddr, 0,
1487 : NULL, -1, NULL, -1,
1488 : fd, NULL, 10);
1489 2 : if (!NT_STATUS_IS_OK(status)) {
1490 0 : winbind_add_failed_connection_entry(domain,
1491 0 : domain->dcname,
1492 0 : NT_STATUS_UNSUCCESSFUL);
1493 0 : return false;
1494 : }
1495 2 : return true;
1496 :
1497 0 : fail:
1498 0 : winbind_add_failed_connection_entry(domain,
1499 : saf_servername,
1500 0 : NT_STATUS_UNSUCCESSFUL);
1501 0 : return false;
1502 :
1503 : }
1504 :
1505 : /*******************************************************************
1506 : Find and make a connection to a DC in the given domain.
1507 :
1508 : @param[in] mem_ctx talloc memory context to allocate from
1509 : @param[in] domain domain to find a dc in
1510 : @param[out] fd fd of the open socket connected to the newly found dc
1511 : @return true when a DC connection is made, false otherwise
1512 : *******************************************************************/
1513 :
1514 4 : static bool find_dc(TALLOC_CTX *mem_ctx,
1515 : struct winbindd_domain *domain,
1516 : uint32_t request_flags,
1517 : int *fd)
1518 : {
1519 4 : struct dc_name_ip *dcs = NULL;
1520 4 : int num_dcs = 0;
1521 :
1522 4 : const char **dcnames = NULL;
1523 4 : size_t num_dcnames = 0;
1524 :
1525 4 : struct sockaddr_storage *addrs = NULL;
1526 4 : int num_addrs = 0;
1527 :
1528 0 : int i;
1529 0 : size_t fd_index;
1530 :
1531 0 : NTSTATUS status;
1532 0 : bool ok;
1533 :
1534 4 : *fd = -1;
1535 :
1536 4 : D_NOTICE("First try to connect to the closest DC (using server "
1537 : "affinity cache). If this fails, try to lookup the DC using "
1538 : "DNS afterwards.\n");
1539 4 : ok = connect_preferred_dc(mem_ctx, domain, request_flags, fd);
1540 4 : if (ok) {
1541 2 : return true;
1542 : }
1543 :
1544 2 : if (domain->force_dc) {
1545 0 : return false;
1546 : }
1547 :
1548 2 : again:
1549 2 : D_DEBUG("Retrieving a list of IP addresses for DCs.\n");
1550 2 : if (!get_dcs(mem_ctx, domain, &dcs, &num_dcs, request_flags) || (num_dcs == 0))
1551 0 : return False;
1552 :
1553 2 : D_DEBUG("Retrieved IP addresses for %d DCs.\n", num_dcs);
1554 6 : for (i=0; i<num_dcs; i++) {
1555 :
1556 4 : if (!add_string_to_array(mem_ctx, dcs[i].name,
1557 : &dcnames, &num_dcnames)) {
1558 0 : return False;
1559 : }
1560 4 : if (!add_sockaddr_to_array(mem_ctx, &dcs[i].ss, TCP_SMB_PORT,
1561 : &addrs, &num_addrs)) {
1562 0 : return False;
1563 : }
1564 : }
1565 :
1566 2 : if ((num_dcnames == 0) || (num_dcnames != num_addrs))
1567 0 : return False;
1568 :
1569 2 : if ((addrs == NULL) || (dcnames == NULL))
1570 0 : return False;
1571 :
1572 2 : D_DEBUG("Trying to establish a connection to one of the %d DCs "
1573 : "(timeout of 10 sec for each DC).\n",
1574 : num_dcs);
1575 2 : status = smbsock_any_connect(addrs, dcnames, NULL, NULL, NULL,
1576 : num_addrs, 0, 10, fd, &fd_index, NULL);
1577 2 : if (!NT_STATUS_IS_OK(status)) {
1578 0 : for (i=0; i<num_dcs; i++) {
1579 0 : char ab[INET6_ADDRSTRLEN];
1580 0 : print_sockaddr(ab, sizeof(ab), &dcs[i].ss);
1581 0 : DBG_DEBUG("smbsock_any_connect failed for "
1582 : "domain %s address %s. Error was %s\n",
1583 : domain->name, ab, nt_errstr(status));
1584 0 : winbind_add_failed_connection_entry(domain,
1585 0 : dcs[i].name, NT_STATUS_UNSUCCESSFUL);
1586 : }
1587 0 : return False;
1588 : }
1589 2 : D_NOTICE("Successfully connected to DC '%s'.\n", dcs[fd_index].name);
1590 :
1591 2 : domain->dcaddr = addrs[fd_index];
1592 :
1593 2 : if (*dcnames[fd_index] != '\0' && !is_ipaddress(dcnames[fd_index])) {
1594 : /* Ok, we've got a name for the DC */
1595 0 : TALLOC_FREE(domain->dcname);
1596 0 : domain->dcname = talloc_strdup(domain, dcnames[fd_index]);
1597 0 : if (domain->dcname == NULL) {
1598 0 : return false;
1599 : }
1600 0 : return true;
1601 : }
1602 :
1603 : /* Try to figure out the name */
1604 2 : TALLOC_FREE(domain->dcname);
1605 2 : ok = dcip_check_name(domain,
1606 : domain,
1607 : &domain->dcaddr,
1608 : &domain->dcname,
1609 : request_flags);
1610 2 : if (ok) {
1611 2 : return true;
1612 : }
1613 :
1614 : /* We can not continue without the DC's name */
1615 0 : winbind_add_failed_connection_entry(domain, dcs[fd_index].name,
1616 0 : NT_STATUS_UNSUCCESSFUL);
1617 :
1618 : /* Throw away all arrays as we're doing this again. */
1619 0 : TALLOC_FREE(dcs);
1620 0 : num_dcs = 0;
1621 :
1622 0 : TALLOC_FREE(dcnames);
1623 0 : num_dcnames = 0;
1624 :
1625 0 : TALLOC_FREE(addrs);
1626 0 : num_addrs = 0;
1627 :
1628 0 : if (*fd != -1) {
1629 0 : close(*fd);
1630 0 : *fd = -1;
1631 : }
1632 :
1633 : /*
1634 : * This should not be an infinite loop, since get_dcs() will not return
1635 : * the DC added to the negative connection cache in the above
1636 : * winbind_add_failed_connection_entry() call.
1637 : */
1638 0 : goto again;
1639 : }
1640 :
1641 8 : static char *current_dc_key(TALLOC_CTX *mem_ctx, const char *domain_name)
1642 : {
1643 8 : return talloc_asprintf_strupper_m(mem_ctx, "CURRENT_DCNAME/%s",
1644 : domain_name);
1645 : }
1646 :
1647 4 : static void store_current_dc_in_gencache(const char *domain_name,
1648 : const char *dc_name,
1649 : struct cli_state *cli)
1650 : {
1651 0 : char addr[INET6_ADDRSTRLEN];
1652 4 : char *key = NULL;
1653 4 : char *value = NULL;
1654 :
1655 4 : if (!cli_state_is_connected(cli)) {
1656 0 : return;
1657 : }
1658 :
1659 4 : print_sockaddr(addr, sizeof(addr),
1660 : smbXcli_conn_remote_sockaddr(cli->conn));
1661 :
1662 4 : key = current_dc_key(talloc_tos(), domain_name);
1663 4 : if (key == NULL) {
1664 0 : goto done;
1665 : }
1666 :
1667 4 : value = talloc_asprintf(talloc_tos(), "%s %s", addr, dc_name);
1668 4 : if (value == NULL) {
1669 0 : goto done;
1670 : }
1671 :
1672 4 : gencache_set(key, value, 0x7fffffff);
1673 4 : done:
1674 4 : TALLOC_FREE(value);
1675 4 : TALLOC_FREE(key);
1676 : }
1677 :
1678 4 : bool fetch_current_dc_from_gencache(TALLOC_CTX *mem_ctx,
1679 : const char *domain_name,
1680 : char **p_dc_name, char **p_dc_ip)
1681 : {
1682 0 : char *key, *p;
1683 4 : char *value = NULL;
1684 4 : bool ret = false;
1685 4 : char *dc_name = NULL;
1686 4 : char *dc_ip = NULL;
1687 :
1688 4 : key = current_dc_key(talloc_tos(), domain_name);
1689 4 : if (key == NULL) {
1690 0 : goto done;
1691 : }
1692 4 : if (!gencache_get(key, mem_ctx, &value, NULL)) {
1693 0 : goto done;
1694 : }
1695 4 : p = strchr(value, ' ');
1696 4 : if (p == NULL) {
1697 0 : goto done;
1698 : }
1699 4 : dc_ip = talloc_strndup(mem_ctx, value, p - value);
1700 4 : if (dc_ip == NULL) {
1701 0 : goto done;
1702 : }
1703 4 : dc_name = talloc_strdup(mem_ctx, p+1);
1704 4 : if (dc_name == NULL) {
1705 0 : goto done;
1706 : }
1707 :
1708 4 : if (p_dc_ip != NULL) {
1709 4 : *p_dc_ip = dc_ip;
1710 4 : dc_ip = NULL;
1711 : }
1712 4 : if (p_dc_name != NULL) {
1713 4 : *p_dc_name = dc_name;
1714 4 : dc_name = NULL;
1715 : }
1716 4 : ret = true;
1717 4 : done:
1718 4 : TALLOC_FREE(dc_name);
1719 4 : TALLOC_FREE(dc_ip);
1720 4 : TALLOC_FREE(key);
1721 4 : TALLOC_FREE(value);
1722 4 : return ret;
1723 : }
1724 :
1725 0 : NTSTATUS wb_open_internal_pipe(TALLOC_CTX *mem_ctx,
1726 : const struct ndr_interface_table *table,
1727 : struct rpc_pipe_client **ret_pipe)
1728 : {
1729 0 : struct rpc_pipe_client *cli = NULL;
1730 0 : const struct auth_session_info *session_info = NULL;
1731 0 : NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1732 :
1733 :
1734 0 : session_info = get_session_info_system();
1735 0 : SMB_ASSERT(session_info != NULL);
1736 :
1737 0 : status = rpc_pipe_open_local_np(
1738 : mem_ctx, table, NULL, NULL, NULL, NULL, session_info, &cli);
1739 0 : if (!NT_STATUS_IS_OK(status)) {
1740 0 : return status;
1741 : }
1742 :
1743 0 : if (ret_pipe) {
1744 0 : *ret_pipe = cli;
1745 : }
1746 :
1747 0 : return NT_STATUS_OK;
1748 : }
1749 :
1750 4 : static NTSTATUS cm_open_connection(struct winbindd_domain *domain,
1751 : struct winbindd_cm_conn *new_conn,
1752 : bool need_rw_dc)
1753 : {
1754 0 : TALLOC_CTX *mem_ctx;
1755 4 : NTSTATUS result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1756 0 : int retries;
1757 4 : uint32_t request_flags = need_rw_dc ? DS_WRITABLE_REQUIRED : 0;
1758 4 : int fd = -1;
1759 4 : bool retry = false;
1760 4 : bool seal_pipes = true;
1761 :
1762 4 : if ((mem_ctx = talloc_init("cm_open_connection")) == NULL) {
1763 0 : set_domain_offline(domain);
1764 0 : return NT_STATUS_NO_MEMORY;
1765 : }
1766 :
1767 4 : D_NOTICE("Creating connection to domain controller. This is a start of "
1768 : "a new connection or a DC failover. The failover only happens "
1769 : "if the domain has more than one DC. We will try to connect 3 "
1770 : "times at most.\n");
1771 4 : for (retries = 0; retries < 3; retries++) {
1772 0 : bool found_dc;
1773 :
1774 4 : D_DEBUG("Attempt %d/3: DC '%s' of domain '%s'.\n",
1775 : retries,
1776 : domain->dcname ? domain->dcname : "",
1777 : domain->name);
1778 :
1779 4 : found_dc = find_dc(mem_ctx, domain, request_flags, &fd);
1780 4 : if (!found_dc) {
1781 : /* This is the one place where we will
1782 : set the global winbindd offline state
1783 : to true, if a "WINBINDD_OFFLINE" entry
1784 : is found in the winbindd cache. */
1785 0 : set_global_winbindd_state_offline();
1786 0 : result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1787 0 : break;
1788 : }
1789 :
1790 4 : new_conn->cli = NULL;
1791 :
1792 4 : result = cm_prepare_connection(domain, fd, domain->dcname,
1793 : &new_conn->cli, &retry);
1794 4 : if (NT_STATUS_IS_OK(result)) {
1795 4 : break;
1796 : }
1797 0 : if (!retry) {
1798 0 : break;
1799 : }
1800 : }
1801 :
1802 4 : if (!NT_STATUS_IS_OK(result)) {
1803 : /* Ensure we setup the retry handler. */
1804 0 : set_domain_offline(domain);
1805 0 : goto out;
1806 : }
1807 :
1808 4 : winbindd_set_locator_kdc_envs(domain);
1809 :
1810 4 : if (domain->online == False) {
1811 : /* We're changing state from offline to online. */
1812 2 : set_global_winbindd_state_online();
1813 : }
1814 4 : set_domain_online(domain);
1815 :
1816 : /*
1817 : * Much as I hate global state, this seems to be the point
1818 : * where we can be certain that we have a proper connection to
1819 : * a DC. wbinfo --dc-info needs that information, store it in
1820 : * gencache with a looong timeout. This will need revisiting
1821 : * once we start to connect to multiple DCs, wbcDcInfo is
1822 : * already prepared for that.
1823 : */
1824 4 : store_current_dc_in_gencache(domain->name, domain->dcname,
1825 : new_conn->cli);
1826 :
1827 4 : seal_pipes = lp_winbind_sealed_pipes();
1828 4 : seal_pipes = lp_parm_bool(-1, "winbind sealed pipes",
1829 4 : domain->name,
1830 : seal_pipes);
1831 :
1832 4 : if (seal_pipes) {
1833 4 : new_conn->auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1834 : } else {
1835 0 : new_conn->auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1836 : }
1837 :
1838 4 : out:
1839 4 : talloc_destroy(mem_ctx);
1840 4 : return result;
1841 : }
1842 :
1843 : /* Close down all open pipes on a connection. */
1844 :
1845 4 : void invalidate_cm_connection(struct winbindd_domain *domain)
1846 : {
1847 0 : NTSTATUS result;
1848 4 : struct winbindd_cm_conn *conn = &domain->conn;
1849 :
1850 4 : domain->sequence_number = DOM_SEQUENCE_NONE;
1851 4 : domain->last_seq_check = 0;
1852 4 : domain->last_status = NT_STATUS_SERVER_DISABLED;
1853 :
1854 : /* We're closing down a possibly dead
1855 : connection. Don't have impossibly long (10s) timeouts. */
1856 :
1857 4 : if (conn->cli) {
1858 0 : cli_set_timeout(conn->cli, 1000); /* 1 second. */
1859 : }
1860 :
1861 4 : if (conn->samr_pipe != NULL) {
1862 0 : if (is_valid_policy_hnd(&conn->sam_connect_handle)) {
1863 0 : dcerpc_samr_Close(conn->samr_pipe->binding_handle,
1864 : talloc_tos(),
1865 : &conn->sam_connect_handle,
1866 : &result);
1867 : }
1868 0 : TALLOC_FREE(conn->samr_pipe);
1869 : /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1870 0 : if (conn->cli) {
1871 0 : cli_set_timeout(conn->cli, 500);
1872 : }
1873 : }
1874 :
1875 4 : if (conn->lsa_pipe != NULL) {
1876 0 : if (is_valid_policy_hnd(&conn->lsa_policy)) {
1877 0 : dcerpc_lsa_Close(conn->lsa_pipe->binding_handle,
1878 : talloc_tos(),
1879 : &conn->lsa_policy,
1880 : &result);
1881 : }
1882 0 : TALLOC_FREE(conn->lsa_pipe);
1883 : /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1884 0 : if (conn->cli) {
1885 0 : cli_set_timeout(conn->cli, 500);
1886 : }
1887 : }
1888 :
1889 4 : if (conn->lsa_pipe_tcp != NULL) {
1890 0 : if (is_valid_policy_hnd(&conn->lsa_policy)) {
1891 0 : dcerpc_lsa_Close(conn->lsa_pipe_tcp->binding_handle,
1892 : talloc_tos(),
1893 : &conn->lsa_policy,
1894 : &result);
1895 : }
1896 0 : TALLOC_FREE(conn->lsa_pipe_tcp);
1897 : /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1898 0 : if (conn->cli) {
1899 0 : cli_set_timeout(conn->cli, 500);
1900 : }
1901 : }
1902 :
1903 4 : if (conn->netlogon_pipe != NULL) {
1904 0 : TALLOC_FREE(conn->netlogon_pipe);
1905 : /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1906 0 : if (conn->cli) {
1907 0 : cli_set_timeout(conn->cli, 500);
1908 : }
1909 : }
1910 :
1911 4 : conn->auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1912 4 : TALLOC_FREE(conn->netlogon_creds_ctx);
1913 :
1914 4 : if (conn->cli) {
1915 0 : cli_shutdown(conn->cli);
1916 : }
1917 :
1918 4 : conn->cli = NULL;
1919 4 : }
1920 :
1921 0 : void close_conns_after_fork(void)
1922 : {
1923 0 : struct winbindd_domain *domain;
1924 0 : struct winbindd_cli_state *cli_state;
1925 :
1926 0 : for (domain = domain_list(); domain; domain = domain->next) {
1927 : /*
1928 : * first close the low level SMB TCP connection
1929 : * so that we don't generate any SMBclose
1930 : * requests in invalidate_cm_connection()
1931 : */
1932 0 : if (cli_state_is_connected(domain->conn.cli)) {
1933 0 : smbXcli_conn_disconnect(domain->conn.cli->conn, NT_STATUS_OK);
1934 : }
1935 :
1936 0 : invalidate_cm_connection(domain);
1937 : }
1938 :
1939 0 : for (cli_state = winbindd_client_list();
1940 0 : cli_state != NULL;
1941 0 : cli_state = cli_state->next) {
1942 0 : if (cli_state->sock >= 0) {
1943 0 : close(cli_state->sock);
1944 0 : cli_state->sock = -1;
1945 : }
1946 : }
1947 0 : }
1948 :
1949 14 : static bool connection_ok(struct winbindd_domain *domain)
1950 : {
1951 0 : bool ok;
1952 :
1953 14 : ok = cli_state_is_connected(domain->conn.cli);
1954 14 : if (!ok) {
1955 6 : DEBUG(3, ("connection_ok: Connection to %s for domain %s is not connected\n",
1956 : domain->dcname, domain->name));
1957 6 : return False;
1958 : }
1959 :
1960 8 : if (!domain->online) {
1961 0 : DEBUG(3, ("connection_ok: Domain %s is offline\n", domain->name));
1962 0 : return False;
1963 : }
1964 :
1965 8 : return True;
1966 : }
1967 :
1968 : /* Initialize a new connection up to the RPC BIND.
1969 : Bypass online status check so always does network calls. */
1970 :
1971 10 : static NTSTATUS init_dc_connection_network(struct winbindd_domain *domain, bool need_rw_dc)
1972 : {
1973 0 : NTSTATUS result;
1974 10 : bool skip_connection = domain->internal;
1975 10 : if (need_rw_dc && domain->rodc) {
1976 0 : skip_connection = false;
1977 : }
1978 :
1979 : /* Internal connections never use the network. */
1980 10 : if (dom_sid_equal(&domain->sid, &global_sid_Builtin)) {
1981 0 : return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1982 : }
1983 :
1984 : /* Still ask the internal LSA and SAMR server about the local domain */
1985 10 : if (skip_connection || connection_ok(domain)) {
1986 6 : if (!domain->initialized) {
1987 0 : set_dc_type_and_flags(domain);
1988 : }
1989 6 : return NT_STATUS_OK;
1990 : }
1991 :
1992 4 : invalidate_cm_connection(domain);
1993 :
1994 4 : if (!domain->primary && !domain->initialized) {
1995 : /*
1996 : * Before we connect to a trust, work out if it is an
1997 : * AD domain by asking our own domain.
1998 : */
1999 2 : set_dc_type_and_flags_trustinfo(domain);
2000 : }
2001 :
2002 4 : result = cm_open_connection(domain, &domain->conn, need_rw_dc);
2003 :
2004 4 : if (NT_STATUS_IS_OK(result) && !domain->initialized) {
2005 2 : set_dc_type_and_flags(domain);
2006 : }
2007 :
2008 4 : return result;
2009 : }
2010 :
2011 21 : NTSTATUS init_dc_connection(struct winbindd_domain *domain, bool need_rw_dc)
2012 : {
2013 21 : if (dom_sid_equal(&domain->sid, &global_sid_Builtin)) {
2014 11 : return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2015 : }
2016 :
2017 10 : SMB_ASSERT(wb_child_domain() || idmap_child());
2018 :
2019 10 : return init_dc_connection_network(domain, need_rw_dc);
2020 : }
2021 :
2022 8 : static NTSTATUS init_dc_connection_rpc(struct winbindd_domain *domain, bool need_rw_dc)
2023 : {
2024 0 : NTSTATUS status;
2025 :
2026 8 : status = init_dc_connection(domain, need_rw_dc);
2027 8 : if (!NT_STATUS_IS_OK(status)) {
2028 0 : return status;
2029 : }
2030 :
2031 8 : if (!domain->internal && domain->conn.cli == NULL) {
2032 : /* happens for trusted domains without inbound trust */
2033 0 : return NT_STATUS_TRUSTED_DOMAIN_FAILURE;
2034 : }
2035 :
2036 8 : return NT_STATUS_OK;
2037 : }
2038 :
2039 : /******************************************************************************
2040 : Set the trust flags (direction and forest location) for a domain
2041 : ******************************************************************************/
2042 :
2043 4 : static bool set_dc_type_and_flags_trustinfo( struct winbindd_domain *domain )
2044 : {
2045 0 : struct winbindd_domain *our_domain;
2046 4 : NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2047 0 : WERROR werr;
2048 0 : struct netr_DomainTrustList trusts;
2049 0 : int i;
2050 4 : uint32_t flags = (NETR_TRUST_FLAG_IN_FOREST |
2051 : NETR_TRUST_FLAG_OUTBOUND |
2052 : NETR_TRUST_FLAG_INBOUND);
2053 0 : struct rpc_pipe_client *cli;
2054 4 : TALLOC_CTX *mem_ctx = NULL;
2055 0 : struct dcerpc_binding_handle *b;
2056 :
2057 4 : if (IS_DC) {
2058 : /*
2059 : * On a DC we loaded all trusts
2060 : * from configuration and never learn
2061 : * new domains.
2062 : */
2063 0 : return true;
2064 : }
2065 :
2066 4 : DEBUG(5, ("set_dc_type_and_flags_trustinfo: domain %s\n", domain->name ));
2067 :
2068 : /* Our primary domain doesn't need to worry about trust flags.
2069 : Force it to go through the network setup */
2070 4 : if ( domain->primary ) {
2071 0 : return False;
2072 : }
2073 :
2074 4 : mem_ctx = talloc_stackframe();
2075 4 : our_domain = find_our_domain();
2076 4 : if (our_domain->internal) {
2077 0 : result = init_dc_connection(our_domain, false);
2078 0 : if (!NT_STATUS_IS_OK(result)) {
2079 0 : DEBUG(3,("set_dc_type_and_flags_trustinfo: "
2080 : "Not able to make a connection to our domain: %s\n",
2081 : nt_errstr(result)));
2082 0 : TALLOC_FREE(mem_ctx);
2083 0 : return false;
2084 : }
2085 : }
2086 :
2087 : /* This won't work unless our domain is AD */
2088 4 : if ( !our_domain->active_directory ) {
2089 0 : TALLOC_FREE(mem_ctx);
2090 0 : return False;
2091 : }
2092 :
2093 4 : if (our_domain->internal) {
2094 0 : result = wb_open_internal_pipe(mem_ctx, &ndr_table_netlogon, &cli);
2095 4 : } else if (!connection_ok(our_domain)) {
2096 2 : DEBUG(3,("set_dc_type_and_flags_trustinfo: "
2097 : "No connection to our domain!\n"));
2098 2 : TALLOC_FREE(mem_ctx);
2099 2 : return False;
2100 : } else {
2101 2 : result = cm_connect_netlogon(our_domain, &cli);
2102 : }
2103 :
2104 2 : if (!NT_STATUS_IS_OK(result)) {
2105 0 : DEBUG(5, ("set_dc_type_and_flags_trustinfo: Could not open "
2106 : "a connection to %s for PIPE_NETLOGON (%s)\n",
2107 : domain->name, nt_errstr(result)));
2108 0 : TALLOC_FREE(mem_ctx);
2109 0 : return False;
2110 : }
2111 2 : b = cli->binding_handle;
2112 :
2113 : /* Use DsEnumerateDomainTrusts to get us the trust direction and type. */
2114 2 : result = dcerpc_netr_DsrEnumerateDomainTrusts(b, mem_ctx,
2115 2 : cli->desthost,
2116 : flags,
2117 : &trusts,
2118 : &werr);
2119 2 : if (!NT_STATUS_IS_OK(result)) {
2120 0 : DEBUG(0,("set_dc_type_and_flags_trustinfo: "
2121 : "failed to query trusted domain list: %s\n",
2122 : nt_errstr(result)));
2123 0 : TALLOC_FREE(mem_ctx);
2124 0 : return false;
2125 : }
2126 2 : if (!W_ERROR_IS_OK(werr)) {
2127 0 : DEBUG(0,("set_dc_type_and_flags_trustinfo: "
2128 : "failed to query trusted domain list: %s\n",
2129 : win_errstr(werr)));
2130 0 : TALLOC_FREE(mem_ctx);
2131 0 : return false;
2132 : }
2133 :
2134 : /* Now find the domain name and get the flags */
2135 :
2136 2 : for ( i=0; i<trusts.count; i++ ) {
2137 2 : if ( strequal( domain->name, trusts.array[i].netbios_name) ) {
2138 2 : domain->domain_flags = trusts.array[i].trust_flags;
2139 2 : domain->domain_type = trusts.array[i].trust_type;
2140 2 : domain->domain_trust_attribs = trusts.array[i].trust_attributes;
2141 :
2142 2 : if ( domain->domain_type == LSA_TRUST_TYPE_UPLEVEL )
2143 2 : domain->active_directory = True;
2144 :
2145 : /* This flag is only set if the domain is *our*
2146 : primary domain and the primary domain is in
2147 : native mode */
2148 :
2149 2 : domain->native_mode = (domain->domain_flags & NETR_TRUST_FLAG_NATIVE);
2150 :
2151 2 : DEBUG(5, ("set_dc_type_and_flags_trustinfo: domain %s is %sin "
2152 : "native mode.\n", domain->name,
2153 : domain->native_mode ? "" : "NOT "));
2154 :
2155 2 : DEBUG(5,("set_dc_type_and_flags_trustinfo: domain %s is %s"
2156 : "running active directory.\n", domain->name,
2157 : domain->active_directory ? "" : "NOT "));
2158 :
2159 2 : domain->can_do_ncacn_ip_tcp = domain->active_directory;
2160 :
2161 2 : domain->initialized = True;
2162 :
2163 2 : break;
2164 : }
2165 : }
2166 :
2167 2 : TALLOC_FREE(mem_ctx);
2168 :
2169 2 : return domain->initialized;
2170 : }
2171 :
2172 : /******************************************************************************
2173 : We can 'sense' certain things about the DC by it's replies to certain
2174 : questions.
2175 :
2176 : This tells us if this particular remote server is Active Directory, and if it
2177 : is native mode.
2178 : ******************************************************************************/
2179 :
2180 0 : static void set_dc_type_and_flags_connect( struct winbindd_domain *domain )
2181 : {
2182 0 : NTSTATUS status, result;
2183 0 : WERROR werr;
2184 0 : TALLOC_CTX *mem_ctx = NULL;
2185 0 : struct rpc_pipe_client *cli = NULL;
2186 0 : struct policy_handle pol;
2187 0 : union dssetup_DsRoleInfo info;
2188 0 : union lsa_PolicyInformation *lsa_info = NULL;
2189 :
2190 0 : if (!domain->internal && !connection_ok(domain)) {
2191 0 : return;
2192 : }
2193 :
2194 0 : mem_ctx = talloc_init("set_dc_type_and_flags on domain %s\n",
2195 : domain->name);
2196 0 : if (!mem_ctx) {
2197 0 : DEBUG(1, ("set_dc_type_and_flags_connect: talloc_init() failed\n"));
2198 0 : return;
2199 : }
2200 :
2201 0 : DEBUG(5, ("set_dc_type_and_flags_connect: domain %s\n", domain->name ));
2202 :
2203 0 : if (domain->internal) {
2204 0 : status = wb_open_internal_pipe(mem_ctx,
2205 : &ndr_table_dssetup,
2206 : &cli);
2207 : } else {
2208 0 : status = cli_rpc_pipe_open_noauth(domain->conn.cli,
2209 : &ndr_table_dssetup,
2210 : &cli);
2211 : }
2212 :
2213 0 : if (!NT_STATUS_IS_OK(status)) {
2214 0 : DEBUG(5, ("set_dc_type_and_flags_connect: Could not bind to "
2215 : "PI_DSSETUP on domain %s: (%s)\n",
2216 : domain->name, nt_errstr(status)));
2217 :
2218 : /* if this is just a non-AD domain we need to continue
2219 : * identifying so that we can in the end return with
2220 : * domain->initialized = True - gd */
2221 :
2222 0 : goto no_dssetup;
2223 : }
2224 :
2225 0 : status = dcerpc_dssetup_DsRoleGetPrimaryDomainInformation(cli->binding_handle, mem_ctx,
2226 : DS_ROLE_BASIC_INFORMATION,
2227 : &info,
2228 : &werr);
2229 0 : TALLOC_FREE(cli);
2230 :
2231 0 : if (NT_STATUS_IS_OK(status)) {
2232 0 : result = werror_to_ntstatus(werr);
2233 : }
2234 0 : if (!NT_STATUS_IS_OK(status)) {
2235 0 : DEBUG(5, ("set_dc_type_and_flags_connect: rpccli_ds_getprimarydominfo "
2236 : "on domain %s failed: (%s)\n",
2237 : domain->name, nt_errstr(status)));
2238 :
2239 : /* older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for
2240 : * every opcode on the DSSETUP pipe, continue with
2241 : * no_dssetup mode here as well to get domain->initialized
2242 : * set - gd */
2243 :
2244 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
2245 0 : goto no_dssetup;
2246 : }
2247 :
2248 0 : TALLOC_FREE(mem_ctx);
2249 0 : return;
2250 : }
2251 :
2252 0 : if ((info.basic.flags & DS_ROLE_PRIMARY_DS_RUNNING) &&
2253 0 : !(info.basic.flags & DS_ROLE_PRIMARY_DS_MIXED_MODE)) {
2254 0 : domain->native_mode = True;
2255 : } else {
2256 0 : domain->native_mode = False;
2257 : }
2258 :
2259 0 : no_dssetup:
2260 0 : if (domain->internal) {
2261 0 : status = wb_open_internal_pipe(mem_ctx,
2262 : &ndr_table_lsarpc,
2263 : &cli);
2264 : } else {
2265 0 : status = cli_rpc_pipe_open_noauth(domain->conn.cli,
2266 : &ndr_table_lsarpc, &cli);
2267 : }
2268 0 : if (!NT_STATUS_IS_OK(status)) {
2269 0 : DEBUG(5, ("set_dc_type_and_flags_connect: Could not bind to "
2270 : "PI_LSARPC on domain %s: (%s)\n",
2271 : domain->name, nt_errstr(status)));
2272 0 : TALLOC_FREE(cli);
2273 0 : TALLOC_FREE(mem_ctx);
2274 0 : return;
2275 : }
2276 :
2277 0 : status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
2278 : SEC_FLAG_MAXIMUM_ALLOWED, &pol);
2279 :
2280 0 : if (NT_STATUS_IS_OK(status)) {
2281 : /* This particular query is exactly what Win2k clients use
2282 : to determine that the DC is active directory */
2283 0 : status = dcerpc_lsa_QueryInfoPolicy2(cli->binding_handle, mem_ctx,
2284 : &pol,
2285 : LSA_POLICY_INFO_DNS,
2286 : &lsa_info,
2287 : &result);
2288 : }
2289 :
2290 0 : if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2291 0 : domain->active_directory = True;
2292 :
2293 0 : if (lsa_info->dns.name.string) {
2294 0 : if (!strequal(domain->name, lsa_info->dns.name.string))
2295 : {
2296 0 : DEBUG(1, ("set_dc_type_and_flags_connect: DC "
2297 : "for domain %s claimed it was a DC "
2298 : "for domain %s, refusing to "
2299 : "initialize\n",
2300 : domain->name,
2301 : lsa_info->dns.name.string));
2302 0 : TALLOC_FREE(cli);
2303 0 : TALLOC_FREE(mem_ctx);
2304 0 : return;
2305 : }
2306 0 : talloc_free(domain->name);
2307 0 : domain->name = talloc_strdup(domain,
2308 0 : lsa_info->dns.name.string);
2309 0 : if (domain->name == NULL) {
2310 0 : goto done;
2311 : }
2312 : }
2313 :
2314 0 : if (lsa_info->dns.dns_domain.string) {
2315 0 : if (domain->alt_name != NULL &&
2316 0 : !strequal(domain->alt_name,
2317 0 : lsa_info->dns.dns_domain.string))
2318 : {
2319 0 : DEBUG(1, ("set_dc_type_and_flags_connect: DC "
2320 : "for domain %s (%s) claimed it was "
2321 : "a DC for domain %s, refusing to "
2322 : "initialize\n",
2323 : domain->alt_name, domain->name,
2324 : lsa_info->dns.dns_domain.string));
2325 0 : TALLOC_FREE(cli);
2326 0 : TALLOC_FREE(mem_ctx);
2327 0 : return;
2328 : }
2329 0 : talloc_free(domain->alt_name);
2330 0 : domain->alt_name =
2331 0 : talloc_strdup(domain,
2332 0 : lsa_info->dns.dns_domain.string);
2333 0 : if (domain->alt_name == NULL) {
2334 0 : goto done;
2335 : }
2336 : }
2337 :
2338 : /* See if we can set some domain trust flags about
2339 : ourself */
2340 :
2341 0 : if (lsa_info->dns.dns_forest.string) {
2342 0 : talloc_free(domain->forest_name);
2343 0 : domain->forest_name =
2344 0 : talloc_strdup(domain,
2345 0 : lsa_info->dns.dns_forest.string);
2346 0 : if (domain->forest_name == NULL) {
2347 0 : goto done;
2348 : }
2349 :
2350 0 : if (strequal(domain->forest_name, domain->alt_name)) {
2351 0 : domain->domain_flags |= NETR_TRUST_FLAG_TREEROOT;
2352 : }
2353 : }
2354 :
2355 0 : if (lsa_info->dns.sid) {
2356 0 : if (!is_null_sid(&domain->sid) &&
2357 0 : !dom_sid_equal(&domain->sid,
2358 0 : lsa_info->dns.sid))
2359 : {
2360 0 : struct dom_sid_buf buf1, buf2;
2361 0 : DEBUG(1, ("set_dc_type_and_flags_connect: DC "
2362 : "for domain %s (%s) claimed it was "
2363 : "a DC for domain %s, refusing to "
2364 : "initialize\n",
2365 : dom_sid_str_buf(&domain->sid, &buf1),
2366 : domain->name,
2367 : dom_sid_str_buf(lsa_info->dns.sid,
2368 : &buf2)));
2369 0 : TALLOC_FREE(cli);
2370 0 : TALLOC_FREE(mem_ctx);
2371 0 : return;
2372 : }
2373 0 : sid_copy(&domain->sid, lsa_info->dns.sid);
2374 : }
2375 : } else {
2376 0 : domain->active_directory = False;
2377 :
2378 0 : status = rpccli_lsa_open_policy(cli, mem_ctx, True,
2379 : SEC_FLAG_MAXIMUM_ALLOWED,
2380 : &pol);
2381 :
2382 0 : if (!NT_STATUS_IS_OK(status)) {
2383 0 : goto done;
2384 : }
2385 :
2386 0 : status = dcerpc_lsa_QueryInfoPolicy(cli->binding_handle, mem_ctx,
2387 : &pol,
2388 : LSA_POLICY_INFO_ACCOUNT_DOMAIN,
2389 : &lsa_info,
2390 : &result);
2391 0 : if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2392 :
2393 0 : if (lsa_info->account_domain.name.string) {
2394 0 : if (!strequal(domain->name,
2395 0 : lsa_info->account_domain.name.string))
2396 : {
2397 0 : DEBUG(1,
2398 : ("set_dc_type_and_flags_connect: "
2399 : "DC for domain %s claimed it was"
2400 : " a DC for domain %s, refusing "
2401 : "to initialize\n", domain->name,
2402 : lsa_info->
2403 : account_domain.name.string));
2404 0 : TALLOC_FREE(cli);
2405 0 : TALLOC_FREE(mem_ctx);
2406 0 : return;
2407 : }
2408 0 : talloc_free(domain->name);
2409 0 : domain->name =
2410 0 : talloc_strdup(domain,
2411 0 : lsa_info->account_domain.name.string);
2412 : }
2413 :
2414 0 : if (lsa_info->account_domain.sid) {
2415 0 : if (!is_null_sid(&domain->sid) &&
2416 0 : !dom_sid_equal(&domain->sid,
2417 0 : lsa_info->account_domain.sid))
2418 : {
2419 0 : struct dom_sid_buf buf1, buf2;
2420 0 : DEBUG(1,
2421 : ("set_dc_type_and_flags_connect: "
2422 : "DC for domain %s (%s) claimed "
2423 : "it was a DC for domain %s, "
2424 : "refusing to initialize\n",
2425 : dom_sid_str_buf(
2426 : &domain->sid, &buf1),
2427 : domain->name,
2428 : dom_sid_str_buf(
2429 : lsa_info->account_domain.sid,
2430 : &buf2)));
2431 0 : TALLOC_FREE(cli);
2432 0 : TALLOC_FREE(mem_ctx);
2433 0 : return;
2434 : }
2435 0 : sid_copy(&domain->sid, lsa_info->account_domain.sid);
2436 : }
2437 : }
2438 : }
2439 0 : done:
2440 :
2441 0 : DEBUG(5, ("set_dc_type_and_flags_connect: domain %s is %sin native mode.\n",
2442 : domain->name, domain->native_mode ? "" : "NOT "));
2443 :
2444 0 : DEBUG(5,("set_dc_type_and_flags_connect: domain %s is %srunning active directory.\n",
2445 : domain->name, domain->active_directory ? "" : "NOT "));
2446 :
2447 0 : domain->can_do_ncacn_ip_tcp = domain->active_directory;
2448 :
2449 0 : TALLOC_FREE(cli);
2450 :
2451 0 : TALLOC_FREE(mem_ctx);
2452 :
2453 0 : domain->initialized = True;
2454 : }
2455 :
2456 : /**********************************************************************
2457 : Set the domain_flags (trust attributes, domain operating modes, etc...
2458 : ***********************************************************************/
2459 :
2460 2 : static void set_dc_type_and_flags( struct winbindd_domain *domain )
2461 : {
2462 2 : if (IS_DC) {
2463 : /*
2464 : * On a DC we loaded all trusts
2465 : * from configuration and never learn
2466 : * new domains.
2467 : */
2468 0 : return;
2469 : }
2470 :
2471 : /* we always have to contact our primary domain */
2472 :
2473 2 : if ( domain->primary || domain->internal) {
2474 0 : DEBUG(10,("set_dc_type_and_flags: setting up flags for "
2475 : "primary or internal domain\n"));
2476 0 : set_dc_type_and_flags_connect( domain );
2477 0 : return;
2478 : }
2479 :
2480 : /* Use our DC to get the information if possible */
2481 :
2482 2 : if ( !set_dc_type_and_flags_trustinfo( domain ) ) {
2483 : /* Otherwise, fallback to contacting the
2484 : domain directly */
2485 0 : set_dc_type_and_flags_connect( domain );
2486 : }
2487 :
2488 2 : return;
2489 : }
2490 :
2491 :
2492 :
2493 : /**********************************************************************
2494 : ***********************************************************************/
2495 :
2496 0 : static NTSTATUS cm_get_schannel_creds(struct winbindd_domain *domain,
2497 : struct netlogon_creds_cli_context **ppdc)
2498 : {
2499 0 : NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2500 0 : struct rpc_pipe_client *netlogon_pipe;
2501 :
2502 0 : *ppdc = NULL;
2503 :
2504 0 : if ((!IS_DC) && (!domain->primary)) {
2505 0 : return NT_STATUS_TRUSTED_DOMAIN_FAILURE;
2506 : }
2507 :
2508 0 : if (domain->conn.netlogon_creds_ctx != NULL) {
2509 0 : *ppdc = domain->conn.netlogon_creds_ctx;
2510 0 : return NT_STATUS_OK;
2511 : }
2512 :
2513 0 : result = cm_connect_netlogon_secure(domain, &netlogon_pipe, ppdc);
2514 0 : if (!NT_STATUS_IS_OK(result)) {
2515 0 : return result;
2516 : }
2517 :
2518 0 : return NT_STATUS_OK;
2519 : }
2520 :
2521 0 : NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
2522 : bool need_rw_dc,
2523 : struct rpc_pipe_client **cli, struct policy_handle *sam_handle)
2524 : {
2525 0 : struct winbindd_cm_conn *conn;
2526 0 : NTSTATUS status, result;
2527 0 : struct netlogon_creds_cli_context *p_creds;
2528 0 : struct cli_credentials *creds = NULL;
2529 0 : bool retry = false; /* allow one retry attempt for expired session */
2530 0 : const char *remote_name = NULL;
2531 0 : const struct sockaddr_storage *remote_sockaddr = NULL;
2532 0 : bool sealed_pipes = true;
2533 0 : bool strong_key = true;
2534 :
2535 0 : if (sid_check_is_our_sam(&domain->sid)) {
2536 0 : if (domain->rodc == false || need_rw_dc == false) {
2537 0 : return open_internal_samr_conn(mem_ctx, domain, cli, sam_handle);
2538 : }
2539 : }
2540 :
2541 0 : if (IS_AD_DC) {
2542 : /*
2543 : * In theory we should not use SAMR within
2544 : * winbindd at all, but that's a larger task to
2545 : * remove this and avoid breaking existing
2546 : * setups.
2547 : *
2548 : * At least as AD DC we have the restriction
2549 : * to avoid SAMR against trusted domains,
2550 : * as there're no existing setups.
2551 : */
2552 0 : return NT_STATUS_REQUEST_NOT_ACCEPTED;
2553 : }
2554 :
2555 0 : retry:
2556 0 : status = init_dc_connection_rpc(domain, need_rw_dc);
2557 0 : if (!NT_STATUS_IS_OK(status)) {
2558 0 : return status;
2559 : }
2560 :
2561 0 : conn = &domain->conn;
2562 :
2563 0 : if (rpccli_is_connected(conn->samr_pipe)) {
2564 0 : goto done;
2565 : }
2566 :
2567 0 : TALLOC_FREE(conn->samr_pipe);
2568 :
2569 : /*
2570 : * No SAMR pipe yet. Attempt to get an NTLMSSP SPNEGO authenticated
2571 : * sign and sealed pipe using the machine account password by
2572 : * preference. If we can't - try schannel, if that fails, try
2573 : * anonymous.
2574 : */
2575 :
2576 0 : result = get_trust_credentials(domain, talloc_tos(), false, &creds);
2577 0 : if (!NT_STATUS_IS_OK(result)) {
2578 0 : DEBUG(10, ("cm_connect_sam: No user available for "
2579 : "domain %s, trying schannel\n", domain->name));
2580 0 : goto schannel;
2581 : }
2582 :
2583 0 : if (cli_credentials_is_anonymous(creds)) {
2584 0 : goto anonymous;
2585 : }
2586 :
2587 0 : remote_name = smbXcli_conn_remote_name(conn->cli->conn);
2588 0 : remote_sockaddr = smbXcli_conn_remote_sockaddr(conn->cli->conn);
2589 :
2590 : /*
2591 : * We have an authenticated connection. Use a SPNEGO
2592 : * authenticated SAMR pipe with sign & seal.
2593 : */
2594 0 : status = cli_rpc_pipe_open_with_creds(conn->cli,
2595 : &ndr_table_samr,
2596 : NCACN_NP,
2597 : DCERPC_AUTH_TYPE_SPNEGO,
2598 : conn->auth_level,
2599 : remote_name,
2600 : remote_sockaddr,
2601 : creds,
2602 : &conn->samr_pipe);
2603 :
2604 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)
2605 0 : && !retry) {
2606 0 : invalidate_cm_connection(domain);
2607 0 : retry = true;
2608 0 : goto retry;
2609 : }
2610 :
2611 0 : if (!NT_STATUS_IS_OK(status)) {
2612 0 : DEBUG(10,("cm_connect_sam: failed to connect to SAMR "
2613 : "pipe for domain %s using NTLMSSP "
2614 : "authenticated pipe: user %s. Error was "
2615 : "%s\n", domain->name,
2616 : cli_credentials_get_unparsed_name(creds, talloc_tos()),
2617 : nt_errstr(status)));
2618 0 : goto schannel;
2619 : }
2620 :
2621 0 : DEBUG(10,("cm_connect_sam: connected to SAMR pipe for "
2622 : "domain %s using NTLMSSP authenticated "
2623 : "pipe: user %s\n", domain->name,
2624 : cli_credentials_get_unparsed_name(creds, talloc_tos())));
2625 :
2626 0 : status = dcerpc_samr_Connect2(conn->samr_pipe->binding_handle, mem_ctx,
2627 0 : conn->samr_pipe->desthost,
2628 : SEC_FLAG_MAXIMUM_ALLOWED,
2629 : &conn->sam_connect_handle,
2630 : &result);
2631 :
2632 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_IO_DEVICE_ERROR) && !retry) {
2633 0 : invalidate_cm_connection(domain);
2634 0 : TALLOC_FREE(conn->samr_pipe);
2635 0 : retry = true;
2636 0 : goto retry;
2637 : }
2638 :
2639 0 : if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2640 0 : goto open_domain;
2641 : }
2642 0 : if (NT_STATUS_IS_OK(status)) {
2643 0 : status = result;
2644 : }
2645 :
2646 0 : DEBUG(10,("cm_connect_sam: ntlmssp-sealed dcerpc_samr_Connect2 "
2647 : "failed for domain %s, error was %s. Trying schannel\n",
2648 : domain->name, nt_errstr(status) ));
2649 0 : TALLOC_FREE(conn->samr_pipe);
2650 :
2651 0 : schannel:
2652 :
2653 : /* Fall back to schannel if it's a W2K pre-SP1 box. */
2654 :
2655 0 : status = cm_get_schannel_creds(domain, &p_creds);
2656 0 : if (!NT_STATUS_IS_OK(status)) {
2657 : /* If this call fails - conn->cli can now be NULL ! */
2658 0 : DEBUG(10, ("cm_connect_sam: Could not get schannel auth info "
2659 : "for domain %s (error %s), trying anon\n",
2660 : domain->name,
2661 : nt_errstr(status) ));
2662 0 : goto anonymous;
2663 : }
2664 0 : TALLOC_FREE(creds);
2665 0 : status = cli_rpc_pipe_open_schannel_with_creds(
2666 : conn->cli, &ndr_table_samr, NCACN_NP, p_creds,
2667 : remote_name,
2668 : remote_sockaddr,
2669 : &conn->samr_pipe);
2670 :
2671 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)
2672 0 : && !retry) {
2673 0 : invalidate_cm_connection(domain);
2674 0 : retry = true;
2675 0 : goto retry;
2676 : }
2677 :
2678 0 : if (!NT_STATUS_IS_OK(status)) {
2679 0 : DEBUG(10,("cm_connect_sam: failed to connect to SAMR pipe for "
2680 : "domain %s using schannel. Error was %s\n",
2681 : domain->name, nt_errstr(status) ));
2682 0 : goto anonymous;
2683 : }
2684 0 : DEBUG(10,("cm_connect_sam: connected to SAMR pipe for domain %s using "
2685 : "schannel.\n", domain->name ));
2686 :
2687 0 : status = dcerpc_samr_Connect2(conn->samr_pipe->binding_handle, mem_ctx,
2688 0 : conn->samr_pipe->desthost,
2689 : SEC_FLAG_MAXIMUM_ALLOWED,
2690 : &conn->sam_connect_handle,
2691 : &result);
2692 :
2693 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_IO_DEVICE_ERROR) && !retry) {
2694 0 : invalidate_cm_connection(domain);
2695 0 : TALLOC_FREE(conn->samr_pipe);
2696 0 : retry = true;
2697 0 : goto retry;
2698 : }
2699 :
2700 0 : if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2701 0 : goto open_domain;
2702 : }
2703 0 : if (NT_STATUS_IS_OK(status)) {
2704 0 : status = result;
2705 : }
2706 0 : DEBUG(10,("cm_connect_sam: schannel-sealed dcerpc_samr_Connect2 failed "
2707 : "for domain %s, error was %s. Trying anonymous\n",
2708 : domain->name, nt_errstr(status) ));
2709 0 : TALLOC_FREE(conn->samr_pipe);
2710 :
2711 0 : anonymous:
2712 :
2713 0 : sealed_pipes = lp_winbind_sealed_pipes();
2714 0 : sealed_pipes = lp_parm_bool(-1, "winbind sealed pipes",
2715 0 : domain->name,
2716 : sealed_pipes);
2717 0 : strong_key = lp_require_strong_key();
2718 0 : strong_key = lp_parm_bool(-1, "require strong key",
2719 0 : domain->name,
2720 : strong_key);
2721 :
2722 : /* Finally fall back to anonymous. */
2723 0 : if (sealed_pipes || strong_key) {
2724 0 : status = NT_STATUS_DOWNGRADE_DETECTED;
2725 0 : DEBUG(1, ("Unwilling to make SAMR connection to domain %s "
2726 : "without connection level security, "
2727 : "must set 'winbind sealed pipes:%s = false' and "
2728 : "'require strong key:%s = false' to proceed: %s\n",
2729 : domain->name, domain->name, domain->name,
2730 : nt_errstr(status)));
2731 0 : goto done;
2732 : }
2733 0 : status = cli_rpc_pipe_open_noauth(conn->cli, &ndr_table_samr,
2734 : &conn->samr_pipe);
2735 :
2736 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)
2737 0 : && !retry) {
2738 0 : invalidate_cm_connection(domain);
2739 0 : retry = true;
2740 0 : goto retry;
2741 : }
2742 :
2743 0 : if (!NT_STATUS_IS_OK(status)) {
2744 0 : goto done;
2745 : }
2746 :
2747 0 : status = dcerpc_samr_Connect2(conn->samr_pipe->binding_handle, mem_ctx,
2748 0 : conn->samr_pipe->desthost,
2749 : SEC_FLAG_MAXIMUM_ALLOWED,
2750 : &conn->sam_connect_handle,
2751 : &result);
2752 :
2753 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_IO_DEVICE_ERROR) && !retry) {
2754 0 : invalidate_cm_connection(domain);
2755 0 : TALLOC_FREE(conn->samr_pipe);
2756 0 : retry = true;
2757 0 : goto retry;
2758 : }
2759 :
2760 0 : if (!NT_STATUS_IS_OK(status)) {
2761 0 : DEBUG(10,("cm_connect_sam: rpccli_samr_Connect2 failed "
2762 : "for domain %s Error was %s\n",
2763 : domain->name, nt_errstr(status) ));
2764 0 : goto done;
2765 : }
2766 0 : if (!NT_STATUS_IS_OK(result)) {
2767 0 : status = result;
2768 0 : DEBUG(10,("cm_connect_sam: dcerpc_samr_Connect2 failed "
2769 : "for domain %s Error was %s\n",
2770 : domain->name, nt_errstr(result)));
2771 0 : goto done;
2772 : }
2773 :
2774 0 : open_domain:
2775 0 : status = dcerpc_samr_OpenDomain(conn->samr_pipe->binding_handle,
2776 : mem_ctx,
2777 : &conn->sam_connect_handle,
2778 : SEC_FLAG_MAXIMUM_ALLOWED,
2779 : &domain->sid,
2780 : &conn->sam_domain_handle,
2781 : &result);
2782 0 : if (!NT_STATUS_IS_OK(status)) {
2783 0 : goto done;
2784 : }
2785 :
2786 0 : status = result;
2787 0 : done:
2788 :
2789 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
2790 : /*
2791 : * if we got access denied, we might just have no access rights
2792 : * to talk to the remote samr server server (e.g. when we are a
2793 : * PDC and we are connecting a w2k8 pdc via an interdomain
2794 : * trust). In that case do not invalidate the whole connection
2795 : * stack
2796 : */
2797 0 : TALLOC_FREE(conn->samr_pipe);
2798 0 : ZERO_STRUCT(conn->sam_domain_handle);
2799 0 : return status;
2800 0 : } else if (!NT_STATUS_IS_OK(status)) {
2801 0 : invalidate_cm_connection(domain);
2802 0 : return status;
2803 : }
2804 :
2805 0 : *cli = conn->samr_pipe;
2806 0 : *sam_handle = conn->sam_domain_handle;
2807 0 : return status;
2808 : }
2809 :
2810 : /**********************************************************************
2811 : open an schanneld ncacn_ip_tcp connection to LSA
2812 : ***********************************************************************/
2813 :
2814 0 : static NTSTATUS cm_connect_lsa_tcp(struct winbindd_domain *domain,
2815 : TALLOC_CTX *mem_ctx,
2816 : struct rpc_pipe_client **cli)
2817 : {
2818 0 : struct winbindd_cm_conn *conn;
2819 0 : struct netlogon_creds_cli_context *p_creds = NULL;
2820 0 : NTSTATUS status;
2821 0 : const char *remote_name = NULL;
2822 0 : const struct sockaddr_storage *remote_sockaddr = NULL;
2823 :
2824 0 : DEBUG(10,("cm_connect_lsa_tcp\n"));
2825 :
2826 0 : status = init_dc_connection_rpc(domain, false);
2827 0 : if (!NT_STATUS_IS_OK(status)) {
2828 0 : return status;
2829 : }
2830 :
2831 0 : conn = &domain->conn;
2832 :
2833 : /*
2834 : * rpccli_is_connected handles more error cases
2835 : */
2836 0 : if (rpccli_is_connected(conn->lsa_pipe_tcp) &&
2837 0 : conn->lsa_pipe_tcp->transport->transport == NCACN_IP_TCP &&
2838 0 : conn->lsa_pipe_tcp->auth->auth_level >= DCERPC_AUTH_LEVEL_INTEGRITY) {
2839 0 : goto done;
2840 : }
2841 :
2842 0 : TALLOC_FREE(conn->lsa_pipe_tcp);
2843 :
2844 0 : status = cm_get_schannel_creds(domain, &p_creds);
2845 0 : if (!NT_STATUS_IS_OK(status)) {
2846 0 : goto done;
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 0 : status = cli_rpc_pipe_open_schannel_with_creds(
2853 : conn->cli,
2854 : &ndr_table_lsarpc,
2855 : NCACN_IP_TCP,
2856 : p_creds,
2857 : remote_name,
2858 : remote_sockaddr,
2859 : &conn->lsa_pipe_tcp);
2860 0 : if (!NT_STATUS_IS_OK(status)) {
2861 0 : DEBUG(10,("cli_rpc_pipe_open_schannel_with_key failed: %s\n",
2862 : nt_errstr(status)));
2863 0 : goto done;
2864 : }
2865 :
2866 0 : done:
2867 0 : if (!NT_STATUS_IS_OK(status)) {
2868 0 : TALLOC_FREE(conn->lsa_pipe_tcp);
2869 0 : return status;
2870 : }
2871 :
2872 0 : *cli = conn->lsa_pipe_tcp;
2873 :
2874 0 : return status;
2875 : }
2876 :
2877 0 : NTSTATUS cm_connect_lsa(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
2878 : struct rpc_pipe_client **cli, struct policy_handle *lsa_policy)
2879 : {
2880 0 : struct winbindd_cm_conn *conn;
2881 0 : NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2882 0 : struct netlogon_creds_cli_context *p_creds;
2883 0 : struct cli_credentials *creds = NULL;
2884 0 : bool retry = false; /* allow one retry attempt for expired session */
2885 0 : const char *remote_name = NULL;
2886 0 : const struct sockaddr_storage *remote_sockaddr = NULL;
2887 0 : bool sealed_pipes = true;
2888 0 : bool strong_key = true;
2889 :
2890 0 : retry:
2891 0 : result = init_dc_connection_rpc(domain, false);
2892 0 : if (!NT_STATUS_IS_OK(result))
2893 0 : return result;
2894 :
2895 0 : conn = &domain->conn;
2896 :
2897 0 : if (rpccli_is_connected(conn->lsa_pipe)) {
2898 0 : goto done;
2899 : }
2900 :
2901 0 : TALLOC_FREE(conn->lsa_pipe);
2902 :
2903 0 : if (IS_AD_DC) {
2904 : /*
2905 : * Make sure we only use schannel as AD DC.
2906 : */
2907 0 : goto schannel;
2908 : }
2909 :
2910 0 : result = get_trust_credentials(domain, talloc_tos(), false, &creds);
2911 0 : if (!NT_STATUS_IS_OK(result)) {
2912 0 : DEBUG(10, ("cm_connect_lsa: No user available for "
2913 : "domain %s, trying schannel\n", domain->name));
2914 0 : goto schannel;
2915 : }
2916 :
2917 0 : if (cli_credentials_is_anonymous(creds)) {
2918 0 : goto anonymous;
2919 : }
2920 :
2921 0 : remote_name = smbXcli_conn_remote_name(conn->cli->conn);
2922 0 : remote_sockaddr = smbXcli_conn_remote_sockaddr(conn->cli->conn);
2923 :
2924 : /*
2925 : * We have an authenticated connection. Use a SPNEGO
2926 : * authenticated LSA pipe with sign & seal.
2927 : */
2928 0 : result = cli_rpc_pipe_open_with_creds
2929 : (conn->cli, &ndr_table_lsarpc, NCACN_NP,
2930 : DCERPC_AUTH_TYPE_SPNEGO,
2931 : conn->auth_level,
2932 : remote_name,
2933 : remote_sockaddr,
2934 : creds,
2935 : &conn->lsa_pipe);
2936 :
2937 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_NETWORK_SESSION_EXPIRED)
2938 0 : && !retry) {
2939 0 : invalidate_cm_connection(domain);
2940 0 : retry = true;
2941 0 : goto retry;
2942 : }
2943 :
2944 0 : if (!NT_STATUS_IS_OK(result)) {
2945 0 : DEBUG(10,("cm_connect_lsa: failed to connect to LSA pipe for "
2946 : "domain %s using NTLMSSP authenticated pipe: user "
2947 : "%s. Error was %s. Trying schannel.\n",
2948 : domain->name,
2949 : cli_credentials_get_unparsed_name(creds, talloc_tos()),
2950 : nt_errstr(result)));
2951 0 : goto schannel;
2952 : }
2953 :
2954 0 : DEBUG(10,("cm_connect_lsa: connected to LSA pipe for domain %s using "
2955 : "NTLMSSP authenticated pipe: user %s\n",
2956 : domain->name, cli_credentials_get_unparsed_name(creds, talloc_tos())));
2957 :
2958 0 : result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
2959 : SEC_FLAG_MAXIMUM_ALLOWED,
2960 : &conn->lsa_policy);
2961 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_IO_DEVICE_ERROR) && !retry) {
2962 0 : invalidate_cm_connection(domain);
2963 0 : TALLOC_FREE(conn->lsa_pipe);
2964 0 : retry = true;
2965 0 : goto retry;
2966 : }
2967 :
2968 0 : if (NT_STATUS_IS_OK(result)) {
2969 0 : goto done;
2970 : }
2971 :
2972 0 : DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
2973 : "schannel\n"));
2974 :
2975 0 : TALLOC_FREE(conn->lsa_pipe);
2976 :
2977 0 : schannel:
2978 :
2979 : /* Fall back to schannel if it's a W2K pre-SP1 box. */
2980 :
2981 0 : result = cm_get_schannel_creds(domain, &p_creds);
2982 0 : if (!NT_STATUS_IS_OK(result)) {
2983 : /* If this call fails - conn->cli can now be NULL ! */
2984 0 : DEBUG(10, ("cm_connect_lsa: Could not get schannel auth info "
2985 : "for domain %s (error %s), trying anon\n",
2986 : domain->name,
2987 : nt_errstr(result) ));
2988 0 : goto anonymous;
2989 : }
2990 :
2991 0 : TALLOC_FREE(creds);
2992 0 : result = cli_rpc_pipe_open_schannel_with_creds(
2993 : conn->cli, &ndr_table_lsarpc, NCACN_NP, p_creds,
2994 : remote_name,
2995 : remote_sockaddr,
2996 : &conn->lsa_pipe);
2997 :
2998 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_NETWORK_SESSION_EXPIRED)
2999 0 : && !retry) {
3000 0 : invalidate_cm_connection(domain);
3001 0 : retry = true;
3002 0 : goto retry;
3003 : }
3004 :
3005 0 : if (!NT_STATUS_IS_OK(result)) {
3006 0 : DEBUG(10,("cm_connect_lsa: failed to connect to LSA pipe for "
3007 : "domain %s using schannel. Error was %s\n",
3008 : domain->name, nt_errstr(result) ));
3009 0 : goto anonymous;
3010 : }
3011 0 : DEBUG(10,("cm_connect_lsa: connected to LSA pipe for domain %s using "
3012 : "schannel.\n", domain->name ));
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 : if (NT_STATUS_IS_OK(result)) {
3026 0 : goto done;
3027 : }
3028 :
3029 0 : if (IS_AD_DC) {
3030 : /*
3031 : * Make sure we only use schannel as AD DC.
3032 : */
3033 0 : goto done;
3034 : }
3035 :
3036 0 : DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
3037 : "anonymous\n"));
3038 :
3039 0 : TALLOC_FREE(conn->lsa_pipe);
3040 :
3041 0 : anonymous:
3042 :
3043 0 : if (IS_AD_DC) {
3044 : /*
3045 : * Make sure we only use schannel as AD DC.
3046 : */
3047 0 : goto done;
3048 : }
3049 :
3050 0 : sealed_pipes = lp_winbind_sealed_pipes();
3051 0 : sealed_pipes = lp_parm_bool(-1, "winbind sealed pipes",
3052 0 : domain->name,
3053 : sealed_pipes);
3054 0 : strong_key = lp_require_strong_key();
3055 0 : strong_key = lp_parm_bool(-1, "require strong key",
3056 0 : domain->name,
3057 : strong_key);
3058 :
3059 : /* Finally fall back to anonymous. */
3060 0 : if (sealed_pipes || strong_key) {
3061 0 : result = NT_STATUS_DOWNGRADE_DETECTED;
3062 0 : DEBUG(1, ("Unwilling to make LSA connection to domain %s "
3063 : "without connection level security, "
3064 : "must set 'winbind sealed pipes:%s = false' and "
3065 : "'require strong key:%s = false' to proceed: %s\n",
3066 : domain->name, domain->name, domain->name,
3067 : nt_errstr(result)));
3068 0 : goto done;
3069 : }
3070 :
3071 0 : result = cli_rpc_pipe_open_noauth(conn->cli,
3072 : &ndr_table_lsarpc,
3073 : &conn->lsa_pipe);
3074 :
3075 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_NETWORK_SESSION_EXPIRED)
3076 0 : && !retry) {
3077 0 : invalidate_cm_connection(domain);
3078 0 : retry = true;
3079 0 : goto retry;
3080 : }
3081 :
3082 0 : if (!NT_STATUS_IS_OK(result)) {
3083 0 : goto done;
3084 : }
3085 :
3086 0 : result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
3087 : SEC_FLAG_MAXIMUM_ALLOWED,
3088 : &conn->lsa_policy);
3089 :
3090 0 : if (NT_STATUS_EQUAL(result, NT_STATUS_IO_DEVICE_ERROR) && !retry) {
3091 0 : invalidate_cm_connection(domain);
3092 0 : TALLOC_FREE(conn->lsa_pipe);
3093 0 : retry = true;
3094 0 : goto retry;
3095 : }
3096 :
3097 0 : done:
3098 0 : if (!NT_STATUS_IS_OK(result)) {
3099 0 : invalidate_cm_connection(domain);
3100 0 : return result;
3101 : }
3102 :
3103 0 : *cli = conn->lsa_pipe;
3104 0 : *lsa_policy = conn->lsa_policy;
3105 0 : return result;
3106 : }
3107 :
3108 : /****************************************************************************
3109 : Open a LSA connection to a DC, suitable for LSA lookup calls.
3110 : ****************************************************************************/
3111 :
3112 0 : NTSTATUS cm_connect_lsat(struct winbindd_domain *domain,
3113 : TALLOC_CTX *mem_ctx,
3114 : struct rpc_pipe_client **cli,
3115 : struct policy_handle *lsa_policy)
3116 : {
3117 0 : NTSTATUS status;
3118 :
3119 0 : if (domain->can_do_ncacn_ip_tcp) {
3120 0 : status = cm_connect_lsa_tcp(domain, mem_ctx, cli);
3121 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
3122 0 : NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR) ||
3123 0 : NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_ACCESS_DENIED)) {
3124 0 : invalidate_cm_connection(domain);
3125 0 : status = cm_connect_lsa_tcp(domain, mem_ctx, cli);
3126 : }
3127 0 : if (NT_STATUS_IS_OK(status)) {
3128 0 : return status;
3129 : }
3130 :
3131 : /*
3132 : * we tried twice to connect via ncan_ip_tcp and schannel and
3133 : * failed - maybe it is a trusted domain we can't connect to ?
3134 : * do not try tcp next time - gd
3135 : *
3136 : * This also prevents NETLOGON over TCP
3137 : */
3138 0 : domain->can_do_ncacn_ip_tcp = false;
3139 : }
3140 :
3141 0 : status = cm_connect_lsa(domain, mem_ctx, cli, lsa_policy);
3142 :
3143 0 : return status;
3144 : }
3145 :
3146 : /****************************************************************************
3147 : Open the netlogon pipe to this DC.
3148 : ****************************************************************************/
3149 :
3150 4 : static NTSTATUS cm_connect_netlogon_transport(struct winbindd_domain *domain,
3151 : enum dcerpc_transport_t transport,
3152 : struct rpc_pipe_client **cli)
3153 : {
3154 4 : struct messaging_context *msg_ctx = global_messaging_context();
3155 0 : struct winbindd_cm_conn *conn;
3156 0 : NTSTATUS result;
3157 0 : enum netr_SchannelType sec_chan_type;
3158 4 : struct cli_credentials *creds = NULL;
3159 :
3160 4 : *cli = NULL;
3161 :
3162 4 : if (IS_AD_DC) {
3163 0 : if (domain->secure_channel_type == SEC_CHAN_NULL) {
3164 : /*
3165 : * Make sure we don't even try to
3166 : * connect to a foreign domain
3167 : * without a direct outbound trust.
3168 : */
3169 0 : return NT_STATUS_NO_TRUST_LSA_SECRET;
3170 : }
3171 : }
3172 :
3173 4 : result = init_dc_connection_rpc(domain, domain->rodc);
3174 4 : if (!NT_STATUS_IS_OK(result)) {
3175 0 : return result;
3176 : }
3177 :
3178 4 : conn = &domain->conn;
3179 :
3180 4 : if (rpccli_is_connected(conn->netlogon_pipe)) {
3181 2 : *cli = conn->netlogon_pipe;
3182 2 : return NT_STATUS_OK;
3183 : }
3184 :
3185 2 : TALLOC_FREE(conn->netlogon_pipe);
3186 2 : TALLOC_FREE(conn->netlogon_creds_ctx);
3187 :
3188 2 : result = get_trust_credentials(domain, talloc_tos(), true, &creds);
3189 2 : if (!NT_STATUS_IS_OK(result)) {
3190 0 : DBG_DEBUG("No user available for domain %s when trying "
3191 : "schannel\n", domain->name);
3192 0 : return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3193 : }
3194 :
3195 2 : if (cli_credentials_is_anonymous(creds)) {
3196 0 : DBG_WARNING("get_trust_credential only gave anonymous for %s, "
3197 : "unable to make get NETLOGON credentials\n",
3198 : domain->name);
3199 0 : return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3200 : }
3201 :
3202 2 : sec_chan_type = cli_credentials_get_secure_channel_type(creds);
3203 2 : if (sec_chan_type == SEC_CHAN_NULL) {
3204 0 : const char *remote_name =
3205 0 : smbXcli_conn_remote_name(conn->cli->conn);
3206 0 : const struct sockaddr_storage *remote_sockaddr =
3207 0 : smbXcli_conn_remote_sockaddr(conn->cli->conn);
3208 :
3209 0 : if (transport == NCACN_IP_TCP) {
3210 0 : DBG_NOTICE("get_secure_channel_type gave SEC_CHAN_NULL "
3211 : "for %s, deny NCACN_IP_TCP and let the "
3212 : "caller fallback to NCACN_NP.\n",
3213 : domain->name);
3214 0 : return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3215 : }
3216 :
3217 0 : DBG_NOTICE("get_secure_channel_type gave SEC_CHAN_NULL for %s, "
3218 : "fallback to noauth on NCACN_NP.\n",
3219 : domain->name);
3220 :
3221 0 : result = cli_rpc_pipe_open_noauth_transport(
3222 : conn->cli,
3223 : transport,
3224 : &ndr_table_netlogon,
3225 : remote_name,
3226 : remote_sockaddr,
3227 : &conn->netlogon_pipe);
3228 0 : if (!NT_STATUS_IS_OK(result)) {
3229 0 : invalidate_cm_connection(domain);
3230 0 : return result;
3231 : }
3232 :
3233 0 : *cli = conn->netlogon_pipe;
3234 0 : return NT_STATUS_OK;
3235 : }
3236 :
3237 2 : result = rpccli_create_netlogon_creds_ctx(creds,
3238 2 : domain->dcname,
3239 : msg_ctx,
3240 : domain,
3241 : &conn->netlogon_creds_ctx);
3242 2 : if (!NT_STATUS_IS_OK(result)) {
3243 0 : DEBUG(1, ("rpccli_create_netlogon_creds failed for %s, "
3244 : "unable to create NETLOGON credentials: %s\n",
3245 : domain->name, nt_errstr(result)));
3246 0 : return result;
3247 : }
3248 :
3249 2 : result = rpccli_connect_netlogon(
3250 : conn->cli, transport,
3251 2 : conn->netlogon_creds_ctx, conn->netlogon_force_reauth, creds,
3252 : &conn->netlogon_pipe);
3253 2 : conn->netlogon_force_reauth = false;
3254 2 : if (!NT_STATUS_IS_OK(result)) {
3255 0 : DBG_DEBUG("rpccli_connect_netlogon failed: %s\n",
3256 : nt_errstr(result));
3257 0 : return result;
3258 : }
3259 :
3260 2 : *cli = conn->netlogon_pipe;
3261 2 : return NT_STATUS_OK;
3262 : }
3263 :
3264 : /****************************************************************************
3265 : Open a NETLOGON connection to a DC, suitable for SamLogon calls.
3266 : ****************************************************************************/
3267 :
3268 4 : NTSTATUS cm_connect_netlogon(struct winbindd_domain *domain,
3269 : struct rpc_pipe_client **cli)
3270 : {
3271 0 : NTSTATUS status;
3272 :
3273 4 : status = init_dc_connection_rpc(domain, domain->rodc);
3274 4 : if (!NT_STATUS_IS_OK(status)) {
3275 0 : return status;
3276 : }
3277 :
3278 4 : if (domain->active_directory && domain->can_do_ncacn_ip_tcp) {
3279 4 : status = cm_connect_netlogon_transport(domain, NCACN_IP_TCP, cli);
3280 4 : if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
3281 4 : NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR) ||
3282 4 : NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_ACCESS_DENIED)) {
3283 0 : invalidate_cm_connection(domain);
3284 0 : status = cm_connect_netlogon_transport(domain, NCACN_IP_TCP, cli);
3285 : }
3286 4 : if (NT_STATUS_IS_OK(status)) {
3287 4 : return status;
3288 : }
3289 :
3290 : /*
3291 : * we tried twice to connect via ncan_ip_tcp and schannel and
3292 : * failed - maybe it is a trusted domain we can't connect to ?
3293 : * do not try tcp next time - gd
3294 : *
3295 : * This also prevents LSA over TCP
3296 : */
3297 0 : domain->can_do_ncacn_ip_tcp = false;
3298 : }
3299 :
3300 0 : status = cm_connect_netlogon_transport(domain, NCACN_NP, cli);
3301 0 : if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
3302 : /*
3303 : * SMB2 session expired, needs reauthentication. Drop
3304 : * connection and retry.
3305 : */
3306 0 : invalidate_cm_connection(domain);
3307 0 : status = cm_connect_netlogon_transport(domain, NCACN_NP, cli);
3308 : }
3309 :
3310 0 : return status;
3311 : }
3312 :
3313 0 : NTSTATUS cm_connect_netlogon_secure(struct winbindd_domain *domain,
3314 : struct rpc_pipe_client **cli,
3315 : struct netlogon_creds_cli_context **ppdc)
3316 : {
3317 0 : NTSTATUS status;
3318 :
3319 0 : if (domain->secure_channel_type == SEC_CHAN_NULL) {
3320 0 : return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3321 : }
3322 :
3323 0 : status = cm_connect_netlogon(domain, cli);
3324 0 : if (!NT_STATUS_IS_OK(status)) {
3325 0 : return status;
3326 : }
3327 :
3328 0 : if (domain->conn.netlogon_creds_ctx == NULL) {
3329 0 : return NT_STATUS_TRUSTED_DOMAIN_FAILURE;
3330 : }
3331 :
3332 0 : *ppdc = domain->conn.netlogon_creds_ctx;
3333 0 : return NT_STATUS_OK;
3334 : }
3335 :
3336 0 : void winbind_msg_ip_dropped(struct messaging_context *msg_ctx,
3337 : void *private_data,
3338 : uint32_t msg_type,
3339 : struct server_id server_id,
3340 : DATA_BLOB *data)
3341 : {
3342 0 : struct winbindd_domain *domain;
3343 0 : char *freeit = NULL;
3344 0 : char *addr;
3345 :
3346 0 : if ((data == NULL)
3347 0 : || (data->data == NULL)
3348 0 : || (data->length == 0)
3349 0 : || (data->data[data->length-1] != '\0')) {
3350 0 : DEBUG(1, ("invalid msg_ip_dropped message: not a valid "
3351 : "string\n"));
3352 0 : return;
3353 : }
3354 :
3355 0 : addr = (char *)data->data;
3356 0 : DEBUG(10, ("IP %s dropped\n", addr));
3357 :
3358 0 : if (!is_ipaddress(addr)) {
3359 0 : char *slash;
3360 : /*
3361 : * Some code sends us ip addresses with the /netmask
3362 : * suffix
3363 : */
3364 0 : slash = strchr(addr, '/');
3365 0 : if (slash == NULL) {
3366 0 : DEBUG(1, ("invalid msg_ip_dropped message: %s\n",
3367 : addr));
3368 0 : return;
3369 : }
3370 0 : freeit = talloc_strndup(talloc_tos(), addr, slash-addr);
3371 0 : if (freeit == NULL) {
3372 0 : DEBUG(1, ("talloc failed\n"));
3373 0 : return;
3374 : }
3375 0 : addr = freeit;
3376 0 : DEBUG(10, ("Stripped /netmask to IP %s\n", addr));
3377 : }
3378 :
3379 0 : for (domain = domain_list(); domain != NULL; domain = domain->next) {
3380 0 : char sockaddr[INET6_ADDRSTRLEN];
3381 :
3382 0 : if (!cli_state_is_connected(domain->conn.cli)) {
3383 0 : continue;
3384 : }
3385 :
3386 0 : print_sockaddr(sockaddr, sizeof(sockaddr),
3387 0 : smbXcli_conn_local_sockaddr(domain->conn.cli->conn));
3388 :
3389 0 : if (strequal(sockaddr, addr)) {
3390 0 : smbXcli_conn_disconnect(domain->conn.cli->conn, NT_STATUS_OK);
3391 : }
3392 : }
3393 0 : TALLOC_FREE(freeit);
3394 : }
3395 :
3396 0 : void winbind_msg_disconnect_dc(struct messaging_context *msg_ctx,
3397 : void *private_data,
3398 : uint32_t msg_type,
3399 : struct server_id server_id,
3400 : DATA_BLOB *data)
3401 : {
3402 0 : struct winbindd_domain *domain;
3403 :
3404 0 : for (domain = domain_list(); domain; domain = domain->next) {
3405 0 : if (domain->internal) {
3406 0 : continue;
3407 : }
3408 0 : invalidate_cm_connection(domain);
3409 : }
3410 0 : }
|