r4232: added server support for multi-part SMBtrans requests, while
[samba.git] / source4 / smb_server / smb_server.c
index 94bf6302c11d8f9b6529d8ff02cda288c8a93340..2ebb927f1ea0b32e12cf7cdeb066e3b700a03ea1 100644 (file)
@@ -3,6 +3,7 @@
    process incoming packets - main loop
    Copyright (C) Andrew Tridgell 1992-2003
    Copyright (C) James J Myers 2003 <myersjj@samba.org>
+   Copyright (C) Stefan Metzmacher 2004
    
    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
 */
 
 #include "includes.h"
+#include "events.h"
+#include "system/time.h"
+#include "dlinklist.h"
+#include "smb_server/smb_server.h"
 
 
 /*
@@ -54,40 +59,87 @@ BOOL req_send_oplock_break(struct smbsrv_tcon *tcon, uint16_t fnum, uint8_t leve
        return True;
 }
 
+
+static void construct_reply(struct smbsrv_request *req);
+
 /****************************************************************************
-receive a SMB request from the wire, forming a request_context from the result
+receive a SMB request header from the wire, forming a request_context
+from the result
 ****************************************************************************/
-static struct smbsrv_request *receive_smb_request(struct smbsrv_connection *smb_conn)
+static NTSTATUS receive_smb_request(struct smbsrv_connection *smb_conn, struct timeval t)
 {
-       ssize_t len, len2;
-       char header[4];
+       NTSTATUS status;
+       ssize_t len;
        struct smbsrv_request *req;
+       size_t nread;
+
+       /* allocate the request if needed */
+       if (smb_conn->partial_req == NULL) {
+               req = init_smb_request(smb_conn);
+               if (req == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
 
-       len = read_data(smb_conn->connection->socket->fde->fd, header, 4);
-       if (len != 4) {
-               return NULL;
+               req->in.buffer = talloc_array_p(req, uint8_t, NBT_HDR_SIZE);
+               if (req->in.buffer == NULL) {
+                       talloc_free(req);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               req->in.size = 0;
+               smb_conn->partial_req = req;
        }
 
-       len = smb_len(header);
+       req = smb_conn->partial_req;
 
-       req = init_smb_request(smb_conn);
+       /* read in the header */
+       if (req->in.size < NBT_HDR_SIZE) {
+               status = socket_recv(smb_conn->connection->socket, 
+                                    req->in.buffer + req->in.size,
+                                    NBT_HDR_SIZE - req->in.size, 
+                                    &nread, 0);
+               if (NT_STATUS_IS_ERR(status)) {
+                       return status;
+               }
+               if (nread == 0) {
+                       return NT_STATUS_OK;
+               }
+               req->in.size += nread;
+
+               /* when we have a full NBT header, then allocate the packet */
+               if (req->in.size == NBT_HDR_SIZE) {
+                       len = smb_len(req->in.buffer) + NBT_HDR_SIZE;
+                       req->in.buffer = talloc_realloc(req, req->in.buffer, len);
+                       if (req->in.buffer == NULL) {
+                               return NT_STATUS_NO_MEMORY;
+                       }
+               } else {
+                       return NT_STATUS_OK;
+               }
+       }
 
-       GetTimeOfDay(&req->request_time);
-       req->chained_fnum = -1;
-       
-       /* allocate the incoming buffer at the right size */
-       req->in.buffer = talloc(req->mem_ctx, len + NBT_HDR_SIZE);
+       /* read in the main packet */
+       len = smb_len(req->in.buffer) + NBT_HDR_SIZE;
 
-       /* fill in the already received header */
-       memcpy(req->in.buffer, header, 4);
+       status = socket_recv(smb_conn->connection->socket, 
+                            req->in.buffer + req->in.size,
+                            len - req->in.size, 
+                            &nread, 0);
+       if (NT_STATUS_IS_ERR(status)) {
+               return status;
+       }
+       if (nread == 0) {
+               return NT_STATUS_OK;
+       }
+
+       req->in.size += nread;
 
-       len2 = read_data(smb_conn->connection->socket->fde->fd, req->in.buffer + NBT_HDR_SIZE, len);
-       if (len2 != len) {
-               return NULL;
+       if (req->in.size != len) {
+               return NT_STATUS_OK;
        }
 
-       /* fill in the rest of the req->in structure */
-       req->in.size = len + NBT_HDR_SIZE;
+       /* we have a full packet */
+       req->request_time = t;
+       req->chained_fnum = -1;
        req->in.allocated = req->in.size;
        req->in.hdr = req->in.buffer + NBT_HDR_SIZE;
        req->in.vwv = req->in.hdr + HDR_VWV;
@@ -110,22 +162,17 @@ static struct smbsrv_request *receive_smb_request(struct smbsrv_connection *smb_
                }
        }
 
-       return req;
+       smb_conn->partial_req = NULL;
+
+       construct_reply(req);
+
+       return NT_STATUS_OK;
 }
 
 /*
-These flags determine some of the permissions required to do an operation 
-
-Note that I don't set NEED_WRITE on some write operations because they
-are used by some brain-dead clients when printing, and I don't want to
-force write permissions on print services.
+  These flags determine some of the permissions required to do an operation 
 */
 #define AS_USER (1<<0)
-#define NEED_WRITE (1<<1)
-#define TIME_INIT (1<<2)
-#define CAN_IPC (1<<3)
-#define AS_GUEST (1<<5)
-#define USE_MUTEX (1<<7)
 
 /* 
    define a list of possible SMB messages and their corresponding
@@ -139,18 +186,18 @@ static const struct smb_message_struct
        int flags;
 }
  smb_messages[256] = {
-/* 0x00 */ { "SMBmkdir",reply_mkdir,AS_USER | NEED_WRITE},
-/* 0x01 */ { "SMBrmdir",reply_rmdir,AS_USER | NEED_WRITE},
-/* 0x02 */ { "SMBopen",reply_open,AS_USER },
+/* 0x00 */ { "SMBmkdir",reply_mkdir,AS_USER},
+/* 0x01 */ { "SMBrmdir",reply_rmdir,AS_USER},
+/* 0x02 */ { "SMBopen",reply_open,AS_USER},
 /* 0x03 */ { "SMBcreate",reply_mknew,AS_USER},
-/* 0x04 */ { "SMBclose",reply_close,AS_USER | CAN_IPC },
+/* 0x04 */ { "SMBclose",reply_close,AS_USER},
 /* 0x05 */ { "SMBflush",reply_flush,AS_USER},
-/* 0x06 */ { "SMBunlink",reply_unlink,AS_USER | NEED_WRITE },
-/* 0x07 */ { "SMBmv",reply_mv,AS_USER | NEED_WRITE },
+/* 0x06 */ { "SMBunlink",reply_unlink,AS_USER},
+/* 0x07 */ { "SMBmv",reply_mv,AS_USER},
 /* 0x08 */ { "SMBgetatr",reply_getatr,AS_USER},
-/* 0x09 */ { "SMBsetatr",reply_setatr,AS_USER | NEED_WRITE},
+/* 0x09 */ { "SMBsetatr",reply_setatr,AS_USER},
 /* 0x0a */ { "SMBread",reply_read,AS_USER},
-/* 0x0b */ { "SMBwrite",reply_write,AS_USER | CAN_IPC },
+/* 0x0b */ { "SMBwrite",reply_write,AS_USER},
 /* 0x0c */ { "SMBlock",reply_lock,AS_USER},
 /* 0x0d */ { "SMBunlock",reply_unlock,AS_USER},
 /* 0x0e */ { "SMBctemp",reply_ctemp,AS_USER },
@@ -173,23 +220,23 @@ static const struct smb_message_struct
 /* 0x1f */ { "SMBwriteBs",reply_writebs,AS_USER},
 /* 0x20 */ { "SMBwritec",NULL,0},
 /* 0x21 */ { NULL, NULL, 0 },
-/* 0x22 */ { "SMBsetattrE",reply_setattrE,AS_USER | NEED_WRITE },
-/* 0x23 */ { "SMBgetattrE",reply_getattrE,AS_USER },
-/* 0x24 */ { "SMBlockingX",reply_lockingX,AS_USER },
-/* 0x25 */ { "SMBtrans",reply_trans,AS_USER | CAN_IPC },
-/* 0x26 */ { "SMBtranss",NULL,AS_USER | CAN_IPC},
+/* 0x22 */ { "SMBsetattrE",reply_setattrE,AS_USER},
+/* 0x23 */ { "SMBgetattrE",reply_getattrE,AS_USER},
+/* 0x24 */ { "SMBlockingX",reply_lockingX,AS_USER},
+/* 0x25 */ { "SMBtrans",reply_trans,AS_USER},
+/* 0x26 */ { "SMBtranss",reply_transs,AS_USER},
 /* 0x27 */ { "SMBioctl",reply_ioctl,0},
 /* 0x28 */ { "SMBioctls",NULL,AS_USER},
-/* 0x29 */ { "SMBcopy",reply_copy,AS_USER | NEED_WRITE },
-/* 0x2a */ { "SMBmove",NULL,AS_USER | NEED_WRITE },
+/* 0x29 */ { "SMBcopy",reply_copy,AS_USER},
+/* 0x2a */ { "SMBmove",NULL,AS_USER},
 /* 0x2b */ { "SMBecho",reply_echo,0},
 /* 0x2c */ { "SMBwriteclose",reply_writeclose,AS_USER},
-/* 0x2d */ { "SMBopenX",reply_open_and_X,AS_USER | CAN_IPC },
-/* 0x2e */ { "SMBreadX",reply_read_and_X,AS_USER | CAN_IPC },
-/* 0x2f */ { "SMBwriteX",reply_write_and_X,AS_USER | CAN_IPC },
+/* 0x2d */ { "SMBopenX",reply_open_and_X,AS_USER},
+/* 0x2e */ { "SMBreadX",reply_read_and_X,AS_USER},
+/* 0x2f */ { "SMBwriteX",reply_write_and_X,AS_USER},
 /* 0x30 */ { NULL, NULL, 0 },
 /* 0x31 */ { NULL, NULL, 0 },
-/* 0x32 */ { "SMBtrans2", reply_trans2, AS_USER | CAN_IPC },
+/* 0x32 */ { "SMBtrans2", reply_trans2, AS_USER},
 /* 0x33 */ { "SMBtranss2", reply_transs2, AS_USER},
 /* 0x34 */ { "SMBfindclose", reply_findclose,AS_USER},
 /* 0x35 */ { "SMBfindnclose", reply_findnclose, AS_USER},
@@ -251,12 +298,12 @@ static const struct smb_message_struct
 /* 0x6d */ { NULL, NULL, 0 },
 /* 0x6e */ { NULL, NULL, 0 },
 /* 0x6f */ { NULL, NULL, 0 },
-/* 0x70 */ { "SMBtcon",reply_tcon,USE_MUTEX},
+/* 0x70 */ { "SMBtcon",reply_tcon,0},
 /* 0x71 */ { "SMBtdis",reply_tdis,0},
-/* 0x72 */ { "SMBnegprot",reply_negprot,USE_MUTEX},
-/* 0x73 */ { "SMBsesssetupX",reply_sesssetup,USE_MUTEX},
+/* 0x72 */ { "SMBnegprot",reply_negprot,0},
+/* 0x73 */ { "SMBsesssetupX",reply_sesssetup,0},
 /* 0x74 */ { "SMBulogoffX", reply_ulogoffX, 0}, /* ulogoff doesn't give a valid TID */
-/* 0x75 */ { "SMBtconX",reply_tcon_and_X,USE_MUTEX},
+/* 0x75 */ { "SMBtconX",reply_tcon_and_X,0},
 /* 0x76 */ { NULL, NULL, 0 },
 /* 0x77 */ { NULL, NULL, 0 },
 /* 0x78 */ { NULL, NULL, 0 },
@@ -299,9 +346,9 @@ static const struct smb_message_struct
 /* 0x9d */ { NULL, NULL, 0 },
 /* 0x9e */ { NULL, NULL, 0 },
 /* 0x9f */ { NULL, NULL, 0 },
-/* 0xa0 */ { "SMBnttrans", reply_nttrans, AS_USER | CAN_IPC },
-/* 0xa1 */ { "SMBnttranss", reply_nttranss, AS_USER | CAN_IPC },
-/* 0xa2 */ { "SMBntcreateX", reply_ntcreate_and_X, AS_USER | CAN_IPC },
+/* 0xa0 */ { "SMBnttrans", reply_nttrans, AS_USER},
+/* 0xa1 */ { "SMBnttranss", reply_nttranss, AS_USER},
+/* 0xa2 */ { "SMBntcreateX", reply_ntcreate_and_X, AS_USER},
 /* 0xa3 */ { NULL, NULL, 0 },
 /* 0xa4 */ { "SMBntcancel", reply_ntcancel, 0 },
 /* 0xa5 */ { "SMBntrename", reply_ntrename, 0 },
@@ -347,14 +394,14 @@ static const struct smb_message_struct
 /* 0xcd */ { NULL, NULL, 0 },
 /* 0xce */ { NULL, NULL, 0 },
 /* 0xcf */ { NULL, NULL, 0 },
-/* 0xd0 */ { "SMBsends",reply_sends,AS_GUEST},
-/* 0xd1 */ { "SMBsendb",NULL,AS_GUEST},
-/* 0xd2 */ { "SMBfwdname",NULL,AS_GUEST},
-/* 0xd3 */ { "SMBcancelf",NULL,AS_GUEST},
-/* 0xd4 */ { "SMBgetmac",NULL,AS_GUEST},
-/* 0xd5 */ { "SMBsendstrt",reply_sendstrt,AS_GUEST},
-/* 0xd6 */ { "SMBsendend",reply_sendend,AS_GUEST},
-/* 0xd7 */ { "SMBsendtxt",reply_sendtxt,AS_GUEST},
+/* 0xd0 */ { "SMBsends",reply_sends,0},
+/* 0xd1 */ { "SMBsendb",NULL,0},
+/* 0xd2 */ { "SMBfwdname",NULL,0},
+/* 0xd3 */ { "SMBcancelf",NULL,0},
+/* 0xd4 */ { "SMBgetmac",NULL,0},
+/* 0xd5 */ { "SMBsendstrt",reply_sendstrt,0},
+/* 0xd6 */ { "SMBsendend",reply_sendend,0},
+/* 0xd7 */ { "SMBsendtxt",reply_sendtxt,0},
 /* 0xd8 */ { NULL, NULL, 0 },
 /* 0xd9 */ { NULL, NULL, 0 },
 /* 0xda */ { NULL, NULL, 0 },
@@ -421,8 +468,8 @@ onto the message queue
 static void switch_message(int type, struct smbsrv_request *req)
 {
        int flags;
-       uint16_t session_tag;
        struct smbsrv_connection *smb_conn = req->smb_conn;
+       uint16_t session_tag;
 
        type &= 0xff;
 
@@ -436,97 +483,43 @@ static void switch_message(int type, struct smbsrv_request *req)
 
        flags = smb_messages[type].flags;
 
-       /* In share mode security we must ignore the vuid. */
-       session_tag = (lp_security() == SEC_SHARE) ? 
-               UID_FIELD_INVALID : 
-               SVAL(req->in.hdr,HDR_UID);
-
        req->tcon = conn_find(smb_conn, SVAL(req->in.hdr,HDR_TID));
 
-       /* setup the user context for this request */
-       req->session = smbsrv_session_find(req->smb_conn, session_tag);
+       if (req->session == NULL) {
+               /* setup the user context for this request if it
+                  hasn't already been initialised (to cope with SMB
+                  chaining) */
 
-       /* Ensure this value is replaced in the incoming packet. */
-       SSVAL(req->in.hdr,HDR_UID,session_tag);
+               /* In share mode security we must ignore the vuid. */
+               if (lp_security() == SEC_SHARE) {
+                       session_tag = UID_FIELD_INVALID;
+               } else {
+                       session_tag = SVAL(req->in.hdr,HDR_UID);
+               }
 
-       if (req->session) {
-               req->session->vuid = session_tag;
+               req->session = smbsrv_session_find(req->smb_conn, session_tag);
+               if (req->session) {
+                       req->session->vuid = session_tag;
+               }
+       } else {
+               session_tag = req->session->vuid;
        }
+
        DEBUG(3,("switch message %s (task_id %d)\n",smb_fn_name(type), smb_conn->connection->service->model_ops->get_id(req)));
 
-       /* does this protocol need to be run as root? */
-       if (!(flags & AS_USER)) {
-               change_to_root_user();
-       }
-       
        /* does this protocol need a valid tree connection? */
        if ((flags & AS_USER) && !req->tcon) {
-               req_reply_error(req, NT_STATUS_NETWORK_NAME_DELETED);
+               req_reply_error(req, NT_STATUS_INVALID_HANDLE);
                return;
        }
 
        /* see if the vuid is valid */
        if ((flags & AS_USER) && !req->session) {
-               if (!(flags & AS_GUEST)) {
-                       req_reply_error(req, NT_STATUS_DOS(ERRSRV, ERRbaduid));
-                       return;
-               }
-       }
-
-       /* does this protocol need to be run as the connected user? */
-#if HACK_REWRITE
-       if ((flags & AS_USER) && !change_to_user(req->tcon,session_tag)) {
-               if (!(flags & AS_GUEST)) {
-                       req_reply_error(req, NT_STATUS_ACCESS_DENIED);
-                       return;
-               }
-
-               /* we'll run it as guest */
-               flags &= ~AS_USER;
-       }
-#endif
-
-       /* this code is to work around a bug is MS client 3 without
-          introducing a security hole - it needs to be able to do
-          print queue checks as guest if it isn't logged in properly */
-       if (flags & AS_USER) {
-               flags &= ~AS_GUEST;
-       }
-       
-       /* does it need write permission? */
-       if ((flags & NEED_WRITE) && !CAN_WRITE(req->tcon)) {
-               req_reply_error(req, NT_STATUS_ACCESS_DENIED);
+               req_reply_error(req, NT_STATUS_DOS(ERRSRV, ERRbaduid));
                return;
        }
-       
-       /* ipc services are limited */
-       if (req->tcon && req->tcon->type == NTVFS_IPC && (flags & AS_USER) && !(flags & CAN_IPC)) {
-               req_reply_error(req, NT_STATUS_ACCESS_DENIED);
-               return;
-       }
-       
-       /* load service specific parameters */
-       if (req->tcon && !set_current_service(req->tcon,(flags & AS_USER)?True:False)) {
-               req_reply_error(req, NT_STATUS_ACCESS_DENIED);
-               return;
-       }
-       
-       /* does this protocol need to be run as guest? */
-#if HACK_REWRITE
-       if ((flags & AS_GUEST) && 
-           !change_to_guest()) {
-               req_reply_error(req, NT_STATUS_ACCESS_DENIED);
-               return;
-       }
-#endif
-       /* THREAD TESTING: use mutex to serialize calls to critical functions with global state */
-       if (flags & USE_MUTEX) {
-               MUTEX_LOCK_BY_ID(MUTEX_SMBD);
-       }
+
        smb_messages[type].fn(req);
-       if (flags & USE_MUTEX) {
-               MUTEX_UNLOCK_BY_ID(MUTEX_SMBD);
-       }
 }
 
 
@@ -563,10 +556,9 @@ static void construct_reply(struct smbsrv_request *req)
                return;
        }
 
-
-       req->smbpid = SVAL(req->in.hdr,HDR_PID);        
        req->flags = CVAL(req->in.hdr, HDR_FLG);
        req->flags2 = SVAL(req->in.hdr, HDR_FLG2);
+       req->smbpid = SVAL(req->in.hdr,HDR_PID);
 
        if (!req_signing_check_incoming(req)) {
                req_reply_error(req, NT_STATUS_ACCESS_DENIED);
@@ -584,7 +576,7 @@ static void construct_reply(struct smbsrv_request *req)
 void chain_reply(struct smbsrv_request *req)
 {
        uint16_t chain_cmd, chain_offset;
-       char *vwv, *data;
+       uint8_t *vwv, *data;
        uint16_t wct;
        uint16_t data_size;
 
@@ -636,8 +628,7 @@ void chain_reply(struct smbsrv_request *req)
 
        /* the current request in the chain might have used an async reply,
           but that doesn't mean the next element needs to */
-       ZERO_STRUCT(req->async);
-       req->control_flags &= ~REQ_CONTROL_ASYNC;
+       ZERO_STRUCTP(req->async_states);
 
        switch_message(chain_cmd, req);
        return;
@@ -672,18 +663,19 @@ static void smbsrv_exit(struct server_service *service, const char *reason)
 static void add_socket(struct server_service *service, 
                       const struct model_ops *model_ops,
                       struct socket_context *socket_ctx, 
-                      struct in_addr *ifip)
+                      struct ipv4_addr *ifip)
 {
-       char *ptr, *tok;
-       const char *delim = ", ";
+       const char **ports = lp_smb_ports();
+       int i;
+       char *ip_str = talloc_strdup(service, sys_inet_ntoa(*ifip));
 
-       for (tok=strtok_r(lp_smb_ports(), delim, &ptr); 
-            tok; 
-            tok=strtok_r(NULL, delim, &ptr)) {
-               uint16_t port = atoi(tok);
+       for (i=0;ports[i];i++) {
+               uint16_t port = atoi(ports[i]);
                if (port == 0) continue;
-               service_setup_socket(service, model_ops, socket_ctx, ifip, &port);
+               service_setup_socket(service, model_ops, "ipv4", ip_str, &port);
        }
+
+       talloc_free(ip_str);
 }
 
 /****************************************************************************
@@ -702,7 +694,7 @@ static void smbsrv_init(struct server_service *service, const struct model_ops *
                   socket per interface and bind to only these.
                */
                for(i = 0; i < num_interfaces; i++) {
-                       struct in_addr *ifip = iface_n_ip(i);
+                       struct ipv4_addr *ifip = iface_n_ip(i);
 
                        if (ifip == NULL) {
                                DEBUG(0,("open_sockets_smbd: interface %d has NULL IP address !\n", i));
@@ -712,61 +704,84 @@ static void smbsrv_init(struct server_service *service, const struct model_ops *
                        add_socket(service, model_ops, NULL, ifip);
                }
        } else {
-               struct in_addr *ifip;
-               TALLOC_CTX *mem_ctx = talloc_init("open_sockets_smbd");
-
-               if (!mem_ctx) {
-                       smb_panic("No memory");
-               }       
-
+               struct ipv4_addr ifip;
                /* Just bind to lp_socket_address() (usually 0.0.0.0) */
-               ifip = interpret_addr2(mem_ctx, lp_socket_address());
-               add_socket(service, model_ops, NULL, ifip);
-
-               talloc_destroy(mem_ctx);
+               ifip = interpret_addr2(lp_socket_address());
+               add_socket(service, model_ops, NULL, &ifip);
        }
 }
 
 /*
   called when a SMB socket becomes readable
 */
-static void smbsrv_recv(struct server_connection *conn, time_t t, uint16_t flags)
+static void smbsrv_recv(struct server_connection *conn, struct timeval t, uint16_t flags)
 {
-       struct smbsrv_request *req;
        struct smbsrv_connection *smb_conn = conn->private_data;
+       NTSTATUS status;
 
        DEBUG(10,("smbsrv_recv\n"));
 
-       req = receive_smb_request(smb_conn);
-       if (!req) {
-               smbsrv_terminate_connection(smb_conn, "receive error");
+       status = receive_smb_request(smb_conn, t);
+       if (NT_STATUS_IS_ERR(status)) {
+               conn->event.fde->flags = 0;
+               smbsrv_terminate_connection(smb_conn, nt_errstr(status));
                return;
        }
 
-       construct_reply(req);
-
        /* free up temporary memory */
        lp_talloc_free();
-       return;
 }
 
 /*
   called when a SMB socket becomes writable
 */
-static void smbsrv_send(struct server_connection *conn, time_t t, uint16_t flags)
+static void smbsrv_send(struct server_connection *conn, struct timeval t, uint16_t flags)
 {
-       DEBUG(10,("smbsrv_send\n"));
-       return;
+       struct smbsrv_connection *smb_conn = conn->private_data;
+
+       while (smb_conn->pending_send) {
+               struct smbsrv_request *req = smb_conn->pending_send;
+               DATA_BLOB blob;
+               NTSTATUS status;
+               size_t sendlen;
+
+               blob.data = req->out.buffer;
+               blob.length = req->out.size;
+
+               /* send as much of this request as we can */
+               status = socket_send(conn->socket, &blob, &sendlen, 0);
+               if (NT_STATUS_IS_ERR(status)) {
+                       smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
+                       return;
+               }
+               if (sendlen == 0) {
+                       break;
+               }
+
+               req->out.buffer += sendlen;
+               req->out.size -= sendlen;
+
+               /* is the whole request gone? */
+               if (req->out.size == 0) {
+                       DLIST_REMOVE(smb_conn->pending_send, req);
+                       req_destroy(req);
+               }
+       }
+
+       /* if no more requests are pending to be sent then
+          we should stop select for write */
+       if (smb_conn->pending_send == NULL) {
+               conn->event.fde->flags &= ~EVENT_FD_WRITE;
+       }
 }
 
 /*
   called when connection is idle
 */
-static void smbsrv_idle(struct server_connection *conn, time_t t)
+static void smbsrv_idle(struct server_connection *conn, struct timeval t)
 {
        DEBUG(10,("smbsrv_idle: not implemented!\n"));
-       conn->event.idle->next_event = t + 5;
-
+       conn->event.idle->next_event = timeval_add(&t, 5, 0);
        return;
 }
 
@@ -776,13 +791,10 @@ static void smbsrv_close(struct server_connection *conn, const char *reason)
 
        DEBUG(5,("smbsrv_close: %s\n",reason));
 
-       close(conn->event.fde->fd);
-       event_remove_fd_all(conn->event.ctx, conn->socket->fde->fd);
-       event_remove_timed(conn->event.ctx, conn->event.idle);
-
        conn_close_all(smb_conn);
 
-       talloc_destroy(smb_conn->mem_ctx);
+       talloc_free(smb_conn);
+
        return;
 }
 
@@ -794,15 +806,12 @@ static void smbsrv_close(struct server_connection *conn, const char *reason)
 */
 void smbd_process_async(struct smbsrv_connection *smb_conn)
 {
-       struct smbsrv_request *req;
+       NTSTATUS status;
        
-       req = receive_smb_request(smb_conn);
-       if (!req) {
-               smbsrv_terminate_connection(smb_conn, "receive error");
-               return;
+       status = receive_smb_request(smb_conn, timeval_current());
+       if (NT_STATUS_IS_ERR(status)) {
+               smbsrv_terminate_connection(smb_conn, nt_errstr(status));
        }
-
-       construct_reply(req);
 }
 
 
@@ -813,28 +822,17 @@ void smbd_process_async(struct smbsrv_connection *smb_conn)
 void smbsrv_accept(struct server_connection *conn)
 {
        struct smbsrv_connection *smb_conn;
-       TALLOC_CTX *mem_ctx;
-       char *socket_addr;
+       int fd;
 
        DEBUG(5,("smbsrv_accept\n"));
 
-       mem_ctx = talloc_init("smbsrv_context");
-
-       smb_conn = talloc_p(mem_ctx, struct smbsrv_connection);
+       smb_conn = talloc_zero_p(conn, struct smbsrv_connection);
        if (!smb_conn) return;
 
-       ZERO_STRUCTP(smb_conn);
-
-       smb_conn->mem_ctx = mem_ctx;
        smb_conn->pid = getpid();
 
        sub_set_context(&smb_conn->substitute);
 
-       /* set an initial client name based on its IP address. This will be replaced with
-          the netbios name later if it gives us one */
-       socket_addr = get_socket_addr(smb_conn->mem_ctx, conn->socket->fde->fd);
-       sub_set_remote_machine(socket_addr);
-
        /* now initialise a few default values associated with this smb socket */
        smb_conn->negotiate.max_send = 0xFFFF;
 
@@ -846,14 +844,19 @@ void smbsrv_accept(struct server_connection *conn)
 
        smb_conn->sessions.next_vuid = VUID_OFFSET;
 
+       srv_init_signing(smb_conn);
+
        conn_init(smb_conn);
 
        smb_conn->connection = conn;
 
        conn->private_data = smb_conn;
 
+       fd = socket_get_fd(conn->socket);
+       set_blocking(fd, True);
+
        /* setup the DCERPC server subsystem */
-       dcesrv_init_context(&smb_conn->dcesrv);
+       dcesrv_init_context(smb_conn, &smb_conn->dcesrv);
 
        return;
 }