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