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