caf226c5a6a2d3e2573b14deb89a1716074f2f8b
[samba.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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 /*
27  * Define this to get the real SMBCFILE and SMBCSRV structures 
28  */
29 #define _SMBC_INTERNAL
30 #include "include/libsmbclient.h"
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 * new,
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 = malloc(sizeof(*srvcache)))) {
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 = new;
67
68         srvcache->server_name = strdup(server);
69         if (!srvcache->server_name) {
70                 errno = ENOMEM;
71                 goto failed;
72         }
73
74         srvcache->share_name = strdup(share);
75         if (!srvcache->share_name) {
76                 errno = ENOMEM;
77                 goto failed;
78         }
79
80         srvcache->workgroup = strdup(workgroup);
81         if (!srvcache->workgroup) {
82                 errno = ENOMEM;
83                 goto failed;
84         }
85
86         srvcache->username = 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         
101         return 1;
102 }
103
104
105
106 /*
107  * Search the server cache for a server 
108  * returns server_fd on success, -1 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                 if (strcmp(server,srv->server_name)  == 0 &&
119                     strcmp(share,srv->share_name)    == 0 &&
120                     strcmp(workgroup,srv->workgroup) == 0 &&
121                     strcmp(user, srv->username)  == 0) 
122                         return srv->server;
123         }
124
125         return NULL;
126 }
127
128
129 /* 
130  * Search the server cache for a server and remove it
131  * returns 0 on success
132  * This function is only used if the external cache is not enabled 
133  */
134 static int smbc_remove_cached_server(SMBCCTX * context, SMBCSRV * server)
135 {
136         struct smbc_server_cache * srv = NULL;
137         
138         for (srv=((struct smbc_server_cache *)context->server_cache);srv;srv=srv->next) {
139                 if (server == srv->server) { 
140
141                         /* remove this sucker */
142                         DLIST_REMOVE(context->server_cache, srv);
143                         SAFE_FREE(srv->server_name);
144                         SAFE_FREE(srv->share_name);
145                         SAFE_FREE(srv->workgroup);
146                         SAFE_FREE(srv->username);
147                         SAFE_FREE(srv);
148                         return 0;
149                 }
150         }
151         /* server not found */
152         return 1;
153 }
154
155
156 /*
157  * Try to remove all the servers in cache
158  * returns 1 on failure and 0 if all servers could be removed.
159  */
160 static int smbc_purge_cached(SMBCCTX * context)
161 {
162         struct smbc_server_cache * srv;
163         struct smbc_server_cache * next;
164         int could_not_purge_all = 0;
165
166         for (srv = ((struct smbc_server_cache *) context->server_cache),
167                  next = (srv ? srv->next :NULL);
168              srv;
169              srv = next, next = (srv ? srv->next : NULL)) {
170
171                 if (smbc_remove_unused_server(context, srv->server)) {
172                         /* could not be removed */
173                         could_not_purge_all = 1;
174                 }
175         }
176         return could_not_purge_all;
177 }
178
179
180
181 /*
182  * This functions initializes all server-cache related functions 
183  * to the default (internal) system.
184  *
185  * We use this to make the rest of the cache system static.
186  */
187
188 int smbc_default_cache_functions(SMBCCTX * context)
189 {
190         context->callbacks.add_cached_srv_fn    = smbc_add_cached_server;
191         context->callbacks.get_cached_srv_fn    = smbc_get_cached_server;
192         context->callbacks.remove_cached_srv_fn = smbc_remove_cached_server;
193         context->callbacks.purge_cached_fn      = smbc_purge_cached;
194
195         return 0;
196 }