s3: Add extid to the dev/inode pair
[samba.git] / source3 / smbd / nttrans.c
index 1a13d962f016bf6125288e75a55b6100b52568dd..86a46505a2f1e4ba228a856179fbdf0c15791a41 100644 (file)
@@ -19,8 +19,8 @@
 */
 
 #include "includes.h"
+#include "smbd/globals.h"
 
-extern int max_send;
 extern enum protocol_types Protocol;
 extern const struct generic_mapping file_generic_mapping;
 
@@ -230,7 +230,8 @@ void send_nt_replies(connection_struct *conn,
                show_msg((char *)req->outbuf);
                if (!srv_send_smb(smbd_server_fd(),
                                (char *)req->outbuf,
-                               IS_CONN_ENCRYPTED(conn))) {
+                               IS_CONN_ENCRYPTED(conn),
+                               &req->pcd)) {
                        exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
                }
 
@@ -259,14 +260,46 @@ void send_nt_replies(connection_struct *conn,
  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.
+
+ Both Windows stream names and POSIX files can contain the ':' character.
+ This function first checks for the existence of a colon in the last component
+ of the given name.  If the name contains a colon we differentiate between a
+ stream and POSIX file by checking if the latter exists through a POSIX stat.
+
+ Function assumes we've already chdir() to the "root" directory of fname.
 ****************************************************************************/
 
 bool is_ntfs_stream_name(const char *fname)
 {
+       const char *lastcomp;
+       SMB_STRUCT_STAT sbuf;
+
+       /* If all pathnames are treated as POSIX we ignore streams. */
        if (lp_posix_pathnames()) {
-               return False;
+               return false;
+       }
+
+       /* Find the last component of the name. */
+       if ((lastcomp = strrchr_m(fname, '/')) != NULL)
+               ++lastcomp;
+       else
+               lastcomp = fname;
+
+       /* If there is no colon in the last component, it's not a stream. */
+       if (strchr_m(lastcomp, ':') == NULL)
+               return false;
+
+       /*
+        * If file already exists on disk, it's not a stream. The stat must
+        * bypass the vfs layer so streams modules don't intefere.
+        */
+       if (sys_stat(fname, &sbuf) == 0) {
+               DEBUG(5, ("is_ntfs_stream_name: file %s contains a ':' but is "
+                       "not a stream\n", fname));
+               return false;
        }
-       return (strchr_m(fname, ':') != NULL) ? True : False;
+
+       return true;
 }
 
 /****************************************************************************
@@ -284,7 +317,7 @@ static void nt_open_pipe(char *fname, connection_struct *conn,
        /* Strip \\ off the name. */
        fname++;
 
-       status = np_open(req, conn, fname, &fsp);
+       status = open_np_file(req, fname, &fsp);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
                        reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
@@ -474,10 +507,24 @@ void reply_ntcreate_and_X(struct smb_request *req)
                        ? BATCH_OPLOCK : 0;
        }
 
-       status = create_file(conn, req, root_dir_fid, fname,
-                            access_mask, share_access, create_disposition,
-                            create_options, file_attributes, oplock_request,
-                            allocation_size, NULL, NULL, &fsp, &info, &sbuf);
+       status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               root_dir_fid,                           /* root_dir_fid */
+               fname,                                  /* fname */
+               CFF_DOS_PATH,                           /* create_file_flags */
+               access_mask,                            /* access_mask */
+               share_access,                           /* share_access */
+               create_disposition,                     /* create_disposition*/
+               create_options,                         /* create_options */
+               file_attributes,                        /* file_attributes */
+               oplock_request,                         /* oplock_request */
+               allocation_size,                        /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp,                                   /* result */
+               &info,                                  /* pinfo */
+               &sbuf);                                 /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
@@ -575,7 +622,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
        p += 8;
        SIVAL(p,0,fattr); /* File Attributes. */
        p += 4;
-       SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
+       SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&sbuf));
        p += 8;
        SOFF_T(p,0,file_len);
        p += 8;
