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 2017 Nexenta Systems, 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/smb_kproto.h> 38 #include <smbsrv/smb_fsops.h> 39 #include <smbsrv/smbinfo.h> 40 41 int smb_session_ofile_max = 32768; 42 43 static volatile uint32_t smb_fids = 0; 44 #define SMB_UNIQ_FID() atomic_inc_32_nv(&smb_fids) 45 46 static uint32_t smb_open_subr(smb_request_t *); 47 extern uint32_t smb_is_executable(char *); 48 static void smb_delete_new_object(smb_request_t *); 49 static int smb_set_open_attributes(smb_request_t *, smb_ofile_t *); 50 static void smb_open_oplock_break(smb_request_t *, smb_node_t *); 51 static boolean_t smb_open_attr_only(smb_arg_open_t *); 52 static boolean_t smb_open_overwrite(smb_arg_open_t *); 53 54 /* 55 * smb_access_generic_to_file 56 * 57 * Search MSDN for IoCreateFile to see following mapping. 58 * 59 * GENERIC_READ STANDARD_RIGHTS_READ, FILE_READ_DATA, 60 * FILE_READ_ATTRIBUTES and FILE_READ_EA 61 * 62 * GENERIC_WRITE STANDARD_RIGHTS_WRITE, FILE_WRITE_DATA, 63 * FILE_WRITE_ATTRIBUTES, FILE_WRITE_EA, and FILE_APPEND_DATA 64 * 65 * GENERIC_EXECUTE STANDARD_RIGHTS_EXECUTE, SYNCHRONIZE, and FILE_EXECUTE. 66 */ 67 static uint32_t 68 smb_access_generic_to_file(uint32_t desired_access) 69 { 70 uint32_t access = 0; 71 72 if (desired_access & GENERIC_ALL) 73 return (FILE_ALL_ACCESS & ~SYNCHRONIZE); 74 75 if (desired_access & GENERIC_EXECUTE) { 76 desired_access &= ~GENERIC_EXECUTE; 77 access |= (STANDARD_RIGHTS_EXECUTE | 78 SYNCHRONIZE | FILE_EXECUTE); 79 } 80 81 if (desired_access & GENERIC_WRITE) { 82 desired_access &= ~GENERIC_WRITE; 83 access |= (FILE_GENERIC_WRITE & ~SYNCHRONIZE); 84 } 85 86 if (desired_access & GENERIC_READ) { 87 desired_access &= ~GENERIC_READ; 88 access |= FILE_GENERIC_READ; 89 } 90 91 return (access | desired_access); 92 } 93 94 /* 95 * smb_omode_to_amask 96 * 97 * This function converts open modes used by Open and Open AndX 98 * commands to desired access bits used by NT Create AndX command. 99 */ 100 uint32_t 101 smb_omode_to_amask(uint32_t desired_access) 102 { 103 switch (desired_access & SMB_DA_ACCESS_MASK) { 104 case SMB_DA_ACCESS_READ: 105 return (FILE_GENERIC_READ); 106 107 case SMB_DA_ACCESS_WRITE: 108 return (FILE_GENERIC_WRITE); 109 110 case SMB_DA_ACCESS_READ_WRITE: 111 return (FILE_GENERIC_READ | FILE_GENERIC_WRITE); 112 113 case SMB_DA_ACCESS_EXECUTE: 114 return (FILE_GENERIC_READ | FILE_GENERIC_EXECUTE); 115 116 default: 117 return (FILE_GENERIC_ALL); 118 } 119 } 120 121 /* 122 * smb_denymode_to_sharemode 123 * 124 * This function converts deny modes used by Open and Open AndX 125 * commands to share access bits used by NT Create AndX command. 126 */ 127 uint32_t 128 smb_denymode_to_sharemode(uint32_t desired_access, char *fname) 129 { 130 switch (desired_access & SMB_DA_SHARE_MASK) { 131 case SMB_DA_SHARE_COMPATIBILITY: 132 if (smb_is_executable(fname)) 133 return (FILE_SHARE_READ | FILE_SHARE_WRITE); 134 135 return (FILE_SHARE_ALL); 136 137 case SMB_DA_SHARE_EXCLUSIVE: 138 return (FILE_SHARE_NONE); 139 140 case SMB_DA_SHARE_DENY_WRITE: 141 return (FILE_SHARE_READ); 142 143 case SMB_DA_SHARE_DENY_READ: 144 return (FILE_SHARE_WRITE); 145 146 case SMB_DA_SHARE_DENY_NONE: 147 default: 148 return (FILE_SHARE_READ | FILE_SHARE_WRITE); 149 } 150 } 151 152 /* 153 * smb_ofun_to_crdisposition 154 * 155 * This function converts open function values used by Open and Open AndX 156 * commands to create disposition values used by NT Create AndX command. 157 */ 158 uint32_t 159 smb_ofun_to_crdisposition(uint16_t ofun) 160 { 161 static int ofun_cr_map[3][2] = 162 { 163 { -1, FILE_CREATE }, 164 { FILE_OPEN, FILE_OPEN_IF }, 165 { FILE_OVERWRITE, FILE_OVERWRITE_IF } 166 }; 167 168 int row = ofun & SMB_OFUN_OPEN_MASK; 169 int col = (ofun & SMB_OFUN_CREATE_MASK) >> 4; 170 171 if (row == 3) 172 return (FILE_MAXIMUM_DISPOSITION + 1); 173 174 return (ofun_cr_map[row][col]); 175 } 176 177 /* 178 * Retry opens to avoid spurious sharing violations, due to timing 179 * issues between closes and opens. The client that already has the 180 * file open may be in the process of closing it. 181 */ 182 uint32_t 183 smb_common_open(smb_request_t *sr) 184 { 185 smb_arg_open_t *parg; 186 uint32_t status = NT_STATUS_SUCCESS; 187 int count; 188 189 parg = kmem_alloc(sizeof (*parg), KM_SLEEP); 190 bcopy(&sr->arg.open, parg, sizeof (*parg)); 191 192 for (count = 0; count <= 4; count++) { 193 if (count != 0) 194 delay(MSEC_TO_TICK(400)); 195 196 status = smb_open_subr(sr); 197 if (status != NT_STATUS_SHARING_VIOLATION) 198 break; 199 200 bcopy(parg, &sr->arg.open, sizeof (*parg)); 201 } 202 203 if (status == NT_STATUS_NO_SUCH_FILE) 204 status = NT_STATUS_OBJECT_NAME_NOT_FOUND; 205 206 kmem_free(parg, sizeof (*parg)); 207 return (status); 208 } 209 210 /* 211 * smb_open_subr 212 * 213 * Notes on write-through behaviour. It looks like pre-LM0.12 versions 214 * of the protocol specify the write-through mode when a file is opened, 215 * (SmbOpen, SmbOpenAndX) so the write calls (SmbWrite, SmbWriteAndClose, 216 * SmbWriteAndUnlock) don't need to contain a write-through flag. 217 * 218 * With LM0.12, the open calls (SmbCreateAndX, SmbNtTransactCreate) 219 * don't indicate which write-through mode to use. Instead the write 220 * calls (SmbWriteAndX, SmbWriteRaw) specify the mode on a per call 221 * basis. 222 * 223 * We don't care which open call was used to get us here, we just need 224 * to ensure that the write-through mode flag is copied from the open 225 * parameters to the node. We test the omode write-through flag in all 226 * write functions. 227 * 228 * This function returns NT status codes. 229 * 230 * The following rules apply when processing a file open request: 231 * 232 * - Oplocks must be broken prior to share checking as the break may 233 * cause other clients to close the file, which would affect sharing 234 * checks. 235 * 236 * - Share checks must take place prior to access checks for correct 237 * Windows semantics and to prevent unnecessary NFS delegation recalls. 238 * 239 * - Oplocks must be acquired after open to ensure the correct 240 * synchronization with NFS delegation and FEM installation. 241 * 242 * DOS readonly bit rules 243 * 244 * 1. The creator of a readonly file can write to/modify the size of the file 245 * using the original create fid, even though the file will appear as readonly 246 * to all other fids and via a CIFS getattr call. 247 * The readonly bit therefore cannot be set in the filesystem until the file 248 * is closed (smb_ofile_close). It is accounted for via ofile and node flags. 249 * 250 * 2. A setinfo operation (using either an open fid or a path) to set/unset 251 * readonly will be successful regardless of whether a creator of a readonly 252 * file has an open fid (and has the special privilege mentioned in #1, 253 * above). I.e., the creator of a readonly fid holding that fid will no longer 254 * have a special privilege. 255 * 256 * 3. The DOS readonly bit affects only data and some metadata. 257 * The following metadata can be changed regardless of the readonly bit: 258 * - security descriptors 259 * - DOS attributes 260 * - timestamps 261 * 262 * In the current implementation, the file size cannot be changed (except for 263 * the exceptions in #1 and #2, above). 264 * 265 * 266 * DOS attribute rules 267 * 268 * These rules are specific to creating / opening files and directories. 269 * How the attribute value (specifically ZERO or FILE_ATTRIBUTE_NORMAL) 270 * should be interpreted may differ in other requests. 271 * 272 * - An attribute value equal to ZERO or FILE_ATTRIBUTE_NORMAL means that the 273 * file's attributes should be cleared. 274 * - If FILE_ATTRIBUTE_NORMAL is specified with any other attributes, 275 * FILE_ATTRIBUTE_NORMAL is ignored. 276 * 277 * 1. Creating a new file 278 * - The request attributes + FILE_ATTRIBUTE_ARCHIVE are applied to the file. 279 * 280 * 2. Creating a new directory 281 * - The request attributes + FILE_ATTRIBUTE_DIRECTORY are applied to the file. 282 * - FILE_ATTRIBUTE_ARCHIVE does not get set. 283 * 284 * 3. Overwriting an existing file 285 * - the request attributes are used as search attributes. If the existing 286 * file does not meet the search criteria access is denied. 287 * - otherwise, applies attributes + FILE_ATTRIBUTE_ARCHIVE. 288 * 289 * 4. Opening an existing file or directory 290 * The request attributes are ignored. 291 */ 292 static uint32_t 293 smb_open_subr(smb_request_t *sr) 294 { 295 boolean_t created = B_FALSE; 296 boolean_t last_comp_found = B_FALSE; 297 smb_node_t *node = NULL; 298 smb_node_t *dnode = NULL; 299 smb_node_t *cur_node = NULL; 300 smb_arg_open_t *op = &sr->sr_open; 301 int rc; 302 smb_ofile_t *of; 303 smb_attr_t new_attr; 304 int max_requested = 0; 305 uint32_t max_allowed; 306 uint32_t status = NT_STATUS_SUCCESS; 307 int is_dir; 308 smb_error_t err; 309 boolean_t is_stream = B_FALSE; 310 int lookup_flags = SMB_FOLLOW_LINKS; 311 uint32_t uniq_fid; 312 smb_pathname_t *pn = &op->fqi.fq_path; 313 smb_server_t *sv = sr->sr_server; 314 315 /* Get out now if we've been cancelled. */ 316 mutex_enter(&sr->sr_mutex); 317 if (sr->sr_state != SMB_REQ_STATE_ACTIVE) { 318 mutex_exit(&sr->sr_mutex); 319 return (NT_STATUS_CANCELLED); 320 } 321 mutex_exit(&sr->sr_mutex); 322 323 is_dir = (op->create_options & FILE_DIRECTORY_FILE) ? 1 : 0; 324 325 /* 326 * If the object being created or opened is a directory 327 * the Disposition parameter must be one of FILE_CREATE, 328 * FILE_OPEN, or FILE_OPEN_IF 329 */ 330 if (is_dir) { 331 if ((op->create_disposition != FILE_CREATE) && 332 (op->create_disposition != FILE_OPEN_IF) && 333 (op->create_disposition != FILE_OPEN)) { 334 return (NT_STATUS_INVALID_PARAMETER); 335 } 336 } 337 338 if (op->desired_access & MAXIMUM_ALLOWED) { 339 max_requested = 1; 340 op->desired_access &= ~MAXIMUM_ALLOWED; 341 } 342 op->desired_access = smb_access_generic_to_file(op->desired_access); 343 344 if (sr->session->s_file_cnt >= smb_session_ofile_max) { 345 ASSERT(sr->uid_user); 346 cmn_err(CE_NOTE, "smbsrv[%s\\%s]: TOO_MANY_OPENED_FILES", 347 sr->uid_user->u_domain, sr->uid_user->u_name); 348 return (NT_STATUS_TOO_MANY_OPENED_FILES); 349 } 350 351 /* This must be NULL at this point */ 352 sr->fid_ofile = NULL; 353 354 op->devstate = 0; 355 356 switch (sr->tid_tree->t_res_type & STYPE_MASK) { 357 case STYPE_DISKTREE: 358 case STYPE_PRINTQ: 359 break; 360 361 case STYPE_IPC: 362 /* 363 * Security descriptors for pipes are not implemented, 364 * so just setup a reasonable access mask. 365 */ 366 op->desired_access = (READ_CONTROL | SYNCHRONIZE | 367 FILE_READ_DATA | FILE_READ_ATTRIBUTES | 368 FILE_WRITE_DATA | FILE_APPEND_DATA); 369 370 /* 371 * Limit the number of open pipe instances. 372 */ 373 if ((rc = smb_threshold_enter(&sv->sv_opipe_ct)) != 0) { 374 status = RPC_NT_SERVER_TOO_BUSY; 375 return (status); 376 } 377 378 /* 379 * No further processing for IPC, we need to either 380 * raise an exception or return success here. 381 */ 382 uniq_fid = SMB_UNIQ_FID(); 383 status = smb_opipe_open(sr, uniq_fid); 384 smb_threshold_exit(&sv->sv_opipe_ct); 385 return (status); 386 387 default: 388 return (NT_STATUS_BAD_DEVICE_TYPE); 389 } 390 391 smb_pathname_init(sr, pn, pn->pn_path); 392 if (!smb_pathname_validate(sr, pn)) 393 return (sr->smb_error.status); 394 395 if (strlen(pn->pn_path) >= SMB_MAXPATHLEN) { 396 return (NT_STATUS_OBJECT_PATH_INVALID); 397 } 398 399 if (is_dir) { 400 if (!smb_validate_dirname(sr, pn)) 401 return (sr->smb_error.status); 402 } else { 403 if (!smb_validate_object_name(sr, pn)) 404 return (sr->smb_error.status); 405 } 406 407 cur_node = op->fqi.fq_dnode ? 408 op->fqi.fq_dnode : sr->tid_tree->t_snode; 409 410 rc = smb_pathname_reduce(sr, sr->user_cr, pn->pn_path, 411 sr->tid_tree->t_snode, cur_node, &op->fqi.fq_dnode, 412 op->fqi.fq_last_comp); 413 if (rc != 0) { 414 return (smb_errno2status(rc)); 415 } 416 417 /* 418 * If the access mask has only DELETE set (ignore 419 * FILE_READ_ATTRIBUTES), then assume that this 420 * is a request to delete the link (if a link) 421 * and do not follow links. Otherwise, follow 422 * the link to the target. 423 */ 424 if ((op->desired_access & ~FILE_READ_ATTRIBUTES) == DELETE) 425 lookup_flags &= ~SMB_FOLLOW_LINKS; 426 427 rc = smb_fsop_lookup_name(sr, zone_kcred(), lookup_flags, 428 sr->tid_tree->t_snode, op->fqi.fq_dnode, op->fqi.fq_last_comp, 429 &op->fqi.fq_fnode); 430 431 if (rc == 0) { 432 last_comp_found = B_TRUE; 433 /* 434 * Need the DOS attributes below, where we 435 * check the search attributes (sattr). 436 */ 437 op->fqi.fq_fattr.sa_mask = SMB_AT_DOSATTR; 438 rc = smb_node_getattr(sr, op->fqi.fq_fnode, zone_kcred(), 439 NULL, &op->fqi.fq_fattr); 440 if (rc != 0) { 441 smb_node_release(op->fqi.fq_fnode); 442 smb_node_release(op->fqi.fq_dnode); 443 return (NT_STATUS_INTERNAL_ERROR); 444 } 445 } else if (rc == ENOENT) { 446 last_comp_found = B_FALSE; 447 op->fqi.fq_fnode = NULL; 448 rc = 0; 449 } else { 450 smb_node_release(op->fqi.fq_dnode); 451 return (smb_errno2status(rc)); 452 } 453 454 455 /* 456 * The uniq_fid is a CIFS-server-wide unique identifier for an ofile 457 * which is used to uniquely identify open instances for the 458 * VFS share reservation and POSIX locks. 459 */ 460 461 uniq_fid = SMB_UNIQ_FID(); 462 463 if (last_comp_found) { 464 465 node = op->fqi.fq_fnode; 466 dnode = op->fqi.fq_dnode; 467 468 if (!smb_node_is_file(node) && !smb_node_is_dir(node) && 469 !smb_node_is_symlink(node)) { 470 smb_node_release(node); 471 smb_node_release(dnode); 472 return (NT_STATUS_ACCESS_DENIED); 473 } 474 475 /* 476 * Reject this request if either: 477 * - the target IS a directory and the client requires that 478 * it must NOT be (required by Lotus Notes) 479 * - the target is NOT a directory and client requires that 480 * it MUST be. 481 */ 482 if (smb_node_is_dir(node)) { 483 if (op->create_options & FILE_NON_DIRECTORY_FILE) { 484 smb_node_release(node); 485 smb_node_release(dnode); 486 return (NT_STATUS_FILE_IS_A_DIRECTORY); 487 } 488 } else { 489 if ((op->create_options & FILE_DIRECTORY_FILE) || 490 (op->nt_flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)) { 491 smb_node_release(node); 492 smb_node_release(dnode); 493 return (NT_STATUS_NOT_A_DIRECTORY); 494 } 495 } 496 497 /* 498 * No more open should be accepted when "Delete on close" 499 * flag is set. 500 */ 501 if (node->flags & NODE_FLAGS_DELETE_ON_CLOSE) { 502 smb_node_release(node); 503 smb_node_release(dnode); 504 return (NT_STATUS_DELETE_PENDING); 505 } 506 507 /* 508 * Specified file already exists so the operation should fail. 509 */ 510 if (op->create_disposition == FILE_CREATE) { 511 smb_node_release(node); 512 smb_node_release(dnode); 513 return (NT_STATUS_OBJECT_NAME_COLLISION); 514 } 515 516 /* 517 * Windows seems to check read-only access before file 518 * sharing check. 519 * 520 * Check to see if the file is currently readonly (irrespective 521 * of whether this open will make it readonly). 522 */ 523 if (SMB_PATHFILE_IS_READONLY(sr, node)) { 524 /* Files data only */ 525 if (!smb_node_is_dir(node)) { 526 if (op->desired_access & (FILE_WRITE_DATA | 527 FILE_APPEND_DATA)) { 528 smb_node_release(node); 529 smb_node_release(dnode); 530 return (NT_STATUS_ACCESS_DENIED); 531 } 532 if (op->create_options & FILE_DELETE_ON_CLOSE) { 533 smb_node_release(node); 534 smb_node_release(dnode); 535 return (NT_STATUS_CANNOT_DELETE); 536 } 537 } 538 } 539 540 if ((op->create_disposition == FILE_SUPERSEDE) || 541 (op->create_disposition == FILE_OVERWRITE_IF) || 542 (op->create_disposition == FILE_OVERWRITE)) { 543 544 if (!smb_sattr_check(op->fqi.fq_fattr.sa_dosattr, 545 op->dattr)) { 546 smb_node_release(node); 547 smb_node_release(dnode); 548 return (NT_STATUS_ACCESS_DENIED); 549 } 550 551 if (smb_node_is_dir(node)) { 552 smb_node_release(node); 553 smb_node_release(dnode); 554 return (NT_STATUS_ACCESS_DENIED); 555 } 556 } 557 558 /* MS-FSA 2.1.5.1.2 */ 559 if (op->create_disposition == FILE_SUPERSEDE) 560 op->desired_access |= DELETE; 561 if ((op->create_disposition == FILE_OVERWRITE_IF) || 562 (op->create_disposition == FILE_OVERWRITE)) 563 op->desired_access |= FILE_WRITE_DATA; 564 565 status = smb_fsop_access(sr, sr->user_cr, node, 566 op->desired_access); 567 if (status != NT_STATUS_SUCCESS) { 568 smb_node_release(node); 569 smb_node_release(dnode); 570 571 /* SMB1 specific? NT_STATUS_PRIVILEGE_NOT_HELD */ 572 if (status == NT_STATUS_PRIVILEGE_NOT_HELD) { 573 return (status); 574 } else { 575 return (NT_STATUS_ACCESS_DENIED); 576 } 577 } 578 579 if (max_requested) { 580 smb_fsop_eaccess(sr, sr->user_cr, node, &max_allowed); 581 op->desired_access |= max_allowed; 582 } 583 /* 584 * According to MS "dochelp" mail in Mar 2015, any handle 585 * on which read or write access is granted implicitly 586 * gets "read attributes", even if it was not requested. 587 * This avoids unexpected access failures later that 588 * would happen if these were not granted. 589 */ 590 if ((op->desired_access & FILE_DATA_ALL) != 0) { 591 op->desired_access |= (READ_CONTROL | 592 FILE_READ_ATTRIBUTES); 593 } 594 595 /* 596 * Oplock break is done prior to sharing checks as the break 597 * may cause other clients to close the file which would 598 * affect the sharing checks. This may block, so set the 599 * file opening count before oplock stuff. 600 */ 601 smb_node_inc_opening_count(node); 602 smb_open_oplock_break(sr, node); 603 604 smb_node_wrlock(node); 605 606 /* 607 * Check for sharing violations 608 */ 609 status = smb_fsop_shrlock(sr->user_cr, node, uniq_fid, 610 op->desired_access, op->share_access); 611 if (status == NT_STATUS_SHARING_VIOLATION) { 612 smb_node_unlock(node); 613 smb_node_dec_opening_count(node); 614 smb_node_release(node); 615 smb_node_release(dnode); 616 return (status); 617 } 618 619 /* 620 * Go ahead with modifications as necessary. 621 */ 622 switch (op->create_disposition) { 623 case FILE_SUPERSEDE: 624 case FILE_OVERWRITE_IF: 625 case FILE_OVERWRITE: 626 op->dattr |= FILE_ATTRIBUTE_ARCHIVE; 627 /* Don't apply readonly bit until smb_ofile_close */ 628 if (op->dattr & FILE_ATTRIBUTE_READONLY) { 629 op->created_readonly = B_TRUE; 630 op->dattr &= ~FILE_ATTRIBUTE_READONLY; 631 } 632 633 /* 634 * Truncate the file data here. 635 * We set alloc_size = op->dsize later, 636 * after we have an ofile. See: 637 * smb_set_open_attributes 638 */ 639 bzero(&new_attr, sizeof (new_attr)); 640 new_attr.sa_dosattr = op->dattr; 641 new_attr.sa_vattr.va_size = 0; 642 new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_SIZE; 643 rc = smb_fsop_setattr(sr, sr->user_cr, node, &new_attr); 644 if (rc != 0) { 645 smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); 646 smb_node_unlock(node); 647 smb_node_dec_opening_count(node); 648 smb_node_release(node); 649 smb_node_release(dnode); 650 return (smb_errno2status(rc)); 651 } 652 653 /* 654 * If file is being replaced, remove existing streams 655 */ 656 if (SMB_IS_STREAM(node) == 0) { 657 status = smb_fsop_remove_streams(sr, 658 sr->user_cr, node); 659 if (status != 0) { 660 smb_fsop_unshrlock(sr->user_cr, node, 661 uniq_fid); 662 smb_node_unlock(node); 663 smb_node_dec_opening_count(node); 664 smb_node_release(node); 665 smb_node_release(dnode); 666 return (status); 667 } 668 } 669 670 op->action_taken = SMB_OACT_TRUNCATED; 671 break; 672 673 default: 674 /* 675 * FILE_OPEN or FILE_OPEN_IF. 676 */ 677 /* 678 * Ignore any user-specified alloc_size for 679 * existing files, to avoid truncation in 680 * smb_set_open_attributes 681 */ 682 op->dsize = 0L; 683 op->action_taken = SMB_OACT_OPENED; 684 break; 685 } 686 } else { 687 /* Last component was not found. */ 688 dnode = op->fqi.fq_dnode; 689 690 if (is_dir == 0) 691 is_stream = smb_is_stream_name(pn->pn_path); 692 693 if ((op->create_disposition == FILE_OPEN) || 694 (op->create_disposition == FILE_OVERWRITE)) { 695 smb_node_release(dnode); 696 return (NT_STATUS_OBJECT_NAME_NOT_FOUND); 697 } 698 699 if (pn->pn_fname && smb_is_invalid_filename(pn->pn_fname)) { 700 smb_node_release(dnode); 701 return (NT_STATUS_OBJECT_NAME_INVALID); 702 } 703 704 /* 705 * Don't create in directories marked "Delete on close". 706 */ 707 if (dnode->flags & NODE_FLAGS_DELETE_ON_CLOSE) { 708 smb_node_release(dnode); 709 return (NT_STATUS_DELETE_PENDING); 710 } 711 712 /* 713 * lock the parent dir node in case another create 714 * request to the same parent directory comes in. 715 */ 716 smb_node_wrlock(dnode); 717 718 /* Don't apply readonly bit until smb_ofile_close */ 719 if (op->dattr & FILE_ATTRIBUTE_READONLY) { 720 op->dattr &= ~FILE_ATTRIBUTE_READONLY; 721 op->created_readonly = B_TRUE; 722 } 723 724 bzero(&new_attr, sizeof (new_attr)); 725 if ((op->crtime.tv_sec != 0) && 726 (op->crtime.tv_sec != UINT_MAX)) { 727 728 new_attr.sa_mask |= SMB_AT_CRTIME; 729 new_attr.sa_crtime = op->crtime; 730 } 731 732 if (is_dir == 0) { 733 op->dattr |= FILE_ATTRIBUTE_ARCHIVE; 734 new_attr.sa_dosattr = op->dattr; 735 new_attr.sa_vattr.va_type = VREG; 736 new_attr.sa_vattr.va_mode = is_stream ? S_IRUSR : 737 S_IRUSR | S_IRGRP | S_IROTH | 738 S_IWUSR | S_IWGRP | S_IWOTH; 739 new_attr.sa_mask |= 740 SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE; 741 742 /* 743 * We set alloc_size = op->dsize later, 744 * (in smb_set_open_attributes) after we 745 * have an ofile on which to save that. 746 * 747 * Legacy Open&X sets size to alloc_size 748 * when creating a new file. 749 */ 750 if (sr->smb_com == SMB_COM_OPEN_ANDX) { 751 new_attr.sa_vattr.va_size = op->dsize; 752 new_attr.sa_mask |= SMB_AT_SIZE; 753 } 754 755 rc = smb_fsop_create(sr, sr->user_cr, dnode, 756 op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode); 757 758 if (rc != 0) { 759 smb_node_unlock(dnode); 760 smb_node_release(dnode); 761 return (smb_errno2status(rc)); 762 } 763 764 node = op->fqi.fq_fnode; 765 smb_node_inc_opening_count(node); 766 smb_node_wrlock(node); 767 768 status = smb_fsop_shrlock(sr->user_cr, node, uniq_fid, 769 op->desired_access, op->share_access); 770 771 if (status == NT_STATUS_SHARING_VIOLATION) { 772 smb_node_unlock(node); 773 smb_node_dec_opening_count(node); 774 smb_delete_new_object(sr); 775 smb_node_release(node); 776 smb_node_unlock(dnode); 777 smb_node_release(dnode); 778 return (status); 779 } 780 } else { 781 op->dattr |= FILE_ATTRIBUTE_DIRECTORY; 782 new_attr.sa_dosattr = op->dattr; 783 new_attr.sa_vattr.va_type = VDIR; 784 new_attr.sa_vattr.va_mode = 0777; 785 new_attr.sa_mask |= 786 SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE; 787 788 rc = smb_fsop_mkdir(sr, sr->user_cr, dnode, 789 op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode); 790 if (rc != 0) { 791 smb_node_unlock(dnode); 792 smb_node_release(dnode); 793 return (smb_errno2status(rc)); 794 } 795 796 node = op->fqi.fq_fnode; 797 smb_node_inc_opening_count(node); 798 smb_node_wrlock(node); 799 } 800 801 created = B_TRUE; 802 op->action_taken = SMB_OACT_CREATED; 803 804 if (max_requested) { 805 smb_fsop_eaccess(sr, sr->user_cr, node, &max_allowed); 806 op->desired_access |= max_allowed; 807 } 808 /* 809 * We created this object (we own it) so grant 810 * read_control + read_attributes on this handle, 811 * even if that was not requested. This avoids 812 * unexpected access failures later. 813 */ 814 op->desired_access |= (READ_CONTROL | FILE_READ_ATTRIBUTES); 815 } 816 817 status = NT_STATUS_SUCCESS; 818 819 of = smb_ofile_open(sr, node, op, SMB_FTYPE_DISK, uniq_fid, 820 &err); 821 if (of == NULL) { 822 status = err.status; 823 } 824 825 /* 826 * We might have blocked in smb_ofile_open long enough so a 827 * tree disconnect might have happened. In that case, we've 828 * just added an ofile to a tree that's disconnecting, and 829 * need to undo that to avoid interfering with tear-down of 830 * the tree connection. 831 */ 832 if (status == NT_STATUS_SUCCESS && 833 !smb_tree_is_connected(sr->tid_tree)) { 834 status = NT_STATUS_INVALID_PARAMETER; 835 } 836 837 /* 838 * This MUST be done after ofile creation, so that explicitly 839 * set timestamps can be remembered on the ofile, and the 840 * readonly flag will be stored "pending" on the node. 841 */ 842 if (status == NT_STATUS_SUCCESS) { 843 if ((rc = smb_set_open_attributes(sr, of)) != 0) { 844 status = smb_errno2status(rc); 845 } 846 } 847 848 if (status == NT_STATUS_SUCCESS) { 849 /* 850 * We've already done access checks above, 851 * and want this call to succeed even when 852 * !(desired_access & FILE_READ_ATTRIBUTES), 853 * so pass kcred here. 854 */ 855 op->fqi.fq_fattr.sa_mask = SMB_AT_ALL; 856 rc = smb_node_getattr(sr, node, zone_kcred(), of, 857 &op->fqi.fq_fattr); 858 if (rc != 0) { 859 status = NT_STATUS_INTERNAL_ERROR; 860 } 861 } 862 863 /* 864 * smb_fsop_unshrlock is a no-op if node is a directory 865 * smb_fsop_unshrlock is done in smb_ofile_close 866 */ 867 if (status != NT_STATUS_SUCCESS) { 868 if (of == NULL) { 869 smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); 870 } else { 871 smb_ofile_close(of, 0); 872 smb_ofile_release(of); 873 } 874 if (created) 875 smb_delete_new_object(sr); 876 smb_node_unlock(node); 877 smb_node_dec_opening_count(node); 878 smb_node_release(node); 879 if (created) 880 smb_node_unlock(dnode); 881 smb_node_release(dnode); 882 return (status); 883 } 884 885 /* 886 * Propagate the write-through mode from the open params 887 * to the node: see the notes in the function header. 888 */ 889 if (sr->sr_cfg->skc_sync_enable || 890 (op->create_options & FILE_WRITE_THROUGH)) 891 node->flags |= NODE_FLAGS_WRITE_THROUGH; 892 893 /* 894 * Set up the fileid and dosattr in open_param for response 895 */ 896 op->fileid = op->fqi.fq_fattr.sa_vattr.va_nodeid; 897 op->dattr = op->fqi.fq_fattr.sa_dosattr; 898 899 /* 900 * Set up the file type in open_param for the response 901 */ 902 op->ftype = SMB_FTYPE_DISK; 903 sr->smb_fid = of->f_fid; 904 sr->fid_ofile = of; 905 906 if (smb_node_is_file(node)) { 907 smb_oplock_acquire(sr, node, of); 908 op->dsize = op->fqi.fq_fattr.sa_vattr.va_size; 909 } else { 910 /* directory or symlink */ 911 op->op_oplock_level = SMB_OPLOCK_NONE; 912 op->dsize = 0; 913 } 914 915 smb_node_dec_opening_count(node); 916 917 smb_node_unlock(node); 918 if (created) 919 smb_node_unlock(dnode); 920 921 smb_node_release(node); 922 smb_node_release(dnode); 923 924 return (NT_STATUS_SUCCESS); 925 } 926 927 /* 928 * smb_open_oplock_break 929 * 930 * If the node has an ofile opened with share access none, 931 * (smb_node_share_check = FALSE) only break BATCH oplock. 932 * Otherwise: 933 * If overwriting, break to SMB_OPLOCK_NONE, else 934 * If opening for anything other than attribute access, 935 * break oplock to LEVEL_II. 936 */ 937 static void 938 smb_open_oplock_break(smb_request_t *sr, smb_node_t *node) 939 { 940 smb_arg_open_t *op = &sr->sr_open; 941 uint32_t flags = 0; 942 943 if (!smb_node_share_check(node)) 944 flags |= SMB_OPLOCK_BREAK_BATCH; 945 946 if (smb_open_overwrite(op)) { 947 flags |= SMB_OPLOCK_BREAK_TO_NONE; 948 (void) smb_oplock_break(sr, node, flags); 949 } else if (!smb_open_attr_only(op)) { 950 flags |= SMB_OPLOCK_BREAK_TO_LEVEL_II; 951 (void) smb_oplock_break(sr, node, flags); 952 } 953 } 954 955 /* 956 * smb_open_attr_only 957 * 958 * Determine if file is being opened for attribute access only. 959 * This is used to determine whether it is necessary to break 960 * existing oplocks on the file. 961 */ 962 static boolean_t 963 smb_open_attr_only(smb_arg_open_t *op) 964 { 965 if (((op->desired_access & ~(FILE_READ_ATTRIBUTES | 966 FILE_WRITE_ATTRIBUTES | SYNCHRONIZE | READ_CONTROL)) == 0) && 967 (op->create_disposition != FILE_SUPERSEDE) && 968 (op->create_disposition != FILE_OVERWRITE)) { 969 return (B_TRUE); 970 } 971 return (B_FALSE); 972 } 973 974 static boolean_t 975 smb_open_overwrite(smb_arg_open_t *op) 976 { 977 if ((op->create_disposition == FILE_SUPERSEDE) || 978 (op->create_disposition == FILE_OVERWRITE_IF) || 979 (op->create_disposition == FILE_OVERWRITE)) { 980 return (B_TRUE); 981 } 982 return (B_FALSE); 983 } 984 985 /* 986 * smb_set_open_attributes 987 * 988 * Last write time: 989 * - If the last_write time specified in the open params is not 0 or -1, 990 * use it as file's mtime. This will be considered an explicitly set 991 * timestamps, not reset by subsequent writes. 992 * 993 * DOS attributes 994 * - If we created_readonly, we now store the real DOS attributes 995 * (including the readonly bit) so subsequent opens will see it. 996 * 997 * Both are stored "pending" rather than in the file system. 998 * 999 * Returns: errno 1000 */ 1001 static int 1002 smb_set_open_attributes(smb_request_t *sr, smb_ofile_t *of) 1003 { 1004 smb_attr_t attr; 1005 smb_arg_open_t *op = &sr->sr_open; 1006 smb_node_t *node = of->f_node; 1007 int rc = 0; 1008 1009 bzero(&attr, sizeof (smb_attr_t)); 1010 1011 if (op->created_readonly) { 1012 attr.sa_dosattr = op->dattr | FILE_ATTRIBUTE_READONLY; 1013 attr.sa_mask |= SMB_AT_DOSATTR; 1014 } 1015 1016 if (op->dsize != 0) { 1017 attr.sa_allocsz = op->dsize; 1018 attr.sa_mask |= SMB_AT_ALLOCSZ; 1019 } 1020 1021 if ((op->mtime.tv_sec != 0) && (op->mtime.tv_sec != UINT_MAX)) { 1022 attr.sa_vattr.va_mtime = op->mtime; 1023 attr.sa_mask |= SMB_AT_MTIME; 1024 } 1025 1026 /* 1027 * Used to have code here to set mtime, ctime, atime 1028 * when the open op->create_disposition is any of: 1029 * FILE_SUPERSEDE, FILE_OVERWRITE_IF, FILE_OVERWRITE. 1030 * We know that in those cases we will have set the 1031 * file size, in which case the file system will 1032 * update those times, so we don't have to. 1033 * 1034 * However, keep track of the fact that we modified 1035 * the file via this handle, so we can do the evil, 1036 * gratuitious mtime update on close that Windows 1037 * clients appear to expect. 1038 */ 1039 if (op->action_taken == SMB_OACT_TRUNCATED) 1040 of->f_written = B_TRUE; 1041 1042 if (attr.sa_mask != 0) 1043 rc = smb_node_setattr(sr, node, of->f_cr, of, &attr); 1044 1045 return (rc); 1046 } 1047 1048 /* 1049 * This function is used to delete a newly created object (file or 1050 * directory) if an error occurs after creation of the object. 1051 */ 1052 static void 1053 smb_delete_new_object(smb_request_t *sr) 1054 { 1055 smb_arg_open_t *op = &sr->sr_open; 1056 smb_fqi_t *fqi = &(op->fqi); 1057 uint32_t flags = 0; 1058 1059 if (SMB_TREE_IS_CASEINSENSITIVE(sr)) 1060 flags |= SMB_IGNORE_CASE; 1061 if (SMB_TREE_SUPPORTS_CATIA(sr)) 1062 flags |= SMB_CATIA; 1063 1064 if (op->create_options & FILE_DIRECTORY_FILE) 1065 (void) smb_fsop_rmdir(sr, sr->user_cr, fqi->fq_dnode, 1066 fqi->fq_last_comp, flags); 1067 else 1068 (void) smb_fsop_remove(sr, sr->user_cr, fqi->fq_dnode, 1069 fqi->fq_last_comp, flags); 1070 } 1071