s3: Fix shadow copy display on Windows 7
[samba.git] / source / smbd / nttrans.c
index 1fbb681c720f68dd5b5b3ba5138f8669ce8b0954..c392380b5ec7e39adff73fe2a2c8b991c71dc464 100644 (file)
 
 extern int max_send;
 extern enum protocol_types Protocol;
-extern struct current_user current_user;
-
-static const char *known_nt_pipes[] = {
-       "\\LANMAN",
-       "\\srvsvc",
-       "\\samr",
-       "\\wkssvc",
-       "\\NETLOGON",
-       "\\ntlsa",
-       "\\ntsvcs",
-       "\\lsass",
-       "\\lsarpc",
-       "\\winreg",
-       "\\initshutdown",
-       "\\spoolss",
-       "\\netdfs",
-       "\\rpcecho",
-        "\\svcctl",
-       "\\eventlog",
-       "\\unixinfo",
-       NULL
-};
+extern const struct generic_mapping file_generic_mapping;
 
 static char *nttrans_realloc(char **ptr, size_t size)
 {
@@ -66,7 +45,8 @@ static char *nttrans_realloc(char **ptr, size_t size)
  HACK ! Always assumes smb_setup field is zero.
 ****************************************************************************/
 
-void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
+void send_nt_replies(connection_struct *conn,
+                       struct smb_request *req, NTSTATUS nt_error,
                     char *params, int paramsize,
                     char *pdata, int datasize)
 {
@@ -112,14 +92,11 @@ void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
                                    + alignment_offset
                                    + data_alignment_offset);
 
-       /*
-        * useable_space can never be more than max_send minus the
-        * alignment offset.
-        */
-
-       useable_space = MIN(useable_space,
-                               max_send - (alignment_offset+data_alignment_offset));
-
+       if (useable_space < 0) {
+               DEBUG(0, ("send_nt_replies failed sanity useable_space "
+                         "= %d!!!", useable_space));
+               exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
+       }
 
        while (params_to_send || data_to_send) {
 
@@ -127,8 +104,7 @@ void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
                 * Calculate whether we will totally or partially fill this packet.
                 */
 
-               total_sent_thistime = params_to_send + data_to_send +
-                                       alignment_offset + data_alignment_offset;
+               total_sent_thistime = params_to_send + data_to_send;
 
                /*
                 * We can never send more than useable_space.
@@ -136,7 +112,9 @@ void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
 
                total_sent_thistime = MIN(total_sent_thistime, useable_space);
 
-               reply_outbuf(req, 18, total_sent_thistime);
+               reply_outbuf(req, 18,
+                            total_sent_thistime + alignment_offset
+                            + data_alignment_offset);
 
                /*
                 * Set total params and data to be sent.
@@ -242,8 +220,10 @@ void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
 
                /* Send the packet */
                show_msg((char *)req->outbuf);
-               if (!send_smb(smbd_server_fd(),(char *)req->outbuf)) {
-                       exit_server_cleanly("send_nt_replies: send_smb failed.");
+               if (!srv_send_smb(smbd_server_fd(),
+                               (char *)req->outbuf,
+                               IS_CONN_ENCRYPTED(conn))) {
+                       exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
                }
 
                TALLOC_FREE(req->outbuf);
@@ -261,13 +241,16 @@ void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
                if(params_to_send < 0 || data_to_send < 0) {
                        DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
                                params_to_send, data_to_send));
-                       return;
+                       exit_server_cleanly("send_nt_replies: internal error");
                }
        }
 }
 
 /****************************************************************************
  Is it an NTFS stream name ?
+ An NTFS file name is <path>.<extention>:<stream name>:<stream type>
+ $DATA can be used as both a stream name and a stream type. A missing stream
+ name or type implies $DATA.
 ****************************************************************************/
 
 bool is_ntfs_stream_name(const char *fname)
