b98df024faa9ca2b4566e39c1e47d817ecafef43
[samba.git] / source / libsmb / libsmb_cache.c
1
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 
9    
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.
14    
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.
19    
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/>.
22 */
23
24 #include "includes.h"
25
26 #include "include/libsmbclient.h"
27 #include "../include/libsmb_internal.h"
28
29 int smbc_default_cache_functions(SMBCCTX * context);
30
31 /*
32  * Structure we use if internal caching mechanism is used 
33  * nothing fancy here.
34  */
35 struct smbc_server_cache {
36         char *server_name;
37         char *share_name;
38         char *workgroup;
39         char *username;
40         SMBCSRV *server;
41         
42         struct smbc_server_cache *next, *prev;
43 };
44         
45
46
47 /*
48  * Add a new connection to the server cache.
49  * This function is only used if the external cache is not enabled 
50  */
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)
54 {
55         struct smbc_server_cache * srvcache = NULL;
56
57         if (!(srvcache = SMB_MALLOC_P(struct smbc_server_cache))) {
58                 errno = ENOMEM;
59                 DEBUG(3, ("Not enough space for server cache allocation\n"));
60                 return 1;
61         }
62        
63         ZERO_STRUCTP(srvcache);
64
65         srvcache->server = newsrv;
66
67         srvcache->server_name = SMB_STRDUP(server);
68         if (!srvcache->server_name) {
69                 errno = ENOMEM;
70                 goto failed;
71         }
72
73         srvcache->share_name = SMB_STRDUP(share);
74         if (!srvcache->share_name) {
75                 errno = ENOMEM;
76                 goto failed;
77         }
78
79         srvcache->workgroup = SMB_STRDUP(workgroup);
80         if (!srvcache->workgroup) {
81                 errno = ENOMEM;
82                 goto failed;
83         }
84
85         srvcache->username = SMB_STRDUP(username);
86         if (!srvcache->username) {
87                 errno = ENOMEM;
88                 goto failed;
89         }
90
91         DLIST_ADD((context->server_cache), srvcache);
92         return 0;
93
94  failed:
95         SAFE_FREE(srvcache->server_name);
96         SAFE_FREE(srvcache->share_name);
97         SAFE_FREE(srvcache->workgroup);
98         SAFE_FREE(srvcache->username);
99         SAFE_FREE(srvcache);
100         
101         return 1;
102 }
103
104
105
106 /*
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 
110  */
111 static SMBCSRV * smbc_get_cached_server(SMBCCTX * context, const char * server, 
112                                   const char * share, const char * workgroup, const char * user)
113 {
114         struct smbc_server_cache * srv = NULL;
115         
116         /* Search the cache lines */
117         for (srv=((struct smbc_server_cache *)context->server_cache);srv;srv=srv->next) {
118
119                 if (strcmp(server,srv->server_name)  == 0 &&
120                     strcmp(workgroup,srv->workgroup) == 0 &&
121                     strcmp(user, srv->username)  == 0) {
122
123                         /* If the share name matches, we're cool */
124                         if (strcmp(share, srv->share_name) == 0) {
125                                 return srv->server;
126                         }
127
128                         /*
129                          * We only return an empty share name or the attribute
130                          * server on an exact match (which would have been
131                          * caught above).
132                          */
133                         if (*share == '\0' || strcmp(share, "*IPC$") == 0)
134                                 continue;
135
136                         /*
137                          * Never return an empty share name or the attribute
138                          * server if it wasn't what was requested.
139                          */
140                         if (*srv->share_name == '\0' ||
141                             strcmp(srv->share_name, "*IPC$") == 0)
142                                 continue;
143
144                         /*
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.
148                          */
149                         if (context->options.one_share_per_server) {
150                                 /*
151                                  * The currently connected share name
152                                  * doesn't match the requested share, so
153                                  * disconnect from the current share.
154                                  */
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);
160                                         continue;
161                                 }
162
163                                 /*
164                                  * Save the new share name.  We've
165                                  * disconnected from the old share, and are
166                                  * about to connect to the new one.
167                                  */
168                                 SAFE_FREE(srv->share_name);
169                                 srv->share_name = SMB_STRDUP(share);
170                                 if (!srv->share_name) {
171                                         /* Out of memory. */
172                                         cli_shutdown(srv->server->cli);
173                                         srv->server->cli = NULL;
174                                         context->callbacks.remove_cached_srv_fn(context, srv->server);
175                                         continue;
176                                 }
177
178
179                                 return srv->server;
180                         }
181                 }
182         }
183
184         return NULL;
185 }
186
187
188 /* 
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 
192  */
193 static int smbc_remove_cached_server(SMBCCTX * context, SMBCSRV * server)
194 {
195         struct smbc_server_cache * srv = NULL;
196         
197         for (srv=((struct smbc_server_cache *)context->server_cache);srv;srv=srv->next) {
198                 if (server == srv->server) { 
199
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);
206                         SAFE_FREE(srv);
207                         return 0;
208                 }
209         }
210         /* server not found */
211         return 1;
212 }
213
214
215 /*
216  * Try to remove all the servers in cache
217  * returns 1 on failure and 0 if all servers could be removed.
218  */
219 static int smbc_purge_cached(SMBCCTX * context)
220 {
221         struct smbc_server_cache * srv;
222         struct smbc_server_cache * next;
223         int could_not_purge_all = 0;
224
225         for (srv = ((struct smbc_server_cache *) context->server_cache),
226                  next = (srv ? srv->next :NULL);
227              srv;
228              srv = next, next = (srv ? srv->next : NULL)) {
229
230                 if (smbc_remove_unused_server(context, srv->server)) {
231                         /* could not be removed */
232                         could_not_purge_all = 1;
233                 }
234         }
235         return could_not_purge_all;
236 }
237
238
239
240 /*
241  * This functions initializes all server-cache related functions 
242  * to the default (internal) system.
243  *
244  * We use this to make the rest of the cache system static.
245  */
246
247 int smbc_default_cache_functions(SMBCCTX * context)
248 {
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;
253
254         return 0;
255 }