Cope with old CIFSFS clients that use SMBunlink to remove
[ira/wip.git] / source3 / smbd / reply.c
index 3498109f12d890e4264950e1d2aec38a78537813..2365ed1da1446daf7f0ff185f6d84ab55760d66e 100644 (file)
@@ -65,9 +65,6 @@ static NTSTATUS check_path_syntax_internal(char *path,
                                if (strchr_m(&s[1], ':')) {
                                        return NT_STATUS_OBJECT_NAME_INVALID;
                                }
-                               if (StrCaseCmp(s, ":$DATA") != 0) {
-                                       return NT_STATUS_INVALID_PARAMETER;
-                               }
                                break;
                        }
                }
@@ -2386,6 +2383,8 @@ static NTSTATUS do_unlink(connection_struct *conn,
        files_struct *fsp;
        uint32 dirtype_orig = dirtype;
        NTSTATUS status;
+       int ret;
+       bool posix_paths = lp_posix_pathnames();
 
        DEBUG(10,("do_unlink: %s, dirtype = %d\n",
                  smb_fname_str_dbg(smb_fname),
@@ -2395,7 +2394,12 @@ static NTSTATUS do_unlink(connection_struct *conn,
                return NT_STATUS_MEDIA_WRITE_PROTECTED;
        }
 
-       if (SMB_VFS_LSTAT(conn, smb_fname) != 0) {
+       if (posix_paths) {
+               ret = SMB_VFS_LSTAT(conn, smb_fname);
+       } else {
+               ret = SMB_VFS_LSTAT(conn, smb_fname);
+       }
+       if (ret != 0) {
                return map_nt_error_from_unix(errno);
        }
 
@@ -2482,7 +2486,9 @@ static NTSTATUS do_unlink(connection_struct *conn,
                 FILE_SHARE_NONE,       /* share_access */
                 FILE_OPEN,             /* create_disposition*/
                 FILE_NON_DIRECTORY_FILE, /* create_options */
-                FILE_ATTRIBUTE_NORMAL, /* file_attributes */
+                                       /* file_attributes */
+                posix_paths ? FILE_FLAG_POSIX_SEMANTICS|0777 :
+                               FILE_ATTRIBUTE_NORMAL,
                 0,                     /* oplock_request */
                 0,                     /* allocation_size */
                 NULL,                  /* sd */
@@ -2580,7 +2586,7 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
        } else {
                struct smb_Dir *dir_hnd = NULL;
                long offset = 0;
-               const char *dname;
+               char *dname = NULL;
 
                if ((dirtype & SAMBA_ATTRIBUTES_MASK) == aDIR) {
                        status = NT_STATUS_OBJECT_NAME_INVALID;
@@ -2622,18 +2628,21 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
                        if (!is_visible_file(conn, fname_dir, dname,
                                             &smb_fname->st, true)) {
                                TALLOC_FREE(frame);
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
                        /* Quick check for "." and ".." */
                        if (ISDOT(dname) || ISDOTDOT(dname)) {
                                TALLOC_FREE(frame);
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
                        if(!mask_match(dname, fname_mask,
                                       conn->case_sensitive)) {
                                TALLOC_FREE(frame);
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
@@ -2646,6 +2655,7 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
                                TALLOC_FREE(dir_hnd);
                                status = NT_STATUS_NO_MEMORY;
                                TALLOC_FREE(frame);
+                               TALLOC_FREE(dname);
                                goto out;
                        }
 
@@ -2653,12 +2663,14 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
                        if (!NT_STATUS_IS_OK(status)) {
                                TALLOC_FREE(dir_hnd);
                                TALLOC_FREE(frame);
+                               TALLOC_FREE(dname);
                                goto out;
                        }
 
                        status = do_unlink(conn, req, smb_fname, dirtype);
                        if (!NT_STATUS_IS_OK(status)) {
                                TALLOC_FREE(frame);
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
@@ -2667,6 +2679,7 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
                                 smb_fname->base_name));
 
                        TALLOC_FREE(frame);
+                       TALLOC_FREE(dname);
                }
                TALLOC_FREE(dir_hnd);
        }
@@ -5290,7 +5303,7 @@ static bool recursive_rmdir(TALLOC_CTX *ctx,
                        connection_struct *conn,
                        struct smb_filename *smb_dname)
 {
-       const char *dname = NULL;
+       char *dname = NULL;
        bool ret = True;
        long offset = 0;
        SMB_STRUCT_STAT st;
@@ -5309,11 +5322,13 @@ static bool recursive_rmdir(TALLOC_CTX *ctx,
                NTSTATUS status;
 
                if (ISDOT(dname) || ISDOTDOT(dname)) {
+                       TALLOC_FREE(dname);
                        continue;
                }
 
                if (!is_visible_file(conn, smb_dname->base_name, dname, &st,
                                     false)) {
+                       TALLOC_FREE(dname);
                        continue;
                }
 
@@ -5356,6 +5371,7 @@ static bool recursive_rmdir(TALLOC_CTX *ctx,
         err_break:
                TALLOC_FREE(smb_dname_full);
                TALLOC_FREE(fullname);
+               TALLOC_FREE(dname);
                if (do_break) {
                        ret = false;
                        break;
@@ -5409,7 +5425,7 @@ NTSTATUS rmdir_internals(TALLOC_CTX *ctx,
                 * retry. If we fail to delete any of them (and we *don't*
                 * do a recursive delete) then fail the rmdir.
                 */
-               const char *dname;
+               char *dname = NULL;
                long dirpos = 0;
                struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
                                                  smb_dname->base_name, NULL,
@@ -5421,16 +5437,22 @@ NTSTATUS rmdir_internals(TALLOC_CTX *ctx,
                }
 
                while ((dname = ReadDirName(dir_hnd, &dirpos, &st))) {
-                       if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
+                       if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0)) {
+                               TALLOC_FREE(dname);
                                continue;
+                       }
                        if (!is_visible_file(conn, smb_dname->base_name, dname,
-                                            &st, false))
+                                            &st, false)) {
+                               TALLOC_FREE(dname);
                                continue;
+                       }
                        if(!IS_VETO_PATH(conn, dname)) {
                                TALLOC_FREE(dir_hnd);
+                               TALLOC_FREE(dname);
                                errno = ENOTEMPTY;
                                goto err;
                        }
+                       TALLOC_FREE(dname);
                }
 
                /* We only have veto files/directories.
@@ -5451,10 +5473,12 @@ NTSTATUS rmdir_internals(TALLOC_CTX *ctx,
                        NTSTATUS status;
 
                        if (ISDOT(dname) || ISDOTDOT(dname)) {
+                               TALLOC_FREE(dname);
                                continue;
                        }
                        if (!is_visible_file(conn, smb_dname->base_name, dname,
                                             &st, false)) {
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
@@ -5499,6 +5523,7 @@ NTSTATUS rmdir_internals(TALLOC_CTX *ctx,
                 err_break:
                        TALLOC_FREE(fullname);
                        TALLOC_FREE(smb_dname_full);
+                       TALLOC_FREE(dname);
                        if (do_break)
                                break;
                }
@@ -5844,25 +5869,17 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
                        bool replace_if_exists)
 {
        TALLOC_CTX *ctx = talloc_tos();
-       struct smb_filename *smb_fname_src = NULL;
        struct smb_filename *smb_fname_dst = NULL;
-       SMB_STRUCT_STAT sbuf;
        NTSTATUS status = NT_STATUS_OK;
        struct share_mode_lock *lck = NULL;
        bool dst_exists, old_is_stream, new_is_stream;
 
-       ZERO_STRUCT(sbuf);
-
        status = check_name(conn, smb_fname_dst_in->base_name);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       /* Make a copy of the src and dst smb_fname structs */
-       status = copy_smb_filename(ctx, fsp->fsp_name, &smb_fname_src);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto out;
-       }
+       /* Make a copy of the dst smb_fname structs */
 
        status = copy_smb_filename(ctx, smb_fname_dst_in, &smb_fname_dst);
        if (!NT_STATUS_IS_OK(status)) {
@@ -5890,8 +5907,8 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
         * filename).
         */
        if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
-           strequal(smb_fname_src->base_name, smb_fname_dst->base_name) &&
-           strequal(smb_fname_src->stream_name, smb_fname_dst->stream_name)) {
+           strequal(fsp->fsp_name->base_name, smb_fname_dst->base_name) &&
+           strequal(fsp->fsp_name->stream_name, smb_fname_dst->stream_name)) {
                char *last_slash;
                char *fname_dst_lcomp_base_mod = NULL;
                struct smb_filename *smb_fname_orig_lcomp = NULL;
@@ -5968,8 +5985,8 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
         * don't do the rename, just return success.
         */
 
-       if (strcsequal(smb_fname_src->base_name, smb_fname_dst->base_name) &&
-           strcsequal(smb_fname_src->stream_name,
+       if (strcsequal(fsp->fsp_name->base_name, smb_fname_dst->base_name) &&
+           strcsequal(fsp->fsp_name->stream_name,
                       smb_fname_dst->stream_name)) {
                DEBUG(3, ("rename_internals_fsp: identical names in rename %s "
                          "- returning success\n",
@@ -5978,7 +5995,7 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
                goto out;
        }
 
-       old_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
+       old_is_stream = is_ntfs_stream_smb_fname(fsp->fsp_name);
        new_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
 
        /* Return the correct error code if both names aren't streams. */
@@ -5996,7 +6013,7 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
 
        if(!replace_if_exists && dst_exists) {
                DEBUG(3, ("rename_internals_fsp: dest exists doing rename "
-                         "%s -> %s\n", smb_fname_str_dbg(smb_fname_src),
+                         "%s -> %s\n", smb_fname_str_dbg(fsp->fsp_name),
                          smb_fname_str_dbg(smb_fname_dst)));
                status = NT_STATUS_OBJECT_NAME_COLLISION;
                goto out;
@@ -6015,38 +6032,23 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
        }
 
        /* Ensure we have a valid stat struct for the source. */
-       if (fsp->fh->fd != -1) {
-               if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
-                       status = map_nt_error_from_unix(errno);
-                       goto out;
-               }
-       } else {
-               int ret = -1;
-               if (fsp->posix_open) {
-                       ret = SMB_VFS_LSTAT(conn, smb_fname_src);
-               } else {
-
-                       ret = SMB_VFS_STAT(conn, smb_fname_src);
-               }
-               if (ret == -1) {
-                       status = map_nt_error_from_unix(errno);
-                       goto out;
-               }
-               sbuf = smb_fname_src->st;
+       status = vfs_stat_fsp(fsp);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
        }
 
-       status = can_rename(conn, fsp, attrs, &sbuf);
+       status = can_rename(conn, fsp, attrs, &fsp->fsp_name->st);
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(3, ("rename_internals_fsp: Error %s rename %s -> %s\n",
-                         nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
+                         nt_errstr(status), smb_fname_str_dbg(fsp->fsp_name),
                          smb_fname_str_dbg(smb_fname_dst)));
                if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION))
                        status = NT_STATUS_ACCESS_DENIED;
                goto out;
        }
 
-       if (rename_path_prefix_equal(smb_fname_src, smb_fname_dst)) {
+       if (rename_path_prefix_equal(fsp->fsp_name, smb_fname_dst)) {
                status = NT_STATUS_ACCESS_DENIED;
        }
 
@@ -6060,14 +6062,14 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
 
        SMB_ASSERT(lck != NULL);
 
-       if(SMB_VFS_RENAME(conn, smb_fname_src, smb_fname_dst) == 0) {
+       if(SMB_VFS_RENAME(conn, fsp->fsp_name, smb_fname_dst) == 0) {
                uint32 create_options = fsp->fh->private_options;
 
                DEBUG(3, ("rename_internals_fsp: succeeded doing rename on "
-                         "%s -> %s\n", smb_fname_str_dbg(smb_fname_src),
+                         "%s -> %s\n", smb_fname_str_dbg(fsp->fsp_name),
                          smb_fname_str_dbg(smb_fname_dst)));
 
-               notify_rename(conn, fsp->is_directory, smb_fname_src,
+               notify_rename(conn, fsp->is_directory, fsp->fsp_name,
                              smb_fname_dst);
 
                rename_open_files(conn, lck, smb_fname_dst);
@@ -6104,11 +6106,10 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
        }
 
        DEBUG(3, ("rename_internals_fsp: Error %s rename %s -> %s\n",
-                 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
+                 nt_errstr(status), smb_fname_str_dbg(fsp->fsp_name),
                  smb_fname_str_dbg(smb_fname_dst)));
 
  out:
-       TALLOC_FREE(smb_fname_src);
        TALLOC_FREE(smb_fname_dst);
 
        return status;
@@ -6135,7 +6136,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
        int count=0;
        NTSTATUS status = NT_STATUS_OK;
        struct smb_Dir *dir_hnd = NULL;
-       const char *dname;
+       char *dname = NULL;
        long offset = 0;
        int create_options = 0;
        bool posix_pathnames = lp_posix_pathnames();
@@ -6323,16 +6324,19 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        if (attrs & aDIR) {
                                sysdir_entry = True;
                        } else {
+                               TALLOC_FREE(dname);
                                continue;
                        }
                }
 
                if (!is_visible_file(conn, fname_src_dir, dname,
                                     &smb_fname_src->st, false)) {
+                       TALLOC_FREE(dname);
                        continue;
                }
 
                if(!mask_match(dname, fname_src_mask, conn->case_sensitive)) {
+                       TALLOC_FREE(dname);
                        continue;
                }
 
@@ -6356,6 +6360,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                                       &destname)) {
                        DEBUG(6, ("resolve_wildcards %s %s failed\n",
                                  smb_fname_src->base_name, destname));
+                       TALLOC_FREE(dname);
                        continue;
                }
                if (!destname) {
@@ -6431,7 +6436,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                DEBUG(3,("rename_internals: doing rename on %s -> "
                         "%s\n", smb_fname_str_dbg(smb_fname_src),
                         smb_fname_str_dbg(smb_fname_src)));
-
+               TALLOC_FREE(dname);
        }
        TALLOC_FREE(dir_hnd);
 
@@ -6440,6 +6445,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
        }
 
  out:
+       TALLOC_FREE(dname);
        TALLOC_FREE(fname_src_dir);
        TALLOC_FREE(fname_src_mask);
        return status;
@@ -6904,7 +6910,7 @@ void reply_copy(struct smb_request *req)
                }
        } else {
                struct smb_Dir *dir_hnd = NULL;
-               const char *dname = NULL;
+               char *dname = NULL;
                long offset = 0;
 
                /*
@@ -6949,16 +6955,19 @@ void reply_copy(struct smb_request *req)
                        char *destname = NULL;
 
                        if (ISDOT(dname) || ISDOTDOT(dname)) {
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
                        if (!is_visible_file(conn, fname_src_dir, dname,
                                             &smb_fname_src->st, false)) {
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
                        if(!mask_match(dname, fname_src_mask,
                                       conn->case_sensitive)) {
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
@@ -6972,6 +6981,7 @@ void reply_copy(struct smb_request *req)
 
                        if (!smb_fname_src->base_name) {
                                TALLOC_FREE(dir_hnd);
+                               TALLOC_FREE(dname);
                                reply_nterror(req, NT_STATUS_NO_MEMORY);
                                goto out;
                        }
@@ -6979,10 +6989,12 @@ void reply_copy(struct smb_request *req)
                        if (!resolve_wildcards(ctx, smb_fname_src->base_name,
                                               smb_fname_dst->base_name,
                                               &destname)) {
+                               TALLOC_FREE(dname);
                                continue;
                        }
                        if (!destname) {
                                TALLOC_FREE(dir_hnd);
+                               TALLOC_FREE(dname);
                                reply_nterror(req, NT_STATUS_NO_MEMORY);
                                goto out;
                        }
@@ -6993,6 +7005,7 @@ void reply_copy(struct smb_request *req)
                        status = check_name(conn, smb_fname_src->base_name);
                        if (!NT_STATUS_IS_OK(status)) {
                                TALLOC_FREE(dir_hnd);
+                               TALLOC_FREE(dname);
                                reply_nterror(req, status);
                                goto out;
                        }
@@ -7000,6 +7013,7 @@ void reply_copy(struct smb_request *req)
                        status = check_name(conn, smb_fname_dst->base_name);
                        if (!NT_STATUS_IS_OK(status)) {
                                TALLOC_FREE(dir_hnd);
+                               TALLOC_FREE(dname);
                                reply_nterror(req, status);
                                goto out;
                        }
@@ -7014,6 +7028,8 @@ void reply_copy(struct smb_request *req)
                        if (NT_STATUS_IS_OK(status)) {
                                count++;
                        }
+
+                       TALLOC_FREE(dname);
                }
                TALLOC_FREE(dir_hnd);
        }
@@ -7676,25 +7692,10 @@ void reply_setattrE(struct smb_request *req)
         */
 
        /* Ensure we have a valid stat struct for the source. */
-       if (fsp->fh->fd != -1) {
-               if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) == -1) {
-                       status = map_nt_error_from_unix(errno);
-                       reply_nterror(req, status);
-                       goto out;
-               }
-       } else {
-               int ret = -1;
-
-               if (fsp->posix_open) {
-                       ret = SMB_VFS_LSTAT(conn, fsp->fsp_name);
-               } else {
-                       ret = SMB_VFS_STAT(conn, fsp->fsp_name);
-               }
-               if (ret == -1) {
-                       status = map_nt_error_from_unix(errno);
-                       reply_nterror(req, status);
-                       goto out;
-               }
+       status = vfs_stat_fsp(fsp);
+       if (!NT_STATUS_IS_OK(status)) {
+               reply_nterror(req, status);
+               goto out;
        }
 
        status = smb_set_file_time(conn, fsp, fsp->fsp_name, &ft, true);
@@ -7793,7 +7794,7 @@ void reply_getattrE(struct smb_request *req)
 
        reply_outbuf(req, 11, 0);
 
-       create_ts = sbuf.st_ex_btime;
+       create_ts = get_create_timespec(conn, fsp, fsp->fsp_name);
        srv_put_dos_date2((char *)req->outbuf, smb_vwv0, create_ts.tv_sec);
        srv_put_dos_date2((char *)req->outbuf, smb_vwv2,
                          convert_timespec_to_time_t(sbuf.st_ex_atime));