s3-build: avoid to globally include printing and spoolss headers.
[vlendec/samba-autobuild/.git] / source3 / smbd / reply.c
index a113a9d98261487873ab536da695238cec431073..ba4c90d55299dce791712866a87c508706659f96 100644 (file)
 */
 
 #include "includes.h"
+#include "printing.h"
 #include "smbd/globals.h"
+#include "../librpc/gen_ndr/cli_spoolss.h"
+#include "rpc_client/cli_spoolss.h"
+#include "rpc_client/init_spoolss.h"
 
 /****************************************************************************
  Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
@@ -322,11 +326,11 @@ size_t srvstr_get_path_req(TALLOC_CTX *mem_ctx, struct smb_request *req,
 bool check_fsp_open(connection_struct *conn, struct smb_request *req,
                    files_struct *fsp)
 {
-       if (!(fsp) || !(conn)) {
+       if ((fsp == NULL) || (conn == NULL)) {
                reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return False;
        }
-       if (((conn) != (fsp)->conn) || req->vuid != (fsp)->vuid) {
+       if ((conn != fsp->conn) || (req->vuid != fsp->vuid)) {
                reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return False;
        }
@@ -343,15 +347,15 @@ bool check_fsp(connection_struct *conn, struct smb_request *req,
        if (!check_fsp_open(conn, req, fsp)) {
                return False;
        }
-       if ((fsp)->is_directory) {
+       if (fsp->is_directory) {
                reply_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
                return False;
        }
-       if ((fsp)->fh->fd == -1) {
+       if (fsp->fh->fd == -1) {
                reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                return False;
        }
-       (fsp)->num_smb_operations++;
+       fsp->num_smb_operations++;
        return True;
 }
 
@@ -386,22 +390,6 @@ bool check_fsp_ntquota_handle(connection_struct *conn, struct smb_request *req,
        return true;
 }
 
-/****************************************************************************
- Check if we have a correct fsp. Replacement for the FSP_BELONGS_CONN macro
-****************************************************************************/
-
-bool fsp_belongs_conn(connection_struct *conn, struct smb_request *req,
-                     files_struct *fsp)
-{
-       if ((fsp) && (conn) && ((conn)==(fsp)->conn)
-           && (req->vuid == (fsp)->vuid)) {
-               return True;
-       }
-
-       reply_nterror(req, NT_STATUS_INVALID_HANDLE);
-       return False;
-}
-
 static bool netbios_session_retarget(const char *name, int name_type)
 {
        char *trim_name;
@@ -495,13 +483,12 @@ static bool netbios_session_retarget(const char *name, int name_type)
  Reply to a (netbios-level) special message.
 ****************************************************************************/
 
-void reply_special(char *inbuf)
+void reply_special(struct smbd_server_connection *sconn, char *inbuf)
 {
        int msg_type = CVAL(inbuf,0);
        int msg_flags = CVAL(inbuf,1);
        fstring name1,name2;
        char name_type1, name_type2;
-       struct smbd_server_connection *sconn = smbd_server_conn;
 
        /*
         * We only really use 4 bytes of the outbuf, but for the smb_setlen
@@ -607,7 +594,7 @@ void reply_tcon(struct smb_request *req)
        const char *p;
        DATA_BLOB password_blob;
        TALLOC_CTX *ctx = talloc_tos();
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
 
        START_PROFILE(SMBtcon);
 
@@ -683,7 +670,7 @@ void reply_tcon_and_X(struct smb_request *req)
        char *path = NULL;
        const char *p, *q;
        uint16 tcon_flags;
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
 
        START_PROFILE(SMBtconX);
 
@@ -704,7 +691,7 @@ void reply_tcon_and_X(struct smb_request *req)
        }
 
        if ((passlen > MAX_PASS_LEN) || (passlen >= req->buflen)) {
-               reply_doserror(req, ERRDOS, ERRbuftoosmall);
+               reply_force_doserror(req, ERRDOS, ERRbuftoosmall);
                END_PROFILE(SMBtconX);
                return;
        }
@@ -744,7 +731,7 @@ void reply_tcon_and_X(struct smb_request *req)
                q = strchr_m(path+2,'\\');
                if (!q) {
                        data_blob_clear_free(&password);
-                       reply_doserror(req, ERRDOS, ERRnosuchshare);
+                       reply_nterror(req, NT_STATUS_BAD_NETWORK_NAME);
                        END_PROFILE(SMBtconX);
                        return;
                }
@@ -864,7 +851,7 @@ void reply_unknown_new(struct smb_request *req, uint8 type)
 {
        DEBUG(0, ("unknown command type (%s): type=%d (0x%X)\n",
                  smb_fn_name(type), type, type));
-       reply_doserror(req, ERRSRV, ERRunknownsmb);
+       reply_force_doserror(req, ERRSRV, ERRunknownsmb);
        return;
 }
 
@@ -901,7 +888,7 @@ void reply_ioctl(struct smb_request *req)
                    replysize = 32;
                    break;
            default:
-                   reply_doserror(req, ERRSRV, ERRnosupport);
+                   reply_force_doserror(req, ERRSRV, ERRnosupport);
                    END_PROFILE(SMBioctl);
                    return;
        }
@@ -920,11 +907,16 @@ void reply_ioctl(struct smb_request *req)
                        files_struct *fsp = file_fsp(
                                req, SVAL(req->vwv+0, 0));
                        if (!fsp) {
-                               reply_doserror(req, ERRDOS, ERRbadfid);
+                               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                                END_PROFILE(SMBioctl);
                                return;
                        }
-                       SSVAL(p,0,fsp->rap_print_jobid);             /* Job number */
+                       /* Job number */
+                       if (fsp->print_file) {
+                               SSVAL(p, 0, fsp->print_file->rap_jobid);
+                       } else {
+                               SSVAL(p, 0, 0);
+                       }
                        srvstr_push((char *)req->outbuf, req->flags2, p+2,
                                    global_myname(), 15,
                                    STR_TERMINATE|STR_ASCII);
@@ -1362,7 +1354,7 @@ void reply_search(struct smb_request *req)
        TALLOC_CTX *ctx = talloc_tos();
        bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
        struct dptr_struct *dirptr = NULL;
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
 
        START_PROFILE(SMBsearch);
 
@@ -1629,7 +1621,7 @@ void reply_fclose(struct smb_request *req)
        NTSTATUS err;
        bool path_contains_wcard = False;
        TALLOC_CTX *ctx = talloc_tos();
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
 
        START_PROFILE(SMBfclose);
 
@@ -1652,7 +1644,7 @@ void reply_fclose(struct smb_request *req)
        p += 2;
 
        if (status_len == 0) {
-               reply_doserror(req, ERRSRV, ERRsrverror);
+               reply_force_doserror(req, ERRSRV, ERRsrverror);
                END_PROFILE(SMBfclose);
                return;
        }
@@ -1694,6 +1686,7 @@ void reply_open(struct smb_request *req)
        uint32 share_mode;
        uint32 create_disposition;
        uint32 create_options = 0;
+       uint32_t private_flags = 0;
        NTSTATUS status;
        bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
        TALLOC_CTX *ctx = talloc_tos();
@@ -1737,8 +1730,8 @@ void reply_open(struct smb_request *req)
        if (!map_open_params_to_ntcreate(smb_fname, deny_mode,
                                         OPENX_FILE_EXISTS_OPEN, &access_mask,
                                         &share_mode, &create_disposition,
-                                        &create_options)) {
-               reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
+                                        &create_options, &private_flags)) {
+               reply_force_doserror(req, ERRDOS, ERRbadaccess);
                goto out;
        }
 
@@ -1754,6 +1747,7 @@ void reply_open(struct smb_request *req)
                dos_attr,                               /* file_attributes */
                oplock_request,                         /* oplock_request */
                0,                                      /* allocation_size */
+               private_flags,
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
@@ -1789,7 +1783,8 @@ void reply_open(struct smb_request *req)
                DEBUG(3,("attempt to open a directory %s\n",
                         fsp_str_dbg(fsp)));
                close_file(req, fsp, ERROR_CLOSE);
-               reply_doserror(req, ERRDOS,ERRnoaccess);
+               reply_botherror(req, NT_STATUS_ACCESS_DENIED,
+                       ERRDOS, ERRnoaccess);
                goto out;
        }
 
@@ -1852,6 +1847,7 @@ void reply_open_and_X(struct smb_request *req)
        uint32 share_mode;
        uint32 create_disposition;
        uint32 create_options = 0;
+       uint32_t private_flags = 0;
        TALLOC_CTX *ctx = talloc_tos();
 
        START_PROFILE(SMBopenX);
@@ -1875,7 +1871,7 @@ void reply_open_and_X(struct smb_request *req)
                if (lp_nt_pipe_support()) {
                        reply_open_pipe_and_X(conn, req);
                } else {
-                       reply_doserror(req, ERRSRV, ERRaccess);
+                       reply_nterror(req, NT_STATUS_NETWORK_ACCESS_DENIED);
                }
                goto out;
        }
