* s/get_dc_name/rpc_dc_name/g (revert a previous change)
[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    
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. The name is guaranteed to be 
160  valid since we have already done a name_status_find on it 
161  ***************************************************************************/
162
163 BOOL rpc_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out)
164 {
165         struct in_addr *ip_list = NULL, dc_ip, exclude_ip;
166         int count, i;
167         BOOL list_ordered;
168         BOOL use_pdc_only;
169         NTSTATUS result;
170         
171         zero_ip(&exclude_ip);
172
173         use_pdc_only = must_use_pdc(domain);
174         
175         /* Lookup domain controller name */
176            
177         if ( use_pdc_only && get_pdc_ip(domain, &dc_ip) ) 
178         {
179                 DEBUG(10,("rpc_dc_name: Atempting to lookup PDC to avoid sam sync delays\n"));
180                 
181                 /* check the connection cache and perform the node status 
182                    lookup only if the IP is not found to be bad */
183
184                 if (name_status_find(domain, 0x1c, 0x20, dc_ip, srv_name) ) {
185                         result = check_negative_conn_cache( domain, srv_name );
186                         if ( NT_STATUS_IS_OK(result) )
187                                 goto done;
188                 }
189                 /* Didn't get name, remember not to talk to this DC. */
190                 exclude_ip = dc_ip;
191         }
192
193         /* get a list of all domain controllers */
194         
195         if (!get_dc_list( domain, &ip_list, &count, &list_ordered) ) {
196                 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
197                 return False;
198         }
199
200         /* Remove the entry we've already failed with (should be the PDC). */
201
202         if ( use_pdc_only ) {
203                 for (i = 0; i < count; i++) {   
204                         if (ip_equal( exclude_ip, ip_list[i]))
205                                 zero_ip(&ip_list[i]);
206                 }
207         }
208
209         /* Pick a nice close server, but only if the list was not ordered */
210         
211         if (!list_ordered && (count > 1) ) {
212                 qsort(ip_list, count, sizeof(struct in_addr), QSORT_CAST ip_compare);
213         }
214
215         for (i = 0; i < count; i++) {
216                 if (is_zero_ip(ip_list[i]))
217                         continue;
218
219                 if (name_status_find(domain, 0x1c, 0x20, ip_list[i], srv_name)) {
220                         result = check_negative_conn_cache( domain, srv_name );
221                         if ( NT_STATUS_IS_OK(result) ) {
222                                 dc_ip = ip_list[i];
223                                 goto done;
224                         }
225                 }
226         }
227         
228
229         SAFE_FREE(ip_list);
230
231         /* No-one to talk to )-: */
232         return False;           /* Boo-hoo */
233         
234  done:
235         /* We have the netbios name and IP address of a domain controller.
236            Ideally we should sent a SAMLOGON request to determine whether
237            the DC is alive and kicking.  If we can catch a dead DC before
238            performing a cli_connect() we can avoid a 30-second timeout. */
239
240         DEBUG(3, ("rpc_dc_name: Returning DC %s (%s) for domain %s\n", srv_name,
241                   inet_ntoa(dc_ip), domain));
242
243         *ip_out = dc_ip;
244
245         SAFE_FREE(ip_list);
246
247         return True;
248 }