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 Carter 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 /**********************************************************************
29 Is this our primary domain ?
30 **********************************************************************/
33 static BOOL is_our_primary_domain(const char *domain)
35 int role = lp_server_role();
37 if ((role == ROLE_DOMAIN_MEMBER) && strequal(lp_workgroup(), domain)) {
39 } else if (strequal(get_global_sam_name(), domain)) {
46 /**************************************************************************
47 Find the name and IP address for a server in the realm/domain
48 *************************************************************************/
50 static BOOL ads_dc_name(const char *domain,
52 struct in_addr *dc_ip,
59 if (!realm && strequal(domain, lp_workgroup())) {
63 sitename = sitename_fetch(realm);
65 /* Try this 3 times then give up. */
66 for( i =0 ; i < 3; i++) {
67 ads = ads_init(realm, domain, NULL);
73 DEBUG(4,("ads_dc_name: domain=%s\n", domain));
76 /* we don't need to bind, just connect */
77 ads->auth.flags |= ADS_AUTH_NO_BIND;
81 if (!ads->config.realm) {
87 /* Now we've found a server, see if our sitename
88 has changed. If so, we need to re-do the DNS query
89 to ensure we only find servers in our site. */
91 if (stored_sitename_changed(realm, sitename)) {
93 sitename = sitename_fetch(realm);
95 /* Ensure we don't cache the DC we just connected to. */
96 namecache_delete(realm, 0x1C);
97 namecache_delete(domain, 0x1C);
102 if (is_our_primary_domain(domain) && (ads->config.flags & ADS_KDC) && ads_closest_dc(ads)) {
103 /* We're going to use this KDC for this realm/domain.
104 If we are using sites, then force the krb5 libs
107 create_local_private_krb5_conf_for_domain(realm,
117 DEBUG(1,("ads_dc_name: sitename (now \"%s\") keeps changing ???\n",
118 sitename ? sitename : ""));
125 fstrcpy(srv_name, ads->config.ldap_server_name);
126 strupper_m(srv_name);
127 *dc_ip = ads->ldap_ip;
130 DEBUG(4,("ads_dc_name: using server='%s' IP=%s\n",
131 srv_name, inet_ntoa(*dc_ip)));
136 /****************************************************************************
137 Utility function to return the name of a DC. The name is guaranteed to be
138 valid since we have already done a name_status_find on it
139 ***************************************************************************/
141 static BOOL rpc_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out)
143 struct ip_service *ip_list = NULL;
144 struct in_addr dc_ip, exclude_ip;
148 zero_ip(&exclude_ip);
150 /* get a list of all domain controllers */
152 if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, NULL, &ip_list, &count,
154 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
158 /* Remove the entry we've already failed with (should be the PDC). */
160 for (i = 0; i < count; i++) {
161 if (is_zero_ip(ip_list[i].ip))
164 if (name_status_find(domain, 0x1c, 0x20, ip_list[i].ip, srv_name)) {
165 result = check_negative_conn_cache( domain, srv_name );
166 if ( NT_STATUS_IS_OK(result) ) {
167 dc_ip = ip_list[i].ip;
176 /* No-one to talk to )-: */
177 return False; /* Boo-hoo */
180 /* We have the netbios name and IP address of a domain controller.
181 Ideally we should sent a SAMLOGON request to determine whether
182 the DC is alive and kicking. If we can catch a dead DC before
183 performing a cli_connect() we can avoid a 30-second timeout. */
185 DEBUG(3, ("rpc_dc_name: Returning DC %s (%s) for domain %s\n", srv_name,
186 inet_ntoa(dc_ip), domain));
195 /**********************************************************************
196 wrapper around ads and rpc methods of finds DC's
197 **********************************************************************/
199 BOOL get_dc_name(const char *domain, const char *realm, fstring srv_name, struct in_addr *ip_out)
201 struct in_addr dc_ip;
203 BOOL our_domain = False;
209 if ( strequal(lp_workgroup(), domain) || strequal(lp_realm(), realm) )
212 /* always try to obey what the admin specified in smb.conf
213 (for the local domain) */
215 if ( (our_domain && lp_security()==SEC_ADS) || realm ) {
216 ret = ads_dc_name(domain, realm, &dc_ip, srv_name);
220 /* if we have only the realm we can't do anything else */
225 /* fall back on rpc methods if the ADS methods fail */
226 ret = rpc_dc_name(domain, srv_name, &dc_ip);