xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_odir.c (revision 0250c53ad267726f2438e3c6556199a0bbf588a2)
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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2022 Tintri by DDN, Inc. All rights reserved.
24  * Copyright 2022-2023 RackTop Systems, Inc.
25  */
26 
27 /*
28  * General Structures Layout
29  * -------------------------
30  *
31  * This is a simplified diagram showing the relationship between most of the
32  * main structures.
33  *
34  * +-------------------+
35  * |     SMB_INFO      |
36  * +-------------------+
37  *          |
38  *          |
39  *          v
40  * +-------------------+       +-------------------+      +-------------------+
41  * |     SESSION       |<----->|     SESSION       |......|      SESSION      |
42  * +-------------------+       +-------------------+      +-------------------+
43  *   |          |
44  *   |          |
45  *   |          v
46  *   |  +-------------------+     +-------------------+   +-------------------+
47  *   |  |       USER        |<--->|       USER        |...|       USER        |
48  *   |  +-------------------+     +-------------------+   +-------------------+
49  *   |
50  *   |
51  *   v
52  * +-------------------+       +-------------------+      +-------------------+
53  * |       TREE        |<----->|       TREE        |......|       TREE        |
54  * +-------------------+       +-------------------+      +-------------------+
55  *      |         |
56  *      |         |
57  *      |         v
58  *      |     +-------+       +-------+      +-------+
59  *      |     | OFILE |<----->| OFILE |......| OFILE |
60  *      |     +-------+       +-------+      +-------+
61  *      |
62  *      |
63  *      v
64  *  +-------+       +------+      +------+
65  *  | ODIR  |<----->| ODIR |......| ODIR |
66  *  +-------+       +------+      +------+
67  *
68  *
69  * Odir State Machine
70  * ------------------
71  *
72  *    +-------------------------+
73  *    |  SMB_ODIR_STATE_OPEN    |<----------- open / creation
74  *    +-------------------------+
75  *	    |            ^
76  *	    | (first)    | (last)
77  *	    | lookup     | release
78  *	    v            |
79  *    +-------------------------+
80  *    | SMB_ODIR_STATE_IN_USE   |----
81  *    +-------------------------+   | lookup / release / read
82  *	    |                ^-------
83  *	    | close
84  *	    |
85  *	    v
86  *    +-------------------------+
87  *    | SMB_ODIR_STATE_CLOSING  |----
88  *    +-------------------------+   | close / release / read
89  *	    |                ^-------
90  *	    | (last) release
91  *	    |
92  *	    v
93  *    +-------------------------+
94  *    | SMB_ODIR_STATE_CLOSED   |----------> deletion
95  *    +-------------------------+
96  *
97  *
98  * SMB_ODIR_STATE_OPEN
99  * - the odir exists in the list of odirs of its tree
100  * - lookup is valid in this state. It will place a hold on the odir
101  *   by incrementing the reference count and the odir will transition
102  *   to SMB_ODIR_STATE_IN_USE
103  * - read/close/release not valid in this state
104  *
105  * SMB_ODIR_STATE_IN_USE
106  * - the odir exists in the list of odirs of its tree.
107  * - lookup is valid in this state. It will place a hold on the odir
108  *   by incrementing the reference count.
109  * - if the last hold is released the odir will transition
110  *   back to SMB_ODIR_STATE_OPEN
111  * - if a close is received the odir will transition to
112  *   SMB_ODIR_STATE_CLOSING.
113  *
114  * SMB_ODIR_STATE_CLOSING
115  * - the odir exists in the list of odirs of its tree.
116  * - lookup will fail in this state.
117  * - when the last hold is released the odir will transition
118  *   to SMB_ODIR_STATE_CLOSED.
119  *
120  * SMB_ODIR_STATE_CLOSED
121  * - the odir exists in the list of odirs of its tree.
122  * - there are no users of the odir (refcnt == 0)
123  * - the odir is being removed from the tree's list and deleted.
124  * - lookup will fail in this state.
125  * - read/close/release not valid in this state
126  *
127  * Comments
128  * --------
129  *    The state machine of the odir structures is controlled by 3 elements:
130  *      - The list of odirs of the tree it belongs to.
131  *      - The mutex embedded in the structure itself.
132  *      - The reference count.
133  *
134  *    There's a mutex embedded in the odir structure used to protect its fields
135  *    and there's a lock embedded in the list of odirs of a tree. To
136  *    increment or to decrement the reference count the mutex must be entered.
137  *    To insert the odir into the list of odirs of the tree and to remove
138  *    the odir from it, the lock must be entered in RW_WRITER mode.
139  *
140  *    In order to avoid deadlocks, when both (mutex and lock of the odir
141  *    list) have to be entered, the lock must be entered first.
142  *
143  *
144  * Odir Interface
145  * ---------------
146  * smb_odir_open(char *pathname)
147  *	Create an odir representing the directory specified in pathname and
148  *	add it into the tree's list of odirs.
149  *	Returns NT status.
150  *
151  * smb_odir_openfh(smb_ofile_t *of)
152  *	Create an odir representing the directory specified by the
153  *	existing open handle (from a prior open of the directory).
154  *	Returns NT status.
155  *
156  * smb_odir_openat(smb_node_t *unode)
157  *	Create an odir representing the extended attribute directory
158  *	associated with the file (or directory) represented by unode
159  *	and add it into the tree's list of odirs.
160  *	Returns NT status.
161  *
162  * smb_odir_t *odir = smb_tree_lookup_odir(..., odid)
163  *	Find the odir corresponding to the specified odid in the tree's
164  *	list of odirs. Place a hold on the odir.
165  *
166  * smb_odir_read(..., smb_odirent_t *odirent)
167  *	Find the next directory entry in the odir and return it in odirent.
168  *
169  * smb_odir_read_fileinfo(..., smb_fileinfo_t *)
170  *	Find the next directory entry in the odir. Return the details of
171  *	the directory entry in smb_fileinfo_t. (See odir internals below)
172  *
173  * smb_odir_read_streaminfo(..., smb_streaminfo_t *)
174  *	Find the next named stream entry in the odir. Return the details of
175  *	the named stream in smb_streaminfo_t.
176  *
177  * smb_odir_close(smb_odir_t *odir)
178  *  Close the odir.
179  *  The caller of close must have a hold on the odir being closed.
180  *  The hold should be released after closing.
181  *
182  * smb_odir_release(smb_odir_t *odir)
183  *	Release the hold on the odir, obtained by lookup.
184  *
185  *
186  * Odir Internals
187  * --------------
188  * The odir object represent an open directory search. Each read operation
189  * provides the caller with a structure containing information  pertaining
190  * to the next directory entry that matches the search criteria, namely
191  * the filename or match pattern and, in the case of smb_odir_read_fileinfo(),
192  * the search attributes.
193  *
194  * The odir maintains a buffer (d_buf) of directory entries read from
195  * the filesystem via a vop_readdir. The buffer is populated when a read
196  * request (smb_odir_next_odirent) finds that the buffer is empty or that
197  * the end of the buffer has been reached, and also when a new client request
198  * (find next) begins.
199  *
200  * The data in d_buf (that which is returned from the file system) can
201  * be in one of two formats. If the file system supports extended directory
202  * entries we request that the data be returned as edirent_t structures. If
203  * it does not the data will be returned as dirent64_t structures. For
204  * convenience, when the next directory entry is read from d_buf by
205  * smb_odir_next_odirent it is translated into an smb_odirent_t.
206  *
207  * smb_odir_read_fileinfo
208  * The processing required to obtain the information to populate the caller's
209  * smb_fileinfo_t differs depending upon whether the directory search is for a
210  * single specified filename or for multiple files matching a search pattern.
211  * Thus smb_odir_read_fileinfo uses two static functions:
212  * smb_odir_single_fileinfo - obtains the smb_fileinfo_t info for the single
213  * filename as specified in smb_odir_open request.
214  * smb_odir_wildcard_fileinfo - obtains the smb_fileinfo_t info for the filename
215  * returned from the smb_odir_next_odirent. This is called in a loop until
216  * an entry matching the search criteria is found or no more entries exist.
217  *
218  * If a directory entry is a VLNK, the name returned in the smb_fileinfo_t
219  * is the name of the directory entry but the attributes are the attribites
220  * of the file that is the target of the link. If the link target cannot
221  * be found the attributes returned are the attributes of the link itself.
222  *
223  * smb_odir_read_streaminfo
224  * In order for an odir to provide information about stream files it
225  * must be opened with smb_odir_openat(). smb_odir_read_streaminfo() can
226  * then be used to obtain the name and size of named stream files.
227  *
228  * Resuming a Search
229  * -----------------
230  * A directory search often consists of multiple client requests: an initial
231  * find_first request followed by zero or more find_next requests and a
232  * find_close request.
233  * The find_first request will open and lookup the odir, read its desired
234  * number of entries from the odir, then release the odir and return.
235  * A find_next request will lookup the odir and read its desired number of
236  * entries from the odir, then release the odir and return.
237  * At the end of the search the find_close request will close the odir.
238  *
239  * In order to be able to resume a directory search (find_next) the odir
240  * provides the capability for the caller to save one or more resume points
241  * (cookies) at the end of a request, and to specify which resume point
242  * (cookie) to restart from at the beginning of the next search.
243  *	smb_odir_save_cookie(..., cookie)
244  *	smb_odir_resume_at(smb_odir_resume_t *resume)
245  * A search can be resumed at a specified resume point (cookie), the resume
246  * point (cookie) stored at a specified index in the d_cookies array, or
247  * a specified filename. The latter (specified filename) is not yet supported.
248  *
249  * See smb_search, smb_find, smb_find_unique, and smb_trans2_find for details
250  */
251 
252 #include <smbsrv/smb_kproto.h>
253 #include <smbsrv/smb_fsops.h>
254 #include <smbsrv/smb_share.h>
255 #include <sys/extdirent.h>
256 
257 /* static functions */
258 static smb_odir_t *smb_odir_create(smb_request_t *, smb_node_t *,
259     const char *, uint16_t, uint16_t, cred_t *);
260 static int smb_odir_single_fileinfo(smb_request_t *, smb_odir_t *,
261     smb_fileinfo_t *);
262 static int smb_odir_wildcard_fileinfo(smb_request_t *, smb_odir_t *,
263     smb_odirent_t *, smb_fileinfo_t *);
264 static int smb_odir_next_odirent(smb_odir_t *, smb_odirent_t *);
265 static boolean_t smb_odir_lookup_link(smb_request_t *, smb_odir_t *,
266     char *, smb_node_t **);
267 static boolean_t smb_odir_match_name(smb_odir_t *, smb_odirent_t *);
268 static void smb_odir_delete(void *);
269 
270 
271 /*
272  * smb_odir_openpath
273  *
274  * Create an odir representing the directory specified in pathname.
275  *
276  * Returns:
277  *    NT Status
278  */
279 uint32_t
280 smb_odir_openpath(smb_request_t *sr, char *path, uint16_t sattr,
281     uint32_t flags, smb_odir_t **odp)
282 {
283 	int		rc;
284 	smb_tree_t	*tree;
285 	smb_node_t	*dnode;
286 	char		pattern[MAXNAMELEN];
287 	uint16_t	odid;
288 	cred_t		*cr;
289 
290 	ASSERT(sr);
291 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
292 	ASSERT(sr->tid_tree);
293 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
294 	*odp = NULL;
295 
296 	tree = sr->tid_tree;
297 
298 	if (sr->session->dialect < NT_LM_0_12)
299 		smb_convert_wildcards(path);
300 
301 	rc = smb_pathname_reduce(sr, sr->user_cr, path,
302 	    tree->t_snode, tree->t_snode, &dnode, pattern);
303 	if (rc != 0)
304 		return (smb_errno2status(rc));
305 
306 	if (!smb_node_is_dir(dnode)) {
307 		smb_node_release(dnode);
308 		return (NT_STATUS_OBJECT_PATH_NOT_FOUND);
309 	}
310 
311 	if (smb_fsop_access(sr, sr->user_cr, dnode, FILE_LIST_DIRECTORY) != 0) {
312 		smb_node_release(dnode);
313 		return (NT_STATUS_ACCESS_DENIED);
314 	}
315 
316 	if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) {
317 		smb_node_release(dnode);
318 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
319 	}
320 
321 	if (flags & SMB_ODIR_OPENF_BACKUP_INTENT)
322 		cr = smb_user_getprivcred(sr->uid_user);
323 	else
324 		cr = sr->uid_user->u_cred;
325 
326 	*odp = smb_odir_create(sr, dnode, pattern, sattr, odid, cr);
327 	smb_node_release(dnode);
328 
329 	return (0);
330 }
331 
332 /*
333  * smb_odir_openfh
334  *
335  * Create an odir representing the directory already opened on "of".
336  *
337  * Returns:
338  *    NT status
339  */
340 uint32_t
341 smb_odir_openfh(smb_request_t *sr, const char *pattern, uint16_t sattr,
342     smb_odir_t **odp)
343 {
344 	smb_ofile_t	*of = sr->fid_ofile;
345 
346 	*odp = NULL;
347 
348 	if (of->f_node == NULL || !smb_node_is_dir(of->f_node))
349 		return (NT_STATUS_INVALID_PARAMETER);
350 
351 	if ((of->f_granted_access & FILE_LIST_DIRECTORY) == 0)
352 		return (NT_STATUS_ACCESS_DENIED);
353 
354 	*odp = smb_odir_create(sr, of->f_node, pattern, sattr, 0, of->f_cr);
355 
356 	return (0);
357 }
358 
359 /*
360  * smb_odir_openat
361  *
362  * Create an odir representing the extended attribute directory
363  * associated with the file (or directory) represented by unode.
364  *
365  * Returns:
366  *    NT status
367  */
368 uint32_t
369 smb_odir_openat(smb_request_t *sr, smb_node_t *unode, smb_odir_t **odp,
370     boolean_t restricted)
371 {
372 	char		pattern[SMB_STREAM_PREFIX_LEN + 2];
373 	vnode_t		*xattr_dvp;
374 	cred_t		*cr;
375 	smb_node_t	*xattr_dnode;
376 	int		rc;
377 
378 	ASSERT(sr);
379 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
380 	ASSERT(unode);
381 	ASSERT(unode->n_magic == SMB_NODE_MAGIC);
382 	*odp = NULL;
383 
384 	if (SMB_TREE_CONTAINS_NODE(sr, unode) == 0 ||
385 	    SMB_TREE_HAS_ACCESS(sr, ACE_LIST_DIRECTORY) == 0)
386 		return (NT_STATUS_ACCESS_DENIED);
387 
388 	cr = zone_kcred();
389 
390 	/* find the xattrdir vnode */
391 	rc = smb_vop_lookup_xattrdir(unode->vp, &xattr_dvp, LOOKUP_XATTR, cr);
392 	if (rc != 0)
393 		return (smb_errno2status(rc));
394 
395 	/* lookup the xattrdir's smb_node */
396 	xattr_dnode = smb_node_lookup(sr, NULL, cr, xattr_dvp, XATTR_DIR,
397 	    unode, NULL);
398 	VN_RELE(xattr_dvp);
399 	if (xattr_dnode == NULL)
400 		return (NT_STATUS_NO_MEMORY);
401 
402 	(void) snprintf(pattern, sizeof (pattern), "%s*", SMB_STREAM_PREFIX);
403 	*odp = smb_odir_create(sr, xattr_dnode, pattern,
404 	    SMB_SEARCH_ATTRIBUTES, 0, cr);
405 
406 	/* Causes restricted stream names to be hidden from the caller */
407 	if (restricted)
408 		(*odp)->d_flags |= SMB_ODIR_FLAG_RESTRICTED;
409 
410 	smb_node_release(xattr_dnode);
411 	return (0);
412 }
413 
414 /*
415  * smb_odir_hold
416  *
417  * A hold will only be granted if the odir is open or in_use.
418  */
419 boolean_t
420 smb_odir_hold(smb_odir_t *od)
421 {
422 	ASSERT(od);
423 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
424 
425 	mutex_enter(&od->d_mutex);
426 
427 	switch (od->d_state) {
428 	case SMB_ODIR_STATE_OPEN:
429 		od->d_refcnt++;
430 		od->d_state = SMB_ODIR_STATE_IN_USE;
431 		break;
432 	case SMB_ODIR_STATE_IN_USE:
433 		od->d_refcnt++;
434 		break;
435 	case SMB_ODIR_STATE_CLOSING:
436 	case SMB_ODIR_STATE_CLOSED:
437 	default:
438 		mutex_exit(&od->d_mutex);
439 		return (B_FALSE);
440 	}
441 
442 	mutex_exit(&od->d_mutex);
443 	return (B_TRUE);
444 }
445 
446 /*
447  * If the odir is in SMB_ODIR_STATE_CLOSING and this release results in
448  * a refcnt of 0, change the state to SMB_ODIR_STATE_CLOSED and post the
449  * object for deletion.  Object deletion is deferred to avoid modifying
450  * a list while an iteration may be in progress.
451  */
452 void
453 smb_odir_release(smb_odir_t *od)
454 {
455 	smb_tree_t *tree = od->d_tree;
456 
457 	SMB_ODIR_VALID(od);
458 
459 	mutex_enter(&od->d_mutex);
460 	ASSERT(od->d_refcnt > 0);
461 
462 	switch (od->d_state) {
463 	case SMB_ODIR_STATE_OPEN:
464 		break;
465 	case SMB_ODIR_STATE_IN_USE:
466 		od->d_refcnt--;
467 		if (od->d_refcnt == 0)
468 			od->d_state = SMB_ODIR_STATE_OPEN;
469 		break;
470 	case SMB_ODIR_STATE_CLOSING:
471 		od->d_refcnt--;
472 		if (od->d_refcnt == 0) {
473 			od->d_state = SMB_ODIR_STATE_CLOSED;
474 			smb_llist_post(&tree->t_odir_list, od,
475 			    smb_odir_delete);
476 		}
477 		break;
478 	case SMB_ODIR_STATE_CLOSED:
479 	default:
480 		break;
481 	}
482 
483 	mutex_exit(&od->d_mutex);
484 }
485 
486 /*
487  * smb_odir_close
488  */
489 void
490 smb_odir_close(smb_odir_t *od)
491 {
492 	ASSERT(od);
493 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
494 
495 	mutex_enter(&od->d_mutex);
496 	ASSERT(od->d_refcnt > 0);
497 	switch (od->d_state) {
498 	case SMB_ODIR_STATE_OPEN:
499 		break;
500 	case SMB_ODIR_STATE_IN_USE:
501 		od->d_state = SMB_ODIR_STATE_CLOSING;
502 		break;
503 	case SMB_ODIR_STATE_CLOSING:
504 	case SMB_ODIR_STATE_CLOSED:
505 	default:
506 		break;
507 	}
508 	mutex_exit(&od->d_mutex);
509 }
510 
511 /*
512  * smb_odir_read
513  *
514  * Find the next directory entry matching the search pattern.
515  * No search attribute matching is performed.
516  *
517  * Returns:
518  *  0 - success.
519  *      - If a matching entry was found eof will be B_FALSE and
520  *        odirent will be populated.
521  * ENOENT
522  *      - If we've scanned to the end, eof will be B_TRUE.
523  * errno - other errors
524  */
525 int
526 smb_odir_read(smb_request_t *sr, smb_odir_t *od,
527     smb_odirent_t *odirent, boolean_t *eof)
528 {
529 	int		rc;
530 
531 	ASSERT(sr);
532 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
533 	ASSERT(od);
534 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
535 	ASSERT(odirent);
536 
537 	mutex_enter(&od->d_mutex);
538 	ASSERT(od->d_refcnt > 0);
539 
540 	switch (od->d_state) {
541 	case SMB_ODIR_STATE_IN_USE:
542 	case SMB_ODIR_STATE_CLOSING:
543 		break;
544 	case SMB_ODIR_STATE_OPEN:
545 	case SMB_ODIR_STATE_CLOSED:
546 	default:
547 		mutex_exit(&od->d_mutex);
548 		return (EBADF);
549 	}
550 
551 	for (;;) {
552 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
553 			break;
554 		if (smb_odir_match_name(od, odirent))
555 			break;
556 	}
557 
558 	mutex_exit(&od->d_mutex);
559 
560 	switch (rc) {
561 	case 0:
562 		*eof = B_FALSE;
563 		return (0);
564 	case ENOENT:
565 		*eof = B_TRUE;
566 		/* FALLTHROUGH */
567 	default:
568 		return (rc);
569 	}
570 }
571 
572 /*
573  * smb_odir_read_fileinfo
574  *
575  * Find the next directory entry matching the search pattern
576  * and attributes: od->d_pattern and od->d_sattr.
577  *
578  * If the search pattern specifies a single filename call
579  * smb_odir_single_fileinfo to get the file attributes and
580  * populate the caller's smb_fileinfo_t.
581  *
582  * If the search pattern contains wildcards call smb_odir_next_odirent
583  * to get the next directory entry then. Repeat until a matching
584  * filename is found. Call smb_odir_wildcard_fileinfo to get the
585  * file attributes and populate the caller's smb_fileinfo_t.
586  * This is repeated until a file matching the search criteria is found.
587  *
588  * Returns:
589  *  0 - success.
590  *      - If a matching entry was found eof will be B_FALSE and
591  *        fileinfo will be populated.
592  * ENOENT
593  *      - If at end of dir, eof will be B_TRUE.
594  * errno - other error
595  */
596 int
597 smb_odir_read_fileinfo(smb_request_t *sr, smb_odir_t *od,
598     smb_fileinfo_t *fileinfo, uint16_t *eof)
599 {
600 	int		rc, errnum;
601 	smb_odirent_t	*odirent;
602 
603 	ASSERT(sr);
604 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
605 	ASSERT(od);
606 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
607 	ASSERT(fileinfo);
608 
609 	mutex_enter(&od->d_mutex);
610 	ASSERT(od->d_refcnt > 0);
611 
612 	switch (od->d_state) {
613 	case SMB_ODIR_STATE_IN_USE:
614 	case SMB_ODIR_STATE_CLOSING:
615 		break;
616 	case SMB_ODIR_STATE_OPEN:
617 	case SMB_ODIR_STATE_CLOSED:
618 	default:
619 		mutex_exit(&od->d_mutex);
620 		return (SET_ERROR(EBADF));
621 	}
622 
623 	if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) {
624 		if (od->d_eof)
625 			rc = SET_ERROR(ENOENT);
626 		else
627 			rc = smb_odir_single_fileinfo(sr, od, fileinfo);
628 		od->d_eof = B_TRUE;
629 	} else {
630 		odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
631 		for (;;) {
632 			mutex_enter(&sr->sr_mutex);
633 			if (sr->sr_state != SMB_REQ_STATE_ACTIVE) {
634 				mutex_exit(&sr->sr_mutex);
635 				rc = SET_ERROR(EINTR);
636 				break;
637 			}
638 			mutex_exit(&sr->sr_mutex);
639 			bzero(fileinfo, sizeof (smb_fileinfo_t));
640 			if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
641 				break;
642 
643 			/* skip non utf8 filename */
644 			if (u8_validate(odirent->od_name,
645 			    strlen(odirent->od_name), NULL,
646 			    U8_VALIDATE_ENTIRE, &errnum) < 0)
647 				continue;
648 
649 			if (!smb_odir_match_name(od, odirent))
650 				continue;
651 
652 			rc = smb_odir_wildcard_fileinfo(sr, od, odirent,
653 			    fileinfo);
654 			if (rc == 0)
655 				break;
656 		}
657 		kmem_free(odirent, sizeof (smb_odirent_t));
658 	}
659 	mutex_exit(&od->d_mutex);
660 
661 	switch (rc) {
662 	case 0:
663 		*eof = 0;
664 		return (0);
665 	case ENOENT:
666 		*eof = 1;	/* per. FindFirst, FindNext spec. */
667 		/* FALLTHROUGH */
668 	default:
669 		return (rc);
670 	}
671 }
672 
673 /*
674  * smb_odir_read_streaminfo
675  *
676  * Find the next directory entry whose name begins with SMB_STREAM_PREFIX,
677  * and thus represents an NTFS named stream.
678  * No search attribute matching is performed.
679  * No case conflict name mangling is required for NTFS named stream names.
680  *
681  * Returns:
682  *  0 - success.
683  *      - If a matching entry was found eof will be B_FALSE and
684  *        sinfo will be populated.
685  *      - If there are no matching entries eof will be B_TRUE.
686  * errno - error
687  */
688 int
689 smb_odir_read_streaminfo(smb_request_t *sr, smb_odir_t *od,
690     smb_streaminfo_t *sinfo, boolean_t *eof)
691 {
692 	int		rc;
693 	cred_t		*kcr;
694 	smb_odirent_t	*odirent;
695 	smb_node_t	*fnode;
696 	smb_attr_t	attr;
697 
698 	ASSERT(sr);
699 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
700 	ASSERT(od);
701 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
702 	ASSERT(sinfo);
703 
704 	kcr = zone_kcred();
705 
706 	mutex_enter(&od->d_mutex);
707 	ASSERT(od->d_refcnt > 0);
708 
709 	switch (od->d_state) {
710 	case SMB_ODIR_STATE_IN_USE:
711 	case SMB_ODIR_STATE_CLOSING:
712 		break;
713 	case SMB_ODIR_STATE_OPEN:
714 	case SMB_ODIR_STATE_CLOSED:
715 	default:
716 		mutex_exit(&od->d_mutex);
717 		return (EBADF);
718 	}
719 
720 	/* Check that odir represents an xattr directory */
721 	if (!(od->d_flags & SMB_ODIR_FLAG_XATTR)) {
722 		*eof = B_TRUE;
723 		mutex_exit(&od->d_mutex);
724 		return (0);
725 	}
726 
727 	odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
728 	bzero(&attr, sizeof (attr));
729 
730 	for (;;) {
731 		bzero(sinfo, sizeof (smb_streaminfo_t));
732 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
733 			break;
734 
735 		if (strncmp(odirent->od_name, SMB_STREAM_PREFIX,
736 		    SMB_STREAM_PREFIX_LEN)) {
737 			continue;
738 		}
739 
740 		/*
741 		 * Hide streams that would be restricted if the caller
742 		 * is also restricted.
743 		 */
744 		if ((od->d_flags & SMB_ODIR_FLAG_RESTRICTED) != 0 &&
745 		    smb_strname_restricted(odirent->od_name))
746 			continue;
747 
748 		rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
749 		    od->d_dnode, odirent->od_name, &fnode);
750 		if (rc == 0) {
751 			/*
752 			 * We just need the file sizes, and don't want
753 			 * EACCES failures here, so use kcred and pass
754 			 * NULL as the sr to skip sr->fid-ofile checks.
755 			 */
756 			attr.sa_mask = SMB_AT_SIZE | SMB_AT_ALLOCSZ;
757 			rc = smb_node_getattr(NULL, fnode, kcr, NULL, &attr);
758 			smb_node_release(fnode);
759 		}
760 
761 		if (rc == 0) {
762 			(void) strlcpy(sinfo->si_name,
763 			    odirent->od_name + SMB_STREAM_PREFIX_LEN,
764 			    sizeof (sinfo->si_name));
765 			sinfo->si_size = attr.sa_vattr.va_size;
766 			sinfo->si_alloc_size = attr.sa_allocsz;
767 			break;
768 		}
769 	}
770 	mutex_exit(&od->d_mutex);
771 
772 	kmem_free(odirent, sizeof (smb_odirent_t));
773 
774 	switch (rc) {
775 	case 0:
776 		*eof = B_FALSE;
777 		return (0);
778 	case ENOENT:
779 		*eof = B_TRUE;
780 		return (0);
781 	default:
782 		return (rc);
783 	}
784 }
785 
786 /*
787  * smb_odir_save_cookie
788  *
789  * Callers can save up to SMB_MAX_SEARCH cookies in the odir
790  * to be used as resume points for a 'find next' request.
791  */
792 void
793 smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie)
794 {
795 	ASSERT(od);
796 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
797 	ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH);
798 
799 	mutex_enter(&od->d_mutex);
800 	od->d_cookies[idx] = cookie;
801 	mutex_exit(&od->d_mutex);
802 }
803 
804 /*
805  * smb_odir_save_fname
806  *
807  * Save a filename / offset pair, which are basically a
808  * one entry cache.  See smb_com_trans2_find_next2.
809  */
810 void
811 smb_odir_save_fname(smb_odir_t *od, uint32_t cookie, const char *fname)
812 {
813 	ASSERT(od);
814 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
815 
816 	mutex_enter(&od->d_mutex);
817 
818 	od->d_last_cookie = cookie;
819 	bzero(od->d_last_name, MAXNAMELEN);
820 	if (fname != NULL)
821 		(void) strlcpy(od->d_last_name, fname, MAXNAMELEN);
822 
823 	mutex_exit(&od->d_mutex);
824 }
825 
826 /*
827  * smb_odir_resume_at
828  *
829  * If SMB_ODIR_FLAG_WILDCARDS is not set, and we're rewinding,
830  * assume we're no longer at EOF.
831  *
832  * Wildcard searching can be resumed from:
833  * - the cookie saved at a specified index (SMBsearch, SMBfind).
834  * - a specified cookie (SMB_trans2_find)
835  * - a specified filename (SMB_trans2_find) - NOT SUPPORTED.
836  *   Defaults to continuing from where the last search ended.
837  *
838  * Continuation from where the last search ended (SMB_trans2_find)
839  * is implemented by saving the last cookie at a specific index (0)
840  * smb_odir_resume_at indicates a new request, so reset od->d_bufptr
841  * and d_eof to force a vop_readdir.
842  */
843 void
844 smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume)
845 {
846 	uint64_t save_offset;
847 
848 	ASSERT(od);
849 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
850 	ASSERT(resume);
851 
852 	if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) {
853 		if (resume->or_type == SMB_ODIR_RESUME_COOKIE)
854 			od->d_eof = B_FALSE;
855 		return;
856 	}
857 	mutex_enter(&od->d_mutex);
858 
859 	save_offset = od->d_offset;
860 	switch (resume->or_type) {
861 
862 	default:
863 	case SMB_ODIR_RESUME_CONT:
864 		/* Continue where we left off. */
865 		break;
866 
867 	case SMB_ODIR_RESUME_IDX:
868 		/*
869 		 * This is used only by the (ancient) SMB_SEARCH.
870 		 * Modern clients use trans2 FindFirst, FindNext.
871 		 */
872 		ASSERT(resume->or_idx >= 0);
873 		ASSERT(resume->or_idx < SMB_MAX_SEARCH);
874 
875 		if ((resume->or_idx < 0) ||
876 		    (resume->or_idx >= SMB_MAX_SEARCH)) {
877 			resume->or_idx = 0;
878 		}
879 		od->d_offset = od->d_cookies[resume->or_idx];
880 		break;
881 
882 	case SMB_ODIR_RESUME_COOKIE:
883 		od->d_offset = resume->or_cookie;
884 		break;
885 
886 	case SMB_ODIR_RESUME_FNAME:
887 		/*
888 		 * If the name matches the last one saved,
889 		 * use the offset that was saved with it in
890 		 * the odir.  Otherwise use the cookie value
891 		 * in the resume data from the client.
892 		 */
893 		if (strcmp(resume->or_fname, od->d_last_name) &&
894 		    od->d_last_cookie != 0) {
895 			od->d_offset = od->d_last_cookie;
896 		} else if (resume->or_cookie != 0) {
897 			od->d_offset = resume->or_cookie;
898 		} /* else continue where we left off */
899 		break;
900 	}
901 
902 	if (od->d_offset != save_offset) {
903 		/* Force a vop_readdir to refresh d_buf */
904 		od->d_bufptr = NULL;
905 		od->d_eof = B_FALSE;
906 	}
907 
908 	mutex_exit(&od->d_mutex);
909 }
910 
911 
912 /* *** static functions *** */
913 
914 /*
915  * smb_odir_create
916  * Allocate and populate an odir obect and add it to the tree's list.
917  */
918 static smb_odir_t *
919 smb_odir_create(smb_request_t *sr, smb_node_t *dnode,
920     const char *pattern, uint16_t sattr, uint16_t odid, cred_t *cr)
921 {
922 	smb_odir_t	*od;
923 	smb_tree_t	*tree;
924 
925 	ASSERT(sr);
926 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
927 	ASSERT(sr->tid_tree);
928 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
929 	ASSERT(dnode);
930 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
931 
932 	tree = sr->tid_tree;
933 
934 	od = kmem_cache_alloc(smb_cache_odir, KM_SLEEP);
935 	bzero(od, sizeof (smb_odir_t));
936 
937 	mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL);
938 
939 	/*
940 	 * Return this to the caller as if they had done
941 	 * smb_tree_lookup_odir() to obtain the odir.
942 	 */
943 	od->d_refcnt = 1;
944 	od->d_state = SMB_ODIR_STATE_IN_USE;
945 	od->d_magic = SMB_ODIR_MAGIC;
946 	od->d_opened_by_pid = sr->smb_pid;
947 	od->d_session = tree->t_session;
948 	od->d_cred = cr;
949 	/*
950 	 * grab a ref for od->d_user
951 	 * released in  smb_odir_delete()
952 	 */
953 	smb_user_hold_internal(sr->uid_user);
954 	od->d_user = sr->uid_user;
955 	od->d_tree = tree;
956 	od->d_dnode = dnode;
957 	smb_node_ref(dnode);
958 	od->d_odid = odid;
959 	od->d_sattr = sattr;
960 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
961 	od->d_flags = 0;
962 	if (smb_contains_wildcards(od->d_pattern))
963 		od->d_flags |= SMB_ODIR_FLAG_WILDCARDS;
964 	if (vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS))
965 		od->d_flags |= SMB_ODIR_FLAG_EDIRENT;
966 	if (smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE))
967 		od->d_flags |= SMB_ODIR_FLAG_IGNORE_CASE;
968 	if (smb_tree_has_feature(tree, SMB_TREE_SHORTNAMES))
969 		od->d_flags |= SMB_ODIR_FLAG_SHORTNAMES;
970 	if (smb_tree_has_feature(tree, SMB_TREE_ACEMASKONACCESS))
971 		od->d_flags |= SMB_ODIR_FLAG_ACEACCESS;
972 	if (SMB_TREE_SUPPORTS_CATIA(sr))
973 		od->d_flags |= SMB_ODIR_FLAG_CATIA;
974 	if (SMB_TREE_SUPPORTS_ABE(sr))
975 		od->d_flags |= SMB_ODIR_FLAG_ABE;
976 	if (dnode->flags & NODE_XATTR_DIR)
977 		od->d_flags |= SMB_ODIR_FLAG_XATTR;
978 	od->d_eof = B_FALSE;
979 
980 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
981 	smb_llist_insert_tail(&tree->t_odir_list, od);
982 	smb_llist_exit(&tree->t_odir_list);
983 
984 	atomic_inc_32(&tree->t_session->s_dir_cnt);
985 	return (od);
986 }
987 
988 /*
989  * Set a new pattern, attributes, and rewind.
990  */
991 void
992 smb_odir_reopen(smb_odir_t *od, const char *pattern, uint16_t sattr)
993 {
994 
995 	SMB_ODIR_VALID(od);
996 
997 	mutex_enter(&od->d_mutex);
998 	od->d_sattr = sattr;
999 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
1000 	if (smb_contains_wildcards(od->d_pattern))
1001 		od->d_flags |= SMB_ODIR_FLAG_WILDCARDS;
1002 	else
1003 		od->d_flags &= ~SMB_ODIR_FLAG_WILDCARDS;
1004 
1005 	/* Internal smb_odir_resume_at */
1006 	od->d_offset = 0;
1007 	od->d_bufptr = NULL;
1008 	od->d_eof = B_FALSE;
1009 
1010 	mutex_exit(&od->d_mutex);
1011 }
1012 
1013 /*
1014  * Delete an odir.
1015  *
1016  * Remove the odir from the tree list before freeing resources
1017  * associated with the odir.
1018  */
1019 static void
1020 smb_odir_delete(void *arg)
1021 {
1022 	smb_tree_t	*tree;
1023 	smb_odir_t	*od = (smb_odir_t *)arg;
1024 
1025 	SMB_ODIR_VALID(od);
1026 	ASSERT(od->d_refcnt == 0);
1027 	ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED);
1028 
1029 	tree = od->d_tree;
1030 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
1031 	smb_llist_remove(&tree->t_odir_list, od);
1032 	if (od->d_odid != 0)
1033 		smb_idpool_free(&tree->t_odid_pool, od->d_odid);
1034 	atomic_dec_32(&tree->t_session->s_dir_cnt);
1035 	smb_llist_exit(&tree->t_odir_list);
1036 
1037 	/*
1038 	 * This odir is no longer on t_odir_list, however...
1039 	 *
1040 	 * This is called via smb_llist_post, which means it may run
1041 	 * BEFORE smb_odir_release drops d_mutex (if another thread
1042 	 * flushes the delete queue before we do).  Synchronize.
1043 	 */
1044 	mutex_enter(&od->d_mutex);
1045 	mutex_exit(&od->d_mutex);
1046 
1047 	od->d_magic = 0;
1048 	smb_node_release(od->d_dnode);
1049 	smb_user_release(od->d_user);
1050 	mutex_destroy(&od->d_mutex);
1051 	kmem_cache_free(smb_cache_odir, od);
1052 }
1053 
1054 boolean_t smb_use_fs_abe = B_FALSE;
1055 
1056 /*
1057  * smb_odir_next_odirent
1058  *
1059  * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer
1060  * is empty or we've reached the end of it), read the next set of
1061  * entries from the file system (vop_readdir).
1062  *
1063  * File systems which support VFSFT_EDIRENT_FLAGS will return the
1064  * directory entries as a buffer of edirent_t structure. Others will
1065  * return a buffer of dirent64_t structures.  For simplicity translate
1066  * the data into an smb_odirent_t structure.
1067  * The ed_name/d_name in d_buf is NULL terminated by the file system.
1068  *
1069  * Some file systems can have directories larger than SMB_MAXDIRSIZE.
1070  * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT and set d_eof
1071  * to true to stop subsequent calls to smb_vop_readdir.
1072  *
1073  * Returns:
1074  *      0 - success. odirent is populated with the next directory entry
1075  * ENOENT - no more directory entries
1076  *  errno - error
1077  */
1078 static int
1079 smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent)
1080 {
1081 	int		rc;
1082 	int		reclen;
1083 	int		eof;
1084 	dirent64_t	*dp;
1085 	edirent_t	*edp;
1086 	char		*np;
1087 	uint32_t	rddir_flags = 0;
1088 
1089 	ASSERT(MUTEX_HELD(&od->d_mutex));
1090 
1091 	bzero(odirent, sizeof (smb_odirent_t));
1092 
1093 	/*
1094 	 * VOP_READDIR() won't return until either the buffer has filled or the
1095 	 * end of the directory is reached. As a result, when we pass this flag,
1096 	 * calls on very large directories can take an unacceptably long time to
1097 	 * complete when the user doesn't have access to most entries. Until we
1098 	 * can cap the amount of time spent in this call, callers will manually
1099 	 * apply ABE to returned entries, instead of passing this flag.
1100 	 */
1101 	if (smb_use_fs_abe && (od->d_flags & SMB_ODIR_FLAG_ABE) != 0)
1102 		rddir_flags |= SMB_ABE;
1103 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
1104 		rddir_flags |= SMB_EDIRENT;
1105 
1106 	if (od->d_bufptr != NULL) {
1107 		if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
1108 			reclen = od->d_edp->ed_reclen;
1109 		else
1110 			reclen = od->d_dp->d_reclen;
1111 
1112 		if (reclen == 0) {
1113 			od->d_bufptr = NULL;
1114 		} else {
1115 			od->d_bufptr += reclen;
1116 			if (od->d_bufptr >= od->d_buf + od->d_bufsize)
1117 				od->d_bufptr = NULL;
1118 		}
1119 	}
1120 
1121 	if (od->d_bufptr == NULL) {
1122 		if (od->d_eof)
1123 			return (ENOENT);
1124 
1125 		od->d_bufsize = sizeof (od->d_buf);
1126 
1127 		rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset,
1128 		    od->d_buf, &od->d_bufsize, &eof, rddir_flags, od->d_cred);
1129 
1130 		if ((rc == 0) && (od->d_bufsize == 0))
1131 			rc = ENOENT;
1132 
1133 		if (rc != 0) {
1134 			od->d_bufptr = NULL;
1135 			od->d_bufsize = 0;
1136 			return (rc);
1137 		}
1138 
1139 		od->d_eof = (eof != 0);
1140 		od->d_bufptr = od->d_buf;
1141 	}
1142 
1143 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
1144 		od->d_offset = od->d_edp->ed_off;
1145 	else
1146 		od->d_offset = od->d_dp->d_off;
1147 
1148 	if (od->d_offset >= SMB_MAXDIRSIZE) {
1149 		od->d_bufptr = NULL;
1150 		od->d_bufsize = 0;
1151 		od->d_eof = B_TRUE;
1152 		return (ENOENT);
1153 	}
1154 
1155 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) {
1156 		edp = od->d_edp;
1157 		odirent->od_ino = edp->ed_ino;
1158 		odirent->od_eflags = edp->ed_eflags;
1159 		np = edp->ed_name;
1160 	} else {
1161 		dp = od->d_dp;
1162 		odirent->od_ino = dp->d_ino;
1163 		odirent->od_eflags = 0;
1164 		np =  dp->d_name;
1165 	}
1166 
1167 	if ((od->d_flags & SMB_ODIR_FLAG_CATIA) &&
1168 	    ((od->d_flags & SMB_ODIR_FLAG_XATTR) == 0)) {
1169 		smb_vop_catia_v4tov5(np, odirent->od_name,
1170 		    sizeof (odirent->od_name));
1171 	} else {
1172 		(void) strlcpy(odirent->od_name, np,
1173 		    sizeof (odirent->od_name));
1174 	}
1175 
1176 	return (0);
1177 }
1178 
1179 /*
1180  * smb_odir_single_fileinfo
1181  *
1182  * Lookup the file identified by od->d_pattern.
1183  *
1184  * If the looked up file is a link, we attempt to lookup the link target
1185  * to use its attributes in place of those of the files's.
1186  * If we fail to lookup the target of the link we use the original
1187  * file's attributes.
1188  * Check if the attributes match the search attributes.
1189  *
1190  * Returns: 0 - success
1191  *     ENOENT - no match
1192  *      errno - error
1193  */
1194 static int
1195 smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od,
1196     smb_fileinfo_t *fileinfo)
1197 {
1198 	int		rc;
1199 	smb_node_t	*fnode, *tgt_node;
1200 	smb_attr_t	attr;
1201 	ino64_t		fid;
1202 	char		*name;
1203 	boolean_t	case_conflict = B_FALSE;
1204 	int		lookup_flags, flags = 0;
1205 	vnode_t		*vp;
1206 
1207 	ASSERT(sr);
1208 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
1209 	ASSERT(od);
1210 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
1211 
1212 	ASSERT(MUTEX_HELD(&od->d_mutex));
1213 	bzero(fileinfo, sizeof (smb_fileinfo_t));
1214 
1215 	rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
1216 	    od->d_dnode, od->d_pattern, &fnode);
1217 	if (rc != 0)
1218 		return (rc);
1219 
1220 	/*
1221 	 * If case sensitive, do a case insensitive smb_vop_lookup to
1222 	 * check for case conflict
1223 	 */
1224 	if (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) {
1225 		lookup_flags = SMB_IGNORE_CASE;
1226 		if (od->d_flags & SMB_ODIR_FLAG_CATIA)
1227 			lookup_flags |= SMB_CATIA;
1228 
1229 		rc = smb_vop_lookup(od->d_dnode->vp, fnode->od_name, &vp,
1230 		    NULL, lookup_flags, &flags, od->d_tree->t_snode->vp,
1231 		    NULL, od->d_cred);
1232 		if (rc != 0)
1233 			return (rc);
1234 		VN_RELE(vp);
1235 
1236 		if (flags & ED_CASE_CONFLICT)
1237 			case_conflict = B_TRUE;
1238 	}
1239 
1240 	bzero(&attr, sizeof (attr));
1241 	attr.sa_mask = SMB_AT_ALL;
1242 	rc = smb_node_getattr(NULL, fnode, zone_kcred(), NULL, &attr);
1243 	if (rc != 0) {
1244 		smb_node_release(fnode);
1245 		return (rc);
1246 	}
1247 
1248 
1249 	/* follow link to get target node & attr */
1250 	if (smb_node_is_symlink(fnode) &&
1251 	    smb_odir_lookup_link(sr, od, fnode->od_name, &tgt_node)) {
1252 		smb_node_release(fnode);
1253 		fnode = tgt_node;
1254 		attr.sa_mask = SMB_AT_ALL;
1255 		rc = smb_node_getattr(NULL, fnode, zone_kcred(), NULL, &attr);
1256 		if (rc != 0) {
1257 			smb_node_release(fnode);
1258 			return (rc);
1259 		}
1260 	}
1261 
1262 	/* check search attributes */
1263 	if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
1264 		smb_node_release(fnode);
1265 		return (ENOENT);
1266 	}
1267 
1268 	name = fnode->od_name;
1269 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1270 		fid = attr.sa_vattr.va_nodeid;
1271 		if (case_conflict || smb_needs_mangled(name)) {
1272 			smb_mangle(name, fid, fileinfo->fi_shortname,
1273 			    SMB_SHORTNAMELEN);
1274 		}
1275 		if (case_conflict)
1276 			name = fileinfo->fi_shortname;
1277 	}
1278 
1279 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1280 
1281 	fileinfo->fi_dosattr = attr.sa_dosattr;
1282 	fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
1283 	fileinfo->fi_size = attr.sa_vattr.va_size;
1284 	fileinfo->fi_alloc_size = attr.sa_allocsz;
1285 	fileinfo->fi_atime = attr.sa_vattr.va_atime;
1286 	fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
1287 	fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
1288 	if (attr.sa_crtime.tv_sec)
1289 		fileinfo->fi_crtime = attr.sa_crtime;
1290 	else
1291 		fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
1292 
1293 	smb_node_release(fnode);
1294 	return (0);
1295 }
1296 
1297 /*
1298  * smb_odir_wildcard_fileinfo
1299  *
1300  * odirent contains a directory entry, obtained from a vop_readdir.
1301  * If a case conflict is identified the filename is mangled and the
1302  * shortname is used as 'name', in place of odirent->od_name.
1303  *
1304  * If the looked up file is a link, we attempt to lookup the link target
1305  * to use its attributes in place of those of the files's.
1306  * If we fail to lookup the target of the link we use the original
1307  * file's attributes.
1308  * Check if the attributes match the search attributes.
1309  *
1310  * Although some file systems can have directories larger than
1311  * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger
1312  * than SMB_MAXDIRSIZE is returned.  It is therefore safe to use the
1313  * offset as the cookie (uint32_t).
1314  *
1315  * Returns: 0 - success (return this entry)
1316  *  any errno - no match, proceed to next entry
1317  */
1318 static int
1319 smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od,
1320     smb_odirent_t *odirent, smb_fileinfo_t *fileinfo)
1321 {
1322 	smb_attr_t	attr;
1323 	char		*name;
1324 	vnode_t		*fvp = NULL;
1325 	int		de_flags;
1326 	int		rc;
1327 	boolean_t	case_conflict;
1328 
1329 	ASSERT(sr);
1330 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
1331 	ASSERT(od);
1332 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
1333 
1334 	ASSERT(MUTEX_HELD(&od->d_mutex));
1335 	bzero(fileinfo, sizeof (smb_fileinfo_t));
1336 
1337 	bzero(&attr, sizeof (attr));
1338 	attr.sa_mask = SMB_AT_ALL;
1339 
1340 	/*
1341 	 * Lookup the vnode and get attributes.  We have the real
1342 	 * (on disk) name here from readdir, so CS lookup.
1343 	 * NB the getattr is done with kcred.
1344 	 */
1345 	rc = smb_vop_lookup(od->d_dnode->vp, odirent->od_name, &fvp,
1346 	    NULL, SMB_CASE_SENSITIVE, &de_flags,
1347 	    od->d_tree->t_snode->vp, &attr, od->d_cred);
1348 
1349 	if (rc != 0)
1350 		return (rc);
1351 
1352 	/*
1353 	 * follow link to get target node & attr
1354 	 */
1355 	if (attr.sa_vattr.va_type == VLNK &&
1356 	    (attr.sa_dosattr & FILE_ATTRIBUTE_REPARSE_POINT) == 0) {
1357 		/*
1358 		 * Follow the symlink (lookup again w/ follow)
1359 		 * Like smb_odir_lookup_link
1360 		 *
1361 		 * Could avoid creating an smb_node_t here,
1362 		 * but symlinks are not so common.
1363 		 */
1364 		smb_node_t *tnode = NULL;
1365 		vnode_t *tvp = NULL;
1366 
1367 		if (smb_odir_lookup_link(sr, od, odirent->od_name, &tnode)) {
1368 
1369 			attr.sa_mask = SMB_AT_ALL;
1370 			rc = smb_fsop_getattr(NULL, zone_kcred(), tnode, &attr);
1371 			if (rc != 0) {
1372 				smb_node_release(tnode);
1373 				VN_RELE(fvp);
1374 				return (rc);
1375 			}
1376 
1377 			/* Use the link target as fvp */
1378 			tvp = tnode->vp;
1379 			VN_HOLD(tvp);
1380 			smb_node_release(tnode);
1381 
1382 			VN_RELE(fvp);
1383 			fvp = tvp;
1384 		}
1385 	}
1386 
1387 	/*
1388 	 * skip system files
1389 	 * Like smb_node_is_system(fnode)
1390 	 */
1391 	if (smb_node_is_vfsroot(od->d_dnode) &&
1392 	    (strcasecmp(odirent->od_name, ".$EXTEND") == 0)) {
1393 		VN_RELE(fvp);
1394 		return (ENOENT);
1395 	}
1396 
1397 	/*
1398 	 * check search attributes
1399 	 */
1400 	if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
1401 		VN_RELE(fvp);
1402 		return (ENOENT);
1403 	}
1404 
1405 	/*
1406 	 * Access Based Enumeration (ABE) checks
1407 	 * Skip this entry if the user can't read it.
1408 	 */
1409 	if ((od->d_flags & SMB_ODIR_FLAG_ABE) != 0) {
1410 		int atype;
1411 		int aflags;
1412 
1413 		if ((od->d_flags & SMB_ODIR_FLAG_ACEACCESS) != 0) {
1414 			atype = V_ACE_MASK;
1415 			aflags = ACE_READ_DATA;
1416 		} else {
1417 			atype = 0;
1418 			aflags = S_IRUSR;
1419 		}
1420 		rc = smb_vop_access(fvp, aflags, atype,
1421 		    od->d_dnode->vp, od->d_cred);
1422 		if (rc != 0) {
1423 			/* skip this entry */
1424 			VN_RELE(fvp);
1425 			return (rc);
1426 		}
1427 	}
1428 
1429 	/*
1430 	 * Deal with names that have conflicts (other names
1431 	 * that match this one if compared case-insensitive)
1432 	 */
1433 	name = odirent->od_name;
1434 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1435 		case_conflict = ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) &&
1436 		    (odirent->od_eflags & ED_CASE_CONFLICT));
1437 		if (case_conflict || smb_needs_mangled(name)) {
1438 			smb_mangle(name, odirent->od_ino,
1439 			    fileinfo->fi_shortname, SMB_SHORTNAMELEN);
1440 		}
1441 		if (case_conflict)
1442 			name = fileinfo->fi_shortname;
1443 	}
1444 
1445 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1446 
1447 	fileinfo->fi_cookie = (uint32_t)od->d_offset;
1448 	fileinfo->fi_dosattr = attr.sa_dosattr;
1449 	fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
1450 	fileinfo->fi_size = attr.sa_vattr.va_size;
1451 	fileinfo->fi_alloc_size = attr.sa_allocsz;
1452 	fileinfo->fi_atime = attr.sa_vattr.va_atime;
1453 	fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
1454 	fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
1455 	if (attr.sa_crtime.tv_sec)
1456 		fileinfo->fi_crtime = attr.sa_crtime;
1457 	else
1458 		fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
1459 
1460 
1461 	VN_RELE(fvp);
1462 
1463 	return (0);
1464 }
1465 
1466 /*
1467  * smb_odir_lookup_link
1468  *
1469  * If the file is a symlink we lookup the object to which the
1470  * symlink refers so that we can return its attributes.
1471  * This can cause a problem if a symlink in a sub-directory
1472  * points to a parent directory (some UNIX GUI's create a symlink
1473  * in $HOME/.desktop that points to the user's home directory).
1474  * Some Windows applications (e.g. virus scanning) loop/hang
1475  * trying to follow this recursive path and there is little
1476  * we can do because the path is constructed on the client.
1477  * smb_dirsymlink_enable allows an end-user to disable
1478  * symlinks to directories. Symlinks to other object types
1479  * should be unaffected.
1480  *
1481  * Returns: B_TRUE  - followed link. tgt_node and tgt_attr set
1482  *          B_FALSE - link not followed
1483  */
1484 static boolean_t
1485 smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od,
1486     char *fname, smb_node_t **tgt_node)
1487 {
1488 	int rc;
1489 	uint32_t flags = SMB_FOLLOW_LINKS | SMB_CASE_SENSITIVE;
1490 
1491 	rc = smb_fsop_lookup(sr, od->d_cred, flags,
1492 	    od->d_tree->t_snode, od->d_dnode, fname, tgt_node);
1493 	if (rc != 0) {
1494 		*tgt_node = NULL;
1495 		return (B_FALSE);
1496 	}
1497 
1498 	if (smb_node_is_dir(*tgt_node) && (!smb_dirsymlink_enable)) {
1499 		smb_node_release(*tgt_node);
1500 		*tgt_node = NULL;
1501 		return (B_FALSE);
1502 	}
1503 
1504 	return (B_TRUE);
1505 }
1506 
1507 /*
1508  * smb_odir_match_name
1509  *
1510  * Check if the directory entry name matches the search pattern:
1511  * - Don't match reserved dos filenames.
1512  * - Check if odirent->od_name matches od->d_pattern.
1513  * - If shortnames are supported, generate the shortname from
1514  *   odirent->od_name and check if it matches od->d_pattern.
1515  */
1516 static boolean_t
1517 smb_odir_match_name(smb_odir_t *od, smb_odirent_t *odirent)
1518 {
1519 	char	*name = odirent->od_name;
1520 	char	shortname[SMB_SHORTNAMELEN];
1521 	ino64_t	ino = odirent->od_ino;
1522 	boolean_t ci = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) != 0;
1523 
1524 	if (smb_is_reserved_dos_name(name))
1525 		return (B_FALSE);
1526 
1527 	if (smb_match(od->d_pattern, name, ci))
1528 		return (B_TRUE);
1529 
1530 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1531 		smb_mangle(name, ino, shortname, SMB_SHORTNAMELEN);
1532 		if (smb_match(od->d_pattern, shortname, ci))
1533 			return (B_TRUE);
1534 	}
1535 
1536 	return (B_FALSE);
1537 }
1538