r2680: switched the libcli/raw/ code over to use talloc_reference(), which simplifies...
authorAndrew Tridgell <tridge@samba.org>
Mon, 27 Sep 2004 08:41:39 +0000 (08:41 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 17:59:21 +0000 (12:59 -0500)
source/libcli/cliconnect.c
source/libcli/raw/clisession.c
source/libcli/raw/clitransport.c
source/libcli/raw/clitree.c
source/librpc/rpc/dcerpc_smb.c
source/rpc_server/samr/samdb.c
source/torture/basic/secleak.c
source/torture/raw/context.c
source/torture/rpc/echo.c

index f7720703051afb361be21bb024ceb6c3f0f60c3b..aa6aec4a1e2f5f9b7838462d754174fde8ede5d9 100644 (file)
@@ -36,8 +36,8 @@ BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
        }
        
        cli->transport = smbcli_transport_init(sock);
+       talloc_free(sock);
        if (!cli->transport) {
-               talloc_free(sock);
                return False;
        }
 
@@ -60,9 +60,9 @@ NTSTATUS smbcli_negprot(struct smbcli_state *cli)
 
 /* wrapper around smb_raw_session_setup() */
 NTSTATUS smbcli_session_setup(struct smbcli_state *cli, 
-                          const char *user, 
-                          const char *password, 
-                          const char *domain)
+                             const char *user, 
+                             const char *password, 
+                             const char *domain)
 {
        union smb_sesssetup setup;
        NTSTATUS status;
@@ -70,6 +70,7 @@ NTSTATUS smbcli_session_setup(struct smbcli_state *cli,
 
        cli->session = smbcli_session_init(cli->transport);
        if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
+       talloc_free(cli->transport);
 
        mem_ctx = talloc_init("smbcli_session_setup");
        if (!mem_ctx) return NT_STATUS_NO_MEMORY;
@@ -110,6 +111,7 @@ NTSTATUS smbcli_send_tconX(struct smbcli_state *cli, const char *sharename,
        NTSTATUS status;
 
        cli->tree = smbcli_tree_init(cli->session);
+       talloc_free(cli->session);
        if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
 
        mem_ctx = talloc_init("tcon");
@@ -183,7 +185,8 @@ NTSTATUS smbcli_full_connection(struct smbcli_state **ret_cli,
 
        (*ret_cli) = smbcli_state_init();
 
-       (*ret_cli)->tree = tree;
+       (*ret_cli)->tree = talloc_reference(*ret_cli, tree);
+       talloc_free(tree);
        (*ret_cli)->session = tree->session;
        (*ret_cli)->transport = tree->session->transport;
 
@@ -221,7 +224,5 @@ struct smbcli_state *smbcli_state_init(void)
 ****************************************************************************/
 void smbcli_shutdown(struct smbcli_state *cli)
 {
-       if (!cli) return;
-       talloc_free(cli->tree);
        talloc_free(cli);
 }
index 0c6c80d94c4d470bd29ca87cea91a9a6de5acbf9..431d2250210fd1e365255f214aef7f1c25fe4226 100644 (file)
 } while (0)
 
 
-/*
-  destroy a smbcli_session
-*/
-static int session_destroy(void *ptr)
-{
-       struct smbcli_session *session = ptr;
-       talloc_free(session->transport);
-       return 0;
-}
-
 /****************************************************************************
  Initialize the session context
 ****************************************************************************/
@@ -50,12 +40,10 @@ struct smbcli_session *smbcli_session_init(struct smbcli_transport *transport)
        }
 
        ZERO_STRUCTP(session);
-       session->transport = transport;
+       session->transport = talloc_reference(session, transport);
        session->pid = (uint16_t)getpid();
        session->vuid = UID_FIELD_INVALID;
 
-       talloc_set_destructor(session, session_destroy);
-
        return session;
 }
 
index eb1d3631ee390e93f5df88f24c9b4d70a17f02c1..f06f2c57ff8849290132d1a1455fd3c64bacb4cf 100644 (file)
@@ -52,7 +52,6 @@ static int transport_destructor(void *ptr)
        event_remove_fd(transport->event.ctx, transport->event.fde);
        event_remove_timed(transport->event.ctx, transport->event.te);
        event_context_destroy(transport->event.ctx);
-       talloc_free(transport->socket);
        return 0;
 }
 
