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