s3:smb2_setinfo: make use of smbd_smb2_generate_outbody()
[Samba.git] / source3 / smbd / smb2_setinfo.c
blob97872dae4477ad14b695eb883c03823968bad2f4
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) Jeremy Allison 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "trans2.h"
27 #include "../lib/util/tevent_ntstatus.h"
29 static struct tevent_req *smbd_smb2_setinfo_send(TALLOC_CTX *mem_ctx,
30 struct tevent_context *ev,
31 struct smbd_smb2_request *smb2req,
32 struct files_struct *in_fsp,
33 uint8_t in_info_type,
34 uint8_t in_file_info_class,
35 DATA_BLOB in_input_buffer,
36 uint32_t in_additional_information);
37 static NTSTATUS smbd_smb2_setinfo_recv(struct tevent_req *req);
39 static void smbd_smb2_request_setinfo_done(struct tevent_req *subreq);
40 NTSTATUS smbd_smb2_request_process_setinfo(struct smbd_smb2_request *req)
42 NTSTATUS status;
43 const uint8_t *inbody;
44 uint8_t in_info_type;
45 uint8_t in_file_info_class;
46 uint16_t in_input_buffer_offset;
47 uint32_t in_input_buffer_length;
48 DATA_BLOB in_input_buffer;
49 uint32_t in_additional_information;
50 uint64_t in_file_id_persistent;
51 uint64_t in_file_id_volatile;
52 struct files_struct *in_fsp;
53 struct tevent_req *subreq;
55 status = smbd_smb2_request_verify_sizes(req, 0x21);
56 if (!NT_STATUS_IS_OK(status)) {
57 return smbd_smb2_request_error(req, status);
59 inbody = SMBD_SMB2_IN_BODY_PTR(req);
61 in_info_type = CVAL(inbody, 0x02);
62 in_file_info_class = CVAL(inbody, 0x03);
63 in_input_buffer_length = IVAL(inbody, 0x04);
64 in_input_buffer_offset = SVAL(inbody, 0x08);
65 /* 0x0A 2 bytes reserved */
66 in_additional_information = IVAL(inbody, 0x0C);
67 in_file_id_persistent = BVAL(inbody, 0x10);
68 in_file_id_volatile = BVAL(inbody, 0x18);
70 if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
71 /* This is ok */
72 } else if (in_input_buffer_offset !=
73 (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
74 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
77 if (in_input_buffer_length > SMBD_SMB2_IN_DYN_LEN(req)) {
78 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
81 in_input_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
82 in_input_buffer.length = in_input_buffer_length;
84 if (in_input_buffer.length > req->sconn->smb2.max_trans) {
85 DEBUG(2,("smbd_smb2_request_process_setinfo: "
86 "client ignored max trans: %s: 0x%08X: 0x%08X\n",
87 __location__, (unsigned)in_input_buffer.length,
88 (unsigned)req->sconn->smb2.max_trans));
89 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
92 status = smbd_smb2_request_verify_creditcharge(req,
93 in_input_buffer.length);
94 if (!NT_STATUS_IS_OK(status)) {
95 return smbd_smb2_request_error(req, status);
98 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
99 if (in_fsp == NULL) {
100 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
103 subreq = smbd_smb2_setinfo_send(req, req->sconn->ev_ctx,
104 req, in_fsp,
105 in_info_type,
106 in_file_info_class,
107 in_input_buffer,
108 in_additional_information);
109 if (subreq == NULL) {
110 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
112 tevent_req_set_callback(subreq, smbd_smb2_request_setinfo_done, req);
114 return smbd_smb2_request_pending_queue(req, subreq, 500);
117 static void smbd_smb2_request_setinfo_done(struct tevent_req *subreq)
119 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
120 struct smbd_smb2_request);
121 DATA_BLOB outbody;
122 NTSTATUS status;
123 NTSTATUS error; /* transport error */
125 status = smbd_smb2_setinfo_recv(subreq);
126 TALLOC_FREE(subreq);
127 if (!NT_STATUS_IS_OK(status)) {
128 error = smbd_smb2_request_error(req, status);
129 if (!NT_STATUS_IS_OK(error)) {
130 smbd_server_connection_terminate(req->sconn,
131 nt_errstr(error));
132 return;
134 return;
137 outbody = smbd_smb2_generate_outbody(req, 0x02);
138 if (outbody.data == NULL) {
139 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
140 if (!NT_STATUS_IS_OK(error)) {
141 smbd_server_connection_terminate(req->sconn,
142 nt_errstr(error));
143 return;
145 return;
148 SSVAL(outbody.data, 0x00, 0x02); /* struct size */
150 error = smbd_smb2_request_done(req, outbody, NULL);
151 if (!NT_STATUS_IS_OK(error)) {
152 smbd_server_connection_terminate(req->sconn,
153 nt_errstr(error));
154 return;
158 struct smbd_smb2_setinfo_state {
159 struct smbd_smb2_request *smb2req;
162 static struct tevent_req *smbd_smb2_setinfo_send(TALLOC_CTX *mem_ctx,
163 struct tevent_context *ev,
164 struct smbd_smb2_request *smb2req,
165 struct files_struct *fsp,
166 uint8_t in_info_type,
167 uint8_t in_file_info_class,
168 DATA_BLOB in_input_buffer,
169 uint32_t in_additional_information)
171 struct tevent_req *req = NULL;
172 struct smbd_smb2_setinfo_state *state = NULL;
173 struct smb_request *smbreq = NULL;
174 connection_struct *conn = smb2req->tcon->compat;
175 NTSTATUS status;
177 req = tevent_req_create(mem_ctx, &state,
178 struct smbd_smb2_setinfo_state);
179 if (req == NULL) {
180 return NULL;
182 state->smb2req = smb2req;
184 DEBUG(10,("smbd_smb2_setinfo_send: %s - %s\n",
185 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
187 smbreq = smbd_smb2_fake_smb_request(smb2req);
188 if (tevent_req_nomem(smbreq, req)) {
189 return tevent_req_post(req, ev);
192 if (IS_IPC(conn)) {
193 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
194 return tevent_req_post(req, ev);
197 switch (in_info_type) {
198 case 0x01:/* SMB2_SETINFO_FILE */
200 uint16_t file_info_level;
201 char *data;
202 int data_size;
203 int ret_size = 0;
206 file_info_level = in_file_info_class + 1000;
207 if (file_info_level == SMB_FILE_RENAME_INFORMATION) {
208 /* SMB2_FILE_RENAME_INFORMATION_INTERNAL == 0xFF00 + in_file_info_class */
209 file_info_level = SMB2_FILE_RENAME_INFORMATION_INTERNAL;
212 if (fsp->fh->fd == -1) {
214 * This is actually a SETFILEINFO on a directory
215 * handle (returned from an NT SMB). NT5.0 seems
216 * to do this call. JRA.
218 if (INFO_LEVEL_IS_UNIX(file_info_level)) {
219 /* Always do lstat for UNIX calls. */
220 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
221 DEBUG(3,("smbd_smb2_setinfo_send: "
222 "SMB_VFS_LSTAT of %s failed "
223 "(%s)\n", fsp_str_dbg(fsp),
224 strerror(errno)));
225 status = map_nt_error_from_unix(errno);
226 tevent_req_nterror(req, status);
227 return tevent_req_post(req, ev);
229 } else {
230 if (SMB_VFS_STAT(conn, fsp->fsp_name) != 0) {
231 DEBUG(3,("smbd_smb2_setinfo_send: "
232 "fileinfo of %s failed (%s)\n",
233 fsp_str_dbg(fsp),
234 strerror(errno)));
235 status = map_nt_error_from_unix(errno);
236 tevent_req_nterror(req, status);
237 return tevent_req_post(req, ev);
240 } else if (fsp->print_file) {
242 * Doing a DELETE_ON_CLOSE should cancel a print job.
244 if ((file_info_level == SMB_SET_FILE_DISPOSITION_INFO)
245 && in_input_buffer.length >= 1
246 && CVAL(in_input_buffer.data,0)) {
247 fsp->fh->private_options |= NTCREATEX_OPTIONS_PRIVATE_DELETE_ON_CLOSE;
249 DEBUG(3,("smbd_smb2_setinfo_send: "
250 "Cancelling print job (%s)\n",
251 fsp_str_dbg(fsp)));
253 tevent_req_done(req);
254 return tevent_req_post(req, ev);
255 } else {
256 tevent_req_nterror(req,
257 NT_STATUS_OBJECT_PATH_INVALID);
258 return tevent_req_post(req, ev);
260 } else {
262 * Original code - this is an open file.
265 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
266 DEBUG(3,("smbd_smb2_setinfo_send: fstat "
267 "of %s failed (%s)\n",
268 fsp_fnum_dbg(fsp),
269 strerror(errno)));
270 status = map_nt_error_from_unix(errno);
271 tevent_req_nterror(req, status);
272 return tevent_req_post(req, ev);
276 data = NULL;
277 data_size = in_input_buffer.length;
278 if (data_size > 0) {
279 data = (char *)SMB_MALLOC_ARRAY(char, data_size);
280 if (tevent_req_nomem(data, req)) {
281 return tevent_req_post(req, ev);
283 memcpy(data, in_input_buffer.data, data_size);
286 status = smbd_do_setfilepathinfo(conn, smbreq, state,
287 file_info_level,
288 fsp,
289 fsp->fsp_name,
290 &data,
291 data_size,
292 &ret_size);
293 SAFE_FREE(data);
294 if (!NT_STATUS_IS_OK(status)) {
295 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
296 status = NT_STATUS_INVALID_INFO_CLASS;
298 tevent_req_nterror(req, status);
299 return tevent_req_post(req, ev);
301 break;
304 case 0x03:/* SMB2_SETINFO_SECURITY */
306 if (!CAN_WRITE(conn)) {
307 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
308 return tevent_req_post(req, ev);
311 status = set_sd_blob(fsp,
312 in_input_buffer.data,
313 in_input_buffer.length,
314 in_additional_information);
315 if (!NT_STATUS_IS_OK(status)) {
316 tevent_req_nterror(req, status);
317 return tevent_req_post(req, ev);
319 break;
322 default:
323 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
324 return tevent_req_post(req, ev);
327 tevent_req_done(req);
328 return tevent_req_post(req, ev);
331 static NTSTATUS smbd_smb2_setinfo_recv(struct tevent_req *req)
333 NTSTATUS status;
335 if (tevent_req_is_nterror(req, &status)) {
336 tevent_req_received(req);
337 return status;
340 tevent_req_received(req);
341 return NT_STATUS_OK;