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 27 /* 28 * smb_com_search 29 * smb_com_find, smb_com_find_close 30 * smb_find_unique 31 * 32 * These commands are used for directory searching. They share the same 33 * message formats, defined below: 34 * 35 * Client Request Description 36 * ---------------------------------- --------------------------------- 37 * 38 * UCHAR WordCount; Count of parameter words = 2 39 * USHORT MaxCount; Number of dir. entries to return 40 * USHORT SearchAttributes; 41 * USHORT ByteCount; Count of data bytes; min = 5 42 * UCHAR BufferFormat1; 0x04 -- ASCII 43 * UCHAR FileName[]; File name, may be null 44 * UCHAR BufferFormat2; 0x05 -- Variable block 45 * USHORT ResumeKeyLength; Length of resume key, may be 0 46 * UCHAR ResumeKey[]; Resume key 47 * 48 * FileName specifies the file to be sought. SearchAttributes indicates 49 * the attributes that the file must have. If SearchAttributes is 50 * zero then only normal files are returned. If the system file, hidden or 51 * directory attributes are specified then the search is inclusive - both the 52 * specified type(s) of files and normal files are returned. If the volume 53 * label attribute is specified then the search is exclusive, and only the 54 * volume label entry is returned. 55 * 56 * MaxCount specifies the number of directory entries to be returned. 57 * 58 * Server Response Description 59 * ---------------------------------- --------------------------------- 60 * 61 * UCHAR WordCount; Count of parameter words = 1 62 * USHORT Count; Number of entries returned 63 * USHORT ByteCount; Count of data bytes; min = 3 64 * UCHAR BufferFormat; 0x05 -- Variable block 65 * USHORT DataLength; Length of data 66 * UCHAR DirectoryInformationData[]; Data 67 * 68 * The response will contain one or more directory entries as determined by 69 * the Count field. No more than MaxCount entries will be returned. Only 70 * entries that match the sought FileName and SearchAttributes combination 71 * will be returned. 72 * 73 * ResumeKey must be null (length = 0) on the initial search request. 74 * Subsequent search requests intended to continue a search must contain 75 * the ResumeKey field extracted from the last directory entry of the 76 * previous response. ResumeKey is self-contained, for calls containing 77 * a non-zero ResumeKey neither the SearchAttributes or FileName fields 78 * will be valid in the request. ResumeKey has the following format: 79 * 80 * Resume Key Field Description 81 * ---------------------------------- --------------------------------- 82 * 83 * UCHAR Reserved; bit 7 - consumer use 84 * bits 5,6 - system use (must preserve) 85 * bits 0-4 - server use (must preserve) 86 * UCHAR FileName[11]; Name of the returned file 87 * UCHAR ReservedForServer[5]; Client must not modify 88 * byte 0 - uniquely identifies find 89 * through find_close 90 * bytes 1-4 - available for server use 91 * (must be non-zero) 92 * UCHAR ReservedForConsumer[4]; Server must not modify 93 * 94 * FileName is 8.3 format, with the three character extension left 95 * justified into FileName[9-11]. 96 * 97 * There may be multiple matching entries in response to a single request 98 * as wildcards are supported in the last component of FileName of the 99 * initial request. 100 * 101 * Returned directory entries in the DirectoryInformationData field of the 102 * response each have the following format: 103 * 104 * Directory Information Field Description 105 * ---------------------------------- --------------------------------- 106 * 107 * SMB_RESUME_KEY ResumeKey; Described above 108 * UCHAR FileAttributes; Attributes of the found file 109 * SMB_TIME LastWriteTime; Time file was last written 110 * SMB_DATE LastWriteDate; Date file was last written 111 * ULONG FileSize; Size of the file 112 * UCHAR FileName[13]; ASCII, space-filled null terminated 113 * 114 * FileName must conform to 8.3 rules, and is padded after the extension 115 * with 0x20 characters if necessary. 116 * 117 * As can be seen from the above structure, these commands cannot return 118 * long filenames, and cannot return UNICODE filenames. 119 * 120 * Files which have a size greater than 2^32 bytes should have the least 121 * significant 32 bits of their size returned in FileSize. 122 * 123 * smb_com_search 124 * -------------- 125 * 126 * If the client is prior to the LANMAN1.0 dialect, the returned FileName 127 * should be uppercased. 128 * If the client has negotiated a dialect prior to the LANMAN1.0 dialect, 129 * or if bit0 of the Flags2 SMB header field of the request is clear, 130 * the returned FileName should be uppercased. 131 * 132 * SMB_COM_SEARCH terminates when either the requested maximum number of 133 * entries that match the named file are found, or the end of directory is 134 * reached without the maximum number of matches being found. A response 135 * containing no entries indicates that no matching entries were found 136 * between the starting point of the search and the end of directory. 137 * 138 * 139 * The find, find_close and find_unique protocols may be used in place of 140 * the core "search" protocol when LANMAN 1.0 dialect has been negotiated. 141 * 142 * smb_com_find 143 * ------------ 144 * 145 * The find protocol is used to match the find OS/2 system call. 146 * 147 * The format of the find protocol is the same as the core "search" protocol. 148 * The difference is that the directory is logically Opened with a find protocol 149 * and logically closed with the find close protocol. 150 * As is true of a failing open, if a find request (find "first" request where 151 * resume_key is null) fails (no entries are found), no find close protocol is 152 * expected. 153 * 154 * If no global characters are present, a "find unique" protocol should be used 155 * (only one entry is expected and find close need not be sent). 156 * 157 * A find request will terminate when either the requested maximum number of 158 * entries that match the named file are found, or the end of directory is 159 * reached without the maximum number of matches being found. A response 160 * containing no entries indicates that no matching entries were found between 161 * the starting point of the search and the end of directory. 162 * 163 * If a find requests more data than can be placed in a message of the 164 * max-xmit-size for the TID specified, the server will return only the number 165 * of entries which will fit. 166 * 167 * 168 * smb_com_find_close 169 * ------------------ 170 * 171 * The find close protocol is used to match the find close OS/2 system call. 172 * 173 * Whereas the first find protocol logically opens the directory, subsequent 174 * find protocols presenting a resume_key further "read" the directory, the 175 * find close protocol "closes" the directory allowing the server to free any 176 * resources held in support of the directory search. 177 * 178 * In our implementation this translates to closing the odir. 179 * 180 * 181 * smb_com_find_unique 182 * ------------------- 183 * 184 * The format of the find unique protocol is the same as the core "search" 185 * protocol. The difference is that the directory is logically opened, any 186 * matching entries returned, and then the directory is logically closed. 187 * 188 * The resume search key key will be returned as in the find protocol and 189 * search protocol however it may NOT be returned to continue the search. 190 * Only one buffer of entries is expected and find close need not be sent. 191 * 192 * If a find unique requests more data than can be placed in a message of the 193 * max-xmit-size for the TID specified, the server will abort the virtual 194 * circuit to the consumer. 195 */ 196 197 #include <smbsrv/smb_incl.h> 198 199 /* *** smb_com_search *** */ 200 201 smb_sdrc_t 202 smb_pre_search(smb_request_t *sr) 203 { 204 DTRACE_SMB_1(op__Search__start, smb_request_t *, sr); 205 return (SDRC_SUCCESS); 206 } 207 208 void 209 smb_post_search(smb_request_t *sr) 210 { 211 DTRACE_SMB_1(op__Search__done, smb_request_t *, sr); 212 } 213 214 smb_sdrc_t 215 smb_com_search(smb_request_t *sr) 216 { 217 int rc; 218 uint16_t count, maxcount, index; 219 uint16_t sattr, odid; 220 uint16_t key_len; 221 uint32_t client_key; 222 char name[SMB_SHORTNAMELEN]; 223 char *path; 224 unsigned char resume_char; 225 unsigned char type; 226 boolean_t find_first, to_upper; 227 smb_tree_t *tree; 228 smb_odir_t *od; 229 smb_fileinfo_t fileinfo; 230 smb_odir_resume_t odir_resume; 231 boolean_t eos; 232 233 to_upper = B_FALSE; 234 if ((sr->session->dialect <= LANMAN1_0) || 235 ((sr->smb_flg2 & SMB_FLAGS2_KNOWS_LONG_NAMES) == 0)) { 236 to_upper = B_TRUE; 237 } 238 239 /* We only handle 8.3 name here */ 240 sr->smb_flg2 &= ~SMB_FLAGS2_KNOWS_LONG_NAMES; 241 sr->smb_flg &= ~SMB_FLAGS_CASE_INSENSITIVE; 242 243 if (smbsr_decode_vwv(sr, "ww", &maxcount, &sattr) != 0) 244 return (SDRC_ERROR); 245 246 rc = smbsr_decode_data(sr, "%Abw", sr, &path, &type, &key_len); 247 if ((rc != 0) || (type != 0x05)) 248 return (SDRC_ERROR); 249 250 tree = sr->tid_tree; 251 252 /* Volume information only */ 253 if ((sattr == FILE_ATTRIBUTE_VOLUME) && (key_len != 21)) { 254 (void) memset(name, ' ', sizeof (name)); 255 (void) strncpy(name, tree->t_volume, sizeof (name)); 256 257 if (key_len >= 21) { 258 (void) smb_mbc_decodef(&sr->smb_data, "17.l", 259 &client_key); 260 } else { 261 client_key = 0; 262 } 263 264 (void) smb_mbc_encodef(&sr->reply, "bwwbwb11c5.lb8.13c", 265 1, 0, VAR_BCC, 5, 0, 0, path+1, 266 client_key, sattr, name); 267 268 rc = (sr->reply.chain_offset - sr->cur_reply_offset) - 8; 269 (void) smb_mbc_poke(&sr->reply, sr->cur_reply_offset, "bwwbw", 270 1, 1, rc+3, 5, rc); 271 272 return (SDRC_SUCCESS); 273 } 274 275 if ((key_len != 0) && (key_len != 21)) 276 return (SDRC_ERROR); 277 278 find_first = (key_len == 0); 279 resume_char = 0; 280 client_key = 0; 281 282 if (find_first) { 283 /* NT interprets NULL filename as "\" */ 284 if (strlen(path) == 0) 285 path = "\\"; 286 287 odid = smb_odir_open(sr, path, sattr); 288 if (odid == 0) { 289 if (sr->smb_error.status == NT_STATUS_ACCESS_DENIED) 290 smbsr_warn(sr, NT_STATUS_NO_MORE_FILES, 291 ERRDOS, ERROR_NO_MORE_FILES); 292 return (SDRC_ERROR); 293 } 294 } else { 295 if (smb_mbc_decodef(&sr->smb_data, "b12.wwl", 296 &resume_char, &index, &odid, &client_key) != 0) { 297 return (SDRC_ERROR); 298 } 299 } 300 301 od = smb_tree_lookup_odir(sr->tid_tree, odid); 302 if (od == NULL) { 303 smbsr_error(sr, NT_STATUS_INVALID_HANDLE, 304 ERRDOS, ERROR_INVALID_HANDLE); 305 return (SDRC_ERROR); 306 } 307 308 if (!find_first) { 309 odir_resume.or_type = SMB_ODIR_RESUME_IDX; 310 odir_resume.or_idx = index; 311 smb_odir_resume_at(od, &odir_resume); 312 } 313 314 (void) smb_mbc_encodef(&sr->reply, "bwwbw", 1, 0, VAR_BCC, 5, 0); 315 316 rc = 0; 317 index = 0; 318 count = 0; 319 if (maxcount > SMB_MAX_SEARCH) 320 maxcount = SMB_MAX_SEARCH; 321 322 while (count < maxcount) { 323 rc = smb_odir_read_fileinfo(sr, od, &fileinfo, &eos); 324 if ((rc != 0 || (eos == B_TRUE))) 325 break; 326 327 if (smb_is_dot_or_dotdot(fileinfo.fi_name)) 328 continue; 329 330 if (*fileinfo.fi_shortname == NULL) { 331 (void) strlcpy(fileinfo.fi_shortname, 332 fileinfo.fi_name, SMB_SHORTNAMELEN - 1); 333 if (to_upper) 334 (void) utf8_strupr(fileinfo.fi_shortname); 335 } 336 337 (void) smb_mbc_encodef(&sr->reply, "b8c3c.wwlbYl13c", 338 resume_char, 339 fileinfo.fi_name83, fileinfo.fi_name83+9, 340 index, odid, client_key, 341 fileinfo.fi_dosattr & 0xff, 342 smb_gmt2local(sr, fileinfo.fi_mtime.tv_sec), 343 (int32_t)fileinfo.fi_size, 344 fileinfo.fi_shortname); 345 346 smb_odir_save_cookie(od, index, fileinfo.fi_cookie); 347 348 count++; 349 index++; 350 } 351 smb_odir_release(od); 352 353 if (rc != 0) { 354 smb_odir_close(od); 355 return (SDRC_ERROR); 356 } 357 358 if (count == 0 && find_first) { 359 smb_odir_close(od); 360 smbsr_warn(sr, NT_STATUS_NO_MORE_FILES, 361 ERRDOS, ERROR_NO_MORE_FILES); 362 return (SDRC_ERROR); 363 } 364 365 rc = (sr->reply.chain_offset - sr->cur_reply_offset) - 8; 366 if (smb_mbc_poke(&sr->reply, sr->cur_reply_offset, "bwwbw", 367 1, count, rc+3, 5, rc) < 0) { 368 smb_odir_close(od); 369 return (SDRC_ERROR); 370 } 371 372 return (SDRC_SUCCESS); 373 } 374 375 376 /* *** smb_com_find *** */ 377 378 smb_sdrc_t 379 smb_pre_find(smb_request_t *sr) 380 { 381 DTRACE_SMB_1(op__Find__start, smb_request_t *, sr); 382 return (SDRC_SUCCESS); 383 } 384 385 void 386 smb_post_find(smb_request_t *sr) 387 { 388 DTRACE_SMB_1(op__Find__done, smb_request_t *, sr); 389 } 390 391 smb_sdrc_t 392 smb_com_find(smb_request_t *sr) 393 { 394 int rc; 395 uint16_t count, maxcount, index; 396 uint16_t sattr, odid; 397 uint16_t key_len; 398 uint32_t client_key; 399 smb_odir_t *od; 400 smb_fileinfo_t fileinfo; 401 boolean_t eos; 402 403 char *path; 404 unsigned char resume_char; 405 unsigned char type; 406 boolean_t find_first = B_TRUE; 407 smb_odir_resume_t odir_resume; 408 409 if (smbsr_decode_vwv(sr, "ww", &maxcount, &sattr) != 0) 410 return (SDRC_ERROR); 411 412 rc = smbsr_decode_data(sr, "%Abw", sr, &path, &type, &key_len); 413 if ((rc != 0) || (type != 0x05)) 414 return (SDRC_ERROR); 415 416 if ((key_len != 0) && (key_len != 21)) 417 return (SDRC_ERROR); 418 419 find_first = (key_len == 0); 420 resume_char = 0; 421 client_key = 0; 422 423 if (find_first) { 424 odid = smb_odir_open(sr, path, sattr); 425 if (odid == 0) 426 return (SDRC_ERROR); 427 } else { 428 if (smb_mbc_decodef(&sr->smb_data, "b12.wwl", 429 &resume_char, &index, &odid, &client_key) != 0) { 430 return (SDRC_ERROR); 431 } 432 } 433 434 od = smb_tree_lookup_odir(sr->tid_tree, odid); 435 if (od == NULL) { 436 smbsr_error(sr, NT_STATUS_INVALID_HANDLE, 437 ERRDOS, ERROR_INVALID_HANDLE); 438 return (SDRC_ERROR); 439 } 440 441 if (!find_first) { 442 odir_resume.or_type = SMB_ODIR_RESUME_IDX; 443 odir_resume.or_idx = index; 444 smb_odir_resume_at(od, &odir_resume); 445 } 446 447 (void) smb_mbc_encodef(&sr->reply, "bwwbw", 1, 0, VAR_BCC, 5, 0); 448 449 rc = 0; 450 index = 0; 451 count = 0; 452 if (maxcount > SMB_MAX_SEARCH) 453 maxcount = SMB_MAX_SEARCH; 454 455 while (count < maxcount) { 456 rc = smb_odir_read_fileinfo(sr, od, &fileinfo, &eos); 457 if ((rc != 0 || (eos == B_TRUE))) 458 break; 459 460 if (*fileinfo.fi_shortname == NULL) { 461 (void) strlcpy(fileinfo.fi_shortname, 462 fileinfo.fi_name, SMB_SHORTNAMELEN - 1); 463 } 464 465 (void) smb_mbc_encodef(&sr->reply, "b8c3c.wwlbYl13c", 466 resume_char, 467 fileinfo.fi_name83, fileinfo.fi_name83+9, 468 index, odid, client_key, 469 fileinfo.fi_dosattr & 0xff, 470 smb_gmt2local(sr, fileinfo.fi_mtime.tv_sec), 471 (int32_t)fileinfo.fi_size, 472 fileinfo.fi_shortname); 473 474 smb_odir_save_cookie(od, index, fileinfo.fi_cookie); 475 476 count++; 477 index++; 478 } 479 smb_odir_release(od); 480 481 if (rc != 0) { 482 smb_odir_close(od); 483 return (SDRC_ERROR); 484 } 485 486 if (count == 0 && find_first) { 487 smb_odir_close(od); 488 smbsr_warn(sr, NT_STATUS_NO_MORE_FILES, 489 ERRDOS, ERROR_NO_MORE_FILES); 490 return (SDRC_ERROR); 491 } 492 493 rc = (MBC_LENGTH(&sr->reply) - sr->cur_reply_offset) - 8; 494 if (smb_mbc_poke(&sr->reply, sr->cur_reply_offset, "bwwbw", 495 1, count, rc+3, 5, rc) < 0) { 496 smb_odir_close(od); 497 return (SDRC_ERROR); 498 } 499 500 return (SDRC_SUCCESS); 501 } 502 503 504 /* *** smb_com_find_close *** */ 505 506 smb_sdrc_t 507 smb_pre_find_close(smb_request_t *sr) 508 { 509 DTRACE_SMB_1(op__FindClose__start, smb_request_t *, sr); 510 return (SDRC_SUCCESS); 511 } 512 513 void 514 smb_post_find_close(smb_request_t *sr) 515 { 516 DTRACE_SMB_1(op__FindClose__done, smb_request_t *, sr); 517 } 518 519 smb_sdrc_t 520 smb_com_find_close(smb_request_t *sr) 521 { 522 int rc; 523 uint16_t maxcount, index; 524 uint16_t sattr, odid; 525 uint16_t key_len; 526 uint32_t client_key; 527 char *path; 528 unsigned char resume_char; 529 unsigned char type; 530 smb_odir_t *od; 531 532 if (smbsr_decode_vwv(sr, "ww", &maxcount, &sattr) != 0) 533 return (SDRC_ERROR); 534 535 rc = smbsr_decode_data(sr, "%Abw", sr, &path, &type, &key_len); 536 if ((rc != 0) || (type != 0x05)) 537 return (SDRC_ERROR); 538 539 if (key_len == 0) { 540 smbsr_error(sr, NT_STATUS_INVALID_HANDLE, 541 ERRDOS, ERROR_INVALID_HANDLE); 542 return (SDRC_ERROR); 543 } else if (key_len != 21) { 544 return (SDRC_ERROR); 545 } 546 547 odid = 0; 548 if (smb_mbc_decodef(&sr->smb_data, "b12.wwl", 549 &resume_char, &index, &odid, &client_key) != 0) { 550 return (SDRC_ERROR); 551 } 552 553 od = smb_tree_lookup_odir(sr->tid_tree, odid); 554 if (od == NULL) { 555 smbsr_error(sr, NT_STATUS_INVALID_HANDLE, 556 ERRDOS, ERROR_INVALID_HANDLE); 557 return (SDRC_ERROR); 558 } 559 560 smb_odir_release(od); 561 smb_odir_close(od); 562 563 if (smbsr_encode_result(sr, 1, 3, "bwwbw", 1, 0, 3, 5, 0)) 564 return (SDRC_ERROR); 565 566 return (SDRC_SUCCESS); 567 } 568 569 570 /* *** smb_com_find_unique *** */ 571 572 smb_sdrc_t 573 smb_pre_find_unique(smb_request_t *sr) 574 { 575 DTRACE_SMB_1(op__FindUnique__start, smb_request_t *, sr); 576 return (SDRC_SUCCESS); 577 } 578 579 void 580 smb_post_find_unique(smb_request_t *sr) 581 { 582 DTRACE_SMB_1(op__FindUnique__done, smb_request_t *, sr); 583 } 584 585 smb_sdrc_t 586 smb_com_find_unique(struct smb_request *sr) 587 { 588 int rc; 589 uint16_t count, maxcount, index; 590 uint16_t sattr, odid; 591 char *path; 592 unsigned char resume_char = '\0'; 593 uint32_t client_key = 0; 594 smb_odir_t *od; 595 smb_fileinfo_t fileinfo; 596 boolean_t eos; 597 struct vardata_block *vdb; 598 599 if (smbsr_decode_vwv(sr, "ww", &maxcount, &sattr) != 0) 600 return (SDRC_ERROR); 601 602 vdb = kmem_alloc(sizeof (struct vardata_block), KM_SLEEP); 603 if ((smbsr_decode_data(sr, "%AV", sr, &path, vdb) != 0) || 604 (vdb->len != 0)) { 605 kmem_free(vdb, sizeof (struct vardata_block)); 606 return (SDRC_ERROR); 607 } 608 kmem_free(vdb, sizeof (struct vardata_block)); 609 610 (void) smb_mbc_encodef(&sr->reply, "bwwbw", 1, 0, VAR_BCC, 5, 0); 611 612 odid = smb_odir_open(sr, path, sattr); 613 if (odid == 0) 614 return (SDRC_ERROR); 615 od = smb_tree_lookup_odir(sr->tid_tree, odid); 616 if (od == NULL) 617 return (SDRC_ERROR); 618 619 rc = 0; 620 count = 0; 621 index = 0; 622 if (maxcount > SMB_MAX_SEARCH) 623 maxcount = SMB_MAX_SEARCH; 624 625 while (count < maxcount) { 626 rc = smb_odir_read_fileinfo(sr, od, &fileinfo, &eos); 627 if ((rc != 0 || (eos == B_TRUE))) 628 break; 629 630 if (*fileinfo.fi_shortname == NULL) { 631 (void) strlcpy(fileinfo.fi_shortname, 632 fileinfo.fi_name, SMB_SHORTNAMELEN - 1); 633 } 634 635 (void) smb_mbc_encodef(&sr->reply, "b8c3c.wwlbYl13c", 636 resume_char, 637 fileinfo.fi_name83, fileinfo.fi_name83+9, 638 index, odid, client_key, 639 fileinfo.fi_dosattr & 0xff, 640 smb_gmt2local(sr, fileinfo.fi_mtime.tv_sec), 641 (int32_t)fileinfo.fi_size, 642 fileinfo.fi_shortname); 643 644 count++; 645 index++; 646 } 647 648 smb_odir_release(od); 649 smb_odir_close(od); 650 651 if (rc != 0) 652 return (SDRC_ERROR); 653 654 if (count == 0) { 655 smbsr_warn(sr, NT_STATUS_NO_MORE_FILES, 656 ERRDOS, ERROR_NO_MORE_FILES); 657 return (SDRC_ERROR); 658 } 659 660 rc = (MBC_LENGTH(&sr->reply) - sr->cur_reply_offset) - 8; 661 if (smb_mbc_poke(&sr->reply, sr->cur_reply_offset, 662 "bwwbw", 1, count, rc+3, 5, rc) < 0) { 663 return (SDRC_ERROR); 664 } 665 666 return (SDRC_SUCCESS); 667 } 668