@@ -721,26 +768,22 @@ 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 (security_info_sent & DACL_SECURITY_INFORMATION) {
-               psd->type |= SEC_DESC_DACL_PRESENT;
-       }
-       if (psd->dacl==0) {
-               security_info_sent &= ~DACL_SECURITY_INFORMATION;
-       }
 
        /* 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);
@@ -936,10 +979,24 @@ static void call_nt_transact_create(connection_struct *conn,
                        ? BATCH_OPLOCK : 0;
        }
 
-       status = create_file(conn, req, root_dir_fid, fname,
-                            access_mask, share_access, create_disposition,
-                            create_options, file_attributes, oplock_request,
-                            allocation_size, sd, ea_list, &fsp, &info, &sbuf);
+       status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               root_dir_fid,                           /* root_dir_fid */
+               fname,                                  /* fname */
+               CFF_DOS_PATH,                           /* create_file_flags */
+               access_mask,                            /* access_mask */
+               share_access,                           /* share_access */
+               create_disposition,                     /* create_disposition*/
+               create_options,                         /* create_options */
+               file_attributes,                        /* file_attributes */
+               oplock_request,                         /* oplock_request */
+               allocation_size,                        /* allocation_size */
+               sd,                                     /* sd */
+               ea_list,                                /* ea_list */
+               &fsp,                                   /* result */
+               &info,                                  /* pinfo */
+               &sbuf);                                 /* psbuf */
 
        if(!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
@@ -1030,7 +1087,7 @@ static void call_nt_transact_create(connection_struct *conn,
        p += 8;
        SIVAL(p,0,fattr); /* File Attributes. */
        p += 4;
-       SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
+       SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&sbuf));
        p += 8;
        SOFF_T(p,0,file_len);
        p += 8;
@@ -1103,6 +1160,7 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
        int info;
        SMB_OFF_T ret=-1;
        NTSTATUS status = NT_STATUS_OK;
+       char *parent;
 
        ZERO_STRUCT(sbuf1);
        ZERO_STRUCT(sbuf2);
@@ -1162,27 +1220,49 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
        DEBUG(10,("copy_internals: doing file copy %s to %s\n",
                                oldname, newname));
 
-        status = open_file_ntcreate(conn, req, oldname, &sbuf1,
-                       FILE_READ_DATA, /* Read-only. */
-                       FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                       FILE_OPEN,
-                       0, /* No create options. */
-                       FILE_ATTRIBUTE_NORMAL,
-                       NO_OPLOCK,
-                       &info, &fsp1);
+        status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               0,                                      /* root_dir_fid */
+               oldname,                                /* fname */
+               0,                                      /* create_file_flags */
+               FILE_READ_DATA,                         /* access_mask */
+               (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
+                   FILE_SHARE_DELETE),
+               FILE_OPEN,                              /* create_disposition*/
+               0,                                      /* create_options */
+               FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
+               NO_OPLOCK,                              /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp1,                                  /* result */
+               &info,                                  /* pinfo */
+               &sbuf1);                                /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-        status = open_file_ntcreate(conn, req, newname, &sbuf2,
-                       FILE_WRITE_DATA, /* Read-only. */
-                       FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                       FILE_CREATE,
-                       0, /* No create options. */
-                       fattr,
-                       NO_OPLOCK,
-                       &info, &fsp2);
+        status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               0,                                      /* root_dir_fid */
+               newname,                                /* fname */
+               0,                                      /* create_file_flags */
+               FILE_WRITE_DATA,                        /* access_mask */
+               (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
+                   FILE_SHARE_DELETE),
+               FILE_CREATE,                            /* create_disposition*/
+               0,                                      /* create_options */
+               fattr,                                  /* file_attributes */
+               NO_OPLOCK,                              /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp2,                                  /* result */
+               &info,                                  /* pinfo */
+               &sbuf2);                                /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                close_file(NULL, fsp1, ERROR_CLOSE);
@@ -1209,8 +1289,11 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
        /* 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
           case. JRA */
