2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-2007
5 Copyright (C) Stefan (metze) Metzmacher 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "fake_file.h"
26 #include "../libcli/security/security.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "passdb/lookup_sid.h"
30 #include "smbprofile.h"
31 #include "libsmb/libsmb.h"
33 extern const struct generic_mapping file_generic_mapping
;
35 static char *nttrans_realloc(char **ptr
, size_t size
)
38 smb_panic("nttrans_realloc() called with NULL ptr");
41 *ptr
= (char *)SMB_REALLOC(*ptr
, size
);
45 memset(*ptr
,'\0',size
);
49 /****************************************************************************
50 Send the required number of replies back.
51 We assume all fields other than the data fields are
52 set correctly for the type of call.
53 HACK ! Always assumes smb_setup field is zero.
54 ****************************************************************************/
56 static void send_nt_replies(connection_struct
*conn
,
57 struct smb_request
*req
, NTSTATUS nt_error
,
58 char *params
, int paramsize
,
59 char *pdata
, int datasize
)
61 int data_to_send
= datasize
;
62 int params_to_send
= paramsize
;
66 int params_sent_thistime
, data_sent_thistime
, total_sent_thistime
;
67 int alignment_offset
= 1;
68 int data_alignment_offset
= 0;
69 struct smbd_server_connection
*sconn
= req
->sconn
;
70 int max_send
= sconn
->smb1
.sessions
.max_send
;
73 * If there genuinely are no parameters or data to send just send
77 if(params_to_send
== 0 && data_to_send
== 0) {
78 reply_outbuf(req
, 18, 0);
79 if (NT_STATUS_V(nt_error
)) {
80 error_packet_set((char *)req
->outbuf
,
84 show_msg((char *)req
->outbuf
);
85 if (!srv_send_smb(sconn
,
88 IS_CONN_ENCRYPTED(conn
),
90 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
92 TALLOC_FREE(req
->outbuf
);
97 * When sending params and data ensure that both are nicely aligned.
98 * Only do this alignment when there is also data to send - else
99 * can cause NT redirector problems.
102 if (((params_to_send
% 4) != 0) && (data_to_send
!= 0)) {
103 data_alignment_offset
= 4 - (params_to_send
% 4);
107 * Space is bufsize minus Netbios over TCP header minus SMB header.
108 * The alignment_offset is to align the param bytes on a four byte
109 * boundary (2 bytes for data len, one byte pad).
110 * NT needs this to work correctly.
113 useable_space
= max_send
- (smb_size
116 + data_alignment_offset
);
118 if (useable_space
< 0) {
119 char *msg
= talloc_asprintf(
121 "send_nt_replies failed sanity useable_space = %d!!!",
123 DEBUG(0, ("%s\n", msg
));
124 exit_server_cleanly(msg
);
127 while (params_to_send
|| data_to_send
) {
130 * Calculate whether we will totally or partially fill this packet.
133 total_sent_thistime
= params_to_send
+ data_to_send
;
136 * We can never send more than useable_space.
139 total_sent_thistime
= MIN(total_sent_thistime
, useable_space
);
141 reply_outbuf(req
, 18,
142 total_sent_thistime
+ alignment_offset
143 + data_alignment_offset
);
146 * Set total params and data to be sent.
149 SIVAL(req
->outbuf
,smb_ntr_TotalParameterCount
,paramsize
);
150 SIVAL(req
->outbuf
,smb_ntr_TotalDataCount
,datasize
);
153 * Calculate how many parameters and data we can fit into
154 * this packet. Parameters get precedence.
157 params_sent_thistime
= MIN(params_to_send
,useable_space
);
158 data_sent_thistime
= useable_space
- params_sent_thistime
;
159 data_sent_thistime
= MIN(data_sent_thistime
,data_to_send
);
161 SIVAL(req
->outbuf
, smb_ntr_ParameterCount
,
162 params_sent_thistime
);
164 if(params_sent_thistime
== 0) {
165 SIVAL(req
->outbuf
,smb_ntr_ParameterOffset
,0);
166 SIVAL(req
->outbuf
,smb_ntr_ParameterDisplacement
,0);
169 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
170 * parameter bytes, however the first 4 bytes of outbuf are
171 * the Netbios over TCP header. Thus use smb_base() to subtract
172 * them from the calculation.
175 SIVAL(req
->outbuf
,smb_ntr_ParameterOffset
,
176 ((smb_buf(req
->outbuf
)+alignment_offset
)
177 - smb_base(req
->outbuf
)));
179 * Absolute displacement of param bytes sent in this packet.
182 SIVAL(req
->outbuf
, smb_ntr_ParameterDisplacement
,
187 * Deal with the data portion.
190 SIVAL(req
->outbuf
, smb_ntr_DataCount
, data_sent_thistime
);
192 if(data_sent_thistime
== 0) {
193 SIVAL(req
->outbuf
,smb_ntr_DataOffset
,0);
194 SIVAL(req
->outbuf
,smb_ntr_DataDisplacement
, 0);
197 * The offset of the data bytes is the offset of the
198 * parameter bytes plus the number of parameters being sent this time.
201 SIVAL(req
->outbuf
, smb_ntr_DataOffset
,
202 ((smb_buf(req
->outbuf
)+alignment_offset
) -
203 smb_base(req
->outbuf
))
204 + params_sent_thistime
+ data_alignment_offset
);
205 SIVAL(req
->outbuf
,smb_ntr_DataDisplacement
, pd
- pdata
);
209 * Copy the param bytes into the packet.
212 if(params_sent_thistime
) {
213 if (alignment_offset
!= 0) {
214 memset(smb_buf(req
->outbuf
), 0,
217 memcpy((smb_buf(req
->outbuf
)+alignment_offset
), pp
,
218 params_sent_thistime
);
222 * Copy in the data bytes
225 if(data_sent_thistime
) {
226 if (data_alignment_offset
!= 0) {
227 memset((smb_buf(req
->outbuf
)+alignment_offset
+
228 params_sent_thistime
), 0,
229 data_alignment_offset
);
231 memcpy(smb_buf(req
->outbuf
)+alignment_offset
232 +params_sent_thistime
+data_alignment_offset
,
233 pd
,data_sent_thistime
);
236 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
237 params_sent_thistime
, data_sent_thistime
, useable_space
));
238 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
239 params_to_send
, data_to_send
, paramsize
, datasize
));
241 if (NT_STATUS_V(nt_error
)) {
242 error_packet_set((char *)req
->outbuf
,
247 /* Send the packet */
248 show_msg((char *)req
->outbuf
);
249 if (!srv_send_smb(sconn
,
252 IS_CONN_ENCRYPTED(conn
),
254 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
257 TALLOC_FREE(req
->outbuf
);
259 pp
+= params_sent_thistime
;
260 pd
+= data_sent_thistime
;
262 params_to_send
-= params_sent_thistime
;
263 data_to_send
-= data_sent_thistime
;
269 if(params_to_send
< 0 || data_to_send
< 0) {
270 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
271 params_to_send
, data_to_send
));
272 exit_server_cleanly("send_nt_replies: internal error");
277 /****************************************************************************
278 Reply to an NT create and X call on a pipe
279 ****************************************************************************/
281 static void nt_open_pipe(char *fname
, connection_struct
*conn
,
282 struct smb_request
*req
, uint16_t *ppnum
)
287 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname
));
289 /* Strip \\ off the name if present. */
290 while (fname
[0] == '\\') {
294 status
= open_np_file(req
, fname
, &fsp
);
295 if (!NT_STATUS_IS_OK(status
)) {
296 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
297 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
301 reply_nterror(req
, status
);
309 /****************************************************************************
310 Reply to an NT create and X call for pipes.
311 ****************************************************************************/
313 static void do_ntcreate_pipe_open(connection_struct
*conn
,
314 struct smb_request
*req
)
317 uint16_t pnum
= FNUM_FIELD_INVALID
;
319 uint32 flags
= IVAL(req
->vwv
+3, 1);
320 TALLOC_CTX
*ctx
= talloc_tos();
322 srvstr_pull_req_talloc(ctx
, req
, &fname
, req
->buf
, STR_TERMINATE
);
325 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
329 nt_open_pipe(fname
, conn
, req
, &pnum
);
337 * Deal with pipe return.
340 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
341 /* This is very strange. We
342 * return 50 words, but only set
343 * the wcnt to 42 ? It's definitely
344 * what happens on the wire....
346 reply_outbuf(req
, 50, 0);
347 SCVAL(req
->outbuf
,smb_wct
,42);
349 reply_outbuf(req
, 34, 0);
352 SSVAL(req
->outbuf
, smb_vwv0
, 0xff); /* andx chain ends */
353 SSVAL(req
->outbuf
, smb_vwv1
, 0); /* no andx offset */
355 p
= (char *)req
->outbuf
+ smb_vwv2
;
359 SIVAL(p
,0,FILE_WAS_OPENED
);
362 SIVAL(p
,0,FILE_ATTRIBUTE_NORMAL
); /* File Attributes. */
365 SSVAL(p
,0,FILE_TYPE_MESSAGE_MODE_PIPE
);
367 SSVAL(p
,2, 0x5FF); /* ? */
370 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
372 SIVAL(p
,0,FILE_GENERIC_ALL
);
374 * For pipes W2K3 seems to return
376 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
378 SIVAL(p
,4,(FILE_GENERIC_READ
|FILE_GENERIC_WRITE
)&~FILE_APPEND_DATA
);
381 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname
));
384 struct case_semantics_state
{
385 connection_struct
*conn
;
388 bool short_case_preserve
;
391 /****************************************************************************
392 Restore case semantics.
393 ****************************************************************************/
395 static int restore_case_semantics(struct case_semantics_state
*state
)
397 state
->conn
->case_sensitive
= state
->case_sensitive
;
398 state
->conn
->case_preserve
= state
->case_preserve
;
399 state
->conn
->short_case_preserve
= state
->short_case_preserve
;
403 /****************************************************************************
405 ****************************************************************************/
407 static struct case_semantics_state
*set_posix_case_semantics(TALLOC_CTX
*mem_ctx
,
408 connection_struct
*conn
)
410 struct case_semantics_state
*result
;
412 if (!(result
= talloc(mem_ctx
, struct case_semantics_state
))) {
417 result
->case_sensitive
= conn
->case_sensitive
;
418 result
->case_preserve
= conn
->case_preserve
;
419 result
->short_case_preserve
= conn
->short_case_preserve
;
422 conn
->case_sensitive
= True
;
423 conn
->case_preserve
= True
;
424 conn
->short_case_preserve
= True
;
426 talloc_set_destructor(result
, restore_case_semantics
);
431 /****************************************************************************
432 Reply to an NT create and X call.
433 ****************************************************************************/
435 void reply_ntcreate_and_X(struct smb_request
*req
)
437 connection_struct
*conn
= req
->conn
;
438 struct smb_filename
*smb_fname
= NULL
;
442 uint32 file_attributes
;
444 uint32 create_disposition
;
445 uint32 create_options
;
447 uint64_t allocation_size
;
448 /* Breakout the oplock request bits so we can set the
449 reply bits separately. */
453 files_struct
*fsp
= NULL
;
455 struct timespec create_timespec
;
456 struct timespec c_timespec
;
457 struct timespec a_timespec
;
458 struct timespec m_timespec
;
459 struct timespec write_time_ts
;
462 uint8_t oplock_granted
= NO_OPLOCK_RETURN
;
463 struct case_semantics_state
*case_state
= NULL
;
464 TALLOC_CTX
*ctx
= talloc_tos();
466 START_PROFILE(SMBntcreateX
);
469 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
473 flags
= IVAL(req
->vwv
+3, 1);
474 access_mask
= IVAL(req
->vwv
+7, 1);
475 file_attributes
= IVAL(req
->vwv
+13, 1);
476 share_access
= IVAL(req
->vwv
+15, 1);
477 create_disposition
= IVAL(req
->vwv
+17, 1);
478 create_options
= IVAL(req
->vwv
+19, 1);
479 root_dir_fid
= (uint16
)IVAL(req
->vwv
+5, 1);
481 allocation_size
= BVAL(req
->vwv
+9, 1);
483 srvstr_get_path_req(ctx
, req
, &fname
, (const char *)req
->buf
,
484 STR_TERMINATE
, &status
);
486 if (!NT_STATUS_IS_OK(status
)) {
487 reply_nterror(req
, status
);
491 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
492 "file_attributes = 0x%x, share_access = 0x%x, "
493 "create_disposition = 0x%x create_options = 0x%x "
494 "root_dir_fid = 0x%x, fname = %s\n",
496 (unsigned int)access_mask
,
497 (unsigned int)file_attributes
,
498 (unsigned int)share_access
,
499 (unsigned int)create_disposition
,
500 (unsigned int)create_options
,
501 (unsigned int)root_dir_fid
,
505 * we need to remove ignored bits when they come directly from the client
506 * because we reuse some of them for internal stuff
508 create_options
&= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK
;
511 * If it's an IPC, use the pipe handler.
515 if (lp_nt_pipe_support()) {
516 do_ntcreate_pipe_open(conn
, req
);
519 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
523 oplock_request
= (flags
& REQUEST_OPLOCK
) ? EXCLUSIVE_OPLOCK
: 0;
524 if (oplock_request
) {
525 oplock_request
|= (flags
& REQUEST_BATCH_OPLOCK
)
529 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
530 case_state
= set_posix_case_semantics(ctx
, conn
);
532 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
537 status
= filename_convert(ctx
,
539 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
545 TALLOC_FREE(case_state
);
547 if (!NT_STATUS_IS_OK(status
)) {
548 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
550 NT_STATUS_PATH_NOT_COVERED
,
554 reply_nterror(req
, status
);
559 * Bug #6898 - clients using Windows opens should
560 * never be able to set this attribute into the
563 file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
565 status
= SMB_VFS_CREATE_FILE(
568 root_dir_fid
, /* root_dir_fid */
569 smb_fname
, /* fname */
570 access_mask
, /* access_mask */
571 share_access
, /* share_access */
572 create_disposition
, /* create_disposition*/
573 create_options
, /* create_options */
574 file_attributes
, /* file_attributes */
575 oplock_request
, /* oplock_request */
576 allocation_size
, /* allocation_size */
577 0, /* private_flags */
583 if (!NT_STATUS_IS_OK(status
)) {
584 if (open_was_deferred(req
->sconn
, req
->mid
)) {
585 /* We have re-scheduled this call, no error. */
588 reply_openerror(req
, status
);
592 /* Ensure we're pointing at the correct stat struct. */
593 TALLOC_FREE(smb_fname
);
594 smb_fname
= fsp
->fsp_name
;
597 * If the caller set the extended oplock request bit
598 * and we granted one (by whatever means) - set the
599 * correct bit for extended oplock reply.
602 if (oplock_request
&&
603 (lp_fake_oplocks(SNUM(conn
))
604 || EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
))) {
607 * Exclusive oplock granted
610 if (flags
& REQUEST_BATCH_OPLOCK
) {
611 oplock_granted
= BATCH_OPLOCK_RETURN
;
613 oplock_granted
= EXCLUSIVE_OPLOCK_RETURN
;
615 } else if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
616 oplock_granted
= LEVEL_II_OPLOCK_RETURN
;
618 oplock_granted
= NO_OPLOCK_RETURN
;
621 file_len
= smb_fname
->st
.st_ex_size
;
623 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
624 /* This is very strange. We
625 * return 50 words, but only set
626 * the wcnt to 42 ? It's definitely
627 * what happens on the wire....
629 reply_outbuf(req
, 50, 0);
630 SCVAL(req
->outbuf
,smb_wct
,42);
632 reply_outbuf(req
, 34, 0);
635 SSVAL(req
->outbuf
, smb_vwv0
, 0xff); /* andx chain ends */
636 SSVAL(req
->outbuf
, smb_vwv1
, 0); /* no andx offset */
638 p
= (char *)req
->outbuf
+ smb_vwv2
;
640 SCVAL(p
, 0, oplock_granted
);
643 SSVAL(p
,0,fsp
->fnum
);
645 if ((create_disposition
== FILE_SUPERSEDE
)
646 && (info
== FILE_WAS_OVERWRITTEN
)) {
647 SIVAL(p
,0,FILE_WAS_SUPERSEDED
);
653 fattr
= dos_mode(conn
, smb_fname
);
655 fattr
= FILE_ATTRIBUTE_NORMAL
;
658 /* Deal with other possible opens having a modified
660 ZERO_STRUCT(write_time_ts
);
661 get_file_infos(fsp
->file_id
, 0, NULL
, &write_time_ts
);
662 if (!null_timespec(write_time_ts
)) {
663 update_stat_ex_mtime(&smb_fname
->st
, write_time_ts
);
667 create_timespec
= get_create_timespec(conn
, fsp
, smb_fname
);
668 a_timespec
= smb_fname
->st
.st_ex_atime
;
669 m_timespec
= smb_fname
->st
.st_ex_mtime
;
670 c_timespec
= get_change_timespec(conn
, fsp
, smb_fname
);
672 if (lp_dos_filetime_resolution(SNUM(conn
))) {
673 dos_filetime_timespec(&create_timespec
);
674 dos_filetime_timespec(&a_timespec
);
675 dos_filetime_timespec(&m_timespec
);
676 dos_filetime_timespec(&c_timespec
);
679 put_long_date_timespec(conn
->ts_res
, p
, create_timespec
); /* create time. */
681 put_long_date_timespec(conn
->ts_res
, p
, a_timespec
); /* access time */
683 put_long_date_timespec(conn
->ts_res
, p
, m_timespec
); /* write time */
685 put_long_date_timespec(conn
->ts_res
, p
, c_timespec
); /* change time */
687 SIVAL(p
,0,fattr
); /* File Attributes. */
689 SOFF_T(p
, 0, SMB_VFS_GET_ALLOC_SIZE(conn
,fsp
,&smb_fname
->st
));
691 SOFF_T(p
,0,file_len
);
693 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
694 uint16_t file_status
= (NO_EAS
|NO_SUBSTREAMS
|NO_REPARSETAG
);
695 size_t num_names
= 0;
696 unsigned int num_streams
= 0;
697 struct stream_struct
*streams
= NULL
;
699 /* Do we have any EA's ? */
700 status
= get_ea_names_from_file(ctx
, conn
, fsp
,
701 smb_fname
->base_name
, NULL
, &num_names
);
702 if (NT_STATUS_IS_OK(status
) && num_names
) {
703 file_status
&= ~NO_EAS
;
705 status
= vfs_streaminfo(conn
, NULL
, smb_fname
->base_name
, ctx
,
706 &num_streams
, &streams
);
707 /* There is always one stream, ::$DATA. */
708 if (NT_STATUS_IS_OK(status
) && num_streams
> 1) {
709 file_status
&= ~NO_SUBSTREAMS
;
711 TALLOC_FREE(streams
);
712 SSVAL(p
,2,file_status
);
715 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
717 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
720 if (fsp
->is_directory
||
722 can_write_to_file(conn
, smb_fname
)) {
723 perms
= FILE_GENERIC_ALL
;
725 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
730 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
731 fsp_fnum_dbg(fsp
), smb_fname_str_dbg(smb_fname
)));
734 END_PROFILE(SMBntcreateX
);
738 /****************************************************************************
739 Reply to a NT_TRANSACT_CREATE call to open a pipe.
740 ****************************************************************************/
742 static void do_nt_transact_create_pipe(connection_struct
*conn
,
743 struct smb_request
*req
,
744 uint16
**ppsetup
, uint32 setup_count
,
745 char **ppparams
, uint32 parameter_count
,
746 char **ppdata
, uint32 data_count
)
749 char *params
= *ppparams
;
750 uint16_t pnum
= FNUM_FIELD_INVALID
;
755 TALLOC_CTX
*ctx
= talloc_tos();
758 * Ensure minimum number of parameters sent.
761 if(parameter_count
< 54) {
762 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
763 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
767 flags
= IVAL(params
,0);
769 srvstr_get_path(ctx
, params
, req
->flags2
, &fname
, params
+53,
770 parameter_count
-53, STR_TERMINATE
,
772 if (!NT_STATUS_IS_OK(status
)) {
773 reply_nterror(req
, status
);
777 nt_open_pipe(fname
, conn
, req
, &pnum
);
784 /* Realloc the size of parameters and data we will return */
785 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
786 /* Extended response is 32 more byyes. */
791 params
= nttrans_realloc(ppparams
, param_len
);
793 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
798 SCVAL(p
,0,NO_OPLOCK_RETURN
);
803 SIVAL(p
,0,FILE_WAS_OPENED
);
807 SIVAL(p
,0,FILE_ATTRIBUTE_NORMAL
); /* File Attributes. */
810 SSVAL(p
,0,FILE_TYPE_MESSAGE_MODE_PIPE
);
812 SSVAL(p
,2, 0x5FF); /* ? */
815 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
817 SIVAL(p
,0,FILE_GENERIC_ALL
);
819 * For pipes W2K3 seems to return
821 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
823 SIVAL(p
,4,(FILE_GENERIC_READ
|FILE_GENERIC_WRITE
)&~FILE_APPEND_DATA
);
826 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname
));
828 /* Send the required number of replies */
829 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
834 /*********************************************************************
835 Windows seems to do canonicalization of inheritance bits. Do the
837 *********************************************************************/
839 static void canonicalize_inheritance_bits(struct security_descriptor
*psd
)
841 bool set_auto_inherited
= false;
844 * We need to filter out the
845 * SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ
846 * bits. If both are set we store SEC_DESC_DACL_AUTO_INHERITED
847 * as this alters whether SEC_ACE_FLAG_INHERITED_ACE is set
848 * when an ACE is inherited. Otherwise we zero these bits out.
851 * http://social.msdn.microsoft.com/Forums/eu/os_fileservices/thread/11f77b68-731e-407d-b1b3-064750716531
856 if ((psd
->type
& (SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
))
857 == (SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
)) {
858 set_auto_inherited
= true;
861 psd
->type
&= ~(SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
);
862 if (set_auto_inherited
) {
863 psd
->type
|= SEC_DESC_DACL_AUTO_INHERITED
;
867 /****************************************************************************
868 Internal fn to set security descriptors.
869 ****************************************************************************/
871 NTSTATUS
set_sd(files_struct
*fsp
, struct security_descriptor
*psd
,
872 uint32_t security_info_sent
)
876 if (!CAN_WRITE(fsp
->conn
)) {
877 return NT_STATUS_ACCESS_DENIED
;
880 if (!lp_nt_acl_support(SNUM(fsp
->conn
))) {
884 if (psd
->owner_sid
== NULL
) {
885 security_info_sent
&= ~SECINFO_OWNER
;
887 if (psd
->group_sid
== NULL
) {
888 security_info_sent
&= ~SECINFO_GROUP
;
891 /* Ensure we have at least one thing set. */
892 if ((security_info_sent
& (SECINFO_OWNER
|SECINFO_GROUP
|SECINFO_DACL
|SECINFO_SACL
)) == 0) {
893 if (security_info_sent
& SECINFO_LABEL
) {
894 /* Only consider SECINFO_LABEL if no other
895 bits are set. Just like W2K3 we don't
899 return NT_STATUS_INVALID_PARAMETER
;
902 /* Ensure we have the rights to do this. */
903 if (security_info_sent
& SECINFO_OWNER
) {
904 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
905 return NT_STATUS_ACCESS_DENIED
;
909 if (security_info_sent
& SECINFO_GROUP
) {
910 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
911 return NT_STATUS_ACCESS_DENIED
;
915 if (security_info_sent
& SECINFO_DACL
) {
916 if (!(fsp
->access_mask
& SEC_STD_WRITE_DAC
)) {
917 return NT_STATUS_ACCESS_DENIED
;
919 /* Convert all the generic bits. */
921 security_acl_map_generic(psd
->dacl
, &file_generic_mapping
);
925 if (security_info_sent
& SECINFO_SACL
) {
926 if (!(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
927 return NT_STATUS_ACCESS_DENIED
;
929 /* Convert all the generic bits. */
931 security_acl_map_generic(psd
->sacl
, &file_generic_mapping
);
935 canonicalize_inheritance_bits(psd
);
937 if (DEBUGLEVEL
>= 10) {
938 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp
)));
939 NDR_PRINT_DEBUG(security_descriptor
, psd
);
942 status
= SMB_VFS_FSET_NT_ACL(fsp
, security_info_sent
, psd
);
949 /****************************************************************************
950 Internal fn to set security descriptors from a data blob.
951 ****************************************************************************/
953 NTSTATUS
set_sd_blob(files_struct
*fsp
, uint8_t *data
, uint32_t sd_len
,
954 uint32_t security_info_sent
)
956 struct security_descriptor
*psd
= NULL
;
960 return NT_STATUS_INVALID_PARAMETER
;
963 status
= unmarshall_sec_desc(talloc_tos(), data
, sd_len
, &psd
);
965 if (!NT_STATUS_IS_OK(status
)) {
969 return set_sd(fsp
, psd
, security_info_sent
);
972 /****************************************************************************
973 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
974 ****************************************************************************/
976 struct ea_list
*read_nttrans_ea_list(TALLOC_CTX
*ctx
, const char *pdata
, size_t data_size
)
978 struct ea_list
*ea_list_head
= NULL
;
985 while (offset
+ 4 <= data_size
) {
986 size_t next_offset
= IVAL(pdata
,offset
);
987 struct ea_list
*eal
= read_ea_list_entry(ctx
, pdata
+ offset
+ 4, data_size
- offset
- 4, NULL
);
993 DLIST_ADD_END(ea_list_head
, eal
, struct ea_list
*);
994 if (next_offset
== 0) {
997 offset
+= next_offset
;
1000 return ea_list_head
;
1003 /****************************************************************************
1004 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1005 ****************************************************************************/
1007 static void call_nt_transact_create(connection_struct
*conn
,
1008 struct smb_request
*req
,
1009 uint16
**ppsetup
, uint32 setup_count
,
1010 char **ppparams
, uint32 parameter_count
,
1011 char **ppdata
, uint32 data_count
,
1012 uint32 max_data_count
)
1014 struct smb_filename
*smb_fname
= NULL
;
1016 char *params
= *ppparams
;
1017 char *data
= *ppdata
;
1018 /* Breakout the oplock request bits so we can set the reply bits separately. */
1022 files_struct
*fsp
= NULL
;
1026 uint32 file_attributes
;
1027 uint32 share_access
;
1028 uint32 create_disposition
;
1029 uint32 create_options
;
1031 struct security_descriptor
*sd
= NULL
;
1033 uint16 root_dir_fid
;
1034 struct timespec create_timespec
;
1035 struct timespec c_timespec
;
1036 struct timespec a_timespec
;
1037 struct timespec m_timespec
;
1038 struct timespec write_time_ts
;
1039 struct ea_list
*ea_list
= NULL
;
1042 uint64_t allocation_size
;
1044 uint8_t oplock_granted
;
1045 struct case_semantics_state
*case_state
= NULL
;
1046 TALLOC_CTX
*ctx
= talloc_tos();
1048 DEBUG(5,("call_nt_transact_create\n"));
1051 * If it's an IPC, use the pipe handler.
1055 if (lp_nt_pipe_support()) {
1056 do_nt_transact_create_pipe(
1058 ppsetup
, setup_count
,
1059 ppparams
, parameter_count
,
1060 ppdata
, data_count
);
1063 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
1068 * Ensure minimum number of parameters sent.
1071 if(parameter_count
< 54) {
1072 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
1073 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1077 flags
= IVAL(params
,0);
1078 access_mask
= IVAL(params
,8);
1079 file_attributes
= IVAL(params
,20);
1080 share_access
= IVAL(params
,24);
1081 create_disposition
= IVAL(params
,28);
1082 create_options
= IVAL(params
,32);
1083 sd_len
= IVAL(params
,36);
1084 ea_len
= IVAL(params
,40);
1085 root_dir_fid
= (uint16
)IVAL(params
,4);
1086 allocation_size
= BVAL(params
,12);
1089 * we need to remove ignored bits when they come directly from the client
1090 * because we reuse some of them for internal stuff
1092 create_options
&= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK
;
1094 /* Ensure the data_len is correct for the sd and ea values given. */
1095 if ((ea_len
+ sd_len
> data_count
)
1096 || (ea_len
> data_count
) || (sd_len
> data_count
)
1097 || (ea_len
+ sd_len
< ea_len
) || (ea_len
+ sd_len
< sd_len
)) {
1098 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1099 "%u, data_count = %u\n", (unsigned int)ea_len
,
1100 (unsigned int)sd_len
, (unsigned int)data_count
));
1101 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1106 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1109 status
= unmarshall_sec_desc(ctx
, (uint8_t *)data
, sd_len
,
1111 if (!NT_STATUS_IS_OK(status
)) {
1112 DEBUG(10, ("call_nt_transact_create: "
1113 "unmarshall_sec_desc failed: %s\n",
1114 nt_errstr(status
)));
1115 reply_nterror(req
, status
);
1121 if (!lp_ea_support(SNUM(conn
))) {
1122 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1123 "EA's not supported.\n",
1124 (unsigned int)ea_len
));
1125 reply_nterror(req
, NT_STATUS_EAS_NOT_SUPPORTED
);
1130 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1131 "too small (should be more than 10)\n",
1132 (unsigned int)ea_len
));
1133 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1137 /* We have already checked that ea_len <= data_count here. */
1138 ea_list
= read_nttrans_ea_list(talloc_tos(), data
+ sd_len
,
1140 if (ea_list
== NULL
) {
1141 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1146 srvstr_get_path(ctx
, params
, req
->flags2
, &fname
,
1147 params
+53, parameter_count
-53,
1148 STR_TERMINATE
, &status
);
1149 if (!NT_STATUS_IS_OK(status
)) {
1150 reply_nterror(req
, status
);
1154 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1155 case_state
= set_posix_case_semantics(ctx
, conn
);
1157 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1162 status
= filename_convert(ctx
,
1164 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1170 TALLOC_FREE(case_state
);
1172 if (!NT_STATUS_IS_OK(status
)) {
1173 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
1174 reply_botherror(req
,
1175 NT_STATUS_PATH_NOT_COVERED
,
1176 ERRSRV
, ERRbadpath
);
1179 reply_nterror(req
, status
);
1183 oplock_request
= (flags
& REQUEST_OPLOCK
) ? EXCLUSIVE_OPLOCK
: 0;
1184 if (oplock_request
) {
1185 oplock_request
|= (flags
& REQUEST_BATCH_OPLOCK
)
1190 * Bug #6898 - clients using Windows opens should
1191 * never be able to set this attribute into the
1194 file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
1196 status
= SMB_VFS_CREATE_FILE(
1199 root_dir_fid
, /* root_dir_fid */
1200 smb_fname
, /* fname */
1201 access_mask
, /* access_mask */
1202 share_access
, /* share_access */
1203 create_disposition
, /* create_disposition*/
1204 create_options
, /* create_options */
1205 file_attributes
, /* file_attributes */
1206 oplock_request
, /* oplock_request */
1207 allocation_size
, /* allocation_size */
1208 0, /* private_flags */
1210 ea_list
, /* ea_list */
1214 if(!NT_STATUS_IS_OK(status
)) {
1215 if (open_was_deferred(req
->sconn
, req
->mid
)) {
1216 /* We have re-scheduled this call, no error. */
1219 reply_openerror(req
, status
);
1223 /* Ensure we're pointing at the correct stat struct. */
1224 TALLOC_FREE(smb_fname
);
1225 smb_fname
= fsp
->fsp_name
;
1228 * If the caller set the extended oplock request bit
1229 * and we granted one (by whatever means) - set the
1230 * correct bit for extended oplock reply.
1233 if (oplock_request
&&
1234 (lp_fake_oplocks(SNUM(conn
))
1235 || EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
))) {
1238 * Exclusive oplock granted
1241 if (flags
& REQUEST_BATCH_OPLOCK
) {
1242 oplock_granted
= BATCH_OPLOCK_RETURN
;
1244 oplock_granted
= EXCLUSIVE_OPLOCK_RETURN
;
1246 } else if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
1247 oplock_granted
= LEVEL_II_OPLOCK_RETURN
;
1249 oplock_granted
= NO_OPLOCK_RETURN
;
1252 file_len
= smb_fname
->st
.st_ex_size
;
1254 /* Realloc the size of parameters and data we will return */
1255 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1256 /* Extended response is 32 more byyes. */
1261 params
= nttrans_realloc(ppparams
, param_len
);
1262 if(params
== NULL
) {
1263 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1268 SCVAL(p
, 0, oplock_granted
);
1271 SSVAL(p
,0,fsp
->fnum
);
1273 if ((create_disposition
== FILE_SUPERSEDE
)
1274 && (info
== FILE_WAS_OVERWRITTEN
)) {
1275 SIVAL(p
,0,FILE_WAS_SUPERSEDED
);
1281 fattr
= dos_mode(conn
, smb_fname
);
1283 fattr
= FILE_ATTRIBUTE_NORMAL
;
1286 /* Deal with other possible opens having a modified
1288 ZERO_STRUCT(write_time_ts
);
1289 get_file_infos(fsp
->file_id
, 0, NULL
, &write_time_ts
);
1290 if (!null_timespec(write_time_ts
)) {
1291 update_stat_ex_mtime(&smb_fname
->st
, write_time_ts
);
1295 create_timespec
= get_create_timespec(conn
, fsp
, smb_fname
);
1296 a_timespec
= smb_fname
->st
.st_ex_atime
;
1297 m_timespec
= smb_fname
->st
.st_ex_mtime
;
1298 c_timespec
= get_change_timespec(conn
, fsp
, smb_fname
);
1300 if (lp_dos_filetime_resolution(SNUM(conn
))) {
1301 dos_filetime_timespec(&create_timespec
);
1302 dos_filetime_timespec(&a_timespec
);
1303 dos_filetime_timespec(&m_timespec
);
1304 dos_filetime_timespec(&c_timespec
);
1307 put_long_date_timespec(conn
->ts_res
, p
, create_timespec
); /* create time. */
1309 put_long_date_timespec(conn
->ts_res
, p
, a_timespec
); /* access time */
1311 put_long_date_timespec(conn
->ts_res
, p
, m_timespec
); /* write time */
1313 put_long_date_timespec(conn
->ts_res
, p
, c_timespec
); /* change time */
1315 SIVAL(p
,0,fattr
); /* File Attributes. */
1317 SOFF_T(p
, 0, SMB_VFS_GET_ALLOC_SIZE(conn
, fsp
, &smb_fname
->st
));
1319 SOFF_T(p
,0,file_len
);
1321 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1322 uint16_t file_status
= (NO_EAS
|NO_SUBSTREAMS
|NO_REPARSETAG
);
1323 size_t num_names
= 0;
1324 unsigned int num_streams
= 0;
1325 struct stream_struct
*streams
= NULL
;
1327 /* Do we have any EA's ? */
1328 status
= get_ea_names_from_file(ctx
, conn
, fsp
,
1329 smb_fname
->base_name
, NULL
, &num_names
);
1330 if (NT_STATUS_IS_OK(status
) && num_names
) {
1331 file_status
&= ~NO_EAS
;
1333 status
= vfs_streaminfo(conn
, NULL
, smb_fname
->base_name
, ctx
,
1334 &num_streams
, &streams
);
1335 /* There is always one stream, ::$DATA. */
1336 if (NT_STATUS_IS_OK(status
) && num_streams
> 1) {
1337 file_status
&= ~NO_SUBSTREAMS
;
1339 TALLOC_FREE(streams
);
1340 SSVAL(p
,2,file_status
);
1343 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
1345 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1348 if (fsp
->is_directory
||
1350 can_write_to_file(conn
, smb_fname
)) {
1351 perms
= FILE_GENERIC_ALL
;
1353 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
1358 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1359 smb_fname_str_dbg(smb_fname
)));
1361 /* Send the required number of replies */
1362 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
1367 /****************************************************************************
1368 Reply to a NT CANCEL request.
1369 conn POINTER CAN BE NULL HERE !
1370 ****************************************************************************/
1372 void reply_ntcancel(struct smb_request
*req
)
1375 * Go through and cancel any pending change notifies.
1378 START_PROFILE(SMBntcancel
);
1379 srv_cancel_sign_response(req
->sconn
);
1380 remove_pending_change_notify_requests_by_mid(req
->sconn
, req
->mid
);
1381 remove_pending_lock_requests_by_mid_smb1(req
->sconn
, req
->mid
);
1383 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1384 (unsigned long long)req
->mid
));
1386 END_PROFILE(SMBntcancel
);
1390 /****************************************************************************
1392 ****************************************************************************/
1394 static NTSTATUS
copy_internals(TALLOC_CTX
*ctx
,
1395 connection_struct
*conn
,
1396 struct smb_request
*req
,
1397 struct smb_filename
*smb_fname_src
,
1398 struct smb_filename
*smb_fname_dst
,
1401 files_struct
*fsp1
,*fsp2
;
1405 NTSTATUS status
= NT_STATUS_OK
;
1408 if (!CAN_WRITE(conn
)) {
1409 status
= NT_STATUS_MEDIA_WRITE_PROTECTED
;
1413 /* Source must already exist. */
1414 if (!VALID_STAT(smb_fname_src
->st
)) {
1415 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1419 /* Ensure attributes match. */
1420 fattr
= dos_mode(conn
, smb_fname_src
);
1421 if ((fattr
& ~attrs
) & (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) {
1422 status
= NT_STATUS_NO_SUCH_FILE
;
1426 /* Disallow if dst file already exists. */
1427 if (VALID_STAT(smb_fname_dst
->st
)) {
1428 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
1432 /* No links from a directory. */
1433 if (S_ISDIR(smb_fname_src
->st
.st_ex_mode
)) {
1434 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
1438 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1439 smb_fname_str_dbg(smb_fname_src
),
1440 smb_fname_str_dbg(smb_fname_dst
)));
1442 status
= SMB_VFS_CREATE_FILE(
1445 0, /* root_dir_fid */
1446 smb_fname_src
, /* fname */
1447 FILE_READ_DATA
|FILE_READ_ATTRIBUTES
|
1448 FILE_READ_EA
, /* access_mask */
1449 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1451 FILE_OPEN
, /* create_disposition*/
1452 0, /* create_options */
1453 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
1454 NO_OPLOCK
, /* oplock_request */
1455 0, /* allocation_size */
1456 0, /* private_flags */
1462 if (!NT_STATUS_IS_OK(status
)) {
1466 status
= SMB_VFS_CREATE_FILE(
1469 0, /* root_dir_fid */
1470 smb_fname_dst
, /* fname */
1471 FILE_WRITE_DATA
|FILE_WRITE_ATTRIBUTES
|
1472 FILE_WRITE_EA
, /* access_mask */
1473 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1475 FILE_CREATE
, /* create_disposition*/
1476 0, /* create_options */
1477 fattr
, /* file_attributes */
1478 NO_OPLOCK
, /* oplock_request */
1479 0, /* allocation_size */
1480 0, /* private_flags */
1486 if (!NT_STATUS_IS_OK(status
)) {
1487 close_file(NULL
, fsp1
, ERROR_CLOSE
);
1491 if (smb_fname_src
->st
.st_ex_size
) {
1492 ret
= vfs_transfer_file(fsp1
, fsp2
, smb_fname_src
->st
.st_ex_size
);
1496 * As we are opening fsp1 read-only we only expect
1497 * an error on close on fsp2 if we are out of space.
1498 * Thus we don't look at the error return from the
1501 close_file(NULL
, fsp1
, NORMAL_CLOSE
);
1503 /* Ensure the modtime is set correctly on the destination file. */
1504 set_close_write_time(fsp2
, smb_fname_src
->st
.st_ex_mtime
);
1506 status
= close_file(NULL
, fsp2
, NORMAL_CLOSE
);
1508 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1509 creates the file. This isn't the correct thing to do in the copy
1511 if (!parent_dirname(talloc_tos(), smb_fname_dst
->base_name
, &parent
,
1513 status
= NT_STATUS_NO_MEMORY
;
1516 file_set_dosmode(conn
, smb_fname_dst
, fattr
, parent
, false);
1517 TALLOC_FREE(parent
);
1519 if (ret
< (off_t
)smb_fname_src
->st
.st_ex_size
) {
1520 status
= NT_STATUS_DISK_FULL
;
1524 if (!NT_STATUS_IS_OK(status
)) {
1525 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1526 nt_errstr(status
), smb_fname_str_dbg(smb_fname_src
),
1527 smb_fname_str_dbg(smb_fname_dst
)));
1533 /****************************************************************************
1534 Reply to a NT rename request.
1535 ****************************************************************************/
1537 void reply_ntrename(struct smb_request
*req
)
1539 connection_struct
*conn
= req
->conn
;
1540 struct smb_filename
*smb_fname_old
= NULL
;
1541 struct smb_filename
*smb_fname_new
= NULL
;
1542 char *oldname
= NULL
;
1543 char *newname
= NULL
;
1546 bool src_has_wcard
= False
;
1547 bool dest_has_wcard
= False
;
1549 uint32_t ucf_flags_src
= 0;
1550 uint32_t ucf_flags_dst
= 0;
1552 TALLOC_CTX
*ctx
= talloc_tos();
1553 bool stream_rename
= false;
1555 START_PROFILE(SMBntrename
);
1558 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1562 attrs
= SVAL(req
->vwv
+0, 0);
1563 rename_type
= SVAL(req
->vwv
+1, 0);
1565 p
= (const char *)req
->buf
+ 1;
1566 p
+= srvstr_get_path_req_wcard(ctx
, req
, &oldname
, p
, STR_TERMINATE
,
1567 &status
, &src_has_wcard
);
1568 if (!NT_STATUS_IS_OK(status
)) {
1569 reply_nterror(req
, status
);
1573 if (ms_has_wild(oldname
)) {
1574 reply_nterror(req
, NT_STATUS_OBJECT_PATH_SYNTAX_BAD
);
1579 p
+= srvstr_get_path_req_wcard(ctx
, req
, &newname
, p
, STR_TERMINATE
,
1580 &status
, &dest_has_wcard
);
1581 if (!NT_STATUS_IS_OK(status
)) {
1582 reply_nterror(req
, status
);
1586 if (!lp_posix_pathnames()) {
1587 /* The newname must begin with a ':' if the
1588 oldname contains a ':'. */
1589 if (strchr_m(oldname
, ':')) {
1590 if (newname
[0] != ':') {
1591 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1594 stream_rename
= true;
1599 * If this is a rename operation, allow wildcards and save the
1600 * destination's last component.
1602 if (rename_type
== RENAME_FLAG_RENAME
) {
1603 ucf_flags_src
= UCF_COND_ALLOW_WCARD_LCOMP
;
1604 ucf_flags_dst
= UCF_COND_ALLOW_WCARD_LCOMP
| UCF_SAVE_LCOMP
;
1607 /* rename_internals() calls unix_convert(), so don't call it here. */
1608 status
= filename_convert(ctx
, conn
,
1609 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1614 if (!NT_STATUS_IS_OK(status
)) {
1615 if (NT_STATUS_EQUAL(status
,
1616 NT_STATUS_PATH_NOT_COVERED
)) {
1617 reply_botherror(req
,
1618 NT_STATUS_PATH_NOT_COVERED
,
1619 ERRSRV
, ERRbadpath
);
1622 reply_nterror(req
, status
);
1626 status
= filename_convert(ctx
, conn
,
1627 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1632 if (!NT_STATUS_IS_OK(status
)) {
1633 if (NT_STATUS_EQUAL(status
,
1634 NT_STATUS_PATH_NOT_COVERED
)) {
1635 reply_botherror(req
,
1636 NT_STATUS_PATH_NOT_COVERED
,
1637 ERRSRV
, ERRbadpath
);
1640 reply_nterror(req
, status
);
1644 if (stream_rename
) {
1645 /* smb_fname_new must be the same as smb_fname_old. */
1646 TALLOC_FREE(smb_fname_new
->base_name
);
1647 smb_fname_new
->base_name
= talloc_strdup(smb_fname_new
,
1648 smb_fname_old
->base_name
);
1649 if (!smb_fname_new
->base_name
) {
1650 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1655 DEBUG(3,("reply_ntrename: %s -> %s\n",
1656 smb_fname_str_dbg(smb_fname_old
),
1657 smb_fname_str_dbg(smb_fname_new
)));
1659 switch(rename_type
) {
1660 case RENAME_FLAG_RENAME
:
1661 status
= rename_internals(ctx
, conn
, req
,
1662 smb_fname_old
, smb_fname_new
,
1663 attrs
, False
, src_has_wcard
,
1667 case RENAME_FLAG_HARD_LINK
:
1668 if (src_has_wcard
|| dest_has_wcard
) {
1670 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1672 status
= hardlink_internals(ctx
, conn
,
1679 case RENAME_FLAG_COPY
:
1680 if (src_has_wcard
|| dest_has_wcard
) {
1682 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1684 status
= copy_internals(ctx
, conn
, req
,
1690 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION
:
1691 status
= NT_STATUS_INVALID_PARAMETER
;
1694 status
= NT_STATUS_ACCESS_DENIED
; /* Default error. */
1698 if (!NT_STATUS_IS_OK(status
)) {
1699 if (open_was_deferred(req
->sconn
, req
->mid
)) {
1700 /* We have re-scheduled this call. */
1704 reply_nterror(req
, status
);
1708 reply_outbuf(req
, 0, 0);
1710 END_PROFILE(SMBntrename
);
1714 /****************************************************************************
1715 Reply to a notify change - queue the request and
1716 don't allow a directory to be opened.
1717 ****************************************************************************/
1719 static void smbd_smb1_notify_reply(struct smb_request
*req
,
1720 NTSTATUS error_code
,
1721 uint8_t *buf
, size_t len
)
1723 send_nt_replies(req
->conn
, req
, error_code
, (char *)buf
, len
, NULL
, 0);
1726 static void call_nt_transact_notify_change(connection_struct
*conn
,
1727 struct smb_request
*req
,
1731 uint32 parameter_count
,
1732 char **ppdata
, uint32 data_count
,
1733 uint32 max_data_count
,
1734 uint32 max_param_count
)
1736 uint16
*setup
= *ppsetup
;
1742 if(setup_count
< 6) {
1743 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1747 fsp
= file_fsp(req
, SVAL(setup
,4));
1748 filter
= IVAL(setup
, 0);
1749 recursive
= (SVAL(setup
, 6) != 0) ? True
: False
;
1751 DEBUG(3,("call_nt_transact_notify_change\n"));
1754 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
1759 char *filter_string
;
1761 if (!(filter_string
= notify_filter_string(NULL
, filter
))) {
1762 reply_nterror(req
,NT_STATUS_NO_MEMORY
);
1766 DEBUG(3,("call_nt_transact_notify_change: notify change "
1767 "called on %s, filter = %s, recursive = %d\n",
1768 fsp_str_dbg(fsp
), filter_string
, recursive
));
1770 TALLOC_FREE(filter_string
);
1773 if((!fsp
->is_directory
) || (conn
!= fsp
->conn
)) {
1774 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1778 if (fsp
->notify
== NULL
) {
1780 status
= change_notify_create(fsp
, filter
, recursive
);
1782 if (!NT_STATUS_IS_OK(status
)) {
1783 DEBUG(10, ("change_notify_create returned %s\n",
1784 nt_errstr(status
)));
1785 reply_nterror(req
, status
);
1790 if (change_notify_fsp_has_changes(fsp
)) {
1793 * We've got changes pending, respond immediately
1797 * TODO: write a torture test to check the filtering behaviour
1801 change_notify_reply(req
,
1805 smbd_smb1_notify_reply
);
1808 * change_notify_reply() above has independently sent its
1815 * No changes pending, queue the request
1818 status
= change_notify_add_request(req
,
1822 smbd_smb1_notify_reply
);
1823 if (!NT_STATUS_IS_OK(status
)) {
1824 reply_nterror(req
, status
);
1829 /****************************************************************************
1830 Reply to an NT transact rename command.
1831 ****************************************************************************/
1833 static void call_nt_transact_rename(connection_struct
*conn
,
1834 struct smb_request
*req
,
1835 uint16
**ppsetup
, uint32 setup_count
,
1836 char **ppparams
, uint32 parameter_count
,
1837 char **ppdata
, uint32 data_count
,
1838 uint32 max_data_count
)
1840 char *params
= *ppparams
;
1841 char *new_name
= NULL
;
1842 files_struct
*fsp
= NULL
;
1843 bool dest_has_wcard
= False
;
1845 TALLOC_CTX
*ctx
= talloc_tos();
1847 if(parameter_count
< 5) {
1848 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1852 fsp
= file_fsp(req
, SVAL(params
, 0));
1853 if (!check_fsp(conn
, req
, fsp
)) {
1856 srvstr_get_path_wcard(ctx
, params
, req
->flags2
, &new_name
, params
+4,
1857 parameter_count
- 4,
1858 STR_TERMINATE
, &status
, &dest_has_wcard
);
1859 if (!NT_STATUS_IS_OK(status
)) {
1860 reply_nterror(req
, status
);
1865 * W2K3 ignores this request as the RAW-RENAME test
1866 * demonstrates, so we do.
1868 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
1870 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1871 fsp_str_dbg(fsp
), new_name
));
1876 /******************************************************************************
1877 Fake up a completely empty SD.
1878 *******************************************************************************/
1880 static NTSTATUS
get_null_nt_acl(TALLOC_CTX
*mem_ctx
, struct security_descriptor
**ppsd
)
1884 *ppsd
= make_standard_sec_desc( mem_ctx
, &global_sid_World
, &global_sid_World
, NULL
, &sd_size
);
1886 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1887 return NT_STATUS_NO_MEMORY
;
1890 return NT_STATUS_OK
;
1893 /****************************************************************************
1894 Reply to query a security descriptor.
1895 Callable from SMB2 and SMB2.
1896 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1898 ****************************************************************************/
1900 NTSTATUS
smbd_do_query_security_desc(connection_struct
*conn
,
1901 TALLOC_CTX
*mem_ctx
,
1903 uint32_t security_info_wanted
,
1904 uint32_t max_data_count
,
1905 uint8_t **ppmarshalled_sd
,
1909 struct security_descriptor
*psd
= NULL
;
1910 TALLOC_CTX
*frame
= talloc_stackframe();
1913 * Get the permissions to return.
1916 if ((security_info_wanted
& SECINFO_SACL
) &&
1917 !(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
1918 DEBUG(10, ("Access to SACL denied.\n"));
1920 return NT_STATUS_ACCESS_DENIED
;
1923 if ((security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|SECINFO_GROUP
)) &&
1924 !(fsp
->access_mask
& SEC_STD_READ_CONTROL
)) {
1925 DEBUG(10, ("Access to DACL, OWNER, or GROUP denied.\n"));
1927 return NT_STATUS_ACCESS_DENIED
;
1930 if (security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|
1931 SECINFO_GROUP
|SECINFO_SACL
)) {
1932 /* Don't return SECINFO_LABEL if anything else was
1933 requested. See bug #8458. */
1934 security_info_wanted
&= ~SECINFO_LABEL
;
1937 if (!lp_nt_acl_support(SNUM(conn
))) {
1938 status
= get_null_nt_acl(frame
, &psd
);
1939 } else if (security_info_wanted
& SECINFO_LABEL
) {
1940 /* Like W2K3 return a null object. */
1941 status
= get_null_nt_acl(frame
, &psd
);
1943 status
= SMB_VFS_FGET_NT_ACL(
1944 fsp
, security_info_wanted
, frame
, &psd
);
1946 if (!NT_STATUS_IS_OK(status
)) {
1951 if (!(security_info_wanted
& SECINFO_OWNER
)) {
1952 psd
->owner_sid
= NULL
;
1954 if (!(security_info_wanted
& SECINFO_GROUP
)) {
1955 psd
->group_sid
= NULL
;
1957 if (!(security_info_wanted
& SECINFO_DACL
)) {
1958 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
1961 if (!(security_info_wanted
& SECINFO_SACL
)) {
1962 psd
->type
&= ~SEC_DESC_SACL_PRESENT
;
1966 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1967 * present in the reply to match Windows behavior */
1968 if (psd
->sacl
== NULL
&&
1969 security_info_wanted
& SECINFO_SACL
)
1970 psd
->type
|= SEC_DESC_SACL_PRESENT
;
1971 if (psd
->dacl
== NULL
&&
1972 security_info_wanted
& SECINFO_DACL
)
1973 psd
->type
|= SEC_DESC_DACL_PRESENT
;
1975 if (security_info_wanted
& SECINFO_LABEL
) {
1976 /* Like W2K3 return a null object. */
1977 psd
->owner_sid
= NULL
;
1978 psd
->group_sid
= NULL
;
1981 psd
->type
&= ~(SEC_DESC_DACL_PRESENT
|SEC_DESC_SACL_PRESENT
);
1984 *psd_size
= ndr_size_security_descriptor(psd
, 0);
1986 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1987 (unsigned long)*psd_size
));
1989 if (DEBUGLEVEL
>= 10) {
1990 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1992 NDR_PRINT_DEBUG(security_descriptor
, psd
);
1995 if (max_data_count
< *psd_size
) {
1997 return NT_STATUS_BUFFER_TOO_SMALL
;
2000 status
= marshall_sec_desc(mem_ctx
, psd
,
2001 ppmarshalled_sd
, psd_size
);
2003 if (!NT_STATUS_IS_OK(status
)) {
2009 return NT_STATUS_OK
;
2012 /****************************************************************************
2013 SMB1 reply to query a security descriptor.
2014 ****************************************************************************/
2016 static void call_nt_transact_query_security_desc(connection_struct
*conn
,
2017 struct smb_request
*req
,
2021 uint32 parameter_count
,
2024 uint32 max_data_count
)
2026 char *params
= *ppparams
;
2027 char *data
= *ppdata
;
2029 uint32 security_info_wanted
;
2030 files_struct
*fsp
= NULL
;
2032 uint8_t *marshalled_sd
= NULL
;
2034 if(parameter_count
< 8) {
2035 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2039 fsp
= file_fsp(req
, SVAL(params
,0));
2041 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2045 security_info_wanted
= IVAL(params
,4);
2047 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
2048 "info_wanted = 0x%x\n", fsp_str_dbg(fsp
),
2049 (unsigned int)security_info_wanted
));
2051 params
= nttrans_realloc(ppparams
, 4);
2052 if(params
== NULL
) {
2053 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2058 * Get the permissions to return.
2061 status
= smbd_do_query_security_desc(conn
,
2064 security_info_wanted
,
2069 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
2070 SIVAL(params
,0,(uint32_t)sd_size
);
2071 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2072 params
, 4, NULL
, 0);
2076 if (!NT_STATUS_IS_OK(status
)) {
2077 reply_nterror(req
, status
);
2081 SMB_ASSERT(sd_size
> 0);
2083 SIVAL(params
,0,(uint32_t)sd_size
);
2085 if (max_data_count
< sd_size
) {
2086 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2087 params
, 4, NULL
, 0);
2092 * Allocate the data we will return.
2095 data
= nttrans_realloc(ppdata
, sd_size
);
2097 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2101 memcpy(data
, marshalled_sd
, sd_size
);
2103 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, 4, data
, (int)sd_size
);
2108 /****************************************************************************
2109 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2110 ****************************************************************************/
2112 static void call_nt_transact_set_security_desc(connection_struct
*conn
,
2113 struct smb_request
*req
,
2117 uint32 parameter_count
,
2120 uint32 max_data_count
)
2122 char *params
= *ppparams
;
2123 char *data
= *ppdata
;
2124 files_struct
*fsp
= NULL
;
2125 uint32 security_info_sent
= 0;
2128 if(parameter_count
< 8) {
2129 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2133 if((fsp
= file_fsp(req
, SVAL(params
,0))) == NULL
) {
2134 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2138 if (!CAN_WRITE(fsp
->conn
)) {
2139 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2143 if(!lp_nt_acl_support(SNUM(conn
))) {
2147 security_info_sent
= IVAL(params
,4);
2149 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2150 fsp_str_dbg(fsp
), (unsigned int)security_info_sent
));
2152 if (data_count
== 0) {
2153 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2157 status
= set_sd_blob(fsp
, (uint8
*)data
, data_count
, security_info_sent
);
2159 if (!NT_STATUS_IS_OK(status
)) {
2160 reply_nterror(req
, status
);
2165 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
2169 /****************************************************************************
2171 ****************************************************************************/
2173 static void call_nt_transact_ioctl(connection_struct
*conn
,
2174 struct smb_request
*req
,
2175 uint16
**ppsetup
, uint32 setup_count
,
2176 char **ppparams
, uint32 parameter_count
,
2177 char **ppdata
, uint32 data_count
,
2178 uint32 max_data_count
)
2186 char *out_data
= NULL
;
2187 uint32 out_data_len
= 0;
2188 char *pdata
= *ppdata
;
2189 TALLOC_CTX
*ctx
= talloc_tos();
2191 if (setup_count
!= 8) {
2192 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count
));
2193 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2197 function
= IVAL(*ppsetup
, 0);
2198 fidnum
= SVAL(*ppsetup
, 4);
2199 isFSctl
= CVAL(*ppsetup
, 6);
2200 compfilter
= CVAL(*ppsetup
, 7);
2202 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2203 function
, fidnum
, isFSctl
, compfilter
));
2205 fsp
=file_fsp(req
, fidnum
);
2208 * We don't really implement IOCTLs, especially on files.
2211 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2213 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2217 /* Has to be for an open file! */
2218 if (!check_fsp_open(conn
, req
, fsp
)) {
2222 SMB_PERFCOUNT_SET_IOCTL(&req
->pcd
, function
);
2225 * out_data might be allocated by the VFS module, but talloc should be
2226 * used, and should be cleaned up when the request ends.
2228 status
= SMB_VFS_FSCTL(fsp
,
2234 (uint8_t **)&out_data
,
2237 if (!NT_STATUS_IS_OK(status
)) {
2238 reply_nterror(req
, status
);
2240 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, out_data
, out_data_len
);
2245 #ifdef HAVE_SYS_QUOTAS
2246 /****************************************************************************
2247 Reply to get user quota
2248 ****************************************************************************/
2250 static void call_nt_transact_get_user_quota(connection_struct
*conn
,
2251 struct smb_request
*req
,
2255 uint32 parameter_count
,
2258 uint32 max_data_count
)
2260 NTSTATUS nt_status
= NT_STATUS_OK
;
2261 char *params
= *ppparams
;
2262 char *pdata
= *ppdata
;
2264 int data_len
=0,param_len
=0;
2267 files_struct
*fsp
= NULL
;
2271 bool start_enum
= True
;
2272 SMB_NTQUOTA_STRUCT qt
;
2273 SMB_NTQUOTA_LIST
*tmp_list
;
2274 SMB_NTQUOTA_HANDLE
*qt_handle
= NULL
;
2279 if (get_current_uid(conn
) != 0) {
2280 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2281 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn
)),
2282 conn
->session_info
->unix_info
->unix_name
));
2283 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2288 * Ensure minimum number of parameters sent.
2291 if (parameter_count
< 4) {
2292 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count
));
2293 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2297 /* maybe we can check the quota_fnum */
2298 fsp
= file_fsp(req
, SVAL(params
,0));
2299 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2300 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2301 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2305 /* the NULL pointer checking for fsp->fake_file_handle->pd
2306 * is done by CHECK_NTQUOTA_HANDLE_OK()
2308 qt_handle
= (SMB_NTQUOTA_HANDLE
*)fsp
->fake_file_handle
->private_data
;
2310 level
= SVAL(params
,2);
2312 /* unknown 12 bytes leading in params */
2315 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE
:
2316 /* seems that we should continue with the enum here --metze */
2318 if (qt_handle
->quota_list
!=NULL
&&
2319 qt_handle
->tmp_list
==NULL
) {
2322 free_ntquota_list(&(qt_handle
->quota_list
));
2324 /* Realloc the size of parameters and data we will return */
2326 params
= nttrans_realloc(ppparams
, param_len
);
2327 if(params
== NULL
) {
2328 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2333 SIVAL(params
,0,data_len
);
2340 case TRANSACT_GET_USER_QUOTA_LIST_START
:
2342 if (qt_handle
->quota_list
==NULL
&&
2343 qt_handle
->tmp_list
==NULL
) {
2347 if (start_enum
&& vfs_get_user_ntquota_list(fsp
,&(qt_handle
->quota_list
))!=0) {
2348 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2352 /* Realloc the size of parameters and data we will return */
2354 params
= nttrans_realloc(ppparams
, param_len
);
2355 if(params
== NULL
) {
2356 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2360 /* we should not trust the value in max_data_count*/
2361 max_data_count
= MIN(max_data_count
,2048);
2363 pdata
= nttrans_realloc(ppdata
, max_data_count
);/* should be max data count from client*/
2365 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2371 /* set params Size of returned Quota Data 4 bytes*/
2372 /* but set it later when we know it */
2374 /* for each entry push the data */
2377 qt_handle
->tmp_list
= qt_handle
->quota_list
;
2380 tmp_list
= qt_handle
->tmp_list
;
2382 for (;((tmp_list
!=NULL
)&&((qt_len
+40+SID_MAX_SIZE
)<max_data_count
));
2383 tmp_list
=tmp_list
->next
,entry
+=entry_len
,qt_len
+=entry_len
) {
2385 sid_len
= ndr_size_dom_sid(
2386 &tmp_list
->quotas
->sid
, 0);
2387 entry_len
= 40 + sid_len
;
2389 /* nextoffset entry 4 bytes */
2390 SIVAL(entry
,0,entry_len
);
2392 /* then the len of the SID 4 bytes */
2393 SIVAL(entry
,4,sid_len
);
2395 /* unknown data 8 bytes uint64_t */
2396 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2398 /* the used disk space 8 bytes uint64_t */
2399 SBIG_UINT(entry
,16,tmp_list
->quotas
->usedspace
);
2401 /* the soft quotas 8 bytes uint64_t */
2402 SBIG_UINT(entry
,24,tmp_list
->quotas
->softlim
);
2404 /* the hard quotas 8 bytes uint64_t */
2405 SBIG_UINT(entry
,32,tmp_list
->quotas
->hardlim
);
2407 /* and now the SID */
2408 sid_linearize(entry
+40, sid_len
, &tmp_list
->quotas
->sid
);
2411 qt_handle
->tmp_list
= tmp_list
;
2413 /* overwrite the offset of the last entry */
2414 SIVAL(entry
-entry_len
,0,0);
2416 data_len
= 4+qt_len
;
2417 /* overwrite the params quota_data_len */
2418 SIVAL(params
,0,data_len
);
2422 case TRANSACT_GET_USER_QUOTA_FOR_SID
:
2424 /* unknown 4 bytes IVAL(pdata,0) */
2426 if (data_count
< 8) {
2427 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count
,8));
2428 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2432 sid_len
= IVAL(pdata
,4);
2433 /* Ensure this is less than 1mb. */
2434 if (sid_len
> (1024*1024)) {
2435 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2439 if (data_count
< 8+sid_len
) {
2440 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count
,(unsigned long)(8+sid_len
)));
2441 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2445 data_len
= 4+40+sid_len
;
2447 if (max_data_count
< data_len
) {
2448 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2449 max_data_count
, data_len
));
2451 SIVAL(params
,0,data_len
);
2453 nt_status
= NT_STATUS_BUFFER_TOO_SMALL
;
2457 if (!sid_parse(pdata
+8,sid_len
,&sid
)) {
2458 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2462 if (vfs_get_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2465 * we have to return zero's in all fields
2466 * instead of returning an error here
2471 /* Realloc the size of parameters and data we will return */
2473 params
= nttrans_realloc(ppparams
, param_len
);
2474 if(params
== NULL
) {
2475 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2479 pdata
= nttrans_realloc(ppdata
, data_len
);
2481 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2487 /* set params Size of returned Quota Data 4 bytes*/
2488 SIVAL(params
,0,data_len
);
2490 /* nextoffset entry 4 bytes */
2493 /* then the len of the SID 4 bytes */
2494 SIVAL(entry
,4,sid_len
);
2496 /* unknown data 8 bytes uint64_t */
2497 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2499 /* the used disk space 8 bytes uint64_t */
2500 SBIG_UINT(entry
,16,qt
.usedspace
);
2502 /* the soft quotas 8 bytes uint64_t */
2503 SBIG_UINT(entry
,24,qt
.softlim
);
2505 /* the hard quotas 8 bytes uint64_t */
2506 SBIG_UINT(entry
,32,qt
.hardlim
);
2508 /* and now the SID */
2509 sid_linearize(entry
+40, sid_len
, &sid
);
2514 DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
2516 fsp_fnum_dbg(fsp
), level
));
2517 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2522 send_nt_replies(conn
, req
, nt_status
, params
, param_len
,
2526 /****************************************************************************
2527 Reply to set user quota
2528 ****************************************************************************/
2530 static void call_nt_transact_set_user_quota(connection_struct
*conn
,
2531 struct smb_request
*req
,
2535 uint32 parameter_count
,
2538 uint32 max_data_count
)
2540 char *params
= *ppparams
;
2541 char *pdata
= *ppdata
;
2542 int data_len
=0,param_len
=0;
2543 SMB_NTQUOTA_STRUCT qt
;
2546 files_struct
*fsp
= NULL
;
2551 if (get_current_uid(conn
) != 0) {
2552 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2553 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn
)),
2554 conn
->session_info
->unix_info
->unix_name
));
2555 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2560 * Ensure minimum number of parameters sent.
2563 if (parameter_count
< 2) {
2564 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count
));
2565 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2569 /* maybe we can check the quota_fnum */
2570 fsp
= file_fsp(req
, SVAL(params
,0));
2571 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2572 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2573 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2577 if (data_count
< 40) {
2578 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count
,40));
2579 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2583 /* offset to next quota record.
2584 * 4 bytes IVAL(pdata,0)
2589 sid_len
= IVAL(pdata
,4);
2591 if (data_count
< 40+sid_len
|| (40+sid_len
< sid_len
)) {
2592 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count
,(unsigned long)40+sid_len
));
2593 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2597 /* unknown 8 bytes in pdata
2598 * maybe its the change time in NTTIME
2601 /* the used space 8 bytes (uint64_t)*/
2602 qt
.usedspace
= BVAL(pdata
,16);
2604 /* the soft quotas 8 bytes (uint64_t)*/
2605 qt
.softlim
= BVAL(pdata
,24);
2607 /* the hard quotas 8 bytes (uint64_t)*/
2608 qt
.hardlim
= BVAL(pdata
,32);
2610 if (!sid_parse(pdata
+40,sid_len
,&sid
)) {
2611 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2615 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid
)));
2617 /* 44 unknown bytes left... */
2619 if (vfs_set_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2620 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2624 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
,
2627 #endif /* HAVE_SYS_QUOTAS */
2629 static void handle_nttrans(connection_struct
*conn
,
2630 struct trans_state
*state
,
2631 struct smb_request
*req
)
2633 if (get_Protocol() >= PROTOCOL_NT1
) {
2634 req
->flags2
|= 0x40; /* IS_LONG_NAME */
2635 SSVAL(discard_const_p(uint8_t, req
->inbuf
),smb_flg2
,req
->flags2
);
2639 SMB_PERFCOUNT_SET_SUBOP(&req
->pcd
, state
->call
);
2641 /* Now we must call the relevant NT_TRANS function */
2642 switch(state
->call
) {
2643 case NT_TRANSACT_CREATE
:
2645 START_PROFILE(NT_transact_create
);
2646 call_nt_transact_create(
2648 &state
->setup
, state
->setup_count
,
2649 &state
->param
, state
->total_param
,
2650 &state
->data
, state
->total_data
,
2651 state
->max_data_return
);
2652 END_PROFILE(NT_transact_create
);
2656 case NT_TRANSACT_IOCTL
:
2658 START_PROFILE(NT_transact_ioctl
);
2659 call_nt_transact_ioctl(
2661 &state
->setup
, state
->setup_count
,
2662 &state
->param
, state
->total_param
,
2663 &state
->data
, state
->total_data
,
2664 state
->max_data_return
);
2665 END_PROFILE(NT_transact_ioctl
);
2669 case NT_TRANSACT_SET_SECURITY_DESC
:
2671 START_PROFILE(NT_transact_set_security_desc
);
2672 call_nt_transact_set_security_desc(
2674 &state
->setup
, state
->setup_count
,
2675 &state
->param
, state
->total_param
,
2676 &state
->data
, state
->total_data
,
2677 state
->max_data_return
);
2678 END_PROFILE(NT_transact_set_security_desc
);
2682 case NT_TRANSACT_NOTIFY_CHANGE
:
2684 START_PROFILE(NT_transact_notify_change
);
2685 call_nt_transact_notify_change(
2687 &state
->setup
, state
->setup_count
,
2688 &state
->param
, state
->total_param
,
2689 &state
->data
, state
->total_data
,
2690 state
->max_data_return
,
2691 state
->max_param_return
);
2692 END_PROFILE(NT_transact_notify_change
);
2696 case NT_TRANSACT_RENAME
:
2698 START_PROFILE(NT_transact_rename
);
2699 call_nt_transact_rename(
2701 &state
->setup
, state
->setup_count
,
2702 &state
->param
, state
->total_param
,
2703 &state
->data
, state
->total_data
,
2704 state
->max_data_return
);
2705 END_PROFILE(NT_transact_rename
);
2709 case NT_TRANSACT_QUERY_SECURITY_DESC
:
2711 START_PROFILE(NT_transact_query_security_desc
);
2712 call_nt_transact_query_security_desc(
2714 &state
->setup
, state
->setup_count
,
2715 &state
->param
, state
->total_param
,
2716 &state
->data
, state
->total_data
,
2717 state
->max_data_return
);
2718 END_PROFILE(NT_transact_query_security_desc
);
2722 #ifdef HAVE_SYS_QUOTAS
2723 case NT_TRANSACT_GET_USER_QUOTA
:
2725 START_PROFILE(NT_transact_get_user_quota
);
2726 call_nt_transact_get_user_quota(
2728 &state
->setup
, state
->setup_count
,
2729 &state
->param
, state
->total_param
,
2730 &state
->data
, state
->total_data
,
2731 state
->max_data_return
);
2732 END_PROFILE(NT_transact_get_user_quota
);
2736 case NT_TRANSACT_SET_USER_QUOTA
:
2738 START_PROFILE(NT_transact_set_user_quota
);
2739 call_nt_transact_set_user_quota(
2741 &state
->setup
, state
->setup_count
,
2742 &state
->param
, state
->total_param
,
2743 &state
->data
, state
->total_data
,
2744 state
->max_data_return
);
2745 END_PROFILE(NT_transact_set_user_quota
);
2748 #endif /* HAVE_SYS_QUOTAS */
2751 /* Error in request */
2752 DEBUG(0,("handle_nttrans: Unknown request %d in "
2753 "nttrans call\n", state
->call
));
2754 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2760 /****************************************************************************
2761 Reply to a SMBNTtrans.
2762 ****************************************************************************/
2764 void reply_nttrans(struct smb_request
*req
)
2766 connection_struct
*conn
= req
->conn
;
2771 uint16 function_code
;
2773 struct trans_state
*state
;
2775 START_PROFILE(SMBnttrans
);
2777 if (req
->wct
< 19) {
2778 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2779 END_PROFILE(SMBnttrans
);
2783 pscnt
= IVAL(req
->vwv
+9, 1);
2784 psoff
= IVAL(req
->vwv
+11, 1);
2785 dscnt
= IVAL(req
->vwv
+13, 1);
2786 dsoff
= IVAL(req
->vwv
+15, 1);
2787 function_code
= SVAL(req
->vwv
+18, 0);
2789 if (IS_IPC(conn
) && (function_code
!= NT_TRANSACT_CREATE
)) {
2790 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2791 END_PROFILE(SMBnttrans
);
2795 result
= allow_new_trans(conn
->pending_trans
, req
->mid
);
2796 if (!NT_STATUS_IS_OK(result
)) {
2797 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result
)));
2798 reply_nterror(req
, result
);
2799 END_PROFILE(SMBnttrans
);
2803 if ((state
= talloc(conn
, struct trans_state
)) == NULL
) {
2804 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2805 END_PROFILE(SMBnttrans
);
2809 state
->cmd
= SMBnttrans
;
2811 state
->mid
= req
->mid
;
2812 state
->vuid
= req
->vuid
;
2813 state
->total_data
= IVAL(req
->vwv
+3, 1);
2815 state
->total_param
= IVAL(req
->vwv
+1, 1);
2816 state
->param
= NULL
;
2817 state
->max_data_return
= IVAL(req
->vwv
+7, 1);
2818 state
->max_param_return
= IVAL(req
->vwv
+5, 1);
2820 /* setup count is in *words* */
2821 state
->setup_count
= 2*CVAL(req
->vwv
+17, 1);
2822 state
->setup
= NULL
;
2823 state
->call
= function_code
;
2825 DEBUG(10, ("num_setup=%u, "
2826 "param_total=%u, this_param=%u, max_param=%u, "
2827 "data_total=%u, this_data=%u, max_data=%u, "
2828 "param_offset=%u, data_offset=%u\n",
2829 (unsigned)state
->setup_count
,
2830 (unsigned)state
->total_param
, (unsigned)pscnt
,
2831 (unsigned)state
->max_param_return
,
2832 (unsigned)state
->total_data
, (unsigned)dscnt
,
2833 (unsigned)state
->max_data_return
,
2834 (unsigned)psoff
, (unsigned)dsoff
));
2837 * All nttrans messages we handle have smb_wct == 19 +
2838 * state->setup_count. Ensure this is so as a sanity check.
2841 if(req
->wct
!= 19 + (state
->setup_count
/2)) {
2842 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2843 req
->wct
, 19 + (state
->setup_count
/2)));
2847 /* Don't allow more than 128mb for each value. */
2848 if ((state
->total_data
> (1024*1024*128)) ||
2849 (state
->total_param
> (1024*1024*128))) {
2850 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2851 END_PROFILE(SMBnttrans
);
2855 if ((dscnt
> state
->total_data
) || (pscnt
> state
->total_param
))
2858 if (state
->total_data
) {
2860 if (trans_oob(state
->total_data
, 0, dscnt
)
2861 || trans_oob(smb_len(req
->inbuf
), dsoff
, dscnt
)) {
2865 /* Can't use talloc here, the core routines do realloc on the
2866 * params and data. */
2867 if ((state
->data
= (char *)SMB_MALLOC(state
->total_data
)) == NULL
) {
2868 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2869 "bytes !\n", (unsigned int)state
->total_data
));
2871 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2872 END_PROFILE(SMBnttrans
);
2876 memcpy(state
->data
,smb_base(req
->inbuf
)+dsoff
,dscnt
);
2879 if (state
->total_param
) {
2881 if (trans_oob(state
->total_param
, 0, pscnt
)
2882 || trans_oob(smb_len(req
->inbuf
), psoff
, pscnt
)) {
2886 /* Can't use talloc here, the core routines do realloc on the
2887 * params and data. */
2888 if ((state
->param
= (char *)SMB_MALLOC(state
->total_param
)) == NULL
) {
2889 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2890 "bytes !\n", (unsigned int)state
->total_param
));
2891 SAFE_FREE(state
->data
);
2893 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2894 END_PROFILE(SMBnttrans
);
2898 memcpy(state
->param
,smb_base(req
->inbuf
)+psoff
,pscnt
);
2901 state
->received_data
= dscnt
;
2902 state
->received_param
= pscnt
;
2904 if(state
->setup_count
> 0) {
2905 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2906 state
->setup_count
));
2909 * No overflow possible here, state->setup_count is an
2910 * unsigned int, being filled by a single byte from
2911 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2912 * below is not necessary, it's here to clarify things. The
2913 * validity of req->vwv and req->wct has been checked in
2914 * init_smb_request already.
2916 if ((state
->setup_count
/2) + 19 > (unsigned int)req
->wct
) {
2920 state
->setup
= (uint16
*)TALLOC(state
, state
->setup_count
);
2921 if (state
->setup
== NULL
) {
2922 DEBUG(0,("reply_nttrans : Out of memory\n"));
2923 SAFE_FREE(state
->data
);
2924 SAFE_FREE(state
->param
);
2926 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2927 END_PROFILE(SMBnttrans
);
2931 memcpy(state
->setup
, req
->vwv
+19, state
->setup_count
);
2932 dump_data(10, (uint8
*)state
->setup
, state
->setup_count
);
2935 if ((state
->received_data
== state
->total_data
) &&
2936 (state
->received_param
== state
->total_param
)) {
2937 handle_nttrans(conn
, state
, req
);
2938 SAFE_FREE(state
->param
);
2939 SAFE_FREE(state
->data
);
2941 END_PROFILE(SMBnttrans
);
2945 DLIST_ADD(conn
->pending_trans
, state
);
2947 /* We need to send an interim response then receive the rest
2948 of the parameter/data bytes */
2949 reply_outbuf(req
, 0, 0);
2950 show_msg((char *)req
->outbuf
);
2951 END_PROFILE(SMBnttrans
);
2956 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2957 SAFE_FREE(state
->data
);
2958 SAFE_FREE(state
->param
);
2960 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2961 END_PROFILE(SMBnttrans
);
2965 /****************************************************************************
2966 Reply to a SMBnttranss
2967 ****************************************************************************/
2969 void reply_nttranss(struct smb_request
*req
)
2971 connection_struct
*conn
= req
->conn
;
2972 uint32_t pcnt
,poff
,dcnt
,doff
,pdisp
,ddisp
;
2973 struct trans_state
*state
;
2975 START_PROFILE(SMBnttranss
);
2977 show_msg((const char *)req
->inbuf
);
2979 /* Windows clients expect all replies to
2980 an NT transact secondary (SMBnttranss 0xA1)
2981 to have a command code of NT transact
2982 (SMBnttrans 0xA0). See bug #8989 for details. */
2983 req
->cmd
= SMBnttrans
;
2985 if (req
->wct
< 18) {
2986 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2987 END_PROFILE(SMBnttranss
);
2991 for (state
= conn
->pending_trans
; state
!= NULL
;
2992 state
= state
->next
) {
2993 if (state
->mid
== req
->mid
) {
2998 if ((state
== NULL
) || (state
->cmd
!= SMBnttrans
)) {
2999 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3000 END_PROFILE(SMBnttranss
);
3004 /* Revise state->total_param and state->total_data in case they have
3005 changed downwards */
3006 if (IVAL(req
->vwv
+1, 1) < state
->total_param
) {
3007 state
->total_param
= IVAL(req
->vwv
+1, 1);
3009 if (IVAL(req
->vwv
+3, 1) < state
->total_data
) {
3010 state
->total_data
= IVAL(req
->vwv
+3, 1);
3013 pcnt
= IVAL(req
->vwv
+5, 1);
3014 poff
= IVAL(req
->vwv
+7, 1);
3015 pdisp
= IVAL(req
->vwv
+9, 1);
3017 dcnt
= IVAL(req
->vwv
+11, 1);
3018 doff
= IVAL(req
->vwv
+13, 1);
3019 ddisp
= IVAL(req
->vwv
+15, 1);
3021 state
->received_param
+= pcnt
;
3022 state
->received_data
+= dcnt
;
3024 if ((state
->received_data
> state
->total_data
) ||
3025 (state
->received_param
> state
->total_param
))
3029 if (trans_oob(state
->total_param
, pdisp
, pcnt
)
3030 || trans_oob(smb_len(req
->inbuf
), poff
, pcnt
)) {
3033 memcpy(state
->param
+pdisp
, smb_base(req
->inbuf
)+poff
,pcnt
);
3037 if (trans_oob(state
->total_data
, ddisp
, dcnt
)
3038 || trans_oob(smb_len(req
->inbuf
), doff
, dcnt
)) {
3041 memcpy(state
->data
+ddisp
, smb_base(req
->inbuf
)+doff
,dcnt
);
3044 if ((state
->received_param
< state
->total_param
) ||
3045 (state
->received_data
< state
->total_data
)) {
3046 END_PROFILE(SMBnttranss
);
3050 handle_nttrans(conn
, state
, req
);
3052 DLIST_REMOVE(conn
->pending_trans
, state
);
3053 SAFE_FREE(state
->data
);
3054 SAFE_FREE(state
->param
);
3056 END_PROFILE(SMBnttranss
);
3061 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3062 DLIST_REMOVE(conn
->pending_trans
, state
);
3063 SAFE_FREE(state
->data
);
3064 SAFE_FREE(state
->param
);
3066 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3067 END_PROFILE(SMBnttranss
);