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