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 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
,
182 struct tevent_req
*subreq
;
184 struct share_mode_data
*d
= lck
->data
;
185 struct defer_rename_state
*rename_state
;
187 struct timeval timeout
;
189 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
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
);
199 if (e
->op_type
!= LEASE_OPLOCK
) {
203 l
= &d
->leases
[e
->lease_idx
];
205 if (smb2_lease_equal(fsp_client_guid(fsp
),
206 &fsp
->lease
->lease
.lease_key
,
212 if (share_mode_stale_pid(d
, i
)) {
216 if (!(e_lease_type
& SMB2_LEASE_HANDLE
)) {
221 break_to
= (e_lease_type
& ~SMB2_LEASE_HANDLE
);
223 send_break_message(fsp
->conn
->sconn
->msg_ctx
, e
, break_to
);
230 /* Setup a watch on this record. */
231 rename_state
= talloc_zero(req
, struct defer_rename_state
);
232 if (rename_state
== 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(
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");
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
);
270 struct share_mode_lock
*lck
;
274 status
= dbwrap_record_watch_recv(subreq
, state
->req
, NULL
);
276 if (!NT_STATUS_IS_OK(status
)) {
277 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
279 tevent_req_nterror(state
->req
, status
);
284 * Make sure we run as the user again
286 ok
= change_to_user(state
->smb2req
->tcon
->compat
,
287 state
->smb2req
->session
->compat
->vuid
);
289 tevent_req_nterror(state
->req
, NT_STATUS_ACCESS_DENIED
);
293 /* should we pass FLAG_CASELESS_PATHNAMES here? */
294 ok
= set_current_service(state
->smb2req
->tcon
->compat
, 0, true);
296 tevent_req_nterror(state
->req
, NT_STATUS_ACCESS_DENIED
);
300 /* Do we still need to wait ? */
301 lck
= get_existing_share_mode_lock(state
->req
, state
->fsp
->file_id
);
303 tevent_req_nterror(state
->req
, NT_STATUS_UNSUCCESSFUL
);
306 subreq
= delay_rename_for_lease_break(state
->req
,
314 /* Yep - keep waiting. */
320 /* Do the rename under the lock. */
321 status
= smbd_do_setfilepathinfo(state
->fsp
->conn
,
322 state
->smb2req
->smb1req
,
324 SMB2_FILE_RENAME_INFORMATION_INTERNAL
,
326 state
->fsp
->fsp_name
,
332 SAFE_FREE(state
->data
);
334 if (!NT_STATUS_IS_OK(status
)) {
335 tevent_req_nterror(state
->req
, status
);
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
;
362 req
= tevent_req_create(mem_ctx
, &state
,
363 struct smbd_smb2_setinfo_state
);
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
);
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
;
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
),
410 status
= map_nt_error_from_unix(errno
);
411 tevent_req_nterror(req
, status
);
412 return tevent_req_post(req
, ev
);
415 if (SMB_VFS_STAT(conn
, fsp
->fsp_name
) != 0) {
416 DEBUG(3,("smbd_smb2_setinfo_send: "
417 "fileinfo of %s failed (%s)\n",
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",
438 tevent_req_done(req
);
439 return tevent_req_post(req
, ev
);
441 tevent_req_nterror(req
,
442 NT_STATUS_OBJECT_PATH_INVALID
);
443 return tevent_req_post(req
, ev
);
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",
455 status
= map_nt_error_from_unix(errno
);
456 tevent_req_nterror(req
, status
);
457 return tevent_req_post(req
, ev
);
462 data_size
= in_input_buffer
.length
;
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
,
478 tevent_req_nterror(req
,
479 NT_STATUS_UNSUCCESSFUL
);
480 return tevent_req_post(req
, ev
);
483 subreq
= delay_rename_for_lease_break(req
,
491 /* Wait for lease break response. */
493 /* Ensure we can't be closed in flight. */
494 if (!aio_add_req_to_fsp(fsp
, req
)) {
496 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
497 return tevent_req_post(req
, ev
);
505 status
= smbd_do_setfilepathinfo(conn
, smbreq
, state
,
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
);
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
);
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
)
556 if (tevent_req_is_nterror(req
, &status
)) {
557 tevent_req_received(req
);
561 tevent_req_received(req
);