@@ -286,25 +269,12 @@ static void nt_open_pipe(char *fname, connection_struct *conn,
                         struct smb_request *req, int *ppnum)
 {
        smb_np_struct *p = NULL;
-       int i;
 
        DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
 
        /* See if it is one we want to handle. */
 
-       if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
-               reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
-                               ERRDOS, ERRbadpipe);
-               return;
-       }
-
-       for( i = 0; known_nt_pipes[i]; i++ ) {
-               if( strequal(fname,known_nt_pipes[i])) {
-                       break;
-               }
-       }
-
-       if ( known_nt_pipes[i] == NULL ) {
+       if (!is_known_pipename(fname)) {
                reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
                                ERRDOS, ERRbadpipe);
                return;
@@ -410,8 +380,9 @@ static void do_ntcreate_pipe_open(connection_struct *conn,
  Reply to an NT create and X call.
 ****************************************************************************/
 
-void reply_ntcreate_and_X(connection_struct *conn, struct smb_request *req)
+void reply_ntcreate_and_X(struct smb_request *req)
 {
+       connection_struct *conn = req->conn;
        char *fname = NULL;
        uint32 flags;
        uint32 access_mask;
@@ -482,6 +453,12 @@ void reply_ntcreate_and_X(connection_struct *conn, struct smb_request *req)
                        (unsigned int)root_dir_fid,
                        fname));
 
+       /*
+        * we need to remove ignored bits when they come directly from the client
+        * because we reuse some of them for internal stuff
+        */
+       create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
+
        /*
         * If it's an IPC, use the pipe handler.
         */
@@ -491,11 +468,10 @@ void reply_ntcreate_and_X(connection_struct *conn, struct smb_request *req)
                        do_ntcreate_pipe_open(conn, req);
                        END_PROFILE(SMBntcreateX);
                        return;
-               } else {
-                       reply_doserror(req, ERRDOS, ERRnoaccess);
-                       END_PROFILE(SMBntcreateX);
-                       return;
                }
+               reply_doserror(req, ERRDOS, ERRnoaccess);
+               END_PROFILE(SMBntcreateX);
+               return;
        }
 
        oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
@@ -551,7 +527,7 @@ void reply_ntcreate_and_X(connection_struct *conn, struct smb_request *req)
        }
 
        file_len = sbuf.st_size;
-       fattr = dos_mode(conn,fname,&sbuf);
+       fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
        if (fattr == 0) {
                fattr = FILE_ATTRIBUTE_NORMAL;
        }
@@ -619,7 +595,7 @@ void reply_ntcreate_and_X(connection_struct *conn, struct smb_request *req)
                uint32 perms = 0;
                p += 25;
                if (fsp->is_directory
-                   || can_write_to_file(conn, fname, &sbuf)) {
+                   || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
                        perms = FILE_GENERIC_ALL;
                } else {
                        perms = FILE_GENERIC_READ|FILE_EXECUTE;
@@ -726,7 +702,7 @@ static void do_nt_transact_create_pipe(connection_struct *conn,
        DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
 
        /* Send the required number of replies */
-       send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
+       send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
 
        return;
 }
@@ -751,28 +727,24 @@ static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
                return status;
        }
 
