The branch, v3-4-test has been updated
       via  1a929d8 s3: Use smbsock_any_connect in winbind
       via  ee2534c s3: Retry *SMBSERVER in nb_connect
       via  198b6d6 s3: Add smbsock_any_connect
       via  35bbc22 s3: Add an async smbsock_connect
       via  9b79de1 v3-4-test: Pull in tevent_req_poll_ntstatus from master
       via  e2296e2 s3: Add async cli_session_request
       via  02c4649 v3-4-test: Pull in read_smb_send from master
       via  56c760a s3: Add some const to name_mangle()
      from  8c2493f s3: Make winbind recover from a signing error

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=v3-4-test


- Log -----------------------------------------------------------------
commit 1a929d8f4c6bae302e5cc53d81117a05eae5fede
Author: Volker Lendecke <v...@samba.org>
Date:   Tue Dec 21 21:55:01 2010 +0100

    s3: Use smbsock_any_connect in winbind
    
    The last 8 patches address bug #7881 (winbind flaky against w2k8).

commit ee2534c18b5afa609ff17d9da7ea10bcf7654fc6
Author: Volker Lendecke <v...@samba.org>
Date:   Tue Dec 21 18:52:53 2010 +0100

    s3: Retry *SMBSERVER in nb_connect

commit 198b6d673787ee40c0ac389ece99cee1844dd585
Author: Volker Lendecke <v...@samba.org>
Date:   Mon Dec 13 17:17:51 2010 +0100

    s3: Add smbsock_any_connect

commit 35bbc2231760badaf0debc9f8f39ebdf00cfe8ad
Author: Volker Lendecke <v...@samba.org>
Date:   Sun Dec 12 18:55:06 2010 +0100

    s3: Add an async smbsock_connect
    
    This connects to 445 and after 5 milliseconds also to 139. It treats a 
netbios
    session setup failure as equivalent as a TCP connect failure. So if 139 is
    faster but fails the nb session setup, the 445 still has the chance to 
succeed.

commit 9b79de1553cfa57d24d1ac0316b814933ef3d33d
Author: Volker Lendecke <v...@samba.org>
Date:   Wed Dec 22 15:21:27 2010 +0100

    v3-4-test: Pull in tevent_req_poll_ntstatus from master

commit e2296e23a8546e249d1b26f4da6277792923bef4
Author: Volker Lendecke <v...@samba.org>
Date:   Sun Dec 12 18:54:31 2010 +0100

    s3: Add async cli_session_request
    
    This does not do the redirects, but I think that might be obsolete anyway

commit 02c4649674d3bd0f54e71910f11d6aff2cdb6c9d
Author: Volker Lendecke <v...@samba.org>
Date:   Wed Dec 22 15:15:47 2010 +0100

    v3-4-test: Pull in read_smb_send from master

commit 56c760ab41b9b4cb9680d873b8f9955be21434f4
Author: Volker Lendecke <v...@samba.org>
Date:   Sun Dec 12 18:53:49 2010 +0100

    s3: Add some const to name_mangle()

-----------------------------------------------------------------------

Summary of changes:
 source3/Makefile.in              |    1 +
 source3/include/async_smb.h      |    7 +
 source3/include/proto.h          |   27 ++-
 source3/lib/util.c               |   11 +
 source3/libsmb/async_smb.c       |  212 ++++++++++++++
 source3/libsmb/nmblib.c          |    2 +-
 source3/libsmb/smbsock_connect.c |  573 ++++++++++++++++++++++++++++++++++++++
 source3/winbindd/winbindd_cm.c   |   58 +---
 8 files changed, 844 insertions(+), 47 deletions(-)
 create mode 100644 source3/libsmb/smbsock_connect.c


Changeset truncated at 500 lines:

diff --git a/source3/Makefile.in b/source3/Makefile.in
index b863e36..82e108a 100644
--- a/source3/Makefile.in
+++ b/source3/Makefile.in
@@ -473,6 +473,7 @@ LIBSMB_OBJ = libsmb/clientgen.o libsmb/cliconnect.o 
libsmb/clifile.o \
              libsmb/credentials.o \
             libsmb/clioplock.o libsmb/clirap2.o \
             libsmb/smb_seal.o libsmb/async_smb.o \
+            libsmb/smbsock_connect.o \
             $(LIBSAMBA_OBJ) \
             $(LIBNMB_OBJ) \
             $(LIBNBT_OBJ) \
diff --git a/source3/include/async_smb.h b/source3/include/async_smb.h
index 7fc4ff7..06e6959 100644
--- a/source3/include/async_smb.h
+++ b/source3/include/async_smb.h
@@ -149,4 +149,11 @@ NTSTATUS cli_pull_error(char *buf);
 
 void cli_set_error(struct cli_state *cli, NTSTATUS status);
 
+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);
+bool cli_session_request_recv(struct tevent_req *req, int *err, uint8_t *resp);
+
 #endif
diff --git a/source3/include/proto.h b/source3/include/proto.h
index bd9665c..f87d91a 100644
--- a/source3/include/proto.h
+++ b/source3/include/proto.h
@@ -1221,6 +1221,9 @@ bool is_valid_policy_hnd(const struct policy_handle *hnd);
 bool policy_hnd_equal(const struct policy_handle *hnd1,
                      const struct policy_handle *hnd2);
 const char *strip_hostname(const char *s);
+bool tevent_req_poll_ntstatus(struct tevent_req *req,
+                             struct tevent_context *ev,
+                             NTSTATUS *status);
 
 /* The following definitions come from lib/util_file.c  */
 
