xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_find.c (revision e8d80663e4f91871f843bb8ad9108dc0b76dfcf3)
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 			if (smb_needs_mangled(fileinfo.fi_name))
336 				continue;
337 			(void) strlcpy(fileinfo.fi_shortname, fileinfo.fi_name,
338 			    SMB_SHORTNAMELEN - 1);
339 			if (to_upper)
340 				(void) smb_strupr(fileinfo.fi_shortname);
341 		}
342 		smb_name83(fileinfo.fi_shortname, name83, SMB_SHORTNAMELEN);
343 
344 		(void) smb_mbc_encodef(&sr->reply, "b11c.wwlbYl13c",
345 		    resume_char, name83, index, odid, client_key,
346 		    fileinfo.fi_dosattr & 0xff,
347 		    smb_time_gmt_to_local(sr, fileinfo.fi_mtime.tv_sec),
348 		    (int32_t)fileinfo.fi_size,
349 		    fileinfo.fi_shortname);
350 
351 		smb_odir_save_cookie(od, index, fileinfo.fi_cookie);
352 
353 		count++;
354 		index++;
355 	}
356 
357 	if (rc != 0) {
358 		smb_odir_close(od);
359 		smb_odir_release(od);
360 		return (SDRC_ERROR);
361 	}
362 
363 	if (count == 0 && find_first) {
364 		smb_odir_close(od);
365 		smb_odir_release(od);
366 		smbsr_warn(sr, NT_STATUS_NO_MORE_FILES,
367 		    ERRDOS, ERROR_NO_MORE_FILES);
368 		return (SDRC_ERROR);
369 	}
370 
371 	rc = (sr->reply.chain_offset - sr->cur_reply_offset) - 8;
372 	if (smb_mbc_poke(&sr->reply, sr->cur_reply_offset, "bwwbw",
373 	    1, count, rc+3, 5, rc) < 0) {
374 		smb_odir_close(od);
375 		smb_odir_release(od);
376 		return (SDRC_ERROR);
377 	}
378 
379 	smb_odir_release(od);
380 	return (SDRC_SUCCESS);
381 }
382 
383 
384 /* *** smb_com_find *** */
385 
386 smb_sdrc_t
387 smb_pre_find(smb_request_t *sr)
388 {
389 	DTRACE_SMB_1(op__Find__start, smb_request_t *, sr);
390 	return (SDRC_SUCCESS);
391 }
392 
393 void
394 smb_post_find(smb_request_t *sr)
395 {
396 	DTRACE_SMB_1(op__Find__done, smb_request_t *, sr);
397 }
398 
399 smb_sdrc_t
400 smb_com_find(smb_request_t *sr)
401 {
402 	int			rc;
403 	uint16_t		count, maxcount, index;
404 	uint16_t		sattr, odid;
405 	uint16_t		key_len;
406 	uint32_t		client_key;
407 	char			name83[SMB_SHORTNAMELEN];
408 	smb_odir_t		*od;
409 	smb_fileinfo_t		fileinfo;
410 	boolean_t		eos;
411 
412 	smb_pathname_t		*pn;
413 	unsigned char		resume_char;
414 	unsigned char		type;
415 	boolean_t		find_first = B_TRUE;
416 	smb_odir_resume_t	odir_resume;
417 
418 	if (smbsr_decode_vwv(sr, "ww", &maxcount, &sattr) != 0)
419 		return (SDRC_ERROR);
420 
421 	pn = &sr->arg.dirop.fqi.fq_path;
422 	rc = smbsr_decode_data(sr, "%Abw", sr, &pn->pn_path, &type, &key_len);
423 	if ((rc != 0) || (type != 0x05))
424 		return (SDRC_ERROR);
425 
426 	if ((key_len != 0) && (key_len != 21))
427 		return (SDRC_ERROR);
428 
429 	smb_pathname_init(sr, pn, pn->pn_path);
430 	if (!smb_pathname_validate(sr, pn))
431 		return (SDRC_ERROR);
432 
433 	if (smb_is_stream_name(pn->pn_path)) {
434 		smbsr_error(sr, NT_STATUS_OBJECT_NAME_INVALID,
435 		    ERRDOS, ERROR_INVALID_NAME);
436 		return (SDRC_ERROR);
437 	}
438 
439 	find_first = (key_len == 0);
440 	resume_char = 0;
441 	client_key = 0;
442 
443 	if (find_first) {
444 		odid = smb_odir_open(sr, pn->pn_path, sattr, 0);
445 		if (odid == 0)
446 			return (SDRC_ERROR);
447 	} else {
448 		if (smb_mbc_decodef(&sr->smb_data, "b12.wwl",
449 		    &resume_char, &index, &odid, &client_key) != 0) {
450 			return (SDRC_ERROR);
451 		}
452 	}
453 
454 	od = smb_tree_lookup_odir(sr->tid_tree, odid);
455 	if (od == NULL) {
456 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
457 		    ERRDOS, ERROR_INVALID_HANDLE);
458 		return (SDRC_ERROR);
459 	}
460 
461 	if (!find_first) {
462 		odir_resume.or_type = SMB_ODIR_RESUME_IDX;
463 		odir_resume.or_idx = index;
464 		smb_odir_resume_at(od, &odir_resume);
465 	}
466 
467 	(void) smb_mbc_encodef(&sr->reply, "bwwbw", 1, 0, VAR_BCC, 5, 0);
468 
469 	rc = 0;
470 	index = 0;
471 	count = 0;
472 	if (maxcount > SMB_MAX_SEARCH)
473 		maxcount = SMB_MAX_SEARCH;
474 
475 	while (count < maxcount) {
476 		rc = smb_odir_read_fileinfo(sr, od, &fileinfo, &eos);
477 		if ((rc != 0 || (eos == B_TRUE)))
478 			break;
479 
480 		if (*fileinfo.fi_shortname == '\0') {
481 			if (smb_needs_mangled(fileinfo.fi_name))
482 				continue;
483 			(void) strlcpy(fileinfo.fi_shortname, fileinfo.fi_name,
484 			    SMB_SHORTNAMELEN - 1);
485 		}
486 		smb_name83(fileinfo.fi_shortname, name83, SMB_SHORTNAMELEN);
487 
488 		(void) smb_mbc_encodef(&sr->reply, "b11c.wwlbYl13c",
489 		    resume_char, name83, index, odid, client_key,
490 		    fileinfo.fi_dosattr & 0xff,
491 		    smb_time_gmt_to_local(sr, fileinfo.fi_mtime.tv_sec),
492 		    (int32_t)fileinfo.fi_size,
493 		    fileinfo.fi_shortname);
494 
495 		smb_odir_save_cookie(od, index, fileinfo.fi_cookie);
496 
497 		count++;
498 		index++;
499 	}
500 
501 	if (rc != 0) {
502 		smb_odir_close(od);
503 		smb_odir_release(od);
504 		return (SDRC_ERROR);
505 	}
506 
507 	if (count == 0 && find_first) {
508 		smb_odir_close(od);
509 		smb_odir_release(od);
510 		smbsr_warn(sr, NT_STATUS_NO_MORE_FILES,
511 		    ERRDOS, ERROR_NO_MORE_FILES);
512 		return (SDRC_ERROR);
513 	}
514 
515 	rc = (MBC_LENGTH(&sr->reply) - sr->cur_reply_offset) - 8;
516 	if (smb_mbc_poke(&sr->reply, sr->cur_reply_offset, "bwwbw",
517 	    1, count, rc+3, 5, rc) < 0) {
518 		smb_odir_close(od);
519 		smb_odir_release(od);
520 		return (SDRC_ERROR);
521 	}
522 
523 	smb_odir_release(od);
524 	return (SDRC_SUCCESS);
525 }
526 
527 
528 /* *** smb_com_find_close *** */
529 
530 smb_sdrc_t
531 smb_pre_find_close(smb_request_t *sr)
532 {
533 	DTRACE_SMB_1(op__FindClose__start, smb_request_t *, sr);
534 	return (SDRC_SUCCESS);
535 }
536 
537 void
538 smb_post_find_close(smb_request_t *sr)
539 {
540 	DTRACE_SMB_1(op__FindClose__done, smb_request_t *, sr);
541 }
542 
543 smb_sdrc_t
544 smb_com_find_close(smb_request_t *sr)
545 {
546 	int		rc;
547 	uint16_t	maxcount, index;
548 	uint16_t	sattr, odid;
549 	uint16_t	key_len;
550 	uint32_t	client_key;
551 	char		*path;
552 	unsigned char	resume_char;
553 	unsigned char	type;
554 	smb_odir_t	*od;
555 
556 	if (smbsr_decode_vwv(sr, "ww", &maxcount, &sattr) != 0)
557 		return (SDRC_ERROR);
558 
559 	rc = smbsr_decode_data(sr, "%Abw", sr, &path, &type, &key_len);
560 	if ((rc != 0) || (type != 0x05))
561 		return (SDRC_ERROR);
562 
563 	if (key_len == 0) {
564 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
565 		    ERRDOS, ERROR_INVALID_HANDLE);
566 		return (SDRC_ERROR);
567 	} else if (key_len != 21) {
568 		return (SDRC_ERROR);
569 	}
570 
571 	odid = 0;
572 	if (smb_mbc_decodef(&sr->smb_data, "b12.wwl",
573 	    &resume_char, &index, &odid, &client_key) != 0) {
574 		return (SDRC_ERROR);
575 	}
576 
577 	od = smb_tree_lookup_odir(sr->tid_tree, odid);
578 	if (od == NULL) {
579 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
580 		    ERRDOS, ERROR_INVALID_HANDLE);
581 		return (SDRC_ERROR);
582 	}
583 
584 	smb_odir_close(od);
585 	smb_odir_release(od);
586 
587 	if (smbsr_encode_result(sr, 1, 3, "bwwbw", 1, 0, 3, 5, 0))
588 		return (SDRC_ERROR);
589 
590 	return (SDRC_SUCCESS);
591 }
592 
593 
594 /* *** smb_com_find_unique *** */
595 
596 smb_sdrc_t
597 smb_pre_find_unique(smb_request_t *sr)
598 {
599 	DTRACE_SMB_1(op__FindUnique__start, smb_request_t *, sr);
600 	return (SDRC_SUCCESS);
601 }
602 
603 void
604 smb_post_find_unique(smb_request_t *sr)
605 {
606 	DTRACE_SMB_1(op__FindUnique__done, smb_request_t *, sr);
607 }
608 
609 smb_sdrc_t
610 smb_com_find_unique(struct smb_request *sr)
611 {
612 	int			rc;
613 	uint16_t		count, maxcount, index;
614 	uint16_t		sattr, odid;
615 	smb_pathname_t		*pn;
616 	unsigned char		resume_char = '\0';
617 	uint32_t		client_key = 0;
618 	char			name83[SMB_SHORTNAMELEN];
619 	smb_odir_t		*od;
620 	smb_fileinfo_t		fileinfo;
621 	boolean_t		eos;
622 	smb_vdb_t		*vdb;
623 
624 	if (smbsr_decode_vwv(sr, "ww", &maxcount, &sattr) != 0)
625 		return (SDRC_ERROR);
626 
627 	pn = &sr->arg.dirop.fqi.fq_path;
628 	vdb = kmem_alloc(sizeof (smb_vdb_t), KM_SLEEP);
629 	if ((smbsr_decode_data(sr, "%AV", sr, &pn->pn_path, vdb) != 0) ||
630 	    (vdb->vdb_len != 0)) {
631 		kmem_free(vdb, sizeof (smb_vdb_t));
632 		return (SDRC_ERROR);
633 	}
634 	kmem_free(vdb, sizeof (smb_vdb_t));
635 
636 	smb_pathname_init(sr, pn, pn->pn_path);
637 	if (!smb_pathname_validate(sr, pn))
638 		return (SDRC_ERROR);
639 
640 	if (smb_is_stream_name(pn->pn_path)) {
641 		smbsr_error(sr, NT_STATUS_OBJECT_NAME_INVALID,
642 		    ERRDOS, ERROR_INVALID_NAME);
643 		return (SDRC_ERROR);
644 	}
645 
646 	(void) smb_mbc_encodef(&sr->reply, "bwwbw", 1, 0, VAR_BCC, 5, 0);
647 
648 	odid = smb_odir_open(sr, pn->pn_path, sattr, 0);
649 	if (odid == 0)
650 		return (SDRC_ERROR);
651 	od = smb_tree_lookup_odir(sr->tid_tree, odid);
652 	if (od == NULL)
653 		return (SDRC_ERROR);
654 
655 	rc = 0;
656 	count = 0;
657 	index = 0;
658 	if (maxcount > SMB_MAX_SEARCH)
659 		maxcount = SMB_MAX_SEARCH;
660 
661 	while (count < maxcount) {
662 		rc = smb_odir_read_fileinfo(sr, od, &fileinfo, &eos);
663 		if ((rc != 0 || (eos == B_TRUE)))
664 			break;
665 
666 		if (*fileinfo.fi_shortname == '\0') {
667 			if (smb_needs_mangled(fileinfo.fi_name))
668 				continue;
669 			(void) strlcpy(fileinfo.fi_shortname, fileinfo.fi_name,
670 			    SMB_SHORTNAMELEN - 1);
671 		}
672 		smb_name83(fileinfo.fi_shortname, name83, SMB_SHORTNAMELEN);
673 
674 		(void) smb_mbc_encodef(&sr->reply, "b11c.wwlbYl13c",
675 		    resume_char, name83, index, odid, client_key,
676 		    fileinfo.fi_dosattr & 0xff,
677 		    smb_time_gmt_to_local(sr, fileinfo.fi_mtime.tv_sec),
678 		    (int32_t)fileinfo.fi_size,
679 		    fileinfo.fi_shortname);
680 
681 		count++;
682 		index++;
683 	}
684 
685 	smb_odir_close(od);
686 	smb_odir_release(od);
687 
688 	if (rc != 0)
689 		return (SDRC_ERROR);
690 
691 	if (count == 0) {
692 		smbsr_warn(sr, NT_STATUS_NO_MORE_FILES,
693 		    ERRDOS, ERROR_NO_MORE_FILES);
694 		return (SDRC_ERROR);
695 	}
696 
697 	rc = (MBC_LENGTH(&sr->reply) - sr->cur_reply_offset) - 8;
698 	if (smb_mbc_poke(&sr->reply, sr->cur_reply_offset,
699 	    "bwwbw", 1, count, rc+3, 5, rc) < 0) {
700 		return (SDRC_ERROR);
701 	}
702 
703 	return (SDRC_SUCCESS);
704 }
705 
706 /*
707  * smb_name83
708  *
709  * Format the filename for inclusion in the resume key. The filename
710  * returned in the resume key is 11 bytes:
711  * - up to 8 bytes of filename, space padded to 8 bytes
712  * - up to 3 bytes of ext, space padded to 3 bytes
713  *
714  * The name passed to smb_name83 should be a shortname or a name that
715  * doesn't require mangling.
716  *
717  * Examples:
718  *	"fname.txt"    -> "FNAME   TXT"
719  *	"fname.tx"     -> "FNAME   TX "
720  *	"filename"     -> "FILENAME   "
721  *	"filename.txt" -> "FILENAMETXT"
722  *	"FILE~1.TXT"   -> "FILE~1  TXT"
723  */
724 static void
725 smb_name83(const char *name, char *buf, size_t buflen)
726 {
727 	const char *p;
728 	char *pbuf;
729 	int i;
730 
731 	ASSERT(name && buf && (buflen >= SMB_NAME83_BUFLEN));
732 
733 	(void) strlcpy(buf, "           ", SMB_NAME83_BUFLEN);
734 
735 	/* Process "." and ".." up front */
736 	if ((strcmp(name, ".") == 0) || (strcmp(name, "..") == 0)) {
737 		(void) strncpy(buf, name, strlen(name));
738 		return;
739 	}
740 
741 	ASSERT(smb_needs_mangled(name) == B_FALSE);
742 
743 	/* Process basename */
744 	for (i = 0, p = name, pbuf = buf;
745 	    (i < SMB_NAME83_BASELEN) && (*p != '\0') && (*p != '.'); ++i)
746 		*pbuf++ = *p++;
747 
748 	/* Process the extension from the last dot in name */
749 	if ((p = strchr(name, '.')) != NULL) {
750 		++p;
751 		pbuf = &buf[SMB_NAME83_BASELEN];
752 		for (i = 0; (i < SMB_NAME83_EXTLEN) && (*p != '\0'); ++i)
753 			*pbuf++ = *p++;
754 	}
755 
756 	(void) smb_strupr(buf);
757 }
758