diff --git a/source3/client/client.c b/source3/client/client.c index 267e3ebeb99..65012f51260 100644 --- a/source3/client/client.c +++ b/source3/client/client.c @@ -6407,7 +6407,8 @@ static int do_message_op(struct cli_credentials *creds) return 1; } - status = cli_connect_nb(desthost, have_ip ? &dest_ss : NULL, + status = cli_connect_nb(talloc_tos(), + desthost, have_ip ? &dest_ss : NULL, port ? port : NBT_SMB_PORT, name_type, lp_netbios_name(), SMB_SIGNING_OFF, diff --git a/source3/libsmb/cliconnect.c b/source3/libsmb/cliconnect.c index 6d924947016..2d47a1124c8 100644 --- a/source3/libsmb/cliconnect.c +++ b/source3/libsmb/cliconnect.c @@ -2751,15 +2751,21 @@ static NTSTATUS cli_connect_nb_recv(struct tevent_req *req, return NT_STATUS_OK; } -NTSTATUS cli_connect_nb(const char *host, const struct sockaddr_storage *dest_ss, - uint16_t port, int name_type, const char *myname, - enum smb_signing_setting signing_state, int flags, struct cli_state **pcli) +NTSTATUS cli_connect_nb(TALLOC_CTX *mem_ctx, + const char *host, + const struct sockaddr_storage *dest_ss, + uint16_t port, + int name_type, + const char *myname, + enum smb_signing_setting signing_state, + int flags, + struct cli_state **pcli) { struct tevent_context *ev; struct tevent_req *req; NTSTATUS status = NT_STATUS_NO_MEMORY; - ev = samba_tevent_context_init(talloc_tos()); + ev = samba_tevent_context_init(mem_ctx); if (ev == NULL) { goto fail; } @@ -2774,7 +2780,7 @@ NTSTATUS cli_connect_nb(const char *host, const struct sockaddr_storage *dest_ss if (!tevent_req_poll_ntstatus(req, ev, &status)) { goto fail; } - status = cli_connect_nb_recv(req, NULL, pcli); + status = cli_connect_nb_recv(req, mem_ctx, pcli); fail: TALLOC_FREE(ev); return status; diff --git a/source3/libsmb/clidfs.c b/source3/libsmb/clidfs.c index 939b3b0da09..de8ac4dc1ea 100644 --- a/source3/libsmb/clidfs.c +++ b/source3/libsmb/clidfs.c @@ -178,10 +178,20 @@ static NTSTATUS do_connect(TALLOC_CTX *ctx, return NT_STATUS_INVALID_PARAMETER; } - status = cli_connect_nb( - server, dest_ss, port, name_type, NULL, - signing_state, - flags, &c); + /* + * The functions cli_resolve_path() and cli_cm_open() might not create a + * new cli context, but might return an already existing one. This + * forces us to have a long lived cli allocated on the NULL context. + */ + status = cli_connect_nb(NULL, + server, + dest_ss, + port, + name_type, + NULL, + signing_state, + flags, + &c); if (!NT_STATUS_IS_OK(status)) { if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) { diff --git a/source3/libsmb/libsmb_server.c b/source3/libsmb/libsmb_server.c index 69b51b829f6..41957bb6eb7 100644 --- a/source3/libsmb/libsmb_server.c +++ b/source3/libsmb/libsmb_server.c @@ -529,9 +529,15 @@ SMBC_server_internal(TALLOC_CTX *ctx, /* * Try 139 first for IPC$ */ - status = cli_connect_nb(server_n, NULL, NBT_SMB_PORT, 0x20, - smbc_getNetbiosName(context), - signing_state, flags, &c); + status = cli_connect_nb(NULL, + server_n, + NULL, + NBT_SMB_PORT, + 0x20, + smbc_getNetbiosName(context), + signing_state, + flags, + &c); } } @@ -539,9 +545,15 @@ SMBC_server_internal(TALLOC_CTX *ctx, /* * No IPC$ or 139 did not work */ - status = cli_connect_nb(server_n, NULL, port, 0x20, + status = cli_connect_nb(NULL, + server_n, + NULL, + port, + 0x20, smbc_getNetbiosName(context), - signing_state, flags, &c); + signing_state, + flags, + &c); } if (!NT_STATUS_IS_OK(status)) { diff --git a/source3/libsmb/passchange.c b/source3/libsmb/passchange.c index b3304ed6fc5..3231d0bfff2 100644 --- a/source3/libsmb/passchange.c +++ b/source3/libsmb/passchange.c @@ -44,8 +44,15 @@ NTSTATUS remote_password_change(const char *remote_machine, *err_str = NULL; - result = cli_connect_nb(remote_machine, NULL, 0, 0x20, NULL, - SMB_SIGNING_IPC_DEFAULT, 0, &cli); + result = cli_connect_nb(talloc_tos(), + remote_machine, + NULL, + 0, + 0x20, + NULL, + SMB_SIGNING_IPC_DEFAULT, + 0, + &cli); if (!NT_STATUS_IS_OK(result)) { if (NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED)) { if (asprintf(err_str, "Unable to connect to SMB server on " diff --git a/source3/libsmb/proto.h b/source3/libsmb/proto.h index 35b6577a4bd..ec24f411a4c 100644 --- a/source3/libsmb/proto.h +++ b/source3/libsmb/proto.h @@ -82,9 +82,15 @@ NTSTATUS cli_tree_connect_creds(struct cli_state *cli, NTSTATUS cli_tree_connect(struct cli_state *cli, const char *share, const char *dev, const char *pass); NTSTATUS cli_tdis(struct cli_state *cli); -NTSTATUS cli_connect_nb(const char *host, const struct sockaddr_storage *dest_ss, - uint16_t port, int name_type, const char *myname, - enum smb_signing_setting signing_state, int flags, struct cli_state **pcli); +NTSTATUS cli_connect_nb(TALLOC_CTX *mem_ctx, + const char *host, + const struct sockaddr_storage *dest_ss, + uint16_t port, + int name_type, + const char *myname, + enum smb_signing_setting signing_state, + int flags, + struct cli_state **pcli); NTSTATUS cli_start_connection(struct cli_state **output_cli, const char *my_name, const char *dest_host, diff --git a/source3/nmbd/nmbd_synclists.c b/source3/nmbd/nmbd_synclists.c index 7f514dfe142..773ba7b3d2c 100644 --- a/source3/nmbd/nmbd_synclists.c +++ b/source3/nmbd/nmbd_synclists.c @@ -84,9 +84,15 @@ static void sync_child(char *name, int nm_type, in_addr_to_sockaddr_storage(&ss, ip); - status = cli_connect_nb(name, &ss, NBT_SMB_PORT, nm_type, - get_local_machine_name(), SMB_SIGNING_DEFAULT, - 0, &cli); + status = cli_connect_nb(talloc_tos(), + name, + &ss, + NBT_SMB_PORT, + nm_type, + get_local_machine_name(), + SMB_SIGNING_DEFAULT, + 0, + &cli); if (!NT_STATUS_IS_OK(status)) { return; } diff --git a/source3/torture/torture.c b/source3/torture/torture.c index d2b97b9bfee..ca66a6db5fc 100644 --- a/source3/torture/torture.c +++ b/source3/torture/torture.c @@ -157,8 +157,15 @@ static struct cli_state *open_nbt_connection(void) flags |= CLI_FULL_CONNECTION_FORCE_DOS_ERRORS; } - status = cli_connect_nb(host, NULL, port_to_use, 0x20, myname, - signing_state, flags, &c); + status = cli_connect_nb(NULL, + host, + NULL, + port_to_use, + 0x20, + myname, + signing_state, + flags, + &c); if (!NT_STATUS_IS_OK(status)) { printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) ); return NULL; diff --git a/source3/utils/net_rpc.c b/source3/utils/net_rpc.c index 2a12b1a1335..ee186c944c6 100644 --- a/source3/utils/net_rpc.c +++ b/source3/utils/net_rpc.c @@ -7501,9 +7501,15 @@ bool net_rpc_check(struct net_context *c, unsigned flags) if (!net_find_server(c, NULL, flags, &server_ss, &server_name)) return false; - status = cli_connect_nb(server_name, &server_ss, 0, 0x20, - lp_netbios_name(), SMB_SIGNING_IPC_DEFAULT, - 0, &cli); + status = cli_connect_nb(c, + server_name, + &server_ss, + 0, + 0x20, + lp_netbios_name(), + SMB_SIGNING_IPC_DEFAULT, + 0, + &cli); if (!NT_STATUS_IS_OK(status)) { if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) { DBG_ERR("NetBIOS support disabled, unable to connect\n"); diff --git a/source3/utils/net_time.c b/source3/utils/net_time.c index f58d62b45ce..91614a11dc2 100644 --- a/source3/utils/net_time.c +++ b/source3/utils/net_time.c @@ -34,8 +34,15 @@ static time_t cli_servertime(const char *host, struct cli_state *cli = NULL; NTSTATUS status; - status = cli_connect_nb(host, dest_ss, 0, 0x20, lp_netbios_name(), - SMB_SIGNING_DEFAULT, 0, &cli); + status = cli_connect_nb(talloc_tos(), + host, + dest_ss, + 0, + 0x20, + lp_netbios_name(), + SMB_SIGNING_DEFAULT, + 0, + &cli); if (!NT_STATUS_IS_OK(status)) { if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) { fprintf(stderr, "Can't contact server %s. NetBIOS support disabled,"