Remove one use of mangle_is_8_3(), not needed.
[ira/wip.git] / source3 / smbd / reply.c
index 628f481c1ce1e03641ac13b215a52106b2ac78b3..a81c22b75050f46845e0d90b46b9163b3810fd3f 100644 (file)
@@ -72,11 +72,16 @@ static NTSTATUS check_path_syntax_internal(char *path,
                        }
                }
 
-               if (!stream_started && *s == ':') {
+               if (!posix_path && !stream_started && *s == ':') {
                        if (*p_last_component_contains_wcard) {
                                return NT_STATUS_OBJECT_NAME_INVALID;
                        }
-                       /* stream names allow more characters than file names */
+                       /* Stream names allow more characters than file names.
+                          We're overloading posix_path here to allow a wider
+                          range of characters. If stream_started is true this
+                          is still a Windows path even if posix_path is true.
+                          JRA.
+                       */
                        stream_started = true;
                        start_of_name_component = false;
                        posix_path = true;
@@ -402,6 +407,95 @@ bool fsp_belongs_conn(connection_struct *conn, struct smb_request *req,
        return False;
 }
 
+static bool netbios_session_retarget(const char *name, int name_type)
+{
+       char *trim_name;
+       char *trim_name_type;
+       const char *retarget_parm;
+       char *retarget;
+       char *p;
+       int retarget_type = 0x20;
+       int retarget_port = 139;
+       struct sockaddr_storage retarget_addr;
+       struct sockaddr_in *in_addr;
+       bool ret = false;
+       uint8_t outbuf[10];
+
+       if (get_socket_port(smbd_server_fd()) != 139) {
+               return false;
+       }
+
+       trim_name = talloc_strdup(talloc_tos(), name);
+       if (trim_name == NULL) {
+               goto fail;
+       }
+       trim_char(trim_name, ' ', ' ');
+
+       trim_name_type = talloc_asprintf(trim_name, "%s#%2.2x", trim_name,
+                                        name_type);
+       if (trim_name_type == NULL) {
+               goto fail;
+       }
+
+       retarget_parm = lp_parm_const_string(-1, "netbios retarget",
+                                            trim_name_type, NULL);
+       if (retarget_parm == NULL) {
+               retarget_parm = lp_parm_const_string(-1, "netbios retarget",
+                                                    trim_name, NULL);
+       }
+       if (retarget_parm == NULL) {
+               goto fail;
+       }
+
+       retarget = talloc_strdup(trim_name, retarget_parm);
+       if (retarget == NULL) {
+               goto fail;
+       }
+
+       DEBUG(10, ("retargeting %s to %s\n", trim_name_type, retarget));
+
+       p = strchr(retarget, ':');
+       if (p != NULL) {
+               *p++ = '\0';
+               retarget_port = atoi(p);
+       }
+
+       p = strchr_m(retarget, '#');
+       if (p != NULL) {
+               *p++ = '\0';
+               sscanf(p, "%x", &retarget_type);
+       }
+
+       ret = resolve_name(retarget, &retarget_addr, retarget_type);
+       if (!ret) {
+               DEBUG(10, ("could not resolve %s\n", retarget));
+               goto fail;
+       }
+
+       if (retarget_addr.ss_family != AF_INET) {
+               DEBUG(10, ("Retarget target not an IPv4 addr\n"));
+               goto fail;
+       }
+
+       in_addr = (struct sockaddr_in *)(void *)&retarget_addr;
+
+       _smb_setlen(outbuf, 6);
+       SCVAL(outbuf, 0, 0x84);
+       *(uint32_t *)(outbuf+4) = in_addr->sin_addr.s_addr;
+       *(uint16_t *)(outbuf+8) = htons(retarget_port);
+
+       if (!srv_send_smb(smbd_server_fd(), (char *)outbuf, false, 0, false,
+                         NULL)) {
+               exit_server_cleanly("netbios_session_regarget: srv_send_smb "
+                                   "failed.");
+       }
+
+       ret = true;
+ fail:
+       TALLOC_FREE(trim_name);
+       return ret;
+}
+
 /****************************************************************************
  Reply to a (netbios-level) special message.
 ****************************************************************************/
@@ -411,7 +505,7 @@ void reply_special(char *inbuf)
        int msg_type = CVAL(inbuf,0);
        int msg_flags = CVAL(inbuf,1);
        fstring name1,name2;
-       char name_type = 0;
+       char name_type1, name_type2;
 
        /*
         * We only really use 4 bytes of the outbuf, but for the smb_setlen
@@ -440,19 +534,23 @@ void reply_special(char *inbuf)
                        DEBUG(0,("Invalid name length in session request\n"));
                        return;
                }
-               name_extract(inbuf,4,name1);
-               name_type = name_extract(inbuf,4 + name_len(inbuf + 4),name2);
-               DEBUG(2,("netbios connect: name1=%s name2=%s\n",
-                        name1,name2));      
+               name_type1 = name_extract(inbuf,4,name1);
+               name_type2 = name_extract(inbuf,4 + name_len(inbuf + 4),name2);
+               DEBUG(2,("netbios connect: name1=%s0x%x name2=%s0x%x\n",
+                        name1, name_type1, name2, name_type2));
+
+               if (netbios_session_retarget(name1, name_type1)) {
+                       exit_server_cleanly("retargeted client");
+               }
 
                set_local_machine_name(name1, True);
                set_remote_machine_name(name2, True);
 
                DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
                         get_local_machine_name(), get_remote_machine_name(),
-                        name_type));
+                        name_type2));
 
-               if (name_type == 'R') {
+               if (name_type2 == 'R') {
                        /* We are being asked for a pathworks session --- 
                           no thanks! */
                        SCVAL(outbuf, 0,0x83);
@@ -492,7 +590,7 @@ void reply_special(char *inbuf)
        DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
                    msg_type, msg_flags));
 
