r16945: Sync trunk -> 3.0 for 3.0.24 code. Still need
[vlendec/samba-autobuild/.git] / source3 / smbd / reply.c
index 83f527d326c635022c436675347bab48bb639397..ff3c6832e4f33c1cd1eb047e8285166858b5dabf 100644 (file)
@@ -269,10 +269,13 @@ NTSTATUS check_path_syntax_wcard(pstring destname, const pstring srcname, BOOL *
                        switch(next_mb_char_size(s)) {
                                case 4:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 3:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 2:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 1:
                                        *d++ = *s++;
                                        break;
@@ -374,10 +377,13 @@ NTSTATUS check_path_syntax_posix(pstring destname, const pstring srcname)
                        switch(next_mb_char_size(s)) {
                                case 4:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 3:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 2:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 1:
                                        *d++ = *s++;
                                        break;
@@ -421,6 +427,7 @@ size_t srvstr_get_path_wcard(char *inbuf, char *dest, const char *src, size_t de
        } else {
                *err = check_path_syntax_wcard(dest, tmppath, contains_wcard);
        }
+
        return ret;
 }
 
@@ -447,6 +454,7 @@ size_t srvstr_get_path(char *inbuf, char *dest, const char *src, size_t dest_len
        } else {
                *err = check_path_syntax(dest, tmppath);
        }
+
        return ret;
 }
 
@@ -541,6 +549,7 @@ int reply_special(char *inbuf,char *outbuf)
 
 /****************************************************************************
  Reply to a tcon.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_tcon(connection_struct *conn,
@@ -599,6 +608,7 @@ int reply_tcon(connection_struct *conn,
 
 /****************************************************************************
  Reply to a tcon and X.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_tcon_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
@@ -732,6 +742,7 @@ int reply_unknown(char *inbuf,char *outbuf)
 
 /****************************************************************************
  Reply to an ioctl.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_ioctl(connection_struct *conn,
@@ -800,6 +811,14 @@ int reply_chkpth(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        srvstr_get_path(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), 0, STR_TERMINATE, &status);
        if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBchkpth);
+
+               /* Strange DOS error code semantics only for chkpth... */
+               if (!(SVAL(inbuf,smb_flg2) & FLAGS2_32_BIT_ERROR_CODES)) {
+                       if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_INVALID,status)) {
+                               /* We need to map to ERRbadpath */
+                               status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
+                       }
+               }
                return ERROR_NT(status);
        }
 
@@ -842,7 +861,7 @@ int reply_chkpth(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
                return(UNIXERROR(ERRDOS,ERRbadpath));
        }
 
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
        DEBUG(3,("chkpth %s mode=%d\n", name, (int)SVAL(inbuf,smb_vwv0)));
 
        END_PROFILE(SMBchkpth);
@@ -988,7 +1007,7 @@ int reply_setatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
                return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
        }
  
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
   
        DEBUG( 3, ( "setatr name=%s mode=%d\n", fname, mode ) );
   
@@ -1079,12 +1098,13 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        BOOL mask_contains_wcard = False;
        BOOL allow_long_path_components = (SVAL(inbuf,smb_flg2) & FLAGS2_LONG_PATH_COMPONENTS) ? True : False;
 
+       START_PROFILE(SMBsearch);
+
        if (lp_posix_pathnames()) {
+               END_PROFILE(SMBsearch);
                return reply_unknown(inbuf, outbuf);
        }
 
-       START_PROFILE(SMBsearch);
-
        *mask = *directory = *fname = 0;
 
        /* If we were called as SMBffirst then we must expect close. */
@@ -1278,12 +1298,13 @@ int reply_fclose(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        NTSTATUS err;
        BOOL path_contains_wcard = False;
 
+       START_PROFILE(SMBfclose);
+
        if (lp_posix_pathnames()) {
+               END_PROFILE(SMBfclose);
                return reply_unknown(inbuf, outbuf);
        }
 
-       START_PROFILE(SMBfclose);
-
        outsize = set_message(outbuf,1,0,True);
        p = smb_buf(inbuf) + 1;
        p += srvstr_get_path_wcard(inbuf, path, p, sizeof(path), 0, STR_TERMINATE, &err, &path_contains_wcard);
@@ -1359,25 +1380,25 @@ int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        if (!map_open_params_to_ntcreate(fname, deny_mode, OPENX_FILE_EXISTS_OPEN,
                        &access_mask, &share_mode, &create_disposition, &create_options)) {
                END_PROFILE(SMBopen);
-               return ERROR_FORCE_DOS(ERRDOS, ERRbadaccess);
+               return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRbadaccess));
        }
 
