s4-auth: Make sure error_string is correctly initialized
[Samba.git] / source3 / smbd / smb2_setinfo.c
blob9361aeaf9177bebdbf4885086807df3243911644
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"
28 #include "../librpc/gen_ndr/open_files.h"
29 #include "source3/lib/dbwrap/dbwrap_watch.h"
30 #include "messages.h"
32 static struct tevent_req *smbd_smb2_setinfo_send(TALLOC_CTX *mem_ctx,
33 struct tevent_context *ev,
34 struct smbd_smb2_request *smb2req,
35 struct files_struct *in_fsp,
36 uint8_t in_info_type,
37 uint8_t in_file_info_class,
38 DATA_BLOB in_input_buffer,
39 uint32_t in_additional_information);
40 static NTSTATUS smbd_smb2_setinfo_recv(struct tevent_req *req);
42 static void smbd_smb2_request_setinfo_done(struct tevent_req *subreq);
43 NTSTATUS smbd_smb2_request_process_setinfo(struct smbd_smb2_request *req)
45 struct smbXsrv_connection *xconn = req->xconn;
46 NTSTATUS status;
47 const uint8_t *inbody;
48 uint8_t in_info_type;
49 uint8_t in_file_info_class;
50 uint16_t in_input_buffer_offset;
51 uint32_t in_input_buffer_length;
52 DATA_BLOB in_input_buffer;
53 uint32_t in_additional_information;
54 uint64_t in_file_id_persistent;
55 uint64_t in_file_id_volatile;
56 struct files_struct *in_fsp;
57 struct tevent_req *subreq;
59 status = smbd_smb2_request_verify_sizes(req, 0x21);
60 if (!NT_STATUS_IS_OK(status)) {
61 return smbd_smb2_request_error(req, status);
63 inbody = SMBD_SMB2_IN_BODY_PTR(req);
65 in_info_type = CVAL(inbody, 0x02);
66 in_file_info_class = CVAL(inbody, 0x03);
67 in_input_buffer_length = IVAL(inbody, 0x04);
68 in_input_buffer_offset = SVAL(inbody, 0x08);
69 /* 0x0A 2 bytes reserved */
70 in_additional_information = IVAL(inbody, 0x0C);
71 in_file_id_persistent = BVAL(inbody, 0x10);
72 in_file_id_volatile = BVAL(inbody, 0x18);
74 if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
75 /* This is ok */
76 } else if (in_input_buffer_offset !=
77 (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
78 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
81 if (in_input_buffer_length > SMBD_SMB2_IN_DYN_LEN(req)) {
82 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
85 in_input_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
86 in_input_buffer.length = in_input_buffer_length;
88 if (in_input_buffer.length > xconn->smb2.server.max_trans) {
89 DEBUG(2,("smbd_smb2_request_process_setinfo: "
90 "client ignored max trans: %s: 0x%08X: 0x%08X\n",
91 __location__, (unsigned)in_input_buffer.length,
92 (unsigned)xconn->smb2.server.max_trans));
93 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
96 status = smbd_smb2_request_verify_creditcharge(req,
97 in_input_buffer.length);
98 if (!NT_STATUS_IS_OK(status)) {
99 return smbd_smb2_request_error(req, status);
102 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
103 if (in_fsp == NULL) {
104 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
107 subreq = smbd_smb2_setinfo_send(req, req->sconn->ev_ctx,
108 req, in_fsp,
109 in_info_type,
110 in_file_info_class,
111 in_input_buffer,
112 in_additional_information);
113 if (subreq == NULL) {
114 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
116 tevent_req_set_callback(subreq, smbd_smb2_request_setinfo_done, req);
118 return smbd_smb2_request_pending_queue(req, subreq, 500);
121 static void smbd_smb2_request_setinfo_done(struct tevent_req *subreq)
123 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
124 struct smbd_smb2_request);
125 DATA_BLOB outbody;
126 NTSTATUS status;
127 NTSTATUS error; /* transport error */
129 status = smbd_smb2_setinfo_recv(subreq);
130 TALLOC_FREE(subreq);
131 if (!NT_STATUS_IS_OK(status)) {
132 error = smbd_smb2_request_error(req, status);
133 if (!NT_STATUS_IS_OK(error)) {
134 smbd_server_connection_terminate(req->xconn,
135 nt_errstr(error));
136 return;
138 return;
141 outbody = smbd_smb2_generate_outbody(req, 0x02);
142 if (outbody.data == NULL) {
143 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
144 if (!NT_STATUS_IS_OK(error)) {
145 smbd_server_connection_terminate(req->xconn,
146 nt_errstr(error));
147 return;
149 return;
152 SSVAL(outbody.data, 0x00, 0x02); /* struct size */
154 error = smbd_smb2_request_done(req, outbody, NULL);
155 if (!NT_STATUS_IS_OK(error)) {
156 smbd_server_connection_terminate(req->xconn,
157 nt_errstr(error));
158 return;
162 struct defer_rename_state {
163 struct tevent_req *req;
164 struct smbd_smb2_request *smb2req;
165 struct tevent_context *ev;
166 struct files_struct *fsp;
167 char *data;
168 int data_size;
171 static int defer_rename_state_destructor(struct defer_rename_state *rename_state)
173 SAFE_FREE(rename_state->data);
174 return 0;
177 static void defer_rename_done(struct tevent_req *subreq);
179 static struct tevent_req *delay_rename_for_lease_break(struct tevent_req *req,
180 struct smbd_smb2_request *smb2req,
181 struct tevent_context *ev,
182 struct files_struct *fsp,
183 struct share_mode_lock *lck,
184 char *data,
185 int data_size)
188 struct tevent_req *subreq;
189 uint32_t i;
190 struct share_mode_data *d = lck->data;
191 struct defer_rename_state *rename_state;
192 bool delay = false;
193 struct timeval timeout;
195 if (fsp->oplock_type != LEASE_OPLOCK) {
196 return NULL;
199 for (i=0; i<d->num_share_modes; i++) {
200 struct share_mode_entry *e = &d->share_modes[i];
201 struct share_mode_lease *l = NULL;
202 uint32_t e_lease_type = get_lease_type(d, e);
203 uint32_t break_to;
205 if (e->op_type != LEASE_OPLOCK) {
206 continue;
209 l = &d->leases[e->lease_idx];
211 if (smb2_lease_equal(fsp_client_guid(fsp),
212 &fsp->lease->lease.lease_key,
213 &l->client_guid,
214 &l->lease_key)) {
215 continue;
218 if (share_mode_stale_pid(d, i)) {
219 continue;
222 if (!(e_lease_type & SMB2_LEASE_HANDLE)) {
223 continue;
226 delay = true;
227 break_to = (e_lease_type & ~SMB2_LEASE_HANDLE);
229 send_break_message(fsp->conn->sconn->msg_ctx, e, break_to);
232 if (!delay) {
233 return NULL;
236 /* Setup a watch on this record. */
237 rename_state = talloc_zero(req, struct defer_rename_state);
238 if (rename_state == NULL) {
239 return NULL;
242 rename_state->req = req;
243 rename_state->smb2req = smb2req;
244 rename_state->ev = ev;
245 rename_state->fsp = fsp;
246 rename_state->data = data;
247 rename_state->data_size = data_size;
249 talloc_set_destructor(rename_state, defer_rename_state_destructor);
251 subreq = dbwrap_record_watch_send(
252 rename_state,
254 lck->data->record,
255 fsp->conn->sconn->msg_ctx);
257 if (subreq == NULL) {
258 exit_server("Could not watch share mode record for rename\n");
261 tevent_req_set_callback(subreq, defer_rename_done, rename_state);
263 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
264 if (!tevent_req_set_endtime(subreq,
266 timeval_sum(&smb2req->request_time, &timeout))) {
267 exit_server("Could not set rename timeout\n");
270 return subreq;
273 static void defer_rename_done(struct tevent_req *subreq)
275 struct defer_rename_state *state = tevent_req_callback_data(
276 subreq, struct defer_rename_state);
277 NTSTATUS status;
278 struct share_mode_lock *lck;
279 int ret_size = 0;
280 bool ok;
282 status = dbwrap_record_watch_recv(subreq, state->req, NULL);
283 TALLOC_FREE(subreq);
284 if (!NT_STATUS_IS_OK(status)) {
285 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
286 nt_errstr(status)));
287 tevent_req_nterror(state->req, status);
288 return;
292 * Make sure we run as the user again
294 ok = change_to_user(state->smb2req->tcon->compat,
295 state->smb2req->session->compat->vuid);
296 if (!ok) {
297 tevent_req_nterror(state->req, NT_STATUS_ACCESS_DENIED);
298 return;
301 /* should we pass FLAG_CASELESS_PATHNAMES here? */
302 ok = set_current_service(state->smb2req->tcon->compat, 0, true);
303 if (!ok) {
304 tevent_req_nterror(state->req, NT_STATUS_ACCESS_DENIED);
305 return;
308 /* Do we still need to wait ? */
309 lck = get_existing_share_mode_lock(state->req, state->fsp->file_id);
310 if (lck == NULL) {
311 tevent_req_nterror(state->req, NT_STATUS_UNSUCCESSFUL);
312 return;
314 subreq = delay_rename_for_lease_break(state->req,
315 state->smb2req,
316 state->ev,
317 state->fsp,
318 lck,
319 state->data,
320 state->data_size);
321 if (subreq) {
322 /* Yep - keep waiting. */
323 state->data = NULL;
324 TALLOC_FREE(state);
325 TALLOC_FREE(lck);
326 return;
329 /* Do the rename under the lock. */
330 status = smbd_do_setfilepathinfo(state->fsp->conn,
331 state->smb2req->smb1req,
332 state,
333 SMB2_FILE_RENAME_INFORMATION_INTERNAL,
334 state->fsp,
335 state->fsp->fsp_name,
336 &state->data,
337 state->data_size,
338 &ret_size);
340 TALLOC_FREE(lck);
341 SAFE_FREE(state->data);
343 if (!NT_STATUS_IS_OK(status)) {
344 tevent_req_nterror(state->req, status);
345 return;
348 tevent_req_done(state->req);
351 struct smbd_smb2_setinfo_state {
352 struct smbd_smb2_request *smb2req;
355 static struct tevent_req *smbd_smb2_setinfo_send(TALLOC_CTX *mem_ctx,
356 struct tevent_context *ev,
357 struct smbd_smb2_request *smb2req,
358 struct files_struct *fsp,
359 uint8_t in_info_type,
360 uint8_t in_file_info_class,
361 DATA_BLOB in_input_buffer,
362 uint32_t in_additional_information)
364 struct tevent_req *req = NULL;
365 struct smbd_smb2_setinfo_state *state = NULL;
366 struct smb_request *smbreq = NULL;
367 connection_struct *conn = smb2req->tcon->compat;
368 struct share_mode_lock *lck = NULL;
369 NTSTATUS status;
371 req = tevent_req_create(mem_ctx, &state,
372 struct smbd_smb2_setinfo_state);
373 if (req == NULL) {
374 return NULL;
376 state->smb2req = smb2req;
378 DEBUG(10,("smbd_smb2_setinfo_send: %s - %s\n",
379 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
381 smbreq = smbd_smb2_fake_smb_request(smb2req);
382 if (tevent_req_nomem(smbreq, req)) {
383 return tevent_req_post(req, ev);
386 if (IS_IPC(conn)) {
387 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
388 return tevent_req_post(req, ev);
391 switch (in_info_type) {
392 case 0x01:/* SMB2_SETINFO_FILE */
394 uint16_t file_info_level;
395 char *data;
396 int data_size;
397 int ret_size = 0;
400 file_info_level = in_file_info_class + 1000;
401 if (file_info_level == SMB_FILE_RENAME_INFORMATION) {
402 /* SMB2_FILE_RENAME_INFORMATION_INTERNAL == 0xFF00 + in_file_info_class */
403 file_info_level = SMB2_FILE_RENAME_INFORMATION_INTERNAL;
406 if (fsp->fh->fd == -1) {
408 * This is actually a SETFILEINFO on a directory
409 * handle (returned from an NT SMB). NT5.0 seems
410 * to do this call. JRA.
412 if (INFO_LEVEL_IS_UNIX(file_info_level)) {
413 /* Always do lstat for UNIX calls. */
414 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
415 DEBUG(3,("smbd_smb2_setinfo_send: "
416 "SMB_VFS_LSTAT of %s failed "
417 "(%s)\n", fsp_str_dbg(fsp),
418 strerror(errno)));
419 status = map_nt_error_from_unix(errno);
420 tevent_req_nterror(req, status);
421 return tevent_req_post(req, ev);
423 } else {
424 if (SMB_VFS_STAT(conn, fsp->fsp_name) != 0) {
425 DEBUG(3,("smbd_smb2_setinfo_send: "
426 "fileinfo of %s failed (%s)\n",
427 fsp_str_dbg(fsp),
428 strerror(errno)));
429 status = map_nt_error_from_unix(errno);
430 tevent_req_nterror(req, status);
431 return tevent_req_post(req, ev);
434 } else if (fsp->print_file) {
436 * Doing a DELETE_ON_CLOSE should cancel a print job.
438 if ((file_info_level == SMB_SET_FILE_DISPOSITION_INFO)
439 && in_input_buffer.length >= 1
440 && CVAL(in_input_buffer.data,0)) {
441 fsp->fh->private_options |= NTCREATEX_OPTIONS_PRIVATE_DELETE_ON_CLOSE;
443 DEBUG(3,("smbd_smb2_setinfo_send: "
444 "Cancelling print job (%s)\n",
445 fsp_str_dbg(fsp)));
447 tevent_req_done(req);
448 return tevent_req_post(req, ev);
449 } else {
450 tevent_req_nterror(req,
451 NT_STATUS_OBJECT_PATH_INVALID);
452 return tevent_req_post(req, ev);
454 } else {
456 * Original code - this is an open file.
459 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
460 DEBUG(3,("smbd_smb2_setinfo_send: fstat "
461 "of %s failed (%s)\n",
462 fsp_fnum_dbg(fsp),
463 strerror(errno)));
464 status = map_nt_error_from_unix(errno);
465 tevent_req_nterror(req, status);
466 return tevent_req_post(req, ev);
470 data = NULL;
471 data_size = in_input_buffer.length;
472 if (data_size > 0) {
473 data = (char *)SMB_MALLOC_ARRAY(char, data_size);
474 if (tevent_req_nomem(data, req)) {
475 return tevent_req_post(req, ev);
477 memcpy(data, in_input_buffer.data, data_size);
480 if (file_info_level == SMB2_FILE_RENAME_INFORMATION_INTERNAL) {
481 struct tevent_req *subreq;
483 lck = get_existing_share_mode_lock(mem_ctx,
484 fsp->file_id);
485 if (lck == NULL) {
486 SAFE_FREE(data);
487 tevent_req_nterror(req,
488 NT_STATUS_UNSUCCESSFUL);
489 return tevent_req_post(req, ev);
492 subreq = delay_rename_for_lease_break(req,
493 smb2req,
495 fsp,
496 lck,
497 data,
498 data_size);
499 if (subreq) {
500 /* Wait for lease break response. */
502 /* Ensure we can't be closed in flight. */
503 if (!aio_add_req_to_fsp(fsp, req)) {
504 TALLOC_FREE(lck);
505 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
506 return tevent_req_post(req, ev);
509 TALLOC_FREE(lck);
510 return req;
514 status = smbd_do_setfilepathinfo(conn, smbreq, state,
515 file_info_level,
516 fsp,
517 fsp->fsp_name,
518 &data,
519 data_size,
520 &ret_size);
521 TALLOC_FREE(lck);
522 SAFE_FREE(data);
523 if (!NT_STATUS_IS_OK(status)) {
524 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
525 status = NT_STATUS_INVALID_INFO_CLASS;
527 tevent_req_nterror(req, status);
528 return tevent_req_post(req, ev);
530 break;
533 case 0x03:/* SMB2_SETINFO_SECURITY */
535 if (!CAN_WRITE(conn)) {
536 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
537 return tevent_req_post(req, ev);
540 status = set_sd_blob(fsp,
541 in_input_buffer.data,
542 in_input_buffer.length,
543 in_additional_information &
544 SMB_SUPPORTED_SECINFO_FLAGS);
545 if (!NT_STATUS_IS_OK(status)) {
546 tevent_req_nterror(req, status);
547 return tevent_req_post(req, ev);
549 break;
552 default:
553 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
554 return tevent_req_post(req, ev);
557 tevent_req_done(req);
558 return tevent_req_post(req, ev);
561 static NTSTATUS smbd_smb2_setinfo_recv(struct tevent_req *req)
563 NTSTATUS status;
565 if (tevent_req_is_nterror(req, &status)) {
566 tevent_req_received(req);
567 return status;
570 tevent_req_received(req);
571 return NT_STATUS_OK;