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