source: vendor/3.6.0/source3/smbd/open.c

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

File size: 108.3 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 3 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, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "includes.h"
23#include "system/filesys.h"
24#include "printing.h"
25#include "smbd/smbd.h"
26#include "smbd/globals.h"
27#include "fake_file.h"
28#include "../libcli/security/security.h"
29#include "../librpc/gen_ndr/ndr_security.h"
30#include "auth.h"
31#include "messages.h"
32
33extern const struct generic_mapping file_generic_mapping;
34
35struct deferred_open_record {
36 bool delayed_for_oplocks;
37 struct file_id id;
38};
39
40/****************************************************************************
41 SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES.
42****************************************************************************/
43
44NTSTATUS smb1_file_se_access_check(struct connection_struct *conn,
45 const struct security_descriptor *sd,
46 const struct security_token *token,
47 uint32_t access_desired,
48 uint32_t *access_granted)
49{
50 *access_granted = 0;
51
52 if (get_current_uid(conn) == (uid_t)0) {
53 /* I'm sorry sir, I didn't know you were root... */
54 *access_granted = access_desired;
55 if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
56 *access_granted |= FILE_GENERIC_ALL;
57 }
58 return NT_STATUS_OK;
59 }
60
61 return se_access_check(sd,
62 token,
63 (access_desired & ~FILE_READ_ATTRIBUTES),
64 access_granted);
65}
66
67/****************************************************************************
68 Check if we have open rights.
69****************************************************************************/
70
71NTSTATUS smbd_check_open_rights(struct connection_struct *conn,
72 const struct smb_filename *smb_fname,
73 uint32_t access_mask,
74 uint32_t *access_granted)
75{
76 /* Check if we have rights to open. */
77 NTSTATUS status;
78 struct security_descriptor *sd = NULL;
79 uint32_t rejected_share_access;
80
81 rejected_share_access = access_mask & ~(conn->share_access);
82
83 if (rejected_share_access) {
84 *access_granted = rejected_share_access;
85 return NT_STATUS_ACCESS_DENIED;
86 }
87
88 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
89 *access_granted = access_mask;
90
91 DEBUG(10,("smbd_check_open_rights: not checking ACL "
92 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
93 smb_fname_str_dbg(smb_fname),
94 (unsigned int)*access_granted ));
95 return NT_STATUS_OK;
96 }
97
98 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
99 (SECINFO_OWNER |
100 SECINFO_GROUP |
101 SECINFO_DACL),&sd);
102
103 if (!NT_STATUS_IS_OK(status)) {
104 DEBUG(10, ("smbd_check_open_rights: Could not get acl "
105 "on %s: %s\n",
106 smb_fname_str_dbg(smb_fname),
107 nt_errstr(status)));
108 return status;
109 }
110
111 status = smb1_file_se_access_check(conn,
112 sd,
113 get_current_nttok(conn),
114 access_mask,
115 access_granted);
116
117 DEBUG(10,("smbd_check_open_rights: file %s requesting "
118 "0x%x returning 0x%x (%s)\n",
119 smb_fname_str_dbg(smb_fname),
120 (unsigned int)access_mask,
121 (unsigned int)*access_granted,
122 nt_errstr(status) ));
123
124 if (!NT_STATUS_IS_OK(status)) {
125 if (DEBUGLEVEL >= 10) {
126 DEBUG(10,("smbd_check_open_rights: acl for %s is:\n",
127 smb_fname_str_dbg(smb_fname) ));
128 NDR_PRINT_DEBUG(security_descriptor, sd);
129 }
130 }
131
132 TALLOC_FREE(sd);
133
134 return status;
135}
136
137/****************************************************************************
138 fd support routines - attempt to do a dos_open.
139****************************************************************************/
140
141static NTSTATUS fd_open(struct connection_struct *conn,
142 files_struct *fsp,
143 int flags,
144 mode_t mode)
145{
146 struct smb_filename *smb_fname = fsp->fsp_name;
147 NTSTATUS status = NT_STATUS_OK;
148
149#ifdef O_NOFOLLOW
150 /*
151 * Never follow symlinks on a POSIX client. The
152 * client should be doing this.
153 */
154
155 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
156 flags |= O_NOFOLLOW;
157 }
158#endif
159
160 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
161 if (fsp->fh->fd == -1) {
162 status = map_nt_error_from_unix(errno);
163 if (errno == EMFILE) {
164 static time_t last_warned = 0L;
165
166 if (time((time_t *) NULL) > last_warned) {
167 DEBUG(0,("Too many open files, unable "
168 "to open more! smbd's max "
169 "open files = %d\n",
170 lp_max_open_files()));
171 last_warned = time((time_t *) NULL);
172 }
173 }
174
175 }
176
177 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
178 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
179 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
180
181 return status;
182}
183
184/****************************************************************************
185 Close the file associated with a fsp.
186****************************************************************************/
187
188NTSTATUS fd_close(files_struct *fsp)
189{
190 int ret;
191
192 if (fsp->dptr) {
193 dptr_CloseDir(fsp);
194 }
195 if (fsp->fh->fd == -1) {
196 return NT_STATUS_OK; /* What we used to call a stat open. */
197 }
198 if (fsp->fh->ref_count > 1) {
199 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
200 }
201
202 ret = SMB_VFS_CLOSE(fsp);
203 fsp->fh->fd = -1;
204 if (ret == -1) {
205 return map_nt_error_from_unix(errno);
206 }
207 return NT_STATUS_OK;
208}
209
210/****************************************************************************
211 Change the ownership of a file to that of the parent directory.
212 Do this by fd if possible.
213****************************************************************************/
214
215void change_file_owner_to_parent(connection_struct *conn,
216 const char *inherit_from_dir,
217 files_struct *fsp)
218{
219 struct smb_filename *smb_fname_parent = NULL;
220 NTSTATUS status;
221 int ret;
222
223 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
224 NULL, NULL, &smb_fname_parent);
225 if (!NT_STATUS_IS_OK(status)) {
226 return;
227 }
228
229 ret = SMB_VFS_STAT(conn, smb_fname_parent);
230 if (ret == -1) {
231 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
232 "directory %s. Error was %s\n",
233 smb_fname_str_dbg(smb_fname_parent),
234 strerror(errno)));
235 TALLOC_FREE(smb_fname_parent);
236 return;
237 }
238
239 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
240 /* Already this uid - no need to change. */
241 DEBUG(10,("change_file_owner_to_parent: file %s "
242 "is already owned by uid %d\n",
243 fsp_str_dbg(fsp),
244 (int)fsp->fsp_name->st.st_ex_uid ));
245 TALLOC_FREE(smb_fname_parent);
246 return;
247 }
248
249 become_root();
250 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
251 unbecome_root();
252 if (ret == -1) {
253 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
254 "file %s to parent directory uid %u. Error "
255 "was %s\n", fsp_str_dbg(fsp),
256 (unsigned int)smb_fname_parent->st.st_ex_uid,
257 strerror(errno) ));
258 } else {
259 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
260 "parent directory uid %u.\n", fsp_str_dbg(fsp),
261 (unsigned int)smb_fname_parent->st.st_ex_uid));
262 /* Ensure the uid entry is updated. */
263 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
264 }
265
266 TALLOC_FREE(smb_fname_parent);
267}
268
269NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
270 const char *inherit_from_dir,
271 const char *fname,
272 SMB_STRUCT_STAT *psbuf)
273{
274 struct smb_filename *smb_fname_parent = NULL;
275 struct smb_filename *smb_fname_cwd = NULL;
276 char *saved_dir = NULL;
277 TALLOC_CTX *ctx = talloc_tos();
278 NTSTATUS status = NT_STATUS_OK;
279 int ret;
280
281 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
282 &smb_fname_parent);
283 if (!NT_STATUS_IS_OK(status)) {
284 return status;
285 }
286
287 ret = SMB_VFS_STAT(conn, smb_fname_parent);
288 if (ret == -1) {
289 status = map_nt_error_from_unix(errno);
290 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
291 "directory %s. Error was %s\n",
292 smb_fname_str_dbg(smb_fname_parent),
293 strerror(errno)));
294 goto out;
295 }
296
297 /* We've already done an lstat into psbuf, and we know it's a
298 directory. If we can cd into the directory and the dev/ino
299 are the same then we can safely chown without races as
300 we're locking the directory in place by being in it. This
301 should work on any UNIX (thanks tridge :-). JRA.
302 */
303
304 saved_dir = vfs_GetWd(ctx,conn);
305 if (!saved_dir) {
306 status = map_nt_error_from_unix(errno);
307 DEBUG(0,("change_dir_owner_to_parent: failed to get "
308 "current working directory. Error was %s\n",
309 strerror(errno)));
310 goto out;
311 }
312
313 /* Chdir into the new path. */
314 if (vfs_ChDir(conn, fname) == -1) {
315 status = map_nt_error_from_unix(errno);
316 DEBUG(0,("change_dir_owner_to_parent: failed to change "
317 "current working directory to %s. Error "
318 "was %s\n", fname, strerror(errno) ));
319 goto chdir;
320 }
321
322 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
323 &smb_fname_cwd);
324 if (!NT_STATUS_IS_OK(status)) {
325 return status;
326 }
327
328 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
329 if (ret == -1) {
330 status = map_nt_error_from_unix(errno);
331 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
332 "directory '.' (%s) Error was %s\n",
333 fname, strerror(errno)));
334 goto chdir;
335 }
336
337 /* Ensure we're pointing at the same place. */
338 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
339 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
340 DEBUG(0,("change_dir_owner_to_parent: "
341 "device/inode on directory %s changed. "
342 "Refusing to chown !\n", fname ));
343 status = NT_STATUS_ACCESS_DENIED;
344 goto chdir;
345 }
346
347 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
348 /* Already this uid - no need to change. */
349 DEBUG(10,("change_dir_owner_to_parent: directory %s "
350 "is already owned by uid %d\n",
351 fname,
352 (int)smb_fname_cwd->st.st_ex_uid ));
353 status = NT_STATUS_OK;
354 goto chdir;
355 }
356
357 become_root();
358 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
359 (gid_t)-1);
360 unbecome_root();
361 if (ret == -1) {
362 status = map_nt_error_from_unix(errno);
363 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
364 "directory %s to parent directory uid %u. "
365 "Error was %s\n", fname,
366 (unsigned int)smb_fname_parent->st.st_ex_uid,
367 strerror(errno) ));
368 goto chdir;
369 }
370
371 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
372 "directory %s to parent directory uid %u.\n",
373 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
374
375 chdir:
376 vfs_ChDir(conn,saved_dir);
377 out:
378 TALLOC_FREE(smb_fname_parent);
379 TALLOC_FREE(smb_fname_cwd);
380 return status;
381}
382
383/****************************************************************************
384 Open a file.
385****************************************************************************/
386
387static NTSTATUS open_file(files_struct *fsp,
388 connection_struct *conn,
389 struct smb_request *req,
390 const char *parent_dir,
391 int flags,
392 mode_t unx_mode,
393 uint32 access_mask, /* client requested access mask. */
394 uint32 open_access_mask) /* what we're actually using in the open. */
395{
396 struct smb_filename *smb_fname = fsp->fsp_name;
397 NTSTATUS status = NT_STATUS_OK;
398 int accmode = (flags & O_ACCMODE);
399 int local_flags = flags;
400 bool file_existed = VALID_STAT(fsp->fsp_name->st);
401 bool file_created = false;
402
403 fsp->fh->fd = -1;
404 errno = EPERM;
405
406 /* Check permissions */
407
408 /*
409 * This code was changed after seeing a client open request
410 * containing the open mode of (DENY_WRITE/read-only) with
411 * the 'create if not exist' bit set. The previous code
412 * would fail to open the file read only on a read-only share
413 * as it was checking the flags parameter directly against O_RDONLY,
414 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
415 * JRA.
416 */
417
418 if (!CAN_WRITE(conn)) {
419 /* It's a read-only share - fail if we wanted to write. */
420 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
421 DEBUG(3,("Permission denied opening %s\n",
422 smb_fname_str_dbg(smb_fname)));
423 return NT_STATUS_ACCESS_DENIED;
424 } else if(flags & O_CREAT) {
425 /* We don't want to write - but we must make sure that
426 O_CREAT doesn't create the file if we have write
427 access into the directory.
428 */
429 flags &= ~(O_CREAT|O_EXCL);
430 local_flags &= ~(O_CREAT|O_EXCL);
431 }
432 }
433
434 /*
435 * This little piece of insanity is inspired by the
436 * fact that an NT client can open a file for O_RDONLY,
437 * but set the create disposition to FILE_EXISTS_TRUNCATE.
438 * If the client *can* write to the file, then it expects to
439 * truncate the file, even though it is opening for readonly.
440 * Quicken uses this stupid trick in backup file creation...
441 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
442 * for helping track this one down. It didn't bite us in 2.0.x
443 * as we always opened files read-write in that release. JRA.
444 */
445
446 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
447 DEBUG(10,("open_file: truncate requested on read-only open "
448 "for file %s\n", smb_fname_str_dbg(smb_fname)));
449 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
450 }
451
452 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
453 (!file_existed && (local_flags & O_CREAT)) ||
454 ((local_flags & O_TRUNC) == O_TRUNC) ) {
455 const char *wild;
456
457 /*
458 * We can't actually truncate here as the file may be locked.
459 * open_file_ntcreate will take care of the truncate later. JRA.
460 */
461
462 local_flags &= ~O_TRUNC;
463
464#if defined(O_NONBLOCK) && defined(S_ISFIFO)
465 /*
466 * We would block on opening a FIFO with no one else on the
467 * other end. Do what we used to do and add O_NONBLOCK to the
468 * open flags. JRA.
469 */
470
471 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
472 local_flags |= O_NONBLOCK;
473 }
474#endif
475
476 /* Don't create files with Microsoft wildcard characters. */
477 if (fsp->base_fsp) {
478 /*
479 * wildcard characters are allowed in stream names
480 * only test the basefilename
481 */
482 wild = fsp->base_fsp->fsp_name->base_name;
483 } else {
484 wild = smb_fname->base_name;
485 }
486 if ((local_flags & O_CREAT) && !file_existed &&
487 ms_has_wild(wild)) {
488 return NT_STATUS_OBJECT_NAME_INVALID;
489 }
490
491 /* Actually do the open */
492 status = fd_open(conn, fsp, local_flags, unx_mode);
493 if (!NT_STATUS_IS_OK(status)) {
494 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
495 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
496 nt_errstr(status),local_flags,flags));
497 return status;
498 }
499
500 if ((local_flags & O_CREAT) && !file_existed) {
501 file_created = true;
502 }
503
504 } else {
505 fsp->fh->fd = -1; /* What we used to call a stat open. */
506 if (file_existed) {
507 uint32_t access_granted = 0;
508
509 status = smbd_check_open_rights(conn,
510 smb_fname,
511 access_mask,
512 &access_granted);
513 if (!NT_STATUS_IS_OK(status)) {
514 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
515 /*
516 * On NT_STATUS_ACCESS_DENIED, access_granted
517 * contains the denied bits.
518 */
519
520 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
521 (access_granted & FILE_WRITE_ATTRIBUTES) &&
522 (lp_map_readonly(SNUM(conn)) ||
523 lp_map_archive(SNUM(conn)) ||
524 lp_map_hidden(SNUM(conn)) ||
525 lp_map_system(SNUM(conn)))) {
526 access_granted &= ~FILE_WRITE_ATTRIBUTES;
527
528 DEBUG(10,("open_file: "
529 "overrode "
530 "FILE_WRITE_"
531 "ATTRIBUTES "
532 "on file %s\n",
533 smb_fname_str_dbg(
534 smb_fname)));
535 }
536
537 if ((access_mask & DELETE_ACCESS) &&
538 (access_granted & DELETE_ACCESS) &&
539 can_delete_file_in_directory(conn,
540 smb_fname)) {
541 /* Were we trying to do a stat open
542 * for delete and didn't get DELETE
543 * access (only) ? Check if the
544 * directory allows DELETE_CHILD.
545 * See here:
546 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
547 * for details. */
548
549 access_granted &= ~DELETE_ACCESS;
550
551 DEBUG(10,("open_file: "
552 "overrode "
553 "DELETE_ACCESS on "
554 "file %s\n",
555 smb_fname_str_dbg(
556 smb_fname)));
557 }
558
559 if (access_granted != 0) {
560 DEBUG(10,("open_file: Access "
561 "denied on file "
562 "%s\n",
563 smb_fname_str_dbg(
564 smb_fname)));
565 return status;
566 }
567 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
568 fsp->posix_open &&
569 S_ISLNK(smb_fname->st.st_ex_mode)) {
570 /* This is a POSIX stat open for delete
571 * or rename on a symlink that points
572 * nowhere. Allow. */
573 DEBUG(10,("open_file: allowing POSIX "
574 "open on bad symlink %s\n",
575 smb_fname_str_dbg(
576 smb_fname)));
577 } else {
578 DEBUG(10,("open_file: "
579 "smbd_check_open_rights on file "
580 "%s returned %s\n",
581 smb_fname_str_dbg(smb_fname),
582 nt_errstr(status) ));
583 return status;
584 }
585 }
586 }
587 }
588
589 if (!file_existed) {
590 int ret;
591
592 if (fsp->fh->fd == -1) {
593 ret = SMB_VFS_STAT(conn, smb_fname);
594 } else {
595 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
596 /* If we have an fd, this stat should succeed. */
597 if (ret == -1) {
598 DEBUG(0,("Error doing fstat on open file %s "
599 "(%s)\n",
600 smb_fname_str_dbg(smb_fname),
601 strerror(errno) ));
602 }
603 }
604
605 /* For a non-io open, this stat failing means file not found. JRA */
606 if (ret == -1) {
607 status = map_nt_error_from_unix(errno);
608 fd_close(fsp);
609 return status;
610 }
611
612 if (file_created) {
613 bool need_re_stat = false;
614 /* Do all inheritance work after we've
615 done a successful stat call and filled
616 in the stat struct in fsp->fsp_name. */
617
618 /* Inherit the ACL if required */
619 if (lp_inherit_perms(SNUM(conn))) {
620 inherit_access_posix_acl(conn, parent_dir,
621 smb_fname->base_name,
622 unx_mode);
623 need_re_stat = true;
624 }
625
626 /* Change the owner if required. */
627 if (lp_inherit_owner(SNUM(conn))) {
628 change_file_owner_to_parent(conn, parent_dir,
629 fsp);
630 need_re_stat = true;
631 }
632
633 if (need_re_stat) {
634 if (fsp->fh->fd == -1) {
635 ret = SMB_VFS_STAT(conn, smb_fname);
636 } else {
637 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
638 /* If we have an fd, this stat should succeed. */
639 if (ret == -1) {
640 DEBUG(0,("Error doing fstat on open file %s "
641 "(%s)\n",
642 smb_fname_str_dbg(smb_fname),
643 strerror(errno) ));
644 }
645 }
646 }
647
648 notify_fname(conn, NOTIFY_ACTION_ADDED,
649 FILE_NOTIFY_CHANGE_FILE_NAME,
650 smb_fname->base_name);
651 }
652 }
653
654 /*
655 * POSIX allows read-only opens of directories. We don't
656 * want to do this (we use a different code path for this)
657 * so catch a directory open and return an EISDIR. JRA.
658 */
659
660 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
661 fd_close(fsp);
662 errno = EISDIR;
663 return NT_STATUS_FILE_IS_A_DIRECTORY;
664 }
665
666 fsp->mode = smb_fname->st.st_ex_mode;
667 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
668 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
669 fsp->file_pid = req ? req->smbpid : 0;
670 fsp->can_lock = True;
671 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
672 if (!CAN_WRITE(conn)) {
673 fsp->can_write = False;
674 } else {
675 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
676 True : False;
677 }
678 fsp->print_file = NULL;
679 fsp->modified = False;
680 fsp->sent_oplock_break = NO_BREAK_SENT;
681 fsp->is_directory = False;
682 if (conn->aio_write_behind_list &&
683 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
684 conn->case_sensitive)) {
685 fsp->aio_write_behind = True;
686 }
687
688 fsp->wcp = NULL; /* Write cache pointer. */
689
690 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
691 conn->session_info->unix_name,
692 smb_fname_str_dbg(smb_fname),
693 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
694 conn->num_files_open));
695
696 errno = 0;
697 return NT_STATUS_OK;
698}
699
700/*******************************************************************
701 Return True if the filename is one of the special executable types.
702********************************************************************/
703
704bool is_executable(const char *fname)
705{
706 if ((fname = strrchr_m(fname,'.'))) {
707 if (strequal(fname,".com") ||
708 strequal(fname,".dll") ||
709 strequal(fname,".exe") ||
710 strequal(fname,".sym")) {
711 return True;
712 }
713 }
714 return False;
715}
716
717/****************************************************************************
718 Check if we can open a file with a share mode.
719 Returns True if conflict, False if not.
720****************************************************************************/
721
722static bool share_conflict(struct share_mode_entry *entry,
723 uint32 access_mask,
724 uint32 share_access)
725{
726 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
727 "entry->share_access = 0x%x, "
728 "entry->private_options = 0x%x\n",
729 (unsigned int)entry->access_mask,
730 (unsigned int)entry->share_access,
731 (unsigned int)entry->private_options));
732
733 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
734 (unsigned int)access_mask, (unsigned int)share_access));
735
736 if ((entry->access_mask & (FILE_WRITE_DATA|
737 FILE_APPEND_DATA|
738 FILE_READ_DATA|
739 FILE_EXECUTE|
740 DELETE_ACCESS)) == 0) {
741 DEBUG(10,("share_conflict: No conflict due to "
742 "entry->access_mask = 0x%x\n",
743 (unsigned int)entry->access_mask ));
744 return False;
745 }
746
747 if ((access_mask & (FILE_WRITE_DATA|
748 FILE_APPEND_DATA|
749 FILE_READ_DATA|
750 FILE_EXECUTE|
751 DELETE_ACCESS)) == 0) {
752 DEBUG(10,("share_conflict: No conflict due to "
753 "access_mask = 0x%x\n",
754 (unsigned int)access_mask ));
755 return False;
756 }
757
758#if 1 /* JRA TEST - Superdebug. */
759#define CHECK_MASK(num, am, right, sa, share) \
760 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
761 (unsigned int)(num), (unsigned int)(am), \
762 (unsigned int)(right), (unsigned int)(am)&(right) )); \
763 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
764 (unsigned int)(num), (unsigned int)(sa), \
765 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
766 if (((am) & (right)) && !((sa) & (share))) { \
767 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
768sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
769 (unsigned int)(share) )); \
770 return True; \
771 }
772#else
773#define CHECK_MASK(num, am, right, sa, share) \
774 if (((am) & (right)) && !((sa) & (share))) { \
775 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
776sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
777 (unsigned int)(share) )); \
778 return True; \
779 }
780#endif
781
782 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
783 share_access, FILE_SHARE_WRITE);
784 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
785 entry->share_access, FILE_SHARE_WRITE);
786
787 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
788 share_access, FILE_SHARE_READ);
789 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
790 entry->share_access, FILE_SHARE_READ);
791
792 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
793 share_access, FILE_SHARE_DELETE);
794 CHECK_MASK(6, access_mask, DELETE_ACCESS,
795 entry->share_access, FILE_SHARE_DELETE);
796
797 DEBUG(10,("share_conflict: No conflict.\n"));
798 return False;
799}
800
801#if defined(DEVELOPER)
802static void validate_my_share_entries(struct smbd_server_connection *sconn,
803 int num,
804 struct share_mode_entry *share_entry)
805{
806 files_struct *fsp;
807
808 if (!procid_is_me(&share_entry->pid)) {
809 return;
810 }
811
812 if (is_deferred_open_entry(share_entry) &&
813 !open_was_deferred(share_entry->op_mid)) {
814 char *str = talloc_asprintf(talloc_tos(),
815 "Got a deferred entry without a request: "
816 "PANIC: %s\n",
817 share_mode_str(talloc_tos(), num, share_entry));
818 smb_panic(str);
819 }
820
821 if (!is_valid_share_mode_entry(share_entry)) {
822 return;
823 }
824
825 fsp = file_find_dif(sconn, share_entry->id,
826 share_entry->share_file_id);
827 if (!fsp) {
828 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
829 share_mode_str(talloc_tos(), num, share_entry) ));
830 smb_panic("validate_my_share_entries: Cannot match a "
831 "share entry with an open file\n");
832 }
833
834 if (is_deferred_open_entry(share_entry) ||
835 is_unused_share_mode_entry(share_entry)) {
836 goto panic;
837 }
838
839 if ((share_entry->op_type == NO_OPLOCK) &&
840 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
841 /* Someone has already written to it, but I haven't yet
842 * noticed */
843 return;
844 }
845
846 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
847 goto panic;
848 }
849
850 return;
851
852 panic:
853 {
854 char *str;
855 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
856 share_mode_str(talloc_tos(), num, share_entry) ));
857 str = talloc_asprintf(talloc_tos(),
858 "validate_my_share_entries: "
859 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
860 fsp->fsp_name->base_name,
861 (unsigned int)fsp->oplock_type,
862 (unsigned int)share_entry->op_type );
863 smb_panic(str);
864 }
865}
866#endif
867
868bool is_stat_open(uint32 access_mask)
869{
870 return (access_mask &&
871 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
872 FILE_WRITE_ATTRIBUTES))==0) &&
873 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
874 FILE_WRITE_ATTRIBUTES)) != 0));
875}
876
877/****************************************************************************
878 Deal with share modes
879 Invarient: Share mode must be locked on entry and exit.
880 Returns -1 on error, or number of share modes on success (may be zero).
881****************************************************************************/
882
883static NTSTATUS open_mode_check(connection_struct *conn,
884 struct share_mode_lock *lck,
885 uint32_t name_hash,
886 uint32 access_mask,
887 uint32 share_access,
888 uint32 create_options,
889 bool *file_existed)
890{
891 int i;
892
893 if(lck->num_share_modes == 0) {
894 return NT_STATUS_OK;
895 }
896
897 *file_existed = True;
898
899 /* A delete on close prohibits everything */
900
901 if (is_delete_on_close_set(lck, name_hash)) {
902 return NT_STATUS_DELETE_PENDING;
903 }
904
905 if (is_stat_open(access_mask)) {
906 /* Stat open that doesn't trigger oplock breaks or share mode
907 * checks... ! JRA. */
908 return NT_STATUS_OK;
909 }
910
911 /*
912 * Check if the share modes will give us access.
913 */
914
915#if defined(DEVELOPER)
916 for(i = 0; i < lck->num_share_modes; i++) {
917 validate_my_share_entries(conn->sconn, i,
918 &lck->share_modes[i]);
919 }
920#endif
921
922 if (!lp_share_modes(SNUM(conn))) {
923 return NT_STATUS_OK;
924 }
925
926 /* Now we check the share modes, after any oplock breaks. */
927 for(i = 0; i < lck->num_share_modes; i++) {
928
929 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
930 continue;
931 }
932
933 /* someone else has a share lock on it, check to see if we can
934 * too */
935 if (share_conflict(&lck->share_modes[i],
936 access_mask, share_access)) {
937 return NT_STATUS_SHARING_VIOLATION;
938 }
939 }
940
941 return NT_STATUS_OK;
942}
943
944static bool is_delete_request(files_struct *fsp) {
945 return ((fsp->access_mask == DELETE_ACCESS) &&
946 (fsp->oplock_type == NO_OPLOCK));
947}
948
949/*
950 * Send a break message to the oplock holder and delay the open for
951 * our client.
952 */
953
954static NTSTATUS send_break_message(files_struct *fsp,
955 struct share_mode_entry *exclusive,
956 uint64_t mid,
957 int oplock_request)
958{
959 NTSTATUS status;
960 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
961
962 DEBUG(10, ("Sending break request to PID %s\n",
963 procid_str_static(&exclusive->pid)));
964 exclusive->op_mid = mid;
965
966 /* Create the message. */
967 share_mode_entry_to_message(msg, exclusive);
968
969 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
970 don't want this set in the share mode struct pointed to by lck. */
971
972 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
973 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
974 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
975 }
976
977 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
978 MSG_SMB_BREAK_REQUEST,
979 (uint8 *)msg,
980 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
981 if (!NT_STATUS_IS_OK(status)) {
982 DEBUG(3, ("Could not send oplock break message: %s\n",
983 nt_errstr(status)));
984 }
985
986 return status;
987}
988
989/*
990 * Return share_mode_entry pointers for :
991 * 1). Batch oplock entry.
992 * 2). Batch or exclusive oplock entry (may be identical to #1).
993 * bool have_level2_oplock
994 * bool have_no_oplock.
995 * Do internal consistency checks on the share mode for a file.
996 */
997
998static void find_oplock_types(files_struct *fsp,
999 int oplock_request,
1000 struct share_mode_lock *lck,
1001 struct share_mode_entry **pp_batch,
1002 struct share_mode_entry **pp_ex_or_batch,
1003 bool *got_level2,
1004 bool *got_no_oplock)
1005{
1006 int i;
1007
1008 *pp_batch = NULL;
1009 *pp_ex_or_batch = NULL;
1010 *got_level2 = false;
1011 *got_no_oplock = false;
1012
1013 /* Ignore stat or internal opens, as is done in
1014 delay_for_batch_oplocks() and
1015 delay_for_exclusive_oplocks().
1016 */
1017 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1018 return;
1019 }
1020
1021 for (i=0; i<lck->num_share_modes; i++) {
1022 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
1023 continue;
1024 }
1025
1026 if (lck->share_modes[i].op_type == NO_OPLOCK &&
1027 is_stat_open(lck->share_modes[i].access_mask)) {
1028 /* We ignore stat opens in the table - they
1029 always have NO_OPLOCK and never get or
1030 cause breaks. JRA. */
1031 continue;
1032 }
1033
1034 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1035 /* batch - can only be one. */
1036 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1037 smb_panic("Bad batch oplock entry.");
1038 }
1039 *pp_batch = &lck->share_modes[i];
1040 }
1041
1042 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1043 /* Exclusive or batch - can only be one. */
1044 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1045 smb_panic("Bad exclusive or batch oplock entry.");
1046 }
1047 *pp_ex_or_batch = &lck->share_modes[i];
1048 }
1049
1050 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1051 if (*pp_batch || *pp_ex_or_batch) {
1052 smb_panic("Bad levelII oplock entry.");
1053 }
1054 *got_level2 = true;
1055 }
1056
1057 if (lck->share_modes[i].op_type == NO_OPLOCK) {
1058 if (*pp_batch || *pp_ex_or_batch) {
1059 smb_panic("Bad no oplock entry.");
1060 }
1061 *got_no_oplock = true;
1062 }
1063 }
1064}
1065
1066static bool delay_for_batch_oplocks(files_struct *fsp,
1067 uint64_t mid,
1068 int oplock_request,
1069 struct share_mode_entry *batch_entry)
1070{
1071 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1072 return false;
1073 }
1074
1075 if (batch_entry != NULL) {
1076 /* Found a batch oplock */
1077 send_break_message(fsp, batch_entry, mid, oplock_request);
1078 return true;
1079 }
1080 return false;
1081}
1082
1083static bool delay_for_exclusive_oplocks(files_struct *fsp,
1084 uint64_t mid,
1085 int oplock_request,
1086 struct share_mode_entry *ex_entry)
1087{
1088 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1089 return false;
1090 }
1091
1092 if (ex_entry != NULL) {
1093 /* Found an exclusive or batch oplock */
1094 bool delay_it = is_delete_request(fsp) ?
1095 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1096 if (delay_it) {
1097 send_break_message(fsp, ex_entry, mid, oplock_request);
1098 return true;
1099 }
1100 }
1101 return false;
1102}
1103
1104static void grant_fsp_oplock_type(files_struct *fsp,
1105 const struct byte_range_lock *br_lck,
1106 int oplock_request,
1107 bool got_level2_oplock,
1108 bool got_a_none_oplock)
1109{
1110 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1111 lp_level2_oplocks(SNUM(fsp->conn));
1112
1113 /* Start by granting what the client asked for,
1114 but ensure no SAMBA_PRIVATE bits can be set. */
1115 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1116
1117 if (oplock_request & INTERNAL_OPEN_ONLY) {
1118 /* No oplocks on internal open. */
1119 fsp->oplock_type = NO_OPLOCK;
1120 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1121 fsp->oplock_type, fsp_str_dbg(fsp)));
1122 return;
1123 } else if (br_lck && br_lck->num_locks > 0) {
1124 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1125 fsp_str_dbg(fsp)));
1126 fsp->oplock_type = NO_OPLOCK;
1127 }
1128
1129 if (is_stat_open(fsp->access_mask)) {
1130 /* Leave the value already set. */
1131 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1132 fsp->oplock_type, fsp_str_dbg(fsp)));
1133 return;
1134 }
1135
1136 /*
1137 * Match what was requested (fsp->oplock_type) with
1138 * what was found in the existing share modes.
1139 */
1140
1141 if (got_a_none_oplock) {
1142 fsp->oplock_type = NO_OPLOCK;
1143 } else if (got_level2_oplock) {
1144 if (fsp->oplock_type == NO_OPLOCK ||
1145 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1146 /* Store a level2 oplock, but don't tell the client */
1147 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1148 } else {
1149 fsp->oplock_type = LEVEL_II_OPLOCK;
1150 }
1151 } else {
1152 /* All share_mode_entries are placeholders or deferred.
1153 * Silently upgrade to fake levelII if the client didn't
1154 * ask for an oplock. */
1155 if (fsp->oplock_type == NO_OPLOCK) {
1156 /* Store a level2 oplock, but don't tell the client */
1157 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1158 }
1159 }
1160
1161 /*
1162 * Don't grant level2 to clients that don't want them
1163 * or if we've turned them off.
1164 */
1165 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1166 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1167 }
1168
1169 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1170 fsp->oplock_type, fsp_str_dbg(fsp)));
1171}
1172
1173bool request_timed_out(struct timeval request_time,
1174 struct timeval timeout)
1175{
1176 struct timeval now, end_time;
1177 GetTimeOfDay(&now);
1178 end_time = timeval_sum(&request_time, &timeout);
1179 return (timeval_compare(&end_time, &now) < 0);
1180}
1181
1182/****************************************************************************
1183 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1184****************************************************************************/
1185
1186static void defer_open(struct share_mode_lock *lck,
1187 struct timeval request_time,
1188 struct timeval timeout,
1189 struct smb_request *req,
1190 struct deferred_open_record *state)
1191{
1192 int i;
1193
1194 /* Paranoia check */
1195
1196 for (i=0; i<lck->num_share_modes; i++) {
1197 struct share_mode_entry *e = &lck->share_modes[i];
1198
1199 if (!is_deferred_open_entry(e)) {
1200 continue;
1201 }
1202
1203 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
1204 DEBUG(0, ("Trying to defer an already deferred "
1205 "request: mid=%llu, exiting\n",
1206 (unsigned long long)req->mid));
1207 exit_server("attempt to defer a deferred request");
1208 }
1209 }
1210
1211 /* End paranoia check */
1212
1213 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1214 "open entry for mid %llu\n",
1215 (unsigned int)request_time.tv_sec,
1216 (unsigned int)request_time.tv_usec,
1217 (unsigned long long)req->mid));
1218
1219 if (!push_deferred_open_message_smb(req, request_time, timeout,
1220 state->id, (char *)state, sizeof(*state))) {
1221 exit_server("push_deferred_open_message_smb failed");
1222 }
1223 add_deferred_open(lck, req->mid, request_time,
1224 sconn_server_id(req->sconn), state->id);
1225}
1226
1227
1228/****************************************************************************
1229 On overwrite open ensure that the attributes match.
1230****************************************************************************/
1231
1232bool open_match_attributes(connection_struct *conn,
1233 uint32 old_dos_attr,
1234 uint32 new_dos_attr,
1235 mode_t existing_unx_mode,
1236 mode_t new_unx_mode,
1237 mode_t *returned_unx_mode)
1238{
1239 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1240
1241 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1242 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1243
1244 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1245 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1246 *returned_unx_mode = new_unx_mode;
1247 } else {
1248 *returned_unx_mode = (mode_t)0;
1249 }
1250
1251 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1252 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1253 "returned_unx_mode = 0%o\n",
1254 (unsigned int)old_dos_attr,
1255 (unsigned int)existing_unx_mode,
1256 (unsigned int)new_dos_attr,
1257 (unsigned int)*returned_unx_mode ));
1258
1259 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1260 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1261 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1262 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1263 return False;
1264 }
1265 }
1266 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1267 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1268 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1269 return False;
1270 }
1271 }
1272 return True;
1273}
1274
1275/****************************************************************************
1276 Special FCB or DOS processing in the case of a sharing violation.
1277 Try and find a duplicated file handle.
1278****************************************************************************/
1279
1280NTSTATUS fcb_or_dos_open(struct smb_request *req,
1281 connection_struct *conn,
1282 files_struct *fsp_to_dup_into,
1283 const struct smb_filename *smb_fname,
1284 struct file_id id,
1285 uint16 file_pid,
1286 uint16 vuid,
1287 uint32 access_mask,
1288 uint32 share_access,
1289 uint32 create_options)
1290{
1291 files_struct *fsp;
1292
1293 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1294 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1295
1296 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1297 fsp = file_find_di_next(fsp)) {
1298
1299 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1300 "vuid = %u, file_pid = %u, private_options = 0x%x "
1301 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1302 fsp->fh->fd, (unsigned int)fsp->vuid,
1303 (unsigned int)fsp->file_pid,
1304 (unsigned int)fsp->fh->private_options,
1305 (unsigned int)fsp->access_mask ));
1306
1307 if (fsp->fh->fd != -1 &&
1308 fsp->vuid == vuid &&
1309 fsp->file_pid == file_pid &&
1310 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1311 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1312 (fsp->access_mask & FILE_WRITE_DATA) &&
1313 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1314 strequal(fsp->fsp_name->stream_name,
1315 smb_fname->stream_name)) {
1316 DEBUG(10,("fcb_or_dos_open: file match\n"));
1317 break;
1318 }
1319 }
1320
1321 if (!fsp) {
1322 return NT_STATUS_NOT_FOUND;
1323 }
1324
1325 /* quite an insane set of semantics ... */
1326 if (is_executable(smb_fname->base_name) &&
1327 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1328 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1329 return NT_STATUS_INVALID_PARAMETER;
1330 }
1331
1332 /* We need to duplicate this fsp. */
1333 return dup_file_fsp(req, fsp, access_mask, share_access,
1334 create_options, fsp_to_dup_into);
1335}
1336
1337/****************************************************************************
1338 Open a file with a share mode - old openX method - map into NTCreate.
1339****************************************************************************/
1340
1341bool map_open_params_to_ntcreate(const struct smb_filename *smb_fname,
1342 int deny_mode, int open_func,
1343 uint32 *paccess_mask,
1344 uint32 *pshare_mode,
1345 uint32 *pcreate_disposition,
1346 uint32 *pcreate_options,
1347 uint32_t *pprivate_flags)
1348{
1349 uint32 access_mask;
1350 uint32 share_mode;
1351 uint32 create_disposition;
1352 uint32 create_options = FILE_NON_DIRECTORY_FILE;
1353 uint32_t private_flags = 0;
1354
1355 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1356 "open_func = 0x%x\n",
1357 smb_fname_str_dbg(smb_fname), (unsigned int)deny_mode,
1358 (unsigned int)open_func ));
1359
1360 /* Create the NT compatible access_mask. */
1361 switch (GET_OPENX_MODE(deny_mode)) {
1362 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1363 case DOS_OPEN_RDONLY:
1364 access_mask = FILE_GENERIC_READ;
1365 break;
1366 case DOS_OPEN_WRONLY:
1367 access_mask = FILE_GENERIC_WRITE;
1368 break;
1369 case DOS_OPEN_RDWR:
1370 case DOS_OPEN_FCB:
1371 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1372 break;
1373 default:
1374 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1375 (unsigned int)GET_OPENX_MODE(deny_mode)));
1376 return False;
1377 }
1378
1379 /* Create the NT compatible create_disposition. */
1380 switch (open_func) {
1381 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1382 create_disposition = FILE_CREATE;
1383 break;
1384
1385 case OPENX_FILE_EXISTS_OPEN:
1386 create_disposition = FILE_OPEN;
1387 break;
1388
1389 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1390 create_disposition = FILE_OPEN_IF;
1391 break;
1392
1393 case OPENX_FILE_EXISTS_TRUNCATE:
1394 create_disposition = FILE_OVERWRITE;
1395 break;
1396
1397 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1398 create_disposition = FILE_OVERWRITE_IF;
1399 break;
1400
1401 default:
1402 /* From samba4 - to be confirmed. */
1403 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1404 create_disposition = FILE_CREATE;
1405 break;
1406 }
1407 DEBUG(10,("map_open_params_to_ntcreate: bad "
1408 "open_func 0x%x\n", (unsigned int)open_func));
1409 return False;
1410 }
1411
1412 /* Create the NT compatible share modes. */
1413 switch (GET_DENY_MODE(deny_mode)) {
1414 case DENY_ALL:
1415 share_mode = FILE_SHARE_NONE;
1416 break;
1417
1418 case DENY_WRITE:
1419 share_mode = FILE_SHARE_READ;
1420 break;
1421
1422 case DENY_READ:
1423 share_mode = FILE_SHARE_WRITE;
1424 break;
1425
1426 case DENY_NONE:
1427 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1428 break;
1429
1430 case DENY_DOS:
1431 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1432 if (is_executable(smb_fname->base_name)) {
1433 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1434 } else {
1435 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1436 share_mode = FILE_SHARE_READ;
1437 } else {
1438 share_mode = FILE_SHARE_NONE;
1439 }
1440 }
1441 break;
1442
1443 case DENY_FCB:
1444 private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1445 share_mode = FILE_SHARE_NONE;
1446 break;
1447
1448 default:
1449 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1450 (unsigned int)GET_DENY_MODE(deny_mode) ));
1451 return False;
1452 }
1453
1454 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1455 "share_mode = 0x%x, create_disposition = 0x%x, "
1456 "create_options = 0x%x private_flags = 0x%x\n",
1457 smb_fname_str_dbg(smb_fname),
1458 (unsigned int)access_mask,
1459 (unsigned int)share_mode,
1460 (unsigned int)create_disposition,
1461 (unsigned int)create_options,
1462 (unsigned int)private_flags));
1463
1464 if (paccess_mask) {
1465 *paccess_mask = access_mask;
1466 }
1467 if (pshare_mode) {
1468 *pshare_mode = share_mode;
1469 }
1470 if (pcreate_disposition) {
1471 *pcreate_disposition = create_disposition;
1472 }
1473 if (pcreate_options) {
1474 *pcreate_options = create_options;
1475 }
1476 if (pprivate_flags) {
1477 *pprivate_flags = private_flags;
1478 }
1479
1480 return True;
1481
1482}
1483
1484static void schedule_defer_open(struct share_mode_lock *lck,
1485 struct timeval request_time,
1486 struct smb_request *req)
1487{
1488 struct deferred_open_record state;
1489
1490 /* This is a relative time, added to the absolute
1491 request_time value to get the absolute timeout time.
1492 Note that if this is the second or greater time we enter
1493 this codepath for this particular request mid then
1494 request_time is left as the absolute time of the *first*
1495 time this request mid was processed. This is what allows
1496 the request to eventually time out. */
1497
1498 struct timeval timeout;
1499
1500 /* Normally the smbd we asked should respond within
1501 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1502 * the client did, give twice the timeout as a safety
1503 * measure here in case the other smbd is stuck
1504 * somewhere else. */
1505
1506 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1507
1508 /* Nothing actually uses state.delayed_for_oplocks
1509 but it's handy to differentiate in debug messages
1510 between a 30 second delay due to oplock break, and
1511 a 1 second delay for share mode conflicts. */
1512
1513 state.delayed_for_oplocks = True;
1514 state.id = lck->id;
1515
1516 if (!request_timed_out(request_time, timeout)) {
1517 defer_open(lck, request_time, timeout, req, &state);
1518 }
1519}
1520
1521/****************************************************************************
1522 Work out what access_mask to use from what the client sent us.
1523****************************************************************************/
1524
1525NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1526 const struct smb_filename *smb_fname,
1527 bool file_existed,
1528 uint32_t access_mask,
1529 uint32_t *access_mask_out)
1530{
1531 NTSTATUS status;
1532 uint32_t orig_access_mask = access_mask;
1533 uint32_t rejected_share_access;
1534
1535 /*
1536 * Convert GENERIC bits to specific bits.
1537 */
1538
1539 se_map_generic(&access_mask, &file_generic_mapping);
1540
1541 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1542 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1543 if (file_existed) {
1544
1545 struct security_descriptor *sd;
1546 uint32_t access_granted = 0;
1547
1548 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1549 (SECINFO_OWNER |
1550 SECINFO_GROUP |
1551 SECINFO_DACL),&sd);
1552
1553 if (!NT_STATUS_IS_OK(status)) {
1554 DEBUG(10,("smbd_calculate_access_mask: "
1555 "Could not get acl on file %s: %s\n",
1556 smb_fname_str_dbg(smb_fname),
1557 nt_errstr(status)));
1558 return NT_STATUS_ACCESS_DENIED;
1559 }
1560
1561 status = smb1_file_se_access_check(conn,
1562 sd,
1563 get_current_nttok(conn),
1564 access_mask,
1565 &access_granted);
1566
1567 TALLOC_FREE(sd);
1568
1569 if (!NT_STATUS_IS_OK(status)) {
1570 DEBUG(10, ("smbd_calculate_access_mask: "
1571 "Access denied on file %s: "
1572 "when calculating maximum access\n",
1573 smb_fname_str_dbg(smb_fname)));
1574 return NT_STATUS_ACCESS_DENIED;
1575 }
1576
1577 access_mask = access_granted;
1578 } else {
1579 access_mask = FILE_GENERIC_ALL;
1580 }
1581
1582 access_mask &= conn->share_access;
1583 }
1584
1585 rejected_share_access = access_mask & ~(conn->share_access);
1586
1587 if (rejected_share_access) {
1588 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1589 "file %s: rejected by share access mask[0x%08X] "
1590 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1591 smb_fname_str_dbg(smb_fname),
1592 conn->share_access,
1593 orig_access_mask, access_mask,
1594 rejected_share_access));
1595 return NT_STATUS_ACCESS_DENIED;
1596 }
1597
1598 *access_mask_out = access_mask;
1599 return NT_STATUS_OK;
1600}
1601
1602/****************************************************************************
1603 Remove the deferred open entry under lock.
1604****************************************************************************/
1605
1606void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1607 struct server_id pid)
1608{
1609 struct share_mode_lock *lck = get_share_mode_lock(talloc_tos(), id,
1610 NULL, NULL, NULL);
1611 if (lck == NULL) {
1612 DEBUG(0, ("could not get share mode lock\n"));
1613 } else {
1614 del_deferred_open_entry(lck, mid, pid);
1615 TALLOC_FREE(lck);
1616 }
1617}
1618
1619/****************************************************************
1620 Ensure we get the brlock lock followed by the share mode lock
1621 in the correct order to prevent deadlocks if other smbd's are
1622 using the brlock database on this file simultaneously with this open
1623 (that code also gets the locks in brlock -> share mode lock order).
1624****************************************************************/
1625
1626static bool acquire_ordered_locks(TALLOC_CTX *mem_ctx,
1627 files_struct *fsp,
1628 const struct file_id id,
1629 const char *connectpath,
1630 const struct smb_filename *smb_fname,
1631 const struct timespec *p_old_write_time,
1632 struct share_mode_lock **p_lck,
1633 struct byte_range_lock **p_br_lck)
1634{
1635 /* Ordering - we must get the br_lck for this
1636 file before the share mode. */
1637 if (lp_locking(fsp->conn->params)) {
1638 *p_br_lck = brl_get_locks_readonly(fsp);
1639 if (*p_br_lck == NULL) {
1640 DEBUG(0, ("Could not get br_lock\n"));
1641 return false;
1642 }
1643 /* Note - we don't need to free the returned
1644 br_lck explicitly as it was allocated on talloc_tos()
1645 and so will be autofreed (and release the lock)
1646 once the frame context disappears.
1647
1648 If it was set to fsp->brlock_rec then it was
1649 talloc_move'd to hang off the fsp pointer and
1650 in this case is guarenteed to not be holding the
1651 lock on the brlock database. */
1652 }
1653
1654 *p_lck = get_share_mode_lock(mem_ctx,
1655 id,
1656 connectpath,
1657 smb_fname,
1658 p_old_write_time);
1659
1660 if (*p_lck == NULL) {
1661 DEBUG(0, ("Could not get share mode lock\n"));
1662 TALLOC_FREE(*p_br_lck);
1663 return false;
1664 }
1665 return true;
1666}
1667
1668/****************************************************************************
1669 Open a file with a share mode. Passed in an already created files_struct *.
1670****************************************************************************/
1671
1672static NTSTATUS open_file_ntcreate(connection_struct *conn,
1673 struct smb_request *req,
1674 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1675 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1676 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1677 uint32 create_options, /* options such as delete on close. */
1678 uint32 new_dos_attributes, /* attributes used for new file. */
1679 int oplock_request, /* internal Samba oplock codes. */
1680 /* Information (FILE_EXISTS etc.) */
1681 uint32_t private_flags, /* Samba specific flags. */
1682 int *pinfo,
1683 files_struct *fsp)
1684{
1685 struct smb_filename *smb_fname = fsp->fsp_name;
1686 int flags=0;
1687 int flags2=0;
1688 bool file_existed = VALID_STAT(smb_fname->st);
1689 bool def_acl = False;
1690 bool posix_open = False;
1691 bool new_file_created = False;
1692 bool clear_ads = false;
1693 struct file_id id;
1694 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1695 mode_t new_unx_mode = (mode_t)0;
1696 mode_t unx_mode = (mode_t)0;
1697 int info;
1698 uint32 existing_dos_attributes = 0;
1699 struct timeval request_time = timeval_zero();
1700 struct share_mode_lock *lck = NULL;
1701 uint32 open_access_mask = access_mask;
1702 NTSTATUS status;
1703 char *parent_dir;
1704
1705 ZERO_STRUCT(id);
1706
1707 /* Windows allows a new file to be created and
1708 silently removes a FILE_ATTRIBUTE_DIRECTORY
1709 sent by the client. Do the same. */
1710
1711 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1712
1713 if (conn->printer) {
1714 /*
1715 * Printers are handled completely differently.
1716 * Most of the passed parameters are ignored.
1717 */
1718
1719 if (pinfo) {
1720 *pinfo = FILE_WAS_CREATED;
1721 }
1722
1723 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1724 smb_fname_str_dbg(smb_fname)));
1725
1726 if (!req) {
1727 DEBUG(0,("open_file_ntcreate: printer open without "
1728 "an SMB request!\n"));
1729 return NT_STATUS_INTERNAL_ERROR;
1730 }
1731
1732 return print_spool_open(fsp, smb_fname->base_name,
1733 req->vuid);
1734 }
1735
1736 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1737 NULL)) {
1738 return NT_STATUS_NO_MEMORY;
1739 }
1740
1741 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1742 posix_open = True;
1743 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1744 new_dos_attributes = 0;
1745 } else {
1746 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1747 * created new. */
1748 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1749 smb_fname, parent_dir);
1750 }
1751
1752 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1753 "access_mask=0x%x share_access=0x%x "
1754 "create_disposition = 0x%x create_options=0x%x "
1755 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1756 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1757 access_mask, share_access, create_disposition,
1758 create_options, (unsigned int)unx_mode, oplock_request,
1759 (unsigned int)private_flags));
1760
1761 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1762 DEBUG(0, ("No smb request but not an internal only open!\n"));
1763 return NT_STATUS_INTERNAL_ERROR;
1764 }
1765
1766 /*
1767 * Only non-internal opens can be deferred at all
1768 */
1769
1770 if (req) {
1771 void *ptr;
1772 if (get_deferred_open_message_state(req,
1773 &request_time,
1774 &ptr)) {
1775
1776 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1777 /* Remember the absolute time of the original
1778 request with this mid. We'll use it later to
1779 see if this has timed out. */
1780
1781 /* Remove the deferred open entry under lock. */
1782 remove_deferred_open_entry(
1783 state->id, req->mid,
1784 sconn_server_id(req->sconn));
1785
1786 /* Ensure we don't reprocess this message. */
1787 remove_deferred_open_message_smb(req->mid);
1788 }
1789 }
1790
1791 status = check_name(conn, smb_fname->base_name);
1792 if (!NT_STATUS_IS_OK(status)) {
1793 return status;
1794 }
1795
1796 if (!posix_open) {
1797 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1798 if (file_existed) {
1799 existing_dos_attributes = dos_mode(conn, smb_fname);
1800 }
1801 }
1802
1803 /* ignore any oplock requests if oplocks are disabled */
1804 if (!lp_oplocks(SNUM(conn)) ||
1805 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1806 /* Mask off everything except the private Samba bits. */
1807 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1808 }
1809
1810 /* this is for OS/2 long file names - say we don't support them */
1811 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1812 /* OS/2 Workplace shell fix may be main code stream in a later
1813 * release. */
1814 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1815 "supported.\n"));
1816 if (use_nt_status()) {
1817 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1818 }
1819 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1820 }
1821
1822 switch( create_disposition ) {
1823 /*
1824 * Currently we're using FILE_SUPERSEDE as the same as
1825 * FILE_OVERWRITE_IF but they really are
1826 * different. FILE_SUPERSEDE deletes an existing file
1827 * (requiring delete access) then recreates it.
1828 */
1829 case FILE_SUPERSEDE:
1830 /* If file exists replace/overwrite. If file doesn't
1831 * exist create. */
1832 flags2 |= (O_CREAT | O_TRUNC);
1833 clear_ads = true;
1834 break;
1835
1836 case FILE_OVERWRITE_IF:
1837 /* If file exists replace/overwrite. If file doesn't
1838 * exist create. */
1839 flags2 |= (O_CREAT | O_TRUNC);
1840 clear_ads = true;
1841 break;
1842
1843 case FILE_OPEN:
1844 /* If file exists open. If file doesn't exist error. */
1845 if (!file_existed) {
1846 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1847 "requested for file %s and file "
1848 "doesn't exist.\n",
1849 smb_fname_str_dbg(smb_fname)));
1850 errno = ENOENT;
1851 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1852 }
1853 break;
1854
1855 case FILE_OVERWRITE:
1856 /* If file exists overwrite. If file doesn't exist
1857 * error. */
1858 if (!file_existed) {
1859 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1860 "requested for file %s and file "
1861 "doesn't exist.\n",
1862 smb_fname_str_dbg(smb_fname) ));
1863 errno = ENOENT;
1864 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1865 }
1866 flags2 |= O_TRUNC;
1867 clear_ads = true;
1868 break;
1869
1870 case FILE_CREATE:
1871 /* If file exists error. If file doesn't exist
1872 * create. */
1873 if (file_existed) {
1874 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1875 "requested for file %s and file "
1876 "already exists.\n",
1877 smb_fname_str_dbg(smb_fname)));
1878 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1879 errno = EISDIR;
1880 } else {
1881 errno = EEXIST;
1882 }
1883 return map_nt_error_from_unix(errno);
1884 }
1885 flags2 |= (O_CREAT|O_EXCL);
1886 break;
1887
1888 case FILE_OPEN_IF:
1889 /* If file exists open. If file doesn't exist
1890 * create. */
1891 flags2 |= O_CREAT;
1892 break;
1893
1894 default:
1895 return NT_STATUS_INVALID_PARAMETER;
1896 }
1897
1898 /* We only care about matching attributes on file exists and
1899 * overwrite. */
1900
1901 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1902 (create_disposition == FILE_OVERWRITE_IF))) {
1903 if (!open_match_attributes(conn, existing_dos_attributes,
1904 new_dos_attributes,
1905 smb_fname->st.st_ex_mode,
1906 unx_mode, &new_unx_mode)) {
1907 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1908 "for file %s (%x %x) (0%o, 0%o)\n",
1909 smb_fname_str_dbg(smb_fname),
1910 existing_dos_attributes,
1911 new_dos_attributes,
1912 (unsigned int)smb_fname->st.st_ex_mode,
1913 (unsigned int)unx_mode ));
1914 errno = EACCES;
1915 return NT_STATUS_ACCESS_DENIED;
1916 }
1917 }
1918
1919 status = smbd_calculate_access_mask(conn, smb_fname, file_existed,
1920 access_mask,
1921 &access_mask);
1922 if (!NT_STATUS_IS_OK(status)) {
1923 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1924 "on file %s returned %s\n",
1925 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1926 return status;
1927 }
1928
1929 open_access_mask = access_mask;
1930
1931 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1932 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1933 }
1934
1935 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1936 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1937 access_mask));
1938
1939 /*
1940 * Note that we ignore the append flag as append does not
1941 * mean the same thing under DOS and Unix.
1942 */
1943
1944 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1945 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1946 /* DENY_DOS opens are always underlying read-write on the
1947 file handle, no matter what the requested access mask
1948 says. */
1949 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1950 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1951 flags = O_RDWR;
1952 } else {
1953 flags = O_WRONLY;
1954 }
1955 } else {
1956 flags = O_RDONLY;
1957 }
1958
1959 /*
1960 * Currently we only look at FILE_WRITE_THROUGH for create options.
1961 */
1962
1963#if defined(O_SYNC)
1964 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1965 flags2 |= O_SYNC;
1966 }
1967#endif /* O_SYNC */
1968
1969 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1970 flags2 |= O_APPEND;
1971 }
1972
1973 if (!posix_open && !CAN_WRITE(conn)) {
1974 /*
1975 * We should really return a permission denied error if either
1976 * O_CREAT or O_TRUNC are set, but for compatibility with
1977 * older versions of Samba we just AND them out.
1978 */
1979 flags2 &= ~(O_CREAT|O_TRUNC);
1980 }
1981
1982 /*
1983 * Ensure we can't write on a read-only share or file.
1984 */
1985
1986 if (flags != O_RDONLY && file_existed &&
1987 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1988 DEBUG(5,("open_file_ntcreate: write access requested for "
1989 "file %s on read only %s\n",
1990 smb_fname_str_dbg(smb_fname),
1991 !CAN_WRITE(conn) ? "share" : "file" ));
1992 errno = EACCES;
1993 return NT_STATUS_ACCESS_DENIED;
1994 }
1995
1996 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1997 fsp->share_access = share_access;
1998 fsp->fh->private_options = private_flags;
1999 fsp->access_mask = open_access_mask; /* We change this to the
2000 * requested access_mask after
2001 * the open is done. */
2002 fsp->posix_open = posix_open;
2003
2004 /* Ensure no SAMBA_PRIVATE bits can be set. */
2005 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2006
2007 if (timeval_is_zero(&request_time)) {
2008 request_time = fsp->open_time;
2009 }
2010
2011 if (file_existed) {
2012 struct byte_range_lock *br_lck = NULL;
2013 struct share_mode_entry *batch_entry = NULL;
2014 struct share_mode_entry *exclusive_entry = NULL;
2015 bool got_level2_oplock = false;
2016 bool got_a_none_oplock = false;
2017
2018 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2019 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2020
2021 if (!acquire_ordered_locks(talloc_tos(),
2022 fsp,
2023 id,
2024 conn->connectpath,
2025 smb_fname,
2026 &old_write_time,
2027 &lck,
2028 &br_lck)) {
2029 return NT_STATUS_SHARING_VIOLATION;
2030 }
2031
2032 /* Get the types we need to examine. */
2033 find_oplock_types(fsp,
2034 oplock_request,
2035 lck,
2036 &batch_entry,
2037 &exclusive_entry,
2038 &got_level2_oplock,
2039 &got_a_none_oplock);
2040
2041 /* First pass - send break only on batch oplocks. */
2042 if ((req != NULL) &&
2043 delay_for_batch_oplocks(fsp,
2044 req->mid,
2045 oplock_request,
2046 batch_entry)) {
2047 schedule_defer_open(lck, request_time, req);
2048 TALLOC_FREE(lck);
2049 return NT_STATUS_SHARING_VIOLATION;
2050 }
2051
2052 /* Use the client requested access mask here, not the one we
2053 * open with. */
2054 status = open_mode_check(conn, lck, fsp->name_hash,
2055 access_mask, share_access,
2056 create_options, &file_existed);
2057
2058 if (NT_STATUS_IS_OK(status)) {
2059 /* We might be going to allow this open. Check oplock
2060 * status again. */
2061 /* Second pass - send break for both batch or
2062 * exclusive oplocks. */
2063 if ((req != NULL) &&
2064 delay_for_exclusive_oplocks(
2065 fsp,
2066 req->mid,
2067 oplock_request,
2068 exclusive_entry)) {
2069 schedule_defer_open(lck, request_time, req);
2070 TALLOC_FREE(lck);
2071 return NT_STATUS_SHARING_VIOLATION;
2072 }
2073 }
2074
2075 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2076 /* DELETE_PENDING is not deferred for a second */
2077 TALLOC_FREE(lck);
2078 return status;
2079 }
2080
2081 grant_fsp_oplock_type(fsp,
2082 br_lck,
2083 oplock_request,
2084 got_level2_oplock,
2085 got_a_none_oplock);
2086
2087 if (!NT_STATUS_IS_OK(status)) {
2088 uint32 can_access_mask;
2089 bool can_access = True;
2090
2091 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2092
2093 /* Check if this can be done with the deny_dos and fcb
2094 * calls. */
2095 if (private_flags &
2096 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2097 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2098 if (req == NULL) {
2099 DEBUG(0, ("DOS open without an SMB "
2100 "request!\n"));
2101 TALLOC_FREE(lck);
2102 return NT_STATUS_INTERNAL_ERROR;
2103 }
2104
2105 /* Use the client requested access mask here,
2106 * not the one we open with. */
2107 status = fcb_or_dos_open(req,
2108 conn,
2109 fsp,
2110 smb_fname,
2111 id,
2112 req->smbpid,
2113 req->vuid,
2114 access_mask,
2115 share_access,
2116 create_options);
2117
2118 if (NT_STATUS_IS_OK(status)) {
2119 TALLOC_FREE(lck);
2120 if (pinfo) {
2121 *pinfo = FILE_WAS_OPENED;
2122 }
2123 return NT_STATUS_OK;
2124 }
2125 }
2126
2127 /*
2128 * This next line is a subtlety we need for
2129 * MS-Access. If a file open will fail due to share
2130 * permissions and also for security (access) reasons,
2131 * we need to return the access failed error, not the
2132 * share error. We can't open the file due to kernel
2133 * oplock deadlock (it's possible we failed above on
2134 * the open_mode_check()) so use a userspace check.
2135 */
2136
2137 if (flags & O_RDWR) {
2138 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2139 } else if (flags & O_WRONLY) {
2140 can_access_mask = FILE_WRITE_DATA;
2141 } else {
2142 can_access_mask = FILE_READ_DATA;
2143 }
2144
2145 if (((can_access_mask & FILE_WRITE_DATA) &&
2146 !CAN_WRITE(conn)) ||
2147 !can_access_file_data(conn, smb_fname,
2148 can_access_mask)) {
2149 can_access = False;
2150 }
2151
2152 /*
2153 * If we're returning a share violation, ensure we
2154 * cope with the braindead 1 second delay.
2155 */
2156
2157 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2158 lp_defer_sharing_violations()) {
2159 struct timeval timeout;
2160 struct deferred_open_record state;
2161 int timeout_usecs;
2162
2163 /* this is a hack to speed up torture tests
2164 in 'make test' */
2165 timeout_usecs = lp_parm_int(SNUM(conn),
2166 "smbd","sharedelay",
2167 SHARING_VIOLATION_USEC_WAIT);
2168
2169 /* This is a relative time, added to the absolute
2170 request_time value to get the absolute timeout time.
2171 Note that if this is the second or greater time we enter
2172 this codepath for this particular request mid then
2173 request_time is left as the absolute time of the *first*
2174 time this request mid was processed. This is what allows
2175 the request to eventually time out. */
2176
2177 timeout = timeval_set(0, timeout_usecs);
2178
2179 /* Nothing actually uses state.delayed_for_oplocks
2180 but it's handy to differentiate in debug messages
2181 between a 30 second delay due to oplock break, and
2182 a 1 second delay for share mode conflicts. */
2183
2184 state.delayed_for_oplocks = False;
2185 state.id = id;
2186
2187 if ((req != NULL)
2188 && !request_timed_out(request_time,
2189 timeout)) {
2190 defer_open(lck, request_time, timeout,
2191 req, &state);
2192 }
2193 }
2194
2195 TALLOC_FREE(lck);
2196 if (can_access) {
2197 /*
2198 * We have detected a sharing violation here
2199 * so return the correct error code
2200 */
2201 status = NT_STATUS_SHARING_VIOLATION;
2202 } else {
2203 status = NT_STATUS_ACCESS_DENIED;
2204 }
2205 return status;
2206 }
2207
2208 /*
2209 * We exit this block with the share entry *locked*.....
2210 */
2211 }
2212
2213 SMB_ASSERT(!file_existed || (lck != NULL));
2214
2215 /*
2216 * Ensure we pay attention to default ACLs on directories if required.
2217 */
2218
2219 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2220 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2221 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2222 }
2223
2224 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2225 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2226 (unsigned int)flags, (unsigned int)flags2,
2227 (unsigned int)unx_mode, (unsigned int)access_mask,
2228 (unsigned int)open_access_mask));
2229
2230 /*
2231 * open_file strips any O_TRUNC flags itself.
2232 */
2233
2234 fsp_open = open_file(fsp, conn, req, parent_dir,
2235 flags|flags2, unx_mode, access_mask,
2236 open_access_mask);
2237
2238 if (!NT_STATUS_IS_OK(fsp_open)) {
2239 if (lck != NULL) {
2240 TALLOC_FREE(lck);
2241 }
2242 return fsp_open;
2243 }
2244
2245 if (!file_existed) {
2246 struct byte_range_lock *br_lck = NULL;
2247 struct share_mode_entry *batch_entry = NULL;
2248 struct share_mode_entry *exclusive_entry = NULL;
2249 bool got_level2_oplock = false;
2250 bool got_a_none_oplock = false;
2251 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2252 /*
2253 * Deal with the race condition where two smbd's detect the
2254 * file doesn't exist and do the create at the same time. One
2255 * of them will win and set a share mode, the other (ie. this
2256 * one) should check if the requested share mode for this
2257 * create is allowed.
2258 */
2259
2260 /*
2261 * Now the file exists and fsp is successfully opened,
2262 * fsp->dev and fsp->inode are valid and should replace the
2263 * dev=0,inode=0 from a non existent file. Spotted by
2264 * Nadav Danieli <nadavd@exanet.com>. JRA.
2265 */
2266
2267 id = fsp->file_id;
2268
2269 if (!acquire_ordered_locks(talloc_tos(),
2270 fsp,
2271 id,
2272 conn->connectpath,
2273 smb_fname,
2274 &old_write_time,
2275 &lck,
2276 &br_lck)) {
2277 return NT_STATUS_SHARING_VIOLATION;
2278 }
2279
2280 /* Get the types we need to examine. */
2281 find_oplock_types(fsp,
2282 oplock_request,
2283 lck,
2284 &batch_entry,
2285 &exclusive_entry,
2286 &got_level2_oplock,
2287 &got_a_none_oplock);
2288
2289 /* First pass - send break only on batch oplocks. */
2290 if ((req != NULL) &&
2291 delay_for_batch_oplocks(fsp,
2292 req->mid,
2293 oplock_request,
2294 batch_entry)) {
2295 schedule_defer_open(lck, request_time, req);
2296 TALLOC_FREE(lck);
2297 fd_close(fsp);
2298 return NT_STATUS_SHARING_VIOLATION;
2299 }
2300
2301 status = open_mode_check(conn, lck, fsp->name_hash,
2302 access_mask, share_access,
2303 create_options, &file_existed);
2304
2305 if (NT_STATUS_IS_OK(status)) {
2306 /* We might be going to allow this open. Check oplock
2307 * status again. */
2308 /* Second pass - send break for both batch or
2309 * exclusive oplocks. */
2310 if ((req != NULL) &&
2311 delay_for_exclusive_oplocks(
2312 fsp,
2313 req->mid,
2314 oplock_request,
2315 exclusive_entry)) {
2316 schedule_defer_open(lck, request_time, req);
2317 TALLOC_FREE(lck);
2318 fd_close(fsp);
2319 return NT_STATUS_SHARING_VIOLATION;
2320 }
2321 }
2322
2323 if (!NT_STATUS_IS_OK(status)) {
2324 struct deferred_open_record state;
2325
2326 fd_close(fsp);
2327
2328 state.delayed_for_oplocks = False;
2329 state.id = id;
2330
2331 /* Do it all over again immediately. In the second
2332 * round we will find that the file existed and handle
2333 * the DELETE_PENDING and FCB cases correctly. No need
2334 * to duplicate the code here. Essentially this is a
2335 * "goto top of this function", but don't tell
2336 * anybody... */
2337
2338 if (req != NULL) {
2339 defer_open(lck, request_time, timeval_zero(),
2340 req, &state);
2341 }
2342 TALLOC_FREE(lck);
2343 return status;
2344 }
2345
2346 grant_fsp_oplock_type(fsp,
2347 br_lck,
2348 oplock_request,
2349 got_level2_oplock,
2350 got_a_none_oplock);
2351
2352 /*
2353 * We exit this block with the share entry *locked*.....
2354 */
2355
2356 }
2357
2358 SMB_ASSERT(lck != NULL);
2359
2360 /* Delete streams if create_disposition requires it */
2361 if (file_existed && clear_ads &&
2362 !is_ntfs_stream_smb_fname(smb_fname)) {
2363 status = delete_all_streams(conn, smb_fname->base_name);
2364 if (!NT_STATUS_IS_OK(status)) {
2365 TALLOC_FREE(lck);
2366 fd_close(fsp);
2367 return status;
2368 }
2369 }
2370
2371 /* note that we ignore failure for the following. It is
2372 basically a hack for NFS, and NFS will never set one of
2373 these only read them. Nobody but Samba can ever set a deny
2374 mode and we have already checked our more authoritative
2375 locking database for permission to set this deny mode. If
2376 the kernel refuses the operations then the kernel is wrong.
2377 note that GPFS supports it as well - jmcd */
2378
2379 if (fsp->fh->fd != -1) {
2380 int ret_flock;
2381 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2382 if(ret_flock == -1 ){
2383
2384 TALLOC_FREE(lck);
2385 fd_close(fsp);
2386
2387 return NT_STATUS_SHARING_VIOLATION;
2388 }
2389 }
2390
2391 /*
2392 * At this point onwards, we can guarentee that the share entry
2393 * is locked, whether we created the file or not, and that the
2394 * deny mode is compatible with all current opens.
2395 */
2396
2397 /*
2398 * If requested, truncate the file.
2399 */
2400
2401 if (file_existed && (flags2&O_TRUNC)) {
2402 /*
2403 * We are modifing the file after open - update the stat
2404 * struct..
2405 */
2406 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2407 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2408 status = map_nt_error_from_unix(errno);
2409 TALLOC_FREE(lck);
2410 fd_close(fsp);
2411 return status;
2412 }
2413 }
2414
2415 /*
2416 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2417 * but we don't have to store this - just ignore it on access check.
2418 */
2419 if (conn->sconn->using_smb2) {
2420 /*
2421 * SMB2 doesn't return it (according to Microsoft tests).
2422 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2423 * File created with access = 0x7 (Read, Write, Delete)
2424 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2425 */
2426 fsp->access_mask = access_mask;
2427 } else {
2428 /* But SMB1 does. */
2429 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2430 }
2431
2432 if (file_existed) {
2433 /* stat opens on existing files don't get oplocks. */
2434 if (is_stat_open(open_access_mask)) {
2435 fsp->oplock_type = NO_OPLOCK;
2436 }
2437
2438 if (!(flags2 & O_TRUNC)) {
2439 info = FILE_WAS_OPENED;
2440 } else {
2441 info = FILE_WAS_OVERWRITTEN;
2442 }
2443 } else {
2444 info = FILE_WAS_CREATED;
2445 }
2446
2447 if (pinfo) {
2448 *pinfo = info;
2449 }
2450
2451 /*
2452 * Setup the oplock info in both the shared memory and
2453 * file structs.
2454 */
2455
2456 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2457 /*
2458 * Could not get the kernel oplock or there are byte-range
2459 * locks on the file.
2460 */
2461 fsp->oplock_type = NO_OPLOCK;
2462 }
2463
2464 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
2465 new_file_created = True;
2466 }
2467
2468 set_share_mode(lck, fsp, get_current_uid(conn),
2469 req ? req->mid : 0,
2470 fsp->oplock_type);
2471
2472 /* Handle strange delete on close create semantics. */
2473 if (create_options & FILE_DELETE_ON_CLOSE) {
2474
2475 status = can_set_delete_on_close(fsp, new_dos_attributes);
2476
2477 if (!NT_STATUS_IS_OK(status)) {
2478 /* Remember to delete the mode we just added. */
2479 del_share_mode(lck, fsp);
2480 TALLOC_FREE(lck);
2481 fd_close(fsp);
2482 return status;
2483 }
2484 /* Note that here we set the *inital* delete on close flag,
2485 not the regular one. The magic gets handled in close. */
2486 fsp->initial_delete_on_close = True;
2487 }
2488
2489 if (new_file_created) {
2490 /* Files should be initially set as archive */
2491 if (lp_map_archive(SNUM(conn)) ||
2492 lp_store_dos_attributes(SNUM(conn))) {
2493 if (!posix_open) {
2494 if (file_set_dosmode(conn, smb_fname,
2495 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2496 parent_dir, true) == 0) {
2497 unx_mode = smb_fname->st.st_ex_mode;
2498 }
2499 }
2500 }
2501 }
2502
2503 /* Determine sparse flag. */
2504 if (posix_open) {
2505 /* POSIX opens are sparse by default. */
2506 fsp->is_sparse = true;
2507 } else {
2508 fsp->is_sparse = (file_existed &&
2509 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2510 }
2511
2512 /*
2513 * Take care of inherited ACLs on created files - if default ACL not
2514 * selected.
2515 */
2516
2517 if (!posix_open && !file_existed && !def_acl) {
2518
2519 int saved_errno = errno; /* We might get ENOSYS in the next
2520 * call.. */
2521
2522 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2523 errno == ENOSYS) {
2524 errno = saved_errno; /* Ignore ENOSYS */
2525 }
2526
2527 } else if (new_unx_mode) {
2528
2529 int ret = -1;
2530
2531 /* Attributes need changing. File already existed. */
2532
2533 {
2534 int saved_errno = errno; /* We might get ENOSYS in the
2535 * next call.. */
2536 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2537
2538 if (ret == -1 && errno == ENOSYS) {
2539 errno = saved_errno; /* Ignore ENOSYS */
2540 } else {
2541 DEBUG(5, ("open_file_ntcreate: reset "
2542 "attributes of file %s to 0%o\n",
2543 smb_fname_str_dbg(smb_fname),
2544 (unsigned int)new_unx_mode));
2545 ret = 0; /* Don't do the fchmod below. */
2546 }
2547 }
2548
2549 if ((ret == -1) &&
2550 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2551 DEBUG(5, ("open_file_ntcreate: failed to reset "
2552 "attributes of file %s to 0%o\n",
2553 smb_fname_str_dbg(smb_fname),
2554 (unsigned int)new_unx_mode));
2555 }
2556
2557 /* If this is a successful open, we must remove any deferred open
2558 * records. */
2559 if (req != NULL) {
2560 del_deferred_open_entry(lck, req->mid,
2561 sconn_server_id(req->sconn));
2562 }
2563 TALLOC_FREE(lck);
2564
2565 return NT_STATUS_OK;
2566}
2567
2568
2569/****************************************************************************
2570 Open a file for for write to ensure that we can fchmod it.
2571****************************************************************************/
2572
2573NTSTATUS open_file_fchmod(connection_struct *conn,
2574 struct smb_filename *smb_fname,
2575 files_struct **result)
2576{
2577 if (!VALID_STAT(smb_fname->st)) {
2578 return NT_STATUS_INVALID_PARAMETER;
2579 }
2580
2581 return SMB_VFS_CREATE_FILE(
2582 conn, /* conn */
2583 NULL, /* req */
2584 0, /* root_dir_fid */
2585 smb_fname, /* fname */
2586 FILE_WRITE_DATA, /* access_mask */
2587 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2588 FILE_SHARE_DELETE),
2589 FILE_OPEN, /* create_disposition*/
2590 0, /* create_options */
2591 0, /* file_attributes */
2592 INTERNAL_OPEN_ONLY, /* oplock_request */
2593 0, /* allocation_size */
2594 0, /* private_flags */
2595 NULL, /* sd */
2596 NULL, /* ea_list */
2597 result, /* result */
2598 NULL); /* pinfo */
2599}
2600
2601static NTSTATUS mkdir_internal(connection_struct *conn,
2602 struct smb_filename *smb_dname,
2603 uint32 file_attributes)
2604{
2605 mode_t mode;
2606 char *parent_dir;
2607 NTSTATUS status;
2608 bool posix_open = false;
2609 bool need_re_stat = false;
2610
2611 if(!CAN_WRITE(conn)) {
2612 DEBUG(5,("mkdir_internal: failing create on read-only share "
2613 "%s\n", lp_servicename(SNUM(conn))));
2614 return NT_STATUS_ACCESS_DENIED;
2615 }
2616
2617 status = check_name(conn, smb_dname->base_name);
2618 if (!NT_STATUS_IS_OK(status)) {
2619 return status;
2620 }
2621
2622 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2623 NULL)) {
2624 return NT_STATUS_NO_MEMORY;
2625 }
2626
2627 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2628 posix_open = true;
2629 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2630 } else {
2631 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2632 }
2633
2634 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2635 return map_nt_error_from_unix(errno);
2636 }
2637
2638 /* Ensure we're checking for a symlink here.... */
2639 /* We don't want to get caught by a symlink racer. */
2640
2641 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2642 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2643 smb_fname_str_dbg(smb_dname), strerror(errno)));
2644 return map_nt_error_from_unix(errno);
2645 }
2646
2647 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2648 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2649 smb_fname_str_dbg(smb_dname)));
2650 return NT_STATUS_ACCESS_DENIED;
2651 }
2652
2653 if (lp_store_dos_attributes(SNUM(conn))) {
2654 if (!posix_open) {
2655 file_set_dosmode(conn, smb_dname,
2656 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2657 parent_dir, true);
2658 }
2659 }
2660
2661 if (lp_inherit_perms(SNUM(conn))) {
2662 inherit_access_posix_acl(conn, parent_dir,
2663 smb_dname->base_name, mode);
2664 need_re_stat = true;
2665 }
2666
2667 if (!posix_open) {
2668 /*
2669 * Check if high bits should have been set,
2670 * then (if bits are missing): add them.
2671 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2672 * dir.
2673 */
2674 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2675 (mode & ~smb_dname->st.st_ex_mode)) {
2676 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2677 (smb_dname->st.st_ex_mode |
2678 (mode & ~smb_dname->st.st_ex_mode)));
2679 need_re_stat = true;
2680 }
2681 }
2682
2683 /* Change the owner if required. */
2684 if (lp_inherit_owner(SNUM(conn))) {
2685 change_dir_owner_to_parent(conn, parent_dir,
2686 smb_dname->base_name,
2687 &smb_dname->st);
2688 need_re_stat = true;
2689 }
2690
2691 if (need_re_stat) {
2692 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2693 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2694 smb_fname_str_dbg(smb_dname), strerror(errno)));
2695 return map_nt_error_from_unix(errno);
2696 }
2697 }
2698
2699 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2700 smb_dname->base_name);
2701
2702 return NT_STATUS_OK;
2703}
2704
2705/****************************************************************************
2706 Ensure we didn't get symlink raced on opening a directory.
2707****************************************************************************/
2708
2709bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2710 const SMB_STRUCT_STAT *sbuf2)
2711{
2712 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2713 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2714 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2715 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2716 return false;
2717 }
2718 return true;
2719}
2720
2721/****************************************************************************
2722 Open a directory from an NT SMB call.
2723****************************************************************************/
2724
2725static NTSTATUS open_directory(connection_struct *conn,
2726 struct smb_request *req,
2727 struct smb_filename *smb_dname,
2728 uint32 access_mask,
2729 uint32 share_access,
2730 uint32 create_disposition,
2731 uint32 create_options,
2732 uint32 file_attributes,
2733 int *pinfo,
2734 files_struct **result)
2735{
2736 files_struct *fsp = NULL;
2737 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2738 struct share_mode_lock *lck = NULL;
2739 NTSTATUS status;
2740 struct timespec mtimespec;
2741 int info = 0;
2742
2743 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
2744
2745 /* Ensure we have a directory attribute. */
2746 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2747
2748 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2749 "share_access = 0x%x create_options = 0x%x, "
2750 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2751 smb_fname_str_dbg(smb_dname),
2752 (unsigned int)access_mask,
2753 (unsigned int)share_access,
2754 (unsigned int)create_options,
2755 (unsigned int)create_disposition,
2756 (unsigned int)file_attributes));
2757
2758 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2759 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2760 is_ntfs_stream_smb_fname(smb_dname)) {
2761 DEBUG(2, ("open_directory: %s is a stream name!\n",
2762 smb_fname_str_dbg(smb_dname)));
2763 return NT_STATUS_NOT_A_DIRECTORY;
2764 }
2765
2766 status = smbd_calculate_access_mask(conn, smb_dname, dir_existed,
2767 access_mask, &access_mask);
2768 if (!NT_STATUS_IS_OK(status)) {
2769 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2770 "on file %s returned %s\n",
2771 smb_fname_str_dbg(smb_dname),
2772 nt_errstr(status)));
2773 return status;
2774 }
2775
2776 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2777 !security_token_has_privilege(get_current_nttok(conn),
2778 SEC_PRIV_SECURITY)) {
2779 DEBUG(10, ("open_directory: open on %s "
2780 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2781 smb_fname_str_dbg(smb_dname)));
2782 return NT_STATUS_PRIVILEGE_NOT_HELD;
2783 }
2784
2785 switch( create_disposition ) {
2786 case FILE_OPEN:
2787
2788 if (!dir_existed) {
2789 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2790 }
2791
2792 info = FILE_WAS_OPENED;
2793 break;
2794
2795 case FILE_CREATE:
2796
2797 /* If directory exists error. If directory doesn't
2798 * exist create. */
2799
2800 status = mkdir_internal(conn, smb_dname,
2801 file_attributes);
2802
2803 if (!NT_STATUS_IS_OK(status)) {
2804 DEBUG(2, ("open_directory: unable to create "
2805 "%s. Error was %s\n",
2806 smb_fname_str_dbg(smb_dname),
2807 nt_errstr(status)));
2808 return status;
2809 }
2810
2811 info = FILE_WAS_CREATED;
2812 break;
2813
2814 case FILE_OPEN_IF:
2815 /*
2816 * If directory exists open. If directory doesn't
2817 * exist create.
2818 */
2819
2820 status = mkdir_internal(conn, smb_dname,
2821 file_attributes);
2822
2823 if (NT_STATUS_IS_OK(status)) {
2824 info = FILE_WAS_CREATED;
2825 }
2826
2827 if (NT_STATUS_EQUAL(status,
2828 NT_STATUS_OBJECT_NAME_COLLISION)) {
2829 info = FILE_WAS_OPENED;
2830 status = NT_STATUS_OK;
2831 }
2832 break;
2833
2834 case FILE_SUPERSEDE:
2835 case FILE_OVERWRITE:
2836 case FILE_OVERWRITE_IF:
2837 default:
2838 DEBUG(5,("open_directory: invalid create_disposition "
2839 "0x%x for directory %s\n",
2840 (unsigned int)create_disposition,
2841 smb_fname_str_dbg(smb_dname)));
2842 return NT_STATUS_INVALID_PARAMETER;
2843 }
2844
2845 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2846 DEBUG(5,("open_directory: %s is not a directory !\n",
2847 smb_fname_str_dbg(smb_dname)));
2848 return NT_STATUS_NOT_A_DIRECTORY;
2849 }
2850
2851 if (info == FILE_WAS_OPENED) {
2852 uint32_t access_granted = 0;
2853 status = smbd_check_open_rights(conn, smb_dname, access_mask,
2854 &access_granted);
2855
2856 /* Were we trying to do a directory open
2857 * for delete and didn't get DELETE
2858 * access (only) ? Check if the
2859 * directory allows DELETE_CHILD.
2860 * See here:
2861 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
2862 * for details. */
2863
2864 if ((NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
2865 (access_mask & DELETE_ACCESS) &&
2866 (access_granted == DELETE_ACCESS) &&
2867 can_delete_file_in_directory(conn, smb_dname))) {
2868 DEBUG(10,("open_directory: overrode ACCESS_DENIED "
2869 "on directory %s\n",
2870 smb_fname_str_dbg(smb_dname)));
2871 status = NT_STATUS_OK;
2872 }
2873
2874 if (!NT_STATUS_IS_OK(status)) {
2875 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2876 "file %s failed with %s\n",
2877 smb_fname_str_dbg(smb_dname),
2878 nt_errstr(status)));
2879 return status;
2880 }
2881 }
2882
2883 status = file_new(req, conn, &fsp);
2884 if(!NT_STATUS_IS_OK(status)) {
2885 return status;
2886 }
2887
2888 /*
2889 * Setup the files_struct for it.
2890 */
2891
2892 fsp->mode = smb_dname->st.st_ex_mode;
2893 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2894 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2895 fsp->file_pid = req ? req->smbpid : 0;
2896 fsp->can_lock = False;
2897 fsp->can_read = False;
2898 fsp->can_write = False;
2899
2900 fsp->share_access = share_access;
2901 fsp->fh->private_options = 0;
2902 /*
2903 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2904 */
2905 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2906 fsp->print_file = NULL;
2907 fsp->modified = False;
2908 fsp->oplock_type = NO_OPLOCK;
2909 fsp->sent_oplock_break = NO_BREAK_SENT;
2910 fsp->is_directory = True;
2911 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2912 status = fsp_set_smb_fname(fsp, smb_dname);
2913 if (!NT_STATUS_IS_OK(status)) {
2914 file_free(req, fsp);
2915 return status;
2916 }
2917
2918 mtimespec = smb_dname->st.st_ex_mtime;
2919
2920#ifdef O_DIRECTORY
2921 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2922#else
2923 /* POSIX allows us to open a directory with O_RDONLY. */
2924 status = fd_open(conn, fsp, O_RDONLY, 0);
2925#endif
2926 if (!NT_STATUS_IS_OK(status)) {
2927 DEBUG(5, ("open_directory: Could not open fd for "
2928 "%s (%s)\n",
2929 smb_fname_str_dbg(smb_dname),
2930 nt_errstr(status)));
2931 file_free(req, fsp);
2932 return status;
2933 }
2934
2935 status = vfs_stat_fsp(fsp);
2936 if (!NT_STATUS_IS_OK(status)) {
2937 fd_close(fsp);
2938 file_free(req, fsp);
2939 return status;
2940 }
2941
2942 /* Ensure there was no race condition. */
2943 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2944 DEBUG(5,("open_directory: stat struct differs for "
2945 "directory %s.\n",
2946 smb_fname_str_dbg(smb_dname)));
2947 fd_close(fsp);
2948 file_free(req, fsp);
2949 return NT_STATUS_ACCESS_DENIED;
2950 }
2951
2952 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2953 conn->connectpath, smb_dname, &mtimespec);
2954
2955 if (lck == NULL) {
2956 DEBUG(0, ("open_directory: Could not get share mode lock for "
2957 "%s\n", smb_fname_str_dbg(smb_dname)));
2958 fd_close(fsp);
2959 file_free(req, fsp);
2960 return NT_STATUS_SHARING_VIOLATION;
2961 }
2962
2963 status = open_mode_check(conn, lck, fsp->name_hash,
2964 access_mask, share_access,
2965 create_options, &dir_existed);
2966
2967 if (!NT_STATUS_IS_OK(status)) {
2968 TALLOC_FREE(lck);
2969 fd_close(fsp);
2970 file_free(req, fsp);
2971 return status;
2972 }
2973
2974 set_share_mode(lck, fsp, get_current_uid(conn),
2975 req ? req->mid : 0, NO_OPLOCK);
2976
2977 /* For directories the delete on close bit at open time seems
2978 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2979 if (create_options & FILE_DELETE_ON_CLOSE) {
2980 status = can_set_delete_on_close(fsp, 0);
2981 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2982 TALLOC_FREE(lck);
2983 fd_close(fsp);
2984 file_free(req, fsp);
2985 return status;
2986 }
2987
2988 if (NT_STATUS_IS_OK(status)) {
2989 /* Note that here we set the *inital* delete on close flag,
2990 not the regular one. The magic gets handled in close. */
2991 fsp->initial_delete_on_close = True;
2992 }
2993 }
2994
2995 TALLOC_FREE(lck);
2996
2997 if (pinfo) {
2998 *pinfo = info;
2999 }
3000
3001 *result = fsp;
3002 return NT_STATUS_OK;
3003}
3004
3005NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3006 struct smb_filename *smb_dname)
3007{
3008 NTSTATUS status;
3009 files_struct *fsp;
3010
3011 status = SMB_VFS_CREATE_FILE(
3012 conn, /* conn */
3013 req, /* req */
3014 0, /* root_dir_fid */
3015 smb_dname, /* fname */
3016 FILE_READ_ATTRIBUTES, /* access_mask */
3017 FILE_SHARE_NONE, /* share_access */
3018 FILE_CREATE, /* create_disposition*/
3019 FILE_DIRECTORY_FILE, /* create_options */
3020 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3021 0, /* oplock_request */
3022 0, /* allocation_size */
3023 0, /* private_flags */
3024 NULL, /* sd */
3025 NULL, /* ea_list */
3026 &fsp, /* result */
3027 NULL); /* pinfo */
3028
3029 if (NT_STATUS_IS_OK(status)) {
3030 close_file(req, fsp, NORMAL_CLOSE);
3031 }
3032
3033 return status;
3034}
3035
3036/****************************************************************************
3037 Receive notification that one of our open files has been renamed by another
3038 smbd process.
3039****************************************************************************/
3040
3041void msg_file_was_renamed(struct messaging_context *msg,
3042 void *private_data,
3043 uint32_t msg_type,
3044 struct server_id server_id,
3045 DATA_BLOB *data)
3046{
3047 struct smbd_server_connection *sconn;
3048 files_struct *fsp;
3049 char *frm = (char *)data->data;
3050 struct file_id id;
3051 const char *sharepath;
3052 const char *base_name;
3053 const char *stream_name;
3054 struct smb_filename *smb_fname = NULL;
3055 size_t sp_len, bn_len;
3056 NTSTATUS status;
3057
3058 sconn = msg_ctx_to_sconn(msg);
3059 if (sconn == NULL) {
3060 DEBUG(1, ("could not find sconn\n"));
3061 return;
3062 }
3063
3064 if (data->data == NULL
3065 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3066 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3067 (int)data->length));
3068 return;
3069 }
3070
3071 /* Unpack the message. */
3072 pull_file_id_24(frm, &id);
3073 sharepath = &frm[24];
3074 sp_len = strlen(sharepath);
3075 base_name = sharepath + sp_len + 1;
3076 bn_len = strlen(base_name);
3077 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3078
3079 /* stream_name must always be NULL if there is no stream. */
3080 if (stream_name[0] == '\0') {
3081 stream_name = NULL;
3082 }
3083
3084 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3085 stream_name, NULL, &smb_fname);
3086 if (!NT_STATUS_IS_OK(status)) {
3087 return;
3088 }
3089
3090 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3091 "file_id %s\n",
3092 sharepath, smb_fname_str_dbg(smb_fname),
3093 file_id_string_tos(&id)));
3094
3095 for(fsp = file_find_di_first(sconn, id); fsp;
3096 fsp = file_find_di_next(fsp)) {
3097 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3098
3099 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3100 fsp->fnum, fsp_str_dbg(fsp),
3101 smb_fname_str_dbg(smb_fname)));
3102 status = fsp_set_smb_fname(fsp, smb_fname);
3103 if (!NT_STATUS_IS_OK(status)) {
3104 goto out;
3105 }
3106 } else {
3107 /* TODO. JRA. */
3108 /* Now we have the complete path we can work out if this is
3109 actually within this share and adjust newname accordingly. */
3110 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3111 "not sharepath %s) "
3112 "fnum %d from %s -> %s\n",
3113 fsp->conn->connectpath,
3114 sharepath,
3115 fsp->fnum,
3116 fsp_str_dbg(fsp),
3117 smb_fname_str_dbg(smb_fname)));
3118 }
3119 }
3120 out:
3121 TALLOC_FREE(smb_fname);
3122 return;
3123}
3124
3125/*
3126 * If a main file is opened for delete, all streams need to be checked for
3127 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3128 * If that works, delete them all by setting the delete on close and close.
3129 */
3130
3131NTSTATUS open_streams_for_delete(connection_struct *conn,
3132 const char *fname)
3133{
3134 struct stream_struct *stream_info;
3135 files_struct **streams;
3136 int i;
3137 unsigned int num_streams;
3138 TALLOC_CTX *frame = talloc_stackframe();
3139 NTSTATUS status;
3140
3141 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
3142 &num_streams, &stream_info);
3143
3144 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3145 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3146 DEBUG(10, ("no streams around\n"));
3147 TALLOC_FREE(frame);
3148 return NT_STATUS_OK;
3149 }
3150
3151 if (!NT_STATUS_IS_OK(status)) {
3152 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
3153 nt_errstr(status)));
3154 goto fail;
3155 }
3156
3157 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3158 num_streams));
3159
3160 if (num_streams == 0) {
3161 TALLOC_FREE(frame);
3162 return NT_STATUS_OK;
3163 }
3164
3165 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
3166 if (streams == NULL) {
3167 DEBUG(0, ("talloc failed\n"));
3168 status = NT_STATUS_NO_MEMORY;
3169 goto fail;
3170 }
3171
3172 for (i=0; i<num_streams; i++) {
3173 struct smb_filename *smb_fname = NULL;
3174
3175 if (strequal(stream_info[i].name, "::$DATA")) {
3176 streams[i] = NULL;
3177 continue;
3178 }
3179
3180 status = create_synthetic_smb_fname(talloc_tos(), fname,
3181 stream_info[i].name,
3182 NULL, &smb_fname);
3183 if (!NT_STATUS_IS_OK(status)) {
3184 goto fail;
3185 }
3186
3187 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3188 DEBUG(10, ("Unable to stat stream: %s\n",
3189 smb_fname_str_dbg(smb_fname)));
3190 }
3191
3192 status = SMB_VFS_CREATE_FILE(
3193 conn, /* conn */
3194 NULL, /* req */
3195 0, /* root_dir_fid */
3196 smb_fname, /* fname */
3197 DELETE_ACCESS, /* access_mask */
3198 (FILE_SHARE_READ | /* share_access */
3199 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3200 FILE_OPEN, /* create_disposition*/
3201 0, /* create_options */
3202 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3203 0, /* oplock_request */
3204 0, /* allocation_size */
3205 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3206 NULL, /* sd */
3207 NULL, /* ea_list */
3208 &streams[i], /* result */
3209 NULL); /* pinfo */
3210
3211 if (!NT_STATUS_IS_OK(status)) {
3212 DEBUG(10, ("Could not open stream %s: %s\n",
3213 smb_fname_str_dbg(smb_fname),
3214 nt_errstr(status)));
3215
3216 TALLOC_FREE(smb_fname);
3217 break;
3218 }
3219 TALLOC_FREE(smb_fname);
3220 }
3221
3222 /*
3223 * don't touch the variable "status" beyond this point :-)
3224 */
3225
3226 for (i -= 1 ; i >= 0; i--) {
3227 if (streams[i] == NULL) {
3228 continue;
3229 }
3230
3231 DEBUG(10, ("Closing stream # %d, %s\n", i,
3232 fsp_str_dbg(streams[i])));
3233 close_file(NULL, streams[i], NORMAL_CLOSE);
3234 }
3235
3236 fail:
3237 TALLOC_FREE(frame);
3238 return status;
3239}
3240
3241/*
3242 * Wrapper around open_file_ntcreate and open_directory
3243 */
3244
3245static NTSTATUS create_file_unixpath(connection_struct *conn,
3246 struct smb_request *req,
3247 struct smb_filename *smb_fname,
3248 uint32_t access_mask,
3249 uint32_t share_access,
3250 uint32_t create_disposition,
3251 uint32_t create_options,
3252 uint32_t file_attributes,
3253 uint32_t oplock_request,
3254 uint64_t allocation_size,
3255 uint32_t private_flags,
3256 struct security_descriptor *sd,
3257 struct ea_list *ea_list,
3258
3259 files_struct **result,
3260 int *pinfo)
3261{
3262 int info = FILE_WAS_OPENED;
3263 files_struct *base_fsp = NULL;
3264 files_struct *fsp = NULL;
3265 NTSTATUS status;
3266
3267 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3268 "file_attributes = 0x%x, share_access = 0x%x, "
3269 "create_disposition = 0x%x create_options = 0x%x "
3270 "oplock_request = 0x%x private_flags = 0x%x "
3271 "ea_list = 0x%p, sd = 0x%p, "
3272 "fname = %s\n",
3273 (unsigned int)access_mask,
3274 (unsigned int)file_attributes,
3275 (unsigned int)share_access,
3276 (unsigned int)create_disposition,
3277 (unsigned int)create_options,
3278 (unsigned int)oplock_request,
3279 (unsigned int)private_flags,
3280 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3281
3282 if (create_options & FILE_OPEN_BY_FILE_ID) {
3283 status = NT_STATUS_NOT_SUPPORTED;
3284 goto fail;
3285 }
3286
3287 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3288 status = NT_STATUS_INVALID_PARAMETER;
3289 goto fail;
3290 }
3291
3292 if (req == NULL) {
3293 oplock_request |= INTERNAL_OPEN_ONLY;
3294 }
3295
3296 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3297 && (access_mask & DELETE_ACCESS)
3298 && !is_ntfs_stream_smb_fname(smb_fname)) {
3299 /*
3300 * We can't open a file with DELETE access if any of the
3301 * streams is open without FILE_SHARE_DELETE
3302 */
3303 status = open_streams_for_delete(conn, smb_fname->base_name);
3304
3305 if (!NT_STATUS_IS_OK(status)) {
3306 goto fail;
3307 }
3308 }
3309
3310 /* This is the correct thing to do (check every time) but can_delete
3311 * is expensive (it may have to read the parent directory
3312 * permissions). So for now we're not doing it unless we have a strong
3313 * hint the client is really going to delete this file. If the client
3314 * is forcing FILE_CREATE let the filesystem take care of the
3315 * permissions. */
3316
3317 /* Setting FILE_SHARE_DELETE is the hint. */
3318
3319 if ((create_disposition != FILE_CREATE)
3320 && (access_mask & DELETE_ACCESS)
3321 && (!(can_delete_file_in_directory(conn, smb_fname) ||
3322 can_access_file_acl(conn, smb_fname, DELETE_ACCESS)))) {
3323 status = NT_STATUS_ACCESS_DENIED;
3324 DEBUG(10,("create_file_unixpath: open file %s "
3325 "for delete ACCESS_DENIED\n",
3326 smb_fname_str_dbg(smb_fname)));
3327 goto fail;
3328 }
3329
3330 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3331 !security_token_has_privilege(get_current_nttok(conn),
3332 SEC_PRIV_SECURITY)) {
3333 DEBUG(10, ("create_file_unixpath: open on %s "
3334 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3335 smb_fname_str_dbg(smb_fname)));
3336 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3337 goto fail;
3338 }
3339
3340 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3341 && is_ntfs_stream_smb_fname(smb_fname)
3342 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3343 uint32 base_create_disposition;
3344 struct smb_filename *smb_fname_base = NULL;
3345
3346 if (create_options & FILE_DIRECTORY_FILE) {
3347 status = NT_STATUS_NOT_A_DIRECTORY;
3348 goto fail;
3349 }
3350
3351 switch (create_disposition) {
3352 case FILE_OPEN:
3353 base_create_disposition = FILE_OPEN;
3354 break;
3355 default:
3356 base_create_disposition = FILE_OPEN_IF;
3357 break;
3358 }
3359
3360 /* Create an smb_filename with stream_name == NULL. */
3361 status = create_synthetic_smb_fname(talloc_tos(),
3362 smb_fname->base_name,
3363 NULL, NULL,
3364 &smb_fname_base);
3365 if (!NT_STATUS_IS_OK(status)) {
3366 goto fail;
3367 }
3368
3369 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3370 DEBUG(10, ("Unable to stat stream: %s\n",
3371 smb_fname_str_dbg(smb_fname_base)));
3372 }
3373
3374 /* Open the base file. */
3375 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3376 FILE_SHARE_READ
3377 | FILE_SHARE_WRITE
3378 | FILE_SHARE_DELETE,
3379 base_create_disposition,
3380 0, 0, 0, 0, 0, NULL, NULL,
3381 &base_fsp, NULL);
3382 TALLOC_FREE(smb_fname_base);
3383
3384 if (!NT_STATUS_IS_OK(status)) {
3385 DEBUG(10, ("create_file_unixpath for base %s failed: "
3386 "%s\n", smb_fname->base_name,
3387 nt_errstr(status)));
3388 goto fail;
3389 }
3390 /* we don't need to low level fd */
3391 fd_close(base_fsp);
3392 }
3393
3394 /*
3395 * If it's a request for a directory open, deal with it separately.
3396 */
3397
3398 if (create_options & FILE_DIRECTORY_FILE) {
3399
3400 if (create_options & FILE_NON_DIRECTORY_FILE) {
3401 status = NT_STATUS_INVALID_PARAMETER;
3402 goto fail;
3403 }
3404
3405 /* Can't open a temp directory. IFS kit test. */
3406 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3407 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3408 status = NT_STATUS_INVALID_PARAMETER;
3409 goto fail;
3410 }
3411
3412 /*
3413 * We will get a create directory here if the Win32
3414 * app specified a security descriptor in the
3415 * CreateDirectory() call.
3416 */
3417
3418 oplock_request = 0;
3419 status = open_directory(
3420 conn, req, smb_fname, access_mask, share_access,
3421 create_disposition, create_options, file_attributes,
3422 &info, &fsp);
3423 } else {
3424
3425 /*
3426 * Ordinary file case.
3427 */
3428
3429 status = file_new(req, conn, &fsp);
3430 if(!NT_STATUS_IS_OK(status)) {
3431 goto fail;
3432 }
3433
3434 status = fsp_set_smb_fname(fsp, smb_fname);
3435 if (!NT_STATUS_IS_OK(status)) {
3436 goto fail;
3437 }
3438
3439 /*
3440 * We're opening the stream element of a base_fsp
3441 * we already opened. Set up the base_fsp pointer.
3442 */
3443 if (base_fsp) {
3444 fsp->base_fsp = base_fsp;
3445 }
3446
3447 status = open_file_ntcreate(conn,
3448 req,
3449 access_mask,
3450 share_access,
3451 create_disposition,
3452 create_options,
3453 file_attributes,
3454 oplock_request,
3455 private_flags,
3456 &info,
3457 fsp);
3458
3459 if(!NT_STATUS_IS_OK(status)) {
3460 file_free(req, fsp);
3461 fsp = NULL;
3462 }
3463
3464 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3465
3466 /* A stream open never opens a directory */
3467
3468 if (base_fsp) {
3469 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3470 goto fail;
3471 }
3472
3473 /*
3474 * Fail the open if it was explicitly a non-directory
3475 * file.
3476 */
3477
3478 if (create_options & FILE_NON_DIRECTORY_FILE) {
3479 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3480 goto fail;
3481 }
3482
3483 oplock_request = 0;
3484 status = open_directory(
3485 conn, req, smb_fname, access_mask,
3486 share_access, create_disposition,
3487 create_options, file_attributes,
3488 &info, &fsp);
3489 }
3490 }
3491
3492 if (!NT_STATUS_IS_OK(status)) {
3493 goto fail;
3494 }
3495
3496 fsp->base_fsp = base_fsp;
3497
3498 /*
3499 * According to the MS documentation, the only time the security
3500 * descriptor is applied to the opened file is iff we *created* the
3501 * file; an existing file stays the same.
3502 *
3503 * Also, it seems (from observation) that you can open the file with
3504 * any access mask but you can still write the sd. We need to override
3505 * the granted access before we call set_sd
3506 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3507 */
3508
3509 if ((sd != NULL) && (info == FILE_WAS_CREATED)
3510 && lp_nt_acl_support(SNUM(conn))) {
3511
3512 uint32_t sec_info_sent;
3513 uint32_t saved_access_mask = fsp->access_mask;
3514
3515 sec_info_sent = get_sec_info(sd);
3516
3517 fsp->access_mask = FILE_GENERIC_ALL;
3518
3519 /* Convert all the generic bits. */
3520 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3521 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3522
3523 if (sec_info_sent & (SECINFO_OWNER|
3524 SECINFO_GROUP|
3525 SECINFO_DACL|
3526 SECINFO_SACL)) {
3527 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3528 }
3529
3530 fsp->access_mask = saved_access_mask;
3531
3532 if (!NT_STATUS_IS_OK(status)) {
3533 goto fail;
3534 }
3535 }
3536
3537 if ((ea_list != NULL) &&
3538 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3539 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3540 if (!NT_STATUS_IS_OK(status)) {
3541 goto fail;
3542 }
3543 }
3544
3545 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3546 status = NT_STATUS_ACCESS_DENIED;
3547 goto fail;
3548 }
3549
3550 /* Save the requested allocation size. */
3551 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3552 if (allocation_size
3553 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3554 fsp->initial_allocation_size = smb_roundup(
3555 fsp->conn, allocation_size);
3556 if (fsp->is_directory) {
3557 /* Can't set allocation size on a directory. */
3558 status = NT_STATUS_ACCESS_DENIED;
3559 goto fail;
3560 }
3561 if (vfs_allocate_file_space(
3562 fsp, fsp->initial_allocation_size) == -1) {
3563 status = NT_STATUS_DISK_FULL;
3564 goto fail;
3565 }
3566 } else {
3567 fsp->initial_allocation_size = smb_roundup(
3568 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3569 }
3570 }
3571
3572 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3573
3574 *result = fsp;
3575 if (pinfo != NULL) {
3576 *pinfo = info;
3577 }
3578
3579 smb_fname->st = fsp->fsp_name->st;
3580
3581 return NT_STATUS_OK;
3582
3583 fail:
3584 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3585
3586 if (fsp != NULL) {
3587 if (base_fsp && fsp->base_fsp == base_fsp) {
3588 /*
3589 * The close_file below will close
3590 * fsp->base_fsp.
3591 */
3592 base_fsp = NULL;
3593 }
3594 close_file(req, fsp, ERROR_CLOSE);
3595 fsp = NULL;
3596 }
3597 if (base_fsp != NULL) {
3598 close_file(req, base_fsp, ERROR_CLOSE);
3599 base_fsp = NULL;
3600 }
3601 return status;
3602}
3603
3604/*
3605 * Calculate the full path name given a relative fid.
3606 */
3607NTSTATUS get_relative_fid_filename(connection_struct *conn,
3608 struct smb_request *req,
3609 uint16_t root_dir_fid,
3610 const struct smb_filename *smb_fname,
3611 struct smb_filename **smb_fname_out)
3612{
3613 files_struct *dir_fsp;
3614 char *parent_fname = NULL;
3615 char *new_base_name = NULL;
3616 NTSTATUS status;
3617
3618 if (root_dir_fid == 0 || !smb_fname) {
3619 status = NT_STATUS_INTERNAL_ERROR;
3620 goto out;
3621 }
3622
3623 dir_fsp = file_fsp(req, root_dir_fid);
3624
3625 if (dir_fsp == NULL) {
3626 status = NT_STATUS_INVALID_HANDLE;
3627 goto out;
3628 }
3629
3630 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3631 status = NT_STATUS_INVALID_HANDLE;
3632 goto out;
3633 }
3634
3635 if (!dir_fsp->is_directory) {
3636
3637 /*
3638 * Check to see if this is a mac fork of some kind.
3639 */
3640
3641 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3642 is_ntfs_stream_smb_fname(smb_fname)) {
3643 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3644 goto out;
3645 }
3646
3647 /*
3648 we need to handle the case when we get a
3649 relative open relative to a file and the
3650 pathname is blank - this is a reopen!
3651 (hint from demyn plantenberg)
3652 */
3653
3654 status = NT_STATUS_INVALID_HANDLE;
3655 goto out;
3656 }
3657
3658 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3659 /*
3660 * We're at the toplevel dir, the final file name
3661 * must not contain ./, as this is filtered out
3662 * normally by srvstr_get_path and unix_convert
3663 * explicitly rejects paths containing ./.
3664 */
3665 parent_fname = talloc_strdup(talloc_tos(), "");
3666 if (parent_fname == NULL) {
3667 status = NT_STATUS_NO_MEMORY;
3668 goto out;
3669 }
3670 } else {
3671 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3672
3673 /*
3674 * Copy in the base directory name.
3675 */
3676
3677 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3678 dir_name_len+2);
3679 if (parent_fname == NULL) {
3680 status = NT_STATUS_NO_MEMORY;
3681 goto out;
3682 }
3683 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3684 dir_name_len+1);
3685
3686 /*
3687 * Ensure it ends in a '/'.
3688 * We used TALLOC_SIZE +2 to add space for the '/'.
3689 */
3690
3691 if(dir_name_len
3692 && (parent_fname[dir_name_len-1] != '\\')
3693 && (parent_fname[dir_name_len-1] != '/')) {
3694 parent_fname[dir_name_len] = '/';
3695 parent_fname[dir_name_len+1] = '\0';
3696 }
3697 }
3698
3699 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3700 smb_fname->base_name);
3701 if (new_base_name == NULL) {
3702 status = NT_STATUS_NO_MEMORY;
3703 goto out;
3704 }
3705
3706 status = filename_convert(req,
3707 conn,
3708 req->flags2 & FLAGS2_DFS_PATHNAMES,
3709 new_base_name,
3710 0,
3711 NULL,
3712 smb_fname_out);
3713 if (!NT_STATUS_IS_OK(status)) {
3714 goto out;
3715 }
3716
3717 out:
3718 TALLOC_FREE(parent_fname);
3719 TALLOC_FREE(new_base_name);
3720 return status;
3721}
3722
3723NTSTATUS create_file_default(connection_struct *conn,
3724 struct smb_request *req,
3725 uint16_t root_dir_fid,
3726 struct smb_filename *smb_fname,
3727 uint32_t access_mask,
3728 uint32_t share_access,
3729 uint32_t create_disposition,
3730 uint32_t create_options,
3731 uint32_t file_attributes,
3732 uint32_t oplock_request,
3733 uint64_t allocation_size,
3734 uint32_t private_flags,
3735 struct security_descriptor *sd,
3736 struct ea_list *ea_list,
3737 files_struct **result,
3738 int *pinfo)
3739{
3740 int info = FILE_WAS_OPENED;
3741 files_struct *fsp = NULL;
3742 NTSTATUS status;
3743 bool stream_name = false;
3744
3745 DEBUG(10,("create_file: access_mask = 0x%x "
3746 "file_attributes = 0x%x, share_access = 0x%x, "
3747 "create_disposition = 0x%x create_options = 0x%x "
3748 "oplock_request = 0x%x "
3749 "private_flags = 0x%x "
3750 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3751 "fname = %s\n",
3752 (unsigned int)access_mask,
3753 (unsigned int)file_attributes,
3754 (unsigned int)share_access,
3755 (unsigned int)create_disposition,
3756 (unsigned int)create_options,
3757 (unsigned int)oplock_request,
3758 (unsigned int)private_flags,
3759 (unsigned int)root_dir_fid,
3760 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3761
3762 /*
3763 * Calculate the filename from the root_dir_if if necessary.
3764 */
3765
3766 if (root_dir_fid != 0) {
3767 struct smb_filename *smb_fname_out = NULL;
3768 status = get_relative_fid_filename(conn, req, root_dir_fid,
3769 smb_fname, &smb_fname_out);
3770 if (!NT_STATUS_IS_OK(status)) {
3771 goto fail;
3772 }
3773 smb_fname = smb_fname_out;
3774 }
3775
3776 /*
3777 * Check to see if this is a mac fork of some kind.
3778 */
3779
3780 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3781 if (stream_name) {
3782 enum FAKE_FILE_TYPE fake_file_type;
3783
3784 fake_file_type = is_fake_file(smb_fname);
3785
3786 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3787
3788 /*
3789 * Here we go! support for changing the disk quotas
3790 * --metze
3791 *
3792 * We need to fake up to open this MAGIC QUOTA file
3793 * and return a valid FID.
3794 *
3795 * w2k close this file directly after openening xp
3796 * also tries a QUERY_FILE_INFO on the file and then
3797 * close it
3798 */
3799 status = open_fake_file(req, conn, req->vuid,
3800 fake_file_type, smb_fname,
3801 access_mask, &fsp);
3802 if (!NT_STATUS_IS_OK(status)) {
3803 goto fail;
3804 }
3805
3806 ZERO_STRUCT(smb_fname->st);
3807 goto done;
3808 }
3809
3810 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3811 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3812 goto fail;
3813 }
3814 }
3815
3816 /* All file access must go through check_name() */
3817
3818 status = check_name(conn, smb_fname->base_name);
3819 if (!NT_STATUS_IS_OK(status)) {
3820 goto fail;
3821 }
3822
3823 if (stream_name && is_ntfs_default_stream_smb_fname(smb_fname)) {
3824 int ret;
3825 smb_fname->stream_name = NULL;
3826 /* We have to handle this error here. */
3827 if (create_options & FILE_DIRECTORY_FILE) {
3828 status = NT_STATUS_NOT_A_DIRECTORY;
3829 goto fail;
3830 }
3831 if (lp_posix_pathnames()) {
3832 ret = SMB_VFS_LSTAT(conn, smb_fname);
3833 } else {
3834 ret = SMB_VFS_STAT(conn, smb_fname);
3835 }
3836
3837 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3838 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3839 goto fail;
3840 }
3841 }
3842
3843 status = create_file_unixpath(
3844 conn, req, smb_fname, access_mask, share_access,
3845 create_disposition, create_options, file_attributes,
3846 oplock_request, allocation_size, private_flags,
3847 sd, ea_list,
3848 &fsp, &info);
3849
3850 if (!NT_STATUS_IS_OK(status)) {
3851 goto fail;
3852 }
3853
3854 done:
3855 DEBUG(10, ("create_file: info=%d\n", info));
3856
3857 *result = fsp;
3858 if (pinfo != NULL) {
3859 *pinfo = info;
3860 }
3861 return NT_STATUS_OK;
3862
3863 fail:
3864 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3865
3866 if (fsp != NULL) {
3867 close_file(req, fsp, ERROR_CLOSE);
3868 fsp = NULL;
3869 }
3870 return status;
3871}
Note: See TracBrowser for help on using the repository browser.