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 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <smbsrv/smb_incl.h> 29 30 31 /* 32 * smb_com_find 33 * 34 * Request Format: (same as core Search Protocol - "Find First" form) 35 * 36 * Client Request Description 37 * ================================== ================================= 38 * 39 * BYTE smb_wct; value = 2 40 * WORD smb_count; max number of entries to find 41 * WORD smb_attr; search attribute 42 * WORD smb_bcc; minimum value = 5 43 * BYTE smb_ident1; ASCII (04) 44 * BYTE smb_pathname[]; filename (may contain global characters) 45 * BYTE smb_ident2; Variable Block (05) 46 * WORD smb_keylen; resume key length (zero if "Find First") 47 * BYTE smb_resumekey[*]; "Find Next" key, * = value of smb_keylen 48 * 49 * Response Format: (same as core Search Protocol) 50 * 51 * Server Response Description 52 * ================================== ================================= 53 * BYTE smb_wct; value = 1 54 * WORD smb_count; number of entries found 55 * WORD smb_bcc; minimum value = 3 56 * BYTE smb_ident; Variable Block (05) 57 * WORD smb_datalen; data length 58 * BYTE smb_data[*]; directory entries 59 * 60 * Directory Information Entry (dir_info) Format: (same as core Search Protocol) 61 * 62 * BYTE find_buf_reserved[21]; reserved (resume_key) 63 * BYTE find_buf_attr; attribute 64 * WORD find_buf_time; modification time (hhhhh mmmmmm xxxxx) 65 * where 'xxxxx' is in 2 second increments 66 * WORD find_buf_date; modification date (yyyyyyy mmmm ddddd) 67 * DWORD find_buf_size; file size 68 * STRING find_buf_pname[13]; file name -- ASCII (null terminated) 69 * 70 * The resume_key has the following format: 71 * 72 * BYTE sr_res; reserved: 73 * bit 7 - reserved for consumer use 74 * bit 5,6 - reserved for system use 75 * (must be preserved) 76 * bits 0-4 - reserved for server 77 * (must be preserved) 78 * BYTE sr_name[11]; pathname sought. 79 * Format: 1-8 character file name, 80 * left justified 0-3 character extension, 81 * BYTE sr_findid[1]; uniquely identifies find through 82 * find_close 83 * BYTE sr_server[4]; available for server use 84 * (must be non-zero) 85 * BYTE sr_res[4]; reserved for consumer use 86 * 87 * Service: 88 * 89 * The Find protocol finds the directory entry or group of entries matching the 90 * specified file pathname. The filename portion of the pathname may contain 91 * global (wild card) characters. 92 * 93 * The Find protocol is used to match the find OS/2 system call. The protocols 94 * "Find", "Find_Unique" and "Find_Close" are methods of reading (or searching) 95 * a directory. These protocols may be used in place of the core "Search" 96 * protocol when LANMAN 1.0 dialect has been negotiated. There may be cases 97 * where the Search protocol will still be used. 98 * 99 * The format of the Find protocol is the same as the core "Search" protocol. 100 * The difference is that the directory is logically Opened with a Find protocol 101 * and logically closed with the Find Close protocol. This allows the Server to 102 * make better use of its resources. Search buffers are thus held (allowing 103 * search resumption via presenting a "resume_key") until a Find Close protocol 104 * is received. The sr_findid field of each resume key is a unique identifier 105 * (within the session) of the search from "Find" through "Find close". Thus if 106 * the consumer does "Find ahead", any find buffers containing resume keys with 107 * the matching find id may be released when the Find Close is requested. 108 * 109 * As is true of a failing open, if a Find request (Find "first" request where 110 * resume_key is null) fails (no entries are found), no find close protocol is 111 * expected. 112 * 113 * If no global characters are present, a "Find Unique" protocol should be used 114 * (only one entry is expected and find close need not be sent). 115 * 116 * The file path name in the request specifies the file to be sought. The 117 * attribute field indicates the attributes that the file must have. If the 118 * attribute is zero then only normal files are returned. If the system file, 119 * hidden or directory attributes are specified then the search is inclusive -- 120 * both the specified type(s) of files and normal files are returned. If the 121 * volume label attribute is specified then the search is exclusive, and only 122 * the volume label entry is returned 123 * 124 * The max-count field specifies the number of directory entries to be returned. 125 * The response will contain zero or more directory entries as determined by the 126 * count-returned field. No more than max-count entries will be returned. Only 127 * entries that match the sought filename/attribute will be returned. 128 * 129 * The resume_key field must be null (length = 0) on the initial ("Find First") 130 * find request. Subsequent find requests intended to continue a search must 131 * contain the resume_key field extracted from the last directory entry of the 132 * previous response. The resume_key field is self-contained, for on calls 133 * containing a resume_key neither the attribute or pathname fields will be 134 * valid in the request. A find request will terminate when either the 135 * requested maximum number of entries that match the named file are found, or 136 * the end of directory is reached without the maximum number of matches being 137 * found. A response containing no entries indicates that no matching entries 138 * were found between the starting point of the search and the end of directory. 139 * 140 * There may be multiple matching entries in response to a single request as 141 * Find supports "wild cards" in the file name (last component of the pathname). 142 * "?" is the wild single characters, "*" or "null" will match any number of 143 * filename characters within a single part of the filename component. The 144 * filename is divided into two parts -- an eight character name and a three 145 * character extension. The name and extension are divided by a ".". 146 * 147 * If a filename part commences with one or more "?"s then exactly that number 148 * of characters will be matched by the Wild Cards, e.g., "??x" will equal "abx" 149 * but not "abcx" or "ax". When a filename part has trailing "?"s then it will 150 * match the specified number of characters or less, e.g., "x??" will match 151 * "xab", "xa" and "x", but not "xabc". If only "?"s are present in the filename 152 * part, then it is handled as for trailing "?"s "*" or "null" match entire 153 * pathname parts, thus "*.abc" or ".abc" will match any file with an extension 154 * of "abc". "*.*", "*" or "null" will match all files in a directory. 155 * 156 * Unprotected servers require the requester to have read permission on the 157 * subtree containing the directory searched (the share specifies read 158 * permission). 159 * 160 * Protected servers require the requester to have permission to search the 161 * specified 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 * The number of entries returned will be the minimum of: 168 * 1. The number of entries requested. 169 * 2. The number of (complete) entries that will fit in the negotiated SMB 170 * buffer. 171 * 3. The number of entries that match the requested name pattern and 172 * attributes. 173 * 174 * The error ERRnofiles set in smb_err field of the response header or a zero 175 * value in smb_count of the response indicates no matching entry was found. 176 * 177 * The resume search key returned along with each directory entry is a server 178 * defined key which when returned in the Find Next protocol, allows the 179 * directory search to be resumed at the directory entry fol lowing the one 180 * denoted by the resume search key. 181 * 182 * The date is in the following format: 183 * bits: 184 * 1 1 1 1 1 1 185 * 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 186 * y y y y y y y m m m m d d d d d 187 * where: 188 * y - bit of year 0-119 (1980-2099) 189 * m - bit of month 1-12 190 * d - bit of day 1-31 191 * 192 * The time is in the following format: 193 * bits: 194 * 1 1 1 1 1 1 195 * 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 196 * h h h h h m m m m m m x x x x x 197 * where: 198 * h - bit of hour (0-23) 199 * m - bit of minute (0-59) 200 * x - bit of 2 second increment 201 * 202 * Find may generate the following errors. 203 * ERRDOS/ERRnofiles 204 * ERRDOS/ERRbadpath 205 * ERRDOS/ERRnoaccess 206 * ERRDOS/ERRbadaccess 207 * ERRDOS/ERRbadshare 208 * ERRSRV/ERRerror 209 * ERRSRV/ERRaccess 210 * ERRSRV/ERRinvnid 211 */ 212 smb_sdrc_t 213 smb_pre_find(smb_request_t *sr) 214 { 215 DTRACE_SMB_1(op__Find__start, smb_request_t *, sr); 216 return (SDRC_SUCCESS); 217 } 218 219 void 220 smb_post_find(smb_request_t *sr) 221 { 222 DTRACE_SMB_1(op__Find__done, smb_request_t *, sr); 223 } 224 225 smb_sdrc_t 226 smb_com_find(smb_request_t *sr) 227 { 228 int rc; 229 unsigned short sattr, count, maxcount; 230 char *path; 231 char filename[14]; 232 uint32_t cookie; 233 struct smb_node *node; 234 unsigned char type; 235 unsigned short key_len; 236 smb_odir_context_t *pc; 237 238 if (smbsr_decode_vwv(sr, "ww", &maxcount, &sattr) != 0) 239 return (SDRC_ERROR); 240 241 rc = smbsr_decode_data(sr, "%Abw", sr, &path, &type, &key_len); 242 if ((rc != 0) || (type != 0x05)) 243 return (SDRC_ERROR); 244 245 if (key_len == 0) { /* begin search */ 246 if (smb_rdir_open(sr, path, sattr) != 0) 247 return (SDRC_ERROR); 248 cookie = 0; 249 } else if (key_len == 21) { 250 sr->smb_sid = 0; 251 if (smb_mbc_decodef(&sr->smb_data, SMB_RESUME_KEY_FMT, 252 filename, &sr->smb_sid, &cookie) != 0) { 253 /* We don't know which rdir to close */ 254 return (SDRC_ERROR); 255 } 256 257 sr->sid_odir = smb_odir_lookup_by_sid(sr->tid_tree, 258 sr->smb_sid); 259 if (sr->sid_odir == NULL) { 260 smbsr_error(sr, NT_STATUS_INVALID_HANDLE, 261 ERRDOS, ERRbadfid); 262 return (SDRC_ERROR); 263 } 264 265 cookie--; /* +1 when returned */ 266 } else { 267 /* We don't know which rdir to close */ 268 return (SDRC_ERROR); 269 } 270 271 (void) smb_mbc_encodef(&sr->reply, "bwwbw", 1, 0, VAR_BCC, 5, 0); 272 273 pc = kmem_zalloc(sizeof (smb_odir_context_t), KM_SLEEP); 274 pc->dc_cookie = cookie; 275 count = 0; 276 node = NULL; 277 rc = 0; 278 279 while (count < maxcount) { 280 if ((rc = smb_rdir_next(sr, &node, pc)) != 0) 281 break; 282 283 (void) smb_mbc_encodef(&sr->reply, ".8c3cbl4.bYl13c", 284 pc->dc_name83, pc->dc_name83+9, sr->smb_sid, 285 pc->dc_cookie+1, pc->dc_dattr, 286 smb_gmt2local(sr, pc->dc_attr.sa_vattr.va_mtime.tv_sec), 287 (int32_t)smb_node_get_size(node, &pc->dc_attr), 288 (*pc->dc_shortname) ? pc->dc_shortname : pc->dc_name); 289 smb_node_release(node); 290 node = NULL; 291 count++; 292 } 293 294 kmem_free(pc, sizeof (smb_odir_context_t)); 295 296 if ((rc != 0) && (rc != ENOENT)) { 297 /* returned error by smb_rdir_next() */ 298 smb_rdir_close(sr); 299 smbsr_errno(sr, rc); 300 return (SDRC_ERROR); 301 } 302 303 if (count == 0) { 304 smb_rdir_close(sr); 305 smbsr_error(sr, 0, ERRDOS, ERRnofiles); 306 return (SDRC_ERROR); 307 } 308 309 rc = (MBC_LENGTH(&sr->reply) - sr->cur_reply_offset) - 8; 310 if (smb_mbc_poke(&sr->reply, sr->cur_reply_offset, "bwwbw", 311 1, count, rc+3, 5, rc) < 0) { 312 smb_rdir_close(sr); 313 return (SDRC_ERROR); 314 } 315 316 return (SDRC_SUCCESS); 317 } 318 319 /* 320 * smb_com_find_close 321 * 322 * Request Format: (same as core Search Protocol - "Find Next" form) 323 * 324 * Client Request Description 325 * ================================== ================================= 326 * 327 * BYTE smb_wct; value = 2 328 * WORD smb_count; max number of entries to find 329 * WORD smb_attr; search attribute 330 * WORD smb_bcc; minimum value = 5 331 * BYTE smb_ident1; ASCII (04) 332 * BYTE smb_pathname[]; null (may contain only null) 333 * BYTE smb_ident2; Variable Block (05) 334 * WORD smb_keylen; resume (close) key length 335 * (may not be zero) 336 * BYTE smb_resumekey[*]; "Find Close" key 337 * (* = value of smb_keylen) 338 * 339 * Response Format: (same format as core Search Protocol) 340 * 341 * Server Response Description 342 * ================================== ================================= 343 * 344 * BYTE smb_wct; value = 1 345 * WORD smb_reserved; reserved 346 * WORD smb_bcc; value = 3 347 * BYTE smb_ident; Variable Block (05) 348 * WORD smb_datalen; data length (value = 0) 349 * 350 * The resume_key (or close key) has the following format: 351 * 352 * BYTE sr_res; reserved: 353 * bit 7 - reserved for consumer use 354 * bit 5,6 - reserved for system use 355 * (must be preserved) 356 * bits 0-4 - rsvd for server 357 * (must be preserved by consumer) 358 * BYTE sr_name[11]; pathname sought. 359 * Format: 1-8 character file name, 360 * left justified 0-3 character extension, 361 * left justified (in last 3 chars) 362 * BYTE sr_findid[1]; uniquely identifies find 363 * through find_close 364 * BYTE sr_server[4]; available for server use 365 * (must be non-zero) 366 * BYTE sr_res[4]; reserved for consumer use 367 * 368 * Service: 369 * 370 * The Find_Close protocol closes the association between a Find id 371 * returned (in the resume_key) by the Find protocol and the directory 372 * search. 373 * 374 * Whereas the First Find protocol logically opens the directory, 375 * subsequent find protocols presenting a resume_key further "read" the 376 * directory, the Find Close protocol "closes" the directory allowing the 377 * server to free any resources held in support of the directory search. 378 * 379 * The Find Close protocol is used to match the find Close OS/2 380 * system call. The protocols "Find", "Find Unique" and "Find Close" are 381 * methods of reading (or searching) a directory. These protocols may 382 * be used in place of the core "Search" protocol when LANMAN 1.0 dialect has 383 * been negotiated. There may be cases where the Search protocol will still be 384 * used. 385 * 386 * Although only the find id portion the resume key should be 387 * required to identify the search being ter minated, the entire 388 * resume_key as returned in the previous Find, either a "Find First" or "Find 389 * Next" is sent to the server in this protocol. 390 * 391 * Find Close may generate the following errors: 392 * 393 * ERRDOS/ERRbadfid 394 * ERRSRV/ERRerror 395 * ERRSRV/ERRinvnid 396 */ 397 smb_sdrc_t 398 smb_pre_find_close(smb_request_t *sr) 399 { 400 DTRACE_SMB_1(op__FindClose__start, smb_request_t *, sr); 401 return (SDRC_SUCCESS); 402 } 403 404 void 405 smb_post_find_close(smb_request_t *sr) 406 { 407 DTRACE_SMB_1(op__FindClose__done, smb_request_t *, sr); 408 } 409 410 smb_sdrc_t 411 smb_com_find_close(smb_request_t *sr) 412 { 413 unsigned short sattr, maxcount; 414 char *path; 415 char filename[14]; 416 uint32_t cookie; 417 unsigned char type; 418 unsigned short key_len; 419 int rc; 420 421 if (smbsr_decode_vwv(sr, "ww", &maxcount, &sattr) != 0) 422 return (SDRC_ERROR); 423 424 rc = smbsr_decode_data(sr, "%Abw", sr, &path, &type, &key_len); 425 if ((rc != 0) || (type != 0x05)) 426 return (SDRC_ERROR); 427 428 if (key_len == 0) { /* begin search */ 429 smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid); 430 return (SDRC_ERROR); 431 } 432 433 if (key_len == 21) { 434 sr->smb_sid = 0; 435 if (smb_mbc_decodef(&sr->smb_data, SMB_RESUME_KEY_FMT, 436 filename, &sr->smb_sid, &cookie) != 0) { 437 return (SDRC_ERROR); 438 } 439 440 sr->sid_odir = smb_odir_lookup_by_sid(sr->tid_tree, 441 sr->smb_sid); 442 if (sr->sid_odir == NULL) { 443 smbsr_error(sr, NT_STATUS_INVALID_HANDLE, 444 ERRDOS, ERRbadfid); 445 return (SDRC_ERROR); 446 } 447 448 cookie--; /* +1 when returned */ 449 } else { 450 return (SDRC_ERROR); 451 } 452 453 smb_rdir_close(sr); 454 if (smbsr_encode_result(sr, 1, 3, "bwwbw", 1, 0, 3, 5, 0)) 455 return (SDRC_ERROR); 456 return (SDRC_SUCCESS); 457 } 458