2 Unix SMB/CIFS implementation.
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/>.
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
27 #include "../lib/util/tevent_ntstatus.h"
28 #include "../librpc/gen_ndr/open_files.h"
29 #include "source3/lib/dbwrap/dbwrap_watch.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
,
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
;
47 const uint8_t *inbody
;
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) {
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
,
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
);
127 NTSTATUS error
; /* transport error */
129 status
= smbd_smb2_setinfo_recv(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
,
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
,
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
,
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
;
171 static int defer_rename_state_destructor(struct defer_rename_state
*rename_state
)
173 SAFE_FREE(rename_state
->data
);
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
,
188 struct tevent_req
*subreq
;
190 struct share_mode_data
*d
= lck
->data
;
191 struct defer_rename_state
*rename_state
;
193 struct timeval timeout
;
195 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
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
);
205 if (e
->op_type
!= LEASE_OPLOCK
) {
209 l
= &d
->leases
[e
->lease_idx
];
211 if (smb2_lease_equal(fsp_client_guid(fsp
),
212 &fsp
->lease
->lease
.lease_key
,
218 if (share_mode_stale_pid(d
, i
)) {
222 if (!(e_lease_type
& SMB2_LEASE_HANDLE
)) {
227 break_to
= (e_lease_type
& ~SMB2_LEASE_HANDLE
);
229 send_break_message(fsp
->conn
->sconn
->msg_ctx
, e
, break_to
);
236 /* Setup a watch on this record. */
237 rename_state
= talloc_zero(req
, struct defer_rename_state
);
238 if (rename_state
== 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_watched_watch_send(
255 (struct server_id
){0});
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");
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
);
278 struct share_mode_lock
*lck
;
282 status
= dbwrap_watched_watch_recv(subreq
, state
->req
, NULL
, NULL
,
285 if (!NT_STATUS_IS_OK(status
)) {
286 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
288 tevent_req_nterror(state
->req
, status
);
293 * Make sure we run as the user again
295 ok
= change_to_user(state
->smb2req
->tcon
->compat
,
296 state
->smb2req
->session
->compat
->vuid
);
298 tevent_req_nterror(state
->req
, NT_STATUS_ACCESS_DENIED
);
302 ok
= set_current_service(state
->smb2req
->tcon
->compat
, 0, true);
304 tevent_req_nterror(state
->req
, NT_STATUS_ACCESS_DENIED
);
308 /* Do we still need to wait ? */
309 lck
= get_existing_share_mode_lock(state
->req
, state
->fsp
->file_id
);
311 tevent_req_nterror(state
->req
, NT_STATUS_UNSUCCESSFUL
);
314 subreq
= delay_rename_for_lease_break(state
->req
,
322 /* Yep - keep waiting. */
329 /* Do the rename under the lock. */
330 status
= smbd_do_setfilepathinfo(state
->fsp
->conn
,
331 state
->smb2req
->smb1req
,
333 SMB2_FILE_RENAME_INFORMATION_INTERNAL
,
335 state
->fsp
->fsp_name
,
341 SAFE_FREE(state
->data
);
343 if (!NT_STATUS_IS_OK(status
)) {
344 tevent_req_nterror(state
->req
, status
);
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
;
371 req
= tevent_req_create(mem_ctx
, &state
,
372 struct smbd_smb2_setinfo_state
);
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
);
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
;
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
),
419 status
= map_nt_error_from_unix(errno
);
420 tevent_req_nterror(req
, status
);
421 return tevent_req_post(req
, ev
);
424 if (SMB_VFS_STAT(conn
, fsp
->fsp_name
) != 0) {
425 DEBUG(3,("smbd_smb2_setinfo_send: "
426 "fileinfo of %s failed (%s)\n",
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",
447 tevent_req_done(req
);
448 return tevent_req_post(req
, ev
);
450 tevent_req_nterror(req
,
451 NT_STATUS_OBJECT_PATH_INVALID
);
452 return tevent_req_post(req
, ev
);
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",
464 status
= map_nt_error_from_unix(errno
);
465 tevent_req_nterror(req
, status
);
466 return tevent_req_post(req
, ev
);
471 data_size
= in_input_buffer
.length
;
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
,
487 tevent_req_nterror(req
,
488 NT_STATUS_UNSUCCESSFUL
);
489 return tevent_req_post(req
, ev
);
492 subreq
= delay_rename_for_lease_break(req
,
500 /* Wait for lease break response. */
502 /* Ensure we can't be closed in flight. */
503 if (!aio_add_req_to_fsp(fsp
, req
)) {
505 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
506 return tevent_req_post(req
, ev
);
514 status
= smbd_do_setfilepathinfo(conn
, smbreq
, state
,
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
);
533 case 0x02:/* SMB2_SETINFO_FS */
535 uint16_t file_info_level
= in_file_info_class
+ 1000;
537 status
= smbd_do_setfsinfo(conn
, smbreq
, state
,
541 if (!NT_STATUS_IS_OK(status
)) {
542 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_LEVEL
)) {
543 status
= NT_STATUS_INVALID_INFO_CLASS
;
545 tevent_req_nterror(req
, status
);
546 return tevent_req_post(req
, ev
);
551 case 0x03:/* SMB2_SETINFO_SECURITY */
553 if (!CAN_WRITE(conn
)) {
554 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
555 return tevent_req_post(req
, ev
);
558 status
= set_sd_blob(fsp
,
559 in_input_buffer
.data
,
560 in_input_buffer
.length
,
561 in_additional_information
&
562 SMB_SUPPORTED_SECINFO_FLAGS
);
563 if (!NT_STATUS_IS_OK(status
)) {
564 tevent_req_nterror(req
, status
);
565 return tevent_req_post(req
, ev
);
571 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
572 return tevent_req_post(req
, ev
);
575 tevent_req_done(req
);
576 return tevent_req_post(req
, ev
);
579 static NTSTATUS
smbd_smb2_setinfo_recv(struct tevent_req
*req
)
583 if (tevent_req_is_nterror(req
, &status
)) {
584 tevent_req_received(req
);
588 tevent_req_received(req
);