ctdb-scripts: Move connection tracking to 10.interface
[samba4-gss.git] / source3 / libsmb / namequery_dc.c
blob3a2f22129b848ea277249c71b9c3c386273f8b48
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 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 3 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, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "libsmb/namequery.h"
27 #include "libads/sitename_cache.h"
28 #include "ads.h"
29 #include "../librpc/gen_ndr/nbt.h"
30 #include "lib/param/loadparm.h"
31 #include "lib/util/string_wrappers.h"
33 /**********************************************************************
34 Is this our primary domain ?
35 **********************************************************************/
37 #ifdef HAVE_ADS
38 static bool is_our_primary_domain(const char *domain)
40 int role = lp_server_role();
42 if ((role == ROLE_DOMAIN_MEMBER) && strequal(lp_workgroup(), domain)) {
43 return True;
44 } else if (strequal(get_global_sam_name(), domain)) {
45 return True;
47 return False;
49 #endif
51 /**************************************************************************
52 Find the name and IP address for a server in the realm/domain
53 *************************************************************************/
55 static bool ads_dc_name(const char *domain,
56 const char *realm,
57 struct sockaddr_storage *dc_ss,
58 fstring srv_name)
60 TALLOC_CTX *tmp_ctx = talloc_stackframe();
61 bool ok = false;
62 ADS_STRUCT *ads;
63 const char *sitename;
64 int i;
65 char addr[INET6_ADDRSTRLEN];
67 if (!realm && strequal(domain, lp_workgroup())) {
68 realm = lp_realm();
71 sitename = sitename_fetch(tmp_ctx, realm);
73 /* Try this 3 times then give up. */
74 for( i =0 ; i < 3; i++) {
75 ads = ads_init(tmp_ctx, realm, domain, NULL, ADS_SASL_PLAIN);
76 if (!ads) {
77 ok = false;
78 goto out;
81 DEBUG(4,("ads_dc_name: domain=%s\n", domain));
83 #ifdef HAVE_ADS
84 /* we don't need to bind, just connect */
85 ads_connect_cldap_only(ads);
86 #endif
88 if (!ads->config.realm) {
89 ok = false;
90 goto out;
93 /* Now we've found a server, see if our sitename
94 has changed. If so, we need to re-do the DNS query
95 to ensure we only find servers in our site. */
97 #ifdef HAVE_ADS
98 if (ads_closest_dc(ads)) {
99 sitename = ads->config.client_site_name;
100 } else
101 #endif
102 if (stored_sitename_changed(realm, sitename)) {
103 sitename = sitename_fetch(tmp_ctx, realm);
104 TALLOC_FREE(ads);
105 /* Ensure we don't cache the DC we just connected to. */
106 namecache_delete(realm, 0x1C);
107 namecache_delete(domain, 0x1C);
108 continue;
111 #ifdef HAVE_ADS
112 if (is_our_primary_domain(domain) && (ads->config.flags & NBT_SERVER_KDC)) {
113 if (ads_closest_dc(ads)) {
114 /* We're going to use this KDC for this realm/domain.
115 If we are using sites, then force the krb5 libs
116 to use this KDC. */
118 create_local_private_krb5_conf_for_domain(realm,
119 domain,
120 sitename,
121 &ads->ldap.ss);
122 } else {
123 create_local_private_krb5_conf_for_domain(realm,
124 domain,
125 NULL,
126 &ads->ldap.ss);
129 #endif
130 break;
133 if (i == 3) {
134 DEBUG(1,("ads_dc_name: sitename (now \"%s\") keeps changing ???\n",
135 sitename ? sitename : ""));
136 ok = false;
137 goto out;
140 fstrcpy(srv_name, ads->config.ldap_server_name);
141 if (!strupper_m(srv_name)) {
142 ok = false;
143 goto out;
145 #ifdef HAVE_ADS
146 *dc_ss = ads->ldap.ss;
147 #else
148 zero_sockaddr(dc_ss);
149 #endif
150 print_sockaddr(addr, sizeof(addr), dc_ss);
151 DEBUG(4,("ads_dc_name: using server='%s' IP=%s\n",
152 srv_name, addr));
154 ok = true;
155 out:
156 TALLOC_FREE(tmp_ctx);
158 return ok;
161 /****************************************************************************
162 Utility function to return the name of a DC. The name is guaranteed to be
163 valid since we have already done a name_status_find on it
164 ***************************************************************************/
166 static bool rpc_dc_name(const char *domain,
167 fstring srv_name,
168 struct sockaddr_storage *ss_out)
170 struct samba_sockaddr *sa_list = NULL;
171 size_t count = 0;
172 struct sockaddr_storage dc_ss;
173 size_t i;
174 NTSTATUS result;
175 char addr[INET6_ADDRSTRLEN];
177 /* get a list of all domain controllers */
179 result = get_sorted_dc_list(talloc_tos(),
180 domain,
181 NULL,
182 &sa_list,
183 &count,
184 false);
185 if (!NT_STATUS_IS_OK(result)) {
186 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
187 return False;
190 /* Remove the entry we've already failed with (should be the PDC). */
192 for (i = 0; i < count; i++) {
193 if (is_zero_addr(&sa_list[i].u.ss))
194 continue;
196 if (name_status_find(domain, 0x1c, 0x20, &sa_list[i].u.ss, srv_name)) {
197 result = check_negative_conn_cache( domain, srv_name );
198 if ( NT_STATUS_IS_OK(result) ) {
199 dc_ss = sa_list[i].u.ss;
200 goto done;
205 TALLOC_FREE(sa_list);
207 /* No-one to talk to )-: */
208 return False; /* Boo-hoo */
210 done:
211 /* We have the netbios name and IP address of a domain controller.
212 Ideally we should sent a SAMLOGON request to determine whether
213 the DC is alive and kicking. If we can catch a dead DC before
214 performing a cli_connect() we can avoid a 30-second timeout. */
216 print_sockaddr(addr, sizeof(addr), &dc_ss);
217 DEBUG(3, ("rpc_dc_name: Returning DC %s (%s) for domain %s\n", srv_name,
218 addr, domain));
220 *ss_out = dc_ss;
221 TALLOC_FREE(sa_list);
223 return True;
226 /**********************************************************************
227 wrapper around ads and rpc methods of finds DC's
228 **********************************************************************/
230 bool get_dc_name(const char *domain,
231 const char *realm,
232 fstring srv_name,
233 struct sockaddr_storage *ss_out)
235 struct sockaddr_storage dc_ss;
236 bool ret;
237 bool our_domain = False;
239 zero_sockaddr(&dc_ss);
241 ret = False;
243 if ( strequal(lp_workgroup(), domain) || strequal(lp_realm(), realm) )
244 our_domain = True;
246 /* always try to obey what the admin specified in smb.conf
247 (for the local domain) */
249 if ( (our_domain && lp_security()==SEC_ADS) || realm ) {
250 ret = ads_dc_name(domain, realm, &dc_ss, srv_name);
253 if (!domain) {
254 /* if we have only the realm we can't do anything else */
255 return False;
258 if (!ret) {
259 /* fall back on rpc methods if the ADS methods fail */
260 ret = rpc_dc_name(domain, srv_name, &dc_ss);
263 *ss_out = dc_ss;
265 return ret;