xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_create.c (revision e688a0bc223983e7c7fedad44c92b8d0292a10f7)
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	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <smbsrv/smb_incl.h>
29 
30 #define	SMB_CREATE_NAMEBUF_SZ	16
31 
32 static uint32_t smb_common_create(smb_request_t *sr);
33 
34 /*
35  * Create a new file, or truncate an existing file to zero length,
36  * open the file and return a fid.  The file is specified using a
37  * fully qualified name relative to the tree.
38  */
39 smb_sdrc_t
40 smb_pre_create(smb_request_t *sr)
41 {
42 	struct open_param *op = &sr->arg.open;
43 	int rc;
44 
45 	bzero(op, sizeof (sr->arg.open));
46 
47 	rc = smbsr_decode_vwv(sr, "wl", &op->dattr, &op->utime.tv_sec);
48 	if (rc == 0)
49 		rc = smbsr_decode_data(sr, "%S", sr, &op->fqi.path);
50 
51 	op->create_disposition = FILE_OVERWRITE_IF;
52 
53 	DTRACE_SMB_2(op__Create__start, smb_request_t *, sr,
54 	    struct open_param *, op);
55 
56 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
57 }
58 
59 void
60 smb_post_create(smb_request_t *sr)
61 {
62 	DTRACE_SMB_1(op__Create__done, smb_request_t *, sr);
63 }
64 
65 smb_sdrc_t
66 smb_com_create(smb_request_t *sr)
67 {
68 	if (smb_common_create(sr) != NT_STATUS_SUCCESS)
69 		return (SDRC_ERROR);
70 
71 	if (smbsr_encode_result(sr, 1, 0, "bww", 1, sr->smb_fid, 0))
72 		return (SDRC_ERROR);
73 
74 	return (SDRC_SUCCESS);
75 }
76 
77 /*
78  * Create a new file and return a fid.  The file is specified using
79  * a fully qualified name relative to the tree.
80  */
81 smb_sdrc_t
82 smb_pre_create_new(smb_request_t *sr)
83 {
84 	struct open_param *op = &sr->arg.open;
85 	int rc;
86 
87 	bzero(op, sizeof (sr->arg.open));
88 
89 	rc = smbsr_decode_vwv(sr, "wl", &op->dattr, &op->utime.tv_sec);
90 	if (rc == 0)
91 		rc = smbsr_decode_data(sr, "%S", sr, &op->fqi.path);
92 
93 	op->create_disposition = FILE_CREATE;
94 
95 	DTRACE_SMB_2(op__CreateNew__start, smb_request_t *, sr,
96 	    struct open_param *, op);
97 
98 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
99 }
100 
101 void
102 smb_post_create_new(smb_request_t *sr)
103 {
104 	DTRACE_SMB_1(op__CreateNew__done, smb_request_t *, sr);
105 }
106 
107 smb_sdrc_t
108 smb_com_create_new(smb_request_t *sr)
109 {
110 	if (smb_common_create(sr) != NT_STATUS_SUCCESS)
111 		return (SDRC_ERROR);
112 
113 	if (smbsr_encode_result(sr, 1, 0, "bww", 1, sr->smb_fid, 0))
114 		return (SDRC_ERROR);
115 
116 	return (SDRC_SUCCESS);
117 }
118 
119 /*
120  * Create a unique file in the specified directory relative to the
121  * current tree.  No attributes are specified.
122  */
123 smb_sdrc_t
124 smb_pre_create_temporary(smb_request_t *sr)
125 {
126 	struct open_param *op = &sr->arg.open;
127 	uint16_t reserved;
128 	int rc;
129 
130 	bzero(op, sizeof (sr->arg.open));
131 
132 	rc = smbsr_decode_vwv(sr, "wl", &reserved, &op->utime.tv_sec);
133 	if (rc == 0)
134 		rc = smbsr_decode_data(sr, "%S", sr, &op->fqi.path);
135 
136 	op->create_disposition = FILE_CREATE;
137 
138 	DTRACE_SMB_2(op__CreateTemporary__start, smb_request_t *, sr,
139 	    struct open_param *, op);
140 
141 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
142 }
143 
144 void
145 smb_post_create_temporary(smb_request_t *sr)
146 {
147 	DTRACE_SMB_1(op__CreateTemporary__done, smb_request_t *, sr);
148 }
149 
150 smb_sdrc_t
151 smb_com_create_temporary(smb_request_t *sr)
152 {
153 	static uint16_t tmp_id = 10000;
154 	struct open_param *op = &sr->arg.open;
155 	char name[SMB_CREATE_NAMEBUF_SZ];
156 	char *buf;
157 	uint16_t bcc;
158 
159 	++tmp_id;
160 	bcc = 1; /* null terminator */
161 	bcc += snprintf(name, SMB_CREATE_NAMEBUF_SZ, "tt%05d.tmp", tmp_id);
162 
163 	buf = smbsr_malloc(&sr->request_storage, MAXPATHLEN);
164 	(void) snprintf(buf, MAXPATHLEN, "%s\\%s", op->fqi.path, name);
165 	op->fqi.path = buf;
166 
167 	if (smb_common_create(sr) != NT_STATUS_SUCCESS)
168 		return (SDRC_ERROR);
169 
170 	if (smbsr_encode_result(sr, 1, VAR_BCC, "bww%S", 1, sr->smb_fid,
171 	    VAR_BCC, sr, name))
172 		return (SDRC_ERROR);
173 
174 	return (SDRC_SUCCESS);
175 }
176 
177 /*
178  * Common create file function.  The file is opened in compatibility
179  * mode with read/write access.
180  */
181 uint32_t
182 smb_common_create(smb_request_t *sr)
183 {
184 	struct open_param *op = &sr->arg.open;
185 	uint32_t status;
186 
187 	op->utime.tv_sec = smb_local2gmt(sr, op->utime.tv_sec);
188 	op->utime.tv_nsec = 0;
189 	op->omode = SMB_DA_ACCESS_READ_WRITE | SMB_DA_SHARE_COMPATIBILITY;
190 	op->desired_access = smb_omode_to_amask(op->omode);
191 	op->share_access = smb_denymode_to_sharemode(op->omode, op->fqi.path);
192 
193 	if ((op->desired_access == ((uint32_t)SMB_INVALID_AMASK)) ||
194 	    (op->share_access == ((uint32_t)SMB_INVALID_SHAREMODE))) {
195 		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER,
196 		    ERRDOS, ERROR_INVALID_PARAMETER);
197 		return (NT_STATUS_INVALID_PARAMETER);
198 	}
199 
200 	op->dsize = 0;
201 
202 	if (sr->smb_flg & SMB_FLAGS_OPLOCK) {
203 		if (sr->smb_flg & SMB_FLAGS_OPLOCK_NOTIFY_ANY) {
204 			op->my_flags = MYF_BATCH_OPLOCK;
205 		} else {
206 			op->my_flags = MYF_EXCLUSIVE_OPLOCK;
207 		}
208 	}
209 
210 	status = smb_common_open(sr);
211 
212 	if (MYF_OPLOCK_TYPE(op->my_flags) == MYF_OPLOCK_NONE) {
213 		sr->smb_flg &=
214 		    ~(SMB_FLAGS_OPLOCK | SMB_FLAGS_OPLOCK_NOTIFY_ANY);
215 	}
216 
217 	return (status);
218 }
219