@@ -1909,8 +1905,9 @@ void reply_open_and_X(struct smb_request *req)
        if (!map_open_params_to_ntcreate(smb_fname, deny_mode, smb_ofun,
                                         &access_mask, &share_mode,
                                         &create_disposition,
-                                        &create_options)) {
-               reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
+                                        &create_options,
+                                        &private_flags)) {
+               reply_force_doserror(req, ERRDOS, ERRbadaccess);
                goto out;
        }
 
@@ -1926,6 +1923,7 @@ void reply_open_and_X(struct smb_request *req)
                smb_attr,                               /* file_attributes */
                oplock_request,                         /* oplock_request */
                0,                                      /* allocation_size */
+               private_flags,
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
@@ -1963,7 +1961,7 @@ void reply_open_and_X(struct smb_request *req)
        mtime = convert_timespec_to_time_t(smb_fname->st.st_ex_mtime);
        if (fattr & aDIR) {
                close_file(req, fsp, ERROR_CLOSE);
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                goto out;
        }
 
@@ -2013,7 +2011,7 @@ void reply_open_and_X(struct smb_request *req)
        SSVAL(req->outbuf,smb_vwv11,smb_action);
 
        if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
-               SIVAL(req->outbuf, smb_vwv15, STD_RIGHT_ALL_ACCESS);
+               SIVAL(req->outbuf, smb_vwv15, SEC_STD_ALL);
        }
 
        chain_reply(req);
