r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[abartlet/samba.git/.git] / source / libsmb / libsmb_cache.c
index de9a1656d84f1acb113e5b7749b589aab9bfc205..b98df024faa9ca2b4566e39c1e47d817ecafef43 100644 (file)
@@ -9,7 +9,7 @@
    
    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,
    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 "include/libsmbclient.h"
 #include "../include/libsmb_internal.h"
+
+int smbc_default_cache_functions(SMBCCTX * context);
+
 /*
  * Structure we use if internal caching mechanism is used 
  * nothing fancy here.
@@ -46,7 +48,7 @@ struct smbc_server_cache {
  * Add a new connection to the server cache.
  * This function is only used if the external cache is not enabled 
  */
-static int smbc_add_cached_server(SMBCCTX * context, SMBCSRV * new,
+static int smbc_add_cached_server(SMBCCTX * context, SMBCSRV * newsrv,
                                  const char * server, const char * share, 
                                  const char * workgroup, const char * username)
 {
@@ -60,7 +62,7 @@ static int smbc_add_cached_server(SMBCCTX * context, SMBCSRV * new,
        
        ZERO_STRUCTP(srvcache);
 
-       srvcache->server = new;
+       srvcache->server = newsrv;
 
        srvcache->server_name = SMB_STRDUP(server);
        if (!srvcache->server_name) {
@@ -94,6 +96,7 @@ static int smbc_add_cached_server(SMBCCTX * context, SMBCSRV * new,
        SAFE_FREE(srvcache->share_name);
        SAFE_FREE(srvcache->workgroup);
        SAFE_FREE(srvcache->username);
+       SAFE_FREE(srvcache);
        
        return 1;
 }
@@ -149,13 +152,30 @@ static SMBCSRV * smbc_get_cached_server(SMBCCTX * context, const char * server,
                                  * doesn't match the requested share, so
                                  * disconnect from the current share.
                                  */
-                                if (! cli_tdis(&srv->server->cli)) {
+                                if (! cli_tdis(srv->server->cli)) {
                                         /* Sigh. Couldn't disconnect. */
-                                        cli_shutdown(&srv->server->cli);
+                                        cli_shutdown(srv->server->cli);
+                                       srv->server->cli = NULL;
                                         context->callbacks.remove_cached_srv_fn(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;
+                                        context->callbacks.remove_cached_srv_fn(context, srv->server);
+                                        continue;
+                                }
+
+
                                 return srv->server;
                         }
                 }