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 static int set_sparse_flag(const SMB_STRUCT_STAT * const sbuf)
25 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
26 if (sbuf->st_size > sbuf->st_blocks * (SMB_OFF_T)STAT_ST_BLOCKSIZE) {
27 return FILE_ATTRIBUTE_SPARSE;
33 /****************************************************************************
34 Change a dos mode to a unix mode.
35 Base permission for files:
36 if creating file and inheriting
37 apply read/write bits from parent directory.
39 everybody gets read bit set
40 dos readonly is represented in unix by removing everyone's write bit
41 dos archive is represented in unix by the user's execute bit
42 dos system is represented in unix by the group's execute bit
43 dos hidden is represented in unix by the other's execute bit
45 Then apply create mask,
48 Base permission for directories:
49 dos directory is represented in unix by unix's dir bit and the exec bit
51 Then apply create mask,
54 ****************************************************************************/
56 mode_t unix_mode(connection_struct *conn, int dosmode, const char *fname, BOOL creating_file)
58 mode_t result = (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
59 mode_t dir_mode = 0; /* Mode of the parent directory if inheriting. */
61 if (!lp_store_dos_attributes(SNUM(conn)) && IS_DOS_READONLY(dosmode)) {
62 result &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
65 if (fname && creating_file && lp_inherit_perms(SNUM(conn))) {
69 dname = parent_dirname(fname);
70 DEBUG(2,("unix_mode(%s) inheriting from %s\n",fname,dname));
71 if (SMB_VFS_STAT(conn,dname,&sbuf) != 0) {
72 DEBUG(4,("unix_mode(%s) failed, [dir %s]: %s\n",fname,dname,strerror(errno)));
73 return(0); /* *** shouldn't happen! *** */
76 /* Save for later - but explicitly remove setuid bit for safety. */
77 dir_mode = sbuf.st_mode & ~S_ISUID;
78 DEBUG(2,("unix_mode(%s) inherit mode %o\n",fname,(int)dir_mode));
83 if (IS_DOS_DIR(dosmode)) {
84 /* We never make directories read only for the owner as under DOS a user
85 can always create a file in a read-only directory. */
86 result |= (S_IFDIR | S_IWUSR);
89 /* Inherit mode of parent directory. */
92 /* Provisionally add all 'x' bits */
93 result |= (S_IXUSR | S_IXGRP | S_IXOTH);
95 /* Apply directory mask */
96 result &= lp_dir_mask(SNUM(conn));
97 /* Add in force bits */
98 result |= lp_force_dir_mode(SNUM(conn));
101 if (lp_map_archive(SNUM(conn)) && IS_DOS_ARCHIVE(dosmode))
104 if (lp_map_system(SNUM(conn)) && IS_DOS_SYSTEM(dosmode))
107 if (lp_map_hidden(SNUM(conn)) && IS_DOS_HIDDEN(dosmode))
111 /* Inherit 666 component of parent directory mode */
112 result |= dir_mode & (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
114 /* Apply mode mask */
115 result &= lp_create_mask(SNUM(conn));
116 /* Add in force bits */
117 result |= lp_force_create_mode(SNUM(conn));
121 DEBUG(3,("unix_mode(%s) returning 0%o\n",fname,(int)result ));
125 /****************************************************************************
126 Change a unix mode to a dos mode.
127 ****************************************************************************/
129 uint32 dos_mode_from_sbuf(connection_struct *conn, const char *path, SMB_STRUCT_STAT *sbuf)
133 if (lp_acl_check_permissions(SNUM(conn))) {
134 if (!can_write_to_file(conn, path, sbuf)) {
137 } else if ((sbuf->st_mode & S_IWUSR) == 0) {
141 if (MAP_ARCHIVE(conn) && ((sbuf->st_mode & S_IXUSR) != 0))
144 if (MAP_SYSTEM(conn) && ((sbuf->st_mode & S_IXGRP) != 0))
147 if (MAP_HIDDEN(conn) && ((sbuf->st_mode & S_IXOTH) != 0))
150 if (S_ISDIR(sbuf->st_mode))
151 result = aDIR | (result & aRONLY);
153 result |= set_sparse_flag(sbuf);
157 if (S_ISLNK(sbuf->st_mode) && S_ISDIR(sbuf->st_mode))
162 DEBUG(8,("dos_mode_from_sbuf returning "));
164 if (result & aHIDDEN) DEBUG(8, ("h"));
165 if (result & aRONLY ) DEBUG(8, ("r"));
166 if (result & aSYSTEM) DEBUG(8, ("s"));
167 if (result & aDIR ) DEBUG(8, ("d"));
168 if (result & aARCH ) DEBUG(8, ("a"));
174 /****************************************************************************
175 Get DOS attributes from an EA.
176 ****************************************************************************/
178 static BOOL get_ea_dos_attribute(connection_struct *conn, const char *path,SMB_STRUCT_STAT *sbuf, uint32 *pattr)
182 unsigned int dosattr;
184 if (!lp_store_dos_attributes(SNUM(conn))) {
190 sizeret = SMB_VFS_GETXATTR(conn, path, SAMBA_XATTR_DOS_ATTRIB, attrstr, sizeof(attrstr));
192 #if defined(ENOTSUP) && defined(ENOATTR)
193 if ((errno != ENOTSUP) && (errno != ENOATTR) && (errno != EACCES)) {
194 DEBUG(1,("get_ea_dos_attributes: Cannot get attribute from EA on file %s: Error = %s\n",
195 path, strerror(errno) ));
196 set_store_dos_attributes(SNUM(conn), False);
201 /* Null terminate string. */
202 attrstr[sizeret] = 0;
203 DEBUG(10,("get_ea_dos_attribute: %s attrstr = %s\n", path, attrstr));
205 if (sizeret < 2 || attrstr[0] != '0' || attrstr[1] != 'x' ||
206 sscanf(attrstr, "%x", &dosattr) != 1) {
207 DEBUG(1,("get_ea_dos_attributes: Badly formed DOSATTRIB on file %s - %s\n", path, attrstr));
211 if (S_ISDIR(sbuf->st_mode)) {
214 *pattr = (uint32)(dosattr & SAMBA_ATTRIBUTES_MASK);
216 DEBUG(8,("get_ea_dos_attribute returning (0x%x)", dosattr));
218 if (dosattr & aHIDDEN) DEBUG(8, ("h"));
219 if (dosattr & aRONLY ) DEBUG(8, ("r"));
220 if (dosattr & aSYSTEM) DEBUG(8, ("s"));
221 if (dosattr & aDIR ) DEBUG(8, ("d"));
222 if (dosattr & aARCH ) DEBUG(8, ("a"));
229 /****************************************************************************
230 Set DOS attributes in an EA.
231 ****************************************************************************/
233 static BOOL set_ea_dos_attribute(connection_struct *conn, const char *path, SMB_STRUCT_STAT *sbuf, uint32 dosmode)
236 files_struct *fsp = NULL;
239 if (!lp_store_dos_attributes(SNUM(conn))) {
243 snprintf(attrstr, sizeof(attrstr)-1, "0x%x", dosmode & SAMBA_ATTRIBUTES_MASK);
244 if (SMB_VFS_SETXATTR(conn, path, SAMBA_XATTR_DOS_ATTRIB, attrstr, strlen(attrstr), 0) == -1) {
245 if((errno != EPERM) && (errno != EACCES)) {
248 || errno == ENOTSUP) {
252 set_store_dos_attributes(SNUM(conn), False);
257 /* We want DOS semantics, ie allow non owner with write permission to change the
258 bits on a file. Just like file_utime below.
261 /* Check if we have write access. */
262 if(!CAN_WRITE(conn) || !lp_dos_filemode(SNUM(conn)))
266 * We need to open the file with write access whilst
267 * still in our current user context. This ensures we
268 * are not violating security in doing the setxattr.
271 fsp = open_file_fchmod(conn,path,sbuf);
275 if (SMB_VFS_SETXATTR(conn, path, SAMBA_XATTR_DOS_ATTRIB, attrstr, strlen(attrstr), 0) == 0) {
279 close_file_fchmod(fsp);
282 DEBUG(10,("set_ea_dos_attribute: set EA %s on file %s\n", attrstr, path));
286 /****************************************************************************
287 Change a unix mode to a dos mode.
288 ****************************************************************************/
290 uint32 dos_mode(connection_struct *conn, const char *path,SMB_STRUCT_STAT *sbuf)
294 DEBUG(8,("dos_mode: %s\n", path));
296 if (!VALID_STAT(*sbuf)) {
300 /* Get the DOS attributes from an EA by preference. */
301 if (get_ea_dos_attribute(conn, path, sbuf, &result)) {
302 result |= set_sparse_flag(sbuf);
306 result = dos_mode_from_sbuf(conn, path, sbuf);
308 /* Now do any modifications that depend on the path name. */
309 /* hide files with a name starting with a . */
310 if (lp_hide_dot_files(SNUM(conn))) {
311 const char *p = strrchr_m(path,'/');
317 if (p[0] == '.' && p[1] != '.' && p[1] != 0)
321 /* Optimization : Only call is_hidden_path if it's not already
323 if (!(result & aHIDDEN) && IS_HIDDEN_PATH(conn,path)) {
327 DEBUG(8,("dos_mode returning "));
329 if (result & aHIDDEN) DEBUG(8, ("h"));
330 if (result & aRONLY ) DEBUG(8, ("r"));
331 if (result & aSYSTEM) DEBUG(8, ("s"));
332 if (result & aDIR ) DEBUG(8, ("d"));
333 if (result & aARCH ) DEBUG(8, ("a"));
340 /*******************************************************************
341 chmod a file - but preserve some bits.
342 ********************************************************************/
344 int file_set_dosmode(connection_struct *conn, const char *fname, uint32 dosmode, SMB_STRUCT_STAT *st, BOOL creating_file)
352 DEBUG(10,("file_set_dosmode: setting dos mode 0x%x on file %s\n", dosmode, fname));
353 if (!st || (st && !VALID_STAT(*st))) {
355 if (SMB_VFS_STAT(conn,fname,st))
359 get_acl_group_bits(conn, fname, &st->st_mode);
361 if (S_ISDIR(st->st_mode))
366 if (dos_mode(conn,fname,st) == dosmode)
369 /* Store the DOS attributes in an EA by preference. */
370 if (set_ea_dos_attribute(conn, fname, st, dosmode)) {
374 unixmode = unix_mode(conn,dosmode,fname, creating_file);
376 /* preserve the s bits */
377 mask |= (S_ISUID | S_ISGID);
379 /* preserve the t bit */
384 /* possibly preserve the x bits */
385 if (!MAP_ARCHIVE(conn))
387 if (!MAP_SYSTEM(conn))
389 if (!MAP_HIDDEN(conn))
392 unixmode |= (st->st_mode & mask);
394 /* if we previously had any r bits set then leave them alone */
395 if ((tmp = st->st_mode & (S_IRUSR|S_IRGRP|S_IROTH))) {
396 unixmode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
400 /* if we previously had any w bits set then leave them alone
401 whilst adding in the new w bits, if the new mode is not rdonly */
402 if (!IS_DOS_READONLY(dosmode)) {
403 unixmode |= (st->st_mode & (S_IWUSR|S_IWGRP|S_IWOTH));
406 if ((ret = SMB_VFS_CHMOD(conn,fname,unixmode)) == 0)
409 if((errno != EPERM) && (errno != EACCES))
412 if(!lp_dos_filemode(SNUM(conn)))
415 /* We want DOS semantics, ie allow non owner with write permission to change the
416 bits on a file. Just like file_utime below.
419 /* Check if we have write access. */
420 if (CAN_WRITE(conn)) {
422 * We need to open the file with write access whilst
423 * still in our current user context. This ensures we
424 * are not violating security in doing the fchmod.
425 * This file open does *not* break any oplocks we are
426 * holding. We need to review this.... may need to
427 * break batch oplocks open by others. JRA.
429 files_struct *fsp = open_file_fchmod(conn,fname,st);
433 ret = SMB_VFS_FCHMOD(fsp, fsp->fh->fd, unixmode);
435 close_file_fchmod(fsp);
441 /*******************************************************************
442 Wrapper around dos_utime that possibly allows DOS semantics rather
444 *******************************************************************/
446 int file_utime(connection_struct *conn, const char *fname, struct utimbuf *times)
448 SMB_STRUCT_STAT sbuf;
454 /* Don't update the time on read-only shares */
455 /* We need this as set_filetime (which can be called on
456 close and other paths) can end up calling this function
457 without the NEED_WRITE protection. Found by :
458 Leo Weppelman <leo@wau.mis.ah.nl>
461 if (!CAN_WRITE(conn)) {
465 if(SMB_VFS_UTIME(conn,fname, times) == 0)
468 if((errno != EPERM) && (errno != EACCES))
471 if(!lp_dos_filetimes(SNUM(conn)))
474 /* We have permission (given by the Samba admin) to
475 break POSIX semantics and allow a user to change
476 the time on a file they don't own but can write to
480 /* Check if we have write access. */
481 if (can_write_to_file(conn, fname, &sbuf)) {
482 /* We are allowed to become root and change the filetime. */
484 ret = SMB_VFS_UTIME(conn,fname, times);
491 /*******************************************************************
492 Change a filetime - possibly allowing DOS semantics.
493 *******************************************************************/
495 BOOL set_filetime(connection_struct *conn, const char *fname, time_t mtime)
497 struct utimbuf times;
499 if (null_mtime(mtime))
502 times.modtime = times.actime = mtime;
504 if (file_utime(conn, fname, ×)) {
505 DEBUG(4,("set_filetime(%s) failed: %s\n",fname,strerror(errno)));