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