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 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_REPLY);
311 	}
312 
313 	if (smb_decode_mbc(&xa->req_param_mb, "%wwww4.u", sr,
314 	    &sattr, &maxcount, &fflag, &infolev, &path) != 0) {
315 		return (SDRC_ERROR_REPLY);
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_REPLY);
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_REPLY);
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_REPLY);
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 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_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 		return (SDRC_ERROR_REPLY);
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_REPLY);
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_REPLY);
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_REPLY);
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_encode_mbc(&xa->rep_param_mb, "wwww",
526 	    count, (more ? 0 : 1), 0, 0);
527 
528 	kmem_free(pattern, MAXNAMELEN);
529 	return (SDRC_NORMAL_REPLY);
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[MANGLE_NAMELEN], name83[MANGLE_NAMELEN];
706 	fsvol_attr_t	vol_attr;
707 
708 	namebuf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
709 	if (more)
710 		*more = 0;
711 
712 	if ((rc = fsd_getattr(&sr->tid_tree->t_fsd, &vol_attr)) != 0) {
713 		kmem_free(namebuf, MAXNAMELEN);
714 		return (rc);
715 	}
716 
717 	if (!wildcards) {
718 		/* Already found entry? */
719 		if (*cookie != 0)
720 			return (0);
721 		shortname[0] = '\0';
722 
723 		rc = smb_fsop_lookup(sr, sr->user_cr, 0, sr->tid_tree->t_snode,
724 		    dir_snode, ihdr->pattern, &snode, &file_attr, shortname,
725 		    name83);
726 
727 		if (rc) {
728 			kmem_free(namebuf, MAXNAMELEN);
729 			return (rc);
730 		}
731 
732 		(void) strlcpy(namebuf, ihdr->pattern, MAXNAMELEN);
733 
734 		/*
735 		 * It is not necessary to set the "force" flag (i.e. to
736 		 * take into account mangling for case-insensitive collisions)
737 		 */
738 
739 		if (shortname[0] == '\0')
740 			(void) smb_mangle_name(snode->attr.sa_vattr.va_nodeid,
741 			    namebuf, shortname, name83, 0);
742 		(void) smb_gather_dents_info((char *)ihdr,
743 		    snode->attr.sa_vattr.va_nodeid,
744 		    strlen(namebuf), namebuf, -1, (int *)&maxcnt,
745 		    &snode->attr, snode, shortname, name83);
746 		kmem_free(namebuf, MAXNAMELEN);
747 		return (0);
748 	}
749 
750 	if ((rc = smb_fsop_getdents(sr, sr->user_cr, dir_snode, cookie,
751 	    0, (int *)&maxcnt, (char *)ihdr, ihdr->pattern)) != 0) {
752 		if (rc == ENOENT) {
753 			kmem_free(namebuf, MAXNAMELEN);
754 			return (0);
755 		}
756 		kmem_free(namebuf, MAXNAMELEN);
757 		return (rc);
758 	}
759 
760 	if (*cookie != 0x7FFFFFFF && more)
761 		*more = 1;
762 
763 	kmem_free(namebuf, MAXNAMELEN);
764 	return (0);
765 }
766 
767 
768 
769 
770 /*
771  * smb_gather_dents_info
772  *
773  * The function will accept information of each directory entry and put
774  * the needed information in the buffer. It is passed as the call back
775  * function for smb_fsop_getdents() to gather trans2 find info.
776  *
777  * If the buffer space is not enough, -1 will be returned. Regardless
778  * of valid entry or not, 0 will be returned; however, only valid entry
779  * will be stored in the buffer.
780  */
781 int /*ARGSUSED*/
782 smb_gather_dents_info(
783     char	*args,
784     ino_t	fileid,
785     int		namelen,
786     char	*name,
787     uint32_t	cookie,
788     int32_t	*countp,
789     smb_attr_t	*attr,
790     smb_node_t	*snode,
791     char	*shortname,
792     char	*name83)
793 {
794 	/*LINTED E_BAD_PTR_CAST_ALIGN*/
795 	smb_dent_info_hdr_t	*ihdr = (smb_dent_info_hdr_t *)args;
796 	smb_dent_info_t		*ient;
797 	uint8_t			*v5_name = NULL;
798 	uint8_t			*np = (uint8_t *)name;
799 	int			reclen = sizeof (smb_dent_info_t) + namelen;
800 
801 	v5_name = kmem_alloc(MAXNAMELEN-1, KM_SLEEP);
802 
803 	if (!ihdr->uio.uio_iovcnt || ihdr->uio.uio_resid < reclen) {
804 		kmem_free(v5_name, MAXNAMELEN-1);
805 		smb_node_release(snode);
806 		return (-1);
807 	}
808 
809 	if (!smb_sattr_check(attr, name, ihdr->sattr)) {
810 		kmem_free(v5_name, MAXNAMELEN-1);
811 		smb_node_release(snode);
812 		return (0);
813 	}
814 
815 	if (catia_callback) {
816 		catia_callback(v5_name, (uint8_t *)name,  MAXNAMELEN-1);
817 		np = v5_name;
818 		reclen = sizeof (smb_dent_info_t) + strlen((char *)v5_name);
819 	}
820 
821 	ASSERT(snode);
822 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
823 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
824 
825 	/*
826 	 * Each entry needs to be properly aligned or we may get an alignment
827 	 * fault on sparc.
828 	 */
829 	ihdr->uio.uio_loffset = (offset_t)PTRALIGN(ihdr->uio.uio_loffset);
830 	/*LINTED E_BAD_PTR_CAST_ALIGN*/
831 	ient = (smb_dent_info_t *)&ihdr->iov->iov_base[ihdr->uio.uio_loffset];
832 
833 	ient->cookie = cookie;
834 	ient->attr = *attr;
835 	ient->snode = snode;
836 
837 	(void) strcpy(ient->name, (char *)np);
838 	(void) strcpy(ient->shortname, shortname);
839 	(void) strcpy(ient->name83, name83);
840 	ihdr->uio.uio_iov->iov_base = (char *)ient;
841 	ihdr->uio.uio_iov->iov_len = reclen;
842 
843 	ihdr->uio.uio_iov++;
844 	ihdr->uio.uio_iovcnt--;
845 	ihdr->uio.uio_resid -= reclen;
846 	ihdr->uio.uio_loffset += reclen;
847 
848 	kmem_free(v5_name, MAXNAMELEN-1);
849 	return (0);
850 }
851 
852 
853 
854 /*
855  * smb_trans2_find_process_ients
856  *
857  * This function encodes the directory entry information store in
858  * the iov structure of the ihdr structure.
859  *
860  * The total entries encoded will be returned. If the entries encoded
861  * is less than the total entries in the iov, the more field will
862  * be updated to 1. Also, the next cookie wil be updated as well.
863  */
864 int
865 smb_trans2_find_process_ients(
866     smb_request_t	*sr,
867     smb_xa_t		*xa,
868     smb_dent_info_hdr_t	*ihdr,
869     uint16_t		fflag,
870     uint16_t		infolev,
871     int			maxdata,
872     smb_node_t		*dir_snode,
873     int			*more,
874     uint32_t		*cookie)
875 {
876 	int i, err = 0;
877 	smb_dent_info_t *ient;
878 	uint32_t mb_flags = (sr->smb_flg2 & SMB_FLAGS2_UNICODE)
879 	    ? SMB_MSGBUF_UNICODE : 0;
880 
881 	for (i = 0; i < SMB_MAX_DENTS_IOVEC; i++) {
882 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
883 		if ((ient = (smb_dent_info_t *)ihdr->iov[i].iov_base) == 0)
884 			break;
885 
886 		/*
887 		 * Observed differences between our response and Windows
888 		 * response, which hasn't caused a problem yet!
889 		 *
890 		 * 1. The NextEntryOffset field for the last entry should
891 		 * be 0.  This code always calculate the record length
892 		 * and puts the result in the NextEntryOffset field.
893 		 *
894 		 * 2. The FileIndex field is always 0.  This code puts
895 		 * the cookie in the FileIndex field.
896 		 */
897 		err = smb_trans2_find_mbc_encode(sr, xa, ient, maxdata, infolev,
898 		    fflag, mb_flags, dir_snode, NULL);
899 
900 		if (err)
901 			break;
902 	}
903 
904 	/*
905 	 * Not enough space to store all the entries returned,
906 	 * which is indicated by setting more.
907 	 */
908 	if (more && err < 0) {
909 		*more = 1;
910 
911 		/*
912 		 * Assume the space will be at least enough for 1 entry.
913 		 */
914 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
915 		ient = (smb_dent_info_t *)ihdr->iov[i-1].iov_base;
916 		*cookie = ient->cookie;
917 	}
918 	return (i);
919 }
920 
921 /*
922  * smb_trans2_find_mbc_encode
923  *
924  * This function encodes the mbc for one directory entry.
925  *
926  * The function returns -1 when the max data requested by client
927  * is reached. If the entry is valid and successful encoded, 0
928  * will be returned; otherwise, 1 will be returned.
929  */
930 int /*ARGSUSED*/
931 smb_trans2_find_mbc_encode(
932     smb_request_t	*sr,
933     smb_xa_t		*xa,
934     smb_dent_info_t	*ient,
935     int			maxdata,
936     uint16_t		infolev,
937     uint16_t		fflag,
938     uint32_t		mb_flags,
939     smb_node_t		*dir_snode,
940     smb_node_t		*sd_snode)
941 {
942 	int uni_namelen;
943 	int shortlen;
944 	uint32_t next_entry_offset;
945 	char buf83[26];
946 	smb_msgbuf_t mb;
947 	uint32_t dattr = 0;
948 	uint32_t size32 = 0;
949 	uint64_t size64 = 0;
950 	smb_node_t *lnk_snode;
951 	smb_attr_t lnkattr;
952 	int rc;
953 
954 	uni_namelen = smb_ascii_or_unicode_strlen(sr, ient->name);
955 	if (uni_namelen == -1)
956 		return (1);
957 
958 	next_entry_offset = maxdata + uni_namelen;
959 
960 	if (MBC_ROOM_FOR(&xa->rep_data_mb, (maxdata + uni_namelen)) == 0)
961 		return (-1);
962 
963 	if (ient->attr.sa_vattr.va_type == VLNK) {
964 		rc = smb_fsop_lookup(sr, sr->user_cr, SMB_FOLLOW_LINKS,
965 		    sr->tid_tree->t_snode, dir_snode, ient->name, &lnk_snode,
966 		    &lnkattr, 0, 0);
967 
968 		/*
969 		 * We normally want to resolve the object to which a symlink
970 		 * refers so that CIFS clients can access sub-directories and
971 		 * find the correct association for files. This causes a
972 		 * problem, however, if a symlink in a sub-directory points
973 		 * to a parent directory (some UNIX GUI's create a symlink in
974 		 * $HOME/.desktop that points to the user's home directory).
975 		 * Some Windows applications (i.e. virus scanning) loop/hang
976 		 * trying to follow this recursive path and there is little
977 		 * we can do because the path is constructed on the client.
978 		 * skc_dirsymlink_enable allows an end-user to disable
979 		 * symlinks to directories. Symlinks to other object types
980 		 * should be unaffected.
981 		 */
982 		if (rc == 0) {
983 			if (smb_info.si.skc_dirsymlink_enable ||
984 			    (lnkattr.sa_vattr.va_type != VDIR)) {
985 				smb_node_release(ient->snode);
986 				ient->snode = lnk_snode;
987 				ient->attr = lnkattr;
988 			} else {
989 				smb_node_release(lnk_snode);
990 			}
991 		}
992 	}
993 
994 	if (infolev != SMB_FIND_FILE_NAMES_INFO) {
995 		size64 = smb_node_get_size(ient->snode, &ient->attr);
996 		size32 = (size64 > 0xFFFFFFFF) ? 0xFFFFFFFF : (uint32_t)size64;
997 		dattr = smb_mode_to_dos_attributes(&ient->attr);
998 	}
999 
1000 	switch (infolev) {
1001 	case SMB_INFO_STANDARD:
1002 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
1003 			(void) smb_encode_mbc(&xa->rep_data_mb, "l",
1004 			    ient->cookie);
1005 
1006 		(void) smb_encode_mbc(&xa->rep_data_mb, "%yyyllwbu", sr,
1007 		    ient->attr.sa_crtime.tv_sec ? ient->attr.sa_crtime.tv_sec :
1008 		    ient->attr.sa_vattr.va_mtime.tv_sec,
1009 		    ient->attr.sa_vattr.va_atime.tv_sec,
1010 		    ient->attr.sa_vattr.va_mtime.tv_sec,
1011 		    size32,
1012 		    size32,
1013 		    dattr,
1014 		    uni_namelen,
1015 		    ient->name);
1016 		break;
1017 
1018 	case SMB_INFO_QUERY_EA_SIZE:
1019 		if (fflag & SMB_FIND_RETURN_RESUME_KEYS)
1020 			(void) smb_encode_mbc(&xa->rep_data_mb, "l",
1021 			    ient->cookie);
1022 
1023 		(void) smb_encode_mbc(&xa->rep_data_mb, "%yyyllwlbu", sr,
1024 		    ient->attr.sa_crtime.tv_sec ? ient->attr.sa_crtime.tv_sec :
1025 		    ient->attr.sa_vattr.va_mtime.tv_sec,
1026 		    ient->attr.sa_vattr.va_atime.tv_sec,
1027 		    ient->attr.sa_vattr.va_mtime.tv_sec,
1028 		    size32,
1029 		    size32,
1030 		    dattr,
1031 		    0L,		/* EA Size */
1032 		    uni_namelen,
1033 		    ient->name);
1034 		break;
1035 
1036 	case SMB_FIND_FILE_DIRECTORY_INFO:
1037 		(void) smb_encode_mbc(&xa->rep_data_mb, "%llTTTTqqllu", sr,
1038 		    next_entry_offset,
1039 		    ient->cookie,
1040 		    ient->attr.sa_crtime.tv_sec ? &ient->attr.sa_crtime :
1041 		    &ient->attr.sa_vattr.va_mtime,
1042 		    &ient->attr.sa_vattr.va_atime,
1043 		    &ient->attr.sa_vattr.va_mtime,
1044 		    &ient->attr.sa_vattr.va_ctime,
1045 		    size64,
1046 		    size64,
1047 		    dattr,
1048 		    uni_namelen,
1049 		    ient->name);
1050 		break;
1051 
1052 	case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1053 		bzero(buf83, sizeof (buf83));
1054 		smb_msgbuf_init(&mb, (uint8_t *)buf83, sizeof (buf83),
1055 		    mb_flags);
1056 		if (smb_msgbuf_encode(&mb, "u", ient->shortname) < 0) {
1057 			smb_msgbuf_term(&mb);
1058 			return (-1);
1059 		}
1060 		shortlen = smb_ascii_or_unicode_strlen(sr, ient->shortname);
1061 
1062 		(void) smb_encode_mbc(&xa->rep_data_mb, "%llTTTTqqlllb.24cu",
1063 		    sr,
1064 		    next_entry_offset,
1065 		    ient->cookie,
1066 		    ient->attr.sa_crtime.tv_sec ? &ient->attr.sa_crtime :
1067 		    &ient->attr.sa_vattr.va_mtime,
1068 		    &ient->attr.sa_vattr.va_atime,
1069 		    &ient->attr.sa_vattr.va_mtime,
1070 		    &ient->attr.sa_vattr.va_ctime,
1071 		    size64,
1072 		    size64,
1073 		    dattr,
1074 		    uni_namelen,
1075 		    0L,
1076 		    shortlen,
1077 		    buf83,
1078 		    ient->name);
1079 
1080 		smb_msgbuf_term(&mb);
1081 		break;
1082 
1083 	case SMB_FIND_FILE_NAMES_INFO:
1084 		(void) smb_encode_mbc(&xa->rep_data_mb, "%lllu", sr,
1085 		    next_entry_offset,
1086 		    ient->cookie,
1087 		    uni_namelen,
1088 		    ient->name);
1089 		break;
1090 	}
1091 
1092 	return (0);
1093 }
1094 
1095 /*
1096  * Close a search started by a Trans2FindFirst2 request.
1097  */
1098 smb_sdrc_t
1099 smb_com_find_close2(smb_request_t *sr)
1100 {
1101 	if (smbsr_decode_vwv(sr, "w", &sr->smb_sid) != 0)
1102 		return (SDRC_ERROR_REPLY);
1103 
1104 	sr->sid_odir = smb_odir_lookup_by_sid(sr->tid_tree, sr->smb_sid);
1105 	if (sr->sid_odir == NULL) {
1106 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
1107 		return (SDRC_ERROR_REPLY);
1108 	}
1109 
1110 	smb_rdir_close(sr);
1111 
1112 	if (smbsr_encode_empty_result(sr))
1113 		return (SDRC_ERROR_REPLY);
1114 
1115 	return (SDRC_NORMAL_REPLY);
1116 }
1117