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