Fix a debug message in send_nt_replies()
[kai/samba.git] / source3 / smbd / nttrans.c
index a51f3afd82749715a109677990368fc0f14c4ee5..b78c946388eff77de8e12ef11b802108eb5fd240 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)
 {
@@ -113,14 +92,14 @@ void send_nt_replies(connection_struct *conn,
                                    + 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) {
+               char *msg = talloc_asprintf(
+                       talloc_tos(),
+                       "send_nt_replies failed sanity useable_space = %d!!!",
+                       useable_space);
+               DEBUG(0, ("%s\n", msg));
+               exit_server_cleanly(msg);
+       }
 
        while (params_to_send || data_to_send) {
 
@@ -128,8 +107,7 @@ void send_nt_replies(connection_struct *conn,
                 * 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.
@@ -137,7 +115,9 @@ void send_nt_replies(connection_struct *conn,
 
                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.
@@ -264,13 +244,16 @@ void send_nt_replies(connection_struct *conn,
                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)
@@ -288,26 +271,14 @@ bool is_ntfs_stream_name(const char *fname)
 static void nt_open_pipe(char *fname, connection_struct *conn,
                         struct smb_request *req, int *ppnum)
 {
-       smb_np_struct *p = NULL;
-       int i;
+       files_struct *fsp;
+       NTSTATUS status;
 
        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;
@@ -318,19 +289,13 @@ static void nt_open_pipe(char *fname, connection_struct *conn,
 
        DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
 
-       p = open_rpc_pipe_p(fname, conn, req->vuid);
-       if (!p) {
-               reply_doserror(req, ERRSRV, ERRnofids);
+       status = np_open(req, conn, fname, &fsp);
+       if (!NT_STATUS_IS_OK(status)) {
+               reply_nterror(req, status);
                return;
        }
 
-       /* TODO: Add pipe to db */
-
-       if ( !store_pipe_opendb( p ) ) {
-               DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
-       }
-
-       *ppnum = p->pnum;
+       *ppnum = fsp->fnum;
        return;
 }
 
@@ -424,7 +389,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
        uint32 create_disposition;
        uint32 create_options;
        uint16 root_dir_fid;
-       SMB_BIG_UINT allocation_size;
+       uint64_t allocation_size;
        /* Breakout the oplock request bits so we can set the
           reply bits separately. */
        uint32 fattr=0;
@@ -456,10 +421,10 @@ void reply_ntcreate_and_X(struct smb_request *req)
        create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
        root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
 
-       allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,
+       allocation_size = (uint64_t)IVAL(req->inbuf,
                                             smb_ntcreate_AllocationSize);
 #ifdef LARGE_SMB_OFF_T
-       allocation_size |= (((SMB_BIG_UINT)IVAL(
+       allocation_size |= (((uint64_t)IVAL(
                                     req->inbuf,
                                     smb_ntcreate_AllocationSize + 4)) << 32);
 #endif
@@ -486,6 +451,12 @@ void reply_ntcreate_and_X(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.
         */
@@ -495,11 +466,10 @@ void reply_ntcreate_and_X(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;
@@ -555,7 +525,7 @@ void reply_ntcreate_and_X(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;
        }
@@ -623,7 +593,7 @@ void reply_ntcreate_and_X(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;
@@ -768,14 +738,11 @@ static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
                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);
+
+       status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
 
        TALLOC_FREE(psd);
 
@@ -850,7 +817,7 @@ static void call_nt_transact_create(connection_struct *conn,
        struct ea_list *ea_list = NULL;
        NTSTATUS status;
        size_t param_len;
-       SMB_BIG_UINT allocation_size;
+       uint64_t allocation_size;
        int oplock_request;
        uint8_t oplock_granted;
        TALLOC_CTX *ctx = talloc_tos();
@@ -869,10 +836,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;
        }
 
        /*
@@ -894,11 +860,17 @@ static void call_nt_transact_create(connection_struct *conn,
        sd_len = IVAL(params,36);
        ea_len = IVAL(params,40);
        root_dir_fid = (uint16)IVAL(params,4);
-       allocation_size = (SMB_BIG_UINT)IVAL(params,12);
+       allocation_size = (uint64_t)IVAL(params,12);
 #ifdef LARGE_SMB_OFF_T
-       allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
+       allocation_size |= (((uint64_t)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)
@@ -1005,7 +977,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;
        }
@@ -1073,7 +1045,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;
@@ -1081,7 +1053,7 @@ 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(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
@@ -1214,7 +1186,7 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                        &info, &fsp2);
 
        if (!NT_STATUS_IS_OK(status)) {
-               close_file(fsp1,ERROR_CLOSE);
+               close_file(NULL, fsp1, ERROR_CLOSE);
                return status;
        }
 
@@ -1228,12 +1200,12 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
         * Thus we don't look at the error return from the
         * close of fsp1.
         */
-       close_file(fsp1,NORMAL_CLOSE);
+       close_file(NULL, 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);
+       status = close_file(NULL, fsp2, NORMAL_CLOSE);
 
        /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
           creates the file. This isn't the correct thing to do in the copy
@@ -1351,7 +1323,7 @@ void reply_ntrename(struct smb_request *req)
                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) {
@@ -1425,7 +1397,7 @@ static void call_nt_transact_notify_change(connection_struct *conn,
                return;
        }
 
-       fsp = file_fsp(SVAL(setup,4));
+       fsp = file_fsp(req, SVAL(setup,4));
        filter = IVAL(setup, 0);
        recursive = (SVAL(setup, 6) != 0) ? True : False;
 
@@ -1516,7 +1488,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();
@@ -1526,9 +1497,8 @@ static void call_nt_transact_rename(connection_struct *conn,
                return;
        }
 
-       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)) {
+       fsp = file_fsp(req, SVAL(params, 0));
+       if (!check_fsp(conn, req, fsp)) {
                return;
        }
        srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
@@ -1539,31 +1509,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(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;
@@ -1605,7 +1557,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;
@@ -1615,7 +1566,7 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
                return;
        }
 
-       fsp = file_fsp(SVAL(params,0));
+       fsp = file_fsp(req, SVAL(params,0));
        if(!fsp) {
                reply_doserror(req, ERRDOS, ERRbadfid);
                return;
@@ -1632,8 +1583,6 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
                return;
        }
 
-       frame = talloc_stackframe();
-
        /*
         * Get the permissions to return.
         */
@@ -1641,18 +1590,11 @@ 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(
-                               conn, 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;
        }
@@ -1666,7 +1608,6 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
        if (max_data_count < sd_size) {
                send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
                                params, 4, *ppdata, 0);
-               TALLOC_FREE(frame);
                return;
        }
 
@@ -1676,7 +1617,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;
        }
@@ -1685,7 +1625,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;
        }
@@ -1695,7 +1634,6 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
 
        send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
 
-       TALLOC_FREE(frame);
        return;
 }
 
@@ -1724,7 +1662,7 @@ static void call_nt_transact_set_security_desc(connection_struct *conn,
                return;
        }
 
-       if((fsp = file_fsp(SVAL(params,0))) == NULL) {
+       if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
                reply_doserror(req, ERRDOS, ERRbadfid);
                return;
        }
@@ -1788,7 +1726,7 @@ static void call_nt_transact_ioctl(connection_struct *conn,
        DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
                 function, fidnum, isFSctl, compfilter));
 
-       fsp=file_fsp(fidnum);
+       fsp=file_fsp(req, fidnum);
        /* this check is done in each implemented function case for now
           because I don't want to break anything... --metze
        FSP_BELONGS_CONN(fsp,conn);*/
@@ -1813,7 +1751,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;
                }
 
@@ -1868,7 +1806,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;
                }
 
@@ -1991,7 +1929,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;
                }
 
@@ -2076,9 +2014,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;
        }
@@ -2094,8 +2033,8 @@ 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)) {
+       fsp = file_fsp(req, SVAL(params,0));
+       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;
@@ -2104,7 +2043,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);
 
