selftest: add a fake root user to nss_wrapper_passwd in s3.
[Samba/gbeck.git] / source3 / modules / onefs_open.c
blobb0f31b9bf8650b1f96413bdc2d8c1e32741274e9
1 /*
2 * Unix SMB/CIFS implementation.
4 * This file began with some code from source3/smbd/open.c and has been
5 * modified it work with ifs_createfile.
7 * ifs_createfile is a CIFS-specific syscall for opening/files and
8 * directories. It adds support for:
9 * - Full in-kernel access checks using a windows access_mask
10 * - Cluster-coherent share mode locks
11 * - Cluster-coherent oplocks
12 * - Streams
13 * - Setting security descriptors at create time
14 * - Setting dos_attributes at create time
16 * Copyright (C) Andrew Tridgell 1992-1998
17 * Copyright (C) Jeremy Allison 2001-2004
18 * Copyright (C) Volker Lendecke 2005
19 * Copyright (C) Tim Prouty, 2008
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, see <http://www.gnu.org/licenses/>.
35 #include "includes.h"
36 #include "onefs.h"
37 #include "onefs_config.h"
38 #include "oplock_onefs.h"
39 #include "smbd/globals.h"
41 extern const struct generic_mapping file_generic_mapping;
43 struct onefs_fsp_data {
44 uint64_t oplock_callback_id;
47 static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
48 struct smb_request *req,
49 const char *fname,
50 uint32_t access_mask,
51 uint32_t share_access,
52 uint32_t create_disposition,
53 uint32_t create_options,
54 uint32_t file_attributes,
55 uint32_t oplock_request,
56 uint64_t allocation_size,
57 struct security_descriptor *sd,
58 struct ea_list *ea_list,
59 files_struct **result,
60 int *pinfo,
61 struct onefs_fsp_data *fsp_data,
62 SMB_STRUCT_STAT *psbuf);
64 /****************************************************************************
65 Open a file.
66 ****************************************************************************/
68 static NTSTATUS onefs_open_file(files_struct *fsp,
69 connection_struct *conn,
70 struct smb_request *req,
71 const char *parent_dir,
72 const char *name,
73 const char *path,
74 SMB_STRUCT_STAT *psbuf,
75 int flags,
76 mode_t unx_mode,
77 uint32 access_mask,
78 uint32 open_access_mask,
79 int oplock_request,
80 uint64 id,
81 uint32 share_access,
82 uint32 create_options,
83 uint32_t new_dos_attributes,
84 struct security_descriptor *sd,
85 int *granted_oplock)
87 NTSTATUS status = NT_STATUS_OK;
88 int accmode = (flags & O_ACCMODE);
89 int local_flags = flags;
90 bool file_existed = VALID_STAT(*psbuf);
91 const char *wild;
92 char *base = NULL;
93 char *stream = NULL;
94 int base_fd = -1;
96 fsp->fh->fd = -1;
97 errno = EPERM;
99 /* Check permissions */
102 * This code was changed after seeing a client open request
103 * containing the open mode of (DENY_WRITE/read-only) with
104 * the 'create if not exist' bit set. The previous code
105 * would fail to open the file read only on a read-only share
106 * as it was checking the flags parameter directly against O_RDONLY,
107 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
108 * JRA.
111 if (!CAN_WRITE(conn)) {
112 /* It's a read-only share - fail if we wanted to write. */
113 if(accmode != O_RDONLY) {
114 DEBUG(3,("Permission denied opening %s\n", path));
115 return NT_STATUS_ACCESS_DENIED;
116 } else if(flags & O_CREAT) {
117 /* We don't want to write - but we must make sure that
118 O_CREAT doesn't create the file if we have write
119 access into the directory.
121 flags &= ~O_CREAT;
122 local_flags &= ~O_CREAT;
127 * This little piece of insanity is inspired by the
128 * fact that an NT client can open a file for O_RDONLY,
129 * but set the create disposition to FILE_EXISTS_TRUNCATE.
130 * If the client *can* write to the file, then it expects to
131 * truncate the file, even though it is opening for readonly.
132 * Quicken uses this stupid trick in backup file creation...
133 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
134 * for helping track this one down. It didn't bite us in 2.0.x
135 * as we always opened files read-write in that release. JRA.
138 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
139 DEBUG(10,("onefs_open_file: truncate requested on read-only "
140 "open for file %s\n", path));
141 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
144 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
146 * We would block on opening a FIFO with no one else on the
147 * other end. Do what we used to do and add O_NONBLOCK to the
148 * open flags. JRA.
151 if (file_existed && S_ISFIFO(psbuf->st_mode)) {
152 local_flags |= O_NONBLOCK;
154 #endif
156 /* Don't create files with Microsoft wildcard characters. */
157 if (fsp->base_fsp) {
159 * wildcard characters are allowed in stream names
160 * only test the basefilename
162 wild = fsp->base_fsp->fsp_name;
163 } else {
164 wild = path;
166 if ((local_flags & O_CREAT) && !file_existed &&
167 ms_has_wild(wild)) {
169 * XXX: may need to remvoe this return...
171 * We dont think this check needs to exist. All it does is
172 * block creating files with Microsoft wildcards, which is
173 * fine if the creation originated from NFS or locally and
174 * then was copied via Samba.
176 DEBUG(1, ("onefs_open_file: creating file with wildcard: %s\n",
177 path));
178 return NT_STATUS_OBJECT_NAME_INVALID;
181 /* Actually do the open */
183 #ifdef O_NOFOLLOW
185 * Never follow symlinks on a POSIX client. The
186 * client should be doing this.
189 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
190 flags |= O_NOFOLLOW;
192 #endif
193 /* Stream handling */
194 if (is_ntfs_stream_name(path)) {
195 status = onefs_split_ntfs_stream_name(talloc_tos(), path,
196 &base, &stream);
198 /* It's a stream, so pass in the base_fd */
199 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) && stream != NULL) {
200 SMB_ASSERT(fsp->base_fsp);
202 DEBUG(10,("Opening a stream: base=%s(%d), stream=%s\n",
203 base, fsp->base_fsp->fh->fd, stream));
205 base_fd = fsp->base_fsp->fh->fd;
208 fsp->fh->fd = onefs_sys_create_file(conn,
209 base_fd,
210 stream != NULL ? stream :
211 (base != NULL ? base : path),
212 access_mask,
213 open_access_mask,
214 share_access,
215 create_options,
216 flags,
217 unx_mode,
218 oplock_request,
221 new_dos_attributes,
222 granted_oplock);
224 if (fsp->fh->fd == -1) {
225 if (errno == EMFILE) {
226 static time_t last_warned = 0L;
228 if (time((time_t *) NULL) > last_warned) {
229 DEBUG(0, ("Too many open files, unable "
230 "to open more! smbd's max "
231 "open files = %d, also check "
232 "sysctl kern.maxfiles and "
233 "sysctl kern.maxfilesperproc\n",
234 lp_max_open_files()));
235 last_warned = time((time_t *) NULL);
239 status = map_nt_error_from_unix(errno);
240 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
241 "(flags=%d)\n",
242 path, strerror(errno), local_flags, flags));
243 return status;
246 if ((local_flags & O_CREAT) && !file_existed) {
248 /* Inherit the ACL if required */
249 if (lp_inherit_perms(SNUM(conn))) {
250 inherit_access_posix_acl(conn, parent_dir, path,
251 unx_mode);
254 /* Change the owner if required. */
255 if (lp_inherit_owner(SNUM(conn))) {
256 change_file_owner_to_parent(conn, parent_dir,
257 fsp);
260 notify_fname(conn, NOTIFY_ACTION_ADDED,
261 FILE_NOTIFY_CHANGE_FILE_NAME, path);
264 if (!file_existed) {
265 int ret;
267 if (fsp->fh->fd == -1) {
268 ret = SMB_VFS_STAT(conn, path, psbuf);
269 } else {
270 ret = SMB_VFS_FSTAT(fsp, psbuf);
271 /* If we have an fd, this stat should succeed. */
272 if (ret == -1) {
273 DEBUG(0,("Error doing fstat on open file %s "
274 "(%s)\n", path,strerror(errno) ));
278 /* For a non-io open, this stat failing means file not found. JRA */
279 if (ret == -1) {
280 status = map_nt_error_from_unix(errno);
281 fd_close(fsp);
282 return status;
287 * POSIX allows read-only opens of directories. We don't
288 * want to do this (we use a different code path for this)
289 * so catch a directory open and return an EISDIR. JRA.
292 if(S_ISDIR(psbuf->st_mode)) {
293 fd_close(fsp);
294 errno = EISDIR;
295 return NT_STATUS_FILE_IS_A_DIRECTORY;
298 fsp->mode = psbuf->st_mode;
299 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
300 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
301 fsp->file_pid = req ? req->smbpid : 0;
302 fsp->can_lock = True;
303 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
304 if (!CAN_WRITE(conn)) {
305 fsp->can_write = False;
306 } else {
307 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
308 True : False;
310 fsp->print_file = False;
311 fsp->modified = False;
312 fsp->sent_oplock_break = NO_BREAK_SENT;
313 fsp->is_directory = False;
314 if (conn->aio_write_behind_list &&
315 is_in_path(path, conn->aio_write_behind_list, conn->case_sensitive)) {
316 fsp->aio_write_behind = True;
319 string_set(&fsp->fsp_name, path);
320 fsp->wcp = NULL; /* Write cache pointer. */
322 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
323 conn->server_info->unix_name,
324 fsp->fsp_name,
325 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
326 conn->num_files_open));
328 errno = 0;
329 return NT_STATUS_OK;
332 /****************************************************************************
333 Handle the 1 second delay in returning a SHARING_VIOLATION error.
334 ****************************************************************************/
336 static void defer_open(struct share_mode_lock *lck,
337 struct timeval request_time,
338 struct timeval timeout,
339 struct smb_request *req,
340 struct deferred_open_record *state)
342 int i;
344 /* Paranoia check */
346 for (i=0; i<lck->num_share_modes; i++) {
347 struct share_mode_entry *e = &lck->share_modes[i];
349 if (!is_deferred_open_entry(e)) {
350 continue;
353 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
354 DEBUG(0, ("Trying to defer an already deferred "
355 "request: mid=%d, exiting\n", req->mid));
356 exit_server("attempt to defer a deferred request");
360 /* End paranoia check */
362 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
363 "open entry for mid %u\n",
364 (unsigned int)request_time.tv_sec,
365 (unsigned int)request_time.tv_usec,
366 (unsigned int)req->mid));
368 if (!push_deferred_smb_message(req, request_time, timeout,
369 (char *)state, sizeof(*state))) {
370 exit_server("push_deferred_smb_message failed");
372 add_deferred_open(lck, req->mid, request_time, state->id);
375 * Push the MID of this packet on the signing queue.
376 * We only do this once, the first time we push the packet
377 * onto the deferred open queue, as this has a side effect
378 * of incrementing the response sequence number.
381 srv_defer_sign_response(req->mid);
384 static void schedule_defer_open(struct share_mode_lock *lck,
385 struct timeval request_time,
386 struct smb_request *req)
388 struct deferred_open_record state;
390 /* This is a relative time, added to the absolute
391 request_time value to get the absolute timeout time.
392 Note that if this is the second or greater time we enter
393 this codepath for this particular request mid then
394 request_time is left as the absolute time of the *first*
395 time this request mid was processed. This is what allows
396 the request to eventually time out. */
398 struct timeval timeout;
400 /* Normally the smbd we asked should respond within
401 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
402 * the client did, give twice the timeout as a safety
403 * measure here in case the other smbd is stuck
404 * somewhere else. */
407 * On OneFS, the kernel will always send an oplock_revoked message
408 * before this timeout is hit.
410 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*10, 0);
412 /* Nothing actually uses state.delayed_for_oplocks
413 but it's handy to differentiate in debug messages
414 between a 30 second delay due to oplock break, and
415 a 1 second delay for share mode conflicts. */
417 state.delayed_for_oplocks = True;
418 state.failed = false;
419 state.id = lck->id;
421 if (!request_timed_out(request_time, timeout)) {
422 defer_open(lck, request_time, timeout, req, &state);
426 /****************************************************************************
427 Open a file with a share mode. Passed in an already created files_struct.
428 ****************************************************************************/
429 NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
430 struct smb_request *req,
431 const char *fname,
432 uint32 access_mask,
433 uint32 share_access,
434 uint32 create_disposition,
435 uint32 create_options,
436 uint32 new_dos_attributes,
437 int oplock_request,
438 struct security_descriptor *sd,
439 files_struct *fsp,
440 int *pinfo,
441 struct onefs_fsp_data *fsp_data,
442 SMB_STRUCT_STAT *psbuf)
444 int flags=0;
445 int flags2=0;
446 bool file_existed = VALID_STAT(*psbuf);
447 bool def_acl = False;
448 bool posix_open = False;
449 bool new_file_created = False;
450 bool clear_ads = False;
451 struct file_id id;
452 mode_t new_unx_mode = (mode_t)0;
453 mode_t unx_mode = (mode_t)0;
454 int info;
455 uint32 existing_dos_attributes = 0;
456 struct pending_message_list *pml = NULL;
457 struct timeval request_time = timeval_zero();
458 struct share_mode_lock *lck = NULL;
459 uint32 open_access_mask = access_mask;
460 NTSTATUS status;
461 int ret_flock;
462 char *parent_dir;
463 const char *newname;
464 int granted_oplock;
465 uint64_t oplock_callback_id = 0;
466 uint32 createfile_attributes = 0;
468 ZERO_STRUCT(id);
470 if (conn->printer) {
472 * Printers are handled completely differently.
473 * Most of the passed parameters are ignored.
476 if (pinfo) {
477 *pinfo = FILE_WAS_CREATED;
480 DEBUG(10, ("onefs_open_file_ntcreate: printer open fname=%s\n",
481 fname));
483 return print_fsp_open(req, conn, fname, req->vuid, fsp, psbuf);
486 if (!parent_dirname(talloc_tos(), fname, &parent_dir, &newname)) {
487 return NT_STATUS_NO_MEMORY;
490 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
491 posix_open = True;
492 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
493 new_dos_attributes = 0;
494 } else {
495 /* We add aARCH to this as this mode is only used if the file is
496 * created new. */
497 unx_mode = unix_mode(conn, new_dos_attributes | aARCH, fname,
498 parent_dir);
501 DEBUG(10,("onefs_open_file_ntcreate: fname=%s, dos_attrs=0x%x "
502 "access_mask=0x%x share_access=0x%x "
503 "create_disposition = 0x%x create_options=0x%x "
504 "unix mode=0%o oplock_request=0x%x\n",
505 fname, new_dos_attributes, access_mask, share_access,
506 create_disposition, create_options, unx_mode,
507 oplock_request));
510 * Any non-stat-only open has the potential to contend oplocks, which
511 * means to avoid blocking in the kernel (which is unacceptable), the
512 * open must be deferred. In order to defer opens, req must not be
513 * NULL. The known cases of calling with a NULL req:
515 * 1. Open the base file of a stream: Always done stat-only
517 * 2. open_file_fchmod(), which is called from 3 places:
518 * A. try_chown: Posix acls only. Never called on onefs.
519 * B. set_ea_dos_attributes: Can't be called from onefs, because
520 * SMB_VFS_SETXATTR return ENOSYS.
521 * C. file_set_dos_mode: This would only happen if the "dos
522 * filemode" smb.conf parameter is set to yes. We ship with
523 * it off, but if a customer were to turn it on it would be
524 * bad.
526 if (req == NULL && !is_stat_open(access_mask) && !is_ntfs_stream_name(fname)) {
527 smb_panic("NULL req on a non-stat-open!");
530 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
531 DEBUG(0, ("No smb request but not an internal only open!\n"));
532 return NT_STATUS_INTERNAL_ERROR;
536 * Only non-internal opens can be deferred at all
539 if ((req != NULL)
540 && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
541 struct deferred_open_record *state =
542 (struct deferred_open_record *)pml->private_data.data;
544 /* Remember the absolute time of the original
545 request with this mid. We'll use it later to
546 see if this has timed out. */
548 request_time = pml->request_time;
550 /* Remove the deferred open entry under lock. */
551 lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
552 NULL);
553 if (lck == NULL) {
554 DEBUG(0, ("could not get share mode lock\n"));
555 } else {
556 del_deferred_open_entry(lck, req->mid);
557 TALLOC_FREE(lck);
560 /* Ensure we don't reprocess this message. */
561 remove_deferred_open_smb_message(req->mid);
564 * When receiving a semlock_async_failure message, the
565 * deferred open will be marked as "failed". Returning
566 * INTERNAL_ERROR.
568 if (state->failed) {
569 DEBUG(0, ("onefs_open_file_ntcreate: "
570 "semlock_async_failure detected!\n"));
571 return NT_STATUS_INTERNAL_ERROR;
575 status = check_name(conn, fname);
576 if (!NT_STATUS_IS_OK(status)) {
577 return status;
580 if (!posix_open) {
581 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
582 if (file_existed) {
583 existing_dos_attributes = dos_mode(conn, fname, psbuf);
587 /* Setup dos_attributes to be set by ifs_createfile */
588 if (lp_store_dos_attributes(SNUM(conn))) {
589 createfile_attributes = (new_dos_attributes | aARCH) &
590 ~(FILE_ATTRIBUTE_NONINDEXED | FILE_ATTRIBUTE_COMPRESSED);
593 /* Ignore oplock requests if oplocks are disabled. */
594 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
595 IS_VETO_OPLOCK_PATH(conn, fname)) {
596 /* Mask off everything except the private Samba bits. */
597 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
600 /* this is for OS/2 long file names - say we don't support them */
601 if (!lp_posix_pathnames() && strstr(fname,".+,;=[].")) {
602 /* OS/2 Workplace shell fix may be main code stream in a later
603 * release. */
604 DEBUG(5,("onefs_open_file_ntcreate: OS/2 long filenames are "
605 "not supported.\n"));
606 if (use_nt_status()) {
607 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
609 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
612 switch( create_disposition ) {
614 * Currently we're using FILE_SUPERSEDE as the same as
615 * FILE_OVERWRITE_IF but they really are
616 * different. FILE_SUPERSEDE deletes an existing file
617 * (requiring delete access) then recreates it.
619 case FILE_SUPERSEDE:
621 * @todo: Clear all file attributes?
622 * http://www.osronline.com/article.cfm?article=302
623 * create if not exist, trunc if exist
625 * If file exists replace/overwrite. If file doesn't
626 * exist create.
628 flags2 |= (O_CREAT | O_TRUNC);
629 clear_ads = true;
630 break;
632 case FILE_OVERWRITE_IF:
633 /* If file exists replace/overwrite. If file doesn't
634 * exist create. */
635 flags2 |= (O_CREAT | O_TRUNC);
636 clear_ads = true;
637 break;
639 case FILE_OPEN:
640 /* If file exists open. If file doesn't exist error. */
641 if (!file_existed) {
642 DEBUG(5,("onefs_open_file_ntcreate: FILE_OPEN "
643 "requested for file %s and file "
644 "doesn't exist.\n", fname ));
645 errno = ENOENT;
646 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
648 break;
650 case FILE_OVERWRITE:
651 /* If file exists overwrite. If file doesn't exist
652 * error. */
653 if (!file_existed) {
654 DEBUG(5, ("onefs_open_file_ntcreate: "
655 "FILE_OVERWRITE requested for file "
656 "%s and file doesn't exist.\n",
657 fname));
658 errno = ENOENT;
659 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
661 flags2 |= O_TRUNC;
662 clear_ads = true;
663 break;
665 case FILE_CREATE:
666 /* If file exists error. If file doesn't exist
667 * create. */
668 if (file_existed) {
669 DEBUG(5, ("onefs_open_file_ntcreate: "
670 "FILE_CREATE requested for file %s "
671 "and file already exists.\n",
672 fname));
673 if (S_ISDIR(psbuf->st_mode)) {
674 errno = EISDIR;
675 } else {
676 errno = EEXIST;
678 return map_nt_error_from_unix(errno);
680 flags2 |= (O_CREAT|O_EXCL);
681 break;
683 case FILE_OPEN_IF:
684 /* If file exists open. If file doesn't exist
685 * create. */
686 flags2 |= O_CREAT;
687 break;
689 default:
690 return NT_STATUS_INVALID_PARAMETER;
693 /* Match attributes on file exists and overwrite. */
694 if (!posix_open && file_existed &&
695 ((create_disposition == FILE_OVERWRITE) ||
696 (create_disposition == FILE_OVERWRITE_IF))) {
697 if (!open_match_attributes(conn, fname,
698 existing_dos_attributes,
699 new_dos_attributes, psbuf->st_mode,
700 unx_mode, &new_unx_mode)) {
701 DEBUG(5, ("onefs_open_file_ntcreate: attributes "
702 "missmatch for file %s (%x %x) (0%o, 0%o)\n",
703 fname, existing_dos_attributes,
704 new_dos_attributes,
705 (unsigned int)psbuf->st_mode,
706 (unsigned int)unx_mode ));
707 errno = EACCES;
708 return NT_STATUS_ACCESS_DENIED;
713 * OneFS understands MAXIMUM_ALLOWED_ACCESS, so only hack the
714 * access_mask, but leave the MAA for the actual open in
715 * open_access_mask.
717 open_access_mask = access_mask;
718 if (open_access_mask & MAXIMUM_ALLOWED_ACCESS) {
719 access_mask |= FILE_GENERIC_ALL;
722 /* Convert GENERIC bits to specific bits. */
723 se_map_generic(&access_mask, &file_generic_mapping);
724 se_map_generic(&open_access_mask, &file_generic_mapping);
726 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
727 /* This will cause oplock breaks. */
728 open_access_mask |= FILE_WRITE_DATA;
731 if (lp_parm_bool(SNUM(fsp->conn), PARM_ONEFS_TYPE,
732 PARM_IGNORE_SACLS, PARM_IGNORE_SACLS_DEFAULT)) {
733 access_mask &= ~SYSTEM_SECURITY_ACCESS;
736 DEBUG(10, ("onefs_open_file_ntcreate: fname=%s, after mapping "
737 "open_access_mask=%#x, access_mask=0x%x\n",
738 fname, open_access_mask, access_mask));
741 * Note that we ignore the append flag as append does not
742 * mean the same thing under DOS and Unix.
745 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
746 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
749 * DENY_DOS opens are always underlying read-write on the
750 * file handle, no matter what the requested access mask
751 * says. Stock samba just sets the flags, but since
752 * ifs_createfile uses the access_mask, it must be updated as
753 * well. This allows BASE-DENY* to pass.
755 if (create_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) {
757 DEBUG(10,("onefs_open_file_ntcreate: deny_dos: "
758 "Adding O_RDWR to flags "
759 "(0x%x) and some READ bits to "
760 "open_access_mask (0x%x)\n",
761 flags, open_access_mask));
763 flags = O_RDWR;
764 open_access_mask |= (FILE_READ_ATTRIBUTES |
765 FILE_READ_DATA | FILE_READ_EA | FILE_EXECUTE);
767 } else if (access_mask & (FILE_READ_ATTRIBUTES |
768 FILE_READ_DATA |
769 FILE_READ_EA |
770 FILE_EXECUTE)) {
771 flags = O_RDWR;
772 } else {
773 flags = O_WRONLY;
775 } else {
776 flags = O_RDONLY;
779 /* Currently we only look at FILE_WRITE_THROUGH for create options. */
780 #if defined(O_SYNC)
781 if ((create_options & FILE_WRITE_THROUGH) &&
782 lp_strict_sync(SNUM(conn))) {
783 flags2 |= O_SYNC;
785 #endif /* O_SYNC */
787 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
788 flags2 |= O_APPEND;
791 if (!posix_open && !CAN_WRITE(conn)) {
793 * We should really return a permission denied error if either
794 * O_CREAT or O_TRUNC are set, but for compatibility with
795 * older versions of Samba we just AND them out.
797 flags2 &= ~(O_CREAT|O_TRUNC);
799 /* Deny DELETE_ACCESS explicitly if the share is read only. */
800 if (access_mask & DELETE_ACCESS) {
801 return map_nt_error_from_unix(EACCES);
805 /* Ensure we can't write on a read-only share or file. */
806 if (flags != O_RDONLY && file_existed &&
807 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
808 DEBUG(5, ("onefs_open_file_ntcreate: write access requested "
809 "for file %s on read only %s\n",
810 fname, !CAN_WRITE(conn) ? "share" : "file" ));
811 errno = EACCES;
812 return NT_STATUS_ACCESS_DENIED;
815 DEBUG(10, ("fsp = %p\n", fsp));
817 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
818 fsp->share_access = share_access;
819 fsp->fh->private_options = create_options;
820 fsp->access_mask = open_access_mask; /* We change this to the
821 * requested access_mask after
822 * the open is done. */
823 fsp->posix_open = posix_open;
825 /* Ensure no SAMBA_PRIVATE bits can be set. */
826 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
828 if (timeval_is_zero(&request_time)) {
829 request_time = fsp->open_time;
832 if (file_existed) {
833 struct timespec old_write_time = get_mtimespec(psbuf);
834 id = vfs_file_id_from_sbuf(conn, psbuf);
836 lck = get_share_mode_lock(talloc_tos(), id,
837 conn->connectpath,
838 fname, &old_write_time);
840 if (lck == NULL) {
841 DEBUG(0, ("Could not get share mode lock\n"));
842 return NT_STATUS_SHARING_VIOLATION;
845 if (lck->delete_on_close) {
846 /* DELETE_PENDING is not deferred for a second */
847 TALLOC_FREE(lck);
848 return NT_STATUS_DELETE_PENDING;
852 SMB_ASSERT(!file_existed || (lck != NULL));
855 * Ensure we pay attention to default ACLs on directories. May be
856 * neccessary depending on ACL policies.
858 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
859 (def_acl = directory_has_default_acl(conn, parent_dir))) {
860 unx_mode = 0777;
863 DEBUG(4,("calling onefs_open_file with flags=0x%X flags2=0x%X "
864 "mode=0%o, access_mask = 0x%x, open_access_mask = 0x%x\n",
865 (unsigned int)flags, (unsigned int)flags2,
866 (unsigned int)unx_mode, (unsigned int)access_mask,
867 (unsigned int)open_access_mask));
870 * Since the open is guaranteed to be stat only if req == NULL, a
871 * callback record is only needed if req != NULL.
873 if (req) {
874 SMB_ASSERT(fsp_data);
875 oplock_callback_id = onefs_oplock_wait_record(req->mid);
876 if (oplock_callback_id == 0) {
877 return NT_STATUS_NO_MEMORY;
879 } else {
881 * It is also already asserted it's either a stream or a
882 * stat-only open at this point.
884 SMB_ASSERT(fsp->oplock_type == NO_OPLOCK);
887 /* Do the open. */
888 status = onefs_open_file(fsp,
889 conn,
890 req,
891 parent_dir,
892 newname,
893 fname,
894 psbuf,
895 flags|flags2,
896 unx_mode,
897 access_mask,
898 open_access_mask,
899 fsp->oplock_type,
900 oplock_callback_id,
901 share_access,
902 create_options,
903 createfile_attributes,
905 &granted_oplock);
907 if (!NT_STATUS_IS_OK(status)) {
909 /* OneFS Oplock Handling */
910 if (errno == EINPROGRESS) {
912 if (lck == NULL) {
914 struct deferred_open_record state;
915 struct timespec old_write_time;
917 old_write_time = get_mtimespec(psbuf);
919 DEBUG(3, ("Someone created file %s with an "
920 "oplock after we looked: Retrying\n",
921 fname));
923 * We hit the race that when we did the stat
924 * on the file it did not exist, and someone
925 * has created it in between the stat and the
926 * open_file() call. Just retry immediately.
928 id = vfs_file_id_from_sbuf(conn, psbuf);
929 if (!(lck = get_share_mode_lock(talloc_tos(),
930 id, conn->connectpath, fname,
931 &old_write_time))) {
933 * Emergency exit
935 DEBUG(0, ("onefs_open_file_ntcreate: "
936 "Could not get share mode "
937 "lock for %s\n", fname));
938 status = NT_STATUS_SHARING_VIOLATION;
939 goto cleanup_destroy;
942 state.delayed_for_oplocks = False;
943 state.id = id;
945 if (req != NULL) {
946 defer_open(lck, request_time,
947 timeval_zero(), req, &state);
949 goto cleanup_destroy;
951 /* Waiting for an oplock */
952 DEBUG(5,("Async createfile because a client has an "
953 "oplock on %s\n", fname));
955 SMB_ASSERT(req);
956 schedule_defer_open(lck, request_time, req);
957 goto cleanup;
960 /* Check for a sharing violation */
961 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
962 uint32 can_access_mask;
963 bool can_access = True;
965 /* Check if this can be done with the deny_dos and fcb
966 * calls. */
968 /* Try to find dup fsp if possible. */
969 if (create_options &
970 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
971 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
973 if (req == NULL) {
974 DEBUG(0, ("DOS open without an SMB "
975 "request!\n"));
976 status = NT_STATUS_INTERNAL_ERROR;
977 goto cleanup_destroy;
980 /* Use the client requested access mask here,
981 * not the one we open with. */
982 status = fcb_or_dos_open(req,
983 conn,
984 fsp,
985 fname,
987 req->smbpid,
988 req->vuid,
989 access_mask,
990 share_access,
991 create_options);
993 if (NT_STATUS_IS_OK(status)) {
994 TALLOC_FREE(lck);
995 if (pinfo) {
996 *pinfo = FILE_WAS_OPENED;
998 status = NT_STATUS_OK;
999 goto cleanup;
1004 * This next line is a subtlety we need for
1005 * MS-Access. If a file open will fail due to share
1006 * permissions and also for security (access) reasons,
1007 * we need to return the access failed error, not the
1008 * share error. We can't open the file due to kernel
1009 * oplock deadlock (it's possible we failed above on
1010 * the open_mode_check()) so use a userspace check.
1013 if (flags & O_RDWR) {
1014 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1015 } else if (flags & O_WRONLY) {
1016 can_access_mask = FILE_WRITE_DATA;
1017 } else {
1018 can_access_mask = FILE_READ_DATA;
1021 if (((can_access_mask & FILE_WRITE_DATA) && !CAN_WRITE(conn)) ||
1022 !can_access_file_data(conn,fname,psbuf,can_access_mask)) {
1023 can_access = False;
1027 * If we're returning a share violation, ensure we
1028 * cope with the braindead 1 second delay.
1030 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
1031 lp_defer_sharing_violations()) {
1032 struct timeval timeout;
1033 struct deferred_open_record state;
1034 int timeout_usecs;
1036 /* this is a hack to speed up torture tests
1037 in 'make test' */
1038 timeout_usecs = lp_parm_int(SNUM(conn),
1039 "smbd","sharedelay",
1040 SHARING_VIOLATION_USEC_WAIT);
1042 /* This is a relative time, added to the
1043 absolute request_time value to get the
1044 absolute timeout time. Note that if this
1045 is the second or greater time we enter this
1046 codepath for this particular request mid
1047 then request_time is left as the absolute
1048 time of the *first* time this request mid
1049 was processed. This is what allows the
1050 request to eventually time out. */
1052 timeout = timeval_set(0, timeout_usecs);
1054 /* Nothing actually uses
1055 state.delayed_for_oplocks but it's handy to
1056 differentiate in debug messages between a
1057 30 second delay due to oplock break, and a
1058 1 second delay for share mode conflicts. */
1060 state.delayed_for_oplocks = False;
1061 state.id = id;
1062 state.failed = false;
1064 if ((req != NULL)
1065 && !request_timed_out(request_time,
1066 timeout)) {
1067 defer_open(lck, request_time, timeout,
1068 req, &state);
1072 if (can_access) {
1074 * We have detected a sharing violation here
1075 * so return the correct error code
1077 status = NT_STATUS_SHARING_VIOLATION;
1078 } else {
1079 status = NT_STATUS_ACCESS_DENIED;
1082 goto cleanup_destroy;
1086 * Normal error, for example EACCES
1088 cleanup_destroy:
1089 if (oplock_callback_id != 0) {
1090 destroy_onefs_callback_record(oplock_callback_id);
1092 cleanup:
1093 TALLOC_FREE(lck);
1094 return status;
1097 fsp->oplock_type = granted_oplock;
1099 if (oplock_callback_id != 0) {
1100 onefs_set_oplock_callback(oplock_callback_id, fsp);
1101 fsp_data->oplock_callback_id = oplock_callback_id;
1102 } else {
1103 SMB_ASSERT(fsp->oplock_type == NO_OPLOCK);
1106 if (!file_existed) {
1107 struct timespec old_write_time = get_mtimespec(psbuf);
1109 * Deal with the race condition where two smbd's detect the
1110 * file doesn't exist and do the create at the same time. One
1111 * of them will win and set a share mode, the other (ie. this
1112 * one) should check if the requested share mode for this
1113 * create is allowed.
1117 * Now the file exists and fsp is successfully opened,
1118 * fsp->dev and fsp->inode are valid and should replace the
1119 * dev=0,inode=0 from a non existent file. Spotted by
1120 * Nadav Danieli <nadavd@exanet.com>. JRA.
1123 id = fsp->file_id;
1125 lck = get_share_mode_lock(talloc_tos(), id,
1126 conn->connectpath,
1127 fname, &old_write_time);
1129 if (lck == NULL) {
1130 DEBUG(0, ("onefs_open_file_ntcreate: Could not get "
1131 "share mode lock for %s\n", fname));
1132 fd_close(fsp);
1133 return NT_STATUS_SHARING_VIOLATION;
1136 if (lck->delete_on_close) {
1137 status = NT_STATUS_DELETE_PENDING;
1140 if (!NT_STATUS_IS_OK(status)) {
1141 struct deferred_open_record state;
1143 fd_close(fsp);
1145 state.delayed_for_oplocks = False;
1146 state.id = id;
1148 /* Do it all over again immediately. In the second
1149 * round we will find that the file existed and handle
1150 * the DELETE_PENDING and FCB cases correctly. No need
1151 * to duplicate the code here. Essentially this is a
1152 * "goto top of this function", but don't tell
1153 * anybody... */
1155 if (req != NULL) {
1156 defer_open(lck, request_time, timeval_zero(),
1157 req, &state);
1159 TALLOC_FREE(lck);
1160 return status;
1164 * We exit this block with the share entry *locked*.....
1169 SMB_ASSERT(lck != NULL);
1171 /* Delete streams if create_disposition requires it */
1172 if (file_existed && clear_ads && !is_ntfs_stream_name(fname)) {
1173 status = delete_all_streams(conn, fname);
1174 if (!NT_STATUS_IS_OK(status)) {
1175 TALLOC_FREE(lck);
1176 fd_close(fsp);
1177 return status;
1181 /* note that we ignore failure for the following. It is
1182 basically a hack for NFS, and NFS will never set one of
1183 these only read them. Nobody but Samba can ever set a deny
1184 mode and we have already checked our more authoritative
1185 locking database for permission to set this deny mode. If
1186 the kernel refuses the operations then the kernel is wrong.
1187 note that GPFS supports it as well - jmcd */
1189 if (fsp->fh->fd != -1) {
1190 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access);
1191 if(ret_flock == -1 ){
1193 TALLOC_FREE(lck);
1194 fd_close(fsp);
1195 return NT_STATUS_SHARING_VIOLATION;
1200 * At this point onwards, we can guarentee that the share entry
1201 * is locked, whether we created the file or not, and that the
1202 * deny mode is compatible with all current opens.
1205 /* Record the options we were opened with. */
1206 fsp->share_access = share_access;
1207 fsp->fh->private_options = create_options;
1209 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
1211 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
1213 if (file_existed) {
1214 /* stat opens on existing files don't get oplocks. */
1215 if (is_stat_open(open_access_mask)) {
1216 fsp->oplock_type = NO_OPLOCK;
1219 if (!(flags2 & O_TRUNC)) {
1220 info = FILE_WAS_OPENED;
1221 } else {
1222 info = FILE_WAS_OVERWRITTEN;
1224 } else {
1225 info = FILE_WAS_CREATED;
1228 if (pinfo) {
1229 *pinfo = info;
1233 * Setup the oplock info in both the shared memory and
1234 * file structs.
1237 if ((fsp->oplock_type != NO_OPLOCK) &&
1238 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
1239 if (!set_file_oplock(fsp, fsp->oplock_type)) {
1240 /* Could not get the kernel oplock */
1241 fsp->oplock_type = NO_OPLOCK;
1245 if (fsp->oplock_type == LEVEL_II_OPLOCK &&
1246 (!lp_level2_oplocks(SNUM(conn)) ||
1247 !(global_client_caps & CAP_LEVEL_II_OPLOCKS))) {
1249 DEBUG(5, ("Downgrading level2 oplock on open "
1250 "because level2 oplocks = off\n"));
1252 release_file_oplock(fsp);
1255 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED ||
1256 info == FILE_WAS_SUPERSEDED) {
1257 new_file_created = True;
1260 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0,
1261 fsp->oplock_type);
1263 /* Handle strange delete on close create semantics. */
1264 if (create_options & FILE_DELETE_ON_CLOSE) {
1265 status = can_set_delete_on_close(fsp, True, new_dos_attributes);
1267 if (!NT_STATUS_IS_OK(status)) {
1268 /* Remember to delete the mode we just added. */
1269 del_share_mode(lck, fsp);
1270 TALLOC_FREE(lck);
1271 fd_close(fsp);
1272 return status;
1274 /* Note that here we set the *inital* delete on close flag,
1275 not the regular one. The magic gets handled in close. */
1276 fsp->initial_delete_on_close = True;
1280 * Take care of inherited ACLs on created files - if default ACL not
1281 * selected.
1282 * May be necessary depending on acl policies.
1284 if (!posix_open && !file_existed && !def_acl && !(VALID_STAT(*psbuf)
1285 && (psbuf->st_flags & SF_HASNTFSACL))) {
1287 int saved_errno = errno; /* We might get ENOSYS in the next
1288 * call.. */
1290 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
1291 errno == ENOSYS) {
1292 errno = saved_errno; /* Ignore ENOSYS */
1295 } else if (new_unx_mode) {
1297 int ret = -1;
1299 /* Attributes need changing. File already existed. */
1302 int saved_errno = errno; /* We might get ENOSYS in the
1303 * next call.. */
1304 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
1306 if (ret == -1 && errno == ENOSYS) {
1307 errno = saved_errno; /* Ignore ENOSYS */
1308 } else {
1309 DEBUG(5, ("onefs_open_file_ntcreate: reset "
1310 "attributes of file %s to 0%o\n",
1311 fname, (unsigned int)new_unx_mode));
1312 ret = 0; /* Don't do the fchmod below. */
1316 if ((ret == -1) &&
1317 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
1318 DEBUG(5, ("onefs_open_file_ntcreate: failed to reset "
1319 "attributes of file %s to 0%o\n",
1320 fname, (unsigned int)new_unx_mode));
1323 /* If this is a successful open, we must remove any deferred open
1324 * records. */
1325 if (req != NULL) {
1326 del_deferred_open_entry(lck, req->mid);
1328 TALLOC_FREE(lck);
1330 return NT_STATUS_OK;
1334 /****************************************************************************
1335 Open a directory from an NT SMB call.
1336 ****************************************************************************/
1337 static NTSTATUS onefs_open_directory(connection_struct *conn,
1338 struct smb_request *req,
1339 const char *fname,
1340 uint32 access_mask,
1341 uint32 share_access,
1342 uint32 create_disposition,
1343 uint32 create_options,
1344 uint32 file_attributes,
1345 struct security_descriptor *sd,
1346 files_struct **result,
1347 int *pinfo,
1348 SMB_STRUCT_STAT *psbuf)
1350 files_struct *fsp = NULL;
1351 struct share_mode_lock *lck = NULL;
1352 NTSTATUS status;
1353 struct timespec mtimespec;
1354 int info = 0;
1355 char *parent_dir;
1356 const char *dirname;
1357 bool posix_open = false;
1358 uint32 create_flags = 0;
1359 uint32 mode = lp_dir_mask(SNUM(conn));
1361 DEBUG(5, ("onefs_open_directory: opening directory %s, "
1362 "access_mask = 0x%x, "
1363 "share_access = 0x%x create_options = 0x%x, "
1364 "create_disposition = 0x%x, file_attributes = 0x%x\n",
1365 fname, (unsigned int)access_mask, (unsigned int)share_access,
1366 (unsigned int)create_options, (unsigned int)create_disposition,
1367 (unsigned int)file_attributes));
1369 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
1370 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
1371 is_ntfs_stream_name(fname)) {
1372 DEBUG(2, ("onefs_open_directory: %s is a stream name!\n", fname));
1373 return NT_STATUS_NOT_A_DIRECTORY;
1376 switch (create_disposition) {
1377 case FILE_OPEN:
1378 /* If directory exists open. If directory doesn't
1379 * exist error. */
1380 create_flags = 0;
1381 info = FILE_WAS_OPENED;
1382 break;
1383 case FILE_CREATE:
1384 /* If directory exists error. If directory doesn't
1385 * exist create. */
1386 create_flags = O_CREAT | O_EXCL;
1387 info = FILE_WAS_CREATED;
1388 break;
1389 case FILE_OPEN_IF:
1390 /* If directory exists open. If directory doesn't
1391 * exist create. */
1393 /* Note: in order to return whether the directory was
1394 * opened or created, we first try to open and then try
1395 * to create. */
1396 create_flags = 0;
1397 info = FILE_WAS_OPENED;
1398 break;
1399 case FILE_SUPERSEDE:
1400 case FILE_OVERWRITE:
1401 case FILE_OVERWRITE_IF:
1402 default:
1403 DEBUG(5, ("onefs_open_directory: invalid "
1404 "create_disposition 0x%x for directory %s\n",
1405 (unsigned int)create_disposition, fname));
1406 return NT_STATUS_INVALID_PARAMETER;
1410 * Check for write access to the share. Done in mkdir_internal() in
1411 * mainline samba.
1413 if (!CAN_WRITE(conn) && (create_flags & O_CREAT)) {
1414 return NT_STATUS_ACCESS_DENIED;
1417 /* Get parent dirname */
1418 if (!parent_dirname(talloc_tos(), fname, &parent_dir, &dirname)) {
1419 return NT_STATUS_NO_MEMORY;
1422 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1423 posix_open = true;
1424 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1425 file_attributes = 0;
1426 } else {
1427 mode = unix_mode(conn, aDIR, fname, parent_dir);
1431 * The NONINDEXED and COMPRESSED bits seem to always be cleared on
1432 * directories, no matter if you specify that they should be set.
1434 file_attributes &=
1435 ~(FILE_ATTRIBUTE_NONINDEXED | FILE_ATTRIBUTE_COMPRESSED);
1437 status = file_new(req, conn, &fsp);
1438 if(!NT_STATUS_IS_OK(status)) {
1439 return status;
1443 * Actual open with retry magic to handle FILE_OPEN_IF which is
1444 * unique because the kernel won't tell us if the file was opened or
1445 * created.
1447 retry_open:
1448 fsp->fh->fd = onefs_sys_create_file(conn,
1450 fname,
1451 access_mask,
1452 access_mask,
1453 share_access,
1454 create_options,
1455 create_flags | O_DIRECTORY,
1456 mode,
1460 file_attributes,
1461 NULL);
1463 if (fsp->fh->fd == -1) {
1464 DEBUG(3, ("Error opening %s. Errno=%d (%s).\n", fname, errno,
1465 strerror(errno)));
1466 SMB_ASSERT(errno != EINPROGRESS);
1468 if (create_disposition == FILE_OPEN_IF) {
1469 if (errno == ENOENT) {
1470 /* Try again, creating it this time. */
1471 create_flags = O_CREAT | O_EXCL;
1472 info = FILE_WAS_CREATED;
1473 goto retry_open;
1474 } else if (errno == EEXIST) {
1475 /* Uggh. Try again again. */
1476 create_flags = 0;
1477 info = FILE_WAS_OPENED;
1478 goto retry_open;
1482 /* Error cases below: */
1483 file_free(req, fsp);
1485 if ((errno == ENOENT) && (create_disposition == FILE_OPEN)) {
1486 DEBUG(5,("onefs_open_directory: FILE_OPEN requested "
1487 "for directory %s and it doesn't "
1488 "exist.\n", fname ));
1489 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1490 } else if ((errno == EEXIST) &&
1491 (create_disposition == FILE_CREATE)) {
1492 DEBUG(5,("onefs_open_directory: FILE_CREATE "
1493 "requested for directory %s and it "
1494 "already exists.\n", fname ));
1495 return NT_STATUS_OBJECT_NAME_COLLISION;
1496 } else if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
1497 /* Catch sharing violations. */
1498 return NT_STATUS_SHARING_VIOLATION;
1501 return map_nt_error_from_unix(errno);
1504 if (info == FILE_WAS_CREATED) {
1506 /* Pulled from mkdir_internal() */
1507 if (SMB_VFS_LSTAT(conn, fname, psbuf) == -1) {
1508 DEBUG(2, ("Could not stat directory '%s' just "
1509 "created: %s\n",fname, strerror(errno)));
1510 return map_nt_error_from_unix(errno);
1513 if (!S_ISDIR(psbuf->st_mode)) {
1514 DEBUG(0, ("Directory just '%s' created is not a "
1515 "directory\n", fname));
1516 return NT_STATUS_ACCESS_DENIED;
1519 if (!posix_open) {
1521 * Check if high bits should have been set, then (if
1522 * bits are missing): add them. Consider bits
1523 * automagically set by UNIX, i.e. SGID bit from
1524 * parent dir.
1526 if (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) &&
1527 (mode & ~psbuf->st_mode)) {
1528 SMB_VFS_CHMOD(conn, fname, (psbuf->st_mode |
1529 (mode & ~psbuf->st_mode)));
1533 /* Change the owner if required. */
1534 if (lp_inherit_owner(SNUM(conn))) {
1535 change_dir_owner_to_parent(conn, parent_dir, fname,
1536 psbuf);
1539 notify_fname(conn, NOTIFY_ACTION_ADDED,
1540 FILE_NOTIFY_CHANGE_DIR_NAME, fname);
1543 /* Stat the fd for Samba bookkeeping. */
1544 if(SMB_VFS_FSTAT(fsp, psbuf) != 0) {
1545 fd_close(fsp);
1546 file_free(req, fsp);
1547 return map_nt_error_from_unix(errno);
1550 /* Setup the files_struct for it. */
1551 fsp->mode = psbuf->st_mode;
1552 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
1553 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
1554 fsp->file_pid = req ? req->smbpid : 0;
1555 fsp->can_lock = False;
1556 fsp->can_read = False;
1557 fsp->can_write = False;
1559 fsp->share_access = share_access;
1560 fsp->fh->private_options = create_options;
1562 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
1564 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
1565 fsp->print_file = False;
1566 fsp->modified = False;
1567 fsp->oplock_type = NO_OPLOCK;
1568 fsp->sent_oplock_break = NO_BREAK_SENT;
1569 fsp->is_directory = True;
1570 fsp->posix_open = posix_open;
1572 string_set(&fsp->fsp_name,fname);
1574 mtimespec = get_mtimespec(psbuf);
1577 * Still set the samba share mode lock for correct delete-on-close
1578 * semantics and to make smbstatus more useful.
1580 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
1581 conn->connectpath,
1582 fname, &mtimespec);
1584 if (lck == NULL) {
1585 DEBUG(0, ("onefs_open_directory: Could not get share mode "
1586 "lock for %s\n", fname));
1587 fd_close(fsp);
1588 file_free(req, fsp);
1589 return NT_STATUS_SHARING_VIOLATION;
1592 if (lck->delete_on_close) {
1593 TALLOC_FREE(lck);
1594 fd_close(fsp);
1595 file_free(req, fsp);
1596 return NT_STATUS_DELETE_PENDING;
1599 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0, NO_OPLOCK);
1602 * For directories the delete on close bit at open time seems
1603 * always to be honored on close... See test 19 in Samba4 BASE-DELETE.
1605 if (create_options & FILE_DELETE_ON_CLOSE) {
1606 status = can_set_delete_on_close(fsp, True, 0);
1607 if (!NT_STATUS_IS_OK(status) &&
1608 !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
1609 TALLOC_FREE(lck);
1610 fd_close(fsp);
1611 file_free(req, fsp);
1612 return status;
1615 if (NT_STATUS_IS_OK(status)) {
1616 /* Note that here we set the *inital* delete on close flag,
1617 not the regular one. The magic gets handled in close. */
1618 fsp->initial_delete_on_close = True;
1622 TALLOC_FREE(lck);
1624 if (pinfo) {
1625 *pinfo = info;
1628 *result = fsp;
1629 return NT_STATUS_OK;
1633 * Wrapper around onefs_open_file_ntcreate and onefs_open_directory.
1635 static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
1636 struct smb_request *req,
1637 const char *fname,
1638 uint32_t access_mask,
1639 uint32_t share_access,
1640 uint32_t create_disposition,
1641 uint32_t create_options,
1642 uint32_t file_attributes,
1643 uint32_t oplock_request,
1644 uint64_t allocation_size,
1645 struct security_descriptor *sd,
1646 struct ea_list *ea_list,
1647 files_struct **result,
1648 int *pinfo,
1649 struct onefs_fsp_data *fsp_data,
1650 SMB_STRUCT_STAT *psbuf)
1652 SMB_STRUCT_STAT sbuf;
1653 int info = FILE_WAS_OPENED;
1654 files_struct *base_fsp = NULL;
1655 files_struct *fsp = NULL;
1656 NTSTATUS status;
1658 DEBUG(10,("onefs_create_file_unixpath: access_mask = 0x%x "
1659 "file_attributes = 0x%x, share_access = 0x%x, "
1660 "create_disposition = 0x%x create_options = 0x%x "
1661 "oplock_request = 0x%x ea_list = 0x%p, sd = 0x%p, "
1662 "fname = %s\n",
1663 (unsigned int)access_mask,
1664 (unsigned int)file_attributes,
1665 (unsigned int)share_access,
1666 (unsigned int)create_disposition,
1667 (unsigned int)create_options,
1668 (unsigned int)oplock_request,
1669 ea_list, sd, fname));
1671 if (create_options & FILE_OPEN_BY_FILE_ID) {
1672 status = NT_STATUS_NOT_SUPPORTED;
1673 goto fail;
1676 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
1677 status = NT_STATUS_INVALID_PARAMETER;
1678 goto fail;
1681 if (req == NULL) {
1682 SMB_ASSERT((oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK) ==
1683 NO_OPLOCK);
1684 oplock_request |= INTERNAL_OPEN_ONLY;
1687 if (psbuf != NULL) {
1688 sbuf = *psbuf;
1690 else {
1691 if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
1692 SET_STAT_INVALID(sbuf);
1696 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
1697 && (access_mask & DELETE_ACCESS)
1698 && !is_ntfs_stream_name(fname)) {
1700 * We can't open a file with DELETE access if any of the
1701 * streams is open without FILE_SHARE_DELETE
1703 status = open_streams_for_delete(conn, fname);
1705 if (!NT_STATUS_IS_OK(status)) {
1706 goto fail;
1710 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
1711 && is_ntfs_stream_name(fname)) {
1712 char *base;
1713 uint32 base_create_disposition;
1715 if (create_options & FILE_DIRECTORY_FILE) {
1716 status = NT_STATUS_NOT_A_DIRECTORY;
1717 goto fail;
1720 status = onefs_split_ntfs_stream_name(talloc_tos(), fname,
1721 &base, NULL);
1722 if (!NT_STATUS_IS_OK(status)) {
1723 DEBUG(10, ("onefs_create_file_unixpath: "
1724 "split_ntfs_stream_name failed: %s\n",
1725 nt_errstr(status)));
1726 goto fail;
1729 SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
1731 switch (create_disposition) {
1732 case FILE_OPEN:
1733 base_create_disposition = FILE_OPEN;
1734 break;
1735 default:
1736 base_create_disposition = FILE_OPEN_IF;
1737 break;
1740 status = onefs_create_file_unixpath(
1741 conn, /* conn */
1742 NULL, /* req */
1743 base, /* fname */
1744 SYNCHRONIZE_ACCESS, /* access_mask */
1745 (FILE_SHARE_READ |
1746 FILE_SHARE_WRITE |
1747 FILE_SHARE_DELETE), /* share_access */
1748 base_create_disposition, /* create_disposition*/
1749 0, /* create_options */
1750 file_attributes, /* file_attributes */
1751 NO_OPLOCK, /* oplock_request */
1752 0, /* allocation_size */
1753 NULL, /* sd */
1754 NULL, /* ea_list */
1755 &base_fsp, /* result */
1756 NULL, /* pinfo */
1757 NULL, /* fsp_data */
1758 NULL); /* psbuf */
1760 if (!NT_STATUS_IS_OK(status)) {
1761 DEBUG(10, ("onefs_create_file_unixpath for base %s "
1762 "failed: %s\n", base, nt_errstr(status)));
1763 goto fail;
1767 * Testing against windows xp/2003/vista shows that oplocks
1768 * can actually be requested and granted on streams (see the
1769 * RAW-OPLOCK-STREAM1 smbtorture test).
1771 if ((oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK) !=
1772 NO_OPLOCK) {
1773 DEBUG(5, ("Oplock(%d) being requested on a stream! "
1774 "Ignoring oplock request: fname=%s\n",
1775 oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK,
1776 fname));
1777 /* Request NO_OPLOCK instead. */
1778 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1782 /* Covert generic bits in the security descriptor. */
1783 if (sd != NULL) {
1784 security_acl_map_generic(sd->dacl, &file_generic_mapping);
1785 security_acl_map_generic(sd->sacl, &file_generic_mapping);
1789 * If it's a request for a directory open, deal with it separately.
1792 if (create_options & FILE_DIRECTORY_FILE) {
1794 if (create_options & FILE_NON_DIRECTORY_FILE) {
1795 status = NT_STATUS_INVALID_PARAMETER;
1796 goto fail;
1799 /* Can't open a temp directory. IFS kit test. */
1800 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
1801 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
1802 status = NT_STATUS_INVALID_PARAMETER;
1803 goto fail;
1807 * We will get a create directory here if the Win32
1808 * app specified a security descriptor in the
1809 * CreateDirectory() call.
1812 status = onefs_open_directory(
1813 conn, /* conn */
1814 req, /* req */
1815 fname, /* fname */
1816 access_mask, /* access_mask */
1817 share_access, /* share_access */
1818 create_disposition, /* create_disposition*/
1819 create_options, /* create_options */
1820 file_attributes, /* file_attributes */
1821 sd, /* sd */
1822 &fsp, /* result */
1823 &info, /* pinfo */
1824 &sbuf); /* psbuf */
1825 } else {
1828 * Ordinary file case.
1831 status = file_new(req, conn, &fsp);
1832 if(!NT_STATUS_IS_OK(status)) {
1833 goto fail;
1837 * We're opening the stream element of a base_fsp
1838 * we already opened. Set up the base_fsp pointer.
1840 if (base_fsp) {
1841 fsp->base_fsp = base_fsp;
1844 status = onefs_open_file_ntcreate(
1845 conn, /* conn */
1846 req, /* req */
1847 fname, /* fname */
1848 access_mask, /* access_mask */
1849 share_access, /* share_access */
1850 create_disposition, /* create_disposition*/
1851 create_options, /* create_options */
1852 file_attributes, /* file_attributes */
1853 oplock_request, /* oplock_request */
1854 sd, /* sd */
1855 fsp, /* result */
1856 &info, /* pinfo */
1857 fsp_data, /* fsp_data */
1858 &sbuf); /* psbuf */
1860 if(!NT_STATUS_IS_OK(status)) {
1861 file_free(req, fsp);
1862 fsp = NULL;
1865 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
1867 /* A stream open never opens a directory */
1869 if (base_fsp) {
1870 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1871 goto fail;
1875 * Fail the open if it was explicitly a non-directory
1876 * file.
1879 if (create_options & FILE_NON_DIRECTORY_FILE) {
1880 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1881 goto fail;
1884 create_options |= FILE_DIRECTORY_FILE;
1886 status = onefs_open_directory(
1887 conn, /* conn */
1888 req, /* req */
1889 fname, /* fname */
1890 access_mask, /* access_mask */
1891 share_access, /* share_access */
1892 create_disposition, /* create_disposition*/
1893 create_options, /* create_options */
1894 file_attributes, /* file_attributes */
1895 sd, /* sd */
1896 &fsp, /* result */
1897 &info, /* pinfo */
1898 &sbuf); /* psbuf */
1902 if (!NT_STATUS_IS_OK(status)) {
1903 goto fail;
1906 fsp->base_fsp = base_fsp;
1908 SMB_ASSERT(fsp);
1910 if ((ea_list != NULL) && (info == FILE_WAS_CREATED)) {
1911 status = set_ea(conn, fsp, fname, ea_list);
1912 if (!NT_STATUS_IS_OK(status)) {
1913 goto fail;
1917 if (!fsp->is_directory && S_ISDIR(sbuf.st_mode)) {
1918 status = NT_STATUS_ACCESS_DENIED;
1919 goto fail;
1922 /* Save the requested allocation size. */
1923 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1924 if (allocation_size
1925 && (allocation_size > sbuf.st_size)) {
1926 fsp->initial_allocation_size = smb_roundup(
1927 fsp->conn, allocation_size);
1928 if (fsp->is_directory) {
1929 /* Can't set allocation size on a directory. */
1930 status = NT_STATUS_ACCESS_DENIED;
1931 goto fail;
1933 if (vfs_allocate_file_space(
1934 fsp, fsp->initial_allocation_size) == -1) {
1935 status = NT_STATUS_DISK_FULL;
1936 goto fail;
1938 } else {
1939 fsp->initial_allocation_size = smb_roundup(
1940 fsp->conn, (uint64_t)sbuf.st_size);
1944 DEBUG(10, ("onefs_create_file_unixpath: info=%d\n", info));
1946 *result = fsp;
1947 if (pinfo != NULL) {
1948 *pinfo = info;
1950 if (psbuf != NULL) {
1951 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
1952 *psbuf = sbuf;
1954 else {
1955 SMB_VFS_FSTAT(fsp, psbuf);
1958 return NT_STATUS_OK;
1960 fail:
1961 DEBUG(10, ("onefs_create_file_unixpath: %s\n", nt_errstr(status)));
1963 if (fsp != NULL) {
1964 if (base_fsp && fsp->base_fsp == base_fsp) {
1966 * The close_file below will close
1967 * fsp->base_fsp.
1969 base_fsp = NULL;
1971 close_file(req, fsp, ERROR_CLOSE);
1972 fsp = NULL;
1974 if (base_fsp != NULL) {
1975 close_file(req, base_fsp, ERROR_CLOSE);
1976 base_fsp = NULL;
1978 return status;
1981 static void destroy_onefs_fsp_data(void *p_data)
1983 struct onefs_fsp_data *fsp_data = (struct onefs_fsp_data *)p_data;
1985 destroy_onefs_callback_record(fsp_data->oplock_callback_id);
1989 * SMB_VFS_CREATE_FILE interface to onefs.
1991 NTSTATUS onefs_create_file(vfs_handle_struct *handle,
1992 struct smb_request *req,
1993 uint16_t root_dir_fid,
1994 const char *fname,
1995 uint32_t create_file_flags,
1996 uint32_t access_mask,
1997 uint32_t share_access,
1998 uint32_t create_disposition,
1999 uint32_t create_options,
2000 uint32_t file_attributes,
2001 uint32_t oplock_request,
2002 uint64_t allocation_size,
2003 struct security_descriptor *sd,
2004 struct ea_list *ea_list,
2005 files_struct **result,
2006 int *pinfo,
2007 SMB_STRUCT_STAT *psbuf)
2009 connection_struct *conn = handle->conn;
2010 struct case_semantics_state *case_state = NULL;
2011 struct onefs_fsp_data fsp_data = {};
2012 SMB_STRUCT_STAT sbuf;
2013 int info = FILE_WAS_OPENED;
2014 files_struct *fsp = NULL;
2015 NTSTATUS status;
2017 DEBUG(10,("onefs_create_file: access_mask = 0x%x "
2018 "file_attributes = 0x%x, share_access = 0x%x, "
2019 "create_disposition = 0x%x create_options = 0x%x "
2020 "oplock_request = 0x%x "
2021 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
2022 "create_file_flags = 0x%x, fname = %s\n",
2023 (unsigned int)access_mask,
2024 (unsigned int)file_attributes,
2025 (unsigned int)share_access,
2026 (unsigned int)create_disposition,
2027 (unsigned int)create_options,
2028 (unsigned int)oplock_request,
2029 (unsigned int)root_dir_fid,
2030 ea_list, sd, create_file_flags, fname));
2032 /* Get the file name if root_dir_fid was specified. */
2033 if (root_dir_fid != 0) {
2034 char *new_fname;
2036 status = get_relative_fid_filename(conn, req, root_dir_fid,
2037 fname, &new_fname);
2038 if (!NT_STATUS_IS_OK(status)) {
2039 goto fail;
2042 fname = new_fname;
2045 /* Resolve the file name if this was a DFS pathname. */
2046 if ((req != NULL) && (req->flags2 & FLAGS2_DFS_PATHNAMES)) {
2047 char *resolved_fname;
2049 status = resolve_dfspath(talloc_tos(), conn, true, fname,
2050 &resolved_fname);
2052 if (!NT_STATUS_IS_OK(status)) {
2054 * For PATH_NOT_COVERED we had
2055 * reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2056 * ERRSRV, ERRbadpath);
2057 * Need to fix in callers
2059 goto fail;
2061 fname = resolved_fname;
2064 /* Check if POSIX semantics are wanted. */
2065 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2066 case_state = set_posix_case_semantics(talloc_tos(), conn);
2069 /* Convert dos path to unix path if it hasn't already been done. */
2070 if (create_file_flags & CFF_DOS_PATH) {
2071 char *converted_fname;
2073 SET_STAT_INVALID(sbuf);
2075 status = unix_convert(talloc_tos(), conn, fname, False,
2076 &converted_fname, NULL, &sbuf);
2077 if (!NT_STATUS_IS_OK(status)) {
2078 goto fail;
2080 fname = converted_fname;
2081 } else {
2082 if (psbuf != NULL) {
2083 sbuf = *psbuf;
2084 } else {
2085 if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
2086 SET_STAT_INVALID(sbuf);
2092 TALLOC_FREE(case_state);
2094 /* All file access must go through check_name() */
2095 status = check_name(conn, fname);
2096 if (!NT_STATUS_IS_OK(status)) {
2097 goto fail;
2100 status = onefs_create_file_unixpath(
2101 conn, /* conn */
2102 req, /* req */
2103 fname, /* fname */
2104 access_mask, /* access_mask */
2105 share_access, /* share_access */
2106 create_disposition, /* create_disposition*/
2107 create_options, /* create_options */
2108 file_attributes, /* file_attributes */
2109 oplock_request, /* oplock_request */
2110 allocation_size, /* allocation_size */
2111 sd, /* sd */
2112 ea_list, /* ea_list */
2113 &fsp, /* result */
2114 &info, /* pinfo */
2115 &fsp_data, /* fsp_data */
2116 &sbuf); /* psbuf */
2118 if (!NT_STATUS_IS_OK(status)) {
2119 goto fail;
2122 DEBUG(10, ("onefs_create_file: info=%d\n", info));
2125 * Setup private onefs_fsp_data. Currently the private data struct is
2126 * only used to store the oplock_callback_id so that when the file is
2127 * closed, the onefs_callback_record can be properly cleaned up in the
2128 * oplock_onefs sub-system.
2130 if (fsp) {
2131 struct onefs_fsp_data *fsp_data_tmp = NULL;
2132 fsp_data_tmp = (struct onefs_fsp_data *)
2133 VFS_ADD_FSP_EXTENSION(handle, fsp, struct onefs_fsp_data,
2134 &destroy_onefs_fsp_data);
2136 if (fsp_data_tmp == NULL) {
2137 status = NT_STATUS_NO_MEMORY;
2138 goto fail;
2141 *fsp_data_tmp = fsp_data;
2144 *result = fsp;
2145 if (pinfo != NULL) {
2146 *pinfo = info;
2148 if (psbuf != NULL) {
2149 *psbuf = sbuf;
2151 return NT_STATUS_OK;
2153 fail:
2154 DEBUG(10, ("onefs_create_file: %s\n", nt_errstr(status)));
2156 if (fsp != NULL) {
2157 close_file(req, fsp, ERROR_CLOSE);
2158 fsp = NULL;
2160 return status;