@@ -2029,7 +2027,7 @@ void reply_open_and_X(struct smb_request *req)
 
 void reply_ulogoffX(struct smb_request *req)
 {
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
        user_struct *vuser;
 
        START_PROFILE(SMBulogoffX);
@@ -2143,6 +2141,7 @@ void reply_mknew(struct smb_request *req)
                fattr,                                  /* file_attributes */
                oplock_request,                         /* oplock_request */
                0,                                      /* allocation_size */
+               0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
@@ -2271,6 +2270,7 @@ void reply_ctemp(struct smb_request *req)
                fattr,                                  /* file_attributes */
                oplock_request,                         /* oplock_request */
                0,                                      /* allocation_size */
+               0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
@@ -2395,7 +2395,7 @@ static NTSTATUS do_unlink(connection_struct *conn,
        if (posix_paths) {
                ret = SMB_VFS_LSTAT(conn, smb_fname);
        } else {
-               ret = SMB_VFS_LSTAT(conn, smb_fname);
+               ret = SMB_VFS_STAT(conn, smb_fname);
        }
        if (ret != 0) {
                return map_nt_error_from_unix(errno);
@@ -2454,24 +2454,6 @@ static NTSTATUS do_unlink(connection_struct *conn,
                return NT_STATUS_OBJECT_NAME_INVALID;
 #endif /* JRATEST */
 
-       /* Fix for bug #3035 from SATOH Fumiyasu <fumiyas@miraclelinux.com>
-
-         On a Windows share, a file with read-only dosmode can be opened with
-         DELETE_ACCESS. But on a Samba share (delete readonly = no), it
-         fails with NT_STATUS_CANNOT_DELETE error.
-
-         This semantic causes a problem that a user can not
-         rename a file with read-only dosmode on a Samba share
-         from a Windows command prompt (i.e. cmd.exe, but can rename
-         from Windows Explorer).
-       */
-
-       if (!lp_delete_readonly(SNUM(conn))) {
-               if (fattr & aRONLY) {
-                       return NT_STATUS_CANNOT_DELETE;
-               }
-       }
-
        /* On open checks the open itself will check the share mode, so
           don't do it here as we'll get it wrong. */
 
@@ -2489,6 +2471,7 @@ static NTSTATUS do_unlink(connection_struct *conn,
                                FILE_ATTRIBUTE_NORMAL,
                 0,                     /* oplock_request */
                 0,                     /* allocation_size */
+                0,                     /* private_flags */
                 NULL,                  /* sd */
                 NULL,                  /* ea_list */
                 &fsp,                  /* result */
@@ -2500,6 +2483,16 @@ static NTSTATUS do_unlink(connection_struct *conn,
                return status;
        }
 
+       status = can_set_delete_on_close(fsp, fattr);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(10, ("do_unlink can_set_delete_on_close for file %s - "
+                       "(%s)\n",
+                       smb_fname_str_dbg(smb_fname),
+                       nt_errstr(status)));
+               close_file(req, fsp, NORMAL_CLOSE);
+               return status;
+       }
+
        /* The set is across all open files on this dev/inode pair. */
        if (!set_delete_on_close(fsp, True, &conn->server_info->utok)) {
                close_file(req, fsp, NORMAL_CLOSE);
@@ -2638,7 +2631,7 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
                                continue;
                        }
 
-                       if(!mask_match(dname, fname_mask, get_Protocol(),
+                       if(!mask_match(dname, fname_mask,
                                       conn->case_sensitive)) {
                                TALLOC_FREE(frame);
                                TALLOC_FREE(talloced);
@@ -2892,13 +2885,17 @@ static void sendfile_short_send(files_struct *fsp,
  Return a readbraw error (4 bytes of zero).
 ****************************************************************************/
 
-static void reply_readbraw_error(void)
+static void reply_readbraw_error(struct smbd_server_connection *sconn)
 {
        char header[4];
+
        SIVAL(header,0,0);
+
+       smbd_lock_socket(sconn);
        if (write_data(smbd_server_fd(),header,4) != 4) {
                fail_readraw();
        }
+       smbd_unlock_socket(sconn);
 }
 
 /****************************************************************************
@@ -2912,6 +2909,7 @@ static void send_file_readbraw(connection_struct *conn,
                               size_t nread,
                               ssize_t mincount)
 {
+       struct smbd_server_connection *sconn = req->sconn;
        char *outbuf = NULL;
        ssize_t ret=0;
 
@@ -2925,7 +2923,7 @@ 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), smbd_server_conn->smb1.signing_state) ) {
+           lp_use_sendfile(SNUM(conn), req->sconn->smb1.signing_state) ) {
                ssize_t sendfile_read = -1;
                char header[4];
                DATA_BLOB header_blob;
@@ -2994,7 +2992,7 @@ normal_readbraw:
        if (!outbuf) {
                DEBUG(0,("send_file_readbraw: TALLOC_ARRAY failed for size %u.\n",
                        (unsigned)(nread+4)));
-               reply_readbraw_error();
+               reply_readbraw_error(sconn);
                return;
        }
 
@@ -3023,6 +3021,7 @@ normal_readbraw:
 void reply_readbraw(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
+       struct smbd_server_connection *sconn = req->sconn;
        ssize_t maxcount,mincount;
        size_t nread = 0;
        SMB_OFF_T startpos;
@@ -3032,14 +3031,22 @@ void reply_readbraw(struct smb_request *req)
 
        START_PROFILE(SMBreadbraw);
 
-       if (srv_is_signing_active(smbd_server_conn) ||
+       if (srv_is_signing_active(sconn) ||
            is_encrypted_packet(req->inbuf)) {
                exit_server_cleanly("reply_readbraw: SMB signing/sealing is active - "
                        "raw reads/writes are disallowed.");
        }
 
        if (req->wct < 8) {
-               reply_readbraw_error();
+               reply_readbraw_error(sconn);
+               END_PROFILE(SMBreadbraw);
+               return;
+       }
+
+       if (sconn->smb1.echo_handler.trusted_fde) {
+               DEBUG(2,("SMBreadbraw rejected with NOT_SUPPORTED because of "
+                        "'async smb echo handler = yes'\n"));
+               reply_readbraw_error(sconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
@@ -3067,7 +3074,7 @@ void reply_readbraw(struct smb_request *req)
                DEBUG(3,("reply_readbraw: fnum %d not valid "
                        "- cache prime?\n",
                        (int)SVAL(req->vwv+0, 0)));
-               reply_readbraw_error();
+               reply_readbraw_error(sconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
@@ -3078,7 +3085,7 @@ void reply_readbraw(struct smb_request *req)
                                (fsp->access_mask & FILE_EXECUTE)))) {
                DEBUG(3,("reply_readbraw: fnum %d not readable.\n",
                                (int)SVAL(req->vwv+0, 0)));
-               reply_readbraw_error();
+               reply_readbraw_error(sconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
@@ -3116,7 +3123,7 @@ void reply_readbraw(struct smb_request *req)
                        DEBUG(0,("reply_readbraw: negative 64 bit "
                                "readraw offset (%.0f) !\n",
                                (double)startpos ));
-                       reply_readbraw_error();
+                       reply_readbraw_error(sconn);
                        END_PROFILE(SMBreadbraw);
                        return;
                }
@@ -3128,12 +3135,12 @@ void reply_readbraw(struct smb_request *req)
        /* ensure we don't overrun the packet size */
        maxcount = MIN(65535,maxcount);
 
-       init_strict_lock_struct(fsp, (uint32)req->smbpid,
+       init_strict_lock_struct(fsp, (uint64_t)req->smbpid,
            (uint64_t)startpos, (uint64_t)maxcount, READ_LOCK,
            &lock);
 
        if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
-               reply_readbraw_error();
+               reply_readbraw_error(sconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
@@ -3188,7 +3195,7 @@ void reply_lockread(struct smb_request *req)
        files_struct *fsp;
        struct byte_range_lock *br_lck = NULL;
        char *p = NULL;
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
 
        START_PROFILE(SMBlockread);
 
@@ -3206,7 +3213,7 @@ void reply_lockread(struct smb_request *req)
        }
 
        if (!CHECK_READ(fsp,req)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBlockread);
                return;
        }
@@ -3228,9 +3235,9 @@ void reply_lockread(struct smb_request *req)
         * Note that the requested lock size is unaffected by max_recv.
         */
 
-       br_lck = do_lock(smbd_messaging_context(),
+       br_lck = do_lock(req->sconn->msg_ctx,
                        fsp,
-                       req->smbpid,
+                       (uint64_t)req->smbpid,
                        (uint64_t)numtoread,
                        (uint64_t)startpos,
                        WRITE_LOCK,
@@ -3298,7 +3305,7 @@ void reply_read(struct smb_request *req)
        int outsize = 0;
        files_struct *fsp;
        struct lock_struct lock;
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
 
        START_PROFILE(SMBread);
 
@@ -3316,7 +3323,7 @@ void reply_read(struct smb_request *req)
        }
 
        if (!CHECK_READ(fsp,req)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBread);
                return;
        }
@@ -3341,12 +3348,12 @@ Returning short read of maximum allowed for compatibility with Windows 2000.\n",
 
        data = smb_buf(req->outbuf) + 3;
 
-       init_strict_lock_struct(fsp, (uint32)req->smbpid,
+       init_strict_lock_struct(fsp, (uint64_t)req->smbpid,
            (uint64_t)startpos, (uint64_t)numtoread, READ_LOCK,
            &lock);
 
        if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
-               reply_doserror(req, ERRDOS,ERRlock);
+               reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                END_PROFILE(SMBread);
                return;
        }
@@ -3423,12 +3430,12 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                return;
        }
 
-       init_strict_lock_struct(fsp, (uint32)req->smbpid,
+       init_strict_lock_struct(fsp, (uint64_t)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);
+               reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                return;
        }
 
@@ -3452,7 +3459,7 @@ 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) &&
            (fsp->wcp == NULL) &&
-           lp_use_sendfile(SNUM(conn), smbd_server_conn->smb1.signing_state) ) {
+           lp_use_sendfile(SNUM(conn), req->sconn->smb1.signing_state) ) {
                uint8 headerbuf[smb_size + 12 * 2];
                DATA_BLOB header;
 
@@ -3626,7 +3633,7 @@ void reply_read_and_X(struct smb_request *req)
        }
 
        if (!CHECK_READ(fsp,req)) {
-               reply_doserror(req, ERRDOS,ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBreadX);
                return;
        }
@@ -3642,7 +3649,7 @@ 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(smbd_server_conn) ||
+                       if (srv_is_signing_active(req->sconn) ||
                            is_encrypted_packet(req->inbuf)) {
                                reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
                                END_PROFILE(SMBreadX);
@@ -3677,7 +3684,7 @@ void reply_read_and_X(struct smb_request *req)
                                 "used and we don't support 64 bit offsets.\n",
                                 (unsigned int)IVAL(req->vwv+10, 0) ));
                        END_PROFILE(SMBreadX);
-                       reply_doserror(req, ERRDOS, ERRbadaccess);
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                        return;
                }
 
@@ -3685,12 +3692,28 @@ void reply_read_and_X(struct smb_request *req)
 
        }
 
-       if (!big_readX &&
-           schedule_aio_read_and_X(conn, req, fsp, startpos, smb_maxcnt)) {
-               goto out;
+       if (!big_readX) {
+               NTSTATUS status = schedule_aio_read_and_X(conn,
+                                       req,
+                                       fsp,
+                                       startpos,
+                                       smb_maxcnt);
+               if (NT_STATUS_IS_OK(status)) {
+                       /* Read scheduled - we're done. */
+                       goto out;
+               }
+               if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
+                       /* Real error - report to client. */
+                       END_PROFILE(SMBreadX);
+                       reply_nterror(req, status);
+                       return;
+               }
+               /* NT_STATUS_RETRY - fall back to sync read. */
        }
 
+       smbd_lock_socket(req->sconn);
        send_file_readX(conn, req, fsp, startpos, smb_maxcnt);
+       smbd_unlock_socket(req->sconn);
 
  out:
        END_PROFILE(SMBreadX);
@@ -3739,7 +3762,7 @@ void reply_writebraw(struct smb_request *req)
         */
        SCVAL(req->inbuf,smb_com,SMBwritec);
 
-       if (srv_is_signing_active(smbd_server_conn)) {
+       if (srv_is_signing_active(req->sconn)) {
                END_PROFILE(SMBwritebraw);
                exit_server_cleanly("reply_writebraw: SMB signing is active - "
                                "raw reads/writes are disallowed.");
@@ -3752,6 +3775,15 @@ void reply_writebraw(struct smb_request *req)
                return;
        }
 
+       if (req->sconn->smb1.echo_handler.trusted_fde) {
+               DEBUG(2,("SMBwritebraw rejected with NOT_SUPPORTED because of "
+                        "'async smb echo handler = yes'\n"));
+               reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
+               error_to_writebrawerr(req);
+               END_PROFILE(SMBwritebraw);
+               return;
+       }
+
        fsp = file_fsp(req, SVAL(req->vwv+0, 0));
        if (!check_fsp(conn, req, fsp)) {
                error_to_writebrawerr(req);
@@ -3760,7 +3792,7 @@ void reply_writebraw(struct smb_request *req)
        }
 
        if (!CHECK_WRITE(fsp)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                error_to_writebrawerr(req);
                END_PROFILE(SMBwritebraw);
                return;
@@ -3789,15 +3821,17 @@ void reply_writebraw(struct smb_request *req)
                return;
        }
 
-       init_strict_lock_struct(fsp, (uint32)req->smbpid,
-           (uint64_t)startpos, (uint64_t)tcount, WRITE_LOCK,
-           &lock);
+       if (!fsp->print_file) {
+               init_strict_lock_struct(fsp, (uint64_t)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);
-               return;
+               if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+                       reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
+                       error_to_writebrawerr(req);
+                       END_PROFILE(SMBwritebraw);
+                       return;
+               }
        }
 
        if (numtowrite>0) {
@@ -3810,7 +3844,7 @@ void reply_writebraw(struct smb_request *req)
                (int)nwritten, (int)write_through));
 
        if (nwritten < (ssize_t)numtowrite)  {
-               reply_doserror(req, ERRHRD, ERRdiskfull);
+               reply_nterror(req, NT_STATUS_DISK_FULL);
                error_to_writebrawerr(req);
                goto strict_unlock;
        }
@@ -3820,7 +3854,7 @@ void reply_writebraw(struct smb_request *req)
        /* Allocate a buffer of 64k + length. */
        buf = TALLOC_ARRAY(NULL, char, 65540);
        if (!buf) {
-               reply_doserror(req, ERRDOS, ERRnomem);
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
                error_to_writebrawerr(req);
                goto strict_unlock;
        }
@@ -3911,7 +3945,9 @@ void reply_writebraw(struct smb_request *req)
                fsp->fnum, (double)startpos, (int)numtowrite,
                (int)total_written));
 
-       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+       if (!fsp->print_file) {
+               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+       }
 
        /* We won't return a status if write through is not selected - this
         * follows what WfWg does */
@@ -3935,7 +3971,9 @@ void reply_writebraw(struct smb_request *req)
        return;
 
 strict_unlock:
-       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+       if (!fsp->print_file) {
+               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+       }
 
        END_PROFILE(SMBwritebraw);
        return;
@@ -3976,7 +4014,7 @@ void reply_writeunlock(struct smb_request *req)
        }
 
        if (!CHECK_WRITE(fsp)) {
-               reply_doserror(req, ERRDOS,ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBwriteunlock);
                return;
        }
