Initial revamp of the libsmbclient interface.
[Samba/gebeck_regimport.git] / source3 / libsmb / libsmb_cache.c
blobe0571fa9fefb51310e5830b73ca615980b1d9322
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 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/>.
24 #include "includes.h"
25 #include "libsmbclient.h"
26 #include "libsmb_internal.h"
29 * Structure we use if internal caching mechanism is used
30 * nothing fancy here.
32 struct smbc_server_cache {
33 char *server_name;
34 char *share_name;
35 char *workgroup;
36 char *username;
37 SMBCSRV *server;
39 struct smbc_server_cache *next, *prev;
45 * Add a new connection to the server cache.
46 * This function is only used if the external cache is not enabled
48 int
49 SMBC_add_cached_server(SMBCCTX * context,
50 SMBCSRV * newsrv,
51 const char * server,
52 const char * share,
53 const char * workgroup,
54 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->cache.server_cache_data), 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 SMBCSRV *
113 SMBC_get_cached_server(SMBCCTX * context,
114 const char * server,
115 const char * share,
116 const char * workgroup,
117 const char * user)
119 struct smbc_server_cache * srv = NULL;
121 /* Search the cache lines */
122 for (srv = context->cache.server_cache_data; srv; srv = srv->next) {
124 if (strcmp(server,srv->server_name) == 0 &&
125 strcmp(workgroup,srv->workgroup) == 0 &&
126 strcmp(user, srv->username) == 0) {
128 /* If the share name matches, we're cool */
129 if (strcmp(share, srv->share_name) == 0) {
130 return srv->server;
134 * We only return an empty share name or the attribute
135 * server on an exact match (which would have been
136 * caught above).
138 if (*share == '\0' || strcmp(share, "*IPC$") == 0)
139 continue;
142 * Never return an empty share name or the attribute
143 * server if it wasn't what was requested.
145 if (*srv->share_name == '\0' ||
146 strcmp(srv->share_name, "*IPC$") == 0)
147 continue;
150 * If we're only allowing one share per server, then
151 * a connection to the server (other than the
152 * attribute server connection) is cool.
154 if (context->one_share_per_server) {
156 * The currently connected share name
157 * doesn't match the requested share, so
158 * disconnect from the current share.
160 if (! cli_tdis(srv->server->cli)) {
161 /* Sigh. Couldn't disconnect. */
162 cli_shutdown(srv->server->cli);
163 srv->server->cli = NULL;
164 context->cache.remove_cached_server_fn(context, srv->server);
165 continue;
169 * Save the new share name. We've
170 * disconnected from the old share, and are
171 * about to connect to the new one.
173 SAFE_FREE(srv->share_name);
174 srv->share_name = SMB_STRDUP(share);
175 if (!srv->share_name) {
176 /* Out of memory. */
177 cli_shutdown(srv->server->cli);
178 srv->server->cli = NULL;
179 context->cache.remove_cached_server_fn(context, srv->server);
180 continue;
184 return srv->server;
189 return NULL;
194 * Search the server cache for a server and remove it
195 * returns 0 on success
196 * This function is only used if the external cache is not enabled
199 SMBC_remove_cached_server(SMBCCTX * context,
200 SMBCSRV * server)
202 struct smbc_server_cache * srv = NULL;
204 for (srv = context->cache.server_cache_data; srv; srv = srv->next) {
205 if (server == srv->server) {
207 /* remove this sucker */
208 DLIST_REMOVE(context->cache.server_cache_data, srv);
209 SAFE_FREE(srv->server_name);
210 SAFE_FREE(srv->share_name);
211 SAFE_FREE(srv->workgroup);
212 SAFE_FREE(srv->username);
213 SAFE_FREE(srv);
214 return 0;
217 /* server not found */
218 return 1;
223 * Try to remove all the servers in cache
224 * returns 1 on failure and 0 if all servers could be removed.
227 SMBC_purge_cached_servers(SMBCCTX * context)
229 struct smbc_server_cache * srv;
230 struct smbc_server_cache * next;
231 int could_not_purge_all = 0;
233 for (srv = context->cache.server_cache_data,
234 next = (srv ? srv->next :NULL);
235 srv;
236 srv = next,
237 next = (srv ? srv->next : NULL)) {
239 if (SMBC_remove_unused_server(context, srv->server)) {
240 /* could not be removed */
241 could_not_purge_all = 1;
244 return could_not_purge_all;