LCOV - code coverage report
Current view: top level - source3/smbd - dosmode.c (source / functions) Hit Total Coverage
Test: coverage report for smb2.twrp.listdir_fix f886ca1c Lines: 329 521 63.1 %
Date: 2023-11-07 19:11:32 Functions: 22 23 95.7 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    dos mode handling functions
       4             :    Copyright (C) Andrew Tridgell 1992-1998
       5             :    Copyright (C) James Peach 2006
       6             : 
       7             :    This program is free software; you can redistribute it and/or modify
       8             :    it under the terms of the GNU General Public License as published by
       9             :    the Free Software Foundation; either version 3 of the License, or
      10             :    (at your option) any later version.
      11             : 
      12             :    This program is distributed in the hope that it will be useful,
      13             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :    GNU General Public License for more details.
      16             : 
      17             :    You should have received a copy of the GNU General Public License
      18             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             : */
      20             : 
      21             : #include "includes.h"
      22             : #include "globals.h"
      23             : #include "system/filesys.h"
      24             : #include "librpc/gen_ndr/ndr_xattr.h"
      25             : #include "librpc/gen_ndr/ioctl.h"
      26             : #include "../libcli/security/security.h"
      27             : #include "smbd/smbd.h"
      28             : #include "lib/param/loadparm.h"
      29             : #include "lib/util/tevent_ntstatus.h"
      30             : #include "lib/util/string_wrappers.h"
      31             : #include "fake_file.h"
      32             : 
      33     2558755 : static void dos_mode_debug_print(const char *func, uint32_t mode)
      34             : {
      35        3702 :         fstring modestr;
      36             : 
      37     2558755 :         if (DEBUGLEVEL < DBGLVL_INFO) {
      38     2558755 :                 return;
      39             :         }
      40             : 
      41           0 :         modestr[0] = '\0';
      42             : 
      43           0 :         if (mode & FILE_ATTRIBUTE_HIDDEN) {
      44           0 :                 fstrcat(modestr, "h");
      45             :         }
      46           0 :         if (mode & FILE_ATTRIBUTE_READONLY) {
      47           0 :                 fstrcat(modestr, "r");
      48             :         }
      49           0 :         if (mode & FILE_ATTRIBUTE_SYSTEM) {
      50           0 :                 fstrcat(modestr, "s");
      51             :         }
      52           0 :         if (mode & FILE_ATTRIBUTE_DIRECTORY) {
      53           0 :                 fstrcat(modestr, "d");
      54             :         }
      55           0 :         if (mode & FILE_ATTRIBUTE_ARCHIVE) {
      56           0 :                 fstrcat(modestr, "a");
      57             :         }
      58           0 :         if (mode & FILE_ATTRIBUTE_SPARSE) {
      59           0 :                 fstrcat(modestr, "[sparse]");
      60             :         }
      61           0 :         if (mode & FILE_ATTRIBUTE_OFFLINE) {
      62           0 :                 fstrcat(modestr, "[offline]");
      63             :         }
      64           0 :         if (mode & FILE_ATTRIBUTE_COMPRESSED) {
      65           0 :                 fstrcat(modestr, "[compressed]");
      66             :         }
      67             : 
      68           0 :         DBG_INFO("%s returning (0x%x): \"%s\"\n", func, (unsigned)mode,
      69             :                  modestr);
      70             : }
      71             : 
      72     1217483 : static uint32_t filter_mode_by_protocol(uint32_t mode)
      73             : {
      74     1217483 :         if (get_Protocol() <= PROTOCOL_LANMAN2) {
      75         124 :                 DEBUG(10,("filter_mode_by_protocol: "
      76             :                         "filtering result 0x%x to 0x%x\n",
      77             :                         (unsigned int)mode,
      78             :                         (unsigned int)(mode & 0x3f) ));
      79         124 :                 mode &= 0x3f;
      80             :         }
      81     1217483 :         return mode;
      82             : }
      83             : 
      84             : /****************************************************************************
      85             :  Change a dos mode to a unix mode.
      86             :     Base permission for files:
      87             :          if creating file and inheriting (i.e. parent_dir != NULL)
      88             :            apply read/write bits from parent directory.
      89             :          else
      90             :            everybody gets read bit set
      91             :          dos readonly is represented in unix by removing everyone's write bit
      92             :          dos archive is represented in unix by the user's execute bit
      93             :          dos system is represented in unix by the group's execute bit
      94             :          dos hidden is represented in unix by the other's execute bit
      95             :          if !inheriting {
      96             :            Then apply create mask,
      97             :            then add force bits.
      98             :          }
      99             :     Base permission for directories:
     100             :          dos directory is represented in unix by unix's dir bit and the exec bit
     101             :          if !inheriting {
     102             :            Then apply create mask,
     103             :            then add force bits.
     104             :          }
     105             : ****************************************************************************/
     106             : 
     107      610376 : mode_t unix_mode(connection_struct *conn, int dosmode,
     108             :                  const struct smb_filename *smb_fname,
     109             :                  struct files_struct *parent_dirfsp)
     110             : {
     111      610376 :         mode_t result = (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
     112      610376 :         mode_t dir_mode = 0; /* Mode of the inherit_from directory if
     113             :                               * inheriting. */
     114             : 
     115      610498 :         if ((dosmode & FILE_ATTRIBUTE_READONLY) &&
     116        1123 :             !lp_store_dos_attributes(SNUM(conn))) {
     117           0 :                 result &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
     118             :         }
     119             : 
     120      610376 :         if ((parent_dirfsp != NULL) && lp_inherit_permissions(SNUM(conn))) {
     121           0 :                 struct stat_ex sbuf = { .st_ex_nlink = 0, };
     122           0 :                 int ret;
     123             : 
     124           0 :                 DBG_DEBUG("[%s] inheriting from [%s]\n",
     125             :                           smb_fname_str_dbg(smb_fname),
     126             :                           smb_fname_str_dbg(parent_dirfsp->fsp_name));
     127             : 
     128           0 :                 ret = SMB_VFS_FSTAT(parent_dirfsp, &sbuf);
     129           0 :                 if (ret != 0) {
     130           0 :                         DBG_ERR("fstat failed [%s]: %s\n",
     131             :                                 smb_fname_str_dbg(parent_dirfsp->fsp_name),
     132             :                                 strerror(errno));
     133           0 :                         return(0);      /* *** shouldn't happen! *** */
     134             :                 }
     135             : 
     136             :                 /* Save for later - but explicitly remove setuid bit for safety. */
     137           0 :                 dir_mode = sbuf.st_ex_mode & ~S_ISUID;
     138           0 :                 DEBUG(2,("unix_mode(%s) inherit mode %o\n",
     139             :                          smb_fname_str_dbg(smb_fname), (int)dir_mode));
     140             :                 /* Clear "result" */
     141           0 :                 result = 0;
     142             :         }
     143             : 
     144      610376 :         if (dosmode & FILE_ATTRIBUTE_DIRECTORY) {
     145             :                 /* We never make directories read only for the owner as under DOS a user
     146             :                 can always create a file in a read-only directory. */
     147       61700 :                 result |= (S_IFDIR | S_IWUSR);
     148             : 
     149       61700 :                 if (dir_mode) {
     150             :                         /* Inherit mode of parent directory. */
     151           0 :                         result |= dir_mode;
     152             :                 } else {
     153             :                         /* Provisionally add all 'x' bits */
     154       61700 :                         result |= (S_IXUSR | S_IXGRP | S_IXOTH);
     155             : 
     156             :                         /* Apply directory mask */
     157       61700 :                         result &= lp_directory_mask(SNUM(conn));
     158             :                         /* Add in force bits */
     159       61700 :                         result |= lp_force_directory_mode(SNUM(conn));
     160             :                 }
     161             :         } else {
     162     1097352 :                 if ((dosmode & FILE_ATTRIBUTE_ARCHIVE) &&
     163      548676 :                     lp_map_archive(SNUM(conn))) {
     164      358206 :                         result |= S_IXUSR;
     165             :                 }
     166             : 
     167      549830 :                 if ((dosmode & FILE_ATTRIBUTE_SYSTEM) &&
     168        1154 :                     lp_map_system(SNUM(conn))) {
     169           0 :                         result |= S_IXGRP;
     170             :                 }
     171             : 
     172      549965 :                 if ((dosmode & FILE_ATTRIBUTE_HIDDEN) &&
     173        1289 :                     lp_map_hidden(SNUM(conn))) {
     174           0 :                         result |= S_IXOTH;
     175             :                 }
     176             : 
     177      548676 :                 if (dir_mode) {
     178             :                         /* Inherit 666 component of parent directory mode */
     179           0 :                         result |= dir_mode & (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
     180             :                 } else {
     181             :                         /* Apply mode mask */
     182      548676 :                         result &= lp_create_mask(SNUM(conn));
     183             :                         /* Add in force bits */
     184      548676 :                         result |= lp_force_create_mode(SNUM(conn));
     185             :                 }
     186             :         }
     187             : 
     188      610376 :         DBG_INFO("unix_mode(%s) returning 0%o\n",
     189             :                  smb_fname_str_dbg(smb_fname), (int)result);
     190             : 
     191      607292 :         return(result);
     192             : }
     193             : 
     194             : /****************************************************************************
     195             :  Change a unix mode to a dos mode.
     196             : ****************************************************************************/
     197             : 
     198         370 : static uint32_t dos_mode_from_sbuf(connection_struct *conn,
     199             :                                    const struct stat_ex *st,
     200             :                                    struct files_struct *fsp)
     201             : {
     202         370 :         int result = 0;
     203         370 :         enum mapreadonly_options ro_opts =
     204         370 :                 (enum mapreadonly_options)lp_map_readonly(SNUM(conn));
     205             : 
     206             : #if defined(UF_IMMUTABLE) && defined(SF_IMMUTABLE)
     207             :         /* if we can find out if a file is immutable we should report it r/o */
     208             :         if (st->st_ex_flags & (UF_IMMUTABLE | SF_IMMUTABLE)) {
     209             :                 result |= FILE_ATTRIBUTE_READONLY;
     210             :         }
     211             : #endif
     212         370 :         if (ro_opts == MAP_READONLY_YES) {
     213             :                 /* Original Samba method - map inverse of user "w" bit. */
     214           0 :                 if ((st->st_ex_mode & S_IWUSR) == 0) {
     215           0 :                         result |= FILE_ATTRIBUTE_READONLY;
     216             :                 }
     217         370 :         } else if (ro_opts == MAP_READONLY_PERMISSIONS) {
     218             :                 /* smb_fname->fsp can be NULL for an MS-DFS link. */
     219             :                 /* Check actual permissions for read-only. */
     220           0 :                 if ((fsp != NULL) && !can_write_to_fsp(fsp)) {
     221           0 :                         result |= FILE_ATTRIBUTE_READONLY;
     222             :                 }
     223             :         } /* Else never set the readonly bit. */
     224             : 
     225         370 :         if (MAP_ARCHIVE(conn) && ((st->st_ex_mode & S_IXUSR) != 0)) {
     226         370 :                 result |= FILE_ATTRIBUTE_ARCHIVE;
     227             :         }
     228             : 
     229         370 :         if (MAP_SYSTEM(conn) && ((st->st_ex_mode & S_IXGRP) != 0)) {
     230           0 :                 result |= FILE_ATTRIBUTE_SYSTEM;
     231             :         }
     232             : 
     233         370 :         if (MAP_HIDDEN(conn) && ((st->st_ex_mode & S_IXOTH) != 0)) {
     234           0 :                 result |= FILE_ATTRIBUTE_HIDDEN;
     235             :         }
     236             : 
     237         370 :         if (S_ISDIR(st->st_ex_mode)) {
     238         370 :                 result = FILE_ATTRIBUTE_DIRECTORY |
     239         370 :                          (result & FILE_ATTRIBUTE_READONLY);
     240             :         }
     241             : 
     242         370 :         dos_mode_debug_print(__func__, result);
     243             : 
     244         370 :         return result;
     245             : }
     246             : 
     247             : /****************************************************************************
     248             :  Get DOS attributes from an EA.
     249             :  This can also pull the create time into the stat struct inside smb_fname.
     250             : ****************************************************************************/
     251             : 
     252     1340902 : NTSTATUS parse_dos_attribute_blob(struct smb_filename *smb_fname,
     253             :                                   DATA_BLOB blob,
     254             :                                   uint32_t *pattr)
     255             : {
     256        2134 :         struct xattr_DOSATTRIB dosattrib;
     257        2134 :         enum ndr_err_code ndr_err;
     258        2134 :         uint32_t dosattr;
     259             : 
     260     1340902 :         ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &dosattrib,
     261             :                         (ndr_pull_flags_fn_t)ndr_pull_xattr_DOSATTRIB);
     262             : 
     263     1340902 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     264           0 :                 DBG_WARNING("bad ndr decode "
     265             :                             "from EA on file %s: Error = %s\n",
     266             :                             smb_fname_str_dbg(smb_fname),
     267             :                             ndr_errstr(ndr_err));
     268           0 :                 return ndr_map_error2ntstatus(ndr_err);
     269             :         }
     270             : 
     271     1340902 :         DBG_DEBUG("%s attr = %s\n",
     272             :                   smb_fname_str_dbg(smb_fname), dosattrib.attrib_hex);
     273             : 
     274     1340902 :         switch (dosattrib.version) {
     275           0 :         case 0xFFFF:
     276           0 :                 dosattr = dosattrib.info.compatinfoFFFF.attrib;
     277           0 :                 break;
     278           0 :         case 1:
     279           0 :                 dosattr = dosattrib.info.info1.attrib;
     280           0 :                 if (!null_nttime(dosattrib.info.info1.create_time)) {
     281           0 :                         struct timespec create_time =
     282           0 :                                 nt_time_to_unix_timespec(
     283             :                                         dosattrib.info.info1.create_time);
     284             : 
     285           0 :                         update_stat_ex_create_time(&smb_fname->st,
     286             :                                                    create_time);
     287             : 
     288           0 :                         DBG_DEBUG("file %s case 1 set btime %s",
     289             :                                   smb_fname_str_dbg(smb_fname),
     290             :                                   time_to_asc(convert_timespec_to_time_t(
     291             :                                                       create_time)));
     292             :                 }
     293           0 :                 break;
     294           0 :         case 2:
     295           0 :                 dosattr = dosattrib.info.oldinfo2.attrib;
     296             :                 /* Don't know what flags to check for this case. */
     297           0 :                 break;
     298           0 :         case 3:
     299           0 :                 dosattr = dosattrib.info.info3.attrib;
     300           0 :                 if ((dosattrib.info.info3.valid_flags & XATTR_DOSINFO_CREATE_TIME) &&
     301           0 :                     !null_nttime(dosattrib.info.info3.create_time)) {
     302           0 :                         struct timespec create_time =
     303           0 :                                 nt_time_to_full_timespec(
     304             :                                         dosattrib.info.info3.create_time);
     305             : 
     306           0 :                         update_stat_ex_create_time(&smb_fname->st,
     307             :                                                    create_time);
     308             : 
     309           0 :                         DBG_DEBUG("file %s case 3 set btime %s",
     310             :                                   smb_fname_str_dbg(smb_fname),
     311             :                                   time_to_asc(convert_timespec_to_time_t(
     312             :                                                       create_time)));
     313             :                 }
     314           0 :                 break;
     315     1340902 :         case 4:
     316             :         case 5:
     317             :         {
     318        2134 :                 uint32_t info_valid_flags;
     319        2134 :                 NTTIME info_create_time;
     320             : 
     321     1340902 :                 if (dosattrib.version == 4) {
     322           0 :                         info_valid_flags = dosattrib.info.info4.valid_flags;
     323           0 :                         info_create_time = dosattrib.info.info4.create_time;
     324           0 :                         dosattr = dosattrib.info.info4.attrib;
     325             :                 } else {
     326     1340902 :                         info_valid_flags = dosattrib.info.info5.valid_flags;
     327     1340902 :                         info_create_time = dosattrib.info.info5.create_time;
     328     1340902 :                         dosattr = dosattrib.info.info5.attrib;
     329             :                 }
     330             : 
     331     1343036 :                 if ((info_valid_flags & XATTR_DOSINFO_CREATE_TIME) &&
     332     1340902 :                     !null_nttime(info_create_time))
     333             :                 {
     334        2134 :                         struct timespec creat_time;
     335             : 
     336     1340902 :                         creat_time = nt_time_to_full_timespec(info_create_time);
     337     1340902 :                         update_stat_ex_create_time(&smb_fname->st, creat_time);
     338             : 
     339     1340902 :                         DBG_DEBUG("file [%s] creation time [%s]\n",
     340             :                                 smb_fname_str_dbg(smb_fname),
     341             :                                 nt_time_string(talloc_tos(), info_create_time));
     342             :                 }
     343             : 
     344     1338768 :                 break;
     345             :         }
     346           0 :         default:
     347           0 :                 DBG_WARNING("Badly formed DOSATTRIB on file %s - %s\n",
     348             :                             smb_fname_str_dbg(smb_fname), blob.data);
     349             :                 /* Should this be INTERNAL_ERROR? */
     350           0 :                 return NT_STATUS_INVALID_PARAMETER;
     351             :         }
     352             : 
     353     1340902 :         if (S_ISDIR(smb_fname->st.st_ex_mode)) {
     354       97606 :                 dosattr |= FILE_ATTRIBUTE_DIRECTORY;
     355             :         }
     356             : 
     357             :         /* FILE_ATTRIBUTE_SPARSE is valid on get but not on set. */
     358     1340902 :         *pattr |= (uint32_t)(dosattr & (SAMBA_ATTRIBUTES_MASK|FILE_ATTRIBUTE_SPARSE));
     359             : 
     360     1340902 :         dos_mode_debug_print(__func__, *pattr);
     361             : 
     362     1340902 :         return NT_STATUS_OK;
     363             : }
     364             : 
     365     1440227 : NTSTATUS fget_ea_dos_attribute(struct files_struct *fsp,
     366             :                               uint32_t *pattr)
     367             : {
     368        2249 :         DATA_BLOB blob;
     369        2249 :         ssize_t sizeret;
     370        2249 :         fstring attrstr;
     371        2249 :         NTSTATUS status;
     372             : 
     373     1440227 :         if (!lp_store_dos_attributes(SNUM(fsp->conn))) {
     374           0 :                 return NT_STATUS_NOT_IMPLEMENTED;
     375             :         }
     376             : 
     377             :         /* Don't reset pattr to zero as we may already have filename-based attributes we
     378             :            need to preserve. */
     379             : 
     380     1440227 :         sizeret = SMB_VFS_FGETXATTR(fsp,
     381             :                                     SAMBA_XATTR_DOS_ATTRIB,
     382             :                                     attrstr,
     383             :                                     sizeof(attrstr));
     384     1440227 :         if (sizeret == -1 && ( errno == EPERM || errno == EACCES )) {
     385             :                 /* we may also retrieve dos attribs for unreadable files, this
     386             :                    is why we'll retry as root. We don't use root in the first
     387             :                    run because in cases like NFS, root might have even less
     388             :                    rights than the real user
     389             :                 */
     390          14 :                 become_root();
     391          14 :                 sizeret = SMB_VFS_FGETXATTR(fsp,
     392             :                                             SAMBA_XATTR_DOS_ATTRIB,
     393             :                                             attrstr,
     394             :                                             sizeof(attrstr));
     395          14 :                 unbecome_root();
     396             :         }
     397     1440227 :         if (sizeret == -1) {
     398      119343 :                 DBG_INFO("Cannot get attribute "
     399             :                          "from EA on file %s: Error = %s\n",
     400             :                          fsp_str_dbg(fsp), strerror(errno));
     401      119343 :                 return map_nt_error_from_unix(errno);
     402             :         }
     403             : 
     404     1320884 :         blob.data = (uint8_t *)attrstr;
     405     1320884 :         blob.length = sizeret;
     406             : 
     407     1320884 :         status = parse_dos_attribute_blob(fsp->fsp_name, blob, pattr);
     408     1320884 :         if (!NT_STATUS_IS_OK(status)) {
     409           0 :                 return status;
     410             :         }
     411             : 
     412     1320884 :         return NT_STATUS_OK;
     413             : }
     414             : 
     415             : /****************************************************************************
     416             :  Set DOS attributes in an EA.
     417             :  Also sets the create time.
     418             : ****************************************************************************/
     419             : 
     420      175450 : NTSTATUS set_ea_dos_attribute(connection_struct *conn,
     421             :                               struct smb_filename *smb_fname,
     422             :                               uint32_t dosmode)
     423             : {
     424      175450 :         struct xattr_DOSATTRIB dosattrib = { .version = 0, };
     425         467 :         enum ndr_err_code ndr_err;
     426      175450 :         DATA_BLOB blob = { .data = NULL, };
     427         467 :         struct timespec btime;
     428         467 :         int ret;
     429             : 
     430      175450 :         if (!lp_store_dos_attributes(SNUM(conn))) {
     431           0 :                 return NT_STATUS_NOT_IMPLEMENTED;
     432             :         }
     433             : 
     434      175450 :         if (smb_fname->fsp == NULL) {
     435             :                 /* symlink */
     436           0 :                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
     437             :         }
     438             :         /*
     439             :          * Don't store FILE_ATTRIBUTE_OFFLINE, it's dealt with in
     440             :          * vfs_default via DMAPI if that is enabled.
     441             :          */
     442      175450 :         dosmode &= ~FILE_ATTRIBUTE_OFFLINE;
     443             : 
     444      175450 :         dosattrib.version = 5;
     445      175450 :         dosattrib.info.info5.valid_flags = XATTR_DOSINFO_ATTRIB |
     446             :                                         XATTR_DOSINFO_CREATE_TIME;
     447      175450 :         dosattrib.info.info5.attrib = dosmode;
     448      350900 :         dosattrib.info.info5.create_time = full_timespec_to_nt_time(
     449      175450 :                 &smb_fname->st.st_ex_btime);
     450             : 
     451      175450 :         DEBUG(10,("set_ea_dos_attributes: set attribute 0x%x, btime = %s on file %s\n",
     452             :                 (unsigned int)dosmode,
     453             :                 time_to_asc(convert_timespec_to_time_t(smb_fname->st.st_ex_btime)),
     454             :                 smb_fname_str_dbg(smb_fname) ));
     455             : 
     456      175450 :         ndr_err = ndr_push_struct_blob(
     457             :                         &blob, talloc_tos(), &dosattrib,
     458             :                         (ndr_push_flags_fn_t)ndr_push_xattr_DOSATTRIB);
     459             : 
     460      175450 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     461           0 :                 DEBUG(5, ("create_acl_blob: ndr_push_xattr_DOSATTRIB failed: %s\n",
     462             :                         ndr_errstr(ndr_err)));
     463           0 :                 return ndr_map_error2ntstatus(ndr_err);
     464             :         }
     465             : 
     466      175450 :         if (blob.data == NULL || blob.length == 0) {
     467             :                 /* Should this be INTERNAL_ERROR? */
     468           0 :                 return NT_STATUS_INVALID_PARAMETER;
     469             :         }
     470             : 
     471      175450 :         ret = SMB_VFS_FSETXATTR(smb_fname->fsp,
     472             :                                SAMBA_XATTR_DOS_ATTRIB,
     473             :                                blob.data, blob.length, 0);
     474      175450 :         if (ret != 0) {
     475           2 :                 NTSTATUS status = NT_STATUS_OK;
     476           2 :                 bool set_dosmode_ok = false;
     477             : 
     478           2 :                 if ((errno != EPERM) && (errno != EACCES)) {
     479           2 :                         DBG_INFO("Cannot set "
     480             :                                  "attribute EA on file %s: Error = %s\n",
     481             :                                  smb_fname_str_dbg(smb_fname), strerror(errno));
     482           2 :                         return map_nt_error_from_unix(errno);
     483             :                 }
     484             : 
     485             :                 /* We want DOS semantics, ie allow non owner with write permission to change the
     486             :                         bits on a file. Just like file_ntimes below.
     487             :                 */
     488             : 
     489             :                 /* Check if we have write access. */
     490           0 :                 if (!CAN_WRITE(conn)) {
     491           0 :                         return NT_STATUS_ACCESS_DENIED;
     492             :                 }
     493             : 
     494           0 :                 status = smbd_check_access_rights_fsp(conn->cwd_fsp,
     495             :                                         smb_fname->fsp,
     496             :                                         false,
     497             :                                         FILE_WRITE_ATTRIBUTES);
     498           0 :                 if (NT_STATUS_IS_OK(status)) {
     499           0 :                         set_dosmode_ok = true;
     500             :                 }
     501             : 
     502           0 :                 if (!set_dosmode_ok && lp_dos_filemode(SNUM(conn))) {
     503           0 :                         set_dosmode_ok = can_write_to_fsp(smb_fname->fsp);
     504             :                 }
     505             : 
     506           0 :                 if (!set_dosmode_ok) {
     507           0 :                         return NT_STATUS_ACCESS_DENIED;
     508             :                 }
     509             : 
     510           0 :                 become_root();
     511           0 :                 ret = SMB_VFS_FSETXATTR(smb_fname->fsp,
     512             :                                         SAMBA_XATTR_DOS_ATTRIB,
     513             :                                         blob.data, blob.length, 0);
     514           0 :                 if (ret == 0) {
     515           0 :                         status = NT_STATUS_OK;
     516             :                 }
     517           0 :                 unbecome_root();
     518           0 :                 if (!NT_STATUS_IS_OK(status)) {
     519           0 :                         return status;
     520             :                 }
     521             :         }
     522             : 
     523             :         /*
     524             :          * We correctly stored the create time.
     525             :          * We *always* set XATTR_DOSINFO_CREATE_TIME,
     526             :          * so now it can no longer be considered
     527             :          * calculated. Make sure to use the value rounded
     528             :          * to NTTIME granularity we've stored in the xattr.
     529             :          */
     530      175448 :         btime = nt_time_to_full_timespec(dosattrib.info.info5.create_time);
     531      175448 :         update_stat_ex_create_time(&smb_fname->st, btime);
     532             : 
     533      175448 :         DEBUG(10,("set_ea_dos_attribute: set EA 0x%x on file %s\n",
     534             :                 (unsigned int)dosmode,
     535             :                 smb_fname_str_dbg(smb_fname)));
     536      175448 :         return NT_STATUS_OK;
     537             : }
     538             : 
     539             : static uint32_t
     540     1217483 : dos_mode_from_name(connection_struct *conn, const char *name, uint32_t dosmode)
     541             : {
     542     1217483 :         const char *p = NULL;
     543     1217483 :         uint32_t result = dosmode;
     544             : 
     545     2432827 :         if (!(result & FILE_ATTRIBUTE_HIDDEN) &&
     546     1215344 :             lp_hide_dot_files(SNUM(conn)))
     547             :         {
     548     1215344 :                 p = strrchr_m(name, '/');
     549     1215344 :                 if (p) {
     550     1093493 :                         p++;
     551             :                 } else {
     552      121019 :                         p = name;
     553             :                 }
     554             : 
     555             :                 /* Only . and .. are not hidden. */
     556     1215344 :                 if ((p[0] == '.') && !(ISDOT(p) || ISDOTDOT(p))) {
     557         921 :                         result |= FILE_ATTRIBUTE_HIDDEN;
     558             :                 }
     559             :         }
     560             : 
     561     1217483 :         if (!(result & FILE_ATTRIBUTE_HIDDEN) && IS_HIDDEN_PATH(conn, name)) {
     562           8 :                 result |= FILE_ATTRIBUTE_HIDDEN;
     563             :         }
     564             : 
     565     1217483 :         return result;
     566             : }
     567             : 
     568             : /****************************************************************************
     569             :  Change a unix mode to a dos mode for an ms dfs link.
     570             : ****************************************************************************/
     571             : 
     572         370 : uint32_t dos_mode_msdfs(connection_struct *conn,
     573             :                         const char *name,
     574             :                         const struct stat_ex *st)
     575             : {
     576         370 :         uint32_t result = 0;
     577             : 
     578         370 :         DEBUG(8, ("dos_mode_msdfs: %s\n", name));
     579             : 
     580         370 :         if (!VALID_STAT(*st)) {
     581           0 :                 return 0;
     582             :         }
     583             : 
     584         370 :         result = dos_mode_from_name(conn, name, result);
     585         370 :         result |= dos_mode_from_sbuf(conn, st, NULL);
     586             : 
     587         370 :         if (result == 0) {
     588           0 :                 result = FILE_ATTRIBUTE_NORMAL;
     589             :         }
     590             : 
     591         370 :         result = filter_mode_by_protocol(result);
     592             : 
     593             :         /*
     594             :          * Add in that it is a reparse point
     595             :          */
     596         370 :         result |= FILE_ATTRIBUTE_REPARSE_POINT;
     597             : 
     598         370 :         dos_mode_debug_print(__func__, result);
     599             : 
     600         370 :         return(result);
     601             : }
     602             : 
     603             : /*
     604             :  * check whether a file or directory is flagged as compressed.
     605             :  */
     606           0 : static NTSTATUS dos_mode_check_compressed(struct files_struct *fsp,
     607             :                                           bool *is_compressed)
     608             : {
     609           0 :         NTSTATUS status;
     610           0 :         uint16_t compression_fmt;
     611             : 
     612           0 :         status = SMB_VFS_FGET_COMPRESSION(
     613             :                 fsp->conn, talloc_tos(), fsp, &compression_fmt);
     614           0 :         if (!NT_STATUS_IS_OK(status)) {
     615           0 :                 return status;
     616             :         }
     617             : 
     618           0 :         if (compression_fmt == COMPRESSION_FORMAT_LZNT1) {
     619           0 :                 *is_compressed = true;
     620             :         } else {
     621           0 :                 *is_compressed = false;
     622             :         }
     623           0 :         return NT_STATUS_OK;
     624             : }
     625             : 
     626     1217113 : static uint32_t dos_mode_post(uint32_t dosmode,
     627             :                               struct files_struct *fsp,
     628             :                               const char *func)
     629             : {
     630     1217113 :         struct smb_filename *smb_fname = NULL;
     631        1568 :         NTSTATUS status;
     632             : 
     633     1217113 :         if (fsp != NULL) {
     634     1217113 :                 smb_fname = fsp->fsp_name;
     635             :         }
     636     1217113 :         SMB_ASSERT(smb_fname != NULL);
     637             : 
     638             :         /*
     639             :          * According to MS-FSA a stream name does not have
     640             :          * separate DOS attribute metadata, so we must return
     641             :          * the DOS attribute from the base filename. With one caveat,
     642             :          * a non-default stream name can never be a directory.
     643             :          *
     644             :          * As this is common to all streams data stores, we handle
     645             :          * it here instead of inside all stream VFS modules.
     646             :          *
     647             :          * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13380
     648             :          */
     649             : 
     650     1217113 :         if (is_named_stream(smb_fname)) {
     651             :                 /* is_ntfs_stream_smb_fname() returns false for a POSIX path. */
     652        4181 :                 dosmode &= ~(FILE_ATTRIBUTE_DIRECTORY);
     653             :         }
     654             : 
     655     1217113 :         if (fsp->conn->fs_capabilities & FILE_FILE_COMPRESSION) {
     656           0 :                 bool compressed = false;
     657             : 
     658           0 :                 status = dos_mode_check_compressed(fsp, &compressed);
     659           0 :                 if (NT_STATUS_IS_OK(status) && compressed) {
     660           0 :                         dosmode |= FILE_ATTRIBUTE_COMPRESSED;
     661             :                 }
     662             :         }
     663             : 
     664     1217113 :         dosmode |= dos_mode_from_name(fsp->conn, smb_fname->base_name, dosmode);
     665             : 
     666     1217113 :         if (S_ISDIR(smb_fname->st.st_ex_mode)) {
     667      143249 :                 dosmode |= FILE_ATTRIBUTE_DIRECTORY;
     668     1073864 :         } else if (dosmode == 0) {
     669       32552 :                 dosmode = FILE_ATTRIBUTE_NORMAL;
     670             :         }
     671             : 
     672     1217113 :         dosmode = filter_mode_by_protocol(dosmode);
     673             : 
     674     1217113 :         dos_mode_debug_print(func, dosmode);
     675     1217113 :         return dosmode;
     676             : }
     677             : 
     678             : /****************************************************************************
     679             :  Change a unix mode to a dos mode.
     680             :  May also read the create timespec into the stat struct in smb_fname
     681             :  if "store dos attributes" is true.
     682             : ****************************************************************************/
     683             : 
     684     1365281 : uint32_t fdos_mode(struct files_struct *fsp)
     685             : {
     686     1365281 :         uint32_t result = 0;
     687     1365281 :         NTSTATUS status = NT_STATUS_OK;
     688             : 
     689     1365281 :         if (fsp == NULL) {
     690             :                 /*
     691             :                  * The pathological case where a caller does
     692             :                  * fdos_mode(smb_fname->fsp) passing a pathref fsp. But as
     693             :                  * smb_fname points at a symlink in POSIX context smb_fname->fsp
     694             :                  * is NULL.
     695             :                  */
     696           8 :                 return FILE_ATTRIBUTE_NORMAL;
     697             :         }
     698             : 
     699     1365273 :         DBG_DEBUG("%s\n", fsp_str_dbg(fsp));
     700             : 
     701     1365273 :         if (fsp->fake_file_handle != NULL) {
     702          21 :                 return dosmode_from_fake_filehandle(fsp->fake_file_handle);
     703             :         }
     704             : 
     705     1365252 :         if (!VALID_STAT(fsp->fsp_name->st)) {
     706           0 :                 return 0;
     707             :         }
     708             : 
     709     1365252 :         if (S_ISLNK(fsp->fsp_name->st.st_ex_mode)) {
     710         349 :                 return FILE_ATTRIBUTE_NORMAL;
     711             :         }
     712             : 
     713     1364903 :         if (fsp->fsp_name->st.cached_dos_attributes != FILE_ATTRIBUTE_INVALID) {
     714      167565 :                 return fsp->fsp_name->st.cached_dos_attributes;
     715             :         }
     716             : 
     717             :         /* Get the DOS attributes via the VFS if we can */
     718     1196977 :         status = vfs_fget_dos_attributes(fsp, &result);
     719     1196977 :         if (!NT_STATUS_IS_OK(status)) {
     720             :                 /*
     721             :                  * Only fall back to using UNIX modes if we get NOT_IMPLEMENTED.
     722             :                  */
     723       91570 :                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
     724           0 :                         result |= dos_mode_from_sbuf(fsp->conn,
     725           0 :                                                      &fsp->fsp_name->st,
     726             :                                                      fsp);
     727             :                 }
     728             :         }
     729             : 
     730     1196977 :         fsp->fsp_name->st.cached_dos_attributes = dos_mode_post(result, fsp, __func__);
     731     1196977 :         return fsp->fsp_name->st.cached_dos_attributes;
     732             : }
     733             : 
     734             : struct dos_mode_at_state {
     735             :         files_struct *dir_fsp;
     736             :         struct smb_filename *smb_fname;
     737             :         uint32_t dosmode;
     738             : };
     739             : 
     740             : static void dos_mode_at_vfs_get_dosmode_done(struct tevent_req *subreq);
     741             : 
     742       20136 : struct tevent_req *dos_mode_at_send(TALLOC_CTX *mem_ctx,
     743             :                                     struct tevent_context *ev,
     744             :                                     files_struct *dir_fsp,
     745             :                                     struct smb_filename *smb_fname)
     746             : {
     747       20136 :         struct tevent_req *req = NULL;
     748       20136 :         struct dos_mode_at_state *state = NULL;
     749       20136 :         struct tevent_req *subreq = NULL;
     750             : 
     751       20136 :         DBG_DEBUG("%s\n", smb_fname_str_dbg(smb_fname));
     752             : 
     753       20136 :         req = tevent_req_create(mem_ctx, &state,
     754             :                                 struct dos_mode_at_state);
     755       20136 :         if (req == NULL) {
     756           0 :                 return NULL;
     757             :         }
     758             : 
     759       20136 :         *state = (struct dos_mode_at_state) {
     760             :                 .dir_fsp = dir_fsp,
     761             :                 .smb_fname = smb_fname,
     762             :         };
     763             : 
     764       20136 :         if (!VALID_STAT(smb_fname->st)) {
     765           0 :                 tevent_req_done(req);
     766           0 :                 return tevent_req_post(req, ev);
     767             :         }
     768             : 
     769       20136 :         if (smb_fname->fsp == NULL) {
     770           0 :                 if (ISDOTDOT(smb_fname->base_name)) {
     771             :                         /*
     772             :                          * smb_fname->fsp is explicitly closed
     773             :                          * for ".." to prevent meta-data leakage.
     774             :                          */
     775           0 :                         state->dosmode = FILE_ATTRIBUTE_DIRECTORY;
     776             :                 } else {
     777             :                         /*
     778             :                          * This is a symlink in POSIX context.
     779             :                          * FIXME ? Should we move to returning
     780             :                          * FILE_ATTRIBUTE_REPARSE_POINT here ?
     781             :                          */
     782           0 :                         state->dosmode = FILE_ATTRIBUTE_NORMAL;
     783             :                 }
     784           0 :                 tevent_req_done(req);
     785           0 :                 return tevent_req_post(req, ev);
     786             :         }
     787             : 
     788       20136 :         subreq = SMB_VFS_GET_DOS_ATTRIBUTES_SEND(state,
     789             :                                                  ev,
     790             :                                                  dir_fsp,
     791             :                                                  smb_fname);
     792       20136 :         if (tevent_req_nomem(subreq, req)) {
     793           0 :                 return tevent_req_post(req, ev);
     794             :         }
     795       20136 :         tevent_req_set_callback(subreq, dos_mode_at_vfs_get_dosmode_done, req);
     796             : 
     797       20136 :         return req;
     798             : }
     799             : 
     800       20136 : static void dos_mode_at_vfs_get_dosmode_done(struct tevent_req *subreq)
     801             : {
     802           0 :         struct tevent_req *req =
     803       20136 :                 tevent_req_callback_data(subreq,
     804             :                 struct tevent_req);
     805           0 :         struct dos_mode_at_state *state =
     806       20136 :                 tevent_req_data(req,
     807             :                 struct dos_mode_at_state);
     808           0 :         struct vfs_aio_state aio_state;
     809           0 :         NTSTATUS status;
     810           0 :         bool ok;
     811             : 
     812             :         /*
     813             :          * Make sure we run as the user again
     814             :          */
     815       20136 :         ok = change_to_user_and_service_by_fsp(state->dir_fsp);
     816       20136 :         SMB_ASSERT(ok);
     817             : 
     818       20136 :         status = SMB_VFS_GET_DOS_ATTRIBUTES_RECV(subreq,
     819             :                                                  &aio_state,
     820             :                                                  &state->dosmode);
     821       20136 :         TALLOC_FREE(subreq);
     822       20136 :         if (!NT_STATUS_IS_OK(status)) {
     823             :                 /*
     824             :                  * Both the sync dos_mode() as well as the async
     825             :                  * dos_mode_at_[send|recv] have no real error return, the only
     826             :                  * unhandled error is when the stat info in smb_fname is not
     827             :                  * valid (cf the checks in dos_mode() and dos_mode_at_send().
     828             :                  *
     829             :                  * If SMB_VFS_GET_DOS_ATTRIBUTES[_SEND|_RECV] fails we must call
     830             :                  * dos_mode_post() which also does the mapping of a last resort
     831             :                  * from S_IFMT(st_mode).
     832             :                  *
     833             :                  * Only if we get NT_STATUS_NOT_IMPLEMENTED or
     834             :                  * NT_STATUS_NOT_SUPPORTED from a stacked VFS module we must
     835             :                  * fallback to sync processing.
     836             :                  */
     837         118 :                 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED) &&
     838         118 :                     !NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED))
     839             :                 {
     840             :                         /*
     841             :                          * state->dosmode should still be 0, but reset
     842             :                          * it to be sure.
     843             :                          */
     844         118 :                         state->dosmode = 0;
     845         118 :                         status = NT_STATUS_OK;
     846             :                 }
     847             :         }
     848       20136 :         if (NT_STATUS_IS_OK(status)) {
     849       40272 :                 state->dosmode = dos_mode_post(state->dosmode,
     850       20136 :                                                state->smb_fname->fsp,
     851             :                                                __func__);
     852       20136 :                 tevent_req_done(req);
     853       20136 :                 return;
     854             :         }
     855             : 
     856             :         /*
     857             :          * Fall back to sync dos_mode() if we got NOT_IMPLEMENTED.
     858             :          */
     859             : 
     860           0 :         state->dosmode = fdos_mode(state->smb_fname->fsp);
     861           0 :         tevent_req_done(req);
     862           0 :         return;
     863             : }
     864             : 
     865       20136 : NTSTATUS dos_mode_at_recv(struct tevent_req *req, uint32_t *dosmode)
     866             : {
     867           0 :         struct dos_mode_at_state *state =
     868       20136 :                 tevent_req_data(req,
     869             :                 struct dos_mode_at_state);
     870           0 :         NTSTATUS status;
     871             : 
     872       20136 :         if (tevent_req_is_nterror(req, &status)) {
     873           0 :                 tevent_req_received(req);
     874           0 :                 return status;
     875             :         }
     876             : 
     877       20136 :         *dosmode = state->dosmode;
     878       20136 :         tevent_req_received(req);
     879       20136 :         return NT_STATUS_OK;
     880             : }
     881             : 
     882             : /*******************************************************************
     883             :  chmod a file - but preserve some bits.
     884             :  If "store dos attributes" is also set it will store the create time
     885             :  from the stat struct in smb_fname (in NTTIME format) in the EA
     886             :  attribute also.
     887             : ********************************************************************/
     888             : 
     889      175851 : int file_set_dosmode(connection_struct *conn,
     890             :                      struct smb_filename *smb_fname,
     891             :                      uint32_t dosmode,
     892             :                      struct smb_filename *parent_dir,
     893             :                      bool newfile)
     894             : {
     895      175851 :         int mask=0;
     896         463 :         mode_t tmp;
     897         463 :         mode_t unixmode;
     898      175851 :         int ret = -1;
     899         463 :         NTSTATUS status;
     900             : 
     901      175851 :         if (!CAN_WRITE(conn)) {
     902           0 :                 errno = EROFS;
     903           0 :                 return -1;
     904             :         }
     905             : 
     906      175851 :         if (S_ISLNK(smb_fname->st.st_ex_mode)) {
     907             :                 /* A symlink in POSIX context, ignore */
     908          16 :                 return 0;
     909             :         }
     910             : 
     911      175835 :         if ((S_ISDIR(smb_fname->st.st_ex_mode)) &&
     912       11089 :             (dosmode & FILE_ATTRIBUTE_TEMPORARY))
     913             :         {
     914         567 :                 errno = EINVAL;
     915         567 :                 return -1;
     916             :         }
     917             : 
     918      175268 :         dosmode &= SAMBA_ATTRIBUTES_MASK;
     919             : 
     920      175268 :         DEBUG(10,("file_set_dosmode: setting dos mode 0x%x on file %s\n",
     921             :                   dosmode, smb_fname_str_dbg(smb_fname)));
     922             : 
     923      175268 :         if (smb_fname->fsp == NULL) {
     924          28 :                 errno = ENOENT;
     925          28 :                 return -1;
     926             :         }
     927             : 
     928      175240 :         if (smb_fname->fsp->posix_flags & FSP_POSIX_FLAGS_OPEN &&
     929         663 :             !lp_store_dos_attributes(SNUM(conn)))
     930             :         {
     931           0 :                 return 0;
     932             :         }
     933             : 
     934      175240 :         unixmode = smb_fname->st.st_ex_mode;
     935             : 
     936      175240 :         get_acl_group_bits(conn, smb_fname->fsp, &smb_fname->st.st_ex_mode);
     937             : 
     938      175240 :         if (S_ISDIR(smb_fname->st.st_ex_mode))
     939       10590 :                 dosmode |= FILE_ATTRIBUTE_DIRECTORY;
     940             :         else
     941      164650 :                 dosmode &= ~FILE_ATTRIBUTE_DIRECTORY;
     942             : 
     943             :         /* Store the DOS attributes in an EA by preference. */
     944      175240 :         status = SMB_VFS_FSET_DOS_ATTRIBUTES(conn,
     945             :                                              metadata_fsp(smb_fname->fsp),
     946             :                                              dosmode);
     947      175240 :         if (NT_STATUS_IS_OK(status)) {
     948      175238 :                 smb_fname->st.cached_dos_attributes = dosmode;
     949      175238 :                 ret = 0;
     950      175238 :                 goto done;
     951             :         }
     952             : 
     953             :         /*
     954             :          * Only fall back to using UNIX modes if
     955             :          * we get NOT_IMPLEMENTED.
     956             :          */
     957           2 :         if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
     958           2 :                 errno = map_errno_from_nt_status(status);
     959           2 :                 return -1;
     960             :         }
     961             : 
     962             :         /* Fall back to UNIX modes. */
     963           0 :         unixmode = unix_mode(
     964             :                 conn,
     965             :                 dosmode,
     966             :                 smb_fname,
     967             :                 parent_dir != NULL ? parent_dir->fsp : NULL);
     968             : 
     969             :         /* preserve the file type bits */
     970           0 :         mask |= S_IFMT;
     971             : 
     972             :         /* preserve the s bits */
     973           0 :         mask |= (S_ISUID | S_ISGID);
     974             : 
     975             :         /* preserve the t bit */
     976             : #ifdef S_ISVTX
     977           0 :         mask |= S_ISVTX;
     978             : #endif
     979             : 
     980             :         /* possibly preserve the x bits */
     981           0 :         if (!MAP_ARCHIVE(conn))
     982           0 :                 mask |= S_IXUSR;
     983           0 :         if (!MAP_SYSTEM(conn))
     984           0 :                 mask |= S_IXGRP;
     985           0 :         if (!MAP_HIDDEN(conn))
     986           0 :                 mask |= S_IXOTH;
     987             : 
     988           0 :         unixmode |= (smb_fname->st.st_ex_mode & mask);
     989             : 
     990             :         /* if we previously had any r bits set then leave them alone */
     991           0 :         if ((tmp = smb_fname->st.st_ex_mode & (S_IRUSR|S_IRGRP|S_IROTH))) {
     992           0 :                 unixmode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
     993           0 :                 unixmode |= tmp;
     994             :         }
     995             : 
     996             :         /* if we previously had any w bits set then leave them alone
     997             :                 whilst adding in the new w bits, if the new mode is not rdonly */
     998           0 :         if (!(dosmode & FILE_ATTRIBUTE_READONLY)) {
     999           0 :                 unixmode |= (smb_fname->st.st_ex_mode & (S_IWUSR|S_IWGRP|S_IWOTH));
    1000             :         }
    1001             : 
    1002             :         /*
    1003             :          * From the chmod 2 man page:
    1004             :          *
    1005             :          * "If the calling process is not privileged, and the group of the file
    1006             :          * does not match the effective group ID of the process or one of its
    1007             :          * supplementary group IDs, the S_ISGID bit will be turned off, but
    1008             :          * this will not cause an error to be returned."
    1009             :          *
    1010             :          * Simply refuse to do the chmod in this case.
    1011             :          */
    1012             : 
    1013           0 :         if (S_ISDIR(smb_fname->st.st_ex_mode) &&
    1014           0 :             (unixmode & S_ISGID) &&
    1015           0 :             geteuid() != sec_initial_uid() &&
    1016           0 :             !current_user_in_group(conn, smb_fname->st.st_ex_gid))
    1017             :         {
    1018           0 :                 DEBUG(3,("file_set_dosmode: setgid bit cannot be "
    1019             :                         "set for directory %s\n",
    1020             :                         smb_fname_str_dbg(smb_fname)));
    1021           0 :                 errno = EPERM;
    1022           0 :                 return -1;
    1023             :         }
    1024             : 
    1025           0 :         ret = SMB_VFS_FCHMOD(smb_fname->fsp, unixmode);
    1026           0 :         if (ret == 0) {
    1027           0 :                 goto done;
    1028             :         }
    1029             : 
    1030           0 :         if((errno != EPERM) && (errno != EACCES))
    1031           0 :                 return -1;
    1032             : 
    1033           0 :         if(!lp_dos_filemode(SNUM(conn)))
    1034           0 :                 return -1;
    1035             : 
    1036             :         /* We want DOS semantics, ie allow non owner with write permission to change the
    1037             :                 bits on a file. Just like file_ntimes below.
    1038             :         */
    1039             : 
    1040           0 :         if (!can_write_to_fsp(smb_fname->fsp))
    1041             :         {
    1042           0 :                 errno = EACCES;
    1043           0 :                 return -1;
    1044             :         }
    1045             : 
    1046           0 :         become_root();
    1047           0 :         ret = SMB_VFS_FCHMOD(smb_fname->fsp, unixmode);
    1048           0 :         unbecome_root();
    1049             : 
    1050      175238 : done:
    1051      175238 :         if (!newfile) {
    1052        2583 :                 notify_fname(conn, NOTIFY_ACTION_MODIFIED,
    1053             :                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
    1054        2583 :                              smb_fname->base_name);
    1055             :         }
    1056      175238 :         if (ret == 0) {
    1057      175238 :                 smb_fname->st.st_ex_mode = unixmode;
    1058             :         }
    1059             : 
    1060      174775 :         return( ret );
    1061             : }
    1062             : 
    1063             : 
    1064         242 : NTSTATUS file_set_sparse(connection_struct *conn,
    1065             :                          files_struct *fsp,
    1066             :                          bool sparse)
    1067             : {
    1068           4 :         const struct loadparm_substitution *lp_sub =
    1069         242 :                 loadparm_s3_global_substitution();
    1070           4 :         uint32_t old_dosmode;
    1071           4 :         uint32_t new_dosmode;
    1072           4 :         NTSTATUS status;
    1073             : 
    1074         242 :         if (!CAN_WRITE(conn)) {
    1075           0 :                 DEBUG(9,("file_set_sparse: fname[%s] set[%u] "
    1076             :                         "on readonly share[%s]\n",
    1077             :                         smb_fname_str_dbg(fsp->fsp_name),
    1078             :                         sparse,
    1079             :                         lp_servicename(talloc_tos(), lp_sub, SNUM(conn))));
    1080           0 :                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
    1081             :         }
    1082             : 
    1083             :         /*
    1084             :          * Windows Server 2008 & 2012 permit FSCTL_SET_SPARSE if any of the
    1085             :          * following access flags are granted.
    1086             :          */
    1087         242 :         if ((fsp->access_mask & (FILE_WRITE_DATA
    1088             :                                 | FILE_WRITE_ATTRIBUTES
    1089             :                                 | SEC_FILE_APPEND_DATA)) == 0) {
    1090           8 :                 DEBUG(9,("file_set_sparse: fname[%s] set[%u] "
    1091             :                         "access_mask[0x%08X] - access denied\n",
    1092             :                         smb_fname_str_dbg(fsp->fsp_name),
    1093             :                         sparse,
    1094             :                         fsp->access_mask));
    1095           8 :                 return NT_STATUS_ACCESS_DENIED;
    1096             :         }
    1097             : 
    1098         234 :         if (fsp->fsp_flags.is_directory) {
    1099           8 :                 DEBUG(9, ("invalid attempt to %s sparse flag on dir %s\n",
    1100             :                           (sparse ? "set" : "clear"),
    1101             :                           smb_fname_str_dbg(fsp->fsp_name)));
    1102           8 :                 return NT_STATUS_INVALID_PARAMETER;
    1103             :         }
    1104             : 
    1105         226 :         if (IS_IPC(conn) || IS_PRINT(conn)) {
    1106           0 :                 DEBUG(9, ("attempt to %s sparse flag over invalid conn\n",
    1107             :                           (sparse ? "set" : "clear")));
    1108           0 :                 return NT_STATUS_INVALID_PARAMETER;
    1109             :         }
    1110             : 
    1111         226 :         if (fsp_is_alternate_stream(fsp)) {
    1112             :                 /*
    1113             :                  * MS-FSA 2.1.1.5 IsSparse
    1114             :                  *
    1115             :                  * This is a per stream attribute, but our backends don't
    1116             :                  * support it a consistent way, therefore just pretend
    1117             :                  * success and ignore the request.
    1118             :                  */
    1119           0 :                 DBG_DEBUG("Ignoring request to set FILE_ATTRIBUTE_SPARSE on "
    1120             :                           "[%s]\n", fsp_str_dbg(fsp));
    1121           0 :                 return NT_STATUS_OK;
    1122             :         }
    1123             : 
    1124         226 :         DEBUG(10,("file_set_sparse: setting sparse bit %u on file %s\n",
    1125             :                   sparse, smb_fname_str_dbg(fsp->fsp_name)));
    1126             : 
    1127         226 :         if (!lp_store_dos_attributes(SNUM(conn))) {
    1128           0 :                 return NT_STATUS_INVALID_DEVICE_REQUEST;
    1129             :         }
    1130             : 
    1131         226 :         status = vfs_stat_fsp(fsp);
    1132         226 :         if (!NT_STATUS_IS_OK(status)) {
    1133           0 :                 return status;
    1134             :         }
    1135             : 
    1136         226 :         old_dosmode = fdos_mode(fsp);
    1137             : 
    1138         226 :         if (sparse && !(old_dosmode & FILE_ATTRIBUTE_SPARSE)) {
    1139         170 :                 new_dosmode = old_dosmode | FILE_ATTRIBUTE_SPARSE;
    1140          56 :         } else if (!sparse && (old_dosmode & FILE_ATTRIBUTE_SPARSE)) {
    1141          40 :                 new_dosmode = old_dosmode & ~FILE_ATTRIBUTE_SPARSE;
    1142             :         } else {
    1143          16 :                 return NT_STATUS_OK;
    1144             :         }
    1145             : 
    1146             :         /* Store the DOS attributes in an EA. */
    1147         210 :         status = SMB_VFS_FSET_DOS_ATTRIBUTES(conn, fsp, new_dosmode);
    1148         210 :         if (!NT_STATUS_IS_OK(status)) {
    1149           0 :                 return status;
    1150             :         }
    1151             : 
    1152         210 :         notify_fname(conn, NOTIFY_ACTION_MODIFIED,
    1153             :                      FILE_NOTIFY_CHANGE_ATTRIBUTES,
    1154         210 :                      fsp->fsp_name->base_name);
    1155             : 
    1156         210 :         fsp->fsp_name->st.cached_dos_attributes = new_dosmode;
    1157         210 :         fsp->fsp_flags.is_sparse = sparse;
    1158             : 
    1159         210 :         return NT_STATUS_OK;
    1160             : }
    1161             : 
    1162             : /*******************************************************************
    1163             :  Wrapper around the VFS ntimes that possibly allows DOS semantics rather
    1164             :  than POSIX.
    1165             : *******************************************************************/
    1166             : 
    1167       11066 : int file_ntimes(connection_struct *conn,
    1168             :                 files_struct *fsp,
    1169             :                 struct smb_file_time *ft)
    1170             : {
    1171       11066 :         int ret = -1;
    1172             : 
    1173       11066 :         errno = 0;
    1174             : 
    1175       11066 :         DBG_INFO("actime: %s",
    1176             :                  time_to_asc(convert_timespec_to_time_t(ft->atime)));
    1177       11066 :         DBG_INFO("modtime: %s",
    1178             :                  time_to_asc(convert_timespec_to_time_t(ft->mtime)));
    1179       11066 :         DBG_INFO("ctime: %s",
    1180             :                  time_to_asc(convert_timespec_to_time_t(ft->ctime)));
    1181       11066 :         DBG_INFO("createtime: %s",
    1182             :                  time_to_asc(convert_timespec_to_time_t(ft->create_time)));
    1183             : 
    1184             :         /* Don't update the time on read-only shares */
    1185             :         /* We need this as set_filetime (which can be called on
    1186             :            close and other paths) can end up calling this function
    1187             :            without the NEED_WRITE protection. Found by :
    1188             :            Leo Weppelman <leo@wau.mis.ah.nl>
    1189             :         */
    1190             : 
    1191       11066 :         if (!CAN_WRITE(conn)) {
    1192           0 :                 return 0;
    1193             :         }
    1194             : 
    1195       11066 :         if (SMB_VFS_FNTIMES(fsp, ft) == 0) {
    1196       10949 :                 return 0;
    1197             :         }
    1198             : 
    1199           2 :         if((errno != EPERM) && (errno != EACCES)) {
    1200           2 :                 return -1;
    1201             :         }
    1202             : 
    1203           0 :         if(!lp_dos_filetimes(SNUM(conn))) {
    1204           0 :                 return -1;
    1205             :         }
    1206             : 
    1207             :         /* We have permission (given by the Samba admin) to
    1208             :            break POSIX semantics and allow a user to change
    1209             :            the time on a file they don't own but can write to
    1210             :            (as DOS does).
    1211             :          */
    1212             : 
    1213             :         /* Check if we have write access. */
    1214           0 :         if (can_write_to_fsp(fsp)) {
    1215             :                 /* We are allowed to become root and change the filetime. */
    1216           0 :                 become_root();
    1217           0 :                 ret = SMB_VFS_FNTIMES(fsp, ft);
    1218           0 :                 unbecome_root();
    1219             :         }
    1220             : 
    1221           0 :         return ret;
    1222             : }
    1223             : 
    1224             : /******************************************************************
    1225             :  Force a "sticky" write time on a pathname. This will always be
    1226             :  returned on all future write time queries and set on close.
    1227             : ******************************************************************/
    1228             : 
    1229        1221 : bool set_sticky_write_time_path(struct file_id fileid, struct timespec mtime)
    1230             : {
    1231        1221 :         if (is_omit_timespec(&mtime)) {
    1232           0 :                 return true;
    1233             :         }
    1234             : 
    1235        1221 :         if (!set_sticky_write_time(fileid, mtime)) {
    1236         165 :                 return false;
    1237             :         }
    1238             : 
    1239        1031 :         return true;
    1240             : }
    1241             : 
    1242             : /******************************************************************
    1243             :  Force a "sticky" write time on an fsp. This will always be
    1244             :  returned on all future write time queries and set on close.
    1245             : ******************************************************************/
    1246             : 
    1247        4345 : bool set_sticky_write_time_fsp(struct files_struct *fsp, struct timespec mtime)
    1248             : {
    1249        4345 :         if (is_omit_timespec(&mtime)) {
    1250        3085 :                 return true;
    1251             :         }
    1252             : 
    1253        1221 :         fsp->fsp_flags.write_time_forced = true;
    1254        1221 :         TALLOC_FREE(fsp->update_write_time_event);
    1255             : 
    1256        1221 :         return set_sticky_write_time_path(fsp->file_id, mtime);
    1257             : }
    1258             : 
    1259             : /******************************************************************
    1260             :  Set a create time EA.
    1261             : ******************************************************************/
    1262             : 
    1263         852 : NTSTATUS set_create_timespec_ea(struct files_struct *fsp,
    1264             :                                 struct timespec create_time)
    1265             : {
    1266          21 :         uint32_t dosmode;
    1267          21 :         int ret;
    1268             : 
    1269         852 :         if (!lp_store_dos_attributes(SNUM(fsp->conn))) {
    1270           0 :                 return NT_STATUS_OK;
    1271             :         }
    1272             : 
    1273         852 :         dosmode = fdos_mode(fsp);
    1274             : 
    1275         852 :         fsp->fsp_name->st.st_ex_btime = create_time;
    1276         852 :         ret = file_set_dosmode(fsp->conn, fsp->fsp_name, dosmode, NULL, false);
    1277         852 :         if (ret == -1) {
    1278           0 :                 return map_nt_error_from_unix(errno);
    1279             :         }
    1280             : 
    1281         852 :         DBG_DEBUG("wrote create time EA for file %s\n",
    1282             :                 smb_fname_str_dbg(fsp->fsp_name));
    1283             : 
    1284         852 :         return NT_STATUS_OK;
    1285             : }
    1286             : 
    1287             : /******************************************************************
    1288             :  Return a create time.
    1289             : ******************************************************************/
    1290             : 
    1291     1321033 : struct timespec get_create_timespec(connection_struct *conn,
    1292             :                                 struct files_struct *fsp,
    1293             :                                 const struct smb_filename *smb_fname)
    1294             : {
    1295     1321033 :         return smb_fname->st.st_ex_btime;
    1296             : }
    1297             : 
    1298             : /******************************************************************
    1299             :  Return a change time (may look at EA in future).
    1300             : ******************************************************************/
    1301             : 
    1302     1300887 : struct timespec get_change_timespec(connection_struct *conn,
    1303             :                                 struct files_struct *fsp,
    1304             :                                 const struct smb_filename *smb_fname)
    1305             : {
    1306     1300887 :         return smb_fname->st.st_ex_mtime;
    1307             : }

Generated by: LCOV version 1.14