-       srv_send_smb(smbd_server_fd(), outbuf, false, NULL);
+       srv_send_smb(smbd_server_fd(), outbuf, false, 0, false, NULL);
        return;
 }
 
@@ -1380,6 +1478,9 @@ void reply_search(struct smb_request *req)
 
        DEBUG(4,("dptr_num is %d\n",dptr_num));
 
+       /* Initialize per SMBsearch/SMBffirst/SMBfunique operation data */
+       dptr_init_search_op(conn->dirptr);
+
        if ((dirtype&0x1F) == aVOLID) {
                char buf[DIR_STRUCT_SIZE];
                memcpy(buf,status,21);
@@ -1598,6 +1699,8 @@ void reply_open(struct smb_request *req)
 
        START_PROFILE(SMBopen);
 
+       SET_STAT_INVALID(sbuf);
+
        if (req->wct < 2) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                END_PROFILE(SMBopen);
@@ -1733,6 +1836,8 @@ void reply_open_and_X(struct smb_request *req)
                return;
        }
 
+       SET_STAT_INVALID(sbuf);
+
        open_flags = SVAL(req->vwv+2, 0);
        deny_mode = SVAL(req->vwv+3, 0);
        smb_attr = SVAL(req->vwv+5, 0);
@@ -1937,6 +2042,7 @@ void reply_mknew(struct smb_request *req)
 
        START_PROFILE(SMBcreate);
        ZERO_STRUCT(ft);
+       SET_STAT_INVALID(sbuf);
 
         if (req->wct < 3) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -2108,13 +2214,14 @@ void reply_ctemp(struct smb_request *req)
                return;
        }
 
-       tmpfd = smb_mkstemp(fname);
+       tmpfd = mkstemp(fname);
        if (tmpfd == -1) {
                reply_unixerror(req, ERRDOS, ERRnoaccess);
                END_PROFILE(SMBctemp);
                return;
        }
 
+       SET_STAT_INVALID(sbuf);
        SMB_VFS_STAT(conn,fname,&sbuf);
 
        /* We should fail if file does not exist. */
@@ -2137,7 +2244,7 @@ void reply_ctemp(struct smb_request *req)
                NULL,                                   /* pinfo */
                &sbuf);                                 /* psbuf */
 
-       /* close fd from smb_mkstemp() */
+       /* close fd from mkstemp() */
        close(tmpfd);
 
        if (!NT_STATUS_IS_OK(status)) {
@@ -2211,6 +2318,16 @@ static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
        }
 
        if (S_ISDIR(pst->st_mode)) {
+               if (fsp->posix_open) {
+                       return NT_STATUS_OK;
+               }
+
+               /* If no pathnames are open below this
+                  directory, allow the rename. */
+
+               if (file_find_subpath(fsp)) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
                return NT_STATUS_OK;
        }
 
@@ -2655,6 +2772,7 @@ static ssize_t fake_sendfile(files_struct *fsp, SMB_OFF_T startpos,
        return (ssize_t)nread;
 }
 
