1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright 2020 Tintri by DDN, Inc. All rights reserved. 25 */ 26 27 /* 28 * This module provides the common open functionality to the various 29 * open and create SMB interface functions. 30 */ 31 32 #include <sys/types.h> 33 #include <sys/cmn_err.h> 34 #include <sys/fcntl.h> 35 #include <sys/nbmlock.h> 36 #include <smbsrv/string.h> 37 #include <smbsrv/smb2_kproto.h> 38 #include <smbsrv/smb_fsops.h> 39 #include <smbsrv/smbinfo.h> 40 41 int smb_session_ofile_max = 32768; 42 43 extern uint32_t smb_is_executable(char *); 44 static void smb_delete_new_object(smb_request_t *); 45 static int smb_set_open_attributes(smb_request_t *, smb_ofile_t *); 46 47 /* 48 * smb_access_generic_to_file 49 * 50 * Search MSDN for IoCreateFile to see following mapping. 51 * 52 * GENERIC_READ STANDARD_RIGHTS_READ, FILE_READ_DATA, 53 * FILE_READ_ATTRIBUTES and FILE_READ_EA 54 * 55 * GENERIC_WRITE STANDARD_RIGHTS_WRITE, FILE_WRITE_DATA, 56 * FILE_WRITE_ATTRIBUTES, FILE_WRITE_EA, and FILE_APPEND_DATA 57 * 58 * GENERIC_EXECUTE STANDARD_RIGHTS_EXECUTE, SYNCHRONIZE, and FILE_EXECUTE. 59 */ 60 static uint32_t 61 smb_access_generic_to_file(uint32_t desired_access) 62 { 63 uint32_t access = 0; 64 65 if (desired_access & GENERIC_ALL) 66 return (FILE_ALL_ACCESS & ~SYNCHRONIZE); 67 68 if (desired_access & GENERIC_EXECUTE) { 69 desired_access &= ~GENERIC_EXECUTE; 70 access |= (STANDARD_RIGHTS_EXECUTE | 71 SYNCHRONIZE | FILE_EXECUTE); 72 } 73 74 if (desired_access & GENERIC_WRITE) { 75 desired_access &= ~GENERIC_WRITE; 76 access |= (FILE_GENERIC_WRITE & ~SYNCHRONIZE); 77 } 78 79 if (desired_access & GENERIC_READ) { 80 desired_access &= ~GENERIC_READ; 81 access |= FILE_GENERIC_READ; 82 } 83 84 return (access | desired_access); 85 } 86 87 /* 88 * smb_omode_to_amask 89 * 90 * This function converts open modes used by Open and Open AndX 91 * commands to desired access bits used by NT Create AndX command. 92 */ 93 uint32_t 94 smb_omode_to_amask(uint32_t desired_access) 95 { 96 switch (desired_access & SMB_DA_ACCESS_MASK) { 97 case SMB_DA_ACCESS_READ: 98 return (FILE_GENERIC_READ); 99 100 case SMB_DA_ACCESS_WRITE: 101 return (FILE_GENERIC_WRITE); 102 103 case SMB_DA_ACCESS_READ_WRITE: 104 return (FILE_GENERIC_READ | FILE_GENERIC_WRITE); 105 106 case SMB_DA_ACCESS_EXECUTE: 107 return (FILE_GENERIC_READ | FILE_GENERIC_EXECUTE); 108 109 default: 110 return (FILE_GENERIC_ALL); 111 } 112 } 113 114 /* 115 * smb_denymode_to_sharemode 116 * 117 * This function converts deny modes used by Open and Open AndX 118 * commands to share access bits used by NT Create AndX command. 119 */ 120 uint32_t 121 smb_denymode_to_sharemode(uint32_t desired_access, char *fname) 122 { 123 switch (desired_access & SMB_DA_SHARE_MASK) { 124 case SMB_DA_SHARE_COMPATIBILITY: 125 if (smb_is_executable(fname)) 126 return (FILE_SHARE_READ | FILE_SHARE_WRITE); 127 128 return (FILE_SHARE_ALL); 129 130 case SMB_DA_SHARE_EXCLUSIVE: 131 return (FILE_SHARE_NONE); 132 133 case SMB_DA_SHARE_DENY_WRITE: 134 return (FILE_SHARE_READ); 135 136 case SMB_DA_SHARE_DENY_READ: 137 return (FILE_SHARE_WRITE); 138 139 case SMB_DA_SHARE_DENY_NONE: 140 default: 141 return (FILE_SHARE_READ | FILE_SHARE_WRITE); 142 } 143 } 144 145 /* 146 * smb_ofun_to_crdisposition 147 * 148 * This function converts open function values used by Open and Open AndX 149 * commands to create disposition values used by NT Create AndX command. 150 */ 151 uint32_t 152 smb_ofun_to_crdisposition(uint16_t ofun) 153 { 154 static int ofun_cr_map[3][2] = 155 { 156 { -1, FILE_CREATE }, 157 { FILE_OPEN, FILE_OPEN_IF }, 158 { FILE_OVERWRITE, FILE_OVERWRITE_IF } 159 }; 160 161 int row = ofun & SMB_OFUN_OPEN_MASK; 162 int col = (ofun & SMB_OFUN_CREATE_MASK) >> 4; 163 164 if (row == 3) 165 return (FILE_MAXIMUM_DISPOSITION + 1); 166 167 return (ofun_cr_map[row][col]); 168 } 169 170 /* 171 * smb_common_open 172 * 173 * Notes on write-through behaviour. It looks like pre-LM0.12 versions 174 * of the protocol specify the write-through mode when a file is opened, 175 * (SmbOpen, SmbOpenAndX) so the write calls (SmbWrite, SmbWriteAndClose, 176 * SmbWriteAndUnlock) don't need to contain a write-through flag. 177 * 178 * With LM0.12, the open calls (SmbCreateAndX, SmbNtTransactCreate) 179 * don't indicate which write-through mode to use. Instead the write 180 * calls (SmbWriteAndX, SmbWriteRaw) specify the mode on a per call 181 * basis. 182 * 183 * We don't care which open call was used to get us here, we just need 184 * to ensure that the write-through mode flag is copied from the open 185 * parameters to the node. We test the omode write-through flag in all 186 * write functions. 187 * 188 * This function returns NT status codes. 189 * 190 * The following rules apply when processing a file open request: 191 * 192 * - Oplocks must be broken prior to share checking as the break may 193 * cause other clients to close the file, which would affect sharing 194 * checks. 195 * 196 * - Share checks must take place prior to access checks for correct 197 * Windows semantics and to prevent unnecessary NFS delegation recalls. 198 * 199 * - Oplocks must be acquired after open to ensure the correct 200 * synchronization with NFS delegation and FEM installation. 201 * 202 * DOS readonly bit rules 203 * 204 * 1. The creator of a readonly file can write to/modify the size of the file 205 * using the original create fid, even though the file will appear as readonly 206 * to all other fids and via a CIFS getattr call. 207 * 208 * 2. A setinfo operation (using either an open fid or a path) to set/unset 209 * readonly will be successful regardless of whether a creator of a readonly 210 * file has an open fid. 211 * 212 * 3. The DOS readonly bit affects only data and some metadata. 213 * The following metadata can be changed regardless of the readonly bit: 214 * - security descriptors 215 * - DOS attributes 216 * - timestamps 217 * 218 * In the current implementation, the file size cannot be changed (except for 219 * the exceptions in #1 and #2, above). 220 * 221 * 222 * DOS attribute rules 223 * 224 * These rules are specific to creating / opening files and directories. 225 * How the attribute value (specifically ZERO or FILE_ATTRIBUTE_NORMAL) 226 * should be interpreted may differ in other requests. 227 * 228 * - An attribute value equal to ZERO or FILE_ATTRIBUTE_NORMAL means that the 229 * file's attributes should be cleared. 230 * - If FILE_ATTRIBUTE_NORMAL is specified with any other attributes, 231 * FILE_ATTRIBUTE_NORMAL is ignored. 232 * 233 * 1. Creating a new file 234 * - The request attributes + FILE_ATTRIBUTE_ARCHIVE are applied to the file. 235 * 236 * 2. Creating a new directory 237 * - The request attributes + FILE_ATTRIBUTE_DIRECTORY are applied to the file. 238 * - FILE_ATTRIBUTE_ARCHIVE does not get set. 239 * 240 * 3. Overwriting an existing file 241 * - the request attributes are used as search attributes. If the existing 242 * file does not meet the search criteria access is denied. 243 * - otherwise, applies attributes + FILE_ATTRIBUTE_ARCHIVE. 244 * 245 * 4. Opening an existing file or directory 246 * The request attributes are ignored. 247 */ 248 uint32_t 249 smb_common_open(smb_request_t *sr) 250 { 251 smb_server_t *sv = sr->sr_server; 252 smb_tree_t *tree = sr->tid_tree; 253 smb_node_t *fnode = NULL; 254 smb_node_t *dnode = NULL; 255 smb_node_t *cur_node = NULL; 256 smb_node_t *tmp_node = NULL; 257 smb_arg_open_t *op = &sr->sr_open; 258 smb_pathname_t *pn = &op->fqi.fq_path; 259 smb_ofile_t *of = NULL; 260 smb_attr_t new_attr; 261 hrtime_t shrlock_t0; 262 int max_requested = 0; 263 uint32_t max_allowed; 264 uint32_t status = NT_STATUS_SUCCESS; 265 int is_dir; 266 int rc; 267 boolean_t is_stream = B_FALSE; 268 int lookup_flags = SMB_FOLLOW_LINKS; 269 uint32_t uniq_fid = 0; 270 uint16_t tree_fid = 0; 271 boolean_t created = B_FALSE; 272 boolean_t last_comp_found = B_FALSE; 273 boolean_t stream_found = B_FALSE; 274 boolean_t opening_incr = B_FALSE; 275 boolean_t dnode_held = B_FALSE; 276 boolean_t dnode_wlock = B_FALSE; 277 boolean_t fnode_held = B_FALSE; 278 boolean_t fnode_wlock = B_FALSE; 279 boolean_t fnode_shrlk = B_FALSE; 280 boolean_t did_open = B_FALSE; 281 boolean_t did_break_handle = B_FALSE; 282 boolean_t did_cleanup_orphans = B_FALSE; 283 char *sname = NULL; 284 285 /* Get out now if we've been cancelled. */ 286 mutex_enter(&sr->sr_mutex); 287 if (sr->sr_state != SMB_REQ_STATE_ACTIVE) { 288 mutex_exit(&sr->sr_mutex); 289 return (NT_STATUS_CANCELLED); 290 } 291 mutex_exit(&sr->sr_mutex); 292 293 is_dir = (op->create_options & FILE_DIRECTORY_FILE) ? 1 : 0; 294 295 /* 296 * If the object being created or opened is a directory 297 * the Disposition parameter must be one of FILE_CREATE, 298 * FILE_OPEN, or FILE_OPEN_IF 299 */ 300 if (is_dir) { 301 if ((op->create_disposition != FILE_CREATE) && 302 (op->create_disposition != FILE_OPEN_IF) && 303 (op->create_disposition != FILE_OPEN)) { 304 return (NT_STATUS_INVALID_PARAMETER); 305 } 306 } 307 308 if (op->desired_access & MAXIMUM_ALLOWED) { 309 max_requested = 1; 310 op->desired_access &= ~MAXIMUM_ALLOWED; 311 } 312 op->desired_access = smb_access_generic_to_file(op->desired_access); 313 314 if (sr->session->s_file_cnt >= smb_session_ofile_max) { 315 ASSERT(sr->uid_user); 316 cmn_err(CE_NOTE, "smbsrv[%s\\%s]: TOO_MANY_OPENED_FILES", 317 sr->uid_user->u_domain, sr->uid_user->u_name); 318 return (NT_STATUS_TOO_MANY_OPENED_FILES); 319 } 320 321 if (smb_idpool_alloc(&tree->t_fid_pool, &tree_fid)) 322 return (NT_STATUS_TOO_MANY_OPENED_FILES); 323 324 /* This must be NULL at this point */ 325 sr->fid_ofile = NULL; 326 327 op->devstate = 0; 328 329 switch (sr->tid_tree->t_res_type & STYPE_MASK) { 330 case STYPE_DISKTREE: 331 case STYPE_PRINTQ: 332 break; 333 334 case STYPE_IPC: 335 /* 336 * Security descriptors for pipes are not implemented, 337 * so just setup a reasonable access mask. 338 */ 339 op->desired_access = (READ_CONTROL | SYNCHRONIZE | 340 FILE_READ_DATA | FILE_READ_ATTRIBUTES | 341 FILE_WRITE_DATA | FILE_APPEND_DATA); 342 343 /* 344 * Limit the number of open pipe instances. 345 */ 346 if ((rc = smb_threshold_enter(&sv->sv_opipe_ct)) != 0) { 347 status = RPC_NT_SERVER_TOO_BUSY; 348 goto errout; 349 } 350 351 /* 352 * Most of IPC open is handled in smb_opipe_open() 353 */ 354 op->create_options = 0; 355 of = smb_ofile_alloc(sr, op, NULL, SMB_FTYPE_MESG_PIPE, 356 tree_fid); 357 tree_fid = 0; // given to the ofile 358 status = smb_opipe_open(sr, of); 359 smb_threshold_exit(&sv->sv_opipe_ct); 360 if (status != NT_STATUS_SUCCESS) 361 goto errout; 362 return (NT_STATUS_SUCCESS); 363 364 default: 365 status = NT_STATUS_BAD_DEVICE_TYPE; 366 goto errout; 367 } 368 369 smb_pathname_init(sr, pn, pn->pn_path); 370 if (!smb_pathname_validate(sr, pn)) { 371 status = sr->smb_error.status; 372 goto errout; 373 } 374 375 if (strlen(pn->pn_path) >= SMB_MAXPATHLEN) { 376 status = NT_STATUS_OBJECT_PATH_INVALID; 377 goto errout; 378 } 379 380 if (is_dir) { 381 if (!smb_validate_dirname(sr, pn)) { 382 status = sr->smb_error.status; 383 goto errout; 384 } 385 } else { 386 if (!smb_validate_object_name(sr, pn)) { 387 status = sr->smb_error.status; 388 goto errout; 389 } 390 } 391 392 cur_node = op->fqi.fq_dnode ? 393 op->fqi.fq_dnode : sr->tid_tree->t_snode; 394 395 rc = smb_pathname_reduce(sr, sr->user_cr, pn->pn_path, 396 sr->tid_tree->t_snode, cur_node, &op->fqi.fq_dnode, 397 op->fqi.fq_last_comp); 398 if (rc != 0) { 399 status = smb_errno2status(rc); 400 goto errout; 401 } 402 dnode = op->fqi.fq_dnode; 403 dnode_held = B_TRUE; 404 405 /* 406 * Lock the parent dir node in case another create 407 * request to the same parent directory comes in. 408 * Drop this once either lookup succeeds, or we've 409 * created the object in this directory. 410 */ 411 smb_node_wrlock(dnode); 412 dnode_wlock = B_TRUE; 413 414 /* 415 * If the access mask has only DELETE set (ignore 416 * FILE_READ_ATTRIBUTES), then assume that this 417 * is a request to delete the link (if a link) 418 * and do not follow links. Otherwise, follow 419 * the link to the target. 420 */ 421 if ((op->desired_access & ~FILE_READ_ATTRIBUTES) == DELETE) 422 lookup_flags &= ~SMB_FOLLOW_LINKS; 423 424 /* 425 * Lookup *just* the file portion of the name. 426 * Returns stream name in sname, which this allocates 427 */ 428 rc = smb_fsop_lookup_file(sr, zone_kcred(), lookup_flags, 429 sr->tid_tree->t_snode, op->fqi.fq_dnode, op->fqi.fq_last_comp, 430 &sname, &op->fqi.fq_fnode); 431 432 if (rc == 0) { 433 last_comp_found = B_TRUE; 434 fnode = op->fqi.fq_fnode; 435 fnode_held = B_TRUE; 436 437 /* 438 * Need the DOS attributes below, where we 439 * check the search attributes (sattr). 440 * Also UID, for owner check below. 441 */ 442 op->fqi.fq_fattr.sa_mask = SMB_AT_DOSATTR | SMB_AT_UID; 443 rc = smb_node_getattr(sr, op->fqi.fq_fnode, zone_kcred(), 444 NULL, &op->fqi.fq_fattr); 445 if (rc != 0) { 446 status = NT_STATUS_INTERNAL_ERROR; 447 goto errout; 448 } 449 } else if (rc == ENOENT) { 450 last_comp_found = B_FALSE; 451 op->fqi.fq_fnode = NULL; 452 rc = 0; 453 } else { 454 status = smb_errno2status(rc); 455 goto errout; 456 } 457 458 if (last_comp_found) { 459 460 fnode = op->fqi.fq_fnode; 461 dnode = op->fqi.fq_dnode; 462 463 if (!smb_node_is_file(fnode) && 464 !smb_node_is_dir(fnode) && 465 !smb_node_is_symlink(fnode)) { 466 status = NT_STATUS_ACCESS_DENIED; 467 goto errout; 468 } 469 470 /* 471 * Reject this request if either: 472 * - the target IS a directory and the client requires that 473 * it must NOT be (required by Lotus Notes) 474 * - the target is NOT a directory and client requires that 475 * it MUST be. 476 * Streams are never directories. 477 */ 478 if (smb_node_is_dir(fnode) && sname == NULL) { 479 if (op->create_options & FILE_NON_DIRECTORY_FILE) { 480 status = NT_STATUS_FILE_IS_A_DIRECTORY; 481 goto errout; 482 } 483 } else { 484 if ((op->create_options & FILE_DIRECTORY_FILE) || 485 (op->nt_flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)) { 486 status = NT_STATUS_NOT_A_DIRECTORY; 487 goto errout; 488 } 489 } 490 491 /* If we're given a stream name, look it up now */ 492 if (sname != NULL) { 493 tmp_node = fnode; 494 rc = smb_fsop_lookup_stream(sr, zone_kcred(), 495 lookup_flags, sr->tid_tree->t_snode, fnode, sname, 496 &fnode); 497 } else { 498 rc = 0; 499 } 500 501 if (rc == 0) { /* Stream Exists (including unnamed stream) */ 502 stream_found = B_TRUE; 503 smb_node_unlock(dnode); 504 dnode_wlock = B_FALSE; 505 506 if (tmp_node != NULL) 507 smb_node_release(tmp_node); 508 509 /* 510 * No more open should be accepted when 511 * "Delete on close" flag is set. 512 */ 513 if (fnode->flags & NODE_FLAGS_DELETE_ON_CLOSE) { 514 status = NT_STATUS_DELETE_PENDING; 515 goto errout; 516 } 517 518 /* 519 * Specified file already exists 520 * so the operation should fail. 521 */ 522 if (op->create_disposition == FILE_CREATE) { 523 status = NT_STATUS_OBJECT_NAME_COLLISION; 524 goto errout; 525 } 526 527 if ((op->create_disposition == FILE_SUPERSEDE) || 528 (op->create_disposition == FILE_OVERWRITE_IF) || 529 (op->create_disposition == FILE_OVERWRITE)) { 530 531 if (sname == NULL) { 532 if (!smb_sattr_check( 533 op->fqi.fq_fattr.sa_dosattr, 534 op->dattr)) { 535 status = 536 NT_STATUS_ACCESS_DENIED; 537 goto errout; 538 } 539 op->desired_access |= 540 FILE_WRITE_ATTRIBUTES; 541 } 542 543 if (smb_node_is_dir(fnode)) { 544 status = NT_STATUS_ACCESS_DENIED; 545 goto errout; 546 } 547 } 548 549 /* MS-FSA 2.1.5.1.2 */ 550 if (op->create_disposition == FILE_SUPERSEDE) 551 op->desired_access |= DELETE; 552 if ((op->create_disposition == FILE_OVERWRITE_IF) || 553 (op->create_disposition == FILE_OVERWRITE)) 554 op->desired_access |= FILE_WRITE_DATA; 555 } else if (rc == ENOENT) { /* File Exists, but Stream doesn't */ 556 if (op->create_disposition == FILE_OPEN || 557 op->create_disposition == FILE_OVERWRITE) { 558 status = NT_STATUS_OBJECT_NAME_NOT_FOUND; 559 goto errout; 560 } 561 562 op->desired_access |= FILE_WRITE_DATA; 563 } else { /* Error looking up stream */ 564 status = smb_errno2status(rc); 565 fnode = tmp_node; 566 goto errout; 567 } 568 569 /* 570 * Windows seems to check read-only access before file 571 * sharing check. 572 * 573 * Check to see if the file is currently readonly (regardless 574 * of whether this open will make it readonly). 575 * Readonly is ignored on directories. 576 */ 577 if (SMB_PATHFILE_IS_READONLY(sr, fnode) && 578 !smb_node_is_dir(fnode)) { 579 if (op->desired_access & 580 (FILE_WRITE_DATA | FILE_APPEND_DATA)) { 581 status = NT_STATUS_ACCESS_DENIED; 582 goto errout; 583 } 584 if (op->create_options & FILE_DELETE_ON_CLOSE) { 585 status = NT_STATUS_CANNOT_DELETE; 586 goto errout; 587 } 588 } 589 590 /* Dataset roots can't be deleted, so don't set DOC */ 591 if ((op->create_options & FILE_DELETE_ON_CLOSE) != 0 && 592 (fnode->flags & NODE_FLAGS_VFSROOT) != 0) { 593 status = NT_STATUS_CANNOT_DELETE; 594 goto errout; 595 } 596 597 status = smb_fsop_access(sr, sr->user_cr, fnode, 598 op->desired_access); 599 600 if (status != NT_STATUS_SUCCESS) 601 goto errout; 602 603 if (max_requested) { 604 smb_fsop_eaccess(sr, sr->user_cr, fnode, &max_allowed); 605 op->desired_access |= max_allowed; 606 } 607 608 /* 609 * File owner should always get read control + read attr. 610 */ 611 if (crgetuid(sr->user_cr) == op->fqi.fq_fattr.sa_vattr.va_uid) 612 op->desired_access |= 613 (READ_CONTROL | FILE_READ_ATTRIBUTES); 614 615 /* 616 * According to MS "dochelp" mail in Mar 2015, any handle 617 * on which read or write access is granted implicitly 618 * gets "read attributes", even if it was not requested. 619 */ 620 if ((op->desired_access & FILE_DATA_ALL) != 0) 621 op->desired_access |= FILE_READ_ATTRIBUTES; 622 623 /* If the stream didn't exist, create it now */ 624 if (!stream_found) { 625 smb_node_t *tmp_node = fnode; 626 627 bzero(&new_attr, sizeof (new_attr)); 628 new_attr.sa_vattr.va_type = VREG; 629 new_attr.sa_vattr.va_mode = S_IRUSR; 630 new_attr.sa_mask |= SMB_AT_TYPE | SMB_AT_MODE; 631 632 rc = smb_fsop_create_stream(sr, sr->user_cr, dnode, 633 fnode, sname, lookup_flags, &new_attr, &fnode); 634 smb_node_release(tmp_node); 635 636 if (rc != 0) { 637 status = smb_errno2status(rc); 638 fnode_held = B_FALSE; 639 goto errout; 640 } 641 op->action_taken = SMB_OACT_CREATED; 642 created = B_TRUE; 643 644 smb_node_unlock(dnode); 645 dnode_wlock = B_FALSE; 646 } 647 648 /* 649 * Oplock break is done prior to sharing checks as the break 650 * may cause other clients to close the file which would 651 * affect the sharing checks, and may delete the file due to 652 * DELETE_ON_CLOSE. This may block, so set the file opening 653 * count before oplock stuff. 654 * 655 * Need the "proposed" ofile (and its TargetOplockKey) for 656 * correct oplock break semantics. 657 */ 658 of = smb_ofile_alloc(sr, op, fnode, SMB_FTYPE_DISK, 659 tree_fid); 660 tree_fid = 0; // given to the ofile 661 uniq_fid = of->f_uniqid; 662 663 smb_node_inc_opening_count(fnode); 664 opening_incr = B_TRUE; 665 666 if (!stream_found) { 667 /* 668 * Stake our Share Access claim. 669 */ 670 smb_node_wrlock(fnode); 671 fnode_wlock = B_TRUE; 672 673 status = smb_fsop_shrlock(sr->user_cr, fnode, uniq_fid, 674 op->desired_access, op->share_access); 675 if (status != 0) 676 goto errout; 677 678 fnode_shrlk = B_TRUE; 679 smb_node_unlock(fnode); 680 fnode_wlock = B_FALSE; 681 goto stream_created; 682 } 683 684 /* 685 * XXX Supposed to do share access checks next. 686 * [MS-FSA] describes that as part of access check: 687 * 2.1.5.1.2.1 Alg... Check Access to an Existing File 688 * 689 * If CreateDisposition is FILE_OPEN or FILE_OPEN_IF: 690 * If Open.Stream.Oplock is not empty and 691 * Open.Stream.Oplock.State contains BATCH_OPLOCK, 692 * the object store MUST check for an oplock 693 * break according to the algorithm in section 2.1.4.12, 694 * with input values as follows: 695 * Open equal to this operation's Open 696 * Oplock equal to Open.Stream.Oplock 697 * Operation equal to "OPEN" 698 * OpParams containing two members: 699 * DesiredAccess, CreateDisposition 700 * 701 * It's not clear how Windows would ask the FS layer if 702 * the file has a BATCH oplock. We'll use a call to the 703 * common oplock code, which calls smb_oplock_break_OPEN 704 * only if the oplock state contains BATCH_OPLOCK. 705 * See: smb_oplock_break_BATCH() 706 * 707 * Also note: There's a nearly identical section in the 708 * spec. at the start of the "else" part of the above 709 * "if (disposition is overwrite, overwrite_if)" so this 710 * section (oplock break, the share mode check, and the 711 * next oplock_break_HANDLE) are all factored out to be 712 * in all cases above that if/else from the spec. 713 */ 714 status = smb_oplock_break_BATCH(fnode, of, 715 op->desired_access, op->create_disposition); 716 if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) { 717 if (sr->session->dialect >= SMB_VERS_2_BASE) 718 (void) smb2sr_go_async(sr); 719 status = smb_oplock_wait_break(sr, fnode, 0); 720 } 721 if (status != NT_STATUS_SUCCESS) 722 goto errout; 723 724 /* 725 * Check for sharing violations, and if any, 726 * do oplock break of handle caching. 727 * 728 * Need node_wrlock during shrlock checks, 729 * and not locked during oplock breaks etc. 730 */ 731 shrlock_t0 = gethrtime(); 732 shrlock_again: 733 smb_node_wrlock(fnode); 734 fnode_wlock = B_TRUE; 735 status = smb_fsop_shrlock(sr->user_cr, fnode, uniq_fid, 736 op->desired_access, op->share_access); 737 smb_node_unlock(fnode); 738 fnode_wlock = B_FALSE; 739 740 /* 741 * [MS-FSA] "OPEN_BREAK_H" 742 * If the (proposed) new open would violate sharing rules, 743 * indicate an oplock break with OPEN_BREAK_H (to break 744 * handle level caching rights) then try again. 745 */ 746 if (status == NT_STATUS_SHARING_VIOLATION && 747 did_break_handle == B_FALSE) { 748 did_break_handle = B_TRUE; 749 750 status = smb_oplock_break_HANDLE(fnode, of); 751 if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) { 752 if (sr->session->dialect >= SMB_VERS_2_BASE) 753 (void) smb2sr_go_async(sr); 754 status = smb_oplock_wait_break(sr, fnode, 0); 755 } else { 756 /* 757 * Even when the oplock layer does NOT 758 * give us the special status indicating 759 * we should wait, it may have scheduled 760 * taskq jobs that may close handles. 761 * Give those a chance to run before we 762 * check again for sharing violations. 763 */ 764 delay(MSEC_TO_TICK(10)); 765 } 766 if (status != NT_STATUS_SUCCESS) 767 goto errout; 768 769 goto shrlock_again; 770 } 771 772 /* 773 * If we still have orphaned durable handles on this file, 774 * let's assume the client has lost interest in those and 775 * close them so they don't cause sharing violations. 776 * See longer comment at smb2_dh_close_my_orphans(). 777 */ 778 if (status == NT_STATUS_SHARING_VIOLATION && 779 sr->session->dialect >= SMB_VERS_2_BASE && 780 did_cleanup_orphans == B_FALSE) { 781 782 did_cleanup_orphans = B_TRUE; 783 smb2_dh_close_my_orphans(sr, of); 784 785 goto shrlock_again; 786 } 787 788 /* 789 * SMB1 expects a 1 sec. delay before returning a 790 * sharing violation error. If breaking oplocks 791 * above took less than a sec, wait some more. 792 * See: smbtorture base.defer_open 793 */ 794 if (status == NT_STATUS_SHARING_VIOLATION && 795 sr->session->dialect < SMB_VERS_2_BASE) { 796 hrtime_t t1 = shrlock_t0 + NANOSEC; 797 hrtime_t now = gethrtime(); 798 if (now < t1) { 799 delay(NSEC_TO_TICK_ROUNDUP(t1 - now)); 800 } 801 } 802 803 if (status != NT_STATUS_SUCCESS) 804 goto errout; 805 fnode_shrlk = B_TRUE; 806 807 /* 808 * The [MS-FSA] spec. describes this oplock break as 809 * part of the sharing access checks. See: 810 * 2.1.5.1.2.2 Algorithm to Check Sharing Access... 811 * At the end of the share mode tests described there, 812 * if it has not returned "sharing violation", it 813 * specifies a call to the alg. in sec. 2.1.4.12, 814 * that boils down to: smb_oplock_break_OPEN() 815 */ 816 status = smb_oplock_break_OPEN(fnode, of, 817 op->desired_access, 818 op->create_disposition); 819 if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) { 820 if (sr->session->dialect >= SMB_VERS_2_BASE) 821 (void) smb2sr_go_async(sr); 822 status = smb_oplock_wait_break(sr, fnode, 0); 823 } 824 if (status != NT_STATUS_SUCCESS) 825 goto errout; 826 827 if ((fnode->flags & NODE_FLAGS_DELETE_COMMITTED) != 0) { 828 /* 829 * Breaking the oplock caused the file to be deleted, 830 * so let's bail and pretend the file wasn't found. 831 * Have to duplicate much of the logic found a the 832 * "errout" label here. 833 * 834 * This code path is exercised by smbtorture 835 * smb2.durable-open.delete_on_close1 836 */ 837 DTRACE_PROBE1(node_deleted, smb_node_t, fnode); 838 tree_fid = of->f_fid; 839 of->f_fid = 0; 840 smb_ofile_free(of); 841 of = NULL; 842 last_comp_found = B_FALSE; 843 844 /* 845 * Get all the holds and locks into the state 846 * they would have if lookup had failed. 847 */ 848 fnode_shrlk = B_FALSE; 849 smb_fsop_unshrlock(sr->user_cr, fnode, uniq_fid); 850 851 opening_incr = B_FALSE; 852 smb_node_dec_opening_count(fnode); 853 854 fnode_held = B_FALSE; 855 smb_node_release(fnode); 856 857 dnode_wlock = B_TRUE; 858 smb_node_wrlock(dnode); 859 860 goto create; 861 } 862 863 /* 864 * Go ahead with modifications as necessary. 865 */ 866 switch (op->create_disposition) { 867 case FILE_SUPERSEDE: 868 case FILE_OVERWRITE_IF: 869 case FILE_OVERWRITE: 870 bzero(&new_attr, sizeof (new_attr)); 871 if (sname == NULL) { 872 op->dattr |= FILE_ATTRIBUTE_ARCHIVE; 873 /* 874 * Don't apply readonly until 875 * smb_set_open_attributes 876 */ 877 if (op->dattr & FILE_ATTRIBUTE_READONLY) { 878 op->dattr &= ~FILE_ATTRIBUTE_READONLY; 879 op->created_readonly = B_TRUE; 880 } 881 new_attr.sa_dosattr = op->dattr; 882 } else { 883 new_attr.sa_dosattr = FILE_ATTRIBUTE_ARCHIVE; 884 } 885 886 /* 887 * Truncate the file data here. 888 * We set alloc_size = op->dsize later, 889 * after we have an ofile. See: 890 * smb_set_open_attributes 891 */ 892 new_attr.sa_vattr.va_size = 0; 893 new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_SIZE; 894 rc = smb_fsop_setattr(sr, sr->user_cr, fnode, 895 &new_attr); 896 if (rc != 0) { 897 status = smb_errno2status(rc); 898 goto errout; 899 } 900 901 /* 902 * If file is being replaced, remove existing streams 903 */ 904 if (SMB_IS_STREAM(fnode) == 0) { 905 status = smb_fsop_remove_streams(sr, 906 sr->user_cr, fnode); 907 if (status != 0) 908 goto errout; 909 } 910 911 op->action_taken = SMB_OACT_TRUNCATED; 912 break; 913 914 default: 915 /* 916 * FILE_OPEN or FILE_OPEN_IF. 917 */ 918 /* 919 * Ignore any user-specified alloc_size for 920 * existing files, to avoid truncation in 921 * smb_set_open_attributes 922 */ 923 op->dsize = 0L; 924 op->action_taken = SMB_OACT_OPENED; 925 break; 926 } 927 } else { 928 create: 929 /* Last component was not found. */ 930 dnode = op->fqi.fq_dnode; 931 932 if (is_dir == 0) 933 is_stream = smb_is_stream_name(pn->pn_path); 934 935 if ((op->create_disposition == FILE_OPEN) || 936 (op->create_disposition == FILE_OVERWRITE)) { 937 status = NT_STATUS_OBJECT_NAME_NOT_FOUND; 938 goto errout; 939 } 940 941 if ((op->desired_access & ACCESS_SYSTEM_SECURITY) != 0 && 942 !smb_user_has_security_priv(sr->uid_user, sr->user_cr)) { 943 status = NT_STATUS_ACCESS_DENIED; 944 goto errout; 945 } 946 947 if (pn->pn_fname && smb_is_invalid_filename(pn->pn_fname)) { 948 status = NT_STATUS_OBJECT_NAME_INVALID; 949 goto errout; 950 } 951 952 /* 953 * Don't create in directories marked "Delete on close". 954 */ 955 if (dnode->flags & NODE_FLAGS_DELETE_ON_CLOSE) { 956 status = NT_STATUS_DELETE_PENDING; 957 goto errout; 958 } 959 960 /* 961 * Create always sets the DOS attributes, type, and mode 962 * in the if/else below (different for file vs directory). 963 * Don't set the readonly bit until smb_set_open_attributes 964 * or that would prevent this open. Note that op->dattr 965 * needs to be what smb_set_open_attributes will use, 966 * except for the readonly bit. 967 */ 968 bzero(&new_attr, sizeof (new_attr)); 969 new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE; 970 if (op->dattr & FILE_ATTRIBUTE_READONLY) { 971 op->dattr &= ~FILE_ATTRIBUTE_READONLY; 972 op->created_readonly = B_TRUE; 973 } 974 975 /* 976 * SMB create can specify the create time. 977 */ 978 if ((op->crtime.tv_sec != 0) && 979 (op->crtime.tv_sec != UINT_MAX)) { 980 new_attr.sa_mask |= SMB_AT_CRTIME; 981 new_attr.sa_crtime = op->crtime; 982 } 983 984 if (is_dir == 0) { 985 op->dattr |= FILE_ATTRIBUTE_ARCHIVE; 986 new_attr.sa_dosattr = op->dattr; 987 new_attr.sa_vattr.va_type = VREG; 988 if (is_stream) 989 new_attr.sa_vattr.va_mode = S_IRUSR | S_IWUSR; 990 else 991 new_attr.sa_vattr.va_mode = 992 S_IRUSR | S_IRGRP | S_IROTH | 993 S_IWUSR | S_IWGRP | S_IWOTH; 994 995 /* 996 * We set alloc_size = op->dsize later, 997 * (in smb_set_open_attributes) after we 998 * have an ofile on which to save that. 999 * 1000 * Legacy Open&X sets size to alloc_size 1001 * when creating a new file. 1002 */ 1003 if (sr->smb_com == SMB_COM_OPEN_ANDX) { 1004 new_attr.sa_vattr.va_size = op->dsize; 1005 new_attr.sa_mask |= SMB_AT_SIZE; 1006 } 1007 1008 rc = smb_fsop_create(sr, sr->user_cr, dnode, 1009 op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode); 1010 } else { 1011 op->dattr |= FILE_ATTRIBUTE_DIRECTORY; 1012 new_attr.sa_dosattr = op->dattr; 1013 new_attr.sa_vattr.va_type = VDIR; 1014 new_attr.sa_vattr.va_mode = 0777; 1015 1016 rc = smb_fsop_mkdir(sr, sr->user_cr, dnode, 1017 op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode); 1018 } 1019 if (rc != 0) { 1020 status = smb_errno2status(rc); 1021 goto errout; 1022 } 1023 1024 /* Create done. */ 1025 smb_node_unlock(dnode); 1026 dnode_wlock = B_FALSE; 1027 1028 created = B_TRUE; 1029 op->action_taken = SMB_OACT_CREATED; 1030 1031 /* Note: hold from create */ 1032 fnode = op->fqi.fq_fnode; 1033 fnode_held = B_TRUE; 1034 1035 if (max_requested) { 1036 smb_fsop_eaccess(sr, sr->user_cr, fnode, &max_allowed); 1037 op->desired_access |= max_allowed; 1038 } 1039 /* 1040 * We created this object (we own it) so grant 1041 * read_control + read_attributes on this handle, 1042 * even if that was not requested. This avoids 1043 * unexpected access failures later. 1044 */ 1045 op->desired_access |= (READ_CONTROL | FILE_READ_ATTRIBUTES); 1046 1047 /* Allocate the ofile and fill in most of it. */ 1048 of = smb_ofile_alloc(sr, op, fnode, SMB_FTYPE_DISK, 1049 tree_fid); 1050 tree_fid = 0; // given to the ofile 1051 uniq_fid = of->f_uniqid; 1052 1053 smb_node_inc_opening_count(fnode); 1054 opening_incr = B_TRUE; 1055 1056 /* 1057 * Share access checks... 1058 */ 1059 smb_node_wrlock(fnode); 1060 fnode_wlock = B_TRUE; 1061 1062 status = smb_fsop_shrlock(sr->user_cr, fnode, uniq_fid, 1063 op->desired_access, op->share_access); 1064 if (status != 0) 1065 goto errout; 1066 fnode_shrlk = B_TRUE; 1067 1068 /* 1069 * MS-FSA 2.1.5.1.1 1070 * If the Oplock member of the DirectoryStream in 1071 * Link.ParentFile.StreamList (ParentOplock) is 1072 * not empty ... oplock break on the parent... 1073 * (dnode is the parent directory) 1074 * 1075 * This compares of->ParentOplockKey with each 1076 * oplock of->TargetOplockKey and breaks... 1077 * so it's OK that we're passing an OF that's 1078 * NOT a member of dnode->n_ofile_list 1079 * 1080 * The break never blocks, so ignore the return. 1081 */ 1082 (void) smb_oplock_break_PARENT(dnode, of); 1083 } 1084 1085 stream_created: 1086 /* 1087 * We might have blocked in smb_oplock_break_OPEN long enough 1088 * so a tree disconnect might have happened. In that case, 1089 * we would be adding an ofile to a tree that's disconnecting, 1090 * which would interfere with tear-down. If so, error out. 1091 */ 1092 if (!smb_tree_is_connected(sr->tid_tree)) { 1093 status = NT_STATUS_INVALID_PARAMETER; 1094 goto errout; 1095 } 1096 1097 /* 1098 * Moved this up from smb_ofile_open() 1099 */ 1100 if ((rc = smb_fsop_open(fnode, of->f_mode, of->f_cr)) != 0) { 1101 status = smb_errno2status(rc); 1102 goto errout; 1103 } 1104 1105 /* 1106 * Complete this open (add to ofile lists) 1107 */ 1108 smb_ofile_open(sr, op, of); 1109 did_open = B_TRUE; 1110 1111 /* 1112 * This MUST be done after ofile creation, so that explicitly 1113 * set timestamps can be remembered on the ofile, and setting 1114 * the readonly flag won't affect access via this open. 1115 */ 1116 if ((rc = smb_set_open_attributes(sr, of)) != 0) { 1117 status = smb_errno2status(rc); 1118 goto errout; 1119 } 1120 1121 /* 1122 * We've already done access checks above, 1123 * and want this call to succeed even when 1124 * !(desired_access & FILE_READ_ATTRIBUTES), 1125 * so pass kcred here. 1126 */ 1127 op->fqi.fq_fattr.sa_mask = SMB_AT_ALL; 1128 (void) smb_node_getattr(sr, fnode, zone_kcred(), of, 1129 &op->fqi.fq_fattr); 1130 1131 /* 1132 * Propagate the write-through mode from the open params 1133 * to the node: see the notes in the function header. 1134 * XXX: write_through should be a flag on the ofile. 1135 */ 1136 if (sr->sr_cfg->skc_sync_enable || 1137 (op->create_options & FILE_WRITE_THROUGH)) 1138 fnode->flags |= NODE_FLAGS_WRITE_THROUGH; 1139 1140 /* 1141 * Set up the fileid and dosattr in open_param for response 1142 */ 1143 op->fileid = op->fqi.fq_fattr.sa_vattr.va_nodeid; 1144 op->dattr = op->fqi.fq_fattr.sa_dosattr; 1145 1146 /* 1147 * Set up the file type in open_param for the response 1148 */ 1149 op->ftype = SMB_FTYPE_DISK; 1150 sr->smb_fid = of->f_fid; 1151 sr->fid_ofile = of; 1152 1153 if (smb_node_is_file(fnode)) { 1154 op->dsize = op->fqi.fq_fattr.sa_vattr.va_size; 1155 } else { 1156 /* directory or symlink */ 1157 op->dsize = 0; 1158 } 1159 1160 /* 1161 * Note: oplock_acquire happens in callers, because 1162 * how that happens is protocol-specific. 1163 */ 1164 1165 if (sname != NULL) 1166 kmem_free(sname, MAXNAMELEN); 1167 if (fnode_wlock) 1168 smb_node_unlock(fnode); 1169 if (opening_incr) 1170 smb_node_dec_opening_count(fnode); 1171 if (fnode_held) 1172 smb_node_release(fnode); 1173 if (dnode_wlock) 1174 smb_node_unlock(dnode); 1175 if (dnode_held) 1176 smb_node_release(dnode); 1177 1178 return (NT_STATUS_SUCCESS); 1179 1180 errout: 1181 if (did_open) { 1182 smb_ofile_close(of, 0); 1183 /* rele via sr->fid_ofile */ 1184 } else if (of != NULL) { 1185 /* No other refs possible */ 1186 smb_ofile_free(of); 1187 } 1188 1189 if (fnode_shrlk) 1190 smb_fsop_unshrlock(sr->user_cr, fnode, uniq_fid); 1191 1192 if (created) { 1193 /* Try to roll-back create. */ 1194 smb_delete_new_object(sr); 1195 } 1196 1197 if (sname != NULL) 1198 kmem_free(sname, MAXNAMELEN); 1199 if (fnode_wlock) 1200 smb_node_unlock(fnode); 1201 if (opening_incr) 1202 smb_node_dec_opening_count(fnode); 1203 if (fnode_held) 1204 smb_node_release(fnode); 1205 if (dnode_wlock) 1206 smb_node_unlock(dnode); 1207 if (dnode_held) 1208 smb_node_release(dnode); 1209 1210 if (tree_fid != 0) 1211 smb_idpool_free(&tree->t_fid_pool, tree_fid); 1212 1213 return (status); 1214 } 1215 1216 /* 1217 * smb_set_open_attributes 1218 * 1219 * Last write time: 1220 * - If the last_write time specified in the open params is not 0 or -1, 1221 * use it as file's mtime. This will be considered an explicitly set 1222 * timestamps, not reset by subsequent writes. 1223 * 1224 * DOS attributes 1225 * - If we created_readonly, we now store the real DOS attributes 1226 * (including the readonly bit) so subsequent opens will see it. 1227 * 1228 * Returns: errno 1229 */ 1230 static int 1231 smb_set_open_attributes(smb_request_t *sr, smb_ofile_t *of) 1232 { 1233 smb_attr_t attr; 1234 smb_arg_open_t *op = &sr->sr_open; 1235 smb_node_t *node = of->f_node; 1236 int rc = 0; 1237 1238 bzero(&attr, sizeof (smb_attr_t)); 1239 1240 if (op->created_readonly) { 1241 attr.sa_dosattr = op->dattr | FILE_ATTRIBUTE_READONLY; 1242 attr.sa_mask |= SMB_AT_DOSATTR; 1243 } 1244 1245 if (op->dsize != 0) { 1246 attr.sa_allocsz = op->dsize; 1247 attr.sa_mask |= SMB_AT_ALLOCSZ; 1248 } 1249 1250 if ((op->mtime.tv_sec != 0) && (op->mtime.tv_sec != UINT_MAX)) { 1251 attr.sa_vattr.va_mtime = op->mtime; 1252 attr.sa_mask |= SMB_AT_MTIME; 1253 } 1254 1255 if (attr.sa_mask != 0) 1256 rc = smb_node_setattr(sr, node, of->f_cr, of, &attr); 1257 1258 return (rc); 1259 } 1260 1261 /* 1262 * This function is used to delete a newly created object (file or 1263 * directory) if an error occurs after creation of the object. 1264 */ 1265 static void 1266 smb_delete_new_object(smb_request_t *sr) 1267 { 1268 smb_arg_open_t *op = &sr->sr_open; 1269 smb_fqi_t *fqi = &(op->fqi); 1270 uint32_t flags = 0; 1271 1272 if (SMB_TREE_IS_CASEINSENSITIVE(sr)) 1273 flags |= SMB_IGNORE_CASE; 1274 if (SMB_TREE_SUPPORTS_CATIA(sr)) 1275 flags |= SMB_CATIA; 1276 1277 if (op->create_options & FILE_DIRECTORY_FILE) 1278 (void) smb_fsop_rmdir(sr, sr->user_cr, fqi->fq_dnode, 1279 fqi->fq_last_comp, flags); 1280 else 1281 (void) smb_fsop_remove(sr, sr->user_cr, fqi->fq_dnode, 1282 fqi->fq_last_comp, flags); 1283 } 1284