me stupid.
[Samba.git] / source / smbd / dosmode.c
blob639e365d1bcd8516c7b7e40e0d545b5810310567
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 dos mode handling functions
5 Copyright (C) Andrew Tridgell 1992-1998
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 extern int DEBUGLEVEL;
26 /****************************************************************************
27 change a dos mode to a unix mode
28 base permission for files:
29 if inheriting
30 apply read/write bits from parent directory.
31 else
32 everybody gets read bit set
33 dos readonly is represented in unix by removing everyone's write bit
34 dos archive is represented in unix by the user's execute bit
35 dos system is represented in unix by the group's execute bit
36 dos hidden is represented in unix by the other's execute bit
37 if !inheriting {
38 Then apply create mask,
39 then add force bits.
41 base permission for directories:
42 dos directory is represented in unix by unix's dir bit and the exec bit
43 if !inheriting {
44 Then apply create mask,
45 then add force bits.
47 ****************************************************************************/
48 mode_t unix_mode(connection_struct *conn,int dosmode,const char *fname)
50 mode_t result = (S_IRUSR | S_IRGRP | S_IROTH);
51 mode_t dir_mode = 0; /* Mode of the parent directory if inheriting. */
53 if ( !IS_DOS_READONLY(dosmode) )
54 result |= (S_IWUSR | S_IWGRP | S_IWOTH);
56 if (fname && lp_inherit_perms(SNUM(conn))) {
57 char *dname;
58 SMB_STRUCT_STAT sbuf;
60 dname = parent_dirname(fname);
61 DEBUG(2,("unix_mode(%s) inheriting from %s\n",fname,dname));
62 if (vfs_stat(conn,dname,&sbuf) != 0) {
63 DEBUG(4,("unix_mode(%s) failed, [dir %s]: %s\n",fname,dname,strerror(errno)));
64 return(0); /* *** shouldn't happen! *** */
67 /* Save for later - but explicitly remove setuid bit for safety. */
68 dir_mode = sbuf.st_mode & ~S_ISUID;
69 DEBUG(2,("unix_mode(%s) inherit mode %o\n",fname,(int)dir_mode));
70 /* Clear "result" */
71 result = 0;
74 if (IS_DOS_DIR(dosmode)) {
75 /* We never make directories read only for the owner as under DOS a user
76 can always create a file in a read-only directory. */
77 result |= (S_IFDIR | S_IWUSR);
79 if (dir_mode) {
80 /* Inherit mode of parent directory. */
81 result |= dir_mode;
82 } else {
83 /* Provisionally add all 'x' bits */
84 result |= (S_IXUSR | S_IXGRP | S_IXOTH);
86 /* Apply directory mask */
87 result &= lp_dir_mask(SNUM(conn));
88 /* Add in force bits */
89 result |= lp_force_dir_mode(SNUM(conn));
91 } else {
92 if (lp_map_archive(SNUM(conn)) && IS_DOS_ARCHIVE(dosmode))
93 result |= S_IXUSR;
95 if (lp_map_system(SNUM(conn)) && IS_DOS_SYSTEM(dosmode))
96 result |= S_IXGRP;
98 if (lp_map_hidden(SNUM(conn)) && IS_DOS_HIDDEN(dosmode))
99 result |= S_IXOTH;
101 if (dir_mode) {
102 /* Inherit 666 component of parent directory mode */
103 result |= dir_mode
104 & (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
105 } else {
106 /* Apply mode mask */
107 result &= lp_create_mask(SNUM(conn));
108 /* Add in force bits */
109 result |= lp_force_create_mode(SNUM(conn));
113 DEBUG(3,("unix_mode(%s) returning 0%o\n",fname,(int)result ));
114 return(result);
118 /****************************************************************************
119 change a unix mode to a dos mode
120 ****************************************************************************/
121 int dos_mode(connection_struct *conn,char *path,SMB_STRUCT_STAT *sbuf)
123 int result = 0;
125 DEBUG(8,("dos_mode: %s\n", path));
127 if ((sbuf->st_mode & S_IWUSR) == 0)
128 result |= aRONLY;
130 if (MAP_ARCHIVE(conn) && ((sbuf->st_mode & S_IXUSR) != 0))
131 result |= aARCH;
133 if (MAP_SYSTEM(conn) && ((sbuf->st_mode & S_IXGRP) != 0))
134 result |= aSYSTEM;
136 if (MAP_HIDDEN(conn) && ((sbuf->st_mode & S_IXOTH) != 0))
137 result |= aHIDDEN;
139 if (S_ISDIR(sbuf->st_mode))
140 result = aDIR | (result & aRONLY);
142 #ifdef S_ISLNK
143 #if LINKS_READ_ONLY
144 if (S_ISLNK(sbuf->st_mode) && S_ISDIR(sbuf->st_mode))
145 result |= aRONLY;
146 #endif
147 #endif
149 /* hide files with a name starting with a . */
150 if (lp_hide_dot_files(SNUM(conn)))
152 char *p = strrchr_m(path,'/');
153 if (p)
154 p++;
155 else
156 p = path;
158 if (p[0] == '.' && p[1] != '.' && p[1] != 0)
159 result |= aHIDDEN;
162 /* Optimization : Only call is_hidden_path if it's not already
163 hidden. */
164 if (!(result & aHIDDEN) && IS_HIDDEN_PATH(conn,path))
166 result |= aHIDDEN;
169 DEBUG(8,("dos_mode returning "));
171 if (result & aHIDDEN) DEBUG(8, ("h"));
172 if (result & aRONLY ) DEBUG(8, ("r"));
173 if (result & aSYSTEM) DEBUG(8, ("s"));
174 if (result & aDIR ) DEBUG(8, ("d"));
175 if (result & aARCH ) DEBUG(8, ("a"));
177 DEBUG(8,("\n"));
179 return(result);
182 /*******************************************************************
183 chmod a file - but preserve some bits
184 ********************************************************************/
185 int file_chmod(connection_struct *conn,char *fname,int dosmode,SMB_STRUCT_STAT *st)
187 SMB_STRUCT_STAT st1;
188 int mask=0;
189 mode_t tmp;
190 mode_t unixmode;
191 int ret = -1;
193 if (!st) {
194 st = &st1;
195 if (vfs_stat(conn,fname,st))
196 return(-1);
199 if (S_ISDIR(st->st_mode))
200 dosmode |= aDIR;
202 if (dos_mode(conn,fname,st) == dosmode)
203 return(0);
205 unixmode = unix_mode(conn,dosmode,fname);
207 /* preserve the s bits */
208 mask |= (S_ISUID | S_ISGID);
210 /* preserve the t bit */
211 #ifdef S_ISVTX
212 mask |= S_ISVTX;
213 #endif
215 /* possibly preserve the x bits */
216 if (!MAP_ARCHIVE(conn))
217 mask |= S_IXUSR;
218 if (!MAP_SYSTEM(conn))
219 mask |= S_IXGRP;
220 if (!MAP_HIDDEN(conn))
221 mask |= S_IXOTH;
223 unixmode |= (st->st_mode & mask);
225 /* if we previously had any r bits set then leave them alone */
226 if ((tmp = st->st_mode & (S_IRUSR|S_IRGRP|S_IROTH))) {
227 unixmode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
228 unixmode |= tmp;
231 /* if we previously had any w bits set then leave them alone
232 whilst adding in the new w bits, if the new mode is not rdonly */
233 if (!IS_DOS_READONLY(dosmode)) {
234 unixmode |= (st->st_mode & (S_IWUSR|S_IWGRP|S_IWOTH));
237 if ((ret = vfs_chmod(conn,fname,unixmode)) == 0)
238 return 0;
240 if((errno != EPERM) && (errno != EACCES))
241 return -1;
243 if(!lp_dos_filemode(SNUM(conn)))
244 return -1;
246 /* We want DOS semantics, ie allow non owner with write permission to change the
247 bits on a file. Just like file_utime below.
250 /* Check if we have write access. */
251 if (CAN_WRITE(conn)) {
253 * We need to open the file with write access whilst
254 * still in our current user context. This ensures we
255 * are not violating security in doing the fchmod.
256 * This file open does *not* break any oplocks we are
257 * holding. We need to review this.... may need to
258 * break batch oplocks open by others. JRA.
260 files_struct *fsp = open_file_fchmod(conn,fname,st);
261 if (!fsp)
262 return -1;
263 become_root();
264 ret = conn->vfs_ops.fchmod(fsp, fsp->fd, unixmode);
265 unbecome_root();
266 close_file_fchmod(fsp);
269 return( ret );
273 /*******************************************************************
274 Wrapper around dos_utime that possibly allows DOS semantics rather
275 than POSIX.
276 *******************************************************************/
277 int file_utime(connection_struct *conn, char *fname, struct utimbuf *times)
279 extern struct current_user current_user;
280 SMB_STRUCT_STAT sb;
281 int ret = -1;
283 errno = 0;
285 if(conn->vfs_ops.utime(conn,fname, times) == 0)
286 return 0;
288 if((errno != EPERM) && (errno != EACCES))
289 return -1;
291 if(!lp_dos_filetimes(SNUM(conn)))
292 return -1;
294 /* We have permission (given by the Samba admin) to
295 break POSIX semantics and allow a user to change
296 the time on a file they don't own but can write to
297 (as DOS does).
300 if(vfs_stat(conn,fname,&sb) != 0)
301 return -1;
303 /* Check if we have write access. */
304 if (CAN_WRITE(conn)) {
305 if (((sb.st_mode & S_IWOTH) ||
306 conn->admin_user ||
307 ((sb.st_mode & S_IWUSR) && current_user.uid==sb.st_uid) ||
308 ((sb.st_mode & S_IWGRP) &&
309 in_group(sb.st_gid,current_user.gid,
310 current_user.ngroups,current_user.groups)))) {
311 /* We are allowed to become root and change the filetime. */
312 become_root();
313 ret = conn->vfs_ops.utime(conn,fname, times);
314 unbecome_root();
318 return ret;
321 /*******************************************************************
322 Change a filetime - possibly allowing DOS semantics.
323 *******************************************************************/
324 BOOL set_filetime(connection_struct *conn, char *fname, time_t mtime)
326 struct utimbuf times;
328 if (null_mtime(mtime)) return(True);
330 times.modtime = times.actime = mtime;
332 if (file_utime(conn, fname, &times)) {
333 DEBUG(4,("set_filetime(%s) failed: %s\n",fname,strerror(errno)));
334 return False;
337 return(True);