+#if defined(WITH_SENDFILE)
 /****************************************************************************
  Deal with the case of sendfile reading less bytes from the file than
  requested. Fill with zeros (all we can do).
@@ -2711,6 +2829,7 @@ static void sendfile_short_send(files_struct *fsp,
                SAFE_FREE(buf);
        }
 }
+#endif /* defined WITH_SENDFILE */
 
 /****************************************************************************
  Return a readbraw error (4 bytes of zero).
@@ -2748,7 +2867,8 @@ static void send_file_readbraw(connection_struct *conn,
         */
 
        if ( !req_is_in_chain(req) && (nread > 0) && (fsp->base_fsp == NULL) &&
-           (fsp->wcp == NULL) && lp_use_sendfile(SNUM(conn)) ) {
+           (fsp->wcp == NULL) &&
+           lp_use_sendfile(SNUM(conn), smbd_server_conn->signing_state) ) {
                ssize_t sendfile_read = -1;
                char header[4];
                DATA_BLOB header_blob;
@@ -2785,6 +2905,18 @@ static void send_file_readbraw(connection_struct *conn,
                        DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
                                fsp->fsp_name, strerror(errno) ));
                        exit_server_cleanly("send_file_readbraw sendfile failed");
+               } else if (sendfile_read == 0) {
+                       /*
+                        * Some sendfile implementations return 0 to indicate
+                        * that there was a short read, but nothing was
+                        * actually written to the socket.  In this case,
+                        * fallback to the normal read path so the header gets
+                        * the correct byte count.
+                        */
+                       DEBUG(3, ("send_file_readbraw: sendfile sent zero "
+                                 "bytes falling back to the normal read: "
+                                 "%s\n", fsp->fsp_name));
+                       goto normal_readbraw;
                }
 
                /* Deal with possible short send. */
@@ -2793,9 +2925,9 @@ static void send_file_readbraw(connection_struct *conn,
                }
                return;
        }
-#endif
 
 normal_readbraw:
+#endif
 
        outbuf = TALLOC_ARRAY(NULL, char, nread+4);
        if (!outbuf) {
@@ -2834,12 +2966,14 @@ void reply_readbraw(struct smb_request *req)
        size_t nread = 0;
        SMB_OFF_T startpos;
        files_struct *fsp;
+       struct lock_struct lock;
        SMB_STRUCT_STAT st;
        SMB_OFF_T size = 0;
 
        START_PROFILE(SMBreadbraw);
 
-       if (srv_is_signing_active() || is_encrypted_packet(req->inbuf)) {
+       if (srv_is_signing_active(smbd_server_conn) ||
+           is_encrypted_packet(req->inbuf)) {
                exit_server_cleanly("reply_readbraw: SMB signing/sealing is active - "
                        "raw reads/writes are disallowed.");
        }
@@ -2934,10 +3068,11 @@ void reply_readbraw(struct smb_request *req)
        /* ensure we don't overrun the packet size */
        maxcount = MIN(65535,maxcount);
 
-       if (is_locked(fsp,(uint32)req->smbpid,
-                       (uint64_t)maxcount,
-                       (uint64_t)startpos,
-                       READ_LOCK)) {
+       init_strict_lock_struct(fsp, (uint32)req->smbpid,
+           (uint64_t)startpos, (uint64_t)maxcount, READ_LOCK,
+           &lock);
+
+       if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
                reply_readbraw_error();
                END_PROFILE(SMBreadbraw);
                return;
@@ -2968,7 +3103,11 @@ void reply_readbraw(struct smb_request *req)
        send_file_readbraw(conn, req, fsp, startpos, nread, mincount);
 
        DEBUG(5,("reply_readbraw finished\n"));
+
+       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+
        END_PROFILE(SMBreadbraw);
+       return;
 }
 
 #undef DBGC_CLASS
@@ -3037,6 +3176,7 @@ void reply_lockread(struct smb_request *req)
                        WINDOWS_LOCK,
                        False, /* Non-blocking lock. */
                        &status,
+                       NULL,
                        NULL);
        TALLOC_FREE(br_lck);
 
@@ -3095,6 +3235,7 @@ void reply_read(struct smb_request *req)
        SMB_OFF_T startpos;
        int outsize = 0;
        files_struct *fsp;
+       struct lock_struct lock;
 
        START_PROFILE(SMBread);
 
@@ -3136,8 +3277,11 @@ Returning short read of maximum allowed for compatibility with Windows 2000.\n",
 
        data = smb_buf(req->outbuf) + 3;
 
-       if (is_locked(fsp, (uint32)req->smbpid, (uint64_t)numtoread,
-                     (uint64_t)startpos, READ_LOCK)) {
+       init_strict_lock_struct(fsp, (uint32)req->smbpid,
+           (uint64_t)startpos, (uint64_t)numtoread, READ_LOCK,
+           &lock);
+
+       if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
                reply_doserror(req, ERRDOS,ERRlock);
                END_PROFILE(SMBread);
                return;
@@ -3148,8 +3292,7 @@ Returning short read of maximum allowed for compatibility with Windows 2000.\n",
 
        if (nread < 0) {
                reply_unixerror(req, ERRDOS,ERRnoaccess);
-               END_PROFILE(SMBread);
-               return;
+               goto strict_unlock;
        }
 
        srv_set_message((char *)req->outbuf, 5, nread+3, False);
@@ -3162,6 +3305,9 @@ Returning short read of maximum allowed for compatibility with Windows 2000.\n",
        DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
                fsp->fnum, (int)numtoread, (int)nread ) );
 
