source: trunk/samba-3.0.25pre1/source/smbd/open.c@ 1

Last change on this file since 1 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 62.7 KB
Line 
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
6 Copyright (C) Volker Lendecke 2005
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#include "includes.h"
24
25extern struct generic_mapping file_generic_mapping;
26extern struct current_user current_user;
27extern userdom_struct current_user_info;
28extern uint16 global_smbpid;
29extern BOOL global_client_failed_oplock_break;
30
31struct deferred_open_record {
32 BOOL delayed_for_oplocks;
33 SMB_DEV_T dev;
34 SMB_INO_T inode;
35};
36
37/****************************************************************************
38 fd support routines - attempt to do a dos_open.
39****************************************************************************/
40
41static NTSTATUS fd_open(struct connection_struct *conn,
42 const char *fname,
43 files_struct *fsp,
44 int flags,
45 mode_t mode)
46{
47 NTSTATUS status = NT_STATUS_OK;
48
49#ifdef O_NOFOLLOW
50 if (!lp_symlinks(SNUM(conn))) {
51 flags |= O_NOFOLLOW;
52 }
53#endif
54
55 fsp->fh->fd = SMB_VFS_OPEN(conn,fname,fsp,flags,mode);
56 if (fsp->fh->fd == -1) {
57 status = map_nt_error_from_unix(errno);
58 }
59
60 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
61 fname, flags, (int)mode, fsp->fh->fd,
62 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
63
64 return status;
65}
66
67/****************************************************************************
68 Close the file associated with a fsp.
69****************************************************************************/
70
71NTSTATUS fd_close(struct connection_struct *conn, files_struct *fsp)
72{
73 if (fsp->fh->fd == -1) {
74 return NT_STATUS_OK; /* What we used to call a stat open. */
75 }
76 if (fsp->fh->ref_count > 1) {
77 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
78 }
79 return fd_close_posix(conn, fsp);
80}
81
82/****************************************************************************
83 Change the ownership of a file to that of the parent directory.
84 Do this by fd if possible.
85****************************************************************************/
86
87static void change_file_owner_to_parent(connection_struct *conn,
88 const char *inherit_from_dir,
89 files_struct *fsp)
90{
91 SMB_STRUCT_STAT parent_st;
92 int ret;
93
94 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
95 if (ret == -1) {
96 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
97 "directory %s. Error was %s\n",
98 inherit_from_dir, strerror(errno) ));
99 return;
100 }
101
102 become_root();
103 ret = SMB_VFS_FCHOWN(fsp, fsp->fh->fd, parent_st.st_uid, (gid_t)-1);
104 unbecome_root();
105 if (ret == -1) {
106 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
107 "file %s to parent directory uid %u. Error "
108 "was %s\n", fsp->fsp_name,
109 (unsigned int)parent_st.st_uid,
110 strerror(errno) ));
111 }
112
113 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
114 "parent directory uid %u.\n", fsp->fsp_name,
115 (unsigned int)parent_st.st_uid ));
116}
117
118static void change_dir_owner_to_parent(connection_struct *conn,
119 const char *inherit_from_dir,
120 const char *fname,
121 SMB_STRUCT_STAT *psbuf)
122{
123 pstring saved_dir;
124 SMB_STRUCT_STAT sbuf;
125 SMB_STRUCT_STAT parent_st;
126 int ret;
127
128 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
129 if (ret == -1) {
130 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
131 "directory %s. Error was %s\n",
132 inherit_from_dir, strerror(errno) ));
133 return;
134 }
135
136 /* We've already done an lstat into psbuf, and we know it's a
137 directory. If we can cd into the directory and the dev/ino
138 are the same then we can safely chown without races as
139 we're locking the directory in place by being in it. This
140 should work on any UNIX (thanks tridge :-). JRA.
141 */
142
143 if (!vfs_GetWd(conn,saved_dir)) {
144 DEBUG(0,("change_dir_owner_to_parent: failed to get "
145 "current working directory\n"));
146 return;
147 }
148
149 /* Chdir into the new path. */
150 if (vfs_ChDir(conn, fname) == -1) {
151 DEBUG(0,("change_dir_owner_to_parent: failed to change "
152 "current working directory to %s. Error "
153 "was %s\n", fname, strerror(errno) ));
154 goto out;
155 }
156
157 if (SMB_VFS_STAT(conn,".",&sbuf) == -1) {
158 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
159 "directory '.' (%s) Error was %s\n",
160 fname, strerror(errno)));
161 goto out;
162 }
163
164 /* Ensure we're pointing at the same place. */
165 if (sbuf.st_dev != psbuf->st_dev ||
166 sbuf.st_ino != psbuf->st_ino ||
167 sbuf.st_mode != psbuf->st_mode ) {
168 DEBUG(0,("change_dir_owner_to_parent: "
169 "device/inode/mode on directory %s changed. "
170 "Refusing to chown !\n", fname ));
171 goto out;
172 }
173
174 become_root();
175 ret = SMB_VFS_CHOWN(conn, ".", parent_st.st_uid, (gid_t)-1);
176 unbecome_root();
177 if (ret == -1) {
178 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
179 "directory %s to parent directory uid %u. "
180 "Error was %s\n", fname,
181 (unsigned int)parent_st.st_uid, strerror(errno) ));
182 goto out;
183 }
184
185 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
186 "directory %s to parent directory uid %u.\n",
187 fname, (unsigned int)parent_st.st_uid ));
188
189 out:
190
191 vfs_ChDir(conn,saved_dir);
192}
193
194/****************************************************************************
195 Open a file.
196****************************************************************************/
197
198static NTSTATUS open_file(files_struct *fsp,
199 connection_struct *conn,
200 const char *parent_dir,
201 const char *name,
202 const char *path,
203 SMB_STRUCT_STAT *psbuf,
204 int flags,
205 mode_t unx_mode,
206 uint32 access_mask, /* client requested access mask. */
207 uint32 open_access_mask) /* what we're actually using in the open. */
208{
209 NTSTATUS status = NT_STATUS_OK;
210 int accmode = (flags & O_ACCMODE);
211 int local_flags = flags;
212 BOOL file_existed = VALID_STAT(*psbuf);
213
214 fsp->fh->fd = -1;
215 errno = EPERM;
216
217 /* Check permissions */
218
219 /*
220 * This code was changed after seeing a client open request
221 * containing the open mode of (DENY_WRITE/read-only) with
222 * the 'create if not exist' bit set. The previous code
223 * would fail to open the file read only on a read-only share
224 * as it was checking the flags parameter directly against O_RDONLY,
225 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
226 * JRA.
227 */
228
229 if (!CAN_WRITE(conn)) {
230 /* It's a read-only share - fail if we wanted to write. */
231 if(accmode != O_RDONLY) {
232 DEBUG(3,("Permission denied opening %s\n", path));
233 return NT_STATUS_ACCESS_DENIED;
234 } else if(flags & O_CREAT) {
235 /* We don't want to write - but we must make sure that
236 O_CREAT doesn't create the file if we have write
237 access into the directory.
238 */
239 flags &= ~O_CREAT;
240 local_flags &= ~O_CREAT;
241 }
242 }
243
244 /*
245 * This little piece of insanity is inspired by the
246 * fact that an NT client can open a file for O_RDONLY,
247 * but set the create disposition to FILE_EXISTS_TRUNCATE.
248 * If the client *can* write to the file, then it expects to
249 * truncate the file, even though it is opening for readonly.
250 * Quicken uses this stupid trick in backup file creation...
251 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
252 * for helping track this one down. It didn't bite us in 2.0.x
253 * as we always opened files read-write in that release. JRA.
254 */
255
256 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
257 DEBUG(10,("open_file: truncate requested on read-only open "
258 "for file %s\n", path));
259 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
260 }
261
262 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
263 (!file_existed && (local_flags & O_CREAT)) ||
264 ((local_flags & O_TRUNC) == O_TRUNC) ) {
265
266 /*
267 * We can't actually truncate here as the file may be locked.
268 * open_file_ntcreate will take care of the truncate later. JRA.
269 */
270
271 local_flags &= ~O_TRUNC;
272
273#if defined(O_NONBLOCK) && defined(S_ISFIFO)
274 /*
275 * We would block on opening a FIFO with no one else on the
276 * other end. Do what we used to do and add O_NONBLOCK to the
277 * open flags. JRA.
278 */
279
280 if (file_existed && S_ISFIFO(psbuf->st_mode)) {
281 local_flags |= O_NONBLOCK;
282 }
283#endif
284
285 /* Don't create files with Microsoft wildcard characters. */
286 if ((local_flags & O_CREAT) && !file_existed &&
287 ms_has_wild(path)) {
288 return NT_STATUS_OBJECT_NAME_INVALID;
289 }
290
291 /* Actually do the open */
292 status = fd_open(conn, path, fsp, local_flags, unx_mode);
293 if (!NT_STATUS_IS_OK(status)) {
294 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
295 "(flags=%d)\n",
296 path,nt_errstr(status),local_flags,flags));
297 return status;
298 }
299
300 if ((local_flags & O_CREAT) && !file_existed) {
301
302 /* Inherit the ACL if required */
303 if (lp_inherit_perms(SNUM(conn))) {
304 inherit_access_acl(conn, parent_dir, path,
305 unx_mode);
306 }
307
308 /* Change the owner if required. */
309 if (lp_inherit_owner(SNUM(conn))) {
310 change_file_owner_to_parent(conn, parent_dir,
311 fsp);
312 }
313
314 notify_fname(conn, NOTIFY_ACTION_ADDED,
315 FILE_NOTIFY_CHANGE_FILE_NAME, path);
316 }
317
318 } else {
319 fsp->fh->fd = -1; /* What we used to call a stat open. */
320 }
321
322 if (!file_existed) {
323 int ret;
324
325 if (fsp->fh->fd == -1) {
326 ret = SMB_VFS_STAT(conn, path, psbuf);
327 } else {
328 ret = SMB_VFS_FSTAT(fsp,fsp->fh->fd,psbuf);
329 /* If we have an fd, this stat should succeed. */
330 if (ret == -1) {
331 DEBUG(0,("Error doing fstat on open file %s "
332 "(%s)\n", path,strerror(errno) ));
333 }
334 }
335
336 /* For a non-io open, this stat failing means file not found. JRA */
337 if (ret == -1) {
338 status = map_nt_error_from_unix(errno);
339 fd_close(conn, fsp);
340 return status;
341 }
342 }
343
344 /*
345 * POSIX allows read-only opens of directories. We don't
346 * want to do this (we use a different code path for this)
347 * so catch a directory open and return an EISDIR. JRA.
348 */
349
350 if(S_ISDIR(psbuf->st_mode)) {
351 fd_close(conn, fsp);
352 errno = EISDIR;
353 return NT_STATUS_FILE_IS_A_DIRECTORY;
354 }
355
356 fsp->mode = psbuf->st_mode;
357 fsp->inode = psbuf->st_ino;
358 fsp->dev = psbuf->st_dev;
359 fsp->vuid = current_user.vuid;
360 fsp->file_pid = global_smbpid;
361 fsp->can_lock = True;
362 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
363 if (!CAN_WRITE(conn)) {
364 fsp->can_write = False;
365 } else {
366 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
367 True : False;
368 }
369 fsp->print_file = False;
370 fsp->modified = False;
371 fsp->sent_oplock_break = NO_BREAK_SENT;
372 fsp->is_directory = False;
373 fsp->is_stat = False;
374 if (conn->aio_write_behind_list &&
375 is_in_path(path, conn->aio_write_behind_list, conn->case_sensitive)) {
376 fsp->aio_write_behind = True;
377 }
378
379 string_set(&fsp->fsp_name, path);
380 fsp->wcp = NULL; /* Write cache pointer. */
381
382 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
383 *current_user_info.smb_name ?
384 current_user_info.smb_name : conn->user,fsp->fsp_name,
385 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
386 conn->num_files_open + 1));
387
388 errno = 0;
389 return NT_STATUS_OK;
390}
391
392/*******************************************************************
393 Return True if the filename is one of the special executable types.
394********************************************************************/
395
396static BOOL is_executable(const char *fname)
397{
398 if ((fname = strrchr_m(fname,'.'))) {
399 if (strequal(fname,".com") ||
400 strequal(fname,".dll") ||
401 strequal(fname,".exe") ||
402 strequal(fname,".sym")) {
403 return True;
404 }
405 }
406 return False;
407}
408
409/****************************************************************************
410 Check if we can open a file with a share mode.
411 Returns True if conflict, False if not.
412****************************************************************************/
413
414static BOOL share_conflict(struct share_mode_entry *entry,
415 uint32 access_mask,
416 uint32 share_access)
417{
418 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
419 "entry->share_access = 0x%x, "
420 "entry->private_options = 0x%x\n",
421 (unsigned int)entry->access_mask,
422 (unsigned int)entry->share_access,
423 (unsigned int)entry->private_options));
424
425 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
426 (unsigned int)access_mask, (unsigned int)share_access));
427
428 if ((entry->access_mask & (FILE_WRITE_DATA|
429 FILE_APPEND_DATA|
430 FILE_READ_DATA|
431 FILE_EXECUTE|
432 DELETE_ACCESS)) == 0) {
433 DEBUG(10,("share_conflict: No conflict due to "
434 "entry->access_mask = 0x%x\n",
435 (unsigned int)entry->access_mask ));
436 return False;
437 }
438
439 if ((access_mask & (FILE_WRITE_DATA|
440 FILE_APPEND_DATA|
441 FILE_READ_DATA|
442 FILE_EXECUTE|
443 DELETE_ACCESS)) == 0) {
444 DEBUG(10,("share_conflict: No conflict due to "
445 "access_mask = 0x%x\n",
446 (unsigned int)access_mask ));
447 return False;
448 }
449
450#if 1 /* JRA TEST - Superdebug. */
451#define CHECK_MASK(num, am, right, sa, share) \
452 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
453 (unsigned int)(num), (unsigned int)(am), \
454 (unsigned int)(right), (unsigned int)(am)&(right) )); \
455 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
456 (unsigned int)(num), (unsigned int)(sa), \
457 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
458 if (((am) & (right)) && !((sa) & (share))) { \
459 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
460sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
461 (unsigned int)(share) )); \
462 return True; \
463 }
464#else
465#define CHECK_MASK(num, am, right, sa, share) \
466 if (((am) & (right)) && !((sa) & (share))) { \
467 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
468sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
469 (unsigned int)(share) )); \
470 return True; \
471 }
472#endif
473
474 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
475 share_access, FILE_SHARE_WRITE);
476 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
477 entry->share_access, FILE_SHARE_WRITE);
478
479 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
480 share_access, FILE_SHARE_READ);
481 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
482 entry->share_access, FILE_SHARE_READ);
483
484 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
485 share_access, FILE_SHARE_DELETE);
486 CHECK_MASK(6, access_mask, DELETE_ACCESS,
487 entry->share_access, FILE_SHARE_DELETE);
488
489 DEBUG(10,("share_conflict: No conflict.\n"));
490 return False;
491}
492
493#if defined(DEVELOPER)
494static void validate_my_share_entries(int num,
495 struct share_mode_entry *share_entry)
496{
497 files_struct *fsp;
498
499 if (!procid_is_me(&share_entry->pid)) {
500 return;
501 }
502
503 if (is_deferred_open_entry(share_entry) &&
504 !open_was_deferred(share_entry->op_mid)) {
505 pstring str;
506 pstr_sprintf(str, "Got a deferred entry without a request: "
507 "PANIC: %s\n", share_mode_str(num, share_entry));
508 smb_panic(str);
509 }
510
511 if (!is_valid_share_mode_entry(share_entry)) {
512 return;
513 }
514
515 fsp = file_find_dif(share_entry->dev, share_entry->inode,
516 share_entry->share_file_id);
517 if (!fsp) {
518 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
519 share_mode_str(num, share_entry) ));
520 smb_panic("validate_my_share_entries: Cannot match a "
521 "share entry with an open file\n");
522 }
523
524 if (is_deferred_open_entry(share_entry) ||
525 is_unused_share_mode_entry(share_entry)) {
526 goto panic;
527 }
528
529 if ((share_entry->op_type == NO_OPLOCK) &&
530 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
531 /* Someone has already written to it, but I haven't yet
532 * noticed */
533 return;
534 }
535
536 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
537 goto panic;
538 }
539
540 return;
541
542 panic:
543 {
544 pstring str;
545 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
546 share_mode_str(num, share_entry) ));
547 slprintf(str, sizeof(str)-1, "validate_my_share_entries: "
548 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
549 fsp->fsp_name, (unsigned int)fsp->oplock_type,
550 (unsigned int)share_entry->op_type );
551 smb_panic(str);
552 }
553}
554#endif
555
556static BOOL is_stat_open(uint32 access_mask)
557{
558 return (access_mask &&
559 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
560 FILE_WRITE_ATTRIBUTES))==0) &&
561 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
562 FILE_WRITE_ATTRIBUTES)) != 0));
563}
564
565/****************************************************************************
566 Deal with share modes
567 Invarient: Share mode must be locked on entry and exit.
568 Returns -1 on error, or number of share modes on success (may be zero).
569****************************************************************************/
570
571static NTSTATUS open_mode_check(connection_struct *conn,
572 const char *fname,
573 struct share_mode_lock *lck,
574 uint32 access_mask,
575 uint32 share_access,
576 uint32 create_options,
577 BOOL *file_existed)
578{
579 int i;
580
581 if(lck->num_share_modes == 0) {
582 return NT_STATUS_OK;
583 }
584
585 *file_existed = True;
586
587 if (is_stat_open(access_mask)) {
588 /* Stat open that doesn't trigger oplock breaks or share mode
589 * checks... ! JRA. */
590 return NT_STATUS_OK;
591 }
592
593 /* A delete on close prohibits everything */
594
595 if (lck->delete_on_close) {
596 return NT_STATUS_DELETE_PENDING;
597 }
598
599 /*
600 * Check if the share modes will give us access.
601 */
602
603#if defined(DEVELOPER)
604 for(i = 0; i < lck->num_share_modes; i++) {
605 validate_my_share_entries(i, &lck->share_modes[i]);
606 }
607#endif
608
609 if (!lp_share_modes(SNUM(conn))) {
610 return NT_STATUS_OK;
611 }
612
613 /* Now we check the share modes, after any oplock breaks. */
614 for(i = 0; i < lck->num_share_modes; i++) {
615
616 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
617 continue;
618 }
619
620 /* someone else has a share lock on it, check to see if we can
621 * too */
622 if (share_conflict(&lck->share_modes[i],
623 access_mask, share_access)) {
624 return NT_STATUS_SHARING_VIOLATION;
625 }
626 }
627
628 return NT_STATUS_OK;
629}
630
631static BOOL is_delete_request(files_struct *fsp) {
632 return ((fsp->access_mask == DELETE_ACCESS) &&
633 (fsp->oplock_type == NO_OPLOCK));
634}
635
636/*
637 * 1) No files open at all or internal open: Grant whatever the client wants.
638 *
639 * 2) Exclusive (or batch) oplock around: If the requested access is a delete
640 * request, break if the oplock around is a batch oplock. If it's another
641 * requested access type, break.
642 *
643 * 3) Only level2 around: Grant level2 and do nothing else.
644 */
645
646static BOOL delay_for_oplocks(struct share_mode_lock *lck,
647 files_struct *fsp,
648 int pass_number,
649 int oplock_request)
650{
651 int i;
652 struct share_mode_entry *exclusive = NULL;
653 BOOL valid_entry = False;
654 BOOL delay_it = False;
655 BOOL have_level2 = False;
656 NTSTATUS status;
657 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
658
659 if (oplock_request & INTERNAL_OPEN_ONLY) {
660 fsp->oplock_type = NO_OPLOCK;
661 }
662
663 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
664 return False;
665 }
666
667 for (i=0; i<lck->num_share_modes; i++) {
668
669 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
670 continue;
671 }
672
673 /* At least one entry is not an invalid or deferred entry. */
674 valid_entry = True;
675
676 if (pass_number == 1) {
677 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
678 SMB_ASSERT(exclusive == NULL);
679 exclusive = &lck->share_modes[i];
680 }
681 } else {
682 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
683 SMB_ASSERT(exclusive == NULL);
684 exclusive = &lck->share_modes[i];
685 }
686 }
687
688 if (lck->share_modes[i].op_type == LEVEL_II_OPLOCK) {
689 SMB_ASSERT(exclusive == NULL);
690 have_level2 = True;
691 }
692 }
693
694 if (!valid_entry) {
695 /* All entries are placeholders or deferred.
696 * Directly grant whatever the client wants. */
697 if (fsp->oplock_type == NO_OPLOCK) {
698 /* Store a level2 oplock, but don't tell the client */
699 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
700 }
701 return False;
702 }
703
704 if (exclusive != NULL) { /* Found an exclusive oplock */
705 SMB_ASSERT(!have_level2);
706 delay_it = is_delete_request(fsp) ?
707 BATCH_OPLOCK_TYPE(exclusive->op_type) : True;
708 }
709
710 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
711 /* We can at most grant level2 as there are other
712 * level2 or NO_OPLOCK entries. */
713 fsp->oplock_type = LEVEL_II_OPLOCK;
714 }
715
716 if ((fsp->oplock_type == NO_OPLOCK) && have_level2) {
717 /* Store a level2 oplock, but don't tell the client */
718 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
719 }
720
721 if (!delay_it) {
722 return False;
723 }
724
725 /*
726 * Send a break message to the oplock holder and delay the open for
727 * our client.
728 */
729
730 DEBUG(10, ("Sending break request to PID %s\n",
731 procid_str_static(&exclusive->pid)));
732 exclusive->op_mid = get_current_mid();
733
734 /* Create the message. */
735 share_mode_entry_to_message(msg, exclusive);
736
737 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
738 don't want this set in the share mode struct pointed to by lck. */
739
740 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
741 SSVAL(msg,6,exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
742 }
743
744 status = message_send_pid(exclusive->pid, MSG_SMB_BREAK_REQUEST,
745 msg, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
746 if (!NT_STATUS_IS_OK(status)) {
747 DEBUG(3, ("Could not send oplock break message: %s\n",
748 nt_errstr(status)));
749 }
750
751 return True;
752}
753
754static BOOL request_timed_out(struct timeval request_time,
755 struct timeval timeout)
756{
757 struct timeval now, end_time;
758 GetTimeOfDay(&now);
759 end_time = timeval_sum(&request_time, &timeout);
760 return (timeval_compare(&end_time, &now) < 0);
761}
762
763/****************************************************************************
764 Handle the 1 second delay in returning a SHARING_VIOLATION error.
765****************************************************************************/
766
767static void defer_open(struct share_mode_lock *lck,
768 struct timeval request_time,
769 struct timeval timeout,
770 struct deferred_open_record *state)
771{
772 uint16 mid = get_current_mid();
773 int i;
774
775 /* Paranoia check */
776
777 for (i=0; i<lck->num_share_modes; i++) {
778 struct share_mode_entry *e = &lck->share_modes[i];
779
780 if (!is_deferred_open_entry(e)) {
781 continue;
782 }
783
784 if (procid_is_me(&e->pid) && (e->op_mid == mid)) {
785 DEBUG(0, ("Trying to defer an already deferred "
786 "request: mid=%d, exiting\n", mid));
787 exit_server("attempt to defer a deferred request");
788 }
789 }
790
791 /* End paranoia check */
792
793 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
794 "open entry for mid %u\n",
795 (unsigned int)request_time.tv_sec,
796 (unsigned int)request_time.tv_usec,
797 (unsigned int)mid));
798
799 if (!push_deferred_smb_message(mid, request_time, timeout,
800 (char *)state, sizeof(*state))) {
801 exit_server("push_deferred_smb_message failed");
802 }
803 add_deferred_open(lck, mid, request_time, state->dev, state->inode);
804
805 /*
806 * Push the MID of this packet on the signing queue.
807 * We only do this once, the first time we push the packet
808 * onto the deferred open queue, as this has a side effect
809 * of incrementing the response sequence number.
810 */
811
812 srv_defer_sign_response(mid);
813}
814
815
816/****************************************************************************
817 On overwrite open ensure that the attributes match.
818****************************************************************************/
819
820static BOOL open_match_attributes(connection_struct *conn,
821 const char *path,
822 uint32 old_dos_attr,
823 uint32 new_dos_attr,
824 mode_t existing_unx_mode,
825 mode_t new_unx_mode,
826 mode_t *returned_unx_mode)
827{
828 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
829
830 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
831 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
832
833 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
834 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
835 *returned_unx_mode = new_unx_mode;
836 } else {
837 *returned_unx_mode = (mode_t)0;
838 }
839
840 DEBUG(10,("open_match_attributes: file %s old_dos_attr = 0x%x, "
841 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
842 "returned_unx_mode = 0%o\n",
843 path,
844 (unsigned int)old_dos_attr,
845 (unsigned int)existing_unx_mode,
846 (unsigned int)new_dos_attr,
847 (unsigned int)*returned_unx_mode ));
848
849 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
850 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
851 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
852 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
853 return False;
854 }
855 }
856 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
857 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
858 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
859 return False;
860 }
861 }
862 return True;
863}
864
865/****************************************************************************
866 Special FCB or DOS processing in the case of a sharing violation.
867 Try and find a duplicated file handle.
868****************************************************************************/
869
870static files_struct *fcb_or_dos_open(connection_struct *conn,
871 const char *fname, SMB_DEV_T dev,
872 SMB_INO_T inode,
873 uint32 access_mask,
874 uint32 share_access,
875 uint32 create_options)
876{
877 files_struct *fsp;
878 files_struct *dup_fsp;
879
880 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
881 "file %s.\n", fname ));
882
883 for(fsp = file_find_di_first(dev, inode); fsp;
884 fsp = file_find_di_next(fsp)) {
885
886 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
887 "vuid = %u, file_pid = %u, private_options = 0x%x "
888 "access_mask = 0x%x\n", fsp->fsp_name,
889 fsp->fh->fd, (unsigned int)fsp->vuid,
890 (unsigned int)fsp->file_pid,
891 (unsigned int)fsp->fh->private_options,
892 (unsigned int)fsp->access_mask ));
893
894 if (fsp->fh->fd != -1 &&
895 fsp->vuid == current_user.vuid &&
896 fsp->file_pid == global_smbpid &&
897 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
898 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
899 (fsp->access_mask & FILE_WRITE_DATA) &&
900 strequal(fsp->fsp_name, fname)) {
901 DEBUG(10,("fcb_or_dos_open: file match\n"));
902 break;
903 }
904 }
905
906 if (!fsp) {
907 return NULL;
908 }
909
910 /* quite an insane set of semantics ... */
911 if (is_executable(fname) &&
912 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
913 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
914 return NULL;
915 }
916
917 /* We need to duplicate this fsp. */
918 if (!NT_STATUS_IS_OK(dup_file_fsp(fsp, access_mask, share_access,
919 create_options, &dup_fsp))) {
920 return NULL;
921 }
922
923 return dup_fsp;
924}
925
926/****************************************************************************
927 Open a file with a share mode - old openX method - map into NTCreate.
928****************************************************************************/
929
930BOOL map_open_params_to_ntcreate(const char *fname, int deny_mode, int open_func,
931 uint32 *paccess_mask,
932 uint32 *pshare_mode,
933 uint32 *pcreate_disposition,
934 uint32 *pcreate_options)
935{
936 uint32 access_mask;
937 uint32 share_mode;
938 uint32 create_disposition;
939 uint32 create_options = 0;
940
941 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
942 "open_func = 0x%x\n",
943 fname, (unsigned int)deny_mode, (unsigned int)open_func ));
944
945 /* Create the NT compatible access_mask. */
946 switch (GET_OPENX_MODE(deny_mode)) {
947 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
948 case DOS_OPEN_RDONLY:
949 access_mask = FILE_GENERIC_READ;
950 break;
951 case DOS_OPEN_WRONLY:
952 access_mask = FILE_GENERIC_WRITE;
953 break;
954 case DOS_OPEN_RDWR:
955 case DOS_OPEN_FCB:
956 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
957 break;
958 default:
959 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
960 (unsigned int)GET_OPENX_MODE(deny_mode)));
961 return False;
962 }
963
964 /* Create the NT compatible create_disposition. */
965 switch (open_func) {
966 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
967 create_disposition = FILE_CREATE;
968 break;
969
970 case OPENX_FILE_EXISTS_OPEN:
971 create_disposition = FILE_OPEN;
972 break;
973
974 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
975 create_disposition = FILE_OPEN_IF;
976 break;
977
978 case OPENX_FILE_EXISTS_TRUNCATE:
979 create_disposition = FILE_OVERWRITE;
980 break;
981
982 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
983 create_disposition = FILE_OVERWRITE_IF;
984 break;
985
986 default:
987 /* From samba4 - to be confirmed. */
988 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
989 create_disposition = FILE_CREATE;
990 break;
991 }
992 DEBUG(10,("map_open_params_to_ntcreate: bad "
993 "open_func 0x%x\n", (unsigned int)open_func));
994 return False;
995 }
996
997 /* Create the NT compatible share modes. */
998 switch (GET_DENY_MODE(deny_mode)) {
999 case DENY_ALL:
1000 share_mode = FILE_SHARE_NONE;
1001 break;
1002
1003 case DENY_WRITE:
1004 share_mode = FILE_SHARE_READ;
1005 break;
1006
1007 case DENY_READ:
1008 share_mode = FILE_SHARE_WRITE;
1009 break;
1010
1011 case DENY_NONE:
1012 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1013 break;
1014
1015 case DENY_DOS:
1016 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1017 if (is_executable(fname)) {
1018 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1019 } else {
1020 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1021 share_mode = FILE_SHARE_READ;
1022 } else {
1023 share_mode = FILE_SHARE_NONE;
1024 }
1025 }
1026 break;
1027
1028 case DENY_FCB:
1029 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1030 share_mode = FILE_SHARE_NONE;
1031 break;
1032
1033 default:
1034 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1035 (unsigned int)GET_DENY_MODE(deny_mode) ));
1036 return False;
1037 }
1038
1039 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1040 "share_mode = 0x%x, create_disposition = 0x%x, "
1041 "create_options = 0x%x\n",
1042 fname,
1043 (unsigned int)access_mask,
1044 (unsigned int)share_mode,
1045 (unsigned int)create_disposition,
1046 (unsigned int)create_options ));
1047
1048 if (paccess_mask) {
1049 *paccess_mask = access_mask;
1050 }
1051 if (pshare_mode) {
1052 *pshare_mode = share_mode;
1053 }
1054 if (pcreate_disposition) {
1055 *pcreate_disposition = create_disposition;
1056 }
1057 if (pcreate_options) {
1058 *pcreate_options = create_options;
1059 }
1060
1061 return True;
1062
1063}
1064
1065static void schedule_defer_open(struct share_mode_lock *lck, struct timeval request_time)
1066{
1067 struct deferred_open_record state;
1068
1069 /* This is a relative time, added to the absolute
1070 request_time value to get the absolute timeout time.
1071 Note that if this is the second or greater time we enter
1072 this codepath for this particular request mid then
1073 request_time is left as the absolute time of the *first*
1074 time this request mid was processed. This is what allows
1075 the request to eventually time out. */
1076
1077 struct timeval timeout;
1078
1079 /* Normally the smbd we asked should respond within
1080 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1081 * the client did, give twice the timeout as a safety
1082 * measure here in case the other smbd is stuck
1083 * somewhere else. */
1084
1085 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1086
1087 /* Nothing actually uses state.delayed_for_oplocks
1088 but it's handy to differentiate in debug messages
1089 between a 30 second delay due to oplock break, and
1090 a 1 second delay for share mode conflicts. */
1091
1092 state.delayed_for_oplocks = True;
1093 state.dev = lck->dev;
1094 state.inode = lck->ino;
1095
1096 if (!request_timed_out(request_time, timeout)) {
1097 defer_open(lck, request_time, timeout, &state);
1098 }
1099}
1100
1101/****************************************************************************
1102 Open a file with a share mode.
1103****************************************************************************/
1104
1105NTSTATUS open_file_ntcreate(connection_struct *conn,
1106 const char *fname,
1107 SMB_STRUCT_STAT *psbuf,
1108 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1109 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1110 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1111 uint32 create_options, /* options such as delete on close. */
1112 uint32 new_dos_attributes, /* attributes used for new file. */
1113 int oplock_request, /* internal Samba oplock codes. */
1114 /* Information (FILE_EXISTS etc.) */
1115 int *pinfo,
1116 files_struct **result)
1117{
1118 int flags=0;
1119 int flags2=0;
1120 BOOL file_existed = VALID_STAT(*psbuf);
1121 BOOL def_acl = False;
1122 BOOL posix_open = False;
1123 SMB_DEV_T dev = 0;
1124 SMB_INO_T inode = 0;
1125 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1126 files_struct *fsp = NULL;
1127 mode_t new_unx_mode = (mode_t)0;
1128 mode_t unx_mode = (mode_t)0;
1129 int info;
1130 uint32 existing_dos_attributes = 0;
1131 struct pending_message_list *pml = NULL;
1132 uint16 mid = get_current_mid();
1133 struct timeval request_time = timeval_zero();
1134 struct share_mode_lock *lck = NULL;
1135 uint32 open_access_mask = access_mask;
1136 NTSTATUS status;
1137 int ret_flock;
1138 char *parent_dir;
1139 const char *newname;
1140
1141 if (conn->printer) {
1142 /*
1143 * Printers are handled completely differently.
1144 * Most of the passed parameters are ignored.
1145 */
1146
1147 if (pinfo) {
1148 *pinfo = FILE_WAS_CREATED;
1149 }
1150
1151 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n", fname));
1152
1153 return print_fsp_open(conn, fname, result);
1154 }
1155
1156 if (!parent_dirname_talloc(tmp_talloc_ctx(), fname, &parent_dir,
1157 &newname)) {
1158 return NT_STATUS_NO_MEMORY;
1159 }
1160
1161 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1162 posix_open = True;
1163 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1164 new_dos_attributes = 0;
1165 } else {
1166 /* We add aARCH to this as this mode is only used if the file is
1167 * created new. */
1168 unx_mode = unix_mode(conn, new_dos_attributes | aARCH, fname,
1169 parent_dir);
1170 }
1171
1172 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1173 "access_mask=0x%x share_access=0x%x "
1174 "create_disposition = 0x%x create_options=0x%x "
1175 "unix mode=0%o oplock_request=%d\n",
1176 fname, new_dos_attributes, access_mask, share_access,
1177 create_disposition, create_options, unx_mode,
1178 oplock_request));
1179
1180 if ((pml = get_open_deferred_message(mid)) != NULL) {
1181 struct deferred_open_record *state =
1182 (struct deferred_open_record *)pml->private_data.data;
1183
1184 /* Remember the absolute time of the original
1185 request with this mid. We'll use it later to
1186 see if this has timed out. */
1187
1188 request_time = pml->request_time;
1189
1190 /* Remove the deferred open entry under lock. */
1191 lck = get_share_mode_lock(NULL, state->dev, state->inode, NULL, NULL);
1192 if (lck == NULL) {
1193 DEBUG(0, ("could not get share mode lock\n"));
1194 } else {
1195 del_deferred_open_entry(lck, mid);
1196 TALLOC_FREE(lck);
1197 }
1198
1199 /* Ensure we don't reprocess this message. */
1200 remove_deferred_open_smb_message(mid);
1201 }
1202
1203 status = check_name(conn, fname);
1204 if (!NT_STATUS_IS_OK(status)) {
1205 return status;
1206 }
1207
1208 if (!posix_open) {
1209 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1210 if (file_existed) {
1211 existing_dos_attributes = dos_mode(conn, fname, psbuf);
1212 }
1213 }
1214
1215 /* ignore any oplock requests if oplocks are disabled */
1216 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
1217 IS_VETO_OPLOCK_PATH(conn, fname)) {
1218 /* Mask off everything except the private Samba bits. */
1219 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1220 }
1221
1222 /* this is for OS/2 long file names - say we don't support them */
1223 if (!lp_posix_pathnames() && strstr(fname,".+,;=[].")) {
1224 /* OS/2 Workplace shell fix may be main code stream in a later
1225 * release. */
1226 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1227 "supported.\n"));
1228 if (use_nt_status()) {
1229 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1230 }
1231 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1232 }
1233
1234 switch( create_disposition ) {
1235 /*
1236 * Currently we're using FILE_SUPERSEDE as the same as
1237 * FILE_OVERWRITE_IF but they really are
1238 * different. FILE_SUPERSEDE deletes an existing file
1239 * (requiring delete access) then recreates it.
1240 */
1241 case FILE_SUPERSEDE:
1242 /* If file exists replace/overwrite. If file doesn't
1243 * exist create. */
1244 flags2 |= (O_CREAT | O_TRUNC);
1245 break;
1246
1247 case FILE_OVERWRITE_IF:
1248 /* If file exists replace/overwrite. If file doesn't
1249 * exist create. */
1250 flags2 |= (O_CREAT | O_TRUNC);
1251 break;
1252
1253 case FILE_OPEN:
1254 /* If file exists open. If file doesn't exist error. */
1255 if (!file_existed) {
1256 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1257 "requested for file %s and file "
1258 "doesn't exist.\n", fname ));
1259 errno = ENOENT;
1260 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1261 }
1262 break;
1263
1264 case FILE_OVERWRITE:
1265 /* If file exists overwrite. If file doesn't exist
1266 * error. */
1267 if (!file_existed) {
1268 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1269 "requested for file %s and file "
1270 "doesn't exist.\n", fname ));
1271 errno = ENOENT;
1272 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1273 }
1274 flags2 |= O_TRUNC;
1275 break;
1276
1277 case FILE_CREATE:
1278 /* If file exists error. If file doesn't exist
1279 * create. */
1280 if (file_existed) {
1281 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1282 "requested for file %s and file "
1283 "already exists.\n", fname ));
1284 if (S_ISDIR(psbuf->st_mode)) {
1285 errno = EISDIR;
1286 } else {
1287 errno = EEXIST;
1288 }
1289 return map_nt_error_from_unix(errno);
1290 }
1291 flags2 |= (O_CREAT|O_EXCL);
1292 break;
1293
1294 case FILE_OPEN_IF:
1295 /* If file exists open. If file doesn't exist
1296 * create. */
1297 flags2 |= O_CREAT;
1298 break;
1299
1300 default:
1301 return NT_STATUS_INVALID_PARAMETER;
1302 }
1303
1304 /* We only care about matching attributes on file exists and
1305 * overwrite. */
1306
1307 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1308 (create_disposition == FILE_OVERWRITE_IF))) {
1309 if (!open_match_attributes(conn, fname,
1310 existing_dos_attributes,
1311 new_dos_attributes, psbuf->st_mode,
1312 unx_mode, &new_unx_mode)) {
1313 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1314 "for file %s (%x %x) (0%o, 0%o)\n",
1315 fname, existing_dos_attributes,
1316 new_dos_attributes,
1317 (unsigned int)psbuf->st_mode,
1318 (unsigned int)unx_mode ));
1319 errno = EACCES;
1320 return NT_STATUS_ACCESS_DENIED;
1321 }
1322 }
1323
1324 /* This is a nasty hack - must fix... JRA. */
1325 if (access_mask == MAXIMUM_ALLOWED_ACCESS) {
1326 open_access_mask = access_mask = FILE_GENERIC_ALL;
1327 }
1328
1329 /*
1330 * Convert GENERIC bits to specific bits.
1331 */
1332
1333 se_map_generic(&access_mask, &file_generic_mapping);
1334 open_access_mask = access_mask;
1335
1336 if (flags2 & O_TRUNC) {
1337 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1338 }
1339
1340 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1341 "access_mask=0x%x\n", fname, access_mask ));
1342
1343 /*
1344 * Note that we ignore the append flag as append does not
1345 * mean the same thing under DOS and Unix.
1346 */
1347
1348 if (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) {
1349 /* DENY_DOS opens are always underlying read-write on the
1350 file handle, no matter what the requested access mask
1351 says. */
1352 if ((create_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1353 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1354 flags = O_RDWR;
1355 } else {
1356 flags = O_WRONLY;
1357 }
1358 } else {
1359 flags = O_RDONLY;
1360 }
1361
1362 /*
1363 * Currently we only look at FILE_WRITE_THROUGH for create options.
1364 */
1365
1366#if defined(O_SYNC)
1367 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1368 flags2 |= O_SYNC;
1369 }
1370#endif /* O_SYNC */
1371
1372 if (posix_open & (access_mask & FILE_APPEND_DATA)) {
1373 flags2 |= O_APPEND;
1374 }
1375
1376 if (!posix_open && !CAN_WRITE(conn)) {
1377 /*
1378 * We should really return a permission denied error if either
1379 * O_CREAT or O_TRUNC are set, but for compatibility with
1380 * older versions of Samba we just AND them out.
1381 */
1382 flags2 &= ~(O_CREAT|O_TRUNC);
1383 }
1384
1385 /*
1386 * Ensure we can't write on a read-only share or file.
1387 */
1388
1389 if (flags != O_RDONLY && file_existed &&
1390 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1391 DEBUG(5,("open_file_ntcreate: write access requested for "
1392 "file %s on read only %s\n",
1393 fname, !CAN_WRITE(conn) ? "share" : "file" ));
1394 errno = EACCES;
1395 return NT_STATUS_ACCESS_DENIED;
1396 }
1397
1398 status = file_new(conn, &fsp);
1399 if(!NT_STATUS_IS_OK(status)) {
1400 return status;
1401 }
1402
1403 fsp->dev = psbuf->st_dev;
1404 fsp->inode = psbuf->st_ino;
1405 fsp->share_access = share_access;
1406 fsp->fh->private_options = create_options;
1407 fsp->access_mask = open_access_mask; /* We change this to the
1408 * requested access_mask after
1409 * the open is done. */
1410 fsp->posix_open = posix_open;
1411
1412 /* Ensure no SAMBA_PRIVATE bits can be set. */
1413 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1414
1415 if (timeval_is_zero(&request_time)) {
1416 request_time = fsp->open_time;
1417 }
1418
1419 if (file_existed) {
1420 dev = psbuf->st_dev;
1421 inode = psbuf->st_ino;
1422
1423 lck = get_share_mode_lock(NULL, dev, inode,
1424 conn->connectpath,
1425 fname);
1426
1427 if (lck == NULL) {
1428 file_free(fsp);
1429 DEBUG(0, ("Could not get share mode lock\n"));
1430 return NT_STATUS_SHARING_VIOLATION;
1431 }
1432
1433 /* First pass - send break only on batch oplocks. */
1434 if (delay_for_oplocks(lck, fsp, 1, oplock_request)) {
1435 schedule_defer_open(lck, request_time);
1436 TALLOC_FREE(lck);
1437 file_free(fsp);
1438 return NT_STATUS_SHARING_VIOLATION;
1439 }
1440
1441 /* Use the client requested access mask here, not the one we
1442 * open with. */
1443 status = open_mode_check(conn, fname, lck,
1444 access_mask, share_access,
1445 create_options, &file_existed);
1446
1447 if (NT_STATUS_IS_OK(status)) {
1448 /* We might be going to allow this open. Check oplock
1449 * status again. */
1450 /* Second pass - send break for both batch or
1451 * exclusive oplocks. */
1452 if (delay_for_oplocks(lck, fsp, 2, oplock_request)) {
1453 schedule_defer_open(lck, request_time);
1454 TALLOC_FREE(lck);
1455 file_free(fsp);
1456 return NT_STATUS_SHARING_VIOLATION;
1457 }
1458 }
1459
1460 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1461 /* DELETE_PENDING is not deferred for a second */
1462 TALLOC_FREE(lck);
1463 file_free(fsp);
1464 return status;
1465 }
1466
1467 if (!NT_STATUS_IS_OK(status)) {
1468 uint32 can_access_mask;
1469 BOOL can_access = True;
1470
1471 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1472
1473 /* Check if this can be done with the deny_dos and fcb
1474 * calls. */
1475 if (create_options &
1476 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1477 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1478 files_struct *fsp_dup;
1479
1480 /* Use the client requested access mask here,
1481 * not the one we open with. */
1482 fsp_dup = fcb_or_dos_open(conn, fname, dev,
1483 inode, access_mask,
1484 share_access,
1485 create_options);
1486
1487 if (fsp_dup) {
1488 TALLOC_FREE(lck);
1489 file_free(fsp);
1490 if (pinfo) {
1491 *pinfo = FILE_WAS_OPENED;
1492 }
1493 conn->num_files_open++;
1494 *result = fsp_dup;
1495 return NT_STATUS_OK;
1496 }
1497 }
1498
1499 /*
1500 * This next line is a subtlety we need for
1501 * MS-Access. If a file open will fail due to share
1502 * permissions and also for security (access) reasons,
1503 * we need to return the access failed error, not the
1504 * share error. We can't open the file due to kernel
1505 * oplock deadlock (it's possible we failed above on
1506 * the open_mode_check()) so use a userspace check.
1507 */
1508
1509 if (flags & O_RDWR) {
1510 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1511 } else if (flags & O_WRONLY) {
1512 can_access_mask = FILE_WRITE_DATA;
1513 } else {
1514 can_access_mask = FILE_READ_DATA;
1515 }
1516
1517 if (((can_access_mask & FILE_WRITE_DATA) && !CAN_WRITE(conn)) ||
1518 !can_access_file(conn,fname,psbuf,can_access_mask)) {
1519 can_access = False;
1520 }
1521
1522 /*
1523 * If we're returning a share violation, ensure we
1524 * cope with the braindead 1 second delay.
1525 */
1526
1527 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
1528 lp_defer_sharing_violations()) {
1529 struct timeval timeout;
1530 struct deferred_open_record state;
1531 int timeout_usecs;
1532
1533 /* this is a hack to speed up torture tests
1534 in 'make test' */
1535 timeout_usecs = lp_parm_int(SNUM(conn),
1536 "smbd","sharedelay",
1537 SHARING_VIOLATION_USEC_WAIT);
1538
1539 /* This is a relative time, added to the absolute
1540 request_time value to get the absolute timeout time.
1541 Note that if this is the second or greater time we enter
1542 this codepath for this particular request mid then
1543 request_time is left as the absolute time of the *first*
1544 time this request mid was processed. This is what allows
1545 the request to eventually time out. */
1546
1547 timeout = timeval_set(0, timeout_usecs);
1548
1549 /* Nothing actually uses state.delayed_for_oplocks
1550 but it's handy to differentiate in debug messages
1551 between a 30 second delay due to oplock break, and
1552 a 1 second delay for share mode conflicts. */
1553
1554 state.delayed_for_oplocks = False;
1555 state.dev = dev;
1556 state.inode = inode;
1557
1558 if (!request_timed_out(request_time,
1559 timeout)) {
1560 defer_open(lck, request_time, timeout,
1561 &state);
1562 }
1563 }
1564
1565 TALLOC_FREE(lck);
1566 if (can_access) {
1567 /*
1568 * We have detected a sharing violation here
1569 * so return the correct error code
1570 */
1571 status = NT_STATUS_SHARING_VIOLATION;
1572 } else {
1573 status = NT_STATUS_ACCESS_DENIED;
1574 }
1575 file_free(fsp);
1576 return status;
1577 }
1578
1579 /*
1580 * We exit this block with the share entry *locked*.....
1581 */
1582 }
1583
1584 SMB_ASSERT(!file_existed || (lck != NULL));
1585
1586 /*
1587 * Ensure we pay attention to default ACLs on directories if required.
1588 */
1589
1590 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1591 (def_acl = directory_has_default_acl(conn, parent_dir))) {
1592 unx_mode = 0777;
1593 }
1594
1595 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
1596 "access_mask = 0x%x, open_access_mask = 0x%x\n",
1597 (unsigned int)flags, (unsigned int)flags2,
1598 (unsigned int)unx_mode, (unsigned int)access_mask,
1599 (unsigned int)open_access_mask));
1600
1601 /*
1602 * open_file strips any O_TRUNC flags itself.
1603 */
1604
1605 fsp_open = open_file(fsp, conn, parent_dir, newname, fname, psbuf,
1606 flags|flags2, unx_mode, access_mask,
1607 open_access_mask);
1608
1609 if (!NT_STATUS_IS_OK(fsp_open)) {
1610 if (lck != NULL) {
1611 TALLOC_FREE(lck);
1612 }
1613 file_free(fsp);
1614 return fsp_open;
1615 }
1616
1617 if (!file_existed) {
1618
1619 /*
1620 * Deal with the race condition where two smbd's detect the
1621 * file doesn't exist and do the create at the same time. One
1622 * of them will win and set a share mode, the other (ie. this
1623 * one) should check if the requested share mode for this
1624 * create is allowed.
1625 */
1626
1627 /*
1628 * Now the file exists and fsp is successfully opened,
1629 * fsp->dev and fsp->inode are valid and should replace the
1630 * dev=0,inode=0 from a non existent file. Spotted by
1631 * Nadav Danieli <nadavd@exanet.com>. JRA.
1632 */
1633
1634 dev = fsp->dev;
1635 inode = fsp->inode;
1636
1637 lck = get_share_mode_lock(NULL, dev, inode,
1638 conn->connectpath,
1639 fname);
1640
1641 if (lck == NULL) {
1642 DEBUG(0, ("open_file_ntcreate: Could not get share "
1643 "mode lock for %s\n", fname));
1644 fd_close(conn, fsp);
1645 file_free(fsp);
1646 return NT_STATUS_SHARING_VIOLATION;
1647 }
1648
1649 status = open_mode_check(conn, fname, lck,
1650 access_mask, share_access,
1651 create_options, &file_existed);
1652
1653 if (!NT_STATUS_IS_OK(status)) {
1654 struct deferred_open_record state;
1655
1656 fd_close(conn, fsp);
1657 file_free(fsp);
1658
1659 state.delayed_for_oplocks = False;
1660 state.dev = dev;
1661 state.inode = inode;
1662
1663 /* Do it all over again immediately. In the second
1664 * round we will find that the file existed and handle
1665 * the DELETE_PENDING and FCB cases correctly. No need
1666 * to duplicate the code here. Essentially this is a
1667 * "goto top of this function", but don't tell
1668 * anybody... */
1669
1670 defer_open(lck, request_time, timeval_zero(),
1671 &state);
1672 TALLOC_FREE(lck);
1673 return status;
1674 }
1675
1676 /*
1677 * We exit this block with the share entry *locked*.....
1678 */
1679
1680 }
1681
1682 SMB_ASSERT(lck != NULL);
1683
1684 /* note that we ignore failure for the following. It is
1685 basically a hack for NFS, and NFS will never set one of
1686 these only read them. Nobody but Samba can ever set a deny
1687 mode and we have already checked our more authoritative
1688 locking database for permission to set this deny mode. If
1689 the kernel refuses the operations then the kernel is wrong.
1690 note that GPFS supports it as well - jmcd */
1691
1692 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, fsp->fh->fd, share_access);
1693 if(ret_flock == -1 ){
1694
1695 TALLOC_FREE(lck);
1696 fd_close(conn, fsp);
1697 file_free(fsp);
1698
1699 return NT_STATUS_SHARING_VIOLATION;
1700 }
1701
1702 /*
1703 * At this point onwards, we can guarentee that the share entry
1704 * is locked, whether we created the file or not, and that the
1705 * deny mode is compatible with all current opens.
1706 */
1707
1708 /*
1709 * If requested, truncate the file.
1710 */
1711
1712 if (flags2&O_TRUNC) {
1713 /*
1714 * We are modifing the file after open - update the stat
1715 * struct..
1716 */
1717 if ((SMB_VFS_FTRUNCATE(fsp,fsp->fh->fd,0) == -1) ||
1718 (SMB_VFS_FSTAT(fsp,fsp->fh->fd,psbuf)==-1)) {
1719 status = map_nt_error_from_unix(errno);
1720 TALLOC_FREE(lck);
1721 fd_close(conn,fsp);
1722 file_free(fsp);
1723 return status;
1724 }
1725 }
1726
1727 /* Record the options we were opened with. */
1728 fsp->share_access = share_access;
1729 fsp->fh->private_options = create_options;
1730 fsp->access_mask = access_mask;
1731
1732 if (file_existed) {
1733 /* stat opens on existing files don't get oplocks. */
1734 if (is_stat_open(open_access_mask)) {
1735 fsp->oplock_type = NO_OPLOCK;
1736 }
1737
1738 if (!(flags2 & O_TRUNC)) {
1739 info = FILE_WAS_OPENED;
1740 } else {
1741 info = FILE_WAS_OVERWRITTEN;
1742 }
1743 } else {
1744 info = FILE_WAS_CREATED;
1745 }
1746
1747 if (pinfo) {
1748 *pinfo = info;
1749 }
1750
1751 /*
1752 * Setup the oplock info in both the shared memory and
1753 * file structs.
1754 */
1755
1756 if ((fsp->oplock_type != NO_OPLOCK) &&
1757 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
1758 if (!set_file_oplock(fsp, fsp->oplock_type)) {
1759 /* Could not get the kernel oplock */
1760 fsp->oplock_type = NO_OPLOCK;
1761 }
1762 }
1763 set_share_mode(lck, fsp, current_user.ut.uid, 0, fsp->oplock_type);
1764
1765 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED ||
1766 info == FILE_WAS_SUPERSEDED) {
1767
1768 /* Handle strange delete on close create semantics. */
1769 if (create_options & FILE_DELETE_ON_CLOSE) {
1770 status = can_set_delete_on_close(fsp, True, new_dos_attributes);
1771
1772 if (!NT_STATUS_IS_OK(status)) {
1773 /* Remember to delete the mode we just added. */
1774 del_share_mode(lck, fsp);
1775 TALLOC_FREE(lck);
1776 fd_close(conn,fsp);
1777 file_free(fsp);
1778 return status;
1779 }
1780 /* Note that here we set the *inital* delete on close flag,
1781 not the regular one. The magic gets handled in close. */
1782 fsp->initial_delete_on_close = True;
1783 }
1784
1785 /* Files should be initially set as archive */
1786 if (lp_map_archive(SNUM(conn)) ||
1787 lp_store_dos_attributes(SNUM(conn))) {
1788 if (!posix_open) {
1789 file_set_dosmode(conn, fname,
1790 new_dos_attributes | aARCH, NULL,
1791 parent_dir);
1792 }
1793 }
1794 }
1795
1796 /*
1797 * Take care of inherited ACLs on created files - if default ACL not
1798 * selected.
1799 */
1800
1801 if (!posix_open && !file_existed && !def_acl) {
1802
1803 int saved_errno = errno; /* We might get ENOSYS in the next
1804 * call.. */
1805
1806 if (SMB_VFS_FCHMOD_ACL(fsp, fsp->fh->fd, unx_mode) == -1 &&
1807 errno == ENOSYS) {
1808 errno = saved_errno; /* Ignore ENOSYS */
1809 }
1810
1811 } else if (new_unx_mode) {
1812
1813 int ret = -1;
1814
1815 /* Attributes need changing. File already existed. */
1816
1817 {
1818 int saved_errno = errno; /* We might get ENOSYS in the
1819 * next call.. */
1820 ret = SMB_VFS_FCHMOD_ACL(fsp, fsp->fh->fd,
1821 new_unx_mode);
1822
1823 if (ret == -1 && errno == ENOSYS) {
1824 errno = saved_errno; /* Ignore ENOSYS */
1825 } else {
1826 DEBUG(5, ("open_file_ntcreate: reset "
1827 "attributes of file %s to 0%o\n",
1828 fname, (unsigned int)new_unx_mode));
1829 ret = 0; /* Don't do the fchmod below. */
1830 }
1831 }
1832
1833 if ((ret == -1) &&
1834 (SMB_VFS_FCHMOD(fsp, fsp->fh->fd, new_unx_mode) == -1))
1835 DEBUG(5, ("open_file_ntcreate: failed to reset "
1836 "attributes of file %s to 0%o\n",
1837 fname, (unsigned int)new_unx_mode));
1838 }
1839
1840 /* If this is a successful open, we must remove any deferred open
1841 * records. */
1842 del_deferred_open_entry(lck, mid);
1843 TALLOC_FREE(lck);
1844
1845 conn->num_files_open++;
1846
1847 *result = fsp;
1848 return NT_STATUS_OK;
1849}
1850
1851/****************************************************************************
1852 Open a file for for write to ensure that we can fchmod it.
1853****************************************************************************/
1854
1855NTSTATUS open_file_fchmod(connection_struct *conn, const char *fname,
1856 SMB_STRUCT_STAT *psbuf, files_struct **result)
1857{
1858 files_struct *fsp = NULL;
1859 NTSTATUS status;
1860
1861 if (!VALID_STAT(*psbuf)) {
1862 return NT_STATUS_INVALID_PARAMETER;
1863 }
1864
1865 status = file_new(conn, &fsp);
1866 if(!NT_STATUS_IS_OK(status)) {
1867 return status;
1868 }
1869
1870 /* note! we must use a non-zero desired access or we don't get
1871 a real file descriptor. Oh what a twisted web we weave. */
1872 status = open_file(fsp, conn, NULL, NULL, fname, psbuf, O_WRONLY, 0,
1873 FILE_WRITE_DATA, FILE_WRITE_DATA);
1874
1875 /*
1876 * This is not a user visible file open.
1877 * Don't set a share mode and don't increment
1878 * the conn->num_files_open.
1879 */
1880
1881 if (!NT_STATUS_IS_OK(status)) {
1882 file_free(fsp);
1883 return status;
1884 }
1885
1886 *result = fsp;
1887 return NT_STATUS_OK;
1888}
1889
1890/****************************************************************************
1891 Close the fchmod file fd - ensure no locks are lost.
1892****************************************************************************/
1893
1894NTSTATUS close_file_fchmod(files_struct *fsp)
1895{
1896 NTSTATUS status = fd_close(fsp->conn, fsp);
1897 file_free(fsp);
1898 return status;
1899}
1900
1901static NTSTATUS mkdir_internal(connection_struct *conn,
1902 const char *name,
1903 uint32 file_attributes,
1904 SMB_STRUCT_STAT *psbuf)
1905{
1906 int ret= -1;
1907 mode_t mode;
1908 char *parent_dir;
1909 const char *dirname;
1910 NTSTATUS status;
1911
1912 if(!CAN_WRITE(conn)) {
1913 DEBUG(5,("mkdir_internal: failing create on read-only share "
1914 "%s\n", lp_servicename(SNUM(conn))));
1915 return NT_STATUS_ACCESS_DENIED;
1916 }
1917
1918 status = check_name(conn, name);
1919 if (!NT_STATUS_IS_OK(status)) {
1920 return status;
1921 }
1922
1923 if (!parent_dirname_talloc(tmp_talloc_ctx(), name, &parent_dir,
1924 &dirname)) {
1925 return NT_STATUS_NO_MEMORY;
1926 }
1927
1928 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1929 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1930 } else {
1931 mode = unix_mode(conn, aDIR, name, parent_dir);
1932 }
1933
1934 if ((ret=SMB_VFS_MKDIR(conn, name, mode)) != 0) {
1935 return map_nt_error_from_unix(errno);
1936 }
1937
1938 /* Ensure we're checking for a symlink here.... */
1939 /* We don't want to get caught by a symlink racer. */
1940
1941 if (SMB_VFS_LSTAT(conn, name, psbuf) == -1) {
1942 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
1943 name, strerror(errno)));
1944 return map_nt_error_from_unix(errno);
1945 }
1946
1947 if (!S_ISDIR(psbuf->st_mode)) {
1948 DEBUG(0, ("Directory just '%s' created is not a directory\n",
1949 name));
1950 return NT_STATUS_ACCESS_DENIED;
1951 }
1952
1953 if (lp_inherit_perms(SNUM(conn))) {
1954 inherit_access_acl(conn, parent_dir, name, mode);
1955 }
1956
1957 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
1958 /*
1959 * Check if high bits should have been set,
1960 * then (if bits are missing): add them.
1961 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
1962 * dir.
1963 */
1964 if (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) && (mode & ~psbuf->st_mode)) {
1965 SMB_VFS_CHMOD(conn, name,
1966 psbuf->st_mode | (mode & ~psbuf->st_mode));
1967 }
1968 }
1969
1970 /* Change the owner if required. */
1971 if (lp_inherit_owner(SNUM(conn))) {
1972 change_dir_owner_to_parent(conn, parent_dir, name, psbuf);
1973 }
1974
1975 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
1976 name);
1977
1978 return NT_STATUS_OK;
1979}
1980
1981/****************************************************************************
1982 Open a directory from an NT SMB call.
1983****************************************************************************/
1984
1985NTSTATUS open_directory(connection_struct *conn,
1986 const char *fname,
1987 SMB_STRUCT_STAT *psbuf,
1988 uint32 access_mask,
1989 uint32 share_access,
1990 uint32 create_disposition,
1991 uint32 create_options,
1992 uint32 file_attributes,
1993 int *pinfo,
1994 files_struct **result)
1995{
1996 files_struct *fsp = NULL;
1997 BOOL dir_existed = VALID_STAT(*psbuf) ? True : False;
1998 struct share_mode_lock *lck = NULL;
1999 NTSTATUS status;
2000 int info = 0;
2001
2002 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2003 "share_access = 0x%x create_options = 0x%x, "
2004 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2005 fname,
2006 (unsigned int)access_mask,
2007 (unsigned int)share_access,
2008 (unsigned int)create_options,
2009 (unsigned int)create_disposition,
2010 (unsigned int)file_attributes));
2011
2012 if (is_ntfs_stream_name(fname)) {
2013 DEBUG(0,("open_directory: %s is a stream name!\n", fname ));
2014 return NT_STATUS_NOT_A_DIRECTORY;
2015 }
2016
2017 switch( create_disposition ) {
2018 case FILE_OPEN:
2019
2020 info = FILE_WAS_OPENED;
2021
2022 /*
2023 * We want to follow symlinks here.
2024 */
2025
2026 if (SMB_VFS_STAT(conn, fname, psbuf) != 0) {
2027 return map_nt_error_from_unix(errno);
2028 }
2029
2030 break;
2031
2032 case FILE_CREATE:
2033
2034 /* If directory exists error. If directory doesn't
2035 * exist create. */
2036
2037 status = mkdir_internal(conn,
2038 fname,
2039 file_attributes,
2040 psbuf);
2041
2042 if (!NT_STATUS_IS_OK(status)) {
2043 DEBUG(2, ("open_directory: unable to create "
2044 "%s. Error was %s\n", fname,
2045 nt_errstr(status)));
2046 return status;
2047 }
2048
2049 info = FILE_WAS_CREATED;
2050 break;
2051
2052 case FILE_OPEN_IF:
2053 /*
2054 * If directory exists open. If directory doesn't
2055 * exist create.
2056 */
2057
2058 status = mkdir_internal(conn,
2059 fname,
2060 file_attributes,
2061 psbuf);
2062
2063 if (NT_STATUS_IS_OK(status)) {
2064 info = FILE_WAS_CREATED;
2065 }
2066
2067 if (NT_STATUS_EQUAL(status,
2068 NT_STATUS_OBJECT_NAME_COLLISION)) {
2069 info = FILE_WAS_OPENED;
2070 status = NT_STATUS_OK;
2071 }
2072
2073 break;
2074
2075 case FILE_SUPERSEDE:
2076 case FILE_OVERWRITE:
2077 case FILE_OVERWRITE_IF:
2078 default:
2079 DEBUG(5,("open_directory: invalid create_disposition "
2080 "0x%x for directory %s\n",
2081 (unsigned int)create_disposition, fname));
2082 return NT_STATUS_INVALID_PARAMETER;
2083 }
2084
2085 if(!S_ISDIR(psbuf->st_mode)) {
2086 DEBUG(5,("open_directory: %s is not a directory !\n",
2087 fname ));
2088 return NT_STATUS_NOT_A_DIRECTORY;
2089 }
2090
2091 status = file_new(conn, &fsp);
2092 if(!NT_STATUS_IS_OK(status)) {
2093 return status;
2094 }
2095
2096 /*
2097 * Setup the files_struct for it.
2098 */
2099
2100 fsp->mode = psbuf->st_mode;
2101 fsp->inode = psbuf->st_ino;
2102 fsp->dev = psbuf->st_dev;
2103 fsp->vuid = current_user.vuid;
2104 fsp->file_pid = global_smbpid;
2105 fsp->can_lock = False;
2106 fsp->can_read = False;
2107 fsp->can_write = False;
2108
2109 fsp->share_access = share_access;
2110 fsp->fh->private_options = create_options;
2111 fsp->access_mask = access_mask;
2112
2113 fsp->print_file = False;
2114 fsp->modified = False;
2115 fsp->oplock_type = NO_OPLOCK;
2116 fsp->sent_oplock_break = NO_BREAK_SENT;
2117 fsp->is_directory = True;
2118 fsp->is_stat = False;
2119 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2120
2121 string_set(&fsp->fsp_name,fname);
2122
2123 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode,
2124 conn->connectpath,
2125 fname);
2126
2127 if (lck == NULL) {
2128 DEBUG(0, ("open_directory: Could not get share mode lock for %s\n", fname));
2129 file_free(fsp);
2130 return NT_STATUS_SHARING_VIOLATION;
2131 }
2132
2133 status = open_mode_check(conn, fname, lck,
2134 access_mask, share_access,
2135 create_options, &dir_existed);
2136
2137 if (!NT_STATUS_IS_OK(status)) {
2138 TALLOC_FREE(lck);
2139 file_free(fsp);
2140 return status;
2141 }
2142
2143 set_share_mode(lck, fsp, current_user.ut.uid, 0, NO_OPLOCK);
2144
2145 /* For directories the delete on close bit at open time seems
2146 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2147 if (create_options & FILE_DELETE_ON_CLOSE) {
2148 status = can_set_delete_on_close(fsp, True, 0);
2149 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2150 TALLOC_FREE(lck);
2151 file_free(fsp);
2152 return status;
2153 }
2154
2155 if (NT_STATUS_IS_OK(status)) {
2156 /* Note that here we set the *inital* delete on close flag,
2157 not the regular one. The magic gets handled in close. */
2158 fsp->initial_delete_on_close = True;
2159 }
2160 }
2161
2162 TALLOC_FREE(lck);
2163
2164 if (pinfo) {
2165 *pinfo = info;
2166 }
2167
2168 conn->num_files_open++;
2169
2170 *result = fsp;
2171 return NT_STATUS_OK;
2172}
2173
2174NTSTATUS create_directory(connection_struct *conn, const char *directory)
2175{
2176 NTSTATUS status;
2177 SMB_STRUCT_STAT sbuf;
2178 files_struct *fsp;
2179
2180 SET_STAT_INVALID(sbuf);
2181
2182 status = open_directory(conn, directory, &sbuf,
2183 FILE_READ_ATTRIBUTES, /* Just a stat open */
2184 FILE_SHARE_NONE, /* Ignored for stat opens */
2185 FILE_CREATE,
2186 0,
2187 FILE_ATTRIBUTE_DIRECTORY,
2188 NULL,
2189 &fsp);
2190
2191 if (NT_STATUS_IS_OK(status)) {
2192 close_file(fsp, NORMAL_CLOSE);
2193 }
2194
2195 return status;
2196}
2197
2198/****************************************************************************
2199 Open a pseudo-file (no locking checks - a 'stat' open).
2200****************************************************************************/
2201
2202NTSTATUS open_file_stat(connection_struct *conn, const char *fname,
2203 SMB_STRUCT_STAT *psbuf, files_struct **result)
2204{
2205 files_struct *fsp = NULL;
2206 NTSTATUS status;
2207
2208 if (!VALID_STAT(*psbuf)) {
2209 return NT_STATUS_INVALID_PARAMETER;
2210 }
2211
2212 /* Can't 'stat' open directories. */
2213 if(S_ISDIR(psbuf->st_mode)) {
2214 return NT_STATUS_FILE_IS_A_DIRECTORY;
2215 }
2216
2217 status = file_new(conn, &fsp);
2218 if(!NT_STATUS_IS_OK(status)) {
2219 return status;
2220 }
2221
2222 DEBUG(5,("open_file_stat: 'opening' file %s\n", fname));
2223
2224 /*
2225 * Setup the files_struct for it.
2226 */
2227
2228 fsp->mode = psbuf->st_mode;
2229 fsp->inode = psbuf->st_ino;
2230 fsp->dev = psbuf->st_dev;
2231 fsp->vuid = current_user.vuid;
2232 fsp->file_pid = global_smbpid;
2233 fsp->can_lock = False;
2234 fsp->can_read = False;
2235 fsp->can_write = False;
2236 fsp->print_file = False;
2237 fsp->modified = False;
2238 fsp->oplock_type = NO_OPLOCK;
2239 fsp->sent_oplock_break = NO_BREAK_SENT;
2240 fsp->is_directory = False;
2241 fsp->is_stat = True;
2242 string_set(&fsp->fsp_name,fname);
2243
2244 conn->num_files_open++;
2245
2246 *result = fsp;
2247 return NT_STATUS_OK;
2248}
2249
2250/****************************************************************************
2251 Receive notification that one of our open files has been renamed by another
2252 smbd process.
2253****************************************************************************/
2254
2255void msg_file_was_renamed(int msg_type, struct process_id src,
2256 void *buf, size_t len, void *private_data)
2257{
2258 files_struct *fsp;
2259 char *frm = (char *)buf;
2260 SMB_DEV_T dev;
2261 SMB_INO_T inode;
2262 const char *sharepath;
2263 const char *newname;
2264 size_t sp_len;
2265
2266 if (buf == NULL || len < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2267 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n", (int)len));
2268 return;
2269 }
2270
2271 /* Unpack the message. */
2272 dev = DEV_T_VAL(frm,0);
2273 inode = INO_T_VAL(frm,8);
2274 sharepath = &frm[16];
2275 newname = sharepath + strlen(sharepath) + 1;
2276 sp_len = strlen(sharepath);
2277
2278 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2279 "dev %x, inode %.0f\n",
2280 sharepath, newname, (unsigned int)dev, (double)inode ));
2281
2282 for(fsp = file_find_di_first(dev, inode); fsp; fsp = file_find_di_next(fsp)) {
2283 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2284 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2285 fsp->fnum, fsp->fsp_name, newname ));
2286 string_set(&fsp->fsp_name, newname);
2287 } else {
2288 /* TODO. JRA. */
2289 /* Now we have the complete path we can work out if this is
2290 actually within this share and adjust newname accordingly. */
2291 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2292 "not sharepath %s) "
2293 "fnum %d from %s -> %s\n",
2294 fsp->conn->connectpath,
2295 sharepath,
2296 fsp->fnum,
2297 fsp->fsp_name,
2298 newname ));
2299 }
2300 }
2301}
Note: See TracBrowser for help on using the repository browser.