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 struct current_user current_user;
27 static const char *known_nt_pipes[] = {
48 static char *nttrans_realloc(char **ptr, size_t size)
51 smb_panic("nttrans_realloc() called with NULL ptr");
54 *ptr = (char *)SMB_REALLOC(*ptr, size);
58 memset(*ptr,'\0',size);
62 /****************************************************************************
63 Send the required number of replies back.
64 We assume all fields other than the data fields are
65 set correctly for the type of call.
66 HACK ! Always assumes smb_setup field is zero.
67 ****************************************************************************/
69 void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
70 char *params, int paramsize,
71 char *pdata, int datasize)
73 int data_to_send = datasize;
74 int params_to_send = paramsize;
78 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
79 int alignment_offset = 3;
80 int data_alignment_offset = 0;
83 * If there genuinely are no parameters or data to send just send
87 if(params_to_send == 0 && data_to_send == 0) {
88 reply_outbuf(req, 18, 0);
89 show_msg((char *)req->outbuf);
94 * When sending params and data ensure that both are nicely aligned.
95 * Only do this alignment when there is also data to send - else
96 * can cause NT redirector problems.
99 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
100 data_alignment_offset = 4 - (params_to_send % 4);
104 * Space is bufsize minus Netbios over TCP header minus SMB header.
105 * The alignment_offset is to align the param bytes on a four byte
106 * boundary (2 bytes for data len, one byte pad).
107 * NT needs this to work correctly.
110 useable_space = max_send - (smb_size
113 + data_alignment_offset);
116 * useable_space can never be more than max_send minus the
120 useable_space = MIN(useable_space,
121 max_send - (alignment_offset+data_alignment_offset));
124 while (params_to_send || data_to_send) {
127 * Calculate whether we will totally or partially fill this packet.
130 total_sent_thistime = params_to_send + data_to_send +
131 alignment_offset + data_alignment_offset;
134 * We can never send more than useable_space.
137 total_sent_thistime = MIN(total_sent_thistime, useable_space);
139 reply_outbuf(req, 18, total_sent_thistime);
142 * Set total params and data to be sent.
145 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
146 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
149 * Calculate how many parameters and data we can fit into
150 * this packet. Parameters get precedence.
153 params_sent_thistime = MIN(params_to_send,useable_space);
154 data_sent_thistime = useable_space - params_sent_thistime;
155 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
157 SIVAL(req->outbuf, smb_ntr_ParameterCount,
158 params_sent_thistime);
160 if(params_sent_thistime == 0) {
161 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
162 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
165 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
166 * parameter bytes, however the first 4 bytes of outbuf are
167 * the Netbios over TCP header. Thus use smb_base() to subtract
168 * them from the calculation.
171 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
172 ((smb_buf(req->outbuf)+alignment_offset)
173 - smb_base(req->outbuf)));
175 * Absolute displacement of param bytes sent in this packet.
178 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
183 * Deal with the data portion.
186 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
188 if(data_sent_thistime == 0) {
189 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
190 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
193 * The offset of the data bytes is the offset of the
194 * parameter bytes plus the number of parameters being sent this time.
197 SIVAL(req->outbuf, smb_ntr_DataOffset,
198 ((smb_buf(req->outbuf)+alignment_offset) -
199 smb_base(req->outbuf))
200 + params_sent_thistime + data_alignment_offset);
201 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
205 * Copy the param bytes into the packet.
208 if(params_sent_thistime) {
209 if (alignment_offset != 0) {
210 memset(smb_buf(req->outbuf), 0,
213 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
214 params_sent_thistime);
218 * Copy in the data bytes
221 if(data_sent_thistime) {
222 if (data_alignment_offset != 0) {
223 memset((smb_buf(req->outbuf)+alignment_offset+
224 params_sent_thistime), 0,
225 data_alignment_offset);
227 memcpy(smb_buf(req->outbuf)+alignment_offset
228 +params_sent_thistime+data_alignment_offset,
229 pd,data_sent_thistime);
232 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
233 params_sent_thistime, data_sent_thistime, useable_space));
234 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
235 params_to_send, data_to_send, paramsize, datasize));
237 if (NT_STATUS_V(nt_error)) {
238 error_packet_set((char *)req->outbuf,
243 /* Send the packet */
244 show_msg((char *)req->outbuf);
245 if (!send_smb(smbd_server_fd(),(char *)req->outbuf)) {
246 exit_server_cleanly("send_nt_replies: send_smb failed.");
249 TALLOC_FREE(req->outbuf);
251 pp += params_sent_thistime;
252 pd += data_sent_thistime;
254 params_to_send -= params_sent_thistime;
255 data_to_send -= data_sent_thistime;
261 if(params_to_send < 0 || data_to_send < 0) {
262 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
263 params_to_send, data_to_send));
269 /****************************************************************************
270 Is it an NTFS stream name ?
271 ****************************************************************************/
273 bool is_ntfs_stream_name(const char *fname)
275 if (lp_posix_pathnames()) {
278 return (strchr_m(fname, ':') != NULL) ? True : False;
281 /****************************************************************************
282 Reply to an NT create and X call on a pipe
283 ****************************************************************************/
285 static void nt_open_pipe(char *fname, connection_struct *conn,
286 struct smb_request *req, int *ppnum)
288 smb_np_struct *p = NULL;
291 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
293 /* See if it is one we want to handle. */
295 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
296 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
301 for( i = 0; known_nt_pipes[i]; i++ ) {
302 if( strequal(fname,known_nt_pipes[i])) {
307 if ( known_nt_pipes[i] == NULL ) {
308 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
313 /* Strip \\ off the name. */
316 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
318 p = open_rpc_pipe_p(fname, conn, req->vuid);
320 reply_doserror(req, ERRSRV, ERRnofids);
324 /* TODO: Add pipe to db */
326 if ( !store_pipe_opendb( p ) ) {
327 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
334 /****************************************************************************
335 Reply to an NT create and X call for pipes.
336 ****************************************************************************/
338 static void do_ntcreate_pipe_open(connection_struct *conn,
339 struct smb_request *req)
344 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
345 TALLOC_CTX *ctx = talloc_tos();
347 srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
348 smb_buf(req->inbuf), STR_TERMINATE);
351 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
355 nt_open_pipe(fname, conn, req, &pnum);
363 * Deal with pipe return.
366 if (flags & EXTENDED_RESPONSE_REQUIRED) {
367 /* This is very strange. We
368 * return 50 words, but only set
369 * the wcnt to 42 ? It's definately
370 * what happens on the wire....
372 reply_outbuf(req, 50, 0);
373 SCVAL(req->outbuf,smb_wct,42);
375 reply_outbuf(req, 34, 0);
378 p = (char *)req->outbuf + smb_vwv2;
382 SIVAL(p,0,FILE_WAS_OPENED);
385 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
388 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
390 SSVAL(p,2, 0x5FF); /* ? */
393 if (flags & EXTENDED_RESPONSE_REQUIRED) {
395 SIVAL(p,0,FILE_GENERIC_ALL);
397 * For pipes W2K3 seems to return
399 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
401 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
404 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
409 /****************************************************************************
410 Reply to an NT create and X call.
411 ****************************************************************************/
413 void reply_ntcreate_and_X(connection_struct *conn, struct smb_request *req)
418 uint32 file_attributes;
420 uint32 create_disposition;
421 uint32 create_options;
423 SMB_BIG_UINT allocation_size;
424 /* Breakout the oplock request bits so we can set the
425 reply bits separately. */
427 SMB_OFF_T file_len = 0;
428 SMB_STRUCT_STAT sbuf;
430 files_struct *fsp = NULL;
432 struct timespec c_timespec;
433 struct timespec a_timespec;
434 struct timespec m_timespec;
436 uint8_t oplock_granted = NO_OPLOCK_RETURN;
437 TALLOC_CTX *ctx = talloc_tos();
439 START_PROFILE(SMBntcreateX);
442 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
446 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
447 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
448 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
449 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
450 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
451 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
452 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
454 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,
455 smb_ntcreate_AllocationSize);
456 #ifdef LARGE_SMB_OFF_T
457 allocation_size |= (((SMB_BIG_UINT)IVAL(
459 smb_ntcreate_AllocationSize + 4)) << 32);
462 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
463 smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
465 if (!NT_STATUS_IS_OK(status)) {
466 reply_nterror(req, status);
467 END_PROFILE(SMBntcreateX);
471 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
472 "file_attributes = 0x%x, share_access = 0x%x, "
473 "create_disposition = 0x%x create_options = 0x%x "
474 "root_dir_fid = 0x%x, fname = %s\n",
476 (unsigned int)access_mask,
477 (unsigned int)file_attributes,
478 (unsigned int)share_access,
479 (unsigned int)create_disposition,
480 (unsigned int)create_options,
481 (unsigned int)root_dir_fid,
485 * If it's an IPC, use the pipe handler.
489 if (lp_nt_pipe_support()) {
490 do_ntcreate_pipe_open(conn, req);
491 END_PROFILE(SMBntcreateX);
494 reply_doserror(req, ERRDOS, ERRnoaccess);
495 END_PROFILE(SMBntcreateX);
500 status = create_file(conn, req, root_dir_fid, fname, flags,
501 access_mask, file_attributes, share_access,
502 create_disposition, create_options,
503 allocation_size, NULL, NULL,
504 &fsp, &info, &oplock_granted, &sbuf);
506 if (!NT_STATUS_IS_OK(status)) {
507 if (open_was_deferred(req->mid)) {
508 /* We have re-scheduled this call, no error. */
509 END_PROFILE(SMBntcreateX);
512 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
513 reply_botherror(req, status, ERRDOS, ERRfilexists);
516 reply_nterror(req, status);
518 END_PROFILE(SMBntcreateX);
522 file_len = sbuf.st_size;
523 fattr = dos_mode(conn,fname,&sbuf);
525 fattr = FILE_ATTRIBUTE_NORMAL;
528 if (flags & EXTENDED_RESPONSE_REQUIRED) {
529 /* This is very strange. We
530 * return 50 words, but only set
531 * the wcnt to 42 ? It's definately
532 * what happens on the wire....
534 reply_outbuf(req, 50, 0);
535 SCVAL(req->outbuf,smb_wct,42);
537 reply_outbuf(req, 34, 0);
540 p = (char *)req->outbuf + smb_vwv2;
542 SCVAL(p, 0, oplock_granted);
545 SSVAL(p,0,fsp->fnum);
547 if ((create_disposition == FILE_SUPERSEDE)
548 && (info == FILE_WAS_OVERWRITTEN)) {
549 SIVAL(p,0,FILE_WAS_SUPERSEDED);
556 c_timespec = get_create_timespec(
557 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
558 a_timespec = get_atimespec(&sbuf);
559 m_timespec = get_mtimespec(&sbuf);
561 if (lp_dos_filetime_resolution(SNUM(conn))) {
562 dos_filetime_timespec(&c_timespec);
563 dos_filetime_timespec(&a_timespec);
564 dos_filetime_timespec(&m_timespec);
567 put_long_date_timespec(p, c_timespec); /* create time. */
569 put_long_date_timespec(p, a_timespec); /* access time */
571 put_long_date_timespec(p, m_timespec); /* write time */
573 put_long_date_timespec(p, m_timespec); /* change time */
575 SIVAL(p,0,fattr); /* File Attributes. */
577 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
579 SOFF_T(p,0,file_len);
581 if (flags & EXTENDED_RESPONSE_REQUIRED) {
585 SCVAL(p,0,fsp->is_directory ? 1 : 0);
587 if (flags & EXTENDED_RESPONSE_REQUIRED) {
590 if (fsp->is_directory
591 || can_write_to_file(conn, fname, &sbuf)) {
592 perms = FILE_GENERIC_ALL;
594 perms = FILE_GENERIC_READ|FILE_EXECUTE;
599 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
600 fsp->fnum, fsp->fsp_name));
603 END_PROFILE(SMBntcreateX);
607 /****************************************************************************
608 Reply to a NT_TRANSACT_CREATE call to open a pipe.
609 ****************************************************************************/
611 static void do_nt_transact_create_pipe(connection_struct *conn,
612 struct smb_request *req,
613 uint16 **ppsetup, uint32 setup_count,
614 char **ppparams, uint32 parameter_count,
615 char **ppdata, uint32 data_count)
618 char *params = *ppparams;
624 TALLOC_CTX *ctx = talloc_tos();
627 * Ensure minimum number of parameters sent.
630 if(parameter_count < 54) {
631 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
632 reply_doserror(req, ERRDOS, ERRnoaccess);
636 flags = IVAL(params,0);
638 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
639 parameter_count-53, STR_TERMINATE,
641 if (!NT_STATUS_IS_OK(status)) {
642 reply_nterror(req, status);
646 nt_open_pipe(fname, conn, req, &pnum);
653 /* Realloc the size of parameters and data we will return */
654 if (flags & EXTENDED_RESPONSE_REQUIRED) {
655 /* Extended response is 32 more byyes. */
660 params = nttrans_realloc(ppparams, param_len);
662 reply_doserror(req, ERRDOS, ERRnomem);
667 SCVAL(p,0,NO_OPLOCK_RETURN);
672 SIVAL(p,0,FILE_WAS_OPENED);
676 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
679 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
681 SSVAL(p,2, 0x5FF); /* ? */
684 if (flags & EXTENDED_RESPONSE_REQUIRED) {
686 SIVAL(p,0,FILE_GENERIC_ALL);
688 * For pipes W2K3 seems to return
690 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
692 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
695 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
697 /* Send the required number of replies */
698 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
703 /****************************************************************************
704 Internal fn to set security descriptors.
705 ****************************************************************************/
707 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
710 SEC_DESC *psd = NULL;
714 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
719 * Init the parse struct we will unmarshall from.
722 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
723 DEBUG(0,("set_sd: talloc_init failed.\n"));
724 return NT_STATUS_NO_MEMORY;
727 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
730 * Setup the prs_struct to point at the memory we just
734 prs_give_memory( &pd, data, sd_len, False);
737 * Finally, unmarshall from the data buffer.
740 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
741 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
743 * Return access denied for want of a better error message..
745 talloc_destroy(mem_ctx);
746 return NT_STATUS_NO_MEMORY;
749 if (psd->owner_sid==0) {
750 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
752 if (psd->group_sid==0) {
753 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
756 security_info_sent &= ~SACL_SECURITY_INFORMATION;
759 security_info_sent &= ~DACL_SECURITY_INFORMATION;
762 status = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
764 talloc_destroy(mem_ctx);
768 /****************************************************************************
769 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
770 ****************************************************************************/
772 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
774 struct ea_list *ea_list_head = NULL;
781 while (offset + 4 <= data_size) {
782 size_t next_offset = IVAL(pdata,offset);
783 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
789 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
790 if (next_offset == 0) {
793 offset += next_offset;
799 /****************************************************************************
800 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
801 ****************************************************************************/
803 static void call_nt_transact_create(connection_struct *conn,
804 struct smb_request *req,
805 uint16 **ppsetup, uint32 setup_count,
806 char **ppparams, uint32 parameter_count,
807 char **ppdata, uint32 data_count,
808 uint32 max_data_count)
811 char *params = *ppparams;
812 char *data = *ppdata;
813 /* Breakout the oplock request bits so we can set the reply bits separately. */
815 SMB_OFF_T file_len = 0;
816 SMB_STRUCT_STAT sbuf;
818 files_struct *fsp = NULL;
822 uint32 file_attributes;
824 uint32 create_disposition;
825 uint32 create_options;
827 struct security_descriptor *sd = NULL;
830 struct timespec c_timespec;
831 struct timespec a_timespec;
832 struct timespec m_timespec;
833 struct ea_list *ea_list = NULL;
836 SMB_BIG_UINT allocation_size;
837 uint8_t oplock_granted;
838 TALLOC_CTX *ctx = talloc_tos();
840 DEBUG(5,("call_nt_transact_create\n"));
843 * If it's an IPC, use the pipe handler.
847 if (lp_nt_pipe_support()) {
848 do_nt_transact_create_pipe(
850 ppsetup, setup_count,
851 ppparams, parameter_count,
855 reply_doserror(req, ERRDOS, ERRnoaccess);
861 * Ensure minimum number of parameters sent.
864 if(parameter_count < 54) {
865 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
866 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
870 flags = IVAL(params,0);
871 access_mask = IVAL(params,8);
872 file_attributes = IVAL(params,20);
873 share_access = IVAL(params,24);
874 create_disposition = IVAL(params,28);
875 create_options = IVAL(params,32);
876 sd_len = IVAL(params,36);
877 ea_len = IVAL(params,40);
878 root_dir_fid = (uint16)IVAL(params,4);
879 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
880 #ifdef LARGE_SMB_OFF_T
881 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
884 /* Ensure the data_len is correct for the sd and ea values given. */
885 if ((ea_len + sd_len > data_count)
886 || (ea_len > data_count) || (sd_len > data_count)
887 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
888 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
889 "%u, data_count = %u\n", (unsigned int)ea_len,
890 (unsigned int)sd_len, (unsigned int)data_count));
891 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
896 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
899 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
901 if (!NT_STATUS_IS_OK(status)) {
902 DEBUG(10, ("call_nt_transact_create: "
903 "unmarshall_sec_desc failed: %s\n",
905 reply_nterror(req, status);
911 if (!lp_ea_support(SNUM(conn))) {
912 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
913 "EA's not supported.\n",
914 (unsigned int)ea_len));
915 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
920 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
921 "too small (should be more than 10)\n",
922 (unsigned int)ea_len ));
923 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
927 /* We have already checked that ea_len <= data_count here. */
928 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
930 if (ea_list == NULL) {
931 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
936 srvstr_get_path(ctx, params, req->flags2, &fname,
937 params+53, parameter_count-53,
938 STR_TERMINATE, &status);
939 if (!NT_STATUS_IS_OK(status)) {
940 reply_nterror(req, status);
944 status = create_file(conn, req, root_dir_fid, fname, flags,
945 access_mask, file_attributes, share_access,
946 create_disposition, create_options,
947 allocation_size, sd, ea_list,
948 &fsp, &info, &oplock_granted, &sbuf);
950 if(!NT_STATUS_IS_OK(status)) {
951 if (open_was_deferred(req->mid)) {
952 /* We have re-scheduled this call, no error. */
955 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
956 reply_botherror(req, status, ERRDOS, ERRfilexists);
959 reply_nterror(req, status);
964 file_len = sbuf.st_size;
965 fattr = dos_mode(conn,fname,&sbuf);
967 fattr = FILE_ATTRIBUTE_NORMAL;
970 /* Realloc the size of parameters and data we will return */
971 if (flags & EXTENDED_RESPONSE_REQUIRED) {
972 /* Extended response is 32 more byyes. */
977 params = nttrans_realloc(ppparams, param_len);
979 reply_doserror(req, ERRDOS, ERRnomem);
984 SCVAL(p, 0, oplock_granted);
987 SSVAL(p,0,fsp->fnum);
989 if ((create_disposition == FILE_SUPERSEDE)
990 && (info == FILE_WAS_OVERWRITTEN)) {
991 SIVAL(p,0,FILE_WAS_SUPERSEDED);
998 c_timespec = get_create_timespec(
999 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1000 a_timespec = get_atimespec(&sbuf);
1001 m_timespec = get_mtimespec(&sbuf);
1003 if (lp_dos_filetime_resolution(SNUM(conn))) {
1004 dos_filetime_timespec(&c_timespec);
1005 dos_filetime_timespec(&a_timespec);
1006 dos_filetime_timespec(&m_timespec);
1009 put_long_date_timespec(p, c_timespec); /* create time. */
1011 put_long_date_timespec(p, a_timespec); /* access time */
1013 put_long_date_timespec(p, m_timespec); /* write time */
1015 put_long_date_timespec(p, m_timespec); /* change time */
1017 SIVAL(p,0,fattr); /* File Attributes. */
1019 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1021 SOFF_T(p,0,file_len);
1023 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1027 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1029 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1032 if (fsp->is_directory
1033 || can_write_to_file(conn, fname, &sbuf)) {
1034 perms = FILE_GENERIC_ALL;
1036 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1041 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1043 /* Send the required number of replies */
1044 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1049 /****************************************************************************
1050 Reply to a NT CANCEL request.
1051 conn POINTER CAN BE NULL HERE !
1052 ****************************************************************************/
1054 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1057 * Go through and cancel any pending change notifies.
1060 START_PROFILE(SMBntcancel);
1061 remove_pending_change_notify_requests_by_mid(req->mid);
1062 remove_pending_lock_requests_by_mid(req->mid);
1063 srv_cancel_sign_response(req->mid);
1065 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1067 END_PROFILE(SMBntcancel);
1071 /****************************************************************************
1073 ****************************************************************************/
1075 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1076 connection_struct *conn,
1077 struct smb_request *req,
1078 const char *oldname_in,
1079 const char *newname_in,
1082 SMB_STRUCT_STAT sbuf1, sbuf2;
1083 char *oldname = NULL;
1084 char *newname = NULL;
1085 char *last_component_oldname = NULL;
1086 char *last_component_newname = NULL;
1087 files_struct *fsp1,*fsp2;
1091 NTSTATUS status = NT_STATUS_OK;
1096 if (!CAN_WRITE(conn)) {
1097 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1100 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1101 &last_component_oldname, &sbuf1);
1102 if (!NT_STATUS_IS_OK(status)) {
1106 status = check_name(conn, oldname);
1107 if (!NT_STATUS_IS_OK(status)) {
1111 /* Source must already exist. */
1112 if (!VALID_STAT(sbuf1)) {
1113 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1115 /* Ensure attributes match. */
1116 fattr = dos_mode(conn,oldname,&sbuf1);
1117 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1118 return NT_STATUS_NO_SUCH_FILE;
1121 status = unix_convert(ctx, conn, newname_in, False, &newname,
1122 &last_component_newname, &sbuf2);
1123 if (!NT_STATUS_IS_OK(status)) {
1127 status = check_name(conn, newname);
1128 if (!NT_STATUS_IS_OK(status)) {
1132 /* Disallow if newname already exists. */
1133 if (VALID_STAT(sbuf2)) {
1134 return NT_STATUS_OBJECT_NAME_COLLISION;
1137 /* No links from a directory. */
1138 if (S_ISDIR(sbuf1.st_mode)) {
1139 return NT_STATUS_FILE_IS_A_DIRECTORY;
1142 /* Ensure this is within the share. */
1143 status = check_reduced_name(conn, oldname);
1144 if (!NT_STATUS_IS_OK(status)) {
1148 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1151 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1152 FILE_READ_DATA, /* Read-only. */
1153 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1155 0, /* No create options. */
1156 FILE_ATTRIBUTE_NORMAL,
1160 if (!NT_STATUS_IS_OK(status)) {
1164 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1165 FILE_WRITE_DATA, /* Read-only. */
1166 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1168 0, /* No create options. */
1173 if (!NT_STATUS_IS_OK(status)) {
1174 close_file(fsp1,ERROR_CLOSE);
1178 if (sbuf1.st_size) {
1179 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1183 * As we are opening fsp1 read-only we only expect
1184 * an error on close on fsp2 if we are out of space.
1185 * Thus we don't look at the error return from the
1188 close_file(fsp1,NORMAL_CLOSE);
1190 /* Ensure the modtime is set correctly on the destination file. */
1191 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1193 status = close_file(fsp2,NORMAL_CLOSE);
1195 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1196 creates the file. This isn't the correct thing to do in the copy
1198 file_set_dosmode(conn, newname, fattr, &sbuf2,
1199 parent_dirname(newname),false);
1201 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1202 return NT_STATUS_DISK_FULL;
1205 if (!NT_STATUS_IS_OK(status)) {
1206 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1207 nt_errstr(status), oldname, newname));
1212 /****************************************************************************
1213 Reply to a NT rename request.
1214 ****************************************************************************/
1216 void reply_ntrename(connection_struct *conn, struct smb_request *req)
1218 char *oldname = NULL;
1219 char *newname = NULL;
1222 bool src_has_wcard = False;
1223 bool dest_has_wcard = False;
1226 TALLOC_CTX *ctx = talloc_tos();
1228 START_PROFILE(SMBntrename);
1231 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1232 END_PROFILE(SMBntrename);
1236 attrs = SVAL(req->inbuf,smb_vwv0);
1237 rename_type = SVAL(req->inbuf,smb_vwv1);
1239 p = smb_buf(req->inbuf) + 1;
1240 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1241 0, STR_TERMINATE, &status,
1243 if (!NT_STATUS_IS_OK(status)) {
1244 reply_nterror(req, status);
1245 END_PROFILE(SMBntrename);
1249 if( is_ntfs_stream_name(oldname)) {
1250 /* Can't rename a stream. */
1251 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1252 END_PROFILE(SMBntrename);
1256 if (ms_has_wild(oldname)) {
1257 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1258 END_PROFILE(SMBntrename);
1263 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1264 0, STR_TERMINATE, &status,
1266 if (!NT_STATUS_IS_OK(status)) {
1267 reply_nterror(req, status);
1268 END_PROFILE(SMBntrename);
1272 status = resolve_dfspath(ctx, conn,
1273 req->flags2 & FLAGS2_DFS_PATHNAMES,
1276 if (!NT_STATUS_IS_OK(status)) {
1277 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1278 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1279 ERRSRV, ERRbadpath);
1280 END_PROFILE(SMBntrename);
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 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1306 switch(rename_type) {
1307 case RENAME_FLAG_RENAME:
1308 status = rename_internals(ctx, conn, req, oldname,
1309 newname, attrs, False, src_has_wcard,
1312 case RENAME_FLAG_HARD_LINK:
1313 if (src_has_wcard || dest_has_wcard) {
1315 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1317 status = hardlink_internals(ctx,
1323 case RENAME_FLAG_COPY:
1324 if (src_has_wcard || dest_has_wcard) {
1326 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1328 status = copy_internals(ctx, conn, req, oldname,
1332 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1333 status = NT_STATUS_INVALID_PARAMETER;
1336 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1340 if (!NT_STATUS_IS_OK(status)) {
1341 if (open_was_deferred(req->mid)) {
1342 /* We have re-scheduled this call. */
1343 END_PROFILE(SMBntrename);
1347 reply_nterror(req, status);
1348 END_PROFILE(SMBntrename);
1352 reply_outbuf(req, 0, 0);
1354 END_PROFILE(SMBntrename);
1358 /****************************************************************************
1359 Reply to a notify change - queue the request and
1360 don't allow a directory to be opened.
1361 ****************************************************************************/
1363 static void call_nt_transact_notify_change(connection_struct *conn,
1364 struct smb_request *req,
1368 uint32 parameter_count,
1369 char **ppdata, uint32 data_count,
1370 uint32 max_data_count,
1371 uint32 max_param_count)
1373 uint16 *setup = *ppsetup;
1379 if(setup_count < 6) {
1380 reply_doserror(req, ERRDOS, ERRbadfunc);
1384 fsp = file_fsp(SVAL(setup,4));
1385 filter = IVAL(setup, 0);
1386 recursive = (SVAL(setup, 6) != 0) ? True : False;
1388 DEBUG(3,("call_nt_transact_notify_change\n"));
1391 reply_doserror(req, ERRDOS, ERRbadfid);
1396 char *filter_string;
1398 if (!(filter_string = notify_filter_string(NULL, filter))) {
1399 reply_nterror(req,NT_STATUS_NO_MEMORY);
1403 DEBUG(3,("call_nt_transact_notify_change: notify change "
1404 "called on %s, filter = %s, recursive = %d\n",
1405 fsp->fsp_name, filter_string, recursive));
1407 TALLOC_FREE(filter_string);
1410 if((!fsp->is_directory) || (conn != fsp->conn)) {
1411 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1415 if (fsp->notify == NULL) {
1417 status = change_notify_create(fsp, filter, recursive);
1419 if (!NT_STATUS_IS_OK(status)) {
1420 DEBUG(10, ("change_notify_create returned %s\n",
1421 nt_errstr(status)));
1422 reply_nterror(req, status);
1427 if (fsp->notify->num_changes != 0) {
1430 * We've got changes pending, respond immediately
1434 * TODO: write a torture test to check the filtering behaviour
1438 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
1441 * change_notify_reply() above has independently sent its
1448 * No changes pending, queue the request
1451 status = change_notify_add_request(req->inbuf, max_param_count, filter,
1453 if (!NT_STATUS_IS_OK(status)) {
1454 reply_nterror(req, status);
1459 /****************************************************************************
1460 Reply to an NT transact rename command.
1461 ****************************************************************************/
1463 static void call_nt_transact_rename(connection_struct *conn,
1464 struct smb_request *req,
1465 uint16 **ppsetup, uint32 setup_count,
1466 char **ppparams, uint32 parameter_count,
1467 char **ppdata, uint32 data_count,
1468 uint32 max_data_count)
1470 char *params = *ppparams;
1471 char *new_name = NULL;
1472 files_struct *fsp = NULL;
1473 bool replace_if_exists = False;
1474 bool dest_has_wcard = False;
1476 TALLOC_CTX *ctx = talloc_tos();
1478 if(parameter_count < 5) {
1479 reply_doserror(req, ERRDOS, ERRbadfunc);
1483 fsp = file_fsp(SVAL(params, 0));
1484 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1485 if (!check_fsp(conn, req, fsp, ¤t_user)) {
1488 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1489 parameter_count - 4,
1490 STR_TERMINATE, &status, &dest_has_wcard);
1491 if (!NT_STATUS_IS_OK(status)) {
1492 reply_nterror(req, status);
1496 status = rename_internals(ctx,
1506 if (!NT_STATUS_IS_OK(status)) {
1507 if (open_was_deferred(req->mid)) {
1508 /* We have re-scheduled this call. */
1511 reply_nterror(req, status);
1516 * Rename was successful.
1518 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
1520 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1521 fsp->fsp_name, new_name));
1526 /******************************************************************************
1527 Fake up a completely empty SD.
1528 *******************************************************************************/
1530 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1534 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1536 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1537 return NT_STATUS_NO_MEMORY;
1540 return NT_STATUS_OK;
1543 /****************************************************************************
1544 Reply to query a security descriptor.
1545 ****************************************************************************/
1547 static void call_nt_transact_query_security_desc(connection_struct *conn,
1548 struct smb_request *req,
1552 uint32 parameter_count,
1555 uint32 max_data_count)
1557 char *params = *ppparams;
1558 char *data = *ppdata;
1560 SEC_DESC *psd = NULL;
1562 uint32 security_info_wanted;
1563 TALLOC_CTX *mem_ctx;
1564 files_struct *fsp = NULL;
1567 if(parameter_count < 8) {
1568 reply_doserror(req, ERRDOS, ERRbadfunc);
1572 fsp = file_fsp(SVAL(params,0));
1574 reply_doserror(req, ERRDOS, ERRbadfid);
1578 security_info_wanted = IVAL(params,4);
1580 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1581 (unsigned int)security_info_wanted ));
1583 params = nttrans_realloc(ppparams, 4);
1584 if(params == NULL) {
1585 reply_doserror(req, ERRDOS, ERRnomem);
1589 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
1590 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1591 reply_doserror(req, ERRDOS, ERRnomem);
1596 * Get the permissions to return.
1599 if (!lp_nt_acl_support(SNUM(conn))) {
1600 status = get_null_nt_acl(mem_ctx, &psd);
1602 status = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd,
1603 security_info_wanted, &psd);
1606 if (!NT_STATUS_IS_OK(status)) {
1607 talloc_destroy(mem_ctx);
1608 reply_nterror(req, status);
1612 sd_size = sec_desc_size(psd);
1614 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1616 SIVAL(params,0,(uint32)sd_size);
1618 if(max_data_count < sd_size) {
1620 send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
1621 params, 4, *ppdata, 0);
1622 talloc_destroy(mem_ctx);
1627 * Allocate the data we will point this at.
1630 data = nttrans_realloc(ppdata, sd_size);
1632 talloc_destroy(mem_ctx);
1633 reply_doserror(req, ERRDOS, ERRnomem);
1638 * Init the parse struct we will marshall into.
1641 prs_init(&pd, 0, mem_ctx, MARSHALL);
1644 * Setup the prs_struct to point at the memory we just
1648 prs_give_memory( &pd, data, (uint32)sd_size, False);
1651 * Finally, linearize into the outgoing buffer.
1654 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1655 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1656 security descriptor.\n"));
1658 * Return access denied for want of a better error message..
1660 talloc_destroy(mem_ctx);
1661 reply_unixerror(req, ERRDOS, ERRnoaccess);
1666 * Now we can delete the security descriptor.
1669 talloc_destroy(mem_ctx);
1671 send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1675 /****************************************************************************
1676 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1677 ****************************************************************************/
1679 static void call_nt_transact_set_security_desc(connection_struct *conn,
1680 struct smb_request *req,
1684 uint32 parameter_count,
1687 uint32 max_data_count)
1689 char *params= *ppparams;
1690 char *data = *ppdata;
1691 files_struct *fsp = NULL;
1692 uint32 security_info_sent = 0;
1695 if(parameter_count < 8) {
1696 reply_doserror(req, ERRDOS, ERRbadfunc);
1700 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
1701 reply_doserror(req, ERRDOS, ERRbadfid);
1705 if(!lp_nt_acl_support(SNUM(conn))) {
1709 security_info_sent = IVAL(params,4);
1711 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1712 (unsigned int)security_info_sent ));
1714 if (data_count == 0) {
1715 reply_doserror(req, ERRDOS, ERRnoaccess);
1719 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
1720 reply_nterror(req, nt_status);
1726 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
1730 /****************************************************************************
1732 ****************************************************************************/
1734 static void call_nt_transact_ioctl(connection_struct *conn,
1735 struct smb_request *req,
1736 uint16 **ppsetup, uint32 setup_count,
1737 char **ppparams, uint32 parameter_count,
1738 char **ppdata, uint32 data_count,
1739 uint32 max_data_count)
1746 static bool logged_message;
1747 char *pdata = *ppdata;
1749 if (setup_count != 8) {
1750 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1751 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1755 function = IVAL(*ppsetup, 0);
1756 fidnum = SVAL(*ppsetup, 4);
1757 isFSctl = CVAL(*ppsetup, 6);
1758 compfilter = CVAL(*ppsetup, 7);
1760 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1761 function, fidnum, isFSctl, compfilter));
1763 fsp=file_fsp(fidnum);
1764 /* this check is done in each implemented function case for now
1765 because I don't want to break anything... --metze
1766 FSP_BELONGS_CONN(fsp,conn);*/
1769 case FSCTL_SET_SPARSE:
1770 /* pretend this succeeded - tho strictly we should
1771 mark the file sparse (if the local fs supports it)
1772 so we can know if we need to pre-allocate or not */
1774 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1775 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
1778 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1780 unsigned char objid[16];
1782 /* This should return the object-id on this file.
1783 * I think I'll make this be the inode+dev. JRA.
1786 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1788 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
1793 pdata = nttrans_realloc(ppdata, data_count);
1794 if (pdata == NULL) {
1795 reply_nterror(req, NT_STATUS_NO_MEMORY);
1798 push_file_id_16(pdata, &fsp->file_id);
1799 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1800 push_file_id_16(pdata+32, &fsp->file_id);
1801 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
1806 case FSCTL_GET_REPARSE_POINT:
1807 /* pretend this fail - my winXP does it like this
1811 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1812 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1815 case FSCTL_SET_REPARSE_POINT:
1816 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1820 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1821 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1824 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1827 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1828 * and return their volume names. If max_data_count is 16, then it is just
1829 * asking for the number of volumes and length of the combined names.
1831 * pdata is the data allocated by our caller, but that uses
1832 * total_data_count (which is 0 in our case) rather than max_data_count.
1833 * Allocate the correct amount and return the pointer to let
1834 * it be deallocated when we return.
1836 SHADOW_COPY_DATA *shadow_data = NULL;
1837 TALLOC_CTX *shadow_mem_ctx = NULL;
1838 bool labels = False;
1839 uint32 labels_data_count = 0;
1843 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
1847 if (max_data_count < 16) {
1848 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1850 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1854 if (max_data_count > 16) {
1858 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1859 if (shadow_mem_ctx == NULL) {
1860 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1861 reply_nterror(req, NT_STATUS_NO_MEMORY);
1865 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1866 if (shadow_data == NULL) {
1867 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1868 talloc_destroy(shadow_mem_ctx);
1869 reply_nterror(req, NT_STATUS_NO_MEMORY);
1873 shadow_data->mem_ctx = shadow_mem_ctx;
1876 * Call the VFS routine to actually do the work.
1878 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1879 talloc_destroy(shadow_data->mem_ctx);
1880 if (errno == ENOSYS) {
1881 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1882 conn->connectpath));
1883 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1886 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1887 conn->connectpath));
1888 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1893 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1898 data_count = 12+labels_data_count+4;
1901 if (max_data_count<data_count) {
1902 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1903 max_data_count,data_count));
1904 talloc_destroy(shadow_data->mem_ctx);
1905 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1909 pdata = nttrans_realloc(ppdata, data_count);
1910 if (pdata == NULL) {
1911 talloc_destroy(shadow_data->mem_ctx);
1912 reply_nterror(req, NT_STATUS_NO_MEMORY);
1918 /* num_volumes 4 bytes */
1919 SIVAL(pdata,0,shadow_data->num_volumes);
1922 /* num_labels 4 bytes */
1923 SIVAL(pdata,4,shadow_data->num_volumes);
1926 /* needed_data_count 4 bytes */
1927 SIVAL(pdata,8,labels_data_count);
1931 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1932 shadow_data->num_volumes,fsp->fsp_name));
1933 if (labels && shadow_data->labels) {
1934 for (i=0;i<shadow_data->num_volumes;i++) {
1935 srvstr_push(pdata, req->flags2,
1936 cur_pdata, shadow_data->labels[i],
1937 2*sizeof(SHADOW_COPY_LABEL),
1938 STR_UNICODE|STR_TERMINATE);
1939 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1940 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1944 talloc_destroy(shadow_data->mem_ctx);
1946 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
1952 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1954 /* pretend this succeeded -
1956 * we have to send back a list with all files owned by this SID
1958 * but I have to check that --metze
1962 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1964 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1966 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
1970 /* unknown 4 bytes: this is not the length of the sid :-( */
1971 /*unknown = IVAL(pdata,0);*/
1973 sid_parse(pdata+4,sid_len,&sid);
1974 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
1976 if (!sid_to_uid(&sid, &uid)) {
1977 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
1978 sid_string_static(&sid),(unsigned long)sid_len));
1982 /* we can take a look at the find source :-)
1984 * find ./ -uid $uid -name '*' is what we need here
1987 * and send 4bytes len and then NULL terminated unicode strings
1990 * but I don't know how to deal with the paged results
1991 * (maybe we can hang the result anywhere in the fsp struct)
1993 * we don't send all files at once
1994 * and at the next we should *not* start from the beginning,
1995 * so we have to cache the result
2000 /* this works for now... */
2001 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2005 if (!logged_message) {
2006 logged_message = True; /* Only print this once... */
2007 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2012 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2016 #ifdef HAVE_SYS_QUOTAS
2017 /****************************************************************************
2018 Reply to get user quota
2019 ****************************************************************************/
2021 static void call_nt_transact_get_user_quota(connection_struct *conn,
2022 struct smb_request *req,
2026 uint32 parameter_count,
2029 uint32 max_data_count)
2031 NTSTATUS nt_status = NT_STATUS_OK;
2032 char *params = *ppparams;
2033 char *pdata = *ppdata;
2035 int data_len=0,param_len=0;
2038 files_struct *fsp = NULL;
2042 bool start_enum = True;
2043 SMB_NTQUOTA_STRUCT qt;
2044 SMB_NTQUOTA_LIST *tmp_list;
2045 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2050 if (current_user.ut.uid != 0) {
2051 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2052 lp_servicename(SNUM(conn)),conn->user));
2053 reply_doserror(req, ERRDOS, ERRnoaccess);
2058 * Ensure minimum number of parameters sent.
2061 if (parameter_count < 4) {
2062 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2063 reply_doserror(req, ERRDOS, ERRinvalidparam);
2067 /* maybe we can check the quota_fnum */
2068 fsp = file_fsp(SVAL(params,0));
2069 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2070 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2071 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2075 /* the NULL pointer checking for fsp->fake_file_handle->pd
2076 * is done by CHECK_NTQUOTA_HANDLE_OK()
2078 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2080 level = SVAL(params,2);
2082 /* unknown 12 bytes leading in params */
2085 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2086 /* seems that we should continue with the enum here --metze */
2088 if (qt_handle->quota_list!=NULL &&
2089 qt_handle->tmp_list==NULL) {
2092 free_ntquota_list(&(qt_handle->quota_list));
2094 /* Realloc the size of parameters and data we will return */
2096 params = nttrans_realloc(ppparams, param_len);
2097 if(params == NULL) {
2098 reply_doserror(req, ERRDOS, ERRnomem);
2103 SIVAL(params,0,data_len);
2110 case TRANSACT_GET_USER_QUOTA_LIST_START:
2112 if (qt_handle->quota_list==NULL &&
2113 qt_handle->tmp_list==NULL) {
2117 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2118 reply_doserror(req, ERRSRV, ERRerror);
2122 /* Realloc the size of parameters and data we will return */
2124 params = nttrans_realloc(ppparams, param_len);
2125 if(params == NULL) {
2126 reply_doserror(req, ERRDOS, ERRnomem);
2130 /* we should not trust the value in max_data_count*/
2131 max_data_count = MIN(max_data_count,2048);
2133 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2135 reply_doserror(req, ERRDOS, ERRnomem);
2141 /* set params Size of returned Quota Data 4 bytes*/
2142 /* but set it later when we know it */
2144 /* for each entry push the data */
2147 qt_handle->tmp_list = qt_handle->quota_list;
2150 tmp_list = qt_handle->tmp_list;
2152 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2153 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2155 sid_len = sid_size(&tmp_list->quotas->sid);
2156 entry_len = 40 + sid_len;
2158 /* nextoffset entry 4 bytes */
2159 SIVAL(entry,0,entry_len);
2161 /* then the len of the SID 4 bytes */
2162 SIVAL(entry,4,sid_len);
2164 /* unknown data 8 bytes SMB_BIG_UINT */
2165 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2167 /* the used disk space 8 bytes SMB_BIG_UINT */
2168 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2170 /* the soft quotas 8 bytes SMB_BIG_UINT */
2171 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2173 /* the hard quotas 8 bytes SMB_BIG_UINT */
2174 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2176 /* and now the SID */
2177 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2180 qt_handle->tmp_list = tmp_list;
2182 /* overwrite the offset of the last entry */
2183 SIVAL(entry-entry_len,0,0);
2185 data_len = 4+qt_len;
2186 /* overwrite the params quota_data_len */
2187 SIVAL(params,0,data_len);
2191 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2193 /* unknown 4 bytes IVAL(pdata,0) */
2195 if (data_count < 8) {
2196 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2197 reply_doserror(req, ERRDOS, ERRunknownlevel);
2201 sid_len = IVAL(pdata,4);
2202 /* Ensure this is less than 1mb. */
2203 if (sid_len > (1024*1024)) {
2204 reply_doserror(req, ERRDOS, ERRnomem);
2208 if (data_count < 8+sid_len) {
2209 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2210 reply_doserror(req, ERRDOS, ERRunknownlevel);
2214 data_len = 4+40+sid_len;
2216 if (max_data_count < data_len) {
2217 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2218 max_data_count, data_len));
2220 SIVAL(params,0,data_len);
2222 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2226 sid_parse(pdata+8,sid_len,&sid);
2228 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2231 * we have to return zero's in all fields
2232 * instead of returning an error here
2237 /* Realloc the size of parameters and data we will return */
2239 params = nttrans_realloc(ppparams, param_len);
2240 if(params == NULL) {
2241 reply_doserror(req, ERRDOS, ERRnomem);
2245 pdata = nttrans_realloc(ppdata, data_len);
2247 reply_doserror(req, ERRDOS, ERRnomem);
2253 /* set params Size of returned Quota Data 4 bytes*/
2254 SIVAL(params,0,data_len);
2256 /* nextoffset entry 4 bytes */
2259 /* then the len of the SID 4 bytes */
2260 SIVAL(entry,4,sid_len);
2262 /* unknown data 8 bytes SMB_BIG_UINT */
2263 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2265 /* the used disk space 8 bytes SMB_BIG_UINT */
2266 SBIG_UINT(entry,16,qt.usedspace);
2268 /* the soft quotas 8 bytes SMB_BIG_UINT */
2269 SBIG_UINT(entry,24,qt.softlim);
2271 /* the hard quotas 8 bytes SMB_BIG_UINT */
2272 SBIG_UINT(entry,32,qt.hardlim);
2274 /* and now the SID */
2275 sid_linearize(entry+40, sid_len, &sid);
2280 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2281 reply_doserror(req, ERRSRV, ERRerror);
2286 send_nt_replies(req, nt_status, params, param_len,
2290 /****************************************************************************
2291 Reply to set user quota
2292 ****************************************************************************/
2294 static void call_nt_transact_set_user_quota(connection_struct *conn,
2295 struct smb_request *req,
2299 uint32 parameter_count,
2302 uint32 max_data_count)
2304 char *params = *ppparams;
2305 char *pdata = *ppdata;
2306 int data_len=0,param_len=0;
2307 SMB_NTQUOTA_STRUCT qt;
2310 files_struct *fsp = NULL;
2315 if (current_user.ut.uid != 0) {
2316 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2317 lp_servicename(SNUM(conn)),conn->user));
2318 reply_doserror(req, ERRDOS, ERRnoaccess);
2323 * Ensure minimum number of parameters sent.
2326 if (parameter_count < 2) {
2327 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2328 reply_doserror(req, ERRDOS, ERRinvalidparam);
2332 /* maybe we can check the quota_fnum */
2333 fsp = file_fsp(SVAL(params,0));
2334 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2335 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2336 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2340 if (data_count < 40) {
2341 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2342 reply_doserror(req, ERRDOS, ERRunknownlevel);
2346 /* offset to next quota record.
2347 * 4 bytes IVAL(pdata,0)
2352 sid_len = IVAL(pdata,4);
2354 if (data_count < 40+sid_len) {
2355 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2356 reply_doserror(req, ERRDOS, ERRunknownlevel);
2360 /* unknown 8 bytes in pdata
2361 * maybe its the change time in NTTIME
2364 /* the used space 8 bytes (SMB_BIG_UINT)*/
2365 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2366 #ifdef LARGE_SMB_OFF_T
2367 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2368 #else /* LARGE_SMB_OFF_T */
2369 if ((IVAL(pdata,20) != 0)&&
2370 ((qt.usedspace != 0xFFFFFFFF)||
2371 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2372 /* more than 32 bits? */
2373 reply_doserror(req, ERRDOS, ERRunknownlevel);
2376 #endif /* LARGE_SMB_OFF_T */
2378 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2379 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2380 #ifdef LARGE_SMB_OFF_T
2381 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2382 #else /* LARGE_SMB_OFF_T */
2383 if ((IVAL(pdata,28) != 0)&&
2384 ((qt.softlim != 0xFFFFFFFF)||
2385 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2386 /* more than 32 bits? */
2387 reply_doserror(req, ERRDOS, ERRunknownlevel);
2390 #endif /* LARGE_SMB_OFF_T */
2392 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2393 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2394 #ifdef LARGE_SMB_OFF_T
2395 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2396 #else /* LARGE_SMB_OFF_T */
2397 if ((IVAL(pdata,36) != 0)&&
2398 ((qt.hardlim != 0xFFFFFFFF)||
2399 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2400 /* more than 32 bits? */
2401 reply_doserror(req, ERRDOS, ERRunknownlevel);
2404 #endif /* LARGE_SMB_OFF_T */
2406 sid_parse(pdata+40,sid_len,&sid);
2407 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2409 /* 44 unknown bytes left... */
2411 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2412 reply_doserror(req, ERRSRV, ERRerror);
2416 send_nt_replies(req, NT_STATUS_OK, params, param_len,
2419 #endif /* HAVE_SYS_QUOTAS */
2421 static void handle_nttrans(connection_struct *conn,
2422 struct trans_state *state,
2423 struct smb_request *req)
2425 if (Protocol >= PROTOCOL_NT1) {
2426 req->flags2 |= 0x40; /* IS_LONG_NAME */
2427 SSVAL(req->inbuf,smb_flg2,req->flags2);
2430 /* Now we must call the relevant NT_TRANS function */
2431 switch(state->call) {
2432 case NT_TRANSACT_CREATE:
2434 START_PROFILE(NT_transact_create);
2435 call_nt_transact_create(
2437 &state->setup, state->setup_count,
2438 &state->param, state->total_param,
2439 &state->data, state->total_data,
2440 state->max_data_return);
2441 END_PROFILE(NT_transact_create);
2445 case NT_TRANSACT_IOCTL:
2447 START_PROFILE(NT_transact_ioctl);
2448 call_nt_transact_ioctl(
2450 &state->setup, state->setup_count,
2451 &state->param, state->total_param,
2452 &state->data, state->total_data,
2453 state->max_data_return);
2454 END_PROFILE(NT_transact_ioctl);
2458 case NT_TRANSACT_SET_SECURITY_DESC:
2460 START_PROFILE(NT_transact_set_security_desc);
2461 call_nt_transact_set_security_desc(
2463 &state->setup, state->setup_count,
2464 &state->param, state->total_param,
2465 &state->data, state->total_data,
2466 state->max_data_return);
2467 END_PROFILE(NT_transact_set_security_desc);
2471 case NT_TRANSACT_NOTIFY_CHANGE:
2473 START_PROFILE(NT_transact_notify_change);
2474 call_nt_transact_notify_change(
2476 &state->setup, state->setup_count,
2477 &state->param, state->total_param,
2478 &state->data, state->total_data,
2479 state->max_data_return,
2480 state->max_param_return);
2481 END_PROFILE(NT_transact_notify_change);
2485 case NT_TRANSACT_RENAME:
2487 START_PROFILE(NT_transact_rename);
2488 call_nt_transact_rename(
2490 &state->setup, state->setup_count,
2491 &state->param, state->total_param,
2492 &state->data, state->total_data,
2493 state->max_data_return);
2494 END_PROFILE(NT_transact_rename);
2498 case NT_TRANSACT_QUERY_SECURITY_DESC:
2500 START_PROFILE(NT_transact_query_security_desc);
2501 call_nt_transact_query_security_desc(
2503 &state->setup, state->setup_count,
2504 &state->param, state->total_param,
2505 &state->data, state->total_data,
2506 state->max_data_return);
2507 END_PROFILE(NT_transact_query_security_desc);
2511 #ifdef HAVE_SYS_QUOTAS
2512 case NT_TRANSACT_GET_USER_QUOTA:
2514 START_PROFILE(NT_transact_get_user_quota);
2515 call_nt_transact_get_user_quota(
2517 &state->setup, state->setup_count,
2518 &state->param, state->total_param,
2519 &state->data, state->total_data,
2520 state->max_data_return);
2521 END_PROFILE(NT_transact_get_user_quota);
2525 case NT_TRANSACT_SET_USER_QUOTA:
2527 START_PROFILE(NT_transact_set_user_quota);
2528 call_nt_transact_set_user_quota(
2530 &state->setup, state->setup_count,
2531 &state->param, state->total_param,
2532 &state->data, state->total_data,
2533 state->max_data_return);
2534 END_PROFILE(NT_transact_set_user_quota);
2537 #endif /* HAVE_SYS_QUOTAS */
2540 /* Error in request */
2541 DEBUG(0,("handle_nttrans: Unknown request %d in "
2542 "nttrans call\n", state->call));
2543 reply_doserror(req, ERRSRV, ERRerror);
2549 /****************************************************************************
2550 Reply to a SMBNTtrans.
2551 ****************************************************************************/
2553 void reply_nttrans(connection_struct *conn, struct smb_request *req)
2559 uint16 function_code;
2561 struct trans_state *state;
2564 START_PROFILE(SMBnttrans);
2566 if (req->wct < 19) {
2567 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2568 END_PROFILE(SMBnttrans);
2572 size = smb_len(req->inbuf) + 4;
2573 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
2574 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
2575 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
2576 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
2577 function_code = SVAL(req->inbuf, smb_nt_Function);
2579 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2580 reply_doserror(req, ERRSRV, ERRaccess);
2581 END_PROFILE(SMBnttrans);
2585 result = allow_new_trans(conn->pending_trans, req->mid);
2586 if (!NT_STATUS_IS_OK(result)) {
2587 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2588 reply_nterror(req, result);
2589 END_PROFILE(SMBnttrans);
2593 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
2594 reply_doserror(req, ERRSRV, ERRaccess);
2595 END_PROFILE(SMBnttrans);
2599 state->cmd = SMBnttrans;
2601 state->mid = req->mid;
2602 state->vuid = req->vuid;
2603 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
2605 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
2606 state->param = NULL;
2607 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
2608 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
2610 /* setup count is in *words* */
2611 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
2612 state->setup = NULL;
2613 state->call = function_code;
2616 * All nttrans messages we handle have smb_wct == 19 +
2617 * state->setup_count. Ensure this is so as a sanity check.
2620 if(req->wct != 19 + (state->setup_count/2)) {
2621 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2622 req->wct, 19 + (state->setup_count/2)));
2626 /* Don't allow more than 128mb for each value. */
2627 if ((state->total_data > (1024*1024*128)) ||
2628 (state->total_param > (1024*1024*128))) {
2629 reply_doserror(req, ERRDOS, ERRnomem);
2630 END_PROFILE(SMBnttrans);
2634 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2637 if (state->total_data) {
2638 /* Can't use talloc here, the core routines do realloc on the
2639 * params and data. */
2640 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2641 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2642 "bytes !\n", (unsigned int)state->total_data));
2644 reply_doserror(req, ERRDOS, ERRnomem);
2645 END_PROFILE(SMBnttrans);
2648 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
2650 if ((smb_base(req->inbuf)+dsoff+dscnt
2651 > (char *)req->inbuf + size) ||
2652 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
2655 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2658 if (state->total_param) {
2659 /* Can't use talloc here, the core routines do realloc on the
2660 * params and data. */
2661 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2662 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2663 "bytes !\n", (unsigned int)state->total_param));
2664 SAFE_FREE(state->data);
2666 reply_doserror(req, ERRDOS, ERRnomem);
2667 END_PROFILE(SMBnttrans);
2670 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
2672 if ((smb_base(req->inbuf)+psoff+pscnt
2673 > (char *)req->inbuf + size) ||
2674 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
2677 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2680 state->received_data = dscnt;
2681 state->received_param = pscnt;
2683 if(state->setup_count > 0) {
2684 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2685 state->setup_count));
2686 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2687 if (state->setup == NULL) {
2688 DEBUG(0,("reply_nttrans : Out of memory\n"));
2689 SAFE_FREE(state->data);
2690 SAFE_FREE(state->param);
2692 reply_doserror(req, ERRDOS, ERRnomem);
2693 END_PROFILE(SMBnttrans);
2697 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2698 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2701 if (smb_nt_SetupStart + state->setup_count > size) {
2705 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
2706 state->setup_count);
2707 dump_data(10, (uint8 *)state->setup, state->setup_count);
2710 if ((state->received_data == state->total_data) &&
2711 (state->received_param == state->total_param)) {
2712 handle_nttrans(conn, state, req);
2713 SAFE_FREE(state->param);
2714 SAFE_FREE(state->data);
2716 END_PROFILE(SMBnttrans);
2720 DLIST_ADD(conn->pending_trans, state);
2722 /* We need to send an interim response then receive the rest
2723 of the parameter/data bytes */
2724 reply_outbuf(req, 0, 0);
2725 show_msg((char *)req->outbuf);
2726 END_PROFILE(SMBnttrans);
2731 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2732 SAFE_FREE(state->data);
2733 SAFE_FREE(state->param);
2735 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2736 END_PROFILE(SMBnttrans);
2740 /****************************************************************************
2741 Reply to a SMBnttranss
2742 ****************************************************************************/
2744 void reply_nttranss(connection_struct *conn, struct smb_request *req)
2746 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
2747 struct trans_state *state;
2751 START_PROFILE(SMBnttranss);
2753 show_msg((char *)req->inbuf);
2755 if (req->wct < 18) {
2756 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2757 END_PROFILE(SMBnttranss);
2761 for (state = conn->pending_trans; state != NULL;
2762 state = state->next) {
2763 if (state->mid == req->mid) {
2768 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2769 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2770 END_PROFILE(SMBnttranss);
2774 /* Revise state->total_param and state->total_data in case they have
2775 changed downwards */
2776 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
2777 < state->total_param) {
2778 state->total_param = IVAL(req->inbuf,
2779 smb_nts_TotalParameterCount);
2781 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
2782 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
2785 size = smb_len(req->inbuf) + 4;
2787 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
2788 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
2789 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
2791 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
2792 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
2793 doff = IVAL(req->inbuf, smb_nts_DataOffset);
2795 state->received_param += pcnt;
2796 state->received_data += dcnt;
2798 if ((state->received_data > state->total_data) ||
2799 (state->received_param > state->total_param))
2803 if (pdisp+pcnt > state->total_param)
2805 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
2807 if (pdisp > state->total_param)
2809 if ((smb_base(req->inbuf) + poff + pcnt
2810 > (char *)req->inbuf + size) ||
2811 (smb_base(req->inbuf) + poff + pcnt
2812 < smb_base(req->inbuf)))
2814 if (state->param + pdisp < state->param)
2817 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2822 if (ddisp+dcnt > state->total_data)
2824 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
2826 if (ddisp > state->total_data)
2828 if ((smb_base(req->inbuf) + doff + dcnt
2829 > (char *)req->inbuf + size) ||
2830 (smb_base(req->inbuf) + doff + dcnt
2831 < smb_base(req->inbuf)))
2833 if (state->data + ddisp < state->data)
2836 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2840 if ((state->received_param < state->total_param) ||
2841 (state->received_data < state->total_data)) {
2842 END_PROFILE(SMBnttranss);
2847 * construct_reply_common will copy smb_com from inbuf to
2848 * outbuf. SMBnttranss is wrong here.
2850 SCVAL(req->inbuf,smb_com,SMBnttrans);
2852 handle_nttrans(conn, state, req);
2854 DLIST_REMOVE(conn->pending_trans, state);
2855 SAFE_FREE(state->data);
2856 SAFE_FREE(state->param);
2858 END_PROFILE(SMBnttranss);
2863 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2864 DLIST_REMOVE(conn->pending_trans, state);
2865 SAFE_FREE(state->data);
2866 SAFE_FREE(state->param);
2868 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2869 END_PROFILE(SMBnttranss);