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/>.
24 extern enum protocol_types Protocol;
26 static char *nttrans_realloc(char **ptr, size_t size)
29 smb_panic("nttrans_realloc() called with NULL ptr");
32 *ptr = (char *)SMB_REALLOC(*ptr, size);
36 memset(*ptr,'\0',size);
40 /****************************************************************************
41 Send the required number of replies back.
42 We assume all fields other than the data fields are
43 set correctly for the type of call.
44 HACK ! Always assumes smb_setup field is zero.
45 ****************************************************************************/
47 void send_nt_replies(connection_struct *conn,
48 struct smb_request *req, NTSTATUS nt_error,
49 char *params, int paramsize,
50 char *pdata, int datasize)
52 int data_to_send = datasize;
53 int params_to_send = paramsize;
57 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
58 int alignment_offset = 3;
59 int data_alignment_offset = 0;
62 * If there genuinely are no parameters or data to send just send
66 if(params_to_send == 0 && data_to_send == 0) {
67 reply_outbuf(req, 18, 0);
68 show_msg((char *)req->outbuf);
73 * When sending params and data ensure that both are nicely aligned.
74 * Only do this alignment when there is also data to send - else
75 * can cause NT redirector problems.
78 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
79 data_alignment_offset = 4 - (params_to_send % 4);
83 * Space is bufsize minus Netbios over TCP header minus SMB header.
84 * The alignment_offset is to align the param bytes on a four byte
85 * boundary (2 bytes for data len, one byte pad).
86 * NT needs this to work correctly.
89 useable_space = max_send - (smb_size
92 + data_alignment_offset);
94 if (useable_space < 0) {
95 DEBUG(0, ("send_nt_replies failed sanity useable_space "
96 "= %d!!!", useable_space));
97 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
100 while (params_to_send || data_to_send) {
103 * Calculate whether we will totally or partially fill this packet.
106 total_sent_thistime = params_to_send + data_to_send;
109 * We can never send more than useable_space.
112 total_sent_thistime = MIN(total_sent_thistime, useable_space);
114 reply_outbuf(req, 18,
115 total_sent_thistime + alignment_offset
116 + data_alignment_offset);
119 * Set total params and data to be sent.
122 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
123 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
126 * Calculate how many parameters and data we can fit into
127 * this packet. Parameters get precedence.
130 params_sent_thistime = MIN(params_to_send,useable_space);
131 data_sent_thistime = useable_space - params_sent_thistime;
132 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
134 SIVAL(req->outbuf, smb_ntr_ParameterCount,
135 params_sent_thistime);
137 if(params_sent_thistime == 0) {
138 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
139 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
142 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
143 * parameter bytes, however the first 4 bytes of outbuf are
144 * the Netbios over TCP header. Thus use smb_base() to subtract
145 * them from the calculation.
148 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
149 ((smb_buf(req->outbuf)+alignment_offset)
150 - smb_base(req->outbuf)));
152 * Absolute displacement of param bytes sent in this packet.
155 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
160 * Deal with the data portion.
163 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
165 if(data_sent_thistime == 0) {
166 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
167 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
170 * The offset of the data bytes is the offset of the
171 * parameter bytes plus the number of parameters being sent this time.
174 SIVAL(req->outbuf, smb_ntr_DataOffset,
175 ((smb_buf(req->outbuf)+alignment_offset) -
176 smb_base(req->outbuf))
177 + params_sent_thistime + data_alignment_offset);
178 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
182 * Copy the param bytes into the packet.
185 if(params_sent_thistime) {
186 if (alignment_offset != 0) {
187 memset(smb_buf(req->outbuf), 0,
190 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
191 params_sent_thistime);
195 * Copy in the data bytes
198 if(data_sent_thistime) {
199 if (data_alignment_offset != 0) {
200 memset((smb_buf(req->outbuf)+alignment_offset+
201 params_sent_thistime), 0,
202 data_alignment_offset);
204 memcpy(smb_buf(req->outbuf)+alignment_offset
205 +params_sent_thistime+data_alignment_offset,
206 pd,data_sent_thistime);
209 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
210 params_sent_thistime, data_sent_thistime, useable_space));
211 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
212 params_to_send, data_to_send, paramsize, datasize));
214 if (NT_STATUS_V(nt_error)) {
215 error_packet_set((char *)req->outbuf,
220 /* Send the packet */
221 show_msg((char *)req->outbuf);
222 if (!srv_send_smb(smbd_server_fd(),
224 IS_CONN_ENCRYPTED(conn))) {
225 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
228 TALLOC_FREE(req->outbuf);
230 pp += params_sent_thistime;
231 pd += data_sent_thistime;
233 params_to_send -= params_sent_thistime;
234 data_to_send -= data_sent_thistime;
240 if(params_to_send < 0 || data_to_send < 0) {
241 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
242 params_to_send, data_to_send));
243 exit_server_cleanly("send_nt_replies: internal error");
248 /****************************************************************************
249 Is it an NTFS stream name ?
250 An NTFS file name is <path>.<extention>:<stream name>:<stream type>
251 $DATA can be used as both a stream name and a stream type. A missing stream
252 name or type implies $DATA.
253 ****************************************************************************/
255 bool is_ntfs_stream_name(const char *fname)
257 if (lp_posix_pathnames()) {
260 return (strchr_m(fname, ':') != NULL) ? True : False;
263 /****************************************************************************
264 Reply to an NT create and X call on a pipe
265 ****************************************************************************/
267 static void nt_open_pipe(char *fname, connection_struct *conn,
268 struct smb_request *req, int *ppnum)
270 smb_np_struct *p = NULL;
272 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
274 /* See if it is one we want to handle. */
276 if (!is_known_pipename(fname)) {
277 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
282 /* Strip \\ off the name. */
285 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
287 p = open_rpc_pipe_p(fname, conn, req->vuid);
289 reply_doserror(req, ERRSRV, ERRnofids);
293 /* TODO: Add pipe to db */
295 if ( !store_pipe_opendb( p ) ) {
296 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
303 /****************************************************************************
304 Reply to an NT create and X call for pipes.
305 ****************************************************************************/
307 static void do_ntcreate_pipe_open(connection_struct *conn,
308 struct smb_request *req)
313 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
314 TALLOC_CTX *ctx = talloc_tos();
316 srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
317 smb_buf(req->inbuf), STR_TERMINATE);
320 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
324 nt_open_pipe(fname, conn, req, &pnum);
332 * Deal with pipe return.
335 if (flags & EXTENDED_RESPONSE_REQUIRED) {
336 /* This is very strange. We
337 * return 50 words, but only set
338 * the wcnt to 42 ? It's definately
339 * what happens on the wire....
341 reply_outbuf(req, 50, 0);
342 SCVAL(req->outbuf,smb_wct,42);
344 reply_outbuf(req, 34, 0);
347 p = (char *)req->outbuf + smb_vwv2;
351 SIVAL(p,0,FILE_WAS_OPENED);
354 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
357 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
359 SSVAL(p,2, 0x5FF); /* ? */
362 if (flags & EXTENDED_RESPONSE_REQUIRED) {
364 SIVAL(p,0,FILE_GENERIC_ALL);
366 * For pipes W2K3 seems to return
368 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
370 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
373 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
378 /****************************************************************************
379 Reply to an NT create and X call.
380 ****************************************************************************/
382 void reply_ntcreate_and_X(struct smb_request *req)
384 connection_struct *conn = req->conn;
388 uint32 file_attributes;
390 uint32 create_disposition;
391 uint32 create_options;
393 SMB_BIG_UINT allocation_size;
394 /* Breakout the oplock request bits so we can set the
395 reply bits separately. */
397 SMB_OFF_T file_len = 0;
398 SMB_STRUCT_STAT sbuf;
400 files_struct *fsp = NULL;
402 struct timespec c_timespec;
403 struct timespec a_timespec;
404 struct timespec m_timespec;
407 uint8_t oplock_granted = NO_OPLOCK_RETURN;
408 TALLOC_CTX *ctx = talloc_tos();
410 START_PROFILE(SMBntcreateX);
413 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
417 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
418 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
419 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
420 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
421 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
422 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
423 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
425 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,
426 smb_ntcreate_AllocationSize);
427 #ifdef LARGE_SMB_OFF_T
428 allocation_size |= (((SMB_BIG_UINT)IVAL(
430 smb_ntcreate_AllocationSize + 4)) << 32);
433 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
434 smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
436 if (!NT_STATUS_IS_OK(status)) {
437 reply_nterror(req, status);
438 END_PROFILE(SMBntcreateX);
442 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
443 "file_attributes = 0x%x, share_access = 0x%x, "
444 "create_disposition = 0x%x create_options = 0x%x "
445 "root_dir_fid = 0x%x, fname = %s\n",
447 (unsigned int)access_mask,
448 (unsigned int)file_attributes,
449 (unsigned int)share_access,
450 (unsigned int)create_disposition,
451 (unsigned int)create_options,
452 (unsigned int)root_dir_fid,
456 * we need to remove ignored bits when they come directly from the client
457 * because we reuse some of them for internal stuff
459 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
462 * If it's an IPC, use the pipe handler.
466 if (lp_nt_pipe_support()) {
467 do_ntcreate_pipe_open(conn, req);
468 END_PROFILE(SMBntcreateX);
471 reply_doserror(req, ERRDOS, ERRnoaccess);
472 END_PROFILE(SMBntcreateX);
476 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
477 if (oplock_request) {
478 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
482 status = create_file(conn, req, root_dir_fid, fname,
483 access_mask, share_access, create_disposition,
484 create_options, file_attributes, oplock_request,
485 allocation_size, NULL, NULL, &fsp, &info, &sbuf);
487 if (!NT_STATUS_IS_OK(status)) {
488 if (open_was_deferred(req->mid)) {
489 /* We have re-scheduled this call, no error. */
490 END_PROFILE(SMBntcreateX);
493 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
494 reply_botherror(req, status, ERRDOS, ERRfilexists);
497 reply_nterror(req, status);
499 END_PROFILE(SMBntcreateX);
504 * If the caller set the extended oplock request bit
505 * and we granted one (by whatever means) - set the
506 * correct bit for extended oplock reply.
509 if (oplock_request &&
510 (lp_fake_oplocks(SNUM(conn))
511 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
514 * Exclusive oplock granted
517 if (flags & REQUEST_BATCH_OPLOCK) {
518 oplock_granted = BATCH_OPLOCK_RETURN;
520 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
522 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
523 oplock_granted = LEVEL_II_OPLOCK_RETURN;
525 oplock_granted = NO_OPLOCK_RETURN;
528 file_len = sbuf.st_size;
529 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
531 fattr = FILE_ATTRIBUTE_NORMAL;
534 if (flags & EXTENDED_RESPONSE_REQUIRED) {
535 /* This is very strange. We
536 * return 50 words, but only set
537 * the wcnt to 42 ? It's definately
538 * what happens on the wire....
540 reply_outbuf(req, 50, 0);
541 SCVAL(req->outbuf,smb_wct,42);
543 reply_outbuf(req, 34, 0);
546 p = (char *)req->outbuf + smb_vwv2;
548 SCVAL(p, 0, oplock_granted);
551 SSVAL(p,0,fsp->fnum);
553 if ((create_disposition == FILE_SUPERSEDE)
554 && (info == FILE_WAS_OVERWRITTEN)) {
555 SIVAL(p,0,FILE_WAS_SUPERSEDED);
562 c_timespec = get_create_timespec(
563 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
564 a_timespec = get_atimespec(&sbuf);
565 m_timespec = get_mtimespec(&sbuf);
567 if (lp_dos_filetime_resolution(SNUM(conn))) {
568 dos_filetime_timespec(&c_timespec);
569 dos_filetime_timespec(&a_timespec);
570 dos_filetime_timespec(&m_timespec);
573 put_long_date_timespec(p, c_timespec); /* create time. */
575 put_long_date_timespec(p, a_timespec); /* access time */
577 put_long_date_timespec(p, m_timespec); /* write time */
579 put_long_date_timespec(p, m_timespec); /* change time */
581 SIVAL(p,0,fattr); /* File Attributes. */
583 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
585 SOFF_T(p,0,file_len);
587 if (flags & EXTENDED_RESPONSE_REQUIRED) {
591 SCVAL(p,0,fsp->is_directory ? 1 : 0);
593 if (flags & EXTENDED_RESPONSE_REQUIRED) {
596 if (fsp->is_directory
597 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
598 perms = FILE_GENERIC_ALL;
600 perms = FILE_GENERIC_READ|FILE_EXECUTE;
605 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
606 fsp->fnum, fsp->fsp_name));
609 END_PROFILE(SMBntcreateX);
613 /****************************************************************************
614 Reply to a NT_TRANSACT_CREATE call to open a pipe.
615 ****************************************************************************/
617 static void do_nt_transact_create_pipe(connection_struct *conn,
618 struct smb_request *req,
619 uint16 **ppsetup, uint32 setup_count,
620 char **ppparams, uint32 parameter_count,
621 char **ppdata, uint32 data_count)
624 char *params = *ppparams;
630 TALLOC_CTX *ctx = talloc_tos();
633 * Ensure minimum number of parameters sent.
636 if(parameter_count < 54) {
637 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
638 reply_doserror(req, ERRDOS, ERRnoaccess);
642 flags = IVAL(params,0);
644 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
645 parameter_count-53, STR_TERMINATE,
647 if (!NT_STATUS_IS_OK(status)) {
648 reply_nterror(req, status);
652 nt_open_pipe(fname, conn, req, &pnum);
659 /* Realloc the size of parameters and data we will return */
660 if (flags & EXTENDED_RESPONSE_REQUIRED) {
661 /* Extended response is 32 more byyes. */
666 params = nttrans_realloc(ppparams, param_len);
668 reply_doserror(req, ERRDOS, ERRnomem);
673 SCVAL(p,0,NO_OPLOCK_RETURN);
678 SIVAL(p,0,FILE_WAS_OPENED);
682 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
685 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
687 SSVAL(p,2, 0x5FF); /* ? */
690 if (flags & EXTENDED_RESPONSE_REQUIRED) {
692 SIVAL(p,0,FILE_GENERIC_ALL);
694 * For pipes W2K3 seems to return
696 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
698 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
701 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
703 /* Send the required number of replies */
704 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
709 /****************************************************************************
710 Internal fn to set security descriptors.
711 ****************************************************************************/
713 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
714 uint32 security_info_sent)
716 SEC_DESC *psd = NULL;
719 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
723 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
725 if (!NT_STATUS_IS_OK(status)) {
729 if (psd->owner_sid==0) {
730 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
732 if (psd->group_sid==0) {
733 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
736 security_info_sent &= ~SACL_SECURITY_INFORMATION;
739 security_info_sent &= ~DACL_SECURITY_INFORMATION;
742 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
749 /****************************************************************************
750 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
751 ****************************************************************************/
753 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
755 struct ea_list *ea_list_head = NULL;
762 while (offset + 4 <= data_size) {
763 size_t next_offset = IVAL(pdata,offset);
764 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
770 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
771 if (next_offset == 0) {
774 offset += next_offset;
780 /****************************************************************************
781 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
782 ****************************************************************************/
784 static void call_nt_transact_create(connection_struct *conn,
785 struct smb_request *req,
786 uint16 **ppsetup, uint32 setup_count,
787 char **ppparams, uint32 parameter_count,
788 char **ppdata, uint32 data_count,
789 uint32 max_data_count)
792 char *params = *ppparams;
793 char *data = *ppdata;
794 /* Breakout the oplock request bits so we can set the reply bits separately. */
796 SMB_OFF_T file_len = 0;
797 SMB_STRUCT_STAT sbuf;
799 files_struct *fsp = NULL;
803 uint32 file_attributes;
805 uint32 create_disposition;
806 uint32 create_options;
808 struct security_descriptor *sd = NULL;
811 struct timespec c_timespec;
812 struct timespec a_timespec;
813 struct timespec m_timespec;
814 struct ea_list *ea_list = NULL;
817 SMB_BIG_UINT allocation_size;
819 uint8_t oplock_granted;
820 TALLOC_CTX *ctx = talloc_tos();
822 DEBUG(5,("call_nt_transact_create\n"));
825 * If it's an IPC, use the pipe handler.
829 if (lp_nt_pipe_support()) {
830 do_nt_transact_create_pipe(
832 ppsetup, setup_count,
833 ppparams, parameter_count,
837 reply_doserror(req, ERRDOS, ERRnoaccess);
842 * Ensure minimum number of parameters sent.
845 if(parameter_count < 54) {
846 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
847 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
851 flags = IVAL(params,0);
852 access_mask = IVAL(params,8);
853 file_attributes = IVAL(params,20);
854 share_access = IVAL(params,24);
855 create_disposition = IVAL(params,28);
856 create_options = IVAL(params,32);
857 sd_len = IVAL(params,36);
858 ea_len = IVAL(params,40);
859 root_dir_fid = (uint16)IVAL(params,4);
860 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
861 #ifdef LARGE_SMB_OFF_T
862 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
866 * we need to remove ignored bits when they come directly from the client
867 * because we reuse some of them for internal stuff
869 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
871 /* Ensure the data_len is correct for the sd and ea values given. */
872 if ((ea_len + sd_len > data_count)
873 || (ea_len > data_count) || (sd_len > data_count)
874 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
875 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
876 "%u, data_count = %u\n", (unsigned int)ea_len,
877 (unsigned int)sd_len, (unsigned int)data_count));
878 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
883 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
886 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
888 if (!NT_STATUS_IS_OK(status)) {
889 DEBUG(10, ("call_nt_transact_create: "
890 "unmarshall_sec_desc failed: %s\n",
892 reply_nterror(req, status);
898 if (!lp_ea_support(SNUM(conn))) {
899 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
900 "EA's not supported.\n",
901 (unsigned int)ea_len));
902 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
907 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
908 "too small (should be more than 10)\n",
909 (unsigned int)ea_len ));
910 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
914 /* We have already checked that ea_len <= data_count here. */
915 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
917 if (ea_list == NULL) {
918 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
923 srvstr_get_path(ctx, params, req->flags2, &fname,
924 params+53, parameter_count-53,
925 STR_TERMINATE, &status);
926 if (!NT_STATUS_IS_OK(status)) {
927 reply_nterror(req, status);
931 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
932 if (oplock_request) {
933 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
937 status = create_file(conn, req, root_dir_fid, fname,
938 access_mask, share_access, create_disposition,
939 create_options, file_attributes, oplock_request,
940 allocation_size, sd, ea_list, &fsp, &info, &sbuf);
942 if(!NT_STATUS_IS_OK(status)) {
943 if (open_was_deferred(req->mid)) {
944 /* We have re-scheduled this call, no error. */
947 reply_openerror(req, status);
952 * If the caller set the extended oplock request bit
953 * and we granted one (by whatever means) - set the
954 * correct bit for extended oplock reply.
957 if (oplock_request &&
958 (lp_fake_oplocks(SNUM(conn))
959 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
962 * Exclusive oplock granted
965 if (flags & REQUEST_BATCH_OPLOCK) {
966 oplock_granted = BATCH_OPLOCK_RETURN;
968 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
970 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
971 oplock_granted = LEVEL_II_OPLOCK_RETURN;
973 oplock_granted = NO_OPLOCK_RETURN;
976 file_len = sbuf.st_size;
977 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
979 fattr = FILE_ATTRIBUTE_NORMAL;
982 /* Realloc the size of parameters and data we will return */
983 if (flags & EXTENDED_RESPONSE_REQUIRED) {
984 /* Extended response is 32 more byyes. */
989 params = nttrans_realloc(ppparams, param_len);
991 reply_doserror(req, ERRDOS, ERRnomem);
996 SCVAL(p, 0, oplock_granted);
999 SSVAL(p,0,fsp->fnum);
1001 if ((create_disposition == FILE_SUPERSEDE)
1002 && (info == FILE_WAS_OVERWRITTEN)) {
1003 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1010 c_timespec = get_create_timespec(
1011 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1012 a_timespec = get_atimespec(&sbuf);
1013 m_timespec = get_mtimespec(&sbuf);
1015 if (lp_dos_filetime_resolution(SNUM(conn))) {
1016 dos_filetime_timespec(&c_timespec);
1017 dos_filetime_timespec(&a_timespec);
1018 dos_filetime_timespec(&m_timespec);
1021 put_long_date_timespec(p, c_timespec); /* create time. */
1023 put_long_date_timespec(p, a_timespec); /* access time */
1025 put_long_date_timespec(p, m_timespec); /* write time */
1027 put_long_date_timespec(p, m_timespec); /* change time */
1029 SIVAL(p,0,fattr); /* File Attributes. */
1031 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1033 SOFF_T(p,0,file_len);
1035 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1039 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1041 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1044 if (fsp->is_directory
1045 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
1046 perms = FILE_GENERIC_ALL;
1048 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1053 DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1055 /* Send the required number of replies */
1056 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1061 /****************************************************************************
1062 Reply to a NT CANCEL request.
1063 conn POINTER CAN BE NULL HERE !
1064 ****************************************************************************/
1066 void reply_ntcancel(struct smb_request *req)
1069 * Go through and cancel any pending change notifies.
1072 START_PROFILE(SMBntcancel);
1073 remove_pending_change_notify_requests_by_mid(req->mid);
1074 remove_pending_lock_requests_by_mid(req->mid);
1075 srv_cancel_sign_response(req->mid);
1077 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1079 END_PROFILE(SMBntcancel);
1083 /****************************************************************************
1085 ****************************************************************************/
1087 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1088 connection_struct *conn,
1089 struct smb_request *req,
1090 const char *oldname_in,
1091 const char *newname_in,
1094 SMB_STRUCT_STAT sbuf1, sbuf2;
1095 char *oldname = NULL;
1096 char *newname = NULL;
1097 char *last_component_oldname = NULL;
1098 char *last_component_newname = NULL;
1099 files_struct *fsp1,*fsp2;
1103 NTSTATUS status = NT_STATUS_OK;
1108 if (!CAN_WRITE(conn)) {
1109 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1112 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1113 &last_component_oldname, &sbuf1);
1114 if (!NT_STATUS_IS_OK(status)) {
1118 status = check_name(conn, oldname);
1119 if (!NT_STATUS_IS_OK(status)) {
1123 /* Source must already exist. */
1124 if (!VALID_STAT(sbuf1)) {
1125 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1127 /* Ensure attributes match. */
1128 fattr = dos_mode(conn,oldname,&sbuf1);
1129 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1130 return NT_STATUS_NO_SUCH_FILE;
1133 status = unix_convert(ctx, conn, newname_in, False, &newname,
1134 &last_component_newname, &sbuf2);
1135 if (!NT_STATUS_IS_OK(status)) {
1139 status = check_name(conn, newname);
1140 if (!NT_STATUS_IS_OK(status)) {
1144 /* Disallow if newname already exists. */
1145 if (VALID_STAT(sbuf2)) {
1146 return NT_STATUS_OBJECT_NAME_COLLISION;
1149 /* No links from a directory. */
1150 if (S_ISDIR(sbuf1.st_mode)) {
1151 return NT_STATUS_FILE_IS_A_DIRECTORY;
1154 /* Ensure this is within the share. */
1155 status = check_reduced_name(conn, oldname);
1156 if (!NT_STATUS_IS_OK(status)) {
1160 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1163 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1164 FILE_READ_DATA, /* Read-only. */
1165 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1167 0, /* No create options. */
1168 FILE_ATTRIBUTE_NORMAL,
1172 if (!NT_STATUS_IS_OK(status)) {
1176 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1177 FILE_WRITE_DATA, /* Read-only. */
1178 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1180 0, /* No create options. */
1185 if (!NT_STATUS_IS_OK(status)) {
1186 close_file(fsp1,ERROR_CLOSE);
1190 if (sbuf1.st_size) {
1191 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1195 * As we are opening fsp1 read-only we only expect
1196 * an error on close on fsp2 if we are out of space.
1197 * Thus we don't look at the error return from the
1200 close_file(fsp1,NORMAL_CLOSE);
1202 /* Ensure the modtime is set correctly on the destination file. */
1203 set_close_write_time(fsp2, get_mtimespec(&sbuf1));
1205 status = close_file(fsp2,NORMAL_CLOSE);
1207 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1208 creates the file. This isn't the correct thing to do in the copy
1210 file_set_dosmode(conn, newname, fattr, &sbuf2,
1211 parent_dirname(newname),false);
1213 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1214 return NT_STATUS_DISK_FULL;
1217 if (!NT_STATUS_IS_OK(status)) {
1218 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1219 nt_errstr(status), oldname, newname));
1224 /****************************************************************************
1225 Reply to a NT rename request.
1226 ****************************************************************************/
1228 void reply_ntrename(struct smb_request *req)
1230 connection_struct *conn = req->conn;
1231 char *oldname = NULL;
1232 char *newname = NULL;
1235 bool src_has_wcard = False;
1236 bool dest_has_wcard = False;
1239 TALLOC_CTX *ctx = talloc_tos();
1241 START_PROFILE(SMBntrename);
1244 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1245 END_PROFILE(SMBntrename);
1249 attrs = SVAL(req->inbuf,smb_vwv0);
1250 rename_type = SVAL(req->inbuf,smb_vwv1);
1252 p = smb_buf(req->inbuf) + 1;
1253 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1254 0, STR_TERMINATE, &status,
1256 if (!NT_STATUS_IS_OK(status)) {
1257 reply_nterror(req, status);
1258 END_PROFILE(SMBntrename);
1262 if( is_ntfs_stream_name(oldname)) {
1263 /* Can't rename a stream. */
1264 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1265 END_PROFILE(SMBntrename);
1269 if (ms_has_wild(oldname)) {
1270 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1271 END_PROFILE(SMBntrename);
1276 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1277 0, STR_TERMINATE, &status,
1279 if (!NT_STATUS_IS_OK(status)) {
1280 reply_nterror(req, status);
1281 END_PROFILE(SMBntrename);
1285 status = resolve_dfspath(ctx, conn,
1286 req->flags2 & FLAGS2_DFS_PATHNAMES,
1289 if (!NT_STATUS_IS_OK(status)) {
1290 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1291 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1292 ERRSRV, ERRbadpath);
1293 END_PROFILE(SMBntrename);
1296 reply_nterror(req, status);
1297 END_PROFILE(SMBntrename);
1301 status = resolve_dfspath(ctx, conn,
1302 req->flags2 & FLAGS2_DFS_PATHNAMES,
1305 if (!NT_STATUS_IS_OK(status)) {
1306 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1307 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1308 ERRSRV, ERRbadpath);
1309 END_PROFILE(SMBntrename);
1312 reply_nterror(req, status);
1313 END_PROFILE(SMBntrename);
1317 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1319 switch(rename_type) {
1320 case RENAME_FLAG_RENAME:
1321 status = rename_internals(ctx, conn, req, oldname,
1322 newname, attrs, False, src_has_wcard,
1323 dest_has_wcard, DELETE_ACCESS);
1325 case RENAME_FLAG_HARD_LINK:
1326 if (src_has_wcard || dest_has_wcard) {
1328 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1330 status = hardlink_internals(ctx,
1336 case RENAME_FLAG_COPY:
1337 if (src_has_wcard || dest_has_wcard) {
1339 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1341 status = copy_internals(ctx, conn, req, oldname,
1345 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1346 status = NT_STATUS_INVALID_PARAMETER;
1349 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1353 if (!NT_STATUS_IS_OK(status)) {
1354 if (open_was_deferred(req->mid)) {
1355 /* We have re-scheduled this call. */
1356 END_PROFILE(SMBntrename);
1360 reply_nterror(req, status);
1361 END_PROFILE(SMBntrename);
1365 reply_outbuf(req, 0, 0);
1367 END_PROFILE(SMBntrename);
1371 /****************************************************************************
1372 Reply to a notify change - queue the request and
1373 don't allow a directory to be opened.
1374 ****************************************************************************/
1376 static void call_nt_transact_notify_change(connection_struct *conn,
1377 struct smb_request *req,
1381 uint32 parameter_count,
1382 char **ppdata, uint32 data_count,
1383 uint32 max_data_count,
1384 uint32 max_param_count)
1386 uint16 *setup = *ppsetup;
1392 if(setup_count < 6) {
1393 reply_doserror(req, ERRDOS, ERRbadfunc);
1397 fsp = file_fsp(SVAL(setup,4));
1398 filter = IVAL(setup, 0);
1399 recursive = (SVAL(setup, 6) != 0) ? True : False;
1401 DEBUG(3,("call_nt_transact_notify_change\n"));
1404 reply_doserror(req, ERRDOS, ERRbadfid);
1409 char *filter_string;
1411 if (!(filter_string = notify_filter_string(NULL, filter))) {
1412 reply_nterror(req,NT_STATUS_NO_MEMORY);
1416 DEBUG(3,("call_nt_transact_notify_change: notify change "
1417 "called on %s, filter = %s, recursive = %d\n",
1418 fsp->fsp_name, filter_string, recursive));
1420 TALLOC_FREE(filter_string);
1423 if((!fsp->is_directory) || (conn != fsp->conn)) {
1424 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1428 if (fsp->notify == NULL) {
1430 status = change_notify_create(fsp, filter, recursive);
1432 if (!NT_STATUS_IS_OK(status)) {
1433 DEBUG(10, ("change_notify_create returned %s\n",
1434 nt_errstr(status)));
1435 reply_nterror(req, status);
1440 if (fsp->notify->num_changes != 0) {
1443 * We've got changes pending, respond immediately
1447 * TODO: write a torture test to check the filtering behaviour
1451 change_notify_reply(fsp->conn, req->inbuf, max_param_count, fsp->notify);
1454 * change_notify_reply() above has independently sent its
1461 * No changes pending, queue the request
1464 status = change_notify_add_request(req,
1468 if (!NT_STATUS_IS_OK(status)) {
1469 reply_nterror(req, status);
1474 /****************************************************************************
1475 Reply to an NT transact rename command.
1476 ****************************************************************************/
1478 static void call_nt_transact_rename(connection_struct *conn,
1479 struct smb_request *req,
1480 uint16 **ppsetup, uint32 setup_count,
1481 char **ppparams, uint32 parameter_count,
1482 char **ppdata, uint32 data_count,
1483 uint32 max_data_count)
1485 char *params = *ppparams;
1486 char *new_name = NULL;
1487 files_struct *fsp = NULL;
1488 bool dest_has_wcard = False;
1490 TALLOC_CTX *ctx = talloc_tos();
1492 if(parameter_count < 5) {
1493 reply_doserror(req, ERRDOS, ERRbadfunc);
1497 fsp = file_fsp(SVAL(params, 0));
1498 if (!check_fsp(conn, req, fsp)) {
1501 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1502 parameter_count - 4,
1503 STR_TERMINATE, &status, &dest_has_wcard);
1504 if (!NT_STATUS_IS_OK(status)) {
1505 reply_nterror(req, status);
1510 * W2K3 ignores this request as the RAW-RENAME test
1511 * demonstrates, so we do.
1513 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1515 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1516 fsp->fsp_name, new_name));
1521 /******************************************************************************
1522 Fake up a completely empty SD.
1523 *******************************************************************************/
1525 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1529 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1531 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1532 return NT_STATUS_NO_MEMORY;
1535 return NT_STATUS_OK;
1538 /****************************************************************************
1539 Reply to query a security descriptor.
1540 ****************************************************************************/
1542 static void call_nt_transact_query_security_desc(connection_struct *conn,
1543 struct smb_request *req,
1547 uint32 parameter_count,
1550 uint32 max_data_count)
1552 char *params = *ppparams;
1553 char *data = *ppdata;
1554 SEC_DESC *psd = NULL;
1556 uint32 security_info_wanted;
1557 files_struct *fsp = NULL;
1561 if(parameter_count < 8) {
1562 reply_doserror(req, ERRDOS, ERRbadfunc);
1566 fsp = file_fsp(SVAL(params,0));
1568 reply_doserror(req, ERRDOS, ERRbadfid);
1572 security_info_wanted = IVAL(params,4);
1574 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1575 (unsigned int)security_info_wanted ));
1577 params = nttrans_realloc(ppparams, 4);
1578 if(params == NULL) {
1579 reply_doserror(req, ERRDOS, ERRnomem);
1584 * Get the permissions to return.
1587 if (!lp_nt_acl_support(SNUM(conn))) {
1588 status = get_null_nt_acl(talloc_tos(), &psd);
1590 status = SMB_VFS_FGET_NT_ACL(
1591 fsp, security_info_wanted, &psd);
1594 if (!NT_STATUS_IS_OK(status)) {
1595 reply_nterror(req, status);
1599 sd_size = ndr_size_security_descriptor(psd, 0);
1601 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1603 SIVAL(params,0,(uint32)sd_size);
1605 if (max_data_count < sd_size) {
1606 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1607 params, 4, *ppdata, 0);
1612 * Allocate the data we will point this at.
1615 data = nttrans_realloc(ppdata, sd_size);
1617 reply_doserror(req, ERRDOS, ERRnomem);
1621 status = marshall_sec_desc(talloc_tos(), psd,
1622 &blob.data, &blob.length);
1624 if (!NT_STATUS_IS_OK(status)) {
1625 reply_nterror(req, status);
1629 SMB_ASSERT(sd_size == blob.length);
1630 memcpy(data, blob.data, sd_size);
1632 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1637 /****************************************************************************
1638 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1639 ****************************************************************************/
1641 static void call_nt_transact_set_security_desc(connection_struct *conn,
1642 struct smb_request *req,
1646 uint32 parameter_count,
1649 uint32 max_data_count)
1651 char *params= *ppparams;
1652 char *data = *ppdata;
1653 files_struct *fsp = NULL;
1654 uint32 security_info_sent = 0;
1657 if(parameter_count < 8) {
1658 reply_doserror(req, ERRDOS, ERRbadfunc);
1662 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
1663 reply_doserror(req, ERRDOS, ERRbadfid);
1667 if(!lp_nt_acl_support(SNUM(conn))) {
1671 security_info_sent = IVAL(params,4);
1673 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1674 (unsigned int)security_info_sent ));
1676 if (data_count == 0) {
1677 reply_doserror(req, ERRDOS, ERRnoaccess);
1681 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1683 if (!NT_STATUS_IS_OK(status)) {
1684 reply_nterror(req, status);
1689 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1693 /****************************************************************************
1695 ****************************************************************************/
1697 static void call_nt_transact_ioctl(connection_struct *conn,
1698 struct smb_request *req,
1699 uint16 **ppsetup, uint32 setup_count,
1700 char **ppparams, uint32 parameter_count,
1701 char **ppdata, uint32 data_count,
1702 uint32 max_data_count)
1709 static bool logged_message;
1710 char *pdata = *ppdata;
1712 if (setup_count != 8) {
1713 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1714 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1718 function = IVAL(*ppsetup, 0);
1719 fidnum = SVAL(*ppsetup, 4);
1720 isFSctl = CVAL(*ppsetup, 6);
1721 compfilter = CVAL(*ppsetup, 7);
1723 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1724 function, fidnum, isFSctl, compfilter));
1726 fsp=file_fsp(fidnum);
1727 /* this check is done in each implemented function case for now
1728 because I don't want to break anything... --metze
1729 FSP_BELONGS_CONN(fsp,conn);*/
1732 case FSCTL_SET_SPARSE:
1733 /* pretend this succeeded - tho strictly we should
1734 mark the file sparse (if the local fs supports it)
1735 so we can know if we need to pre-allocate or not */
1737 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1738 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1741 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1743 unsigned char objid[16];
1745 /* This should return the object-id on this file.
1746 * I think I'll make this be the inode+dev. JRA.
1749 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1751 if (!fsp_belongs_conn(conn, req, fsp)) {
1756 pdata = nttrans_realloc(ppdata, data_count);
1757 if (pdata == NULL) {
1758 reply_nterror(req, NT_STATUS_NO_MEMORY);
1761 push_file_id_16(pdata, &fsp->file_id);
1762 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1763 push_file_id_16(pdata+32, &fsp->file_id);
1764 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1769 case FSCTL_GET_REPARSE_POINT:
1770 /* pretend this fail - my winXP does it like this
1774 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1775 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1778 case FSCTL_SET_REPARSE_POINT:
1779 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1783 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1784 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1787 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1790 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1791 * and return their volume names. If max_data_count is 16, then it is just
1792 * asking for the number of volumes and length of the combined names.
1794 * pdata is the data allocated by our caller, but that uses
1795 * total_data_count (which is 0 in our case) rather than max_data_count.
1796 * Allocate the correct amount and return the pointer to let
1797 * it be deallocated when we return.
1799 SHADOW_COPY_DATA *shadow_data = NULL;
1800 TALLOC_CTX *shadow_mem_ctx = NULL;
1801 bool labels = False;
1802 uint32 labels_data_count = 0;
1806 if (!fsp_belongs_conn(conn, req, fsp)) {
1810 if (max_data_count < 16) {
1811 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1813 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1817 if (max_data_count > 16) {
1821 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1822 if (shadow_mem_ctx == NULL) {
1823 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1824 reply_nterror(req, NT_STATUS_NO_MEMORY);
1828 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1829 if (shadow_data == NULL) {
1830 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1831 talloc_destroy(shadow_mem_ctx);
1832 reply_nterror(req, NT_STATUS_NO_MEMORY);
1836 shadow_data->mem_ctx = shadow_mem_ctx;
1839 * Call the VFS routine to actually do the work.
1841 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1842 talloc_destroy(shadow_data->mem_ctx);
1843 if (errno == ENOSYS) {
1844 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1845 conn->connectpath));
1846 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1849 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1850 conn->connectpath));
1851 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1856 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1861 data_count = 12+labels_data_count+4;
1864 if (max_data_count<data_count) {
1865 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1866 max_data_count,data_count));
1867 talloc_destroy(shadow_data->mem_ctx);
1868 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1872 pdata = nttrans_realloc(ppdata, data_count);
1873 if (pdata == NULL) {
1874 talloc_destroy(shadow_data->mem_ctx);
1875 reply_nterror(req, NT_STATUS_NO_MEMORY);
1881 /* num_volumes 4 bytes */
1882 SIVAL(pdata,0,shadow_data->num_volumes);
1885 /* num_labels 4 bytes */
1886 SIVAL(pdata,4,shadow_data->num_volumes);
1889 /* needed_data_count 4 bytes */
1890 SIVAL(pdata,8,labels_data_count);
1894 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1895 shadow_data->num_volumes,fsp->fsp_name));
1896 if (labels && shadow_data->labels) {
1897 for (i=0;i<shadow_data->num_volumes;i++) {
1898 srvstr_push(pdata, req->flags2,
1899 cur_pdata, shadow_data->labels[i],
1900 2*sizeof(SHADOW_COPY_LABEL),
1901 STR_UNICODE|STR_TERMINATE);
1902 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1903 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1907 talloc_destroy(shadow_data->mem_ctx);
1909 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1915 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1917 /* pretend this succeeded -
1919 * we have to send back a list with all files owned by this SID
1921 * but I have to check that --metze
1925 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1927 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1929 if (!fsp_belongs_conn(conn, req, fsp)) {
1933 /* unknown 4 bytes: this is not the length of the sid :-( */
1934 /*unknown = IVAL(pdata,0);*/
1936 sid_parse(pdata+4,sid_len,&sid);
1937 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
1939 if (!sid_to_uid(&sid, &uid)) {
1940 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
1941 sid_string_dbg(&sid),
1942 (unsigned long)sid_len));
1946 /* we can take a look at the find source :-)
1948 * find ./ -uid $uid -name '*' is what we need here
1951 * and send 4bytes len and then NULL terminated unicode strings
1954 * but I don't know how to deal with the paged results
1955 * (maybe we can hang the result anywhere in the fsp struct)
1957 * we don't send all files at once
1958 * and at the next we should *not* start from the beginning,
1959 * so we have to cache the result
1964 /* this works for now... */
1965 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1969 if (!logged_message) {
1970 logged_message = True; /* Only print this once... */
1971 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
1976 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1980 #ifdef HAVE_SYS_QUOTAS
1981 /****************************************************************************
1982 Reply to get user quota
1983 ****************************************************************************/
1985 static void call_nt_transact_get_user_quota(connection_struct *conn,
1986 struct smb_request *req,
1990 uint32 parameter_count,
1993 uint32 max_data_count)
1995 NTSTATUS nt_status = NT_STATUS_OK;
1996 char *params = *ppparams;
1997 char *pdata = *ppdata;
1999 int data_len=0,param_len=0;
2002 files_struct *fsp = NULL;
2006 bool start_enum = True;
2007 SMB_NTQUOTA_STRUCT qt;
2008 SMB_NTQUOTA_LIST *tmp_list;
2009 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2014 if (conn->server_info->utok.uid != 0) {
2015 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2016 "[%s]\n", lp_servicename(SNUM(conn)),
2017 conn->server_info->unix_name));
2018 reply_doserror(req, ERRDOS, ERRnoaccess);
2023 * Ensure minimum number of parameters sent.
2026 if (parameter_count < 4) {
2027 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2028 reply_doserror(req, ERRDOS, ERRinvalidparam);
2032 /* maybe we can check the quota_fnum */
2033 fsp = file_fsp(SVAL(params,0));
2034 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2035 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2036 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2040 /* the NULL pointer checking for fsp->fake_file_handle->pd
2041 * is done by CHECK_NTQUOTA_HANDLE_OK()
2043 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2045 level = SVAL(params,2);
2047 /* unknown 12 bytes leading in params */
2050 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2051 /* seems that we should continue with the enum here --metze */
2053 if (qt_handle->quota_list!=NULL &&
2054 qt_handle->tmp_list==NULL) {
2057 free_ntquota_list(&(qt_handle->quota_list));
2059 /* Realloc the size of parameters and data we will return */
2061 params = nttrans_realloc(ppparams, param_len);
2062 if(params == NULL) {
2063 reply_doserror(req, ERRDOS, ERRnomem);
2068 SIVAL(params,0,data_len);
2075 case TRANSACT_GET_USER_QUOTA_LIST_START:
2077 if (qt_handle->quota_list==NULL &&
2078 qt_handle->tmp_list==NULL) {
2082 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2083 reply_doserror(req, ERRSRV, ERRerror);
2087 /* Realloc the size of parameters and data we will return */
2089 params = nttrans_realloc(ppparams, param_len);
2090 if(params == NULL) {
2091 reply_doserror(req, ERRDOS, ERRnomem);
2095 /* we should not trust the value in max_data_count*/
2096 max_data_count = MIN(max_data_count,2048);
2098 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2100 reply_doserror(req, ERRDOS, ERRnomem);
2106 /* set params Size of returned Quota Data 4 bytes*/
2107 /* but set it later when we know it */
2109 /* for each entry push the data */
2112 qt_handle->tmp_list = qt_handle->quota_list;
2115 tmp_list = qt_handle->tmp_list;
2117 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2118 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2120 sid_len = ndr_size_dom_sid(
2121 &tmp_list->quotas->sid, 0);
2122 entry_len = 40 + sid_len;
2124 /* nextoffset entry 4 bytes */
2125 SIVAL(entry,0,entry_len);
2127 /* then the len of the SID 4 bytes */
2128 SIVAL(entry,4,sid_len);
2130 /* unknown data 8 bytes SMB_BIG_UINT */
2131 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2133 /* the used disk space 8 bytes SMB_BIG_UINT */
2134 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2136 /* the soft quotas 8 bytes SMB_BIG_UINT */
2137 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2139 /* the hard quotas 8 bytes SMB_BIG_UINT */
2140 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2142 /* and now the SID */
2143 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2146 qt_handle->tmp_list = tmp_list;
2148 /* overwrite the offset of the last entry */
2149 SIVAL(entry-entry_len,0,0);
2151 data_len = 4+qt_len;
2152 /* overwrite the params quota_data_len */
2153 SIVAL(params,0,data_len);
2157 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2159 /* unknown 4 bytes IVAL(pdata,0) */
2161 if (data_count < 8) {
2162 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2163 reply_doserror(req, ERRDOS, ERRunknownlevel);
2167 sid_len = IVAL(pdata,4);
2168 /* Ensure this is less than 1mb. */
2169 if (sid_len > (1024*1024)) {
2170 reply_doserror(req, ERRDOS, ERRnomem);
2174 if (data_count < 8+sid_len) {
2175 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2176 reply_doserror(req, ERRDOS, ERRunknownlevel);
2180 data_len = 4+40+sid_len;
2182 if (max_data_count < data_len) {
2183 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2184 max_data_count, data_len));
2186 SIVAL(params,0,data_len);
2188 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2192 sid_parse(pdata+8,sid_len,&sid);
2194 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2197 * we have to return zero's in all fields
2198 * instead of returning an error here
2203 /* Realloc the size of parameters and data we will return */
2205 params = nttrans_realloc(ppparams, param_len);
2206 if(params == NULL) {
2207 reply_doserror(req, ERRDOS, ERRnomem);
2211 pdata = nttrans_realloc(ppdata, data_len);
2213 reply_doserror(req, ERRDOS, ERRnomem);
2219 /* set params Size of returned Quota Data 4 bytes*/
2220 SIVAL(params,0,data_len);
2222 /* nextoffset entry 4 bytes */
2225 /* then the len of the SID 4 bytes */
2226 SIVAL(entry,4,sid_len);
2228 /* unknown data 8 bytes SMB_BIG_UINT */
2229 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2231 /* the used disk space 8 bytes SMB_BIG_UINT */
2232 SBIG_UINT(entry,16,qt.usedspace);
2234 /* the soft quotas 8 bytes SMB_BIG_UINT */
2235 SBIG_UINT(entry,24,qt.softlim);
2237 /* the hard quotas 8 bytes SMB_BIG_UINT */
2238 SBIG_UINT(entry,32,qt.hardlim);
2240 /* and now the SID */
2241 sid_linearize(entry+40, sid_len, &sid);
2246 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2247 reply_doserror(req, ERRSRV, ERRerror);
2252 send_nt_replies(conn, req, nt_status, params, param_len,
2256 /****************************************************************************
2257 Reply to set user quota
2258 ****************************************************************************/
2260 static void call_nt_transact_set_user_quota(connection_struct *conn,
2261 struct smb_request *req,
2265 uint32 parameter_count,
2268 uint32 max_data_count)
2270 char *params = *ppparams;
2271 char *pdata = *ppdata;
2272 int data_len=0,param_len=0;
2273 SMB_NTQUOTA_STRUCT qt;
2276 files_struct *fsp = NULL;
2281 if (conn->server_info->utok.uid != 0) {
2282 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2283 "[%s]\n", lp_servicename(SNUM(conn)),
2284 conn->server_info->unix_name));
2285 reply_doserror(req, ERRDOS, ERRnoaccess);
2290 * Ensure minimum number of parameters sent.
2293 if (parameter_count < 2) {
2294 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2295 reply_doserror(req, ERRDOS, ERRinvalidparam);
2299 /* maybe we can check the quota_fnum */
2300 fsp = file_fsp(SVAL(params,0));
2301 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2302 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2303 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2307 if (data_count < 40) {
2308 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2309 reply_doserror(req, ERRDOS, ERRunknownlevel);
2313 /* offset to next quota record.
2314 * 4 bytes IVAL(pdata,0)
2319 sid_len = IVAL(pdata,4);
2321 if (data_count < 40+sid_len) {
2322 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2323 reply_doserror(req, ERRDOS, ERRunknownlevel);
2327 /* unknown 8 bytes in pdata
2328 * maybe its the change time in NTTIME
2331 /* the used space 8 bytes (SMB_BIG_UINT)*/
2332 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2333 #ifdef LARGE_SMB_OFF_T
2334 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2335 #else /* LARGE_SMB_OFF_T */
2336 if ((IVAL(pdata,20) != 0)&&
2337 ((qt.usedspace != 0xFFFFFFFF)||
2338 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2339 /* more than 32 bits? */
2340 reply_doserror(req, ERRDOS, ERRunknownlevel);
2343 #endif /* LARGE_SMB_OFF_T */
2345 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2346 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2347 #ifdef LARGE_SMB_OFF_T
2348 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2349 #else /* LARGE_SMB_OFF_T */
2350 if ((IVAL(pdata,28) != 0)&&
2351 ((qt.softlim != 0xFFFFFFFF)||
2352 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2353 /* more than 32 bits? */
2354 reply_doserror(req, ERRDOS, ERRunknownlevel);
2357 #endif /* LARGE_SMB_OFF_T */
2359 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2360 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2361 #ifdef LARGE_SMB_OFF_T
2362 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2363 #else /* LARGE_SMB_OFF_T */
2364 if ((IVAL(pdata,36) != 0)&&
2365 ((qt.hardlim != 0xFFFFFFFF)||
2366 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2367 /* more than 32 bits? */
2368 reply_doserror(req, ERRDOS, ERRunknownlevel);
2371 #endif /* LARGE_SMB_OFF_T */
2373 sid_parse(pdata+40,sid_len,&sid);
2374 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2376 /* 44 unknown bytes left... */
2378 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2379 reply_doserror(req, ERRSRV, ERRerror);
2383 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2386 #endif /* HAVE_SYS_QUOTAS */
2388 static void handle_nttrans(connection_struct *conn,
2389 struct trans_state *state,
2390 struct smb_request *req)
2392 if (Protocol >= PROTOCOL_NT1) {
2393 req->flags2 |= 0x40; /* IS_LONG_NAME */
2394 SSVAL(req->inbuf,smb_flg2,req->flags2);
2397 /* Now we must call the relevant NT_TRANS function */
2398 switch(state->call) {
2399 case NT_TRANSACT_CREATE:
2401 START_PROFILE(NT_transact_create);
2402 call_nt_transact_create(
2404 &state->setup, state->setup_count,
2405 &state->param, state->total_param,
2406 &state->data, state->total_data,
2407 state->max_data_return);
2408 END_PROFILE(NT_transact_create);
2412 case NT_TRANSACT_IOCTL:
2414 START_PROFILE(NT_transact_ioctl);
2415 call_nt_transact_ioctl(
2417 &state->setup, state->setup_count,
2418 &state->param, state->total_param,
2419 &state->data, state->total_data,
2420 state->max_data_return);
2421 END_PROFILE(NT_transact_ioctl);
2425 case NT_TRANSACT_SET_SECURITY_DESC:
2427 START_PROFILE(NT_transact_set_security_desc);
2428 call_nt_transact_set_security_desc(
2430 &state->setup, state->setup_count,
2431 &state->param, state->total_param,
2432 &state->data, state->total_data,
2433 state->max_data_return);
2434 END_PROFILE(NT_transact_set_security_desc);
2438 case NT_TRANSACT_NOTIFY_CHANGE:
2440 START_PROFILE(NT_transact_notify_change);
2441 call_nt_transact_notify_change(
2443 &state->setup, state->setup_count,
2444 &state->param, state->total_param,
2445 &state->data, state->total_data,
2446 state->max_data_return,
2447 state->max_param_return);
2448 END_PROFILE(NT_transact_notify_change);
2452 case NT_TRANSACT_RENAME:
2454 START_PROFILE(NT_transact_rename);
2455 call_nt_transact_rename(
2457 &state->setup, state->setup_count,
2458 &state->param, state->total_param,
2459 &state->data, state->total_data,
2460 state->max_data_return);
2461 END_PROFILE(NT_transact_rename);
2465 case NT_TRANSACT_QUERY_SECURITY_DESC:
2467 START_PROFILE(NT_transact_query_security_desc);
2468 call_nt_transact_query_security_desc(
2470 &state->setup, state->setup_count,
2471 &state->param, state->total_param,
2472 &state->data, state->total_data,
2473 state->max_data_return);
2474 END_PROFILE(NT_transact_query_security_desc);
2478 #ifdef HAVE_SYS_QUOTAS
2479 case NT_TRANSACT_GET_USER_QUOTA:
2481 START_PROFILE(NT_transact_get_user_quota);
2482 call_nt_transact_get_user_quota(
2484 &state->setup, state->setup_count,
2485 &state->param, state->total_param,
2486 &state->data, state->total_data,
2487 state->max_data_return);
2488 END_PROFILE(NT_transact_get_user_quota);
2492 case NT_TRANSACT_SET_USER_QUOTA:
2494 START_PROFILE(NT_transact_set_user_quota);
2495 call_nt_transact_set_user_quota(
2497 &state->setup, state->setup_count,
2498 &state->param, state->total_param,
2499 &state->data, state->total_data,
2500 state->max_data_return);
2501 END_PROFILE(NT_transact_set_user_quota);
2504 #endif /* HAVE_SYS_QUOTAS */
2507 /* Error in request */
2508 DEBUG(0,("handle_nttrans: Unknown request %d in "
2509 "nttrans call\n", state->call));
2510 reply_doserror(req, ERRSRV, ERRerror);
2516 /****************************************************************************
2517 Reply to a SMBNTtrans.
2518 ****************************************************************************/
2520 void reply_nttrans(struct smb_request *req)
2522 connection_struct *conn = req->conn;
2527 uint16 function_code;
2529 struct trans_state *state;
2533 START_PROFILE(SMBnttrans);
2535 if (req->wct < 19) {
2536 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2537 END_PROFILE(SMBnttrans);
2541 size = smb_len(req->inbuf) + 4;
2542 av_size = smb_len(req->inbuf);
2543 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
2544 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
2545 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
2546 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
2547 function_code = SVAL(req->inbuf, smb_nt_Function);
2549 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2550 reply_doserror(req, ERRSRV, ERRaccess);
2551 END_PROFILE(SMBnttrans);
2555 result = allow_new_trans(conn->pending_trans, req->mid);
2556 if (!NT_STATUS_IS_OK(result)) {
2557 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2558 reply_nterror(req, result);
2559 END_PROFILE(SMBnttrans);
2563 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2564 reply_doserror(req, ERRSRV, ERRaccess);
2565 END_PROFILE(SMBnttrans);
2569 state->cmd = SMBnttrans;
2571 state->mid = req->mid;
2572 state->vuid = req->vuid;
2573 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
2575 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
2576 state->param = NULL;
2577 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
2578 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
2580 /* setup count is in *words* */
2581 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
2582 state->setup = NULL;
2583 state->call = function_code;
2585 DEBUG(10, ("num_setup=%u, "
2586 "param_total=%u, this_param=%u, max_param=%u, "
2587 "data_total=%u, this_data=%u, max_data=%u, "
2588 "param_offset=%u, data_offset=%u\n",
2589 (unsigned)state->setup_count,
2590 (unsigned)state->total_param, (unsigned)pscnt,
2591 (unsigned)state->max_param_return,
2592 (unsigned)state->total_data, (unsigned)dscnt,
2593 (unsigned)state->max_data_return,
2594 (unsigned)psoff, (unsigned)dsoff));
2597 * All nttrans messages we handle have smb_wct == 19 +
2598 * state->setup_count. Ensure this is so as a sanity check.
2601 if(req->wct != 19 + (state->setup_count/2)) {
2602 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2603 req->wct, 19 + (state->setup_count/2)));
2607 /* Don't allow more than 128mb for each value. */
2608 if ((state->total_data > (1024*1024*128)) ||
2609 (state->total_param > (1024*1024*128))) {
2610 reply_doserror(req, ERRDOS, ERRnomem);
2611 END_PROFILE(SMBnttrans);
2615 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2618 if (state->total_data) {
2619 /* Can't use talloc here, the core routines do realloc on the
2620 * params and data. */
2621 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2622 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2623 "bytes !\n", (unsigned int)state->total_data));
2625 reply_doserror(req, ERRDOS, ERRnomem);
2626 END_PROFILE(SMBnttrans);
2630 if (dscnt > state->total_data ||
2631 dsoff+dscnt < dsoff) {
2635 if (dsoff > av_size ||
2637 dsoff+dscnt > av_size) {
2641 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2644 if (state->total_param) {
2645 /* Can't use talloc here, the core routines do realloc on the
2646 * params and data. */
2647 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2648 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2649 "bytes !\n", (unsigned int)state->total_param));
2650 SAFE_FREE(state->data);
2652 reply_doserror(req, ERRDOS, ERRnomem);
2653 END_PROFILE(SMBnttrans);
2657 if (pscnt > state->total_param ||
2658 psoff+pscnt < psoff) {
2662 if (psoff > av_size ||
2664 psoff+pscnt > av_size) {
2668 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2671 state->received_data = dscnt;
2672 state->received_param = pscnt;
2674 if(state->setup_count > 0) {
2675 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2676 state->setup_count));
2677 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2678 if (state->setup == NULL) {
2679 DEBUG(0,("reply_nttrans : Out of memory\n"));
2680 SAFE_FREE(state->data);
2681 SAFE_FREE(state->param);
2683 reply_doserror(req, ERRDOS, ERRnomem);
2684 END_PROFILE(SMBnttrans);
2688 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2689 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2692 if (smb_nt_SetupStart + state->setup_count > size) {
2696 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
2697 state->setup_count);
2698 dump_data(10, (uint8 *)state->setup, state->setup_count);
2701 if ((state->received_data == state->total_data) &&
2702 (state->received_param == state->total_param)) {
2703 handle_nttrans(conn, state, req);
2704 SAFE_FREE(state->param);
2705 SAFE_FREE(state->data);
2707 END_PROFILE(SMBnttrans);
2711 DLIST_ADD(conn->pending_trans, state);
2713 /* We need to send an interim response then receive the rest
2714 of the parameter/data bytes */
2715 reply_outbuf(req, 0, 0);
2716 show_msg((char *)req->outbuf);
2717 END_PROFILE(SMBnttrans);
2722 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2723 SAFE_FREE(state->data);
2724 SAFE_FREE(state->param);
2726 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2727 END_PROFILE(SMBnttrans);
2731 /****************************************************************************
2732 Reply to a SMBnttranss
2733 ****************************************************************************/
2735 void reply_nttranss(struct smb_request *req)
2737 connection_struct *conn = req->conn;
2738 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2739 struct trans_state *state;
2743 START_PROFILE(SMBnttranss);
2745 show_msg((char *)req->inbuf);
2747 if (req->wct < 18) {
2748 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2749 END_PROFILE(SMBnttranss);
2753 for (state = conn->pending_trans; state != NULL;
2754 state = state->next) {
2755 if (state->mid == req->mid) {
2760 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2761 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2762 END_PROFILE(SMBnttranss);
2766 /* Revise state->total_param and state->total_data in case they have
2767 changed downwards */
2768 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
2769 < state->total_param) {
2770 state->total_param = IVAL(req->inbuf,
2771 smb_nts_TotalParameterCount);
2773 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
2774 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
2777 size = smb_len(req->inbuf) + 4;
2778 av_size = smb_len(req->inbuf);
2780 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
2781 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
2782 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
2784 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
2785 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
2786 doff = IVAL(req->inbuf, smb_nts_DataOffset);
2788 state->received_param += pcnt;
2789 state->received_data += dcnt;
2791 if ((state->received_data > state->total_data) ||
2792 (state->received_param > state->total_param))
2796 if (pdisp > state->total_param ||
2797 pcnt > state->total_param ||
2798 pdisp+pcnt > state->total_param ||
2799 pdisp+pcnt < pdisp) {
2803 if (poff > av_size ||
2805 poff+pcnt > av_size ||
2810 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2815 if (ddisp > state->total_data ||
2816 dcnt > state->total_data ||
2817 ddisp+dcnt > state->total_data ||
2818 ddisp+dcnt < ddisp) {
2822 if (ddisp > av_size ||
2824 ddisp+dcnt > av_size ||
2825 ddisp+dcnt < ddisp) {
2829 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2833 if ((state->received_param < state->total_param) ||
2834 (state->received_data < state->total_data)) {
2835 END_PROFILE(SMBnttranss);
2840 * construct_reply_common will copy smb_com from inbuf to
2841 * outbuf. SMBnttranss is wrong here.
2843 SCVAL(req->inbuf,smb_com,SMBnttrans);
2845 handle_nttrans(conn, state, req);
2847 DLIST_REMOVE(conn->pending_trans, state);
2848 SAFE_FREE(state->data);
2849 SAFE_FREE(state->param);
2851 END_PROFILE(SMBnttranss);
2856 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2857 DLIST_REMOVE(conn->pending_trans, state);
2858 SAFE_FREE(state->data);
2859 SAFE_FREE(state->param);
2861 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2862 END_PROFILE(SMBnttranss);