@@ -2191,16 +2130,16 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                                /* then the len of the SID 4 bytes */
                                SIVAL(entry,4,sid_len);
 
-                               /* unknown data 8 bytes SMB_BIG_UINT */
-                               SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
+                               /* unknown data 8 bytes uint64_t */
+                               SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
 
-                               /* the used disk space 8 bytes SMB_BIG_UINT */
+                               /* the used disk space 8 bytes uint64_t */
                                SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
 
-                               /* the soft quotas 8 bytes SMB_BIG_UINT */
+                               /* the soft quotas 8 bytes uint64_t */
                                SBIG_UINT(entry,24,tmp_list->quotas->softlim);
 
-                               /* the hard quotas 8 bytes SMB_BIG_UINT */
+                               /* the hard quotas 8 bytes uint64_t */
                                SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
 
                                /* and now the SID */
@@ -2289,16 +2228,16 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                        /* then the len of the SID 4 bytes */
                        SIVAL(entry,4,sid_len);
 
-                       /* unknown data 8 bytes SMB_BIG_UINT */
-                       SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
+                       /* unknown data 8 bytes uint64_t */
+                       SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
 
-                       /* the used disk space 8 bytes SMB_BIG_UINT */
+                       /* the used disk space 8 bytes uint64_t */
                        SBIG_UINT(entry,16,qt.usedspace);
 
-                       /* the soft quotas 8 bytes SMB_BIG_UINT */
+                       /* the soft quotas 8 bytes uint64_t */
                        SBIG_UINT(entry,24,qt.softlim);
 
