r16792: minor (but critical fixes) from Jeremy, Volker, & Guenther
[Samba.git] / source / smbd / dosmode.c
blob61145fde2f0d03e781bd8f154ee84d70792a5140
1 /*
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 static int set_sparse_flag(const SMB_STRUCT_STAT * const sbuf)
26 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
27 if (sbuf->st_size > sbuf->st_blocks * (SMB_OFF_T)STAT_ST_BLOCKSIZE) {
28 return FILE_ATTRIBUTE_SPARSE;
30 #endif
31 return 0;
34 /****************************************************************************
35 Work out whether this file is offline
36 ****************************************************************************/
38 #ifndef ISDOT
39 #define ISDOT(p) (*(p) == '.' && *((p) + 1) == '\0')
40 #endif /* ISDOT */
42 #ifndef ISDOTDOT
43 #define ISDOTDOT(p) (*(p) == '.' && *((p) + 1) == '.' && *((p) + 2) == '\0')
44 #endif /* ISDOTDOT */
46 static uint32 set_offline_flag(connection_struct *conn, const char *const path)
48 if (ISDOT(path) || ISDOTDOT(path)) {
49 return 0;
52 if (!lp_dmapi_support(SNUM(conn)) || !dmapi_have_session()) {
53 return 0;
56 return dmapi_file_flags(path);
59 /****************************************************************************
60 Change a dos mode to a unix mode.
61 Base permission for files:
62 if creating file and inheriting
63 apply read/write bits from parent directory.
64 else
65 everybody gets read bit set
66 dos readonly is represented in unix by removing everyone's write bit
67 dos archive is represented in unix by the user's execute bit
68 dos system is represented in unix by the group's execute bit
69 dos hidden is represented in unix by the other's execute bit
70 if !inheriting {
71 Then apply create mask,
72 then add force bits.
74 Base permission for directories:
75 dos directory is represented in unix by unix's dir bit and the exec bit
76 if !inheriting {
77 Then apply create mask,
78 then add force bits.
80 ****************************************************************************/
82 mode_t unix_mode(connection_struct *conn, int dosmode, const char *fname, BOOL creating_file)
84 mode_t result = (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
85 mode_t dir_mode = 0; /* Mode of the parent directory if inheriting. */
87 if (!lp_store_dos_attributes(SNUM(conn)) && IS_DOS_READONLY(dosmode)) {
88 result &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
91 if (fname && creating_file && lp_inherit_perms(SNUM(conn))) {
92 char *dname;
93 SMB_STRUCT_STAT sbuf;
95 dname = parent_dirname(fname);
96 DEBUG(2,("unix_mode(%s) inheriting from %s\n",fname,dname));
97 if (SMB_VFS_STAT(conn,dname,&sbuf) != 0) {
98 DEBUG(4,("unix_mode(%s) failed, [dir %s]: %s\n",fname,dname,strerror(errno)));
99 return(0); /* *** shouldn't happen! *** */
102 /* Save for later - but explicitly remove setuid bit for safety. */
103 dir_mode = sbuf.st_mode & ~S_ISUID;
104 DEBUG(2,("unix_mode(%s) inherit mode %o\n",fname,(int)dir_mode));
105 /* Clear "result" */
106 result = 0;
109 if (IS_DOS_DIR(dosmode)) {
110 /* We never make directories read only for the owner as under DOS a user
111 can always create a file in a read-only directory. */
112 result |= (S_IFDIR | S_IWUSR);
114 if (dir_mode) {
115 /* Inherit mode of parent directory. */
116 result |= dir_mode;
117 } else {
118 /* Provisionally add all 'x' bits */
119 result |= (S_IXUSR | S_IXGRP | S_IXOTH);
121 /* Apply directory mask */
122 result &= lp_dir_mask(SNUM(conn));
123 /* Add in force bits */
124 result |= lp_force_dir_mode(SNUM(conn));
126 } else {
127 if (lp_map_archive(SNUM(conn)) && IS_DOS_ARCHIVE(dosmode))
128 result |= S_IXUSR;
130 if (lp_map_system(SNUM(conn)) && IS_DOS_SYSTEM(dosmode))
131 result |= S_IXGRP;
133 if (lp_map_hidden(SNUM(conn)) && IS_DOS_HIDDEN(dosmode))
134 result |= S_IXOTH;
136 if (dir_mode) {
137 /* Inherit 666 component of parent directory mode */
138 result |= dir_mode & (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
139 } else {
140 /* Apply mode mask */
141 result &= lp_create_mask(SNUM(conn));
142 /* Add in force bits */
143 result |= lp_force_create_mode(SNUM(conn));
147 DEBUG(3,("unix_mode(%s) returning 0%o\n",fname,(int)result ));
148 return(result);
151 /****************************************************************************
152 Change a unix mode to a dos mode.
153 ****************************************************************************/
155 static uint32 dos_mode_from_sbuf(connection_struct *conn, const char *path, SMB_STRUCT_STAT *sbuf)
157 int result = 0;
158 enum mapreadonly_options ro_opts = (enum mapreadonly_options)lp_map_readonly(SNUM(conn));
160 if (ro_opts == MAP_READONLY_YES) {
161 /* Original Samba method - map inverse of user "w" bit. */
162 if ((sbuf->st_mode & S_IWUSR) == 0) {
163 result |= aRONLY;
165 } else if (ro_opts == MAP_READONLY_PERMISSIONS) {
166 /* Check actual permissions for read-only. */
167 if (!can_write_to_file(conn, path, sbuf)) {
168 result |= aRONLY;
170 } /* Else never set the readonly bit. */
172 if (MAP_ARCHIVE(conn) && ((sbuf->st_mode & S_IXUSR) != 0))
173 result |= aARCH;
175 if (MAP_SYSTEM(conn) && ((sbuf->st_mode & S_IXGRP) != 0))
176 result |= aSYSTEM;
178 if (MAP_HIDDEN(conn) && ((sbuf->st_mode & S_IXOTH) != 0))
179 result |= aHIDDEN;
181 if (S_ISDIR(sbuf->st_mode))
182 result = aDIR | (result & aRONLY);
184 result |= set_sparse_flag(sbuf);
186 #ifdef S_ISLNK
187 #if LINKS_READ_ONLY
188 if (S_ISLNK(sbuf->st_mode) && S_ISDIR(sbuf->st_mode))
189 result |= aRONLY;
190 #endif
191 #endif
193 DEBUG(8,("dos_mode_from_sbuf returning "));
195 if (result & aHIDDEN) DEBUG(8, ("h"));
196 if (result & aRONLY ) DEBUG(8, ("r"));
197 if (result & aSYSTEM) DEBUG(8, ("s"));
198 if (result & aDIR ) DEBUG(8, ("d"));
199 if (result & aARCH ) DEBUG(8, ("a"));
201 DEBUG(8,("\n"));
202 return result;
205 /****************************************************************************
206 Get DOS attributes from an EA.
207 ****************************************************************************/
209 static BOOL get_ea_dos_attribute(connection_struct *conn, const char *path,SMB_STRUCT_STAT *sbuf, uint32 *pattr)
211 ssize_t sizeret;
212 fstring attrstr;
213 unsigned int dosattr;
215 if (!lp_store_dos_attributes(SNUM(conn))) {
216 return False;
219 /* Don't reset pattr to zero as we may already have filename-based attributes we
220 need to preserve. */
222 sizeret = SMB_VFS_GETXATTR(conn, path, SAMBA_XATTR_DOS_ATTRIB, attrstr, sizeof(attrstr));
223 if (sizeret == -1) {
224 #if defined(ENOTSUP) && defined(ENOATTR)
225 if ((errno != ENOTSUP) && (errno != ENOATTR) && (errno != EACCES) && (errno != EPERM)) {
226 DEBUG(1,("get_ea_dos_attributes: Cannot get attribute from EA on file %s: Error = %s\n",
227 path, strerror(errno) ));
228 set_store_dos_attributes(SNUM(conn), False);
230 #endif
231 return False;
233 /* Null terminate string. */
234 attrstr[sizeret] = 0;
235 DEBUG(10,("get_ea_dos_attribute: %s attrstr = %s\n", path, attrstr));
237 if (sizeret < 2 || attrstr[0] != '0' || attrstr[1] != 'x' ||
238 sscanf(attrstr, "%x", &dosattr) != 1) {
239 DEBUG(1,("get_ea_dos_attributes: Badly formed DOSATTRIB on file %s - %s\n", path, attrstr));
240 return False;
243 if (S_ISDIR(sbuf->st_mode)) {
244 dosattr |= aDIR;
246 *pattr = (uint32)(dosattr & SAMBA_ATTRIBUTES_MASK);
248 DEBUG(8,("get_ea_dos_attribute returning (0x%x)", dosattr));
250 if (dosattr & aHIDDEN) DEBUG(8, ("h"));
251 if (dosattr & aRONLY ) DEBUG(8, ("r"));
252 if (dosattr & aSYSTEM) DEBUG(8, ("s"));
253 if (dosattr & aDIR ) DEBUG(8, ("d"));
254 if (dosattr & aARCH ) DEBUG(8, ("a"));
256 DEBUG(8,("\n"));
258 return True;
261 /****************************************************************************
262 Set DOS attributes in an EA.
263 ****************************************************************************/
265 static BOOL set_ea_dos_attribute(connection_struct *conn, const char *path, SMB_STRUCT_STAT *sbuf, uint32 dosmode)
267 fstring attrstr;
268 files_struct *fsp = NULL;
269 BOOL ret = False;
271 if (!lp_store_dos_attributes(SNUM(conn))) {
272 return False;
275 snprintf(attrstr, sizeof(attrstr)-1, "0x%x", dosmode & SAMBA_ATTRIBUTES_MASK);
276 if (SMB_VFS_SETXATTR(conn, path, SAMBA_XATTR_DOS_ATTRIB, attrstr, strlen(attrstr), 0) == -1) {
277 if((errno != EPERM) && (errno != EACCES)) {
278 if (errno == ENOSYS
279 #if defined(ENOTSUP)
280 || errno == ENOTSUP) {
281 #else
283 #endif
284 set_store_dos_attributes(SNUM(conn), False);
286 return False;
289 /* We want DOS semantics, ie allow non owner with write permission to change the
290 bits on a file. Just like file_utime below.
293 /* Check if we have write access. */
294 if(!CAN_WRITE(conn) || !lp_dos_filemode(SNUM(conn)))
295 return False;
298 * We need to open the file with write access whilst
299 * still in our current user context. This ensures we
300 * are not violating security in doing the setxattr.
303 fsp = open_file_fchmod(conn,path,sbuf);
304 if (!fsp)
305 return ret;
306 become_root();
307 if (SMB_VFS_SETXATTR(conn, path, SAMBA_XATTR_DOS_ATTRIB, attrstr, strlen(attrstr), 0) == 0) {
308 ret = True;
310 unbecome_root();
311 close_file_fchmod(fsp);
312 return ret;
314 DEBUG(10,("set_ea_dos_attribute: set EA %s on file %s\n", attrstr, path));
315 return True;
318 /****************************************************************************
319 Change a unix mode to a dos mode for an ms dfs link.
320 ****************************************************************************/
322 uint32 dos_mode_msdfs(connection_struct *conn, const char *path,SMB_STRUCT_STAT *sbuf)
324 uint32 result = 0;
326 DEBUG(8,("dos_mode_msdfs: %s\n", path));
328 if (!VALID_STAT(*sbuf)) {
329 return 0;
332 /* First do any modifications that depend on the path name. */
333 /* hide files with a name starting with a . */
334 if (lp_hide_dot_files(SNUM(conn))) {
335 const char *p = strrchr_m(path,'/');
336 if (p) {
337 p++;
338 } else {
339 p = path;
342 if (p[0] == '.' && p[1] != '.' && p[1] != 0) {
343 result |= aHIDDEN;
347 result |= dos_mode_from_sbuf(conn, path, sbuf);
349 /* Optimization : Only call is_hidden_path if it's not already
350 hidden. */
351 if (!(result & aHIDDEN) && IS_HIDDEN_PATH(conn,path)) {
352 result |= aHIDDEN;
355 DEBUG(8,("dos_mode_msdfs returning "));
357 if (result & aHIDDEN) DEBUG(8, ("h"));
358 if (result & aRONLY ) DEBUG(8, ("r"));
359 if (result & aSYSTEM) DEBUG(8, ("s"));
360 if (result & aDIR ) DEBUG(8, ("d"));
361 if (result & aARCH ) DEBUG(8, ("a"));
362 if (result & FILE_ATTRIBUTE_SPARSE ) DEBUG(8, ("[sparse]"));
364 DEBUG(8,("\n"));
366 return(result);
369 /****************************************************************************
370 Change a unix mode to a dos mode.
371 ****************************************************************************/
373 uint32 dos_mode(connection_struct *conn, const char *path,SMB_STRUCT_STAT *sbuf)
375 uint32 result = 0;
377 DEBUG(8,("dos_mode: %s\n", path));
379 if (!VALID_STAT(*sbuf)) {
380 return 0;
383 /* First do any modifications that depend on the path name. */
384 /* hide files with a name starting with a . */
385 if (lp_hide_dot_files(SNUM(conn))) {
386 const char *p = strrchr_m(path,'/');
387 if (p) {
388 p++;
389 } else {
390 p = path;
393 if (p[0] == '.' && p[1] != '.' && p[1] != 0) {
394 result |= aHIDDEN;
398 /* Get the DOS attributes from an EA by preference. */
399 if (get_ea_dos_attribute(conn, path, sbuf, &result)) {
400 result |= set_sparse_flag(sbuf);
401 } else {
402 result |= dos_mode_from_sbuf(conn, path, sbuf);
405 if (S_ISREG(sbuf->st_mode)) {
406 result |= set_offline_flag(conn, path);
409 /* Optimization : Only call is_hidden_path if it's not already
410 hidden. */
411 if (!(result & aHIDDEN) && IS_HIDDEN_PATH(conn,path)) {
412 result |= aHIDDEN;
415 DEBUG(8,("dos_mode returning "));
417 if (result & aHIDDEN) DEBUG(8, ("h"));
418 if (result & aRONLY ) DEBUG(8, ("r"));
419 if (result & aSYSTEM) DEBUG(8, ("s"));
420 if (result & aDIR ) DEBUG(8, ("d"));
421 if (result & aARCH ) DEBUG(8, ("a"));
422 if (result & FILE_ATTRIBUTE_SPARSE ) DEBUG(8, ("[sparse]"));
424 DEBUG(8,("\n"));
426 return(result);
429 /*******************************************************************
430 chmod a file - but preserve some bits.
431 ********************************************************************/
433 int file_set_dosmode(connection_struct *conn, const char *fname, uint32 dosmode, SMB_STRUCT_STAT *st, BOOL creating_file)
435 SMB_STRUCT_STAT st1;
436 int mask=0;
437 mode_t tmp;
438 mode_t unixmode;
439 int ret = -1;
441 /* We only allow READONLY|HIDDEN|SYSTEM|DIRECTORY|ARCHIVE here. */
442 dosmode &= SAMBA_ATTRIBUTES_MASK;
444 DEBUG(10,("file_set_dosmode: setting dos mode 0x%x on file %s\n", dosmode, fname));
445 if (!st || (st && !VALID_STAT(*st))) {
446 st = &st1;
447 if (SMB_VFS_STAT(conn,fname,st))
448 return(-1);
451 get_acl_group_bits(conn, fname, &st->st_mode);
453 if (S_ISDIR(st->st_mode))
454 dosmode |= aDIR;
455 else
456 dosmode &= ~aDIR;
458 if (dos_mode(conn,fname,st) == dosmode)
459 return(0);
461 /* Store the DOS attributes in an EA by preference. */
462 if (set_ea_dos_attribute(conn, fname, st, dosmode)) {
463 return 0;
466 unixmode = unix_mode(conn,dosmode,fname, creating_file);
468 /* preserve the s bits */
469 mask |= (S_ISUID | S_ISGID);
471 /* preserve the t bit */
472 #ifdef S_ISVTX
473 mask |= S_ISVTX;
474 #endif
476 /* possibly preserve the x bits */
477 if (!MAP_ARCHIVE(conn))
478 mask |= S_IXUSR;
479 if (!MAP_SYSTEM(conn))
480 mask |= S_IXGRP;
481 if (!MAP_HIDDEN(conn))
482 mask |= S_IXOTH;
484 unixmode |= (st->st_mode & mask);
486 /* if we previously had any r bits set then leave them alone */
487 if ((tmp = st->st_mode & (S_IRUSR|S_IRGRP|S_IROTH))) {
488 unixmode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
489 unixmode |= tmp;
492 /* if we previously had any w bits set then leave them alone
493 whilst adding in the new w bits, if the new mode is not rdonly */
494 if (!IS_DOS_READONLY(dosmode)) {
495 unixmode |= (st->st_mode & (S_IWUSR|S_IWGRP|S_IWOTH));
498 if ((ret = SMB_VFS_CHMOD(conn,fname,unixmode)) == 0)
499 return 0;
501 if((errno != EPERM) && (errno != EACCES))
502 return -1;
504 if(!lp_dos_filemode(SNUM(conn)))
505 return -1;
507 /* We want DOS semantics, ie allow non owner with write permission to change the
508 bits on a file. Just like file_utime below.
511 /* Check if we have write access. */
512 if (CAN_WRITE(conn)) {
514 * We need to open the file with write access whilst
515 * still in our current user context. This ensures we
516 * are not violating security in doing the fchmod.
517 * This file open does *not* break any oplocks we are
518 * holding. We need to review this.... may need to
519 * break batch oplocks open by others. JRA.
521 files_struct *fsp = open_file_fchmod(conn,fname,st);
522 if (!fsp)
523 return -1;
524 become_root();
525 ret = SMB_VFS_FCHMOD(fsp, fsp->fh->fd, unixmode);
526 unbecome_root();
527 close_file_fchmod(fsp);
530 return( ret );
533 /*******************************************************************
534 Wrapper around dos_utime that possibly allows DOS semantics rather
535 than POSIX.
536 *******************************************************************/
538 int file_utime(connection_struct *conn, const char *fname, struct utimbuf *times)
540 SMB_STRUCT_STAT sbuf;
541 int ret = -1;
543 errno = 0;
544 ZERO_STRUCT(sbuf);
546 /* Don't update the time on read-only shares */
547 /* We need this as set_filetime (which can be called on
548 close and other paths) can end up calling this function
549 without the NEED_WRITE protection. Found by :
550 Leo Weppelman <leo@wau.mis.ah.nl>
553 if (!CAN_WRITE(conn)) {
554 return 0;
557 if(SMB_VFS_UTIME(conn,fname, times) == 0)
558 return 0;
560 if((errno != EPERM) && (errno != EACCES))
561 return -1;
563 if(!lp_dos_filetimes(SNUM(conn)))
564 return -1;
566 /* We have permission (given by the Samba admin) to
567 break POSIX semantics and allow a user to change
568 the time on a file they don't own but can write to
569 (as DOS does).
572 /* Check if we have write access. */
573 if (can_write_to_file(conn, fname, &sbuf)) {
574 /* We are allowed to become root and change the filetime. */
575 become_root();
576 ret = SMB_VFS_UTIME(conn,fname, times);
577 unbecome_root();
580 return ret;
583 /*******************************************************************
584 Change a filetime - possibly allowing DOS semantics.
585 *******************************************************************/
587 BOOL set_filetime(connection_struct *conn, const char *fname, time_t mtime)
589 struct utimbuf times;
591 if (null_mtime(mtime))
592 return(True);
594 times.modtime = times.actime = mtime;
596 if (file_utime(conn, fname, &times)) {
597 DEBUG(4,("set_filetime(%s) failed: %s\n",fname,strerror(errno)));
598 return False;
601 return(True);