2 Unix SMB/CIFS implementation.
3 dos mode handling functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) James Peach 2006
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.
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.
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/>.
22 #include "librpc/gen_ndr/ndr_xattr.h"
24 static uint32_t filter_mode_by_protocol(uint32_t mode
)
26 if (get_Protocol() <= PROTOCOL_LANMAN2
) {
27 DEBUG(10,("filter_mode_by_protocol: "
28 "filtering result 0x%x to 0x%x\n",
30 (unsigned int)(mode
& 0x3f) ));
36 static int set_link_read_only_flag(const SMB_STRUCT_STAT
*const sbuf
)
40 if (S_ISLNK(sbuf
->st_mode
) && S_ISDIR(sbuf
->st_mode
))
47 /****************************************************************************
48 Change a dos mode to a unix mode.
49 Base permission for files:
50 if creating file and inheriting (i.e. parent_dir != NULL)
51 apply read/write bits from parent directory.
53 everybody gets read bit set
54 dos readonly is represented in unix by removing everyone's write bit
55 dos archive is represented in unix by the user's execute bit
56 dos system is represented in unix by the group's execute bit
57 dos hidden is represented in unix by the other's execute bit
59 Then apply create mask,
62 Base permission for directories:
63 dos directory is represented in unix by unix's dir bit and the exec bit
65 Then apply create mask,
68 ****************************************************************************/
70 mode_t
unix_mode(connection_struct
*conn
, int dosmode
,
71 const struct smb_filename
*smb_fname
,
72 const char *inherit_from_dir
)
74 mode_t result
= (S_IRUSR
| S_IRGRP
| S_IROTH
| S_IWUSR
| S_IWGRP
| S_IWOTH
);
75 mode_t dir_mode
= 0; /* Mode of the inherit_from directory if
78 if (!lp_store_dos_attributes(SNUM(conn
)) && IS_DOS_READONLY(dosmode
)) {
79 result
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
82 if ((inherit_from_dir
!= NULL
) && lp_inherit_perms(SNUM(conn
))) {
83 struct smb_filename
*smb_fname_parent
= NULL
;
86 DEBUG(2, ("unix_mode(%s) inheriting from %s\n",
87 smb_fname_str_dbg(smb_fname
),
90 status
= create_synthetic_smb_fname(talloc_tos(),
91 inherit_from_dir
, NULL
,
92 NULL
, &smb_fname_parent
);
93 if (!NT_STATUS_IS_OK(status
)) {
94 DEBUG(1,("unix_mode(%s) failed, [dir %s]: %s\n",
95 smb_fname_str_dbg(smb_fname
),
96 inherit_from_dir
, nt_errstr(status
)));
100 if (SMB_VFS_STAT(conn
, smb_fname_parent
) != 0) {
101 DEBUG(4,("unix_mode(%s) failed, [dir %s]: %s\n",
102 smb_fname_str_dbg(smb_fname
),
103 inherit_from_dir
, strerror(errno
)));
104 TALLOC_FREE(smb_fname_parent
);
105 return(0); /* *** shouldn't happen! *** */
108 /* Save for later - but explicitly remove setuid bit for safety. */
109 dir_mode
= smb_fname_parent
->st
.st_ex_mode
& ~S_ISUID
;
110 DEBUG(2,("unix_mode(%s) inherit mode %o\n",
111 smb_fname_str_dbg(smb_fname
), (int)dir_mode
));
114 TALLOC_FREE(smb_fname_parent
);
117 if (IS_DOS_DIR(dosmode
)) {
118 /* We never make directories read only for the owner as under DOS a user
119 can always create a file in a read-only directory. */
120 result
|= (S_IFDIR
| S_IWUSR
);
123 /* Inherit mode of parent directory. */
126 /* Provisionally add all 'x' bits */
127 result
|= (S_IXUSR
| S_IXGRP
| S_IXOTH
);
129 /* Apply directory mask */
130 result
&= lp_dir_mask(SNUM(conn
));
131 /* Add in force bits */
132 result
|= lp_force_dir_mode(SNUM(conn
));
135 if (lp_map_archive(SNUM(conn
)) && IS_DOS_ARCHIVE(dosmode
))
138 if (lp_map_system(SNUM(conn
)) && IS_DOS_SYSTEM(dosmode
))
141 if (lp_map_hidden(SNUM(conn
)) && IS_DOS_HIDDEN(dosmode
))
145 /* Inherit 666 component of parent directory mode */
146 result
|= dir_mode
& (S_IRUSR
| S_IRGRP
| S_IROTH
| S_IWUSR
| S_IWGRP
| S_IWOTH
);
148 /* Apply mode mask */
149 result
&= lp_create_mask(SNUM(conn
));
150 /* Add in force bits */
151 result
|= lp_force_create_mode(SNUM(conn
));
155 DEBUG(3,("unix_mode(%s) returning 0%o\n", smb_fname_str_dbg(smb_fname
),
160 /****************************************************************************
161 Change a unix mode to a dos mode.
162 ****************************************************************************/
164 static uint32
dos_mode_from_sbuf(connection_struct
*conn
,
165 const struct smb_filename
*smb_fname
)
168 enum mapreadonly_options ro_opts
= (enum mapreadonly_options
)lp_map_readonly(SNUM(conn
));
170 if (ro_opts
== MAP_READONLY_YES
) {
171 /* Original Samba method - map inverse of user "w" bit. */
172 if ((smb_fname
->st
.st_ex_mode
& S_IWUSR
) == 0) {
175 } else if (ro_opts
== MAP_READONLY_PERMISSIONS
) {
176 /* Check actual permissions for read-only. */
177 if (!can_write_to_file(conn
, smb_fname
)) {
180 } /* Else never set the readonly bit. */
182 if (MAP_ARCHIVE(conn
) && ((smb_fname
->st
.st_ex_mode
& S_IXUSR
) != 0))
185 if (MAP_SYSTEM(conn
) && ((smb_fname
->st
.st_ex_mode
& S_IXGRP
) != 0))
188 if (MAP_HIDDEN(conn
) && ((smb_fname
->st
.st_ex_mode
& S_IXOTH
) != 0))
191 if (S_ISDIR(smb_fname
->st
.st_ex_mode
))
192 result
= aDIR
| (result
& aRONLY
);
194 result
|= set_link_read_only_flag(&smb_fname
->st
);
196 DEBUG(8,("dos_mode_from_sbuf returning "));
198 if (result
& aHIDDEN
) DEBUG(8, ("h"));
199 if (result
& aRONLY
) DEBUG(8, ("r"));
200 if (result
& aSYSTEM
) DEBUG(8, ("s"));
201 if (result
& aDIR
) DEBUG(8, ("d"));
202 if (result
& aARCH
) DEBUG(8, ("a"));
208 /****************************************************************************
209 Get DOS attributes from an EA.
210 This can also pull the create time into the stat struct inside smb_fname.
211 ****************************************************************************/
213 static bool get_ea_dos_attribute(connection_struct
*conn
,
214 struct smb_filename
*smb_fname
,
217 struct xattr_DOSATTRIB dosattrib
;
218 enum ndr_err_code ndr_err
;
224 if (!lp_store_dos_attributes(SNUM(conn
))) {
228 /* Don't reset pattr to zero as we may already have filename-based attributes we
231 sizeret
= SMB_VFS_GETXATTR(conn
, smb_fname
->base_name
,
232 SAMBA_XATTR_DOS_ATTRIB
, attrstr
,
237 || errno
== ENOTSUP
) {
241 DEBUG(1,("get_ea_dos_attribute: Cannot get attribute "
242 "from EA on file %s: Error = %s\n",
243 smb_fname_str_dbg(smb_fname
),
245 set_store_dos_attributes(SNUM(conn
), False
);
250 blob
.data
= (uint8_t *)attrstr
;
251 blob
.length
= sizeret
;
253 ndr_err
= ndr_pull_struct_blob(&blob
, talloc_tos(), &dosattrib
,
254 (ndr_pull_flags_fn_t
)ndr_pull_xattr_DOSATTRIB
);
256 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
257 DEBUG(1,("get_ea_dos_attribute: bad ndr decode "
258 "from EA on file %s: Error = %s\n",
259 smb_fname_str_dbg(smb_fname
),
260 ndr_errstr(ndr_err
)));
264 DEBUG(10,("get_ea_dos_attribute: %s attr = %s\n",
265 smb_fname_str_dbg(smb_fname
), dosattrib
.attrib_hex
));
267 switch (dosattrib
.version
) {
269 dosattr
= dosattrib
.info
.compatinfoFFFF
.attrib
;
272 dosattr
= dosattrib
.info
.info1
.attrib
;
273 if (!null_nttime(dosattrib
.info
.info1
.create_time
)) {
274 struct timespec create_time
=
275 nt_time_to_unix_timespec(
276 &dosattrib
.info
.info1
.create_time
);
278 update_stat_ex_create_time(&smb_fname
->st
,
281 DEBUG(10,("get_ea_dos_attribute: file %s case 1 "
283 smb_fname_str_dbg(smb_fname
),
284 time_to_asc(convert_timespec_to_time_t(
289 dosattr
= dosattrib
.info
.oldinfo2
.attrib
;
290 /* Don't know what flags to check for this case. */
293 dosattr
= dosattrib
.info
.info3
.attrib
;
294 if ((dosattrib
.info
.info3
.valid_flags
& XATTR_DOSINFO_CREATE_TIME
) &&
295 !null_nttime(dosattrib
.info
.info3
.create_time
)) {
296 struct timespec create_time
=
297 nt_time_to_unix_timespec(
298 &dosattrib
.info
.info3
.create_time
);
300 update_stat_ex_create_time(&smb_fname
->st
,
303 DEBUG(10,("get_ea_dos_attribute: file %s case 3 "
305 smb_fname_str_dbg(smb_fname
),
306 time_to_asc(convert_timespec_to_time_t(
311 DEBUG(1,("get_ea_dos_attribute: Badly formed DOSATTRIB on "
312 "file %s - %s\n", smb_fname_str_dbg(smb_fname
),
317 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
320 *pattr
= (uint32
)(dosattr
& SAMBA_ATTRIBUTES_MASK
);
322 DEBUG(8,("get_ea_dos_attribute returning (0x%x)", dosattr
));
324 if (dosattr
& aHIDDEN
) DEBUG(8, ("h"));
325 if (dosattr
& aRONLY
) DEBUG(8, ("r"));
326 if (dosattr
& aSYSTEM
) DEBUG(8, ("s"));
327 if (dosattr
& aDIR
) DEBUG(8, ("d"));
328 if (dosattr
& aARCH
) DEBUG(8, ("a"));
335 /****************************************************************************
336 Set DOS attributes in an EA.
337 Also sets the create time.
338 ****************************************************************************/
340 static bool set_ea_dos_attribute(connection_struct
*conn
,
341 struct smb_filename
*smb_fname
,
344 struct xattr_DOSATTRIB dosattrib
;
345 enum ndr_err_code ndr_err
;
347 files_struct
*fsp
= NULL
;
350 if (!lp_store_dos_attributes(SNUM(conn
))) {
354 ZERO_STRUCT(dosattrib
);
357 dosattrib
.version
= 3;
358 dosattrib
.info
.info3
.valid_flags
= XATTR_DOSINFO_ATTRIB
|
359 XATTR_DOSINFO_CREATE_TIME
;
360 dosattrib
.info
.info3
.attrib
= dosmode
;
361 unix_timespec_to_nt_time(&dosattrib
.info
.info3
.create_time
,
362 smb_fname
->st
.st_ex_btime
);
364 DEBUG(10,("set_ea_dos_attributes: set attribute 0x%x, btime = %s on file %s\n",
365 (unsigned int)dosmode
,
366 time_to_asc(convert_timespec_to_time_t(smb_fname
->st
.st_ex_btime
)),
367 smb_fname_str_dbg(smb_fname
) ));
369 ndr_err
= ndr_push_struct_blob(
370 &blob
, talloc_tos(), &dosattrib
,
371 (ndr_push_flags_fn_t
)ndr_push_xattr_DOSATTRIB
);
373 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
374 DEBUG(5, ("create_acl_blob: ndr_push_xattr_DOSATTRIB failed: %s\n",
375 ndr_errstr(ndr_err
)));
379 if (blob
.data
== NULL
|| blob
.length
== 0) {
383 if (SMB_VFS_SETXATTR(conn
, smb_fname
->base_name
,
384 SAMBA_XATTR_DOS_ATTRIB
, blob
.data
, blob
.length
,
386 if((errno
!= EPERM
) && (errno
!= EACCES
)) {
389 || errno
== ENOTSUP
) {
393 DEBUG(1,("set_ea_dos_attributes: Cannot set "
394 "attribute EA on file %s: Error = %s\n",
395 smb_fname_str_dbg(smb_fname
),
397 set_store_dos_attributes(SNUM(conn
), False
);
402 /* We want DOS semantics, ie allow non owner with write permission to change the
403 bits on a file. Just like file_ntimes below.
406 /* Check if we have write access. */
407 if(!CAN_WRITE(conn
) || !lp_dos_filemode(SNUM(conn
)))
411 * We need to open the file with write access whilst
412 * still in our current user context. This ensures we
413 * are not violating security in doing the setxattr.
416 if (!NT_STATUS_IS_OK(open_file_fchmod(NULL
, conn
, smb_fname
,
420 if (SMB_VFS_SETXATTR(conn
, smb_fname
->base_name
,
421 SAMBA_XATTR_DOS_ATTRIB
, blob
.data
,
422 blob
.length
, 0) == 0) {
426 close_file_fchmod(NULL
, fsp
);
429 DEBUG(10,("set_ea_dos_attribute: set EA 0x%x on file %s\n",
430 (unsigned int)dosmode
,
431 smb_fname_str_dbg(smb_fname
)));
435 /****************************************************************************
436 Change a unix mode to a dos mode for an ms dfs link.
437 ****************************************************************************/
439 uint32
dos_mode_msdfs(connection_struct
*conn
,
440 const struct smb_filename
*smb_fname
)
444 DEBUG(8,("dos_mode_msdfs: %s\n", smb_fname_str_dbg(smb_fname
)));
446 if (!VALID_STAT(smb_fname
->st
)) {
450 /* First do any modifications that depend on the path name. */
451 /* hide files with a name starting with a . */
452 if (lp_hide_dot_files(SNUM(conn
))) {
453 const char *p
= strrchr_m(smb_fname
->base_name
, '/');
457 p
= smb_fname
->base_name
;
460 /* Only . and .. are not hidden. */
461 if (p
[0] == '.' && !((p
[1] == '\0') ||
462 (p
[1] == '.' && p
[2] == '\0'))) {
467 result
|= dos_mode_from_sbuf(conn
, smb_fname
);
469 /* Optimization : Only call is_hidden_path if it's not already
471 if (!(result
& aHIDDEN
) &&
472 IS_HIDDEN_PATH(conn
, smb_fname
->base_name
)) {
477 result
= FILE_ATTRIBUTE_NORMAL
;
480 result
= filter_mode_by_protocol(result
);
482 DEBUG(8,("dos_mode_msdfs returning "));
484 if (result
& aHIDDEN
) DEBUG(8, ("h"));
485 if (result
& aRONLY
) DEBUG(8, ("r"));
486 if (result
& aSYSTEM
) DEBUG(8, ("s"));
487 if (result
& aDIR
) DEBUG(8, ("d"));
488 if (result
& aARCH
) DEBUG(8, ("a"));
489 if (result
& FILE_ATTRIBUTE_SPARSE
) DEBUG(8, ("[sparse]"));
496 #ifdef HAVE_STAT_DOS_FLAGS
497 /****************************************************************************
498 Convert dos attributes (FILE_ATTRIBUTE_*) to dos stat flags (UF_*)
499 ****************************************************************************/
501 int dos_attributes_to_stat_dos_flags(uint32_t dosmode
)
503 uint32_t dos_stat_flags
= 0;
506 dos_stat_flags
|= UF_DOS_ARCHIVE
;
507 if (dosmode
& aHIDDEN
)
508 dos_stat_flags
|= UF_DOS_HIDDEN
;
509 if (dosmode
& aRONLY
)
510 dos_stat_flags
|= UF_DOS_RO
;
511 if (dosmode
& aSYSTEM
)
512 dos_stat_flags
|= UF_DOS_SYSTEM
;
513 if (dosmode
& FILE_ATTRIBUTE_NONINDEXED
)
514 dos_stat_flags
|= UF_DOS_NOINDEX
;
516 return dos_stat_flags
;
519 /****************************************************************************
520 Gets DOS attributes, accessed via st_ex_flags in the stat struct.
521 ****************************************************************************/
523 static bool get_stat_dos_flags(connection_struct
*conn
,
524 const struct smb_filename
*smb_fname
,
527 SMB_ASSERT(VALID_STAT(smb_fname
->st
));
530 if (!lp_store_dos_attributes(SNUM(conn
))) {
534 DEBUG(5, ("Getting stat dos attributes for %s.\n",
535 smb_fname_str_dbg(smb_fname
)));
537 if (smb_fname
->st
.st_ex_flags
& UF_DOS_ARCHIVE
)
539 if (smb_fname
->st
.st_ex_flags
& UF_DOS_HIDDEN
)
541 if (smb_fname
->st
.st_ex_flags
& UF_DOS_RO
)
543 if (smb_fname
->st
.st_ex_flags
& UF_DOS_SYSTEM
)
545 if (smb_fname
->st
.st_ex_flags
& UF_DOS_NOINDEX
)
546 *dosmode
|= FILE_ATTRIBUTE_NONINDEXED
;
547 if (smb_fname
->st
.st_ex_flags
& FILE_ATTRIBUTE_SPARSE
)
548 *dosmode
|= FILE_ATTRIBUTE_SPARSE
;
549 if (S_ISDIR(smb_fname
->st
.st_ex_mode
))
552 *dosmode
|= set_link_read_only_flag(&smb_fname
->st
);
557 /****************************************************************************
558 Sets DOS attributes, stored in st_ex_flags of the inode.
559 ****************************************************************************/
561 static bool set_stat_dos_flags(connection_struct
*conn
,
562 const struct smb_filename
*smb_fname
,
564 bool *attributes_changed
)
566 uint32_t new_flags
= 0;
569 SMB_ASSERT(VALID_STAT(smb_fname
->st
));
570 SMB_ASSERT(attributes_changed
);
572 *attributes_changed
= false;
574 if (!lp_store_dos_attributes(SNUM(conn
))) {
578 DEBUG(5, ("Setting stat dos attributes for %s.\n",
579 smb_fname_str_dbg(smb_fname
)));
581 new_flags
= (smb_fname
->st
.st_ex_flags
& ~UF_DOS_FLAGS
) |
582 dos_attributes_to_stat_dos_flags(dosmode
);
584 /* Return early if no flags changed. */
585 if (new_flags
== smb_fname
->st
.st_ex_flags
)
588 DEBUG(5, ("Setting stat dos attributes=0x%x, prev=0x%x\n", new_flags
,
589 smb_fname
->st
.st_ex_flags
));
591 /* Set new flags with chflags. */
592 error
= SMB_VFS_CHFLAGS(conn
, smb_fname
->base_name
, new_flags
);
594 DEBUG(0, ("Failed setting new stat dos attributes (0x%x) on "
595 "file %s! errno=%d\n", new_flags
,
596 smb_fname_str_dbg(smb_fname
), errno
));
600 *attributes_changed
= true;
603 #endif /* HAVE_STAT_DOS_FLAGS */
605 /****************************************************************************
606 Change a unix mode to a dos mode.
607 May also read the create timespec into the stat struct in smb_fname
608 if "store dos attributes" is true.
609 ****************************************************************************/
611 uint32
dos_mode(connection_struct
*conn
, struct smb_filename
*smb_fname
)
614 bool offline
, used_stat_dos_flags
= false;
616 DEBUG(8,("dos_mode: %s\n", smb_fname_str_dbg(smb_fname
)));
618 if (!VALID_STAT(smb_fname
->st
)) {
622 /* First do any modifications that depend on the path name. */
623 /* hide files with a name starting with a . */
624 if (lp_hide_dot_files(SNUM(conn
))) {
625 const char *p
= strrchr_m(smb_fname
->base_name
,'/');
629 p
= smb_fname
->base_name
;
632 /* Only . and .. are not hidden. */
633 if (p
[0] == '.' && !((p
[1] == '\0') ||
634 (p
[1] == '.' && p
[2] == '\0'))) {
639 #ifdef HAVE_STAT_DOS_FLAGS
640 used_stat_dos_flags
= get_stat_dos_flags(conn
, smb_fname
, &result
);
642 if (!used_stat_dos_flags
) {
643 /* Get the DOS attributes from an EA by preference. */
644 if (!get_ea_dos_attribute(conn
, smb_fname
, &result
)) {
645 result
|= dos_mode_from_sbuf(conn
, smb_fname
);
649 offline
= SMB_VFS_IS_OFFLINE(conn
, smb_fname
->base_name
, &smb_fname
->st
);
650 if (S_ISREG(smb_fname
->st
.st_ex_mode
) && offline
) {
651 result
|= FILE_ATTRIBUTE_OFFLINE
;
654 /* Optimization : Only call is_hidden_path if it's not already
656 if (!(result
& aHIDDEN
) &&
657 IS_HIDDEN_PATH(conn
, smb_fname
->base_name
)) {
662 result
= FILE_ATTRIBUTE_NORMAL
;
665 result
= filter_mode_by_protocol(result
);
667 DEBUG(8,("dos_mode returning "));
669 if (result
& aHIDDEN
) DEBUG(8, ("h"));
670 if (result
& aRONLY
) DEBUG(8, ("r"));
671 if (result
& aSYSTEM
) DEBUG(8, ("s"));
672 if (result
& aDIR
) DEBUG(8, ("d"));
673 if (result
& aARCH
) DEBUG(8, ("a"));
674 if (result
& FILE_ATTRIBUTE_SPARSE
) DEBUG(8, ("[sparse]"));
681 /*******************************************************************
682 chmod a file - but preserve some bits.
683 If "store dos attributes" is also set it will store the create time
684 from the stat struct in smb_fname (in NTTIME format) in the EA
686 ********************************************************************/
688 int file_set_dosmode(connection_struct
*conn
, struct smb_filename
*smb_fname
,
689 uint32 dosmode
, const char *parent_dir
, bool newfile
)
694 int ret
= -1, lret
= -1;
696 struct timespec new_create_timespec
;
698 /* We only allow READONLY|HIDDEN|SYSTEM|DIRECTORY|ARCHIVE here. */
699 dosmode
&= (SAMBA_ATTRIBUTES_MASK
| FILE_ATTRIBUTE_OFFLINE
);
701 DEBUG(10,("file_set_dosmode: setting dos mode 0x%x on file %s\n",
702 dosmode
, smb_fname_str_dbg(smb_fname
)));
704 unixmode
= smb_fname
->st
.st_ex_mode
;
706 get_acl_group_bits(conn
, smb_fname
->base_name
,
707 &smb_fname
->st
.st_ex_mode
);
709 if (S_ISDIR(smb_fname
->st
.st_ex_mode
))
714 new_create_timespec
= smb_fname
->st
.st_ex_btime
;
716 old_mode
= dos_mode(conn
, smb_fname
);
718 if (dosmode
& FILE_ATTRIBUTE_OFFLINE
) {
719 if (!(old_mode
& FILE_ATTRIBUTE_OFFLINE
)) {
720 lret
= SMB_VFS_SET_OFFLINE(conn
, smb_fname
->base_name
);
722 DEBUG(0, ("set_dos_mode: client has asked to "
723 "set FILE_ATTRIBUTE_OFFLINE to "
724 "%s/%s but there was an error while "
725 "setting it or it is not "
726 "supported.\n", parent_dir
,
727 smb_fname_str_dbg(smb_fname
)));
732 dosmode
&= ~FILE_ATTRIBUTE_OFFLINE
;
733 old_mode
&= ~FILE_ATTRIBUTE_OFFLINE
;
735 smb_fname
->st
.st_ex_btime
= new_create_timespec
;
737 #ifdef HAVE_STAT_DOS_FLAGS
739 bool attributes_changed
;
741 if (set_stat_dos_flags(conn
, smb_fname
, dosmode
,
742 &attributes_changed
))
744 if (!newfile
&& attributes_changed
) {
745 notify_fname(conn
, NOTIFY_ACTION_MODIFIED
,
746 FILE_NOTIFY_CHANGE_ATTRIBUTES
,
747 smb_fname
->base_name
);
749 smb_fname
->st
.st_ex_mode
= unixmode
;
754 /* Store the DOS attributes in an EA by preference. */
755 if (set_ea_dos_attribute(conn
, smb_fname
, dosmode
)) {
757 notify_fname(conn
, NOTIFY_ACTION_MODIFIED
,
758 FILE_NOTIFY_CHANGE_ATTRIBUTES
,
759 smb_fname
->base_name
);
761 smb_fname
->st
.st_ex_mode
= unixmode
;
765 unixmode
= unix_mode(conn
, dosmode
, smb_fname
, parent_dir
);
767 /* preserve the s bits */
768 mask
|= (S_ISUID
| S_ISGID
);
770 /* preserve the t bit */
775 /* possibly preserve the x bits */
776 if (!MAP_ARCHIVE(conn
))
778 if (!MAP_SYSTEM(conn
))
780 if (!MAP_HIDDEN(conn
))
783 unixmode
|= (smb_fname
->st
.st_ex_mode
& mask
);
785 /* if we previously had any r bits set then leave them alone */
786 if ((tmp
= smb_fname
->st
.st_ex_mode
& (S_IRUSR
|S_IRGRP
|S_IROTH
))) {
787 unixmode
&= ~(S_IRUSR
|S_IRGRP
|S_IROTH
);
791 /* if we previously had any w bits set then leave them alone
792 whilst adding in the new w bits, if the new mode is not rdonly */
793 if (!IS_DOS_READONLY(dosmode
)) {
794 unixmode
|= (smb_fname
->st
.st_ex_mode
& (S_IWUSR
|S_IWGRP
|S_IWOTH
));
797 ret
= SMB_VFS_CHMOD(conn
, smb_fname
->base_name
, unixmode
);
799 if(!newfile
|| (lret
!= -1)) {
800 notify_fname(conn
, NOTIFY_ACTION_MODIFIED
,
801 FILE_NOTIFY_CHANGE_ATTRIBUTES
,
802 smb_fname
->base_name
);
804 smb_fname
->st
.st_ex_mode
= unixmode
;
808 if((errno
!= EPERM
) && (errno
!= EACCES
))
811 if(!lp_dos_filemode(SNUM(conn
)))
814 /* We want DOS semantics, ie allow non owner with write permission to change the
815 bits on a file. Just like file_ntimes below.
818 /* Check if we have write access. */
819 if (CAN_WRITE(conn
)) {
821 * We need to open the file with write access whilst
822 * still in our current user context. This ensures we
823 * are not violating security in doing the fchmod.
824 * This file open does *not* break any oplocks we are
825 * holding. We need to review this.... may need to
826 * break batch oplocks open by others. JRA.
829 if (!NT_STATUS_IS_OK(open_file_fchmod(NULL
, conn
, smb_fname
,
833 ret
= SMB_VFS_FCHMOD(fsp
, unixmode
);
835 close_file_fchmod(NULL
, fsp
);
837 notify_fname(conn
, NOTIFY_ACTION_MODIFIED
,
838 FILE_NOTIFY_CHANGE_ATTRIBUTES
,
839 smb_fname
->base_name
);
842 smb_fname
->st
.st_ex_mode
= unixmode
;
850 NTSTATUS
file_set_sparse(connection_struct
*conn
,
851 struct smb_filename
*smb_fname
,
855 uint32_t old_dosmode
;
856 uint32_t new_dosmode
;
858 DEBUG(10,("file_set_sparse: setting sparse bit %u on file %s\n",
859 sparse
, smb_fname_str_dbg(smb_fname
)));
861 if (!lp_store_dos_attributes(SNUM(conn
))) {
862 return NT_STATUS_INVALID_DEVICE_REQUEST
;
865 SET_STAT_INVALID(st
);
867 if (SMB_VFS_STAT(conn
, smb_fname
)) {
868 return map_nt_error_from_unix(errno
);
871 old_dosmode
= dos_mode(conn
, smb_fname
);
873 if (sparse
&& !(old_dosmode
& FILE_ATTRIBUTE_SPARSE
)) {
874 new_dosmode
= old_dosmode
| FILE_ATTRIBUTE_SPARSE
;
875 } else if (!sparse
&& (old_dosmode
& FILE_ATTRIBUTE_SPARSE
)) {
876 new_dosmode
= old_dosmode
& ~FILE_ATTRIBUTE_SPARSE
;
881 /* Store the DOS attributes in an EA. */
882 if (!set_ea_dos_attribute(conn
, smb_fname
,
887 return map_nt_error_from_unix(errno
);
890 notify_fname(conn
, NOTIFY_ACTION_MODIFIED
,
891 FILE_NOTIFY_CHANGE_ATTRIBUTES
,
892 smb_fname
->base_name
);
898 /*******************************************************************
899 Wrapper around the VFS ntimes that possibly allows DOS semantics rather
901 *******************************************************************/
903 int file_ntimes(connection_struct
*conn
, const struct smb_filename
*smb_fname
,
904 struct smb_file_time
*ft
)
910 DEBUG(6, ("file_ntime: actime: %s",
911 time_to_asc(convert_timespec_to_time_t(ft
->atime
))));
912 DEBUG(6, ("file_ntime: modtime: %s",
913 time_to_asc(convert_timespec_to_time_t(ft
->mtime
))));
914 DEBUG(6, ("file_ntime: ctime: %s",
915 time_to_asc(convert_timespec_to_time_t(ft
->ctime
))));
916 DEBUG(6, ("file_ntime: createtime: %s",
917 time_to_asc(convert_timespec_to_time_t(ft
->create_time
))));
919 /* Don't update the time on read-only shares */
920 /* We need this as set_filetime (which can be called on
921 close and other paths) can end up calling this function
922 without the NEED_WRITE protection. Found by :
923 Leo Weppelman <leo@wau.mis.ah.nl>
926 if (!CAN_WRITE(conn
)) {
930 if(SMB_VFS_NTIMES(conn
, smb_fname
, ft
) == 0) {
934 if((errno
!= EPERM
) && (errno
!= EACCES
)) {
938 if(!lp_dos_filetimes(SNUM(conn
))) {
942 /* We have permission (given by the Samba admin) to
943 break POSIX semantics and allow a user to change
944 the time on a file they don't own but can write to
948 /* Check if we have write access. */
949 if (can_write_to_file(conn
, smb_fname
)) {
950 /* We are allowed to become root and change the filetime. */
952 ret
= SMB_VFS_NTIMES(conn
, smb_fname
, ft
);
959 /******************************************************************
960 Force a "sticky" write time on a pathname. This will always be
961 returned on all future write time queries and set on close.
962 ******************************************************************/
964 bool set_sticky_write_time_path(struct file_id fileid
, struct timespec mtime
)
966 if (null_timespec(mtime
)) {
970 if (!set_sticky_write_time(fileid
, mtime
)) {
977 /******************************************************************
978 Force a "sticky" write time on an fsp. This will always be
979 returned on all future write time queries and set on close.
980 ******************************************************************/
982 bool set_sticky_write_time_fsp(struct files_struct
*fsp
, struct timespec mtime
)
984 if (null_timespec(mtime
)) {
988 fsp
->write_time_forced
= true;
989 TALLOC_FREE(fsp
->update_write_time_event
);
991 return set_sticky_write_time_path(fsp
->file_id
, mtime
);
994 /******************************************************************
995 Set a create time EA.
996 ******************************************************************/
998 NTSTATUS
set_create_timespec_ea(connection_struct
*conn
,
999 const struct smb_filename
*psmb_fname
,
1000 struct timespec create_time
)
1003 struct smb_filename
*smb_fname
= NULL
;
1007 if (!lp_store_dos_attributes(SNUM(conn
))) {
1008 return NT_STATUS_OK
;
1011 status
= create_synthetic_smb_fname(talloc_tos(),
1012 psmb_fname
->base_name
,
1013 NULL
, &psmb_fname
->st
,
1016 if (!NT_STATUS_IS_OK(status
)) {
1020 dosmode
= dos_mode(conn
, smb_fname
);
1022 smb_fname
->st
.st_ex_btime
= create_time
;
1024 ret
= file_set_dosmode(conn
, smb_fname
, dosmode
, NULL
, false);
1026 map_nt_error_from_unix(errno
);
1029 DEBUG(10,("set_create_timespec_ea: wrote create time EA for file %s\n",
1030 smb_fname_str_dbg(smb_fname
)));
1032 return NT_STATUS_OK
;
1035 /******************************************************************
1036 Return a create time.
1037 ******************************************************************/
1039 struct timespec
get_create_timespec(connection_struct
*conn
,
1040 struct files_struct
*fsp
,
1041 const struct smb_filename
*smb_fname
)
1043 return smb_fname
->st
.st_ex_btime
;
1046 /******************************************************************
1047 Return a change time (may look at EA in future).
1048 ******************************************************************/
1050 struct timespec
get_change_timespec(connection_struct
*conn
,
1051 struct files_struct
*fsp
,
1052 const struct smb_filename
*smb_fname
)
1054 return smb_fname
->st
.st_ex_mtime
;