-       file_set_dosmode(conn, newname, fattr, &sbuf2,
-                        parent_dirname(newname),false);
+       if (!parent_dirname(talloc_tos(), newname, &parent, NULL)) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       file_set_dosmode(conn, newname, fattr, &sbuf2, parent, false);
+       TALLOC_FREE(parent);
 
        if (ret < (SMB_OFF_T)sbuf1.st_size) {
                return NT_STATUS_DISK_FULL;
@@ -1260,13 +1343,6 @@ void reply_ntrename(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);
@@ -1314,6 +1390,13 @@ void reply_ntrename(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) {
@@ -1591,16 +1674,29 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
                status = SMB_VFS_FGET_NT_ACL(
                        fsp, security_info_wanted, &psd);
        }
-
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
                return;
        }
 
-       sd_size = ndr_size_security_descriptor(psd, 0);
+       /* 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, NULL, 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) {
@@ -1707,7 +1803,6 @@ static void call_nt_transact_ioctl(connection_struct *conn,
        files_struct *fsp;
        uint8 isFSctl;
        uint8 compfilter;
-       static bool logged_message;
        char *pdata = *ppdata;
 
        if (setup_count != 8) {
@@ -1729,6 +1824,8 @@ static void call_nt_transact_ioctl(connection_struct *conn,
           because I don't want to break anything... --metze
        FSP_BELONGS_CONN(fsp,conn);*/
 
+       SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
+
        switch (function) {
        case FSCTL_SET_SPARSE:
                /* pretend this succeeded - tho strictly we should
@@ -1759,6 +1856,8 @@ static void call_nt_transact_ioctl(connection_struct *conn,
                        reply_nterror(req, NT_STATUS_NO_MEMORY);
                        return;
                }
+
+               /* For backwards compatibility only store the dev/inode. */
                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);
@@ -1967,8 +2066,8 @@ static void call_nt_transact_ioctl(connection_struct *conn,
                return;
        }
        default:
-               if (!logged_message) {
-                       logged_message = True; /* Only print this once... */
+               if (!logged_ioctl_message) {
+                       logged_ioctl_message = true; /* Only print this once... */
                        DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
                                 function));
                }
@@ -2119,7 +2218,7 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                                tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
 
                                sid_len = ndr_size_dom_sid(
-                                       &tmp_list->quotas->sid, 0);
+                                       &tmp_list->quotas->sid, NULL, 0);
                                entry_len = 40 + sid_len;
 
                                /* nextoffset entry 4 bytes */
@@ -2395,6 +2494,9 @@ static void handle_nttrans(connection_struct *conn,
                SSVAL(req->inbuf,smb_flg2,req->flags2);
        }
 
+
+       SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
+
        /* Now we must call the relevant NT_TRANS function */
        switch(state->call) {
                case NT_TRANSACT_CREATE:
@@ -2528,8 +2630,6 @@ void reply_nttrans(struct smb_request *req)
        uint16 function_code;
        NTSTATUS result;
        struct trans_state *state;
-       uint32_t size;
-       uint32_t av_size;
 
        START_PROFILE(SMBnttrans);
 
@@ -2539,8 +2639,6 @@ void reply_nttrans(struct smb_request *req)
                return;
        }
 
-       size = smb_len(req->inbuf) + 4;
-       av_size = smb_len(req->inbuf);
        pscnt = IVAL(req->vwv+9, 1);
        psoff = IVAL(req->vwv+11, 1);
        dscnt = IVAL(req->vwv+13, 1);
@@ -2617,6 +2715,12 @@ void reply_nttrans(struct smb_request *req)
                goto bad_param;
 
        if (state->total_data)  {
+
+               if (trans_oob(state->total_data, 0, dscnt)
+                   || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
+                       goto bad_param;
+               }
+
                /* Can't use talloc here, the core routines do realloc on the
                 * params and data. */
                if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
@@ -2628,21 +2732,16 @@ void reply_nttrans(struct smb_request *req)
                        return;
                }
 
