s3:smbd: pass down smbd_server_connection via smbd_echo_state
[kai/samba.git] / source3 / smbd / process.c
index 5fd3e93325461d058bc33e5e683e73f51d6fe9fd..0e15e32d35958adb805d05db874f7f02b9a88988 100644 (file)
@@ -27,7 +27,7 @@
 #include "../lib/async_req/async_sock.h"
 #include "ctdbd_conn.h"
 #include "../lib/util/select.h"
-#include "printing/pcap.h"
+#include "printing/queue_process.h"
 #include "system/select.h"
 #include "passdb.h"
 #include "auth.h"
 
 extern bool global_machine_password_needs_changing;
 
+/* Internal message queue for deferred opens. */
+struct pending_message_list {
+       struct pending_message_list *next, *prev;
+       struct timeval request_time; /* When was this first issued? */
+       struct smbd_server_connection *sconn;
+       struct timed_event *te;
+       struct smb_perfcount_data pcd;
+       uint32_t seqnum;
+       bool encrypted;
+       bool processed;
+       DATA_BLOB buf;
+       DATA_BLOB private_data;
+};
+
 static void construct_reply_common(struct smb_request *req, const char *inbuf,
                                   char *outbuf);
-static struct pending_message_list *get_deferred_open_message_smb(uint64_t mid);
+static struct pending_message_list *get_deferred_open_message_smb(
+       struct smbd_server_connection *sconn, uint64_t mid);
 
 static bool smbd_lock_socket_internal(struct smbd_server_connection *sconn)
 {
@@ -143,7 +158,7 @@ bool srv_send_smb(struct smbd_server_connection *sconn, char *buffer,
        }
 
        if (do_encrypt) {
-               NTSTATUS status = srv_encrypt_buffer(buffer, &buf_out);
+               NTSTATUS status = srv_encrypt_buffer(sconn, buffer, &buf_out);
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(0, ("send_smb: SMB encryption failed "
                                "on outgoing packet! Error %s\n",
@@ -167,12 +182,12 @@ bool srv_send_smb(struct smbd_server_connection *sconn, char *buffer,
                         get_peer_addr(sconn->sock, addr, sizeof(addr)),
                         (int)ret, strerror(errno) ));
 
-               srv_free_enc_buffer(buf_out);
+               srv_free_enc_buffer(sconn, buf_out);
                goto out;
        }
 
        SMB_PERFCOUNT_SET_MSGLEN_OUT(pcd, len);
-       srv_free_enc_buffer(buf_out);
+       srv_free_enc_buffer(sconn, buf_out);
 out:
        SMB_PERFCOUNT_END(pcd);
 
@@ -198,9 +213,10 @@ int srv_set_message(char *buf,
        return (smb_size + num_words*2 + num_bytes);
 }
 
-static bool valid_smb_header(const uint8_t *inbuf)
+static bool valid_smb_header(struct smbd_server_connection *sconn,
+                            const uint8_t *inbuf)
 {
-       if (is_encrypted_packet(inbuf)) {
+       if (is_encrypted_packet(sconn, inbuf)) {
                return true;
        }
        /*
@@ -454,16 +470,17 @@ static NTSTATUS receive_smb_talloc(TALLOC_CTX *mem_ctx,
        status = receive_smb_raw_talloc(mem_ctx, sconn, sock, buffer, timeout,
                                        p_unread, &len);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("read_smb_length_return_keepalive failed for "
-                         "client %s read error = %s.\n",
-                         tsocket_address_string(sconn->remote_address,
-                                                talloc_tos()),
-                         nt_errstr(status)));
+               DEBUG(NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)?5:1,
+                     ("receive_smb_raw_talloc failed for client %s "
+                      "read error = %s.\n",
+                      tsocket_address_string(sconn->remote_address,
+                                             talloc_tos()),
+                      nt_errstr(status)) );
                return status;
        }
 
-       if (is_encrypted_packet((uint8_t *)*buffer)) {
-               status = srv_decrypt_buffer(*buffer);
+       if (is_encrypted_packet(sconn, (uint8_t *)*buffer)) {
+               status = srv_decrypt_buffer(sconn, *buffer);
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(0, ("receive_smb_talloc: SMB decryption failed on "
                                "incoming packet! Error %s\n",
@@ -555,6 +572,7 @@ static void smbd_deferred_open_timer(struct event_context *ev,
 {
        struct pending_message_list *msg = talloc_get_type(private_data,
                                           struct pending_message_list);
+       struct smbd_server_connection *sconn = msg->sconn;
        TALLOC_CTX *mem_ctx = talloc_tos();
        uint64_t mid = (uint64_t)SVAL(msg->buf.data,smb_mid);
        uint8_t *inbuf;
@@ -575,14 +593,14 @@ static void smbd_deferred_open_timer(struct event_context *ev,
         * re-processed in error. */
        msg->processed = true;
 
-       process_smb(smbd_server_conn, inbuf,
+       process_smb(sconn, inbuf,
                    msg->buf.length, 0,
                    msg->seqnum, msg->encrypted, &msg->pcd);
 
        /* If it's still there and was processed, remove it. */
-       msg = get_deferred_open_message_smb(mid);
+       msg = get_deferred_open_message_smb(sconn, mid);
        if (msg && msg->processed) {
-               remove_deferred_open_message_smb(mid);
+               remove_deferred_open_message_smb(sconn, mid);
        }
 }
 
@@ -605,6 +623,7 @@ static bool push_queued_message(struct smb_request *req,
                DEBUG(0,("push_message: malloc fail (1)\n"));
                return False;
        }
+       msg->sconn = req->sconn;
 
        msg->buf = data_blob_talloc(msg, req->inbuf, msg_len);
        if(msg->buf.data == NULL) {
@@ -640,7 +659,8 @@ static bool push_queued_message(struct smb_request *req,
                return false;
        }
 
-       DLIST_ADD_END(deferred_open_queue, msg, struct pending_message_list *);
+       DLIST_ADD_END(req->sconn->deferred_open_queue, msg,
+                     struct pending_message_list *);
 
        DEBUG(10,("push_message: pushed message length %u on "
                  "deferred_open_queue\n", (unsigned int)msg_len));
@@ -652,22 +672,23 @@ static bool push_queued_message(struct smb_request *req,
  Function to delete a sharing violation open message by mid.
 ****************************************************************************/
 
-void remove_deferred_open_message_smb(uint64_t mid)
+void remove_deferred_open_message_smb(struct smbd_server_connection *sconn,
+                                     uint64_t mid)
 {
        struct pending_message_list *pml;
 
-       if (smbd_server_conn->using_smb2) {
-               remove_deferred_open_message_smb2(smbd_server_conn, mid);
+       if (sconn->using_smb2) {
+               remove_deferred_open_message_smb2(sconn, mid);
                return;
        }
 
-       for (pml = deferred_open_queue; pml; pml = pml->next) {
+       for (pml = sconn->deferred_open_queue; pml; pml = pml->next) {
                if (mid == (uint64_t)SVAL(pml->buf.data,smb_mid)) {
                        DEBUG(10,("remove_deferred_open_message_smb: "
                                  "deleting mid %llu len %u\n",
                                  (unsigned long long)mid,
                                  (unsigned int)pml->buf.length ));
-                       DLIST_REMOVE(deferred_open_queue, pml);
+                       DLIST_REMOVE(sconn->deferred_open_queue, pml);
                        TALLOC_FREE(pml);
                        return;
                }
@@ -679,17 +700,18 @@ void remove_deferred_open_message_smb(uint64_t mid)
  schedule it for immediate processing.
 ****************************************************************************/
 
-void schedule_deferred_open_message_smb(uint64_t mid)
+void schedule_deferred_open_message_smb(struct smbd_server_connection *sconn,
+                                       uint64_t mid)
 {
        struct pending_message_list *pml;
        int i = 0;
 
-       if (smbd_server_conn->using_smb2) {
-               schedule_deferred_open_message_smb2(smbd_server_conn, mid);
+       if (sconn->using_smb2) {
+               schedule_deferred_open_message_smb2(sconn, mid);
                return;
        }
 
-       for (pml = deferred_open_queue; pml; pml = pml->next) {
+       for (pml = sconn->deferred_open_queue; pml; pml = pml->next) {
                uint64_t msg_mid = (uint64_t)SVAL(pml->buf.data,smb_mid);
 
                DEBUG(10,("schedule_deferred_open_message_smb: [%d] "
@@ -727,7 +749,7 @@ void schedule_deferred_open_message_smb(uint64_t mid)
 
                        TALLOC_FREE(pml->te);
                        pml->te = te;
-                       DLIST_PROMOTE(deferred_open_queue, pml);
+                       DLIST_PROMOTE(sconn->deferred_open_queue, pml);
                        return;
                }
        }
@@ -741,15 +763,15 @@ void schedule_deferred_open_message_smb(uint64_t mid)
  Return true if this mid is on the deferred queue and was not yet processed.
 ****************************************************************************/
 
-bool open_was_deferred(uint64_t mid)
+bool open_was_deferred(struct smbd_server_connection *sconn, uint64_t mid)
 {
        struct pending_message_list *pml;
 
-       if (smbd_server_conn->using_smb2) {
-               return open_was_deferred_smb2(smbd_server_conn, mid);
+       if (sconn->using_smb2) {
+               return open_was_deferred_smb2(sconn, mid);
        }
 
-       for (pml = deferred_open_queue; pml; pml = pml->next) {
+       for (pml = sconn->deferred_open_queue; pml; pml = pml->next) {
                if (((uint64_t)SVAL(pml->buf.data,smb_mid)) == mid && !pml->processed) {
                        return True;
                }
@@ -761,11 +783,12 @@ bool open_was_deferred(uint64_t mid)
  Return the message queued by this mid.
 ****************************************************************************/
 
-static struct pending_message_list *get_deferred_open_message_smb(uint64_t mid)
+static struct pending_message_list *get_deferred_open_message_smb(
+       struct smbd_server_connection *sconn, uint64_t mid)
 {
        struct pending_message_list *pml;
 
-       for (pml = deferred_open_queue; pml; pml = pml->next) {
+       for (pml = sconn->deferred_open_queue; pml; pml = pml->next) {
                if (((uint64_t)SVAL(pml->buf.data,smb_mid)) == mid) {
                        return pml;
                }
@@ -783,13 +806,13 @@ bool get_deferred_open_message_state(struct smb_request *smbreq,
 {
        struct pending_message_list *pml;
 
-       if (smbd_server_conn->using_smb2) {
+       if (smbreq->sconn->using_smb2) {
                return get_deferred_open_message_state_smb2(smbreq->smb2req,
                                        p_request_time,
                                        pp_state);
        }
 
-       pml = get_deferred_open_message_smb(smbreq->mid);
+       pml = get_deferred_open_message_smb(smbreq->sconn, smbreq->mid);
        if (!pml) {
                return false;
        }
@@ -882,7 +905,7 @@ static void smbd_sig_hup_handler(struct tevent_context *ev,
        DEBUG(1,("Reloading services after SIGHUP\n"));
        reload_services(msg_ctx, smbd_server_conn->sock, False);
        if (am_parent) {
-               pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
+               printing_subsystem_update(ev, msg_ctx, true);
        }
 }
 
@@ -898,7 +921,8 @@ void smbd_setup_sig_hup_handler(struct tevent_context *ev,
        }
 }
 
-static NTSTATUS smbd_server_connection_loop_once(struct smbd_server_connection *conn)
+static NTSTATUS smbd_server_connection_loop_once(struct tevent_context *ev_ctx,
+                                                struct smbd_server_connection *conn)
 {
        int timeout;
        int num_pfds = 0;
@@ -912,11 +936,10 @@ static NTSTATUS smbd_server_connection_loop_once(struct smbd_server_connection *
         * select for longer than it would take to wait for them.
         */
 
-       event_add_to_poll_args(server_event_context(), conn,
-                              &conn->pfds, &num_pfds, &timeout);
+       event_add_to_poll_args(ev_ctx, conn, &conn->pfds, &num_pfds, &timeout);
 
        /* Process a signal and timed events now... */
-       if (run_events_poll(server_event_context(), 0, NULL, 0)) {
+       if (run_events_poll(ev_ctx, 0, NULL, 0)) {
                return NT_STATUS_RETRY;
        }
 
@@ -938,8 +961,7 @@ static NTSTATUS smbd_server_connection_loop_once(struct smbd_server_connection *
                return map_nt_error_from_unix(errno);
        }
 
-       retry = run_events_poll(server_event_context(), ret, conn->pfds,
-                               num_pfds);
+       retry = run_events_poll(ev_ctx, ret, conn->pfds, num_pfds);
        if (retry) {
                return NT_STATUS_RETRY;
        }
@@ -1367,7 +1389,7 @@ static connection_struct *switch_message(uint8 type, struct smb_request *req, in
 
        /* Make sure this is an SMB packet. smb_size contains NetBIOS header
         * so subtract 4 from it. */
-       if (!valid_smb_header(req->inbuf)
+       if (!valid_smb_header(sconn, req->inbuf)
            || (size < (smb_size - 4))) {
                DEBUG(2,("Non-SMB packet of length %d. Terminating server\n",
                         smb_len(req->inbuf)));
@@ -1607,7 +1629,7 @@ static void process_smb(struct smbd_server_connection *sconn,
                if (smbd_is_smb2_header(inbuf, nread)) {
                        smbd_smb2_first_negprot(sconn, inbuf, nread);
                        return;
-               } else if (nread >= smb_size && valid_smb_header(inbuf)
+               } else if (nread >= smb_size && valid_smb_header(sconn, inbuf)
                                && CVAL(inbuf, smb_com) != 0x72) {
                        /* This is a non-negprot SMB1 packet.
                           Disable SMB2 from now on. */
@@ -1669,15 +1691,21 @@ void remove_from_common_flags2(uint32 v)
 static void construct_reply_common(struct smb_request *req, const char *inbuf,
                                   char *outbuf)
 {
+       uint16_t in_flags2 = SVAL(inbuf,smb_flg2);
+       uint16_t out_flags2 = common_flags2;
+
+       out_flags2 |= in_flags2 & FLAGS2_UNICODE_STRINGS;
+       out_flags2 |= in_flags2 & FLAGS2_SMB_SECURITY_SIGNATURES;
+       out_flags2 |= in_flags2 & FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED;
+
        srv_set_message(outbuf,0,0,false);
 
        SCVAL(outbuf, smb_com, req->cmd);
        SIVAL(outbuf,smb_rcls,0);
        SCVAL(outbuf,smb_flg, FLAG_REPLY | (CVAL(inbuf,smb_flg) & FLAG_CASELESS_PATHNAMES)); 
-       SSVAL(outbuf,smb_flg2,
-               (SVAL(inbuf,smb_flg2) & FLAGS2_UNICODE_STRINGS) |
-               common_flags2);
+       SSVAL(outbuf,smb_flg2, out_flags2);
        memset(outbuf+smb_pidhigh,'\0',(smb_tid-smb_pidhigh));
+       memcpy(outbuf+smb_ss_field, inbuf+smb_ss_field, 8);
 
        SSVAL(outbuf,smb_tid,SVAL(inbuf,smb_tid));
        SSVAL(outbuf,smb_pid,SVAL(inbuf,smb_pid));
@@ -2021,15 +2049,24 @@ void chain_reply(struct smb_request *req)
        SMB_PERFCOUNT_SET_MSGLEN_IN(&req->pcd, smblen);
 
        /*
-        * Check if the client tries to fool us. The request so far uses the
-        * space to the end of the byte buffer in the request just
-        * processed. The chain_offset can't point into that area. If that was
-        * the case, we could end up with an endless processing of the chain,
-        * we would always handle the same request.
+        * Check if the client tries to fool us. The chain offset
+        * needs to point beyond the current request in the chain, it
+        * needs to strictly grow. Otherwise we might be tricked into
+        * an endless loop always processing the same request over and
+        * over again. We used to assume that vwv and the byte buffer
+        * array in a chain are always attached, but OS/2 the
+        * Write&X/Read&X chain puts the Read&X vwv array right behind
+        * the Write&X vwv chain. The Write&X bcc array is put behind
+        * the Read&X vwv array. So now we check whether the chain
+        * offset points strictly behind the previous vwv
+        * array. req->buf points right after the vwv array of the
+        * previous request. See
+        * https://bugzilla.samba.org/show_bug.cgi?id=8360 for more
+        * information.
         */
 
-       already_used = PTR_DIFF(req->buf+req->buflen, smb_base(req->inbuf));
-       if (chain_offset < already_used) {
+       already_used = PTR_DIFF(req->buf, smb_base(req->inbuf));
+       if (chain_offset <= already_used) {
                goto error;
        }
 
@@ -2196,35 +2233,41 @@ static void smbd_server_connection_read_handler(
        NTSTATUS status;
        uint32_t seqnum;
 
-       bool from_client = (sconn->sock == fd);
+       bool from_client;
+
+       if (lp_async_smb_echo_handler()
+           && fd_is_readable(sconn->smb1.echo_handler.trusted_fd)) {
+               /*
+                * This is the super-ugly hack to prefer the packets
+                * forwarded by the echo handler over the ones by the
+                * client directly
+                */
+               fd = sconn->smb1.echo_handler.trusted_fd;
+       }
+
+       from_client = (sconn->sock == fd);
 
        if (from_client) {
                smbd_lock_socket(sconn);
 
-               if (lp_async_smb_echo_handler() && !fd_is_readable(fd)) {
+               if (!fd_is_readable(fd)) {
                        DEBUG(10,("the echo listener was faster\n"));
                        smbd_unlock_socket(sconn);
                        return;
                }
+       }
 
-               /* TODO: make this completely nonblocking */
-               status = receive_smb_talloc(mem_ctx, sconn, fd,
-                                           (char **)(void *)&inbuf,
-                                           0, /* timeout */
-                                           &unread_bytes,
-                                           &encrypted,
-                                           &inbuf_len, &seqnum,
-                                           false /* trusted channel */);
+       /* TODO: make this completely nonblocking */
+       status = receive_smb_talloc(mem_ctx, sconn, fd,
+                                   (char **)(void *)&inbuf,
+                                   0, /* timeout */
+                                   &unread_bytes,
+                                   &encrypted,
+                                   &inbuf_len, &seqnum,
+                                   false /* trusted channel */);
+
+       if (from_client) {
                smbd_unlock_socket(sconn);
-       } else {
-               /* TODO: make this completely nonblocking */
-               status = receive_smb_talloc(mem_ctx, sconn, fd,
-                                           (char **)(void *)&inbuf,
-                                           0, /* timeout */
-                                           &unread_bytes,
-                                           &encrypted,
-                                           &inbuf_len, &seqnum,
-                                           true /* trusted channel */);
        }
 
        if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
@@ -2330,7 +2373,8 @@ static int client_get_tcp_info(int sock, struct sockaddr_storage *server,
  */
 static bool keepalive_fn(const struct timeval *now, void *private_data)
 {
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = talloc_get_type_abort(
+               private_data, struct smbd_server_connection);
        bool ret;
 
        if (sconn->using_smb2) {
@@ -2338,9 +2382,9 @@ static bool keepalive_fn(const struct timeval *now, void *private_data)
                return false;
        }
 
-       smbd_lock_socket(smbd_server_conn);
+       smbd_lock_socket(sconn);
        ret = send_keepalive(sconn->sock);
-       smbd_unlock_socket(smbd_server_conn);
+       smbd_unlock_socket(sconn);
 
        if (!ret) {
                char addr[INET6_ADDRSTRLEN];
@@ -2407,32 +2451,6 @@ static bool housekeeping_fn(const struct timeval *now, void *private_data)
        return true;
 }
 
-static int create_unlink_tmp(const char *dir)
-{
-       char *fname;
-       int fd;
-
-       fname = talloc_asprintf(talloc_tos(), "%s/listenerlock_XXXXXX", dir);
-       if (fname == NULL) {
-               errno = ENOMEM;
-               return -1;
-       }
-       fd = mkstemp(fname);
-       if (fd == -1) {
-               TALLOC_FREE(fname);
-               return -1;
-       }
-       if (unlink(fname) == -1) {
-               int sys_errno = errno;
-               close(fd);
-               TALLOC_FREE(fname);
-               errno = sys_errno;
-               return -1;
-       }
-       TALLOC_FREE(fname);
-       return fd;
-}
-
 /*
  * Read an smb packet in the echo handler child, giving the parent
  * smbd one second to react once the socket becomes readable.
@@ -2595,7 +2613,6 @@ struct smbd_echo_state {
 
        struct tevent_fd *parent_fde;
 
-       struct tevent_fd *read_fde;
        struct tevent_req *write_req;
 };
 
@@ -2647,7 +2664,8 @@ static void smbd_echo_writer_done(struct tevent_req *req)
        smbd_echo_activate_writer(state);
 }
 
-static bool smbd_echo_reply(uint8_t *inbuf, size_t inbuf_len,
+static bool smbd_echo_reply(struct smbd_echo_state *state,
+                           uint8_t *inbuf, size_t inbuf_len,
                            uint32_t seqnum)
 {
        struct smb_request req;
@@ -2668,12 +2686,12 @@ static bool smbd_echo_reply(uint8_t *inbuf, size_t inbuf_len,
                DEBUG(10, ("Got short packet: %d bytes\n", (int)inbuf_len));
                return false;
        }
-       if (!valid_smb_header(inbuf)) {
+       if (!valid_smb_header(state->sconn, inbuf)) {
                DEBUG(10, ("Got invalid SMB header\n"));
                return false;
        }
 
-       if (!init_smb_request(&req, smbd_server_conn, inbuf, 0, false,
+       if (!init_smb_request(&req, state->sconn, inbuf, 0, false,
                              seqnum)) {
                return false;
        }
@@ -2731,107 +2749,13 @@ static void smbd_echo_exit(struct tevent_context *ev,
        exit(0);
 }
 
-static void smbd_echo_reader(struct tevent_context *ev,
-                            struct tevent_fd *fde, uint16_t flags,
-                            void *private_data)
-{
-       struct smbd_echo_state *state = talloc_get_type_abort(
-               private_data, struct smbd_echo_state);
-       struct smbd_server_connection *sconn = state->sconn;
-       size_t unread, num_pending;
-       NTSTATUS status;
-       struct iovec *tmp;
-       size_t iov_len;
-       uint32_t seqnum = 0;
-       bool reply;
-       bool ok;
-       bool encrypted = false;
-
-       smb_msleep(1000);
-
-       ok = smbd_lock_socket_internal(sconn);
-       if (!ok) {
-               DEBUG(0, ("%s: failed to lock socket\n",
-                       __location__));
-               exit(1);
-       }
-
-       if (!fd_is_readable(sconn->sock)) {
-               DEBUG(10,("echo_handler[%d] the parent smbd was faster\n",
-                         (int)sys_getpid()));
-               ok = smbd_unlock_socket_internal(sconn);
-               if (!ok) {
-                       DEBUG(1, ("%s: failed to unlock socket in\n",
-                               __location__));
-                       exit(1);
-               }
-               return;
-       }
-
-       num_pending = talloc_array_length(state->pending);
-       tmp = talloc_realloc(state, state->pending, struct iovec,
-                            num_pending+1);
-       if (tmp == NULL) {
-               DEBUG(1, ("talloc_realloc failed\n"));
-               exit(1);
-       }
-       state->pending = tmp;
-
-       DEBUG(10,("echo_handler[%d]: reading pdu\n", (int)sys_getpid()));
-
-       status = receive_smb_talloc(state->pending, sconn, sconn->sock,
-                                   (char **)(void *)&state->pending[num_pending].iov_base,
-                                   0 /* timeout */,
-                                   &unread,
-                                   &encrypted,
-                                   &iov_len,
-                                   &seqnum,
-                                   false /* trusted_channel*/);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("echo_handler[%d]: receive_smb_raw_talloc failed: %s\n",
-                         (int)sys_getpid(), nt_errstr(status)));
-               exit(1);
-       }
-       state->pending[num_pending].iov_len = iov_len;
-
-       ok = smbd_unlock_socket_internal(sconn);
-       if (!ok) {
-               DEBUG(1, ("%s: failed to unlock socket in\n",
-                       __location__));
-               exit(1);
-       }
-
-       reply = smbd_echo_reply((uint8_t *)state->pending[num_pending].iov_base,
-                               state->pending[num_pending].iov_len,
-                               seqnum);
-       if (reply) {
-               DEBUG(10,("echo_handler[%d]: replied to client\n", (int)sys_getpid()));
-               /* no check, shrinking by some bytes does not fail */
-               state->pending = talloc_realloc(state, state->pending,
-                                               struct iovec,
-                                               num_pending);
-               return;
-       }
-
-       if (state->pending[num_pending].iov_len >= smb_size) {
-               /*
-                * place the seqnum in the packet so that the main process
-                * can reply with signing
-                */
-               SIVAL((uint8_t *)state->pending[num_pending].iov_base,
-                     smb_ss_field, seqnum);
-               SIVAL((uint8_t *)state->pending[num_pending].iov_base,
-                     smb_ss_field+4, NT_STATUS_V(NT_STATUS_OK));
-       }
-
-       DEBUG(10,("echo_handler[%d]: forward to main\n", (int)sys_getpid()));
-       smbd_echo_activate_writer(state);
-}
+static void smbd_echo_got_packet(struct tevent_req *req);
 
 static void smbd_echo_loop(struct smbd_server_connection *sconn,
                           int parent_pipe)
 {
        struct smbd_echo_state *state;
+       struct tevent_req *read_req;
 
        state = talloc_zero(sconn, struct smbd_echo_state);
        if (state == NULL) {
@@ -2854,14 +2778,14 @@ static void smbd_echo_loop(struct smbd_server_connection *sconn,
                TALLOC_FREE(state);
                return;
        }
-       state->read_fde = tevent_add_fd(state->ev, state, sconn->sock,
-                                       TEVENT_FD_READ, smbd_echo_reader,
-                                       state);
-       if (state->read_fde == NULL) {
-               DEBUG(1, ("tevent_add_fd failed\n"));
+
+       read_req = smbd_echo_read_send(state, state->ev, sconn);
+       if (read_req == NULL) {
+               DEBUG(1, ("smbd_echo_read_send failed\n"));
                TALLOC_FREE(state);
                return;
        }
+       tevent_req_set_callback(read_req, smbd_echo_got_packet, state);
 
        while (true) {
                if (tevent_loop_once(state->ev) == -1) {
@@ -2873,6 +2797,66 @@ static void smbd_echo_loop(struct smbd_server_connection *sconn,
        TALLOC_FREE(state);
 }
 
+static void smbd_echo_got_packet(struct tevent_req *req)
+{
+       struct smbd_echo_state *state = tevent_req_callback_data(
+               req, struct smbd_echo_state);
+       NTSTATUS status;
+       char *buf = NULL;
+       size_t buflen = 0;
+       uint32_t seqnum = 0;
+       bool reply;
+
+       status = smbd_echo_read_recv(req, state, &buf, &buflen, &seqnum);
+       TALLOC_FREE(req);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("smbd_echo_read_recv returned %s\n",
+                         nt_errstr(status)));
+               exit(1);
+       }
+
+       reply = smbd_echo_reply(state, (uint8_t *)buf, buflen, seqnum);
+       if (!reply) {
+               size_t num_pending;
+               struct iovec *tmp;
+               struct iovec *iov;
+
+               num_pending = talloc_array_length(state->pending);
+               tmp = talloc_realloc(state, state->pending, struct iovec,
+                                    num_pending+1);
+               if (tmp == NULL) {
+                       DEBUG(1, ("talloc_realloc failed\n"));
+                       exit(1);
+               }
+               state->pending = tmp;
+
+               if (buflen >= smb_size) {
+                       /*
+                        * place the seqnum in the packet so that the main process
+                        * can reply with signing
+                        */
+                       SIVAL(buf, smb_ss_field, seqnum);
+                       SIVAL(buf, smb_ss_field+4, NT_STATUS_V(NT_STATUS_OK));
+               }
+
+               iov = &state->pending[num_pending];
+               iov->iov_base = buf;
+               iov->iov_len = buflen;
+
+               DEBUG(10,("echo_handler[%d]: forward to main\n",
+                         (int)sys_getpid()));
+               smbd_echo_activate_writer(state);
+       }
+
+       req = smbd_echo_read_send(state, state->ev, state->sconn);
+       if (req == NULL) {
+               DEBUG(1, ("smbd_echo_read_send failed\n"));
+               exit(1);
+       }
+       tevent_req_set_callback(req, smbd_echo_got_packet, state);
+}
+
+
 /*
  * Handle SMBecho requests in a forked child process
  */
@@ -2979,7 +2963,8 @@ static NTSTATUS smbd_register_ips(struct smbd_server_connection *sconn,
  Process commands from the client
 ****************************************************************************/
 
-void smbd_process(struct smbd_server_connection *sconn)
+void smbd_process(struct tevent_context *ev_ctx,
+                 struct smbd_server_connection *sconn)
 {
        TALLOC_CTX *frame = talloc_stackframe();
        struct sockaddr_storage ss;
@@ -2987,6 +2972,7 @@ void smbd_process(struct smbd_server_connection *sconn)
        socklen_t sa_socklen;
        struct tsocket_address *local_address = NULL;
        struct tsocket_address *remote_address = NULL;
+       const char *locaddr = NULL;
        const char *remaddr = NULL;
        char *rhost;
        int ret;
@@ -3045,6 +3031,19 @@ void smbd_process(struct smbd_server_connection *sconn)
        sconn->local_address = local_address;
        sconn->remote_address = remote_address;
 
+       if (tsocket_address_is_inet(local_address, "ip")) {
+               locaddr = tsocket_address_inet_addr_string(
+                               sconn->local_address,
+                               talloc_tos());
+               if (locaddr == NULL) {
+                       DEBUG(0,("%s: tsocket_address_inet_addr_string local failed - %s\n",
+                                __location__, strerror(errno)));
+                       exit_server_cleanly("tsocket_address_inet_addr_string local failed.\n");
+               }
+       } else {
+               locaddr = "0.0.0.0";
+       }
+
        if (tsocket_address_is_inet(remote_address, "ip")) {
                remaddr = tsocket_address_inet_addr_string(
                                sconn->remote_address,
@@ -3083,6 +3082,10 @@ void smbd_process(struct smbd_server_connection *sconn)
        }
        sconn->remote_hostname = talloc_move(sconn, &rhost);
 
+       sub_set_socket_ids(remaddr,
+                          sconn->remote_hostname,
+                          locaddr);
+
        if (!allow_access(lp_hostsdeny(-1), lp_hostsallow(-1),
                          sconn->remote_hostname,
                          remaddr)) {
@@ -3147,22 +3150,22 @@ void smbd_process(struct smbd_server_connection *sconn)
                           MSG_DEBUG, debug_message);
 
        if ((lp_keepalive() != 0)
-           && !(event_add_idle(server_event_context(), NULL,
+           && !(event_add_idle(ev_ctx, NULL,
                                timeval_set(lp_keepalive(), 0),
                                "keepalive", keepalive_fn,
-                               NULL))) {
+                               sconn))) {
                DEBUG(0, ("Could not add keepalive event\n"));
                exit(1);
        }
 
-       if (!(event_add_idle(server_event_context(), NULL,
+       if (!(event_add_idle(ev_ctx, NULL,
                             timeval_set(IDLE_CLOSED_TIMEOUT, 0),
                             "deadtime", deadtime_fn, sconn))) {
                DEBUG(0, ("Could not add deadtime event\n"));
                exit(1);
        }
 
-       if (!(event_add_idle(server_event_context(), NULL,
+       if (!(event_add_idle(ev_ctx, NULL,
                             timeval_set(SMBD_HOUSEKEEPING_INTERVAL, 0),
                             "housekeeping", housekeeping_fn, sconn))) {
                DEBUG(0, ("Could not add housekeeping event\n"));
@@ -3219,7 +3222,7 @@ void smbd_process(struct smbd_server_connection *sconn)
                exit_server("init_dptrs() failed");
        }
 
-       sconn->smb1.fde = event_add_fd(server_event_context(),
+       sconn->smb1.fde = event_add_fd(ev_ctx,
                                                  sconn,
                                                  sconn->sock,
                                                  EVENT_FD_READ,
@@ -3238,7 +3241,7 @@ void smbd_process(struct smbd_server_connection *sconn)
 
                errno = 0;
 
-               status = smbd_server_connection_loop_once(sconn);
+               status = smbd_server_connection_loop_once(ev_ctx, sconn);
                if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY) &&
                    !NT_STATUS_IS_OK(status)) {
                        DEBUG(3, ("smbd_server_connection_loop_once failed: %s,"