Fix bug #10097 - MacOSX 10.9 will not follow path-based DFS referrals handed out...
[samba.git] / source3 / smbd / dosmode.c
index 307da32bc3eeff93c2f93cba669754ea4073009a..326e995be778b94b9259861e1ec0fcd80bf7560c 100644 (file)
 */
 
 #include "includes.h"
+#include "system/filesys.h"
 #include "librpc/gen_ndr/ndr_xattr.h"
 #include "../libcli/security/security.h"
+#include "smbd/smbd.h"
 
 static uint32_t filter_mode_by_protocol(uint32_t mode)
 {
@@ -39,7 +41,7 @@ static int set_link_read_only_flag(const SMB_STRUCT_STAT *const sbuf)
 #ifdef S_ISLNK
 #if LINKS_READ_ONLY
        if (S_ISLNK(sbuf->st_mode) && S_ISDIR(sbuf->st_mode))
-               return aRONLY;
+               return FILE_ATTRIBUTE_READONLY;
 #endif
 #endif
        return 0;
@@ -171,36 +173,36 @@ static uint32 dos_mode_from_sbuf(connection_struct *conn,
        if (ro_opts == MAP_READONLY_YES) {
                /* Original Samba method - map inverse of user "w" bit. */
                if ((smb_fname->st.st_ex_mode & S_IWUSR) == 0) {
-                       result |= aRONLY;
+                       result |= FILE_ATTRIBUTE_READONLY;
                }
        } else if (ro_opts == MAP_READONLY_PERMISSIONS) {
                /* Check actual permissions for read-only. */
                if (!can_write_to_file(conn, smb_fname)) {
-                       result |= aRONLY;
+                       result |= FILE_ATTRIBUTE_READONLY;
                }
        } /* Else never set the readonly bit. */
 
        if (MAP_ARCHIVE(conn) && ((smb_fname->st.st_ex_mode & S_IXUSR) != 0))
-               result |= aARCH;
+               result |= FILE_ATTRIBUTE_ARCHIVE;
 
        if (MAP_SYSTEM(conn) && ((smb_fname->st.st_ex_mode & S_IXGRP) != 0))
-               result |= aSYSTEM;
+               result |= FILE_ATTRIBUTE_SYSTEM;
 
        if (MAP_HIDDEN(conn) && ((smb_fname->st.st_ex_mode & S_IXOTH) != 0))
-               result |= aHIDDEN;   
+               result |= FILE_ATTRIBUTE_HIDDEN;
 
        if (S_ISDIR(smb_fname->st.st_ex_mode))
-               result = aDIR | (result & aRONLY);
+               result = FILE_ATTRIBUTE_DIRECTORY | (result & FILE_ATTRIBUTE_READONLY);
 
        result |= set_link_read_only_flag(&smb_fname->st);
 
        DEBUG(8,("dos_mode_from_sbuf returning "));
 
-       if (result & aHIDDEN) DEBUG(8, ("h"));
-       if (result & aRONLY ) DEBUG(8, ("r"));
-       if (result & aSYSTEM) DEBUG(8, ("s"));
-       if (result & aDIR   ) DEBUG(8, ("d"));
-       if (result & aARCH  ) DEBUG(8, ("a"));
+       if (result & FILE_ATTRIBUTE_HIDDEN) DEBUG(8, ("h"));
+       if (result & FILE_ATTRIBUTE_READONLY ) DEBUG(8, ("r"));
+       if (result & FILE_ATTRIBUTE_SYSTEM) DEBUG(8, ("s"));
+       if (result & FILE_ATTRIBUTE_DIRECTORY   ) DEBUG(8, ("d"));
+       if (result & FILE_ATTRIBUTE_ARCHIVE  ) DEBUG(8, ("a"));
 
        DEBUG(8,("\n"));
        return result;
@@ -316,18 +318,18 @@ static bool get_ea_dos_attribute(connection_struct *conn,
        }
 
        if (S_ISDIR(smb_fname->st.st_ex_mode)) {
-               dosattr |= aDIR;
+               dosattr |= FILE_ATTRIBUTE_DIRECTORY;
        }
        /* FILE_ATTRIBUTE_SPARSE is valid on get but not on set. */
        *pattr = (uint32)(dosattr & (SAMBA_ATTRIBUTES_MASK|FILE_ATTRIBUTE_SPARSE));
 
        DEBUG(8,("get_ea_dos_attribute returning (0x%x)", dosattr));
 
-       if (dosattr & aHIDDEN) DEBUG(8, ("h"));
-       if (dosattr & aRONLY ) DEBUG(8, ("r"));
-       if (dosattr & aSYSTEM) DEBUG(8, ("s"));
-       if (dosattr & aDIR   ) DEBUG(8, ("d"));
-       if (dosattr & aARCH  ) DEBUG(8, ("a"));
+       if (dosattr & FILE_ATTRIBUTE_HIDDEN) DEBUG(8, ("h"));
+       if (dosattr & FILE_ATTRIBUTE_READONLY ) DEBUG(8, ("r"));
+       if (dosattr & FILE_ATTRIBUTE_SYSTEM) DEBUG(8, ("s"));
+       if (dosattr & FILE_ATTRIBUTE_DIRECTORY   ) DEBUG(8, ("d"));
+       if (dosattr & FILE_ATTRIBUTE_ARCHIVE  ) DEBUG(8, ("a"));
 
        DEBUG(8,("\n"));
 
@@ -346,8 +348,6 @@ static bool set_ea_dos_attribute(connection_struct *conn,
        struct xattr_DOSATTRIB dosattrib;
        enum ndr_err_code ndr_err;
        DATA_BLOB blob;
-       files_struct *fsp = NULL;
-       bool ret = false;
 
        if (!lp_store_dos_attributes(SNUM(conn))) {
                return False;
@@ -385,6 +385,9 @@ static bool set_ea_dos_attribute(connection_struct *conn,
        if (SMB_VFS_SETXATTR(conn, smb_fname->base_name,
                             SAMBA_XATTR_DOS_ATTRIB, blob.data, blob.length,
                             0) == -1) {
+               bool ret = false;
+               files_struct *fsp = NULL;
+
                if((errno != EPERM) && (errno != EACCES)) {
                        if (errno == ENOSYS
 #if defined(ENOTSUP)
@@ -417,9 +420,9 @@ static bool set_ea_dos_attribute(connection_struct *conn,
 
                if (!NT_STATUS_IS_OK(open_file_fchmod(conn, smb_fname,
                                                      &fsp)))
-                       return ret;
+                       return false;
                become_root();
-               if (SMB_VFS_SETXATTR(conn, smb_fname->base_name,
+               if (SMB_VFS_FSETXATTR(fsp,
                                     SAMBA_XATTR_DOS_ATTRIB, blob.data,
                                     blob.length, 0) == 0) {
                        ret = true;
@@ -462,7 +465,7 @@ uint32 dos_mode_msdfs(connection_struct *conn,
                /* Only . and .. are not hidden. */
                if (p[0] == '.' && !((p[1] == '\0') ||
                                (p[1] == '.' && p[2] == '\0'))) {
-                       result |= aHIDDEN;
+                       result |= FILE_ATTRIBUTE_HIDDEN;
                }
        }
 
@@ -470,9 +473,9 @@ uint32 dos_mode_msdfs(connection_struct *conn,
 
        /* Optimization : Only call is_hidden_path if it's not already
           hidden. */
-       if (!(result & aHIDDEN) &&
+       if (!(result & FILE_ATTRIBUTE_HIDDEN) &&
            IS_HIDDEN_PATH(conn, smb_fname->base_name)) {
-               result |= aHIDDEN;
+               result |= FILE_ATTRIBUTE_HIDDEN;
        }
 
        if (result == 0) {
@@ -481,13 +484,18 @@ uint32 dos_mode_msdfs(connection_struct *conn,
 
        result = filter_mode_by_protocol(result);
 
+       /*
+        * Add in that it is a reparse point
+        */
+       result |= FILE_ATTRIBUTE_REPARSE_POINT;
+
        DEBUG(8,("dos_mode_msdfs returning "));
 
-       if (result & aHIDDEN) DEBUG(8, ("h"));
-       if (result & aRONLY ) DEBUG(8, ("r"));
-       if (result & aSYSTEM) DEBUG(8, ("s"));
-       if (result & aDIR   ) DEBUG(8, ("d"));
-       if (result & aARCH  ) DEBUG(8, ("a"));
+       if (result & FILE_ATTRIBUTE_HIDDEN) DEBUG(8, ("h"));
+       if (result & FILE_ATTRIBUTE_READONLY ) DEBUG(8, ("r"));
+       if (result & FILE_ATTRIBUTE_SYSTEM) DEBUG(8, ("s"));
+       if (result & FILE_ATTRIBUTE_DIRECTORY   ) DEBUG(8, ("d"));
+       if (result & FILE_ATTRIBUTE_ARCHIVE  ) DEBUG(8, ("a"));
        if (result & FILE_ATTRIBUTE_SPARSE ) DEBUG(8, ("[sparse]"));
 
        DEBUG(8,("\n"));
@@ -504,13 +512,13 @@ int dos_attributes_to_stat_dos_flags(uint32_t dosmode)
 {
        uint32_t dos_stat_flags = 0;
 
-       if (dosmode & aARCH)
+       if (dosmode & FILE_ATTRIBUTE_ARCHIVE)
                dos_stat_flags |= UF_DOS_ARCHIVE;
-       if (dosmode & aHIDDEN)
+       if (dosmode & FILE_ATTRIBUTE_HIDDEN)
                dos_stat_flags |= UF_DOS_HIDDEN;
-       if (dosmode & aRONLY)
+       if (dosmode & FILE_ATTRIBUTE_READONLY)
                dos_stat_flags |= UF_DOS_RO;
-       if (dosmode & aSYSTEM)
+       if (dosmode & FILE_ATTRIBUTE_SYSTEM)
                dos_stat_flags |= UF_DOS_SYSTEM;
        if (dosmode & FILE_ATTRIBUTE_NONINDEXED)
                dos_stat_flags |= UF_DOS_NOINDEX;
@@ -537,19 +545,19 @@ static bool get_stat_dos_flags(connection_struct *conn,
                  smb_fname_str_dbg(smb_fname)));
 
        if (smb_fname->st.st_ex_flags & UF_DOS_ARCHIVE)
-               *dosmode |= aARCH;
+               *dosmode |= FILE_ATTRIBUTE_ARCHIVE;
        if (smb_fname->st.st_ex_flags & UF_DOS_HIDDEN)
-               *dosmode |= aHIDDEN;
+               *dosmode |= FILE_ATTRIBUTE_HIDDEN;
        if (smb_fname->st.st_ex_flags & UF_DOS_RO)
-               *dosmode |= aRONLY;
+               *dosmode |= FILE_ATTRIBUTE_READONLY;
        if (smb_fname->st.st_ex_flags & UF_DOS_SYSTEM)
-               *dosmode |= aSYSTEM;
+               *dosmode |= FILE_ATTRIBUTE_SYSTEM;
        if (smb_fname->st.st_ex_flags & UF_DOS_NOINDEX)
                *dosmode |= FILE_ATTRIBUTE_NONINDEXED;
        if (smb_fname->st.st_ex_flags & FILE_ATTRIBUTE_SPARSE)
                *dosmode |= FILE_ATTRIBUTE_SPARSE;
        if (S_ISDIR(smb_fname->st.st_ex_mode))
-               *dosmode |= aDIR;
+               *dosmode |= FILE_ATTRIBUTE_DIRECTORY;
 
        *dosmode |= set_link_read_only_flag(&smb_fname->st);
 
@@ -634,7 +642,7 @@ uint32 dos_mode(connection_struct *conn, struct smb_filename *smb_fname)
                /* Only . and .. are not hidden. */
                if (p[0] == '.' && !((p[1] == '\0') ||
                                (p[1] == '.' && p[2] == '\0'))) {
-                       result |= aHIDDEN;
+                       result |= FILE_ATTRIBUTE_HIDDEN;
                }
        }
 
@@ -655,9 +663,9 @@ uint32 dos_mode(connection_struct *conn, struct smb_filename *smb_fname)
 
        /* Optimization : Only call is_hidden_path if it's not already
           hidden. */
-       if (!(result & aHIDDEN) &&
+       if (!(result & FILE_ATTRIBUTE_HIDDEN) &&
            IS_HIDDEN_PATH(conn, smb_fname->base_name)) {
-               result |= aHIDDEN;
+               result |= FILE_ATTRIBUTE_HIDDEN;
        }
 
        if (result == 0) {
@@ -668,11 +676,11 @@ uint32 dos_mode(connection_struct *conn, struct smb_filename *smb_fname)
 
        DEBUG(8,("dos_mode returning "));
 
-       if (result & aHIDDEN) DEBUG(8, ("h"));
-       if (result & aRONLY ) DEBUG(8, ("r"));
-       if (result & aSYSTEM) DEBUG(8, ("s"));
-       if (result & aDIR   ) DEBUG(8, ("d"));
-       if (result & aARCH  ) DEBUG(8, ("a"));
+       if (result & FILE_ATTRIBUTE_HIDDEN) DEBUG(8, ("h"));
+       if (result & FILE_ATTRIBUTE_READONLY ) DEBUG(8, ("r"));
+       if (result & FILE_ATTRIBUTE_SYSTEM) DEBUG(8, ("s"));
+       if (result & FILE_ATTRIBUTE_DIRECTORY   ) DEBUG(8, ("d"));
+       if (result & FILE_ATTRIBUTE_ARCHIVE  ) DEBUG(8, ("a"));
        if (result & FILE_ATTRIBUTE_SPARSE ) DEBUG(8, ("[sparse]"));
 
        DEBUG(8,("\n"));
@@ -709,9 +717,9 @@ int file_set_dosmode(connection_struct *conn, struct smb_filename *smb_fname,
                           &smb_fname->st.st_ex_mode);
 
        if (S_ISDIR(smb_fname->st.st_ex_mode))
-               dosmode |= aDIR;
+               dosmode |= FILE_ATTRIBUTE_DIRECTORY;
        else
-               dosmode &= ~aDIR;
+               dosmode &= ~FILE_ATTRIBUTE_DIRECTORY;
 
        new_create_timespec = smb_fname->st.st_ex_btime;
 
@@ -719,7 +727,7 @@ int file_set_dosmode(connection_struct *conn, struct smb_filename *smb_fname,
 
        if (dosmode & FILE_ATTRIBUTE_OFFLINE) {
                if (!(old_mode & FILE_ATTRIBUTE_OFFLINE)) {
-                       lret = SMB_VFS_SET_OFFLINE(conn, smb_fname->base_name);
+                       lret = SMB_VFS_SET_OFFLINE(conn, smb_fname);
                        if (lret == -1) {
                                DEBUG(0, ("set_dos_mode: client has asked to "
                                          "set FILE_ATTRIBUTE_OFFLINE to "
@@ -796,6 +804,27 @@ int file_set_dosmode(connection_struct *conn, struct smb_filename *smb_fname,
                unixmode |= (smb_fname->st.st_ex_mode & (S_IWUSR|S_IWGRP|S_IWOTH));
        }
 
+       /*
+        * From the chmod 2 man page:
+        *
+        * "If the calling process is not privileged, and the group of the file
+        * does not match the effective group ID of the process or one of its
+        * supplementary group IDs, the S_ISGID bit will be turned off, but
+        * this will not cause an error to be returned."
+        *
+        * Simply refuse to do the chmod in this case.
+        */
+
+       if (S_ISDIR(smb_fname->st.st_ex_mode) && (unixmode & S_ISGID) &&
+                       geteuid() != sec_initial_uid() &&
+                       !current_user_in_group(conn, smb_fname->st.st_ex_gid)) {
+               DEBUG(3,("file_set_dosmode: setgid bit cannot be "
+                       "set for directory %s\n",
+                       smb_fname_str_dbg(smb_fname)));
+               errno = EPERM;
+               return -1;
+       }
+
        ret = SMB_VFS_CHMOD(conn, smb_fname->base_name, unixmode);
        if (ret == 0) {
                if(!newfile || (lret != -1)) {