@@ -3985,13 +4023,13 @@ 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) {
-               init_strict_lock_struct(fsp, (uint32)req->smbpid,
+       if (!fsp->print_file && numtowrite > 0) {
+               init_strict_lock_struct(fsp, (uint64_t)req->smbpid,
                    (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
                    &lock);
 
                if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
-                       reply_doserror(req, ERRDOS, ERRlock);
+                       reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                        END_PROFILE(SMBwriteunlock);
                        return;
                }
@@ -4021,14 +4059,14 @@ void reply_writeunlock(struct smb_request *req)
        }
 
        if((nwritten < numtowrite) && (numtowrite != 0)) {
-               reply_doserror(req, ERRHRD, ERRdiskfull);
+               reply_nterror(req, NT_STATUS_DISK_FULL);
                goto strict_unlock;
        }
 
-       if (numtowrite) {
-               status = do_unlock(smbd_messaging_context(),
+       if (numtowrite && !fsp->print_file) {
+               status = do_unlock(req->sconn->msg_ctx,
                                fsp,
-                               req->smbpid,
+                               (uint64_t)req->smbpid,
                                (uint64_t)numtowrite, 
                                (uint64_t)startpos,
                                WINDOWS_LOCK);
@@ -4047,7 +4085,7 @@ void reply_writeunlock(struct smb_request *req)
                 fsp->fnum, (int)numtowrite, (int)nwritten));
 
 strict_unlock:
-       if (numtowrite) {
+       if (numtowrite && !fsp->print_file) {
                SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
        }
 
@@ -4097,7 +4135,7 @@ void reply_write(struct smb_request *req)
        }
 
        if (!CHECK_WRITE(fsp)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBwrite);
                return;
        }
@@ -4106,14 +4144,16 @@ void reply_write(struct smb_request *req)
        startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
        data = (const char *)req->buf + 3;
 
-       init_strict_lock_struct(fsp, (uint32)req->smbpid,
-           (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
-           &lock);
+       if (!fsp->print_file) {
+               init_strict_lock_struct(fsp, (uint64_t)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;
+               if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+                       reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
+                       END_PROFILE(SMBwrite);
+                       return;
+               }
        }
 
        /*
@@ -4155,7 +4195,7 @@ void reply_write(struct smb_request *req)
        }
 
        if((nwritten == 0) && (numtowrite != 0)) {
-               reply_doserror(req, ERRHRD, ERRdiskfull);
+               reply_nterror(req, NT_STATUS_DISK_FULL);
                goto strict_unlock;
        }
 
@@ -4171,7 +4211,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);
+       if (!fsp->print_file) {
+               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+       }
 
        END_PROFILE(SMBwrite);
        return;
@@ -4185,13 +4227,13 @@ strict_unlock:
                                                (2*14) + /* word count (including bcc) */ \
                                                1 /* pad byte */)
 
-bool is_valid_writeX_buffer(const uint8_t *inbuf)
+bool is_valid_writeX_buffer(struct smbd_server_connection *sconn,
+                           const uint8_t *inbuf)
 {
        size_t numtowrite;
        connection_struct *conn = NULL;
        unsigned int doff = 0;
        size_t len = smb_len_large(inbuf);
-       struct smbd_server_connection *sconn = smbd_server_conn;
 
        if (is_encrypted_packet(inbuf)) {
                /* Can't do this on encrypted
@@ -4280,6 +4322,7 @@ void reply_write_and_X(struct smb_request *req)
        unsigned int smblen;
        char *data;
        NTSTATUS status;
+       int saved_errno = 0;
 
        START_PROFILE(SMBwriteX);
 
@@ -4307,14 +4350,14 @@ void reply_write_and_X(struct smb_request *req)
                        return;
                }
                if (numtowrite != req->unread_bytes) {
-                       reply_doserror(req, ERRDOS, ERRbadmem);
+                       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        END_PROFILE(SMBwriteX);
                        return;
                }
        } else {
                if (smb_doff > smblen || smb_doff + numtowrite < numtowrite ||
                                smb_doff + numtowrite > smblen) {
-                       reply_doserror(req, ERRDOS, ERRbadmem);
+                       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        END_PROFILE(SMBwriteX);
                        return;
                }
@@ -4323,7 +4366,7 @@ void reply_write_and_X(struct smb_request *req)
        /* If it's an IPC, pass off the pipe handler. */
        if (IS_IPC(conn)) {
                if (req->unread_bytes) {
-                       reply_doserror(req, ERRDOS, ERRbadmem);
+                       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        END_PROFILE(SMBwriteX);
                        return;
                }
@@ -4342,7 +4385,7 @@ void reply_write_and_X(struct smb_request *req)
        }
 
        if (!CHECK_WRITE(fsp)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBwriteX);
                return;
        }
@@ -4366,7 +4409,7 @@ void reply_write_and_X(struct smb_request *req)
                        DEBUG(0,("reply_write_and_X - large offset (%x << 32) "
                                 "used and we don't support 64 bit offsets.\n",
                                 (unsigned int)IVAL(req->vwv+12, 0) ));
-                       reply_doserror(req, ERRDOS, ERRbadaccess);
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                        END_PROFILE(SMBwriteX);
                        return;
                }
@@ -4374,16 +4417,6 @@ void reply_write_and_X(struct smb_request *req)
 #endif /* LARGE_SMB_OFF_T */
        }
 
-       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;
-       }
-
        /* X/Open SMB protocol says that, unlike SMBwrite
        if the length is zero then NO truncation is
        done, just a write of zero. To truncate a file,
@@ -4392,24 +4425,49 @@ void reply_write_and_X(struct smb_request *req)
        if(numtowrite == 0) {
                nwritten = 0;
        } else {
+               if (req->unread_bytes == 0) {
+                       status = schedule_aio_write_and_X(conn,
+                                               req,
+                                               fsp,
+                                               data,
+                                               startpos,
+                                               numtowrite);
 
-               if ((req->unread_bytes == 0) &&
-                   schedule_aio_write_and_X(conn, req, fsp, data, startpos,
-                                            numtowrite)) {
-                       goto strict_unlock;
+                       if (NT_STATUS_IS_OK(status)) {
+                               /* write scheduled - we're done. */
+                               goto out;
+                       }
+                       if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
+                               /* Real error - report to client. */
+                               reply_nterror(req, status);
+                               goto out;
+                       }
+                       /* NT_STATUS_RETRY - fall through to sync write. */
+               }
+
+               init_strict_lock_struct(fsp, (uint64_t)req->smbpid,
+                   (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
+                   &lock);
+
+               if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+                       reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
+                       goto out;
                }
 
                nwritten = write_file(req,fsp,data,startpos,numtowrite);
+               saved_errno = errno;
+
+               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
        }
 
        if(nwritten < 0) {
-               reply_nterror(req, map_nt_error_from_unix(errno));
-               goto strict_unlock;
+               reply_nterror(req, map_nt_error_from_unix(saved_errno));
+               goto out;
        }
 
        if((nwritten == 0) && (numtowrite != 0)) {
-               reply_doserror(req, ERRHRD, ERRdiskfull);
-               goto strict_unlock;
+               reply_nterror(req, NT_STATUS_DISK_FULL);
+               goto out;
        }
 
        reply_outbuf(req, 6, 0);
@@ -4429,18 +4487,14 @@ void reply_write_and_X(struct smb_request *req)
                DEBUG(5,("reply_write_and_X: sync_file for %s returned %s\n",
                         fsp_str_dbg(fsp), nt_errstr(status)));
                reply_nterror(req, status);
-               goto strict_unlock;
+               goto out;
        }
 
-       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
-
        END_PROFILE(SMBwriteX);
        chain_reply(req);
        return;
 
-strict_unlock:
-       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
-
+out:
        END_PROFILE(SMBwriteX);
        return;
 }
@@ -4619,7 +4673,7 @@ void reply_close(struct smb_request *req)
         */
 
        if(!fsp || (fsp->conn != conn) || (fsp->vuid != req->vuid)) {
-               reply_doserror(req, ERRDOS, ERRbadfid);
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                END_PROFILE(SMBclose);
                return;
        }
@@ -4698,7 +4752,7 @@ void reply_writeclose(struct smb_request *req)
                return;
        }
        if (!CHECK_WRITE(fsp)) {
-               reply_doserror(req, ERRDOS,ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBwriteclose);
                return;
        }
@@ -4708,13 +4762,13 @@ 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) {
-               init_strict_lock_struct(fsp, (uint32)req->smbpid,
+       if (!fsp->print_file) {
+               init_strict_lock_struct(fsp, (uint64_t)req->smbpid,
                    (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
                    &lock);
 
                if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
-                       reply_doserror(req, ERRDOS,ERRlock);
+                       reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                        END_PROFILE(SMBwriteclose);
                        return;
                }
@@ -4740,7 +4794,7 @@ void reply_writeclose(struct smb_request *req)
                 conn->num_files_open));
 
        if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
-               reply_doserror(req, ERRHRD, ERRdiskfull);
+               reply_nterror(req, NT_STATUS_DISK_FULL);
                goto strict_unlock;
        }
 
@@ -4754,7 +4808,7 @@ void reply_writeclose(struct smb_request *req)
        SSVAL(req->outbuf,smb_vwv0,nwritten);
 
 strict_unlock:
