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