kcc: Fix typos in original samba_kcc
[Samba.git] / source3 / smbd / smb2_setinfo.c
blob3f7bbec94b549c95be304e7bd4053b433ebd2179
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 void defer_rename_done(struct tevent_req *subreq);
173 static struct tevent_req *delay_rename_for_lease_break(struct tevent_req *req,
174 struct smbd_smb2_request *smb2req,
175 struct tevent_context *ev,
176 struct files_struct *fsp,
177 struct share_mode_lock *lck,
178 char *data,
179 int data_size)
182 struct tevent_req *subreq;
183 uint32_t i;
184 struct share_mode_data *d = lck->data;
185 struct defer_rename_state *rename_state;
186 bool delay = false;
187 struct timeval timeout;
189 if (fsp->oplock_type != LEASE_OPLOCK) {
190 return NULL;
193 for (i=0; i<d->num_share_modes; i++) {
194 struct share_mode_entry *e = &d->share_modes[i];
195 struct share_mode_lease *l = NULL;
196 uint32_t e_lease_type = get_lease_type(d, e);
197 uint32_t break_to;
199 if (e->op_type != LEASE_OPLOCK) {
200 continue;
203 l = &d->leases[e->lease_idx];
205 if (smb2_lease_equal(fsp_client_guid(fsp),
206 &fsp->lease->lease.lease_key,
207 &l->client_guid,
208 &l->lease_key)) {
209 continue;
212 if (share_mode_stale_pid(d, i)) {
213 continue;
216 if (!(e_lease_type & SMB2_LEASE_HANDLE)) {
217 continue;
220 delay = true;
221 break_to = (e_lease_type & ~SMB2_LEASE_HANDLE);
223 send_break_message(fsp->conn->sconn->msg_ctx, e, break_to);
226 if (!delay) {
227 return NULL;
230 /* Setup a watch on this record. */
231 rename_state = talloc_zero(req, struct defer_rename_state);
232 if (rename_state == NULL) {
233 return NULL;
236 rename_state->req = req;
237 rename_state->smb2req = smb2req;
238 rename_state->ev = ev;
239 rename_state->fsp = fsp;
240 rename_state->data = data;
241 rename_state->data_size = data_size;
243 subreq = dbwrap_record_watch_send(
244 rename_state,
246 lck->data->record,
247 fsp->conn->sconn->msg_ctx);
249 if (subreq == NULL) {
250 exit_server("Could not watch share mode record for rename\n");
253 tevent_req_set_callback(subreq, defer_rename_done, rename_state);
255 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
256 if (!tevent_req_set_endtime(subreq,
258 timeval_sum(&smb2req->request_time, &timeout))) {
259 exit_server("Could not set rename timeout\n");
262 return subreq;
265 static void defer_rename_done(struct tevent_req *subreq)
267 struct defer_rename_state *state = tevent_req_callback_data(
268 subreq, struct defer_rename_state);
269 NTSTATUS status;
270 struct share_mode_lock *lck;
271 int ret_size = 0;
272 bool ok;
274 status = dbwrap_record_watch_recv(subreq, state->req, NULL);
275 TALLOC_FREE(subreq);
276 if (!NT_STATUS_IS_OK(status)) {
277 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
278 nt_errstr(status)));
279 tevent_req_nterror(state->req, status);
280 return;
284 * Make sure we run as the user again
286 ok = change_to_user(state->smb2req->tcon->compat,
287 state->smb2req->session->compat->vuid);
288 if (!ok) {
289 tevent_req_nterror(state->req, NT_STATUS_ACCESS_DENIED);
290 return;
293 /* should we pass FLAG_CASELESS_PATHNAMES here? */
294 ok = set_current_service(state->smb2req->tcon->compat, 0, true);
295 if (!ok) {
296 tevent_req_nterror(state->req, NT_STATUS_ACCESS_DENIED);
297 return;
300 /* Do we still need to wait ? */
301 lck = get_existing_share_mode_lock(state->req, state->fsp->file_id);
302 if (lck == NULL) {
303 tevent_req_nterror(state->req, NT_STATUS_UNSUCCESSFUL);
304 return;
306 subreq = delay_rename_for_lease_break(state->req,
307 state->smb2req,
308 state->ev,
309 state->fsp,
310 lck,
311 state->data,
312 state->data_size);
313 if (subreq) {
314 /* Yep - keep waiting. */
315 TALLOC_FREE(state);
316 TALLOC_FREE(lck);
317 return;
320 /* Do the rename under the lock. */
321 status = smbd_do_setfilepathinfo(state->fsp->conn,
322 state->smb2req->smb1req,
323 state,
324 SMB2_FILE_RENAME_INFORMATION_INTERNAL,
325 state->fsp,
326 state->fsp->fsp_name,
327 &state->data,
328 state->data_size,
329 &ret_size);
331 TALLOC_FREE(lck);
332 SAFE_FREE(state->data);
334 if (!NT_STATUS_IS_OK(status)) {
335 tevent_req_nterror(state->req, status);
336 return;
339 tevent_req_done(state->req);
342 struct smbd_smb2_setinfo_state {
343 struct smbd_smb2_request *smb2req;
346 static struct tevent_req *smbd_smb2_setinfo_send(TALLOC_CTX *mem_ctx,
347 struct tevent_context *ev,
348 struct smbd_smb2_request *smb2req,
349 struct files_struct *fsp,
350 uint8_t in_info_type,
351 uint8_t in_file_info_class,
352 DATA_BLOB in_input_buffer,
353 uint32_t in_additional_information)
355 struct tevent_req *req = NULL;
356 struct smbd_smb2_setinfo_state *state = NULL;
357 struct smb_request *smbreq = NULL;
358 connection_struct *conn = smb2req->tcon->compat;
359 struct share_mode_lock *lck = NULL;
360 NTSTATUS status;
362 req = tevent_req_create(mem_ctx, &state,
363 struct smbd_smb2_setinfo_state);
364 if (req == NULL) {
365 return NULL;
367 state->smb2req = smb2req;
369 DEBUG(10,("smbd_smb2_setinfo_send: %s - %s\n",
370 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
372 smbreq = smbd_smb2_fake_smb_request(smb2req);
373 if (tevent_req_nomem(smbreq, req)) {
374 return tevent_req_post(req, ev);
377 if (IS_IPC(conn)) {
378 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
379 return tevent_req_post(req, ev);
382 switch (in_info_type) {
383 case 0x01:/* SMB2_SETINFO_FILE */
385 uint16_t file_info_level;
386 char *data;
387 int data_size;
388 int ret_size = 0;
391 file_info_level = in_file_info_class + 1000;
392 if (file_info_level == SMB_FILE_RENAME_INFORMATION) {
393 /* SMB2_FILE_RENAME_INFORMATION_INTERNAL == 0xFF00 + in_file_info_class */
394 file_info_level = SMB2_FILE_RENAME_INFORMATION_INTERNAL;
397 if (fsp->fh->fd == -1) {
399 * This is actually a SETFILEINFO on a directory
400 * handle (returned from an NT SMB). NT5.0 seems
401 * to do this call. JRA.
403 if (INFO_LEVEL_IS_UNIX(file_info_level)) {
404 /* Always do lstat for UNIX calls. */
405 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
406 DEBUG(3,("smbd_smb2_setinfo_send: "
407 "SMB_VFS_LSTAT of %s failed "
408 "(%s)\n", fsp_str_dbg(fsp),
409 strerror(errno)));
410 status = map_nt_error_from_unix(errno);
411 tevent_req_nterror(req, status);
412 return tevent_req_post(req, ev);
414 } else {
415 if (SMB_VFS_STAT(conn, fsp->fsp_name) != 0) {
416 DEBUG(3,("smbd_smb2_setinfo_send: "
417 "fileinfo of %s failed (%s)\n",
418 fsp_str_dbg(fsp),
419 strerror(errno)));
420 status = map_nt_error_from_unix(errno);
421 tevent_req_nterror(req, status);
422 return tevent_req_post(req, ev);
425 } else if (fsp->print_file) {
427 * Doing a DELETE_ON_CLOSE should cancel a print job.
429 if ((file_info_level == SMB_SET_FILE_DISPOSITION_INFO)
430 && in_input_buffer.length >= 1
431 && CVAL(in_input_buffer.data,0)) {
432 fsp->fh->private_options |= NTCREATEX_OPTIONS_PRIVATE_DELETE_ON_CLOSE;
434 DEBUG(3,("smbd_smb2_setinfo_send: "
435 "Cancelling print job (%s)\n",
436 fsp_str_dbg(fsp)));
438 tevent_req_done(req);
439 return tevent_req_post(req, ev);
440 } else {
441 tevent_req_nterror(req,
442 NT_STATUS_OBJECT_PATH_INVALID);
443 return tevent_req_post(req, ev);
445 } else {
447 * Original code - this is an open file.
450 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
451 DEBUG(3,("smbd_smb2_setinfo_send: fstat "
452 "of %s failed (%s)\n",
453 fsp_fnum_dbg(fsp),
454 strerror(errno)));
455 status = map_nt_error_from_unix(errno);
456 tevent_req_nterror(req, status);
457 return tevent_req_post(req, ev);
461 data = NULL;
462 data_size = in_input_buffer.length;
463 if (data_size > 0) {
464 data = (char *)SMB_MALLOC_ARRAY(char, data_size);
465 if (tevent_req_nomem(data, req)) {
466 return tevent_req_post(req, ev);
468 memcpy(data, in_input_buffer.data, data_size);
471 if (file_info_level == SMB2_FILE_RENAME_INFORMATION_INTERNAL) {
472 struct tevent_req *subreq;
474 lck = get_existing_share_mode_lock(mem_ctx,
475 fsp->file_id);
476 if (lck == NULL) {
477 SAFE_FREE(data);
478 tevent_req_nterror(req,
479 NT_STATUS_UNSUCCESSFUL);
480 return tevent_req_post(req, ev);
483 subreq = delay_rename_for_lease_break(req,
484 smb2req,
486 fsp,
487 lck,
488 data,
489 data_size);
490 if (subreq) {
491 /* Wait for lease break response. */
493 /* Ensure we can't be closed in flight. */
494 if (!aio_add_req_to_fsp(fsp, req)) {
495 TALLOC_FREE(lck);
496 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
497 return tevent_req_post(req, ev);
500 TALLOC_FREE(lck);
501 return req;
505 status = smbd_do_setfilepathinfo(conn, smbreq, state,
506 file_info_level,
507 fsp,
508 fsp->fsp_name,
509 &data,
510 data_size,
511 &ret_size);
512 TALLOC_FREE(lck);
513 SAFE_FREE(data);
514 if (!NT_STATUS_IS_OK(status)) {
515 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
516 status = NT_STATUS_INVALID_INFO_CLASS;
518 tevent_req_nterror(req, status);
519 return tevent_req_post(req, ev);
521 break;
524 case 0x03:/* SMB2_SETINFO_SECURITY */
526 if (!CAN_WRITE(conn)) {
527 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
528 return tevent_req_post(req, ev);
531 status = set_sd_blob(fsp,
532 in_input_buffer.data,
533 in_input_buffer.length,
534 in_additional_information &
535 SMB_SUPPORTED_SECINFO_FLAGS);
536 if (!NT_STATUS_IS_OK(status)) {
537 tevent_req_nterror(req, status);
538 return tevent_req_post(req, ev);
540 break;
543 default:
544 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
545 return tevent_req_post(req, ev);
548 tevent_req_done(req);
549 return tevent_req_post(req, ev);
552 static NTSTATUS smbd_smb2_setinfo_recv(struct tevent_req *req)
554 NTSTATUS status;
556 if (tevent_req_is_nterror(req, &status)) {
557 tevent_req_received(req);
558 return status;
561 tevent_req_received(req);
562 return NT_STATUS_OK;