1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-11 05:18:09 +03:00

r2680: switched the libcli/raw/ code over to use talloc_reference(), which simplifies things quite a bit

This commit is contained in:
Andrew Tridgell 2004-09-27 08:41:39 +00:00 committed by Gerald (Jerry) Carter
parent 9087fab0ad
commit c82a9cf750
9 changed files with 26 additions and 67 deletions

View File

@ -36,8 +36,8 @@ BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
} }
cli->transport = smbcli_transport_init(sock); cli->transport = smbcli_transport_init(sock);
talloc_free(sock);
if (!cli->transport) { if (!cli->transport) {
talloc_free(sock);
return False; return False;
} }
@ -60,9 +60,9 @@ NTSTATUS smbcli_negprot(struct smbcli_state *cli)
/* wrapper around smb_raw_session_setup() */ /* wrapper around smb_raw_session_setup() */
NTSTATUS smbcli_session_setup(struct smbcli_state *cli, NTSTATUS smbcli_session_setup(struct smbcli_state *cli,
const char *user, const char *user,
const char *password, const char *password,
const char *domain) const char *domain)
{ {
union smb_sesssetup setup; union smb_sesssetup setup;
NTSTATUS status; NTSTATUS status;
@ -70,6 +70,7 @@ NTSTATUS smbcli_session_setup(struct smbcli_state *cli,
cli->session = smbcli_session_init(cli->transport); cli->session = smbcli_session_init(cli->transport);
if (!cli->session) return NT_STATUS_UNSUCCESSFUL; if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
talloc_free(cli->transport);
mem_ctx = talloc_init("smbcli_session_setup"); mem_ctx = talloc_init("smbcli_session_setup");
if (!mem_ctx) return NT_STATUS_NO_MEMORY; 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; NTSTATUS status;
cli->tree = smbcli_tree_init(cli->session); cli->tree = smbcli_tree_init(cli->session);
talloc_free(cli->session);
if (!cli->tree) return NT_STATUS_UNSUCCESSFUL; if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
mem_ctx = talloc_init("tcon"); 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) = 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)->session = tree->session;
(*ret_cli)->transport = tree->session->transport; (*ret_cli)->transport = tree->session->transport;
@ -221,7 +224,5 @@ struct smbcli_state *smbcli_state_init(void)
****************************************************************************/ ****************************************************************************/
void smbcli_shutdown(struct smbcli_state *cli) void smbcli_shutdown(struct smbcli_state *cli)
{ {
if (!cli) return;
talloc_free(cli->tree);
talloc_free(cli); talloc_free(cli);
} }

View File

@ -27,16 +27,6 @@
} while (0) } 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 Initialize the session context
****************************************************************************/ ****************************************************************************/
@ -50,12 +40,10 @@ struct smbcli_session *smbcli_session_init(struct smbcli_transport *transport)
} }
ZERO_STRUCTP(session); ZERO_STRUCTP(session);
session->transport = transport; session->transport = talloc_reference(session, transport);
session->pid = (uint16_t)getpid(); session->pid = (uint16_t)getpid();
session->vuid = UID_FIELD_INVALID; session->vuid = UID_FIELD_INVALID;
talloc_set_destructor(session, session_destroy);
return session; return session;
} }

View File

@ -52,7 +52,6 @@ static int transport_destructor(void *ptr)
event_remove_fd(transport->event.ctx, transport->event.fde); event_remove_fd(transport->event.ctx, transport->event.fde);
event_remove_timed(transport->event.ctx, transport->event.te); event_remove_timed(transport->event.ctx, transport->event.te);
event_context_destroy(transport->event.ctx); event_context_destroy(transport->event.ctx);
talloc_free(transport->socket);
return 0; return 0;
} }
@ -75,7 +74,7 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock)
return NULL; return NULL;
} }
transport->socket = sock; transport->socket = talloc_reference(transport, sock);
transport->negotiate.protocol = PROTOCOL_NT1; transport->negotiate.protocol = PROTOCOL_NT1;
transport->options.use_spnego = lp_use_spnego(); transport->options.use_spnego = lp_use_spnego();
transport->negotiate.max_xmit = ~0; transport->negotiate.max_xmit = ~0;

