4 Unix SMB/Netbios implementation.
6 dos mode handling functions
7 Copyright (C) Andrew Tridgell 1992-1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 extern int DEBUGLEVEL;
28 /****************************************************************************
29 change a dos mode to a unix mode
30 base permission for files:
32 apply read/write bits from parent directory.
34 everybody gets read bit set
35 dos readonly is represented in unix by removing everyone's write bit
36 dos archive is represented in unix by the user's execute bit
37 dos system is represented in unix by the group's execute bit
38 dos hidden is represented in unix by the other's execute bit
40 Then apply create mask,
43 base permission for directories:
44 dos directory is represented in unix by unix's dir bit and the exec bit
46 Then apply create mask,
49 ****************************************************************************/
50 mode_t unix_mode(connection_struct *conn,int dosmode,const char *fname)
52 mode_t result = (S_IRUSR | S_IRGRP | S_IROTH);
53 mode_t dir_mode = 0; /* Mode of the parent directory if inheriting. */
55 if ( !IS_DOS_READONLY(dosmode) )
56 result |= (S_IWUSR | S_IWGRP | S_IWOTH);
58 if (fname && lp_inherit_perms(SNUM(conn))) {
62 dname = parent_dirname(fname);
63 DEBUG(2,("unix_mode(%s) inheriting from %s\n",fname,dname));
64 if (vfs_stat(conn,dname,&sbuf) != 0) {
65 DEBUG(4,("unix_mode(%s) failed, [dir %s]: %s\n",fname,dname,strerror(errno)));
66 return(0); /* *** shouldn't happen! *** */
69 /* Save for later - but explicitly remove setuid bit for safety. */
70 dir_mode = sbuf.st_mode & ~S_ISUID;
71 DEBUG(2,("unix_mode(%s) inherit mode %o\n",fname,(int)dir_mode));
76 if (IS_DOS_DIR(dosmode)) {
77 /* We never make directories read only for the owner as under DOS a user
78 can always create a file in a read-only directory. */
79 result |= (S_IFDIR | S_IWUSR);
82 /* Inherit mode of parent directory. */
85 /* Provisionally add all 'x' bits */
86 result |= (S_IXUSR | S_IXGRP | S_IXOTH);
88 /* Apply directory mask */
89 result &= lp_dir_mask(SNUM(conn));
90 /* Add in force bits */
91 result |= lp_force_dir_mode(SNUM(conn));
94 if (lp_map_archive(SNUM(conn)) && IS_DOS_ARCHIVE(dosmode))
97 if (lp_map_system(SNUM(conn)) && IS_DOS_SYSTEM(dosmode))
100 if (lp_map_hidden(SNUM(conn)) && IS_DOS_HIDDEN(dosmode))
104 /* Inherit 666 component of parent directory mode */
106 & (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
108 /* Apply mode mask */
109 result &= lp_create_mask(SNUM(conn));
110 /* Add in force bits */
111 result |= lp_force_create_mode(SNUM(conn));
115 DEBUG(3,("unix_mode(%s) returning 0%o\n",fname,(int)result ));
120 /****************************************************************************
121 change a unix mode to a dos mode
122 ****************************************************************************/
123 int dos_mode(connection_struct *conn,char *path,SMB_STRUCT_STAT *sbuf)
127 DEBUG(8,("dos_mode: %s\n", path));
129 if ((sbuf->st_mode & S_IWUSR) == 0)
132 if (MAP_ARCHIVE(conn) && ((sbuf->st_mode & S_IXUSR) != 0))
135 if (MAP_SYSTEM(conn) && ((sbuf->st_mode & S_IXGRP) != 0))
138 if (MAP_HIDDEN(conn) && ((sbuf->st_mode & S_IXOTH) != 0))
141 if (S_ISDIR(sbuf->st_mode))
142 result = aDIR | (result & aRONLY);
146 if (S_ISLNK(sbuf->st_mode) && S_ISDIR(sbuf->st_mode))
151 /* hide files with a name starting with a . */
152 if (lp_hide_dot_files(SNUM(conn)))
154 char *p = strrchr(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))
171 DEBUG(8,("dos_mode returning "));
173 if (result & aHIDDEN) DEBUG(8, ("h"));
174 if (result & aRONLY ) DEBUG(8, ("r"));
175 if (result & aSYSTEM) DEBUG(8, ("s"));
176 if (result & aDIR ) DEBUG(8, ("d"));
177 if (result & aARCH ) DEBUG(8, ("a"));
184 /*******************************************************************
185 chmod a file - but preserve some bits
186 ********************************************************************/
187 int file_chmod(connection_struct *conn,char *fname,int dosmode,SMB_STRUCT_STAT *st)
189 extern struct current_user current_user;
198 if (vfs_stat(conn,fname,st))
202 if (S_ISDIR(st->st_mode))
205 if (dos_mode(conn,fname,st) == dosmode)
208 unixmode = unix_mode(conn,dosmode,fname);
210 /* preserve the s bits */
211 mask |= (S_ISUID | S_ISGID);
213 /* preserve the t bit */
218 /* possibly preserve the x bits */
219 if (!MAP_ARCHIVE(conn))
221 if (!MAP_SYSTEM(conn))
223 if (!MAP_HIDDEN(conn))
226 unixmode |= (st->st_mode & mask);
228 /* if we previously had any r bits set then leave them alone */
229 if ((tmp = st->st_mode & (S_IRUSR|S_IRGRP|S_IROTH))) {
230 unixmode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
234 /* if we previously had any w bits set then leave them alone
235 whilst adding in the new w bits, if the new mode is not rdonly */
236 if (!IS_DOS_READONLY(dosmode)) {
237 unixmode |= (st->st_mode & (S_IWUSR|S_IWGRP|S_IWOTH));
240 if ((ret = vfs_chmod(conn,fname,unixmode)) == 0)
243 if((errno != EPERM) && (errno != EACCES))
246 if(!lp_dos_filemode(SNUM(conn)))
249 /* We want DOS semantics, ie allow non owner with write permission to change the
250 bits on a file. Just like file_utime below.
253 /* Check if we have write access. */
254 if (CAN_WRITE(conn)) {
255 if (((st->st_mode & S_IWOTH) ||
257 ((st->st_mode & S_IWUSR) && current_user.uid==st->st_uid) ||
258 ((st->st_mode & S_IWGRP) &&
259 in_group(st->st_gid,current_user.gid, current_user.ngroups,current_user.groups)))) {
260 /* We are allowed to become root and change the file mode. */
262 ret = vfs_chmod(conn,fname,unixmode);
271 /*******************************************************************
272 Wrapper around dos_utime that possibly allows DOS semantics rather
274 *******************************************************************/
275 int file_utime(connection_struct *conn, char *fname, struct utimbuf *times)
277 extern struct current_user current_user;
283 if(conn->vfs_ops.utime(conn,dos_to_unix(fname, False), times) == 0)
286 if((errno != EPERM) && (errno != EACCES))
289 if(!lp_dos_filetimes(SNUM(conn)))
292 /* We have permission (given by the Samba admin) to
293 break POSIX semantics and allow a user to change
294 the time on a file they don't own but can write to
298 if(vfs_stat(conn,fname,&sb) != 0)
301 /* Check if we have write access. */
302 if (CAN_WRITE(conn)) {
303 if (((sb.st_mode & S_IWOTH) ||
305 ((sb.st_mode & S_IWUSR) && current_user.uid==sb.st_uid) ||
306 ((sb.st_mode & S_IWGRP) &&
307 in_group(sb.st_gid,current_user.gid,
308 current_user.ngroups,current_user.groups)))) {
309 /* We are allowed to become root and change the filetime. */
311 ret = conn->vfs_ops.utime(conn,dos_to_unix(fname, False), times);
319 /*******************************************************************
320 Change a filetime - possibly allowing DOS semantics.
321 *******************************************************************/
322 BOOL set_filetime(connection_struct *conn, char *fname, time_t mtime)
324 struct utimbuf times;
326 if (null_mtime(mtime)) return(True);
328 times.modtime = times.actime = mtime;
330 if (file_utime(conn, fname, ×)) {
331 DEBUG(4,("set_filetime(%s) failed: %s\n",fname,strerror(errno)));