s3:torture:delete: untangle function call from result check
[Samba/gebeck_regimport.git] / source3 / libsmb / namequery_dc.c
blobd4e68cb00cd31cecae1a0d99ae3b05ae962c1f14
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"
29 #include "lib/param/loadparm.h"
31 /**********************************************************************
32 Is this our primary domain ?
33 **********************************************************************/
35 #ifdef HAVE_KRB5
36 static bool is_our_primary_domain(const char *domain)
38 int role = lp_server_role();
40 if ((role == ROLE_DOMAIN_MEMBER) && strequal(lp_workgroup(), domain)) {
41 return True;
42 } else if (strequal(get_global_sam_name(), domain)) {
43 return True;
45 return False;
47 #endif
49 /**************************************************************************
50 Find the name and IP address for a server in the realm/domain
51 *************************************************************************/
53 static bool ads_dc_name(const char *domain,
54 const char *realm,
55 struct sockaddr_storage *dc_ss,
56 fstring srv_name)
58 ADS_STRUCT *ads;
59 char *sitename;
60 int i;
61 char addr[INET6_ADDRSTRLEN];
63 if (!realm && strequal(domain, lp_workgroup())) {
64 realm = lp_realm();
67 sitename = sitename_fetch(realm);
69 /* Try this 3 times then give up. */
70 for( i =0 ; i < 3; i++) {
71 ads = ads_init(realm, domain, NULL);
72 if (!ads) {
73 SAFE_FREE(sitename);
74 return False;
77 DEBUG(4,("ads_dc_name: domain=%s\n", domain));
79 #ifdef HAVE_ADS
80 /* we don't need to bind, just connect */
81 ads->auth.flags |= ADS_AUTH_NO_BIND;
82 ads_connect(ads);
83 #endif
85 if (!ads->config.realm) {
86 SAFE_FREE(sitename);
87 ads_destroy(&ads);
88 return False;
91 /* Now we've found a server, see if our sitename
92 has changed. If so, we need to re-do the DNS query
93 to ensure we only find servers in our site. */
95 if (stored_sitename_changed(realm, sitename)) {
96 SAFE_FREE(sitename);
97 sitename = sitename_fetch(realm);
98 ads_destroy(&ads);
99 /* Ensure we don't cache the DC we just connected to. */
100 namecache_delete(realm, 0x1C);
101 namecache_delete(domain, 0x1C);
102 continue;
105 #ifdef HAVE_ADS
106 if (is_our_primary_domain(domain) && (ads->config.flags & NBT_SERVER_KDC)) {
107 if (ads_closest_dc(ads)) {
108 /* We're going to use this KDC for this realm/domain.
109 If we are using sites, then force the krb5 libs
110 to use this KDC. */
112 create_local_private_krb5_conf_for_domain(realm,
113 domain,
114 sitename,
115 &ads->ldap.ss,
116 ads->config.ldap_server_name);
117 } else {
118 create_local_private_krb5_conf_for_domain(realm,
119 domain,
120 NULL,
121 &ads->ldap.ss,
122 ads->config.ldap_server_name);
125 #endif
126 break;
129 if (i == 3) {
130 DEBUG(1,("ads_dc_name: sitename (now \"%s\") keeps changing ???\n",
131 sitename ? sitename : ""));
132 SAFE_FREE(sitename);
133 return False;
136 SAFE_FREE(sitename);
138 fstrcpy(srv_name, ads->config.ldap_server_name);
139 strupper_m(srv_name);
140 #ifdef HAVE_ADS
141 *dc_ss = ads->ldap.ss;
142 #else
143 zero_sockaddr(dc_ss);
144 #endif
145 ads_destroy(&ads);
147 print_sockaddr(addr, sizeof(addr), dc_ss);
148 DEBUG(4,("ads_dc_name: using server='%s' IP=%s\n",
149 srv_name, addr));
151 return True;
154 /****************************************************************************
155 Utility function to return the name of a DC. The name is guaranteed to be
156 valid since we have already done a name_status_find on it
157 ***************************************************************************/
159 static bool rpc_dc_name(const char *domain,
160 fstring srv_name,
161 struct sockaddr_storage *ss_out)
163 struct ip_service *ip_list = NULL;
164 struct sockaddr_storage dc_ss;
165 int count, i;
166 NTSTATUS result;
167 char addr[INET6_ADDRSTRLEN];
169 /* get a list of all domain controllers */
171 if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, NULL, &ip_list, &count,
172 False))) {
173 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
174 return False;
177 /* Remove the entry we've already failed with (should be the PDC). */
179 for (i = 0; i < count; i++) {
180 if (is_zero_addr(&ip_list[i].ss))
181 continue;
183 if (name_status_find(domain, 0x1c, 0x20, &ip_list[i].ss, srv_name)) {
184 result = check_negative_conn_cache( domain, srv_name );
185 if ( NT_STATUS_IS_OK(result) ) {
186 dc_ss = ip_list[i].ss;
187 goto done;
192 SAFE_FREE(ip_list);
194 /* No-one to talk to )-: */
195 return False; /* Boo-hoo */
197 done:
198 /* We have the netbios name and IP address of a domain controller.
199 Ideally we should sent a SAMLOGON request to determine whether
200 the DC is alive and kicking. If we can catch a dead DC before
201 performing a cli_connect() we can avoid a 30-second timeout. */
203 print_sockaddr(addr, sizeof(addr), &dc_ss);
204 DEBUG(3, ("rpc_dc_name: Returning DC %s (%s) for domain %s\n", srv_name,
205 addr, domain));
207 *ss_out = dc_ss;
208 SAFE_FREE(ip_list);
210 return True;
213 /**********************************************************************
214 wrapper around ads and rpc methods of finds DC's
215 **********************************************************************/
217 bool get_dc_name(const char *domain,
218 const char *realm,
219 fstring srv_name,
220 struct sockaddr_storage *ss_out)
222 struct sockaddr_storage dc_ss;
223 bool ret;
224 bool our_domain = False;
226 zero_sockaddr(&dc_ss);
228 ret = False;
230 if ( strequal(lp_workgroup(), domain) || strequal(lp_realm(), realm) )
231 our_domain = True;
233 /* always try to obey what the admin specified in smb.conf
234 (for the local domain) */
236 if ( (our_domain && lp_security()==SEC_ADS) || realm ) {
237 ret = ads_dc_name(domain, realm, &dc_ss, srv_name);
240 if (!domain) {
241 /* if we have only the realm we can't do anything else */
242 return False;
245 if (!ret) {
246 /* fall back on rpc methods if the ADS methods fail */
247 ret = rpc_dc_name(domain, srv_name, &dc_ss);
250 *ss_out = dc_ss;
252 return ret;