+strict_unlock:
+       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+
        END_PROFILE(SMBread);
        return;
 }
@@ -3206,20 +3352,29 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
 {
        SMB_STRUCT_STAT sbuf;
        ssize_t nread = -1;
+       struct lock_struct lock;
 
        if(SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
                reply_unixerror(req, ERRDOS, ERRnoaccess);
                return;
        }
 
-       if (startpos > sbuf.st_size) {
-               smb_maxcnt = 0;
-       } else if (smb_maxcnt > (sbuf.st_size - startpos)) {
-               smb_maxcnt = (sbuf.st_size - startpos);
+       init_strict_lock_struct(fsp, (uint32)req->smbpid,
+           (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
+           &lock);
+
+       if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+               reply_doserror(req, ERRDOS, ERRlock);
+               return;
        }
 
-       if (smb_maxcnt == 0) {
-               goto normal_read;
+       if (!S_ISREG(sbuf.st_mode) || (startpos > sbuf.st_size)
+           || (smb_maxcnt > (sbuf.st_size - startpos))) {
+               /*
+                * We already know that we would do a short read, so don't
+                * try the sendfile() path.
+                */
+               goto nosendfile_read;
        }
 
 #if defined(WITH_SENDFILE)
@@ -3231,7 +3386,8 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
 
        if (!req_is_in_chain(req) &&
            !is_encrypted_packet(req->inbuf) && (fsp->base_fsp == NULL) &&
-           lp_use_sendfile(SNUM(conn)) && (fsp->wcp == NULL) ) {
+           (fsp->wcp == NULL) &&
+           lp_use_sendfile(SNUM(conn), smbd_server_conn->signing_state) ) {
                uint8 headerbuf[smb_size + 12 * 2];
                DATA_BLOB header;
 
@@ -3273,13 +3429,24 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                                DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
                                        fsp->fnum, (int)smb_maxcnt, (int)nread ) );
                                /* No outbuf here means successful sendfile. */
-                               TALLOC_FREE(req->outbuf);
-                               return;
+                               goto strict_unlock;
                        }
 
                        DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
                                fsp->fsp_name, strerror(errno) ));
                        exit_server_cleanly("send_file_readX sendfile failed");
+               } else if (nread == 0) {
+                       /*
+                        * Some sendfile implementations return 0 to indicate
+                        * that there was a short read, but nothing was
+                        * actually written to the socket.  In this case,
+                        * fallback to the normal read path so the header gets
+                        * the correct byte count.
+                        */
+                       DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
+                                 "falling back to the normal read: %s\n",
+                                 fsp->fsp_name));
+                       goto normal_read;
                }
 
                DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
@@ -3289,17 +3456,16 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                if (nread != smb_maxcnt + sizeof(headerbuf)) {
                        sendfile_short_send(fsp, nread, sizeof(headerbuf), smb_maxcnt);
                }
-
                /* No outbuf here means successful sendfile. */
-               TALLOC_FREE(req->outbuf);
                SMB_PERFCOUNT_SET_MSGLEN_OUT(&req->pcd, nread);
                SMB_PERFCOUNT_END(&req->pcd);
-               return;
+               goto strict_unlock;
        }
-#endif
 
 normal_read:
 
+#endif
+
        if ((smb_maxcnt & 0xFF0000) > 0x10000) {
                uint8 headerbuf[smb_size + 2*12];
 
@@ -3319,13 +3485,17 @@ normal_read:
                                fsp->fsp_name, strerror(errno) ));
                        exit_server_cleanly("send_file_readX: fake_sendfile failed");
                }
-               TALLOC_FREE(req->outbuf);
-               return;
+               goto strict_unlock;
        }
 
+nosendfile_read:
+
        reply_outbuf(req, 12, smb_maxcnt);
 
        nread = read_file(fsp, smb_buf(req->outbuf), startpos, smb_maxcnt);
+
+       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+
        if (nread < 0) {
                reply_unixerror(req, ERRDOS, ERRnoaccess);
                return;
@@ -3337,6 +3507,12 @@ normal_read:
                    fsp->fnum, (int)smb_maxcnt, (int)nread ) );
 
        chain_reply(req);
+       return;
+
+ strict_unlock:
+       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+       TALLOC_FREE(req->outbuf);
+       return;
 }
 
 /****************************************************************************
@@ -3394,7 +3570,8 @@ void reply_read_and_X(struct smb_request *req)
                                return;
                        }
                        /* We currently don't do this on signed or sealed data. */
