Fix a memleak caused by a crappy get_sorted_dc_list() API
[Samba.git] / source / winbindd / winbindd_cm.c
blob74cd5db32173e5b41d32c39b0052c83fa46260e9
1 /*
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
34 selection etc
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
46 implementation. -tpot
51 TODO:
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
57 various sam handles.
61 #include "includes.h"
62 #include "winbindd.h"
64 #undef DBGC_CLASS
65 #define DBGC_CLASS DBGC_WINBIND
67 struct dc_name_ip {
68 fstring name;
69 struct sockaddr_storage ss;
72 extern struct winbindd_methods reconnect_methods;
73 extern bool override_logfile;
75 static NTSTATUS init_dc_connection_network(struct winbindd_domain *domain);
76 static void set_dc_type_and_flags( struct winbindd_domain *domain );
77 static bool get_dcs(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
78 struct dc_name_ip **dcs, int *num_dcs);
80 /****************************************************************
81 Child failed to find DC's. Reschedule check.
82 ****************************************************************/
84 static void msg_failed_to_go_online(struct messaging_context *msg,
85 void *private_data,
86 uint32_t msg_type,
87 struct server_id server_id,
88 DATA_BLOB *data)
90 struct winbindd_domain *domain;
91 const char *domainname = (const char *)data->data;
93 if (data->data == NULL || data->length == 0) {
94 return;
97 DEBUG(5,("msg_fail_to_go_online: received for domain %s.\n", domainname));
99 for (domain = domain_list(); domain; domain = domain->next) {
100 if (domain->internal) {
101 continue;
104 if (strequal(domain->name, domainname)) {
105 if (domain->online) {
106 /* We're already online, ignore. */
107 DEBUG(5,("msg_fail_to_go_online: domain %s "
108 "already online.\n", domainname));
109 continue;
112 /* Reschedule the online check. */
113 set_domain_offline(domain);
114 break;
119 /****************************************************************
120 Actually cause a reconnect from a message.
121 ****************************************************************/
123 static void msg_try_to_go_online(struct messaging_context *msg,
124 void *private_data,
125 uint32_t msg_type,
126 struct server_id server_id,
127 DATA_BLOB *data)
129 struct winbindd_domain *domain;
130 const char *domainname = (const char *)data->data;
132 if (data->data == NULL || data->length == 0) {
133 return;
136 DEBUG(5,("msg_try_to_go_online: received for domain %s.\n", domainname));
138 for (domain = domain_list(); domain; domain = domain->next) {
139 if (domain->internal) {
140 continue;
143 if (strequal(domain->name, domainname)) {
145 if (domain->online) {
146 /* We're already online, ignore. */
147 DEBUG(5,("msg_try_to_go_online: domain %s "
148 "already online.\n", domainname));
149 continue;
152 /* This call takes care of setting the online
153 flag to true if we connected, or re-adding
154 the offline handler if false. Bypasses online
155 check so always does network calls. */
157 init_dc_connection_network(domain);
158 break;
163 /****************************************************************
164 Fork a child to try and contact a DC. Do this as contacting a
165 DC requires blocking lookups and we don't want to block our
166 parent.
167 ****************************************************************/
169 static bool fork_child_dc_connect(struct winbindd_domain *domain)
171 struct dc_name_ip *dcs = NULL;
172 int num_dcs = 0;
173 TALLOC_CTX *mem_ctx = NULL;
174 pid_t child_pid;
175 pid_t parent_pid = sys_getpid();
177 /* Stop zombies */
178 CatchChild();
180 child_pid = sys_fork();
182 if (child_pid == -1) {
183 DEBUG(0, ("fork_child_dc_connect: Could not fork: %s\n", strerror(errno)));
184 return False;
187 if (child_pid != 0) {
188 /* Parent */
189 messaging_register(winbind_messaging_context(), NULL,
190 MSG_WINBIND_TRY_TO_GO_ONLINE,
191 msg_try_to_go_online);
192 messaging_register(winbind_messaging_context(), NULL,
193 MSG_WINBIND_FAILED_TO_GO_ONLINE,
194 msg_failed_to_go_online);
195 return True;
198 /* Child. */
200 /* Leave messages blocked - we will never process one. */
202 if (!reinit_after_fork(winbind_messaging_context(), true)) {
203 DEBUG(0,("reinit_after_fork() failed\n"));
204 _exit(0);
207 close_conns_after_fork();
209 if (!override_logfile) {
210 char *logfile;
211 if (asprintf(&logfile, "%s/log.winbindd-dc-connect", get_dyn_LOGFILEBASE()) > 0) {
212 lp_set_logfile(logfile);
213 SAFE_FREE(logfile);
214 reopen_logs();
218 mem_ctx = talloc_init("fork_child_dc_connect");
219 if (!mem_ctx) {
220 DEBUG(0,("talloc_init failed.\n"));
221 _exit(0);
224 if ((!get_dcs(mem_ctx, domain, &dcs, &num_dcs)) || (num_dcs == 0)) {
225 /* Still offline ? Can't find DC's. */
226 messaging_send_buf(winbind_messaging_context(),
227 pid_to_procid(parent_pid),
228 MSG_WINBIND_FAILED_TO_GO_ONLINE,
229 (uint8 *)domain->name,
230 strlen(domain->name)+1);
231 _exit(0);
234 /* We got a DC. Send a message to our parent to get it to
235 try and do the same. */
237 messaging_send_buf(winbind_messaging_context(),
238 pid_to_procid(parent_pid),
239 MSG_WINBIND_TRY_TO_GO_ONLINE,
240 (uint8 *)domain->name,
241 strlen(domain->name)+1);
242 _exit(0);
245 /****************************************************************
246 Handler triggered if we're offline to try and detect a DC.
247 ****************************************************************/
249 static void check_domain_online_handler(struct event_context *ctx,
250 struct timed_event *te,
251 const struct timeval *now,
252 void *private_data)
254 struct winbindd_domain *domain =
255 (struct winbindd_domain *)private_data;
257 DEBUG(10,("check_domain_online_handler: called for domain "
258 "%s (online = %s)\n", domain->name,
259 domain->online ? "True" : "False" ));
261 TALLOC_FREE(domain->check_online_event);
263 /* Are we still in "startup" mode ? */
265 if (domain->startup && (now->tv_sec > domain->startup_time + 30)) {
266 /* No longer in "startup" mode. */
267 DEBUG(10,("check_domain_online_handler: domain %s no longer in 'startup' mode.\n",
268 domain->name ));
269 domain->startup = False;
272 /* We've been told to stay offline, so stay
273 that way. */
275 if (get_global_winbindd_state_offline()) {
276 DEBUG(10,("check_domain_online_handler: domain %s remaining globally offline\n",
277 domain->name ));
278 return;
281 /* Fork a child to test if it can contact a DC.
282 If it can then send ourselves a message to
283 cause a reconnect. */
285 fork_child_dc_connect(domain);
288 /****************************************************************
289 If we're still offline setup the timeout check.
290 ****************************************************************/
292 static void calc_new_online_timeout_check(struct winbindd_domain *domain)
294 int wbc = lp_winbind_cache_time();
296 if (domain->startup) {
297 domain->check_online_timeout = 10;
298 } else if (domain->check_online_timeout < wbc) {
299 domain->check_online_timeout = wbc;
303 /****************************************************************
304 Set domain offline and also add handler to put us back online
305 if we detect a DC.
306 ****************************************************************/
308 void set_domain_offline(struct winbindd_domain *domain)
310 DEBUG(10,("set_domain_offline: called for domain %s\n",
311 domain->name ));
313 TALLOC_FREE(domain->check_online_event);
315 if (domain->internal) {
316 DEBUG(3,("set_domain_offline: domain %s is internal - logic error.\n",
317 domain->name ));
318 return;
321 domain->online = False;
323 /* Offline domains are always initialized. They're
324 re-initialized when they go back online. */
326 domain->initialized = True;
328 /* We only add the timeout handler that checks and
329 allows us to go back online when we've not
330 been told to remain offline. */
332 if (get_global_winbindd_state_offline()) {
333 DEBUG(10,("set_domain_offline: domain %s remaining globally offline\n",
334 domain->name ));
335 return;
338 /* If we're in statup mode, check again in 10 seconds, not in
339 lp_winbind_cache_time() seconds (which is 5 mins by default). */
341 calc_new_online_timeout_check(domain);
343 domain->check_online_event = event_add_timed(winbind_event_context(),
344 NULL,
345 timeval_current_ofs(domain->check_online_timeout,0),
346 "check_domain_online_handler",
347 check_domain_online_handler,
348 domain);
350 /* The above *has* to succeed for winbindd to work. */
351 if (!domain->check_online_event) {
352 smb_panic("set_domain_offline: failed to add online handler");
355 DEBUG(10,("set_domain_offline: added event handler for domain %s\n",
356 domain->name ));
358 /* Send an offline message to the idmap child when our
359 primary domain goes offline */
361 if ( domain->primary ) {
362 struct winbindd_child *idmap = idmap_child();
364 if ( idmap->pid != 0 ) {
365 messaging_send_buf(winbind_messaging_context(),
366 pid_to_procid(idmap->pid),
367 MSG_WINBIND_OFFLINE,
368 (uint8 *)domain->name,
369 strlen(domain->name)+1);
373 return;
376 /****************************************************************
377 Set domain online - if allowed.
378 ****************************************************************/
380 static void set_domain_online(struct winbindd_domain *domain)
382 struct timeval now;
384 DEBUG(10,("set_domain_online: called for domain %s\n",
385 domain->name ));
387 if (domain->internal) {
388 DEBUG(3,("set_domain_online: domain %s is internal - logic error.\n",
389 domain->name ));
390 return;
393 if (get_global_winbindd_state_offline()) {
394 DEBUG(10,("set_domain_online: domain %s remaining globally offline\n",
395 domain->name ));
396 return;
399 winbindd_set_locator_kdc_envs(domain);
401 /* If we are waiting to get a krb5 ticket, trigger immediately. */
402 GetTimeOfDay(&now);
403 set_event_dispatch_time(winbind_event_context(),
404 "krb5_ticket_gain_handler", now);
406 /* Ok, we're out of any startup mode now... */
407 domain->startup = False;
409 if (domain->online == False) {
410 /* We were offline - now we're online. We default to
411 using the MS-RPC backend if we started offline,
412 and if we're going online for the first time we
413 should really re-initialize the backends and the
414 checks to see if we're talking to an AD or NT domain.
417 domain->initialized = False;
419 /* 'reconnect_methods' is the MS-RPC backend. */
420 if (domain->backend == &reconnect_methods) {
421 domain->backend = NULL;
425 /* Ensure we have no online timeout checks. */
426 domain->check_online_timeout = 0;
427 TALLOC_FREE(domain->check_online_event);
429 /* Ensure we ignore any pending child messages. */
430 messaging_deregister(winbind_messaging_context(),
431 MSG_WINBIND_TRY_TO_GO_ONLINE, NULL);
432 messaging_deregister(winbind_messaging_context(),
433 MSG_WINBIND_FAILED_TO_GO_ONLINE, NULL);
435 domain->online = True;
437 /* Send an online message to the idmap child when our
438 primary domain comes online */
440 if ( domain->primary ) {
441 struct winbindd_child *idmap = idmap_child();
443 if ( idmap->pid != 0 ) {
444 messaging_send_buf(winbind_messaging_context(),
445 pid_to_procid(idmap->pid),
446 MSG_WINBIND_ONLINE,
447 (uint8 *)domain->name,
448 strlen(domain->name)+1);
452 return;
455 /****************************************************************
456 Requested to set a domain online.
457 ****************************************************************/
459 void set_domain_online_request(struct winbindd_domain *domain)
461 struct timeval tev;
463 DEBUG(10,("set_domain_online_request: called for domain %s\n",
464 domain->name ));
466 if (get_global_winbindd_state_offline()) {
467 DEBUG(10,("set_domain_online_request: domain %s remaining globally offline\n",
468 domain->name ));
469 return;
472 /* We've been told it's safe to go online and
473 try and connect to a DC. But I don't believe it
474 because network manager seems to lie.
475 Wait at least 5 seconds. Heuristics suck... */
477 if (!domain->check_online_event) {
478 /* If we've come from being globally offline we
479 don't have a check online event handler set.
480 We need to add one now we're trying to go
481 back online. */
483 DEBUG(10,("set_domain_online_request: domain %s was globally offline.\n",
484 domain->name ));
486 domain->check_online_event = event_add_timed(winbind_event_context(),
487 NULL,
488 timeval_current_ofs(5, 0),
489 "check_domain_online_handler",
490 check_domain_online_handler,
491 domain);
493 /* The above *has* to succeed for winbindd to work. */
494 if (!domain->check_online_event) {
495 smb_panic("set_domain_online_request: failed to add online handler");
499 GetTimeOfDay(&tev);
501 /* Go into "startup" mode again. */
502 domain->startup_time = tev.tv_sec;
503 domain->startup = True;
505 tev.tv_sec += 5;
507 set_event_dispatch_time(winbind_event_context(), "check_domain_online_handler", tev);
510 /****************************************************************
511 Add -ve connection cache entries for domain and realm.
512 ****************************************************************/
514 void winbind_add_failed_connection_entry(const struct winbindd_domain *domain,
515 const char *server,
516 NTSTATUS result)
518 add_failed_connection_entry(domain->name, server, result);
519 /* If this was the saf name for the last thing we talked to,
520 remove it. */
521 saf_delete(domain->name);
522 if (*domain->alt_name) {
523 add_failed_connection_entry(domain->alt_name, server, result);
524 saf_delete(domain->alt_name);
526 winbindd_unset_locator_kdc_env(domain);
529 /* Choose between anonymous or authenticated connections. We need to use
530 an authenticated connection if DCs have the RestrictAnonymous registry
531 entry set > 0, or the "Additional restrictions for anonymous
532 connections" set in the win2k Local Security Policy.
534 Caller to free() result in domain, username, password
537 static void cm_get_ipc_userpass(char **username, char **domain, char **password)
539 *username = (char *)secrets_fetch(SECRETS_AUTH_USER, NULL);
540 *domain = (char *)secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
541 *password = (char *)secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
543 if (*username && **username) {
545 if (!*domain || !**domain)
546 *domain = smb_xstrdup(lp_workgroup());
548 if (!*password || !**password)
549 *password = smb_xstrdup("");
551 DEBUG(3, ("cm_get_ipc_userpass: Retrieved auth-user from secrets.tdb [%s\\%s]\n",
552 *domain, *username));
554 } else {
555 DEBUG(3, ("cm_get_ipc_userpass: No auth-user defined\n"));
556 *username = smb_xstrdup("");
557 *domain = smb_xstrdup("");
558 *password = smb_xstrdup("");
562 static bool get_dc_name_via_netlogon(struct winbindd_domain *domain,
563 fstring dcname,
564 struct sockaddr_storage *dc_ss)
566 struct winbindd_domain *our_domain = NULL;
567 struct rpc_pipe_client *netlogon_pipe = NULL;
568 NTSTATUS result;
569 WERROR werr;
570 TALLOC_CTX *mem_ctx;
571 unsigned int orig_timeout;
572 const char *tmp = NULL;
573 const char *p;
575 /* Hmmmm. We can only open one connection to the NETLOGON pipe at the
576 * moment.... */
578 if (IS_DC) {
579 return False;
582 if (domain->primary) {
583 return False;
586 our_domain = find_our_domain();
588 if ((mem_ctx = talloc_init("get_dc_name_via_netlogon")) == NULL) {
589 return False;
592 result = cm_connect_netlogon(our_domain, &netlogon_pipe);
593 if (!NT_STATUS_IS_OK(result)) {
594 talloc_destroy(mem_ctx);
595 return False;
598 /* This call can take a long time - allow the server to time out.
599 35 seconds should do it. */
601 orig_timeout = cli_set_timeout(netlogon_pipe->cli, 35000);
603 if (our_domain->active_directory) {
604 struct netr_DsRGetDCNameInfo *domain_info = NULL;
606 result = rpccli_netr_DsRGetDCName(netlogon_pipe,
607 mem_ctx,
608 our_domain->dcname,
609 domain->name,
610 NULL,
611 NULL,
612 DS_RETURN_DNS_NAME,
613 &domain_info,
614 &werr);
615 if (W_ERROR_IS_OK(werr)) {
616 tmp = talloc_strdup(
617 mem_ctx, domain_info->dc_unc);
618 if (tmp == NULL) {
619 DEBUG(0, ("talloc_strdup failed\n"));
620 talloc_destroy(mem_ctx);
621 return false;
623 if (strlen(domain->alt_name) == 0) {
624 fstrcpy(domain->alt_name,
625 domain_info->domain_name);
627 if (strlen(domain->forest_name) == 0) {
628 fstrcpy(domain->forest_name,
629 domain_info->forest_name);
632 } else {
633 result = rpccli_netr_GetAnyDCName(netlogon_pipe, mem_ctx,
634 our_domain->dcname,
635 domain->name,
636 &tmp,
637 &werr);
640 /* And restore our original timeout. */
641 cli_set_timeout(netlogon_pipe->cli, orig_timeout);
643 if (!NT_STATUS_IS_OK(result)) {
644 DEBUG(10,("rpccli_netr_GetAnyDCName failed: %s\n",
645 nt_errstr(result)));
646 talloc_destroy(mem_ctx);
647 return false;
650 if (!W_ERROR_IS_OK(werr)) {
651 DEBUG(10,("rpccli_netr_GetAnyDCName failed: %s\n",
652 dos_errstr(werr)));
653 talloc_destroy(mem_ctx);
654 return false;
657 /* rpccli_netr_GetAnyDCName gives us a name with \\ */
658 p = strip_hostname(tmp);
660 fstrcpy(dcname, p);
662 talloc_destroy(mem_ctx);
664 DEBUG(10,("rpccli_netr_GetAnyDCName returned %s\n", dcname));
666 if (!resolve_name(dcname, dc_ss, 0x20)) {
667 return False;
670 return True;
674 * Helper function to assemble trust password and account name
676 static NTSTATUS get_trust_creds(const struct winbindd_domain *domain,
677 char **machine_password,
678 char **machine_account,
679 char **machine_krb5_principal)
681 const char *account_name;
682 const char *name = NULL;
684 /* If we are a DC and this is not our own domain */
686 if (IS_DC) {
687 name = domain->name;
688 } else {
689 struct winbindd_domain *our_domain = find_our_domain();
691 if (!our_domain)
692 return NT_STATUS_INVALID_SERVER_STATE;
694 name = our_domain->name;
697 if (!get_trust_pw_clear(name, machine_password,
698 &account_name, NULL))
700 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
703 if ((machine_account != NULL) &&
704 (asprintf(machine_account, "%s$", account_name) == -1))
706 return NT_STATUS_NO_MEMORY;
709 /* this is at least correct when domain is our domain,
710 * which is the only case, when this is currently used: */
711 if (machine_krb5_principal != NULL)
713 if (asprintf(machine_krb5_principal, "%s$@%s",
714 account_name, domain->alt_name) == -1)
716 return NT_STATUS_NO_MEMORY;
719 strupper_m(*machine_krb5_principal);
722 return NT_STATUS_OK;
725 /************************************************************************
726 Given a fd with a just-connected TCP connection to a DC, open a connection
727 to the pipe.
728 ************************************************************************/
730 static NTSTATUS cm_prepare_connection(const struct winbindd_domain *domain,
731 const int sockfd,
732 const char *controller,
733 struct cli_state **cli,
734 bool *retry)
736 char *machine_password = NULL;
737 char *machine_krb5_principal = NULL;
738 char *machine_account = NULL;
739 char *ipc_username = NULL;
740 char *ipc_domain = NULL;
741 char *ipc_password = NULL;
743 struct named_mutex *mutex;
745 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
747 struct sockaddr peeraddr;
748 socklen_t peeraddr_len;
750 struct sockaddr_in *peeraddr_in = (struct sockaddr_in *)&peeraddr;
752 DEBUG(10,("cm_prepare_connection: connecting to DC %s for domain %s\n",
753 controller, domain->name ));
755 *retry = True;
757 mutex = grab_named_mutex(talloc_tos(), controller,
758 WINBIND_SERVER_MUTEX_WAIT_TIME);
759 if (mutex == NULL) {
760 DEBUG(0,("cm_prepare_connection: mutex grab failed for %s\n",
761 controller));
762 result = NT_STATUS_POSSIBLE_DEADLOCK;
763 goto done;
766 if ((*cli = cli_initialise()) == NULL) {
767 DEBUG(1, ("Could not cli_initialize\n"));
768 result = NT_STATUS_NO_MEMORY;
769 goto done;
772 (*cli)->timeout = 10000; /* 10 seconds */
773 (*cli)->fd = sockfd;
774 fstrcpy((*cli)->desthost, controller);
775 (*cli)->use_kerberos = True;
777 peeraddr_len = sizeof(peeraddr);
779 if ((getpeername((*cli)->fd, &peeraddr, &peeraddr_len) != 0) ||
780 (peeraddr_len != sizeof(struct sockaddr_in)) ||
781 (peeraddr_in->sin_family != PF_INET))
783 DEBUG(0,("cm_prepare_connection: %s\n", strerror(errno)));
784 result = NT_STATUS_UNSUCCESSFUL;
785 goto done;
788 if (ntohs(peeraddr_in->sin_port) == 139) {
789 struct nmb_name calling;
790 struct nmb_name called;
792 make_nmb_name(&calling, global_myname(), 0x0);
793 make_nmb_name(&called, "*SMBSERVER", 0x20);
795 if (!cli_session_request(*cli, &calling, &called)) {
796 DEBUG(8, ("cli_session_request failed for %s\n",
797 controller));
798 result = NT_STATUS_UNSUCCESSFUL;
799 goto done;
803 cli_setup_signing_state(*cli, Undefined);
805 if (!cli_negprot(*cli)) {
806 DEBUG(1, ("cli_negprot failed\n"));
807 result = NT_STATUS_UNSUCCESSFUL;
808 goto done;
811 if (!is_trusted_domain_situation(domain->name) &&
812 (*cli)->protocol >= PROTOCOL_NT1 &&
813 (*cli)->capabilities & CAP_EXTENDED_SECURITY)
815 ADS_STATUS ads_status;
817 result = get_trust_creds(domain, &machine_password,
818 &machine_account,
819 &machine_krb5_principal);
820 if (!NT_STATUS_IS_OK(result)) {
821 goto anon_fallback;
824 if (lp_security() == SEC_ADS) {
826 /* Try a krb5 session */
828 (*cli)->use_kerberos = True;
829 DEBUG(5, ("connecting to %s from %s with kerberos principal "
830 "[%s]\n", controller, global_myname(),
831 machine_krb5_principal));
833 winbindd_set_locator_kdc_envs(domain);
835 ads_status = cli_session_setup_spnego(*cli,
836 machine_krb5_principal,
837 machine_password,
838 domain->name);
840 if (!ADS_ERR_OK(ads_status)) {
841 DEBUG(4,("failed kerberos session setup with %s\n",
842 ads_errstr(ads_status)));
845 result = ads_ntstatus(ads_status);
846 if (NT_STATUS_IS_OK(result)) {
847 /* Ensure creds are stored for NTLMSSP authenticated pipe access. */
848 cli_init_creds(*cli, machine_account, domain->name, machine_password);
849 goto session_setup_done;
853 /* Fall back to non-kerberos session setup using NTLMSSP SPNEGO with the machine account. */
854 (*cli)->use_kerberos = False;
856 DEBUG(5, ("connecting to %s from %s with username "
857 "[%s]\\[%s]\n", controller, global_myname(),
858 domain->name, machine_account));
860 ads_status = cli_session_setup_spnego(*cli,
861 machine_account,
862 machine_password,
863 domain->name);
864 if (!ADS_ERR_OK(ads_status)) {
865 DEBUG(4, ("authenticated session setup failed with %s\n",
866 ads_errstr(ads_status)));
869 result = ads_ntstatus(ads_status);
870 if (NT_STATUS_IS_OK(result)) {
871 /* Ensure creds are stored for NTLMSSP authenticated pipe access. */
872 cli_init_creds(*cli, machine_account, domain->name, machine_password);
873 goto session_setup_done;
877 /* Fall back to non-kerberos session setup with auth_user */
879 (*cli)->use_kerberos = False;
881 cm_get_ipc_userpass(&ipc_username, &ipc_domain, &ipc_password);
883 if ((((*cli)->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) != 0) &&
884 (strlen(ipc_username) > 0)) {
886 /* Only try authenticated if we have a username */
888 DEBUG(5, ("connecting to %s from %s with username "
889 "[%s]\\[%s]\n", controller, global_myname(),
890 ipc_domain, ipc_username));
892 if (NT_STATUS_IS_OK(cli_session_setup(
893 *cli, ipc_username,
894 ipc_password, strlen(ipc_password)+1,
895 ipc_password, strlen(ipc_password)+1,
896 ipc_domain))) {
897 /* Successful logon with given username. */
898 cli_init_creds(*cli, ipc_username, ipc_domain, ipc_password);
899 goto session_setup_done;
900 } else {
901 DEBUG(4, ("authenticated session setup with user %s\\%s failed.\n",
902 ipc_domain, ipc_username ));
906 anon_fallback:
908 /* Fall back to anonymous connection, this might fail later */
910 if (NT_STATUS_IS_OK(cli_session_setup(*cli, "", NULL, 0,
911 NULL, 0, ""))) {
912 DEBUG(5, ("Connected anonymously\n"));
913 cli_init_creds(*cli, "", "", "");
914 goto session_setup_done;
917 result = cli_nt_error(*cli);
919 if (NT_STATUS_IS_OK(result))
920 result = NT_STATUS_UNSUCCESSFUL;
922 /* We can't session setup */
924 goto done;
926 session_setup_done:
928 /* cache the server name for later connections */
930 saf_store( domain->name, (*cli)->desthost );
931 if (domain->alt_name && (*cli)->use_kerberos) {
932 saf_store( domain->alt_name, (*cli)->desthost );
935 winbindd_set_locator_kdc_envs(domain);
937 if (!cli_send_tconX(*cli, "IPC$", "IPC", "", 0)) {
939 result = cli_nt_error(*cli);
941 DEBUG(1,("failed tcon_X with %s\n", nt_errstr(result)));
943 if (NT_STATUS_IS_OK(result))
944 result = NT_STATUS_UNSUCCESSFUL;
946 goto done;
949 TALLOC_FREE(mutex);
950 *retry = False;
952 /* set the domain if empty; needed for schannel connections */
953 if ( !*(*cli)->domain ) {
954 fstrcpy( (*cli)->domain, domain->name );
957 result = NT_STATUS_OK;
959 done:
960 TALLOC_FREE(mutex);
961 SAFE_FREE(machine_account);
962 SAFE_FREE(machine_password);
963 SAFE_FREE(machine_krb5_principal);
964 SAFE_FREE(ipc_username);
965 SAFE_FREE(ipc_domain);
966 SAFE_FREE(ipc_password);
968 if (!NT_STATUS_IS_OK(result)) {
969 winbind_add_failed_connection_entry(domain, controller, result);
970 if ((*cli) != NULL) {
971 cli_shutdown(*cli);
972 *cli = NULL;
976 return result;
979 static bool add_one_dc_unique(TALLOC_CTX *mem_ctx, const char *domain_name,
980 const char *dcname, struct sockaddr_storage *pss,
981 struct dc_name_ip **dcs, int *num)
983 if (!NT_STATUS_IS_OK(check_negative_conn_cache(domain_name, dcname))) {
984 DEBUG(10, ("DC %s was in the negative conn cache\n", dcname));
985 return False;
988 *dcs = TALLOC_REALLOC_ARRAY(mem_ctx, *dcs, struct dc_name_ip, (*num)+1);
990 if (*dcs == NULL)
991 return False;
993 fstrcpy((*dcs)[*num].name, dcname);
994 (*dcs)[*num].ss = *pss;
995 *num += 1;
996 return True;
999 static bool add_sockaddr_to_array(TALLOC_CTX *mem_ctx,
1000 struct sockaddr_storage *pss, uint16 port,
1001 struct sockaddr_storage **addrs, int *num)
1003 *addrs = TALLOC_REALLOC_ARRAY(mem_ctx, *addrs, struct sockaddr_storage, (*num)+1);
1005 if (*addrs == NULL) {
1006 *num = 0;
1007 return False;
1010 (*addrs)[*num] = *pss;
1011 set_sockaddr_port(&(*addrs)[*num], port);
1013 *num += 1;
1014 return True;
1017 /*******************************************************************
1018 convert an ip to a name
1019 *******************************************************************/
1021 static bool dcip_to_name(TALLOC_CTX *mem_ctx,
1022 const struct winbindd_domain *domain,
1023 struct sockaddr_storage *pss,
1024 fstring name )
1026 struct ip_service ip_list;
1027 uint32_t nt_version = NETLOGON_VERSION_1;
1029 ip_list.ss = *pss;
1030 ip_list.port = 0;
1032 #ifdef WITH_ADS
1033 /* For active directory servers, try to get the ldap server name.
1034 None of these failures should be considered critical for now */
1036 if (lp_security() == SEC_ADS) {
1037 ADS_STRUCT *ads;
1038 char addr[INET6_ADDRSTRLEN];
1040 print_sockaddr(addr, sizeof(addr), pss);
1042 ads = ads_init(domain->alt_name, domain->name, NULL);
1043 ads->auth.flags |= ADS_AUTH_NO_BIND;
1045 if (ads_try_connect(ads, addr)) {
1046 /* We got a cldap packet. */
1047 fstrcpy(name, ads->config.ldap_server_name);
1048 namecache_store(name, 0x20, 1, &ip_list);
1050 DEBUG(10,("dcip_to_name: flags = 0x%x\n", (unsigned int)ads->config.flags));
1052 if (domain->primary && (ads->config.flags & NBT_SERVER_KDC)) {
1053 if (ads_closest_dc(ads)) {
1054 char *sitename = sitename_fetch(ads->config.realm);
1056 /* We're going to use this KDC for this realm/domain.
1057 If we are using sites, then force the krb5 libs
1058 to use this KDC. */
1060 create_local_private_krb5_conf_for_domain(domain->alt_name,
1061 domain->name,
1062 sitename,
1063 pss);
1065 SAFE_FREE(sitename);
1066 } else {
1067 /* use an off site KDC */
1068 create_local_private_krb5_conf_for_domain(domain->alt_name,
1069 domain->name,
1070 NULL,
1071 pss);
1073 winbindd_set_locator_kdc_envs(domain);
1075 /* Ensure we contact this DC also. */
1076 saf_store( domain->name, name);
1077 saf_store( domain->alt_name, name);
1080 ads_destroy( &ads );
1081 return True;
1084 ads_destroy( &ads );
1086 #endif
1088 /* try GETDC requests next */
1090 if (send_getdc_request(mem_ctx, winbind_messaging_context(),
1091 pss, domain->name, &domain->sid,
1092 nt_version)) {
1093 const char *dc_name = NULL;
1094 int i;
1095 smb_msleep(100);
1096 for (i=0; i<5; i++) {
1097 if (receive_getdc_response(mem_ctx, pss, domain->name,
1098 &nt_version,
1099 &dc_name, NULL)) {
1100 fstrcpy(name, dc_name);
1101 namecache_store(name, 0x20, 1, &ip_list);
1102 return True;
1104 smb_msleep(500);
1108 /* try node status request */
1110 if ( name_status_find(domain->name, 0x1c, 0x20, pss, name) ) {
1111 namecache_store(name, 0x20, 1, &ip_list);
1112 return True;
1114 return False;
1117 /*******************************************************************
1118 Retreive a list of IP address for domain controllers. Fill in
1119 the dcs[] with results.
1120 *******************************************************************/
1122 static bool get_dcs(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
1123 struct dc_name_ip **dcs, int *num_dcs)
1125 fstring dcname;
1126 struct sockaddr_storage ss;
1127 struct ip_service *ip_list = NULL;
1128 int iplist_size = 0;
1129 int i;
1130 bool is_our_domain;
1131 enum security_types sec = (enum security_types)lp_security();
1133 is_our_domain = strequal(domain->name, lp_workgroup());
1135 if ( !is_our_domain
1136 && get_dc_name_via_netlogon(domain, dcname, &ss)
1137 && add_one_dc_unique(mem_ctx, domain->name, dcname, &ss, dcs, num_dcs) )
1139 char addr[INET6_ADDRSTRLEN];
1140 print_sockaddr(addr, sizeof(addr), &ss);
1141 DEBUG(10, ("Retrieved DC %s at %s via netlogon\n",
1142 dcname, addr));
1143 return True;
1146 if (sec == SEC_ADS) {
1147 char *sitename = NULL;
1149 /* We need to make sure we know the local site before
1150 doing any DNS queries, as this will restrict the
1151 get_sorted_dc_list() call below to only fetching
1152 DNS records for the correct site. */
1154 /* Find any DC to get the site record.
1155 We deliberately don't care about the
1156 return here. */
1158 get_dc_name(domain->name, domain->alt_name, dcname, &ss);
1160 sitename = sitename_fetch(domain->alt_name);
1161 if (sitename) {
1162 NTSTATUS status;
1164 /* Do the site-specific AD dns lookup first. */
1165 status = get_sorted_dc_list(domain->alt_name,
1166 sitename, &ip_list,
1167 &iplist_size, True);
1168 if (!NT_STATUS_IS_OK(status)) {
1170 * Work around a crappy about-to-be-replaced
1171 * get_sorted_dc_list error handling :-)
1173 SAFE_FREE(ip_list);
1174 iplist_size = 0;
1177 for ( i=0; i<iplist_size; i++ ) {
1178 char addr[INET6_ADDRSTRLEN];
1179 print_sockaddr(addr, sizeof(addr),
1180 &ip_list[i].ss);
1181 add_one_dc_unique(mem_ctx,
1182 domain->name,
1183 addr,
1184 &ip_list[i].ss,
1185 dcs,
1186 num_dcs);
1189 SAFE_FREE(ip_list);
1190 SAFE_FREE(sitename);
1191 iplist_size = 0;
1194 /* Now we add DCs from the main AD dns lookup. */
1195 get_sorted_dc_list(domain->alt_name, NULL, &ip_list, &iplist_size, True);
1197 for ( i=0; i<iplist_size; i++ ) {
1198 char addr[INET6_ADDRSTRLEN];
1199 print_sockaddr(addr, sizeof(addr),
1200 &ip_list[i].ss);
1201 add_one_dc_unique(mem_ctx,
1202 domain->name,
1203 addr,
1204 &ip_list[i].ss,
1205 dcs,
1206 num_dcs);
1208 SAFE_FREE(ip_list);
1209 iplist_size = 0;
1212 /* try standard netbios queries if no ADS */
1214 if (iplist_size==0) {
1215 get_sorted_dc_list(domain->name, NULL, &ip_list, &iplist_size, False);
1218 /* FIXME!! this is where we should re-insert the GETDC requests --jerry */
1220 /* now add to the dc array. We'll wait until the last minute
1221 to look up the name of the DC. But we fill in the char* for
1222 the ip now in to make the failed connection cache work */
1224 for ( i=0; i<iplist_size; i++ ) {
1225 char addr[INET6_ADDRSTRLEN];
1226 print_sockaddr(addr, sizeof(addr),
1227 &ip_list[i].ss);
1228 add_one_dc_unique(mem_ctx, domain->name, addr,
1229 &ip_list[i].ss, dcs, num_dcs);
1232 SAFE_FREE( ip_list );
1234 return True;
1237 static bool find_new_dc(TALLOC_CTX *mem_ctx,
1238 struct winbindd_domain *domain,
1239 fstring dcname, struct sockaddr_storage *pss, int *fd)
1241 struct dc_name_ip *dcs = NULL;
1242 int num_dcs = 0;
1244 const char **dcnames = NULL;
1245 int num_dcnames = 0;
1247 struct sockaddr_storage *addrs = NULL;
1248 int num_addrs = 0;
1250 int i, fd_index;
1252 again:
1253 if (!get_dcs(mem_ctx, domain, &dcs, &num_dcs) || (num_dcs == 0))
1254 return False;
1256 for (i=0; i<num_dcs; i++) {
1258 if (!add_string_to_array(mem_ctx, dcs[i].name,
1259 &dcnames, &num_dcnames)) {
1260 return False;
1262 if (!add_sockaddr_to_array(mem_ctx, &dcs[i].ss, 445,
1263 &addrs, &num_addrs)) {
1264 return False;
1267 if (!add_string_to_array(mem_ctx, dcs[i].name,
1268 &dcnames, &num_dcnames)) {
1269 return False;
1271 if (!add_sockaddr_to_array(mem_ctx, &dcs[i].ss, 139,
1272 &addrs, &num_addrs)) {
1273 return False;
1277 if ((num_dcnames == 0) || (num_dcnames != num_addrs))
1278 return False;
1280 if ((addrs == NULL) || (dcnames == NULL))
1281 return False;
1283 /* 5 second timeout. */
1284 if (!open_any_socket_out(addrs, num_addrs, 5000, &fd_index, fd) ) {
1285 for (i=0; i<num_dcs; i++) {
1286 char ab[INET6_ADDRSTRLEN];
1287 print_sockaddr(ab, sizeof(ab), &dcs[i].ss);
1288 DEBUG(10, ("find_new_dc: open_any_socket_out failed for "
1289 "domain %s address %s. Error was %s\n",
1290 domain->name, ab, strerror(errno) ));
1291 winbind_add_failed_connection_entry(domain,
1292 dcs[i].name, NT_STATUS_UNSUCCESSFUL);
1294 return False;
1297 *pss = addrs[fd_index];
1299 if (*dcnames[fd_index] != '\0' && !is_ipaddress(dcnames[fd_index])) {
1300 /* Ok, we've got a name for the DC */
1301 fstrcpy(dcname, dcnames[fd_index]);
1302 return True;
1305 /* Try to figure out the name */
1306 if (dcip_to_name(mem_ctx, domain, pss, dcname)) {
1307 return True;
1310 /* We can not continue without the DC's name */
1311 winbind_add_failed_connection_entry(domain, dcs[fd_index].name,
1312 NT_STATUS_UNSUCCESSFUL);
1313 goto again;
1316 static NTSTATUS cm_open_connection(struct winbindd_domain *domain,
1317 struct winbindd_cm_conn *new_conn)
1319 TALLOC_CTX *mem_ctx;
1320 NTSTATUS result;
1321 char *saf_servername = saf_fetch( domain->name );
1322 int retries;
1324 if ((mem_ctx = talloc_init("cm_open_connection")) == NULL) {
1325 SAFE_FREE(saf_servername);
1326 set_domain_offline(domain);
1327 return NT_STATUS_NO_MEMORY;
1330 /* we have to check the server affinity cache here since
1331 later we selecte a DC based on response time and not preference */
1333 /* Check the negative connection cache
1334 before talking to it. It going down may have
1335 triggered the reconnection. */
1337 if ( saf_servername && NT_STATUS_IS_OK(check_negative_conn_cache( domain->name, saf_servername))) {
1339 DEBUG(10,("cm_open_connection: saf_servername is '%s' for domain %s\n",
1340 saf_servername, domain->name ));
1342 /* convert an ip address to a name */
1343 if (is_ipaddress( saf_servername ) ) {
1344 fstring saf_name;
1345 struct sockaddr_storage ss;
1347 if (!interpret_string_addr(&ss, saf_servername,
1348 AI_NUMERICHOST)) {
1349 return NT_STATUS_UNSUCCESSFUL;
1351 if (dcip_to_name(mem_ctx, domain, &ss, saf_name )) {
1352 fstrcpy( domain->dcname, saf_name );
1353 } else {
1354 winbind_add_failed_connection_entry(
1355 domain, saf_servername,
1356 NT_STATUS_UNSUCCESSFUL);
1358 } else {
1359 fstrcpy( domain->dcname, saf_servername );
1362 SAFE_FREE( saf_servername );
1365 for (retries = 0; retries < 3; retries++) {
1366 int fd = -1;
1367 bool retry = False;
1369 result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1371 DEBUG(10,("cm_open_connection: dcname is '%s' for domain %s\n",
1372 domain->dcname, domain->name ));
1374 if (*domain->dcname
1375 && NT_STATUS_IS_OK(check_negative_conn_cache( domain->name, domain->dcname))
1376 && (resolve_name(domain->dcname, &domain->dcaddr, 0x20)))
1378 struct sockaddr_storage *addrs = NULL;
1379 int num_addrs = 0;
1380 int dummy = 0;
1382 if (!add_sockaddr_to_array(mem_ctx, &domain->dcaddr, 445, &addrs, &num_addrs)) {
1383 set_domain_offline(domain);
1384 talloc_destroy(mem_ctx);
1385 return NT_STATUS_NO_MEMORY;
1387 if (!add_sockaddr_to_array(mem_ctx, &domain->dcaddr, 139, &addrs, &num_addrs)) {
1388 set_domain_offline(domain);
1389 talloc_destroy(mem_ctx);
1390 return NT_STATUS_NO_MEMORY;
1393 /* 5 second timeout. */
1394 if (!open_any_socket_out(addrs, num_addrs, 5000, &dummy, &fd)) {
1395 fd = -1;
1399 if ((fd == -1)
1400 && !find_new_dc(mem_ctx, domain, domain->dcname, &domain->dcaddr, &fd))
1402 /* This is the one place where we will
1403 set the global winbindd offline state
1404 to true, if a "WINBINDD_OFFLINE" entry
1405 is found in the winbindd cache. */
1406 set_global_winbindd_state_offline();
1407 break;
1410 new_conn->cli = NULL;
1412 result = cm_prepare_connection(domain, fd, domain->dcname,
1413 &new_conn->cli, &retry);
1415 if (!retry)
1416 break;
1419 if (NT_STATUS_IS_OK(result)) {
1421 winbindd_set_locator_kdc_envs(domain);
1423 if (domain->online == False) {
1424 /* We're changing state from offline to online. */
1425 set_global_winbindd_state_online();
1427 set_domain_online(domain);
1428 } else {
1429 /* Ensure we setup the retry handler. */
1430 set_domain_offline(domain);
1433 talloc_destroy(mem_ctx);
1434 return result;
1437 /* Close down all open pipes on a connection. */
1439 void invalidate_cm_connection(struct winbindd_cm_conn *conn)
1441 /* We're closing down a possibly dead
1442 connection. Don't have impossibly long (10s) timeouts. */
1444 if (conn->cli) {
1445 cli_set_timeout(conn->cli, 1000); /* 1 second. */
1448 if (conn->samr_pipe != NULL) {
1449 if (!cli_rpc_pipe_close(conn->samr_pipe)) {
1450 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1451 if (conn->cli) {
1452 cli_set_timeout(conn->cli, 500);
1455 conn->samr_pipe = NULL;
1458 if (conn->lsa_pipe != NULL) {
1459 if (!cli_rpc_pipe_close(conn->lsa_pipe)) {
1460 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1461 if (conn->cli) {
1462 cli_set_timeout(conn->cli, 500);
1465 conn->lsa_pipe = NULL;
1468 if (conn->netlogon_pipe != NULL) {
1469 if (!cli_rpc_pipe_close(conn->netlogon_pipe)) {
1470 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1471 if (conn->cli) {
1472 cli_set_timeout(conn->cli, 500);
1475 conn->netlogon_pipe = NULL;
1478 if (conn->cli) {
1479 cli_shutdown(conn->cli);
1482 conn->cli = NULL;
1485 void close_conns_after_fork(void)
1487 struct winbindd_domain *domain;
1489 for (domain = domain_list(); domain; domain = domain->next) {
1490 if (domain->conn.cli == NULL)
1491 continue;
1493 if (domain->conn.cli->fd == -1)
1494 continue;
1496 close(domain->conn.cli->fd);
1497 domain->conn.cli->fd = -1;
1501 static bool connection_ok(struct winbindd_domain *domain)
1503 if (domain->conn.cli == NULL) {
1504 DEBUG(8, ("connection_ok: Connection to %s for domain %s has NULL "
1505 "cli!\n", domain->dcname, domain->name));
1506 return False;
1509 if (!domain->conn.cli->initialised) {
1510 DEBUG(3, ("connection_ok: Connection to %s for domain %s was never "
1511 "initialised!\n", domain->dcname, domain->name));
1512 return False;
1515 if (domain->conn.cli->fd == -1) {
1516 DEBUG(3, ("connection_ok: Connection to %s for domain %s has died or was "
1517 "never started (fd == -1)\n",
1518 domain->dcname, domain->name));
1519 return False;
1522 if (domain->online == False) {
1523 DEBUG(3, ("connection_ok: Domain %s is offline\n", domain->name));
1524 return False;
1527 return True;
1530 /* Initialize a new connection up to the RPC BIND.
1531 Bypass online status check so always does network calls. */
1533 static NTSTATUS init_dc_connection_network(struct winbindd_domain *domain)
1535 NTSTATUS result;
1537 /* Internal connections never use the network. */
1538 if (domain->internal) {
1539 domain->initialized = True;
1540 return NT_STATUS_OK;
1543 if (connection_ok(domain)) {
1544 if (!domain->initialized) {
1545 set_dc_type_and_flags(domain);
1547 return NT_STATUS_OK;
1550 invalidate_cm_connection(&domain->conn);
1552 result = cm_open_connection(domain, &domain->conn);
1554 if (NT_STATUS_IS_OK(result) && !domain->initialized) {
1555 set_dc_type_and_flags(domain);
1558 return result;
1561 NTSTATUS init_dc_connection(struct winbindd_domain *domain)
1563 if (domain->initialized && !domain->online) {
1564 /* We check for online status elsewhere. */
1565 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1568 return init_dc_connection_network(domain);
1571 /******************************************************************************
1572 Set the trust flags (direction and forest location) for a domain
1573 ******************************************************************************/
1575 static bool set_dc_type_and_flags_trustinfo( struct winbindd_domain *domain )
1577 struct winbindd_domain *our_domain;
1578 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1579 struct netr_DomainTrustList trusts;
1580 int i;
1581 uint32 flags = (NETR_TRUST_FLAG_IN_FOREST |
1582 NETR_TRUST_FLAG_OUTBOUND |
1583 NETR_TRUST_FLAG_INBOUND);
1584 struct rpc_pipe_client *cli;
1585 TALLOC_CTX *mem_ctx = NULL;
1587 DEBUG(5, ("set_dc_type_and_flags_trustinfo: domain %s\n", domain->name ));
1589 /* Our primary domain doesn't need to worry about trust flags.
1590 Force it to go through the network setup */
1591 if ( domain->primary ) {
1592 return False;
1595 our_domain = find_our_domain();
1597 if ( !connection_ok(our_domain) ) {
1598 DEBUG(3,("set_dc_type_and_flags_trustinfo: No connection to our domain!\n"));
1599 return False;
1602 /* This won't work unless our domain is AD */
1604 if ( !our_domain->active_directory ) {
1605 return False;
1608 /* Use DsEnumerateDomainTrusts to get us the trust direction
1609 and type */
1611 result = cm_connect_netlogon(our_domain, &cli);
1613 if (!NT_STATUS_IS_OK(result)) {
1614 DEBUG(5, ("set_dc_type_and_flags_trustinfo: Could not open "
1615 "a connection to %s for PIPE_NETLOGON (%s)\n",
1616 domain->name, nt_errstr(result)));
1617 return False;
1620 if ( (mem_ctx = talloc_init("set_dc_type_and_flags_trustinfo")) == NULL ) {
1621 DEBUG(0,("set_dc_type_and_flags_trustinfo: talloc_init() failed!\n"));
1622 return False;
1625 result = rpccli_netr_DsrEnumerateDomainTrusts(cli, mem_ctx,
1626 cli->cli->desthost,
1627 flags,
1628 &trusts,
1629 NULL);
1630 if (!NT_STATUS_IS_OK(result)) {
1631 DEBUG(0,("set_dc_type_and_flags_trustinfo: "
1632 "failed to query trusted domain list: %s\n",
1633 nt_errstr(result)));
1634 talloc_destroy(mem_ctx);
1635 return false;
1638 /* Now find the domain name and get the flags */
1640 for ( i=0; i<trusts.count; i++ ) {
1641 if ( strequal( domain->name, trusts.array[i].netbios_name) ) {
1642 domain->domain_flags = trusts.array[i].trust_flags;
1643 domain->domain_type = trusts.array[i].trust_type;
1644 domain->domain_trust_attribs = trusts.array[i].trust_attributes;
1646 if ( domain->domain_type == NETR_TRUST_TYPE_UPLEVEL )
1647 domain->active_directory = True;
1649 /* This flag is only set if the domain is *our*
1650 primary domain and the primary domain is in
1651 native mode */
1653 domain->native_mode = (domain->domain_flags & NETR_TRUST_FLAG_NATIVE);
1655 DEBUG(5, ("set_dc_type_and_flags_trustinfo: domain %s is %sin "
1656 "native mode.\n", domain->name,
1657 domain->native_mode ? "" : "NOT "));
1659 DEBUG(5,("set_dc_type_and_flags_trustinfo: domain %s is %s"
1660 "running active directory.\n", domain->name,
1661 domain->active_directory ? "" : "NOT "));
1664 domain->initialized = True;
1666 if ( !winbindd_can_contact_domain( domain) )
1667 domain->internal = True;
1669 break;
1673 talloc_destroy( mem_ctx );
1675 return domain->initialized;
1678 /******************************************************************************
1679 We can 'sense' certain things about the DC by it's replies to certain
1680 questions.
1682 This tells us if this particular remote server is Active Directory, and if it
1683 is native mode.
1684 ******************************************************************************/
1686 static void set_dc_type_and_flags_connect( struct winbindd_domain *domain )
1688 NTSTATUS result;
1689 WERROR werr;
1690 TALLOC_CTX *mem_ctx = NULL;
1691 struct rpc_pipe_client *cli;
1692 POLICY_HND pol;
1693 union dssetup_DsRoleInfo info;
1694 union lsa_PolicyInformation *lsa_info = NULL;
1696 if (!connection_ok(domain)) {
1697 return;
1700 mem_ctx = talloc_init("set_dc_type_and_flags on domain %s\n",
1701 domain->name);
1702 if (!mem_ctx) {
1703 DEBUG(1, ("set_dc_type_and_flags_connect: talloc_init() failed\n"));
1704 return;
1707 DEBUG(5, ("set_dc_type_and_flags_connect: domain %s\n", domain->name ));
1709 cli = cli_rpc_pipe_open_noauth(domain->conn.cli, PI_DSSETUP,
1710 &result);
1712 if (cli == NULL) {
1713 DEBUG(5, ("set_dc_type_and_flags_connect: Could not bind to "
1714 "PI_DSSETUP on domain %s: (%s)\n",
1715 domain->name, nt_errstr(result)));
1717 /* if this is just a non-AD domain we need to continue
1718 * identifying so that we can in the end return with
1719 * domain->initialized = True - gd */
1721 goto no_dssetup;
1724 result = rpccli_dssetup_DsRoleGetPrimaryDomainInformation(cli, mem_ctx,
1725 DS_ROLE_BASIC_INFORMATION,
1726 &info,
1727 &werr);
1728 cli_rpc_pipe_close(cli);
1730 if (!NT_STATUS_IS_OK(result)) {
1731 DEBUG(5, ("set_dc_type_and_flags_connect: rpccli_ds_getprimarydominfo "
1732 "on domain %s failed: (%s)\n",
1733 domain->name, nt_errstr(result)));
1735 /* older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for
1736 * every opcode on the DSSETUP pipe, continue with
1737 * no_dssetup mode here as well to get domain->initialized
1738 * set - gd */
1740 if (NT_STATUS_V(result) == DCERPC_FAULT_OP_RNG_ERROR) {
1741 goto no_dssetup;
1744 TALLOC_FREE(mem_ctx);
1745 return;
1748 if ((info.basic.flags & DS_ROLE_PRIMARY_DS_RUNNING) &&
1749 !(info.basic.flags & DS_ROLE_PRIMARY_DS_MIXED_MODE)) {
1750 domain->native_mode = True;
1751 } else {
1752 domain->native_mode = False;
1755 no_dssetup:
1756 cli = cli_rpc_pipe_open_noauth(domain->conn.cli, PI_LSARPC, &result);
1758 if (cli == NULL) {
1759 DEBUG(5, ("set_dc_type_and_flags_connect: Could not bind to "
1760 "PI_LSARPC on domain %s: (%s)\n",
1761 domain->name, nt_errstr(result)));
1762 cli_rpc_pipe_close(cli);
1763 TALLOC_FREE(mem_ctx);
1764 return;
1767 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1768 SEC_RIGHTS_MAXIMUM_ALLOWED, &pol);
1770 if (NT_STATUS_IS_OK(result)) {
1771 /* This particular query is exactly what Win2k clients use
1772 to determine that the DC is active directory */
1773 result = rpccli_lsa_QueryInfoPolicy2(cli, mem_ctx,
1774 &pol,
1775 LSA_POLICY_INFO_DNS,
1776 &lsa_info);
1779 if (NT_STATUS_IS_OK(result)) {
1780 domain->active_directory = True;
1782 if (lsa_info->dns.name.string) {
1783 fstrcpy(domain->name, lsa_info->dns.name.string);
1786 if (lsa_info->dns.dns_domain.string) {
1787 fstrcpy(domain->alt_name,
1788 lsa_info->dns.dns_domain.string);
1791 /* See if we can set some domain trust flags about
1792 ourself */
1794 if (lsa_info->dns.dns_forest.string) {
1795 fstrcpy(domain->forest_name,
1796 lsa_info->dns.dns_forest.string);
1798 if (strequal(domain->forest_name, domain->alt_name)) {
1799 domain->domain_flags |= NETR_TRUST_FLAG_TREEROOT;
1803 if (lsa_info->dns.sid) {
1804 sid_copy(&domain->sid, lsa_info->dns.sid);
1806 } else {
1807 domain->active_directory = False;
1809 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
1810 SEC_RIGHTS_MAXIMUM_ALLOWED,
1811 &pol);
1813 if (!NT_STATUS_IS_OK(result)) {
1814 goto done;
1817 result = rpccli_lsa_QueryInfoPolicy(cli, mem_ctx,
1818 &pol,
1819 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
1820 &lsa_info);
1822 if (NT_STATUS_IS_OK(result)) {
1824 if (lsa_info->account_domain.name.string) {
1825 fstrcpy(domain->name,
1826 lsa_info->account_domain.name.string);
1829 if (lsa_info->account_domain.sid) {
1830 sid_copy(&domain->sid, lsa_info->account_domain.sid);
1834 done:
1836 DEBUG(5, ("set_dc_type_and_flags_connect: domain %s is %sin native mode.\n",
1837 domain->name, domain->native_mode ? "" : "NOT "));
1839 DEBUG(5,("set_dc_type_and_flags_connect: domain %s is %srunning active directory.\n",
1840 domain->name, domain->active_directory ? "" : "NOT "));
1842 cli_rpc_pipe_close(cli);
1844 TALLOC_FREE(mem_ctx);
1846 domain->initialized = True;
1849 /**********************************************************************
1850 Set the domain_flags (trust attributes, domain operating modes, etc...
1851 ***********************************************************************/
1853 static void set_dc_type_and_flags( struct winbindd_domain *domain )
1855 /* we always have to contact our primary domain */
1857 if ( domain->primary ) {
1858 DEBUG(10,("set_dc_type_and_flags: setting up flags for "
1859 "primary domain\n"));
1860 set_dc_type_and_flags_connect( domain );
1861 return;
1864 /* Use our DC to get the information if possible */
1866 if ( !set_dc_type_and_flags_trustinfo( domain ) ) {
1867 /* Otherwise, fallback to contacting the
1868 domain directly */
1869 set_dc_type_and_flags_connect( domain );
1872 return;
1877 /**********************************************************************
1878 ***********************************************************************/
1880 static bool cm_get_schannel_dcinfo(struct winbindd_domain *domain,
1881 struct dcinfo **ppdc)
1883 NTSTATUS result;
1884 struct rpc_pipe_client *netlogon_pipe;
1886 if (lp_client_schannel() == False) {
1887 return False;
1890 result = cm_connect_netlogon(domain, &netlogon_pipe);
1891 if (!NT_STATUS_IS_OK(result)) {
1892 return False;
1895 /* Return a pointer to the struct dcinfo from the
1896 netlogon pipe. */
1898 *ppdc = domain->conn.netlogon_pipe->dc;
1899 return True;
1902 NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
1903 struct rpc_pipe_client **cli, POLICY_HND *sam_handle)
1905 struct winbindd_cm_conn *conn;
1906 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1907 fstring conn_pwd;
1908 struct dcinfo *p_dcinfo;
1909 char *machine_password = NULL;
1910 char *machine_account = NULL;
1911 char *domain_name = NULL;
1913 result = init_dc_connection(domain);
1914 if (!NT_STATUS_IS_OK(result)) {
1915 return result;
1918 conn = &domain->conn;
1920 if (conn->samr_pipe != NULL) {
1921 goto done;
1925 * No SAMR pipe yet. Attempt to get an NTLMSSP SPNEGO authenticated
1926 * sign and sealed pipe using the machine account password by
1927 * preference. If we can't - try schannel, if that fails, try
1928 * anonymous.
1931 pwd_get_cleartext(&conn->cli->pwd, conn_pwd);
1932 if ((conn->cli->user_name[0] == '\0') ||
1933 (conn->cli->domain[0] == '\0') ||
1934 (conn_pwd[0] == '\0'))
1936 result = get_trust_creds(domain, &machine_password,
1937 &machine_account, NULL);
1938 if (!NT_STATUS_IS_OK(result)) {
1939 DEBUG(10, ("cm_connect_sam: No no user available for "
1940 "domain %s, trying schannel\n", conn->cli->domain));
1941 goto schannel;
1943 domain_name = domain->name;
1944 } else {
1945 machine_password = SMB_STRDUP(conn_pwd);
1946 machine_account = SMB_STRDUP(conn->cli->user_name);
1947 domain_name = conn->cli->domain;
1950 if (!machine_password || !machine_account) {
1951 result = NT_STATUS_NO_MEMORY;
1952 goto done;
1955 /* We have an authenticated connection. Use a NTLMSSP SPNEGO
1956 authenticated SAMR pipe with sign & seal. */
1957 conn->samr_pipe =
1958 cli_rpc_pipe_open_spnego_ntlmssp(conn->cli, PI_SAMR,
1959 PIPE_AUTH_LEVEL_PRIVACY,
1960 domain_name,
1961 machine_account,
1962 machine_password, &result);
1964 if (conn->samr_pipe == NULL) {
1965 DEBUG(10,("cm_connect_sam: failed to connect to SAMR "
1966 "pipe for domain %s using NTLMSSP "
1967 "authenticated pipe: user %s\\%s. Error was "
1968 "%s\n", domain->name, domain_name,
1969 machine_account, nt_errstr(result)));
1970 goto schannel;
1973 DEBUG(10,("cm_connect_sam: connected to SAMR pipe for "
1974 "domain %s using NTLMSSP authenticated "
1975 "pipe: user %s\\%s\n", domain->name,
1976 domain_name, machine_account));
1978 result = rpccli_samr_Connect2(conn->samr_pipe, mem_ctx,
1979 conn->samr_pipe->cli->desthost,
1980 SEC_RIGHTS_MAXIMUM_ALLOWED,
1981 &conn->sam_connect_handle);
1982 if (NT_STATUS_IS_OK(result)) {
1983 goto open_domain;
1985 DEBUG(10,("cm_connect_sam: ntlmssp-sealed rpccli_samr_Connect2 "
1986 "failed for domain %s, error was %s. Trying schannel\n",
1987 domain->name, nt_errstr(result) ));
1988 cli_rpc_pipe_close(conn->samr_pipe);
1990 schannel:
1992 /* Fall back to schannel if it's a W2K pre-SP1 box. */
1994 if (!cm_get_schannel_dcinfo(domain, &p_dcinfo)) {
1995 /* If this call fails - conn->cli can now be NULL ! */
1996 DEBUG(10, ("cm_connect_sam: Could not get schannel auth info "
1997 "for domain %s, trying anon\n", domain->name));
1998 goto anonymous;
2000 conn->samr_pipe = cli_rpc_pipe_open_schannel_with_key
2001 (conn->cli, PI_SAMR, PIPE_AUTH_LEVEL_PRIVACY,
2002 domain->name, p_dcinfo, &result);
2004 if (conn->samr_pipe == NULL) {
2005 DEBUG(10,("cm_connect_sam: failed to connect to SAMR pipe for "
2006 "domain %s using schannel. Error was %s\n",
2007 domain->name, nt_errstr(result) ));
2008 goto anonymous;
2010 DEBUG(10,("cm_connect_sam: connected to SAMR pipe for domain %s using "
2011 "schannel.\n", domain->name ));
2013 result = rpccli_samr_Connect2(conn->samr_pipe, mem_ctx,
2014 conn->samr_pipe->cli->desthost,
2015 SEC_RIGHTS_MAXIMUM_ALLOWED,
2016 &conn->sam_connect_handle);
2017 if (NT_STATUS_IS_OK(result)) {
2018 goto open_domain;
2020 DEBUG(10,("cm_connect_sam: schannel-sealed rpccli_samr_Connect2 failed "
2021 "for domain %s, error was %s. Trying anonymous\n",
2022 domain->name, nt_errstr(result) ));
2023 cli_rpc_pipe_close(conn->samr_pipe);
2025 anonymous:
2027 /* Finally fall back to anonymous. */
2028 conn->samr_pipe = cli_rpc_pipe_open_noauth(conn->cli, PI_SAMR,
2029 &result);
2031 if (conn->samr_pipe == NULL) {
2032 result = NT_STATUS_PIPE_NOT_AVAILABLE;
2033 goto done;
2036 result = rpccli_samr_Connect2(conn->samr_pipe, mem_ctx,
2037 conn->samr_pipe->cli->desthost,
2038 SEC_RIGHTS_MAXIMUM_ALLOWED,
2039 &conn->sam_connect_handle);
2040 if (!NT_STATUS_IS_OK(result)) {
2041 DEBUG(10,("cm_connect_sam: rpccli_samr_Connect2 failed "
2042 "for domain %s Error was %s\n",
2043 domain->name, nt_errstr(result) ));
2044 goto done;
2047 open_domain:
2048 result = rpccli_samr_OpenDomain(conn->samr_pipe,
2049 mem_ctx,
2050 &conn->sam_connect_handle,
2051 SEC_RIGHTS_MAXIMUM_ALLOWED,
2052 &domain->sid,
2053 &conn->sam_domain_handle);
2055 done:
2057 if (!NT_STATUS_IS_OK(result)) {
2058 invalidate_cm_connection(conn);
2059 return result;
2062 *cli = conn->samr_pipe;
2063 *sam_handle = conn->sam_domain_handle;
2064 SAFE_FREE(machine_password);
2065 SAFE_FREE(machine_account);
2066 return result;
2069 NTSTATUS cm_connect_lsa(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
2070 struct rpc_pipe_client **cli, POLICY_HND *lsa_policy)
2072 struct winbindd_cm_conn *conn;
2073 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2074 fstring conn_pwd;
2075 struct dcinfo *p_dcinfo;
2077 result = init_dc_connection(domain);
2078 if (!NT_STATUS_IS_OK(result))
2079 return result;
2081 conn = &domain->conn;
2083 if (conn->lsa_pipe != NULL) {
2084 goto done;
2087 pwd_get_cleartext(&conn->cli->pwd, conn_pwd);
2088 if ((conn->cli->user_name[0] == '\0') ||
2089 (conn->cli->domain[0] == '\0') ||
2090 (conn_pwd[0] == '\0')) {
2091 DEBUG(10, ("cm_connect_lsa: No no user available for "
2092 "domain %s, trying schannel\n", conn->cli->domain));
2093 goto schannel;
2096 /* We have an authenticated connection. Use a NTLMSSP SPNEGO
2097 * authenticated LSA pipe with sign & seal. */
2098 conn->lsa_pipe = cli_rpc_pipe_open_spnego_ntlmssp
2099 (conn->cli, PI_LSARPC, PIPE_AUTH_LEVEL_PRIVACY,
2100 conn->cli->domain, conn->cli->user_name, conn_pwd, &result);
2102 if (conn->lsa_pipe == NULL) {
2103 DEBUG(10,("cm_connect_lsa: failed to connect to LSA pipe for "
2104 "domain %s using NTLMSSP authenticated pipe: user "
2105 "%s\\%s. Error was %s. Trying schannel.\n",
2106 domain->name, conn->cli->domain,
2107 conn->cli->user_name, nt_errstr(result)));
2108 goto schannel;
2111 DEBUG(10,("cm_connect_lsa: connected to LSA pipe for domain %s using "
2112 "NTLMSSP authenticated pipe: user %s\\%s\n",
2113 domain->name, conn->cli->domain, conn->cli->user_name ));
2115 result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
2116 SEC_RIGHTS_MAXIMUM_ALLOWED,
2117 &conn->lsa_policy);
2118 if (NT_STATUS_IS_OK(result)) {
2119 goto done;
2122 DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
2123 "schannel\n"));
2125 cli_rpc_pipe_close(conn->lsa_pipe);
2127 schannel:
2129 /* Fall back to schannel if it's a W2K pre-SP1 box. */
2131 if (!cm_get_schannel_dcinfo(domain, &p_dcinfo)) {
2132 /* If this call fails - conn->cli can now be NULL ! */
2133 DEBUG(10, ("cm_connect_lsa: Could not get schannel auth info "
2134 "for domain %s, trying anon\n", domain->name));
2135 goto anonymous;
2137 conn->lsa_pipe = cli_rpc_pipe_open_schannel_with_key
2138 (conn->cli, PI_LSARPC, PIPE_AUTH_LEVEL_PRIVACY,
2139 domain->name, p_dcinfo, &result);
2141 if (conn->lsa_pipe == NULL) {
2142 DEBUG(10,("cm_connect_lsa: failed to connect to LSA pipe for "
2143 "domain %s using schannel. Error was %s\n",
2144 domain->name, nt_errstr(result) ));
2145 goto anonymous;
2147 DEBUG(10,("cm_connect_lsa: connected to LSA pipe for domain %s using "
2148 "schannel.\n", domain->name ));
2150 result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
2151 SEC_RIGHTS_MAXIMUM_ALLOWED,
2152 &conn->lsa_policy);
2153 if (NT_STATUS_IS_OK(result)) {
2154 goto done;
2157 DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
2158 "anonymous\n"));
2160 cli_rpc_pipe_close(conn->lsa_pipe);
2162 anonymous:
2164 conn->lsa_pipe = cli_rpc_pipe_open_noauth(conn->cli, PI_LSARPC,
2165 &result);
2166 if (conn->lsa_pipe == NULL) {
2167 result = NT_STATUS_PIPE_NOT_AVAILABLE;
2168 goto done;
2171 result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
2172 SEC_RIGHTS_MAXIMUM_ALLOWED,
2173 &conn->lsa_policy);
2174 done:
2175 if (!NT_STATUS_IS_OK(result)) {
2176 invalidate_cm_connection(conn);
2177 return result;
2180 *cli = conn->lsa_pipe;
2181 *lsa_policy = conn->lsa_policy;
2182 return result;
2185 /****************************************************************************
2186 Open the netlogon pipe to this DC. Use schannel if specified in client conf.
2187 session key stored in conn->netlogon_pipe->dc->sess_key.
2188 ****************************************************************************/
2190 NTSTATUS cm_connect_netlogon(struct winbindd_domain *domain,
2191 struct rpc_pipe_client **cli)
2193 struct winbindd_cm_conn *conn;
2194 NTSTATUS result;
2196 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
2197 uint8 mach_pwd[16];
2198 uint32 sec_chan_type;
2199 const char *account_name;
2200 struct rpc_pipe_client *netlogon_pipe = NULL;
2202 *cli = NULL;
2204 result = init_dc_connection(domain);
2205 if (!NT_STATUS_IS_OK(result)) {
2206 return result;
2209 conn = &domain->conn;
2211 if (conn->netlogon_pipe != NULL) {
2212 *cli = conn->netlogon_pipe;
2213 return NT_STATUS_OK;
2216 netlogon_pipe = cli_rpc_pipe_open_noauth(conn->cli, PI_NETLOGON,
2217 &result);
2218 if (netlogon_pipe == NULL) {
2219 return result;
2222 if ((!IS_DC) && (!domain->primary)) {
2223 /* Clear the schannel request bit and drop down */
2224 neg_flags &= ~NETLOGON_NEG_SCHANNEL;
2225 goto no_schannel;
2228 if (lp_client_schannel() != False) {
2229 neg_flags |= NETLOGON_NEG_SCHANNEL;
2232 if (!get_trust_pw_hash(domain->name, mach_pwd, &account_name,
2233 &sec_chan_type))
2235 cli_rpc_pipe_close(netlogon_pipe);
2236 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2239 result = rpccli_netlogon_setup_creds(
2240 netlogon_pipe,
2241 domain->dcname, /* server name. */
2242 domain->name, /* domain name */
2243 global_myname(), /* client name */
2244 account_name, /* machine account */
2245 mach_pwd, /* machine password */
2246 sec_chan_type, /* from get_trust_pw */
2247 &neg_flags);
2249 if (!NT_STATUS_IS_OK(result)) {
2250 cli_rpc_pipe_close(netlogon_pipe);
2251 return result;
2254 if ((lp_client_schannel() == True) &&
2255 ((neg_flags & NETLOGON_NEG_SCHANNEL) == 0)) {
2256 DEBUG(3, ("Server did not offer schannel\n"));
2257 cli_rpc_pipe_close(netlogon_pipe);
2258 return NT_STATUS_ACCESS_DENIED;
2261 no_schannel:
2262 if ((lp_client_schannel() == False) ||
2263 ((neg_flags & NETLOGON_NEG_SCHANNEL) == 0)) {
2265 * NetSamLogonEx only works for schannel
2267 domain->can_do_samlogon_ex = False;
2269 /* We're done - just keep the existing connection to NETLOGON
2270 * open */
2271 conn->netlogon_pipe = netlogon_pipe;
2272 *cli = conn->netlogon_pipe;
2273 return NT_STATUS_OK;
2276 /* Using the credentials from the first pipe, open a signed and sealed
2277 second netlogon pipe. The session key is stored in the schannel
2278 part of the new pipe auth struct.
2281 conn->netlogon_pipe =
2282 cli_rpc_pipe_open_schannel_with_key(conn->cli,
2283 PI_NETLOGON,
2284 PIPE_AUTH_LEVEL_PRIVACY,
2285 domain->name,
2286 netlogon_pipe->dc,
2287 &result);
2289 /* We can now close the initial netlogon pipe. */
2290 cli_rpc_pipe_close(netlogon_pipe);
2292 if (conn->netlogon_pipe == NULL) {
2293 DEBUG(3, ("Could not open schannel'ed NETLOGON pipe. Error "
2294 "was %s\n", nt_errstr(result)));
2296 /* make sure we return something besides OK */
2297 return !NT_STATUS_IS_OK(result) ? result : NT_STATUS_PIPE_NOT_AVAILABLE;
2301 * Try NetSamLogonEx for AD domains
2303 domain->can_do_samlogon_ex = domain->active_directory;
2305 *cli = conn->netlogon_pipe;
2306 return NT_STATUS_OK;