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