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 */ 25 26 /* 27 * This module provides the common open functionality to the various 28 * open and create SMB interface functions. 29 */ 30 31 #include <sys/types.h> 32 #include <sys/cmn_err.h> 33 #include <sys/fcntl.h> 34 #include <sys/nbmlock.h> 35 #include <smbsrv/string.h> 36 #include <smbsrv/smb_kproto.h> 37 #include <smbsrv/smb_fsops.h> 38 #include <smbsrv/smbinfo.h> 39 40 volatile uint32_t smb_fids = 0; 41 42 static uint32_t smb_open_subr(smb_request_t *); 43 extern uint32_t smb_is_executable(char *); 44 static void smb_delete_new_object(smb_request_t *); 45 static int smb_set_open_timestamps(smb_request_t *, smb_ofile_t *, boolean_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 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_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 * Retry opens to avoid spurious sharing violations, due to timing 172 * issues between closes and opens. The client that already has the 173 * file open may be in the process of closing it. 174 */ 175 uint32_t 176 smb_common_open(smb_request_t *sr) 177 { 178 smb_arg_open_t *parg; 179 uint32_t status = NT_STATUS_SUCCESS; 180 int count; 181 182 parg = kmem_alloc(sizeof (*parg), KM_SLEEP); 183 bcopy(&sr->arg.open, parg, sizeof (*parg)); 184 185 for (count = 0; count <= 4; count++) { 186 if (count != 0) 187 delay(MSEC_TO_TICK(400)); 188 189 status = smb_open_subr(sr); 190 if (status != NT_STATUS_SHARING_VIOLATION) 191 break; 192 193 bcopy(parg, &sr->arg.open, sizeof (*parg)); 194 } 195 196 if (status == NT_STATUS_SHARING_VIOLATION) { 197 smbsr_error(sr, NT_STATUS_SHARING_VIOLATION, 198 ERRDOS, ERROR_SHARING_VIOLATION); 199 } 200 201 if (status == NT_STATUS_NO_SUCH_FILE) { 202 smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND, 203 ERRDOS, ERROR_FILE_NOT_FOUND); 204 } 205 206 kmem_free(parg, sizeof (*parg)); 207 208 return (status); 209 } 210 211 /* 212 * smb_open_subr 213 * 214 * Notes on write-through behaviour. It looks like pre-LM0.12 versions 215 * of the protocol specify the write-through mode when a file is opened, 216 * (SmbOpen, SmbOpenAndX) so the write calls (SmbWrite, SmbWriteAndClose, 217 * SmbWriteAndUnlock) don't need to contain a write-through flag. 218 * 219 * With LM0.12, the open calls (SmbCreateAndX, SmbNtTransactCreate) 220 * don't indicate which write-through mode to use. Instead the write 221 * calls (SmbWriteAndX, SmbWriteRaw) specify the mode on a per call 222 * basis. 223 * 224 * We don't care which open call was used to get us here, we just need 225 * to ensure that the write-through mode flag is copied from the open 226 * parameters to the node. We test the omode write-through flag in all 227 * write functions. 228 * 229 * This function will return NT status codes but it also raises errors, 230 * in which case it won't return to the caller. Be careful how you 231 * handle things in here. 232 * 233 * The following rules apply when processing a file open request: 234 * 235 * - Oplocks must be broken prior to share checking to prevent open 236 * starvation due to batch oplocks. Checking share reservations first 237 * could potentially result in unnecessary open failures due to 238 * open/close batching on the client. 239 * 240 * - Share checks must take place prior to access checks for correct 241 * Windows semantics and to prevent unnecessary NFS delegation recalls. 242 * 243 * - Oplocks must be acquired after open to ensure the correct 244 * synchronization with NFS delegation and FEM installation. 245 * 246 * 247 * DOS readonly bit rules 248 * 249 * 1. The creator of a readonly file can write to/modify the size of the file 250 * using the original create fid, even though the file will appear as readonly 251 * to all other fids and via a CIFS getattr call. 252 * The readonly bit therefore cannot be set in the filesystem until the file 253 * is closed (smb_ofile_close). It is accounted for via ofile and node flags. 254 * 255 * 2. A setinfo operation (using either an open fid or a path) to set/unset 256 * readonly will be successful regardless of whether a creator of a readonly 257 * file has an open fid (and has the special privilege mentioned in #1, 258 * above). I.e., the creator of a readonly fid holding that fid will no longer 259 * have a special privilege. 260 * 261 * 3. The DOS readonly bit affects only data and some metadata. 262 * The following metadata can be changed regardless of the readonly bit: 263 * - security descriptors 264 * - DOS attributes 265 * - timestamps 266 * 267 * In the current implementation, the file size cannot be changed (except for 268 * the exceptions in #1 and #2, above). 269 * 270 * 271 * DOS attribute rules 272 * 273 * These rules are specific to creating / opening files and directories. 274 * How the attribute value (specifically ZERO or FILE_ATTRIBUTE_NORMAL) 275 * should be interpreted may differ in other requests. 276 * 277 * - An attribute value equal to ZERO or FILE_ATTRIBUTE_NORMAL means that the 278 * file's attributes should be cleared. 279 * - If FILE_ATTRIBUTE_NORMAL is specified with any other attributes, 280 * FILE_ATTRIBUTE_NORMAL is ignored. 281 * 282 * 1. Creating a new file 283 * - The request attributes + FILE_ATTRIBUTE_ARCHIVE are applied to the file. 284 * 285 * 2. Creating a new directory 286 * - The request attributes + FILE_ATTRIBUTE_DIRECTORY are applied to the file. 287 * - FILE_ATTRIBUTE_ARCHIVE does not get set. 288 * 289 * 3. Overwriting an existing file 290 * - the request attributes are used as search attributes. If the existing 291 * file does not meet the search criteria access is denied. 292 * - otherwise, applies attributes + FILE_ATTRIBUTE_ARCHIVE. 293 * 294 * 4. Opening an existing file or directory 295 * The request attributes are ignored. 296 */ 297 static uint32_t 298 smb_open_subr(smb_request_t *sr) 299 { 300 boolean_t created = B_FALSE; 301 boolean_t last_comp_found = B_FALSE; 302 smb_node_t *node = NULL; 303 smb_node_t *dnode = NULL; 304 smb_node_t *cur_node = NULL; 305 smb_arg_open_t *op = &sr->sr_open; 306 int rc; 307 smb_ofile_t *of; 308 smb_attr_t new_attr; 309 int max_requested = 0; 310 uint32_t max_allowed; 311 uint32_t status = NT_STATUS_SUCCESS; 312 int is_dir; 313 smb_error_t err; 314 boolean_t is_stream = B_FALSE; 315 int lookup_flags = SMB_FOLLOW_LINKS; 316 uint32_t uniq_fid; 317 smb_pathname_t *pn = &op->fqi.fq_path; 318 319 is_dir = (op->create_options & FILE_DIRECTORY_FILE) ? 1 : 0; 320 321 /* 322 * If the object being created or opened is a directory 323 * the Disposition parameter must be one of FILE_CREATE, 324 * FILE_OPEN, or FILE_OPEN_IF 325 */ 326 if (is_dir) { 327 if ((op->create_disposition != FILE_CREATE) && 328 (op->create_disposition != FILE_OPEN_IF) && 329 (op->create_disposition != FILE_OPEN)) { 330 smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 331 ERRDOS, ERROR_INVALID_ACCESS); 332 return (NT_STATUS_INVALID_PARAMETER); 333 } 334 } 335 336 if (op->desired_access & MAXIMUM_ALLOWED) { 337 max_requested = 1; 338 op->desired_access &= ~MAXIMUM_ALLOWED; 339 } 340 op->desired_access = smb_access_generic_to_file(op->desired_access); 341 342 if (sr->session->s_file_cnt >= SMB_SESSION_OFILE_MAX) { 343 ASSERT(sr->uid_user); 344 cmn_err(CE_NOTE, "smbd[%s\\%s]: TOO_MANY_OPENED_FILES", 345 sr->uid_user->u_domain, sr->uid_user->u_name); 346 347 smbsr_error(sr, NT_STATUS_TOO_MANY_OPENED_FILES, 348 ERRDOS, ERROR_TOO_MANY_OPEN_FILES); 349 return (NT_STATUS_TOO_MANY_OPENED_FILES); 350 } 351 352 /* This must be NULL at this point */ 353 sr->fid_ofile = NULL; 354 355 op->devstate = 0; 356 357 switch (sr->tid_tree->t_res_type & STYPE_MASK) { 358 case STYPE_DISKTREE: 359 case STYPE_PRINTQ: 360 break; 361 362 case STYPE_IPC: 363 /* 364 * No further processing for IPC, we need to either 365 * raise an exception or return success here. 366 */ 367 if ((status = smb_opipe_open(sr)) != NT_STATUS_SUCCESS) 368 smbsr_error(sr, status, 0, 0); 369 return (status); 370 371 default: 372 smbsr_error(sr, NT_STATUS_BAD_DEVICE_TYPE, 373 ERRDOS, ERROR_BAD_DEV_TYPE); 374 return (NT_STATUS_BAD_DEVICE_TYPE); 375 } 376 377 smb_pathname_init(sr, pn, pn->pn_path); 378 if (!smb_pathname_validate(sr, pn)) 379 return (sr->smb_error.status); 380 381 if (strlen(pn->pn_path) >= MAXPATHLEN) { 382 smbsr_error(sr, 0, ERRSRV, ERRfilespecs); 383 return (NT_STATUS_NAME_TOO_LONG); 384 } 385 386 if (is_dir) { 387 if (!smb_validate_dirname(sr, pn)) 388 return (sr->smb_error.status); 389 } else { 390 if (!smb_validate_object_name(sr, pn)) 391 return (sr->smb_error.status); 392 } 393 394 cur_node = op->fqi.fq_dnode ? 395 op->fqi.fq_dnode : sr->tid_tree->t_snode; 396 397 /* 398 * if no path or filename are specified the stream should be 399 * created on cur_node 400 */ 401 if (!is_dir && !pn->pn_pname && !pn->pn_fname && pn->pn_sname) { 402 /* 403 * Can't currently handle a stream on the tree root. 404 * If a stream is being opened return "not found", otherwise 405 * return "access denied". 406 */ 407 if (cur_node == sr->tid_tree->t_snode) { 408 if (op->create_disposition == FILE_OPEN) { 409 smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND, 410 ERRDOS, ERROR_FILE_NOT_FOUND); 411 return (NT_STATUS_OBJECT_NAME_NOT_FOUND); 412 } 413 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS, 414 ERROR_ACCESS_DENIED); 415 return (NT_STATUS_ACCESS_DENIED); 416 } 417 418 (void) snprintf(op->fqi.fq_last_comp, 419 sizeof (op->fqi.fq_last_comp), 420 "%s%s", cur_node->od_name, pn->pn_sname); 421 422 op->fqi.fq_dnode = cur_node->n_dnode; 423 smb_node_ref(op->fqi.fq_dnode); 424 } else { 425 if (rc = smb_pathname_reduce(sr, sr->user_cr, pn->pn_path, 426 sr->tid_tree->t_snode, cur_node, &op->fqi.fq_dnode, 427 op->fqi.fq_last_comp)) { 428 smbsr_errno(sr, rc); 429 return (sr->smb_error.status); 430 } 431 } 432 433 /* 434 * If the access mask has only DELETE set (ignore 435 * FILE_READ_ATTRIBUTES), then assume that this 436 * is a request to delete the link (if a link) 437 * and do not follow links. Otherwise, follow 438 * the link to the target. 439 */ 440 if ((op->desired_access & ~FILE_READ_ATTRIBUTES) == DELETE) 441 lookup_flags &= ~SMB_FOLLOW_LINKS; 442 443 rc = smb_fsop_lookup_name(sr, kcred, lookup_flags, 444 sr->tid_tree->t_snode, op->fqi.fq_dnode, op->fqi.fq_last_comp, 445 &op->fqi.fq_fnode); 446 447 if (rc == 0) { 448 last_comp_found = B_TRUE; 449 rc = smb_node_getattr(sr, op->fqi.fq_fnode, 450 &op->fqi.fq_fattr); 451 if (rc != 0) { 452 smb_node_release(op->fqi.fq_fnode); 453 smb_node_release(op->fqi.fq_dnode); 454 smbsr_error(sr, NT_STATUS_INTERNAL_ERROR, 455 ERRDOS, ERROR_INTERNAL_ERROR); 456 return (sr->smb_error.status); 457 } 458 } else if (rc == ENOENT) { 459 last_comp_found = B_FALSE; 460 op->fqi.fq_fnode = NULL; 461 rc = 0; 462 } else { 463 smb_node_release(op->fqi.fq_dnode); 464 smbsr_errno(sr, rc); 465 return (sr->smb_error.status); 466 } 467 468 469 /* 470 * The uniq_fid is a CIFS-server-wide unique identifier for an ofile 471 * which is used to uniquely identify open instances for the 472 * VFS share reservation and POSIX locks. 473 */ 474 475 uniq_fid = SMB_UNIQ_FID(); 476 477 if (last_comp_found) { 478 479 node = op->fqi.fq_fnode; 480 dnode = op->fqi.fq_dnode; 481 482 if (!smb_node_is_file(node) && !smb_node_is_dir(node) && 483 !smb_node_is_symlink(node)) { 484 smb_node_release(node); 485 smb_node_release(dnode); 486 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS, 487 ERRnoaccess); 488 return (NT_STATUS_ACCESS_DENIED); 489 } 490 491 /* 492 * Reject this request if either: 493 * - the target IS a directory and the client requires that 494 * it must NOT be (required by Lotus Notes) 495 * - the target is NOT a directory and client requires that 496 * it MUST be. 497 */ 498 if (smb_node_is_dir(node)) { 499 if (op->create_options & FILE_NON_DIRECTORY_FILE) { 500 smb_node_release(node); 501 smb_node_release(dnode); 502 smbsr_error(sr, NT_STATUS_FILE_IS_A_DIRECTORY, 503 ERRDOS, ERROR_ACCESS_DENIED); 504 return (NT_STATUS_FILE_IS_A_DIRECTORY); 505 } 506 } else { 507 if ((op->create_options & FILE_DIRECTORY_FILE) || 508 (op->nt_flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)) { 509 smb_node_release(node); 510 smb_node_release(dnode); 511 smbsr_error(sr, NT_STATUS_NOT_A_DIRECTORY, 512 ERRDOS, ERROR_DIRECTORY); 513 return (NT_STATUS_NOT_A_DIRECTORY); 514 } 515 } 516 517 /* 518 * No more open should be accepted when "Delete on close" 519 * flag is set. 520 */ 521 if (node->flags & NODE_FLAGS_DELETE_ON_CLOSE) { 522 smb_node_release(node); 523 smb_node_release(dnode); 524 smbsr_error(sr, NT_STATUS_DELETE_PENDING, 525 ERRDOS, ERROR_ACCESS_DENIED); 526 return (NT_STATUS_DELETE_PENDING); 527 } 528 529 /* 530 * Specified file already exists so the operation should fail. 531 */ 532 if (op->create_disposition == FILE_CREATE) { 533 smb_node_release(node); 534 smb_node_release(dnode); 535 smbsr_error(sr, NT_STATUS_OBJECT_NAME_COLLISION, 536 ERRDOS, ERROR_FILE_EXISTS); 537 return (NT_STATUS_OBJECT_NAME_COLLISION); 538 } 539 540 /* 541 * Windows seems to check read-only access before file 542 * sharing check. 543 * 544 * Check to see if the file is currently readonly (irrespective 545 * of whether this open will make it readonly). 546 */ 547 if (SMB_PATHFILE_IS_READONLY(sr, node)) { 548 /* Files data only */ 549 if (!smb_node_is_dir(node)) { 550 if (op->desired_access & (FILE_WRITE_DATA | 551 FILE_APPEND_DATA)) { 552 smb_node_release(node); 553 smb_node_release(dnode); 554 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 555 ERRDOS, ERRnoaccess); 556 return (NT_STATUS_ACCESS_DENIED); 557 } 558 } 559 } 560 561 if (smb_oplock_conflict(node, sr->session, op)) 562 (void) smb_oplock_break(node, sr->session, B_FALSE); 563 564 smb_node_wrlock(node); 565 566 if ((op->create_disposition == FILE_SUPERSEDE) || 567 (op->create_disposition == FILE_OVERWRITE_IF) || 568 (op->create_disposition == FILE_OVERWRITE)) { 569 570 if ((!(op->desired_access & 571 (FILE_WRITE_DATA | FILE_APPEND_DATA | 572 FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA))) || 573 (!smb_sattr_check(op->fqi.fq_fattr.sa_dosattr, 574 op->dattr))) { 575 smb_node_unlock(node); 576 smb_node_release(node); 577 smb_node_release(dnode); 578 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 579 ERRDOS, ERRnoaccess); 580 return (NT_STATUS_ACCESS_DENIED); 581 } 582 } 583 584 status = smb_fsop_shrlock(sr->user_cr, node, uniq_fid, 585 op->desired_access, op->share_access); 586 587 if (status == NT_STATUS_SHARING_VIOLATION) { 588 smb_node_unlock(node); 589 smb_node_release(node); 590 smb_node_release(dnode); 591 return (status); 592 } 593 594 status = smb_fsop_access(sr, sr->user_cr, node, 595 op->desired_access); 596 597 if (status != NT_STATUS_SUCCESS) { 598 smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); 599 600 smb_node_unlock(node); 601 smb_node_release(node); 602 smb_node_release(dnode); 603 604 if (status == NT_STATUS_PRIVILEGE_NOT_HELD) { 605 smbsr_error(sr, status, 606 ERRDOS, ERROR_PRIVILEGE_NOT_HELD); 607 return (status); 608 } else { 609 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 610 ERRDOS, ERROR_ACCESS_DENIED); 611 return (NT_STATUS_ACCESS_DENIED); 612 } 613 } 614 615 switch (op->create_disposition) { 616 case FILE_SUPERSEDE: 617 case FILE_OVERWRITE_IF: 618 case FILE_OVERWRITE: 619 if (smb_node_is_dir(node)) { 620 smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); 621 smb_node_unlock(node); 622 smb_node_release(node); 623 smb_node_release(dnode); 624 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 625 ERRDOS, ERROR_ACCESS_DENIED); 626 return (NT_STATUS_ACCESS_DENIED); 627 } 628 629 op->dattr |= FILE_ATTRIBUTE_ARCHIVE; 630 /* Don't apply readonly bit until smb_ofile_close */ 631 if (op->dattr & FILE_ATTRIBUTE_READONLY) { 632 op->created_readonly = B_TRUE; 633 op->dattr &= ~FILE_ATTRIBUTE_READONLY; 634 } 635 636 bzero(&new_attr, sizeof (new_attr)); 637 new_attr.sa_dosattr = op->dattr; 638 new_attr.sa_vattr.va_size = op->dsize; 639 new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_SIZE; 640 rc = smb_fsop_setattr(sr, sr->user_cr, node, &new_attr); 641 if (rc != 0) { 642 smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); 643 smb_node_unlock(node); 644 smb_node_release(node); 645 smb_node_release(dnode); 646 smbsr_errno(sr, rc); 647 return (sr->smb_error.status); 648 } 649 650 /* 651 * If file is being replaced, remove existing streams 652 */ 653 if (SMB_IS_STREAM(node) == 0) { 654 rc = smb_fsop_remove_streams(sr, sr->user_cr, 655 node); 656 if (rc != 0) { 657 smb_fsop_unshrlock(sr->user_cr, node, 658 uniq_fid); 659 smb_node_unlock(node); 660 smb_node_release(node); 661 smb_node_release(dnode); 662 return (sr->smb_error.status); 663 } 664 } 665 666 op->action_taken = SMB_OACT_TRUNCATED; 667 break; 668 669 default: 670 /* 671 * FILE_OPEN or FILE_OPEN_IF. 672 */ 673 op->action_taken = SMB_OACT_OPENED; 674 break; 675 } 676 } else { 677 /* Last component was not found. */ 678 dnode = op->fqi.fq_dnode; 679 680 if (is_dir == 0) 681 is_stream = smb_is_stream_name(pn->pn_path); 682 683 if ((op->create_disposition == FILE_OPEN) || 684 (op->create_disposition == FILE_OVERWRITE)) { 685 smb_node_release(dnode); 686 smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND, 687 ERRDOS, ERROR_FILE_NOT_FOUND); 688 return (NT_STATUS_OBJECT_NAME_NOT_FOUND); 689 } 690 691 if (pn->pn_fname && smb_is_invalid_filename(pn->pn_fname)) { 692 smb_node_release(dnode); 693 smbsr_error(sr, NT_STATUS_OBJECT_NAME_INVALID, 694 ERRDOS, ERROR_INVALID_NAME); 695 return (NT_STATUS_OBJECT_NAME_INVALID); 696 } 697 698 /* 699 * lock the parent dir node in case another create 700 * request to the same parent directory comes in. 701 */ 702 smb_node_wrlock(dnode); 703 704 /* Don't apply readonly bit until smb_ofile_close */ 705 if (op->dattr & FILE_ATTRIBUTE_READONLY) { 706 op->dattr &= ~FILE_ATTRIBUTE_READONLY; 707 op->created_readonly = B_TRUE; 708 } 709 710 bzero(&new_attr, sizeof (new_attr)); 711 if ((op->crtime.tv_sec != 0) && 712 (op->crtime.tv_sec != UINT_MAX)) { 713 714 new_attr.sa_mask |= SMB_AT_CRTIME; 715 new_attr.sa_crtime = op->crtime; 716 } 717 718 if (is_dir == 0) { 719 op->dattr |= FILE_ATTRIBUTE_ARCHIVE; 720 new_attr.sa_dosattr = op->dattr; 721 new_attr.sa_vattr.va_type = VREG; 722 new_attr.sa_vattr.va_mode = is_stream ? S_IRUSR : 723 S_IRUSR | S_IRGRP | S_IROTH | 724 S_IWUSR | S_IWGRP | S_IWOTH; 725 new_attr.sa_mask |= 726 SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE; 727 728 if (op->dsize) { 729 new_attr.sa_vattr.va_size = op->dsize; 730 new_attr.sa_mask |= SMB_AT_SIZE; 731 } 732 733 rc = smb_fsop_create(sr, sr->user_cr, dnode, 734 op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode); 735 736 if (rc != 0) { 737 smb_node_unlock(dnode); 738 smb_node_release(dnode); 739 smbsr_errno(sr, rc); 740 return (sr->smb_error.status); 741 } 742 743 node = op->fqi.fq_fnode; 744 smb_node_wrlock(node); 745 746 status = smb_fsop_shrlock(sr->user_cr, node, uniq_fid, 747 op->desired_access, op->share_access); 748 749 if (status == NT_STATUS_SHARING_VIOLATION) { 750 smb_node_unlock(node); 751 smb_delete_new_object(sr); 752 smb_node_release(node); 753 smb_node_unlock(dnode); 754 smb_node_release(dnode); 755 return (status); 756 } 757 } else { 758 op->dattr |= FILE_ATTRIBUTE_DIRECTORY; 759 new_attr.sa_dosattr = op->dattr; 760 new_attr.sa_vattr.va_type = VDIR; 761 new_attr.sa_vattr.va_mode = 0777; 762 new_attr.sa_mask |= 763 SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE; 764 765 rc = smb_fsop_mkdir(sr, sr->user_cr, dnode, 766 op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode); 767 if (rc != 0) { 768 smb_node_unlock(dnode); 769 smb_node_release(dnode); 770 smbsr_errno(sr, rc); 771 return (sr->smb_error.status); 772 } 773 774 node = op->fqi.fq_fnode; 775 smb_node_wrlock(node); 776 } 777 778 created = B_TRUE; 779 op->action_taken = SMB_OACT_CREATED; 780 } 781 782 if (max_requested) { 783 smb_fsop_eaccess(sr, sr->user_cr, node, &max_allowed); 784 op->desired_access |= max_allowed; 785 } 786 787 status = NT_STATUS_SUCCESS; 788 789 of = smb_ofile_open(sr->tid_tree, node, sr->smb_pid, op, SMB_FTYPE_DISK, 790 uniq_fid, &err); 791 if (of == NULL) { 792 smbsr_error(sr, err.status, err.errcls, err.errcode); 793 status = err.status; 794 } 795 796 if (status == NT_STATUS_SUCCESS) { 797 if (!smb_tree_is_connected(sr->tid_tree)) { 798 smbsr_error(sr, 0, ERRSRV, ERRinvnid); 799 status = NT_STATUS_UNSUCCESSFUL; 800 } 801 } 802 803 /* 804 * This MUST be done after ofile creation, so that explicitly 805 * set timestamps can be remembered on the ofile. 806 */ 807 if (status == NT_STATUS_SUCCESS) { 808 if ((rc = smb_set_open_timestamps(sr, of, created)) != 0) { 809 smbsr_errno(sr, rc); 810 status = sr->smb_error.status; 811 } 812 } 813 814 if (status == NT_STATUS_SUCCESS) { 815 if (smb_node_getattr(sr, node, &op->fqi.fq_fattr) != 0) { 816 smbsr_error(sr, NT_STATUS_INTERNAL_ERROR, 817 ERRDOS, ERROR_INTERNAL_ERROR); 818 status = NT_STATUS_INTERNAL_ERROR; 819 } 820 } 821 822 /* 823 * smb_fsop_unshrlock is a no-op if node is a directory 824 * smb_fsop_unshrlock is done in smb_ofile_close 825 */ 826 if (status != NT_STATUS_SUCCESS) { 827 if (of == NULL) { 828 smb_fsop_unshrlock(sr->user_cr, node, uniq_fid); 829 } else { 830 smb_ofile_close(of, 0); 831 smb_ofile_release(of); 832 } 833 if (created) 834 smb_delete_new_object(sr); 835 smb_node_unlock(node); 836 smb_node_release(node); 837 if (created) 838 smb_node_unlock(dnode); 839 smb_node_release(dnode); 840 return (status); 841 } 842 843 /* 844 * Propagate the write-through mode from the open params 845 * to the node: see the notes in the function header. 846 */ 847 if (sr->sr_cfg->skc_sync_enable || 848 (op->create_options & FILE_WRITE_THROUGH)) 849 node->flags |= NODE_FLAGS_WRITE_THROUGH; 850 851 /* 852 * Set up the fileid and dosattr in open_param for response 853 */ 854 op->fileid = op->fqi.fq_fattr.sa_vattr.va_nodeid; 855 op->dattr = op->fqi.fq_fattr.sa_dosattr; 856 857 /* 858 * Set up the file type in open_param for the response 859 */ 860 op->ftype = SMB_FTYPE_DISK; 861 sr->smb_fid = of->f_fid; 862 sr->fid_ofile = of; 863 864 smb_node_unlock(node); 865 if (created) 866 smb_node_unlock(dnode); 867 868 if (smb_node_is_file(node)) { 869 smb_oplock_acquire(node, of, op); 870 op->dsize = op->fqi.fq_fattr.sa_vattr.va_size; 871 } else { 872 /* directory or symlink */ 873 op->op_oplock_level = SMB_OPLOCK_NONE; 874 op->dsize = 0; 875 } 876 877 smb_node_release(node); 878 smb_node_release(dnode); 879 880 return (NT_STATUS_SUCCESS); 881 } 882 883 /* 884 * smb_set_open_timestamps 885 * 886 * Last write time: 887 * - If the last_write time specified in the open params is not 0 or -1, 888 * use it as file's mtime. This will be considered an explicitly set 889 * timestamps, not reset by subsequent writes. 890 * 891 * Opening existing file (not directory): 892 * - If opening an existing file for overwrite set initial ATIME, MTIME 893 * & CTIME to now. (This is achieved by setting them as pending then forcing 894 * an smb_node_setattr() to apply pending times.) 895 * 896 * - Note If opening an existing file NOT for overwrite, windows would 897 * set the atime on file close, however setting the atime would cause 898 * the ARCHIVE attribute to be set, which does not occur on windows, 899 * so we do not do the atime update. 900 * 901 * Returns: errno 902 */ 903 static int 904 smb_set_open_timestamps(smb_request_t *sr, smb_ofile_t *of, boolean_t created) 905 { 906 int rc = 0; 907 smb_arg_open_t *op = &sr->sr_open; 908 smb_node_t *node = of->f_node; 909 boolean_t existing_file, set_times; 910 smb_attr_t attr; 911 912 bzero(&attr, sizeof (smb_attr_t)); 913 set_times = B_FALSE; 914 915 if ((op->mtime.tv_sec != 0) && (op->mtime.tv_sec != UINT_MAX)) { 916 attr.sa_mask = SMB_AT_MTIME; 917 attr.sa_vattr.va_mtime = op->mtime; 918 set_times = B_TRUE; 919 } 920 921 existing_file = !(created || smb_node_is_dir(node)); 922 if (existing_file) { 923 switch (op->create_disposition) { 924 case FILE_SUPERSEDE: 925 case FILE_OVERWRITE_IF: 926 case FILE_OVERWRITE: 927 smb_ofile_set_write_time_pending(of); 928 set_times = B_TRUE; 929 break; 930 default: 931 break; 932 } 933 } 934 935 if (set_times) 936 rc = smb_node_setattr(sr, node, sr->user_cr, of, &attr); 937 938 return (rc); 939 } 940 941 /* 942 * This function is used to delete a newly created object (file or 943 * directory) if an error occurs after creation of the object. 944 */ 945 static void 946 smb_delete_new_object(smb_request_t *sr) 947 { 948 smb_arg_open_t *op = &sr->sr_open; 949 smb_fqi_t *fqi = &(op->fqi); 950 uint32_t flags = 0; 951 952 if (SMB_TREE_IS_CASEINSENSITIVE(sr)) 953 flags |= SMB_IGNORE_CASE; 954 if (SMB_TREE_SUPPORTS_CATIA(sr)) 955 flags |= SMB_CATIA; 956 957 if (op->create_options & FILE_DIRECTORY_FILE) 958 (void) smb_fsop_rmdir(sr, sr->user_cr, fqi->fq_dnode, 959 fqi->fq_last_comp, flags); 960 else 961 (void) smb_fsop_remove(sr, sr->user_cr, fqi->fq_dnode, 962 fqi->fq_last_comp, flags); 963 } 964