selftest:Samba4: report when samba is started and ready
[Samba.git] / source3 / libsmb / namequery_dc.c
blob4ee5b5278e443d482d91cf94f0fa4883169c15b8
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"
32 /**********************************************************************
33 Is this our primary domain ?
34 **********************************************************************/
36 #ifdef HAVE_ADS
37 static bool is_our_primary_domain(const char *domain)
39 int role = lp_server_role();
41 if ((role == ROLE_DOMAIN_MEMBER) && strequal(lp_workgroup(), domain)) {
42 return True;
43 } else if (strequal(get_global_sam_name(), domain)) {
44 return True;
46 return False;
48 #endif
50 /**************************************************************************
51 Find the name and IP address for a server in the realm/domain
52 *************************************************************************/
54 static bool ads_dc_name(const char *domain,
55 const char *realm,
56 struct sockaddr_storage *dc_ss,
57 fstring srv_name)
59 ADS_STRUCT *ads;
60 char *sitename;
61 int i;
62 char addr[INET6_ADDRSTRLEN];
64 if (!realm && strequal(domain, lp_workgroup())) {
65 realm = lp_realm();
68 sitename = sitename_fetch(talloc_tos(), realm);
70 /* Try this 3 times then give up. */
71 for( i =0 ; i < 3; i++) {
72 ads = ads_init(realm, domain, NULL);
73 if (!ads) {
74 TALLOC_FREE(sitename);
75 return False;
78 DEBUG(4,("ads_dc_name: domain=%s\n", domain));
80 #ifdef HAVE_ADS
81 /* we don't need to bind, just connect */
82 ads->auth.flags |= ADS_AUTH_NO_BIND;
83 ads_connect(ads);
84 #endif
86 if (!ads->config.realm) {
87 TALLOC_FREE(sitename);
88 ads_destroy(&ads);
89 return False;
92 /* Now we've found a server, see if our sitename
93 has changed. If so, we need to re-do the DNS query
94 to ensure we only find servers in our site. */
96 if (stored_sitename_changed(realm, sitename)) {
97 TALLOC_FREE(sitename);
98 sitename = sitename_fetch(talloc_tos(), realm);
99 ads_destroy(&ads);
100 /* Ensure we don't cache the DC we just connected to. */
101 namecache_delete(realm, 0x1C);
102 namecache_delete(domain, 0x1C);
103 continue;
106 #ifdef HAVE_ADS
107 if (is_our_primary_domain(domain) && (ads->config.flags & NBT_SERVER_KDC)) {
108 if (ads_closest_dc(ads)) {
109 /* We're going to use this KDC for this realm/domain.
110 If we are using sites, then force the krb5 libs
111 to use this KDC. */
113 create_local_private_krb5_conf_for_domain(realm,
114 domain,
115 sitename,
116 &ads->ldap.ss);
117 } else {
118 create_local_private_krb5_conf_for_domain(realm,
119 domain,
120 NULL,
121 &ads->ldap.ss);
124 #endif
125 break;
128 if (i == 3) {
129 DEBUG(1,("ads_dc_name: sitename (now \"%s\") keeps changing ???\n",
130 sitename ? sitename : ""));
131 TALLOC_FREE(sitename);
132 ads_destroy(&ads);
133 return False;
136 TALLOC_FREE(sitename);
138 fstrcpy(srv_name, ads->config.ldap_server_name);
139 if (!strupper_m(srv_name)) {
140 ads_destroy(&ads);
141 return false;
143 #ifdef HAVE_ADS
144 *dc_ss = ads->ldap.ss;
145 #else
146 zero_sockaddr(dc_ss);
147 #endif
148 ads_destroy(&ads);
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 return True;
157 /****************************************************************************
158 Utility function to return the name of a DC. The name is guaranteed to be
159 valid since we have already done a name_status_find on it
160 ***************************************************************************/
162 static bool rpc_dc_name(const char *domain,
163 fstring srv_name,
164 struct sockaddr_storage *ss_out)
166 struct ip_service *ip_list = NULL;
167 struct sockaddr_storage dc_ss;
168 int count, i;
169 NTSTATUS result;
170 char addr[INET6_ADDRSTRLEN];
172 /* get a list of all domain controllers */
174 if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, NULL, &ip_list, &count,
175 False))) {
176 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
177 return False;
180 /* Remove the entry we've already failed with (should be the PDC). */
182 for (i = 0; i < count; i++) {
183 if (is_zero_addr(&ip_list[i].ss))
184 continue;
186 if (name_status_find(domain, 0x1c, 0x20, &ip_list[i].ss, srv_name)) {
187 result = check_negative_conn_cache( domain, srv_name );
188 if ( NT_STATUS_IS_OK(result) ) {
189 dc_ss = ip_list[i].ss;
190 goto done;
195 SAFE_FREE(ip_list);
197 /* No-one to talk to )-: */
198 return False; /* Boo-hoo */
200 done:
201 /* We have the netbios name and IP address of a domain controller.
202 Ideally we should sent a SAMLOGON request to determine whether
203 the DC is alive and kicking. If we can catch a dead DC before
204 performing a cli_connect() we can avoid a 30-second timeout. */
206 print_sockaddr(addr, sizeof(addr), &dc_ss);
207 DEBUG(3, ("rpc_dc_name: Returning DC %s (%s) for domain %s\n", srv_name,
208 addr, domain));
210 *ss_out = dc_ss;
211 SAFE_FREE(ip_list);
213 return True;
216 /**********************************************************************
217 wrapper around ads and rpc methods of finds DC's
218 **********************************************************************/
220 bool get_dc_name(const char *domain,
221 const char *realm,
222 fstring srv_name,
223 struct sockaddr_storage *ss_out)
225 struct sockaddr_storage dc_ss;
226 bool ret;
227 bool our_domain = False;
229 zero_sockaddr(&dc_ss);
231 ret = False;
233 if ( strequal(lp_workgroup(), domain) || strequal(lp_realm(), realm) )
234 our_domain = True;
236 /* always try to obey what the admin specified in smb.conf
237 (for the local domain) */
239 if ( (our_domain && lp_security()==SEC_ADS) || realm ) {
240 ret = ads_dc_name(domain, realm, &dc_ss, srv_name);
243 if (!domain) {
244 /* if we have only the realm we can't do anything else */
245 return False;
248 if (!ret) {
249 /* fall back on rpc methods if the ADS methods fail */
250 ret = rpc_dc_name(domain, srv_name, &dc_ss);
253 *ss_out = dc_ss;
255 return ret;