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 definately
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 p
= (char *)req
->outbuf
+ smb_vwv2
;
359 SIVAL(p
,0,FILE_WAS_OPENED
);
362 SIVAL(p
,0,FILE_ATTRIBUTE_NORMAL
); /* File Attributes. */
365 SSVAL(p
,0,FILE_TYPE_MESSAGE_MODE_PIPE
);
367 SSVAL(p
,2, 0x5FF); /* ? */
370 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
372 SIVAL(p
,0,FILE_GENERIC_ALL
);
374 * For pipes W2K3 seems to return
376 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
378 SIVAL(p
,4,(FILE_GENERIC_READ
|FILE_GENERIC_WRITE
)&~FILE_APPEND_DATA
);
381 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname
));
386 struct case_semantics_state
{
387 connection_struct
*conn
;
390 bool short_case_preserve
;
393 /****************************************************************************
394 Restore case semantics.
395 ****************************************************************************/
397 static int restore_case_semantics(struct case_semantics_state
*state
)
399 state
->conn
->case_sensitive
= state
->case_sensitive
;
400 state
->conn
->case_preserve
= state
->case_preserve
;
401 state
->conn
->short_case_preserve
= state
->short_case_preserve
;
405 /****************************************************************************
407 ****************************************************************************/
409 static struct case_semantics_state
*set_posix_case_semantics(TALLOC_CTX
*mem_ctx
,
410 connection_struct
*conn
)
412 struct case_semantics_state
*result
;
414 if (!(result
= talloc(mem_ctx
, struct case_semantics_state
))) {
419 result
->case_sensitive
= conn
->case_sensitive
;
420 result
->case_preserve
= conn
->case_preserve
;
421 result
->short_case_preserve
= conn
->short_case_preserve
;
424 conn
->case_sensitive
= True
;
425 conn
->case_preserve
= True
;
426 conn
->short_case_preserve
= True
;
428 talloc_set_destructor(result
, restore_case_semantics
);
433 /****************************************************************************
434 Reply to an NT create and X call.
435 ****************************************************************************/
437 void reply_ntcreate_and_X(struct smb_request
*req
)
439 connection_struct
*conn
= req
->conn
;
440 struct smb_filename
*smb_fname
= NULL
;
444 uint32 file_attributes
;
446 uint32 create_disposition
;
447 uint32 create_options
;
449 uint64_t allocation_size
;
450 /* Breakout the oplock request bits so we can set the
451 reply bits separately. */
453 SMB_OFF_T file_len
= 0;
455 files_struct
*fsp
= NULL
;
457 struct timespec create_timespec
;
458 struct timespec c_timespec
;
459 struct timespec a_timespec
;
460 struct timespec m_timespec
;
461 struct timespec write_time_ts
;
464 uint8_t oplock_granted
= NO_OPLOCK_RETURN
;
465 struct case_semantics_state
*case_state
= NULL
;
466 TALLOC_CTX
*ctx
= talloc_tos();
468 START_PROFILE(SMBntcreateX
);
471 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
475 flags
= IVAL(req
->vwv
+3, 1);
476 access_mask
= IVAL(req
->vwv
+7, 1);
477 file_attributes
= IVAL(req
->vwv
+13, 1);
478 share_access
= IVAL(req
->vwv
+15, 1);
479 create_disposition
= IVAL(req
->vwv
+17, 1);
480 create_options
= IVAL(req
->vwv
+19, 1);
481 root_dir_fid
= (uint16
)IVAL(req
->vwv
+5, 1);
483 allocation_size
= BVAL(req
->vwv
+9, 1);
485 srvstr_get_path_req(ctx
, req
, &fname
, (const char *)req
->buf
,
486 STR_TERMINATE
, &status
);
488 if (!NT_STATUS_IS_OK(status
)) {
489 reply_nterror(req
, status
);
493 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
494 "file_attributes = 0x%x, share_access = 0x%x, "
495 "create_disposition = 0x%x create_options = 0x%x "
496 "root_dir_fid = 0x%x, fname = %s\n",
498 (unsigned int)access_mask
,
499 (unsigned int)file_attributes
,
500 (unsigned int)share_access
,
501 (unsigned int)create_disposition
,
502 (unsigned int)create_options
,
503 (unsigned int)root_dir_fid
,
507 * we need to remove ignored bits when they come directly from the client
508 * because we reuse some of them for internal stuff
510 create_options
&= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK
;
513 * If it's an IPC, use the pipe handler.
517 if (lp_nt_pipe_support()) {
518 do_ntcreate_pipe_open(conn
, req
);
521 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
525 oplock_request
= (flags
& REQUEST_OPLOCK
) ? EXCLUSIVE_OPLOCK
: 0;
526 if (oplock_request
) {
527 oplock_request
|= (flags
& REQUEST_BATCH_OPLOCK
)
531 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
532 case_state
= set_posix_case_semantics(ctx
, conn
);
534 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
539 status
= filename_convert(ctx
,
541 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
547 TALLOC_FREE(case_state
);
549 if (!NT_STATUS_IS_OK(status
)) {
550 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
552 NT_STATUS_PATH_NOT_COVERED
,
556 reply_nterror(req
, status
);
561 * Bug #6898 - clients using Windows opens should
562 * never be able to set this attribute into the
565 file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
567 status
= SMB_VFS_CREATE_FILE(
570 root_dir_fid
, /* root_dir_fid */
571 smb_fname
, /* fname */
572 access_mask
, /* access_mask */
573 share_access
, /* share_access */
574 create_disposition
, /* create_disposition*/
575 create_options
, /* create_options */
576 file_attributes
, /* file_attributes */
577 oplock_request
, /* oplock_request */
578 allocation_size
, /* allocation_size */
579 0, /* private_flags */
585 if (!NT_STATUS_IS_OK(status
)) {
586 if (open_was_deferred(req
->sconn
, req
->mid
)) {
587 /* We have re-scheduled this call, no error. */
590 reply_openerror(req
, status
);
594 /* Ensure we're pointing at the correct stat struct. */
595 TALLOC_FREE(smb_fname
);
596 smb_fname
= fsp
->fsp_name
;
599 * If the caller set the extended oplock request bit
600 * and we granted one (by whatever means) - set the
601 * correct bit for extended oplock reply.
604 if (oplock_request
&&
605 (lp_fake_oplocks(SNUM(conn
))
606 || EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
))) {
609 * Exclusive oplock granted
612 if (flags
& REQUEST_BATCH_OPLOCK
) {
613 oplock_granted
= BATCH_OPLOCK_RETURN
;
615 oplock_granted
= EXCLUSIVE_OPLOCK_RETURN
;
617 } else if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
618 oplock_granted
= LEVEL_II_OPLOCK_RETURN
;
620 oplock_granted
= NO_OPLOCK_RETURN
;
623 file_len
= smb_fname
->st
.st_ex_size
;
625 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
626 /* This is very strange. We
627 * return 50 words, but only set
628 * the wcnt to 42 ? It's definately
629 * what happens on the wire....
631 reply_outbuf(req
, 50, 0);
632 SCVAL(req
->outbuf
,smb_wct
,42);
634 reply_outbuf(req
, 34, 0);
637 p
= (char *)req
->outbuf
+ smb_vwv2
;
639 SCVAL(p
, 0, oplock_granted
);
642 SSVAL(p
,0,fsp
->fnum
);
644 if ((create_disposition
== FILE_SUPERSEDE
)
645 && (info
== FILE_WAS_OVERWRITTEN
)) {
646 SIVAL(p
,0,FILE_WAS_SUPERSEDED
);
652 fattr
= dos_mode(conn
, smb_fname
);
654 fattr
= FILE_ATTRIBUTE_NORMAL
;
657 /* Deal with other possible opens having a modified
659 ZERO_STRUCT(write_time_ts
);
660 get_file_infos(fsp
->file_id
, 0, NULL
, &write_time_ts
);
661 if (!null_timespec(write_time_ts
)) {
662 update_stat_ex_mtime(&smb_fname
->st
, write_time_ts
);
666 create_timespec
= get_create_timespec(conn
, fsp
, smb_fname
);
667 a_timespec
= smb_fname
->st
.st_ex_atime
;
668 m_timespec
= smb_fname
->st
.st_ex_mtime
;
669 c_timespec
= get_change_timespec(conn
, fsp
, smb_fname
);
671 if (lp_dos_filetime_resolution(SNUM(conn
))) {
672 dos_filetime_timespec(&create_timespec
);
673 dos_filetime_timespec(&a_timespec
);
674 dos_filetime_timespec(&m_timespec
);
675 dos_filetime_timespec(&c_timespec
);
678 put_long_date_timespec(conn
->ts_res
, p
, create_timespec
); /* create time. */
680 put_long_date_timespec(conn
->ts_res
, p
, a_timespec
); /* access time */
682 put_long_date_timespec(conn
->ts_res
, p
, m_timespec
); /* write time */
684 put_long_date_timespec(conn
->ts_res
, p
, c_timespec
); /* change time */
686 SIVAL(p
,0,fattr
); /* File Attributes. */
688 SOFF_T(p
, 0, SMB_VFS_GET_ALLOC_SIZE(conn
,fsp
,&smb_fname
->st
));
690 SOFF_T(p
,0,file_len
);
692 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
693 uint16_t file_status
= (NO_EAS
|NO_SUBSTREAMS
|NO_REPARSETAG
);
694 size_t num_names
= 0;
695 unsigned int num_streams
= 0;
696 struct stream_struct
*streams
= NULL
;
698 /* Do we have any EA's ? */
699 status
= get_ea_names_from_file(ctx
, conn
, fsp
,
700 smb_fname
->base_name
, NULL
, &num_names
);
701 if (NT_STATUS_IS_OK(status
) && num_names
) {
702 file_status
&= ~NO_EAS
;
704 status
= vfs_streaminfo(conn
, NULL
, smb_fname
->base_name
, ctx
,
705 &num_streams
, &streams
);
706 /* There is always one stream, ::$DATA. */
707 if (NT_STATUS_IS_OK(status
) && num_streams
> 1) {
708 file_status
&= ~NO_SUBSTREAMS
;
710 TALLOC_FREE(streams
);
711 SSVAL(p
,2,file_status
);
714 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
716 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
719 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: fnum = %d, open name = %s\n",
729 fsp
->fnum
, smb_fname_str_dbg(smb_fname
)));
733 END_PROFILE(SMBntcreateX
);
737 /****************************************************************************
738 Reply to a NT_TRANSACT_CREATE call to open a pipe.
739 ****************************************************************************/
741 static void do_nt_transact_create_pipe(connection_struct
*conn
,
742 struct smb_request
*req
,
743 uint16
**ppsetup
, uint32 setup_count
,
744 char **ppparams
, uint32 parameter_count
,
745 char **ppdata
, uint32 data_count
)
748 char *params
= *ppparams
;
754 TALLOC_CTX
*ctx
= talloc_tos();
757 * Ensure minimum number of parameters sent.
760 if(parameter_count
< 54) {
761 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
762 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
766 flags
= IVAL(params
,0);
768 srvstr_get_path(ctx
, params
, req
->flags2
, &fname
, params
+53,
769 parameter_count
-53, STR_TERMINATE
,
771 if (!NT_STATUS_IS_OK(status
)) {
772 reply_nterror(req
, status
);
776 nt_open_pipe(fname
, conn
, req
, &pnum
);
783 /* Realloc the size of parameters and data we will return */
784 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
785 /* Extended response is 32 more byyes. */
790 params
= nttrans_realloc(ppparams
, param_len
);
792 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
797 SCVAL(p
,0,NO_OPLOCK_RETURN
);
802 SIVAL(p
,0,FILE_WAS_OPENED
);
806 SIVAL(p
,0,FILE_ATTRIBUTE_NORMAL
); /* File Attributes. */
809 SSVAL(p
,0,FILE_TYPE_MESSAGE_MODE_PIPE
);
811 SSVAL(p
,2, 0x5FF); /* ? */
814 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
816 SIVAL(p
,0,FILE_GENERIC_ALL
);
818 * For pipes W2K3 seems to return
820 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
822 SIVAL(p
,4,(FILE_GENERIC_READ
|FILE_GENERIC_WRITE
)&~FILE_APPEND_DATA
);
825 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname
));
827 /* Send the required number of replies */
828 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
833 /****************************************************************************
834 Internal fn to set security descriptors.
835 ****************************************************************************/
837 NTSTATUS
set_sd(files_struct
*fsp
, uint8_t *data
, uint32_t sd_len
,
838 uint32_t security_info_sent
)
840 struct security_descriptor
*psd
= NULL
;
844 return NT_STATUS_INVALID_PARAMETER
;
847 if (!CAN_WRITE(fsp
->conn
)) {
848 return NT_STATUS_ACCESS_DENIED
;
851 if (!lp_nt_acl_support(SNUM(fsp
->conn
))) {
855 status
= unmarshall_sec_desc(talloc_tos(), data
, sd_len
, &psd
);
857 if (!NT_STATUS_IS_OK(status
)) {
861 if (psd
->owner_sid
== NULL
) {
862 security_info_sent
&= ~SECINFO_OWNER
;
864 if (psd
->group_sid
== NULL
) {
865 security_info_sent
&= ~SECINFO_GROUP
;
868 /* Ensure we have at least one thing set. */
869 if ((security_info_sent
& (SECINFO_OWNER
|SECINFO_GROUP
|SECINFO_DACL
|SECINFO_SACL
)) == 0) {
870 if (security_info_sent
& SECINFO_LABEL
) {
871 /* Only consider SECINFO_LABEL if no other
872 bits are set. Just like W2K3 we don't
876 return NT_STATUS_INVALID_PARAMETER
;
879 /* Ensure we have the rights to do this. */
880 if (security_info_sent
& SECINFO_OWNER
) {
881 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
882 return NT_STATUS_ACCESS_DENIED
;
886 if (security_info_sent
& SECINFO_GROUP
) {
887 if (!(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
888 return NT_STATUS_ACCESS_DENIED
;
892 if (security_info_sent
& SECINFO_DACL
) {
893 if (!(fsp
->access_mask
& SEC_STD_WRITE_DAC
)) {
894 return NT_STATUS_ACCESS_DENIED
;
896 /* Convert all the generic bits. */
898 security_acl_map_generic(psd
->dacl
, &file_generic_mapping
);
902 if (security_info_sent
& SECINFO_SACL
) {
903 if (!(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
904 return NT_STATUS_ACCESS_DENIED
;
906 /* Convert all the generic bits. */
908 security_acl_map_generic(psd
->sacl
, &file_generic_mapping
);
912 if (DEBUGLEVEL
>= 10) {
913 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp
)));
914 NDR_PRINT_DEBUG(security_descriptor
, psd
);
917 status
= SMB_VFS_FSET_NT_ACL(fsp
, security_info_sent
, psd
);
924 /****************************************************************************
925 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
926 ****************************************************************************/
928 struct ea_list
*read_nttrans_ea_list(TALLOC_CTX
*ctx
, const char *pdata
, size_t data_size
)
930 struct ea_list
*ea_list_head
= NULL
;
937 while (offset
+ 4 <= data_size
) {
938 size_t next_offset
= IVAL(pdata
,offset
);
939 struct ea_list
*eal
= read_ea_list_entry(ctx
, pdata
+ offset
+ 4, data_size
- offset
- 4, NULL
);
945 DLIST_ADD_END(ea_list_head
, eal
, struct ea_list
*);
946 if (next_offset
== 0) {
949 offset
+= next_offset
;
955 /****************************************************************************
956 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
957 ****************************************************************************/
959 static void call_nt_transact_create(connection_struct
*conn
,
960 struct smb_request
*req
,
961 uint16
**ppsetup
, uint32 setup_count
,
962 char **ppparams
, uint32 parameter_count
,
963 char **ppdata
, uint32 data_count
,
964 uint32 max_data_count
)
966 struct smb_filename
*smb_fname
= NULL
;
968 char *params
= *ppparams
;
969 char *data
= *ppdata
;
970 /* Breakout the oplock request bits so we can set the reply bits separately. */
972 SMB_OFF_T file_len
= 0;
974 files_struct
*fsp
= NULL
;
978 uint32 file_attributes
;
980 uint32 create_disposition
;
981 uint32 create_options
;
983 struct security_descriptor
*sd
= NULL
;
986 struct timespec create_timespec
;
987 struct timespec c_timespec
;
988 struct timespec a_timespec
;
989 struct timespec m_timespec
;
990 struct timespec write_time_ts
;
991 struct ea_list
*ea_list
= NULL
;
994 uint64_t allocation_size
;
996 uint8_t oplock_granted
;
997 struct case_semantics_state
*case_state
= NULL
;
998 TALLOC_CTX
*ctx
= talloc_tos();
1000 DEBUG(5,("call_nt_transact_create\n"));
1003 * If it's an IPC, use the pipe handler.
1007 if (lp_nt_pipe_support()) {
1008 do_nt_transact_create_pipe(
1010 ppsetup
, setup_count
,
1011 ppparams
, parameter_count
,
1012 ppdata
, data_count
);
1015 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
1020 * Ensure minimum number of parameters sent.
1023 if(parameter_count
< 54) {
1024 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
1025 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1029 flags
= IVAL(params
,0);
1030 access_mask
= IVAL(params
,8);
1031 file_attributes
= IVAL(params
,20);
1032 share_access
= IVAL(params
,24);
1033 create_disposition
= IVAL(params
,28);
1034 create_options
= IVAL(params
,32);
1035 sd_len
= IVAL(params
,36);
1036 ea_len
= IVAL(params
,40);
1037 root_dir_fid
= (uint16
)IVAL(params
,4);
1038 allocation_size
= BVAL(params
,12);
1041 * we need to remove ignored bits when they come directly from the client
1042 * because we reuse some of them for internal stuff
1044 create_options
&= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK
;
1046 /* Ensure the data_len is correct for the sd and ea values given. */
1047 if ((ea_len
+ sd_len
> data_count
)
1048 || (ea_len
> data_count
) || (sd_len
> data_count
)
1049 || (ea_len
+ sd_len
< ea_len
) || (ea_len
+ sd_len
< sd_len
)) {
1050 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1051 "%u, data_count = %u\n", (unsigned int)ea_len
,
1052 (unsigned int)sd_len
, (unsigned int)data_count
));
1053 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1058 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1061 status
= unmarshall_sec_desc(ctx
, (uint8_t *)data
, sd_len
,
1063 if (!NT_STATUS_IS_OK(status
)) {
1064 DEBUG(10, ("call_nt_transact_create: "
1065 "unmarshall_sec_desc failed: %s\n",
1066 nt_errstr(status
)));
1067 reply_nterror(req
, status
);
1073 if (!lp_ea_support(SNUM(conn
))) {
1074 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1075 "EA's not supported.\n",
1076 (unsigned int)ea_len
));
1077 reply_nterror(req
, NT_STATUS_EAS_NOT_SUPPORTED
);
1082 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1083 "too small (should be more than 10)\n",
1084 (unsigned int)ea_len
));
1085 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1089 /* We have already checked that ea_len <= data_count here. */
1090 ea_list
= read_nttrans_ea_list(talloc_tos(), data
+ sd_len
,
1092 if (ea_list
== NULL
) {
1093 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1098 srvstr_get_path(ctx
, params
, req
->flags2
, &fname
,
1099 params
+53, parameter_count
-53,
1100 STR_TERMINATE
, &status
);
1101 if (!NT_STATUS_IS_OK(status
)) {
1102 reply_nterror(req
, status
);
1106 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1107 case_state
= set_posix_case_semantics(ctx
, conn
);
1109 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1114 status
= filename_convert(ctx
,
1116 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1122 TALLOC_FREE(case_state
);
1124 if (!NT_STATUS_IS_OK(status
)) {
1125 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
1126 reply_botherror(req
,
1127 NT_STATUS_PATH_NOT_COVERED
,
1128 ERRSRV
, ERRbadpath
);
1131 reply_nterror(req
, status
);
1135 oplock_request
= (flags
& REQUEST_OPLOCK
) ? EXCLUSIVE_OPLOCK
: 0;
1136 if (oplock_request
) {
1137 oplock_request
|= (flags
& REQUEST_BATCH_OPLOCK
)
1142 * Bug #6898 - clients using Windows opens should
1143 * never be able to set this attribute into the
1146 file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
1148 status
= SMB_VFS_CREATE_FILE(
1151 root_dir_fid
, /* root_dir_fid */
1152 smb_fname
, /* fname */
1153 access_mask
, /* access_mask */
1154 share_access
, /* share_access */
1155 create_disposition
, /* create_disposition*/
1156 create_options
, /* create_options */
1157 file_attributes
, /* file_attributes */
1158 oplock_request
, /* oplock_request */
1159 allocation_size
, /* allocation_size */
1160 0, /* private_flags */
1162 ea_list
, /* ea_list */
1166 if(!NT_STATUS_IS_OK(status
)) {
1167 if (open_was_deferred(req
->sconn
, req
->mid
)) {
1168 /* We have re-scheduled this call, no error. */
1171 reply_openerror(req
, status
);
1175 /* Ensure we're pointing at the correct stat struct. */
1176 TALLOC_FREE(smb_fname
);
1177 smb_fname
= fsp
->fsp_name
;
1180 * If the caller set the extended oplock request bit
1181 * and we granted one (by whatever means) - set the
1182 * correct bit for extended oplock reply.
1185 if (oplock_request
&&
1186 (lp_fake_oplocks(SNUM(conn
))
1187 || EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
))) {
1190 * Exclusive oplock granted
1193 if (flags
& REQUEST_BATCH_OPLOCK
) {
1194 oplock_granted
= BATCH_OPLOCK_RETURN
;
1196 oplock_granted
= EXCLUSIVE_OPLOCK_RETURN
;
1198 } else if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
1199 oplock_granted
= LEVEL_II_OPLOCK_RETURN
;
1201 oplock_granted
= NO_OPLOCK_RETURN
;
1204 file_len
= smb_fname
->st
.st_ex_size
;
1206 /* Realloc the size of parameters and data we will return */
1207 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1208 /* Extended response is 32 more byyes. */
1213 params
= nttrans_realloc(ppparams
, param_len
);
1214 if(params
== NULL
) {
1215 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1220 SCVAL(p
, 0, oplock_granted
);
1223 SSVAL(p
,0,fsp
->fnum
);
1225 if ((create_disposition
== FILE_SUPERSEDE
)
1226 && (info
== FILE_WAS_OVERWRITTEN
)) {
1227 SIVAL(p
,0,FILE_WAS_SUPERSEDED
);
1233 fattr
= dos_mode(conn
, smb_fname
);
1235 fattr
= FILE_ATTRIBUTE_NORMAL
;
1238 /* Deal with other possible opens having a modified
1240 ZERO_STRUCT(write_time_ts
);
1241 get_file_infos(fsp
->file_id
, 0, NULL
, &write_time_ts
);
1242 if (!null_timespec(write_time_ts
)) {
1243 update_stat_ex_mtime(&smb_fname
->st
, write_time_ts
);
1247 create_timespec
= get_create_timespec(conn
, fsp
, smb_fname
);
1248 a_timespec
= smb_fname
->st
.st_ex_atime
;
1249 m_timespec
= smb_fname
->st
.st_ex_mtime
;
1250 c_timespec
= get_change_timespec(conn
, fsp
, smb_fname
);
1252 if (lp_dos_filetime_resolution(SNUM(conn
))) {
1253 dos_filetime_timespec(&create_timespec
);
1254 dos_filetime_timespec(&a_timespec
);
1255 dos_filetime_timespec(&m_timespec
);
1256 dos_filetime_timespec(&c_timespec
);
1259 put_long_date_timespec(conn
->ts_res
, p
, create_timespec
); /* create time. */
1261 put_long_date_timespec(conn
->ts_res
, p
, a_timespec
); /* access time */
1263 put_long_date_timespec(conn
->ts_res
, p
, m_timespec
); /* write time */
1265 put_long_date_timespec(conn
->ts_res
, p
, c_timespec
); /* change time */
1267 SIVAL(p
,0,fattr
); /* File Attributes. */
1269 SOFF_T(p
, 0, SMB_VFS_GET_ALLOC_SIZE(conn
, fsp
, &smb_fname
->st
));
1271 SOFF_T(p
,0,file_len
);
1273 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1274 uint16_t file_status
= (NO_EAS
|NO_SUBSTREAMS
|NO_REPARSETAG
);
1275 size_t num_names
= 0;
1276 unsigned int num_streams
= 0;
1277 struct stream_struct
*streams
= NULL
;
1279 /* Do we have any EA's ? */
1280 status
= get_ea_names_from_file(ctx
, conn
, fsp
,
1281 smb_fname
->base_name
, NULL
, &num_names
);
1282 if (NT_STATUS_IS_OK(status
) && num_names
) {
1283 file_status
&= ~NO_EAS
;
1285 status
= vfs_streaminfo(conn
, NULL
, smb_fname
->base_name
, ctx
,
1286 &num_streams
, &streams
);
1287 /* There is always one stream, ::$DATA. */
1288 if (NT_STATUS_IS_OK(status
) && num_streams
> 1) {
1289 file_status
&= ~NO_SUBSTREAMS
;
1291 TALLOC_FREE(streams
);
1292 SSVAL(p
,2,file_status
);
1295 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
1297 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1300 if (fsp
->is_directory
||
1301 can_write_to_file(conn
, smb_fname
)) {
1302 perms
= FILE_GENERIC_ALL
;
1304 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
1309 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1310 smb_fname_str_dbg(smb_fname
)));
1312 /* Send the required number of replies */
1313 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
1318 /****************************************************************************
1319 Reply to a NT CANCEL request.
1320 conn POINTER CAN BE NULL HERE !
1321 ****************************************************************************/
1323 void reply_ntcancel(struct smb_request
*req
)
1326 * Go through and cancel any pending change notifies.
1329 START_PROFILE(SMBntcancel
);
1330 srv_cancel_sign_response(req
->sconn
);
1331 remove_pending_change_notify_requests_by_mid(req
->sconn
, req
->mid
);
1332 remove_pending_lock_requests_by_mid_smb1(req
->sconn
, req
->mid
);
1334 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1335 (unsigned long long)req
->mid
));
1337 END_PROFILE(SMBntcancel
);
1341 /****************************************************************************
1343 ****************************************************************************/
1345 static NTSTATUS
copy_internals(TALLOC_CTX
*ctx
,
1346 connection_struct
*conn
,
1347 struct smb_request
*req
,
1348 struct smb_filename
*smb_fname_src
,
1349 struct smb_filename
*smb_fname_dst
,
1352 files_struct
*fsp1
,*fsp2
;
1356 NTSTATUS status
= NT_STATUS_OK
;
1359 if (!CAN_WRITE(conn
)) {
1360 status
= NT_STATUS_MEDIA_WRITE_PROTECTED
;
1364 /* Source must already exist. */
1365 if (!VALID_STAT(smb_fname_src
->st
)) {
1366 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1370 /* Ensure attributes match. */
1371 fattr
= dos_mode(conn
, smb_fname_src
);
1372 if ((fattr
& ~attrs
) & (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) {
1373 status
= NT_STATUS_NO_SUCH_FILE
;
1377 /* Disallow if dst file already exists. */
1378 if (VALID_STAT(smb_fname_dst
->st
)) {
1379 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
1383 /* No links from a directory. */
1384 if (S_ISDIR(smb_fname_src
->st
.st_ex_mode
)) {
1385 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
1389 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1390 smb_fname_str_dbg(smb_fname_src
),
1391 smb_fname_str_dbg(smb_fname_dst
)));
1393 status
= SMB_VFS_CREATE_FILE(
1396 0, /* root_dir_fid */
1397 smb_fname_src
, /* fname */
1398 FILE_READ_DATA
|FILE_READ_ATTRIBUTES
|
1399 FILE_READ_EA
, /* access_mask */
1400 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1402 FILE_OPEN
, /* create_disposition*/
1403 0, /* create_options */
1404 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
1405 NO_OPLOCK
, /* oplock_request */
1406 0, /* allocation_size */
1407 0, /* private_flags */
1413 if (!NT_STATUS_IS_OK(status
)) {
1417 status
= SMB_VFS_CREATE_FILE(
1420 0, /* root_dir_fid */
1421 smb_fname_dst
, /* fname */
1422 FILE_WRITE_DATA
|FILE_WRITE_ATTRIBUTES
|
1423 FILE_WRITE_EA
, /* access_mask */
1424 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
1426 FILE_CREATE
, /* create_disposition*/
1427 0, /* create_options */
1428 fattr
, /* file_attributes */
1429 NO_OPLOCK
, /* oplock_request */
1430 0, /* allocation_size */
1431 0, /* private_flags */
1437 if (!NT_STATUS_IS_OK(status
)) {
1438 close_file(NULL
, fsp1
, ERROR_CLOSE
);
1442 if (smb_fname_src
->st
.st_ex_size
) {
1443 ret
= vfs_transfer_file(fsp1
, fsp2
, smb_fname_src
->st
.st_ex_size
);
1447 * As we are opening fsp1 read-only we only expect
1448 * an error on close on fsp2 if we are out of space.
1449 * Thus we don't look at the error return from the
1452 close_file(NULL
, fsp1
, NORMAL_CLOSE
);
1454 /* Ensure the modtime is set correctly on the destination file. */
1455 set_close_write_time(fsp2
, smb_fname_src
->st
.st_ex_mtime
);
1457 status
= close_file(NULL
, fsp2
, NORMAL_CLOSE
);
1459 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1460 creates the file. This isn't the correct thing to do in the copy
1462 if (!parent_dirname(talloc_tos(), smb_fname_dst
->base_name
, &parent
,
1464 status
= NT_STATUS_NO_MEMORY
;
1467 file_set_dosmode(conn
, smb_fname_dst
, fattr
, parent
, false);
1468 TALLOC_FREE(parent
);
1470 if (ret
< (SMB_OFF_T
)smb_fname_src
->st
.st_ex_size
) {
1471 status
= NT_STATUS_DISK_FULL
;
1475 if (!NT_STATUS_IS_OK(status
)) {
1476 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1477 nt_errstr(status
), smb_fname_str_dbg(smb_fname_src
),
1478 smb_fname_str_dbg(smb_fname_dst
)));
1484 /****************************************************************************
1485 Reply to a NT rename request.
1486 ****************************************************************************/
1488 void reply_ntrename(struct smb_request
*req
)
1490 connection_struct
*conn
= req
->conn
;
1491 struct smb_filename
*smb_fname_old
= NULL
;
1492 struct smb_filename
*smb_fname_new
= NULL
;
1493 char *oldname
= NULL
;
1494 char *newname
= NULL
;
1497 bool src_has_wcard
= False
;
1498 bool dest_has_wcard
= False
;
1500 uint32_t ucf_flags_src
= 0;
1501 uint32_t ucf_flags_dst
= 0;
1503 TALLOC_CTX
*ctx
= talloc_tos();
1504 bool stream_rename
= false;
1506 START_PROFILE(SMBntrename
);
1509 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1513 attrs
= SVAL(req
->vwv
+0, 0);
1514 rename_type
= SVAL(req
->vwv
+1, 0);
1516 p
= (const char *)req
->buf
+ 1;
1517 p
+= srvstr_get_path_req_wcard(ctx
, req
, &oldname
, p
, STR_TERMINATE
,
1518 &status
, &src_has_wcard
);
1519 if (!NT_STATUS_IS_OK(status
)) {
1520 reply_nterror(req
, status
);
1524 if (ms_has_wild(oldname
)) {
1525 reply_nterror(req
, NT_STATUS_OBJECT_PATH_SYNTAX_BAD
);
1530 p
+= srvstr_get_path_req_wcard(ctx
, req
, &newname
, p
, STR_TERMINATE
,
1531 &status
, &dest_has_wcard
);
1532 if (!NT_STATUS_IS_OK(status
)) {
1533 reply_nterror(req
, status
);
1537 if (!lp_posix_pathnames()) {
1538 /* The newname must begin with a ':' if the
1539 oldname contains a ':'. */
1540 if (strchr_m(oldname
, ':')) {
1541 if (newname
[0] != ':') {
1542 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1545 stream_rename
= true;
1550 * If this is a rename operation, allow wildcards and save the
1551 * destination's last component.
1553 if (rename_type
== RENAME_FLAG_RENAME
) {
1554 ucf_flags_src
= UCF_COND_ALLOW_WCARD_LCOMP
;
1555 ucf_flags_dst
= UCF_COND_ALLOW_WCARD_LCOMP
| UCF_SAVE_LCOMP
;
1558 /* rename_internals() calls unix_convert(), so don't call it here. */
1559 status
= filename_convert(ctx
, conn
,
1560 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1565 if (!NT_STATUS_IS_OK(status
)) {
1566 if (NT_STATUS_EQUAL(status
,
1567 NT_STATUS_PATH_NOT_COVERED
)) {
1568 reply_botherror(req
,
1569 NT_STATUS_PATH_NOT_COVERED
,
1570 ERRSRV
, ERRbadpath
);
1573 reply_nterror(req
, status
);
1577 status
= filename_convert(ctx
, conn
,
1578 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1583 if (!NT_STATUS_IS_OK(status
)) {
1584 if (NT_STATUS_EQUAL(status
,
1585 NT_STATUS_PATH_NOT_COVERED
)) {
1586 reply_botherror(req
,
1587 NT_STATUS_PATH_NOT_COVERED
,
1588 ERRSRV
, ERRbadpath
);
1591 reply_nterror(req
, status
);
1595 if (stream_rename
) {
1596 /* smb_fname_new must be the same as smb_fname_old. */
1597 TALLOC_FREE(smb_fname_new
->base_name
);
1598 smb_fname_new
->base_name
= talloc_strdup(smb_fname_new
,
1599 smb_fname_old
->base_name
);
1600 if (!smb_fname_new
->base_name
) {
1601 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1606 DEBUG(3,("reply_ntrename: %s -> %s\n",
1607 smb_fname_str_dbg(smb_fname_old
),
1608 smb_fname_str_dbg(smb_fname_new
)));
1610 switch(rename_type
) {
1611 case RENAME_FLAG_RENAME
:
1612 status
= rename_internals(ctx
, conn
, req
,
1613 smb_fname_old
, smb_fname_new
,
1614 attrs
, False
, src_has_wcard
,
1618 case RENAME_FLAG_HARD_LINK
:
1619 if (src_has_wcard
|| dest_has_wcard
) {
1621 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1623 status
= hardlink_internals(ctx
, conn
,
1630 case RENAME_FLAG_COPY
:
1631 if (src_has_wcard
|| dest_has_wcard
) {
1633 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1635 status
= copy_internals(ctx
, conn
, req
,
1641 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION
:
1642 status
= NT_STATUS_INVALID_PARAMETER
;
1645 status
= NT_STATUS_ACCESS_DENIED
; /* Default error. */
1649 if (!NT_STATUS_IS_OK(status
)) {
1650 if (open_was_deferred(req
->sconn
, req
->mid
)) {
1651 /* We have re-scheduled this call. */
1655 reply_nterror(req
, status
);
1659 reply_outbuf(req
, 0, 0);
1661 END_PROFILE(SMBntrename
);
1665 /****************************************************************************
1666 Reply to a notify change - queue the request and
1667 don't allow a directory to be opened.
1668 ****************************************************************************/
1670 static void smbd_smb1_notify_reply(struct smb_request
*req
,
1671 NTSTATUS error_code
,
1672 uint8_t *buf
, size_t len
)
1674 send_nt_replies(req
->conn
, req
, error_code
, (char *)buf
, len
, NULL
, 0);
1677 static void call_nt_transact_notify_change(connection_struct
*conn
,
1678 struct smb_request
*req
,
1682 uint32 parameter_count
,
1683 char **ppdata
, uint32 data_count
,
1684 uint32 max_data_count
,
1685 uint32 max_param_count
)
1687 uint16
*setup
= *ppsetup
;
1693 if(setup_count
< 6) {
1694 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1698 fsp
= file_fsp(req
, SVAL(setup
,4));
1699 filter
= IVAL(setup
, 0);
1700 recursive
= (SVAL(setup
, 6) != 0) ? True
: False
;
1702 DEBUG(3,("call_nt_transact_notify_change\n"));
1705 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
1710 char *filter_string
;
1712 if (!(filter_string
= notify_filter_string(NULL
, filter
))) {
1713 reply_nterror(req
,NT_STATUS_NO_MEMORY
);
1717 DEBUG(3,("call_nt_transact_notify_change: notify change "
1718 "called on %s, filter = %s, recursive = %d\n",
1719 fsp_str_dbg(fsp
), filter_string
, recursive
));
1721 TALLOC_FREE(filter_string
);
1724 if((!fsp
->is_directory
) || (conn
!= fsp
->conn
)) {
1725 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1729 if (fsp
->notify
== NULL
) {
1731 status
= change_notify_create(fsp
, filter
, recursive
);
1733 if (!NT_STATUS_IS_OK(status
)) {
1734 DEBUG(10, ("change_notify_create returned %s\n",
1735 nt_errstr(status
)));
1736 reply_nterror(req
, status
);
1741 if (fsp
->notify
->num_changes
!= 0) {
1744 * We've got changes pending, respond immediately
1748 * TODO: write a torture test to check the filtering behaviour
1752 change_notify_reply(req
,
1756 smbd_smb1_notify_reply
);
1759 * change_notify_reply() above has independently sent its
1766 * No changes pending, queue the request
1769 status
= change_notify_add_request(req
,
1773 smbd_smb1_notify_reply
);
1774 if (!NT_STATUS_IS_OK(status
)) {
1775 reply_nterror(req
, status
);
1780 /****************************************************************************
1781 Reply to an NT transact rename command.
1782 ****************************************************************************/
1784 static void call_nt_transact_rename(connection_struct
*conn
,
1785 struct smb_request
*req
,
1786 uint16
**ppsetup
, uint32 setup_count
,
1787 char **ppparams
, uint32 parameter_count
,
1788 char **ppdata
, uint32 data_count
,
1789 uint32 max_data_count
)
1791 char *params
= *ppparams
;
1792 char *new_name
= NULL
;
1793 files_struct
*fsp
= NULL
;
1794 bool dest_has_wcard
= False
;
1796 TALLOC_CTX
*ctx
= talloc_tos();
1798 if(parameter_count
< 5) {
1799 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1803 fsp
= file_fsp(req
, SVAL(params
, 0));
1804 if (!check_fsp(conn
, req
, fsp
)) {
1807 srvstr_get_path_wcard(ctx
, params
, req
->flags2
, &new_name
, params
+4,
1808 parameter_count
- 4,
1809 STR_TERMINATE
, &status
, &dest_has_wcard
);
1810 if (!NT_STATUS_IS_OK(status
)) {
1811 reply_nterror(req
, status
);
1816 * W2K3 ignores this request as the RAW-RENAME test
1817 * demonstrates, so we do.
1819 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
1821 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1822 fsp_str_dbg(fsp
), new_name
));
1827 /******************************************************************************
1828 Fake up a completely empty SD.
1829 *******************************************************************************/
1831 static NTSTATUS
get_null_nt_acl(TALLOC_CTX
*mem_ctx
, struct security_descriptor
**ppsd
)
1835 *ppsd
= make_standard_sec_desc( mem_ctx
, &global_sid_World
, &global_sid_World
, NULL
, &sd_size
);
1837 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1838 return NT_STATUS_NO_MEMORY
;
1841 return NT_STATUS_OK
;
1844 /****************************************************************************
1845 Reply to query a security descriptor.
1846 Callable from SMB2 and SMB2.
1847 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1849 ****************************************************************************/
1851 NTSTATUS
smbd_do_query_security_desc(connection_struct
*conn
,
1852 TALLOC_CTX
*mem_ctx
,
1854 uint32_t security_info_wanted
,
1855 uint32_t max_data_count
,
1856 uint8_t **ppmarshalled_sd
,
1860 struct security_descriptor
*psd
= NULL
;
1863 * Get the permissions to return.
1866 if ((security_info_wanted
& SECINFO_SACL
) &&
1867 !(fsp
->access_mask
& SEC_FLAG_SYSTEM_SECURITY
)) {
1868 return NT_STATUS_ACCESS_DENIED
;
1871 if ((security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|SECINFO_GROUP
)) &&
1872 !(fsp
->access_mask
& SEC_STD_READ_CONTROL
)) {
1873 return NT_STATUS_ACCESS_DENIED
;
1876 if (security_info_wanted
& (SECINFO_DACL
|SECINFO_OWNER
|
1877 SECINFO_GROUP
|SECINFO_SACL
)) {
1878 /* Don't return SECINFO_LABEL if anything else was
1879 requested. See bug #8458. */
1880 security_info_wanted
&= ~SECINFO_LABEL
;
1883 if (!lp_nt_acl_support(SNUM(conn
))) {
1884 status
= get_null_nt_acl(mem_ctx
, &psd
);
1885 } else if (security_info_wanted
& SECINFO_LABEL
) {
1886 /* Like W2K3 return a null object. */
1887 status
= get_null_nt_acl(mem_ctx
, &psd
);
1889 status
= SMB_VFS_FGET_NT_ACL(
1890 fsp
, security_info_wanted
, &psd
);
1892 if (!NT_STATUS_IS_OK(status
)) {
1896 if (!(security_info_wanted
& SECINFO_OWNER
)) {
1897 psd
->owner_sid
= NULL
;
1899 if (!(security_info_wanted
& SECINFO_GROUP
)) {
1900 psd
->group_sid
= NULL
;
1902 if (!(security_info_wanted
& SECINFO_DACL
)) {
1903 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
1906 if (!(security_info_wanted
& SECINFO_SACL
)) {
1907 psd
->type
&= ~SEC_DESC_SACL_PRESENT
;
1911 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1912 * present in the reply to match Windows behavior */
1913 if (psd
->sacl
== NULL
&&
1914 security_info_wanted
& SECINFO_SACL
)
1915 psd
->type
|= SEC_DESC_SACL_PRESENT
;
1916 if (psd
->dacl
== NULL
&&
1917 security_info_wanted
& SECINFO_DACL
)
1918 psd
->type
|= SEC_DESC_DACL_PRESENT
;
1920 if (security_info_wanted
& SECINFO_LABEL
) {
1921 /* Like W2K3 return a null object. */
1922 psd
->owner_sid
= NULL
;
1923 psd
->group_sid
= NULL
;
1926 psd
->type
&= ~(SEC_DESC_DACL_PRESENT
|SEC_DESC_SACL_PRESENT
);
1929 *psd_size
= ndr_size_security_descriptor(psd
, 0);
1931 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1932 (unsigned long)*psd_size
));
1934 if (DEBUGLEVEL
>= 10) {
1935 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1937 NDR_PRINT_DEBUG(security_descriptor
, psd
);
1940 if (max_data_count
< *psd_size
) {
1942 return NT_STATUS_BUFFER_TOO_SMALL
;
1945 status
= marshall_sec_desc(mem_ctx
, psd
,
1946 ppmarshalled_sd
, psd_size
);
1948 if (!NT_STATUS_IS_OK(status
)) {
1954 return NT_STATUS_OK
;
1957 /****************************************************************************
1958 SMB1 reply to query a security descriptor.
1959 ****************************************************************************/
1961 static void call_nt_transact_query_security_desc(connection_struct
*conn
,
1962 struct smb_request
*req
,
1966 uint32 parameter_count
,
1969 uint32 max_data_count
)
1971 char *params
= *ppparams
;
1972 char *data
= *ppdata
;
1974 uint32 security_info_wanted
;
1975 files_struct
*fsp
= NULL
;
1977 uint8_t *marshalled_sd
= NULL
;
1979 if(parameter_count
< 8) {
1980 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1984 fsp
= file_fsp(req
, SVAL(params
,0));
1986 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
1990 security_info_wanted
= IVAL(params
,4);
1992 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1993 "info_wanted = 0x%x\n", fsp_str_dbg(fsp
),
1994 (unsigned int)security_info_wanted
));
1996 params
= nttrans_realloc(ppparams
, 4);
1997 if(params
== NULL
) {
1998 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2003 * Get the permissions to return.
2006 status
= smbd_do_query_security_desc(conn
,
2009 security_info_wanted
,
2014 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
2015 SIVAL(params
,0,(uint32_t)sd_size
);
2016 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2017 params
, 4, NULL
, 0);
2021 if (!NT_STATUS_IS_OK(status
)) {
2022 reply_nterror(req
, status
);
2026 SMB_ASSERT(sd_size
> 0);
2028 SIVAL(params
,0,(uint32_t)sd_size
);
2030 if (max_data_count
< sd_size
) {
2031 send_nt_replies(conn
, req
, NT_STATUS_BUFFER_TOO_SMALL
,
2032 params
, 4, NULL
, 0);
2037 * Allocate the data we will return.
2040 data
= nttrans_realloc(ppdata
, sd_size
);
2042 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2046 memcpy(data
, marshalled_sd
, sd_size
);
2048 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, 4, data
, (int)sd_size
);
2053 /****************************************************************************
2054 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2055 ****************************************************************************/
2057 static void call_nt_transact_set_security_desc(connection_struct
*conn
,
2058 struct smb_request
*req
,
2062 uint32 parameter_count
,
2065 uint32 max_data_count
)
2067 char *params
= *ppparams
;
2068 char *data
= *ppdata
;
2069 files_struct
*fsp
= NULL
;
2070 uint32 security_info_sent
= 0;
2073 if(parameter_count
< 8) {
2074 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2078 if((fsp
= file_fsp(req
, SVAL(params
,0))) == NULL
) {
2079 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2083 if (!CAN_WRITE(fsp
->conn
)) {
2084 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2088 if(!lp_nt_acl_support(SNUM(conn
))) {
2092 security_info_sent
= IVAL(params
,4);
2094 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2095 fsp_str_dbg(fsp
), (unsigned int)security_info_sent
));
2097 if (data_count
== 0) {
2098 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2102 status
= set_sd(fsp
, (uint8
*)data
, data_count
, security_info_sent
);
2104 if (!NT_STATUS_IS_OK(status
)) {
2105 reply_nterror(req
, status
);
2110 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
2114 /****************************************************************************
2116 ****************************************************************************/
2118 static void call_nt_transact_ioctl(connection_struct
*conn
,
2119 struct smb_request
*req
,
2120 uint16
**ppsetup
, uint32 setup_count
,
2121 char **ppparams
, uint32 parameter_count
,
2122 char **ppdata
, uint32 data_count
,
2123 uint32 max_data_count
)
2131 char *out_data
= NULL
;
2132 uint32 out_data_len
= 0;
2133 char *pdata
= *ppdata
;
2134 TALLOC_CTX
*ctx
= talloc_tos();
2136 if (setup_count
!= 8) {
2137 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count
));
2138 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2142 function
= IVAL(*ppsetup
, 0);
2143 fidnum
= SVAL(*ppsetup
, 4);
2144 isFSctl
= CVAL(*ppsetup
, 6);
2145 compfilter
= CVAL(*ppsetup
, 7);
2147 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2148 function
, fidnum
, isFSctl
, compfilter
));
2150 fsp
=file_fsp(req
, fidnum
);
2153 * We don't really implement IOCTLs, especially on files.
2156 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2158 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
2162 /* Has to be for an open file! */
2163 if (!check_fsp_open(conn
, req
, fsp
)) {
2167 SMB_PERFCOUNT_SET_IOCTL(&req
->pcd
, function
);
2170 * out_data might be allocated by the VFS module, but talloc should be
2171 * used, and should be cleaned up when the request ends.
2173 status
= SMB_VFS_FSCTL(fsp
,
2179 (uint8_t **)&out_data
,
2182 if (!NT_STATUS_IS_OK(status
)) {
2183 reply_nterror(req
, status
);
2185 send_nt_replies(conn
, req
, NT_STATUS_OK
, NULL
, 0, out_data
, out_data_len
);
2190 #ifdef HAVE_SYS_QUOTAS
2191 /****************************************************************************
2192 Reply to get user quota
2193 ****************************************************************************/
2195 static void call_nt_transact_get_user_quota(connection_struct
*conn
,
2196 struct smb_request
*req
,
2200 uint32 parameter_count
,
2203 uint32 max_data_count
)
2205 NTSTATUS nt_status
= NT_STATUS_OK
;
2206 char *params
= *ppparams
;
2207 char *pdata
= *ppdata
;
2209 int data_len
=0,param_len
=0;
2212 files_struct
*fsp
= NULL
;
2216 bool start_enum
= True
;
2217 SMB_NTQUOTA_STRUCT qt
;
2218 SMB_NTQUOTA_LIST
*tmp_list
;
2219 SMB_NTQUOTA_HANDLE
*qt_handle
= NULL
;
2224 if (get_current_uid(conn
) != 0) {
2225 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2226 "[%s]\n", lp_servicename(SNUM(conn
)),
2227 conn
->session_info
->unix_info
->unix_name
));
2228 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2233 * Ensure minimum number of parameters sent.
2236 if (parameter_count
< 4) {
2237 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count
));
2238 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2242 /* maybe we can check the quota_fnum */
2243 fsp
= file_fsp(req
, SVAL(params
,0));
2244 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2245 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2246 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2250 /* the NULL pointer checking for fsp->fake_file_handle->pd
2251 * is done by CHECK_NTQUOTA_HANDLE_OK()
2253 qt_handle
= (SMB_NTQUOTA_HANDLE
*)fsp
->fake_file_handle
->private_data
;
2255 level
= SVAL(params
,2);
2257 /* unknown 12 bytes leading in params */
2260 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE
:
2261 /* seems that we should continue with the enum here --metze */
2263 if (qt_handle
->quota_list
!=NULL
&&
2264 qt_handle
->tmp_list
==NULL
) {
2267 free_ntquota_list(&(qt_handle
->quota_list
));
2269 /* Realloc the size of parameters and data we will return */
2271 params
= nttrans_realloc(ppparams
, param_len
);
2272 if(params
== NULL
) {
2273 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2278 SIVAL(params
,0,data_len
);
2285 case TRANSACT_GET_USER_QUOTA_LIST_START
:
2287 if (qt_handle
->quota_list
==NULL
&&
2288 qt_handle
->tmp_list
==NULL
) {
2292 if (start_enum
&& vfs_get_user_ntquota_list(fsp
,&(qt_handle
->quota_list
))!=0) {
2293 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2297 /* Realloc the size of parameters and data we will return */
2299 params
= nttrans_realloc(ppparams
, param_len
);
2300 if(params
== NULL
) {
2301 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2305 /* we should not trust the value in max_data_count*/
2306 max_data_count
= MIN(max_data_count
,2048);
2308 pdata
= nttrans_realloc(ppdata
, max_data_count
);/* should be max data count from client*/
2310 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2316 /* set params Size of returned Quota Data 4 bytes*/
2317 /* but set it later when we know it */
2319 /* for each entry push the data */
2322 qt_handle
->tmp_list
= qt_handle
->quota_list
;
2325 tmp_list
= qt_handle
->tmp_list
;
2327 for (;((tmp_list
!=NULL
)&&((qt_len
+40+SID_MAX_SIZE
)<max_data_count
));
2328 tmp_list
=tmp_list
->next
,entry
+=entry_len
,qt_len
+=entry_len
) {
2330 sid_len
= ndr_size_dom_sid(
2331 &tmp_list
->quotas
->sid
, 0);
2332 entry_len
= 40 + sid_len
;
2334 /* nextoffset entry 4 bytes */
2335 SIVAL(entry
,0,entry_len
);
2337 /* then the len of the SID 4 bytes */
2338 SIVAL(entry
,4,sid_len
);
2340 /* unknown data 8 bytes uint64_t */
2341 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2343 /* the used disk space 8 bytes uint64_t */
2344 SBIG_UINT(entry
,16,tmp_list
->quotas
->usedspace
);
2346 /* the soft quotas 8 bytes uint64_t */
2347 SBIG_UINT(entry
,24,tmp_list
->quotas
->softlim
);
2349 /* the hard quotas 8 bytes uint64_t */
2350 SBIG_UINT(entry
,32,tmp_list
->quotas
->hardlim
);
2352 /* and now the SID */
2353 sid_linearize(entry
+40, sid_len
, &tmp_list
->quotas
->sid
);
2356 qt_handle
->tmp_list
= tmp_list
;
2358 /* overwrite the offset of the last entry */
2359 SIVAL(entry
-entry_len
,0,0);
2361 data_len
= 4+qt_len
;
2362 /* overwrite the params quota_data_len */
2363 SIVAL(params
,0,data_len
);
2367 case TRANSACT_GET_USER_QUOTA_FOR_SID
:
2369 /* unknown 4 bytes IVAL(pdata,0) */
2371 if (data_count
< 8) {
2372 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count
,8));
2373 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2377 sid_len
= IVAL(pdata
,4);
2378 /* Ensure this is less than 1mb. */
2379 if (sid_len
> (1024*1024)) {
2380 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2384 if (data_count
< 8+sid_len
) {
2385 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count
,(unsigned long)(8+sid_len
)));
2386 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2390 data_len
= 4+40+sid_len
;
2392 if (max_data_count
< data_len
) {
2393 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2394 max_data_count
, data_len
));
2396 SIVAL(params
,0,data_len
);
2398 nt_status
= NT_STATUS_BUFFER_TOO_SMALL
;
2402 if (!sid_parse(pdata
+8,sid_len
,&sid
)) {
2403 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2407 if (vfs_get_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2410 * we have to return zero's in all fields
2411 * instead of returning an error here
2416 /* Realloc the size of parameters and data we will return */
2418 params
= nttrans_realloc(ppparams
, param_len
);
2419 if(params
== NULL
) {
2420 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2424 pdata
= nttrans_realloc(ppdata
, data_len
);
2426 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2432 /* set params Size of returned Quota Data 4 bytes*/
2433 SIVAL(params
,0,data_len
);
2435 /* nextoffset entry 4 bytes */
2438 /* then the len of the SID 4 bytes */
2439 SIVAL(entry
,4,sid_len
);
2441 /* unknown data 8 bytes uint64_t */
2442 SBIG_UINT(entry
,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2444 /* the used disk space 8 bytes uint64_t */
2445 SBIG_UINT(entry
,16,qt
.usedspace
);
2447 /* the soft quotas 8 bytes uint64_t */
2448 SBIG_UINT(entry
,24,qt
.softlim
);
2450 /* the hard quotas 8 bytes uint64_t */
2451 SBIG_UINT(entry
,32,qt
.hardlim
);
2453 /* and now the SID */
2454 sid_linearize(entry
+40, sid_len
, &sid
);
2459 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp
->fnum
,level
));
2460 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2465 send_nt_replies(conn
, req
, nt_status
, params
, param_len
,
2469 /****************************************************************************
2470 Reply to set user quota
2471 ****************************************************************************/
2473 static void call_nt_transact_set_user_quota(connection_struct
*conn
,
2474 struct smb_request
*req
,
2478 uint32 parameter_count
,
2481 uint32 max_data_count
)
2483 char *params
= *ppparams
;
2484 char *pdata
= *ppdata
;
2485 int data_len
=0,param_len
=0;
2486 SMB_NTQUOTA_STRUCT qt
;
2489 files_struct
*fsp
= NULL
;
2494 if (get_current_uid(conn
) != 0) {
2495 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2496 "[%s]\n", lp_servicename(SNUM(conn
)),
2497 conn
->session_info
->unix_info
->unix_name
));
2498 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2503 * Ensure minimum number of parameters sent.
2506 if (parameter_count
< 2) {
2507 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count
));
2508 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2512 /* maybe we can check the quota_fnum */
2513 fsp
= file_fsp(req
, SVAL(params
,0));
2514 if (!check_fsp_ntquota_handle(conn
, req
, fsp
)) {
2515 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2516 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
2520 if (data_count
< 40) {
2521 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count
,40));
2522 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2526 /* offset to next quota record.
2527 * 4 bytes IVAL(pdata,0)
2532 sid_len
= IVAL(pdata
,4);
2534 if (data_count
< 40+sid_len
|| (40+sid_len
< sid_len
)) {
2535 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count
,(unsigned long)40+sid_len
));
2536 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2540 /* unknown 8 bytes in pdata
2541 * maybe its the change time in NTTIME
2544 /* the used space 8 bytes (uint64_t)*/
2545 qt
.usedspace
= BVAL(pdata
,16);
2547 /* the soft quotas 8 bytes (uint64_t)*/
2548 qt
.softlim
= BVAL(pdata
,24);
2550 /* the hard quotas 8 bytes (uint64_t)*/
2551 qt
.hardlim
= BVAL(pdata
,32);
2553 if (!sid_parse(pdata
+40,sid_len
,&sid
)) {
2554 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2558 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid
)));
2560 /* 44 unknown bytes left... */
2562 if (vfs_set_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2563 reply_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
2567 send_nt_replies(conn
, req
, NT_STATUS_OK
, params
, param_len
,
2570 #endif /* HAVE_SYS_QUOTAS */
2572 static void handle_nttrans(connection_struct
*conn
,
2573 struct trans_state
*state
,
2574 struct smb_request
*req
)
2576 if (get_Protocol() >= PROTOCOL_NT1
) {
2577 req
->flags2
|= 0x40; /* IS_LONG_NAME */
2578 SSVAL(discard_const_p(uint8_t, req
->inbuf
),smb_flg2
,req
->flags2
);
2582 SMB_PERFCOUNT_SET_SUBOP(&req
->pcd
, state
->call
);
2584 /* Now we must call the relevant NT_TRANS function */
2585 switch(state
->call
) {
2586 case NT_TRANSACT_CREATE
:
2588 START_PROFILE(NT_transact_create
);
2589 call_nt_transact_create(
2591 &state
->setup
, state
->setup_count
,
2592 &state
->param
, state
->total_param
,
2593 &state
->data
, state
->total_data
,
2594 state
->max_data_return
);
2595 END_PROFILE(NT_transact_create
);
2599 case NT_TRANSACT_IOCTL
:
2601 START_PROFILE(NT_transact_ioctl
);
2602 call_nt_transact_ioctl(
2604 &state
->setup
, state
->setup_count
,
2605 &state
->param
, state
->total_param
,
2606 &state
->data
, state
->total_data
,
2607 state
->max_data_return
);
2608 END_PROFILE(NT_transact_ioctl
);
2612 case NT_TRANSACT_SET_SECURITY_DESC
:
2614 START_PROFILE(NT_transact_set_security_desc
);
2615 call_nt_transact_set_security_desc(
2617 &state
->setup
, state
->setup_count
,
2618 &state
->param
, state
->total_param
,
2619 &state
->data
, state
->total_data
,
2620 state
->max_data_return
);
2621 END_PROFILE(NT_transact_set_security_desc
);
2625 case NT_TRANSACT_NOTIFY_CHANGE
:
2627 START_PROFILE(NT_transact_notify_change
);
2628 call_nt_transact_notify_change(
2630 &state
->setup
, state
->setup_count
,
2631 &state
->param
, state
->total_param
,
2632 &state
->data
, state
->total_data
,
2633 state
->max_data_return
,
2634 state
->max_param_return
);
2635 END_PROFILE(NT_transact_notify_change
);
2639 case NT_TRANSACT_RENAME
:
2641 START_PROFILE(NT_transact_rename
);
2642 call_nt_transact_rename(
2644 &state
->setup
, state
->setup_count
,
2645 &state
->param
, state
->total_param
,
2646 &state
->data
, state
->total_data
,
2647 state
->max_data_return
);
2648 END_PROFILE(NT_transact_rename
);
2652 case NT_TRANSACT_QUERY_SECURITY_DESC
:
2654 START_PROFILE(NT_transact_query_security_desc
);
2655 call_nt_transact_query_security_desc(
2657 &state
->setup
, state
->setup_count
,
2658 &state
->param
, state
->total_param
,
2659 &state
->data
, state
->total_data
,
2660 state
->max_data_return
);
2661 END_PROFILE(NT_transact_query_security_desc
);
2665 #ifdef HAVE_SYS_QUOTAS
2666 case NT_TRANSACT_GET_USER_QUOTA
:
2668 START_PROFILE(NT_transact_get_user_quota
);
2669 call_nt_transact_get_user_quota(
2671 &state
->setup
, state
->setup_count
,
2672 &state
->param
, state
->total_param
,
2673 &state
->data
, state
->total_data
,
2674 state
->max_data_return
);
2675 END_PROFILE(NT_transact_get_user_quota
);
2679 case NT_TRANSACT_SET_USER_QUOTA
:
2681 START_PROFILE(NT_transact_set_user_quota
);
2682 call_nt_transact_set_user_quota(
2684 &state
->setup
, state
->setup_count
,
2685 &state
->param
, state
->total_param
,
2686 &state
->data
, state
->total_data
,
2687 state
->max_data_return
);
2688 END_PROFILE(NT_transact_set_user_quota
);
2691 #endif /* HAVE_SYS_QUOTAS */
2694 /* Error in request */
2695 DEBUG(0,("handle_nttrans: Unknown request %d in "
2696 "nttrans call\n", state
->call
));
2697 reply_nterror(req
, NT_STATUS_INVALID_LEVEL
);
2703 /****************************************************************************
2704 Reply to a SMBNTtrans.
2705 ****************************************************************************/
2707 void reply_nttrans(struct smb_request
*req
)
2709 connection_struct
*conn
= req
->conn
;
2714 uint16 function_code
;
2716 struct trans_state
*state
;
2718 START_PROFILE(SMBnttrans
);
2720 if (req
->wct
< 19) {
2721 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2722 END_PROFILE(SMBnttrans
);
2726 pscnt
= IVAL(req
->vwv
+9, 1);
2727 psoff
= IVAL(req
->vwv
+11, 1);
2728 dscnt
= IVAL(req
->vwv
+13, 1);
2729 dsoff
= IVAL(req
->vwv
+15, 1);
2730 function_code
= SVAL(req
->vwv
+18, 0);
2732 if (IS_IPC(conn
) && (function_code
!= NT_TRANSACT_CREATE
)) {
2733 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
2734 END_PROFILE(SMBnttrans
);
2738 result
= allow_new_trans(conn
->pending_trans
, req
->mid
);
2739 if (!NT_STATUS_IS_OK(result
)) {
2740 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result
)));
2741 reply_nterror(req
, result
);
2742 END_PROFILE(SMBnttrans
);
2746 if ((state
= talloc(conn
, struct trans_state
)) == NULL
) {
2747 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2748 END_PROFILE(SMBnttrans
);
2752 state
->cmd
= SMBnttrans
;
2754 state
->mid
= req
->mid
;
2755 state
->vuid
= req
->vuid
;
2756 state
->total_data
= IVAL(req
->vwv
+3, 1);
2758 state
->total_param
= IVAL(req
->vwv
+1, 1);
2759 state
->param
= NULL
;
2760 state
->max_data_return
= IVAL(req
->vwv
+7, 1);
2761 state
->max_param_return
= IVAL(req
->vwv
+5, 1);
2763 /* setup count is in *words* */
2764 state
->setup_count
= 2*CVAL(req
->vwv
+17, 1);
2765 state
->setup
= NULL
;
2766 state
->call
= function_code
;
2768 DEBUG(10, ("num_setup=%u, "
2769 "param_total=%u, this_param=%u, max_param=%u, "
2770 "data_total=%u, this_data=%u, max_data=%u, "
2771 "param_offset=%u, data_offset=%u\n",
2772 (unsigned)state
->setup_count
,
2773 (unsigned)state
->total_param
, (unsigned)pscnt
,
2774 (unsigned)state
->max_param_return
,
2775 (unsigned)state
->total_data
, (unsigned)dscnt
,
2776 (unsigned)state
->max_data_return
,
2777 (unsigned)psoff
, (unsigned)dsoff
));
2780 * All nttrans messages we handle have smb_wct == 19 +
2781 * state->setup_count. Ensure this is so as a sanity check.
2784 if(req
->wct
!= 19 + (state
->setup_count
/2)) {
2785 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2786 req
->wct
, 19 + (state
->setup_count
/2)));
2790 /* Don't allow more than 128mb for each value. */
2791 if ((state
->total_data
> (1024*1024*128)) ||
2792 (state
->total_param
> (1024*1024*128))) {
2793 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2794 END_PROFILE(SMBnttrans
);
2798 if ((dscnt
> state
->total_data
) || (pscnt
> state
->total_param
))
2801 if (state
->total_data
) {
2803 if (trans_oob(state
->total_data
, 0, dscnt
)
2804 || trans_oob(smb_len(req
->inbuf
), dsoff
, dscnt
)) {
2808 /* Can't use talloc here, the core routines do realloc on the
2809 * params and data. */
2810 if ((state
->data
= (char *)SMB_MALLOC(state
->total_data
)) == NULL
) {
2811 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2812 "bytes !\n", (unsigned int)state
->total_data
));
2814 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2815 END_PROFILE(SMBnttrans
);
2819 memcpy(state
->data
,smb_base(req
->inbuf
)+dsoff
,dscnt
);
2822 if (state
->total_param
) {
2824 if (trans_oob(state
->total_param
, 0, pscnt
)
2825 || trans_oob(smb_len(req
->inbuf
), psoff
, pscnt
)) {
2829 /* Can't use talloc here, the core routines do realloc on the
2830 * params and data. */
2831 if ((state
->param
= (char *)SMB_MALLOC(state
->total_param
)) == NULL
) {
2832 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2833 "bytes !\n", (unsigned int)state
->total_param
));
2834 SAFE_FREE(state
->data
);
2836 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2837 END_PROFILE(SMBnttrans
);
2841 memcpy(state
->param
,smb_base(req
->inbuf
)+psoff
,pscnt
);
2844 state
->received_data
= dscnt
;
2845 state
->received_param
= pscnt
;
2847 if(state
->setup_count
> 0) {
2848 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2849 state
->setup_count
));
2852 * No overflow possible here, state->setup_count is an
2853 * unsigned int, being filled by a single byte from
2854 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2855 * below is not necessary, it's here to clarify things. The
2856 * validity of req->vwv and req->wct has been checked in
2857 * init_smb_request already.
2859 if ((state
->setup_count
/2) + 19 > (unsigned int)req
->wct
) {
2863 state
->setup
= (uint16
*)TALLOC(state
, state
->setup_count
);
2864 if (state
->setup
== NULL
) {
2865 DEBUG(0,("reply_nttrans : Out of memory\n"));
2866 SAFE_FREE(state
->data
);
2867 SAFE_FREE(state
->param
);
2869 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2870 END_PROFILE(SMBnttrans
);
2874 memcpy(state
->setup
, req
->vwv
+19, state
->setup_count
);
2875 dump_data(10, (uint8
*)state
->setup
, state
->setup_count
);
2878 if ((state
->received_data
== state
->total_data
) &&
2879 (state
->received_param
== state
->total_param
)) {
2880 handle_nttrans(conn
, state
, req
);
2881 SAFE_FREE(state
->param
);
2882 SAFE_FREE(state
->data
);
2884 END_PROFILE(SMBnttrans
);
2888 DLIST_ADD(conn
->pending_trans
, state
);
2890 /* We need to send an interim response then receive the rest
2891 of the parameter/data bytes */
2892 reply_outbuf(req
, 0, 0);
2893 show_msg((char *)req
->outbuf
);
2894 END_PROFILE(SMBnttrans
);
2899 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2900 SAFE_FREE(state
->data
);
2901 SAFE_FREE(state
->param
);
2903 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2904 END_PROFILE(SMBnttrans
);
2908 /****************************************************************************
2909 Reply to a SMBnttranss
2910 ****************************************************************************/
2912 void reply_nttranss(struct smb_request
*req
)
2914 connection_struct
*conn
= req
->conn
;
2915 uint32_t pcnt
,poff
,dcnt
,doff
,pdisp
,ddisp
;
2916 struct trans_state
*state
;
2918 START_PROFILE(SMBnttranss
);
2920 show_msg((const char *)req
->inbuf
);
2922 if (req
->wct
< 18) {
2923 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2924 END_PROFILE(SMBnttranss
);
2928 for (state
= conn
->pending_trans
; state
!= NULL
;
2929 state
= state
->next
) {
2930 if (state
->mid
== req
->mid
) {
2935 if ((state
== NULL
) || (state
->cmd
!= SMBnttrans
)) {
2936 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2937 END_PROFILE(SMBnttranss
);
2941 /* Revise state->total_param and state->total_data in case they have
2942 changed downwards */
2943 if (IVAL(req
->vwv
+1, 1) < state
->total_param
) {
2944 state
->total_param
= IVAL(req
->vwv
+1, 1);
2946 if (IVAL(req
->vwv
+3, 1) < state
->total_data
) {
2947 state
->total_data
= IVAL(req
->vwv
+3, 1);
2950 pcnt
= IVAL(req
->vwv
+5, 1);
2951 poff
= IVAL(req
->vwv
+7, 1);
2952 pdisp
= IVAL(req
->vwv
+9, 1);
2954 dcnt
= IVAL(req
->vwv
+11, 1);
2955 doff
= IVAL(req
->vwv
+13, 1);
2956 ddisp
= IVAL(req
->vwv
+15, 1);
2958 state
->received_param
+= pcnt
;
2959 state
->received_data
+= dcnt
;
2961 if ((state
->received_data
> state
->total_data
) ||
2962 (state
->received_param
> state
->total_param
))
2966 if (trans_oob(state
->total_param
, pdisp
, pcnt
)
2967 || trans_oob(smb_len(req
->inbuf
), poff
, pcnt
)) {
2970 memcpy(state
->param
+pdisp
, smb_base(req
->inbuf
)+poff
,pcnt
);
2974 if (trans_oob(state
->total_data
, ddisp
, dcnt
)
2975 || trans_oob(smb_len(req
->inbuf
), doff
, dcnt
)) {
2978 memcpy(state
->data
+ddisp
, smb_base(req
->inbuf
)+doff
,dcnt
);
2981 if ((state
->received_param
< state
->total_param
) ||
2982 (state
->received_data
< state
->total_data
)) {
2983 END_PROFILE(SMBnttranss
);
2987 handle_nttrans(conn
, state
, req
);
2989 DLIST_REMOVE(conn
->pending_trans
, state
);
2990 SAFE_FREE(state
->data
);
2991 SAFE_FREE(state
->param
);
2993 END_PROFILE(SMBnttranss
);
2998 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2999 DLIST_REMOVE(conn
->pending_trans
, state
);
3000 SAFE_FREE(state
->data
);
3001 SAFE_FREE(state
->param
);
3003 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3004 END_PROFILE(SMBnttranss
);