2 Unix SMB/CIFS implementation.
3 dos mode handling functions
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /****************************************************************************
24 change a dos mode to a unix mode
25 base permission for files:
27 apply read/write bits from parent directory.
29 everybody gets read bit set
30 dos readonly is represented in unix by removing everyone's write bit
31 dos archive is represented in unix by the user's execute bit
32 dos system is represented in unix by the group's execute bit
33 dos hidden is represented in unix by the other's execute bit
35 Then apply create mask,
38 base permission for directories:
39 dos directory is represented in unix by unix's dir bit and the exec bit
41 Then apply create mask,
44 ****************************************************************************/
45 mode_t
unix_mode(connection_struct
*conn
,int dosmode
,const char *fname
)
47 mode_t result
= (S_IRUSR
| S_IRGRP
| S_IROTH
);
48 mode_t dir_mode
= 0; /* Mode of the parent directory if inheriting. */
50 if ( !IS_DOS_READONLY(dosmode
) )
51 result
|= (S_IWUSR
| S_IWGRP
| S_IWOTH
);
53 if (fname
&& lp_inherit_perms(SNUM(conn
))) {
57 dname
= parent_dirname(fname
);
58 DEBUG(2,("unix_mode(%s) inheriting from %s\n",fname
,dname
));
59 if (SMB_VFS_STAT(conn
,dname
,&sbuf
) != 0) {
60 DEBUG(4,("unix_mode(%s) failed, [dir %s]: %s\n",fname
,dname
,strerror(errno
)));
61 return(0); /* *** shouldn't happen! *** */
64 /* Save for later - but explicitly remove setuid bit for safety. */
65 dir_mode
= sbuf
.st_mode
& ~S_ISUID
;
66 DEBUG(2,("unix_mode(%s) inherit mode %o\n",fname
,(int)dir_mode
));
71 if (IS_DOS_DIR(dosmode
)) {
72 /* We never make directories read only for the owner as under DOS a user
73 can always create a file in a read-only directory. */
74 result
|= (S_IFDIR
| S_IWUSR
);
77 /* Inherit mode of parent directory. */
80 /* Provisionally add all 'x' bits */
81 result
|= (S_IXUSR
| S_IXGRP
| S_IXOTH
);
83 /* Apply directory mask */
84 result
&= lp_dir_mask(SNUM(conn
));
85 /* Add in force bits */
86 result
|= lp_force_dir_mode(SNUM(conn
));
89 if (lp_map_archive(SNUM(conn
)) && IS_DOS_ARCHIVE(dosmode
))
92 if (lp_map_system(SNUM(conn
)) && IS_DOS_SYSTEM(dosmode
))
95 if (lp_map_hidden(SNUM(conn
)) && IS_DOS_HIDDEN(dosmode
))
99 /* Inherit 666 component of parent directory mode */
101 & (S_IRUSR
| S_IRGRP
| S_IROTH
| S_IWUSR
| S_IWGRP
| S_IWOTH
);
103 /* Apply mode mask */
104 result
&= lp_create_mask(SNUM(conn
));
105 /* Add in force bits */
106 result
|= lp_force_create_mode(SNUM(conn
));
110 DEBUG(3,("unix_mode(%s) returning 0%o\n",fname
,(int)result
));
115 /****************************************************************************
116 change a unix mode to a dos mode
117 ****************************************************************************/
118 uint32
dos_mode(connection_struct
*conn
,char *path
,SMB_STRUCT_STAT
*sbuf
)
122 DEBUG(8,("dos_mode: %s\n", path
));
124 if ((sbuf
->st_mode
& S_IWUSR
) == 0)
127 if (MAP_ARCHIVE(conn
) && ((sbuf
->st_mode
& S_IXUSR
) != 0))
130 if (MAP_SYSTEM(conn
) && ((sbuf
->st_mode
& S_IXGRP
) != 0))
133 if (MAP_HIDDEN(conn
) && ((sbuf
->st_mode
& S_IXOTH
) != 0))
136 if (S_ISDIR(sbuf
->st_mode
))
137 result
= aDIR
| (result
& aRONLY
);
139 #if defined (HAVE_STAT_ST_BLOCKS) && defined (HAVE_STAT_ST_BLKSIZE)
140 if (sbuf
->st_size
> sbuf
->st_blocks
* (SMB_OFF_T
)sbuf
->st_blksize
) {
141 result
|= FILE_ATTRIBUTE_SPARSE
;
147 if (S_ISLNK(sbuf
->st_mode
) && S_ISDIR(sbuf
->st_mode
))
152 /* hide files with a name starting with a . */
153 if (lp_hide_dot_files(SNUM(conn
))) {
154 char *p
= strrchr_m(path
,'/');
160 if (p
[0] == '.' && p
[1] != '.' && p
[1] != 0)
164 /* Optimization : Only call is_hidden_path if it's not already
166 if (!(result
& aHIDDEN
) && IS_HIDDEN_PATH(conn
,path
)) {
170 DEBUG(8,("dos_mode returning "));
172 if (result
& aHIDDEN
) DEBUG(8, ("h"));
173 if (result
& aRONLY
) DEBUG(8, ("r"));
174 if (result
& aSYSTEM
) DEBUG(8, ("s"));
175 if (result
& aDIR
) DEBUG(8, ("d"));
176 if (result
& aARCH
) DEBUG(8, ("a"));
183 /*******************************************************************
184 chmod a file - but preserve some bits
185 ********************************************************************/
187 int file_chmod(connection_struct
*conn
,char *fname
, uint32 dosmode
,SMB_STRUCT_STAT
*st
)
197 if (SMB_VFS_STAT(conn
,fname
,st
))
201 get_acl_group_bits(conn
, fname
, &st
->st_mode
);
203 if (S_ISDIR(st
->st_mode
))
208 if (dos_mode(conn
,fname
,st
) == dosmode
)
211 unixmode
= unix_mode(conn
,dosmode
,fname
);
213 /* preserve the s bits */
214 mask
|= (S_ISUID
| S_ISGID
);
216 /* preserve the t bit */
221 /* possibly preserve the x bits */
222 if (!MAP_ARCHIVE(conn
))
224 if (!MAP_SYSTEM(conn
))
226 if (!MAP_HIDDEN(conn
))
229 unixmode
|= (st
->st_mode
& mask
);
231 /* if we previously had any r bits set then leave them alone */
232 if ((tmp
= st
->st_mode
& (S_IRUSR
|S_IRGRP
|S_IROTH
))) {
233 unixmode
&= ~(S_IRUSR
|S_IRGRP
|S_IROTH
);
237 /* if we previously had any w bits set then leave them alone
238 whilst adding in the new w bits, if the new mode is not rdonly */
239 if (!IS_DOS_READONLY(dosmode
)) {
240 unixmode
|= (st
->st_mode
& (S_IWUSR
|S_IWGRP
|S_IWOTH
));
243 if ((ret
= SMB_VFS_CHMOD(conn
,fname
,unixmode
)) == 0)
246 if((errno
!= EPERM
) && (errno
!= EACCES
))
249 if(!lp_dos_filemode(SNUM(conn
)))
252 /* We want DOS semantics, ie allow non owner with write permission to change the
253 bits on a file. Just like file_utime below.
256 /* Check if we have write access. */
257 if (CAN_WRITE(conn
)) {
259 * We need to open the file with write access whilst
260 * still in our current user context. This ensures we
261 * are not violating security in doing the fchmod.
262 * This file open does *not* break any oplocks we are
263 * holding. We need to review this.... may need to
264 * break batch oplocks open by others. JRA.
266 files_struct
*fsp
= open_file_fchmod(conn
,fname
,st
);
270 ret
= SMB_VFS_FCHMOD(fsp
, fsp
->fd
, unixmode
);
272 close_file_fchmod(fsp
);
279 /*******************************************************************
280 Wrapper around dos_utime that possibly allows DOS semantics rather
282 *******************************************************************/
283 int file_utime(connection_struct
*conn
, char *fname
, struct utimbuf
*times
)
285 extern struct current_user current_user
;
291 if(SMB_VFS_UTIME(conn
,fname
, times
) == 0)
294 if((errno
!= EPERM
) && (errno
!= EACCES
))
297 if(!lp_dos_filetimes(SNUM(conn
)))
300 /* We have permission (given by the Samba admin) to
301 break POSIX semantics and allow a user to change
302 the time on a file they don't own but can write to
306 if(SMB_VFS_STAT(conn
,fname
,&sb
) != 0)
309 /* Check if we have write access. */
310 if (CAN_WRITE(conn
)) {
311 if (((sb
.st_mode
& S_IWOTH
) ||
313 ((sb
.st_mode
& S_IWUSR
) && current_user
.uid
==sb
.st_uid
) ||
314 ((sb
.st_mode
& S_IWGRP
) &&
315 in_group(sb
.st_gid
,current_user
.gid
,
316 current_user
.ngroups
,current_user
.groups
)))) {
317 /* We are allowed to become root and change the filetime. */
319 ret
= SMB_VFS_UTIME(conn
,fname
, times
);
327 /*******************************************************************
328 Change a filetime - possibly allowing DOS semantics.
329 *******************************************************************/
330 BOOL
set_filetime(connection_struct
*conn
, char *fname
, time_t mtime
)
332 struct utimbuf times
;
334 if (null_mtime(mtime
)) return(True
);
336 times
.modtime
= times
.actime
= mtime
;
338 if (file_utime(conn
, fname
, ×
)) {
339 DEBUG(4,("set_filetime(%s) failed: %s\n",fname
,strerror(errno
)));