-       if (psd->owner_sid==0) {
+       if (psd->owner_sid == NULL) {
                security_info_sent &= ~OWNER_SECURITY_INFORMATION;
        }
-       if (psd->group_sid==0) {
+       if (psd->group_sid == NULL) {
                security_info_sent &= ~GROUP_SECURITY_INFORMATION;
        }
-       if (psd->sacl==0) {
-               security_info_sent &= ~SACL_SECURITY_INFORMATION;
-       }
-       if (psd->dacl==0) {
-               security_info_sent &= ~DACL_SECURITY_INFORMATION;
-       }
 
-       if (fsp->fh->fd != -1) {
-               status = SMB_VFS_FSET_NT_ACL(fsp, fsp->fh->fd,
-                                            security_info_sent, psd);
-       }
-       else {
-               status = SMB_VFS_SET_NT_ACL(fsp, fsp->fsp_name,
-                                           security_info_sent, psd);
+       /* Convert all the generic bits. */
+       security_acl_map_generic(psd->dacl, &file_generic_mapping);
+       security_acl_map_generic(psd->sacl, &file_generic_mapping);
+
+       if (DEBUGLEVEL >= 10) {
+               DEBUG(10,("set_sd for file %s\n", fsp->fsp_name ));
+               NDR_PRINT_DEBUG(security_descriptor, psd);
        }
 
+       status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
+
        TALLOC_FREE(psd);
 
        return status;
@@ -865,10 +837,9 @@ static void call_nt_transact_create(connection_struct *conn,
                                ppparams, parameter_count,
                                ppdata, data_count);
                        return;
-               } else {
-                       reply_doserror(req, ERRDOS, ERRnoaccess);
-                       return;
                }
+               reply_doserror(req, ERRDOS, ERRnoaccess);
+               return;
        }
 
        /*
@@ -895,6 +866,12 @@ static void call_nt_transact_create(connection_struct *conn,
        allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
 #endif
 
+       /*
+        * we need to remove ignored bits when they come directly from the client
+        * because we reuse some of them for internal stuff
+        */
+       create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
+
        /* Ensure the data_len is correct for the sd and ea values given. */
        if ((ea_len + sd_len > data_count)
            || (ea_len > data_count) || (sd_len > data_count)
@@ -1001,7 +978,7 @@ static void call_nt_transact_create(connection_struct *conn,
        }
 
        file_len = sbuf.st_size;
-       fattr = dos_mode(conn,fname,&sbuf);
+       fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
        if (fattr == 0) {
                fattr = FILE_ATTRIBUTE_NORMAL;
        }
@@ -1069,7 +1046,7 @@ static void call_nt_transact_create(connection_struct *conn,
                uint32 perms = 0;
                p += 25;
                if (fsp->is_directory
-                   || can_write_to_file(conn, fname, &sbuf)) {
+                   || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
                        perms = FILE_GENERIC_ALL;
                } else {
                        perms = FILE_GENERIC_READ|FILE_EXECUTE;
@@ -1077,10 +1054,10 @@ static void call_nt_transact_create(connection_struct *conn,
                SIVAL(p,0,perms);
        }
 
-       DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
+       DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
 
        /* Send the required number of replies */
-       send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
+       send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
 
        return;
 }
@@ -1090,7 +1067,7 @@ static void call_nt_transact_create(connection_struct *conn,
  conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
-void reply_ntcancel(connection_struct *conn, struct smb_request *req)
+void reply_ntcancel(struct smb_request *req)
 {
        /*
         * Go through and cancel any pending change notifies.
@@ -1099,7 +1076,7 @@ void reply_ntcancel(connection_struct *conn, struct smb_request *req)
        START_PROFILE(SMBntcancel);
        remove_pending_change_notify_requests_by_mid(req->mid);
        remove_pending_lock_requests_by_mid(req->mid);
-       srv_cancel_sign_response(req->mid);
+       srv_cancel_sign_response(req->mid, true);
 
        DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
 
@@ -1227,7 +1204,7 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
        close_file(fsp1,NORMAL_CLOSE);
 
        /* Ensure the modtime is set correctly on the destination file. */
-       fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
+       set_close_write_time(fsp2, get_mtimespec(&sbuf1));
 
        status = close_file(fsp2,NORMAL_CLOSE);
 
@@ -1252,8 +1229,9 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
  Reply to a NT rename request.
 ****************************************************************************/
 
-void reply_ntrename(connection_struct *conn, struct smb_request *req)
+void reply_ntrename(struct smb_request *req)
 {
+       connection_struct *conn = req->conn;
        char *oldname = NULL;
        char *newname = NULL;
        char *p;
@@ -1285,13 +1263,6 @@ void reply_ntrename(connection_struct *conn, struct smb_request *req)
                return;
        }
 
-       if( is_ntfs_stream_name(oldname)) {
-               /* Can't rename a stream. */
-               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
-               END_PROFILE(SMBntrename);
-               return;
-       }
-
        if (ms_has_wild(oldname)) {
                reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
                END_PROFILE(SMBntrename);
@@ -1340,13 +1311,20 @@ void reply_ntrename(connection_struct *conn, struct smb_request *req)
                return;
        }
 
+       /* The new name must begin with a ':' if the old name is a stream. */
+       if (is_ntfs_stream_name(oldname) && (newname[0] != ':')) {
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               END_PROFILE(SMBntrename);
+               return;
+       }
+
        DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
 
        switch(rename_type) {
                case RENAME_FLAG_RENAME:
                        status = rename_internals(ctx, conn, req, oldname,
                                        newname, attrs, False, src_has_wcard,
-                                       dest_has_wcard);
+                                       dest_has_wcard, DELETE_ACCESS);
                        break;
                case RENAME_FLAG_HARD_LINK:
                        if (src_has_wcard || dest_has_wcard) {
@@ -1474,7 +1452,7 @@ static void call_nt_transact_notify_change(connection_struct *conn,
                 * here.
                 */
 
-               change_notify_reply(req->inbuf, max_param_count, fsp->notify);
+               change_notify_reply(fsp->conn, req->inbuf, max_param_count, fsp->notify);
 
                /*
                 * change_notify_reply() above has independently sent its
@@ -1487,7 +1465,9 @@ static void call_nt_transact_notify_change(connection_struct *conn,
         * No changes pending, queue the request
         */
 
-       status = change_notify_add_request(req->inbuf, max_param_count, filter,
+       status = change_notify_add_request(req,
+                       max_param_count,
+                       filter,
                        recursive, fsp);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
@@ -1509,7 +1489,6 @@ static void call_nt_transact_rename(connection_struct *conn,
        char *params = *ppparams;
        char *new_name = NULL;
        files_struct *fsp = NULL;
-       bool replace_if_exists = False;
        bool dest_has_wcard = False;
        NTSTATUS status;
        TALLOC_CTX *ctx = talloc_tos();
@@ -1520,8 +1499,7 @@ static void call_nt_transact_rename(connection_struct *conn,
        }
 
        fsp = file_fsp(SVAL(params, 0));
-       replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                return;
        }
        srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
@@ -1532,31 +1510,13 @@ static void call_nt_transact_rename(connection_struct *conn,
                return;
        }
 
-       status = rename_internals(ctx,
-                       conn,
-                       req,
-                       fsp->fsp_name,
-                       new_name,
-                       0,
-                       replace_if_exists,
-                       False,
-                       dest_has_wcard);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               if (open_was_deferred(req->mid)) {
-                       /* We have re-scheduled this call. */
-                       return;
-               }
-               reply_nterror(req, status);
-               return;
-       }
-
        /*
-        * Rename was successful.
+        * W2K3 ignores this request as the RAW-RENAME test
+        * demonstrates, so we do.
         */
-       send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
+       send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
 
-       DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
+       DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
                 fsp->fsp_name, new_name));
 
        return;
@@ -1598,7 +1558,6 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
        SEC_DESC *psd = NULL;
        size_t sd_size;
        uint32 security_info_wanted;
-       TALLOC_CTX *frame;
        files_struct *fsp = NULL;
        NTSTATUS status;
        DATA_BLOB blob;
@@ -1625,8 +1584,6 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
                return;
        }
 
-       frame = talloc_stackframe();
-
        /*
         * Get the permissions to return.
         */
@@ -1634,32 +1591,37 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
        if (!lp_nt_acl_support(SNUM(conn))) {
                status = get_null_nt_acl(talloc_tos(), &psd);
        } else {
-               if (fsp->fh->fd != -1) {
-                       status = SMB_VFS_FGET_NT_ACL(
-                               fsp, fsp->fh->fd, security_info_wanted, &psd);
-               }
-               else {
-                       status = SMB_VFS_GET_NT_ACL(
-                               fsp, fsp->fsp_name, security_info_wanted, &psd);
-               }
+               status = SMB_VFS_FGET_NT_ACL(
+                       fsp, security_info_wanted, &psd);
        }
-
        if (!NT_STATUS_IS_OK(status)) {
-               TALLOC_FREE(frame);
                reply_nterror(req, status);
                return;
        }
 
-       sd_size = sec_desc_size(psd);
+       /* If the SACL/DACL is NULL, but was requested, we mark that it is
+        * present in the reply to match Windows behavior */
+       if (psd->sacl == NULL &&
+           security_info_wanted & SACL_SECURITY_INFORMATION)
+               psd->type |= SEC_DESC_SACL_PRESENT;
+       if (psd->dacl == NULL &&
+           security_info_wanted & DACL_SECURITY_INFORMATION)
+               psd->type |= SEC_DESC_DACL_PRESENT;
+
+       sd_size = ndr_size_security_descriptor(psd, 0);
 
        DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
 
+       if (DEBUGLEVEL >= 10) {
+               DEBUG(10,("call_nt_transact_query_security_desc for file %s\n", fsp->fsp_name ));
+               NDR_PRINT_DEBUG(security_descriptor, psd);
+       }
+
        SIVAL(params,0,(uint32)sd_size);
 
        if (max_data_count < sd_size) {
-               send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
+               send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
                                params, 4, *ppdata, 0);
-               TALLOC_FREE(frame);
                return;
        }
 
@@ -1669,7 +1631,6 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
 
        data = nttrans_realloc(ppdata, sd_size);
        if(data == NULL) {
-               TALLOC_FREE(frame);
                reply_doserror(req, ERRDOS, ERRnomem);
                return;
        }
@@ -1678,7 +1639,6 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
                                   &blob.data, &blob.length);
 
        if (!NT_STATUS_IS_OK(status)) {
-               TALLOC_FREE(frame);
                reply_nterror(req, status);
                return;
        }
@@ -1686,9 +1646,8 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
        SMB_ASSERT(sd_size == blob.length);
        memcpy(data, blob.data, sd_size);
 
-       send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
+       send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
 
-       TALLOC_FREE(frame);
        return;
 }
 
@@ -1744,7 +1703,7 @@ static void call_nt_transact_set_security_desc(connection_struct *conn,
        }
 
   done:
-       send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
+       send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
        return;
 }
 
