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
,
546 TALLOC_FREE(case_state
);
548 if (!NT_STATUS_IS_OK(status
)) {
549 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
551 NT_STATUS_PATH_NOT_COVERED
,
555 reply_nterror(req
, status
);
560 * Bug #6898 - clients using Windows opens should
561 * never be able to set this attribute into the
564 file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
566 status
= SMB_VFS_CREATE_FILE(
569 root_dir_fid
, /* root_dir_fid */
570 smb_fname
, /* fname */
571 access_mask
, /* access_mask */
572 share_access
, /* share_access */
573 create_disposition
, /* create_disposition*/
574 create_options
, /* create_options */
575 file_attributes
, /* file_attributes */
576 oplock_request
, /* oplock_request */
578 allocation_size
, /* allocation_size */
579 0, /* private_flags */
584 NULL
, NULL
); /* create context */
586 if (!NT_STATUS_IS_OK(status
)) {
587 if (open_was_deferred(req
->xconn
, req
->mid
)) {
588 /* We have re-scheduled this call, no error. */
591 reply_openerror(req
, status
);
595 /* Ensure we're pointing at the correct stat struct. */
596 TALLOC_FREE(smb_fname
);
597 smb_fname
= fsp
->fsp_name
;
600 * If the caller set the extended oplock request bit
601 * and we granted one (by whatever means) - set the
602 * correct bit for extended oplock reply.
605 if (oplock_request
&&
606 (lp_fake_oplocks(SNUM(conn
))
607 || EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
))) {
610 * Exclusive oplock granted
613 if (flags
& REQUEST_BATCH_OPLOCK
) {
614 oplock_granted
= BATCH_OPLOCK_RETURN
;
616 oplock_granted
= EXCLUSIVE_OPLOCK_RETURN
;
618 } else if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
619 oplock_granted
= LEVEL_II_OPLOCK_RETURN
;
621 oplock_granted
= NO_OPLOCK_RETURN
;
624 file_len
= smb_fname
->st
.st_ex_size
;
626 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
627 /* This is very strange. We
628 * return 50 words, but only set
629 * the wcnt to 42 ? It's definitely
630 * what happens on the wire....
632 reply_outbuf(req
, 50, 0);
633 SCVAL(req
->outbuf
,smb_wct
,42);
635 reply_outbuf(req
, 34, 0);
638 SSVAL(req
->outbuf
, smb_vwv0
, 0xff); /* andx chain ends */
639 SSVAL(req
->outbuf
, smb_vwv1
, 0); /* no andx offset */
641 p
= (char *)req
->outbuf
+ smb_vwv2
;
643 SCVAL(p
, 0, oplock_granted
);
646 SSVAL(p
,0,fsp
->fnum
);
648 if ((create_disposition
== FILE_SUPERSEDE
)
649 && (info
== FILE_WAS_OVERWRITTEN
)) {
650 SIVAL(p
,0,FILE_WAS_SUPERSEDED
);
656 fattr
= dos_mode(conn
, smb_fname
);
658 fattr
= FILE_ATTRIBUTE_NORMAL
;
662 create_timespec
= get_create_timespec(conn
, fsp
, smb_fname
);
663 a_timespec
= smb_fname
->st
.st_ex_atime
;
664 m_timespec
= smb_fname
->st
.st_ex_mtime
;
665 c_timespec
= get_change_timespec(conn
, fsp
, smb_fname
);
667 if (lp_dos_filetime_resolution(SNUM(conn
))) {
668 dos_filetime_timespec(&create_timespec
);
669 dos_filetime_timespec(&a_timespec
);
670 dos_filetime_timespec(&m_timespec
);
671 dos_filetime_timespec(&c_timespec
);
674 put_long_date_timespec(conn
->ts_res
, p
, create_timespec
); /* create time. */
676 put_long_date_timespec(conn
->ts_res
, p
, a_timespec
); /* access time */
678 put_long_date_timespec(conn
->ts_res
, p
, m_timespec
); /* write time */
680 put_long_date_timespec(conn
->ts_res
, p
, c_timespec
); /* change time */
682 SIVAL(p
,0,fattr
); /* File Attributes. */
684 SOFF_T(p
, 0, SMB_VFS_GET_ALLOC_SIZE(conn
,fsp
,&smb_fname
->st
));
686 SOFF_T(p
,0,file_len
);
688 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
689 uint16_t file_status
= (NO_EAS
|NO_SUBSTREAMS
|NO_REPARSETAG
);
690 unsigned int num_streams
= 0;
691 struct stream_struct
*streams
= NULL
;
693 if (lp_ea_support(SNUM(conn
))) {
694 size_t num_names
= 0;
695 /* Do we have any EA's ? */
696 status
= get_ea_names_from_file(
697 ctx
, conn
, fsp
, smb_fname
, NULL
, &num_names
);
698 if (NT_STATUS_IS_OK(status
) && num_names
) {
699 file_status
&= ~NO_EAS
;
703 status
= vfs_streaminfo(conn
, NULL
, smb_fname
, ctx
,
704 &num_streams
, &streams
);
705 /* There is always one stream, ::$DATA. */
706 if (NT_STATUS_IS_OK(status
) && num_streams
> 1) {
707 file_status
&= ~NO_SUBSTREAMS
;
709 TALLOC_FREE(streams
);
710 SSVAL(p
,2,file_status
);
713 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
715 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
718 if (fsp
->is_directory
||
720 can_write_to_file(conn
, smb_fname
)) {
721 perms
= FILE_GENERIC_ALL
;
723 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
728 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
729 fsp_fnum_dbg(fsp
), smb_fname_str_dbg(smb_fname
)));
732 END_PROFILE(SMBntcreateX
);
736 /****************************************************************************
737 Reply to a NT_TRANSACT_CREATE call to open a pipe.
738 ****************************************************************************/
740 static void do_nt_transact_create_pipe(connection_struct
*conn
,
741 struct smb_request
*req
,
742 uint16_t **ppsetup
, uint32_t setup_count
,
743 char **ppparams
, uint32_t parameter_count
,
744 char **ppdata
, uint32_t data_count
)
747 char *params
= *ppparams
;
748 uint16_t pnum
= FNUM_FIELD_INVALID
;
753 TALLOC_CTX
*ctx
= talloc_tos();
756 * Ensure minimum number of parameters sent.
759 if(parameter_count
< 54) {
760 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
761 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
765 flags
= IVAL(params
,0);
767 if (req
->posix_pathnames
) {
768 srvstr_get_path_posix(ctx
,
786 if (!NT_STATUS_IS_OK(status
)) {
787 reply_nterror(req
, status
);
791 nt_open_pipe(fname
, conn
, req
, &pnum
);
798 /* Realloc the size of parameters and data we will return */
799 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
800 /* Extended response is 32 more byyes. */
805 params
= nttrans_realloc(ppparams
, param_len
);
807 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
812 SCVAL(p
,0,NO_OPLOCK_RETURN
);
817 SIVAL(p
,0,FILE_WAS_OPENED
);
821 SIVAL(p
,0,FILE_ATTRIBUTE_NORMAL
); /* File Attributes. */
824 SSVAL(p
,0,FILE_TYPE_MESSAGE_MODE_PIPE
);
826 SSVAL(p
,2, 0x5FF); /* ? */
829 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
831 SIVAL(p
,0,FILE_GENERIC_ALL
);
833 * For pipes W2K3 seems to return
835 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
837 SIVAL(p
,4,(FILE_GENERIC_READ
|FILE_GENERIC_WRITE
)&~FILE_APPEND_DATA
);
840 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname
));
842 /* Send the required number of replies */
843 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
848 /*********************************************************************
849 Windows seems to do canonicalization of inheritance bits. Do the
851 *********************************************************************/
853 static void canonicalize_inheritance_bits(struct security_descriptor
*psd
)
855 bool set_auto_inherited
= false;
858 * We need to filter out the
859 * SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ
860 * bits. If both are set we store SEC_DESC_DACL_AUTO_INHERITED
861 * as this alters whether SEC_ACE_FLAG_INHERITED_ACE is set
862 * when an ACE is inherited. Otherwise we zero these bits out.
865 * http://social.msdn.microsoft.com/Forums/eu/os_fileservices/thread/11f77b68-731e-407d-b1b3-064750716531
870 if ((psd
->type
& (SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
))
871 == (SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
)) {
872 set_auto_inherited
= true;
875 psd
->type
&= ~(SEC_DESC_DACL_AUTO_INHERITED
|SEC_DESC_DACL_AUTO_INHERIT_REQ
);
876 if (set_auto_inherited
) {
877 psd
->type
|= SEC_DESC_DACL_AUTO_INHERITED
;
881 /****************************************************************************
882 Internal fn to set security descriptors.
883 ****************************************************************************/
885 NTSTATUS
set_sd(files_struct
*fsp
, struct security_descriptor
*psd
,
886 uint32_t security_info_sent
)
890 if (!CAN_WRITE(fsp
->conn
)) {
891 return NT_STATUS_ACCESS_DENIED
;
894 if (!lp_nt_acl_support(SNUM(fsp
->conn
))) {
898 if (S_ISLNK(fsp
->fsp_name
->st
.st_ex_mode
)) {
899 DEBUG(10, ("ACL set on symlink %s denied.\n",
901 return NT_STATUS_ACCESS_DENIED
;
904 if (psd
->owner_sid
== NULL
) {
905 security_info_sent
&= ~SECINFO_OWNER
;
907 if (psd
->group_sid
== NULL
) {
908 security_info_sent
&= ~SECINFO_GROUP
;
911 /* Ensure we have at least one thing set. */
912 if ((security_info_sent
& (SECINFO_OWNER
|SECINFO_GROUP
|SECINFO_DACL
|SECINFO_SACL
)) == 0) {
917 /* Ensure we have the rights to do this. */
918 if (security_info_sent
& SECINFO_OWNER
) {
919 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
920 return NT_STATUS_ACCESS_DENIED
;
924 if (security_info_sent
& SECINFO_GROUP
) {
925 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
926 return NT_STATUS_ACCESS_DENIED
;
930 if (security_info_sent
& SECINFO_DACL
) {
931 if (!(fsp
->access_mask
& SEC_STD_WRITE_DAC
)) {
932 return NT_STATUS_ACCESS_DENIED
;
934 /* Convert all the generic bits. */
936 security_acl_map_generic(psd
->dacl
, &file_generic_mapping
);
940 if (security_info_sent
& SECINFO_SACL
) {
941 if (!(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
942 return NT_STATUS_ACCESS_DENIED
;
944 /* Convert all the generic bits. */
946 security_acl_map_generic(psd
->sacl
, &file_generic_mapping
);
950 canonicalize_inheritance_bits(psd
);
952 if (DEBUGLEVEL
>= 10) {
953 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp
)));
954 NDR_PRINT_DEBUG(security_descriptor
, psd
);
957 status
= SMB_VFS_FSET_NT_ACL(fsp
, security_info_sent
, psd
);
964 /****************************************************************************
965 Internal fn to set security descriptors from a data blob.
966 ****************************************************************************/
968 NTSTATUS
set_sd_blob(files_struct
*fsp
, uint8_t *data
, uint32_t sd_len
,
969 uint32_t security_info_sent
)
971 struct security_descriptor
*psd
= NULL
;
975 return NT_STATUS_INVALID_PARAMETER
;
978 status
= unmarshall_sec_desc(talloc_tos(), data
, sd_len
, &psd
);
980 if (!NT_STATUS_IS_OK(status
)) {
984 return set_sd(fsp
, psd
, security_info_sent
);
987 /****************************************************************************
988 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
989 ****************************************************************************/
991 static void call_nt_transact_create(connection_struct
*conn
,
992 struct smb_request
*req
,
993 uint16_t **ppsetup
, uint32_t setup_count
,
994 char **ppparams
, uint32_t parameter_count
,
995 char **ppdata
, uint32_t data_count
,
996 uint32_t max_data_count
)
998 struct smb_filename
*smb_fname
= NULL
;
1000 char *params
= *ppparams
;
1001 char *data
= *ppdata
;
1002 /* Breakout the oplock request bits so we can set the reply bits separately. */
1006 files_struct
*fsp
= NULL
;
1009 uint32_t access_mask
;
1010 uint32_t file_attributes
;
1011 uint32_t share_access
;
1012 uint32_t create_disposition
;
1013 uint32_t create_options
;
1015 struct security_descriptor
*sd
= NULL
;
1017 uint16_t root_dir_fid
;
1018 struct timespec create_timespec
;
1019 struct timespec c_timespec
;
1020 struct timespec a_timespec
;
1021 struct timespec m_timespec
;
1022 struct ea_list
*ea_list
= NULL
;
1025 uint64_t allocation_size
;
1027 uint8_t oplock_granted
;
1028 struct case_semantics_state
*case_state
= NULL
;
1030 TALLOC_CTX
*ctx
= talloc_tos();
1032 DEBUG(5,("call_nt_transact_create\n"));
1035 * If it's an IPC, use the pipe handler.
1039 if (lp_nt_pipe_support()) {
1040 do_nt_transact_create_pipe(
1042 ppsetup
, setup_count
,
1043 ppparams
, parameter_count
,
1044 ppdata
, data_count
);
1047 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
1052 * Ensure minimum number of parameters sent.
1055 if(parameter_count
< 54) {
1056 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
1057 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1061 flags
= IVAL(params
,0);
1062 access_mask
= IVAL(params
,8);
1063 file_attributes
= IVAL(params
,20);
1064 share_access
= IVAL(params
,24);
1065 create_disposition
= IVAL(params
,28);
1066 create_options
= IVAL(params
,32);
1067 sd_len
= IVAL(params
,36);
1068 ea_len
= IVAL(params
,40);
1069 root_dir_fid
= (uint16_t)IVAL(params
,4);
1070 allocation_size
= BVAL(params
,12);
1073 * we need to remove ignored bits when they come directly from the client
1074 * because we reuse some of them for internal stuff
1076 create_options
&= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK
;
1078 if (req
->posix_pathnames
) {
1079 srvstr_get_path_posix(ctx
,
1088 srvstr_get_path(ctx
,
1097 if (!NT_STATUS_IS_OK(status
)) {
1098 reply_nterror(req
, status
);
1102 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1103 case_state
= set_posix_case_semantics(ctx
, conn
);
1105 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1110 ucf_flags
= filename_create_ucf_flags(req
, create_disposition
);
1111 status
= filename_convert(ctx
,
1118 TALLOC_FREE(case_state
);
1120 if (!NT_STATUS_IS_OK(status
)) {
1121 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
1122 reply_botherror(req
,
1123 NT_STATUS_PATH_NOT_COVERED
,
1124 ERRSRV
, ERRbadpath
);
1127 reply_nterror(req
, status
);
1131 /* Ensure the data_len is correct for the sd and ea values given. */
1132 if ((ea_len
+ sd_len
> data_count
)
1133 || (ea_len
> data_count
) || (sd_len
> data_count
)
1134 || (ea_len
+ sd_len
< ea_len
) || (ea_len
+ sd_len
< sd_len
)) {
1135 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1136 "%u, data_count = %u\n", (unsigned int)ea_len
,
1137 (unsigned int)sd_len
, (unsigned int)data_count
));
1138 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1143 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1146 status
= unmarshall_sec_desc(ctx
, (uint8_t *)data
, sd_len
,
1148 if (!NT_STATUS_IS_OK(status
)) {
1149 DEBUG(10, ("call_nt_transact_create: "
1150 "unmarshall_sec_desc failed: %s\n",
1151 nt_errstr(status
)));
1152 reply_nterror(req
, status
);
1158 if (!lp_ea_support(SNUM(conn
))) {
1159 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1160 "EA's not supported.\n",
1161 (unsigned int)ea_len
));
1162 reply_nterror(req
, NT_STATUS_EAS_NOT_SUPPORTED
);
1167 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1168 "too small (should be more than 10)\n",
1169 (unsigned int)ea_len
));
1170 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1174 /* We have already checked that ea_len <= data_count here. */
1175 ea_list
= read_nttrans_ea_list(talloc_tos(), data
+ sd_len
,
1177 if (ea_list
== NULL
) {
1178 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1182 if (!req
->posix_pathnames
&&
1183 ea_list_has_invalid_name(ea_list
)) {
1184 /* Realloc the size of parameters and data we will return */
1185 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1186 /* Extended response is 32 more byyes. */
1191 params
= nttrans_realloc(ppparams
, param_len
);
1192 if(params
== NULL
) {
1193 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1197 memset(params
, '\0', param_len
);
1198 send_nt_replies(conn
, req
, STATUS_INVALID_EA_NAME
,
1199 params
, param_len
, NULL
, 0);
1204 oplock_request
= (flags
& REQUEST_OPLOCK
) ? EXCLUSIVE_OPLOCK
: 0;
1205 if (oplock_request
) {
1206 oplock_request
|= (flags
& REQUEST_BATCH_OPLOCK
)
1211 * Bug #6898 - clients using Windows opens should
1212 * never be able to set this attribute into the
1215 file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
1217 status
= SMB_VFS_CREATE_FILE(
1220 root_dir_fid
, /* root_dir_fid */
1221 smb_fname
, /* fname */
1222 access_mask
, /* access_mask */
1223 share_access
, /* share_access */
1224 create_disposition
, /* create_disposition*/
1225 create_options
, /* create_options */
1226 file_attributes
, /* file_attributes */
1227 oplock_request
, /* oplock_request */
1229 allocation_size
, /* allocation_size */
1230 0, /* private_flags */
1232 ea_list
, /* ea_list */
1235 NULL
, NULL
); /* create context */
1237 if(!NT_STATUS_IS_OK(status
)) {
1238 if (open_was_deferred(req
->xconn
, req
->mid
)) {
1239 /* We have re-scheduled this call, no error. */
1242 reply_openerror(req
, status
);
1246 /* Ensure we're pointing at the correct stat struct. */
1247 TALLOC_FREE(smb_fname
);
1248 smb_fname
= fsp
->fsp_name
;
1251 * If the caller set the extended oplock request bit
1252 * and we granted one (by whatever means) - set the
1253 * correct bit for extended oplock reply.
1256 if (oplock_request
&&
1257 (lp_fake_oplocks(SNUM(conn
))
1258 || EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
))) {
1261 * Exclusive oplock granted
1264 if (flags
& REQUEST_BATCH_OPLOCK
) {
1265 oplock_granted
= BATCH_OPLOCK_RETURN
;
1267 oplock_granted
= EXCLUSIVE_OPLOCK_RETURN
;
1269 } else if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
1270 oplock_granted
= LEVEL_II_OPLOCK_RETURN
;
1272 oplock_granted
= NO_OPLOCK_RETURN
;
1275 file_len
= smb_fname
->st
.st_ex_size
;
1277 /* Realloc the size of parameters and data we will return */
1278 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1279 /* Extended response is 32 more byyes. */
1284 params
= nttrans_realloc(ppparams
, param_len
);
1285 if(params
== NULL
) {
1286 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1291 SCVAL(p
, 0, oplock_granted
);
1294 SSVAL(p
,0,fsp
->fnum
);
1296 if ((create_disposition
== FILE_SUPERSEDE
)
1297 && (info
== FILE_WAS_OVERWRITTEN
)) {
1298 SIVAL(p
,0,FILE_WAS_SUPERSEDED
);
1304 fattr
= dos_mode(conn
, smb_fname
);
1306 fattr
= FILE_ATTRIBUTE_NORMAL
;
1310 create_timespec
= get_create_timespec(conn
, fsp
, smb_fname
);
1311 a_timespec
= smb_fname
->st
.st_ex_atime
;
1312 m_timespec
= smb_fname
->st
.st_ex_mtime
;
1313 c_timespec
= get_change_timespec(conn
, fsp
, smb_fname
);
1315 if (lp_dos_filetime_resolution(SNUM(conn
))) {
1316 dos_filetime_timespec(&create_timespec
);
1317 dos_filetime_timespec(&a_timespec
);
1318 dos_filetime_timespec(&m_timespec
);
1319 dos_filetime_timespec(&c_timespec
);
1322 put_long_date_timespec(conn
->ts_res
, p
, create_timespec
); /* create time. */
1324 put_long_date_timespec(conn
->ts_res
, p
, a_timespec
); /* access time */
1326 put_long_date_timespec(conn
->ts_res
, p
, m_timespec
); /* write time */
1328 put_long_date_timespec(conn
->ts_res
, p
, c_timespec
); /* change time */
1330 SIVAL(p
,0,fattr
); /* File Attributes. */
1332 SOFF_T(p
, 0, SMB_VFS_GET_ALLOC_SIZE(conn
, fsp
, &smb_fname
->st
));
1334 SOFF_T(p
,0,file_len
);
1336 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1337 uint16_t file_status
= (NO_EAS
|NO_SUBSTREAMS
|NO_REPARSETAG
);
1338 unsigned int num_streams
= 0;
1339 struct stream_struct
*streams
= NULL
;
1341 if (lp_ea_support(SNUM(conn
))) {
1342 size_t num_names
= 0;
1343 /* Do we have any EA's ? */
1344 status
= get_ea_names_from_file(
1345 ctx
, conn
, fsp
, smb_fname
, NULL
, &num_names
);
1346 if (NT_STATUS_IS_OK(status
) && num_names
) {
1347 file_status
&= ~NO_EAS
;
1351 status
= vfs_streaminfo(conn
, NULL
, smb_fname
, ctx
,
1352 &num_streams
, &streams
);
1353 /* There is always one stream, ::$DATA. */
1354 if (NT_STATUS_IS_OK(status
) && num_streams
> 1) {
1355 file_status
&= ~NO_SUBSTREAMS
;
1357 TALLOC_FREE(streams
);
1358 SSVAL(p
,2,file_status
);
1361 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
1363 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1366 if (fsp
->is_directory
||
1368 can_write_to_file(conn
, smb_fname
)) {
1369 perms
= FILE_GENERIC_ALL
;
1371 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
1376 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1377 smb_fname_str_dbg(smb_fname
)));
1379 /* Send the required number of replies */
1380 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
1385 /****************************************************************************
1386 Reply to a NT CANCEL request.
1387 conn POINTER CAN BE NULL HERE !
1388 ****************************************************************************/
1390 void reply_ntcancel(struct smb_request
*req
)
1392 struct smbXsrv_connection
*xconn
= req
->xconn
;
1393 struct smbd_server_connection
*sconn
= req
->sconn
;
1396 * Go through and cancel any pending change notifies.
1399 START_PROFILE(SMBntcancel
);
1400 srv_cancel_sign_response(xconn
);
1401 remove_pending_change_notify_requests_by_mid(sconn
, req
->mid
);
1402 remove_pending_lock_requests_by_mid_smb1(sconn
, req
->mid
);
1404 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1405 (unsigned long long)req
->mid
));
1407 END_PROFILE(SMBntcancel
);
1411 /****************************************************************************
1413 ****************************************************************************/
1415 static NTSTATUS
copy_internals(TALLOC_CTX
*ctx
,
1416 connection_struct
*conn
,
1417 struct smb_request
*req
,
1418 struct smb_filename
*smb_fname_src
,
1419 struct smb_filename
*smb_fname_dst
,
1422 files_struct
*fsp1
,*fsp2
;
1426 NTSTATUS status
= NT_STATUS_OK
;
1429 if (!CAN_WRITE(conn
)) {
1430 status
= NT_STATUS_MEDIA_WRITE_PROTECTED
;
1434 /* Source must already exist. */
1435 if (!VALID_STAT(smb_fname_src
->st
)) {
1436 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1440 /* Ensure attributes match. */
1441 fattr
= dos_mode(conn
, smb_fname_src
);
1442 if ((fattr
& ~attrs
) & (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) {
1443 status
= NT_STATUS_NO_SUCH_FILE
;
1447 /* Disallow if dst file already exists. */
1448 if (VALID_STAT(smb_fname_dst
->st
)) {
1449 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
1453 /* No links from a directory. */
1454 if (S_ISDIR(smb_fname_src
->st
.st_ex_mode
)) {
1455 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
1459 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1460 smb_fname_str_dbg(smb_fname_src
),
1461 smb_fname_str_dbg(smb_fname_dst
)));
1463 status
= SMB_VFS_CREATE_FILE(
1466 0, /* root_dir_fid */
1467 smb_fname_src
, /* fname */
1468 FILE_READ_DATA
|FILE_READ_ATTRIBUTES
|
1469 FILE_READ_EA
, /* access_mask */
1470 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1472 FILE_OPEN
, /* create_disposition*/
1473 0, /* create_options */
1474 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
1475 NO_OPLOCK
, /* oplock_request */
1477 0, /* allocation_size */
1478 0, /* private_flags */
1483 NULL
, NULL
); /* create context */
1485 if (!NT_STATUS_IS_OK(status
)) {
1489 status
= SMB_VFS_CREATE_FILE(
1492 0, /* root_dir_fid */
1493 smb_fname_dst
, /* fname */
1494 FILE_WRITE_DATA
|FILE_WRITE_ATTRIBUTES
|
1495 FILE_WRITE_EA
, /* access_mask */
1496 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1498 FILE_CREATE
, /* create_disposition*/
1499 0, /* create_options */
1500 fattr
, /* file_attributes */
1501 NO_OPLOCK
, /* oplock_request */
1503 0, /* allocation_size */
1504 0, /* private_flags */
1509 NULL
, NULL
); /* create context */
1511 if (!NT_STATUS_IS_OK(status
)) {
1512 close_file(NULL
, fsp1
, ERROR_CLOSE
);
1516 if (smb_fname_src
->st
.st_ex_size
) {
1517 ret
= vfs_transfer_file(fsp1
, fsp2
, smb_fname_src
->st
.st_ex_size
);
1521 * As we are opening fsp1 read-only we only expect
1522 * an error on close on fsp2 if we are out of space.
1523 * Thus we don't look at the error return from the
1526 close_file(NULL
, fsp1
, NORMAL_CLOSE
);
1528 /* Ensure the modtime is set correctly on the destination file. */
1529 set_close_write_time(fsp2
, smb_fname_src
->st
.st_ex_mtime
);
1531 status
= close_file(NULL
, fsp2
, NORMAL_CLOSE
);
1533 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1534 creates the file. This isn't the correct thing to do in the copy
1536 if (!parent_dirname(talloc_tos(), smb_fname_dst
->base_name
, &parent
,
1538 status
= NT_STATUS_NO_MEMORY
;
1541 file_set_dosmode(conn
, smb_fname_dst
, fattr
, parent
, false);
1542 TALLOC_FREE(parent
);
1544 if (ret
< (off_t
)smb_fname_src
->st
.st_ex_size
) {
1545 status
= NT_STATUS_DISK_FULL
;
1549 if (!NT_STATUS_IS_OK(status
)) {
1550 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1551 nt_errstr(status
), smb_fname_str_dbg(smb_fname_src
),
1552 smb_fname_str_dbg(smb_fname_dst
)));
1558 /****************************************************************************
1559 Reply to a NT rename request.
1560 ****************************************************************************/
1562 void reply_ntrename(struct smb_request
*req
)
1564 connection_struct
*conn
= req
->conn
;
1565 struct smb_filename
*smb_fname_old
= NULL
;
1566 struct smb_filename
*smb_fname_new
= NULL
;
1567 char *oldname
= NULL
;
1568 char *newname
= NULL
;
1571 bool src_has_wcard
= False
;
1572 bool dest_has_wcard
= False
;
1574 uint32_t ucf_flags_src
= ucf_flags_from_smb_request(req
);
1575 uint32_t ucf_flags_dst
= ucf_flags_from_smb_request(req
);
1576 uint16_t rename_type
;
1577 TALLOC_CTX
*ctx
= talloc_tos();
1578 bool stream_rename
= false;
1580 START_PROFILE(SMBntrename
);
1583 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1587 attrs
= SVAL(req
->vwv
+0, 0);
1588 rename_type
= SVAL(req
->vwv
+1, 0);
1590 p
= (const char *)req
->buf
+ 1;
1591 p
+= srvstr_get_path_req_wcard(ctx
, req
, &oldname
, p
, STR_TERMINATE
,
1592 &status
, &src_has_wcard
);
1593 if (!NT_STATUS_IS_OK(status
)) {
1594 reply_nterror(req
, status
);
1598 if (!req
->posix_pathnames
&& ms_has_wild(oldname
)) {
1599 reply_nterror(req
, NT_STATUS_OBJECT_PATH_SYNTAX_BAD
);
1604 p
+= srvstr_get_path_req_wcard(ctx
, req
, &newname
, p
, STR_TERMINATE
,
1605 &status
, &dest_has_wcard
);
1606 if (!NT_STATUS_IS_OK(status
)) {
1607 reply_nterror(req
, status
);
1611 if (!req
->posix_pathnames
) {
1612 /* The newname must begin with a ':' if the
1613 oldname contains a ':'. */
1614 if (strchr_m(oldname
, ':')) {
1615 if (newname
[0] != ':') {
1616 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1619 stream_rename
= true;
1624 * If this is a rename operation, allow wildcards and save the
1625 * destination's last component.
1627 if (rename_type
== RENAME_FLAG_RENAME
) {
1628 ucf_flags_src
|= UCF_COND_ALLOW_WCARD_LCOMP
;
1629 ucf_flags_dst
|= UCF_COND_ALLOW_WCARD_LCOMP
| UCF_SAVE_LCOMP
;
1632 /* rename_internals() calls unix_convert(), so don't call it here. */
1633 status
= filename_convert(ctx
, conn
,
1638 if (!NT_STATUS_IS_OK(status
)) {
1639 if (NT_STATUS_EQUAL(status
,
1640 NT_STATUS_PATH_NOT_COVERED
)) {
1641 reply_botherror(req
,
1642 NT_STATUS_PATH_NOT_COVERED
,
1643 ERRSRV
, ERRbadpath
);
1646 reply_nterror(req
, status
);
1650 status
= filename_convert(ctx
, conn
,
1655 if (!NT_STATUS_IS_OK(status
)) {
1656 if (NT_STATUS_EQUAL(status
,
1657 NT_STATUS_PATH_NOT_COVERED
)) {
1658 reply_botherror(req
,
1659 NT_STATUS_PATH_NOT_COVERED
,
1660 ERRSRV
, ERRbadpath
);
1663 reply_nterror(req
, status
);
1667 if (stream_rename
) {
1668 /* smb_fname_new must be the same as smb_fname_old. */
1669 TALLOC_FREE(smb_fname_new
->base_name
);
1670 smb_fname_new
->base_name
= talloc_strdup(smb_fname_new
,
1671 smb_fname_old
->base_name
);
1672 if (!smb_fname_new
->base_name
) {
1673 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1678 DEBUG(3,("reply_ntrename: %s -> %s\n",
1679 smb_fname_str_dbg(smb_fname_old
),
1680 smb_fname_str_dbg(smb_fname_new
)));
1682 switch(rename_type
) {
1683 case RENAME_FLAG_RENAME
:
1684 status
= rename_internals(ctx
, conn
, req
,
1685 smb_fname_old
, smb_fname_new
,
1686 attrs
, False
, src_has_wcard
,
1690 case RENAME_FLAG_HARD_LINK
:
1691 if (src_has_wcard
|| dest_has_wcard
) {
1693 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1695 status
= hardlink_internals(ctx
, conn
,
1702 case RENAME_FLAG_COPY
:
1703 if (src_has_wcard
|| dest_has_wcard
) {
1705 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1707 status
= copy_internals(ctx
, conn
, req
,
1713 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION
:
1714 status
= NT_STATUS_INVALID_PARAMETER
;
1717 status
= NT_STATUS_ACCESS_DENIED
; /* Default error. */
1721 if (!NT_STATUS_IS_OK(status
)) {
1722 if (open_was_deferred(req
->xconn
, req
->mid
)) {
1723 /* We have re-scheduled this call. */
1727 reply_nterror(req
, status
);
1731 reply_outbuf(req
, 0, 0);
1733 END_PROFILE(SMBntrename
);
1737 /****************************************************************************
1738 Reply to a notify change - queue the request and
1739 don't allow a directory to be opened.
1740 ****************************************************************************/
1742 static void smbd_smb1_notify_reply(struct smb_request
*req
,
1743 NTSTATUS error_code
,
1744 uint8_t *buf
, size_t len
)
1746 send_nt_replies(req
->conn
, req
, error_code
, (char *)buf
, len
, NULL
, 0);
1749 static void call_nt_transact_notify_change(connection_struct
*conn
,
1750 struct smb_request
*req
,
1752 uint32_t setup_count
,
1754 uint32_t parameter_count
,
1755 char **ppdata
, uint32_t data_count
,
1756 uint32_t max_data_count
,
1757 uint32_t max_param_count
)
1759 uint16_t *setup
= *ppsetup
;
1765 if(setup_count
< 6) {
1766 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1770 fsp
= file_fsp(req
, SVAL(setup
,4));
1771 filter
= IVAL(setup
, 0);
1772 recursive
= (SVAL(setup
, 6) != 0) ? True
: False
;
1774 DEBUG(3,("call_nt_transact_notify_change\n"));
1777 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
1782 char *filter_string
;
1784 if (!(filter_string
= notify_filter_string(NULL
, filter
))) {
1785 reply_nterror(req
,NT_STATUS_NO_MEMORY
);
1789 DEBUG(3,("call_nt_transact_notify_change: notify change "
1790 "called on %s, filter = %s, recursive = %d\n",
1791 fsp_str_dbg(fsp
), filter_string
, recursive
));
1793 TALLOC_FREE(filter_string
);
1796 if((!fsp
->is_directory
) || (conn
!= fsp
->conn
)) {
1797 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1801 if (fsp
->notify
== NULL
) {
1803 status
= change_notify_create(fsp
, filter
, recursive
);
1805 if (!NT_STATUS_IS_OK(status
)) {
1806 DEBUG(10, ("change_notify_create returned %s\n",
1807 nt_errstr(status
)));
1808 reply_nterror(req
, status
);
1813 if (change_notify_fsp_has_changes(fsp
)) {
1816 * We've got changes pending, respond immediately
1820 * TODO: write a torture test to check the filtering behaviour
1824 change_notify_reply(req
,
1828 smbd_smb1_notify_reply
);
1831 * change_notify_reply() above has independently sent its
1838 * No changes pending, queue the request
1841 status
= change_notify_add_request(req
,
1845 smbd_smb1_notify_reply
);
1846 if (!NT_STATUS_IS_OK(status
)) {
1847 reply_nterror(req
, status
);
1852 /****************************************************************************
1853 Reply to an NT transact rename command.
1854 ****************************************************************************/
1856 static void call_nt_transact_rename(connection_struct
*conn
,
1857 struct smb_request
*req
,
1858 uint16_t **ppsetup
, uint32_t setup_count
,
1859 char **ppparams
, uint32_t parameter_count
,
1860 char **ppdata
, uint32_t data_count
,
1861 uint32_t max_data_count
)
1863 char *params
= *ppparams
;
1864 char *new_name
= NULL
;
1865 files_struct
*fsp
= NULL
;
1866 bool dest_has_wcard
= False
;
1868 TALLOC_CTX
*ctx
= talloc_tos();
1870 if(parameter_count
< 5) {
1871 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1875 fsp
= file_fsp(req
, SVAL(params
, 0));
1876 if (!check_fsp(conn
, req
, fsp
)) {
1879 if (req
->posix_pathnames
) {
1880 srvstr_get_path_wcard_posix(ctx
,
1885 parameter_count
- 4,
1890 srvstr_get_path_wcard(ctx
,
1895 parameter_count
- 4,
1901 if (!NT_STATUS_IS_OK(status
)) {
1902 reply_nterror(req
, status
);
1907 * W2K3 ignores this request as the RAW-RENAME test
1908 * demonstrates, so we do.
1910 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
1912 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1913 fsp_str_dbg(fsp
), new_name
));
1918 /******************************************************************************
1919 Fake up a completely empty SD.
1920 *******************************************************************************/
1922 static NTSTATUS
get_null_nt_acl(TALLOC_CTX
*mem_ctx
, struct security_descriptor
**ppsd
)
1926 *ppsd
= make_standard_sec_desc( mem_ctx
, &global_sid_World
, &global_sid_World
, NULL
, &sd_size
);
1928 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1929 return NT_STATUS_NO_MEMORY
;
1932 return NT_STATUS_OK
;
1935 /****************************************************************************
1936 Reply to query a security descriptor.
1937 Callable from SMB1 and SMB2.
1938 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1940 ****************************************************************************/
1942 NTSTATUS
smbd_do_query_security_desc(connection_struct
*conn
,
1943 TALLOC_CTX
*mem_ctx
,
1945 uint32_t security_info_wanted
,
1946 uint32_t max_data_count
,
1947 uint8_t **ppmarshalled_sd
,
1951 struct security_descriptor
*psd
= NULL
;
1952 TALLOC_CTX
*frame
= talloc_stackframe();
1955 * Get the permissions to return.
1958 if ((security_info_wanted
& SECINFO_SACL
) &&
1959 !(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
1960 DEBUG(10, ("Access to SACL denied.\n"));
1962 return NT_STATUS_ACCESS_DENIED
;
1965 if ((security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|SECINFO_GROUP
)) &&
1966 !(fsp
->access_mask
& SEC_STD_READ_CONTROL
)) {
1967 DEBUG(10, ("Access to DACL, OWNER, or GROUP denied.\n"));
1969 return NT_STATUS_ACCESS_DENIED
;
1972 if (S_ISLNK(fsp
->fsp_name
->st
.st_ex_mode
)) {
1973 DEBUG(10, ("ACL get on symlink %s denied.\n",
1976 return NT_STATUS_ACCESS_DENIED
;
1979 if (security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|
1980 SECINFO_GROUP
|SECINFO_SACL
)) {
1981 /* Don't return SECINFO_LABEL if anything else was
1982 requested. See bug #8458. */
1983 security_info_wanted
&= ~SECINFO_LABEL
;
1986 if (!lp_nt_acl_support(SNUM(conn
))) {
1987 status
= get_null_nt_acl(frame
, &psd
);
1988 } else if (security_info_wanted
& SECINFO_LABEL
) {
1989 /* Like W2K3 return a null object. */
1990 status
= get_null_nt_acl(frame
, &psd
);
1992 status
= SMB_VFS_FGET_NT_ACL(
1993 fsp
, security_info_wanted
, frame
, &psd
);
1995 if (!NT_STATUS_IS_OK(status
)) {
2000 if (!(security_info_wanted
& SECINFO_OWNER
)) {
2001 psd
->owner_sid
= NULL
;
2003 if (!(security_info_wanted
& SECINFO_GROUP
)) {
2004 psd
->group_sid
= NULL
;
2006 if (!(security_info_wanted
& SECINFO_DACL
)) {
2007 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
2010 if (!(security_info_wanted
& SECINFO_SACL
)) {
2011 psd
->type
&= ~SEC_DESC_SACL_PRESENT
;
2015 /* If the SACL/DACL is NULL, but was requested, we mark that it is
2016 * present in the reply to match Windows behavior */
2017 if (psd
->sacl
== NULL
&&
2018 security_info_wanted
& SECINFO_SACL
)
2019 psd
->type
|= SEC_DESC_SACL_PRESENT
;
2020 if (psd
->dacl
== NULL
&&
2021 security_info_wanted
& SECINFO_DACL
)
2022 psd
->type
|= SEC_DESC_DACL_PRESENT
;
2024 if (security_info_wanted
& SECINFO_LABEL
) {
2025 /* Like W2K3 return a null object. */
2026 psd
->owner_sid
= NULL
;
2027 psd
->group_sid
= NULL
;
2030 psd
->type
&= ~(SEC_DESC_DACL_PRESENT
|SEC_DESC_SACL_PRESENT
);
2033 *psd_size
= ndr_size_security_descriptor(psd
, 0);
2035 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
2036 (unsigned long)*psd_size
));
2038 if (DEBUGLEVEL
>= 10) {
2039 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
2041 NDR_PRINT_DEBUG(security_descriptor
, psd
);
2044 if (max_data_count
< *psd_size
) {
2046 return NT_STATUS_BUFFER_TOO_SMALL
;
2049 status
= marshall_sec_desc(mem_ctx
, psd
,
2050 ppmarshalled_sd
, psd_size
);
2052 if (!NT_STATUS_IS_OK(status
)) {
2058 return NT_STATUS_OK
;
2061 /****************************************************************************
2062 SMB1 reply to query a security descriptor.
2063 ****************************************************************************/
2065 static void call_nt_transact_query_security_desc(connection_struct
*conn
,
2066 struct smb_request
*req
,
2068 uint32_t setup_count
,
2070 uint32_t parameter_count
,
2072 uint32_t data_count
,
2073 uint32_t max_data_count
)
2075 char *params
= *ppparams
;
2076 char *data
= *ppdata
;
2078 uint32_t security_info_wanted
;
2079 files_struct
*fsp
= NULL
;
2081 uint8_t *marshalled_sd
= NULL
;
2083 if(parameter_count
< 8) {
2084 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2088 fsp
= file_fsp(req
, SVAL(params
,0));
2090 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2094 security_info_wanted
= IVAL(params
,4);
2096 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
2097 "info_wanted = 0x%x\n", fsp_str_dbg(fsp
),
2098 (unsigned int)security_info_wanted
));
2100 params
= nttrans_realloc(ppparams
, 4);
2101 if(params
== NULL
) {
2102 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2107 * Get the permissions to return.
2110 status
= smbd_do_query_security_desc(conn
,
2113 security_info_wanted
&
2114 SMB_SUPPORTED_SECINFO_FLAGS
,
2119 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
2120 SIVAL(params
,0,(uint32_t)sd_size
);
2121 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2122 params
, 4, NULL
, 0);
2126 if (!NT_STATUS_IS_OK(status
)) {
2127 reply_nterror(req
, status
);
2131 SMB_ASSERT(sd_size
> 0);
2133 SIVAL(params
,0,(uint32_t)sd_size
);
2135 if (max_data_count
< sd_size
) {
2136 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2137 params
, 4, NULL
, 0);
2142 * Allocate the data we will return.
2145 data
= nttrans_realloc(ppdata
, sd_size
);
2147 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2151 memcpy(data
, marshalled_sd
, sd_size
);
2153 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, 4, data
, (int)sd_size
);
2158 /****************************************************************************
2159 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2160 ****************************************************************************/
2162 static void call_nt_transact_set_security_desc(connection_struct
*conn
,
2163 struct smb_request
*req
,
2165 uint32_t setup_count
,
2167 uint32_t parameter_count
,
2169 uint32_t data_count
,
2170 uint32_t max_data_count
)
2172 char *params
= *ppparams
;
2173 char *data
= *ppdata
;
2174 files_struct
*fsp
= NULL
;
2175 uint32_t security_info_sent
= 0;
2178 if(parameter_count
< 8) {
2179 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2183 if((fsp
= file_fsp(req
, SVAL(params
,0))) == NULL
) {
2184 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2188 if (!CAN_WRITE(fsp
->conn
)) {
2189 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2193 if(!lp_nt_acl_support(SNUM(conn
))) {
2197 security_info_sent
= IVAL(params
,4);
2199 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2200 fsp_str_dbg(fsp
), (unsigned int)security_info_sent
));
2202 if (data_count
== 0) {
2203 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2207 status
= set_sd_blob(fsp
, (uint8_t *)data
, data_count
,
2208 security_info_sent
& SMB_SUPPORTED_SECINFO_FLAGS
);
2209 if (!NT_STATUS_IS_OK(status
)) {
2210 reply_nterror(req
, status
);
2215 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
2219 /****************************************************************************
2221 ****************************************************************************/
2223 static void call_nt_transact_ioctl(connection_struct
*conn
,
2224 struct smb_request
*req
,
2225 uint16_t **ppsetup
, uint32_t setup_count
,
2226 char **ppparams
, uint32_t parameter_count
,
2227 char **ppdata
, uint32_t data_count
,
2228 uint32_t max_data_count
)
2236 char *out_data
= NULL
;
2237 uint32_t out_data_len
= 0;
2238 char *pdata
= *ppdata
;
2239 TALLOC_CTX
*ctx
= talloc_tos();
2241 if (setup_count
!= 8) {
2242 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count
));
2243 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2247 function
= IVAL(*ppsetup
, 0);
2248 fidnum
= SVAL(*ppsetup
, 4);
2249 isFSctl
= CVAL(*ppsetup
, 6);
2250 compfilter
= CVAL(*ppsetup
, 7);
2252 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2253 function
, fidnum
, isFSctl
, compfilter
));
2255 fsp
=file_fsp(req
, fidnum
);
2258 * We don't really implement IOCTLs, especially on files.
2261 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2263 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2267 /* Has to be for an open file! */
2268 if (!check_fsp_open(conn
, req
, fsp
)) {
2272 SMB_PERFCOUNT_SET_IOCTL(&req
->pcd
, function
);
2275 * out_data might be allocated by the VFS module, but talloc should be
2276 * used, and should be cleaned up when the request ends.
2278 status
= SMB_VFS_FSCTL(fsp
,
2284 (uint8_t **)&out_data
,
2287 if (!NT_STATUS_IS_OK(status
)) {
2288 reply_nterror(req
, status
);
2290 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, out_data
, out_data_len
);
2295 #ifdef HAVE_SYS_QUOTAS
2296 /****************************************************************************
2297 Reply to get user quota
2298 ****************************************************************************/
2300 static void call_nt_transact_get_user_quota(connection_struct
*conn
,
2301 struct smb_request
*req
,
2303 uint32_t setup_count
,
2305 uint32_t parameter_count
,
2307 uint32_t data_count
,
2308 uint32_t max_data_count
)
2310 NTSTATUS nt_status
= NT_STATUS_OK
;
2311 char *params
= *ppparams
;
2312 char *pdata
= *ppdata
;
2314 int data_len
=0,param_len
=0;
2317 files_struct
*fsp
= NULL
;
2321 bool start_enum
= True
;
2322 SMB_NTQUOTA_STRUCT qt
;
2323 SMB_NTQUOTA_LIST
*tmp_list
;
2324 SMB_NTQUOTA_HANDLE
*qt_handle
= NULL
;
2329 if (get_current_uid(conn
) != sec_initial_uid()) {
2330 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2331 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn
)),
2332 conn
->session_info
->unix_info
->unix_name
));
2333 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2338 * Ensure minimum number of parameters sent.
2341 if (parameter_count
< 4) {
2342 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count
));
2343 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2347 /* maybe we can check the quota_fnum */
2348 fsp
= file_fsp(req
, SVAL(params
,0));
2349 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2350 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2351 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2355 /* the NULL pointer checking for fsp->fake_file_handle->pd
2356 * is done by CHECK_NTQUOTA_HANDLE_OK()
2358 qt_handle
= (SMB_NTQUOTA_HANDLE
*)fsp
->fake_file_handle
->private_data
;
2360 level
= SVAL(params
,2);
2362 /* unknown 12 bytes leading in params */
2365 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE
:
2366 /* seems that we should continue with the enum here --metze */
2368 if (qt_handle
->quota_list
!=NULL
&&
2369 qt_handle
->tmp_list
==NULL
) {
2372 free_ntquota_list(&(qt_handle
->quota_list
));
2374 /* Realloc the size of parameters and data we will return */
2376 params
= nttrans_realloc(ppparams
, param_len
);
2377 if(params
== NULL
) {
2378 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2383 SIVAL(params
,0,data_len
);
2390 case TRANSACT_GET_USER_QUOTA_LIST_START
:
2392 if (qt_handle
->quota_list
==NULL
&&
2393 qt_handle
->tmp_list
==NULL
) {
2397 if (start_enum
&& vfs_get_user_ntquota_list(fsp
,&(qt_handle
->quota_list
))!=0) {
2398 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2402 /* Realloc the size of parameters and data we will return */
2404 params
= nttrans_realloc(ppparams
, param_len
);
2405 if(params
== NULL
) {
2406 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2410 /* we should not trust the value in max_data_count*/
2411 max_data_count
= MIN(max_data_count
,2048);
2413 pdata
= nttrans_realloc(ppdata
, max_data_count
);/* should be max data count from client*/
2415 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2421 /* set params Size of returned Quota Data 4 bytes*/
2422 /* but set it later when we know it */
2424 /* for each entry push the data */
2427 qt_handle
->tmp_list
= qt_handle
->quota_list
;
2430 tmp_list
= qt_handle
->tmp_list
;
2432 for (;((tmp_list
!=NULL
)&&((qt_len
+40+SID_MAX_SIZE
)<max_data_count
));
2433 tmp_list
=tmp_list
->next
,entry
+=entry_len
,qt_len
+=entry_len
) {
2435 sid_len
= ndr_size_dom_sid(
2436 &tmp_list
->quotas
->sid
, 0);
2437 entry_len
= 40 + sid_len
;
2439 /* nextoffset entry 4 bytes */
2440 SIVAL(entry
,0,entry_len
);
2442 /* then the len of the SID 4 bytes */
2443 SIVAL(entry
,4,sid_len
);
2445 /* unknown data 8 bytes uint64_t */
2446 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2448 /* the used disk space 8 bytes uint64_t */
2449 SBIG_UINT(entry
,16,tmp_list
->quotas
->usedspace
);
2451 /* the soft quotas 8 bytes uint64_t */
2452 SBIG_UINT(entry
,24,tmp_list
->quotas
->softlim
);
2454 /* the hard quotas 8 bytes uint64_t */
2455 SBIG_UINT(entry
,32,tmp_list
->quotas
->hardlim
);
2457 /* and now the SID */
2458 sid_linearize((uint8_t *)(entry
+40), sid_len
,
2459 &tmp_list
->quotas
->sid
);
2462 qt_handle
->tmp_list
= tmp_list
;
2464 /* overwrite the offset of the last entry */
2465 SIVAL(entry
-entry_len
,0,0);
2467 data_len
= 4+qt_len
;
2468 /* overwrite the params quota_data_len */
2469 SIVAL(params
,0,data_len
);
2473 case TRANSACT_GET_USER_QUOTA_FOR_SID
:
2475 /* unknown 4 bytes IVAL(pdata,0) */
2477 if (data_count
< 8) {
2478 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count
,8));
2479 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2483 sid_len
= IVAL(pdata
,4);
2484 /* Ensure this is less than 1mb. */
2485 if (sid_len
> (1024*1024)) {
2486 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2490 if (data_count
< 8+sid_len
) {
2491 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count
,(unsigned long)(8+sid_len
)));
2492 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2496 data_len
= 4+40+sid_len
;
2498 if (max_data_count
< data_len
) {
2499 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2500 max_data_count
, data_len
));
2502 SIVAL(params
,0,data_len
);
2504 nt_status
= NT_STATUS_BUFFER_TOO_SMALL
;
2508 if (!sid_parse((const uint8_t *)(pdata
+8), sid_len
,
2510 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2514 nt_status
= vfs_get_ntquota(fsp
, SMB_USER_QUOTA_TYPE
,
2516 if (!NT_STATUS_IS_OK(nt_status
)) {
2517 reply_nterror(req
, nt_status
);
2521 /* Realloc the size of parameters and data we will return */
2523 params
= nttrans_realloc(ppparams
, param_len
);
2524 if(params
== NULL
) {
2525 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2529 pdata
= nttrans_realloc(ppdata
, data_len
);
2531 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2537 /* set params Size of returned Quota Data 4 bytes*/
2538 SIVAL(params
,0,data_len
);
2540 /* nextoffset entry 4 bytes */
2543 /* then the len of the SID 4 bytes */
2544 SIVAL(entry
,4,sid_len
);
2546 /* unknown data 8 bytes uint64_t */
2547 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2549 /* the used disk space 8 bytes uint64_t */
2550 SBIG_UINT(entry
,16,qt
.usedspace
);
2552 /* the soft quotas 8 bytes uint64_t */
2553 SBIG_UINT(entry
,24,qt
.softlim
);
2555 /* the hard quotas 8 bytes uint64_t */
2556 SBIG_UINT(entry
,32,qt
.hardlim
);
2558 /* and now the SID */
2559 sid_linearize((uint8_t *)(entry
+40), sid_len
, &sid
);
2564 DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
2566 fsp_fnum_dbg(fsp
), level
));
2567 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2572 send_nt_replies(conn
, req
, nt_status
, params
, param_len
,
2576 /****************************************************************************
2577 Reply to set user quota
2578 ****************************************************************************/
2580 static void call_nt_transact_set_user_quota(connection_struct
*conn
,
2581 struct smb_request
*req
,
2583 uint32_t setup_count
,
2585 uint32_t parameter_count
,
2587 uint32_t data_count
,
2588 uint32_t max_data_count
)
2590 char *params
= *ppparams
;
2591 char *pdata
= *ppdata
;
2592 int data_len
=0,param_len
=0;
2593 SMB_NTQUOTA_STRUCT qt
;
2596 files_struct
*fsp
= NULL
;
2601 if (get_current_uid(conn
) != 0) {
2602 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2603 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn
)),
2604 conn
->session_info
->unix_info
->unix_name
));
2605 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2610 * Ensure minimum number of parameters sent.
2613 if (parameter_count
< 2) {
2614 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count
));
2615 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2619 /* maybe we can check the quota_fnum */
2620 fsp
= file_fsp(req
, SVAL(params
,0));
2621 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2622 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2623 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2627 if (data_count
< 40) {
2628 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count
,40));
2629 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2633 /* offset to next quota record.
2634 * 4 bytes IVAL(pdata,0)
2639 sid_len
= IVAL(pdata
,4);
2641 if (data_count
< 40+sid_len
|| (40+sid_len
< sid_len
)) {
2642 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count
,(unsigned long)40+sid_len
));
2643 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2647 /* unknown 8 bytes in pdata
2648 * maybe its the change time in NTTIME
2651 /* the used space 8 bytes (uint64_t)*/
2652 qt
.usedspace
= BVAL(pdata
,16);
2654 /* the soft quotas 8 bytes (uint64_t)*/
2655 qt
.softlim
= BVAL(pdata
,24);
2657 /* the hard quotas 8 bytes (uint64_t)*/
2658 qt
.hardlim
= BVAL(pdata
,32);
2660 if (!sid_parse((const uint8_t *)(pdata
+40), sid_len
, &sid
)) {
2661 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2665 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid
)));
2667 /* 44 unknown bytes left... */
2669 if (vfs_set_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2670 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2674 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
,
2677 #endif /* HAVE_SYS_QUOTAS */
2679 static void handle_nttrans(connection_struct
*conn
,
2680 struct trans_state
*state
,
2681 struct smb_request
*req
)
2683 if (get_Protocol() >= PROTOCOL_NT1
) {
2684 req
->flags2
|= 0x40; /* IS_LONG_NAME */
2685 SSVAL(discard_const_p(uint8_t, req
->inbuf
),smb_flg2
,req
->flags2
);
2689 SMB_PERFCOUNT_SET_SUBOP(&req
->pcd
, state
->call
);
2691 /* Now we must call the relevant NT_TRANS function */
2692 switch(state
->call
) {
2693 case NT_TRANSACT_CREATE
:
2695 START_PROFILE(NT_transact_create
);
2696 call_nt_transact_create(
2698 &state
->setup
, state
->setup_count
,
2699 &state
->param
, state
->total_param
,
2700 &state
->data
, state
->total_data
,
2701 state
->max_data_return
);
2702 END_PROFILE(NT_transact_create
);
2706 case NT_TRANSACT_IOCTL
:
2708 START_PROFILE(NT_transact_ioctl
);
2709 call_nt_transact_ioctl(
2711 &state
->setup
, state
->setup_count
,
2712 &state
->param
, state
->total_param
,
2713 &state
->data
, state
->total_data
,
2714 state
->max_data_return
);
2715 END_PROFILE(NT_transact_ioctl
);
2719 case NT_TRANSACT_SET_SECURITY_DESC
:
2721 START_PROFILE(NT_transact_set_security_desc
);
2722 call_nt_transact_set_security_desc(
2724 &state
->setup
, state
->setup_count
,
2725 &state
->param
, state
->total_param
,
2726 &state
->data
, state
->total_data
,
2727 state
->max_data_return
);
2728 END_PROFILE(NT_transact_set_security_desc
);
2732 case NT_TRANSACT_NOTIFY_CHANGE
:
2734 START_PROFILE(NT_transact_notify_change
);
2735 call_nt_transact_notify_change(
2737 &state
->setup
, state
->setup_count
,
2738 &state
->param
, state
->total_param
,
2739 &state
->data
, state
->total_data
,
2740 state
->max_data_return
,
2741 state
->max_param_return
);
2742 END_PROFILE(NT_transact_notify_change
);
2746 case NT_TRANSACT_RENAME
:
2748 START_PROFILE(NT_transact_rename
);
2749 call_nt_transact_rename(
2751 &state
->setup
, state
->setup_count
,
2752 &state
->param
, state
->total_param
,
2753 &state
->data
, state
->total_data
,
2754 state
->max_data_return
);
2755 END_PROFILE(NT_transact_rename
);
2759 case NT_TRANSACT_QUERY_SECURITY_DESC
:
2761 START_PROFILE(NT_transact_query_security_desc
);
2762 call_nt_transact_query_security_desc(
2764 &state
->setup
, state
->setup_count
,
2765 &state
->param
, state
->total_param
,
2766 &state
->data
, state
->total_data
,
2767 state
->max_data_return
);
2768 END_PROFILE(NT_transact_query_security_desc
);
2772 #ifdef HAVE_SYS_QUOTAS
2773 case NT_TRANSACT_GET_USER_QUOTA
:
2775 START_PROFILE(NT_transact_get_user_quota
);
2776 call_nt_transact_get_user_quota(
2778 &state
->setup
, state
->setup_count
,
2779 &state
->param
, state
->total_param
,
2780 &state
->data
, state
->total_data
,
2781 state
->max_data_return
);
2782 END_PROFILE(NT_transact_get_user_quota
);
2786 case NT_TRANSACT_SET_USER_QUOTA
:
2788 START_PROFILE(NT_transact_set_user_quota
);
2789 call_nt_transact_set_user_quota(
2791 &state
->setup
, state
->setup_count
,
2792 &state
->param
, state
->total_param
,
2793 &state
->data
, state
->total_data
,
2794 state
->max_data_return
);
2795 END_PROFILE(NT_transact_set_user_quota
);
2798 #endif /* HAVE_SYS_QUOTAS */
2801 /* Error in request */
2802 DEBUG(0,("handle_nttrans: Unknown request %d in "
2803 "nttrans call\n", state
->call
));
2804 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2810 /****************************************************************************
2811 Reply to a SMBNTtrans.
2812 ****************************************************************************/
2814 void reply_nttrans(struct smb_request
*req
)
2816 connection_struct
*conn
= req
->conn
;
2821 uint16_t function_code
;
2823 struct trans_state
*state
;
2825 START_PROFILE(SMBnttrans
);
2827 if (req
->wct
< 19) {
2828 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2829 END_PROFILE(SMBnttrans
);
2833 pscnt
= IVAL(req
->vwv
+9, 1);
2834 psoff
= IVAL(req
->vwv
+11, 1);
2835 dscnt
= IVAL(req
->vwv
+13, 1);
2836 dsoff
= IVAL(req
->vwv
+15, 1);
2837 function_code
= SVAL(req
->vwv
+18, 0);
2839 if (IS_IPC(conn
) && (function_code
!= NT_TRANSACT_CREATE
)) {
2840 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2841 END_PROFILE(SMBnttrans
);
2845 result
= allow_new_trans(conn
->pending_trans
, req
->mid
);
2846 if (!NT_STATUS_IS_OK(result
)) {
2847 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result
)));
2848 reply_nterror(req
, result
);
2849 END_PROFILE(SMBnttrans
);
2853 if ((state
= talloc(conn
, struct trans_state
)) == NULL
) {
2854 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2855 END_PROFILE(SMBnttrans
);
2859 state
->cmd
= SMBnttrans
;
2861 state
->mid
= req
->mid
;
2862 state
->vuid
= req
->vuid
;
2863 state
->total_data
= IVAL(req
->vwv
+3, 1);
2865 state
->total_param
= IVAL(req
->vwv
+1, 1);
2866 state
->param
= NULL
;
2867 state
->max_data_return
= IVAL(req
->vwv
+7, 1);
2868 state
->max_param_return
= IVAL(req
->vwv
+5, 1);
2870 /* setup count is in *words* */
2871 state
->setup_count
= 2*CVAL(req
->vwv
+17, 1);
2872 state
->setup
= NULL
;
2873 state
->call
= function_code
;
2875 DEBUG(10, ("num_setup=%u, "
2876 "param_total=%u, this_param=%u, max_param=%u, "
2877 "data_total=%u, this_data=%u, max_data=%u, "
2878 "param_offset=%u, data_offset=%u\n",
2879 (unsigned)state
->setup_count
,
2880 (unsigned)state
->total_param
, (unsigned)pscnt
,
2881 (unsigned)state
->max_param_return
,
2882 (unsigned)state
->total_data
, (unsigned)dscnt
,
2883 (unsigned)state
->max_data_return
,
2884 (unsigned)psoff
, (unsigned)dsoff
));
2887 * All nttrans messages we handle have smb_wct == 19 +
2888 * state->setup_count. Ensure this is so as a sanity check.
2891 if(req
->wct
!= 19 + (state
->setup_count
/2)) {
2892 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2893 req
->wct
, 19 + (state
->setup_count
/2)));
2897 /* Don't allow more than 128mb for each value. */
2898 if ((state
->total_data
> (1024*1024*128)) ||
2899 (state
->total_param
> (1024*1024*128))) {
2900 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2901 END_PROFILE(SMBnttrans
);
2905 if ((dscnt
> state
->total_data
) || (pscnt
> state
->total_param
))
2908 if (state
->total_data
) {
2910 if (trans_oob(state
->total_data
, 0, dscnt
)
2911 || trans_oob(smb_len(req
->inbuf
), dsoff
, dscnt
)) {
2915 /* Can't use talloc here, the core routines do realloc on the
2916 * params and data. */
2917 if ((state
->data
= (char *)SMB_MALLOC(state
->total_data
)) == NULL
) {
2918 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2919 "bytes !\n", (unsigned int)state
->total_data
));
2921 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2922 END_PROFILE(SMBnttrans
);
2926 memcpy(state
->data
,smb_base(req
->inbuf
)+dsoff
,dscnt
);
2929 if (state
->total_param
) {
2931 if (trans_oob(state
->total_param
, 0, pscnt
)
2932 || trans_oob(smb_len(req
->inbuf
), psoff
, pscnt
)) {
2936 /* Can't use talloc here, the core routines do realloc on the
2937 * params and data. */
2938 if ((state
->param
= (char *)SMB_MALLOC(state
->total_param
)) == NULL
) {
2939 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2940 "bytes !\n", (unsigned int)state
->total_param
));
2941 SAFE_FREE(state
->data
);
2943 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2944 END_PROFILE(SMBnttrans
);
2948 memcpy(state
->param
,smb_base(req
->inbuf
)+psoff
,pscnt
);
2951 state
->received_data
= dscnt
;
2952 state
->received_param
= pscnt
;
2954 if(state
->setup_count
> 0) {
2955 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2956 state
->setup_count
));
2959 * No overflow possible here, state->setup_count is an
2960 * unsigned int, being filled by a single byte from
2961 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2962 * below is not necessary, it's here to clarify things. The
2963 * validity of req->vwv and req->wct has been checked in
2964 * init_smb_request already.
2966 if ((state
->setup_count
/2) + 19 > (unsigned int)req
->wct
) {
2970 state
->setup
= (uint16_t *)TALLOC(state
, state
->setup_count
);
2971 if (state
->setup
== NULL
) {
2972 DEBUG(0,("reply_nttrans : Out of memory\n"));
2973 SAFE_FREE(state
->data
);
2974 SAFE_FREE(state
->param
);
2976 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2977 END_PROFILE(SMBnttrans
);
2981 memcpy(state
->setup
, req
->vwv
+19, state
->setup_count
);
2982 dump_data(10, (uint8_t *)state
->setup
, state
->setup_count
);
2985 if ((state
->received_data
== state
->total_data
) &&
2986 (state
->received_param
== state
->total_param
)) {
2987 handle_nttrans(conn
, state
, req
);
2988 SAFE_FREE(state
->param
);
2989 SAFE_FREE(state
->data
);
2991 END_PROFILE(SMBnttrans
);
2995 DLIST_ADD(conn
->pending_trans
, state
);
2997 /* We need to send an interim response then receive the rest
2998 of the parameter/data bytes */
2999 reply_outbuf(req
, 0, 0);
3000 show_msg((char *)req
->outbuf
);
3001 END_PROFILE(SMBnttrans
);
3006 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3007 SAFE_FREE(state
->data
);
3008 SAFE_FREE(state
->param
);
3010 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3011 END_PROFILE(SMBnttrans
);
3015 /****************************************************************************
3016 Reply to a SMBnttranss
3017 ****************************************************************************/
3019 void reply_nttranss(struct smb_request
*req
)
3021 connection_struct
*conn
= req
->conn
;
3022 uint32_t pcnt
,poff
,dcnt
,doff
,pdisp
,ddisp
;
3023 struct trans_state
*state
;
3025 START_PROFILE(SMBnttranss
);
3027 show_msg((const char *)req
->inbuf
);
3029 /* Windows clients expect all replies to
3030 an NT transact secondary (SMBnttranss 0xA1)
3031 to have a command code of NT transact
3032 (SMBnttrans 0xA0). See bug #8989 for details. */
3033 req
->cmd
= SMBnttrans
;
3035 if (req
->wct
< 18) {
3036 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3037 END_PROFILE(SMBnttranss
);
3041 for (state
= conn
->pending_trans
; state
!= NULL
;
3042 state
= state
->next
) {
3043 if (state
->mid
== req
->mid
) {
3048 if ((state
== NULL
) || (state
->cmd
!= SMBnttrans
)) {
3049 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3050 END_PROFILE(SMBnttranss
);
3054 /* Revise state->total_param and state->total_data in case they have
3055 changed downwards */
3056 if (IVAL(req
->vwv
+1, 1) < state
->total_param
) {
3057 state
->total_param
= IVAL(req
->vwv
+1, 1);
3059 if (IVAL(req
->vwv
+3, 1) < state
->total_data
) {
3060 state
->total_data
= IVAL(req
->vwv
+3, 1);
3063 pcnt
= IVAL(req
->vwv
+5, 1);
3064 poff
= IVAL(req
->vwv
+7, 1);
3065 pdisp
= IVAL(req
->vwv
+9, 1);
3067 dcnt
= IVAL(req
->vwv
+11, 1);
3068 doff
= IVAL(req
->vwv
+13, 1);
3069 ddisp
= IVAL(req
->vwv
+15, 1);
3071 state
->received_param
+= pcnt
;
3072 state
->received_data
+= dcnt
;
3074 if ((state
->received_data
> state
->total_data
) ||
3075 (state
->received_param
> state
->total_param
))
3079 if (trans_oob(state
->total_param
, pdisp
, pcnt
)
3080 || trans_oob(smb_len(req
->inbuf
), poff
, pcnt
)) {
3083 memcpy(state
->param
+pdisp
, smb_base(req
->inbuf
)+poff
,pcnt
);
3087 if (trans_oob(state
->total_data
, ddisp
, dcnt
)
3088 || trans_oob(smb_len(req
->inbuf
), doff
, dcnt
)) {
3091 memcpy(state
->data
+ddisp
, smb_base(req
->inbuf
)+doff
,dcnt
);
3094 if ((state
->received_param
< state
->total_param
) ||
3095 (state
->received_data
< state
->total_data
)) {
3096 END_PROFILE(SMBnttranss
);
3100 handle_nttrans(conn
, state
, req
);
3102 DLIST_REMOVE(conn
->pending_trans
, state
);
3103 SAFE_FREE(state
->data
);
3104 SAFE_FREE(state
->param
);
3106 END_PROFILE(SMBnttranss
);
3111 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3112 DLIST_REMOVE(conn
->pending_trans
, state
);
3113 SAFE_FREE(state
->data
);
3114 SAFE_FREE(state
->param
);
3116 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3117 END_PROFILE(SMBnttranss
);