-       fsp = open_file_ntcreate(conn,fname,&sbuf,
+       status = open_file_ntcreate(conn,fname,&sbuf,
                        access_mask,
                        share_mode,
                        create_disposition,
                        create_options,
                        dos_attr,
                        oplock_request,
-                       &info);
+                       &info, &fsp);
 
-       if (!fsp) {
+       if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBopen);
                if (open_was_deferred(SVAL(inbuf,smb_mid))) {
                        /* We have re-scheduled this call. */
                        return -1;
                }
-               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
+               return ERROR_NT(status);
        }
 
        size = sbuf.st_size;
@@ -1425,9 +1446,9 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
        uint32 smb_attr = SVAL(inbuf,smb_vwv5);
        /* Breakout the oplock request bits so we can set the
                reply bits separately. */
-       BOOL ex_oplock_request = EXTENDED_OPLOCK_REQUEST(inbuf);
-       BOOL core_oplock_request = CORE_OPLOCK_REQUEST(inbuf);
-       BOOL oplock_request = ex_oplock_request | core_oplock_request;
+       int ex_oplock_request = EXTENDED_OPLOCK_REQUEST(inbuf);
+       int core_oplock_request = CORE_OPLOCK_REQUEST(inbuf);
+       int oplock_request = ex_oplock_request | core_oplock_request;
 #if 0
        int smb_sattr = SVAL(inbuf,smb_vwv4); 
        uint32 smb_time = make_unix_date3(inbuf+smb_vwv6);
@@ -1482,25 +1503,25 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
                                &create_disposition,
                                &create_options)) {
                END_PROFILE(SMBopenX);
-               return ERROR_FORCE_DOS(ERRDOS, ERRbadaccess);
+               return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRbadaccess));
        }
 
-       fsp = open_file_ntcreate(conn,fname,&sbuf,
+       status = open_file_ntcreate(conn,fname,&sbuf,
                        access_mask,
                        share_mode,
                        create_disposition,
                        create_options,
                        smb_attr,
                        oplock_request,
-                       &smb_action);
+                       &smb_action, &fsp);
       
-       if (!fsp) {
+       if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBopenX);
                if (open_was_deferred(SVAL(inbuf,smb_mid))) {
                        /* We have re-scheduled this call. */
                        return -1;
                }
-               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
+               return ERROR_NT(status);
        }
 
        size = sbuf.st_size;
@@ -1511,13 +1532,13 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
                fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
                if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
                        close_file(fsp,ERROR_CLOSE);
-                       END_PROFILE(SMBntcreateX);
+                       END_PROFILE(SMBopenX);
                        return ERROR_NT(NT_STATUS_DISK_FULL);
                }
                retval = vfs_set_filelen(fsp, (SMB_OFF_T)allocation_size);
                if (retval < 0) {
                        close_file(fsp,ERROR_CLOSE);
-                       END_PROFILE(SMBwrite);
+                       END_PROFILE(SMBopenX);
                        return ERROR_NT(NT_STATUS_DISK_FULL);
                }
                size = get_allocation_size(conn,fsp,&sbuf);
@@ -1583,6 +1604,7 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
 
 /****************************************************************************
  Reply to a SMBulogoffX.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_ulogoffX(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
@@ -1660,22 +1682,22 @@ int reply_mknew(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        }
 
        /* Open file using ntcreate. */
-       fsp = open_file_ntcreate(conn,fname,&sbuf,
+       status = open_file_ntcreate(conn,fname,&sbuf,
                                access_mask,
                                share_mode,
                                create_disposition,
                                create_options,
                                fattr,
                                oplock_request,
-                               NULL);
+                               NULL, &fsp);
   
-       if (!fsp) {
+       if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBcreate);
                if (open_was_deferred(SVAL(inbuf,smb_mid))) {
                        /* We have re-scheduled this call. */
                        return -1;
                }
-               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
+               return ERROR_NT(status);
        }
  
        outsize = set_message(outbuf,1,0,True);
@@ -1744,25 +1766,25 @@ int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        SMB_VFS_STAT(conn,fname,&sbuf);
 
        /* We should fail if file does not exist. */
