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 * Structure we use if internal caching mechanism is used
33 struct smbc_server_cache
{
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
))) {
57 DEBUG(3, ("Not enough space for server cache allocation\n"));
61 ZERO_STRUCTP(srvcache
);
63 srvcache
->server
= newsrv
;
65 srvcache
->server_name
= SMB_STRDUP(server
);
66 if (!srvcache
->server_name
) {
71 srvcache
->share_name
= SMB_STRDUP(share
);
72 if (!srvcache
->share_name
) {
77 srvcache
->workgroup
= SMB_STRDUP(workgroup
);
78 if (!srvcache
->workgroup
) {
83 srvcache
->username
= SMB_STRDUP(username
);
84 if (!srvcache
->username
) {
89 DLIST_ADD((context
->server_cache
), srvcache
);
93 SAFE_FREE(srvcache
->server_name
);
94 SAFE_FREE(srvcache
->share_name
);
95 SAFE_FREE(srvcache
->workgroup
);
96 SAFE_FREE(srvcache
->username
);
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) {
127 * We only return an empty share name or the attribute
128 * server on an exact match (which would have been
131 if (*share
== '\0' || strcmp(share
, "*IPC$") == 0)
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)
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 srv
->server
->cli
= NULL
;
157 context
->callbacks
.remove_cached_srv_fn(context
, srv
->server
);
162 * Save the new share name. We've
163 * disconnected from the old share, and are
164 * about to connect to the new one.
166 SAFE_FREE(srv
->share_name
);
167 srv
->share_name
= SMB_STRDUP(share
);
168 if (!srv
->share_name
) {
170 cli_shutdown(srv
->server
->cli
);
171 srv
->server
->cli
= NULL
;
172 context
->callbacks
.remove_cached_srv_fn(context
, srv
->server
);
187 * Search the server cache for a server and remove it
188 * returns 0 on success
189 * This function is only used if the external cache is not enabled
191 static int smbc_remove_cached_server(SMBCCTX
* context
, SMBCSRV
* server
)
193 struct smbc_server_cache
* srv
= NULL
;
195 for (srv
=((struct smbc_server_cache
*)context
->server_cache
);srv
;srv
=srv
->next
) {
196 if (server
== srv
->server
) {
198 /* remove this sucker */
199 DLIST_REMOVE(context
->server_cache
, srv
);
200 SAFE_FREE(srv
->server_name
);
201 SAFE_FREE(srv
->share_name
);
202 SAFE_FREE(srv
->workgroup
);
203 SAFE_FREE(srv
->username
);
208 /* server not found */
214 * Try to remove all the servers in cache
215 * returns 1 on failure and 0 if all servers could be removed.
217 static int smbc_purge_cached(SMBCCTX
* context
)
219 struct smbc_server_cache
* srv
;
220 struct smbc_server_cache
* next
;
221 int could_not_purge_all
= 0;
223 for (srv
= ((struct smbc_server_cache
*) context
->server_cache
),
224 next
= (srv
? srv
->next
:NULL
);
226 srv
= next
, next
= (srv
? srv
->next
: NULL
)) {
228 if (smbc_remove_unused_server(context
, srv
->server
)) {
229 /* could not be removed */
230 could_not_purge_all
= 1;
233 return could_not_purge_all
;
239 * This functions initializes all server-cache related functions
240 * to the default (internal) system.
242 * We use this to make the rest of the cache system static.
245 int smbc_default_cache_functions(SMBCCTX
* context
)
247 context
->callbacks
.add_cached_srv_fn
= smbc_add_cached_server
;
248 context
->callbacks
.get_cached_srv_fn
= smbc_get_cached_server
;
249 context
->callbacks
.remove_cached_srv_fn
= smbc_remove_cached_server
;
250 context
->callbacks
.purge_cached_fn
= smbc_purge_cached
;