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