2 Unix SMB/CIFS implementation.
4 Winbind daemon connection manager
6 Copyright (C) Tim Potter 2001
7 Copyright (C) Andrew Bartlett 2002
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.
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.
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.
28 #define FAILED_CONNECTION_CACHE_TIMEOUT 30 /* Seconds between attempts */
30 struct failed_connection_cache {
35 struct failed_connection_cache *prev, *next;
38 static struct failed_connection_cache *failed_connection_cache;
40 /**********************************************************************
41 Check for a previously failed connection
42 **********************************************************************/
44 static NTSTATUS check_negative_conn_cache( const char *domain, const char *server )
46 struct failed_connection_cache *fcc;
47 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
49 /* can't check if we don't have strings */
51 if ( !domain || !server )
54 for (fcc = failed_connection_cache; fcc; fcc = fcc->next) {
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
63 if ( !strequal(domain, fcc->domain_name) || !strequal(server, fcc->controller) )
64 continue; /* no match; check the next entry */
66 /* we have a match so see if it is still current */
68 if ((time(NULL) - fcc->lookup_time) > FAILED_CONNECTION_CACHE_TIMEOUT)
70 /* Cache entry has expired, delete it */
72 DEBUG(10, ("check_negative_conn_cache: cache entry expired for %s, %s\n",
75 DLIST_REMOVE(failed_connection_cache, fcc);
81 /* The timeout hasn't expired yet so return false */
83 DEBUG(10, ("check_negative_conn_cache: returning negative entry for %s, %s\n",
86 result = fcc->nt_status;
90 /* end of function means no cache entry */
94 /**********************************************************************
95 Add an entry to the failed conneciton cache
96 **********************************************************************/
98 void add_failed_connection_entry(const char *domain, const char *server, NTSTATUS result)
100 struct failed_connection_cache *fcc;
102 SMB_ASSERT(!NT_STATUS_IS_OK(result));
104 /* Check we already aren't in the cache. We always have to have
105 a domain, but maybe not a specific DC name. */
107 for (fcc = failed_connection_cache; fcc; fcc = fcc->next) {
108 if ( strequal(fcc->domain_name, domain) && strequal(fcc->controller, server) )
110 DEBUG(10, ("add_failed_connection_entry: domain %s (%s) already tried and failed\n",
116 /* Create negative lookup cache entry for this domain and controller */
118 if ( !(fcc = (struct failed_connection_cache *)malloc(sizeof(struct failed_connection_cache))) )
120 DEBUG(0, ("malloc failed in add_failed_connection_entry!\n"));
126 fstrcpy( fcc->domain_name, domain );
127 fstrcpy( fcc->controller, server );
128 fcc->lookup_time = time(NULL);
129 fcc->nt_status = result;
131 DEBUG(10,("add_failed_connection_entry: added domain %s (%s) to failed conn cache\n",
134 DLIST_ADD(failed_connection_cache, fcc);
137 /****************************************************************************
138 ****************************************************************************/
140 void flush_negative_conn_cache( void )
142 struct failed_connection_cache *fcc;
144 fcc = failed_connection_cache;
147 struct failed_connection_cache *fcc_next;
149 fcc_next = fcc->next;
150 DLIST_REMOVE(failed_connection_cache, fcc);
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 ***************************************************************************/
164 BOOL rpc_find_dc(const char *domain, fstring srv_name, struct in_addr *ip_out)
166 struct in_addr *ip_list = NULL, dc_ip, exclude_ip;
172 zero_ip(&exclude_ip);
174 use_pdc_only = must_use_pdc(domain);
176 /* Lookup domain controller name */
178 if ( use_pdc_only && get_pdc_ip(domain, &dc_ip) )
180 DEBUG(10,("rpc_find_dc: Atempting to lookup PDC to avoid sam sync delays\n"));
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) )
188 /* Didn't get name, remember not to talk to this DC. */
192 /* get a list of all domain controllers */
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));
199 /* Remove the entry we've already failed with (should be the PDC). */
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]);
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);
213 for (i = 0; i < count; i++) {
214 if (is_zero_ip(ip_list[i]))
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) ) {
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. */
236 DEBUG(3, ("rpc_find_dc: Returning DC %s (%s) for domain %s\n", srv_name,
237 inet_ntoa(dc_ip), domain));