r6334: revert 3.0.15pre1 changes. roll back to 3.0.14.
[Samba.git] / source / smbd / open.c
blob0a3903234ddb822800bbe0f644e4fe020b666ce6
1 /*
2 Unix SMB/CIFS implementation.
3 file opening and share modes
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2004
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 userdom_struct current_user_info;
25 extern uint16 global_oplock_port;
26 extern uint16 global_smbpid;
27 extern BOOL global_client_failed_oplock_break;
29 struct dev_inode_bundle {
30 SMB_DEV_T dev;
31 SMB_INO_T inode;
34 /****************************************************************************
35 fd support routines - attempt to do a dos_open.
36 ****************************************************************************/
38 static int fd_open(struct connection_struct *conn, const char *fname,
39 int flags, mode_t mode)
41 int fd;
42 #ifdef O_NOFOLLOW
43 if (!lp_symlinks(SNUM(conn)))
44 flags |= O_NOFOLLOW;
45 #endif
47 fd = SMB_VFS_OPEN(conn,fname,flags,mode);
49 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n", fname,
50 flags, (int)mode, fd, (fd == -1) ? strerror(errno) : "" ));
52 return fd;
55 /****************************************************************************
56 Close the file associated with a fsp.
57 ****************************************************************************/
59 int fd_close(struct connection_struct *conn, files_struct *fsp)
61 if (fsp->fd == -1)
62 return 0; /* what we used to call a stat open. */
63 return fd_close_posix(conn, fsp);
67 /****************************************************************************
68 Check a filename for the pipe string.
69 ****************************************************************************/
71 static void check_for_pipe(const char *fname)
73 /* special case of pipe opens */
74 char s[10];
75 StrnCpy(s,fname,sizeof(s)-1);
76 strlower_m(s);
77 if (strstr(s,"pipe/")) {
78 DEBUG(3,("Rejecting named pipe open for %s\n",fname));
79 unix_ERR_class = ERRSRV;
80 unix_ERR_code = ERRaccess;
81 unix_ERR_ntstatus = NT_STATUS_ACCESS_DENIED;
85 /****************************************************************************
86 Open a file.
87 ****************************************************************************/
89 static BOOL open_file(files_struct *fsp,connection_struct *conn,
90 const char *fname,SMB_STRUCT_STAT *psbuf,int flags,mode_t mode, uint32 desired_access)
92 extern struct current_user current_user;
93 int accmode = (flags & O_ACCMODE);
94 int local_flags = flags;
96 fsp->fd = -1;
97 fsp->oplock_type = NO_OPLOCK;
98 errno = EPERM;
100 /* Check permissions */
103 * This code was changed after seeing a client open request
104 * containing the open mode of (DENY_WRITE/read-only) with
105 * the 'create if not exist' bit set. The previous code
106 * would fail to open the file read only on a read-only share
107 * as it was checking the flags parameter directly against O_RDONLY,
108 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
109 * JRA.
112 if (!CAN_WRITE(conn)) {
113 /* It's a read-only share - fail if we wanted to write. */
114 if(accmode != O_RDONLY) {
115 DEBUG(3,("Permission denied opening %s\n",fname));
116 check_for_pipe(fname);
117 return False;
118 } else if(flags & O_CREAT) {
119 /* We don't want to write - but we must make sure that O_CREAT
120 doesn't create the file if we have write access into the
121 directory.
123 flags &= ~O_CREAT;
124 local_flags &= ~O_CREAT;
129 * This little piece of insanity is inspired by the
130 * fact that an NT client can open a file for O_RDONLY,
131 * but set the create disposition to FILE_EXISTS_TRUNCATE.
132 * If the client *can* write to the file, then it expects to
133 * truncate the file, even though it is opening for readonly.
134 * Quicken uses this stupid trick in backup file creation...
135 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
136 * for helping track this one down. It didn't bite us in 2.0.x
137 * as we always opened files read-write in that release. JRA.
140 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
141 DEBUG(10,("open_file: truncate requested on read-only open for file %s\n",fname ));
142 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
145 if ((desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
146 (local_flags & O_CREAT) || ((local_flags & O_TRUNC) == O_TRUNC) ) {
149 * We can't actually truncate here as the file may be locked.
150 * open_file_shared will take care of the truncate later. JRA.
153 local_flags &= ~O_TRUNC;
155 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
157 * We would block on opening a FIFO with no one else on the
158 * other end. Do what we used to do and add O_NONBLOCK to the
159 * open flags. JRA.
162 if (VALID_STAT(*psbuf) && S_ISFIFO(psbuf->st_mode))
163 local_flags |= O_NONBLOCK;
164 #endif
166 /* Don't create files with Microsoft wildcard characters. */
167 if ((local_flags & O_CREAT) && !VALID_STAT(*psbuf) && ms_has_wild(fname)) {
168 unix_ERR_class = ERRDOS;
169 unix_ERR_code = ERRinvalidname;
170 unix_ERR_ntstatus = NT_STATUS_OBJECT_NAME_INVALID;
171 return False;
174 /* Actually do the open */
175 fsp->fd = fd_open(conn, fname, local_flags, mode);
176 if (fsp->fd == -1) {
177 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) (flags=%d)\n",
178 fname,strerror(errno),local_flags,flags));
179 check_for_pipe(fname);
180 return False;
183 /* Inherit the ACL if the file was created. */
184 if ((local_flags & O_CREAT) && !VALID_STAT(*psbuf))
185 inherit_access_acl(conn, fname, mode);
187 } else
188 fsp->fd = -1; /* What we used to call a stat open. */
190 if (!VALID_STAT(*psbuf)) {
191 int ret;
193 if (fsp->fd == -1)
194 ret = SMB_VFS_STAT(conn, fname, psbuf);
195 else {
196 ret = SMB_VFS_FSTAT(fsp,fsp->fd,psbuf);
197 /* If we have an fd, this stat should succeed. */
198 if (ret == -1)
199 DEBUG(0,("Error doing fstat on open file %s (%s)\n", fname,strerror(errno) ));
202 /* For a non-io open, this stat failing means file not found. JRA */
203 if (ret == -1) {
204 fd_close(conn, fsp);
205 return False;
210 * POSIX allows read-only opens of directories. We don't
211 * want to do this (we use a different code path for this)
212 * so catch a directory open and return an EISDIR. JRA.
215 if(S_ISDIR(psbuf->st_mode)) {
216 fd_close(conn, fsp);
217 errno = EISDIR;
218 return False;
221 fsp->mode = psbuf->st_mode;
222 fsp->inode = psbuf->st_ino;
223 fsp->dev = psbuf->st_dev;
224 fsp->vuid = current_user.vuid;
225 fsp->file_pid = global_smbpid;
226 fsp->size = psbuf->st_size;
227 fsp->can_lock = True;
228 fsp->can_read = ((flags & O_WRONLY)==0);
229 fsp->can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
230 fsp->share_mode = 0;
231 fsp->desired_access = desired_access;
232 fsp->print_file = False;
233 fsp->modified = False;
234 fsp->oplock_type = NO_OPLOCK;
235 fsp->sent_oplock_break = NO_BREAK_SENT;
236 fsp->is_directory = False;
237 fsp->is_stat = False;
238 fsp->directory_delete_on_close = False;
239 string_set(&fsp->fsp_name,fname);
240 fsp->wcp = NULL; /* Write cache pointer. */
242 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
243 *current_user_info.smb_name ? current_user_info.smb_name : conn->user,fsp->fsp_name,
244 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
245 conn->num_files_open + 1));
247 errno = 0;
248 return True;
251 /*******************************************************************
252 Return True if the filename is one of the special executable types.
253 ********************************************************************/
255 static BOOL is_executable(const char *fname)
257 if ((fname = strrchr_m(fname,'.'))) {
258 if (strequal(fname,".com") ||
259 strequal(fname,".dll") ||
260 strequal(fname,".exe") ||
261 strequal(fname,".sym")) {
262 return True;
265 return False;
268 enum {AFAIL,AREAD,AWRITE,AALL};
270 /*******************************************************************
271 Reproduce the share mode access table.
272 This is horrendoously complex, and really can't be justified on any
273 rational grounds except that this is _exactly_ what NT does. See
274 the DENY1 and DENY2 tests in smbtorture for a comprehensive set of
275 test routines.
276 ********************************************************************/
278 static int access_table(int new_deny,int old_deny,int old_mode,
279 BOOL same_pid, BOOL isexe)
281 if (new_deny == DENY_ALL || old_deny == DENY_ALL) return(AFAIL);
283 if (same_pid) {
284 if (isexe && old_mode == DOS_OPEN_RDONLY &&
285 old_deny == DENY_DOS && new_deny == DENY_READ) {
286 return AFAIL;
288 if (!isexe && old_mode == DOS_OPEN_RDONLY &&
289 old_deny == DENY_DOS && new_deny == DENY_DOS) {
290 return AREAD;
292 if (new_deny == DENY_FCB && old_deny == DENY_DOS) {
293 if (isexe) return AFAIL;
294 if (old_mode == DOS_OPEN_RDONLY) return AFAIL;
295 return AALL;
297 if (old_mode == DOS_OPEN_RDONLY && old_deny == DENY_DOS) {
298 if (new_deny == DENY_FCB || new_deny == DENY_READ) {
299 if (isexe) return AREAD;
300 return AFAIL;
303 if (old_deny == DENY_FCB) {
304 if (new_deny == DENY_DOS || new_deny == DENY_FCB) return AALL;
305 return AFAIL;
309 if (old_deny == DENY_DOS || new_deny == DENY_DOS ||
310 old_deny == DENY_FCB || new_deny == DENY_FCB) {
311 if (isexe) {
312 if (old_deny == DENY_FCB || new_deny == DENY_FCB) {
313 return AFAIL;
315 if (old_deny == DENY_DOS) {
316 if (new_deny == DENY_READ &&
317 (old_mode == DOS_OPEN_RDONLY ||
318 old_mode == DOS_OPEN_RDWR)) {
319 return AFAIL;
321 if (new_deny == DENY_WRITE &&
322 (old_mode == DOS_OPEN_WRONLY ||
323 old_mode == DOS_OPEN_RDWR)) {
324 return AFAIL;
326 return AALL;
328 if (old_deny == DENY_NONE) return AALL;
329 if (old_deny == DENY_READ) return AWRITE;
330 if (old_deny == DENY_WRITE) return AREAD;
332 /* it isn't a exe, dll, sym or com file */
333 if (old_deny == new_deny && same_pid)
334 return(AALL);
336 if (old_deny == DENY_READ || new_deny == DENY_READ) return AFAIL;
337 if (old_mode == DOS_OPEN_RDONLY) return(AREAD);
339 return(AFAIL);
342 switch (new_deny)
344 case DENY_WRITE:
345 if (old_deny==DENY_WRITE && old_mode==DOS_OPEN_RDONLY) return(AREAD);
346 if (old_deny==DENY_READ && old_mode==DOS_OPEN_RDONLY) return(AWRITE);
347 if (old_deny==DENY_NONE && old_mode==DOS_OPEN_RDONLY) return(AALL);
348 return(AFAIL);
349 case DENY_READ:
350 if (old_deny==DENY_WRITE && old_mode==DOS_OPEN_WRONLY) return(AREAD);
351 if (old_deny==DENY_READ && old_mode==DOS_OPEN_WRONLY) return(AWRITE);
352 if (old_deny==DENY_NONE && old_mode==DOS_OPEN_WRONLY) return(AALL);
353 return(AFAIL);
354 case DENY_NONE:
355 if (old_deny==DENY_WRITE) return(AREAD);
356 if (old_deny==DENY_READ) return(AWRITE);
357 if (old_deny==DENY_NONE) return(AALL);
358 return(AFAIL);
360 return(AFAIL);
363 /****************************************************************************
364 Check if we can open a file with a share mode.
365 ****************************************************************************/
367 static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, int share_mode, uint32 desired_access,
368 const char *fname, BOOL fcbopen, int *flags)
370 int deny_mode = GET_DENY_MODE(share_mode);
371 int old_open_mode = GET_OPEN_MODE(share->share_mode);
372 int old_deny_mode = GET_DENY_MODE(share->share_mode);
373 BOOL non_io_open_request;
374 BOOL non_io_open_existing;
377 * share modes = false means don't bother to check for
378 * DENY mode conflict. This is a *really* bad idea :-). JRA.
381 if(!lp_share_modes(SNUM(conn)))
382 return True;
384 if (desired_access & ~(SYNCHRONIZE_ACCESS|READ_CONTROL_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) {
385 non_io_open_request = False;
386 } else {
387 non_io_open_request = True;
390 if (share->desired_access & ~(SYNCHRONIZE_ACCESS|READ_CONTROL_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) {
391 non_io_open_existing = False;
392 } else {
393 non_io_open_existing = True;
397 * Don't allow any opens once the delete on close flag has been
398 * set.
401 if (GET_DELETE_ON_CLOSE_FLAG(share->share_mode)) {
402 DEBUG(5,("check_share_mode: Failing open on file %s as delete on close flag is set.\n",
403 fname ));
404 /* Use errno to map to correct error. */
405 unix_ERR_class = SMB_SUCCESS;
406 unix_ERR_code = 0;
407 unix_ERR_ntstatus = NT_STATUS_OK;
408 return False;
411 /* this is a nasty hack, but necessary until we rewrite our open
412 handling to use a NTCreateX call as the basic call.
413 NT may open a file with neither read nor write access, and in
414 this case it expects the open not to conflict with any
415 existing deny modes. This happens (for example) during a
416 "xcopy /o" where the second file descriptor is used for
417 ACL sets
418 (tridge)
422 * This is a bit wierd - the test for desired access not having the
423 * critical bits seems seems odd. Firstly, if both opens have no
424 * critical bits then always ignore. Then check the "allow delete"
425 * then check for either. This probably isn't quite right yet but
426 * gets us much closer. JRA.
430 * If desired_access doesn't contain READ_DATA,WRITE_DATA,APPEND_DATA or EXECUTE
431 * and the existing desired_acces then share modes don't conflict.
434 if (non_io_open_request && non_io_open_existing) {
437 * Wrinkle discovered by smbtorture....
438 * If both are non-io open and requester is asking for delete and current open has delete access
439 * but neither open has allowed file share delete then deny.... this is very strange and
440 * seems to be the only case in which non-io opens conflict. JRA.
443 if ((desired_access & DELETE_ACCESS) && (share->desired_access & DELETE_ACCESS) &&
444 (!GET_ALLOW_SHARE_DELETE(share->share_mode) || !GET_ALLOW_SHARE_DELETE(share_mode))) {
445 DEBUG(5,("check_share_mode: Failing open on file %s as delete access requests conflict.\n",
446 fname ));
447 unix_ERR_class = ERRDOS;
448 unix_ERR_code = ERRbadshare;
449 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
451 return False;
454 DEBUG(5,("check_share_mode: Allowing open on file %s as both desired access (0x%x) \
455 and existing desired access (0x%x) are non-data opens\n",
456 fname, (unsigned int)desired_access, (unsigned int)share->desired_access ));
457 return True;
458 } else if (non_io_open_request || non_io_open_existing) {
460 * If either are non-io opens then share modes don't conflict.
462 DEBUG(5,("check_share_mode: One non-io open. Allowing open on file %s as desired access (0x%x) doesn't conflict with\
463 existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsigned int)share->desired_access ));
464 return True;
468 * If delete access was requested and the existing share mode doesn't have
469 * ALLOW_SHARE_DELETE then deny.
472 if ((desired_access & DELETE_ACCESS) && !GET_ALLOW_SHARE_DELETE(share->share_mode)) {
473 DEBUG(5,("check_share_mode: Failing open on file %s as delete access requested and allow share delete not set.\n",
474 fname ));
475 unix_ERR_class = ERRDOS;
476 unix_ERR_code = ERRbadshare;
477 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
479 return False;
483 * The inverse of the above.
484 * If delete access was granted and the new share mode doesn't have
485 * ALLOW_SHARE_DELETE then deny.
488 if ((share->desired_access & DELETE_ACCESS) && !GET_ALLOW_SHARE_DELETE(share_mode)) {
489 DEBUG(5,("check_share_mode: Failing open on file %s as delete access granted and allow share delete not requested.\n",
490 fname ));
491 unix_ERR_class = ERRDOS;
492 unix_ERR_code = ERRbadshare;
493 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
494 return False;
497 #if 0
498 /* Bluarc test may need this ... needs further investigation. */
499 if (deny_mode == DENY_ALL || old_deny_mode == DENY_ALL) {
500 unix_ERR_class = ERRDOS;
501 unix_ERR_code = ERRbadshare;
502 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
503 return False;
505 #endif
508 * If desired_access doesn't contain READ_DATA,WRITE_DATA,APPEND_DATA or EXECUTE
509 * then share modes don't conflict. Likewise with existing desired access.
512 if ( !(desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
513 !(share->desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ) {
514 DEBUG(5,("check_share_mode: Allowing open on file %s as desired access (0x%x) doesn't conflict with \
515 existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsigned int)share->desired_access ));
516 return True;
520 int access_allowed = access_table(deny_mode,old_deny_mode,old_open_mode,
521 (share->pid == sys_getpid()),is_executable(fname));
523 if ((access_allowed == AFAIL) ||
524 (!fcbopen && (access_allowed == AREAD && *flags == O_RDWR)) ||
525 (access_allowed == AREAD && *flags != O_RDONLY) ||
526 (access_allowed == AWRITE && *flags != O_WRONLY)) {
528 DEBUG(2,("Share violation on file (%d,%d,%d,%d,%s,fcbopen = %d, flags = %d) = %d\n",
529 deny_mode,old_deny_mode,old_open_mode,
530 (int)share->pid,fname, fcbopen, *flags, access_allowed));
532 unix_ERR_class = ERRDOS;
533 unix_ERR_code = ERRbadshare;
534 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
536 return False;
539 if (access_allowed == AREAD)
540 *flags = O_RDONLY;
542 if (access_allowed == AWRITE)
543 *flags = O_WRONLY;
547 return True;
551 #if defined(DEVELOPER)
552 static void validate_my_share_entries(int num, share_mode_entry *share_entry)
554 files_struct *fsp;
556 if (share_entry->pid != sys_getpid())
557 return;
559 fsp = file_find_dif(share_entry->dev, share_entry->inode, share_entry->share_file_id);
560 if (!fsp) {
561 DEBUG(0,("validate_my_share_entries: PANIC : %s\n", share_mode_str(num, share_entry) ));
562 smb_panic("validate_my_share_entries: Cannot match a share entry with an open file\n");
565 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
566 pstring str;
567 DEBUG(0,("validate_my_share_entries: PANIC : %s\n", share_mode_str(num, share_entry) ));
568 slprintf(str, sizeof(str)-1, "validate_my_share_entries: file %s, oplock_type = 0x%x, op_type = 0x%x\n",
569 fsp->fsp_name, (unsigned int)fsp->oplock_type, (unsigned int)share_entry->op_type );
570 smb_panic(str);
573 #endif
575 struct share_mode_entry_list {
576 struct share_mode_entry_list *next, *prev;
577 share_mode_entry entry;
580 static void free_broken_entry_list(struct share_mode_entry_list *broken_entry_list)
582 while (broken_entry_list) {
583 struct share_mode_entry_list *broken_entry = broken_entry_list;
584 DLIST_REMOVE(broken_entry_list, broken_entry);
585 SAFE_FREE(broken_entry);
589 /****************************************************************************
590 Deal with open deny mode and oplock break processing.
591 Invarient: Share mode must be locked on entry and exit.
592 Returns -1 on error, or number of share modes on success (may be zero).
593 ****************************************************************************/
595 static int open_mode_check(connection_struct *conn, const char *fname, SMB_DEV_T dev,
596 SMB_INO_T inode,
597 uint32 desired_access,
598 int share_mode, int *p_flags, int *p_oplock_request,
599 BOOL *p_all_current_opens_are_level_II)
601 int i;
602 int num_share_modes;
603 int oplock_contention_count = 0;
604 share_mode_entry *old_shares = NULL;
605 BOOL fcbopen = False;
606 BOOL broke_oplock;
608 if(GET_OPEN_MODE(share_mode) == DOS_OPEN_FCB)
609 fcbopen = True;
611 num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
613 if(num_share_modes == 0) {
614 SAFE_FREE(old_shares);
615 return 0;
618 if (desired_access && ((desired_access & ~(SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES))==0) &&
619 ((desired_access & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) != 0)) {
620 /* Stat open that doesn't trigger oplock breaks or share mode checks... ! JRA. */
621 SAFE_FREE(old_shares);
622 return num_share_modes;
626 * Check if the share modes will give us access.
629 do {
630 struct share_mode_entry_list *broken_entry_list = NULL;
631 struct share_mode_entry_list *broken_entry = NULL;
633 broke_oplock = False;
634 *p_all_current_opens_are_level_II = True;
636 for(i = 0; i < num_share_modes; i++) {
637 BOOL cause_oplock_break = False;
638 share_mode_entry *share_entry = &old_shares[i];
640 #if defined(DEVELOPER)
641 validate_my_share_entries(i, share_entry);
642 #endif
645 * By observation of NetBench, oplocks are broken *before* share
646 * modes are checked. This allows a file to be closed by the client
647 * if the share mode would deny access and the client has an oplock.
648 * Check if someone has an oplock on this file. If so we must break
649 * it before continuing.
652 /* Was this a delete this file request ? */
653 if (!*p_oplock_request && desired_access == DELETE_ACCESS &&
654 !BATCH_OPLOCK_TYPE(share_entry->op_type)) {
655 /* Don't break the oplock in this case. */
656 cause_oplock_break = False;
657 } else if((*p_oplock_request && EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) ||
658 (!*p_oplock_request && (share_entry->op_type != NO_OPLOCK))) {
659 cause_oplock_break = True;
662 if(cause_oplock_break) {
663 BOOL opb_ret;
665 DEBUG(5,("open_mode_check: oplock_request = %d, breaking oplock (%x) on file %s, \
666 dev = %x, inode = %.0f\n", *p_oplock_request, share_entry->op_type, fname, (unsigned int)dev, (double)inode));
668 /* Ensure the reply for the open uses the correct sequence number. */
669 /* This isn't a real deferred packet as it's response will also increment
670 * the sequence.
672 srv_defer_sign_response(get_current_mid());
674 /* Oplock break - unlock to request it. */
675 unlock_share_entry(conn, dev, inode);
677 opb_ret = request_oplock_break(share_entry);
679 /* Now relock. */
680 lock_share_entry(conn, dev, inode);
682 if(opb_ret == False) {
683 DEBUG(0,("open_mode_check: FAILED when breaking oplock (%x) on file %s, \
684 dev = %x, inode = %.0f\n", old_shares[i].op_type, fname, (unsigned int)dev, (double)inode));
685 SAFE_FREE(old_shares);
686 errno = EACCES;
687 unix_ERR_class = ERRDOS;
688 unix_ERR_code = ERRbadshare;
689 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
690 return -1;
693 broken_entry = SMB_MALLOC_P(struct share_mode_entry_list);
694 if (!broken_entry) {
695 smb_panic("open_mode_check: malloc fail.\n");
697 broken_entry->entry = *share_entry;
698 DLIST_ADD(broken_entry_list, broken_entry);
699 broke_oplock = True;
701 } else if (!LEVEL_II_OPLOCK_TYPE(share_entry->op_type)) {
702 *p_all_current_opens_are_level_II = False;
704 } /* end for */
706 if (broke_oplock) {
707 /* Update the current open table. */
708 SAFE_FREE(old_shares);
709 num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
712 /* Now we check the share modes, after any oplock breaks. */
713 for(i = 0; i < num_share_modes; i++) {
714 share_mode_entry *share_entry = &old_shares[i];
716 /* someone else has a share lock on it, check to see if we can too */
717 if (!check_share_mode(conn, share_entry, share_mode, desired_access,
718 fname, fcbopen, p_flags)) {
719 SAFE_FREE(old_shares);
720 free_broken_entry_list(broken_entry_list);
721 errno = EACCES;
722 return -1;
726 for(broken_entry = broken_entry_list; broken_entry; broken_entry = broken_entry->next) {
727 oplock_contention_count++;
729 /* Paranoia check that this is no longer an exlusive entry. */
730 for(i = 0; i < num_share_modes; i++) {
731 share_mode_entry *share_entry = &old_shares[i];
733 if (share_modes_identical(&broken_entry->entry, share_entry) &&
734 EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type) ) {
737 * This should not happen. The target left this oplock
738 * as exlusive.... The process *must* be dead....
741 DEBUG(0,("open_mode_check: exlusive oplock left by process %d \
742 after break ! For file %s, dev = %x, inode = %.0f. Deleting it to continue...\n",
743 (int)broken_entry->entry.pid, fname, (unsigned int)dev, (double)inode));
745 if (process_exists(broken_entry->entry.pid)) {
746 DEBUG(0,("open_mode_check: Existent process %lu left active oplock.\n",
747 (unsigned long)broken_entry->entry.pid ));
750 if (del_share_entry(dev, inode, &broken_entry->entry, NULL) == -1) {
751 free_broken_entry_list(broken_entry_list);
752 errno = EACCES;
753 unix_ERR_class = ERRDOS;
754 unix_ERR_code = ERRbadshare;
755 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
756 return -1;
760 * We must reload the share modes after deleting the
761 * other process's entry.
764 SAFE_FREE(old_shares);
765 num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
766 break;
768 } /* end for paranoia... */
769 } /* end for broken_entry */
770 free_broken_entry_list(broken_entry_list);
771 } while(broke_oplock);
774 * Refuse to grant an oplock in case the contention limit is
775 * reached when going through the lock list multiple times.
778 if(oplock_contention_count >= lp_oplock_contention_limit(SNUM(conn))) {
779 *p_oplock_request = 0;
780 DEBUG(4,("open_mode_check: oplock contention = %d. Not granting oplock.\n",
781 oplock_contention_count ));
784 SAFE_FREE(old_shares);
785 return num_share_modes;
788 /****************************************************************************
789 Delete the record for a handled deferred open entry.
790 ****************************************************************************/
792 static void delete_defered_open_entry_record(connection_struct *conn, SMB_DEV_T dev, SMB_INO_T inode)
794 uint16 mid = get_current_mid();
795 pid_t mypid = sys_getpid();
796 deferred_open_entry *de_array = NULL;
797 int num_de_entries, i;
799 if (!lp_defer_sharing_violations()) {
800 return;
803 num_de_entries = get_deferred_opens(conn, dev, inode, &de_array);
804 for (i = 0; i < num_de_entries; i++) {
805 deferred_open_entry *entry = &de_array[i];
806 if (entry->pid == mypid && entry->mid == mid && entry->dev == dev &&
807 entry->inode == inode) {
809 /* Remove the deferred open entry from the array. */
810 delete_deferred_open_entry(entry);
811 SAFE_FREE(de_array);
812 return;
815 SAFE_FREE(de_array);
818 /****************************************************************************
819 Handle the 1 second delay in returning a SHARING_VIOLATION error.
820 ****************************************************************************/
822 void defer_open_sharing_error(connection_struct *conn, struct timeval *ptv,
823 char *fname, SMB_DEV_T dev, SMB_INO_T inode)
825 uint16 mid = get_current_mid();
826 pid_t mypid = sys_getpid();
827 deferred_open_entry *de_array = NULL;
828 int num_de_entries, i;
829 struct dev_inode_bundle dib;
831 if (!lp_defer_sharing_violations()) {
832 return;
835 dib.dev = dev;
836 dib.inode = inode;
838 num_de_entries = get_deferred_opens(conn, dev, inode, &de_array);
839 for (i = 0; i < num_de_entries; i++) {
840 deferred_open_entry *entry = &de_array[i];
841 if (entry->pid == mypid && entry->mid == mid) {
843 * Check if a 1 second timeout has expired.
845 if (usec_time_diff(ptv, &entry->time) > SHARING_VIOLATION_USEC_WAIT) {
846 DEBUG(10,("defer_open_sharing_error: Deleting deferred open entry for mid %u, \
847 file %s\n",
848 (unsigned int)mid, fname ));
850 /* Expired, return a real error. */
851 /* Remove the deferred open entry from the array. */
853 delete_deferred_open_entry(entry);
854 SAFE_FREE(de_array);
855 return;
858 * If the timeout hasn't expired yet and we still have a sharing violation,
859 * just leave the entry in the deferred open array alone. We do need to
860 * reschedule this open call though (with the original created time).
862 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] updating \
863 deferred open entry for mid %u, file %s\n",
864 (unsigned int)entry->time.tv_sec,
865 (unsigned int)entry->time.tv_usec,
866 (unsigned int)mid, fname ));
868 push_sharing_violation_open_smb_message(&entry->time, (char *)&dib, sizeof(dib));
869 SAFE_FREE(de_array);
870 return;
874 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred open entry for mid %u, file %s\n",
875 (unsigned int)ptv->tv_sec, (unsigned int)ptv->tv_usec, (unsigned int)mid, fname ));
877 if (!push_sharing_violation_open_smb_message(ptv, (char *)&dib, sizeof(dib))) {
878 SAFE_FREE(de_array);
879 return;
881 if (!add_deferred_open(mid, ptv, dev, inode, global_oplock_port, fname)) {
882 remove_sharing_violation_open_smb_message(mid);
886 * Push the MID of this packet on the signing queue.
887 * We only do this once, the first time we push the packet
888 * onto the deferred open queue, as this has a side effect
889 * of incrementing the response sequence number.
892 srv_defer_sign_response(mid);
894 SAFE_FREE(de_array);
897 /****************************************************************************
898 Set a kernel flock on a file for NFS interoperability.
899 This requires a patch to Linux.
900 ****************************************************************************/
902 static void kernel_flock(files_struct *fsp, int deny_mode)
904 #if HAVE_KERNEL_SHARE_MODES
905 int kernel_mode = 0;
906 if (deny_mode == DENY_READ) kernel_mode = LOCK_MAND|LOCK_WRITE;
907 else if (deny_mode == DENY_WRITE) kernel_mode = LOCK_MAND|LOCK_READ;
908 else if (deny_mode == DENY_ALL) kernel_mode = LOCK_MAND;
909 if (kernel_mode) flock(fsp->fd, kernel_mode);
910 #endif
915 static BOOL open_match_attributes(connection_struct *conn, const char *path, uint32 old_dos_mode, uint32 new_dos_mode,
916 mode_t existing_mode, mode_t new_mode, mode_t *returned_mode)
918 uint32 noarch_old_dos_mode, noarch_new_dos_mode;
920 noarch_old_dos_mode = (old_dos_mode & ~FILE_ATTRIBUTE_ARCHIVE);
921 noarch_new_dos_mode = (new_dos_mode & ~FILE_ATTRIBUTE_ARCHIVE);
923 if((noarch_old_dos_mode == 0 && noarch_new_dos_mode != 0) ||
924 (noarch_old_dos_mode != 0 && ((noarch_old_dos_mode & noarch_new_dos_mode) == noarch_old_dos_mode)))
925 *returned_mode = new_mode;
926 else
927 *returned_mode = (mode_t)0;
929 DEBUG(10,("open_match_attributes: file %s old_dos_mode = 0x%x, existing_mode = 0%o, new_dos_mode = 0x%x returned_mode = 0%o\n",
930 path,
931 old_dos_mode, (unsigned int)existing_mode, new_dos_mode, (unsigned int)*returned_mode ));
933 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
934 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
935 if ((old_dos_mode & FILE_ATTRIBUTE_SYSTEM) && !(new_dos_mode & FILE_ATTRIBUTE_SYSTEM))
936 return False;
938 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
939 if ((old_dos_mode & FILE_ATTRIBUTE_HIDDEN) && !(new_dos_mode & FILE_ATTRIBUTE_HIDDEN))
940 return False;
942 return True;
945 /****************************************************************************
946 Open a file with a share mode.
947 ****************************************************************************/
949 files_struct *open_file_shared(connection_struct *conn,char *fname, SMB_STRUCT_STAT *psbuf,
950 int share_mode,int ofun, uint32 new_dos_mode, int oplock_request,
951 int *Access,int *action)
953 return open_file_shared1(conn, fname, psbuf, 0, share_mode, ofun, new_dos_mode,
954 oplock_request, Access, action);
957 /****************************************************************************
958 Open a file with a share mode.
959 ****************************************************************************/
961 files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_STAT *psbuf,
962 uint32 desired_access,
963 int share_mode,int ofun, uint32 new_dos_mode,
964 int oplock_request,
965 int *Access,int *paction)
967 int flags=0;
968 int flags2=0;
969 int deny_mode = GET_DENY_MODE(share_mode);
970 BOOL allow_share_delete = GET_ALLOW_SHARE_DELETE(share_mode);
971 BOOL delete_on_close = GET_DELETE_ON_CLOSE_FLAG(share_mode);
972 BOOL file_existed = VALID_STAT(*psbuf);
973 BOOL fcbopen = False;
974 BOOL def_acl = False;
975 BOOL add_share_mode = True;
976 BOOL internal_only_open = False;
977 SMB_DEV_T dev = 0;
978 SMB_INO_T inode = 0;
979 int num_share_modes = 0;
980 BOOL all_current_opens_are_level_II = False;
981 BOOL fsp_open = False;
982 files_struct *fsp = NULL;
983 int open_mode=0;
984 uint16 port = 0;
985 mode_t new_mode = (mode_t)0;
986 int action;
987 uint32 existing_dos_mode = 0;
988 struct pending_message_list *pml = NULL;
989 uint16 mid = get_current_mid();
990 /* We add aARCH to this as this mode is only used if the file is created new. */
991 mode_t mode = unix_mode(conn,new_dos_mode | aARCH,fname, True);
993 if (oplock_request == INTERNAL_OPEN_ONLY) {
994 internal_only_open = True;
995 oplock_request = 0;
998 if ((pml = get_open_deferred_message(mid)) != NULL) {
999 struct dev_inode_bundle dib;
1001 memcpy(&dib, pml->private_data.data, sizeof(dib));
1003 /* There could be a race condition where the dev/inode pair
1004 has changed since we deferred the message. If so, just
1005 remove the deferred open entry and return sharing violation. */
1007 /* If the timeout value is non-zero, we need to just
1008 return sharing violation. Don't retry the open
1009 as we were not notified of a close and we don't want to
1010 trigger another spurious oplock break. */
1012 if (!file_existed || dib.dev != psbuf->st_dev || dib.inode != psbuf->st_ino ||
1013 pml->msg_time.tv_sec || pml->msg_time.tv_usec) {
1014 /* Ensure we don't reprocess this message. */
1015 remove_sharing_violation_open_smb_message(mid);
1017 /* Now remove the deferred open entry under lock. */
1018 lock_share_entry(conn, dib.dev, dib.inode);
1019 delete_defered_open_entry_record(conn, dib.dev, dib.inode);
1020 unlock_share_entry(conn, dib.dev, dib.inode);
1022 unix_ERR_class = ERRDOS;
1023 unix_ERR_code = ERRbadshare;
1024 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
1025 return NULL;
1027 /* Ensure we don't reprocess this message. */
1028 remove_sharing_violation_open_smb_message(mid);
1032 if (conn->printer) {
1033 /* printers are handled completely differently. Most of the passed parameters are
1034 ignored */
1035 if (Access)
1036 *Access = DOS_OPEN_WRONLY;
1037 if (paction)
1038 *paction = FILE_WAS_CREATED;
1039 return print_fsp_open(conn, fname);
1042 fsp = file_new(conn);
1043 if(!fsp)
1044 return NULL;
1046 DEBUG(10,("open_file_shared: fname = %s, dos_attrs = %x, share_mode = %x, ofun = %x, mode = %o, oplock request = %d\n",
1047 fname, new_dos_mode, share_mode, ofun, (int)mode, oplock_request ));
1049 if (!check_name(fname,conn)) {
1050 file_free(fsp);
1051 return NULL;
1054 new_dos_mode &= SAMBA_ATTRIBUTES_MASK;
1055 if (file_existed) {
1056 existing_dos_mode = dos_mode(conn, fname, psbuf);
1059 /* ignore any oplock requests if oplocks are disabled */
1060 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break) {
1061 oplock_request = 0;
1064 /* this is for OS/2 long file names - say we don't support them */
1065 if (strstr(fname,".+,;=[].")) {
1066 unix_ERR_class = ERRDOS;
1067 /* OS/2 Workplace shell fix may be main code stream in a later release. */
1068 unix_ERR_code = ERRcannotopen;
1069 unix_ERR_ntstatus = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1070 DEBUG(5,("open_file_shared: OS/2 long filenames are not supported.\n"));
1071 /* need to reset errno or DEVELOPER will cause us to coredump */
1072 errno = 0;
1073 file_free(fsp);
1074 return NULL;
1077 if ((GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL) && file_existed) {
1078 DEBUG(5,("open_file_shared: create new requested for file %s and file already exists.\n",
1079 fname ));
1080 file_free(fsp);
1081 if (S_ISDIR(psbuf->st_mode)) {
1082 errno = EISDIR;
1083 } else {
1084 errno = EEXIST;
1086 return NULL;
1089 if (CAN_WRITE(conn) && (GET_FILE_CREATE_DISPOSITION(ofun) == FILE_CREATE_IF_NOT_EXIST))
1090 flags2 |= O_CREAT;
1092 if (CAN_WRITE(conn) && (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_TRUNCATE))
1093 flags2 |= O_TRUNC;
1095 /* We only care about matching attributes on file exists and truncate. */
1096 if (file_existed && (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_TRUNCATE)) {
1097 if (!open_match_attributes(conn, fname, existing_dos_mode, new_dos_mode,
1098 psbuf->st_mode, mode, &new_mode)) {
1099 DEBUG(5,("open_file_shared: attributes missmatch for file %s (%x %x) (0%o, 0%o)\n",
1100 fname, existing_dos_mode, new_dos_mode,
1101 (int)psbuf->st_mode, (int)mode ));
1102 file_free(fsp);
1103 errno = EACCES;
1104 return NULL;
1108 if (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL)
1109 flags2 |= O_EXCL;
1111 /* note that we ignore the append flag as
1112 append does not mean the same thing under dos and unix */
1114 switch (GET_OPEN_MODE(share_mode)) {
1115 case DOS_OPEN_WRONLY:
1116 flags = O_WRONLY;
1117 if (desired_access == 0)
1118 desired_access = FILE_WRITE_DATA;
1119 break;
1120 case DOS_OPEN_FCB:
1121 fcbopen = True;
1122 flags = O_RDWR;
1123 if (desired_access == 0)
1124 desired_access = FILE_READ_DATA|FILE_WRITE_DATA;
1125 break;
1126 case DOS_OPEN_RDWR:
1127 flags = O_RDWR;
1128 if (desired_access == 0)
1129 desired_access = FILE_READ_DATA|FILE_WRITE_DATA;
1130 break;
1131 default:
1132 flags = O_RDONLY;
1133 if (desired_access == 0)
1134 desired_access = FILE_READ_DATA;
1135 break;
1138 #if defined(O_SYNC)
1139 if (GET_FILE_SYNC_OPENMODE(share_mode)) {
1140 flags2 |= O_SYNC;
1142 #endif /* O_SYNC */
1144 if (flags != O_RDONLY && file_existed &&
1145 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_mode))) {
1146 if (!fcbopen) {
1147 DEBUG(5,("open_file_shared: read/write access requested for file %s on read only %s\n",
1148 fname, !CAN_WRITE(conn) ? "share" : "file" ));
1149 file_free(fsp);
1150 errno = EACCES;
1151 return NULL;
1153 flags = O_RDONLY;
1156 if (deny_mode > DENY_NONE && deny_mode!=DENY_FCB) {
1157 DEBUG(2,("Invalid deny mode %d on file %s\n",deny_mode,fname));
1158 file_free(fsp);
1159 errno = EINVAL;
1160 return NULL;
1163 if (desired_access && ((desired_access & ~(SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES))==0) &&
1164 ((desired_access & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) != 0)) {
1165 /* Stat open that doesn't trigger oplock breaks or share mode checks... ! JRA. */
1166 deny_mode = DENY_NONE;
1167 if (file_existed) {
1168 oplock_request = 0;
1169 add_share_mode = False;
1170 flags2 &= ~O_CREAT;
1174 if (file_existed) {
1176 dev = psbuf->st_dev;
1177 inode = psbuf->st_ino;
1179 lock_share_entry(conn, dev, inode);
1181 num_share_modes = open_mode_check(conn, fname, dev, inode,
1182 desired_access,
1183 share_mode,
1184 &flags, &oplock_request, &all_current_opens_are_level_II);
1185 if(num_share_modes == -1) {
1188 * This next line is a subtlety we need for MS-Access. If a file open will
1189 * fail due to share permissions and also for security (access)
1190 * reasons, we need to return the access failed error, not the
1191 * share error. This means we must attempt to open the file anyway
1192 * in order to get the UNIX access error - even if we're going to
1193 * fail the open for share reasons. This is bad, as we're burning
1194 * another fd if there are existing locks but there's nothing else
1195 * we can do. We also ensure we're not going to create or tuncate
1196 * the file as we only want an access decision at this stage. JRA.
1198 errno = 0;
1199 fsp_open = open_file(fsp,conn,fname,psbuf,
1200 flags|(flags2&~(O_TRUNC|O_CREAT)),mode,desired_access);
1202 DEBUG(4,("open_file_shared : share_mode deny - calling open_file with \
1203 flags=0x%X flags2=0x%X mode=0%o returned %d\n",
1204 flags,(flags2&~(O_TRUNC|O_CREAT)),(int)mode,(int)fsp_open ));
1206 if (!fsp_open && errno) {
1207 unix_ERR_class = ERRDOS;
1208 unix_ERR_code = ERRnoaccess;
1209 unix_ERR_ntstatus = NT_STATUS_ACCESS_DENIED;
1213 * If we're returning a share violation, ensure we cope with
1214 * the braindead 1 second delay.
1217 if (!internal_only_open && NT_STATUS_EQUAL(unix_ERR_ntstatus,NT_STATUS_SHARING_VIOLATION)) {
1218 /* The fsp->open_time here represents the current time of day. */
1219 defer_open_sharing_error(conn, &fsp->open_time, fname, dev, inode);
1222 unlock_share_entry(conn, dev, inode);
1223 if (fsp_open) {
1224 fd_close(conn, fsp);
1226 * We have detected a sharing violation here
1227 * so return the correct error code
1229 unix_ERR_class = ERRDOS;
1230 unix_ERR_code = ERRbadshare;
1231 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
1233 file_free(fsp);
1234 return NULL;
1238 * We exit this block with the share entry *locked*.....
1243 * Ensure we pay attention to default ACLs on directories if required.
1246 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1247 (def_acl = directory_has_default_acl(conn, parent_dirname(fname))))
1248 mode = 0777;
1250 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n",
1251 flags,flags2,(int)mode));
1254 * open_file strips any O_TRUNC flags itself.
1257 fsp_open = open_file(fsp,conn,fname,psbuf,flags|flags2,mode,desired_access);
1259 if (!fsp_open && (flags == O_RDWR) && (errno != ENOENT) && fcbopen) {
1260 if((fsp_open = open_file(fsp,conn,fname,psbuf,O_RDONLY,mode,desired_access)) == True)
1261 flags = O_RDONLY;
1264 if (!fsp_open) {
1265 if(file_existed)
1266 unlock_share_entry(conn, dev, inode);
1267 file_free(fsp);
1268 return NULL;
1272 * Deal with the race condition where two smbd's detect the file doesn't
1273 * exist and do the create at the same time. One of them will win and
1274 * set a share mode, the other (ie. this one) should check if the
1275 * requested share mode for this create is allowed.
1278 if (!file_existed) {
1281 * Now the file exists and fsp is successfully opened,
1282 * fsp->dev and fsp->inode are valid and should replace the
1283 * dev=0,inode=0 from a non existent file. Spotted by
1284 * Nadav Danieli <nadavd@exanet.com>. JRA.
1287 dev = fsp->dev;
1288 inode = fsp->inode;
1290 lock_share_entry_fsp(fsp);
1292 num_share_modes = open_mode_check(conn, fname, dev, inode,
1293 desired_access,
1294 share_mode,
1295 &flags, &oplock_request, &all_current_opens_are_level_II);
1297 if(num_share_modes == -1) {
1299 * If we're returning a share violation, ensure we cope with
1300 * the braindead 1 second delay.
1303 if (!internal_only_open && NT_STATUS_EQUAL(unix_ERR_ntstatus,NT_STATUS_SHARING_VIOLATION)) {
1304 /* The fsp->open_time here represents the current time of day. */
1305 defer_open_sharing_error(conn, &fsp->open_time, fname, dev, inode);
1308 unlock_share_entry_fsp(fsp);
1309 fd_close(conn,fsp);
1310 file_free(fsp);
1312 * We have detected a sharing violation here, so
1313 * return the correct code.
1315 unix_ERR_class = ERRDOS;
1316 unix_ERR_code = ERRbadshare;
1317 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
1318 return NULL;
1322 * If there are any share modes set then the file *did*
1323 * exist. Ensure we return the correct value for action.
1326 if (num_share_modes > 0)
1327 file_existed = True;
1330 * We exit this block with the share entry *locked*.....
1334 /* note that we ignore failure for the following. It is
1335 basically a hack for NFS, and NFS will never set one of
1336 these only read them. Nobody but Samba can ever set a deny
1337 mode and we have already checked our more authoritative
1338 locking database for permission to set this deny mode. If
1339 the kernel refuses the operations then the kernel is wrong */
1340 kernel_flock(fsp, deny_mode);
1343 * At this point onwards, we can guarentee that the share entry
1344 * is locked, whether we created the file or not, and that the
1345 * deny mode is compatible with all current opens.
1349 * If requested, truncate the file.
1352 if (flags2&O_TRUNC) {
1354 * We are modifing the file after open - update the stat struct..
1356 if ((SMB_VFS_FTRUNCATE(fsp,fsp->fd,0) == -1) || (SMB_VFS_FSTAT(fsp,fsp->fd,psbuf)==-1)) {
1357 unlock_share_entry_fsp(fsp);
1358 fd_close(conn,fsp);
1359 file_free(fsp);
1360 return NULL;
1364 switch (flags) {
1365 case O_RDONLY:
1366 open_mode = DOS_OPEN_RDONLY;
1367 break;
1368 case O_RDWR:
1369 open_mode = DOS_OPEN_RDWR;
1370 break;
1371 case O_WRONLY:
1372 open_mode = DOS_OPEN_WRONLY;
1373 break;
1376 fsp->share_mode = SET_DENY_MODE(deny_mode) |
1377 SET_OPEN_MODE(open_mode) |
1378 SET_ALLOW_SHARE_DELETE(allow_share_delete);
1380 DEBUG(10,("open_file_shared : share_mode = %x\n", fsp->share_mode ));
1382 if (Access) {
1383 (*Access) = open_mode;
1386 action = 0;
1388 if (file_existed && !(flags2 & O_TRUNC))
1389 action = FILE_WAS_OPENED;
1390 if (file_existed && (flags2 & O_TRUNC))
1391 action = FILE_WAS_OVERWRITTEN;
1392 if (!file_existed)
1393 action = FILE_WAS_CREATED;
1395 if (paction) {
1396 *paction = action;
1400 * Setup the oplock info in both the shared memory and
1401 * file structs.
1404 if(oplock_request && (num_share_modes == 0) &&
1405 !IS_VETO_OPLOCK_PATH(conn,fname) && set_file_oplock(fsp, oplock_request) ) {
1406 port = global_oplock_port;
1407 } else if (oplock_request && all_current_opens_are_level_II) {
1408 port = global_oplock_port;
1409 oplock_request = LEVEL_II_OPLOCK;
1410 set_file_oplock(fsp, oplock_request);
1411 } else {
1412 port = 0;
1413 oplock_request = 0;
1416 if (add_share_mode) {
1417 set_share_mode(fsp, port, oplock_request);
1420 if (delete_on_close) {
1421 uint32 dosmode = existing_dos_mode;
1422 NTSTATUS result;
1424 if (action == FILE_WAS_OVERWRITTEN || action == FILE_WAS_CREATED) {
1425 dosmode = new_dos_mode;
1427 result = set_delete_on_close_internal(fsp, delete_on_close, dosmode);
1429 if (NT_STATUS_V(result) != NT_STATUS_V(NT_STATUS_OK)) {
1430 uint8 u_e_c;
1431 uint32 u_e_code;
1432 /* Remember to delete the mode we just added. */
1433 if (add_share_mode) {
1434 del_share_mode(fsp, NULL);
1436 unlock_share_entry_fsp(fsp);
1437 fd_close(conn,fsp);
1438 file_free(fsp);
1439 ntstatus_to_dos(result, &u_e_c, &u_e_code);
1440 unix_ERR_ntstatus = result;
1441 unix_ERR_class = u_e_c;
1442 unix_ERR_code = u_e_code;
1443 return NULL;
1447 if (action == FILE_WAS_OVERWRITTEN || action == FILE_WAS_CREATED) {
1448 /* Files should be initially set as archive */
1449 if (lp_map_archive(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1450 file_set_dosmode(conn, fname, new_dos_mode | aARCH, NULL, True);
1455 * Take care of inherited ACLs on created files - if default ACL not
1456 * selected.
1459 if (!file_existed && !def_acl) {
1461 int saved_errno = errno; /* We might get ENOSYS in the next call.. */
1463 if (SMB_VFS_FCHMOD_ACL(fsp, fsp->fd, mode) == -1 && errno == ENOSYS)
1464 errno = saved_errno; /* Ignore ENOSYS */
1466 } else if (new_mode) {
1468 int ret = -1;
1470 /* Attributes need changing. File already existed. */
1473 int saved_errno = errno; /* We might get ENOSYS in the next call.. */
1474 ret = SMB_VFS_FCHMOD_ACL(fsp, fsp->fd, new_mode);
1476 if (ret == -1 && errno == ENOSYS) {
1477 errno = saved_errno; /* Ignore ENOSYS */
1478 } else {
1479 DEBUG(5, ("open_file_shared: failed to reset attributes of file %s to 0%o\n",
1480 fname, (int)new_mode));
1481 ret = 0; /* Don't do the fchmod below. */
1485 if ((ret == -1) && (SMB_VFS_FCHMOD(fsp, fsp->fd, new_mode) == -1))
1486 DEBUG(5, ("open_file_shared: failed to reset attributes of file %s to 0%o\n",
1487 fname, (int)new_mode));
1490 /* If this is a successful open, we must remove any deferred open records. */
1491 delete_defered_open_entry_record(conn, fsp->dev, fsp->inode);
1492 unlock_share_entry_fsp(fsp);
1494 conn->num_files_open++;
1496 return fsp;
1499 /****************************************************************************
1500 Open a file for for write to ensure that we can fchmod it.
1501 ****************************************************************************/
1503 files_struct *open_file_fchmod(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf)
1505 files_struct *fsp = NULL;
1506 BOOL fsp_open;
1508 if (!VALID_STAT(*psbuf))
1509 return NULL;
1511 fsp = file_new(conn);
1512 if(!fsp)
1513 return NULL;
1515 /* note! we must use a non-zero desired access or we don't get
1516 a real file descriptor. Oh what a twisted web we weave. */
1517 fsp_open = open_file(fsp,conn,fname,psbuf,O_WRONLY,0,FILE_WRITE_DATA);
1520 * This is not a user visible file open.
1521 * Don't set a share mode and don't increment
1522 * the conn->num_files_open.
1525 if (!fsp_open) {
1526 file_free(fsp);
1527 return NULL;
1530 return fsp;
1533 /****************************************************************************
1534 Close the fchmod file fd - ensure no locks are lost.
1535 ****************************************************************************/
1537 int close_file_fchmod(files_struct *fsp)
1539 int ret = fd_close(fsp->conn, fsp);
1540 file_free(fsp);
1541 return ret;
1544 /****************************************************************************
1545 Open a directory from an NT SMB call.
1546 ****************************************************************************/
1548 files_struct *open_directory(connection_struct *conn, char *fname, SMB_STRUCT_STAT *psbuf,
1549 uint32 desired_access, int share_mode, int smb_ofun, int *action)
1551 extern struct current_user current_user;
1552 BOOL got_stat = False;
1553 files_struct *fsp = file_new(conn);
1554 BOOL delete_on_close = GET_DELETE_ON_CLOSE_FLAG(share_mode);
1556 if(!fsp)
1557 return NULL;
1559 if (VALID_STAT(*psbuf))
1560 got_stat = True;
1562 if (got_stat && (GET_FILE_OPEN_DISPOSITION(smb_ofun) == FILE_EXISTS_FAIL)) {
1563 file_free(fsp);
1564 errno = EEXIST; /* Setup so correct error is returned to client. */
1565 return NULL;
1568 if (GET_FILE_CREATE_DISPOSITION(smb_ofun) == FILE_CREATE_IF_NOT_EXIST) {
1570 if (got_stat) {
1572 if(!S_ISDIR(psbuf->st_mode)) {
1573 DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
1574 file_free(fsp);
1575 errno = EACCES;
1576 return NULL;
1578 *action = FILE_WAS_OPENED;
1580 } else {
1583 * Try and create the directory.
1586 if(!CAN_WRITE(conn)) {
1587 DEBUG(2,("open_directory: failing create on read-only share\n"));
1588 file_free(fsp);
1589 errno = EACCES;
1590 return NULL;
1593 if (ms_has_wild(fname)) {
1594 file_free(fsp);
1595 DEBUG(5,("open_directory: failing create on filename %s with wildcards\n", fname));
1596 unix_ERR_class = ERRDOS;
1597 unix_ERR_code = ERRinvalidname;
1598 unix_ERR_ntstatus = NT_STATUS_OBJECT_NAME_INVALID;
1599 return NULL;
1602 if( strchr_m(fname, ':')) {
1603 file_free(fsp);
1604 DEBUG(5,("open_directory: failing create on filename %s with colon in name\n", fname));
1605 unix_ERR_class = ERRDOS;
1606 unix_ERR_code = ERRinvalidname;
1607 unix_ERR_ntstatus = NT_STATUS_NOT_A_DIRECTORY;
1608 return NULL;
1611 if(vfs_MkDir(conn,fname, unix_mode(conn,aDIR, fname, True)) < 0) {
1612 DEBUG(2,("open_directory: unable to create %s. Error was %s\n",
1613 fname, strerror(errno) ));
1614 file_free(fsp);
1615 return NULL;
1618 if(SMB_VFS_STAT(conn,fname, psbuf) != 0) {
1619 file_free(fsp);
1620 return NULL;
1623 *action = FILE_WAS_CREATED;
1626 } else {
1629 * Don't create - just check that it *was* a directory.
1632 if(!got_stat) {
1633 DEBUG(3,("open_directory: unable to stat name = %s. Error was %s\n",
1634 fname, strerror(errno) ));
1635 file_free(fsp);
1636 return NULL;
1639 if(!S_ISDIR(psbuf->st_mode)) {
1640 DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
1641 file_free(fsp);
1642 return NULL;
1645 *action = FILE_WAS_OPENED;
1648 DEBUG(5,("open_directory: opening directory %s\n", fname));
1651 * Setup the files_struct for it.
1654 fsp->mode = psbuf->st_mode;
1655 fsp->inode = psbuf->st_ino;
1656 fsp->dev = psbuf->st_dev;
1657 fsp->size = psbuf->st_size;
1658 fsp->vuid = current_user.vuid;
1659 fsp->file_pid = global_smbpid;
1660 fsp->can_lock = True;
1661 fsp->can_read = False;
1662 fsp->can_write = False;
1663 fsp->share_mode = share_mode;
1664 fsp->desired_access = desired_access;
1665 fsp->print_file = False;
1666 fsp->modified = False;
1667 fsp->oplock_type = NO_OPLOCK;
1668 fsp->sent_oplock_break = NO_BREAK_SENT;
1669 fsp->is_directory = True;
1670 fsp->is_stat = False;
1671 fsp->directory_delete_on_close = False;
1672 string_set(&fsp->fsp_name,fname);
1674 if (delete_on_close) {
1675 NTSTATUS result = set_delete_on_close_internal(fsp, delete_on_close, 0);
1677 if (NT_STATUS_V(result) != NT_STATUS_V(NT_STATUS_OK)) {
1678 file_free(fsp);
1679 return NULL;
1682 conn->num_files_open++;
1684 return fsp;
1687 /****************************************************************************
1688 Open a pseudo-file (no locking checks - a 'stat' open).
1689 ****************************************************************************/
1691 files_struct *open_file_stat(connection_struct *conn, char *fname, SMB_STRUCT_STAT *psbuf)
1693 extern struct current_user current_user;
1694 files_struct *fsp = NULL;
1696 if (!VALID_STAT(*psbuf))
1697 return NULL;
1699 /* Can't 'stat' open directories. */
1700 if(S_ISDIR(psbuf->st_mode))
1701 return NULL;
1703 fsp = file_new(conn);
1704 if(!fsp)
1705 return NULL;
1707 DEBUG(5,("open_file_stat: 'opening' file %s\n", fname));
1710 * Setup the files_struct for it.
1713 fsp->mode = psbuf->st_mode;
1714 fsp->inode = psbuf->st_ino;
1715 fsp->dev = psbuf->st_dev;
1716 fsp->size = psbuf->st_size;
1717 fsp->vuid = current_user.vuid;
1718 fsp->file_pid = global_smbpid;
1719 fsp->can_lock = False;
1720 fsp->can_read = False;
1721 fsp->can_write = False;
1722 fsp->share_mode = 0;
1723 fsp->desired_access = 0;
1724 fsp->print_file = False;
1725 fsp->modified = False;
1726 fsp->oplock_type = NO_OPLOCK;
1727 fsp->sent_oplock_break = NO_BREAK_SENT;
1728 fsp->is_directory = False;
1729 fsp->is_stat = True;
1730 fsp->directory_delete_on_close = False;
1731 string_set(&fsp->fsp_name,fname);
1733 conn->num_files_open++;
1735 return fsp;