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;
25 extern const struct generic_mapping file_generic_mapping;
27 static char *nttrans_realloc(char **ptr, size_t size)
30 smb_panic("nttrans_realloc() called with NULL ptr");
33 *ptr = (char *)SMB_REALLOC(*ptr, size);
37 memset(*ptr,'\0',size);
41 /****************************************************************************
42 Send the required number of replies back.
43 We assume all fields other than the data fields are
44 set correctly for the type of call.
45 HACK ! Always assumes smb_setup field is zero.
46 ****************************************************************************/
48 void send_nt_replies(connection_struct *conn,
49 struct smb_request *req, NTSTATUS nt_error,
50 char *params, int paramsize,
51 char *pdata, int datasize)
53 int data_to_send = datasize;
54 int params_to_send = paramsize;
58 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
59 int alignment_offset = 3;
60 int data_alignment_offset = 0;
63 * If there genuinely are no parameters or data to send just send
67 if(params_to_send == 0 && data_to_send == 0) {
68 reply_outbuf(req, 18, 0);
69 show_msg((char *)req->outbuf);
74 * When sending params and data ensure that both are nicely aligned.
75 * Only do this alignment when there is also data to send - else
76 * can cause NT redirector problems.
79 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
80 data_alignment_offset = 4 - (params_to_send % 4);
84 * Space is bufsize minus Netbios over TCP header minus SMB header.
85 * The alignment_offset is to align the param bytes on a four byte
86 * boundary (2 bytes for data len, one byte pad).
87 * NT needs this to work correctly.
90 useable_space = max_send - (smb_size
93 + data_alignment_offset);
95 if (useable_space < 0) {
96 char *msg = talloc_asprintf(
98 "send_nt_replies failed sanity useable_space = %d!!!",
100 DEBUG(0, ("%s\n", msg));
101 exit_server_cleanly(msg);
104 while (params_to_send || data_to_send) {
107 * Calculate whether we will totally or partially fill this packet.
110 total_sent_thistime = params_to_send + data_to_send;
113 * We can never send more than useable_space.
116 total_sent_thistime = MIN(total_sent_thistime, useable_space);
118 reply_outbuf(req, 18,
119 total_sent_thistime + alignment_offset
120 + data_alignment_offset);
123 * Set total params and data to be sent.
126 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
127 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
130 * Calculate how many parameters and data we can fit into
131 * this packet. Parameters get precedence.
134 params_sent_thistime = MIN(params_to_send,useable_space);
135 data_sent_thistime = useable_space - params_sent_thistime;
136 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
138 SIVAL(req->outbuf, smb_ntr_ParameterCount,
139 params_sent_thistime);
141 if(params_sent_thistime == 0) {
142 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
143 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
146 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
147 * parameter bytes, however the first 4 bytes of outbuf are
148 * the Netbios over TCP header. Thus use smb_base() to subtract
149 * them from the calculation.
152 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
153 ((smb_buf(req->outbuf)+alignment_offset)
154 - smb_base(req->outbuf)));
156 * Absolute displacement of param bytes sent in this packet.
159 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
164 * Deal with the data portion.
167 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
169 if(data_sent_thistime == 0) {
170 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
171 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
174 * The offset of the data bytes is the offset of the
175 * parameter bytes plus the number of parameters being sent this time.
178 SIVAL(req->outbuf, smb_ntr_DataOffset,
179 ((smb_buf(req->outbuf)+alignment_offset) -
180 smb_base(req->outbuf))
181 + params_sent_thistime + data_alignment_offset);
182 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
186 * Copy the param bytes into the packet.
189 if(params_sent_thistime) {
190 if (alignment_offset != 0) {
191 memset(smb_buf(req->outbuf), 0,
194 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
195 params_sent_thistime);
199 * Copy in the data bytes
202 if(data_sent_thistime) {
203 if (data_alignment_offset != 0) {
204 memset((smb_buf(req->outbuf)+alignment_offset+
205 params_sent_thistime), 0,
206 data_alignment_offset);
208 memcpy(smb_buf(req->outbuf)+alignment_offset
209 +params_sent_thistime+data_alignment_offset,
210 pd,data_sent_thistime);
213 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
214 params_sent_thistime, data_sent_thistime, useable_space));
215 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
216 params_to_send, data_to_send, paramsize, datasize));
218 if (NT_STATUS_V(nt_error)) {
219 error_packet_set((char *)req->outbuf,
224 /* Send the packet */
225 show_msg((char *)req->outbuf);
226 if (!srv_send_smb(smbd_server_fd(),
228 IS_CONN_ENCRYPTED(conn))) {
229 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
232 TALLOC_FREE(req->outbuf);
234 pp += params_sent_thistime;
235 pd += data_sent_thistime;
237 params_to_send -= params_sent_thistime;
238 data_to_send -= data_sent_thistime;
244 if(params_to_send < 0 || data_to_send < 0) {
245 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
246 params_to_send, data_to_send));
247 exit_server_cleanly("send_nt_replies: internal error");
252 /****************************************************************************
253 Is it an NTFS stream name ?
254 An NTFS file name is <path>.<extention>:<stream name>:<stream type>
255 $DATA can be used as both a stream name and a stream type. A missing stream
256 name or type implies $DATA.
257 ****************************************************************************/
259 bool is_ntfs_stream_name(const char *fname)
261 if (lp_posix_pathnames()) {
264 return (strchr_m(fname, ':') != NULL) ? True : False;
267 /****************************************************************************
268 Reply to an NT create and X call on a pipe
269 ****************************************************************************/
271 static void nt_open_pipe(char *fname, connection_struct *conn,
272 struct smb_request *req, int *ppnum)
277 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
279 /* See if it is one we want to handle. */
281 if (!is_known_pipename(fname)) {
282 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
287 /* Strip \\ off the name. */
290 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
292 status = np_open(req, conn, fname, &fsp);
293 if (!NT_STATUS_IS_OK(status)) {
294 reply_nterror(req, status);
302 /****************************************************************************
303 Reply to an NT create and X call for pipes.
304 ****************************************************************************/
306 static void do_ntcreate_pipe_open(connection_struct *conn,
307 struct smb_request *req)
312 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
313 TALLOC_CTX *ctx = talloc_tos();
315 srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
316 smb_buf(req->inbuf), STR_TERMINATE);
319 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
323 nt_open_pipe(fname, conn, req, &pnum);
331 * Deal with pipe return.
334 if (flags & EXTENDED_RESPONSE_REQUIRED) {
335 /* This is very strange. We
336 * return 50 words, but only set
337 * the wcnt to 42 ? It's definately
338 * what happens on the wire....
340 reply_outbuf(req, 50, 0);
341 SCVAL(req->outbuf,smb_wct,42);
343 reply_outbuf(req, 34, 0);
346 p = (char *)req->outbuf + smb_vwv2;
350 SIVAL(p,0,FILE_WAS_OPENED);
353 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
356 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
358 SSVAL(p,2, 0x5FF); /* ? */
361 if (flags & EXTENDED_RESPONSE_REQUIRED) {
363 SIVAL(p,0,FILE_GENERIC_ALL);
365 * For pipes W2K3 seems to return
367 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
369 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
372 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
377 /****************************************************************************
378 Reply to an NT create and X call.
379 ****************************************************************************/
381 void reply_ntcreate_and_X(struct smb_request *req)
383 connection_struct *conn = req->conn;
387 uint32 file_attributes;
389 uint32 create_disposition;
390 uint32 create_options;
392 uint64_t allocation_size;
393 /* Breakout the oplock request bits so we can set the
394 reply bits separately. */
396 SMB_OFF_T file_len = 0;
397 SMB_STRUCT_STAT sbuf;
399 files_struct *fsp = NULL;
401 struct timespec c_timespec;
402 struct timespec a_timespec;
403 struct timespec m_timespec;
406 uint8_t oplock_granted = NO_OPLOCK_RETURN;
407 TALLOC_CTX *ctx = talloc_tos();
409 START_PROFILE(SMBntcreateX);
412 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
416 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
417 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
418 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
419 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
420 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
421 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
422 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
424 allocation_size = (uint64_t)IVAL(req->inbuf,
425 smb_ntcreate_AllocationSize);
426 #ifdef LARGE_SMB_OFF_T
427 allocation_size |= (((uint64_t)IVAL(
429 smb_ntcreate_AllocationSize + 4)) << 32);
432 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
433 smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
435 if (!NT_STATUS_IS_OK(status)) {
436 reply_nterror(req, status);
437 END_PROFILE(SMBntcreateX);
441 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
442 "file_attributes = 0x%x, share_access = 0x%x, "
443 "create_disposition = 0x%x create_options = 0x%x "
444 "root_dir_fid = 0x%x, fname = %s\n",
446 (unsigned int)access_mask,
447 (unsigned int)file_attributes,
448 (unsigned int)share_access,
449 (unsigned int)create_disposition,
450 (unsigned int)create_options,
451 (unsigned int)root_dir_fid,
455 * we need to remove ignored bits when they come directly from the client
456 * because we reuse some of them for internal stuff
458 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
461 * If it's an IPC, use the pipe handler.
465 if (lp_nt_pipe_support()) {
466 do_ntcreate_pipe_open(conn, req);
467 END_PROFILE(SMBntcreateX);
470 reply_doserror(req, ERRDOS, ERRnoaccess);
471 END_PROFILE(SMBntcreateX);
475 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
476 if (oplock_request) {
477 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
481 status = create_file(conn, req, root_dir_fid, fname,
482 access_mask, share_access, create_disposition,
483 create_options, file_attributes, oplock_request,
484 allocation_size, NULL, NULL, &fsp, &info, &sbuf);
486 if (!NT_STATUS_IS_OK(status)) {
487 if (open_was_deferred(req->mid)) {
488 /* We have re-scheduled this call, no error. */
489 END_PROFILE(SMBntcreateX);
492 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
493 reply_botherror(req, status, ERRDOS, ERRfilexists);
496 reply_nterror(req, status);
498 END_PROFILE(SMBntcreateX);
503 * If the caller set the extended oplock request bit
504 * and we granted one (by whatever means) - set the
505 * correct bit for extended oplock reply.
508 if (oplock_request &&
509 (lp_fake_oplocks(SNUM(conn))
510 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
513 * Exclusive oplock granted
516 if (flags & REQUEST_BATCH_OPLOCK) {
517 oplock_granted = BATCH_OPLOCK_RETURN;
519 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
521 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
522 oplock_granted = LEVEL_II_OPLOCK_RETURN;
524 oplock_granted = NO_OPLOCK_RETURN;
527 file_len = sbuf.st_size;
528 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
530 fattr = FILE_ATTRIBUTE_NORMAL;
533 if (flags & EXTENDED_RESPONSE_REQUIRED) {
534 /* This is very strange. We
535 * return 50 words, but only set
536 * the wcnt to 42 ? It's definately
537 * what happens on the wire....
539 reply_outbuf(req, 50, 0);
540 SCVAL(req->outbuf,smb_wct,42);
542 reply_outbuf(req, 34, 0);
545 p = (char *)req->outbuf + smb_vwv2;
547 SCVAL(p, 0, oplock_granted);
550 SSVAL(p,0,fsp->fnum);
552 if ((create_disposition == FILE_SUPERSEDE)
553 && (info == FILE_WAS_OVERWRITTEN)) {
554 SIVAL(p,0,FILE_WAS_SUPERSEDED);
561 c_timespec = get_create_timespec(
562 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
563 a_timespec = get_atimespec(&sbuf);
564 m_timespec = get_mtimespec(&sbuf);
566 if (lp_dos_filetime_resolution(SNUM(conn))) {
567 dos_filetime_timespec(&c_timespec);
568 dos_filetime_timespec(&a_timespec);
569 dos_filetime_timespec(&m_timespec);
572 put_long_date_timespec(p, c_timespec); /* create time. */
574 put_long_date_timespec(p, a_timespec); /* access time */
576 put_long_date_timespec(p, m_timespec); /* write time */
578 put_long_date_timespec(p, m_timespec); /* change time */
580 SIVAL(p,0,fattr); /* File Attributes. */
582 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
584 SOFF_T(p,0,file_len);
586 if (flags & EXTENDED_RESPONSE_REQUIRED) {
590 SCVAL(p,0,fsp->is_directory ? 1 : 0);
592 if (flags & EXTENDED_RESPONSE_REQUIRED) {
595 if (fsp->is_directory
596 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
597 perms = FILE_GENERIC_ALL;
599 perms = FILE_GENERIC_READ|FILE_EXECUTE;
604 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
605 fsp->fnum, fsp->fsp_name));
608 END_PROFILE(SMBntcreateX);
612 /****************************************************************************
613 Reply to a NT_TRANSACT_CREATE call to open a pipe.
614 ****************************************************************************/
616 static void do_nt_transact_create_pipe(connection_struct *conn,
617 struct smb_request *req,
618 uint16 **ppsetup, uint32 setup_count,
619 char **ppparams, uint32 parameter_count,
620 char **ppdata, uint32 data_count)
623 char *params = *ppparams;
629 TALLOC_CTX *ctx = talloc_tos();
632 * Ensure minimum number of parameters sent.
635 if(parameter_count < 54) {
636 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
637 reply_doserror(req, ERRDOS, ERRnoaccess);
641 flags = IVAL(params,0);
643 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
644 parameter_count-53, STR_TERMINATE,
646 if (!NT_STATUS_IS_OK(status)) {
647 reply_nterror(req, status);
651 nt_open_pipe(fname, conn, req, &pnum);
658 /* Realloc the size of parameters and data we will return */
659 if (flags & EXTENDED_RESPONSE_REQUIRED) {
660 /* Extended response is 32 more byyes. */
665 params = nttrans_realloc(ppparams, param_len);
667 reply_doserror(req, ERRDOS, ERRnomem);
672 SCVAL(p,0,NO_OPLOCK_RETURN);
677 SIVAL(p,0,FILE_WAS_OPENED);
681 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
684 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
686 SSVAL(p,2, 0x5FF); /* ? */
689 if (flags & EXTENDED_RESPONSE_REQUIRED) {
691 SIVAL(p,0,FILE_GENERIC_ALL);
693 * For pipes W2K3 seems to return
695 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
697 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
700 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
702 /* Send the required number of replies */
703 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
708 /****************************************************************************
709 Internal fn to set security descriptors.
710 ****************************************************************************/
712 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
713 uint32 security_info_sent)
715 SEC_DESC *psd = NULL;
718 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
722 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
724 if (!NT_STATUS_IS_OK(status)) {
728 if (psd->owner_sid==0) {
729 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
731 if (psd->group_sid==0) {
732 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
735 security_info_sent &= ~SACL_SECURITY_INFORMATION;
738 security_info_sent &= ~DACL_SECURITY_INFORMATION;
741 /* Convert all the generic bits. */
742 security_acl_map_generic(psd->dacl, &file_generic_mapping);
743 security_acl_map_generic(psd->sacl, &file_generic_mapping);
745 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
752 /****************************************************************************
753 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
754 ****************************************************************************/
756 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
758 struct ea_list *ea_list_head = NULL;
765 while (offset + 4 <= data_size) {
766 size_t next_offset = IVAL(pdata,offset);
767 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
773 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
774 if (next_offset == 0) {
777 offset += next_offset;
783 /****************************************************************************
784 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
785 ****************************************************************************/
787 static void call_nt_transact_create(connection_struct *conn,
788 struct smb_request *req,
789 uint16 **ppsetup, uint32 setup_count,
790 char **ppparams, uint32 parameter_count,
791 char **ppdata, uint32 data_count,
792 uint32 max_data_count)
795 char *params = *ppparams;
796 char *data = *ppdata;
797 /* Breakout the oplock request bits so we can set the reply bits separately. */
799 SMB_OFF_T file_len = 0;
800 SMB_STRUCT_STAT sbuf;
802 files_struct *fsp = NULL;
806 uint32 file_attributes;
808 uint32 create_disposition;
809 uint32 create_options;
811 struct security_descriptor *sd = NULL;
814 struct timespec c_timespec;
815 struct timespec a_timespec;
816 struct timespec m_timespec;
817 struct ea_list *ea_list = NULL;
820 uint64_t allocation_size;
822 uint8_t oplock_granted;
823 TALLOC_CTX *ctx = talloc_tos();
825 DEBUG(5,("call_nt_transact_create\n"));
828 * If it's an IPC, use the pipe handler.
832 if (lp_nt_pipe_support()) {
833 do_nt_transact_create_pipe(
835 ppsetup, setup_count,
836 ppparams, parameter_count,
840 reply_doserror(req, ERRDOS, ERRnoaccess);
845 * Ensure minimum number of parameters sent.
848 if(parameter_count < 54) {
849 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
850 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
854 flags = IVAL(params,0);
855 access_mask = IVAL(params,8);
856 file_attributes = IVAL(params,20);
857 share_access = IVAL(params,24);
858 create_disposition = IVAL(params,28);
859 create_options = IVAL(params,32);
860 sd_len = IVAL(params,36);
861 ea_len = IVAL(params,40);
862 root_dir_fid = (uint16)IVAL(params,4);
863 allocation_size = (uint64_t)IVAL(params,12);
864 #ifdef LARGE_SMB_OFF_T
865 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
869 * we need to remove ignored bits when they come directly from the client
870 * because we reuse some of them for internal stuff
872 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
874 /* Ensure the data_len is correct for the sd and ea values given. */
875 if ((ea_len + sd_len > data_count)
876 || (ea_len > data_count) || (sd_len > data_count)
877 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
878 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
879 "%u, data_count = %u\n", (unsigned int)ea_len,
880 (unsigned int)sd_len, (unsigned int)data_count));
881 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
886 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
889 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
891 if (!NT_STATUS_IS_OK(status)) {
892 DEBUG(10, ("call_nt_transact_create: "
893 "unmarshall_sec_desc failed: %s\n",
895 reply_nterror(req, status);
901 if (!lp_ea_support(SNUM(conn))) {
902 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
903 "EA's not supported.\n",
904 (unsigned int)ea_len));
905 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
910 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
911 "too small (should be more than 10)\n",
912 (unsigned int)ea_len ));
913 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
917 /* We have already checked that ea_len <= data_count here. */
918 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
920 if (ea_list == NULL) {
921 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
926 srvstr_get_path(ctx, params, req->flags2, &fname,
927 params+53, parameter_count-53,
928 STR_TERMINATE, &status);
929 if (!NT_STATUS_IS_OK(status)) {
930 reply_nterror(req, status);
934 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
935 if (oplock_request) {
936 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
940 status = create_file(conn, req, root_dir_fid, fname,
941 access_mask, share_access, create_disposition,
942 create_options, file_attributes, oplock_request,
943 allocation_size, sd, ea_list, &fsp, &info, &sbuf);
945 if(!NT_STATUS_IS_OK(status)) {
946 if (open_was_deferred(req->mid)) {
947 /* We have re-scheduled this call, no error. */
950 reply_openerror(req, status);
955 * If the caller set the extended oplock request bit
956 * and we granted one (by whatever means) - set the
957 * correct bit for extended oplock reply.
960 if (oplock_request &&
961 (lp_fake_oplocks(SNUM(conn))
962 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
965 * Exclusive oplock granted
968 if (flags & REQUEST_BATCH_OPLOCK) {
969 oplock_granted = BATCH_OPLOCK_RETURN;
971 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
973 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
974 oplock_granted = LEVEL_II_OPLOCK_RETURN;
976 oplock_granted = NO_OPLOCK_RETURN;
979 file_len = sbuf.st_size;
980 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
982 fattr = FILE_ATTRIBUTE_NORMAL;
985 /* Realloc the size of parameters and data we will return */
986 if (flags & EXTENDED_RESPONSE_REQUIRED) {
987 /* Extended response is 32 more byyes. */
992 params = nttrans_realloc(ppparams, param_len);
994 reply_doserror(req, ERRDOS, ERRnomem);
999 SCVAL(p, 0, oplock_granted);
1002 SSVAL(p,0,fsp->fnum);
1004 if ((create_disposition == FILE_SUPERSEDE)
1005 && (info == FILE_WAS_OVERWRITTEN)) {
1006 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1013 c_timespec = get_create_timespec(
1014 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1015 a_timespec = get_atimespec(&sbuf);
1016 m_timespec = get_mtimespec(&sbuf);
1018 if (lp_dos_filetime_resolution(SNUM(conn))) {
1019 dos_filetime_timespec(&c_timespec);
1020 dos_filetime_timespec(&a_timespec);
1021 dos_filetime_timespec(&m_timespec);
1024 put_long_date_timespec(p, c_timespec); /* create time. */
1026 put_long_date_timespec(p, a_timespec); /* access time */
1028 put_long_date_timespec(p, m_timespec); /* write time */
1030 put_long_date_timespec(p, m_timespec); /* change time */
1032 SIVAL(p,0,fattr); /* File Attributes. */
1034 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1036 SOFF_T(p,0,file_len);
1038 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1042 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1044 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1047 if (fsp->is_directory
1048 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
1049 perms = FILE_GENERIC_ALL;
1051 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1056 DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1058 /* Send the required number of replies */
1059 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1064 /****************************************************************************
1065 Reply to a NT CANCEL request.
1066 conn POINTER CAN BE NULL HERE !
1067 ****************************************************************************/
1069 void reply_ntcancel(struct smb_request *req)
1072 * Go through and cancel any pending change notifies.
1075 START_PROFILE(SMBntcancel);
1076 remove_pending_change_notify_requests_by_mid(req->mid);
1077 remove_pending_lock_requests_by_mid(req->mid);
1078 srv_cancel_sign_response(req->mid);
1080 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1082 END_PROFILE(SMBntcancel);
1086 /****************************************************************************
1088 ****************************************************************************/
1090 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1091 connection_struct *conn,
1092 struct smb_request *req,
1093 const char *oldname_in,
1094 const char *newname_in,
1097 SMB_STRUCT_STAT sbuf1, sbuf2;
1098 char *oldname = NULL;
1099 char *newname = NULL;
1100 char *last_component_oldname = NULL;
1101 char *last_component_newname = NULL;
1102 files_struct *fsp1,*fsp2;
1106 NTSTATUS status = NT_STATUS_OK;
1111 if (!CAN_WRITE(conn)) {
1112 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1115 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1116 &last_component_oldname, &sbuf1);
1117 if (!NT_STATUS_IS_OK(status)) {
1121 status = check_name(conn, oldname);
1122 if (!NT_STATUS_IS_OK(status)) {
1126 /* Source must already exist. */
1127 if (!VALID_STAT(sbuf1)) {
1128 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1130 /* Ensure attributes match. */
1131 fattr = dos_mode(conn,oldname,&sbuf1);
1132 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1133 return NT_STATUS_NO_SUCH_FILE;
1136 status = unix_convert(ctx, conn, newname_in, False, &newname,
1137 &last_component_newname, &sbuf2);
1138 if (!NT_STATUS_IS_OK(status)) {
1142 status = check_name(conn, newname);
1143 if (!NT_STATUS_IS_OK(status)) {
1147 /* Disallow if newname already exists. */
1148 if (VALID_STAT(sbuf2)) {
1149 return NT_STATUS_OBJECT_NAME_COLLISION;
1152 /* No links from a directory. */
1153 if (S_ISDIR(sbuf1.st_mode)) {
1154 return NT_STATUS_FILE_IS_A_DIRECTORY;
1157 /* Ensure this is within the share. */
1158 status = check_reduced_name(conn, oldname);
1159 if (!NT_STATUS_IS_OK(status)) {
1163 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1166 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1167 FILE_READ_DATA, /* Read-only. */
1168 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1170 0, /* No create options. */
1171 FILE_ATTRIBUTE_NORMAL,
1175 if (!NT_STATUS_IS_OK(status)) {
1179 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1180 FILE_WRITE_DATA, /* Read-only. */
1181 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1183 0, /* No create options. */
1188 if (!NT_STATUS_IS_OK(status)) {
1189 close_file(NULL, fsp1, ERROR_CLOSE);
1193 if (sbuf1.st_size) {
1194 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1198 * As we are opening fsp1 read-only we only expect
1199 * an error on close on fsp2 if we are out of space.
1200 * Thus we don't look at the error return from the
1203 close_file(NULL, fsp1, NORMAL_CLOSE);
1205 /* Ensure the modtime is set correctly on the destination file. */
1206 set_close_write_time(fsp2, get_mtimespec(&sbuf1));
1208 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1210 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1211 creates the file. This isn't the correct thing to do in the copy
1213 file_set_dosmode(conn, newname, fattr, &sbuf2,
1214 parent_dirname(newname),false);
1216 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1217 return NT_STATUS_DISK_FULL;
1220 if (!NT_STATUS_IS_OK(status)) {
1221 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1222 nt_errstr(status), oldname, newname));
1227 /****************************************************************************
1228 Reply to a NT rename request.
1229 ****************************************************************************/
1231 void reply_ntrename(struct smb_request *req)
1233 connection_struct *conn = req->conn;
1234 char *oldname = NULL;
1235 char *newname = NULL;
1238 bool src_has_wcard = False;
1239 bool dest_has_wcard = False;
1242 TALLOC_CTX *ctx = talloc_tos();
1244 START_PROFILE(SMBntrename);
1247 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1248 END_PROFILE(SMBntrename);
1252 attrs = SVAL(req->inbuf,smb_vwv0);
1253 rename_type = SVAL(req->inbuf,smb_vwv1);
1255 p = smb_buf(req->inbuf) + 1;
1256 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1257 0, STR_TERMINATE, &status,
1259 if (!NT_STATUS_IS_OK(status)) {
1260 reply_nterror(req, status);
1261 END_PROFILE(SMBntrename);
1265 if( is_ntfs_stream_name(oldname)) {
1266 /* Can't rename a stream. */
1267 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1268 END_PROFILE(SMBntrename);
1272 if (ms_has_wild(oldname)) {
1273 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1274 END_PROFILE(SMBntrename);
1279 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1280 0, STR_TERMINATE, &status,
1282 if (!NT_STATUS_IS_OK(status)) {
1283 reply_nterror(req, status);
1284 END_PROFILE(SMBntrename);
1288 status = resolve_dfspath(ctx, conn,
1289 req->flags2 & FLAGS2_DFS_PATHNAMES,
1292 if (!NT_STATUS_IS_OK(status)) {
1293 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1294 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1295 ERRSRV, ERRbadpath);
1296 END_PROFILE(SMBntrename);
1299 reply_nterror(req, status);
1300 END_PROFILE(SMBntrename);
1304 status = resolve_dfspath(ctx, conn,
1305 req->flags2 & FLAGS2_DFS_PATHNAMES,
1308 if (!NT_STATUS_IS_OK(status)) {
1309 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1310 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1311 ERRSRV, ERRbadpath);
1312 END_PROFILE(SMBntrename);
1315 reply_nterror(req, status);
1316 END_PROFILE(SMBntrename);
1320 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1322 switch(rename_type) {
1323 case RENAME_FLAG_RENAME:
1324 status = rename_internals(ctx, conn, req, oldname,
1325 newname, attrs, False, src_has_wcard,
1326 dest_has_wcard, DELETE_ACCESS);
1328 case RENAME_FLAG_HARD_LINK:
1329 if (src_has_wcard || dest_has_wcard) {
1331 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1333 status = hardlink_internals(ctx,
1339 case RENAME_FLAG_COPY:
1340 if (src_has_wcard || dest_has_wcard) {
1342 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1344 status = copy_internals(ctx, conn, req, oldname,
1348 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1349 status = NT_STATUS_INVALID_PARAMETER;
1352 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1356 if (!NT_STATUS_IS_OK(status)) {
1357 if (open_was_deferred(req->mid)) {
1358 /* We have re-scheduled this call. */
1359 END_PROFILE(SMBntrename);
1363 reply_nterror(req, status);
1364 END_PROFILE(SMBntrename);
1368 reply_outbuf(req, 0, 0);
1370 END_PROFILE(SMBntrename);
1374 /****************************************************************************
1375 Reply to a notify change - queue the request and
1376 don't allow a directory to be opened.
1377 ****************************************************************************/
1379 static void call_nt_transact_notify_change(connection_struct *conn,
1380 struct smb_request *req,
1384 uint32 parameter_count,
1385 char **ppdata, uint32 data_count,
1386 uint32 max_data_count,
1387 uint32 max_param_count)
1389 uint16 *setup = *ppsetup;
1395 if(setup_count < 6) {
1396 reply_doserror(req, ERRDOS, ERRbadfunc);
1400 fsp = file_fsp(req, SVAL(setup,4));
1401 filter = IVAL(setup, 0);
1402 recursive = (SVAL(setup, 6) != 0) ? True : False;
1404 DEBUG(3,("call_nt_transact_notify_change\n"));
1407 reply_doserror(req, ERRDOS, ERRbadfid);
1412 char *filter_string;
1414 if (!(filter_string = notify_filter_string(NULL, filter))) {
1415 reply_nterror(req,NT_STATUS_NO_MEMORY);
1419 DEBUG(3,("call_nt_transact_notify_change: notify change "
1420 "called on %s, filter = %s, recursive = %d\n",
1421 fsp->fsp_name, filter_string, recursive));
1423 TALLOC_FREE(filter_string);
1426 if((!fsp->is_directory) || (conn != fsp->conn)) {
1427 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1431 if (fsp->notify == NULL) {
1433 status = change_notify_create(fsp, filter, recursive);
1435 if (!NT_STATUS_IS_OK(status)) {
1436 DEBUG(10, ("change_notify_create returned %s\n",
1437 nt_errstr(status)));
1438 reply_nterror(req, status);
1443 if (fsp->notify->num_changes != 0) {
1446 * We've got changes pending, respond immediately
1450 * TODO: write a torture test to check the filtering behaviour
1454 change_notify_reply(fsp->conn, req->inbuf, max_param_count, fsp->notify);
1457 * change_notify_reply() above has independently sent its
1464 * No changes pending, queue the request
1467 status = change_notify_add_request(req,
1471 if (!NT_STATUS_IS_OK(status)) {
1472 reply_nterror(req, status);
1477 /****************************************************************************
1478 Reply to an NT transact rename command.
1479 ****************************************************************************/
1481 static void call_nt_transact_rename(connection_struct *conn,
1482 struct smb_request *req,
1483 uint16 **ppsetup, uint32 setup_count,
1484 char **ppparams, uint32 parameter_count,
1485 char **ppdata, uint32 data_count,
1486 uint32 max_data_count)
1488 char *params = *ppparams;
1489 char *new_name = NULL;
1490 files_struct *fsp = NULL;
1491 bool dest_has_wcard = False;
1493 TALLOC_CTX *ctx = talloc_tos();
1495 if(parameter_count < 5) {
1496 reply_doserror(req, ERRDOS, ERRbadfunc);
1500 fsp = file_fsp(req, SVAL(params, 0));
1501 if (!check_fsp(conn, req, fsp)) {
1504 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1505 parameter_count - 4,
1506 STR_TERMINATE, &status, &dest_has_wcard);
1507 if (!NT_STATUS_IS_OK(status)) {
1508 reply_nterror(req, status);
1513 * W2K3 ignores this request as the RAW-RENAME test
1514 * demonstrates, so we do.
1516 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1518 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1519 fsp->fsp_name, new_name));
1524 /******************************************************************************
1525 Fake up a completely empty SD.
1526 *******************************************************************************/
1528 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1532 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1534 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1535 return NT_STATUS_NO_MEMORY;
1538 return NT_STATUS_OK;
1541 /****************************************************************************
1542 Reply to query a security descriptor.
1543 ****************************************************************************/
1545 static void call_nt_transact_query_security_desc(connection_struct *conn,
1546 struct smb_request *req,
1550 uint32 parameter_count,
1553 uint32 max_data_count)
1555 char *params = *ppparams;
1556 char *data = *ppdata;
1557 SEC_DESC *psd = NULL;
1559 uint32 security_info_wanted;
1560 files_struct *fsp = NULL;
1564 if(parameter_count < 8) {
1565 reply_doserror(req, ERRDOS, ERRbadfunc);
1569 fsp = file_fsp(req, SVAL(params,0));
1571 reply_doserror(req, ERRDOS, ERRbadfid);
1575 security_info_wanted = IVAL(params,4);
1577 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1578 (unsigned int)security_info_wanted ));
1580 params = nttrans_realloc(ppparams, 4);
1581 if(params == NULL) {
1582 reply_doserror(req, ERRDOS, ERRnomem);
1587 * Get the permissions to return.
1590 if (!lp_nt_acl_support(SNUM(conn))) {
1591 status = get_null_nt_acl(talloc_tos(), &psd);
1593 status = SMB_VFS_FGET_NT_ACL(
1594 fsp, security_info_wanted, &psd);
1597 if (!NT_STATUS_IS_OK(status)) {
1598 reply_nterror(req, status);
1602 sd_size = ndr_size_security_descriptor(psd, 0);
1604 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1606 SIVAL(params,0,(uint32)sd_size);
1608 if (max_data_count < sd_size) {
1609 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1610 params, 4, *ppdata, 0);
1615 * Allocate the data we will point this at.
1618 data = nttrans_realloc(ppdata, sd_size);
1620 reply_doserror(req, ERRDOS, ERRnomem);
1624 status = marshall_sec_desc(talloc_tos(), psd,
1625 &blob.data, &blob.length);
1627 if (!NT_STATUS_IS_OK(status)) {
1628 reply_nterror(req, status);
1632 SMB_ASSERT(sd_size == blob.length);
1633 memcpy(data, blob.data, sd_size);
1635 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1640 /****************************************************************************
1641 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1642 ****************************************************************************/
1644 static void call_nt_transact_set_security_desc(connection_struct *conn,
1645 struct smb_request *req,
1649 uint32 parameter_count,
1652 uint32 max_data_count)
1654 char *params= *ppparams;
1655 char *data = *ppdata;
1656 files_struct *fsp = NULL;
1657 uint32 security_info_sent = 0;
1660 if(parameter_count < 8) {
1661 reply_doserror(req, ERRDOS, ERRbadfunc);
1665 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1666 reply_doserror(req, ERRDOS, ERRbadfid);
1670 if(!lp_nt_acl_support(SNUM(conn))) {
1674 security_info_sent = IVAL(params,4);
1676 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1677 (unsigned int)security_info_sent ));
1679 if (data_count == 0) {
1680 reply_doserror(req, ERRDOS, ERRnoaccess);
1684 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1686 if (!NT_STATUS_IS_OK(status)) {
1687 reply_nterror(req, status);
1692 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1696 /****************************************************************************
1698 ****************************************************************************/
1700 static void call_nt_transact_ioctl(connection_struct *conn,
1701 struct smb_request *req,
1702 uint16 **ppsetup, uint32 setup_count,
1703 char **ppparams, uint32 parameter_count,
1704 char **ppdata, uint32 data_count,
1705 uint32 max_data_count)
1712 static bool logged_message;
1713 char *pdata = *ppdata;
1715 if (setup_count != 8) {
1716 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1717 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1721 function = IVAL(*ppsetup, 0);
1722 fidnum = SVAL(*ppsetup, 4);
1723 isFSctl = CVAL(*ppsetup, 6);
1724 compfilter = CVAL(*ppsetup, 7);
1726 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1727 function, fidnum, isFSctl, compfilter));
1729 fsp=file_fsp(req, fidnum);
1730 /* this check is done in each implemented function case for now
1731 because I don't want to break anything... --metze
1732 FSP_BELONGS_CONN(fsp,conn);*/
1735 case FSCTL_SET_SPARSE:
1736 /* pretend this succeeded - tho strictly we should
1737 mark the file sparse (if the local fs supports it)
1738 so we can know if we need to pre-allocate or not */
1740 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1741 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1744 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1746 unsigned char objid[16];
1748 /* This should return the object-id on this file.
1749 * I think I'll make this be the inode+dev. JRA.
1752 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1754 if (!fsp_belongs_conn(conn, req, fsp)) {
1759 pdata = nttrans_realloc(ppdata, data_count);
1760 if (pdata == NULL) {
1761 reply_nterror(req, NT_STATUS_NO_MEMORY);
1764 push_file_id_16(pdata, &fsp->file_id);
1765 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1766 push_file_id_16(pdata+32, &fsp->file_id);
1767 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1772 case FSCTL_GET_REPARSE_POINT:
1773 /* pretend this fail - my winXP does it like this
1777 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1778 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1781 case FSCTL_SET_REPARSE_POINT:
1782 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1786 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1787 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1790 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1793 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1794 * and return their volume names. If max_data_count is 16, then it is just
1795 * asking for the number of volumes and length of the combined names.
1797 * pdata is the data allocated by our caller, but that uses
1798 * total_data_count (which is 0 in our case) rather than max_data_count.
1799 * Allocate the correct amount and return the pointer to let
1800 * it be deallocated when we return.
1802 SHADOW_COPY_DATA *shadow_data = NULL;
1803 TALLOC_CTX *shadow_mem_ctx = NULL;
1804 bool labels = False;
1805 uint32 labels_data_count = 0;
1809 if (!fsp_belongs_conn(conn, req, fsp)) {
1813 if (max_data_count < 16) {
1814 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1816 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1820 if (max_data_count > 16) {
1824 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1825 if (shadow_mem_ctx == NULL) {
1826 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1827 reply_nterror(req, NT_STATUS_NO_MEMORY);
1831 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1832 if (shadow_data == NULL) {
1833 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1834 talloc_destroy(shadow_mem_ctx);
1835 reply_nterror(req, NT_STATUS_NO_MEMORY);
1839 shadow_data->mem_ctx = shadow_mem_ctx;
1842 * Call the VFS routine to actually do the work.
1844 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1845 talloc_destroy(shadow_data->mem_ctx);
1846 if (errno == ENOSYS) {
1847 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1848 conn->connectpath));
1849 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1852 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1853 conn->connectpath));
1854 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1859 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1864 data_count = 12+labels_data_count+4;
1867 if (max_data_count<data_count) {
1868 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1869 max_data_count,data_count));
1870 talloc_destroy(shadow_data->mem_ctx);
1871 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1875 pdata = nttrans_realloc(ppdata, data_count);
1876 if (pdata == NULL) {
1877 talloc_destroy(shadow_data->mem_ctx);
1878 reply_nterror(req, NT_STATUS_NO_MEMORY);
1884 /* num_volumes 4 bytes */
1885 SIVAL(pdata,0,shadow_data->num_volumes);
1888 /* num_labels 4 bytes */
1889 SIVAL(pdata,4,shadow_data->num_volumes);
1892 /* needed_data_count 4 bytes */
1893 SIVAL(pdata,8,labels_data_count);
1897 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1898 shadow_data->num_volumes,fsp->fsp_name));
1899 if (labels && shadow_data->labels) {
1900 for (i=0;i<shadow_data->num_volumes;i++) {
1901 srvstr_push(pdata, req->flags2,
1902 cur_pdata, shadow_data->labels[i],
1903 2*sizeof(SHADOW_COPY_LABEL),
1904 STR_UNICODE|STR_TERMINATE);
1905 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1906 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1910 talloc_destroy(shadow_data->mem_ctx);
1912 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1918 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1920 /* pretend this succeeded -
1922 * we have to send back a list with all files owned by this SID
1924 * but I have to check that --metze
1928 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1930 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1932 if (!fsp_belongs_conn(conn, req, fsp)) {
1936 /* unknown 4 bytes: this is not the length of the sid :-( */
1937 /*unknown = IVAL(pdata,0);*/
1939 sid_parse(pdata+4,sid_len,&sid);
1940 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
1942 if (!sid_to_uid(&sid, &uid)) {
1943 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
1944 sid_string_dbg(&sid),
1945 (unsigned long)sid_len));
1949 /* we can take a look at the find source :-)
1951 * find ./ -uid $uid -name '*' is what we need here
1954 * and send 4bytes len and then NULL terminated unicode strings
1957 * but I don't know how to deal with the paged results
1958 * (maybe we can hang the result anywhere in the fsp struct)
1960 * we don't send all files at once
1961 * and at the next we should *not* start from the beginning,
1962 * so we have to cache the result
1967 /* this works for now... */
1968 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1972 if (!logged_message) {
1973 logged_message = True; /* Only print this once... */
1974 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
1979 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1983 #ifdef HAVE_SYS_QUOTAS
1984 /****************************************************************************
1985 Reply to get user quota
1986 ****************************************************************************/
1988 static void call_nt_transact_get_user_quota(connection_struct *conn,
1989 struct smb_request *req,
1993 uint32 parameter_count,
1996 uint32 max_data_count)
1998 NTSTATUS nt_status = NT_STATUS_OK;
1999 char *params = *ppparams;
2000 char *pdata = *ppdata;
2002 int data_len=0,param_len=0;
2005 files_struct *fsp = NULL;
2009 bool start_enum = True;
2010 SMB_NTQUOTA_STRUCT qt;
2011 SMB_NTQUOTA_LIST *tmp_list;
2012 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2017 if (conn->server_info->utok.uid != 0) {
2018 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2019 "[%s]\n", lp_servicename(SNUM(conn)),
2020 conn->server_info->unix_name));
2021 reply_doserror(req, ERRDOS, ERRnoaccess);
2026 * Ensure minimum number of parameters sent.
2029 if (parameter_count < 4) {
2030 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2031 reply_doserror(req, ERRDOS, ERRinvalidparam);
2035 /* maybe we can check the quota_fnum */
2036 fsp = file_fsp(req, SVAL(params,0));
2037 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2038 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2039 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2043 /* the NULL pointer checking for fsp->fake_file_handle->pd
2044 * is done by CHECK_NTQUOTA_HANDLE_OK()
2046 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2048 level = SVAL(params,2);
2050 /* unknown 12 bytes leading in params */
2053 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2054 /* seems that we should continue with the enum here --metze */
2056 if (qt_handle->quota_list!=NULL &&
2057 qt_handle->tmp_list==NULL) {
2060 free_ntquota_list(&(qt_handle->quota_list));
2062 /* Realloc the size of parameters and data we will return */
2064 params = nttrans_realloc(ppparams, param_len);
2065 if(params == NULL) {
2066 reply_doserror(req, ERRDOS, ERRnomem);
2071 SIVAL(params,0,data_len);
2078 case TRANSACT_GET_USER_QUOTA_LIST_START:
2080 if (qt_handle->quota_list==NULL &&
2081 qt_handle->tmp_list==NULL) {
2085 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2086 reply_doserror(req, ERRSRV, ERRerror);
2090 /* Realloc the size of parameters and data we will return */
2092 params = nttrans_realloc(ppparams, param_len);
2093 if(params == NULL) {
2094 reply_doserror(req, ERRDOS, ERRnomem);
2098 /* we should not trust the value in max_data_count*/
2099 max_data_count = MIN(max_data_count,2048);
2101 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2103 reply_doserror(req, ERRDOS, ERRnomem);
2109 /* set params Size of returned Quota Data 4 bytes*/
2110 /* but set it later when we know it */
2112 /* for each entry push the data */
2115 qt_handle->tmp_list = qt_handle->quota_list;
2118 tmp_list = qt_handle->tmp_list;
2120 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2121 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2123 sid_len = ndr_size_dom_sid(
2124 &tmp_list->quotas->sid, 0);
2125 entry_len = 40 + sid_len;
2127 /* nextoffset entry 4 bytes */
2128 SIVAL(entry,0,entry_len);
2130 /* then the len of the SID 4 bytes */
2131 SIVAL(entry,4,sid_len);
2133 /* unknown data 8 bytes uint64_t */
2134 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2136 /* the used disk space 8 bytes uint64_t */
2137 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2139 /* the soft quotas 8 bytes uint64_t */
2140 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2142 /* the hard quotas 8 bytes uint64_t */
2143 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2145 /* and now the SID */
2146 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2149 qt_handle->tmp_list = tmp_list;
2151 /* overwrite the offset of the last entry */
2152 SIVAL(entry-entry_len,0,0);
2154 data_len = 4+qt_len;
2155 /* overwrite the params quota_data_len */
2156 SIVAL(params,0,data_len);
2160 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2162 /* unknown 4 bytes IVAL(pdata,0) */
2164 if (data_count < 8) {
2165 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2166 reply_doserror(req, ERRDOS, ERRunknownlevel);
2170 sid_len = IVAL(pdata,4);
2171 /* Ensure this is less than 1mb. */
2172 if (sid_len > (1024*1024)) {
2173 reply_doserror(req, ERRDOS, ERRnomem);
2177 if (data_count < 8+sid_len) {
2178 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2179 reply_doserror(req, ERRDOS, ERRunknownlevel);
2183 data_len = 4+40+sid_len;
2185 if (max_data_count < data_len) {
2186 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2187 max_data_count, data_len));
2189 SIVAL(params,0,data_len);
2191 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2195 sid_parse(pdata+8,sid_len,&sid);
2197 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2200 * we have to return zero's in all fields
2201 * instead of returning an error here
2206 /* Realloc the size of parameters and data we will return */
2208 params = nttrans_realloc(ppparams, param_len);
2209 if(params == NULL) {
2210 reply_doserror(req, ERRDOS, ERRnomem);
2214 pdata = nttrans_realloc(ppdata, data_len);
2216 reply_doserror(req, ERRDOS, ERRnomem);
2222 /* set params Size of returned Quota Data 4 bytes*/
2223 SIVAL(params,0,data_len);
2225 /* nextoffset entry 4 bytes */
2228 /* then the len of the SID 4 bytes */
2229 SIVAL(entry,4,sid_len);
2231 /* unknown data 8 bytes uint64_t */
2232 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2234 /* the used disk space 8 bytes uint64_t */
2235 SBIG_UINT(entry,16,qt.usedspace);
2237 /* the soft quotas 8 bytes uint64_t */
2238 SBIG_UINT(entry,24,qt.softlim);
2240 /* the hard quotas 8 bytes uint64_t */
2241 SBIG_UINT(entry,32,qt.hardlim);
2243 /* and now the SID */
2244 sid_linearize(entry+40, sid_len, &sid);
2249 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2250 reply_doserror(req, ERRSRV, ERRerror);
2255 send_nt_replies(conn, req, nt_status, params, param_len,
2259 /****************************************************************************
2260 Reply to set user quota
2261 ****************************************************************************/
2263 static void call_nt_transact_set_user_quota(connection_struct *conn,
2264 struct smb_request *req,
2268 uint32 parameter_count,
2271 uint32 max_data_count)
2273 char *params = *ppparams;
2274 char *pdata = *ppdata;
2275 int data_len=0,param_len=0;
2276 SMB_NTQUOTA_STRUCT qt;
2279 files_struct *fsp = NULL;
2284 if (conn->server_info->utok.uid != 0) {
2285 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2286 "[%s]\n", lp_servicename(SNUM(conn)),
2287 conn->server_info->unix_name));
2288 reply_doserror(req, ERRDOS, ERRnoaccess);
2293 * Ensure minimum number of parameters sent.
2296 if (parameter_count < 2) {
2297 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2298 reply_doserror(req, ERRDOS, ERRinvalidparam);
2302 /* maybe we can check the quota_fnum */
2303 fsp = file_fsp(req, SVAL(params,0));
2304 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2305 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2306 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2310 if (data_count < 40) {
2311 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2312 reply_doserror(req, ERRDOS, ERRunknownlevel);
2316 /* offset to next quota record.
2317 * 4 bytes IVAL(pdata,0)
2322 sid_len = IVAL(pdata,4);
2324 if (data_count < 40+sid_len) {
2325 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2326 reply_doserror(req, ERRDOS, ERRunknownlevel);
2330 /* unknown 8 bytes in pdata
2331 * maybe its the change time in NTTIME
2334 /* the used space 8 bytes (uint64_t)*/
2335 qt.usedspace = (uint64_t)IVAL(pdata,16);
2336 #ifdef LARGE_SMB_OFF_T
2337 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2338 #else /* LARGE_SMB_OFF_T */
2339 if ((IVAL(pdata,20) != 0)&&
2340 ((qt.usedspace != 0xFFFFFFFF)||
2341 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2342 /* more than 32 bits? */
2343 reply_doserror(req, ERRDOS, ERRunknownlevel);
2346 #endif /* LARGE_SMB_OFF_T */
2348 /* the soft quotas 8 bytes (uint64_t)*/
2349 qt.softlim = (uint64_t)IVAL(pdata,24);
2350 #ifdef LARGE_SMB_OFF_T
2351 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2352 #else /* LARGE_SMB_OFF_T */
2353 if ((IVAL(pdata,28) != 0)&&
2354 ((qt.softlim != 0xFFFFFFFF)||
2355 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2356 /* more than 32 bits? */
2357 reply_doserror(req, ERRDOS, ERRunknownlevel);
2360 #endif /* LARGE_SMB_OFF_T */
2362 /* the hard quotas 8 bytes (uint64_t)*/
2363 qt.hardlim = (uint64_t)IVAL(pdata,32);
2364 #ifdef LARGE_SMB_OFF_T
2365 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2366 #else /* LARGE_SMB_OFF_T */
2367 if ((IVAL(pdata,36) != 0)&&
2368 ((qt.hardlim != 0xFFFFFFFF)||
2369 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2370 /* more than 32 bits? */
2371 reply_doserror(req, ERRDOS, ERRunknownlevel);
2374 #endif /* LARGE_SMB_OFF_T */
2376 sid_parse(pdata+40,sid_len,&sid);
2377 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2379 /* 44 unknown bytes left... */
2381 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2382 reply_doserror(req, ERRSRV, ERRerror);
2386 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2389 #endif /* HAVE_SYS_QUOTAS */
2391 static void handle_nttrans(connection_struct *conn,
2392 struct trans_state *state,
2393 struct smb_request *req)
2395 if (Protocol >= PROTOCOL_NT1) {
2396 req->flags2 |= 0x40; /* IS_LONG_NAME */
2397 SSVAL(req->inbuf,smb_flg2,req->flags2);
2400 /* Now we must call the relevant NT_TRANS function */
2401 switch(state->call) {
2402 case NT_TRANSACT_CREATE:
2404 START_PROFILE(NT_transact_create);
2405 call_nt_transact_create(
2407 &state->setup, state->setup_count,
2408 &state->param, state->total_param,
2409 &state->data, state->total_data,
2410 state->max_data_return);
2411 END_PROFILE(NT_transact_create);
2415 case NT_TRANSACT_IOCTL:
2417 START_PROFILE(NT_transact_ioctl);
2418 call_nt_transact_ioctl(
2420 &state->setup, state->setup_count,
2421 &state->param, state->total_param,
2422 &state->data, state->total_data,
2423 state->max_data_return);
2424 END_PROFILE(NT_transact_ioctl);
2428 case NT_TRANSACT_SET_SECURITY_DESC:
2430 START_PROFILE(NT_transact_set_security_desc);
2431 call_nt_transact_set_security_desc(
2433 &state->setup, state->setup_count,
2434 &state->param, state->total_param,
2435 &state->data, state->total_data,
2436 state->max_data_return);
2437 END_PROFILE(NT_transact_set_security_desc);
2441 case NT_TRANSACT_NOTIFY_CHANGE:
2443 START_PROFILE(NT_transact_notify_change);
2444 call_nt_transact_notify_change(
2446 &state->setup, state->setup_count,
2447 &state->param, state->total_param,
2448 &state->data, state->total_data,
2449 state->max_data_return,
2450 state->max_param_return);
2451 END_PROFILE(NT_transact_notify_change);
2455 case NT_TRANSACT_RENAME:
2457 START_PROFILE(NT_transact_rename);
2458 call_nt_transact_rename(
2460 &state->setup, state->setup_count,
2461 &state->param, state->total_param,
2462 &state->data, state->total_data,
2463 state->max_data_return);
2464 END_PROFILE(NT_transact_rename);
2468 case NT_TRANSACT_QUERY_SECURITY_DESC:
2470 START_PROFILE(NT_transact_query_security_desc);
2471 call_nt_transact_query_security_desc(
2473 &state->setup, state->setup_count,
2474 &state->param, state->total_param,
2475 &state->data, state->total_data,
2476 state->max_data_return);
2477 END_PROFILE(NT_transact_query_security_desc);
2481 #ifdef HAVE_SYS_QUOTAS
2482 case NT_TRANSACT_GET_USER_QUOTA:
2484 START_PROFILE(NT_transact_get_user_quota);
2485 call_nt_transact_get_user_quota(
2487 &state->setup, state->setup_count,
2488 &state->param, state->total_param,
2489 &state->data, state->total_data,
2490 state->max_data_return);
2491 END_PROFILE(NT_transact_get_user_quota);
2495 case NT_TRANSACT_SET_USER_QUOTA:
2497 START_PROFILE(NT_transact_set_user_quota);
2498 call_nt_transact_set_user_quota(
2500 &state->setup, state->setup_count,
2501 &state->param, state->total_param,
2502 &state->data, state->total_data,
2503 state->max_data_return);
2504 END_PROFILE(NT_transact_set_user_quota);
2507 #endif /* HAVE_SYS_QUOTAS */
2510 /* Error in request */
2511 DEBUG(0,("handle_nttrans: Unknown request %d in "
2512 "nttrans call\n", state->call));
2513 reply_doserror(req, ERRSRV, ERRerror);
2519 /****************************************************************************
2520 Reply to a SMBNTtrans.
2521 ****************************************************************************/
2523 void reply_nttrans(struct smb_request *req)
2525 connection_struct *conn = req->conn;
2530 uint16 function_code;
2532 struct trans_state *state;
2536 START_PROFILE(SMBnttrans);
2538 if (req->wct < 19) {
2539 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2540 END_PROFILE(SMBnttrans);
2544 size = smb_len(req->inbuf) + 4;
2545 av_size = smb_len(req->inbuf);
2546 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
2547 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
2548 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
2549 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
2550 function_code = SVAL(req->inbuf, smb_nt_Function);
2552 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2553 reply_doserror(req, ERRSRV, ERRaccess);
2554 END_PROFILE(SMBnttrans);
2558 result = allow_new_trans(conn->pending_trans, req->mid);
2559 if (!NT_STATUS_IS_OK(result)) {
2560 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2561 reply_nterror(req, result);
2562 END_PROFILE(SMBnttrans);
2566 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2567 reply_doserror(req, ERRSRV, ERRaccess);
2568 END_PROFILE(SMBnttrans);
2572 state->cmd = SMBnttrans;
2574 state->mid = req->mid;
2575 state->vuid = req->vuid;
2576 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
2578 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
2579 state->param = NULL;
2580 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
2581 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
2583 /* setup count is in *words* */
2584 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
2585 state->setup = NULL;
2586 state->call = function_code;
2588 DEBUG(10, ("num_setup=%u, "
2589 "param_total=%u, this_param=%u, max_param=%u, "
2590 "data_total=%u, this_data=%u, max_data=%u, "
2591 "param_offset=%u, data_offset=%u\n",
2592 (unsigned)state->setup_count,
2593 (unsigned)state->total_param, (unsigned)pscnt,
2594 (unsigned)state->max_param_return,
2595 (unsigned)state->total_data, (unsigned)dscnt,
2596 (unsigned)state->max_data_return,
2597 (unsigned)psoff, (unsigned)dsoff));
2600 * All nttrans messages we handle have smb_wct == 19 +
2601 * state->setup_count. Ensure this is so as a sanity check.
2604 if(req->wct != 19 + (state->setup_count/2)) {
2605 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2606 req->wct, 19 + (state->setup_count/2)));
2610 /* Don't allow more than 128mb for each value. */
2611 if ((state->total_data > (1024*1024*128)) ||
2612 (state->total_param > (1024*1024*128))) {
2613 reply_doserror(req, ERRDOS, ERRnomem);
2614 END_PROFILE(SMBnttrans);
2618 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2621 if (state->total_data) {
2622 /* Can't use talloc here, the core routines do realloc on the
2623 * params and data. */
2624 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2625 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2626 "bytes !\n", (unsigned int)state->total_data));
2628 reply_doserror(req, ERRDOS, ERRnomem);
2629 END_PROFILE(SMBnttrans);
2633 if (dscnt > state->total_data ||
2634 dsoff+dscnt < dsoff) {
2638 if (dsoff > av_size ||
2640 dsoff+dscnt > av_size) {
2644 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2647 if (state->total_param) {
2648 /* Can't use talloc here, the core routines do realloc on the
2649 * params and data. */
2650 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2651 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2652 "bytes !\n", (unsigned int)state->total_param));
2653 SAFE_FREE(state->data);
2655 reply_doserror(req, ERRDOS, ERRnomem);
2656 END_PROFILE(SMBnttrans);
2660 if (pscnt > state->total_param ||
2661 psoff+pscnt < psoff) {
2665 if (psoff > av_size ||
2667 psoff+pscnt > av_size) {
2671 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2674 state->received_data = dscnt;
2675 state->received_param = pscnt;
2677 if(state->setup_count > 0) {
2678 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2679 state->setup_count));
2680 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2681 if (state->setup == NULL) {
2682 DEBUG(0,("reply_nttrans : Out of memory\n"));
2683 SAFE_FREE(state->data);
2684 SAFE_FREE(state->param);
2686 reply_doserror(req, ERRDOS, ERRnomem);
2687 END_PROFILE(SMBnttrans);
2691 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2692 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2695 if (smb_nt_SetupStart + state->setup_count > size) {
2699 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
2700 state->setup_count);
2701 dump_data(10, (uint8 *)state->setup, state->setup_count);
2704 if ((state->received_data == state->total_data) &&
2705 (state->received_param == state->total_param)) {
2706 handle_nttrans(conn, state, req);
2707 SAFE_FREE(state->param);
2708 SAFE_FREE(state->data);
2710 END_PROFILE(SMBnttrans);
2714 DLIST_ADD(conn->pending_trans, state);
2716 /* We need to send an interim response then receive the rest
2717 of the parameter/data bytes */
2718 reply_outbuf(req, 0, 0);
2719 show_msg((char *)req->outbuf);
2720 END_PROFILE(SMBnttrans);
2725 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2726 SAFE_FREE(state->data);
2727 SAFE_FREE(state->param);
2729 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2730 END_PROFILE(SMBnttrans);
2734 /****************************************************************************
2735 Reply to a SMBnttranss
2736 ****************************************************************************/
2738 void reply_nttranss(struct smb_request *req)
2740 connection_struct *conn = req->conn;
2741 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2742 struct trans_state *state;
2746 START_PROFILE(SMBnttranss);
2748 show_msg((char *)req->inbuf);
2750 if (req->wct < 18) {
2751 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2752 END_PROFILE(SMBnttranss);
2756 for (state = conn->pending_trans; state != NULL;
2757 state = state->next) {
2758 if (state->mid == req->mid) {
2763 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2764 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2765 END_PROFILE(SMBnttranss);
2769 /* Revise state->total_param and state->total_data in case they have
2770 changed downwards */
2771 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
2772 < state->total_param) {
2773 state->total_param = IVAL(req->inbuf,
2774 smb_nts_TotalParameterCount);
2776 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
2777 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
2780 size = smb_len(req->inbuf) + 4;
2781 av_size = smb_len(req->inbuf);
2783 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
2784 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
2785 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
2787 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
2788 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
2789 doff = IVAL(req->inbuf, smb_nts_DataOffset);
2791 state->received_param += pcnt;
2792 state->received_data += dcnt;
2794 if ((state->received_data > state->total_data) ||
2795 (state->received_param > state->total_param))
2799 if (pdisp > state->total_param ||
2800 pcnt > state->total_param ||
2801 pdisp+pcnt > state->total_param ||
2802 pdisp+pcnt < pdisp) {
2806 if (poff > av_size ||
2808 poff+pcnt > av_size ||
2813 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2818 if (ddisp > state->total_data ||
2819 dcnt > state->total_data ||
2820 ddisp+dcnt > state->total_data ||
2821 ddisp+dcnt < ddisp) {
2825 if (ddisp > av_size ||
2827 ddisp+dcnt > av_size ||
2828 ddisp+dcnt < ddisp) {
2832 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2836 if ((state->received_param < state->total_param) ||
2837 (state->received_data < state->total_data)) {
2838 END_PROFILE(SMBnttranss);
2843 * construct_reply_common will copy smb_com from inbuf to
2844 * outbuf. SMBnttranss is wrong here.
2846 SCVAL(req->inbuf,smb_com,SMBnttrans);
2848 handle_nttrans(conn, state, req);
2850 DLIST_REMOVE(conn->pending_trans, state);
2851 SAFE_FREE(state->data);
2852 SAFE_FREE(state->param);
2854 END_PROFILE(SMBnttranss);
2859 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2860 DLIST_REMOVE(conn->pending_trans, state);
2861 SAFE_FREE(state->data);
2862 SAFE_FREE(state->param);
2864 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2865 END_PROFILE(SMBnttranss);