@@ -1793,7 +1752,7 @@ static void call_nt_transact_ioctl(connection_struct *conn,
                   so we can know if we need to pre-allocate or not */
 
                DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
-               send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
+               send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
                return;
 
        case FSCTL_CREATE_OR_GET_OBJECT_ID:
@@ -1806,7 +1765,7 @@ static void call_nt_transact_ioctl(connection_struct *conn,
 
                DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
 
-               if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
+               if (!fsp_belongs_conn(conn, req, fsp)) {
                        return;
                }
 
@@ -1819,7 +1778,7 @@ static void call_nt_transact_ioctl(connection_struct *conn,
                push_file_id_16(pdata, &fsp->file_id);
                memcpy(pdata+16,create_volume_objectid(conn,objid),16);
                push_file_id_16(pdata+32, &fsp->file_id);
-               send_nt_replies(req, NT_STATUS_OK, NULL, 0,
+               send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
                                pdata, data_count);
                return;
        }
@@ -1861,7 +1820,7 @@ static void call_nt_transact_ioctl(connection_struct *conn,
                uint32 i;
                char *cur_pdata;
 
-               if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
+               if (!fsp_belongs_conn(conn, req, fsp)) {
                        return;
                }
 
@@ -1945,7 +1904,7 @@ static void call_nt_transact_ioctl(connection_struct *conn,
                }
 
                /* needed_data_count 4 bytes */
-               SIVAL(pdata,8,labels_data_count);
+               SIVAL(pdata, 8, labels_data_count+4);
 
                cur_pdata+=12;
 
@@ -1964,7 +1923,7 @@ static void call_nt_transact_ioctl(connection_struct *conn,
 
                talloc_destroy(shadow_data->mem_ctx);
 
-               send_nt_replies(req, NT_STATUS_OK, NULL, 0,
+               send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
                                pdata, data_count);
 
                return;
@@ -1984,7 +1943,7 @@ static void call_nt_transact_ioctl(connection_struct *conn,
 
                DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
 
-               if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
+               if (!fsp_belongs_conn(conn, req, fsp)) {
                        return;
                }
 
@@ -2020,7 +1979,7 @@ static void call_nt_transact_ioctl(connection_struct *conn,
                 */
 
                /* this works for now... */
-               send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
+               send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
                return;
        }
        default:
@@ -2069,9 +2028,10 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
        ZERO_STRUCT(qt);
 
        /* access check */
-       if (current_user.ut.uid != 0) {
-               DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
-                       lp_servicename(SNUM(conn)),conn->user));
+       if (conn->server_info->utok.uid != 0) {
+               DEBUG(1,("get_user_quota: access_denied service [%s] user "
+                        "[%s]\n", lp_servicename(SNUM(conn)),
+                        conn->server_info->unix_name));
                reply_doserror(req, ERRDOS, ERRnoaccess);
                return;
        }
@@ -2088,7 +2048,7 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
 
        /* maybe we can check the quota_fnum */
        fsp = file_fsp(SVAL(params,0));
-       if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
+       if (!check_fsp_ntquota_handle(conn, req, fsp)) {
                DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
                reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return;
@@ -2097,7 +2057,7 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
        /* the NULL pointer checking for fsp->fake_file_handle->pd
         * is done by CHECK_NTQUOTA_HANDLE_OK()
         */
-       qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
+       qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
 
        level = SVAL(params,2);
 
@@ -2174,7 +2134,8 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                        for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
                                tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
 
-                               sid_len = sid_size(&tmp_list->quotas->sid);
+                               sid_len = ndr_size_dom_sid(
+                                       &tmp_list->quotas->sid, 0);
                                entry_len = 40 + sid_len;
 
                                /* nextoffset entry 4 bytes */
@@ -2305,7 +2266,7 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                        break;
        }
 
-       send_nt_replies(req, nt_status, params, param_len,
+       send_nt_replies(conn, req, nt_status, params, param_len,
                        pdata, data_len);
 }
 
@@ -2334,9 +2295,10 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
        ZERO_STRUCT(qt);
 
        /* access check */
-       if (current_user.ut.uid != 0) {
-               DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
-                       lp_servicename(SNUM(conn)),conn->user));
+       if (conn->server_info->utok.uid != 0) {
+               DEBUG(1,("set_user_quota: access_denied service [%s] user "
+                        "[%s]\n", lp_servicename(SNUM(conn)),
+                        conn->server_info->unix_name));
                reply_doserror(req, ERRDOS, ERRnoaccess);
                return;
        }
@@ -2353,7 +2315,7 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
 
        /* maybe we can check the quota_fnum */
        fsp = file_fsp(SVAL(params,0));
-       if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
+       if (!check_fsp_ntquota_handle(conn, req, fsp)) {
                DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
                reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return;
@@ -2435,7 +2397,7 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
                return;
        }
 
-       send_nt_replies(req, NT_STATUS_OK, params, param_len,
+       send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
                        pdata, data_len);
 }
 #endif /* HAVE_SYS_QUOTAS */
@@ -2572,16 +2534,18 @@ static void handle_nttrans(connection_struct *conn,
  Reply to a SMBNTtrans.
 ****************************************************************************/
 
-void reply_nttrans(connection_struct *conn, struct smb_request *req)
+void reply_nttrans(struct smb_request *req)
 {
-       uint32 pscnt;
-       uint32 psoff;
-       uint32 dscnt;
-       uint32 dsoff;
+       connection_struct *conn = req->conn;
+       uint32_t pscnt;
+       uint32_t psoff;
+       uint32_t dscnt;
+       uint32_t dsoff;
        uint16 function_code;
        NTSTATUS result;
        struct trans_state *state;
-       int size;
+       uint32_t size;
+       uint32_t av_size;
 
        START_PROFILE(SMBnttrans);
 
@@ -2592,6 +2556,7 @@ void reply_nttrans(connection_struct *conn, struct smb_request *req)
        }
 
        size = smb_len(req->inbuf) + 4;
+       av_size = smb_len(req->inbuf);
        pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
        psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
        dscnt = IVAL(req->inbuf,smb_nt_DataCount);
@@ -2612,7 +2577,7 @@ void reply_nttrans(connection_struct *conn, struct smb_request *req)
                return;
        }
 
-       if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
+       if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
                reply_doserror(req, ERRSRV, ERRaccess);
                END_PROFILE(SMBnttrans);
                return;
@@ -2667,12 +2632,17 @@ void reply_nttrans(connection_struct *conn, struct smb_request *req)
                        END_PROFILE(SMBnttrans);
                        return;
                }
-               if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
+
+               if (dscnt > state->total_data ||
+                               dsoff+dscnt < dsoff) {
                        goto bad_param;
-               if ((smb_base(req->inbuf)+dsoff+dscnt
-                    > (char *)req->inbuf + size) ||
-                   (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
+               }
+
+               if (dsoff > av_size ||
+                               dscnt > av_size ||
+                               dsoff+dscnt > av_size) {
                        goto bad_param;
+               }
 
                memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
        }
@@ -2689,12 +2659,17 @@ void reply_nttrans(connection_struct *conn, struct smb_request *req)
                        END_PROFILE(SMBnttrans);
                        return;
                }
-               if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
+
+               if (pscnt > state->total_param ||
+                               psoff+pscnt < psoff) {
                        goto bad_param;
-               if ((smb_base(req->inbuf)+psoff+pscnt
-                    > (char *)req->inbuf + size) ||
-                   (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
+               }
+
+               if (psoff > av_size ||
+                               pscnt > av_size ||
+                               psoff+pscnt > av_size) {
                        goto bad_param;
+               }
 
                memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
        }
@@ -2763,12 +2738,13 @@ void reply_nttrans(connection_struct *conn, struct smb_request *req)
  Reply to a SMBnttranss
  ****************************************************************************/
 
-void reply_nttranss(connection_struct *conn, struct smb_request *req)
+void reply_nttranss(struct smb_request *req)
 {
-       unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
+       connection_struct *conn = req->conn;
+       uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
        struct trans_state *state;
-
-       int size;
+       uint32_t av_size;
+       uint32_t size;
 
        START_PROFILE(SMBnttranss);
 
@@ -2805,6 +2781,7 @@ void reply_nttranss(connection_struct *conn, struct smb_request *req)
        }
 
        size = smb_len(req->inbuf) + 4;
+       av_size = smb_len(req->inbuf);
 
        pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
        poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
@@ -2822,38 +2799,38 @@ void reply_nttranss(connection_struct *conn, struct smb_request *req)
                goto bad_param;
 
        if (pcnt) {
-               if (pdisp+pcnt > state->total_param)
-                       goto bad_param;
-               if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
-                       goto bad_param;
-               if (pdisp > state->total_param)
+               if (pdisp > state->total_param ||
+                               pcnt > state->total_param ||
+                               pdisp+pcnt > state->total_param ||
+                               pdisp+pcnt < pdisp) {
                        goto bad_param;
-               if ((smb_base(req->inbuf) + poff + pcnt
-                    > (char *)req->inbuf + size) ||
-                   (smb_base(req->inbuf) + poff + pcnt
-                    < smb_base(req->inbuf)))
-                       goto bad_param;
-               if (state->param + pdisp < state->param)
+               }
+
+               if (poff > av_size ||
+                               pcnt > av_size ||
+                               poff+pcnt > av_size ||
+                               poff+pcnt < poff) {
                        goto bad_param;
+               }
 
                memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
                       pcnt);
        }
 
        if (dcnt) {
-               if (ddisp+dcnt > state->total_data)
-                       goto bad_param;
-               if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
+               if (ddisp > state->total_data ||
+                               dcnt > state->total_data ||
+                               ddisp+dcnt > state->total_data ||
+                               ddisp+dcnt < ddisp) {
                        goto bad_param;
-               if (ddisp > state->total_data)
-                       goto bad_param;
-               if ((smb_base(req->inbuf) + doff + dcnt
-                    > (char *)req->inbuf + size) ||
-                   (smb_base(req->inbuf) + doff + dcnt
-                    < smb_base(req->inbuf)))
-                       goto bad_param;
-               if (state->data + ddisp < state->data)
+               }
+
+               if (doff > av_size ||
+                               dcnt > av_size ||
+                               doff+dcnt > av_size ||
+                               doff+dcnt < doff) {
                        goto bad_param;
+               }
 
                memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
                       dcnt);