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.
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
36 struct smbc_server_cache
{
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
))) {
60 DEBUG(3, ("Not enough space for server cache allocation\n"));
64 ZERO_STRUCTP(srvcache
);
66 srvcache
->server
= newsrv
;
68 srvcache
->server_name
= SMB_STRDUP(server
);
69 if (!srvcache
->server_name
) {
74 srvcache
->share_name
= SMB_STRDUP(share
);
75 if (!srvcache
->share_name
) {
80 srvcache
->workgroup
= SMB_STRDUP(workgroup
);
81 if (!srvcache
->workgroup
) {
86 srvcache
->username
= SMB_STRDUP(username
);
87 if (!srvcache
->username
) {
92 DLIST_ADD((context
->server_cache
), srvcache
);
96 SAFE_FREE(srvcache
->server_name
);
97 SAFE_FREE(srvcache
->share_name
);
98 SAFE_FREE(srvcache
->workgroup
);
99 SAFE_FREE(srvcache
->username
);
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) {
130 * We only return an empty share name or the attribute
131 * server on an exact match (which would have been
134 if (*share
== '\0' || strcmp(share
, "*IPC$") == 0)
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)
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
);
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
) {
173 cli_shutdown(srv
->server
->cli
);
174 srv
->server
->cli
= NULL
;
175 context
->callbacks
.remove_cached_srv_fn(context
, srv
->server
);
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
);
211 /* server not found */
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
);
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
;