1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-24 21:34:56 +03:00
samba-mirror/source3/libsmb/libsmb_cache.c

247 lines
7.4 KiB
C
Raw Normal View History

/*
Unix SMB/CIFS implementation.
SMB client library implementation (server cache)
Copyright (C) Andrew Tridgell 1998
Copyright (C) Richard Sharpe 2000
Copyright (C) John Terpstra 2000
Copyright (C) Tom Jansen (Ninja ISD) 2002
2009-11-22 00:52:12 +03:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
2009-11-22 00:52:12 +03:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2009-11-22 00:52:12 +03:00
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "libsmb/libsmb.h"
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
#include "libsmbclient.h"
#include "libsmb_internal.h"
/*
* Structure we use if internal caching mechanism is used
* nothing fancy here.
*/
struct smbc_server_cache {
char *server_name;
char *share_name;
char *workgroup;
char *username;
SMBCSRV *server;
2009-11-22 00:52:12 +03:00
struct smbc_server_cache *next, *prev;
};
/*
* Add a new connection to the server cache.
* This function is only used if the external cache is not enabled
*/
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
int
SMBC_add_cached_server(SMBCCTX * context,
SMBCSRV * newsrv,
const char * server,
const char * share,
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
const char * workgroup,
const char * username)
{
struct smbc_server_cache * srvcache = NULL;
2009-11-22 00:52:12 +03:00
if (!(srvcache = SMB_MALLOC_P(struct smbc_server_cache))) {
errno = ENOMEM;
DEBUG(3, ("Not enough space for server cache allocation\n"));
return 1;
}
2009-11-22 00:52:12 +03:00
ZERO_STRUCTP(srvcache);
2009-11-22 00:52:12 +03:00
srvcache->server = newsrv;
2009-11-22 00:52:12 +03:00
srvcache->server_name = SMB_STRDUP(server);
if (!srvcache->server_name) {
errno = ENOMEM;
goto failed;
}
2009-11-22 00:52:12 +03:00
srvcache->share_name = SMB_STRDUP(share);
if (!srvcache->share_name) {
errno = ENOMEM;
goto failed;
}
2009-11-22 00:52:12 +03:00
srvcache->workgroup = SMB_STRDUP(workgroup);
if (!srvcache->workgroup) {
errno = ENOMEM;
goto failed;
}
2009-11-22 00:52:12 +03:00
srvcache->username = SMB_STRDUP(username);
if (!srvcache->username) {
errno = ENOMEM;
goto failed;
}
2009-11-22 00:52:12 +03:00
DLIST_ADD(context->internal->server_cache, srvcache);
return 0;
2009-11-22 00:52:12 +03:00
failed:
SAFE_FREE(srvcache->server_name);
SAFE_FREE(srvcache->share_name);
SAFE_FREE(srvcache->workgroup);
SAFE_FREE(srvcache->username);
SAFE_FREE(srvcache);
2009-11-22 00:52:12 +03:00
return 1;
}
/*
* Search the server cache for a server
* returns server handle on success, NULL on error (not found)
* This function is only used if the external cache is not enabled
*/
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
SMBCSRV *
SMBC_get_cached_server(SMBCCTX * context,
const char * server,
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
const char * share,
const char * workgroup,
const char * user)
{
struct smbc_server_cache * srv = NULL;
2009-11-22 00:52:12 +03:00
/* Search the cache lines */
for (srv = context->internal->server_cache; srv; srv = srv->next) {
2009-11-22 00:52:12 +03:00
if (strcmp(server,srv->server_name) == 0 &&
strcmp(workgroup,srv->workgroup) == 0 &&
strcmp(user, srv->username) == 0) {
2009-11-22 00:52:12 +03:00
/* If the share name matches, we're cool */
if (strcmp(share, srv->share_name) == 0) {
return srv->server;
}
2009-11-22 00:52:12 +03:00
/*
* We only return an empty share name or the attribute
* server on an exact match (which would have been
* caught above).
*/
if (*share == '\0' || strcmp(share, "*IPC$") == 0)
continue;
2009-11-22 00:52:12 +03:00
/*
* Never return an empty share name or the attribute
* server if it wasn't what was requested.
*/
if (*srv->share_name == '\0' ||
strcmp(srv->share_name, "*IPC$") == 0)
continue;
2009-11-22 00:52:12 +03:00
/*
* If we're only allowing one share per server, then
* a connection to the server (other than the
* attribute server connection) is cool.
*/
if (smbc_getOptionOneSharePerServer(context)) {
2010-01-03 20:46:57 +03:00
NTSTATUS status;
/*
* The currently connected share name
* doesn't match the requested share, so
* disconnect from the current share.
*/
2010-01-03 20:46:57 +03:00
status = cli_tdis(srv->server->cli);
if (!NT_STATUS_IS_OK(status)) {
/* Sigh. Couldn't disconnect. */
cli_shutdown(srv->server->cli);
srv->server->cli = NULL;
smbc_getFunctionRemoveCachedServer(context)(context, srv->server);
continue;
}
2009-11-22 00:52:12 +03:00
/*
* Save the new share name. We've
* disconnected from the old share, and are
* about to connect to the new one.
*/
SAFE_FREE(srv->share_name);
srv->share_name = SMB_STRDUP(share);
if (!srv->share_name) {
/* Out of memory. */
cli_shutdown(srv->server->cli);
srv->server->cli = NULL;
smbc_getFunctionRemoveCachedServer(context)(context, srv->server);
continue;
}
2009-11-22 00:52:12 +03:00
return srv->server;
}
}
}
2009-11-22 00:52:12 +03:00
return NULL;
}
/*
* Search the server cache for a server and remove it
* returns 0 on success
* This function is only used if the external cache is not enabled
*/
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
int
SMBC_remove_cached_server(SMBCCTX * context,
SMBCSRV * server)
{
struct smbc_server_cache * srv = NULL;
2009-11-22 00:52:12 +03:00
for (srv = context->internal->server_cache; srv; srv = srv->next) {
if (server == srv->server) {
2009-11-22 00:52:12 +03:00
/* remove this sucker */
DLIST_REMOVE(context->internal->server_cache, srv);
SAFE_FREE(srv->server_name);
SAFE_FREE(srv->share_name);
SAFE_FREE(srv->workgroup);
SAFE_FREE(srv->username);
SAFE_FREE(srv);
return 0;
}
}
/* server not found */
return 1;
}
/*
* Try to remove all the servers in cache
* returns 1 on failure and 0 if all servers could be removed.
*/
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
int
SMBC_purge_cached_servers(SMBCCTX * context)
{
struct smbc_server_cache * srv;
struct smbc_server_cache * next;
int could_not_purge_all = 0;
2009-11-22 00:52:12 +03:00
for (srv = context->internal->server_cache,
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
next = (srv ? srv->next :NULL);
srv;
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
srv = next,
next = (srv ? srv->next : NULL)) {
2009-11-22 00:52:12 +03:00
Initial revamp of the libsmbclient interface. The libsmbclient interface has suffered from difficulty of improvement and feature enrichment without causing ABI breakage. Although there were a number of issues, the primary ones were: (a) the user of the library would manually manipulate the context structure members, meaning that nothing in the context structure could change other than adding stuff at the end; (b) there were three methods of setting options: setting bits in a flags field within the context structure, setting explicit options variables within an options structure in the context structure, and by calling the smbc_option_set() function; (c) the authentication callback did not traditionally provide enough information to the callee which required adding an option for a callback with a different signature, and now there are requests for even more information at the callback, requiring yet a third signature and option to set it (if we implement that feature). This commit provides a reorganization of the code which fixes (a) and (b). The context structure is now entirely opaque, and there are setter and getter functions for manipulating it. This makes maintaining ABI consistency much, much easier. Additionally, the options setting/getting has been unified into a single mechanism using smbc_option_set() and smbc_option_get(). Yet to be completed is a refactoring of the authentication callback (c). The test programs in examples/libsmbclient have been modified (if necessary; some applications require no changes at all) for the new API and a few have been minimally tested. Derrell (This used to be commit d4b4bae8ded824d06ad5ab0e219f71187ee5c771)
2008-02-28 19:23:20 +03:00
if (SMBC_remove_unused_server(context, srv->server)) {
/* could not be removed */
could_not_purge_all = 1;
}
}
return could_not_purge_all;
}