xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_trans2_find.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"@(#)smb_trans2_find.c	1.13	08/08/07 SMI"
27 
28 /*
29  * This module provides functions for TRANS2_FIND_FIRST2 and
30  * TRANS2_FIND_NEXT2 requests. The requests allow the client to search
31  * for the file(s) which match the file specification.  The search is
32  * started with TRANS2_FIND_FIRST2 and can be continued if necessary with
33  * TRANS2_FIND_NEXT2. There are numerous levels of information which may be
34  * obtained for the returned files, the desired level is specified in the
35  * InformationLevel field of the requests.
36  *
37  *  InformationLevel Name              Value
38  *  =================================  ================
39  *
40  *  SMB_INFO_STANDARD                  1
41  *  SMB_INFO_QUERY_EA_SIZE             2
42  *  SMB_INFO_QUERY_EAS_FROM_LIST       3
43  *  SMB_FIND_FILE_DIRECTORY_INFO       0x101
44  *  SMB_FIND_FILE_FULL_DIRECTORY_INFO  0x102
45  *  SMB_FIND_FILE_NAMES_INFO           0x103
46  *  SMB_FIND_FILE_BOTH_DIRECTORY_INFO  0x104
47  *
48  * The following sections detail the data returned for each
49  * InformationLevel. The requested information is placed in the Data
50  * portion of the transaction response. Note: a client which does not
51  * support long names can only request SMB_INFO_STANDARD.
52  *
53  * A four-byte resume key precedes each data item (described below) if bit
54  * 2 in the Flags field is set, i.e. if the request indicates the server
55  * should return resume keys. Note: it is not always the case. If the
56  * data item already includes the resume key, the resume key should not be
57  * added again.
58  *
59  * 4.3.4.1   SMB_INFO_STANDARD
60  *
61  *  Response Field                    Description
62  *  ================================  ==================================
63  *
64  *  SMB_DATE CreationDate;            Date when file was created
65  *  SMB_TIME CreationTime;            Time when file was created
66  *  SMB_DATE LastAccessDate;          Date of last file access
67  *  SMB_TIME LastAccessTime;          Time of last file access
68  *  SMB_DATE LastWriteDate;           Date of last write to the file
69  *  SMB_TIME LastWriteTime;           Time of last write to the file
70  *  ULONG  DataSize;                  File Size
71  *  ULONG AllocationSize;             Size of filesystem allocation unit
72  *  USHORT Attributes;                File Attributes
73  *  UCHAR FileNameLength;             Length of filename in bytes
74  *  STRING FileName;                  Name of found file
75  *
76  * 4.3.4.2   SMB_INFO_QUERY_EA_SIZE
77  *
78  *  Response Field                     Description
79  *  =================================  ==================================
80  *
81  *   SMB_DATE CreationDate;            Date when file was created
82  *   SMB_TIME CreationTime;            Time when file was created
83  *   SMB_DATE LastAccessDate;          Date of last file access
84  *   SMB_TIME LastAccessTime;          Time of last file access
85  *   SMB_DATE LastWriteDate;           Date of last write to the file
86  *   SMB_TIME LastWriteTime;           Time of last write to the file
87  *   ULONG DataSize;                   File Size
88  *   ULONG AllocationSize;             Size of filesystem allocation unit
89  *   USHORT Attributes;                File Attributes
90  *   ULONG EaSize;                     Size of file's EA information
91  *   UCHAR FileNameLength;             Length of filename in bytes
92  *   STRING FileName;                  Name of found file
93  *
94  * 4.3.4.3   SMB_INFO_QUERY_EAS_FROM_LIST
95  *
96  * This request returns the same information as SMB_INFO_QUERY_EA_SIZE, but
97  * only for files which have an EA list which match the EA information in
98  * the Data part of the request.
99  *
100  * 4.3.4.4   SMB_FIND_FILE_DIRECTORY_INFO
101  *
102  *  Response Field                     Description
103  *  =================================  ==================================
104  *
105  *  ULONG NextEntryOffset;             Offset from this structure to
106  *					beginning of next one
107  *  ULONG FileIndex;
108  *  LARGE_INTEGER CreationTime;        file creation time
109  *  LARGE_INTEGER LastAccessTime;      last access time
110  *  LARGE_INTEGER LastWriteTime;       last write time
111  *  LARGE_INTEGER ChangeTime;          last attribute change time
112  *  LARGE_INTEGER EndOfFile;           file size
113  *  LARGE_INTEGER AllocationSize;      size of filesystem allocation information
114  *  ULONG ExtFileAttributes;           Extended file attributes
115  *					(see section 3.11)
116  *  ULONG FileNameLength;              Length of filename in bytes
117  *  STRING FileName;                   Name of the file
118  *
119  * 4.3.4.5   SMB_FIND_FILE_FULL_DIRECTORY_INFO
120  *
121  *  Response Field                     Description
122  *  =================================  ==================================
123  *
124  *  ULONG NextEntryOffset;             Offset from this structure to
125  *					beginning of next one
126  *  ULONG FileIndex;
127  *  LARGE_INTEGER CreationTime;        file creation time
128  *  LARGE_INTEGER LastAccessTime;      last access time
129  *  LARGE_INTEGER LastWriteTime;       last write time
130  *  LARGE_INTEGER ChangeTime;          last attribute change time
131  *  LARGE_INTEGER EndOfFile;           file size
132  *  LARGE_INTEGER AllocationSize;      size of filesystem allocation information
133  *  ULONG ExtFileAttributes;           Extended file attributes
134  *					(see section 3.11)
135  *  ULONG FileNameLength;              Length of filename in bytes
136  *  ULONG EaSize;                      Size of file's extended attributes
137  *  STRING FileName;                   Name of the file
138  *
139  * 4.3.4.6   SMB_FIND_FILE_BOTH_DIRECTORY_INFO
140  *
141  *  Response Field                     Description
142  *  =================================  ==================================
143  *
144  *  ULONG NextEntryOffset;             Offset from this structure to
145  *					beginning of next one
146  *  ULONG FileIndex;
147  *  LARGE_INTEGER CreationTime;        file creation time
148  *  LARGE_INTEGER LastAccessTime;      last access time
149  *  LARGE_INTEGER LastWriteTime;       last write time
150  *  LARGE_INTEGER ChangeTime;          last attribute change time
151  *  LARGE_INTEGER EndOfFile;           file size
152  *  LARGE_INTEGER AllocationSize;      size of filesystem allocation information
153  *  ULONG ExtFileAttributes;           Extended file attributes
154  *					(see section 3.11)
155  *  ULONG FileNameLength;              Length of FileName in bytes
156  *  ULONG EaSize;                      Size of file's extended attributes
157  *  UCHAR ShortNameLength;             Length of file's short name in bytes
158  *  UCHAR Reserved
159  *  WCHAR ShortName[12];               File's 8.3 conformant name in Unicode
160  *  STRING FileName;                   Files full length name
161  *
162  * 4.3.4.7   SMB_FIND_FILE_NAMES_INFO
163  *
164  *  Response Field                     Description
165  *  =================================  ==================================
166  *
167  *  ULONG NextEntryOffset;             Offset from this structure to
168  *                                     beginning of next one
169  *  ULONG FileIndex;
170  *  ULONG FileNameLength;              Length of FileName in bytes
171  *  STRING FileName;                   Files full length name
172  */
173 
174 #include <smbsrv/smb_incl.h>
175 #include <smbsrv/msgbuf.h>
176 #include <smbsrv/smbtrans.h>
177 #include <smbsrv/smb_fsops.h>
178 
179 static int smb_trans2_find_get_maxdata(smb_request_t *, uint16_t, uint16_t);
180 
181 int smb_trans2_find_get_dents(smb_request_t *, smb_xa_t *,
182     uint16_t, uint16_t, int, smb_node_t *,
183     uint16_t, uint16_t, int, char *, uint32_t *, int *, int *);
184 
185 int smb_gather_dents_info(char *, ino_t, int, char *, uint32_t, int32_t *,
186     smb_attr_t *, smb_node_t *, char *, char *);
187 
188 int smb_trans2_find_process_ients(smb_request_t *, smb_xa_t *,
189     smb_dent_info_hdr_t *, uint16_t, uint16_t, int,
190     smb_node_t *, int *, uint32_t *);
191 
192 int smb_trans2_find_mbc_encode(smb_request_t *, smb_xa_t *,
193     smb_dent_info_t *, int, uint16_t, uint16_t,
194     uint32_t, smb_node_t *, smb_node_t *);
195 
196 /*
197  * The UNIX characters below are considered illegal in Windows file names.
198  * The following character conversions are used to support sites in which
199  * Catia v4 is in use on UNIX and Catia v5 is in use on Windows.
200  *
201  * ---------------------------
202  * Unix-char	| Windows-char
203  * ---------------------------
204  *   "		| (0x00a8) Diaeresis
205  *   *		| (0x00a4) Currency Sign
206  *   :		| (0x00f7) Division Sign
207  *   <		| (0x00ab) Left-Pointing Double Angle Quotation Mark
208  *   >		| (0x00bb) Right-Pointing Double Angle Quotation Mark
209  *   ?		| (0x00bf) Inverted Question mark
210  *   \		| (0x00ff) Latin Small Letter Y with Diaeresis
211  *   |		| (0x00a6) Broken Bar
212  */
213 static int (*catia_callback)(uint8_t *, uint8_t *, int) = NULL;
214 void smb_register_catia_callback(
215     int (*catia_v4tov5)(uint8_t *, uint8_t *, int));
216 void smb_unregister_catia_callback();
217 
218 /*
219  * Tunable parameter to limit the maximum
220  * number of entries to be returned.
221  */
222 uint16_t smb_trans2_find_max = 128;
223 
224 /*
225  * smb_register_catia_callback
226  *
227  * This function will be invoked by the catia module to register its
228  * function that translates filename in version 4 to a format that is
229  * compatible to version 5.
230  */
231 void
232 smb_register_catia_callback(
233     int (*catia_v4tov5)(uint8_t *, uint8_t *, int))
234 {
235 	catia_callback = catia_v4tov5;
236 }
237 
238 /*
239  * smb_unregister_catia_callback
240  *
241  * This function will unregister the catia callback prior to the catia
242  * module gets unloaded.
243  */
244 void
245 smb_unregister_catia_callback()
246 {
247 	catia_callback = 0;
248 }
249 
250 /*
251  * smb_com_trans2_find_first2
252  *
253  *  Client Request                Value
254  *  ============================  ==================================
255  *
256  *  UCHAR  WordCount              15
257  *  UCHAR  TotalDataCount         Total size of extended attribute list
258  *  UCHAR  SetupCount             1
259  *  UCHAR  Setup[0]               TRANS2_FIND_FIRST2
260  *
261  *  Parameter Block Encoding      Description
262  *  ============================  ==================================
263  *  USHORT SearchAttributes;
264  *  USHORT SearchCount;           Maximum number of entries to return
265  *  USHORT Flags;                 Additional information:
266  *                                Bit 0 - close search after this request
267  *                                Bit 1 - close search if end of search
268  *                                reached
269  *                                Bit 2 - return resume keys for each
270  *                                entry found
271  *                                Bit 3 - continue search from previous
272  *                                ending place
273  *                                Bit 4 - find with backup intent
274  *  USHORT InformationLevel;      See below
275  *  ULONG SearchStorageType;
276  *  STRING FileName;              Pattern for the search
277  *  UCHAR Data[ TotalDataCount ]  FEAList if InformationLevel is
278  *                                QUERY_EAS_FROM_LIST
279  *
280  *  Response Parameter Block      Description
281  *  ============================  ==================================
282  *
283  *  USHORT Sid;                   Search handle
284  *  USHORT SearchCount;           Number of entries returned
285  *  USHORT EndOfSearch;           Was last entry returned?
286  *  USHORT EaErrorOffset;         Offset into EA list if EA error
287  *  USHORT LastNameOffset;        Offset into data to file name of last
288  *                                entry, if server needs it to resume
289  *                                search; else 0
290  *  UCHAR Data[ TotalDataCount ]  Level dependent info about the matches
291  *                                found in the search
292  */
293 smb_sdrc_t
294 smb_com_trans2_find_first2(smb_request_t *sr, smb_xa_t *xa)
295 {
296 	int		more = 0, rc;
297 	uint16_t	sattr, fflag, infolev;
298 	uint16_t	maxcount = 0;
299 	int		maxdata;
300 	int		count, wildcards;
301 	uint32_t	cookie;
302 	char		*path;
303 	smb_node_t	*dir_snode;
304 	char		*pattern;
305 	uint16_t	sid;
306 
307 	if (!STYPE_ISDSK(sr->tid_tree->t_res_type)) {
308 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
309 		    ERRDOS, ERROR_ACCESS_DENIED);
310 		return (SDRC_ERROR);
311 	}
312 
313 	if (smb_mbc_decodef(&xa->req_param_mb, "%wwww4.u", sr,
314 	    &sattr, &maxcount, &fflag, &infolev, &path) != 0) {
315 		return (SDRC_ERROR);
316 	}
317 
318 	maxdata = smb_trans2_find_get_maxdata(sr, infolev, fflag);
319 	if (maxdata == 0) {
320 		smbsr_error(sr, NT_STATUS_INVALID_LEVEL,
321 		    ERRDOS, ERROR_INVALID_LEVEL);
322 		return (SDRC_ERROR);
323 	}
324 
325 	/*
326 	 * When maxcount is zero Windows behaves as if it was 1.
327 	 */
328 	if (maxcount == 0)
329 		maxcount = 1;
330 
331 	if ((smb_trans2_find_max != 0) && (maxcount > smb_trans2_find_max))
332 		maxcount = smb_trans2_find_max;
333 
334 	if (sr->smb_flg2 & SMB_FLAGS2_UNICODE)
335 		(void) smb_convert_unicode_wildcards(path);
336 
337 	if (smb_rdir_open(sr, path, sattr) != 0)
338 		return (SDRC_ERROR);
339 
340 	/*
341 	 * Get a copy of information
342 	 */
343 	dir_snode = sr->sid_odir->d_dir_snode;
344 	pattern = kmem_alloc(MAXNAMELEN, KM_SLEEP);
345 	(void) strcpy(pattern, sr->sid_odir->d_pattern);
346 
347 	if (strcmp(pattern, "*.*") == 0)
348 		(void) strncpy(pattern, "*", sizeof (pattern));
349 
350 	wildcards = sr->sid_odir->d_wildcards;
351 	sattr = sr->sid_odir->d_sattr;
352 	cookie = 0;
353 
354 	rc = smb_trans2_find_get_dents(sr, xa, fflag, infolev, maxdata,
355 	    dir_snode, sattr, maxcount, wildcards,
356 	    pattern, &cookie, &more, &count);
357 
358 	if (!count)
359 		rc = ENOENT;
360 
361 	if (rc) {
362 		smb_rdir_close(sr);
363 		kmem_free(pattern, MAXNAMELEN);
364 		smbsr_errno(sr, rc);
365 		return (SDRC_ERROR);
366 	}
367 
368 	/*
369 	 * Save the sid here in case the search is closed below,
370 	 * which will invalidate sr->smb_sid.  We return the
371 	 * sid, even though the search has been closed, to be
372 	 * compatible with Windows.
373 	 */
374 	sid = sr->smb_sid;
375 
376 	if (fflag & SMB_FIND_CLOSE_AFTER_REQUEST ||
377 	    (!more && fflag & SMB_FIND_CLOSE_AT_EOS)) {
378 		smb_rdir_close(sr);
379 	} else {
380 		mutex_enter(&sr->sid_odir->d_mutex);
381 		sr->sid_odir->d_cookie = cookie;
382 		mutex_exit(&sr->sid_odir->d_mutex);
383 	}
384 
385 	(void) smb_mbc_encodef(&xa->rep_param_mb, "wwwww",
386 	    sid, count, (more ? 0 : 1), 0, 0);
387 
388 	kmem_free(pattern, MAXNAMELEN);
389 	return (SDRC_SUCCESS);
390 }
391 
392 /*
393  * smb_com_trans2_find_next2
394  *
395  *  Client Request                     Value
396  *  ================================== =================================
397  *
398  *  WordCount                          15
399  *  SetupCount                         1
400  *  Setup[0]                           TRANS2_FIND_NEXT2
401  *
402  *  Parameter Block Encoding           Description
403  *  ================================== =================================
404  *
405  *  USHORT Sid;                        Search handle
406  *  USHORT SearchCount;                Maximum number of entries to
407  *                                      return
408  *  USHORT InformationLevel;           Levels described in
409  *                                      TRANS2_FIND_FIRST2 request
410  *  ULONG ResumeKey;                   Value returned by previous find2
411  *                                      call
412  *  USHORT Flags;                      Additional information: bit set-
413  *                                      0 - close search after this
414  *                                      request
415  *                                      1 - close search if end of search
416  *                                      reached
417  *                                      2 - return resume keys for each
418  *                                      entry found
419  *                                      3 - resume/continue from previous
420  *                                      ending place
421  *                                      4 - find with backup intent
422  *  STRING FileName;                   Resume file name
423  *
424  * Sid is the value returned by a previous successful TRANS2_FIND_FIRST2
425  * call.  If Bit3 of Flags is set, then FileName may be the NULL string,
426  * since the search is continued from the previous TRANS2_FIND request.
427  * Otherwise, FileName must not be more than 256 characters long.
428  *
429  *  Response Field                     Description
430  *  ================================== =================================
431  *
432  *  USHORT SearchCount;                Number of entries returned
433  *  USHORT EndOfSearch;                Was last entry returned?
434  *  USHORT EaErrorOffset;              Offset into EA list if EA error
435  *  USHORT LastNameOffset;             Offset into data to file name of
436  *                                      last entry, if server needs it to
437  *                                      resume search; else 0
438  *  UCHAR Data[TotalDataCount]         Level dependent info about the
439  *                                      matches found in the search
440  */
441 smb_sdrc_t
442 smb_com_trans2_find_next2(smb_request_t *sr, smb_xa_t *xa)
443 {
444 	uint16_t fflag, infolev;
445 	int	maxdata, count, wildcards, more = 0, rc;
446 	uint32_t cookie;
447 	uint16_t maxcount = 0;
448 	smb_node_t *dir_snode;
449 	char *pattern;
450 	uint16_t sattr;
451 
452 	/*
453 	 * The last parameter in the request is a path, which is a
454 	 * null-terminated unicode string.
455 	 *
456 	 * smb_mbc_decodef(&xa->req_param_mb, "%www lwu", sr,
457 	 *    &sr->smb_sid, &maxcount, &infolev, &cookie, &fflag, &path)
458 	 *
459 	 * We don't reference this parameter and it is not currently
460 	 * decoded because we a expect 2-byte null but Mac OS 10
461 	 * clients send a 1-byte null, which leads to a decode error.
462 	 */
463 	if (smb_mbc_decodef(&xa->req_param_mb, "%wwwlw", sr,
464 	    &sr->smb_sid, &maxcount, &infolev, &cookie, &fflag) != 0) {
465 		return (SDRC_ERROR);
466 	}
467 
468 	sr->sid_odir = smb_odir_lookup_by_sid(sr->tid_tree, sr->smb_sid);
469 	if (sr->sid_odir == NULL) {
470 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
471 		return (SDRC_ERROR);
472 	}
473 
474 	maxdata = smb_trans2_find_get_maxdata(sr, infolev, fflag);
475 	if (maxdata == 0) {
476 		smb_rdir_close(sr);
477 		smbsr_error(sr, NT_STATUS_INVALID_LEVEL,
478 		    ERRDOS, ERROR_INVALID_LEVEL);
479 		return (SDRC_ERROR);
480 	}
481 
482 	/*
483 	 * When maxcount is zero Windows behaves as if it was 1.
484 	 */
485 	if (maxcount == 0)
486 		maxcount = 1;
487 
488 	if ((smb_trans2_find_max != 0) && (maxcount > smb_trans2_find_max))
489 		maxcount = smb_trans2_find_max;
490 
491 	/*
492 	 * Get a copy of information
493 	 */
494 	dir_snode = sr->sid_odir->d_dir_snode;
495 	pattern = kmem_alloc(MAXNAMELEN, KM_SLEEP);
496 	(void) strcpy(pattern, sr->sid_odir->d_pattern);
497 	wildcards = sr->sid_odir->d_wildcards;
498 	sattr = sr->sid_odir->d_sattr;
499 	if (fflag & SMB_FIND_CONTINUE_FROM_LAST) {
500 		mutex_enter(&sr->sid_odir->d_mutex);
501 		cookie = sr->sid_odir->d_cookie;
502 		mutex_exit(&sr->sid_odir->d_mutex);
503 	}
504 
505 	rc = smb_trans2_find_get_dents(sr, xa, fflag, infolev, maxdata,
506 	    dir_snode, sattr, maxcount, wildcards, pattern, &cookie,
507 	    &more, &count);
508 
509 	if (rc) {
510 		smb_rdir_close(sr);
511 		kmem_free(pattern, MAXNAMELEN);
512 		smbsr_errno(sr, rc);
513 		return (SDRC_ERROR);
514 	}
515 
516 	if (fflag & SMB_FIND_CLOSE_AFTER_REQUEST ||
517 	    (!more && fflag & SMB_FIND_CLOSE_AT_EOS))
518 		smb_rdir_close(sr);
519 	else {
520 		mutex_enter(&sr->sid_odir->d_mutex);
521 		sr->sid_odir->d_cookie = cookie;
522 		mutex_exit(&sr->sid_odir->d_mutex);
523 	}
524 
525 	(void) smb_mbc_encodef(&xa->rep_param_mb, "wwww",
526 	    count, (more ? 0 : 1), 0, 0);
527 
528 	kmem_free(pattern, MAXNAMELEN);
529 	return (SDRC_SUCCESS);
530 }
531 
532 /*
533  * smb_trans2_find_get_maxdata
534  *
535  * Calculate the minimum response space required for the specified
536  * information level.
537  *
538  * A non-zero return value provides the minimum space required.
539  * A return value of zero indicates an unknown information level.
540  */
541 static int
542 smb_trans2_find_get_maxdata(smb_request_t *sr, uint16_t infolev, uint16_t fflag)
543 {
544 	int maxdata;
545 
546 	maxdata = smb_ascii_or_unicode_null_len(sr);
547 
548 	switch (infolev) {
549 	case SMB_INFO_STANDARD :
550 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
551 			maxdata += sizeof (int32_t);
552 		maxdata += 2 + 2 + 2 + 4 + 4 + 2 + 1;
553 		break;
554 
555 	case SMB_INFO_QUERY_EA_SIZE:
556 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
557 			maxdata += sizeof (int32_t);
558 		maxdata += 2 + 2 + 2 + 4 + 4 + 2 + 4 + 1;
559 		break;
560 
561 	case SMB_FIND_FILE_DIRECTORY_INFO:
562 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4;
563 		break;
564 
565 	case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
566 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4 + 4 + 2 + 24;
567 		break;
568 
569 	case SMB_FIND_FILE_NAMES_INFO:
570 		maxdata += 4 + 4 + 4;
571 		break;
572 
573 	case SMB_MAC_FIND_BOTH_HFS_INFO:
574 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 1 + 1 + 2 +
575 		    4 + 32 + 4 + 1 + 1 + 24 + 4;
576 		break;
577 
578 	default:
579 		maxdata = 0;
580 	}
581 
582 	return (maxdata);
583 }
584 
585 /*
586  * smb_trans2_find_get_dents
587  *
588  * This function will get all the directory entry information and mbc
589  * encode it in the xa. If there is an error, it will be returned;
590  * otherwise, 0 is returned.
591  *
592  * The more field will be updated. If the value returned is one, it means
593  * there are more entries; otherwise, the returned value will be zero. The
594  * cookie will also be updated to indicate the next start point for the
595  * search. The count value will also be updated to stores the total entries
596  * encoded.
597  */
598 int smb_trans2_find_get_dents(
599     smb_request_t	*sr,
600     smb_xa_t		*xa,
601     uint16_t		fflag,
602     uint16_t		infolev,
603     int			maxdata,
604     smb_node_t		*dir_snode,
605     uint16_t		sattr,
606     uint16_t		maxcount,
607     int			wildcards,
608     char		*pattern,
609     uint32_t		*cookie,
610     int			*more,
611     int			*count)
612 {
613 	smb_dent_info_hdr_t	*ihdr;
614 	smb_dent_info_t		*ient;
615 	int			dent_buf_size;
616 	int			i;
617 	int			total;
618 	int			maxentries;
619 	int			rc;
620 
621 	ihdr = kmem_zalloc(sizeof (smb_dent_info_hdr_t), KM_SLEEP);
622 	*count = 0;
623 
624 	if (!wildcards)
625 		maxentries = maxcount = 1;
626 	else {
627 		maxentries = (xa->rep_data_mb.max_bytes -
628 		    xa->rep_data_mb.chain_offset) / maxdata;
629 		if (maxcount > SMB_MAX_DENTS_IOVEC)
630 			maxcount = SMB_MAX_DENTS_IOVEC;
631 		if (maxentries > maxcount)
632 			maxentries = maxcount;
633 	}
634 
635 	/* Each entry will need to be aligned so add _POINTER_ALIGNMENT */
636 	dent_buf_size =
637 	    maxentries * (SMB_MAX_DENT_INFO_SIZE + _POINTER_ALIGNMENT);
638 	ihdr->iov->iov_base = kmem_alloc(dent_buf_size, KM_SLEEP);
639 
640 	ihdr->sattr = sattr;
641 	ihdr->pattern = pattern;
642 	ihdr->sr = sr;
643 
644 	ihdr->uio.uio_iovcnt = maxcount;
645 	ihdr->uio.uio_resid = dent_buf_size;
646 	ihdr->uio.uio_iov = ihdr->iov;
647 	ihdr->uio.uio_loffset = 0;
648 
649 	rc = smb_get_dents(sr, cookie, dir_snode, wildcards, ihdr, more);
650 	if (rc != 0) {
651 		goto out;
652 	}
653 
654 	if (ihdr->iov->iov_len == 0)
655 		*count = 0;
656 	else
657 		*count = smb_trans2_find_process_ients(sr, xa, ihdr, fflag,
658 		    infolev, maxdata, dir_snode, more, cookie);
659 	rc = 0;
660 
661 out:
662 
663 	total = maxcount - ihdr->uio.uio_iovcnt;
664 	ASSERT((total >= 0) && (total <= SMB_MAX_DENTS_IOVEC));
665 	for (i = 0; i < total; i++) {
666 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
667 		ient = (smb_dent_info_t *)ihdr->iov[i].iov_base;
668 		ASSERT(ient);
669 		smb_node_release(ient->snode);
670 	}
671 
672 	kmem_free(ihdr->iov->iov_base, dent_buf_size);
673 	kmem_free(ihdr, sizeof (smb_dent_info_hdr_t));
674 	return (0);
675 }
676 
677 
678 
679 /*
680  * smb_get_dents
681  *
682  * This function utilizes "smb_fsop_getdents()" to get dir entries.
683  * The "smb_gather_dents_info()" is the call back function called
684  * inside the file system. It is very important that the function
685  * does not sleep or yield since it is processed inside a file
686  * system transaction.
687  *
688  * The function returns 0 when successful and error code when failed.
689  * If more is provided, the return value of 1 is returned indicating
690  * more entries; otherwise, 0 is returned.
691  */
692 int smb_get_dents(
693     smb_request_t	*sr,
694     uint32_t		*cookie,
695     smb_node_t		*dir_snode,
696     uint32_t		wildcards,
697     smb_dent_info_hdr_t	*ihdr,
698     int			*more)
699 {
700 	int		rc;
701 	char		*namebuf;
702 	smb_node_t	*snode;
703 	smb_attr_t	file_attr;
704 	uint32_t	maxcnt = ihdr->uio.uio_iovcnt;
705 	char		shortname[SMB_SHORTNAMELEN], name83[SMB_SHORTNAMELEN];
706 
707 	namebuf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
708 	if (more)
709 		*more = 0;
710 
711 	if (!wildcards) {
712 		/* Already found entry? */
713 		if (*cookie != 0)
714 			return (0);
715 		shortname[0] = '\0';
716 
717 		rc = smb_fsop_lookup(sr, sr->user_cr, 0, sr->tid_tree->t_snode,
718 		    dir_snode, ihdr->pattern, &snode, &file_attr, shortname,
719 		    name83);
720 
721 		if (rc) {
722 			kmem_free(namebuf, MAXNAMELEN);
723 			return (rc);
724 		}
725 
726 		(void) strlcpy(namebuf, snode->od_name, MAXNAMELEN);
727 
728 		/*
729 		 * It is not necessary to set the "force" flag (i.e. to
730 		 * take into account mangling for case-insensitive collisions)
731 		 */
732 
733 		if (shortname[0] == '\0')
734 			(void) smb_mangle_name(snode->attr.sa_vattr.va_nodeid,
735 			    namebuf, shortname, name83, 0);
736 		(void) smb_gather_dents_info((char *)ihdr,
737 		    snode->attr.sa_vattr.va_nodeid,
738 		    strlen(namebuf), namebuf, -1, (int *)&maxcnt,
739 		    &snode->attr, snode, shortname, name83);
740 		kmem_free(namebuf, MAXNAMELEN);
741 		return (0);
742 	}
743 
744 	if ((rc = smb_fsop_getdents(sr, sr->user_cr, dir_snode, cookie,
745 	    0, (int *)&maxcnt, (char *)ihdr, ihdr->pattern)) != 0) {
746 		if (rc == ENOENT) {
747 			kmem_free(namebuf, MAXNAMELEN);
748 			return (0);
749 		}
750 		kmem_free(namebuf, MAXNAMELEN);
751 		return (rc);
752 	}
753 
754 	if (*cookie != 0x7FFFFFFF && more)
755 		*more = 1;
756 
757 	kmem_free(namebuf, MAXNAMELEN);
758 	return (0);
759 }
760 
761 
762 
763 
764 /*
765  * smb_gather_dents_info
766  *
767  * The function will accept information of each directory entry and put
768  * the needed information in the buffer. It is passed as the call back
769  * function for smb_fsop_getdents() to gather trans2 find info.
770  *
771  * If the buffer space is not enough, -1 will be returned. Regardless
772  * of valid entry or not, 0 will be returned; however, only valid entry
773  * will be stored in the buffer.
774  */
775 int /*ARGSUSED*/
776 smb_gather_dents_info(
777     char	*args,
778     ino_t	fileid,
779     int		namelen,
780     char	*name,
781     uint32_t	cookie,
782     int32_t	*countp,
783     smb_attr_t	*attr,
784     smb_node_t	*snode,
785     char	*shortname,
786     char	*name83)
787 {
788 	/*LINTED E_BAD_PTR_CAST_ALIGN*/
789 	smb_dent_info_hdr_t	*ihdr = (smb_dent_info_hdr_t *)args;
790 	smb_dent_info_t		*ient;
791 	uint8_t			*v5_name = NULL;
792 	uint8_t			*np = (uint8_t *)name;
793 	int			reclen = sizeof (smb_dent_info_t) + namelen;
794 
795 	v5_name = kmem_alloc(MAXNAMELEN-1, KM_SLEEP);
796 
797 	if (!ihdr->uio.uio_iovcnt || ihdr->uio.uio_resid < reclen) {
798 		kmem_free(v5_name, MAXNAMELEN-1);
799 		smb_node_release(snode);
800 		return (-1);
801 	}
802 
803 	if (!smb_sattr_check(attr, name, ihdr->sattr)) {
804 		kmem_free(v5_name, MAXNAMELEN-1);
805 		smb_node_release(snode);
806 		return (0);
807 	}
808 
809 	if (catia_callback) {
810 		catia_callback(v5_name, (uint8_t *)name,  MAXNAMELEN-1);
811 		np = v5_name;
812 		reclen = sizeof (smb_dent_info_t) + strlen((char *)v5_name);
813 	}
814 
815 	ASSERT(snode);
816 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
817 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
818 
819 	/*
820 	 * Each entry needs to be properly aligned or we may get an alignment
821 	 * fault on sparc.
822 	 */
823 	ihdr->uio.uio_loffset = (offset_t)PTRALIGN(ihdr->uio.uio_loffset);
824 	/*LINTED E_BAD_PTR_CAST_ALIGN*/
825 	ient = (smb_dent_info_t *)&ihdr->iov->iov_base[ihdr->uio.uio_loffset];
826 
827 	ient->cookie = cookie;
828 	ient->attr = *attr;
829 	ient->snode = snode;
830 
831 	(void) strcpy(ient->name, (char *)np);
832 	(void) strcpy(ient->shortname, shortname);
833 	(void) strcpy(ient->name83, name83);
834 	ihdr->uio.uio_iov->iov_base = (char *)ient;
835 	ihdr->uio.uio_iov->iov_len = reclen;
836 
837 	ihdr->uio.uio_iov++;
838 	ihdr->uio.uio_iovcnt--;
839 	ihdr->uio.uio_resid -= reclen;
840 	ihdr->uio.uio_loffset += reclen;
841 
842 	kmem_free(v5_name, MAXNAMELEN-1);
843 	return (0);
844 }
845 
846 
847 
848 /*
849  * smb_trans2_find_process_ients
850  *
851  * This function encodes the directory entry information store in
852  * the iov structure of the ihdr structure.
853  *
854  * The total entries encoded will be returned. If the entries encoded
855  * is less than the total entries in the iov, the more field will
856  * be updated to 1. Also, the next cookie wil be updated as well.
857  */
858 int
859 smb_trans2_find_process_ients(
860     smb_request_t	*sr,
861     smb_xa_t		*xa,
862     smb_dent_info_hdr_t	*ihdr,
863     uint16_t		fflag,
864     uint16_t		infolev,
865     int			maxdata,
866     smb_node_t		*dir_snode,
867     int			*more,
868     uint32_t		*cookie)
869 {
870 	int i, err = 0;
871 	smb_dent_info_t *ient;
872 	uint32_t mb_flags = (sr->smb_flg2 & SMB_FLAGS2_UNICODE)
873 	    ? SMB_MSGBUF_UNICODE : 0;
874 
875 	for (i = 0; i < SMB_MAX_DENTS_IOVEC; i++) {
876 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
877 		if ((ient = (smb_dent_info_t *)ihdr->iov[i].iov_base) == 0)
878 			break;
879 
880 		/*
881 		 * Observed differences between our response and Windows
882 		 * response, which hasn't caused a problem yet!
883 		 *
884 		 * 1. The NextEntryOffset field for the last entry should
885 		 * be 0.  This code always calculate the record length
886 		 * and puts the result in the NextEntryOffset field.
887 		 *
888 		 * 2. The FileIndex field is always 0.  This code puts
889 		 * the cookie in the FileIndex field.
890 		 */
891 		err = smb_trans2_find_mbc_encode(sr, xa, ient, maxdata, infolev,
892 		    fflag, mb_flags, dir_snode, NULL);
893 
894 		if (err)
895 			break;
896 	}
897 
898 	/*
899 	 * Not enough space to store all the entries returned,
900 	 * which is indicated by setting more.
901 	 */
902 	if (more && err < 0) {
903 		*more = 1;
904 
905 		/*
906 		 * Assume the space will be at least enough for 1 entry.
907 		 */
908 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
909 		ient = (smb_dent_info_t *)ihdr->iov[i-1].iov_base;
910 		*cookie = ient->cookie;
911 	}
912 	return (i);
913 }
914 
915 /*
916  * smb_trans2_find_mbc_encode
917  *
918  * This function encodes the mbc for one directory entry.
919  *
920  * The function returns -1 when the max data requested by client
921  * is reached. If the entry is valid and successful encoded, 0
922  * will be returned; otherwise, 1 will be returned.
923  */
924 int /*ARGSUSED*/
925 smb_trans2_find_mbc_encode(
926     smb_request_t	*sr,
927     smb_xa_t		*xa,
928     smb_dent_info_t	*ient,
929     int			maxdata,
930     uint16_t		infolev,
931     uint16_t		fflag,
932     uint32_t		mb_flags,
933     smb_node_t		*dir_snode,
934     smb_node_t		*sd_snode)
935 {
936 	int uni_namelen;
937 	int shortlen;
938 	uint32_t next_entry_offset;
939 	char buf83[26];
940 	smb_msgbuf_t mb;
941 	uint32_t dattr = 0;
942 	uint32_t dsize32 = 0;
943 	uint32_t asize32 = 0;
944 	u_offset_t datasz = 0;
945 	u_offset_t allocsz = 0;
946 	smb_node_t *lnk_snode;
947 	smb_attr_t lnkattr;
948 	int rc;
949 
950 	uni_namelen = smb_ascii_or_unicode_strlen(sr, ient->name);
951 	if (uni_namelen == -1)
952 		return (1);
953 
954 	next_entry_offset = maxdata + uni_namelen;
955 
956 	if (MBC_ROOM_FOR(&xa->rep_data_mb, (maxdata + uni_namelen)) == 0)
957 		return (-1);
958 
959 	if (ient->attr.sa_vattr.va_type == VLNK) {
960 		rc = smb_fsop_lookup(sr, sr->user_cr, SMB_FOLLOW_LINKS,
961 		    sr->tid_tree->t_snode, dir_snode, ient->name, &lnk_snode,
962 		    &lnkattr, 0, 0);
963 
964 		/*
965 		 * We normally want to resolve the object to which a symlink
966 		 * refers so that CIFS clients can access sub-directories and
967 		 * find the correct association for files. This causes a
968 		 * problem, however, if a symlink in a sub-directory points
969 		 * to a parent directory (some UNIX GUI's create a symlink in
970 		 * $HOME/.desktop that points to the user's home directory).
971 		 * Some Windows applications (i.e. virus scanning) loop/hang
972 		 * trying to follow this recursive path and there is little
973 		 * we can do because the path is constructed on the client.
974 		 * skc_dirsymlink_enable allows an end-user to disable
975 		 * symlinks to directories. Symlinks to other object types
976 		 * should be unaffected.
977 		 */
978 		if (rc == 0) {
979 			if (smb_dirsymlink_enable ||
980 			    (lnkattr.sa_vattr.va_type != VDIR)) {
981 				smb_node_release(ient->snode);
982 				ient->snode = lnk_snode;
983 				ient->attr = lnkattr;
984 			} else {
985 				smb_node_release(lnk_snode);
986 			}
987 		}
988 	}
989 
990 	if (infolev != SMB_FIND_FILE_NAMES_INFO) {
991 		/* data size */
992 		datasz = smb_node_get_size(ient->snode, &ient->attr);
993 		dsize32 = (datasz > UINT_MAX) ? UINT_MAX : (uint32_t)datasz;
994 
995 		/* allocation size */
996 		allocsz = ient->attr.sa_vattr.va_nblocks * DEV_BSIZE;
997 		asize32 = (allocsz > UINT_MAX) ? UINT_MAX : (uint32_t)allocsz;
998 
999 		dattr = smb_node_get_dosattr(ient->snode);
1000 	}
1001 
1002 	switch (infolev) {
1003 	case SMB_INFO_STANDARD:
1004 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
1005 			(void) smb_mbc_encodef(&xa->rep_data_mb, "l",
1006 			    ient->cookie);
1007 
1008 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%yyyllwbu", sr,
1009 		    ient->attr.sa_crtime.tv_sec ?
1010 		    smb_gmt2local(sr, ient->attr.sa_crtime.tv_sec) :
1011 		    smb_gmt2local(sr, ient->attr.sa_vattr.va_mtime.tv_sec),
1012 		    smb_gmt2local(sr, ient->attr.sa_vattr.va_atime.tv_sec),
1013 		    smb_gmt2local(sr, ient->attr.sa_vattr.va_mtime.tv_sec),
1014 		    dsize32,
1015 		    asize32,
1016 		    dattr,
1017 		    uni_namelen,
1018 		    ient->name);
1019 		break;
1020 
1021 	case SMB_INFO_QUERY_EA_SIZE:
1022 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
1023 			(void) smb_mbc_encodef(&xa->rep_data_mb, "l",
1024 			    ient->cookie);
1025 
1026 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%yyyllwlbz", sr,
1027 		    ient->attr.sa_crtime.tv_sec ?
1028 		    smb_gmt2local(sr, ient->attr.sa_crtime.tv_sec) :
1029 		    smb_gmt2local(sr, ient->attr.sa_vattr.va_mtime.tv_sec),
1030 		    smb_gmt2local(sr, ient->attr.sa_vattr.va_atime.tv_sec),
1031 		    smb_gmt2local(sr, ient->attr.sa_vattr.va_mtime.tv_sec),
1032 		    dsize32,
1033 		    asize32,
1034 		    dattr,
1035 		    0L,		/* EA Size */
1036 		    uni_namelen,
1037 		    ient->name);
1038 		break;
1039 
1040 	case SMB_FIND_FILE_DIRECTORY_INFO:
1041 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqllu", sr,
1042 		    next_entry_offset,
1043 		    ient->cookie,
1044 		    ient->attr.sa_crtime.tv_sec ? &ient->attr.sa_crtime :
1045 		    &ient->attr.sa_vattr.va_mtime,
1046 		    &ient->attr.sa_vattr.va_atime,
1047 		    &ient->attr.sa_vattr.va_mtime,
1048 		    &ient->attr.sa_vattr.va_ctime,
1049 		    (uint64_t)datasz,
1050 		    (uint64_t)allocsz,
1051 		    dattr,
1052 		    uni_namelen,
1053 		    ient->name);
1054 		break;
1055 
1056 	case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1057 		bzero(buf83, sizeof (buf83));
1058 		smb_msgbuf_init(&mb, (uint8_t *)buf83, sizeof (buf83),
1059 		    mb_flags);
1060 		if (smb_msgbuf_encode(&mb, "u", ient->shortname) < 0) {
1061 			smb_msgbuf_term(&mb);
1062 			return (-1);
1063 		}
1064 		shortlen = smb_ascii_or_unicode_strlen(sr, ient->shortname);
1065 
1066 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%llTTTTqqlllb.24cu",
1067 		    sr,
1068 		    next_entry_offset,
1069 		    ient->cookie,
1070 		    ient->attr.sa_crtime.tv_sec ? &ient->attr.sa_crtime :
1071 		    &ient->attr.sa_vattr.va_mtime,
1072 		    &ient->attr.sa_vattr.va_atime,
1073 		    &ient->attr.sa_vattr.va_mtime,
1074 		    &ient->attr.sa_vattr.va_ctime,
1075 		    (uint64_t)datasz,
1076 		    (uint64_t)allocsz,
1077 		    dattr,
1078 		    uni_namelen,
1079 		    0L,
1080 		    shortlen,
1081 		    buf83,
1082 		    ient->name);
1083 
1084 		smb_msgbuf_term(&mb);
1085 		break;
1086 
1087 	case SMB_FIND_FILE_NAMES_INFO:
1088 		(void) smb_mbc_encodef(&xa->rep_data_mb, "%lllu", sr,
1089 		    next_entry_offset,
1090 		    ient->cookie,
1091 		    uni_namelen,
1092 		    ient->name);
1093 		break;
1094 	}
1095 
1096 	return (0);
1097 }
1098 
1099 /*
1100  * Close a search started by a Trans2FindFirst2 request.
1101  */
1102 smb_sdrc_t
1103 smb_pre_find_close2(smb_request_t *sr)
1104 {
1105 	int rc;
1106 
1107 	rc = smbsr_decode_vwv(sr, "w", &sr->smb_sid);
1108 
1109 	DTRACE_SMB_1(op__FindClose2__start, smb_request_t *, sr);
1110 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
1111 }
1112 
1113 void
1114 smb_post_find_close2(smb_request_t *sr)
1115 {
1116 	DTRACE_SMB_1(op__FindClose2__done, smb_request_t *, sr);
1117 }
1118 
1119 smb_sdrc_t
1120 smb_com_find_close2(smb_request_t *sr)
1121 {
1122 	sr->sid_odir = smb_odir_lookup_by_sid(sr->tid_tree, sr->smb_sid);
1123 	if (sr->sid_odir == NULL) {
1124 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
1125 		return (SDRC_ERROR);
1126 	}
1127 
1128 	smb_rdir_close(sr);
1129 
1130 	if (smbsr_encode_empty_result(sr))
1131 		return (SDRC_ERROR);
1132 
1133 	return (SDRC_SUCCESS);
1134 }
1135