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