xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_common_open.c (revision 5677e04907859594bfe1a95ba3bfdb1629b89dac)
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 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
25  */
26 
27 /*
28  * This module provides the common open functionality to the various
29  * open and create SMB interface functions.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/cmn_err.h>
34 #include <sys/fcntl.h>
35 #include <sys/nbmlock.h>
36 #include <smbsrv/string.h>
37 #include <smbsrv/smb_kproto.h>
38 #include <smbsrv/smb_fsops.h>
39 #include <smbsrv/smbinfo.h>
40 
41 static volatile uint32_t smb_fids = 0;
42 #define	SMB_UNIQ_FID()	atomic_inc_32_nv(&smb_fids)
43 
44 static uint32_t smb_open_subr(smb_request_t *);
45 extern uint32_t smb_is_executable(char *);
46 static void smb_delete_new_object(smb_request_t *);
47 static int smb_set_open_attributes(smb_request_t *, smb_ofile_t *);
48 static void smb_open_oplock_break(smb_request_t *, smb_node_t *);
49 static boolean_t smb_open_attr_only(smb_arg_open_t *);
50 static boolean_t smb_open_overwrite(smb_arg_open_t *);
51 
52 /*
53  * smb_access_generic_to_file
54  *
55  * Search MSDN for IoCreateFile to see following mapping.
56  *
57  * GENERIC_READ		STANDARD_RIGHTS_READ, FILE_READ_DATA,
58  *			FILE_READ_ATTRIBUTES and FILE_READ_EA
59  *
60  * GENERIC_WRITE	STANDARD_RIGHTS_WRITE, FILE_WRITE_DATA,
61  *               FILE_WRITE_ATTRIBUTES, FILE_WRITE_EA, and FILE_APPEND_DATA
62  *
63  * GENERIC_EXECUTE	STANDARD_RIGHTS_EXECUTE, SYNCHRONIZE, and FILE_EXECUTE.
64  */
65 static uint32_t
66 smb_access_generic_to_file(uint32_t desired_access)
67 {
68 	uint32_t access = 0;
69 
70 	if (desired_access & GENERIC_ALL)
71 		return (FILE_ALL_ACCESS & ~SYNCHRONIZE);
72 
73 	if (desired_access & GENERIC_EXECUTE) {
74 		desired_access &= ~GENERIC_EXECUTE;
75 		access |= (STANDARD_RIGHTS_EXECUTE |
76 		    SYNCHRONIZE | FILE_EXECUTE);
77 	}
78 
79 	if (desired_access & GENERIC_WRITE) {
80 		desired_access &= ~GENERIC_WRITE;
81 		access |= (FILE_GENERIC_WRITE & ~SYNCHRONIZE);
82 	}
83 
84 	if (desired_access & GENERIC_READ) {
85 		desired_access &= ~GENERIC_READ;
86 		access |= FILE_GENERIC_READ;
87 	}
88 
89 	return (access | desired_access);
90 }
91 
92 /*
93  * smb_omode_to_amask
94  *
95  * This function converts open modes used by Open and Open AndX
96  * commands to desired access bits used by NT Create AndX command.
97  */
98 uint32_t
99 smb_omode_to_amask(uint32_t desired_access)
100 {
101 	switch (desired_access & SMB_DA_ACCESS_MASK) {
102 	case SMB_DA_ACCESS_READ:
103 		return (FILE_GENERIC_READ);
104 
105 	case SMB_DA_ACCESS_WRITE:
106 		return (FILE_GENERIC_WRITE);
107 
108 	case SMB_DA_ACCESS_READ_WRITE:
109 		return (FILE_GENERIC_READ | FILE_GENERIC_WRITE);
110 
111 	case SMB_DA_ACCESS_EXECUTE:
112 		return (FILE_GENERIC_READ | FILE_GENERIC_EXECUTE);
113 
114 	default:
115 		return (FILE_GENERIC_ALL);
116 	}
117 }
118 
119 /*
120  * smb_denymode_to_sharemode
121  *
122  * This function converts deny modes used by Open and Open AndX
123  * commands to share access bits used by NT Create AndX command.
124  */
125 uint32_t
126 smb_denymode_to_sharemode(uint32_t desired_access, char *fname)
127 {
128 	switch (desired_access & SMB_DA_SHARE_MASK) {
129 	case SMB_DA_SHARE_COMPATIBILITY:
130 		if (smb_is_executable(fname))
131 			return (FILE_SHARE_READ | FILE_SHARE_WRITE);
132 
133 		return (FILE_SHARE_ALL);
134 
135 	case SMB_DA_SHARE_EXCLUSIVE:
136 		return (FILE_SHARE_NONE);
137 
138 	case SMB_DA_SHARE_DENY_WRITE:
139 		return (FILE_SHARE_READ);
140 
141 	case SMB_DA_SHARE_DENY_READ:
142 		return (FILE_SHARE_WRITE);
143 
144 	case SMB_DA_SHARE_DENY_NONE:
145 	default:
146 		return (FILE_SHARE_READ | FILE_SHARE_WRITE);
147 	}
148 }
149 
150 /*
151  * smb_ofun_to_crdisposition
152  *
153  * This function converts open function values used by Open and Open AndX
154  * commands to create disposition values used by NT Create AndX command.
155  */
156 uint32_t
157 smb_ofun_to_crdisposition(uint16_t  ofun)
158 {
159 	static int ofun_cr_map[3][2] =
160 	{
161 		{ -1,			FILE_CREATE },
162 		{ FILE_OPEN,		FILE_OPEN_IF },
163 		{ FILE_OVERWRITE,	FILE_OVERWRITE_IF }
164 	};
165 
166 	int row = ofun & SMB_OFUN_OPEN_MASK;
167 	int col = (ofun & SMB_OFUN_CREATE_MASK) >> 4;
168 
169 	if (row == 3)
170 		return (FILE_MAXIMUM_DISPOSITION + 1);
171 
172 	return (ofun_cr_map[row][col]);
173 }
174 
175 /*
176  * Retry opens to avoid spurious sharing violations, due to timing
177  * issues between closes and opens.  The client that already has the
178  * file open may be in the process of closing it.
179  */
180 uint32_t
181 smb_common_open(smb_request_t *sr)
182 {
183 	smb_arg_open_t	*parg;
184 	uint32_t	status = NT_STATUS_SUCCESS;
185 	int		count;
186 
187 	parg = kmem_alloc(sizeof (*parg), KM_SLEEP);
188 	bcopy(&sr->arg.open, parg, sizeof (*parg));
189 
190 	for (count = 0; count <= 4; count++) {
191 		if (count != 0)
192 			delay(MSEC_TO_TICK(400));
193 
194 		status = smb_open_subr(sr);
195 		if (status != NT_STATUS_SHARING_VIOLATION)
196 			break;
197 
198 		bcopy(parg, &sr->arg.open, sizeof (*parg));
199 	}
200 
201 	if (status == NT_STATUS_NO_SUCH_FILE)
202 		status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
203 
204 	kmem_free(parg, sizeof (*parg));
205 	return (status);
206 }
207 
208 /*
209  * smb_open_subr
210  *
211  * Notes on write-through behaviour. It looks like pre-LM0.12 versions
212  * of the protocol specify the write-through mode when a file is opened,
213  * (SmbOpen, SmbOpenAndX) so the write calls (SmbWrite, SmbWriteAndClose,
214  * SmbWriteAndUnlock) don't need to contain a write-through flag.
215  *
216  * With LM0.12, the open calls (SmbCreateAndX, SmbNtTransactCreate)
217  * don't indicate which write-through mode to use. Instead the write
218  * calls (SmbWriteAndX, SmbWriteRaw) specify the mode on a per call
219  * basis.
220  *
221  * We don't care which open call was used to get us here, we just need
222  * to ensure that the write-through mode flag is copied from the open
223  * parameters to the node. We test the omode write-through flag in all
224  * write functions.
225  *
226  * This function returns NT status codes.
227  *
228  * The following rules apply when processing a file open request:
229  *
230  * - Oplocks must be broken prior to share checking as the break may
231  *   cause other clients to close the file, which would affect sharing
232  *   checks.
233  *
234  * - Share checks must take place prior to access checks for correct
235  * Windows semantics and to prevent unnecessary NFS delegation recalls.
236  *
237  * - Oplocks must be acquired after open to ensure the correct
238  * synchronization with NFS delegation and FEM installation.
239  *
240  * DOS readonly bit rules
241  *
242  * 1. The creator of a readonly file can write to/modify the size of the file
243  * using the original create fid, even though the file will appear as readonly
244  * to all other fids and via a CIFS getattr call.
245  * The readonly bit therefore cannot be set in the filesystem until the file
246  * is closed (smb_ofile_close). It is accounted for via ofile and node flags.
247  *
248  * 2. A setinfo operation (using either an open fid or a path) to set/unset
249  * readonly will be successful regardless of whether a creator of a readonly
250  * file has an open fid (and has the special privilege mentioned in #1,
251  * above).  I.e., the creator of a readonly fid holding that fid will no longer
252  * have a special privilege.
253  *
254  * 3. The DOS readonly bit affects only data and some metadata.
255  * The following metadata can be changed regardless of the readonly bit:
256  * 	- security descriptors
257  *	- DOS attributes
258  *	- timestamps
259  *
260  * In the current implementation, the file size cannot be changed (except for
261  * the exceptions in #1 and #2, above).
262  *
263  *
264  * DOS attribute rules
265  *
266  * These rules are specific to creating / opening files and directories.
267  * How the attribute value (specifically ZERO or FILE_ATTRIBUTE_NORMAL)
268  * should be interpreted may differ in other requests.
269  *
270  * - An attribute value equal to ZERO or FILE_ATTRIBUTE_NORMAL means that the
271  *   file's attributes should be cleared.
272  * - If FILE_ATTRIBUTE_NORMAL is specified with any other attributes,
273  *   FILE_ATTRIBUTE_NORMAL is ignored.
274  *
275  * 1. Creating a new file
276  * - The request attributes + FILE_ATTRIBUTE_ARCHIVE are applied to the file.
277  *
278  * 2. Creating a new directory
279  * - The request attributes + FILE_ATTRIBUTE_DIRECTORY are applied to the file.
280  * - FILE_ATTRIBUTE_ARCHIVE does not get set.
281  *
282  * 3. Overwriting an existing file
283  * - the request attributes are used as search attributes. If the existing
284  *   file does not meet the search criteria access is denied.
285  * - otherwise, applies attributes + FILE_ATTRIBUTE_ARCHIVE.
286  *
287  * 4. Opening an existing file or directory
288  *    The request attributes are ignored.
289  */
290 static uint32_t
291 smb_open_subr(smb_request_t *sr)
292 {
293 	boolean_t	created = B_FALSE;
294 	boolean_t	last_comp_found = B_FALSE;
295 	smb_node_t	*node = NULL;
296 	smb_node_t	*dnode = NULL;
297 	smb_node_t	*cur_node = NULL;
298 	smb_arg_open_t	*op = &sr->sr_open;
299 	int		rc;
300 	smb_ofile_t	*of;
301 	smb_attr_t	new_attr;
302 	int		max_requested = 0;
303 	uint32_t	max_allowed;
304 	uint32_t	status = NT_STATUS_SUCCESS;
305 	int		is_dir;
306 	smb_error_t	err;
307 	boolean_t	is_stream = B_FALSE;
308 	int		lookup_flags = SMB_FOLLOW_LINKS;
309 	uint32_t	uniq_fid;
310 	smb_pathname_t	*pn = &op->fqi.fq_path;
311 	smb_server_t	*sv = sr->sr_server;
312 
313 	/* Get out now if we've been cancelled. */
314 	mutex_enter(&sr->sr_mutex);
315 	if (sr->sr_state != SMB_REQ_STATE_ACTIVE) {
316 		mutex_exit(&sr->sr_mutex);
317 		return (NT_STATUS_CANCELLED);
318 	}
319 	mutex_exit(&sr->sr_mutex);
320 
321 	is_dir = (op->create_options & FILE_DIRECTORY_FILE) ? 1 : 0;
322 
323 	/*
324 	 * If the object being created or opened is a directory
325 	 * the Disposition parameter must be one of FILE_CREATE,
326 	 * FILE_OPEN, or FILE_OPEN_IF
327 	 */
328 	if (is_dir) {
329 		if ((op->create_disposition != FILE_CREATE) &&
330 		    (op->create_disposition != FILE_OPEN_IF) &&
331 		    (op->create_disposition != FILE_OPEN)) {
332 			return (NT_STATUS_INVALID_PARAMETER);
333 		}
334 	}
335 
336 	if (op->desired_access & MAXIMUM_ALLOWED) {
337 		max_requested = 1;
338 		op->desired_access &= ~MAXIMUM_ALLOWED;
339 	}
340 	op->desired_access = smb_access_generic_to_file(op->desired_access);
341 
342 	if (sr->session->s_file_cnt >= SMB_SESSION_OFILE_MAX) {
343 		ASSERT(sr->uid_user);
344 		cmn_err(CE_NOTE, "smbsrv[%s\\%s]: TOO_MANY_OPENED_FILES",
345 		    sr->uid_user->u_domain, sr->uid_user->u_name);
346 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
347 	}
348 
349 	/* This must be NULL at this point */
350 	sr->fid_ofile = NULL;
351 
352 	op->devstate = 0;
353 
354 	switch (sr->tid_tree->t_res_type & STYPE_MASK) {
355 	case STYPE_DISKTREE:
356 	case STYPE_PRINTQ:
357 		break;
358 
359 	case STYPE_IPC:
360 		/*
361 		 * Security descriptors for pipes are not implemented,
362 		 * so just setup a reasonable access mask.
363 		 */
364 		op->desired_access = (READ_CONTROL | SYNCHRONIZE |
365 		    FILE_READ_DATA | FILE_READ_ATTRIBUTES |
366 		    FILE_WRITE_DATA | FILE_APPEND_DATA);
367 
368 		/*
369 		 * Limit the number of open pipe instances.
370 		 */
371 		if ((rc = smb_threshold_enter(&sv->sv_opipe_ct)) != 0) {
372 			status = RPC_NT_SERVER_TOO_BUSY;
373 			return (status);
374 		}
375 
376 		/*
377 		 * No further processing for IPC, we need to either
378 		 * raise an exception or return success here.
379 		 */
380 		uniq_fid = SMB_UNIQ_FID();
381 		status = smb_opipe_open(sr, uniq_fid);
382 		smb_threshold_exit(&sv->sv_opipe_ct);
383 		return (status);
384 
385 	default:
386 		return (NT_STATUS_BAD_DEVICE_TYPE);
387 	}
388 
389 	smb_pathname_init(sr, pn, pn->pn_path);
390 	if (!smb_pathname_validate(sr, pn))
391 		return (sr->smb_error.status);
392 
393 	if (strlen(pn->pn_path) >= SMB_MAXPATHLEN) {
394 		return (NT_STATUS_OBJECT_PATH_INVALID);
395 	}
396 
397 	if (is_dir) {
398 		if (!smb_validate_dirname(sr, pn))
399 			return (sr->smb_error.status);
400 	} else {
401 		if (!smb_validate_object_name(sr, pn))
402 			return (sr->smb_error.status);
403 	}
404 
405 	cur_node = op->fqi.fq_dnode ?
406 	    op->fqi.fq_dnode : sr->tid_tree->t_snode;
407 
408 	rc = smb_pathname_reduce(sr, sr->user_cr, pn->pn_path,
409 	    sr->tid_tree->t_snode, cur_node, &op->fqi.fq_dnode,
410 	    op->fqi.fq_last_comp);
411 	if (rc != 0) {
412 		return (smb_errno2status(rc));
413 	}
414 
415 	/*
416 	 * If the access mask has only DELETE set (ignore
417 	 * FILE_READ_ATTRIBUTES), then assume that this
418 	 * is a request to delete the link (if a link)
419 	 * and do not follow links.  Otherwise, follow
420 	 * the link to the target.
421 	 */
422 	if ((op->desired_access & ~FILE_READ_ATTRIBUTES) == DELETE)
423 		lookup_flags &= ~SMB_FOLLOW_LINKS;
424 
425 	rc = smb_fsop_lookup_name(sr, zone_kcred(), lookup_flags,
426 	    sr->tid_tree->t_snode, op->fqi.fq_dnode, op->fqi.fq_last_comp,
427 	    &op->fqi.fq_fnode);
428 
429 	if (rc == 0) {
430 		last_comp_found = B_TRUE;
431 		/*
432 		 * Need the DOS attributes below, where we
433 		 * check the search attributes (sattr).
434 		 */
435 		op->fqi.fq_fattr.sa_mask = SMB_AT_DOSATTR;
436 		rc = smb_node_getattr(sr, op->fqi.fq_fnode, zone_kcred(),
437 		    NULL, &op->fqi.fq_fattr);
438 		if (rc != 0) {
439 			smb_node_release(op->fqi.fq_fnode);
440 			smb_node_release(op->fqi.fq_dnode);
441 			return (NT_STATUS_INTERNAL_ERROR);
442 		}
443 	} else if (rc == ENOENT) {
444 		last_comp_found = B_FALSE;
445 		op->fqi.fq_fnode = NULL;
446 		rc = 0;
447 	} else {
448 		smb_node_release(op->fqi.fq_dnode);
449 		return (smb_errno2status(rc));
450 	}
451 
452 
453 	/*
454 	 * The uniq_fid is a CIFS-server-wide unique identifier for an ofile
455 	 * which is used to uniquely identify open instances for the
456 	 * VFS share reservation and POSIX locks.
457 	 */
458 
459 	uniq_fid = SMB_UNIQ_FID();
460 
461 	if (last_comp_found) {
462 
463 		node = op->fqi.fq_fnode;
464 		dnode = op->fqi.fq_dnode;
465 
466 		if (!smb_node_is_file(node) && !smb_node_is_dir(node) &&
467 		    !smb_node_is_symlink(node)) {
468 			smb_node_release(node);
469 			smb_node_release(dnode);
470 			return (NT_STATUS_ACCESS_DENIED);
471 		}
472 
473 		/*
474 		 * Reject this request if either:
475 		 * - the target IS a directory and the client requires that
476 		 *   it must NOT be (required by Lotus Notes)
477 		 * - the target is NOT a directory and client requires that
478 		 *   it MUST be.
479 		 */
480 		if (smb_node_is_dir(node)) {
481 			if (op->create_options & FILE_NON_DIRECTORY_FILE) {
482 				smb_node_release(node);
483 				smb_node_release(dnode);
484 				return (NT_STATUS_FILE_IS_A_DIRECTORY);
485 			}
486 		} else {
487 			if ((op->create_options & FILE_DIRECTORY_FILE) ||
488 			    (op->nt_flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)) {
489 				smb_node_release(node);
490 				smb_node_release(dnode);
491 				return (NT_STATUS_NOT_A_DIRECTORY);
492 			}
493 		}
494 
495 		/*
496 		 * No more open should be accepted when "Delete on close"
497 		 * flag is set.
498 		 */
499 		if (node->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
500 			smb_node_release(node);
501 			smb_node_release(dnode);
502 			return (NT_STATUS_DELETE_PENDING);
503 		}
504 
505 		/*
506 		 * Specified file already exists so the operation should fail.
507 		 */
508 		if (op->create_disposition == FILE_CREATE) {
509 			smb_node_release(node);
510 			smb_node_release(dnode);
511 			return (NT_STATUS_OBJECT_NAME_COLLISION);
512 		}
513 
514 		/*
515 		 * Windows seems to check read-only access before file
516 		 * sharing check.
517 		 *
518 		 * Check to see if the file is currently readonly (irrespective
519 		 * of whether this open will make it readonly).
520 		 */
521 		if (SMB_PATHFILE_IS_READONLY(sr, node)) {
522 			/* Files data only */
523 			if (!smb_node_is_dir(node)) {
524 				if (op->desired_access & (FILE_WRITE_DATA |
525 				    FILE_APPEND_DATA)) {
526 					smb_node_release(node);
527 					smb_node_release(dnode);
528 					return (NT_STATUS_ACCESS_DENIED);
529 				}
530 			}
531 		}
532 
533 		if ((op->create_disposition == FILE_SUPERSEDE) ||
534 		    (op->create_disposition == FILE_OVERWRITE_IF) ||
535 		    (op->create_disposition == FILE_OVERWRITE)) {
536 
537 			if (!smb_sattr_check(op->fqi.fq_fattr.sa_dosattr,
538 			    op->dattr)) {
539 				smb_node_release(node);
540 				smb_node_release(dnode);
541 				return (NT_STATUS_ACCESS_DENIED);
542 			}
543 
544 			if (smb_node_is_dir(node)) {
545 				smb_node_release(node);
546 				smb_node_release(dnode);
547 				return (NT_STATUS_ACCESS_DENIED);
548 			}
549 		}
550 
551 		/* MS-FSA 2.1.5.1.2 */
552 		if (op->create_disposition == FILE_SUPERSEDE)
553 			op->desired_access |= DELETE;
554 		if ((op->create_disposition == FILE_OVERWRITE_IF) ||
555 		    (op->create_disposition == FILE_OVERWRITE))
556 			op->desired_access |= FILE_WRITE_DATA;
557 
558 		status = smb_fsop_access(sr, sr->user_cr, node,
559 		    op->desired_access);
560 		if (status != NT_STATUS_SUCCESS) {
561 			smb_node_release(node);
562 			smb_node_release(dnode);
563 
564 			/* SMB1 specific? NT_STATUS_PRIVILEGE_NOT_HELD */
565 			if (status == NT_STATUS_PRIVILEGE_NOT_HELD) {
566 				return (status);
567 			} else {
568 				return (NT_STATUS_ACCESS_DENIED);
569 			}
570 		}
571 
572 		if (max_requested) {
573 			smb_fsop_eaccess(sr, sr->user_cr, node, &max_allowed);
574 			op->desired_access |= max_allowed;
575 		}
576 		/*
577 		 * According to MS "dochelp" mail in Mar 2015, any handle
578 		 * on which read or write access is granted implicitly
579 		 * gets "read attributes", even if it was not requested.
580 		 * This avoids unexpected access failures later that
581 		 * would happen if these were not granted.
582 		 */
583 		if ((op->desired_access & FILE_DATA_ALL) != 0) {
584 			op->desired_access |= (READ_CONTROL |
585 			    FILE_READ_ATTRIBUTES);
586 		}
587 
588 		/*
589 		 * Oplock break is done prior to sharing checks as the break
590 		 * may cause other clients to close the file which would
591 		 * affect the sharing checks. This may block, so set the
592 		 * file opening count before oplock stuff.
593 		 */
594 		smb_node_inc_opening_count(node);
595 		smb_open_oplock_break(sr, node);
596 
597 		smb_node_wrlock(node);
598 
599 		/*
600 		 * Check for sharing violations
601 		 */
602 		status = smb_fsop_shrlock(sr->user_cr, node, uniq_fid,
603 		    op->desired_access, op->share_access);
604 		if (status == NT_STATUS_SHARING_VIOLATION) {
605 			smb_node_unlock(node);
606 			smb_node_dec_opening_count(node);
607 			smb_node_release(node);
608 			smb_node_release(dnode);
609 			return (status);
610 		}
611 
612 		/*
613 		 * Go ahead with modifications as necessary.
614 		 */
615 		switch (op->create_disposition) {
616 		case FILE_SUPERSEDE:
617 		case FILE_OVERWRITE_IF:
618 		case FILE_OVERWRITE:
619 			op->dattr |= FILE_ATTRIBUTE_ARCHIVE;
620 			/* Don't apply readonly bit until smb_ofile_close */
621 			if (op->dattr & FILE_ATTRIBUTE_READONLY) {
622 				op->created_readonly = B_TRUE;
623 				op->dattr &= ~FILE_ATTRIBUTE_READONLY;
624 			}
625 
626 			/*
627 			 * Truncate the file data here.
628 			 * We set alloc_size = op->dsize later,
629 			 * after we have an ofile.  See:
630 			 * smb_set_open_attributes
631 			 */
632 			bzero(&new_attr, sizeof (new_attr));
633 			new_attr.sa_dosattr = op->dattr;
634 			new_attr.sa_vattr.va_size = 0;
635 			new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_SIZE;
636 			rc = smb_fsop_setattr(sr, sr->user_cr, node, &new_attr);
637 			if (rc != 0) {
638 				smb_fsop_unshrlock(sr->user_cr, node, uniq_fid);
639 				smb_node_unlock(node);
640 				smb_node_dec_opening_count(node);
641 				smb_node_release(node);
642 				smb_node_release(dnode);
643 				return (smb_errno2status(rc));
644 			}
645 
646 			/*
647 			 * If file is being replaced, remove existing streams
648 			 */
649 			if (SMB_IS_STREAM(node) == 0) {
650 				status = smb_fsop_remove_streams(sr,
651 				    sr->user_cr, node);
652 				if (status != 0) {
653 					smb_fsop_unshrlock(sr->user_cr, node,
654 					    uniq_fid);
655 					smb_node_unlock(node);
656 					smb_node_dec_opening_count(node);
657 					smb_node_release(node);
658 					smb_node_release(dnode);
659 					return (status);
660 				}
661 			}
662 
663 			op->action_taken = SMB_OACT_TRUNCATED;
664 			break;
665 
666 		default:
667 			/*
668 			 * FILE_OPEN or FILE_OPEN_IF.
669 			 */
670 			/*
671 			 * Ignore any user-specified alloc_size for
672 			 * existing files, to avoid truncation in
673 			 * smb_set_open_attributes
674 			 */
675 			op->dsize = 0L;
676 			op->action_taken = SMB_OACT_OPENED;
677 			break;
678 		}
679 	} else {
680 		/* Last component was not found. */
681 		dnode = op->fqi.fq_dnode;
682 
683 		if (is_dir == 0)
684 			is_stream = smb_is_stream_name(pn->pn_path);
685 
686 		if ((op->create_disposition == FILE_OPEN) ||
687 		    (op->create_disposition == FILE_OVERWRITE)) {
688 			smb_node_release(dnode);
689 			return (NT_STATUS_OBJECT_NAME_NOT_FOUND);
690 		}
691 
692 		if (pn->pn_fname && smb_is_invalid_filename(pn->pn_fname)) {
693 			smb_node_release(dnode);
694 			return (NT_STATUS_OBJECT_NAME_INVALID);
695 		}
696 
697 		/*
698 		 * lock the parent dir node in case another create
699 		 * request to the same parent directory comes in.
700 		 */
701 		smb_node_wrlock(dnode);
702 
703 		/* Don't apply readonly bit until smb_ofile_close */
704 		if (op->dattr & FILE_ATTRIBUTE_READONLY) {
705 			op->dattr &= ~FILE_ATTRIBUTE_READONLY;
706 			op->created_readonly = B_TRUE;
707 		}
708 
709 		bzero(&new_attr, sizeof (new_attr));
710 		if ((op->crtime.tv_sec != 0) &&
711 		    (op->crtime.tv_sec != UINT_MAX)) {
712 
713 			new_attr.sa_mask |= SMB_AT_CRTIME;
714 			new_attr.sa_crtime = op->crtime;
715 		}
716 
717 		if (is_dir == 0) {
718 			op->dattr |= FILE_ATTRIBUTE_ARCHIVE;
719 			new_attr.sa_dosattr = op->dattr;
720 			new_attr.sa_vattr.va_type = VREG;
721 			new_attr.sa_vattr.va_mode = is_stream ? S_IRUSR :
722 			    S_IRUSR | S_IRGRP | S_IROTH |
723 			    S_IWUSR | S_IWGRP | S_IWOTH;
724 			new_attr.sa_mask |=
725 			    SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE;
726 
727 			/*
728 			 * We set alloc_size = op->dsize later,
729 			 * (in smb_set_open_attributes) after we
730 			 * have an ofile on which to save that.
731 			 *
732 			 * Legacy Open&X sets size to alloc_size
733 			 * when creating a new file.
734 			 */
735 			if (sr->smb_com == SMB_COM_OPEN_ANDX) {
736 				new_attr.sa_vattr.va_size = op->dsize;
737 				new_attr.sa_mask |= SMB_AT_SIZE;
738 			}
739 
740 			rc = smb_fsop_create(sr, sr->user_cr, dnode,
741 			    op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode);
742 
743 			if (rc != 0) {
744 				smb_node_unlock(dnode);
745 				smb_node_release(dnode);
746 				return (smb_errno2status(rc));
747 			}
748 
749 			node = op->fqi.fq_fnode;
750 			smb_node_inc_opening_count(node);
751 			smb_node_wrlock(node);
752 
753 			status = smb_fsop_shrlock(sr->user_cr, node, uniq_fid,
754 			    op->desired_access, op->share_access);
755 
756 			if (status == NT_STATUS_SHARING_VIOLATION) {
757 				smb_node_unlock(node);
758 				smb_node_dec_opening_count(node);
759 				smb_delete_new_object(sr);
760 				smb_node_release(node);
761 				smb_node_unlock(dnode);
762 				smb_node_release(dnode);
763 				return (status);
764 			}
765 		} else {
766 			op->dattr |= FILE_ATTRIBUTE_DIRECTORY;
767 			new_attr.sa_dosattr = op->dattr;
768 			new_attr.sa_vattr.va_type = VDIR;
769 			new_attr.sa_vattr.va_mode = 0777;
770 			new_attr.sa_mask |=
771 			    SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE;
772 
773 			rc = smb_fsop_mkdir(sr, sr->user_cr, dnode,
774 			    op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode);
775 			if (rc != 0) {
776 				smb_node_unlock(dnode);
777 				smb_node_release(dnode);
778 				return (smb_errno2status(rc));
779 			}
780 
781 			node = op->fqi.fq_fnode;
782 			smb_node_inc_opening_count(node);
783 			smb_node_wrlock(node);
784 		}
785 
786 		created = B_TRUE;
787 		op->action_taken = SMB_OACT_CREATED;
788 
789 		if (max_requested) {
790 			smb_fsop_eaccess(sr, sr->user_cr, node, &max_allowed);
791 			op->desired_access |= max_allowed;
792 		}
793 		/*
794 		 * We created this object (we own it) so grant
795 		 * read_control + read_attributes on this handle,
796 		 * even if that was not requested.  This avoids
797 		 * unexpected access failures later.
798 		 */
799 		op->desired_access |= (READ_CONTROL | FILE_READ_ATTRIBUTES);
800 	}
801 
802 	status = NT_STATUS_SUCCESS;
803 
804 	of = smb_ofile_open(sr, node, op, SMB_FTYPE_DISK, uniq_fid,
805 	    &err);
806 	if (of == NULL) {
807 		status = err.status;
808 	}
809 
810 	/*
811 	 * We might have blocked in smb_ofile_open long enough so a
812 	 * tree disconnect might have happened.  In that case, we've
813 	 * just added an ofile to a tree that's disconnecting, and
814 	 * need to undo that to avoid interfering with tear-down of
815 	 * the tree connection.
816 	 */
817 	if (status == NT_STATUS_SUCCESS &&
818 	    !smb_tree_is_connected(sr->tid_tree)) {
819 		status = NT_STATUS_INVALID_PARAMETER;
820 	}
821 
822 	/*
823 	 * This MUST be done after ofile creation, so that explicitly
824 	 * set timestamps can be remembered on the ofile, and the
825 	 * readonly flag will be stored "pending" on the node.
826 	 */
827 	if (status == NT_STATUS_SUCCESS) {
828 		if ((rc = smb_set_open_attributes(sr, of)) != 0) {
829 			status = smb_errno2status(rc);
830 		}
831 	}
832 
833 	if (status == NT_STATUS_SUCCESS) {
834 		/*
835 		 * We've already done access checks above,
836 		 * and want this call to succeed even when
837 		 * !(desired_access & FILE_READ_ATTRIBUTES),
838 		 * so pass kcred here.
839 		 */
840 		op->fqi.fq_fattr.sa_mask = SMB_AT_ALL;
841 		rc = smb_node_getattr(sr, node, zone_kcred(), of,
842 		    &op->fqi.fq_fattr);
843 		if (rc != 0) {
844 			status = NT_STATUS_INTERNAL_ERROR;
845 		}
846 	}
847 
848 	/*
849 	 * smb_fsop_unshrlock is a no-op if node is a directory
850 	 * smb_fsop_unshrlock is done in smb_ofile_close
851 	 */
852 	if (status != NT_STATUS_SUCCESS) {
853 		if (of == NULL) {
854 			smb_fsop_unshrlock(sr->user_cr, node, uniq_fid);
855 		} else {
856 			smb_ofile_close(of, 0);
857 			smb_ofile_release(of);
858 		}
859 		if (created)
860 			smb_delete_new_object(sr);
861 		smb_node_unlock(node);
862 		smb_node_dec_opening_count(node);
863 		smb_node_release(node);
864 		if (created)
865 			smb_node_unlock(dnode);
866 		smb_node_release(dnode);
867 		return (status);
868 	}
869 
870 	/*
871 	 * Propagate the write-through mode from the open params
872 	 * to the node: see the notes in the function header.
873 	 */
874 	if (sr->sr_cfg->skc_sync_enable ||
875 	    (op->create_options & FILE_WRITE_THROUGH))
876 		node->flags |= NODE_FLAGS_WRITE_THROUGH;
877 
878 	/*
879 	 * Set up the fileid and dosattr in open_param for response
880 	 */
881 	op->fileid = op->fqi.fq_fattr.sa_vattr.va_nodeid;
882 	op->dattr = op->fqi.fq_fattr.sa_dosattr;
883 
884 	/*
885 	 * Set up the file type in open_param for the response
886 	 */
887 	op->ftype = SMB_FTYPE_DISK;
888 	sr->smb_fid = of->f_fid;
889 	sr->fid_ofile = of;
890 
891 	if (smb_node_is_file(node)) {
892 		smb_oplock_acquire(sr, node, of);
893 		op->dsize = op->fqi.fq_fattr.sa_vattr.va_size;
894 	} else {
895 		/* directory or symlink */
896 		op->op_oplock_level = SMB_OPLOCK_NONE;
897 		op->dsize = 0;
898 	}
899 
900 	smb_node_dec_opening_count(node);
901 
902 	smb_node_unlock(node);
903 	if (created)
904 		smb_node_unlock(dnode);
905 
906 	smb_node_release(node);
907 	smb_node_release(dnode);
908 
909 	return (NT_STATUS_SUCCESS);
910 }
911 
912 /*
913  * smb_open_oplock_break
914  *
915  * If the node has an ofile opened with share access none,
916  * (smb_node_share_check = FALSE) only break BATCH oplock.
917  * Otherwise:
918  * If overwriting, break to SMB_OPLOCK_NONE, else
919  * If opening for anything other than attribute access,
920  * break oplock to LEVEL_II.
921  */
922 static void
923 smb_open_oplock_break(smb_request_t *sr, smb_node_t *node)
924 {
925 	smb_arg_open_t	*op = &sr->sr_open;
926 	uint32_t	flags = 0;
927 
928 	if (!smb_node_share_check(node))
929 		flags |= SMB_OPLOCK_BREAK_BATCH;
930 
931 	if (smb_open_overwrite(op)) {
932 		flags |= SMB_OPLOCK_BREAK_TO_NONE;
933 		(void) smb_oplock_break(sr, node, flags);
934 	} else if (!smb_open_attr_only(op)) {
935 		flags |= SMB_OPLOCK_BREAK_TO_LEVEL_II;
936 		(void) smb_oplock_break(sr, node, flags);
937 	}
938 }
939 
940 /*
941  * smb_open_attr_only
942  *
943  * Determine if file is being opened for attribute access only.
944  * This is used to determine whether it is necessary to break
945  * existing oplocks on the file.
946  */
947 static boolean_t
948 smb_open_attr_only(smb_arg_open_t *op)
949 {
950 	if (((op->desired_access & ~(FILE_READ_ATTRIBUTES |
951 	    FILE_WRITE_ATTRIBUTES | SYNCHRONIZE | READ_CONTROL)) == 0) &&
952 	    (op->create_disposition != FILE_SUPERSEDE) &&
953 	    (op->create_disposition != FILE_OVERWRITE)) {
954 		return (B_TRUE);
955 	}
956 	return (B_FALSE);
957 }
958 
959 static boolean_t
960 smb_open_overwrite(smb_arg_open_t *op)
961 {
962 	if ((op->create_disposition == FILE_SUPERSEDE) ||
963 	    (op->create_disposition == FILE_OVERWRITE_IF) ||
964 	    (op->create_disposition == FILE_OVERWRITE)) {
965 		return (B_TRUE);
966 	}
967 	return (B_FALSE);
968 }
969 
970 /*
971  * smb_set_open_attributes
972  *
973  * Last write time:
974  * - If the last_write time specified in the open params is not 0 or -1,
975  *   use it as file's mtime. This will be considered an explicitly set
976  *   timestamps, not reset by subsequent writes.
977  *
978  * DOS attributes
979  * - If we created_readonly, we now store the real DOS attributes
980  *   (including the readonly bit) so subsequent opens will see it.
981  *
982  * Both are stored "pending" rather than in the file system.
983  *
984  * Returns: errno
985  */
986 static int
987 smb_set_open_attributes(smb_request_t *sr, smb_ofile_t *of)
988 {
989 	smb_attr_t	attr;
990 	smb_arg_open_t	*op = &sr->sr_open;
991 	smb_node_t	*node = of->f_node;
992 	int		rc = 0;
993 
994 	bzero(&attr, sizeof (smb_attr_t));
995 
996 	if (op->created_readonly) {
997 		attr.sa_dosattr = op->dattr | FILE_ATTRIBUTE_READONLY;
998 		attr.sa_mask |= SMB_AT_DOSATTR;
999 	}
1000 
1001 	if (op->dsize != 0) {
1002 		attr.sa_allocsz = op->dsize;
1003 		attr.sa_mask |= SMB_AT_ALLOCSZ;
1004 	}
1005 
1006 	if ((op->mtime.tv_sec != 0) && (op->mtime.tv_sec != UINT_MAX)) {
1007 		attr.sa_vattr.va_mtime = op->mtime;
1008 		attr.sa_mask |= SMB_AT_MTIME;
1009 	}
1010 
1011 	/*
1012 	 * Used to have code here to set mtime, ctime, atime
1013 	 * when the open op->create_disposition is any of:
1014 	 * FILE_SUPERSEDE, FILE_OVERWRITE_IF, FILE_OVERWRITE.
1015 	 * We know that in those cases we will have set the
1016 	 * file size, in which case the file system will
1017 	 * update those times, so we don't have to.
1018 	 *
1019 	 * However, keep track of the fact that we modified
1020 	 * the file via this handle, so we can do the evil,
1021 	 * gratuitious mtime update on close that Windows
1022 	 * clients appear to expect.
1023 	 */
1024 	if (op->action_taken == SMB_OACT_TRUNCATED)
1025 		of->f_written = B_TRUE;
1026 
1027 	if (attr.sa_mask != 0)
1028 		rc = smb_node_setattr(sr, node, of->f_cr, of, &attr);
1029 
1030 	return (rc);
1031 }
1032 
1033 /*
1034  * This function is used to delete a newly created object (file or
1035  * directory) if an error occurs after creation of the object.
1036  */
1037 static void
1038 smb_delete_new_object(smb_request_t *sr)
1039 {
1040 	smb_arg_open_t	*op = &sr->sr_open;
1041 	smb_fqi_t	*fqi = &(op->fqi);
1042 	uint32_t	flags = 0;
1043 
1044 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
1045 		flags |= SMB_IGNORE_CASE;
1046 	if (SMB_TREE_SUPPORTS_CATIA(sr))
1047 		flags |= SMB_CATIA;
1048 
1049 	if (op->create_options & FILE_DIRECTORY_FILE)
1050 		(void) smb_fsop_rmdir(sr, sr->user_cr, fqi->fq_dnode,
1051 		    fqi->fq_last_comp, flags);
1052 	else
1053 		(void) smb_fsop_remove(sr, sr->user_cr, fqi->fq_dnode,
1054 		    fqi->fq_last_comp, flags);
1055 }
1056