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  * This module provides functions for TRANS2_FIND_FIRST2 and
29  * TRANS2_FIND_NEXT2 requests. The requests allow the client to search
30  * for the file(s) which match the file specification.  The search is
31  * started with TRANS2_FIND_FIRST2 and can be continued if necessary with
32  * TRANS2_FIND_NEXT2. There are numerous levels of information which may be
33  * obtained for the returned files, the desired level is specified in the
34  * InformationLevel field of the requests.
35  *
36  *  InformationLevel Name              Value
37  *  =================================  ================
38  *
39  *  SMB_INFO_STANDARD                  1
40  *  SMB_INFO_QUERY_EA_SIZE             2
41  *  SMB_INFO_QUERY_EAS_FROM_LIST       3
42  *  SMB_FIND_FILE_DIRECTORY_INFO       0x101
43  *  SMB_FIND_FILE_FULL_DIRECTORY_INFO  0x102
44  *  SMB_FIND_FILE_NAMES_INFO           0x103
45  *  SMB_FIND_FILE_BOTH_DIRECTORY_INFO  0x104
46  *  SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO  0x105
47  *  SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO  0x106
48  *
49  * The following sections detail the data returned for each
50  * InformationLevel. The requested information is placed in the Data
51  * portion of the transaction response. Note: a client which does not
52  * support long names can only request SMB_INFO_STANDARD.
53  *
54  * A four-byte resume key precedes each data item (described below) if bit
55  * 2 in the Flags field is set, i.e. if the request indicates the server
56  * should return resume keys. Note: it is not always the case. If the
57  * data item already includes the resume key, the resume key should not be
58  * added again.
59  *
60  * 4.3.4.1   SMB_INFO_STANDARD
61  *
62  *  Response Field                    Description
63  *  ================================  ==================================
64  *
65  *  SMB_DATE CreationDate;            Date when file was created
66  *  SMB_TIME CreationTime;            Time when file was created
67  *  SMB_DATE LastAccessDate;          Date of last file access
68  *  SMB_TIME LastAccessTime;          Time of last file access
69  *  SMB_DATE LastWriteDate;           Date of last write to the file
70  *  SMB_TIME LastWriteTime;           Time of last write to the file
71  *  ULONG  DataSize;                  File Size
72  *  ULONG AllocationSize;             Size of filesystem allocation unit
73  *  USHORT Attributes;                File Attributes
74  *  UCHAR FileNameLength;             Length of filename in bytes
75  *  STRING FileName;                  Name of found file
76  *
77  * 4.3.4.2   SMB_INFO_QUERY_EA_SIZE
78  *
79  *  Response Field                     Description
80  *  =================================  ==================================
81  *
82  *   SMB_DATE CreationDate;            Date when file was created
83  *   SMB_TIME CreationTime;            Time when file was created
84  *   SMB_DATE LastAccessDate;          Date of last file access
85  *   SMB_TIME LastAccessTime;          Time of last file access
86  *   SMB_DATE LastWriteDate;           Date of last write to the file
87  *   SMB_TIME LastWriteTime;           Time of last write to the file
88  *   ULONG DataSize;                   File Size
89  *   ULONG AllocationSize;             Size of filesystem allocation unit
90  *   USHORT Attributes;                File Attributes
91  *   ULONG EaSize;                     Size of file's EA information
92  *   UCHAR FileNameLength;             Length of filename in bytes
93  *   STRING FileName;                  Name of found file
94  *
95  * 4.3.4.3   SMB_INFO_QUERY_EAS_FROM_LIST
96  *
97  * This request returns the same information as SMB_INFO_QUERY_EA_SIZE, but
98  * only for files which have an EA list which match the EA information in
99  * the Data part of the request.
100  *
101  * 4.3.4.4   SMB_FIND_FILE_DIRECTORY_INFO
102  *
103  *  Response Field                     Description
104  *  =================================  ==================================
105  *
106  *  ULONG NextEntryOffset;             Offset from this structure to
107  *					beginning of next one
108  *  ULONG FileIndex;
109  *  LARGE_INTEGER CreationTime;        file creation time
110  *  LARGE_INTEGER LastAccessTime;      last access time
111  *  LARGE_INTEGER LastWriteTime;       last write time
112  *  LARGE_INTEGER ChangeTime;          last attribute change time
113  *  LARGE_INTEGER EndOfFile;           file size
114  *  LARGE_INTEGER AllocationSize;      size of filesystem allocation information
115  *  ULONG ExtFileAttributes;           Extended file attributes
116  *					(see section 3.11)
117  *  ULONG FileNameLength;              Length of filename in bytes
118  *  STRING FileName;                   Name of the file
119  *
120  * 4.3.4.5   SMB_FIND_FILE_FULL_DIRECTORY_INFO
121  *
122  *  Response Field                     Description
123  *  =================================  ==================================
124  *
125  *  ULONG NextEntryOffset;             Offset from this structure to
126  *					beginning of next one
127  *  ULONG FileIndex;
128  *  LARGE_INTEGER CreationTime;        file creation time
129  *  LARGE_INTEGER LastAccessTime;      last access time
130  *  LARGE_INTEGER LastWriteTime;       last write time
131  *  LARGE_INTEGER ChangeTime;          last attribute change time
132  *  LARGE_INTEGER EndOfFile;           file size
133  *  LARGE_INTEGER AllocationSize;      size of filesystem allocation information
134  *  ULONG ExtFileAttributes;           Extended file attributes
135  *					(see section 3.11)
136  *  ULONG FileNameLength;              Length of filename in bytes
137  *  ULONG EaSize;                      Size of file's extended attributes
138  *  STRING FileName;                   Name of the file
139  *
140  *
141  *  SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO
142  *
143  *  This is the same as SMB_FIND_FILE_FULL_DIRECTORY_INFO but with
144  *  FileId inserted after EaSize. FileId is preceded by a 4 byte
145  *  alignment padding.
146  *
147  *  Response Field                     Description
148  *  =================================  ==================================
149  *  ...
150  *  ULONG EaSize;                      Size of file's extended attributes
151  *  UCHAR Reserved[4]
152  *  LARGE_INTEGER FileId               Internal file system unique id.
153  *  STRING FileName;                   Name of the file
154  *
155  * 4.3.4.6   SMB_FIND_FILE_BOTH_DIRECTORY_INFO
156  *
157  *  Response Field                     Description
158  *  =================================  ==================================
159  *
160  *  ULONG NextEntryOffset;             Offset from this structure to
161  *					beginning of next one
162  *  ULONG FileIndex;
163  *  LARGE_INTEGER CreationTime;        file creation time
164  *  LARGE_INTEGER LastAccessTime;      last access time
165  *  LARGE_INTEGER LastWriteTime;       last write time
166  *  LARGE_INTEGER ChangeTime;          last attribute change time
167  *  LARGE_INTEGER EndOfFile;           file size
168  *  LARGE_INTEGER AllocationSize;      size of filesystem allocation information
169  *  ULONG ExtFileAttributes;           Extended file attributes
170  *					(see section 3.11)
171  *  ULONG FileNameLength;              Length of FileName in bytes
172  *  ULONG EaSize;                      Size of file's extended attributes
173  *  UCHAR ShortNameLength;             Length of file's short name in bytes
174  *  UCHAR Reserved
175  *  WCHAR ShortName[12];               File's 8.3 conformant name in Unicode
176  *  STRING FileName;                   Files full length name
177  *
178  *
179  *  SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO
180  *
181  *  This is the same as SMB_FIND_FILE_BOTH_DIRECTORY_INFO but with
182  *  FileId inserted after ShortName. FileId is preceded by a 2 byte
183  *  alignment pad.
184  *
185  *  Response Field                     Description
186  *  =================================  ==================================
187  *  ...
188  *  WCHAR ShortName[12];               File's 8.3 conformant name in Unicode
189  *  UCHAR Reserved[2]
190  *  LARGE_INTEGER FileId               Internal file system unique id.
191  *  STRING FileName;                   Files full length name
192  *
193  * 4.3.4.7   SMB_FIND_FILE_NAMES_INFO
194  *
195  *  Response Field                     Description
196  *  =================================  ==================================
197  *
198  *  ULONG NextEntryOffset;             Offset from this structure to
199  *                                     beginning of next one
200  *  ULONG FileIndex;
201  *  ULONG FileNameLength;              Length of FileName in bytes
202  *  STRING FileName;                   Files full length name
203  */
204 
205 #include <smbsrv/smb_incl.h>
206 #include <smbsrv/msgbuf.h>
207 #include <smbsrv/smb_fsops.h>
208 
209 typedef struct smb_find_args {
210 	uint16_t fa_infolev;
211 	uint16_t fa_maxcount;
212 	uint16_t fa_fflag;
213 	uint32_t fa_maxdata;
214 } smb_find_args_t;
215 
216 static int smb_trans2_find_entries(smb_request_t *, smb_xa_t *,
217     smb_odir_t *, smb_find_args_t *, boolean_t *);
218 static int smb_trans2_find_get_maxdata(smb_request_t *, uint16_t, uint16_t);
219 static int smb_trans2_find_mbc_encode(smb_request_t *, smb_xa_t *,
220     smb_fileinfo_t *, smb_find_args_t *);
221 
222 /*
223  * Tunable parameter to limit the maximum
224  * number of entries to be returned.
225  */
226 uint16_t smb_trans2_find_max = 128;
227 
228 /*
229  * smb_com_trans2_find_first2
230  *
231  *  Client Request                Value
232  *  ============================  ==================================
233  *
234  *  UCHAR  WordCount              15
235  *  UCHAR  TotalDataCount         Total size of extended attribute list
236  *  UCHAR  SetupCount             1
237  *  UCHAR  Setup[0]               TRANS2_FIND_FIRST2
238  *
239  *  Parameter Block Encoding      Description
240  *  ============================  ==================================
241  *  USHORT SearchAttributes;
242  *  USHORT SearchCount;           Maximum number of entries to return
243  *  USHORT Flags;                 Additional information:
244  *                                Bit 0 - close search after this request
245  *                                Bit 1 - close search if end of search
246  *                                reached
247  *                                Bit 2 - return resume keys for each
248  *                                entry found
249  *                                Bit 3 - continue search from previous
250  *                                ending place
251  *                                Bit 4 - find with backup intent
252  *  USHORT InformationLevel;      See below
253  *  ULONG SearchStorageType;
254  *  STRING FileName;              Pattern for the search
255  *  UCHAR Data[ TotalDataCount ]  FEAList if InformationLevel is
256  *                                QUERY_EAS_FROM_LIST
257  *
258  *  Response Parameter Block      Description
259  *  ============================  ==================================
260  *
261  *  USHORT Sid;                   Search handle
262  *  USHORT SearchCount;           Number of entries returned
263  *  USHORT EndOfSearch;           Was last entry returned?
264  *  USHORT EaErrorOffset;         Offset into EA list if EA error
265  *  USHORT LastNameOffset;        Offset into data to file name of last
266  *                                entry, if server needs it to resume
267  *                                search; else 0
268  *  UCHAR Data[ TotalDataCount ]  Level dependent info about the matches
269  *                                found in the search
270  */
271 smb_sdrc_t
272 smb_com_trans2_find_first2(smb_request_t *sr, smb_xa_t *xa)
273 {
274 	int		count;
275 	uint16_t	sattr, odid;
276 	char		*path;
277 	smb_odir_t	*od;
278 	smb_find_args_t	args;
279 	boolean_t	eos;
280 	uint32_t	odir_flags = 0;
281 
282 	bzero(&args, sizeof (smb_find_args_t));
283 
284 	if (!STYPE_ISDSK(sr->tid_tree->t_res_type)) {
285 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
286 		    ERRDOS, ERROR_ACCESS_DENIED);
287 		return (SDRC_ERROR);
288 	}
289 
290 	if (smb_mbc_decodef(&xa->req_param_mb, "%wwww4.u", sr, &sattr,
291 	    &args.fa_maxcount, &args.fa_fflag, &args.fa_infolev, &path) != 0) {
292 		return (SDRC_ERROR);
293 	}
294 
295 	if (smb_is_stream_name(path)) {
296 		smbsr_error(sr, NT_STATUS_OBJECT_NAME_INVALID,
297 		    ERRDOS, ERROR_INVALID_NAME);
298 		return (SDRC_ERROR);
299 	}
300 
301 	if (args.fa_fflag & SMB_FIND_WITH_BACKUP_INTENT) {
302 		sr->user_cr = smb_user_getprivcred(sr->uid_user);
303 		odir_flags = SMB_ODIR_OPENF_BACKUP_INTENT;
304 	}
305 
306 	args.fa_maxdata =
307 	    smb_trans2_find_get_maxdata(sr, args.fa_infolev, args.fa_fflag);
308 	if (args.fa_maxdata == 0)
309 		return (SDRC_ERROR);
310 
311 	if (sr->smb_flg2 & SMB_FLAGS2_UNICODE)
312 		(void) smb_convert_wildcards(path);
313 
314 	odid = smb_odir_open(sr, path, sattr, odir_flags);
315 	if (odid == 0)
316 		return (SDRC_ERROR);
317 
318 	od = smb_tree_lookup_odir(sr->tid_tree, odid);
319 	if (od == NULL)
320 		return (SDRC_ERROR);
321 	count = smb_trans2_find_entries(sr, xa, od, &args, &eos);
322 
323 	if (count == -1) {
324 		smb_odir_close(od);
325 		smb_odir_release(od);
326 		return (SDRC_ERROR);
327 	}
328 
329 	if (count == 0) {
330 		smb_odir_close(od);
331 		smb_odir_release(od);
332 		smbsr_errno(sr, ENOENT);
333 		return (SDRC_ERROR);
334 	}
335 
336 	if ((args.fa_fflag & SMB_FIND_CLOSE_AFTER_REQUEST) ||
337 	    (eos && (args.fa_fflag & SMB_FIND_CLOSE_AT_EOS))) {
338 		smb_odir_close(od);
339 	} /* else leave odir open for trans2_find_next2 */
340 
341 	smb_odir_release(od);
342 
343 	(void) smb_mbc_encodef(&xa->rep_param_mb, "wwwww",
344 	    odid, count, (eos) ? 1 : 0, 0, 0);
345 
346 	return (SDRC_SUCCESS);
347 }
348 
349 /*
350  * smb_com_trans2_find_next2
351  *
352  *  Client Request                     Value
353  *  ================================== =================================
354  *
355  *  WordCount                          15
356  *  SetupCount                         1
357  *  Setup[0]                           TRANS2_FIND_NEXT2
358  *
359  *  Parameter Block Encoding           Description
360  *  ================================== =================================
361  *
362  *  USHORT Sid;                        Search handle
363  *  USHORT SearchCount;                Maximum number of entries to
364  *                                      return
365  *  USHORT InformationLevel;           Levels described in
366  *                                      TRANS2_FIND_FIRST2 request
367  *  ULONG ResumeKey;                   Value returned by previous find2
368  *                                      call
369  *  USHORT Flags;                      Additional information: bit set-
370  *                                      0 - close search after this
371  *                                      request
372  *                                      1 - close search if end of search
373  *                                      reached
374  *                                      2 - return resume keys for each
375  *                                      entry found
376  *                                      3 - resume/continue from previous
377  *                                      ending place
378  *                                      4 - find with backup intent
379  *  STRING FileName;                   Resume file name
380  *
381  * Sid is the value returned by a previous successful TRANS2_FIND_FIRST2
382  * call.  If Bit3 of Flags is set, then FileName may be the NULL string,
383  * since the search is continued from the previous TRANS2_FIND request.
384  * Otherwise, FileName must not be more than 256 characters long.
385  *
386  *  Response Field                     Description
387  *  ================================== =================================
388  *
389  *  USHORT SearchCount;                Number of entries returned
390  *  USHORT EndOfSearch;                Was last entry returned?
391  *  USHORT EaErrorOffset;              Offset into EA list if EA error
392  *  USHORT LastNameOffset;             Offset into data to file name of
393  *                                      last entry, if server needs it to
394  *                                      resume search; else 0
395  *  UCHAR Data[TotalDataCount]         Level dependent info about the
396  *                                      matches found in the search
397  *
398  *
399  * The last parameter in the request is a filename, which is a
400  * null-terminated unicode string.
401  *
402  * smb_mbc_decodef(&xa->req_param_mb, "%www lwu", sr,
403  *    &odid, &fa_maxcount, &fa_infolev, &cookie, &fa_fflag, &fname)
404  *
405  * The filename parameter is not currently decoded because we
406  * expect a 2-byte null but Mac OS 10 clients send a 1-byte null,
407  * which leads to a decode error.
408  * Thus, we do not support resume by filename.  We treat a request
409  * to resume by filename as SMB_FIND_CONTINUE_FROM_LAST.
410  */
411 smb_sdrc_t
412 smb_com_trans2_find_next2(smb_request_t *sr, smb_xa_t *xa)
413 {
414 	int			count;
415 	uint16_t		odid;
416 	uint32_t		cookie;
417 	smb_odir_t		*od;
418 	smb_find_args_t		args;
419 	boolean_t		eos;
420 	smb_odir_resume_t	odir_resume;
421 
422 	bzero(&args, sizeof (smb_find_args_t));
423 
424 	if (smb_mbc_decodef(&xa->req_param_mb, "%wwwlw", sr, &odid,
425 	    &args.fa_maxcount, &args.fa_infolev, &cookie, &args.fa_fflag)
426 	    != 0) {
427 		return (SDRC_ERROR);
428 	}
429 
430 	/* continuation by filename not supported */
431 	if ((args.fa_fflag & SMB_FIND_CONTINUE_FROM_LAST) || (cookie == 0)) {
432 		odir_resume.or_type = SMB_ODIR_RESUME_IDX;
433 		odir_resume.or_idx = 0;
434 	} else {
435 		odir_resume.or_type = SMB_ODIR_RESUME_COOKIE;
436 		odir_resume.or_cookie = cookie;
437 	}
438 
439 	if (args.fa_fflag & SMB_FIND_WITH_BACKUP_INTENT)
440 		sr->user_cr = smb_user_getprivcred(sr->uid_user);
441 
442 	args.fa_maxdata =
443 	    smb_trans2_find_get_maxdata(sr, args.fa_infolev, args.fa_fflag);
444 	if (args.fa_maxdata == 0)
445 		return (SDRC_ERROR);
446 
447 	od = smb_tree_lookup_odir(sr->tid_tree, odid);
448 	if (od == NULL) {
449 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
450 		    ERRDOS, ERROR_INVALID_HANDLE);
451 		return (SDRC_ERROR);
452 	}
453 	smb_odir_resume_at(od, &odir_resume);
454 	count = smb_trans2_find_entries(sr, xa, od, &args, &eos);
455 
456 	if (count == -1) {
457 		smb_odir_close(od);
458 		smb_odir_release(od);
459 		return (SDRC_ERROR);
460 	}
461 
462 	if ((args.fa_fflag & SMB_FIND_CLOSE_AFTER_REQUEST) ||
463 	    (eos && (args.fa_fflag & SMB_FIND_CLOSE_AT_EOS))) {
464 		smb_odir_close(od);
465 	} /* else leave odir open for trans2_find_next2 */
466 
467 	smb_odir_release(od);
468 	(void) smb_mbc_encodef(&xa->rep_param_mb, "wwww",
469 	    count, (eos) ? 1 : 0, 0, 0);
470 
471 	return (SDRC_SUCCESS);
472 }
473 
474 
475 /*
476  * smb_trans2_find_entries
477  *
478  * Find and encode up to args->fa_maxcount directory entries.
479  * For compatibilty with Windows, if args->fa_maxcount is zero treat it as 1.
480  *
481  * Returns:
482  *   count - count of entries encoded
483  *           *eos = B_TRUE if no more directory entries
484  *      -1 - error
485  */
486 static int
487 smb_trans2_find_entries(smb_request_t *sr, smb_xa_t *xa, smb_odir_t *od,
488     smb_find_args_t *args, boolean_t *eos)
489 {
490 	int		rc;
491 	uint16_t	count, maxcount;
492 	uint32_t	cookie;
493 	smb_fileinfo_t	fileinfo;
494 
495 	if ((maxcount = args->fa_maxcount) == 0)
496 		maxcount = 1;
497 
498 	if ((smb_trans2_find_max != 0) && (maxcount > smb_trans2_find_max))
499 		maxcount = smb_trans2_find_max;
500 
501 	count = 0;
502 	while (count < maxcount) {
503 		if (smb_odir_read_fileinfo(sr, od, &fileinfo, eos) != 0)
504 			return (-1);
505 		if (*eos == B_TRUE)
506 			break;
507 
508 		rc = smb_trans2_find_mbc_encode(sr, xa, &fileinfo, args);
509 		if (rc == -1)
510 			return (-1);
511 		if (rc == 1)
512 			break;
513 
514 		cookie = fileinfo.fi_cookie;
515 		++count;
516 	}
517 
518 	/* save the last cookie returned to client */
519 	if (count != 0)
520 		smb_odir_save_cookie(od, 0, cookie);
521 
522 	/* if eos not already detected, check if more entries */
523 	if (!*eos)
524 		(void) smb_odir_read_fileinfo(sr, od, &fileinfo, eos);
525 
526 	return (count);
527 }
528 
529 /*
530  * smb_trans2_find_get_maxdata
531  *
532  * Calculate the minimum response space required for the specified
533  * information level.
534  *
535  * A non-zero return value provides the minimum space required.
536  * A return value of zero indicates an unknown information level.
537  */
538 static int
539 smb_trans2_find_get_maxdata(smb_request_t *sr, uint16_t infolev, uint16_t fflag)
540 {
541 	int maxdata;
542 
543 	maxdata = smb_ascii_or_unicode_null_len(sr);
544 
545 	switch (infolev) {
546 	case SMB_INFO_STANDARD :
547 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
548 			maxdata += sizeof (int32_t);
549 		maxdata += 2 + 2 + 2 + 4 + 4 + 2 + 1;
550 		break;
551 
552 	case SMB_INFO_QUERY_EA_SIZE:
553 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
554 			maxdata += sizeof (int32_t);
555 		maxdata += 2 + 2 + 2 + 4 + 4 + 2 + 4 + 1;
556 		break;
557 
558 	case SMB_FIND_FILE_DIRECTORY_INFO:
559 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4;
560 		break;
561 
562 	case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
563 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4 + 4;
564 		break;
565 
566 	case SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO:
567 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4 + 4 + 4 + 8;
568 		break;
569 
570 	case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
571 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4 + 4 + 2 + 24;
572 		break;
573 
574 	case SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO:
575 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4 + 4 + 2 + 24
576 		    + 2 + 8;
577 		break;
578 
579 	case SMB_FIND_FILE_NAMES_INFO:
580 		maxdata += 4 + 4 + 4;
581 		break;
582 
583 	case SMB_MAC_FIND_BOTH_HFS_INFO:
584 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 1 + 1 + 2 +
585 		    4 + 32 + 4 + 1 + 1 + 24 + 4;
586 		break;
587 
588 	default:
589 		maxdata = 0;
590 		smbsr_error(sr, NT_STATUS_INVALID_LEVEL,
591 		    ERRDOS, ERROR_INVALID_LEVEL);
592 	}
593 
594 	return (maxdata);
595 }
596 
597 /*
598  * smb_trans2_mbc_encode
599  *
600  * This function encodes the mbc for one directory entry.
601  *
602  * The function returns -1 when the max data requested by client
603  * is reached. If the entry is valid and successful encoded, 0
604  * will be returned; otherwise, 1 will be returned.
605  *
606  * We always null terminate the filename. The space for the null
607  * is included in the maxdata calculation and is therefore included
608  * in the next_entry_offset. namelen is the unterminated length of
609  * the filename. For levels except STANDARD and EA_SIZE, if the
610  * filename is ascii the name length returned to the client should
611  * include the null terminator. Otherwise the length returned to
612  * the client should not include the terminator.
613  *
614  * Returns: 0 - data successfully encoded
615  *          1 - client request's maxdata limit reached
616  *	   -1 - error
617  */
618 static int
619 smb_trans2_find_mbc_encode(smb_request_t *sr, smb_xa_t *xa,
620     smb_fileinfo_t *fileinfo, smb_find_args_t *args)
621 {
622 	int		namelen, shortlen, buflen;
623 	uint32_t	next_entry_offset;
624 	uint32_t	dsize32, asize32;
625 	uint32_t	mb_flags = 0;
626 	char		buf83[26];
627 	char		*tmpbuf;
628 	smb_msgbuf_t	mb;
629 
630 	namelen = smb_ascii_or_unicode_strlen(sr, fileinfo->fi_name);
631 	if (namelen == -1)
632 		return (-1);
633 
634 	next_entry_offset = args->fa_maxdata + namelen;
635 
636 	if (MBC_ROOM_FOR(&xa->rep_data_mb, (args->fa_maxdata + namelen)) == 0)
637 		return (1);
638 
639 	/*
640 	 * If ascii the filename length returned to the client should
641 	 * include the null terminator for levels except STANDARD and
642 	 * EASIZE.
643 	 */
644 	if (!(sr->smb_flg2 & SMB_FLAGS2_UNICODE)) {
645 		if ((args->fa_infolev != SMB_INFO_STANDARD) &&
646 		    (args->fa_infolev != SMB_INFO_QUERY_EA_SIZE))
647 			namelen += 1;
648 	}
649 
650 	mb_flags = (sr->smb_flg2 & SMB_FLAGS2_UNICODE) ? SMB_MSGBUF_UNICODE : 0;
651 	dsize32 = (fileinfo->fi_size > UINT_MAX) ?
652 	    UINT_MAX : (uint32_t)fileinfo->fi_size;
653 	asize32 = (fileinfo->fi_alloc_size > UINT_MAX) ?
654 	    UINT_MAX : (uint32_t)fileinfo->fi_alloc_size;
655 
656 	switch (args->fa_infolev) {
657 	case SMB_INFO_STANDARD:
658 		if (args->fa_fflag & SMB_FIND_RETURN_RESUME_KEYS)
659 			(void) smb_mbc_encodef(&xa->rep_data_mb, "l",
660 			    fileinfo->fi_cookie);
661 
662 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%yyyllwbu", sr,
663 		    smb_gmt2local(sr, fileinfo->fi_crtime.tv_sec),
664 		    smb_gmt2local(sr, fileinfo->fi_atime.tv_sec),
665 		    smb_gmt2local(sr, fileinfo->fi_mtime.tv_sec),
666 		    dsize32,
667 		    asize32,
668 		    fileinfo->fi_dosattr,
669 		    namelen,
670 		    fileinfo->fi_name);
671 		break;
672 
673 	case SMB_INFO_QUERY_EA_SIZE:
674 		if (args->fa_fflag & SMB_FIND_RETURN_RESUME_KEYS)
675 			(void) smb_mbc_encodef(&xa->rep_data_mb, "l",
676 			    fileinfo->fi_cookie);
677 
678 		/*
679 		 * Unicode filename should NOT be aligned. Encode ('u')
680 		 * into a temporary buffer, then encode buffer as a
681 		 * byte stream ('#c').
682 		 * Regardless of whether unicode or ascii, a single
683 		 * termination byte is used.
684 		 */
685 		buflen = namelen + sizeof (mts_wchar_t);
686 		tmpbuf = kmem_zalloc(buflen, KM_SLEEP);
687 		smb_msgbuf_init(&mb, (uint8_t *)tmpbuf, buflen, mb_flags);
688 		if (smb_msgbuf_encode(&mb, "u", fileinfo->fi_name) < 0) {
689 			smb_msgbuf_term(&mb);
690 			kmem_free(tmpbuf, buflen);
691 			return (-1);
692 		}
693 		tmpbuf[namelen] = '\0';
694 
695 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%yyyllwlb#c", sr,
696 		    smb_gmt2local(sr, fileinfo->fi_crtime.tv_sec),
697 		    smb_gmt2local(sr, fileinfo->fi_atime.tv_sec),
698 		    smb_gmt2local(sr, fileinfo->fi_mtime.tv_sec),
699 		    dsize32,
700 		    asize32,
701 		    fileinfo->fi_dosattr,
702 		    0L,		/* EA Size */
703 		    namelen,
704 		    namelen + 1,
705 		    tmpbuf);
706 
707 		smb_msgbuf_term(&mb);
708 		kmem_free(tmpbuf, buflen);
709 		break;
710 
711 	case SMB_FIND_FILE_DIRECTORY_INFO:
712 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqllu", sr,
713 		    next_entry_offset,
714 		    fileinfo->fi_cookie,
715 		    &fileinfo->fi_crtime,
716 		    &fileinfo->fi_atime,
717 		    &fileinfo->fi_mtime,
718 		    &fileinfo->fi_ctime,
719 		    fileinfo->fi_size,
720 		    fileinfo->fi_alloc_size,
721 		    fileinfo->fi_dosattr,
722 		    namelen,
723 		    fileinfo->fi_name);
724 		break;
725 
726 	case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
727 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqlllu", sr,
728 		    next_entry_offset,
729 		    fileinfo->fi_cookie,
730 		    &fileinfo->fi_crtime,
731 		    &fileinfo->fi_atime,
732 		    &fileinfo->fi_mtime,
733 		    &fileinfo->fi_ctime,
734 		    fileinfo->fi_size,
735 		    fileinfo->fi_alloc_size,
736 		    fileinfo->fi_dosattr,
737 		    namelen,
738 		    0L,
739 		    fileinfo->fi_name);
740 		break;
741 
742 	case SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO:
743 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqlll4.qu", sr,
744 		    next_entry_offset,
745 		    fileinfo->fi_cookie,
746 		    &fileinfo->fi_crtime,
747 		    &fileinfo->fi_atime,
748 		    &fileinfo->fi_mtime,
749 		    &fileinfo->fi_ctime,
750 		    fileinfo->fi_size,
751 		    fileinfo->fi_alloc_size,
752 		    fileinfo->fi_dosattr,
753 		    namelen,
754 		    0L,
755 		    fileinfo->fi_nodeid,
756 		    fileinfo->fi_name);
757 		break;
758 
759 	case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
760 		bzero(buf83, sizeof (buf83));
761 		smb_msgbuf_init(&mb, (uint8_t *)buf83, sizeof (buf83),
762 		    mb_flags);
763 		if (smb_msgbuf_encode(&mb, "U", fileinfo->fi_shortname) < 0) {
764 			smb_msgbuf_term(&mb);
765 			return (-1);
766 		}
767 		shortlen = mts_wcequiv_strlen(fileinfo->fi_shortname);
768 
769 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqlllb.24cu",
770 		    sr,
771 		    next_entry_offset,
772 		    fileinfo->fi_cookie,
773 		    &fileinfo->fi_crtime,
774 		    &fileinfo->fi_atime,
775 		    &fileinfo->fi_mtime,
776 		    &fileinfo->fi_ctime,
777 		    fileinfo->fi_size,
778 		    fileinfo->fi_alloc_size,
779 		    fileinfo->fi_dosattr,
780 		    namelen,
781 		    0L,
782 		    shortlen,
783 		    buf83,
784 		    fileinfo->fi_name);
785 
786 		smb_msgbuf_term(&mb);
787 		break;
788 
789 	case SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO:
790 		bzero(buf83, sizeof (buf83));
791 		smb_msgbuf_init(&mb, (uint8_t *)buf83, sizeof (buf83),
792 		    mb_flags);
793 		if (smb_msgbuf_encode(&mb, "u", fileinfo->fi_shortname) < 0) {
794 			smb_msgbuf_term(&mb);
795 			return (-1);
796 		}
797 		shortlen = smb_ascii_or_unicode_strlen(sr,
798 		    fileinfo->fi_shortname);
799 
800 		(void) smb_mbc_encodef(&xa->rep_data_mb,
801 		    "%llTTTTqqlllb.24c2.qu",
802 		    sr,
803 		    next_entry_offset,
804 		    fileinfo->fi_cookie,
805 		    &fileinfo->fi_crtime,
806 		    &fileinfo->fi_atime,
807 		    &fileinfo->fi_mtime,
808 		    &fileinfo->fi_ctime,
809 		    fileinfo->fi_size,
810 		    fileinfo->fi_alloc_size,
811 		    fileinfo->fi_dosattr,
812 		    namelen,
813 		    0L,
814 		    shortlen,
815 		    buf83,
816 		    fileinfo->fi_nodeid,
817 		    fileinfo->fi_name);
818 
819 		smb_msgbuf_term(&mb);
820 		break;
821 
822 	case SMB_FIND_FILE_NAMES_INFO:
823 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%lllu", sr,
824 		    next_entry_offset,
825 		    fileinfo->fi_cookie,
826 		    namelen,
827 		    fileinfo->fi_name);
828 		break;
829 	}
830 
831 	return (0);
832 }
833 
834 /*
835  * Close a search started by a Trans2FindFirst2 request.
836  */
837 smb_sdrc_t
838 smb_pre_find_close2(smb_request_t *sr)
839 {
840 	DTRACE_SMB_1(op__FindClose2__start, smb_request_t *, sr);
841 	return (SDRC_SUCCESS);
842 }
843 
844 void
845 smb_post_find_close2(smb_request_t *sr)
846 {
847 	DTRACE_SMB_1(op__FindClose2__done, smb_request_t *, sr);
848 }
849 
850 smb_sdrc_t
851 smb_com_find_close2(smb_request_t *sr)
852 {
853 	uint16_t	odid;
854 	smb_odir_t	*od;
855 
856 	if (smbsr_decode_vwv(sr, "w", &odid) != 0)
857 		return (SDRC_ERROR);
858 
859 	od = smb_tree_lookup_odir(sr->tid_tree, odid);
860 	if (od == NULL) {
861 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
862 		    ERRDOS, ERROR_INVALID_HANDLE);
863 		return (SDRC_ERROR);
864 	}
865 
866 	smb_odir_close(od);
867 	smb_odir_release(od);
868 
869 	if (smbsr_encode_empty_result(sr))
870 		return (SDRC_ERROR);
871 
872 	return (SDRC_SUCCESS);
873 }
874