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
||
721 can_write_to_file(conn
, smb_fname
)) {
722 perms
= FILE_GENERIC_ALL
;
724 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
729 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
730 fsp_fnum_dbg(fsp
), smb_fname_str_dbg(smb_fname
)));
733 END_PROFILE(SMBntcreateX
);
737 /****************************************************************************
738 Reply to a NT_TRANSACT_CREATE call to open a pipe.
739 ****************************************************************************/
741 static void do_nt_transact_create_pipe(connection_struct
*conn
,
742 struct smb_request
*req
,
743 uint16
**ppsetup
, uint32 setup_count
,
744 char **ppparams
, uint32 parameter_count
,
745 char **ppdata
, uint32 data_count
)
748 char *params
= *ppparams
;
749 uint16_t pnum
= FNUM_FIELD_INVALID
;
754 TALLOC_CTX
*ctx
= talloc_tos();
757 * Ensure minimum number of parameters sent.
760 if(parameter_count
< 54) {
761 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
762 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
766 flags
= IVAL(params
,0);
768 srvstr_get_path(ctx
, params
, req
->flags2
, &fname
, params
+53,
769 parameter_count
-53, STR_TERMINATE
,
771 if (!NT_STATUS_IS_OK(status
)) {
772 reply_nterror(req
, status
);
776 nt_open_pipe(fname
, conn
, req
, &pnum
);
783 /* Realloc the size of parameters and data we will return */
784 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
785 /* Extended response is 32 more byyes. */
790 params
= nttrans_realloc(ppparams
, param_len
);
792 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
797 SCVAL(p
,0,NO_OPLOCK_RETURN
);
802 SIVAL(p
,0,FILE_WAS_OPENED
);
806 SIVAL(p
,0,FILE_ATTRIBUTE_NORMAL
); /* File Attributes. */
809 SSVAL(p
,0,FILE_TYPE_MESSAGE_MODE_PIPE
);
811 SSVAL(p
,2, 0x5FF); /* ? */
814 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
816 SIVAL(p
,0,FILE_GENERIC_ALL
);
818 * For pipes W2K3 seems to return
820 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
822 SIVAL(p
,4,(FILE_GENERIC_READ
|FILE_GENERIC_WRITE
)&~FILE_APPEND_DATA
);
825 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname
));
827 /* Send the required number of replies */
828 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
833 /*********************************************************************
834 Windows seems to do canonicalization of inheritance bits. Do the
836 *********************************************************************/
838 static void canonicalize_inheritance_bits(struct security_descriptor
*psd
)
840 bool set_auto_inherited
= false;
843 * We need to filter out the
844 * SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ
845 * bits. If both are set we store SEC_DESC_DACL_AUTO_INHERITED
846 * as this alters whether SEC_ACE_FLAG_INHERITED_ACE is set
847 * when an ACE is inherited. Otherwise we zero these bits out.
850 * http://social.msdn.microsoft.com/Forums/eu/os_fileservices/thread/11f77b68-731e-407d-b1b3-064750716531
855 if ((psd
->type
& (SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
))
856 == (SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
)) {
857 set_auto_inherited
= true;
860 psd
->type
&= ~(SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
);
861 if (set_auto_inherited
) {
862 psd
->type
|= SEC_DESC_DACL_AUTO_INHERITED
;
866 /****************************************************************************
867 Internal fn to set security descriptors.
868 ****************************************************************************/
870 NTSTATUS
set_sd(files_struct
*fsp
, struct security_descriptor
*psd
,
871 uint32_t security_info_sent
)
875 if (!CAN_WRITE(fsp
->conn
)) {
876 return NT_STATUS_ACCESS_DENIED
;
879 if (!lp_nt_acl_support(SNUM(fsp
->conn
))) {
883 if (psd
->owner_sid
== NULL
) {
884 security_info_sent
&= ~SECINFO_OWNER
;
886 if (psd
->group_sid
== NULL
) {
887 security_info_sent
&= ~SECINFO_GROUP
;
890 /* Ensure we have at least one thing set. */
891 if ((security_info_sent
& (SECINFO_OWNER
|SECINFO_GROUP
|SECINFO_DACL
|SECINFO_SACL
)) == 0) {
896 /* Ensure we have the rights to do this. */
897 if (security_info_sent
& SECINFO_OWNER
) {
898 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
899 return NT_STATUS_ACCESS_DENIED
;
903 if (security_info_sent
& SECINFO_GROUP
) {
904 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
905 return NT_STATUS_ACCESS_DENIED
;
909 if (security_info_sent
& SECINFO_DACL
) {
910 if (!(fsp
->access_mask
& SEC_STD_WRITE_DAC
)) {
911 return NT_STATUS_ACCESS_DENIED
;
913 /* Convert all the generic bits. */
915 security_acl_map_generic(psd
->dacl
, &file_generic_mapping
);
919 if (security_info_sent
& SECINFO_SACL
) {
920 if (!(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
921 return NT_STATUS_ACCESS_DENIED
;
923 /* Convert all the generic bits. */
925 security_acl_map_generic(psd
->sacl
, &file_generic_mapping
);
929 canonicalize_inheritance_bits(psd
);
931 if (DEBUGLEVEL
>= 10) {
932 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp
)));
933 NDR_PRINT_DEBUG(security_descriptor
, psd
);
936 status
= SMB_VFS_FSET_NT_ACL(fsp
, security_info_sent
, psd
);
943 /****************************************************************************
944 Internal fn to set security descriptors from a data blob.
945 ****************************************************************************/
947 NTSTATUS
set_sd_blob(files_struct
*fsp
, uint8_t *data
, uint32_t sd_len
,
948 uint32_t security_info_sent
)
950 struct security_descriptor
*psd
= NULL
;
954 return NT_STATUS_INVALID_PARAMETER
;
957 status
= unmarshall_sec_desc(talloc_tos(), data
, sd_len
, &psd
);
959 if (!NT_STATUS_IS_OK(status
)) {
963 return set_sd(fsp
, psd
, security_info_sent
);
966 /****************************************************************************
967 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
968 ****************************************************************************/
970 struct ea_list
*read_nttrans_ea_list(TALLOC_CTX
*ctx
, const char *pdata
, size_t data_size
)
972 struct ea_list
*ea_list_head
= NULL
;
979 while (offset
+ 4 <= data_size
) {
980 size_t next_offset
= IVAL(pdata
,offset
);
981 struct ea_list
*eal
= read_ea_list_entry(ctx
, pdata
+ offset
+ 4, data_size
- offset
- 4, NULL
);
987 DLIST_ADD_END(ea_list_head
, eal
, struct ea_list
*);
988 if (next_offset
== 0) {
992 /* Integer wrap protection for the increment. */
993 if (offset
+ next_offset
< offset
) {
997 offset
+= next_offset
;
999 /* Integer wrap protection for while loop. */
1000 if (offset
+ 4 < offset
) {
1006 return ea_list_head
;
1009 /****************************************************************************
1010 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1011 ****************************************************************************/
1013 static void call_nt_transact_create(connection_struct
*conn
,
1014 struct smb_request
*req
,
1015 uint16
**ppsetup
, uint32 setup_count
,
1016 char **ppparams
, uint32 parameter_count
,
1017 char **ppdata
, uint32 data_count
,
1018 uint32 max_data_count
)
1020 struct smb_filename
*smb_fname
= NULL
;
1022 char *params
= *ppparams
;
1023 char *data
= *ppdata
;
1024 /* Breakout the oplock request bits so we can set the reply bits separately. */
1028 files_struct
*fsp
= NULL
;
1032 uint32 file_attributes
;
1033 uint32 share_access
;
1034 uint32 create_disposition
;
1035 uint32 create_options
;
1037 struct security_descriptor
*sd
= NULL
;
1039 uint16 root_dir_fid
;
1040 struct timespec create_timespec
;
1041 struct timespec c_timespec
;
1042 struct timespec a_timespec
;
1043 struct timespec m_timespec
;
1044 struct timespec write_time_ts
;
1045 struct ea_list
*ea_list
= NULL
;
1048 uint64_t allocation_size
;
1050 uint8_t oplock_granted
;
1051 struct case_semantics_state
*case_state
= NULL
;
1052 TALLOC_CTX
*ctx
= talloc_tos();
1054 DEBUG(5,("call_nt_transact_create\n"));
1057 * If it's an IPC, use the pipe handler.
1061 if (lp_nt_pipe_support()) {
1062 do_nt_transact_create_pipe(
1064 ppsetup
, setup_count
,
1065 ppparams
, parameter_count
,
1066 ppdata
, data_count
);
1069 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
1074 * Ensure minimum number of parameters sent.
1077 if(parameter_count
< 54) {
1078 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
1079 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1083 flags
= IVAL(params
,0);
1084 access_mask
= IVAL(params
,8);
1085 file_attributes
= IVAL(params
,20);
1086 share_access
= IVAL(params
,24);
1087 create_disposition
= IVAL(params
,28);
1088 create_options
= IVAL(params
,32);
1089 sd_len
= IVAL(params
,36);
1090 ea_len
= IVAL(params
,40);
1091 root_dir_fid
= (uint16
)IVAL(params
,4);
1092 allocation_size
= BVAL(params
,12);
1095 * we need to remove ignored bits when they come directly from the client
1096 * because we reuse some of them for internal stuff
1098 create_options
&= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK
;
1100 srvstr_get_path(ctx
, params
, req
->flags2
, &fname
,
1101 params
+53, parameter_count
-53,
1102 STR_TERMINATE
, &status
);
1103 if (!NT_STATUS_IS_OK(status
)) {
1104 reply_nterror(req
, status
);
1108 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1109 case_state
= set_posix_case_semantics(ctx
, conn
);
1111 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1116 status
= filename_convert(ctx
,
1118 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1124 TALLOC_FREE(case_state
);
1126 if (!NT_STATUS_IS_OK(status
)) {
1127 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
1128 reply_botherror(req
,
1129 NT_STATUS_PATH_NOT_COVERED
,
1130 ERRSRV
, ERRbadpath
);
1133 reply_nterror(req
, status
);
1137 /* Ensure the data_len is correct for the sd and ea values given. */
1138 if ((ea_len
+ sd_len
> data_count
)
1139 || (ea_len
> data_count
) || (sd_len
> data_count
)
1140 || (ea_len
+ sd_len
< ea_len
) || (ea_len
+ sd_len
< sd_len
)) {
1141 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1142 "%u, data_count = %u\n", (unsigned int)ea_len
,
1143 (unsigned int)sd_len
, (unsigned int)data_count
));
1144 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1149 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1152 status
= unmarshall_sec_desc(ctx
, (uint8_t *)data
, sd_len
,
1154 if (!NT_STATUS_IS_OK(status
)) {
1155 DEBUG(10, ("call_nt_transact_create: "
1156 "unmarshall_sec_desc failed: %s\n",
1157 nt_errstr(status
)));
1158 reply_nterror(req
, status
);
1164 if (!lp_ea_support(SNUM(conn
))) {
1165 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1166 "EA's not supported.\n",
1167 (unsigned int)ea_len
));
1168 reply_nterror(req
, NT_STATUS_EAS_NOT_SUPPORTED
);
1173 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1174 "too small (should be more than 10)\n",
1175 (unsigned int)ea_len
));
1176 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1180 /* We have already checked that ea_len <= data_count here. */
1181 ea_list
= read_nttrans_ea_list(talloc_tos(), data
+ sd_len
,
1183 if (ea_list
== NULL
) {
1184 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1188 if (ea_list_has_invalid_name(ea_list
)) {
1189 /* Realloc the size of parameters and data we will return */
1190 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1191 /* Extended response is 32 more byyes. */
1196 params
= nttrans_realloc(ppparams
, param_len
);
1197 if(params
== NULL
) {
1198 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1202 memset(params
, '\0', param_len
);
1203 send_nt_replies(conn
, req
, STATUS_INVALID_EA_NAME
,
1204 params
, param_len
, NULL
, 0);
1209 oplock_request
= (flags
& REQUEST_OPLOCK
) ? EXCLUSIVE_OPLOCK
: 0;
1210 if (oplock_request
) {
1211 oplock_request
|= (flags
& REQUEST_BATCH_OPLOCK
)
1216 * Bug #6898 - clients using Windows opens should
1217 * never be able to set this attribute into the
1220 file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
1222 status
= SMB_VFS_CREATE_FILE(
1225 root_dir_fid
, /* root_dir_fid */
1226 smb_fname
, /* fname */
1227 access_mask
, /* access_mask */
1228 share_access
, /* share_access */
1229 create_disposition
, /* create_disposition*/
1230 create_options
, /* create_options */
1231 file_attributes
, /* file_attributes */
1232 oplock_request
, /* oplock_request */
1233 allocation_size
, /* allocation_size */
1234 0, /* private_flags */
1236 ea_list
, /* ea_list */
1240 if(!NT_STATUS_IS_OK(status
)) {
1241 if (open_was_deferred(req
->sconn
, req
->mid
)) {
1242 /* We have re-scheduled this call, no error. */
1245 reply_openerror(req
, status
);
1249 /* Ensure we're pointing at the correct stat struct. */
1250 TALLOC_FREE(smb_fname
);
1251 smb_fname
= fsp
->fsp_name
;
1254 * If the caller set the extended oplock request bit
1255 * and we granted one (by whatever means) - set the
1256 * correct bit for extended oplock reply.
1259 if (oplock_request
&&
1260 (lp_fake_oplocks(SNUM(conn
))
1261 || EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
))) {
1264 * Exclusive oplock granted
1267 if (flags
& REQUEST_BATCH_OPLOCK
) {
1268 oplock_granted
= BATCH_OPLOCK_RETURN
;
1270 oplock_granted
= EXCLUSIVE_OPLOCK_RETURN
;
1272 } else if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
1273 oplock_granted
= LEVEL_II_OPLOCK_RETURN
;
1275 oplock_granted
= NO_OPLOCK_RETURN
;
1278 file_len
= smb_fname
->st
.st_ex_size
;
1280 /* Realloc the size of parameters and data we will return */
1281 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1282 /* Extended response is 32 more byyes. */
1287 params
= nttrans_realloc(ppparams
, param_len
);
1288 if(params
== NULL
) {
1289 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1294 SCVAL(p
, 0, oplock_granted
);
1297 SSVAL(p
,0,fsp
->fnum
);
1299 if ((create_disposition
== FILE_SUPERSEDE
)
1300 && (info
== FILE_WAS_OVERWRITTEN
)) {
1301 SIVAL(p
,0,FILE_WAS_SUPERSEDED
);
1307 fattr
= dos_mode(conn
, smb_fname
);
1309 fattr
= FILE_ATTRIBUTE_NORMAL
;
1312 /* Deal with other possible opens having a modified
1314 ZERO_STRUCT(write_time_ts
);
1315 get_file_infos(fsp
->file_id
, 0, NULL
, &write_time_ts
);
1316 if (!null_timespec(write_time_ts
)) {
1317 update_stat_ex_mtime(&smb_fname
->st
, write_time_ts
);
1321 create_timespec
= get_create_timespec(conn
, fsp
, smb_fname
);
1322 a_timespec
= smb_fname
->st
.st_ex_atime
;
1323 m_timespec
= smb_fname
->st
.st_ex_mtime
;
1324 c_timespec
= get_change_timespec(conn
, fsp
, smb_fname
);
1326 if (lp_dos_filetime_resolution(SNUM(conn
))) {
1327 dos_filetime_timespec(&create_timespec
);
1328 dos_filetime_timespec(&a_timespec
);
1329 dos_filetime_timespec(&m_timespec
);
1330 dos_filetime_timespec(&c_timespec
);
1333 put_long_date_timespec(conn
->ts_res
, p
, create_timespec
); /* create time. */
1335 put_long_date_timespec(conn
->ts_res
, p
, a_timespec
); /* access time */
1337 put_long_date_timespec(conn
->ts_res
, p
, m_timespec
); /* write time */
1339 put_long_date_timespec(conn
->ts_res
, p
, c_timespec
); /* change time */
1341 SIVAL(p
,0,fattr
); /* File Attributes. */
1343 SOFF_T(p
, 0, SMB_VFS_GET_ALLOC_SIZE(conn
, fsp
, &smb_fname
->st
));
1345 SOFF_T(p
,0,file_len
);
1347 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1348 uint16_t file_status
= (NO_EAS
|NO_SUBSTREAMS
|NO_REPARSETAG
);
1349 size_t num_names
= 0;
1350 unsigned int num_streams
= 0;
1351 struct stream_struct
*streams
= NULL
;
1353 /* Do we have any EA's ? */
1354 status
= get_ea_names_from_file(ctx
, conn
, fsp
,
1355 smb_fname
->base_name
, NULL
, &num_names
);
1356 if (NT_STATUS_IS_OK(status
) && num_names
) {
1357 file_status
&= ~NO_EAS
;
1359 status
= vfs_streaminfo(conn
, NULL
, smb_fname
->base_name
, ctx
,
1360 &num_streams
, &streams
);
1361 /* There is always one stream, ::$DATA. */
1362 if (NT_STATUS_IS_OK(status
) && num_streams
> 1) {
1363 file_status
&= ~NO_SUBSTREAMS
;
1365 TALLOC_FREE(streams
);
1366 SSVAL(p
,2,file_status
);
1369 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
1371 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1374 if (fsp
->is_directory
||
1375 can_write_to_file(conn
, smb_fname
)) {
1376 perms
= FILE_GENERIC_ALL
;
1378 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
1383 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1384 smb_fname_str_dbg(smb_fname
)));
1386 /* Send the required number of replies */
1387 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
1392 /****************************************************************************
1393 Reply to a NT CANCEL request.
1394 conn POINTER CAN BE NULL HERE !
1395 ****************************************************************************/
1397 void reply_ntcancel(struct smb_request
*req
)
1400 * Go through and cancel any pending change notifies.
1403 START_PROFILE(SMBntcancel
);
1404 srv_cancel_sign_response(req
->sconn
);
1405 remove_pending_change_notify_requests_by_mid(req
->sconn
, req
->mid
);
1406 remove_pending_lock_requests_by_mid_smb1(req
->sconn
, req
->mid
);
1408 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1409 (unsigned long long)req
->mid
));
1411 END_PROFILE(SMBntcancel
);
1415 /****************************************************************************
1417 ****************************************************************************/
1419 static NTSTATUS
copy_internals(TALLOC_CTX
*ctx
,
1420 connection_struct
*conn
,
1421 struct smb_request
*req
,
1422 struct smb_filename
*smb_fname_src
,
1423 struct smb_filename
*smb_fname_dst
,
1426 files_struct
*fsp1
,*fsp2
;
1430 NTSTATUS status
= NT_STATUS_OK
;
1433 if (!CAN_WRITE(conn
)) {
1434 status
= NT_STATUS_MEDIA_WRITE_PROTECTED
;
1438 /* Source must already exist. */
1439 if (!VALID_STAT(smb_fname_src
->st
)) {
1440 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1444 /* Ensure attributes match. */
1445 fattr
= dos_mode(conn
, smb_fname_src
);
1446 if ((fattr
& ~attrs
) & (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) {
1447 status
= NT_STATUS_NO_SUCH_FILE
;
1451 /* Disallow if dst file already exists. */
1452 if (VALID_STAT(smb_fname_dst
->st
)) {
1453 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
1457 /* No links from a directory. */
1458 if (S_ISDIR(smb_fname_src
->st
.st_ex_mode
)) {
1459 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
1463 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1464 smb_fname_str_dbg(smb_fname_src
),
1465 smb_fname_str_dbg(smb_fname_dst
)));
1467 status
= SMB_VFS_CREATE_FILE(
1470 0, /* root_dir_fid */
1471 smb_fname_src
, /* fname */
1472 FILE_READ_DATA
|FILE_READ_ATTRIBUTES
|
1473 FILE_READ_EA
, /* access_mask */
1474 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1476 FILE_OPEN
, /* create_disposition*/
1477 0, /* create_options */
1478 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
1479 NO_OPLOCK
, /* oplock_request */
1480 0, /* allocation_size */
1481 0, /* private_flags */
1487 if (!NT_STATUS_IS_OK(status
)) {
1491 status
= SMB_VFS_CREATE_FILE(
1494 0, /* root_dir_fid */
1495 smb_fname_dst
, /* fname */
1496 FILE_WRITE_DATA
|FILE_WRITE_ATTRIBUTES
|
1497 FILE_WRITE_EA
, /* access_mask */
1498 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1500 FILE_CREATE
, /* create_disposition*/
1501 0, /* create_options */
1502 fattr
, /* file_attributes */
1503 NO_OPLOCK
, /* oplock_request */
1504 0, /* allocation_size */
1505 0, /* private_flags */
1511 if (!NT_STATUS_IS_OK(status
)) {
1512 close_file(NULL
, fsp1
, ERROR_CLOSE
);
1516 if (smb_fname_src
->st
.st_ex_size
) {
1517 ret
= vfs_transfer_file(fsp1
, fsp2
, smb_fname_src
->st
.st_ex_size
);
1521 * As we are opening fsp1 read-only we only expect
1522 * an error on close on fsp2 if we are out of space.
1523 * Thus we don't look at the error return from the
1526 close_file(NULL
, fsp1
, NORMAL_CLOSE
);
1528 /* Ensure the modtime is set correctly on the destination file. */
1529 set_close_write_time(fsp2
, smb_fname_src
->st
.st_ex_mtime
);
1531 status
= close_file(NULL
, fsp2
, NORMAL_CLOSE
);
1533 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1534 creates the file. This isn't the correct thing to do in the copy
1536 if (!parent_dirname(talloc_tos(), smb_fname_dst
->base_name
, &parent
,
1538 status
= NT_STATUS_NO_MEMORY
;
1541 file_set_dosmode(conn
, smb_fname_dst
, fattr
, parent
, false);
1542 TALLOC_FREE(parent
);
1544 if (ret
< (off_t
)smb_fname_src
->st
.st_ex_size
) {
1545 status
= NT_STATUS_DISK_FULL
;
1549 if (!NT_STATUS_IS_OK(status
)) {
1550 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1551 nt_errstr(status
), smb_fname_str_dbg(smb_fname_src
),
1552 smb_fname_str_dbg(smb_fname_dst
)));
1558 /****************************************************************************
1559 Reply to a NT rename request.
1560 ****************************************************************************/
1562 void reply_ntrename(struct smb_request
*req
)
1564 connection_struct
*conn
= req
->conn
;
1565 struct smb_filename
*smb_fname_old
= NULL
;
1566 struct smb_filename
*smb_fname_new
= NULL
;
1567 char *oldname
= NULL
;
1568 char *newname
= NULL
;
1571 bool src_has_wcard
= False
;
1572 bool dest_has_wcard
= False
;
1574 uint32_t ucf_flags_src
= 0;
1575 uint32_t ucf_flags_dst
= 0;
1577 TALLOC_CTX
*ctx
= talloc_tos();
1578 bool stream_rename
= false;
1580 START_PROFILE(SMBntrename
);
1583 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1587 attrs
= SVAL(req
->vwv
+0, 0);
1588 rename_type
= SVAL(req
->vwv
+1, 0);
1590 p
= (const char *)req
->buf
+ 1;
1591 p
+= srvstr_get_path_req_wcard(ctx
, req
, &oldname
, p
, STR_TERMINATE
,
1592 &status
, &src_has_wcard
);
1593 if (!NT_STATUS_IS_OK(status
)) {
1594 reply_nterror(req
, status
);
1598 if (ms_has_wild(oldname
)) {
1599 reply_nterror(req
, NT_STATUS_OBJECT_PATH_SYNTAX_BAD
);
1604 p
+= srvstr_get_path_req_wcard(ctx
, req
, &newname
, p
, STR_TERMINATE
,
1605 &status
, &dest_has_wcard
);
1606 if (!NT_STATUS_IS_OK(status
)) {
1607 reply_nterror(req
, status
);
1611 if (!lp_posix_pathnames()) {
1612 /* The newname must begin with a ':' if the
1613 oldname contains a ':'. */
1614 if (strchr_m(oldname
, ':')) {
1615 if (newname
[0] != ':') {
1616 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1619 stream_rename
= true;
1624 * If this is a rename operation, allow wildcards and save the
1625 * destination's last component.
1627 if (rename_type
== RENAME_FLAG_RENAME
) {
1628 ucf_flags_src
= UCF_COND_ALLOW_WCARD_LCOMP
;
1629 ucf_flags_dst
= UCF_COND_ALLOW_WCARD_LCOMP
| UCF_SAVE_LCOMP
;
1632 /* rename_internals() calls unix_convert(), so don't call it here. */
1633 status
= filename_convert(ctx
, conn
,
1634 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1639 if (!NT_STATUS_IS_OK(status
)) {
1640 if (NT_STATUS_EQUAL(status
,
1641 NT_STATUS_PATH_NOT_COVERED
)) {
1642 reply_botherror(req
,
1643 NT_STATUS_PATH_NOT_COVERED
,
1644 ERRSRV
, ERRbadpath
);
1647 reply_nterror(req
, status
);
1651 status
= filename_convert(ctx
, conn
,
1652 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1657 if (!NT_STATUS_IS_OK(status
)) {
1658 if (NT_STATUS_EQUAL(status
,
1659 NT_STATUS_PATH_NOT_COVERED
)) {
1660 reply_botherror(req
,
1661 NT_STATUS_PATH_NOT_COVERED
,
1662 ERRSRV
, ERRbadpath
);
1665 reply_nterror(req
, status
);
1669 if (stream_rename
) {
1670 /* smb_fname_new must be the same as smb_fname_old. */
1671 TALLOC_FREE(smb_fname_new
->base_name
);
1672 smb_fname_new
->base_name
= talloc_strdup(smb_fname_new
,
1673 smb_fname_old
->base_name
);
1674 if (!smb_fname_new
->base_name
) {
1675 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1680 DEBUG(3,("reply_ntrename: %s -> %s\n",
1681 smb_fname_str_dbg(smb_fname_old
),
1682 smb_fname_str_dbg(smb_fname_new
)));
1684 switch(rename_type
) {
1685 case RENAME_FLAG_RENAME
:
1686 status
= rename_internals(ctx
, conn
, req
,
1687 smb_fname_old
, smb_fname_new
,
1688 attrs
, False
, src_has_wcard
,
1692 case RENAME_FLAG_HARD_LINK
:
1693 if (src_has_wcard
|| dest_has_wcard
) {
1695 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1697 status
= hardlink_internals(ctx
, conn
,
1704 case RENAME_FLAG_COPY
:
1705 if (src_has_wcard
|| dest_has_wcard
) {
1707 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1709 status
= copy_internals(ctx
, conn
, req
,
1715 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION
:
1716 status
= NT_STATUS_INVALID_PARAMETER
;
1719 status
= NT_STATUS_ACCESS_DENIED
; /* Default error. */
1723 if (!NT_STATUS_IS_OK(status
)) {
1724 if (open_was_deferred(req
->sconn
, req
->mid
)) {
1725 /* We have re-scheduled this call. */
1729 reply_nterror(req
, status
);
1733 reply_outbuf(req
, 0, 0);
1735 END_PROFILE(SMBntrename
);
1739 /****************************************************************************
1740 Reply to a notify change - queue the request and
1741 don't allow a directory to be opened.
1742 ****************************************************************************/
1744 static void smbd_smb1_notify_reply(struct smb_request
*req
,
1745 NTSTATUS error_code
,
1746 uint8_t *buf
, size_t len
)
1748 send_nt_replies(req
->conn
, req
, error_code
, (char *)buf
, len
, NULL
, 0);
1751 static void call_nt_transact_notify_change(connection_struct
*conn
,
1752 struct smb_request
*req
,
1756 uint32 parameter_count
,
1757 char **ppdata
, uint32 data_count
,
1758 uint32 max_data_count
,
1759 uint32 max_param_count
)
1761 uint16
*setup
= *ppsetup
;
1767 if(setup_count
< 6) {
1768 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1772 fsp
= file_fsp(req
, SVAL(setup
,4));
1773 filter
= IVAL(setup
, 0);
1774 recursive
= (SVAL(setup
, 6) != 0) ? True
: False
;
1776 DEBUG(3,("call_nt_transact_notify_change\n"));
1779 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
1784 char *filter_string
;
1786 if (!(filter_string
= notify_filter_string(NULL
, filter
))) {
1787 reply_nterror(req
,NT_STATUS_NO_MEMORY
);
1791 DEBUG(3,("call_nt_transact_notify_change: notify change "
1792 "called on %s, filter = %s, recursive = %d\n",
1793 fsp_str_dbg(fsp
), filter_string
, recursive
));
1795 TALLOC_FREE(filter_string
);
1798 if((!fsp
->is_directory
) || (conn
!= fsp
->conn
)) {
1799 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1803 if (fsp
->notify
== NULL
) {
1805 status
= change_notify_create(fsp
, filter
, recursive
);
1807 if (!NT_STATUS_IS_OK(status
)) {
1808 DEBUG(10, ("change_notify_create returned %s\n",
1809 nt_errstr(status
)));
1810 reply_nterror(req
, status
);
1815 if (change_notify_fsp_has_changes(fsp
)) {
1818 * We've got changes pending, respond immediately
1822 * TODO: write a torture test to check the filtering behaviour
1826 change_notify_reply(req
,
1830 smbd_smb1_notify_reply
);
1833 * change_notify_reply() above has independently sent its
1840 * No changes pending, queue the request
1843 status
= change_notify_add_request(req
,
1847 smbd_smb1_notify_reply
);
1848 if (!NT_STATUS_IS_OK(status
)) {
1849 reply_nterror(req
, status
);
1854 /****************************************************************************
1855 Reply to an NT transact rename command.
1856 ****************************************************************************/
1858 static void call_nt_transact_rename(connection_struct
*conn
,
1859 struct smb_request
*req
,
1860 uint16
**ppsetup
, uint32 setup_count
,
1861 char **ppparams
, uint32 parameter_count
,
1862 char **ppdata
, uint32 data_count
,
1863 uint32 max_data_count
)
1865 char *params
= *ppparams
;
1866 char *new_name
= NULL
;
1867 files_struct
*fsp
= NULL
;
1868 bool dest_has_wcard
= False
;
1870 TALLOC_CTX
*ctx
= talloc_tos();
1872 if(parameter_count
< 5) {
1873 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1877 fsp
= file_fsp(req
, SVAL(params
, 0));
1878 if (!check_fsp(conn
, req
, fsp
)) {
1881 srvstr_get_path_wcard(ctx
, params
, req
->flags2
, &new_name
, params
+4,
1882 parameter_count
- 4,
1883 STR_TERMINATE
, &status
, &dest_has_wcard
);
1884 if (!NT_STATUS_IS_OK(status
)) {
1885 reply_nterror(req
, status
);
1890 * W2K3 ignores this request as the RAW-RENAME test
1891 * demonstrates, so we do.
1893 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
1895 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1896 fsp_str_dbg(fsp
), new_name
));
1901 /******************************************************************************
1902 Fake up a completely empty SD.
1903 *******************************************************************************/
1905 static NTSTATUS
get_null_nt_acl(TALLOC_CTX
*mem_ctx
, struct security_descriptor
**ppsd
)
1909 *ppsd
= make_standard_sec_desc( mem_ctx
, &global_sid_World
, &global_sid_World
, NULL
, &sd_size
);
1911 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1912 return NT_STATUS_NO_MEMORY
;
1915 return NT_STATUS_OK
;
1918 /****************************************************************************
1919 Reply to query a security descriptor.
1920 Callable from SMB2 and SMB2.
1921 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1923 ****************************************************************************/
1925 NTSTATUS
smbd_do_query_security_desc(connection_struct
*conn
,
1926 TALLOC_CTX
*mem_ctx
,
1928 uint32_t security_info_wanted
,
1929 uint32_t max_data_count
,
1930 uint8_t **ppmarshalled_sd
,
1934 struct security_descriptor
*psd
= NULL
;
1935 TALLOC_CTX
*frame
= talloc_stackframe();
1938 * Get the permissions to return.
1941 if ((security_info_wanted
& SECINFO_SACL
) &&
1942 !(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
1943 DEBUG(10, ("Access to SACL denied.\n"));
1945 return NT_STATUS_ACCESS_DENIED
;
1948 if ((security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|SECINFO_GROUP
)) &&
1949 !(fsp
->access_mask
& SEC_STD_READ_CONTROL
)) {
1950 DEBUG(10, ("Access to DACL, OWNER, or GROUP denied.\n"));
1952 return NT_STATUS_ACCESS_DENIED
;
1955 if (security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|
1956 SECINFO_GROUP
|SECINFO_SACL
)) {
1957 /* Don't return SECINFO_LABEL if anything else was
1958 requested. See bug #8458. */
1959 security_info_wanted
&= ~SECINFO_LABEL
;
1962 if (!lp_nt_acl_support(SNUM(conn
))) {
1963 status
= get_null_nt_acl(frame
, &psd
);
1964 } else if (security_info_wanted
& SECINFO_LABEL
) {
1965 /* Like W2K3 return a null object. */
1966 status
= get_null_nt_acl(frame
, &psd
);
1968 status
= SMB_VFS_FGET_NT_ACL(
1969 fsp
, security_info_wanted
, frame
, &psd
);
1971 if (!NT_STATUS_IS_OK(status
)) {
1976 if (!(security_info_wanted
& SECINFO_OWNER
)) {
1977 psd
->owner_sid
= NULL
;
1979 if (!(security_info_wanted
& SECINFO_GROUP
)) {
1980 psd
->group_sid
= NULL
;
1982 if (!(security_info_wanted
& SECINFO_DACL
)) {
1983 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
1986 if (!(security_info_wanted
& SECINFO_SACL
)) {
1987 psd
->type
&= ~SEC_DESC_SACL_PRESENT
;
1991 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1992 * present in the reply to match Windows behavior */
1993 if (psd
->sacl
== NULL
&&
1994 security_info_wanted
& SECINFO_SACL
)
1995 psd
->type
|= SEC_DESC_SACL_PRESENT
;
1996 if (psd
->dacl
== NULL
&&
1997 security_info_wanted
& SECINFO_DACL
)
1998 psd
->type
|= SEC_DESC_DACL_PRESENT
;
2000 if (security_info_wanted
& SECINFO_LABEL
) {
2001 /* Like W2K3 return a null object. */
2002 psd
->owner_sid
= NULL
;
2003 psd
->group_sid
= NULL
;
2006 psd
->type
&= ~(SEC_DESC_DACL_PRESENT
|SEC_DESC_SACL_PRESENT
);
2009 *psd_size
= ndr_size_security_descriptor(psd
, 0);
2011 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
2012 (unsigned long)*psd_size
));
2014 if (DEBUGLEVEL
>= 10) {
2015 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
2017 NDR_PRINT_DEBUG(security_descriptor
, psd
);
2020 if (max_data_count
< *psd_size
) {
2022 return NT_STATUS_BUFFER_TOO_SMALL
;
2025 status
= marshall_sec_desc(mem_ctx
, psd
,
2026 ppmarshalled_sd
, psd_size
);
2028 if (!NT_STATUS_IS_OK(status
)) {
2034 return NT_STATUS_OK
;
2037 /****************************************************************************
2038 SMB1 reply to query a security descriptor.
2039 ****************************************************************************/
2041 static void call_nt_transact_query_security_desc(connection_struct
*conn
,
2042 struct smb_request
*req
,
2046 uint32 parameter_count
,
2049 uint32 max_data_count
)
2051 char *params
= *ppparams
;
2052 char *data
= *ppdata
;
2054 uint32 security_info_wanted
;
2055 files_struct
*fsp
= NULL
;
2057 uint8_t *marshalled_sd
= NULL
;
2059 if(parameter_count
< 8) {
2060 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2064 fsp
= file_fsp(req
, SVAL(params
,0));
2066 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2070 security_info_wanted
= IVAL(params
,4);
2072 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
2073 "info_wanted = 0x%x\n", fsp_str_dbg(fsp
),
2074 (unsigned int)security_info_wanted
));
2076 params
= nttrans_realloc(ppparams
, 4);
2077 if(params
== NULL
) {
2078 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2083 * Get the permissions to return.
2086 status
= smbd_do_query_security_desc(conn
,
2089 security_info_wanted
,
2094 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
2095 SIVAL(params
,0,(uint32_t)sd_size
);
2096 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2097 params
, 4, NULL
, 0);
2101 if (!NT_STATUS_IS_OK(status
)) {
2102 reply_nterror(req
, status
);
2106 SMB_ASSERT(sd_size
> 0);
2108 SIVAL(params
,0,(uint32_t)sd_size
);
2110 if (max_data_count
< sd_size
) {
2111 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2112 params
, 4, NULL
, 0);
2117 * Allocate the data we will return.
2120 data
= nttrans_realloc(ppdata
, sd_size
);
2122 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2126 memcpy(data
, marshalled_sd
, sd_size
);
2128 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, 4, data
, (int)sd_size
);
2133 /****************************************************************************
2134 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2135 ****************************************************************************/
2137 static void call_nt_transact_set_security_desc(connection_struct
*conn
,
2138 struct smb_request
*req
,
2142 uint32 parameter_count
,
2145 uint32 max_data_count
)
2147 char *params
= *ppparams
;
2148 char *data
= *ppdata
;
2149 files_struct
*fsp
= NULL
;
2150 uint32 security_info_sent
= 0;
2153 if(parameter_count
< 8) {
2154 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2158 if((fsp
= file_fsp(req
, SVAL(params
,0))) == NULL
) {
2159 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2163 if (!CAN_WRITE(fsp
->conn
)) {
2164 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2168 if(!lp_nt_acl_support(SNUM(conn
))) {
2172 security_info_sent
= IVAL(params
,4);
2174 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2175 fsp_str_dbg(fsp
), (unsigned int)security_info_sent
));
2177 if (data_count
== 0) {
2178 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2182 status
= set_sd_blob(fsp
, (uint8
*)data
, data_count
, security_info_sent
);
2184 if (!NT_STATUS_IS_OK(status
)) {
2185 reply_nterror(req
, status
);
2190 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
2194 /****************************************************************************
2196 ****************************************************************************/
2198 static void call_nt_transact_ioctl(connection_struct
*conn
,
2199 struct smb_request
*req
,
2200 uint16
**ppsetup
, uint32 setup_count
,
2201 char **ppparams
, uint32 parameter_count
,
2202 char **ppdata
, uint32 data_count
,
2203 uint32 max_data_count
)
2211 char *out_data
= NULL
;
2212 uint32 out_data_len
= 0;
2213 char *pdata
= *ppdata
;
2214 TALLOC_CTX
*ctx
= talloc_tos();
2216 if (setup_count
!= 8) {
2217 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count
));
2218 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2222 function
= IVAL(*ppsetup
, 0);
2223 fidnum
= SVAL(*ppsetup
, 4);
2224 isFSctl
= CVAL(*ppsetup
, 6);
2225 compfilter
= CVAL(*ppsetup
, 7);
2227 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2228 function
, fidnum
, isFSctl
, compfilter
));
2230 fsp
=file_fsp(req
, fidnum
);
2233 * We don't really implement IOCTLs, especially on files.
2236 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2238 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2242 /* Has to be for an open file! */
2243 if (!check_fsp_open(conn
, req
, fsp
)) {
2247 SMB_PERFCOUNT_SET_IOCTL(&req
->pcd
, function
);
2250 * out_data might be allocated by the VFS module, but talloc should be
2251 * used, and should be cleaned up when the request ends.
2253 status
= SMB_VFS_FSCTL(fsp
,
2259 (uint8_t **)&out_data
,
2262 if (!NT_STATUS_IS_OK(status
)) {
2263 reply_nterror(req
, status
);
2265 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, out_data
, out_data_len
);
2270 #ifdef HAVE_SYS_QUOTAS
2271 /****************************************************************************
2272 Reply to get user quota
2273 ****************************************************************************/
2275 static void call_nt_transact_get_user_quota(connection_struct
*conn
,
2276 struct smb_request
*req
,
2280 uint32 parameter_count
,
2283 uint32 max_data_count
)
2285 NTSTATUS nt_status
= NT_STATUS_OK
;
2286 char *params
= *ppparams
;
2287 char *pdata
= *ppdata
;
2289 int data_len
=0,param_len
=0;
2292 files_struct
*fsp
= NULL
;
2296 bool start_enum
= True
;
2297 SMB_NTQUOTA_STRUCT qt
;
2298 SMB_NTQUOTA_LIST
*tmp_list
;
2299 SMB_NTQUOTA_HANDLE
*qt_handle
= NULL
;
2304 if (get_current_uid(conn
) != 0) {
2305 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2306 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn
)),
2307 conn
->session_info
->unix_info
->unix_name
));
2308 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2313 * Ensure minimum number of parameters sent.
2316 if (parameter_count
< 4) {
2317 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count
));
2318 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2322 /* maybe we can check the quota_fnum */
2323 fsp
= file_fsp(req
, SVAL(params
,0));
2324 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2325 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2326 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2330 /* the NULL pointer checking for fsp->fake_file_handle->pd
2331 * is done by CHECK_NTQUOTA_HANDLE_OK()
2333 qt_handle
= (SMB_NTQUOTA_HANDLE
*)fsp
->fake_file_handle
->private_data
;
2335 level
= SVAL(params
,2);
2337 /* unknown 12 bytes leading in params */
2340 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE
:
2341 /* seems that we should continue with the enum here --metze */
2343 if (qt_handle
->quota_list
!=NULL
&&
2344 qt_handle
->tmp_list
==NULL
) {
2347 free_ntquota_list(&(qt_handle
->quota_list
));
2349 /* Realloc the size of parameters and data we will return */
2351 params
= nttrans_realloc(ppparams
, param_len
);
2352 if(params
== NULL
) {
2353 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2358 SIVAL(params
,0,data_len
);
2365 case TRANSACT_GET_USER_QUOTA_LIST_START
:
2367 if (qt_handle
->quota_list
==NULL
&&
2368 qt_handle
->tmp_list
==NULL
) {
2372 if (start_enum
&& vfs_get_user_ntquota_list(fsp
,&(qt_handle
->quota_list
))!=0) {
2373 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2377 /* Realloc the size of parameters and data we will return */
2379 params
= nttrans_realloc(ppparams
, param_len
);
2380 if(params
== NULL
) {
2381 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2385 /* we should not trust the value in max_data_count*/
2386 max_data_count
= MIN(max_data_count
,2048);
2388 pdata
= nttrans_realloc(ppdata
, max_data_count
);/* should be max data count from client*/
2390 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2396 /* set params Size of returned Quota Data 4 bytes*/
2397 /* but set it later when we know it */
2399 /* for each entry push the data */
2402 qt_handle
->tmp_list
= qt_handle
->quota_list
;
2405 tmp_list
= qt_handle
->tmp_list
;
2407 for (;((tmp_list
!=NULL
)&&((qt_len
+40+SID_MAX_SIZE
)<max_data_count
));
2408 tmp_list
=tmp_list
->next
,entry
+=entry_len
,qt_len
+=entry_len
) {
2410 sid_len
= ndr_size_dom_sid(
2411 &tmp_list
->quotas
->sid
, 0);
2412 entry_len
= 40 + sid_len
;
2414 /* nextoffset entry 4 bytes */
2415 SIVAL(entry
,0,entry_len
);
2417 /* then the len of the SID 4 bytes */
2418 SIVAL(entry
,4,sid_len
);
2420 /* unknown data 8 bytes uint64_t */
2421 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2423 /* the used disk space 8 bytes uint64_t */
2424 SBIG_UINT(entry
,16,tmp_list
->quotas
->usedspace
);
2426 /* the soft quotas 8 bytes uint64_t */
2427 SBIG_UINT(entry
,24,tmp_list
->quotas
->softlim
);
2429 /* the hard quotas 8 bytes uint64_t */
2430 SBIG_UINT(entry
,32,tmp_list
->quotas
->hardlim
);
2432 /* and now the SID */
2433 sid_linearize(entry
+40, sid_len
, &tmp_list
->quotas
->sid
);
2436 qt_handle
->tmp_list
= tmp_list
;
2438 /* overwrite the offset of the last entry */
2439 SIVAL(entry
-entry_len
,0,0);
2441 data_len
= 4+qt_len
;
2442 /* overwrite the params quota_data_len */
2443 SIVAL(params
,0,data_len
);
2447 case TRANSACT_GET_USER_QUOTA_FOR_SID
:
2449 /* unknown 4 bytes IVAL(pdata,0) */
2451 if (data_count
< 8) {
2452 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count
,8));
2453 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2457 sid_len
= IVAL(pdata
,4);
2458 /* Ensure this is less than 1mb. */
2459 if (sid_len
> (1024*1024)) {
2460 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2464 if (data_count
< 8+sid_len
) {
2465 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count
,(unsigned long)(8+sid_len
)));
2466 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2470 data_len
= 4+40+sid_len
;
2472 if (max_data_count
< data_len
) {
2473 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2474 max_data_count
, data_len
));
2476 SIVAL(params
,0,data_len
);
2478 nt_status
= NT_STATUS_BUFFER_TOO_SMALL
;
2482 if (!sid_parse(pdata
+8,sid_len
,&sid
)) {
2483 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2487 if (vfs_get_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2490 * we have to return zero's in all fields
2491 * instead of returning an error here
2496 /* Realloc the size of parameters and data we will return */
2498 params
= nttrans_realloc(ppparams
, param_len
);
2499 if(params
== NULL
) {
2500 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2504 pdata
= nttrans_realloc(ppdata
, data_len
);
2506 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2512 /* set params Size of returned Quota Data 4 bytes*/
2513 SIVAL(params
,0,data_len
);
2515 /* nextoffset entry 4 bytes */
2518 /* then the len of the SID 4 bytes */
2519 SIVAL(entry
,4,sid_len
);
2521 /* unknown data 8 bytes uint64_t */
2522 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2524 /* the used disk space 8 bytes uint64_t */
2525 SBIG_UINT(entry
,16,qt
.usedspace
);
2527 /* the soft quotas 8 bytes uint64_t */
2528 SBIG_UINT(entry
,24,qt
.softlim
);
2530 /* the hard quotas 8 bytes uint64_t */
2531 SBIG_UINT(entry
,32,qt
.hardlim
);
2533 /* and now the SID */
2534 sid_linearize(entry
+40, sid_len
, &sid
);
2539 DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
2541 fsp_fnum_dbg(fsp
), level
));
2542 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2547 send_nt_replies(conn
, req
, nt_status
, params
, param_len
,
2551 /****************************************************************************
2552 Reply to set user quota
2553 ****************************************************************************/
2555 static void call_nt_transact_set_user_quota(connection_struct
*conn
,
2556 struct smb_request
*req
,
2560 uint32 parameter_count
,
2563 uint32 max_data_count
)
2565 char *params
= *ppparams
;
2566 char *pdata
= *ppdata
;
2567 int data_len
=0,param_len
=0;
2568 SMB_NTQUOTA_STRUCT qt
;
2571 files_struct
*fsp
= NULL
;
2576 if (get_current_uid(conn
) != 0) {
2577 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2578 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn
)),
2579 conn
->session_info
->unix_info
->unix_name
));
2580 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2585 * Ensure minimum number of parameters sent.
2588 if (parameter_count
< 2) {
2589 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count
));
2590 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2594 /* maybe we can check the quota_fnum */
2595 fsp
= file_fsp(req
, SVAL(params
,0));
2596 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2597 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2598 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2602 if (data_count
< 40) {
2603 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count
,40));
2604 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2608 /* offset to next quota record.
2609 * 4 bytes IVAL(pdata,0)
2614 sid_len
= IVAL(pdata
,4);
2616 if (data_count
< 40+sid_len
|| (40+sid_len
< sid_len
)) {
2617 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count
,(unsigned long)40+sid_len
));
2618 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2622 /* unknown 8 bytes in pdata
2623 * maybe its the change time in NTTIME
2626 /* the used space 8 bytes (uint64_t)*/
2627 qt
.usedspace
= BVAL(pdata
,16);
2629 /* the soft quotas 8 bytes (uint64_t)*/
2630 qt
.softlim
= BVAL(pdata
,24);
2632 /* the hard quotas 8 bytes (uint64_t)*/
2633 qt
.hardlim
= BVAL(pdata
,32);
2635 if (!sid_parse(pdata
+40,sid_len
,&sid
)) {
2636 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2640 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid
)));
2642 /* 44 unknown bytes left... */
2644 if (vfs_set_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2645 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2649 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
,
2652 #endif /* HAVE_SYS_QUOTAS */
2654 static void handle_nttrans(connection_struct
*conn
,
2655 struct trans_state
*state
,
2656 struct smb_request
*req
)
2658 if (get_Protocol() >= PROTOCOL_NT1
) {
2659 req
->flags2
|= 0x40; /* IS_LONG_NAME */
2660 SSVAL(discard_const_p(uint8_t, req
->inbuf
),smb_flg2
,req
->flags2
);
2664 SMB_PERFCOUNT_SET_SUBOP(&req
->pcd
, state
->call
);
2666 /* Now we must call the relevant NT_TRANS function */
2667 switch(state
->call
) {
2668 case NT_TRANSACT_CREATE
:
2670 START_PROFILE(NT_transact_create
);
2671 call_nt_transact_create(
2673 &state
->setup
, state
->setup_count
,
2674 &state
->param
, state
->total_param
,
2675 &state
->data
, state
->total_data
,
2676 state
->max_data_return
);
2677 END_PROFILE(NT_transact_create
);
2681 case NT_TRANSACT_IOCTL
:
2683 START_PROFILE(NT_transact_ioctl
);
2684 call_nt_transact_ioctl(
2686 &state
->setup
, state
->setup_count
,
2687 &state
->param
, state
->total_param
,
2688 &state
->data
, state
->total_data
,
2689 state
->max_data_return
);
2690 END_PROFILE(NT_transact_ioctl
);
2694 case NT_TRANSACT_SET_SECURITY_DESC
:
2696 START_PROFILE(NT_transact_set_security_desc
);
2697 call_nt_transact_set_security_desc(
2699 &state
->setup
, state
->setup_count
,
2700 &state
->param
, state
->total_param
,
2701 &state
->data
, state
->total_data
,
2702 state
->max_data_return
);
2703 END_PROFILE(NT_transact_set_security_desc
);
2707 case NT_TRANSACT_NOTIFY_CHANGE
:
2709 START_PROFILE(NT_transact_notify_change
);
2710 call_nt_transact_notify_change(
2712 &state
->setup
, state
->setup_count
,
2713 &state
->param
, state
->total_param
,
2714 &state
->data
, state
->total_data
,
2715 state
->max_data_return
,
2716 state
->max_param_return
);
2717 END_PROFILE(NT_transact_notify_change
);
2721 case NT_TRANSACT_RENAME
:
2723 START_PROFILE(NT_transact_rename
);
2724 call_nt_transact_rename(
2726 &state
->setup
, state
->setup_count
,
2727 &state
->param
, state
->total_param
,
2728 &state
->data
, state
->total_data
,
2729 state
->max_data_return
);
2730 END_PROFILE(NT_transact_rename
);
2734 case NT_TRANSACT_QUERY_SECURITY_DESC
:
2736 START_PROFILE(NT_transact_query_security_desc
);
2737 call_nt_transact_query_security_desc(
2739 &state
->setup
, state
->setup_count
,
2740 &state
->param
, state
->total_param
,
2741 &state
->data
, state
->total_data
,
2742 state
->max_data_return
);
2743 END_PROFILE(NT_transact_query_security_desc
);
2747 #ifdef HAVE_SYS_QUOTAS
2748 case NT_TRANSACT_GET_USER_QUOTA
:
2750 START_PROFILE(NT_transact_get_user_quota
);
2751 call_nt_transact_get_user_quota(
2753 &state
->setup
, state
->setup_count
,
2754 &state
->param
, state
->total_param
,
2755 &state
->data
, state
->total_data
,
2756 state
->max_data_return
);
2757 END_PROFILE(NT_transact_get_user_quota
);
2761 case NT_TRANSACT_SET_USER_QUOTA
:
2763 START_PROFILE(NT_transact_set_user_quota
);
2764 call_nt_transact_set_user_quota(
2766 &state
->setup
, state
->setup_count
,
2767 &state
->param
, state
->total_param
,
2768 &state
->data
, state
->total_data
,
2769 state
->max_data_return
);
2770 END_PROFILE(NT_transact_set_user_quota
);
2773 #endif /* HAVE_SYS_QUOTAS */
2776 /* Error in request */
2777 DEBUG(0,("handle_nttrans: Unknown request %d in "
2778 "nttrans call\n", state
->call
));
2779 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2785 /****************************************************************************
2786 Reply to a SMBNTtrans.
2787 ****************************************************************************/
2789 void reply_nttrans(struct smb_request
*req
)
2791 connection_struct
*conn
= req
->conn
;
2796 uint16 function_code
;
2798 struct trans_state
*state
;
2800 START_PROFILE(SMBnttrans
);
2802 if (req
->wct
< 19) {
2803 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2804 END_PROFILE(SMBnttrans
);
2808 pscnt
= IVAL(req
->vwv
+9, 1);
2809 psoff
= IVAL(req
->vwv
+11, 1);
2810 dscnt
= IVAL(req
->vwv
+13, 1);
2811 dsoff
= IVAL(req
->vwv
+15, 1);
2812 function_code
= SVAL(req
->vwv
+18, 0);
2814 if (IS_IPC(conn
) && (function_code
!= NT_TRANSACT_CREATE
)) {
2815 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2816 END_PROFILE(SMBnttrans
);
2820 result
= allow_new_trans(conn
->pending_trans
, req
->mid
);
2821 if (!NT_STATUS_IS_OK(result
)) {
2822 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result
)));
2823 reply_nterror(req
, result
);
2824 END_PROFILE(SMBnttrans
);
2828 if ((state
= talloc(conn
, struct trans_state
)) == NULL
) {
2829 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2830 END_PROFILE(SMBnttrans
);
2834 state
->cmd
= SMBnttrans
;
2836 state
->mid
= req
->mid
;
2837 state
->vuid
= req
->vuid
;
2838 state
->total_data
= IVAL(req
->vwv
+3, 1);
2840 state
->total_param
= IVAL(req
->vwv
+1, 1);
2841 state
->param
= NULL
;
2842 state
->max_data_return
= IVAL(req
->vwv
+7, 1);
2843 state
->max_param_return
= IVAL(req
->vwv
+5, 1);
2845 /* setup count is in *words* */
2846 state
->setup_count
= 2*CVAL(req
->vwv
+17, 1);
2847 state
->setup
= NULL
;
2848 state
->call
= function_code
;
2850 DEBUG(10, ("num_setup=%u, "
2851 "param_total=%u, this_param=%u, max_param=%u, "
2852 "data_total=%u, this_data=%u, max_data=%u, "
2853 "param_offset=%u, data_offset=%u\n",
2854 (unsigned)state
->setup_count
,
2855 (unsigned)state
->total_param
, (unsigned)pscnt
,
2856 (unsigned)state
->max_param_return
,
2857 (unsigned)state
->total_data
, (unsigned)dscnt
,
2858 (unsigned)state
->max_data_return
,
2859 (unsigned)psoff
, (unsigned)dsoff
));
2862 * All nttrans messages we handle have smb_wct == 19 +
2863 * state->setup_count. Ensure this is so as a sanity check.
2866 if(req
->wct
!= 19 + (state
->setup_count
/2)) {
2867 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2868 req
->wct
, 19 + (state
->setup_count
/2)));
2872 /* Don't allow more than 128mb for each value. */
2873 if ((state
->total_data
> (1024*1024*128)) ||
2874 (state
->total_param
> (1024*1024*128))) {
2875 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2876 END_PROFILE(SMBnttrans
);
2880 if ((dscnt
> state
->total_data
) || (pscnt
> state
->total_param
))
2883 if (state
->total_data
) {
2885 if (trans_oob(state
->total_data
, 0, dscnt
)
2886 || trans_oob(smb_len(req
->inbuf
), dsoff
, dscnt
)) {
2890 /* Can't use talloc here, the core routines do realloc on the
2891 * params and data. */
2892 if ((state
->data
= (char *)SMB_MALLOC(state
->total_data
)) == NULL
) {
2893 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2894 "bytes !\n", (unsigned int)state
->total_data
));
2896 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2897 END_PROFILE(SMBnttrans
);
2901 memcpy(state
->data
,smb_base(req
->inbuf
)+dsoff
,dscnt
);
2904 if (state
->total_param
) {
2906 if (trans_oob(state
->total_param
, 0, pscnt
)
2907 || trans_oob(smb_len(req
->inbuf
), psoff
, pscnt
)) {
2911 /* Can't use talloc here, the core routines do realloc on the
2912 * params and data. */
2913 if ((state
->param
= (char *)SMB_MALLOC(state
->total_param
)) == NULL
) {
2914 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2915 "bytes !\n", (unsigned int)state
->total_param
));
2916 SAFE_FREE(state
->data
);
2918 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2919 END_PROFILE(SMBnttrans
);
2923 memcpy(state
->param
,smb_base(req
->inbuf
)+psoff
,pscnt
);
2926 state
->received_data
= dscnt
;
2927 state
->received_param
= pscnt
;
2929 if(state
->setup_count
> 0) {
2930 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2931 state
->setup_count
));
2934 * No overflow possible here, state->setup_count is an
2935 * unsigned int, being filled by a single byte from
2936 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2937 * below is not necessary, it's here to clarify things. The
2938 * validity of req->vwv and req->wct has been checked in
2939 * init_smb_request already.
2941 if ((state
->setup_count
/2) + 19 > (unsigned int)req
->wct
) {
2945 state
->setup
= (uint16
*)TALLOC(state
, state
->setup_count
);
2946 if (state
->setup
== NULL
) {
2947 DEBUG(0,("reply_nttrans : Out of memory\n"));
2948 SAFE_FREE(state
->data
);
2949 SAFE_FREE(state
->param
);
2951 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2952 END_PROFILE(SMBnttrans
);
2956 memcpy(state
->setup
, req
->vwv
+19, state
->setup_count
);
2957 dump_data(10, (uint8
*)state
->setup
, state
->setup_count
);
2960 if ((state
->received_data
== state
->total_data
) &&
2961 (state
->received_param
== state
->total_param
)) {
2962 handle_nttrans(conn
, state
, req
);
2963 SAFE_FREE(state
->param
);
2964 SAFE_FREE(state
->data
);
2966 END_PROFILE(SMBnttrans
);
2970 DLIST_ADD(conn
->pending_trans
, state
);
2972 /* We need to send an interim response then receive the rest
2973 of the parameter/data bytes */
2974 reply_outbuf(req
, 0, 0);
2975 show_msg((char *)req
->outbuf
);
2976 END_PROFILE(SMBnttrans
);
2981 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2982 SAFE_FREE(state
->data
);
2983 SAFE_FREE(state
->param
);
2985 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2986 END_PROFILE(SMBnttrans
);
2990 /****************************************************************************
2991 Reply to a SMBnttranss
2992 ****************************************************************************/
2994 void reply_nttranss(struct smb_request
*req
)
2996 connection_struct
*conn
= req
->conn
;
2997 uint32_t pcnt
,poff
,dcnt
,doff
,pdisp
,ddisp
;
2998 struct trans_state
*state
;
3000 START_PROFILE(SMBnttranss
);
3002 show_msg((const char *)req
->inbuf
);
3004 /* Windows clients expect all replies to
3005 an NT transact secondary (SMBnttranss 0xA1)
3006 to have a command code of NT transact
3007 (SMBnttrans 0xA0). See bug #8989 for details. */
3008 req
->cmd
= SMBnttrans
;
3010 if (req
->wct
< 18) {
3011 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3012 END_PROFILE(SMBnttranss
);
3016 for (state
= conn
->pending_trans
; state
!= NULL
;
3017 state
= state
->next
) {
3018 if (state
->mid
== req
->mid
) {
3023 if ((state
== NULL
) || (state
->cmd
!= SMBnttrans
)) {
3024 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3025 END_PROFILE(SMBnttranss
);
3029 /* Revise state->total_param and state->total_data in case they have
3030 changed downwards */
3031 if (IVAL(req
->vwv
+1, 1) < state
->total_param
) {
3032 state
->total_param
= IVAL(req
->vwv
+1, 1);
3034 if (IVAL(req
->vwv
+3, 1) < state
->total_data
) {
3035 state
->total_data
= IVAL(req
->vwv
+3, 1);
3038 pcnt
= IVAL(req
->vwv
+5, 1);
3039 poff
= IVAL(req
->vwv
+7, 1);
3040 pdisp
= IVAL(req
->vwv
+9, 1);
3042 dcnt
= IVAL(req
->vwv
+11, 1);
3043 doff
= IVAL(req
->vwv
+13, 1);
3044 ddisp
= IVAL(req
->vwv
+15, 1);
3046 state
->received_param
+= pcnt
;
3047 state
->received_data
+= dcnt
;
3049 if ((state
->received_data
> state
->total_data
) ||
3050 (state
->received_param
> state
->total_param
))
3054 if (trans_oob(state
->total_param
, pdisp
, pcnt
)
3055 || trans_oob(smb_len(req
->inbuf
), poff
, pcnt
)) {
3058 memcpy(state
->param
+pdisp
, smb_base(req
->inbuf
)+poff
,pcnt
);
3062 if (trans_oob(state
->total_data
, ddisp
, dcnt
)
3063 || trans_oob(smb_len(req
->inbuf
), doff
, dcnt
)) {
3066 memcpy(state
->data
+ddisp
, smb_base(req
->inbuf
)+doff
,dcnt
);
3069 if ((state
->received_param
< state
->total_param
) ||
3070 (state
->received_data
< state
->total_data
)) {
3071 END_PROFILE(SMBnttranss
);
3075 handle_nttrans(conn
, state
, req
);
3077 DLIST_REMOVE(conn
->pending_trans
, state
);
3078 SAFE_FREE(state
->data
);
3079 SAFE_FREE(state
->param
);
3081 END_PROFILE(SMBnttranss
);
3086 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3087 DLIST_REMOVE(conn
->pending_trans
, state
);
3088 SAFE_FREE(state
->data
);
3089 SAFE_FREE(state
->param
);
3091 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3092 END_PROFILE(SMBnttranss
);