@@ -3074,7 +3077,7 @@ struct packet_struct *receive_dgram_packet(int fd, int t,
 bool match_mailslot_name(struct packet_struct *p, const char *mailslot_name);
 int matching_len_bits(unsigned char *p1, unsigned char *p2, size_t len);
 void sort_query_replies(char *data, int n, struct in_addr ip);
-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 name_extract(char *buf,int ofs, fstring name);
 int name_len(char *s1);
 
@@ -7279,6 +7282,28 @@ void *avahi_start_register(TALLOC_CTX *mem_ctx, struct 
tevent_context *ev,
 
 /* Misc protos */
 
+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);
+NTSTATUS smbsock_connect_recv(struct tevent_req *req, int *sock,
+                             uint16_t *port);
+NTSTATUS smbsock_connect(const struct sockaddr_storage *addr,
+                        const char *called_name, const char *calling_name,
+                        int *pfd, uint16_t *port);
+
+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);
+NTSTATUS smbsock_any_connect_recv(struct tevent_req *req, int *pfd,
+                                 size_t *chosen_index, uint16_t *port);
+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);
+
 /* The following definitions come from rpc_server/srv_samr_nt.c */
 NTSTATUS access_check_object( SEC_DESC *psd, NT_USER_TOKEN *token,
                                SE_PRIV *rights, uint32 rights_mask,
diff --git a/source3/lib/util.c b/source3/lib/util.c
index 3d7336f..c9afe7b 100644
--- a/source3/lib/util.c
+++ b/source3/lib/util.c
@@ -3188,3 +3188,14 @@ const char *strip_hostname(const char *s)
 
        return s;
 }
+
+bool tevent_req_poll_ntstatus(struct tevent_req *req,
+                             struct tevent_context *ev,
+                             NTSTATUS *status)
+{
+       bool ret = tevent_req_poll(req, ev);
+       if (!ret) {
+               *status = map_nt_error_from_unix(errno);
+       }
+       return ret;
+}
diff --git a/source3/libsmb/async_smb.c b/source3/libsmb/async_smb.c
index 0336ff2..62a8c81 100644
--- a/source3/libsmb/async_smb.c
+++ b/source3/libsmb/async_smb.c
@@ -22,6 +22,93 @@
 static void cli_state_handler(struct event_context *event_ctx,
                              struct fd_event *event, uint16 flags, void *p);
 
+/*
+ * Read an smb packet asynchronously, discard keepalives
+ */
+
+struct read_smb_state {
+       struct tevent_context *ev;
+       int fd;
+       uint8_t *buf;
+};
+
+static ssize_t read_smb_more(uint8_t *buf, size_t buflen, void *private_data);
+static void read_smb_done(struct tevent_req *subreq);
+
+static struct tevent_req *read_smb_send(TALLOC_CTX *mem_ctx,
+                                       struct tevent_context *ev,
+                                       int fd)
+{
+       struct tevent_req *result, *subreq;
+       struct read_smb_state *state;
+
+       result = tevent_req_create(mem_ctx, &state, struct read_smb_state);
+       if (result == NULL) {
+               return NULL;
+       }
+       state->ev = ev;
+       state->fd = fd;
+
+       subreq = read_packet_send(state, ev, fd, 4, read_smb_more, NULL);
+       if (subreq == NULL) {
+               goto fail;
+       }
+       tevent_req_set_callback(subreq, read_smb_done, result);
+       return result;
+ fail:
+       TALLOC_FREE(result);
+       return NULL;
+}
+
+static ssize_t read_smb_more(uint8_t *buf, size_t buflen, void *private_data)
+{
+       if (buflen > 4) {
+               return 0;       /* We've been here, we're done */
+       }
+       return smb_len_large(buf);
+}
+
+static void read_smb_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct read_smb_state *state = tevent_req_data(
+               req, struct read_smb_state);
+       ssize_t len;
+       int err;
+
+       len = read_packet_recv(subreq, state, &state->buf, &err);
+       TALLOC_FREE(subreq);
+       if (len == -1) {
+               tevent_req_error(req, err);
+               return;
+       }
+
+       if (CVAL(state->buf, 0) == SMBkeepalive) {
+               subreq = read_packet_send(state, state->ev, state->fd, 4,
+                                         read_smb_more, NULL);
+               if (tevent_req_nomem(subreq, req)) {
+                       return;
+               }
+               tevent_req_set_callback(subreq, read_smb_done, req);
+               return;
+       }
+       tevent_req_done(req);
+}
+
+static ssize_t read_smb_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+                            uint8_t **pbuf, int *perrno)
+{
+       struct read_smb_state *state = tevent_req_data(
+               req, struct read_smb_state);
+
+       if (tevent_req_is_unix_error(req, perrno)) {
+               return -1;
+       }
+       *pbuf = talloc_move(mem_ctx, &state->buf);
+       return talloc_get_size(*pbuf);
+}
+
 /**
  * Fetch an error out of a NBT packet
  * @param[in] buf      The SMB packet
@@ -1135,3 +1222,128 @@ static void cli_state_handler(struct event_context 
*event_ctx,
                cli->fd = -1;
        }
 }
+
+
+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(
+               (char *)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(
+               (char *)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, 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/nmblib.c b/source3/libsmb/nmblib.c
index 5f3eda4..1206d9d 100644
--- a/source3/libsmb/nmblib.c
+++ b/source3/libsmb/nmblib.c
@@ -1279,7 +1279,7 @@ static int name_interpret(char *in, fstring name)
  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 0000000..8ab12c5
--- /dev/null
+++ b/source3/libsmb/smbsock_connect.c
@@ -0,0 +1,573 @@
+/*
+   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;
+       }
+


-- 
Samba Shared Repository

Reply via email to