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