From e27ba5e4c6f684c0fc0f0db9a84164384ff3c0fd Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 12 Nov 2005 02:12:51 +0000 Subject: r11693: added a full async composite function for SMB2 that does: - name resolution - socket connect - negprot - multi-stage session setup - tcon (This used to be commit c1a8e866fe6a0544b7b26da451ea093cdcacdd8f) --- source4/libcli/smb2/config.mk | 3 +- source4/libcli/smb2/connect.c | 280 +++++++++++++++++++++++++++++++++++++++++ source4/torture/smb2/connect.c | 111 ++-------------- 3 files changed, 289 insertions(+), 105 deletions(-) create mode 100644 source4/libcli/smb2/connect.c (limited to 'source4') diff --git a/source4/libcli/smb2/config.mk b/source4/libcli/smb2/config.mk index a180768694..097ed6df12 100644 --- a/source4/libcli/smb2/config.mk +++ b/source4/libcli/smb2/config.mk @@ -6,5 +6,6 @@ OBJ_FILES = \ session.o \ tcon.o \ create.o \ - close.o + close.o \ + connect.o REQUIRED_SUBSYSTEMS = LIBCLI_RAW LIBPACKET diff --git a/source4/libcli/smb2/connect.c b/source4/libcli/smb2/connect.c new file mode 100644 index 0000000000..18f28539ea --- /dev/null +++ b/source4/libcli/smb2/connect.c @@ -0,0 +1,280 @@ +/* + Unix SMB/CIFS implementation. + + SMB2 composite connection setup + + Copyright (C) Andrew Tridgell 2005 + + 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 + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + 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. +*/ + +#include "includes.h" +#include "libcli/raw/libcliraw.h" +#include "libcli/smb2/smb2.h" +#include "libcli/smb2/smb2_calls.h" +#include "libcli/composite/composite.h" + +struct smb2_connect_state { + struct smb2_request *req; + struct composite_context *creq; + struct cli_credentials *credentials; + const char *host; + const char *share; + struct smb2_negprot negprot; + struct smb2_tree_connect tcon; + struct smb2_session *session; + struct smb2_tree *tree; +}; + +/* + continue after tcon reply +*/ +static void continue_tcon(struct smb2_request *req) +{ + struct composite_context *c = talloc_get_type(req->async.private, + struct composite_context); + struct smb2_connect_state *state = talloc_get_type(c->private_data, + struct smb2_connect_state); + + c->status = smb2_tree_connect_recv(req, &state->tcon); + if (!NT_STATUS_IS_OK(c->status)) { + composite_error(c, c->status); + return; + } + + state->tree->tid = state->tcon.out.tid; + + composite_done(c); +} + +/* + continue after a session setup +*/ +static void continue_session(struct composite_context *creq) +{ + struct composite_context *c = talloc_get_type(creq->async.private_data, + struct composite_context); + struct smb2_connect_state *state = talloc_get_type(c->private_data, + struct smb2_connect_state); + + c->status = smb2_session_setup_spnego_recv(creq); + if (!NT_STATUS_IS_OK(c->status)) { + composite_error(c, c->status); + return; + } + + state->tree = smb2_tree_init(state->session, state, True); + if (state->tree == NULL) { + composite_error(c, NT_STATUS_NO_MEMORY); + return; + } + + state->tcon.in.unknown1 = 0x09; + state->tcon.in.path = talloc_asprintf(state, "\\\\%s\\%s", + state->host, state->share); + if (state->tcon.in.path == NULL) { + composite_error(c, NT_STATUS_NO_MEMORY); + return; + } + + state->req = smb2_tree_connect_send(state->tree, &state->tcon); + if (state->req == NULL) { + composite_error(c, NT_STATUS_NO_MEMORY); + return; + } + + state->req->async.fn = continue_tcon; + state->req->async.private = c; +} + +/* + continue after negprot reply +*/ +static void continue_negprot(struct smb2_request *req) +{ + struct composite_context *c = talloc_get_type(req->async.private, + struct composite_context); + struct smb2_connect_state *state = talloc_get_type(c->private_data, + struct smb2_connect_state); + struct smb2_transport *transport = req->transport; + + c->status = smb2_negprot_recv(req, c, &state->negprot); + if (!NT_STATUS_IS_OK(c->status)) { + composite_error(c, c->status); + return; + } + + state->session = smb2_session_init(transport, state, True); + if (state->session == NULL) { + composite_error(c, NT_STATUS_NO_MEMORY); + return; + } + + state->creq = smb2_session_setup_spnego_send(state->session, state->credentials); + if (state->creq == NULL) { + composite_error(c, NT_STATUS_NO_MEMORY); + return; + } + + state->creq->async.fn = continue_session; + state->creq->async.private_data = c; +} + +/* + continue after a socket connect completes +*/ +static void continue_socket(struct composite_context *creq) +{ + struct composite_context *c = talloc_get_type(creq->async.private_data, + struct composite_context); + struct smb2_connect_state *state = talloc_get_type(c->private_data, + struct smb2_connect_state); + struct smbcli_socket *sock; + struct smb2_transport *transport; + + c->status = smbcli_sock_connect_recv(creq, state, &sock); + if (!NT_STATUS_IS_OK(c->status)) { + composite_error(c, c->status); + return; + } + + transport = smb2_transport_init(sock, state); + if (transport == NULL) { + composite_error(c, NT_STATUS_NO_MEMORY); + return; + } + + ZERO_STRUCT(state->negprot); + state->negprot.in.unknown1 = 0x010024; + + state->req = smb2_negprot_send(transport, &state->negprot); + if (state->req == NULL) { + composite_error(c, NT_STATUS_NO_MEMORY); + return; + } + + state->req->async.fn = continue_negprot; + state->req->async.private = c; +} + + +/* + continue after a resolve finishes +*/ +static void continue_resolve(struct composite_context *creq) +{ + struct composite_context *c = talloc_get_type(creq->async.private_data, + struct composite_context); + struct smb2_connect_state *state = talloc_get_type(c->private_data, + struct smb2_connect_state); + const char *addr; + + c->status = resolve_name_recv(creq, state, &addr); + if (!NT_STATUS_IS_OK(c->status)) { + composite_error(c, c->status); + return; + } + + state->creq = smbcli_sock_connect_send(state, addr, 445, state->host, c->event_ctx); + if (state->creq == NULL) { + composite_error(c, NT_STATUS_NO_MEMORY); + return; + } + + state->creq->async.private_data = c; + state->creq->async.fn = continue_socket; +} + +/* + a composite function that does a full negprot/sesssetup/tcon, returning + a connected smb2_tree + */ +struct composite_context *smb2_connect_send(TALLOC_CTX *mem_ctx, + const char *host, + const char *share, + struct cli_credentials *credentials, + struct event_context *ev) +{ + struct composite_context *c; + struct smb2_connect_state *state; + struct nbt_name name; + + c = talloc_zero(mem_ctx, struct composite_context); + if (c == NULL) return NULL; + + state = talloc(c, struct smb2_connect_state); + if (state == NULL) { + c->status = NT_STATUS_NO_MEMORY; + goto failed; + } + + c->state = COMPOSITE_STATE_IN_PROGRESS; + c->private_data = state; + c->event_ctx = ev; + + state->credentials = credentials; + state->host = talloc_strdup(c, host); + state->share = talloc_strdup(c, share); + if (state->host == NULL || state->share == NULL) { + c->status = NT_STATUS_NO_MEMORY; + goto failed; + } + + ZERO_STRUCT(name); + name.name = host; + + state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order()); + if (state->creq == NULL) goto failed; + + state->creq->async.private_data = c; + state->creq->async.fn = continue_resolve; + + return c; + +failed: + composite_trigger_error(c); + return c; +} + +/* + receive a connect reply +*/ +NTSTATUS smb2_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, + struct smb2_tree **tree) +{ + NTSTATUS status; + struct smb2_connect_state *state = talloc_get_type(c->private_data, + struct smb2_connect_state); + status = composite_wait(c); + if (NT_STATUS_IS_OK(status)) { + *tree = talloc_steal(mem_ctx, state->tree); + } + talloc_free(c); + return status; +} + +/* + sync version of smb2_connect +*/ +NTSTATUS smb2_connect(TALLOC_CTX *mem_ctx, + const char *host, const char *share, + struct cli_credentials *credentials, + struct smb2_tree **tree, + struct event_context *ev) +{ + struct composite_context *c = smb2_connect_send(mem_ctx, host, share, + credentials, ev); + return smb2_connect_recv(c, mem_ctx, tree); +} diff --git a/source4/torture/smb2/connect.c b/source4/torture/smb2/connect.c index e88db8ac5b..f76553c019 100644 --- a/source4/torture/smb2/connect.c +++ b/source4/torture/smb2/connect.c @@ -39,100 +39,6 @@ }} while (0) -/* - send a negotiate - */ -static struct smb2_transport *torture_smb2_negprot(TALLOC_CTX *mem_ctx, const char *host) -{ - struct smbcli_socket *socket; - struct smb2_transport *transport; - NTSTATUS status; - struct smb2_negprot io; - - socket = smbcli_sock_connect_byname(host, 445, mem_ctx, NULL); - if (socket == NULL) { - printf("Failed to connect to %s\n", host); - return False; - } - - transport = smb2_transport_init(socket, mem_ctx); - if (transport == NULL) { - printf("Failed to setup smb2 transport\n"); - return False; - } - - ZERO_STRUCT(io); - io.in.unknown1 = 0x010024; - - /* send a negprot */ - status = smb2_negprot(transport, mem_ctx, &io); - if (!NT_STATUS_IS_OK(status)) { - printf("negprot failed - %s\n", nt_errstr(status)); - return NULL; - } - - printf("Negprot reply:\n"); - printf("current_time = %s\n", nt_time_string(mem_ctx, io.out.current_time)); - printf("boot_time = %s\n", nt_time_string(mem_ctx, io.out.boot_time)); - - transport->negotiate.secblob = io.out.secblob; - - return transport; -} - -/* - send a session setup -*/ -static struct smb2_session *torture_smb2_session(struct smb2_transport *transport, - struct cli_credentials *credentials) -{ - struct smb2_session *session; - NTSTATUS status; - - session = smb2_session_init(transport, transport, True); - - status = smb2_session_setup_spnego(session, credentials); - if (!NT_STATUS_IS_OK(status)) { - printf("Session setup failed - %s\n", nt_errstr(status)); - return NULL; - } - - printf("Session setup gave UID 0x%016llx\n", session->uid); - - return session; -} - - -/* - send a tree connect -*/ -static struct smb2_tree *torture_smb2_tree(struct smb2_session *session, - const char *share) -{ - struct smb2_tree *tree; - struct smb2_tree_connect io; - NTSTATUS status; - - tree = smb2_tree_init(session, session, True); - - io.in.unknown1 = 0x09; - io.in.path = talloc_asprintf(tree, "\\\\%s\\%s", - session->transport->socket->hostname, - share); - - status = smb2_tree_connect(tree, &io); - if (!NT_STATUS_IS_OK(status)) { - printf("tcon failed - %s\n", nt_errstr(status)); - return NULL; - } - - printf("Tree connect gave tid = 0x%x\n", io.out.tid); - - tree->tid = io.out.tid; - - return tree; -} - /* send a close */ @@ -221,22 +127,19 @@ static struct smb2_handle torture_smb2_create(struct smb2_tree *tree, BOOL torture_smb2_connect(void) { TALLOC_CTX *mem_ctx = talloc_new(NULL); - struct smb2_transport *transport; - struct smb2_session *session; struct smb2_tree *tree; const char *host = lp_parm_string(-1, "torture", "host"); const char *share = lp_parm_string(-1, "torture", "share"); struct cli_credentials *credentials = cmdline_credentials; struct smb2_handle h1, h2; + NTSTATUS status; - transport = torture_smb2_negprot(mem_ctx, host); - if (transport == NULL) return False; - - session = torture_smb2_session(transport, credentials); - if (session == NULL) return False; - - tree = torture_smb2_tree(session, share); - if (tree == NULL) return False; + status = smb2_connect(mem_ctx, host, share, credentials, &tree, + event_context_find(mem_ctx)); + if (!NT_STATUS_IS_OK(status)) { + printf("Connection failed - %s\n", nt_errstr(status)); + return False; + } h1 = torture_smb2_create(tree, "test9.dat"); h2 = torture_smb2_create(tree, "test9.dat"); -- cgit