1da6c28aamw/*
2da6c28aamw * CDDL HEADER START
3da6c28aamw *
4da6c28aamw * The contents of this file are subject to the terms of the
5da6c28aamw * Common Development and Distribution License (the "License").
6da6c28aamw * You may not use this file except in compliance with the License.
7da6c28aamw *
8da6c28aamw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aamw * or http://www.opensolaris.org/os/licensing.
10da6c28aamw * See the License for the specific language governing permissions
11da6c28aamw * and limitations under the License.
12da6c28aamw *
13da6c28aamw * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aamw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aamw * If applicable, add the following below this CDDL HEADER, with the
16da6c28aamw * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aamw * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aamw *
19da6c28aamw * CDDL HEADER END
20da6c28aamw */
21148c5f4Alan Wright
22da6c28aamw/*
23148c5f4Alan Wright * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
245677e04Gordon Ross * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
25da6c28aamw */
26da6c28aamw
27da6c28aamw/*
28da6c28aamw * This module provides the common open functionality to the various
29da6c28aamw * open and create SMB interface functions.
30da6c28aamw */
31da6c28aamw
32bbf6f00Jordan Brown#include <sys/types.h>
33bbf6f00Jordan Brown#include <sys/cmn_err.h>
34da6c28aamw#include <sys/fcntl.h>
35dc20a30as#include <sys/nbmlock.h>
36bbf6f00Jordan Brown#include <smbsrv/string.h>
37148d1a4Matt Barden#include <smbsrv/smb2_kproto.h>
38bbf6f00Jordan Brown#include <smbsrv/smb_fsops.h>
39bbf6f00Jordan Brown#include <smbsrv/smbinfo.h>
40da6c28aamw
4191ca6bfGordon Rossint smb_session_ofile_max = 32768;
4291ca6bfGordon Ross
43faa1795jbextern uint32_t smb_is_executable(char *);
448b2cc8aafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic void smb_delete_new_object(smb_request_t *);
455fd03bcGordon Rossstatic int smb_set_open_attributes(smb_request_t *, smb_ofile_t *);
46da6c28aamw
47da6c28aamw/*
48da6c28aamw * smb_access_generic_to_file
49da6c28aamw *
50da6c28aamw * Search MSDN for IoCreateFile to see following mapping.
51da6c28aamw *
52da6c28aamw * GENERIC_READ		STANDARD_RIGHTS_READ, FILE_READ_DATA,
53da6c28aamw *			FILE_READ_ATTRIBUTES and FILE_READ_EA
54da6c28aamw *
55da6c28aamw * GENERIC_WRITE	STANDARD_RIGHTS_WRITE, FILE_WRITE_DATA,
56da6c28aamw *               FILE_WRITE_ATTRIBUTES, FILE_WRITE_EA, and FILE_APPEND_DATA
57da6c28aamw *
58da6c28aamw * GENERIC_EXECUTE	STANDARD_RIGHTS_EXECUTE, SYNCHRONIZE, and FILE_EXECUTE.
59da6c28aamw */
60a90cf9fGordon Rossstatic uint32_t
61da6c28aamwsmb_access_generic_to_file(uint32_t desired_access)
62da6c28aamw{
63a90cf9fGordon Ross	uint32_t access = 0;
64da6c28aamw
65da6c28aamw	if (desired_access & GENERIC_ALL)
66da6c28aamw		return (FILE_ALL_ACCESS & ~SYNCHRONIZE);
67da6c28aamw
68da6c28aamw	if (desired_access & GENERIC_EXECUTE) {
69da6c28aamw		desired_access &= ~GENERIC_EXECUTE;
70da6c28aamw		access |= (STANDARD_RIGHTS_EXECUTE |
71da6c28aamw		    SYNCHRONIZE | FILE_EXECUTE);
72da6c28aamw	}
73da6c28aamw
74da6c28aamw	if (desired_access & GENERIC_WRITE) {
75da6c28aamw		desired_access &= ~GENERIC_WRITE;
76da6c28aamw		access |= (FILE_GENERIC_WRITE & ~SYNCHRONIZE);
77da6c28aamw	}
78da6c28aamw
79da6c28aamw	if (desired_access & GENERIC_READ) {
80da6c28aamw		desired_access &= ~GENERIC_READ;
81da6c28aamw		access |= FILE_GENERIC_READ;
82da6c28aamw	}
83da6c28aamw
84da6c28aamw	return (access | desired_access);
85da6c28aamw}
86da6c28aamw
87da6c28aamw/*
88da6c28aamw * smb_omode_to_amask
89da6c28aamw *
90da6c28aamw * This function converts open modes used by Open and Open AndX
91da6c28aamw * commands to desired access bits used by NT Create AndX command.
92da6c28aamw */
93da6c28aamwuint32_t
94da6c28aamwsmb_omode_to_amask(uint32_t desired_access)
95da6c28aamw{
96da6c28aamw	switch (desired_access & SMB_DA_ACCESS_MASK) {
97da6c28aamw	case SMB_DA_ACCESS_READ:
98da6c28aamw		return (FILE_GENERIC_READ);
99da6c28aamw
100da6c28aamw	case SMB_DA_ACCESS_WRITE:
101da6c28aamw		return (FILE_GENERIC_WRITE);
102da6c28aamw
103da6c28aamw	case SMB_DA_ACCESS_READ_WRITE:
104da6c28aamw		return (FILE_GENERIC_READ | FILE_GENERIC_WRITE);
105da6c28aamw
106da6c28aamw	case SMB_DA_ACCESS_EXECUTE:
107c5f48faGordon Ross		return (FILE_GENERIC_READ | FILE_GENERIC_EXECUTE);
108da6c28aamw
1092c2961fjose borrego	default:
1102c2961fjose borrego		return (FILE_GENERIC_ALL);
1112c2961fjose borrego	}
112da6c28aamw}
113da6c28aamw
114da6c28aamw/*
115da6c28aamw * smb_denymode_to_sharemode
116da6c28aamw *
117da6c28aamw * This function converts deny modes used by Open and Open AndX
118da6c28aamw * commands to share access bits used by NT Create AndX command.
119da6c28aamw */
120da6c28aamwuint32_t
121da6c28aamwsmb_denymode_to_sharemode(uint32_t desired_access, char *fname)
122da6c28aamw{
123da6c28aamw	switch (desired_access & SMB_DA_SHARE_MASK) {
124da6c28aamw	case SMB_DA_SHARE_COMPATIBILITY:
125da6c28aamw		if (smb_is_executable(fname))
126da6c28aamw			return (FILE_SHARE_READ | FILE_SHARE_WRITE);
127c8ec8eejose borrego
128c8ec8eejose borrego		return (FILE_SHARE_ALL);
129da6c28aamw
130da6c28aamw	case SMB_DA_SHARE_EXCLUSIVE:
131da6c28aamw		return (FILE_SHARE_NONE);
132da6c28aamw
133da6c28aamw	case SMB_DA_SHARE_DENY_WRITE:
134da6c28aamw		return (FILE_SHARE_READ);
135da6c28aamw
136da6c28aamw	case SMB_DA_SHARE_DENY_READ:
137da6c28aamw		return (FILE_SHARE_WRITE);
138da6c28aamw
139da6c28aamw	case SMB_DA_SHARE_DENY_NONE:
1402c2961fjose borrego	default:
141da6c28aamw		return (FILE_SHARE_READ | FILE_SHARE_WRITE);
142da6c28aamw	}
143da6c28aamw}
144da6c28aamw
145da6c28aamw/*
146da6c28aamw * smb_ofun_to_crdisposition
147da6c28aamw *
148da6c28aamw * This function converts open function values used by Open and Open AndX
149da6c28aamw * commands to create disposition values used by NT Create AndX command.
150da6c28aamw */
151da6c28aamwuint32_t
152da6c28aamwsmb_ofun_to_crdisposition(uint16_t  ofun)
153da6c28aamw{
154da6c28aamw	static int ofun_cr_map[3][2] =
155da6c28aamw	{
156da6c28aamw		{ -1,			FILE_CREATE },
157da6c28aamw		{ FILE_OPEN,		FILE_OPEN_IF },
158da6c28aamw		{ FILE_OVERWRITE,	FILE_OVERWRITE_IF }
159da6c28aamw	};
160da6c28aamw
161da6c28aamw	int row = ofun & SMB_OFUN_OPEN_MASK;
162da6c28aamw	int col = (ofun & SMB_OFUN_CREATE_MASK) >> 4;
163da6c28aamw
164da6c28aamw	if (row == 3)
1652c2961fjose borrego		return (FILE_MAXIMUM_DISPOSITION + 1);
166da6c28aamw
167da6c28aamw	return (ofun_cr_map[row][col]);
168da6c28aamw}
169da6c28aamw
170da6c28aamw/*
17194047d4Gordon Ross * smb_common_open
172da6c28aamw *
173da6c28aamw * Notes on write-through behaviour. It looks like pre-LM0.12 versions
174da6c28aamw * of the protocol specify the write-through mode when a file is opened,
175da6c28aamw * (SmbOpen, SmbOpenAndX) so the write calls (SmbWrite, SmbWriteAndClose,
176