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 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <smbsrv/smb_incl.h> 27 #include <smbsrv/smb_fsops.h> 28 #include <smbsrv/smbinfo.h> 29 #include <sys/nbmlock.h> 30 31 static int smb_delete_check_path(smb_request_t *, boolean_t *); 32 static int smb_delete_single_file(smb_request_t *, smb_error_t *); 33 static int smb_delete_multiple_files(smb_request_t *, smb_error_t *); 34 static int smb_delete_find_fname(smb_request_t *, smb_odir_t *); 35 static int smb_delete_check_attr(smb_request_t *, smb_error_t *); 36 static int smb_delete_remove_file(smb_request_t *, smb_error_t *); 37 38 static void smb_delete_error(smb_error_t *, uint32_t, uint16_t, uint16_t); 39 40 /* 41 * smb_com_delete 42 * 43 * The delete file message is sent to delete a data file. The appropriate 44 * Tid and additional pathname are passed. Read only files may not be 45 * deleted, the read-only attribute must be reset prior to file deletion. 46 * 47 * NT supports a hidden permission known as File Delete Child (FDC). If 48 * the user has FullControl access to a directory, the user is permitted 49 * to delete any object in the directory regardless of the permissions 50 * on the object. 51 * 52 * Client Request Description 53 * ================================== ================================= 54 * UCHAR WordCount; Count of parameter words = 1 55 * USHORT SearchAttributes; 56 * USHORT ByteCount; Count of data bytes; min = 2 57 * UCHAR BufferFormat; 0x04 58 * STRING FileName[]; File name 59 * 60 * Multiple files may be deleted in response to a single request as 61 * SMB_COM_DELETE supports wildcards 62 * 63 * SearchAttributes indicates the attributes that the target file(s) must 64 * have. If the attribute is zero then only normal files are deleted. If 65 * the system file or hidden attributes are specified then the delete is 66 * inclusive -both the specified type(s) of files and normal files are 67 * deleted. Attributes are described in the "Attribute Encoding" section 68 * of this document. 69 * 70 * If bit0 of the Flags2 field of the SMB header is set, a pattern is 71 * passed in, and the file has a long name, then the passed pattern much 72 * match the long file name for the delete to succeed. If bit0 is clear, a 73 * pattern is passed in, and the file has a long name, then the passed 74 * pattern must match the file's short name for the deletion to succeed. 75 * 76 * Server Response Description 77 * ================================== ================================= 78 * UCHAR WordCount; Count of parameter words = 0 79 * USHORT ByteCount; Count of data bytes = 0 80 * 81 * 4.2.10.1 Errors 82 * 83 * ERRDOS/ERRbadpath 84 * ERRDOS/ERRbadfile 85 * ERRDOS/ERRnoaccess 86 * ERRDOS/ERRbadshare # returned by NT for files that are already open 87 * ERRHRD/ERRnowrite 88 * ERRSRV/ERRaccess 89 * ERRSRV/ERRinvdevice 90 * ERRSRV/ERRinvid 91 * ERRSRV/ERRbaduid 92 */ 93 smb_sdrc_t 94 smb_pre_delete(smb_request_t *sr) 95 { 96 int rc; 97 smb_fqi_t *fqi; 98 99 fqi = &sr->arg.dirop.fqi; 100 101 if ((rc = smbsr_decode_vwv(sr, "w", &fqi->fq_sattr)) == 0) 102 rc = smbsr_decode_data(sr, "%S", sr, &fqi->fq_path.pn_path); 103 104 DTRACE_SMB_2(op__Delete__start, smb_request_t *, sr, smb_fqi_t *, fqi); 105 106 return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR); 107 } 108 109 void 110 smb_post_delete(smb_request_t *sr) 111 { 112 DTRACE_SMB_1(op__Delete__done, smb_request_t *, sr); 113 } 114 115 /* 116 * smb_com_delete 117 * 118 * 1. pre-process pathname - smb_delete_check_path() 119 * checks dot, bad path syntax, wildcards in path 120 * 121 * 2. process the path to get directory node & last_comp, 122 * store these in fqi 123 * - If smb_pathname_reduce cannot find the specified path, 124 * the error (ENOTDIR) is translated to NT_STATUS_OBJECT_PATH_NOT_FOUND 125 * if the target is a single file (no wildcards). If there are 126 * wildcards in the last_comp, NT_STATUS_OBJECT_NAME_NOT_FOUND is 127 * used instead. 128 * - If the directory node is the mount point and the last component 129 * is ".." NT_STATUS_OBJECT_PATH_SYNTAX_BAD is returned. 130 * 131 * 3. check access permissions 132 * 133 * 4. invoke the appropriate deletion routine to find and remove 134 * the specified file(s). 135 * - if target is a single file (no wildcards) - smb_delete_single_file 136 * - if the target contains wildcards - smb_delete_multiple_files 137 * 138 * Returns: SDRC_SUCCESS or SDRC_ERROR 139 */ 140 smb_sdrc_t 141 smb_com_delete(smb_request_t *sr) 142 { 143 int rc; 144 smb_error_t err; 145 uint32_t status; 146 boolean_t wildcards; 147 smb_fqi_t *fqi; 148 149 fqi = &sr->arg.dirop.fqi; 150 151 if (smb_delete_check_path(sr, &wildcards) != 0) 152 return (SDRC_ERROR); 153 154 rc = smb_pathname_reduce(sr, sr->user_cr, fqi->fq_path.pn_path, 155 sr->tid_tree->t_snode, sr->tid_tree->t_snode, 156 &fqi->fq_dnode, fqi->fq_last_comp); 157 if (rc == 0) { 158 if (fqi->fq_dnode->vp->v_type != VDIR) { 159 smb_node_release(fqi->fq_dnode); 160 rc = ENOTDIR; 161 } 162 } 163 if (rc != 0) { 164 if (rc == ENOTDIR) { 165 if (wildcards) 166 status = NT_STATUS_OBJECT_NAME_NOT_FOUND; 167 else 168 status = NT_STATUS_OBJECT_PATH_NOT_FOUND; 169 smbsr_error(sr, status, ERRDOS, ERROR_FILE_NOT_FOUND); 170 } else { 171 smbsr_errno(sr, rc); 172 } 173 174 return (SDRC_ERROR); 175 } 176 177 if ((fqi->fq_dnode == sr->tid_tree->t_snode) && 178 (strcmp(fqi->fq_last_comp, "..") == 0)) { 179 smb_node_release(fqi->fq_dnode); 180 smbsr_error(sr, NT_STATUS_OBJECT_PATH_SYNTAX_BAD, 181 ERRDOS, ERROR_BAD_PATHNAME); 182 return (SDRC_ERROR); 183 } 184 185 rc = smb_fsop_access(sr, sr->user_cr, fqi->fq_dnode, 186 FILE_LIST_DIRECTORY); 187 if (rc != 0) { 188 smb_node_release(fqi->fq_dnode); 189 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 190 ERRDOS, ERROR_ACCESS_DENIED); 191 return (SDRC_ERROR); 192 } 193 194 if (wildcards) 195 rc = smb_delete_multiple_files(sr, &err); 196 else 197 rc = smb_delete_single_file(sr, &err); 198 199 smb_node_release(fqi->fq_dnode); 200 201 if (rc != 0) 202 smbsr_set_error(sr, &err); 203 else 204 rc = smbsr_encode_empty_result(sr); 205 206 return (rc == 0 ? SDRC_SUCCESS : SDRC_ERROR); 207 } 208 209 /* 210 * smb_delete_single_file 211 * 212 * Find the specified file and, if its attributes match the search 213 * criteria, delete it. 214 * 215 * Returns 0 - success (file deleted) 216 * -1 - error, err is populated with error details 217 */ 218 static int 219 smb_delete_single_file(smb_request_t *sr, smb_error_t *err) 220 { 221 smb_fqi_t *fqi; 222 smb_attr_t ret_attr; 223 uint32_t status; 224 225 fqi = &sr->arg.dirop.fqi; 226 227 smb_pathname_setup(sr, &fqi->fq_path); 228 status = smb_validate_object_name(&fqi->fq_path); 229 if (status != NT_STATUS_SUCCESS) { 230 smb_delete_error(err, status, ERRDOS, ERROR_INVALID_NAME); 231 return (-1); 232 } 233 234 if (smb_fsop_lookup_name(sr, sr->user_cr, 0, sr->tid_tree->t_snode, 235 fqi->fq_dnode, fqi->fq_last_comp, &fqi->fq_fnode, &ret_attr) != 0) { 236 smb_delete_error(err, NT_STATUS_OBJECT_NAME_NOT_FOUND, 237 ERRDOS, ERROR_FILE_NOT_FOUND); 238 return (-1); 239 } 240 241 if (smb_delete_check_attr(sr, err) != 0) { 242 smb_node_release(fqi->fq_fnode); 243 return (-1); 244 } 245 246 if (smb_delete_remove_file(sr, err) != 0) { 247 smb_node_release(fqi->fq_fnode); 248 return (-1); 249 } 250 251 smb_node_release(fqi->fq_fnode); 252 return (0); 253 } 254 255 /* 256 * smb_delete_multiple_files 257 * 258 * For each matching file found by smb_delete_find_fname: 259 * 1. lookup file 260 * 2. check the file's attributes 261 * - The search ends with an error if a readonly file 262 * (NT_STATUS_CANNOT_DELETE) is matched. 263 * - The search ends (but not an error) if a directory is 264 * matched and the request's search did not include 265 * directories. 266 * - Otherwise, if smb_delete_check_attr fails the file 267 * is skipped and the search continues (at step 1) 268 * 3. delete the file 269 * 270 * Returns 0 - success 271 * -1 - error, err is populated with error details 272 */ 273 static int 274 smb_delete_multiple_files(smb_request_t *sr, smb_error_t *err) 275 { 276 int rc, deleted = 0; 277 smb_fqi_t *fqi; 278 smb_attr_t ret_attr; 279 uint16_t odid; 280 smb_odir_t *od; 281 282 fqi = &sr->arg.dirop.fqi; 283 284 /* 285 * Specify all search attributes (SMB_SEARCH_ATTRIBUTES) so that 286 * delete-specific checking can be done (smb_delete_check_attr). 287 */ 288 odid = smb_odir_open(sr, fqi->fq_path.pn_path, 289 SMB_SEARCH_ATTRIBUTES, 0); 290 if (odid == 0) 291 return (-1); 292 293 if ((od = smb_tree_lookup_odir(sr->tid_tree, odid)) == NULL) 294 return (-1); 295 296 for (;;) { 297 rc = smb_delete_find_fname(sr, od); 298 if (rc != 0) 299 break; 300 301 rc = smb_fsop_lookup_name(sr, sr->user_cr, 0, 302 sr->tid_tree->t_snode, fqi->fq_dnode, 303 fqi->fq_od_name, &fqi->fq_fnode, &ret_attr); 304 if (rc != 0) 305 break; 306 307 if (smb_delete_check_attr(sr, err) != 0) { 308 smb_node_release(fqi->fq_fnode); 309 if (err->status == NT_STATUS_CANNOT_DELETE) { 310 smb_odir_release(od); 311 smb_odir_close(od); 312 return (-1); 313 } 314 if ((err->status == NT_STATUS_FILE_IS_A_DIRECTORY) && 315 (SMB_SEARCH_DIRECTORY(fqi->fq_sattr) != 0)) 316 break; 317 continue; 318 } 319 320 if (smb_delete_remove_file(sr, err) == 0) { 321 ++deleted; 322 smb_node_release(fqi->fq_fnode); 323 continue; 324 } 325 if (err->status == NT_STATUS_OBJECT_NAME_NOT_FOUND) { 326 smb_node_release(fqi->fq_fnode); 327 continue; 328 } 329 330 smb_odir_release(od); 331 smb_odir_close(od); 332 smb_node_release(fqi->fq_fnode); 333 return (-1); 334 } 335 336 smb_odir_release(od); 337 smb_odir_close(od); 338 339 if ((rc != 0) && (rc != ENOENT)) { 340 smbsr_map_errno(rc, err); 341 return (-1); 342 } 343 344 if (deleted == 0) { 345 smb_delete_error(err, NT_STATUS_NO_SUCH_FILE, 346 ERRDOS, ERROR_FILE_NOT_FOUND); 347 return (-1); 348 } 349 350 return (0); 351 } 352 353 /* 354 * smb_delete_find_fname 355 * 356 * Find next filename that matches search pattern (fqi->fq_last_comp) 357 * and save it in fqi->fq_od_name. 358 * 359 * Case insensitivity note: 360 * If the tree is case insensitive and there's a case conflict 361 * with the name returned from smb_odir_read, smb_delete_find_fname 362 * performs case conflict name mangling to produce a unique filename. 363 * This ensures that any subsequent smb_fsop_lookup, (which will 364 * find the first case insensitive match) will find the correct file. 365 * 366 * Returns: 0 - success 367 * errno 368 */ 369 static int 370 smb_delete_find_fname(smb_request_t *sr, smb_odir_t *od) 371 { 372 int rc; 373 smb_odirent_t *odirent; 374 boolean_t eos; 375 char *name; 376 char shortname[SMB_SHORTNAMELEN]; 377 char name83[SMB_SHORTNAMELEN]; 378 smb_fqi_t *fqi; 379 380 fqi = &sr->arg.dirop.fqi; 381 odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP); 382 383 rc = smb_odir_read(sr, od, odirent, &eos); 384 if (rc != 0) { 385 kmem_free(odirent, sizeof (smb_odirent_t)); 386 return (rc); 387 } 388 if (eos) { 389 kmem_free(odirent, sizeof (smb_odirent_t)); 390 return (ENOENT); 391 } 392 393 /* if case conflict, force mangle and use shortname */ 394 if ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) && 395 (odirent->od_eflags & ED_CASE_CONFLICT)) { 396 (void) smb_mangle_name(odirent->od_ino, odirent->od_name, 397 shortname, name83, 1); 398 name = shortname; 399 } else { 400 name = odirent->od_name; 401 } 402 (void) strlcpy(fqi->fq_od_name, name, sizeof (fqi->fq_od_name)); 403 404 kmem_free(odirent, sizeof (smb_odirent_t)); 405 return (0); 406 } 407 408 /* 409 * smb_delete_check_attr 410 * 411 * Check file's dos atributes to ensure that 412 * 1. the file is not a directory - NT_STATUS_FILE_IS_A_DIRECTORY 413 * 2. the file is not readonly - NT_STATUS_CANNOT_DELETE 414 * 3. the file's dos attributes comply with the specified search attributes 415 * If the file is either hidden or system and those attributes 416 * are not specified in the search attributes - NT_STATUS_NO_SUCH_FILE 417 * 418 * Returns: 0 - file's attributes pass all checks 419 * -1 - err populated with error details 420 */ 421 static int 422 smb_delete_check_attr(smb_request_t *sr, smb_error_t *err) 423 { 424 smb_fqi_t *fqi; 425 smb_node_t *node; 426 uint16_t dosattr, sattr; 427 428 fqi = &sr->arg.dirop.fqi; 429 sattr = fqi->fq_sattr; 430 node = fqi->fq_fnode; 431 dosattr = smb_node_get_dosattr(node); 432 433 if (dosattr & FILE_ATTRIBUTE_DIRECTORY) { 434 smb_delete_error(err, NT_STATUS_FILE_IS_A_DIRECTORY, 435 ERRDOS, ERROR_ACCESS_DENIED); 436 return (-1); 437 } 438 439 if (SMB_PATHFILE_IS_READONLY(sr, node)) { 440 smb_delete_error(err, NT_STATUS_CANNOT_DELETE, 441 ERRDOS, ERROR_ACCESS_DENIED); 442 return (-1); 443 } 444 445 if ((dosattr & FILE_ATTRIBUTE_HIDDEN) && !(SMB_SEARCH_HIDDEN(sattr))) { 446 smb_delete_error(err, NT_STATUS_NO_SUCH_FILE, 447 ERRDOS, ERROR_FILE_NOT_FOUND); 448 return (-1); 449 } 450 451 if ((dosattr & FILE_ATTRIBUTE_SYSTEM) && !(SMB_SEARCH_SYSTEM(sattr))) { 452 smb_delete_error(err, NT_STATUS_NO_SUCH_FILE, 453 ERRDOS, ERROR_FILE_NOT_FOUND); 454 return (-1); 455 } 456 457 return (0); 458 } 459 460 /* 461 * smb_delete_remove_file 462 * 463 * For consistency with Windows 2000, the range check should be done 464 * after checking for sharing violations. Attempting to delete a 465 * locked file will result in sharing violation, which is the same 466 * thing that will happen if you try to delete a non-locked open file. 467 * 468 * Note that windows 2000 rejects lock requests on open files that 469 * have been opened with metadata open modes. The error is 470 * STATUS_ACCESS_DENIED. 471 * 472 * NT does not always close a file immediately, which can cause the 473 * share and access checking to fail (the node refcnt is greater 474 * than one), and the file doesn't get deleted. Breaking the oplock 475 * before share and access checking gives the client a chance to 476 * close the file. 477 * 478 * Returns: 0 - success 479 * -1 - error, err populated with error details 480 */ 481 static int 482 smb_delete_remove_file(smb_request_t *sr, smb_error_t *err) 483 { 484 int rc; 485 uint32_t status; 486 smb_fqi_t *fqi; 487 smb_node_t *node; 488 uint32_t flags = 0; 489 490 fqi = &sr->arg.dirop.fqi; 491 node = fqi->fq_fnode; 492 493 (void) smb_oplock_break(node, sr->session, B_FALSE); 494 495 smb_node_start_crit(node, RW_READER); 496 497 status = smb_node_delete_check(node); 498 if (status != NT_STATUS_SUCCESS) { 499 smb_delete_error(err, NT_STATUS_SHARING_VIOLATION, 500 ERRDOS, ERROR_SHARING_VIOLATION); 501 smb_node_end_crit(node); 502 return (-1); 503 } 504 505 status = smb_range_check(sr, node, 0, UINT64_MAX, B_TRUE); 506 if (status != NT_STATUS_SUCCESS) { 507 smb_delete_error(err, NT_STATUS_ACCESS_DENIED, 508 ERRDOS, ERROR_ACCESS_DENIED); 509 smb_node_end_crit(node); 510 return (-1); 511 } 512 513 if (SMB_TREE_SUPPORTS_CATIA(sr)) 514 flags |= SMB_CATIA; 515 516 rc = smb_fsop_remove(sr, sr->user_cr, node->dir_snode, 517 node->od_name, flags); 518 if (rc != 0) { 519 if (rc == ENOENT) 520 smb_delete_error(err, NT_STATUS_OBJECT_NAME_NOT_FOUND, 521 ERRDOS, ERROR_FILE_NOT_FOUND); 522 else 523 smbsr_map_errno(rc, err); 524 525 smb_node_end_crit(node); 526 return (-1); 527 } 528 529 smb_node_end_crit(node); 530 return (0); 531 } 532 533 534 /* 535 * smb_delete_check_path 536 * 537 * Perform initial validation on the pathname and last_comp. 538 * 539 * wildcards in path: 540 * Wildcards in the path (excluding the last_comp) should result 541 * in NT_STATUS_OBJECT_NAME_INVALID. 542 * 543 * bad path syntax: 544 * On unix .. at the root of a file system links to the root. Thus 545 * an attempt to lookup "/../../.." will be the same as looking up "/" 546 * CIFs clients expect the above to result in 547 * NT_STATUS_OBJECT_PATH_SYNTAX_BAD. It is currently not possible 548 * (and questionable if it's desirable) to deal with all cases 549 * but paths beginning with \\.. are handled. See bad_paths[]. 550 * Cases like "\\dir\\..\\.." will be caught and handled after the 551 * pnreduce. Cases like "\\dir\\..\\..\\filename" will still result 552 * in "\\filename" which is contrary to windows behavior. 553 * 554 * dot: 555 * A filename of '.' should result in NT_STATUS_OBJECT_NAME_INVALID 556 * Any wildcard filename that resolves to '.' should result in 557 * NT_STATUS_OBJECT_NAME_INVALID if the search attributes include 558 * FILE_ATTRIBUTE_DIRECTORY 559 * 560 * Returns: 561 * 0: path is valid. Sets *wildcard to TRUE if wildcard delete 562 * i.e. if wildcards in last component 563 * -1: path is invalid. Sets error information in sr. 564 */ 565 static int 566 smb_delete_check_path(smb_request_t *sr, boolean_t *wildcard) 567 { 568 smb_fqi_t *fqi = &sr->arg.dirop.fqi; 569 char *p, *last_comp; 570 int i, wildcards; 571 smb_pathname_t *pn = &fqi->fq_path; 572 573 struct { 574 char *name; 575 int len; 576 } *bad, bad_paths[] = { 577 {"\\..\0", 4}, 578 {"\\..\\", 4}, 579 {"..\0", 3}, 580 {"..\\", 3} 581 }; 582 583 /* find last component, strip trailing '\\' */ 584 p = pn->pn_path + strlen(pn->pn_path) - 1; 585 while (*p == '\\') { 586 *p = '\0'; 587 --p; 588 } 589 590 if ((p = strrchr(pn->pn_path, '\\')) == NULL) 591 last_comp = pn->pn_path; 592 else 593 last_comp = ++p; 594 595 wildcards = smb_convert_wildcards(last_comp); 596 597 if (last_comp != pn->pn_path) { 598 /* 599 * Wildcards are only allowed in the last component. 600 * Check for additional wildcards in the path. 601 */ 602 if (smb_convert_wildcards(pn->pn_path) != wildcards) { 603 smbsr_error(sr, NT_STATUS_OBJECT_NAME_INVALID, 604 ERRDOS, ERROR_INVALID_NAME); 605 return (-1); 606 } 607 } 608 609 /* path above the mount point */ 610 for (i = 0; i < sizeof (bad_paths) / sizeof (bad_paths[0]); ++i) { 611 bad = &bad_paths[i]; 612 if (strncmp(pn->pn_path, bad->name, bad->len) == 0) { 613 smbsr_error(sr, NT_STATUS_OBJECT_PATH_SYNTAX_BAD, 614 ERRDOS, ERROR_BAD_PATHNAME); 615 return (-1); 616 } 617 } 618 619 /* last component is, or resolves to, '.' (dot) */ 620 if ((strcmp(last_comp, ".") == 0) || 621 (SMB_SEARCH_DIRECTORY(fqi->fq_sattr) && 622 (smb_match(last_comp, ".")))) { 623 smbsr_error(sr, NT_STATUS_OBJECT_NAME_INVALID, 624 ERRDOS, ERROR_INVALID_NAME); 625 return (-1); 626 } 627 628 *wildcard = (wildcards != 0); 629 return (0); 630 } 631 632 /* 633 * smb_delete_error 634 */ 635 static void 636 smb_delete_error(smb_error_t *err, 637 uint32_t status, uint16_t errcls, uint16_t errcode) 638 { 639 err->severity = ERROR_SEVERITY_ERROR; 640 err->status = status; 641 err->errcls = errcls; 642 err->errcode = errcode; 643 } 644