source: vendor/current/source3/libsmb/namequery_dc.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 6.5 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 Winbind daemon connection manager
5
6 Copyright (C) Tim Potter 2001
7 Copyright (C) Andrew Bartlett 2002
8 Copyright (C) Gerald Carter 2003
9
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.
14
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.
19
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/>.
22*/
23
24
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"
30
31/**********************************************************************
32 Is this our primary domain ?
33**********************************************************************/
34
35#ifdef HAVE_KRB5
36static bool is_our_primary_domain(const char *domain)
37{
38 int role = lp_server_role();
39
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;
44 }
45 return False;
46}
47#endif
48
49/**************************************************************************
50 Find the name and IP address for a server in the realm/domain
51 *************************************************************************/
52
53static bool ads_dc_name(const char *domain,
54 const char *realm,
55 struct sockaddr_storage *dc_ss,
56 fstring srv_name)
57{
58 ADS_STRUCT *ads;
59 char *sitename;
60 int i;
61 char addr[INET6_ADDRSTRLEN];
62
63 if (!realm && strequal(domain, lp_workgroup())) {
64 realm = lp_realm();
65 }
66
67 sitename = sitename_fetch(talloc_tos(), realm);
68
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 TALLOC_FREE(sitename);
74 return False;
75 }
76
77 DEBUG(4,("ads_dc_name: domain=%s\n", domain));
78
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
84
85 if (!ads->config.realm) {
86 TALLOC_FREE(sitename);
87 ads_destroy(&ads);
88 return False;
89 }
90
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. */
94
95 if (stored_sitename_changed(realm, sitename)) {
96 TALLOC_FREE(sitename);
97 sitename = sitename_fetch(talloc_tos(), 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;
103 }
104
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. */
111
112 create_local_private_krb5_conf_for_domain(realm,
113 domain,
114 sitename,
115 &ads->ldap.ss);
116 } else {
117 create_local_private_krb5_conf_for_domain(realm,
118 domain,
119 NULL,
120 &ads->ldap.ss);
121 }
122 }
123#endif
124 break;
125 }
126
127 if (i == 3) {
128 DEBUG(1,("ads_dc_name: sitename (now \"%s\") keeps changing ???\n",
129 sitename ? sitename : ""));
130 TALLOC_FREE(sitename);
131 ads_destroy(&ads);
132 return False;
133 }
134
135 TALLOC_FREE(sitename);
136
137 fstrcpy(srv_name, ads->config.ldap_server_name);
138 if (!strupper_m(srv_name)) {
139 ads_destroy(&ads);
140 return false;
141 }
142#ifdef HAVE_ADS
143 *dc_ss = ads->ldap.ss;
144#else
145 zero_sockaddr(dc_ss);
146#endif
147 ads_destroy(&ads);
148
149 print_sockaddr(addr, sizeof(addr), dc_ss);
150 DEBUG(4,("ads_dc_name: using server='%s' IP=%s\n",
151 srv_name, addr));
152
153 return True;
154}
155
156/****************************************************************************
157 Utility function to return the name of a DC. The name is guaranteed to be
158 valid since we have already done a name_status_find on it
159 ***************************************************************************/
160
161static bool rpc_dc_name(const char *domain,
162 fstring srv_name,
163 struct sockaddr_storage *ss_out)
164{
165 struct ip_service *ip_list = NULL;
166 struct sockaddr_storage dc_ss;
167 int count, i;
168 NTSTATUS result;
169 char addr[INET6_ADDRSTRLEN];
170
171 /* get a list of all domain controllers */
172
173 if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, NULL, &ip_list, &count,
174 False))) {
175 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
176 return False;
177 }
178
179 /* Remove the entry we've already failed with (should be the PDC). */
180
181 for (i = 0; i < count; i++) {
182 if (is_zero_addr(&ip_list[i].ss))
183 continue;
184
185 if (name_status_find(domain, 0x1c, 0x20, &ip_list[i].ss, srv_name)) {
186 result = check_negative_conn_cache( domain, srv_name );
187 if ( NT_STATUS_IS_OK(result) ) {
188 dc_ss = ip_list[i].ss;
189 goto done;
190 }
191 }
192 }
193
194 SAFE_FREE(ip_list);
195
196 /* No-one to talk to )-: */
197 return False; /* Boo-hoo */
198
199 done:
200 /* We have the netbios name and IP address of a domain controller.
201 Ideally we should sent a SAMLOGON request to determine whether
202 the DC is alive and kicking. If we can catch a dead DC before
203 performing a cli_connect() we can avoid a 30-second timeout. */
204
205 print_sockaddr(addr, sizeof(addr), &dc_ss);
206 DEBUG(3, ("rpc_dc_name: Returning DC %s (%s) for domain %s\n", srv_name,
207 addr, domain));
208
209 *ss_out = dc_ss;
210 SAFE_FREE(ip_list);
211
212 return True;
213}
214
215/**********************************************************************
216 wrapper around ads and rpc methods of finds DC's
217**********************************************************************/
218
219bool get_dc_name(const char *domain,
220 const char *realm,
221 fstring srv_name,
222 struct sockaddr_storage *ss_out)
223{
224 struct sockaddr_storage dc_ss;
225 bool ret;
226 bool our_domain = False;
227
228 zero_sockaddr(&dc_ss);
229
230 ret = False;
231
232 if ( strequal(lp_workgroup(), domain) || strequal(lp_realm(), realm) )
233 our_domain = True;
234
235 /* always try to obey what the admin specified in smb.conf
236 (for the local domain) */
237
238 if ( (our_domain && lp_security()==SEC_ADS) || realm ) {
239 ret = ads_dc_name(domain, realm, &dc_ss, srv_name);
240 }
241
242 if (!domain) {
243 /* if we have only the realm we can't do anything else */
244 return False;
245 }
246
247 if (!ret) {
248 /* fall back on rpc methods if the ADS methods fail */
249 ret = rpc_dc_name(domain, srv_name, &dc_ss);
250 }
251
252 *ss_out = dc_ss;
253
254 return ret;
255}
Note: See TracBrowser for help on using the repository browser.