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"
32 #include "lib/util_ea.h"
34 extern const struct generic_mapping file_generic_mapping
;
36 static char *nttrans_realloc(char **ptr
, size_t size
)
39 smb_panic("nttrans_realloc() called with NULL ptr");
42 *ptr
= (char *)SMB_REALLOC(*ptr
, size
);
46 memset(*ptr
,'\0',size
);
50 /****************************************************************************
51 Send the required number of replies back.
52 We assume all fields other than the data fields are
53 set correctly for the type of call.
54 HACK ! Always assumes smb_setup field is zero.
55 ****************************************************************************/
57 static void send_nt_replies(connection_struct
*conn
,
58 struct smb_request
*req
, NTSTATUS nt_error
,
59 char *params
, int paramsize
,
60 char *pdata
, int datasize
)
62 int data_to_send
= datasize
;
63 int params_to_send
= paramsize
;
67 int params_sent_thistime
, data_sent_thistime
, total_sent_thistime
;
68 int alignment_offset
= 1;
69 int data_alignment_offset
= 0;
70 struct smbd_server_connection
*sconn
= req
->sconn
;
71 int max_send
= sconn
->smb1
.sessions
.max_send
;
74 * If there genuinely are no parameters or data to send just send
78 if(params_to_send
== 0 && data_to_send
== 0) {
79 reply_outbuf(req
, 18, 0);
80 if (NT_STATUS_V(nt_error
)) {
81 error_packet_set((char *)req
->outbuf
,
85 show_msg((char *)req
->outbuf
);
86 if (!srv_send_smb(sconn
,
89 IS_CONN_ENCRYPTED(conn
),
91 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
93 TALLOC_FREE(req
->outbuf
);
98 * When sending params and data ensure that both are nicely aligned.
99 * Only do this alignment when there is also data to send - else
100 * can cause NT redirector problems.
103 if (((params_to_send
% 4) != 0) && (data_to_send
!= 0)) {
104 data_alignment_offset
= 4 - (params_to_send
% 4);
108 * Space is bufsize minus Netbios over TCP header minus SMB header.
109 * The alignment_offset is to align the param bytes on a four byte
110 * boundary (2 bytes for data len, one byte pad).
111 * NT needs this to work correctly.
114 useable_space
= max_send
- (smb_size
117 + data_alignment_offset
);
119 if (useable_space
< 0) {
120 char *msg
= talloc_asprintf(
122 "send_nt_replies failed sanity useable_space = %d!!!",
124 DEBUG(0, ("%s\n", msg
));
125 exit_server_cleanly(msg
);
128 while (params_to_send
|| data_to_send
) {
131 * Calculate whether we will totally or partially fill this packet.
134 total_sent_thistime
= params_to_send
+ data_to_send
;
137 * We can never send more than useable_space.
140 total_sent_thistime
= MIN(total_sent_thistime
, useable_space
);
142 reply_outbuf(req
, 18,
143 total_sent_thistime
+ alignment_offset
144 + data_alignment_offset
);
147 * Set total params and data to be sent.
150 SIVAL(req
->outbuf
,smb_ntr_TotalParameterCount
,paramsize
);
151 SIVAL(req
->outbuf
,smb_ntr_TotalDataCount
,datasize
);
154 * Calculate how many parameters and data we can fit into
155 * this packet. Parameters get precedence.
158 params_sent_thistime
= MIN(params_to_send
,useable_space
);
159 data_sent_thistime
= useable_space
- params_sent_thistime
;
160 data_sent_thistime
= MIN(data_sent_thistime
,data_to_send
);
162 SIVAL(req
->outbuf
, smb_ntr_ParameterCount
,
163 params_sent_thistime
);
165 if(params_sent_thistime
== 0) {
166 SIVAL(req
->outbuf
,smb_ntr_ParameterOffset
,0);
167 SIVAL(req
->outbuf
,smb_ntr_ParameterDisplacement
,0);
170 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
171 * parameter bytes, however the first 4 bytes of outbuf are
172 * the Netbios over TCP header. Thus use smb_base() to subtract
173 * them from the calculation.
176 SIVAL(req
->outbuf
,smb_ntr_ParameterOffset
,
177 ((smb_buf(req
->outbuf
)+alignment_offset
)
178 - smb_base(req
->outbuf
)));
180 * Absolute displacement of param bytes sent in this packet.
183 SIVAL(req
->outbuf
, smb_ntr_ParameterDisplacement
,
188 * Deal with the data portion.
191 SIVAL(req
->outbuf
, smb_ntr_DataCount
, data_sent_thistime
);
193 if(data_sent_thistime
== 0) {
194 SIVAL(req
->outbuf
,smb_ntr_DataOffset
,0);
195 SIVAL(req
->outbuf
,smb_ntr_DataDisplacement
, 0);
198 * The offset of the data bytes is the offset of the
199 * parameter bytes plus the number of parameters being sent this time.
202 SIVAL(req
->outbuf
, smb_ntr_DataOffset
,
203 ((smb_buf(req
->outbuf
)+alignment_offset
) -
204 smb_base(req
->outbuf
))
205 + params_sent_thistime
+ data_alignment_offset
);
206 SIVAL(req
->outbuf
,smb_ntr_DataDisplacement
, pd
- pdata
);
210 * Copy the param bytes into the packet.
213 if(params_sent_thistime
) {
214 if (alignment_offset
!= 0) {
215 memset(smb_buf(req
->outbuf
), 0,
218 memcpy((smb_buf(req
->outbuf
)+alignment_offset
), pp
,
219 params_sent_thistime
);
223 * Copy in the data bytes
226 if(data_sent_thistime
) {
227 if (data_alignment_offset
!= 0) {
228 memset((smb_buf(req
->outbuf
)+alignment_offset
+
229 params_sent_thistime
), 0,
230 data_alignment_offset
);
232 memcpy(smb_buf(req
->outbuf
)+alignment_offset
233 +params_sent_thistime
+data_alignment_offset
,
234 pd
,data_sent_thistime
);
237 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
238 params_sent_thistime
, data_sent_thistime
, useable_space
));
239 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
240 params_to_send
, data_to_send
, paramsize
, datasize
));
242 if (NT_STATUS_V(nt_error
)) {
243 error_packet_set((char *)req
->outbuf
,
248 /* Send the packet */
249 show_msg((char *)req
->outbuf
);
250 if (!srv_send_smb(sconn
,
253 IS_CONN_ENCRYPTED(conn
),
255 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
258 TALLOC_FREE(req
->outbuf
);
260 pp
+= params_sent_thistime
;
261 pd
+= data_sent_thistime
;
263 params_to_send
-= params_sent_thistime
;
264 data_to_send
-= data_sent_thistime
;
270 if(params_to_send
< 0 || data_to_send
< 0) {
271 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
272 params_to_send
, data_to_send
));
273 exit_server_cleanly("send_nt_replies: internal error");
278 /****************************************************************************
279 Reply to an NT create and X call on a pipe
280 ****************************************************************************/
282 static void nt_open_pipe(char *fname
, connection_struct
*conn
,
283 struct smb_request
*req
, uint16_t *ppnum
)
288 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname
));
290 /* Strip \\ off the name if present. */
291 while (fname
[0] == '\\') {
295 status
= open_np_file(req
, fname
, &fsp
);
296 if (!NT_STATUS_IS_OK(status
)) {
297 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
298 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
302 reply_nterror(req
, status
);
310 /****************************************************************************
311 Reply to an NT create and X call for pipes.
312 ****************************************************************************/
314 static void do_ntcreate_pipe_open(connection_struct
*conn
,
315 struct smb_request
*req
)
318 uint16_t pnum
= FNUM_FIELD_INVALID
;
320 uint32 flags
= IVAL(req
->vwv
+3, 1);
321 TALLOC_CTX
*ctx
= talloc_tos();
323 srvstr_pull_req_talloc(ctx
, req
, &fname
, req
->buf
, STR_TERMINATE
);
326 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
330 nt_open_pipe(fname
, conn
, req
, &pnum
);
338 * Deal with pipe return.
341 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
342 /* This is very strange. We
343 * return 50 words, but only set
344 * the wcnt to 42 ? It's definitely
345 * what happens on the wire....
347 reply_outbuf(req
, 50, 0);
348 SCVAL(req
->outbuf
,smb_wct
,42);
350 reply_outbuf(req
, 34, 0);
353 SSVAL(req
->outbuf
, smb_vwv0
, 0xff); /* andx chain ends */
354 SSVAL(req
->outbuf
, smb_vwv1
, 0); /* no andx offset */
356 p
= (char *)req
->outbuf
+ smb_vwv2
;
360 SIVAL(p
,0,FILE_WAS_OPENED
);
363 SIVAL(p
,0,FILE_ATTRIBUTE_NORMAL
); /* File Attributes. */
366 SSVAL(p
,0,FILE_TYPE_MESSAGE_MODE_PIPE
);
368 SSVAL(p
,2, 0x5FF); /* ? */
371 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
373 SIVAL(p
,0,FILE_GENERIC_ALL
);
375 * For pipes W2K3 seems to return
377 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
379 SIVAL(p
,4,(FILE_GENERIC_READ
|FILE_GENERIC_WRITE
)&~FILE_APPEND_DATA
);
382 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname
));
385 struct case_semantics_state
{
386 connection_struct
*conn
;
389 bool short_case_preserve
;
392 /****************************************************************************
393 Restore case semantics.
394 ****************************************************************************/
396 static int restore_case_semantics(struct case_semantics_state
*state
)
398 state
->conn
->case_sensitive
= state
->case_sensitive
;
399 state
->conn
->case_preserve
= state
->case_preserve
;
400 state
->conn
->short_case_preserve
= state
->short_case_preserve
;
404 /****************************************************************************
406 ****************************************************************************/
408 static struct case_semantics_state
*set_posix_case_semantics(TALLOC_CTX
*mem_ctx
,
409 connection_struct
*conn
)
411 struct case_semantics_state
*result
;
413 if (!(result
= talloc(mem_ctx
, struct case_semantics_state
))) {
418 result
->case_sensitive
= conn
->case_sensitive
;
419 result
->case_preserve
= conn
->case_preserve
;
420 result
->short_case_preserve
= conn
->short_case_preserve
;
423 conn
->case_sensitive
= True
;
424 conn
->case_preserve
= True
;
425 conn
->short_case_preserve
= True
;
427 talloc_set_destructor(result
, restore_case_semantics
);
432 /****************************************************************************
433 Reply to an NT create and X call.
434 ****************************************************************************/
436 void reply_ntcreate_and_X(struct smb_request
*req
)
438 connection_struct
*conn
= req
->conn
;
439 struct smb_filename
*smb_fname
= NULL
;
443 uint32 file_attributes
;
445 uint32 create_disposition
;
446 uint32 create_options
;
448 uint64_t allocation_size
;
449 /* Breakout the oplock request bits so we can set the
450 reply bits separately. */
454 files_struct
*fsp
= NULL
;
456 struct timespec create_timespec
;
457 struct timespec c_timespec
;
458 struct timespec a_timespec
;
459 struct timespec m_timespec
;
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
;
659 create_timespec
= get_create_timespec(conn
, fsp
, smb_fname
);
660 a_timespec
= smb_fname
->st
.st_ex_atime
;
661 m_timespec
= smb_fname
->st
.st_ex_mtime
;
662 c_timespec
= get_change_timespec(conn
, fsp
, smb_fname
);
664 if (lp_dos_filetime_resolution(SNUM(conn
))) {
665 dos_filetime_timespec(&create_timespec
);
666 dos_filetime_timespec(&a_timespec
);
667 dos_filetime_timespec(&m_timespec
);
668 dos_filetime_timespec(&c_timespec
);
671 put_long_date_timespec(conn
->ts_res
, p
, create_timespec
); /* create time. */
673 put_long_date_timespec(conn
->ts_res
, p
, a_timespec
); /* access time */
675 put_long_date_timespec(conn
->ts_res
, p
, m_timespec
); /* write time */
677 put_long_date_timespec(conn
->ts_res
, p
, c_timespec
); /* change time */
679 SIVAL(p
,0,fattr
); /* File Attributes. */
681 SOFF_T(p
, 0, SMB_VFS_GET_ALLOC_SIZE(conn
,fsp
,&smb_fname
->st
));
683 SOFF_T(p
,0,file_len
);
685 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
686 uint16_t file_status
= (NO_EAS
|NO_SUBSTREAMS
|NO_REPARSETAG
);
687 size_t num_names
= 0;
688 unsigned int num_streams
= 0;
689 struct stream_struct
*streams
= NULL
;
691 /* Do we have any EA's ? */
692 status
= get_ea_names_from_file(ctx
, conn
, fsp
,
693 smb_fname
->base_name
, NULL
, &num_names
);
694 if (NT_STATUS_IS_OK(status
) && num_names
) {
695 file_status
&= ~NO_EAS
;
697 status
= vfs_streaminfo(conn
, NULL
, smb_fname
->base_name
, ctx
,
698 &num_streams
, &streams
);
699 /* There is always one stream, ::$DATA. */
700 if (NT_STATUS_IS_OK(status
) && num_streams
> 1) {
701 file_status
&= ~NO_SUBSTREAMS
;
703 TALLOC_FREE(streams
);
704 SSVAL(p
,2,file_status
);
707 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
709 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
712 if (fsp
->is_directory
||
714 can_write_to_file(conn
, smb_fname
)) {
715 perms
= FILE_GENERIC_ALL
;
717 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
722 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
723 fsp_fnum_dbg(fsp
), smb_fname_str_dbg(smb_fname
)));
726 END_PROFILE(SMBntcreateX
);
730 /****************************************************************************
731 Reply to a NT_TRANSACT_CREATE call to open a pipe.
732 ****************************************************************************/
734 static void do_nt_transact_create_pipe(connection_struct
*conn
,
735 struct smb_request
*req
,
736 uint16
**ppsetup
, uint32 setup_count
,
737 char **ppparams
, uint32 parameter_count
,
738 char **ppdata
, uint32 data_count
)
741 char *params
= *ppparams
;
742 uint16_t pnum
= FNUM_FIELD_INVALID
;
747 TALLOC_CTX
*ctx
= talloc_tos();
750 * Ensure minimum number of parameters sent.
753 if(parameter_count
< 54) {
754 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
755 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
759 flags
= IVAL(params
,0);
761 srvstr_get_path(ctx
, params
, req
->flags2
, &fname
, params
+53,
762 parameter_count
-53, STR_TERMINATE
,
764 if (!NT_STATUS_IS_OK(status
)) {
765 reply_nterror(req
, status
);
769 nt_open_pipe(fname
, conn
, req
, &pnum
);
776 /* Realloc the size of parameters and data we will return */
777 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
778 /* Extended response is 32 more byyes. */
783 params
= nttrans_realloc(ppparams
, param_len
);
785 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
790 SCVAL(p
,0,NO_OPLOCK_RETURN
);
795 SIVAL(p
,0,FILE_WAS_OPENED
);
799 SIVAL(p
,0,FILE_ATTRIBUTE_NORMAL
); /* File Attributes. */
802 SSVAL(p
,0,FILE_TYPE_MESSAGE_MODE_PIPE
);
804 SSVAL(p
,2, 0x5FF); /* ? */
807 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
809 SIVAL(p
,0,FILE_GENERIC_ALL
);
811 * For pipes W2K3 seems to return
813 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
815 SIVAL(p
,4,(FILE_GENERIC_READ
|FILE_GENERIC_WRITE
)&~FILE_APPEND_DATA
);
818 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname
));
820 /* Send the required number of replies */
821 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
826 /*********************************************************************
827 Windows seems to do canonicalization of inheritance bits. Do the
829 *********************************************************************/
831 static void canonicalize_inheritance_bits(struct security_descriptor
*psd
)
833 bool set_auto_inherited
= false;
836 * We need to filter out the
837 * SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ
838 * bits. If both are set we store SEC_DESC_DACL_AUTO_INHERITED
839 * as this alters whether SEC_ACE_FLAG_INHERITED_ACE is set
840 * when an ACE is inherited. Otherwise we zero these bits out.
843 * http://social.msdn.microsoft.com/Forums/eu/os_fileservices/thread/11f77b68-731e-407d-b1b3-064750716531
848 if ((psd
->type
& (SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
))
849 == (SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
)) {
850 set_auto_inherited
= true;
853 psd
->type
&= ~(SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
);
854 if (set_auto_inherited
) {
855 psd
->type
|= SEC_DESC_DACL_AUTO_INHERITED
;
859 /****************************************************************************
860 Internal fn to set security descriptors.
861 ****************************************************************************/
863 NTSTATUS
set_sd(files_struct
*fsp
, struct security_descriptor
*psd
,
864 uint32_t security_info_sent
)
868 if (!CAN_WRITE(fsp
->conn
)) {
869 return NT_STATUS_ACCESS_DENIED
;
872 if (!lp_nt_acl_support(SNUM(fsp
->conn
))) {
876 if (psd
->owner_sid
== NULL
) {
877 security_info_sent
&= ~SECINFO_OWNER
;
879 if (psd
->group_sid
== NULL
) {
880 security_info_sent
&= ~SECINFO_GROUP
;
883 /* Ensure we have at least one thing set. */
884 if ((security_info_sent
& (SECINFO_OWNER
|SECINFO_GROUP
|SECINFO_DACL
|SECINFO_SACL
)) == 0) {
889 /* Ensure we have the rights to do this. */
890 if (security_info_sent
& SECINFO_OWNER
) {
891 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
892 return NT_STATUS_ACCESS_DENIED
;
896 if (security_info_sent
& SECINFO_GROUP
) {
897 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
898 return NT_STATUS_ACCESS_DENIED
;
902 if (security_info_sent
& SECINFO_DACL
) {
903 if (!(fsp
->access_mask
& SEC_STD_WRITE_DAC
)) {
904 return NT_STATUS_ACCESS_DENIED
;
906 /* Convert all the generic bits. */
908 security_acl_map_generic(psd
->dacl
, &file_generic_mapping
);
912 if (security_info_sent
& SECINFO_SACL
) {
913 if (!(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
914 return NT_STATUS_ACCESS_DENIED
;
916 /* Convert all the generic bits. */
918 security_acl_map_generic(psd
->sacl
, &file_generic_mapping
);
922 canonicalize_inheritance_bits(psd
);
924 if (DEBUGLEVEL
>= 10) {
925 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp
)));
926 NDR_PRINT_DEBUG(security_descriptor
, psd
);
929 status
= SMB_VFS_FSET_NT_ACL(fsp
, security_info_sent
, psd
);
936 /****************************************************************************
937 Internal fn to set security descriptors from a data blob.
938 ****************************************************************************/
940 NTSTATUS
set_sd_blob(files_struct
*fsp
, uint8_t *data
, uint32_t sd_len
,
941 uint32_t security_info_sent
)
943 struct security_descriptor
*psd
= NULL
;
947 return NT_STATUS_INVALID_PARAMETER
;
950 status
= unmarshall_sec_desc(talloc_tos(), data
, sd_len
, &psd
);
952 if (!NT_STATUS_IS_OK(status
)) {
956 return set_sd(fsp
, psd
, security_info_sent
);
959 /****************************************************************************
960 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
961 ****************************************************************************/
963 static void call_nt_transact_create(connection_struct
*conn
,
964 struct smb_request
*req
,
965 uint16
**ppsetup
, uint32 setup_count
,
966 char **ppparams
, uint32 parameter_count
,
967 char **ppdata
, uint32 data_count
,
968 uint32 max_data_count
)
970 struct smb_filename
*smb_fname
= NULL
;
972 char *params
= *ppparams
;
973 char *data
= *ppdata
;
974 /* Breakout the oplock request bits so we can set the reply bits separately. */
978 files_struct
*fsp
= NULL
;
982 uint32 file_attributes
;
984 uint32 create_disposition
;
985 uint32 create_options
;
987 struct security_descriptor
*sd
= NULL
;
990 struct timespec create_timespec
;
991 struct timespec c_timespec
;
992 struct timespec a_timespec
;
993 struct timespec m_timespec
;
994 struct ea_list
*ea_list
= NULL
;
997 uint64_t allocation_size
;
999 uint8_t oplock_granted
;
1000 struct case_semantics_state
*case_state
= NULL
;
1001 TALLOC_CTX
*ctx
= talloc_tos();
1003 DEBUG(5,("call_nt_transact_create\n"));
1006 * If it's an IPC, use the pipe handler.
1010 if (lp_nt_pipe_support()) {
1011 do_nt_transact_create_pipe(
1013 ppsetup
, setup_count
,
1014 ppparams
, parameter_count
,
1015 ppdata
, data_count
);
1018 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
1023 * Ensure minimum number of parameters sent.
1026 if(parameter_count
< 54) {
1027 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
1028 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1032 flags
= IVAL(params
,0);
1033 access_mask
= IVAL(params
,8);
1034 file_attributes
= IVAL(params
,20);
1035 share_access
= IVAL(params
,24);
1036 create_disposition
= IVAL(params
,28);
1037 create_options
= IVAL(params
,32);
1038 sd_len
= IVAL(params
,36);
1039 ea_len
= IVAL(params
,40);
1040 root_dir_fid
= (uint16
)IVAL(params
,4);
1041 allocation_size
= BVAL(params
,12);
1044 * we need to remove ignored bits when they come directly from the client
1045 * because we reuse some of them for internal stuff
1047 create_options
&= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK
;
1049 srvstr_get_path(ctx
, params
, req
->flags2
, &fname
,
1050 params
+53, parameter_count
-53,
1051 STR_TERMINATE
, &status
);
1052 if (!NT_STATUS_IS_OK(status
)) {
1053 reply_nterror(req
, status
);
1057 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1058 case_state
= set_posix_case_semantics(ctx
, conn
);
1060 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1065 status
= filename_convert(ctx
,
1067 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1069 UCF_PREP_CREATEFILE
,
1073 TALLOC_FREE(case_state
);
1075 if (!NT_STATUS_IS_OK(status
)) {
1076 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
1077 reply_botherror(req
,
1078 NT_STATUS_PATH_NOT_COVERED
,
1079 ERRSRV
, ERRbadpath
);
1082 reply_nterror(req
, status
);
1086 /* Ensure the data_len is correct for the sd and ea values given. */
1087 if ((ea_len
+ sd_len
> data_count
)
1088 || (ea_len
> data_count
) || (sd_len
> data_count
)
1089 || (ea_len
+ sd_len
< ea_len
) || (ea_len
+ sd_len
< sd_len
)) {
1090 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1091 "%u, data_count = %u\n", (unsigned int)ea_len
,
1092 (unsigned int)sd_len
, (unsigned int)data_count
));
1093 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1098 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1101 status
= unmarshall_sec_desc(ctx
, (uint8_t *)data
, sd_len
,
1103 if (!NT_STATUS_IS_OK(status
)) {
1104 DEBUG(10, ("call_nt_transact_create: "
1105 "unmarshall_sec_desc failed: %s\n",
1106 nt_errstr(status
)));
1107 reply_nterror(req
, status
);
1113 if (!lp_ea_support(SNUM(conn
))) {
1114 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1115 "EA's not supported.\n",
1116 (unsigned int)ea_len
));
1117 reply_nterror(req
, NT_STATUS_EAS_NOT_SUPPORTED
);
1122 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1123 "too small (should be more than 10)\n",
1124 (unsigned int)ea_len
));
1125 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1129 /* We have already checked that ea_len <= data_count here. */
1130 ea_list
= read_nttrans_ea_list(talloc_tos(), data
+ sd_len
,
1132 if (ea_list
== NULL
) {
1133 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1137 if (ea_list_has_invalid_name(ea_list
)) {
1138 /* Realloc the size of parameters and data we will return */
1139 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1140 /* Extended response is 32 more byyes. */
1145 params
= nttrans_realloc(ppparams
, param_len
);
1146 if(params
== NULL
) {
1147 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1151 memset(params
, '\0', param_len
);
1152 send_nt_replies(conn
, req
, STATUS_INVALID_EA_NAME
,
1153 params
, param_len
, NULL
, 0);
1158 oplock_request
= (flags
& REQUEST_OPLOCK
) ? EXCLUSIVE_OPLOCK
: 0;
1159 if (oplock_request
) {
1160 oplock_request
|= (flags
& REQUEST_BATCH_OPLOCK
)
1165 * Bug #6898 - clients using Windows opens should
1166 * never be able to set this attribute into the
1169 file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
1171 status
= SMB_VFS_CREATE_FILE(
1174 root_dir_fid
, /* root_dir_fid */
1175 smb_fname
, /* fname */
1176 access_mask
, /* access_mask */
1177 share_access
, /* share_access */
1178 create_disposition
, /* create_disposition*/
1179 create_options
, /* create_options */
1180 file_attributes
, /* file_attributes */
1181 oplock_request
, /* oplock_request */
1182 allocation_size
, /* allocation_size */
1183 0, /* private_flags */
1185 ea_list
, /* ea_list */
1189 if(!NT_STATUS_IS_OK(status
)) {
1190 if (open_was_deferred(req
->sconn
, req
->mid
)) {
1191 /* We have re-scheduled this call, no error. */
1194 reply_openerror(req
, status
);
1198 /* Ensure we're pointing at the correct stat struct. */
1199 TALLOC_FREE(smb_fname
);
1200 smb_fname
= fsp
->fsp_name
;
1203 * If the caller set the extended oplock request bit
1204 * and we granted one (by whatever means) - set the
1205 * correct bit for extended oplock reply.
1208 if (oplock_request
&&
1209 (lp_fake_oplocks(SNUM(conn
))
1210 || EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
))) {
1213 * Exclusive oplock granted
1216 if (flags
& REQUEST_BATCH_OPLOCK
) {
1217 oplock_granted
= BATCH_OPLOCK_RETURN
;
1219 oplock_granted
= EXCLUSIVE_OPLOCK_RETURN
;
1221 } else if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
1222 oplock_granted
= LEVEL_II_OPLOCK_RETURN
;
1224 oplock_granted
= NO_OPLOCK_RETURN
;
1227 file_len
= smb_fname
->st
.st_ex_size
;
1229 /* Realloc the size of parameters and data we will return */
1230 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1231 /* Extended response is 32 more byyes. */
1236 params
= nttrans_realloc(ppparams
, param_len
);
1237 if(params
== NULL
) {
1238 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1243 SCVAL(p
, 0, oplock_granted
);
1246 SSVAL(p
,0,fsp
->fnum
);
1248 if ((create_disposition
== FILE_SUPERSEDE
)
1249 && (info
== FILE_WAS_OVERWRITTEN
)) {
1250 SIVAL(p
,0,FILE_WAS_SUPERSEDED
);
1256 fattr
= dos_mode(conn
, smb_fname
);
1258 fattr
= FILE_ATTRIBUTE_NORMAL
;
1262 create_timespec
= get_create_timespec(conn
, fsp
, smb_fname
);
1263 a_timespec
= smb_fname
->st
.st_ex_atime
;
1264 m_timespec
= smb_fname
->st
.st_ex_mtime
;
1265 c_timespec
= get_change_timespec(conn
, fsp
, smb_fname
);
1267 if (lp_dos_filetime_resolution(SNUM(conn
))) {
1268 dos_filetime_timespec(&create_timespec
);
1269 dos_filetime_timespec(&a_timespec
);
1270 dos_filetime_timespec(&m_timespec
);
1271 dos_filetime_timespec(&c_timespec
);
1274 put_long_date_timespec(conn
->ts_res
, p
, create_timespec
); /* create time. */
1276 put_long_date_timespec(conn
->ts_res
, p
, a_timespec
); /* access time */
1278 put_long_date_timespec(conn
->ts_res
, p
, m_timespec
); /* write time */
1280 put_long_date_timespec(conn
->ts_res
, p
, c_timespec
); /* change time */
1282 SIVAL(p
,0,fattr
); /* File Attributes. */
1284 SOFF_T(p
, 0, SMB_VFS_GET_ALLOC_SIZE(conn
, fsp
, &smb_fname
->st
));
1286 SOFF_T(p
,0,file_len
);
1288 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1289 uint16_t file_status
= (NO_EAS
|NO_SUBSTREAMS
|NO_REPARSETAG
);
1290 size_t num_names
= 0;
1291 unsigned int num_streams
= 0;
1292 struct stream_struct
*streams
= NULL
;
1294 /* Do we have any EA's ? */
1295 status
= get_ea_names_from_file(ctx
, conn
, fsp
,
1296 smb_fname
->base_name
, NULL
, &num_names
);
1297 if (NT_STATUS_IS_OK(status
) && num_names
) {
1298 file_status
&= ~NO_EAS
;
1300 status
= vfs_streaminfo(conn
, NULL
, smb_fname
->base_name
, ctx
,
1301 &num_streams
, &streams
);
1302 /* There is always one stream, ::$DATA. */
1303 if (NT_STATUS_IS_OK(status
) && num_streams
> 1) {
1304 file_status
&= ~NO_SUBSTREAMS
;
1306 TALLOC_FREE(streams
);
1307 SSVAL(p
,2,file_status
);
1310 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
1312 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1315 if (fsp
->is_directory
||
1317 can_write_to_file(conn
, smb_fname
)) {
1318 perms
= FILE_GENERIC_ALL
;
1320 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
1325 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1326 smb_fname_str_dbg(smb_fname
)));
1328 /* Send the required number of replies */
1329 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
1334 /****************************************************************************
1335 Reply to a NT CANCEL request.
1336 conn POINTER CAN BE NULL HERE !
1337 ****************************************************************************/
1339 void reply_ntcancel(struct smb_request
*req
)
1342 * Go through and cancel any pending change notifies.
1345 START_PROFILE(SMBntcancel
);
1346 srv_cancel_sign_response(req
->sconn
);
1347 remove_pending_change_notify_requests_by_mid(req
->sconn
, req
->mid
);
1348 remove_pending_lock_requests_by_mid_smb1(req
->sconn
, req
->mid
);
1350 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1351 (unsigned long long)req
->mid
));
1353 END_PROFILE(SMBntcancel
);
1357 /****************************************************************************
1359 ****************************************************************************/
1361 static NTSTATUS
copy_internals(TALLOC_CTX
*ctx
,
1362 connection_struct
*conn
,
1363 struct smb_request
*req
,
1364 struct smb_filename
*smb_fname_src
,
1365 struct smb_filename
*smb_fname_dst
,
1368 files_struct
*fsp1
,*fsp2
;
1372 NTSTATUS status
= NT_STATUS_OK
;
1375 if (!CAN_WRITE(conn
)) {
1376 status
= NT_STATUS_MEDIA_WRITE_PROTECTED
;
1380 /* Source must already exist. */
1381 if (!VALID_STAT(smb_fname_src
->st
)) {
1382 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1386 /* Ensure attributes match. */
1387 fattr
= dos_mode(conn
, smb_fname_src
);
1388 if ((fattr
& ~attrs
) & (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) {
1389 status
= NT_STATUS_NO_SUCH_FILE
;
1393 /* Disallow if dst file already exists. */
1394 if (VALID_STAT(smb_fname_dst
->st
)) {
1395 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
1399 /* No links from a directory. */
1400 if (S_ISDIR(smb_fname_src
->st
.st_ex_mode
)) {
1401 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
1405 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1406 smb_fname_str_dbg(smb_fname_src
),
1407 smb_fname_str_dbg(smb_fname_dst
)));
1409 status
= SMB_VFS_CREATE_FILE(
1412 0, /* root_dir_fid */
1413 smb_fname_src
, /* fname */
1414 FILE_READ_DATA
|FILE_READ_ATTRIBUTES
|
1415 FILE_READ_EA
, /* access_mask */
1416 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1418 FILE_OPEN
, /* create_disposition*/
1419 0, /* create_options */
1420 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
1421 NO_OPLOCK
, /* oplock_request */
1422 0, /* allocation_size */
1423 0, /* private_flags */
1429 if (!NT_STATUS_IS_OK(status
)) {
1433 status
= SMB_VFS_CREATE_FILE(
1436 0, /* root_dir_fid */
1437 smb_fname_dst
, /* fname */
1438 FILE_WRITE_DATA
|FILE_WRITE_ATTRIBUTES
|
1439 FILE_WRITE_EA
, /* access_mask */
1440 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1442 FILE_CREATE
, /* create_disposition*/
1443 0, /* create_options */
1444 fattr
, /* file_attributes */
1445 NO_OPLOCK
, /* oplock_request */
1446 0, /* allocation_size */
1447 0, /* private_flags */
1453 if (!NT_STATUS_IS_OK(status
)) {
1454 close_file(NULL
, fsp1
, ERROR_CLOSE
);
1458 if (smb_fname_src
->st
.st_ex_size
) {
1459 ret
= vfs_transfer_file(fsp1
, fsp2
, smb_fname_src
->st
.st_ex_size
);
1463 * As we are opening fsp1 read-only we only expect
1464 * an error on close on fsp2 if we are out of space.
1465 * Thus we don't look at the error return from the
1468 close_file(NULL
, fsp1
, NORMAL_CLOSE
);
1470 /* Ensure the modtime is set correctly on the destination file. */
1471 set_close_write_time(fsp2
, smb_fname_src
->st
.st_ex_mtime
);
1473 status
= close_file(NULL
, fsp2
, NORMAL_CLOSE
);
1475 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1476 creates the file. This isn't the correct thing to do in the copy
1478 if (!parent_dirname(talloc_tos(), smb_fname_dst
->base_name
, &parent
,
1480 status
= NT_STATUS_NO_MEMORY
;
1483 file_set_dosmode(conn
, smb_fname_dst
, fattr
, parent
, false);
1484 TALLOC_FREE(parent
);
1486 if (ret
< (off_t
)smb_fname_src
->st
.st_ex_size
) {
1487 status
= NT_STATUS_DISK_FULL
;
1491 if (!NT_STATUS_IS_OK(status
)) {
1492 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1493 nt_errstr(status
), smb_fname_str_dbg(smb_fname_src
),
1494 smb_fname_str_dbg(smb_fname_dst
)));
1500 /****************************************************************************
1501 Reply to a NT rename request.
1502 ****************************************************************************/
1504 void reply_ntrename(struct smb_request
*req
)
1506 connection_struct
*conn
= req
->conn
;
1507 struct smb_filename
*smb_fname_old
= NULL
;
1508 struct smb_filename
*smb_fname_new
= NULL
;
1509 char *oldname
= NULL
;
1510 char *newname
= NULL
;
1513 bool src_has_wcard
= False
;
1514 bool dest_has_wcard
= False
;
1516 uint32_t ucf_flags_src
= 0;
1517 uint32_t ucf_flags_dst
= 0;
1519 TALLOC_CTX
*ctx
= talloc_tos();
1520 bool stream_rename
= false;
1522 START_PROFILE(SMBntrename
);
1525 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1529 attrs
= SVAL(req
->vwv
+0, 0);
1530 rename_type
= SVAL(req
->vwv
+1, 0);
1532 p
= (const char *)req
->buf
+ 1;
1533 p
+= srvstr_get_path_req_wcard(ctx
, req
, &oldname
, p
, STR_TERMINATE
,
1534 &status
, &src_has_wcard
);
1535 if (!NT_STATUS_IS_OK(status
)) {
1536 reply_nterror(req
, status
);
1540 if (ms_has_wild(oldname
)) {
1541 reply_nterror(req
, NT_STATUS_OBJECT_PATH_SYNTAX_BAD
);
1546 p
+= srvstr_get_path_req_wcard(ctx
, req
, &newname
, p
, STR_TERMINATE
,
1547 &status
, &dest_has_wcard
);
1548 if (!NT_STATUS_IS_OK(status
)) {
1549 reply_nterror(req
, status
);
1553 if (!lp_posix_pathnames()) {
1554 /* The newname must begin with a ':' if the
1555 oldname contains a ':'. */
1556 if (strchr_m(oldname
, ':')) {
1557 if (newname
[0] != ':') {
1558 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1561 stream_rename
= true;
1566 * If this is a rename operation, allow wildcards and save the
1567 * destination's last component.
1569 if (rename_type
== RENAME_FLAG_RENAME
) {
1570 ucf_flags_src
= UCF_COND_ALLOW_WCARD_LCOMP
;
1571 ucf_flags_dst
= UCF_COND_ALLOW_WCARD_LCOMP
| UCF_SAVE_LCOMP
;
1574 /* rename_internals() calls unix_convert(), so don't call it here. */
1575 status
= filename_convert(ctx
, conn
,
1576 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1581 if (!NT_STATUS_IS_OK(status
)) {
1582 if (NT_STATUS_EQUAL(status
,
1583 NT_STATUS_PATH_NOT_COVERED
)) {
1584 reply_botherror(req
,
1585 NT_STATUS_PATH_NOT_COVERED
,
1586 ERRSRV
, ERRbadpath
);
1589 reply_nterror(req
, status
);
1593 status
= filename_convert(ctx
, conn
,
1594 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1599 if (!NT_STATUS_IS_OK(status
)) {
1600 if (NT_STATUS_EQUAL(status
,
1601 NT_STATUS_PATH_NOT_COVERED
)) {
1602 reply_botherror(req
,
1603 NT_STATUS_PATH_NOT_COVERED
,
1604 ERRSRV
, ERRbadpath
);
1607 reply_nterror(req
, status
);
1611 if (stream_rename
) {
1612 /* smb_fname_new must be the same as smb_fname_old. */
1613 TALLOC_FREE(smb_fname_new
->base_name
);
1614 smb_fname_new
->base_name
= talloc_strdup(smb_fname_new
,
1615 smb_fname_old
->base_name
);
1616 if (!smb_fname_new
->base_name
) {
1617 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1622 DEBUG(3,("reply_ntrename: %s -> %s\n",
1623 smb_fname_str_dbg(smb_fname_old
),
1624 smb_fname_str_dbg(smb_fname_new
)));
1626 switch(rename_type
) {
1627 case RENAME_FLAG_RENAME
:
1628 status
= rename_internals(ctx
, conn
, req
,
1629 smb_fname_old
, smb_fname_new
,
1630 attrs
, False
, src_has_wcard
,
1634 case RENAME_FLAG_HARD_LINK
:
1635 if (src_has_wcard
|| dest_has_wcard
) {
1637 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1639 status
= hardlink_internals(ctx
, conn
,
1646 case RENAME_FLAG_COPY
:
1647 if (src_has_wcard
|| dest_has_wcard
) {
1649 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1651 status
= copy_internals(ctx
, conn
, req
,
1657 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION
:
1658 status
= NT_STATUS_INVALID_PARAMETER
;
1661 status
= NT_STATUS_ACCESS_DENIED
; /* Default error. */
1665 if (!NT_STATUS_IS_OK(status
)) {
1666 if (open_was_deferred(req
->sconn
, req
->mid
)) {
1667 /* We have re-scheduled this call. */
1671 reply_nterror(req
, status
);
1675 reply_outbuf(req
, 0, 0);
1677 END_PROFILE(SMBntrename
);
1681 /****************************************************************************
1682 Reply to a notify change - queue the request and
1683 don't allow a directory to be opened.
1684 ****************************************************************************/
1686 static void smbd_smb1_notify_reply(struct smb_request
*req
,
1687 NTSTATUS error_code
,
1688 uint8_t *buf
, size_t len
)
1690 send_nt_replies(req
->conn
, req
, error_code
, (char *)buf
, len
, NULL
, 0);
1693 static void call_nt_transact_notify_change(connection_struct
*conn
,
1694 struct smb_request
*req
,
1698 uint32 parameter_count
,
1699 char **ppdata
, uint32 data_count
,
1700 uint32 max_data_count
,
1701 uint32 max_param_count
)
1703 uint16
*setup
= *ppsetup
;
1709 if(setup_count
< 6) {
1710 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1714 fsp
= file_fsp(req
, SVAL(setup
,4));
1715 filter
= IVAL(setup
, 0);
1716 recursive
= (SVAL(setup
, 6) != 0) ? True
: False
;
1718 DEBUG(3,("call_nt_transact_notify_change\n"));
1721 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
1726 char *filter_string
;
1728 if (!(filter_string
= notify_filter_string(NULL
, filter
))) {
1729 reply_nterror(req
,NT_STATUS_NO_MEMORY
);
1733 DEBUG(3,("call_nt_transact_notify_change: notify change "
1734 "called on %s, filter = %s, recursive = %d\n",
1735 fsp_str_dbg(fsp
), filter_string
, recursive
));
1737 TALLOC_FREE(filter_string
);
1740 if((!fsp
->is_directory
) || (conn
!= fsp
->conn
)) {
1741 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1745 if (fsp
->notify
== NULL
) {
1747 status
= change_notify_create(fsp
, filter
, recursive
);
1749 if (!NT_STATUS_IS_OK(status
)) {
1750 DEBUG(10, ("change_notify_create returned %s\n",
1751 nt_errstr(status
)));
1752 reply_nterror(req
, status
);
1757 if (change_notify_fsp_has_changes(fsp
)) {
1760 * We've got changes pending, respond immediately
1764 * TODO: write a torture test to check the filtering behaviour
1768 change_notify_reply(req
,
1772 smbd_smb1_notify_reply
);
1775 * change_notify_reply() above has independently sent its
1782 * No changes pending, queue the request
1785 status
= change_notify_add_request(req
,
1789 smbd_smb1_notify_reply
);
1790 if (!NT_STATUS_IS_OK(status
)) {
1791 reply_nterror(req
, status
);
1796 /****************************************************************************
1797 Reply to an NT transact rename command.
1798 ****************************************************************************/
1800 static void call_nt_transact_rename(connection_struct
*conn
,
1801 struct smb_request
*req
,
1802 uint16
**ppsetup
, uint32 setup_count
,
1803 char **ppparams
, uint32 parameter_count
,
1804 char **ppdata
, uint32 data_count
,
1805 uint32 max_data_count
)
1807 char *params
= *ppparams
;
1808 char *new_name
= NULL
;
1809 files_struct
*fsp
= NULL
;
1810 bool dest_has_wcard
= False
;
1812 TALLOC_CTX
*ctx
= talloc_tos();
1814 if(parameter_count
< 5) {
1815 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1819 fsp
= file_fsp(req
, SVAL(params
, 0));
1820 if (!check_fsp(conn
, req
, fsp
)) {
1823 srvstr_get_path_wcard(ctx
, params
, req
->flags2
, &new_name
, params
+4,
1824 parameter_count
- 4,
1825 STR_TERMINATE
, &status
, &dest_has_wcard
);
1826 if (!NT_STATUS_IS_OK(status
)) {
1827 reply_nterror(req
, status
);
1832 * W2K3 ignores this request as the RAW-RENAME test
1833 * demonstrates, so we do.
1835 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
1837 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1838 fsp_str_dbg(fsp
), new_name
));
1843 /******************************************************************************
1844 Fake up a completely empty SD.
1845 *******************************************************************************/
1847 static NTSTATUS
get_null_nt_acl(TALLOC_CTX
*mem_ctx
, struct security_descriptor
**ppsd
)
1851 *ppsd
= make_standard_sec_desc( mem_ctx
, &global_sid_World
, &global_sid_World
, NULL
, &sd_size
);
1853 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1854 return NT_STATUS_NO_MEMORY
;
1857 return NT_STATUS_OK
;
1860 /****************************************************************************
1861 Reply to query a security descriptor.
1862 Callable from SMB2 and SMB2.
1863 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1865 ****************************************************************************/
1867 NTSTATUS
smbd_do_query_security_desc(connection_struct
*conn
,
1868 TALLOC_CTX
*mem_ctx
,
1870 uint32_t security_info_wanted
,
1871 uint32_t max_data_count
,
1872 uint8_t **ppmarshalled_sd
,
1876 struct security_descriptor
*psd
= NULL
;
1877 TALLOC_CTX
*frame
= talloc_stackframe();
1880 * Get the permissions to return.
1883 if ((security_info_wanted
& SECINFO_SACL
) &&
1884 !(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
1885 DEBUG(10, ("Access to SACL denied.\n"));
1887 return NT_STATUS_ACCESS_DENIED
;
1890 if ((security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|SECINFO_GROUP
)) &&
1891 !(fsp
->access_mask
& SEC_STD_READ_CONTROL
)) {
1892 DEBUG(10, ("Access to DACL, OWNER, or GROUP denied.\n"));
1894 return NT_STATUS_ACCESS_DENIED
;
1897 if (security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|
1898 SECINFO_GROUP
|SECINFO_SACL
)) {
1899 /* Don't return SECINFO_LABEL if anything else was
1900 requested. See bug #8458. */
1901 security_info_wanted
&= ~SECINFO_LABEL
;
1904 if (!lp_nt_acl_support(SNUM(conn
))) {
1905 status
= get_null_nt_acl(frame
, &psd
);
1906 } else if (security_info_wanted
& SECINFO_LABEL
) {
1907 /* Like W2K3 return a null object. */
1908 status
= get_null_nt_acl(frame
, &psd
);
1910 status
= SMB_VFS_FGET_NT_ACL(
1911 fsp
, security_info_wanted
, frame
, &psd
);
1913 if (!NT_STATUS_IS_OK(status
)) {
1918 if (!(security_info_wanted
& SECINFO_OWNER
)) {
1919 psd
->owner_sid
= NULL
;
1921 if (!(security_info_wanted
& SECINFO_GROUP
)) {
1922 psd
->group_sid
= NULL
;
1924 if (!(security_info_wanted
& SECINFO_DACL
)) {
1925 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
1928 if (!(security_info_wanted
& SECINFO_SACL
)) {
1929 psd
->type
&= ~SEC_DESC_SACL_PRESENT
;
1933 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1934 * present in the reply to match Windows behavior */
1935 if (psd
->sacl
== NULL
&&
1936 security_info_wanted
& SECINFO_SACL
)
1937 psd
->type
|= SEC_DESC_SACL_PRESENT
;
1938 if (psd
->dacl
== NULL
&&
1939 security_info_wanted
& SECINFO_DACL
)
1940 psd
->type
|= SEC_DESC_DACL_PRESENT
;
1942 if (security_info_wanted
& SECINFO_LABEL
) {
1943 /* Like W2K3 return a null object. */
1944 psd
->owner_sid
= NULL
;
1945 psd
->group_sid
= NULL
;
1948 psd
->type
&= ~(SEC_DESC_DACL_PRESENT
|SEC_DESC_SACL_PRESENT
);
1951 *psd_size
= ndr_size_security_descriptor(psd
, 0);
1953 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1954 (unsigned long)*psd_size
));
1956 if (DEBUGLEVEL
>= 10) {
1957 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1959 NDR_PRINT_DEBUG(security_descriptor
, psd
);
1962 if (max_data_count
< *psd_size
) {
1964 return NT_STATUS_BUFFER_TOO_SMALL
;
1967 status
= marshall_sec_desc(mem_ctx
, psd
,
1968 ppmarshalled_sd
, psd_size
);
1970 if (!NT_STATUS_IS_OK(status
)) {
1976 return NT_STATUS_OK
;
1979 /****************************************************************************
1980 SMB1 reply to query a security descriptor.
1981 ****************************************************************************/
1983 static void call_nt_transact_query_security_desc(connection_struct
*conn
,
1984 struct smb_request
*req
,
1988 uint32 parameter_count
,
1991 uint32 max_data_count
)
1993 char *params
= *ppparams
;
1994 char *data
= *ppdata
;
1996 uint32 security_info_wanted
;
1997 files_struct
*fsp
= NULL
;
1999 uint8_t *marshalled_sd
= NULL
;
2001 if(parameter_count
< 8) {
2002 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2006 fsp
= file_fsp(req
, SVAL(params
,0));
2008 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2012 security_info_wanted
= IVAL(params
,4);
2014 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
2015 "info_wanted = 0x%x\n", fsp_str_dbg(fsp
),
2016 (unsigned int)security_info_wanted
));
2018 params
= nttrans_realloc(ppparams
, 4);
2019 if(params
== NULL
) {
2020 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2025 * Get the permissions to return.
2028 status
= smbd_do_query_security_desc(conn
,
2031 security_info_wanted
,
2036 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
2037 SIVAL(params
,0,(uint32_t)sd_size
);
2038 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2039 params
, 4, NULL
, 0);
2043 if (!NT_STATUS_IS_OK(status
)) {
2044 reply_nterror(req
, status
);
2048 SMB_ASSERT(sd_size
> 0);
2050 SIVAL(params
,0,(uint32_t)sd_size
);
2052 if (max_data_count
< sd_size
) {
2053 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2054 params
, 4, NULL
, 0);
2059 * Allocate the data we will return.
2062 data
= nttrans_realloc(ppdata
, sd_size
);
2064 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2068 memcpy(data
, marshalled_sd
, sd_size
);
2070 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, 4, data
, (int)sd_size
);
2075 /****************************************************************************
2076 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2077 ****************************************************************************/
2079 static void call_nt_transact_set_security_desc(connection_struct
*conn
,
2080 struct smb_request
*req
,
2084 uint32 parameter_count
,
2087 uint32 max_data_count
)
2089 char *params
= *ppparams
;
2090 char *data
= *ppdata
;
2091 files_struct
*fsp
= NULL
;
2092 uint32 security_info_sent
= 0;
2095 if(parameter_count
< 8) {
2096 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2100 if((fsp
= file_fsp(req
, SVAL(params
,0))) == NULL
) {
2101 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2105 if (!CAN_WRITE(fsp
->conn
)) {
2106 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2110 if(!lp_nt_acl_support(SNUM(conn
))) {
2114 security_info_sent
= IVAL(params
,4);
2116 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2117 fsp_str_dbg(fsp
), (unsigned int)security_info_sent
));
2119 if (data_count
== 0) {
2120 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2124 status
= set_sd_blob(fsp
, (uint8
*)data
, data_count
, security_info_sent
);
2126 if (!NT_STATUS_IS_OK(status
)) {
2127 reply_nterror(req
, status
);
2132 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
2136 /****************************************************************************
2138 ****************************************************************************/
2140 static void call_nt_transact_ioctl(connection_struct
*conn
,
2141 struct smb_request
*req
,
2142 uint16
**ppsetup
, uint32 setup_count
,
2143 char **ppparams
, uint32 parameter_count
,
2144 char **ppdata
, uint32 data_count
,
2145 uint32 max_data_count
)
2153 char *out_data
= NULL
;
2154 uint32 out_data_len
= 0;
2155 char *pdata
= *ppdata
;
2156 TALLOC_CTX
*ctx
= talloc_tos();
2158 if (setup_count
!= 8) {
2159 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count
));
2160 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2164 function
= IVAL(*ppsetup
, 0);
2165 fidnum
= SVAL(*ppsetup
, 4);
2166 isFSctl
= CVAL(*ppsetup
, 6);
2167 compfilter
= CVAL(*ppsetup
, 7);
2169 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2170 function
, fidnum
, isFSctl
, compfilter
));
2172 fsp
=file_fsp(req
, fidnum
);
2175 * We don't really implement IOCTLs, especially on files.
2178 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2180 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2184 /* Has to be for an open file! */
2185 if (!check_fsp_open(conn
, req
, fsp
)) {
2189 SMB_PERFCOUNT_SET_IOCTL(&req
->pcd
, function
);
2192 * out_data might be allocated by the VFS module, but talloc should be
2193 * used, and should be cleaned up when the request ends.
2195 status
= SMB_VFS_FSCTL(fsp
,
2201 (uint8_t **)&out_data
,
2204 if (!NT_STATUS_IS_OK(status
)) {
2205 reply_nterror(req
, status
);
2207 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, out_data
, out_data_len
);
2212 #ifdef HAVE_SYS_QUOTAS
2213 /****************************************************************************
2214 Reply to get user quota
2215 ****************************************************************************/
2217 static void call_nt_transact_get_user_quota(connection_struct
*conn
,
2218 struct smb_request
*req
,
2222 uint32 parameter_count
,
2225 uint32 max_data_count
)
2227 NTSTATUS nt_status
= NT_STATUS_OK
;
2228 char *params
= *ppparams
;
2229 char *pdata
= *ppdata
;
2231 int data_len
=0,param_len
=0;
2234 files_struct
*fsp
= NULL
;
2238 bool start_enum
= True
;
2239 SMB_NTQUOTA_STRUCT qt
;
2240 SMB_NTQUOTA_LIST
*tmp_list
;
2241 SMB_NTQUOTA_HANDLE
*qt_handle
= NULL
;
2246 if (get_current_uid(conn
) != 0) {
2247 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2248 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn
)),
2249 conn
->session_info
->unix_info
->unix_name
));
2250 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2255 * Ensure minimum number of parameters sent.
2258 if (parameter_count
< 4) {
2259 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count
));
2260 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2264 /* maybe we can check the quota_fnum */
2265 fsp
= file_fsp(req
, SVAL(params
,0));
2266 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2267 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2268 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2272 /* the NULL pointer checking for fsp->fake_file_handle->pd
2273 * is done by CHECK_NTQUOTA_HANDLE_OK()
2275 qt_handle
= (SMB_NTQUOTA_HANDLE
*)fsp
->fake_file_handle
->private_data
;
2277 level
= SVAL(params
,2);
2279 /* unknown 12 bytes leading in params */
2282 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE
:
2283 /* seems that we should continue with the enum here --metze */
2285 if (qt_handle
->quota_list
!=NULL
&&
2286 qt_handle
->tmp_list
==NULL
) {
2289 free_ntquota_list(&(qt_handle
->quota_list
));
2291 /* Realloc the size of parameters and data we will return */
2293 params
= nttrans_realloc(ppparams
, param_len
);
2294 if(params
== NULL
) {
2295 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2300 SIVAL(params
,0,data_len
);
2307 case TRANSACT_GET_USER_QUOTA_LIST_START
:
2309 if (qt_handle
->quota_list
==NULL
&&
2310 qt_handle
->tmp_list
==NULL
) {
2314 if (start_enum
&& vfs_get_user_ntquota_list(fsp
,&(qt_handle
->quota_list
))!=0) {
2315 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2319 /* Realloc the size of parameters and data we will return */
2321 params
= nttrans_realloc(ppparams
, param_len
);
2322 if(params
== NULL
) {
2323 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2327 /* we should not trust the value in max_data_count*/
2328 max_data_count
= MIN(max_data_count
,2048);
2330 pdata
= nttrans_realloc(ppdata
, max_data_count
);/* should be max data count from client*/
2332 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2338 /* set params Size of returned Quota Data 4 bytes*/
2339 /* but set it later when we know it */
2341 /* for each entry push the data */
2344 qt_handle
->tmp_list
= qt_handle
->quota_list
;
2347 tmp_list
= qt_handle
->tmp_list
;
2349 for (;((tmp_list
!=NULL
)&&((qt_len
+40+SID_MAX_SIZE
)<max_data_count
));
2350 tmp_list
=tmp_list
->next
,entry
+=entry_len
,qt_len
+=entry_len
) {
2352 sid_len
= ndr_size_dom_sid(
2353 &tmp_list
->quotas
->sid
, 0);
2354 entry_len
= 40 + sid_len
;
2356 /* nextoffset entry 4 bytes */
2357 SIVAL(entry
,0,entry_len
);
2359 /* then the len of the SID 4 bytes */
2360 SIVAL(entry
,4,sid_len
);
2362 /* unknown data 8 bytes uint64_t */
2363 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2365 /* the used disk space 8 bytes uint64_t */
2366 SBIG_UINT(entry
,16,tmp_list
->quotas
->usedspace
);
2368 /* the soft quotas 8 bytes uint64_t */
2369 SBIG_UINT(entry
,24,tmp_list
->quotas
->softlim
);
2371 /* the hard quotas 8 bytes uint64_t */
2372 SBIG_UINT(entry
,32,tmp_list
->quotas
->hardlim
);
2374 /* and now the SID */
2375 sid_linearize(entry
+40, sid_len
, &tmp_list
->quotas
->sid
);
2378 qt_handle
->tmp_list
= tmp_list
;
2380 /* overwrite the offset of the last entry */
2381 SIVAL(entry
-entry_len
,0,0);
2383 data_len
= 4+qt_len
;
2384 /* overwrite the params quota_data_len */
2385 SIVAL(params
,0,data_len
);
2389 case TRANSACT_GET_USER_QUOTA_FOR_SID
:
2391 /* unknown 4 bytes IVAL(pdata,0) */
2393 if (data_count
< 8) {
2394 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count
,8));
2395 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2399 sid_len
= IVAL(pdata
,4);
2400 /* Ensure this is less than 1mb. */
2401 if (sid_len
> (1024*1024)) {
2402 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2406 if (data_count
< 8+sid_len
) {
2407 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count
,(unsigned long)(8+sid_len
)));
2408 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2412 data_len
= 4+40+sid_len
;
2414 if (max_data_count
< data_len
) {
2415 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2416 max_data_count
, data_len
));
2418 SIVAL(params
,0,data_len
);
2420 nt_status
= NT_STATUS_BUFFER_TOO_SMALL
;
2424 if (!sid_parse(pdata
+8,sid_len
,&sid
)) {
2425 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2429 if (vfs_get_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2432 * we have to return zero's in all fields
2433 * instead of returning an error here
2438 /* Realloc the size of parameters and data we will return */
2440 params
= nttrans_realloc(ppparams
, param_len
);
2441 if(params
== NULL
) {
2442 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2446 pdata
= nttrans_realloc(ppdata
, data_len
);
2448 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2454 /* set params Size of returned Quota Data 4 bytes*/
2455 SIVAL(params
,0,data_len
);
2457 /* nextoffset entry 4 bytes */
2460 /* then the len of the SID 4 bytes */
2461 SIVAL(entry
,4,sid_len
);
2463 /* unknown data 8 bytes uint64_t */
2464 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2466 /* the used disk space 8 bytes uint64_t */
2467 SBIG_UINT(entry
,16,qt
.usedspace
);
2469 /* the soft quotas 8 bytes uint64_t */
2470 SBIG_UINT(entry
,24,qt
.softlim
);
2472 /* the hard quotas 8 bytes uint64_t */
2473 SBIG_UINT(entry
,32,qt
.hardlim
);
2475 /* and now the SID */
2476 sid_linearize(entry
+40, sid_len
, &sid
);
2481 DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
2483 fsp_fnum_dbg(fsp
), level
));
2484 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2489 send_nt_replies(conn
, req
, nt_status
, params
, param_len
,
2493 /****************************************************************************
2494 Reply to set user quota
2495 ****************************************************************************/
2497 static void call_nt_transact_set_user_quota(connection_struct
*conn
,
2498 struct smb_request
*req
,
2502 uint32 parameter_count
,
2505 uint32 max_data_count
)
2507 char *params
= *ppparams
;
2508 char *pdata
= *ppdata
;
2509 int data_len
=0,param_len
=0;
2510 SMB_NTQUOTA_STRUCT qt
;
2513 files_struct
*fsp
= NULL
;
2518 if (get_current_uid(conn
) != 0) {
2519 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2520 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn
)),
2521 conn
->session_info
->unix_info
->unix_name
));
2522 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2527 * Ensure minimum number of parameters sent.
2530 if (parameter_count
< 2) {
2531 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count
));
2532 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2536 /* maybe we can check the quota_fnum */
2537 fsp
= file_fsp(req
, SVAL(params
,0));
2538 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2539 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2540 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2544 if (data_count
< 40) {
2545 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count
,40));
2546 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2550 /* offset to next quota record.
2551 * 4 bytes IVAL(pdata,0)
2556 sid_len
= IVAL(pdata
,4);
2558 if (data_count
< 40+sid_len
|| (40+sid_len
< sid_len
)) {
2559 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count
,(unsigned long)40+sid_len
));
2560 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2564 /* unknown 8 bytes in pdata
2565 * maybe its the change time in NTTIME
2568 /* the used space 8 bytes (uint64_t)*/
2569 qt
.usedspace
= BVAL(pdata
,16);
2571 /* the soft quotas 8 bytes (uint64_t)*/
2572 qt
.softlim
= BVAL(pdata
,24);
2574 /* the hard quotas 8 bytes (uint64_t)*/
2575 qt
.hardlim
= BVAL(pdata
,32);
2577 if (!sid_parse(pdata
+40,sid_len
,&sid
)) {
2578 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2582 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid
)));
2584 /* 44 unknown bytes left... */
2586 if (vfs_set_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2587 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2591 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
,
2594 #endif /* HAVE_SYS_QUOTAS */
2596 static void handle_nttrans(connection_struct
*conn
,
2597 struct trans_state
*state
,
2598 struct smb_request
*req
)
2600 if (get_Protocol() >= PROTOCOL_NT1
) {
2601 req
->flags2
|= 0x40; /* IS_LONG_NAME */
2602 SSVAL(discard_const_p(uint8_t, req
->inbuf
),smb_flg2
,req
->flags2
);
2606 SMB_PERFCOUNT_SET_SUBOP(&req
->pcd
, state
->call
);
2608 /* Now we must call the relevant NT_TRANS function */
2609 switch(state
->call
) {
2610 case NT_TRANSACT_CREATE
:
2612 START_PROFILE(NT_transact_create
);
2613 call_nt_transact_create(
2615 &state
->setup
, state
->setup_count
,
2616 &state
->param
, state
->total_param
,
2617 &state
->data
, state
->total_data
,
2618 state
->max_data_return
);
2619 END_PROFILE(NT_transact_create
);
2623 case NT_TRANSACT_IOCTL
:
2625 START_PROFILE(NT_transact_ioctl
);
2626 call_nt_transact_ioctl(
2628 &state
->setup
, state
->setup_count
,
2629 &state
->param
, state
->total_param
,
2630 &state
->data
, state
->total_data
,
2631 state
->max_data_return
);
2632 END_PROFILE(NT_transact_ioctl
);
2636 case NT_TRANSACT_SET_SECURITY_DESC
:
2638 START_PROFILE(NT_transact_set_security_desc
);
2639 call_nt_transact_set_security_desc(
2641 &state
->setup
, state
->setup_count
,
2642 &state
->param
, state
->total_param
,
2643 &state
->data
, state
->total_data
,
2644 state
->max_data_return
);
2645 END_PROFILE(NT_transact_set_security_desc
);
2649 case NT_TRANSACT_NOTIFY_CHANGE
:
2651 START_PROFILE(NT_transact_notify_change
);
2652 call_nt_transact_notify_change(
2654 &state
->setup
, state
->setup_count
,
2655 &state
->param
, state
->total_param
,
2656 &state
->data
, state
->total_data
,
2657 state
->max_data_return
,
2658 state
->max_param_return
);
2659 END_PROFILE(NT_transact_notify_change
);
2663 case NT_TRANSACT_RENAME
:
2665 START_PROFILE(NT_transact_rename
);
2666 call_nt_transact_rename(
2668 &state
->setup
, state
->setup_count
,
2669 &state
->param
, state
->total_param
,
2670 &state
->data
, state
->total_data
,
2671 state
->max_data_return
);
2672 END_PROFILE(NT_transact_rename
);
2676 case NT_TRANSACT_QUERY_SECURITY_DESC
:
2678 START_PROFILE(NT_transact_query_security_desc
);
2679 call_nt_transact_query_security_desc(
2681 &state
->setup
, state
->setup_count
,
2682 &state
->param
, state
->total_param
,
2683 &state
->data
, state
->total_data
,
2684 state
->max_data_return
);
2685 END_PROFILE(NT_transact_query_security_desc
);
2689 #ifdef HAVE_SYS_QUOTAS
2690 case NT_TRANSACT_GET_USER_QUOTA
:
2692 START_PROFILE(NT_transact_get_user_quota
);
2693 call_nt_transact_get_user_quota(
2695 &state
->setup
, state
->setup_count
,
2696 &state
->param
, state
->total_param
,
2697 &state
->data
, state
->total_data
,
2698 state
->max_data_return
);
2699 END_PROFILE(NT_transact_get_user_quota
);
2703 case NT_TRANSACT_SET_USER_QUOTA
:
2705 START_PROFILE(NT_transact_set_user_quota
);
2706 call_nt_transact_set_user_quota(
2708 &state
->setup
, state
->setup_count
,
2709 &state
->param
, state
->total_param
,
2710 &state
->data
, state
->total_data
,
2711 state
->max_data_return
);
2712 END_PROFILE(NT_transact_set_user_quota
);
2715 #endif /* HAVE_SYS_QUOTAS */
2718 /* Error in request */
2719 DEBUG(0,("handle_nttrans: Unknown request %d in "
2720 "nttrans call\n", state
->call
));
2721 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2727 /****************************************************************************
2728 Reply to a SMBNTtrans.
2729 ****************************************************************************/
2731 void reply_nttrans(struct smb_request
*req
)
2733 connection_struct
*conn
= req
->conn
;
2738 uint16 function_code
;
2740 struct trans_state
*state
;
2742 START_PROFILE(SMBnttrans
);
2744 if (req
->wct
< 19) {
2745 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2746 END_PROFILE(SMBnttrans
);
2750 pscnt
= IVAL(req
->vwv
+9, 1);
2751 psoff
= IVAL(req
->vwv
+11, 1);
2752 dscnt
= IVAL(req
->vwv
+13, 1);
2753 dsoff
= IVAL(req
->vwv
+15, 1);
2754 function_code
= SVAL(req
->vwv
+18, 0);
2756 if (IS_IPC(conn
) && (function_code
!= NT_TRANSACT_CREATE
)) {
2757 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2758 END_PROFILE(SMBnttrans
);
2762 result
= allow_new_trans(conn
->pending_trans
, req
->mid
);
2763 if (!NT_STATUS_IS_OK(result
)) {
2764 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result
)));
2765 reply_nterror(req
, result
);
2766 END_PROFILE(SMBnttrans
);
2770 if ((state
= talloc(conn
, struct trans_state
)) == NULL
) {
2771 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2772 END_PROFILE(SMBnttrans
);
2776 state
->cmd
= SMBnttrans
;
2778 state
->mid
= req
->mid
;
2779 state
->vuid
= req
->vuid
;
2780 state
->total_data
= IVAL(req
->vwv
+3, 1);
2782 state
->total_param
= IVAL(req
->vwv
+1, 1);
2783 state
->param
= NULL
;
2784 state
->max_data_return
= IVAL(req
->vwv
+7, 1);
2785 state
->max_param_return
= IVAL(req
->vwv
+5, 1);
2787 /* setup count is in *words* */
2788 state
->setup_count
= 2*CVAL(req
->vwv
+17, 1);
2789 state
->setup
= NULL
;
2790 state
->call
= function_code
;
2792 DEBUG(10, ("num_setup=%u, "
2793 "param_total=%u, this_param=%u, max_param=%u, "
2794 "data_total=%u, this_data=%u, max_data=%u, "
2795 "param_offset=%u, data_offset=%u\n",
2796 (unsigned)state
->setup_count
,
2797 (unsigned)state
->total_param
, (unsigned)pscnt
,
2798 (unsigned)state
->max_param_return
,
2799 (unsigned)state
->total_data
, (unsigned)dscnt
,
2800 (unsigned)state
->max_data_return
,
2801 (unsigned)psoff
, (unsigned)dsoff
));
2804 * All nttrans messages we handle have smb_wct == 19 +
2805 * state->setup_count. Ensure this is so as a sanity check.
2808 if(req
->wct
!= 19 + (state
->setup_count
/2)) {
2809 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2810 req
->wct
, 19 + (state
->setup_count
/2)));
2814 /* Don't allow more than 128mb for each value. */
2815 if ((state
->total_data
> (1024*1024*128)) ||
2816 (state
->total_param
> (1024*1024*128))) {
2817 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2818 END_PROFILE(SMBnttrans
);
2822 if ((dscnt
> state
->total_data
) || (pscnt
> state
->total_param
))
2825 if (state
->total_data
) {
2827 if (trans_oob(state
->total_data
, 0, dscnt
)
2828 || trans_oob(smb_len(req
->inbuf
), dsoff
, dscnt
)) {
2832 /* Can't use talloc here, the core routines do realloc on the
2833 * params and data. */
2834 if ((state
->data
= (char *)SMB_MALLOC(state
->total_data
)) == NULL
) {
2835 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2836 "bytes !\n", (unsigned int)state
->total_data
));
2838 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2839 END_PROFILE(SMBnttrans
);
2843 memcpy(state
->data
,smb_base(req
->inbuf
)+dsoff
,dscnt
);
2846 if (state
->total_param
) {
2848 if (trans_oob(state
->total_param
, 0, pscnt
)
2849 || trans_oob(smb_len(req
->inbuf
), psoff
, pscnt
)) {
2853 /* Can't use talloc here, the core routines do realloc on the
2854 * params and data. */
2855 if ((state
->param
= (char *)SMB_MALLOC(state
->total_param
)) == NULL
) {
2856 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2857 "bytes !\n", (unsigned int)state
->total_param
));
2858 SAFE_FREE(state
->data
);
2860 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2861 END_PROFILE(SMBnttrans
);
2865 memcpy(state
->param
,smb_base(req
->inbuf
)+psoff
,pscnt
);
2868 state
->received_data
= dscnt
;
2869 state
->received_param
= pscnt
;
2871 if(state
->setup_count
> 0) {
2872 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2873 state
->setup_count
));
2876 * No overflow possible here, state->setup_count is an
2877 * unsigned int, being filled by a single byte from
2878 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2879 * below is not necessary, it's here to clarify things. The
2880 * validity of req->vwv and req->wct has been checked in
2881 * init_smb_request already.
2883 if ((state
->setup_count
/2) + 19 > (unsigned int)req
->wct
) {
2887 state
->setup
= (uint16
*)TALLOC(state
, state
->setup_count
);
2888 if (state
->setup
== NULL
) {
2889 DEBUG(0,("reply_nttrans : Out of memory\n"));
2890 SAFE_FREE(state
->data
);
2891 SAFE_FREE(state
->param
);
2893 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2894 END_PROFILE(SMBnttrans
);
2898 memcpy(state
->setup
, req
->vwv
+19, state
->setup_count
);
2899 dump_data(10, (uint8
*)state
->setup
, state
->setup_count
);
2902 if ((state
->received_data
== state
->total_data
) &&
2903 (state
->received_param
== state
->total_param
)) {
2904 handle_nttrans(conn
, state
, req
);
2905 SAFE_FREE(state
->param
);
2906 SAFE_FREE(state
->data
);
2908 END_PROFILE(SMBnttrans
);
2912 DLIST_ADD(conn
->pending_trans
, state
);
2914 /* We need to send an interim response then receive the rest
2915 of the parameter/data bytes */
2916 reply_outbuf(req
, 0, 0);
2917 show_msg((char *)req
->outbuf
);
2918 END_PROFILE(SMBnttrans
);
2923 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2924 SAFE_FREE(state
->data
);
2925 SAFE_FREE(state
->param
);
2927 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2928 END_PROFILE(SMBnttrans
);
2932 /****************************************************************************
2933 Reply to a SMBnttranss
2934 ****************************************************************************/
2936 void reply_nttranss(struct smb_request
*req
)
2938 connection_struct
*conn
= req
->conn
;
2939 uint32_t pcnt
,poff
,dcnt
,doff
,pdisp
,ddisp
;
2940 struct trans_state
*state
;
2942 START_PROFILE(SMBnttranss
);
2944 show_msg((const char *)req
->inbuf
);
2946 /* Windows clients expect all replies to
2947 an NT transact secondary (SMBnttranss 0xA1)
2948 to have a command code of NT transact
2949 (SMBnttrans 0xA0). See bug #8989 for details. */
2950 req
->cmd
= SMBnttrans
;
2952 if (req
->wct
< 18) {
2953 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2954 END_PROFILE(SMBnttranss
);
2958 for (state
= conn
->pending_trans
; state
!= NULL
;
2959 state
= state
->next
) {
2960 if (state
->mid
== req
->mid
) {
2965 if ((state
== NULL
) || (state
->cmd
!= SMBnttrans
)) {
2966 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2967 END_PROFILE(SMBnttranss
);
2971 /* Revise state->total_param and state->total_data in case they have
2972 changed downwards */
2973 if (IVAL(req
->vwv
+1, 1) < state
->total_param
) {
2974 state
->total_param
= IVAL(req
->vwv
+1, 1);
2976 if (IVAL(req
->vwv
+3, 1) < state
->total_data
) {
2977 state
->total_data
= IVAL(req
->vwv
+3, 1);
2980 pcnt
= IVAL(req
->vwv
+5, 1);
2981 poff
= IVAL(req
->vwv
+7, 1);
2982 pdisp
= IVAL(req
->vwv
+9, 1);
2984 dcnt
= IVAL(req
->vwv
+11, 1);
2985 doff
= IVAL(req
->vwv
+13, 1);
2986 ddisp
= IVAL(req
->vwv
+15, 1);
2988 state
->received_param
+= pcnt
;
2989 state
->received_data
+= dcnt
;
2991 if ((state
->received_data
> state
->total_data
) ||
2992 (state
->received_param
> state
->total_param
))
2996 if (trans_oob(state
->total_param
, pdisp
, pcnt
)
2997 || trans_oob(smb_len(req
->inbuf
), poff
, pcnt
)) {
3000 memcpy(state
->param
+pdisp
, smb_base(req
->inbuf
)+poff
,pcnt
);
3004 if (trans_oob(state
->total_data
, ddisp
, dcnt
)
3005 || trans_oob(smb_len(req
->inbuf
), doff
, dcnt
)) {
3008 memcpy(state
->data
+ddisp
, smb_base(req
->inbuf
)+doff
,dcnt
);
3011 if ((state
->received_param
< state
->total_param
) ||
3012 (state
->received_data
< state
->total_data
)) {
3013 END_PROFILE(SMBnttranss
);
3017 handle_nttrans(conn
, state
, req
);
3019 DLIST_REMOVE(conn
->pending_trans
, state
);
3020 SAFE_FREE(state
->data
);
3021 SAFE_FREE(state
->param
);
3023 END_PROFILE(SMBnttranss
);
3028 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3029 DLIST_REMOVE(conn
->pending_trans
, state
);
3030 SAFE_FREE(state
->data
);
3031 SAFE_FREE(state
->param
);
3033 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3034 END_PROFILE(SMBnttranss
);