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/>.
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
35 struct smbc_server_cache
{
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
))) {
59 DEBUG(3, ("Not enough space for server cache allocation\n"));
63 ZERO_STRUCTP(srvcache
);
65 srvcache
->server
= newsrv
;
67 srvcache
->server_name
= SMB_STRDUP(server
);
68 if (!srvcache
->server_name
) {
73 srvcache
->share_name
= SMB_STRDUP(share
);
74 if (!srvcache
->share_name
) {
79 srvcache
->workgroup
= SMB_STRDUP(workgroup
);
80 if (!srvcache
->workgroup
) {
85 srvcache
->username
= SMB_STRDUP(username
);
86 if (!srvcache
->username
) {
91 DLIST_ADD((context
->server_cache
), srvcache
);
95 SAFE_FREE(srvcache
->server_name
);
96 SAFE_FREE(srvcache
->share_name
);
97 SAFE_FREE(srvcache
->workgroup
);
98 SAFE_FREE(srvcache
->username
);
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) {
129 * We only return an empty share name or the attribute
130 * server on an exact match (which would have been
133 if (*share
== '\0' || strcmp(share
, "*IPC$") == 0)
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)
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
);
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
) {
172 cli_shutdown(srv
->server
->cli
);
173 srv
->server
->cli
= NULL
;
174 context
->callbacks
.remove_cached_srv_fn(context
, srv
->server
);
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
);
210 /* server not found */
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
);
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
;