-       if (numtowrite) {
+       if (numtowrite && !fsp->print_file) {
                SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
        }
 
@@ -4798,9 +4852,9 @@ void reply_lock(struct smb_request *req)
        DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
                 fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
 
-       br_lck = do_lock(smbd_messaging_context(),
+       br_lck = do_lock(req->sconn->msg_ctx,
                        fsp,
-                       req->smbpid,
+                       (uint64_t)req->smbpid,
                        count,
                        offset,
                        WRITE_LOCK,
@@ -4853,9 +4907,9 @@ void reply_unlock(struct smb_request *req)
        count = (uint64_t)IVAL(req->vwv+1, 0);
        offset = (uint64_t)IVAL(req->vwv+3, 0);
 
-       status = do_unlock(smbd_messaging_context(),
+       status = do_unlock(req->sconn->msg_ctx,
                        fsp,
-                       req->smbpid,
+                       (uint64_t)req->smbpid,
                        count,
                        offset,
                        WINDOWS_LOCK);
@@ -4890,7 +4944,7 @@ void reply_tdis(struct smb_request *req)
 
        if (!conn) {
                DEBUG(4,("Invalid connection in tdis\n"));
-               reply_doserror(req, ERRSRV, ERRinvnid);
+               reply_nterror(req, NT_STATUS_NETWORK_NAME_DELETED);
                END_PROFILE(SMBtdis);
                return;
        }
@@ -4990,7 +5044,7 @@ void reply_printopen(struct smb_request *req)
        }
 
        if (!CAN_PRINT(conn)) {
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBsplopen);
                return;
        }
@@ -5003,7 +5057,7 @@ void reply_printopen(struct smb_request *req)
        }
 
        /* Open for exclusive use, write only. */
-       status = print_fsp_open(req, conn, NULL, req->vuid, fsp);
+       status = print_spool_open(fsp, NULL, req->vuid);
 
        if (!NT_STATUS_IS_OK(status)) {
                file_free(req, fsp);
@@ -5048,7 +5102,7 @@ void reply_printclose(struct smb_request *req)
         }
 
        if (!CAN_PRINT(conn)) {
-               reply_nterror(req, NT_STATUS_DOS(ERRSRV, ERRerror));
+               reply_force_doserror(req, ERRSRV, ERRerror);
                END_PROFILE(SMBsplclose);
                return;
        }
@@ -5096,7 +5150,7 @@ void reply_printqueue(struct smb_request *req)
           one printer - I think we should now only accept it if they
           get it right (tridge) */
        if (!CAN_PRINT(conn)) {
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBsplretq);
                return;
        }
@@ -5111,38 +5165,100 @@ void reply_printqueue(struct smb_request *req)
                 start_index, max_count));
 
        {
-               print_queue_struct *queue = NULL;
-               print_status_struct status;
-               int count = print_queue_status(SNUM(conn), &queue, &status);
-               int num_to_get = ABS(max_count);
-               int first = (max_count>0?start_index:start_index+max_count+1);
-               int i;
-
-               if (first >= count)
-                       num_to_get = 0;
-               else
-                       num_to_get = MIN(num_to_get,count-first);
+               TALLOC_CTX *mem_ctx = talloc_tos();
+               NTSTATUS status;
+               WERROR werr;
+               const char *sharename = lp_servicename(SNUM(conn));
+               struct rpc_pipe_client *cli = NULL;
+               struct policy_handle handle;
+               struct spoolss_DevmodeContainer devmode_ctr;
+               union spoolss_JobInfo *info;
+               uint32_t count;
+               uint32_t num_to_get;
+               uint32_t first;
+               uint32_t i;
+
+               ZERO_STRUCT(handle);
+
+               status = rpc_connect_spoolss_pipe(conn, &cli);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0, ("reply_printqueue: "
+                                 "could not connect to spoolss: %s\n",
+                                 nt_errstr(status)));
+                       reply_nterror(req, status);
+                       goto out;
+               }
+
+               ZERO_STRUCT(devmode_ctr);
 
+               status = rpccli_spoolss_OpenPrinter(cli, mem_ctx,
+                                               sharename,
+                                               NULL, devmode_ctr,
+                                               SEC_FLAG_MAXIMUM_ALLOWED,
+                                               &handle,
+                                               &werr);
+               if (!NT_STATUS_IS_OK(status)) {
+                       reply_nterror(req, status);
+                       goto out;
+               }
+               if (!W_ERROR_IS_OK(werr)) {
+                       reply_nterror(req, werror_to_ntstatus(werr));
+                       goto out;
+               }
 
-               for (i=first;i<first+num_to_get;i++) {
+               werr = rpccli_spoolss_enumjobs(cli, mem_ctx,
+                                              &handle,
+                                              0, /* firstjob */
+                                              0xff, /* numjobs */
+                                              2, /* level */
+                                              0, /* offered */
+                                              &count,
+                                              &info);
+               if (!W_ERROR_IS_OK(werr)) {
+                       reply_nterror(req, werror_to_ntstatus(werr));
+                       goto out;
+               }
+
+               if (max_count > 0) {
+                       first = start_index;
+               } else {
+                       first = start_index + max_count + 1;
+               }
+
+               if (first >= count) {
+                       num_to_get = first;
+               } else {
+                       num_to_get = first + MIN(ABS(max_count), count - first);
+               }
+
+               for (i = first; i < num_to_get; i++) {
                        char blob[28];
                        char *p = blob;
+                       time_t qtime = spoolss_Time_to_time_t(&info[i].info2.submitted);
+                       int qstatus;
+                       uint16_t qrapjobid = pjobid_to_rap(sharename,
+                                                       info[i].info2.job_id);
+
+                       if (info[i].info2.status == JOB_STATUS_PRINTING) {
+                               qstatus = 2;
+                       } else {
+                               qstatus = 3;
+                       }
 
-                       srv_put_dos_date2(p,0,queue[i].time);
-                       SCVAL(p,4,(queue[i].status==LPQ_PRINTING?2:3));
-                       SSVAL(p,5, queue[i].job);
-                       SIVAL(p,7,queue[i].size);
-                       SCVAL(p,11,0);
+                       srv_put_dos_date2(p, 0, qtime);
+                       SCVAL(p, 4, qstatus);
+                       SSVAL(p, 5, qrapjobid);
+                       SIVAL(p, 7, info[i].info2.size);
+                       SCVAL(p, 11, 0);
                        srvstr_push(blob, req->flags2, p+12,
-                                   queue[i].fs_user, 16, STR_ASCII);
+                                   info[i].info2.notify_name, 16, STR_ASCII);
 
                        if (message_push_blob(
                                    &req->outbuf,
                                    data_blob_const(
                                            blob, sizeof(blob))) == -1) {
                                reply_nterror(req, NT_STATUS_NO_MEMORY);
-                               END_PROFILE(SMBsplretq);
-                               return;
+                               goto out;
                        }
                }
 
@@ -5154,9 +5270,15 @@ void reply_printqueue(struct smb_request *req)
                        SSVAL(smb_buf(req->outbuf),1,28*count);
                }
 
-               SAFE_FREE(queue);
 
-               DEBUG(3,("%d entries returned in queue\n",count));
+               DEBUG(3, ("%u entries returned in queue\n",
+                         (unsigned)count));
+
+out:
+               if (cli && is_valid_policy_hnd(&handle)) {
+                       rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
+               }
+
        }
 
        END_PROFILE(SMBsplretq);
@@ -5189,14 +5311,14 @@ void reply_printwrite(struct smb_request *req)
                 return;
         }
 
-       if (!CAN_PRINT(conn)) {
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+       if (!fsp->print_file) {
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBsplwr);
                return;
        }
 
        if (!CHECK_WRITE(fsp)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBsplwr);
                return;
        }
@@ -5211,7 +5333,7 @@ void reply_printwrite(struct smb_request *req)
 
        data = (const char *)req->buf + 3;
 
