xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_find.c (revision b1d7ec75953cd517f5b7c3d9cb427ff8ec5d7d07)
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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #include <smbsrv/smb_kproto.h>
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 #define	SMB_NAME83_BUFLEN	12
198 static void smb_name83(const char *, char *, size_t);
199 
200 /* *** smb_com_search *** */
201 
202 smb_sdrc_t
203 smb_pre_search(smb_request_t *sr)
204 {
205 	DTRACE_SMB_1(op__Search__start, smb_request_t *, sr);
206 	return (SDRC_SUCCESS);
207 }
208 
209 void
210 smb_post_search(smb_request_t *sr)
211 {
212 	DTRACE_SMB_1(op__Search__done, smb_request_t *, sr);
213 }
214 
215 smb_sdrc_t
216 smb_com_search(smb_request_t *sr)
217 {
218 	int			rc;
219 	uint16_t		count, maxcount, index;
220 	uint16_t		sattr, odid;
221 	uint16_t		key_len;
222 	uint32_t		client_key;
223 	char			name[SMB_SHORTNAMELEN];
224 	char			name83[SMB_SHORTNAMELEN];
225 	smb_pathname_t		*pn;
226 	unsigned char		resume_char;
227 	unsigned char		type;
228 	boolean_t		find_first, to_upper;
229 	smb_tree_t		*tree;
230 	smb_odir_t		*od;
231 	smb_fileinfo_t		fileinfo;
232 	smb_odir_resume_t	odir_resume;
233 	boolean_t		eos;
234 
235 	to_upper = B_FALSE;
236 	if ((sr->session->dialect <= LANMAN1_0) ||
237 	    ((sr->smb_flg2 & SMB_FLAGS2_KNOWS_LONG_NAMES) == 0)) {
238 		to_upper = B_TRUE;
239 	}
240 
241 	/* We only handle 8.3 name here */
242 	sr->smb_flg2 &= ~SMB_FLAGS2_KNOWS_LONG_NAMES;
243 	sr->smb_flg &= ~SMB_FLAGS_CASE_INSENSITIVE;
244 
245 	if (smbsr_decode_vwv(sr, "ww", &maxcount, &sattr) != 0)
246 		return (SDRC_ERROR);
247 
248 	pn = &sr->arg.dirop.fqi.fq_path;
249 	rc = smbsr_decode_data(sr, "%Abw", sr, &pn->pn_path, &type, &key_len);
250 	if ((rc != 0) || (type != 0x05))
251 		return (SDRC_ERROR);
252 
253 	smb_pathname_init(sr, pn, pn->pn_path);
254 	if (!smb_pathname_validate(sr, pn) ||
255 	    smb_is_stream_name(pn->pn_path)) {
256 		smbsr_warn(sr, NT_STATUS_NO_MORE_FILES,
257 		    ERRDOS, ERROR_NO_MORE_FILES);
258 		return (SDRC_ERROR);
259 	}
260 
261 	tree = sr->tid_tree;
262 
263 	/* Volume information only */
264 	if ((sattr == FILE_ATTRIBUTE_VOLUME) && (key_len != 21)) {
265 		(void) memset(name, ' ', sizeof (name));
266 		(void) strncpy(name, tree->t_volume, sizeof (name));
267 
268 		if (key_len >= 21) {
269 			(void) smb_mbc_decodef(&sr->smb_data, "17.l",
270 			    &client_key);
271 		} else {
272 			client_key = 0;
273 		}
274 
275 		(void) smb_mbc_encodef(&sr->reply, "bwwbwb11c5.lb8.13c",
276 		    1, 0, VAR_BCC, 5, 0, 0, pn->pn_path+1,
277 		    client_key, sattr, name);
278 
279 		rc = (sr->reply.chain_offset - sr->cur_reply_offset) - 8;
280 		(void) smb_mbc_poke(&sr->reply, sr->cur_reply_offset, "bwwbw",
281 		    1, 1, rc+3, 5, rc);
282 
283 		return (SDRC_SUCCESS);
284 	}
285 
286 	if ((key_len != 0) && (key_len != 21))
287 		return (SDRC_ERROR);
288 
289 	find_first = (key_len == 0);
290 	resume_char = 0;
291 	client_key = 0;
292 
293 	if (find_first) {
294 		odid = smb_odir_open(sr, pn->pn_path, sattr, 0);
295 		if (odid == 0) {
296 			if (sr->smb_error.status == NT_STATUS_ACCESS_DENIED)
297 				smbsr_warn(sr, NT_STATUS_NO_MORE_FILES,
298 				    ERRDOS, ERROR_NO_MORE_FILES);
299 			return (SDRC_ERROR);
300 		}
301 	} else {
302 		if (smb_mbc_decodef(&sr->smb_data, "b12.wwl",
303 		    &resume_char, &index, &odid, &client_key) != 0) {
304 			return (SDRC_ERROR);
305 		}
306 	}
307 
308 	od = smb_tree_lookup_odir(sr->tid_tree, odid);
309 	if (od == NULL) {
310 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
311 		    ERRDOS, ERROR_INVALID_HANDLE);
312 		return (SDRC_ERROR);
313 	}
314 
315 	if (!find_first) {
316 		odir_resume.or_type = SMB_ODIR_RESUME_IDX;
317 		odir_resume.or_idx = index;
318 		smb_odir_resume_at(od, &odir_resume);
319 	}
320 
321 	(void) smb_mbc_encodef(&sr->reply, "bwwbw", 1, 0, VAR_BCC, 5, 0);
322 
323 	rc = 0;
324 	index = 0;
325 	count = 0;
326 	if (maxcount > SMB_MAX_SEARCH)
327 		maxcount = SMB_MAX_SEARCH;
328 
329 	while (count < maxcount) {
330 		rc = smb_odir_read_fileinfo(sr, od, &fileinfo, &eos);
331 		if ((rc != 0 || (eos == B_TRUE)))
332 			break;
333 
334 		if (*fileinfo.fi_shortname == '\0') {
335 			(void) strlcpy(fileinfo.fi_shortname, fileinfo.fi_name,
336 			    SMB_SHORTNAMELEN - 1);
337 			if (to_upper)
338 				(void) smb_strupr(fileinfo.fi_shortname);
339 		}
340 		smb_name83(fileinfo.fi_shortname, name83, SMB_SHORTNAMELEN);
341 
342 		(void) smb_mbc_encodef(&sr->reply, "b11c.wwlbYl13c",
343 		    resume_char, name83, index, odid, client_key,
344 		    fileinfo.fi_dosattr & 0xff,
345 		    smb_time_gmt_to_local(sr, fileinfo.fi_mtime.tv_sec),
346 		    (int32_t)fileinfo.fi_size,
347 		    fileinfo.fi_shortname);
348 
349 		smb_odir_save_cookie(od, index, fileinfo.fi_cookie);
350 
351 		count++;
352 		index++;
353 	}
354 
355 	if (rc != 0) {
356 		smb_odir_close(od);
357 		smb_odir_release(od);
358 		return (SDRC_ERROR);
359 	}
360 
361 	if (count == 0 && find_first) {
362 		smb_odir_close(od);
363 		smb_odir_release(od);
364 		smbsr_warn(sr, NT_STATUS_NO_MORE_FILES,
365 		    ERRDOS, ERROR_NO_MORE_FILES);
366 		return (SDRC_ERROR);
367 	}
368 
369 	rc = (sr->reply.chain_offset - sr->cur_reply_offset) - 8;
370 	if (smb_mbc_poke(&sr->reply, sr->cur_reply_offset, "bwwbw",
371 	    1, count, rc+3, 5, rc) < 0) {
372 		smb_odir_close(od);
373 		smb_odir_release(od);
374 		return (SDRC_ERROR);
375 	}
376 
377 	smb_odir_release(od);
378 	return (SDRC_SUCCESS);
379 }
380 
381 
382 /* *** smb_com_find *** */
383 
384 smb_sdrc_t
385 smb_pre_find(smb_request_t *sr)
386 {
387 	DTRACE_SMB_1(op__Find__start, smb_request_t *, sr);
388 	return (SDRC_SUCCESS);
389 }
390 
391 void
392 smb_post_find(smb_request_t *sr)
393 {
394 	DTRACE_SMB_1(op__Find__done, smb_request_t *, sr);
395 }
396 
397 smb_sdrc_t
398 smb_com_find(smb_request_t *sr)
399 {
400 	int			rc;
401 	uint16_t		count, maxcount, index;
402 	uint16_t		sattr, odid;
403 	uint16_t		key_len;
404 	uint32_t		client_key;
405 	char			name83[SMB_SHORTNAMELEN];
406 	smb_odir_t		*od;
407 	smb_fileinfo_t		fileinfo;
408 	boolean_t		eos;
409 
410 	smb_pathname_t		*pn;
411 	unsigned char		resume_char;
412 	unsigned char		type;
413 	boolean_t		find_first = B_TRUE;
414 	smb_odir_resume_t	odir_resume;
415 
416 	if (smbsr_decode_vwv(sr, "ww", &maxcount, &sattr) != 0)
417 		return (SDRC_ERROR);
418 
419 	pn = &sr->arg.dirop.fqi.fq_path;
420 	rc = smbsr_decode_data(sr, "%Abw", sr, &pn->pn_path, &type, &key_len);
421 	if ((rc != 0) || (type != 0x05))
422 		return (SDRC_ERROR);
423 
424 	if ((key_len != 0) && (key_len != 21))
425 		return (SDRC_ERROR);
426 
427 	smb_pathname_init(sr, pn, pn->pn_path);
428 	if (!smb_pathname_validate(sr, pn))
429 		return (SDRC_ERROR);
430 
431 	if (smb_is_stream_name(pn->pn_path)) {
432 		smbsr_error(sr, NT_STATUS_OBJECT_NAME_INVALID,
433 		    ERRDOS, ERROR_INVALID_NAME);
434 		return (SDRC_ERROR);
435 	}
436 
437 	find_first = (key_len == 0);
438 	resume_char = 0;
439 	client_key = 0;
440 
441 	if (find_first) {
442 		odid = smb_odir_open(sr, pn->pn_path, sattr, 0);
443 		if (odid == 0)
444 			return (SDRC_ERROR);
445 	} else {
446 		if (smb_mbc_decodef(&sr->smb_data, "b12.wwl",
447 		    &resume_char, &index, &odid, &client_key) != 0) {
448 			return (SDRC_ERROR);
449 		}
450 	}
451 
452 	od = smb_tree_lookup_odir(sr->tid_tree, odid);
453 	if (od == NULL) {
454 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
455 		    ERRDOS, ERROR_INVALID_HANDLE);
456 		return (SDRC_ERROR);
457 	}
458 
459 	if (!find_first) {
460 		odir_resume.or_type = SMB_ODIR_RESUME_IDX;
461 		odir_resume.or_idx = index;
462 		smb_odir_resume_at(od, &odir_resume);
463 	}
464 
465 	(void) smb_mbc_encodef(&sr->reply, "bwwbw", 1, 0, VAR_BCC, 5, 0);
466 
467 	rc = 0;
468 	index = 0;
469 	count = 0;
470 	if (maxcount > SMB_MAX_SEARCH)
471 		maxcount = SMB_MAX_SEARCH;
472 
473 	while (count < maxcount) {
474 		rc = smb_odir_read_fileinfo(sr, od, &fileinfo, &eos);
475 		if ((rc != 0 || (eos == B_TRUE)))
476 			break;
477 
478 		if (*fileinfo.fi_shortname == '\0') {
479 			(void) strlcpy(fileinfo.fi_shortname, fileinfo.fi_name,
480 			    SMB_SHORTNAMELEN - 1);
481 		}
482 		smb_name83(fileinfo.fi_shortname, name83, SMB_SHORTNAMELEN);
483 
484 		(void) smb_mbc_encodef(&sr->reply, "b11c.wwlbYl13c",
485 		    resume_char, name83, index, odid, client_key,
486 		    fileinfo.fi_dosattr & 0xff,
487 		    smb_time_gmt_to_local(sr, fileinfo.fi_mtime.tv_sec),
488 		    (int32_t)fileinfo.fi_size,
489 		    fileinfo.fi_shortname);
490 
491 		smb_odir_save_cookie(od, index, fileinfo.fi_cookie);
492 
493 		count++;
494 		index++;
495 	}
496 
497 	if (rc != 0) {
498 		smb_odir_close(od);
499 		smb_odir_release(od);
500 		return (SDRC_ERROR);
501 	}
502 
503 	if (count == 0 && find_first) {
504 		smb_odir_close(od);
505 		smb_odir_release(od);
506 		smbsr_warn(sr, NT_STATUS_NO_MORE_FILES,
507 		    ERRDOS, ERROR_NO_MORE_FILES);
508 		return (SDRC_ERROR);
509 	}
510 
511 	rc = (MBC_LENGTH(&sr->reply) - sr->cur_reply_offset) - 8;
512 	if (smb_mbc_poke(&sr->reply, sr->cur_reply_offset, "bwwbw",
513 	    1, count, rc+3, 5, rc) < 0) {
514 		smb_odir_close(od);
515 		smb_odir_release(od);
516 		return (SDRC_ERROR);
517 	}
518 
519 	smb_odir_release(od);
520 	return (SDRC_SUCCESS);
521 }
522 
523 
524 /* *** smb_com_find_close *** */
525 
526 smb_sdrc_t
527 smb_pre_find_close(smb_request_t *sr)
528 {
529 	DTRACE_SMB_1(op__FindClose__start, smb_request_t *, sr);
530 	return (SDRC_SUCCESS);
531 }
532 
533 void
534 smb_post_find_close(smb_request_t *sr)
535 {
536 	DTRACE_SMB_1(op__FindClose__done, smb_request_t *, sr);
537 }
538 
539 smb_sdrc_t
540 smb_com_find_close(smb_request_t *sr)
541 {
542 	int		rc;
543 	uint16_t	maxcount, index;
544 	uint16_t	sattr, odid;
545 	uint16_t	key_len;
546 	uint32_t	client_key;
547 	char		*path;
548 	unsigned char	resume_char;
549 	unsigned char	type;
550 	smb_odir_t	*od;
551 
552 	if (smbsr_decode_vwv(sr, "ww", &maxcount, &sattr) != 0)
553 		return (SDRC_ERROR);
554 
555 	rc = smbsr_decode_data(sr, "%Abw", sr, &path, &type, &key_len);
556 	if ((rc != 0) || (type != 0x05))
557 		return (SDRC_ERROR);
558 
559 	if (key_len == 0) {
560 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
561 		    ERRDOS, ERROR_INVALID_HANDLE);
562 		return (SDRC_ERROR);
563 	} else if (key_len != 21) {
564 		return (SDRC_ERROR);
565 	}
566 
567 	odid = 0;
568 	if (smb_mbc_decodef(&sr->smb_data, "b12.wwl",
569 	    &resume_char, &index, &odid, &client_key) != 0) {
570 		return (SDRC_ERROR);
571 	}
572 
573 	od = smb_tree_lookup_odir(sr->tid_tree, odid);
574 	if (od == NULL) {
575 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
576 		    ERRDOS, ERROR_INVALID_HANDLE);
577 		return (SDRC_ERROR);
578 	}
579 
580 	smb_odir_close(od);
581 	smb_odir_release(od);
582 
583 	if (smbsr_encode_result(sr, 1, 3, "bwwbw", 1, 0, 3, 5, 0))
584 		return (SDRC_ERROR);
585 
586 	return (SDRC_SUCCESS);
587 }
588 
589 
590 /* *** smb_com_find_unique *** */
591 
592 smb_sdrc_t
593 smb_pre_find_unique(smb_request_t *sr)
594 {
595 	DTRACE_SMB_1(op__FindUnique__start, smb_request_t *, sr);
596 	return (SDRC_SUCCESS);
597 }
598 
599 void
600 smb_post_find_unique(smb_request_t *sr)
601 {
602 	DTRACE_SMB_1(op__FindUnique__done, smb_request_t *, sr);
603 }
604 
605 smb_sdrc_t
606 smb_com_find_unique(struct smb_request *sr)
607 {
608 	int			rc;
609 	uint16_t		count, maxcount, index;
610 	uint16_t		sattr, odid;
611 	smb_pathname_t		*pn;
612 	unsigned char		resume_char = '\0';
613 	uint32_t		client_key = 0;
614 	char			name83[SMB_SHORTNAMELEN];
615 	smb_odir_t		*od;
616 	smb_fileinfo_t		fileinfo;
617 	boolean_t		eos;
618 	smb_vdb_t		*vdb;
619 
620 	if (smbsr_decode_vwv(sr, "ww", &maxcount, &sattr) != 0)
621 		return (SDRC_ERROR);
622 
623 	pn = &sr->arg.dirop.fqi.fq_path;
624 	vdb = kmem_alloc(sizeof (smb_vdb_t), KM_SLEEP);
625 	if ((smbsr_decode_data(sr, "%AV", sr, &pn->pn_path, vdb) != 0) ||
626 	    (vdb->vdb_len != 0)) {
627 		kmem_free(vdb, sizeof (smb_vdb_t));
628 		return (SDRC_ERROR);
629 	}
630 	kmem_free(vdb, sizeof (smb_vdb_t));
631 
632 	smb_pathname_init(sr, pn, pn->pn_path);
633 	if (!smb_pathname_validate(sr, pn))
634 		return (SDRC_ERROR);
635 
636 	if (smb_is_stream_name(pn->pn_path)) {
637 		smbsr_error(sr, NT_STATUS_OBJECT_NAME_INVALID,
638 		    ERRDOS, ERROR_INVALID_NAME);
639 		return (SDRC_ERROR);
640 	}
641 
642 	(void) smb_mbc_encodef(&sr->reply, "bwwbw", 1, 0, VAR_BCC, 5, 0);
643 
644 	odid = smb_odir_open(sr, pn->pn_path, sattr, 0);
645 	if (odid == 0)
646 		return (SDRC_ERROR);
647 	od = smb_tree_lookup_odir(sr->tid_tree, odid);
648 	if (od == NULL)
649 		return (SDRC_ERROR);
650 
651 	rc = 0;
652 	count = 0;
653 	index = 0;
654 	if (maxcount > SMB_MAX_SEARCH)
655 		maxcount = SMB_MAX_SEARCH;
656 
657 	while (count < maxcount) {
658 		rc = smb_odir_read_fileinfo(sr, od, &fileinfo, &eos);
659 		if ((rc != 0 || (eos == B_TRUE)))
660 			break;
661 
662 		if (*fileinfo.fi_shortname == '\0') {
663 			(void) strlcpy(fileinfo.fi_shortname, fileinfo.fi_name,
664 			    SMB_SHORTNAMELEN - 1);
665 		}
666 		smb_name83(fileinfo.fi_shortname, name83, SMB_SHORTNAMELEN);
667 
668 		(void) smb_mbc_encodef(&sr->reply, "b11c.wwlbYl13c",
669 		    resume_char, name83, index, odid, client_key,
670 		    fileinfo.fi_dosattr & 0xff,
671 		    smb_time_gmt_to_local(sr, fileinfo.fi_mtime.tv_sec),
672 		    (int32_t)fileinfo.fi_size,
673 		    fileinfo.fi_shortname);
674 
675 		count++;
676 		index++;
677 	}
678 
679 	smb_odir_close(od);
680 	smb_odir_release(od);
681 
682 	if (rc != 0)
683 		return (SDRC_ERROR);
684 
685 	if (count == 0) {
686 		smbsr_warn(sr, NT_STATUS_NO_MORE_FILES,
687 		    ERRDOS, ERROR_NO_MORE_FILES);
688 		return (SDRC_ERROR);
689 	}
690 
691 	rc = (MBC_LENGTH(&sr->reply) - sr->cur_reply_offset) - 8;
692 	if (smb_mbc_poke(&sr->reply, sr->cur_reply_offset,
693 	    "bwwbw", 1, count, rc+3, 5, rc) < 0) {
694 		return (SDRC_ERROR);
695 	}
696 
697 	return (SDRC_SUCCESS);
698 }
699 
700 /*
701  * smb_name83
702  *
703  * Format the filename for inclusion in the resume key. The filename
704  * returned in the resume key is 11 bytes:
705  * - up to 8 bytes of filename, space padded to 8 bytes
706  * - up to 3 bytes of ext, space padded to 3 bytes
707  *
708  * The name passed to smb_name83 should be a shortname or a name that
709  * doesn't require mangling.
710  *
711  * Examples:
712  *	"fname.txt"    -> "FNAME   TXT"
713  *	"fname.tx"     -> "FNAME   TX "
714  *	"filename"     -> "FILENAME   "
715  *	"filename.txt" -> "FILENAMETXT"
716  *	"FILE~1.TXT"   -> "FILE~1  TXT"
717  */
718 static void
719 smb_name83(const char *name, char *buf, size_t buflen)
720 {
721 	const char *p;
722 	char *pbuf;
723 	int i;
724 
725 	ASSERT(name && buf && (buflen >= SMB_NAME83_BUFLEN));
726 
727 	(void) strlcpy(buf, "           ", SMB_NAME83_BUFLEN);
728 
729 	/* Process "." and ".." up front */
730 	if ((strcmp(name, ".") == 0) || (strcmp(name, "..") == 0)) {
731 		(void) strncpy(buf, name, strlen(name));
732 		return;
733 	}
734 
735 	ASSERT(smb_needs_mangled(name) == B_FALSE);
736 
737 	/* Process basename */
738 	for (i = 0, p = name, pbuf = buf;
739 	    (i < SMB_NAME83_BASELEN) && (*p != '\0') && (*p != '.'); ++i)
740 		*pbuf++ = *p++;
741 
742 	/* Process the extension from the last dot in name */
743 	if ((p = strchr(name, '.')) != NULL) {
744 		++p;
745 		pbuf = &buf[SMB_NAME83_BASELEN];
746 		for (i = 0; (i < SMB_NAME83_EXTLEN) && (*p != '\0'); ++i)
747 			*pbuf++ = *p++;
748 	}
749 
750 	(void) smb_strupr(buf);
751 }
752