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