xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_trans2_find.c (revision c211fc479225fa54805cf480633bf6689ca9a2db)
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 
281 	bzero(&args, sizeof (smb_find_args_t));
282 
283 	if (!STYPE_ISDSK(sr->tid_tree->t_res_type)) {
284 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
285 		    ERRDOS, ERROR_ACCESS_DENIED);
286 		return (SDRC_ERROR);
287 	}
288 
289 	if (smb_mbc_decodef(&xa->req_param_mb, "%wwww4.u", sr, &sattr,
290 	    &args.fa_maxcount, &args.fa_fflag, &args.fa_infolev, &path) != 0) {
291 		return (SDRC_ERROR);
292 	}
293 
294 	if (smb_is_stream_name(path)) {
295 		smbsr_error(sr, NT_STATUS_OBJECT_NAME_INVALID,
296 		    ERRDOS, ERROR_INVALID_NAME);
297 		return (SDRC_ERROR);
298 	}
299 
300 	if (args.fa_fflag & SMB_FIND_WITH_BACKUP_INTENT)
301 		sr->user_cr = smb_user_getprivcred(sr->uid_user);
302 
303 	args.fa_maxdata =
304 	    smb_trans2_find_get_maxdata(sr, args.fa_infolev, args.fa_fflag);
305 	if (args.fa_maxdata == 0)
306 		return (SDRC_ERROR);
307 
308 	if (sr->smb_flg2 & SMB_FLAGS2_UNICODE)
309 		(void) smb_convert_wildcards(path);
310 
311 	odid = smb_odir_open(sr, path, sattr);
312 	if (odid == 0)
313 		return (SDRC_ERROR);
314 
315 	od = smb_tree_lookup_odir(sr->tid_tree, odid);
316 	if (od == NULL)
317 		return (SDRC_ERROR);
318 	count = smb_trans2_find_entries(sr, xa, od, &args, &eos);
319 	smb_odir_release(od);
320 
321 	if (count == -1) {
322 		smb_odir_close(od);
323 		return (SDRC_ERROR);
324 	}
325 
326 	if (count == 0) {
327 		smb_odir_close(od);
328 		smbsr_errno(sr, ENOENT);
329 		return (SDRC_ERROR);
330 	}
331 
332 	if ((args.fa_fflag & SMB_FIND_CLOSE_AFTER_REQUEST) ||
333 	    (eos && (args.fa_fflag & SMB_FIND_CLOSE_AT_EOS))) {
334 		smb_odir_close(od);
335 	} /* else leave odir open for trans2_find_next2 */
336 
337 	(void) smb_mbc_encodef(&xa->rep_param_mb, "wwwww",
338 	    odid, count, (eos) ? 1 : 0, 0, 0);
339 
340 	return (SDRC_SUCCESS);
341 }
342 
343 /*
344  * smb_com_trans2_find_next2
345  *
346  *  Client Request                     Value
347  *  ================================== =================================
348  *
349  *  WordCount                          15
350  *  SetupCount                         1
351  *  Setup[0]                           TRANS2_FIND_NEXT2
352  *
353  *  Parameter Block Encoding           Description
354  *  ================================== =================================
355  *
356  *  USHORT Sid;                        Search handle
357  *  USHORT SearchCount;                Maximum number of entries to
358  *                                      return
359  *  USHORT InformationLevel;           Levels described in
360  *                                      TRANS2_FIND_FIRST2 request
361  *  ULONG ResumeKey;                   Value returned by previous find2
362  *                                      call
363  *  USHORT Flags;                      Additional information: bit set-
364  *                                      0 - close search after this
365  *                                      request
366  *                                      1 - close search if end of search
367  *                                      reached
368  *                                      2 - return resume keys for each
369  *                                      entry found
370  *                                      3 - resume/continue from previous
371  *                                      ending place
372  *                                      4 - find with backup intent
373  *  STRING FileName;                   Resume file name
374  *
375  * Sid is the value returned by a previous successful TRANS2_FIND_FIRST2
376  * call.  If Bit3 of Flags is set, then FileName may be the NULL string,
377  * since the search is continued from the previous TRANS2_FIND request.
378  * Otherwise, FileName must not be more than 256 characters long.
379  *
380  *  Response Field                     Description
381  *  ================================== =================================
382  *
383  *  USHORT SearchCount;                Number of entries returned
384  *  USHORT EndOfSearch;                Was last entry returned?
385  *  USHORT EaErrorOffset;              Offset into EA list if EA error
386  *  USHORT LastNameOffset;             Offset into data to file name of
387  *                                      last entry, if server needs it to
388  *                                      resume search; else 0
389  *  UCHAR Data[TotalDataCount]         Level dependent info about the
390  *                                      matches found in the search
391  *
392  *
393  * The last parameter in the request is a filename, which is a
394  * null-terminated unicode string.
395  *
396  * smb_mbc_decodef(&xa->req_param_mb, "%www lwu", sr,
397  *    &odid, &fa_maxcount, &fa_infolev, &cookie, &fa_fflag, &fname)
398  *
399  * The filename parameter is not currently decoded because we
400  * expect a 2-byte null but Mac OS 10 clients send a 1-byte null,
401  * which leads to a decode error.
402  * Thus, we do not support resume by filename.  We treat a request
403  * to resume by filename as SMB_FIND_CONTINUE_FROM_LAST.
404  */
405 smb_sdrc_t
406 smb_com_trans2_find_next2(smb_request_t *sr, smb_xa_t *xa)
407 {
408 	int			count;
409 	uint16_t		odid;
410 	uint32_t		cookie;
411 	smb_odir_t		*od;
412 	smb_find_args_t		args;
413 	boolean_t		eos;
414 	smb_odir_resume_t	odir_resume;
415 
416 	bzero(&args, sizeof (smb_find_args_t));
417 
418 	if (smb_mbc_decodef(&xa->req_param_mb, "%wwwlw", sr, &odid,
419 	    &args.fa_maxcount, &args.fa_infolev, &cookie, &args.fa_fflag)
420 	    != 0) {
421 		return (SDRC_ERROR);
422 	}
423 
424 	/* continuation by filename not supported */
425 	if ((args.fa_fflag & SMB_FIND_CONTINUE_FROM_LAST) || (cookie == 0)) {
426 		odir_resume.or_type = SMB_ODIR_RESUME_IDX;
427 		odir_resume.or_idx = 0;
428 	} else {
429 		odir_resume.or_type = SMB_ODIR_RESUME_COOKIE;
430 		odir_resume.or_cookie = cookie;
431 	}
432 
433 	if (args.fa_fflag & SMB_FIND_WITH_BACKUP_INTENT)
434 		sr->user_cr = smb_user_getprivcred(sr->uid_user);
435 
436 	args.fa_maxdata =
437 	    smb_trans2_find_get_maxdata(sr, args.fa_infolev, args.fa_fflag);
438 	if (args.fa_maxdata == 0)
439 		return (SDRC_ERROR);
440 
441 	od = smb_tree_lookup_odir(sr->tid_tree, odid);
442 	if (od == NULL) {
443 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
444 		    ERRDOS, ERROR_INVALID_HANDLE);
445 		return (SDRC_ERROR);
446 	}
447 	smb_odir_resume_at(od, &odir_resume);
448 	count = smb_trans2_find_entries(sr, xa, od, &args, &eos);
449 	smb_odir_release(od);
450 
451 	if (count == -1) {
452 		smb_odir_close(od);
453 		return (SDRC_ERROR);
454 	}
455 
456 	if ((args.fa_fflag & SMB_FIND_CLOSE_AFTER_REQUEST) ||
457 	    (eos && (args.fa_fflag & SMB_FIND_CLOSE_AT_EOS))) {
458 		smb_odir_close(od);
459 	} /* else leave odir open for trans2_find_next2 */
460 
461 	(void) smb_mbc_encodef(&xa->rep_param_mb, "wwww",
462 	    count, (eos) ? 1 : 0, 0, 0);
463 
464 	return (SDRC_SUCCESS);
465 }
466 
467 
468 /*
469  * smb_trans2_find_entries
470  *
471  * Find and encode up to args->fa_maxcount directory entries.
472  * For compatibilty with Windows, if args->fa_maxcount is zero treat it as 1.
473  *
474  * Returns:
475  *   count - count of entries encoded
476  *           *eos = B_TRUE if no more directory entries
477  *      -1 - error
478  */
479 static int
480 smb_trans2_find_entries(smb_request_t *sr, smb_xa_t *xa, smb_odir_t *od,
481     smb_find_args_t *args, boolean_t *eos)
482 {
483 	int		rc;
484 	uint16_t	count, maxcount;
485 	uint32_t	cookie;
486 	smb_fileinfo_t	fileinfo;
487 
488 	if ((maxcount = args->fa_maxcount) == 0)
489 		maxcount = 1;
490 
491 	if ((smb_trans2_find_max != 0) && (maxcount > smb_trans2_find_max))
492 		maxcount = smb_trans2_find_max;
493 
494 	count = 0;
495 	while (count < maxcount) {
496 		if (smb_odir_read_fileinfo(sr, od, &fileinfo, eos) != 0)
497 			return (-1);
498 		if (*eos == B_TRUE)
499 			break;
500 
501 		rc = smb_trans2_find_mbc_encode(sr, xa, &fileinfo, args);
502 		if (rc == -1)
503 			return (-1);
504 		if (rc == 1)
505 			break;
506 
507 		cookie = fileinfo.fi_cookie;
508 		++count;
509 	}
510 
511 	/* save the last cookie returned to client */
512 	if (count != 0)
513 		smb_odir_save_cookie(od, 0, cookie);
514 
515 	/* if eos not already detected, check if more entries */
516 	if (!*eos)
517 		(void) smb_odir_read_fileinfo(sr, od, &fileinfo, eos);
518 
519 	return (count);
520 }
521 
522 /*
523  * smb_trans2_find_get_maxdata
524  *
525  * Calculate the minimum response space required for the specified
526  * information level.
527  *
528  * A non-zero return value provides the minimum space required.
529  * A return value of zero indicates an unknown information level.
530  */
531 static int
532 smb_trans2_find_get_maxdata(smb_request_t *sr, uint16_t infolev, uint16_t fflag)
533 {
534 	int maxdata;
535 
536 	maxdata = smb_ascii_or_unicode_null_len(sr);
537 
538 	switch (infolev) {
539 	case SMB_INFO_STANDARD :
540 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
541 			maxdata += sizeof (int32_t);
542 		maxdata += 2 + 2 + 2 + 4 + 4 + 2 + 1;
543 		break;
544 
545 	case SMB_INFO_QUERY_EA_SIZE:
546 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
547 			maxdata += sizeof (int32_t);
548 		maxdata += 2 + 2 + 2 + 4 + 4 + 2 + 4 + 1;
549 		break;
550 
551 	case SMB_FIND_FILE_DIRECTORY_INFO:
552 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4;
553 		break;
554 
555 	case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
556 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4 + 4;
557 		break;
558 
559 	case SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO:
560 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4 + 4 + 4 + 8;
561 		break;
562 
563 	case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
564 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4 + 4 + 2 + 24;
565 		break;
566 
567 	case SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO:
568 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4 + 4 + 2 + 24
569 		    + 2 + 8;
570 		break;
571 
572 	case SMB_FIND_FILE_NAMES_INFO:
573 		maxdata += 4 + 4 + 4;
574 		break;
575 
576 	case SMB_MAC_FIND_BOTH_HFS_INFO:
577 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 1 + 1 + 2 +
578 		    4 + 32 + 4 + 1 + 1 + 24 + 4;
579 		break;
580 
581 	default:
582 		maxdata = 0;
583 		smbsr_error(sr, NT_STATUS_INVALID_LEVEL,
584 		    ERRDOS, ERROR_INVALID_LEVEL);
585 	}
586 
587 	return (maxdata);
588 }
589 
590 /*
591  * smb_trans2_mbc_encode
592  *
593  * This function encodes the mbc for one directory entry.
594  *
595  * The function returns -1 when the max data requested by client
596  * is reached. If the entry is valid and successful encoded, 0
597  * will be returned; otherwise, 1 will be returned.
598  *
599  * We always null terminate the filename. The space for the null
600  * is included in the maxdata calculation and is therefore included
601  * in the next_entry_offset. namelen is the unterminated length of
602  * the filename. For levels except STANDARD and EA_SIZE, if the
603  * filename is ascii the name length returned to the client should
604  * include the null terminator. Otherwise the length returned to
605  * the client should not include the terminator.
606  *
607  * Returns: 0 - data successfully encoded
608  *          1 - client request's maxdata limit reached
609  *	   -1 - error
610  */
611 static int
612 smb_trans2_find_mbc_encode(smb_request_t *sr, smb_xa_t *xa,
613     smb_fileinfo_t *fileinfo, smb_find_args_t *args)
614 {
615 	int		namelen, shortlen, buflen;
616 	uint32_t	next_entry_offset;
617 	uint32_t	dsize32, asize32;
618 	uint32_t	mb_flags = 0;
619 	char		buf83[26];
620 	char		*tmpbuf;
621 	smb_msgbuf_t	mb;
622 
623 	namelen = smb_ascii_or_unicode_strlen(sr, fileinfo->fi_name);
624 	if (namelen == -1)
625 		return (-1);
626 
627 	next_entry_offset = args->fa_maxdata + namelen;
628 
629 	if (MBC_ROOM_FOR(&xa->rep_data_mb, (args->fa_maxdata + namelen)) == 0)
630 		return (1);
631 
632 	/*
633 	 * If ascii the filename length returned to the client should
634 	 * include the null terminator for levels except STANDARD and
635 	 * EASIZE.
636 	 */
637 	if (!(sr->smb_flg2 & SMB_FLAGS2_UNICODE)) {
638 		if ((args->fa_infolev != SMB_INFO_STANDARD) &&
639 		    (args->fa_infolev != SMB_INFO_QUERY_EA_SIZE))
640 			namelen += 1;
641 	}
642 
643 	mb_flags = (sr->smb_flg2 & SMB_FLAGS2_UNICODE) ? SMB_MSGBUF_UNICODE : 0;
644 	dsize32 = (fileinfo->fi_size > UINT_MAX) ?
645 	    UINT_MAX : (uint32_t)fileinfo->fi_size;
646 	asize32 = (fileinfo->fi_alloc_size > UINT_MAX) ?
647 	    UINT_MAX : (uint32_t)fileinfo->fi_alloc_size;
648 
649 	switch (args->fa_infolev) {
650 	case SMB_INFO_STANDARD:
651 		if (args->fa_fflag & SMB_FIND_RETURN_RESUME_KEYS)
652 			(void) smb_mbc_encodef(&xa->rep_data_mb, "l",
653 			    fileinfo->fi_cookie);
654 
655 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%yyyllwbu", sr,
656 		    smb_gmt2local(sr, fileinfo->fi_crtime.tv_sec),
657 		    smb_gmt2local(sr, fileinfo->fi_atime.tv_sec),
658 		    smb_gmt2local(sr, fileinfo->fi_mtime.tv_sec),
659 		    dsize32,
660 		    asize32,
661 		    fileinfo->fi_dosattr,
662 		    namelen,
663 		    fileinfo->fi_name);
664 		break;
665 
666 	case SMB_INFO_QUERY_EA_SIZE:
667 		if (args->fa_fflag & SMB_FIND_RETURN_RESUME_KEYS)
668 			(void) smb_mbc_encodef(&xa->rep_data_mb, "l",
669 			    fileinfo->fi_cookie);
670 
671 		/*
672 		 * Unicode filename should NOT be aligned. Encode ('u')
673 		 * into a temporary buffer, then encode buffer as a
674 		 * byte stream ('#c').
675 		 * Regardless of whether unicode or ascii, a single
676 		 * termination byte is used.
677 		 */
678 		buflen = namelen + sizeof (mts_wchar_t);
679 		tmpbuf = kmem_zalloc(buflen, KM_SLEEP);
680 		smb_msgbuf_init(&mb, (uint8_t *)tmpbuf, buflen, mb_flags);
681 		if (smb_msgbuf_encode(&mb, "u", fileinfo->fi_name) < 0) {
682 			smb_msgbuf_term(&mb);
683 			kmem_free(tmpbuf, buflen);
684 			return (-1);
685 		}
686 		tmpbuf[namelen] = '\0';
687 
688 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%yyyllwlb#c", sr,
689 		    smb_gmt2local(sr, fileinfo->fi_crtime.tv_sec),
690 		    smb_gmt2local(sr, fileinfo->fi_atime.tv_sec),
691 		    smb_gmt2local(sr, fileinfo->fi_mtime.tv_sec),
692 		    dsize32,
693 		    asize32,
694 		    fileinfo->fi_dosattr,
695 		    0L,		/* EA Size */
696 		    namelen,
697 		    namelen + 1,
698 		    tmpbuf);
699 
700 		smb_msgbuf_term(&mb);
701 		kmem_free(tmpbuf, buflen);
702 		break;
703 
704 	case SMB_FIND_FILE_DIRECTORY_INFO:
705 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqllu", sr,
706 		    next_entry_offset,
707 		    fileinfo->fi_cookie,
708 		    &fileinfo->fi_crtime,
709 		    &fileinfo->fi_atime,
710 		    &fileinfo->fi_mtime,
711 		    &fileinfo->fi_ctime,
712 		    fileinfo->fi_size,
713 		    fileinfo->fi_alloc_size,
714 		    fileinfo->fi_dosattr,
715 		    namelen,
716 		    fileinfo->fi_name);
717 		break;
718 
719 	case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
720 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqlllu", sr,
721 		    next_entry_offset,
722 		    fileinfo->fi_cookie,
723 		    &fileinfo->fi_crtime,
724 		    &fileinfo->fi_atime,
725 		    &fileinfo->fi_mtime,
726 		    &fileinfo->fi_ctime,
727 		    fileinfo->fi_size,
728 		    fileinfo->fi_alloc_size,
729 		    fileinfo->fi_dosattr,
730 		    namelen,
731 		    0L,
732 		    fileinfo->fi_name);
733 		break;
734 
735 	case SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO:
736 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqlll4.qu", sr,
737 		    next_entry_offset,
738 		    fileinfo->fi_cookie,
739 		    &fileinfo->fi_crtime,
740 		    &fileinfo->fi_atime,
741 		    &fileinfo->fi_mtime,
742 		    &fileinfo->fi_ctime,
743 		    fileinfo->fi_size,
744 		    fileinfo->fi_alloc_size,
745 		    fileinfo->fi_dosattr,
746 		    namelen,
747 		    0L,
748 		    fileinfo->fi_nodeid,
749 		    fileinfo->fi_name);
750 		break;
751 
752 	case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
753 		bzero(buf83, sizeof (buf83));
754 		smb_msgbuf_init(&mb, (uint8_t *)buf83, sizeof (buf83),
755 		    mb_flags);
756 		if (smb_msgbuf_encode(&mb, "U", fileinfo->fi_shortname) < 0) {
757 			smb_msgbuf_term(&mb);
758 			return (-1);
759 		}
760 		shortlen = mts_wcequiv_strlen(fileinfo->fi_shortname);
761 
762 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqlllb.24cu",
763 		    sr,
764 		    next_entry_offset,
765 		    fileinfo->fi_cookie,
766 		    &fileinfo->fi_crtime,
767 		    &fileinfo->fi_atime,
768 		    &fileinfo->fi_mtime,
769 		    &fileinfo->fi_ctime,
770 		    fileinfo->fi_size,
771 		    fileinfo->fi_alloc_size,
772 		    fileinfo->fi_dosattr,
773 		    namelen,
774 		    0L,
775 		    shortlen,
776 		    buf83,
777 		    fileinfo->fi_name);
778 
779 		smb_msgbuf_term(&mb);
780 		break;
781 
782 	case SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO:
783 		bzero(buf83, sizeof (buf83));
784 		smb_msgbuf_init(&mb, (uint8_t *)buf83, sizeof (buf83),
785 		    mb_flags);
786 		if (smb_msgbuf_encode(&mb, "u", fileinfo->fi_shortname) < 0) {
787 			smb_msgbuf_term(&mb);
788 			return (-1);
789 		}
790 		shortlen = smb_ascii_or_unicode_strlen(sr,
791 		    fileinfo->fi_shortname);
792 
793 		(void) smb_mbc_encodef(&xa->rep_data_mb,
794 		    "%llTTTTqqlllb.24c2.qu",
795 		    sr,
796 		    next_entry_offset,
797 		    fileinfo->fi_cookie,
798 		    &fileinfo->fi_crtime,
799 		    &fileinfo->fi_atime,
800 		    &fileinfo->fi_mtime,
801 		    &fileinfo->fi_ctime,
802 		    fileinfo->fi_size,
803 		    fileinfo->fi_alloc_size,
804 		    fileinfo->fi_dosattr,
805 		    namelen,
806 		    0L,
807 		    shortlen,
808 		    buf83,
809 		    fileinfo->fi_nodeid,
810 		    fileinfo->fi_name);
811 
812 		smb_msgbuf_term(&mb);
813 		break;
814 
815 	case SMB_FIND_FILE_NAMES_INFO:
816 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%lllu", sr,
817 		    next_entry_offset,
818 		    fileinfo->fi_cookie,
819 		    namelen,
820 		    fileinfo->fi_name);
821 		break;
822 	}
823 
824 	return (0);
825 }
826 
827 /*
828  * Close a search started by a Trans2FindFirst2 request.
829  */
830 smb_sdrc_t
831 smb_pre_find_close2(smb_request_t *sr)
832 {
833 	DTRACE_SMB_1(op__FindClose2__start, smb_request_t *, sr);
834 	return (SDRC_SUCCESS);
835 }
836 
837 void
838 smb_post_find_close2(smb_request_t *sr)
839 {
840 	DTRACE_SMB_1(op__FindClose2__done, smb_request_t *, sr);
841 }
842 
843 smb_sdrc_t
844 smb_com_find_close2(smb_request_t *sr)
845 {
846 	uint16_t	odid;
847 	smb_odir_t	*od;
848 
849 	if (smbsr_decode_vwv(sr, "w", &odid) != 0)
850 		return (SDRC_ERROR);
851 
852 	od = smb_tree_lookup_odir(sr->tid_tree, odid);
853 	if (od == NULL) {
854 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
855 		    ERRDOS, ERROR_INVALID_HANDLE);
856 		return (SDRC_ERROR);
857 	}
858 
859 	smb_odir_close(od);
860 	smb_odir_release(od);
861 
862 	if (smbsr_encode_empty_result(sr))
863 		return (SDRC_ERROR);
864 
865 	return (SDRC_SUCCESS);
866 }
867