@@ -75,7 +74,7 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock)
                return NULL;
        }
 
-       transport->socket = sock;
+       transport->socket = talloc_reference(transport, sock);
        transport->negotiate.protocol = PROTOCOL_NT1;
        transport->options.use_spnego = lp_use_spnego();
        transport->negotiate.max_xmit = ~0;
index e0072a31b1ece0f63b4dbc7ec241ef2b2b24572d..f19cbf8e2840c2e2d2667153aa664390daf25171 100644 (file)
        if (!req) return NULL; \
 } while (0)
 
-/*
-  destroy a smbcli_tree
-*/
-static int tree_destructor(void *ptr)
-{
-       struct smbcli_tree *tree = ptr;
-       talloc_free(tree->session);
-       return 0;
-}
-
 /****************************************************************************
  Initialize the tree context
 ****************************************************************************/
@@ -49,8 +39,7 @@ struct smbcli_tree *smbcli_tree_init(struct smbcli_session *session)
        }
 
        ZERO_STRUCTP(tree);
-       tree->session = session;
-       talloc_set_destructor(tree, tree_destructor);
+       tree->session = talloc_reference(tree, session);
 
        return tree;
 }
@@ -188,17 +177,16 @@ NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
                return NT_STATUS_NO_MEMORY;
        }
 
-       talloc_set_name_const(sock, "smbcli_tree_full_connection");
-
        /* open a TCP socket to the server */
        if (!smbcli_sock_connect_byname(sock, dest_host, port)) {
+               talloc_free(sock);
                DEBUG(2,("Failed to establish socket connection - %s\n", strerror(errno)));
                return NT_STATUS_UNSUCCESSFUL;
        }
 
        transport = smbcli_transport_init(sock);
+       talloc_free(sock);
        if (!transport) {
-               talloc_free(sock);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -220,8 +208,8 @@ NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
        }
 
        session = smbcli_session_init(transport);
+       talloc_free(transport);
        if (!session) {
-               talloc_free(transport);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -255,8 +243,8 @@ NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
        session->vuid = setup.generic.out.vuid;
 
        tree = smbcli_tree_init(session);
+       talloc_free(session);
        if (!tree) {
-               talloc_free(session);
                talloc_free(mem_ctx);
                return NT_STATUS_NO_MEMORY;
        }
index 7000bcd20d5c4751e50af49986d34f1bd3cfd9c3..893093c050b2ed503f211d841f40a2d19af55416 100644 (file)
@@ -334,7 +334,7 @@ static NTSTATUS smb_shutdown_pipe(struct dcerpc_pipe *p)
        c.close.in.fnum = smb->fnum;
        c.close.in.write_time = 0;
        smb_raw_close(smb->tree, &c);
-       talloc_free(smb->tree);
+       talloc_free(smb);
 
        return NT_STATUS_OK;
 }
@@ -371,15 +371,14 @@ NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe **p,
 {
        struct smb_private *smb;
         NTSTATUS status;
-       char *name = NULL;
+       char *name;
        union smb_open io;
-       TALLOC_CTX *mem_ctx;
 
-       asprintf(&name, "\\%s", pipe_name);
+       name = talloc_asprintf(tree, "\\%s", pipe_name);
        if (!name) {
                return NT_STATUS_NO_MEMORY;
        }
-
+       
        io.ntcreatex.level = RAW_OPEN_NTCREATEX;
        io.ntcreatex.in.flags = 0;
        io.ntcreatex.in.root_fid = 0;
@@ -400,14 +399,8 @@ NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe **p,
        io.ntcreatex.in.security_flags = 0;
        io.ntcreatex.in.fname = name;
 
-       mem_ctx = talloc_init("torture_rpc_connection");
-       if (!mem_ctx) {
-               free(name);
-               return NT_STATUS_NO_MEMORY;
-       }
-       status = smb_raw_open(tree, mem_ctx, &io);
-       free(name);
-       talloc_free(mem_ctx);
+       status = smb_raw_open(tree, name, &io);
+       talloc_free(name);
 
        if (!NT_STATUS_IS_OK(status)) {
                 return status;
@@ -440,10 +433,9 @@ NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe **p,
        }
 
        smb->fnum = io.ntcreatex.out.fnum;
-       smb->tree = tree;
+       smb->tree = talloc_reference(smb, tree);
 
        (*p)->transport.private = smb;
-       talloc_increase_ref_count(tree);
 
         return NT_STATUS_OK;
 }
