6b8a1ab4af9f3c63033755f2cf4097b792eb93ef
[ab/samba-autobuild/.git] / source3 / libsmb / libsmb_cache.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB client library implementation (server cache)
4    Copyright (C) Andrew Tridgell 1998
5    Copyright (C) Richard Sharpe 2000
6    Copyright (C) John Terpstra 2000
7    Copyright (C) Tom Jansen (Ninja ISD) 2002
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "libsmb/libsmb.h"
25 #include "libsmbclient.h"
26 #include "libsmb_internal.h"
27
28 /*
29  * Structure we use if internal caching mechanism is used
30  * nothing fancy here.
31  */
32 struct smbc_server_cache {
33         char *server_name;
34         char *share_name;
35         char *workgroup;
36         char *username;
37         SMBCSRV *server;
38
39         struct smbc_server_cache *next, *prev;
40 };
41
42
43
44 /*
45  * Add a new connection to the server cache.
46  * This function is only used if the external cache is not enabled
47  */
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)
55 {
56         struct smbc_server_cache * srvcache = NULL;
57
58         srvcache = SMB_CALLOC_ARRAY(struct smbc_server_cache, 1);
59         if (srvcache == NULL) {
60                 errno = ENOMEM;
61                 DEBUG(3, ("Not enough space for server cache allocation\n"));
62                 return 1;
63         }
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->internal->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 SMBCSRV *
112 SMBC_get_cached_server(SMBCCTX * context,
113                        const char * server,
114                        const char * share,
115                        const char * workgroup,
116                        const char * user)
117 {
118         struct smbc_server_cache * srv = NULL;
119
120         /* Search the cache lines */
121         for (srv = context->internal->server_cache; srv; srv = srv->next) {
122
123                 if (strcmp(server,srv->server_name)  == 0 &&
124                     strcmp(workgroup,srv->workgroup) == 0 &&
125                     strcmp(user, srv->username)  == 0) {
126
127                         /* If the share name matches, we're cool */
128                         if (strcmp(share, srv->share_name) == 0) {
129                                 return srv->server;
130                         }
131
132                         /*
133                          * We only return an empty share name or the attribute
134                          * server on an exact match (which would have been
135                          * caught above).
136                          */
137                         if (*share == '\0' || strcmp(share, "*IPC$") == 0)
138                                 continue;
139
140                         /*
141                          * Never return an empty share name or the attribute
142                          * server if it wasn't what was requested.
143                          */
144                         if (*srv->share_name == '\0' ||
145                             strcmp(srv->share_name, "*IPC$") == 0)
146                                 continue;
147
148                         /*
149                          * If we're only allowing one share per server, then
150                          * a connection to the server (other than the
151                          * attribute server connection) is cool.
152                          */
153                         if (smbc_getOptionOneSharePerServer(context)) {
154                                 NTSTATUS status;
155                                 /*
156                                  * The currently connected share name
157                                  * doesn't match the requested share, so
158                                  * disconnect from the current share.
159                                  */
160                                 status = cli_tdis(srv->server->cli);
161                                 if (!NT_STATUS_IS_OK(status)) {
162                                         /* Sigh. Couldn't disconnect. */
163                                         cli_shutdown(srv->server->cli);
164                                         srv->server->cli = NULL;
165                                         smbc_getFunctionRemoveCachedServer(context)(context, srv->server);
166                                         continue;
167                                 }
168
169                                 /*
170                                  * Save the new share name.  We've
171                                  * disconnected from the old share, and are
172                                  * about to connect to the new one.
173                                  */
174                                 SAFE_FREE(srv->share_name);
175                                 srv->share_name = SMB_STRDUP(share);
176                                 if (!srv->share_name) {
177                                         /* Out of memory. */
178                                         cli_shutdown(srv->server->cli);
179                                         srv->server->cli = NULL;
180                                         smbc_getFunctionRemoveCachedServer(context)(context, srv->server);
181                                         continue;
182                                 }
183
184                                 return srv->server;
185                         }
186                 }
187         }
188
189         return NULL;
190 }
191
192
193 /*
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
197  */
198 int
199 SMBC_remove_cached_server(SMBCCTX * context,
200                           SMBCSRV * server)
201 {
202         struct smbc_server_cache * srv = NULL;
203
204         for (srv = context->internal->server_cache; srv; srv = srv->next) {
205                 if (server == srv->server) {
206
207                         /* remove this sucker */
208                         DLIST_REMOVE(context->internal->server_cache, 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;
215                 }
216         }
217         /* server not found */
218         return 1;
219 }
220
221
222 /*
223  * Try to remove all the servers in cache
224  * returns 1 on failure and 0 if all servers could be removed.
225  */
226 int
227 SMBC_purge_cached_servers(SMBCCTX * context)
228 {
229         struct smbc_server_cache * srv;
230         struct smbc_server_cache * next;
231         int could_not_purge_all = 0;
232
233         for (srv = context->internal->server_cache,
234                      next = (srv ? srv->next :NULL);
235              srv;
236              srv = next,
237                      next = (srv ? srv->next : NULL)) {
238
239                 if (SMBC_remove_unused_server(context, srv->server)) {
240                         /* could not be removed */
241                         could_not_purge_all = 1;
242                 }
243         }
244         return could_not_purge_all;
245 }