-                       /* the hard quotas 8 bytes SMB_BIG_UINT */
+                       /* the hard quotas 8 bytes uint64_t */
                        SBIG_UINT(entry,32,qt.hardlim);
 
                        /* and now the SID */
@@ -2342,9 +2281,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;
        }
@@ -2360,8 +2300,8 @@ 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)) {
+       fsp = file_fsp(req, SVAL(params,0));
+       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;
@@ -2391,10 +2331,10 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
         * maybe its the change time in NTTIME
         */
 
-       /* the used space 8 bytes (SMB_BIG_UINT)*/
-       qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
+       /* the used space 8 bytes (uint64_t)*/
+       qt.usedspace = (uint64_t)IVAL(pdata,16);
 #ifdef LARGE_SMB_OFF_T
-       qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
+       qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
 #else /* LARGE_SMB_OFF_T */
        if ((IVAL(pdata,20) != 0)&&
                ((qt.usedspace != 0xFFFFFFFF)||
@@ -2405,10 +2345,10 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
        }
 #endif /* LARGE_SMB_OFF_T */
 
-       /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
-       qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
+       /* the soft quotas 8 bytes (uint64_t)*/
+       qt.softlim = (uint64_t)IVAL(pdata,24);
 #ifdef LARGE_SMB_OFF_T
-       qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
+       qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
 #else /* LARGE_SMB_OFF_T */
        if ((IVAL(pdata,28) != 0)&&
                ((qt.softlim != 0xFFFFFFFF)||
@@ -2419,10 +2359,10 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
        }
 #endif /* LARGE_SMB_OFF_T */
 
-       /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
-       qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
+       /* the hard quotas 8 bytes (uint64_t)*/
+       qt.hardlim = (uint64_t)IVAL(pdata,32);
 #ifdef LARGE_SMB_OFF_T
-       qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
+       qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
 #else /* LARGE_SMB_OFF_T */
        if ((IVAL(pdata,36) != 0)&&
                ((qt.hardlim != 0xFFFFFFFF)||
@@ -2583,14 +2523,15 @@ static void handle_nttrans(connection_struct *conn,
 void reply_nttrans(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
-       uint32 pscnt;
-       uint32 psoff;
-       uint32 dscnt;
-       uint32 dsoff;
+       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);
 
@@ -2601,6 +2542,7 @@ void reply_nttrans(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);
@@ -2621,7 +2563,7 @@ void reply_nttrans(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;
@@ -2643,6 +2585,17 @@ void reply_nttrans(struct smb_request *req)
        state->setup = NULL;
        state->call = function_code;
 
+       DEBUG(10, ("num_setup=%u, "
+                  "param_total=%u, this_param=%u, max_param=%u, "
+                  "data_total=%u, this_data=%u, max_data=%u, "
+                  "param_offset=%u, data_offset=%u\n",
+                  (unsigned)state->setup_count,
+                  (unsigned)state->total_param, (unsigned)pscnt,
+                  (unsigned)state->max_param_return,
+                  (unsigned)state->total_data, (unsigned)dscnt,
+                  (unsigned)state->max_data_return,
+                  (unsigned)psoff, (unsigned)dsoff));
+
        /*
         * All nttrans messages we handle have smb_wct == 19 +
         * state->setup_count.  Ensure this is so as a sanity check.
@@ -2676,12 +2629,17 @@ void reply_nttrans(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);
        }
@@ -2698,12 +2656,17 @@ void reply_nttrans(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);
        }
@@ -2775,10 +2738,10 @@ void reply_nttrans(struct smb_request *req)
 void reply_nttranss(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
-       unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
+       uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
        struct trans_state *state;
-
-       int size;
+       uint32_t av_size;
+       uint32_t size;
 
        START_PROFILE(SMBnttranss);
 
@@ -2815,6 +2778,7 @@ void reply_nttranss(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);
@@ -2832,38 +2796,38 @@ void reply_nttranss(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)
-                       goto bad_param;
-               if ((smb_base(req->inbuf) + poff + pcnt
-                    > (char *)req->inbuf + size) ||
-                   (smb_base(req->inbuf) + poff + pcnt
-                    < smb_base(req->inbuf)))
+               if (pdisp > state->total_param ||
+                               pcnt > state->total_param ||
+                               pdisp+pcnt > state->total_param ||
+                               pdisp+pcnt < pdisp) {
                        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))
-                       goto bad_param;
-               if (ddisp > state->total_data)
+               if (ddisp > state->total_data ||
+                               dcnt > state->total_data ||
+                               ddisp+dcnt > state->total_data ||
+                               ddisp+dcnt < ddisp) {
                        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 (ddisp > av_size ||
+                               dcnt > av_size ||
+                               ddisp+dcnt > av_size ||
+                               ddisp+dcnt < ddisp) {
                        goto bad_param;
+               }
 
                memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
                       dcnt);