index 33c4eec86ab0d15faa8f5a67642aac2d45c41a3c..4160ef78fce39b586670588862b5c5f372acf885 100644 (file)
@@ -68,8 +68,7 @@ void *samdb_connect(TALLOC_CTX *mem_ctx)
          second open due to the broken nature of unix locking.
        */
        if (ctx != NULL) {
-               talloc_increase_ref_count(ctx);
-               return ctx;
+               return talloc_reference(mem_ctx, ctx);
        }
 
        ctx = talloc_p(mem_ctx, struct samdb_context);
index 54fa3ecee666f0d091d6a7ab05f25c46b5cbf4d3..998faec54efd6a6a5edb55c0631e2eb7f765949b 100644 (file)
@@ -27,7 +27,6 @@ static BOOL try_failed_login(struct smbcli_state *cli)
        NTSTATUS status;
        union smb_sesssetup setup;
        struct smbcli_session *session;
-       TALLOC_CTX *mem_ctx = talloc_init("failed_login");
 
        session = smbcli_session_init(cli->transport);
        setup.generic.level = RAW_SESSSETUP_GENERIC;
@@ -37,15 +36,13 @@ static BOOL try_failed_login(struct smbcli_state *cli)
        setup.generic.in.user = "INVALID-USERNAME";
        setup.generic.in.domain = "INVALID-DOMAIN";
 
-       status = smb_raw_session_setup(session, mem_ctx, &setup);
+       status = smb_raw_session_setup(session, session, &setup);
+       talloc_free(session);
        if (NT_STATUS_IS_OK(status)) {
                printf("Allowed session setup with invalid credentials?!\n");
                return False;
        }
 
-       talloc_free(session);
-       talloc_free(mem_ctx);
-
        return True;
 }
 
index 194d2de93ba14c2aa59243cbfebeaee361ba8c0d..b5f439b5e9de06e0470af3c024b64927792fb9c4 100644 (file)
@@ -81,7 +81,6 @@ static BOOL test_session(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
 
        printf("create a second security context on the same transport\n");
        session = smbcli_session_init(cli->transport);
-       talloc_increase_ref_count(cli->transport);
 
        setup.generic.level = RAW_SESSSETUP_GENERIC;
        setup.generic.in.sesskey = cli->transport->negotiate.sesskey;
@@ -97,7 +96,6 @@ static BOOL test_session(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
 
        printf("create a third security context on the same transport, with vuid set\n");
        session2 = smbcli_session_init(cli->transport);
-       talloc_increase_ref_count(cli->transport);
 
        session2->vuid = session->vuid;
        setup.generic.level = RAW_SESSSETUP_GENERIC;
@@ -119,7 +117,6 @@ static BOOL test_session(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
        if (cli->transport->negotiate.capabilities & CAP_EXTENDED_SECURITY) {
                printf("create a fourth security context on the same transport, without extended security\n");
                session3 = smbcli_session_init(cli->transport);
-               talloc_increase_ref_count(cli->transport);
 
                session3->vuid = session->vuid;
                setup.generic.level = RAW_SESSSETUP_GENERIC;
@@ -137,7 +134,6 @@ static BOOL test_session(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
                
        printf("use the same tree as the existing connection\n");
        tree = smbcli_tree_init(session);
-       talloc_increase_ref_count(session);
        tree->tid = cli->tree->tid;
 
        printf("create a file using the new vuid\n");
@@ -228,7 +224,6 @@ static BOOL test_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
        
        printf("create a second tree context on the same session\n");
        tree = smbcli_tree_init(cli->session);
-       talloc_increase_ref_count(cli->session);
 
        tcon.generic.level = RAW_TCON_TCONX;
        tcon.tconx.in.flags = 0;
index 707b0d0313d130dfd2b23fa87682d0cde33afc70..7d12c1b7f3f19c534c897f8f1eb25014758e6a0d 100644 (file)
@@ -308,7 +308,7 @@ BOOL torture_rpc_echo(int dummy)
 */
        printf("\n");
        
-       talloc_destroy(mem_ctx);
+       talloc_free(mem_ctx);
 
         torture_rpc_close(p);
        return ret;