-                       if (srv_is_signing_active() || is_encrypted_packet(req->inbuf)) {
+                       if (srv_is_signing_active(smbd_server_conn) ||
+                           is_encrypted_packet(req->inbuf)) {
                                reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
                                END_PROFILE(SMBreadX);
                                return;
@@ -3436,21 +3613,14 @@ void reply_read_and_X(struct smb_request *req)
 
        }
 
-       if (is_locked(fsp, (uint32)req->smbpid, (uint64_t)smb_maxcnt,
-                     (uint64_t)startpos, READ_LOCK)) {
-               END_PROFILE(SMBreadX);
-               reply_doserror(req, ERRDOS, ERRlock);
-               return;
-       }
-
        if (!big_readX &&
            schedule_aio_read_and_X(conn, req, fsp, startpos, smb_maxcnt)) {
-               END_PROFILE(SMBreadX);
-               return;
+               goto out;
        }
 
        send_file_readX(conn, req, fsp, startpos, smb_maxcnt);
 
+ out:
        END_PROFILE(SMBreadX);
        return;
 }
@@ -3485,6 +3655,7 @@ void reply_writebraw(struct smb_request *req)
        char *data=NULL;
        bool write_through;
        files_struct *fsp;
+       struct lock_struct lock;
        NTSTATUS status;
 
        START_PROFILE(SMBwritebraw);
@@ -3496,7 +3667,7 @@ void reply_writebraw(struct smb_request *req)
         */
        SCVAL(req->inbuf,smb_com,SMBwritec);
 
-       if (srv_is_signing_active()) {
+       if (srv_is_signing_active(smbd_server_conn)) {
                END_PROFILE(SMBwritebraw);
                exit_server_cleanly("reply_writebraw: SMB signing is active - "
                                "raw reads/writes are disallowed.");
@@ -3546,8 +3717,11 @@ void reply_writebraw(struct smb_request *req)
                return;
        }
 
-       if (is_locked(fsp,(uint32)req->smbpid,(uint64_t)tcount,
-                               (uint64_t)startpos, WRITE_LOCK)) {
+       init_strict_lock_struct(fsp, (uint32)req->smbpid,
+           (uint64_t)startpos, (uint64_t)tcount, WRITE_LOCK,
+           &lock);
+
+       if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
                reply_doserror(req, ERRDOS, ERRlock);
                error_to_writebrawerr(req);
                END_PROFILE(SMBwritebraw);
@@ -3566,8 +3740,7 @@ void reply_writebraw(struct smb_request *req)
        if (nwritten < (ssize_t)numtowrite)  {
                reply_unixerror(req, ERRHRD, ERRdiskfull);
                error_to_writebrawerr(req);
-               END_PROFILE(SMBwritebraw);
-               return;
+               goto strict_unlock;
        }
 
        total_written = nwritten;
@@ -3577,8 +3750,7 @@ void reply_writebraw(struct smb_request *req)
        if (!buf) {
                reply_doserror(req, ERRDOS, ERRnomem);
                error_to_writebrawerr(req);
-               END_PROFILE(SMBwritebraw);
-               return;
+               goto strict_unlock;
        }
 
        /* Return a SMBwritebraw message to the redirector to tell
@@ -3590,9 +3762,10 @@ void reply_writebraw(struct smb_request *req)
        SSVALS(buf,smb_vwv0,0xFFFF);
        show_msg(buf);
        if (!srv_send_smb(smbd_server_fd(),
-                       buf,
-                       IS_CONN_ENCRYPTED(conn),
-                       &req->pcd)) {
+                         buf,
+                         false, 0, /* no signing */
+                         IS_CONN_ENCRYPTED(conn),
+                         &req->pcd)) {
                exit_server_cleanly("reply_writebraw: srv_send_smb "
                        "failed.");
        }
@@ -3636,8 +3809,7 @@ void reply_writebraw(struct smb_request *req)
                        TALLOC_FREE(buf);
                        reply_unixerror(req, ERRHRD, ERRdiskfull);
                        error_to_writebrawerr(req);
-                       END_PROFILE(SMBwritebraw);
-                       return;
+                       goto strict_unlock;
                }
 
                if (nwritten < (ssize_t)numtowrite) {
@@ -3659,8 +3831,7 @@ void reply_writebraw(struct smb_request *req)
                        fsp->fsp_name, nt_errstr(status) ));
                reply_nterror(req, status);
                error_to_writebrawerr(req);
-               END_PROFILE(SMBwritebraw);
-               return;
+               goto strict_unlock;
        }
 
        DEBUG(3,("reply_writebraw: secondart write fnum=%d start=%.0f num=%d "
@@ -3668,6 +3839,8 @@ void reply_writebraw(struct smb_request *req)
                fsp->fnum, (double)startpos, (int)numtowrite,
                (int)total_written));
 
