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

Last change on this file was 989, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.7

File size: 134.0 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 "../librpc/gen_ndr/open_files.h"
31#include "../librpc/gen_ndr/idmap.h"
32#include "../librpc/gen_ndr/ioctl.h"
33#include "passdb/lookup_sid.h"
34#include "auth.h"
35#include "serverid.h"
36#include "messages.h"
37#include "source3/lib/dbwrap/dbwrap_watch.h"
38#include "locking/leases_db.h"
39#include "librpc/gen_ndr/ndr_leases_db.h"
40
41extern const struct generic_mapping file_generic_mapping;
42
43struct deferred_open_record {
44 bool delayed_for_oplocks;
45 bool async_open;
46 struct file_id id;
47};
48
49/****************************************************************************
50 If the requester wanted DELETE_ACCESS and was rejected because
51 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
52 overrides this.
53****************************************************************************/
54
55static bool parent_override_delete(connection_struct *conn,
56 const struct smb_filename *smb_fname,
57 uint32_t access_mask,
58 uint32_t rejected_mask)
59{
60 if ((access_mask & DELETE_ACCESS) &&
61 (rejected_mask & DELETE_ACCESS) &&
62 can_delete_file_in_directory(conn, smb_fname)) {
63 return true;
64 }
65 return false;
66}
67
68/****************************************************************************
69 Check if we have open rights.
70****************************************************************************/
71
72NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
73 const struct smb_filename *smb_fname,
74 bool use_privs,
75 uint32_t access_mask)
76{
77 /* Check if we have rights to open. */
78 NTSTATUS status;
79 struct security_descriptor *sd = NULL;
80 uint32_t rejected_share_access;
81 uint32_t rejected_mask = access_mask;
82 uint32_t do_not_check_mask = 0;
83
84 rejected_share_access = access_mask & ~(conn->share_access);
85
86 if (rejected_share_access) {
87 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
88 "on %s (0x%x)\n",
89 (unsigned int)access_mask,
90 smb_fname_str_dbg(smb_fname),
91 (unsigned int)rejected_share_access ));
92 return NT_STATUS_ACCESS_DENIED;
93 }
94
95 if (!use_privs && get_current_uid(conn) == (uid_t)0) {
96 /* I'm sorry sir, I didn't know you were root... */
97 DEBUG(10,("smbd_check_access_rights: root override "
98 "on %s. Granting 0x%x\n",
99 smb_fname_str_dbg(smb_fname),
100 (unsigned int)access_mask ));
101 return NT_STATUS_OK;
102 }
103
104 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
105 DEBUG(10,("smbd_check_access_rights: not checking ACL "
106 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
107 smb_fname_str_dbg(smb_fname),
108 (unsigned int)access_mask ));
109 return NT_STATUS_OK;
110 }
111
112 if (access_mask == DELETE_ACCESS &&
113 VALID_STAT(smb_fname->st) &&
114 S_ISLNK(smb_fname->st.st_ex_mode)) {
115 /* We can always delete a symlink. */
116 DEBUG(10,("smbd_check_access_rights: not checking ACL "
117 "on DELETE_ACCESS on symlink %s.\n",
118 smb_fname_str_dbg(smb_fname) ));
119 return NT_STATUS_OK;
120 }
121
122 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
123 (SECINFO_OWNER |
124 SECINFO_GROUP |
125 SECINFO_DACL), talloc_tos(), &sd);
126
127 if (!NT_STATUS_IS_OK(status)) {
128 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
129 "on %s: %s\n",
130 smb_fname_str_dbg(smb_fname),
131 nt_errstr(status)));
132
133 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
134 goto access_denied;
135 }
136
137 return status;
138 }
139
140 /*
141 * If we can access the path to this file, by
142 * default we have FILE_READ_ATTRIBUTES from the
143 * containing directory. See the section:
144 * "Algorithm to Check Access to an Existing File"
145 * in MS-FSA.pdf.
146 *
147 * se_file_access_check() also takes care of
148 * owner WRITE_DAC and READ_CONTROL.
149 */
150 do_not_check_mask = FILE_READ_ATTRIBUTES;
151
152 /*
153 * Samba 3.6 and earlier granted execute access even
154 * if the ACL did not contain execute rights.
155 * Samba 4.0 is more correct and checks it.
156 * The compatibilty mode allows one to skip this check
157 * to smoothen upgrades.
158 */
159 if (lp_acl_allow_execute_always(SNUM(conn))) {
160 do_not_check_mask |= FILE_EXECUTE;
161 }
162
163 status = se_file_access_check(sd,
164 get_current_nttok(conn),
165 use_privs,
166 (access_mask & ~do_not_check_mask),
167 &rejected_mask);
168
169 DEBUG(10,("smbd_check_access_rights: file %s requesting "
170 "0x%x returning 0x%x (%s)\n",
171 smb_fname_str_dbg(smb_fname),
172 (unsigned int)access_mask,
173 (unsigned int)rejected_mask,
174 nt_errstr(status) ));
175
176 if (!NT_STATUS_IS_OK(status)) {
177 if (DEBUGLEVEL >= 10) {
178 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
179 smb_fname_str_dbg(smb_fname) ));
180 NDR_PRINT_DEBUG(security_descriptor, sd);
181 }
182 }
183
184 TALLOC_FREE(sd);
185
186 if (NT_STATUS_IS_OK(status) ||
187 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
188 return status;
189 }
190
191 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
192
193 access_denied:
194
195 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
196 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
197 !lp_store_dos_attributes(SNUM(conn)) &&
198 (lp_map_readonly(SNUM(conn)) ||
199 lp_map_archive(SNUM(conn)) ||
200 lp_map_hidden(SNUM(conn)) ||
201 lp_map_system(SNUM(conn)))) {
202 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
203
204 DEBUG(10,("smbd_check_access_rights: "
205 "overrode "
206 "FILE_WRITE_ATTRIBUTES "
207 "on file %s\n",
208 smb_fname_str_dbg(smb_fname)));
209 }
210
211 if (parent_override_delete(conn,
212 smb_fname,
213 access_mask,
214 rejected_mask)) {
215 /* Were we trying to do an open
216 * for delete and didn't get DELETE
217 * access (only) ? Check if the
218 * directory allows DELETE_CHILD.
219 * See here:
220 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
221 * for details. */
222
223 rejected_mask &= ~DELETE_ACCESS;
224
225 DEBUG(10,("smbd_check_access_rights: "
226 "overrode "
227 "DELETE_ACCESS on "
228 "file %s\n",
229 smb_fname_str_dbg(smb_fname)));
230 }
231
232 if (rejected_mask != 0) {
233 return NT_STATUS_ACCESS_DENIED;
234 }
235 return NT_STATUS_OK;
236}
237
238static NTSTATUS check_parent_access(struct connection_struct *conn,
239 struct smb_filename *smb_fname,
240 uint32_t access_mask)
241{
242 NTSTATUS status;
243 char *parent_dir = NULL;
244 struct security_descriptor *parent_sd = NULL;
245 uint32_t access_granted = 0;
246
247 if (!parent_dirname(talloc_tos(),
248 smb_fname->base_name,
249 &parent_dir,
250 NULL)) {
251 return NT_STATUS_NO_MEMORY;
252 }
253
254 if (get_current_uid(conn) == (uid_t)0) {
255 /* I'm sorry sir, I didn't know you were root... */
256 DEBUG(10,("check_parent_access: root override "
257 "on %s. Granting 0x%x\n",
258 smb_fname_str_dbg(smb_fname),
259 (unsigned int)access_mask ));
260 return NT_STATUS_OK;
261 }
262
263 status = SMB_VFS_GET_NT_ACL(conn,
264 parent_dir,
265 SECINFO_DACL,
266 talloc_tos(),
267 &parent_sd);
268
269 if (!NT_STATUS_IS_OK(status)) {
270 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
271 "%s with error %s\n",
272 parent_dir,
273 nt_errstr(status)));
274 return status;
275 }
276
277 /*
278 * If we can access the path to this file, by
279 * default we have FILE_READ_ATTRIBUTES from the
280 * containing directory. See the section:
281 * "Algorithm to Check Access to an Existing File"
282 * in MS-FSA.pdf.
283 *
284 * se_file_access_check() also takes care of
285 * owner WRITE_DAC and READ_CONTROL.
286 */
287 status = se_file_access_check(parent_sd,
288 get_current_nttok(conn),
289 false,
290 (access_mask & ~FILE_READ_ATTRIBUTES),
291 &access_granted);
292 if(!NT_STATUS_IS_OK(status)) {
293 DEBUG(5,("check_parent_access: access check "
294 "on directory %s for "
295 "path %s for mask 0x%x returned (0x%x) %s\n",
296 parent_dir,
297 smb_fname->base_name,
298 access_mask,
299 access_granted,
300 nt_errstr(status) ));
301 return status;
302 }
303
304 return NT_STATUS_OK;
305}
306
307/****************************************************************************
308 Ensure when opening a base file for a stream open that we have permissions
309 to do so given the access mask on the base file.
310****************************************************************************/
311
312static NTSTATUS check_base_file_access(struct connection_struct *conn,
313 struct smb_filename *smb_fname,
314 uint32_t access_mask)
315{
316 NTSTATUS status;
317
318 status = smbd_calculate_access_mask(conn, smb_fname,
319 false,
320 access_mask,
321 &access_mask);
322 if (!NT_STATUS_IS_OK(status)) {
323 DEBUG(10, ("smbd_calculate_access_mask "
324 "on file %s returned %s\n",
325 smb_fname_str_dbg(smb_fname),
326 nt_errstr(status)));
327 return status;
328 }
329
330 if (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) {
331 uint32_t dosattrs;
332 if (!CAN_WRITE(conn)) {
333 return NT_STATUS_ACCESS_DENIED;
334 }
335 dosattrs = dos_mode(conn, smb_fname);
336 if (IS_DOS_READONLY(dosattrs)) {
337 return NT_STATUS_ACCESS_DENIED;
338 }
339 }
340
341 return smbd_check_access_rights(conn,
342 smb_fname,
343 false,
344 access_mask);
345}
346
347/****************************************************************************
348 fd support routines - attempt to do a dos_open.
349****************************************************************************/
350
351NTSTATUS fd_open(struct connection_struct *conn,
352 files_struct *fsp,
353 int flags,
354 mode_t mode)
355{
356 struct smb_filename *smb_fname = fsp->fsp_name;
357 NTSTATUS status = NT_STATUS_OK;
358
359#ifdef O_NOFOLLOW
360 /*
361 * Never follow symlinks on a POSIX client. The
362 * client should be doing this.
363 */
364
365 if ((fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) || !lp_follow_symlinks(SNUM(conn))) {
366 flags |= O_NOFOLLOW;
367 }
368#endif
369
370 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
371 if (fsp->fh->fd == -1) {
372 int posix_errno = errno;
373#ifdef O_NOFOLLOW
374#if defined(ENOTSUP) && defined(OSF1)
375 /* handle special Tru64 errno */
376 if (errno == ENOTSUP) {
377 posix_errno = ELOOP;
378 }
379#endif /* ENOTSUP */
380#ifdef EFTYPE
381 /* fix broken NetBSD errno */
382 if (errno == EFTYPE) {
383 posix_errno = ELOOP;
384 }
385#endif /* EFTYPE */
386 /* fix broken FreeBSD errno */
387 if (errno == EMLINK) {
388 posix_errno = ELOOP;
389 }
390#endif /* O_NOFOLLOW */
391 status = map_nt_error_from_unix(posix_errno);
392 if (errno == EMFILE) {
393 static time_t last_warned = 0L;
394
395 if (time((time_t *) NULL) > last_warned) {
396 DEBUG(0,("Too many open files, unable "
397 "to open more! smbd's max "
398 "open files = %d\n",
399 lp_max_open_files()));
400 last_warned = time((time_t *) NULL);
401 }
402 }
403
404 }
405
406 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
407 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
408 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
409
410 return status;
411}
412
413/****************************************************************************
414 Close the file associated with a fsp.
415****************************************************************************/
416
417NTSTATUS fd_close(files_struct *fsp)
418{
419 int ret;
420
421 if (fsp->dptr) {
422 dptr_CloseDir(fsp);
423 }
424 if (fsp->fh->fd == -1) {
425 return NT_STATUS_OK; /* What we used to call a stat open. */
426 }
427 if (fsp->fh->ref_count > 1) {
428 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
429 }
430
431 ret = SMB_VFS_CLOSE(fsp);
432 fsp->fh->fd = -1;
433 if (ret == -1) {
434 return map_nt_error_from_unix(errno);
435 }
436 return NT_STATUS_OK;
437}
438
439/****************************************************************************
440 Change the ownership of a file to that of the parent directory.
441 Do this by fd if possible.
442****************************************************************************/
443
444void change_file_owner_to_parent(connection_struct *conn,
445 const char *inherit_from_dir,
446 files_struct *fsp)
447{
448 struct smb_filename *smb_fname_parent;
449 int ret;
450
451 smb_fname_parent = synthetic_smb_fname(talloc_tos(), inherit_from_dir,
452 NULL, NULL);
453 if (smb_fname_parent == NULL) {
454 return;
455 }
456
457 ret = SMB_VFS_STAT(conn, smb_fname_parent);
458 if (ret == -1) {
459 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
460 "directory %s. Error was %s\n",
461 smb_fname_str_dbg(smb_fname_parent),
462 strerror(errno)));
463 TALLOC_FREE(smb_fname_parent);
464 return;
465 }
466
467 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
468 /* Already this uid - no need to change. */
469 DEBUG(10,("change_file_owner_to_parent: file %s "
470 "is already owned by uid %d\n",
471 fsp_str_dbg(fsp),
472 (int)fsp->fsp_name->st.st_ex_uid ));
473 TALLOC_FREE(smb_fname_parent);
474 return;
475 }
476
477 become_root();
478 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
479 unbecome_root();
480 if (ret == -1) {
481 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
482 "file %s to parent directory uid %u. Error "
483 "was %s\n", fsp_str_dbg(fsp),
484 (unsigned int)smb_fname_parent->st.st_ex_uid,
485 strerror(errno) ));
486 } else {
487 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
488 "parent directory uid %u.\n", fsp_str_dbg(fsp),
489 (unsigned int)smb_fname_parent->st.st_ex_uid));
490 /* Ensure the uid entry is updated. */
491 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
492 }
493
494 TALLOC_FREE(smb_fname_parent);
495}
496
497NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
498 const char *inherit_from_dir,
499 const char *fname,
500 SMB_STRUCT_STAT *psbuf)
501{
502 struct smb_filename *smb_fname_parent;
503 struct smb_filename *smb_fname_cwd = NULL;
504 char *saved_dir = NULL;
505 TALLOC_CTX *ctx = talloc_tos();
506 NTSTATUS status = NT_STATUS_OK;
507 int ret;
508
509 smb_fname_parent = synthetic_smb_fname(ctx, inherit_from_dir,
510 NULL, NULL);
511 if (smb_fname_parent == NULL) {
512 return NT_STATUS_NO_MEMORY;
513 }
514
515 ret = SMB_VFS_STAT(conn, smb_fname_parent);
516 if (ret == -1) {
517 status = map_nt_error_from_unix(errno);
518 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
519 "directory %s. Error was %s\n",
520 smb_fname_str_dbg(smb_fname_parent),
521 strerror(errno)));
522 goto out;
523 }
524
525 /* We've already done an lstat into psbuf, and we know it's a
526 directory. If we can cd into the directory and the dev/ino
527 are the same then we can safely chown without races as
528 we're locking the directory in place by being in it. This
529 should work on any UNIX (thanks tridge :-). JRA.
530 */
531
532 saved_dir = vfs_GetWd(ctx,conn);
533 if (!saved_dir) {
534 status = map_nt_error_from_unix(errno);
535 DEBUG(0,("change_dir_owner_to_parent: failed to get "
536 "current working directory. Error was %s\n",
537 strerror(errno)));
538 goto out;
539 }
540
541 /* Chdir into the new path. */
542 if (vfs_ChDir(conn, fname) == -1) {
543 status = map_nt_error_from_unix(errno);
544 DEBUG(0,("change_dir_owner_to_parent: failed to change "
545 "current working directory to %s. Error "
546 "was %s\n", fname, strerror(errno) ));
547 goto chdir;
548 }
549
550 smb_fname_cwd = synthetic_smb_fname(ctx, ".", NULL, NULL);
551 if (smb_fname_cwd == NULL) {
552 status = NT_STATUS_NO_MEMORY;
553 goto chdir;
554 }
555
556 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
557 if (ret == -1) {
558 status = map_nt_error_from_unix(errno);
559 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
560 "directory '.' (%s) Error was %s\n",
561 fname, strerror(errno)));
562 goto chdir;
563 }
564
565 /* Ensure we're pointing at the same place. */
566 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
567 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
568 DEBUG(0,("change_dir_owner_to_parent: "
569 "device/inode on directory %s changed. "
570 "Refusing to chown !\n", fname ));
571 status = NT_STATUS_ACCESS_DENIED;
572 goto chdir;
573 }
574
575 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
576 /* Already this uid - no need to change. */
577 DEBUG(10,("change_dir_owner_to_parent: directory %s "
578 "is already owned by uid %d\n",
579 fname,
580 (int)smb_fname_cwd->st.st_ex_uid ));
581 status = NT_STATUS_OK;
582 goto chdir;
583 }
584
585 become_root();
586 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
587 (gid_t)-1);
588 unbecome_root();
589 if (ret == -1) {
590 status = map_nt_error_from_unix(errno);
591 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
592 "directory %s to parent directory uid %u. "
593 "Error was %s\n", fname,
594 (unsigned int)smb_fname_parent->st.st_ex_uid,
595 strerror(errno) ));
596 } else {
597 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
598 "directory %s to parent directory uid %u.\n",
599 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
600 /* Ensure the uid entry is updated. */
601 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
602 }
603
604 chdir:
605 vfs_ChDir(conn,saved_dir);
606 out:
607 TALLOC_FREE(smb_fname_parent);
608 TALLOC_FREE(smb_fname_cwd);
609 return status;
610}
611
612/****************************************************************************
613 Open a file - returning a guaranteed ATOMIC indication of if the
614 file was created or not.
615****************************************************************************/
616
617static NTSTATUS fd_open_atomic(struct connection_struct *conn,
618 files_struct *fsp,
619 int flags,
620 mode_t mode,
621 bool *file_created)
622{
623 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
624 bool file_existed = VALID_STAT(fsp->fsp_name->st);
625
626 *file_created = false;
627
628 if (!(flags & O_CREAT)) {
629 /*
630 * We're not creating the file, just pass through.
631 */
632 return fd_open(conn, fsp, flags, mode);
633 }
634
635 if (flags & O_EXCL) {
636 /*
637 * Fail if already exists, just pass through.
638 */
639 status = fd_open(conn, fsp, flags, mode);
640
641 /*
642 * Here we've opened with O_CREAT|O_EXCL. If that went
643 * NT_STATUS_OK, we *know* we created this file.
644 */
645 *file_created = NT_STATUS_IS_OK(status);
646
647 return status;
648 }
649
650 /*
651 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
652 * To know absolutely if we created the file or not,
653 * we can never call O_CREAT without O_EXCL. So if
654 * we think the file existed, try without O_CREAT|O_EXCL.
655 * If we think the file didn't exist, try with
656 * O_CREAT|O_EXCL. Keep bouncing between these two
657 * requests until either the file is created, or
658 * opened. Either way, we keep going until we get
659 * a returnable result (error, or open/create).
660 */
661
662 while(1) {
663 int curr_flags = flags;
664
665 if (file_existed) {
666 /* Just try open, do not create. */
667 curr_flags &= ~(O_CREAT);
668 status = fd_open(conn, fsp, curr_flags, mode);
669 if (NT_STATUS_EQUAL(status,
670 NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
671 /*
672 * Someone deleted it in the meantime.
673 * Retry with O_EXCL.
674 */
675 file_existed = false;
676 DEBUG(10,("fd_open_atomic: file %s existed. "
677 "Retry.\n",
678 smb_fname_str_dbg(fsp->fsp_name)));
679 continue;
680 }
681 } else {
682 /* Try create exclusively, fail if it exists. */
683 curr_flags |= O_EXCL;
684 status = fd_open(conn, fsp, curr_flags, mode);
685 if (NT_STATUS_EQUAL(status,
686 NT_STATUS_OBJECT_NAME_COLLISION)) {
687 /*
688 * Someone created it in the meantime.
689 * Retry without O_CREAT.
690 */
691 file_existed = true;
692 DEBUG(10,("fd_open_atomic: file %s "
693 "did not exist. Retry.\n",
694 smb_fname_str_dbg(fsp->fsp_name)));
695 continue;
696 }
697 if (NT_STATUS_IS_OK(status)) {
698 /*
699 * Here we've opened with O_CREAT|O_EXCL
700 * and got success. We *know* we created
701 * this file.
702 */
703 *file_created = true;
704 }
705 }
706 /* Create is done, or failed. */
707 break;
708 }
709 return status;
710}
711
712/****************************************************************************
713 Open a file.
714****************************************************************************/
715
716static NTSTATUS open_file(files_struct *fsp,
717 connection_struct *conn,
718 struct smb_request *req,
719 const char *parent_dir,
720 int flags,
721 mode_t unx_mode,
722 uint32_t access_mask, /* client requested access mask. */
723 uint32_t open_access_mask, /* what we're actually using in the open. */
724 bool *p_file_created)
725{
726 struct smb_filename *smb_fname = fsp->fsp_name;
727 NTSTATUS status = NT_STATUS_OK;
728 int accmode = (flags & O_ACCMODE);
729 int local_flags = flags;
730 bool file_existed = VALID_STAT(fsp->fsp_name->st);
731
732 fsp->fh->fd = -1;
733 errno = EPERM;
734
735 /* Check permissions */
736
737 /*
738 * This code was changed after seeing a client open request
739 * containing the open mode of (DENY_WRITE/read-only) with
740 * the 'create if not exist' bit set. The previous code
741 * would fail to open the file read only on a read-only share
742 * as it was checking the flags parameter directly against O_RDONLY,
743 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
744 * JRA.
745 */
746
747 if (!CAN_WRITE(conn)) {
748 /* It's a read-only share - fail if we wanted to write. */
749 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
750 DEBUG(3,("Permission denied opening %s\n",
751 smb_fname_str_dbg(smb_fname)));
752 return NT_STATUS_ACCESS_DENIED;
753 }
754 if (flags & O_CREAT) {
755 /* We don't want to write - but we must make sure that
756 O_CREAT doesn't create the file if we have write
757 access into the directory.
758 */
759 flags &= ~(O_CREAT|O_EXCL);
760 local_flags &= ~(O_CREAT|O_EXCL);
761 }
762 }
763
764 /*
765 * This little piece of insanity is inspired by the
766 * fact that an NT client can open a file for O_RDONLY,
767 * but set the create disposition to FILE_EXISTS_TRUNCATE.
768 * If the client *can* write to the file, then it expects to
769 * truncate the file, even though it is opening for readonly.
770 * Quicken uses this stupid trick in backup file creation...
771 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
772 * for helping track this one down. It didn't bite us in 2.0.x
773 * as we always opened files read-write in that release. JRA.
774 */
775
776 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
777 DEBUG(10,("open_file: truncate requested on read-only open "
778 "for file %s\n", smb_fname_str_dbg(smb_fname)));
779 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
780 }
781
782 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
783 (!file_existed && (local_flags & O_CREAT)) ||
784 ((local_flags & O_TRUNC) == O_TRUNC) ) {
785 const char *wild;
786 int ret;
787
788#if defined(O_NONBLOCK) && defined(S_ISFIFO)
789 /*
790 * We would block on opening a FIFO with no one else on the
791 * other end. Do what we used to do and add O_NONBLOCK to the
792 * open flags. JRA.
793 */
794
795 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
796 local_flags &= ~O_TRUNC; /* Can't truncate a FIFO. */
797 local_flags |= O_NONBLOCK;
798 }
799#endif
800
801 /* Don't create files with Microsoft wildcard characters. */
802 if (fsp->base_fsp) {
803 /*
804 * wildcard characters are allowed in stream names
805 * only test the basefilename
806 */
807 wild = fsp->base_fsp->fsp_name->base_name;
808 } else {
809 wild = smb_fname->base_name;
810 }
811 if ((local_flags & O_CREAT) && !file_existed &&
812 !(fsp->posix_flags & FSP_POSIX_FLAGS_PATHNAMES) &&
813 ms_has_wild(wild)) {
814 return NT_STATUS_OBJECT_NAME_INVALID;
815 }
816
817 /* Can we access this file ? */
818 if (!fsp->base_fsp) {
819 /* Only do this check on non-stream open. */
820 if (file_existed) {
821 status = smbd_check_access_rights(conn,
822 smb_fname,
823 false,
824 access_mask);
825
826 if (!NT_STATUS_IS_OK(status)) {
827 DEBUG(10, ("open_file: "
828 "smbd_check_access_rights "
829 "on file %s returned %s\n",
830 smb_fname_str_dbg(smb_fname),
831 nt_errstr(status)));
832 }
833
834 if (!NT_STATUS_IS_OK(status) &&
835 !NT_STATUS_EQUAL(status,
836 NT_STATUS_OBJECT_NAME_NOT_FOUND))
837 {
838 return status;
839 }
840
841 if (NT_STATUS_EQUAL(status,
842 NT_STATUS_OBJECT_NAME_NOT_FOUND))
843 {
844 DEBUG(10, ("open_file: "
845 "file %s vanished since we "
846 "checked for existence.\n",
847 smb_fname_str_dbg(smb_fname)));
848 file_existed = false;
849 SET_STAT_INVALID(fsp->fsp_name->st);
850 }
851 }
852
853 if (!file_existed) {
854 if (!(local_flags & O_CREAT)) {
855 /* File didn't exist and no O_CREAT. */
856 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
857 }
858
859 status = check_parent_access(conn,
860 smb_fname,
861 SEC_DIR_ADD_FILE);
862 if (!NT_STATUS_IS_OK(status)) {
863 DEBUG(10, ("open_file: "
864 "check_parent_access on "
865 "file %s returned %s\n",
866 smb_fname_str_dbg(smb_fname),
867 nt_errstr(status) ));
868 return status;
869 }
870 }
871 }
872
873 /*
874 * Actually do the open - if O_TRUNC is needed handle it
875 * below under the share mode lock.
876 */
877 status = fd_open_atomic(conn, fsp, local_flags & ~O_TRUNC,
878 unx_mode, p_file_created);
879 if (!NT_STATUS_IS_OK(status)) {
880 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
881 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
882 nt_errstr(status),local_flags,flags));
883 return status;
884 }
885
886 if (local_flags & O_NONBLOCK) {
887 /*
888 * GPFS can return ETIMEDOUT for pread on
889 * nonblocking file descriptors when files
890 * migrated to tape need to be recalled. I
891 * could imagine this happens elsehwere
892 * too. With blocking file descriptors this
893 * does not happen.
894 */
895 ret = set_blocking(fsp->fh->fd, true);
896 if (ret == -1) {
897 status = map_nt_error_from_unix(errno);
898 DBG_WARNING("Could not set fd to blocking: "
899 "%s\n", strerror(errno));
900 fd_close(fsp);
901 return status;
902 }
903 }
904
905 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
906 if (ret == -1) {
907 /* If we have an fd, this stat should succeed. */
908 DEBUG(0,("Error doing fstat on open file %s "
909 "(%s)\n",
910 smb_fname_str_dbg(smb_fname),
911 strerror(errno) ));
912 status = map_nt_error_from_unix(errno);
913 fd_close(fsp);
914 return status;
915 }
916
917 if (*p_file_created) {
918 /* We created this file. */
919
920 bool need_re_stat = false;
921 /* Do all inheritance work after we've
922 done a successful fstat call and filled
923 in the stat struct in fsp->fsp_name. */
924
925 /* Inherit the ACL if required */
926 if (lp_inherit_permissions(SNUM(conn))) {
927 inherit_access_posix_acl(conn, parent_dir,
928 smb_fname->base_name,
929 unx_mode);
930 need_re_stat = true;
931 }
932
933 /* Change the owner if required. */
934 if (lp_inherit_owner(SNUM(conn))) {
935 change_file_owner_to_parent(conn, parent_dir,
936 fsp);
937 need_re_stat = true;
938 }
939
940 if (need_re_stat) {
941 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
942 /* If we have an fd, this stat should succeed. */
943 if (ret == -1) {
944 DEBUG(0,("Error doing fstat on open file %s "
945 "(%s)\n",
946 smb_fname_str_dbg(smb_fname),
947 strerror(errno) ));
948 }
949 }
950
951 notify_fname(conn, NOTIFY_ACTION_ADDED,
952 FILE_NOTIFY_CHANGE_FILE_NAME,
953 smb_fname->base_name);
954 }
955 } else {
956 fsp->fh->fd = -1; /* What we used to call a stat open. */
957 if (!file_existed) {
958 /* File must exist for a stat open. */
959 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
960 }
961
962 status = smbd_check_access_rights(conn,
963 smb_fname,
964 false,
965 access_mask);
966
967 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
968 (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) &&
969 S_ISLNK(smb_fname->st.st_ex_mode)) {
970 /* This is a POSIX stat open for delete
971 * or rename on a symlink that points
972 * nowhere. Allow. */
973 DEBUG(10,("open_file: allowing POSIX "
974 "open on bad symlink %s\n",
975 smb_fname_str_dbg(smb_fname)));
976 status = NT_STATUS_OK;
977 }
978
979 if (!NT_STATUS_IS_OK(status)) {
980 DEBUG(10,("open_file: "
981 "smbd_check_access_rights on file "
982 "%s returned %s\n",
983 smb_fname_str_dbg(smb_fname),
984 nt_errstr(status) ));
985 return status;
986 }
987 }
988
989 /*
990 * POSIX allows read-only opens of directories. We don't
991 * want to do this (we use a different code path for this)
992 * so catch a directory open and return an EISDIR. JRA.
993 */
994
995 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
996 fd_close(fsp);
997 errno = EISDIR;
998 return NT_STATUS_FILE_IS_A_DIRECTORY;
999 }
1000
1001 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1002 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
1003 fsp->file_pid = req ? req->smbpid : 0;
1004 fsp->can_lock = True;
1005 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
1006 fsp->can_write =
1007 CAN_WRITE(conn) &&
1008 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
1009 fsp->print_file = NULL;
1010 fsp->modified = False;
1011 fsp->sent_oplock_break = NO_BREAK_SENT;
1012 fsp->is_directory = False;
1013 if (conn->aio_write_behind_list &&
1014 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
1015 conn->case_sensitive)) {
1016 fsp->aio_write_behind = True;
1017 }
1018
1019 fsp->wcp = NULL; /* Write cache pointer. */
1020
1021 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
1022 conn->session_info->unix_info->unix_name,
1023 smb_fname_str_dbg(smb_fname),
1024 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
1025 conn->num_files_open));
1026
1027 errno = 0;
1028 return NT_STATUS_OK;
1029}
1030
1031/****************************************************************************
1032 Check if we can open a file with a share mode.
1033 Returns True if conflict, False if not.
1034****************************************************************************/
1035
1036static bool share_conflict(struct share_mode_entry *entry,
1037 uint32_t access_mask,
1038 uint32_t share_access)
1039{
1040 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
1041 "entry->share_access = 0x%x, "
1042 "entry->private_options = 0x%x\n",
1043 (unsigned int)entry->access_mask,
1044 (unsigned int)entry->share_access,
1045 (unsigned int)entry->private_options));
1046
1047 if (server_id_is_disconnected(&entry->pid)) {
1048 /*
1049 * note: cleanup should have been done by
1050 * delay_for_batch_oplocks()
1051 */
1052 return false;
1053 }
1054
1055 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
1056 (unsigned int)access_mask, (unsigned int)share_access));
1057
1058 if ((entry->access_mask & (FILE_WRITE_DATA|
1059 FILE_APPEND_DATA|
1060 FILE_READ_DATA|
1061 FILE_EXECUTE|
1062 DELETE_ACCESS)) == 0) {
1063 DEBUG(10,("share_conflict: No conflict due to "
1064 "entry->access_mask = 0x%x\n",
1065 (unsigned int)entry->access_mask ));
1066 return False;
1067 }
1068
1069 if ((access_mask & (FILE_WRITE_DATA|
1070 FILE_APPEND_DATA|
1071 FILE_READ_DATA|
1072 FILE_EXECUTE|
1073 DELETE_ACCESS)) == 0) {
1074 DEBUG(10,("share_conflict: No conflict due to "
1075 "access_mask = 0x%x\n",
1076 (unsigned int)access_mask ));
1077 return False;
1078 }
1079
1080#if 1 /* JRA TEST - Superdebug. */
1081#define CHECK_MASK(num, am, right, sa, share) \
1082 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
1083 (unsigned int)(num), (unsigned int)(am), \
1084 (unsigned int)(right), (unsigned int)(am)&(right) )); \
1085 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
1086 (unsigned int)(num), (unsigned int)(sa), \
1087 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
1088 if (((am) & (right)) && !((sa) & (share))) { \
1089 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1090sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1091 (unsigned int)(share) )); \
1092 return True; \
1093 }
1094#else
1095#define CHECK_MASK(num, am, right, sa, share) \
1096 if (((am) & (right)) && !((sa) & (share))) { \
1097 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1098sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1099 (unsigned int)(share) )); \
1100 return True; \
1101 }
1102#endif
1103
1104 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1105 share_access, FILE_SHARE_WRITE);
1106 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1107 entry->share_access, FILE_SHARE_WRITE);
1108
1109 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
1110 share_access, FILE_SHARE_READ);
1111 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
1112 entry->share_access, FILE_SHARE_READ);
1113
1114 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
1115 share_access, FILE_SHARE_DELETE);
1116 CHECK_MASK(6, access_mask, DELETE_ACCESS,
1117 entry->share_access, FILE_SHARE_DELETE);
1118
1119 DEBUG(10,("share_conflict: No conflict.\n"));
1120 return False;
1121}
1122
1123#if defined(DEVELOPER)
1124static void validate_my_share_entries(struct smbd_server_connection *sconn,
1125 int num,
1126 struct share_mode_entry *share_entry)
1127{
1128 struct server_id self = messaging_server_id(sconn->msg_ctx);
1129 files_struct *fsp;
1130
1131 if (!serverid_equal(&self, &share_entry->pid)) {
1132 return;
1133 }
1134
1135 if (share_entry->op_mid == 0) {
1136 /* INTERNAL_OPEN_ONLY */
1137 return;
1138 }
1139
1140 if (!is_valid_share_mode_entry(share_entry)) {
1141 return;
1142 }
1143
1144 fsp = file_find_dif(sconn, share_entry->id,
1145 share_entry->share_file_id);
1146 if (!fsp) {
1147 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1148 share_mode_str(talloc_tos(), num, share_entry) ));
1149 smb_panic("validate_my_share_entries: Cannot match a "
1150 "share entry with an open file\n");
1151 }
1152
1153 if (((uint16_t)fsp->oplock_type) != share_entry->op_type) {
1154 goto panic;
1155 }
1156
1157 return;
1158
1159 panic:
1160 {
1161 char *str;
1162 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1163 share_mode_str(talloc_tos(), num, share_entry) ));
1164 str = talloc_asprintf(talloc_tos(),
1165 "validate_my_share_entries: "
1166 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1167 fsp->fsp_name->base_name,
1168 (unsigned int)fsp->oplock_type,
1169 (unsigned int)share_entry->op_type );
1170 smb_panic(str);
1171 }
1172}
1173#endif
1174
1175bool is_stat_open(uint32_t access_mask)
1176{
1177 const uint32_t stat_open_bits =
1178 (SYNCHRONIZE_ACCESS|
1179 FILE_READ_ATTRIBUTES|
1180 FILE_WRITE_ATTRIBUTES);
1181
1182 return (((access_mask & stat_open_bits) != 0) &&
1183 ((access_mask & ~stat_open_bits) == 0));
1184}
1185
1186static bool has_delete_on_close(struct share_mode_lock *lck,
1187 uint32_t name_hash)
1188{
1189 struct share_mode_data *d = lck->data;
1190 uint32_t i;
1191
1192 if (d->num_share_modes == 0) {
1193 return false;
1194 }
1195 if (!is_delete_on_close_set(lck, name_hash)) {
1196 return false;
1197 }
1198 for (i=0; i<d->num_share_modes; i++) {
1199 if (!share_mode_stale_pid(d, i)) {
1200 return true;
1201 }
1202 }
1203 return false;
1204}
1205
1206/****************************************************************************
1207 Deal with share modes
1208 Invariant: Share mode must be locked on entry and exit.
1209 Returns -1 on error, or number of share modes on success (may be zero).
1210****************************************************************************/
1211
1212static NTSTATUS open_mode_check(connection_struct *conn,
1213 struct share_mode_lock *lck,
1214 uint32_t access_mask,
1215 uint32_t share_access)
1216{
1217 int i;
1218
1219 if(lck->data->num_share_modes == 0) {
1220 return NT_STATUS_OK;
1221 }
1222
1223 if (is_stat_open(access_mask)) {
1224 /* Stat open that doesn't trigger oplock breaks or share mode
1225 * checks... ! JRA. */
1226 return NT_STATUS_OK;
1227 }
1228
1229 /*
1230 * Check if the share modes will give us access.
1231 */
1232
1233#if defined(DEVELOPER)
1234 for(i = 0; i < lck->data->num_share_modes; i++) {
1235 validate_my_share_entries(conn->sconn, i,
1236 &lck->data->share_modes[i]);
1237 }
1238#endif
1239
1240 /* Now we check the share modes, after any oplock breaks. */
1241 for(i = 0; i < lck->data->num_share_modes; i++) {
1242
1243 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1244 continue;
1245 }
1246
1247 /* someone else has a share lock on it, check to see if we can
1248 * too */
1249 if (share_conflict(&lck->data->share_modes[i],
1250 access_mask, share_access)) {
1251
1252 if (share_mode_stale_pid(lck->data, i)) {
1253 continue;
1254 }
1255
1256 return NT_STATUS_SHARING_VIOLATION;
1257 }
1258 }
1259
1260 return NT_STATUS_OK;
1261}
1262
1263/*
1264 * Send a break message to the oplock holder and delay the open for
1265 * our client.
1266 */
1267
1268NTSTATUS send_break_message(struct messaging_context *msg_ctx,
1269 const struct share_mode_entry *exclusive,
1270 uint16_t break_to)
1271{
1272 NTSTATUS status;
1273 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1274 struct server_id_buf tmp;
1275
1276 DEBUG(10, ("Sending break request to PID %s\n",
1277 server_id_str_buf(exclusive->pid, &tmp)));
1278
1279 /* Create the message. */
1280 share_mode_entry_to_message(msg, exclusive);
1281
1282 /* Overload entry->op_type */
1283 /*
1284 * This is a cut from uint32_t to uint16_t, but so far only the lower 3
1285 * bits (LEASE_WRITE/HANDLE/READ are used anyway.
1286 */
1287 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET, break_to);
1288
1289 status = messaging_send_buf(msg_ctx, exclusive->pid,
1290 MSG_SMB_BREAK_REQUEST,
1291 (uint8_t *)msg, sizeof(msg));
1292 if (!NT_STATUS_IS_OK(status)) {
1293 DEBUG(3, ("Could not send oplock break message: %s\n",
1294 nt_errstr(status)));
1295 }
1296
1297 return status;
1298}
1299
1300/*
1301 * Do internal consistency checks on the share mode for a file.
1302 */
1303
1304static bool validate_oplock_types(struct share_mode_lock *lck)
1305{
1306 struct share_mode_data *d = lck->data;
1307 bool batch = false;
1308 bool ex_or_batch = false;
1309 bool level2 = false;
1310 bool no_oplock = false;
1311 uint32_t num_non_stat_opens = 0;
1312 uint32_t i;
1313
1314 for (i=0; i<d->num_share_modes; i++) {
1315 struct share_mode_entry *e = &d->share_modes[i];
1316
1317 if (!is_valid_share_mode_entry(e)) {
1318 continue;
1319 }
1320
1321 if (e->op_mid == 0) {
1322 /* INTERNAL_OPEN_ONLY */
1323 continue;
1324 }
1325
1326 if (e->op_type == NO_OPLOCK && is_stat_open(e->access_mask)) {
1327 /* We ignore stat opens in the table - they
1328 always have NO_OPLOCK and never get or
1329 cause breaks. JRA. */
1330 continue;
1331 }
1332
1333 num_non_stat_opens += 1;
1334
1335 if (BATCH_OPLOCK_TYPE(e->op_type)) {
1336 /* batch - can only be one. */
1337 if (share_mode_stale_pid(d, i)) {
1338 DEBUG(10, ("Found stale batch oplock\n"));
1339 continue;
1340 }
1341 if (ex_or_batch || batch || level2 || no_oplock) {
1342 DEBUG(0, ("Bad batch oplock entry %u.",
1343 (unsigned)i));
1344 return false;
1345 }
1346 batch = true;
1347 }
1348
1349 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1350 if (share_mode_stale_pid(d, i)) {
1351 DEBUG(10, ("Found stale duplicate oplock\n"));
1352 continue;
1353 }
1354 /* Exclusive or batch - can only be one. */
1355 if (ex_or_batch || level2 || no_oplock) {
1356 DEBUG(0, ("Bad exclusive or batch oplock "
1357 "entry %u.", (unsigned)i));
1358 return false;
1359 }
1360 ex_or_batch = true;
1361 }
1362
1363 if (LEVEL_II_OPLOCK_TYPE(e->op_type)) {
1364 if (batch || ex_or_batch) {
1365 if (share_mode_stale_pid(d, i)) {
1366 DEBUG(10, ("Found stale LevelII "
1367 "oplock\n"));
1368 continue;
1369 }
1370 DEBUG(0, ("Bad levelII oplock entry %u.",
1371 (unsigned)i));
1372 return false;
1373 }
1374 level2 = true;
1375 }
1376
1377 if (e->op_type == NO_OPLOCK) {
1378 if (batch || ex_or_batch) {
1379 if (share_mode_stale_pid(d, i)) {
1380 DEBUG(10, ("Found stale NO_OPLOCK "
1381 "entry\n"));
1382 continue;
1383 }
1384 DEBUG(0, ("Bad no oplock entry %u.",
1385 (unsigned)i));
1386 return false;
1387 }
1388 no_oplock = true;
1389 }
1390 }
1391
1392 remove_stale_share_mode_entries(d);
1393
1394 if ((batch || ex_or_batch) && (num_non_stat_opens != 1)) {
1395 DEBUG(1, ("got batch (%d) or ex (%d) non-exclusively (%d)\n",
1396 (int)batch, (int)ex_or_batch,
1397 (int)d->num_share_modes));
1398 return false;
1399 }
1400
1401 return true;
1402}
1403
1404static bool delay_for_oplock(files_struct *fsp,
1405 int oplock_request,
1406 const struct smb2_lease *lease,
1407 struct share_mode_lock *lck,
1408 bool have_sharing_violation,
1409 uint32_t create_disposition,
1410 bool first_open_attempt)
1411{
1412 struct share_mode_data *d = lck->data;
1413 uint32_t i;
1414 bool delay = false;
1415 bool will_overwrite;
1416
1417 if ((oplock_request & INTERNAL_OPEN_ONLY) ||
1418 is_stat_open(fsp->access_mask)) {
1419 return false;
1420 }
1421
1422 switch (create_disposition) {
1423 case FILE_SUPERSEDE:
1424 case FILE_OVERWRITE:
1425 case FILE_OVERWRITE_IF:
1426 will_overwrite = true;
1427 break;
1428 default:
1429 will_overwrite = false;
1430 break;
1431 }
1432
1433 for (i=0; i<d->num_share_modes; i++) {
1434 struct share_mode_entry *e = &d->share_modes[i];
1435 struct share_mode_lease *l = NULL;
1436 uint32_t e_lease_type = get_lease_type(d, e);
1437 uint32_t break_to;
1438 uint32_t delay_mask = 0;
1439
1440 if (e->op_type == LEASE_OPLOCK) {
1441 l = &d->leases[e->lease_idx];
1442 }
1443
1444 if (have_sharing_violation) {
1445 delay_mask = SMB2_LEASE_HANDLE;
1446 } else {
1447 delay_mask = SMB2_LEASE_WRITE;
1448 }
1449
1450 break_to = e_lease_type & ~delay_mask;
1451
1452 if (will_overwrite) {
1453 /*
1454 * we'll decide about SMB2_LEASE_READ later.
1455 *
1456 * Maybe the break will be defered
1457 */
1458 break_to &= ~SMB2_LEASE_HANDLE;
1459 }
1460
1461 DEBUG(10, ("entry %u: e_lease_type %u, will_overwrite: %u\n",
1462 (unsigned)i, (unsigned)e_lease_type,
1463 (unsigned)will_overwrite));
1464
1465 if (lease != NULL && l != NULL) {
1466 bool ign;
1467
1468 ign = smb2_lease_equal(fsp_client_guid(fsp),
1469 &lease->lease_key,
1470 &l->client_guid,
1471 &l->lease_key);
1472 if (ign) {
1473 continue;
1474 }
1475 }
1476
1477 if ((e_lease_type & ~break_to) == 0) {
1478 if (l != NULL && l->breaking) {
1479 delay = true;
1480 }
1481 continue;
1482 }
1483
1484 if (share_mode_stale_pid(d, i)) {
1485 continue;
1486 }
1487
1488 if (will_overwrite) {
1489 /*
1490 * If we break anyway break to NONE directly.
1491 * Otherwise vfs_set_filelen() will trigger the
1492 * break.
1493 */
1494 break_to &= ~(SMB2_LEASE_READ|SMB2_LEASE_WRITE);
1495 }
1496
1497 if (e->op_type != LEASE_OPLOCK) {
1498 /*
1499 * Oplocks only support breaking to R or NONE.
1500 */
1501 break_to &= ~(SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1502 }
1503
1504 DEBUG(10, ("breaking from %d to %d\n",
1505 (int)e_lease_type, (int)break_to));
1506 send_break_message(fsp->conn->sconn->msg_ctx, e,
1507 break_to);
1508 if (e_lease_type & delay_mask) {
1509 delay = true;
1510 }
1511 if (l != NULL && l->breaking && !first_open_attempt) {
1512 delay = true;
1513 }
1514 continue;
1515 }
1516
1517 return delay;
1518}
1519
1520static bool file_has_brlocks(files_struct *fsp)
1521{
1522 struct byte_range_lock *br_lck;
1523
1524 br_lck = brl_get_locks_readonly(fsp);
1525 if (!br_lck)
1526 return false;
1527
1528 return (brl_num_locks(br_lck) > 0);
1529}
1530
1531int find_share_mode_lease(struct share_mode_data *d,
1532 const struct GUID *client_guid,
1533 const struct smb2_lease_key *key)
1534{
1535 uint32_t i;
1536
1537 for (i=0; i<d->num_leases; i++) {
1538 struct share_mode_lease *l = &d->leases[i];
1539
1540 if (smb2_lease_equal(client_guid,
1541 key,
1542 &l->client_guid,
1543 &l->lease_key)) {
1544 return i;
1545 }
1546 }
1547
1548 return -1;
1549}
1550
1551struct fsp_lease *find_fsp_lease(struct files_struct *new_fsp,
1552 const struct smb2_lease_key *key,
1553 const struct share_mode_lease *l)
1554{
1555 struct files_struct *fsp;
1556
1557 /*
1558 * TODO: Measure how expensive this loop is with thousands of open
1559 * handles...
1560 */
1561
1562 for (fsp = file_find_di_first(new_fsp->conn->sconn, new_fsp->file_id);
1563 fsp != NULL;
1564 fsp = file_find_di_next(fsp)) {
1565
1566 if (fsp == new_fsp) {
1567 continue;
1568 }
1569 if (fsp->oplock_type != LEASE_OPLOCK) {
1570 continue;
1571 }
1572 if (smb2_lease_key_equal(&fsp->lease->lease.lease_key, key)) {
1573 fsp->lease->ref_count += 1;
1574 return fsp->lease;
1575 }
1576 }
1577
1578 /* Not found - must be leased in another smbd. */
1579 new_fsp->lease = talloc_zero(new_fsp->conn->sconn, struct fsp_lease);
1580 if (new_fsp->lease == NULL) {
1581 return NULL;
1582 }
1583 new_fsp->lease->ref_count = 1;
1584 new_fsp->lease->sconn = new_fsp->conn->sconn;
1585 new_fsp->lease->lease.lease_key = *key;
1586 new_fsp->lease->lease.lease_state = l->current_state;
1587 /*
1588 * We internally treat all leases as V2 and update
1589 * the epoch, but when sending breaks it matters if
1590 * the requesting lease was v1 or v2.
1591 */
1592 new_fsp->lease->lease.lease_version = l->lease_version;
1593 new_fsp->lease->lease.lease_epoch = l->epoch;
1594 return new_fsp->lease;
1595}
1596
1597static NTSTATUS grant_fsp_lease(struct files_struct *fsp,
1598 struct share_mode_lock *lck,
1599 const struct smb2_lease *lease,
1600 uint32_t *p_lease_idx,
1601 uint32_t granted)
1602{
1603 struct share_mode_data *d = lck->data;
1604 const struct GUID *client_guid = fsp_client_guid(fsp);
1605 struct share_mode_lease *tmp;
1606 NTSTATUS status;
1607 int idx;
1608
1609 idx = find_share_mode_lease(d, client_guid, &lease->lease_key);
1610
1611 if (idx != -1) {
1612 struct share_mode_lease *l = &d->leases[idx];
1613 bool do_upgrade;
1614 uint32_t existing, requested;
1615
1616 fsp->lease = find_fsp_lease(fsp, &lease->lease_key, l);
1617 if (fsp->lease == NULL) {
1618 DEBUG(1, ("Did not find existing lease for file %s\n",
1619 fsp_str_dbg(fsp)));
1620 return NT_STATUS_NO_MEMORY;
1621 }
1622
1623 *p_lease_idx = idx;
1624
1625 /*
1626 * Upgrade only if the requested lease is a strict upgrade.
1627 */
1628 existing = l->current_state;
1629 requested = lease->lease_state;
1630
1631 /*
1632 * Tricky: This test makes sure that "requested" is a
1633 * strict bitwise superset of "existing".
1634 */
1635 do_upgrade = ((existing & requested) == existing);
1636
1637 /*
1638 * Upgrade only if there's a change.
1639 */
1640 do_upgrade &= (granted != existing);
1641
1642 /*
1643 * Upgrade only if other leases don't prevent what was asked
1644 * for.
1645 */
1646 do_upgrade &= (granted == requested);
1647
1648 /*
1649 * only upgrade if we are not in breaking state
1650 */
1651 do_upgrade &= !l->breaking;
1652
1653 DEBUG(10, ("existing=%"PRIu32", requested=%"PRIu32", "
1654 "granted=%"PRIu32", do_upgrade=%d\n",
1655 existing, requested, granted, (int)do_upgrade));
1656
1657 if (do_upgrade) {
1658 l->current_state = granted;
1659 l->epoch += 1;
1660 }
1661
1662 /* Ensure we're in sync with current lease state. */
1663 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
1664 return NT_STATUS_OK;
1665 }
1666
1667 /*
1668 * Create new lease
1669 */
1670
1671 tmp = talloc_realloc(d, d->leases, struct share_mode_lease,
1672 d->num_leases+1);
1673 if (tmp == NULL) {
1674 /*
1675 * See [MS-SMB2]
1676 */
1677 return NT_STATUS_INSUFFICIENT_RESOURCES;
1678 }
1679 d->leases = tmp;
1680
1681 fsp->lease = talloc_zero(fsp->conn->sconn, struct fsp_lease);
1682 if (fsp->lease == NULL) {
1683 return NT_STATUS_INSUFFICIENT_RESOURCES;
1684 }
1685 fsp->lease->ref_count = 1;
1686 fsp->lease->sconn = fsp->conn->sconn;
1687 fsp->lease->lease.lease_version = lease->lease_version;
1688 fsp->lease->lease.lease_key = lease->lease_key;
1689 fsp->lease->lease.lease_state = granted;
1690 fsp->lease->lease.lease_epoch = lease->lease_epoch + 1;
1691
1692 *p_lease_idx = d->num_leases;
1693
1694 d->leases[d->num_leases] = (struct share_mode_lease) {
1695 .client_guid = *client_guid,
1696 .lease_key = fsp->lease->lease.lease_key,
1697 .current_state = fsp->lease->lease.lease_state,
1698 .lease_version = fsp->lease->lease.lease_version,
1699 .epoch = fsp->lease->lease.lease_epoch,
1700 };
1701
1702 status = leases_db_add(client_guid,
1703 &lease->lease_key,
1704 &fsp->file_id,
1705 fsp->conn->connectpath,
1706 fsp->fsp_name->base_name,
1707 fsp->fsp_name->stream_name);
1708 if (!NT_STATUS_IS_OK(status)) {
1709 DEBUG(10, ("%s: leases_db_add failed: %s\n", __func__,
1710 nt_errstr(status)));
1711 TALLOC_FREE(fsp->lease);
1712 return NT_STATUS_INSUFFICIENT_RESOURCES;
1713 }
1714
1715 d->num_leases += 1;
1716 d->modified = true;
1717
1718 return NT_STATUS_OK;
1719}
1720
1721static bool is_same_lease(const files_struct *fsp,
1722 const struct share_mode_data *d,
1723 const struct share_mode_entry *e,
1724 const struct smb2_lease *lease)
1725{
1726 if (e->op_type != LEASE_OPLOCK) {
1727 return false;
1728 }
1729 if (lease == NULL) {
1730 return false;
1731 }
1732
1733 return smb2_lease_equal(fsp_client_guid(fsp),
1734 &lease->lease_key,
1735 &d->leases[e->lease_idx].client_guid,
1736 &d->leases[e->lease_idx].lease_key);
1737}
1738
1739static NTSTATUS grant_fsp_oplock_type(struct smb_request *req,
1740 struct files_struct *fsp,
1741 struct share_mode_lock *lck,
1742 int oplock_request,
1743 struct smb2_lease *lease)
1744{
1745 struct share_mode_data *d = lck->data;
1746 bool got_handle_lease = false;
1747 bool got_oplock = false;
1748 uint32_t i;
1749 uint32_t granted;
1750 uint32_t lease_idx = UINT32_MAX;
1751 bool ok;
1752 NTSTATUS status;
1753
1754 if (oplock_request & INTERNAL_OPEN_ONLY) {
1755 /* No oplocks on internal open. */
1756 oplock_request = NO_OPLOCK;
1757 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1758 fsp->oplock_type, fsp_str_dbg(fsp)));
1759 }
1760
1761 if (oplock_request == LEASE_OPLOCK) {
1762 if (lease == NULL) {
1763 /*
1764 * The SMB2 layer should have checked this
1765 */
1766 return NT_STATUS_INTERNAL_ERROR;
1767 }
1768
1769 granted = lease->lease_state;
1770
1771 if (lp_kernel_oplocks(SNUM(fsp->conn))) {
1772 DEBUG(10, ("No lease granted because kernel oplocks are enabled\n"));
1773 granted = SMB2_LEASE_NONE;
1774 }
1775 if ((granted & (SMB2_LEASE_READ|SMB2_LEASE_WRITE)) == 0) {
1776 DEBUG(10, ("No read or write lease requested\n"));
1777 granted = SMB2_LEASE_NONE;
1778 }
1779 if (granted == SMB2_LEASE_WRITE) {
1780 DEBUG(10, ("pure write lease requested\n"));
1781 granted = SMB2_LEASE_NONE;
1782 }
1783 if (granted == (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
1784 DEBUG(10, ("write and handle lease requested\n"));
1785 granted = SMB2_LEASE_NONE;
1786 }
1787 } else {
1788 granted = map_oplock_to_lease_type(
1789 oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1790 }
1791
1792 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1793 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1794 fsp_str_dbg(fsp)));
1795 granted &= ~SMB2_LEASE_READ;
1796 }
1797
1798 for (i=0; i<d->num_share_modes; i++) {
1799 struct share_mode_entry *e = &d->share_modes[i];
1800 uint32_t e_lease_type;
1801
1802 e_lease_type = get_lease_type(d, e);
1803
1804 if ((granted & SMB2_LEASE_WRITE) &&
1805 !is_same_lease(fsp, d, e, lease) &&
1806 !share_mode_stale_pid(d, i)) {
1807 /*
1808 * Can grant only one writer
1809 */
1810 granted &= ~SMB2_LEASE_WRITE;
1811 }
1812
1813 if ((e_lease_type & SMB2_LEASE_HANDLE) && !got_handle_lease &&
1814 !share_mode_stale_pid(d, i)) {
1815 got_handle_lease = true;
1816 }
1817
1818 if ((e->op_type != LEASE_OPLOCK) && !got_oplock &&
1819 !share_mode_stale_pid(d, i)) {
1820 got_oplock = true;
1821 }
1822 }
1823
1824 if ((granted & SMB2_LEASE_READ) && !(granted & SMB2_LEASE_WRITE)) {
1825 bool allow_level2 =
1826 (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1827 lp_level2_oplocks(SNUM(fsp->conn));
1828
1829 if (!allow_level2) {
1830 granted = SMB2_LEASE_NONE;
1831 }
1832 }
1833
1834 if (oplock_request == LEASE_OPLOCK) {
1835 if (got_oplock) {
1836 granted &= ~SMB2_LEASE_HANDLE;
1837 }
1838
1839 fsp->oplock_type = LEASE_OPLOCK;
1840
1841 status = grant_fsp_lease(fsp, lck, lease, &lease_idx,
1842 granted);
1843 if (!NT_STATUS_IS_OK(status)) {
1844 return status;
1845
1846 }
1847 *lease = fsp->lease->lease;
1848 DEBUG(10, ("lease_state=%d\n", lease->lease_state));
1849 } else {
1850 if (got_handle_lease) {
1851 granted = SMB2_LEASE_NONE;
1852 }
1853
1854 switch (granted) {
1855 case SMB2_LEASE_READ|SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE:
1856 fsp->oplock_type = BATCH_OPLOCK|EXCLUSIVE_OPLOCK;
1857 break;
1858 case SMB2_LEASE_READ|SMB2_LEASE_WRITE:
1859 fsp->oplock_type = EXCLUSIVE_OPLOCK;
1860 break;
1861 case SMB2_LEASE_READ|SMB2_LEASE_HANDLE:
1862 case SMB2_LEASE_READ:
1863 fsp->oplock_type = LEVEL_II_OPLOCK;
1864 break;
1865 default:
1866 fsp->oplock_type = NO_OPLOCK;
1867 break;
1868 }
1869
1870 status = set_file_oplock(fsp);
1871 if (!NT_STATUS_IS_OK(status)) {
1872 /*
1873 * Could not get the kernel oplock
1874 */
1875 fsp->oplock_type = NO_OPLOCK;
1876 }
1877 }
1878
1879 ok = set_share_mode(lck, fsp, get_current_uid(fsp->conn),
1880 req ? req->mid : 0,
1881 fsp->oplock_type,
1882 lease_idx);
1883 if (!ok) {
1884 return NT_STATUS_NO_MEMORY;
1885 }
1886
1887 ok = update_num_read_oplocks(fsp, lck);
1888 if (!ok) {
1889 del_share_mode(lck, fsp);
1890 return NT_STATUS_INTERNAL_ERROR;
1891 }
1892
1893 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1894 fsp->oplock_type, fsp_str_dbg(fsp)));
1895
1896 return NT_STATUS_OK;
1897}
1898
1899static bool request_timed_out(struct timeval request_time,
1900 struct timeval timeout)
1901{
1902 struct timeval now, end_time;
1903 GetTimeOfDay(&now);
1904 end_time = timeval_sum(&request_time, &timeout);
1905 return (timeval_compare(&end_time, &now) < 0);
1906}
1907
1908struct defer_open_state {
1909 struct smbXsrv_connection *xconn;
1910 uint64_t mid;
1911};
1912
1913static void defer_open_done(struct tevent_req *req);
1914
1915/****************************************************************************
1916 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1917****************************************************************************/
1918
1919static void defer_open(struct share_mode_lock *lck,
1920 struct timeval request_time,
1921 struct timeval timeout,
1922 struct smb_request *req,
1923 struct deferred_open_record *state)
1924{
1925 struct deferred_open_record *open_rec;
1926
1927 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1928 "open entry for mid %llu\n",
1929 (unsigned int)request_time.tv_sec,
1930 (unsigned int)request_time.tv_usec,
1931 (unsigned long long)req->mid));
1932
1933 open_rec = talloc(NULL, struct deferred_open_record);
1934 if (open_rec == NULL) {
1935 TALLOC_FREE(lck);
1936 exit_server("talloc failed");
1937 }
1938
1939 *open_rec = *state;
1940
1941 if (lck) {
1942 struct defer_open_state *watch_state;
1943 struct tevent_req *watch_req;
1944 bool ret;
1945
1946 watch_state = talloc(open_rec, struct defer_open_state);
1947 if (watch_state == NULL) {
1948 exit_server("talloc failed");
1949 }
1950 watch_state->xconn = req->xconn;
1951 watch_state->mid = req->mid;
1952
1953 DEBUG(10, ("defering mid %llu\n",
1954 (unsigned long long)req->mid));
1955
1956 watch_req = dbwrap_record_watch_send(
1957 watch_state, req->sconn->ev_ctx, lck->data->record,
1958 req->sconn->msg_ctx);
1959 if (watch_req == NULL) {
1960 exit_server("Could not watch share mode record");
1961 }
1962 tevent_req_set_callback(watch_req, defer_open_done,
1963 watch_state);
1964
1965 ret = tevent_req_set_endtime(
1966 watch_req, req->sconn->ev_ctx,
1967 timeval_sum(&request_time, &timeout));
1968 SMB_ASSERT(ret);
1969 }
1970
1971 if (!push_deferred_open_message_smb(req, request_time, timeout,
1972 state->id, open_rec)) {
1973 TALLOC_FREE(lck);
1974 exit_server("push_deferred_open_message_smb failed");
1975 }
1976}
1977
1978static void defer_open_done(struct tevent_req *req)
1979{
1980 struct defer_open_state *state = tevent_req_callback_data(
1981 req, struct defer_open_state);
1982 NTSTATUS status;
1983 bool ret;
1984
1985 status = dbwrap_record_watch_recv(req, talloc_tos(), NULL);
1986 TALLOC_FREE(req);
1987 if (!NT_STATUS_IS_OK(status)) {
1988 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
1989 nt_errstr(status)));
1990 /*
1991 * Even if it failed, retry anyway. TODO: We need a way to
1992 * tell a re-scheduled open about that error.
1993 */
1994 }
1995
1996 DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state->mid));
1997
1998 ret = schedule_deferred_open_message_smb(state->xconn, state->mid);
1999 SMB_ASSERT(ret);
2000 TALLOC_FREE(state);
2001}
2002
2003
2004/****************************************************************************
2005 On overwrite open ensure that the attributes match.
2006****************************************************************************/
2007
2008static bool open_match_attributes(connection_struct *conn,
2009 uint32_t old_dos_attr,
2010 uint32_t new_dos_attr,
2011 mode_t existing_unx_mode,
2012 mode_t new_unx_mode,
2013 mode_t *returned_unx_mode)
2014{
2015 uint32_t noarch_old_dos_attr, noarch_new_dos_attr;
2016
2017 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
2018 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
2019
2020 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
2021 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
2022 *returned_unx_mode = new_unx_mode;
2023 } else {
2024 *returned_unx_mode = (mode_t)0;
2025 }
2026
2027 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
2028 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
2029 "returned_unx_mode = 0%o\n",
2030 (unsigned int)old_dos_attr,
2031 (unsigned int)existing_unx_mode,
2032 (unsigned int)new_dos_attr,
2033 (unsigned int)*returned_unx_mode ));
2034
2035 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
2036 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
2037 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
2038 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
2039 return False;
2040 }
2041 }
2042 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
2043 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
2044 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
2045 return False;
2046 }
2047 }
2048 return True;
2049}
2050
2051/****************************************************************************
2052 Special FCB or DOS processing in the case of a sharing violation.
2053 Try and find a duplicated file handle.
2054****************************************************************************/
2055
2056static NTSTATUS fcb_or_dos_open(struct smb_request *req,
2057 connection_struct *conn,
2058 files_struct *fsp_to_dup_into,
2059 const struct smb_filename *smb_fname,
2060 struct file_id id,
2061 uint16_t file_pid,
2062 uint64_t vuid,
2063 uint32_t access_mask,
2064 uint32_t share_access,
2065 uint32_t create_options)
2066{
2067 files_struct *fsp;
2068
2069 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
2070 "file %s.\n", smb_fname_str_dbg(smb_fname)));
2071
2072 for(fsp = file_find_di_first(conn->sconn, id); fsp;
2073 fsp = file_find_di_next(fsp)) {
2074
2075 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
2076 "vuid = %llu, file_pid = %u, private_options = 0x%x "
2077 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
2078 fsp->fh->fd, (unsigned long long)fsp->vuid,
2079 (unsigned int)fsp->file_pid,
2080 (unsigned int)fsp->fh->private_options,
2081 (unsigned int)fsp->access_mask ));
2082
2083 if (fsp != fsp_to_dup_into &&
2084 fsp->fh->fd != -1 &&
2085 fsp->vuid == vuid &&
2086 fsp->file_pid == file_pid &&
2087 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
2088 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
2089 (fsp->access_mask & FILE_WRITE_DATA) &&
2090 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
2091 strequal(fsp->fsp_name->stream_name,
2092 smb_fname->stream_name)) {
2093 DEBUG(10,("fcb_or_dos_open: file match\n"));
2094 break;
2095 }
2096 }
2097
2098 if (!fsp) {
2099 return NT_STATUS_NOT_FOUND;
2100 }
2101
2102 /* quite an insane set of semantics ... */
2103 if (is_executable(smb_fname->base_name) &&
2104 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
2105 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
2106 return NT_STATUS_INVALID_PARAMETER;
2107 }
2108
2109 /* We need to duplicate this fsp. */
2110 return dup_file_fsp(req, fsp, access_mask, share_access,
2111 create_options, fsp_to_dup_into);
2112}
2113
2114static void schedule_defer_open(struct share_mode_lock *lck,
2115 struct file_id id,
2116 struct timeval request_time,
2117 struct smb_request *req)
2118{
2119 struct deferred_open_record state;
2120
2121 /* This is a relative time, added to the absolute
2122 request_time value to get the absolute timeout time.
2123 Note that if this is the second or greater time we enter
2124 this codepath for this particular request mid then
2125 request_time is left as the absolute time of the *first*
2126 time this request mid was processed. This is what allows
2127 the request to eventually time out. */
2128
2129 struct timeval timeout;
2130
2131 /* Normally the smbd we asked should respond within
2132 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
2133 * the client did, give twice the timeout as a safety
2134 * measure here in case the other smbd is stuck
2135 * somewhere else. */
2136
2137 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
2138
2139 /* Nothing actually uses state.delayed_for_oplocks
2140 but it's handy to differentiate in debug messages
2141 between a 30 second delay due to oplock break, and
2142 a 1 second delay for share mode conflicts. */
2143
2144 state.delayed_for_oplocks = True;
2145 state.async_open = false;
2146 state.id = id;
2147
2148 if (!request_timed_out(request_time, timeout)) {
2149 defer_open(lck, request_time, timeout, req, &state);
2150 }
2151}
2152
2153/****************************************************************************
2154 Reschedule an open call that went asynchronous.
2155****************************************************************************/
2156
2157static void schedule_async_open(struct timeval request_time,
2158 struct smb_request *req)
2159{
2160 struct deferred_open_record state;
2161 struct timeval timeout;
2162
2163 timeout = timeval_set(20, 0);
2164
2165 ZERO_STRUCT(state);
2166 state.delayed_for_oplocks = false;
2167 state.async_open = true;
2168
2169 if (!request_timed_out(request_time, timeout)) {
2170 defer_open(NULL, request_time, timeout, req, &state);
2171 }
2172}
2173
2174/****************************************************************************
2175 Work out what access_mask to use from what the client sent us.
2176****************************************************************************/
2177
2178static NTSTATUS smbd_calculate_maximum_allowed_access(
2179 connection_struct *conn,
2180 const struct smb_filename *smb_fname,
2181 bool use_privs,
2182 uint32_t *p_access_mask)
2183{
2184 struct security_descriptor *sd;
2185 uint32_t access_granted;
2186 NTSTATUS status;
2187
2188 if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
2189 *p_access_mask |= FILE_GENERIC_ALL;
2190 return NT_STATUS_OK;
2191 }
2192
2193 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
2194 (SECINFO_OWNER |
2195 SECINFO_GROUP |
2196 SECINFO_DACL),
2197 talloc_tos(), &sd);
2198
2199 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2200 /*
2201 * File did not exist
2202 */
2203 *p_access_mask = FILE_GENERIC_ALL;
2204 return NT_STATUS_OK;
2205 }
2206 if (!NT_STATUS_IS_OK(status)) {
2207 DEBUG(10,("Could not get acl on file %s: %s\n",
2208 smb_fname_str_dbg(smb_fname),
2209 nt_errstr(status)));
2210 return NT_STATUS_ACCESS_DENIED;
2211 }
2212
2213 /*
2214 * If we can access the path to this file, by
2215 * default we have FILE_READ_ATTRIBUTES from the
2216 * containing directory. See the section:
2217 * "Algorithm to Check Access to an Existing File"
2218 * in MS-FSA.pdf.
2219 *
2220 * se_file_access_check()
2221 * also takes care of owner WRITE_DAC and READ_CONTROL.
2222 */
2223 status = se_file_access_check(sd,
2224 get_current_nttok(conn),
2225 use_privs,
2226 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
2227 &access_granted);
2228
2229 TALLOC_FREE(sd);
2230
2231 if (!NT_STATUS_IS_OK(status)) {
2232 DEBUG(10, ("Access denied on file %s: "
2233 "when calculating maximum access\n",
2234 smb_fname_str_dbg(smb_fname)));
2235 return NT_STATUS_ACCESS_DENIED;
2236 }
2237 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
2238
2239 if (!(access_granted & DELETE_ACCESS)) {
2240 if (can_delete_file_in_directory(conn, smb_fname)) {
2241 *p_access_mask |= DELETE_ACCESS;
2242 }
2243 }
2244
2245 return NT_STATUS_OK;
2246}
2247
2248NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
2249 const struct smb_filename *smb_fname,
2250 bool use_privs,
2251 uint32_t access_mask,
2252 uint32_t *access_mask_out)
2253{
2254 NTSTATUS status;
2255 uint32_t orig_access_mask = access_mask;
2256 uint32_t rejected_share_access;
2257
2258 /*
2259 * Convert GENERIC bits to specific bits.
2260 */
2261
2262 se_map_generic(&access_mask, &file_generic_mapping);
2263
2264 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
2265 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
2266
2267 status = smbd_calculate_maximum_allowed_access(
2268 conn, smb_fname, use_privs, &access_mask);
2269
2270 if (!NT_STATUS_IS_OK(status)) {
2271 return status;
2272 }
2273
2274 access_mask &= conn->share_access;
2275 }
2276
2277 rejected_share_access = access_mask & ~(conn->share_access);
2278
2279 if (rejected_share_access) {
2280 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
2281 "file %s: rejected by share access mask[0x%08X] "
2282 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
2283 smb_fname_str_dbg(smb_fname),
2284 conn->share_access,
2285 orig_access_mask, access_mask,
2286 rejected_share_access));
2287 return NT_STATUS_ACCESS_DENIED;
2288 }
2289
2290 *access_mask_out = access_mask;
2291 return NT_STATUS_OK;
2292}
2293
2294/****************************************************************************
2295 Remove the deferred open entry under lock.
2296****************************************************************************/
2297
2298/****************************************************************************
2299 Return true if this is a state pointer to an asynchronous create.
2300****************************************************************************/
2301
2302bool is_deferred_open_async(const struct deferred_open_record *rec)
2303{
2304 return rec->async_open;
2305}
2306
2307static bool clear_ads(uint32_t create_disposition)
2308{
2309 bool ret = false;
2310
2311 switch (create_disposition) {
2312 case FILE_SUPERSEDE:
2313 case FILE_OVERWRITE_IF:
2314 case FILE_OVERWRITE:
2315 ret = true;
2316 break;
2317 default:
2318 break;
2319 }
2320 return ret;
2321}
2322
2323static int disposition_to_open_flags(uint32_t create_disposition)
2324{
2325 int ret = 0;
2326
2327 /*
2328 * Currently we're using FILE_SUPERSEDE as the same as
2329 * FILE_OVERWRITE_IF but they really are
2330 * different. FILE_SUPERSEDE deletes an existing file
2331 * (requiring delete access) then recreates it.
2332 */
2333
2334 switch (create_disposition) {
2335 case FILE_SUPERSEDE:
2336 case FILE_OVERWRITE_IF:
2337 /*
2338 * If file exists replace/overwrite. If file doesn't
2339 * exist create.
2340 */
2341 ret = O_CREAT|O_TRUNC;
2342 break;
2343
2344 case FILE_OPEN:
2345 /*
2346 * If file exists open. If file doesn't exist error.
2347 */
2348 ret = 0;
2349 break;
2350
2351 case FILE_OVERWRITE:
2352 /*
2353 * If file exists overwrite. If file doesn't exist
2354 * error.
2355 */
2356 ret = O_TRUNC;
2357 break;
2358
2359 case FILE_CREATE:
2360 /*
2361 * If file exists error. If file doesn't exist create.
2362 */
2363 ret = O_CREAT|O_EXCL;
2364 break;
2365
2366 case FILE_OPEN_IF:
2367 /*
2368 * If file exists open. If file doesn't exist create.
2369 */
2370 ret = O_CREAT;
2371 break;
2372 }
2373 return ret;
2374}
2375
2376static int calculate_open_access_flags(uint32_t access_mask,
2377 uint32_t private_flags)
2378{
2379 bool need_write, need_read;
2380
2381 /*
2382 * Note that we ignore the append flag as append does not
2383 * mean the same thing under DOS and Unix.
2384 */
2385
2386 need_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA));
2387 if (!need_write) {
2388 return O_RDONLY;
2389 }
2390
2391 /* DENY_DOS opens are always underlying read-write on the
2392 file handle, no matter what the requested access mask
2393 says. */
2394
2395 need_read =
2396 ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
2397 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|
2398 FILE_READ_EA|FILE_EXECUTE));
2399
2400 if (!need_read) {
2401 return O_WRONLY;
2402 }
2403 return O_RDWR;
2404}
2405
2406/****************************************************************************
2407 Open a file with a share mode. Passed in an already created files_struct *.
2408****************************************************************************/
2409
2410static NTSTATUS open_file_ntcreate(connection_struct *conn,
2411 struct smb_request *req,
2412 uint32_t access_mask, /* access bits (FILE_READ_DATA etc.) */
2413 uint32_t share_access, /* share constants (FILE_SHARE_READ etc) */
2414 uint32_t create_disposition, /* FILE_OPEN_IF etc. */
2415 uint32_t create_options, /* options such as delete on close. */
2416 uint32_t new_dos_attributes, /* attributes used for new file. */
2417 int oplock_request, /* internal Samba oplock codes. */
2418 struct smb2_lease *lease,
2419 /* Information (FILE_EXISTS etc.) */
2420 uint32_t private_flags, /* Samba specific flags. */
2421 int *pinfo,
2422 files_struct *fsp)
2423{
2424 struct smb_filename *smb_fname = fsp->fsp_name;
2425 int flags=0;
2426 int flags2=0;
2427 bool file_existed = VALID_STAT(smb_fname->st);
2428 bool def_acl = False;
2429 bool posix_open = False;
2430 bool new_file_created = False;
2431 bool first_open_attempt = true;
2432 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
2433 mode_t new_unx_mode = (mode_t)0;
2434 mode_t unx_mode = (mode_t)0;
2435 int info;
2436 uint32_t existing_dos_attributes = 0;
2437 struct timeval request_time = timeval_zero();
2438 struct share_mode_lock *lck = NULL;
2439 uint32_t open_access_mask = access_mask;
2440 NTSTATUS status;
2441 char *parent_dir;
2442 SMB_STRUCT_STAT saved_stat = smb_fname->st;
2443 struct timespec old_write_time;
2444 struct file_id id;
2445
2446 if (conn->printer) {
2447 /*
2448 * Printers are handled completely differently.
2449 * Most of the passed parameters are ignored.
2450 */
2451
2452 if (pinfo) {
2453 *pinfo = FILE_WAS_CREATED;
2454 }
2455
2456 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
2457 smb_fname_str_dbg(smb_fname)));
2458
2459 if (!req) {
2460 DEBUG(0,("open_file_ntcreate: printer open without "
2461 "an SMB request!\n"));
2462 return NT_STATUS_INTERNAL_ERROR;
2463 }
2464
2465 return print_spool_open(fsp, smb_fname->base_name,
2466 req->vuid);
2467 }
2468
2469 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
2470 NULL)) {
2471 return NT_STATUS_NO_MEMORY;
2472 }
2473
2474 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2475 posix_open = True;
2476 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2477 new_dos_attributes = 0;
2478 } else {
2479 /* Windows allows a new file to be created and
2480 silently removes a FILE_ATTRIBUTE_DIRECTORY
2481 sent by the client. Do the same. */
2482
2483 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
2484
2485 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2486 * created new. */
2487 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2488 smb_fname, parent_dir);
2489 }
2490
2491 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2492 "access_mask=0x%x share_access=0x%x "
2493 "create_disposition = 0x%x create_options=0x%x "
2494 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2495 smb_fname_str_dbg(smb_fname), new_dos_attributes,
2496 access_mask, share_access, create_disposition,
2497 create_options, (unsigned int)unx_mode, oplock_request,
2498 (unsigned int)private_flags));
2499
2500 if (req == NULL) {
2501 /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
2502 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) != 0));
2503 } else {
2504 /* And req != NULL means no INTERNAL_OPEN_ONLY */
2505 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) == 0));
2506 }
2507
2508 /*
2509 * Only non-internal opens can be deferred at all
2510 */
2511
2512 if (req) {
2513 struct deferred_open_record *open_rec;
2514 if (get_deferred_open_message_state(req,
2515 &request_time,
2516 &open_rec)) {
2517 /* Remember the absolute time of the original
2518 request with this mid. We'll use it later to
2519 see if this has timed out. */
2520
2521 /* If it was an async create retry, the file
2522 didn't exist. */
2523
2524 if (is_deferred_open_async(open_rec)) {
2525 SET_STAT_INVALID(smb_fname->st);
2526 file_existed = false;
2527 }
2528
2529 /* Ensure we don't reprocess this message. */
2530 remove_deferred_open_message_smb(req->xconn, req->mid);
2531
2532 first_open_attempt = false;
2533 }
2534 }
2535
2536 if (!posix_open) {
2537 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
2538 if (file_existed) {
2539 /*
2540 * Only use strored DOS attributes for checks
2541 * against requested attributes (below via
2542 * open_match_attributes()), cf bug #11992
2543 * for details. -slow
2544 */
2545 bool ok;
2546 uint32_t attr = 0;
2547
2548 ok = get_ea_dos_attribute(conn, smb_fname, &attr);
2549 if (ok) {
2550 existing_dos_attributes = attr;
2551 }
2552 }
2553 }
2554
2555 /* ignore any oplock requests if oplocks are disabled */
2556 if (!lp_oplocks(SNUM(conn)) ||
2557 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
2558 /* Mask off everything except the private Samba bits. */
2559 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
2560 }
2561
2562 /* this is for OS/2 long file names - say we don't support them */
2563 if (req != NULL && !req->posix_pathnames &&
2564 strstr(smb_fname->base_name,".+,;=[].")) {
2565 /* OS/2 Workplace shell fix may be main code stream in a later
2566 * release. */
2567 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2568 "supported.\n"));
2569 if (use_nt_status()) {
2570 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2571 }
2572 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2573 }
2574
2575 switch( create_disposition ) {
2576 case FILE_OPEN:
2577 /* If file exists open. If file doesn't exist error. */
2578 if (!file_existed) {
2579 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2580 "requested for file %s and file "
2581 "doesn't exist.\n",
2582 smb_fname_str_dbg(smb_fname)));
2583 errno = ENOENT;
2584 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2585 }
2586 break;
2587
2588 case FILE_OVERWRITE:
2589 /* If file exists overwrite. If file doesn't exist
2590 * error. */
2591 if (!file_existed) {
2592 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2593 "requested for file %s and file "
2594 "doesn't exist.\n",
2595 smb_fname_str_dbg(smb_fname) ));
2596 errno = ENOENT;
2597 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2598 }
2599 break;
2600
2601 case FILE_CREATE:
2602 /* If file exists error. If file doesn't exist
2603 * create. */
2604 if (file_existed) {
2605 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2606 "requested for file %s and file "
2607 "already exists.\n",
2608 smb_fname_str_dbg(smb_fname)));
2609 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2610 errno = EISDIR;
2611 } else {
2612 errno = EEXIST;
2613 }
2614 return map_nt_error_from_unix(errno);
2615 }
2616 break;
2617
2618 case FILE_SUPERSEDE:
2619 case FILE_OVERWRITE_IF:
2620 case FILE_OPEN_IF:
2621 break;
2622 default:
2623 return NT_STATUS_INVALID_PARAMETER;
2624 }
2625
2626 flags2 = disposition_to_open_flags(create_disposition);
2627
2628 /* We only care about matching attributes on file exists and
2629 * overwrite. */
2630
2631 if (!posix_open && file_existed &&
2632 ((create_disposition == FILE_OVERWRITE) ||
2633 (create_disposition == FILE_OVERWRITE_IF))) {
2634 if (!open_match_attributes(conn, existing_dos_attributes,
2635 new_dos_attributes,
2636 smb_fname->st.st_ex_mode,
2637 unx_mode, &new_unx_mode)) {
2638 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2639 "for file %s (%x %x) (0%o, 0%o)\n",
2640 smb_fname_str_dbg(smb_fname),
2641 existing_dos_attributes,
2642 new_dos_attributes,
2643 (unsigned int)smb_fname->st.st_ex_mode,
2644 (unsigned int)unx_mode ));
2645 errno = EACCES;
2646 return NT_STATUS_ACCESS_DENIED;
2647 }
2648 }
2649
2650 status = smbd_calculate_access_mask(conn, smb_fname,
2651 false,
2652 access_mask,
2653 &access_mask);
2654 if (!NT_STATUS_IS_OK(status)) {
2655 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2656 "on file %s returned %s\n",
2657 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2658 return status;
2659 }
2660
2661 open_access_mask = access_mask;
2662
2663 if (flags2 & O_TRUNC) {
2664 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2665 }
2666
2667 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2668 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2669 access_mask));
2670
2671 /*
2672 * Note that we ignore the append flag as append does not
2673 * mean the same thing under DOS and Unix.
2674 */
2675
2676 flags = calculate_open_access_flags(access_mask, private_flags);
2677
2678 /*
2679 * Currently we only look at FILE_WRITE_THROUGH for create options.
2680 */
2681
2682#if defined(O_SYNC)
2683 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2684 flags2 |= O_SYNC;
2685 }
2686#endif /* O_SYNC */
2687
2688 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2689 flags2 |= O_APPEND;
2690 }
2691
2692 if (!posix_open && !CAN_WRITE(conn)) {
2693 /*
2694 * We should really return a permission denied error if either
2695 * O_CREAT or O_TRUNC are set, but for compatibility with
2696 * older versions of Samba we just AND them out.
2697 */
2698 flags2 &= ~(O_CREAT|O_TRUNC);
2699 }
2700
2701 if (first_open_attempt && lp_kernel_oplocks(SNUM(conn))) {
2702 /*
2703 * With kernel oplocks the open breaking an oplock
2704 * blocks until the oplock holder has given up the
2705 * oplock or closed the file. We prevent this by first
2706 * trying to open the file with O_NONBLOCK (see "man
2707 * fcntl" on Linux). For the second try, triggered by
2708 * an oplock break response, we do not need this
2709 * anymore.
2710 *
2711 * This is true under the assumption that only Samba
2712 * requests kernel oplocks. Once someone else like
2713 * NFSv4 starts to use that API, we will have to
2714 * modify this by communicating with the NFSv4 server.
2715 */
2716 flags2 |= O_NONBLOCK;
2717 }
2718
2719 /*
2720 * Ensure we can't write on a read-only share or file.
2721 */
2722
2723 if (flags != O_RDONLY && file_existed &&
2724 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2725 DEBUG(5,("open_file_ntcreate: write access requested for "
2726 "file %s on read only %s\n",
2727 smb_fname_str_dbg(smb_fname),
2728 !CAN_WRITE(conn) ? "share" : "file" ));
2729 errno = EACCES;
2730 return NT_STATUS_ACCESS_DENIED;
2731 }
2732
2733 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2734 fsp->share_access = share_access;
2735 fsp->fh->private_options = private_flags;
2736 fsp->access_mask = open_access_mask; /* We change this to the
2737 * requested access_mask after
2738 * the open is done. */
2739 if (posix_open) {
2740 fsp->posix_flags |= FSP_POSIX_FLAGS_ALL;
2741 }
2742
2743 if (timeval_is_zero(&request_time)) {
2744 request_time = fsp->open_time;
2745 }
2746
2747 /*
2748 * Ensure we pay attention to default ACLs on directories if required.
2749 */
2750
2751 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2752 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2753 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2754 }
2755
2756 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2757 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2758 (unsigned int)flags, (unsigned int)flags2,
2759 (unsigned int)unx_mode, (unsigned int)access_mask,
2760 (unsigned int)open_access_mask));
2761
2762 fsp_open = open_file(fsp, conn, req, parent_dir,
2763 flags|flags2, unx_mode, access_mask,
2764 open_access_mask, &new_file_created);
2765
2766 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_NETWORK_BUSY)) {
2767 struct deferred_open_record state;
2768
2769 /*
2770 * EWOULDBLOCK/EAGAIN maps to NETWORK_BUSY.
2771 */
2772 if (file_existed && S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
2773 DEBUG(10, ("FIFO busy\n"));
2774 return NT_STATUS_NETWORK_BUSY;
2775 }
2776 if (req == NULL) {
2777 DEBUG(10, ("Internal open busy\n"));
2778 return NT_STATUS_NETWORK_BUSY;
2779 }
2780
2781 /*
2782 * From here on we assume this is an oplock break triggered
2783 */
2784
2785 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
2786 if (lck == NULL) {
2787 state.delayed_for_oplocks = false;
2788 state.async_open = false;
2789 state.id = fsp->file_id;
2790 defer_open(NULL, request_time, timeval_set(0, 0),
2791 req, &state);
2792 DEBUG(10, ("No share mode lock found after "
2793 "EWOULDBLOCK, retrying sync\n"));
2794 return NT_STATUS_SHARING_VIOLATION;
2795 }
2796
2797 if (!validate_oplock_types(lck)) {
2798 smb_panic("validate_oplock_types failed");
2799 }
2800
2801 if (delay_for_oplock(fsp, 0, lease, lck, false,
2802 create_disposition, first_open_attempt)) {
2803 schedule_defer_open(lck, fsp->file_id, request_time,
2804 req);
2805 TALLOC_FREE(lck);
2806 DEBUG(10, ("Sent oplock break request to kernel "
2807 "oplock holder\n"));
2808 return NT_STATUS_SHARING_VIOLATION;
2809 }
2810
2811 /*
2812 * No oplock from Samba around. Immediately retry with
2813 * a blocking open.
2814 */
2815 state.delayed_for_oplocks = false;
2816 state.async_open = false;
2817 state.id = fsp->file_id;
2818 defer_open(lck, request_time, timeval_set(0, 0), req, &state);
2819 TALLOC_FREE(lck);
2820 DEBUG(10, ("No Samba oplock around after EWOULDBLOCK. "
2821 "Retrying sync\n"));
2822 return NT_STATUS_SHARING_VIOLATION;
2823 }
2824
2825 if (!NT_STATUS_IS_OK(fsp_open)) {
2826 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2827 schedule_async_open(request_time, req);
2828 }
2829 return fsp_open;
2830 }
2831
2832 if (new_file_created) {
2833 /*
2834 * As we atomically create using O_CREAT|O_EXCL,
2835 * then if new_file_created is true, then
2836 * file_existed *MUST* have been false (even
2837 * if the file was previously detected as being
2838 * there).
2839 */
2840 file_existed = false;
2841 }
2842
2843 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2844 /*
2845 * The file did exist, but some other (local or NFS)
2846 * process either renamed/unlinked and re-created the
2847 * file with different dev/ino after we walked the path,
2848 * but before we did the open. We could retry the
2849 * open but it's a rare enough case it's easier to
2850 * just fail the open to prevent creating any problems
2851 * in the open file db having the wrong dev/ino key.
2852 */
2853 fd_close(fsp);
2854 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2855 "Old (dev=0x%llu, ino =0x%llu). "
2856 "New (dev=0x%llu, ino=0x%llu). Failing open "
2857 " with NT_STATUS_ACCESS_DENIED.\n",
2858 smb_fname_str_dbg(smb_fname),
2859 (unsigned long long)saved_stat.st_ex_dev,
2860 (unsigned long long)saved_stat.st_ex_ino,
2861 (unsigned long long)smb_fname->st.st_ex_dev,
2862 (unsigned long long)smb_fname->st.st_ex_ino));
2863 return NT_STATUS_ACCESS_DENIED;
2864 }
2865
2866 old_write_time = smb_fname->st.st_ex_mtime;
2867
2868 /*
2869 * Deal with the race condition where two smbd's detect the
2870 * file doesn't exist and do the create at the same time. One
2871 * of them will win and set a share mode, the other (ie. this
2872 * one) should check if the requested share mode for this
2873 * create is allowed.
2874 */
2875
2876 /*
2877 * Now the file exists and fsp is successfully opened,
2878 * fsp->dev and fsp->inode are valid and should replace the
2879 * dev=0,inode=0 from a non existent file. Spotted by
2880 * Nadav Danieli <nadavd@exanet.com>. JRA.
2881 */
2882
2883 id = fsp->file_id;
2884
2885 lck = get_share_mode_lock(talloc_tos(), id,
2886 conn->connectpath,
2887 smb_fname, &old_write_time);
2888
2889 if (lck == NULL) {
2890 DEBUG(0, ("open_file_ntcreate: Could not get share "
2891 "mode lock for %s\n",
2892 smb_fname_str_dbg(smb_fname)));
2893 fd_close(fsp);
2894 return NT_STATUS_SHARING_VIOLATION;
2895 }
2896
2897 /* Get the types we need to examine. */
2898 if (!validate_oplock_types(lck)) {
2899 smb_panic("validate_oplock_types failed");
2900 }
2901
2902 if (has_delete_on_close(lck, fsp->name_hash)) {
2903 TALLOC_FREE(lck);
2904 fd_close(fsp);
2905 return NT_STATUS_DELETE_PENDING;
2906 }
2907
2908 status = open_mode_check(conn, lck,
2909 access_mask, share_access);
2910
2911 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION) ||
2912 (lck->data->num_share_modes > 0)) {
2913 /*
2914 * This comes from ancient times out of open_mode_check. I
2915 * have no clue whether this is still necessary. I can't think
2916 * of a case where this would actually matter further down in
2917 * this function. I leave it here for further investigation
2918 * :-)
2919 */
2920 file_existed = true;
2921 }
2922
2923 if ((req != NULL) &&
2924 delay_for_oplock(
2925 fsp, oplock_request, lease, lck,
2926 NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION),
2927 create_disposition, first_open_attempt)) {
2928 schedule_defer_open(lck, fsp->file_id, request_time, req);
2929 TALLOC_FREE(lck);
2930 fd_close(fsp);
2931 return NT_STATUS_SHARING_VIOLATION;
2932 }
2933
2934 if (!NT_STATUS_IS_OK(status)) {
2935 uint32_t can_access_mask;
2936 bool can_access = True;
2937
2938 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2939
2940 /* Check if this can be done with the deny_dos and fcb
2941 * calls. */
2942 if (private_flags &
2943 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2944 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2945 if (req == NULL) {
2946 DEBUG(0, ("DOS open without an SMB "
2947 "request!\n"));
2948 TALLOC_FREE(lck);
2949 fd_close(fsp);
2950 return NT_STATUS_INTERNAL_ERROR;
2951 }
2952
2953 /* Use the client requested access mask here,
2954 * not the one we open with. */
2955 status = fcb_or_dos_open(req,
2956 conn,
2957 fsp,
2958 smb_fname,
2959 id,
2960 req->smbpid,
2961 req->vuid,
2962 access_mask,
2963 share_access,
2964 create_options);
2965
2966 if (NT_STATUS_IS_OK(status)) {
2967 TALLOC_FREE(lck);
2968 if (pinfo) {
2969 *pinfo = FILE_WAS_OPENED;
2970 }
2971 return NT_STATUS_OK;
2972 }
2973 }
2974
2975 /*
2976 * This next line is a subtlety we need for
2977 * MS-Access. If a file open will fail due to share
2978 * permissions and also for security (access) reasons,
2979 * we need to return the access failed error, not the
2980 * share error. We can't open the file due to kernel
2981 * oplock deadlock (it's possible we failed above on
2982 * the open_mode_check()) so use a userspace check.
2983 */
2984
2985 if (flags & O_RDWR) {
2986 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2987 } else if (flags & O_WRONLY) {
2988 can_access_mask = FILE_WRITE_DATA;
2989 } else {
2990 can_access_mask = FILE_READ_DATA;
2991 }
2992
2993 if (((can_access_mask & FILE_WRITE_DATA) &&
2994 !CAN_WRITE(conn)) ||
2995 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2996 smb_fname,
2997 false,
2998 can_access_mask))) {
2999 can_access = False;
3000 }
3001
3002 /*
3003 * If we're returning a share violation, ensure we
3004 * cope with the braindead 1 second delay (SMB1 only).
3005 */
3006
3007 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
3008 !conn->sconn->using_smb2 &&
3009 lp_defer_sharing_violations()) {
3010 struct timeval timeout;
3011 struct deferred_open_record state;
3012 int timeout_usecs;
3013
3014 /* this is a hack to speed up torture tests
3015 in 'make test' */
3016 timeout_usecs = lp_parm_int(SNUM(conn),
3017 "smbd","sharedelay",
3018 SHARING_VIOLATION_USEC_WAIT);
3019
3020 /* This is a relative time, added to the absolute
3021 request_time value to get the absolute timeout time.
3022 Note that if this is the second or greater time we enter
3023 this codepath for this particular request mid then
3024 request_time is left as the absolute time of the *first*
3025 time this request mid was processed. This is what allows
3026 the request to eventually time out. */
3027
3028 timeout = timeval_set(0, timeout_usecs);
3029
3030 /* Nothing actually uses state.delayed_for_oplocks
3031 but it's handy to differentiate in debug messages
3032 between a 30 second delay due to oplock break, and
3033 a 1 second delay for share mode conflicts. */
3034
3035 state.delayed_for_oplocks = False;
3036 state.async_open = false;
3037 state.id = id;
3038
3039 if ((req != NULL)
3040 && !request_timed_out(request_time,
3041 timeout)) {
3042 defer_open(lck, request_time, timeout,
3043 req, &state);
3044 }
3045 }
3046
3047 TALLOC_FREE(lck);
3048 fd_close(fsp);
3049 if (can_access) {
3050 /*
3051 * We have detected a sharing violation here
3052 * so return the correct error code
3053 */
3054 status = NT_STATUS_SHARING_VIOLATION;
3055 } else {
3056 status = NT_STATUS_ACCESS_DENIED;
3057 }
3058 return status;
3059 }
3060
3061 /* Should we atomically (to the client at least) truncate ? */
3062 if ((!new_file_created) &&
3063 (flags2 & O_TRUNC) &&
3064 (!S_ISFIFO(fsp->fsp_name->st.st_ex_mode))) {
3065 int ret;
3066
3067 ret = vfs_set_filelen(fsp, 0);
3068 if (ret != 0) {
3069 status = map_nt_error_from_unix(errno);
3070 TALLOC_FREE(lck);
3071 fd_close(fsp);
3072 return status;
3073 }
3074 }
3075
3076 /*
3077 * We have the share entry *locked*.....
3078 */
3079
3080 /* Delete streams if create_disposition requires it */
3081 if (!new_file_created && clear_ads(create_disposition) &&
3082 !is_ntfs_stream_smb_fname(smb_fname)) {
3083 status = delete_all_streams(conn, smb_fname->base_name);
3084 if (!NT_STATUS_IS_OK(status)) {
3085 TALLOC_FREE(lck);
3086 fd_close(fsp);
3087 return status;
3088 }
3089 }
3090
3091 /* note that we ignore failure for the following. It is
3092 basically a hack for NFS, and NFS will never set one of
3093 these only read them. Nobody but Samba can ever set a deny
3094 mode and we have already checked our more authoritative
3095 locking database for permission to set this deny mode. If
3096 the kernel refuses the operations then the kernel is wrong.
3097 note that GPFS supports it as well - jmcd */
3098
3099 if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
3100 int ret_flock;
3101 /*
3102 * Beware: streams implementing VFS modules may
3103 * implement streams in a way that fsp will have the
3104 * basefile open in the fsp fd, so lacking a distinct
3105 * fd for the stream kernel_flock will apply on the
3106 * basefile which is wrong. The actual check is
3107 * deffered to the VFS module implementing the
3108 * kernel_flock call.
3109 */
3110 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
3111 if(ret_flock == -1 ){
3112
3113 TALLOC_FREE(lck);
3114 fd_close(fsp);
3115
3116 return NT_STATUS_SHARING_VIOLATION;
3117 }
3118
3119 fsp->kernel_share_modes_taken = true;
3120 }
3121
3122 /*
3123 * At this point onwards, we can guarantee that the share entry
3124 * is locked, whether we created the file or not, and that the
3125 * deny mode is compatible with all current opens.
3126 */
3127
3128 /*
3129 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3130 * but we don't have to store this - just ignore it on access check.
3131 */
3132 if (conn->sconn->using_smb2) {
3133 /*
3134 * SMB2 doesn't return it (according to Microsoft tests).
3135 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
3136 * File created with access = 0x7 (Read, Write, Delete)
3137 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
3138 */
3139 fsp->access_mask = access_mask;
3140 } else {
3141 /* But SMB1 does. */
3142 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3143 }
3144
3145 if (file_existed) {
3146 /*
3147 * stat opens on existing files don't get oplocks.
3148 * They can get leases.
3149 *
3150 * Note that we check for stat open on the *open_access_mask*,
3151 * i.e. the access mask we actually used to do the open,
3152 * not the one the client asked for (which is in
3153 * fsp->access_mask). This is due to the fact that
3154 * FILE_OVERWRITE and FILE_OVERWRITE_IF add in O_TRUNC,
3155 * which adds FILE_WRITE_DATA to open_access_mask.
3156 */
3157 if (is_stat_open(open_access_mask) && lease == NULL) {
3158 oplock_request = NO_OPLOCK;
3159 }
3160 }
3161
3162 if (new_file_created) {
3163 info = FILE_WAS_CREATED;
3164 } else {
3165 if (flags2 & O_TRUNC) {
3166 info = FILE_WAS_OVERWRITTEN;
3167 } else {
3168 info = FILE_WAS_OPENED;
3169 }
3170 }
3171
3172 if (pinfo) {
3173 *pinfo = info;
3174 }
3175
3176 /*
3177 * Setup the oplock info in both the shared memory and
3178 * file structs.
3179 */
3180 status = grant_fsp_oplock_type(req, fsp, lck, oplock_request, lease);
3181 if (!NT_STATUS_IS_OK(status)) {
3182 TALLOC_FREE(lck);
3183 fd_close(fsp);
3184 return status;
3185 }
3186
3187 /* Handle strange delete on close create semantics. */
3188 if (create_options & FILE_DELETE_ON_CLOSE) {
3189
3190 status = can_set_delete_on_close(fsp, new_dos_attributes);
3191
3192 if (!NT_STATUS_IS_OK(status)) {
3193 /* Remember to delete the mode we just added. */
3194 del_share_mode(lck, fsp);
3195 TALLOC_FREE(lck);
3196 fd_close(fsp);
3197 return status;
3198 }
3199 /* Note that here we set the *inital* delete on close flag,
3200 not the regular one. The magic gets handled in close. */
3201 fsp->initial_delete_on_close = True;
3202 }
3203
3204 if (info != FILE_WAS_OPENED) {
3205 /* Overwritten files should be initially set as archive */
3206 if ((info == FILE_WAS_OVERWRITTEN && lp_map_archive(SNUM(conn))) ||
3207 lp_store_dos_attributes(SNUM(conn))) {
3208 if (!posix_open) {
3209 if (file_set_dosmode(conn, smb_fname,
3210 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
3211 parent_dir, true) == 0) {
3212 unx_mode = smb_fname->st.st_ex_mode;
3213 }
3214 }
3215 }
3216 }
3217
3218 /* Determine sparse flag. */
3219 if (posix_open) {
3220 /* POSIX opens are sparse by default. */
3221 fsp->is_sparse = true;
3222 } else {
3223 fsp->is_sparse = (file_existed &&
3224 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
3225 }
3226
3227 /*
3228 * Take care of inherited ACLs on created files - if default ACL not
3229 * selected.
3230 */
3231
3232 if (!posix_open && new_file_created && !def_acl) {
3233
3234 int saved_errno = errno; /* We might get ENOSYS in the next
3235 * call.. */
3236
3237 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
3238 errno == ENOSYS) {
3239 errno = saved_errno; /* Ignore ENOSYS */
3240 }
3241
3242 } else if (new_unx_mode) {
3243
3244 int ret = -1;
3245
3246 /* Attributes need changing. File already existed. */
3247
3248 {
3249 int saved_errno = errno; /* We might get ENOSYS in the
3250 * next call.. */
3251 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
3252
3253 if (ret == -1 && errno == ENOSYS) {
3254 errno = saved_errno; /* Ignore ENOSYS */
3255 } else {
3256 DEBUG(5, ("open_file_ntcreate: reset "
3257 "attributes of file %s to 0%o\n",
3258 smb_fname_str_dbg(smb_fname),
3259 (unsigned int)new_unx_mode));
3260 ret = 0; /* Don't do the fchmod below. */
3261 }
3262 }
3263
3264 if ((ret == -1) &&
3265 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
3266 DEBUG(5, ("open_file_ntcreate: failed to reset "
3267 "attributes of file %s to 0%o\n",
3268 smb_fname_str_dbg(smb_fname),
3269 (unsigned int)new_unx_mode));
3270 }
3271
3272 {
3273 /*
3274 * Deal with other opens having a modified write time.
3275 */
3276 struct timespec write_time = get_share_mode_write_time(lck);
3277
3278 if (!null_timespec(write_time)) {
3279 update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
3280 }
3281 }
3282
3283 TALLOC_FREE(lck);
3284
3285 return NT_STATUS_OK;
3286}
3287
3288static NTSTATUS mkdir_internal(connection_struct *conn,
3289 struct smb_filename *smb_dname,
3290 uint32_t file_attributes)
3291{
3292 mode_t mode;
3293 char *parent_dir = NULL;
3294 NTSTATUS status;
3295 bool posix_open = false;
3296 bool need_re_stat = false;
3297 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
3298
3299 if (!CAN_WRITE(conn) || (access_mask & ~(conn->share_access))) {
3300 DEBUG(5,("mkdir_internal: failing share access "
3301 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
3302 return NT_STATUS_ACCESS_DENIED;
3303 }
3304
3305 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
3306 NULL)) {
3307 return NT_STATUS_NO_MEMORY;
3308 }
3309
3310 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3311 posix_open = true;
3312 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
3313 } else {
3314 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
3315 }
3316
3317 status = check_parent_access(conn,
3318 smb_dname,
3319 access_mask);
3320 if(!NT_STATUS_IS_OK(status)) {
3321 DEBUG(5,("mkdir_internal: check_parent_access "
3322 "on directory %s for path %s returned %s\n",
3323 parent_dir,
3324 smb_dname->base_name,
3325 nt_errstr(status) ));
3326 return status;
3327 }
3328
3329 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
3330 return map_nt_error_from_unix(errno);
3331 }
3332
3333 /* Ensure we're checking for a symlink here.... */
3334 /* We don't want to get caught by a symlink racer. */
3335
3336 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
3337 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3338 smb_fname_str_dbg(smb_dname), strerror(errno)));
3339 return map_nt_error_from_unix(errno);
3340 }
3341
3342 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
3343 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
3344 smb_fname_str_dbg(smb_dname)));
3345 return NT_STATUS_NOT_A_DIRECTORY;
3346 }
3347
3348 if (lp_store_dos_attributes(SNUM(conn))) {
3349 if (!posix_open) {
3350 file_set_dosmode(conn, smb_dname,
3351 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
3352 parent_dir, true);
3353 }
3354 }
3355
3356 if (lp_inherit_permissions(SNUM(conn))) {
3357 inherit_access_posix_acl(conn, parent_dir,
3358 smb_dname->base_name, mode);
3359 need_re_stat = true;
3360 }
3361
3362 if (!posix_open) {
3363 /*
3364 * Check if high bits should have been set,
3365 * then (if bits are missing): add them.
3366 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
3367 * dir.
3368 */
3369 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
3370 (mode & ~smb_dname->st.st_ex_mode)) {
3371 SMB_VFS_CHMOD(conn, smb_dname->base_name,
3372 (smb_dname->st.st_ex_mode |
3373 (mode & ~smb_dname->st.st_ex_mode)));
3374 need_re_stat = true;
3375 }
3376 }
3377
3378 /* Change the owner if required. */
3379 if (lp_inherit_owner(SNUM(conn))) {
3380 change_dir_owner_to_parent(conn, parent_dir,
3381 smb_dname->base_name,
3382 &smb_dname->st);
3383 need_re_stat = true;
3384 }
3385
3386 if (need_re_stat) {
3387 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
3388 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3389 smb_fname_str_dbg(smb_dname), strerror(errno)));
3390 return map_nt_error_from_unix(errno);
3391 }
3392 }
3393
3394 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
3395 smb_dname->base_name);
3396
3397 return NT_STATUS_OK;
3398}
3399
3400/****************************************************************************
3401 Open a directory from an NT SMB call.
3402****************************************************************************/
3403
3404static NTSTATUS open_directory(connection_struct *conn,
3405 struct smb_request *req,
3406 struct smb_filename *smb_dname,
3407 uint32_t access_mask,
3408 uint32_t share_access,
3409 uint32_t create_disposition,
3410 uint32_t create_options,
3411 uint32_t file_attributes,
3412 int *pinfo,
3413 files_struct **result)
3414{
3415 files_struct *fsp = NULL;
3416 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
3417 struct share_mode_lock *lck = NULL;
3418 NTSTATUS status;
3419 struct timespec mtimespec;
3420 int info = 0;
3421 bool ok;
3422
3423 if (is_ntfs_stream_smb_fname(smb_dname)) {
3424 DEBUG(2, ("open_directory: %s is a stream name!\n",
3425 smb_fname_str_dbg(smb_dname)));
3426 return NT_STATUS_NOT_A_DIRECTORY;
3427 }
3428
3429 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
3430 /* Ensure we have a directory attribute. */
3431 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
3432 }
3433
3434 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
3435 "share_access = 0x%x create_options = 0x%x, "
3436 "create_disposition = 0x%x, file_attributes = 0x%x\n",
3437 smb_fname_str_dbg(smb_dname),
3438 (unsigned int)access_mask,
3439 (unsigned int)share_access,
3440 (unsigned int)create_options,
3441 (unsigned int)create_disposition,
3442 (unsigned int)file_attributes));
3443
3444 status = smbd_calculate_access_mask(conn, smb_dname, false,
3445 access_mask, &access_mask);
3446 if (!NT_STATUS_IS_OK(status)) {
3447 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
3448 "on file %s returned %s\n",
3449 smb_fname_str_dbg(smb_dname),
3450 nt_errstr(status)));
3451 return status;
3452 }
3453
3454 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3455 !security_token_has_privilege(get_current_nttok(conn),
3456 SEC_PRIV_SECURITY)) {
3457 DEBUG(10, ("open_directory: open on %s "
3458 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3459 smb_fname_str_dbg(smb_dname)));
3460 return NT_STATUS_PRIVILEGE_NOT_HELD;
3461 }
3462
3463 switch( create_disposition ) {
3464 case FILE_OPEN:
3465
3466 if (!dir_existed) {
3467 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3468 }
3469
3470 info = FILE_WAS_OPENED;
3471 break;
3472
3473 case FILE_CREATE:
3474
3475 /* If directory exists error. If directory doesn't
3476 * exist create. */
3477
3478 if (dir_existed) {
3479 status = NT_STATUS_OBJECT_NAME_COLLISION;
3480 DEBUG(2, ("open_directory: unable to create "
3481 "%s. Error was %s\n",
3482 smb_fname_str_dbg(smb_dname),
3483 nt_errstr(status)));
3484 return status;
3485 }
3486
3487 status = mkdir_internal(conn, smb_dname,
3488 file_attributes);
3489
3490 if (!NT_STATUS_IS_OK(status)) {
3491 DEBUG(2, ("open_directory: unable to create "
3492 "%s. Error was %s\n",
3493 smb_fname_str_dbg(smb_dname),
3494 nt_errstr(status)));
3495 return status;
3496 }
3497
3498 info = FILE_WAS_CREATED;
3499 break;
3500
3501 case FILE_OPEN_IF:
3502 /*
3503 * If directory exists open. If directory doesn't
3504 * exist create.
3505 */
3506
3507 if (dir_existed) {
3508 status = NT_STATUS_OK;
3509 info = FILE_WAS_OPENED;
3510 } else {
3511 status = mkdir_internal(conn, smb_dname,
3512 file_attributes);
3513
3514 if (NT_STATUS_IS_OK(status)) {
3515 info = FILE_WAS_CREATED;
3516 } else {
3517 /* Cope with create race. */
3518 if (!NT_STATUS_EQUAL(status,
3519 NT_STATUS_OBJECT_NAME_COLLISION)) {
3520 DEBUG(2, ("open_directory: unable to create "
3521 "%s. Error was %s\n",
3522 smb_fname_str_dbg(smb_dname),
3523 nt_errstr(status)));
3524 return status;
3525 }
3526
3527 /*
3528 * If mkdir_internal() returned
3529 * NT_STATUS_OBJECT_NAME_COLLISION
3530 * we still must lstat the path.
3531 */
3532
3533 if (SMB_VFS_LSTAT(conn, smb_dname)
3534 == -1) {
3535 DEBUG(2, ("Could not stat "
3536 "directory '%s' just "
3537 "opened: %s\n",
3538 smb_fname_str_dbg(
3539 smb_dname),
3540 strerror(errno)));
3541 return map_nt_error_from_unix(
3542 errno);
3543 }
3544
3545 info = FILE_WAS_OPENED;
3546 }
3547 }
3548
3549 break;
3550
3551 case FILE_SUPERSEDE:
3552 case FILE_OVERWRITE:
3553 case FILE_OVERWRITE_IF:
3554 default:
3555 DEBUG(5,("open_directory: invalid create_disposition "
3556 "0x%x for directory %s\n",
3557 (unsigned int)create_disposition,
3558 smb_fname_str_dbg(smb_dname)));
3559 return NT_STATUS_INVALID_PARAMETER;
3560 }
3561
3562 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
3563 DEBUG(5,("open_directory: %s is not a directory !\n",
3564 smb_fname_str_dbg(smb_dname)));
3565 return NT_STATUS_NOT_A_DIRECTORY;
3566 }
3567
3568 if (info == FILE_WAS_OPENED) {
3569 status = smbd_check_access_rights(conn,
3570 smb_dname,
3571 false,
3572 access_mask);
3573 if (!NT_STATUS_IS_OK(status)) {
3574 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3575 "file %s failed with %s\n",
3576 smb_fname_str_dbg(smb_dname),
3577 nt_errstr(status)));
3578 return status;
3579 }
3580 }
3581
3582 status = file_new(req, conn, &fsp);
3583 if(!NT_STATUS_IS_OK(status)) {
3584 return status;
3585 }
3586
3587 /*
3588 * Setup the files_struct for it.
3589 */
3590
3591 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
3592 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
3593 fsp->file_pid = req ? req->smbpid : 0;
3594 fsp->can_lock = False;
3595 fsp->can_read = False;
3596 fsp->can_write = False;
3597
3598 fsp->share_access = share_access;
3599 fsp->fh->private_options = 0;
3600 /*
3601 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3602 */
3603 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3604 fsp->print_file = NULL;
3605 fsp->modified = False;
3606 fsp->oplock_type = NO_OPLOCK;
3607 fsp->sent_oplock_break = NO_BREAK_SENT;
3608 fsp->is_directory = True;
3609 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3610 fsp->posix_flags |= FSP_POSIX_FLAGS_ALL;
3611 }
3612 status = fsp_set_smb_fname(fsp, smb_dname);
3613 if (!NT_STATUS_IS_OK(status)) {
3614 file_free(req, fsp);
3615 return status;
3616 }
3617
3618 /* Don't store old timestamps for directory
3619 handles in the internal database. We don't
3620 update them in there if new objects
3621 are creaded in the directory. Currently
3622 we only update timestamps on file writes.
3623 See bug #9870.
3624 */
3625 ZERO_STRUCT(mtimespec);
3626
3627 if (access_mask & (FILE_LIST_DIRECTORY|
3628 FILE_ADD_FILE|
3629 FILE_ADD_SUBDIRECTORY|
3630 FILE_TRAVERSE|
3631 DELETE_ACCESS|
3632 FILE_DELETE_CHILD)) {
3633#ifdef O_DIRECTORY
3634 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3635#else
3636 /* POSIX allows us to open a directory with O_RDONLY. */
3637 status = fd_open(conn, fsp, O_RDONLY, 0);
3638#endif
3639 if (!NT_STATUS_IS_OK(status)) {
3640 DEBUG(5, ("open_directory: Could not open fd for "
3641 "%s (%s)\n",
3642 smb_fname_str_dbg(smb_dname),
3643 nt_errstr(status)));
3644 file_free(req, fsp);
3645 return status;
3646 }
3647 } else {
3648 fsp->fh->fd = -1;
3649 DEBUG(10, ("Not opening Directory %s\n",
3650 smb_fname_str_dbg(smb_dname)));
3651 }
3652
3653 status = vfs_stat_fsp(fsp);
3654 if (!NT_STATUS_IS_OK(status)) {
3655 fd_close(fsp);
3656 file_free(req, fsp);
3657 return status;
3658 }
3659
3660 if(!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3661 DEBUG(5,("open_directory: %s is not a directory !\n",
3662 smb_fname_str_dbg(smb_dname)));
3663 fd_close(fsp);
3664 file_free(req, fsp);
3665 return NT_STATUS_NOT_A_DIRECTORY;
3666 }
3667
3668 /* Ensure there was no race condition. We need to check
3669 * dev/inode but not permissions, as these can change
3670 * legitimately */
3671 if (!check_same_dev_ino(&smb_dname->st, &fsp->fsp_name->st)) {
3672 DEBUG(5,("open_directory: stat struct differs for "
3673 "directory %s.\n",
3674 smb_fname_str_dbg(smb_dname)));
3675 fd_close(fsp);
3676 file_free(req, fsp);
3677 return NT_STATUS_ACCESS_DENIED;
3678 }
3679
3680 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3681 conn->connectpath, smb_dname,
3682 &mtimespec);
3683
3684 if (lck == NULL) {
3685 DEBUG(0, ("open_directory: Could not get share mode lock for "
3686 "%s\n", smb_fname_str_dbg(smb_dname)));
3687 fd_close(fsp);
3688 file_free(req, fsp);
3689 return NT_STATUS_SHARING_VIOLATION;
3690 }
3691
3692 if (has_delete_on_close(lck, fsp->name_hash)) {
3693 TALLOC_FREE(lck);
3694 fd_close(fsp);
3695 file_free(req, fsp);
3696 return NT_STATUS_DELETE_PENDING;
3697 }
3698
3699 status = open_mode_check(conn, lck,
3700 access_mask, share_access);
3701
3702 if (!NT_STATUS_IS_OK(status)) {
3703 TALLOC_FREE(lck);
3704 fd_close(fsp);
3705 file_free(req, fsp);
3706 return status;
3707 }
3708
3709 ok = set_share_mode(lck, fsp, get_current_uid(conn),
3710 req ? req->mid : 0, NO_OPLOCK,
3711 UINT32_MAX);
3712 if (!ok) {
3713 TALLOC_FREE(lck);
3714 fd_close(fsp);
3715 file_free(req, fsp);
3716 return NT_STATUS_NO_MEMORY;
3717 }
3718
3719 /* For directories the delete on close bit at open time seems
3720 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3721 if (create_options & FILE_DELETE_ON_CLOSE) {
3722 status = can_set_delete_on_close(fsp, 0);
3723 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3724 del_share_mode(lck, fsp);
3725 TALLOC_FREE(lck);
3726 fd_close(fsp);
3727 file_free(req, fsp);
3728 return status;
3729 }
3730
3731 if (NT_STATUS_IS_OK(status)) {
3732 /* Note that here we set the *inital* delete on close flag,
3733 not the regular one. The magic gets handled in close. */
3734 fsp->initial_delete_on_close = True;
3735 }
3736 }
3737
3738 {
3739 /*
3740 * Deal with other opens having a modified write time. Is this
3741 * possible for directories?
3742 */
3743 struct timespec write_time = get_share_mode_write_time(lck);
3744
3745 if (!null_timespec(write_time)) {
3746 update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
3747 }
3748 }
3749
3750 TALLOC_FREE(lck);
3751
3752 if (pinfo) {
3753 *pinfo = info;
3754 }
3755
3756 *result = fsp;
3757 return NT_STATUS_OK;
3758}
3759
3760NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3761 struct smb_filename *smb_dname)
3762{
3763 NTSTATUS status;
3764 files_struct *fsp;
3765
3766 status = SMB_VFS_CREATE_FILE(
3767 conn, /* conn */
3768 req, /* req */
3769 0, /* root_dir_fid */
3770 smb_dname, /* fname */
3771 FILE_READ_ATTRIBUTES, /* access_mask */
3772 FILE_SHARE_NONE, /* share_access */
3773 FILE_CREATE, /* create_disposition*/
3774 FILE_DIRECTORY_FILE, /* create_options */
3775 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3776 0, /* oplock_request */
3777 NULL, /* lease */
3778 0, /* allocation_size */
3779 0, /* private_flags */
3780 NULL, /* sd */
3781 NULL, /* ea_list */
3782 &fsp, /* result */
3783 NULL, /* pinfo */
3784 NULL, NULL); /* create context */
3785
3786 if (NT_STATUS_IS_OK(status)) {
3787 close_file(req, fsp, NORMAL_CLOSE);
3788 }
3789
3790 return status;
3791}
3792
3793/****************************************************************************
3794 Receive notification that one of our open files has been renamed by another
3795 smbd process.
3796****************************************************************************/
3797
3798void msg_file_was_renamed(struct messaging_context *msg,
3799 void *private_data,
3800 uint32_t msg_type,
3801 struct server_id server_id,
3802 DATA_BLOB *data)
3803{
3804 files_struct *fsp;
3805 char *frm = (char *)data->data;
3806 struct file_id id;
3807 const char *sharepath;
3808 const char *base_name;
3809 const char *stream_name;
3810 struct smb_filename *smb_fname = NULL;
3811 size_t sp_len, bn_len;
3812 NTSTATUS status;
3813 struct smbd_server_connection *sconn =
3814 talloc_get_type_abort(private_data,
3815 struct smbd_server_connection);
3816
3817 if (data->data == NULL
3818 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3819 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3820 (int)data->length));
3821 return;
3822 }
3823
3824 /* Unpack the message. */
3825 pull_file_id_24(frm, &id);
3826 sharepath = &frm[24];
3827 sp_len = strlen(sharepath);
3828 base_name = sharepath + sp_len + 1;
3829 bn_len = strlen(base_name);
3830 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3831
3832 /* stream_name must always be NULL if there is no stream. */
3833 if (stream_name[0] == '\0') {
3834 stream_name = NULL;
3835 }
3836
3837 smb_fname = synthetic_smb_fname(talloc_tos(), base_name,
3838 stream_name, NULL);
3839 if (smb_fname == NULL) {
3840 return;
3841 }
3842
3843 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3844 "file_id %s\n",
3845 sharepath, smb_fname_str_dbg(smb_fname),
3846 file_id_string_tos(&id)));
3847
3848 for(fsp = file_find_di_first(sconn, id); fsp;
3849 fsp = file_find_di_next(fsp)) {
3850 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3851
3852 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3853 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3854 smb_fname_str_dbg(smb_fname)));
3855 status = fsp_set_smb_fname(fsp, smb_fname);
3856 if (!NT_STATUS_IS_OK(status)) {
3857 goto out;
3858 }
3859 } else {
3860 /* TODO. JRA. */
3861 /* Now we have the complete path we can work out if this is
3862 actually within this share and adjust newname accordingly. */
3863 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3864 "not sharepath %s) "
3865 "%s from %s -> %s\n",
3866 fsp->conn->connectpath,
3867 sharepath,
3868 fsp_fnum_dbg(fsp),
3869 fsp_str_dbg(fsp),
3870 smb_fname_str_dbg(smb_fname)));
3871 }
3872 }
3873 out:
3874 TALLOC_FREE(smb_fname);
3875 return;
3876}
3877
3878/*
3879 * If a main file is opened for delete, all streams need to be checked for
3880 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3881 * If that works, delete them all by setting the delete on close and close.
3882 */
3883
3884NTSTATUS open_streams_for_delete(connection_struct *conn,
3885 const char *fname)
3886{
3887 struct stream_struct *stream_info = NULL;
3888 files_struct **streams = NULL;
3889 int i;
3890 unsigned int num_streams = 0;
3891 TALLOC_CTX *frame = talloc_stackframe();
3892 NTSTATUS status;
3893 bool saved_posix_pathnames;
3894
3895 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3896 &num_streams, &stream_info);
3897
3898 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3899 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3900 DEBUG(10, ("no streams around\n"));
3901 TALLOC_FREE(frame);
3902 return NT_STATUS_OK;
3903 }
3904
3905 if (!NT_STATUS_IS_OK(status)) {
3906 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3907 nt_errstr(status)));
3908 goto fail;
3909 }
3910
3911 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3912 num_streams));
3913
3914 if (num_streams == 0) {
3915 TALLOC_FREE(frame);
3916 return NT_STATUS_OK;
3917 }
3918
3919 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3920 if (streams == NULL) {
3921 DEBUG(0, ("talloc failed\n"));
3922 status = NT_STATUS_NO_MEMORY;
3923 goto fail;
3924 }
3925
3926 /*
3927 * Any stream names *must* be treated as Windows
3928 * pathnames, even if we're using UNIX extensions.
3929 */
3930
3931 saved_posix_pathnames = lp_set_posix_pathnames(false);
3932
3933 for (i=0; i<num_streams; i++) {
3934 struct smb_filename *smb_fname;
3935
3936 if (strequal(stream_info[i].name, "::$DATA")) {
3937 streams[i] = NULL;
3938 continue;
3939 }
3940
3941 smb_fname = synthetic_smb_fname(
3942 talloc_tos(), fname, stream_info[i].name, NULL);
3943 if (smb_fname == NULL) {
3944 status = NT_STATUS_NO_MEMORY;
3945 goto fail;
3946 }
3947
3948 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3949 DEBUG(10, ("Unable to stat stream: %s\n",
3950 smb_fname_str_dbg(smb_fname)));
3951 }
3952
3953 status = SMB_VFS_CREATE_FILE(
3954 conn, /* conn */
3955 NULL, /* req */
3956 0, /* root_dir_fid */
3957 smb_fname, /* fname */
3958 DELETE_ACCESS, /* access_mask */
3959 (FILE_SHARE_READ | /* share_access */
3960 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3961 FILE_OPEN, /* create_disposition*/
3962 0, /* create_options */
3963 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3964 0, /* oplock_request */
3965 NULL, /* lease */
3966 0, /* allocation_size */
3967 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3968 NULL, /* sd */
3969 NULL, /* ea_list */
3970 &streams[i], /* result */
3971 NULL, /* pinfo */
3972 NULL, NULL); /* create context */
3973
3974 if (!NT_STATUS_IS_OK(status)) {
3975 DEBUG(10, ("Could not open stream %s: %s\n",
3976 smb_fname_str_dbg(smb_fname),
3977 nt_errstr(status)));
3978
3979 TALLOC_FREE(smb_fname);
3980 break;
3981 }
3982 TALLOC_FREE(smb_fname);
3983 }
3984
3985 /*
3986 * don't touch the variable "status" beyond this point :-)
3987 */
3988
3989 for (i -= 1 ; i >= 0; i--) {
3990 if (streams[i] == NULL) {
3991 continue;
3992 }
3993
3994 DEBUG(10, ("Closing stream # %d, %s\n", i,
3995 fsp_str_dbg(streams[i])));
3996 close_file(NULL, streams[i], NORMAL_CLOSE);
3997 }
3998
3999 fail:
4000
4001 (void)lp_set_posix_pathnames(saved_posix_pathnames);
4002 TALLOC_FREE(frame);
4003 return status;
4004}
4005
4006/*********************************************************************
4007 Create a default ACL by inheriting from the parent. If no inheritance
4008 from the parent available, don't set anything. This will leave the actual
4009 permissions the new file or directory already got from the filesystem
4010 as the NT ACL when read.
4011*********************************************************************/
4012
4013static NTSTATUS inherit_new_acl(files_struct *fsp)
4014{
4015 TALLOC_CTX *frame = talloc_stackframe();
4016 char *parent_name = NULL;
4017 struct security_descriptor *parent_desc = NULL;
4018 NTSTATUS status = NT_STATUS_OK;
4019 struct security_descriptor *psd = NULL;
4020 const struct dom_sid *owner_sid = NULL;
4021 const struct dom_sid *group_sid = NULL;
4022 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
4023 struct security_token *token = fsp->conn->session_info->security_token;
4024 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
4025 bool inheritable_components = false;
4026 bool try_builtin_administrators = false;
4027 const struct dom_sid *BA_U_sid = NULL;
4028 const struct dom_sid *BA_G_sid = NULL;
4029 bool try_system = false;
4030 const struct dom_sid *SY_U_sid = NULL;
4031 const struct dom_sid *SY_G_sid = NULL;
4032 size_t size = 0;
4033
4034 if (!parent_dirname(frame, fsp->fsp_name->base_name, &parent_name, NULL)) {
4035 TALLOC_FREE(frame);
4036 return NT_STATUS_NO_MEMORY;
4037 }
4038
4039 status = SMB_VFS_GET_NT_ACL(fsp->conn,
4040 parent_name,
4041 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
4042 frame,
4043 &parent_desc);
4044 if (!NT_STATUS_IS_OK(status)) {
4045 TALLOC_FREE(frame);
4046 return status;
4047 }
4048
4049 inheritable_components = sd_has_inheritable_components(parent_desc,
4050 fsp->is_directory);
4051
4052 if (!inheritable_components && !inherit_owner) {
4053 TALLOC_FREE(frame);
4054 /* Nothing to inherit and not setting owner. */
4055 return NT_STATUS_OK;
4056 }
4057
4058 /* Create an inherited descriptor from the parent. */
4059
4060 if (DEBUGLEVEL >= 10) {
4061 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
4062 fsp_str_dbg(fsp) ));
4063 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
4064 }
4065
4066 /* Inherit from parent descriptor if "inherit owner" set. */
4067 if (inherit_owner) {
4068 owner_sid = parent_desc->owner_sid;
4069 group_sid = parent_desc->group_sid;
4070 }
4071
4072 if (owner_sid == NULL) {
4073 if (security_token_has_builtin_administrators(token)) {
4074 try_builtin_administrators = true;
4075 } else if (security_token_is_system(token)) {
4076 try_builtin_administrators = true;
4077 try_system = true;
4078 }
4079 }
4080
4081 if (group_sid == NULL &&
4082 token->num_sids == PRIMARY_GROUP_SID_INDEX)
4083 {
4084 if (security_token_is_system(token)) {
4085 try_builtin_administrators = true;
4086 try_system = true;
4087 }
4088 }
4089
4090 if (try_builtin_administrators) {
4091 struct unixid ids;
4092 bool ok;
4093
4094 ZERO_STRUCT(ids);
4095 ok = sids_to_unixids(&global_sid_Builtin_Administrators, 1, &ids);
4096 if (ok) {
4097 switch (ids.type) {
4098 case ID_TYPE_BOTH:
4099 BA_U_sid = &global_sid_Builtin_Administrators;
4100 BA_G_sid = &global_sid_Builtin_Administrators;
4101 break;
4102 case ID_TYPE_UID:
4103 BA_U_sid = &global_sid_Builtin_Administrators;
4104 break;
4105 case ID_TYPE_GID:
4106 BA_G_sid = &global_sid_Builtin_Administrators;
4107 break;
4108 default:
4109 break;
4110 }
4111 }
4112 }
4113
4114 if (try_system) {
4115 struct unixid ids;
4116 bool ok;
4117
4118 ZERO_STRUCT(ids);
4119 ok = sids_to_unixids(&global_sid_System, 1, &ids);
4120 if (ok) {
4121 switch (ids.type) {
4122 case ID_TYPE_BOTH:
4123 SY_U_sid = &global_sid_System;
4124 SY_G_sid = &global_sid_System;
4125 break;
4126 case ID_TYPE_UID:
4127 SY_U_sid = &global_sid_System;
4128 break;
4129 case ID_TYPE_GID:
4130 SY_G_sid = &global_sid_System;
4131 break;
4132 default:
4133 break;
4134 }
4135 }
4136 }
4137
4138 if (owner_sid == NULL) {
4139 owner_sid = BA_U_sid;
4140 }
4141
4142 if (owner_sid == NULL) {
4143 owner_sid = SY_U_sid;
4144 }
4145
4146 if (group_sid == NULL) {
4147 group_sid = SY_G_sid;
4148 }
4149
4150 if (try_system && group_sid == NULL) {
4151 group_sid = BA_G_sid;
4152 }
4153
4154 if (owner_sid == NULL) {
4155 owner_sid = &token->sids[PRIMARY_USER_SID_INDEX];
4156 }
4157 if (group_sid == NULL) {
4158 if (token->num_sids == PRIMARY_GROUP_SID_INDEX) {
4159 group_sid = &token->sids[PRIMARY_USER_SID_INDEX];
4160 } else {
4161 group_sid = &token->sids[PRIMARY_GROUP_SID_INDEX];
4162 }
4163 }
4164
4165 status = se_create_child_secdesc(frame,
4166 &psd,
4167 &size,
4168 parent_desc,
4169 owner_sid,
4170 group_sid,
4171 fsp->is_directory);
4172 if (!NT_STATUS_IS_OK(status)) {
4173 TALLOC_FREE(frame);
4174 return status;
4175 }
4176
4177 /* If inheritable_components == false,
4178 se_create_child_secdesc()
4179 creates a security desriptor with a NULL dacl
4180 entry, but with SEC_DESC_DACL_PRESENT. We need
4181 to remove that flag. */
4182
4183 if (!inheritable_components) {
4184 security_info_sent &= ~SECINFO_DACL;
4185 psd->type &= ~SEC_DESC_DACL_PRESENT;
4186 }
4187
4188 if (DEBUGLEVEL >= 10) {
4189 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
4190 fsp_str_dbg(fsp) ));
4191 NDR_PRINT_DEBUG(security_descriptor, psd);
4192 }
4193
4194 if (inherit_owner) {
4195 /* We need to be root to force this. */
4196 become_root();
4197 }
4198 status = SMB_VFS_FSET_NT_ACL(fsp,
4199 security_info_sent,
4200 psd);
4201 if (inherit_owner) {
4202 unbecome_root();
4203 }
4204 TALLOC_FREE(frame);
4205 return status;
4206}
4207
4208/*
4209 * If we already have a lease, it must match the new file id. [MS-SMB2]
4210 * 3.3.5.9.8 speaks about INVALID_PARAMETER if an already used lease key is
4211 * used for a different file name.
4212 */
4213
4214struct lease_match_state {
4215 /* Input parameters. */
4216 TALLOC_CTX *mem_ctx;
4217 const char *servicepath;
4218 const struct smb_filename *fname;
4219 bool file_existed;
4220 struct file_id id;
4221 /* Return parameters. */
4222 uint32_t num_file_ids;
4223 struct file_id *ids;
4224 NTSTATUS match_status;
4225};
4226
4227/*************************************************************
4228 File doesn't exist but this lease key+guid is already in use.
4229
4230 This is only allowable in the dynamic share case where the
4231 service path must be different.
4232
4233 There is a small race condition here in the multi-connection
4234 case where a client sends two create calls on different connections,
4235 where the file doesn't exist and one smbd creates the leases_db
4236 entry first, but this will get fixed by the multichannel cleanup
4237 when all identical client_guids get handled by a single smbd.
4238**************************************************************/
4239
4240static void lease_match_parser_new_file(
4241 uint32_t num_files,
4242 const struct leases_db_file *files,
4243 struct lease_match_state *state)
4244{
4245 uint32_t i;
4246
4247 for (i = 0; i < num_files; i++) {
4248 const struct leases_db_file *f = &files[i];
4249 if (strequal(state->servicepath, f->servicepath)) {
4250 state->match_status = NT_STATUS_INVALID_PARAMETER;
4251 return;
4252 }
4253 }
4254
4255 /* Dynamic share case. Break leases on all other files. */
4256 state->match_status = leases_db_copy_file_ids(state->mem_ctx,
4257 num_files,
4258 files,
4259 &state->ids);
4260 if (!NT_STATUS_IS_OK(state->match_status)) {
4261 return;
4262 }
4263
4264 state->num_file_ids = num_files;
4265 state->match_status = NT_STATUS_OPLOCK_NOT_GRANTED;
4266 return;
4267}
4268
4269static void lease_match_parser(
4270 uint32_t num_files,
4271 const struct leases_db_file *files,
4272 void *private_data)
4273{
4274 struct lease_match_state *state =
4275 (struct lease_match_state *)private_data;
4276 uint32_t i;
4277
4278 if (!state->file_existed) {
4279 /*
4280 * Deal with name mismatch or
4281 * possible dynamic share case separately
4282 * to make code clearer.
4283 */
4284 lease_match_parser_new_file(num_files,
4285 files,
4286 state);
4287 return;
4288 }
4289
4290 /* File existed. */
4291 state->match_status = NT_STATUS_OK;
4292
4293 for (i = 0; i < num_files; i++) {
4294 const struct leases_db_file *f = &files[i];
4295
4296 /* Everything should be the same. */
4297 if (!file_id_equal(&state->id, &f->id)) {
4298 /* This should catch all dynamic share cases. */
4299 state->match_status = NT_STATUS_OPLOCK_NOT_GRANTED;
4300 break;
4301 }
4302 if (!strequal(f->servicepath, state->servicepath)) {
4303 state->match_status = NT_STATUS_INVALID_PARAMETER;
4304 break;
4305 }
4306 if (!strequal(f->base_name, state->fname->base_name)) {
4307 state->match_status = NT_STATUS_INVALID_PARAMETER;
4308 break;
4309 }
4310 if (!strequal(f->stream_name, state->fname->stream_name)) {
4311 state->match_status = NT_STATUS_INVALID_PARAMETER;
4312 break;
4313 }
4314 }
4315
4316 if (NT_STATUS_IS_OK(state->match_status)) {
4317 /*
4318 * Common case - just opening another handle on a
4319 * file on a non-dynamic share.
4320 */
4321 return;
4322 }
4323
4324 if (NT_STATUS_EQUAL(state->match_status, NT_STATUS_INVALID_PARAMETER)) {
4325 /* Mismatched path. Error back to client. */
4326 return;
4327 }
4328
4329 /*
4330 * File id mismatch. Dynamic share case NT_STATUS_OPLOCK_NOT_GRANTED.
4331 * Don't allow leases.
4332 */
4333
4334 state->match_status = leases_db_copy_file_ids(state->mem_ctx,
4335 num_files,
4336 files,
4337 &state->ids);
4338 if (!NT_STATUS_IS_OK(state->match_status)) {
4339 return;
4340 }
4341
4342 state->num_file_ids = num_files;
4343 state->match_status = NT_STATUS_OPLOCK_NOT_GRANTED;
4344 return;
4345}
4346
4347static NTSTATUS lease_match(connection_struct *conn,
4348 struct smb_request *req,
4349 struct smb2_lease_key *lease_key,
4350 const char *servicepath,
4351 const struct smb_filename *fname,
4352 uint16_t *p_version,
4353 uint16_t *p_epoch)
4354{
4355 struct smbd_server_connection *sconn = req->sconn;
4356 TALLOC_CTX *tos = talloc_tos();
4357 struct lease_match_state state = {
4358 .mem_ctx = tos,
4359 .servicepath = servicepath,
4360 .fname = fname,
4361 .match_status = NT_STATUS_OK
4362 };
4363 uint32_t i;
4364 NTSTATUS status;
4365
4366 state.file_existed = VALID_STAT(fname->st);
4367 if (state.file_existed) {
4368 state.id = vfs_file_id_from_sbuf(conn, &fname->st);
4369 } else {
4370 memset(&state.id, '\0', sizeof(state.id));
4371 }
4372
4373 status = leases_db_parse(&sconn->client->connections->smb2.client.guid,
4374 lease_key, lease_match_parser, &state);
4375 if (!NT_STATUS_IS_OK(status)) {
4376 /*
4377 * Not found or error means okay: We can make the lease pass
4378 */
4379 return NT_STATUS_OK;
4380 }
4381 if (!NT_STATUS_EQUAL(state.match_status, NT_STATUS_OPLOCK_NOT_GRANTED)) {
4382 /*
4383 * Anything but NT_STATUS_OPLOCK_NOT_GRANTED, let the caller
4384 * deal with it.
4385 */
4386 return state.match_status;
4387 }
4388
4389 /* We have to break all existing leases. */
4390 for (i = 0; i < state.num_file_ids; i++) {
4391 struct share_mode_lock *lck;
4392 struct share_mode_data *d;
4393 uint32_t j;
4394
4395 if (file_id_equal(&state.ids[i], &state.id)) {
4396 /* Don't need to break our own file. */
4397 continue;
4398 }
4399
4400 lck = get_existing_share_mode_lock(talloc_tos(), state.ids[i]);
4401 if (lck == NULL) {
4402 /* Race condition - file already closed. */
4403 continue;
4404 }
4405 d = lck->data;
4406 for (j=0; j<d->num_share_modes; j++) {
4407 struct share_mode_entry *e = &d->share_modes[j];
4408 uint32_t e_lease_type = get_lease_type(d, e);
4409 struct share_mode_lease *l = NULL;
4410
4411 if (share_mode_stale_pid(d, j)) {
4412 continue;
4413 }
4414
4415 if (e->op_type == LEASE_OPLOCK) {
4416 l = &lck->data->leases[e->lease_idx];
4417 if (!smb2_lease_key_equal(&l->lease_key,
4418 lease_key)) {
4419 continue;
4420 }
4421 *p_epoch = l->epoch;
4422 *p_version = l->lease_version;
4423 }
4424
4425 if (e_lease_type == SMB2_LEASE_NONE) {
4426 continue;
4427 }
4428
4429 send_break_message(conn->sconn->msg_ctx, e,
4430 SMB2_LEASE_NONE);
4431
4432 /*
4433 * Windows 7 and 8 lease clients
4434 * are broken in that they will not
4435 * respond to lease break requests
4436 * whilst waiting for an outstanding
4437 * open request on that lease handle
4438 * on the same TCP connection, due
4439 * to holding an internal inode lock.
4440 *
4441 * This means we can't reschedule
4442 * ourselves here, but must return
4443 * from the create.
4444 *
4445 * Work around:
4446 *
4447 * Send the breaks and then return
4448 * SMB2_LEASE_NONE in the lease handle
4449 * to cause them to acknowledge the
4450 * lease break. Consulatation with
4451 * Microsoft engineering confirmed
4452 * this approach is safe.
4453 */
4454
4455 }
4456 TALLOC_FREE(lck);
4457 }
4458 /*
4459 * Ensure we don't grant anything more so we
4460 * never upgrade.
4461 */
4462 return NT_STATUS_OPLOCK_NOT_GRANTED;
4463}
4464
4465/*
4466 * Wrapper around open_file_ntcreate and open_directory
4467 */
4468
4469static NTSTATUS create_file_unixpath(connection_struct *conn,
4470 struct smb_request *req,
4471 struct smb_filename *smb_fname,
4472 uint32_t access_mask,
4473 uint32_t share_access,
4474 uint32_t create_disposition,
4475 uint32_t create_options,
4476 uint32_t file_attributes,
4477 uint32_t oplock_request,
4478 struct smb2_lease *lease,
4479 uint64_t allocation_size,
4480 uint32_t private_flags,
4481 struct security_descriptor *sd,
4482 struct ea_list *ea_list,
4483
4484 files_struct **result,
4485 int *pinfo)
4486{
4487 int info = FILE_WAS_OPENED;
4488 files_struct *base_fsp = NULL;
4489 files_struct *fsp = NULL;
4490 NTSTATUS status;
4491
4492 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
4493 "file_attributes = 0x%x, share_access = 0x%x, "
4494 "create_disposition = 0x%x create_options = 0x%x "
4495 "oplock_request = 0x%x private_flags = 0x%x "
4496 "ea_list = 0x%p, sd = 0x%p, "
4497 "fname = %s\n",
4498 (unsigned int)access_mask,
4499 (unsigned int)file_attributes,
4500 (unsigned int)share_access,
4501 (unsigned int)create_disposition,
4502 (unsigned int)create_options,
4503 (unsigned int)oplock_request,
4504 (unsigned int)private_flags,
4505 ea_list, sd, smb_fname_str_dbg(smb_fname)));
4506
4507 if (create_options & FILE_OPEN_BY_FILE_ID) {
4508 status = NT_STATUS_NOT_SUPPORTED;
4509 goto fail;
4510 }
4511
4512 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
4513 status = NT_STATUS_INVALID_PARAMETER;
4514 goto fail;
4515 }
4516
4517 if (req == NULL) {
4518 oplock_request |= INTERNAL_OPEN_ONLY;
4519 }
4520
4521 if (lease != NULL) {
4522 uint16_t epoch = lease->lease_epoch;
4523 uint16_t version = lease->lease_version;
4524 status = lease_match(conn,
4525 req,
4526 &lease->lease_key,
4527 conn->connectpath,
4528 smb_fname,
4529 &version,
4530 &epoch);
4531 if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_NOT_GRANTED)) {
4532 /* Dynamic share file. No leases and update epoch... */
4533 lease->lease_state = SMB2_LEASE_NONE;
4534 lease->lease_epoch = epoch;
4535 lease->lease_version = version;
4536 } else if (!NT_STATUS_IS_OK(status)) {
4537 goto fail;
4538 }
4539 }
4540
4541 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
4542 && (access_mask & DELETE_ACCESS)
4543 && !is_ntfs_stream_smb_fname(smb_fname)) {
4544 /*
4545 * We can't open a file with DELETE access if any of the
4546 * streams is open without FILE_SHARE_DELETE
4547 */
4548 status = open_streams_for_delete(conn, smb_fname->base_name);
4549
4550 if (!NT_STATUS_IS_OK(status)) {
4551 goto fail;
4552 }
4553 }
4554
4555 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
4556 !security_token_has_privilege(get_current_nttok(conn),
4557 SEC_PRIV_SECURITY)) {
4558 DEBUG(10, ("create_file_unixpath: open on %s "
4559 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
4560 smb_fname_str_dbg(smb_fname)));
4561 status = NT_STATUS_PRIVILEGE_NOT_HELD;
4562 goto fail;
4563 }
4564
4565 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
4566 && is_ntfs_stream_smb_fname(smb_fname)
4567 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
4568 uint32_t base_create_disposition;
4569 struct smb_filename *smb_fname_base = NULL;
4570
4571 if (create_options & FILE_DIRECTORY_FILE) {
4572 status = NT_STATUS_NOT_A_DIRECTORY;
4573 goto fail;
4574 }
4575
4576 switch (create_disposition) {
4577 case FILE_OPEN:
4578 base_create_disposition = FILE_OPEN;
4579 break;
4580 default:
4581 base_create_disposition = FILE_OPEN_IF;
4582 break;
4583 }
4584
4585 /* Create an smb_filename with stream_name == NULL. */
4586 smb_fname_base = synthetic_smb_fname(talloc_tos(),
4587 smb_fname->base_name,
4588 NULL, NULL);
4589 if (smb_fname_base == NULL) {
4590 status = NT_STATUS_NO_MEMORY;
4591 goto fail;
4592 }
4593
4594 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
4595 DEBUG(10, ("Unable to stat stream: %s\n",
4596 smb_fname_str_dbg(smb_fname_base)));
4597 } else {
4598 /*
4599 * https://bugzilla.samba.org/show_bug.cgi?id=10229
4600 * We need to check if the requested access mask
4601 * could be used to open the underlying file (if
4602 * it existed), as we're passing in zero for the
4603 * access mask to the base filename.
4604 */
4605 status = check_base_file_access(conn,
4606 smb_fname_base,
4607 access_mask);
4608
4609 if (!NT_STATUS_IS_OK(status)) {
4610 DEBUG(10, ("Permission check "
4611 "for base %s failed: "
4612 "%s\n", smb_fname->base_name,
4613 nt_errstr(status)));
4614 goto fail;
4615 }
4616 }
4617
4618 /* Open the base file. */
4619 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
4620 FILE_SHARE_READ
4621 | FILE_SHARE_WRITE
4622 | FILE_SHARE_DELETE,
4623 base_create_disposition,
4624 0, 0, 0, NULL, 0, 0, NULL, NULL,
4625 &base_fsp, NULL);
4626 TALLOC_FREE(smb_fname_base);
4627
4628 if (!NT_STATUS_IS_OK(status)) {
4629 DEBUG(10, ("create_file_unixpath for base %s failed: "
4630 "%s\n", smb_fname->base_name,
4631 nt_errstr(status)));
4632 goto fail;
4633 }
4634 /* we don't need the low level fd */
4635 fd_close(base_fsp);
4636 }
4637
4638 /*
4639 * If it's a request for a directory open, deal with it separately.
4640 */
4641
4642 if (create_options & FILE_DIRECTORY_FILE) {
4643
4644 if (create_options & FILE_NON_DIRECTORY_FILE) {
4645 status = NT_STATUS_INVALID_PARAMETER;
4646 goto fail;
4647 }
4648
4649 /* Can't open a temp directory. IFS kit test. */
4650 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
4651 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
4652 status = NT_STATUS_INVALID_PARAMETER;
4653 goto fail;
4654 }
4655
4656 /*
4657 * We will get a create directory here if the Win32
4658 * app specified a security descriptor in the
4659 * CreateDirectory() call.
4660 */
4661
4662 oplock_request = 0;
4663 status = open_directory(
4664 conn, req, smb_fname, access_mask, share_access,
4665 create_disposition, create_options, file_attributes,
4666 &info, &fsp);
4667 } else {
4668
4669 /*
4670 * Ordinary file case.
4671 */
4672
4673 status = file_new(req, conn, &fsp);
4674 if(!NT_STATUS_IS_OK(status)) {
4675 goto fail;
4676 }
4677
4678 status = fsp_set_smb_fname(fsp, smb_fname);
4679 if (!NT_STATUS_IS_OK(status)) {
4680 goto fail;
4681 }
4682
4683 if (base_fsp) {
4684 /*
4685 * We're opening the stream element of a
4686 * base_fsp we already opened. Set up the
4687 * base_fsp pointer.
4688 */
4689 fsp->base_fsp = base_fsp;
4690 }
4691
4692 if (allocation_size) {
4693 fsp->initial_allocation_size = smb_roundup(fsp->conn,
4694 allocation_size);
4695 }
4696
4697 status = open_file_ntcreate(conn,
4698 req,
4699 access_mask,
4700 share_access,
4701 create_disposition,
4702 create_options,
4703 file_attributes,
4704 oplock_request,
4705 lease,
4706 private_flags,
4707 &info,
4708 fsp);
4709
4710 if(!NT_STATUS_IS_OK(status)) {
4711 file_free(req, fsp);
4712 fsp = NULL;
4713 }
4714
4715 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
4716
4717 /* A stream open never opens a directory */
4718
4719 if (base_fsp) {
4720 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4721 goto fail;
4722 }
4723
4724 /*
4725 * Fail the open if it was explicitly a non-directory
4726 * file.
4727 */
4728
4729 if (create_options & FILE_NON_DIRECTORY_FILE) {
4730 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4731 goto fail;
4732 }
4733
4734 oplock_request = 0;
4735 status = open_directory(
4736 conn, req, smb_fname, access_mask,
4737 share_access, create_disposition,
4738 create_options, file_attributes,
4739 &info, &fsp);
4740 }
4741 }
4742
4743 if (!NT_STATUS_IS_OK(status)) {
4744 goto fail;
4745 }
4746
4747 fsp->base_fsp = base_fsp;
4748
4749 if ((ea_list != NULL) &&
4750 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
4751 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
4752 if (!NT_STATUS_IS_OK(status)) {
4753 goto fail;
4754 }
4755 }
4756
4757 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
4758 status = NT_STATUS_ACCESS_DENIED;
4759 goto fail;
4760 }
4761
4762 /* Save the requested allocation size. */
4763 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
4764 if ((allocation_size > fsp->fsp_name->st.st_ex_size)
4765 && !(fsp->is_directory))
4766 {
4767 fsp->initial_allocation_size = smb_roundup(
4768 fsp->conn, allocation_size);
4769 if (vfs_allocate_file_space(
4770 fsp, fsp->initial_allocation_size) == -1) {
4771 status = NT_STATUS_DISK_FULL;
4772 goto fail;
4773 }
4774 } else {
4775 fsp->initial_allocation_size = smb_roundup(
4776 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
4777 }
4778 } else {
4779 fsp->initial_allocation_size = 0;
4780 }
4781
4782 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
4783 fsp->base_fsp == NULL) {
4784 if (sd != NULL) {
4785 /*
4786 * According to the MS documentation, the only time the security
4787 * descriptor is applied to the opened file is iff we *created* the
4788 * file; an existing file stays the same.
4789 *
4790 * Also, it seems (from observation) that you can open the file with
4791 * any access mask but you can still write the sd. We need to override
4792 * the granted access before we call set_sd
4793 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
4794 */
4795
4796 uint32_t sec_info_sent;
4797 uint32_t saved_access_mask = fsp->access_mask;
4798
4799 sec_info_sent = get_sec_info(sd);
4800
4801 fsp->access_mask = FILE_GENERIC_ALL;
4802
4803 if (sec_info_sent & (SECINFO_OWNER|
4804 SECINFO_GROUP|
4805 SECINFO_DACL|
4806 SECINFO_SACL)) {
4807 status = set_sd(fsp, sd, sec_info_sent);
4808 }
4809
4810 fsp->access_mask = saved_access_mask;
4811
4812 if (!NT_STATUS_IS_OK(status)) {
4813 goto fail;
4814 }
4815 } else if (lp_inherit_acls(SNUM(conn))) {
4816 /* Inherit from parent. Errors here are not fatal. */
4817 status = inherit_new_acl(fsp);
4818 if (!NT_STATUS_IS_OK(status)) {
4819 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
4820 fsp_str_dbg(fsp),
4821 nt_errstr(status) ));
4822 }
4823 }
4824 }
4825
4826 if ((conn->fs_capabilities & FILE_FILE_COMPRESSION)
4827 && (create_options & FILE_NO_COMPRESSION)
4828 && (info == FILE_WAS_CREATED)) {
4829 status = SMB_VFS_SET_COMPRESSION(conn, fsp, fsp,
4830 COMPRESSION_FORMAT_NONE);
4831 if (!NT_STATUS_IS_OK(status)) {
4832 DEBUG(1, ("failed to disable compression: %s\n",
4833 nt_errstr(status)));
4834 }
4835 }
4836
4837 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
4838
4839 *result = fsp;
4840 if (pinfo != NULL) {
4841 *pinfo = info;
4842 }
4843
4844 smb_fname->st = fsp->fsp_name->st;
4845
4846 return NT_STATUS_OK;
4847
4848 fail:
4849 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
4850
4851 if (fsp != NULL) {
4852 if (base_fsp && fsp->base_fsp == base_fsp) {
4853 /*
4854 * The close_file below will close
4855 * fsp->base_fsp.
4856 */
4857 base_fsp = NULL;
4858 }
4859 close_file(req, fsp, ERROR_CLOSE);
4860 fsp = NULL;
4861 }
4862 if (base_fsp != NULL) {
4863 close_file(req, base_fsp, ERROR_CLOSE);
4864 base_fsp = NULL;
4865 }
4866 return status;
4867}
4868
4869/*
4870 * Calculate the full path name given a relative fid.
4871 */
4872NTSTATUS get_relative_fid_filename(connection_struct *conn,
4873 struct smb_request *req,
4874 uint16_t root_dir_fid,
4875 const struct smb_filename *smb_fname,
4876 struct smb_filename **smb_fname_out)
4877{
4878 files_struct *dir_fsp;
4879 char *parent_fname = NULL;
4880 char *new_base_name = NULL;
4881 uint32_t ucf_flags = ((req != NULL && req->posix_pathnames) ?
4882 UCF_POSIX_PATHNAMES : 0);
4883 NTSTATUS status;
4884
4885 if (root_dir_fid == 0 || !smb_fname) {
4886 status = NT_STATUS_INTERNAL_ERROR;
4887 goto out;
4888 }
4889
4890 dir_fsp = file_fsp(req, root_dir_fid);
4891
4892 if (dir_fsp == NULL) {
4893 status = NT_STATUS_INVALID_HANDLE;
4894 goto out;
4895 }
4896
4897 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
4898 status = NT_STATUS_INVALID_HANDLE;
4899 goto out;
4900 }
4901
4902 if (!dir_fsp->is_directory) {
4903
4904 /*
4905 * Check to see if this is a mac fork of some kind.
4906 */
4907
4908 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
4909 is_ntfs_stream_smb_fname(smb_fname)) {
4910 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
4911 goto out;
4912 }
4913
4914 /*
4915 we need to handle the case when we get a
4916 relative open relative to a file and the
4917 pathname is blank - this is a reopen!
4918 (hint from demyn plantenberg)
4919 */
4920
4921 status = NT_STATUS_INVALID_HANDLE;
4922 goto out;
4923 }
4924
4925 if (ISDOT(dir_fsp->fsp_name->base_name)) {
4926 /*
4927 * We're at the toplevel dir, the final file name
4928 * must not contain ./, as this is filtered out
4929 * normally by srvstr_get_path and unix_convert
4930 * explicitly rejects paths containing ./.
4931 */
4932 parent_fname = talloc_strdup(talloc_tos(), "");
4933 if (parent_fname == NULL) {
4934 status = NT_STATUS_NO_MEMORY;
4935 goto out;
4936 }
4937 } else {
4938 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
4939
4940 /*
4941 * Copy in the base directory name.
4942 */
4943
4944 parent_fname = talloc_array(talloc_tos(), char,
4945 dir_name_len+2);
4946 if (parent_fname == NULL) {
4947 status = NT_STATUS_NO_MEMORY;
4948 goto out;
4949 }
4950 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
4951 dir_name_len+1);
4952
4953 /*
4954 * Ensure it ends in a '/'.
4955 * We used TALLOC_SIZE +2 to add space for the '/'.
4956 */
4957
4958 if(dir_name_len
4959 && (parent_fname[dir_name_len-1] != '\\')
4960 && (parent_fname[dir_name_len-1] != '/')) {
4961 parent_fname[dir_name_len] = '/';
4962 parent_fname[dir_name_len+1] = '\0';
4963 }
4964 }
4965
4966 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
4967 smb_fname->base_name);
4968 if (new_base_name == NULL) {
4969 status = NT_STATUS_NO_MEMORY;
4970 goto out;
4971 }
4972
4973 status = filename_convert(req,
4974 conn,
4975 req->flags2 & FLAGS2_DFS_PATHNAMES,
4976 new_base_name,
4977 ucf_flags,
4978 NULL,
4979 smb_fname_out);
4980 if (!NT_STATUS_IS_OK(status)) {
4981 goto out;
4982 }
4983
4984 out:
4985 TALLOC_FREE(parent_fname);
4986 TALLOC_FREE(new_base_name);
4987 return status;
4988}
4989
4990NTSTATUS create_file_default(connection_struct *conn,
4991 struct smb_request *req,
4992 uint16_t root_dir_fid,
4993 struct smb_filename *smb_fname,
4994 uint32_t access_mask,
4995 uint32_t share_access,
4996 uint32_t create_disposition,
4997 uint32_t create_options,
4998 uint32_t file_attributes,
4999 uint32_t oplock_request,
5000 struct smb2_lease *lease,
5001 uint64_t allocation_size,
5002 uint32_t private_flags,
5003 struct security_descriptor *sd,
5004 struct ea_list *ea_list,
5005 files_struct **result,
5006 int *pinfo,
5007 const struct smb2_create_blobs *in_context_blobs,
5008 struct smb2_create_blobs *out_context_blobs)
5009{
5010 int info = FILE_WAS_OPENED;
5011 files_struct *fsp = NULL;
5012 NTSTATUS status;
5013 bool stream_name = false;
5014
5015 DEBUG(10,("create_file: access_mask = 0x%x "
5016 "file_attributes = 0x%x, share_access = 0x%x, "
5017 "create_disposition = 0x%x create_options = 0x%x "
5018 "oplock_request = 0x%x "
5019 "private_flags = 0x%x "
5020 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
5021 "fname = %s\n",
5022 (unsigned int)access_mask,
5023 (unsigned int)file_attributes,
5024 (unsigned int)share_access,
5025 (unsigned int)create_disposition,
5026 (unsigned int)create_options,
5027 (unsigned int)oplock_request,
5028 (unsigned int)private_flags,
5029 (unsigned int)root_dir_fid,
5030 ea_list, sd, smb_fname_str_dbg(smb_fname)));
5031
5032 /*
5033 * Calculate the filename from the root_dir_if if necessary.
5034 */
5035
5036 if (root_dir_fid != 0) {
5037 struct smb_filename *smb_fname_out = NULL;
5038 status = get_relative_fid_filename(conn, req, root_dir_fid,
5039 smb_fname, &smb_fname_out);
5040 if (!NT_STATUS_IS_OK(status)) {
5041 goto fail;
5042 }
5043 smb_fname = smb_fname_out;
5044 }
5045
5046 /*
5047 * Check to see if this is a mac fork of some kind.
5048 */
5049
5050 stream_name = is_ntfs_stream_smb_fname(smb_fname);
5051 if (stream_name) {
5052 enum FAKE_FILE_TYPE fake_file_type;
5053
5054 fake_file_type = is_fake_file(smb_fname);
5055
5056 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
5057
5058 /*
5059 * Here we go! support for changing the disk quotas
5060 * --metze
5061 *
5062 * We need to fake up to open this MAGIC QUOTA file
5063 * and return a valid FID.
5064 *
5065 * w2k close this file directly after openening xp
5066 * also tries a QUERY_FILE_INFO on the file and then
5067 * close it
5068 */
5069 status = open_fake_file(req, conn, req->vuid,
5070 fake_file_type, smb_fname,
5071 access_mask, &fsp);
5072 if (!NT_STATUS_IS_OK(status)) {
5073 goto fail;
5074 }
5075
5076 ZERO_STRUCT(smb_fname->st);
5077 goto done;
5078 }
5079
5080 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
5081 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5082 goto fail;
5083 }
5084 }
5085
5086 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
5087 int ret;
5088 smb_fname->stream_name = NULL;
5089 /* We have to handle this error here. */
5090 if (create_options & FILE_DIRECTORY_FILE) {
5091 status = NT_STATUS_NOT_A_DIRECTORY;
5092 goto fail;
5093 }
5094 if (req != NULL && req->posix_pathnames) {
5095 ret = SMB_VFS_LSTAT(conn, smb_fname);
5096 } else {
5097 ret = SMB_VFS_STAT(conn, smb_fname);
5098 }
5099
5100 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
5101 status = NT_STATUS_FILE_IS_A_DIRECTORY;
5102 goto fail;
5103 }
5104 }
5105
5106 status = create_file_unixpath(
5107 conn, req, smb_fname, access_mask, share_access,
5108 create_disposition, create_options, file_attributes,
5109 oplock_request, lease, allocation_size, private_flags,
5110 sd, ea_list,
5111 &fsp, &info);
5112
5113 if (!NT_STATUS_IS_OK(status)) {
5114 goto fail;
5115 }
5116
5117 done:
5118 DEBUG(10, ("create_file: info=%d\n", info));
5119
5120 *result = fsp;
5121 if (pinfo != NULL) {
5122 *pinfo = info;
5123 }
5124 return NT_STATUS_OK;
5125
5126 fail:
5127 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
5128
5129 if (fsp != NULL) {
5130 close_file(req, fsp, ERROR_CLOSE);
5131 fsp = NULL;
5132 }
5133 return status;
5134}
Note: See TracBrowser for help on using the repository browser.