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