-       fsp = open_file_ntcreate(conn,fname,&sbuf,
+       status = open_file_ntcreate(conn,fname,&sbuf,
                                FILE_GENERIC_READ | FILE_GENERIC_WRITE,
                                FILE_SHARE_READ|FILE_SHARE_WRITE,
                                FILE_OPEN,
                                0,
                                fattr,
                                oplock_request,
-                               NULL);
+                               NULL, &fsp);
 
        /* close fd from smb_mkstemp() */
        close(tmpfd);
 
-       if (!fsp) {
+       if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBctemp);
                if (open_was_deferred(SVAL(inbuf,smb_mid))) {
                        /* We have re-scheduled this call. */
                        return -1;
                }
-               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
+               return ERROR_NT(status);
        }
 
        outsize = set_message(outbuf,1,0,True);
@@ -1810,6 +1832,7 @@ static NTSTATUS can_rename(connection_struct *conn, char *fname, uint16 dirtype,
 {
        files_struct *fsp;
        uint32 fmode;
+       NTSTATUS status;
 
        if (!CAN_WRITE(conn)) {
                return NT_STATUS_MEDIA_WRITE_PROTECTED;
@@ -1824,25 +1847,16 @@ static NTSTATUS can_rename(connection_struct *conn, char *fname, uint16 dirtype,
                return NT_STATUS_OK;
        }
 
-       /* We need a better way to return NT status codes from open... */
-       set_saved_ntstatus(NT_STATUS_OK);
-
-       fsp = open_file_ntcreate(conn, fname, pst,
+       status = open_file_ntcreate(conn, fname, pst,
                                DELETE_ACCESS,
                                FILE_SHARE_READ|FILE_SHARE_WRITE,
                                FILE_OPEN,
                                0,
                                FILE_ATTRIBUTE_NORMAL,
                                0,
-                               NULL);
+                               NULL, &fsp);
 
-       if (!fsp) {
-               NTSTATUS ret = get_saved_ntstatus();
-               if (!NT_STATUS_IS_OK(ret)) {
-                       set_saved_ntstatus(NT_STATUS_OK);
-                       return ret;
-               }
-               set_saved_ntstatus(NT_STATUS_OK);
+       if (!NT_STATUS_IS_OK(status)) {
                return NT_STATUS_ACCESS_DENIED;
        }
        close_file(fsp,NORMAL_CLOSE);
@@ -1858,6 +1872,7 @@ NTSTATUS can_delete(connection_struct *conn, char *fname, uint32 dirtype, BOOL b
        SMB_STRUCT_STAT sbuf;
        uint32 fattr;
        files_struct *fsp;
+       NTSTATUS status;
 
        DEBUG(10,("can_delete: %s, dirtype = %d\n", fname, dirtype ));
 
@@ -1917,26 +1932,17 @@ NTSTATUS can_delete(connection_struct *conn, char *fname, uint32 dirtype, BOOL b
                /* On open checks the open itself will check the share mode, so
                   don't do it here as we'll get it wrong. */
 
-               /* We need a better way to return NT status codes from open... */
-               set_saved_ntstatus(NT_STATUS_OK);
-
-               fsp = open_file_ntcreate(conn, fname, &sbuf,
+               status = open_file_ntcreate(conn, fname, &sbuf,
                                        DELETE_ACCESS,
                                        FILE_SHARE_NONE,
                                        FILE_OPEN,
                                        0,
                                        FILE_ATTRIBUTE_NORMAL,
                                        0,
-                                       NULL);
+                                       NULL, &fsp);
 
-               if (!fsp) {
-                       NTSTATUS ret = get_saved_ntstatus();
-                       if (!NT_STATUS_IS_OK(ret)) {
-                               set_saved_ntstatus(NT_STATUS_OK);
-                               return ret;
-                       }
-                       set_saved_ntstatus(NT_STATUS_OK);
-                       return NT_STATUS_ACCESS_DENIED;
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
                }
                close_file(fsp,NORMAL_CLOSE);
        }
@@ -1982,8 +1988,8 @@ NTSTATUS unlink_internals(connection_struct *conn, uint32 dirtype, char *name, B
         * Tine Smukavec <valentin.smukavec@hermes.si>.
         */
        
-       if (!rc && mangle_is_mangled(mask,SNUM(conn)))
-               mangle_check_cache( mask, sizeof(pstring)-1, SNUM(conn));
+       if (!rc && mangle_is_mangled(mask,conn->params))
+               mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
        
        if (!has_wild) {
                pstrcat(directory,"/");
@@ -2107,7 +2113,7 @@ int reply_unlink(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
         */
        process_pending_change_notify_queue((time_t)0);
        
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
   
        END_PROFILE(SMBunlink);
        return outsize;
@@ -2176,7 +2182,8 @@ void send_file_readbraw(connection_struct *conn, files_struct *fsp, SMB_OFF_T st
         * reply_readbraw has already checked the length.
         */
 
-       if (chain_size ==0 && (nread > 0) && (lp_write_cache_size(SNUM(conn)) == 0) && lp_use_sendfile(SNUM(conn)) ) {
+       if ( (chain_size == 0) && (nread > 0) &&
+           (fsp->wcp == NULL) && lp_use_sendfile(SNUM(conn)) ) {
                DATA_BLOB header;
 
                _smb_setlen(outbuf,nread);
@@ -2318,7 +2325,7 @@ int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_s
        /* ensure we don't overrun the packet size */
        maxcount = MIN(65535,maxcount);
 
-       if (!is_locked(fsp,conn,(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
+       if (!is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
                SMB_STRUCT_STAT st;
                SMB_OFF_T size = 0;
   
@@ -2389,8 +2396,13 @@ int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length
         * Note that the requested lock size is unaffected by max_recv.
         */
        
-       status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), 
-                        (SMB_BIG_UINT)numtoread, (SMB_BIG_UINT)startpos, WRITE_LOCK, &my_lock_ctx);
+       status = do_lock_spin(fsp,
+                               (uint32)SVAL(inbuf,smb_pid), 
+                               (SMB_BIG_UINT)numtoread,
+                               (SMB_BIG_UINT)startpos,
+                               WRITE_LOCK,
+                               WINDOWS_LOCK,
+                               &my_lock_ctx);
 
        if (NT_STATUS_V(status)) {
 #if 0
@@ -2406,8 +2418,15 @@ int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length
                         * this smb into a queued request and push it
                         * onto the blocking lock queue.
                         */
-                       if(push_blocking_lock_request(inbuf, length, -1, 0, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)startpos,
-                                                               (SMB_BIG_UINT)numtoread)) {
+                       if(push_blocking_lock_request(inbuf, length,
+                                       fsp,
+                                       -1,
+                                       0,
+                                       SVAL(inbuf,smb_pid),
+                                       WRITE_LOCK,
+                                       WINDOWS_LOCK,
+                                       (SMB_BIG_UINT)startpos,
+                                       (SMB_BIG_UINT)numtoread)) {
                                END_PROFILE(SMBlockread);
                                return -1;
                        }
@@ -2485,7 +2504,7 @@ Returning short read of maximum allowed for compatibility with Windows 2000.\n",
 
        data = smb_buf(outbuf) + 3;
   
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)numtoread,(SMB_BIG_UINT)startpos, READ_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtoread,(SMB_BIG_UINT)startpos, READ_LOCK)) {
                END_PROFILE(SMBread);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -2529,8 +2548,8 @@ int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length
         * on a train in Germany :-). JRA.
         */
 
-       if (chain_size ==0 && (CVAL(inbuf,smb_vwv0) == 0xFF) && lp_use_sendfile(SNUM(conn)) &&
-                       (lp_write_cache_size(SNUM(conn)) == 0) ) {
+       if ((chain_size == 0) && (CVAL(inbuf,smb_vwv0) == 0xFF) &&
+           lp_use_sendfile(SNUM(conn)) && (fsp->wcp == NULL) ) {
                SMB_STRUCT_STAT sbuf;
                DATA_BLOB header;
 
@@ -2610,7 +2629,6 @@ int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length
        nread = read_file(fsp,data,startpos,smb_maxcnt);
   
        if (nread < 0) {
-               END_PROFILE(SMBreadX);
                return(UNIXERROR(ERRDOS,ERRnoaccess));
        }
 
@@ -2693,7 +2711,7 @@ int reply_read_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
 
        }
 
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)smb_maxcnt,(SMB_BIG_UINT)startpos, READ_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)smb_maxcnt,(SMB_BIG_UINT)startpos, READ_LOCK)) {
                END_PROFILE(SMBreadX);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -2756,7 +2774,7 @@ int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size,
        SCVAL(inbuf,smb_com,SMBwritec);
        SCVAL(outbuf,smb_com,SMBwritec);
 
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
                END_PROFILE(SMBwritebraw);
                return(ERROR_DOS(ERRDOS,ERRlock));
        }
@@ -2877,7 +2895,7 @@ int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
        startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
        data = smb_buf(inbuf) + 3;
   
-       if (numtowrite && is_locked(fsp,conn,(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
+       if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
                END_PROFILE(SMBwriteunlock);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -2899,8 +2917,12 @@ int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
        }
 
        if (numtowrite) {
-               status = do_unlock(fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)numtowrite, 
-                                  (SMB_BIG_UINT)startpos);
+               status = do_unlock(fsp,
+                               (uint32)SVAL(inbuf,smb_pid),
+                               (SMB_BIG_UINT)numtowrite, 
+                               (SMB_BIG_UINT)startpos,
+                               WINDOWS_LOCK);
+
                if (NT_STATUS_V(status)) {
                        END_PROFILE(SMBwriteunlock);
                        return ERROR_NT(status);
@@ -2950,7 +2972,7 @@ int reply_write(connection_struct *conn, char *inbuf,char *outbuf,int size,int d
        startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
        data = smb_buf(inbuf) + 3;
   
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
                END_PROFILE(SMBwrite);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -3065,7 +3087,7 @@ int reply_write_and_X(connection_struct *conn, char *inbuf,char *outbuf,int leng
 #endif /* LARGE_SMB_OFF_T */
        }
 
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
                END_PROFILE(SMBwriteX);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -3191,7 +3213,7 @@ int reply_lseek(connection_struct *conn, char *inbuf,char *outbuf, int size, int
 
 int reply_flush(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
 {
-       int outsize = set_message(outbuf,0,0,True);
+       int outsize = set_message(outbuf,0,0,False);
        uint16 fnum = SVAL(inbuf,smb_vwv0);
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
        START_PROFILE(SMBflush);
@@ -3212,6 +3234,7 @@ int reply_flush(connection_struct *conn, char *inbuf,char *outbuf, int size, int
 
 /****************************************************************************
  Reply to a exit.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_exit(connection_struct *conn, 
@@ -3222,7 +3245,7 @@ int reply_exit(connection_struct *conn,
 
        file_close_pid(SVAL(inbuf,smb_pid),SVAL(inbuf,smb_uid));
 
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
 
        DEBUG(3,("exit\n"));
 
@@ -3243,7 +3266,7 @@ int reply_close(connection_struct *conn, char *inbuf,char *outbuf, int size,
        files_struct *fsp = NULL;
        START_PROFILE(SMBclose);
 
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
 
        /* If it's an IPC, pass off to the pipe handler. */
        if (IS_IPC(conn)) {
@@ -3339,7 +3362,7 @@ int reply_writeclose(connection_struct *conn,
        mtime = srv_make_unix_date3(inbuf+smb_vwv4);
        data = smb_buf(inbuf) + 1;
   
-       if (numtowrite && is_locked(fsp,conn,(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
+       if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
                END_PROFILE(SMBwriteclose);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -3391,7 +3414,7 @@ int reply_writeclose(connection_struct *conn,
 int reply_lock(connection_struct *conn,
               char *inbuf,char *outbuf, int length, int dum_buffsize)
 {
-       int outsize = set_message(outbuf,0,0,True);
+       int outsize = set_message(outbuf,0,0,False);
        SMB_BIG_UINT count,offset;
        NTSTATUS status;
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
@@ -3409,7 +3432,13 @@ int reply_lock(connection_struct *conn,
        DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
                 fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
 
-       status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), count, offset, WRITE_LOCK, &my_lock_ctx);
+       status = do_lock_spin(fsp,
+                               (uint32)SVAL(inbuf,smb_pid),
+                               count,
+                               offset,
+                               WRITE_LOCK,
+                               WINDOWS_LOCK,
+                               &my_lock_ctx);
        if (NT_STATUS_V(status)) {
 #if 0
                /* Tests using Samba4 against W2K show this call never creates a blocking lock. */
@@ -3419,7 +3448,14 @@ int reply_lock(connection_struct *conn,
                         * this smb into a queued request and push it
                         * onto the blocking lock queue.
                         */
-                       if(push_blocking_lock_request(inbuf, length, -1, 0, SVAL(inbuf,smb_pid), offset, count)) {
+                       if(push_blocking_lock_request(inbuf, length,
+                               fsp,
+                               -1,
+                               0,
+                               SVAL(inbuf,smb_pid),
+                               WRITE_LOCK,
+                               WINDOWS_LOCK,
+                               offset, count)) {
                                END_PROFILE(SMBlock);
                                return -1;
                        }
@@ -3440,7 +3476,7 @@ int reply_lock(connection_struct *conn,
 int reply_unlock(connection_struct *conn, char *inbuf,char *outbuf, int size, 
                 int dum_buffsize)
 {
-       int outsize = set_message(outbuf,0,0,True);
+       int outsize = set_message(outbuf,0,0,False);
        SMB_BIG_UINT count,offset;
        NTSTATUS status;
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
@@ -3451,7 +3487,12 @@ int reply_unlock(connection_struct *conn, char *inbuf,char *outbuf, int size,
        count = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv1);
        offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
        
-       status = do_unlock(fsp, conn, SVAL(inbuf,smb_pid), count, offset);
+       status = do_unlock(fsp,
+                       (uint32)SVAL(inbuf,smb_pid),
+                       count,
+                       offset,
+                       WINDOWS_LOCK);
+
        if (NT_STATUS_V(status)) {
                END_PROFILE(SMBunlock);
                return ERROR_NT(status);
@@ -3469,12 +3510,13 @@ int reply_unlock(connection_struct *conn, char *inbuf,char *outbuf, int size,
 
 /****************************************************************************
  Reply to a tdis.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_tdis(connection_struct *conn, 
               char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
 {
-       int outsize = set_message(outbuf,0,0,True);
+       int outsize = set_message(outbuf,0,0,False);
        uint16 vuid;
        START_PROFILE(SMBtdis);
 
@@ -3496,6 +3538,7 @@ int reply_tdis(connection_struct *conn,
 
 /****************************************************************************
  Reply to a echo.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_echo(connection_struct *conn,
@@ -3549,6 +3592,8 @@ int reply_printopen(connection_struct *conn,
 {
        int outsize = 0;
        files_struct *fsp;
+       NTSTATUS status;
+       
        START_PROFILE(SMBsplopen);
        
        if (!CAN_PRINT(conn)) {
@@ -3557,11 +3602,11 @@ int reply_printopen(connection_struct *conn,
        }
 
        /* Open for exclusive use, write only. */
-       fsp = print_fsp_open(conn, NULL);
+       status = print_fsp_open(conn, NULL, &fsp);
 
-       if (!fsp) {
+       if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBsplopen);
-               return(UNIXERROR(ERRDOS,ERRnoaccess));
+               return(ERROR_NT(status));
        }
 
        outsize = set_message(outbuf,1,0,True);
@@ -3581,7 +3626,7 @@ int reply_printopen(connection_struct *conn,
 int reply_printclose(connection_struct *conn,
                     char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
 {
-       int outsize = set_message(outbuf,0,0,True);
+       int outsize = set_message(outbuf,0,0,False);
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
        int close_err = 0;
        START_PROFILE(SMBsplclose);
@@ -3686,7 +3731,7 @@ int reply_printqueue(connection_struct *conn,
 int reply_printwrite(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
 {
        int numtowrite;
-       int outsize = set_message(outbuf,0,0,True);
+       int outsize = set_message(outbuf,0,0,False);
        char *data;
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
 
@@ -3789,6 +3834,17 @@ int reply_mkdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
 
        status = mkdir_internal(conn, directory,bad_path);
        if (!NT_STATUS_IS_OK(status)) {
+
+               if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION) &&
+                   !use_nt_status()) {
+                       /*
+                        * Yes, in the DOS error code case we get a
+                        * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
+                        * samba4 torture test.
+                        */
+                       status = NT_STATUS_DOS(ERRDOS, ERRnoaccess);
+               }
+
                END_PROFILE(SMBmkdir);
                return ERROR_NT(status);
        }
@@ -3811,7 +3867,7 @@ int reply_mkdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
                change_owner_to_parent(conn, NULL, directory, &sbuf);
        }
 
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
 
        DEBUG( 3, ( "mkdir %s ret=%d\n", directory, outsize ) );
 
@@ -3999,7 +4055,7 @@ int reply_rmdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
                return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRbadpath);
        }
  
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
   
        DEBUG( 3, ( "rmdir %s\n", directory ) );
   
@@ -4359,14 +4415,14 @@ NTSTATUS rename_internals(connection_struct *conn, char *name, char *newname, ui
         * Tine Smukavec <valentin.smukavec@hermes.si>.
         */
 
-       if (!rc && mangle_is_mangled(mask,SNUM(conn)))
-               mangle_check_cache( mask, sizeof(pstring)-1, SNUM(conn));
+       if (!rc && mangle_is_mangled(mask, conn->params))
+               mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
 
        if (!has_wild) {
                /*
                 * No wildcards - just process the one file.
                 */
-               BOOL is_short_name = mangle_is_8_3(name, True, SNUM(conn));
+               BOOL is_short_name = mangle_is_8_3(name, True, conn->params);
 
                /* Add a terminating '/' to the directory name. */
                pstrcat(directory,"/");
@@ -4671,7 +4727,7 @@ int reply_mv(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
         * update after a rename..
         */     
        process_pending_change_notify_queue((time_t)0);
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
   
        END_PROFILE(SMBmv);
        return(outsize);
@@ -4690,6 +4746,7 @@ BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
        pstring dest;
        uint32 dosattrs;
        uint32 new_create_disposition;
+       NTSTATUS status;
  
        *err_ret = 0;
 
@@ -4718,16 +4775,16 @@ BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
                }
        }
 
-       fsp1 = open_file_ntcreate(conn,src,&src_sbuf,
+       status = open_file_ntcreate(conn,src,&src_sbuf,
                        FILE_GENERIC_READ,
                        FILE_SHARE_READ|FILE_SHARE_WRITE,
                        FILE_OPEN,
                        0,
                        FILE_ATTRIBUTE_NORMAL,
                        INTERNAL_OPEN_ONLY,
-                       NULL);
+                       NULL, &fsp1);
 
-       if (!fsp1) {
+       if (!NT_STATUS_IS_OK(status)) {
                return(False);
        }
 
@@ -4736,16 +4793,16 @@ BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
                ZERO_STRUCTP(&sbuf2);
        }
 
-       fsp2 = open_file_ntcreate(conn,dest,&sbuf2,
+       status = open_file_ntcreate(conn,dest,&sbuf2,
                        FILE_GENERIC_WRITE,
                        FILE_SHARE_READ|FILE_SHARE_WRITE,
                        new_create_disposition,
                        0,
                        dosattrs,
                        INTERNAL_OPEN_ONLY,
-                       NULL);
+                       NULL, &fsp2);
 
-       if (!fsp2) {
+       if (!NT_STATUS_IS_OK(status)) {
                close_file(fsp1,ERROR_CLOSE);
                return(False);
        }
@@ -4877,8 +4934,8 @@ int reply_copy(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
         * Tine Smukavec <valentin.smukavec@hermes.si>.
         */
 
-       if (!rc && mangle_is_mangled(mask, SNUM(conn)))
-               mangle_check_cache( mask, sizeof(pstring)-1, SNUM(conn));
+       if (!rc && mangle_is_mangled(mask, conn->params))
+               mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
 
        has_wild = path_contains_wcard1;
 
@@ -4946,8 +5003,12 @@ int reply_copy(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
                        END_PROFILE(SMBcopy);
                        return ERROR_DOS(ERRDOS,error);
                } else {
-                       if((errno == ENOENT) && (bad_path1 || bad_path2)) {
-                               set_saved_error_triple(ERRDOS, ERRbadpath, NT_STATUS_OK);
+                       if((errno == ENOENT) && (bad_path1 || bad_path2) &&
+                          !use_nt_status()) {
+                               /* Samba 3.0.22 has ERRDOS/ERRbadpath in the
+                                * DOS error code case
+                                */
+                               return ERROR_DOS(ERRDOS, ERRbadpath);
                        }
                        END_PROFILE(SMBcopy);
                        return(UNIXERROR(ERRDOS,error));
@@ -5002,7 +5063,7 @@ int reply_setdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
                return ERROR_DOS(ERRDOS,ERRbadpath);
        }
   
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
        SCVAL(outbuf,smb_reh,CVAL(inbuf,smb_reh));
   
        DEBUG(3,("setdir %s\n", newdir));
@@ -5018,12 +5079,12 @@ int reply_setdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
  Get a lock pid, dealing with large count requests.
 ****************************************************************************/
 
-uint16 get_lock_pid( char *data, int data_offset, BOOL large_file_format)
+uint32 get_lock_pid( char *data, int data_offset, BOOL large_file_format)
 {
        if(!large_file_format)
-               return SVAL(data,SMB_LPID_OFFSET(data_offset));
+               return (uint32)SVAL(data,SMB_LPID_OFFSET(data_offset));
        else
-               return SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
+               return (uint32)SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
 }
 
 /****************************************************************************
@@ -5160,7 +5221,7 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
        uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
        uint16 num_locks = SVAL(inbuf,smb_vwv7);
        SMB_BIG_UINT count = 0, offset = 0;
-       uint16 lock_pid;
+       uint32 lock_pid;
        int32 lock_timeout = IVAL(inbuf,smb_vwv4);
        int i;
        char *data;
@@ -5180,7 +5241,7 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
                /* we don't support these - and CANCEL_LOCK makes w2k
                   and XP reboot so I don't really want to be
                   compatible! (tridge) */
-               return ERROR_DOS(ERRDOS, ERRnoatomiclocks);
+               return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
        }
        
        if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
@@ -5206,7 +5267,14 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
                 */
                
                if (fsp->oplock_type == 0) {
-                       DEBUG(0,("reply_lockingX: Error : oplock break from "
+
+                       /* The Samba4 nbench simulator doesn't understand
+                          the difference between break to level2 and break
+                          to none from level2 - it sends oplock break
+                          replies in both cases. Don't keep logging an error
+                          message here - just ignore it. JRA. */
+
+                       DEBUG(5,("reply_lockingX: Error : oplock break from "
                                 "client for fnum = %d (oplock=%d) and no "
                                 "oplock granted on this file (%s).\n",
                                 fsp->fnum, fsp->oplock_type, fsp->fsp_name));
@@ -5278,7 +5346,12 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
                          "pid %u, file %s\n", (double)offset, (double)count,
                          (unsigned int)lock_pid, fsp->fsp_name ));
                
-               status = do_unlock(fsp,conn,lock_pid,count,offset);
+               status = do_unlock(fsp,
+                               lock_pid,
+                               count,
+                               offset,
+                               WINDOWS_LOCK);
+
                if (NT_STATUS_V(status)) {
                        END_PROFILE(SMBlockingX);
                        return ERROR_NT(status);
@@ -5296,6 +5369,7 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
           of smb_lkrng structs */
        
        for(i = 0; i < (int)num_locks; i++) {
+               enum brl_type lock_type = ((locktype & 1) ? READ_LOCK:WRITE_LOCK);
                lock_pid = get_lock_pid( data, i, large_file_format);
                count = get_lock_count( data, i, large_file_format);
                offset = get_lock_offset( data, i, large_file_format, &err);
@@ -5313,9 +5387,14 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
                          (double)count, (unsigned int)lock_pid,
                          fsp->fsp_name, (int)lock_timeout ));
                
-               status = do_lock_spin(fsp,conn,lock_pid, count,offset, 
-                                     ((locktype & 1) ? READ_LOCK:WRITE_LOCK),
-                                     &my_lock_ctx);
+               status = do_lock_spin(fsp,
+                                       lock_pid,
+                                       count,
+                                       offset, 
+                                       lock_type,
+                                       WINDOWS_LOCK,
+                                       &my_lock_ctx);
+
                if (NT_STATUS_V(status)) {
                        /*
                         * Interesting fact found by IFSTEST /t
@@ -5333,8 +5412,13 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
                                 * onto the blocking lock queue.
                                 */
                                if(push_blocking_lock_request(inbuf, length,
-                                                             lock_timeout, i,
-                                                             lock_pid, offset,
+                                                             fsp,
+                                                             lock_timeout,
+                                                             i,
+                                                             lock_pid,
+                                                             lock_type,
+                                                             WINDOWS_LOCK,
+                                                             offset,
                                                              count)) {
                                        END_PROFILE(SMBlockingX);
                                        return -1;
@@ -5367,7 +5451,11 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
                                return ERROR_DOS(ERRDOS,ERRnoaccess);
                        }
                        
-                       do_unlock(fsp,conn,lock_pid,count,offset);
+                       do_unlock(fsp,
+                               lock_pid,
+                               count,
+                               offset,
+                               WINDOWS_LOCK);
                }
                END_PROFILE(SMBlockingX);
                return ERROR_NT(status);
@@ -5429,7 +5517,7 @@ int reply_readbmpx(connection_struct *conn, char *inbuf,char *outbuf,int length,
        tcount = maxcount;
        total_read = 0;
 
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
                END_PROFILE(SMBreadBmpx);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -5474,7 +5562,7 @@ int reply_setattrE(connection_struct *conn, char *inbuf,char *outbuf, int size,
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
        START_PROFILE(SMBsetattrE);
 
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
 
        if(!fsp || (fsp->conn != conn)) {
                END_PROFILE(SMBsetattrE);
@@ -5561,7 +5649,7 @@ int reply_writebmpx(connection_struct *conn, char *inbuf,char *outbuf, int size,
                not an SMBwritebmpx - set this up now so we don't forget */
        SCVAL(outbuf,smb_com,SMBwritec);
 
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos,WRITE_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos,WRITE_LOCK)) {
                END_PROFILE(SMBwriteBmpx);
                return(ERROR_DOS(ERRDOS,ERRlock));
        }