-       if (write_file(req,fsp,data,-1,numtowrite) != numtowrite) {
+       if (write_file(req,fsp,data,(SMB_OFF_T)-1,numtowrite) != numtowrite) {
                reply_nterror(req, map_nt_error_from_unix(errno));
                END_PROFILE(SMBsplwr);
                return;
@@ -5290,264 +5412,6 @@ void reply_mkdir(struct smb_request *req)
        return;
 }
 
-/****************************************************************************
- Static function used by reply_rmdir to delete an entire directory
- tree recursively. Return True on ok, False on fail.
-****************************************************************************/
-
-static bool recursive_rmdir(TALLOC_CTX *ctx,
-                       connection_struct *conn,
-                       struct smb_filename *smb_dname)
-{
-       const char *dname = NULL;
-       char *talloced = NULL;
-       bool ret = True;
-       long offset = 0;
-       SMB_STRUCT_STAT st;
-       struct smb_Dir *dir_hnd;
-
-       SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
-
-       dir_hnd = OpenDir(talloc_tos(), conn, smb_dname->base_name, NULL, 0);
-       if(dir_hnd == NULL)
-               return False;
-
-       while((dname = ReadDirName(dir_hnd, &offset, &st, &talloced))) {
-               struct smb_filename *smb_dname_full = NULL;
-               char *fullname = NULL;
-               bool do_break = true;
-               NTSTATUS status;
-
-               if (ISDOT(dname) || ISDOTDOT(dname)) {
-                       TALLOC_FREE(talloced);
-                       continue;
-               }
-
-               if (!is_visible_file(conn, smb_dname->base_name, dname, &st,
-                                    false)) {
-                       TALLOC_FREE(talloced);
-                       continue;
-               }
-
-               /* Construct the full name. */
-               fullname = talloc_asprintf(ctx,
-                               "%s/%s",
-                               smb_dname->base_name,
-                               dname);
-               if (!fullname) {
-                       errno = ENOMEM;
-                       goto err_break;
-               }
-
-               status = create_synthetic_smb_fname(talloc_tos(), fullname,
-                                                   NULL, NULL,
-                                                   &smb_dname_full);
-               if (!NT_STATUS_IS_OK(status)) {
-                       goto err_break;
-               }
-
-               if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
-                       goto err_break;
-               }
-
-               if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
-                       if(!recursive_rmdir(ctx, conn, smb_dname_full)) {
-                               goto err_break;
-                       }
-                       if(SMB_VFS_RMDIR(conn,
-                                        smb_dname_full->base_name) != 0) {
-                               goto err_break;
-                       }
-               } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
-                       goto err_break;
-               }
-
-               /* Successful iteration. */
-               do_break = false;
-
-        err_break:
-               TALLOC_FREE(smb_dname_full);
-               TALLOC_FREE(fullname);
-               TALLOC_FREE(talloced);
-               if (do_break) {
-                       ret = false;
-                       break;
-               }
-       }
-       TALLOC_FREE(dir_hnd);
-       return ret;
-}
-
-/****************************************************************************
- The internals of the rmdir code - called elsewhere.
-****************************************************************************/
-
-NTSTATUS rmdir_internals(TALLOC_CTX *ctx,
-                        connection_struct *conn,
-                        struct smb_filename *smb_dname)
-{
-       int ret;
-
-       SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
-
-       /* Might be a symlink. */
-       if(SMB_VFS_LSTAT(conn, smb_dname) != 0) {
-               return map_nt_error_from_unix(errno);
-       }
-
-       if (S_ISLNK(smb_dname->st.st_ex_mode)) {
-               /* Is what it points to a directory ? */
-               if(SMB_VFS_STAT(conn, smb_dname) != 0) {
-                       return map_nt_error_from_unix(errno);
-               }
-               if (!(S_ISDIR(smb_dname->st.st_ex_mode))) {
-                       return NT_STATUS_NOT_A_DIRECTORY;
-               }
-               ret = SMB_VFS_UNLINK(conn, smb_dname);
-       } else {
-               ret = SMB_VFS_RMDIR(conn, smb_dname->base_name);
-       }
-       if (ret == 0) {
-               notify_fname(conn, NOTIFY_ACTION_REMOVED,
-                            FILE_NOTIFY_CHANGE_DIR_NAME,
-                            smb_dname->base_name);
-               return NT_STATUS_OK;
-       }
-
-       if(((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
-               /*
-                * Check to see if the only thing in this directory are
-                * vetoed files/directories. If so then delete them and
-                * retry. If we fail to delete any of them (and we *don't*
-                * do a recursive delete) then fail the rmdir.
-                */
-               SMB_STRUCT_STAT st;
-               const char *dname = NULL;
-               char *talloced = NULL;
-               long dirpos = 0;
-               struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
-                                                 smb_dname->base_name, NULL,
-                                                 0);
-
-               if(dir_hnd == NULL) {
-                       errno = ENOTEMPTY;
-                       goto err;
-               }
-
-               while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
-                                           &talloced)) != NULL) {
-                       if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0)) {
-                               TALLOC_FREE(talloced);
-                               continue;
-                       }
-                       if (!is_visible_file(conn, smb_dname->base_name, dname,
-                                            &st, false)) {
-                               TALLOC_FREE(talloced);
-                               continue;
-                       }
-                       if(!IS_VETO_PATH(conn, dname)) {
-                               TALLOC_FREE(dir_hnd);
-                               TALLOC_FREE(talloced);
-                               errno = ENOTEMPTY;
-                               goto err;
-                       }
-                       TALLOC_FREE(talloced);
-               }
-
-               /* We only have veto files/directories.
-                * Are we allowed to delete them ? */
-
-               if(!lp_recursive_veto_delete(SNUM(conn))) {
-                       TALLOC_FREE(dir_hnd);
-                       errno = ENOTEMPTY;
-                       goto err;
-               }
-
-               /* Do a recursive delete. */
-               RewindDir(dir_hnd,&dirpos);
-               while ((dname = ReadDirName(dir_hnd, &dirpos, &st,
-                                           &talloced)) != NULL) {
-                       struct smb_filename *smb_dname_full = NULL;
-                       char *fullname = NULL;
-                       bool do_break = true;
-                       NTSTATUS status;
-
-                       if (ISDOT(dname) || ISDOTDOT(dname)) {
-                               TALLOC_FREE(talloced);
-                               continue;
-                       }
-                       if (!is_visible_file(conn, smb_dname->base_name, dname,
-                                            &st, false)) {
-                               TALLOC_FREE(talloced);
-                               continue;
-                       }
-
-                       fullname = talloc_asprintf(ctx,
-                                       "%s/%s",
-                                       smb_dname->base_name,
-                                       dname);
-
-                       if(!fullname) {
-                               errno = ENOMEM;
-                               goto err_break;
-                       }
-
-                       status = create_synthetic_smb_fname(talloc_tos(),
-                                                           fullname, NULL,
-                                                           NULL,
-                                                           &smb_dname_full);
-                       if (!NT_STATUS_IS_OK(status)) {
-                               errno = map_errno_from_nt_status(status);
-                               goto err_break;
-                       }
-
-                       if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
-                               goto err_break;
-                       }
-                       if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
-                               if(!recursive_rmdir(ctx, conn,
-                                                   smb_dname_full)) {
-                                       goto err_break;
-                               }
-                               if(SMB_VFS_RMDIR(conn,
-                                       smb_dname_full->base_name) != 0) {
-                                       goto err_break;
-                               }
-                       } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
-                               goto err_break;
-                       }
-
-                       /* Successful iteration. */
-                       do_break = false;
-
-                err_break:
-                       TALLOC_FREE(fullname);
-                       TALLOC_FREE(smb_dname_full);
-                       TALLOC_FREE(talloced);
-                       if (do_break)
-                               break;
-               }
-               TALLOC_FREE(dir_hnd);
-               /* Retry the rmdir */
-               ret = SMB_VFS_RMDIR(conn, smb_dname->base_name);
-       }
-
-  err:
-
-       if (ret != 0) {
-               DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
-                        "%s\n", smb_fname_str_dbg(smb_dname),
-                        strerror(errno)));
-               return map_nt_error_from_unix(errno);
-       }
-
-       notify_fname(conn, NOTIFY_ACTION_REMOVED,
-                    FILE_NOTIFY_CHANGE_DIR_NAME,
-                    smb_dname->base_name);
-
-       return NT_STATUS_OK;
-}
-
 /****************************************************************************
  Reply to a rmdir.
 ****************************************************************************/
@@ -5559,7 +5423,9 @@ void reply_rmdir(struct smb_request *req)
        char *directory = NULL;
        NTSTATUS status;
        TALLOC_CTX *ctx = talloc_tos();
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       files_struct *fsp = NULL;
+       int info = 0;
+       struct smbd_server_connection *sconn = req->sconn;
 
        START_PROFILE(SMBrmdir);
 
@@ -5591,14 +5457,55 @@ void reply_rmdir(struct smb_request *req)
                goto out;
        }
 
-       dptr_closepath(sconn, smb_dname->base_name, req->smbpid);
-       status = rmdir_internals(ctx, conn, smb_dname);
+       status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               0,                                      /* root_dir_fid */
+               smb_dname,                              /* fname */
+               DELETE_ACCESS,                          /* access_mask */
+               (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
+                       FILE_SHARE_DELETE),
+               FILE_OPEN,                              /* create_disposition*/
+               FILE_DIRECTORY_FILE,                    /* create_options */
+               FILE_ATTRIBUTE_DIRECTORY,               /* file_attributes */
+               0,                                      /* oplock_request */
+               0,                                      /* allocation_size */
+               0,                                      /* private_flags */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp,                                   /* result */
+               &info);                                 /* pinfo */
+
+       if (!NT_STATUS_IS_OK(status)) {
+               if (open_was_deferred(req->mid)) {
+                       /* We have re-scheduled this call. */
+                       goto out;
+               }
+               reply_nterror(req, status);
+               goto out;
+       }
+
+       status = can_set_delete_on_close(fsp, FILE_ATTRIBUTE_DIRECTORY);
        if (!NT_STATUS_IS_OK(status)) {
+               close_file(req, fsp, ERROR_CLOSE);
                reply_nterror(req, status);
                goto out;
        }
 
