notifyd: Use messaging_register for MSG_SMB_NOTIFY_GET_DB
[vlendec/samba-autobuild/.git] / source3 / smbd / dosmode.c
index 0f3eef04492c5a94e71bab952ddea7be337670b0..0ee6894785169a8ba72dca77eb475a36066ae0ad 100644 (file)
@@ -27,7 +27,7 @@
 #include "lib/param/loadparm.h"
 
 static NTSTATUS get_file_handle_for_metadata(connection_struct *conn,
-                               struct smb_filename *smb_fname,
+                               const struct smb_filename *smb_fname,
                                files_struct **ret_fsp,
                                bool *need_close);
 
@@ -135,8 +135,11 @@ mode_t unix_mode(connection_struct *conn, int dosmode,
                          smb_fname_str_dbg(smb_fname),
                          inherit_from_dir));
 
-               smb_fname_parent = synthetic_smb_fname(
-                       talloc_tos(), inherit_from_dir, NULL, NULL);
+               smb_fname_parent = synthetic_smb_fname(talloc_tos(),
+                                       inherit_from_dir,
+                                       NULL,
+                                       NULL,
+                                       smb_fname->flags);
                if (smb_fname_parent == NULL) {
                        DEBUG(1,("unix_mode(%s) failed, [dir %s]: No memory\n",
                                 smb_fname_str_dbg(smb_fname),
@@ -257,9 +260,9 @@ static uint32_t dos_mode_from_sbuf(connection_struct *conn,
  This can also pull the create time into the stat struct inside smb_fname.
 ****************************************************************************/
 
-static bool get_ea_dos_attribute(connection_struct *conn,
-                                struct smb_filename *smb_fname,
-                                uint32_t *pattr)
+NTSTATUS get_ea_dos_attribute(connection_struct *conn,
+                             struct smb_filename *smb_fname,
+                             uint32_t *pattr)
 {
        struct xattr_DOSATTRIB dosattrib;
        enum ndr_err_code ndr_err;
@@ -269,29 +272,20 @@ static bool get_ea_dos_attribute(connection_struct *conn,
        uint32_t dosattr;
 
        if (!lp_store_dos_attributes(SNUM(conn))) {
-               return False;
+               return NT_STATUS_NOT_IMPLEMENTED;
        }
 
        /* Don't reset pattr to zero as we may already have filename-based attributes we
           need to preserve. */
 
-       sizeret = SMB_VFS_GETXATTR(conn, smb_fname->base_name,
+       sizeret = SMB_VFS_GETXATTR(conn, smb_fname,
                                   SAMBA_XATTR_DOS_ATTRIB, attrstr,
                                   sizeof(attrstr));
        if (sizeret == -1) {
-               if (errno == ENOSYS
-#if defined(ENOTSUP)
-                       || errno == ENOTSUP) {
-#else
-                               ) {
-#endif
-                       DEBUG(1,("get_ea_dos_attribute: Cannot get attribute "
-                                "from EA on file %s: Error = %s\n",
-                                smb_fname_str_dbg(smb_fname),
-                                strerror(errno)));
-                       set_store_dos_attributes(SNUM(conn), False);
-               }
-               return False;
+               DBG_INFO("Cannot get attribute "
+                        "from EA on file %s: Error = %s\n",
+                        smb_fname_str_dbg(smb_fname), strerror(errno));
+               return map_nt_error_from_unix(errno);
        }
 
        blob.data = (uint8_t *)attrstr;
@@ -305,7 +299,7 @@ static bool get_ea_dos_attribute(connection_struct *conn,
                         "from EA on file %s: Error = %s\n",
                         smb_fname_str_dbg(smb_fname),
                         ndr_errstr(ndr_err)));
-               return false;
+               return ndr_map_error2ntstatus(ndr_err);
        }
 
        DEBUG(10,("get_ea_dos_attribute: %s attr = %s\n",
@@ -358,18 +352,19 @@ static bool get_ea_dos_attribute(connection_struct *conn,
                        DEBUG(1,("get_ea_dos_attribute: Badly formed DOSATTRIB on "
                                 "file %s - %s\n", smb_fname_str_dbg(smb_fname),
                                 attrstr));
-                       return false;
+                       /* Should this be INTERNAL_ERROR? */
+                       return NT_STATUS_INVALID_PARAMETER;
        }
 
        if (S_ISDIR(smb_fname->st.st_ex_mode)) {
                dosattr |= FILE_ATTRIBUTE_DIRECTORY;
        }
        /* FILE_ATTRIBUTE_SPARSE is valid on get but not on set. */
-       *pattr = (uint32_t)(dosattr & (SAMBA_ATTRIBUTES_MASK|FILE_ATTRIBUTE_SPARSE));
+       *pattr |= (uint32_t)(dosattr & (SAMBA_ATTRIBUTES_MASK|FILE_ATTRIBUTE_SPARSE));
 
        dos_mode_debug_print(__func__, *pattr);
 
-       return True;
+       return NT_STATUS_OK;
 }
 
 /****************************************************************************
@@ -377,14 +372,24 @@ static bool get_ea_dos_attribute(connection_struct *conn,
  Also sets the create time.
 ****************************************************************************/
 
-static bool set_ea_dos_attribute(connection_struct *conn,
-                                struct smb_filename *smb_fname,
-                                uint32_t dosmode)
+NTSTATUS set_ea_dos_attribute(connection_struct *conn,
+                             const struct smb_filename *smb_fname,
+                             uint32_t dosmode)
 {
        struct xattr_DOSATTRIB dosattrib;
        enum ndr_err_code ndr_err;
        DATA_BLOB blob;
 
+       if (!lp_store_dos_attributes(SNUM(conn))) {
+               return NT_STATUS_NOT_IMPLEMENTED;
+       }
+
+       /*
+        * Don't store FILE_ATTRIBUTE_OFFLINE, it's dealt with in
+        * vfs_default via DMAPI if that is enabled.
+        */
+       dosmode &= ~FILE_ATTRIBUTE_OFFLINE;
+
        ZERO_STRUCT(dosattrib);
        ZERO_STRUCT(blob);
 
@@ -407,34 +412,26 @@ static bool set_ea_dos_attribute(connection_struct *conn,
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                DEBUG(5, ("create_acl_blob: ndr_push_xattr_DOSATTRIB failed: %s\n",
                        ndr_errstr(ndr_err)));
-               return false;
+               return ndr_map_error2ntstatus(ndr_err);
        }
 
        if (blob.data == NULL || blob.length == 0) {
-               return false;
+               /* Should this be INTERNAL_ERROR? */
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
-       if (SMB_VFS_SETXATTR(conn, smb_fname->base_name,
+       if (SMB_VFS_SETXATTR(conn, smb_fname,
                             SAMBA_XATTR_DOS_ATTRIB, blob.data, blob.length,
                             0) == -1) {
-               bool ret = false;
+               NTSTATUS status = NT_STATUS_OK;
                bool need_close = false;
                files_struct *fsp = NULL;
 
                if((errno != EPERM) && (errno != EACCES)) {
-                       if (errno == ENOSYS
-#if defined(ENOTSUP)
-                               || errno == ENOTSUP) {
-#else
-                               ) {
-#endif
-                               DEBUG(1,("set_ea_dos_attributes: Cannot set "
-                                        "attribute EA on file %s: Error = %s\n",
-                                        smb_fname_str_dbg(smb_fname),
-                                        strerror(errno) ));
-                               set_store_dos_attributes(SNUM(conn), False);
-                       }
-                       return false;
+                       DBG_INFO("Cannot set "
+                                "attribute EA on file %s: Error = %s\n",
+                                smb_fname_str_dbg(smb_fname), strerror(errno));
+                       return map_nt_error_from_unix(errno);
                }
 
                /* We want DOS semantics, ie allow non owner with write permission to change the
@@ -443,10 +440,10 @@ static bool set_ea_dos_attribute(connection_struct *conn,
 
                /* Check if we have write access. */
                if(!CAN_WRITE(conn) || !lp_dos_filemode(SNUM(conn)))
-                       return false;
+                       return NT_STATUS_ACCESS_DENIED;
 
                if (!can_write_to_file(conn, smb_fname)) {
-                       return false;
+                       return NT_STATUS_ACCESS_DENIED;
                }
 
                /*
@@ -454,29 +451,30 @@ static bool set_ea_dos_attribute(connection_struct *conn,
                 * metadata operation under root.
                 */
 
-               if (!NT_STATUS_IS_OK(get_file_handle_for_metadata(conn,
+               status = get_file_handle_for_metadata(conn,
                                                smb_fname,
                                                &fsp,
-                                               &need_close))) {
-                       return false;
+                                               &need_close);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
                }
 
                become_root();
                if (SMB_VFS_FSETXATTR(fsp,
                                     SAMBA_XATTR_DOS_ATTRIB, blob.data,
                                     blob.length, 0) == 0) {
-                       ret = true;
+                       status = NT_STATUS_OK;
                }
                unbecome_root();
                if (need_close) {
                        close_file(NULL, fsp, NORMAL_CLOSE);
                }
-               return ret;
+               return status;
        }
        DEBUG(10,("set_ea_dos_attribute: set EA 0x%x on file %s\n",
                (unsigned int)dosmode,
                smb_fname_str_dbg(smb_fname)));
-       return true;
+       return NT_STATUS_OK;
 }
 
 /****************************************************************************
@@ -570,6 +568,40 @@ err_out:
        return status;
 }
 
+static uint32_t dos_mode_from_name(connection_struct *conn,
+                                  const struct smb_filename *smb_fname,
+                                  uint32_t dosmode)
+{
+       const char *p = NULL;
+       uint32_t result = dosmode;
+
+       if (!(result & FILE_ATTRIBUTE_HIDDEN) &&
+           lp_hide_dot_files(SNUM(conn)))
+       {
+               p = strrchr_m(smb_fname->base_name, '/');
+               if (p) {
+                       p++;
+               } else {
+                       p = smb_fname->base_name;
+               }
+
+               /* Only . and .. are not hidden. */
+               if ((p[0] == '.') &&
+                   !((p[1] == '\0') || (p[1] == '.' && p[2] == '\0')))
+               {
+                       result |= FILE_ATTRIBUTE_HIDDEN;
+               }
+       }
+
+       if (!(result & FILE_ATTRIBUTE_HIDDEN) &&
+           IS_HIDDEN_PATH(conn, smb_fname->base_name))
+       {
+               result |= FILE_ATTRIBUTE_HIDDEN;
+       }
+
+       return result;
+}
+
 /****************************************************************************
  Change a unix mode to a dos mode.
  May also read the create timespec into the stat struct in smb_fname
@@ -579,7 +611,7 @@ err_out:
 uint32_t dos_mode(connection_struct *conn, struct smb_filename *smb_fname)
 {
        uint32_t result = 0;
-       bool offline;
+       NTSTATUS status = NT_STATUS_OK;
 
        DEBUG(8,("dos_mode: %s\n", smb_fname_str_dbg(smb_fname)));
 
@@ -587,50 +619,26 @@ uint32_t dos_mode(connection_struct *conn, struct smb_filename *smb_fname)
                return 0;
        }
 
-       /* First do any modifications that depend on the path name. */
-       /* hide files with a name starting with a . */
-       if (lp_hide_dot_files(SNUM(conn))) {
-               const char *p = strrchr_m(smb_fname->base_name,'/');
-               if (p) {
-                       p++;
-               } else {
-                       p = smb_fname->base_name;
-               }
-
-               /* Only . and .. are not hidden. */
-               if (p[0] == '.' && !((p[1] == '\0') ||
-                               (p[1] == '.' && p[2] == '\0'))) {
-                       result |= FILE_ATTRIBUTE_HIDDEN;
-               }
-       }
-
-       /* Get the DOS attributes from an EA by preference. */
-       if (!get_ea_dos_attribute(conn, smb_fname, &result)) {
+       /* Get the DOS attributes via the VFS if we can */
+       status = SMB_VFS_GET_DOS_ATTRIBUTES(conn, smb_fname, &result);
+       if (!NT_STATUS_IS_OK(status)) {
                result |= dos_mode_from_sbuf(conn, smb_fname);
        }
 
-       offline = SMB_VFS_IS_OFFLINE(conn, smb_fname, &smb_fname->st);
-       if (S_ISREG(smb_fname->st.st_ex_mode) && offline) {
-               result |= FILE_ATTRIBUTE_OFFLINE;
-       }
-
        if (conn->fs_capabilities & FILE_FILE_COMPRESSION) {
                bool compressed = false;
-               NTSTATUS status = dos_mode_check_compressed(conn, smb_fname,
-                                                           &compressed);
+               status = dos_mode_check_compressed(conn, smb_fname,
+                                                  &compressed);
                if (NT_STATUS_IS_OK(status) && compressed) {
                        result |= FILE_ATTRIBUTE_COMPRESSED;
                }
        }
 
-       /* Optimization : Only call is_hidden_path if it's not already
-          hidden. */
-       if (!(result & FILE_ATTRIBUTE_HIDDEN) &&
-           IS_HIDDEN_PATH(conn, smb_fname->base_name)) {
-               result |= FILE_ATTRIBUTE_HIDDEN;
-       }
+       result |= dos_mode_from_name(conn, smb_fname, result);
 
-       if (result == 0) {
+       if (S_ISDIR(smb_fname->st.st_ex_mode)) {
+               result |= FILE_ATTRIBUTE_DIRECTORY;
+       } else if (result == 0) {
                result = FILE_ATTRIBUTE_NORMAL;
        }
 
@@ -655,8 +663,6 @@ int file_set_dosmode(connection_struct *conn, struct smb_filename *smb_fname,
        mode_t tmp;
        mode_t unixmode;
        int ret = -1, lret = -1;
-       uint32_t old_mode;
-       struct timespec new_create_timespec;
        files_struct *fsp = NULL;
        bool need_close = false;
        NTSTATUS status;
@@ -666,68 +672,43 @@ int file_set_dosmode(connection_struct *conn, struct smb_filename *smb_fname,
                return -1;
        }
 
-       /* We only allow READONLY|HIDDEN|SYSTEM|DIRECTORY|ARCHIVE here. */
-       dosmode &= (SAMBA_ATTRIBUTES_MASK | FILE_ATTRIBUTE_OFFLINE);
+       dosmode &= SAMBA_ATTRIBUTES_MASK;
 
        DEBUG(10,("file_set_dosmode: setting dos mode 0x%x on file %s\n",
                  dosmode, smb_fname_str_dbg(smb_fname)));
 
        unixmode = smb_fname->st.st_ex_mode;
 
-       get_acl_group_bits(conn, smb_fname->base_name,
-                          &smb_fname->st.st_ex_mode);
+       get_acl_group_bits(conn, smb_fname,
+                       &smb_fname->st.st_ex_mode);
 
        if (S_ISDIR(smb_fname->st.st_ex_mode))
                dosmode |= FILE_ATTRIBUTE_DIRECTORY;
        else
                dosmode &= ~FILE_ATTRIBUTE_DIRECTORY;
 
-       new_create_timespec = smb_fname->st.st_ex_btime;
-
-       old_mode = dos_mode(conn, smb_fname);
-
-       if ((dosmode & FILE_ATTRIBUTE_OFFLINE) &&
-           !(old_mode & FILE_ATTRIBUTE_OFFLINE)) {
-               lret = SMB_VFS_SET_OFFLINE(conn, smb_fname);
-               if (lret == -1) {
-                       if (errno == ENOTSUP) {
-                               DEBUG(10, ("Setting FILE_ATTRIBUTE_OFFLINE for "
-                                          "%s/%s is not supported.\n",
-                                          parent_dir,
-                                          smb_fname_str_dbg(smb_fname)));
-                       } else {
-                               DEBUG(0, ("An error occurred while setting "
-                                         "FILE_ATTRIBUTE_OFFLINE for "
-                                         "%s/%s: %s", parent_dir,
-                                         smb_fname_str_dbg(smb_fname),
-                                         strerror(errno)));
-                       }
-               }
-       }
-
-       dosmode  &= ~FILE_ATTRIBUTE_OFFLINE;
-       old_mode &= ~FILE_ATTRIBUTE_OFFLINE;
-
-       smb_fname->st.st_ex_btime = new_create_timespec;
-
        /* Store the DOS attributes in an EA by preference. */
-       if (lp_store_dos_attributes(SNUM(conn))) {
-               /*
-                * Don't fall back to using UNIX modes. Finally
-                * follow the smb.conf manpage.
-                */
-               if (!set_ea_dos_attribute(conn, smb_fname, dosmode)) {
-                       return -1;
-               }
+       status = SMB_VFS_SET_DOS_ATTRIBUTES(conn, smb_fname, dosmode);
+       if (NT_STATUS_IS_OK(status)) {
                if (!newfile) {
                        notify_fname(conn, NOTIFY_ACTION_MODIFIED,
-                                    FILE_NOTIFY_CHANGE_ATTRIBUTES,
-                                    smb_fname->base_name);
+                               FILE_NOTIFY_CHANGE_ATTRIBUTES,
+                               smb_fname->base_name);
                }
                smb_fname->st.st_ex_mode = unixmode;
                return 0;
+       } else {
+               /*
+                * Only fall back to using UNIX modes if
+                * we get NOT_IMPLEMENTED.
+                */
+               if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
+                       errno = map_errno_from_nt_status(status);
+                       return -1;
+               }
        }
 
+       /* Fall back to UNIX modes. */
        unixmode = unix_mode(conn, dosmode, smb_fname, parent_dir);
 
        /* preserve the file type bits */
@@ -784,7 +765,7 @@ int file_set_dosmode(connection_struct *conn, struct smb_filename *smb_fname,
                return -1;
        }
 
-       ret = SMB_VFS_CHMOD(conn, smb_fname->base_name, unixmode);
+       ret = SMB_VFS_CHMOD(conn, smb_fname, unixmode);
        if (ret == 0) {
                if(!newfile || (lret != -1)) {
                        notify_fname(conn, NOTIFY_ACTION_MODIFIED,
@@ -911,12 +892,9 @@ NTSTATUS file_set_sparse(connection_struct *conn,
        }
 
        /* Store the DOS attributes in an EA. */
-       if (!set_ea_dos_attribute(conn, fsp->fsp_name,
-                                 new_dosmode)) {
-               if (errno == 0) {
-                       errno = EIO;
-               }
-               return map_nt_error_from_unix(errno);
+       status = SMB_VFS_FSET_DOS_ATTRIBUTES(conn, fsp, new_dosmode);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
        notify_fname(conn, NOTIFY_ACTION_MODIFIED,
@@ -1040,8 +1018,11 @@ NTSTATUS set_create_timespec_ea(connection_struct *conn,
                return NT_STATUS_OK;
        }
 
-       smb_fname = synthetic_smb_fname(talloc_tos(), psmb_fname->base_name,
-                                       NULL, &psmb_fname->st);
+       smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       psmb_fname->base_name,
+                                       NULL,
+                                       &psmb_fname->st,
+                                       psmb_fname->flags);
 
        if (smb_fname == NULL) {
                return NT_STATUS_NO_MEMORY;
@@ -1053,7 +1034,7 @@ NTSTATUS set_create_timespec_ea(connection_struct *conn,
 
        ret = file_set_dosmode(conn, smb_fname, dosmode, NULL, false);
        if (ret == -1) {
-               map_nt_error_from_unix(errno);
+               return map_nt_error_from_unix(errno);
        }
 
        DEBUG(10,("set_create_timespec_ea: wrote create time EA for file %s\n",
@@ -1092,13 +1073,14 @@ struct timespec get_change_timespec(connection_struct *conn,
 ****************************************************************************/
 
 static NTSTATUS get_file_handle_for_metadata(connection_struct *conn,
-                               struct smb_filename *smb_fname,
+                               const struct smb_filename *smb_fname,
                                files_struct **ret_fsp,
                                bool *need_close)
 {
        NTSTATUS status;
        files_struct *fsp;
        struct file_id file_id;
+       struct smb_filename *smb_fname_cp = NULL;
 
        *need_close = false;
 
@@ -1117,12 +1099,18 @@ static NTSTATUS get_file_handle_for_metadata(connection_struct *conn,
                }
        }
 
+       smb_fname_cp = cp_smb_filename(talloc_tos(),
+                                       smb_fname);
+       if (smb_fname_cp == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
        /* Opens an INTERNAL_OPEN_ONLY write handle. */
        status = SMB_VFS_CREATE_FILE(
                conn,                                   /* conn */
                NULL,                                   /* req */
                0,                                      /* root_dir_fid */
-               smb_fname,                              /* fname */
+               smb_fname_cp,                           /* fname */
                FILE_WRITE_DATA,                        /* access_mask */
                (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
                        FILE_SHARE_DELETE),
@@ -1139,6 +1127,8 @@ static NTSTATUS get_file_handle_for_metadata(connection_struct *conn,
                NULL,                                   /* pinfo */
                NULL, NULL);                            /* create context */
 
+       TALLOC_FREE(smb_fname_cp);
+
        if (NT_STATUS_IS_OK(status)) {
                *need_close = true;
        }