| 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 (Jerry) 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 |
|
|---|
| 27 | #define CONNCACHE_ADDR 1
|
|---|
| 28 | #define CONNCACHE_NAME 2
|
|---|
| 29 |
|
|---|
| 30 | /* cache entry contains either a server name **or** and IP address as
|
|---|
| 31 | the key. This means that a server could have two entries (one for each key) */
|
|---|
| 32 |
|
|---|
| 33 | struct failed_connection_cache {
|
|---|
| 34 | fstring domain_name;
|
|---|
| 35 | fstring controller;
|
|---|
| 36 | time_t lookup_time;
|
|---|
| 37 | NTSTATUS nt_status;
|
|---|
| 38 | struct failed_connection_cache *prev, *next;
|
|---|
| 39 | };
|
|---|
| 40 |
|
|---|
| 41 | static struct failed_connection_cache *failed_connection_cache;
|
|---|
| 42 |
|
|---|
| 43 | /**********************************************************************
|
|---|
| 44 | Check for a previously failed connection.
|
|---|
| 45 | failed_cache_timeout is an a absolute number of seconds after which
|
|---|
| 46 | we should time this out. If failed_cache_timeout == 0 then time out
|
|---|
| 47 | immediately. If failed_cache_timeout == -1 then never time out.
|
|---|
| 48 | **********************************************************************/
|
|---|
| 49 |
|
|---|
| 50 | NTSTATUS check_negative_conn_cache_timeout( const char *domain, const char *server, unsigned int failed_cache_timeout )
|
|---|
| 51 | {
|
|---|
| 52 | struct failed_connection_cache *fcc;
|
|---|
| 53 | NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
|
|---|
| 54 |
|
|---|
| 55 | /* can't check if we don't have strings */
|
|---|
| 56 |
|
|---|
| 57 | if ( !domain || !server )
|
|---|
| 58 | return NT_STATUS_OK;
|
|---|
| 59 |
|
|---|
| 60 | for (fcc = failed_connection_cache; fcc; fcc = fcc->next) {
|
|---|
| 61 |
|
|---|
| 62 | if (!(strequal(domain, fcc->domain_name) && strequal(server, fcc->controller))) {
|
|---|
| 63 | continue; /* no match; check the next entry */
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /* we have a match so see if it is still current */
|
|---|
| 67 | if (failed_cache_timeout != (unsigned int)-1) {
|
|---|
| 68 | if (failed_cache_timeout == 0 ||
|
|---|
| 69 | (time(NULL) - fcc->lookup_time) > (time_t)failed_cache_timeout) {
|
|---|
| 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 |
|
|---|
| 82 | /* The timeout hasn't expired yet so return false */
|
|---|
| 83 |
|
|---|
| 84 | DEBUG(10, ("check_negative_conn_cache: returning negative entry for %s, %s\n",
|
|---|
| 85 | domain, server ));
|
|---|
| 86 |
|
|---|
| 87 | result = fcc->nt_status;
|
|---|
| 88 | return result;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /* end of function means no cache entry */
|
|---|
| 92 | return NT_STATUS_OK;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | NTSTATUS check_negative_conn_cache( const char *domain, const char *server)
|
|---|
| 96 | {
|
|---|
| 97 | return check_negative_conn_cache_timeout(domain, server, FAILED_CONNECTION_CACHE_TIMEOUT);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /**********************************************************************
|
|---|
| 101 | Add an entry to the failed conneciton cache (aither a name of dotted
|
|---|
| 102 | decimal IP
|
|---|
| 103 | **********************************************************************/
|
|---|
| 104 |
|
|---|
| 105 | void add_failed_connection_entry(const char *domain, const char *server, NTSTATUS result)
|
|---|
| 106 | {
|
|---|
| 107 | struct failed_connection_cache *fcc;
|
|---|
| 108 |
|
|---|
| 109 | SMB_ASSERT(!NT_STATUS_IS_OK(result));
|
|---|
| 110 |
|
|---|
| 111 | /* Check we already aren't in the cache. We always have to have
|
|---|
| 112 | a domain, but maybe not a specific DC name. */
|
|---|
| 113 |
|
|---|
| 114 | for (fcc = failed_connection_cache; fcc; fcc = fcc->next) {
|
|---|
| 115 | if ( strequal(fcc->domain_name, domain) && strequal(fcc->controller, server) ) {
|
|---|
| 116 | DEBUG(10, ("add_failed_connection_entry: domain %s (%s) already tried and failed\n",
|
|---|
| 117 | domain, server ));
|
|---|
| 118 | /* Update the failed time. */
|
|---|
| 119 | fcc->lookup_time = time(NULL);
|
|---|
| 120 | return;
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /* Create negative lookup cache entry for this domain and controller */
|
|---|
| 125 |
|
|---|
| 126 | if ( !(fcc = SMB_MALLOC_P(struct failed_connection_cache)) ) {
|
|---|
| 127 | DEBUG(0, ("malloc failed in add_failed_connection_entry!\n"));
|
|---|
| 128 | return;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | ZERO_STRUCTP(fcc);
|
|---|
| 132 |
|
|---|
| 133 | fstrcpy( fcc->domain_name, domain );
|
|---|
| 134 | fstrcpy( fcc->controller, server );
|
|---|
| 135 | fcc->lookup_time = time(NULL);
|
|---|
| 136 | fcc->nt_status = result;
|
|---|
| 137 |
|
|---|
| 138 | DEBUG(10,("add_failed_connection_entry: added domain %s (%s) to failed conn cache\n",
|
|---|
| 139 | domain, server ));
|
|---|
| 140 |
|
|---|
| 141 | DLIST_ADD(failed_connection_cache, fcc);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /****************************************************************************
|
|---|
| 145 | ****************************************************************************/
|
|---|
| 146 |
|
|---|
| 147 | void flush_negative_conn_cache( void )
|
|---|
| 148 | {
|
|---|
| 149 | struct failed_connection_cache *fcc;
|
|---|
| 150 |
|
|---|
| 151 | fcc = failed_connection_cache;
|
|---|
| 152 |
|
|---|
| 153 | while (fcc) {
|
|---|
| 154 | struct failed_connection_cache *fcc_next;
|
|---|
| 155 |
|
|---|
| 156 | fcc_next = fcc->next;
|
|---|
| 157 | DLIST_REMOVE(failed_connection_cache, fcc);
|
|---|
| 158 | free(fcc);
|
|---|
| 159 |
|
|---|
| 160 | fcc = fcc_next;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /****************************************************************************
|
|---|
| 166 | Remove all negative entries for a domain. Used when going to online state in
|
|---|
| 167 | winbindd.
|
|---|
| 168 | ****************************************************************************/
|
|---|
| 169 |
|
|---|
| 170 | void flush_negative_conn_cache_for_domain(const char *domain)
|
|---|
| 171 | {
|
|---|
| 172 | struct failed_connection_cache *fcc;
|
|---|
| 173 |
|
|---|
| 174 | fcc = failed_connection_cache;
|
|---|
| 175 |
|
|---|
| 176 | while (fcc) {
|
|---|
| 177 | struct failed_connection_cache *fcc_next;
|
|---|
| 178 |
|
|---|
| 179 | fcc_next = fcc->next;
|
|---|
| 180 |
|
|---|
| 181 | if (strequal(fcc->domain_name, domain)) {
|
|---|
| 182 | DEBUG(10,("flush_negative_conn_cache_for_domain: removed server %s "
|
|---|
| 183 | " from failed cache for domain %s\n",
|
|---|
| 184 | fcc->controller, domain));
|
|---|
| 185 | DLIST_REMOVE(failed_connection_cache, fcc);
|
|---|
| 186 | free(fcc);
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | fcc = fcc_next;
|
|---|
| 190 | }
|
|---|
| 191 | }
|
|---|