View File

@ -26,16 +26,6 @@
if (!req) return NULL; \ if (!req) return NULL; \
} while (0) } 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 Initialize the tree context
****************************************************************************/ ****************************************************************************/
@ -49,8 +39,7 @@ struct smbcli_tree *smbcli_tree_init(struct smbcli_session *session)
} }
ZERO_STRUCTP(tree); ZERO_STRUCTP(tree);
tree->session = session; tree->session = talloc_reference(tree, session);
talloc_set_destructor(tree, tree_destructor);
return tree; return tree;
} }
@ -188,17 +177,16 @@ NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
return NT_STATUS_NO_MEMORY; return NT_STATUS_NO_MEMORY;
} }
talloc_set_name_const(sock, "smbcli_tree_full_connection");
/* open a TCP socket to the server */ /* open a TCP socket to the server */
if (!smbcli_sock_connect_byname(sock, dest_host, port)) { if (!smbcli_sock_connect_byname(sock, dest_host, port)) {
talloc_free(sock);
DEBUG(2,("Failed to establish socket connection - %s\n", strerror(errno))); DEBUG(2,("Failed to establish socket connection - %s\n", strerror(errno)));
return NT_STATUS_UNSUCCESSFUL; return NT_STATUS_UNSUCCESSFUL;
} }
transport = smbcli_transport_init(sock); transport = smbcli_transport_init(sock);
talloc_free(sock);
if (!transport) { if (!transport) {
talloc_free(sock);
return NT_STATUS_NO_MEMORY; return NT_STATUS_NO_MEMORY;
} }
@ -220,8 +208,8 @@ NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
} }
session = smbcli_session_init(transport); session = smbcli_session_init(transport);
talloc_free(transport);
if (!session) { if (!session) {
talloc_free(transport);
return NT_STATUS_NO_MEMORY; 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; session->vuid = setup.generic.out.vuid;
tree = smbcli_tree_init(session); tree = smbcli_tree_init(session);
talloc_free(session);
if (!tree) { if (!tree) {
talloc_free(session);
talloc_free(mem_ctx); talloc_free(mem_ctx);
return NT_STATUS_NO_MEMORY; return NT_STATUS_NO_MEMORY;
} }

View File

@ -334,7 +334,7 @@ static NTSTATUS smb_shutdown_pipe(struct dcerpc_pipe *p)
c.close.in.fnum = smb->fnum; c.close.in.fnum = smb->fnum;
c.close.in.write_time = 0; c.close.in.write_time = 0;
smb_raw_close(smb->tree, &c); smb_raw_close(smb->tree, &c);
talloc_free(smb->tree); talloc_free(smb);
return NT_STATUS_OK; return NT_STATUS_OK;
} }
@ -371,15 +371,14 @@ NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe **p,
{ {
struct smb_private *smb; struct smb_private *smb;
NTSTATUS status; NTSTATUS status;
char *name = NULL; char *name;
union smb_open io; union smb_open io;
TALLOC_CTX *mem_ctx;
asprintf(&name, "\\%s", pipe_name); name = talloc_asprintf(tree, "\\%s", pipe_name);
if (!name) { if (!name) {
return NT_STATUS_NO_MEMORY; return NT_STATUS_NO_MEMORY;
} }
io.ntcreatex.level = RAW_OPEN_NTCREATEX; io.ntcreatex.level = RAW_OPEN_NTCREATEX;
io.ntcreatex.in.flags = 0; io.ntcreatex.in.flags = 0;
io.ntcreatex.in.root_fid = 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.security_flags = 0;
io.ntcreatex.in.fname = name; io.ntcreatex.in.fname = name;
mem_ctx = talloc_init("torture_rpc_connection"); status = smb_raw_open(tree, name, &io);
if (!mem_ctx) { talloc_free(name);
free(name);
return NT_STATUS_NO_MEMORY;
}
status = smb_raw_open(tree, mem_ctx, &io);
free(name);
talloc_free(mem_ctx);
if (!NT_STATUS_IS_OK(status)) { if (!NT_STATUS_IS_OK(status)) {
return status; return status;
@ -440,10 +433,9 @@ NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe **p,
} }
smb->fnum = io.ntcreatex.out.fnum; smb->fnum = io.ntcreatex.out.fnum;
smb->tree = tree; smb->tree = talloc_reference(smb, tree);
(*p)->transport.private = smb; (*p)->transport.private = smb;
talloc_increase_ref_count(tree);
return NT_STATUS_OK; return NT_STATUS_OK;
} }