-       reply_outbuf(req, 0, 0);
+       if (!set_delete_on_close(fsp, true, &conn->server_info->utok)) {
+               close_file(req, fsp, ERROR_CLOSE);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
+               goto out;
+       }
+
+       status = close_file(req, fsp, NORMAL_CLOSE);
+       if (!NT_STATUS_IS_OK(status)) {
+               reply_nterror(req, status);
+       } else {
+               reply_outbuf(req, 0, 0);
+       }
+
+       dptr_closepath(sconn, smb_dname->base_name, req->smbpid);
 
        DEBUG(3, ("rmdir %s\n", smb_fname_str_dbg(smb_dname)));
  out:
@@ -5774,7 +5681,7 @@ static void rename_open_files(connection_struct *conn,
        }
 
        /* Send messages to all smbd's (not ourself) that the name has changed. */
-       rename_share_filename(smbd_messaging_context(), lck, conn->connectpath,
+       rename_share_filename(conn->sconn->msg_ctx, lck, conn->connectpath,
                              smb_fname_dst);
 
 }
@@ -6069,6 +5976,21 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
                          "%s -> %s\n", smb_fname_str_dbg(fsp->fsp_name),
                          smb_fname_str_dbg(smb_fname_dst)));
 
+               if (lp_map_archive(SNUM(conn)) ||
+                   lp_store_dos_attributes(SNUM(conn))) {
+                       /* We must set the archive bit on the newly
+                          renamed file. */
+                       if (SMB_VFS_STAT(conn, smb_fname_dst) == 0) {
+                               uint32_t old_dosmode = dos_mode(conn,
+                                                       smb_fname_dst);
+                               file_set_dosmode(conn,
+                                       smb_fname_dst,
+                                       old_dosmode | FILE_ATTRIBUTE_ARCHIVE,
+                                       NULL,
+                                       true);
+                       }
+               }
+
                notify_rename(conn, fsp->is_directory, fsp->fsp_name,
                              smb_fname_dst);
 
