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

r2710: continue with the new style of providing a parent context whenever

possible to a structure creation routine. This makes for much easier
global cleanup.
This commit is contained in:
Andrew Tridgell 2004-09-28 05:44:59 +00:00 committed by Gerald (Jerry) Carter
parent 911a8d590c
commit e14ee428ec
19 changed files with 124 additions and 96 deletions

View File

@ -169,7 +169,7 @@ static NTSTATUS connect_to_domain_password_server(struct smbcli_state **cli,
return NT_STATUS_NO_LOGON_SERVERS;
/* Attempt connection */
result = smbcli_full_connection(cli, lp_netbios_name(), remote_machine,
result = smbcli_full_connection(NULL, cli, lp_netbios_name(), remote_machine,
&dest_ip, 0, "IPC$", "IPC", "", "", "",0, retry);
if (!NT_STATUS_IS_OK(result)) {

View File

@ -2727,7 +2727,7 @@ static struct smbcli_state *do_connect(const char *server, const char *share)
again:
/* have to open a new connection */
if (!(c=smbcli_state_init()) || !smbcli_socket_connect(c, server_n)) {
if (!(c=smbcli_state_init(NULL)) || !smbcli_socket_connect(c, server_n)) {
d_printf("Connection to %s failed\n", server_n);
return NULL;
}
@ -2858,7 +2858,7 @@ static int do_message_op(void)
server_name = dest_ip ? dest_ip : desthost;
if (!(cli=smbcli_state_init()) || !smbcli_socket_connect(cli, server_name)) {
if (!(cli=smbcli_state_init(NULL)) || !smbcli_socket_connect(cli, server_name)) {
d_printf("Connection to %s failed\n", server_name);
return 1;
}

View File

@ -279,7 +279,7 @@ smb_connect(const char *workgroup, /* I - Workgroup */
myname = get_myname();
nt_status = smbcli_full_connection(&c, myname, server, NULL, 0, share, "?????",
nt_status = smbcli_full_connection(NULL, &c, myname, server, NULL, 0, share, "?????",
username, workgroup, password, 0, NULL);
free(myname);

View File

@ -27,7 +27,7 @@ BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
{
struct smbcli_socket *sock;
sock = smbcli_sock_init();
sock = smbcli_sock_init(cli);
if (!sock) return False;
if (!smbcli_sock_connect_byname(sock, server, 0)) {
@ -149,17 +149,18 @@ NTSTATUS smbcli_send_tconX(struct smbcli_state *cli, const char *sharename,
/*
easy way to get to a fully connected smbcli_state in one call
*/
NTSTATUS smbcli_full_connection(struct smbcli_state **ret_cli,
const char *myname,
const char *host,
struct in_addr *ip,
const char *sharename,
const char *devtype,
const char *username,
const char *domain,
const char *password,
uint_t flags,
BOOL *retry)
NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
struct smbcli_state **ret_cli,
const char *myname,
const char *host,
struct in_addr *ip,
const char *sharename,
const char *devtype,
const char *username,
const char *domain,
const char *password,
uint_t flags,
BOOL *retry)
{
struct smbcli_tree *tree;
NTSTATUS status;
@ -177,21 +178,23 @@ NTSTATUS smbcli_full_connection(struct smbcli_state **ret_cli,
username = talloc_strdup(mem_ctx, p+1);
}
status = smbcli_tree_full_connection(&tree, myname, host, 0, sharename, devtype,
status = smbcli_tree_full_connection(parent_ctx,
&tree, myname, host, 0, sharename, devtype,
username, domain, password);
if (!NT_STATUS_IS_OK(status)) {
goto done;
}
(*ret_cli) = smbcli_state_init();
(*ret_cli) = smbcli_state_init(parent_ctx);
(*ret_cli)->tree = talloc_reference(*ret_cli, tree);
talloc_free(tree);
(*ret_cli)->tree = tree;
(*ret_cli)->session = tree->session;
(*ret_cli)->transport = tree->session->transport;
talloc_steal(*ret_cli, tree->session->transport->socket);
done:
talloc_free(mem_ctx);
return status;
}
@ -207,11 +210,11 @@ NTSTATUS smbcli_tdis(struct smbcli_state *cli)
/****************************************************************************
Initialise a client state structure.
****************************************************************************/
struct smbcli_state *smbcli_state_init(void)
struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
{
struct smbcli_state *cli;
cli = talloc_p(NULL, struct smbcli_state);
cli = talloc_p(mem_ctx, struct smbcli_state);
if (cli) {
ZERO_STRUCTP(cli);
}

View File

@ -244,7 +244,7 @@ int smbcli_dfs_open_connection(struct smbcli_client* cluster,
return -1;
c = cluster->cli[i];
if (NT_STATUS_IS_ERR(smbcli_full_connection(&c,
if (NT_STATUS_IS_ERR(smbcli_full_connection(NULL, &c,
NULL, host, NULL, 0,
share, "?????",
cluster->username, cluster->workgroup,

View File

@ -37,11 +37,11 @@ static int sock_destructor(void *ptr)
/*
create a smbcli_socket context
*/
struct smbcli_socket *smbcli_sock_init(void)
struct smbcli_socket *smbcli_sock_init(TALLOC_CTX *mem_ctx)
{
struct smbcli_socket *sock;
sock = talloc_p(NULL, struct smbcli_socket);
sock = talloc_p(mem_ctx, struct smbcli_socket);
if (!sock) {
return NULL;
}

View File

@ -151,7 +151,8 @@ NTSTATUS smb_tree_disconnect(struct smbcli_tree *tree)
a convenient function to establish a smbcli_tree from scratch, using reasonable default
parameters
*/
NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
NTSTATUS smbcli_tree_full_connection(TALLOC_CTX *parent_ctx,
struct smbcli_tree **ret_tree,
const char *my_name,
const char *dest_host, int port,
const char *service, const char *service_type,
@ -172,7 +173,7 @@ NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
*ret_tree = NULL;
sock = smbcli_sock_init();
sock = smbcli_sock_init(parent_ctx);
if (!sock) {
return NT_STATUS_NO_MEMORY;
}

View File

@ -334,6 +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);
return NT_STATUS_OK;
@ -433,7 +434,7 @@ NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe **p,
}
smb->fnum = io.ntcreatex.out.fnum;
smb->tree = talloc_reference(smb, tree);
smb->tree = tree;
(*p)->transport.private = smb;

View File

@ -460,12 +460,12 @@ static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **p,
}
if (!username || !username[0]) {
status = smbcli_full_connection(&cli, lp_netbios_name(),
status = smbcli_full_connection(NULL, &cli, lp_netbios_name(),
binding->host, NULL,
"ipc$", "?????",
"", "", NULL, 0, &retry);
} else {
status = smbcli_full_connection(&cli, lp_netbios_name(),
status = smbcli_full_connection(NULL, &cli, lp_netbios_name(),
binding->host, NULL,
"ipc$", "?????",
username, domain,
@ -486,7 +486,6 @@ static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **p,
/* this ensures that the reference count is decremented so
a pipe close will really close the link */
talloc_free(cli->tree);
talloc_steal(*p, cli);
(*p)->flags = binding->flags;

View File

@ -122,13 +122,14 @@ static NTSTATUS cvfs_connect(struct smbsrv_request *req, const char *sharename,
ntvfs_set_private(req->tcon, depth, private);
status = smbcli_tree_full_connection(&private->tree,
"vfs_cifs",
host,
0,
remote_share, "?????",
user, domain,
pass);
status = smbcli_tree_full_connection(private,
&private->tree,
"vfs_cifs",
host,
0,
remote_share, "?????",
user, domain,
pass);
if (!NT_STATUS_IS_OK(status)) {
return status;
}

View File

@ -38,7 +38,6 @@ struct ipc_private {
/* a list of open pipes */
struct pipe_state {
struct pipe_state *next, *prev;
TALLOC_CTX *mem_ctx;
const char *pipe_name;
uint16_t fnum;
struct dcesrv_connection *dce_conn;
@ -85,10 +84,9 @@ again:
*/
static void pipe_shutdown(struct ipc_private *private, struct pipe_state *p)
{
TALLOC_CTX *mem_ctx = private->pipe_list->mem_ctx;
dcesrv_endpoint_disconnect(private->pipe_list->dce_conn);
DLIST_REMOVE(private->pipe_list, private->pipe_list);
talloc_destroy(mem_ctx);
talloc_free(p->dce_conn);
DLIST_REMOVE(private->pipe_list, p);
talloc_destroy(p);
}
@ -199,33 +197,25 @@ static NTSTATUS ipc_open_generic(struct smbsrv_request *req, const char *fname,
struct pipe_state **ps)
{
struct pipe_state *p;
TALLOC_CTX *mem_ctx;
NTSTATUS status;
struct dcesrv_ep_description ep_description;
struct auth_session_info *session_info = NULL;
NTVFS_GET_PRIVATE(ipc_private, private, req);
mem_ctx = talloc_init("ipc_open '%s'", fname);
if (!mem_ctx) {
return NT_STATUS_NO_MEMORY;
}
p = talloc(mem_ctx, sizeof(struct pipe_state));
p = talloc_p(private, struct pipe_state);
if (!p) {
talloc_destroy(mem_ctx);
return NT_STATUS_NO_MEMORY;
}
p->mem_ctx = mem_ctx;
p->pipe_name = talloc_strdup(mem_ctx, fname);
p->pipe_name = talloc_strdup(p, fname);
if (!p->pipe_name) {
talloc_destroy(mem_ctx);
talloc_free(p);
return NT_STATUS_NO_MEMORY;
}
p->fnum = find_next_fnum(private);
if (p->fnum == 0) {
talloc_destroy(mem_ctx);
talloc_free(p);
return NT_STATUS_TOO_MANY_OPENED_FILES;
}
@ -261,7 +251,7 @@ static NTSTATUS ipc_open_generic(struct smbsrv_request *req, const char *fname,
session_info,
&p->dce_conn);
if (!NT_STATUS_IS_OK(status)) {
talloc_destroy(mem_ctx);
talloc_free(p);
return status;
}

View File

@ -268,6 +268,29 @@ NTSTATUS dcesrv_fetch_session_key(struct dcesrv_connection *p,
}
/*
destroy a link to an endpoint
*/
static int dcesrv_endpoint_destructor(void *ptr)
{
struct dcesrv_connection *p = ptr;
if (p->iface) {
p->iface->unbind(p, p->iface);
}
/* destroy any handles */
while (p->handles) {
dcesrv_handle_destroy(p, p->handles);
}
if (p->auth_state.gensec_security) {
gensec_end(&p->auth_state.gensec_security);
}
return 0;
}
/*
connect to a dcerpc endpoint
*/
@ -294,6 +317,8 @@ NTSTATUS dcesrv_endpoint_connect(struct dcesrv_context *dce_ctx,
(*p)->auth_state.session_key = dcesrv_generic_session_key;
(*p)->srv_conn = NULL;
talloc_set_destructor(*p, dcesrv_endpoint_destructor);
return NT_STATUS_OK;
}
@ -332,27 +357,6 @@ NTSTATUS dcesrv_endpoint_search_connect(struct dcesrv_context *dce_ctx,
}
/*
disconnect a link to an endpoint
*/
void dcesrv_endpoint_disconnect(struct dcesrv_connection *p)
{
if (p->iface) {
p->iface->unbind(p, p->iface);
}
/* destroy any handles */
while (p->handles) {
dcesrv_handle_destroy(p, p->handles);
}
if (p->auth_state.gensec_security) {
gensec_end(&p->auth_state.gensec_security);
}
talloc_free(p);
}
static void dcesrv_init_hdr(struct dcerpc_packet *pkt)
{
pkt->rpc_vers = 5;

View File

@ -171,7 +171,7 @@ static BOOL connect_servers(void)
printf("Connecting to \\\\%s\\%s as %s - instance %d\n",
servers[i].server_name, servers[i].share_name,
servers[i].username, j);
status = smbcli_full_connection(&servers[i].cli[j],
status = smbcli_full_connection(NULL, &servers[i].cli[j],
"gentest",
servers[i].server_name, NULL,
servers[i].share_name, "?????",

View File

@ -119,7 +119,7 @@ static struct smbcli_state *connect_one(char *share, int snum)
slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
do {
status = smbcli_full_connection(&c, myname,
status = smbcli_full_connection(NULL, &c, myname,
server, NULL,
share, "?????",
servers[snum].username, lp_workgroup(),

View File

@ -173,9 +173,10 @@ static struct smbcli_state *connect_one(char *share)
slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
nt_status = smbcli_full_connection(&c, myname, server_n, NULL, 0, share, "?????",
username, lp_workgroup(), password, 0,
NULL);
nt_status = smbcli_full_connection(NULL,
&c, myname, server_n, NULL, 0, share, "?????",
username, lp_workgroup(), password, 0,
NULL);
if (!NT_STATUS_IS_OK(nt_status)) {
DEBUG(0, ("smbcli_full_connection failed with error %s\n", nt_errstr(nt_status)));

View File

@ -169,7 +169,7 @@ static struct smbcli_state *connect_one(char *share)
*share = 0;
share++;
status = smbcli_full_connection(&c, "masktest",
status = smbcli_full_connection(NULL, &c, "masktest",
server, NULL,
share, "?????",
username, lp_workgroup(),

View File

@ -173,7 +173,6 @@ static BOOL test_session(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
printf("logoff the new vuid\n");
status = smb_raw_ulogoff(session);
CHECK_STATUS(status, NT_STATUS_OK);
talloc_free(session);
printf("the new vuid should not now be accessible\n");
status = smb_raw_write(tree, &wr);
@ -186,9 +185,8 @@ static BOOL test_session(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
status = smb_raw_close(cli->tree, &cl);
CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
/* close down the new tree, which will also close the session
as the reference count will be 0 */
talloc_free(tree);
talloc_free(session);
done:
return ret;

View File

@ -2887,30 +2887,40 @@ static BOOL test_Connect(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
struct samr_Connect4 r4;
struct samr_Connect5 r5;
union samr_ConnectInfo info;
BOOL ret = True;
struct policy_handle h;
BOOL ret = True, got_handle = False;
printf("testing samr_Connect\n");
r.in.system_name = 0;
r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
r.out.connect_handle = handle;
r.out.connect_handle = &h;
status = dcerpc_samr_Connect(p, mem_ctx, &r);
if (!NT_STATUS_IS_OK(status)) {
printf("Connect failed - %s\n", nt_errstr(status));
ret = False;
} else {
got_handle = True;
*handle = h;
}
printf("testing samr_Connect2\n");
r2.in.system_name = NULL;
r2.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
r2.out.connect_handle = handle;
r2.out.connect_handle = &h;
status = dcerpc_samr_Connect2(p, mem_ctx, &r2);
if (!NT_STATUS_IS_OK(status)) {
printf("Connect2 failed - %s\n", nt_errstr(status));
ret = False;
} else {
if (got_handle) {
test_Close(p, mem_ctx, handle);
}
got_handle = True;
*handle = h;
}
printf("testing samr_Connect3\n");
@ -2918,12 +2928,18 @@ static BOOL test_Connect(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
r3.in.system_name = NULL;
r3.in.unknown = 0;
r3.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
r3.out.connect_handle = handle;
r3.out.connect_handle = &h;
status = dcerpc_samr_Connect3(p, mem_ctx, &r3);
if (!NT_STATUS_IS_OK(status)) {
printf("Connect3 failed - %s\n", nt_errstr(status));
ret = False;
} else {
if (got_handle) {
test_Close(p, mem_ctx, handle);
}
got_handle = True;
*handle = h;
}
printf("testing samr_Connect4\n");
@ -2931,12 +2947,18 @@ static BOOL test_Connect(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
r4.in.system_name = "";
r4.in.unknown = 0;
r4.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
r4.out.connect_handle = handle;
r4.out.connect_handle = &h;
status = dcerpc_samr_Connect4(p, mem_ctx, &r4);
if (!NT_STATUS_IS_OK(status)) {
printf("Connect4 failed - %s\n", nt_errstr(status));
ret = False;
} else {
if (got_handle) {
test_Close(p, mem_ctx, handle);
}
got_handle = True;
*handle = h;
}
printf("testing samr_Connect5\n");
@ -2949,12 +2971,18 @@ static BOOL test_Connect(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
r5.in.level = 1;
r5.in.info = &info;
r5.out.info = &info;
r5.out.connect_handle = handle;
r5.out.connect_handle = &h;
status = dcerpc_samr_Connect5(p, mem_ctx, &r5);
if (!NT_STATUS_IS_OK(status)) {
printf("Connect5 failed - %s\n", nt_errstr(status));
ret = False;
} else {
if (got_handle) {
test_Close(p, mem_ctx, handle);
}
got_handle = True;
*handle = h;
}
return ret;

View File

@ -43,7 +43,7 @@ static struct smbcli_state *open_nbt_connection(void)
make_nmb_name(&calling, lp_netbios_name(), 0x0);
choose_called_name(&called, host, 0x20);
cli = smbcli_state_init();
cli = smbcli_state_init(NULL);
if (!cli) {
printf("Failed initialize smbcli_struct to connect with %s\n", host);
return NULL;
@ -93,7 +93,8 @@ BOOL torture_open_connection_share(struct smbcli_state **c,
if (use_kerberos)
flags |= SMBCLI_FULL_CONNECTION_USE_KERBEROS;
status = smbcli_full_connection(c, lp_netbios_name(),
status = smbcli_full_connection(NULL,
c, lp_netbios_name(),
hostname, NULL,
sharename, "?????",
username, username[0]?userdomain:"",
@ -857,7 +858,8 @@ static BOOL run_tcon_devtype_test(int dummy)
const char *userdomain = lp_parm_string(-1, "torture", "userdomain");
const char *password = lp_parm_string(-1, "torture", "password");
status = smbcli_full_connection(&cli1, lp_netbios_name(),
status = smbcli_full_connection(NULL,
&cli1, lp_netbios_name(),
host, NULL,
share, "?????",
username, userdomain,