libcli/smb: move smb2cli_close.c from source3 to the toplevel
[Samba/gebeck_regimport.git] / source3 / libsmb / namequery_dc.c
blob39b780c611e6a7bb142ba7db07c571cb9a16b4db
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 "libads/sitename_cache.h"
27 #include "ads.h"
28 #include "../librpc/gen_ndr/nbt.h"
30 /**********************************************************************
31 Is this our primary domain ?
32 **********************************************************************/
34 #ifdef HAVE_KRB5
35 static bool is_our_primary_domain(const char *domain)
37 int role = lp_server_role();
39 if ((role == ROLE_DOMAIN_MEMBER) && strequal(lp_workgroup(), domain)) {
40 return True;
41 } else if (strequal(get_global_sam_name(), domain)) {
42 return True;
44 return False;
46 #endif
48 /**************************************************************************
49 Find the name and IP address for a server in the realm/domain
50 *************************************************************************/
52 static bool ads_dc_name(const char *domain,
53 const char *realm,
54 struct sockaddr_storage *dc_ss,
55 fstring srv_name)
57 ADS_STRUCT *ads;
58 char *sitename;
59 int i;
60 char addr[INET6_ADDRSTRLEN];
62 if (!realm && strequal(domain, lp_workgroup())) {
63 realm = lp_realm();
66 sitename = sitename_fetch(realm);
68 /* Try this 3 times then give up. */
69 for( i =0 ; i < 3; i++) {
70 ads = ads_init(realm, domain, NULL);
71 if (!ads) {
72 SAFE_FREE(sitename);
73 return False;
76 DEBUG(4,("ads_dc_name: domain=%s\n", domain));
78 #ifdef HAVE_ADS
79 /* we don't need to bind, just connect */
80 ads->auth.flags |= ADS_AUTH_NO_BIND;
81 ads_connect(ads);
82 #endif
84 if (!ads->config.realm) {
85 SAFE_FREE(sitename);
86 ads_destroy(&ads);
87 return False;
90 /* Now we've found a server, see if our sitename
91 has changed. If so, we need to re-do the DNS query
92 to ensure we only find servers in our site. */
94 if (stored_sitename_changed(realm, sitename)) {
95 SAFE_FREE(sitename);
96 sitename = sitename_fetch(realm);
97 ads_destroy(&ads);
98 /* Ensure we don't cache the DC we just connected to. */
99 namecache_delete(realm, 0x1C);
100 namecache_delete(domain, 0x1C);
101 continue;
104 #ifdef HAVE_ADS
105 if (is_our_primary_domain(domain) && (ads->config.flags & NBT_SERVER_KDC)) {
106 if (ads_closest_dc(ads)) {
107 /* We're going to use this KDC for this realm/domain.
108 If we are using sites, then force the krb5 libs
109 to use this KDC. */
111 create_local_private_krb5_conf_for_domain(realm,
112 domain,
113 sitename,
114 &ads->ldap.ss,
115 ads->config.ldap_server_name);
116 } else {
117 create_local_private_krb5_conf_for_domain(realm,
118 domain,
119 NULL,
120 &ads->ldap.ss,
121 ads->config.ldap_server_name);
124 #endif
125 break;
128 if (i == 3) {
129 DEBUG(1,("ads_dc_name: sitename (now \"%s\") keeps changing ???\n",
130 sitename ? sitename : ""));
131 SAFE_FREE(sitename);
132 return False;
135 SAFE_FREE(sitename);
137 fstrcpy(srv_name, ads->config.ldap_server_name);
138 strupper_m(srv_name);
139 #ifdef HAVE_ADS
140 *dc_ss = ads->ldap.ss;
141 #else
142 zero_sockaddr(dc_ss);
143 #endif
144 ads_destroy(&ads);
146 print_sockaddr(addr, sizeof(addr), dc_ss);
147 DEBUG(4,("ads_dc_name: using server='%s' IP=%s\n",
148 srv_name, addr));
150 return True;
153 /****************************************************************************
154 Utility function to return the name of a DC. The name is guaranteed to be
155 valid since we have already done a name_status_find on it
156 ***************************************************************************/
158 static bool rpc_dc_name(const char *domain,
159 fstring srv_name,
160 struct sockaddr_storage *ss_out)
162 struct ip_service *ip_list = NULL;
163 struct sockaddr_storage dc_ss;
164 int count, i;
165 NTSTATUS result;
166 char addr[INET6_ADDRSTRLEN];
168 /* get a list of all domain controllers */
170 if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, NULL, &ip_list, &count,
171 False))) {
172 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
173 return False;
176 /* Remove the entry we've already failed with (should be the PDC). */
178 for (i = 0; i < count; i++) {
179 if (is_zero_addr(&ip_list[i].ss))
180 continue;
182 if (name_status_find(domain, 0x1c, 0x20, &ip_list[i].ss, srv_name)) {
183 result = check_negative_conn_cache( domain, srv_name );
184 if ( NT_STATUS_IS_OK(result) ) {
185 dc_ss = ip_list[i].ss;
186 goto done;
191 SAFE_FREE(ip_list);
193 /* No-one to talk to )-: */
194 return False; /* Boo-hoo */
196 done:
197 /* We have the netbios name and IP address of a domain controller.
198 Ideally we should sent a SAMLOGON request to determine whether
199 the DC is alive and kicking. If we can catch a dead DC before
200 performing a cli_connect() we can avoid a 30-second timeout. */
202 print_sockaddr(addr, sizeof(addr), &dc_ss);
203 DEBUG(3, ("rpc_dc_name: Returning DC %s (%s) for domain %s\n", srv_name,
204 addr, domain));
206 *ss_out = dc_ss;
207 SAFE_FREE(ip_list);
209 return True;
212 /**********************************************************************
213 wrapper around ads and rpc methods of finds DC's
214 **********************************************************************/
216 bool get_dc_name(const char *domain,
217 const char *realm,
218 fstring srv_name,
219 struct sockaddr_storage *ss_out)
221 struct sockaddr_storage dc_ss;
222 bool ret;
223 bool our_domain = False;
225 zero_sockaddr(&dc_ss);
227 ret = False;
229 if ( strequal(lp_workgroup(), domain) || strequal(lp_realm(), realm) )
230 our_domain = True;
232 /* always try to obey what the admin specified in smb.conf
233 (for the local domain) */
235 if ( (our_domain && lp_security()==SEC_ADS) || realm ) {
236 ret = ads_dc_name(domain, realm, &dc_ss, srv_name);
239 if (!domain) {
240 /* if we have only the realm we can't do anything else */
241 return False;
244 if (!ret) {
245 /* fall back on rpc methods if the ADS methods fail */
246 ret = rpc_dc_name(domain, srv_name, &dc_ss);
249 *ss_out = dc_ss;
251 return ret;