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 2020 Nexenta by DDN, Inc. All rights reserved.
24  */
25 
26 #include <sys/sid.h>
27 #include <sys/nbmlock.h>
28 #include <smbsrv/smb_fsops.h>
29 #include <smbsrv/smb_kproto.h>
30 #include <acl/acl_common.h>
31 #include <sys/fcntl.h>
32 #include <sys/filio.h>
33 #include <sys/flock.h>
34 #include <fs/fs_subr.h>
35 
36 extern caller_context_t smb_ct;
37 
38 static int smb_fsop_create_file_with_stream(smb_request_t *, cred_t *,
39     smb_node_t *, char *, char *, int, smb_attr_t *, smb_node_t **);
40 
41 static int smb_fsop_create_file(smb_request_t *, cred_t *, smb_node_t *,
42     char *, int, smb_attr_t *, smb_node_t **);
43 
44 #ifdef	_KERNEL
45 static int smb_fsop_create_with_sd(smb_request_t *, cred_t *, smb_node_t *,
46     char *, smb_attr_t *, smb_node_t **, smb_fssd_t *);
47 static int smb_fsop_sdinherit(smb_request_t *, smb_node_t *, smb_fssd_t *);
48 #endif	/* _KERNEL */
49 
50 /*
51  * The smb_fsop_* functions have knowledge of CIFS semantics.
52  *
53  * The smb_vop_* functions have minimal knowledge of CIFS semantics and
54  * serve as an interface to the VFS layer.
55  *
56  * Hence, smb_request_t and smb_node_t structures should not be passed
57  * from the smb_fsop_* layer to the smb_vop_* layer.
58  *
59  * In general, CIFS service code should only ever call smb_fsop_*
60  * functions directly, and never smb_vop_* functions directly.
61  *
62  * smb_fsop_* functions should call smb_vop_* functions where possible, instead
63  * of their smb_fsop_* counterparts.  However, there are times when
64  * this cannot be avoided.
65  */
66 
67 /*
68  * Note: Stream names cannot be mangled.
69  */
70 
71 /*
72  * smb_fsop_amask_to_omode
73  *
74  * Convert the access mask to the open mode (for use
75  * with the VOP_OPEN call).
76  *
77  * Note that opening a file for attribute only access
78  * will also translate into an FREAD or FWRITE open mode
79  * (i.e., it's not just for data).
80  *
81  * This is needed so that opens are tracked appropriately
82  * for oplock processing.
83  */
84 
85 int
86 smb_fsop_amask_to_omode(uint32_t access)
87 {
88 	int mode = 0;
89 
90 	if (access & (FILE_READ_DATA | FILE_EXECUTE |
91 	    FILE_READ_ATTRIBUTES | FILE_READ_EA))
92 		mode |= FREAD;
93 
94 	if (access & (FILE_WRITE_DATA | FILE_APPEND_DATA |
95 	    FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA))
96 		mode |= FWRITE;
97 
98 	if (access & FILE_APPEND_DATA)
99 		mode |= FAPPEND;
100 
101 	return (mode);
102 }
103 
104 int
105 smb_fsop_open(smb_node_t *node, int mode, cred_t *cred)
106 {
107 	/*
108 	 * Assuming that the same vnode is returned as we had before.
109 	 * (I.e., with certain types of files or file systems, a
110 	 * different vnode might be returned by VOP_OPEN)
111 	 */
112 	return (smb_vop_open(&node->vp, mode, cred));
113 }
114 
115 void
116 smb_fsop_close(smb_node_t *node, int mode, cred_t *cred)
117 {
118 	smb_vop_close(node->vp, mode, cred);
119 }
120 
121 #ifdef	_KERNEL
122 static int
123 smb_fsop_create_with_sd(smb_request_t *sr, cred_t *cr,
124     smb_node_t *dnode, char *name,
125     smb_attr_t *attr, smb_node_t **ret_snode, smb_fssd_t *fs_sd)
126 {
127 	vsecattr_t *vsap;
128 	vsecattr_t vsecattr;
129 	smb_attr_t set_attr;
130 	acl_t *acl, *dacl, *sacl;
131 	vnode_t *vp;
132 	cred_t *kcr = zone_kcred();
133 	int aclbsize = 0;	/* size of acl list in bytes */
134 	int flags = 0;
135 	int rc;
136 	boolean_t is_dir;
137 
138 	ASSERT(fs_sd);
139 	ASSERT(ret_snode != NULL);
140 
141 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
142 		flags = SMB_IGNORE_CASE;
143 	if (SMB_TREE_SUPPORTS_CATIA(sr))
144 		flags |= SMB_CATIA;
145 
146 	ASSERT(cr);
147 
148 	is_dir = ((fs_sd->sd_flags & SMB_FSSD_FLAGS_DIR) != 0);
149 
150 	if (smb_tree_has_feature(sr->tid_tree, SMB_TREE_ACLONCREATE)) {
151 		dacl = fs_sd->sd_zdacl;
152 		sacl = fs_sd->sd_zsacl;
153 		if (dacl != NULL || sacl != NULL) {
154 			if (dacl && sacl) {
155 				acl = smb_fsacl_merge(dacl, sacl);
156 			} else if (dacl) {
157 				acl = dacl;
158 			} else {
159 				acl = sacl;
160 			}
161 
162 			rc = smb_fsacl_to_vsa(acl, &vsecattr, &aclbsize);
163 
164 			if (dacl && sacl)
165 				acl_free(acl);
166 
167 			if (rc != 0)
168 				return (rc);
169 
170 			vsap = &vsecattr;
171 		} else {
172 			vsap = NULL;
173 		}
174 
175 		/* The tree ACEs may prevent a create */
176 		rc = EACCES;
177 		if (is_dir) {
178 			if (SMB_TREE_HAS_ACCESS(sr, ACE_ADD_SUBDIRECTORY) != 0)
179 				rc = smb_vop_mkdir(dnode->vp, name, attr,
180 				    &vp, flags, cr, vsap);
181 		} else {
182 			if (SMB_TREE_HAS_ACCESS(sr, ACE_ADD_FILE) != 0)
183 				rc = smb_vop_create(dnode->vp, name, attr,
184 				    &vp, flags, cr, vsap);
185 		}
186 
187 		if (vsap != NULL)
188 			kmem_free(vsap->vsa_aclentp, aclbsize);
189 
190 		if (rc != 0)
191 			return (rc);
192 
193 		set_attr.sa_mask = 0;
194 
195 		/*
196 		 * Ideally we should be able to specify the owner and owning
197 		 * group at create time along with the ACL. Since we cannot
198 		 * do that right now, kcred is passed to smb_vop_setattr so it
199 		 * doesn't fail due to lack of permission.
200 		 */
201 		if (fs_sd->sd_secinfo & SMB_OWNER_SECINFO) {
202 			set_attr.sa_vattr.va_uid = fs_sd->sd_uid;
203 			set_attr.sa_mask |= SMB_AT_UID;
204 		}
205 
206 		if (fs_sd->sd_secinfo & SMB_GROUP_SECINFO) {
207 			set_attr.sa_vattr.va_gid = fs_sd->sd_gid;
208 			set_attr.sa_mask |= SMB_AT_GID;
209 		}
210 
211 		if (set_attr.sa_mask)
212 			rc = smb_vop_setattr(vp, NULL, &set_attr, 0, kcr);
213 
214 		if (rc == 0) {
215 			*ret_snode = smb_node_lookup(sr, &sr->arg.open, cr, vp,
216 			    name, dnode, NULL);
217 
218 			if (*ret_snode == NULL)
219 				rc = ENOMEM;
220 
221 			VN_RELE(vp);
222 		}
223 	} else {
224 		/*
225 		 * For filesystems that don't support ACL-on-create, try
226 		 * to set the specified SD after create, which could actually
227 		 * fail because of conflicts between inherited security
228 		 * attributes upon creation and the specified SD.
229 		 *
230 		 * Passing kcred to smb_fsop_sdwrite() to overcome this issue.
231 		 */
232 
233 		if (is_dir) {
234 			rc = smb_vop_mkdir(dnode->vp, name, attr, &vp,
235 			    flags, cr, NULL);
236 		} else {
237 			rc = smb_vop_create(dnode->vp, name, attr, &vp,
238 			    flags, cr, NULL);
239 		}
240 
241 		if (rc != 0)
242 			return (rc);
243 
244 		*ret_snode = smb_node_lookup(sr, &sr->arg.open, cr, vp,
245 		    name, dnode, NULL);
246 
247 		if (*ret_snode != NULL) {
248 			if (!smb_tree_has_feature(sr->tid_tree,
249 			    SMB_TREE_NFS_MOUNTED))
250 				rc = smb_fsop_sdwrite(sr, kcr, *ret_snode,
251 				    fs_sd, 1);
252 		} else {
253 			rc = ENOMEM;
254 		}
255 
256 		VN_RELE(vp);
257 	}
258 
259 	if (rc != 0) {
260 		if (is_dir)
261 			(void) smb_vop_rmdir(dnode->vp, name, flags, cr);
262 		else
263 			(void) smb_vop_remove(dnode->vp, name, flags, cr);
264 	}
265 
266 	return (rc);
267 }
268 #endif	/* _KERNEL */
269 
270 /*
271  * smb_fsop_create
272  *
273  * All SMB functions should use this wrapper to ensure that
274  * all the smb_vop_creates are performed with the appropriate credentials.
275  * Please document any direct calls to explain the reason for avoiding
276  * this wrapper.
277  *
278  * *ret_snode is returned with a reference upon success.  No reference is
279  * taken if an error is returned.
280  */
281 int
282 smb_fsop_create(smb_request_t *sr, cred_t *cr, smb_node_t *dnode,
283     char *name, smb_attr_t *attr, smb_node_t **ret_snode)
284 {
285 	int	rc = 0;
286 	int	flags = 0;
287 	char	*fname, *sname;
288 	char	*longname = NULL;
289 
290 	ASSERT(cr);
291 	ASSERT(dnode);
292 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
293 	ASSERT(dnode->n_state != SMB_NODE_STATE_DESTROYING);
294 
295 	ASSERT(ret_snode);
296 	*ret_snode = 0;
297 
298 	ASSERT(name);
299 	if (*name == 0)
300 		return (EINVAL);
301 
302 	ASSERT(sr);
303 	ASSERT(sr->tid_tree);
304 
305 	if (SMB_TREE_CONTAINS_NODE(sr, dnode) == 0)
306 		return (EACCES);
307 
308 	if (SMB_TREE_IS_READONLY(sr))
309 		return (EROFS);
310 
311 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
312 		flags = SMB_IGNORE_CASE;
313 	if (SMB_TREE_SUPPORTS_CATIA(sr))
314 		flags |= SMB_CATIA;
315 	if (SMB_TREE_SUPPORTS_ABE(sr))
316 		flags |= SMB_ABE;
317 
318 	if (smb_is_stream_name(name)) {
319 		fname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
320 		sname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
321 		smb_stream_parse_name(name, fname, sname);
322 
323 		rc = smb_fsop_create_file_with_stream(sr, cr, dnode,
324 		    fname, sname, flags, attr, ret_snode);
325 
326 		kmem_free(fname, MAXNAMELEN);
327 		kmem_free(sname, MAXNAMELEN);
328 		return (rc);
329 	}
330 
331 	/* Not a named stream */
332 
333 	if (SMB_TREE_SUPPORTS_SHORTNAMES(sr) && smb_maybe_mangled(name)) {
334 		longname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
335 		rc = smb_unmangle(dnode, name, longname, MAXNAMELEN, flags);
336 		kmem_free(longname, MAXNAMELEN);
337 
338 		if (rc == 0)
339 			rc = EEXIST;
340 		if (rc != ENOENT)
341 			return (rc);
342 	}
343 
344 	rc = smb_fsop_create_file(sr, cr, dnode, name, flags,
345 	    attr, ret_snode);
346 	return (rc);
347 
348 }
349 
350 
351 /*
352  * smb_fsop_create_file_with_stream
353  *
354  * Create named stream (sname) on file (fname), creating the file if it
355  * doesn't exist.
356  * If we created the file and then creation of the named stream fails,
357  * we delete the file.
358  * Since we use the real file name for the smb_vop_remove we
359  * clear the SMB_IGNORE_CASE flag to ensure a case sensitive
360  * match.
361  *
362  * Note that some stream "types" are "restricted" and only
363  * internal callers (cr == kcred) can create those.
364  */
365 static int
366 smb_fsop_create_file_with_stream(smb_request_t *sr, cred_t *cr,
367     smb_node_t *dnode, char *fname, char *sname, int flags,
368     smb_attr_t *attr, smb_node_t **ret_snode)
369 {
370 	smb_node_t	*fnode;
371 	cred_t		*kcr = zone_kcred();
372 	int		rc = 0;
373 	boolean_t	fcreate = B_FALSE;
374 
375 	ASSERT(ret_snode != NULL);
376 
377 	if (cr != kcr && smb_strname_restricted(sname))
378 		return (EACCES);
379 
380 	/* Look up / create the unnamed stream, fname */
381 	rc = smb_fsop_lookup(sr, cr, flags | SMB_FOLLOW_LINKS,
382 	    sr->tid_tree->t_snode, dnode, fname, &fnode);
383 	if (rc == 0) {
384 		if (smb_fsop_access(sr, sr->user_cr, fnode,
385 		    sr->sr_open.desired_access) != 0) {
386 			smb_node_release(fnode);
387 			rc = EACCES;
388 		}
389 	} else if (rc == ENOENT) {
390 		fcreate = B_TRUE;
391 		rc = smb_fsop_create_file(sr, cr, dnode, fname, flags,
392 		    attr, &fnode);
393 	}
394 	if (rc != 0)
395 		return (rc);
396 
397 	rc = smb_fsop_create_stream(sr, cr, dnode, fnode, sname, flags, attr,
398 	    ret_snode);
399 
400 	if (rc != 0) {
401 		if (fcreate) {
402 			flags &= ~SMB_IGNORE_CASE;
403 			(void) smb_vop_remove(dnode->vp,
404 			    fnode->od_name, flags, cr);
405 		}
406 	}
407 
408 	smb_node_release(fnode);
409 	return (rc);
410 }
411 
412 /*
413  * smb_fsop_create_stream
414  *
415  * Create named stream (sname) on existing file (fnode).
416  *
417  * The second parameter of smb_vop_setattr() is set to
418  * NULL, even though an unnamed stream exists.  This is
419  * because we want to set the UID and GID on the named
420  * stream in this case for consistency with the (unnamed
421  * stream) file (see comments for smb_vop_setattr()).
422  *
423  * Note that some stream "types" are "restricted" and only
424  * internal callers (cr == kcred) can create those.
425  */
426 int
427 smb_fsop_create_stream(smb_request_t *sr, cred_t *cr,
428     smb_node_t *dnode, smb_node_t *fnode, char *sname, int flags,
429     smb_attr_t *attr, smb_node_t **ret_snode)
430 {
431 	smb_attr_t	fattr;
432 	vnode_t		*xattrdvp;
433 	vnode_t		*vp;
434 	cred_t		*kcr = zone_kcred();
435 	int		rc = 0;
436 
437 	ASSERT(ret_snode != NULL);
438 
439 	if (cr != kcr && smb_strname_restricted(sname))
440 		return (EACCES);
441 
442 	bzero(&fattr, sizeof (fattr));
443 	fattr.sa_mask = SMB_AT_UID | SMB_AT_GID;
444 	rc = smb_vop_getattr(fnode->vp, NULL, &fattr, 0, kcr);
445 
446 	if (rc == 0) {
447 		/* create the named stream, sname */
448 		rc = smb_vop_stream_create(fnode->vp, sname,
449 		    attr, &vp, &xattrdvp, flags, cr);
450 	}
451 	if (rc != 0)
452 		return (rc);
453 
454 	attr->sa_vattr.va_uid = fattr.sa_vattr.va_uid;
455 	attr->sa_vattr.va_gid = fattr.sa_vattr.va_gid;
456 	attr->sa_mask = SMB_AT_UID | SMB_AT_GID;
457 
458 	rc = smb_vop_setattr(vp, NULL, attr, 0, kcr);
459 	if (rc != 0) {
460 		VN_RELE(xattrdvp);
461 		VN_RELE(vp);
462 		return (rc);
463 	}
464 
465 	*ret_snode = smb_stream_node_lookup(sr, cr, fnode, xattrdvp,
466 	    vp, sname);
467 
468 	VN_RELE(xattrdvp);
469 	VN_RELE(vp);
470 
471 	if (*ret_snode == NULL)
472 		rc = ENOMEM;
473 
474 	/* notify change to the unnamed stream */
475 	if (rc == 0)
476 		smb_node_notify_change(dnode,
477 		    FILE_ACTION_ADDED_STREAM, fnode->od_name);
478 
479 	return (rc);
480 }
481 
482 /*
483  * smb_fsop_create_file
484  */
485 static int
486 smb_fsop_create_file(smb_request_t *sr, cred_t *cr,
487     smb_node_t *dnode, char *name, int flags,
488     smb_attr_t *attr, smb_node_t **ret_snode)
489 {
490 	smb_arg_open_t	*op = &sr->sr_open;
491 	vnode_t		*vp;
492 	int		rc;
493 
494 	ASSERT(ret_snode != NULL);
495 
496 #ifdef	_KERNEL
497 	smb_fssd_t	fs_sd;
498 	uint32_t	secinfo;
499 	uint32_t	status;
500 
501 	if (op->sd) {
502 		/*
503 		 * SD sent by client in Windows format. Needs to be
504 		 * converted to FS format. Inherit DACL/SACL if they're not
505 		 * specified.
506 		 */
507 		secinfo = smb_sd_get_secinfo(op->sd);
508 
509 		if ((secinfo & SMB_SACL_SECINFO) != 0 &&
510 		    !smb_user_has_security_priv(sr->uid_user, cr))
511 			return (EPERM);
512 
513 		smb_fssd_init(&fs_sd, secinfo, 0);
514 
515 		status = smb_sd_tofs(op->sd, &fs_sd);
516 		if (status == NT_STATUS_SUCCESS) {
517 			rc = smb_fsop_sdinherit(sr, dnode, &fs_sd);
518 			if (rc == 0)
519 				rc = smb_fsop_create_with_sd(sr, cr, dnode,
520 				    name, attr, ret_snode, &fs_sd);
521 
522 		} else {
523 			rc = EINVAL;
524 		}
525 		smb_fssd_term(&fs_sd);
526 	} else if (sr->tid_tree->t_acltype == ACE_T) {
527 		/*
528 		 * No incoming SD and filesystem is ZFS
529 		 * Server applies Windows inheritance rules,
530 		 * see smb_fsop_sdinherit() comments as to why.
531 		 */
532 		smb_fssd_init(&fs_sd, 0, 0);
533 		rc = smb_fsop_sdinherit(sr, dnode, &fs_sd);
534 		if (rc == 0) {
535 			rc = smb_fsop_create_with_sd(sr, cr, dnode,
536 			    name, attr, ret_snode, &fs_sd);
537 		}
538 
539 		smb_fssd_term(&fs_sd);
540 	} else
541 #endif	/* _KERNEL */
542 	{
543 		/*
544 		 * No incoming SD and filesystem is not ZFS
545 		 * let the filesystem handles the inheritance.
546 		 */
547 		rc = smb_vop_create(dnode->vp, name, attr, &vp,
548 		    flags, cr, NULL);
549 
550 		if (rc == 0) {
551 			*ret_snode = smb_node_lookup(sr, op, cr, vp,
552 			    name, dnode, NULL);
553 
554 			if (*ret_snode == NULL)
555 				rc = ENOMEM;
556 
557 			VN_RELE(vp);
558 		}
559 
560 	}
561 
562 	if (rc == 0)
563 		smb_node_notify_change(dnode, FILE_ACTION_ADDED, name);
564 
565 	return (rc);
566 }
567 
568 /*
569  * smb_fsop_mkdir
570  *
571  * All SMB functions should use this wrapper to ensure that
572  * the the calls are performed with the appropriate credentials.
573  * Please document any direct call to explain the reason
574  * for avoiding this wrapper.
575  *
576  * It is assumed that a reference exists on snode coming into this routine.
577  *
578  * *ret_snode is returned with a reference upon success.  No reference is
579  * taken if an error is returned.
580  */
581 int
582 smb_fsop_mkdir(
583     smb_request_t *sr,
584     cred_t *cr,
585     smb_node_t *dnode,
586     char *name,
587     smb_attr_t *attr,
588     smb_node_t **ret_snode)
589 {
590 	struct open_param *op = &sr->arg.open;
591 	char *longname;
592 	vnode_t *vp;
593 	int flags = 0;
594 	int rc;
595 
596 #ifdef	_KERNEL
597 	smb_fssd_t fs_sd;
598 	uint32_t secinfo;
599 	uint32_t status;
600 #endif	/* _KERNEL */
601 
602 	ASSERT(cr);
603 	ASSERT(dnode);
604 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
605 	ASSERT(dnode->n_state != SMB_NODE_STATE_DESTROYING);
606 
607 	ASSERT(ret_snode);
608 	*ret_snode = 0;
609 
610 	ASSERT(name);
611 	if (*name == 0)
612 		return (EINVAL);
613 
614 	ASSERT(sr);
615 	ASSERT(sr->tid_tree);
616 
617 	if (SMB_TREE_CONTAINS_NODE(sr, dnode) == 0)
618 		return (EACCES);
619 
620 	if (SMB_TREE_IS_READONLY(sr))
621 		return (EROFS);
622 	if (SMB_TREE_SUPPORTS_CATIA(sr))
623 		flags |= SMB_CATIA;
624 	if (SMB_TREE_SUPPORTS_ABE(sr))
625 		flags |= SMB_ABE;
626 
627 	if (SMB_TREE_SUPPORTS_SHORTNAMES(sr) && smb_maybe_mangled(name)) {
628 		longname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
629 		rc = smb_unmangle(dnode, name, longname, MAXNAMELEN, flags);
630 		kmem_free(longname, MAXNAMELEN);
631 
632 		/*
633 		 * If the name passed in by the client has an unmangled
634 		 * equivalent that is found in the specified directory,
635 		 * then the mkdir cannot succeed.  Return EEXIST.
636 		 *
637 		 * Only if ENOENT is returned will a mkdir be attempted.
638 		 */
639 
640 		if (rc == 0)
641 			rc = EEXIST;
642 
643 		if (rc != ENOENT)
644 			return (rc);
645 	}
646 
647 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
648 		flags = SMB_IGNORE_CASE;
649 
650 #ifdef	_KERNEL
651 	if (op->sd) {
652 		/*
653 		 * SD sent by client in Windows format. Needs to be
654 		 * converted to FS format. Inherit DACL/SACL if they're not
655 		 * specified.
656 		 */
657 		secinfo = smb_sd_get_secinfo(op->sd);
658 
659 		if ((secinfo & SMB_SACL_SECINFO) != 0 &&
660 		    !smb_user_has_security_priv(sr->uid_user, cr))
661 			return (EPERM);
662 
663 		smb_fssd_init(&fs_sd, secinfo, SMB_FSSD_FLAGS_DIR);
664 
665 		status = smb_sd_tofs(op->sd, &fs_sd);
666 		if (status == NT_STATUS_SUCCESS) {
667 			rc = smb_fsop_sdinherit(sr, dnode, &fs_sd);
668 			if (rc == 0)
669 				rc = smb_fsop_create_with_sd(sr, cr, dnode,
670 				    name, attr, ret_snode, &fs_sd);
671 		}
672 		else
673 			rc = EINVAL;
674 		smb_fssd_term(&fs_sd);
675 	} else if (sr->tid_tree->t_acltype == ACE_T) {
676 		/*
677 		 * No incoming SD and filesystem is ZFS
678 		 * Server applies Windows inheritance rules,
679 		 * see smb_fsop_sdinherit() comments as to why.
680 		 */
681 		smb_fssd_init(&fs_sd, 0, SMB_FSSD_FLAGS_DIR);
682 		rc = smb_fsop_sdinherit(sr, dnode, &fs_sd);
683 		if (rc == 0) {
684 			rc = smb_fsop_create_with_sd(sr, cr, dnode,
685 			    name, attr, ret_snode, &fs_sd);
686 		}
687 
688 		smb_fssd_term(&fs_sd);
689 
690 	} else
691 #endif	/* _KERNEL */
692 	{
693 		rc = smb_vop_mkdir(dnode->vp, name, attr, &vp, flags, cr,
694 		    NULL);
695 
696 		if (rc == 0) {
697 			*ret_snode = smb_node_lookup(sr, op, cr, vp, name,
698 			    dnode, NULL);
699 
700 			if (*ret_snode == NULL)
701 				rc = ENOMEM;
702 
703 			VN_RELE(vp);
704 		}
705 	}
706 
707 	if (rc == 0)
708 		smb_node_notify_change(dnode, FILE_ACTION_ADDED, name);
709 
710 	return (rc);
711 }
712 
713 /*
714  * smb_fsop_remove
715  *
716  * All SMB functions should use this wrapper to ensure that
717  * the the calls are performed with the appropriate credentials.
718  * Please document any direct call to explain the reason
719  * for avoiding this wrapper.
720  *
721  * It is assumed that a reference exists on snode coming into this routine.
722  *
723  * A null smb_request might be passed to this function.
724  *
725  * Note that some stream "types" are "restricted" and only
726  * internal callers (cr == kcred) can remove those.
727  */
728 int
729 smb_fsop_remove(
730     smb_request_t	*sr,
731     cred_t		*cr,
732     smb_node_t		*dnode,
733     char		*name,
734     uint32_t		flags)
735 {
736 	smb_node_t	*fnode;
737 	char		*longname;
738 	char		*fname;
739 	char		*sname;
740 	int		rc;
741 
742 	ASSERT(cr);
743 	/*
744 	 * The state of the node could be SMB_NODE_STATE_DESTROYING if this
745 	 * function is called during the deletion of the node (because of
746 	 * DELETE_ON_CLOSE).
747 	 */
748 	ASSERT(dnode);
749 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
750 
751 	if (SMB_TREE_CONTAINS_NODE(sr, dnode) == 0 ||
752 	    SMB_TREE_HAS_ACCESS(sr, ACE_DELETE) == 0)
753 		return (EACCES);
754 
755 	if (SMB_TREE_IS_READONLY(sr))
756 		return (EROFS);
757 
758 	fname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
759 	sname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
760 
761 	if (dnode->flags & NODE_XATTR_DIR) {
762 		if (cr != zone_kcred() && smb_strname_restricted(name)) {
763 			rc = EACCES;
764 			goto out;
765 		}
766 
767 		fnode = dnode->n_dnode;
768 		rc = smb_vop_stream_remove(fnode->vp, name, flags, cr);
769 
770 		/* notify change to the unnamed stream */
771 		if ((rc == 0) && fnode->n_dnode) {
772 			smb_node_notify_change(fnode->n_dnode,
773 			    FILE_ACTION_REMOVED_STREAM, fnode->od_name);
774 		}
775 	} else if (smb_is_stream_name(name)) {
776 		smb_stream_parse_name(name, fname, sname);
777 
778 		if (cr != zone_kcred() && smb_strname_restricted(sname)) {
779 			rc = EACCES;
780 			goto out;
781 		}
782 
783 		/*
784 		 * Look up the unnamed stream (i.e. fname).
785 		 * Unmangle processing will be done on fname
786 		 * as well as any link target.
787 		 */
788 
789 		rc = smb_fsop_lookup(sr, cr, flags | SMB_FOLLOW_LINKS,
790 		    sr->tid_tree->t_snode, dnode, fname, &fnode);
791 
792 		if (rc != 0) {
793 			goto out;
794 		}
795 
796 		/*
797 		 * XXX
798 		 * Need to find out what permission is required by NTFS
799 		 * to remove a stream.
800 		 */
801 		rc = smb_vop_stream_remove(fnode->vp, sname, flags, cr);
802 
803 		smb_node_release(fnode);
804 
805 		/* notify change to the unnamed stream */
806 		if (rc == 0) {
807 			smb_node_notify_change(dnode,
808 			    FILE_ACTION_REMOVED_STREAM, fname);
809 		}
810 	} else {
811 		rc = smb_vop_remove(dnode->vp, name, flags, cr);
812 
813 		if (rc == ENOENT) {
814 			if (!SMB_TREE_SUPPORTS_SHORTNAMES(sr) ||
815 			    !smb_maybe_mangled(name)) {
816 				goto out;
817 			}
818 			longname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
819 
820 			if (SMB_TREE_SUPPORTS_ABE(sr))
821 				flags |= SMB_ABE;
822 
823 			rc = smb_unmangle(dnode, name, longname, MAXNAMELEN,
824 			    flags);
825 
826 			if (rc == 0) {
827 				/*
828 				 * longname is the real (case-sensitive)
829 				 * on-disk name.
830 				 * We make sure we do a remove on this exact
831 				 * name, as the name was mangled and denotes
832 				 * a unique file.
833 				 */
834 				flags &= ~SMB_IGNORE_CASE;
835 				rc = smb_vop_remove(dnode->vp, longname,
836 				    flags, cr);
837 			}
838 			kmem_free(longname, MAXNAMELEN);
839 		}
840 		if (rc == 0) {
841 			smb_node_notify_change(dnode,
842 			    FILE_ACTION_REMOVED, name);
843 		}
844 	}
845 
846 out:
847 	kmem_free(fname, MAXNAMELEN);
848 	kmem_free(sname, MAXNAMELEN);
849 
850 	return (rc);
851 }
852 
853 /*
854  * smb_fsop_remove_streams
855  *
856  * This function removes a file's streams without removing the
857  * file itself.
858  *
859  * It is assumed that fnode is not a link.
860  */
861 uint32_t
862 smb_fsop_remove_streams(smb_request_t *sr, cred_t *cr, smb_node_t *fnode)
863 {
864 	int rc, flags = 0;
865 	smb_odir_t *od;
866 	smb_odirent_t *odirent;
867 	uint32_t status;
868 	boolean_t eos;
869 
870 	ASSERT(sr);
871 	ASSERT(cr);
872 	ASSERT(fnode);
873 	ASSERT(fnode->n_magic == SMB_NODE_MAGIC);
874 	ASSERT(fnode->n_state != SMB_NODE_STATE_DESTROYING);
875 
876 	if (SMB_TREE_CONTAINS_NODE(sr, fnode) == 0)
877 		return (NT_STATUS_ACCESS_DENIED);
878 
879 	if (SMB_TREE_IS_READONLY(sr))
880 		return (NT_STATUS_ACCESS_DENIED);
881 
882 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
883 		flags = SMB_IGNORE_CASE;
884 
885 	if (SMB_TREE_SUPPORTS_CATIA(sr))
886 		flags |= SMB_CATIA;
887 
888 	status = smb_odir_openat(sr, fnode, &od);
889 	switch (status) {
890 	case 0:
891 		break;
892 	case NT_STATUS_OBJECT_NAME_NOT_FOUND:
893 	case NT_STATUS_NO_SUCH_FILE:
894 	case NT_STATUS_NOT_SUPPORTED:
895 		/* No streams to remove. */
896 		return (0);
897 	default:
898 		return (status);
899 	}
900 
901 	odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
902 	for (;;) {
903 		rc = smb_odir_read(sr, od, odirent, &eos);
904 		if ((rc != 0) || (eos))
905 			break;
906 		(void) smb_vop_remove(od->d_dnode->vp, odirent->od_name,
907 		    flags, cr);
908 	}
909 	kmem_free(odirent, sizeof (smb_odirent_t));
910 	if (eos && rc == ENOENT)
911 		rc = 0;
912 
913 	smb_odir_close(od);
914 	smb_odir_release(od);
915 	if (rc)
916 		status = smb_errno2status(rc);
917 	return (status);
918 }
919 
920 /*
921  * smb_fsop_rmdir
922  *
923  * All SMB functions should use this wrapper to ensure that
924  * the the calls are performed with the appropriate credentials.
925  * Please document any direct call to explain the reason
926  * for avoiding this wrapper.
927  *
928  * It is assumed that a reference exists on snode coming into this routine.
929  */
930 int
931 smb_fsop_rmdir(
932     smb_request_t	*sr,
933     cred_t		*cr,
934     smb_node_t		*dnode,
935     char		*name,
936     uint32_t		flags)
937 {
938 	int		rc;
939 	char		*longname;
940 
941 	ASSERT(cr);
942 	/*
943 	 * The state of the node could be SMB_NODE_STATE_DESTROYING if this
944 	 * function is called during the deletion of the node (because of
945 	 * DELETE_ON_CLOSE).
946 	 */
947 	ASSERT(dnode);
948 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
949 
950 	if (SMB_TREE_CONTAINS_NODE(sr, dnode) == 0 ||
951 	    SMB_TREE_HAS_ACCESS(sr, ACE_DELETE_CHILD) == 0)
952 		return (EACCES);
953 
954 	if (SMB_TREE_IS_READONLY(sr))
955 		return (EROFS);
956 
957 	rc = smb_vop_rmdir(dnode->vp, name, flags, cr);
958 
959 	if (rc == ENOENT) {
960 		if (!SMB_TREE_SUPPORTS_SHORTNAMES(sr) ||
961 		    !smb_maybe_mangled(name)) {
962 			return (rc);
963 		}
964 
965 		longname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
966 
967 		if (SMB_TREE_SUPPORTS_ABE(sr))
968 			flags |= SMB_ABE;
969 		rc = smb_unmangle(dnode, name, longname, MAXNAMELEN, flags);
970 
971 		if (rc == 0) {
972 			/*
973 			 * longname is the real (case-sensitive)
974 			 * on-disk name.
975 			 * We make sure we do a rmdir on this exact
976 			 * name, as the name was mangled and denotes
977 			 * a unique directory.
978 			 */
979 			flags &= ~SMB_IGNORE_CASE;
980 			rc = smb_vop_rmdir(dnode->vp, longname, flags, cr);
981 		}
982 
983 		kmem_free(longname, MAXNAMELEN);
984 	}
985 
986 	if (rc == 0)
987 		smb_node_notify_change(dnode, FILE_ACTION_REMOVED, name);
988 
989 	return (rc);
990 }
991 
992 /*
993  * smb_fsop_getattr
994  *
995  * All SMB functions should use this wrapper to ensure that
996  * the the calls are performed with the appropriate credentials.
997  * Please document any direct call to explain the reason
998  * for avoiding this wrapper.
999  *
1000  * It is assumed that a reference exists on snode coming into this routine.
1001  */
1002 int
1003 smb_fsop_getattr(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
1004     smb_attr_t *attr)
1005 {
1006 	smb_node_t *unnamed_node;
1007 	vnode_t *unnamed_vp = NULL;
1008 	uint32_t status;
1009 	uint32_t access = 0;
1010 	int flags = 0;
1011 	int rc;
1012 
1013 	ASSERT(cr);
1014 	ASSERT(snode);
1015 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
1016 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
1017 
1018 	if (SMB_TREE_CONTAINS_NODE(sr, snode) == 0 ||
1019 	    SMB_TREE_HAS_ACCESS(sr, ACE_READ_ATTRIBUTES) == 0)
1020 		return (EACCES);
1021 
1022 	/* sr could be NULL in some cases */
1023 	if (sr && sr->fid_ofile) {
1024 		/* if uid and/or gid is requested */
1025 		if (attr->sa_mask & (SMB_AT_UID|SMB_AT_GID))
1026 			access |= READ_CONTROL;
1027 
1028 		/* if anything else is also requested */
1029 		if (attr->sa_mask & ~(SMB_AT_UID|SMB_AT_GID))
1030 			access |= FILE_READ_ATTRIBUTES;
1031 
1032 		status = smb_ofile_access(sr->fid_ofile, cr, access);
1033 		if (status != NT_STATUS_SUCCESS)
1034 			return (EACCES);
1035 
1036 		if (smb_tree_has_feature(sr->tid_tree,
1037 		    SMB_TREE_ACEMASKONACCESS))
1038 			flags = ATTR_NOACLCHECK;
1039 	}
1040 
1041 	unnamed_node = SMB_IS_STREAM(snode);
1042 
1043 	if (unnamed_node) {
1044 		ASSERT(unnamed_node->n_magic == SMB_NODE_MAGIC);
1045 		ASSERT(unnamed_node->n_state != SMB_NODE_STATE_DESTROYING);
1046 		unnamed_vp = unnamed_node->vp;
1047 	}
1048 
1049 	rc = smb_vop_getattr(snode->vp, unnamed_vp, attr, flags, cr);
1050 
1051 	if ((rc == 0) && smb_node_is_dfslink(snode)) {
1052 		/* a DFS link should be treated as a directory */
1053 		attr->sa_dosattr |= FILE_ATTRIBUTE_DIRECTORY;
1054 	}
1055 
1056 	return (rc);
1057 }
1058 
1059 /*
1060  * smb_fsop_link
1061  *
1062  * All SMB functions should use this smb_vop_link wrapper to ensure that
1063  * the smb_vop_link is performed with the appropriate credentials.
1064  * Please document any direct call to smb_vop_link to explain the reason
1065  * for avoiding this wrapper.
1066  *
1067  * It is assumed that references exist on from_dnode and to_dnode coming
1068  * into this routine.
1069  */
1070 int
1071 smb_fsop_link(smb_request_t *sr, cred_t *cr, smb_node_t *from_fnode,
1072     smb_node_t *to_dnode, char *to_name)
1073 {
1074 	char	*longname = NULL;
1075 	int	flags = 0;
1076 	int	rc;
1077 
1078 	ASSERT(sr);
1079 	ASSERT(sr->tid_tree);
1080 	ASSERT(cr);
1081 	ASSERT(to_dnode);
1082 	ASSERT(to_dnode->n_magic == SMB_NODE_MAGIC);
1083 	ASSERT(to_dnode->n_state != SMB_NODE_STATE_DESTROYING);
1084 	ASSERT(from_fnode);
1085 	ASSERT(from_fnode->n_magic == SMB_NODE_MAGIC);
1086 	ASSERT(from_fnode->n_state != SMB_NODE_STATE_DESTROYING);
1087 
1088 	if (SMB_TREE_CONTAINS_NODE(sr, from_fnode) == 0)
1089 		return (EACCES);
1090 
1091 	if (SMB_TREE_CONTAINS_NODE(sr, to_dnode) == 0)
1092 		return (EACCES);
1093 
1094 	if (SMB_TREE_IS_READONLY(sr))
1095 		return (EROFS);
1096 
1097 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
1098 		flags = SMB_IGNORE_CASE;
1099 	if (SMB_TREE_SUPPORTS_CATIA(sr))
1100 		flags |= SMB_CATIA;
1101 	if (SMB_TREE_SUPPORTS_ABE(sr))
1102 		flags |= SMB_ABE;
1103 
1104 	if (SMB_TREE_SUPPORTS_SHORTNAMES(sr) && smb_maybe_mangled(to_name)) {
1105 		longname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1106 		rc = smb_unmangle(to_dnode, to_name, longname,
1107 		    MAXNAMELEN, flags);
1108 		kmem_free(longname, MAXNAMELEN);
1109 
1110 		if (rc == 0)
1111 			rc = EEXIST;
1112 		if (rc != ENOENT)
1113 			return (rc);
1114 	}
1115 
1116 	rc = smb_vop_link(to_dnode->vp, from_fnode->vp, to_name, flags, cr);
1117 
1118 	if (rc == 0)
1119 		smb_node_notify_change(to_dnode, FILE_ACTION_ADDED, to_name);
1120 
1121 	return (rc);
1122 }
1123 
1124 /*
1125  * smb_fsop_rename
1126  *
1127  * All SMB functions should use this smb_vop_rename wrapper to ensure that
1128  * the smb_vop_rename is performed with the appropriate credentials.
1129  * Please document any direct call to smb_vop_rename to explain the reason
1130  * for avoiding this wrapper.
1131  *
1132  * It is assumed that references exist on from_dnode and to_dnode coming
1133  * into this routine.
1134  */
1135 int
1136 smb_fsop_rename(
1137     smb_request_t *sr,
1138     cred_t *cr,
1139     smb_node_t *from_dnode,
1140     char *from_name,
1141     smb_node_t *to_dnode,
1142     char *to_name)
1143 {
1144 	smb_node_t *from_snode;
1145 	smb_attr_t from_attr;
1146 	vnode_t *from_vp;
1147 	int flags = 0, ret_flags;
1148 	int rc;
1149 	boolean_t isdir;
1150 
1151 	ASSERT(cr);
1152 	ASSERT(from_dnode);
1153 	ASSERT(from_dnode->n_magic == SMB_NODE_MAGIC);
1154 	ASSERT(from_dnode->n_state != SMB_NODE_STATE_DESTROYING);
1155 
1156 	ASSERT(to_dnode);
1157 	ASSERT(to_dnode->n_magic == SMB_NODE_MAGIC);
1158 	ASSERT(to_dnode->n_state != SMB_NODE_STATE_DESTROYING);
1159 
1160 	if (SMB_TREE_CONTAINS_NODE(sr, from_dnode) == 0)
1161 		return (EACCES);
1162 
1163 	if (SMB_TREE_CONTAINS_NODE(sr, to_dnode) == 0)
1164 		return (EACCES);
1165 
1166 	ASSERT(sr);
1167 	ASSERT(sr->tid_tree);
1168 	if (SMB_TREE_IS_READONLY(sr))
1169 		return (EROFS);
1170 
1171 	/*
1172 	 * Note: There is no need to check SMB_TREE_IS_CASEINSENSITIVE
1173 	 * here.
1174 	 *
1175 	 * A case-sensitive rename is always done in this routine
1176 	 * because we are using the on-disk name from an earlier lookup.
1177 	 * If a mangled name was passed in by the caller (denoting a
1178 	 * deterministic lookup), then the exact file must be renamed
1179 	 * (i.e. SMB_IGNORE_CASE must not be passed to VOP_RENAME, or
1180 	 * else the underlying file system might return a "first-match"
1181 	 * on this on-disk name, possibly resulting in the wrong file).
1182 	 */
1183 
1184 	if (SMB_TREE_SUPPORTS_CATIA(sr))
1185 		flags |= SMB_CATIA;
1186 
1187 	/*
1188 	 * XXX: Lock required through smb_node_release() below?
1189 	 */
1190 
1191 	rc = smb_vop_lookup(from_dnode->vp, from_name, &from_vp, NULL,
1192 	    flags, &ret_flags, NULL, &from_attr, cr);
1193 
1194 	if (rc != 0)
1195 		return (rc);
1196 
1197 	/*
1198 	 * Make sure "from" vp is not a mount point.
1199 	 */
1200 	if (from_vp->v_type == VDIR && vn_ismntpt(from_vp)) {
1201 		VN_RELE(from_vp);
1202 		return (EACCES);
1203 	}
1204 
1205 	if (from_attr.sa_dosattr & FILE_ATTRIBUTE_REPARSE_POINT) {
1206 		VN_RELE(from_vp);
1207 		return (EACCES);
1208 	}
1209 
1210 	isdir = ((from_attr.sa_dosattr & FILE_ATTRIBUTE_DIRECTORY) != 0);
1211 
1212 	if ((isdir && SMB_TREE_HAS_ACCESS(sr,
1213 	    ACE_DELETE_CHILD | ACE_ADD_SUBDIRECTORY) !=
1214 	    (ACE_DELETE_CHILD | ACE_ADD_SUBDIRECTORY)) ||
1215 	    (!isdir && SMB_TREE_HAS_ACCESS(sr, ACE_DELETE | ACE_ADD_FILE) !=
1216 	    (ACE_DELETE | ACE_ADD_FILE))) {
1217 		VN_RELE(from_vp);
1218 		return (EACCES);
1219 	}
1220 
1221 	/*
1222 	 * SMB checks access on open and retains an access granted
1223 	 * mask for use while the file is open.  ACL changes should
1224 	 * not affect access to an open file.
1225 	 *
1226 	 * If the rename is being performed on an ofile:
1227 	 * - Check the ofile's access granted mask to see if the
1228 	 *   rename is permitted - requires DELETE access.
1229 	 * - If the file system does access checking, set the
1230 	 *   ATTR_NOACLCHECK flag to ensure that the file system
1231 	 *   does not check permissions on subsequent calls.
1232 	 */
1233 	if (sr && sr->fid_ofile) {
1234 		rc = smb_ofile_access(sr->fid_ofile, cr, DELETE);
1235 		if (rc != NT_STATUS_SUCCESS) {
1236 			VN_RELE(from_vp);
1237 			return (EACCES);
1238 		}
1239 
1240 		if (smb_tree_has_feature(sr->tid_tree,
1241 		    SMB_TREE_ACEMASKONACCESS))
1242 			flags = ATTR_NOACLCHECK;
1243 	}
1244 
1245 	rc = smb_vop_rename(from_dnode->vp, from_name, to_dnode->vp,
1246 	    to_name, flags, cr);
1247 
1248 	if (rc == 0) {
1249 		from_snode = smb_node_lookup(sr, NULL, cr, from_vp, from_name,
1250 		    from_dnode, NULL);
1251 
1252 		if (from_snode == NULL) {
1253 			rc = ENOMEM;
1254 		} else {
1255 			smb_node_rename(from_dnode, from_snode,
1256 			    to_dnode, to_name);
1257 			smb_node_release(from_snode);
1258 		}
1259 	}
1260 	VN_RELE(from_vp);
1261 
1262 	if (rc == 0) {
1263 		if (from_dnode == to_dnode) {
1264 			smb_node_notify_change(from_dnode,
1265 			    FILE_ACTION_RENAMED_OLD_NAME, from_name);
1266 			smb_node_notify_change(to_dnode,
1267 			    FILE_ACTION_RENAMED_NEW_NAME, to_name);
1268 		} else {
1269 			smb_node_notify_change(from_dnode,
1270 			    FILE_ACTION_REMOVED, from_name);
1271 			smb_node_notify_change(to_dnode,
1272 			    FILE_ACTION_ADDED, to_name);
1273 		}
1274 	}
1275 
1276 	/* XXX: unlock */
1277 
1278 	return (rc);
1279 }
1280 
1281 /*
1282  * smb_fsop_setattr
1283  *
1284  * All SMB functions should use this wrapper to ensure that
1285  * the the calls are performed with the appropriate credentials.
1286  * Please document any direct call to explain the reason
1287  * for avoiding this wrapper.
1288  *
1289  * It is assumed that a reference exists on snode coming into
1290  * this function.
1291  * A null smb_request might be passed to this function.
1292  */
1293 int
1294 smb_fsop_setattr(
1295     smb_request_t	*sr,
1296     cred_t		*cr,
1297     smb_node_t		*snode,
1298     smb_attr_t		*set_attr)
1299 {
1300 	smb_node_t *unnamed_node;
1301 	vnode_t *unnamed_vp = NULL;
1302 	uint32_t status;
1303 	uint32_t access;
1304 	int rc = 0;
1305 	int flags = 0;
1306 	uint_t sa_mask;
1307 
1308 	ASSERT(cr);
1309 	ASSERT(snode);
1310 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
1311 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
1312 
1313 	if (SMB_TREE_CONTAINS_NODE(sr, snode) == 0)
1314 		return (EACCES);
1315 
1316 	if (SMB_TREE_IS_READONLY(sr))
1317 		return (EROFS);
1318 
1319 	if (SMB_TREE_HAS_ACCESS(sr,
1320 	    ACE_WRITE_ATTRIBUTES | ACE_WRITE_NAMED_ATTRS) == 0)
1321 		return (EACCES);
1322 
1323 	/*
1324 	 * SMB checks access on open and retains an access granted
1325 	 * mask for use while the file is open.  ACL changes should
1326 	 * not affect access to an open file.
1327 	 *
1328 	 * If the setattr is being performed on an ofile:
1329 	 * - Check the ofile's access granted mask to see if the
1330 	 *   setattr is permitted.
1331 	 *   UID, GID - require WRITE_OWNER
1332 	 *   SIZE, ALLOCSZ - require FILE_WRITE_DATA
1333 	 *   all other attributes require FILE_WRITE_ATTRIBUTES
1334 	 *
1335 	 * - If the file system does access checking, set the
1336 	 *   ATTR_NOACLCHECK flag to ensure that the file system
1337 	 *   does not check permissions on subsequent calls.
1338 	 */
1339 	if (sr && sr->fid_ofile) {
1340 		sa_mask = set_attr->sa_mask;
1341 		access = 0;
1342 
1343 		if (sa_mask & (SMB_AT_SIZE | SMB_AT_ALLOCSZ)) {
1344 			access |= FILE_WRITE_DATA;
1345 			sa_mask &= ~(SMB_AT_SIZE | SMB_AT_ALLOCSZ);
1346 		}
1347 
1348 		if (sa_mask & (SMB_AT_UID|SMB_AT_GID)) {
1349 			access |= WRITE_OWNER;
1350 			sa_mask &= ~(SMB_AT_UID|SMB_AT_GID);
1351 		}
1352 
1353 		if (sa_mask)
1354 			access |= FILE_WRITE_ATTRIBUTES;
1355 
1356 		status = smb_ofile_access(sr->fid_ofile, cr, access);
1357 		if (status != NT_STATUS_SUCCESS)
1358 			return (EACCES);
1359 
1360 		if (smb_tree_has_feature(sr->tid_tree,
1361 		    SMB_TREE_ACEMASKONACCESS))
1362 			flags = ATTR_NOACLCHECK;
1363 	}
1364 
1365 	unnamed_node = SMB_IS_STREAM(snode);
1366 
1367 	if (unnamed_node) {
1368 		ASSERT(unnamed_node->n_magic == SMB_NODE_MAGIC);
1369 		ASSERT(unnamed_node->n_state != SMB_NODE_STATE_DESTROYING);
1370 		unnamed_vp = unnamed_node->vp;
1371 	}
1372 
1373 	rc = smb_vop_setattr(snode->vp, unnamed_vp, set_attr, flags, cr);
1374 	return (rc);
1375 }
1376 
1377 /*
1378  * Support for SMB2 setinfo FileValidDataLengthInformation.
1379  * Free (zero out) data in the range off, off+len
1380  */
1381 int
1382 smb_fsop_freesp(
1383     smb_request_t	*sr,
1384     cred_t		*cr,
1385     smb_ofile_t		*ofile,
1386     off64_t		off,
1387     off64_t		len)
1388 {
1389 	flock64_t flk;
1390 	smb_node_t *node = ofile->f_node;
1391 	uint32_t status;
1392 	uint32_t access = FILE_WRITE_DATA;
1393 	int rc;
1394 
1395 	ASSERT(cr);
1396 	ASSERT(node);
1397 	ASSERT(node->n_magic == SMB_NODE_MAGIC);
1398 	ASSERT(node->n_state != SMB_NODE_STATE_DESTROYING);
1399 
1400 	if (SMB_TREE_CONTAINS_NODE(sr, node) == 0)
1401 		return (EACCES);
1402 
1403 	if (SMB_TREE_IS_READONLY(sr))
1404 		return (EROFS);
1405 
1406 	if (SMB_TREE_HAS_ACCESS(sr, access) == 0)
1407 		return (EACCES);
1408 
1409 	/*
1410 	 * SMB checks access on open and retains an access granted
1411 	 * mask for use while the file is open.  ACL changes should
1412 	 * not affect access to an open file.
1413 	 *
1414 	 * If the setattr is being performed on an ofile:
1415 	 * - Check the ofile's access granted mask to see if this
1416 	 *   modification should be permitted (FILE_WRITE_DATA)
1417 	 */
1418 	status = smb_ofile_access(sr->fid_ofile, cr, access);
1419 	if (status != NT_STATUS_SUCCESS)
1420 		return (EACCES);
1421 
1422 	bzero(&flk, sizeof (flk));
1423 	flk.l_start = off;
1424 	flk.l_len = len;
1425 
1426 	rc = smb_vop_space(node->vp, F_FREESP, &flk, FWRITE, 0LL, cr);
1427 	return (rc);
1428 }
1429 
1430 /*
1431  * smb_fsop_read
1432  *
1433  * All SMB functions should use this wrapper to ensure that
1434  * the the calls are performed with the appropriate credentials.
1435  * Please document any direct call to explain the reason
1436  * for avoiding this wrapper.
1437  *
1438  * It is assumed that a reference exists on snode coming into this routine.
1439  * Note that ofile may be different from sr->fid_ofile, or may be NULL.
1440  */
1441 int
1442 smb_fsop_read(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
1443     smb_ofile_t *ofile, uio_t *uio, int ioflag)
1444 {
1445 	caller_context_t ct;
1446 	cred_t *kcr = zone_kcred();
1447 	uint32_t amask;
1448 	int svmand;
1449 	int rc;
1450 
1451 	ASSERT(cr);
1452 	ASSERT(snode);
1453 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
1454 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
1455 
1456 	ASSERT(sr);
1457 
1458 	if (ofile != NULL) {
1459 		/*
1460 		 * Check tree access.  Not SMB_TREE_HAS_ACCESS
1461 		 * because we need to use ofile->f_tree
1462 		 */
1463 		if ((ofile->f_tree->t_access & ACE_READ_DATA) == 0)
1464 			return (EACCES);
1465 
1466 		/*
1467 		 * Check ofile access.  Use in-line smb_ofile_access
1468 		 * so we can check both amask bits at the same time.
1469 		 * If any bit in amask is granted, allow this read.
1470 		 */
1471 		amask = FILE_READ_DATA;
1472 		if (sr->smb_flg2 & SMB_FLAGS2_READ_IF_EXECUTE)
1473 			amask |= FILE_EXECUTE;
1474 		if (cr != kcr && (ofile->f_granted_access & amask) == 0)
1475 			return (EACCES);
1476 	}
1477 
1478 	/*
1479 	 * Streams permission are checked against the unnamed stream,
1480 	 * but in FS level they have their own permissions. To avoid
1481 	 * rejection by FS due to lack of permission on the actual
1482 	 * extended attr kcred is passed for streams.
1483 	 */
1484 	if (SMB_IS_STREAM(snode))
1485 		cr = kcr;
1486 
1487 	smb_node_start_crit(snode, RW_READER);
1488 	rc = nbl_svmand(snode->vp, kcr, &svmand);
1489 	if (rc) {
1490 		smb_node_end_crit(snode);
1491 		return (rc);
1492 	}
1493 
1494 	/*
1495 	 * Note: SMB allows a zero-byte read, which should not
1496 	 * conflict with any locks.  However nbl_lock_conflict
1497 	 * takes a zero-byte length as lock to EOF, so we must
1498 	 * special case that here.
1499 	 */
1500 	if (uio->uio_resid > 0) {
1501 		ct = smb_ct;
1502 		if (ofile != NULL)
1503 			ct.cc_pid = ofile->f_uniqid;
1504 		rc = nbl_lock_conflict(snode->vp, NBL_READ, uio->uio_loffset,
1505 		    uio->uio_resid, svmand, &ct);
1506 		if (rc != 0) {
1507 			smb_node_end_crit(snode);
1508 			return (ERANGE);
1509 		}
1510 	}
1511 
1512 	rc = smb_vop_read(snode->vp, uio, ioflag, cr);
1513 	smb_node_end_crit(snode);
1514 
1515 	return (rc);
1516 }
1517 
1518 /*
1519  * smb_fsop_write
1520  *
1521  * It is assumed that a reference exists on snode coming into this routine.
1522  * Note that ofile may be different from sr->fid_ofile, or may be NULL.
1523  */
1524 int
1525 smb_fsop_write(
1526     smb_request_t *sr,
1527     cred_t *cr,
1528     smb_node_t *snode,
1529     smb_ofile_t *ofile,
1530     uio_t *uio,
1531     uint32_t *lcount,
1532     int ioflag)
1533 {
1534 	caller_context_t ct;
1535 	smb_attr_t attr;
1536 	cred_t *kcr = zone_kcred();
1537 	smb_node_t *u_node;
1538 	vnode_t *u_vp = NULL;
1539 	vnode_t *vp;
1540 	uint32_t amask;
1541 	int svmand;
1542 	int rc;
1543 
1544 	ASSERT(cr);
1545 	ASSERT(snode);
1546 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
1547 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
1548 
1549 	ASSERT(sr);
1550 	vp = snode->vp;
1551 
1552 	if (ofile != NULL) {
1553 		amask = FILE_WRITE_DATA | FILE_APPEND_DATA;
1554 
1555 		/* Check tree access. */
1556 		if ((ofile->f_tree->t_access & amask) == 0)
1557 			return (EROFS);
1558 
1559 		/*
1560 		 * Check ofile access.  Use in-line smb_ofile_access
1561 		 * so we can check both amask bits at the same time.
1562 		 * If any bit in amask is granted, allow this write.
1563 		 */
1564 		if (cr != kcr && (ofile->f_granted_access & amask) == 0)
1565 			return (EACCES);
1566 	}
1567 
1568 	/*
1569 	 * Streams permission are checked against the unnamed stream,
1570 	 * but in FS level they have their own permissions. To avoid
1571 	 * rejection by FS due to lack of permission on the actual
1572 	 * extended attr kcred is passed for streams.
1573 	 */
1574 	u_node = SMB_IS_STREAM(snode);
1575 	if (u_node != NULL) {
1576 		ASSERT(u_node->n_magic == SMB_NODE_MAGIC);
1577 		ASSERT(u_node->n_state != SMB_NODE_STATE_DESTROYING);
1578 		u_vp = u_node->vp;
1579 		cr = kcr;
1580 	}
1581 
1582 	smb_node_start_crit(snode, RW_READER);
1583 	rc = nbl_svmand(vp, kcr, &svmand);
1584 	if (rc) {
1585 		smb_node_end_crit(snode);
1586 		return (rc);
1587 	}
1588 
1589 	/*
1590 	 * Note: SMB allows a zero-byte write, which should not
1591 	 * conflict with any locks.  However nbl_lock_conflict
1592 	 * takes a zero-byte length as lock to EOF, so we must
1593 	 * special case that here.
1594 	 */
1595 	if (uio->uio_resid > 0) {
1596 		ct = smb_ct;
1597 		if (ofile != NULL)
1598 			ct.cc_pid = ofile->f_uniqid;
1599 		rc = nbl_lock_conflict(vp, NBL_WRITE, uio->uio_loffset,
1600 		    uio->uio_resid, svmand, &ct);
1601 		if (rc != 0) {
1602 			smb_node_end_crit(snode);
1603 			return (ERANGE);
1604 		}
1605 	}
1606 
1607 	rc = smb_vop_write(vp, uio, ioflag, lcount, cr);
1608 
1609 	/*
1610 	 * Once the mtime has been set via this ofile, the
1611 	 * automatic mtime changes from writes via this ofile
1612 	 * should cease, preserving the mtime that was set.
1613 	 * See: [MS-FSA] 2.1.5.14 and smb_node_setattr.
1614 	 *
1615 	 * The VFS interface does not offer a way to ask it to
1616 	 * skip the mtime updates, so we simulate the desired
1617 	 * behavior by re-setting the mtime after writes on a
1618 	 * handle where the mtime has been set.
1619 	 */
1620 	if (ofile != NULL &&
1621 	    (ofile->f_pending_attr.sa_mask & SMB_AT_MTIME) != 0) {
1622 		bcopy(&ofile->f_pending_attr, &attr, sizeof (attr));
1623 		attr.sa_mask = SMB_AT_MTIME;
1624 		(void) smb_vop_setattr(vp, u_vp, &attr, 0, kcr);
1625 	}
1626 
1627 	smb_node_end_crit(snode);
1628 
1629 	return (rc);
1630 }
1631 
1632 /*
1633  * Find the next allocated range starting at or after
1634  * the offset (*datap), returning the start/end of
1635  * that range in (*datap, *holep)
1636  */
1637 int
1638 smb_fsop_next_alloc_range(
1639     cred_t *cr,
1640     smb_node_t *node,
1641     off64_t *datap,
1642     off64_t *holep)
1643 {
1644 	int err;
1645 
1646 	err = smb_vop_ioctl(node->vp, _FIO_SEEK_DATA, datap, cr);
1647 	if (err != 0)
1648 		return (err);
1649 
1650 	*holep = *datap;
1651 	err = smb_vop_ioctl(node->vp, _FIO_SEEK_HOLE, holep, cr);
1652 
1653 	return (err);
1654 }
1655 
1656 /*
1657  * smb_fsop_statfs
1658  *
1659  * This is a wrapper function used for stat operations.
1660  */
1661 int
1662 smb_fsop_statfs(
1663     cred_t *cr,
1664     smb_node_t *snode,
1665     struct statvfs64 *statp)
1666 {
1667 	ASSERT(cr);
1668 	ASSERT(snode);
1669 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
1670 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
1671 
1672 	return (smb_vop_statfs(snode->vp, statp, cr));
1673 }
1674 
1675 /*
1676  * smb_fsop_access
1677  *
1678  * Named streams do not have separate permissions from the associated
1679  * unnamed stream.  Thus, if node is a named stream, the permissions
1680  * check will be performed on the associated unnamed stream.
1681  *
1682  * However, our named streams do have their own quarantine attribute,
1683  * separate from that on the unnamed stream. If READ or EXECUTE
1684  * access has been requested on a named stream, an additional access
1685  * check is performed on the named stream in case it has been
1686  * quarantined.  kcred is used to avoid issues with the permissions
1687  * set on the extended attribute file representing the named stream.
1688  *
1689  * Note that some stream "types" are "restricted" and only
1690  * internal callers (cr == kcred) can access those.
1691  */
1692 int
1693 smb_fsop_access(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
1694     uint32_t faccess)
1695 {
1696 	int access = 0;
1697 	int error;
1698 	vnode_t *dir_vp;
1699 	boolean_t acl_check = B_TRUE;
1700 	smb_node_t *unnamed_node;
1701 
1702 	ASSERT(sr);
1703 	ASSERT(cr);
1704 	ASSERT(snode);
1705 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
1706 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
1707 
1708 	if (SMB_TREE_IS_READONLY(sr)) {
1709 		if (faccess & (FILE_WRITE_DATA|FILE_APPEND_DATA|
1710 		    FILE_WRITE_EA|FILE_DELETE_CHILD|FILE_WRITE_ATTRIBUTES|
1711 		    DELETE|WRITE_DAC|WRITE_OWNER)) {
1712 			return (NT_STATUS_ACCESS_DENIED);
1713 		}
1714 	}
1715 
1716 	if (smb_node_is_reparse(snode) && (faccess & DELETE))
1717 		return (NT_STATUS_ACCESS_DENIED);
1718 
1719 	unnamed_node = SMB_IS_STREAM(snode);
1720 	if (unnamed_node) {
1721 		cred_t *kcr = zone_kcred();
1722 
1723 		ASSERT(unnamed_node->n_magic == SMB_NODE_MAGIC);
1724 		ASSERT(unnamed_node->n_state != SMB_NODE_STATE_DESTROYING);
1725 
1726 		if (cr != kcr && smb_strname_restricted(snode->od_name))
1727 			return (NT_STATUS_ACCESS_DENIED);
1728 
1729 		/*
1730 		 * Perform VREAD access check on the named stream in case it
1731 		 * is quarantined. kcred is passed to smb_vop_access so it
1732 		 * doesn't fail due to lack of permission.
1733 		 */
1734 		if (faccess & (FILE_READ_DATA | FILE_EXECUTE)) {
1735 			error = smb_vop_access(snode->vp, VREAD,
1736 			    0, NULL, kcr);
1737 			if (error)
1738 				return (NT_STATUS_ACCESS_DENIED);
1739 		}
1740 
1741 		/*
1742 		 * Streams authorization should be performed against the
1743 		 * unnamed stream.
1744 		 */
1745 		snode = unnamed_node;
1746 	}
1747 
1748 	if (faccess & ACCESS_SYSTEM_SECURITY) {
1749 		/*
1750 		 * This permission is required for reading/writing SACL and
1751 		 * it's not part of DACL. It's only granted via proper
1752 		 * privileges.
1753 		 */
1754 		if (!smb_user_has_security_priv(sr->uid_user, cr))
1755 			return (NT_STATUS_PRIVILEGE_NOT_HELD);
1756 
1757 		faccess &= ~ACCESS_SYSTEM_SECURITY;
1758 	}
1759 
1760 	/* Links don't have ACL */
1761 	if ((!smb_tree_has_feature(sr->tid_tree, SMB_TREE_ACEMASKONACCESS)) ||
1762 	    smb_node_is_symlink(snode))
1763 		acl_check = B_FALSE;
1764 
1765 	/* Deny access based on the share access mask */
1766 
1767 	if ((faccess & ~sr->tid_tree->t_access) != 0)
1768 		return (NT_STATUS_ACCESS_DENIED);
1769 
1770 	if (acl_check) {
1771 		dir_vp = (snode->n_dnode) ? snode->n_dnode->vp : NULL;
1772 		error = smb_vop_access(snode->vp, faccess, V_ACE_MASK, dir_vp,
1773 		    cr);
1774 	} else {
1775 		/*
1776 		 * FS doesn't understand 32-bit mask, need to map
1777 		 */
1778 		if (faccess & (FILE_WRITE_DATA | FILE_APPEND_DATA))
1779 			access |= VWRITE;
1780 
1781 		if (faccess & FILE_READ_DATA)
1782 			access |= VREAD;
1783 
1784 		if (faccess & FILE_EXECUTE)
1785 			access |= VEXEC;
1786 
1787 		error = smb_vop_access(snode->vp, access, 0, NULL, cr);
1788 	}
1789 
1790 	return ((error) ? NT_STATUS_ACCESS_DENIED : NT_STATUS_SUCCESS);
1791 }
1792 
1793 /*
1794  * smb_fsop_lookup_name()
1795  *
1796  * Lookup both the file and stream specified in 'name'.
1797  * If name indicates that the file is a stream file, perform
1798  * stream specific lookup, otherwise call smb_fsop_lookup.
1799  *
1800  * On success, returns the found node in *ret_snode. This will be either a named
1801  * or unnamed stream node, depending on the name specified.
1802  *
1803  * Return an error if the looked-up file is in outside the tree.
1804  * (Required when invoked from open path.)
1805  *
1806  * Case sensitivity flags (SMB_IGNORE_CASE, SMB_CASE_SENSITIVE):
1807  * if SMB_CASE_SENSITIVE is set, the SMB_IGNORE_CASE flag will NOT be set
1808  * based on the tree's case sensitivity. However, if the SMB_IGNORE_CASE
1809  * flag is set in the flags value passed as a parameter, a case insensitive
1810  * lookup WILL be done (regardless of whether SMB_CASE_SENSITIVE is set
1811  * or not).
1812  */
1813 
1814 int
1815 smb_fsop_lookup_name(
1816     smb_request_t *sr,
1817     cred_t	*cr,
1818     int		flags,
1819     smb_node_t	*root_node,
1820     smb_node_t	*dnode,
1821     char	*name,
1822     smb_node_t	**ret_snode)
1823 {
1824 	char *sname = NULL;
1825 	int rc;
1826 	smb_node_t *tmp_node;
1827 
1828 	ASSERT(ret_snode != NULL);
1829 
1830 	rc = smb_fsop_lookup_file(sr, cr, flags, root_node, dnode, name,
1831 	    &sname, ret_snode);
1832 
1833 	if (rc != 0 || sname == NULL)
1834 		return (rc);
1835 
1836 	tmp_node = *ret_snode;
1837 	rc = smb_fsop_lookup_stream(sr, cr, flags, root_node, tmp_node, sname,
1838 	    ret_snode);
1839 	kmem_free(sname, MAXNAMELEN);
1840 	smb_node_release(tmp_node);
1841 
1842 	return (rc);
1843 }
1844 
1845 /*
1846  * smb_fsop_lookup_file()
1847  *
1848  * Look up of the file portion of 'name'. If a Stream is specified,
1849  * return the stream name in 'sname', which this allocates.
1850  * The caller must free 'sname'.
1851  *
1852  * Return an error if the looked-up file is outside the tree.
1853  * (Required when invoked from open path.)
1854  *
1855  * Case sensitivity flags (SMB_IGNORE_CASE, SMB_CASE_SENSITIVE):
1856  * if SMB_CASE_SENSITIVE is set, the SMB_IGNORE_CASE flag will NOT be set
1857  * based on the tree's case sensitivity. However, if the SMB_IGNORE_CASE
1858  * flag is set in the flags value passed as a parameter, a case insensitive
1859  * lookup WILL be done (regardless of whether SMB_CASE_SENSITIVE is set
1860  * or not).
1861  */
1862 
1863 int
1864 smb_fsop_lookup_file(
1865     smb_request_t *sr,
1866     cred_t	*cr,
1867     int		flags,
1868     smb_node_t	*root_node,
1869     smb_node_t	*dnode,
1870     char	*name,
1871     char	**sname,
1872     smb_node_t	**ret_snode)
1873 {
1874 	char		*fname;
1875 	int		rc;
1876 
1877 	ASSERT(cr);
1878 	ASSERT(dnode);
1879 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
1880 	ASSERT(dnode->n_state != SMB_NODE_STATE_DESTROYING);
1881 	ASSERT(ret_snode != NULL);
1882 
1883 	/*
1884 	 * The following check is required for streams processing, below
1885 	 */
1886 
1887 	if (!(flags & SMB_CASE_SENSITIVE)) {
1888 		if (SMB_TREE_IS_CASEINSENSITIVE(sr))
1889 			flags |= SMB_IGNORE_CASE;
1890 	}
1891 
1892 	*sname = NULL;
1893 	if (smb_is_stream_name(name)) {
1894 		*sname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1895 		fname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1896 		smb_stream_parse_name(name, fname, *sname);
1897 
1898 		/*
1899 		 * Look up the unnamed stream (i.e. fname).
1900 		 * Unmangle processing will be done on fname
1901 		 * as well as any link target.
1902 		 */
1903 		rc = smb_fsop_lookup(sr, cr, flags, root_node, dnode,
1904 		    fname, ret_snode);
1905 		kmem_free(fname, MAXNAMELEN);
1906 	} else {
1907 		rc = smb_fsop_lookup(sr, cr, flags, root_node, dnode, name,
1908 		    ret_snode);
1909 	}
1910 
1911 	if (rc == 0) {
1912 		ASSERT(ret_snode);
1913 		if (SMB_TREE_CONTAINS_NODE(sr, *ret_snode) == 0) {
1914 			smb_node_release(*ret_snode);
1915 			*ret_snode = NULL;
1916 			rc = EACCES;
1917 		}
1918 	}
1919 
1920 	if (rc != 0 && *sname != NULL) {
1921 		kmem_free(*sname, MAXNAMELEN);
1922 		*sname = NULL;
1923 	}
1924 	return (rc);
1925 }
1926 
1927 /*
1928  * smb_fsop_lookup_stream
1929  *
1930  * The file exists, see if the stream exists.
1931  */
1932 int
1933 smb_fsop_lookup_stream(
1934     smb_request_t *sr,
1935     cred_t *cr,
1936     int flags,
1937     smb_node_t *root_node,
1938     smb_node_t *fnode,
1939     char *sname,
1940     smb_node_t **ret_snode)
1941 {
1942 	char		*od_name;
1943 	vnode_t		*xattrdirvp;
1944 	vnode_t		*vp;
1945 	int rc;
1946 
1947 	/*
1948 	 * The following check is required for streams processing, below
1949 	 */
1950 
1951 	if (!(flags & SMB_CASE_SENSITIVE)) {
1952 		if (SMB_TREE_IS_CASEINSENSITIVE(sr))
1953 			flags |= SMB_IGNORE_CASE;
1954 	}
1955 
1956 	od_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1957 
1958 	/*
1959 	 * od_name is the on-disk name of the stream, except
1960 	 * without the prepended stream prefix (SMB_STREAM_PREFIX)
1961 	 */
1962 
1963 	rc = smb_vop_stream_lookup(fnode->vp, sname, &vp, od_name,
1964 	    &xattrdirvp, flags, root_node->vp, cr);
1965 
1966 	if (rc != 0) {
1967 		kmem_free(od_name, MAXNAMELEN);
1968 		return (rc);
1969 	}
1970 
1971 	*ret_snode = smb_stream_node_lookup(sr, cr, fnode, xattrdirvp,
1972 	    vp, od_name);
1973 
1974 	kmem_free(od_name, MAXNAMELEN);
1975 	VN_RELE(xattrdirvp);
1976 	VN_RELE(vp);
1977 
1978 	if (*ret_snode == NULL)
1979 		return (ENOMEM);
1980 
1981 	return (rc);
1982 }
1983 
1984 /*
1985  * smb_fsop_lookup
1986  *
1987  * All SMB functions should use this smb_vop_lookup wrapper to ensure that
1988  * the smb_vop_lookup is performed with the appropriate credentials and using
1989  * case insensitive compares. Please document any direct call to smb_vop_lookup
1990  * to explain the reason for avoiding this wrapper.
1991  *
1992  * It is assumed that a reference exists on dnode coming into this routine
1993  * (and that it is safe from deallocation).
1994  *
1995  * Same with the root_node.
1996  *
1997  * *ret_snode is returned with a reference upon success.  No reference is
1998  * taken if an error is returned.
1999  *
2000  * Note: The returned ret_snode may be in a child mount.  This is ok for
2001  * readdir.
2002  *
2003  * Other smb_fsop_* routines will call SMB_TREE_CONTAINS_NODE() to prevent
2004  * operations on files not in the parent mount.
2005  *
2006  * Case sensitivity flags (SMB_IGNORE_CASE, SMB_CASE_SENSITIVE):
2007  * if SMB_CASE_SENSITIVE is set, the SMB_IGNORE_CASE flag will NOT be set
2008  * based on the tree's case sensitivity. However, if the SMB_IGNORE_CASE
2009  * flag is set in the flags value passed as a parameter, a case insensitive
2010  * lookup WILL be done (regardless of whether SMB_CASE_SENSITIVE is set
2011  * or not).
2012  */
2013 int
2014 smb_fsop_lookup(
2015     smb_request_t *sr,
2016     cred_t	*cr,
2017     int		flags,
2018     smb_node_t	*root_node,
2019     smb_node_t	*dnode,
2020     char	*name,
2021     smb_node_t	**ret_snode)
2022 {
2023 	smb_node_t *lnk_target_node;
2024 	smb_node_t *lnk_dnode;
2025 	char *longname;
2026 	char *od_name;
2027 	vnode_t *vp;
2028 	int rc;
2029 	int ret_flags;
2030 	smb_attr_t attr;
2031 
2032 	ASSERT(cr);
2033 	ASSERT(dnode);
2034 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
2035 	ASSERT(dnode->n_state != SMB_NODE_STATE_DESTROYING);
2036 
2037 	if (name == NULL)
2038 		return (EINVAL);
2039 
2040 	if (SMB_TREE_CONTAINS_NODE(sr, dnode) == 0)
2041 		return (EACCES);
2042 
2043 	if (!(flags & SMB_CASE_SENSITIVE)) {
2044 		if (SMB_TREE_IS_CASEINSENSITIVE(sr))
2045 			flags |= SMB_IGNORE_CASE;
2046 	}
2047 	if (SMB_TREE_SUPPORTS_CATIA(sr))
2048 		flags |= SMB_CATIA;
2049 	if (SMB_TREE_SUPPORTS_ABE(sr))
2050 		flags |= SMB_ABE;
2051 
2052 	/*
2053 	 * Can have "" or "." when opening named streams on a directory.
2054 	 */
2055 	if (name[0] == '\0' || (name[0] == '.' && name[1] == '\0')) {
2056 		smb_node_ref(dnode);
2057 		*ret_snode = dnode;
2058 		return (0);
2059 	}
2060 
2061 	od_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2062 
2063 	rc = smb_vop_lookup(dnode->vp, name, &vp, od_name, flags,
2064 	    &ret_flags, root_node ? root_node->vp : NULL, &attr, cr);
2065 
2066 	if (rc != 0) {
2067 		if (!SMB_TREE_SUPPORTS_SHORTNAMES(sr) ||
2068 		    !smb_maybe_mangled(name)) {
2069 			kmem_free(od_name, MAXNAMELEN);
2070 			return (rc);
2071 		}
2072 
2073 		longname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2074 		rc = smb_unmangle(dnode, name, longname, MAXNAMELEN, flags);
2075 		if (rc != 0) {
2076 			kmem_free(od_name, MAXNAMELEN);
2077 			kmem_free(longname, MAXNAMELEN);
2078 			return (rc);
2079 		}
2080 
2081 		/*
2082 		 * longname is the real (case-sensitive)
2083 		 * on-disk name.
2084 		 * We make sure we do a lookup on this exact
2085 		 * name, as the name was mangled and denotes
2086 		 * a unique file.
2087 		 */
2088 
2089 		if (flags & SMB_IGNORE_CASE)
2090 			flags &= ~SMB_IGNORE_CASE;
2091 
2092 		rc = smb_vop_lookup(dnode->vp, longname, &vp, od_name,
2093 		    flags, &ret_flags, root_node ? root_node->vp : NULL, &attr,
2094 		    cr);
2095 
2096 		kmem_free(longname, MAXNAMELEN);
2097 
2098 		if (rc != 0) {
2099 			kmem_free(od_name, MAXNAMELEN);
2100 			return (rc);
2101 		}
2102 	}
2103 
2104 	if ((flags & SMB_FOLLOW_LINKS) && (vp->v_type == VLNK) &&
2105 	    ((attr.sa_dosattr & FILE_ATTRIBUTE_REPARSE_POINT) == 0)) {
2106 		rc = smb_pathname(sr, od_name, FOLLOW, root_node, dnode,
2107 		    &lnk_dnode, &lnk_target_node, cr, NULL);
2108 
2109 		if (rc != 0) {
2110 			/*
2111 			 * The link is assumed to be for the last component
2112 			 * of a path.  Hence any ENOTDIR error will be returned
2113 			 * as ENOENT.
2114 			 */
2115 			if (rc == ENOTDIR)
2116 				rc = ENOENT;
2117 
2118 			VN_RELE(vp);
2119 			kmem_free(od_name, MAXNAMELEN);
2120 			return (rc);
2121 		}
2122 
2123 		/*
2124 		 * Release the original VLNK vnode
2125 		 */
2126 
2127 		VN_RELE(vp);
2128 		vp = lnk_target_node->vp;
2129 
2130 		rc = smb_vop_traverse_check(&vp);
2131 
2132 		if (rc != 0) {
2133 			smb_node_release(lnk_dnode);
2134 			smb_node_release(lnk_target_node);
2135 			kmem_free(od_name, MAXNAMELEN);
2136 			return (rc);
2137 		}
2138 
2139 		/*
2140 		 * smb_vop_traverse_check() may have returned a different vnode
2141 		 */
2142 
2143 		if (lnk_target_node->vp == vp) {
2144 			*ret_snode = lnk_target_node;
2145 		} else {
2146 			*ret_snode = smb_node_lookup(sr, NULL, cr, vp,
2147 			    lnk_target_node->od_name, lnk_dnode, NULL);
2148 			VN_RELE(vp);
2149 
2150 			if (*ret_snode == NULL)
2151 				rc = ENOMEM;
2152 			smb_node_release(lnk_target_node);
2153 		}
2154 
2155 		smb_node_release(lnk_dnode);
2156 
2157 	} else {
2158 
2159 		rc = smb_vop_traverse_check(&vp);
2160 		if (rc) {
2161 			VN_RELE(vp);
2162 			kmem_free(od_name, MAXNAMELEN);
2163 			return (rc);
2164 		}
2165 
2166 		*ret_snode = smb_node_lookup(sr, NULL, cr, vp, od_name,
2167 		    dnode, NULL);
2168 		VN_RELE(vp);
2169 
2170 		if (*ret_snode == NULL)
2171 			rc = ENOMEM;
2172 	}
2173 
2174 	kmem_free(od_name, MAXNAMELEN);
2175 	return (rc);
2176 }
2177 
2178 int /*ARGSUSED*/
2179 smb_fsop_commit(smb_request_t *sr, cred_t *cr, smb_node_t *snode)
2180 {
2181 	ASSERT(cr);
2182 	ASSERT(snode);
2183 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
2184 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
2185 
2186 	ASSERT(sr);
2187 	ASSERT(sr->tid_tree);
2188 	if (SMB_TREE_IS_READONLY(sr))
2189 		return (EROFS);
2190 
2191 	return (smb_vop_commit(snode->vp, cr));
2192 }
2193 
2194 /*
2195  * smb_fsop_aclread
2196  *
2197  * Retrieve filesystem ACL. Depends on requested ACLs in
2198  * fs_sd->sd_secinfo, it'll set DACL and SACL pointers in
2199  * fs_sd. Note that requesting a DACL/SACL doesn't mean that
2200  * the corresponding field in fs_sd should be non-NULL upon
2201  * return, since the target ACL might not contain that type of
2202  * entries.
2203  *
2204  * Returned ACL is always in ACE_T (aka ZFS) format.
2205  * If successful the allocated memory for the ACL should be freed
2206  * using smb_fsacl_free() or smb_fssd_term()
2207  */
2208 int
2209 smb_fsop_aclread(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
2210     smb_fssd_t *fs_sd)
2211 {
2212 	int error = 0;
2213 	int flags = 0;
2214 	int access = 0;
2215 	acl_t *acl;
2216 
2217 	ASSERT(cr);
2218 
2219 	/* Can't query security on named streams */
2220 	if (SMB_IS_STREAM(snode) != NULL)
2221 		return (EINVAL);
2222 
2223 	if (SMB_TREE_HAS_ACCESS(sr, ACE_READ_ACL) == 0)
2224 		return (EACCES);
2225 
2226 	if (sr->fid_ofile) {
2227 		if (fs_sd->sd_secinfo & SMB_DACL_SECINFO)
2228 			access = READ_CONTROL;
2229 
2230 		if (fs_sd->sd_secinfo & SMB_SACL_SECINFO)
2231 			access |= ACCESS_SYSTEM_SECURITY;
2232 
2233 		error = smb_ofile_access(sr->fid_ofile, cr, access);
2234 		if (error != NT_STATUS_SUCCESS) {
2235 			return (EACCES);
2236 		}
2237 	}
2238 
2239 
2240 	if (smb_tree_has_feature(sr->tid_tree, SMB_TREE_ACEMASKONACCESS))
2241 		flags = ATTR_NOACLCHECK;
2242 
2243 	error = smb_vop_acl_read(snode->vp, &acl, flags,
2244 	    sr->tid_tree->t_acltype, cr);
2245 	if (error != 0) {
2246 		return (error);
2247 	}
2248 
2249 	error = acl_translate(acl, _ACL_ACE_ENABLED,
2250 	    smb_node_is_dir(snode), fs_sd->sd_uid, fs_sd->sd_gid);
2251 
2252 	if (error == 0) {
2253 		smb_fsacl_split(acl, &fs_sd->sd_zdacl, &fs_sd->sd_zsacl,
2254 		    fs_sd->sd_secinfo);
2255 	}
2256 
2257 	acl_free(acl);
2258 	return (error);
2259 }
2260 
2261 /*
2262  * smb_fsop_aclwrite
2263  *
2264  * Stores the filesystem ACL provided in fs_sd->sd_acl.
2265  */
2266 int
2267 smb_fsop_aclwrite(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
2268     smb_fssd_t *fs_sd)
2269 {
2270 	int target_flavor;
2271 	int error = 0;
2272 	int flags = 0;
2273 	int access = 0;
2274 	acl_t *acl, *dacl, *sacl;
2275 
2276 	ASSERT(cr);
2277 
2278 	ASSERT(sr);
2279 	ASSERT(sr->tid_tree);
2280 	if (SMB_TREE_IS_READONLY(sr))
2281 		return (EROFS);
2282 
2283 	/* Can't set security on named streams */
2284 	if (SMB_IS_STREAM(snode) != NULL)
2285 		return (EINVAL);
2286 
2287 	if (SMB_TREE_HAS_ACCESS(sr, ACE_WRITE_ACL) == 0)
2288 		return (EACCES);
2289 
2290 	if (sr->fid_ofile) {
2291 		if (fs_sd->sd_secinfo & SMB_DACL_SECINFO)
2292 			access = WRITE_DAC;
2293 
2294 		if (fs_sd->sd_secinfo & SMB_SACL_SECINFO)
2295 			access |= ACCESS_SYSTEM_SECURITY;
2296 
2297 		error = smb_ofile_access(sr->fid_ofile, cr, access);
2298 		if (error != NT_STATUS_SUCCESS)
2299 			return (EACCES);
2300 	}
2301 
2302 	switch (sr->tid_tree->t_acltype) {
2303 	case ACLENT_T:
2304 		target_flavor = _ACL_ACLENT_ENABLED;
2305 		break;
2306 
2307 	case ACE_T:
2308 		target_flavor = _ACL_ACE_ENABLED;
2309 		break;
2310 	default:
2311 		return (EINVAL);
2312 	}
2313 
2314 	dacl = fs_sd->sd_zdacl;
2315 	sacl = fs_sd->sd_zsacl;
2316 
2317 	ASSERT(dacl || sacl);
2318 	if ((dacl == NULL) && (sacl == NULL))
2319 		return (EINVAL);
2320 
2321 	if (dacl && sacl)
2322 		acl = smb_fsacl_merge(dacl, sacl);
2323 	else if (dacl)
2324 		acl = dacl;
2325 	else
2326 		acl = sacl;
2327 
2328 	error = acl_translate(acl, target_flavor, smb_node_is_dir(snode),
2329 	    fs_sd->sd_uid, fs_sd->sd_gid);
2330 	if (error == 0) {
2331 		if (smb_tree_has_feature(sr->tid_tree,
2332 		    SMB_TREE_ACEMASKONACCESS))
2333 			flags = ATTR_NOACLCHECK;
2334 
2335 		error = smb_vop_acl_write(snode->vp, acl, flags, cr);
2336 	}
2337 
2338 	if (dacl && sacl)
2339 		acl_free(acl);
2340 
2341 	return (error);
2342 }
2343 
2344 acl_type_t
2345 smb_fsop_acltype(smb_node_t *snode)
2346 {
2347 	return (smb_vop_acl_type(snode->vp));
2348 }
2349 
2350 /*
2351  * smb_fsop_sdread
2352  *
2353  * Read the requested security descriptor items from filesystem.
2354  * The items are specified in fs_sd->sd_secinfo.
2355  */
2356 int
2357 smb_fsop_sdread(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
2358     smb_fssd_t *fs_sd)
2359 {
2360 	int error = 0;
2361 	int getowner = 0;
2362 	cred_t *ga_cred;
2363 	smb_attr_t attr;
2364 
2365 	ASSERT(cr);
2366 	ASSERT(fs_sd);
2367 
2368 	/* Can't query security on named streams */
2369 	if (SMB_IS_STREAM(snode) != NULL)
2370 		return (EINVAL);
2371 
2372 	/*
2373 	 * File's uid/gid is fetched in two cases:
2374 	 *
2375 	 * 1. it's explicitly requested
2376 	 *
2377 	 * 2. target ACL is ACE_T (ZFS ACL). They're needed for
2378 	 *    owner@/group@ entries. In this case kcred should be used
2379 	 *    because uid/gid are fetched on behalf of smb server.
2380 	 */
2381 	if (fs_sd->sd_secinfo & (SMB_OWNER_SECINFO | SMB_GROUP_SECINFO)) {
2382 		getowner = 1;
2383 		ga_cred = cr;
2384 	} else if (sr->tid_tree->t_acltype == ACE_T) {
2385 		getowner = 1;
2386 		ga_cred = zone_kcred();
2387 	}
2388 
2389 	if (getowner) {
2390 		/*
2391 		 * Windows require READ_CONTROL to read owner/group SID since
2392 		 * they're part of Security Descriptor.
2393 		 * ZFS only requires read_attribute. Need to have a explicit
2394 		 * access check here.
2395 		 */
2396 		if (sr->fid_ofile == NULL) {
2397 			error = smb_fsop_access(sr, ga_cred, snode,
2398 			    READ_CONTROL);
2399 			if (error)
2400 				return (EACCES);
2401 		}
2402 
2403 		attr.sa_mask = SMB_AT_UID | SMB_AT_GID;
2404 		error = smb_fsop_getattr(sr, ga_cred, snode, &attr);
2405 		if (error == 0) {
2406 			fs_sd->sd_uid = attr.sa_vattr.va_uid;
2407 			fs_sd->sd_gid = attr.sa_vattr.va_gid;
2408 		} else {
2409 			return (error);
2410 		}
2411 	}
2412 
2413 	if (fs_sd->sd_secinfo & SMB_ACL_SECINFO) {
2414 		error = smb_fsop_aclread(sr, cr, snode, fs_sd);
2415 	}
2416 
2417 	return (error);
2418 }
2419 
2420 /*
2421  * smb_fsop_sdmerge
2422  *
2423  * From SMB point of view DACL and SACL are two separate list
2424  * which can be manipulated independently without one affecting
2425  * the other, but entries for both DACL and SACL will end up
2426  * in the same ACL if target filesystem supports ACE_T ACLs.
2427  *
2428  * So, if either DACL or SACL is present in the client set request
2429  * the entries corresponding to the non-present ACL shouldn't
2430  * be touched in the FS ACL.
2431  *
2432  * fs_sd parameter contains DACL and SACL specified by SMB
2433  * client to be set on a file/directory. The client could
2434  * specify both or one of these ACLs (if none is specified
2435  * we don't get this far). When both DACL and SACL are given
2436  * by client the existing ACL should be overwritten. If only
2437  * one of them is specified the entries corresponding to the other
2438  * ACL should not be touched. For example, if only DACL
2439  * is specified in input fs_sd, the function reads audit entries
2440  * of the existing ACL of the file and point fs_sd->sd_zsdacl
2441  * pointer to the fetched SACL, this way when smb_fsop_sdwrite()
2442  * function is called the passed fs_sd would point to the specified
2443  * DACL by client and fetched SACL from filesystem, so the file
2444  * will end up with correct ACL.
2445  */
2446 static int
2447 smb_fsop_sdmerge(smb_request_t *sr, smb_node_t *snode, smb_fssd_t *fs_sd)
2448 {
2449 	smb_fssd_t cur_sd;
2450 	cred_t *kcr = zone_kcred();
2451 	int error = 0;
2452 
2453 	if (sr->tid_tree->t_acltype != ACE_T)
2454 		/* Don't bother if target FS doesn't support ACE_T */
2455 		return (0);
2456 
2457 	if ((fs_sd->sd_secinfo & SMB_ACL_SECINFO) != SMB_ACL_SECINFO) {
2458 		if (fs_sd->sd_secinfo & SMB_DACL_SECINFO) {
2459 			/*
2460 			 * Don't overwrite existing audit entries
2461 			 */
2462 			smb_fssd_init(&cur_sd, SMB_SACL_SECINFO,
2463 			    fs_sd->sd_flags);
2464 
2465 			error = smb_fsop_sdread(sr, kcr, snode, &cur_sd);
2466 			if (error == 0) {
2467 				ASSERT(fs_sd->sd_zsacl == NULL);
2468 				fs_sd->sd_zsacl = cur_sd.sd_zsacl;
2469 				if (fs_sd->sd_zsacl && fs_sd->sd_zdacl)
2470 					fs_sd->sd_zsacl->acl_flags =
2471 					    fs_sd->sd_zdacl->acl_flags;
2472 			}
2473 		} else {
2474 			/*
2475 			 * Don't overwrite existing access entries
2476 			 */
2477 			smb_fssd_init(&cur_sd, SMB_DACL_SECINFO,
2478 			    fs_sd->sd_flags);
2479 
2480 			error = smb_fsop_sdread(sr, kcr, snode, &cur_sd);
2481 			if (error == 0) {
2482 				ASSERT(fs_sd->sd_zdacl == NULL);
2483 				fs_sd->sd_zdacl = cur_sd.sd_zdacl;
2484 				if (fs_sd->sd_zdacl && fs_sd->sd_zsacl)
2485 					fs_sd->sd_zdacl->acl_flags =
2486 					    fs_sd->sd_zsacl->acl_flags;
2487 			}
2488 		}
2489 
2490 		if (error)
2491 			smb_fssd_term(&cur_sd);
2492 	}
2493 
2494 	return (error);
2495 }
2496 
2497 /*
2498  * smb_fsop_sdwrite
2499  *
2500  * Stores the given uid, gid and acl in filesystem.
2501  * Provided items in fs_sd are specified by fs_sd->sd_secinfo.
2502  *
2503  * A SMB security descriptor could contain owner, primary group,
2504  * DACL and SACL. Setting an SD should be atomic but here it has to
2505  * be done via two separate FS operations: VOP_SETATTR and
2506  * VOP_SETSECATTR. Therefore, this function has to simulate the
2507  * atomicity as well as it can.
2508  *
2509  * Get the current uid, gid before setting the new uid/gid
2510  * so if smb_fsop_aclwrite fails they can be restored. root cred is
2511  * used to get currend uid/gid since this operation is performed on
2512  * behalf of the server not the user.
2513  *
2514  * If setting uid/gid fails with EPERM it means that and invalid
2515  * owner has been specified. Callers should translate this to
2516  * STATUS_INVALID_OWNER which is not the normal mapping for EPERM
2517  * in upper layers, so EPERM is mapped to EBADE.
2518  *
2519  * If 'overwrite' is non-zero, then the existing ACL is ignored.
2520  */
2521 int
2522 smb_fsop_sdwrite(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
2523     smb_fssd_t *fs_sd, int overwrite)
2524 {
2525 	smb_attr_t set_attr;
2526 	smb_attr_t orig_attr;
2527 	cred_t *kcr = zone_kcred();
2528 	int error = 0;
2529 	int access = 0;
2530 
2531 	ASSERT(cr);
2532 	ASSERT(fs_sd);
2533 
2534 	ASSERT(sr);
2535 	ASSERT(sr->tid_tree);
2536 	if (SMB_TREE_IS_READONLY(sr))
2537 		return (EROFS);
2538 
2539 	/* Can't set security on named streams */
2540 	if (SMB_IS_STREAM(snode) != NULL)
2541 		return (EINVAL);
2542 
2543 	bzero(&set_attr, sizeof (smb_attr_t));
2544 
2545 	if (fs_sd->sd_secinfo & SMB_OWNER_SECINFO) {
2546 		set_attr.sa_vattr.va_uid = fs_sd->sd_uid;
2547 		set_attr.sa_mask |= SMB_AT_UID;
2548 		access |= WRITE_OWNER;
2549 	}
2550 
2551 	if (fs_sd->sd_secinfo & SMB_GROUP_SECINFO) {
2552 		set_attr.sa_vattr.va_gid = fs_sd->sd_gid;
2553 		set_attr.sa_mask |= SMB_AT_GID;
2554 		access |= WRITE_OWNER;
2555 	}
2556 
2557 	if (fs_sd->sd_secinfo & SMB_DACL_SECINFO)
2558 		access |= WRITE_DAC;
2559 
2560 	if (fs_sd->sd_secinfo & SMB_SACL_SECINFO)
2561 		access |= ACCESS_SYSTEM_SECURITY;
2562 
2563 	if (sr->fid_ofile)
2564 		error = smb_ofile_access(sr->fid_ofile, cr, access);
2565 	else
2566 		error = smb_fsop_access(sr, cr, snode, access);
2567 
2568 	if (error)
2569 		return (EACCES);
2570 
2571 	if (set_attr.sa_mask) {
2572 		orig_attr.sa_mask = SMB_AT_UID | SMB_AT_GID;
2573 		error = smb_fsop_getattr(sr, kcr, snode, &orig_attr);
2574 		if (error == 0) {
2575 			error = smb_fsop_setattr(sr, cr, snode, &set_attr);
2576 			if (error == EPERM)
2577 				error = EBADE;
2578 		}
2579 
2580 		if (error)
2581 			return (error);
2582 	}
2583 
2584 	if (fs_sd->sd_secinfo & SMB_ACL_SECINFO) {
2585 		if (overwrite == 0)
2586 			error = smb_fsop_sdmerge(sr, snode, fs_sd);
2587 
2588 		if (error == 0)
2589 			error = smb_fsop_aclwrite(sr, cr, snode, fs_sd);
2590 
2591 		if (error != 0) {
2592 			/*
2593 			 * Revert uid/gid changes if required.
2594 			 */
2595 			if (set_attr.sa_mask) {
2596 				orig_attr.sa_mask = set_attr.sa_mask;
2597 				(void) smb_fsop_setattr(sr, kcr, snode,
2598 				    &orig_attr);
2599 			}
2600 		}
2601 	}
2602 
2603 	return (error);
2604 }
2605 
2606 #ifdef	_KERNEL
2607 /*
2608  * smb_fsop_sdinherit
2609  *
2610  * Inherit the security descriptor from the parent container.
2611  * This function is called after FS has created the file/folder
2612  * so if this doesn't do anything it means FS inheritance is
2613  * in place.
2614  *
2615  * Do inheritance for ZFS internally.
2616  *
2617  * If we want to let ZFS does the inheritance the
2618  * following setting should be true:
2619  *
2620  *  - aclinherit = passthrough
2621  *  - aclmode = passthrough
2622  *  - smbd umask = 0777
2623  *
2624  * This will result in right effective permissions but
2625  * ZFS will always add 6 ACEs for owner, owning group
2626  * and others to be POSIX compliant. This is not what
2627  * Windows clients/users expect, so we decided that CIFS
2628  * implements Windows rules and overwrite whatever ZFS
2629  * comes up with. This way we also don't have to care
2630  * about ZFS aclinherit and aclmode settings.
2631  */
2632 static int
2633 smb_fsop_sdinherit(smb_request_t *sr, smb_node_t *dnode, smb_fssd_t *fs_sd)
2634 {
2635 	acl_t *dacl = NULL;
2636 	acl_t *sacl = NULL;
2637 	int is_dir;
2638 	int error;
2639 	uint32_t secinfo;
2640 	smb_fssd_t pfs_sd;
2641 
2642 	ASSERT(fs_sd);
2643 
2644 	secinfo = fs_sd->sd_secinfo;
2645 
2646 	/* Anything to do? */
2647 	if ((secinfo & SMB_ACL_SECINFO) == SMB_ACL_SECINFO)
2648 		return (0);
2649 
2650 	/*
2651 	 * No forced inheritance for non-ZFS filesystems.
2652 	 */
2653 	if (sr->tid_tree->t_acltype != ACE_T)
2654 		return (0);
2655 
2656 	smb_fssd_init(&pfs_sd, SMB_ACL_SECINFO, fs_sd->sd_flags);
2657 
2658 	/* Fetch parent directory's ACL */
2659 	error = smb_fsop_sdread(sr, zone_kcred(), dnode, &pfs_sd);
2660 	if (error) {
2661 		return (error);
2662 	}
2663 
2664 	is_dir = (fs_sd->sd_flags & SMB_FSSD_FLAGS_DIR);
2665 	if ((secinfo & SMB_DACL_SECINFO) == 0) {
2666 		dacl = smb_fsacl_inherit(pfs_sd.sd_zdacl, is_dir,
2667 		    SMB_DACL_SECINFO, sr->user_cr);
2668 		fs_sd->sd_zdacl = dacl;
2669 	}
2670 
2671 	if ((secinfo & SMB_SACL_SECINFO) == 0) {
2672 		sacl = smb_fsacl_inherit(pfs_sd.sd_zsacl, is_dir,
2673 		    SMB_SACL_SECINFO, sr->user_cr);
2674 		fs_sd->sd_zsacl = sacl;
2675 	}
2676 
2677 	smb_fsacl_free(pfs_sd.sd_zdacl);
2678 	smb_fsacl_free(pfs_sd.sd_zsacl);
2679 	return (0);
2680 }
2681 #endif	/* _KERNEL */
2682 
2683 /*
2684  * smb_fsop_eaccess
2685  *
2686  * Returns the effective permission of the given credential for the
2687  * specified object.
2688  *
2689  * This is just a workaround. We need VFS/FS support for this.
2690  */
2691 void
2692 smb_fsop_eaccess(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
2693     uint32_t *eaccess)
2694 {
2695 	int access = 0;
2696 	vnode_t *dir_vp;
2697 	smb_node_t *unnamed_node;
2698 
2699 	ASSERT(cr);
2700 	ASSERT(snode);
2701 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
2702 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
2703 
2704 	unnamed_node = SMB_IS_STREAM(snode);
2705 	if (unnamed_node) {
2706 		ASSERT(unnamed_node->n_magic == SMB_NODE_MAGIC);
2707 		ASSERT(unnamed_node->n_state != SMB_NODE_STATE_DESTROYING);
2708 		/*
2709 		 * Streams authorization should be performed against the
2710 		 * unnamed stream.
2711 		 */
2712 		snode = unnamed_node;
2713 	}
2714 
2715 	if (smb_tree_has_feature(sr->tid_tree, SMB_TREE_ACEMASKONACCESS)) {
2716 		dir_vp = (snode->n_dnode) ? snode->n_dnode->vp : NULL;
2717 		smb_vop_eaccess(snode->vp, (int *)eaccess, V_ACE_MASK, dir_vp,
2718 		    cr);
2719 		return;
2720 	}
2721 
2722 	/*
2723 	 * FS doesn't understand 32-bit mask
2724 	 */
2725 	smb_vop_eaccess(snode->vp, &access, 0, NULL, cr);
2726 	access &= sr->tid_tree->t_access;
2727 
2728 	*eaccess = READ_CONTROL | FILE_READ_EA | FILE_READ_ATTRIBUTES;
2729 
2730 	if (access & VREAD)
2731 		*eaccess |= FILE_READ_DATA;
2732 
2733 	if (access & VEXEC)
2734 		*eaccess |= FILE_EXECUTE;
2735 
2736 	if (access & VWRITE)
2737 		*eaccess |= FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES |
2738 		    FILE_WRITE_EA | FILE_APPEND_DATA | FILE_DELETE_CHILD;
2739 
2740 	if (access & (VREAD | VWRITE))
2741 		*eaccess |= SYNCHRONIZE;
2742 
2743 #ifdef	_FAKE_KERNEL
2744 	/* Should be: if (we are the owner)... */
2745 	if (access & VWRITE)
2746 		*eaccess |= DELETE | WRITE_DAC | WRITE_OWNER;
2747 #endif
2748 }
2749 
2750 /*
2751  * smb_fsop_shrlock
2752  *
2753  * For the current open request, check file sharing rules
2754  * against existing opens.
2755  *
2756  * Returns NT_STATUS_SHARING_VIOLATION if there is any
2757  * sharing conflict.  Returns NT_STATUS_SUCCESS otherwise.
2758  *
2759  * Full system-wide share reservation synchronization is available
2760  * when the nbmand (non-blocking mandatory) mount option is set
2761  * (i.e. nbl_need_crit() is true) and nbmand critical regions are used.
2762  * This provides synchronization with NFS and local processes.  The
2763  * critical regions are entered in VOP_SHRLOCK()/fs_shrlock() (called
2764  * from smb_open_subr()/smb_fsop_shrlock()/smb_vop_shrlock()) as well
2765  * as the CIFS rename and delete paths.
2766  *
2767  * The CIFS server will also enter the nbl critical region in the open,
2768  * rename, and delete paths when nbmand is not set.  There is limited
2769  * coordination with local and VFS share reservations in this case.
2770  * Note that when the nbmand mount option is not set, the VFS layer
2771  * only processes advisory reservations and the delete mode is not checked.
2772  *
2773  * Whether or not the nbmand mount option is set, intra-CIFS share
2774  * checking is done in the open, delete, and rename paths using a CIFS
2775  * critical region (node->n_share_lock).
2776  */
2777 uint32_t
2778 smb_fsop_shrlock(cred_t *cr, smb_node_t *node, uint32_t uniq_fid,
2779     uint32_t desired_access, uint32_t share_access)
2780 {
2781 	int rc;
2782 
2783 	/* Allow access if the request is just for meta data */
2784 	if ((desired_access & FILE_DATA_ALL) == 0)
2785 		return (NT_STATUS_SUCCESS);
2786 
2787 	rc = smb_node_open_check(node, desired_access, share_access);
2788 	if (rc)
2789 		return (NT_STATUS_SHARING_VIOLATION);
2790 
2791 	rc = smb_vop_shrlock(node->vp, uniq_fid, desired_access, share_access,
2792 	    cr);
2793 	if (rc)
2794 		return (NT_STATUS_SHARING_VIOLATION);
2795 
2796 	return (NT_STATUS_SUCCESS);
2797 }
2798 
2799 void
2800 smb_fsop_unshrlock(cred_t *cr, smb_node_t *node, uint32_t uniq_fid)
2801 {
2802 	(void) smb_vop_unshrlock(node->vp, uniq_fid, cr);
2803 }
2804 
2805 int
2806 smb_fsop_frlock(smb_node_t *node, smb_lock_t *lock, boolean_t unlock,
2807     cred_t *cr)
2808 {
2809 	flock64_t bf;
2810 	int flag = F_REMOTELOCK;
2811 
2812 	/*
2813 	 * VOP_FRLOCK() will not be called if:
2814 	 *
2815 	 * 1) The lock has a range of zero bytes. The semantics of Windows and
2816 	 *    POSIX are different. In the case of POSIX it asks for the locking
2817 	 *    of all the bytes from the offset provided until the end of the
2818 	 *    file. In the case of Windows a range of zero locks nothing and
2819 	 *    doesn't conflict with any other lock.
2820 	 *
2821 	 * 2) The lock rolls over (start + lenght < start). Solaris will assert
2822 	 *    if such a request is submitted. This will not create
2823 	 *    incompatibilities between POSIX and Windows. In the Windows world,
2824 	 *    if a client submits such a lock, the server will not lock any
2825 	 *    bytes. Interestingly if the same lock (same offset and length) is
2826 	 *    resubmitted Windows will consider that there is an overlap and
2827 	 *    the granting rules will then apply.
2828 	 *
2829 	 * 3) The SMB-level process IDs (smb_pid) are not passed down to the
2830 	 *    POSIX level in l_pid because (a) the rules about lock PIDs are
2831 	 *    different in SMB, and (b) we're putting our ofile f_uniqid in
2832 	 *    the POSIX l_pid field to segregate locks per SMB ofile.
2833 	 *    (We're also using a "remote" system ID in l_sysid.)
2834 	 *    All SMB locking PIDs are handled at the SMB level and
2835 	 *    not exposed in POSIX locking.
2836 	 */
2837 	if ((lock->l_length == 0) ||
2838 	    ((lock->l_start + lock->l_length - 1) < lock->l_start))
2839 		return (0);
2840 
2841 	bzero(&bf, sizeof (bf));
2842 
2843 	if (unlock) {
2844 		bf.l_type = F_UNLCK;
2845 	} else if (lock->l_type == SMB_LOCK_TYPE_READONLY) {
2846 		bf.l_type = F_RDLCK;
2847 		flag |= FREAD;
2848 	} else if (lock->l_type == SMB_LOCK_TYPE_READWRITE) {
2849 		bf.l_type = F_WRLCK;
2850 		flag |= FWRITE;
2851 	}
2852 
2853 	bf.l_start = lock->l_start;
2854 	bf.l_len = lock->l_length;
2855 	bf.l_pid = lock->l_file->f_uniqid;
2856 	bf.l_sysid = smb_ct.cc_sysid;
2857 
2858 	return (smb_vop_frlock(node->vp, cr, flag, &bf));
2859 }
2860