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