xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_trans2_find.c (revision 2850d85b7b93f31e578520dc3b3feb24db609c62)
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	"%Z%%M%	%I%	%E% 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 int
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 		/* NOTREACHED */
311 	}
312 
313 	if (smb_decode_mbc(&xa->req_param_mb, "%wwww4.u", sr,
314 	    &sattr, &maxcount, &fflag, &infolev, &path) != 0) {
315 		smbsr_decode_error(sr);
316 		/* NOTREACHED */
317 	}
318 
319 	maxdata = smb_trans2_find_get_maxdata(sr, infolev, fflag);
320 	if (maxdata == 0) {
321 		smbsr_error(sr, NT_STATUS_INVALID_LEVEL,
322 		    ERRDOS, ERROR_INVALID_LEVEL);
323 		/* NOTREACHED */
324 	}
325 
326 	/*
327 	 * When maxcount is zero Windows behaves as if it was 1.
328 	 */
329 	if (maxcount == 0)
330 		maxcount = 1;
331 
332 	if ((smb_trans2_find_max != 0) && (maxcount > smb_trans2_find_max))
333 		maxcount = smb_trans2_find_max;
334 
335 	if (sr->smb_flg2 & SMB_FLAGS2_UNICODE)
336 		(void) smb_convert_unicode_wildcards(path);
337 
338 	(void) smb_rdir_open(sr, path, sattr);
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 		/* NOTREACHED */
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_encode_mbc(&xa->rep_param_mb, "wwwww",
386 	    sid, count, (more ? 0 : 1), 0, 0);
387 
388 	kmem_free(pattern, MAXNAMELEN);
389 	return (SDRC_NORMAL_REPLY);
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 int
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_decode_mbc(&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_decode_mbc(&xa->req_param_mb, "%www lw", sr,
464 	    &sr->smb_sid, &maxcount, &infolev, &cookie, &fflag) != 0) {
465 		smbsr_decode_error(sr);
466 		/* NOTREACHED */
467 	}
468 
469 	sr->sid_odir = smb_odir_lookup_by_sid(sr->tid_tree, sr->smb_sid);
470 	if (sr->sid_odir == NULL) {
471 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
472 		/* NOTREACHED */
473 	}
474 
475 	maxdata = smb_trans2_find_get_maxdata(sr, infolev, fflag);
476 	if (maxdata == 0) {
477 		smb_rdir_close(sr);
478 		smbsr_error(sr, NT_STATUS_INVALID_LEVEL,
479 		    ERRDOS, ERROR_INVALID_LEVEL);
480 		/* NOTREACHED */
481 	}
482 
483 	/*
484 	 * When maxcount is zero Windows behaves as if it was 1.
485 	 */
486 	if (maxcount == 0)
487 		maxcount = 1;
488 
489 	if ((smb_trans2_find_max != 0) && (maxcount > smb_trans2_find_max))
490 		maxcount = smb_trans2_find_max;
491 
492 	/*
493 	 * Get a copy of information
494 	 */
495 	dir_snode = sr->sid_odir->d_dir_snode;
496 	pattern = kmem_alloc(MAXNAMELEN, KM_SLEEP);
497 	(void) strcpy(pattern, sr->sid_odir->d_pattern);
498 	wildcards = sr->sid_odir->d_wildcards;
499 	sattr = sr->sid_odir->d_sattr;
500 	if (fflag & SMB_FIND_CONTINUE_FROM_LAST) {
501 		mutex_enter(&sr->sid_odir->d_mutex);
502 		cookie = sr->sid_odir->d_cookie;
503 		mutex_exit(&sr->sid_odir->d_mutex);
504 	}
505 
506 	rc = smb_trans2_find_get_dents(sr, xa, fflag, infolev, maxdata,
507 	    dir_snode, sattr, maxcount, wildcards, pattern, &cookie,
508 	    &more, &count);
509 
510 	if (rc) {
511 		smb_rdir_close(sr);
512 		kmem_free(pattern, MAXNAMELEN);
513 		smbsr_errno(sr, rc);
514 		/* NOTREACHED */
515 	}
516 
517 	if (fflag & SMB_FIND_CLOSE_AFTER_REQUEST ||
518 	    (!more && fflag & SMB_FIND_CLOSE_AT_EOS))
519 		smb_rdir_close(sr);
520 	else {
521 		mutex_enter(&sr->sid_odir->d_mutex);
522 		sr->sid_odir->d_cookie = cookie;
523 		mutex_exit(&sr->sid_odir->d_mutex);
524 	}
525 
526 	(void) smb_encode_mbc(&xa->rep_param_mb, "wwww",
527 	    count, (more ? 0 : 1), 0, 0);
528 
529 	kmem_free(pattern, MAXNAMELEN);
530 	return (SDRC_NORMAL_REPLY);
531 }
532 
533 /*
534  * smb_trans2_find_get_maxdata
535  *
536  * Calculate the minimum response space required for the specified
537  * information level.
538  *
539  * A non-zero return value provides the minimum space required.
540  * A return value of zero indicates an unknown information level.
541  */
542 static int
543 smb_trans2_find_get_maxdata(smb_request_t *sr, uint16_t infolev, uint16_t fflag)
544 {
545 	int maxdata;
546 
547 	maxdata = smb_ascii_or_unicode_null_len(sr);
548 
549 	switch (infolev) {
550 	case SMB_INFO_STANDARD :
551 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
552 			maxdata += sizeof (int32_t);
553 		maxdata += 2 + 2 + 2 + 4 + 4 + 2 + 1;
554 		break;
555 
556 	case SMB_INFO_QUERY_EA_SIZE:
557 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
558 			maxdata += sizeof (int32_t);
559 		maxdata += 2 + 2 + 2 + 4 + 4 + 2 + 4 + 1;
560 		break;
561 
562 	case SMB_FIND_FILE_DIRECTORY_INFO:
563 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 4;
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_NAMES_INFO:
571 		maxdata += 4 + 4 + 4;
572 		break;
573 
574 	case SMB_MAC_FIND_BOTH_HFS_INFO:
575 		maxdata += 4 + 4 + 8 + 8 + 8 + 8 + 8 + 8 + 8 + 4 + 1 + 1 + 2 +
576 		    4 + 32 + 4 + 1 + 1 + 24 + 4;
577 		break;
578 
579 	default:
580 		maxdata = 0;
581 	}
582 
583 	return (maxdata);
584 }
585 
586 /*
587  * smb_trans2_find_get_dents
588  *
589  * This function will get all the directory entry information and mbc
590  * encode it in the xa. If there is an error, it will be returned;
591  * otherwise, 0 is returned.
592  *
593  * The more field will be updated. If the value returned is one, it means
594  * there are more entries; otherwise, the returned value will be zero. The
595  * cookie will also be updated to indicate the next start point for the
596  * search. The count value will also be updated to stores the total entries
597  * encoded.
598  */
599 int smb_trans2_find_get_dents(
600     smb_request_t	*sr,
601     smb_xa_t		*xa,
602     uint16_t		fflag,
603     uint16_t		infolev,
604     int			maxdata,
605     smb_node_t		*dir_snode,
606     uint16_t		sattr,
607     uint16_t		maxcount,
608     int			wildcards,
609     char		*pattern,
610     uint32_t		*cookie,
611     int			*more,
612     int			*count)
613 {
614 	smb_dent_info_hdr_t	*ihdr;
615 	smb_dent_info_t		*ient;
616 	int			dent_buf_size;
617 	int			i;
618 	int			total;
619 	int			maxentries;
620 	int			rc;
621 
622 	ihdr = kmem_zalloc(sizeof (smb_dent_info_hdr_t), KM_SLEEP);
623 	*count = 0;
624 
625 	if (!wildcards)
626 		maxentries = maxcount = 1;
627 	else {
628 		maxentries = (xa->rep_data_mb.max_bytes -
629 		    xa->rep_data_mb.chain_offset) / maxdata;
630 		if (maxcount > SMB_MAX_DENTS_IOVEC)
631 			maxcount = SMB_MAX_DENTS_IOVEC;
632 		if (maxentries > maxcount)
633 			maxentries = maxcount;
634 	}
635 
636 	/* Each entry will need to be aligned so add _POINTER_ALIGNMENT */
637 	dent_buf_size =
638 	    maxentries * (SMB_MAX_DENT_INFO_SIZE + _POINTER_ALIGNMENT);
639 	ihdr->iov->iov_base = kmem_alloc(dent_buf_size, KM_SLEEP);
640 
641 	ihdr->sattr = sattr;
642 	ihdr->pattern = pattern;
643 	ihdr->sr = sr;
644 
645 	ihdr->uio.uio_iovcnt = maxcount;
646 	ihdr->uio.uio_resid = dent_buf_size;
647 	ihdr->uio.uio_iov = ihdr->iov;
648 	ihdr->uio.uio_loffset = 0;
649 
650 	rc = smb_get_dents(sr, cookie, dir_snode, wildcards, ihdr, more);
651 	if (rc != 0) {
652 		goto out;
653 	}
654 
655 	if (ihdr->iov->iov_len == 0)
656 		*count = 0;
657 	else
658 		*count = smb_trans2_find_process_ients(sr, xa, ihdr, fflag,
659 		    infolev, maxdata, dir_snode, more, cookie);
660 	rc = 0;
661 
662 out:
663 
664 	total = maxcount - ihdr->uio.uio_iovcnt;
665 	ASSERT((total >= 0) && (total <= SMB_MAX_DENTS_IOVEC));
666 	for (i = 0; i < total; i++) {
667 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
668 		ient = (smb_dent_info_t *)ihdr->iov[i].iov_base;
669 		ASSERT(ient);
670 		smb_node_release(ient->snode);
671 	}
672 
673 	kmem_free(ihdr->iov->iov_base, dent_buf_size);
674 	kmem_free(ihdr, sizeof (smb_dent_info_hdr_t));
675 	return (0);
676 }
677 
678 
679 
680 /*
681  * smb_get_dents
682  *
683  * This function utilizes "smb_fsop_getdents()" to get dir entries.
684  * The "smb_gather_dents_info()" is the call back function called
685  * inside the file system. It is very important that the function
686  * does not sleep or yield since it is processed inside a file
687  * system transaction.
688  *
689  * The function returns 0 when successful and error code when failed.
690  * If more is provided, the return value of 1 is returned indicating
691  * more entries; otherwise, 0 is returned.
692  */
693 int smb_get_dents(
694     smb_request_t	*sr,
695     uint32_t		*cookie,
696     smb_node_t		*dir_snode,
697     uint32_t		wildcards,
698     smb_dent_info_hdr_t	*ihdr,
699     int			*more)
700 {
701 	int		rc;
702 	char		*namebuf;
703 	smb_node_t	*snode;
704 	smb_attr_t	file_attr;
705 	uint32_t	maxcnt = ihdr->uio.uio_iovcnt;
706 	char		shortname[MANGLE_NAMELEN], name83[MANGLE_NAMELEN];
707 	fsvol_attr_t	vol_attr;
708 
709 	namebuf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
710 	if (more)
711 		*more = 0;
712 
713 	if ((rc = fsd_getattr(&sr->tid_tree->t_fsd, &vol_attr)) != 0) {
714 		kmem_free(namebuf, MAXNAMELEN);
715 		return (rc);
716 	}
717 
718 	if (!wildcards) {
719 		/* Already found entry? */
720 		if (*cookie != 0)
721 			return (0);
722 		shortname[0] = '\0';
723 
724 		rc = smb_fsop_lookup(sr, sr->user_cr, 0, sr->tid_tree->t_snode,
725 		    dir_snode, ihdr->pattern, &snode, &file_attr, shortname,
726 		    name83);
727 
728 		if (rc) {
729 			kmem_free(namebuf, MAXNAMELEN);
730 			return (rc);
731 		}
732 
733 		(void) strlcpy(namebuf, ihdr->pattern, MAXNAMELEN);
734 
735 		/*
736 		 * It is not necessary to set the "force" flag (i.e. to
737 		 * take into account mangling for case-insensitive collisions)
738 		 */
739 
740 		if (shortname[0] == '\0')
741 			(void) smb_mangle_name(snode->attr.sa_vattr.va_nodeid,
742 			    namebuf, shortname, name83, 0);
743 		(void) smb_gather_dents_info((char *)ihdr,
744 		    snode->attr.sa_vattr.va_nodeid,
745 		    strlen(namebuf), namebuf, -1, (int *)&maxcnt,
746 		    &snode->attr, snode, shortname, name83);
747 		kmem_free(namebuf, MAXNAMELEN);
748 		return (0);
749 	}
750 
751 	if ((rc = smb_fsop_getdents(sr, sr->user_cr, dir_snode, cookie,
752 	    0, (int *)&maxcnt, (char *)ihdr, ihdr->pattern)) != 0) {
753 		if (rc == ENOENT) {
754 			kmem_free(namebuf, MAXNAMELEN);
755 			return (0);
756 		}
757 		kmem_free(namebuf, MAXNAMELEN);
758 		return (rc);
759 	}
760 
761 	if (*cookie != 0x7FFFFFFF && more)
762 		*more = 1;
763 
764 	kmem_free(namebuf, MAXNAMELEN);
765 	return (0);
766 }
767 
768 
769 
770 
771 /*
772  * smb_gather_dents_info
773  *
774  * The function will accept information of each directory entry and put
775  * the needed information in the buffer. It is passed as the call back
776  * function for smb_fsop_getdents() to gather trans2 find info.
777  *
778  * If the buffer space is not enough, -1 will be returned. Regardless
779  * of valid entry or not, 0 will be returned; however, only valid entry
780  * will be stored in the buffer.
781  */
782 int /*ARGSUSED*/
783 smb_gather_dents_info(
784     char	*args,
785     ino_t	fileid,
786     int		namelen,
787     char	*name,
788     uint32_t	cookie,
789     int32_t	*countp,
790     smb_attr_t	*attr,
791     smb_node_t	*snode,
792     char	*shortname,
793     char	*name83)
794 {
795 	/*LINTED E_BAD_PTR_CAST_ALIGN*/
796 	smb_dent_info_hdr_t	*ihdr = (smb_dent_info_hdr_t *)args;
797 	smb_dent_info_t		*ient;
798 	uint8_t			*v5_name = NULL;
799 	uint8_t			*np = (uint8_t *)name;
800 	int			reclen = sizeof (smb_dent_info_t) + namelen;
801 
802 	v5_name = kmem_alloc(MAXNAMELEN-1, KM_SLEEP);
803 
804 	if (!ihdr->uio.uio_iovcnt || ihdr->uio.uio_resid < reclen) {
805 		kmem_free(v5_name, MAXNAMELEN-1);
806 		smb_node_release(snode);
807 		return (-1);
808 	}
809 
810 	if (!smb_sattr_check(attr, name, ihdr->sattr)) {
811 		kmem_free(v5_name, MAXNAMELEN-1);
812 		smb_node_release(snode);
813 		return (0);
814 	}
815 
816 	if (catia_callback) {
817 		catia_callback(v5_name, (uint8_t *)name,  MAXNAMELEN-1);
818 		np = v5_name;
819 		reclen = sizeof (smb_dent_info_t) + strlen((char *)v5_name);
820 	}
821 
822 	ASSERT(snode);
823 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
824 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
825 
826 	/*
827 	 * Each entry needs to be properly aligned or we may get an alignment
828 	 * fault on sparc.
829 	 */
830 	ihdr->uio.uio_loffset = (offset_t)PTRALIGN(ihdr->uio.uio_loffset);
831 	/*LINTED E_BAD_PTR_CAST_ALIGN*/
832 	ient = (smb_dent_info_t *)&ihdr->iov->iov_base[ihdr->uio.uio_loffset];
833 
834 	ient->cookie = cookie;
835 	ient->attr = *attr;
836 	ient->snode = snode;
837 
838 	(void) strcpy(ient->name, (char *)np);
839 	(void) strcpy(ient->shortname, shortname);
840 	(void) strcpy(ient->name83, name83);
841 	ihdr->uio.uio_iov->iov_base = (char *)ient;
842 	ihdr->uio.uio_iov->iov_len = reclen;
843 
844 	ihdr->uio.uio_iov++;
845 	ihdr->uio.uio_iovcnt--;
846 	ihdr->uio.uio_resid -= reclen;
847 	ihdr->uio.uio_loffset += reclen;
848 
849 	kmem_free(v5_name, MAXNAMELEN-1);
850 	return (0);
851 }
852 
853 
854 
855 /*
856  * smb_trans2_find_process_ients
857  *
858  * This function encodes the directory entry information store in
859  * the iov structure of the ihdr structure.
860  *
861  * The total entries encoded will be returned. If the entries encoded
862  * is less than the total entries in the iov, the more field will
863  * be updated to 1. Also, the next cookie wil be updated as well.
864  */
865 int
866 smb_trans2_find_process_ients(
867     smb_request_t	*sr,
868     smb_xa_t		*xa,
869     smb_dent_info_hdr_t	*ihdr,
870     uint16_t		fflag,
871     uint16_t		infolev,
872     int			maxdata,
873     smb_node_t		*dir_snode,
874     int			*more,
875     uint32_t		*cookie)
876 {
877 	int i, err = 0;
878 	smb_dent_info_t *ient;
879 	uint32_t mb_flags = (sr->smb_flg2 & SMB_FLAGS2_UNICODE)
880 	    ? SMB_MSGBUF_UNICODE : 0;
881 
882 	for (i = 0; i < SMB_MAX_DENTS_IOVEC; i++) {
883 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
884 		if ((ient = (smb_dent_info_t *)ihdr->iov[i].iov_base) == 0)
885 			break;
886 
887 		/*
888 		 * Observed differences between our response and Windows
889 		 * response, which hasn't caused a problem yet!
890 		 *
891 		 * 1. The NextEntryOffset field for the last entry should
892 		 * be 0.  This code always calculate the record length
893 		 * and puts the result in the NextEntryOffset field.
894 		 *
895 		 * 2. The FileIndex field is always 0.  This code puts
896 		 * the cookie in the FileIndex field.
897 		 */
898 		err = smb_trans2_find_mbc_encode(sr, xa, ient, maxdata, infolev,
899 		    fflag, mb_flags, dir_snode, NULL);
900 
901 		if (err)
902 			break;
903 	}
904 
905 	/*
906 	 * Not enough space to store all the entries returned,
907 	 * which is indicated by setting more.
908 	 */
909 	if (more && err < 0) {
910 		*more = 1;
911 
912 		/*
913 		 * Assume the space will be at least enough for 1 entry.
914 		 */
915 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
916 		ient = (smb_dent_info_t *)ihdr->iov[i-1].iov_base;
917 		*cookie = ient->cookie;
918 	}
919 	return (i);
920 }
921 
922 /*
923  * smb_trans2_find_mbc_encode
924  *
925  * This function encodes the mbc for one directory entry.
926  *
927  * The function returns -1 when the max data requested by client
928  * is reached. If the entry is valid and successful encoded, 0
929  * will be returned; otherwise, 1 will be returned.
930  */
931 int /*ARGSUSED*/
932 smb_trans2_find_mbc_encode(
933     smb_request_t	*sr,
934     smb_xa_t		*xa,
935     smb_dent_info_t	*ient,
936     int			maxdata,
937     uint16_t		infolev,
938     uint16_t		fflag,
939     uint32_t		mb_flags,
940     smb_node_t		*dir_snode,
941     smb_node_t		*sd_snode)
942 {
943 	int uni_namelen;
944 	int shortlen;
945 	uint32_t next_entry_offset;
946 	char buf83[26];
947 	smb_msgbuf_t mb;
948 	uint32_t dattr = 0;
949 	uint32_t size32 = 0;
950 	uint64_t size64 = 0;
951 	smb_node_t *lnk_snode;
952 	smb_attr_t lnkattr;
953 	int rc;
954 
955 	uni_namelen = smb_ascii_or_unicode_strlen(sr, ient->name);
956 	if (uni_namelen == -1)
957 		return (1);
958 
959 	next_entry_offset = maxdata + uni_namelen;
960 
961 	if (MBC_ROOM_FOR(&xa->rep_data_mb, (maxdata + uni_namelen)) == 0)
962 		return (-1);
963 
964 	if (ient->attr.sa_vattr.va_type == VLNK) {
965 		rc = smb_fsop_lookup(sr, sr->user_cr, SMB_FOLLOW_LINKS,
966 		    sr->tid_tree->t_snode, dir_snode, ient->name, &lnk_snode,
967 		    &lnkattr, 0, 0);
968 
969 		/*
970 		 * We normally want to resolve the object to which a symlink
971 		 * refers so that CIFS clients can access sub-directories and
972 		 * find the correct association for files. This causes a
973 		 * problem, however, if a symlink in a sub-directory points
974 		 * to a parent directory (some UNIX GUI's create a symlink in
975 		 * $HOME/.desktop that points to the user's home directory).
976 		 * Some Windows applications (i.e. virus scanning) loop/hang
977 		 * trying to follow this recursive path and there is little
978 		 * we can do because the path is constructed on the client.
979 		 * skc_dirsymlink_enable allows an end-user to disable
980 		 * symlinks to directories. Symlinks to other object types
981 		 * should be unaffected.
982 		 */
983 		if (rc == 0) {
984 			if (smb_info.si.skc_dirsymlink_enable ||
985 			    (lnkattr.sa_vattr.va_type != VDIR)) {
986 				smb_node_release(ient->snode);
987 				ient->snode = lnk_snode;
988 				ient->attr = lnkattr;
989 			} else {
990 				smb_node_release(lnk_snode);
991 			}
992 		}
993 	}
994 
995 	if (infolev != SMB_FIND_FILE_NAMES_INFO) {
996 		size64 = smb_node_get_size(ient->snode, &ient->attr);
997 		size32 = (size64 > 0xFFFFFFFF) ? 0xFFFFFFFF : (uint32_t)size64;
998 		dattr = smb_mode_to_dos_attributes(&ient->attr);
999 	}
1000 
1001 	switch (infolev) {
1002 	case SMB_INFO_STANDARD:
1003 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
1004 			(void) smb_encode_mbc(&xa->rep_data_mb, "l",
1005 			    ient->cookie);
1006 
1007 		(void) smb_encode_mbc(&xa->rep_data_mb, "%yyyllwbu", sr,
1008 		    ient->attr.sa_crtime.tv_sec ? ient->attr.sa_crtime.tv_sec :
1009 		    ient->attr.sa_vattr.va_mtime.tv_sec,
1010 		    ient->attr.sa_vattr.va_atime.tv_sec,
1011 		    ient->attr.sa_vattr.va_mtime.tv_sec,
1012 		    size32,
1013 		    size32,
1014 		    dattr,
1015 		    uni_namelen,
1016 		    ient->name);
1017 		break;
1018 
1019 	case SMB_INFO_QUERY_EA_SIZE:
1020 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
1021 			(void) smb_encode_mbc(&xa->rep_data_mb, "l",
1022 			    ient->cookie);
1023 
1024 		(void) smb_encode_mbc(&xa->rep_data_mb, "%yyyllwlbu", sr,
1025 		    ient->attr.sa_crtime.tv_sec ? ient->attr.sa_crtime.tv_sec :
1026 		    ient->attr.sa_vattr.va_mtime.tv_sec,
1027 		    ient->attr.sa_vattr.va_atime.tv_sec,
1028 		    ient->attr.sa_vattr.va_mtime.tv_sec,
1029 		    size32,
1030 		    size32,
1031 		    dattr,
1032 		    0L,		/* EA Size */
1033 		    uni_namelen,
1034 		    ient->name);
1035 		break;
1036 
1037 	case SMB_FIND_FILE_DIRECTORY_INFO:
1038 		(void) smb_encode_mbc(&xa->rep_data_mb, "%llTTTTqqllu", sr,
1039 		    next_entry_offset,
1040 		    ient->cookie,
1041 		    ient->attr.sa_crtime.tv_sec ? &ient->attr.sa_crtime :
1042 		    &ient->attr.sa_vattr.va_mtime,
1043 		    &ient->attr.sa_vattr.va_atime,
1044 		    &ient->attr.sa_vattr.va_mtime,
1045 		    &ient->attr.sa_vattr.va_ctime,
1046 		    size64,
1047 		    size64,
1048 		    dattr,
1049 		    uni_namelen,
1050 		    ient->name);
1051 		break;
1052 
1053 	case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1054 		bzero(buf83, sizeof (buf83));
1055 		smb_msgbuf_init(&mb, (uint8_t *)buf83, sizeof (buf83),
1056 		    mb_flags);
1057 		if (smb_msgbuf_encode(&mb, "u", ient->shortname) < 0) {
1058 			smb_msgbuf_term(&mb);
1059 			return (-1);
1060 		}
1061 		shortlen = smb_ascii_or_unicode_strlen(sr, ient->shortname);
1062 
1063 		(void) smb_encode_mbc(&xa->rep_data_mb, "%llTTTTqqlllb.24cu",
1064 		    sr,
1065 		    next_entry_offset,
1066 		    ient->cookie,
1067 		    ient->attr.sa_crtime.tv_sec ? &ient->attr.sa_crtime :
1068 		    &ient->attr.sa_vattr.va_mtime,
1069 		    &ient->attr.sa_vattr.va_atime,
1070 		    &ient->attr.sa_vattr.va_mtime,
1071 		    &ient->attr.sa_vattr.va_ctime,
1072 		    size64,
1073 		    size64,
1074 		    dattr,
1075 		    uni_namelen,
1076 		    0L,
1077 		    shortlen,
1078 		    buf83,
1079 		    ient->name);
1080 
1081 		smb_msgbuf_term(&mb);
1082 		break;
1083 
1084 	case SMB_FIND_FILE_NAMES_INFO:
1085 		(void) smb_encode_mbc(&xa->rep_data_mb, "%lllu", sr,
1086 		    next_entry_offset,
1087 		    ient->cookie,
1088 		    uni_namelen,
1089 		    ient->name);
1090 		break;
1091 	}
1092 
1093 	return (0);
1094 }
1095 
1096 /*
1097  * Close a search started by a Trans2FindFirst2 request.
1098  */
1099 int
1100 smb_com_find_close2(smb_request_t *sr)
1101 {
1102 	if (smbsr_decode_vwv(sr, "w", &sr->smb_sid) != 0) {
1103 		smbsr_decode_error(sr);
1104 		/* NOTREACHED */
1105 	}
1106 
1107 	sr->sid_odir = smb_odir_lookup_by_sid(sr->tid_tree, sr->smb_sid);
1108 	if (sr->sid_odir == NULL) {
1109 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
1110 		/* NOTREACHED */
1111 	}
1112 
1113 	smb_rdir_close(sr);
1114 	smbsr_encode_empty_result(sr);
1115 	return (SDRC_NORMAL_REPLY);
1116 }
1117