View File

@ -68,8 +68,7 @@ void *samdb_connect(TALLOC_CTX *mem_ctx)
second open due to the broken nature of unix locking. second open due to the broken nature of unix locking.
*/ */
if (ctx != NULL) { if (ctx != NULL) {
talloc_increase_ref_count(ctx); return talloc_reference(mem_ctx, ctx);
return ctx;
} }
ctx = talloc_p(mem_ctx, struct samdb_context); ctx = talloc_p(mem_ctx, struct samdb_context);

View File

@ -27,7 +27,6 @@ static BOOL try_failed_login(struct smbcli_state *cli)
NTSTATUS status; NTSTATUS status;
union smb_sesssetup setup; union smb_sesssetup setup;
struct smbcli_session *session; struct smbcli_session *session;
TALLOC_CTX *mem_ctx = talloc_init("failed_login");
session = smbcli_session_init(cli->transport); session = smbcli_session_init(cli->transport);
setup.generic.level = RAW_SESSSETUP_GENERIC; 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.user = "INVALID-USERNAME";
setup.generic.in.domain = "INVALID-DOMAIN"; 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)) { if (NT_STATUS_IS_OK(status)) {
printf("Allowed session setup with invalid credentials?!\n"); printf("Allowed session setup with invalid credentials?!\n");
return False; return False;
} }
talloc_free(session);
talloc_free(mem_ctx);
return True; return True;
} }

View 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"); printf("create a second security context on the same transport\n");
session = smbcli_session_init(cli->transport); session = smbcli_session_init(cli->transport);
talloc_increase_ref_count(cli->transport);
setup.generic.level = RAW_SESSSETUP_GENERIC; setup.generic.level = RAW_SESSSETUP_GENERIC;
setup.generic.in.sesskey = cli->transport->negotiate.sesskey; 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"); printf("create a third security context on the same transport, with vuid set\n");
session2 = smbcli_session_init(cli->transport); session2 = smbcli_session_init(cli->transport);
talloc_increase_ref_count(cli->transport);
session2->vuid = session->vuid; session2->vuid = session->vuid;
setup.generic.level = RAW_SESSSETUP_GENERIC; 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) { if (cli->transport->negotiate.capabilities & CAP_EXTENDED_SECURITY) {
printf("create a fourth security context on the same transport, without extended security\n"); printf("create a fourth security context on the same transport, without extended security\n");
session3 = smbcli_session_init(cli->transport); session3 = smbcli_session_init(cli->transport);
talloc_increase_ref_count(cli->transport);
session3->vuid = session->vuid; session3->vuid = session->vuid;
setup.generic.level = RAW_SESSSETUP_GENERIC; 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"); printf("use the same tree as the existing connection\n");
tree = smbcli_tree_init(session); tree = smbcli_tree_init(session);
talloc_increase_ref_count(session);
tree->tid = cli->tree->tid; tree->tid = cli->tree->tid;
printf("create a file using the new vuid\n"); 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"); printf("create a second tree context on the same session\n");
tree = smbcli_tree_init(cli->session); tree = smbcli_tree_init(cli->session);
talloc_increase_ref_count(cli->session);
tcon.generic.level = RAW_TCON_TCONX; tcon.generic.level = RAW_TCON_TCONX;
tcon.tconx.in.flags = 0; tcon.tconx.in.flags = 0;

View File

@ -308,7 +308,7 @@ BOOL torture_rpc_echo(int dummy)
*/ */
printf("\n"); printf("\n");
talloc_destroy(mem_ctx); talloc_free(mem_ctx);
torture_rpc_close(p); torture_rpc_close(p);
return ret; return ret;