+       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+
        /* We won't return a status if write through is not selected - this
         * follows what WfWg does */
        END_PROFILE(SMBwritebraw);
@@ -3688,6 +3861,12 @@ void reply_writebraw(struct smb_request *req)
                TALLOC_FREE(req->outbuf);
        }
        return;
+
+strict_unlock:
+       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+
+       END_PROFILE(SMBwritebraw);
+       return;
 }
 
 #undef DBGC_CLASS
@@ -3706,6 +3885,7 @@ void reply_writeunlock(struct smb_request *req)
        const char *data;
        NTSTATUS status = NT_STATUS_OK;
        files_struct *fsp;
+       struct lock_struct lock;
 
        START_PROFILE(SMBwriteunlock);
 
@@ -3732,12 +3912,16 @@ void reply_writeunlock(struct smb_request *req)
        startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
        data = (const char *)req->buf + 3;
 
-       if (numtowrite
-           && is_locked(fsp, (uint32)req->smbpid, (uint64_t)numtowrite,
-                        (uint64_t)startpos, WRITE_LOCK)) {
-               reply_doserror(req, ERRDOS, ERRlock);
-               END_PROFILE(SMBwriteunlock);
-               return;
+       if (numtowrite) {
+               init_strict_lock_struct(fsp, (uint32)req->smbpid,
+                   (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
+                   &lock);
+
+               if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+                       reply_doserror(req, ERRDOS, ERRlock);
+                       END_PROFILE(SMBwriteunlock);
+                       return;
+               }
        }
 
        /* The special X/Open SMB protocol handling of
@@ -3754,14 +3938,12 @@ void reply_writeunlock(struct smb_request *req)
                DEBUG(5,("reply_writeunlock: sync_file for %s returned %s\n",
                        fsp->fsp_name, nt_errstr(status) ));
                reply_nterror(req, status);
-               END_PROFILE(SMBwriteunlock);
-               return;
+               goto strict_unlock;
        }
 
        if(((nwritten < numtowrite) && (numtowrite != 0))||(nwritten < 0)) {
                reply_unixerror(req, ERRHRD, ERRdiskfull);
-               END_PROFILE(SMBwriteunlock);
-               return;
+               goto strict_unlock;
        }
 
        if (numtowrite) {
@@ -3774,8 +3956,7 @@ void reply_writeunlock(struct smb_request *req)
 
                if (NT_STATUS_V(status)) {
                        reply_nterror(req, status);
-                       END_PROFILE(SMBwriteunlock);
-                       return;
+                       goto strict_unlock;
                }
        }
 
@@ -3786,6 +3967,11 @@ void reply_writeunlock(struct smb_request *req)
        DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
                 fsp->fnum, (int)numtowrite, (int)nwritten));
 
+strict_unlock:
+       if (numtowrite) {
+               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+       }
+
        END_PROFILE(SMBwriteunlock);
        return;
 }
@@ -3805,6 +3991,7 @@ void reply_write(struct smb_request *req)
        SMB_OFF_T startpos;
        const char *data;
        files_struct *fsp;
+       struct lock_struct lock;
        NTSTATUS status;
 
        START_PROFILE(SMBwrite);
@@ -3839,8 +4026,11 @@ void reply_write(struct smb_request *req)
        startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
        data = (const char *)req->buf + 3;
 
-       if (is_locked(fsp, (uint32)req->smbpid, (uint64_t)numtowrite,
-                     (uint64_t)startpos, WRITE_LOCK)) {
+       init_strict_lock_struct(fsp, (uint32)req->smbpid,
+           (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
+           &lock);
+
+       if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
                reply_doserror(req, ERRDOS, ERRlock);
                END_PROFILE(SMBwrite);
                return;
@@ -3859,14 +4049,12 @@ void reply_write(struct smb_request *req)
                nwritten = vfs_allocate_file_space(fsp, (SMB_OFF_T)startpos);
                if (nwritten < 0) {
                        reply_nterror(req, NT_STATUS_DISK_FULL);
-                       END_PROFILE(SMBwrite);
-                       return;
+                       goto strict_unlock;
                }
                nwritten = vfs_set_filelen(fsp, (SMB_OFF_T)startpos);
                if (nwritten < 0) {
                        reply_nterror(req, NT_STATUS_DISK_FULL);
-                       END_PROFILE(SMBwrite);
-                       return;
+                       goto strict_unlock;
                }
                trigger_write_time_update_immediate(fsp);
        } else {
@@ -3878,14 +4066,12 @@ void reply_write(struct smb_request *req)
                DEBUG(5,("reply_write: sync_file for %s returned %s\n",
                        fsp->fsp_name, nt_errstr(status) ));
                reply_nterror(req, status);
-               END_PROFILE(SMBwrite);
-               return;
+               goto strict_unlock;
        }
 
        if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
                reply_unixerror(req, ERRHRD, ERRdiskfull);
-               END_PROFILE(SMBwrite);
-               return;
+               goto strict_unlock;
        }
 
        reply_outbuf(req, 1, 0);
@@ -3899,6 +4085,9 @@ void reply_write(struct smb_request *req)
 
        DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten));
 
+strict_unlock:
+       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+
        END_PROFILE(SMBwrite);
        return;
 }
@@ -3996,6 +4185,7 @@ void reply_write_and_X(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
        files_struct *fsp;
+       struct lock_struct lock;
        SMB_OFF_T startpos;
        size_t numtowrite;
        bool write_through;
@@ -4098,9 +4288,11 @@ void reply_write_and_X(struct smb_request *req)
 #endif /* LARGE_SMB_OFF_T */
        }
 
