Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / source / libsmb / libsmb_cache.c
blob5d948ea5e25dd77512e349fc69e317c308e7a467
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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
27 #include "include/libsmbclient.h"
28 #include "../include/libsmb_internal.h"
30 * Structure we use if internal caching mechanism is used
31 * nothing fancy here.
33 struct smbc_server_cache {
34 char *server_name;
35 char *share_name;
36 char *workgroup;
37 char *username;
38 SMBCSRV *server;
40 struct smbc_server_cache *next, *prev;
46 * Add a new connection to the server cache.
47 * This function is only used if the external cache is not enabled
49 static int smbc_add_cached_server(SMBCCTX * context, SMBCSRV * newsrv,
50 const char * server, const char * share,
51 const char * workgroup, const char * username)
53 struct smbc_server_cache * srvcache = NULL;
55 if (!(srvcache = SMB_MALLOC_P(struct smbc_server_cache))) {
56 errno = ENOMEM;
57 DEBUG(3, ("Not enough space for server cache allocation\n"));
58 return 1;
61 ZERO_STRUCTP(srvcache);
63 srvcache->server = newsrv;
65 srvcache->server_name = SMB_STRDUP(server);
66 if (!srvcache->server_name) {
67 errno = ENOMEM;
68 goto failed;
71 srvcache->share_name = SMB_STRDUP(share);
72 if (!srvcache->share_name) {
73 errno = ENOMEM;
74 goto failed;
77 srvcache->workgroup = SMB_STRDUP(workgroup);
78 if (!srvcache->workgroup) {
79 errno = ENOMEM;
80 goto failed;
83 srvcache->username = SMB_STRDUP(username);
84 if (!srvcache->username) {
85 errno = ENOMEM;
86 goto failed;
89 DLIST_ADD((context->server_cache), srvcache);
90 return 0;
92 failed:
93 SAFE_FREE(srvcache->server_name);
94 SAFE_FREE(srvcache->share_name);
95 SAFE_FREE(srvcache->workgroup);
96 SAFE_FREE(srvcache->username);
97 SAFE_FREE(srvcache);
99 return 1;
105 * Search the server cache for a server
106 * returns server handle on success, NULL on error (not found)
107 * This function is only used if the external cache is not enabled
109 static SMBCSRV * smbc_get_cached_server(SMBCCTX * context, const char * server,
110 const char * share, const char * workgroup, const char * user)
112 struct smbc_server_cache * srv = NULL;
114 /* Search the cache lines */
115 for (srv=((struct smbc_server_cache *)context->server_cache);srv;srv=srv->next) {
117 if (strcmp(server,srv->server_name) == 0 &&
118 strcmp(workgroup,srv->workgroup) == 0 &&
119 strcmp(user, srv->username) == 0) {
121 /* If the share name matches, we're cool */
122 if (strcmp(share, srv->share_name) == 0) {
123 return srv->server;
127 * We only return an empty share name or the attribute
128 * server on an exact match (which would have been
129 * caught above).
131 if (*share == '\0' || strcmp(share, "*IPC$") == 0)
132 continue;
135 * Never return an empty share name or the attribute
136 * server if it wasn't what was requested.
138 if (*srv->share_name == '\0' ||
139 strcmp(srv->share_name, "*IPC$") == 0)
140 continue;
143 * If we're only allowing one share per server, then
144 * a connection to the server (other than the
145 * attribute server connection) is cool.
147 if (context->options.one_share_per_server) {
149 * The currently connected share name
150 * doesn't match the requested share, so
151 * disconnect from the current share.
153 if (! cli_tdis(&srv->server->cli)) {
154 /* Sigh. Couldn't disconnect. */
155 cli_shutdown(&srv->server->cli);
156 context->callbacks.remove_cached_srv_fn(context, srv->server);
157 continue;
161 * Save the new share name. We've
162 * disconnected from the old share, and are
163 * about to connect to the new one.
165 SAFE_FREE(srv->share_name);
166 srv->share_name = SMB_STRDUP(share);
167 if (!srv->share_name) {
168 /* Out of memory. */
169 cli_shutdown(&srv->server->cli);
170 context->callbacks.remove_cached_srv_fn(context, srv->server);
171 continue;
175 return srv->server;
180 return NULL;
185 * Search the server cache for a server and remove it
186 * returns 0 on success
187 * This function is only used if the external cache is not enabled
189 static int smbc_remove_cached_server(SMBCCTX * context, SMBCSRV * server)
191 struct smbc_server_cache * srv = NULL;
193 for (srv=((struct smbc_server_cache *)context->server_cache);srv;srv=srv->next) {
194 if (server == srv->server) {
196 /* remove this sucker */
197 DLIST_REMOVE(context->server_cache, srv);
198 SAFE_FREE(srv->server_name);
199 SAFE_FREE(srv->share_name);
200 SAFE_FREE(srv->workgroup);
201 SAFE_FREE(srv->username);
202 SAFE_FREE(srv);
203 return 0;
206 /* server not found */
207 return 1;
212 * Try to remove all the servers in cache
213 * returns 1 on failure and 0 if all servers could be removed.
215 static int smbc_purge_cached(SMBCCTX * context)
217 struct smbc_server_cache * srv;
218 struct smbc_server_cache * next;
219 int could_not_purge_all = 0;
221 for (srv = ((struct smbc_server_cache *) context->server_cache),
222 next = (srv ? srv->next :NULL);
223 srv;
224 srv = next, next = (srv ? srv->next : NULL)) {
226 if (smbc_remove_unused_server(context, srv->server)) {
227 /* could not be removed */
228 could_not_purge_all = 1;
231 return could_not_purge_all;
237 * This functions initializes all server-cache related functions
238 * to the default (internal) system.
240 * We use this to make the rest of the cache system static.
243 int smbc_default_cache_functions(SMBCCTX * context)
245 context->callbacks.add_cached_srv_fn = smbc_add_cached_server;
246 context->callbacks.get_cached_srv_fn = smbc_get_cached_server;
247 context->callbacks.remove_cached_srv_fn = smbc_remove_cached_server;
248 context->callbacks.purge_cached_fn = smbc_purge_cached;
250 return 0;