Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : file opening and share modes
4 : Copyright (C) Andrew Tridgell 1992-1998
5 : Copyright (C) Jeremy Allison 2001-2004
6 : Copyright (C) Volker Lendecke 2005
7 : Copyright (C) Ralph Boehme 2017
8 :
9 : This program is free software; you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation; either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program. If not, see <http://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "includes.h"
24 : #include "system/filesys.h"
25 : #include "lib/util/server_id.h"
26 : #include "printing.h"
27 : #include "locking/share_mode_lock.h"
28 : #include "smbd/smbd.h"
29 : #include "smbd/globals.h"
30 : #include "fake_file.h"
31 : #include "../libcli/security/security.h"
32 : #include "../librpc/gen_ndr/ndr_security.h"
33 : #include "../librpc/gen_ndr/ndr_open_files.h"
34 : #include "../librpc/gen_ndr/idmap.h"
35 : #include "../librpc/gen_ndr/ioctl.h"
36 : #include "passdb/lookup_sid.h"
37 : #include "auth.h"
38 : #include "serverid.h"
39 : #include "messages.h"
40 : #include "source3/lib/dbwrap/dbwrap_watch.h"
41 : #include "locking/leases_db.h"
42 : #include "librpc/gen_ndr/ndr_leases_db.h"
43 : #include "lib/util/time_basic.h"
44 :
45 : extern const struct generic_mapping file_generic_mapping;
46 :
47 : struct deferred_open_record {
48 : struct smbXsrv_connection *xconn;
49 : uint64_t mid;
50 :
51 : bool async_open;
52 :
53 : /*
54 : * Timer for async opens, needed because they don't use a watch on
55 : * a locking.tdb record. This is currently only used for real async
56 : * opens and just terminates smbd if the async open times out.
57 : */
58 : struct tevent_timer *te;
59 :
60 : /*
61 : * For the samba kernel oplock case we use both a timeout and
62 : * a watch on locking.tdb. This way in case it's smbd holding
63 : * the kernel oplock we get directly notified for the retry
64 : * once the kernel oplock is properly broken. Store the req
65 : * here so that it can be timely discarded once the timer
66 : * above fires.
67 : */
68 : struct tevent_req *watch_req;
69 : };
70 :
71 : /****************************************************************************
72 : If the requester wanted DELETE_ACCESS and was rejected because
73 : the file ACL didn't include DELETE_ACCESS, see if the parent ACL
74 : overrides this.
75 : ****************************************************************************/
76 :
77 1791 : static bool parent_override_delete(connection_struct *conn,
78 : struct files_struct *dirfsp,
79 : const struct smb_filename *smb_fname,
80 : uint32_t access_mask,
81 : uint32_t rejected_mask)
82 : {
83 1795 : if ((access_mask & DELETE_ACCESS) &&
84 2580 : (rejected_mask & DELETE_ACCESS) &&
85 1290 : can_delete_file_in_directory(conn,
86 : dirfsp,
87 : smb_fname))
88 : {
89 1266 : return true;
90 : }
91 521 : return false;
92 : }
93 :
94 : /****************************************************************************
95 : Check if we have open rights.
96 : ****************************************************************************/
97 :
98 288895 : static NTSTATUS smbd_check_access_rights_fname(
99 : struct connection_struct *conn,
100 : const struct smb_filename *smb_fname,
101 : bool use_privs,
102 : uint32_t access_mask,
103 : uint32_t do_not_check_mask)
104 : {
105 691 : uint32_t rejected_share_access;
106 691 : uint32_t effective_access;
107 :
108 288895 : rejected_share_access = access_mask & ~(conn->share_access);
109 :
110 288895 : if (rejected_share_access) {
111 0 : DBG_DEBUG("rejected share access 0x%"PRIx32" on "
112 : "%s (0x%"PRIx32")\n",
113 : access_mask,
114 : smb_fname_str_dbg(smb_fname),
115 : rejected_share_access);
116 0 : return NT_STATUS_ACCESS_DENIED;
117 : }
118 :
119 288895 : effective_access = access_mask & ~do_not_check_mask;
120 288895 : if (effective_access == 0) {
121 45888 : DBG_DEBUG("do_not_check_mask override on %s. Granting 0x%x for free.\n",
122 : smb_fname_str_dbg(smb_fname),
123 : (unsigned int)access_mask);
124 45888 : return NT_STATUS_OK;
125 : }
126 :
127 243007 : if (!use_privs && get_current_uid(conn) == (uid_t)0) {
128 : /* I'm sorry sir, I didn't know you were root... */
129 1993 : DBG_DEBUG("root override on %s. Granting 0x%x\n",
130 : smb_fname_str_dbg(smb_fname),
131 : (unsigned int)access_mask);
132 1993 : return NT_STATUS_OK;
133 : }
134 :
135 241361 : if ((access_mask & DELETE_ACCESS) &&
136 179192 : !lp_acl_check_permissions(SNUM(conn)))
137 : {
138 0 : DBG_DEBUG("Not checking ACL on DELETE_ACCESS on file %s. "
139 : "Granting 0x%"PRIx32"\n",
140 : smb_fname_str_dbg(smb_fname),
141 : access_mask);
142 0 : return NT_STATUS_OK;
143 : }
144 :
145 241014 : if (access_mask == DELETE_ACCESS &&
146 169501 : VALID_STAT(smb_fname->st) &&
147 169501 : S_ISLNK(smb_fname->st.st_ex_mode))
148 : {
149 : /* We can always delete a symlink. */
150 63 : DBG_DEBUG("Not checking ACL on DELETE_ACCESS on symlink %s.\n",
151 : smb_fname_str_dbg(smb_fname));
152 63 : return NT_STATUS_OK;
153 : }
154 :
155 240951 : return NT_STATUS_MORE_PROCESSING_REQUIRED;
156 : }
157 :
158 240951 : static NTSTATUS smbd_check_access_rights_sd(
159 : struct connection_struct *conn,
160 : struct files_struct *dirfsp,
161 : const struct smb_filename *smb_fname,
162 : struct security_descriptor *sd,
163 : bool use_privs,
164 : uint32_t access_mask,
165 : uint32_t do_not_check_mask)
166 : {
167 240951 : uint32_t rejected_mask = access_mask;
168 685 : NTSTATUS status;
169 :
170 240951 : if (sd == NULL) {
171 0 : goto access_denied;
172 : }
173 :
174 240951 : status = se_file_access_check(sd,
175 : get_current_nttok(conn),
176 : use_privs,
177 240951 : (access_mask & ~do_not_check_mask),
178 : &rejected_mask);
179 :
180 240951 : DBG_DEBUG("File [%s] requesting [0x%"PRIx32"] "
181 : "returning [0x%"PRIx32"] (%s)\n",
182 : smb_fname_str_dbg(smb_fname),
183 : access_mask,
184 : rejected_mask,
185 : nt_errstr(status));
186 :
187 240951 : if (!NT_STATUS_IS_OK(status)) {
188 1791 : if (DEBUGLEVEL >= 10) {
189 0 : DBG_DEBUG("acl for %s is:\n",
190 : smb_fname_str_dbg(smb_fname));
191 0 : NDR_PRINT_DEBUG(security_descriptor, sd);
192 : }
193 : }
194 :
195 240951 : TALLOC_FREE(sd);
196 :
197 240951 : if (NT_STATUS_IS_OK(status) ||
198 1785 : !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED))
199 : {
200 239160 : return status;
201 : }
202 :
203 : /* Here we know status == NT_STATUS_ACCESS_DENIED. */
204 :
205 1791 : access_denied:
206 :
207 1791 : if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
208 257 : (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
209 189 : !lp_store_dos_attributes(SNUM(conn)) &&
210 0 : (lp_map_readonly(SNUM(conn)) ||
211 0 : lp_map_archive(SNUM(conn)) ||
212 0 : lp_map_hidden(SNUM(conn)) ||
213 0 : lp_map_system(SNUM(conn))))
214 : {
215 0 : rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
216 :
217 0 : DBG_DEBUG("overrode FILE_WRITE_ATTRIBUTES on file %s\n",
218 : smb_fname_str_dbg(smb_fname));
219 : }
220 :
221 1791 : if (parent_override_delete(conn,
222 : dirfsp,
223 : smb_fname,
224 : access_mask,
225 : rejected_mask))
226 : {
227 : /*
228 : * Were we trying to do an open for delete and didn't get DELETE
229 : * access. Check if the directory allows DELETE_CHILD.
230 : * See here:
231 : * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
232 : * for details.
233 : */
234 :
235 1266 : rejected_mask &= ~DELETE_ACCESS;
236 :
237 1266 : DBG_DEBUG("Overrode DELETE_ACCESS on file %s\n",
238 : smb_fname_str_dbg(smb_fname));
239 : }
240 :
241 1791 : if (rejected_mask != 0) {
242 613 : return NT_STATUS_ACCESS_DENIED;
243 : }
244 1178 : return NT_STATUS_OK;
245 : }
246 :
247 288983 : NTSTATUS smbd_check_access_rights_fsp(struct files_struct *dirfsp,
248 : struct files_struct *fsp,
249 : bool use_privs,
250 : uint32_t access_mask)
251 : {
252 288983 : struct security_descriptor *sd = NULL;
253 288983 : uint32_t do_not_check_mask = 0;
254 691 : NTSTATUS status;
255 :
256 : /* Cope with fake/printer fsp's. */
257 288983 : if (fsp->fake_file_handle != NULL || fsp->print_file != NULL) {
258 2 : if ((fsp->access_mask & access_mask) != access_mask) {
259 0 : return NT_STATUS_ACCESS_DENIED;
260 : }
261 2 : return NT_STATUS_OK;
262 : }
263 :
264 288981 : if (fsp_get_pathref_fd(fsp) == -1) {
265 : /*
266 : * This is a POSIX open on a symlink. For the pathname
267 : * version of this function we used to return the st_mode
268 : * bits turned into an NT ACL. For a symlink the mode bits
269 : * are always rwxrwxrwx which means the pathname version always
270 : * returned NT_STATUS_OK for a symlink. For the handle reference
271 : * to a symlink use the handle access bits.
272 : */
273 86 : if ((fsp->access_mask & access_mask) != access_mask) {
274 16 : return NT_STATUS_ACCESS_DENIED;
275 : }
276 70 : return NT_STATUS_OK;
277 : }
278 :
279 : /*
280 : * If we can access the path to this file, by
281 : * default we have FILE_READ_ATTRIBUTES from the
282 : * containing directory. See the section:
283 : * "Algorithm to Check Access to an Existing File"
284 : * in MS-FSA.pdf.
285 : *
286 : * se_file_access_check() also takes care of
287 : * owner WRITE_DAC and READ_CONTROL.
288 : */
289 288895 : do_not_check_mask = FILE_READ_ATTRIBUTES;
290 :
291 : /*
292 : * Samba 3.6 and earlier granted execute access even
293 : * if the ACL did not contain execute rights.
294 : * Samba 4.0 is more correct and checks it.
295 : * The compatibility mode allows one to skip this check
296 : * to smoothen upgrades.
297 : */
298 288895 : if (lp_acl_allow_execute_always(SNUM(fsp->conn))) {
299 0 : do_not_check_mask |= FILE_EXECUTE;
300 : }
301 :
302 289586 : status = smbd_check_access_rights_fname(fsp->conn,
303 288895 : fsp->fsp_name,
304 : use_privs,
305 : access_mask,
306 : do_not_check_mask);
307 288895 : if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
308 47944 : return status;
309 : }
310 :
311 240951 : status = SMB_VFS_FGET_NT_ACL(metadata_fsp(fsp),
312 : (SECINFO_OWNER |
313 : SECINFO_GROUP |
314 : SECINFO_DACL),
315 : talloc_tos(),
316 : &sd);
317 240951 : if (!NT_STATUS_IS_OK(status)) {
318 0 : DBG_DEBUG("Could not get acl on %s: %s\n",
319 : fsp_str_dbg(fsp),
320 : nt_errstr(status));
321 0 : return status;
322 : }
323 :
324 240951 : return smbd_check_access_rights_sd(fsp->conn,
325 : dirfsp,
326 240951 : fsp->fsp_name,
327 : sd,
328 : use_privs,
329 : access_mask,
330 : do_not_check_mask);
331 : }
332 :
333 : /*
334 : * Given an fsp that represents a parent directory,
335 : * check if the requested access can be granted.
336 : */
337 170415 : NTSTATUS check_parent_access_fsp(struct files_struct *fsp,
338 : uint32_t access_mask)
339 : {
340 363 : NTSTATUS status;
341 170415 : struct security_descriptor *parent_sd = NULL;
342 170415 : uint32_t access_granted = 0;
343 170415 : struct share_mode_lock *lck = NULL;
344 363 : uint32_t name_hash;
345 363 : bool delete_on_close_set;
346 170415 : TALLOC_CTX *frame = talloc_stackframe();
347 :
348 170415 : if (get_current_uid(fsp->conn) == (uid_t)0) {
349 : /* I'm sorry sir, I didn't know you were root... */
350 697 : DBG_DEBUG("root override on %s. Granting 0x%x\n",
351 : fsp_str_dbg(fsp),
352 : (unsigned int)access_mask);
353 697 : status = NT_STATUS_OK;
354 697 : goto out;
355 : }
356 :
357 169718 : status = SMB_VFS_FGET_NT_ACL(fsp,
358 : SECINFO_DACL,
359 : frame,
360 : &parent_sd);
361 :
362 169718 : if (!NT_STATUS_IS_OK(status)) {
363 0 : DBG_INFO("SMB_VFS_FGET_NT_ACL failed for "
364 : "%s with error %s\n",
365 : fsp_str_dbg(fsp),
366 : nt_errstr(status));
367 0 : goto out;
368 : }
369 :
370 : /*
371 : * If we can access the path to this file, by
372 : * default we have FILE_READ_ATTRIBUTES from the
373 : * containing directory. See the section:
374 : * "Algorithm to Check Access to an Existing File"
375 : * in MS-FSA.pdf.
376 : *
377 : * se_file_access_check() also takes care of
378 : * owner WRITE_DAC and READ_CONTROL.
379 : */
380 169718 : status = se_file_access_check(parent_sd,
381 169718 : get_current_nttok(fsp->conn),
382 : false,
383 : (access_mask & ~FILE_READ_ATTRIBUTES),
384 : &access_granted);
385 169718 : if(!NT_STATUS_IS_OK(status)) {
386 12 : DBG_INFO("access check "
387 : "on directory %s for mask 0x%x returned (0x%x) %s\n",
388 : fsp_str_dbg(fsp),
389 : access_mask,
390 : access_granted,
391 : nt_errstr(status));
392 12 : goto out;
393 : }
394 :
395 169706 : if (!(access_mask & (SEC_DIR_ADD_FILE | SEC_DIR_ADD_SUBDIR))) {
396 0 : status = NT_STATUS_OK;
397 0 : goto out;
398 : }
399 169706 : if (!lp_check_parent_directory_delete_on_close(SNUM(fsp->conn))) {
400 42665 : status = NT_STATUS_OK;
401 42665 : goto out;
402 : }
403 :
404 : /* Check if the directory has delete-on-close set */
405 127404 : status = file_name_hash(fsp->conn,
406 127041 : fsp->fsp_name->base_name,
407 : &name_hash);
408 127041 : if (!NT_STATUS_IS_OK(status)) {
409 0 : goto out;
410 : }
411 :
412 : /*
413 : * Don't take a lock here. We just need a snapshot
414 : * of the current state of delete on close and this is
415 : * called in a codepath where we may already have a lock
416 : * (and we explicitly can't hold 2 locks at the same time
417 : * as that may deadlock).
418 : */
419 127041 : lck = fetch_share_mode_unlocked(frame, fsp->file_id);
420 127041 : if (lck == NULL) {
421 100623 : status = NT_STATUS_OK;
422 100623 : goto out;
423 : }
424 :
425 26418 : delete_on_close_set = is_delete_on_close_set(lck, name_hash);
426 26418 : if (delete_on_close_set) {
427 7 : status = NT_STATUS_DELETE_PENDING;
428 7 : goto out;
429 : }
430 :
431 26392 : status = NT_STATUS_OK;
432 :
433 170415 : out:
434 170415 : TALLOC_FREE(frame);
435 170415 : return status;
436 : }
437 :
438 : /****************************************************************************
439 : Ensure when opening a base file for a stream open that we have permissions
440 : to do so given the access mask on the base file.
441 : ****************************************************************************/
442 :
443 7117 : static NTSTATUS check_base_file_access(struct files_struct *fsp,
444 : uint32_t access_mask)
445 : {
446 3 : NTSTATUS status;
447 :
448 7117 : status = smbd_calculate_access_mask_fsp(fsp->conn->cwd_fsp,
449 : fsp,
450 : false,
451 : access_mask,
452 : &access_mask);
453 7117 : if (!NT_STATUS_IS_OK(status)) {
454 0 : DEBUG(10, ("smbd_calculate_access_mask "
455 : "on file %s returned %s\n",
456 : fsp_str_dbg(fsp),
457 : nt_errstr(status)));
458 0 : return status;
459 : }
460 :
461 7117 : if (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) {
462 0 : uint32_t dosattrs;
463 4216 : if (!CAN_WRITE(fsp->conn)) {
464 0 : return NT_STATUS_ACCESS_DENIED;
465 : }
466 4216 : dosattrs = fdos_mode(fsp);
467 4216 : if (dosattrs & FILE_ATTRIBUTE_READONLY) {
468 4 : return NT_STATUS_ACCESS_DENIED;
469 : }
470 : }
471 :
472 7113 : return smbd_check_access_rights_fsp(fsp->conn->cwd_fsp,
473 : fsp,
474 : false,
475 : access_mask);
476 : }
477 :
478 3419596 : static NTSTATUS chdir_below_conn(
479 : TALLOC_CTX *mem_ctx,
480 : connection_struct *conn,
481 : const char *connectpath,
482 : size_t connectpath_len,
483 : struct smb_filename *dir_fname,
484 : struct smb_filename **_oldwd_fname)
485 : {
486 3419596 : struct smb_filename *oldwd_fname = NULL;
487 3419596 : struct smb_filename *smb_fname_dot = NULL;
488 3419596 : struct smb_filename *real_fname = NULL;
489 3419596 : const char *relative = NULL;
490 10588 : NTSTATUS status;
491 10588 : int ret;
492 10588 : bool ok;
493 :
494 3419596 : if (!ISDOT(dir_fname->base_name)) {
495 :
496 622148 : oldwd_fname = vfs_GetWd(talloc_tos(), conn);
497 622148 : if (oldwd_fname == NULL) {
498 0 : status = map_nt_error_from_unix(errno);
499 0 : goto out;
500 : }
501 :
502 : /* Pin parent directory in place. */
503 622148 : ret = vfs_ChDir(conn, dir_fname);
504 622148 : if (ret == -1) {
505 16222 : status = map_nt_error_from_unix(errno);
506 16222 : DBG_DEBUG("chdir to %s failed: %s\n",
507 : dir_fname->base_name,
508 : strerror(errno));
509 16222 : goto out;
510 : }
511 : }
512 :
513 3403374 : smb_fname_dot = synthetic_smb_fname(
514 : talloc_tos(),
515 : ".",
516 : NULL,
517 : NULL,
518 : dir_fname->twrp,
519 : dir_fname->flags);
520 3403374 : if (smb_fname_dot == NULL) {
521 0 : status = NT_STATUS_NO_MEMORY;
522 0 : goto out;
523 : }
524 :
525 3403374 : real_fname = SMB_VFS_REALPATH(conn, talloc_tos(), smb_fname_dot);
526 3403374 : if (real_fname == NULL) {
527 0 : status = map_nt_error_from_unix(errno);
528 0 : DBG_DEBUG("realpath in %s failed: %s\n",
529 : dir_fname->base_name,
530 : strerror(errno));
531 0 : goto out;
532 : }
533 3403374 : TALLOC_FREE(smb_fname_dot);
534 :
535 3413945 : ok = subdir_of(connectpath,
536 : connectpath_len,
537 3403374 : real_fname->base_name,
538 : &relative);
539 3403374 : if (ok) {
540 3273262 : TALLOC_FREE(real_fname);
541 3273262 : *_oldwd_fname = oldwd_fname;
542 3273262 : return NT_STATUS_OK;
543 : }
544 :
545 130112 : DBG_NOTICE("Bad access attempt: %s is a symlink "
546 : "outside the share path\n"
547 : "conn_rootdir =%s\n"
548 : "resolved_name=%s\n",
549 : dir_fname->base_name,
550 : connectpath,
551 : real_fname->base_name);
552 130112 : TALLOC_FREE(real_fname);
553 :
554 130112 : status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
555 :
556 146334 : out:
557 146334 : if (oldwd_fname != NULL) {
558 26712 : ret = vfs_ChDir(conn, oldwd_fname);
559 26712 : SMB_ASSERT(ret == 0);
560 26712 : TALLOC_FREE(oldwd_fname);
561 : }
562 :
563 146334 : return status;
564 : }
565 :
566 : /*
567 : * Get the symlink target of dirfsp/symlink_name, making sure the
568 : * target is below connection_path.
569 : */
570 :
571 2361 : static NTSTATUS symlink_target_below_conn(
572 : TALLOC_CTX *mem_ctx,
573 : const char *connection_path,
574 : size_t connection_path_len,
575 : struct files_struct *fsp,
576 : struct files_struct *dirfsp,
577 : struct smb_filename *symlink_name,
578 : char **_target)
579 : {
580 2361 : char *target = NULL;
581 2361 : char *absolute = NULL;
582 2361 : const char *relative = NULL;
583 0 : NTSTATUS status;
584 0 : bool ok;
585 :
586 2361 : if (fsp_get_pathref_fd(fsp) != -1) {
587 : /*
588 : * fsp is an O_PATH open, Linux does a "freadlink"
589 : * with an empty name argument to readlinkat
590 : */
591 1359 : status = readlink_talloc(talloc_tos(), fsp, NULL, &target);
592 : } else {
593 1002 : status = readlink_talloc(
594 : talloc_tos(), dirfsp, symlink_name, &target);
595 : }
596 :
597 2361 : if (!NT_STATUS_IS_OK(status)) {
598 0 : DBG_DEBUG("readlink_talloc failed: %s\n", nt_errstr(status));
599 0 : return status;
600 : }
601 :
602 2361 : if (target[0] != '/') {
603 420 : char *tmp = talloc_asprintf(
604 420 : talloc_tos(),
605 : "%s/%s/%s",
606 : connection_path,
607 420 : dirfsp->fsp_name->base_name,
608 : target);
609 :
610 420 : TALLOC_FREE(target);
611 :
612 420 : if (tmp == NULL) {
613 0 : return NT_STATUS_NO_MEMORY;
614 : }
615 420 : target = tmp;
616 : }
617 :
618 2361 : DBG_DEBUG("redirecting to %s\n", target);
619 :
620 2361 : absolute = canonicalize_absolute_path(talloc_tos(), target);
621 2361 : TALLOC_FREE(target);
622 :
623 2361 : if (absolute == NULL) {
624 0 : return NT_STATUS_NO_MEMORY;
625 : }
626 :
627 : /*
628 : * We're doing the "below connection_path" here because it's
629 : * cheap. It might be that we get a symlink out of the share,
630 : * pointing to yet another symlink getting us back into the
631 : * share. If we need that, we would have to remove the check
632 : * here.
633 : */
634 2361 : ok = subdir_of(
635 : connection_path,
636 : connection_path_len,
637 : absolute,
638 : &relative);
639 2361 : if (!ok) {
640 371 : DBG_NOTICE("Bad access attempt: %s is a symlink "
641 : "outside the share path\n"
642 : "conn_rootdir =%s\n"
643 : "resolved_name=%s\n",
644 : symlink_name->base_name,
645 : connection_path,
646 : absolute);
647 371 : TALLOC_FREE(absolute);
648 371 : return NT_STATUS_OBJECT_NAME_NOT_FOUND;
649 : }
650 :
651 1990 : if (relative[0] == '\0') {
652 : /*
653 : * special case symlink to share root: "." is our
654 : * share root filename
655 : */
656 22 : absolute[0] = '.';
657 22 : absolute[1] = '\0';
658 : } else {
659 1968 : memmove(absolute, relative, strlen(relative)+1);
660 : }
661 :
662 1990 : *_target = absolute;
663 1990 : return NT_STATUS_OK;
664 : }
665 :
666 : /****************************************************************************
667 : Non-widelink open.
668 : ****************************************************************************/
669 :
670 3896345 : static NTSTATUS non_widelink_open(const struct files_struct *dirfsp,
671 : files_struct *fsp,
672 : struct smb_filename *smb_fname,
673 : const struct vfs_open_how *_how)
674 : {
675 3896345 : struct connection_struct *conn = fsp->conn;
676 3896345 : const char *connpath = SMB_VFS_CONNECTPATH(conn, dirfsp, smb_fname);
677 12000 : size_t connpath_len;
678 3896345 : NTSTATUS status = NT_STATUS_OK;
679 3896345 : int fd = -1;
680 3896345 : char *orig_smb_fname_base = smb_fname->base_name;
681 3896345 : struct smb_filename *orig_fsp_name = fsp->fsp_name;
682 3896345 : struct smb_filename *smb_fname_rel = NULL;
683 3896345 : struct smb_filename *oldwd_fname = NULL;
684 3896345 : struct smb_filename *parent_dir_fname = NULL;
685 3896345 : struct vfs_open_how how = *_how;
686 3896345 : char *target = NULL;
687 3896345 : size_t link_depth = 0;
688 12000 : int ret;
689 :
690 3896345 : SMB_ASSERT(!fsp_is_alternate_stream(fsp));
691 :
692 3896345 : if (connpath == NULL) {
693 : /*
694 : * This can happen with shadow_copy2 if the snapshot
695 : * path is not found
696 : */
697 4 : return NT_STATUS_OBJECT_NAME_NOT_FOUND;
698 : }
699 3896341 : connpath_len = strlen(connpath);
700 :
701 3898331 : again:
702 3898331 : if (smb_fname->base_name[0] == '/') {
703 1435316 : int cmp = strcmp(connpath, smb_fname->base_name);
704 1435316 : if (cmp == 0) {
705 1117235 : smb_fname->base_name = talloc_strdup(smb_fname, "");
706 1117235 : if (smb_fname->base_name == NULL) {
707 0 : status = NT_STATUS_NO_MEMORY;
708 0 : goto out;
709 : }
710 : }
711 : }
712 :
713 3898331 : if (dirfsp == conn->cwd_fsp) {
714 :
715 3419596 : status = SMB_VFS_PARENT_PATHNAME(fsp->conn,
716 : talloc_tos(),
717 : smb_fname,
718 : &parent_dir_fname,
719 : &smb_fname_rel);
720 3419596 : if (!NT_STATUS_IS_OK(status)) {
721 0 : goto out;
722 : }
723 :
724 3419596 : status = chdir_below_conn(
725 : talloc_tos(),
726 : conn,
727 : connpath,
728 : connpath_len,
729 : parent_dir_fname,
730 : &oldwd_fname);
731 3419596 : if (!NT_STATUS_IS_OK(status)) {
732 146334 : goto out;
733 : }
734 :
735 : /* Setup fsp->fsp_name to be relative to cwd */
736 3273262 : fsp->fsp_name = smb_fname_rel;
737 : } else {
738 : /*
739 : * fsp->fsp_name is unchanged as it is already correctly
740 : * relative to conn->cwd.
741 : */
742 478735 : smb_fname_rel = smb_fname;
743 : }
744 :
745 : {
746 : /*
747 : * Assert nobody can step in with a symlink on the
748 : * path, there is no path anymore and we'll use
749 : * O_NOFOLLOW to open.
750 : */
751 3751997 : char *slash = strchr_m(smb_fname_rel->base_name, '/');
752 3751997 : SMB_ASSERT(slash == NULL);
753 : }
754 :
755 3751997 : how.flags |= O_NOFOLLOW;
756 :
757 3751997 : fd = SMB_VFS_OPENAT(conn,
758 : dirfsp,
759 : smb_fname_rel,
760 : fsp,
761 : &how);
762 3751997 : fsp_set_fd(fsp, fd); /* This preserves errno */
763 :
764 3751997 : if (fd == -1) {
765 1365802 : status = map_nt_error_from_unix(errno);
766 :
767 1365802 : if (errno == ENOENT) {
768 1364697 : goto out;
769 : }
770 :
771 : /*
772 : * ENOENT makes it worthless retrying with a
773 : * stat, we know for sure the file does not
774 : * exist. For everything else we want to know
775 : * what's there.
776 : */
777 1105 : ret = SMB_VFS_FSTATAT(
778 : fsp->conn,
779 : dirfsp,
780 : smb_fname_rel,
781 : &fsp->fsp_name->st,
782 : AT_SYMLINK_NOFOLLOW);
783 :
784 1105 : if (ret == -1) {
785 : /*
786 : * Keep the original error. Otherwise we would
787 : * mask for example EROFS for open(O_CREAT),
788 : * turning it into ENOENT.
789 : */
790 45 : goto out;
791 : }
792 : } else {
793 2386195 : ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
794 : }
795 :
796 2387255 : if (ret == -1) {
797 0 : status = map_nt_error_from_unix(errno);
798 0 : DBG_DEBUG("fstat[at](%s) failed: %s\n",
799 : smb_fname_str_dbg(smb_fname),
800 : strerror(errno));
801 0 : goto out;
802 : }
803 :
804 2387255 : fsp->fsp_flags.is_directory = S_ISDIR(fsp->fsp_name->st.st_ex_mode);
805 2387255 : orig_fsp_name->st = fsp->fsp_name->st;
806 :
807 2387255 : if (!S_ISLNK(fsp->fsp_name->st.st_ex_mode)) {
808 2384842 : goto out;
809 : }
810 :
811 : /*
812 : * Found a symlink to follow in user space
813 : */
814 :
815 2413 : if (fsp->fsp_name->flags & SMB_FILENAME_POSIX_PATH) {
816 : /* Never follow symlinks on posix open. */
817 52 : status = NT_STATUS_STOPPED_ON_SYMLINK;
818 52 : goto out;
819 : }
820 2361 : if (!lp_follow_symlinks(SNUM(conn))) {
821 : /* Explicitly no symlinks. */
822 0 : status = NT_STATUS_STOPPED_ON_SYMLINK;
823 0 : goto out;
824 : }
825 :
826 2361 : link_depth += 1;
827 2361 : if (link_depth >= 40) {
828 0 : status = NT_STATUS_STOPPED_ON_SYMLINK;
829 0 : goto out;
830 : }
831 :
832 2361 : fsp->fsp_name = orig_fsp_name;
833 :
834 2361 : status = symlink_target_below_conn(
835 : talloc_tos(),
836 : connpath,
837 : connpath_len,
838 : fsp,
839 : discard_const_p(files_struct, dirfsp),
840 : smb_fname_rel,
841 : &target);
842 :
843 2361 : if (!NT_STATUS_IS_OK(status)) {
844 371 : DBG_DEBUG("symlink_target_below_conn() failed: %s\n",
845 : nt_errstr(status));
846 371 : goto out;
847 : }
848 :
849 : /*
850 : * Close what openat(O_PATH) potentially left behind
851 : */
852 1990 : fd_close(fsp);
853 :
854 1990 : if (smb_fname->base_name != orig_smb_fname_base) {
855 0 : TALLOC_FREE(smb_fname->base_name);
856 : }
857 1990 : smb_fname->base_name = target;
858 :
859 1990 : if (oldwd_fname != NULL) {
860 11 : ret = vfs_ChDir(conn, oldwd_fname);
861 11 : if (ret == -1) {
862 0 : smb_panic("unable to get back to old directory\n");
863 : }
864 11 : TALLOC_FREE(oldwd_fname);
865 : }
866 :
867 : /*
868 : * And do it all again... As smb_fname is not relative to the passed in
869 : * dirfsp anymore, we pass conn->cwd_fsp as dirfsp to
870 : * non_widelink_open() to trigger the chdir(parentdir) logic.
871 : */
872 1990 : dirfsp = conn->cwd_fsp;
873 :
874 1990 : goto again;
875 :
876 3896341 : out:
877 3896341 : fsp->fsp_name = orig_fsp_name;
878 3896341 : smb_fname->base_name = orig_smb_fname_base;
879 :
880 3896341 : TALLOC_FREE(parent_dir_fname);
881 :
882 3896341 : if (!NT_STATUS_IS_OK(status)) {
883 1511531 : fd_close(fsp);
884 : }
885 :
886 3896341 : if (oldwd_fname != NULL) {
887 595425 : ret = vfs_ChDir(conn, oldwd_fname);
888 595425 : if (ret == -1) {
889 0 : smb_panic("unable to get back to old directory\n");
890 : }
891 595425 : TALLOC_FREE(oldwd_fname);
892 : }
893 3896341 : return status;
894 : }
895 :
896 : /****************************************************************************
897 : fd support routines - attempt to do a dos_open.
898 : ****************************************************************************/
899 :
900 3907996 : NTSTATUS fd_openat(const struct files_struct *dirfsp,
901 : struct smb_filename *smb_fname,
902 : files_struct *fsp,
903 : const struct vfs_open_how *_how)
904 : {
905 3907996 : struct vfs_open_how how = *_how;
906 3907996 : struct connection_struct *conn = fsp->conn;
907 3907996 : NTSTATUS status = NT_STATUS_OK;
908 3907996 : bool fsp_is_stream = fsp_is_alternate_stream(fsp);
909 3907996 : bool smb_fname_is_stream = is_named_stream(smb_fname);
910 :
911 3907996 : SMB_ASSERT(fsp_is_stream == smb_fname_is_stream);
912 :
913 : /*
914 : * Never follow symlinks on a POSIX client. The
915 : * client should be doing this.
916 : */
917 :
918 3907996 : if ((fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) || !lp_follow_symlinks(SNUM(conn))) {
919 4489 : how.flags |= O_NOFOLLOW;
920 : }
921 :
922 3907996 : if (fsp_is_stream) {
923 5 : int fd;
924 :
925 11651 : fd = SMB_VFS_OPENAT(
926 : conn,
927 : NULL, /* stream open is relative to fsp->base_fsp */
928 : smb_fname,
929 : fsp,
930 : &how);
931 11651 : if (fd == -1) {
932 4157 : status = map_nt_error_from_unix(errno);
933 : }
934 11651 : fsp_set_fd(fsp, fd);
935 :
936 11651 : if (fd != -1) {
937 7494 : status = vfs_stat_fsp(fsp);
938 7494 : if (!NT_STATUS_IS_OK(status)) {
939 0 : DBG_DEBUG("vfs_stat_fsp failed: %s\n",
940 : nt_errstr(status));
941 0 : fd_close(fsp);
942 : }
943 : }
944 :
945 11651 : return status;
946 : }
947 :
948 : /*
949 : * Only follow symlinks within a share
950 : * definition.
951 : */
952 3896345 : status = non_widelink_open(dirfsp, fsp, smb_fname, &how);
953 3896345 : if (!NT_STATUS_IS_OK(status)) {
954 1511535 : if (NT_STATUS_EQUAL(status, NT_STATUS_TOO_MANY_OPENED_FILES)) {
955 0 : static time_t last_warned = 0L;
956 :
957 10 : if (time((time_t *) NULL) > last_warned) {
958 2 : DEBUG(0,("Too many open files, unable "
959 : "to open more! smbd's max "
960 : "open files = %d\n",
961 : lp_max_open_files()));
962 2 : last_warned = time((time_t *) NULL);
963 : }
964 : }
965 :
966 1511535 : DBG_DEBUG("name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
967 : smb_fname_str_dbg(smb_fname),
968 : how.flags,
969 : (int)how.mode,
970 : fsp_get_pathref_fd(fsp),
971 : nt_errstr(status));
972 1511535 : return status;
973 : }
974 :
975 2384810 : DBG_DEBUG("name %s, flags = 0%o mode = 0%o, fd = %d\n",
976 : smb_fname_str_dbg(smb_fname),
977 : how.flags,
978 : (int)how.mode,
979 : fsp_get_pathref_fd(fsp));
980 :
981 2384810 : return status;
982 : }
983 :
984 : /****************************************************************************
985 : Close the file associated with a fsp.
986 : ****************************************************************************/
987 :
988 7381307 : NTSTATUS fd_close(files_struct *fsp)
989 : {
990 35719 : NTSTATUS status;
991 35719 : int ret;
992 :
993 7381307 : if (fsp == fsp->conn->cwd_fsp) {
994 0 : return NT_STATUS_OK;
995 : }
996 :
997 7381307 : if (fsp->fsp_flags.fstat_before_close) {
998 34 : status = vfs_stat_fsp(fsp);
999 34 : if (!NT_STATUS_IS_OK(status)) {
1000 : /*
1001 : * If this is a stream and delete-on-close was set, the
1002 : * backing object (an xattr from streams_xattr) might
1003 : * already be deleted so fstat() fails with
1004 : * NT_STATUS_NOT_FOUND. So if fsp refers to a stream we
1005 : * ignore the error and only bail for normal files where
1006 : * an fstat() should still work. NB. We cannot use
1007 : * fsp_is_alternate_stream(fsp) for this as the base_fsp
1008 : * has already been closed at this point and so the value
1009 : * fsp_is_alternate_stream() checks for is already NULL.
1010 : */
1011 2 : if (fsp->fsp_name->stream_name == NULL) {
1012 0 : return status;
1013 : }
1014 : }
1015 : }
1016 :
1017 7381307 : if (fsp->dptr) {
1018 18534 : dptr_CloseDir(fsp);
1019 : }
1020 7381307 : if (fsp_get_pathref_fd(fsp) == -1) {
1021 : /*
1022 : * Either a directory where the dptr_CloseDir() already closed
1023 : * the fd or a stat open.
1024 : */
1025 3314511 : return NT_STATUS_OK;
1026 : }
1027 4066796 : if (fh_get_refcount(fsp->fh) > 1) {
1028 113 : return NT_STATUS_OK; /* Shared handle. Only close last reference. */
1029 : }
1030 :
1031 4066683 : ret = SMB_VFS_CLOSE(fsp);
1032 4066683 : fsp_set_fd(fsp, -1);
1033 4066683 : if (ret == -1) {
1034 0 : return map_nt_error_from_unix(errno);
1035 : }
1036 4066683 : return NT_STATUS_OK;
1037 : }
1038 :
1039 : /****************************************************************************
1040 : Change the ownership of a file to that of the parent directory.
1041 : Do this by fd if possible.
1042 : ****************************************************************************/
1043 :
1044 8 : static void change_file_owner_to_parent_fsp(struct files_struct *parent_fsp,
1045 : struct files_struct *fsp)
1046 : {
1047 0 : int ret;
1048 :
1049 8 : if (parent_fsp->fsp_name->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
1050 : /* Already this uid - no need to change. */
1051 0 : DBG_DEBUG("file %s is already owned by uid %u\n",
1052 : fsp_str_dbg(fsp),
1053 : (unsigned int)fsp->fsp_name->st.st_ex_uid);
1054 0 : return;
1055 : }
1056 :
1057 8 : become_root();
1058 8 : ret = SMB_VFS_FCHOWN(fsp,
1059 : parent_fsp->fsp_name->st.st_ex_uid,
1060 : (gid_t)-1);
1061 8 : unbecome_root();
1062 8 : if (ret == -1) {
1063 0 : DBG_ERR("failed to fchown "
1064 : "file %s to parent directory uid %u. Error "
1065 : "was %s\n",
1066 : fsp_str_dbg(fsp),
1067 : (unsigned int)parent_fsp->fsp_name->st.st_ex_uid,
1068 : strerror(errno));
1069 : } else {
1070 8 : DBG_DEBUG("changed new file %s to "
1071 : "parent directory uid %u.\n",
1072 : fsp_str_dbg(fsp),
1073 : (unsigned int)parent_fsp->fsp_name->st.st_ex_uid);
1074 : /* Ensure the uid entry is updated. */
1075 8 : fsp->fsp_name->st.st_ex_uid =
1076 8 : parent_fsp->fsp_name->st.st_ex_uid;
1077 : }
1078 : }
1079 :
1080 8 : static NTSTATUS change_dir_owner_to_parent_fsp(struct files_struct *parent_fsp,
1081 : struct files_struct *fsp)
1082 : {
1083 0 : NTSTATUS status;
1084 0 : int ret;
1085 :
1086 8 : if (parent_fsp->fsp_name->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
1087 : /* Already this uid - no need to change. */
1088 0 : DBG_DEBUG("directory %s is already owned by uid %u\n",
1089 : fsp_str_dbg(fsp),
1090 : (unsigned int)fsp->fsp_name->st.st_ex_uid);
1091 0 : return NT_STATUS_OK;
1092 : }
1093 :
1094 8 : become_root();
1095 8 : ret = SMB_VFS_FCHOWN(fsp,
1096 : parent_fsp->fsp_name->st.st_ex_uid,
1097 : (gid_t)-1);
1098 8 : unbecome_root();
1099 8 : if (ret == -1) {
1100 0 : status = map_nt_error_from_unix(errno);
1101 0 : DBG_ERR("failed to chown "
1102 : "directory %s to parent directory uid %u. "
1103 : "Error was %s\n",
1104 : fsp_str_dbg(fsp),
1105 : (unsigned int)parent_fsp->fsp_name->st.st_ex_uid,
1106 : nt_errstr(status));
1107 0 : return status;
1108 : }
1109 :
1110 8 : DBG_DEBUG("changed ownership of new "
1111 : "directory %s to parent directory uid %u.\n",
1112 : fsp_str_dbg(fsp),
1113 : (unsigned int)parent_fsp->fsp_name->st.st_ex_uid);
1114 :
1115 : /* Ensure the uid entry is updated. */
1116 8 : fsp->fsp_name->st.st_ex_uid = parent_fsp->fsp_name->st.st_ex_uid;
1117 :
1118 8 : return NT_STATUS_OK;
1119 : }
1120 :
1121 : /****************************************************************************
1122 : Open a file - returning a guaranteed ATOMIC indication of if the
1123 : file was created or not.
1124 : ****************************************************************************/
1125 :
1126 161892 : static NTSTATUS fd_open_atomic(struct files_struct *dirfsp,
1127 : struct smb_filename *smb_fname,
1128 : files_struct *fsp,
1129 : const struct vfs_open_how *_how,
1130 : bool *file_created)
1131 : {
1132 161892 : struct vfs_open_how how = *_how;
1133 161892 : NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1134 279 : NTSTATUS retry_status;
1135 161892 : bool file_existed = VALID_STAT(smb_fname->st);
1136 :
1137 161892 : if (!(how.flags & O_CREAT)) {
1138 : /*
1139 : * We're not creating the file, just pass through.
1140 : */
1141 1040 : status = fd_openat(dirfsp, smb_fname, fsp, &how);
1142 1040 : *file_created = false;
1143 1040 : return status;
1144 : }
1145 :
1146 160852 : if (how.flags & O_EXCL) {
1147 : /*
1148 : * Fail if already exists, just pass through.
1149 : */
1150 131279 : status = fd_openat(dirfsp, smb_fname, fsp, &how);
1151 :
1152 : /*
1153 : * Here we've opened with O_CREAT|O_EXCL. If that went
1154 : * NT_STATUS_OK, we *know* we created this file.
1155 : */
1156 131279 : *file_created = NT_STATUS_IS_OK(status);
1157 :
1158 131279 : return status;
1159 : }
1160 :
1161 : /*
1162 : * Now it gets tricky. We have O_CREAT, but not O_EXCL.
1163 : * To know absolutely if we created the file or not,
1164 : * we can never call O_CREAT without O_EXCL. So if
1165 : * we think the file existed, try without O_CREAT|O_EXCL.
1166 : * If we think the file didn't exist, try with
1167 : * O_CREAT|O_EXCL.
1168 : *
1169 : * The big problem here is dangling symlinks. Opening
1170 : * without O_NOFOLLOW means both bad symlink
1171 : * and missing path return -1, ENOENT from open(). As POSIX
1172 : * is pathname based it's not possible to tell
1173 : * the difference between these two cases in a
1174 : * non-racy way, so change to try only two attempts before
1175 : * giving up.
1176 : *
1177 : * We don't have this problem for the O_NOFOLLOW
1178 : * case as it just returns NT_STATUS_OBJECT_PATH_NOT_FOUND
1179 : * mapped from the ELOOP POSIX error.
1180 : */
1181 :
1182 29573 : if (file_existed) {
1183 176 : how.flags = _how->flags & ~(O_CREAT);
1184 176 : retry_status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1185 : } else {
1186 29397 : how.flags = _how->flags | O_EXCL;
1187 29397 : retry_status = NT_STATUS_OBJECT_NAME_COLLISION;
1188 : }
1189 :
1190 29573 : status = fd_openat(dirfsp, smb_fname, fsp, &how);
1191 29573 : if (NT_STATUS_IS_OK(status)) {
1192 29567 : *file_created = !file_existed;
1193 29567 : return NT_STATUS_OK;
1194 : }
1195 6 : if (NT_STATUS_EQUAL(status, retry_status)) {
1196 :
1197 4 : file_existed = !file_existed;
1198 :
1199 4 : DBG_DEBUG("File %s %s. Retry.\n",
1200 : fsp_str_dbg(fsp),
1201 : file_existed ? "existed" : "did not exist");
1202 :
1203 4 : if (file_existed) {
1204 4 : how.flags = _how->flags & ~(O_CREAT);
1205 : } else {
1206 0 : how.flags = _how->flags | O_EXCL;
1207 : }
1208 :
1209 4 : status = fd_openat(dirfsp, smb_fname, fsp, &how);
1210 : }
1211 :
1212 6 : *file_created = (NT_STATUS_IS_OK(status) && !file_existed);
1213 6 : return status;
1214 : }
1215 :
1216 215075 : static NTSTATUS reopen_from_fsp(struct files_struct *dirfsp,
1217 : struct smb_filename *smb_fname,
1218 : struct files_struct *fsp,
1219 : const struct vfs_open_how *how,
1220 : bool *p_file_created)
1221 : {
1222 677 : NTSTATUS status;
1223 677 : int old_fd;
1224 :
1225 267860 : if (fsp->fsp_flags.have_proc_fds &&
1226 53183 : ((old_fd = fsp_get_pathref_fd(fsp)) != -1)) {
1227 :
1228 398 : struct sys_proc_fd_path_buf buf;
1229 106366 : struct smb_filename proc_fname = (struct smb_filename){
1230 53183 : .base_name = sys_proc_fd_path(old_fd, &buf),
1231 : };
1232 53183 : mode_t mode = fsp->fsp_name->st.st_ex_mode;
1233 398 : int new_fd;
1234 :
1235 53183 : SMB_ASSERT(fsp->fsp_flags.is_pathref);
1236 :
1237 53183 : if (S_ISLNK(mode)) {
1238 0 : return NT_STATUS_STOPPED_ON_SYMLINK;
1239 : }
1240 53183 : if (!(S_ISREG(mode) || S_ISDIR(mode))) {
1241 0 : return NT_STATUS_IO_REPARSE_TAG_NOT_HANDLED;
1242 : }
1243 :
1244 53183 : fsp->fsp_flags.is_pathref = false;
1245 :
1246 53183 : new_fd = SMB_VFS_OPENAT(fsp->conn,
1247 : fsp->conn->cwd_fsp,
1248 : &proc_fname,
1249 : fsp,
1250 : how);
1251 53183 : if (new_fd == -1) {
1252 22 : status = map_nt_error_from_unix(errno);
1253 22 : fd_close(fsp);
1254 22 : return status;
1255 : }
1256 :
1257 53161 : status = fd_close(fsp);
1258 53161 : if (!NT_STATUS_IS_OK(status)) {
1259 0 : return status;
1260 : }
1261 :
1262 53161 : fsp_set_fd(fsp, new_fd);
1263 53161 : return NT_STATUS_OK;
1264 : }
1265 :
1266 : /*
1267 : * Close the existing pathref fd and set the fsp flag
1268 : * is_pathref to false so we get a "normal" fd this time.
1269 : */
1270 161892 : status = fd_close(fsp);
1271 161892 : if (!NT_STATUS_IS_OK(status)) {
1272 0 : return status;
1273 : }
1274 :
1275 161892 : fsp->fsp_flags.is_pathref = false;
1276 :
1277 161892 : status = fd_open_atomic(dirfsp, smb_fname, fsp, how, p_file_created);
1278 161892 : return status;
1279 : }
1280 :
1281 : /****************************************************************************
1282 : Open a file.
1283 : ****************************************************************************/
1284 :
1285 402884 : static NTSTATUS open_file(
1286 : struct smb_request *req,
1287 : struct files_struct *dirfsp,
1288 : struct smb_filename *smb_fname_atname,
1289 : files_struct *fsp,
1290 : const struct vfs_open_how *_how,
1291 : uint32_t access_mask, /* client requested access mask. */
1292 : uint32_t open_access_mask, /* what we're actually using in the open. */
1293 : uint32_t private_flags,
1294 : bool *p_file_created)
1295 : {
1296 402884 : connection_struct *conn = fsp->conn;
1297 402884 : struct smb_filename *smb_fname = fsp->fsp_name;
1298 402884 : struct vfs_open_how how = *_how;
1299 402884 : NTSTATUS status = NT_STATUS_OK;
1300 402884 : bool file_existed = VALID_STAT(fsp->fsp_name->st);
1301 402884 : const uint32_t need_fd_mask =
1302 : FILE_READ_DATA |
1303 : FILE_WRITE_DATA |
1304 : FILE_APPEND_DATA |
1305 : FILE_EXECUTE |
1306 : SEC_FLAG_SYSTEM_SECURITY;
1307 402884 : bool creating = !file_existed && (how.flags & O_CREAT);
1308 402884 : bool open_fd = false;
1309 402884 : bool posix_open = (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN);
1310 :
1311 : /*
1312 : * Catch early an attempt to open an existing
1313 : * directory as a file.
1314 : */
1315 402884 : if (file_existed && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
1316 34497 : return NT_STATUS_FILE_IS_A_DIRECTORY;
1317 : }
1318 :
1319 : /*
1320 : * This little piece of insanity is inspired by the
1321 : * fact that an NT client can open a file for O_RDONLY,
1322 : * but set the create disposition to FILE_EXISTS_TRUNCATE.
1323 : * If the client *can* write to the file, then it expects to
1324 : * truncate the file, even though it is opening for readonly.
1325 : * Quicken uses this stupid trick in backup file creation...
1326 : * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
1327 : * for helping track this one down. It didn't bite us in 2.0.x
1328 : * as we always opened files read-write in that release. JRA.
1329 : */
1330 :
1331 368387 : if (((how.flags & O_ACCMODE) == O_RDONLY) && (how.flags & O_TRUNC)) {
1332 170 : DBG_DEBUG("truncate requested on read-only open for file %s\n",
1333 : smb_fname_str_dbg(smb_fname));
1334 170 : how.flags = (how.flags & ~O_ACCMODE) | O_RDWR;
1335 : }
1336 :
1337 : /* Check permissions */
1338 :
1339 : /*
1340 : * This code was changed after seeing a client open request
1341 : * containing the open mode of (DENY_WRITE/read-only) with
1342 : * the 'create if not exist' bit set. The previous code
1343 : * would fail to open the file read only on a read-only share
1344 : * as it was checking the flags parameter directly against O_RDONLY,
1345 : * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
1346 : * JRA.
1347 : */
1348 :
1349 368387 : if (!CAN_WRITE(conn)) {
1350 : /* It's a read-only share - fail if we wanted to write. */
1351 0 : if ((how.flags & O_ACCMODE) != O_RDONLY ||
1352 0 : (how.flags & O_TRUNC) || (how.flags & O_APPEND)) {
1353 0 : DEBUG(3,("Permission denied opening %s\n",
1354 : smb_fname_str_dbg(smb_fname)));
1355 0 : return NT_STATUS_ACCESS_DENIED;
1356 : }
1357 : /*
1358 : * We don't want to write - but we must make sure that
1359 : * O_CREAT doesn't create the file if we have write
1360 : * access into the directory.
1361 : */
1362 0 : how.flags &= ~(O_CREAT | O_EXCL);
1363 : }
1364 :
1365 368387 : if ((open_access_mask & need_fd_mask) || creating ||
1366 176812 : (how.flags & O_TRUNC)) {
1367 191575 : open_fd = true;
1368 : }
1369 :
1370 368387 : if (open_fd) {
1371 451 : int ret;
1372 :
1373 : #if defined(O_NONBLOCK) && defined(S_ISFIFO)
1374 : /*
1375 : * We would block on opening a FIFO with no one else on the
1376 : * other end. Do what we used to do and add O_NONBLOCK to the
1377 : * open flags. JRA.
1378 : */
1379 :
1380 191575 : if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
1381 0 : how.flags |= O_NONBLOCK;
1382 : }
1383 : #endif
1384 :
1385 191575 : if (!posix_open) {
1386 190873 : const char *wild = smb_fname->base_name;
1387 : /*
1388 : * Don't open files with Microsoft wildcard characters.
1389 : */
1390 190873 : if (fsp_is_alternate_stream(fsp)) {
1391 : /*
1392 : * wildcard characters are allowed in stream
1393 : * names only test the basefilename
1394 : */
1395 3855 : wild = fsp->base_fsp->fsp_name->base_name;
1396 : }
1397 :
1398 190873 : if (ms_has_wild(wild)) {
1399 0 : return NT_STATUS_OBJECT_NAME_INVALID;
1400 : }
1401 : }
1402 :
1403 : /* Can we access this file ? */
1404 191575 : if (!fsp_is_alternate_stream(fsp)) {
1405 : /* Only do this check on non-stream open. */
1406 187720 : if (file_existed) {
1407 29276 : status = smbd_check_access_rights_fsp(
1408 : dirfsp,
1409 : fsp,
1410 : false,
1411 : open_access_mask);
1412 :
1413 29276 : if (!NT_STATUS_IS_OK(status)) {
1414 465 : DBG_DEBUG("smbd_check_access_rights_fsp"
1415 : " on file %s returned %s\n",
1416 : fsp_str_dbg(fsp),
1417 : nt_errstr(status));
1418 : }
1419 :
1420 29276 : if (!NT_STATUS_IS_OK(status) &&
1421 465 : !NT_STATUS_EQUAL(status,
1422 : NT_STATUS_OBJECT_NAME_NOT_FOUND))
1423 : {
1424 465 : return status;
1425 : }
1426 :
1427 28811 : if (NT_STATUS_EQUAL(status,
1428 : NT_STATUS_OBJECT_NAME_NOT_FOUND))
1429 : {
1430 0 : DEBUG(10, ("open_file: "
1431 : "file %s vanished since we "
1432 : "checked for existence.\n",
1433 : smb_fname_str_dbg(smb_fname)));
1434 0 : file_existed = false;
1435 0 : SET_STAT_INVALID(fsp->fsp_name->st);
1436 : }
1437 : }
1438 :
1439 187255 : if (!file_existed) {
1440 158444 : if (!(how.flags & O_CREAT)) {
1441 : /* File didn't exist and no O_CREAT. */
1442 0 : return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1443 : }
1444 :
1445 158444 : status = check_parent_access_fsp(
1446 : dirfsp,
1447 : SEC_DIR_ADD_FILE);
1448 158444 : if (!NT_STATUS_IS_OK(status)) {
1449 9 : DBG_DEBUG("check_parent_access_fsp on "
1450 : "directory %s for file %s "
1451 : "returned %s\n",
1452 : smb_fname_str_dbg(
1453 : dirfsp->fsp_name),
1454 : smb_fname_str_dbg(smb_fname),
1455 : nt_errstr(status));
1456 9 : return status;
1457 : }
1458 : }
1459 : }
1460 :
1461 : /*
1462 : * Actually do the open - if O_TRUNC is needed handle it
1463 : * below under the share mode lock.
1464 : */
1465 191101 : how.flags &= ~O_TRUNC;
1466 191101 : status = reopen_from_fsp(dirfsp,
1467 : smb_fname_atname,
1468 : fsp,
1469 : &how,
1470 : p_file_created);
1471 191101 : if (NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK)) {
1472 : /*
1473 : * Non-O_PATH reopen that hit a race
1474 : * condition: Someone has put a symlink where
1475 : * we used to have a file. Can't happen with
1476 : * O_PATH and reopening from /proc/self/fd/ or
1477 : * equivalent.
1478 : */
1479 0 : status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1480 : }
1481 191101 : if (!NT_STATUS_IS_OK(status)) {
1482 33 : DBG_NOTICE("Error opening file %s (%s) (in_flags=%d) "
1483 : "(flags=%d)\n",
1484 : smb_fname_str_dbg(smb_fname),
1485 : nt_errstr(status),
1486 : _how->flags,
1487 : how.flags);
1488 33 : return status;
1489 : }
1490 :
1491 191068 : if (how.flags & O_NONBLOCK) {
1492 : /*
1493 : * GPFS can return ETIMEDOUT for pread on
1494 : * nonblocking file descriptors when files
1495 : * migrated to tape need to be recalled. I
1496 : * could imagine this happens elsewhere
1497 : * too. With blocking file descriptors this
1498 : * does not happen.
1499 : */
1500 191068 : ret = vfs_set_blocking(fsp, true);
1501 191068 : if (ret == -1) {
1502 0 : status = map_nt_error_from_unix(errno);
1503 0 : DBG_WARNING("Could not set fd to blocking: "
1504 : "%s\n", strerror(errno));
1505 0 : fd_close(fsp);
1506 0 : return status;
1507 : }
1508 : }
1509 :
1510 191068 : if (*p_file_created) {
1511 : /* We created this file. */
1512 :
1513 160660 : bool need_re_stat = false;
1514 : /* Do all inheritance work after we've
1515 : done a successful fstat call and filled
1516 : in the stat struct in fsp->fsp_name. */
1517 :
1518 : /* Inherit the ACL if required */
1519 160660 : if (lp_inherit_permissions(SNUM(conn))) {
1520 0 : inherit_access_posix_acl(conn,
1521 : dirfsp,
1522 : smb_fname,
1523 : how.mode);
1524 0 : need_re_stat = true;
1525 : }
1526 :
1527 : /* Change the owner if required. */
1528 160660 : if (lp_inherit_owner(SNUM(conn)) != INHERIT_OWNER_NO) {
1529 8 : change_file_owner_to_parent_fsp(dirfsp, fsp);
1530 8 : need_re_stat = true;
1531 : }
1532 :
1533 160660 : if (need_re_stat) {
1534 8 : status = vfs_stat_fsp(fsp);
1535 : /*
1536 : * If we have an fd, this stat should succeed.
1537 : */
1538 8 : if (!NT_STATUS_IS_OK(status)) {
1539 0 : DBG_ERR("Error doing fstat on open "
1540 : "file %s (%s)\n",
1541 : smb_fname_str_dbg(smb_fname),
1542 : nt_errstr(status));
1543 0 : fd_close(fsp);
1544 0 : return status;
1545 : }
1546 : }
1547 :
1548 160660 : notify_fname(conn, NOTIFY_ACTION_ADDED,
1549 : FILE_NOTIFY_CHANGE_FILE_NAME,
1550 160660 : smb_fname->base_name);
1551 : }
1552 : } else {
1553 176812 : if (!file_existed) {
1554 : /* File must exist for a stat open. */
1555 0 : return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1556 : }
1557 :
1558 176812 : if (S_ISLNK(smb_fname->st.st_ex_mode) &&
1559 129 : !posix_open)
1560 : {
1561 : /*
1562 : * Don't allow stat opens on symlinks directly unless
1563 : * it's a POSIX open. Match the return code from
1564 : * openat_pathref_fsp().
1565 : */
1566 3 : return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1567 : }
1568 :
1569 176809 : if (!fsp->fsp_flags.is_pathref) {
1570 : /*
1571 : * There is only one legit case where end up here:
1572 : * openat_pathref_fsp() failed to open a symlink, so the
1573 : * fsp was created by fsp_new() which doesn't set
1574 : * is_pathref. Other than that, we should always have a
1575 : * pathref fsp at this point. The subsequent checks
1576 : * assert this.
1577 : */
1578 8 : if (!(smb_fname->flags & SMB_FILENAME_POSIX_PATH)) {
1579 8 : DBG_ERR("[%s] is not a POSIX pathname\n",
1580 : smb_fname_str_dbg(smb_fname));
1581 8 : return NT_STATUS_INTERNAL_ERROR;
1582 : }
1583 0 : if (!S_ISLNK(smb_fname->st.st_ex_mode)) {
1584 0 : DBG_ERR("[%s] is not a symlink\n",
1585 : smb_fname_str_dbg(smb_fname));
1586 0 : return NT_STATUS_INTERNAL_ERROR;
1587 : }
1588 0 : if (fsp_get_pathref_fd(fsp) != -1) {
1589 0 : DBG_ERR("fd for [%s] is not -1: fd [%d]\n",
1590 : smb_fname_str_dbg(smb_fname),
1591 : fsp_get_pathref_fd(fsp));
1592 0 : return NT_STATUS_INTERNAL_ERROR;
1593 : }
1594 : }
1595 :
1596 : /*
1597 : * Access to streams is checked by checking the basefile and
1598 : * that has already been checked by check_base_file_access()
1599 : * in create_file_unixpath().
1600 : */
1601 176801 : if (!fsp_is_alternate_stream(fsp)) {
1602 175135 : status = smbd_check_access_rights_fsp(dirfsp,
1603 : fsp,
1604 : false,
1605 : open_access_mask);
1606 :
1607 175135 : if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
1608 0 : posix_open &&
1609 0 : S_ISLNK(smb_fname->st.st_ex_mode)) {
1610 : /* This is a POSIX stat open for delete
1611 : * or rename on a symlink that points
1612 : * nowhere. Allow. */
1613 0 : DEBUG(10,("open_file: allowing POSIX "
1614 : "open on bad symlink %s\n",
1615 : smb_fname_str_dbg(smb_fname)));
1616 0 : status = NT_STATUS_OK;
1617 : }
1618 :
1619 175135 : if (!NT_STATUS_IS_OK(status)) {
1620 100 : DBG_DEBUG("smbd_check_access_rights_fsp on file "
1621 : "%s returned %s\n",
1622 : fsp_str_dbg(fsp),
1623 : nt_errstr(status));
1624 100 : return status;
1625 : }
1626 : }
1627 : }
1628 :
1629 367769 : fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1630 367769 : fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
1631 367769 : fsp->file_pid = req ? req->smbpid : 0;
1632 367769 : fsp->fsp_flags.can_lock = true;
1633 367769 : fsp->fsp_flags.can_read = ((access_mask & FILE_READ_DATA) != 0);
1634 368412 : fsp->fsp_flags.can_write =
1635 734895 : CAN_WRITE(conn) &&
1636 367769 : ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
1637 367769 : fsp->print_file = NULL;
1638 367769 : fsp->fsp_flags.modified = false;
1639 367769 : fsp->sent_oplock_break = NO_BREAK_SENT;
1640 367769 : fsp->fsp_flags.is_directory = false;
1641 733987 : if (is_in_path(smb_fname->base_name,
1642 : conn->aio_write_behind_list,
1643 366218 : posix_open ? true : conn->case_sensitive)) {
1644 0 : fsp->fsp_flags.aio_write_behind = true;
1645 : }
1646 :
1647 367769 : DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
1648 : conn->session_info->unix_info->unix_name,
1649 : smb_fname_str_dbg(smb_fname),
1650 : BOOLSTR(fsp->fsp_flags.can_read),
1651 : BOOLSTR(fsp->fsp_flags.can_write),
1652 : conn->num_files_open));
1653 :
1654 367769 : return NT_STATUS_OK;
1655 : }
1656 :
1657 45348 : static bool mask_conflict(
1658 : uint32_t new_access,
1659 : uint32_t existing_access,
1660 : uint32_t access_mask,
1661 : uint32_t new_sharemode,
1662 : uint32_t existing_sharemode,
1663 : uint32_t sharemode_mask)
1664 : {
1665 45348 : bool want_access = (new_access & access_mask);
1666 45348 : bool allow_existing = (existing_sharemode & sharemode_mask);
1667 45348 : bool have_access = (existing_access & access_mask);
1668 45348 : bool allow_new = (new_sharemode & sharemode_mask);
1669 :
1670 45348 : if (want_access && !allow_existing) {
1671 16504 : DBG_DEBUG("Access request 0x%"PRIx32"/0x%"PRIx32" conflicts "
1672 : "with existing sharemode 0x%"PRIx32"/0x%"PRIx32"\n",
1673 : new_access,
1674 : access_mask,
1675 : existing_sharemode,
1676 : sharemode_mask);
1677 16504 : return true;
1678 : }
1679 28844 : if (have_access && !allow_new) {
1680 4308 : DBG_DEBUG("Sharemode request 0x%"PRIx32"/0x%"PRIx32" conflicts "
1681 : "with existing access 0x%"PRIx32"/0x%"PRIx32"\n",
1682 : new_sharemode,
1683 : sharemode_mask,
1684 : existing_access,
1685 : access_mask);
1686 4308 : return true;
1687 : }
1688 24386 : return false;
1689 : }
1690 :
1691 : /****************************************************************************
1692 : Check if we can open a file with a share mode.
1693 : Returns True if conflict, False if not.
1694 : ****************************************************************************/
1695 :
1696 : static const uint32_t conflicting_access =
1697 : FILE_WRITE_DATA|
1698 : FILE_APPEND_DATA|
1699 : FILE_READ_DATA|
1700 : FILE_EXECUTE|
1701 : DELETE_ACCESS;
1702 :
1703 401719 : static bool share_conflict(uint32_t e_access_mask,
1704 : uint32_t e_share_access,
1705 : uint32_t access_mask,
1706 : uint32_t share_access)
1707 : {
1708 933 : bool conflict;
1709 :
1710 401719 : DBG_DEBUG("existing access_mask = 0x%"PRIx32", "
1711 : "existing share access = 0x%"PRIx32", "
1712 : "access_mask = 0x%"PRIx32", "
1713 : "share_access = 0x%"PRIx32"\n",
1714 : e_access_mask,
1715 : e_share_access,
1716 : access_mask,
1717 : share_access);
1718 :
1719 401719 : if ((e_access_mask & conflicting_access) == 0) {
1720 383919 : DBG_DEBUG("No conflict due to "
1721 : "existing access_mask = 0x%"PRIx32"\n",
1722 : e_access_mask);
1723 383919 : return false;
1724 : }
1725 17800 : if ((access_mask & conflicting_access) == 0) {
1726 2684 : DBG_DEBUG("No conflict due to access_mask = 0x%"PRIx32"\n",
1727 : access_mask);
1728 2684 : return false;
1729 : }
1730 :
1731 15116 : conflict = mask_conflict(
1732 : access_mask, e_access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1733 : share_access, e_share_access, FILE_SHARE_WRITE);
1734 15116 : conflict |= mask_conflict(
1735 : access_mask, e_access_mask, FILE_READ_DATA | FILE_EXECUTE,
1736 : share_access, e_share_access, FILE_SHARE_READ);
1737 15116 : conflict |= mask_conflict(
1738 : access_mask, e_access_mask, DELETE_ACCESS,
1739 : share_access, e_share_access, FILE_SHARE_DELETE);
1740 :
1741 15116 : DBG_DEBUG("conflict=%s\n", conflict ? "true" : "false");
1742 15033 : return conflict;
1743 : }
1744 :
1745 : #if defined(DEVELOPER)
1746 :
1747 : struct validate_my_share_entries_state {
1748 : struct smbd_server_connection *sconn;
1749 : struct file_id fid;
1750 : struct server_id self;
1751 : };
1752 :
1753 24842 : static bool validate_my_share_entries_fn(
1754 : struct share_mode_entry *e,
1755 : bool *modified,
1756 : void *private_data)
1757 : {
1758 24842 : struct validate_my_share_entries_state *state = private_data;
1759 85 : files_struct *fsp;
1760 :
1761 24842 : if (!server_id_equal(&state->self, &e->pid)) {
1762 9614 : return false;
1763 : }
1764 :
1765 15214 : if (e->op_mid == 0) {
1766 : /* INTERNAL_OPEN_ONLY */
1767 1208 : return false;
1768 : }
1769 :
1770 14004 : fsp = file_find_dif(state->sconn, state->fid, e->share_file_id);
1771 14004 : if (!fsp) {
1772 0 : DBG_ERR("PANIC : %s\n",
1773 : share_mode_str(talloc_tos(), 0, &state->fid, e));
1774 0 : smb_panic("validate_my_share_entries: Cannot match a "
1775 : "share entry with an open file\n");
1776 : }
1777 :
1778 14004 : if (((uint16_t)fsp->oplock_type) != e->op_type) {
1779 0 : goto panic;
1780 : }
1781 :
1782 13935 : return false;
1783 :
1784 0 : panic:
1785 : {
1786 0 : char *str;
1787 0 : DBG_ERR("validate_my_share_entries: PANIC : %s\n",
1788 : share_mode_str(talloc_tos(), 0, &state->fid, e));
1789 0 : str = talloc_asprintf(talloc_tos(),
1790 : "validate_my_share_entries: "
1791 : "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1792 0 : fsp->fsp_name->base_name,
1793 0 : (unsigned int)fsp->oplock_type,
1794 0 : (unsigned int)e->op_type);
1795 0 : smb_panic(str);
1796 : }
1797 :
1798 : return false;
1799 : }
1800 : #endif
1801 :
1802 : /**
1803 : * Allowed access mask for stat opens relevant to oplocks
1804 : **/
1805 1106574 : bool is_oplock_stat_open(uint32_t access_mask)
1806 : {
1807 1106574 : const uint32_t stat_open_bits =
1808 : (SYNCHRONIZE_ACCESS|
1809 : FILE_READ_ATTRIBUTES|
1810 : FILE_WRITE_ATTRIBUTES);
1811 :
1812 1634926 : return (((access_mask & stat_open_bits) != 0) &&
1813 529469 : ((access_mask & ~stat_open_bits) == 0));
1814 : }
1815 :
1816 : /**
1817 : * Allowed access mask for stat opens relevant to leases
1818 : **/
1819 496 : bool is_lease_stat_open(uint32_t access_mask)
1820 : {
1821 496 : const uint32_t stat_open_bits =
1822 : (SYNCHRONIZE_ACCESS|
1823 : FILE_READ_ATTRIBUTES|
1824 : FILE_WRITE_ATTRIBUTES|
1825 : READ_CONTROL_ACCESS);
1826 :
1827 948 : return (((access_mask & stat_open_bits) != 0) &&
1828 452 : ((access_mask & ~stat_open_bits) == 0));
1829 : }
1830 :
1831 : struct has_delete_on_close_state {
1832 : bool ret;
1833 : };
1834 :
1835 158 : static bool has_delete_on_close_fn(
1836 : struct share_mode_entry *e,
1837 : bool *modified,
1838 : void *private_data)
1839 : {
1840 158 : struct has_delete_on_close_state *state = private_data;
1841 158 : state->ret = !share_entry_stale_pid(e);
1842 158 : return state->ret;
1843 : }
1844 :
1845 449980 : static bool has_delete_on_close(struct share_mode_lock *lck,
1846 : uint32_t name_hash)
1847 : {
1848 449980 : struct has_delete_on_close_state state = { .ret = false };
1849 991 : bool ok;
1850 :
1851 449980 : if (!is_delete_on_close_set(lck, name_hash)) {
1852 448837 : return false;
1853 : }
1854 :
1855 158 : ok= share_mode_forall_entries(lck, has_delete_on_close_fn, &state);
1856 158 : if (!ok) {
1857 0 : DBG_DEBUG("share_mode_forall_entries failed\n");
1858 0 : return false;
1859 : }
1860 158 : return state.ret;
1861 : }
1862 :
1863 438576 : static void share_mode_flags_restrict(
1864 : struct share_mode_lock *lck,
1865 : uint32_t access_mask,
1866 : uint32_t share_mode,
1867 : uint32_t lease_type)
1868 : {
1869 938 : uint32_t existing_access_mask, existing_share_mode;
1870 938 : uint32_t existing_lease_type;
1871 :
1872 438576 : share_mode_flags_get(
1873 : lck,
1874 : &existing_access_mask,
1875 : &existing_share_mode,
1876 : &existing_lease_type);
1877 :
1878 438576 : existing_access_mask |= access_mask;
1879 438576 : if (access_mask & conflicting_access) {
1880 374782 : existing_share_mode &= share_mode;
1881 : }
1882 438576 : existing_lease_type |= lease_type;
1883 :
1884 438576 : share_mode_flags_set(
1885 : lck,
1886 : existing_access_mask,
1887 : existing_share_mode,
1888 : existing_lease_type,
1889 : NULL);
1890 438576 : }
1891 :
1892 : /****************************************************************************
1893 : Deal with share modes
1894 : Invariant: Share mode must be locked on entry and exit.
1895 : Returns -1 on error, or number of share modes on success (may be zero).
1896 : ****************************************************************************/
1897 :
1898 : struct open_mode_check_state {
1899 : struct file_id fid;
1900 : uint32_t access_mask;
1901 : uint32_t share_access;
1902 : uint32_t lease_type;
1903 : };
1904 :
1905 11167 : static bool open_mode_check_fn(
1906 : struct share_mode_entry *e,
1907 : bool *modified,
1908 : void *private_data)
1909 : {
1910 11167 : struct open_mode_check_state *state = private_data;
1911 50 : bool disconnected, stale;
1912 50 : uint32_t access_mask, share_access, lease_type;
1913 :
1914 11167 : disconnected = server_id_is_disconnected(&e->pid);
1915 11167 : if (disconnected) {
1916 2 : return false;
1917 : }
1918 :
1919 11165 : access_mask = state->access_mask | e->access_mask;
1920 11165 : share_access = state->share_access;
1921 11165 : if (e->access_mask & conflicting_access) {
1922 10909 : share_access &= e->share_access;
1923 : }
1924 11165 : lease_type = state->lease_type | get_lease_type(e, state->fid);
1925 :
1926 11165 : if ((access_mask == state->access_mask) &&
1927 67 : (share_access == state->share_access) &&
1928 67 : (lease_type == state->lease_type)) {
1929 67 : return false;
1930 : }
1931 :
1932 11098 : stale = share_entry_stale_pid(e);
1933 11098 : if (stale) {
1934 4 : return false;
1935 : }
1936 :
1937 11094 : state->access_mask = access_mask;
1938 11094 : state->share_access = share_access;
1939 11094 : state->lease_type = lease_type;
1940 :
1941 11094 : return false;
1942 : }
1943 :
1944 449822 : static NTSTATUS open_mode_check(connection_struct *conn,
1945 : struct file_id fid,
1946 : struct share_mode_lock *lck,
1947 : uint32_t access_mask,
1948 : uint32_t share_access)
1949 : {
1950 985 : struct open_mode_check_state state;
1951 985 : bool ok, conflict;
1952 449822 : bool modified = false;
1953 :
1954 449822 : if (is_oplock_stat_open(access_mask)) {
1955 : /* Stat open that doesn't trigger oplock breaks or share mode
1956 : * checks... ! JRA. */
1957 48425 : return NT_STATUS_OK;
1958 : }
1959 :
1960 : /*
1961 : * Check if the share modes will give us access.
1962 : */
1963 :
1964 : #if defined(DEVELOPER)
1965 : {
1966 401397 : struct validate_my_share_entries_state validate_state = {
1967 401397 : .sconn = conn->sconn,
1968 : .fid = fid,
1969 401397 : .self = messaging_server_id(conn->sconn->msg_ctx),
1970 : };
1971 401397 : ok = share_mode_forall_entries(
1972 : lck, validate_my_share_entries_fn, &validate_state);
1973 401397 : SMB_ASSERT(ok);
1974 : }
1975 : #endif
1976 :
1977 401397 : share_mode_flags_get(
1978 : lck, &state.access_mask, &state.share_access, NULL);
1979 :
1980 401397 : conflict = share_conflict(
1981 : state.access_mask,
1982 : state.share_access,
1983 : access_mask,
1984 : share_access);
1985 401397 : if (!conflict) {
1986 390293 : DBG_DEBUG("No conflict due to share_mode_flags access\n");
1987 390293 : return NT_STATUS_OK;
1988 : }
1989 :
1990 11104 : state = (struct open_mode_check_state) {
1991 : .fid = fid,
1992 : .share_access = (FILE_SHARE_READ|
1993 : FILE_SHARE_WRITE|
1994 : FILE_SHARE_DELETE),
1995 : };
1996 :
1997 : /*
1998 : * Walk the share mode array to recalculate d->flags
1999 : */
2000 :
2001 11104 : ok = share_mode_forall_entries(lck, open_mode_check_fn, &state);
2002 11104 : if (!ok) {
2003 0 : DBG_DEBUG("share_mode_forall_entries failed\n");
2004 0 : return NT_STATUS_INTERNAL_ERROR;
2005 : }
2006 :
2007 11104 : share_mode_flags_set(
2008 : lck,
2009 : state.access_mask,
2010 : state.share_access,
2011 : state.lease_type,
2012 : &modified);
2013 11104 : if (!modified) {
2014 : /*
2015 : * We only end up here if we had a sharing violation
2016 : * from d->flags and have recalculated it.
2017 : */
2018 10782 : return NT_STATUS_SHARING_VIOLATION;
2019 : }
2020 :
2021 322 : conflict = share_conflict(
2022 : state.access_mask,
2023 : state.share_access,
2024 : access_mask,
2025 : share_access);
2026 322 : if (!conflict) {
2027 283 : DBG_DEBUG("No conflict due to share_mode_flags access\n");
2028 283 : return NT_STATUS_OK;
2029 : }
2030 :
2031 39 : return NT_STATUS_SHARING_VIOLATION;
2032 : }
2033 :
2034 : /*
2035 : * Send a break message to the oplock holder and delay the open for
2036 : * our client.
2037 : */
2038 :
2039 593 : NTSTATUS send_break_message(struct messaging_context *msg_ctx,
2040 : const struct file_id *id,
2041 : const struct share_mode_entry *exclusive,
2042 : uint16_t break_to)
2043 : {
2044 593 : struct oplock_break_message msg = {
2045 : .id = *id,
2046 593 : .share_file_id = exclusive->share_file_id,
2047 : .break_to = break_to,
2048 : };
2049 0 : enum ndr_err_code ndr_err;
2050 0 : DATA_BLOB blob;
2051 0 : NTSTATUS status;
2052 :
2053 593 : if (DEBUGLVL(10)) {
2054 0 : struct server_id_buf buf;
2055 0 : DBG_DEBUG("Sending break message to %s\n",
2056 : server_id_str_buf(exclusive->pid, &buf));
2057 0 : NDR_PRINT_DEBUG(oplock_break_message, &msg);
2058 : }
2059 :
2060 593 : ndr_err = ndr_push_struct_blob(
2061 : &blob,
2062 : talloc_tos(),
2063 : &msg,
2064 : (ndr_push_flags_fn_t)ndr_push_oplock_break_message);
2065 593 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2066 0 : DBG_WARNING("ndr_push_oplock_break_message failed: %s\n",
2067 : ndr_errstr(ndr_err));
2068 0 : return ndr_map_error2ntstatus(ndr_err);
2069 : }
2070 :
2071 593 : status = messaging_send(
2072 : msg_ctx, exclusive->pid, MSG_SMB_BREAK_REQUEST, &blob);
2073 593 : TALLOC_FREE(blob.data);
2074 593 : if (!NT_STATUS_IS_OK(status)) {
2075 0 : DEBUG(3, ("Could not send oplock break message: %s\n",
2076 : nt_errstr(status)));
2077 : }
2078 :
2079 593 : return status;
2080 : }
2081 :
2082 : struct validate_oplock_types_state {
2083 : bool valid;
2084 : bool batch;
2085 : bool ex_or_batch;
2086 : bool level2;
2087 : bool no_oplock;
2088 : uint32_t num_non_stat_opens;
2089 : };
2090 :
2091 56657 : static bool validate_oplock_types_fn(
2092 : struct share_mode_entry *e,
2093 : bool *modified,
2094 : void *private_data)
2095 : {
2096 56657 : struct validate_oplock_types_state *state = private_data;
2097 :
2098 56657 : if (e->op_mid == 0) {
2099 : /* INTERNAL_OPEN_ONLY */
2100 1434 : return false;
2101 : }
2102 :
2103 55221 : if (e->op_type == NO_OPLOCK && is_oplock_stat_open(e->access_mask)) {
2104 : /*
2105 : * We ignore stat opens in the table - they always
2106 : * have NO_OPLOCK and never get or cause breaks. JRA.
2107 : */
2108 31223 : return false;
2109 : }
2110 :
2111 23996 : state->num_non_stat_opens += 1;
2112 :
2113 23996 : if (BATCH_OPLOCK_TYPE(e->op_type)) {
2114 : /* batch - can only be one. */
2115 314 : if (share_entry_stale_pid(e)) {
2116 16 : DBG_DEBUG("Found stale batch oplock\n");
2117 16 : return false;
2118 : }
2119 298 : if (state->ex_or_batch ||
2120 298 : state->batch ||
2121 298 : state->level2 ||
2122 298 : state->no_oplock) {
2123 0 : DBG_ERR("Bad batch oplock entry\n");
2124 0 : state->valid = false;
2125 0 : return true;
2126 : }
2127 298 : state->batch = true;
2128 : }
2129 :
2130 23980 : if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
2131 384 : if (share_entry_stale_pid(e)) {
2132 0 : DBG_DEBUG("Found stale duplicate oplock\n");
2133 0 : return false;
2134 : }
2135 : /* Exclusive or batch - can only be one. */
2136 384 : if (state->ex_or_batch ||
2137 384 : state->level2 ||
2138 384 : state->no_oplock) {
2139 0 : DBG_ERR("Bad exclusive or batch oplock entry\n");
2140 0 : state->valid = false;
2141 0 : return true;
2142 : }
2143 384 : state->ex_or_batch = true;
2144 : }
2145 :
2146 23980 : if (LEVEL_II_OPLOCK_TYPE(e->op_type)) {
2147 236 : if (state->batch || state->ex_or_batch) {
2148 0 : if (share_entry_stale_pid(e)) {
2149 0 : DBG_DEBUG("Found stale LevelII oplock\n");
2150 0 : return false;
2151 : }
2152 0 : DBG_DEBUG("Bad levelII oplock entry\n");
2153 0 : state->valid = false;
2154 0 : return true;
2155 : }
2156 236 : state->level2 = true;
2157 : }
2158 :
2159 23980 : if (e->op_type == NO_OPLOCK) {
2160 22640 : if (state->batch || state->ex_or_batch) {
2161 0 : if (share_entry_stale_pid(e)) {
2162 0 : DBG_DEBUG("Found stale NO_OPLOCK entry\n");
2163 0 : return false;
2164 : }
2165 0 : DBG_ERR("Bad no oplock entry\n");
2166 0 : state->valid = false;
2167 0 : return true;
2168 : }
2169 22640 : state->no_oplock = true;
2170 : }
2171 :
2172 23889 : return false;
2173 : }
2174 :
2175 : /*
2176 : * Do internal consistency checks on the share mode for a file.
2177 : */
2178 :
2179 449984 : static bool validate_oplock_types(struct share_mode_lock *lck)
2180 : {
2181 449984 : struct validate_oplock_types_state state = { .valid = true };
2182 991 : static bool skip_validation;
2183 991 : bool validate;
2184 991 : bool ok;
2185 :
2186 449984 : if (skip_validation) {
2187 0 : return true;
2188 : }
2189 :
2190 449984 : validate = lp_parm_bool(-1, "smbd", "validate_oplock_types", false);
2191 449984 : if (!validate) {
2192 0 : DBG_DEBUG("smbd:validate_oplock_types not set to yes\n");
2193 0 : skip_validation = true;
2194 0 : return true;
2195 : }
2196 :
2197 449984 : ok = share_mode_forall_entries(lck, validate_oplock_types_fn, &state);
2198 449984 : if (!ok) {
2199 0 : DBG_DEBUG("share_mode_forall_entries failed\n");
2200 0 : return false;
2201 : }
2202 449984 : if (!state.valid) {
2203 0 : DBG_DEBUG("Got invalid oplock configuration\n");
2204 0 : return false;
2205 : }
2206 :
2207 449984 : if ((state.batch || state.ex_or_batch) &&
2208 384 : (state.num_non_stat_opens != 1)) {
2209 0 : DBG_WARNING("got batch (%d) or ex (%d) non-exclusively "
2210 : "(%"PRIu32")\n",
2211 : (int)state.batch,
2212 : (int)state.ex_or_batch,
2213 : state.num_non_stat_opens);
2214 0 : return false;
2215 : }
2216 :
2217 448993 : return true;
2218 : }
2219 :
2220 15358 : static bool is_same_lease(const files_struct *fsp,
2221 : const struct share_mode_entry *e,
2222 : const struct smb2_lease *lease)
2223 : {
2224 15358 : if (e->op_type != LEASE_OPLOCK) {
2225 14302 : return false;
2226 : }
2227 980 : if (lease == NULL) {
2228 198 : return false;
2229 : }
2230 :
2231 782 : return smb2_lease_equal(fsp_client_guid(fsp),
2232 : &lease->lease_key,
2233 : &e->client_guid,
2234 : &e->lease_key);
2235 : }
2236 :
2237 348727 : static bool file_has_brlocks(files_struct *fsp)
2238 : {
2239 590 : struct byte_range_lock *br_lck;
2240 :
2241 348727 : br_lck = brl_get_locks_readonly(fsp);
2242 348727 : if (!br_lck)
2243 0 : return false;
2244 :
2245 348727 : return (brl_num_locks(br_lck) > 0);
2246 : }
2247 :
2248 264 : struct fsp_lease *find_fsp_lease(struct files_struct *new_fsp,
2249 : const struct smb2_lease_key *key,
2250 : uint32_t current_state,
2251 : uint16_t lease_version,
2252 : uint16_t lease_epoch)
2253 : {
2254 0 : struct files_struct *fsp;
2255 :
2256 : /*
2257 : * TODO: Measure how expensive this loop is with thousands of open
2258 : * handles...
2259 : */
2260 :
2261 264 : for (fsp = file_find_di_first(new_fsp->conn->sconn, new_fsp->file_id, true);
2262 360 : fsp != NULL;
2263 96 : fsp = file_find_di_next(fsp, true)) {
2264 :
2265 308 : if (fsp == new_fsp) {
2266 0 : continue;
2267 : }
2268 308 : if (fsp->oplock_type != LEASE_OPLOCK) {
2269 14 : continue;
2270 : }
2271 294 : if (smb2_lease_key_equal(&fsp->lease->lease.lease_key, key)) {
2272 212 : fsp->lease->ref_count += 1;
2273 212 : return fsp->lease;
2274 : }
2275 : }
2276 :
2277 : /* Not found - must be leased in another smbd. */
2278 52 : new_fsp->lease = talloc_zero(new_fsp->conn->sconn, struct fsp_lease);
2279 52 : if (new_fsp->lease == NULL) {
2280 0 : return NULL;
2281 : }
2282 52 : new_fsp->lease->ref_count = 1;
2283 52 : new_fsp->lease->sconn = new_fsp->conn->sconn;
2284 52 : new_fsp->lease->lease.lease_key = *key;
2285 52 : new_fsp->lease->lease.lease_state = current_state;
2286 : /*
2287 : * We internally treat all leases as V2 and update
2288 : * the epoch, but when sending breaks it matters if
2289 : * the requesting lease was v1 or v2.
2290 : */
2291 52 : new_fsp->lease->lease.lease_version = lease_version;
2292 52 : new_fsp->lease->lease.lease_epoch = lease_epoch;
2293 52 : return new_fsp->lease;
2294 : }
2295 :
2296 972 : static NTSTATUS try_lease_upgrade(struct files_struct *fsp,
2297 : struct share_mode_lock *lck,
2298 : const struct GUID *client_guid,
2299 : const struct smb2_lease *lease,
2300 : uint32_t granted)
2301 : {
2302 0 : bool do_upgrade;
2303 0 : uint32_t current_state, breaking_to_requested, breaking_to_required;
2304 0 : bool breaking;
2305 0 : uint16_t lease_version, epoch;
2306 0 : uint32_t existing, requested;
2307 0 : NTSTATUS status;
2308 :
2309 972 : status = leases_db_get(
2310 : client_guid,
2311 : &lease->lease_key,
2312 972 : &fsp->file_id,
2313 : ¤t_state,
2314 : &breaking,
2315 : &breaking_to_requested,
2316 : &breaking_to_required,
2317 : &lease_version,
2318 : &epoch);
2319 972 : if (!NT_STATUS_IS_OK(status)) {
2320 760 : return status;
2321 : }
2322 :
2323 212 : fsp->lease = find_fsp_lease(
2324 : fsp,
2325 : &lease->lease_key,
2326 : current_state,
2327 : lease_version,
2328 : epoch);
2329 212 : if (fsp->lease == NULL) {
2330 0 : DEBUG(1, ("Did not find existing lease for file %s\n",
2331 : fsp_str_dbg(fsp)));
2332 0 : return NT_STATUS_NO_MEMORY;
2333 : }
2334 :
2335 : /*
2336 : * Upgrade only if the requested lease is a strict upgrade.
2337 : */
2338 212 : existing = current_state;
2339 212 : requested = lease->lease_state;
2340 :
2341 : /*
2342 : * Tricky: This test makes sure that "requested" is a
2343 : * strict bitwise superset of "existing".
2344 : */
2345 212 : do_upgrade = ((existing & requested) == existing);
2346 :
2347 : /*
2348 : * Upgrade only if there's a change.
2349 : */
2350 212 : do_upgrade &= (granted != existing);
2351 :
2352 : /*
2353 : * Upgrade only if other leases don't prevent what was asked
2354 : * for.
2355 : */
2356 212 : do_upgrade &= (granted == requested);
2357 :
2358 : /*
2359 : * only upgrade if we are not in breaking state
2360 : */
2361 212 : do_upgrade &= !breaking;
2362 :
2363 212 : DEBUG(10, ("existing=%"PRIu32", requested=%"PRIu32", "
2364 : "granted=%"PRIu32", do_upgrade=%d\n",
2365 : existing, requested, granted, (int)do_upgrade));
2366 :
2367 212 : if (do_upgrade) {
2368 0 : NTSTATUS set_status;
2369 :
2370 52 : current_state = granted;
2371 52 : epoch += 1;
2372 :
2373 52 : set_status = leases_db_set(
2374 : client_guid,
2375 : &lease->lease_key,
2376 : current_state,
2377 : breaking,
2378 : breaking_to_requested,
2379 : breaking_to_required,
2380 : lease_version,
2381 : epoch);
2382 :
2383 52 : if (!NT_STATUS_IS_OK(set_status)) {
2384 0 : DBG_DEBUG("leases_db_set failed: %s\n",
2385 : nt_errstr(set_status));
2386 0 : return set_status;
2387 : }
2388 : }
2389 :
2390 212 : fsp_lease_update(fsp);
2391 :
2392 212 : return NT_STATUS_OK;
2393 : }
2394 :
2395 760 : static NTSTATUS grant_new_fsp_lease(struct files_struct *fsp,
2396 : struct share_mode_lock *lck,
2397 : const struct GUID *client_guid,
2398 : const struct smb2_lease *lease,
2399 : uint32_t granted)
2400 : {
2401 0 : NTSTATUS status;
2402 :
2403 760 : fsp->lease = talloc_zero(fsp->conn->sconn, struct fsp_lease);
2404 760 : if (fsp->lease == NULL) {
2405 0 : return NT_STATUS_INSUFFICIENT_RESOURCES;
2406 : }
2407 760 : fsp->lease->ref_count = 1;
2408 760 : fsp->lease->sconn = fsp->conn->sconn;
2409 760 : fsp->lease->lease.lease_version = lease->lease_version;
2410 760 : fsp->lease->lease.lease_key = lease->lease_key;
2411 760 : fsp->lease->lease.lease_state = granted;
2412 760 : fsp->lease->lease.lease_epoch = lease->lease_epoch + 1;
2413 :
2414 760 : status = leases_db_add(client_guid,
2415 : &lease->lease_key,
2416 760 : &fsp->file_id,
2417 760 : fsp->lease->lease.lease_state,
2418 760 : fsp->lease->lease.lease_version,
2419 760 : fsp->lease->lease.lease_epoch,
2420 760 : fsp->conn->connectpath,
2421 760 : fsp->fsp_name->base_name,
2422 760 : fsp->fsp_name->stream_name);
2423 760 : if (!NT_STATUS_IS_OK(status)) {
2424 0 : DEBUG(10, ("%s: leases_db_add failed: %s\n", __func__,
2425 : nt_errstr(status)));
2426 0 : TALLOC_FREE(fsp->lease);
2427 0 : return NT_STATUS_INSUFFICIENT_RESOURCES;
2428 : }
2429 :
2430 : /*
2431 : * We used to set lck->data->modified=true here without
2432 : * actually modifying lck->data, triggering a needless
2433 : * writeback of lck->data.
2434 : *
2435 : * Apart from that writeback, setting modified=true has the
2436 : * effect of triggering all waiters for this file to
2437 : * retry. This only makes sense if any blocking condition
2438 : * (i.e. waiting for a lease to be downgraded or removed) is
2439 : * gone. This routine here only adds a lease, so it will never
2440 : * free up resources that blocked waiters can now claim. So
2441 : * that second effect also does not matter in this
2442 : * routine. Thus setting lck->data->modified=true does not
2443 : * need to be done here.
2444 : */
2445 :
2446 760 : return NT_STATUS_OK;
2447 : }
2448 :
2449 972 : static NTSTATUS grant_fsp_lease(struct files_struct *fsp,
2450 : struct share_mode_lock *lck,
2451 : const struct smb2_lease *lease,
2452 : uint32_t granted)
2453 : {
2454 972 : const struct GUID *client_guid = fsp_client_guid(fsp);
2455 0 : NTSTATUS status;
2456 :
2457 972 : status = try_lease_upgrade(fsp, lck, client_guid, lease, granted);
2458 :
2459 972 : if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
2460 760 : status = grant_new_fsp_lease(
2461 : fsp, lck, client_guid, lease, granted);
2462 : }
2463 :
2464 972 : return status;
2465 : }
2466 :
2467 347755 : static int map_lease_type_to_oplock(uint32_t lease_type)
2468 : {
2469 347755 : int result = NO_OPLOCK;
2470 :
2471 347755 : switch (lease_type) {
2472 1152 : case SMB2_LEASE_READ|SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE:
2473 1152 : result = BATCH_OPLOCK|EXCLUSIVE_OPLOCK;
2474 1152 : break;
2475 177 : case SMB2_LEASE_READ|SMB2_LEASE_WRITE:
2476 177 : result = EXCLUSIVE_OPLOCK;
2477 177 : break;
2478 254 : case SMB2_LEASE_READ|SMB2_LEASE_HANDLE:
2479 : case SMB2_LEASE_READ:
2480 254 : result = LEVEL_II_OPLOCK;
2481 254 : break;
2482 : }
2483 :
2484 347755 : return result;
2485 : }
2486 :
2487 : struct delay_for_oplock_state {
2488 : struct files_struct *fsp;
2489 : const struct smb2_lease *lease;
2490 : bool will_overwrite;
2491 : uint32_t delay_mask;
2492 : bool first_open_attempt;
2493 : bool got_handle_lease;
2494 : bool got_oplock;
2495 : bool have_other_lease;
2496 : uint32_t total_lease_types;
2497 : bool delay;
2498 : };
2499 :
2500 20144 : static bool delay_for_oplock_fn(
2501 : struct share_mode_entry *e,
2502 : bool *modified,
2503 : void *private_data)
2504 : {
2505 20144 : struct delay_for_oplock_state *state = private_data;
2506 20144 : struct files_struct *fsp = state->fsp;
2507 20144 : const struct smb2_lease *lease = state->lease;
2508 20144 : bool e_is_lease = (e->op_type == LEASE_OPLOCK);
2509 20144 : uint32_t e_lease_type = SMB2_LEASE_NONE;
2510 78 : uint32_t break_to;
2511 20144 : bool lease_is_breaking = false;
2512 :
2513 20144 : if (e_is_lease) {
2514 0 : NTSTATUS status;
2515 :
2516 708 : if (lease != NULL) {
2517 504 : bool our_lease = is_same_lease(fsp, e, lease);
2518 504 : if (our_lease) {
2519 212 : DBG_DEBUG("Ignoring our own lease\n");
2520 212 : return false;
2521 : }
2522 : }
2523 :
2524 496 : status = leases_db_get(
2525 496 : &e->client_guid,
2526 496 : &e->lease_key,
2527 496 : &fsp->file_id,
2528 : &e_lease_type, /* current_state */
2529 : &lease_is_breaking,
2530 : NULL, /* breaking_to_requested */
2531 : NULL, /* breaking_to_required */
2532 : NULL, /* lease_version */
2533 : NULL); /* epoch */
2534 :
2535 : /*
2536 : * leases_db_get() can return NT_STATUS_NOT_FOUND
2537 : * if the share_mode_entry e is stale and the
2538 : * lease record was already removed. In this case return
2539 : * false so the traverse continues.
2540 : */
2541 :
2542 496 : if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) &&
2543 0 : share_entry_stale_pid(e))
2544 : {
2545 0 : struct GUID_txt_buf guid_strbuf;
2546 0 : struct file_id_buf file_id_strbuf;
2547 0 : DBG_DEBUG("leases_db_get for client_guid [%s] "
2548 : "lease_key [%"PRIu64"/%"PRIu64"] "
2549 : "file_id [%s] failed for stale "
2550 : "share_mode_entry\n",
2551 : GUID_buf_string(&e->client_guid, &guid_strbuf),
2552 : e->lease_key.data[0],
2553 : e->lease_key.data[1],
2554 : file_id_str_buf(fsp->file_id, &file_id_strbuf));
2555 0 : return false;
2556 : }
2557 496 : if (!NT_STATUS_IS_OK(status)) {
2558 0 : struct GUID_txt_buf guid_strbuf;
2559 0 : struct file_id_buf file_id_strbuf;
2560 0 : DBG_ERR("leases_db_get for client_guid [%s] "
2561 : "lease_key [%"PRIu64"/%"PRIu64"] "
2562 : "file_id [%s] failed: %s\n",
2563 : GUID_buf_string(&e->client_guid, &guid_strbuf),
2564 : e->lease_key.data[0],
2565 : e->lease_key.data[1],
2566 : file_id_str_buf(fsp->file_id, &file_id_strbuf),
2567 : nt_errstr(status));
2568 0 : smb_panic("leases_db_get() failed");
2569 : }
2570 : } else {
2571 19436 : e_lease_type = get_lease_type(e, fsp->file_id);
2572 : }
2573 :
2574 19932 : if (((e_lease_type & ~state->total_lease_types) != 0) &&
2575 1017 : !share_entry_stale_pid(e))
2576 : {
2577 1011 : state->total_lease_types |= e_lease_type;
2578 : }
2579 :
2580 19932 : if (!state->got_handle_lease &&
2581 19926 : ((e_lease_type & SMB2_LEASE_HANDLE) != 0) &&
2582 599 : !share_entry_stale_pid(e)) {
2583 595 : state->got_handle_lease = true;
2584 : }
2585 :
2586 19932 : if (!state->got_oplock &&
2587 14944 : (e->op_type != LEASE_OPLOCK) &&
2588 14378 : !share_entry_stale_pid(e)) {
2589 14376 : state->got_oplock = true;
2590 : }
2591 :
2592 20008 : if (!state->have_other_lease &&
2593 14930 : !is_same_lease(fsp, e, lease) &&
2594 14854 : !share_entry_stale_pid(e)) {
2595 14848 : state->have_other_lease = true;
2596 : }
2597 :
2598 19932 : if (e_is_lease && is_lease_stat_open(fsp->access_mask)) {
2599 2 : return false;
2600 : }
2601 :
2602 19930 : break_to = e_lease_type & ~state->delay_mask;
2603 :
2604 19930 : if (state->will_overwrite) {
2605 221 : break_to &= ~(SMB2_LEASE_HANDLE|SMB2_LEASE_READ);
2606 : }
2607 :
2608 19930 : DBG_DEBUG("e_lease_type %u, will_overwrite: %u\n",
2609 : (unsigned)e_lease_type,
2610 : (unsigned)state->will_overwrite);
2611 :
2612 19930 : if ((e_lease_type & ~break_to) == 0) {
2613 19423 : if (lease_is_breaking) {
2614 8 : state->delay = true;
2615 : }
2616 19423 : return false;
2617 : }
2618 :
2619 507 : if (share_entry_stale_pid(e)) {
2620 4 : return false;
2621 : }
2622 :
2623 503 : if (state->will_overwrite) {
2624 : /*
2625 : * If we break anyway break to NONE directly.
2626 : * Otherwise vfs_set_filelen() will trigger the
2627 : * break.
2628 : */
2629 60 : break_to &= ~(SMB2_LEASE_READ|SMB2_LEASE_WRITE);
2630 : }
2631 :
2632 503 : if (!e_is_lease) {
2633 : /*
2634 : * Oplocks only support breaking to R or NONE.
2635 : */
2636 321 : break_to &= ~(SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
2637 : }
2638 :
2639 503 : DBG_DEBUG("breaking from %d to %d\n",
2640 : (int)e_lease_type,
2641 : (int)break_to);
2642 503 : send_break_message(
2643 503 : fsp->conn->sconn->msg_ctx, &fsp->file_id, e, break_to);
2644 503 : if (e_lease_type & state->delay_mask) {
2645 481 : state->delay = true;
2646 : }
2647 503 : if (lease_is_breaking && !state->first_open_attempt) {
2648 26 : state->delay = true;
2649 : }
2650 :
2651 503 : return false;
2652 : };
2653 :
2654 441868 : static NTSTATUS delay_for_oplock(files_struct *fsp,
2655 : int oplock_request,
2656 : const struct smb2_lease *lease,
2657 : struct share_mode_lock *lck,
2658 : bool have_sharing_violation,
2659 : uint32_t create_disposition,
2660 : bool first_open_attempt,
2661 : int *poplock_type,
2662 : uint32_t *pgranted)
2663 : {
2664 441868 : struct delay_for_oplock_state state = {
2665 : .fsp = fsp,
2666 : .lease = lease,
2667 : .first_open_attempt = first_open_attempt,
2668 : };
2669 983 : uint32_t requested;
2670 983 : uint32_t granted;
2671 983 : int oplock_type;
2672 983 : bool ok;
2673 :
2674 441868 : *poplock_type = NO_OPLOCK;
2675 441868 : *pgranted = 0;
2676 :
2677 441868 : if (fsp->fsp_flags.is_directory) {
2678 : /*
2679 : * No directory leases yet
2680 : */
2681 82197 : SMB_ASSERT(oplock_request == NO_OPLOCK);
2682 82197 : if (have_sharing_violation) {
2683 238 : return NT_STATUS_SHARING_VIOLATION;
2684 : }
2685 81959 : return NT_STATUS_OK;
2686 : }
2687 :
2688 359671 : if (oplock_request == LEASE_OPLOCK) {
2689 1068 : if (lease == NULL) {
2690 : /*
2691 : * The SMB2 layer should have checked this
2692 : */
2693 0 : return NT_STATUS_INTERNAL_ERROR;
2694 : }
2695 :
2696 1068 : requested = lease->lease_state;
2697 : } else {
2698 358603 : requested = map_oplock_to_lease_type(
2699 357966 : oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2700 : }
2701 :
2702 359671 : share_mode_flags_get(lck, NULL, NULL, &state.total_lease_types);
2703 :
2704 359671 : if (is_oplock_stat_open(fsp->access_mask)) {
2705 7393 : goto grant;
2706 : }
2707 :
2708 352911 : state.delay_mask = have_sharing_violation ?
2709 352278 : SMB2_LEASE_HANDLE : SMB2_LEASE_WRITE;
2710 :
2711 352278 : switch (create_disposition) {
2712 9729 : case FILE_SUPERSEDE:
2713 : case FILE_OVERWRITE:
2714 : case FILE_OVERWRITE_IF:
2715 9729 : state.will_overwrite = true;
2716 9729 : break;
2717 342549 : default:
2718 342549 : state.will_overwrite = false;
2719 342549 : break;
2720 : }
2721 :
2722 352278 : state.total_lease_types = SMB2_LEASE_NONE;
2723 352278 : ok = share_mode_forall_entries(lck, delay_for_oplock_fn, &state);
2724 352278 : if (!ok) {
2725 0 : return NT_STATUS_INTERNAL_ERROR;
2726 : }
2727 :
2728 352278 : if (state.delay) {
2729 495 : return NT_STATUS_RETRY;
2730 : }
2731 :
2732 351783 : grant:
2733 359176 : if (have_sharing_violation) {
2734 10449 : return NT_STATUS_SHARING_VIOLATION;
2735 : }
2736 :
2737 348727 : granted = requested;
2738 :
2739 348727 : if (oplock_request == LEASE_OPLOCK) {
2740 972 : if (lp_kernel_oplocks(SNUM(fsp->conn))) {
2741 0 : DEBUG(10, ("No lease granted because kernel oplocks are enabled\n"));
2742 0 : granted = SMB2_LEASE_NONE;
2743 : }
2744 972 : if ((granted & (SMB2_LEASE_READ|SMB2_LEASE_WRITE)) == 0) {
2745 106 : DEBUG(10, ("No read or write lease requested\n"));
2746 106 : granted = SMB2_LEASE_NONE;
2747 : }
2748 972 : if (granted == SMB2_LEASE_WRITE) {
2749 2 : DEBUG(10, ("pure write lease requested\n"));
2750 2 : granted = SMB2_LEASE_NONE;
2751 : }
2752 972 : if (granted == (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
2753 2 : DEBUG(10, ("write and handle lease requested\n"));
2754 2 : granted = SMB2_LEASE_NONE;
2755 : }
2756 : }
2757 :
2758 348727 : if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
2759 97 : DBG_DEBUG("file %s has byte range locks\n",
2760 : fsp_str_dbg(fsp));
2761 97 : granted &= ~SMB2_LEASE_READ;
2762 : }
2763 :
2764 348727 : if (state.have_other_lease) {
2765 : /*
2766 : * Can grant only one writer
2767 : */
2768 3904 : granted &= ~SMB2_LEASE_WRITE;
2769 : }
2770 :
2771 348727 : if ((granted & SMB2_LEASE_READ) && !(granted & SMB2_LEASE_WRITE)) {
2772 752 : bool allow_level2 =
2773 1498 : (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
2774 746 : lp_level2_oplocks(SNUM(fsp->conn));
2775 :
2776 752 : if (!allow_level2) {
2777 6 : granted = SMB2_LEASE_NONE;
2778 : }
2779 : }
2780 :
2781 348727 : if (oplock_request == LEASE_OPLOCK) {
2782 972 : if (state.got_oplock) {
2783 40 : granted &= ~SMB2_LEASE_HANDLE;
2784 : }
2785 :
2786 972 : oplock_type = LEASE_OPLOCK;
2787 : } else {
2788 347755 : if (state.got_handle_lease) {
2789 50 : granted = SMB2_LEASE_NONE;
2790 : }
2791 :
2792 : /*
2793 : * Reflect possible downgrades from:
2794 : * - map_lease_type_to_oplock() => "RH" to just LEVEL_II
2795 : */
2796 347755 : oplock_type = map_lease_type_to_oplock(granted);
2797 347755 : granted = map_oplock_to_lease_type(oplock_type);
2798 : }
2799 :
2800 348727 : state.total_lease_types |= granted;
2801 :
2802 : {
2803 590 : uint32_t acc, sh, ls;
2804 348727 : share_mode_flags_get(lck, &acc, &sh, &ls);
2805 348727 : ls = state.total_lease_types;
2806 348727 : share_mode_flags_set(lck, acc, sh, ls, NULL);
2807 : }
2808 :
2809 348727 : DBG_DEBUG("oplock type 0x%x granted (%s%s%s)(0x%x), on file %s, "
2810 : "requested 0x%x (%s%s%s)(0x%x) => total (%s%s%s)(0x%x)\n",
2811 : fsp->oplock_type,
2812 : granted & SMB2_LEASE_READ ? "R":"",
2813 : granted & SMB2_LEASE_WRITE ? "W":"",
2814 : granted & SMB2_LEASE_HANDLE ? "H":"",
2815 : granted,
2816 : fsp_str_dbg(fsp),
2817 : oplock_request,
2818 : requested & SMB2_LEASE_READ ? "R":"",
2819 : requested & SMB2_LEASE_WRITE ? "W":"",
2820 : requested & SMB2_LEASE_HANDLE ? "H":"",
2821 : requested,
2822 : state.total_lease_types & SMB2_LEASE_READ ? "R":"",
2823 : state.total_lease_types & SMB2_LEASE_WRITE ? "W":"",
2824 : state.total_lease_types & SMB2_LEASE_HANDLE ? "H":"",
2825 : state.total_lease_types);
2826 :
2827 348727 : *poplock_type = oplock_type;
2828 348727 : *pgranted = granted;
2829 348727 : return NT_STATUS_OK;
2830 : }
2831 :
2832 449822 : static NTSTATUS handle_share_mode_lease(
2833 : files_struct *fsp,
2834 : struct share_mode_lock *lck,
2835 : uint32_t create_disposition,
2836 : uint32_t access_mask,
2837 : uint32_t share_access,
2838 : int oplock_request,
2839 : const struct smb2_lease *lease,
2840 : bool first_open_attempt,
2841 : int *poplock_type,
2842 : uint32_t *pgranted)
2843 : {
2844 449822 : bool sharing_violation = false;
2845 985 : NTSTATUS status;
2846 :
2847 449822 : *poplock_type = NO_OPLOCK;
2848 449822 : *pgranted = 0;
2849 :
2850 450807 : status = open_mode_check(
2851 449822 : fsp->conn, fsp->file_id, lck, access_mask, share_access);
2852 449822 : if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
2853 10821 : sharing_violation = true;
2854 10821 : status = NT_STATUS_OK; /* handled later */
2855 : }
2856 :
2857 449822 : if (!NT_STATUS_IS_OK(status)) {
2858 0 : return status;
2859 : }
2860 :
2861 449822 : if (oplock_request == INTERNAL_OPEN_ONLY) {
2862 7954 : if (sharing_violation) {
2863 64 : DBG_DEBUG("Sharing violation for internal open\n");
2864 64 : return NT_STATUS_SHARING_VIOLATION;
2865 : }
2866 :
2867 : /*
2868 : * Internal opens never do oplocks or leases. We don't
2869 : * need to go through delay_for_oplock().
2870 : */
2871 7890 : return NT_STATUS_OK;
2872 : }
2873 :
2874 441868 : status = delay_for_oplock(
2875 : fsp,
2876 : oplock_request,
2877 : lease,
2878 : lck,
2879 : sharing_violation,
2880 : create_disposition,
2881 : first_open_attempt,
2882 : poplock_type,
2883 : pgranted);
2884 441868 : if (!NT_STATUS_IS_OK(status)) {
2885 11182 : return status;
2886 : }
2887 :
2888 430686 : return NT_STATUS_OK;
2889 : }
2890 :
2891 8642 : static bool request_timed_out(struct smb_request *req, struct timeval timeout)
2892 : {
2893 34 : struct timeval now, end_time;
2894 8642 : GetTimeOfDay(&now);
2895 8642 : end_time = timeval_sum(&req->request_time, &timeout);
2896 8642 : return (timeval_compare(&end_time, &now) < 0);
2897 : }
2898 :
2899 : struct defer_open_state {
2900 : struct smbXsrv_connection *xconn;
2901 : uint64_t mid;
2902 : };
2903 :
2904 : static void defer_open_done(struct tevent_req *req);
2905 :
2906 : /**
2907 : * Defer an open and watch a locking.tdb record
2908 : *
2909 : * This defers an open that gets rescheduled once the locking.tdb record watch
2910 : * is triggered by a change to the record.
2911 : *
2912 : * It is used to defer opens that triggered an oplock break and for the SMB1
2913 : * sharing violation delay.
2914 : **/
2915 495 : static void defer_open(struct share_mode_lock *lck,
2916 : struct timeval timeout,
2917 : struct smb_request *req,
2918 : struct file_id id)
2919 : {
2920 495 : struct deferred_open_record *open_rec = NULL;
2921 0 : struct timeval abs_timeout;
2922 0 : struct defer_open_state *watch_state;
2923 0 : struct tevent_req *watch_req;
2924 0 : struct timeval_buf tvbuf1, tvbuf2;
2925 0 : struct file_id_buf fbuf;
2926 0 : bool ok;
2927 :
2928 495 : abs_timeout = timeval_sum(&req->request_time, &timeout);
2929 :
2930 495 : DBG_DEBUG("request time [%s] timeout [%s] mid [%" PRIu64 "] "
2931 : "file_id [%s]\n",
2932 : timeval_str_buf(&req->request_time, false, true, &tvbuf1),
2933 : timeval_str_buf(&abs_timeout, false, true, &tvbuf2),
2934 : req->mid,
2935 : file_id_str_buf(id, &fbuf));
2936 :
2937 495 : open_rec = talloc_zero(NULL, struct deferred_open_record);
2938 495 : if (open_rec == NULL) {
2939 0 : TALLOC_FREE(lck);
2940 0 : exit_server("talloc failed");
2941 : }
2942 :
2943 495 : watch_state = talloc(open_rec, struct defer_open_state);
2944 495 : if (watch_state == NULL) {
2945 0 : exit_server("talloc failed");
2946 : }
2947 495 : watch_state->xconn = req->xconn;
2948 495 : watch_state->mid = req->mid;
2949 :
2950 495 : DBG_DEBUG("deferring mid %" PRIu64 "\n", req->mid);
2951 :
2952 495 : watch_req = share_mode_watch_send(
2953 : watch_state,
2954 495 : req->sconn->ev_ctx,
2955 : lck,
2956 495 : (struct server_id){0});
2957 495 : if (watch_req == NULL) {
2958 0 : exit_server("Could not watch share mode record");
2959 : }
2960 495 : tevent_req_set_callback(watch_req, defer_open_done, watch_state);
2961 :
2962 495 : ok = tevent_req_set_endtime(watch_req, req->sconn->ev_ctx, abs_timeout);
2963 495 : if (!ok) {
2964 0 : exit_server("tevent_req_set_endtime failed");
2965 : }
2966 :
2967 495 : ok = push_deferred_open_message_smb(req, timeout, id, open_rec);
2968 495 : if (!ok) {
2969 0 : TALLOC_FREE(lck);
2970 0 : exit_server("push_deferred_open_message_smb failed");
2971 : }
2972 495 : }
2973 :
2974 443 : static void defer_open_done(struct tevent_req *req)
2975 : {
2976 443 : struct defer_open_state *state = tevent_req_callback_data(
2977 : req, struct defer_open_state);
2978 0 : NTSTATUS status;
2979 0 : bool ret;
2980 :
2981 443 : status = share_mode_watch_recv(req, NULL, NULL);
2982 443 : TALLOC_FREE(req);
2983 443 : if (!NT_STATUS_IS_OK(status)) {
2984 0 : DEBUG(5, ("dbwrap_watched_watch_recv returned %s\n",
2985 : nt_errstr(status)));
2986 : /*
2987 : * Even if it failed, retry anyway. TODO: We need a way to
2988 : * tell a re-scheduled open about that error.
2989 : */
2990 : }
2991 :
2992 443 : DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state->mid));
2993 :
2994 443 : ret = schedule_deferred_open_message_smb(state->xconn, state->mid);
2995 443 : SMB_ASSERT(ret);
2996 443 : TALLOC_FREE(state);
2997 443 : }
2998 :
2999 : /**
3000 : * Actually attempt the kernel oplock polling open.
3001 : */
3002 :
3003 4059 : static void poll_open_fn(struct tevent_context *ev,
3004 : struct tevent_timer *te,
3005 : struct timeval current_time,
3006 : void *private_data)
3007 : {
3008 4059 : struct deferred_open_record *open_rec = talloc_get_type_abort(
3009 : private_data, struct deferred_open_record);
3010 17 : bool ok;
3011 :
3012 4059 : TALLOC_FREE(open_rec->watch_req);
3013 :
3014 4059 : ok = schedule_deferred_open_message_smb(
3015 : open_rec->xconn, open_rec->mid);
3016 4059 : if (!ok) {
3017 0 : exit_server("schedule_deferred_open_message_smb failed");
3018 : }
3019 4059 : DBG_DEBUG("timer fired. Retrying open !\n");
3020 4059 : }
3021 :
3022 : static void poll_open_done(struct tevent_req *subreq);
3023 :
3024 : struct poll_open_setup_watcher_state {
3025 : TALLOC_CTX *mem_ctx;
3026 : struct tevent_context *ev_ctx;
3027 : struct tevent_req *watch_req;
3028 : };
3029 :
3030 4 : static void poll_open_setup_watcher_fn(struct share_mode_lock *lck,
3031 : void *private_data)
3032 : {
3033 4 : struct poll_open_setup_watcher_state *state =
3034 : (struct poll_open_setup_watcher_state *)private_data;
3035 :
3036 4 : if (!validate_oplock_types(lck)) {
3037 0 : smb_panic("validate_oplock_types failed");
3038 : }
3039 :
3040 8 : state->watch_req = share_mode_watch_send(
3041 : state->mem_ctx,
3042 : state->ev_ctx,
3043 : lck,
3044 4 : (struct server_id) {0});
3045 4 : if (state->watch_req == NULL) {
3046 0 : DBG_WARNING("share_mode_watch_send failed\n");
3047 0 : return;
3048 : }
3049 : }
3050 :
3051 : /**
3052 : * Reschedule an open for 1 second from now, if not timed out.
3053 : **/
3054 8147 : static bool setup_poll_open(
3055 : struct smb_request *req,
3056 : const struct file_id *id,
3057 : struct timeval max_timeout,
3058 : struct timeval interval)
3059 : {
3060 34 : static struct file_id zero_id = {};
3061 34 : bool ok;
3062 8147 : struct deferred_open_record *open_rec = NULL;
3063 34 : struct timeval endtime, next_interval;
3064 34 : struct file_id_buf ftmp;
3065 :
3066 8147 : if (request_timed_out(req, max_timeout)) {
3067 3983 : return false;
3068 : }
3069 :
3070 4147 : open_rec = talloc_zero(NULL, struct deferred_open_record);
3071 4147 : if (open_rec == NULL) {
3072 0 : DBG_WARNING("talloc failed\n");
3073 0 : return false;
3074 : }
3075 4147 : open_rec->xconn = req->xconn;
3076 4147 : open_rec->mid = req->mid;
3077 :
3078 : /*
3079 : * Make sure open_rec->te does not come later than the
3080 : * request's maximum endtime.
3081 : */
3082 :
3083 4147 : endtime = timeval_sum(&req->request_time, &max_timeout);
3084 4147 : next_interval = timeval_current_ofs(interval.tv_sec, interval.tv_usec);
3085 4147 : next_interval = timeval_min(&endtime, &next_interval);
3086 :
3087 4147 : open_rec->te = tevent_add_timer(
3088 : req->sconn->ev_ctx,
3089 : open_rec,
3090 : next_interval,
3091 : poll_open_fn,
3092 : open_rec);
3093 4147 : if (open_rec->te == NULL) {
3094 0 : DBG_WARNING("tevent_add_timer failed\n");
3095 0 : TALLOC_FREE(open_rec);
3096 0 : return false;
3097 : }
3098 :
3099 4147 : if (id != NULL) {
3100 8 : struct poll_open_setup_watcher_state wstate = {
3101 : .mem_ctx = open_rec,
3102 8 : .ev_ctx = req->sconn->ev_ctx,
3103 : };
3104 0 : NTSTATUS status;
3105 :
3106 8 : status = share_mode_do_locked_vfs_denied(*id,
3107 : poll_open_setup_watcher_fn,
3108 : &wstate);
3109 8 : if (NT_STATUS_IS_OK(status)) {
3110 4 : if (wstate.watch_req == NULL) {
3111 0 : DBG_WARNING("share_mode_watch_send failed\n");
3112 0 : TALLOC_FREE(open_rec);
3113 0 : return false;
3114 : }
3115 4 : open_rec->watch_req = wstate.watch_req;
3116 4 : tevent_req_set_callback(open_rec->watch_req,
3117 : poll_open_done,
3118 : open_rec);
3119 4 : } else if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
3120 0 : DBG_WARNING("share_mode_do_locked_vfs_denied failed - %s\n",
3121 : nt_errstr(status));
3122 0 : TALLOC_FREE(open_rec);
3123 0 : return false;
3124 : }
3125 : } else {
3126 4122 : id = &zero_id;
3127 : }
3128 :
3129 4147 : ok = push_deferred_open_message_smb(req, max_timeout, *id, open_rec);
3130 4147 : if (!ok) {
3131 0 : DBG_WARNING("push_deferred_open_message_smb failed\n");
3132 0 : TALLOC_FREE(open_rec);
3133 0 : return false;
3134 : }
3135 :
3136 4147 : DBG_DEBUG("poll request time [%s] mid [%" PRIu64 "] file_id [%s]\n",
3137 : timeval_string(talloc_tos(), &req->request_time, false),
3138 : req->mid,
3139 : file_id_str_buf(*id, &ftmp));
3140 :
3141 4130 : return true;
3142 : }
3143 :
3144 4 : static void poll_open_done(struct tevent_req *subreq)
3145 : {
3146 4 : struct deferred_open_record *open_rec = tevent_req_callback_data(
3147 : subreq, struct deferred_open_record);
3148 0 : NTSTATUS status;
3149 0 : bool ok;
3150 :
3151 4 : status = share_mode_watch_recv(subreq, NULL, NULL);
3152 4 : TALLOC_FREE(subreq);
3153 4 : open_rec->watch_req = NULL;
3154 4 : TALLOC_FREE(open_rec->te);
3155 :
3156 4 : DBG_DEBUG("dbwrap_watched_watch_recv returned %s\n",
3157 : nt_errstr(status));
3158 :
3159 4 : ok = schedule_deferred_open_message_smb(
3160 : open_rec->xconn, open_rec->mid);
3161 4 : if (!ok) {
3162 0 : exit_server("schedule_deferred_open_message_smb failed");
3163 : }
3164 4 : }
3165 :
3166 8139 : bool defer_smb1_sharing_violation(struct smb_request *req)
3167 : {
3168 34 : bool ok;
3169 34 : int timeout_usecs;
3170 :
3171 8139 : if (!lp_defer_sharing_violations()) {
3172 0 : return false;
3173 : }
3174 :
3175 : /*
3176 : * Try every 200msec up to (by default) one second. To be
3177 : * precise, according to behaviour note <247> in [MS-CIFS],
3178 : * the server tries 5 times. But up to one second should be
3179 : * close enough.
3180 : */
3181 :
3182 8139 : timeout_usecs = lp_parm_int(
3183 8139 : SNUM(req->conn),
3184 : "smbd",
3185 : "sharedelay",
3186 : SHARING_VIOLATION_USEC_WAIT);
3187 :
3188 8139 : ok = setup_poll_open(
3189 : req,
3190 : NULL,
3191 8139 : (struct timeval) { .tv_usec = timeout_usecs },
3192 8139 : (struct timeval) { .tv_usec = 200000 });
3193 8139 : return ok;
3194 : }
3195 :
3196 : /****************************************************************************
3197 : On overwrite open ensure that the attributes match.
3198 : ****************************************************************************/
3199 :
3200 2905 : static bool open_match_attributes(connection_struct *conn,
3201 : uint32_t old_dos_attr,
3202 : uint32_t new_dos_attr,
3203 : mode_t new_unx_mode,
3204 : mode_t *returned_unx_mode)
3205 : {
3206 274 : uint32_t noarch_old_dos_attr, noarch_new_dos_attr;
3207 :
3208 2905 : noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
3209 2905 : noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
3210 :
3211 2905 : if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
3212 2274 : (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
3213 657 : *returned_unx_mode = new_unx_mode;
3214 : } else {
3215 2248 : *returned_unx_mode = (mode_t)0;
3216 : }
3217 :
3218 2905 : DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
3219 : "new_dos_attr = 0x%x "
3220 : "returned_unx_mode = 0%o\n",
3221 : (unsigned int)old_dos_attr,
3222 : (unsigned int)new_dos_attr,
3223 : (unsigned int)*returned_unx_mode ));
3224 :
3225 : /* If we're mapping SYSTEM and HIDDEN ensure they match. */
3226 2905 : if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
3227 2905 : if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
3228 896 : !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
3229 504 : return False;
3230 : }
3231 : }
3232 2338 : if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
3233 2338 : if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
3234 754 : !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
3235 497 : return False;
3236 : }
3237 : }
3238 1685 : return True;
3239 : }
3240 :
3241 495 : static void schedule_defer_open(struct share_mode_lock *lck,
3242 : struct file_id id,
3243 : struct smb_request *req)
3244 : {
3245 : /* This is a relative time, added to the absolute
3246 : request_time value to get the absolute timeout time.
3247 : Note that if this is the second or greater time we enter
3248 : this codepath for this particular request mid then
3249 : request_time is left as the absolute time of the *first*
3250 : time this request mid was processed. This is what allows
3251 : the request to eventually time out. */
3252 :
3253 0 : struct timeval timeout;
3254 :
3255 : /* Normally the smbd we asked should respond within
3256 : * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
3257 : * the client did, give twice the timeout as a safety
3258 : * measure here in case the other smbd is stuck
3259 : * somewhere else. */
3260 :
3261 495 : timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
3262 :
3263 495 : if (request_timed_out(req, timeout)) {
3264 0 : return;
3265 : }
3266 :
3267 495 : defer_open(lck, timeout, req, id);
3268 : }
3269 :
3270 : /****************************************************************************
3271 : Reschedule an open call that went asynchronous.
3272 : ****************************************************************************/
3273 :
3274 0 : static void schedule_async_open_timer(struct tevent_context *ev,
3275 : struct tevent_timer *te,
3276 : struct timeval current_time,
3277 : void *private_data)
3278 : {
3279 0 : exit_server("async open timeout");
3280 : }
3281 :
3282 0 : static void schedule_async_open(struct smb_request *req)
3283 : {
3284 0 : struct deferred_open_record *open_rec = NULL;
3285 0 : struct timeval timeout = timeval_set(20, 0);
3286 0 : bool ok;
3287 :
3288 0 : if (request_timed_out(req, timeout)) {
3289 0 : return;
3290 : }
3291 :
3292 0 : open_rec = talloc_zero(NULL, struct deferred_open_record);
3293 0 : if (open_rec == NULL) {
3294 0 : exit_server("deferred_open_record_create failed");
3295 : }
3296 0 : open_rec->async_open = true;
3297 :
3298 0 : ok = push_deferred_open_message_smb(
3299 0 : req, timeout, (struct file_id){0}, open_rec);
3300 0 : if (!ok) {
3301 0 : exit_server("push_deferred_open_message_smb failed");
3302 : }
3303 :
3304 0 : open_rec->te = tevent_add_timer(req->sconn->ev_ctx,
3305 : req,
3306 : timeval_current_ofs(20, 0),
3307 : schedule_async_open_timer,
3308 : open_rec);
3309 0 : if (open_rec->te == NULL) {
3310 0 : exit_server("tevent_add_timer failed");
3311 : }
3312 : }
3313 :
3314 449980 : static NTSTATUS check_and_store_share_mode(
3315 : struct files_struct *fsp,
3316 : struct smb_request *req,
3317 : struct share_mode_lock *lck,
3318 : uint32_t create_disposition,
3319 : uint32_t access_mask,
3320 : uint32_t share_access,
3321 : int oplock_request,
3322 : const struct smb2_lease *lease,
3323 : bool first_open_attempt)
3324 : {
3325 991 : NTSTATUS status;
3326 449980 : int oplock_type = NO_OPLOCK;
3327 449980 : uint32_t granted_lease = 0;
3328 449980 : const struct smb2_lease_key *lease_key = NULL;
3329 991 : bool delete_on_close;
3330 991 : bool ok;
3331 :
3332 : /* Get the types we need to examine. */
3333 449980 : if (!validate_oplock_types(lck)) {
3334 0 : smb_panic("validate_oplock_types failed");
3335 : }
3336 :
3337 449980 : delete_on_close = has_delete_on_close(lck, fsp->name_hash);
3338 449980 : if (delete_on_close) {
3339 158 : return NT_STATUS_DELETE_PENDING;
3340 : }
3341 :
3342 449822 : status = handle_share_mode_lease(fsp,
3343 : lck,
3344 : create_disposition,
3345 : access_mask,
3346 : share_access,
3347 : oplock_request,
3348 : lease,
3349 : first_open_attempt,
3350 : &oplock_type,
3351 : &granted_lease);
3352 449822 : if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
3353 495 : schedule_defer_open(lck, fsp->file_id, req);
3354 495 : return NT_STATUS_SHARING_VIOLATION;
3355 : }
3356 449327 : if (!NT_STATUS_IS_OK(status)) {
3357 10751 : return status;
3358 : }
3359 :
3360 438576 : if (oplock_type == LEASE_OPLOCK) {
3361 972 : lease_key = &lease->lease_key;
3362 : }
3363 :
3364 438576 : share_mode_flags_restrict(lck, access_mask, share_access, 0);
3365 :
3366 877152 : ok = set_share_mode(lck,
3367 : fsp,
3368 438576 : get_current_uid(fsp->conn),
3369 : req ? req->mid : 0,
3370 : oplock_type,
3371 : lease_key,
3372 : share_access,
3373 : access_mask);
3374 438576 : if (!ok) {
3375 0 : return NT_STATUS_NO_MEMORY;
3376 : }
3377 :
3378 438576 : if (oplock_type == LEASE_OPLOCK) {
3379 972 : status = grant_fsp_lease(fsp, lck, lease, granted_lease);
3380 972 : if (!NT_STATUS_IS_OK(status)) {
3381 0 : del_share_mode(lck, fsp);
3382 0 : return status;
3383 : }
3384 :
3385 972 : DBG_DEBUG("lease_state=%d\n", fsp->lease->lease.lease_state);
3386 : }
3387 :
3388 438576 : fsp->oplock_type = oplock_type;
3389 :
3390 438576 : return NT_STATUS_OK;
3391 : }
3392 :
3393 : /****************************************************************************
3394 : Work out what access_mask to use from what the client sent us.
3395 : ****************************************************************************/
3396 :
3397 3335 : static NTSTATUS smbd_calculate_maximum_allowed_access_fsp(
3398 : struct files_struct *dirfsp,
3399 : struct files_struct *fsp,
3400 : bool use_privs,
3401 : uint32_t *p_access_mask)
3402 : {
3403 3335 : struct security_descriptor *sd = NULL;
3404 3335 : uint32_t access_granted = 0;
3405 47 : uint32_t dosattrs;
3406 47 : NTSTATUS status;
3407 :
3408 : /* Cope with symlinks */
3409 3335 : if (fsp == NULL || fsp_get_pathref_fd(fsp) == -1) {
3410 1523 : *p_access_mask = FILE_GENERIC_ALL;
3411 1523 : return NT_STATUS_OK;
3412 : }
3413 :
3414 : /* Cope with fake/printer fsp's. */
3415 1812 : if (fsp->fake_file_handle != NULL || fsp->print_file != NULL) {
3416 0 : *p_access_mask = FILE_GENERIC_ALL;
3417 0 : return NT_STATUS_OK;
3418 : }
3419 :
3420 1812 : if (!use_privs && (get_current_uid(fsp->conn) == (uid_t)0)) {
3421 12 : *p_access_mask |= FILE_GENERIC_ALL;
3422 12 : return NT_STATUS_OK;
3423 : }
3424 :
3425 1800 : status = SMB_VFS_FGET_NT_ACL(metadata_fsp(fsp),
3426 : (SECINFO_OWNER |
3427 : SECINFO_GROUP |
3428 : SECINFO_DACL),
3429 : talloc_tos(),
3430 : &sd);
3431 :
3432 1800 : if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3433 : /*
3434 : * File did not exist
3435 : */
3436 0 : *p_access_mask = FILE_GENERIC_ALL;
3437 0 : return NT_STATUS_OK;
3438 : }
3439 1800 : if (!NT_STATUS_IS_OK(status)) {
3440 0 : DBG_ERR("Could not get acl on file %s: %s\n",
3441 : fsp_str_dbg(fsp),
3442 : nt_errstr(status));
3443 0 : return status;
3444 : }
3445 :
3446 : /*
3447 : * If we can access the path to this file, by
3448 : * default we have FILE_READ_ATTRIBUTES from the
3449 : * containing directory. See the section:
3450 : * "Algorithm to Check Access to an Existing File"
3451 : * in MS-FSA.pdf.
3452 : *
3453 : * se_file_access_check()
3454 : * also takes care of owner WRITE_DAC and READ_CONTROL.
3455 : */
3456 1800 : status = se_file_access_check(sd,
3457 1800 : get_current_nttok(fsp->conn),
3458 : use_privs,
3459 1800 : (*p_access_mask & ~FILE_READ_ATTRIBUTES),
3460 : &access_granted);
3461 :
3462 1800 : TALLOC_FREE(sd);
3463 :
3464 1800 : if (!NT_STATUS_IS_OK(status)) {
3465 20 : DBG_ERR("Status %s on file %s: "
3466 : "when calculating maximum access\n",
3467 : nt_errstr(status),
3468 : fsp_str_dbg(fsp));
3469 20 : return status;
3470 : }
3471 :
3472 1780 : *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
3473 :
3474 1780 : if (!(access_granted & DELETE_ACCESS)) {
3475 266 : if (can_delete_file_in_directory(fsp->conn,
3476 : dirfsp,
3477 266 : fsp->fsp_name)) {
3478 266 : *p_access_mask |= DELETE_ACCESS;
3479 : }
3480 : }
3481 :
3482 1780 : dosattrs = fdos_mode(fsp);
3483 1780 : if ((dosattrs & FILE_ATTRIBUTE_READONLY) || !CAN_WRITE(fsp->conn)) {
3484 4 : *p_access_mask &= ~(FILE_GENERIC_WRITE | DELETE_ACCESS);
3485 : }
3486 :
3487 1780 : return NT_STATUS_OK;
3488 : }
3489 :
3490 499159 : NTSTATUS smbd_calculate_access_mask_fsp(struct files_struct *dirfsp,
3491 : struct files_struct *fsp,
3492 : bool use_privs,
3493 : uint32_t access_mask,
3494 : uint32_t *access_mask_out)
3495 : {
3496 1220 : NTSTATUS status;
3497 499159 : uint32_t orig_access_mask = access_mask;
3498 1220 : uint32_t rejected_share_access;
3499 :
3500 499159 : if (access_mask & SEC_MASK_INVALID) {
3501 456 : DBG_DEBUG("access_mask [%8x] contains invalid bits\n",
3502 : access_mask);
3503 456 : return NT_STATUS_ACCESS_DENIED;
3504 : }
3505 :
3506 : /*
3507 : * Convert GENERIC bits to specific bits.
3508 : */
3509 :
3510 498703 : se_map_generic(&access_mask, &file_generic_mapping);
3511 :
3512 : /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
3513 498703 : if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
3514 :
3515 3335 : status = smbd_calculate_maximum_allowed_access_fsp(
3516 : dirfsp,
3517 : fsp,
3518 : use_privs,
3519 : &access_mask);
3520 :
3521 3335 : if (!NT_STATUS_IS_OK(status)) {
3522 20 : return status;
3523 : }
3524 :
3525 3315 : access_mask &= fsp->conn->share_access;
3526 : }
3527 :
3528 498683 : rejected_share_access = access_mask & ~(fsp->conn->share_access);
3529 :
3530 498683 : if (rejected_share_access) {
3531 0 : DBG_INFO("Access denied on file %s: "
3532 : "rejected by share access mask[0x%08X] "
3533 : "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
3534 : fsp_str_dbg(fsp),
3535 : fsp->conn->share_access,
3536 : orig_access_mask, access_mask,
3537 : rejected_share_access);
3538 0 : return NT_STATUS_ACCESS_DENIED;
3539 : }
3540 :
3541 498683 : *access_mask_out = access_mask;
3542 498683 : return NT_STATUS_OK;
3543 : }
3544 :
3545 : /****************************************************************************
3546 : Remove the deferred open entry under lock.
3547 : ****************************************************************************/
3548 :
3549 : /****************************************************************************
3550 : Return true if this is a state pointer to an asynchronous create.
3551 : ****************************************************************************/
3552 :
3553 4458 : bool is_deferred_open_async(const struct deferred_open_record *rec)
3554 : {
3555 4458 : return rec->async_open;
3556 : }
3557 :
3558 195959 : static bool clear_ads(uint32_t create_disposition)
3559 : {
3560 195959 : bool ret = false;
3561 :
3562 195959 : switch (create_disposition) {
3563 754 : case FILE_SUPERSEDE:
3564 : case FILE_OVERWRITE_IF:
3565 : case FILE_OVERWRITE:
3566 799 : ret = true;
3567 799 : break;
3568 194891 : default:
3569 194891 : break;
3570 : }
3571 195690 : return ret;
3572 : }
3573 :
3574 405306 : static int disposition_to_open_flags(uint32_t create_disposition)
3575 : {
3576 405306 : int ret = 0;
3577 :
3578 : /*
3579 : * Currently we're using FILE_SUPERSEDE as the same as
3580 : * FILE_OVERWRITE_IF but they really are
3581 : * different. FILE_SUPERSEDE deletes an existing file
3582 : * (requiring delete access) then recreates it.
3583 : */
3584 :
3585 405306 : switch (create_disposition) {
3586 9287 : case FILE_SUPERSEDE:
3587 : case FILE_OVERWRITE_IF:
3588 : /*
3589 : * If file exists replace/overwrite. If file doesn't
3590 : * exist create.
3591 : */
3592 9287 : ret = O_CREAT|O_TRUNC;
3593 9287 : break;
3594 :
3595 230954 : case FILE_OPEN:
3596 : /*
3597 : * If file exists open. If file doesn't exist error.
3598 : */
3599 230954 : ret = 0;
3600 230954 : break;
3601 :
3602 2179 : case FILE_OVERWRITE:
3603 : /*
3604 : * If file exists overwrite. If file doesn't exist
3605 : * error.
3606 : */
3607 2179 : ret = O_TRUNC;
3608 2179 : break;
3609 :
3610 131240 : case FILE_CREATE:
3611 : /*
3612 : * If file exists error. If file doesn't exist create.
3613 : */
3614 131240 : ret = O_CREAT|O_EXCL;
3615 131240 : break;
3616 :
3617 30693 : case FILE_OPEN_IF:
3618 : /*
3619 : * If file exists open. If file doesn't exist create.
3620 : */
3621 30693 : ret = O_CREAT;
3622 30693 : break;
3623 : }
3624 405306 : return ret;
3625 : }
3626 :
3627 403910 : static int calculate_open_access_flags(uint32_t access_mask,
3628 : uint32_t private_flags)
3629 : {
3630 835 : bool need_write, need_read;
3631 :
3632 : /*
3633 : * Note that we ignore the append flag as append does not
3634 : * mean the same thing under DOS and Unix.
3635 : */
3636 :
3637 403910 : need_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA));
3638 403910 : if (!need_write) {
3639 222455 : return O_RDONLY;
3640 : }
3641 :
3642 : /* DENY_DOS opens are always underlying read-write on the
3643 : file handle, no matter what the requested access mask
3644 : says. */
3645 :
3646 181670 : need_read =
3647 360873 : ((private_flags & NTCREATEX_FLAG_DENY_DOS) ||
3648 180236 : access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|
3649 : FILE_READ_EA|FILE_EXECUTE));
3650 :
3651 181145 : if (!need_read) {
3652 7594 : return O_WRONLY;
3653 : }
3654 173044 : return O_RDWR;
3655 : }
3656 :
3657 : struct open_ntcreate_lock_state {
3658 : struct share_mode_entry_prepare_state prepare_state;
3659 : struct files_struct *fsp;
3660 : const char *object_type;
3661 : struct smb_request *req;
3662 : uint32_t create_disposition;
3663 : uint32_t access_mask;
3664 : uint32_t share_access;
3665 : int oplock_request;
3666 : const struct smb2_lease *lease;
3667 : bool first_open_attempt;
3668 : bool keep_locked;
3669 : NTSTATUS status;
3670 : struct timespec write_time;
3671 : share_mode_entry_prepare_unlock_fn_t cleanup_fn;
3672 : };
3673 :
3674 449980 : static void open_ntcreate_lock_add_entry(struct share_mode_lock *lck,
3675 : bool *keep_locked,
3676 : void *private_data)
3677 : {
3678 449980 : struct open_ntcreate_lock_state *state =
3679 : (struct open_ntcreate_lock_state *)private_data;
3680 :
3681 : /*
3682 : * By default drop the g_lock again if we leave the
3683 : * tdb chainlock.
3684 : */
3685 449980 : *keep_locked = false;
3686 :
3687 449980 : state->status = check_and_store_share_mode(state->fsp,
3688 : state->req,
3689 : lck,
3690 : state->create_disposition,
3691 : state->access_mask,
3692 : state->share_access,
3693 : state->oplock_request,
3694 : state->lease,
3695 449980 : state->first_open_attempt);
3696 449980 : if (!NT_STATUS_IS_OK(state->status)) {
3697 11351 : return;
3698 : }
3699 :
3700 438576 : state->write_time = get_share_mode_write_time(lck);
3701 :
3702 : /*
3703 : * keep the g_lock while existing the tdb chainlock,
3704 : * we we're asked to, which mean we'll keep
3705 : * the share_mode_lock during object creation,
3706 : * or setting delete on close.
3707 : */
3708 438576 : *keep_locked = state->keep_locked;
3709 : }
3710 :
3711 2 : static void open_ntcreate_lock_cleanup_oplock(struct share_mode_lock *lck,
3712 : void *private_data)
3713 : {
3714 2 : struct open_ntcreate_lock_state *state =
3715 : (struct open_ntcreate_lock_state *)private_data;
3716 0 : bool ok;
3717 :
3718 2 : ok = remove_share_oplock(lck, state->fsp);
3719 2 : if (!ok) {
3720 0 : DBG_ERR("Could not remove oplock for %s %s\n",
3721 : state->object_type, fsp_str_dbg(state->fsp));
3722 : }
3723 2 : }
3724 :
3725 24 : static void open_ntcreate_lock_cleanup_entry(struct share_mode_lock *lck,
3726 : void *private_data)
3727 : {
3728 24 : struct open_ntcreate_lock_state *state =
3729 : (struct open_ntcreate_lock_state *)private_data;
3730 0 : bool ok;
3731 :
3732 24 : ok = del_share_mode(lck, state->fsp);
3733 24 : if (!ok) {
3734 0 : DBG_ERR("Could not delete share entry for %s %s\n",
3735 : state->object_type, fsp_str_dbg(state->fsp));
3736 : }
3737 24 : }
3738 :
3739 356593 : static void possibly_set_archive(struct connection_struct *conn,
3740 : struct files_struct *fsp,
3741 : struct smb_filename *smb_fname,
3742 : struct smb_filename *parent_dir_fname,
3743 : int info,
3744 : uint32_t dosattrs,
3745 : mode_t *unx_mode)
3746 : {
3747 356593 : bool set_archive = false;
3748 592 : int ret;
3749 :
3750 356593 : if (info == FILE_WAS_OPENED) {
3751 194867 : return;
3752 : }
3753 :
3754 : /* Overwritten files should be initially set as archive */
3755 161457 : if ((info == FILE_WAS_OVERWRITTEN && lp_map_archive(SNUM(conn)))) {
3756 315 : set_archive = true;
3757 161142 : } else if (lp_store_dos_attributes(SNUM(conn))) {
3758 160819 : set_archive = true;
3759 : }
3760 161134 : if (!set_archive) {
3761 0 : return;
3762 : }
3763 :
3764 161457 : ret = file_set_dosmode(conn,
3765 : smb_fname,
3766 : dosattrs | FILE_ATTRIBUTE_ARCHIVE,
3767 : parent_dir_fname,
3768 : true);
3769 161457 : if (ret != 0) {
3770 0 : return;
3771 : }
3772 161457 : *unx_mode = smb_fname->st.st_ex_mode;
3773 : }
3774 :
3775 : /****************************************************************************
3776 : Open a file with a share mode. Passed in an already created files_struct *.
3777 : ****************************************************************************/
3778 :
3779 499490 : static NTSTATUS open_file_ntcreate(connection_struct *conn,
3780 : struct smb_request *req,
3781 : uint32_t access_mask, /* access bits (FILE_READ_DATA etc.) */
3782 : uint32_t share_access, /* share constants (FILE_SHARE_READ etc) */
3783 : uint32_t create_disposition, /* FILE_OPEN_IF etc. */
3784 : uint32_t create_options, /* options such as delete on close. */
3785 : uint32_t new_dos_attributes, /* attributes used for new file. */
3786 : int oplock_request, /* internal Samba oplock codes. */
3787 : const struct smb2_lease *lease,
3788 : /* Information (FILE_EXISTS etc.) */
3789 : uint32_t private_flags, /* Samba specific flags. */
3790 : struct smb_filename *parent_dir_fname, /* parent. */
3791 : struct smb_filename *smb_fname_atname, /* atname relative to parent. */
3792 : int *pinfo,
3793 : files_struct *fsp)
3794 : {
3795 499490 : struct smb_filename *smb_fname = fsp->fsp_name;
3796 499490 : int flags=0;
3797 499490 : bool file_existed = VALID_STAT(smb_fname->st);
3798 499490 : bool def_acl = False;
3799 499490 : bool posix_open = False;
3800 499490 : bool new_file_created = False;
3801 499490 : bool first_open_attempt = true;
3802 499490 : NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
3803 499490 : mode_t new_unx_mode = (mode_t)0;
3804 499490 : mode_t unx_mode = (mode_t)0;
3805 1144 : int info;
3806 499490 : uint32_t existing_dos_attributes = 0;
3807 499490 : struct open_ntcreate_lock_state lck_state = {};
3808 499490 : bool keep_locked = false;
3809 499490 : uint32_t open_access_mask = access_mask;
3810 1144 : NTSTATUS status;
3811 499490 : SMB_STRUCT_STAT saved_stat = smb_fname->st;
3812 1144 : struct timespec old_write_time;
3813 499490 : bool setup_poll = false;
3814 1144 : NTSTATUS ulstatus;
3815 :
3816 499490 : if (conn->printer) {
3817 : /*
3818 : * Printers are handled completely differently.
3819 : * Most of the passed parameters are ignored.
3820 : */
3821 :
3822 2 : if (pinfo) {
3823 2 : *pinfo = FILE_WAS_CREATED;
3824 : }
3825 :
3826 2 : DBG_DEBUG("printer open fname=%s\n",
3827 : smb_fname_str_dbg(smb_fname));
3828 :
3829 2 : if (!req) {
3830 0 : DBG_ERR("printer open without an SMB request!\n");
3831 0 : return NT_STATUS_INTERNAL_ERROR;
3832 : }
3833 :
3834 2 : return print_spool_open(fsp, smb_fname->base_name,
3835 : req->vuid);
3836 : }
3837 :
3838 499488 : if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3839 2073 : posix_open = True;
3840 2073 : unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
3841 2073 : new_dos_attributes = 0;
3842 : } else {
3843 : /* Windows allows a new file to be created and
3844 : silently removes a FILE_ATTRIBUTE_DIRECTORY
3845 : sent by the client. Do the same. */
3846 :
3847 497415 : new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
3848 :
3849 : /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
3850 : * created new. */
3851 497415 : unx_mode = unix_mode(
3852 : conn,
3853 : new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
3854 : smb_fname,
3855 : parent_dir_fname->fsp);
3856 : }
3857 :
3858 499488 : DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
3859 : "access_mask=0x%x share_access=0x%x "
3860 : "create_disposition = 0x%x create_options=0x%x "
3861 : "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
3862 : smb_fname_str_dbg(smb_fname), new_dos_attributes,
3863 : access_mask, share_access, create_disposition,
3864 : create_options, (unsigned int)unx_mode, oplock_request,
3865 : (unsigned int)private_flags));
3866 :
3867 499488 : if (req == NULL) {
3868 : /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
3869 8403 : SMB_ASSERT(oplock_request == INTERNAL_OPEN_ONLY);
3870 : } else {
3871 : /* And req != NULL means no INTERNAL_OPEN_ONLY */
3872 491085 : SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) == 0));
3873 : }
3874 :
3875 : /*
3876 : * Only non-internal opens can be deferred at all
3877 : */
3878 :
3879 499488 : if (req) {
3880 1139 : struct deferred_open_record *open_rec;
3881 491085 : if (get_deferred_open_message_state(req, NULL, &open_rec)) {
3882 :
3883 : /* If it was an async create retry, the file
3884 : didn't exist. */
3885 :
3886 4454 : if (is_deferred_open_async(open_rec)) {
3887 0 : SET_STAT_INVALID(smb_fname->st);
3888 0 : file_existed = false;
3889 : }
3890 :
3891 : /* Ensure we don't reprocess this message. */
3892 4454 : remove_deferred_open_message_smb(req->xconn, req->mid);
3893 :
3894 4454 : first_open_attempt = false;
3895 : }
3896 : }
3897 :
3898 499488 : if (!posix_open) {
3899 497415 : new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
3900 497415 : if (file_existed) {
3901 : /*
3902 : * Only use stored DOS attributes for checks
3903 : * against requested attributes (below via
3904 : * open_match_attributes()), cf bug #11992
3905 : * for details. -slow
3906 : */
3907 243250 : uint32_t attr = 0;
3908 :
3909 243250 : status = vfs_fget_dos_attributes(smb_fname->fsp, &attr);
3910 243250 : if (NT_STATUS_IS_OK(status)) {
3911 215477 : existing_dos_attributes = attr;
3912 : }
3913 : }
3914 : }
3915 :
3916 : /* ignore any oplock requests if oplocks are disabled */
3917 499488 : if (!lp_oplocks(SNUM(conn)) ||
3918 499488 : IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
3919 : /* Mask off everything except the private Samba bits. */
3920 0 : oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
3921 : }
3922 :
3923 : /* this is for OS/2 long file names - say we don't support them */
3924 499488 : if (req != NULL && !req->posix_pathnames &&
3925 488966 : strstr(smb_fname->base_name,".+,;=[].")) {
3926 : /* OS/2 Workplace shell fix may be main code stream in a later
3927 : * release. */
3928 13 : DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
3929 : "supported.\n"));
3930 13 : if (use_nt_status()) {
3931 9 : return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3932 : }
3933 4 : return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
3934 : }
3935 :
3936 499475 : switch( create_disposition ) {
3937 325303 : case FILE_OPEN:
3938 : /* If file exists open. If file doesn't exist error. */
3939 325303 : if (!file_existed) {
3940 94011 : DEBUG(5,("open_file_ntcreate: FILE_OPEN "
3941 : "requested for file %s and file "
3942 : "doesn't exist.\n",
3943 : smb_fname_str_dbg(smb_fname)));
3944 94011 : return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3945 : }
3946 230954 : break;
3947 :
3948 2470 : case FILE_OVERWRITE:
3949 : /* If file exists overwrite. If file doesn't exist
3950 : * error. */
3951 2470 : if (!file_existed) {
3952 25 : DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
3953 : "requested for file %s and file "
3954 : "doesn't exist.\n",
3955 : smb_fname_str_dbg(smb_fname) ));
3956 25 : return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3957 : }
3958 2179 : break;
3959 :
3960 131390 : case FILE_CREATE:
3961 : /* If file exists error. If file doesn't exist
3962 : * create. */
3963 131390 : if (file_existed) {
3964 103 : DEBUG(5,("open_file_ntcreate: FILE_CREATE "
3965 : "requested for file %s and file "
3966 : "already exists.\n",
3967 : smb_fname_str_dbg(smb_fname)));
3968 103 : if (S_ISDIR(smb_fname->st.st_ex_mode)) {
3969 20 : return NT_STATUS_FILE_IS_A_DIRECTORY;
3970 : }
3971 83 : return NT_STATUS_OBJECT_NAME_COLLISION;
3972 : }
3973 131240 : break;
3974 :
3975 39980 : case FILE_SUPERSEDE:
3976 : case FILE_OVERWRITE_IF:
3977 : case FILE_OPEN_IF:
3978 39980 : break;
3979 30 : default:
3980 30 : return NT_STATUS_INVALID_PARAMETER;
3981 : }
3982 :
3983 405306 : flags = disposition_to_open_flags(create_disposition);
3984 :
3985 : /* We only care about matching attributes on file exists and
3986 : * overwrite. */
3987 :
3988 405306 : if (!posix_open && file_existed &&
3989 240957 : ((create_disposition == FILE_OVERWRITE) ||
3990 : (create_disposition == FILE_OVERWRITE_IF))) {
3991 2905 : if (!open_match_attributes(conn, existing_dos_attributes,
3992 : new_dos_attributes,
3993 : unx_mode, &new_unx_mode)) {
3994 1064 : DEBUG(5,("open_file_ntcreate: attributes mismatch "
3995 : "for file %s (%x %x) (0%o, 0%o)\n",
3996 : smb_fname_str_dbg(smb_fname),
3997 : existing_dos_attributes,
3998 : new_dos_attributes,
3999 : (unsigned int)smb_fname->st.st_ex_mode,
4000 : (unsigned int)unx_mode ));
4001 1064 : return NT_STATUS_ACCESS_DENIED;
4002 : }
4003 : }
4004 :
4005 404242 : status = smbd_calculate_access_mask_fsp(parent_dir_fname->fsp,
4006 : smb_fname->fsp,
4007 : false,
4008 : access_mask,
4009 : &access_mask);
4010 404242 : if (!NT_STATUS_IS_OK(status)) {
4011 332 : DBG_DEBUG("smbd_calculate_access_mask_fsp "
4012 : "on file %s returned %s\n",
4013 : smb_fname_str_dbg(smb_fname),
4014 : nt_errstr(status));
4015 332 : return status;
4016 : }
4017 :
4018 403910 : open_access_mask = access_mask;
4019 :
4020 403910 : if (flags & O_TRUNC) {
4021 10764 : open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
4022 : }
4023 :
4024 403910 : if (file_existed) {
4025 : /*
4026 : * stat opens on existing files don't get oplocks.
4027 : * They can get leases.
4028 : *
4029 : * Note that we check for stat open on the *open_access_mask*,
4030 : * i.e. the access mask we actually used to do the open,
4031 : * not the one the client asked for (which is in
4032 : * fsp->access_mask). This is due to the fact that
4033 : * FILE_OVERWRITE and FILE_OVERWRITE_IF add in O_TRUNC,
4034 : * which adds FILE_WRITE_DATA to open_access_mask.
4035 : */
4036 243216 : if (is_oplock_stat_open(open_access_mask) && lease == NULL) {
4037 31720 : oplock_request = NO_OPLOCK;
4038 : }
4039 : }
4040 :
4041 403910 : DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
4042 : "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
4043 : access_mask));
4044 :
4045 : /*
4046 : * Note that we ignore the append flag as append does not
4047 : * mean the same thing under DOS and Unix.
4048 : */
4049 :
4050 403910 : flags |= calculate_open_access_flags(access_mask, private_flags);
4051 :
4052 : /*
4053 : * Currently we only look at FILE_WRITE_THROUGH for create options.
4054 : */
4055 :
4056 : #if defined(O_SYNC)
4057 403910 : if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
4058 17 : flags |= O_SYNC;
4059 : }
4060 : #endif /* O_SYNC */
4061 :
4062 403910 : if (posix_open && (access_mask & FILE_APPEND_DATA)) {
4063 523 : flags |= O_APPEND;
4064 : }
4065 :
4066 403910 : if (!posix_open && !CAN_WRITE(conn)) {
4067 : /*
4068 : * We should really return a permission denied error if either
4069 : * O_CREAT or O_TRUNC are set, but for compatibility with
4070 : * older versions of Samba we just AND them out.
4071 : */
4072 300 : flags &= ~(O_CREAT | O_TRUNC);
4073 : }
4074 :
4075 : /*
4076 : * With kernel oplocks the open breaking an oplock
4077 : * blocks until the oplock holder has given up the
4078 : * oplock or closed the file. We prevent this by always
4079 : * trying to open the file with O_NONBLOCK (see "man
4080 : * fcntl" on Linux).
4081 : *
4082 : * If a process that doesn't use the smbd open files
4083 : * database or communication methods holds a kernel
4084 : * oplock we must periodically poll for available open
4085 : * using O_NONBLOCK.
4086 : */
4087 403910 : flags |= O_NONBLOCK;
4088 :
4089 : /*
4090 : * Ensure we can't write on a read-only share or file.
4091 : */
4092 :
4093 403910 : if (((flags & O_ACCMODE) != O_RDONLY) && file_existed &&
4094 21787 : (!CAN_WRITE(conn) ||
4095 21533 : (existing_dos_attributes & FILE_ATTRIBUTE_READONLY))) {
4096 1017 : DEBUG(5,("open_file_ntcreate: write access requested for "
4097 : "file %s on read only %s\n",
4098 : smb_fname_str_dbg(smb_fname),
4099 : !CAN_WRITE(conn) ? "share" : "file" ));
4100 1017 : return NT_STATUS_ACCESS_DENIED;
4101 : }
4102 :
4103 402893 : if (VALID_STAT(smb_fname->st)) {
4104 : /*
4105 : * Only try and create a file id before open
4106 : * for an existing file. For a file being created
4107 : * this won't do anything useful until the file
4108 : * exists and has a valid stat struct.
4109 : */
4110 242199 : fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
4111 : }
4112 402893 : fh_set_private_options(fsp->fh, private_flags);
4113 402893 : fsp->access_mask = open_access_mask; /* We change this to the
4114 : * requested access_mask after
4115 : * the open is done. */
4116 402893 : if (posix_open) {
4117 2067 : fsp->posix_flags |= FSP_POSIX_FLAGS_ALL;
4118 : }
4119 :
4120 402893 : if ((create_options & FILE_DELETE_ON_CLOSE) && (flags & O_CREAT) &&
4121 640 : !file_existed) {
4122 : /* Delete on close semantics for new files. */
4123 736 : status = can_set_delete_on_close(fsp,
4124 : new_dos_attributes);
4125 736 : if (!NT_STATUS_IS_OK(status)) {
4126 9 : fd_close(fsp);
4127 9 : return status;
4128 : }
4129 : }
4130 :
4131 : /*
4132 : * Ensure we pay attention to default ACLs on directories if required.
4133 : */
4134 :
4135 552582 : if ((flags & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
4136 150046 : (def_acl = directory_has_default_acl_fsp(parent_dir_fname->fsp))) {
4137 149392 : unx_mode = (0777 & lp_create_mask(SNUM(conn)));
4138 : }
4139 :
4140 402884 : DEBUG(4,
4141 : ("calling open_file with flags=0x%X mode=0%o, "
4142 : "access_mask = 0x%x, open_access_mask = 0x%x\n",
4143 : (unsigned int)flags,
4144 : (unsigned int)unx_mode,
4145 : (unsigned int)access_mask,
4146 : (unsigned int)open_access_mask));
4147 :
4148 : {
4149 402884 : struct vfs_open_how how = {
4150 : .flags = flags,
4151 : .mode = unx_mode,
4152 : };
4153 :
4154 402884 : if (create_options & FILE_OPEN_FOR_BACKUP_INTENT) {
4155 17 : how.resolve |= VFS_OPEN_HOW_WITH_BACKUP_INTENT;
4156 : }
4157 :
4158 402884 : fsp_open = open_file(req,
4159 : parent_dir_fname->fsp,
4160 : smb_fname_atname,
4161 : fsp,
4162 : &how,
4163 : access_mask,
4164 : open_access_mask,
4165 : private_flags,
4166 : &new_file_created);
4167 : }
4168 402884 : if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_NETWORK_BUSY)) {
4169 8 : if (file_existed && S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
4170 0 : DEBUG(10, ("FIFO busy\n"));
4171 0 : return NT_STATUS_NETWORK_BUSY;
4172 : }
4173 8 : if (req == NULL) {
4174 0 : DEBUG(10, ("Internal open busy\n"));
4175 0 : return NT_STATUS_NETWORK_BUSY;
4176 : }
4177 : /*
4178 : * This handles the kernel oplock case:
4179 : *
4180 : * the file has an active kernel oplock and the open() returned
4181 : * EWOULDBLOCK/EAGAIN which maps to NETWORK_BUSY.
4182 : *
4183 : * "Samba locking.tdb oplocks" are handled below after acquiring
4184 : * the sharemode lock with get_share_mode_lock().
4185 : */
4186 8 : setup_poll = true;
4187 : }
4188 :
4189 402884 : if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
4190 : /*
4191 : * EINTR from the open(2) syscall. Just setup a retry
4192 : * in a bit. We can't use the sys_write() tight retry
4193 : * loop here, as we might have to actually deal with
4194 : * lease-break signals to avoid a deadlock.
4195 : */
4196 0 : setup_poll = true;
4197 : }
4198 :
4199 402884 : if (setup_poll) {
4200 : /*
4201 : * Retry once a second. If there's a share_mode_lock
4202 : * around, also wait for it in case it was smbd
4203 : * holding that kernel oplock that can quickly tell us
4204 : * the oplock got removed.
4205 : */
4206 :
4207 8 : setup_poll_open(
4208 : req,
4209 8 : &fsp->file_id,
4210 : timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0),
4211 : timeval_set(1, 0));
4212 :
4213 8 : return NT_STATUS_SHARING_VIOLATION;
4214 : }
4215 :
4216 402876 : if (!NT_STATUS_IS_OK(fsp_open)) {
4217 35107 : bool wait_for_aio = NT_STATUS_EQUAL(
4218 : fsp_open, NT_STATUS_MORE_PROCESSING_REQUIRED);
4219 35107 : if (wait_for_aio) {
4220 0 : schedule_async_open(req);
4221 : }
4222 35107 : return fsp_open;
4223 : }
4224 :
4225 367769 : if (new_file_created) {
4226 : /*
4227 : * As we atomically create using O_CREAT|O_EXCL,
4228 : * then if new_file_created is true, then
4229 : * file_existed *MUST* have been false (even
4230 : * if the file was previously detected as being
4231 : * there).
4232 : */
4233 160382 : file_existed = false;
4234 : }
4235 :
4236 367491 : if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
4237 : /*
4238 : * The file did exist, but some other (local or NFS)
4239 : * process either renamed/unlinked and re-created the
4240 : * file with different dev/ino after we walked the path,
4241 : * but before we did the open. We could retry the
4242 : * open but it's a rare enough case it's easier to
4243 : * just fail the open to prevent creating any problems
4244 : * in the open file db having the wrong dev/ino key.
4245 : */
4246 0 : fd_close(fsp);
4247 0 : DBG_WARNING("file %s - dev/ino mismatch. "
4248 : "Old (dev=%ju, ino=%ju). "
4249 : "New (dev=%ju, ino=%ju). Failing open "
4250 : "with NT_STATUS_ACCESS_DENIED.\n",
4251 : smb_fname_str_dbg(smb_fname),
4252 : (uintmax_t)saved_stat.st_ex_dev,
4253 : (uintmax_t)saved_stat.st_ex_ino,
4254 : (uintmax_t)smb_fname->st.st_ex_dev,
4255 : (uintmax_t)smb_fname->st.st_ex_ino);
4256 0 : return NT_STATUS_ACCESS_DENIED;
4257 : }
4258 :
4259 367769 : old_write_time = smb_fname->st.st_ex_mtime;
4260 :
4261 : /*
4262 : * Deal with the race condition where two smbd's detect the
4263 : * file doesn't exist and do the create at the same time. One
4264 : * of them will win and set a share mode, the other (ie. this
4265 : * one) should check if the requested share mode for this
4266 : * create is allowed.
4267 : */
4268 :
4269 : /*
4270 : * Now the file exists and fsp is successfully opened,
4271 : * fsp->dev and fsp->inode are valid and should replace the
4272 : * dev=0,inode=0 from a non existent file. Spotted by
4273 : * Nadav Danieli <nadavd@exanet.com>. JRA.
4274 : */
4275 :
4276 367769 : if (new_file_created) {
4277 160382 : info = FILE_WAS_CREATED;
4278 : } else {
4279 207109 : if (flags & O_TRUNC) {
4280 884 : info = FILE_WAS_OVERWRITTEN;
4281 : } else {
4282 206178 : info = FILE_WAS_OPENED;
4283 : }
4284 : }
4285 :
4286 : /*
4287 : * If we created a new file, overwrite an existing one
4288 : * or going to delete it later, we should keep
4289 : * the share_mode_lock (g_lock) until we call
4290 : * share_mode_entry_prepare_unlock()
4291 : */
4292 367444 : if (info != FILE_WAS_OPENED) {
4293 161266 : keep_locked = true;
4294 206178 : } else if (create_options & FILE_DELETE_ON_CLOSE) {
4295 134299 : keep_locked = true;
4296 : }
4297 :
4298 367769 : lck_state = (struct open_ntcreate_lock_state) {
4299 : .fsp = fsp,
4300 : .object_type = "file",
4301 : .req = req,
4302 : .create_disposition = create_disposition,
4303 : .access_mask = access_mask,
4304 : .share_access = share_access,
4305 : .oplock_request = oplock_request,
4306 : .lease = lease,
4307 : .first_open_attempt = first_open_attempt,
4308 : .keep_locked = keep_locked,
4309 : };
4310 :
4311 367769 : status = share_mode_entry_prepare_lock_add(&lck_state.prepare_state,
4312 : fsp->file_id,
4313 : conn->connectpath,
4314 : smb_fname,
4315 : &old_write_time,
4316 : open_ntcreate_lock_add_entry,
4317 643 : &lck_state);
4318 367769 : if (!NT_STATUS_IS_OK(status)) {
4319 0 : DBG_ERR("share_mode_entry_prepare_lock_add() failed for %s - %s\n",
4320 : smb_fname_str_dbg(smb_fname), nt_errstr(status));
4321 0 : fd_close(fsp);
4322 0 : return status;
4323 : }
4324 :
4325 367769 : status = lck_state.status;
4326 367769 : if (!NT_STATUS_IS_OK(status)) {
4327 11152 : fd_close(fsp);
4328 11152 : return status;
4329 : }
4330 :
4331 : /*
4332 : * From here we need to use 'goto unlock;' instead of return !!!
4333 : */
4334 :
4335 356617 : if (fsp->oplock_type != NO_OPLOCK && fsp->oplock_type != LEASE_OPLOCK) {
4336 : /*
4337 : * Now ask for kernel oplocks
4338 : * and cleanup on failure.
4339 : */
4340 1706 : status = set_file_oplock(fsp);
4341 1706 : if (!NT_STATUS_IS_OK(status)) {
4342 : /*
4343 : * Could not get the kernel oplock
4344 : */
4345 2 : lck_state.cleanup_fn =
4346 : open_ntcreate_lock_cleanup_oplock;
4347 2 : fsp->oplock_type = NO_OPLOCK;
4348 : }
4349 : }
4350 :
4351 : /* Should we atomically (to the client at least) truncate ? */
4352 356617 : if ((!new_file_created) && (flags & O_TRUNC) &&
4353 799 : (S_ISREG(fsp->fsp_name->st.st_ex_mode))) {
4354 45 : int ret;
4355 :
4356 799 : ret = SMB_VFS_FTRUNCATE(fsp, 0);
4357 799 : if (ret != 0) {
4358 0 : status = map_nt_error_from_unix(errno);
4359 0 : lck_state.cleanup_fn =
4360 : open_ntcreate_lock_cleanup_entry;
4361 0 : goto unlock;
4362 : }
4363 799 : notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
4364 : FILE_NOTIFY_CHANGE_SIZE
4365 : | FILE_NOTIFY_CHANGE_ATTRIBUTES,
4366 799 : fsp->fsp_name->base_name);
4367 : }
4368 :
4369 : /*
4370 : * We have the share entry *locked*.....
4371 : */
4372 :
4373 : /* Delete streams if create_disposition requires it */
4374 552262 : if (!new_file_created &&
4375 195690 : clear_ads(create_disposition) &&
4376 799 : !fsp_is_alternate_stream(fsp)) {
4377 755 : status = delete_all_streams(conn, smb_fname);
4378 755 : if (!NT_STATUS_IS_OK(status)) {
4379 0 : lck_state.cleanup_fn =
4380 : open_ntcreate_lock_cleanup_entry;
4381 0 : goto unlock;
4382 : }
4383 : }
4384 :
4385 537045 : if (!fsp->fsp_flags.is_pathref &&
4386 360856 : fsp_get_io_fd(fsp) != -1 &&
4387 180428 : lp_kernel_share_modes(SNUM(conn)))
4388 : {
4389 0 : int ret;
4390 : /*
4391 : * Beware: streams implementing VFS modules may
4392 : * implement streams in a way that fsp will have the
4393 : * basefile open in the fsp fd, so lacking a distinct
4394 : * fd for the stream the file-system sharemode will
4395 : * apply on the basefile which is wrong. The actual
4396 : * check is deferred to the VFS module implementing
4397 : * the file-system sharemode call.
4398 : */
4399 0 : ret = SMB_VFS_FILESYSTEM_SHAREMODE(fsp,
4400 : share_access,
4401 : access_mask);
4402 0 : if (ret == -1){
4403 0 : status = NT_STATUS_SHARING_VIOLATION;
4404 0 : lck_state.cleanup_fn =
4405 : open_ntcreate_lock_cleanup_entry;
4406 0 : goto unlock;
4407 : }
4408 :
4409 0 : fsp->fsp_flags.kernel_share_modes_taken = true;
4410 : }
4411 :
4412 : /*
4413 : * At this point onwards, we can guarantee that the share entry
4414 : * is locked, whether we created the file or not, and that the
4415 : * deny mode is compatible with all current opens.
4416 : */
4417 :
4418 : /*
4419 : * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
4420 : * but we don't have to store this - just ignore it on access check.
4421 : */
4422 356617 : if (conn->sconn->using_smb2) {
4423 : /*
4424 : * SMB2 doesn't return it (according to Microsoft tests).
4425 : * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
4426 : * File created with access = 0x7 (Read, Write, Delete)
4427 : * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
4428 : */
4429 301097 : fsp->access_mask = access_mask;
4430 : } else {
4431 : /* But SMB1 does. */
4432 55520 : fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
4433 : }
4434 :
4435 356617 : if (pinfo) {
4436 356617 : *pinfo = info;
4437 : }
4438 :
4439 : /* Handle strange delete on close create semantics. */
4440 356617 : if (create_options & FILE_DELETE_ON_CLOSE) {
4441 134962 : if (!new_file_created) {
4442 134235 : status = can_set_delete_on_close(fsp,
4443 : existing_dos_attributes);
4444 :
4445 134235 : if (!NT_STATUS_IS_OK(status)) {
4446 : /* Remember to delete the mode we just added. */
4447 24 : lck_state.cleanup_fn =
4448 : open_ntcreate_lock_cleanup_entry;
4449 24 : goto unlock;
4450 : }
4451 : }
4452 : /* Note that here we set the *initial* delete on close flag,
4453 : not the regular one. The magic gets handled in close. */
4454 134938 : fsp->fsp_flags.initial_delete_on_close = true;
4455 : }
4456 :
4457 356593 : possibly_set_archive(conn,
4458 : fsp,
4459 : smb_fname,
4460 : parent_dir_fname,
4461 : info,
4462 : new_dos_attributes,
4463 : &smb_fname->st.st_ex_mode);
4464 :
4465 : /* Determine sparse flag. */
4466 356593 : if (posix_open) {
4467 : /* POSIX opens are sparse by default. */
4468 1547 : fsp->fsp_flags.is_sparse = true;
4469 : } else {
4470 355046 : fsp->fsp_flags.is_sparse =
4471 355046 : (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE);
4472 : }
4473 :
4474 : /*
4475 : * Take care of inherited ACLs on created files - if default ACL not
4476 : * selected.
4477 : */
4478 :
4479 356593 : if (!posix_open && new_file_created && !def_acl) {
4480 20656 : if (unx_mode != smb_fname->st.st_ex_mode) {
4481 20656 : int ret = SMB_VFS_FCHMOD(fsp, unx_mode);
4482 20656 : if (ret == -1) {
4483 0 : DBG_INFO("failed to reset "
4484 : "attributes of file %s to 0%o\n",
4485 : smb_fname_str_dbg(smb_fname),
4486 : (unsigned int)unx_mode);
4487 : }
4488 : }
4489 :
4490 335937 : } else if (new_unx_mode) {
4491 : /*
4492 : * We only get here in the case of:
4493 : *
4494 : * a). Not a POSIX open.
4495 : * b). File already existed.
4496 : * c). File was overwritten.
4497 : * d). Requested DOS attributes didn't match
4498 : * the DOS attributes on the existing file.
4499 : *
4500 : * In that case new_unx_mode has been set
4501 : * equal to the calculated mode (including
4502 : * possible inheritance of the mode from the
4503 : * containing directory).
4504 : *
4505 : * Note this mode was calculated with the
4506 : * DOS attribute FILE_ATTRIBUTE_ARCHIVE added,
4507 : * so the mode change here is suitable for
4508 : * an overwritten file.
4509 : */
4510 :
4511 198 : if (new_unx_mode != smb_fname->st.st_ex_mode) {
4512 198 : int ret = SMB_VFS_FCHMOD(fsp, new_unx_mode);
4513 198 : if (ret == -1) {
4514 0 : DBG_INFO("failed to reset "
4515 : "attributes of file %s to 0%o\n",
4516 : smb_fname_str_dbg(smb_fname),
4517 : (unsigned int)new_unx_mode);
4518 : }
4519 : }
4520 : }
4521 :
4522 : /*
4523 : * Deal with other opens having a modified write time.
4524 : */
4525 357185 : if (fsp_getinfo_ask_sharemode(fsp) &&
4526 355046 : !is_omit_timespec(&lck_state.write_time))
4527 : {
4528 355046 : update_stat_ex_mtime(&fsp->fsp_name->st, lck_state.write_time);
4529 : }
4530 :
4531 356001 : status = NT_STATUS_OK;
4532 :
4533 356617 : unlock:
4534 356617 : ulstatus = share_mode_entry_prepare_unlock(&lck_state.prepare_state,
4535 : lck_state.cleanup_fn,
4536 592 : &lck_state);
4537 356617 : if (!NT_STATUS_IS_OK(ulstatus)) {
4538 0 : DBG_ERR("share_mode_entry_prepare_unlock() failed for %s - %s\n",
4539 : smb_fname_str_dbg(smb_fname), nt_errstr(ulstatus));
4540 0 : smb_panic("share_mode_entry_prepare_unlock() failed!");
4541 : }
4542 :
4543 356617 : if (!NT_STATUS_IS_OK(status)) {
4544 24 : fd_close(fsp);
4545 24 : return status;
4546 : }
4547 :
4548 356593 : return NT_STATUS_OK;
4549 : }
4550 :
4551 11001 : static NTSTATUS mkdir_internal(connection_struct *conn,
4552 : struct smb_filename *parent_dir_fname, /* parent. */
4553 : struct smb_filename *smb_fname_atname, /* atname relative to parent. */
4554 : struct smb_filename *smb_dname, /* full pathname from root of share. */
4555 : uint32_t file_attributes,
4556 : struct files_struct *fsp)
4557 : {
4558 68 : const struct loadparm_substitution *lp_sub =
4559 11001 : loadparm_s3_global_substitution();
4560 68 : mode_t mode;
4561 68 : NTSTATUS status;
4562 11001 : bool posix_open = false;
4563 11001 : bool need_re_stat = false;
4564 11001 : uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
4565 11001 : struct vfs_open_how how = { .flags = O_RDONLY|O_DIRECTORY, };
4566 68 : int ret;
4567 :
4568 11001 : if (!CAN_WRITE(conn) || (access_mask & ~(conn->share_access))) {
4569 0 : DEBUG(5,("mkdir_internal: failing share access "
4570 : "%s\n", lp_servicename(talloc_tos(), lp_sub, SNUM(conn))));
4571 0 : return NT_STATUS_ACCESS_DENIED;
4572 : }
4573 :
4574 11001 : if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
4575 562 : posix_open = true;
4576 562 : mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
4577 : } else {
4578 10439 : mode = unix_mode(conn,
4579 : FILE_ATTRIBUTE_DIRECTORY,
4580 : smb_dname,
4581 : parent_dir_fname->fsp);
4582 : }
4583 :
4584 11001 : status = check_parent_access_fsp(parent_dir_fname->fsp, access_mask);
4585 11001 : if(!NT_STATUS_IS_OK(status)) {
4586 0 : DBG_INFO("check_parent_access_fsp "
4587 : "on directory %s for path %s returned %s\n",
4588 : smb_fname_str_dbg(parent_dir_fname),
4589 : smb_dname->base_name,
4590 : nt_errstr(status));
4591 0 : return status;
4592 : }
4593 :
4594 11001 : if (lp_inherit_acls(SNUM(conn))) {
4595 10333 : if (directory_has_default_acl_fsp(parent_dir_fname->fsp)) {
4596 9929 : mode = (0777 & lp_directory_mask(SNUM(conn)));
4597 : }
4598 : }
4599 :
4600 11001 : ret = SMB_VFS_MKDIRAT(conn,
4601 : parent_dir_fname->fsp,
4602 : smb_fname_atname,
4603 : mode);
4604 11001 : if (ret != 0) {
4605 5 : return map_nt_error_from_unix(errno);
4606 : }
4607 :
4608 : /*
4609 : * Make this a pathref fsp for now. open_directory() will reopen as a
4610 : * full fsp.
4611 : */
4612 10996 : fsp->fsp_flags.is_pathref = true;
4613 :
4614 10996 : status = fd_openat(parent_dir_fname->fsp, smb_fname_atname, fsp, &how);
4615 10996 : if (!NT_STATUS_IS_OK(status)) {
4616 0 : return status;
4617 : }
4618 :
4619 : /* Ensure we're checking for a symlink here.... */
4620 : /* We don't want to get caught by a symlink racer. */
4621 :
4622 10996 : status = vfs_stat_fsp(fsp);
4623 10996 : if (!NT_STATUS_IS_OK(status)) {
4624 0 : DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
4625 : smb_fname_str_dbg(smb_dname), nt_errstr(status)));
4626 0 : return status;
4627 : }
4628 :
4629 10996 : if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
4630 0 : DEBUG(0, ("Directory '%s' just created is not a directory !\n",
4631 : smb_fname_str_dbg(smb_dname)));
4632 0 : return NT_STATUS_NOT_A_DIRECTORY;
4633 : }
4634 :
4635 10996 : if (lp_store_dos_attributes(SNUM(conn))) {
4636 10996 : file_set_dosmode(conn,
4637 : smb_dname,
4638 : file_attributes | FILE_ATTRIBUTE_DIRECTORY,
4639 : parent_dir_fname,
4640 : true);
4641 : }
4642 :
4643 10996 : if (lp_inherit_permissions(SNUM(conn))) {
4644 0 : inherit_access_posix_acl(conn, parent_dir_fname->fsp,
4645 : smb_dname, mode);
4646 0 : need_re_stat = true;
4647 : }
4648 :
4649 10996 : if (!posix_open) {
4650 : /*
4651 : * Check if high bits should have been set,
4652 : * then (if bits are missing): add them.
4653 : * Consider bits automagically set by UNIX, i.e. SGID bit from parent
4654 : * dir.
4655 : */
4656 10434 : if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
4657 0 : (mode & ~smb_dname->st.st_ex_mode)) {
4658 0 : SMB_VFS_FCHMOD(fsp,
4659 : (smb_dname->st.st_ex_mode |
4660 : (mode & ~smb_dname->st.st_ex_mode)));
4661 0 : need_re_stat = true;
4662 : }
4663 : }
4664 :
4665 : /* Change the owner if required. */
4666 10996 : if (lp_inherit_owner(SNUM(conn)) != INHERIT_OWNER_NO) {
4667 8 : change_dir_owner_to_parent_fsp(parent_dir_fname->fsp,
4668 : fsp);
4669 8 : need_re_stat = true;
4670 : }
4671 :
4672 10996 : if (need_re_stat) {
4673 8 : status = vfs_stat_fsp(fsp);
4674 8 : if (!NT_STATUS_IS_OK(status)) {
4675 0 : DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
4676 : smb_fname_str_dbg(smb_dname), nt_errstr(status)));
4677 0 : return status;
4678 : }
4679 : }
4680 :
4681 10996 : notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
4682 10996 : smb_dname->base_name);
4683 :
4684 10996 : return NT_STATUS_OK;
4685 : }
4686 :
4687 : /****************************************************************************
4688 : Open a directory from an NT SMB call.
4689 : ****************************************************************************/
4690 :
4691 87441 : static NTSTATUS open_directory(connection_struct *conn,
4692 : struct smb_request *req,
4693 : uint32_t access_mask,
4694 : uint32_t share_access,
4695 : uint32_t create_disposition,
4696 : uint32_t create_options,
4697 : uint32_t file_attributes,
4698 : struct smb_filename *parent_dir_fname,
4699 : struct smb_filename *smb_fname_atname,
4700 : int *pinfo,
4701 : struct files_struct *fsp)
4702 : {
4703 87441 : struct smb_filename *smb_dname = fsp->fsp_name;
4704 87441 : bool dir_existed = VALID_STAT(smb_dname->st);
4705 87441 : struct open_ntcreate_lock_state lck_state = {};
4706 87441 : bool keep_locked = false;
4707 382 : NTSTATUS status;
4708 382 : struct timespec mtimespec;
4709 87441 : int info = 0;
4710 382 : uint32_t need_fd_access;
4711 382 : NTSTATUS ulstatus;
4712 :
4713 87441 : if (is_ntfs_stream_smb_fname(smb_dname)) {
4714 0 : DEBUG(2, ("open_directory: %s is a stream name!\n",
4715 : smb_fname_str_dbg(smb_dname)));
4716 0 : return NT_STATUS_NOT_A_DIRECTORY;
4717 : }
4718 :
4719 87441 : if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
4720 : /* Ensure we have a directory attribute. */
4721 86272 : file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
4722 : }
4723 :
4724 87441 : DBG_INFO("opening directory %s, access_mask = 0x%"PRIx32", "
4725 : "share_access = 0x%"PRIx32" create_options = 0x%"PRIx32", "
4726 : "create_disposition = 0x%"PRIx32", "
4727 : "file_attributes = 0x%"PRIx32"\n",
4728 : smb_fname_str_dbg(smb_dname),
4729 : access_mask,
4730 : share_access,
4731 : create_options,
4732 : create_disposition,
4733 : file_attributes);
4734 :
4735 87441 : status = smbd_calculate_access_mask_fsp(parent_dir_fname->fsp,
4736 : smb_dname->fsp,
4737 : false,
4738 : access_mask,
4739 : &access_mask);
4740 87441 : if (!NT_STATUS_IS_OK(status)) {
4741 144 : DBG_DEBUG("smbd_calculate_access_mask_fsp "
4742 : "on file %s returned %s\n",
4743 : smb_fname_str_dbg(smb_dname),
4744 : nt_errstr(status));
4745 144 : return status;
4746 : }
4747 :
4748 87297 : if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
4749 281 : !security_token_has_privilege(get_current_nttok(conn),
4750 : SEC_PRIV_SECURITY)) {
4751 0 : DEBUG(10, ("open_directory: open on %s "
4752 : "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
4753 : smb_fname_str_dbg(smb_dname)));
4754 0 : return NT_STATUS_PRIVILEGE_NOT_HELD;
4755 : }
4756 :
4757 87297 : switch( create_disposition ) {
4758 73923 : case FILE_OPEN:
4759 :
4760 73923 : if (!dir_existed) {
4761 1620 : return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4762 : }
4763 :
4764 72023 : info = FILE_WAS_OPENED;
4765 72023 : break;
4766 :
4767 10835 : case FILE_CREATE:
4768 :
4769 : /* If directory exists error. If directory doesn't
4770 : * exist create. */
4771 :
4772 10835 : if (dir_existed) {
4773 967 : status = NT_STATUS_OBJECT_NAME_COLLISION;
4774 967 : DEBUG(2, ("open_directory: unable to create "
4775 : "%s. Error was %s\n",
4776 : smb_fname_str_dbg(smb_dname),
4777 : nt_errstr(status)));
4778 967 : return status;
4779 : }
4780 :
4781 9868 : status = mkdir_internal(conn,
4782 : parent_dir_fname,
4783 : smb_fname_atname,
4784 : smb_dname,
4785 : file_attributes,
4786 : fsp);
4787 :
4788 9868 : if (!NT_STATUS_IS_OK(status)) {
4789 0 : DEBUG(2, ("open_directory: unable to create "
4790 : "%s. Error was %s\n",
4791 : smb_fname_str_dbg(smb_dname),
4792 : nt_errstr(status)));
4793 0 : return status;
4794 : }
4795 :
4796 9803 : info = FILE_WAS_CREATED;
4797 9803 : break;
4798 :
4799 2482 : case FILE_OPEN_IF:
4800 : /*
4801 : * If directory exists open. If directory doesn't
4802 : * exist create.
4803 : */
4804 :
4805 2482 : if (dir_existed) {
4806 1697 : status = NT_STATUS_OK;
4807 1342 : info = FILE_WAS_OPENED;
4808 : } else {
4809 1133 : status = mkdir_internal(conn,
4810 : parent_dir_fname,
4811 : smb_fname_atname,
4812 : smb_dname,
4813 : file_attributes,
4814 : fsp);
4815 :
4816 1133 : if (NT_STATUS_IS_OK(status)) {
4817 1125 : info = FILE_WAS_CREATED;
4818 : } else {
4819 0 : int ret;
4820 : /* Cope with create race. */
4821 5 : if (!NT_STATUS_EQUAL(status,
4822 : NT_STATUS_OBJECT_NAME_COLLISION)) {
4823 0 : DEBUG(2, ("open_directory: unable to create "
4824 : "%s. Error was %s\n",
4825 : smb_fname_str_dbg(smb_dname),
4826 : nt_errstr(status)));
4827 0 : return status;
4828 : }
4829 :
4830 : /*
4831 : * If mkdir_internal() returned
4832 : * NT_STATUS_OBJECT_NAME_COLLISION
4833 : * we still must lstat the path.
4834 : */
4835 5 : ret = SMB_VFS_FSTATAT(
4836 : conn,
4837 : parent_dir_fname->fsp,
4838 : smb_fname_atname,
4839 : &smb_dname->st,
4840 : AT_SYMLINK_NOFOLLOW);
4841 5 : if (ret == -1) {
4842 0 : DEBUG(2, ("Could not stat "
4843 : "directory '%s' just "
4844 : "opened: %s\n",
4845 : smb_fname_str_dbg(
4846 : smb_dname),
4847 : strerror(errno)));
4848 0 : return map_nt_error_from_unix(
4849 0 : errno);
4850 : }
4851 :
4852 5 : info = FILE_WAS_OPENED;
4853 : }
4854 : }
4855 :
4856 2472 : break;
4857 :
4858 57 : case FILE_SUPERSEDE:
4859 : case FILE_OVERWRITE:
4860 : case FILE_OVERWRITE_IF:
4861 : default:
4862 57 : DEBUG(5,("open_directory: invalid create_disposition "
4863 : "0x%x for directory %s\n",
4864 : (unsigned int)create_disposition,
4865 : smb_fname_str_dbg(smb_dname)));
4866 57 : return NT_STATUS_INVALID_PARAMETER;
4867 : }
4868 :
4869 84653 : if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
4870 2430 : DEBUG(5,("open_directory: %s is not a directory !\n",
4871 : smb_fname_str_dbg(smb_dname)));
4872 2430 : return NT_STATUS_NOT_A_DIRECTORY;
4873 : }
4874 :
4875 : /*
4876 : * Setup the files_struct for it.
4877 : */
4878 :
4879 82223 : fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
4880 82223 : fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
4881 82223 : fsp->file_pid = req ? req->smbpid : 0;
4882 82223 : fsp->fsp_flags.can_lock = false;
4883 82223 : fsp->fsp_flags.can_read = false;
4884 82223 : fsp->fsp_flags.can_write = false;
4885 :
4886 82223 : fh_set_private_options(fsp->fh, 0);
4887 : /*
4888 : * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
4889 : */
4890 82223 : fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
4891 82223 : fsp->print_file = NULL;
4892 82223 : fsp->fsp_flags.modified = false;
4893 82223 : fsp->oplock_type = NO_OPLOCK;
4894 82223 : fsp->sent_oplock_break = NO_BREAK_SENT;
4895 82223 : fsp->fsp_flags.is_directory = true;
4896 82223 : if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
4897 1169 : fsp->posix_flags |= FSP_POSIX_FLAGS_ALL;
4898 : }
4899 :
4900 : /* Don't store old timestamps for directory
4901 : handles in the internal database. We don't
4902 : update them in there if new objects
4903 : are created in the directory. Currently
4904 : we only update timestamps on file writes.
4905 : See bug #9870.
4906 : */
4907 82223 : mtimespec = make_omit_timespec();
4908 :
4909 : /*
4910 : * Obviously for FILE_LIST_DIRECTORY we need to reopen to get an fd
4911 : * usable for reading a directory. SMB2_FLUSH may be called on
4912 : * directories opened with FILE_ADD_FILE and FILE_ADD_SUBDIRECTORY so
4913 : * for those we need to reopen as well.
4914 : */
4915 82223 : need_fd_access =
4916 : FILE_LIST_DIRECTORY |
4917 : FILE_ADD_FILE |
4918 : FILE_ADD_SUBDIRECTORY;
4919 :
4920 82223 : if (access_mask & need_fd_access) {
4921 23974 : struct vfs_open_how how = {
4922 : .flags = O_RDONLY | O_DIRECTORY,
4923 : };
4924 227 : bool file_created;
4925 :
4926 23974 : status = reopen_from_fsp(fsp->conn->cwd_fsp,
4927 : fsp->fsp_name,
4928 : fsp,
4929 : &how,
4930 : &file_created);
4931 23974 : if (!NT_STATUS_IS_OK(status)) {
4932 12 : DBG_INFO("Could not open fd for [%s]: %s\n",
4933 : smb_fname_str_dbg(smb_dname),
4934 : nt_errstr(status));
4935 12 : return status;
4936 : }
4937 : }
4938 :
4939 82211 : status = vfs_stat_fsp(fsp);
4940 82211 : if (!NT_STATUS_IS_OK(status)) {
4941 0 : fd_close(fsp);
4942 0 : return status;
4943 : }
4944 :
4945 82211 : if(!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
4946 0 : DEBUG(5,("open_directory: %s is not a directory !\n",
4947 : smb_fname_str_dbg(smb_dname)));
4948 0 : fd_close(fsp);
4949 0 : return NT_STATUS_NOT_A_DIRECTORY;
4950 : }
4951 :
4952 : /* Ensure there was no race condition. We need to check
4953 : * dev/inode but not permissions, as these can change
4954 : * legitimately */
4955 82211 : if (!check_same_dev_ino(&smb_dname->st, &fsp->fsp_name->st)) {
4956 0 : DEBUG(5,("open_directory: stat struct differs for "
4957 : "directory %s.\n",
4958 : smb_fname_str_dbg(smb_dname)));
4959 0 : fd_close(fsp);
4960 0 : return NT_STATUS_ACCESS_DENIED;
4961 : }
4962 :
4963 82211 : if (info == FILE_WAS_OPENED) {
4964 71215 : status = smbd_check_access_rights_fsp(parent_dir_fname->fsp,
4965 : fsp,
4966 : false,
4967 : access_mask);
4968 71215 : if (!NT_STATUS_IS_OK(status)) {
4969 0 : DBG_DEBUG("smbd_check_access_rights_fsp on "
4970 : "file %s failed with %s\n",
4971 : fsp_str_dbg(fsp),
4972 : nt_errstr(status));
4973 0 : fd_close(fsp);
4974 0 : return status;
4975 : }
4976 : }
4977 :
4978 : /*
4979 : * If we created a new directory or going to delete it later,
4980 : * we should keep * the share_mode_lock (g_lock) until we call
4981 : * share_mode_entry_prepare_unlock()
4982 : */
4983 82211 : if (info != FILE_WAS_OPENED) {
4984 10928 : keep_locked = true;
4985 71215 : } else if (create_options & FILE_DELETE_ON_CLOSE) {
4986 2182 : keep_locked = true;
4987 : }
4988 :
4989 82211 : lck_state = (struct open_ntcreate_lock_state) {
4990 : .fsp = fsp,
4991 : .object_type = "directory",
4992 : .req = req,
4993 : .create_disposition = create_disposition,
4994 : .access_mask = access_mask,
4995 : .share_access = share_access,
4996 : .oplock_request = NO_OPLOCK,
4997 : .lease = NULL,
4998 : .first_open_attempt = true,
4999 : .keep_locked = keep_locked,
5000 : };
5001 :
5002 82211 : status = share_mode_entry_prepare_lock_add(&lck_state.prepare_state,
5003 : fsp->file_id,
5004 : conn->connectpath,
5005 : smb_dname,
5006 : &mtimespec,
5007 : open_ntcreate_lock_add_entry,
5008 348 : &lck_state);
5009 82211 : if (!NT_STATUS_IS_OK(status)) {
5010 0 : DBG_ERR("share_mode_entry_prepare_lock_add() failed for %s - %s\n",
5011 : smb_fname_str_dbg(smb_dname), nt_errstr(status));
5012 0 : fd_close(fsp);
5013 0 : return status;
5014 : }
5015 :
5016 82211 : status = lck_state.status;
5017 82211 : if (!NT_STATUS_IS_OK(status)) {
5018 252 : fd_close(fsp);
5019 252 : return status;
5020 : }
5021 :
5022 : /*
5023 : * From here we need to use 'goto unlock;' instead of return !!!
5024 : */
5025 :
5026 : /* For directories the delete on close bit at open time seems
5027 : always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
5028 81959 : if (create_options & FILE_DELETE_ON_CLOSE) {
5029 2091 : status = can_set_delete_on_close(fsp, 0);
5030 2091 : if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
5031 0 : lck_state.cleanup_fn =
5032 : open_ntcreate_lock_cleanup_entry;
5033 0 : goto unlock;
5034 : }
5035 :
5036 2091 : if (NT_STATUS_IS_OK(status)) {
5037 : /* Note that here we set the *initial* delete on close flag,
5038 : not the regular one. The magic gets handled in close. */
5039 2008 : fsp->fsp_flags.initial_delete_on_close = true;
5040 : }
5041 : }
5042 :
5043 : /*
5044 : * Deal with other opens having a modified write time.
5045 : */
5046 81959 : if (!is_omit_timespec(&lck_state.write_time)) {
5047 0 : update_stat_ex_mtime(&fsp->fsp_name->st, lck_state.write_time);
5048 : }
5049 :
5050 81959 : if (pinfo) {
5051 81959 : *pinfo = info;
5052 : }
5053 :
5054 81613 : status = NT_STATUS_OK;
5055 :
5056 81959 : unlock:
5057 81959 : ulstatus = share_mode_entry_prepare_unlock(&lck_state.prepare_state,
5058 : lck_state.cleanup_fn,
5059 346 : &lck_state);
5060 81959 : if (!NT_STATUS_IS_OK(ulstatus)) {
5061 0 : DBG_ERR("share_mode_entry_prepare_unlock() failed for %s - %s\n",
5062 : smb_fname_str_dbg(smb_dname), nt_errstr(ulstatus));
5063 0 : smb_panic("share_mode_entry_prepare_unlock() failed!");
5064 : }
5065 :
5066 81959 : if (!NT_STATUS_IS_OK(status)) {
5067 0 : fd_close(fsp);
5068 0 : return status;
5069 : }
5070 :
5071 81959 : return NT_STATUS_OK;
5072 : }
5073 :
5074 5659 : NTSTATUS create_directory(connection_struct *conn,
5075 : struct smb_request *req,
5076 : struct files_struct *dirfsp,
5077 : struct smb_filename *smb_dname)
5078 : {
5079 51 : NTSTATUS status;
5080 51 : files_struct *fsp;
5081 :
5082 5659 : status = SMB_VFS_CREATE_FILE(
5083 : conn, /* conn */
5084 : req, /* req */
5085 : dirfsp, /* dirfsp */
5086 : smb_dname, /* fname */
5087 : FILE_READ_ATTRIBUTES, /* access_mask */
5088 : FILE_SHARE_NONE, /* share_access */
5089 : FILE_CREATE, /* create_disposition*/
5090 : FILE_DIRECTORY_FILE, /* create_options */
5091 : FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
5092 : 0, /* oplock_request */
5093 : NULL, /* lease */
5094 : 0, /* allocation_size */
5095 : 0, /* private_flags */
5096 : NULL, /* sd */
5097 : NULL, /* ea_list */
5098 : &fsp, /* result */
5099 : NULL, /* pinfo */
5100 : NULL, NULL); /* create context */
5101 :
5102 5659 : if (NT_STATUS_IS_OK(status)) {
5103 5637 : close_file_free(req, &fsp, NORMAL_CLOSE);
5104 : }
5105 :
5106 5659 : return status;
5107 : }
5108 :
5109 : /****************************************************************************
5110 : Receive notification that one of our open files has been renamed by another
5111 : smbd process.
5112 : ****************************************************************************/
5113 :
5114 24 : void msg_file_was_renamed(struct messaging_context *msg_ctx,
5115 : void *private_data,
5116 : uint32_t msg_type,
5117 : struct server_id src,
5118 : DATA_BLOB *data)
5119 : {
5120 24 : struct file_rename_message *msg = NULL;
5121 2 : enum ndr_err_code ndr_err;
5122 2 : files_struct *fsp;
5123 24 : struct smb_filename *smb_fname = NULL;
5124 2 : struct smbd_server_connection *sconn =
5125 24 : talloc_get_type_abort(private_data,
5126 : struct smbd_server_connection);
5127 :
5128 24 : msg = talloc(talloc_tos(), struct file_rename_message);
5129 24 : if (msg == NULL) {
5130 0 : DBG_WARNING("talloc failed\n");
5131 0 : return;
5132 : }
5133 :
5134 24 : ndr_err = ndr_pull_struct_blob_all(
5135 : data,
5136 : msg,
5137 : msg,
5138 : (ndr_pull_flags_fn_t)ndr_pull_file_rename_message);
5139 24 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
5140 0 : DBG_DEBUG("ndr_pull_oplock_break_message failed: %s\n",
5141 : ndr_errstr(ndr_err));
5142 0 : goto out;
5143 : }
5144 24 : if (DEBUGLEVEL >= 10) {
5145 0 : struct server_id_buf buf;
5146 0 : DBG_DEBUG("Got rename message from %s\n",
5147 : server_id_str_buf(src, &buf));
5148 0 : NDR_PRINT_DEBUG(file_rename_message, msg);
5149 : }
5150 :
5151 : /* stream_name must always be NULL if there is no stream. */
5152 24 : if ((msg->stream_name != NULL) && (msg->stream_name[0] == '\0')) {
5153 0 : msg->stream_name = NULL;
5154 : }
5155 :
5156 24 : smb_fname = synthetic_smb_fname(msg,
5157 : msg->base_name,
5158 : msg->stream_name,
5159 : NULL,
5160 : 0,
5161 : 0);
5162 24 : if (smb_fname == NULL) {
5163 0 : DBG_DEBUG("synthetic_smb_fname failed\n");
5164 0 : goto out;
5165 : }
5166 :
5167 24 : fsp = file_find_dif(sconn, msg->id, msg->share_file_id);
5168 24 : if (fsp == NULL) {
5169 0 : DBG_DEBUG("fsp not found\n");
5170 0 : goto out;
5171 : }
5172 :
5173 24 : if (strcmp(fsp->conn->connectpath, msg->servicepath) == 0) {
5174 2 : SMB_STRUCT_STAT fsp_orig_sbuf;
5175 2 : NTSTATUS status;
5176 24 : DBG_DEBUG("renaming file %s from %s -> %s\n",
5177 : fsp_fnum_dbg(fsp),
5178 : fsp_str_dbg(fsp),
5179 : smb_fname_str_dbg(smb_fname));
5180 :
5181 : /*
5182 : * The incoming smb_fname here has an
5183 : * invalid stat struct from synthetic_smb_fname()
5184 : * above.
5185 : * Preserve the existing stat from the
5186 : * open fsp after fsp_set_smb_fname()
5187 : * overwrites with the invalid stat.
5188 : *
5189 : * (We could just copy this into
5190 : * smb_fname->st, but keep this code
5191 : * identical to the fix in rename_open_files()
5192 : * for clarity.
5193 : *
5194 : * We will do an fstat before returning
5195 : * any of this metadata to the client anyway.
5196 : */
5197 24 : fsp_orig_sbuf = fsp->fsp_name->st;
5198 24 : status = fsp_set_smb_fname(fsp, smb_fname);
5199 24 : if (!NT_STATUS_IS_OK(status)) {
5200 0 : DBG_DEBUG("fsp_set_smb_fname failed: %s\n",
5201 : nt_errstr(status));
5202 : }
5203 24 : fsp->fsp_name->st = fsp_orig_sbuf;
5204 : } else {
5205 : /* TODO. JRA. */
5206 : /*
5207 : * Now we have the complete path we can work out if
5208 : * this is actually within this share and adjust
5209 : * newname accordingly.
5210 : */
5211 0 : DBG_DEBUG("share mismatch (sharepath %s not sharepath %s) "
5212 : "%s from %s -> %s\n",
5213 : fsp->conn->connectpath,
5214 : msg->servicepath,
5215 : fsp_fnum_dbg(fsp),
5216 : fsp_str_dbg(fsp),
5217 : smb_fname_str_dbg(smb_fname));
5218 : }
5219 24 : out:
5220 24 : TALLOC_FREE(msg);
5221 : }
5222 :
5223 : /*
5224 : * If a main file is opened for delete, all streams need to be checked for
5225 : * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
5226 : * If that works, delete them all by setting the delete on close and close.
5227 : */
5228 :
5229 366208 : static NTSTATUS open_streams_for_delete(connection_struct *conn,
5230 : const struct smb_filename *smb_fname)
5231 : {
5232 366208 : struct stream_struct *stream_info = NULL;
5233 366208 : files_struct **streams = NULL;
5234 753 : int j;
5235 366208 : unsigned int i, num_streams = 0;
5236 366208 : TALLOC_CTX *frame = talloc_stackframe();
5237 366208 : const struct smb_filename *pathref = NULL;
5238 753 : NTSTATUS status;
5239 :
5240 366208 : if (smb_fname->fsp == NULL) {
5241 207022 : struct smb_filename *tmp = NULL;
5242 207394 : status = synthetic_pathref(frame,
5243 : conn->cwd_fsp,
5244 207022 : smb_fname->base_name,
5245 : NULL,
5246 : NULL,
5247 207022 : smb_fname->twrp,
5248 207022 : smb_fname->flags,
5249 : &tmp);
5250 207022 : if (!NT_STATUS_IS_OK(status)) {
5251 207014 : if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
5252 207014 : || NT_STATUS_EQUAL(status,
5253 : NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
5254 207012 : DBG_DEBUG("no streams around\n");
5255 207012 : TALLOC_FREE(frame);
5256 207012 : return NT_STATUS_OK;
5257 : }
5258 2 : DBG_DEBUG("synthetic_pathref failed: %s\n",
5259 : nt_errstr(status));
5260 2 : goto fail;
5261 : }
5262 8 : pathref = tmp;
5263 : } else {
5264 158805 : pathref = smb_fname;
5265 : }
5266 159194 : status = vfs_fstreaminfo(pathref->fsp, talloc_tos(),
5267 : &num_streams, &stream_info);
5268 :
5269 159194 : if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
5270 159194 : || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
5271 0 : DEBUG(10, ("no streams around\n"));
5272 0 : TALLOC_FREE(frame);
5273 0 : return NT_STATUS_OK;
5274 : }
5275 :
5276 159194 : if (!NT_STATUS_IS_OK(status)) {
5277 0 : DEBUG(10, ("vfs_fstreaminfo failed: %s\n",
5278 : nt_errstr(status)));
5279 0 : goto fail;
5280 : }
5281 :
5282 159194 : DEBUG(10, ("open_streams_for_delete found %d streams\n",
5283 : num_streams));
5284 :
5285 159194 : if (num_streams == 0) {
5286 14365 : TALLOC_FREE(frame);
5287 14365 : return NT_STATUS_OK;
5288 : }
5289 :
5290 144829 : streams = talloc_array(talloc_tos(), files_struct *, num_streams);
5291 144829 : if (streams == NULL) {
5292 0 : DEBUG(0, ("talloc failed\n"));
5293 0 : status = NT_STATUS_NO_MEMORY;
5294 0 : goto fail;
5295 : }
5296 :
5297 290332 : for (i=0; i<num_streams; i++) {
5298 258 : struct smb_filename *smb_fname_cp;
5299 :
5300 145561 : if (strequal(stream_info[i].name, "::$DATA")) {
5301 144681 : streams[i] = NULL;
5302 144681 : continue;
5303 : }
5304 :
5305 880 : smb_fname_cp = synthetic_smb_fname(talloc_tos(),
5306 880 : smb_fname->base_name,
5307 880 : stream_info[i].name,
5308 : NULL,
5309 880 : smb_fname->twrp,
5310 880 : (smb_fname->flags &
5311 : ~SMB_FILENAME_POSIX_PATH));
5312 880 : if (smb_fname_cp == NULL) {
5313 0 : status = NT_STATUS_NO_MEMORY;
5314 0 : goto fail;
5315 : }
5316 :
5317 880 : status = openat_pathref_fsp(conn->cwd_fsp, smb_fname_cp);
5318 880 : if (!NT_STATUS_IS_OK(status)) {
5319 0 : DBG_DEBUG("Unable to open stream [%s]: %s\n",
5320 : smb_fname_str_dbg(smb_fname_cp),
5321 : nt_errstr(status));
5322 0 : TALLOC_FREE(smb_fname_cp);
5323 0 : break;
5324 : }
5325 :
5326 880 : status = SMB_VFS_CREATE_FILE(
5327 : conn, /* conn */
5328 : NULL, /* req */
5329 : NULL, /* dirfsp */
5330 : smb_fname_cp, /* fname */
5331 : DELETE_ACCESS, /* access_mask */
5332 : (FILE_SHARE_READ | /* share_access */
5333 : FILE_SHARE_WRITE | FILE_SHARE_DELETE),
5334 : FILE_OPEN, /* create_disposition*/
5335 : 0, /* create_options */
5336 : FILE_ATTRIBUTE_NORMAL, /* file_attributes */
5337 : 0, /* oplock_request */
5338 : NULL, /* lease */
5339 : 0, /* allocation_size */
5340 : 0, /* private_flags */
5341 : NULL, /* sd */
5342 : NULL, /* ea_list */
5343 : &streams[i], /* result */
5344 : NULL, /* pinfo */
5345 : NULL, NULL); /* create context */
5346 :
5347 880 : if (!NT_STATUS_IS_OK(status)) {
5348 58 : DEBUG(10, ("Could not open stream %s: %s\n",
5349 : smb_fname_str_dbg(smb_fname_cp),
5350 : nt_errstr(status)));
5351 :
5352 58 : TALLOC_FREE(smb_fname_cp);
5353 58 : break;
5354 : }
5355 1078 : TALLOC_FREE(smb_fname_cp);
5356 : }
5357 :
5358 : /*
5359 : * don't touch the variable "status" beyond this point :-)
5360 : */
5361 :
5362 290332 : for (j = i-1 ; j >= 0; j--) {
5363 145503 : if (streams[j] == NULL) {
5364 144681 : continue;
5365 : }
5366 :
5367 822 : DEBUG(10, ("Closing stream # %d, %s\n", j,
5368 : fsp_str_dbg(streams[j])));
5369 822 : close_file_free(NULL, &streams[j], NORMAL_CLOSE);
5370 : }
5371 :
5372 144829 : fail:
5373 144831 : TALLOC_FREE(frame);
5374 144831 : return status;
5375 : }
5376 :
5377 : /*********************************************************************
5378 : Create a default ACL by inheriting from the parent. If no inheritance
5379 : from the parent available, don't set anything. This will leave the actual
5380 : permissions the new file or directory already got from the filesystem
5381 : as the NT ACL when read.
5382 : *********************************************************************/
5383 :
5384 147684 : static NTSTATUS inherit_new_acl(files_struct *dirfsp, files_struct *fsp)
5385 : {
5386 147684 : TALLOC_CTX *frame = talloc_stackframe();
5387 147684 : struct security_descriptor *parent_desc = NULL;
5388 147684 : NTSTATUS status = NT_STATUS_OK;
5389 147684 : struct security_descriptor *psd = NULL;
5390 147684 : const struct dom_sid *owner_sid = NULL;
5391 147684 : const struct dom_sid *group_sid = NULL;
5392 147684 : uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
5393 147684 : struct security_token *token = fsp->conn->session_info->security_token;
5394 148029 : bool inherit_owner =
5395 147684 : (lp_inherit_owner(SNUM(fsp->conn)) == INHERIT_OWNER_WINDOWS_AND_UNIX);
5396 147684 : bool inheritable_components = false;
5397 147684 : bool try_builtin_administrators = false;
5398 147684 : const struct dom_sid *BA_U_sid = NULL;
5399 147684 : const struct dom_sid *BA_G_sid = NULL;
5400 147684 : bool try_system = false;
5401 147684 : const struct dom_sid *SY_U_sid = NULL;
5402 147684 : const struct dom_sid *SY_G_sid = NULL;
5403 147684 : size_t size = 0;
5404 345 : bool ok;
5405 :
5406 147684 : status = SMB_VFS_FGET_NT_ACL(dirfsp,
5407 : (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
5408 : frame,
5409 : &parent_desc);
5410 147684 : if (!NT_STATUS_IS_OK(status)) {
5411 0 : TALLOC_FREE(frame);
5412 0 : return status;
5413 : }
5414 :
5415 148029 : inheritable_components = sd_has_inheritable_components(parent_desc,
5416 147684 : fsp->fsp_flags.is_directory);
5417 :
5418 147684 : if (!inheritable_components && !inherit_owner) {
5419 692 : TALLOC_FREE(frame);
5420 : /* Nothing to inherit and not setting owner. */
5421 692 : return NT_STATUS_OK;
5422 : }
5423 :
5424 : /* Create an inherited descriptor from the parent. */
5425 :
5426 146992 : if (DEBUGLEVEL >= 10) {
5427 0 : DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
5428 : fsp_str_dbg(fsp) ));
5429 0 : NDR_PRINT_DEBUG(security_descriptor, parent_desc);
5430 : }
5431 :
5432 : /* Inherit from parent descriptor if "inherit owner" set. */
5433 146992 : if (inherit_owner) {
5434 8 : owner_sid = parent_desc->owner_sid;
5435 8 : group_sid = parent_desc->group_sid;
5436 : }
5437 :
5438 146647 : if (owner_sid == NULL) {
5439 146984 : if (security_token_has_builtin_administrators(token)) {
5440 57773 : try_builtin_administrators = true;
5441 88866 : } else if (security_token_is_system(token)) {
5442 0 : try_builtin_administrators = true;
5443 0 : try_system = true;
5444 : }
5445 : }
5446 :
5447 146992 : if (group_sid == NULL &&
5448 146984 : token->num_sids == PRIMARY_GROUP_SID_INDEX)
5449 : {
5450 0 : if (security_token_is_system(token)) {
5451 0 : try_builtin_administrators = true;
5452 0 : try_system = true;
5453 : }
5454 : }
5455 :
5456 146992 : if (try_builtin_administrators) {
5457 58118 : struct unixid ids = { .id = 0 };
5458 :
5459 58118 : ok = sids_to_unixids(&global_sid_Builtin_Administrators, 1, &ids);
5460 58118 : if (ok) {
5461 58118 : switch (ids.type) {
5462 57969 : case ID_TYPE_BOTH:
5463 57969 : BA_U_sid = &global_sid_Builtin_Administrators;
5464 57969 : BA_G_sid = &global_sid_Builtin_Administrators;
5465 57969 : break;
5466 0 : case ID_TYPE_UID:
5467 0 : BA_U_sid = &global_sid_Builtin_Administrators;
5468 0 : break;
5469 149 : case ID_TYPE_GID:
5470 149 : BA_G_sid = &global_sid_Builtin_Administrators;
5471 149 : break;
5472 0 : default:
5473 0 : break;
5474 : }
5475 : }
5476 : }
5477 :
5478 146992 : if (try_system) {
5479 0 : struct unixid ids = { .id = 0 };
5480 :
5481 0 : ok = sids_to_unixids(&global_sid_System, 1, &ids);
5482 0 : if (ok) {
5483 0 : switch (ids.type) {
5484 0 : case ID_TYPE_BOTH:
5485 0 : SY_U_sid = &global_sid_System;
5486 0 : SY_G_sid = &global_sid_System;
5487 0 : break;
5488 0 : case ID_TYPE_UID:
5489 0 : SY_U_sid = &global_sid_System;
5490 0 : break;
5491 0 : case ID_TYPE_GID:
5492 0 : SY_G_sid = &global_sid_System;
5493 0 : break;
5494 0 : default:
5495 0 : break;
5496 : }
5497 : }
5498 : }
5499 :
5500 146992 : if (owner_sid == NULL) {
5501 146984 : owner_sid = BA_U_sid;
5502 : }
5503 :
5504 146992 : if (owner_sid == NULL) {
5505 89015 : owner_sid = SY_U_sid;
5506 : }
5507 :
5508 146992 : if (group_sid == NULL) {
5509 146984 : group_sid = SY_G_sid;
5510 : }
5511 :
5512 146992 : if (try_system && group_sid == NULL) {
5513 0 : group_sid = BA_G_sid;
5514 : }
5515 :
5516 146992 : if (owner_sid == NULL) {
5517 89015 : owner_sid = &token->sids[PRIMARY_USER_SID_INDEX];
5518 : }
5519 146992 : if (group_sid == NULL) {
5520 146984 : if (token->num_sids == PRIMARY_GROUP_SID_INDEX) {
5521 0 : group_sid = &token->sids[PRIMARY_USER_SID_INDEX];
5522 : } else {
5523 146984 : group_sid = &token->sids[PRIMARY_GROUP_SID_INDEX];
5524 : }
5525 : }
5526 :
5527 147337 : status = se_create_child_secdesc(frame,
5528 : &psd,
5529 : &size,
5530 : parent_desc,
5531 : owner_sid,
5532 : group_sid,
5533 146992 : fsp->fsp_flags.is_directory);
5534 146992 : if (!NT_STATUS_IS_OK(status)) {
5535 0 : TALLOC_FREE(frame);
5536 0 : return status;
5537 : }
5538 :
5539 : /* If inheritable_components == false,
5540 : se_create_child_secdesc()
5541 : creates a security descriptor with a NULL dacl
5542 : entry, but with SEC_DESC_DACL_PRESENT. We need
5543 : to remove that flag. */
5544 :
5545 146992 : if (!inheritable_components) {
5546 0 : security_info_sent &= ~SECINFO_DACL;
5547 0 : psd->type &= ~SEC_DESC_DACL_PRESENT;
5548 : }
5549 :
5550 146992 : if (DEBUGLEVEL >= 10) {
5551 0 : DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
5552 : fsp_str_dbg(fsp) ));
5553 0 : NDR_PRINT_DEBUG(security_descriptor, psd);
5554 : }
5555 :
5556 146992 : if (inherit_owner) {
5557 : /* We need to be root to force this. */
5558 8 : become_root();
5559 : }
5560 146992 : status = SMB_VFS_FSET_NT_ACL(metadata_fsp(fsp),
5561 : security_info_sent,
5562 : psd);
5563 146992 : if (inherit_owner) {
5564 8 : unbecome_root();
5565 : }
5566 146992 : TALLOC_FREE(frame);
5567 146992 : return status;
5568 : }
5569 :
5570 : /*
5571 : * If we already have a lease, it must match the new file id. [MS-SMB2]
5572 : * 3.3.5.9.8 speaks about INVALID_PARAMETER if an already used lease key is
5573 : * used for a different file name.
5574 : */
5575 :
5576 : struct lease_match_state {
5577 : /* Input parameters. */
5578 : TALLOC_CTX *mem_ctx;
5579 : const char *servicepath;
5580 : const struct smb_filename *fname;
5581 : bool file_existed;
5582 : struct file_id id;
5583 : /* Return parameters. */
5584 : uint32_t num_file_ids;
5585 : struct file_id *ids;
5586 : NTSTATUS match_status;
5587 : };
5588 :
5589 : /*************************************************************
5590 : File doesn't exist but this lease key+guid is already in use.
5591 :
5592 : This is only allowable in the dynamic share case where the
5593 : service path must be different.
5594 :
5595 : There is a small race condition here in the multi-connection
5596 : case where a client sends two create calls on different connections,
5597 : where the file doesn't exist and one smbd creates the leases_db
5598 : entry first, but this will get fixed by the multichannel cleanup
5599 : when all identical client_guids get handled by a single smbd.
5600 : **************************************************************/
5601 :
5602 6 : static void lease_match_parser_new_file(
5603 : uint32_t num_files,
5604 : const struct leases_db_file *files,
5605 : struct lease_match_state *state)
5606 : {
5607 0 : uint32_t i;
5608 :
5609 8 : for (i = 0; i < num_files; i++) {
5610 6 : const struct leases_db_file *f = &files[i];
5611 6 : if (strequal(state->servicepath, f->servicepath)) {
5612 4 : state->match_status = NT_STATUS_INVALID_PARAMETER;
5613 4 : return;
5614 : }
5615 : }
5616 :
5617 : /* Dynamic share case. Break leases on all other files. */
5618 2 : state->match_status = leases_db_copy_file_ids(state->mem_ctx,
5619 : num_files,
5620 : files,
5621 : &state->ids);
5622 2 : if (!NT_STATUS_IS_OK(state->match_status)) {
5623 0 : return;
5624 : }
5625 :
5626 2 : state->num_file_ids = num_files;
5627 2 : state->match_status = NT_STATUS_OPLOCK_NOT_GRANTED;
5628 2 : return;
5629 : }
5630 :
5631 220 : static void lease_match_parser(
5632 : uint32_t num_files,
5633 : const struct leases_db_file *files,
5634 : void *private_data)
5635 : {
5636 220 : struct lease_match_state *state =
5637 : (struct lease_match_state *)private_data;
5638 0 : uint32_t i;
5639 :
5640 220 : if (!state->file_existed) {
5641 : /*
5642 : * Deal with name mismatch or
5643 : * possible dynamic share case separately
5644 : * to make code clearer.
5645 : */
5646 6 : lease_match_parser_new_file(num_files,
5647 : files,
5648 : state);
5649 6 : return;
5650 : }
5651 :
5652 : /* File existed. */
5653 214 : state->match_status = NT_STATUS_OK;
5654 :
5655 424 : for (i = 0; i < num_files; i++) {
5656 216 : const struct leases_db_file *f = &files[i];
5657 :
5658 : /* Everything should be the same. */
5659 216 : if (!file_id_equal(&state->id, &f->id)) {
5660 : /*
5661 : * The client asked for a lease on a
5662 : * file that doesn't match the file_id
5663 : * in the database.
5664 : *
5665 : * Maybe this is a dynamic share, i.e.
5666 : * a share where the servicepath is
5667 : * different for different users (e.g.
5668 : * the [HOMES] share.
5669 : *
5670 : * If the servicepath is different, but the requested
5671 : * file name + stream name is the same then this is
5672 : * a dynamic share, the client is using the same share
5673 : * name and doesn't know that the underlying servicepath
5674 : * is different. It was expecting a lease on the
5675 : * same file. Return NT_STATUS_OPLOCK_NOT_GRANTED
5676 : * to break leases
5677 : *
5678 : * Otherwise the client has messed up, or is
5679 : * testing our error codes, so return
5680 : * NT_STATUS_INVALID_PARAMETER.
5681 : */
5682 10 : if (!strequal(f->servicepath, state->servicepath) &&
5683 8 : strequal(f->base_name, state->fname->base_name) &&
5684 4 : strequal(f->stream_name, state->fname->stream_name))
5685 4 : {
5686 : /*
5687 : * Name is the same but servicepath is
5688 : * different, dynamic share. Break leases.
5689 : */
5690 4 : state->match_status =
5691 : NT_STATUS_OPLOCK_NOT_GRANTED;
5692 : } else {
5693 2 : state->match_status =
5694 : NT_STATUS_INVALID_PARAMETER;
5695 : }
5696 6 : break;
5697 : }
5698 210 : if (!strequal(f->servicepath, state->servicepath)) {
5699 0 : state->match_status = NT_STATUS_INVALID_PARAMETER;
5700 0 : break;
5701 : }
5702 210 : if (!strequal(f->base_name, state->fname->base_name)) {
5703 0 : state->match_status = NT_STATUS_INVALID_PARAMETER;
5704 0 : break;
5705 : }
5706 210 : if (!strequal(f->stream_name, state->fname->stream_name)) {
5707 0 : state->match_status = NT_STATUS_INVALID_PARAMETER;
5708 0 : break;
5709 : }
5710 : }
5711 :
5712 214 : if (NT_STATUS_IS_OK(state->match_status)) {
5713 : /*
5714 : * Common case - just opening another handle on a
5715 : * file on a non-dynamic share.
5716 : */
5717 208 : return;
5718 : }
5719 :
5720 6 : if (NT_STATUS_EQUAL(state->match_status, NT_STATUS_INVALID_PARAMETER)) {
5721 : /* Mismatched path. Error back to client. */
5722 2 : return;
5723 : }
5724 :
5725 : /*
5726 : * File id mismatch. Dynamic share case NT_STATUS_OPLOCK_NOT_GRANTED.
5727 : * Don't allow leases.
5728 : */
5729 :
5730 4 : state->match_status = leases_db_copy_file_ids(state->mem_ctx,
5731 : num_files,
5732 : files,
5733 : &state->ids);
5734 4 : if (!NT_STATUS_IS_OK(state->match_status)) {
5735 0 : return;
5736 : }
5737 :
5738 4 : state->num_file_ids = num_files;
5739 4 : state->match_status = NT_STATUS_OPLOCK_NOT_GRANTED;
5740 4 : return;
5741 : }
5742 :
5743 : struct lease_match_break_state {
5744 : struct messaging_context *msg_ctx;
5745 : const struct smb2_lease_key *lease_key;
5746 : struct file_id id;
5747 :
5748 : bool found_lease;
5749 : uint16_t version;
5750 : uint16_t epoch;
5751 : };
5752 :
5753 6 : static bool lease_match_break_fn(
5754 : struct share_mode_entry *e,
5755 : void *private_data)
5756 : {
5757 6 : struct lease_match_break_state *state = private_data;
5758 0 : bool stale, equal;
5759 6 : uint32_t e_lease_type = SMB2_LEASE_NONE;
5760 0 : NTSTATUS status;
5761 :
5762 6 : stale = share_entry_stale_pid(e);
5763 6 : if (stale) {
5764 0 : return false;
5765 : }
5766 :
5767 6 : equal = smb2_lease_key_equal(&e->lease_key, state->lease_key);
5768 6 : if (!equal) {
5769 0 : return false;
5770 : }
5771 :
5772 6 : status = leases_db_get(
5773 6 : &e->client_guid,
5774 6 : &e->lease_key,
5775 6 : &state->id,
5776 : &e_lease_type, /* current_state */
5777 : NULL, /* breaking */
5778 : NULL, /* breaking_to_requested */
5779 : NULL, /* breaking_to_required */
5780 : &state->version, /* lease_version */
5781 : &state->epoch); /* epoch */
5782 6 : if (NT_STATUS_IS_OK(status)) {
5783 6 : state->found_lease = true;
5784 : } else {
5785 0 : DBG_WARNING("Could not find version/epoch: %s\n",
5786 : nt_errstr(status));
5787 0 : return false;
5788 : }
5789 :
5790 6 : if (e_lease_type == SMB2_LEASE_NONE) {
5791 4 : return false;
5792 : }
5793 2 : send_break_message(state->msg_ctx, &state->id, e, SMB2_LEASE_NONE);
5794 :
5795 : /*
5796 : * Windows 7 and 8 lease clients are broken in that they will
5797 : * not respond to lease break requests whilst waiting for an
5798 : * outstanding open request on that lease handle on the same
5799 : * TCP connection, due to holding an internal inode lock.
5800 : *
5801 : * This means we can't reschedule ourselves here, but must
5802 : * return from the create.
5803 : *
5804 : * Work around:
5805 : *
5806 : * Send the breaks and then return SMB2_LEASE_NONE in the
5807 : * lease handle to cause them to acknowledge the lease
5808 : * break. Consultation with Microsoft engineering confirmed
5809 : * this approach is safe.
5810 : */
5811 :
5812 2 : return false;
5813 : }
5814 :
5815 6 : static void lease_match_fid_fn(struct share_mode_lock *lck,
5816 : void *private_data)
5817 : {
5818 0 : bool ok;
5819 :
5820 6 : ok = share_mode_forall_leases(lck, lease_match_break_fn, private_data);
5821 6 : if (!ok) {
5822 0 : DBG_DEBUG("share_mode_forall_leases failed\n");
5823 : }
5824 6 : }
5825 :
5826 1084 : static NTSTATUS lease_match(connection_struct *conn,
5827 : struct smb_request *req,
5828 : const struct smb2_lease_key *lease_key,
5829 : const char *servicepath,
5830 : const struct smb_filename *fname,
5831 : uint16_t *p_version,
5832 : uint16_t *p_epoch)
5833 : {
5834 1084 : struct smbd_server_connection *sconn = req->sconn;
5835 1084 : TALLOC_CTX *tos = talloc_tos();
5836 1084 : struct lease_match_state state = {
5837 : .mem_ctx = tos,
5838 : .servicepath = servicepath,
5839 : .fname = fname,
5840 : .match_status = NT_STATUS_OK
5841 : };
5842 0 : uint32_t i;
5843 0 : NTSTATUS status;
5844 :
5845 1084 : state.file_existed = VALID_STAT(fname->st);
5846 1084 : if (state.file_existed) {
5847 524 : state.id = vfs_file_id_from_sbuf(conn, &fname->st);
5848 : }
5849 :
5850 1084 : status = leases_db_parse(&sconn->client->global->client_guid,
5851 : lease_key, lease_match_parser, &state);
5852 1084 : if (!NT_STATUS_IS_OK(status)) {
5853 : /*
5854 : * Not found or error means okay: We can make the lease pass
5855 : */
5856 864 : return NT_STATUS_OK;
5857 : }
5858 220 : if (!NT_STATUS_EQUAL(state.match_status, NT_STATUS_OPLOCK_NOT_GRANTED)) {
5859 : /*
5860 : * Anything but NT_STATUS_OPLOCK_NOT_GRANTED, let the caller
5861 : * deal with it.
5862 : */
5863 214 : return state.match_status;
5864 : }
5865 :
5866 : /* We have to break all existing leases. */
5867 16 : for (i = 0; i < state.num_file_ids; i++) {
5868 10 : struct lease_match_break_state break_state = {
5869 10 : .msg_ctx = conn->sconn->msg_ctx,
5870 : .lease_key = lease_key,
5871 : };
5872 :
5873 10 : if (file_id_equal(&state.ids[i], &state.id)) {
5874 : /* Don't need to break our own file. */
5875 4 : continue;
5876 : }
5877 :
5878 6 : break_state.id = state.ids[i];
5879 :
5880 6 : status = share_mode_do_locked_vfs_denied(break_state.id,
5881 : lease_match_fid_fn,
5882 : &break_state);
5883 6 : if (!NT_STATUS_IS_OK(status)) {
5884 : /* Race condition - file already closed. */
5885 0 : continue;
5886 : }
5887 :
5888 6 : if (break_state.found_lease) {
5889 6 : *p_version = break_state.version;
5890 6 : *p_epoch = break_state.epoch;
5891 : }
5892 : }
5893 : /*
5894 : * Ensure we don't grant anything more so we
5895 : * never upgrade.
5896 : */
5897 6 : return NT_STATUS_OPLOCK_NOT_GRANTED;
5898 : }
5899 :
5900 : /*
5901 : * Wrapper around open_file_ntcreate and open_directory
5902 : */
5903 :
5904 556242 : static NTSTATUS create_file_unixpath(connection_struct *conn,
5905 : struct smb_request *req,
5906 : struct files_struct *dirfsp,
5907 : struct smb_filename *smb_fname,
5908 : uint32_t access_mask,
5909 : uint32_t share_access,
5910 : uint32_t create_disposition,
5911 : uint32_t create_options,
5912 : uint32_t file_attributes,
5913 : uint32_t oplock_request,
5914 : const struct smb2_lease *lease,
5915 : uint64_t allocation_size,
5916 : uint32_t private_flags,
5917 : struct security_descriptor *sd,
5918 : struct ea_list *ea_list,
5919 :
5920 : files_struct **result,
5921 : int *pinfo)
5922 : {
5923 1528 : struct smb2_lease none_lease;
5924 556242 : int info = FILE_WAS_OPENED;
5925 556242 : files_struct *base_fsp = NULL;
5926 556242 : files_struct *fsp = NULL;
5927 556242 : bool free_fsp_on_error = false;
5928 1528 : NTSTATUS status;
5929 1528 : int ret;
5930 556242 : struct smb_filename *parent_dir_fname = NULL;
5931 556242 : struct smb_filename *smb_fname_atname = NULL;
5932 :
5933 556242 : DBG_DEBUG("access_mask = 0x%"PRIx32" "
5934 : "file_attributes = 0x%"PRIx32" "
5935 : "share_access = 0x%"PRIx32" "
5936 : "create_disposition = 0x%"PRIx32" "
5937 : "create_options = 0x%"PRIx32" "
5938 : "oplock_request = 0x%"PRIx32" "
5939 : "private_flags = 0x%"PRIx32" "
5940 : "ea_list = %p, "
5941 : "sd = %p, "
5942 : "fname = %s\n",
5943 : access_mask,
5944 : file_attributes,
5945 : share_access,
5946 : create_disposition,
5947 : create_options,
5948 : oplock_request,
5949 : private_flags,
5950 : ea_list,
5951 : sd,
5952 : smb_fname_str_dbg(smb_fname));
5953 :
5954 556242 : if (create_options & FILE_OPEN_BY_FILE_ID) {
5955 5 : status = NT_STATUS_NOT_SUPPORTED;
5956 5 : goto fail;
5957 : }
5958 :
5959 556237 : if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
5960 60 : status = NT_STATUS_INVALID_PARAMETER;
5961 60 : goto fail;
5962 : }
5963 :
5964 556177 : if (!(create_options & FILE_OPEN_REPARSE_POINT) &&
5965 497795 : (smb_fname->fsp != NULL) && /* new files don't have an fsp */
5966 232416 : VALID_STAT(smb_fname->fsp->fsp_name->st))
5967 : {
5968 232416 : mode_t type = (smb_fname->fsp->fsp_name->st.st_ex_mode &
5969 : S_IFMT);
5970 :
5971 232416 : switch (type) {
5972 231622 : case S_IFREG:
5973 : FALL_THROUGH;
5974 : case S_IFDIR:
5975 231622 : break;
5976 130 : case S_IFLNK:
5977 : /*
5978 : * We should never get this far with a symlink
5979 : * "as such". Report as not existing.
5980 : */
5981 130 : status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5982 130 : goto fail;
5983 0 : default:
5984 0 : status = NT_STATUS_IO_REPARSE_TAG_NOT_HANDLED;
5985 0 : goto fail;
5986 : }
5987 : }
5988 :
5989 556047 : if (req == NULL) {
5990 8403 : oplock_request |= INTERNAL_OPEN_ONLY;
5991 : }
5992 :
5993 556047 : if (lease != NULL) {
5994 1084 : uint16_t epoch = lease->lease_epoch;
5995 1084 : uint16_t version = lease->lease_version;
5996 :
5997 1084 : if (req == NULL) {
5998 0 : DBG_WARNING("Got lease on internal open\n");
5999 0 : status = NT_STATUS_INTERNAL_ERROR;
6000 0 : goto fail;
6001 : }
6002 :
6003 1084 : status = lease_match(conn,
6004 : req,
6005 : &lease->lease_key,
6006 1084 : conn->connectpath,
6007 : smb_fname,
6008 : &version,
6009 : &epoch);
6010 1084 : if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_NOT_GRANTED)) {
6011 : /* Dynamic share file. No leases and update epoch... */
6012 6 : none_lease = *lease;
6013 6 : none_lease.lease_state = SMB2_LEASE_NONE;
6014 6 : none_lease.lease_epoch = epoch;
6015 6 : none_lease.lease_version = version;
6016 6 : lease = &none_lease;
6017 1078 : } else if (!NT_STATUS_IS_OK(status)) {
6018 6 : goto fail;
6019 : }
6020 : }
6021 :
6022 556041 : if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
6023 502834 : && (access_mask & DELETE_ACCESS)
6024 367962 : && !is_named_stream(smb_fname)) {
6025 : /*
6026 : * We can't open a file with DELETE access if any of the
6027 : * streams is open without FILE_SHARE_DELETE
6028 : */
6029 366208 : status = open_streams_for_delete(conn, smb_fname);
6030 :
6031 366208 : if (!NT_STATUS_IS_OK(status)) {
6032 60 : goto fail;
6033 : }
6034 : }
6035 :
6036 555981 : if (access_mask & SEC_FLAG_SYSTEM_SECURITY) {
6037 0 : bool ok;
6038 :
6039 781 : ok = security_token_has_privilege(get_current_nttok(conn),
6040 : SEC_PRIV_SECURITY);
6041 781 : if (!ok) {
6042 0 : DBG_DEBUG("open on %s failed - "
6043 : "SEC_FLAG_SYSTEM_SECURITY denied.\n",
6044 : smb_fname_str_dbg(smb_fname));
6045 0 : status = NT_STATUS_PRIVILEGE_NOT_HELD;
6046 0 : goto fail;
6047 : }
6048 :
6049 781 : if (conn->sconn->using_smb2 &&
6050 : (access_mask == SEC_FLAG_SYSTEM_SECURITY))
6051 : {
6052 : /*
6053 : * No other bits set. Windows SMB2 refuses this.
6054 : * See smbtorture3 SMB2-SACL test.
6055 : *
6056 : * Note this is an SMB2-only behavior,
6057 : * smbtorture3 SMB1-SYSTEM-SECURITY already tests
6058 : * that SMB1 allows this.
6059 : */
6060 2 : status = NT_STATUS_ACCESS_DENIED;
6061 2 : goto fail;
6062 : }
6063 : }
6064 :
6065 : /*
6066 : * Files or directories can't be opened DELETE_ON_CLOSE without
6067 : * delete access.
6068 : * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13358
6069 : */
6070 555979 : if ((create_options & FILE_DELETE_ON_CLOSE) &&
6071 231823 : ((access_mask & DELETE_ACCESS) == 0)) {
6072 84 : status = NT_STATUS_INVALID_PARAMETER;
6073 84 : goto fail;
6074 : }
6075 :
6076 555895 : if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
6077 502688 : && is_named_stream(smb_fname))
6078 : {
6079 3 : uint32_t base_create_disposition;
6080 7329 : struct smb_filename *smb_fname_base = NULL;
6081 3 : uint32_t base_privflags;
6082 :
6083 7329 : if (create_options & FILE_DIRECTORY_FILE) {
6084 12 : DBG_DEBUG("Can't open a stream as directory\n");
6085 12 : status = NT_STATUS_NOT_A_DIRECTORY;
6086 12 : goto fail;
6087 : }
6088 :
6089 7317 : switch (create_disposition) {
6090 4746 : case FILE_OPEN:
6091 4746 : base_create_disposition = FILE_OPEN;
6092 4746 : break;
6093 2569 : default:
6094 2569 : base_create_disposition = FILE_OPEN_IF;
6095 2569 : break;
6096 : }
6097 :
6098 7317 : smb_fname_base = cp_smb_filename_nostream(
6099 : talloc_tos(), smb_fname);
6100 :
6101 7317 : if (smb_fname_base == NULL) {
6102 0 : status = NT_STATUS_NO_MEMORY;
6103 0 : goto fail;
6104 : }
6105 :
6106 : /*
6107 : * We may be creating the basefile as part of creating the
6108 : * stream, so it's legal if the basefile doesn't exist at this
6109 : * point, the create_file_unixpath() below will create it. But
6110 : * if the basefile exists we want a handle so we can fstat() it.
6111 : */
6112 :
6113 7317 : ret = vfs_stat(conn, smb_fname_base);
6114 7317 : if (ret == -1 && errno != ENOENT) {
6115 0 : status = map_nt_error_from_unix(errno);
6116 0 : TALLOC_FREE(smb_fname_base);
6117 0 : goto fail;
6118 : }
6119 7317 : if (ret == 0) {
6120 7117 : status = openat_pathref_fsp(conn->cwd_fsp,
6121 : smb_fname_base);
6122 7117 : if (!NT_STATUS_IS_OK(status)) {
6123 0 : DBG_ERR("open_smb_fname_fsp [%s] failed: %s\n",
6124 : smb_fname_str_dbg(smb_fname_base),
6125 : nt_errstr(status));
6126 0 : TALLOC_FREE(smb_fname_base);
6127 0 : goto fail;
6128 : }
6129 :
6130 : /*
6131 : * https://bugzilla.samba.org/show_bug.cgi?id=10229
6132 : * We need to check if the requested access mask
6133 : * could be used to open the underlying file (if
6134 : * it existed), as we're passing in zero for the
6135 : * access mask to the base filename.
6136 : */
6137 7117 : status = check_base_file_access(smb_fname_base->fsp,
6138 : access_mask);
6139 :
6140 7117 : if (!NT_STATUS_IS_OK(status)) {
6141 8 : DEBUG(10, ("Permission check "
6142 : "for base %s failed: "
6143 : "%s\n", smb_fname->base_name,
6144 : nt_errstr(status)));
6145 8 : TALLOC_FREE(smb_fname_base);
6146 8 : goto fail;
6147 : }
6148 : }
6149 :
6150 7309 : base_privflags = NTCREATEX_FLAG_STREAM_BASEOPEN;
6151 :
6152 : /* Open the base file. */
6153 7309 : status = create_file_unixpath(conn,
6154 : NULL,
6155 : dirfsp,
6156 : smb_fname_base,
6157 : 0,
6158 : FILE_SHARE_READ
6159 : | FILE_SHARE_WRITE
6160 : | FILE_SHARE_DELETE,
6161 : base_create_disposition,
6162 : 0,
6163 : 0,
6164 : 0,
6165 : NULL,
6166 : 0,
6167 : base_privflags,
6168 : NULL,
6169 : NULL,
6170 : &base_fsp,
6171 : NULL);
6172 7309 : TALLOC_FREE(smb_fname_base);
6173 :
6174 7309 : if (!NT_STATUS_IS_OK(status)) {
6175 8 : DEBUG(10, ("create_file_unixpath for base %s failed: "
6176 : "%s\n", smb_fname->base_name,
6177 : nt_errstr(status)));
6178 8 : goto fail;
6179 : }
6180 : }
6181 :
6182 555867 : if (smb_fname->fsp != NULL) {
6183 :
6184 288295 : fsp = smb_fname->fsp;
6185 :
6186 : /*
6187 : * We're about to use smb_fname->fsp for the fresh open.
6188 : *
6189 : * Every fsp passed in via smb_fname->fsp already
6190 : * holds a fsp->fsp_name. If it is already this
6191 : * fsp->fsp_name that we got passed in as our input
6192 : * argument smb_fname, these two are assumed to have
6193 : * the same lifetime: Every fsp hangs of "conn", and
6194 : * fsp->fsp_name is its talloc child.
6195 : */
6196 :
6197 288295 : if (smb_fname != smb_fname->fsp->fsp_name) {
6198 : /*
6199 : * "smb_fname" is temporary in this case, but
6200 : * the destructor of smb_fname would also tear
6201 : * down the fsp we're about to use. Unlink
6202 : * them from each other.
6203 : */
6204 288293 : smb_fname_fsp_unlink(smb_fname);
6205 :
6206 : /*
6207 : * "fsp" is ours now
6208 : */
6209 288293 : free_fsp_on_error = true;
6210 : }
6211 :
6212 288295 : status = fsp_bind_smb(fsp, req);
6213 288295 : if (!NT_STATUS_IS_OK(status)) {
6214 0 : goto fail;
6215 : }
6216 :
6217 288295 : if (fsp_is_alternate_stream(fsp)) {
6218 3280 : struct files_struct *tmp_base_fsp = fsp->base_fsp;
6219 :
6220 3280 : fsp_set_base_fsp(fsp, NULL);
6221 :
6222 3280 : fd_close(tmp_base_fsp);
6223 3280 : file_free(NULL, tmp_base_fsp);
6224 : }
6225 : } else {
6226 : /*
6227 : * No fsp passed in that we can use, create one
6228 : */
6229 267572 : status = file_new(req, conn, &fsp);
6230 267572 : if(!NT_STATUS_IS_OK(status)) {
6231 2 : goto fail;
6232 : }
6233 267570 : free_fsp_on_error = true;
6234 :
6235 267570 : status = fsp_set_smb_fname(fsp, smb_fname);
6236 267570 : if (!NT_STATUS_IS_OK(status)) {
6237 0 : goto fail;
6238 : }
6239 : }
6240 :
6241 555865 : SMB_ASSERT(fsp->fsp_name->fsp != NULL);
6242 555865 : SMB_ASSERT(fsp->fsp_name->fsp == fsp);
6243 :
6244 555865 : if (base_fsp) {
6245 : /*
6246 : * We're opening the stream element of a
6247 : * base_fsp we already opened. Set up the
6248 : * base_fsp pointer.
6249 : */
6250 7301 : fsp_set_base_fsp(fsp, base_fsp);
6251 : }
6252 :
6253 555865 : if (dirfsp != NULL) {
6254 553508 : status = SMB_VFS_PARENT_PATHNAME(
6255 : conn,
6256 : talloc_tos(),
6257 : smb_fname,
6258 : &parent_dir_fname,
6259 : &smb_fname_atname);
6260 553508 : if (!NT_STATUS_IS_OK(status)) {
6261 0 : goto fail;
6262 : }
6263 : } else {
6264 : /*
6265 : * Get a pathref on the parent. We can re-use this for
6266 : * multiple calls to check parent ACLs etc. to avoid
6267 : * pathname calls.
6268 : */
6269 2357 : status = parent_pathref(talloc_tos(),
6270 : conn->cwd_fsp,
6271 : smb_fname,
6272 : &parent_dir_fname,
6273 : &smb_fname_atname);
6274 2357 : if (!NT_STATUS_IS_OK(status)) {
6275 0 : goto fail;
6276 : }
6277 :
6278 2357 : dirfsp = parent_dir_fname->fsp;
6279 2357 : status = fsp_set_smb_fname(dirfsp, parent_dir_fname);
6280 2357 : if (!NT_STATUS_IS_OK(status)) {
6281 0 : goto fail;
6282 : }
6283 : }
6284 :
6285 : /*
6286 : * If it's a request for a directory open, deal with it separately.
6287 : */
6288 :
6289 555865 : if (create_options & FILE_DIRECTORY_FILE) {
6290 :
6291 56375 : if (create_options & FILE_NON_DIRECTORY_FILE) {
6292 0 : status = NT_STATUS_INVALID_PARAMETER;
6293 0 : goto fail;
6294 : }
6295 :
6296 : /* Can't open a temp directory. IFS kit test. */
6297 56375 : if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
6298 55366 : (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
6299 0 : status = NT_STATUS_INVALID_PARAMETER;
6300 0 : goto fail;
6301 : }
6302 :
6303 : /*
6304 : * We will get a create directory here if the Win32
6305 : * app specified a security descriptor in the
6306 : * CreateDirectory() call.
6307 : */
6308 :
6309 56375 : oplock_request = 0;
6310 56375 : status = open_directory(conn,
6311 : req,
6312 : access_mask,
6313 : share_access,
6314 : create_disposition,
6315 : create_options,
6316 : file_attributes,
6317 : dirfsp->fsp_name,
6318 : smb_fname_atname,
6319 : &info,
6320 : fsp);
6321 : } else {
6322 :
6323 : /*
6324 : * Ordinary file case.
6325 : */
6326 :
6327 499490 : if (allocation_size) {
6328 432 : fsp->initial_allocation_size = smb_roundup(fsp->conn,
6329 : allocation_size);
6330 : }
6331 :
6332 499490 : status = open_file_ntcreate(conn,
6333 : req,
6334 : access_mask,
6335 : share_access,
6336 : create_disposition,
6337 : create_options,
6338 : file_attributes,
6339 : oplock_request,
6340 : lease,
6341 : private_flags,
6342 : dirfsp->fsp_name,
6343 : smb_fname_atname,
6344 : &info,
6345 : fsp);
6346 499490 : if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
6347 :
6348 : /* A stream open never opens a directory */
6349 :
6350 34517 : if (base_fsp) {
6351 0 : status = NT_STATUS_FILE_IS_A_DIRECTORY;
6352 0 : goto fail;
6353 : }
6354 :
6355 : /*
6356 : * Fail the open if it was explicitly a non-directory
6357 : * file.
6358 : */
6359 :
6360 34517 : if (create_options & FILE_NON_DIRECTORY_FILE) {
6361 3451 : status = NT_STATUS_FILE_IS_A_DIRECTORY;
6362 3451 : goto fail;
6363 : }
6364 :
6365 31066 : oplock_request = 0;
6366 31066 : status = open_directory(conn,
6367 : req,
6368 : access_mask,
6369 : share_access,
6370 : create_disposition,
6371 : create_options,
6372 : file_attributes,
6373 : dirfsp->fsp_name,
6374 : smb_fname_atname,
6375 : &info,
6376 : fsp);
6377 : }
6378 : }
6379 :
6380 552414 : if (!NT_STATUS_IS_OK(status)) {
6381 113860 : goto fail;
6382 : }
6383 :
6384 438554 : fsp->fsp_flags.is_fsa = true;
6385 :
6386 438554 : if ((ea_list != NULL) &&
6387 290 : ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
6388 265 : status = set_ea(conn, fsp, ea_list);
6389 265 : if (!NT_STATUS_IS_OK(status)) {
6390 0 : goto fail;
6391 : }
6392 : }
6393 :
6394 438554 : if (!fsp->fsp_flags.is_directory &&
6395 356595 : S_ISDIR(fsp->fsp_name->st.st_ex_mode))
6396 : {
6397 0 : status = NT_STATUS_ACCESS_DENIED;
6398 0 : goto fail;
6399 : }
6400 :
6401 : /* Save the requested allocation size. */
6402 438554 : if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
6403 172455 : if ((allocation_size > (uint64_t)fsp->fsp_name->st.st_ex_size)
6404 184 : && !(fsp->fsp_flags.is_directory))
6405 : {
6406 358 : fsp->initial_allocation_size = smb_roundup(
6407 179 : fsp->conn, allocation_size);
6408 179 : if (vfs_allocate_file_space(
6409 179 : fsp, fsp->initial_allocation_size) == -1) {
6410 0 : status = NT_STATUS_DISK_FULL;
6411 0 : goto fail;
6412 : }
6413 : } else {
6414 172276 : fsp->initial_allocation_size = smb_roundup(
6415 172276 : fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
6416 : }
6417 : } else {
6418 266099 : fsp->initial_allocation_size = 0;
6419 : }
6420 :
6421 610210 : if ((info == FILE_WAS_CREATED) &&
6422 172002 : lp_nt_acl_support(SNUM(conn)) &&
6423 171656 : !fsp_is_alternate_stream(fsp)) {
6424 169415 : if (sd != NULL) {
6425 : /*
6426 : * According to the MS documentation, the only time the security
6427 : * descriptor is applied to the opened file is iff we *created* the
6428 : * file; an existing file stays the same.
6429 : *
6430 : * Also, it seems (from observation) that you can open the file with
6431 : * any access mask but you can still write the sd. We need to override
6432 : * the granted access before we call set_sd
6433 : * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
6434 : */
6435 :
6436 0 : uint32_t sec_info_sent;
6437 165 : uint32_t saved_access_mask = fsp->access_mask;
6438 :
6439 165 : sec_info_sent = get_sec_info(sd);
6440 :
6441 165 : fsp->access_mask = FILE_GENERIC_ALL;
6442 :
6443 165 : if (sec_info_sent & (SECINFO_OWNER|
6444 : SECINFO_GROUP|
6445 : SECINFO_DACL|
6446 : SECINFO_SACL)) {
6447 142 : status = set_sd(fsp, sd, sec_info_sent);
6448 : }
6449 :
6450 165 : fsp->access_mask = saved_access_mask;
6451 :
6452 165 : if (!NT_STATUS_IS_OK(status)) {
6453 0 : goto fail;
6454 : }
6455 169250 : } else if (lp_inherit_acls(SNUM(conn))) {
6456 : /* Inherit from parent. Errors here are not fatal. */
6457 147684 : status = inherit_new_acl(dirfsp, fsp);
6458 147684 : if (!NT_STATUS_IS_OK(status)) {
6459 0 : DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
6460 : fsp_str_dbg(fsp),
6461 : nt_errstr(status) ));
6462 : }
6463 : }
6464 : }
6465 :
6466 438554 : if ((conn->fs_capabilities & FILE_FILE_COMPRESSION)
6467 0 : && (create_options & FILE_NO_COMPRESSION)
6468 0 : && (info == FILE_WAS_CREATED)) {
6469 0 : status = SMB_VFS_SET_COMPRESSION(conn, fsp, fsp,
6470 : COMPRESSION_FORMAT_NONE);
6471 0 : if (!NT_STATUS_IS_OK(status)) {
6472 0 : DEBUG(1, ("failed to disable compression: %s\n",
6473 : nt_errstr(status)));
6474 : }
6475 : }
6476 :
6477 438554 : DEBUG(10, ("create_file_unixpath: info=%d\n", info));
6478 :
6479 438554 : *result = fsp;
6480 438554 : if (pinfo != NULL) {
6481 431253 : *pinfo = info;
6482 : }
6483 :
6484 438554 : smb_fname->st = fsp->fsp_name->st;
6485 :
6486 438554 : TALLOC_FREE(parent_dir_fname);
6487 :
6488 438554 : return NT_STATUS_OK;
6489 :
6490 117688 : fail:
6491 117688 : DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
6492 :
6493 117688 : if (fsp != NULL) {
6494 : /*
6495 : * The close_file below will close
6496 : * fsp->base_fsp.
6497 : */
6498 117311 : base_fsp = NULL;
6499 117311 : close_file_smb(req, fsp, ERROR_CLOSE);
6500 117311 : if (free_fsp_on_error) {
6501 117309 : file_free(req, fsp);
6502 117309 : fsp = NULL;
6503 : }
6504 : }
6505 117688 : if (base_fsp != NULL) {
6506 0 : close_file_free(req, &base_fsp, ERROR_CLOSE);
6507 : }
6508 :
6509 117688 : TALLOC_FREE(parent_dir_fname);
6510 :
6511 117688 : return status;
6512 : }
6513 :
6514 548978 : NTSTATUS create_file_default(connection_struct *conn,
6515 : struct smb_request *req,
6516 : struct files_struct *dirfsp,
6517 : struct smb_filename *smb_fname,
6518 : uint32_t access_mask,
6519 : uint32_t share_access,
6520 : uint32_t create_disposition,
6521 : uint32_t create_options,
6522 : uint32_t file_attributes,
6523 : uint32_t oplock_request,
6524 : const struct smb2_lease *lease,
6525 : uint64_t allocation_size,
6526 : uint32_t private_flags,
6527 : struct security_descriptor *sd,
6528 : struct ea_list *ea_list,
6529 : files_struct **result,
6530 : int *pinfo,
6531 : const struct smb2_create_blobs *in_context_blobs,
6532 : struct smb2_create_blobs *out_context_blobs)
6533 : {
6534 548978 : int info = FILE_WAS_OPENED;
6535 548978 : files_struct *fsp = NULL;
6536 1525 : NTSTATUS status;
6537 548978 : bool stream_name = false;
6538 548978 : struct smb2_create_blob *posx = NULL;
6539 :
6540 548978 : DBG_DEBUG("access_mask = 0x%" PRIu32
6541 : " file_attributes = 0x%" PRIu32
6542 : " share_access = 0x%" PRIu32
6543 : " create_disposition = 0x%" PRIu32
6544 : " create_options = 0x%" PRIu32
6545 : " oplock_request = 0x%" PRIu32
6546 : " private_flags = 0x%" PRIu32
6547 : " ea_list = %p, sd = %p, fname = %s\n",
6548 : access_mask,
6549 : file_attributes,
6550 : share_access,
6551 : create_disposition,
6552 : create_options,
6553 : oplock_request,
6554 : private_flags,
6555 : ea_list,
6556 : sd,
6557 : smb_fname_str_dbg(smb_fname));
6558 :
6559 548978 : if (req != NULL) {
6560 : /*
6561 : * Remember the absolute time of the original request
6562 : * with this mid. We'll use it later to see if this
6563 : * has timed out.
6564 : */
6565 547884 : get_deferred_open_message_state(req, &req->request_time, NULL);
6566 : }
6567 :
6568 : /*
6569 : * Check to see if this is a mac fork of some kind.
6570 : */
6571 :
6572 548978 : stream_name = is_ntfs_stream_smb_fname(smb_fname);
6573 548978 : if (stream_name) {
6574 3 : enum FAKE_FILE_TYPE fake_file_type;
6575 :
6576 7394 : fake_file_type = is_fake_file(smb_fname);
6577 :
6578 7394 : if (req != NULL && fake_file_type != FAKE_FILE_TYPE_NONE) {
6579 :
6580 : /*
6581 : * Here we go! support for changing the disk quotas
6582 : * --metze
6583 : *
6584 : * We need to fake up to open this MAGIC QUOTA file
6585 : * and return a valid FID.
6586 : *
6587 : * w2k close this file directly after opening xp
6588 : * also tries a QUERY_FILE_INFO on the file and then
6589 : * close it
6590 : */
6591 21 : status = open_fake_file(req, conn, req->vuid,
6592 : fake_file_type, smb_fname,
6593 : access_mask, &fsp);
6594 21 : if (!NT_STATUS_IS_OK(status)) {
6595 0 : goto fail;
6596 : }
6597 :
6598 21 : ZERO_STRUCT(smb_fname->st);
6599 21 : goto done;
6600 : }
6601 :
6602 7373 : if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
6603 0 : status = NT_STATUS_OBJECT_NAME_INVALID;
6604 0 : goto fail;
6605 : }
6606 : }
6607 :
6608 548957 : if (is_ntfs_default_stream_smb_fname(smb_fname)) {
6609 0 : int ret;
6610 : /* We have to handle this error here. */
6611 44 : if (create_options & FILE_DIRECTORY_FILE) {
6612 12 : status = NT_STATUS_NOT_A_DIRECTORY;
6613 12 : goto fail;
6614 : }
6615 32 : ret = vfs_stat(conn, smb_fname);
6616 32 : if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
6617 12 : status = NT_STATUS_FILE_IS_A_DIRECTORY;
6618 12 : goto fail;
6619 : }
6620 : }
6621 :
6622 548933 : posx = smb2_create_blob_find(
6623 : in_context_blobs, SMB2_CREATE_TAG_POSIX);
6624 548933 : if (posx != NULL) {
6625 2738 : uint32_t wire_mode_bits = 0;
6626 2738 : mode_t mode_bits = 0;
6627 2738 : SMB_STRUCT_STAT sbuf = { 0 };
6628 2738 : enum perm_type ptype =
6629 : (create_options & FILE_DIRECTORY_FILE) ?
6630 2738 : PERM_NEW_DIR : PERM_NEW_FILE;
6631 :
6632 2738 : if (posx->data.length != 4) {
6633 0 : status = NT_STATUS_INVALID_PARAMETER;
6634 0 : goto fail;
6635 : }
6636 :
6637 2738 : wire_mode_bits = IVAL(posx->data.data, 0);
6638 2738 : status = unix_perms_from_wire(
6639 : conn, &sbuf, wire_mode_bits, ptype, &mode_bits);
6640 2738 : if (!NT_STATUS_IS_OK(status)) {
6641 0 : goto fail;
6642 : }
6643 : /*
6644 : * Remove type info from mode, leaving only the
6645 : * permissions and setuid/gid bits.
6646 : */
6647 2738 : mode_bits &= ~S_IFMT;
6648 :
6649 2738 : file_attributes = (FILE_FLAG_POSIX_SEMANTICS | mode_bits);
6650 : }
6651 :
6652 548933 : status = create_file_unixpath(conn,
6653 : req,
6654 : dirfsp,
6655 : smb_fname,
6656 : access_mask,
6657 : share_access,
6658 : create_disposition,
6659 : create_options,
6660 : file_attributes,
6661 : oplock_request,
6662 : lease,
6663 : allocation_size,
6664 : private_flags,
6665 : sd,
6666 : ea_list,
6667 : &fsp,
6668 : &info);
6669 548933 : if (!NT_STATUS_IS_OK(status)) {
6670 117680 : goto fail;
6671 : }
6672 :
6673 431253 : done:
6674 431274 : DEBUG(10, ("create_file: info=%d\n", info));
6675 :
6676 431274 : *result = fsp;
6677 431274 : if (pinfo != NULL) {
6678 392188 : *pinfo = info;
6679 : }
6680 431274 : return NT_STATUS_OK;
6681 :
6682 117704 : fail:
6683 117704 : DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
6684 :
6685 117704 : if (fsp != NULL) {
6686 0 : close_file_free(req, &fsp, ERROR_CLOSE);
6687 : }
6688 117704 : return status;
6689 : }
|