ctdb-scripts: Ignore shellcheck SC2181 warning (use of $?)
[metze/samba/wip.git] / source3 / libsmb / libsmb_cache.c
index de9a1656d84f1acb113e5b7749b589aab9bfc205..2b32447807e7ab8d10f3040281f56247113cd97e 100644 (file)
@@ -1,33 +1,32 @@
-
-/* 
+/*
    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 
-   
+   Copyright (C) Tom Jansen (Ninja ISD) 2002
+
    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 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    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.
-   
+
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#include "libsmb/libsmb.h"
+#include "libsmbclient.h"
+#include "libsmb_internal.h"
 
-#include "include/libsmbclient.h"
-#include "../include/libsmb_internal.h"
 /*
- * Structure we use if internal caching mechanism is used 
+ * Structure we use if internal caching mechanism is used
  * nothing fancy here.
  */
 struct smbc_server_cache {
@@ -36,19 +35,23 @@ struct smbc_server_cache {
        char *workgroup;
        char *username;
        SMBCSRV *server;
-       
+
        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 
+ * This function is only used if the external cache is not enabled
  */
-static int smbc_add_cached_server(SMBCCTX * context, SMBCSRV * new,
-                                 const char * server, const char * share, 
-                                 const char * workgroup, const char * username)
+int
+SMBC_add_cached_server(SMBCCTX * context,
+                       SMBCSRV * newsrv,
+                       const char * server,
+                       const char * share,
+                       const char * workgroup,
+                       const char * username)
 {
        struct smbc_server_cache * srvcache = NULL;
 
@@ -57,10 +60,10 @@ static int smbc_add_cached_server(SMBCCTX * context, SMBCSRV * new,
                DEBUG(3, ("Not enough space for server cache allocation\n"));
                return 1;
        }
-       
+
        ZERO_STRUCTP(srvcache);
 
-       srvcache->server = new;
+       srvcache->server = newsrv;
 
        srvcache->server_name = SMB_STRDUP(server);
        if (!srvcache->server_name) {
@@ -86,32 +89,37 @@ static int smbc_add_cached_server(SMBCCTX * context, SMBCSRV * new,
                goto failed;
        }
 
-       DLIST_ADD((context->server_cache), srvcache);
+       DLIST_ADD(context->internal->server_cache, srvcache);
        return 0;
 
- failed:
+failed:
        SAFE_FREE(srvcache->server_name);
        SAFE_FREE(srvcache->share_name);
        SAFE_FREE(srvcache->workgroup);
        SAFE_FREE(srvcache->username);
-       
+       SAFE_FREE(srvcache);
+
        return 1;
 }
 
 
 
 /*
- * Search the server cache for a server 
+ * 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 
+ * This function is only used if the external cache is not enabled
  */
-static SMBCSRV * smbc_get_cached_server(SMBCCTX * context, const char * server, 
-                                 const char * share, const char * workgroup, const char * user)
+SMBCSRV *
+SMBC_get_cached_server(SMBCCTX * context,
+                       const char * server,
+                       const char * share,
+                       const char * workgroup,
+                       const char * user)
 {
        struct smbc_server_cache * srv = NULL;
-       
+
        /* Search the cache lines */
-       for (srv=((struct smbc_server_cache *)context->server_cache);srv;srv=srv->next) {
+       for (srv = context->internal->server_cache; srv; srv = srv->next) {
 
                if (strcmp(server,srv->server_name)  == 0 &&
                    strcmp(workgroup,srv->workgroup) == 0 &&
@@ -143,16 +151,34 @@ static SMBCSRV * smbc_get_cached_server(SMBCCTX * context, const char * server,
                          * a connection to the server (other than the
                          * attribute server connection) is cool.
                          */
-                        if (context->options.one_share_per_server) {
+                        if (smbc_getOptionOneSharePerServer(context)) {
+                               NTSTATUS status;
                                 /*
                                  * The currently connected share name
                                  * doesn't match the requested share, so
                                  * disconnect from the current share.
                                  */
-                                if (! cli_tdis(&srv->server->cli)) {
+                               status = cli_tdis(srv->server->cli);
+                               if (!NT_STATUS_IS_OK(status)) {
                                         /* Sigh. Couldn't disconnect. */
-                                        cli_shutdown(&srv->server->cli);
-                                        context->callbacks.remove_cached_srv_fn(context, srv->server);
+                                        cli_shutdown(srv->server->cli);
+                                       srv->server->cli = NULL;
+                                        smbc_getFunctionRemoveCachedServer(context)(context, srv->server);
+                                        continue;
+                                }
+
+                                /*
+                                 * 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;
                                 }
 
@@ -165,20 +191,22 @@ static SMBCSRV * smbc_get_cached_server(SMBCCTX * context, const char * server,
 }
 
 
-/* 
+/*
  * 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 
+ * This function is only used if the external cache is not enabled
  */
-static int smbc_remove_cached_server(SMBCCTX * context, SMBCSRV * server)
+int
+SMBC_remove_cached_server(SMBCCTX * context,
+                          SMBCSRV * server)
 {
        struct smbc_server_cache * srv = NULL;
-       
-       for (srv=((struct smbc_server_cache *)context->server_cache);srv;srv=srv->next) {
-               if (server == srv->server) { 
+
+       for (srv = context->internal->server_cache; srv; srv = srv->next) {
+               if (server == srv->server) {
 
                        /* remove this sucker */
-                       DLIST_REMOVE(context->server_cache, srv);
+                       DLIST_REMOVE(context->internal->server_cache, srv);
                        SAFE_FREE(srv->server_name);
                        SAFE_FREE(srv->share_name);
                        SAFE_FREE(srv->workgroup);
@@ -196,40 +224,23 @@ static int smbc_remove_cached_server(SMBCCTX * context, SMBCSRV * server)
  * Try to remove all the servers in cache
  * returns 1 on failure and 0 if all servers could be removed.
  */
-static int smbc_purge_cached(SMBCCTX * context)
+int
+SMBC_purge_cached_servers(SMBCCTX * context)
 {
        struct smbc_server_cache * srv;
        struct smbc_server_cache * next;
        int could_not_purge_all = 0;
 
-       for (srv = ((struct smbc_server_cache *) context->server_cache),
-                 next = (srv ? srv->next :NULL);
+       for (srv = context->internal->server_cache,
+                     next = (srv ? srv->next :NULL);
              srv;
-             srv = next, next = (srv ? srv->next : NULL)) {
+             srv = next,
+                     next = (srv ? srv->next : NULL)) {
 
-               if (smbc_remove_unused_server(context, srv->server)) {
+               if (SMBC_remove_unused_server(context, srv->server)) {
                        /* could not be removed */
                        could_not_purge_all = 1;
                }
        }
        return could_not_purge_all;
 }
-
-
-
-/*
- * This functions initializes all server-cache related functions 
- * to the default (internal) system.
- *
- * We use this to make the rest of the cache system static.
- */
-
-int smbc_default_cache_functions(SMBCCTX * context)
-{
-       context->callbacks.add_cached_srv_fn    = smbc_add_cached_server;
-       context->callbacks.get_cached_srv_fn    = smbc_get_cached_server;
-       context->callbacks.remove_cached_srv_fn = smbc_remove_cached_server;
-       context->callbacks.purge_cached_fn      = smbc_purge_cached;
-
-       return 0;
-}