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 "lib/param/loadparm.h"
82 #include "libcli/auth/netlogon_creds_cli.h"
84 #include "rpc_server/rpc_ncacn_np.h"
87 #define DBGC_CLASS DBGC_WINBIND
91 struct sockaddr_storage ss;
94 extern struct winbindd_methods reconnect_methods;
95 extern bool override_logfile;
97 static NTSTATUS init_dc_connection_network(struct winbindd_domain *domain, bool need_rw_dc);
98 static void set_dc_type_and_flags( struct winbindd_domain *domain );
99 static bool set_dc_type_and_flags_trustinfo( struct winbindd_domain *domain );
100 static bool get_dcs(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
101 struct dc_name_ip **dcs, int *num_dcs);
103 /****************************************************************
104 Child failed to find DC's. Reschedule check.
105 ****************************************************************/
107 static void msg_failed_to_go_online(struct messaging_context *msg,
110 struct server_id server_id,
113 struct winbindd_domain *domain;
114 const char *domainname = (const char *)data->data;
116 if (data->data == NULL || data->length == 0) {
120 DEBUG(5,("msg_fail_to_go_online: received for domain %s.\n", domainname));
122 for (domain = domain_list(); domain; domain = domain->next) {
123 if (domain->internal) {
127 if (strequal(domain->name, domainname)) {
128 if (domain->online) {
129 /* We're already online, ignore. */
130 DEBUG(5,("msg_fail_to_go_online: domain %s "
131 "already online.\n", domainname));
135 /* Reschedule the online check. */
136 set_domain_offline(domain);
142 /****************************************************************
143 Actually cause a reconnect from a message.
144 ****************************************************************/
146 static void msg_try_to_go_online(struct messaging_context *msg,
149 struct server_id server_id,
152 struct winbindd_domain *domain;
153 const char *domainname = (const char *)data->data;
155 if (data->data == NULL || data->length == 0) {
159 DEBUG(5,("msg_try_to_go_online: received for domain %s.\n", domainname));
161 for (domain = domain_list(); domain; domain = domain->next) {
162 if (domain->internal) {
166 if (strequal(domain->name, domainname)) {
168 if (domain->online) {
169 /* We're already online, ignore. */
170 DEBUG(5,("msg_try_to_go_online: domain %s "
171 "already online.\n", domainname));
175 /* This call takes care of setting the online
176 flag to true if we connected, or re-adding
177 the offline handler if false. Bypasses online
178 check so always does network calls. */
180 init_dc_connection_network(domain, true);
186 /****************************************************************
187 Fork a child to try and contact a DC. Do this as contacting a
188 DC requires blocking lookups and we don't want to block our
190 ****************************************************************/
192 static bool fork_child_dc_connect(struct winbindd_domain *domain)
194 struct dc_name_ip *dcs = NULL;
196 TALLOC_CTX *mem_ctx = NULL;
197 pid_t parent_pid = getpid();
201 if (domain->dc_probe_pid != (pid_t)-1) {
203 * We might already have a DC probe
204 * child working, check.
206 if (process_exists_by_pid(domain->dc_probe_pid)) {
207 DEBUG(10,("fork_child_dc_connect: pid %u already "
208 "checking for DC's.\n",
209 (unsigned int)domain->dc_probe_pid));
212 domain->dc_probe_pid = (pid_t)-1;
215 domain->dc_probe_pid = fork();
217 if (domain->dc_probe_pid == (pid_t)-1) {
218 DEBUG(0, ("fork_child_dc_connect: Could not fork: %s\n", strerror(errno)));
222 if (domain->dc_probe_pid != (pid_t)0) {
224 messaging_register(winbind_messaging_context(), NULL,
225 MSG_WINBIND_TRY_TO_GO_ONLINE,
226 msg_try_to_go_online);
227 messaging_register(winbind_messaging_context(), NULL,
228 MSG_WINBIND_FAILED_TO_GO_ONLINE,
229 msg_failed_to_go_online);
235 /* Leave messages blocked - we will never process one. */
237 if (!override_logfile) {
238 if (asprintf(&lfile, "%s/log.winbindd-dc-connect", get_dyn_LOGFILEBASE()) == -1) {
239 DEBUG(0, ("fork_child_dc_connect: out of memory.\n"));
244 status = winbindd_reinit_after_fork(NULL, lfile);
245 if (!NT_STATUS_IS_OK(status)) {
246 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
248 messaging_send_buf(winbind_messaging_context(),
249 pid_to_procid(parent_pid),
250 MSG_WINBIND_FAILED_TO_GO_ONLINE,
251 (const uint8_t *)domain->name,
252 strlen(domain->name)+1);
257 mem_ctx = talloc_init("fork_child_dc_connect");
259 DEBUG(0,("talloc_init failed.\n"));
260 messaging_send_buf(winbind_messaging_context(),
261 pid_to_procid(parent_pid),
262 MSG_WINBIND_FAILED_TO_GO_ONLINE,
263 (const uint8_t *)domain->name,
264 strlen(domain->name)+1);
268 if ((!get_dcs(mem_ctx, domain, &dcs, &num_dcs)) || (num_dcs == 0)) {
269 /* Still offline ? Can't find DC's. */
270 messaging_send_buf(winbind_messaging_context(),
271 pid_to_procid(parent_pid),
272 MSG_WINBIND_FAILED_TO_GO_ONLINE,
273 (const uint8_t *)domain->name,
274 strlen(domain->name)+1);
278 /* We got a DC. Send a message to our parent to get it to
279 try and do the same. */
281 messaging_send_buf(winbind_messaging_context(),
282 pid_to_procid(parent_pid),
283 MSG_WINBIND_TRY_TO_GO_ONLINE,
284 (const uint8_t *)domain->name,
285 strlen(domain->name)+1);
289 /****************************************************************
290 Handler triggered if we're offline to try and detect a DC.
291 ****************************************************************/
293 static void check_domain_online_handler(struct tevent_context *ctx,
294 struct tevent_timer *te,
298 struct winbindd_domain *domain =
299 (struct winbindd_domain *)private_data;
301 DEBUG(10,("check_domain_online_handler: called for domain "
302 "%s (online = %s)\n", domain->name,
303 domain->online ? "True" : "False" ));
305 TALLOC_FREE(domain->check_online_event);
307 /* Are we still in "startup" mode ? */
309 if (domain->startup && (time_mono(NULL) > domain->startup_time + 30)) {
310 /* No longer in "startup" mode. */
311 DEBUG(10,("check_domain_online_handler: domain %s no longer in 'startup' mode.\n",
313 domain->startup = False;
316 /* We've been told to stay offline, so stay
319 if (get_global_winbindd_state_offline()) {
320 DEBUG(10,("check_domain_online_handler: domain %s remaining globally offline\n",
325 /* Fork a child to test if it can contact a DC.
326 If it can then send ourselves a message to
327 cause a reconnect. */
329 fork_child_dc_connect(domain);
332 /****************************************************************
333 If we're still offline setup the timeout check.
334 ****************************************************************/
336 static void calc_new_online_timeout_check(struct winbindd_domain *domain)
338 int wbr = lp_winbind_reconnect_delay();
340 if (domain->startup) {
341 domain->check_online_timeout = 10;
342 } else if (domain->check_online_timeout < wbr) {
343 domain->check_online_timeout = wbr;
347 void winbind_msg_domain_offline(struct messaging_context *msg_ctx,
350 struct server_id server_id,
353 const char *domain_name = (const char *)data->data;
354 struct winbindd_domain *domain;
356 domain = find_domain_from_name_noinit(domain_name);
357 if (domain == NULL) {
361 domain->online = false;
363 DEBUG(10, ("Domain %s is marked as offline now.\n",
367 void winbind_msg_domain_online(struct messaging_context *msg_ctx,
370 struct server_id server_id,
373 const char *domain_name = (const char *)data->data;
374 struct winbindd_domain *domain;
376 domain = find_domain_from_name_noinit(domain_name);
377 if (domain == NULL) {
381 domain->online = true;
383 DEBUG(10, ("Domain %s is marked as online now.\n",
387 /****************************************************************
388 Set domain offline and also add handler to put us back online
390 ****************************************************************/
392 void set_domain_offline(struct winbindd_domain *domain)
394 pid_t parent_pid = getppid();
396 DEBUG(10,("set_domain_offline: called for domain %s\n",
399 TALLOC_FREE(domain->check_online_event);
401 if (domain->internal) {
402 DEBUG(3,("set_domain_offline: domain %s is internal - logic error.\n",
407 domain->online = False;
409 /* Offline domains are always initialized. They're
410 re-initialized when they go back online. */
412 domain->initialized = True;
414 /* We only add the timeout handler that checks and
415 allows us to go back online when we've not
416 been told to remain offline. */
418 if (get_global_winbindd_state_offline()) {
419 DEBUG(10,("set_domain_offline: domain %s remaining globally offline\n",
424 /* If we're in startup mode, check again in 10 seconds, not in
425 lp_winbind_reconnect_delay() seconds (which is 30 seconds by default). */
427 calc_new_online_timeout_check(domain);
429 domain->check_online_event = tevent_add_timer(winbind_event_context(),
431 timeval_current_ofs(domain->check_online_timeout,0),
432 check_domain_online_handler,
435 /* The above *has* to succeed for winbindd to work. */
436 if (!domain->check_online_event) {
437 smb_panic("set_domain_offline: failed to add online handler");
440 DEBUG(10,("set_domain_offline: added event handler for domain %s\n",
443 /* Send a message to the parent that the domain is offline. */
444 if (parent_pid > 1 && !domain->internal) {
445 messaging_send_buf(winbind_messaging_context(),
446 pid_to_procid(parent_pid),
447 MSG_WINBIND_DOMAIN_OFFLINE,
448 (uint8 *)domain->name,
449 strlen(domain->name) + 1);
452 /* Send an offline message to the idmap child when our
453 primary domain goes offline */
455 if ( domain->primary ) {
456 struct winbindd_child *idmap = idmap_child();
458 if ( idmap->pid != 0 ) {
459 messaging_send_buf(winbind_messaging_context(),
460 pid_to_procid(idmap->pid),
462 (const uint8_t *)domain->name,
463 strlen(domain->name)+1);
470 /****************************************************************
471 Set domain online - if allowed.
472 ****************************************************************/
474 static void set_domain_online(struct winbindd_domain *domain)
476 pid_t parent_pid = getppid();
478 DEBUG(10,("set_domain_online: called for domain %s\n",
481 if (domain->internal) {
482 DEBUG(3,("set_domain_online: domain %s is internal - logic error.\n",
487 if (get_global_winbindd_state_offline()) {
488 DEBUG(10,("set_domain_online: domain %s remaining globally offline\n",
493 winbindd_set_locator_kdc_envs(domain);
495 /* If we are waiting to get a krb5 ticket, trigger immediately. */
496 ccache_regain_all_now();
498 /* Ok, we're out of any startup mode now... */
499 domain->startup = False;
501 if (domain->online == False) {
502 /* We were offline - now we're online. We default to
503 using the MS-RPC backend if we started offline,
504 and if we're going online for the first time we
505 should really re-initialize the backends and the
506 checks to see if we're talking to an AD or NT domain.
509 domain->initialized = False;
511 /* 'reconnect_methods' is the MS-RPC backend. */
512 if (domain->backend == &reconnect_methods) {
513 domain->backend = NULL;
517 /* Ensure we have no online timeout checks. */
518 domain->check_online_timeout = 0;
519 TALLOC_FREE(domain->check_online_event);
521 /* Ensure we ignore any pending child messages. */
522 messaging_deregister(winbind_messaging_context(),
523 MSG_WINBIND_TRY_TO_GO_ONLINE, NULL);
524 messaging_deregister(winbind_messaging_context(),
525 MSG_WINBIND_FAILED_TO_GO_ONLINE, NULL);
527 domain->online = True;
529 /* Send a message to the parent that the domain is online. */
530 if (parent_pid > 1 && !domain->internal) {
531 messaging_send_buf(winbind_messaging_context(),
532 pid_to_procid(parent_pid),
533 MSG_WINBIND_DOMAIN_ONLINE,
534 (uint8 *)domain->name,
535 strlen(domain->name) + 1);
538 /* Send an online message to the idmap child when our
539 primary domain comes online */
541 if ( domain->primary ) {
542 struct winbindd_child *idmap = idmap_child();
544 if ( idmap->pid != 0 ) {
545 messaging_send_buf(winbind_messaging_context(),
546 pid_to_procid(idmap->pid),
548 (const uint8_t *)domain->name,
549 strlen(domain->name)+1);
556 /****************************************************************
557 Requested to set a domain online.
558 ****************************************************************/
560 void set_domain_online_request(struct winbindd_domain *domain)
564 DEBUG(10,("set_domain_online_request: called for domain %s\n",
567 if (get_global_winbindd_state_offline()) {
568 DEBUG(10,("set_domain_online_request: domain %s remaining globally offline\n",
573 if (domain->internal) {
574 DEBUG(10, ("set_domain_online_request: Internal domains are "
579 /* We've been told it's safe to go online and
580 try and connect to a DC. But I don't believe it
581 because network manager seems to lie.
582 Wait at least 5 seconds. Heuristics suck... */
587 /* Go into "startup" mode again. */
588 domain->startup_time = time_mono(NULL);
589 domain->startup = True;
593 if (!domain->check_online_event) {
594 /* If we've come from being globally offline we
595 don't have a check online event handler set.
596 We need to add one now we're trying to go
599 DEBUG(10,("set_domain_online_request: domain %s was globally offline.\n",
603 TALLOC_FREE(domain->check_online_event);
605 domain->check_online_event = tevent_add_timer(winbind_event_context(),
608 check_domain_online_handler,
611 /* The above *has* to succeed for winbindd to work. */
612 if (!domain->check_online_event) {
613 smb_panic("set_domain_online_request: failed to add online handler");
617 /****************************************************************
618 Add -ve connection cache entries for domain and realm.
619 ****************************************************************/
621 static void winbind_add_failed_connection_entry(
622 const struct winbindd_domain *domain,
626 add_failed_connection_entry(domain->name, server, result);
627 /* If this was the saf name for the last thing we talked to,
629 saf_delete(domain->name);
630 if (domain->alt_name != NULL) {
631 add_failed_connection_entry(domain->alt_name, server, result);
632 saf_delete(domain->alt_name);
634 winbindd_unset_locator_kdc_env(domain);
637 /* Choose between anonymous or authenticated connections. We need to use
638 an authenticated connection if DCs have the RestrictAnonymous registry
639 entry set > 0, or the "Additional restrictions for anonymous
640 connections" set in the win2k Local Security Policy.
642 Caller to free() result in domain, username, password
645 static void cm_get_ipc_userpass(char **username, char **domain, char **password)
647 *username = (char *)secrets_fetch(SECRETS_AUTH_USER, NULL);
648 *domain = (char *)secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
649 *password = (char *)secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
651 if (*username && **username) {
653 if (!*domain || !**domain)
654 *domain = smb_xstrdup(lp_workgroup());
656 if (!*password || !**password)
657 *password = smb_xstrdup("");
659 DEBUG(3, ("cm_get_ipc_userpass: Retrieved auth-user from secrets.tdb [%s\\%s]\n",
660 *domain, *username));
663 DEBUG(3, ("cm_get_ipc_userpass: No auth-user defined\n"));
664 *username = smb_xstrdup("");
665 *domain = smb_xstrdup("");
666 *password = smb_xstrdup("");
670 static bool get_dc_name_via_netlogon(struct winbindd_domain *domain,
672 struct sockaddr_storage *dc_ss)
674 struct winbindd_domain *our_domain = NULL;
675 struct rpc_pipe_client *netlogon_pipe = NULL;
679 unsigned int orig_timeout;
680 const char *tmp = NULL;
682 struct dcerpc_binding_handle *b;
684 /* Hmmmm. We can only open one connection to the NETLOGON pipe at the
691 if (domain->primary) {
695 our_domain = find_our_domain();
697 if ((mem_ctx = talloc_init("get_dc_name_via_netlogon")) == NULL) {
701 result = cm_connect_netlogon(our_domain, &netlogon_pipe);
702 if (!NT_STATUS_IS_OK(result)) {
703 talloc_destroy(mem_ctx);
707 b = netlogon_pipe->binding_handle;
709 /* This call can take a long time - allow the server to time out.
710 35 seconds should do it. */
712 orig_timeout = rpccli_set_timeout(netlogon_pipe, 35000);
714 if (our_domain->active_directory) {
715 struct netr_DsRGetDCNameInfo *domain_info = NULL;
717 result = dcerpc_netr_DsRGetDCName(b,
726 if (NT_STATUS_IS_OK(result) && W_ERROR_IS_OK(werr)) {
728 mem_ctx, domain_info->dc_unc);
730 DEBUG(0, ("talloc_strdup failed\n"));
731 talloc_destroy(mem_ctx);
734 if (domain->alt_name == NULL) {
735 domain->alt_name = talloc_strdup(domain,
736 domain_info->domain_name);
737 if (domain->alt_name == NULL) {
738 DEBUG(0, ("talloc_strdup failed\n"));
739 talloc_destroy(mem_ctx);
743 if (domain->forest_name == NULL) {
744 domain->forest_name = talloc_strdup(domain,
745 domain_info->forest_name);
746 if (domain->forest_name == NULL) {
747 DEBUG(0, ("talloc_strdup failed\n"));
748 talloc_destroy(mem_ctx);
754 result = dcerpc_netr_GetAnyDCName(b, mem_ctx,
761 /* And restore our original timeout. */
762 rpccli_set_timeout(netlogon_pipe, orig_timeout);
764 if (!NT_STATUS_IS_OK(result)) {
765 DEBUG(10,("dcerpc_netr_GetAnyDCName failed: %s\n",
767 talloc_destroy(mem_ctx);
771 if (!W_ERROR_IS_OK(werr)) {
772 DEBUG(10,("dcerpc_netr_GetAnyDCName failed: %s\n",
774 talloc_destroy(mem_ctx);
778 /* dcerpc_netr_GetAnyDCName gives us a name with \\ */
779 p = strip_hostname(tmp);
783 talloc_destroy(mem_ctx);
785 DEBUG(10,("dcerpc_netr_GetAnyDCName returned %s\n", dcname));
787 if (!resolve_name(dcname, dc_ss, 0x20, true)) {
795 * Helper function to assemble trust password and account name
797 static NTSTATUS get_trust_creds(const struct winbindd_domain *domain,
798 char **machine_password,
799 char **machine_account,
800 char **machine_krb5_principal)
802 const char *account_name;
803 const char *name = NULL;
805 /* If we are a DC and this is not our own domain */
810 struct winbindd_domain *our_domain = find_our_domain();
813 return NT_STATUS_INVALID_SERVER_STATE;
815 name = our_domain->name;
818 if (!get_trust_pw_clear(name, machine_password,
819 &account_name, NULL))
821 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
824 if ((machine_account != NULL) &&
825 (asprintf(machine_account, "%s$", account_name) == -1))
827 return NT_STATUS_NO_MEMORY;
830 /* For now assume our machine account only exists in our domain */
832 if (machine_krb5_principal != NULL)
834 struct winbindd_domain *our_domain = find_our_domain();
837 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
840 if (our_domain->alt_name == NULL) {
841 return NT_STATUS_INVALID_PARAMETER;
844 if (asprintf(machine_krb5_principal, "%s$@%s",
845 account_name, our_domain->alt_name) == -1)
847 return NT_STATUS_NO_MEMORY;
850 if (!strupper_m(*machine_krb5_principal)) {
851 SAFE_FREE(*machine_krb5_principal);
852 return NT_STATUS_INVALID_PARAMETER;
859 /************************************************************************
860 Given a fd with a just-connected TCP connection to a DC, open a connection
862 ************************************************************************/
864 static NTSTATUS cm_prepare_connection(const struct winbindd_domain *domain,
866 const char *controller,
867 struct cli_state **cli,
870 bool try_spnego = false;
871 bool try_ipc_auth = false;
872 char *machine_password = NULL;
873 char *machine_krb5_principal = NULL;
874 char *machine_account = NULL;
875 char *ipc_username = NULL;
876 char *ipc_domain = NULL;
877 char *ipc_password = NULL;
879 uint16_t sec_mode = 0;
881 struct named_mutex *mutex;
883 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
885 enum smb_signing_setting smb_sign_client_connections = lp_client_signing();
887 if (smb_sign_client_connections == SMB_SIGNING_DEFAULT) {
889 * If we are connecting to our own AD domain, require
890 * smb signing to disrupt MITM attacks
892 if (domain->primary && lp_security() == SEC_ADS) {
893 smb_sign_client_connections = SMB_SIGNING_REQUIRED;
895 * If we are in or are an AD domain and connecting to another
896 * AD domain in our forest
897 * then require smb signing to disrupt MITM attacks
899 } else if ((lp_security() == SEC_ADS ||
900 lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC)
901 && domain->active_directory
902 && (domain->domain_trust_attribs
903 & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST)) {
904 smb_sign_client_connections = SMB_SIGNING_REQUIRED;
908 DEBUG(10,("cm_prepare_connection: connecting to DC %s for domain %s\n",
909 controller, domain->name ));
913 mutex = grab_named_mutex(talloc_tos(), controller,
914 WINBIND_SERVER_MUTEX_WAIT_TIME);
917 DEBUG(0,("cm_prepare_connection: mutex grab failed for %s\n",
919 result = NT_STATUS_POSSIBLE_DEADLOCK;
923 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
925 *cli = cli_state_create(NULL, sockfd,
926 controller, domain->alt_name,
927 smb_sign_client_connections, flags);
930 DEBUG(1, ("Could not cli_initialize\n"));
931 result = NT_STATUS_NO_MEMORY;
935 cli_set_timeout(*cli, 10000); /* 10 seconds */
937 result = smbXcli_negprot((*cli)->conn, (*cli)->timeout,
938 lp_client_min_protocol(),
939 lp_client_max_protocol());
941 if (!NT_STATUS_IS_OK(result)) {
942 DEBUG(1, ("cli_negprot failed: %s\n", nt_errstr(result)));
946 if (smbXcli_conn_protocol((*cli)->conn) >= PROTOCOL_NT1 &&
947 smb1cli_conn_capabilities((*cli)->conn) & CAP_EXTENDED_SECURITY) {
949 } else if (smbXcli_conn_protocol((*cli)->conn) >= PROTOCOL_SMB2_02) {
953 if (!is_dc_trusted_domain_situation(domain->name) && try_spnego) {
954 result = get_trust_creds(domain, &machine_password,
956 &machine_krb5_principal);
957 if (!NT_STATUS_IS_OK(result)) {
961 if (lp_security() == SEC_ADS) {
963 /* Try a krb5 session */
965 (*cli)->use_kerberos = True;
966 DEBUG(5, ("connecting to %s from %s with kerberos principal "
967 "[%s] and realm [%s]\n", controller, lp_netbios_name(),
968 machine_krb5_principal, domain->alt_name));
970 winbindd_set_locator_kdc_envs(domain);
972 result = cli_session_setup(*cli,
973 machine_krb5_principal,
975 strlen(machine_password)+1,
977 strlen(machine_password)+1,
980 if (!NT_STATUS_IS_OK(result)) {
981 DEBUG(4,("failed kerberos session setup with %s\n",
985 if (NT_STATUS_IS_OK(result)) {
986 /* Ensure creds are stored for NTLMSSP authenticated pipe access. */
987 result = cli_init_creds(*cli, machine_account, lp_workgroup(), machine_password);
988 if (!NT_STATUS_IS_OK(result)) {
991 goto session_setup_done;
995 /* Fall back to non-kerberos session setup using NTLMSSP SPNEGO with the machine account. */
996 (*cli)->use_kerberos = False;
998 DEBUG(5, ("connecting to %s from %s with username "
999 "[%s]\\[%s]\n", controller, lp_netbios_name(),
1000 lp_workgroup(), machine_account));
1002 result = cli_session_setup(*cli,
1005 strlen(machine_password)+1,
1007 strlen(machine_password)+1,
1009 if (!NT_STATUS_IS_OK(result)) {
1010 DEBUG(4, ("authenticated session setup failed with %s\n",
1011 nt_errstr(result)));
1014 if (NT_STATUS_IS_OK(result)) {
1015 /* Ensure creds are stored for NTLMSSP authenticated pipe access. */
1016 result = cli_init_creds(*cli, machine_account, lp_workgroup(), machine_password);
1017 if (!NT_STATUS_IS_OK(result)) {
1020 goto session_setup_done;
1024 /* Fall back to non-kerberos session setup with auth_user */
1026 (*cli)->use_kerberos = False;
1028 cm_get_ipc_userpass(&ipc_username, &ipc_domain, &ipc_password);
1030 sec_mode = smb1cli_conn_server_security_mode((*cli)->conn);
1032 try_ipc_auth = false;
1034 try_ipc_auth = true;
1035 } else if (sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
1036 try_ipc_auth = true;
1039 if (try_ipc_auth && (strlen(ipc_username) > 0)) {
1041 /* Only try authenticated if we have a username */
1043 DEBUG(5, ("connecting to %s from %s with username "
1044 "[%s]\\[%s]\n", controller, lp_netbios_name(),
1045 ipc_domain, ipc_username));
1047 if (NT_STATUS_IS_OK(cli_session_setup(
1049 ipc_password, strlen(ipc_password)+1,
1050 ipc_password, strlen(ipc_password)+1,
1052 /* Successful logon with given username. */
1053 result = cli_init_creds(*cli, ipc_username, ipc_domain, ipc_password);
1054 if (!NT_STATUS_IS_OK(result)) {
1057 goto session_setup_done;
1059 DEBUG(4, ("authenticated session setup with user %s\\%s failed.\n",
1060 ipc_domain, ipc_username ));
1066 /* Fall back to anonymous connection, this might fail later */
1067 DEBUG(10,("cm_prepare_connection: falling back to anonymous "
1068 "connection for DC %s\n",
1071 result = cli_session_setup(*cli, "", NULL, 0, NULL, 0, "");
1072 if (NT_STATUS_IS_OK(result)) {
1073 DEBUG(5, ("Connected anonymously\n"));
1074 result = cli_init_creds(*cli, "", "", "");
1075 if (!NT_STATUS_IS_OK(result)) {
1078 goto session_setup_done;
1081 /* We can't session setup */
1087 * This should be a short term hack until
1088 * dynamic re-authentication is implemented.
1090 * See Bug 9175 - winbindd doesn't recover from
1091 * NT_STATUS_NETWORK_SESSION_EXPIRED
1093 if (smbXcli_conn_protocol((*cli)->conn) >= PROTOCOL_SMB2_02) {
1094 smbXcli_session_set_disconnect_expired((*cli)->smb2.session);
1097 /* cache the server name for later connections */
1099 saf_store(domain->name, controller);
1100 if (domain->alt_name && (*cli)->use_kerberos) {
1101 saf_store(domain->alt_name, controller);
1104 winbindd_set_locator_kdc_envs(domain);
1106 result = cli_tree_connect(*cli, "IPC$", "IPC", "", 0);
1108 if (!NT_STATUS_IS_OK(result)) {
1109 DEBUG(1,("failed tcon_X with %s\n", nt_errstr(result)));
1116 /* set the domain if empty; needed for schannel connections */
1117 if ( !(*cli)->domain[0] ) {
1118 result = cli_set_domain((*cli), domain->name);
1119 if (!NT_STATUS_IS_OK(result)) {
1120 SAFE_FREE(ipc_username);
1121 SAFE_FREE(ipc_domain);
1122 SAFE_FREE(ipc_password);
1127 result = NT_STATUS_OK;
1131 SAFE_FREE(machine_account);
1132 SAFE_FREE(machine_password);
1133 SAFE_FREE(machine_krb5_principal);
1134 SAFE_FREE(ipc_username);
1135 SAFE_FREE(ipc_domain);
1136 SAFE_FREE(ipc_password);
1138 if (!NT_STATUS_IS_OK(result)) {
1139 winbind_add_failed_connection_entry(domain, controller, result);
1140 if ((*cli) != NULL) {
1149 /*******************************************************************
1150 Add a dcname and sockaddr_storage pair to the end of a dc_name_ip
1153 Keeps the list unique by not adding duplicate entries.
1155 @param[in] mem_ctx talloc memory context to allocate from
1156 @param[in] domain_name domain of the DC
1157 @param[in] dcname name of the DC to add to the list
1158 @param[in] pss Internet address and port pair to add to the list
1159 @param[in,out] dcs array of dc_name_ip structures to add to
1160 @param[in,out] num_dcs number of dcs returned in the dcs array
1161 @return true if the list was added to, false otherwise
1162 *******************************************************************/
1164 static bool add_one_dc_unique(TALLOC_CTX *mem_ctx, const char *domain_name,
1165 const char *dcname, struct sockaddr_storage *pss,
1166 struct dc_name_ip **dcs, int *num)
1170 if (!NT_STATUS_IS_OK(check_negative_conn_cache(domain_name, dcname))) {
1171 DEBUG(10, ("DC %s was in the negative conn cache\n", dcname));
1175 /* Make sure there's no duplicates in the list */
1176 for (i=0; i<*num; i++)
1178 (struct sockaddr *)(void *)&(*dcs)[i].ss,
1179 (struct sockaddr *)(void *)pss))
1182 *dcs = talloc_realloc(mem_ctx, *dcs, struct dc_name_ip, (*num)+1);
1187 fstrcpy((*dcs)[*num].name, dcname);
1188 (*dcs)[*num].ss = *pss;
1193 static bool add_sockaddr_to_array(TALLOC_CTX *mem_ctx,
1194 struct sockaddr_storage *pss, uint16 port,
1195 struct sockaddr_storage **addrs, int *num)
1197 *addrs = talloc_realloc(mem_ctx, *addrs, struct sockaddr_storage, (*num)+1);
1199 if (*addrs == NULL) {
1204 (*addrs)[*num] = *pss;
1205 set_sockaddr_port((struct sockaddr *)&(*addrs)[*num], port);
1211 /*******************************************************************
1212 convert an ip to a name
1213 *******************************************************************/
1215 static bool dcip_to_name(TALLOC_CTX *mem_ctx,
1216 const struct winbindd_domain *domain,
1217 struct sockaddr_storage *pss,
1220 struct ip_service ip_list;
1221 uint32_t nt_version = NETLOGON_NT_VERSION_1;
1223 const char *dc_name;
1230 /* For active directory servers, try to get the ldap server name.
1231 None of these failures should be considered critical for now */
1233 if ((lp_security() == SEC_ADS) && (domain->alt_name != NULL)) {
1235 ADS_STATUS ads_status;
1236 char addr[INET6_ADDRSTRLEN];
1238 print_sockaddr(addr, sizeof(addr), pss);
1240 ads = ads_init(domain->alt_name, domain->name, addr);
1241 ads->auth.flags |= ADS_AUTH_NO_BIND;
1243 ads_status = ads_connect(ads);
1244 if (ADS_ERR_OK(ads_status)) {
1245 /* We got a cldap packet. */
1246 *name = talloc_strdup(mem_ctx,
1247 ads->config.ldap_server_name);
1248 if (*name == NULL) {
1251 namecache_store(*name, 0x20, 1, &ip_list);
1253 DEBUG(10,("dcip_to_name: flags = 0x%x\n", (unsigned int)ads->config.flags));
1255 if (domain->primary && (ads->config.flags & NBT_SERVER_KDC)) {
1256 if (ads_closest_dc(ads)) {
1257 char *sitename = sitename_fetch(mem_ctx, ads->config.realm);
1259 /* We're going to use this KDC for this realm/domain.
1260 If we are using sites, then force the krb5 libs
1263 create_local_private_krb5_conf_for_domain(domain->alt_name,
1268 TALLOC_FREE(sitename);
1270 /* use an off site KDC */
1271 create_local_private_krb5_conf_for_domain(domain->alt_name,
1276 winbindd_set_locator_kdc_envs(domain);
1278 /* Ensure we contact this DC also. */
1279 saf_store(domain->name, *name);
1280 saf_store(domain->alt_name, *name);
1283 ads_destroy( &ads );
1287 ads_destroy( &ads );
1292 status = nbt_getdc(winbind_messaging_context(), 10, pss, domain->name,
1293 &domain->sid, nt_version, mem_ctx, &nt_version,
1295 if (NT_STATUS_IS_OK(status)) {
1296 *name = talloc_strdup(mem_ctx, dc_name);
1297 if (*name == NULL) {
1300 namecache_store(*name, 0x20, 1, &ip_list);
1304 /* try node status request */
1306 if (name_status_find(domain->name, 0x1c, 0x20, pss, nbtname) ) {
1307 namecache_store(nbtname, 0x20, 1, &ip_list);
1310 *name = talloc_strdup(mem_ctx, nbtname);
1311 if (*name == NULL) {
1321 /*******************************************************************
1322 Retrieve a list of IP addresses for domain controllers.
1324 The array is sorted in the preferred connection order.
1326 @param[in] mem_ctx talloc memory context to allocate from
1327 @param[in] domain domain to retrieve DCs for
1328 @param[out] dcs array of dcs that will be returned
1329 @param[out] num_dcs number of dcs returned in the dcs array
1331 *******************************************************************/
1333 static bool get_dcs(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
1334 struct dc_name_ip **dcs, int *num_dcs)
1337 struct sockaddr_storage ss;
1338 struct ip_service *ip_list = NULL;
1339 int iplist_size = 0;
1342 enum security_types sec = (enum security_types)lp_security();
1344 is_our_domain = strequal(domain->name, lp_workgroup());
1346 /* If not our domain, get the preferred DC, by asking our primary DC */
1348 && get_dc_name_via_netlogon(domain, dcname, &ss)
1349 && add_one_dc_unique(mem_ctx, domain->name, dcname, &ss, dcs,
1352 char addr[INET6_ADDRSTRLEN];
1353 print_sockaddr(addr, sizeof(addr), &ss);
1354 DEBUG(10, ("Retrieved DC %s at %s via netlogon\n",
1359 if ((sec == SEC_ADS) && (domain->alt_name != NULL)) {
1360 char *sitename = NULL;
1362 /* We need to make sure we know the local site before
1363 doing any DNS queries, as this will restrict the
1364 get_sorted_dc_list() call below to only fetching
1365 DNS records for the correct site. */
1367 /* Find any DC to get the site record.
1368 We deliberately don't care about the
1371 get_dc_name(domain->name, domain->alt_name, dcname, &ss);
1373 sitename = sitename_fetch(mem_ctx, domain->alt_name);
1376 /* Do the site-specific AD dns lookup first. */
1377 get_sorted_dc_list(domain->alt_name, sitename, &ip_list,
1378 &iplist_size, True);
1380 /* Add ips to the DC array. We don't look up the name
1381 of the DC in this function, but we fill in the char*
1382 of the ip now to make the failed connection cache
1384 for ( i=0; i<iplist_size; i++ ) {
1385 char addr[INET6_ADDRSTRLEN];
1386 print_sockaddr(addr, sizeof(addr),
1388 add_one_dc_unique(mem_ctx,
1397 TALLOC_FREE(sitename);
1401 /* Now we add DCs from the main AD DNS lookup. */
1402 get_sorted_dc_list(domain->alt_name, NULL, &ip_list,
1403 &iplist_size, True);
1405 for ( i=0; i<iplist_size; i++ ) {
1406 char addr[INET6_ADDRSTRLEN];
1407 print_sockaddr(addr, sizeof(addr),
1409 add_one_dc_unique(mem_ctx,
1421 /* Try standard netbios queries if no ADS and fall back to DNS queries
1422 * if alt_name is available */
1423 if (*num_dcs == 0) {
1424 get_sorted_dc_list(domain->name, NULL, &ip_list, &iplist_size,
1426 if (iplist_size == 0) {
1427 if (domain->alt_name != NULL) {
1428 get_sorted_dc_list(domain->alt_name, NULL, &ip_list,
1429 &iplist_size, true);
1433 for ( i=0; i<iplist_size; i++ ) {
1434 char addr[INET6_ADDRSTRLEN];
1435 print_sockaddr(addr, sizeof(addr),
1437 add_one_dc_unique(mem_ctx,
1452 /*******************************************************************
1453 Find and make a connection to a DC in the given domain.
1455 @param[in] mem_ctx talloc memory context to allocate from
1456 @param[in] domain domain to find a dc in
1457 @param[out] dcname NetBIOS or FQDN of DC that's connected to
1458 @param[out] pss DC Internet address and port
1459 @param[out] fd fd of the open socket connected to the newly found dc
1460 @return true when a DC connection is made, false otherwise
1461 *******************************************************************/
1463 static bool find_new_dc(TALLOC_CTX *mem_ctx,
1464 struct winbindd_domain *domain,
1465 char **dcname, struct sockaddr_storage *pss, int *fd)
1467 struct dc_name_ip *dcs = NULL;
1470 const char **dcnames = NULL;
1471 int num_dcnames = 0;
1473 struct sockaddr_storage *addrs = NULL;
1484 if (!get_dcs(mem_ctx, domain, &dcs, &num_dcs) || (num_dcs == 0))
1487 for (i=0; i<num_dcs; i++) {
1489 if (!add_string_to_array(mem_ctx, dcs[i].name,
1490 &dcnames, &num_dcnames)) {
1493 if (!add_sockaddr_to_array(mem_ctx, &dcs[i].ss, TCP_SMB_PORT,
1494 &addrs, &num_addrs)) {
1499 if ((num_dcnames == 0) || (num_dcnames != num_addrs))
1502 if ((addrs == NULL) || (dcnames == NULL))
1505 status = smbsock_any_connect(addrs, dcnames, NULL, NULL, NULL,
1506 num_addrs, 0, 10, fd, &fd_index, NULL);
1507 if (!NT_STATUS_IS_OK(status)) {
1508 for (i=0; i<num_dcs; i++) {
1509 char ab[INET6_ADDRSTRLEN];
1510 print_sockaddr(ab, sizeof(ab), &dcs[i].ss);
1511 DEBUG(10, ("find_new_dc: smbsock_any_connect failed for "
1512 "domain %s address %s. Error was %s\n",
1513 domain->name, ab, nt_errstr(status) ));
1514 winbind_add_failed_connection_entry(domain,
1515 dcs[i].name, NT_STATUS_UNSUCCESSFUL);
1520 *pss = addrs[fd_index];
1522 if (*dcnames[fd_index] != '\0' && !is_ipaddress(dcnames[fd_index])) {
1523 /* Ok, we've got a name for the DC */
1524 *dcname = talloc_strdup(mem_ctx, dcnames[fd_index]);
1525 if (*dcname == NULL) {
1531 /* Try to figure out the name */
1532 if (dcip_to_name(mem_ctx, domain, pss, dcname)) {
1536 /* We can not continue without the DC's name */
1537 winbind_add_failed_connection_entry(domain, dcs[fd_index].name,
1538 NT_STATUS_UNSUCCESSFUL);
1540 /* Throw away all arrays as we're doing this again. */
1544 TALLOC_FREE(dcnames);
1556 static char *current_dc_key(TALLOC_CTX *mem_ctx, const char *domain_name)
1558 return talloc_asprintf_strupper_m(mem_ctx, "CURRENT_DCNAME/%s",
1562 static void store_current_dc_in_gencache(const char *domain_name,
1563 const char *dc_name,
1564 struct cli_state *cli)
1566 char addr[INET6_ADDRSTRLEN];
1570 if (!cli_state_is_connected(cli)) {
1574 print_sockaddr(addr, sizeof(addr),
1575 smbXcli_conn_remote_sockaddr(cli->conn));
1577 key = current_dc_key(talloc_tos(), domain_name);
1582 value = talloc_asprintf(talloc_tos(), "%s %s", addr, dc_name);
1583 if (value == NULL) {
1587 gencache_set(key, value, 0x7fffffff);
1593 bool fetch_current_dc_from_gencache(TALLOC_CTX *mem_ctx,
1594 const char *domain_name,
1595 char **p_dc_name, char **p_dc_ip)
1600 char *dc_name = NULL;
1603 key = current_dc_key(talloc_tos(), domain_name);
1607 if (!gencache_get(key, mem_ctx, &value, NULL)) {
1610 p = strchr(value, ' ');
1614 dc_ip = talloc_strndup(mem_ctx, value, p - value);
1615 if (dc_ip == NULL) {
1618 dc_name = talloc_strdup(mem_ctx, p+1);
1619 if (dc_name == NULL) {
1623 if (p_dc_ip != NULL) {
1627 if (p_dc_name != NULL) {
1628 *p_dc_name = dc_name;
1633 TALLOC_FREE(dc_name);
1640 NTSTATUS wb_open_internal_pipe(TALLOC_CTX *mem_ctx,
1641 const struct ndr_interface_table *table,
1642 struct rpc_pipe_client **ret_pipe)
1644 struct rpc_pipe_client *cli = NULL;
1645 const struct auth_session_info *session_info;
1646 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1649 session_info = get_session_info_system();
1650 SMB_ASSERT(session_info != NULL);
1652 /* create a connection to the specified pipe */
1653 if (lp_parm_bool(-1, "winbindd", "use external pipes", false)) {
1654 status = rpc_pipe_open_interface(mem_ctx,
1658 winbind_messaging_context(),
1661 status = rpc_pipe_open_internal(mem_ctx,
1665 winbind_messaging_context(),
1668 if (!NT_STATUS_IS_OK(status)) {
1669 DEBUG(0, ("open_internal_pipe: Could not connect to %s pipe: %s\n",
1670 table->name, nt_errstr(status)));
1678 return NT_STATUS_OK;
1681 static NTSTATUS cm_open_connection(struct winbindd_domain *domain,
1682 struct winbindd_cm_conn *new_conn)
1684 TALLOC_CTX *mem_ctx;
1686 char *saf_servername;
1689 if ((mem_ctx = talloc_init("cm_open_connection")) == NULL) {
1690 set_domain_offline(domain);
1691 return NT_STATUS_NO_MEMORY;
1694 saf_servername = saf_fetch(mem_ctx, domain->name );
1696 /* we have to check the server affinity cache here since
1697 later we select a DC based on response time and not preference */
1699 /* Check the negative connection cache
1700 before talking to it. It going down may have
1701 triggered the reconnection. */
1703 if ( saf_servername && NT_STATUS_IS_OK(check_negative_conn_cache( domain->name, saf_servername))) {
1705 DEBUG(10,("cm_open_connection: saf_servername is '%s' for domain %s\n",
1706 saf_servername, domain->name ));
1708 /* convert an ip address to a name */
1709 if (is_ipaddress( saf_servername ) ) {
1710 char *dcname = NULL;
1711 struct sockaddr_storage ss;
1713 if (!interpret_string_addr(&ss, saf_servername,
1715 TALLOC_FREE(mem_ctx);
1716 return NT_STATUS_UNSUCCESSFUL;
1718 if (dcip_to_name(mem_ctx, domain, &ss, &dcname)) {
1719 domain->dcname = talloc_strdup(domain,
1721 if (domain->dcname == NULL) {
1722 TALLOC_FREE(mem_ctx);
1723 return NT_STATUS_NO_MEMORY;
1726 winbind_add_failed_connection_entry(
1727 domain, saf_servername,
1728 NT_STATUS_UNSUCCESSFUL);
1731 domain->dcname = talloc_strdup(domain, saf_servername);
1732 if (domain->dcname == NULL) {
1733 TALLOC_FREE(mem_ctx);
1734 return NT_STATUS_NO_MEMORY;
1739 for (retries = 0; retries < 3; retries++) {
1742 char *dcname = NULL;
1744 result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1746 DEBUG(10,("cm_open_connection: dcname is '%s' for domain %s\n",
1747 domain->dcname ? domain->dcname : "", domain->name ));
1749 if (domain->dcname != NULL
1750 && NT_STATUS_IS_OK(check_negative_conn_cache( domain->name, domain->dcname))
1751 && (resolve_name(domain->dcname, &domain->dcaddr, 0x20, true)))
1755 status = smbsock_connect(&domain->dcaddr, 0,
1758 if (!NT_STATUS_IS_OK(status)) {
1764 !find_new_dc(mem_ctx, domain, &dcname, &domain->dcaddr, &fd))
1766 /* This is the one place where we will
1767 set the global winbindd offline state
1768 to true, if a "WINBINDD_OFFLINE" entry
1769 is found in the winbindd cache. */
1770 set_global_winbindd_state_offline();
1773 if (dcname != NULL) {
1774 talloc_free(domain->dcname);
1776 domain->dcname = talloc_move(domain, &dcname);
1777 if (domain->dcname == NULL) {
1778 result = NT_STATUS_NO_MEMORY;
1783 new_conn->cli = NULL;
1785 result = cm_prepare_connection(domain, fd, domain->dcname,
1786 &new_conn->cli, &retry);
1787 if (!NT_STATUS_IS_OK(result)) {
1788 /* Don't leak the smb connection socket */
1796 if (NT_STATUS_IS_OK(result)) {
1797 bool seal_pipes = true;
1799 winbindd_set_locator_kdc_envs(domain);
1801 if (domain->online == False) {
1802 /* We're changing state from offline to online. */
1803 set_global_winbindd_state_online();
1805 set_domain_online(domain);
1808 * Much as I hate global state, this seems to be the point
1809 * where we can be certain that we have a proper connection to
1810 * a DC. wbinfo --dc-info needs that information, store it in
1811 * gencache with a looong timeout. This will need revisiting
1812 * once we start to connect to multiple DCs, wbcDcInfo is
1813 * already prepared for that.
1815 store_current_dc_in_gencache(domain->name, domain->dcname,
1818 seal_pipes = lp_winbind_sealed_pipes();
1819 seal_pipes = lp_parm_bool(-1, "winbind sealed pipes",
1824 new_conn->auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1826 new_conn->auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1829 /* Ensure we setup the retry handler. */
1830 set_domain_offline(domain);
1833 talloc_destroy(mem_ctx);
1837 /* Close down all open pipes on a connection. */
1839 void invalidate_cm_connection(struct winbindd_cm_conn *conn)
1843 /* We're closing down a possibly dead
1844 connection. Don't have impossibly long (10s) timeouts. */
1847 cli_set_timeout(conn->cli, 1000); /* 1 second. */
1850 if (conn->samr_pipe != NULL) {
1851 if (is_valid_policy_hnd(&conn->sam_connect_handle)) {
1852 dcerpc_samr_Close(conn->samr_pipe->binding_handle,
1854 &conn->sam_connect_handle,
1857 TALLOC_FREE(conn->samr_pipe);
1858 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1860 cli_set_timeout(conn->cli, 500);
1864 if (conn->lsa_pipe != NULL) {
1865 if (is_valid_policy_hnd(&conn->lsa_policy)) {
1866 dcerpc_lsa_Close(conn->lsa_pipe->binding_handle,
1871 TALLOC_FREE(conn->lsa_pipe);
1872 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1874 cli_set_timeout(conn->cli, 500);
1878 if (conn->lsa_pipe_tcp != NULL) {
1879 if (is_valid_policy_hnd(&conn->lsa_policy)) {
1880 dcerpc_lsa_Close(conn->lsa_pipe_tcp->binding_handle,
1885 TALLOC_FREE(conn->lsa_pipe_tcp);
1886 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1888 cli_set_timeout(conn->cli, 500);
1892 if (conn->netlogon_pipe != NULL) {
1893 TALLOC_FREE(conn->netlogon_pipe);
1894 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1896 cli_set_timeout(conn->cli, 500);
1900 conn->auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1901 conn->netlogon_force_reauth = false;
1902 conn->netlogon_flags = 0;
1903 TALLOC_FREE(conn->netlogon_creds);
1906 cli_shutdown(conn->cli);
1912 void close_conns_after_fork(void)
1914 struct winbindd_domain *domain;
1915 struct winbindd_cli_state *cli_state;
1917 for (domain = domain_list(); domain; domain = domain->next) {
1919 * first close the low level SMB TCP connection
1920 * so that we don't generate any SMBclose
1921 * requests in invalidate_cm_connection()
1923 if (cli_state_is_connected(domain->conn.cli)) {
1924 smbXcli_conn_disconnect(domain->conn.cli->conn, NT_STATUS_OK);
1927 invalidate_cm_connection(&domain->conn);
1930 for (cli_state = winbindd_client_list();
1932 cli_state = cli_state->next) {
1933 if (cli_state->sock >= 0) {
1934 close(cli_state->sock);
1935 cli_state->sock = -1;
1940 static bool connection_ok(struct winbindd_domain *domain)
1944 ok = cli_state_is_connected(domain->conn.cli);
1946 DEBUG(3, ("connection_ok: Connection to %s for domain %s is not connected\n",
1947 domain->dcname, domain->name));
1951 if (domain->online == False) {
1952 DEBUG(3, ("connection_ok: Domain %s is offline\n", domain->name));
1959 /* Initialize a new connection up to the RPC BIND.
1960 Bypass online status check so always does network calls. */
1962 static NTSTATUS init_dc_connection_network(struct winbindd_domain *domain, bool need_rw_dc)
1965 bool skip_connection = domain->internal;
1966 if (need_rw_dc && domain->rodc) {
1967 skip_connection = false;
1970 /* Internal connections never use the network. */
1971 if (dom_sid_equal(&domain->sid, &global_sid_Builtin)) {
1972 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1975 /* Still ask the internal LSA and SAMR server about the local domain */
1976 if (skip_connection || connection_ok(domain)) {
1977 if (!domain->initialized) {
1978 set_dc_type_and_flags(domain);
1980 return NT_STATUS_OK;
1983 invalidate_cm_connection(&domain->conn);
1985 if (!domain->primary && !domain->initialized) {
1987 * Before we connect to a trust, work out if it is an
1988 * AD domain by asking our own domain.
1990 set_dc_type_and_flags_trustinfo(domain);
1993 result = cm_open_connection(domain, &domain->conn);
1995 if (NT_STATUS_IS_OK(result) && !domain->initialized) {
1996 set_dc_type_and_flags(domain);
2002 NTSTATUS init_dc_connection(struct winbindd_domain *domain, bool need_rw_dc)
2004 if (dom_sid_equal(&domain->sid, &global_sid_Builtin)) {
2005 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2008 if (domain->initialized && !domain->online) {
2009 /* We check for online status elsewhere. */
2010 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
2013 return init_dc_connection_network(domain, need_rw_dc);
2016 static NTSTATUS init_dc_connection_rpc(struct winbindd_domain *domain, bool need_rw_dc)
2020 status = init_dc_connection(domain, need_rw_dc);
2021 if (!NT_STATUS_IS_OK(status)) {
2025 if (!domain->internal && domain->conn.cli == NULL) {
2026 /* happens for trusted domains without inbound trust */
2027 return NT_STATUS_TRUSTED_DOMAIN_FAILURE;
2030 return NT_STATUS_OK;
2033 /******************************************************************************
2034 Set the trust flags (direction and forest location) for a domain
2035 ******************************************************************************/
2037 static bool set_dc_type_and_flags_trustinfo( struct winbindd_domain *domain )
2039 struct winbindd_domain *our_domain;
2040 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2042 struct netr_DomainTrustList trusts;
2044 uint32 flags = (NETR_TRUST_FLAG_IN_FOREST |
2045 NETR_TRUST_FLAG_OUTBOUND |
2046 NETR_TRUST_FLAG_INBOUND);
2047 struct rpc_pipe_client *cli;
2048 TALLOC_CTX *mem_ctx = NULL;
2049 struct dcerpc_binding_handle *b;
2051 DEBUG(5, ("set_dc_type_and_flags_trustinfo: domain %s\n", domain->name ));
2053 /* Our primary domain doesn't need to worry about trust flags.
2054 Force it to go through the network setup */
2055 if ( domain->primary ) {
2059 mem_ctx = talloc_stackframe();
2060 our_domain = find_our_domain();
2061 if (our_domain->internal) {
2062 result = init_dc_connection(our_domain, false);
2063 if (!NT_STATUS_IS_OK(result)) {
2064 DEBUG(3,("set_dc_type_and_flags_trustinfo: "
2065 "Not able to make a connection to our domain: %s\n",
2066 nt_errstr(result)));
2067 TALLOC_FREE(mem_ctx);
2072 /* This won't work unless our domain is AD */
2073 if ( !our_domain->active_directory ) {
2074 TALLOC_FREE(mem_ctx);
2078 if (our_domain->internal) {
2079 result = wb_open_internal_pipe(mem_ctx, &ndr_table_netlogon, &cli);
2080 } else if (!connection_ok(our_domain)) {
2081 DEBUG(3,("set_dc_type_and_flags_trustinfo: "
2082 "No connection to our domain!\n"));
2083 TALLOC_FREE(mem_ctx);
2086 result = cm_connect_netlogon(our_domain, &cli);
2089 if (!NT_STATUS_IS_OK(result)) {
2090 DEBUG(5, ("set_dc_type_and_flags_trustinfo: Could not open "
2091 "a connection to %s for PIPE_NETLOGON (%s)\n",
2092 domain->name, nt_errstr(result)));
2093 TALLOC_FREE(mem_ctx);
2096 b = cli->binding_handle;
2098 /* Use DsEnumerateDomainTrusts to get us the trust direction and type. */
2099 result = dcerpc_netr_DsrEnumerateDomainTrusts(b, mem_ctx,
2104 if (!NT_STATUS_IS_OK(result)) {
2105 DEBUG(0,("set_dc_type_and_flags_trustinfo: "
2106 "failed to query trusted domain list: %s\n",
2107 nt_errstr(result)));
2108 TALLOC_FREE(mem_ctx);
2111 if (!W_ERROR_IS_OK(werr)) {
2112 DEBUG(0,("set_dc_type_and_flags_trustinfo: "
2113 "failed to query trusted domain list: %s\n",
2115 TALLOC_FREE(mem_ctx);
2119 /* Now find the domain name and get the flags */
2121 for ( i=0; i<trusts.count; i++ ) {
2122 if ( strequal( domain->name, trusts.array[i].netbios_name) ) {
2123 domain->domain_flags = trusts.array[i].trust_flags;
2124 domain->domain_type = trusts.array[i].trust_type;
2125 domain->domain_trust_attribs = trusts.array[i].trust_attributes;
2127 if ( domain->domain_type == LSA_TRUST_TYPE_UPLEVEL )
2128 domain->active_directory = True;
2130 /* This flag is only set if the domain is *our*
2131 primary domain and the primary domain is in
2134 domain->native_mode = (domain->domain_flags & NETR_TRUST_FLAG_NATIVE);
2136 DEBUG(5, ("set_dc_type_and_flags_trustinfo: domain %s is %sin "
2137 "native mode.\n", domain->name,
2138 domain->native_mode ? "" : "NOT "));
2140 DEBUG(5,("set_dc_type_and_flags_trustinfo: domain %s is %s"
2141 "running active directory.\n", domain->name,
2142 domain->active_directory ? "" : "NOT "));
2144 domain->can_do_ncacn_ip_tcp = domain->active_directory;
2146 domain->initialized = True;
2152 TALLOC_FREE(mem_ctx);
2154 return domain->initialized;
2157 /******************************************************************************
2158 We can 'sense' certain things about the DC by it's replies to certain
2161 This tells us if this particular remote server is Active Directory, and if it
2163 ******************************************************************************/
2165 static void set_dc_type_and_flags_connect( struct winbindd_domain *domain )
2167 NTSTATUS status, result;
2169 TALLOC_CTX *mem_ctx = NULL;
2170 struct rpc_pipe_client *cli = NULL;
2171 struct policy_handle pol;
2172 union dssetup_DsRoleInfo info;
2173 union lsa_PolicyInformation *lsa_info = NULL;
2175 if (!domain->internal && !connection_ok(domain)) {
2179 mem_ctx = talloc_init("set_dc_type_and_flags on domain %s\n",
2182 DEBUG(1, ("set_dc_type_and_flags_connect: talloc_init() failed\n"));
2186 DEBUG(5, ("set_dc_type_and_flags_connect: domain %s\n", domain->name ));
2188 if (domain->internal) {
2189 status = wb_open_internal_pipe(mem_ctx,
2193 status = cli_rpc_pipe_open_noauth(domain->conn.cli,
2198 if (!NT_STATUS_IS_OK(status)) {
2199 DEBUG(5, ("set_dc_type_and_flags_connect: Could not bind to "
2200 "PI_DSSETUP on domain %s: (%s)\n",
2201 domain->name, nt_errstr(status)));
2203 /* if this is just a non-AD domain we need to continue
2204 * identifying so that we can in the end return with
2205 * domain->initialized = True - gd */
2210 status = dcerpc_dssetup_DsRoleGetPrimaryDomainInformation(cli->binding_handle, mem_ctx,
2211 DS_ROLE_BASIC_INFORMATION,
2216 if (NT_STATUS_IS_OK(status)) {
2217 result = werror_to_ntstatus(werr);
2219 if (!NT_STATUS_IS_OK(status)) {
2220 DEBUG(5, ("set_dc_type_and_flags_connect: rpccli_ds_getprimarydominfo "
2221 "on domain %s failed: (%s)\n",
2222 domain->name, nt_errstr(status)));
2224 /* older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for
2225 * every opcode on the DSSETUP pipe, continue with
2226 * no_dssetup mode here as well to get domain->initialized
2229 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
2233 TALLOC_FREE(mem_ctx);
2237 if ((info.basic.flags & DS_ROLE_PRIMARY_DS_RUNNING) &&
2238 !(info.basic.flags & DS_ROLE_PRIMARY_DS_MIXED_MODE)) {
2239 domain->native_mode = True;
2241 domain->native_mode = False;
2245 if (domain->internal) {
2246 status = wb_open_internal_pipe(mem_ctx,
2250 status = cli_rpc_pipe_open_noauth(domain->conn.cli,
2251 &ndr_table_lsarpc, &cli);
2253 if (!NT_STATUS_IS_OK(status)) {
2254 DEBUG(5, ("set_dc_type_and_flags_connect: Could not bind to "
2255 "PI_LSARPC on domain %s: (%s)\n",
2256 domain->name, nt_errstr(status)));
2258 TALLOC_FREE(mem_ctx);
2262 status = rpccli_lsa_open_policy2(cli, mem_ctx, True,
2263 SEC_FLAG_MAXIMUM_ALLOWED, &pol);
2265 if (NT_STATUS_IS_OK(status)) {
2266 /* This particular query is exactly what Win2k clients use
2267 to determine that the DC is active directory */
2268 status = dcerpc_lsa_QueryInfoPolicy2(cli->binding_handle, mem_ctx,
2270 LSA_POLICY_INFO_DNS,
2275 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2276 domain->active_directory = True;
2278 if (lsa_info->dns.name.string) {
2279 talloc_free(domain->name);
2280 domain->name = talloc_strdup(domain,
2281 lsa_info->dns.name.string);
2282 if (domain->name == NULL) {
2287 if (lsa_info->dns.dns_domain.string) {
2288 talloc_free(domain->alt_name);
2290 talloc_strdup(domain,
2291 lsa_info->dns.dns_domain.string);
2292 if (domain->alt_name == NULL) {
2297 /* See if we can set some domain trust flags about
2300 if (lsa_info->dns.dns_forest.string) {
2301 talloc_free(domain->forest_name);
2302 domain->forest_name =
2303 talloc_strdup(domain,
2304 lsa_info->dns.dns_forest.string);
2305 if (domain->forest_name == NULL) {
2309 if (strequal(domain->forest_name, domain->alt_name)) {
2310 domain->domain_flags |= NETR_TRUST_FLAG_TREEROOT;
2314 if (lsa_info->dns.sid) {
2315 sid_copy(&domain->sid, lsa_info->dns.sid);
2318 domain->active_directory = False;
2320 status = rpccli_lsa_open_policy(cli, mem_ctx, True,
2321 SEC_FLAG_MAXIMUM_ALLOWED,
2324 if (!NT_STATUS_IS_OK(status)) {
2328 status = dcerpc_lsa_QueryInfoPolicy(cli->binding_handle, mem_ctx,
2330 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
2333 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2335 if (lsa_info->account_domain.name.string) {
2336 talloc_free(domain->name);
2338 talloc_strdup(domain,
2339 lsa_info->account_domain.name.string);
2342 if (lsa_info->account_domain.sid) {
2343 sid_copy(&domain->sid, lsa_info->account_domain.sid);
2349 DEBUG(5, ("set_dc_type_and_flags_connect: domain %s is %sin native mode.\n",
2350 domain->name, domain->native_mode ? "" : "NOT "));
2352 DEBUG(5,("set_dc_type_and_flags_connect: domain %s is %srunning active directory.\n",
2353 domain->name, domain->active_directory ? "" : "NOT "));
2355 domain->can_do_ncacn_ip_tcp = domain->active_directory;
2359 TALLOC_FREE(mem_ctx);
2361 domain->initialized = True;
2364 /**********************************************************************
2365 Set the domain_flags (trust attributes, domain operating modes, etc...
2366 ***********************************************************************/
2368 static void set_dc_type_and_flags( struct winbindd_domain *domain )
2370 /* we always have to contact our primary domain */
2372 if ( domain->primary || domain->internal) {
2373 DEBUG(10,("set_dc_type_and_flags: setting up flags for "
2374 "primary or internal domain\n"));
2375 set_dc_type_and_flags_connect( domain );
2379 /* Use our DC to get the information if possible */
2381 if ( !set_dc_type_and_flags_trustinfo( domain ) ) {
2382 /* Otherwise, fallback to contacting the
2384 set_dc_type_and_flags_connect( domain );
2392 /**********************************************************************
2393 ***********************************************************************/
2395 static NTSTATUS cm_get_schannel_creds(struct winbindd_domain *domain,
2396 struct netlogon_creds_cli_context **ppdc)
2398 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2399 struct rpc_pipe_client *netlogon_pipe;
2403 if ((!IS_DC) && (!domain->primary)) {
2404 return NT_STATUS_TRUSTED_DOMAIN_FAILURE;
2407 if (domain->conn.netlogon_creds != NULL) {
2408 if (!(domain->conn.netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
2409 return NT_STATUS_TRUSTED_DOMAIN_FAILURE;
2411 *ppdc = domain->conn.netlogon_creds;
2412 return NT_STATUS_OK;
2415 result = cm_connect_netlogon(domain, &netlogon_pipe);
2416 if (!NT_STATUS_IS_OK(result)) {
2420 if (domain->conn.netlogon_creds == NULL) {
2421 return NT_STATUS_TRUSTED_DOMAIN_FAILURE;
2424 if (!(domain->conn.netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
2425 return NT_STATUS_TRUSTED_DOMAIN_FAILURE;
2428 *ppdc = domain->conn.netlogon_creds;
2429 return NT_STATUS_OK;
2432 NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
2434 struct rpc_pipe_client **cli, struct policy_handle *sam_handle)
2436 struct winbindd_cm_conn *conn;
2437 NTSTATUS status, result;
2438 struct netlogon_creds_cli_context *p_creds;
2439 char *machine_password = NULL;
2440 char *machine_account = NULL;
2441 const char *domain_name = NULL;
2443 if (sid_check_is_our_sam(&domain->sid)) {
2444 if (domain->rodc == false || need_rw_dc == false) {
2445 return open_internal_samr_conn(mem_ctx, domain, cli, sam_handle);
2449 status = init_dc_connection_rpc(domain, need_rw_dc);
2450 if (!NT_STATUS_IS_OK(status)) {
2454 conn = &domain->conn;
2456 if (rpccli_is_connected(conn->samr_pipe)) {
2460 TALLOC_FREE(conn->samr_pipe);
2463 * No SAMR pipe yet. Attempt to get an NTLMSSP SPNEGO authenticated
2464 * sign and sealed pipe using the machine account password by
2465 * preference. If we can't - try schannel, if that fails, try
2469 if ((conn->cli->user_name[0] == '\0') ||
2470 (conn->cli->domain[0] == '\0') ||
2471 (conn->cli->password == NULL || conn->cli->password[0] == '\0'))
2473 status = get_trust_creds(domain, &machine_password,
2474 &machine_account, NULL);
2475 if (!NT_STATUS_IS_OK(status)) {
2476 DEBUG(10, ("cm_connect_sam: No no user available for "
2477 "domain %s, trying schannel\n", conn->cli->domain));
2480 domain_name = domain->name;
2482 machine_password = SMB_STRDUP(conn->cli->password);
2483 machine_account = SMB_STRDUP(conn->cli->user_name);
2484 domain_name = conn->cli->domain;
2487 if (!machine_password || !machine_account) {
2488 status = NT_STATUS_NO_MEMORY;
2492 /* We have an authenticated connection. Use a NTLMSSP SPNEGO
2493 authenticated SAMR pipe with sign & seal. */
2494 status = cli_rpc_pipe_open_spnego(conn->cli,
2499 smbXcli_conn_remote_name(conn->cli->conn),
2505 if (!NT_STATUS_IS_OK(status)) {
2506 DEBUG(10,("cm_connect_sam: failed to connect to SAMR "
2507 "pipe for domain %s using NTLMSSP "
2508 "authenticated pipe: user %s\\%s. Error was "
2509 "%s\n", domain->name, domain_name,
2510 machine_account, nt_errstr(status)));
2514 DEBUG(10,("cm_connect_sam: connected to SAMR pipe for "
2515 "domain %s using NTLMSSP authenticated "
2516 "pipe: user %s\\%s\n", domain->name,
2517 domain_name, machine_account));
2519 status = dcerpc_samr_Connect2(conn->samr_pipe->binding_handle, mem_ctx,
2520 conn->samr_pipe->desthost,
2521 SEC_FLAG_MAXIMUM_ALLOWED,
2522 &conn->sam_connect_handle,
2524 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2527 if (NT_STATUS_IS_OK(status)) {
2531 DEBUG(10,("cm_connect_sam: ntlmssp-sealed dcerpc_samr_Connect2 "
2532 "failed for domain %s, error was %s. Trying schannel\n",
2533 domain->name, nt_errstr(status) ));
2534 TALLOC_FREE(conn->samr_pipe);
2538 /* Fall back to schannel if it's a W2K pre-SP1 box. */
2540 status = cm_get_schannel_creds(domain, &p_creds);
2541 if (!NT_STATUS_IS_OK(status)) {
2542 /* If this call fails - conn->cli can now be NULL ! */
2543 DEBUG(10, ("cm_connect_sam: Could not get schannel auth info "
2544 "for domain %s (error %s), trying anon\n",
2546 nt_errstr(status) ));
2549 status = cli_rpc_pipe_open_schannel_with_key
2550 (conn->cli, &ndr_table_samr, NCACN_NP,
2551 domain->name, p_creds, &conn->samr_pipe);
2553 if (!NT_STATUS_IS_OK(status)) {
2554 DEBUG(10,("cm_connect_sam: failed to connect to SAMR pipe for "
2555 "domain %s using schannel. Error was %s\n",
2556 domain->name, nt_errstr(status) ));
2559 DEBUG(10,("cm_connect_sam: connected to SAMR pipe for domain %s using "
2560 "schannel.\n", domain->name ));
2562 status = dcerpc_samr_Connect2(conn->samr_pipe->binding_handle, mem_ctx,
2563 conn->samr_pipe->desthost,
2564 SEC_FLAG_MAXIMUM_ALLOWED,
2565 &conn->sam_connect_handle,
2567 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
2570 if (NT_STATUS_IS_OK(status)) {
2573 DEBUG(10,("cm_connect_sam: schannel-sealed dcerpc_samr_Connect2 failed "
2574 "for domain %s, error was %s. Trying anonymous\n",
2575 domain->name, nt_errstr(status) ));
2576 TALLOC_FREE(conn->samr_pipe);
2580 /* Finally fall back to anonymous. */
2581 if (lp_winbind_sealed_pipes() || lp_require_strong_key()) {
2582 status = NT_STATUS_DOWNGRADE_DETECTED;
2583 DEBUG(1, ("Unwilling to make SAMR connection to domain %s"
2584 "without connection level security, "
2585 "must set 'winbind sealed pipes = false' and "
2586 "'require strong key = false' to proceed: %s\n",
2587 domain->name, nt_errstr(status)));
2590 status = cli_rpc_pipe_open_noauth(conn->cli, &ndr_table_samr,
2593 if (!NT_STATUS_IS_OK(status)) {
2597 status = dcerpc_samr_Connect2(conn->samr_pipe->binding_handle, mem_ctx,
2598 conn->samr_pipe->desthost,
2599 SEC_FLAG_MAXIMUM_ALLOWED,
2600 &conn->sam_connect_handle,
2602 if (!NT_STATUS_IS_OK(status)) {
2603 DEBUG(10,("cm_connect_sam: rpccli_samr_Connect2 failed "
2604 "for domain %s Error was %s\n",
2605 domain->name, nt_errstr(status) ));
2608 if (!NT_STATUS_IS_OK(result)) {
2610 DEBUG(10,("cm_connect_sam: dcerpc_samr_Connect2 failed "
2611 "for domain %s Error was %s\n",
2612 domain->name, nt_errstr(result)));
2617 status = dcerpc_samr_OpenDomain(conn->samr_pipe->binding_handle,
2619 &conn->sam_connect_handle,
2620 SEC_FLAG_MAXIMUM_ALLOWED,
2622 &conn->sam_domain_handle,
2624 if (!NT_STATUS_IS_OK(status)) {
2631 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
2633 * if we got access denied, we might just have no access rights
2634 * to talk to the remote samr server server (e.g. when we are a
2635 * PDC and we are connecting a w2k8 pdc via an interdomain
2636 * trust). In that case do not invalidate the whole connection
2639 TALLOC_FREE(conn->samr_pipe);
2640 ZERO_STRUCT(conn->sam_domain_handle);
2642 } else if (!NT_STATUS_IS_OK(status)) {
2643 invalidate_cm_connection(conn);
2647 *cli = conn->samr_pipe;
2648 *sam_handle = conn->sam_domain_handle;
2649 SAFE_FREE(machine_password);
2650 SAFE_FREE(machine_account);
2654 /**********************************************************************
2655 open an schanneld ncacn_ip_tcp connection to LSA
2656 ***********************************************************************/
2658 NTSTATUS cm_connect_lsa_tcp(struct winbindd_domain *domain,
2659 TALLOC_CTX *mem_ctx,
2660 struct rpc_pipe_client **cli)
2662 struct winbindd_cm_conn *conn;
2663 struct netlogon_creds_cli_context *creds;
2666 DEBUG(10,("cm_connect_lsa_tcp\n"));
2668 status = init_dc_connection_rpc(domain, false);
2669 if (!NT_STATUS_IS_OK(status)) {
2673 conn = &domain->conn;
2675 if (conn->lsa_pipe_tcp &&
2676 conn->lsa_pipe_tcp->transport->transport == NCACN_IP_TCP &&
2677 conn->lsa_pipe_tcp->auth->auth_level >= DCERPC_AUTH_LEVEL_INTEGRITY &&
2678 rpccli_is_connected(conn->lsa_pipe_tcp)) {
2682 TALLOC_FREE(conn->lsa_pipe_tcp);
2684 status = cm_get_schannel_creds(domain, &creds);
2685 if (!NT_STATUS_IS_OK(status)) {
2689 status = cli_rpc_pipe_open_schannel_with_key(conn->cli,
2694 &conn->lsa_pipe_tcp);
2695 if (!NT_STATUS_IS_OK(status)) {
2696 DEBUG(10,("cli_rpc_pipe_open_schannel_with_key failed: %s\n",
2697 nt_errstr(status)));
2702 if (!NT_STATUS_IS_OK(status)) {
2703 TALLOC_FREE(conn->lsa_pipe_tcp);
2707 *cli = conn->lsa_pipe_tcp;
2712 NTSTATUS cm_connect_lsa(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
2713 struct rpc_pipe_client **cli, struct policy_handle *lsa_policy)
2715 struct winbindd_cm_conn *conn;
2716 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2717 struct netlogon_creds_cli_context *p_creds;
2719 result = init_dc_connection_rpc(domain, false);
2720 if (!NT_STATUS_IS_OK(result))
2723 conn = &domain->conn;
2725 if (rpccli_is_connected(conn->lsa_pipe)) {
2729 TALLOC_FREE(conn->lsa_pipe);
2731 if ((conn->cli->user_name[0] == '\0') ||
2732 (conn->cli->domain[0] == '\0') ||
2733 (conn->cli->password == NULL || conn->cli->password[0] == '\0')) {
2734 DEBUG(10, ("cm_connect_lsa: No no user available for "
2735 "domain %s, trying schannel\n", conn->cli->domain));
2739 /* We have an authenticated connection. Use a NTLMSSP SPNEGO
2740 * authenticated LSA pipe with sign & seal. */
2741 result = cli_rpc_pipe_open_spnego
2742 (conn->cli, &ndr_table_lsarpc, NCACN_NP,
2745 smbXcli_conn_remote_name(conn->cli->conn),
2746 conn->cli->domain, conn->cli->user_name, conn->cli->password,
2749 if (!NT_STATUS_IS_OK(result)) {
2750 DEBUG(10,("cm_connect_lsa: failed to connect to LSA pipe for "
2751 "domain %s using NTLMSSP authenticated pipe: user "
2752 "%s\\%s. Error was %s. Trying schannel.\n",
2753 domain->name, conn->cli->domain,
2754 conn->cli->user_name, nt_errstr(result)));
2758 DEBUG(10,("cm_connect_lsa: connected to LSA pipe for domain %s using "
2759 "NTLMSSP authenticated pipe: user %s\\%s\n",
2760 domain->name, conn->cli->domain, conn->cli->user_name ));
2762 result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
2763 SEC_FLAG_MAXIMUM_ALLOWED,
2765 if (NT_STATUS_IS_OK(result)) {
2769 DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
2772 TALLOC_FREE(conn->lsa_pipe);
2776 /* Fall back to schannel if it's a W2K pre-SP1 box. */
2778 result = cm_get_schannel_creds(domain, &p_creds);
2779 if (!NT_STATUS_IS_OK(result)) {
2780 /* If this call fails - conn->cli can now be NULL ! */
2781 DEBUG(10, ("cm_connect_lsa: Could not get schannel auth info "
2782 "for domain %s (error %s), trying anon\n",
2784 nt_errstr(result) ));
2787 result = cli_rpc_pipe_open_schannel_with_key
2788 (conn->cli, &ndr_table_lsarpc, NCACN_NP,
2789 domain->name, p_creds, &conn->lsa_pipe);
2791 if (!NT_STATUS_IS_OK(result)) {
2792 DEBUG(10,("cm_connect_lsa: failed to connect to LSA pipe for "
2793 "domain %s using schannel. Error was %s\n",
2794 domain->name, nt_errstr(result) ));
2797 DEBUG(10,("cm_connect_lsa: connected to LSA pipe for domain %s using "
2798 "schannel.\n", domain->name ));
2800 result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
2801 SEC_FLAG_MAXIMUM_ALLOWED,
2803 if (NT_STATUS_IS_OK(result)) {
2807 DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
2810 TALLOC_FREE(conn->lsa_pipe);
2814 if (lp_winbind_sealed_pipes() || lp_require_strong_key()) {
2815 result = NT_STATUS_DOWNGRADE_DETECTED;
2816 DEBUG(1, ("Unwilling to make LSA connection to domain %s"
2817 "without connection level security, "
2818 "must set 'winbind sealed pipes = false' and "
2819 "'require strong key = false' to proceed: %s\n",
2820 domain->name, nt_errstr(result)));
2824 result = cli_rpc_pipe_open_noauth(conn->cli,
2827 if (!NT_STATUS_IS_OK(result)) {
2831 result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
2832 SEC_FLAG_MAXIMUM_ALLOWED,
2835 if (!NT_STATUS_IS_OK(result)) {
2836 invalidate_cm_connection(conn);
2840 *cli = conn->lsa_pipe;
2841 *lsa_policy = conn->lsa_policy;
2845 /****************************************************************************
2846 Open a LSA connection to a DC, suiteable for LSA lookup calls.
2847 ****************************************************************************/
2849 NTSTATUS cm_connect_lsat(struct winbindd_domain *domain,
2850 TALLOC_CTX *mem_ctx,
2851 struct rpc_pipe_client **cli,
2852 struct policy_handle *lsa_policy)
2856 if (domain->can_do_ncacn_ip_tcp) {
2857 status = cm_connect_lsa_tcp(domain, mem_ctx, cli);
2858 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
2859 NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR) ||
2860 NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_ACCESS_DENIED)) {
2861 invalidate_cm_connection(&domain->conn);
2862 status = cm_connect_lsa_tcp(domain, mem_ctx, cli);
2864 if (NT_STATUS_IS_OK(status)) {
2869 * we tried twice to connect via ncan_ip_tcp and schannel and
2870 * failed - maybe it is a trusted domain we can't connect to ?
2871 * do not try tcp next time - gd
2873 domain->can_do_ncacn_ip_tcp = false;
2876 status = cm_connect_lsa(domain, mem_ctx, cli, lsa_policy);
2881 /****************************************************************************
2882 Open the netlogon pipe to this DC. Use schannel if specified in client conf.
2883 session key stored in conn->netlogon_pipe->dc->sess_key.
2884 ****************************************************************************/
2886 NTSTATUS cm_connect_netlogon(struct winbindd_domain *domain,
2887 struct rpc_pipe_client **cli)
2889 struct messaging_context *msg_ctx = winbind_messaging_context();
2890 struct winbindd_cm_conn *conn;
2892 enum netr_SchannelType sec_chan_type;
2893 const char *_account_name;
2894 const char *account_name;
2895 struct samr_Password current_nt_hash;
2896 struct samr_Password *previous_nt_hash = NULL;
2897 struct netlogon_creds_CredentialState *creds = NULL;
2902 result = init_dc_connection_rpc(domain, true);
2903 if (!NT_STATUS_IS_OK(result)) {
2907 conn = &domain->conn;
2909 if (rpccli_is_connected(conn->netlogon_pipe)) {
2910 *cli = conn->netlogon_pipe;
2911 return NT_STATUS_OK;
2914 TALLOC_FREE(conn->netlogon_pipe);
2915 conn->netlogon_flags = 0;
2916 TALLOC_FREE(conn->netlogon_creds);
2918 if ((!IS_DC) && (!domain->primary)) {
2922 ok = get_trust_pw_hash(domain->name,
2923 current_nt_hash.hash,
2927 DEBUG(1, ("get_trust_pw_hash failed for %s, "
2928 "unable to get NETLOGON credentials\n",
2930 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2933 account_name = talloc_asprintf(talloc_tos(), "%s$", _account_name);
2934 if (account_name == NULL) {
2935 return NT_STATUS_NO_MEMORY;
2938 result = rpccli_create_netlogon_creds(domain->dcname,
2944 &conn->netlogon_creds);
2945 if (!NT_STATUS_IS_OK(result)) {
2946 DEBUG(1, ("rpccli_create_netlogon_creds failed for %s, "
2947 "unable to create NETLOGON credentials: %s\n",
2948 domain->name, nt_errstr(result)));
2949 SAFE_FREE(previous_nt_hash);
2953 result = rpccli_setup_netlogon_creds(conn->cli,
2954 conn->netlogon_creds,
2955 conn->netlogon_force_reauth,
2958 conn->netlogon_force_reauth = false;
2959 SAFE_FREE(previous_nt_hash);
2960 if (!NT_STATUS_IS_OK(result)) {
2961 DEBUG(1, ("rpccli_setup_netlogon_creds failed for %s, "
2962 "unable to setup NETLOGON credentials: %s\n",
2963 domain->name, nt_errstr(result)));
2967 result = netlogon_creds_cli_get(conn->netlogon_creds,
2970 if (!NT_STATUS_IS_OK(result)) {
2971 DEBUG(1, ("netlogon_creds_cli_get failed for %s, "
2972 "unable to get NETLOGON credentials: %s\n",
2973 domain->name, nt_errstr(result)));
2976 conn->netlogon_flags = creds->negotiate_flags;
2980 if (!(conn->netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
2981 if (lp_winbind_sealed_pipes() || lp_require_strong_key()) {
2982 result = NT_STATUS_DOWNGRADE_DETECTED;
2983 DEBUG(1, ("Unwilling to make connection to domain %s"
2984 "without connection level security, "
2985 "must set 'winbind sealed pipes = false' and "
2986 "'require strong key = false' to proceed: %s\n",
2987 domain->name, nt_errstr(result)));
2988 invalidate_cm_connection(conn);
2991 result = cli_rpc_pipe_open_noauth(conn->cli,
2992 &ndr_table_netlogon,
2993 &conn->netlogon_pipe);
2994 if (!NT_STATUS_IS_OK(result)) {
2995 invalidate_cm_connection(conn);
2999 *cli = conn->netlogon_pipe;
3000 return NT_STATUS_OK;
3003 /* Using the credentials from the first pipe, open a signed and sealed
3004 second netlogon pipe. The session key is stored in the schannel
3005 part of the new pipe auth struct.
3008 result = cli_rpc_pipe_open_schannel_with_key(
3009 conn->cli, &ndr_table_netlogon, NCACN_NP,
3011 conn->netlogon_creds,
3012 &conn->netlogon_pipe);
3013 if (!NT_STATUS_IS_OK(result)) {
3014 DEBUG(3, ("Could not open schannel'ed NETLOGON pipe. Error "
3015 "was %s\n", nt_errstr(result)));
3017 invalidate_cm_connection(conn);
3021 *cli = conn->netlogon_pipe;
3022 return NT_STATUS_OK;
3025 void winbind_msg_ip_dropped(struct messaging_context *msg_ctx,
3028 struct server_id server_id,
3031 struct winbindd_domain *domain;
3032 char *freeit = NULL;
3036 || (data->data == NULL)
3037 || (data->length == 0)
3038 || (data->data[data->length-1] != '\0')) {
3039 DEBUG(1, ("invalid msg_ip_dropped message: not a valid "
3044 addr = (char *)data->data;
3045 DEBUG(10, ("IP %s dropped\n", addr));
3047 if (!is_ipaddress(addr)) {
3050 * Some code sends us ip addresses with the /netmask
3053 slash = strchr(addr, '/');
3054 if (slash == NULL) {
3055 DEBUG(1, ("invalid msg_ip_dropped message: %s",
3059 freeit = talloc_strndup(talloc_tos(), addr, slash-addr);
3060 if (freeit == NULL) {
3061 DEBUG(1, ("talloc failed\n"));
3065 DEBUG(10, ("Stripped /netmask to IP %s\n", addr));
3068 for (domain = domain_list(); domain != NULL; domain = domain->next) {
3069 char sockaddr[INET6_ADDRSTRLEN];
3071 if (!cli_state_is_connected(domain->conn.cli)) {
3075 print_sockaddr(sockaddr, sizeof(sockaddr),
3076 smbXcli_conn_local_sockaddr(domain->conn.cli->conn));
3078 if (strequal(sockaddr, addr)) {
3079 smbXcli_conn_disconnect(domain->conn.cli->conn, NT_STATUS_OK);
3082 TALLOC_FREE(freeit);