s4-pvfs: change the handling of access checking on create
[ira/wip.git] / source4 / ntvfs / posix / pvfs_setfileinfo.c
index 18647c95c590c7ea45e81e008b40b6e7303abb5a..244548382ce1018c09247f8096d9f01fb10317fa 100644 (file)
@@ -76,12 +76,47 @@ static uint32_t pvfs_setfileinfo_access(union smb_setfileinfo *info)
        return needed;
 }
 
+/*
+  rename_information level for streams
+*/
+static NTSTATUS pvfs_setfileinfo_rename_stream(struct pvfs_state *pvfs, 
+                                              struct ntvfs_request *req, 
+                                              struct pvfs_filename *name,
+                                              int fd,
+                                              DATA_BLOB *odb_locking_key,
+                                              union smb_setfileinfo *info)
+{
+       NTSTATUS status;
+       struct odb_lock *lck = NULL;
+
+       if (info->rename_information.in.new_name[0] != ':') {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       status = pvfs_access_check_simple(pvfs, req, name, SEC_FILE_WRITE_ATTRIBUTE);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       lck = odb_lock(req, pvfs->odb_context, odb_locking_key);
+       if (lck == NULL) {
+               DEBUG(0,("Unable to lock opendb for can_stat\n"));
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+
+
+       status = pvfs_stream_rename(pvfs, name, fd, 
+                                   info->rename_information.in.new_name+1);
+       return status;
+}
+
 /*
   rename_information level
 */
 static NTSTATUS pvfs_setfileinfo_rename(struct pvfs_state *pvfs, 
                                        struct ntvfs_request *req, 
                                        struct pvfs_filename *name,
+                                       int fd,
                                        DATA_BLOB *odb_locking_key,
                                        union smb_setfileinfo *info)
 {
@@ -96,14 +131,10 @@ static NTSTATUS pvfs_setfileinfo_rename(struct pvfs_state *pvfs,
                return NT_STATUS_NOT_SUPPORTED;
        }
 
-       if (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
-               /* don't allow this for now */
-               return NT_STATUS_FILE_IS_A_DIRECTORY;
-       }
-
-       /* don't allow stream renames for now */
+       /* handle stream renames specially */
        if (name->stream_name) {
-               return NT_STATUS_INVALID_PARAMETER;
+               return pvfs_setfileinfo_rename_stream(pvfs, req, name, fd, 
+                                                     odb_locking_key, info);
        }
 
        /* w2k3 does not appear to allow relative rename. On SMB2, vista sends it sometimes,
@@ -137,7 +168,7 @@ static NTSTATUS pvfs_setfileinfo_rename(struct pvfs_state *pvfs,
        }
 
        /* resolve the new name */
-       status = pvfs_resolve_name(pvfs, name, new_name, 0, &name2);
+       status = pvfs_resolve_name(pvfs, req, new_name, 0, &name2);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -176,10 +207,7 @@ static NTSTATUS pvfs_setfileinfo_rename(struct pvfs_state *pvfs,
                return NT_STATUS_INTERNAL_DB_CORRUPTION;
        }
 
-       status = pvfs_do_rename(pvfs, name, name2->full_name);
-       if (NT_STATUS_IS_OK(status)) {
-               status = odb_rename(lck, name2->full_name);
-       }
+       status = pvfs_do_rename(pvfs, lck, name, name2->full_name);
        talloc_free(lck);
        NT_STATUS_NOT_OK_RETURN(status);
        if (NT_STATUS_IS_OK(status)) {
@@ -280,8 +308,8 @@ NTSTATUS pvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
                          struct ntvfs_request *req, 
                          union smb_setfileinfo *info)
 {
-       struct pvfs_state *pvfs = ntvfs->private_data;
-       struct utimbuf unix_times;
+       struct pvfs_state *pvfs = talloc_get_type(ntvfs->private_data,
+                                 struct pvfs_state);
        struct pvfs_file *f;
        struct pvfs_file_handle *h;
        struct pvfs_filename newstats;
@@ -350,8 +378,6 @@ NTSTATUS pvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
                }
                if (!null_nttime(info->basic_info.in.write_time)) {
                        newstats.dos.write_time = info->basic_info.in.write_time;
-                       newstats.dos.flags |= XATTR_ATTRIB_FLAG_STICKY_WRITE_TIME;
-                       h->sticky_write_time = true;
                }
                if (!null_nttime(info->basic_info.in.change_time)) {
                        newstats.dos.change_time = info->basic_info.in.change_time;
@@ -404,7 +430,7 @@ NTSTATUS pvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
 
        case RAW_SFILEINFO_RENAME_INFORMATION:
        case RAW_SFILEINFO_RENAME_INFORMATION_SMB2:
-               return pvfs_setfileinfo_rename(pvfs, req, h->name,
+               return pvfs_setfileinfo_rename(pvfs, req, h->name, f->handle->fd,
                                               &h->odb_locking_key,
                                               info);
 
@@ -447,27 +473,63 @@ NTSTATUS pvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
        }
 
        /* possibly change the file timestamps */
-       ZERO_STRUCT(unix_times);
        if (newstats.dos.create_time != h->name->dos.create_time) {
                change_mask |= FILE_NOTIFY_CHANGE_CREATION;
        }
        if (newstats.dos.access_time != h->name->dos.access_time) {
-               unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
                change_mask |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
        }
        if (newstats.dos.write_time != h->name->dos.write_time) {
-               unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
                change_mask |= FILE_NOTIFY_CHANGE_LAST_WRITE;
        }
-       if (unix_times.actime != 0 || unix_times.modtime != 0) {
-               if (utime(h->name->full_name, &unix_times) == -1) {
-                       return pvfs_map_errno(pvfs, errno);
+       if ((change_mask & FILE_NOTIFY_CHANGE_LAST_ACCESS) ||
+           (change_mask & FILE_NOTIFY_CHANGE_LAST_WRITE)) {
+               struct timeval tv[2];
+
+               nttime_to_timeval(&tv[0], newstats.dos.access_time);
+               nttime_to_timeval(&tv[1], newstats.dos.write_time);
+
+               if (!timeval_is_zero(&tv[0]) || !timeval_is_zero(&tv[1])) {
+                       if (utimes(h->name->full_name, tv) == -1) {
+                               DEBUG(0,("pvfs_setfileinfo: utimes() failed '%s' - %s\n",
+                                        h->name->full_name, strerror(errno)));
+                               return pvfs_map_errno(pvfs, errno);
+                       }
+               }
+       }
+       if (change_mask & FILE_NOTIFY_CHANGE_LAST_WRITE) {
+               struct odb_lock *lck;
+
+               lck = odb_lock(req, h->pvfs->odb_context, &h->odb_locking_key);
+               if (lck == NULL) {
+                       DEBUG(0,("Unable to lock opendb for write time update\n"));
+                       return NT_STATUS_INTERNAL_ERROR;
                }
+
+               status = odb_set_write_time(lck, newstats.dos.write_time, true);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0,("Unable to update write time: %s\n",
+                               nt_errstr(status)));
+                       talloc_free(lck);
+                       return status;
+               }
+
+               talloc_free(lck);
+
+               h->write_time.update_forced = true;
+               h->write_time.update_on_close = false;
+               talloc_free(h->write_time.update_event);
+               h->write_time.update_event = NULL;
        }
 
        /* possibly change the attribute */
        if (newstats.dos.attrib != h->name->dos.attrib) {
-               mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
+               mode_t mode;
+               if ((newstats.dos.attrib & FILE_ATTRIBUTE_DIRECTORY) &&
+                   !(h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+               mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
                if (!(h->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
                        if (fchmod(h->fd, mode) == -1) {
                                return pvfs_map_errno(pvfs, errno);
@@ -548,7 +610,8 @@ static NTSTATUS pvfs_setpathinfo_setup_retry(struct ntvfs_module_context *ntvfs,
                                             struct odb_lock *lck,
                                             NTSTATUS status)
 {
-       struct pvfs_state *pvfs = ntvfs->private_data;
+       struct pvfs_state *pvfs = talloc_get_type(ntvfs->private_data,
+                                 struct pvfs_state);
        struct timeval end_time;
 
        if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
@@ -571,11 +634,11 @@ static NTSTATUS pvfs_setpathinfo_setup_retry(struct ntvfs_module_context *ntvfs,
 NTSTATUS pvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
                          struct ntvfs_request *req, union smb_setfileinfo *info)
 {
-       struct pvfs_state *pvfs = ntvfs->private_data;
+       struct pvfs_state *pvfs = talloc_get_type(ntvfs->private_data,
+                                 struct pvfs_state);
        struct pvfs_filename *name;
        struct pvfs_filename newstats;
        NTSTATUS status;
-       struct utimbuf unix_times;
        uint32_t access_needed;
        uint32_t change_mask = 0;
        struct odb_lock *lck = NULL;
@@ -713,7 +776,7 @@ NTSTATUS pvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
        case RAW_SFILEINFO_RENAME_INFORMATION_SMB2:
                status = pvfs_locking_key(name, name, &odb_locking_key);
                NT_STATUS_NOT_OK_RETURN(status);
-               status = pvfs_setfileinfo_rename(pvfs, req, name,
+               status = pvfs_setfileinfo_rename(pvfs, req, name, -1,
                                                 &odb_locking_key, info);
                NT_STATUS_NOT_OK_RETURN(status);
                return NT_STATUS_OK;
@@ -741,21 +804,51 @@ NTSTATUS pvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
        }
 
        /* possibly change the file timestamps */
-       ZERO_STRUCT(unix_times);
        if (newstats.dos.create_time != name->dos.create_time) {
                change_mask |= FILE_NOTIFY_CHANGE_CREATION;
        }
        if (newstats.dos.access_time != name->dos.access_time) {
-               unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
                change_mask |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
        }
        if (newstats.dos.write_time != name->dos.write_time) {
-               unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
                change_mask |= FILE_NOTIFY_CHANGE_LAST_WRITE;
        }
-       if (unix_times.actime != 0 || unix_times.modtime != 0) {
-               if (utime(name->full_name, &unix_times) == -1) {
-                       return pvfs_map_errno(pvfs, errno);
+       if ((change_mask & FILE_NOTIFY_CHANGE_LAST_ACCESS) ||
+           (change_mask & FILE_NOTIFY_CHANGE_LAST_WRITE)) {
+               struct timeval tv[2];
+
+               nttime_to_timeval(&tv[0], newstats.dos.access_time);
+               nttime_to_timeval(&tv[1], newstats.dos.write_time);
+
+               if (!timeval_is_zero(&tv[0]) || !timeval_is_zero(&tv[1])) {
+                       if (utimes(name->full_name, tv) == -1) {
+                               DEBUG(0,("pvfs_setpathinfo: utimes() failed '%s' - %s\n",
+                                        name->full_name, strerror(errno)));
+                               return pvfs_map_errno(pvfs, errno);
+                       }
+               }
+       }
+       if (change_mask & FILE_NOTIFY_CHANGE_LAST_WRITE) {
+               if (lck == NULL) {
+                       DATA_BLOB lkey;
+                       status = pvfs_locking_key(name, name, &lkey);
+                       NT_STATUS_NOT_OK_RETURN(status);
+
+                       lck = odb_lock(req, pvfs->odb_context, &lkey);
+                       data_blob_free(&lkey);
+                       if (lck == NULL) {
+                               DEBUG(0,("Unable to lock opendb for write time update\n"));
+                               return NT_STATUS_INTERNAL_ERROR;
+                       }
+               }
+
+               status = odb_set_write_time(lck, newstats.dos.write_time, true);
+               if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
+                       /* it could be that nobody has opened the file */
+               } else if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0,("Unable to update write time: %s\n",
+                               nt_errstr(status)));
+                       return status;
                }
        }