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