diff options
Diffstat (limited to 'source3/libsmb')
-rw-r--r-- | source3/libsmb/async_smb.c | 129 | ||||
-rw-r--r-- | source3/libsmb/clidgram.c | 9 | ||||
-rw-r--r-- | source3/libsmb/clikrb5.c | 34 | ||||
-rw-r--r-- | source3/libsmb/clireadwrite.c | 7 | ||||
-rw-r--r-- | source3/libsmb/nmblib.c | 2 | ||||
-rw-r--r-- | source3/libsmb/smbsock_connect.c | 575 |
6 files changed, 727 insertions, 29 deletions
diff --git a/source3/libsmb/async_smb.c b/source3/libsmb/async_smb.c index e8a0b13880..4d530282ce 100644 --- a/source3/libsmb/async_smb.c +++ b/source3/libsmb/async_smb.c @@ -705,6 +705,8 @@ static void cli_smb_received(struct tevent_req *subreq) DEBUG(10, ("cli_check_sign_mac failed\n")); TALLOC_FREE(inbuf); status = NT_STATUS_ACCESS_DENIED; + close(cli->fd); + cli->fd = -1; goto fail; } @@ -1069,3 +1071,130 @@ NTSTATUS cli_smb_oplock_break_waiter_recv(struct tevent_req *req, *plevel = state->level; return NT_STATUS_OK; } + + +struct cli_session_request_state { + struct tevent_context *ev; + int sock; + uint32 len_hdr; + struct iovec iov[3]; + uint8_t nb_session_response; +}; + +static void cli_session_request_sent(struct tevent_req *subreq); +static void cli_session_request_recvd(struct tevent_req *subreq); + +struct tevent_req *cli_session_request_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + int sock, + const struct nmb_name *called, + const struct nmb_name *calling) +{ + struct tevent_req *req, *subreq; + struct cli_session_request_state *state; + + req = tevent_req_create(mem_ctx, &state, + struct cli_session_request_state); + if (req == NULL) { + return NULL; + } + state->ev = ev; + state->sock = sock; + + state->iov[1].iov_base = name_mangle( + state, called->name, called->name_type); + if (tevent_req_nomem(state->iov[1].iov_base, req)) { + return tevent_req_post(req, ev); + } + state->iov[1].iov_len = name_len( + (unsigned char *)state->iov[1].iov_base, + talloc_get_size(state->iov[1].iov_base)); + + state->iov[2].iov_base = name_mangle( + state, calling->name, calling->name_type); + if (tevent_req_nomem(state->iov[2].iov_base, req)) { + return tevent_req_post(req, ev); + } + state->iov[2].iov_len = name_len( + (unsigned char *)state->iov[2].iov_base, + talloc_get_size(state->iov[2].iov_base)); + + _smb_setlen(((char *)&state->len_hdr), + state->iov[1].iov_len + state->iov[2].iov_len); + SCVAL((char *)&state->len_hdr, 0, 0x81); + + state->iov[0].iov_base = &state->len_hdr; + state->iov[0].iov_len = sizeof(state->len_hdr); + + subreq = writev_send(state, ev, NULL, sock, true, state->iov, 3); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, cli_session_request_sent, req); + return req; +} + +static void cli_session_request_sent(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct cli_session_request_state *state = tevent_req_data( + req, struct cli_session_request_state); + ssize_t ret; + int err; + + ret = writev_recv(subreq, &err); + TALLOC_FREE(subreq); + if (ret == -1) { + tevent_req_error(req, err); + return; + } + subreq = read_smb_send(state, state->ev, state->sock); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, cli_session_request_recvd, req); +} + +static void cli_session_request_recvd(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct cli_session_request_state *state = tevent_req_data( + req, struct cli_session_request_state); + uint8_t *buf; + ssize_t ret; + int err; + + ret = read_smb_recv(subreq, talloc_tos(), &buf, &err); + TALLOC_FREE(subreq); + + if (ret < 4) { + ret = -1; + err = EIO; + } + if (ret == -1) { + tevent_req_error(req, err); + return; + } + /* + * In case of an error there is more information in the data + * portion according to RFC1002. We're not subtle enough to + * respond to the different error conditions, so drop the + * error info here. + */ + state->nb_session_response = CVAL(buf, 0); + tevent_req_done(req); +} + +bool cli_session_request_recv(struct tevent_req *req, int *err, uint8_t *resp) +{ + struct cli_session_request_state *state = tevent_req_data( + req, struct cli_session_request_state); + + if (tevent_req_is_unix_error(req, err)) { + return false; + } + *resp = state->nb_session_response; + return true; +} diff --git a/source3/libsmb/clidgram.c b/source3/libsmb/clidgram.c index f5dbd72f22..05fb5d3294 100644 --- a/source3/libsmb/clidgram.c +++ b/source3/libsmb/clidgram.c @@ -238,11 +238,13 @@ bool receive_getdc_response(TALLOC_CTX *mem_ctx, if (blob.length < 4) { DEBUG(0,("invalid length: %d\n", (int)blob.length)); + free_packet(packet); return false; } if (RIVAL(blob.data,0) != DGRAM_SMB) { DEBUG(0,("invalid packet\n")); + free_packet(packet); return false; } @@ -253,11 +255,13 @@ bool receive_getdc_response(TALLOC_CTX *mem_ctx, (ndr_pull_flags_fn_t)ndr_pull_dgram_smb_packet); if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) { DEBUG(0,("failed to parse packet\n")); + free_packet(packet); return false; } if (p.smb.smb_command != SMB_TRANSACTION) { DEBUG(0,("invalid smb_command: %d\n", p.smb.smb_command)); + free_packet(packet); return false; } @@ -271,6 +275,7 @@ bool receive_getdc_response(TALLOC_CTX *mem_ctx, status = pull_netlogon_samlogon_response(&blob, mem_ctx, NULL, &r); if (!NT_STATUS_IS_OK(status)) { + free_packet(packet); return false; } @@ -285,11 +290,13 @@ bool receive_getdc_response(TALLOC_CTX *mem_ctx, if (!strequal(returned_domain, domain_name)) { DEBUG(3, ("GetDC: Expected domain %s, got %s\n", domain_name, returned_domain)); + free_packet(packet); return false; } *dc_name = talloc_strdup(mem_ctx, returned_dc); if (!*dc_name) { + free_packet(packet); return false; } @@ -300,6 +307,7 @@ bool receive_getdc_response(TALLOC_CTX *mem_ctx, *_r = (struct netlogon_samlogon_response *)talloc_memdup( mem_ctx, &r, sizeof(struct netlogon_samlogon_response)); if (!*_r) { + free_packet(packet); return false; } } @@ -307,5 +315,6 @@ bool receive_getdc_response(TALLOC_CTX *mem_ctx, DEBUG(10, ("GetDC gave name %s for domain %s\n", *dc_name, returned_domain)); + free_packet(packet); return True; } diff --git a/source3/libsmb/clikrb5.c b/source3/libsmb/clikrb5.c index b0dec0ad4c..7b5cd09433 100644 --- a/source3/libsmb/clikrb5.c +++ b/source3/libsmb/clikrb5.c @@ -696,26 +696,16 @@ static krb5_error_code create_gss_checksum(krb5_data *in_data, /* [inout] */ memset(gss_cksum, '\0', base_cksum_size + orig_length); SIVAL(gss_cksum, 0, GSSAPI_BNDLENGTH); - /* Precalculated MD5sum of NULL channel bindings (20 bytes) */ - /* Channel bindings are: (all ints encoded as little endian) - - [4 bytes] initiator_addrtype (255 for null bindings) - [4 bytes] initiator_address length - [n bytes] .. initiator_address data - not present - in null bindings. - [4 bytes] acceptor_addrtype (255 for null bindings) - [4 bytes] acceptor_address length - [n bytes] .. acceptor_address data - not present - in null bindings. - [4 bytes] application_data length - [n bytes] .. application_ data - not present - in null bindings. - MD5 of this is ""\x14\x8f\x0c\xf7\xb1u\xdey*J\x9a%\xdfV\xc5\x18" - */ - - memcpy(&gss_cksum[4], - "\x14\x8f\x0c\xf7\xb1u\xdey*J\x9a%\xdfV\xc5\x18", - GSSAPI_BNDLENGTH); + /* + * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes. + * This matches the behavior of heimdal and mit. + * + * And it is needed to work against some closed source + * SMB servers. + * + * See bug #7883 + */ + memset(&gss_cksum[4], 0x00, GSSAPI_BNDLENGTH); SIVAL(gss_cksum, 20, gss_flags); @@ -832,7 +822,7 @@ static krb5_error_code ads_krb5_mk_req(krb5_context context, goto cleanup_creds; } -#if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) +#if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) if( credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE ) { /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket as part of the kerberos exchange. */ @@ -894,7 +884,6 @@ static krb5_error_code ads_krb5_mk_req(krb5_context context, gss_flags |= GSS_C_DELEG_FLAG; } } -#endif /* Frees and reallocates in_data into a GSS checksum blob. */ retval = create_gss_checksum(&in_data, gss_flags); @@ -902,7 +891,6 @@ static krb5_error_code ads_krb5_mk_req(krb5_context context, goto cleanup_data; } -#if defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) /* We always want GSS-checksum types. */ retval = krb5_auth_con_set_req_cksumtype(context, *auth_context, GSSAPI_CHECKSUM ); if (retval) { diff --git a/source3/libsmb/clireadwrite.c b/source3/libsmb/clireadwrite.c index 6342de484c..1f5f9252d4 100644 --- a/source3/libsmb/clireadwrite.c +++ b/source3/libsmb/clireadwrite.c @@ -704,8 +704,7 @@ static bool cli_issue_write(struct cli_state *cli, off_t offset, uint16 mode, const char *buf, - size_t size, - int i) + size_t size) { char *p; bool large_writex = false; @@ -779,8 +778,6 @@ static bool cli_issue_write(struct cli_state *cli, cli_setup_bcc(cli, p+size); } - SSVAL(cli->outbuf,smb_mid,cli->mid + i); - show_msg(cli->outbuf); if (direct_writes) { /* For direct writes we now need to write the data @@ -829,7 +826,7 @@ ssize_t cli_write(struct cli_state *cli, if (!cli_issue_write(cli, fnum, offset + bsent, write_mode, buf + bsent, - size1, issued)) + size1)) return -1; issued++; } diff --git a/source3/libsmb/nmblib.c b/source3/libsmb/nmblib.c index c82d4dd916..c47f827ad2 100644 --- a/source3/libsmb/nmblib.c +++ b/source3/libsmb/nmblib.c @@ -1293,7 +1293,7 @@ static int name_interpret(unsigned char *buf, size_t buf_len, Note: <Out> must be (33 + strlen(scope) + 2) bytes long, at minimum. ****************************************************************************/ -char *name_mangle(TALLOC_CTX *mem_ctx, char *In, char name_type) +char *name_mangle(TALLOC_CTX *mem_ctx, const char *In, char name_type) { int i; int len; diff --git a/source3/libsmb/smbsock_connect.c b/source3/libsmb/smbsock_connect.c new file mode 100644 index 0000000000..b6c1c3c058 --- /dev/null +++ b/source3/libsmb/smbsock_connect.c @@ -0,0 +1,575 @@ +/* + Unix SMB/CIFS implementation. + Connect to 445 and 139/nbsesssetup + Copyright (C) Volker Lendecke 2010 + + 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 3 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, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" +#include "../lib/async_req/async_sock.h" +#include "async_smb.h" + +struct nb_connect_state { + struct tevent_context *ev; + const struct sockaddr_storage *addr; + const char *called_name; + int sock; + + struct nmb_name called; + struct nmb_name calling; +}; + +static int nb_connect_state_destructor(struct nb_connect_state *state); +static void nb_connect_connected(struct tevent_req *subreq); +static void nb_connect_done(struct tevent_req *subreq); + +static struct tevent_req *nb_connect_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + const struct sockaddr_storage *addr, + const char *called_name, + int called_type, + const char *calling_name, + int calling_type) +{ + struct tevent_req *req, *subreq; + struct nb_connect_state *state; + + req = tevent_req_create(mem_ctx, &state, struct nb_connect_state); + if (req == NULL) { + return NULL; + } + state->ev = ev; + state->called_name = called_name; + state->addr = addr; + + state->sock = -1; + make_nmb_name(&state->called, called_name, called_type); + make_nmb_name(&state->calling, calling_name, calling_type); + + talloc_set_destructor(state, nb_connect_state_destructor); + + subreq = open_socket_out_send(state, ev, addr, 139, 5000); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, nb_connect_connected, req); + return req; +} + +static int nb_connect_state_destructor(struct nb_connect_state *state) +{ + if (state->sock != -1) { + close(state->sock); + } + return 0; +} + +static void nb_connect_connected(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct nb_connect_state *state = tevent_req_data( + req, struct nb_connect_state); + NTSTATUS status; + + status = open_socket_out_recv(subreq, &state->sock); + TALLOC_FREE(subreq); + if (!NT_STATUS_IS_OK(status)) { + tevent_req_nterror(req, status); + return; + } + subreq = cli_session_request_send(state, state->ev, state->sock, + &state->called, &state->calling); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, nb_connect_done, req); +} + +static void nb_connect_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct nb_connect_state *state = tevent_req_data( + req, struct nb_connect_state); + bool ret; + int err; + uint8_t resp; + + ret = cli_session_request_recv(subreq, &err, &resp); + TALLOC_FREE(subreq); + if (!ret) { + tevent_req_nterror(req, map_nt_error_from_unix(err)); + return; + } + + /* + * RFC1002: 0x82 - POSITIVE SESSION RESPONSE + */ + + if (resp != 0x82) { + /* + * The server did not like our session request + */ + close(state->sock); + state->sock = -1; + + if (strequal(state->called_name, "*SMBSERVER")) { + /* + * Here we could try a name status request and + * use the first 0x20 type name. + */ + tevent_req_nterror( + req, NT_STATUS_RESOURCE_NAME_NOT_FOUND); + return; + } + + /* + * We could be subtle and distinguish between + * different failure modes, but what we do here + * instead is just retry with *SMBSERVER type 0x20. + */ + state->called_name = "*SMBSERVER"; + make_nmb_name(&state->called, state->called_name, 0x20); + + subreq = open_socket_out_send(state, state->ev, state->addr, + 139, 5000); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, nb_connect_connected, req); + return; + } + + tevent_req_done(req); + return; + +} + +static NTSTATUS nb_connect_recv(struct tevent_req *req, int *sock) +{ + struct nb_connect_state *state = tevent_req_data( + req, struct nb_connect_state); + NTSTATUS status; + + if (tevent_req_is_nterror(req, &status)) { + return status; + } + *sock = state->sock; + state->sock = -1; + return NT_STATUS_OK; +} + +struct smbsock_connect_state { + struct tevent_context *ev; + const struct sockaddr_storage *addr; + const char *called_name; + const char *calling_name; + struct tevent_req *req_139; + struct tevent_req *req_445; + int sock; + uint16_t port; +}; + +static int smbsock_connect_state_destructor( + struct smbsock_connect_state *state); +static void smbsock_connect_connected(struct tevent_req *subreq); +static void smbsock_connect_do_139(struct tevent_req *subreq); + +struct tevent_req *smbsock_connect_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + const struct sockaddr_storage *addr, + const char *called_name, + const char *calling_name) +{ + struct tevent_req *req, *subreq; + struct smbsock_connect_state *state; + + req = tevent_req_create(mem_ctx, &state, struct smbsock_connect_state); + if (req == NULL) { + return NULL; + } + state->ev = ev; + state->addr = addr; + state->sock = -1; + state->called_name = + (called_name != NULL) ? called_name : "*SMBSERVER"; + state->calling_name = + (calling_name != NULL) ? calling_name : global_myname(); + + talloc_set_destructor(state, smbsock_connect_state_destructor); + + state->req_445 = open_socket_out_send(state, ev, addr, 445, 5000); + if (tevent_req_nomem(state->req_445, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(state->req_445, smbsock_connect_connected, + req); + + /* + * After 5 msecs, fire the 139 request + */ + state->req_139 = tevent_wakeup_send( + state, ev, timeval_current_ofs(0, 5000)); + if (tevent_req_nomem(state->req_139, req)) { + TALLOC_FREE(state->req_445); + return tevent_req_post(req, ev); + } + tevent_req_set_callback(state->req_139, smbsock_connect_do_139, + req); + return req; +} + +static int smbsock_connect_state_destructor( + struct smbsock_connect_state *state) +{ + if (state->sock != -1) { + close(state->sock); + } + return 0; +} + +static void smbsock_connect_do_139(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct smbsock_connect_state *state = tevent_req_data( + req, struct smbsock_connect_state); + bool ret; + + ret = tevent_wakeup_recv(subreq); + TALLOC_FREE(subreq); + if (!ret) { + tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR); + return; + } + state->req_139 = nb_connect_send(state, state->ev, state->addr, + state->called_name, 0x20, + state->calling_name, 0x0); + if (tevent_req_nomem(state->req_139, req)) { + return; + } + tevent_req_set_callback(state->req_139, smbsock_connect_connected, + req); +} + +static void smbsock_connect_connected(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct smbsock_connect_state *state = tevent_req_data( + req, struct smbsock_connect_state); + struct tevent_req *unfinished_req; + NTSTATUS status; + + if (subreq == state->req_445) { + + status = open_socket_out_recv(subreq, &state->sock); + TALLOC_FREE(state->req_445); + unfinished_req = state->req_139; + state->port = 445; + + } else if (subreq == state->req_139) { + + status = nb_connect_recv(subreq, &state->sock); + TALLOC_FREE(state->req_139); + unfinished_req = state->req_445; + state->port = 139; + + } else { + tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR); + return; + } + + if (NT_STATUS_IS_OK(status)) { + TALLOC_FREE(unfinished_req); + state->req_139 = NULL; + state->req_445 = NULL; + tevent_req_done(req); + return; + } + if (unfinished_req == NULL) { + /* + * Both requests failed + */ + tevent_req_nterror(req, status); + return; + } + /* + * Do nothing, wait for the second request to come here. + */ +} + +NTSTATUS smbsock_connect_recv(struct tevent_req *req, int *sock, + uint16_t *port) +{ + struct smbsock_connect_state *state = tevent_req_data( + req, struct smbsock_connect_state); + NTSTATUS status; + + if (tevent_req_is_nterror(req, &status)) { + return status; + } + *sock = state->sock; + state->sock = -1; + if (port != NULL) { + *port = state->port; + } + return NT_STATUS_OK; +} + +NTSTATUS smbsock_connect(const struct sockaddr_storage *addr, + const char *called_name, const char *calling_name, + int *pfd, uint16_t *port) +{ + TALLOC_CTX *frame = talloc_stackframe(); + struct event_context *ev; + struct tevent_req *req; + NTSTATUS status = NT_STATUS_NO_MEMORY; + + ev = event_context_init(frame); + if (ev == NULL) { + goto fail; + } + req = smbsock_connect_send(frame, ev, addr, called_name, calling_name); + if (req == NULL) { + goto fail; + } + if (!tevent_req_poll_ntstatus(req, ev, &status)) { + goto fail; + } + status = smbsock_connect_recv(req, pfd, port); + fail: + TALLOC_FREE(frame); + return status; +} + +struct smbsock_any_connect_state { + struct tevent_context *ev; + const struct sockaddr_storage *addrs; + const char **called_names; + size_t num_addrs; + + struct tevent_req **requests; + size_t num_sent; + size_t num_received; + + int fd; + uint16_t port; + size_t chosen_index; +}; + +static bool smbsock_any_connect_send_next( + struct tevent_req *req, struct smbsock_any_connect_state *state); +static void smbsock_any_connect_trynext(struct tevent_req *subreq); +static void smbsock_any_connect_connected(struct tevent_req *subreq); + +struct tevent_req *smbsock_any_connect_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + const struct sockaddr_storage *addrs, + const char **called_names, + size_t num_addrs) +{ + struct tevent_req *req, *subreq; + struct smbsock_any_connect_state *state; + + req = tevent_req_create(mem_ctx, &state, + struct smbsock_any_connect_state); + if (req == NULL) { + return NULL; + } + state->ev = ev; + state->addrs = addrs; + state->num_addrs = num_addrs; + state->called_names = called_names; + + if (num_addrs == 0) { + tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER); + return tevent_req_post(req, ev); + } + + state->requests = talloc_zero_array(state, struct tevent_req *, + num_addrs); + if (tevent_req_nomem(state->requests, req)) { + return tevent_req_post(req, ev); + } + if (!smbsock_any_connect_send_next(req, state)) { + return tevent_req_post(req, ev); + } + if (state->num_sent >= state->num_addrs) { + return req; + } + subreq = tevent_wakeup_send(state, ev, timeval_current_ofs(0, 10000)); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, smbsock_any_connect_trynext, req); + return req; +} + +static void smbsock_any_connect_trynext(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct smbsock_any_connect_state *state = tevent_req_data( + req, struct smbsock_any_connect_state); + bool ret; + + ret = tevent_wakeup_recv(subreq); + TALLOC_FREE(subreq); + if (!ret) { + tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR); + return; + } + if (!smbsock_any_connect_send_next(req, state)) { + return; + } + if (state->num_sent >= state->num_addrs) { + return; + } + subreq = tevent_wakeup_send(state, state->ev, + tevent_timeval_set(0, 10000)); + if (tevent_req_nomem(subreq, req)) { + return; + } + tevent_req_set_callback(subreq, smbsock_any_connect_trynext, req); +} + +static bool smbsock_any_connect_send_next( + struct tevent_req *req, struct smbsock_any_connect_state *state) +{ + struct tevent_req *subreq; + + if (state->num_sent >= state->num_addrs) { + tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR); + return false; + } + subreq = smbsock_connect_send( + state->requests, state->ev, &state->addrs[state->num_sent], + (state->called_names != NULL) + ? state->called_names[state->num_sent] : NULL, + NULL); + if (tevent_req_nomem(subreq, req)) { + return false; + } + tevent_req_set_callback(subreq, smbsock_any_connect_connected, req); + + state->requests[state->num_sent] = subreq; + state->num_sent += 1; + + return true; +} + +static void smbsock_any_connect_connected(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct smbsock_any_connect_state *state = tevent_req_data( + req, struct smbsock_any_connect_state); + NTSTATUS status; + int fd; + uint16_t port; + size_t i; + size_t chosen_index = 0; + + for (i=0; i<state->num_sent; i++) { + if (state->requests[i] == subreq) { + chosen_index = i; + break; + } + } + if (i == state->num_sent) { + tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR); + return; + } + + status = smbsock_connect_recv(subreq, &fd, &port); + + TALLOC_FREE(subreq); + state->requests[chosen_index] = NULL; + + if (NT_STATUS_IS_OK(status)) { + /* + * This will kill all the other requests + */ + TALLOC_FREE(state->requests); + state->fd = fd; + state->port = port; + state->chosen_index = chosen_index; + tevent_req_done(req); + return; + } + + state->num_received += 1; + if (state->num_received <= state->num_addrs) { + /* + * More addrs pending, wait for the others + */ + return; + } + + /* + * This is the last response, none succeeded. + */ + tevent_req_nterror(req, status); + return; +} + +NTSTATUS smbsock_any_connect_recv(struct tevent_req *req, int *pfd, + size_t *chosen_index, uint16_t *port) +{ + struct smbsock_any_connect_state *state = tevent_req_data( + req, struct smbsock_any_connect_state); + NTSTATUS status; + + if (tevent_req_is_nterror(req, &status)) { + return status; + } + *pfd = state->fd; + if (chosen_index != NULL) { + *chosen_index = state->chosen_index; + } + if (port != NULL) { + *port = state->port; + } + return NT_STATUS_OK; +} + +NTSTATUS smbsock_any_connect(const struct sockaddr_storage *addrs, + const char **called_names, size_t num_addrs, + int *pfd, size_t *chosen_index, uint16_t *port) +{ + TALLOC_CTX *frame = talloc_stackframe(); + struct event_context *ev; + struct tevent_req *req; + NTSTATUS status = NT_STATUS_NO_MEMORY; + + ev = event_context_init(frame); + if (ev == NULL) { + goto fail; + } + req = smbsock_any_connect_send(frame, ev, addrs, called_names, + num_addrs); + if (req == NULL) { + goto fail; + } + if (!tevent_req_poll_ntstatus(req, ev, &status)) { + goto fail; + } + status = smbsock_any_connect_recv(req, pfd, chosen_index, port); + fail: + TALLOC_FREE(frame); + return status; +} |