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 ofile 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        rddir_flags = 0;
1063 
1064         ASSERT(MUTEX_HELD(&od->d_mutex));
1065 
1066         bzero(odirent, sizeof (smb_odirent_t));
1067 
1068         if (od->d_flags & SMB_ODIR_FLAG_ABE)
1069                 rddir_flags |= SMB_ABE;
1070         if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
1071                 rddir_flags |= SMB_EDIRENT;
1072 
1073         if (od->d_bufptr != NULL) {
1074                 if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
1075                         reclen = od->d_edp->ed_reclen;
1076                 else
1077                         reclen = od->d_dp->d_reclen;
1078 
1079                 if (reclen == 0) {
1080                         od->d_bufptr = NULL;
1081                 } else {
1082                         od->d_bufptr += reclen;
1083                         if (od->d_bufptr >= od->d_buf + od->d_bufsize)
1084                                 od->d_bufptr = NULL;
1085                 }
1086         }
1087 
1088         if (od->d_bufptr == NULL) {
1089                 if (od->d_eof)
1090                         return (ENOENT);
1091 
1092                 od->d_bufsize = sizeof (od->d_buf);
1093 
1094                 rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset,
1095                     od->d_buf, &od->d_bufsize, &eof, rddir_flags, od->d_cred);
1096 
1097                 if ((rc == 0) && (od->d_bufsize == 0))
1098                         rc = ENOENT;
1099 
1100                 if (rc != 0) {
1101                         od->d_bufptr = NULL;
1102                         od->d_bufsize = 0;
1103                         return (rc);
1104                 }
1105 
1106                 od->d_eof = (eof != 0);
1107                 od->d_bufptr = od->d_buf;
1108         }
1109 
1110         if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
1111                 od->d_offset = od->d_edp->ed_off;
1112         else
1113                 od->d_offset = od->d_dp->d_off;
1114 
1115         if (od->d_offset >= SMB_MAXDIRSIZE) {
1116                 od->d_bufptr = NULL;
1117                 od->d_bufsize = 0;
1118                 od->d_eof = B_TRUE;
1119                 return (ENOENT);
1120         }
1121 
1122         if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) {
1123                 edp = od->d_edp;
1124                 odirent->od_ino = edp->ed_ino;
1125                 odirent->od_eflags = edp->ed_eflags;
1126                 np = edp->ed_name;
1127         } else {
1128                 dp = od->d_dp;
1129                 odirent->od_ino = dp->d_ino;
1130                 odirent->od_eflags = 0;
1131                 np =  dp->d_name;
1132         }
1133 
1134         if ((od->d_flags & SMB_ODIR_FLAG_CATIA) &&
1135             ((od->d_flags & SMB_ODIR_FLAG_XATTR) == 0)) {
1136                 smb_vop_catia_v4tov5(np, odirent->od_name,
1137                     sizeof (odirent->od_name));
1138         } else {
1139                 (void) strlcpy(odirent->od_name, np,
1140                     sizeof (odirent->od_name));
1141         }
1142 
1143         return (0);
1144 }
1145 
1146 /*
1147  * smb_odir_single_fileinfo
1148  *
1149  * Lookup the file identified by od->d_pattern.
1150  *
1151  * If the looked up file is a link, we attempt to lookup the link target
1152  * to use its attributes in place of those of the files's.
1153  * If we fail to lookup the target of the link we use the original
1154  * file's attributes.
1155  * Check if the attributes match the search attributes.
1156  *
1157  * Returns: 0 - success
1158  *     ENOENT - no match
1159  *      errno - error
1160  */
1161 static int
1162 smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od,
1163     smb_fileinfo_t *fileinfo)
1164 {
1165         int             rc;
1166         smb_node_t      *fnode, *tgt_node;
1167         smb_attr_t      attr;
1168         ino64_t         fid;
1169         char            *name;
1170         boolean_t       case_conflict = B_FALSE;
1171         int             lookup_flags, flags = 0;
1172         vnode_t         *vp;
1173 
1174         ASSERT(sr);
1175         ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
1176         ASSERT(od);
1177         ASSERT(od->d_magic == SMB_ODIR_MAGIC);
1178 
1179         ASSERT(MUTEX_HELD(&od->d_mutex));
1180         bzero(fileinfo, sizeof (smb_fileinfo_t));
1181 
1182         rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
1183             od->d_dnode, od->d_pattern, &fnode);
1184         if (rc != 0)
1185                 return (rc);
1186 
1187         /*
1188          * If case sensitive, do a case insensitive smb_vop_lookup to
1189          * check for case conflict
1190          */
1191         if (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) {
1192                 lookup_flags = SMB_IGNORE_CASE;
1193                 if (od->d_flags & SMB_ODIR_FLAG_CATIA)
1194                         lookup_flags |= SMB_CATIA;
1195 
1196                 rc = smb_vop_lookup(od->d_dnode->vp, fnode->od_name, &vp,
1197                     NULL, lookup_flags, &flags, od->d_tree->t_snode->vp,
1198                     NULL, od->d_cred);
1199                 if (rc != 0)
1200                         return (rc);
1201                 VN_RELE(vp);
1202 
1203                 if (flags & ED_CASE_CONFLICT)
1204                         case_conflict = B_TRUE;
1205         }
1206 
1207         bzero(&attr, sizeof (attr));
1208         attr.sa_mask = SMB_AT_ALL;
1209         rc = smb_node_getattr(NULL, fnode, zone_kcred(), NULL, &attr);
1210         if (rc != 0) {
1211                 smb_node_release(fnode);
1212                 return (rc);
1213         }
1214 
1215 
1216         /* follow link to get target node & attr */
1217         if (smb_node_is_symlink(fnode) &&
1218             smb_odir_lookup_link(sr, od, fnode->od_name, &tgt_node)) {
1219                 smb_node_release(fnode);
1220                 fnode = tgt_node;
1221                 attr.sa_mask = SMB_AT_ALL;
1222                 rc = smb_node_getattr(NULL, fnode, zone_kcred(), NULL, &attr);
1223                 if (rc != 0) {
1224                         smb_node_release(fnode);
1225                         return (rc);
1226                 }
1227         }
1228 
1229         /* check search attributes */
1230         if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
1231                 smb_node_release(fnode);
1232                 return (ENOENT);
1233         }
1234 
1235         name = fnode->od_name;
1236         if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1237                 fid = attr.sa_vattr.va_nodeid;
1238                 if (case_conflict || smb_needs_mangled(name)) {
1239                         smb_mangle(name, fid, fileinfo->fi_shortname,
1240                             SMB_SHORTNAMELEN);
1241                 }
1242                 if (case_conflict)
1243                         name = fileinfo->fi_shortname;
1244         }
1245 
1246         (void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1247 
1248         fileinfo->fi_dosattr = attr.sa_dosattr;
1249         fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
1250         fileinfo->fi_size = attr.sa_vattr.va_size;
1251         fileinfo->fi_alloc_size = attr.sa_allocsz;
1252         fileinfo->fi_atime = attr.sa_vattr.va_atime;
1253         fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
1254         fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
1255         if (attr.sa_crtime.tv_sec)
1256                 fileinfo->fi_crtime = attr.sa_crtime;
1257         else
1258                 fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
1259 
1260         smb_node_release(fnode);
1261         return (0);
1262 }
1263 
1264 /*
1265  * smb_odir_wildcard_fileinfo
1266  *
1267  * odirent contains a directory entry, obtained from a vop_readdir.
1268  * If a case conflict is identified the filename is mangled and the
1269  * shortname is used as 'name', in place of odirent->od_name.
1270  *
1271  * If the looked up file is a link, we attempt to lookup the link target
1272  * to use its attributes in place of those of the files's.
1273  * If we fail to lookup the target of the link we use the original
1274  * file's attributes.
1275  * Check if the attributes match the search attributes.
1276  *
1277  * Although some file systems can have directories larger than
1278  * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger
1279  * than SMB_MAXDIRSIZE is returned.  It is therefore safe to use the
1280  * offset as the cookie (uint32_t).
1281  *
1282  * Returns: 0 - success
1283  *     ENOENT - no match, proceed to next entry
1284  *      errno - error
1285  */
1286 static int
1287 smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od,
1288     smb_odirent_t *odirent, smb_fileinfo_t *fileinfo)
1289 {
1290         int             rc;
1291         cred_t          *cr;
1292         smb_node_t      *fnode, *tgt_node;
1293         smb_attr_t      attr;
1294         char            *name;
1295         boolean_t       case_conflict;
1296 
1297         ASSERT(sr);
1298         ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
1299         ASSERT(od);
1300         ASSERT(od->d_magic == SMB_ODIR_MAGIC);
1301 
1302         ASSERT(MUTEX_HELD(&od->d_mutex));
1303         bzero(fileinfo, sizeof (smb_fileinfo_t));
1304 
1305         rc = smb_fsop_lookup(sr, od->d_cred, SMB_CASE_SENSITIVE,
1306             od->d_tree->t_snode, od->d_dnode, odirent->od_name, &fnode);
1307         if (rc != 0)
1308                 return (rc);
1309 
1310         /* follow link to get target node & attr */
1311         if (smb_node_is_symlink(fnode) &&
1312             smb_odir_lookup_link(sr, od, odirent->od_name, &tgt_node)) {
1313                 smb_node_release(fnode);
1314                 fnode = tgt_node;
1315         }
1316 
1317         /* skip system files */
1318         if (smb_node_is_system(fnode)) {
1319                 smb_node_release(fnode);
1320                 return (ENOENT);
1321         }
1322 
1323         /*
1324          * Windows directory listings return not only names, but
1325          * also some attributes.  In Unix, you need some access to
1326          * get those attributes.  Which credential should we use to
1327          * get those?  If we're doing Access Based Enumeration (ABE)
1328          * we want this getattr to fail, which will cause the caller
1329          * to skip this entry.  If we're NOT doing ABE, we normally
1330          * want to show all the directory entries (including their
1331          * attributes) so we want this getattr to succeed!
1332          */
1333         if (smb_tree_has_feature(od->d_tree, SMB_TREE_ABE))
1334                 cr = od->d_cred;
1335         else
1336                 cr = zone_kcred();
1337 
1338         bzero(&attr, sizeof (attr));
1339         attr.sa_mask = SMB_AT_ALL;
1340         rc = smb_node_getattr(NULL, fnode, cr, NULL, &attr);
1341         if (rc != 0) {
1342                 smb_node_release(fnode);
1343                 return (rc);
1344         }
1345 
1346         /* check search attributes */
1347         if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
1348                 smb_node_release(fnode);
1349                 return (ENOENT);
1350         }
1351 
1352         name = odirent->od_name;
1353         if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1354                 case_conflict = ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) &&
1355                     (odirent->od_eflags & ED_CASE_CONFLICT));
1356                 if (case_conflict || smb_needs_mangled(name)) {
1357                         smb_mangle(name, odirent->od_ino,
1358                             fileinfo->fi_shortname, SMB_SHORTNAMELEN);
1359                 }
1360                 if (case_conflict)
1361                         name = fileinfo->fi_shortname;
1362         }
1363 
1364         (void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1365 
1366         fileinfo->fi_cookie = (uint32_t)od->d_offset;
1367         fileinfo->fi_dosattr = attr.sa_dosattr;
1368         fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
1369         fileinfo->fi_size = attr.sa_vattr.va_size;
1370         fileinfo->fi_alloc_size = attr.sa_allocsz;
1371         fileinfo->fi_atime = attr.sa_vattr.va_atime;
1372         fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
1373         fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
1374         if (attr.sa_crtime.tv_sec)
1375                 fileinfo->fi_crtime = attr.sa_crtime;
1376         else
1377                 fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
1378 
1379         smb_node_release(fnode);
1380         return (0);
1381 }
1382 
1383 /*
1384  * smb_odir_lookup_link
1385  *
1386  * If the file is a symlink we lookup the object to which the
1387  * symlink refers so that we can return its attributes.
1388  * This can cause a problem if a symlink in a sub-directory
1389  * points to a parent directory (some UNIX GUI's create a symlink
1390  * in $HOME/.desktop that points to the user's home directory).
1391  * Some Windows applications (e.g. virus scanning) loop/hang
1392  * trying to follow this recursive path and there is little
1393  * we can do because the path is constructed on the client.
1394  * smb_dirsymlink_enable allows an end-user to disable
1395  * symlinks to directories. Symlinks to other object types
1396  * should be unaffected.
1397  *
1398  * Returns: B_TRUE  - followed link. tgt_node and tgt_attr set
1399  *          B_FALSE - link not followed
1400  */
1401 static boolean_t
1402 smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od,
1403     char *fname, smb_node_t **tgt_node)
1404 {
1405         int rc;
1406         uint32_t flags = SMB_FOLLOW_LINKS | SMB_CASE_SENSITIVE;
1407 
1408         rc = smb_fsop_lookup(sr, od->d_cred, flags,
1409             od->d_tree->t_snode, od->d_dnode, fname, tgt_node);
1410         if (rc != 0) {
1411                 *tgt_node = NULL;
1412                 return (B_FALSE);
1413         }
1414 
1415         if (smb_node_is_dir(*tgt_node) && (!smb_dirsymlink_enable)) {
1416                 smb_node_release(*tgt_node);
1417                 *tgt_node = NULL;
1418                 return (B_FALSE);
1419         }
1420 
1421         return (B_TRUE);
1422 }
1423 
1424 /*
1425  * smb_odir_match_name
1426  *
1427  * Check if the directory entry name matches the search pattern:
1428  * - Don't match reserved dos filenames.
1429  * - Check if odirent->od_name matches od->d_pattern.
1430  * - If shortnames are supported, generate the shortname from
1431  *   odirent->od_name and check if it matches od->d_pattern.
1432  */
1433 static boolean_t
1434 smb_odir_match_name(smb_odir_t *od, smb_odirent_t *odirent)
1435 {
1436         char    *name = odirent->od_name;
1437         char    shortname[SMB_SHORTNAMELEN];
1438         ino64_t ino = odirent->od_ino;
1439         boolean_t ci = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) != 0;
1440 
1441         if (smb_is_reserved_dos_name(name))
1442                 return (B_FALSE);
1443 
1444         if (smb_match(od->d_pattern, name, ci))
1445                 return (B_TRUE);
1446 
1447         if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1448                 smb_mangle(name, ino, shortname, SMB_SHORTNAMELEN);
1449                 if (smb_match(od->d_pattern, shortname, ci))
1450                         return (B_TRUE);
1451         }
1452 
1453         return (B_FALSE);
1454 }