@@ -6084,7 +6006,7 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
                 */
 
                if (create_options & FILE_DELETE_ON_CLOSE) {
-                       status = can_set_delete_on_close(fsp, True, 0);
+                       status = can_set_delete_on_close(fsp, 0);
 
                        if (NT_STATUS_IS_OK(status)) {
                                /* Note that here we set the *inital* delete on close flag,
@@ -6261,6 +6183,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        posix_pathnames ? FILE_FLAG_POSIX_SEMANTICS|0777 : 0, /* file_attributes */
                        0,                              /* oplock_request */
                        0,                              /* allocation_size */
+                       0,                              /* private_flags */
                        NULL,                           /* sd */
                        NULL,                           /* ea_list */
                        &fsp,                           /* result */
@@ -6337,8 +6260,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        continue;
                }
 
-               if(!mask_match(dname, fname_src_mask, get_Protocol(),
-                              conn->case_sensitive)) {
+               if(!mask_match(dname, fname_src_mask, conn->case_sensitive)) {
                        TALLOC_FREE(talloced);
                        continue;
                }
@@ -6400,6 +6322,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        posix_pathnames ? FILE_FLAG_POSIX_SEMANTICS|0777 : 0, /* file_attributes */
                        0,                              /* oplock_request */
                        0,                              /* allocation_size */
+                       0,                              /* private_flags */
                        NULL,                           /* sd */
                        NULL,                           /* ea_list */
                        &fsp,                           /* result */
@@ -6620,6 +6543,7 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
                if (!map_open_params_to_ntcreate(smb_fname_dst_tmp, 0, ofun,
                                                 NULL, NULL,
                                                 &new_create_disposition,
+                                                NULL,
                                                 NULL)) {
                        status = NT_STATUS_INVALID_PARAMETER;
                        goto out;
@@ -6639,6 +6563,7 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
                FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
                INTERNAL_OPEN_ONLY,                     /* oplock_request */
                0,                                      /* allocation_size */
+               0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp1,                                  /* result */
@@ -6667,6 +6592,7 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
                dosattrs,                               /* file_attributes */
                INTERNAL_OPEN_ONLY,                     /* oplock_request */
                0,                                      /* allocation_size */
+               0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp2,                                  /* result */
@@ -6777,7 +6703,7 @@ void reply_copy(struct smb_request *req)
        if (tid2 != conn->cnum) {
                /* can't currently handle inter share copies XXXX */
                DEBUG(3,("Rejecting inter-share copy\n"));
-               reply_doserror(req, ERRSRV, ERRinvdevice);
+               reply_nterror(req, NT_STATUS_BAD_DEVICE_TYPE);
                goto out;
        }
 
@@ -6816,19 +6742,19 @@ void reply_copy(struct smb_request *req)
        target_is_directory = VALID_STAT_OF_DIR(smb_fname_dst->st);
 
        if ((flags&1) && target_is_directory) {
-               reply_doserror(req, ERRDOS, ERRbadfile);
+               reply_nterror(req, NT_STATUS_NO_SUCH_FILE);
                goto out;
        }
 
        if ((flags&2) && !target_is_directory) {
-               reply_doserror(req, ERRDOS, ERRbadpath);
+               reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
                goto out;
        }
 
        if ((flags&(1<<5)) && VALID_STAT_OF_DIR(smb_fname_src->st)) {
                /* wants a tree copy! XXXX */
                DEBUG(3,("Rejecting tree copy\n"));
-               reply_doserror(req, ERRSRV, ERRerror);
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                goto out;
        }
 
@@ -6969,7 +6895,7 @@ void reply_copy(struct smb_request *req)
                                continue;
                        }
 
-                       if(!mask_match(dname, fname_src_mask, get_Protocol(),
+                       if(!mask_match(dname, fname_src_mask,
                                       conn->case_sensitive)) {
                                TALLOC_FREE(talloced);
                                continue;
@@ -7039,7 +6965,7 @@ void reply_copy(struct smb_request *req)
        }
 
        if (count == 0) {
-               reply_doserror(req, ERRDOS, error);
+               reply_nterror(req, dos_to_ntstatus(ERRDOS, error));
                goto out;
        }
 
@@ -7064,13 +6990,13 @@ void reply_copy(struct smb_request *req)
  Get a lock pid, dealing with large count requests.
 ****************************************************************************/
 
-uint32 get_lock_pid(const uint8_t *data, int data_offset,
+uint64_t get_lock_pid(const uint8_t *data, int data_offset,
                    bool large_file_format)
 {
        if(!large_file_format)
-               return (uint32)SVAL(data,SMB_LPID_OFFSET(data_offset));
+               return (uint64_t)SVAL(data,SMB_LPID_OFFSET(data_offset));
        else
-               return (uint32)SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
+               return (uint64_t)SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
 }
 
 /****************************************************************************
@@ -7221,7 +7147,7 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                          "pid %u, file %s\n",
                          (double)e->offset,
                          (double)e->count,
-                         (unsigned int)e->smbpid,
+                         (unsigned int)e->smblctx,
                          fsp_str_dbg(fsp)));
 
                if (e->brltype != UNLOCK_LOCK) {
@@ -7229,9 +7155,9 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                        return NT_STATUS_INVALID_PARAMETER;
                }
 
-               status = do_unlock(smbd_messaging_context(),
+               status = do_unlock(req->sconn->msg_ctx,
                                fsp,
-                               e->smbpid,
+                               e->smblctx,
                                e->count,
                                e->offset,
                                WINDOWS_LOCK);
@@ -7256,25 +7182,49 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
        for(i = 0; i < (int)num_locks; i++) {
                struct smbd_lock_element *e = &locks[i];
 
-               DEBUG(10,("smbd_do_locking: lock start=%.0f, len=%.0f for pid "
-                         "%u, file %s timeout = %d\n",
+               DEBUG(10,("smbd_do_locking: lock start=%.0f, len=%.0f for smblctx "
+                         "%llu, file %s timeout = %d\n",
                          (double)e->offset,
                          (double)e->count,
-                         (unsigned int)e->smbpid,
+                         (unsigned long long)e->smblctx,
                          fsp_str_dbg(fsp),
                          (int)timeout));
 
                if (type & LOCKING_ANDX_CANCEL_LOCK) {
                        struct blocking_lock_record *blr = NULL;
 
+                       if (num_locks > 1) {
+                               /*
+                                * MS-CIFS (2.2.4.32.1) states that a cancel is honored if and only
+                                * if the lock vector contains one entry. When given mutliple cancel
+                                * requests in a single PDU we expect the server to return an
+                                * error. Windows servers seem to accept the request but only
+                                * cancel the first lock.
+                                * JRA - Do what Windows does (tm) :-).
+                                */
+
+#if 0
+                               /* MS-CIFS (2.2.4.32.1) behavior. */
+                               return NT_STATUS_DOS(ERRDOS,
+                                               ERRcancelviolation);
+#else
+                               /* Windows behavior. */
+                               if (i != 0) {
+                                       DEBUG(10,("smbd_do_locking: ignoring subsequent "
+                                               "cancel request\n"));
+                                       continue;
+                               }
+#endif
+                       }
+
                        if (lp_blocking_locks(SNUM(conn))) {
 
                                /* Schedule a message to ourselves to
                                   remove the blocking lock record and
                                   return the right error. */
 
-                               blr = blocking_lock_cancel(fsp,
-                                               e->smbpid,
+                               blr = blocking_lock_cancel_smb1(fsp,
+                                               e->smblctx,
                                                e->offset,
                                                e->count,
                                                WINDOWS_LOCK,
@@ -7288,7 +7238,7 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                        }
                        /* Remove a matching pending lock. */
                        status = do_lock_cancel(fsp,
-                                               e->smbpid,
+                                               e->smblctx,
                                                e->count,
                                                e->offset,
                                                WINDOWS_LOCK,
@@ -7297,18 +7247,18 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                        bool blocking_lock = timeout ? true : false;
                        bool defer_lock = false;
                        struct byte_range_lock *br_lck;
-                       uint32_t block_smbpid;
+                       uint64_t block_smblctx;
 
-                       br_lck = do_lock(smbd_messaging_context(),
+                       br_lck = do_lock(req->sconn->msg_ctx,
                                        fsp,
-                                       e->smbpid,
+                                       e->smblctx,
                                        e->count,
                                        e->offset, 
                                        e->brltype,
                                        WINDOWS_LOCK,
                                        blocking_lock,
                                        &status,
-                                       &block_smbpid,
+                                       &block_smblctx,
                                        NULL);
 
                        if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
@@ -7320,13 +7270,23 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                                defer_lock = true;
                        }
 
-                       /* This heuristic seems to match W2K3 very well. If a
-                          lock sent with timeout of zero would fail with NT_STATUS_FILE_LOCK_CONFLICT
-                          it pretends we asked for a timeout of between 150 - 300 milliseconds as
-                          far as I can tell. Replacement for do_lock_spin(). JRA. */
-
-                       if (br_lck && lp_blocking_locks(SNUM(conn)) && !blocking_lock &&
-                                       NT_STATUS_EQUAL((status), NT_STATUS_FILE_LOCK_CONFLICT)) {
+                       /* If a lock sent with timeout of zero would fail, and
+                        * this lock has been requested multiple times,
+                        * according to brl_lock_failed() we convert this
+                        * request to a blocking lock with a timeout of between
+                        * 150 - 300 milliseconds.
+                        *
+                        * If lp_lock_spin_time() has been set to 0, we skip
+                        * this blocking retry and fail immediately.
+                        *
+                        * Replacement for do_lock_spin(). JRA. */
+
+                       if (!req->sconn->using_smb2 &&
+                           br_lck && lp_blocking_locks(SNUM(conn)) &&
+                           lp_lock_spin_time() && !blocking_lock &&
+                           NT_STATUS_EQUAL((status),
+                               NT_STATUS_FILE_LOCK_CONFLICT))
+                       {
                                defer_lock = true;
                                timeout = lp_lock_spin_time();
                        }
@@ -7342,12 +7302,12 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                                                        fsp,
                                                        timeout,
                                                        i,
-                                                       e->smbpid,
+                                                       e->smblctx,
                                                        e->brltype,
                                                        WINDOWS_LOCK,
                                                        e->offset,
                                                        e->count,
-                                                       block_smbpid)) {
+                                                       block_smblctx)) {
                                        TALLOC_FREE(br_lck);
                                        *async = true;
                                        return NT_STATUS_OK;
@@ -7379,9 +7339,9 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                for(i--; i >= 0; i--) {
                        struct smbd_lock_element *e = &locks[i];
 
-                       do_unlock(smbd_messaging_context(),
+                       do_unlock(req->sconn->msg_ctx,
                                fsp,
-                               e->smbpid,
+                               e->smblctx,
                                e->count,
                                e->offset,
                                WINDOWS_LOCK);
@@ -7444,7 +7404,7 @@ void reply_lockingX(struct smb_request *req)
                /* we don't support these - and CANCEL_LOCK makes w2k
                   and XP reboot so I don't really want to be
                   compatible! (tridge) */
-               reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
+               reply_force_doserror(req, ERRDOS, ERRnoatomiclocks);
                END_PROFILE(SMBlockingX);
                return;
        }
@@ -7487,7 +7447,7 @@ void reply_lockingX(struct smb_request *req)
                                return;
                        } else {
                                END_PROFILE(SMBlockingX);
-                               reply_doserror(req, ERRDOS, ERRlock);
+                               reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                                return;
                        }
                }
@@ -7546,7 +7506,7 @@ void reply_lockingX(struct smb_request *req)
        /* Data now points at the beginning of the list
           of smb_unlkrng structs */
        for(i = 0; i < (int)num_ulocks; i++) {
-               ulocks[i].smbpid = get_lock_pid(data, i, large_file_format);
+               ulocks[i].smblctx = get_lock_pid(data, i, large_file_format);
                ulocks[i].count = get_lock_count(data, i, large_file_format);
                ulocks[i].offset = get_lock_offset(data, i, large_file_format, &err);
                ulocks[i].brltype = UNLOCK_LOCK;
@@ -7555,8 +7515,8 @@ void reply_lockingX(struct smb_request *req)
                 * There is no error code marked "stupid client bug".... :-).
                 */
                if(err) {
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                        END_PROFILE(SMBlockingX);
-                       reply_doserror(req, ERRDOS, ERRnoaccess);
                        return;
                }
        }
@@ -7568,7 +7528,7 @@ void reply_lockingX(struct smb_request *req)
           of smb_lkrng structs */
 
        for(i = 0; i < (int)num_locks; i++) {
-               locks[i].smbpid = get_lock_pid(data, i, large_file_format);
+               locks[i].smblctx = get_lock_pid(data, i, large_file_format);
                locks[i].count = get_lock_count(data, i, large_file_format);
                locks[i].offset = get_lock_offset(data, i, large_file_format, &err);
 
@@ -7590,8 +7550,8 @@ void reply_lockingX(struct smb_request *req)
                 * There is no error code marked "stupid client bug".... :-).
                 */
                if(err) {
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                        END_PROFILE(SMBlockingX);
-                       reply_doserror(req, ERRDOS, ERRnoaccess);
                        return;
                }
        }
@@ -7632,7 +7592,7 @@ void reply_lockingX(struct smb_request *req)
 void reply_readbmpx(struct smb_request *req)
 {
        START_PROFILE(SMBreadBmpx);
-       reply_doserror(req, ERRSRV, ERRuseSTD);
+       reply_force_doserror(req, ERRSRV, ERRuseSTD);
        END_PROFILE(SMBreadBmpx);
        return;
 }
@@ -7646,7 +7606,7 @@ void reply_readbmpx(struct smb_request *req)
 void reply_readbs(struct smb_request *req)
 {
        START_PROFILE(SMBreadBs);
-       reply_doserror(req, ERRSRV, ERRuseSTD);
+       reply_force_doserror(req, ERRSRV, ERRuseSTD);
        END_PROFILE(SMBreadBs);
        return;
 }
@@ -7673,7 +7633,7 @@ void reply_setattrE(struct smb_request *req)
        fsp = file_fsp(req, SVAL(req->vwv+0, 0));
 
        if(!fsp || (fsp->conn != conn)) {
-               reply_doserror(req, ERRDOS, ERRbadfid);
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                goto out;
        }
 
@@ -7704,7 +7664,7 @@ void reply_setattrE(struct smb_request *req)
 
        status = smb_set_file_time(conn, fsp, fsp->fsp_name, &ft, true);
        if (!NT_STATUS_IS_OK(status)) {
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, status);
                goto out;
        }
 
@@ -7732,7 +7692,7 @@ void reply_setattrE(struct smb_request *req)
 void reply_writebmpx(struct smb_request *req)
 {
        START_PROFILE(SMBwriteBmpx);
-       reply_doserror(req, ERRSRV, ERRuseSTD);
+       reply_force_doserror(req, ERRSRV, ERRuseSTD);
        END_PROFILE(SMBwriteBmpx);
        return;
 }
@@ -7746,7 +7706,7 @@ void reply_writebmpx(struct smb_request *req)
 void reply_writebs(struct smb_request *req)
 {
        START_PROFILE(SMBwriteBs);
-       reply_doserror(req, ERRSRV, ERRuseSTD);
+       reply_force_doserror(req, ERRSRV, ERRuseSTD);
        END_PROFILE(SMBwriteBs);
        return;
 }
@@ -7773,7 +7733,7 @@ void reply_getattrE(struct smb_request *req)
        fsp = file_fsp(req, SVAL(req->vwv+0, 0));
 
        if(!fsp || (fsp->conn != conn)) {
-               reply_doserror(req, ERRDOS, ERRbadfid);
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                END_PROFILE(SMBgetattrE);
                return;
        }