-       if (is_locked(fsp,(uint32)req->smbpid,
-                     (uint64_t)numtowrite,
-                     (uint64_t)startpos, WRITE_LOCK)) {
+       init_strict_lock_struct(fsp, (uint32)req->smbpid,
+           (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
+           &lock);
+
+       if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
                reply_doserror(req, ERRDOS, ERRlock);
                END_PROFILE(SMBwriteX);
                return;
@@ -4118,8 +4310,7 @@ void reply_write_and_X(struct smb_request *req)
                if ((req->unread_bytes == 0) &&
                    schedule_aio_write_and_X(conn, req, fsp, data, startpos,
                                             numtowrite)) {
-                       END_PROFILE(SMBwriteX);
-                       return;
+                       goto strict_unlock;
                }
 
                nwritten = write_file(req,fsp,data,startpos,numtowrite);
@@ -4127,8 +4318,7 @@ void reply_write_and_X(struct smb_request *req)
 
        if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
                reply_unixerror(req, ERRHRD, ERRdiskfull);
-               END_PROFILE(SMBwriteX);
-               return;
+               goto strict_unlock;
        }
 
        reply_outbuf(req, 6, 0);
@@ -4148,13 +4338,20 @@ void reply_write_and_X(struct smb_request *req)
                DEBUG(5,("reply_write_and_X: sync_file for %s returned %s\n",
                        fsp->fsp_name, nt_errstr(status) ));
                reply_nterror(req, status);
-               END_PROFILE(SMBwriteX);
-               return;
+               goto strict_unlock;
        }
 
+       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+
        END_PROFILE(SMBwriteX);
        chain_reply(req);
        return;
+
+strict_unlock:
+       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+
+       END_PROFILE(SMBwriteX);
+       return;
 }
 
 /****************************************************************************
@@ -4394,6 +4591,7 @@ void reply_writeclose(struct smb_request *req)
        const char *data;
        struct timespec mtime;
        files_struct *fsp;
+       struct lock_struct lock;
 
        START_PROFILE(SMBwriteclose);
 
@@ -4420,12 +4618,16 @@ void reply_writeclose(struct smb_request *req)
        mtime = convert_time_t_to_timespec(srv_make_unix_date3(req->vwv+4));
        data = (const char *)req->buf + 1;
 
-       if (numtowrite
-           && is_locked(fsp, (uint32)req->smbpid, (uint64_t)numtowrite,
-                        (uint64_t)startpos, WRITE_LOCK)) {
-               reply_doserror(req, ERRDOS,ERRlock);
-               END_PROFILE(SMBwriteclose);
-               return;
+       if (numtowrite) {
+               init_strict_lock_struct(fsp, (uint32)req->smbpid,
+                   (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
+                   &lock);
+
+               if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+                       reply_doserror(req, ERRDOS,ERRlock);
+                       END_PROFILE(SMBwriteclose);
+                       return;
+               }
        }
 
        nwritten = write_file(req,fsp,data,startpos,numtowrite);
@@ -4449,19 +4651,23 @@ void reply_writeclose(struct smb_request *req)
 
        if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
                reply_doserror(req, ERRHRD, ERRdiskfull);
-               END_PROFILE(SMBwriteclose);
-               return;
+               goto strict_unlock;
        }
 
        if(!NT_STATUS_IS_OK(close_status)) {
                reply_nterror(req, close_status);
-               END_PROFILE(SMBwriteclose);
-               return;
+               goto strict_unlock;
        }
 
        reply_outbuf(req, 1, 0);
 
        SSVAL(req->outbuf,smb_vwv0,nwritten);
+
+strict_unlock:
+       if (numtowrite) {
+               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+       }
+
        END_PROFILE(SMBwriteclose);
        return;
 }
@@ -4511,6 +4717,7 @@ void reply_lock(struct smb_request *req)
                        WINDOWS_LOCK,
                        False, /* Non-blocking lock. */
                        &status,
