2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-2007
5 Copyright (C) Stefan (metze) Metzmacher 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "fake_file.h"
26 #include "../libcli/security/security.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "passdb/lookup_sid.h"
30 #include "smbprofile.h"
31 #include "libsmb/libsmb.h"
33 extern const struct generic_mapping file_generic_mapping
;
35 static char *nttrans_realloc(char **ptr
, size_t size
)
38 smb_panic("nttrans_realloc() called with NULL ptr");
41 *ptr
= (char *)SMB_REALLOC(*ptr
, size
);
45 memset(*ptr
,'\0',size
);
49 /****************************************************************************
50 Send the required number of replies back.
51 We assume all fields other than the data fields are
52 set correctly for the type of call.
53 HACK ! Always assumes smb_setup field is zero.
54 ****************************************************************************/
56 static void send_nt_replies(connection_struct
*conn
,
57 struct smb_request
*req
, NTSTATUS nt_error
,
58 char *params
, int paramsize
,
59 char *pdata
, int datasize
)
61 int data_to_send
= datasize
;
62 int params_to_send
= paramsize
;
66 int params_sent_thistime
, data_sent_thistime
, total_sent_thistime
;
67 int alignment_offset
= 1;
68 int data_alignment_offset
= 0;
69 struct smbd_server_connection
*sconn
= req
->sconn
;
70 int max_send
= sconn
->smb1
.sessions
.max_send
;
73 * If there genuinely are no parameters or data to send just send
77 if(params_to_send
== 0 && data_to_send
== 0) {
78 reply_outbuf(req
, 18, 0);
79 if (NT_STATUS_V(nt_error
)) {
80 error_packet_set((char *)req
->outbuf
,
84 show_msg((char *)req
->outbuf
);
85 if (!srv_send_smb(sconn
,
88 IS_CONN_ENCRYPTED(conn
),
90 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
92 TALLOC_FREE(req
->outbuf
);
97 * When sending params and data ensure that both are nicely aligned.
98 * Only do this alignment when there is also data to send - else
99 * can cause NT redirector problems.
102 if (((params_to_send
% 4) != 0) && (data_to_send
!= 0)) {
103 data_alignment_offset
= 4 - (params_to_send
% 4);
107 * Space is bufsize minus Netbios over TCP header minus SMB header.
108 * The alignment_offset is to align the param bytes on a four byte
109 * boundary (2 bytes for data len, one byte pad).
110 * NT needs this to work correctly.
113 useable_space
= max_send
- (smb_size
116 + data_alignment_offset
);
118 if (useable_space
< 0) {
119 char *msg
= talloc_asprintf(
121 "send_nt_replies failed sanity useable_space = %d!!!",
123 DEBUG(0, ("%s\n", msg
));
124 exit_server_cleanly(msg
);
127 while (params_to_send
|| data_to_send
) {
130 * Calculate whether we will totally or partially fill this packet.
133 total_sent_thistime
= params_to_send
+ data_to_send
;
136 * We can never send more than useable_space.
139 total_sent_thistime
= MIN(total_sent_thistime
, useable_space
);
141 reply_outbuf(req
, 18,
142 total_sent_thistime
+ alignment_offset
143 + data_alignment_offset
);
146 * We might have had SMBnttranss in req->inbuf, fix that.
148 SCVAL(req
->outbuf
, smb_com
, SMBnttrans
);
151 * Set total params and data to be sent.
154 SIVAL(req
->outbuf
,smb_ntr_TotalParameterCount
,paramsize
);
155 SIVAL(req
->outbuf
,smb_ntr_TotalDataCount
,datasize
);
158 * Calculate how many parameters and data we can fit into
159 * this packet. Parameters get precedence.
162 params_sent_thistime
= MIN(params_to_send
,useable_space
);
163 data_sent_thistime
= useable_space
- params_sent_thistime
;
164 data_sent_thistime
= MIN(data_sent_thistime
,data_to_send
);
166 SIVAL(req
->outbuf
, smb_ntr_ParameterCount
,
167 params_sent_thistime
);
169 if(params_sent_thistime
== 0) {
170 SIVAL(req
->outbuf
,smb_ntr_ParameterOffset
,0);
171 SIVAL(req
->outbuf
,smb_ntr_ParameterDisplacement
,0);
174 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
175 * parameter bytes, however the first 4 bytes of outbuf are
176 * the Netbios over TCP header. Thus use smb_base() to subtract
177 * them from the calculation.
180 SIVAL(req
->outbuf
,smb_ntr_ParameterOffset
,
181 ((smb_buf(req
->outbuf
)+alignment_offset
)
182 - smb_base(req
->outbuf
)));
184 * Absolute displacement of param bytes sent in this packet.
187 SIVAL(req
->outbuf
, smb_ntr_ParameterDisplacement
,
192 * Deal with the data portion.
195 SIVAL(req
->outbuf
, smb_ntr_DataCount
, data_sent_thistime
);
197 if(data_sent_thistime
== 0) {
198 SIVAL(req
->outbuf
,smb_ntr_DataOffset
,0);
199 SIVAL(req
->outbuf
,smb_ntr_DataDisplacement
, 0);
202 * The offset of the data bytes is the offset of the
203 * parameter bytes plus the number of parameters being sent this time.
206 SIVAL(req
->outbuf
, smb_ntr_DataOffset
,
207 ((smb_buf(req
->outbuf
)+alignment_offset
) -
208 smb_base(req
->outbuf
))
209 + params_sent_thistime
+ data_alignment_offset
);
210 SIVAL(req
->outbuf
,smb_ntr_DataDisplacement
, pd
- pdata
);
214 * Copy the param bytes into the packet.
217 if(params_sent_thistime
) {
218 if (alignment_offset
!= 0) {
219 memset(smb_buf(req
->outbuf
), 0,
222 memcpy((smb_buf(req
->outbuf
)+alignment_offset
), pp
,
223 params_sent_thistime
);
227 * Copy in the data bytes
230 if(data_sent_thistime
) {
231 if (data_alignment_offset
!= 0) {
232 memset((smb_buf(req
->outbuf
)+alignment_offset
+
233 params_sent_thistime
), 0,
234 data_alignment_offset
);
236 memcpy(smb_buf(req
->outbuf
)+alignment_offset
237 +params_sent_thistime
+data_alignment_offset
,
238 pd
,data_sent_thistime
);
241 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
242 params_sent_thistime
, data_sent_thistime
, useable_space
));
243 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
244 params_to_send
, data_to_send
, paramsize
, datasize
));
246 if (NT_STATUS_V(nt_error
)) {
247 error_packet_set((char *)req
->outbuf
,
252 /* Send the packet */
253 show_msg((char *)req
->outbuf
);
254 if (!srv_send_smb(sconn
,
257 IS_CONN_ENCRYPTED(conn
),
259 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
262 TALLOC_FREE(req
->outbuf
);
264 pp
+= params_sent_thistime
;
265 pd
+= data_sent_thistime
;
267 params_to_send
-= params_sent_thistime
;
268 data_to_send
-= data_sent_thistime
;
274 if(params_to_send
< 0 || data_to_send
< 0) {
275 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
276 params_to_send
, data_to_send
));
277 exit_server_cleanly("send_nt_replies: internal error");
282 /****************************************************************************
283 Reply to an NT create and X call on a pipe
284 ****************************************************************************/
286 static void nt_open_pipe(char *fname
, connection_struct
*conn
,
287 struct smb_request
*req
, int *ppnum
)
292 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname
));
294 /* Strip \\ off the name. */
297 status
= open_np_file(req
, fname
, &fsp
);
298 if (!NT_STATUS_IS_OK(status
)) {
299 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
300 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
304 reply_nterror(req
, status
);
312 /****************************************************************************
313 Reply to an NT create and X call for pipes.
314 ****************************************************************************/
316 static void do_ntcreate_pipe_open(connection_struct
*conn
,
317 struct smb_request
*req
)
322 uint32 flags
= IVAL(req
->vwv
+3, 1);
323 TALLOC_CTX
*ctx
= talloc_tos();
325 srvstr_pull_req_talloc(ctx
, req
, &fname
, req
->buf
, STR_TERMINATE
);
328 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
332 nt_open_pipe(fname
, conn
, req
, &pnum
);
340 * Deal with pipe return.
343 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
344 /* This is very strange. We
345 * return 50 words, but only set
346 * the wcnt to 42 ? It's definitely
347 * what happens on the wire....
349 reply_outbuf(req
, 50, 0);
350 SCVAL(req
->outbuf
,smb_wct
,42);
352 reply_outbuf(req
, 34, 0);
355 SSVAL(req
->outbuf
, smb_vwv0
, 0xff); /* andx chain ends */
356 SSVAL(req
->outbuf
, smb_vwv1
, 0); /* no andx offset */
358 p
= (char *)req
->outbuf
+ smb_vwv2
;
362 SIVAL(p
,0,FILE_WAS_OPENED
);
365 SIVAL(p
,0,FILE_ATTRIBUTE_NORMAL
); /* File Attributes. */
368 SSVAL(p
,0,FILE_TYPE_MESSAGE_MODE_PIPE
);
370 SSVAL(p
,2, 0x5FF); /* ? */
373 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
375 SIVAL(p
,0,FILE_GENERIC_ALL
);
377 * For pipes W2K3 seems to return
379 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
381 SIVAL(p
,4,(FILE_GENERIC_READ
|FILE_GENERIC_WRITE
)&~FILE_APPEND_DATA
);
384 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname
));
387 struct case_semantics_state
{
388 connection_struct
*conn
;
391 bool short_case_preserve
;
394 /****************************************************************************
395 Restore case semantics.
396 ****************************************************************************/
398 static int restore_case_semantics(struct case_semantics_state
*state
)
400 state
->conn
->case_sensitive
= state
->case_sensitive
;
401 state
->conn
->case_preserve
= state
->case_preserve
;
402 state
->conn
->short_case_preserve
= state
->short_case_preserve
;
406 /****************************************************************************
408 ****************************************************************************/
410 static struct case_semantics_state
*set_posix_case_semantics(TALLOC_CTX
*mem_ctx
,
411 connection_struct
*conn
)
413 struct case_semantics_state
*result
;
415 if (!(result
= talloc(mem_ctx
, struct case_semantics_state
))) {
420 result
->case_sensitive
= conn
->case_sensitive
;
421 result
->case_preserve
= conn
->case_preserve
;
422 result
->short_case_preserve
= conn
->short_case_preserve
;
425 conn
->case_sensitive
= True
;
426 conn
->case_preserve
= True
;
427 conn
->short_case_preserve
= True
;
429 talloc_set_destructor(result
, restore_case_semantics
);
434 /****************************************************************************
435 Reply to an NT create and X call.
436 ****************************************************************************/
438 void reply_ntcreate_and_X(struct smb_request
*req
)
440 connection_struct
*conn
= req
->conn
;
441 struct smb_filename
*smb_fname
= NULL
;
445 uint32 file_attributes
;
447 uint32 create_disposition
;
448 uint32 create_options
;
450 uint64_t allocation_size
;
451 /* Breakout the oplock request bits so we can set the
452 reply bits separately. */
454 SMB_OFF_T file_len
= 0;
456 files_struct
*fsp
= NULL
;
458 struct timespec create_timespec
;
459 struct timespec c_timespec
;
460 struct timespec a_timespec
;
461 struct timespec m_timespec
;
462 struct timespec write_time_ts
;
465 uint8_t oplock_granted
= NO_OPLOCK_RETURN
;
466 struct case_semantics_state
*case_state
= NULL
;
467 TALLOC_CTX
*ctx
= talloc_tos();
469 START_PROFILE(SMBntcreateX
);
472 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
476 flags
= IVAL(req
->vwv
+3, 1);
477 access_mask
= IVAL(req
->vwv
+7, 1);
478 file_attributes
= IVAL(req
->vwv
+13, 1);
479 share_access
= IVAL(req
->vwv
+15, 1);
480 create_disposition
= IVAL(req
->vwv
+17, 1);
481 create_options
= IVAL(req
->vwv
+19, 1);
482 root_dir_fid
= (uint16
)IVAL(req
->vwv
+5, 1);
484 allocation_size
= BVAL(req
->vwv
+9, 1);
486 srvstr_get_path_req(ctx
, req
, &fname
, (const char *)req
->buf
,
487 STR_TERMINATE
, &status
);
489 if (!NT_STATUS_IS_OK(status
)) {
490 reply_nterror(req
, status
);
494 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
495 "file_attributes = 0x%x, share_access = 0x%x, "
496 "create_disposition = 0x%x create_options = 0x%x "
497 "root_dir_fid = 0x%x, fname = %s\n",
499 (unsigned int)access_mask
,
500 (unsigned int)file_attributes
,
501 (unsigned int)share_access
,
502 (unsigned int)create_disposition
,
503 (unsigned int)create_options
,
504 (unsigned int)root_dir_fid
,
508 * we need to remove ignored bits when they come directly from the client
509 * because we reuse some of them for internal stuff
511 create_options
&= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK
;
514 * If it's an IPC, use the pipe handler.
518 if (lp_nt_pipe_support()) {
519 do_ntcreate_pipe_open(conn
, req
);
522 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
526 oplock_request
= (flags
& REQUEST_OPLOCK
) ? EXCLUSIVE_OPLOCK
: 0;
527 if (oplock_request
) {
528 oplock_request
|= (flags
& REQUEST_BATCH_OPLOCK
)
532 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
533 case_state
= set_posix_case_semantics(ctx
, conn
);
535 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
540 status
= filename_convert(ctx
,
542 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
548 TALLOC_FREE(case_state
);
550 if (!NT_STATUS_IS_OK(status
)) {
551 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
553 NT_STATUS_PATH_NOT_COVERED
,
557 reply_nterror(req
, status
);
562 * Bug #6898 - clients using Windows opens should
563 * never be able to set this attribute into the
566 file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
568 status
= SMB_VFS_CREATE_FILE(
571 root_dir_fid
, /* root_dir_fid */
572 smb_fname
, /* fname */
573 access_mask
, /* access_mask */
574 share_access
, /* share_access */
575 create_disposition
, /* create_disposition*/
576 create_options
, /* create_options */
577 file_attributes
, /* file_attributes */
578 oplock_request
, /* oplock_request */
579 allocation_size
, /* allocation_size */
580 0, /* private_flags */
586 if (!NT_STATUS_IS_OK(status
)) {
587 if (open_was_deferred(req
->sconn
, 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
;
661 /* Deal with other possible opens having a modified
663 ZERO_STRUCT(write_time_ts
);
664 get_file_infos(fsp
->file_id
, 0, NULL
, &write_time_ts
);
665 if (!null_timespec(write_time_ts
)) {
666 update_stat_ex_mtime(&smb_fname
->st
, write_time_ts
);
670 create_timespec
= get_create_timespec(conn
, fsp
, smb_fname
);
671 a_timespec
= smb_fname
->st
.st_ex_atime
;
672 m_timespec
= smb_fname
->st
.st_ex_mtime
;
673 c_timespec
= get_change_timespec(conn
, fsp
, smb_fname
);
675 if (lp_dos_filetime_resolution(SNUM(conn
))) {
676 dos_filetime_timespec(&create_timespec
);
677 dos_filetime_timespec(&a_timespec
);
678 dos_filetime_timespec(&m_timespec
);
679 dos_filetime_timespec(&c_timespec
);
682 put_long_date_timespec(conn
->ts_res
, p
, create_timespec
); /* create time. */
684 put_long_date_timespec(conn
->ts_res
, p
, a_timespec
); /* access time */
686 put_long_date_timespec(conn
->ts_res
, p
, m_timespec
); /* write time */
688 put_long_date_timespec(conn
->ts_res
, p
, c_timespec
); /* change time */
690 SIVAL(p
,0,fattr
); /* File Attributes. */
692 SOFF_T(p
, 0, SMB_VFS_GET_ALLOC_SIZE(conn
,fsp
,&smb_fname
->st
));
694 SOFF_T(p
,0,file_len
);
696 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
697 uint16_t file_status
= (NO_EAS
|NO_SUBSTREAMS
|NO_REPARSETAG
);
698 size_t num_names
= 0;
699 unsigned int num_streams
= 0;
700 struct stream_struct
*streams
= NULL
;
702 /* Do we have any EA's ? */
703 status
= get_ea_names_from_file(ctx
, conn
, fsp
,
704 smb_fname
->base_name
, NULL
, &num_names
);
705 if (NT_STATUS_IS_OK(status
) && num_names
) {
706 file_status
&= ~NO_EAS
;
708 status
= vfs_streaminfo(conn
, NULL
, smb_fname
->base_name
, ctx
,
709 &num_streams
, &streams
);
710 /* There is always one stream, ::$DATA. */
711 if (NT_STATUS_IS_OK(status
) && num_streams
> 1) {
712 file_status
&= ~NO_SUBSTREAMS
;
714 TALLOC_FREE(streams
);
715 SSVAL(p
,2,file_status
);
718 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
720 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
723 if (fsp
->is_directory
||
724 can_write_to_file(conn
, smb_fname
)) {
725 perms
= FILE_GENERIC_ALL
;
727 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
732 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
733 fsp
->fnum
, smb_fname_str_dbg(smb_fname
)));
736 END_PROFILE(SMBntcreateX
);
740 /****************************************************************************
741 Reply to a NT_TRANSACT_CREATE call to open a pipe.
742 ****************************************************************************/
744 static void do_nt_transact_create_pipe(connection_struct
*conn
,
745 struct smb_request
*req
,
746 uint16
**ppsetup
, uint32 setup_count
,
747 char **ppparams
, uint32 parameter_count
,
748 char **ppdata
, uint32 data_count
)
751 char *params
= *ppparams
;
757 TALLOC_CTX
*ctx
= talloc_tos();
760 * Ensure minimum number of parameters sent.
763 if(parameter_count
< 54) {
764 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
765 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
769 flags
= IVAL(params
,0);
771 srvstr_get_path(ctx
, params
, req
->flags2
, &fname
, params
+53,
772 parameter_count
-53, STR_TERMINATE
,
774 if (!NT_STATUS_IS_OK(status
)) {
775 reply_nterror(req
, status
);
779 nt_open_pipe(fname
, conn
, req
, &pnum
);
786 /* Realloc the size of parameters and data we will return */
787 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
788 /* Extended response is 32 more byyes. */
793 params
= nttrans_realloc(ppparams
, param_len
);
795 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
800 SCVAL(p
,0,NO_OPLOCK_RETURN
);
805 SIVAL(p
,0,FILE_WAS_OPENED
);
809 SIVAL(p
,0,FILE_ATTRIBUTE_NORMAL
); /* File Attributes. */
812 SSVAL(p
,0,FILE_TYPE_MESSAGE_MODE_PIPE
);
814 SSVAL(p
,2, 0x5FF); /* ? */
817 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
819 SIVAL(p
,0,FILE_GENERIC_ALL
);
821 * For pipes W2K3 seems to return
823 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
825 SIVAL(p
,4,(FILE_GENERIC_READ
|FILE_GENERIC_WRITE
)&~FILE_APPEND_DATA
);
828 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname
));
830 /* Send the required number of replies */
831 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
836 /****************************************************************************
837 Internal fn to set security descriptors.
838 ****************************************************************************/
840 NTSTATUS
set_sd(files_struct
*fsp
, uint8_t *data
, uint32_t sd_len
,
841 uint32_t security_info_sent
)
843 struct security_descriptor
*psd
= NULL
;
847 return NT_STATUS_INVALID_PARAMETER
;
850 if (!CAN_WRITE(fsp
->conn
)) {
851 return NT_STATUS_ACCESS_DENIED
;
854 if (!lp_nt_acl_support(SNUM(fsp
->conn
))) {
858 status
= unmarshall_sec_desc(talloc_tos(), data
, sd_len
, &psd
);
860 if (!NT_STATUS_IS_OK(status
)) {
864 if (psd
->owner_sid
== NULL
) {
865 security_info_sent
&= ~SECINFO_OWNER
;
867 if (psd
->group_sid
== NULL
) {
868 security_info_sent
&= ~SECINFO_GROUP
;
871 /* Ensure we have at least one thing set. */
872 if ((security_info_sent
& (SECINFO_OWNER
|SECINFO_GROUP
|SECINFO_DACL
|SECINFO_SACL
)) == 0) {
873 if (security_info_sent
& SECINFO_LABEL
) {
874 /* Only consider SECINFO_LABEL if no other
875 bits are set. Just like W2K3 we don't
879 return NT_STATUS_INVALID_PARAMETER
;
882 /* Ensure we have the rights to do this. */
883 if (security_info_sent
& SECINFO_OWNER
) {
884 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
885 return NT_STATUS_ACCESS_DENIED
;
889 if (security_info_sent
& SECINFO_GROUP
) {
890 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
891 return NT_STATUS_ACCESS_DENIED
;
895 if (security_info_sent
& SECINFO_DACL
) {
896 if (!(fsp
->access_mask
& SEC_STD_WRITE_DAC
)) {
897 return NT_STATUS_ACCESS_DENIED
;
899 /* Convert all the generic bits. */
901 security_acl_map_generic(psd
->dacl
, &file_generic_mapping
);
905 if (security_info_sent
& SECINFO_SACL
) {
906 if (!(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
907 return NT_STATUS_ACCESS_DENIED
;
909 /* Convert all the generic bits. */
911 security_acl_map_generic(psd
->sacl
, &file_generic_mapping
);
915 if (DEBUGLEVEL
>= 10) {
916 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp
)));
917 NDR_PRINT_DEBUG(security_descriptor
, psd
);
920 status
= SMB_VFS_FSET_NT_ACL(fsp
, security_info_sent
, psd
);
927 /****************************************************************************
928 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
929 ****************************************************************************/
931 struct ea_list
*read_nttrans_ea_list(TALLOC_CTX
*ctx
, const char *pdata
, size_t data_size
)
933 struct ea_list
*ea_list_head
= NULL
;
940 while (offset
+ 4 <= data_size
) {
941 size_t next_offset
= IVAL(pdata
,offset
);
942 struct ea_list
*eal
= read_ea_list_entry(ctx
, pdata
+ offset
+ 4, data_size
- offset
- 4, NULL
);
948 DLIST_ADD_END(ea_list_head
, eal
, struct ea_list
*);
949 if (next_offset
== 0) {
952 offset
+= next_offset
;
958 /****************************************************************************
959 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
960 ****************************************************************************/
962 static void call_nt_transact_create(connection_struct
*conn
,
963 struct smb_request
*req
,
964 uint16
**ppsetup
, uint32 setup_count
,
965 char **ppparams
, uint32 parameter_count
,
966 char **ppdata
, uint32 data_count
,
967 uint32 max_data_count
)
969 struct smb_filename
*smb_fname
= NULL
;
971 char *params
= *ppparams
;
972 char *data
= *ppdata
;
973 /* Breakout the oplock request bits so we can set the reply bits separately. */
975 SMB_OFF_T file_len
= 0;
977 files_struct
*fsp
= NULL
;
981 uint32 file_attributes
;
983 uint32 create_disposition
;
984 uint32 create_options
;
986 struct security_descriptor
*sd
= NULL
;
989 struct timespec create_timespec
;
990 struct timespec c_timespec
;
991 struct timespec a_timespec
;
992 struct timespec m_timespec
;
993 struct timespec write_time_ts
;
994 struct ea_list
*ea_list
= NULL
;
997 uint64_t allocation_size
;
999 uint8_t oplock_granted
;
1000 struct case_semantics_state
*case_state
= NULL
;
1001 TALLOC_CTX
*ctx
= talloc_tos();
1003 DEBUG(5,("call_nt_transact_create\n"));
1006 * If it's an IPC, use the pipe handler.
1010 if (lp_nt_pipe_support()) {
1011 do_nt_transact_create_pipe(
1013 ppsetup
, setup_count
,
1014 ppparams
, parameter_count
,
1015 ppdata
, data_count
);
1018 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
1023 * Ensure minimum number of parameters sent.
1026 if(parameter_count
< 54) {
1027 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
1028 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1032 flags
= IVAL(params
,0);
1033 access_mask
= IVAL(params
,8);
1034 file_attributes
= IVAL(params
,20);
1035 share_access
= IVAL(params
,24);
1036 create_disposition
= IVAL(params
,28);
1037 create_options
= IVAL(params
,32);
1038 sd_len
= IVAL(params
,36);
1039 ea_len
= IVAL(params
,40);
1040 root_dir_fid
= (uint16
)IVAL(params
,4);
1041 allocation_size
= BVAL(params
,12);
1044 * we need to remove ignored bits when they come directly from the client
1045 * because we reuse some of them for internal stuff
1047 create_options
&= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK
;
1049 /* Ensure the data_len is correct for the sd and ea values given. */
1050 if ((ea_len
+ sd_len
> data_count
)
1051 || (ea_len
> data_count
) || (sd_len
> data_count
)
1052 || (ea_len
+ sd_len
< ea_len
) || (ea_len
+ sd_len
< sd_len
)) {
1053 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1054 "%u, data_count = %u\n", (unsigned int)ea_len
,
1055 (unsigned int)sd_len
, (unsigned int)data_count
));
1056 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1061 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1064 status
= unmarshall_sec_desc(ctx
, (uint8_t *)data
, sd_len
,
1066 if (!NT_STATUS_IS_OK(status
)) {
1067 DEBUG(10, ("call_nt_transact_create: "
1068 "unmarshall_sec_desc failed: %s\n",
1069 nt_errstr(status
)));
1070 reply_nterror(req
, status
);
1076 if (!lp_ea_support(SNUM(conn
))) {
1077 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1078 "EA's not supported.\n",
1079 (unsigned int)ea_len
));
1080 reply_nterror(req
, NT_STATUS_EAS_NOT_SUPPORTED
);
1085 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1086 "too small (should be more than 10)\n",
1087 (unsigned int)ea_len
));
1088 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1092 /* We have already checked that ea_len <= data_count here. */
1093 ea_list
= read_nttrans_ea_list(talloc_tos(), data
+ sd_len
,
1095 if (ea_list
== NULL
) {
1096 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1101 srvstr_get_path(ctx
, params
, req
->flags2
, &fname
,
1102 params
+53, parameter_count
-53,
1103 STR_TERMINATE
, &status
);
1104 if (!NT_STATUS_IS_OK(status
)) {
1105 reply_nterror(req
, status
);
1109 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1110 case_state
= set_posix_case_semantics(ctx
, conn
);
1112 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1117 status
= filename_convert(ctx
,
1119 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1125 TALLOC_FREE(case_state
);
1127 if (!NT_STATUS_IS_OK(status
)) {
1128 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
1129 reply_botherror(req
,
1130 NT_STATUS_PATH_NOT_COVERED
,
1131 ERRSRV
, ERRbadpath
);
1134 reply_nterror(req
, status
);
1138 oplock_request
= (flags
& REQUEST_OPLOCK
) ? EXCLUSIVE_OPLOCK
: 0;
1139 if (oplock_request
) {
1140 oplock_request
|= (flags
& REQUEST_BATCH_OPLOCK
)
1145 * Bug #6898 - clients using Windows opens should
1146 * never be able to set this attribute into the
1149 file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
1151 status
= SMB_VFS_CREATE_FILE(
1154 root_dir_fid
, /* root_dir_fid */
1155 smb_fname
, /* fname */
1156 access_mask
, /* access_mask */
1157 share_access
, /* share_access */
1158 create_disposition
, /* create_disposition*/
1159 create_options
, /* create_options */
1160 file_attributes
, /* file_attributes */
1161 oplock_request
, /* oplock_request */
1162 allocation_size
, /* allocation_size */
1163 0, /* private_flags */
1165 ea_list
, /* ea_list */
1169 if(!NT_STATUS_IS_OK(status
)) {
1170 if (open_was_deferred(req
->sconn
, req
->mid
)) {
1171 /* We have re-scheduled this call, no error. */
1174 reply_openerror(req
, status
);
1178 /* Ensure we're pointing at the correct stat struct. */
1179 TALLOC_FREE(smb_fname
);
1180 smb_fname
= fsp
->fsp_name
;
1183 * If the caller set the extended oplock request bit
1184 * and we granted one (by whatever means) - set the
1185 * correct bit for extended oplock reply.
1188 if (oplock_request
&&
1189 (lp_fake_oplocks(SNUM(conn
))
1190 || EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
))) {
1193 * Exclusive oplock granted
1196 if (flags
& REQUEST_BATCH_OPLOCK
) {
1197 oplock_granted
= BATCH_OPLOCK_RETURN
;
1199 oplock_granted
= EXCLUSIVE_OPLOCK_RETURN
;
1201 } else if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
1202 oplock_granted
= LEVEL_II_OPLOCK_RETURN
;
1204 oplock_granted
= NO_OPLOCK_RETURN
;
1207 file_len
= smb_fname
->st
.st_ex_size
;
1209 /* Realloc the size of parameters and data we will return */
1210 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1211 /* Extended response is 32 more byyes. */
1216 params
= nttrans_realloc(ppparams
, param_len
);
1217 if(params
== NULL
) {
1218 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1223 SCVAL(p
, 0, oplock_granted
);
1226 SSVAL(p
,0,fsp
->fnum
);
1228 if ((create_disposition
== FILE_SUPERSEDE
)
1229 && (info
== FILE_WAS_OVERWRITTEN
)) {
1230 SIVAL(p
,0,FILE_WAS_SUPERSEDED
);
1236 fattr
= dos_mode(conn
, smb_fname
);
1238 fattr
= FILE_ATTRIBUTE_NORMAL
;
1241 /* Deal with other possible opens having a modified
1243 ZERO_STRUCT(write_time_ts
);
1244 get_file_infos(fsp
->file_id
, 0, NULL
, &write_time_ts
);
1245 if (!null_timespec(write_time_ts
)) {
1246 update_stat_ex_mtime(&smb_fname
->st
, write_time_ts
);
1250 create_timespec
= get_create_timespec(conn
, fsp
, smb_fname
);
1251 a_timespec
= smb_fname
->st
.st_ex_atime
;
1252 m_timespec
= smb_fname
->st
.st_ex_mtime
;
1253 c_timespec
= get_change_timespec(conn
, fsp
, smb_fname
);
1255 if (lp_dos_filetime_resolution(SNUM(conn
))) {
1256 dos_filetime_timespec(&create_timespec
);
1257 dos_filetime_timespec(&a_timespec
);
1258 dos_filetime_timespec(&m_timespec
);
1259 dos_filetime_timespec(&c_timespec
);
1262 put_long_date_timespec(conn
->ts_res
, p
, create_timespec
); /* create time. */
1264 put_long_date_timespec(conn
->ts_res
, p
, a_timespec
); /* access time */
1266 put_long_date_timespec(conn
->ts_res
, p
, m_timespec
); /* write time */
1268 put_long_date_timespec(conn
->ts_res
, p
, c_timespec
); /* change time */
1270 SIVAL(p
,0,fattr
); /* File Attributes. */
1272 SOFF_T(p
, 0, SMB_VFS_GET_ALLOC_SIZE(conn
, fsp
, &smb_fname
->st
));
1274 SOFF_T(p
,0,file_len
);
1276 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1277 uint16_t file_status
= (NO_EAS
|NO_SUBSTREAMS
|NO_REPARSETAG
);
1278 size_t num_names
= 0;
1279 unsigned int num_streams
= 0;
1280 struct stream_struct
*streams
= NULL
;
1282 /* Do we have any EA's ? */
1283 status
= get_ea_names_from_file(ctx
, conn
, fsp
,
1284 smb_fname
->base_name
, NULL
, &num_names
);
1285 if (NT_STATUS_IS_OK(status
) && num_names
) {
1286 file_status
&= ~NO_EAS
;
1288 status
= vfs_streaminfo(conn
, NULL
, smb_fname
->base_name
, ctx
,
1289 &num_streams
, &streams
);
1290 /* There is always one stream, ::$DATA. */
1291 if (NT_STATUS_IS_OK(status
) && num_streams
> 1) {
1292 file_status
&= ~NO_SUBSTREAMS
;
1294 TALLOC_FREE(streams
);
1295 SSVAL(p
,2,file_status
);
1298 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
1300 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1303 if (fsp
->is_directory
||
1304 can_write_to_file(conn
, smb_fname
)) {
1305 perms
= FILE_GENERIC_ALL
;
1307 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
1312 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1313 smb_fname_str_dbg(smb_fname
)));
1315 /* Send the required number of replies */
1316 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
1321 /****************************************************************************
1322 Reply to a NT CANCEL request.
1323 conn POINTER CAN BE NULL HERE !
1324 ****************************************************************************/
1326 void reply_ntcancel(struct smb_request
*req
)
1329 * Go through and cancel any pending change notifies.
1332 START_PROFILE(SMBntcancel
);
1333 srv_cancel_sign_response(req
->sconn
);
1334 remove_pending_change_notify_requests_by_mid(req
->sconn
, req
->mid
);
1335 remove_pending_lock_requests_by_mid_smb1(req
->sconn
, req
->mid
);
1337 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1338 (unsigned long long)req
->mid
));
1340 END_PROFILE(SMBntcancel
);
1344 /****************************************************************************
1346 ****************************************************************************/
1348 static NTSTATUS
copy_internals(TALLOC_CTX
*ctx
,
1349 connection_struct
*conn
,
1350 struct smb_request
*req
,
1351 struct smb_filename
*smb_fname_src
,
1352 struct smb_filename
*smb_fname_dst
,
1355 files_struct
*fsp1
,*fsp2
;
1359 NTSTATUS status
= NT_STATUS_OK
;
1362 if (!CAN_WRITE(conn
)) {
1363 status
= NT_STATUS_MEDIA_WRITE_PROTECTED
;
1367 /* Source must already exist. */
1368 if (!VALID_STAT(smb_fname_src
->st
)) {
1369 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1373 /* Ensure attributes match. */
1374 fattr
= dos_mode(conn
, smb_fname_src
);
1375 if ((fattr
& ~attrs
) & (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) {
1376 status
= NT_STATUS_NO_SUCH_FILE
;
1380 /* Disallow if dst file already exists. */
1381 if (VALID_STAT(smb_fname_dst
->st
)) {
1382 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
1386 /* No links from a directory. */
1387 if (S_ISDIR(smb_fname_src
->st
.st_ex_mode
)) {
1388 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
1392 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1393 smb_fname_str_dbg(smb_fname_src
),
1394 smb_fname_str_dbg(smb_fname_dst
)));
1396 status
= SMB_VFS_CREATE_FILE(
1399 0, /* root_dir_fid */
1400 smb_fname_src
, /* fname */
1401 FILE_READ_DATA
|FILE_READ_ATTRIBUTES
|
1402 FILE_READ_EA
, /* access_mask */
1403 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1405 FILE_OPEN
, /* create_disposition*/
1406 0, /* create_options */
1407 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
1408 NO_OPLOCK
, /* oplock_request */
1409 0, /* allocation_size */
1410 0, /* private_flags */
1416 if (!NT_STATUS_IS_OK(status
)) {
1420 status
= SMB_VFS_CREATE_FILE(
1423 0, /* root_dir_fid */
1424 smb_fname_dst
, /* fname */
1425 FILE_WRITE_DATA
|FILE_WRITE_ATTRIBUTES
|
1426 FILE_WRITE_EA
, /* access_mask */
1427 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1429 FILE_CREATE
, /* create_disposition*/
1430 0, /* create_options */
1431 fattr
, /* file_attributes */
1432 NO_OPLOCK
, /* oplock_request */
1433 0, /* allocation_size */
1434 0, /* private_flags */
1440 if (!NT_STATUS_IS_OK(status
)) {
1441 close_file(NULL
, fsp1
, ERROR_CLOSE
);
1445 if (smb_fname_src
->st
.st_ex_size
) {
1446 ret
= vfs_transfer_file(fsp1
, fsp2
, smb_fname_src
->st
.st_ex_size
);
1450 * As we are opening fsp1 read-only we only expect
1451 * an error on close on fsp2 if we are out of space.
1452 * Thus we don't look at the error return from the
1455 close_file(NULL
, fsp1
, NORMAL_CLOSE
);
1457 /* Ensure the modtime is set correctly on the destination file. */
1458 set_close_write_time(fsp2
, smb_fname_src
->st
.st_ex_mtime
);
1460 status
= close_file(NULL
, fsp2
, NORMAL_CLOSE
);
1462 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1463 creates the file. This isn't the correct thing to do in the copy
1465 if (!parent_dirname(talloc_tos(), smb_fname_dst
->base_name
, &parent
,
1467 status
= NT_STATUS_NO_MEMORY
;
1470 file_set_dosmode(conn
, smb_fname_dst
, fattr
, parent
, false);
1471 TALLOC_FREE(parent
);
1473 if (ret
< (SMB_OFF_T
)smb_fname_src
->st
.st_ex_size
) {
1474 status
= NT_STATUS_DISK_FULL
;
1478 if (!NT_STATUS_IS_OK(status
)) {
1479 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1480 nt_errstr(status
), smb_fname_str_dbg(smb_fname_src
),
1481 smb_fname_str_dbg(smb_fname_dst
)));
1487 /****************************************************************************
1488 Reply to a NT rename request.
1489 ****************************************************************************/
1491 void reply_ntrename(struct smb_request
*req
)
1493 connection_struct
*conn
= req
->conn
;
1494 struct smb_filename
*smb_fname_old
= NULL
;
1495 struct smb_filename
*smb_fname_new
= NULL
;
1496 char *oldname
= NULL
;
1497 char *newname
= NULL
;
1500 bool src_has_wcard
= False
;
1501 bool dest_has_wcard
= False
;
1503 uint32_t ucf_flags_src
= 0;
1504 uint32_t ucf_flags_dst
= 0;
1506 TALLOC_CTX
*ctx
= talloc_tos();
1507 bool stream_rename
= false;
1509 START_PROFILE(SMBntrename
);
1512 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1516 attrs
= SVAL(req
->vwv
+0, 0);
1517 rename_type
= SVAL(req
->vwv
+1, 0);
1519 p
= (const char *)req
->buf
+ 1;
1520 p
+= srvstr_get_path_req_wcard(ctx
, req
, &oldname
, p
, STR_TERMINATE
,
1521 &status
, &src_has_wcard
);
1522 if (!NT_STATUS_IS_OK(status
)) {
1523 reply_nterror(req
, status
);
1527 if (ms_has_wild(oldname
)) {
1528 reply_nterror(req
, NT_STATUS_OBJECT_PATH_SYNTAX_BAD
);
1533 p
+= srvstr_get_path_req_wcard(ctx
, req
, &newname
, p
, STR_TERMINATE
,
1534 &status
, &dest_has_wcard
);
1535 if (!NT_STATUS_IS_OK(status
)) {
1536 reply_nterror(req
, status
);
1540 if (!lp_posix_pathnames()) {
1541 /* The newname must begin with a ':' if the
1542 oldname contains a ':'. */
1543 if (strchr_m(oldname
, ':')) {
1544 if (newname
[0] != ':') {
1545 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1548 stream_rename
= true;
1553 * If this is a rename operation, allow wildcards and save the
1554 * destination's last component.
1556 if (rename_type
== RENAME_FLAG_RENAME
) {
1557 ucf_flags_src
= UCF_COND_ALLOW_WCARD_LCOMP
;
1558 ucf_flags_dst
= UCF_COND_ALLOW_WCARD_LCOMP
| UCF_SAVE_LCOMP
;
1561 /* rename_internals() calls unix_convert(), so don't call it here. */
1562 status
= filename_convert(ctx
, conn
,
1563 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1568 if (!NT_STATUS_IS_OK(status
)) {
1569 if (NT_STATUS_EQUAL(status
,
1570 NT_STATUS_PATH_NOT_COVERED
)) {
1571 reply_botherror(req
,
1572 NT_STATUS_PATH_NOT_COVERED
,
1573 ERRSRV
, ERRbadpath
);
1576 reply_nterror(req
, status
);
1580 status
= filename_convert(ctx
, conn
,
1581 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1586 if (!NT_STATUS_IS_OK(status
)) {
1587 if (NT_STATUS_EQUAL(status
,
1588 NT_STATUS_PATH_NOT_COVERED
)) {
1589 reply_botherror(req
,
1590 NT_STATUS_PATH_NOT_COVERED
,
1591 ERRSRV
, ERRbadpath
);
1594 reply_nterror(req
, status
);
1598 if (stream_rename
) {
1599 /* smb_fname_new must be the same as smb_fname_old. */
1600 TALLOC_FREE(smb_fname_new
->base_name
);
1601 smb_fname_new
->base_name
= talloc_strdup(smb_fname_new
,
1602 smb_fname_old
->base_name
);
1603 if (!smb_fname_new
->base_name
) {
1604 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1609 DEBUG(3,("reply_ntrename: %s -> %s\n",
1610 smb_fname_str_dbg(smb_fname_old
),
1611 smb_fname_str_dbg(smb_fname_new
)));
1613 switch(rename_type
) {
1614 case RENAME_FLAG_RENAME
:
1615 status
= rename_internals(ctx
, conn
, req
,
1616 smb_fname_old
, smb_fname_new
,
1617 attrs
, False
, src_has_wcard
,
1621 case RENAME_FLAG_HARD_LINK
:
1622 if (src_has_wcard
|| dest_has_wcard
) {
1624 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1626 status
= hardlink_internals(ctx
, conn
,
1633 case RENAME_FLAG_COPY
:
1634 if (src_has_wcard
|| dest_has_wcard
) {
1636 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1638 status
= copy_internals(ctx
, conn
, req
,
1644 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION
:
1645 status
= NT_STATUS_INVALID_PARAMETER
;
1648 status
= NT_STATUS_ACCESS_DENIED
; /* Default error. */
1652 if (!NT_STATUS_IS_OK(status
)) {
1653 if (open_was_deferred(req
->sconn
, req
->mid
)) {
1654 /* We have re-scheduled this call. */
1658 reply_nterror(req
, status
);
1662 reply_outbuf(req
, 0, 0);
1664 END_PROFILE(SMBntrename
);
1668 /****************************************************************************
1669 Reply to a notify change - queue the request and
1670 don't allow a directory to be opened.
1671 ****************************************************************************/
1673 static void smbd_smb1_notify_reply(struct smb_request
*req
,
1674 NTSTATUS error_code
,
1675 uint8_t *buf
, size_t len
)
1677 send_nt_replies(req
->conn
, req
, error_code
, (char *)buf
, len
, NULL
, 0);
1680 static void call_nt_transact_notify_change(connection_struct
*conn
,
1681 struct smb_request
*req
,
1685 uint32 parameter_count
,
1686 char **ppdata
, uint32 data_count
,
1687 uint32 max_data_count
,
1688 uint32 max_param_count
)
1690 uint16
*setup
= *ppsetup
;
1696 if(setup_count
< 6) {
1697 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1701 fsp
= file_fsp(req
, SVAL(setup
,4));
1702 filter
= IVAL(setup
, 0);
1703 recursive
= (SVAL(setup
, 6) != 0) ? True
: False
;
1705 DEBUG(3,("call_nt_transact_notify_change\n"));
1708 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
1713 char *filter_string
;
1715 if (!(filter_string
= notify_filter_string(NULL
, filter
))) {
1716 reply_nterror(req
,NT_STATUS_NO_MEMORY
);
1720 DEBUG(3,("call_nt_transact_notify_change: notify change "
1721 "called on %s, filter = %s, recursive = %d\n",
1722 fsp_str_dbg(fsp
), filter_string
, recursive
));
1724 TALLOC_FREE(filter_string
);
1727 if((!fsp
->is_directory
) || (conn
!= fsp
->conn
)) {
1728 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1732 if (fsp
->notify
== NULL
) {
1734 status
= change_notify_create(fsp
, filter
, recursive
);
1736 if (!NT_STATUS_IS_OK(status
)) {
1737 DEBUG(10, ("change_notify_create returned %s\n",
1738 nt_errstr(status
)));
1739 reply_nterror(req
, status
);
1744 if (fsp
->notify
->num_changes
!= 0) {
1747 * We've got changes pending, respond immediately
1751 * TODO: write a torture test to check the filtering behaviour
1755 change_notify_reply(req
,
1759 smbd_smb1_notify_reply
);
1762 * change_notify_reply() above has independently sent its
1769 * No changes pending, queue the request
1772 status
= change_notify_add_request(req
,
1776 smbd_smb1_notify_reply
);
1777 if (!NT_STATUS_IS_OK(status
)) {
1778 reply_nterror(req
, status
);
1783 /****************************************************************************
1784 Reply to an NT transact rename command.
1785 ****************************************************************************/
1787 static void call_nt_transact_rename(connection_struct
*conn
,
1788 struct smb_request
*req
,
1789 uint16
**ppsetup
, uint32 setup_count
,
1790 char **ppparams
, uint32 parameter_count
,
1791 char **ppdata
, uint32 data_count
,
1792 uint32 max_data_count
)
1794 char *params
= *ppparams
;
1795 char *new_name
= NULL
;
1796 files_struct
*fsp
= NULL
;
1797 bool dest_has_wcard
= False
;
1799 TALLOC_CTX
*ctx
= talloc_tos();
1801 if(parameter_count
< 5) {
1802 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1806 fsp
= file_fsp(req
, SVAL(params
, 0));
1807 if (!check_fsp(conn
, req
, fsp
)) {
1810 srvstr_get_path_wcard(ctx
, params
, req
->flags2
, &new_name
, params
+4,
1811 parameter_count
- 4,
1812 STR_TERMINATE
, &status
, &dest_has_wcard
);
1813 if (!NT_STATUS_IS_OK(status
)) {
1814 reply_nterror(req
, status
);
1819 * W2K3 ignores this request as the RAW-RENAME test
1820 * demonstrates, so we do.
1822 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
1824 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1825 fsp_str_dbg(fsp
), new_name
));
1830 /******************************************************************************
1831 Fake up a completely empty SD.
1832 *******************************************************************************/
1834 static NTSTATUS
get_null_nt_acl(TALLOC_CTX
*mem_ctx
, struct security_descriptor
**ppsd
)
1838 *ppsd
= make_standard_sec_desc( mem_ctx
, &global_sid_World
, &global_sid_World
, NULL
, &sd_size
);
1840 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1841 return NT_STATUS_NO_MEMORY
;
1844 return NT_STATUS_OK
;
1847 /****************************************************************************
1848 Reply to query a security descriptor.
1849 Callable from SMB2 and SMB2.
1850 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1852 ****************************************************************************/
1854 NTSTATUS
smbd_do_query_security_desc(connection_struct
*conn
,
1855 TALLOC_CTX
*mem_ctx
,
1857 uint32_t security_info_wanted
,
1858 uint32_t max_data_count
,
1859 uint8_t **ppmarshalled_sd
,
1863 struct security_descriptor
*psd
= NULL
;
1866 * Get the permissions to return.
1869 if ((security_info_wanted
& SECINFO_SACL
) &&
1870 !(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
1871 return NT_STATUS_ACCESS_DENIED
;
1874 if ((security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|SECINFO_GROUP
)) &&
1875 !(fsp
->access_mask
& SEC_STD_READ_CONTROL
)) {
1876 return NT_STATUS_ACCESS_DENIED
;
1879 if (security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|
1880 SECINFO_GROUP
|SECINFO_SACL
)) {
1881 /* Don't return SECINFO_LABEL if anything else was
1882 requested. See bug #8458. */
1883 security_info_wanted
&= ~SECINFO_LABEL
;
1886 if (!lp_nt_acl_support(SNUM(conn
))) {
1887 status
= get_null_nt_acl(mem_ctx
, &psd
);
1888 } else if (security_info_wanted
& SECINFO_LABEL
) {
1889 /* Like W2K3 return a null object. */
1890 status
= get_null_nt_acl(mem_ctx
, &psd
);
1892 status
= SMB_VFS_FGET_NT_ACL(
1893 fsp
, security_info_wanted
, &psd
);
1895 if (!NT_STATUS_IS_OK(status
)) {
1899 if (!(security_info_wanted
& SECINFO_OWNER
)) {
1900 psd
->owner_sid
= NULL
;
1902 if (!(security_info_wanted
& SECINFO_GROUP
)) {
1903 psd
->group_sid
= NULL
;
1905 if (!(security_info_wanted
& SECINFO_DACL
)) {
1906 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
1909 if (!(security_info_wanted
& SECINFO_SACL
)) {
1910 psd
->type
&= ~SEC_DESC_SACL_PRESENT
;
1914 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1915 * present in the reply to match Windows behavior */
1916 if (psd
->sacl
== NULL
&&
1917 security_info_wanted
& SECINFO_SACL
)
1918 psd
->type
|= SEC_DESC_SACL_PRESENT
;
1919 if (psd
->dacl
== NULL
&&
1920 security_info_wanted
& SECINFO_DACL
)
1921 psd
->type
|= SEC_DESC_DACL_PRESENT
;
1923 if (security_info_wanted
& SECINFO_LABEL
) {
1924 /* Like W2K3 return a null object. */
1925 psd
->owner_sid
= NULL
;
1926 psd
->group_sid
= NULL
;
1929 psd
->type
&= ~(SEC_DESC_DACL_PRESENT
|SEC_DESC_SACL_PRESENT
);
1932 *psd_size
= ndr_size_security_descriptor(psd
, 0);
1934 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1935 (unsigned long)*psd_size
));
1937 if (DEBUGLEVEL
>= 10) {
1938 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1940 NDR_PRINT_DEBUG(security_descriptor
, psd
);
1943 if (max_data_count
< *psd_size
) {
1945 return NT_STATUS_BUFFER_TOO_SMALL
;
1948 status
= marshall_sec_desc(mem_ctx
, psd
,
1949 ppmarshalled_sd
, psd_size
);
1951 if (!NT_STATUS_IS_OK(status
)) {
1957 return NT_STATUS_OK
;
1960 /****************************************************************************
1961 SMB1 reply to query a security descriptor.
1962 ****************************************************************************/
1964 static void call_nt_transact_query_security_desc(connection_struct
*conn
,
1965 struct smb_request
*req
,
1969 uint32 parameter_count
,
1972 uint32 max_data_count
)
1974 char *params
= *ppparams
;
1975 char *data
= *ppdata
;
1977 uint32 security_info_wanted
;
1978 files_struct
*fsp
= NULL
;
1980 uint8_t *marshalled_sd
= NULL
;
1982 if(parameter_count
< 8) {
1983 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1987 fsp
= file_fsp(req
, SVAL(params
,0));
1989 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
1993 security_info_wanted
= IVAL(params
,4);
1995 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1996 "info_wanted = 0x%x\n", fsp_str_dbg(fsp
),
1997 (unsigned int)security_info_wanted
));
1999 params
= nttrans_realloc(ppparams
, 4);
2000 if(params
== NULL
) {
2001 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2006 * Get the permissions to return.
2009 status
= smbd_do_query_security_desc(conn
,
2012 security_info_wanted
,
2017 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
2018 SIVAL(params
,0,(uint32_t)sd_size
);
2019 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2020 params
, 4, NULL
, 0);
2024 if (!NT_STATUS_IS_OK(status
)) {
2025 reply_nterror(req
, status
);
2029 SMB_ASSERT(sd_size
> 0);
2031 SIVAL(params
,0,(uint32_t)sd_size
);
2033 if (max_data_count
< sd_size
) {
2034 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2035 params
, 4, NULL
, 0);
2040 * Allocate the data we will return.
2043 data
= nttrans_realloc(ppdata
, sd_size
);
2045 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2049 memcpy(data
, marshalled_sd
, sd_size
);
2051 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, 4, data
, (int)sd_size
);
2056 /****************************************************************************
2057 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2058 ****************************************************************************/
2060 static void call_nt_transact_set_security_desc(connection_struct
*conn
,
2061 struct smb_request
*req
,
2065 uint32 parameter_count
,
2068 uint32 max_data_count
)
2070 char *params
= *ppparams
;
2071 char *data
= *ppdata
;
2072 files_struct
*fsp
= NULL
;
2073 uint32 security_info_sent
= 0;
2076 if(parameter_count
< 8) {
2077 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2081 if((fsp
= file_fsp(req
, SVAL(params
,0))) == NULL
) {
2082 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2086 if (!CAN_WRITE(fsp
->conn
)) {
2087 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2091 if(!lp_nt_acl_support(SNUM(conn
))) {
2095 security_info_sent
= IVAL(params
,4);
2097 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2098 fsp_str_dbg(fsp
), (unsigned int)security_info_sent
));
2100 if (data_count
== 0) {
2101 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2105 status
= set_sd(fsp
, (uint8
*)data
, data_count
, security_info_sent
);
2107 if (!NT_STATUS_IS_OK(status
)) {
2108 reply_nterror(req
, status
);
2113 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
2117 /****************************************************************************
2119 ****************************************************************************/
2121 static void call_nt_transact_ioctl(connection_struct
*conn
,
2122 struct smb_request
*req
,
2123 uint16
**ppsetup
, uint32 setup_count
,
2124 char **ppparams
, uint32 parameter_count
,
2125 char **ppdata
, uint32 data_count
,
2126 uint32 max_data_count
)
2134 char *out_data
= NULL
;
2135 uint32 out_data_len
= 0;
2136 char *pdata
= *ppdata
;
2137 TALLOC_CTX
*ctx
= talloc_tos();
2139 if (setup_count
!= 8) {
2140 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count
));
2141 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2145 function
= IVAL(*ppsetup
, 0);
2146 fidnum
= SVAL(*ppsetup
, 4);
2147 isFSctl
= CVAL(*ppsetup
, 6);
2148 compfilter
= CVAL(*ppsetup
, 7);
2150 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2151 function
, fidnum
, isFSctl
, compfilter
));
2153 fsp
=file_fsp(req
, fidnum
);
2156 * We don't really implement IOCTLs, especially on files.
2159 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2161 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2165 /* Has to be for an open file! */
2166 if (!check_fsp_open(conn
, req
, fsp
)) {
2170 SMB_PERFCOUNT_SET_IOCTL(&req
->pcd
, function
);
2173 * out_data might be allocated by the VFS module, but talloc should be
2174 * used, and should be cleaned up when the request ends.
2176 status
= SMB_VFS_FSCTL(fsp
,
2182 (uint8_t **)&out_data
,
2185 if (!NT_STATUS_IS_OK(status
)) {
2186 reply_nterror(req
, status
);
2188 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, out_data
, out_data_len
);
2193 #ifdef HAVE_SYS_QUOTAS
2194 /****************************************************************************
2195 Reply to get user quota
2196 ****************************************************************************/
2198 static void call_nt_transact_get_user_quota(connection_struct
*conn
,
2199 struct smb_request
*req
,
2203 uint32 parameter_count
,
2206 uint32 max_data_count
)
2208 NTSTATUS nt_status
= NT_STATUS_OK
;
2209 char *params
= *ppparams
;
2210 char *pdata
= *ppdata
;
2212 int data_len
=0,param_len
=0;
2215 files_struct
*fsp
= NULL
;
2219 bool start_enum
= True
;
2220 SMB_NTQUOTA_STRUCT qt
;
2221 SMB_NTQUOTA_LIST
*tmp_list
;
2222 SMB_NTQUOTA_HANDLE
*qt_handle
= NULL
;
2227 if (get_current_uid(conn
) != 0) {
2228 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2229 "[%s]\n", lp_servicename(SNUM(conn
)),
2230 conn
->session_info
->unix_info
->unix_name
));
2231 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2236 * Ensure minimum number of parameters sent.
2239 if (parameter_count
< 4) {
2240 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count
));
2241 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2245 /* maybe we can check the quota_fnum */
2246 fsp
= file_fsp(req
, SVAL(params
,0));
2247 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2248 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2249 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2253 /* the NULL pointer checking for fsp->fake_file_handle->pd
2254 * is done by CHECK_NTQUOTA_HANDLE_OK()
2256 qt_handle
= (SMB_NTQUOTA_HANDLE
*)fsp
->fake_file_handle
->private_data
;
2258 level
= SVAL(params
,2);
2260 /* unknown 12 bytes leading in params */
2263 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE
:
2264 /* seems that we should continue with the enum here --metze */
2266 if (qt_handle
->quota_list
!=NULL
&&
2267 qt_handle
->tmp_list
==NULL
) {
2270 free_ntquota_list(&(qt_handle
->quota_list
));
2272 /* Realloc the size of parameters and data we will return */
2274 params
= nttrans_realloc(ppparams
, param_len
);
2275 if(params
== NULL
) {
2276 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2281 SIVAL(params
,0,data_len
);
2288 case TRANSACT_GET_USER_QUOTA_LIST_START
:
2290 if (qt_handle
->quota_list
==NULL
&&
2291 qt_handle
->tmp_list
==NULL
) {
2295 if (start_enum
&& vfs_get_user_ntquota_list(fsp
,&(qt_handle
->quota_list
))!=0) {
2296 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2300 /* Realloc the size of parameters and data we will return */
2302 params
= nttrans_realloc(ppparams
, param_len
);
2303 if(params
== NULL
) {
2304 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2308 /* we should not trust the value in max_data_count*/
2309 max_data_count
= MIN(max_data_count
,2048);
2311 pdata
= nttrans_realloc(ppdata
, max_data_count
);/* should be max data count from client*/
2313 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2319 /* set params Size of returned Quota Data 4 bytes*/
2320 /* but set it later when we know it */
2322 /* for each entry push the data */
2325 qt_handle
->tmp_list
= qt_handle
->quota_list
;
2328 tmp_list
= qt_handle
->tmp_list
;
2330 for (;((tmp_list
!=NULL
)&&((qt_len
+40+SID_MAX_SIZE
)<max_data_count
));
2331 tmp_list
=tmp_list
->next
,entry
+=entry_len
,qt_len
+=entry_len
) {
2333 sid_len
= ndr_size_dom_sid(
2334 &tmp_list
->quotas
->sid
, 0);
2335 entry_len
= 40 + sid_len
;
2337 /* nextoffset entry 4 bytes */
2338 SIVAL(entry
,0,entry_len
);
2340 /* then the len of the SID 4 bytes */
2341 SIVAL(entry
,4,sid_len
);
2343 /* unknown data 8 bytes uint64_t */
2344 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2346 /* the used disk space 8 bytes uint64_t */
2347 SBIG_UINT(entry
,16,tmp_list
->quotas
->usedspace
);
2349 /* the soft quotas 8 bytes uint64_t */
2350 SBIG_UINT(entry
,24,tmp_list
->quotas
->softlim
);
2352 /* the hard quotas 8 bytes uint64_t */
2353 SBIG_UINT(entry
,32,tmp_list
->quotas
->hardlim
);
2355 /* and now the SID */
2356 sid_linearize(entry
+40, sid_len
, &tmp_list
->quotas
->sid
);
2359 qt_handle
->tmp_list
= tmp_list
;
2361 /* overwrite the offset of the last entry */
2362 SIVAL(entry
-entry_len
,0,0);
2364 data_len
= 4+qt_len
;
2365 /* overwrite the params quota_data_len */
2366 SIVAL(params
,0,data_len
);
2370 case TRANSACT_GET_USER_QUOTA_FOR_SID
:
2372 /* unknown 4 bytes IVAL(pdata,0) */
2374 if (data_count
< 8) {
2375 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count
,8));
2376 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2380 sid_len
= IVAL(pdata
,4);
2381 /* Ensure this is less than 1mb. */
2382 if (sid_len
> (1024*1024)) {
2383 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2387 if (data_count
< 8+sid_len
) {
2388 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count
,(unsigned long)(8+sid_len
)));
2389 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2393 data_len
= 4+40+sid_len
;
2395 if (max_data_count
< data_len
) {
2396 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2397 max_data_count
, data_len
));
2399 SIVAL(params
,0,data_len
);
2401 nt_status
= NT_STATUS_BUFFER_TOO_SMALL
;
2405 if (!sid_parse(pdata
+8,sid_len
,&sid
)) {
2406 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2410 if (vfs_get_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2413 * we have to return zero's in all fields
2414 * instead of returning an error here
2419 /* Realloc the size of parameters and data we will return */
2421 params
= nttrans_realloc(ppparams
, param_len
);
2422 if(params
== NULL
) {
2423 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2427 pdata
= nttrans_realloc(ppdata
, data_len
);
2429 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2435 /* set params Size of returned Quota Data 4 bytes*/
2436 SIVAL(params
,0,data_len
);
2438 /* nextoffset entry 4 bytes */
2441 /* then the len of the SID 4 bytes */
2442 SIVAL(entry
,4,sid_len
);
2444 /* unknown data 8 bytes uint64_t */
2445 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2447 /* the used disk space 8 bytes uint64_t */
2448 SBIG_UINT(entry
,16,qt
.usedspace
);
2450 /* the soft quotas 8 bytes uint64_t */
2451 SBIG_UINT(entry
,24,qt
.softlim
);
2453 /* the hard quotas 8 bytes uint64_t */
2454 SBIG_UINT(entry
,32,qt
.hardlim
);
2456 /* and now the SID */
2457 sid_linearize(entry
+40, sid_len
, &sid
);
2462 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp
->fnum
,level
));
2463 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2468 send_nt_replies(conn
, req
, nt_status
, params
, param_len
,
2472 /****************************************************************************
2473 Reply to set user quota
2474 ****************************************************************************/
2476 static void call_nt_transact_set_user_quota(connection_struct
*conn
,
2477 struct smb_request
*req
,
2481 uint32 parameter_count
,
2484 uint32 max_data_count
)
2486 char *params
= *ppparams
;
2487 char *pdata
= *ppdata
;
2488 int data_len
=0,param_len
=0;
2489 SMB_NTQUOTA_STRUCT qt
;
2492 files_struct
*fsp
= NULL
;
2497 if (get_current_uid(conn
) != 0) {
2498 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2499 "[%s]\n", lp_servicename(SNUM(conn
)),
2500 conn
->session_info
->unix_info
->unix_name
));
2501 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2506 * Ensure minimum number of parameters sent.
2509 if (parameter_count
< 2) {
2510 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count
));
2511 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2515 /* maybe we can check the quota_fnum */
2516 fsp
= file_fsp(req
, SVAL(params
,0));
2517 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2518 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2519 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2523 if (data_count
< 40) {
2524 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count
,40));
2525 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2529 /* offset to next quota record.
2530 * 4 bytes IVAL(pdata,0)
2535 sid_len
= IVAL(pdata
,4);
2537 if (data_count
< 40+sid_len
|| (40+sid_len
< sid_len
)) {
2538 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count
,(unsigned long)40+sid_len
));
2539 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2543 /* unknown 8 bytes in pdata
2544 * maybe its the change time in NTTIME
2547 /* the used space 8 bytes (uint64_t)*/
2548 qt
.usedspace
= BVAL(pdata
,16);
2550 /* the soft quotas 8 bytes (uint64_t)*/
2551 qt
.softlim
= BVAL(pdata
,24);
2553 /* the hard quotas 8 bytes (uint64_t)*/
2554 qt
.hardlim
= BVAL(pdata
,32);
2556 if (!sid_parse(pdata
+40,sid_len
,&sid
)) {
2557 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2561 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid
)));
2563 /* 44 unknown bytes left... */
2565 if (vfs_set_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2566 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2570 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
,
2573 #endif /* HAVE_SYS_QUOTAS */
2575 static void handle_nttrans(connection_struct
*conn
,
2576 struct trans_state
*state
,
2577 struct smb_request
*req
)
2579 if (get_Protocol() >= PROTOCOL_NT1
) {
2580 req
->flags2
|= 0x40; /* IS_LONG_NAME */
2581 SSVAL(discard_const_p(uint8_t, req
->inbuf
),smb_flg2
,req
->flags2
);
2585 SMB_PERFCOUNT_SET_SUBOP(&req
->pcd
, state
->call
);
2587 /* Now we must call the relevant NT_TRANS function */
2588 switch(state
->call
) {
2589 case NT_TRANSACT_CREATE
:
2591 START_PROFILE(NT_transact_create
);
2592 call_nt_transact_create(
2594 &state
->setup
, state
->setup_count
,
2595 &state
->param
, state
->total_param
,
2596 &state
->data
, state
->total_data
,
2597 state
->max_data_return
);
2598 END_PROFILE(NT_transact_create
);
2602 case NT_TRANSACT_IOCTL
:
2604 START_PROFILE(NT_transact_ioctl
);
2605 call_nt_transact_ioctl(
2607 &state
->setup
, state
->setup_count
,
2608 &state
->param
, state
->total_param
,
2609 &state
->data
, state
->total_data
,
2610 state
->max_data_return
);
2611 END_PROFILE(NT_transact_ioctl
);
2615 case NT_TRANSACT_SET_SECURITY_DESC
:
2617 START_PROFILE(NT_transact_set_security_desc
);
2618 call_nt_transact_set_security_desc(
2620 &state
->setup
, state
->setup_count
,
2621 &state
->param
, state
->total_param
,
2622 &state
->data
, state
->total_data
,
2623 state
->max_data_return
);
2624 END_PROFILE(NT_transact_set_security_desc
);
2628 case NT_TRANSACT_NOTIFY_CHANGE
:
2630 START_PROFILE(NT_transact_notify_change
);
2631 call_nt_transact_notify_change(
2633 &state
->setup
, state
->setup_count
,
2634 &state
->param
, state
->total_param
,
2635 &state
->data
, state
->total_data
,
2636 state
->max_data_return
,
2637 state
->max_param_return
);
2638 END_PROFILE(NT_transact_notify_change
);
2642 case NT_TRANSACT_RENAME
:
2644 START_PROFILE(NT_transact_rename
);
2645 call_nt_transact_rename(
2647 &state
->setup
, state
->setup_count
,
2648 &state
->param
, state
->total_param
,
2649 &state
->data
, state
->total_data
,
2650 state
->max_data_return
);
2651 END_PROFILE(NT_transact_rename
);
2655 case NT_TRANSACT_QUERY_SECURITY_DESC
:
2657 START_PROFILE(NT_transact_query_security_desc
);
2658 call_nt_transact_query_security_desc(
2660 &state
->setup
, state
->setup_count
,
2661 &state
->param
, state
->total_param
,
2662 &state
->data
, state
->total_data
,
2663 state
->max_data_return
);
2664 END_PROFILE(NT_transact_query_security_desc
);
2668 #ifdef HAVE_SYS_QUOTAS
2669 case NT_TRANSACT_GET_USER_QUOTA
:
2671 START_PROFILE(NT_transact_get_user_quota
);
2672 call_nt_transact_get_user_quota(
2674 &state
->setup
, state
->setup_count
,
2675 &state
->param
, state
->total_param
,
2676 &state
->data
, state
->total_data
,
2677 state
->max_data_return
);
2678 END_PROFILE(NT_transact_get_user_quota
);
2682 case NT_TRANSACT_SET_USER_QUOTA
:
2684 START_PROFILE(NT_transact_set_user_quota
);
2685 call_nt_transact_set_user_quota(
2687 &state
->setup
, state
->setup_count
,
2688 &state
->param
, state
->total_param
,
2689 &state
->data
, state
->total_data
,
2690 state
->max_data_return
);
2691 END_PROFILE(NT_transact_set_user_quota
);
2694 #endif /* HAVE_SYS_QUOTAS */
2697 /* Error in request */
2698 DEBUG(0,("handle_nttrans: Unknown request %d in "
2699 "nttrans call\n", state
->call
));
2700 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2706 /****************************************************************************
2707 Reply to a SMBNTtrans.
2708 ****************************************************************************/
2710 void reply_nttrans(struct smb_request
*req
)
2712 connection_struct
*conn
= req
->conn
;
2717 uint16 function_code
;
2719 struct trans_state
*state
;
2721 START_PROFILE(SMBnttrans
);
2723 if (req
->wct
< 19) {
2724 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2725 END_PROFILE(SMBnttrans
);
2729 pscnt
= IVAL(req
->vwv
+9, 1);
2730 psoff
= IVAL(req
->vwv
+11, 1);
2731 dscnt
= IVAL(req
->vwv
+13, 1);
2732 dsoff
= IVAL(req
->vwv
+15, 1);
2733 function_code
= SVAL(req
->vwv
+18, 0);
2735 if (IS_IPC(conn
) && (function_code
!= NT_TRANSACT_CREATE
)) {
2736 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2737 END_PROFILE(SMBnttrans
);
2741 result
= allow_new_trans(conn
->pending_trans
, req
->mid
);
2742 if (!NT_STATUS_IS_OK(result
)) {
2743 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result
)));
2744 reply_nterror(req
, result
);
2745 END_PROFILE(SMBnttrans
);
2749 if ((state
= talloc(conn
, struct trans_state
)) == NULL
) {
2750 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2751 END_PROFILE(SMBnttrans
);
2755 state
->cmd
= SMBnttrans
;
2757 state
->mid
= req
->mid
;
2758 state
->vuid
= req
->vuid
;
2759 state
->total_data
= IVAL(req
->vwv
+3, 1);
2761 state
->total_param
= IVAL(req
->vwv
+1, 1);
2762 state
->param
= NULL
;
2763 state
->max_data_return
= IVAL(req
->vwv
+7, 1);
2764 state
->max_param_return
= IVAL(req
->vwv
+5, 1);
2766 /* setup count is in *words* */
2767 state
->setup_count
= 2*CVAL(req
->vwv
+17, 1);
2768 state
->setup
= NULL
;
2769 state
->call
= function_code
;
2771 DEBUG(10, ("num_setup=%u, "
2772 "param_total=%u, this_param=%u, max_param=%u, "
2773 "data_total=%u, this_data=%u, max_data=%u, "
2774 "param_offset=%u, data_offset=%u\n",
2775 (unsigned)state
->setup_count
,
2776 (unsigned)state
->total_param
, (unsigned)pscnt
,
2777 (unsigned)state
->max_param_return
,
2778 (unsigned)state
->total_data
, (unsigned)dscnt
,
2779 (unsigned)state
->max_data_return
,
2780 (unsigned)psoff
, (unsigned)dsoff
));
2783 * All nttrans messages we handle have smb_wct == 19 +
2784 * state->setup_count. Ensure this is so as a sanity check.
2787 if(req
->wct
!= 19 + (state
->setup_count
/2)) {
2788 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2789 req
->wct
, 19 + (state
->setup_count
/2)));
2793 /* Don't allow more than 128mb for each value. */
2794 if ((state
->total_data
> (1024*1024*128)) ||
2795 (state
->total_param
> (1024*1024*128))) {
2796 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2797 END_PROFILE(SMBnttrans
);
2801 if ((dscnt
> state
->total_data
) || (pscnt
> state
->total_param
))
2804 if (state
->total_data
) {
2806 if (trans_oob(state
->total_data
, 0, dscnt
)
2807 || trans_oob(smb_len(req
->inbuf
), dsoff
, dscnt
)) {
2811 /* Can't use talloc here, the core routines do realloc on the
2812 * params and data. */
2813 if ((state
->data
= (char *)SMB_MALLOC(state
->total_data
)) == NULL
) {
2814 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2815 "bytes !\n", (unsigned int)state
->total_data
));
2817 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2818 END_PROFILE(SMBnttrans
);
2822 memcpy(state
->data
,smb_base(req
->inbuf
)+dsoff
,dscnt
);
2825 if (state
->total_param
) {
2827 if (trans_oob(state
->total_param
, 0, pscnt
)
2828 || trans_oob(smb_len(req
->inbuf
), psoff
, pscnt
)) {
2832 /* Can't use talloc here, the core routines do realloc on the
2833 * params and data. */
2834 if ((state
->param
= (char *)SMB_MALLOC(state
->total_param
)) == NULL
) {
2835 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2836 "bytes !\n", (unsigned int)state
->total_param
));
2837 SAFE_FREE(state
->data
);
2839 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2840 END_PROFILE(SMBnttrans
);
2844 memcpy(state
->param
,smb_base(req
->inbuf
)+psoff
,pscnt
);
2847 state
->received_data
= dscnt
;
2848 state
->received_param
= pscnt
;
2850 if(state
->setup_count
> 0) {
2851 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2852 state
->setup_count
));
2855 * No overflow possible here, state->setup_count is an
2856 * unsigned int, being filled by a single byte from
2857 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2858 * below is not necessary, it's here to clarify things. The
2859 * validity of req->vwv and req->wct has been checked in
2860 * init_smb_request already.
2862 if ((state
->setup_count
/2) + 19 > (unsigned int)req
->wct
) {
2866 state
->setup
= (uint16
*)TALLOC(state
, state
->setup_count
);
2867 if (state
->setup
== NULL
) {
2868 DEBUG(0,("reply_nttrans : Out of memory\n"));
2869 SAFE_FREE(state
->data
);
2870 SAFE_FREE(state
->param
);
2872 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2873 END_PROFILE(SMBnttrans
);
2877 memcpy(state
->setup
, req
->vwv
+19, state
->setup_count
);
2878 dump_data(10, (uint8
*)state
->setup
, state
->setup_count
);
2881 if ((state
->received_data
== state
->total_data
) &&
2882 (state
->received_param
== state
->total_param
)) {
2883 handle_nttrans(conn
, state
, req
);
2884 SAFE_FREE(state
->param
);
2885 SAFE_FREE(state
->data
);
2887 END_PROFILE(SMBnttrans
);
2891 DLIST_ADD(conn
->pending_trans
, state
);
2893 /* We need to send an interim response then receive the rest
2894 of the parameter/data bytes */
2895 reply_outbuf(req
, 0, 0);
2896 show_msg((char *)req
->outbuf
);
2897 END_PROFILE(SMBnttrans
);
2902 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2903 SAFE_FREE(state
->data
);
2904 SAFE_FREE(state
->param
);
2906 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2907 END_PROFILE(SMBnttrans
);
2911 /****************************************************************************
2912 Reply to a SMBnttranss
2913 ****************************************************************************/
2915 void reply_nttranss(struct smb_request
*req
)
2917 connection_struct
*conn
= req
->conn
;
2918 uint32_t pcnt
,poff
,dcnt
,doff
,pdisp
,ddisp
;
2919 struct trans_state
*state
;
2921 START_PROFILE(SMBnttranss
);
2923 show_msg((const char *)req
->inbuf
);
2925 if (req
->wct
< 18) {
2926 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2927 END_PROFILE(SMBnttranss
);
2931 for (state
= conn
->pending_trans
; state
!= NULL
;
2932 state
= state
->next
) {
2933 if (state
->mid
== req
->mid
) {
2938 if ((state
== NULL
) || (state
->cmd
!= SMBnttrans
)) {
2939 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2940 END_PROFILE(SMBnttranss
);
2944 /* Revise state->total_param and state->total_data in case they have
2945 changed downwards */
2946 if (IVAL(req
->vwv
+1, 1) < state
->total_param
) {
2947 state
->total_param
= IVAL(req
->vwv
+1, 1);
2949 if (IVAL(req
->vwv
+3, 1) < state
->total_data
) {
2950 state
->total_data
= IVAL(req
->vwv
+3, 1);
2953 pcnt
= IVAL(req
->vwv
+5, 1);
2954 poff
= IVAL(req
->vwv
+7, 1);
2955 pdisp
= IVAL(req
->vwv
+9, 1);
2957 dcnt
= IVAL(req
->vwv
+11, 1);
2958 doff
= IVAL(req
->vwv
+13, 1);
2959 ddisp
= IVAL(req
->vwv
+15, 1);
2961 state
->received_param
+= pcnt
;
2962 state
->received_data
+= dcnt
;
2964 if ((state
->received_data
> state
->total_data
) ||
2965 (state
->received_param
> state
->total_param
))
2969 if (trans_oob(state
->total_param
, pdisp
, pcnt
)
2970 || trans_oob(smb_len(req
->inbuf
), poff
, pcnt
)) {
2973 memcpy(state
->param
+pdisp
, smb_base(req
->inbuf
)+poff
,pcnt
);
2977 if (trans_oob(state
->total_data
, ddisp
, dcnt
)
2978 || trans_oob(smb_len(req
->inbuf
), doff
, dcnt
)) {
2981 memcpy(state
->data
+ddisp
, smb_base(req
->inbuf
)+doff
,dcnt
);
2984 if ((state
->received_param
< state
->total_param
) ||
2985 (state
->received_data
< state
->total_data
)) {
2986 END_PROFILE(SMBnttranss
);
2990 handle_nttrans(conn
, state
, req
);
2992 DLIST_REMOVE(conn
->pending_trans
, state
);
2993 SAFE_FREE(state
->data
);
2994 SAFE_FREE(state
->param
);
2996 END_PROFILE(SMBnttranss
);
3001 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3002 DLIST_REMOVE(conn
->pending_trans
, state
);
3003 SAFE_FREE(state
->data
);
3004 SAFE_FREE(state
->param
);
3006 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3007 END_PROFILE(SMBnttranss
);