-               if (dscnt > state->total_data ||
-                               dsoff+dscnt < dsoff) {
-                       goto bad_param;
-               }
-
-               if (dsoff > av_size ||
-                               dscnt > av_size ||
-                               dsoff+dscnt > av_size) {
-                       goto bad_param;
-               }
-
                memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
        }
 
        if (state->total_param) {
+
+               if (trans_oob(state->total_param, 0, pscnt)
+                   || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
+                       goto bad_param;
+               }
+
                /* Can't use talloc here, the core routines do realloc on the
                 * params and data. */
                if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
@@ -2655,17 +2754,6 @@ void reply_nttrans(struct smb_request *req)
                        return;
                }
 
-               if (pscnt > state->total_param ||
-                               psoff+pscnt < psoff) {
-                       goto bad_param;
-               }
-
-               if (psoff > av_size ||
-                               pscnt > av_size ||
-                               psoff+pscnt > av_size) {
-                       goto bad_param;
-               }
-
                memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
        }
 
@@ -2675,6 +2763,19 @@ void reply_nttrans(struct smb_request *req)
        if(state->setup_count > 0) {
                DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
                          state->setup_count));
+
+               /*
+                * No overflow possible here, state->setup_count is an
+                * unsigned int, being filled by a single byte from
+                * CVAL(req->vwv+13, 0) above. The cast in the comparison
+                * below is not necessary, it's here to clarify things. The
+                * validity of req->vwv and req->wct has been checked in
+                * init_smb_request already.
+                */
+               if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
+                       goto bad_param;
+               }
+
                state->setup = (uint16 *)TALLOC(state, state->setup_count);
                if (state->setup == NULL) {
                        DEBUG(0,("reply_nttrans : Out of memory\n"));
@@ -2686,14 +2787,6 @@ void reply_nttrans(struct smb_request *req)
                        return;
                }
 
-               if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
-                   (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
-                       goto bad_param;
-               }
-               if (smb_nt_SetupStart + state->setup_count > size) {
-                       goto bad_param;
-               }
-
                memcpy(state->setup, req->vwv+19, state->setup_count);
                dump_data(10, (uint8 *)state->setup, state->setup_count);
        }
@@ -2737,8 +2830,6 @@ void reply_nttranss(struct smb_request *req)
        connection_struct *conn = req->conn;
        uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
        struct trans_state *state;
-       uint32_t av_size;
-       uint32_t size;
 
        START_PROFILE(SMBnttranss);
 
@@ -2772,9 +2863,6 @@ void reply_nttranss(struct smb_request *req)
                state->total_data = IVAL(req->vwv+3, 1);
        }
 
-       size = smb_len(req->inbuf) + 4;
-       av_size = smb_len(req->inbuf);
-
        pcnt = IVAL(req->vwv+5, 1);
        poff = IVAL(req->vwv+7, 1);
        pdisp = IVAL(req->vwv+9, 1);
@@ -2791,41 +2879,19 @@ void reply_nttranss(struct smb_request *req)
                goto bad_param;
 
        if (pcnt) {
-               if (pdisp > state->total_param ||
-                               pcnt > state->total_param ||
-                               pdisp+pcnt > state->total_param ||
-                               pdisp+pcnt < pdisp) {
+               if (trans_oob(state->total_param, pdisp, pcnt)
+                   || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
                        goto bad_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);
+               memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
        }
 
        if (dcnt) {
-               if (ddisp > state->total_data ||
-                               dcnt > state->total_data ||
-                               ddisp+dcnt > state->total_data ||
-                               ddisp+dcnt < ddisp) {
+               if (trans_oob(state->total_data, ddisp, dcnt)
+                   || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
                        goto bad_param;
                }
-
-               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);
+               memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
        }
 
        if ((state->received_param < state->total_param) ||