+                       NULL,
                        NULL);
 
        TALLOC_FREE(br_lck);
@@ -4616,11 +4823,15 @@ void reply_tdis(struct smb_request *req)
 void reply_echo(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
+       struct smb_perfcount_data local_pcd;
+       struct smb_perfcount_data *cur_pcd;
        int smb_reverb;
        int seq_num;
 
        START_PROFILE(SMBecho);
 
+       smb_init_perfcount_data(&local_pcd);
+
        if (req->wct < 1) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                END_PROFILE(SMBecho);
@@ -4641,14 +4852,24 @@ void reply_echo(struct smb_request *req)
                smb_reverb = 100;
        }
 
-       for (seq_num =1 ; seq_num <= smb_reverb ; seq_num++) {
+       for (seq_num = 1 ; seq_num <= smb_reverb ; seq_num++) {
+
+               /* this makes sure we catch the request pcd */
+               if (seq_num == smb_reverb) {
+                       cur_pcd = &req->pcd;
+               } else {
+                       SMB_PERFCOUNT_COPY_CONTEXT(&req->pcd, &local_pcd);
+                       cur_pcd = &local_pcd;
+               }
+
                SSVAL(req->outbuf,smb_vwv0,seq_num);
 
                show_msg((char *)req->outbuf);
                if (!srv_send_smb(smbd_server_fd(),
                                (char *)req->outbuf,
+                               true, req->seqnum+1,
                                IS_CONN_ENCRYPTED(conn)||req->encrypted,
-                               &req->pcd))
+                               cur_pcd))
                        exit_server_cleanly("reply_echo: srv_send_smb failed.");
        }
 
@@ -5797,8 +6018,6 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                /*
                 * No wildcards - just process the one file.
                 */
-               bool is_short_name = mangle_is_8_3(name, True, conn->params);
-
                /* Add a terminating '/' to the directory name. */
                directory = talloc_asprintf_append(directory,
                                "/%s",
@@ -5820,10 +6039,10 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                DEBUG(3, ("rename_internals: case_sensitive = %d, "
                          "case_preserve = %d, short case preserve = %d, "
                          "directory = %s, newname = %s, "
-                         "last_component_dest = %s, is_8_3 = %d\n",
+                         "last_component_dest = %s\n",
                          conn->case_sensitive, conn->case_preserve,
                          conn->short_case_preserve, directory,
-                         newname, last_component_dest, is_short_name));
+                         newname, last_component_dest));
 
                /* The dest name still may have wildcards. */
                if (dest_has_wild) {
@@ -6901,6 +7120,9 @@ void reply_lockingX(struct smb_request *req)
                                offset,
                                WINDOWS_LOCK);
 
+               DEBUG(10, ("reply_lockingX: unlock returned %s\n",
+                   nt_errstr(status)));
+
                if (NT_STATUS_V(status)) {
                        END_PROFILE(SMBlockingX);
                        reply_nterror(req, status);
@@ -6942,19 +7164,22 @@ void reply_lockingX(struct smb_request *req)
                          fsp->fsp_name, (int)lock_timeout ));
 
                if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
+                       struct blocking_lock_record *blr = NULL;
+
                        if (lp_blocking_locks(SNUM(conn))) {
 
                                /* Schedule a message to ourselves to
                                   remove the blocking lock record and
                                   return the right error. */
 
-                               if (!blocking_lock_cancel(fsp,
+                               blr = blocking_lock_cancel(fsp,
                                                lock_pid,
                                                offset,
                                                count,
                                                WINDOWS_LOCK,
                                                locktype,
-                                               NT_STATUS_FILE_LOCK_CONFLICT)) {
+                                               NT_STATUS_FILE_LOCK_CONFLICT);
+                               if (blr == NULL) {
                                        END_PROFILE(SMBlockingX);
                                        reply_nterror(
                                                req,
@@ -6969,7 +7194,8 @@ void reply_lockingX(struct smb_request *req)
                                                lock_pid,
                                                count,
                                                offset,
-                                               WINDOWS_LOCK);
+                                               WINDOWS_LOCK,
+                                               blr);
                } else {
                        bool blocking_lock = lock_timeout ? True : False;
                        bool defer_lock = False;
@@ -6985,7 +7211,8 @@ void reply_lockingX(struct smb_request *req)
                                        WINDOWS_LOCK,
                                        blocking_lock,
                                        &status,
-                                       &block_smbpid);
+                                       &block_smbpid,
+                                       NULL);
 
                        if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
                                /* Windows internal resolution for blocking locks seems