2 Unix SMB/CIFS implementation.
4 Winbind daemon connection manager
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
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.
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.
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/>.
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:
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
35 - manage re-entrancy for when winbindd becomes able to handle
36 multiple outstanding rpc requests
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.
45 The TNG design is quite good but I disagree with some aspects of the
53 - I'm pretty annoyed by all the make_nmb_name() stuff. It should be
54 moved down into another function.
56 - Take care when destroying cli_structs as they can be shared between
63 #include "../libcli/auth/libcli_auth.h"
64 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
65 #include "rpc_client/cli_pipe.h"
66 #include "rpc_client/cli_netlogon.h"
67 #include "../librpc/gen_ndr/ndr_samr_c.h"
68 #include "../librpc/gen_ndr/ndr_lsa_c.h"
69 #include "rpc_client/cli_lsarpc.h"
70 #include "../librpc/gen_ndr/ndr_dssetup_c.h"
71 #include "libads/sitename_cache.h"
72 #include "libsmb/libsmb.h"
73 #include "libsmb/clidgram.h"
76 #include "../libcli/security/security.h"
79 #include "auth/gensec/gensec.h"
80 #include "../libcli/smb/smbXcli_base.h"
81 #include "libcli/auth/netlogon_creds_cli.h"
83 #include "rpc_server/rpc_ncacn_np.h"
84 #include "auth/credentials/credentials.h"
85 #include "lib/param/param.h"
88 #define DBGC_CLASS DBGC_WINBIND
92 struct sockaddr_storage ss
;
95 extern struct winbindd_methods reconnect_methods
;
96 extern bool override_logfile
;
98 static NTSTATUS
init_dc_connection_network(struct winbindd_domain
*domain
, bool need_rw_dc
);
99 static void set_dc_type_and_flags( struct winbindd_domain
*domain
);
100 static bool set_dc_type_and_flags_trustinfo( struct winbindd_domain
*domain
);
101 static bool get_dcs(TALLOC_CTX
*mem_ctx
, struct winbindd_domain
*domain
,
102 struct dc_name_ip
**dcs
, int *num_dcs
);
104 /****************************************************************
105 Child failed to find DC's. Reschedule check.
106 ****************************************************************/
108 static void msg_failed_to_go_online(struct messaging_context
*msg
,
111 struct server_id server_id
,
114 struct winbindd_domain
*domain
;
115 const char *domainname
= (const char *)data
->data
;
117 if (data
->data
== NULL
|| data
->length
== 0) {
121 DEBUG(5,("msg_fail_to_go_online: received for domain %s.\n", domainname
));
123 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
124 if (domain
->internal
) {
128 if (strequal(domain
->name
, domainname
)) {
129 if (domain
->online
) {
130 /* We're already online, ignore. */
131 DEBUG(5,("msg_fail_to_go_online: domain %s "
132 "already online.\n", domainname
));
136 /* Reschedule the online check. */
137 set_domain_offline(domain
);
143 /****************************************************************
144 Actually cause a reconnect from a message.
145 ****************************************************************/
147 static void msg_try_to_go_online(struct messaging_context
*msg
,
150 struct server_id server_id
,
153 struct winbindd_domain
*domain
;
154 const char *domainname
= (const char *)data
->data
;
156 if (data
->data
== NULL
|| data
->length
== 0) {
160 DEBUG(5,("msg_try_to_go_online: received for domain %s.\n", domainname
));
162 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
163 if (domain
->internal
) {
167 if (strequal(domain
->name
, domainname
)) {
169 if (domain
->online
) {
170 /* We're already online, ignore. */
171 DEBUG(5,("msg_try_to_go_online: domain %s "
172 "already online.\n", domainname
));
176 /* This call takes care of setting the online
177 flag to true if we connected, or re-adding
178 the offline handler if false. Bypasses online
179 check so always does network calls. */
181 init_dc_connection_network(domain
, true);
187 /****************************************************************
188 Fork a child to try and contact a DC. Do this as contacting a
189 DC requires blocking lookups and we don't want to block our
191 ****************************************************************/
193 static bool fork_child_dc_connect(struct winbindd_domain
*domain
)
195 struct dc_name_ip
*dcs
= NULL
;
197 TALLOC_CTX
*mem_ctx
= NULL
;
198 pid_t parent_pid
= getpid();
202 if (domain
->dc_probe_pid
!= (pid_t
)-1) {
204 * We might already have a DC probe
205 * child working, check.
207 if (process_exists_by_pid(domain
->dc_probe_pid
)) {
208 DEBUG(10,("fork_child_dc_connect: pid %u already "
209 "checking for DC's.\n",
210 (unsigned int)domain
->dc_probe_pid
));
213 domain
->dc_probe_pid
= (pid_t
)-1;
216 domain
->dc_probe_pid
= fork();
218 if (domain
->dc_probe_pid
== (pid_t
)-1) {
219 DEBUG(0, ("fork_child_dc_connect: Could not fork: %s\n", strerror(errno
)));
223 if (domain
->dc_probe_pid
!= (pid_t
)0) {
225 messaging_register(winbind_messaging_context(), NULL
,
226 MSG_WINBIND_TRY_TO_GO_ONLINE
,
227 msg_try_to_go_online
);
228 messaging_register(winbind_messaging_context(), NULL
,
229 MSG_WINBIND_FAILED_TO_GO_ONLINE
,
230 msg_failed_to_go_online
);
236 /* Leave messages blocked - we will never process one. */
238 if (!override_logfile
) {
239 if (asprintf(&lfile
, "%s/log.winbindd-dc-connect", get_dyn_LOGFILEBASE()) == -1) {
240 DEBUG(0, ("fork_child_dc_connect: out of memory.\n"));
245 status
= winbindd_reinit_after_fork(NULL
, lfile
);
246 if (!NT_STATUS_IS_OK(status
)) {
247 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
249 messaging_send_buf(winbind_messaging_context(),
250 pid_to_procid(parent_pid
),
251 MSG_WINBIND_FAILED_TO_GO_ONLINE
,
252 (const uint8_t *)domain
->name
,
253 strlen(domain
->name
)+1);
258 mem_ctx
= talloc_init("fork_child_dc_connect");
260 DEBUG(0,("talloc_init failed.\n"));
261 messaging_send_buf(winbind_messaging_context(),
262 pid_to_procid(parent_pid
),
263 MSG_WINBIND_FAILED_TO_GO_ONLINE
,
264 (const uint8_t *)domain
->name
,
265 strlen(domain
->name
)+1);
269 if ((!get_dcs(mem_ctx
, domain
, &dcs
, &num_dcs
)) || (num_dcs
== 0)) {
270 /* Still offline ? Can't find DC's. */
271 messaging_send_buf(winbind_messaging_context(),
272 pid_to_procid(parent_pid
),
273 MSG_WINBIND_FAILED_TO_GO_ONLINE
,
274 (const uint8_t *)domain
->name
,
275 strlen(domain
->name
)+1);
279 /* We got a DC. Send a message to our parent to get it to
280 try and do the same. */
282 messaging_send_buf(winbind_messaging_context(),
283 pid_to_procid(parent_pid
),
284 MSG_WINBIND_TRY_TO_GO_ONLINE
,
285 (const uint8_t *)domain
->name
,
286 strlen(domain
->name
)+1);
290 /****************************************************************
291 Handler triggered if we're offline to try and detect a DC.
292 ****************************************************************/
294 static void check_domain_online_handler(struct tevent_context
*ctx
,
295 struct tevent_timer
*te
,
299 struct winbindd_domain
*domain
=
300 (struct winbindd_domain
*)private_data
;
302 DEBUG(10,("check_domain_online_handler: called for domain "
303 "%s (online = %s)\n", domain
->name
,
304 domain
->online
? "True" : "False" ));
306 TALLOC_FREE(domain
->check_online_event
);
308 /* Are we still in "startup" mode ? */
310 if (domain
->startup
&& (time_mono(NULL
) > domain
->startup_time
+ 30)) {
311 /* No longer in "startup" mode. */
312 DEBUG(10,("check_domain_online_handler: domain %s no longer in 'startup' mode.\n",
314 domain
->startup
= False
;
317 /* We've been told to stay offline, so stay
320 if (get_global_winbindd_state_offline()) {
321 DEBUG(10,("check_domain_online_handler: domain %s remaining globally offline\n",
326 /* Fork a child to test if it can contact a DC.
327 If it can then send ourselves a message to
328 cause a reconnect. */
330 fork_child_dc_connect(domain
);
333 /****************************************************************
334 If we're still offline setup the timeout check.
335 ****************************************************************/
337 static void calc_new_online_timeout_check(struct winbindd_domain
*domain
)
339 int wbr
= lp_winbind_reconnect_delay();
341 if (domain
->startup
) {
342 domain
->check_online_timeout
= 10;
343 } else if (domain
->check_online_timeout
< wbr
) {
344 domain
->check_online_timeout
= wbr
;
348 void winbind_msg_domain_offline(struct messaging_context
*msg_ctx
,
351 struct server_id server_id
,
354 const char *domain_name
= (const char *)data
->data
;
355 struct winbindd_domain
*domain
;
357 domain
= find_domain_from_name_noinit(domain_name
);
358 if (domain
== NULL
) {
362 domain
->online
= false;
364 DEBUG(10, ("Domain %s is marked as offline now.\n",
368 void winbind_msg_domain_online(struct messaging_context
*msg_ctx
,
371 struct server_id server_id
,
374 const char *domain_name
= (const char *)data
->data
;
375 struct winbindd_domain
*domain
;
377 domain
= find_domain_from_name_noinit(domain_name
);
378 if (domain
== NULL
) {
382 domain
->online
= true;
384 DEBUG(10, ("Domain %s is marked as online now.\n",
388 /****************************************************************
389 Set domain offline and also add handler to put us back online
391 ****************************************************************/
393 void set_domain_offline(struct winbindd_domain
*domain
)
395 pid_t parent_pid
= getppid();
397 DEBUG(10,("set_domain_offline: called for domain %s\n",
400 TALLOC_FREE(domain
->check_online_event
);
402 if (domain
->internal
) {
403 DEBUG(3,("set_domain_offline: domain %s is internal - logic error.\n",
408 domain
->online
= False
;
410 /* Offline domains are always initialized. They're
411 re-initialized when they go back online. */
413 domain
->initialized
= True
;
415 /* We only add the timeout handler that checks and
416 allows us to go back online when we've not
417 been told to remain offline. */
419 if (get_global_winbindd_state_offline()) {
420 DEBUG(10,("set_domain_offline: domain %s remaining globally offline\n",
425 /* If we're in startup mode, check again in 10 seconds, not in
426 lp_winbind_reconnect_delay() seconds (which is 30 seconds by default). */
428 calc_new_online_timeout_check(domain
);
430 domain
->check_online_event
= tevent_add_timer(winbind_event_context(),
432 timeval_current_ofs(domain
->check_online_timeout
,0),
433 check_domain_online_handler
,
436 /* The above *has* to succeed for winbindd to work. */
437 if (!domain
->check_online_event
) {
438 smb_panic("set_domain_offline: failed to add online handler");
441 DEBUG(10,("set_domain_offline: added event handler for domain %s\n",
444 /* Send a message to the parent that the domain is offline. */
445 if (parent_pid
> 1 && !domain
->internal
) {
446 messaging_send_buf(winbind_messaging_context(),
447 pid_to_procid(parent_pid
),
448 MSG_WINBIND_DOMAIN_OFFLINE
,
449 (uint8_t *)domain
->name
,
450 strlen(domain
->name
) + 1);
453 /* Send an offline message to the idmap child when our
454 primary domain goes offline */
456 if ( domain
->primary
) {
457 struct winbindd_child
*idmap
= idmap_child();
459 if ( idmap
->pid
!= 0 ) {
460 messaging_send_buf(winbind_messaging_context(),
461 pid_to_procid(idmap
->pid
),
463 (const uint8_t *)domain
->name
,
464 strlen(domain
->name
)+1);
471 /****************************************************************
472 Set domain online - if allowed.
473 ****************************************************************/
475 static void set_domain_online(struct winbindd_domain
*domain
)
477 pid_t parent_pid
= getppid();
479 DEBUG(10,("set_domain_online: called for domain %s\n",
482 if (domain
->internal
) {
483 DEBUG(3,("set_domain_online: domain %s is internal - logic error.\n",
488 if (get_global_winbindd_state_offline()) {
489 DEBUG(10,("set_domain_online: domain %s remaining globally offline\n",
494 winbindd_set_locator_kdc_envs(domain
);
496 /* If we are waiting to get a krb5 ticket, trigger immediately. */
497 ccache_regain_all_now();
499 /* Ok, we're out of any startup mode now... */
500 domain
->startup
= False
;
502 if (domain
->online
== False
) {
503 /* We were offline - now we're online. We default to
504 using the MS-RPC backend if we started offline,
505 and if we're going online for the first time we
506 should really re-initialize the backends and the
507 checks to see if we're talking to an AD or NT domain.
510 domain
->initialized
= False
;
512 /* 'reconnect_methods' is the MS-RPC backend. */
513 if (domain
->backend
== &reconnect_methods
) {
514 domain
->backend
= NULL
;
518 /* Ensure we have no online timeout checks. */
519 domain
->check_online_timeout
= 0;
520 TALLOC_FREE(domain
->check_online_event
);
522 /* Ensure we ignore any pending child messages. */
523 messaging_deregister(winbind_messaging_context(),
524 MSG_WINBIND_TRY_TO_GO_ONLINE
, NULL
);
525 messaging_deregister(winbind_messaging_context(),
526 MSG_WINBIND_FAILED_TO_GO_ONLINE
, NULL
);
528 domain
->online
= True
;
530 /* Send a message to the parent that the domain is online. */
531 if (parent_pid
> 1 && !domain
->internal
) {
532 messaging_send_buf(winbind_messaging_context(),
533 pid_to_procid(parent_pid
),
534 MSG_WINBIND_DOMAIN_ONLINE
,
535 (uint8_t *)domain
->name
,
536 strlen(domain
->name
) + 1);
539 /* Send an online message to the idmap child when our
540 primary domain comes online */
542 if ( domain
->primary
) {
543 struct winbindd_child
*idmap
= idmap_child();
545 if ( idmap
->pid
!= 0 ) {
546 messaging_send_buf(winbind_messaging_context(),
547 pid_to_procid(idmap
->pid
),
549 (const uint8_t *)domain
->name
,
550 strlen(domain
->name
)+1);
557 /****************************************************************
558 Requested to set a domain online.
559 ****************************************************************/
561 void set_domain_online_request(struct winbindd_domain
*domain
)
565 DEBUG(10,("set_domain_online_request: called for domain %s\n",
568 if (get_global_winbindd_state_offline()) {
569 DEBUG(10,("set_domain_online_request: domain %s remaining globally offline\n",
574 if (domain
->internal
) {
575 DEBUG(10, ("set_domain_online_request: Internal domains are "
580 /* We've been told it's safe to go online and
581 try and connect to a DC. But I don't believe it
582 because network manager seems to lie.
583 Wait at least 5 seconds. Heuristics suck... */
588 /* Go into "startup" mode again. */
589 domain
->startup_time
= time_mono(NULL
);
590 domain
->startup
= True
;
594 if (!domain
->check_online_event
) {
595 /* If we've come from being globally offline we
596 don't have a check online event handler set.
597 We need to add one now we're trying to go
600 DEBUG(10,("set_domain_online_request: domain %s was globally offline.\n",
604 TALLOC_FREE(domain
->check_online_event
);
606 domain
->check_online_event
= tevent_add_timer(winbind_event_context(),
609 check_domain_online_handler
,
612 /* The above *has* to succeed for winbindd to work. */
613 if (!domain
->check_online_event
) {
614 smb_panic("set_domain_online_request: failed to add online handler");
618 /****************************************************************
619 Add -ve connection cache entries for domain and realm.
620 ****************************************************************/
622 static void winbind_add_failed_connection_entry(
623 const struct winbindd_domain
*domain
,
627 add_failed_connection_entry(domain
->name
, server
, result
);
628 /* If this was the saf name for the last thing we talked to,
630 saf_delete(domain
->name
);
631 if (domain
->alt_name
!= NULL
) {
632 add_failed_connection_entry(domain
->alt_name
, server
, result
);
633 saf_delete(domain
->alt_name
);
635 winbindd_unset_locator_kdc_env(domain
);
638 /* Choose between anonymous or authenticated connections. We need to use
639 an authenticated connection if DCs have the RestrictAnonymous registry
640 entry set > 0, or the "Additional restrictions for anonymous
641 connections" set in the win2k Local Security Policy.
643 Caller to free() result in domain, username, password
646 static void cm_get_ipc_userpass(char **username
, char **domain
, char **password
)
648 *username
= (char *)secrets_fetch(SECRETS_AUTH_USER
, NULL
);
649 *domain
= (char *)secrets_fetch(SECRETS_AUTH_DOMAIN
, NULL
);
650 *password
= (char *)secrets_fetch(SECRETS_AUTH_PASSWORD
, NULL
);
652 if (*username
&& **username
) {
654 if (!*domain
|| !**domain
)
655 *domain
= smb_xstrdup(lp_workgroup());
657 if (!*password
|| !**password
)
658 *password
= smb_xstrdup("");
660 DEBUG(3, ("cm_get_ipc_userpass: Retrieved auth-user from secrets.tdb [%s\\%s]\n",
661 *domain
, *username
));
664 DEBUG(3, ("cm_get_ipc_userpass: No auth-user defined\n"));
665 *username
= smb_xstrdup("");
666 *domain
= smb_xstrdup("");
667 *password
= smb_xstrdup("");
671 static NTSTATUS
cm_get_ipc_credentials(TALLOC_CTX
*mem_ctx
,
672 struct cli_credentials
**_creds
)
675 TALLOC_CTX
*frame
= talloc_stackframe();
676 NTSTATUS status
= NT_STATUS_INTERNAL_ERROR
;
677 struct loadparm_context
*lp_ctx
;
678 char *username
= NULL
;
679 char *netbios_domain
= NULL
;
680 char *password
= NULL
;
681 struct cli_credentials
*creds
= NULL
;
684 cm_get_ipc_userpass(&username
, &netbios_domain
, &password
);
686 lp_ctx
= loadparm_init_s3(frame
, loadparm_s3_helpers());
687 if (lp_ctx
== NULL
) {
688 DEBUG(1, ("loadparm_init_s3 failed\n"));
689 status
= NT_STATUS_INTERNAL_ERROR
;
693 creds
= cli_credentials_init(mem_ctx
);
695 status
= NT_STATUS_NO_MEMORY
;
699 cli_credentials_set_conf(creds
, lp_ctx
);
700 cli_credentials_set_kerberos_state(creds
, CRED_DONT_USE_KERBEROS
);
702 ok
= cli_credentials_set_domain(creds
, netbios_domain
, CRED_SPECIFIED
);
704 status
= NT_STATUS_NO_MEMORY
;
708 ok
= cli_credentials_set_username(creds
, username
, CRED_SPECIFIED
);
710 status
= NT_STATUS_NO_MEMORY
;
714 ok
= cli_credentials_set_password(creds
, password
, CRED_SPECIFIED
);
716 status
= NT_STATUS_NO_MEMORY
;
722 status
= NT_STATUS_OK
;
726 SAFE_FREE(netbios_domain
);
732 static bool cm_is_ipc_credentials(struct cli_credentials
*creds
)
734 TALLOC_CTX
*frame
= talloc_stackframe();
735 char *ipc_account
= NULL
;
736 char *ipc_domain
= NULL
;
737 char *ipc_password
= NULL
;
738 const char *creds_account
= NULL
;
739 const char *creds_domain
= NULL
;
740 const char *creds_password
= NULL
;
743 cm_get_ipc_userpass(&ipc_account
, &ipc_domain
, &ipc_password
);
745 creds_account
= cli_credentials_get_username(creds
);
746 creds_domain
= cli_credentials_get_domain(creds
);
747 creds_password
= cli_credentials_get_password(creds
);
749 if (!strequal(ipc_domain
, creds_domain
)) {
753 if (!strequal(ipc_account
, creds_account
)) {
757 if (!strcsequal(ipc_password
, creds_password
)) {
763 SAFE_FREE(ipc_account
);
764 SAFE_FREE(ipc_domain
);
765 SAFE_FREE(ipc_password
);
770 static bool get_dc_name_via_netlogon(struct winbindd_domain
*domain
,
772 struct sockaddr_storage
*dc_ss
)
774 struct winbindd_domain
*our_domain
= NULL
;
775 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
779 unsigned int orig_timeout
;
780 const char *tmp
= NULL
;
782 struct dcerpc_binding_handle
*b
;
784 /* Hmmmm. We can only open one connection to the NETLOGON pipe at the
791 if (domain
->primary
) {
795 our_domain
= find_our_domain();
797 if ((mem_ctx
= talloc_init("get_dc_name_via_netlogon")) == NULL
) {
801 result
= cm_connect_netlogon(our_domain
, &netlogon_pipe
);
802 if (!NT_STATUS_IS_OK(result
)) {
803 talloc_destroy(mem_ctx
);
807 b
= netlogon_pipe
->binding_handle
;
809 /* This call can take a long time - allow the server to time out.
810 35 seconds should do it. */
812 orig_timeout
= rpccli_set_timeout(netlogon_pipe
, 35000);
814 if (our_domain
->active_directory
) {
815 struct netr_DsRGetDCNameInfo
*domain_info
= NULL
;
817 result
= dcerpc_netr_DsRGetDCName(b
,
826 if (NT_STATUS_IS_OK(result
) && W_ERROR_IS_OK(werr
)) {
828 mem_ctx
, domain_info
->dc_unc
);
830 DEBUG(0, ("talloc_strdup failed\n"));
831 talloc_destroy(mem_ctx
);
834 if (domain
->alt_name
== NULL
) {
835 domain
->alt_name
= talloc_strdup(domain
,
836 domain_info
->domain_name
);
837 if (domain
->alt_name
== NULL
) {
838 DEBUG(0, ("talloc_strdup failed\n"));
839 talloc_destroy(mem_ctx
);
843 if (domain
->forest_name
== NULL
) {
844 domain
->forest_name
= talloc_strdup(domain
,
845 domain_info
->forest_name
);
846 if (domain
->forest_name
== NULL
) {
847 DEBUG(0, ("talloc_strdup failed\n"));
848 talloc_destroy(mem_ctx
);
854 result
= dcerpc_netr_GetAnyDCName(b
, mem_ctx
,
861 /* And restore our original timeout. */
862 rpccli_set_timeout(netlogon_pipe
, orig_timeout
);
864 if (!NT_STATUS_IS_OK(result
)) {
865 DEBUG(10,("dcerpc_netr_GetAnyDCName failed: %s\n",
867 talloc_destroy(mem_ctx
);
871 if (!W_ERROR_IS_OK(werr
)) {
872 DEBUG(10,("dcerpc_netr_GetAnyDCName failed: %s\n",
874 talloc_destroy(mem_ctx
);
878 /* dcerpc_netr_GetAnyDCName gives us a name with \\ */
879 p
= strip_hostname(tmp
);
883 talloc_destroy(mem_ctx
);
885 DEBUG(10,("dcerpc_netr_GetAnyDCName returned %s\n", dcname
));
887 if (!resolve_name(dcname
, dc_ss
, 0x20, true)) {
895 * Helper function to assemble trust password and account name
897 static NTSTATUS
get_trust_credentials(struct winbindd_domain
*domain
,
900 struct cli_credentials
**_creds
)
902 const struct winbindd_domain
*creds_domain
= NULL
;
903 struct cli_credentials
*creds
;
905 bool force_machine_account
= false;
908 /* If we are a DC and this is not our own domain */
910 if (!domain
->active_directory
) {
913 * For non active directory domains
914 * we can only use NTLMSSP for SMB.
916 * But the trust account is not allowed
917 * to use SMB with NTLMSSP.
919 force_machine_account
= true;
923 if (IS_DC
&& !force_machine_account
) {
924 creds_domain
= domain
;
926 creds_domain
= find_our_domain();
927 if (creds_domain
== NULL
) {
928 return NT_STATUS_INVALID_SERVER_STATE
;
932 status
= pdb_get_trust_credentials(creds_domain
->name
,
933 creds_domain
->alt_name
,
936 if (!NT_STATUS_IS_OK(status
)) {
940 if (domain
->primary
&& lp_security() == SEC_ADS
) {
941 cli_credentials_set_kerberos_state(creds
,
942 CRED_AUTO_USE_KERBEROS
);
943 } else if (domain
->active_directory
) {
944 cli_credentials_set_kerberos_state(creds
,
945 CRED_MUST_USE_KERBEROS
);
947 cli_credentials_set_kerberos_state(creds
,
948 CRED_DONT_USE_KERBEROS
);
952 * When we contact our own domain and get a list of the trusted domain
953 * we have the information if we are able to contact the DC with
954 * with our machine account password.
956 ok
= winbindd_can_contact_domain(domain
);
959 * We can only use schannel against a direct trust
961 cli_credentials_set_secure_channel_type(creds
,
970 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
973 status
= cm_get_ipc_credentials(mem_ctx
, &creds
);
974 if (!NT_STATUS_IS_OK(status
)) {
982 /************************************************************************
983 Given a fd with a just-connected TCP connection to a DC, open a connection
985 ************************************************************************/
987 static NTSTATUS
cm_prepare_connection(struct winbindd_domain
*domain
,
989 const char *controller
,
990 struct cli_state
**cli
,
993 bool try_ipc_auth
= false;
994 const char *machine_password
= NULL
;
995 const char *machine_krb5_principal
= NULL
;
996 const char *machine_account
= NULL
;
997 const char *machine_domain
= NULL
;
999 struct cli_credentials
*creds
= NULL
;
1000 enum credentials_use_kerberos krb5_state
;
1002 struct named_mutex
*mutex
;
1004 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1006 enum smb_signing_setting smb_sign_client_connections
= lp_client_ipc_signing();
1008 if (smb_sign_client_connections
== SMB_SIGNING_DEFAULT
) {
1010 * If we are connecting to our own AD domain, require
1011 * smb signing to disrupt MITM attacks
1013 if (domain
->primary
&& lp_security() == SEC_ADS
) {
1014 smb_sign_client_connections
= SMB_SIGNING_REQUIRED
;
1016 * If we are in or are an AD domain and connecting to another
1017 * AD domain in our forest
1018 * then require smb signing to disrupt MITM attacks
1020 } else if ((lp_security() == SEC_ADS
||
1021 lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC
)
1022 && domain
->active_directory
1023 && (domain
->domain_trust_attribs
1024 & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
)) {
1025 smb_sign_client_connections
= SMB_SIGNING_REQUIRED
;
1029 DEBUG(10,("cm_prepare_connection: connecting to DC %s for domain %s\n",
1030 controller
, domain
->name
));
1034 mutex
= grab_named_mutex(talloc_tos(), controller
,
1035 WINBIND_SERVER_MUTEX_WAIT_TIME
);
1036 if (mutex
== NULL
) {
1038 DEBUG(0,("cm_prepare_connection: mutex grab failed for %s\n",
1040 result
= NT_STATUS_POSSIBLE_DEADLOCK
;
1044 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
1046 *cli
= cli_state_create(NULL
, sockfd
,
1047 controller
, domain
->alt_name
,
1048 smb_sign_client_connections
, flags
);
1051 DEBUG(1, ("Could not cli_initialize\n"));
1052 result
= NT_STATUS_NO_MEMORY
;
1056 cli_set_timeout(*cli
, 10000); /* 10 seconds */
1058 set_socket_options(sockfd
, lp_socket_options());
1060 result
= smbXcli_negprot((*cli
)->conn
, (*cli
)->timeout
,
1061 lp_client_ipc_min_protocol(),
1062 lp_client_ipc_max_protocol());
1064 if (!NT_STATUS_IS_OK(result
)) {
1065 DEBUG(1, ("cli_negprot failed: %s\n", nt_errstr(result
)));
1069 if (smbXcli_conn_protocol((*cli
)->conn
) >= PROTOCOL_NT1
&&
1070 smb1cli_conn_capabilities((*cli
)->conn
) & CAP_EXTENDED_SECURITY
) {
1071 try_ipc_auth
= true;
1072 } else if (smbXcli_conn_protocol((*cli
)->conn
) >= PROTOCOL_SMB2_02
) {
1073 try_ipc_auth
= true;
1074 } else if (smb_sign_client_connections
== SMB_SIGNING_REQUIRED
) {
1076 * If we are forcing on SMB signing, then we must
1077 * require authentication unless this is a one-way
1078 * trust, and we have no stored user/password
1080 try_ipc_auth
= true;
1084 result
= get_trust_credentials(domain
, talloc_tos(), false, &creds
);
1085 if (!NT_STATUS_IS_OK(result
)) {
1086 DEBUG(1, ("get_trust_credentials(%s) failed: %s\n",
1087 domain
->name
, nt_errstr(result
)));
1092 * Without SPNEGO or NTLMSSP (perhaps via SMB2) we
1093 * would try and authentication with our machine
1094 * account password and fail. This is very rare in
1095 * the modern world however
1097 creds
= cli_credentials_init_anon(talloc_tos());
1098 if (creds
== NULL
) {
1099 result
= NT_STATUS_NO_MEMORY
;
1100 DEBUG(1, ("cli_credentials_init_anon(%s) failed: %s\n",
1101 domain
->name
, nt_errstr(result
)));
1106 krb5_state
= cli_credentials_get_kerberos_state(creds
);
1108 machine_krb5_principal
= cli_credentials_get_principal(creds
,
1110 if (machine_krb5_principal
== NULL
) {
1111 krb5_state
= CRED_DONT_USE_KERBEROS
;
1114 machine_account
= cli_credentials_get_username(creds
);
1115 machine_password
= cli_credentials_get_password(creds
);
1116 machine_domain
= cli_credentials_get_domain(creds
);
1118 if (krb5_state
!= CRED_DONT_USE_KERBEROS
) {
1120 /* Try a krb5 session */
1122 (*cli
)->use_kerberos
= True
;
1123 DEBUG(5, ("connecting to %s from %s with kerberos principal "
1124 "[%s] and realm [%s]\n", controller
, lp_netbios_name(),
1125 machine_krb5_principal
, domain
->alt_name
));
1127 winbindd_set_locator_kdc_envs(domain
);
1129 result
= cli_session_setup(*cli
,
1130 machine_krb5_principal
,
1132 strlen(machine_password
)+1,
1134 strlen(machine_password
)+1,
1137 if (NT_STATUS_IS_OK(result
)) {
1138 goto session_setup_done
;
1141 DEBUG(4,("failed kerberos session setup with %s\n",
1142 nt_errstr(result
)));
1145 if (krb5_state
!= CRED_MUST_USE_KERBEROS
) {
1146 /* Fall back to non-kerberos session setup using NTLMSSP SPNEGO with the machine account. */
1147 (*cli
)->use_kerberos
= False
;
1149 DEBUG(5, ("connecting to %s from %s using NTLMSSP with username "
1150 "[%s]\\[%s]\n", controller
, lp_netbios_name(),
1151 machine_domain
, machine_account
));
1153 result
= cli_session_setup(*cli
,
1156 strlen(machine_password
)+1,
1158 strlen(machine_password
)+1,
1162 if (NT_STATUS_IS_OK(result
)) {
1163 goto session_setup_done
;
1167 * If we are not going to validiate the conneciton
1168 * with SMB signing, then allow us to fall back to
1171 if (NT_STATUS_EQUAL(result
, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
)
1172 || NT_STATUS_EQUAL(result
, NT_STATUS_TRUSTED_DOMAIN_FAILURE
)
1173 || NT_STATUS_EQUAL(result
, NT_STATUS_INVALID_ACCOUNT_NAME
)
1174 || NT_STATUS_EQUAL(result
, NT_STATUS_NO_LOGON_SERVERS
)
1175 || NT_STATUS_EQUAL(result
, NT_STATUS_LOGON_FAILURE
))
1177 if (cli_credentials_is_anonymous(creds
)) {
1181 if (!cm_is_ipc_credentials(creds
)) {
1185 if (smb_sign_client_connections
== SMB_SIGNING_REQUIRED
) {
1192 DEBUG(4, ("authenticated session setup failed with %s\n",
1193 nt_errstr(result
)));
1198 result
= cm_get_ipc_credentials(talloc_tos(), &creds
);
1199 if (!NT_STATUS_IS_OK(result
)) {
1203 if (cli_credentials_is_anonymous(creds
)) {
1208 machine_account
= cli_credentials_get_username(creds
);
1209 machine_password
= cli_credentials_get_password(creds
);
1210 machine_domain
= cli_credentials_get_domain(creds
);
1212 /* Fall back to non-kerberos session setup using NTLMSSP SPNEGO with the ipc creds. */
1213 (*cli
)->use_kerberos
= False
;
1215 DEBUG(5, ("connecting to %s from %s using NTLMSSP with username "
1216 "[%s]\\[%s]\n", controller
, lp_netbios_name(),
1217 machine_domain
, machine_account
));
1219 result
= cli_session_setup(*cli
,
1222 strlen(machine_password
)+1,
1224 strlen(machine_password
)+1,
1227 if (NT_STATUS_IS_OK(result
)) {
1228 goto session_setup_done
;
1232 * If we are not going to validiate the conneciton
1233 * with SMB signing, then allow us to fall back to
1236 if (NT_STATUS_EQUAL(result
, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
)
1237 || NT_STATUS_EQUAL(result
, NT_STATUS_TRUSTED_DOMAIN_FAILURE
)
1238 || NT_STATUS_EQUAL(result
, NT_STATUS_INVALID_ACCOUNT_NAME
)
1239 || NT_STATUS_EQUAL(result
, NT_STATUS_NO_LOGON_SERVERS
)
1240 || NT_STATUS_EQUAL(result
, NT_STATUS_LOGON_FAILURE
))
1245 DEBUG(4, ("authenticated session setup failed with %s\n",
1246 nt_errstr(result
)));
1252 if (smb_sign_client_connections
== SMB_SIGNING_REQUIRED
) {
1256 /* Fall back to anonymous connection, this might fail later */
1257 DEBUG(10,("cm_prepare_connection: falling back to anonymous "
1258 "connection for DC %s\n",
1261 (*cli
)->use_kerberos
= False
;
1263 result
= cli_session_setup(*cli
, "", "", 0, "", 0, "");
1264 if (NT_STATUS_IS_OK(result
)) {
1265 DEBUG(5, ("Connected anonymously\n"));
1266 goto session_setup_done
;
1269 /* We can't session setup */
1275 * This should be a short term hack until
1276 * dynamic re-authentication is implemented.
1278 * See Bug 9175 - winbindd doesn't recover from
1279 * NT_STATUS_NETWORK_SESSION_EXPIRED
1281 if (smbXcli_conn_protocol((*cli
)->conn
) >= PROTOCOL_SMB2_02
) {
1282 smbXcli_session_set_disconnect_expired((*cli
)->smb2
.session
);
1285 result
= cli_tree_connect(*cli
, "IPC$", "IPC", "", 0);
1287 if (!NT_STATUS_IS_OK(result
)) {
1288 DEBUG(1,("failed tcon_X with %s\n", nt_errstr(result
)));
1292 /* cache the server name for later connections */
1294 saf_store(domain
->name
, controller
);
1295 if (domain
->alt_name
&& (*cli
)->use_kerberos
) {
1296 saf_store(domain
->alt_name
, controller
);
1299 winbindd_set_locator_kdc_envs(domain
);
1304 result
= NT_STATUS_OK
;
1309 if (!NT_STATUS_IS_OK(result
)) {
1310 winbind_add_failed_connection_entry(domain
, controller
, result
);
1311 if ((*cli
) != NULL
) {
1320 /*******************************************************************
1321 Add a dcname and sockaddr_storage pair to the end of a dc_name_ip
1324 Keeps the list unique by not adding duplicate entries.
1326 @param[in] mem_ctx talloc memory context to allocate from
1327 @param[in] domain_name domain of the DC
1328 @param[in] dcname name of the DC to add to the list
1329 @param[in] pss Internet address and port pair to add to the list
1330 @param[in,out] dcs array of dc_name_ip structures to add to
1331 @param[in,out] num_dcs number of dcs returned in the dcs array
1332 @return true if the list was added to, false otherwise
1333 *******************************************************************/
1335 static bool add_one_dc_unique(TALLOC_CTX
*mem_ctx
, const char *domain_name
,
1336 const char *dcname
, struct sockaddr_storage
*pss
,
1337 struct dc_name_ip
**dcs
, int *num
)
1341 if (!NT_STATUS_IS_OK(check_negative_conn_cache(domain_name
, dcname
))) {
1342 DEBUG(10, ("DC %s was in the negative conn cache\n", dcname
));
1346 /* Make sure there's no duplicates in the list */
1347 for (i
=0; i
<*num
; i
++)
1349 (struct sockaddr
*)(void *)&(*dcs
)[i
].ss
,
1350 (struct sockaddr
*)(void *)pss
))
1353 *dcs
= talloc_realloc(mem_ctx
, *dcs
, struct dc_name_ip
, (*num
)+1);
1358 fstrcpy((*dcs
)[*num
].name
, dcname
);
1359 (*dcs
)[*num
].ss
= *pss
;
1364 static bool add_sockaddr_to_array(TALLOC_CTX
*mem_ctx
,
1365 struct sockaddr_storage
*pss
, uint16_t port
,
1366 struct sockaddr_storage
**addrs
, int *num
)
1368 *addrs
= talloc_realloc(mem_ctx
, *addrs
, struct sockaddr_storage
, (*num
)+1);
1370 if (*addrs
== NULL
) {
1375 (*addrs
)[*num
] = *pss
;
1376 set_sockaddr_port((struct sockaddr
*)&(*addrs
)[*num
], port
);
1382 /*******************************************************************
1383 convert an ip to a name
1384 *******************************************************************/
1386 static bool dcip_to_name(TALLOC_CTX
*mem_ctx
,
1387 const struct winbindd_domain
*domain
,
1388 struct sockaddr_storage
*pss
,
1391 struct ip_service ip_list
;
1392 uint32_t nt_version
= NETLOGON_NT_VERSION_1
;
1394 const char *dc_name
;
1397 bool is_ad_domain
= false;
1403 /* For active directory servers, try to get the ldap server name.
1404 None of these failures should be considered critical for now */
1406 if ((lp_security() == SEC_ADS
) && (domain
->alt_name
!= NULL
)) {
1407 is_ad_domain
= true;
1408 } else if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC
) {
1409 is_ad_domain
= domain
->active_directory
;
1414 ADS_STATUS ads_status
;
1415 char addr
[INET6_ADDRSTRLEN
];
1417 print_sockaddr(addr
, sizeof(addr
), pss
);
1419 ads
= ads_init(domain
->alt_name
, domain
->name
, addr
);
1420 ads
->auth
.flags
|= ADS_AUTH_NO_BIND
;
1422 ads_status
= ads_connect(ads
);
1423 if (ADS_ERR_OK(ads_status
)) {
1424 /* We got a cldap packet. */
1425 *name
= talloc_strdup(mem_ctx
,
1426 ads
->config
.ldap_server_name
);
1427 if (*name
== NULL
) {
1430 namecache_store(*name
, 0x20, 1, &ip_list
);
1432 DEBUG(10,("dcip_to_name: flags = 0x%x\n", (unsigned int)ads
->config
.flags
));
1434 if (domain
->primary
&& (ads
->config
.flags
& NBT_SERVER_KDC
)) {
1435 if (ads_closest_dc(ads
)) {
1436 char *sitename
= sitename_fetch(mem_ctx
, ads
->config
.realm
);
1438 /* We're going to use this KDC for this realm/domain.
1439 If we are using sites, then force the krb5 libs
1442 create_local_private_krb5_conf_for_domain(domain
->alt_name
,
1447 TALLOC_FREE(sitename
);
1449 /* use an off site KDC */
1450 create_local_private_krb5_conf_for_domain(domain
->alt_name
,
1455 winbindd_set_locator_kdc_envs(domain
);
1457 /* Ensure we contact this DC also. */
1458 saf_store(domain
->name
, *name
);
1459 saf_store(domain
->alt_name
, *name
);
1462 ads_destroy( &ads
);
1466 ads_destroy( &ads
);
1471 status
= nbt_getdc(winbind_messaging_context(), 10, pss
, domain
->name
,
1472 &domain
->sid
, nt_version
, mem_ctx
, &nt_version
,
1474 if (NT_STATUS_IS_OK(status
)) {
1475 *name
= talloc_strdup(mem_ctx
, dc_name
);
1476 if (*name
== NULL
) {
1479 namecache_store(*name
, 0x20, 1, &ip_list
);
1483 /* try node status request */
1485 if (name_status_find(domain
->name
, 0x1c, 0x20, pss
, nbtname
) ) {
1486 namecache_store(nbtname
, 0x20, 1, &ip_list
);
1489 *name
= talloc_strdup(mem_ctx
, nbtname
);
1490 if (*name
== NULL
) {
1500 /*******************************************************************
1501 Retrieve a list of IP addresses for domain controllers.
1503 The array is sorted in the preferred connection order.
1505 @param[in] mem_ctx talloc memory context to allocate from
1506 @param[in] domain domain to retrieve DCs for
1507 @param[out] dcs array of dcs that will be returned
1508 @param[out] num_dcs number of dcs returned in the dcs array
1510 *******************************************************************/
1512 static bool get_dcs(TALLOC_CTX
*mem_ctx
, struct winbindd_domain
*domain
,
1513 struct dc_name_ip
**dcs
, int *num_dcs
)
1516 struct sockaddr_storage ss
;
1517 struct ip_service
*ip_list
= NULL
;
1518 int iplist_size
= 0;
1521 enum security_types sec
= (enum security_types
)lp_security();
1523 is_our_domain
= strequal(domain
->name
, lp_workgroup());
1525 /* If not our domain, get the preferred DC, by asking our primary DC */
1527 && get_dc_name_via_netlogon(domain
, dcname
, &ss
)
1528 && add_one_dc_unique(mem_ctx
, domain
->name
, dcname
, &ss
, dcs
,
1531 char addr
[INET6_ADDRSTRLEN
];
1532 print_sockaddr(addr
, sizeof(addr
), &ss
);
1533 DEBUG(10, ("Retrieved DC %s at %s via netlogon\n",
1538 if ((sec
== SEC_ADS
) && (domain
->alt_name
!= NULL
)) {
1539 char *sitename
= NULL
;
1541 /* We need to make sure we know the local site before
1542 doing any DNS queries, as this will restrict the
1543 get_sorted_dc_list() call below to only fetching
1544 DNS records for the correct site. */
1546 /* Find any DC to get the site record.
1547 We deliberately don't care about the
1550 get_dc_name(domain
->name
, domain
->alt_name
, dcname
, &ss
);
1552 sitename
= sitename_fetch(mem_ctx
, domain
->alt_name
);
1555 /* Do the site-specific AD dns lookup first. */
1556 get_sorted_dc_list(domain
->alt_name
, sitename
, &ip_list
,
1557 &iplist_size
, True
);
1559 /* Add ips to the DC array. We don't look up the name
1560 of the DC in this function, but we fill in the char*
1561 of the ip now to make the failed connection cache
1563 for ( i
=0; i
<iplist_size
; i
++ ) {
1564 char addr
[INET6_ADDRSTRLEN
];
1565 print_sockaddr(addr
, sizeof(addr
),
1567 add_one_dc_unique(mem_ctx
,
1576 TALLOC_FREE(sitename
);
1580 /* Now we add DCs from the main AD DNS lookup. */
1581 get_sorted_dc_list(domain
->alt_name
, NULL
, &ip_list
,
1582 &iplist_size
, True
);
1584 for ( i
=0; i
<iplist_size
; i
++ ) {
1585 char addr
[INET6_ADDRSTRLEN
];
1586 print_sockaddr(addr
, sizeof(addr
),
1588 add_one_dc_unique(mem_ctx
,
1600 /* Try standard netbios queries if no ADS and fall back to DNS queries
1601 * if alt_name is available */
1602 if (*num_dcs
== 0) {
1603 get_sorted_dc_list(domain
->name
, NULL
, &ip_list
, &iplist_size
,
1605 if (iplist_size
== 0) {
1606 if (domain
->alt_name
!= NULL
) {
1607 get_sorted_dc_list(domain
->alt_name
, NULL
, &ip_list
,
1608 &iplist_size
, true);
1612 for ( i
=0; i
<iplist_size
; i
++ ) {
1613 char addr
[INET6_ADDRSTRLEN
];
1614 print_sockaddr(addr
, sizeof(addr
),
1616 add_one_dc_unique(mem_ctx
,
1631 /*******************************************************************
1632 Find and make a connection to a DC in the given domain.
1634 @param[in] mem_ctx talloc memory context to allocate from
1635 @param[in] domain domain to find a dc in
1636 @param[out] dcname NetBIOS or FQDN of DC that's connected to
1637 @param[out] pss DC Internet address and port
1638 @param[out] fd fd of the open socket connected to the newly found dc
1639 @return true when a DC connection is made, false otherwise
1640 *******************************************************************/
1642 static bool find_new_dc(TALLOC_CTX
*mem_ctx
,
1643 struct winbindd_domain
*domain
,
1644 char **dcname
, struct sockaddr_storage
*pss
, int *fd
)
1646 struct dc_name_ip
*dcs
= NULL
;
1649 const char **dcnames
= NULL
;
1650 size_t num_dcnames
= 0;
1652 struct sockaddr_storage
*addrs
= NULL
;
1663 if (!get_dcs(mem_ctx
, domain
, &dcs
, &num_dcs
) || (num_dcs
== 0))
1666 for (i
=0; i
<num_dcs
; i
++) {
1668 if (!add_string_to_array(mem_ctx
, dcs
[i
].name
,
1669 &dcnames
, &num_dcnames
)) {
1672 if (!add_sockaddr_to_array(mem_ctx
, &dcs
[i
].ss
, TCP_SMB_PORT
,
1673 &addrs
, &num_addrs
)) {
1678 if ((num_dcnames
== 0) || (num_dcnames
!= num_addrs
))
1681 if ((addrs
== NULL
) || (dcnames
== NULL
))
1684 status
= smbsock_any_connect(addrs
, dcnames
, NULL
, NULL
, NULL
,
1685 num_addrs
, 0, 10, fd
, &fd_index
, NULL
);
1686 if (!NT_STATUS_IS_OK(status
)) {
1687 for (i
=0; i
<num_dcs
; i
++) {
1688 char ab
[INET6_ADDRSTRLEN
];
1689 print_sockaddr(ab
, sizeof(ab
), &dcs
[i
].ss
);
1690 DEBUG(10, ("find_new_dc: smbsock_any_connect failed for "
1691 "domain %s address %s. Error was %s\n",
1692 domain
->name
, ab
, nt_errstr(status
) ));
1693 winbind_add_failed_connection_entry(domain
,
1694 dcs
[i
].name
, NT_STATUS_UNSUCCESSFUL
);
1699 *pss
= addrs
[fd_index
];
1701 if (*dcnames
[fd_index
] != '\0' && !is_ipaddress(dcnames
[fd_index
])) {
1702 /* Ok, we've got a name for the DC */
1703 *dcname
= talloc_strdup(mem_ctx
, dcnames
[fd_index
]);
1704 if (*dcname
== NULL
) {
1710 /* Try to figure out the name */
1711 if (dcip_to_name(mem_ctx
, domain
, pss
, dcname
)) {
1715 /* We can not continue without the DC's name */
1716 winbind_add_failed_connection_entry(domain
, dcs
[fd_index
].name
,
1717 NT_STATUS_UNSUCCESSFUL
);
1719 /* Throw away all arrays as we're doing this again. */
1723 TALLOC_FREE(dcnames
);
1735 static char *current_dc_key(TALLOC_CTX
*mem_ctx
, const char *domain_name
)
1737 return talloc_asprintf_strupper_m(mem_ctx
, "CURRENT_DCNAME/%s",
1741 static void store_current_dc_in_gencache(const char *domain_name
,
1742 const char *dc_name
,
1743 struct cli_state
*cli
)
1745 char addr
[INET6_ADDRSTRLEN
];
1749 if (!cli_state_is_connected(cli
)) {
1753 print_sockaddr(addr
, sizeof(addr
),
1754 smbXcli_conn_remote_sockaddr(cli
->conn
));
1756 key
= current_dc_key(talloc_tos(), domain_name
);
1761 value
= talloc_asprintf(talloc_tos(), "%s %s", addr
, dc_name
);
1762 if (value
== NULL
) {
1766 gencache_set(key
, value
, 0x7fffffff);
1772 bool fetch_current_dc_from_gencache(TALLOC_CTX
*mem_ctx
,
1773 const char *domain_name
,
1774 char **p_dc_name
, char **p_dc_ip
)
1779 char *dc_name
= NULL
;
1782 key
= current_dc_key(talloc_tos(), domain_name
);
1786 if (!gencache_get(key
, mem_ctx
, &value
, NULL
)) {
1789 p
= strchr(value
, ' ');
1793 dc_ip
= talloc_strndup(mem_ctx
, value
, p
- value
);
1794 if (dc_ip
== NULL
) {
1797 dc_name
= talloc_strdup(mem_ctx
, p
+1);
1798 if (dc_name
== NULL
) {
1802 if (p_dc_ip
!= NULL
) {
1806 if (p_dc_name
!= NULL
) {
1807 *p_dc_name
= dc_name
;
1812 TALLOC_FREE(dc_name
);
1819 NTSTATUS
wb_open_internal_pipe(TALLOC_CTX
*mem_ctx
,
1820 const struct ndr_interface_table
*table
,
1821 struct rpc_pipe_client
**ret_pipe
)
1823 struct rpc_pipe_client
*cli
= NULL
;
1824 const struct auth_session_info
*session_info
;
1825 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1828 session_info
= get_session_info_system();
1829 SMB_ASSERT(session_info
!= NULL
);
1831 /* create a connection to the specified pipe */
1832 if (lp_parm_bool(-1, "winbindd", "use external pipes", false)) {
1833 status
= rpc_pipe_open_interface(mem_ctx
,
1837 winbind_messaging_context(),
1840 status
= rpc_pipe_open_internal(mem_ctx
,
1844 winbind_messaging_context(),
1847 if (!NT_STATUS_IS_OK(status
)) {
1848 DEBUG(0, ("open_internal_pipe: Could not connect to %s pipe: %s\n",
1849 table
->name
, nt_errstr(status
)));
1857 return NT_STATUS_OK
;
1860 static NTSTATUS
cm_open_connection(struct winbindd_domain
*domain
,
1861 struct winbindd_cm_conn
*new_conn
)
1863 TALLOC_CTX
*mem_ctx
;
1865 char *saf_servername
;
1868 if ((mem_ctx
= talloc_init("cm_open_connection")) == NULL
) {
1869 set_domain_offline(domain
);
1870 return NT_STATUS_NO_MEMORY
;
1873 saf_servername
= saf_fetch(mem_ctx
, domain
->name
);
1875 /* we have to check the server affinity cache here since
1876 later we select a DC based on response time and not preference */
1878 /* Check the negative connection cache
1879 before talking to it. It going down may have
1880 triggered the reconnection. */
1882 if ( saf_servername
&& NT_STATUS_IS_OK(check_negative_conn_cache( domain
->name
, saf_servername
))) {
1884 DEBUG(10,("cm_open_connection: saf_servername is '%s' for domain %s\n",
1885 saf_servername
, domain
->name
));
1887 /* convert an ip address to a name */
1888 if (is_ipaddress( saf_servername
) ) {
1889 char *dcname
= NULL
;
1890 struct sockaddr_storage ss
;
1892 if (!interpret_string_addr(&ss
, saf_servername
,
1894 TALLOC_FREE(mem_ctx
);
1895 return NT_STATUS_UNSUCCESSFUL
;
1897 if (dcip_to_name(mem_ctx
, domain
, &ss
, &dcname
)) {
1898 domain
->dcname
= talloc_strdup(domain
,
1900 if (domain
->dcname
== NULL
) {
1901 TALLOC_FREE(mem_ctx
);
1902 return NT_STATUS_NO_MEMORY
;
1905 winbind_add_failed_connection_entry(
1906 domain
, saf_servername
,
1907 NT_STATUS_UNSUCCESSFUL
);
1910 domain
->dcname
= talloc_strdup(domain
, saf_servername
);
1911 if (domain
->dcname
== NULL
) {
1912 TALLOC_FREE(mem_ctx
);
1913 return NT_STATUS_NO_MEMORY
;
1918 for (retries
= 0; retries
< 3; retries
++) {
1921 char *dcname
= NULL
;
1923 result
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
1925 DEBUG(10,("cm_open_connection: dcname is '%s' for domain %s\n",
1926 domain
->dcname
? domain
->dcname
: "", domain
->name
));
1928 if (domain
->dcname
!= NULL
1929 && NT_STATUS_IS_OK(check_negative_conn_cache( domain
->name
, domain
->dcname
))
1930 && (resolve_name(domain
->dcname
, &domain
->dcaddr
, 0x20, true)))
1934 status
= smbsock_connect(&domain
->dcaddr
, 0,
1937 if (!NT_STATUS_IS_OK(status
)) {
1943 !find_new_dc(mem_ctx
, domain
, &dcname
, &domain
->dcaddr
, &fd
))
1945 /* This is the one place where we will
1946 set the global winbindd offline state
1947 to true, if a "WINBINDD_OFFLINE" entry
1948 is found in the winbindd cache. */
1949 set_global_winbindd_state_offline();
1952 if (dcname
!= NULL
) {
1953 talloc_free(domain
->dcname
);
1955 domain
->dcname
= talloc_move(domain
, &dcname
);
1956 if (domain
->dcname
== NULL
) {
1957 result
= NT_STATUS_NO_MEMORY
;
1962 new_conn
->cli
= NULL
;
1964 result
= cm_prepare_connection(domain
, fd
, domain
->dcname
,
1965 &new_conn
->cli
, &retry
);
1966 if (!NT_STATUS_IS_OK(result
)) {
1967 /* Don't leak the smb connection socket */
1975 if (NT_STATUS_IS_OK(result
)) {
1976 bool seal_pipes
= true;
1978 winbindd_set_locator_kdc_envs(domain
);
1980 if (domain
->online
== False
) {
1981 /* We're changing state from offline to online. */
1982 set_global_winbindd_state_online();
1984 set_domain_online(domain
);
1987 * Much as I hate global state, this seems to be the point
1988 * where we can be certain that we have a proper connection to
1989 * a DC. wbinfo --dc-info needs that information, store it in
1990 * gencache with a looong timeout. This will need revisiting
1991 * once we start to connect to multiple DCs, wbcDcInfo is
1992 * already prepared for that.
1994 store_current_dc_in_gencache(domain
->name
, domain
->dcname
,
1997 seal_pipes
= lp_winbind_sealed_pipes();
1998 seal_pipes
= lp_parm_bool(-1, "winbind sealed pipes",
2003 new_conn
->auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
2005 new_conn
->auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
2008 /* Ensure we setup the retry handler. */
2009 set_domain_offline(domain
);
2012 talloc_destroy(mem_ctx
);
2016 /* Close down all open pipes on a connection. */
2018 void invalidate_cm_connection(struct winbindd_domain
*domain
)
2021 struct winbindd_cm_conn
*conn
= &domain
->conn
;
2023 /* We're closing down a possibly dead
2024 connection. Don't have impossibly long (10s) timeouts. */
2027 cli_set_timeout(conn
->cli
, 1000); /* 1 second. */
2030 if (conn
->samr_pipe
!= NULL
) {
2031 if (is_valid_policy_hnd(&conn
->sam_connect_handle
)) {
2032 dcerpc_samr_Close(conn
->samr_pipe
->binding_handle
,
2034 &conn
->sam_connect_handle
,
2037 TALLOC_FREE(conn
->samr_pipe
);
2038 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
2040 cli_set_timeout(conn
->cli
, 500);
2044 if (conn
->lsa_pipe
!= NULL
) {
2045 if (is_valid_policy_hnd(&conn
->lsa_policy
)) {
2046 dcerpc_lsa_Close(conn
->lsa_pipe
->binding_handle
,
2051 TALLOC_FREE(conn
->lsa_pipe
);
2052 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
2054 cli_set_timeout(conn
->cli
, 500);
2058 if (conn
->lsa_pipe_tcp
!= NULL
) {
2059 if (is_valid_policy_hnd(&conn
->lsa_policy
)) {
2060 dcerpc_lsa_Close(conn
->lsa_pipe_tcp
->binding_handle
,
2065 TALLOC_FREE(conn
->lsa_pipe_tcp
);
2066 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
2068 cli_set_timeout(conn
->cli
, 500);
2072 if (conn
->netlogon_pipe
!= NULL
) {
2073 TALLOC_FREE(conn
->netlogon_pipe
);
2074 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
2076 cli_set_timeout(conn
->cli
, 500);
2080 conn
->auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
2081 conn
->netlogon_force_reauth
= false;
2082 conn
->netlogon_flags
= 0;
2083 TALLOC_FREE(conn
->netlogon_creds
);
2086 cli_shutdown(conn
->cli
);
2092 void close_conns_after_fork(void)
2094 struct winbindd_domain
*domain
;
2095 struct winbindd_cli_state
*cli_state
;
2097 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
2099 * first close the low level SMB TCP connection
2100 * so that we don't generate any SMBclose
2101 * requests in invalidate_cm_connection()
2103 if (cli_state_is_connected(domain
->conn
.cli
)) {
2104 smbXcli_conn_disconnect(domain
->conn
.cli
->conn
, NT_STATUS_OK
);
2107 invalidate_cm_connection(domain
);
2110 for (cli_state
= winbindd_client_list();
2112 cli_state
= cli_state
->next
) {
2113 if (cli_state
->sock
>= 0) {
2114 close(cli_state
->sock
);
2115 cli_state
->sock
= -1;
2120 static bool connection_ok(struct winbindd_domain
*domain
)
2124 ok
= cli_state_is_connected(domain
->conn
.cli
);
2126 DEBUG(3, ("connection_ok: Connection to %s for domain %s is not connected\n",
2127 domain
->dcname
, domain
->name
));
2131 if (domain
->online
== False
) {
2132 DEBUG(3, ("connection_ok: Domain %s is offline\n", domain
->name
));
2139 /* Initialize a new connection up to the RPC BIND.
2140 Bypass online status check so always does network calls. */
2142 static NTSTATUS
init_dc_connection_network(struct winbindd_domain
*domain
, bool need_rw_dc
)
2145 bool skip_connection
= domain
->internal
;
2146 if (need_rw_dc
&& domain
->rodc
) {
2147 skip_connection
= false;
2150 /* Internal connections never use the network. */
2151 if (dom_sid_equal(&domain
->sid
, &global_sid_Builtin
)) {
2152 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2155 /* Still ask the internal LSA and SAMR server about the local domain */
2156 if (skip_connection
|| connection_ok(domain
)) {
2157 if (!domain
->initialized
) {
2158 set_dc_type_and_flags(domain
);
2160 return NT_STATUS_OK
;
2163 invalidate_cm_connection(domain
);
2165 if (!domain
->primary
&& !domain
->initialized
) {
2167 * Before we connect to a trust, work out if it is an
2168 * AD domain by asking our own domain.
2170 set_dc_type_and_flags_trustinfo(domain
);
2173 result
= cm_open_connection(domain
, &domain
->conn
);
2175 if (NT_STATUS_IS_OK(result
) && !domain
->initialized
) {
2176 set_dc_type_and_flags(domain
);
2182 NTSTATUS
init_dc_connection(struct winbindd_domain
*domain
, bool need_rw_dc
)
2184 if (dom_sid_equal(&domain
->sid
, &global_sid_Builtin
)) {
2185 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2188 if (domain
->initialized
&& !domain
->online
) {
2189 /* We check for online status elsewhere. */
2190 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
2193 return init_dc_connection_network(domain
, need_rw_dc
);
2196 static NTSTATUS
init_dc_connection_rpc(struct winbindd_domain
*domain
, bool need_rw_dc
)
2200 status
= init_dc_connection(domain
, need_rw_dc
);
2201 if (!NT_STATUS_IS_OK(status
)) {
2205 if (!domain
->internal
&& domain
->conn
.cli
== NULL
) {
2206 /* happens for trusted domains without inbound trust */
2207 return NT_STATUS_TRUSTED_DOMAIN_FAILURE
;
2210 return NT_STATUS_OK
;
2213 /******************************************************************************
2214 Set the trust flags (direction and forest location) for a domain
2215 ******************************************************************************/
2217 static bool set_dc_type_and_flags_trustinfo( struct winbindd_domain
*domain
)
2219 struct winbindd_domain
*our_domain
;
2220 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2222 struct netr_DomainTrustList trusts
;
2224 uint32_t flags
= (NETR_TRUST_FLAG_IN_FOREST
|
2225 NETR_TRUST_FLAG_OUTBOUND
|
2226 NETR_TRUST_FLAG_INBOUND
);
2227 struct rpc_pipe_client
*cli
;
2228 TALLOC_CTX
*mem_ctx
= NULL
;
2229 struct dcerpc_binding_handle
*b
;
2231 DEBUG(5, ("set_dc_type_and_flags_trustinfo: domain %s\n", domain
->name
));
2233 /* Our primary domain doesn't need to worry about trust flags.
2234 Force it to go through the network setup */
2235 if ( domain
->primary
) {
2239 mem_ctx
= talloc_stackframe();
2240 our_domain
= find_our_domain();
2241 if (our_domain
->internal
) {
2242 result
= init_dc_connection(our_domain
, false);
2243 if (!NT_STATUS_IS_OK(result
)) {
2244 DEBUG(3,("set_dc_type_and_flags_trustinfo: "
2245 "Not able to make a connection to our domain: %s\n",
2246 nt_errstr(result
)));
2247 TALLOC_FREE(mem_ctx
);
2252 /* This won't work unless our domain is AD */
2253 if ( !our_domain
->active_directory
) {
2254 TALLOC_FREE(mem_ctx
);
2258 if (our_domain
->internal
) {
2259 result
= wb_open_internal_pipe(mem_ctx
, &ndr_table_netlogon
, &cli
);
2260 } else if (!connection_ok(our_domain
)) {
2261 DEBUG(3,("set_dc_type_and_flags_trustinfo: "
2262 "No connection to our domain!\n"));
2263 TALLOC_FREE(mem_ctx
);
2266 result
= cm_connect_netlogon(our_domain
, &cli
);
2269 if (!NT_STATUS_IS_OK(result
)) {
2270 DEBUG(5, ("set_dc_type_and_flags_trustinfo: Could not open "
2271 "a connection to %s for PIPE_NETLOGON (%s)\n",
2272 domain
->name
, nt_errstr(result
)));
2273 TALLOC_FREE(mem_ctx
);
2276 b
= cli
->binding_handle
;
2278 /* Use DsEnumerateDomainTrusts to get us the trust direction and type. */
2279 result
= dcerpc_netr_DsrEnumerateDomainTrusts(b
, mem_ctx
,
2284 if (!NT_STATUS_IS_OK(result
)) {
2285 DEBUG(0,("set_dc_type_and_flags_trustinfo: "
2286 "failed to query trusted domain list: %s\n",
2287 nt_errstr(result
)));
2288 TALLOC_FREE(mem_ctx
);
2291 if (!W_ERROR_IS_OK(werr
)) {
2292 DEBUG(0,("set_dc_type_and_flags_trustinfo: "
2293 "failed to query trusted domain list: %s\n",
2295 TALLOC_FREE(mem_ctx
);
2299 /* Now find the domain name and get the flags */
2301 for ( i
=0; i
<trusts
.count
; i
++ ) {
2302 if ( strequal( domain
->name
, trusts
.array
[i
].netbios_name
) ) {
2303 domain
->domain_flags
= trusts
.array
[i
].trust_flags
;
2304 domain
->domain_type
= trusts
.array
[i
].trust_type
;
2305 domain
->domain_trust_attribs
= trusts
.array
[i
].trust_attributes
;
2307 if ( domain
->domain_type
== LSA_TRUST_TYPE_UPLEVEL
)
2308 domain
->active_directory
= True
;
2310 /* This flag is only set if the domain is *our*
2311 primary domain and the primary domain is in
2314 domain
->native_mode
= (domain
->domain_flags
& NETR_TRUST_FLAG_NATIVE
);
2316 DEBUG(5, ("set_dc_type_and_flags_trustinfo: domain %s is %sin "
2317 "native mode.\n", domain
->name
,
2318 domain
->native_mode
? "" : "NOT "));
2320 DEBUG(5,("set_dc_type_and_flags_trustinfo: domain %s is %s"
2321 "running active directory.\n", domain
->name
,
2322 domain
->active_directory
? "" : "NOT "));
2324 domain
->can_do_ncacn_ip_tcp
= domain
->active_directory
;
2326 domain
->initialized
= True
;
2332 TALLOC_FREE(mem_ctx
);
2334 return domain
->initialized
;
2337 /******************************************************************************
2338 We can 'sense' certain things about the DC by it's replies to certain
2341 This tells us if this particular remote server is Active Directory, and if it
2343 ******************************************************************************/
2345 static void set_dc_type_and_flags_connect( struct winbindd_domain
*domain
)
2347 NTSTATUS status
, result
;
2349 TALLOC_CTX
*mem_ctx
= NULL
;
2350 struct rpc_pipe_client
*cli
= NULL
;
2351 struct policy_handle pol
;
2352 union dssetup_DsRoleInfo info
;
2353 union lsa_PolicyInformation
*lsa_info
= NULL
;
2355 if (!domain
->internal
&& !connection_ok(domain
)) {
2359 mem_ctx
= talloc_init("set_dc_type_and_flags on domain %s\n",
2362 DEBUG(1, ("set_dc_type_and_flags_connect: talloc_init() failed\n"));
2366 DEBUG(5, ("set_dc_type_and_flags_connect: domain %s\n", domain
->name
));
2368 if (domain
->internal
) {
2369 status
= wb_open_internal_pipe(mem_ctx
,
2373 status
= cli_rpc_pipe_open_noauth(domain
->conn
.cli
,
2378 if (!NT_STATUS_IS_OK(status
)) {
2379 DEBUG(5, ("set_dc_type_and_flags_connect: Could not bind to "
2380 "PI_DSSETUP on domain %s: (%s)\n",
2381 domain
->name
, nt_errstr(status
)));
2383 /* if this is just a non-AD domain we need to continue
2384 * identifying so that we can in the end return with
2385 * domain->initialized = True - gd */
2390 status
= dcerpc_dssetup_DsRoleGetPrimaryDomainInformation(cli
->binding_handle
, mem_ctx
,
2391 DS_ROLE_BASIC_INFORMATION
,
2396 if (NT_STATUS_IS_OK(status
)) {
2397 result
= werror_to_ntstatus(werr
);
2399 if (!NT_STATUS_IS_OK(status
)) {
2400 DEBUG(5, ("set_dc_type_and_flags_connect: rpccli_ds_getprimarydominfo "
2401 "on domain %s failed: (%s)\n",
2402 domain
->name
, nt_errstr(status
)));
2404 /* older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for
2405 * every opcode on the DSSETUP pipe, continue with
2406 * no_dssetup mode here as well to get domain->initialized
2409 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
)) {
2413 TALLOC_FREE(mem_ctx
);
2417 if ((info
.basic
.flags
& DS_ROLE_PRIMARY_DS_RUNNING
) &&
2418 !(info
.basic
.flags
& DS_ROLE_PRIMARY_DS_MIXED_MODE
)) {
2419 domain
->native_mode
= True
;
2421 domain
->native_mode
= False
;
2425 if (domain
->internal
) {
2426 status
= wb_open_internal_pipe(mem_ctx
,
2430 status
= cli_rpc_pipe_open_noauth(domain
->conn
.cli
,
2431 &ndr_table_lsarpc
, &cli
);
2433 if (!NT_STATUS_IS_OK(status
)) {
2434 DEBUG(5, ("set_dc_type_and_flags_connect: Could not bind to "
2435 "PI_LSARPC on domain %s: (%s)\n",
2436 domain
->name
, nt_errstr(status
)));
2438 TALLOC_FREE(mem_ctx
);
2442 status
= rpccli_lsa_open_policy2(cli
, mem_ctx
, True
,
2443 SEC_FLAG_MAXIMUM_ALLOWED
, &pol
);
2445 if (NT_STATUS_IS_OK(status
)) {
2446 /* This particular query is exactly what Win2k clients use
2447 to determine that the DC is active directory */
2448 status
= dcerpc_lsa_QueryInfoPolicy2(cli
->binding_handle
, mem_ctx
,
2450 LSA_POLICY_INFO_DNS
,
2455 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
2456 domain
->active_directory
= True
;
2458 if (lsa_info
->dns
.name
.string
) {
2459 if (!strequal(domain
->name
, lsa_info
->dns
.name
.string
))
2461 DEBUG(1, ("set_dc_type_and_flags_connect: DC "
2462 "for domain %s claimed it was a DC "
2463 "for domain %s, refusing to "
2466 lsa_info
->dns
.name
.string
));
2468 TALLOC_FREE(mem_ctx
);
2471 talloc_free(domain
->name
);
2472 domain
->name
= talloc_strdup(domain
,
2473 lsa_info
->dns
.name
.string
);
2474 if (domain
->name
== NULL
) {
2479 if (lsa_info
->dns
.dns_domain
.string
) {
2480 if (domain
->alt_name
!= NULL
&&
2481 !strequal(domain
->alt_name
,
2482 lsa_info
->dns
.dns_domain
.string
))
2484 DEBUG(1, ("set_dc_type_and_flags_connect: DC "
2485 "for domain %s (%s) claimed it was "
2486 "a DC for domain %s, refusing to "
2488 domain
->alt_name
, domain
->name
,
2489 lsa_info
->dns
.dns_domain
.string
));
2491 TALLOC_FREE(mem_ctx
);
2494 talloc_free(domain
->alt_name
);
2496 talloc_strdup(domain
,
2497 lsa_info
->dns
.dns_domain
.string
);
2498 if (domain
->alt_name
== NULL
) {
2503 /* See if we can set some domain trust flags about
2506 if (lsa_info
->dns
.dns_forest
.string
) {
2507 talloc_free(domain
->forest_name
);
2508 domain
->forest_name
=
2509 talloc_strdup(domain
,
2510 lsa_info
->dns
.dns_forest
.string
);
2511 if (domain
->forest_name
== NULL
) {
2515 if (strequal(domain
->forest_name
, domain
->alt_name
)) {
2516 domain
->domain_flags
|= NETR_TRUST_FLAG_TREEROOT
;
2520 if (lsa_info
->dns
.sid
) {
2521 if (!is_null_sid(&domain
->sid
) &&
2522 !dom_sid_equal(&domain
->sid
,
2525 DEBUG(1, ("set_dc_type_and_flags_connect: DC "
2526 "for domain %s (%s) claimed it was "
2527 "a DC for domain %s, refusing to "
2529 dom_sid_string(talloc_tos(),
2532 dom_sid_string(talloc_tos(),
2533 lsa_info
->dns
.sid
)));
2535 TALLOC_FREE(mem_ctx
);
2538 sid_copy(&domain
->sid
, lsa_info
->dns
.sid
);
2541 domain
->active_directory
= False
;
2543 status
= rpccli_lsa_open_policy(cli
, mem_ctx
, True
,
2544 SEC_FLAG_MAXIMUM_ALLOWED
,
2547 if (!NT_STATUS_IS_OK(status
)) {
2551 status
= dcerpc_lsa_QueryInfoPolicy(cli
->binding_handle
, mem_ctx
,
2553 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
2556 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
2558 if (lsa_info
->account_domain
.name
.string
) {
2559 if (!strequal(domain
->name
,
2560 lsa_info
->account_domain
.name
.string
))
2563 ("set_dc_type_and_flags_connect: "
2564 "DC for domain %s claimed it was"
2565 " a DC for domain %s, refusing "
2566 "to initialize\n", domain
->name
,
2568 account_domain
.name
.string
));
2570 TALLOC_FREE(mem_ctx
);
2573 talloc_free(domain
->name
);
2575 talloc_strdup(domain
,
2576 lsa_info
->account_domain
.name
.string
);
2579 if (lsa_info
->account_domain
.sid
) {
2580 if (!is_null_sid(&domain
->sid
) &&
2581 !dom_sid_equal(&domain
->sid
,
2582 lsa_info
->account_domain
.sid
))
2585 ("set_dc_type_and_flags_connect: "
2586 "DC for domain %s (%s) claimed "
2587 "it was a DC for domain %s, "
2588 "refusing to initialize\n",
2589 dom_sid_string(talloc_tos(),
2592 dom_sid_string(talloc_tos(),
2593 lsa_info
->account_domain
.sid
)));
2595 TALLOC_FREE(mem_ctx
);
2598 sid_copy(&domain
->sid
, lsa_info
->account_domain
.sid
);
2604 DEBUG(5, ("set_dc_type_and_flags_connect: domain %s is %sin native mode.\n",
2605 domain
->name
, domain
->native_mode
? "" : "NOT "));
2607 DEBUG(5,("set_dc_type_and_flags_connect: domain %s is %srunning active directory.\n",
2608 domain
->name
, domain
->active_directory
? "" : "NOT "));
2610 domain
->can_do_ncacn_ip_tcp
= domain
->active_directory
;
2614 TALLOC_FREE(mem_ctx
);
2616 domain
->initialized
= True
;
2619 /**********************************************************************
2620 Set the domain_flags (trust attributes, domain operating modes, etc...
2621 ***********************************************************************/
2623 static void set_dc_type_and_flags( struct winbindd_domain
*domain
)
2625 /* we always have to contact our primary domain */
2627 if ( domain
->primary
|| domain
->internal
) {
2628 DEBUG(10,("set_dc_type_and_flags: setting up flags for "
2629 "primary or internal domain\n"));
2630 set_dc_type_and_flags_connect( domain
);
2634 /* Use our DC to get the information if possible */
2636 if ( !set_dc_type_and_flags_trustinfo( domain
) ) {
2637 /* Otherwise, fallback to contacting the
2639 set_dc_type_and_flags_connect( domain
);
2647 /**********************************************************************
2648 ***********************************************************************/
2650 static NTSTATUS
cm_get_schannel_creds(struct winbindd_domain
*domain
,
2651 struct netlogon_creds_cli_context
**ppdc
)
2653 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2654 struct rpc_pipe_client
*netlogon_pipe
;
2658 if ((!IS_DC
) && (!domain
->primary
)) {
2659 return NT_STATUS_TRUSTED_DOMAIN_FAILURE
;
2662 if (domain
->conn
.netlogon_creds
!= NULL
) {
2663 if (!(domain
->conn
.netlogon_flags
& NETLOGON_NEG_AUTHENTICATED_RPC
)) {
2664 return NT_STATUS_TRUSTED_DOMAIN_FAILURE
;
2666 *ppdc
= domain
->conn
.netlogon_creds
;
2667 return NT_STATUS_OK
;
2670 result
= cm_connect_netlogon(domain
, &netlogon_pipe
);
2671 if (!NT_STATUS_IS_OK(result
)) {
2675 if (domain
->conn
.netlogon_creds
== NULL
) {
2676 return NT_STATUS_TRUSTED_DOMAIN_FAILURE
;
2679 if (!(domain
->conn
.netlogon_flags
& NETLOGON_NEG_AUTHENTICATED_RPC
)) {
2680 return NT_STATUS_TRUSTED_DOMAIN_FAILURE
;
2683 *ppdc
= domain
->conn
.netlogon_creds
;
2684 return NT_STATUS_OK
;
2687 NTSTATUS
cm_connect_sam(struct winbindd_domain
*domain
, TALLOC_CTX
*mem_ctx
,
2689 struct rpc_pipe_client
**cli
, struct policy_handle
*sam_handle
)
2691 struct winbindd_cm_conn
*conn
;
2692 NTSTATUS status
, result
;
2693 struct netlogon_creds_cli_context
*p_creds
;
2694 struct cli_credentials
*creds
= NULL
;
2695 bool retry
= false; /* allow one retry attempt for expired session */
2697 if (sid_check_is_our_sam(&domain
->sid
)) {
2698 if (domain
->rodc
== false || need_rw_dc
== false) {
2699 return open_internal_samr_conn(mem_ctx
, domain
, cli
, sam_handle
);
2704 status
= init_dc_connection_rpc(domain
, need_rw_dc
);
2705 if (!NT_STATUS_IS_OK(status
)) {
2709 conn
= &domain
->conn
;
2711 if (rpccli_is_connected(conn
->samr_pipe
)) {
2715 TALLOC_FREE(conn
->samr_pipe
);
2718 * No SAMR pipe yet. Attempt to get an NTLMSSP SPNEGO authenticated
2719 * sign and sealed pipe using the machine account password by
2720 * preference. If we can't - try schannel, if that fails, try
2724 result
= get_trust_credentials(domain
, talloc_tos(), false, &creds
);
2725 if (!NT_STATUS_IS_OK(result
)) {
2726 DEBUG(10, ("cm_connect_sam: No user available for "
2727 "domain %s, trying schannel\n", domain
->name
));
2731 if (cli_credentials_is_anonymous(creds
)) {
2736 * We have an authenticated connection. Use a SPNEGO
2737 * authenticated SAMR pipe with sign & seal.
2739 status
= cli_rpc_pipe_open_with_creds(conn
->cli
,
2742 DCERPC_AUTH_TYPE_SPNEGO
,
2744 smbXcli_conn_remote_name(conn
->cli
->conn
),
2748 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
)
2750 invalidate_cm_connection(domain
);
2755 if (!NT_STATUS_IS_OK(status
)) {
2756 DEBUG(10,("cm_connect_sam: failed to connect to SAMR "
2757 "pipe for domain %s using NTLMSSP "
2758 "authenticated pipe: user %s. Error was "
2759 "%s\n", domain
->name
,
2760 cli_credentials_get_unparsed_name(creds
, talloc_tos()),
2761 nt_errstr(status
)));
2765 DEBUG(10,("cm_connect_sam: connected to SAMR pipe for "
2766 "domain %s using NTLMSSP authenticated "
2767 "pipe: user %s\n", domain
->name
,
2768 cli_credentials_get_unparsed_name(creds
, talloc_tos())));
2770 status
= dcerpc_samr_Connect2(conn
->samr_pipe
->binding_handle
, mem_ctx
,
2771 conn
->samr_pipe
->desthost
,
2772 SEC_FLAG_MAXIMUM_ALLOWED
,
2773 &conn
->sam_connect_handle
,
2776 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_DEVICE_ERROR
) && !retry
) {
2777 invalidate_cm_connection(domain
);
2778 TALLOC_FREE(conn
->samr_pipe
);
2783 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
2786 if (NT_STATUS_IS_OK(status
)) {
2790 DEBUG(10,("cm_connect_sam: ntlmssp-sealed dcerpc_samr_Connect2 "
2791 "failed for domain %s, error was %s. Trying schannel\n",
2792 domain
->name
, nt_errstr(status
) ));
2793 TALLOC_FREE(conn
->samr_pipe
);
2797 /* Fall back to schannel if it's a W2K pre-SP1 box. */
2799 status
= cm_get_schannel_creds(domain
, &p_creds
);
2800 if (!NT_STATUS_IS_OK(status
)) {
2801 /* If this call fails - conn->cli can now be NULL ! */
2802 DEBUG(10, ("cm_connect_sam: Could not get schannel auth info "
2803 "for domain %s (error %s), trying anon\n",
2805 nt_errstr(status
) ));
2809 result
= get_trust_credentials(domain
, talloc_tos(), true, &creds
);
2810 if (!NT_STATUS_IS_OK(result
)) {
2811 DEBUG(10, ("cm_connect_sam: No user available for "
2812 "domain %s (error %s), trying anon\n", domain
->name
,
2813 nt_errstr(result
)));
2816 status
= cli_rpc_pipe_open_schannel_with_creds
2817 (conn
->cli
, &ndr_table_samr
, NCACN_NP
,
2818 creds
, p_creds
, &conn
->samr_pipe
);
2820 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
)
2822 invalidate_cm_connection(domain
);
2827 if (!NT_STATUS_IS_OK(status
)) {
2828 DEBUG(10,("cm_connect_sam: failed to connect to SAMR pipe for "
2829 "domain %s using schannel. Error was %s\n",
2830 domain
->name
, nt_errstr(status
) ));
2833 DEBUG(10,("cm_connect_sam: connected to SAMR pipe for domain %s using "
2834 "schannel.\n", domain
->name
));
2836 status
= dcerpc_samr_Connect2(conn
->samr_pipe
->binding_handle
, mem_ctx
,
2837 conn
->samr_pipe
->desthost
,
2838 SEC_FLAG_MAXIMUM_ALLOWED
,
2839 &conn
->sam_connect_handle
,
2842 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_DEVICE_ERROR
) && !retry
) {
2843 invalidate_cm_connection(domain
);
2844 TALLOC_FREE(conn
->samr_pipe
);
2849 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
2852 if (NT_STATUS_IS_OK(status
)) {
2855 DEBUG(10,("cm_connect_sam: schannel-sealed dcerpc_samr_Connect2 failed "
2856 "for domain %s, error was %s. Trying anonymous\n",
2857 domain
->name
, nt_errstr(status
) ));
2858 TALLOC_FREE(conn
->samr_pipe
);
2862 /* Finally fall back to anonymous. */
2863 if (lp_winbind_sealed_pipes() || lp_require_strong_key()) {
2864 status
= NT_STATUS_DOWNGRADE_DETECTED
;
2865 DEBUG(1, ("Unwilling to make SAMR connection to domain %s "
2866 "without connection level security, "
2867 "must set 'winbind sealed pipes = false' and "
2868 "'require strong key = false' to proceed: %s\n",
2869 domain
->name
, nt_errstr(status
)));
2872 status
= cli_rpc_pipe_open_noauth(conn
->cli
, &ndr_table_samr
,
2875 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
)
2877 invalidate_cm_connection(domain
);
2882 if (!NT_STATUS_IS_OK(status
)) {
2886 status
= dcerpc_samr_Connect2(conn
->samr_pipe
->binding_handle
, mem_ctx
,
2887 conn
->samr_pipe
->desthost
,
2888 SEC_FLAG_MAXIMUM_ALLOWED
,
2889 &conn
->sam_connect_handle
,
2892 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_DEVICE_ERROR
) && !retry
) {
2893 invalidate_cm_connection(domain
);
2894 TALLOC_FREE(conn
->samr_pipe
);
2899 if (!NT_STATUS_IS_OK(status
)) {
2900 DEBUG(10,("cm_connect_sam: rpccli_samr_Connect2 failed "
2901 "for domain %s Error was %s\n",
2902 domain
->name
, nt_errstr(status
) ));
2905 if (!NT_STATUS_IS_OK(result
)) {
2907 DEBUG(10,("cm_connect_sam: dcerpc_samr_Connect2 failed "
2908 "for domain %s Error was %s\n",
2909 domain
->name
, nt_errstr(result
)));
2914 status
= dcerpc_samr_OpenDomain(conn
->samr_pipe
->binding_handle
,
2916 &conn
->sam_connect_handle
,
2917 SEC_FLAG_MAXIMUM_ALLOWED
,
2919 &conn
->sam_domain_handle
,
2921 if (!NT_STATUS_IS_OK(status
)) {
2928 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
2930 * if we got access denied, we might just have no access rights
2931 * to talk to the remote samr server server (e.g. when we are a
2932 * PDC and we are connecting a w2k8 pdc via an interdomain
2933 * trust). In that case do not invalidate the whole connection
2936 TALLOC_FREE(conn
->samr_pipe
);
2937 ZERO_STRUCT(conn
->sam_domain_handle
);
2939 } else if (!NT_STATUS_IS_OK(status
)) {
2940 invalidate_cm_connection(domain
);
2944 *cli
= conn
->samr_pipe
;
2945 *sam_handle
= conn
->sam_domain_handle
;
2949 /**********************************************************************
2950 open an schanneld ncacn_ip_tcp connection to LSA
2951 ***********************************************************************/
2953 static NTSTATUS
cm_connect_lsa_tcp(struct winbindd_domain
*domain
,
2954 TALLOC_CTX
*mem_ctx
,
2955 struct rpc_pipe_client
**cli
)
2957 struct winbindd_cm_conn
*conn
;
2958 struct netlogon_creds_cli_context
*p_creds
= NULL
;
2959 struct cli_credentials
*creds
= NULL
;
2962 DEBUG(10,("cm_connect_lsa_tcp\n"));
2964 status
= init_dc_connection_rpc(domain
, false);
2965 if (!NT_STATUS_IS_OK(status
)) {
2969 conn
= &domain
->conn
;
2971 if (conn
->lsa_pipe_tcp
&&
2972 conn
->lsa_pipe_tcp
->transport
->transport
== NCACN_IP_TCP
&&
2973 conn
->lsa_pipe_tcp
->auth
->auth_level
>= DCERPC_AUTH_LEVEL_INTEGRITY
&&
2974 rpccli_is_connected(conn
->lsa_pipe_tcp
)) {
2978 TALLOC_FREE(conn
->lsa_pipe_tcp
);
2980 status
= cm_get_schannel_creds(domain
, &p_creds
);
2981 if (!NT_STATUS_IS_OK(status
)) {
2985 status
= get_trust_credentials(domain
, talloc_tos(), true, &creds
);
2986 if (!NT_STATUS_IS_OK(status
)) {
2990 status
= cli_rpc_pipe_open_schannel_with_creds(conn
->cli
,
2995 &conn
->lsa_pipe_tcp
);
2996 if (!NT_STATUS_IS_OK(status
)) {
2997 DEBUG(10,("cli_rpc_pipe_open_schannel_with_key failed: %s\n",
2998 nt_errstr(status
)));
3003 if (!NT_STATUS_IS_OK(status
)) {
3004 TALLOC_FREE(conn
->lsa_pipe_tcp
);
3008 *cli
= conn
->lsa_pipe_tcp
;
3013 NTSTATUS
cm_connect_lsa(struct winbindd_domain
*domain
, TALLOC_CTX
*mem_ctx
,
3014 struct rpc_pipe_client
**cli
, struct policy_handle
*lsa_policy
)
3016 struct winbindd_cm_conn
*conn
;
3017 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
3018 struct netlogon_creds_cli_context
*p_creds
;
3019 struct cli_credentials
*creds
= NULL
;
3020 bool retry
= false; /* allow one retry attempt for expired session */
3023 result
= init_dc_connection_rpc(domain
, false);
3024 if (!NT_STATUS_IS_OK(result
))
3027 conn
= &domain
->conn
;
3029 if (rpccli_is_connected(conn
->lsa_pipe
)) {
3033 TALLOC_FREE(conn
->lsa_pipe
);
3035 result
= get_trust_credentials(domain
, talloc_tos(), false, &creds
);
3036 if (!NT_STATUS_IS_OK(result
)) {
3037 DEBUG(10, ("cm_connect_lsa: No user available for "
3038 "domain %s, trying schannel\n", domain
->name
));
3042 if (cli_credentials_is_anonymous(creds
)) {
3047 * We have an authenticated connection. Use a SPNEGO
3048 * authenticated LSA pipe with sign & seal.
3050 result
= cli_rpc_pipe_open_with_creds
3051 (conn
->cli
, &ndr_table_lsarpc
, NCACN_NP
,
3052 DCERPC_AUTH_TYPE_SPNEGO
,
3054 smbXcli_conn_remote_name(conn
->cli
->conn
),
3058 if (NT_STATUS_EQUAL(result
, NT_STATUS_NETWORK_SESSION_EXPIRED
)
3060 invalidate_cm_connection(domain
);
3065 if (!NT_STATUS_IS_OK(result
)) {
3066 DEBUG(10,("cm_connect_lsa: failed to connect to LSA pipe for "
3067 "domain %s using NTLMSSP authenticated pipe: user "
3068 "%s. Error was %s. Trying schannel.\n",
3070 cli_credentials_get_unparsed_name(creds
, talloc_tos()),
3071 nt_errstr(result
)));
3075 DEBUG(10,("cm_connect_lsa: connected to LSA pipe for domain %s using "
3076 "NTLMSSP authenticated pipe: user %s\n",
3077 domain
->name
, cli_credentials_get_unparsed_name(creds
, talloc_tos())));
3079 result
= rpccli_lsa_open_policy(conn
->lsa_pipe
, mem_ctx
, True
,
3080 SEC_FLAG_MAXIMUM_ALLOWED
,
3082 if (NT_STATUS_EQUAL(result
, NT_STATUS_IO_DEVICE_ERROR
) && !retry
) {
3083 invalidate_cm_connection(domain
);
3084 TALLOC_FREE(conn
->lsa_pipe
);
3089 if (NT_STATUS_IS_OK(result
)) {
3093 DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
3096 TALLOC_FREE(conn
->lsa_pipe
);
3100 /* Fall back to schannel if it's a W2K pre-SP1 box. */
3102 result
= cm_get_schannel_creds(domain
, &p_creds
);
3103 if (!NT_STATUS_IS_OK(result
)) {
3104 /* If this call fails - conn->cli can now be NULL ! */
3105 DEBUG(10, ("cm_connect_lsa: Could not get schannel auth info "
3106 "for domain %s (error %s), trying anon\n",
3108 nt_errstr(result
) ));
3113 result
= get_trust_credentials(domain
, talloc_tos(), true, &creds
);
3114 if (!NT_STATUS_IS_OK(result
)) {
3115 DEBUG(10, ("cm_connect_lsa: No user available for "
3116 "domain %s (error %s), trying anon\n", domain
->name
,
3117 nt_errstr(result
)));
3120 result
= cli_rpc_pipe_open_schannel_with_creds
3121 (conn
->cli
, &ndr_table_lsarpc
, NCACN_NP
,
3122 creds
, p_creds
, &conn
->lsa_pipe
);
3124 if (NT_STATUS_EQUAL(result
, NT_STATUS_NETWORK_SESSION_EXPIRED
)
3126 invalidate_cm_connection(domain
);
3131 if (!NT_STATUS_IS_OK(result
)) {
3132 DEBUG(10,("cm_connect_lsa: failed to connect to LSA pipe for "
3133 "domain %s using schannel. Error was %s\n",
3134 domain
->name
, nt_errstr(result
) ));
3137 DEBUG(10,("cm_connect_lsa: connected to LSA pipe for domain %s using "
3138 "schannel.\n", domain
->name
));
3140 result
= rpccli_lsa_open_policy(conn
->lsa_pipe
, mem_ctx
, True
,
3141 SEC_FLAG_MAXIMUM_ALLOWED
,
3144 if (NT_STATUS_EQUAL(result
, NT_STATUS_IO_DEVICE_ERROR
) && !retry
) {
3145 invalidate_cm_connection(domain
);
3146 TALLOC_FREE(conn
->lsa_pipe
);
3151 if (NT_STATUS_IS_OK(result
)) {
3155 DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
3158 TALLOC_FREE(conn
->lsa_pipe
);
3162 if (lp_winbind_sealed_pipes() || lp_require_strong_key()) {
3163 result
= NT_STATUS_DOWNGRADE_DETECTED
;
3164 DEBUG(1, ("Unwilling to make LSA connection to domain %s "
3165 "without connection level security, "
3166 "must set 'winbind sealed pipes = false' and "
3167 "'require strong key = false' to proceed: %s\n",
3168 domain
->name
, nt_errstr(result
)));
3172 result
= cli_rpc_pipe_open_noauth(conn
->cli
,
3176 if (NT_STATUS_EQUAL(result
, NT_STATUS_NETWORK_SESSION_EXPIRED
)
3178 invalidate_cm_connection(domain
);
3183 if (!NT_STATUS_IS_OK(result
)) {
3187 result
= rpccli_lsa_open_policy(conn
->lsa_pipe
, mem_ctx
, True
,
3188 SEC_FLAG_MAXIMUM_ALLOWED
,
3191 if (NT_STATUS_EQUAL(result
, NT_STATUS_IO_DEVICE_ERROR
) && !retry
) {
3192 invalidate_cm_connection(domain
);
3193 TALLOC_FREE(conn
->lsa_pipe
);
3199 if (!NT_STATUS_IS_OK(result
)) {
3200 invalidate_cm_connection(domain
);
3204 *cli
= conn
->lsa_pipe
;
3205 *lsa_policy
= conn
->lsa_policy
;
3209 /****************************************************************************
3210 Open a LSA connection to a DC, suiteable for LSA lookup calls.
3211 ****************************************************************************/
3213 NTSTATUS
cm_connect_lsat(struct winbindd_domain
*domain
,
3214 TALLOC_CTX
*mem_ctx
,
3215 struct rpc_pipe_client
**cli
,
3216 struct policy_handle
*lsa_policy
)
3220 if (domain
->can_do_ncacn_ip_tcp
) {
3221 status
= cm_connect_lsa_tcp(domain
, mem_ctx
, cli
);
3222 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
) ||
3223 NT_STATUS_EQUAL(status
, NT_STATUS_RPC_SEC_PKG_ERROR
) ||
3224 NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_ACCESS_DENIED
)) {
3225 invalidate_cm_connection(domain
);
3226 status
= cm_connect_lsa_tcp(domain
, mem_ctx
, cli
);
3228 if (NT_STATUS_IS_OK(status
)) {
3233 * we tried twice to connect via ncan_ip_tcp and schannel and
3234 * failed - maybe it is a trusted domain we can't connect to ?
3235 * do not try tcp next time - gd
3237 * This also prevents NETLOGON over TCP
3239 domain
->can_do_ncacn_ip_tcp
= false;
3242 status
= cm_connect_lsa(domain
, mem_ctx
, cli
, lsa_policy
);
3247 /****************************************************************************
3248 Open the netlogon pipe to this DC. Use schannel if specified in client conf.
3249 session key stored in conn->netlogon_pipe->dc->sess_key.
3250 ****************************************************************************/
3252 static NTSTATUS
cm_connect_netlogon_transport(struct winbindd_domain
*domain
,
3253 enum dcerpc_transport_t transport
,
3254 struct rpc_pipe_client
**cli
)
3256 struct messaging_context
*msg_ctx
= winbind_messaging_context();
3257 struct winbindd_cm_conn
*conn
;
3259 enum netr_SchannelType sec_chan_type
;
3260 struct netlogon_creds_CredentialState
*netlogon_creds
= NULL
;
3261 struct cli_credentials
*creds
= NULL
;
3265 result
= init_dc_connection_rpc(domain
, domain
->rodc
);
3266 if (!NT_STATUS_IS_OK(result
)) {
3270 conn
= &domain
->conn
;
3272 if (rpccli_is_connected(conn
->netlogon_pipe
)) {
3273 *cli
= conn
->netlogon_pipe
;
3274 return NT_STATUS_OK
;
3277 TALLOC_FREE(conn
->netlogon_pipe
);
3278 conn
->netlogon_flags
= 0;
3279 TALLOC_FREE(conn
->netlogon_creds
);
3281 result
= get_trust_credentials(domain
, talloc_tos(), true, &creds
);
3282 if (!NT_STATUS_IS_OK(result
)) {
3283 DEBUG(10, ("cm_connect_sam: No user available for "
3284 "domain %s when trying schannel\n", domain
->name
));
3285 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3288 if (cli_credentials_is_anonymous(creds
)) {
3289 DEBUG(1, ("get_trust_credential only gave anonymous for %s, unable to make get NETLOGON credentials\n",
3291 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3294 sec_chan_type
= cli_credentials_get_secure_channel_type(creds
);
3295 if (sec_chan_type
== SEC_CHAN_NULL
) {
3296 DBG_WARNING("get_secure_channel_type gave SEC_CHAN_NULL for %s\n",
3298 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3301 result
= rpccli_create_netlogon_creds_with_creds(creds
,
3305 &conn
->netlogon_creds
);
3306 if (!NT_STATUS_IS_OK(result
)) {
3307 DEBUG(1, ("rpccli_create_netlogon_creds failed for %s, "
3308 "unable to create NETLOGON credentials: %s\n",
3309 domain
->name
, nt_errstr(result
)));
3313 result
= rpccli_setup_netlogon_creds_with_creds(conn
->cli
, transport
,
3314 conn
->netlogon_creds
,
3315 conn
->netlogon_force_reauth
,
3317 conn
->netlogon_force_reauth
= false;
3318 if (!NT_STATUS_IS_OK(result
)) {
3319 DEBUG(1, ("rpccli_setup_netlogon_creds failed for %s, "
3320 "unable to setup NETLOGON credentials: %s\n",
3321 domain
->name
, nt_errstr(result
)));
3325 result
= netlogon_creds_cli_get(conn
->netlogon_creds
,
3328 if (!NT_STATUS_IS_OK(result
)) {
3329 DEBUG(1, ("netlogon_creds_cli_get failed for %s, "
3330 "unable to get NETLOGON credentials: %s\n",
3331 domain
->name
, nt_errstr(result
)));
3334 conn
->netlogon_flags
= netlogon_creds
->negotiate_flags
;
3335 TALLOC_FREE(netlogon_creds
);
3338 * FIXME: Document in which case we are not able to contact
3339 * a DC without schannel. Which information do we try to get
3342 if (!(conn
->netlogon_flags
& NETLOGON_NEG_AUTHENTICATED_RPC
)) {
3343 if (lp_winbind_sealed_pipes() || lp_require_strong_key()) {
3344 result
= NT_STATUS_DOWNGRADE_DETECTED
;
3345 DEBUG(1, ("Unwilling to make connection to domain %s"
3346 "without connection level security, "
3347 "must set 'winbind sealed pipes = false' and "
3348 "'require strong key = false' to proceed: %s\n",
3349 domain
->name
, nt_errstr(result
)));
3350 invalidate_cm_connection(domain
);
3353 result
= cli_rpc_pipe_open_noauth_transport(conn
->cli
,
3355 &ndr_table_netlogon
,
3356 &conn
->netlogon_pipe
);
3357 if (!NT_STATUS_IS_OK(result
)) {
3358 invalidate_cm_connection(domain
);
3362 *cli
= conn
->netlogon_pipe
;
3363 return NT_STATUS_OK
;
3366 /* Using the credentials from the first pipe, open a signed and sealed
3367 second netlogon pipe. The session key is stored in the schannel
3368 part of the new pipe auth struct.
3371 result
= cli_rpc_pipe_open_schannel_with_creds(
3372 conn
->cli
, &ndr_table_netlogon
, transport
,
3374 conn
->netlogon_creds
,
3375 &conn
->netlogon_pipe
);
3376 if (!NT_STATUS_IS_OK(result
)) {
3377 DEBUG(3, ("Could not open schannel'ed NETLOGON pipe. Error "
3378 "was %s\n", nt_errstr(result
)));
3380 invalidate_cm_connection(domain
);
3384 *cli
= conn
->netlogon_pipe
;
3385 return NT_STATUS_OK
;
3388 /****************************************************************************
3389 Open a LSA connection to a DC, suiteable for LSA lookup calls.
3390 ****************************************************************************/
3392 NTSTATUS
cm_connect_netlogon(struct winbindd_domain
*domain
,
3393 struct rpc_pipe_client
**cli
)
3397 status
= init_dc_connection_rpc(domain
, domain
->rodc
);
3398 if (!NT_STATUS_IS_OK(status
)) {
3402 if (domain
->active_directory
&& domain
->can_do_ncacn_ip_tcp
) {
3403 status
= cm_connect_netlogon_transport(domain
, NCACN_IP_TCP
, cli
);
3404 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
) ||
3405 NT_STATUS_EQUAL(status
, NT_STATUS_RPC_SEC_PKG_ERROR
) ||
3406 NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_ACCESS_DENIED
)) {
3407 invalidate_cm_connection(domain
);
3408 status
= cm_connect_netlogon_transport(domain
, NCACN_IP_TCP
, cli
);
3410 if (NT_STATUS_IS_OK(status
)) {
3415 * we tried twice to connect via ncan_ip_tcp and schannel and
3416 * failed - maybe it is a trusted domain we can't connect to ?
3417 * do not try tcp next time - gd
3419 * This also prevents LSA over TCP
3421 domain
->can_do_ncacn_ip_tcp
= false;
3424 status
= cm_connect_netlogon_transport(domain
, NCACN_NP
, cli
);
3425 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
)) {
3427 * SMB2 session expired, needs reauthentication. Drop
3428 * connection and retry.
3430 invalidate_cm_connection(domain
);
3431 status
= cm_connect_netlogon_transport(domain
, NCACN_NP
, cli
);
3437 void winbind_msg_ip_dropped(struct messaging_context
*msg_ctx
,
3440 struct server_id server_id
,
3443 struct winbindd_domain
*domain
;
3444 char *freeit
= NULL
;
3448 || (data
->data
== NULL
)
3449 || (data
->length
== 0)
3450 || (data
->data
[data
->length
-1] != '\0')) {
3451 DEBUG(1, ("invalid msg_ip_dropped message: not a valid "
3456 addr
= (char *)data
->data
;
3457 DEBUG(10, ("IP %s dropped\n", addr
));
3459 if (!is_ipaddress(addr
)) {
3462 * Some code sends us ip addresses with the /netmask
3465 slash
= strchr(addr
, '/');
3466 if (slash
== NULL
) {
3467 DEBUG(1, ("invalid msg_ip_dropped message: %s",
3471 freeit
= talloc_strndup(talloc_tos(), addr
, slash
-addr
);
3472 if (freeit
== NULL
) {
3473 DEBUG(1, ("talloc failed\n"));
3477 DEBUG(10, ("Stripped /netmask to IP %s\n", addr
));
3480 for (domain
= domain_list(); domain
!= NULL
; domain
= domain
->next
) {
3481 char sockaddr
[INET6_ADDRSTRLEN
];
3483 if (!cli_state_is_connected(domain
->conn
.cli
)) {
3487 print_sockaddr(sockaddr
, sizeof(sockaddr
),
3488 smbXcli_conn_local_sockaddr(domain
->conn
.cli
->conn
));
3490 if (strequal(sockaddr
, addr
)) {
3491 smbXcli_conn_disconnect(domain
->conn
.cli
->conn
, NT_STATUS_OK
);
3494 TALLOC_FREE(freeit
);