include updates
[tomato.git] / release / src-rt-6.x.4708 / router / samba-3.0.25b / source / libsmb / libsmb_cache.c
blobfdde7acaf869380912ac4f6138f8e3be8a7a32e8
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 int smbc_default_cache_functions(SMBCCTX * context);
33 * Structure we use if internal caching mechanism is used
34 * nothing fancy here.
36 struct smbc_server_cache {
37 char *server_name;
38 char *share_name;
39 char *workgroup;
40 char *username;
41 SMBCSRV *server;
43 struct smbc_server_cache *next, *prev;
49 * Add a new connection to the server cache.
50 * This function is only used if the external cache is not enabled
52 static int smbc_add_cached_server(SMBCCTX * context, SMBCSRV * newsrv,
53 const char * server, const char * share,
54 const char * workgroup, const char * username)
56 struct smbc_server_cache * srvcache = NULL;
58 if (!(srvcache = SMB_MALLOC_P(struct smbc_server_cache))) {
59 errno = ENOMEM;
60 DEBUG(3, ("Not enough space for server cache allocation\n"));
61 return 1;
64 ZERO_STRUCTP(srvcache);
66 srvcache->server = newsrv;
68 srvcache->server_name = SMB_STRDUP(server);
69 if (!srvcache->server_name) {
70 errno = ENOMEM;
71 goto failed;
74 srvcache->share_name = SMB_STRDUP(share);
75 if (!srvcache->share_name) {
76 errno = ENOMEM;
77 goto failed;
80 srvcache->workgroup = SMB_STRDUP(workgroup);
81 if (!srvcache->workgroup) {
82 errno = ENOMEM;
83 goto failed;
86 srvcache->username = SMB_STRDUP(username);
87 if (!srvcache->username) {
88 errno = ENOMEM;
89 goto failed;
92 DLIST_ADD((context->server_cache), srvcache);
93 return 0;
95 failed:
96 SAFE_FREE(srvcache->server_name);
97 SAFE_FREE(srvcache->share_name);
98 SAFE_FREE(srvcache->workgroup);
99 SAFE_FREE(srvcache->username);
100 SAFE_FREE(srvcache);
102 return 1;
108 * Search the server cache for a server
109 * returns server handle on success, NULL on error (not found)
110 * This function is only used if the external cache is not enabled
112 static SMBCSRV * smbc_get_cached_server(SMBCCTX * context, const char * server,
113 const char * share, const char * workgroup, const char * user)
115 struct smbc_server_cache * srv = NULL;
117 /* Search the cache lines */
118 for (srv=((struct smbc_server_cache *)context->server_cache);srv;srv=srv->next) {
120 if (strcmp(server,srv->server_name) == 0 &&
121 strcmp(workgroup,srv->workgroup) == 0 &&
122 strcmp(user, srv->username) == 0) {
124 /* If the share name matches, we're cool */
125 if (strcmp(share, srv->share_name) == 0) {
126 return srv->server;
130 * We only return an empty share name or the attribute
131 * server on an exact match (which would have been
132 * caught above).
134 if (*share == '\0' || strcmp(share, "*IPC$") == 0)
135 continue;
138 * Never return an empty share name or the attribute
139 * server if it wasn't what was requested.
141 if (*srv->share_name == '\0' ||
142 strcmp(srv->share_name, "*IPC$") == 0)
143 continue;
146 * If we're only allowing one share per server, then
147 * a connection to the server (other than the
148 * attribute server connection) is cool.
150 if (context->options.one_share_per_server) {
152 * The currently connected share name
153 * doesn't match the requested share, so
154 * disconnect from the current share.
156 if (! cli_tdis(srv->server->cli)) {
157 /* Sigh. Couldn't disconnect. */
158 cli_shutdown(srv->server->cli);
159 srv->server->cli = NULL;
160 context->callbacks.remove_cached_srv_fn(context, srv->server);
161 continue;
165 * Save the new share name. We've
166 * disconnected from the old share, and are
167 * about to connect to the new one.
169 SAFE_FREE(srv->share_name);
170 srv->share_name = SMB_STRDUP(share);
171 if (!srv->share_name) {
172 /* Out of memory. */
173 cli_shutdown(srv->server->cli);
174 srv->server->cli = NULL;
175 context->callbacks.remove_cached_srv_fn(context, srv->server);
176 continue;
180 return srv->server;
185 return NULL;
190 * Search the server cache for a server and remove it
191 * returns 0 on success
192 * This function is only used if the external cache is not enabled
194 static int smbc_remove_cached_server(SMBCCTX * context, SMBCSRV * server)
196 struct smbc_server_cache * srv = NULL;
198 for (srv=((struct smbc_server_cache *)context->server_cache);srv;srv=srv->next) {
199 if (server == srv->server) {
201 /* remove this sucker */
202 DLIST_REMOVE(context->server_cache, srv);
203 SAFE_FREE(srv->server_name);
204 SAFE_FREE(srv->share_name);
205 SAFE_FREE(srv->workgroup);
206 SAFE_FREE(srv->username);
207 SAFE_FREE(srv);
208 return 0;
211 /* server not found */
212 return 1;
217 * Try to remove all the servers in cache
218 * returns 1 on failure and 0 if all servers could be removed.
220 static int smbc_purge_cached(SMBCCTX * context)
222 struct smbc_server_cache * srv;
223 struct smbc_server_cache * next;
224 int could_not_purge_all = 0;
226 for (srv = ((struct smbc_server_cache *) context->server_cache),
227 next = (srv ? srv->next :NULL);
228 srv;
229 srv = next, next = (srv ? srv->next : NULL)) {
231 if (smbc_remove_unused_server(context, srv->server)) {
232 /* could not be removed */
233 could_not_purge_all = 1;
236 return could_not_purge_all;
242 * This functions initializes all server-cache related functions
243 * to the default (internal) system.
245 * We use this to make the rest of the cache system static.
248 int smbc_default_cache_functions(SMBCCTX * context)
250 context->callbacks.add_cached_srv_fn = smbc_add_cached_server;
251 context->callbacks.get_cached_srv_fn = smbc_get_cached_server;
252 context->callbacks.remove_cached_srv_fn = smbc_remove_cached_server;
253 context->callbacks.purge_cached_fn = smbc_purge_cached;
255 return 0;