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