r20296: If we're going to overwrite krb5.conf
[amitay/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 2 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, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25
26 #include "includes.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 in_addr *dc_ip,
53                         fstring srv_name)
54 {
55         ADS_STRUCT *ads;
56         char *sitename = sitename_fetch();
57         int i;
58
59         if (!realm && strequal(domain, lp_workgroup())) {
60                 realm = lp_realm();
61         }
62
63         /* Try this 3 times then give up. */
64         for( i =0 ; i < 3; i++) {
65                 ads = ads_init(realm, domain, NULL);
66                 if (!ads) {
67                         SAFE_FREE(sitename);
68                         return False;
69                 }
70
71                 DEBUG(4,("ads_dc_name: domain=%s\n", domain));
72
73 #ifdef HAVE_ADS
74                 /* we don't need to bind, just connect */
75                 ads->auth.flags |= ADS_AUTH_NO_BIND;
76                 ads_connect(ads);
77 #endif
78
79                 if (!ads->config.realm) {
80                         SAFE_FREE(sitename);
81                         ads_destroy(&ads);
82                         return False;
83                 }
84
85                 /* Now we've found a server, see if our sitename
86                    has changed. If so, we need to re-do the DNS query
87                    to ensure we only find servers in our site. */
88
89                 if (stored_sitename_changed(sitename)) {
90                         SAFE_FREE(sitename);
91                         sitename = sitename_fetch();
92                         ads_destroy(&ads);
93                         /* Ensure we don't cache the DC we just connected to. */
94                         namecache_delete(realm, 0x1C);
95                         namecache_delete(domain, 0x1C);
96                         continue;
97                 }
98
99 #ifdef HAVE_KRB5
100                 if (is_our_primary_domain(domain) && (ads->config.flags & ADS_KDC) && ads_closest_dc(ads)) {
101                         /* We're going to use this KDC for this realm/domain.
102                            If we are using sites, then force the krb5 libs
103                            to use this KDC. */
104
105                         create_local_private_krb5_conf_for_domain(realm,
106                                                                 domain,
107                                                                 ads->ldap_ip);
108                 }
109 #endif
110                 break;
111         }
112
113         if (i == 3) {
114                 DEBUG(1,("ads_dc_name: sitename (now \"%s\") keeps changing ???\n",
115                         sitename ? sitename : ""));
116                 SAFE_FREE(sitename);
117                 return False;
118         }
119
120         SAFE_FREE(sitename);
121
122         fstrcpy(srv_name, ads->config.ldap_server_name);
123         strupper_m(srv_name);
124         *dc_ip = ads->ldap_ip;
125         ads_destroy(&ads);
126         
127         DEBUG(4,("ads_dc_name: using server='%s' IP=%s\n",
128                  srv_name, inet_ntoa(*dc_ip)));
129         
130         return True;
131 }
132
133 /****************************************************************************
134  Utility function to return the name of a DC. The name is guaranteed to be 
135  valid since we have already done a name_status_find on it 
136  ***************************************************************************/
137
138 static BOOL rpc_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out)
139 {
140         struct ip_service *ip_list = NULL;
141         struct in_addr dc_ip, exclude_ip;
142         int count, i;
143         NTSTATUS result;
144         
145         zero_ip(&exclude_ip);
146
147         /* get a list of all domain controllers */
148         
149         if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, &ip_list, &count,
150                                                 False))) {
151                 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
152                 return False;
153         }
154
155         /* Remove the entry we've already failed with (should be the PDC). */
156
157         for (i = 0; i < count; i++) {
158                 if (is_zero_ip(ip_list[i].ip))
159                         continue;
160
161                 if (name_status_find(domain, 0x1c, 0x20, ip_list[i].ip, srv_name)) {
162                         result = check_negative_conn_cache( domain, srv_name );
163                         if ( NT_STATUS_IS_OK(result) ) {
164                                 dc_ip = ip_list[i].ip;
165                                 goto done;
166                         }
167                 }
168         }
169         
170
171         SAFE_FREE(ip_list);
172
173         /* No-one to talk to )-: */
174         return False;           /* Boo-hoo */
175         
176  done:
177         /* We have the netbios name and IP address of a domain controller.
178            Ideally we should sent a SAMLOGON request to determine whether
179            the DC is alive and kicking.  If we can catch a dead DC before
180            performing a cli_connect() we can avoid a 30-second timeout. */
181
182         DEBUG(3, ("rpc_dc_name: Returning DC %s (%s) for domain %s\n", srv_name,
183                   inet_ntoa(dc_ip), domain));
184
185         *ip_out = dc_ip;
186
187         SAFE_FREE(ip_list);
188
189         return True;
190 }
191
192 /**********************************************************************
193  wrapper around ads and rpc methods of finds DC's
194 **********************************************************************/
195
196 BOOL get_dc_name(const char *domain, const char *realm, fstring srv_name, struct in_addr *ip_out)
197 {
198         struct in_addr dc_ip;
199         BOOL ret;
200         BOOL our_domain = False;
201
202         zero_ip(&dc_ip);
203
204         ret = False;
205         
206         if ( strequal(lp_workgroup(), domain) || strequal(lp_realm(), realm) )
207                 our_domain = True;
208         
209         /* always try to obey what the admin specified in smb.conf 
210            (for the local domain) */
211         
212         if ( (our_domain && lp_security()==SEC_ADS) || realm ) {
213                 ret = ads_dc_name(domain, realm, &dc_ip, srv_name);
214         }
215         
216         if (!ret) {
217                 /* fall back on rpc methods if the ADS methods fail */
218                 ret = rpc_dc_name(domain, srv_name, &dc_ip);
219         }
220                 
221         *ip_out = dc_ip;
222
223         return ret;
224 }