Refactoring: Change calling conventions for cli_rpc_pipe_open_noauth
[Samba/gbeck.git] / source3 / winbindd / winbindd_cm.c
blobb7e2f086fc737a6662909194d1502b301b27917e
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 = rpccli_set_timeout(netlogon_pipe, 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 rpccli_set_timeout(netlogon_pipe, 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 /* For now assume our machine account only exists in our domain */
711 if (machine_krb5_principal != NULL)
713 struct winbindd_domain *our_domain = find_our_domain();
715 if (!our_domain) {
716 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
719 if (asprintf(machine_krb5_principal, "%s$@%s",
720 account_name, our_domain->alt_name) == -1)
722 return NT_STATUS_NO_MEMORY;
725 strupper_m(*machine_krb5_principal);
728 return NT_STATUS_OK;
731 /************************************************************************
732 Given a fd with a just-connected TCP connection to a DC, open a connection
733 to the pipe.
734 ************************************************************************/
736 static NTSTATUS cm_prepare_connection(const struct winbindd_domain *domain,
737 const int sockfd,
738 const char *controller,
739 struct cli_state **cli,
740 bool *retry)
742 char *machine_password = NULL;
743 char *machine_krb5_principal = NULL;
744 char *machine_account = NULL;
745 char *ipc_username = NULL;
746 char *ipc_domain = NULL;
747 char *ipc_password = NULL;
749 struct named_mutex *mutex;
751 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
753 struct sockaddr peeraddr;
754 socklen_t peeraddr_len;
756 struct sockaddr_in *peeraddr_in = (struct sockaddr_in *)&peeraddr;
758 DEBUG(10,("cm_prepare_connection: connecting to DC %s for domain %s\n",
759 controller, domain->name ));
761 *retry = True;
763 mutex = grab_named_mutex(talloc_tos(), controller,
764 WINBIND_SERVER_MUTEX_WAIT_TIME);
765 if (mutex == NULL) {
766 DEBUG(0,("cm_prepare_connection: mutex grab failed for %s\n",
767 controller));
768 result = NT_STATUS_POSSIBLE_DEADLOCK;
769 goto done;
772 if ((*cli = cli_initialise()) == NULL) {
773 DEBUG(1, ("Could not cli_initialize\n"));
774 result = NT_STATUS_NO_MEMORY;
775 goto done;
778 (*cli)->timeout = 10000; /* 10 seconds */
779 (*cli)->fd = sockfd;
780 fstrcpy((*cli)->desthost, controller);
781 (*cli)->use_kerberos = True;
783 peeraddr_len = sizeof(peeraddr);
785 if ((getpeername((*cli)->fd, &peeraddr, &peeraddr_len) != 0) ||
786 (peeraddr_len != sizeof(struct sockaddr_in)) ||
787 (peeraddr_in->sin_family != PF_INET))
789 DEBUG(0,("cm_prepare_connection: %s\n", strerror(errno)));
790 result = NT_STATUS_UNSUCCESSFUL;
791 goto done;
794 if (ntohs(peeraddr_in->sin_port) == 139) {
795 struct nmb_name calling;
796 struct nmb_name called;
798 make_nmb_name(&calling, global_myname(), 0x0);
799 make_nmb_name(&called, "*SMBSERVER", 0x20);
801 if (!cli_session_request(*cli, &calling, &called)) {
802 DEBUG(8, ("cli_session_request failed for %s\n",
803 controller));
804 result = NT_STATUS_UNSUCCESSFUL;
805 goto done;
809 cli_setup_signing_state(*cli, Undefined);
811 if (!cli_negprot(*cli)) {
812 DEBUG(1, ("cli_negprot failed\n"));
813 result = NT_STATUS_UNSUCCESSFUL;
814 goto done;
817 if (!is_dc_trusted_domain_situation(domain->name) &&
818 (*cli)->protocol >= PROTOCOL_NT1 &&
819 (*cli)->capabilities & CAP_EXTENDED_SECURITY)
821 ADS_STATUS ads_status;
823 result = get_trust_creds(domain, &machine_password,
824 &machine_account,
825 &machine_krb5_principal);
826 if (!NT_STATUS_IS_OK(result)) {
827 goto anon_fallback;
830 if (lp_security() == SEC_ADS) {
832 /* Try a krb5 session */
834 (*cli)->use_kerberos = True;
835 DEBUG(5, ("connecting to %s from %s with kerberos principal "
836 "[%s] and realm [%s]\n", controller, global_myname(),
837 machine_krb5_principal, domain->alt_name));
839 winbindd_set_locator_kdc_envs(domain);
841 ads_status = cli_session_setup_spnego(*cli,
842 machine_krb5_principal,
843 machine_password,
844 lp_workgroup(),
845 domain->name);
847 if (!ADS_ERR_OK(ads_status)) {
848 DEBUG(4,("failed kerberos session setup with %s\n",
849 ads_errstr(ads_status)));
852 result = ads_ntstatus(ads_status);
853 if (NT_STATUS_IS_OK(result)) {
854 /* Ensure creds are stored for NTLMSSP authenticated pipe access. */
855 cli_init_creds(*cli, machine_account, domain->name, machine_password);
856 goto session_setup_done;
860 /* Fall back to non-kerberos session setup using NTLMSSP SPNEGO with the machine account. */
861 (*cli)->use_kerberos = False;
863 DEBUG(5, ("connecting to %s from %s with username "
864 "[%s]\\[%s]\n", controller, global_myname(),
865 lp_workgroup(), machine_account));
867 ads_status = cli_session_setup_spnego(*cli,
868 machine_account,
869 machine_password,
870 lp_workgroup(),
871 NULL);
872 if (!ADS_ERR_OK(ads_status)) {
873 DEBUG(4, ("authenticated session setup failed with %s\n",
874 ads_errstr(ads_status)));
877 result = ads_ntstatus(ads_status);
878 if (NT_STATUS_IS_OK(result)) {
879 /* Ensure creds are stored for NTLMSSP authenticated pipe access. */
880 cli_init_creds(*cli, machine_account, domain->name, machine_password);
881 goto session_setup_done;
885 /* Fall back to non-kerberos session setup with auth_user */
887 (*cli)->use_kerberos = False;
889 cm_get_ipc_userpass(&ipc_username, &ipc_domain, &ipc_password);
891 if ((((*cli)->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) != 0) &&
892 (strlen(ipc_username) > 0)) {
894 /* Only try authenticated if we have a username */
896 DEBUG(5, ("connecting to %s from %s with username "
897 "[%s]\\[%s]\n", controller, global_myname(),
898 ipc_domain, ipc_username));
900 if (NT_STATUS_IS_OK(cli_session_setup(
901 *cli, ipc_username,
902 ipc_password, strlen(ipc_password)+1,
903 ipc_password, strlen(ipc_password)+1,
904 ipc_domain))) {
905 /* Successful logon with given username. */
906 cli_init_creds(*cli, ipc_username, ipc_domain, ipc_password);
907 goto session_setup_done;
908 } else {
909 DEBUG(4, ("authenticated session setup with user %s\\%s failed.\n",
910 ipc_domain, ipc_username ));
914 anon_fallback:
916 /* Fall back to anonymous connection, this might fail later */
918 if (NT_STATUS_IS_OK(cli_session_setup(*cli, "", NULL, 0,
919 NULL, 0, ""))) {
920 DEBUG(5, ("Connected anonymously\n"));
921 cli_init_creds(*cli, "", "", "");
922 goto session_setup_done;
925 result = cli_nt_error(*cli);
927 if (NT_STATUS_IS_OK(result))
928 result = NT_STATUS_UNSUCCESSFUL;
930 /* We can't session setup */
932 goto done;
934 session_setup_done:
936 /* cache the server name for later connections */
938 saf_store( domain->name, (*cli)->desthost );
939 if (domain->alt_name && (*cli)->use_kerberos) {
940 saf_store( domain->alt_name, (*cli)->desthost );
943 winbindd_set_locator_kdc_envs(domain);
945 if (!cli_send_tconX(*cli, "IPC$", "IPC", "", 0)) {
947 result = cli_nt_error(*cli);
949 DEBUG(1,("failed tcon_X with %s\n", nt_errstr(result)));
951 if (NT_STATUS_IS_OK(result))
952 result = NT_STATUS_UNSUCCESSFUL;
954 goto done;
957 TALLOC_FREE(mutex);
958 *retry = False;
960 /* set the domain if empty; needed for schannel connections */
961 if ( !*(*cli)->domain ) {
962 fstrcpy( (*cli)->domain, domain->name );
965 result = NT_STATUS_OK;
967 done:
968 TALLOC_FREE(mutex);
969 SAFE_FREE(machine_account);
970 SAFE_FREE(machine_password);
971 SAFE_FREE(machine_krb5_principal);
972 SAFE_FREE(ipc_username);
973 SAFE_FREE(ipc_domain);
974 SAFE_FREE(ipc_password);
976 if (!NT_STATUS_IS_OK(result)) {
977 winbind_add_failed_connection_entry(domain, controller, result);
978 if ((*cli) != NULL) {
979 cli_shutdown(*cli);
980 *cli = NULL;
984 return result;
987 static bool add_one_dc_unique(TALLOC_CTX *mem_ctx, const char *domain_name,
988 const char *dcname, struct sockaddr_storage *pss,
989 struct dc_name_ip **dcs, int *num)
991 if (!NT_STATUS_IS_OK(check_negative_conn_cache(domain_name, dcname))) {
992 DEBUG(10, ("DC %s was in the negative conn cache\n", dcname));
993 return False;
996 *dcs = TALLOC_REALLOC_ARRAY(mem_ctx, *dcs, struct dc_name_ip, (*num)+1);
998 if (*dcs == NULL)
999 return False;
1001 fstrcpy((*dcs)[*num].name, dcname);
1002 (*dcs)[*num].ss = *pss;
1003 *num += 1;
1004 return True;
1007 static bool add_sockaddr_to_array(TALLOC_CTX *mem_ctx,
1008 struct sockaddr_storage *pss, uint16 port,
1009 struct sockaddr_storage **addrs, int *num)
1011 *addrs = TALLOC_REALLOC_ARRAY(mem_ctx, *addrs, struct sockaddr_storage, (*num)+1);
1013 if (*addrs == NULL) {
1014 *num = 0;
1015 return False;
1018 (*addrs)[*num] = *pss;
1019 set_sockaddr_port(&(*addrs)[*num], port);
1021 *num += 1;
1022 return True;
1025 /*******************************************************************
1026 convert an ip to a name
1027 *******************************************************************/
1029 static bool dcip_to_name(TALLOC_CTX *mem_ctx,
1030 const struct winbindd_domain *domain,
1031 struct sockaddr_storage *pss,
1032 fstring name )
1034 struct ip_service ip_list;
1035 uint32_t nt_version = NETLOGON_VERSION_1;
1037 ip_list.ss = *pss;
1038 ip_list.port = 0;
1040 #ifdef WITH_ADS
1041 /* For active directory servers, try to get the ldap server name.
1042 None of these failures should be considered critical for now */
1044 if (lp_security() == SEC_ADS) {
1045 ADS_STRUCT *ads;
1046 ADS_STATUS ads_status;
1047 char addr[INET6_ADDRSTRLEN];
1049 print_sockaddr(addr, sizeof(addr), pss);
1051 ads = ads_init(domain->alt_name, domain->name, addr);
1052 ads->auth.flags |= ADS_AUTH_NO_BIND;
1054 ads_status = ads_connect(ads);
1055 if (ADS_ERR_OK(ads_status)) {
1056 /* We got a cldap packet. */
1057 fstrcpy(name, ads->config.ldap_server_name);
1058 namecache_store(name, 0x20, 1, &ip_list);
1060 DEBUG(10,("dcip_to_name: flags = 0x%x\n", (unsigned int)ads->config.flags));
1062 if (domain->primary && (ads->config.flags & NBT_SERVER_KDC)) {
1063 if (ads_closest_dc(ads)) {
1064 char *sitename = sitename_fetch(ads->config.realm);
1066 /* We're going to use this KDC for this realm/domain.
1067 If we are using sites, then force the krb5 libs
1068 to use this KDC. */
1070 create_local_private_krb5_conf_for_domain(domain->alt_name,
1071 domain->name,
1072 sitename,
1073 pss);
1075 SAFE_FREE(sitename);
1076 } else {
1077 /* use an off site KDC */
1078 create_local_private_krb5_conf_for_domain(domain->alt_name,
1079 domain->name,
1080 NULL,
1081 pss);
1083 winbindd_set_locator_kdc_envs(domain);
1085 /* Ensure we contact this DC also. */
1086 saf_store( domain->name, name);
1087 saf_store( domain->alt_name, name);
1090 ads_destroy( &ads );
1091 return True;
1094 ads_destroy( &ads );
1096 #endif
1098 /* try GETDC requests next */
1100 if (send_getdc_request(mem_ctx, winbind_messaging_context(),
1101 pss, domain->name, &domain->sid,
1102 nt_version)) {
1103 const char *dc_name = NULL;
1104 int i;
1105 smb_msleep(100);
1106 for (i=0; i<5; i++) {
1107 if (receive_getdc_response(mem_ctx, pss, domain->name,
1108 &nt_version,
1109 &dc_name, NULL)) {
1110 fstrcpy(name, dc_name);
1111 namecache_store(name, 0x20, 1, &ip_list);
1112 return True;
1114 smb_msleep(500);
1118 /* try node status request */
1120 if ( name_status_find(domain->name, 0x1c, 0x20, pss, name) ) {
1121 namecache_store(name, 0x20, 1, &ip_list);
1122 return True;
1124 return False;
1127 /*******************************************************************
1128 Retreive a list of IP address for domain controllers. Fill in
1129 the dcs[] with results.
1130 *******************************************************************/
1132 static bool get_dcs(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
1133 struct dc_name_ip **dcs, int *num_dcs)
1135 fstring dcname;
1136 struct sockaddr_storage ss;
1137 struct ip_service *ip_list = NULL;
1138 int iplist_size = 0;
1139 int i;
1140 bool is_our_domain;
1141 enum security_types sec = (enum security_types)lp_security();
1143 is_our_domain = strequal(domain->name, lp_workgroup());
1145 if ( !is_our_domain
1146 && get_dc_name_via_netlogon(domain, dcname, &ss)
1147 && add_one_dc_unique(mem_ctx, domain->name, dcname, &ss, dcs, num_dcs) )
1149 char addr[INET6_ADDRSTRLEN];
1150 print_sockaddr(addr, sizeof(addr), &ss);
1151 DEBUG(10, ("Retrieved DC %s at %s via netlogon\n",
1152 dcname, addr));
1153 return True;
1156 if (sec == SEC_ADS) {
1157 char *sitename = NULL;
1159 /* We need to make sure we know the local site before
1160 doing any DNS queries, as this will restrict the
1161 get_sorted_dc_list() call below to only fetching
1162 DNS records for the correct site. */
1164 /* Find any DC to get the site record.
1165 We deliberately don't care about the
1166 return here. */
1168 get_dc_name(domain->name, domain->alt_name, dcname, &ss);
1170 sitename = sitename_fetch(domain->alt_name);
1171 if (sitename) {
1173 /* Do the site-specific AD dns lookup first. */
1174 get_sorted_dc_list(domain->alt_name, sitename, &ip_list, &iplist_size, True);
1176 for ( i=0; i<iplist_size; i++ ) {
1177 char addr[INET6_ADDRSTRLEN];
1178 print_sockaddr(addr, sizeof(addr),
1179 &ip_list[i].ss);
1180 add_one_dc_unique(mem_ctx,
1181 domain->name,
1182 addr,
1183 &ip_list[i].ss,
1184 dcs,
1185 num_dcs);
1188 SAFE_FREE(ip_list);
1189 SAFE_FREE(sitename);
1190 iplist_size = 0;
1193 /* Now we add DCs from the main AD dns lookup. */
1194 get_sorted_dc_list(domain->alt_name, NULL, &ip_list, &iplist_size, True);
1196 for ( i=0; i<iplist_size; i++ ) {
1197 char addr[INET6_ADDRSTRLEN];
1198 print_sockaddr(addr, sizeof(addr),
1199 &ip_list[i].ss);
1200 add_one_dc_unique(mem_ctx,
1201 domain->name,
1202 addr,
1203 &ip_list[i].ss,
1204 dcs,
1205 num_dcs);
1209 /* try standard netbios queries if no ADS */
1211 if (iplist_size==0) {
1212 get_sorted_dc_list(domain->name, NULL, &ip_list, &iplist_size, False);
1215 /* FIXME!! this is where we should re-insert the GETDC requests --jerry */
1217 /* now add to the dc array. We'll wait until the last minute
1218 to look up the name of the DC. But we fill in the char* for
1219 the ip now in to make the failed connection cache work */
1221 for ( i=0; i<iplist_size; i++ ) {
1222 char addr[INET6_ADDRSTRLEN];
1223 print_sockaddr(addr, sizeof(addr),
1224 &ip_list[i].ss);
1225 add_one_dc_unique(mem_ctx, domain->name, addr,
1226 &ip_list[i].ss, dcs, num_dcs);
1229 SAFE_FREE( ip_list );
1231 return True;
1234 static bool find_new_dc(TALLOC_CTX *mem_ctx,
1235 struct winbindd_domain *domain,
1236 fstring dcname, struct sockaddr_storage *pss, int *fd)
1238 struct dc_name_ip *dcs = NULL;
1239 int num_dcs = 0;
1241 const char **dcnames = NULL;
1242 int num_dcnames = 0;
1244 struct sockaddr_storage *addrs = NULL;
1245 int num_addrs = 0;
1247 int i, fd_index;
1249 *fd = -1;
1251 again:
1252 if (!get_dcs(mem_ctx, domain, &dcs, &num_dcs) || (num_dcs == 0))
1253 return False;
1255 for (i=0; i<num_dcs; i++) {
1257 if (!add_string_to_array(mem_ctx, dcs[i].name,
1258 &dcnames, &num_dcnames)) {
1259 return False;
1261 if (!add_sockaddr_to_array(mem_ctx, &dcs[i].ss, 445,
1262 &addrs, &num_addrs)) {
1263 return False;
1266 if (!add_string_to_array(mem_ctx, dcs[i].name,
1267 &dcnames, &num_dcnames)) {
1268 return False;
1270 if (!add_sockaddr_to_array(mem_ctx, &dcs[i].ss, 139,
1271 &addrs, &num_addrs)) {
1272 return False;
1276 if ((num_dcnames == 0) || (num_dcnames != num_addrs))
1277 return False;
1279 if ((addrs == NULL) || (dcnames == NULL))
1280 return False;
1282 /* 5 second timeout. */
1283 if (!open_any_socket_out(addrs, num_addrs, 5000, &fd_index, fd) ) {
1284 for (i=0; i<num_dcs; i++) {
1285 char ab[INET6_ADDRSTRLEN];
1286 print_sockaddr(ab, sizeof(ab), &dcs[i].ss);
1287 DEBUG(10, ("find_new_dc: open_any_socket_out failed for "
1288 "domain %s address %s. Error was %s\n",
1289 domain->name, ab, strerror(errno) ));
1290 winbind_add_failed_connection_entry(domain,
1291 dcs[i].name, NT_STATUS_UNSUCCESSFUL);
1293 return False;
1296 *pss = addrs[fd_index];
1298 if (*dcnames[fd_index] != '\0' && !is_ipaddress(dcnames[fd_index])) {
1299 /* Ok, we've got a name for the DC */
1300 fstrcpy(dcname, dcnames[fd_index]);
1301 return True;
1304 /* Try to figure out the name */
1305 if (dcip_to_name(mem_ctx, domain, pss, dcname)) {
1306 return True;
1309 /* We can not continue without the DC's name */
1310 winbind_add_failed_connection_entry(domain, dcs[fd_index].name,
1311 NT_STATUS_UNSUCCESSFUL);
1313 /* Throw away all arrays as we're doing this again. */
1314 TALLOC_FREE(dcs);
1315 num_dcs = 0;
1317 TALLOC_FREE(dcnames);
1318 num_dcnames = 0;
1320 TALLOC_FREE(addrs);
1321 num_addrs = 0;
1323 *fd = -1;
1325 goto again;
1328 static NTSTATUS cm_open_connection(struct winbindd_domain *domain,
1329 struct winbindd_cm_conn *new_conn)
1331 TALLOC_CTX *mem_ctx;
1332 NTSTATUS result;
1333 char *saf_servername = saf_fetch( domain->name );
1334 int retries;
1336 if ((mem_ctx = talloc_init("cm_open_connection")) == NULL) {
1337 SAFE_FREE(saf_servername);
1338 set_domain_offline(domain);
1339 return NT_STATUS_NO_MEMORY;
1342 /* we have to check the server affinity cache here since
1343 later we selecte a DC based on response time and not preference */
1345 /* Check the negative connection cache
1346 before talking to it. It going down may have
1347 triggered the reconnection. */
1349 if ( saf_servername && NT_STATUS_IS_OK(check_negative_conn_cache( domain->name, saf_servername))) {
1351 DEBUG(10,("cm_open_connection: saf_servername is '%s' for domain %s\n",
1352 saf_servername, domain->name ));
1354 /* convert an ip address to a name */
1355 if (is_ipaddress( saf_servername ) ) {
1356 fstring saf_name;
1357 struct sockaddr_storage ss;
1359 if (!interpret_string_addr(&ss, saf_servername,
1360 AI_NUMERICHOST)) {
1361 return NT_STATUS_UNSUCCESSFUL;
1363 if (dcip_to_name(mem_ctx, domain, &ss, saf_name )) {
1364 fstrcpy( domain->dcname, saf_name );
1365 } else {
1366 winbind_add_failed_connection_entry(
1367 domain, saf_servername,
1368 NT_STATUS_UNSUCCESSFUL);
1370 } else {
1371 fstrcpy( domain->dcname, saf_servername );
1374 SAFE_FREE( saf_servername );
1377 for (retries = 0; retries < 3; retries++) {
1378 int fd = -1;
1379 bool retry = False;
1381 result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1383 DEBUG(10,("cm_open_connection: dcname is '%s' for domain %s\n",
1384 domain->dcname, domain->name ));
1386 if (*domain->dcname
1387 && NT_STATUS_IS_OK(check_negative_conn_cache( domain->name, domain->dcname))
1388 && (resolve_name(domain->dcname, &domain->dcaddr, 0x20)))
1390 struct sockaddr_storage *addrs = NULL;
1391 int num_addrs = 0;
1392 int dummy = 0;
1394 if (!add_sockaddr_to_array(mem_ctx, &domain->dcaddr, 445, &addrs, &num_addrs)) {
1395 set_domain_offline(domain);
1396 talloc_destroy(mem_ctx);
1397 return NT_STATUS_NO_MEMORY;
1399 if (!add_sockaddr_to_array(mem_ctx, &domain->dcaddr, 139, &addrs, &num_addrs)) {
1400 set_domain_offline(domain);
1401 talloc_destroy(mem_ctx);
1402 return NT_STATUS_NO_MEMORY;
1405 /* 5 second timeout. */
1406 if (!open_any_socket_out(addrs, num_addrs, 5000, &dummy, &fd)) {
1407 fd = -1;
1411 if ((fd == -1)
1412 && !find_new_dc(mem_ctx, domain, domain->dcname, &domain->dcaddr, &fd))
1414 /* This is the one place where we will
1415 set the global winbindd offline state
1416 to true, if a "WINBINDD_OFFLINE" entry
1417 is found in the winbindd cache. */
1418 set_global_winbindd_state_offline();
1419 break;
1422 new_conn->cli = NULL;
1424 result = cm_prepare_connection(domain, fd, domain->dcname,
1425 &new_conn->cli, &retry);
1427 if (!retry)
1428 break;
1431 if (NT_STATUS_IS_OK(result)) {
1433 winbindd_set_locator_kdc_envs(domain);
1435 if (domain->online == False) {
1436 /* We're changing state from offline to online. */
1437 set_global_winbindd_state_online();
1439 set_domain_online(domain);
1440 } else {
1441 /* Ensure we setup the retry handler. */
1442 set_domain_offline(domain);
1445 talloc_destroy(mem_ctx);
1446 return result;
1449 /* Close down all open pipes on a connection. */
1451 void invalidate_cm_connection(struct winbindd_cm_conn *conn)
1453 /* We're closing down a possibly dead
1454 connection. Don't have impossibly long (10s) timeouts. */
1456 if (conn->cli) {
1457 cli_set_timeout(conn->cli, 1000); /* 1 second. */
1460 if (conn->samr_pipe != NULL) {
1461 TALLOC_FREE(conn->samr_pipe);
1462 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1463 if (conn->cli) {
1464 cli_set_timeout(conn->cli, 500);
1468 if (conn->lsa_pipe != NULL) {
1469 TALLOC_FREE(conn->lsa_pipe);
1470 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1471 if (conn->cli) {
1472 cli_set_timeout(conn->cli, 500);
1476 if (conn->netlogon_pipe != NULL) {
1477 TALLOC_FREE(conn->netlogon_pipe);
1478 /* Ok, it must be dead. Drop timeout to 0.5 sec. */
1479 if (conn->cli) {
1480 cli_set_timeout(conn->cli, 500);
1484 if (conn->cli) {
1485 cli_shutdown(conn->cli);
1488 conn->cli = NULL;
1491 void close_conns_after_fork(void)
1493 struct winbindd_domain *domain;
1495 for (domain = domain_list(); domain; domain = domain->next) {
1496 if (domain->conn.cli == NULL)
1497 continue;
1499 if (domain->conn.cli->fd == -1)
1500 continue;
1502 close(domain->conn.cli->fd);
1503 domain->conn.cli->fd = -1;
1507 static bool connection_ok(struct winbindd_domain *domain)
1509 if (domain->conn.cli == NULL) {
1510 DEBUG(8, ("connection_ok: Connection to %s for domain %s has NULL "
1511 "cli!\n", domain->dcname, domain->name));
1512 return False;
1515 if (!domain->conn.cli->initialised) {
1516 DEBUG(3, ("connection_ok: Connection to %s for domain %s was never "
1517 "initialised!\n", domain->dcname, domain->name));
1518 return False;
1521 if (domain->conn.cli->fd == -1) {
1522 DEBUG(3, ("connection_ok: Connection to %s for domain %s has died or was "
1523 "never started (fd == -1)\n",
1524 domain->dcname, domain->name));
1525 return False;
1528 if (domain->online == False) {
1529 DEBUG(3, ("connection_ok: Domain %s is offline\n", domain->name));
1530 return False;
1533 return True;
1536 /* Initialize a new connection up to the RPC BIND.
1537 Bypass online status check so always does network calls. */
1539 static NTSTATUS init_dc_connection_network(struct winbindd_domain *domain)
1541 NTSTATUS result;
1543 /* Internal connections never use the network. */
1544 if (domain->internal) {
1545 domain->initialized = True;
1546 return NT_STATUS_OK;
1549 if (connection_ok(domain)) {
1550 if (!domain->initialized) {
1551 set_dc_type_and_flags(domain);
1553 return NT_STATUS_OK;
1556 invalidate_cm_connection(&domain->conn);
1558 result = cm_open_connection(domain, &domain->conn);
1560 if (NT_STATUS_IS_OK(result) && !domain->initialized) {
1561 set_dc_type_and_flags(domain);
1564 return result;
1567 NTSTATUS init_dc_connection(struct winbindd_domain *domain)
1569 if (domain->initialized && !domain->online) {
1570 /* We check for online status elsewhere. */
1571 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1574 return init_dc_connection_network(domain);
1577 /******************************************************************************
1578 Set the trust flags (direction and forest location) for a domain
1579 ******************************************************************************/
1581 static bool set_dc_type_and_flags_trustinfo( struct winbindd_domain *domain )
1583 struct winbindd_domain *our_domain;
1584 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1585 struct netr_DomainTrustList trusts;
1586 int i;
1587 uint32 flags = (NETR_TRUST_FLAG_IN_FOREST |
1588 NETR_TRUST_FLAG_OUTBOUND |
1589 NETR_TRUST_FLAG_INBOUND);
1590 struct rpc_pipe_client *cli;
1591 TALLOC_CTX *mem_ctx = NULL;
1593 DEBUG(5, ("set_dc_type_and_flags_trustinfo: domain %s\n", domain->name ));
1595 /* Our primary domain doesn't need to worry about trust flags.
1596 Force it to go through the network setup */
1597 if ( domain->primary ) {
1598 return False;
1601 our_domain = find_our_domain();
1603 if ( !connection_ok(our_domain) ) {
1604 DEBUG(3,("set_dc_type_and_flags_trustinfo: No connection to our domain!\n"));
1605 return False;
1608 /* This won't work unless our domain is AD */
1610 if ( !our_domain->active_directory ) {
1611 return False;
1614 /* Use DsEnumerateDomainTrusts to get us the trust direction
1615 and type */
1617 result = cm_connect_netlogon(our_domain, &cli);
1619 if (!NT_STATUS_IS_OK(result)) {
1620 DEBUG(5, ("set_dc_type_and_flags_trustinfo: Could not open "
1621 "a connection to %s for PIPE_NETLOGON (%s)\n",
1622 domain->name, nt_errstr(result)));
1623 return False;
1626 if ( (mem_ctx = talloc_init("set_dc_type_and_flags_trustinfo")) == NULL ) {
1627 DEBUG(0,("set_dc_type_and_flags_trustinfo: talloc_init() failed!\n"));
1628 return False;
1631 result = rpccli_netr_DsrEnumerateDomainTrusts(cli, mem_ctx,
1632 cli->desthost,
1633 flags,
1634 &trusts,
1635 NULL);
1636 if (!NT_STATUS_IS_OK(result)) {
1637 DEBUG(0,("set_dc_type_and_flags_trustinfo: "
1638 "failed to query trusted domain list: %s\n",
1639 nt_errstr(result)));
1640 talloc_destroy(mem_ctx);
1641 return false;
1644 /* Now find the domain name and get the flags */
1646 for ( i=0; i<trusts.count; i++ ) {
1647 if ( strequal( domain->name, trusts.array[i].netbios_name) ) {
1648 domain->domain_flags = trusts.array[i].trust_flags;
1649 domain->domain_type = trusts.array[i].trust_type;
1650 domain->domain_trust_attribs = trusts.array[i].trust_attributes;
1652 if ( domain->domain_type == NETR_TRUST_TYPE_UPLEVEL )
1653 domain->active_directory = True;
1655 /* This flag is only set if the domain is *our*
1656 primary domain and the primary domain is in
1657 native mode */
1659 domain->native_mode = (domain->domain_flags & NETR_TRUST_FLAG_NATIVE);
1661 DEBUG(5, ("set_dc_type_and_flags_trustinfo: domain %s is %sin "
1662 "native mode.\n", domain->name,
1663 domain->native_mode ? "" : "NOT "));
1665 DEBUG(5,("set_dc_type_and_flags_trustinfo: domain %s is %s"
1666 "running active directory.\n", domain->name,
1667 domain->active_directory ? "" : "NOT "));
1670 domain->initialized = True;
1672 if ( !winbindd_can_contact_domain( domain) )
1673 domain->internal = True;
1675 break;
1679 talloc_destroy( mem_ctx );
1681 return domain->initialized;
1684 /******************************************************************************
1685 We can 'sense' certain things about the DC by it's replies to certain
1686 questions.
1688 This tells us if this particular remote server is Active Directory, and if it
1689 is native mode.
1690 ******************************************************************************/
1692 static void set_dc_type_and_flags_connect( struct winbindd_domain *domain )
1694 NTSTATUS result;
1695 WERROR werr;
1696 TALLOC_CTX *mem_ctx = NULL;
1697 struct rpc_pipe_client *cli;
1698 POLICY_HND pol;
1699 union dssetup_DsRoleInfo info;
1700 union lsa_PolicyInformation *lsa_info = NULL;
1702 if (!connection_ok(domain)) {
1703 return;
1706 mem_ctx = talloc_init("set_dc_type_and_flags on domain %s\n",
1707 domain->name);
1708 if (!mem_ctx) {
1709 DEBUG(1, ("set_dc_type_and_flags_connect: talloc_init() failed\n"));
1710 return;
1713 DEBUG(5, ("set_dc_type_and_flags_connect: domain %s\n", domain->name ));
1715 result = cli_rpc_pipe_open_noauth(domain->conn.cli,
1716 &ndr_table_dssetup.syntax_id,
1717 &cli);
1719 if (!NT_STATUS_IS_OK(result)) {
1720 DEBUG(5, ("set_dc_type_and_flags_connect: Could not bind to "
1721 "PI_DSSETUP on domain %s: (%s)\n",
1722 domain->name, nt_errstr(result)));
1724 /* if this is just a non-AD domain we need to continue
1725 * identifying so that we can in the end return with
1726 * domain->initialized = True - gd */
1728 goto no_dssetup;
1731 result = rpccli_dssetup_DsRoleGetPrimaryDomainInformation(cli, mem_ctx,
1732 DS_ROLE_BASIC_INFORMATION,
1733 &info,
1734 &werr);
1735 TALLOC_FREE(cli);
1737 if (!NT_STATUS_IS_OK(result)) {
1738 DEBUG(5, ("set_dc_type_and_flags_connect: rpccli_ds_getprimarydominfo "
1739 "on domain %s failed: (%s)\n",
1740 domain->name, nt_errstr(result)));
1742 /* older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for
1743 * every opcode on the DSSETUP pipe, continue with
1744 * no_dssetup mode here as well to get domain->initialized
1745 * set - gd */
1747 if (NT_STATUS_V(result) == DCERPC_FAULT_OP_RNG_ERROR) {
1748 goto no_dssetup;
1751 TALLOC_FREE(mem_ctx);
1752 return;
1755 if ((info.basic.flags & DS_ROLE_PRIMARY_DS_RUNNING) &&
1756 !(info.basic.flags & DS_ROLE_PRIMARY_DS_MIXED_MODE)) {
1757 domain->native_mode = True;
1758 } else {
1759 domain->native_mode = False;
1762 no_dssetup:
1763 result = cli_rpc_pipe_open_noauth(domain->conn.cli,
1764 &ndr_table_lsarpc.syntax_id, &cli);
1766 if (!NT_STATUS_IS_OK(result)) {
1767 DEBUG(5, ("set_dc_type_and_flags_connect: Could not bind to "
1768 "PI_LSARPC on domain %s: (%s)\n",
1769 domain->name, nt_errstr(result)));
1770 TALLOC_FREE(cli);
1771 TALLOC_FREE(mem_ctx);
1772 return;
1775 result = rpccli_lsa_open_policy2(cli, mem_ctx, True,
1776 SEC_RIGHTS_MAXIMUM_ALLOWED, &pol);
1778 if (NT_STATUS_IS_OK(result)) {
1779 /* This particular query is exactly what Win2k clients use
1780 to determine that the DC is active directory */
1781 result = rpccli_lsa_QueryInfoPolicy2(cli, mem_ctx,
1782 &pol,
1783 LSA_POLICY_INFO_DNS,
1784 &lsa_info);
1787 if (NT_STATUS_IS_OK(result)) {
1788 domain->active_directory = True;
1790 if (lsa_info->dns.name.string) {
1791 fstrcpy(domain->name, lsa_info->dns.name.string);
1794 if (lsa_info->dns.dns_domain.string) {
1795 fstrcpy(domain->alt_name,
1796 lsa_info->dns.dns_domain.string);
1799 /* See if we can set some domain trust flags about
1800 ourself */
1802 if (lsa_info->dns.dns_forest.string) {
1803 fstrcpy(domain->forest_name,
1804 lsa_info->dns.dns_forest.string);
1806 if (strequal(domain->forest_name, domain->alt_name)) {
1807 domain->domain_flags |= NETR_TRUST_FLAG_TREEROOT;
1811 if (lsa_info->dns.sid) {
1812 sid_copy(&domain->sid, lsa_info->dns.sid);
1814 } else {
1815 domain->active_directory = False;
1817 result = rpccli_lsa_open_policy(cli, mem_ctx, True,
1818 SEC_RIGHTS_MAXIMUM_ALLOWED,
1819 &pol);
1821 if (!NT_STATUS_IS_OK(result)) {
1822 goto done;
1825 result = rpccli_lsa_QueryInfoPolicy(cli, mem_ctx,
1826 &pol,
1827 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
1828 &lsa_info);
1830 if (NT_STATUS_IS_OK(result)) {
1832 if (lsa_info->account_domain.name.string) {
1833 fstrcpy(domain->name,
1834 lsa_info->account_domain.name.string);
1837 if (lsa_info->account_domain.sid) {
1838 sid_copy(&domain->sid, lsa_info->account_domain.sid);
1842 done:
1844 DEBUG(5, ("set_dc_type_and_flags_connect: domain %s is %sin native mode.\n",
1845 domain->name, domain->native_mode ? "" : "NOT "));
1847 DEBUG(5,("set_dc_type_and_flags_connect: domain %s is %srunning active directory.\n",
1848 domain->name, domain->active_directory ? "" : "NOT "));
1850 TALLOC_FREE(cli);
1852 TALLOC_FREE(mem_ctx);
1854 domain->initialized = True;
1857 /**********************************************************************
1858 Set the domain_flags (trust attributes, domain operating modes, etc...
1859 ***********************************************************************/
1861 static void set_dc_type_and_flags( struct winbindd_domain *domain )
1863 /* we always have to contact our primary domain */
1865 if ( domain->primary ) {
1866 DEBUG(10,("set_dc_type_and_flags: setting up flags for "
1867 "primary domain\n"));
1868 set_dc_type_and_flags_connect( domain );
1869 return;
1872 /* Use our DC to get the information if possible */
1874 if ( !set_dc_type_and_flags_trustinfo( domain ) ) {
1875 /* Otherwise, fallback to contacting the
1876 domain directly */
1877 set_dc_type_and_flags_connect( domain );
1880 return;
1885 /**********************************************************************
1886 ***********************************************************************/
1888 static bool cm_get_schannel_dcinfo(struct winbindd_domain *domain,
1889 struct dcinfo **ppdc)
1891 NTSTATUS result;
1892 struct rpc_pipe_client *netlogon_pipe;
1894 if (lp_client_schannel() == False) {
1895 return False;
1898 result = cm_connect_netlogon(domain, &netlogon_pipe);
1899 if (!NT_STATUS_IS_OK(result)) {
1900 return False;
1903 /* Return a pointer to the struct dcinfo from the
1904 netlogon pipe. */
1906 *ppdc = domain->conn.netlogon_pipe->dc;
1907 return True;
1910 NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
1911 struct rpc_pipe_client **cli, POLICY_HND *sam_handle)
1913 struct winbindd_cm_conn *conn;
1914 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1915 fstring conn_pwd;
1916 struct dcinfo *p_dcinfo;
1917 char *machine_password = NULL;
1918 char *machine_account = NULL;
1919 char *domain_name = NULL;
1921 result = init_dc_connection(domain);
1922 if (!NT_STATUS_IS_OK(result)) {
1923 return result;
1926 conn = &domain->conn;
1928 if (conn->samr_pipe != NULL) {
1929 goto done;
1933 * No SAMR pipe yet. Attempt to get an NTLMSSP SPNEGO authenticated
1934 * sign and sealed pipe using the machine account password by
1935 * preference. If we can't - try schannel, if that fails, try
1936 * anonymous.
1939 pwd_get_cleartext(&conn->cli->pwd, conn_pwd);
1940 if ((conn->cli->user_name[0] == '\0') ||
1941 (conn->cli->domain[0] == '\0') ||
1942 (conn_pwd[0] == '\0'))
1944 result = get_trust_creds(domain, &machine_password,
1945 &machine_account, NULL);
1946 if (!NT_STATUS_IS_OK(result)) {
1947 DEBUG(10, ("cm_connect_sam: No no user available for "
1948 "domain %s, trying schannel\n", conn->cli->domain));
1949 goto schannel;
1951 domain_name = domain->name;
1952 } else {
1953 machine_password = SMB_STRDUP(conn_pwd);
1954 machine_account = SMB_STRDUP(conn->cli->user_name);
1955 domain_name = conn->cli->domain;
1958 if (!machine_password || !machine_account) {
1959 result = NT_STATUS_NO_MEMORY;
1960 goto done;
1963 /* We have an authenticated connection. Use a NTLMSSP SPNEGO
1964 authenticated SAMR pipe with sign & seal. */
1965 conn->samr_pipe =
1966 cli_rpc_pipe_open_spnego_ntlmssp(conn->cli, PI_SAMR,
1967 PIPE_AUTH_LEVEL_PRIVACY,
1968 domain_name,
1969 machine_account,
1970 machine_password, &result);
1972 if (conn->samr_pipe == NULL) {
1973 DEBUG(10,("cm_connect_sam: failed to connect to SAMR "
1974 "pipe for domain %s using NTLMSSP "
1975 "authenticated pipe: user %s\\%s. Error was "
1976 "%s\n", domain->name, domain_name,
1977 machine_account, nt_errstr(result)));
1978 goto schannel;
1981 DEBUG(10,("cm_connect_sam: connected to SAMR pipe for "
1982 "domain %s using NTLMSSP authenticated "
1983 "pipe: user %s\\%s\n", domain->name,
1984 domain_name, machine_account));
1986 result = rpccli_samr_Connect2(conn->samr_pipe, mem_ctx,
1987 conn->samr_pipe->desthost,
1988 SEC_RIGHTS_MAXIMUM_ALLOWED,
1989 &conn->sam_connect_handle);
1990 if (NT_STATUS_IS_OK(result)) {
1991 goto open_domain;
1993 DEBUG(10,("cm_connect_sam: ntlmssp-sealed rpccli_samr_Connect2 "
1994 "failed for domain %s, error was %s. Trying schannel\n",
1995 domain->name, nt_errstr(result) ));
1996 TALLOC_FREE(conn->samr_pipe);
1998 schannel:
2000 /* Fall back to schannel if it's a W2K pre-SP1 box. */
2002 if (!cm_get_schannel_dcinfo(domain, &p_dcinfo)) {
2003 /* If this call fails - conn->cli can now be NULL ! */
2004 DEBUG(10, ("cm_connect_sam: Could not get schannel auth info "
2005 "for domain %s, trying anon\n", domain->name));
2006 goto anonymous;
2008 conn->samr_pipe = cli_rpc_pipe_open_schannel_with_key
2009 (conn->cli, PI_SAMR, PIPE_AUTH_LEVEL_PRIVACY,
2010 domain->name, p_dcinfo, &result);
2012 if (conn->samr_pipe == NULL) {
2013 DEBUG(10,("cm_connect_sam: failed to connect to SAMR pipe for "
2014 "domain %s using schannel. Error was %s\n",
2015 domain->name, nt_errstr(result) ));
2016 goto anonymous;
2018 DEBUG(10,("cm_connect_sam: connected to SAMR pipe for domain %s using "
2019 "schannel.\n", domain->name ));
2021 result = rpccli_samr_Connect2(conn->samr_pipe, mem_ctx,
2022 conn->samr_pipe->desthost,
2023 SEC_RIGHTS_MAXIMUM_ALLOWED,
2024 &conn->sam_connect_handle);
2025 if (NT_STATUS_IS_OK(result)) {
2026 goto open_domain;
2028 DEBUG(10,("cm_connect_sam: schannel-sealed rpccli_samr_Connect2 failed "
2029 "for domain %s, error was %s. Trying anonymous\n",
2030 domain->name, nt_errstr(result) ));
2031 TALLOC_FREE(conn->samr_pipe);
2033 anonymous:
2035 /* Finally fall back to anonymous. */
2036 result = cli_rpc_pipe_open_noauth(conn->cli, &ndr_table_samr.syntax_id,
2037 &conn->samr_pipe);
2039 if (!NT_STATUS_IS_OK(result)) {
2040 goto done;
2043 result = rpccli_samr_Connect2(conn->samr_pipe, mem_ctx,
2044 conn->samr_pipe->desthost,
2045 SEC_RIGHTS_MAXIMUM_ALLOWED,
2046 &conn->sam_connect_handle);
2047 if (!NT_STATUS_IS_OK(result)) {
2048 DEBUG(10,("cm_connect_sam: rpccli_samr_Connect2 failed "
2049 "for domain %s Error was %s\n",
2050 domain->name, nt_errstr(result) ));
2051 goto done;
2054 open_domain:
2055 result = rpccli_samr_OpenDomain(conn->samr_pipe,
2056 mem_ctx,
2057 &conn->sam_connect_handle,
2058 SEC_RIGHTS_MAXIMUM_ALLOWED,
2059 &domain->sid,
2060 &conn->sam_domain_handle);
2062 done:
2064 if (!NT_STATUS_IS_OK(result)) {
2065 invalidate_cm_connection(conn);
2066 return result;
2069 *cli = conn->samr_pipe;
2070 *sam_handle = conn->sam_domain_handle;
2071 SAFE_FREE(machine_password);
2072 SAFE_FREE(machine_account);
2073 return result;
2076 NTSTATUS cm_connect_lsa(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
2077 struct rpc_pipe_client **cli, POLICY_HND *lsa_policy)
2079 struct winbindd_cm_conn *conn;
2080 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2081 fstring conn_pwd;
2082 struct dcinfo *p_dcinfo;
2084 result = init_dc_connection(domain);
2085 if (!NT_STATUS_IS_OK(result))
2086 return result;
2088 conn = &domain->conn;
2090 if (conn->lsa_pipe != NULL) {
2091 goto done;
2094 pwd_get_cleartext(&conn->cli->pwd, conn_pwd);
2095 if ((conn->cli->user_name[0] == '\0') ||
2096 (conn->cli->domain[0] == '\0') ||
2097 (conn_pwd[0] == '\0')) {
2098 DEBUG(10, ("cm_connect_lsa: No no user available for "
2099 "domain %s, trying schannel\n", conn->cli->domain));
2100 goto schannel;
2103 /* We have an authenticated connection. Use a NTLMSSP SPNEGO
2104 * authenticated LSA pipe with sign & seal. */
2105 conn->lsa_pipe = cli_rpc_pipe_open_spnego_ntlmssp
2106 (conn->cli, PI_LSARPC, PIPE_AUTH_LEVEL_PRIVACY,
2107 conn->cli->domain, conn->cli->user_name, conn_pwd, &result);
2109 if (conn->lsa_pipe == NULL) {
2110 DEBUG(10,("cm_connect_lsa: failed to connect to LSA pipe for "
2111 "domain %s using NTLMSSP authenticated pipe: user "
2112 "%s\\%s. Error was %s. Trying schannel.\n",
2113 domain->name, conn->cli->domain,
2114 conn->cli->user_name, nt_errstr(result)));
2115 goto schannel;
2118 DEBUG(10,("cm_connect_lsa: connected to LSA pipe for domain %s using "
2119 "NTLMSSP authenticated pipe: user %s\\%s\n",
2120 domain->name, conn->cli->domain, conn->cli->user_name ));
2122 result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
2123 SEC_RIGHTS_MAXIMUM_ALLOWED,
2124 &conn->lsa_policy);
2125 if (NT_STATUS_IS_OK(result)) {
2126 goto done;
2129 DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
2130 "schannel\n"));
2132 TALLOC_FREE(conn->lsa_pipe);
2134 schannel:
2136 /* Fall back to schannel if it's a W2K pre-SP1 box. */
2138 if (!cm_get_schannel_dcinfo(domain, &p_dcinfo)) {
2139 /* If this call fails - conn->cli can now be NULL ! */
2140 DEBUG(10, ("cm_connect_lsa: Could not get schannel auth info "
2141 "for domain %s, trying anon\n", domain->name));
2142 goto anonymous;
2144 conn->lsa_pipe = cli_rpc_pipe_open_schannel_with_key
2145 (conn->cli, PI_LSARPC, PIPE_AUTH_LEVEL_PRIVACY,
2146 domain->name, p_dcinfo, &result);
2148 if (conn->lsa_pipe == NULL) {
2149 DEBUG(10,("cm_connect_lsa: failed to connect to LSA pipe for "
2150 "domain %s using schannel. Error was %s\n",
2151 domain->name, nt_errstr(result) ));
2152 goto anonymous;
2154 DEBUG(10,("cm_connect_lsa: connected to LSA pipe for domain %s using "
2155 "schannel.\n", domain->name ));
2157 result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
2158 SEC_RIGHTS_MAXIMUM_ALLOWED,
2159 &conn->lsa_policy);
2160 if (NT_STATUS_IS_OK(result)) {
2161 goto done;
2164 DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
2165 "anonymous\n"));
2167 TALLOC_FREE(conn->lsa_pipe);
2169 anonymous:
2171 result = cli_rpc_pipe_open_noauth(conn->cli,
2172 &ndr_table_lsarpc.syntax_id,
2173 &conn->lsa_pipe);
2174 if (!NT_STATUS_IS_OK(result)) {
2175 result = NT_STATUS_PIPE_NOT_AVAILABLE;
2176 goto done;
2179 result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
2180 SEC_RIGHTS_MAXIMUM_ALLOWED,
2181 &conn->lsa_policy);
2182 done:
2183 if (!NT_STATUS_IS_OK(result)) {
2184 invalidate_cm_connection(conn);
2185 return result;
2188 *cli = conn->lsa_pipe;
2189 *lsa_policy = conn->lsa_policy;
2190 return result;
2193 /****************************************************************************
2194 Open the netlogon pipe to this DC. Use schannel if specified in client conf.
2195 session key stored in conn->netlogon_pipe->dc->sess_key.
2196 ****************************************************************************/
2198 NTSTATUS cm_connect_netlogon(struct winbindd_domain *domain,
2199 struct rpc_pipe_client **cli)
2201 struct winbindd_cm_conn *conn;
2202 NTSTATUS result;
2204 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
2205 uint8 mach_pwd[16];
2206 uint32 sec_chan_type;
2207 const char *account_name;
2208 struct rpc_pipe_client *netlogon_pipe = NULL;
2210 *cli = NULL;
2212 result = init_dc_connection(domain);
2213 if (!NT_STATUS_IS_OK(result)) {
2214 return result;
2217 conn = &domain->conn;
2219 if (conn->netlogon_pipe != NULL) {
2220 *cli = conn->netlogon_pipe;
2221 return NT_STATUS_OK;
2224 result = cli_rpc_pipe_open_noauth(conn->cli,
2225 &ndr_table_netlogon.syntax_id,
2226 &netlogon_pipe);
2227 if (!NT_STATUS_IS_OK(result)) {
2228 return result;
2231 if ((!IS_DC) && (!domain->primary)) {
2232 /* Clear the schannel request bit and drop down */
2233 neg_flags &= ~NETLOGON_NEG_SCHANNEL;
2234 goto no_schannel;
2237 if (lp_client_schannel() != False) {
2238 neg_flags |= NETLOGON_NEG_SCHANNEL;
2241 if (!get_trust_pw_hash(domain->name, mach_pwd, &account_name,
2242 &sec_chan_type))
2244 TALLOC_FREE(netlogon_pipe);
2245 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2248 result = rpccli_netlogon_setup_creds(
2249 netlogon_pipe,
2250 domain->dcname, /* server name. */
2251 domain->name, /* domain name */
2252 global_myname(), /* client name */
2253 account_name, /* machine account */
2254 mach_pwd, /* machine password */
2255 sec_chan_type, /* from get_trust_pw */
2256 &neg_flags);
2258 if (!NT_STATUS_IS_OK(result)) {
2259 TALLOC_FREE(netlogon_pipe);
2260 return result;
2263 if ((lp_client_schannel() == True) &&
2264 ((neg_flags & NETLOGON_NEG_SCHANNEL) == 0)) {
2265 DEBUG(3, ("Server did not offer schannel\n"));
2266 TALLOC_FREE(netlogon_pipe);
2267 return NT_STATUS_ACCESS_DENIED;
2270 no_schannel:
2271 if ((lp_client_schannel() == False) ||
2272 ((neg_flags & NETLOGON_NEG_SCHANNEL) == 0)) {
2274 * NetSamLogonEx only works for schannel
2276 domain->can_do_samlogon_ex = False;
2278 /* We're done - just keep the existing connection to NETLOGON
2279 * open */
2280 conn->netlogon_pipe = netlogon_pipe;
2281 *cli = conn->netlogon_pipe;
2282 return NT_STATUS_OK;
2285 /* Using the credentials from the first pipe, open a signed and sealed
2286 second netlogon pipe. The session key is stored in the schannel
2287 part of the new pipe auth struct.
2290 conn->netlogon_pipe =
2291 cli_rpc_pipe_open_schannel_with_key(conn->cli,
2292 PI_NETLOGON,
2293 PIPE_AUTH_LEVEL_PRIVACY,
2294 domain->name,
2295 netlogon_pipe->dc,
2296 &result);
2298 /* We can now close the initial netlogon pipe. */
2299 TALLOC_FREE(netlogon_pipe);
2301 if (conn->netlogon_pipe == NULL) {
2302 DEBUG(3, ("Could not open schannel'ed NETLOGON pipe. Error "
2303 "was %s\n", nt_errstr(result)));
2305 /* make sure we return something besides OK */
2306 return !NT_STATUS_IS_OK(result) ? result : NT_STATUS_PIPE_NOT_AVAILABLE;
2310 * Try NetSamLogonEx for AD domains
2312 domain->can_do_samlogon_ex = domain->active_directory;
2314 *cli = conn->netlogon_pipe;
2315 return NT_STATUS_OK;