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 smbXsrv_connection
*xconn
= req
->xconn
;
71 int max_send
= xconn
->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(xconn
,
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(xconn
,
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_t 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
;
442 uint32_t access_mask
;
443 uint32_t file_attributes
;
444 uint32_t share_access
;
445 uint32_t create_disposition
;
446 uint32_t create_options
;
447 uint16_t root_dir_fid
;
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
;
465 TALLOC_CTX
*ctx
= talloc_tos();
467 START_PROFILE(SMBntcreateX
);
470 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
474 flags
= IVAL(req
->vwv
+3, 1);
475 access_mask
= IVAL(req
->vwv
+7, 1);
476 file_attributes
= IVAL(req
->vwv
+13, 1);
477 share_access
= IVAL(req
->vwv
+15, 1);
478 create_disposition
= IVAL(req
->vwv
+17, 1);
479 create_options
= IVAL(req
->vwv
+19, 1);
480 root_dir_fid
= (uint16_t)IVAL(req
->vwv
+5, 1);
482 allocation_size
= BVAL(req
->vwv
+9, 1);
484 srvstr_get_path_req(ctx
, req
, &fname
, (const char *)req
->buf
,
485 STR_TERMINATE
, &status
);
487 if (!NT_STATUS_IS_OK(status
)) {
488 reply_nterror(req
, status
);
492 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
493 "file_attributes = 0x%x, share_access = 0x%x, "
494 "create_disposition = 0x%x create_options = 0x%x "
495 "root_dir_fid = 0x%x, fname = %s\n",
497 (unsigned int)access_mask
,
498 (unsigned int)file_attributes
,
499 (unsigned int)share_access
,
500 (unsigned int)create_disposition
,
501 (unsigned int)create_options
,
502 (unsigned int)root_dir_fid
,
506 * we need to remove ignored bits when they come directly from the client
507 * because we reuse some of them for internal stuff
509 create_options
&= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK
;
512 * If it's an IPC, use the pipe handler.
516 if (lp_nt_pipe_support()) {
517 do_ntcreate_pipe_open(conn
, req
);
520 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
524 oplock_request
= (flags
& REQUEST_OPLOCK
) ? EXCLUSIVE_OPLOCK
: 0;
525 if (oplock_request
) {
526 oplock_request
|= (flags
& REQUEST_BATCH_OPLOCK
)
530 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
531 case_state
= set_posix_case_semantics(ctx
, conn
);
533 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
538 ucf_flags
= filename_create_ucf_flags(req
, create_disposition
);
539 status
= filename_convert(ctx
,
541 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
547 TALLOC_FREE(case_state
);
549 if (!NT_STATUS_IS_OK(status
)) {
550 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
552 NT_STATUS_PATH_NOT_COVERED
,
556 reply_nterror(req
, status
);
561 * Bug #6898 - clients using Windows opens should
562 * never be able to set this attribute into the
565 file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
567 status
= SMB_VFS_CREATE_FILE(
570 root_dir_fid
, /* root_dir_fid */
571 smb_fname
, /* fname */
572 access_mask
, /* access_mask */
573 share_access
, /* share_access */
574 create_disposition
, /* create_disposition*/
575 create_options
, /* create_options */
576 file_attributes
, /* file_attributes */
577 oplock_request
, /* oplock_request */
579 allocation_size
, /* allocation_size */
580 0, /* private_flags */
585 NULL
, NULL
); /* create context */
587 if (!NT_STATUS_IS_OK(status
)) {
588 if (open_was_deferred(req
->xconn
, req
->mid
)) {
589 /* We have re-scheduled this call, no error. */
592 reply_openerror(req
, status
);
596 /* Ensure we're pointing at the correct stat struct. */
597 TALLOC_FREE(smb_fname
);
598 smb_fname
= fsp
->fsp_name
;
601 * If the caller set the extended oplock request bit
602 * and we granted one (by whatever means) - set the
603 * correct bit for extended oplock reply.
606 if (oplock_request
&&
607 (lp_fake_oplocks(SNUM(conn
))
608 || EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
))) {
611 * Exclusive oplock granted
614 if (flags
& REQUEST_BATCH_OPLOCK
) {
615 oplock_granted
= BATCH_OPLOCK_RETURN
;
617 oplock_granted
= EXCLUSIVE_OPLOCK_RETURN
;
619 } else if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
620 oplock_granted
= LEVEL_II_OPLOCK_RETURN
;
622 oplock_granted
= NO_OPLOCK_RETURN
;
625 file_len
= smb_fname
->st
.st_ex_size
;
627 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
628 /* This is very strange. We
629 * return 50 words, but only set
630 * the wcnt to 42 ? It's definitely
631 * what happens on the wire....
633 reply_outbuf(req
, 50, 0);
634 SCVAL(req
->outbuf
,smb_wct
,42);
636 reply_outbuf(req
, 34, 0);
639 SSVAL(req
->outbuf
, smb_vwv0
, 0xff); /* andx chain ends */
640 SSVAL(req
->outbuf
, smb_vwv1
, 0); /* no andx offset */
642 p
= (char *)req
->outbuf
+ smb_vwv2
;
644 SCVAL(p
, 0, oplock_granted
);
647 SSVAL(p
,0,fsp
->fnum
);
649 if ((create_disposition
== FILE_SUPERSEDE
)
650 && (info
== FILE_WAS_OVERWRITTEN
)) {
651 SIVAL(p
,0,FILE_WAS_SUPERSEDED
);
657 fattr
= dos_mode(conn
, smb_fname
);
659 fattr
= FILE_ATTRIBUTE_NORMAL
;
663 create_timespec
= get_create_timespec(conn
, fsp
, smb_fname
);
664 a_timespec
= smb_fname
->st
.st_ex_atime
;
665 m_timespec
= smb_fname
->st
.st_ex_mtime
;
666 c_timespec
= get_change_timespec(conn
, fsp
, smb_fname
);
668 if (lp_dos_filetime_resolution(SNUM(conn
))) {
669 dos_filetime_timespec(&create_timespec
);
670 dos_filetime_timespec(&a_timespec
);
671 dos_filetime_timespec(&m_timespec
);
672 dos_filetime_timespec(&c_timespec
);
675 put_long_date_timespec(conn
->ts_res
, p
, create_timespec
); /* create time. */
677 put_long_date_timespec(conn
->ts_res
, p
, a_timespec
); /* access time */
679 put_long_date_timespec(conn
->ts_res
, p
, m_timespec
); /* write time */
681 put_long_date_timespec(conn
->ts_res
, p
, c_timespec
); /* change time */
683 SIVAL(p
,0,fattr
); /* File Attributes. */
685 SOFF_T(p
, 0, SMB_VFS_GET_ALLOC_SIZE(conn
,fsp
,&smb_fname
->st
));
687 SOFF_T(p
,0,file_len
);
689 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
690 uint16_t file_status
= (NO_EAS
|NO_SUBSTREAMS
|NO_REPARSETAG
);
691 size_t num_names
= 0;
692 unsigned int num_streams
= 0;
693 struct stream_struct
*streams
= NULL
;
695 /* Do we have any EA's ? */
696 status
= get_ea_names_from_file(ctx
, conn
, fsp
,
697 smb_fname
, NULL
, &num_names
);
698 if (NT_STATUS_IS_OK(status
) && num_names
) {
699 file_status
&= ~NO_EAS
;
701 status
= vfs_streaminfo(conn
, NULL
, smb_fname
, ctx
,
702 &num_streams
, &streams
);
703 /* There is always one stream, ::$DATA. */
704 if (NT_STATUS_IS_OK(status
) && num_streams
> 1) {
705 file_status
&= ~NO_SUBSTREAMS
;
707 TALLOC_FREE(streams
);
708 SSVAL(p
,2,file_status
);
711 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
713 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
716 if (fsp
->is_directory
||
718 can_write_to_file(conn
, smb_fname
)) {
719 perms
= FILE_GENERIC_ALL
;
721 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
726 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
727 fsp_fnum_dbg(fsp
), smb_fname_str_dbg(smb_fname
)));
730 END_PROFILE(SMBntcreateX
);
734 /****************************************************************************
735 Reply to a NT_TRANSACT_CREATE call to open a pipe.
736 ****************************************************************************/
738 static void do_nt_transact_create_pipe(connection_struct
*conn
,
739 struct smb_request
*req
,
740 uint16_t **ppsetup
, uint32_t setup_count
,
741 char **ppparams
, uint32_t parameter_count
,
742 char **ppdata
, uint32_t data_count
)
745 char *params
= *ppparams
;
746 uint16_t pnum
= FNUM_FIELD_INVALID
;
751 TALLOC_CTX
*ctx
= talloc_tos();
754 * Ensure minimum number of parameters sent.
757 if(parameter_count
< 54) {
758 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
759 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
763 flags
= IVAL(params
,0);
765 if (req
->posix_pathnames
) {
766 srvstr_get_path_posix(ctx
,
784 if (!NT_STATUS_IS_OK(status
)) {
785 reply_nterror(req
, status
);
789 nt_open_pipe(fname
, conn
, req
, &pnum
);
796 /* Realloc the size of parameters and data we will return */
797 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
798 /* Extended response is 32 more byyes. */
803 params
= nttrans_realloc(ppparams
, param_len
);
805 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
810 SCVAL(p
,0,NO_OPLOCK_RETURN
);
815 SIVAL(p
,0,FILE_WAS_OPENED
);
819 SIVAL(p
,0,FILE_ATTRIBUTE_NORMAL
); /* File Attributes. */
822 SSVAL(p
,0,FILE_TYPE_MESSAGE_MODE_PIPE
);
824 SSVAL(p
,2, 0x5FF); /* ? */
827 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
829 SIVAL(p
,0,FILE_GENERIC_ALL
);
831 * For pipes W2K3 seems to return
833 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
835 SIVAL(p
,4,(FILE_GENERIC_READ
|FILE_GENERIC_WRITE
)&~FILE_APPEND_DATA
);
838 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname
));
840 /* Send the required number of replies */
841 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
846 /*********************************************************************
847 Windows seems to do canonicalization of inheritance bits. Do the
849 *********************************************************************/
851 static void canonicalize_inheritance_bits(struct security_descriptor
*psd
)
853 bool set_auto_inherited
= false;
856 * We need to filter out the
857 * SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ
858 * bits. If both are set we store SEC_DESC_DACL_AUTO_INHERITED
859 * as this alters whether SEC_ACE_FLAG_INHERITED_ACE is set
860 * when an ACE is inherited. Otherwise we zero these bits out.
863 * http://social.msdn.microsoft.com/Forums/eu/os_fileservices/thread/11f77b68-731e-407d-b1b3-064750716531
868 if ((psd
->type
& (SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
))
869 == (SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
)) {
870 set_auto_inherited
= true;
873 psd
->type
&= ~(SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
);
874 if (set_auto_inherited
) {
875 psd
->type
|= SEC_DESC_DACL_AUTO_INHERITED
;
879 /****************************************************************************
880 Internal fn to set security descriptors.
881 ****************************************************************************/
883 NTSTATUS
set_sd(files_struct
*fsp
, struct security_descriptor
*psd
,
884 uint32_t security_info_sent
)
888 if (!CAN_WRITE(fsp
->conn
)) {
889 return NT_STATUS_ACCESS_DENIED
;
892 if (!lp_nt_acl_support(SNUM(fsp
->conn
))) {
896 if (S_ISLNK(fsp
->fsp_name
->st
.st_ex_mode
)) {
897 DEBUG(10, ("ACL set on symlink %s denied.\n",
899 return NT_STATUS_ACCESS_DENIED
;
902 if (psd
->owner_sid
== NULL
) {
903 security_info_sent
&= ~SECINFO_OWNER
;
905 if (psd
->group_sid
== NULL
) {
906 security_info_sent
&= ~SECINFO_GROUP
;
909 /* Ensure we have at least one thing set. */
910 if ((security_info_sent
& (SECINFO_OWNER
|SECINFO_GROUP
|SECINFO_DACL
|SECINFO_SACL
)) == 0) {
915 /* Ensure we have the rights to do this. */
916 if (security_info_sent
& SECINFO_OWNER
) {
917 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
918 return NT_STATUS_ACCESS_DENIED
;
922 if (security_info_sent
& SECINFO_GROUP
) {
923 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
924 return NT_STATUS_ACCESS_DENIED
;
928 if (security_info_sent
& SECINFO_DACL
) {
929 if (!(fsp
->access_mask
& SEC_STD_WRITE_DAC
)) {
930 return NT_STATUS_ACCESS_DENIED
;
932 /* Convert all the generic bits. */
934 security_acl_map_generic(psd
->dacl
, &file_generic_mapping
);
938 if (security_info_sent
& SECINFO_SACL
) {
939 if (!(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
940 return NT_STATUS_ACCESS_DENIED
;
942 /* Convert all the generic bits. */
944 security_acl_map_generic(psd
->sacl
, &file_generic_mapping
);
948 canonicalize_inheritance_bits(psd
);
950 if (DEBUGLEVEL
>= 10) {
951 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp
)));
952 NDR_PRINT_DEBUG(security_descriptor
, psd
);
955 status
= SMB_VFS_FSET_NT_ACL(fsp
, security_info_sent
, psd
);
962 /****************************************************************************
963 Internal fn to set security descriptors from a data blob.
964 ****************************************************************************/
966 NTSTATUS
set_sd_blob(files_struct
*fsp
, uint8_t *data
, uint32_t sd_len
,
967 uint32_t security_info_sent
)
969 struct security_descriptor
*psd
= NULL
;
973 return NT_STATUS_INVALID_PARAMETER
;
976 status
= unmarshall_sec_desc(talloc_tos(), data
, sd_len
, &psd
);
978 if (!NT_STATUS_IS_OK(status
)) {
982 return set_sd(fsp
, psd
, security_info_sent
);
985 /****************************************************************************
986 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
987 ****************************************************************************/
989 static void call_nt_transact_create(connection_struct
*conn
,
990 struct smb_request
*req
,
991 uint16_t **ppsetup
, uint32_t setup_count
,
992 char **ppparams
, uint32_t parameter_count
,
993 char **ppdata
, uint32_t data_count
,
994 uint32_t max_data_count
)
996 struct smb_filename
*smb_fname
= NULL
;
998 char *params
= *ppparams
;
999 char *data
= *ppdata
;
1000 /* Breakout the oplock request bits so we can set the reply bits separately. */
1004 files_struct
*fsp
= NULL
;
1007 uint32_t access_mask
;
1008 uint32_t file_attributes
;
1009 uint32_t share_access
;
1010 uint32_t create_disposition
;
1011 uint32_t create_options
;
1013 struct security_descriptor
*sd
= NULL
;
1015 uint16_t root_dir_fid
;
1016 struct timespec create_timespec
;
1017 struct timespec c_timespec
;
1018 struct timespec a_timespec
;
1019 struct timespec m_timespec
;
1020 struct ea_list
*ea_list
= NULL
;
1023 uint64_t allocation_size
;
1025 uint8_t oplock_granted
;
1026 struct case_semantics_state
*case_state
= NULL
;
1028 TALLOC_CTX
*ctx
= talloc_tos();
1030 DEBUG(5,("call_nt_transact_create\n"));
1033 * If it's an IPC, use the pipe handler.
1037 if (lp_nt_pipe_support()) {
1038 do_nt_transact_create_pipe(
1040 ppsetup
, setup_count
,
1041 ppparams
, parameter_count
,
1042 ppdata
, data_count
);
1045 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
1050 * Ensure minimum number of parameters sent.
1053 if(parameter_count
< 54) {
1054 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
1055 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1059 flags
= IVAL(params
,0);
1060 access_mask
= IVAL(params
,8);
1061 file_attributes
= IVAL(params
,20);
1062 share_access
= IVAL(params
,24);
1063 create_disposition
= IVAL(params
,28);
1064 create_options
= IVAL(params
,32);
1065 sd_len
= IVAL(params
,36);
1066 ea_len
= IVAL(params
,40);
1067 root_dir_fid
= (uint16_t)IVAL(params
,4);
1068 allocation_size
= BVAL(params
,12);
1071 * we need to remove ignored bits when they come directly from the client
1072 * because we reuse some of them for internal stuff
1074 create_options
&= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK
;
1076 if (req
->posix_pathnames
) {
1077 srvstr_get_path_posix(ctx
,
1086 srvstr_get_path(ctx
,
1095 if (!NT_STATUS_IS_OK(status
)) {
1096 reply_nterror(req
, status
);
1100 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1101 case_state
= set_posix_case_semantics(ctx
, conn
);
1103 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1108 ucf_flags
= filename_create_ucf_flags(req
, create_disposition
);
1109 status
= filename_convert(ctx
,
1111 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1117 TALLOC_FREE(case_state
);
1119 if (!NT_STATUS_IS_OK(status
)) {
1120 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
1121 reply_botherror(req
,
1122 NT_STATUS_PATH_NOT_COVERED
,
1123 ERRSRV
, ERRbadpath
);
1126 reply_nterror(req
, status
);
1130 /* Ensure the data_len is correct for the sd and ea values given. */
1131 if ((ea_len
+ sd_len
> data_count
)
1132 || (ea_len
> data_count
) || (sd_len
> data_count
)
1133 || (ea_len
+ sd_len
< ea_len
) || (ea_len
+ sd_len
< sd_len
)) {
1134 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1135 "%u, data_count = %u\n", (unsigned int)ea_len
,
1136 (unsigned int)sd_len
, (unsigned int)data_count
));
1137 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1142 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1145 status
= unmarshall_sec_desc(ctx
, (uint8_t *)data
, sd_len
,
1147 if (!NT_STATUS_IS_OK(status
)) {
1148 DEBUG(10, ("call_nt_transact_create: "
1149 "unmarshall_sec_desc failed: %s\n",
1150 nt_errstr(status
)));
1151 reply_nterror(req
, status
);
1157 if (!lp_ea_support(SNUM(conn
))) {
1158 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1159 "EA's not supported.\n",
1160 (unsigned int)ea_len
));
1161 reply_nterror(req
, NT_STATUS_EAS_NOT_SUPPORTED
);
1166 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1167 "too small (should be more than 10)\n",
1168 (unsigned int)ea_len
));
1169 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1173 /* We have already checked that ea_len <= data_count here. */
1174 ea_list
= read_nttrans_ea_list(talloc_tos(), data
+ sd_len
,
1176 if (ea_list
== NULL
) {
1177 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1181 if (!req
->posix_pathnames
&&
1182 ea_list_has_invalid_name(ea_list
)) {
1183 /* Realloc the size of parameters and data we will return */
1184 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1185 /* Extended response is 32 more byyes. */
1190 params
= nttrans_realloc(ppparams
, param_len
);
1191 if(params
== NULL
) {
1192 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1196 memset(params
, '\0', param_len
);
1197 send_nt_replies(conn
, req
, STATUS_INVALID_EA_NAME
,
1198 params
, param_len
, NULL
, 0);
1203 oplock_request
= (flags
& REQUEST_OPLOCK
) ? EXCLUSIVE_OPLOCK
: 0;
1204 if (oplock_request
) {
1205 oplock_request
|= (flags
& REQUEST_BATCH_OPLOCK
)
1210 * Bug #6898 - clients using Windows opens should
1211 * never be able to set this attribute into the
1214 file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
1216 status
= SMB_VFS_CREATE_FILE(
1219 root_dir_fid
, /* root_dir_fid */
1220 smb_fname
, /* fname */
1221 access_mask
, /* access_mask */
1222 share_access
, /* share_access */
1223 create_disposition
, /* create_disposition*/
1224 create_options
, /* create_options */
1225 file_attributes
, /* file_attributes */
1226 oplock_request
, /* oplock_request */
1228 allocation_size
, /* allocation_size */
1229 0, /* private_flags */
1231 ea_list
, /* ea_list */
1234 NULL
, NULL
); /* create context */
1236 if(!NT_STATUS_IS_OK(status
)) {
1237 if (open_was_deferred(req
->xconn
, req
->mid
)) {
1238 /* We have re-scheduled this call, no error. */
1241 reply_openerror(req
, status
);
1245 /* Ensure we're pointing at the correct stat struct. */
1246 TALLOC_FREE(smb_fname
);
1247 smb_fname
= fsp
->fsp_name
;
1250 * If the caller set the extended oplock request bit
1251 * and we granted one (by whatever means) - set the
1252 * correct bit for extended oplock reply.
1255 if (oplock_request
&&
1256 (lp_fake_oplocks(SNUM(conn
))
1257 || EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
))) {
1260 * Exclusive oplock granted
1263 if (flags
& REQUEST_BATCH_OPLOCK
) {
1264 oplock_granted
= BATCH_OPLOCK_RETURN
;
1266 oplock_granted
= EXCLUSIVE_OPLOCK_RETURN
;
1268 } else if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
1269 oplock_granted
= LEVEL_II_OPLOCK_RETURN
;
1271 oplock_granted
= NO_OPLOCK_RETURN
;
1274 file_len
= smb_fname
->st
.st_ex_size
;
1276 /* Realloc the size of parameters and data we will return */
1277 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1278 /* Extended response is 32 more byyes. */
1283 params
= nttrans_realloc(ppparams
, param_len
);
1284 if(params
== NULL
) {
1285 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1290 SCVAL(p
, 0, oplock_granted
);
1293 SSVAL(p
,0,fsp
->fnum
);
1295 if ((create_disposition
== FILE_SUPERSEDE
)
1296 && (info
== FILE_WAS_OVERWRITTEN
)) {
1297 SIVAL(p
,0,FILE_WAS_SUPERSEDED
);
1303 fattr
= dos_mode(conn
, smb_fname
);
1305 fattr
= FILE_ATTRIBUTE_NORMAL
;
1309 create_timespec
= get_create_timespec(conn
, fsp
, smb_fname
);
1310 a_timespec
= smb_fname
->st
.st_ex_atime
;
1311 m_timespec
= smb_fname
->st
.st_ex_mtime
;
1312 c_timespec
= get_change_timespec(conn
, fsp
, smb_fname
);
1314 if (lp_dos_filetime_resolution(SNUM(conn
))) {
1315 dos_filetime_timespec(&create_timespec
);
1316 dos_filetime_timespec(&a_timespec
);
1317 dos_filetime_timespec(&m_timespec
);
1318 dos_filetime_timespec(&c_timespec
);
1321 put_long_date_timespec(conn
->ts_res
, p
, create_timespec
); /* create time. */
1323 put_long_date_timespec(conn
->ts_res
, p
, a_timespec
); /* access time */
1325 put_long_date_timespec(conn
->ts_res
, p
, m_timespec
); /* write time */
1327 put_long_date_timespec(conn
->ts_res
, p
, c_timespec
); /* change time */
1329 SIVAL(p
,0,fattr
); /* File Attributes. */
1331 SOFF_T(p
, 0, SMB_VFS_GET_ALLOC_SIZE(conn
, fsp
, &smb_fname
->st
));
1333 SOFF_T(p
,0,file_len
);
1335 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1336 uint16_t file_status
= (NO_EAS
|NO_SUBSTREAMS
|NO_REPARSETAG
);
1337 size_t num_names
= 0;
1338 unsigned int num_streams
= 0;
1339 struct stream_struct
*streams
= NULL
;
1341 /* Do we have any EA's ? */
1342 status
= get_ea_names_from_file(ctx
, conn
, fsp
,
1343 smb_fname
, NULL
, &num_names
);
1344 if (NT_STATUS_IS_OK(status
) && num_names
) {
1345 file_status
&= ~NO_EAS
;
1347 status
= vfs_streaminfo(conn
, NULL
, smb_fname
, ctx
,
1348 &num_streams
, &streams
);
1349 /* There is always one stream, ::$DATA. */
1350 if (NT_STATUS_IS_OK(status
) && num_streams
> 1) {
1351 file_status
&= ~NO_SUBSTREAMS
;
1353 TALLOC_FREE(streams
);
1354 SSVAL(p
,2,file_status
);
1357 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
1359 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1362 if (fsp
->is_directory
||
1364 can_write_to_file(conn
, smb_fname
)) {
1365 perms
= FILE_GENERIC_ALL
;
1367 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
1372 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1373 smb_fname_str_dbg(smb_fname
)));
1375 /* Send the required number of replies */
1376 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
1381 /****************************************************************************
1382 Reply to a NT CANCEL request.
1383 conn POINTER CAN BE NULL HERE !
1384 ****************************************************************************/
1386 void reply_ntcancel(struct smb_request
*req
)
1388 struct smbXsrv_connection
*xconn
= req
->xconn
;
1389 struct smbd_server_connection
*sconn
= req
->sconn
;
1392 * Go through and cancel any pending change notifies.
1395 START_PROFILE(SMBntcancel
);
1396 srv_cancel_sign_response(xconn
);
1397 remove_pending_change_notify_requests_by_mid(sconn
, req
->mid
);
1398 remove_pending_lock_requests_by_mid_smb1(sconn
, req
->mid
);
1400 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1401 (unsigned long long)req
->mid
));
1403 END_PROFILE(SMBntcancel
);
1407 /****************************************************************************
1409 ****************************************************************************/
1411 static NTSTATUS
copy_internals(TALLOC_CTX
*ctx
,
1412 connection_struct
*conn
,
1413 struct smb_request
*req
,
1414 struct smb_filename
*smb_fname_src
,
1415 struct smb_filename
*smb_fname_dst
,
1418 files_struct
*fsp1
,*fsp2
;
1422 NTSTATUS status
= NT_STATUS_OK
;
1425 if (!CAN_WRITE(conn
)) {
1426 status
= NT_STATUS_MEDIA_WRITE_PROTECTED
;
1430 /* Source must already exist. */
1431 if (!VALID_STAT(smb_fname_src
->st
)) {
1432 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1436 /* Ensure attributes match. */
1437 fattr
= dos_mode(conn
, smb_fname_src
);
1438 if ((fattr
& ~attrs
) & (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) {
1439 status
= NT_STATUS_NO_SUCH_FILE
;
1443 /* Disallow if dst file already exists. */
1444 if (VALID_STAT(smb_fname_dst
->st
)) {
1445 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
1449 /* No links from a directory. */
1450 if (S_ISDIR(smb_fname_src
->st
.st_ex_mode
)) {
1451 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
1455 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1456 smb_fname_str_dbg(smb_fname_src
),
1457 smb_fname_str_dbg(smb_fname_dst
)));
1459 status
= SMB_VFS_CREATE_FILE(
1462 0, /* root_dir_fid */
1463 smb_fname_src
, /* fname */
1464 FILE_READ_DATA
|FILE_READ_ATTRIBUTES
|
1465 FILE_READ_EA
, /* access_mask */
1466 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1468 FILE_OPEN
, /* create_disposition*/
1469 0, /* create_options */
1470 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
1471 NO_OPLOCK
, /* oplock_request */
1473 0, /* allocation_size */
1474 0, /* private_flags */
1479 NULL
, NULL
); /* create context */
1481 if (!NT_STATUS_IS_OK(status
)) {
1485 status
= SMB_VFS_CREATE_FILE(
1488 0, /* root_dir_fid */
1489 smb_fname_dst
, /* fname */
1490 FILE_WRITE_DATA
|FILE_WRITE_ATTRIBUTES
|
1491 FILE_WRITE_EA
, /* access_mask */
1492 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1494 FILE_CREATE
, /* create_disposition*/
1495 0, /* create_options */
1496 fattr
, /* file_attributes */
1497 NO_OPLOCK
, /* oplock_request */
1499 0, /* allocation_size */
1500 0, /* private_flags */
1505 NULL
, NULL
); /* create context */
1507 if (!NT_STATUS_IS_OK(status
)) {
1508 close_file(NULL
, fsp1
, ERROR_CLOSE
);
1512 if (smb_fname_src
->st
.st_ex_size
) {
1513 ret
= vfs_transfer_file(fsp1
, fsp2
, smb_fname_src
->st
.st_ex_size
);
1517 * As we are opening fsp1 read-only we only expect
1518 * an error on close on fsp2 if we are out of space.
1519 * Thus we don't look at the error return from the
1522 close_file(NULL
, fsp1
, NORMAL_CLOSE
);
1524 /* Ensure the modtime is set correctly on the destination file. */
1525 set_close_write_time(fsp2
, smb_fname_src
->st
.st_ex_mtime
);
1527 status
= close_file(NULL
, fsp2
, NORMAL_CLOSE
);
1529 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1530 creates the file. This isn't the correct thing to do in the copy
1532 if (!parent_dirname(talloc_tos(), smb_fname_dst
->base_name
, &parent
,
1534 status
= NT_STATUS_NO_MEMORY
;
1537 file_set_dosmode(conn
, smb_fname_dst
, fattr
, parent
, false);
1538 TALLOC_FREE(parent
);
1540 if (ret
< (off_t
)smb_fname_src
->st
.st_ex_size
) {
1541 status
= NT_STATUS_DISK_FULL
;
1545 if (!NT_STATUS_IS_OK(status
)) {
1546 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1547 nt_errstr(status
), smb_fname_str_dbg(smb_fname_src
),
1548 smb_fname_str_dbg(smb_fname_dst
)));
1554 /****************************************************************************
1555 Reply to a NT rename request.
1556 ****************************************************************************/
1558 void reply_ntrename(struct smb_request
*req
)
1560 connection_struct
*conn
= req
->conn
;
1561 struct smb_filename
*smb_fname_old
= NULL
;
1562 struct smb_filename
*smb_fname_new
= NULL
;
1563 char *oldname
= NULL
;
1564 char *newname
= NULL
;
1567 bool src_has_wcard
= False
;
1568 bool dest_has_wcard
= False
;
1570 uint32_t ucf_flags_src
= (req
->posix_pathnames
? UCF_POSIX_PATHNAMES
: 0);
1571 uint32_t ucf_flags_dst
= (req
->posix_pathnames
? UCF_POSIX_PATHNAMES
: 0);
1572 uint16_t rename_type
;
1573 TALLOC_CTX
*ctx
= talloc_tos();
1574 bool stream_rename
= false;
1576 START_PROFILE(SMBntrename
);
1579 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1583 attrs
= SVAL(req
->vwv
+0, 0);
1584 rename_type
= SVAL(req
->vwv
+1, 0);
1586 p
= (const char *)req
->buf
+ 1;
1587 p
+= srvstr_get_path_req_wcard(ctx
, req
, &oldname
, p
, STR_TERMINATE
,
1588 &status
, &src_has_wcard
);
1589 if (!NT_STATUS_IS_OK(status
)) {
1590 reply_nterror(req
, status
);
1594 if (!req
->posix_pathnames
&& ms_has_wild(oldname
)) {
1595 reply_nterror(req
, NT_STATUS_OBJECT_PATH_SYNTAX_BAD
);
1600 p
+= srvstr_get_path_req_wcard(ctx
, req
, &newname
, p
, STR_TERMINATE
,
1601 &status
, &dest_has_wcard
);
1602 if (!NT_STATUS_IS_OK(status
)) {
1603 reply_nterror(req
, status
);
1607 if (!req
->posix_pathnames
) {
1608 /* The newname must begin with a ':' if the
1609 oldname contains a ':'. */
1610 if (strchr_m(oldname
, ':')) {
1611 if (newname
[0] != ':') {
1612 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1615 stream_rename
= true;
1620 * If this is a rename operation, allow wildcards and save the
1621 * destination's last component.
1623 if (rename_type
== RENAME_FLAG_RENAME
) {
1624 ucf_flags_src
= UCF_COND_ALLOW_WCARD_LCOMP
;
1625 ucf_flags_dst
= UCF_COND_ALLOW_WCARD_LCOMP
| UCF_SAVE_LCOMP
;
1628 /* rename_internals() calls unix_convert(), so don't call it here. */
1629 status
= filename_convert(ctx
, conn
,
1630 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1635 if (!NT_STATUS_IS_OK(status
)) {
1636 if (NT_STATUS_EQUAL(status
,
1637 NT_STATUS_PATH_NOT_COVERED
)) {
1638 reply_botherror(req
,
1639 NT_STATUS_PATH_NOT_COVERED
,
1640 ERRSRV
, ERRbadpath
);
1643 reply_nterror(req
, status
);
1647 status
= filename_convert(ctx
, conn
,
1648 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1653 if (!NT_STATUS_IS_OK(status
)) {
1654 if (NT_STATUS_EQUAL(status
,
1655 NT_STATUS_PATH_NOT_COVERED
)) {
1656 reply_botherror(req
,
1657 NT_STATUS_PATH_NOT_COVERED
,
1658 ERRSRV
, ERRbadpath
);
1661 reply_nterror(req
, status
);
1665 if (stream_rename
) {
1666 /* smb_fname_new must be the same as smb_fname_old. */
1667 TALLOC_FREE(smb_fname_new
->base_name
);
1668 smb_fname_new
->base_name
= talloc_strdup(smb_fname_new
,
1669 smb_fname_old
->base_name
);
1670 if (!smb_fname_new
->base_name
) {
1671 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1676 DEBUG(3,("reply_ntrename: %s -> %s\n",
1677 smb_fname_str_dbg(smb_fname_old
),
1678 smb_fname_str_dbg(smb_fname_new
)));
1680 switch(rename_type
) {
1681 case RENAME_FLAG_RENAME
:
1682 status
= rename_internals(ctx
, conn
, req
,
1683 smb_fname_old
, smb_fname_new
,
1684 attrs
, False
, src_has_wcard
,
1688 case RENAME_FLAG_HARD_LINK
:
1689 if (src_has_wcard
|| dest_has_wcard
) {
1691 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1693 status
= hardlink_internals(ctx
, conn
,
1700 case RENAME_FLAG_COPY
:
1701 if (src_has_wcard
|| dest_has_wcard
) {
1703 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1705 status
= copy_internals(ctx
, conn
, req
,
1711 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION
:
1712 status
= NT_STATUS_INVALID_PARAMETER
;
1715 status
= NT_STATUS_ACCESS_DENIED
; /* Default error. */
1719 if (!NT_STATUS_IS_OK(status
)) {
1720 if (open_was_deferred(req
->xconn
, req
->mid
)) {
1721 /* We have re-scheduled this call. */
1725 reply_nterror(req
, status
);
1729 reply_outbuf(req
, 0, 0);
1731 END_PROFILE(SMBntrename
);
1735 /****************************************************************************
1736 Reply to a notify change - queue the request and
1737 don't allow a directory to be opened.
1738 ****************************************************************************/
1740 static void smbd_smb1_notify_reply(struct smb_request
*req
,
1741 NTSTATUS error_code
,
1742 uint8_t *buf
, size_t len
)
1744 send_nt_replies(req
->conn
, req
, error_code
, (char *)buf
, len
, NULL
, 0);
1747 static void call_nt_transact_notify_change(connection_struct
*conn
,
1748 struct smb_request
*req
,
1750 uint32_t setup_count
,
1752 uint32_t parameter_count
,
1753 char **ppdata
, uint32_t data_count
,
1754 uint32_t max_data_count
,
1755 uint32_t max_param_count
)
1757 uint16_t *setup
= *ppsetup
;
1763 if(setup_count
< 6) {
1764 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1768 fsp
= file_fsp(req
, SVAL(setup
,4));
1769 filter
= IVAL(setup
, 0);
1770 recursive
= (SVAL(setup
, 6) != 0) ? True
: False
;
1772 DEBUG(3,("call_nt_transact_notify_change\n"));
1775 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
1780 char *filter_string
;
1782 if (!(filter_string
= notify_filter_string(NULL
, filter
))) {
1783 reply_nterror(req
,NT_STATUS_NO_MEMORY
);
1787 DEBUG(3,("call_nt_transact_notify_change: notify change "
1788 "called on %s, filter = %s, recursive = %d\n",
1789 fsp_str_dbg(fsp
), filter_string
, recursive
));
1791 TALLOC_FREE(filter_string
);
1794 if((!fsp
->is_directory
) || (conn
!= fsp
->conn
)) {
1795 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1799 if (fsp
->notify
== NULL
) {
1801 status
= change_notify_create(fsp
, filter
, recursive
);
1803 if (!NT_STATUS_IS_OK(status
)) {
1804 DEBUG(10, ("change_notify_create returned %s\n",
1805 nt_errstr(status
)));
1806 reply_nterror(req
, status
);
1811 if (change_notify_fsp_has_changes(fsp
)) {
1814 * We've got changes pending, respond immediately
1818 * TODO: write a torture test to check the filtering behaviour
1822 change_notify_reply(req
,
1826 smbd_smb1_notify_reply
);
1829 * change_notify_reply() above has independently sent its
1836 * No changes pending, queue the request
1839 status
= change_notify_add_request(req
,
1843 smbd_smb1_notify_reply
);
1844 if (!NT_STATUS_IS_OK(status
)) {
1845 reply_nterror(req
, status
);
1850 /****************************************************************************
1851 Reply to an NT transact rename command.
1852 ****************************************************************************/
1854 static void call_nt_transact_rename(connection_struct
*conn
,
1855 struct smb_request
*req
,
1856 uint16_t **ppsetup
, uint32_t setup_count
,
1857 char **ppparams
, uint32_t parameter_count
,
1858 char **ppdata
, uint32_t data_count
,
1859 uint32_t max_data_count
)
1861 char *params
= *ppparams
;
1862 char *new_name
= NULL
;
1863 files_struct
*fsp
= NULL
;
1864 bool dest_has_wcard
= False
;
1866 TALLOC_CTX
*ctx
= talloc_tos();
1868 if(parameter_count
< 5) {
1869 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1873 fsp
= file_fsp(req
, SVAL(params
, 0));
1874 if (!check_fsp(conn
, req
, fsp
)) {
1877 if (req
->posix_pathnames
) {
1878 srvstr_get_path_wcard_posix(ctx
,
1883 parameter_count
- 4,
1888 srvstr_get_path_wcard(ctx
,
1893 parameter_count
- 4,
1899 if (!NT_STATUS_IS_OK(status
)) {
1900 reply_nterror(req
, status
);
1905 * W2K3 ignores this request as the RAW-RENAME test
1906 * demonstrates, so we do.
1908 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
1910 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1911 fsp_str_dbg(fsp
), new_name
));
1916 /******************************************************************************
1917 Fake up a completely empty SD.
1918 *******************************************************************************/
1920 static NTSTATUS
get_null_nt_acl(TALLOC_CTX
*mem_ctx
, struct security_descriptor
**ppsd
)
1924 *ppsd
= make_standard_sec_desc( mem_ctx
, &global_sid_World
, &global_sid_World
, NULL
, &sd_size
);
1926 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1927 return NT_STATUS_NO_MEMORY
;
1930 return NT_STATUS_OK
;
1933 /****************************************************************************
1934 Reply to query a security descriptor.
1935 Callable from SMB1 and SMB2.
1936 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1938 ****************************************************************************/
1940 NTSTATUS
smbd_do_query_security_desc(connection_struct
*conn
,
1941 TALLOC_CTX
*mem_ctx
,
1943 uint32_t security_info_wanted
,
1944 uint32_t max_data_count
,
1945 uint8_t **ppmarshalled_sd
,
1949 struct security_descriptor
*psd
= NULL
;
1950 TALLOC_CTX
*frame
= talloc_stackframe();
1953 * Get the permissions to return.
1956 if ((security_info_wanted
& SECINFO_SACL
) &&
1957 !(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
1958 DEBUG(10, ("Access to SACL denied.\n"));
1960 return NT_STATUS_ACCESS_DENIED
;
1963 if ((security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|SECINFO_GROUP
)) &&
1964 !(fsp
->access_mask
& SEC_STD_READ_CONTROL
)) {
1965 DEBUG(10, ("Access to DACL, OWNER, or GROUP denied.\n"));
1967 return NT_STATUS_ACCESS_DENIED
;
1970 if (S_ISLNK(fsp
->fsp_name
->st
.st_ex_mode
)) {
1971 DEBUG(10, ("ACL get on symlink %s denied.\n",
1974 return NT_STATUS_ACCESS_DENIED
;
1977 if (security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|
1978 SECINFO_GROUP
|SECINFO_SACL
)) {
1979 /* Don't return SECINFO_LABEL if anything else was
1980 requested. See bug #8458. */
1981 security_info_wanted
&= ~SECINFO_LABEL
;
1984 if (!lp_nt_acl_support(SNUM(conn
))) {
1985 status
= get_null_nt_acl(frame
, &psd
);
1986 } else if (security_info_wanted
& SECINFO_LABEL
) {
1987 /* Like W2K3 return a null object. */
1988 status
= get_null_nt_acl(frame
, &psd
);
1990 status
= SMB_VFS_FGET_NT_ACL(
1991 fsp
, security_info_wanted
, frame
, &psd
);
1993 if (!NT_STATUS_IS_OK(status
)) {
1998 if (!(security_info_wanted
& SECINFO_OWNER
)) {
1999 psd
->owner_sid
= NULL
;
2001 if (!(security_info_wanted
& SECINFO_GROUP
)) {
2002 psd
->group_sid
= NULL
;
2004 if (!(security_info_wanted
& SECINFO_DACL
)) {
2005 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
2008 if (!(security_info_wanted
& SECINFO_SACL
)) {
2009 psd
->type
&= ~SEC_DESC_SACL_PRESENT
;
2013 /* If the SACL/DACL is NULL, but was requested, we mark that it is
2014 * present in the reply to match Windows behavior */
2015 if (psd
->sacl
== NULL
&&
2016 security_info_wanted
& SECINFO_SACL
)
2017 psd
->type
|= SEC_DESC_SACL_PRESENT
;
2018 if (psd
->dacl
== NULL
&&
2019 security_info_wanted
& SECINFO_DACL
)
2020 psd
->type
|= SEC_DESC_DACL_PRESENT
;
2022 if (security_info_wanted
& SECINFO_LABEL
) {
2023 /* Like W2K3 return a null object. */
2024 psd
->owner_sid
= NULL
;
2025 psd
->group_sid
= NULL
;
2028 psd
->type
&= ~(SEC_DESC_DACL_PRESENT
|SEC_DESC_SACL_PRESENT
);
2031 *psd_size
= ndr_size_security_descriptor(psd
, 0);
2033 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
2034 (unsigned long)*psd_size
));
2036 if (DEBUGLEVEL
>= 10) {
2037 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
2039 NDR_PRINT_DEBUG(security_descriptor
, psd
);
2042 if (max_data_count
< *psd_size
) {
2044 return NT_STATUS_BUFFER_TOO_SMALL
;
2047 status
= marshall_sec_desc(mem_ctx
, psd
,
2048 ppmarshalled_sd
, psd_size
);
2050 if (!NT_STATUS_IS_OK(status
)) {
2056 return NT_STATUS_OK
;
2059 /****************************************************************************
2060 SMB1 reply to query a security descriptor.
2061 ****************************************************************************/
2063 static void call_nt_transact_query_security_desc(connection_struct
*conn
,
2064 struct smb_request
*req
,
2066 uint32_t setup_count
,
2068 uint32_t parameter_count
,
2070 uint32_t data_count
,
2071 uint32_t max_data_count
)
2073 char *params
= *ppparams
;
2074 char *data
= *ppdata
;
2076 uint32_t security_info_wanted
;
2077 files_struct
*fsp
= NULL
;
2079 uint8_t *marshalled_sd
= NULL
;
2081 if(parameter_count
< 8) {
2082 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2086 fsp
= file_fsp(req
, SVAL(params
,0));
2088 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2092 security_info_wanted
= IVAL(params
,4);
2094 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
2095 "info_wanted = 0x%x\n", fsp_str_dbg(fsp
),
2096 (unsigned int)security_info_wanted
));
2098 params
= nttrans_realloc(ppparams
, 4);
2099 if(params
== NULL
) {
2100 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2105 * Get the permissions to return.
2108 status
= smbd_do_query_security_desc(conn
,
2111 security_info_wanted
&
2112 SMB_SUPPORTED_SECINFO_FLAGS
,
2117 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
2118 SIVAL(params
,0,(uint32_t)sd_size
);
2119 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2120 params
, 4, NULL
, 0);
2124 if (!NT_STATUS_IS_OK(status
)) {
2125 reply_nterror(req
, status
);
2129 SMB_ASSERT(sd_size
> 0);
2131 SIVAL(params
,0,(uint32_t)sd_size
);
2133 if (max_data_count
< sd_size
) {
2134 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2135 params
, 4, NULL
, 0);
2140 * Allocate the data we will return.
2143 data
= nttrans_realloc(ppdata
, sd_size
);
2145 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2149 memcpy(data
, marshalled_sd
, sd_size
);
2151 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, 4, data
, (int)sd_size
);
2156 /****************************************************************************
2157 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2158 ****************************************************************************/
2160 static void call_nt_transact_set_security_desc(connection_struct
*conn
,
2161 struct smb_request
*req
,
2163 uint32_t setup_count
,
2165 uint32_t parameter_count
,
2167 uint32_t data_count
,
2168 uint32_t max_data_count
)
2170 char *params
= *ppparams
;
2171 char *data
= *ppdata
;
2172 files_struct
*fsp
= NULL
;
2173 uint32_t security_info_sent
= 0;
2176 if(parameter_count
< 8) {
2177 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2181 if((fsp
= file_fsp(req
, SVAL(params
,0))) == NULL
) {
2182 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2186 if (!CAN_WRITE(fsp
->conn
)) {
2187 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2191 if(!lp_nt_acl_support(SNUM(conn
))) {
2195 security_info_sent
= IVAL(params
,4);
2197 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2198 fsp_str_dbg(fsp
), (unsigned int)security_info_sent
));
2200 if (data_count
== 0) {
2201 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2205 status
= set_sd_blob(fsp
, (uint8_t *)data
, data_count
,
2206 security_info_sent
& SMB_SUPPORTED_SECINFO_FLAGS
);
2207 if (!NT_STATUS_IS_OK(status
)) {
2208 reply_nterror(req
, status
);
2213 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
2217 /****************************************************************************
2219 ****************************************************************************/
2221 static void call_nt_transact_ioctl(connection_struct
*conn
,
2222 struct smb_request
*req
,
2223 uint16_t **ppsetup
, uint32_t setup_count
,
2224 char **ppparams
, uint32_t parameter_count
,
2225 char **ppdata
, uint32_t data_count
,
2226 uint32_t max_data_count
)
2234 char *out_data
= NULL
;
2235 uint32_t out_data_len
= 0;
2236 char *pdata
= *ppdata
;
2237 TALLOC_CTX
*ctx
= talloc_tos();
2239 if (setup_count
!= 8) {
2240 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count
));
2241 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2245 function
= IVAL(*ppsetup
, 0);
2246 fidnum
= SVAL(*ppsetup
, 4);
2247 isFSctl
= CVAL(*ppsetup
, 6);
2248 compfilter
= CVAL(*ppsetup
, 7);
2250 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2251 function
, fidnum
, isFSctl
, compfilter
));
2253 fsp
=file_fsp(req
, fidnum
);
2256 * We don't really implement IOCTLs, especially on files.
2259 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2261 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2265 /* Has to be for an open file! */
2266 if (!check_fsp_open(conn
, req
, fsp
)) {
2270 SMB_PERFCOUNT_SET_IOCTL(&req
->pcd
, function
);
2273 * out_data might be allocated by the VFS module, but talloc should be
2274 * used, and should be cleaned up when the request ends.
2276 status
= SMB_VFS_FSCTL(fsp
,
2282 (uint8_t **)&out_data
,
2285 if (!NT_STATUS_IS_OK(status
)) {
2286 reply_nterror(req
, status
);
2288 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, out_data
, out_data_len
);
2293 #ifdef HAVE_SYS_QUOTAS
2294 /****************************************************************************
2295 Reply to get user quota
2296 ****************************************************************************/
2298 static void call_nt_transact_get_user_quota(connection_struct
*conn
,
2299 struct smb_request
*req
,
2301 uint32_t setup_count
,
2303 uint32_t parameter_count
,
2305 uint32_t data_count
,
2306 uint32_t max_data_count
)
2308 NTSTATUS nt_status
= NT_STATUS_OK
;
2309 char *params
= *ppparams
;
2310 char *pdata
= *ppdata
;
2312 int data_len
=0,param_len
=0;
2315 files_struct
*fsp
= NULL
;
2319 bool start_enum
= True
;
2320 SMB_NTQUOTA_STRUCT qt
;
2321 SMB_NTQUOTA_LIST
*tmp_list
;
2322 SMB_NTQUOTA_HANDLE
*qt_handle
= NULL
;
2327 if (get_current_uid(conn
) != sec_initial_uid()) {
2328 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2329 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn
)),
2330 conn
->session_info
->unix_info
->unix_name
));
2331 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2336 * Ensure minimum number of parameters sent.
2339 if (parameter_count
< 4) {
2340 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count
));
2341 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2345 /* maybe we can check the quota_fnum */
2346 fsp
= file_fsp(req
, SVAL(params
,0));
2347 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2348 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2349 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2353 /* the NULL pointer checking for fsp->fake_file_handle->pd
2354 * is done by CHECK_NTQUOTA_HANDLE_OK()
2356 qt_handle
= (SMB_NTQUOTA_HANDLE
*)fsp
->fake_file_handle
->private_data
;
2358 level
= SVAL(params
,2);
2360 /* unknown 12 bytes leading in params */
2363 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE
:
2364 /* seems that we should continue with the enum here --metze */
2366 if (qt_handle
->quota_list
!=NULL
&&
2367 qt_handle
->tmp_list
==NULL
) {
2370 free_ntquota_list(&(qt_handle
->quota_list
));
2372 /* Realloc the size of parameters and data we will return */
2374 params
= nttrans_realloc(ppparams
, param_len
);
2375 if(params
== NULL
) {
2376 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2381 SIVAL(params
,0,data_len
);
2388 case TRANSACT_GET_USER_QUOTA_LIST_START
:
2390 if (qt_handle
->quota_list
==NULL
&&
2391 qt_handle
->tmp_list
==NULL
) {
2395 if (start_enum
&& vfs_get_user_ntquota_list(fsp
,&(qt_handle
->quota_list
))!=0) {
2396 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2400 /* Realloc the size of parameters and data we will return */
2402 params
= nttrans_realloc(ppparams
, param_len
);
2403 if(params
== NULL
) {
2404 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2408 /* we should not trust the value in max_data_count*/
2409 max_data_count
= MIN(max_data_count
,2048);
2411 pdata
= nttrans_realloc(ppdata
, max_data_count
);/* should be max data count from client*/
2413 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2419 /* set params Size of returned Quota Data 4 bytes*/
2420 /* but set it later when we know it */
2422 /* for each entry push the data */
2425 qt_handle
->tmp_list
= qt_handle
->quota_list
;
2428 tmp_list
= qt_handle
->tmp_list
;
2430 for (;((tmp_list
!=NULL
)&&((qt_len
+40+SID_MAX_SIZE
)<max_data_count
));
2431 tmp_list
=tmp_list
->next
,entry
+=entry_len
,qt_len
+=entry_len
) {
2433 sid_len
= ndr_size_dom_sid(
2434 &tmp_list
->quotas
->sid
, 0);
2435 entry_len
= 40 + sid_len
;
2437 /* nextoffset entry 4 bytes */
2438 SIVAL(entry
,0,entry_len
);
2440 /* then the len of the SID 4 bytes */
2441 SIVAL(entry
,4,sid_len
);
2443 /* unknown data 8 bytes uint64_t */
2444 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2446 /* the used disk space 8 bytes uint64_t */
2447 SBIG_UINT(entry
,16,tmp_list
->quotas
->usedspace
);
2449 /* the soft quotas 8 bytes uint64_t */
2450 SBIG_UINT(entry
,24,tmp_list
->quotas
->softlim
);
2452 /* the hard quotas 8 bytes uint64_t */
2453 SBIG_UINT(entry
,32,tmp_list
->quotas
->hardlim
);
2455 /* and now the SID */
2456 sid_linearize((uint8_t *)(entry
+40), sid_len
,
2457 &tmp_list
->quotas
->sid
);
2460 qt_handle
->tmp_list
= tmp_list
;
2462 /* overwrite the offset of the last entry */
2463 SIVAL(entry
-entry_len
,0,0);
2465 data_len
= 4+qt_len
;
2466 /* overwrite the params quota_data_len */
2467 SIVAL(params
,0,data_len
);
2471 case TRANSACT_GET_USER_QUOTA_FOR_SID
:
2473 /* unknown 4 bytes IVAL(pdata,0) */
2475 if (data_count
< 8) {
2476 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count
,8));
2477 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2481 sid_len
= IVAL(pdata
,4);
2482 /* Ensure this is less than 1mb. */
2483 if (sid_len
> (1024*1024)) {
2484 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2488 if (data_count
< 8+sid_len
) {
2489 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count
,(unsigned long)(8+sid_len
)));
2490 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2494 data_len
= 4+40+sid_len
;
2496 if (max_data_count
< data_len
) {
2497 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2498 max_data_count
, data_len
));
2500 SIVAL(params
,0,data_len
);
2502 nt_status
= NT_STATUS_BUFFER_TOO_SMALL
;
2506 if (!sid_parse((const uint8_t *)(pdata
+8), sid_len
,
2508 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2512 nt_status
= vfs_get_ntquota(fsp
, SMB_USER_QUOTA_TYPE
,
2514 if (!NT_STATUS_IS_OK(nt_status
)) {
2515 reply_nterror(req
, nt_status
);
2519 /* Realloc the size of parameters and data we will return */
2521 params
= nttrans_realloc(ppparams
, param_len
);
2522 if(params
== NULL
) {
2523 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2527 pdata
= nttrans_realloc(ppdata
, data_len
);
2529 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2535 /* set params Size of returned Quota Data 4 bytes*/
2536 SIVAL(params
,0,data_len
);
2538 /* nextoffset entry 4 bytes */
2541 /* then the len of the SID 4 bytes */
2542 SIVAL(entry
,4,sid_len
);
2544 /* unknown data 8 bytes uint64_t */
2545 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2547 /* the used disk space 8 bytes uint64_t */
2548 SBIG_UINT(entry
,16,qt
.usedspace
);
2550 /* the soft quotas 8 bytes uint64_t */
2551 SBIG_UINT(entry
,24,qt
.softlim
);
2553 /* the hard quotas 8 bytes uint64_t */
2554 SBIG_UINT(entry
,32,qt
.hardlim
);
2556 /* and now the SID */
2557 sid_linearize((uint8_t *)(entry
+40), sid_len
, &sid
);
2562 DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
2564 fsp_fnum_dbg(fsp
), level
));
2565 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2570 send_nt_replies(conn
, req
, nt_status
, params
, param_len
,
2574 /****************************************************************************
2575 Reply to set user quota
2576 ****************************************************************************/
2578 static void call_nt_transact_set_user_quota(connection_struct
*conn
,
2579 struct smb_request
*req
,
2581 uint32_t setup_count
,
2583 uint32_t parameter_count
,
2585 uint32_t data_count
,
2586 uint32_t max_data_count
)
2588 char *params
= *ppparams
;
2589 char *pdata
= *ppdata
;
2590 int data_len
=0,param_len
=0;
2591 SMB_NTQUOTA_STRUCT qt
;
2594 files_struct
*fsp
= NULL
;
2599 if (get_current_uid(conn
) != 0) {
2600 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2601 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn
)),
2602 conn
->session_info
->unix_info
->unix_name
));
2603 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2608 * Ensure minimum number of parameters sent.
2611 if (parameter_count
< 2) {
2612 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count
));
2613 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2617 /* maybe we can check the quota_fnum */
2618 fsp
= file_fsp(req
, SVAL(params
,0));
2619 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2620 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2621 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2625 if (data_count
< 40) {
2626 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count
,40));
2627 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2631 /* offset to next quota record.
2632 * 4 bytes IVAL(pdata,0)
2637 sid_len
= IVAL(pdata
,4);
2639 if (data_count
< 40+sid_len
|| (40+sid_len
< sid_len
)) {
2640 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count
,(unsigned long)40+sid_len
));
2641 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2645 /* unknown 8 bytes in pdata
2646 * maybe its the change time in NTTIME
2649 /* the used space 8 bytes (uint64_t)*/
2650 qt
.usedspace
= BVAL(pdata
,16);
2652 /* the soft quotas 8 bytes (uint64_t)*/
2653 qt
.softlim
= BVAL(pdata
,24);
2655 /* the hard quotas 8 bytes (uint64_t)*/
2656 qt
.hardlim
= BVAL(pdata
,32);
2658 if (!sid_parse((const uint8_t *)(pdata
+40), sid_len
, &sid
)) {
2659 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2663 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid
)));
2665 /* 44 unknown bytes left... */
2667 if (vfs_set_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2668 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2672 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
,
2675 #endif /* HAVE_SYS_QUOTAS */
2677 static void handle_nttrans(connection_struct
*conn
,
2678 struct trans_state
*state
,
2679 struct smb_request
*req
)
2681 if (get_Protocol() >= PROTOCOL_NT1
) {
2682 req
->flags2
|= 0x40; /* IS_LONG_NAME */
2683 SSVAL(discard_const_p(uint8_t, req
->inbuf
),smb_flg2
,req
->flags2
);
2687 SMB_PERFCOUNT_SET_SUBOP(&req
->pcd
, state
->call
);
2689 /* Now we must call the relevant NT_TRANS function */
2690 switch(state
->call
) {
2691 case NT_TRANSACT_CREATE
:
2693 START_PROFILE(NT_transact_create
);
2694 call_nt_transact_create(
2696 &state
->setup
, state
->setup_count
,
2697 &state
->param
, state
->total_param
,
2698 &state
->data
, state
->total_data
,
2699 state
->max_data_return
);
2700 END_PROFILE(NT_transact_create
);
2704 case NT_TRANSACT_IOCTL
:
2706 START_PROFILE(NT_transact_ioctl
);
2707 call_nt_transact_ioctl(
2709 &state
->setup
, state
->setup_count
,
2710 &state
->param
, state
->total_param
,
2711 &state
->data
, state
->total_data
,
2712 state
->max_data_return
);
2713 END_PROFILE(NT_transact_ioctl
);
2717 case NT_TRANSACT_SET_SECURITY_DESC
:
2719 START_PROFILE(NT_transact_set_security_desc
);
2720 call_nt_transact_set_security_desc(
2722 &state
->setup
, state
->setup_count
,
2723 &state
->param
, state
->total_param
,
2724 &state
->data
, state
->total_data
,
2725 state
->max_data_return
);
2726 END_PROFILE(NT_transact_set_security_desc
);
2730 case NT_TRANSACT_NOTIFY_CHANGE
:
2732 START_PROFILE(NT_transact_notify_change
);
2733 call_nt_transact_notify_change(
2735 &state
->setup
, state
->setup_count
,
2736 &state
->param
, state
->total_param
,
2737 &state
->data
, state
->total_data
,
2738 state
->max_data_return
,
2739 state
->max_param_return
);
2740 END_PROFILE(NT_transact_notify_change
);
2744 case NT_TRANSACT_RENAME
:
2746 START_PROFILE(NT_transact_rename
);
2747 call_nt_transact_rename(
2749 &state
->setup
, state
->setup_count
,
2750 &state
->param
, state
->total_param
,
2751 &state
->data
, state
->total_data
,
2752 state
->max_data_return
);
2753 END_PROFILE(NT_transact_rename
);
2757 case NT_TRANSACT_QUERY_SECURITY_DESC
:
2759 START_PROFILE(NT_transact_query_security_desc
);
2760 call_nt_transact_query_security_desc(
2762 &state
->setup
, state
->setup_count
,
2763 &state
->param
, state
->total_param
,
2764 &state
->data
, state
->total_data
,
2765 state
->max_data_return
);
2766 END_PROFILE(NT_transact_query_security_desc
);
2770 #ifdef HAVE_SYS_QUOTAS
2771 case NT_TRANSACT_GET_USER_QUOTA
:
2773 START_PROFILE(NT_transact_get_user_quota
);
2774 call_nt_transact_get_user_quota(
2776 &state
->setup
, state
->setup_count
,
2777 &state
->param
, state
->total_param
,
2778 &state
->data
, state
->total_data
,
2779 state
->max_data_return
);
2780 END_PROFILE(NT_transact_get_user_quota
);
2784 case NT_TRANSACT_SET_USER_QUOTA
:
2786 START_PROFILE(NT_transact_set_user_quota
);
2787 call_nt_transact_set_user_quota(
2789 &state
->setup
, state
->setup_count
,
2790 &state
->param
, state
->total_param
,
2791 &state
->data
, state
->total_data
,
2792 state
->max_data_return
);
2793 END_PROFILE(NT_transact_set_user_quota
);
2796 #endif /* HAVE_SYS_QUOTAS */
2799 /* Error in request */
2800 DEBUG(0,("handle_nttrans: Unknown request %d in "
2801 "nttrans call\n", state
->call
));
2802 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2808 /****************************************************************************
2809 Reply to a SMBNTtrans.
2810 ****************************************************************************/
2812 void reply_nttrans(struct smb_request
*req
)
2814 connection_struct
*conn
= req
->conn
;
2819 uint16_t function_code
;
2821 struct trans_state
*state
;
2823 START_PROFILE(SMBnttrans
);
2825 if (req
->wct
< 19) {
2826 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2827 END_PROFILE(SMBnttrans
);
2831 pscnt
= IVAL(req
->vwv
+9, 1);
2832 psoff
= IVAL(req
->vwv
+11, 1);
2833 dscnt
= IVAL(req
->vwv
+13, 1);
2834 dsoff
= IVAL(req
->vwv
+15, 1);
2835 function_code
= SVAL(req
->vwv
+18, 0);
2837 if (IS_IPC(conn
) && (function_code
!= NT_TRANSACT_CREATE
)) {
2838 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2839 END_PROFILE(SMBnttrans
);
2843 result
= allow_new_trans(conn
->pending_trans
, req
->mid
);
2844 if (!NT_STATUS_IS_OK(result
)) {
2845 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result
)));
2846 reply_nterror(req
, result
);
2847 END_PROFILE(SMBnttrans
);
2851 if ((state
= talloc(conn
, struct trans_state
)) == NULL
) {
2852 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2853 END_PROFILE(SMBnttrans
);
2857 state
->cmd
= SMBnttrans
;
2859 state
->mid
= req
->mid
;
2860 state
->vuid
= req
->vuid
;
2861 state
->total_data
= IVAL(req
->vwv
+3, 1);
2863 state
->total_param
= IVAL(req
->vwv
+1, 1);
2864 state
->param
= NULL
;
2865 state
->max_data_return
= IVAL(req
->vwv
+7, 1);
2866 state
->max_param_return
= IVAL(req
->vwv
+5, 1);
2868 /* setup count is in *words* */
2869 state
->setup_count
= 2*CVAL(req
->vwv
+17, 1);
2870 state
->setup
= NULL
;
2871 state
->call
= function_code
;
2873 DEBUG(10, ("num_setup=%u, "
2874 "param_total=%u, this_param=%u, max_param=%u, "
2875 "data_total=%u, this_data=%u, max_data=%u, "
2876 "param_offset=%u, data_offset=%u\n",
2877 (unsigned)state
->setup_count
,
2878 (unsigned)state
->total_param
, (unsigned)pscnt
,
2879 (unsigned)state
->max_param_return
,
2880 (unsigned)state
->total_data
, (unsigned)dscnt
,
2881 (unsigned)state
->max_data_return
,
2882 (unsigned)psoff
, (unsigned)dsoff
));
2885 * All nttrans messages we handle have smb_wct == 19 +
2886 * state->setup_count. Ensure this is so as a sanity check.
2889 if(req
->wct
!= 19 + (state
->setup_count
/2)) {
2890 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2891 req
->wct
, 19 + (state
->setup_count
/2)));
2895 /* Don't allow more than 128mb for each value. */
2896 if ((state
->total_data
> (1024*1024*128)) ||
2897 (state
->total_param
> (1024*1024*128))) {
2898 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2899 END_PROFILE(SMBnttrans
);
2903 if ((dscnt
> state
->total_data
) || (pscnt
> state
->total_param
))
2906 if (state
->total_data
) {
2908 if (trans_oob(state
->total_data
, 0, dscnt
)
2909 || trans_oob(smb_len(req
->inbuf
), dsoff
, dscnt
)) {
2913 /* Can't use talloc here, the core routines do realloc on the
2914 * params and data. */
2915 if ((state
->data
= (char *)SMB_MALLOC(state
->total_data
)) == NULL
) {
2916 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2917 "bytes !\n", (unsigned int)state
->total_data
));
2919 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2920 END_PROFILE(SMBnttrans
);
2924 memcpy(state
->data
,smb_base(req
->inbuf
)+dsoff
,dscnt
);
2927 if (state
->total_param
) {
2929 if (trans_oob(state
->total_param
, 0, pscnt
)
2930 || trans_oob(smb_len(req
->inbuf
), psoff
, pscnt
)) {
2934 /* Can't use talloc here, the core routines do realloc on the
2935 * params and data. */
2936 if ((state
->param
= (char *)SMB_MALLOC(state
->total_param
)) == NULL
) {
2937 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2938 "bytes !\n", (unsigned int)state
->total_param
));
2939 SAFE_FREE(state
->data
);
2941 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2942 END_PROFILE(SMBnttrans
);
2946 memcpy(state
->param
,smb_base(req
->inbuf
)+psoff
,pscnt
);
2949 state
->received_data
= dscnt
;
2950 state
->received_param
= pscnt
;
2952 if(state
->setup_count
> 0) {
2953 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2954 state
->setup_count
));
2957 * No overflow possible here, state->setup_count is an
2958 * unsigned int, being filled by a single byte from
2959 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2960 * below is not necessary, it's here to clarify things. The
2961 * validity of req->vwv and req->wct has been checked in
2962 * init_smb_request already.
2964 if ((state
->setup_count
/2) + 19 > (unsigned int)req
->wct
) {
2968 state
->setup
= (uint16_t *)TALLOC(state
, state
->setup_count
);
2969 if (state
->setup
== NULL
) {
2970 DEBUG(0,("reply_nttrans : Out of memory\n"));
2971 SAFE_FREE(state
->data
);
2972 SAFE_FREE(state
->param
);
2974 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2975 END_PROFILE(SMBnttrans
);
2979 memcpy(state
->setup
, req
->vwv
+19, state
->setup_count
);
2980 dump_data(10, (uint8_t *)state
->setup
, state
->setup_count
);
2983 if ((state
->received_data
== state
->total_data
) &&
2984 (state
->received_param
== state
->total_param
)) {
2985 handle_nttrans(conn
, state
, req
);
2986 SAFE_FREE(state
->param
);
2987 SAFE_FREE(state
->data
);
2989 END_PROFILE(SMBnttrans
);
2993 DLIST_ADD(conn
->pending_trans
, state
);
2995 /* We need to send an interim response then receive the rest
2996 of the parameter/data bytes */
2997 reply_outbuf(req
, 0, 0);
2998 show_msg((char *)req
->outbuf
);
2999 END_PROFILE(SMBnttrans
);
3004 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3005 SAFE_FREE(state
->data
);
3006 SAFE_FREE(state
->param
);
3008 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3009 END_PROFILE(SMBnttrans
);
3013 /****************************************************************************
3014 Reply to a SMBnttranss
3015 ****************************************************************************/
3017 void reply_nttranss(struct smb_request
*req
)
3019 connection_struct
*conn
= req
->conn
;
3020 uint32_t pcnt
,poff
,dcnt
,doff
,pdisp
,ddisp
;
3021 struct trans_state
*state
;
3023 START_PROFILE(SMBnttranss
);
3025 show_msg((const char *)req
->inbuf
);
3027 /* Windows clients expect all replies to
3028 an NT transact secondary (SMBnttranss 0xA1)
3029 to have a command code of NT transact
3030 (SMBnttrans 0xA0). See bug #8989 for details. */
3031 req
->cmd
= SMBnttrans
;
3033 if (req
->wct
< 18) {
3034 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3035 END_PROFILE(SMBnttranss
);
3039 for (state
= conn
->pending_trans
; state
!= NULL
;
3040 state
= state
->next
) {
3041 if (state
->mid
== req
->mid
) {
3046 if ((state
== NULL
) || (state
->cmd
!= SMBnttrans
)) {
3047 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3048 END_PROFILE(SMBnttranss
);
3052 /* Revise state->total_param and state->total_data in case they have
3053 changed downwards */
3054 if (IVAL(req
->vwv
+1, 1) < state
->total_param
) {
3055 state
->total_param
= IVAL(req
->vwv
+1, 1);
3057 if (IVAL(req
->vwv
+3, 1) < state
->total_data
) {
3058 state
->total_data
= IVAL(req
->vwv
+3, 1);
3061 pcnt
= IVAL(req
->vwv
+5, 1);
3062 poff
= IVAL(req
->vwv
+7, 1);
3063 pdisp
= IVAL(req
->vwv
+9, 1);
3065 dcnt
= IVAL(req
->vwv
+11, 1);
3066 doff
= IVAL(req
->vwv
+13, 1);
3067 ddisp
= IVAL(req
->vwv
+15, 1);
3069 state
->received_param
+= pcnt
;
3070 state
->received_data
+= dcnt
;
3072 if ((state
->received_data
> state
->total_data
) ||
3073 (state
->received_param
> state
->total_param
))
3077 if (trans_oob(state
->total_param
, pdisp
, pcnt
)
3078 || trans_oob(smb_len(req
->inbuf
), poff
, pcnt
)) {
3081 memcpy(state
->param
+pdisp
, smb_base(req
->inbuf
)+poff
,pcnt
);
3085 if (trans_oob(state
->total_data
, ddisp
, dcnt
)
3086 || trans_oob(smb_len(req
->inbuf
), doff
, dcnt
)) {
3089 memcpy(state
->data
+ddisp
, smb_base(req
->inbuf
)+doff
,dcnt
);
3092 if ((state
->received_param
< state
->total_param
) ||
3093 (state
->received_data
< state
->total_data
)) {
3094 END_PROFILE(SMBnttranss
);
3098 handle_nttrans(conn
, state
, req
);
3100 DLIST_REMOVE(conn
->pending_trans
, state
);
3101 SAFE_FREE(state
->data
);
3102 SAFE_FREE(state
->param
);
3104 END_PROFILE(SMBnttranss
);
3109 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3110 DLIST_REMOVE(conn
->pending_trans
, state
);
3111 SAFE_FREE(state
->data
);
3112 SAFE_FREE(state
->param
);
3114 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3115 END_PROFILE(SMBnttranss
);