e98b728963ebe415b50a42e19755c89f3080412b
[ira/wip.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    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24
25 #include "includes.h"
26
27
28 #define FAILED_CONNECTION_CACHE_TIMEOUT 30 /* Seconds between attempts */
29
30 struct failed_connection_cache {
31         fstring domain_name;
32         fstring controller;
33         time_t lookup_time;
34         NTSTATUS nt_status;
35         struct failed_connection_cache *prev, *next;
36 };
37
38 static struct failed_connection_cache *failed_connection_cache;
39
40 /**********************************************************************
41  Check for a previously failed connection
42 **********************************************************************/
43
44 static NTSTATUS check_negative_conn_cache( const char *domain, const char *server )
45 {
46         struct failed_connection_cache *fcc;
47         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
48         
49         /* can't check if we don't have strings */
50         
51         if ( !domain || !server )
52                 return NT_STATUS_OK;
53
54         for (fcc = failed_connection_cache; fcc; fcc = fcc->next) {
55                 
56                 /* 
57                  * we have a match IFF the domain and server name matches
58                  *   (a) the domain matches, 
59                  *   (b) the IP address matches (if we have one)
60                  *   (c) the server name (if specified) matches
61                  */
62                  
63                 if ( !strequal(domain, fcc->domain_name) || !strequal(server, fcc->controller) )
64                         continue; /* no match; check the next entry */
65                 
66                 /* we have a match so see if it is still current */
67
68                 if ((time(NULL) - fcc->lookup_time) > FAILED_CONNECTION_CACHE_TIMEOUT) 
69                 {
70                         /* Cache entry has expired, delete it */
71
72                         DEBUG(10, ("check_negative_conn_cache: cache entry expired for %s, %s\n", 
73                                 domain, server ));
74
75                         DLIST_REMOVE(failed_connection_cache, fcc);
76                         SAFE_FREE(fcc);
77
78                         return NT_STATUS_OK;
79                 }
80
81                 /* The timeout hasn't expired yet so return false */
82
83                 DEBUG(10, ("check_negative_conn_cache: returning negative entry for %s, %s\n", 
84                         domain, server ));
85
86                 result = fcc->nt_status;
87                 return result;
88         }
89
90         /* end of function means no cache entry */      
91         return NT_STATUS_OK;
92 }
93
94 /**********************************************************************
95  Add an entry to the failed conneciton cache
96 **********************************************************************/
97
98 void add_failed_connection_entry(const char *domain, const char *server, NTSTATUS result) 
99 {
100         struct failed_connection_cache *fcc;
101
102         SMB_ASSERT(!NT_STATUS_IS_OK(result));
103
104         /* Check we already aren't in the cache.  We always have to have 
105            a domain, but maybe not a specific DC name. */
106
107         for (fcc = failed_connection_cache; fcc; fcc = fcc->next) {
108                 if ( strequal(fcc->domain_name, domain) && strequal(fcc->controller, server) ) 
109                 {
110                         DEBUG(10, ("add_failed_connection_entry: domain %s (%s) already tried and failed\n",
111                                    domain, server ));
112                         return;
113                 }
114         }
115
116         /* Create negative lookup cache entry for this domain and controller */
117
118         if ( !(fcc = (struct failed_connection_cache *)malloc(sizeof(struct failed_connection_cache))) ) 
119         {
120                 DEBUG(0, ("malloc failed in add_failed_connection_entry!\n"));
121                 return;
122         }
123         
124         ZERO_STRUCTP(fcc);
125         
126         fstrcpy( fcc->domain_name, domain );
127         fstrcpy( fcc->controller, server );
128         fcc->lookup_time = time(NULL);
129         fcc->nt_status = result;
130         
131         DEBUG(10,("add_failed_connection_entry: added domain %s (%s) to failed conn cache\n",
132                 domain, server ));
133         
134         DLIST_ADD(failed_connection_cache, fcc);
135 }
136
137 /****************************************************************************
138 ****************************************************************************/
139  
140 void flush_negative_conn_cache( void )
141 {
142         struct failed_connection_cache *fcc;
143         
144         fcc = failed_connection_cache;
145
146         while (fcc) {
147                 struct failed_connection_cache *fcc_next;
148
149                 fcc_next = fcc->next;
150                 DLIST_REMOVE(failed_connection_cache, fcc);
151                 free(fcc);
152
153                 fcc = fcc_next;
154         }
155
156 }
157
158 /****************************************************************************
159  Utility function to return the name of a DC using RPC. The name is 
160  guaranteed to be valid since we have already done a name_status_find on it 
161  and we have checked our negative connection cache
162  ***************************************************************************/
163  
164 BOOL rpc_find_dc(const char *domain, fstring srv_name, struct in_addr *ip_out)
165 {
166         struct in_addr *ip_list = NULL, dc_ip, exclude_ip;
167         int count, i;
168         BOOL list_ordered;
169         BOOL use_pdc_only;
170         NTSTATUS result;
171         
172         zero_ip(&exclude_ip);
173
174         use_pdc_only = must_use_pdc(domain);
175         
176         /* Lookup domain controller name */
177            
178         if ( use_pdc_only && get_pdc_ip(domain, &dc_ip) ) 
179         {
180                 DEBUG(10,("rpc_find_dc: Atempting to lookup PDC to avoid sam sync delays\n"));
181                 
182                 if (name_status_find(domain, 0x1c, 0x20, dc_ip, srv_name)) {
183                         /* makre we we haven't tried this on previously and failed */
184                         result = check_negative_conn_cache( domain, srv_name );
185                         if ( NT_STATUS_IS_OK(result) )
186                                 goto done;
187                 }
188                 /* Didn't get name, remember not to talk to this DC. */
189                 exclude_ip = dc_ip;
190         }
191
192         /* get a list of all domain controllers */
193         
194         if (!get_dc_list( domain, &ip_list, &count, &list_ordered) ) {
195                 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
196                 return False;
197         }
198
199         /* Remove the entry we've already failed with (should be the PDC). */
200
201         if ( use_pdc_only ) {
202                 for (i = 0; i < count; i++) {   
203                         if (ip_equal( exclude_ip, ip_list[i]))
204                                 zero_ip(&ip_list[i]);
205                 }
206         }
207
208         /* Pick a nice close server, but only if the list was not ordered */
209         if (!list_ordered && (count > 1) ) {
210                 qsort(ip_list, count, sizeof(struct in_addr), QSORT_CAST ip_compare);
211         }
212
213         for (i = 0; i < count; i++) {
214                 if (is_zero_ip(ip_list[i]))
215                         continue;
216
217                 if (name_status_find(domain, 0x1c, 0x20, ip_list[i], srv_name)) {
218                         result = check_negative_conn_cache( domain, srv_name );
219                         if ( NT_STATUS_IS_OK(result) ) {
220                                 dc_ip = ip_list[i];
221                                 goto done;
222                         }
223                 }
224         }
225
226
227         SAFE_FREE(ip_list);
228
229         return False;
230 done:
231         /* We have the netbios name and IP address of a domain controller.
232            Ideally we should sent a SAMLOGON request to determine whether
233            the DC is alive and kicking.  If we can catch a dead DC before
234            performing a cli_connect() we can avoid a 30-second timeout. */
235
236         DEBUG(3, ("rpc_find_dc: Returning DC %s (%s) for domain %s\n", srv_name,
237                   inet_ntoa(dc_ip), domain));
238
239         *ip_out = dc_ip;
240
241         SAFE_FREE(ip_list);
242
243         return True;
244 }
245