r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / libsmb / libsmb_cache.c
blobb98df024faa9ca2b4566e39c1e47d817ecafef43
2 /*
3 Unix SMB/CIFS implementation.
4 SMB client library implementation (server cache)
5 Copyright (C) Andrew Tridgell 1998
6 Copyright (C) Richard Sharpe 2000
7 Copyright (C) John Terpstra 2000
8 Copyright (C) Tom Jansen (Ninja ISD) 2002
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.
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.
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/>.
24 #include "includes.h"
26 #include "include/libsmbclient.h"
27 #include "../include/libsmb_internal.h"
29 int smbc_default_cache_functions(SMBCCTX * context);
32 * Structure we use if internal caching mechanism is used
33 * nothing fancy here.
35 struct smbc_server_cache {
36 char *server_name;
37 char *share_name;
38 char *workgroup;
39 char *username;
40 SMBCSRV *server;
42 struct smbc_server_cache *next, *prev;
48 * Add a new connection to the server cache.
49 * This function is only used if the external cache is not enabled
51 static int smbc_add_cached_server(SMBCCTX * context, SMBCSRV * newsrv,
52 const char * server, const char * share,
53 const char * workgroup, const char * username)
55 struct smbc_server_cache * srvcache = NULL;
57 if (!(srvcache = SMB_MALLOC_P(struct smbc_server_cache))) {
58 errno = ENOMEM;
59 DEBUG(3, ("Not enough space for server cache allocation\n"));
60 return 1;
63 ZERO_STRUCTP(srvcache);
65 srvcache->server = newsrv;
67 srvcache->server_name = SMB_STRDUP(server);
68 if (!srvcache->server_name) {
69 errno = ENOMEM;
70 goto failed;
73 srvcache->share_name = SMB_STRDUP(share);
74 if (!srvcache->share_name) {
75 errno = ENOMEM;
76 goto failed;
79 srvcache->workgroup = SMB_STRDUP(workgroup);
80 if (!srvcache->workgroup) {
81 errno = ENOMEM;
82 goto failed;
85 srvcache->username = SMB_STRDUP(username);
86 if (!srvcache->username) {
87 errno = ENOMEM;
88 goto failed;
91 DLIST_ADD((context->server_cache), srvcache);
92 return 0;
94 failed:
95 SAFE_FREE(srvcache->server_name);
96 SAFE_FREE(srvcache->share_name);
97 SAFE_FREE(srvcache->workgroup);
98 SAFE_FREE(srvcache->username);
99 SAFE_FREE(srvcache);
101 return 1;
107 * Search the server cache for a server
108 * returns server handle on success, NULL on error (not found)
109 * This function is only used if the external cache is not enabled
111 static SMBCSRV * smbc_get_cached_server(SMBCCTX * context, const char * server,
112 const char * share, const char * workgroup, const char * user)
114 struct smbc_server_cache * srv = NULL;
116 /* Search the cache lines */
117 for (srv=((struct smbc_server_cache *)context->server_cache);srv;srv=srv->next) {
119 if (strcmp(server,srv->server_name) == 0 &&
120 strcmp(workgroup,srv->workgroup) == 0 &&
121 strcmp(user, srv->username) == 0) {
123 /* If the share name matches, we're cool */
124 if (strcmp(share, srv->share_name) == 0) {
125 return srv->server;
129 * We only return an empty share name or the attribute
130 * server on an exact match (which would have been
131 * caught above).
133 if (*share == '\0' || strcmp(share, "*IPC$") == 0)
134 continue;
137 * Never return an empty share name or the attribute
138 * server if it wasn't what was requested.
140 if (*srv->share_name == '\0' ||
141 strcmp(srv->share_name, "*IPC$") == 0)
142 continue;
145 * If we're only allowing one share per server, then
146 * a connection to the server (other than the
147 * attribute server connection) is cool.
149 if (context->options.one_share_per_server) {
151 * The currently connected share name
152 * doesn't match the requested share, so
153 * disconnect from the current share.
155 if (! cli_tdis(srv->server->cli)) {
156 /* Sigh. Couldn't disconnect. */
157 cli_shutdown(srv->server->cli);
158 srv->server->cli = NULL;
159 context->callbacks.remove_cached_srv_fn(context, srv->server);
160 continue;
164 * Save the new share name. We've
165 * disconnected from the old share, and are
166 * about to connect to the new one.
168 SAFE_FREE(srv->share_name);
169 srv->share_name = SMB_STRDUP(share);
170 if (!srv->share_name) {
171 /* Out of memory. */
172 cli_shutdown(srv->server->cli);
173 srv->server->cli = NULL;
174 context->callbacks.remove_cached_srv_fn(context, srv->server);
175 continue;
179 return srv->server;
184 return NULL;
189 * Search the server cache for a server and remove it
190 * returns 0 on success
191 * This function is only used if the external cache is not enabled
193 static int smbc_remove_cached_server(SMBCCTX * context, SMBCSRV * server)
195 struct smbc_server_cache * srv = NULL;
197 for (srv=((struct smbc_server_cache *)context->server_cache);srv;srv=srv->next) {
198 if (server == srv->server) {
200 /* remove this sucker */
201 DLIST_REMOVE(context->server_cache, srv);
202 SAFE_FREE(srv->server_name);
203 SAFE_FREE(srv->share_name);
204 SAFE_FREE(srv->workgroup);
205 SAFE_FREE(srv->username);
206 SAFE_FREE(srv);
207 return 0;
210 /* server not found */
211 return 1;
216 * Try to remove all the servers in cache
217 * returns 1 on failure and 0 if all servers could be removed.
219 static int smbc_purge_cached(SMBCCTX * context)
221 struct smbc_server_cache * srv;
222 struct smbc_server_cache * next;
223 int could_not_purge_all = 0;
225 for (srv = ((struct smbc_server_cache *) context->server_cache),
226 next = (srv ? srv->next :NULL);
227 srv;
228 srv = next, next = (srv ? srv->next : NULL)) {
230 if (smbc_remove_unused_server(context, srv->server)) {
231 /* could not be removed */
232 could_not_purge_all = 1;
235 return could_not_purge_all;
241 * This functions initializes all server-cache related functions
242 * to the default (internal) system.
244 * We use this to make the rest of the cache system static.
247 int smbc_default_cache_functions(SMBCCTX * context)
249 context->callbacks.add_cached_srv_fn = smbc_add_cached_server;
250 context->callbacks.get_cached_srv_fn = smbc_get_cached_server;
251 context->callbacks.remove_cached_srv_fn = smbc_remove_cached_server;
252 context->callbacks.purge_cached_fn = smbc_purge_cached;
254 return 0;