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