2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-1998
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern enum protocol_types Protocol;
26 extern int smb_read_error;
27 extern struct current_user current_user;
29 static const char *known_nt_pipes[] = {
50 static char *nttrans_realloc(char **ptr, size_t size)
53 smb_panic("nttrans_realloc() called with NULL ptr");
56 *ptr = (char *)SMB_REALLOC(*ptr, size);
60 memset(*ptr,'\0',size);
64 /****************************************************************************
65 Send the required number of replies back.
66 We assume all fields other than the data fields are
67 set correctly for the type of call.
68 HACK ! Always assumes smb_setup field is zero.
69 ****************************************************************************/
71 int send_nt_replies(const char *inbuf,
80 int data_to_send = datasize;
81 int params_to_send = paramsize;
85 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
86 int alignment_offset = 3;
87 int data_alignment_offset = 0;
90 * Initially set the wcnt area to be 18 - this is true for all
94 set_message(inbuf,outbuf,18,0,True);
96 if (NT_STATUS_V(nt_error)) {
101 * If there genuinely are no parameters or data to send just send
105 if(params_to_send == 0 && data_to_send == 0) {
107 if (!send_smb(smbd_server_fd(),outbuf)) {
108 exit_server_cleanly("send_nt_replies: send_smb failed.");
114 * When sending params and data ensure that both are nicely aligned.
115 * Only do this alignment when there is also data to send - else
116 * can cause NT redirector problems.
119 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
120 data_alignment_offset = 4 - (params_to_send % 4);
124 * Space is bufsize minus Netbios over TCP header minus SMB header.
125 * The alignment_offset is to align the param bytes on a four byte
126 * boundary (2 bytes for data len, one byte pad).
127 * NT needs this to work correctly.
130 useable_space = bufsize - ((smb_buf(outbuf)+
131 alignment_offset+data_alignment_offset) -
135 * useable_space can never be more than max_send minus the
139 useable_space = MIN(useable_space,
140 max_send - (alignment_offset+data_alignment_offset));
143 while (params_to_send || data_to_send) {
146 * Calculate whether we will totally or partially fill this packet.
149 total_sent_thistime = params_to_send + data_to_send +
150 alignment_offset + data_alignment_offset;
153 * We can never send more than useable_space.
156 total_sent_thistime = MIN(total_sent_thistime, useable_space);
158 set_message(inbuf,outbuf, 18, total_sent_thistime, True);
161 * Set total params and data to be sent.
164 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
165 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
168 * Calculate how many parameters and data we can fit into
169 * this packet. Parameters get precedence.
172 params_sent_thistime = MIN(params_to_send,useable_space);
173 data_sent_thistime = useable_space - params_sent_thistime;
174 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
176 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
178 if(params_sent_thistime == 0) {
179 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
180 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
183 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
184 * parameter bytes, however the first 4 bytes of outbuf are
185 * the Netbios over TCP header. Thus use smb_base() to subtract
186 * them from the calculation.
189 SIVAL(outbuf,smb_ntr_ParameterOffset,
190 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
192 * Absolute displacement of param bytes sent in this packet.
195 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
199 * Deal with the data portion.
202 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
204 if(data_sent_thistime == 0) {
205 SIVAL(outbuf,smb_ntr_DataOffset,0);
206 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
209 * The offset of the data bytes is the offset of the
210 * parameter bytes plus the number of parameters being sent this time.
213 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
214 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
215 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
219 * Copy the param bytes into the packet.
222 if(params_sent_thistime) {
223 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
227 * Copy in the data bytes
230 if(data_sent_thistime) {
231 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
232 data_alignment_offset,pd,data_sent_thistime);
235 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
236 params_sent_thistime, data_sent_thistime, useable_space));
237 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
238 params_to_send, data_to_send, paramsize, datasize));
240 /* Send the packet */
242 if (!send_smb(smbd_server_fd(),outbuf)) {
243 exit_server_cleanly("send_nt_replies: send_smb failed.");
246 pp += params_sent_thistime;
247 pd += data_sent_thistime;
249 params_to_send -= params_sent_thistime;
250 data_to_send -= data_sent_thistime;
256 if(params_to_send < 0 || data_to_send < 0) {
257 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
258 params_to_send, data_to_send));
266 /****************************************************************************
267 Is it an NTFS stream name ?
268 ****************************************************************************/
270 BOOL is_ntfs_stream_name(const char *fname)
272 if (lp_posix_pathnames()) {
275 return (strchr_m(fname, ':') != NULL) ? True : False;
278 /****************************************************************************
280 ****************************************************************************/
282 static BOOL saved_case_sensitive;
283 static BOOL saved_case_preserve;
284 static BOOL saved_short_case_preserve;
286 /****************************************************************************
288 ****************************************************************************/
290 static uint32 set_posix_case_semantics(connection_struct *conn, uint32 file_attributes)
292 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
293 return file_attributes;
296 saved_case_sensitive = conn->case_sensitive;
297 saved_case_preserve = conn->case_preserve;
298 saved_short_case_preserve = conn->short_case_preserve;
301 conn->case_sensitive = True;
302 conn->case_preserve = True;
303 conn->short_case_preserve = True;
305 return (file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
308 /****************************************************************************
309 Restore case semantics.
310 ****************************************************************************/
312 static void restore_case_semantics(connection_struct *conn, uint32 file_attributes)
314 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
318 conn->case_sensitive = saved_case_sensitive;
319 conn->case_preserve = saved_case_preserve;
320 conn->short_case_preserve = saved_short_case_preserve;
323 /****************************************************************************
324 Reply to an NT create and X call on a pipe.
325 ****************************************************************************/
327 static int nt_open_pipe(char *fname, connection_struct *conn,
328 char *inbuf, char *outbuf, int *ppnum)
330 smb_np_struct *p = NULL;
331 uint16 vuid = SVAL(inbuf, smb_uid);
334 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
336 /* See if it is one we want to handle. */
338 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
339 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
342 for( i = 0; known_nt_pipes[i]; i++ ) {
343 if( strequal(fname,known_nt_pipes[i])) {
348 if ( known_nt_pipes[i] == NULL ) {
349 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
352 /* Strip \\ off the name. */
355 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
357 p = open_rpc_pipe_p(fname, conn, vuid);
359 return(ERROR_DOS(ERRSRV,ERRnofids));
362 /* TODO: Add pipe to db */
364 if ( !store_pipe_opendb( p ) ) {
365 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
372 /****************************************************************************
373 Reply to an NT create and X call for pipes.
374 ****************************************************************************/
376 static int do_ntcreate_pipe_open(connection_struct *conn,
377 char *inbuf,char *outbuf,int length,int bufsize)
383 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
385 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
387 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0) {
392 * Deal with pipe return.
395 if (flags & EXTENDED_RESPONSE_REQUIRED) {
396 /* This is very strange. We
397 * return 50 words, but only set
398 * the wcnt to 42 ? It's definately
399 * what happens on the wire....
401 set_message(inbuf,outbuf,50,0,True);
402 SCVAL(outbuf,smb_wct,42);
404 set_message(inbuf,outbuf,34,0,True);
407 p = outbuf + smb_vwv2;
411 SIVAL(p,0,FILE_WAS_OPENED);
414 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
417 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
419 SSVAL(p,2, 0x5FF); /* ? */
422 if (flags & EXTENDED_RESPONSE_REQUIRED) {
424 SIVAL(p,0,FILE_GENERIC_ALL);
426 * For pipes W2K3 seems to return
428 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
430 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
433 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
435 return chain_reply(inbuf,outbuf,length,bufsize);
438 /****************************************************************************
439 Reply to an NT create and X call for a quota file.
440 ****************************************************************************/
442 int reply_ntcreate_and_X_quota(connection_struct *conn,
447 enum FAKE_FILE_TYPE fake_file_type,
452 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
456 status = open_fake_file(conn, fake_file_type, fname, desired_access,
459 if (!NT_STATUS_IS_OK(status)) {
460 return ERROR_NT(status);
463 set_message(inbuf,outbuf,34,0,True);
465 p = outbuf + smb_vwv2;
467 /* SCVAL(p,0,NO_OPLOCK_RETURN); */
469 SSVAL(p,0,fsp->fnum);
471 DEBUG(5,("reply_ntcreate_and_X_quota: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
473 result = chain_reply(inbuf,outbuf,length,bufsize);
477 /****************************************************************************
478 Reply to an NT create and X call.
479 ****************************************************************************/
481 int reply_ntcreate_and_X(connection_struct *conn,
482 char *inbuf,char *outbuf,int length,int bufsize)
486 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
487 uint32 access_mask = IVAL(inbuf,smb_ntcreate_DesiredAccess);
488 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
489 uint32 new_file_attributes;
490 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
491 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
492 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
493 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
494 /* Breakout the oplock request bits so we can set the
495 reply bits separately. */
496 int oplock_request = 0;
498 SMB_OFF_T file_len = 0;
499 SMB_STRUCT_STAT sbuf;
501 files_struct *fsp = NULL;
503 struct timespec c_timespec;
504 struct timespec a_timespec;
505 struct timespec m_timespec;
506 BOOL extended_oplock_granted = False;
509 START_PROFILE(SMBntcreateX);
511 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
512 "file_attributes = 0x%x, share_access = 0x%x, "
513 "create_disposition = 0x%x create_options = 0x%x "
514 "root_dir_fid = 0x%x\n",
516 (unsigned int)access_mask,
517 (unsigned int)file_attributes,
518 (unsigned int)share_access,
519 (unsigned int)create_disposition,
520 (unsigned int)create_options,
521 (unsigned int)root_dir_fid ));
524 * If it's an IPC, use the pipe handler.
528 if (lp_nt_pipe_support()) {
529 END_PROFILE(SMBntcreateX);
530 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
532 END_PROFILE(SMBntcreateX);
533 return(ERROR_DOS(ERRDOS,ERRnoaccess));
537 if (create_options & FILE_OPEN_BY_FILE_ID) {
538 END_PROFILE(SMBntcreateX);
539 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
546 if(root_dir_fid != 0) {
548 * This filename is relative to a directory fid.
551 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
555 END_PROFILE(SMBntcreateX);
556 return ERROR_DOS(ERRDOS,ERRbadfid);
559 if(!dir_fsp->is_directory) {
561 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
562 if (!NT_STATUS_IS_OK(status)) {
563 END_PROFILE(SMBntcreateX);
564 return ERROR_NT(status);
568 * Check to see if this is a mac fork of some kind.
571 if( is_ntfs_stream_name(fname)) {
572 END_PROFILE(SMBntcreateX);
573 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
577 we need to handle the case when we get a
578 relative open relative to a file and the
579 pathname is blank - this is a reopen!
580 (hint from demyn plantenberg)
583 END_PROFILE(SMBntcreateX);
584 return(ERROR_DOS(ERRDOS,ERRbadfid));
588 * Copy in the base directory name.
591 pstrcpy( fname, dir_fsp->fsp_name );
592 dir_name_len = strlen(fname);
595 * Ensure it ends in a '\'.
598 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
603 srvstr_get_path(inbuf, rel_fname, smb_buf(inbuf), sizeof(rel_fname), 0, STR_TERMINATE, &status);
604 if (!NT_STATUS_IS_OK(status)) {
605 END_PROFILE(SMBntcreateX);
606 return ERROR_NT(status);
608 pstrcat(fname, rel_fname);
610 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
611 if (!NT_STATUS_IS_OK(status)) {
612 END_PROFILE(SMBntcreateX);
613 return ERROR_NT(status);
617 * Check to see if this is a mac fork of some kind.
620 if( is_ntfs_stream_name(fname)) {
621 enum FAKE_FILE_TYPE fake_file_type = is_fake_file(fname);
622 if (fake_file_type!=FAKE_FILE_TYPE_NONE) {
624 * Here we go! support for changing the disk quotas --metze
626 * We need to fake up to open this MAGIC QUOTA file
627 * and return a valid FID.
629 * w2k close this file directly after openening
630 * xp also tries a QUERY_FILE_INFO on the file and then close it
632 result = reply_ntcreate_and_X_quota(conn, inbuf, outbuf, length, bufsize,
633 fake_file_type, fname);
634 END_PROFILE(SMBntcreateX);
637 END_PROFILE(SMBntcreateX);
638 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
644 * Now contruct the smb_open_mode value from the filename,
645 * desired access and the share access.
647 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, fname);
648 if (!NT_STATUS_IS_OK(status)) {
649 END_PROFILE(SMBntcreateX);
650 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
651 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
653 return ERROR_NT(status);
656 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
657 if (oplock_request) {
658 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
662 * Ordinary file or directory.
666 * Check if POSIX semantics are wanted.
669 new_file_attributes = set_posix_case_semantics(conn, file_attributes);
671 status = unix_convert(conn, fname, False, NULL, &sbuf);
672 if (!NT_STATUS_IS_OK(status)) {
673 restore_case_semantics(conn, file_attributes);
674 END_PROFILE(SMBntcreateX);
675 return ERROR_NT(status);
677 /* All file access must go through check_name() */
678 status = check_name(conn, fname);
679 if (!NT_STATUS_IS_OK(status)) {
680 restore_case_semantics(conn, file_attributes);
681 END_PROFILE(SMBntcreateX);
682 return ERROR_NT(status);
685 /* This is the correct thing to do (check every time) but can_delete is
686 expensive (it may have to read the parent directory permissions). So
687 for now we're not doing it unless we have a strong hint the client
688 is really going to delete this file. If the client is forcing FILE_CREATE
689 let the filesystem take care of the permissions. */
691 /* Setting FILE_SHARE_DELETE is the hint. */
693 if (lp_acl_check_permissions(SNUM(conn))
694 && (create_disposition != FILE_CREATE)
695 && (share_access & FILE_SHARE_DELETE)
696 && (access_mask & DELETE_ACCESS)) {
697 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
698 !can_delete_file_in_directory(conn, fname)) {
699 restore_case_semantics(conn, file_attributes);
700 END_PROFILE(SMBntcreateX);
701 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
706 * If it's a request for a directory open, deal with it separately.
709 if(create_options & FILE_DIRECTORY_FILE) {
711 /* Can't open a temp directory. IFS kit test. */
712 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
713 END_PROFILE(SMBntcreateX);
714 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
718 status = open_directory(conn, fname, &sbuf,
726 restore_case_semantics(conn, file_attributes);
728 if(!NT_STATUS_IS_OK(status)) {
729 if (!use_nt_status() && NT_STATUS_EQUAL(
730 status, NT_STATUS_OBJECT_NAME_COLLISION)) {
731 status = NT_STATUS_DOS(ERRDOS, ERRfilexists);
733 END_PROFILE(SMBntcreateX);
734 return ERROR_NT(status);
740 * Ordinary file case.
743 /* NB. We have a potential bug here. If we
744 * cause an oplock break to ourselves, then we
745 * could end up processing filename related
746 * SMB requests whilst we await the oplock
747 * break response. As we may have changed the
748 * filename case semantics to be POSIX-like,
749 * this could mean a filename request could
750 * fail when it should succeed. This is a rare
751 * condition, but eventually we must arrange
752 * to restore the correct case semantics
753 * before issuing an oplock break request to
754 * our client. JRA. */
756 status = open_file_ntcreate(conn,fname,&sbuf,
765 if (!NT_STATUS_IS_OK(status)) {
766 /* We cheat here. There are two cases we
767 * care about. One is a directory rename,
768 * where the NT client will attempt to
769 * open the source directory for
770 * DELETE access. Note that when the
771 * NT client does this it does *not*
772 * set the directory bit in the
773 * request packet. This is translated
774 * into a read/write open
775 * request. POSIX states that any open
776 * for write request on a directory
777 * will generate an EISDIR error, so
778 * we can catch this here and open a
779 * pseudo handle that is flagged as a
780 * directory. The second is an open
781 * for a permissions read only, which
782 * we handle in the open_file_stat case. JRA.
785 if (NT_STATUS_EQUAL(status,
786 NT_STATUS_FILE_IS_A_DIRECTORY)) {
789 * Fail the open if it was explicitly a non-directory file.
792 if (create_options & FILE_NON_DIRECTORY_FILE) {
793 restore_case_semantics(conn, file_attributes);
794 END_PROFILE(SMBntcreateX);
795 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
799 status = open_directory(conn, fname, &sbuf,
807 if(!NT_STATUS_IS_OK(status)) {
808 restore_case_semantics(conn, file_attributes);
809 if (!use_nt_status() && NT_STATUS_EQUAL(
810 status, NT_STATUS_OBJECT_NAME_COLLISION)) {
811 status = NT_STATUS_DOS(ERRDOS, ERRfilexists);
813 END_PROFILE(SMBntcreateX);
814 return ERROR_NT(status);
817 restore_case_semantics(conn, file_attributes);
818 END_PROFILE(SMBntcreateX);
819 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
820 /* We have re-scheduled this call. */
823 return ERROR_NT(status);
828 restore_case_semantics(conn, file_attributes);
830 file_len = sbuf.st_size;
831 fattr = dos_mode(conn,fname,&sbuf);
833 fattr = FILE_ATTRIBUTE_NORMAL;
835 if (!fsp->is_directory && (fattr & aDIR)) {
836 close_file(fsp,ERROR_CLOSE);
837 END_PROFILE(SMBntcreateX);
838 return ERROR_DOS(ERRDOS,ERRnoaccess);
841 /* Save the requested allocation size. */
842 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
843 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize);
844 #ifdef LARGE_SMB_OFF_T
845 allocation_size |= (((SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
847 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
848 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
849 if (fsp->is_directory) {
850 close_file(fsp,ERROR_CLOSE);
851 END_PROFILE(SMBntcreateX);
852 /* Can't set allocation size on a directory. */
853 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
855 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
856 close_file(fsp,ERROR_CLOSE);
857 END_PROFILE(SMBntcreateX);
858 return ERROR_NT(NT_STATUS_DISK_FULL);
861 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
866 * If the caller set the extended oplock request bit
867 * and we granted one (by whatever means) - set the
868 * correct bit for extended oplock reply.
871 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
872 extended_oplock_granted = True;
875 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
876 extended_oplock_granted = True;
879 if (flags & EXTENDED_RESPONSE_REQUIRED) {
880 /* This is very strange. We
881 * return 50 words, but only set
882 * the wcnt to 42 ? It's definately
883 * what happens on the wire....
885 set_message(inbuf,outbuf,50,0,True);
886 SCVAL(outbuf,smb_wct,42);
888 set_message(inbuf,outbuf,34,0,True);
891 p = outbuf + smb_vwv2;
894 * Currently as we don't support level II oplocks we just report
895 * exclusive & batch here.
898 if (extended_oplock_granted) {
899 if (flags & REQUEST_BATCH_OPLOCK) {
900 SCVAL(p,0, BATCH_OPLOCK_RETURN);
902 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
904 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
905 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
907 SCVAL(p,0,NO_OPLOCK_RETURN);
911 SSVAL(p,0,fsp->fnum);
913 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
914 SIVAL(p,0,FILE_WAS_SUPERSEDED);
921 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
922 a_timespec = get_atimespec(&sbuf);
923 m_timespec = get_mtimespec(&sbuf);
925 if (lp_dos_filetime_resolution(SNUM(conn))) {
926 dos_filetime_timespec(&c_timespec);
927 dos_filetime_timespec(&a_timespec);
928 dos_filetime_timespec(&m_timespec);
931 put_long_date_timespec(p, c_timespec); /* create time. */
933 put_long_date_timespec(p, a_timespec); /* access time */
935 put_long_date_timespec(p, m_timespec); /* write time */
937 put_long_date_timespec(p, m_timespec); /* change time */
939 SIVAL(p,0,fattr); /* File Attributes. */
941 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
943 SOFF_T(p,0,file_len);
945 if (flags & EXTENDED_RESPONSE_REQUIRED) {
949 SCVAL(p,0,fsp->is_directory ? 1 : 0);
951 if (flags & EXTENDED_RESPONSE_REQUIRED) {
954 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
955 perms = FILE_GENERIC_ALL;
957 perms = FILE_GENERIC_READ|FILE_EXECUTE;
962 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
964 result = chain_reply(inbuf,outbuf,length,bufsize);
965 END_PROFILE(SMBntcreateX);
969 /****************************************************************************
970 Reply to a NT_TRANSACT_CREATE call to open a pipe.
971 ****************************************************************************/
973 static int do_nt_transact_create_pipe( connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
974 uint16 **ppsetup, uint32 setup_count,
975 char **ppparams, uint32 parameter_count,
976 char **ppdata, uint32 data_count)
979 char *params = *ppparams;
988 * Ensure minimum number of parameters sent.
991 if(parameter_count < 54) {
992 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
993 return ERROR_DOS(ERRDOS,ERRnoaccess);
996 flags = IVAL(params,0);
998 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
999 if (!NT_STATUS_IS_OK(status)) {
1000 return ERROR_NT(status);
1003 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0) {
1007 /* Realloc the size of parameters and data we will return */
1008 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1009 /* Extended response is 32 more byyes. */
1014 params = nttrans_realloc(ppparams, param_len);
1015 if(params == NULL) {
1016 return ERROR_DOS(ERRDOS,ERRnomem);
1020 SCVAL(p,0,NO_OPLOCK_RETURN);
1025 SIVAL(p,0,FILE_WAS_OPENED);
1029 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1032 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1034 SSVAL(p,2, 0x5FF); /* ? */
1037 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1039 SIVAL(p,0,FILE_GENERIC_ALL);
1041 * For pipes W2K3 seems to return
1043 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
1045 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
1048 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1050 /* Send the required number of replies */
1051 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, *ppdata, 0);
1056 /****************************************************************************
1057 Internal fn to set security descriptors.
1058 ****************************************************************************/
1060 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1063 SEC_DESC *psd = NULL;
1064 TALLOC_CTX *mem_ctx;
1067 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
1068 return NT_STATUS_OK;
1072 * Init the parse struct we will unmarshall from.
1075 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1076 DEBUG(0,("set_sd: talloc_init failed.\n"));
1077 return NT_STATUS_NO_MEMORY;
1080 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1083 * Setup the prs_struct to point at the memory we just
1087 prs_give_memory( &pd, data, sd_len, False);
1090 * Finally, unmarshall from the data buffer.
1093 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1094 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1096 * Return access denied for want of a better error message..
1098 talloc_destroy(mem_ctx);
1099 return NT_STATUS_NO_MEMORY;
1102 if (psd->owner_sid==0) {
1103 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1105 if (psd->group_sid==0) {
1106 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1109 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1112 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1115 status = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1117 talloc_destroy(mem_ctx);
1121 /****************************************************************************
1122 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1123 ****************************************************************************/
1125 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1127 struct ea_list *ea_list_head = NULL;
1130 if (data_size < 4) {
1134 while (offset + 4 <= data_size) {
1135 size_t next_offset = IVAL(pdata,offset);
1136 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1142 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1143 if (next_offset == 0) {
1146 offset += next_offset;
1149 return ea_list_head;
1152 /****************************************************************************
1153 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1154 ****************************************************************************/
1156 static int call_nt_transact_create(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1157 uint16 **ppsetup, uint32 setup_count,
1158 char **ppparams, uint32 parameter_count,
1159 char **ppdata, uint32 data_count, uint32 max_data_count)
1162 char *params = *ppparams;
1163 char *data = *ppdata;
1164 /* Breakout the oplock request bits so we can set the reply bits separately. */
1165 int oplock_request = 0;
1167 SMB_OFF_T file_len = 0;
1168 SMB_STRUCT_STAT sbuf;
1170 files_struct *fsp = NULL;
1172 BOOL extended_oplock_granted = False;
1175 uint32 file_attributes;
1176 uint32 new_file_attributes;
1177 uint32 share_access;
1178 uint32 create_disposition;
1179 uint32 create_options;
1182 uint16 root_dir_fid;
1183 struct timespec c_timespec;
1184 struct timespec a_timespec;
1185 struct timespec m_timespec;
1186 struct ea_list *ea_list = NULL;
1187 TALLOC_CTX *ctx = NULL;
1192 DEBUG(5,("call_nt_transact_create\n"));
1195 * If it's an IPC, use the pipe handler.
1199 if (lp_nt_pipe_support()) {
1200 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1202 ppsetup, setup_count,
1203 ppparams, parameter_count,
1204 ppdata, data_count);
1206 return ERROR_DOS(ERRDOS,ERRnoaccess);
1211 * Ensure minimum number of parameters sent.
1214 if(parameter_count < 54) {
1215 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1216 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1219 flags = IVAL(params,0);
1220 access_mask = IVAL(params,8);
1221 file_attributes = IVAL(params,20);
1222 share_access = IVAL(params,24);
1223 create_disposition = IVAL(params,28);
1224 create_options = IVAL(params,32);
1225 sd_len = IVAL(params,36);
1226 ea_len = IVAL(params,40);
1227 root_dir_fid = (uint16)IVAL(params,4);
1229 /* Ensure the data_len is correct for the sd and ea values given. */
1230 if ((ea_len + sd_len > data_count) ||
1231 (ea_len > data_count) || (sd_len > data_count) ||
1232 (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1233 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1234 (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1235 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1239 if (!lp_ea_support(SNUM(conn))) {
1240 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1241 (unsigned int)ea_len ));
1242 return ERROR_NT(NT_STATUS_EAS_NOT_SUPPORTED);
1246 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1247 (unsigned int)ea_len ));
1248 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1252 if (create_options & FILE_OPEN_BY_FILE_ID) {
1253 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1257 * Get the file name.
1260 if(root_dir_fid != 0) {
1262 * This filename is relative to a directory fid.
1264 files_struct *dir_fsp = file_fsp(params,4);
1265 size_t dir_name_len;
1268 return ERROR_DOS(ERRDOS,ERRbadfid);
1271 if(!dir_fsp->is_directory) {
1272 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1273 if (!NT_STATUS_IS_OK(status)) {
1274 return ERROR_NT(status);
1278 * Check to see if this is a mac fork of some kind.
1281 if( is_ntfs_stream_name(fname)) {
1282 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1285 return ERROR_DOS(ERRDOS,ERRbadfid);
1289 * Copy in the base directory name.
1292 pstrcpy( fname, dir_fsp->fsp_name );
1293 dir_name_len = strlen(fname);
1296 * Ensure it ends in a '\'.
1299 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1300 pstrcat(fname, "/");
1306 srvstr_get_path(inbuf, tmpname, params+53, sizeof(tmpname), parameter_count-53, STR_TERMINATE, &status);
1307 if (!NT_STATUS_IS_OK(status)) {
1308 return ERROR_NT(status);
1310 pstrcat(fname, tmpname);
1313 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1314 if (!NT_STATUS_IS_OK(status)) {
1315 return ERROR_NT(status);
1319 * Check to see if this is a mac fork of some kind.
1322 if( is_ntfs_stream_name(fname)) {
1323 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1327 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1328 if (oplock_request) {
1329 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1333 * Ordinary file or directory.
1337 * Check if POSIX semantics are wanted.
1340 new_file_attributes = set_posix_case_semantics(conn, file_attributes);
1342 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, fname);
1343 if (!NT_STATUS_IS_OK(status)) {
1344 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1345 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1347 return ERROR_NT(status);
1350 status = unix_convert(conn, fname, False, NULL, &sbuf);
1351 if (!NT_STATUS_IS_OK(status)) {
1352 restore_case_semantics(conn, file_attributes);
1353 return ERROR_NT(status);
1355 /* All file access must go through check_name() */
1356 status = check_name(conn, fname);
1357 if (!NT_STATUS_IS_OK(status)) {
1358 restore_case_semantics(conn, file_attributes);
1359 return ERROR_NT(status);
1362 /* This is the correct thing to do (check every time) but can_delete is
1363 expensive (it may have to read the parent directory permissions). So
1364 for now we're not doing it unless we have a strong hint the client
1365 is really going to delete this file. If the client is forcing FILE_CREATE
1366 let the filesystem take care of the permissions. */
1368 /* Setting FILE_SHARE_DELETE is the hint. */
1370 if (lp_acl_check_permissions(SNUM(conn))
1371 && (create_disposition != FILE_CREATE)
1372 && (share_access & FILE_SHARE_DELETE)
1373 && (access_mask & DELETE_ACCESS)) {
1374 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
1375 !can_delete_file_in_directory(conn, fname)) {
1376 restore_case_semantics(conn, file_attributes);
1377 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1382 pdata = data + sd_len;
1384 /* We have already checked that ea_len <= data_count here. */
1385 ea_list = read_nttrans_ea_list(tmp_talloc_ctx(), pdata,
1388 restore_case_semantics(conn, file_attributes);
1389 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1394 * If it's a request for a directory open, deal with it separately.
1397 if(create_options & FILE_DIRECTORY_FILE) {
1399 /* Can't open a temp directory. IFS kit test. */
1400 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1401 restore_case_semantics(conn, file_attributes);
1402 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1406 * We will get a create directory here if the Win32
1407 * app specified a security descriptor in the
1408 * CreateDirectory() call.
1412 status = open_directory(conn, fname, &sbuf,
1417 new_file_attributes,
1419 if(!NT_STATUS_IS_OK(status)) {
1420 restore_case_semantics(conn, file_attributes);
1421 return ERROR_NT(status);
1427 * Ordinary file case.
1430 status = open_file_ntcreate(conn,fname,&sbuf,
1435 new_file_attributes,
1439 if (!NT_STATUS_IS_OK(status)) {
1440 if (NT_STATUS_EQUAL(status,
1441 NT_STATUS_FILE_IS_A_DIRECTORY)) {
1444 * Fail the open if it was explicitly a non-directory file.
1447 if (create_options & FILE_NON_DIRECTORY_FILE) {
1448 restore_case_semantics(conn, file_attributes);
1449 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1453 status = open_directory(conn, fname, &sbuf,
1458 new_file_attributes,
1460 if(!NT_STATUS_IS_OK(status)) {
1461 restore_case_semantics(conn, file_attributes);
1462 return ERROR_NT(status);
1465 restore_case_semantics(conn, file_attributes);
1466 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1467 /* We have re-scheduled this call. */
1470 return ERROR_NT(status);
1476 * According to the MS documentation, the only time the security
1477 * descriptor is applied to the opened file is iff we *created* the
1478 * file; an existing file stays the same.
1480 * Also, it seems (from observation) that you can open the file with
1481 * any access mask but you can still write the sd. We need to override
1482 * the granted access before we call set_sd
1483 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1486 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1487 uint32 saved_access_mask = fsp->access_mask;
1489 /* We have already checked that sd_len <= data_count here. */
1491 fsp->access_mask = FILE_GENERIC_ALL;
1493 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1494 if (!NT_STATUS_IS_OK(status)) {
1495 talloc_destroy(ctx);
1496 close_file(fsp,ERROR_CLOSE);
1497 restore_case_semantics(conn, file_attributes);
1498 return ERROR_NT(status);
1500 fsp->access_mask = saved_access_mask;
1503 if (ea_len && (info == FILE_WAS_CREATED)) {
1504 status = set_ea(conn, fsp, fname, ea_list);
1505 if (!NT_STATUS_IS_OK(status)) {
1506 close_file(fsp,ERROR_CLOSE);
1507 restore_case_semantics(conn, file_attributes);
1508 return ERROR_NT(status);
1512 restore_case_semantics(conn, file_attributes);
1514 file_len = sbuf.st_size;
1515 fattr = dos_mode(conn,fname,&sbuf);
1517 fattr = FILE_ATTRIBUTE_NORMAL;
1519 if (!fsp->is_directory && (fattr & aDIR)) {
1520 close_file(fsp,ERROR_CLOSE);
1521 return ERROR_DOS(ERRDOS,ERRnoaccess);
1524 /* Save the requested allocation size. */
1525 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1526 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1527 #ifdef LARGE_SMB_OFF_T
1528 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1530 if (allocation_size && (allocation_size > file_len)) {
1531 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1532 if (fsp->is_directory) {
1533 close_file(fsp,ERROR_CLOSE);
1534 /* Can't set allocation size on a directory. */
1535 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1537 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1538 close_file(fsp,ERROR_CLOSE);
1539 return ERROR_NT(NT_STATUS_DISK_FULL);
1542 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1547 * If the caller set the extended oplock request bit
1548 * and we granted one (by whatever means) - set the
1549 * correct bit for extended oplock reply.
1552 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1553 extended_oplock_granted = True;
1556 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1557 extended_oplock_granted = True;
1560 /* Realloc the size of parameters and data we will return */
1561 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1562 /* Extended response is 32 more byyes. */
1567 params = nttrans_realloc(ppparams, param_len);
1568 if(params == NULL) {
1569 return ERROR_DOS(ERRDOS,ERRnomem);
1573 if (extended_oplock_granted) {
1574 if (flags & REQUEST_BATCH_OPLOCK) {
1575 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1577 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1579 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1580 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1582 SCVAL(p,0,NO_OPLOCK_RETURN);
1586 SSVAL(p,0,fsp->fnum);
1588 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1589 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1596 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1597 a_timespec = get_atimespec(&sbuf);
1598 m_timespec = get_mtimespec(&sbuf);
1600 if (lp_dos_filetime_resolution(SNUM(conn))) {
1601 dos_filetime_timespec(&c_timespec);
1602 dos_filetime_timespec(&a_timespec);
1603 dos_filetime_timespec(&m_timespec);
1606 put_long_date_timespec(p, c_timespec); /* create time. */
1608 put_long_date_timespec(p, a_timespec); /* access time */
1610 put_long_date_timespec(p, m_timespec); /* write time */
1612 put_long_date_timespec(p, m_timespec); /* change time */
1614 SIVAL(p,0,fattr); /* File Attributes. */
1616 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1618 SOFF_T(p,0,file_len);
1620 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1624 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1626 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1629 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1630 perms = FILE_GENERIC_ALL;
1632 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1637 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1639 /* Send the required number of replies */
1640 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, *ppdata, 0);
1645 /****************************************************************************
1646 Reply to a NT CANCEL request.
1647 conn POINTER CAN BE NULL HERE !
1648 ****************************************************************************/
1650 int reply_ntcancel(connection_struct *conn,
1651 char *inbuf,char *outbuf,int length,int bufsize)
1654 * Go through and cancel any pending change notifies.
1657 int mid = SVAL(inbuf,smb_mid);
1658 START_PROFILE(SMBntcancel);
1659 remove_pending_change_notify_requests_by_mid(mid);
1660 remove_pending_lock_requests_by_mid(mid);
1661 srv_cancel_sign_response(mid);
1663 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1665 END_PROFILE(SMBntcancel);
1669 /****************************************************************************
1671 ****************************************************************************/
1673 static NTSTATUS copy_internals(connection_struct *conn, char *oldname, char *newname, uint32 attrs)
1675 SMB_STRUCT_STAT sbuf1, sbuf2;
1676 pstring last_component_oldname;
1677 pstring last_component_newname;
1678 files_struct *fsp1,*fsp2;
1682 NTSTATUS status = NT_STATUS_OK;
1687 if (!CAN_WRITE(conn)) {
1688 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1691 status = unix_convert(conn, oldname, False, last_component_oldname, &sbuf1);
1692 if (!NT_STATUS_IS_OK(status)) {
1696 status = check_name(conn, oldname);
1697 if (!NT_STATUS_IS_OK(status)) {
1701 /* Source must already exist. */
1702 if (!VALID_STAT(sbuf1)) {
1703 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1705 /* Ensure attributes match. */
1706 fattr = dos_mode(conn,oldname,&sbuf1);
1707 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1708 return NT_STATUS_NO_SUCH_FILE;
1711 status = unix_convert(conn, newname, False, last_component_newname, &sbuf2);
1712 if (!NT_STATUS_IS_OK(status)) {
1716 status = check_name(conn, newname);
1717 if (!NT_STATUS_IS_OK(status)) {
1721 /* Disallow if newname already exists. */
1722 if (VALID_STAT(sbuf2)) {
1723 return NT_STATUS_OBJECT_NAME_COLLISION;
1726 /* No links from a directory. */
1727 if (S_ISDIR(sbuf1.st_mode)) {
1728 return NT_STATUS_FILE_IS_A_DIRECTORY;
1731 /* Ensure this is within the share. */
1732 status = reduce_name(conn, oldname);
1733 if (!NT_STATUS_IS_OK(status)) {
1737 DEBUG(10,("copy_internals: doing file copy %s to %s\n", oldname, newname));
1739 status = open_file_ntcreate(conn,oldname,&sbuf1,
1740 FILE_READ_DATA, /* Read-only. */
1741 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1743 0, /* No create options. */
1744 FILE_ATTRIBUTE_NORMAL,
1748 if (!NT_STATUS_IS_OK(status)) {
1752 status = open_file_ntcreate(conn,newname,&sbuf2,
1753 FILE_WRITE_DATA, /* Read-only. */
1754 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1756 0, /* No create options. */
1761 if (!NT_STATUS_IS_OK(status)) {
1762 close_file(fsp1,ERROR_CLOSE);
1766 if (sbuf1.st_size) {
1767 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1771 * As we are opening fsp1 read-only we only expect
1772 * an error on close on fsp2 if we are out of space.
1773 * Thus we don't look at the error return from the
1776 close_file(fsp1,NORMAL_CLOSE);
1778 /* Ensure the modtime is set correctly on the destination file. */
1779 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1781 status = close_file(fsp2,NORMAL_CLOSE);
1783 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1784 creates the file. This isn't the correct thing to do in the copy
1786 file_set_dosmode(conn, newname, fattr, &sbuf2,
1787 parent_dirname(newname));
1789 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1790 return NT_STATUS_DISK_FULL;
1793 if (!NT_STATUS_IS_OK(status)) {
1794 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1795 nt_errstr(status), oldname, newname));
1800 /****************************************************************************
1801 Reply to a NT rename request.
1802 ****************************************************************************/
1804 int reply_ntrename(connection_struct *conn,
1805 char *inbuf,char *outbuf,int length,int bufsize)
1812 BOOL src_has_wcard = False;
1813 BOOL dest_has_wcard = False;
1814 uint32 attrs = SVAL(inbuf,smb_vwv0);
1815 uint16 rename_type = SVAL(inbuf,smb_vwv1);
1817 START_PROFILE(SMBntrename);
1819 p = smb_buf(inbuf) + 1;
1820 p += srvstr_get_path_wcard(inbuf, oldname, p, sizeof(oldname), 0, STR_TERMINATE, &status, &src_has_wcard);
1821 if (!NT_STATUS_IS_OK(status)) {
1822 END_PROFILE(SMBntrename);
1823 return ERROR_NT(status);
1826 if( is_ntfs_stream_name(oldname)) {
1827 /* Can't rename a stream. */
1828 END_PROFILE(SMBntrename);
1829 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1832 if (ms_has_wild(oldname)) {
1833 END_PROFILE(SMBntrename);
1834 return ERROR_NT(NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1838 p += srvstr_get_path_wcard(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, &dest_has_wcard);
1839 if (!NT_STATUS_IS_OK(status)) {
1840 END_PROFILE(SMBntrename);
1841 return ERROR_NT(status);
1844 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, oldname);
1845 if (!NT_STATUS_IS_OK(status)) {
1846 END_PROFILE(SMBntrename);
1847 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1848 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1850 return ERROR_NT(status);
1853 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, newname);
1854 if (!NT_STATUS_IS_OK(status)) {
1855 END_PROFILE(SMBntrename);
1856 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1857 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1859 return ERROR_NT(status);
1862 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1864 switch(rename_type) {
1865 case RENAME_FLAG_RENAME:
1866 status = rename_internals(conn, oldname, newname, attrs, False, src_has_wcard, dest_has_wcard);
1868 case RENAME_FLAG_HARD_LINK:
1869 if (src_has_wcard || dest_has_wcard) {
1871 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1873 status = hardlink_internals(conn, oldname, newname);
1876 case RENAME_FLAG_COPY:
1877 if (src_has_wcard || dest_has_wcard) {
1879 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1881 status = copy_internals(conn, oldname, newname, attrs);
1884 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1885 status = NT_STATUS_INVALID_PARAMETER;
1888 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1892 if (!NT_STATUS_IS_OK(status)) {
1893 END_PROFILE(SMBntrename);
1894 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1895 /* We have re-scheduled this call. */
1898 return ERROR_NT(status);
1901 outsize = set_message(inbuf,outbuf,0,0,False);
1903 END_PROFILE(SMBntrename);
1907 /****************************************************************************
1908 Reply to a notify change - queue the request and
1909 don't allow a directory to be opened.
1910 ****************************************************************************/
1912 static int call_nt_transact_notify_change(connection_struct *conn, char *inbuf,
1913 char *outbuf, int length,
1915 uint16 **ppsetup, uint32 setup_count,
1917 uint32 parameter_count,
1918 char **ppdata, uint32 data_count,
1919 uint32 max_data_count,
1920 uint32 max_param_count)
1922 uint16 *setup = *ppsetup;
1928 if(setup_count < 6) {
1929 return ERROR_DOS(ERRDOS,ERRbadfunc);
1932 fsp = file_fsp((char *)setup,4);
1933 filter = IVAL(setup, 0);
1934 recursive = (SVAL(setup, 6) != 0) ? True : False;
1936 DEBUG(3,("call_nt_transact_notify_change\n"));
1939 return ERROR_DOS(ERRDOS,ERRbadfid);
1943 char *filter_string;
1945 if (!(filter_string = notify_filter_string(NULL, filter))) {
1946 return ERROR_NT(NT_STATUS_NO_MEMORY);
1949 DEBUG(3,("call_nt_transact_notify_change: notify change "
1950 "called on %s, filter = %s, recursive = %d\n",
1951 fsp->fsp_name, filter_string, recursive));
1953 TALLOC_FREE(filter_string);
1956 if((!fsp->is_directory) || (conn != fsp->conn)) {
1957 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1960 if (fsp->notify == NULL) {
1962 status = change_notify_create(fsp, filter, recursive);
1964 if (!NT_STATUS_IS_OK(status)) {
1965 DEBUG(10, ("change_notify_create returned %s\n",
1966 nt_errstr(status)));
1967 return ERROR_NT(status);
1971 if (fsp->notify->num_changes != 0) {
1974 * We've got changes pending, respond immediately
1978 * TODO: write a torture test to check the filtering behaviour
1982 change_notify_reply(inbuf, fsp->notify);
1985 * change_notify_reply() above has independently sent its
1992 * No changes pending, queue the request
1995 status = change_notify_add_request(inbuf, filter, recursive, fsp);
1996 if (!NT_STATUS_IS_OK(status)) {
1997 return ERROR_NT(status);
2003 /****************************************************************************
2004 Reply to an NT transact rename command.
2005 ****************************************************************************/
2007 static int call_nt_transact_rename(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2008 uint16 **ppsetup, uint32 setup_count,
2009 char **ppparams, uint32 parameter_count,
2010 char **ppdata, uint32 data_count, uint32 max_data_count)
2012 char *params = *ppparams;
2014 files_struct *fsp = NULL;
2015 BOOL replace_if_exists = False;
2016 BOOL dest_has_wcard = False;
2019 if(parameter_count < 5) {
2020 return ERROR_DOS(ERRDOS,ERRbadfunc);
2023 fsp = file_fsp(params, 0);
2024 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2025 CHECK_FSP(fsp, conn);
2026 srvstr_get_path_wcard(inbuf, new_name, params+4, sizeof(new_name), parameter_count - 4,
2027 STR_TERMINATE, &status, &dest_has_wcard);
2028 if (!NT_STATUS_IS_OK(status)) {
2029 return ERROR_NT(status);
2032 status = rename_internals(conn, fsp->fsp_name,
2033 new_name, 0, replace_if_exists, False, dest_has_wcard);
2035 if (!NT_STATUS_IS_OK(status)) {
2036 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
2037 /* We have re-scheduled this call. */
2040 return ERROR_NT(status);
2044 * Rename was successful.
2046 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2048 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2049 fsp->fsp_name, new_name));
2054 /******************************************************************************
2055 Fake up a completely empty SD.
2056 *******************************************************************************/
2058 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2062 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2064 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2071 /****************************************************************************
2072 Reply to query a security descriptor.
2073 ****************************************************************************/
2075 static int call_nt_transact_query_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2076 uint16 **ppsetup, uint32 setup_count,
2077 char **ppparams, uint32 parameter_count,
2078 char **ppdata, uint32 data_count, uint32 max_data_count)
2080 char *params = *ppparams;
2081 char *data = *ppdata;
2083 SEC_DESC *psd = NULL;
2085 uint32 security_info_wanted;
2086 TALLOC_CTX *mem_ctx;
2087 files_struct *fsp = NULL;
2089 if(parameter_count < 8) {
2090 return ERROR_DOS(ERRDOS,ERRbadfunc);
2093 fsp = file_fsp(params,0);
2095 return ERROR_DOS(ERRDOS,ERRbadfid);
2098 security_info_wanted = IVAL(params,4);
2100 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2101 (unsigned int)security_info_wanted ));
2103 params = nttrans_realloc(ppparams, 4);
2104 if(params == NULL) {
2105 return ERROR_DOS(ERRDOS,ERRnomem);
2108 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2109 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2110 return ERROR_DOS(ERRDOS,ERRnomem);
2114 * Get the permissions to return.
2117 if (!lp_nt_acl_support(SNUM(conn))) {
2118 sd_size = get_null_nt_acl(mem_ctx, &psd);
2120 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
2124 talloc_destroy(mem_ctx);
2125 return(UNIXERROR(ERRDOS,ERRnoaccess));
2128 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2130 SIVAL(params,0,(uint32)sd_size);
2132 if(max_data_count < sd_size) {
2134 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
2135 params, 4, *ppdata, 0);
2136 talloc_destroy(mem_ctx);
2141 * Allocate the data we will point this at.
2144 data = nttrans_realloc(ppdata, sd_size);
2146 talloc_destroy(mem_ctx);
2147 return ERROR_DOS(ERRDOS,ERRnomem);
2151 * Init the parse struct we will marshall into.
2154 prs_init(&pd, 0, mem_ctx, MARSHALL);
2157 * Setup the prs_struct to point at the memory we just
2161 prs_give_memory( &pd, data, (uint32)sd_size, False);
2164 * Finally, linearize into the outgoing buffer.
2167 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2168 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2169 security descriptor.\n"));
2171 * Return access denied for want of a better error message..
2173 talloc_destroy(mem_ctx);
2174 return(UNIXERROR(ERRDOS,ERRnoaccess));
2178 * Now we can delete the security descriptor.
2181 talloc_destroy(mem_ctx);
2183 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data,
2188 /****************************************************************************
2189 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2190 ****************************************************************************/
2192 static int call_nt_transact_set_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2193 uint16 **ppsetup, uint32 setup_count,
2194 char **ppparams, uint32 parameter_count,
2195 char **ppdata, uint32 data_count, uint32 max_data_count)
2197 char *params= *ppparams;
2198 char *data = *ppdata;
2199 files_struct *fsp = NULL;
2200 uint32 security_info_sent = 0;
2203 if(parameter_count < 8) {
2204 return ERROR_DOS(ERRDOS,ERRbadfunc);
2207 if((fsp = file_fsp(params,0)) == NULL) {
2208 return ERROR_DOS(ERRDOS,ERRbadfid);
2211 if(!lp_nt_acl_support(SNUM(conn))) {
2215 security_info_sent = IVAL(params,4);
2217 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2218 (unsigned int)security_info_sent ));
2220 if (data_count == 0) {
2221 return ERROR_DOS(ERRDOS, ERRnoaccess);
2224 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2225 return ERROR_NT(nt_status);
2230 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2234 /****************************************************************************
2236 ****************************************************************************/
2238 static int call_nt_transact_ioctl(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2239 uint16 **ppsetup, uint32 setup_count,
2240 char **ppparams, uint32 parameter_count,
2241 char **ppdata, uint32 data_count, uint32 max_data_count)
2248 static BOOL logged_message;
2249 char *pdata = *ppdata;
2251 if (setup_count != 8) {
2252 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2253 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2256 function = IVAL(*ppsetup, 0);
2257 fidnum = SVAL(*ppsetup, 4);
2258 isFSctl = CVAL(*ppsetup, 6);
2259 compfilter = CVAL(*ppsetup, 7);
2261 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2262 function, fidnum, isFSctl, compfilter));
2264 fsp=file_fsp((char *)*ppsetup, 4);
2265 /* this check is done in each implemented function case for now
2266 because I don't want to break anything... --metze
2267 FSP_BELONGS_CONN(fsp,conn);*/
2270 case FSCTL_SET_SPARSE:
2271 /* pretend this succeeded - tho strictly we should
2272 mark the file sparse (if the local fs supports it)
2273 so we can know if we need to pre-allocate or not */
2275 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2276 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL,
2280 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2282 unsigned char objid[16];
2284 /* This should return the object-id on this file.
2285 * I think I'll make this be the inode+dev. JRA.
2288 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2291 pdata = nttrans_realloc(ppdata, data_count);
2292 if (pdata == NULL) {
2293 return ERROR_NT(NT_STATUS_NO_MEMORY);
2295 push_file_id_16(pdata, &fsp->file_id);
2296 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2297 push_file_id_16(pdata+32, &fsp->file_id);
2298 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, pdata, data_count);
2302 case FSCTL_GET_REPARSE_POINT:
2303 /* pretend this fail - my winXP does it like this
2307 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2308 return ERROR_NT(NT_STATUS_NOT_A_REPARSE_POINT);
2310 case FSCTL_SET_REPARSE_POINT:
2311 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2315 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2316 return ERROR_NT(NT_STATUS_NOT_A_REPARSE_POINT);
2318 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2321 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2322 * and return their volume names. If max_data_count is 16, then it is just
2323 * asking for the number of volumes and length of the combined names.
2325 * pdata is the data allocated by our caller, but that uses
2326 * total_data_count (which is 0 in our case) rather than max_data_count.
2327 * Allocate the correct amount and return the pointer to let
2328 * it be deallocated when we return.
2330 SHADOW_COPY_DATA *shadow_data = NULL;
2331 TALLOC_CTX *shadow_mem_ctx = NULL;
2332 BOOL labels = False;
2333 uint32 labels_data_count = 0;
2337 FSP_BELONGS_CONN(fsp,conn);
2339 if (max_data_count < 16) {
2340 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2342 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2345 if (max_data_count > 16) {
2349 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2350 if (shadow_mem_ctx == NULL) {
2351 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2352 return ERROR_NT(NT_STATUS_NO_MEMORY);
2355 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2356 if (shadow_data == NULL) {
2357 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2358 talloc_destroy(shadow_mem_ctx);
2359 return ERROR_NT(NT_STATUS_NO_MEMORY);
2362 shadow_data->mem_ctx = shadow_mem_ctx;
2365 * Call the VFS routine to actually do the work.
2367 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2368 talloc_destroy(shadow_data->mem_ctx);
2369 if (errno == ENOSYS) {
2370 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2371 conn->connectpath));
2372 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2374 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2375 conn->connectpath));
2376 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
2380 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2385 data_count = 12+labels_data_count+4;
2388 if (max_data_count<data_count) {
2389 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2390 max_data_count,data_count));
2391 talloc_destroy(shadow_data->mem_ctx);
2392 return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
2395 pdata = nttrans_realloc(ppdata, data_count);
2396 if (pdata == NULL) {
2397 talloc_destroy(shadow_data->mem_ctx);
2398 return ERROR_NT(NT_STATUS_NO_MEMORY);
2403 /* num_volumes 4 bytes */
2404 SIVAL(pdata,0,shadow_data->num_volumes);
2407 /* num_labels 4 bytes */
2408 SIVAL(pdata,4,shadow_data->num_volumes);
2411 /* needed_data_count 4 bytes */
2412 SIVAL(pdata,8,labels_data_count);
2416 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2417 shadow_data->num_volumes,fsp->fsp_name));
2418 if (labels && shadow_data->labels) {
2419 for (i=0;i<shadow_data->num_volumes;i++) {
2420 srvstr_push(outbuf, cur_pdata, shadow_data->labels[i], 2*sizeof(SHADOW_COPY_LABEL), STR_UNICODE|STR_TERMINATE);
2421 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2422 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2426 talloc_destroy(shadow_data->mem_ctx);
2428 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0,
2434 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2436 /* pretend this succeeded -
2438 * we have to send back a list with all files owned by this SID
2440 * but I have to check that --metze
2444 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2446 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2448 FSP_BELONGS_CONN(fsp,conn);
2450 /* unknown 4 bytes: this is not the length of the sid :-( */
2451 /*unknown = IVAL(pdata,0);*/
2453 sid_parse(pdata+4,sid_len,&sid);
2454 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2456 if (!sid_to_uid(&sid, &uid)) {
2457 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2458 sid_string_static(&sid),(unsigned long)sid_len));
2462 /* we can take a look at the find source :-)
2464 * find ./ -uid $uid -name '*' is what we need here
2467 * and send 4bytes len and then NULL terminated unicode strings
2470 * but I don't know how to deal with the paged results
2471 * (maybe we can hang the result anywhere in the fsp struct)
2473 * we don't send all files at once
2474 * and at the next we should *not* start from the beginning,
2475 * so we have to cache the result
2480 /* this works for now... */
2481 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0,
2486 if (!logged_message) {
2487 logged_message = True; /* Only print this once... */
2488 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2493 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2497 #ifdef HAVE_SYS_QUOTAS
2498 /****************************************************************************
2499 Reply to get user quota
2500 ****************************************************************************/
2502 static int call_nt_transact_get_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2503 uint16 **ppsetup, uint32 setup_count,
2504 char **ppparams, uint32 parameter_count,
2505 char **ppdata, uint32 data_count, uint32 max_data_count)
2507 NTSTATUS nt_status = NT_STATUS_OK;
2508 char *params = *ppparams;
2509 char *pdata = *ppdata;
2511 int data_len=0,param_len=0;
2514 files_struct *fsp = NULL;
2518 BOOL start_enum = True;
2519 SMB_NTQUOTA_STRUCT qt;
2520 SMB_NTQUOTA_LIST *tmp_list;
2521 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2526 if (current_user.ut.uid != 0) {
2527 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2528 lp_servicename(SNUM(conn)),conn->user));
2529 return ERROR_DOS(ERRDOS,ERRnoaccess);
2533 * Ensure minimum number of parameters sent.
2536 if (parameter_count < 4) {
2537 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2538 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2541 /* maybe we can check the quota_fnum */
2542 fsp = file_fsp(params,0);
2543 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2544 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2545 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2548 /* the NULL pointer checking for fsp->fake_file_handle->pd
2549 * is done by CHECK_NTQUOTA_HANDLE_OK()
2551 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2553 level = SVAL(params,2);
2555 /* unknown 12 bytes leading in params */
2558 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2559 /* seems that we should continue with the enum here --metze */
2561 if (qt_handle->quota_list!=NULL &&
2562 qt_handle->tmp_list==NULL) {
2565 free_ntquota_list(&(qt_handle->quota_list));
2567 /* Realloc the size of parameters and data we will return */
2569 params = nttrans_realloc(ppparams, param_len);
2570 if(params == NULL) {
2571 return ERROR_DOS(ERRDOS,ERRnomem);
2575 SIVAL(params,0,data_len);
2582 case TRANSACT_GET_USER_QUOTA_LIST_START:
2584 if (qt_handle->quota_list==NULL &&
2585 qt_handle->tmp_list==NULL) {
2589 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
2590 return ERROR_DOS(ERRSRV,ERRerror);
2592 /* Realloc the size of parameters and data we will return */
2594 params = nttrans_realloc(ppparams, param_len);
2595 if(params == NULL) {
2596 return ERROR_DOS(ERRDOS,ERRnomem);
2599 /* we should not trust the value in max_data_count*/
2600 max_data_count = MIN(max_data_count,2048);
2602 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2604 return ERROR_DOS(ERRDOS,ERRnomem);
2609 /* set params Size of returned Quota Data 4 bytes*/
2610 /* but set it later when we know it */
2612 /* for each entry push the data */
2615 qt_handle->tmp_list = qt_handle->quota_list;
2618 tmp_list = qt_handle->tmp_list;
2620 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2621 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2623 sid_len = sid_size(&tmp_list->quotas->sid);
2624 entry_len = 40 + sid_len;
2626 /* nextoffset entry 4 bytes */
2627 SIVAL(entry,0,entry_len);
2629 /* then the len of the SID 4 bytes */
2630 SIVAL(entry,4,sid_len);
2632 /* unknown data 8 bytes SMB_BIG_UINT */
2633 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2635 /* the used disk space 8 bytes SMB_BIG_UINT */
2636 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2638 /* the soft quotas 8 bytes SMB_BIG_UINT */
2639 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2641 /* the hard quotas 8 bytes SMB_BIG_UINT */
2642 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2644 /* and now the SID */
2645 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2648 qt_handle->tmp_list = tmp_list;
2650 /* overwrite the offset of the last entry */
2651 SIVAL(entry-entry_len,0,0);
2653 data_len = 4+qt_len;
2654 /* overwrite the params quota_data_len */
2655 SIVAL(params,0,data_len);
2659 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2661 /* unknown 4 bytes IVAL(pdata,0) */
2663 if (data_count < 8) {
2664 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2665 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2668 sid_len = IVAL(pdata,4);
2669 /* Ensure this is less than 1mb. */
2670 if (sid_len > (1024*1024)) {
2671 return ERROR_DOS(ERRDOS,ERRnomem);
2674 if (data_count < 8+sid_len) {
2675 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2676 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2679 data_len = 4+40+sid_len;
2681 if (max_data_count < data_len) {
2682 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2683 max_data_count, data_len));
2685 SIVAL(params,0,data_len);
2687 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2691 sid_parse(pdata+8,sid_len,&sid);
2693 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2696 * we have to return zero's in all fields
2697 * instead of returning an error here
2702 /* Realloc the size of parameters and data we will return */
2704 params = nttrans_realloc(ppparams, param_len);
2705 if(params == NULL) {
2706 return ERROR_DOS(ERRDOS,ERRnomem);
2709 pdata = nttrans_realloc(ppdata, data_len);
2711 return ERROR_DOS(ERRDOS,ERRnomem);
2716 /* set params Size of returned Quota Data 4 bytes*/
2717 SIVAL(params,0,data_len);
2719 /* nextoffset entry 4 bytes */
2722 /* then the len of the SID 4 bytes */
2723 SIVAL(entry,4,sid_len);
2725 /* unknown data 8 bytes SMB_BIG_UINT */
2726 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2728 /* the used disk space 8 bytes SMB_BIG_UINT */
2729 SBIG_UINT(entry,16,qt.usedspace);
2731 /* the soft quotas 8 bytes SMB_BIG_UINT */
2732 SBIG_UINT(entry,24,qt.softlim);
2734 /* the hard quotas 8 bytes SMB_BIG_UINT */
2735 SBIG_UINT(entry,32,qt.hardlim);
2737 /* and now the SID */
2738 sid_linearize(entry+40, sid_len, &sid);
2743 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2744 return ERROR_DOS(ERRSRV,ERRerror);
2748 send_nt_replies(inbuf, outbuf, bufsize, nt_status, params, param_len,
2754 /****************************************************************************
2755 Reply to set user quota
2756 ****************************************************************************/
2758 static int call_nt_transact_set_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2759 uint16 **ppsetup, uint32 setup_count,
2760 char **ppparams, uint32 parameter_count,
2761 char **ppdata, uint32 data_count, uint32 max_data_count)
2763 char *params = *ppparams;
2764 char *pdata = *ppdata;
2765 int data_len=0,param_len=0;
2766 SMB_NTQUOTA_STRUCT qt;
2769 files_struct *fsp = NULL;
2774 if (current_user.ut.uid != 0) {
2775 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2776 lp_servicename(SNUM(conn)),conn->user));
2777 return ERROR_DOS(ERRDOS,ERRnoaccess);
2781 * Ensure minimum number of parameters sent.
2784 if (parameter_count < 2) {
2785 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2786 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2789 /* maybe we can check the quota_fnum */
2790 fsp = file_fsp(params,0);
2791 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2792 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2793 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2796 if (data_count < 40) {
2797 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2798 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2801 /* offset to next quota record.
2802 * 4 bytes IVAL(pdata,0)
2807 sid_len = IVAL(pdata,4);
2809 if (data_count < 40+sid_len) {
2810 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2811 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2814 /* unknown 8 bytes in pdata
2815 * maybe its the change time in NTTIME
2818 /* the used space 8 bytes (SMB_BIG_UINT)*/
2819 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2820 #ifdef LARGE_SMB_OFF_T
2821 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2822 #else /* LARGE_SMB_OFF_T */
2823 if ((IVAL(pdata,20) != 0)&&
2824 ((qt.usedspace != 0xFFFFFFFF)||
2825 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2826 /* more than 32 bits? */
2827 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2829 #endif /* LARGE_SMB_OFF_T */
2831 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2832 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2833 #ifdef LARGE_SMB_OFF_T
2834 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2835 #else /* LARGE_SMB_OFF_T */
2836 if ((IVAL(pdata,28) != 0)&&
2837 ((qt.softlim != 0xFFFFFFFF)||
2838 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2839 /* more than 32 bits? */
2840 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2842 #endif /* LARGE_SMB_OFF_T */
2844 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2845 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2846 #ifdef LARGE_SMB_OFF_T
2847 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2848 #else /* LARGE_SMB_OFF_T */
2849 if ((IVAL(pdata,36) != 0)&&
2850 ((qt.hardlim != 0xFFFFFFFF)||
2851 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2852 /* more than 32 bits? */
2853 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2855 #endif /* LARGE_SMB_OFF_T */
2857 sid_parse(pdata+40,sid_len,&sid);
2858 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2860 /* 44 unknown bytes left... */
2862 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2863 return ERROR_DOS(ERRSRV,ERRerror);
2866 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len,
2871 #endif /* HAVE_SYS_QUOTAS */
2873 static int handle_nttrans(connection_struct *conn,
2874 struct trans_state *state,
2875 char *inbuf, char *outbuf, int size, int bufsize)
2879 if (Protocol >= PROTOCOL_NT1) {
2880 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | 0x40); /* IS_LONG_NAME */
2883 /* Now we must call the relevant NT_TRANS function */
2884 switch(state->call) {
2885 case NT_TRANSACT_CREATE:
2887 START_PROFILE(NT_transact_create);
2888 outsize = call_nt_transact_create(conn, inbuf, outbuf,
2890 &state->setup, state->setup_count,
2891 &state->param, state->total_param,
2892 &state->data, state->total_data,
2893 state->max_data_return);
2894 END_PROFILE(NT_transact_create);
2898 case NT_TRANSACT_IOCTL:
2900 START_PROFILE(NT_transact_ioctl);
2901 outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
2903 &state->setup, state->setup_count,
2904 &state->param, state->total_param,
2905 &state->data, state->total_data, state->max_data_return);
2906 END_PROFILE(NT_transact_ioctl);
2910 case NT_TRANSACT_SET_SECURITY_DESC:
2912 START_PROFILE(NT_transact_set_security_desc);
2913 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
2915 &state->setup, state->setup_count,
2916 &state->param, state->total_param,
2917 &state->data, state->total_data, state->max_data_return);
2918 END_PROFILE(NT_transact_set_security_desc);
2922 case NT_TRANSACT_NOTIFY_CHANGE:
2924 START_PROFILE(NT_transact_notify_change);
2925 outsize = call_nt_transact_notify_change(
2926 conn, inbuf, outbuf, size, bufsize,
2927 &state->setup, state->setup_count,
2928 &state->param, state->total_param,
2929 &state->data, state->total_data,
2930 state->max_data_return,
2931 state->max_param_return);
2932 END_PROFILE(NT_transact_notify_change);
2936 case NT_TRANSACT_RENAME:
2938 START_PROFILE(NT_transact_rename);
2939 outsize = call_nt_transact_rename(conn, inbuf, outbuf,
2941 &state->setup, state->setup_count,
2942 &state->param, state->total_param,
2943 &state->data, state->total_data, state->max_data_return);
2944 END_PROFILE(NT_transact_rename);
2948 case NT_TRANSACT_QUERY_SECURITY_DESC:
2950 START_PROFILE(NT_transact_query_security_desc);
2951 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
2953 &state->setup, state->setup_count,
2954 &state->param, state->total_param,
2955 &state->data, state->total_data, state->max_data_return);
2956 END_PROFILE(NT_transact_query_security_desc);
2960 #ifdef HAVE_SYS_QUOTAS
2961 case NT_TRANSACT_GET_USER_QUOTA:
2963 START_PROFILE(NT_transact_get_user_quota);
2964 outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf,
2966 &state->setup, state->setup_count,
2967 &state->param, state->total_param,
2968 &state->data, state->total_data, state->max_data_return);
2969 END_PROFILE(NT_transact_get_user_quota);
2973 case NT_TRANSACT_SET_USER_QUOTA:
2975 START_PROFILE(NT_transact_set_user_quota);
2976 outsize = call_nt_transact_set_user_quota(conn, inbuf, outbuf,
2978 &state->setup, state->setup_count,
2979 &state->param, state->total_param,
2980 &state->data, state->total_data, state->max_data_return);
2981 END_PROFILE(NT_transact_set_user_quota);
2984 #endif /* HAVE_SYS_QUOTAS */
2987 /* Error in request */
2988 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n",
2990 return ERROR_DOS(ERRSRV,ERRerror);
2995 /****************************************************************************
2996 Reply to a SMBNTtrans.
2997 ****************************************************************************/
2999 int reply_nttrans(connection_struct *conn,
3000 char *inbuf,char *outbuf,int size,int bufsize)
3003 uint32 pscnt = IVAL(inbuf,smb_nt_ParameterCount);
3004 uint32 psoff = IVAL(inbuf,smb_nt_ParameterOffset);
3005 uint32 dscnt = IVAL(inbuf,smb_nt_DataCount);
3006 uint32 dsoff = IVAL(inbuf,smb_nt_DataOffset);
3008 uint16 function_code = SVAL( inbuf, smb_nt_Function);
3010 struct trans_state *state;
3012 START_PROFILE(SMBnttrans);
3014 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3015 END_PROFILE(SMBnttrans);
3016 return ERROR_DOS(ERRSRV,ERRaccess);
3019 result = allow_new_trans(conn->pending_trans, SVAL(inbuf, smb_mid));
3020 if (!NT_STATUS_IS_OK(result)) {
3021 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3022 END_PROFILE(SMBnttrans);
3023 return ERROR_NT(result);
3026 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3027 END_PROFILE(SMBnttrans);
3028 return ERROR_DOS(ERRSRV,ERRaccess);
3031 state->cmd = SMBnttrans;
3033 state->mid = SVAL(inbuf,smb_mid);
3034 state->vuid = SVAL(inbuf,smb_uid);
3035 state->total_data = IVAL(inbuf, smb_nt_TotalDataCount);
3037 state->total_param = IVAL(inbuf, smb_nt_TotalParameterCount);
3038 state->param = NULL;
3039 state->max_data_return = IVAL(inbuf,smb_nt_MaxDataCount);
3040 state->max_param_return = IVAL(inbuf,smb_nt_MaxParameterCount);
3042 /* setup count is in *words* */
3043 state->setup_count = 2*CVAL(inbuf,smb_nt_SetupCount);
3044 state->setup = NULL;
3045 state->call = function_code;
3048 * All nttrans messages we handle have smb_wct == 19 +
3049 * state->setup_count. Ensure this is so as a sanity check.
3052 if(CVAL(inbuf, smb_wct) != 19 + (state->setup_count/2)) {
3053 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3054 CVAL(inbuf, smb_wct), 19 + (state->setup_count/2)));
3058 /* Don't allow more than 128mb for each value. */
3059 if ((state->total_data > (1024*1024*128)) ||
3060 (state->total_param > (1024*1024*128))) {
3061 END_PROFILE(SMBnttrans);
3062 return ERROR_DOS(ERRDOS,ERRnomem);
3065 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3068 if (state->total_data) {
3069 /* Can't use talloc here, the core routines do realloc on the
3070 * params and data. */
3071 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3072 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3073 "bytes !\n", (unsigned int)state->total_data));
3075 END_PROFILE(SMBnttrans);
3076 return(ERROR_DOS(ERRDOS,ERRnomem));
3078 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3080 if ((smb_base(inbuf)+dsoff+dscnt > inbuf + size) ||
3081 (smb_base(inbuf)+dsoff+dscnt < smb_base(inbuf)))
3084 memcpy(state->data,smb_base(inbuf)+dsoff,dscnt);
3087 if (state->total_param) {
3088 /* Can't use talloc here, the core routines do realloc on the
3089 * params and data. */
3090 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3091 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3092 "bytes !\n", (unsigned int)state->total_param));
3093 SAFE_FREE(state->data);
3095 END_PROFILE(SMBnttrans);
3096 return(ERROR_DOS(ERRDOS,ERRnomem));
3098 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3100 if ((smb_base(inbuf)+psoff+pscnt > inbuf + size) ||
3101 (smb_base(inbuf)+psoff+pscnt < smb_base(inbuf)))
3104 memcpy(state->param,smb_base(inbuf)+psoff,pscnt);
3107 state->received_data = dscnt;
3108 state->received_param = pscnt;
3110 if(state->setup_count > 0) {
3111 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3112 state->setup_count));
3113 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3114 if (state->setup == NULL) {
3115 DEBUG(0,("reply_nttrans : Out of memory\n"));
3116 SAFE_FREE(state->data);
3117 SAFE_FREE(state->param);
3119 END_PROFILE(SMBnttrans);
3120 return ERROR_DOS(ERRDOS,ERRnomem);
3123 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3124 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3127 if (smb_nt_SetupStart + state->setup_count > size) {
3131 memcpy( state->setup, &inbuf[smb_nt_SetupStart], state->setup_count);
3132 dump_data(10, (uint8 *)state->setup, state->setup_count);
3135 if ((state->received_data == state->total_data) &&
3136 (state->received_param == state->total_param)) {
3137 outsize = handle_nttrans(conn, state, inbuf, outbuf,
3139 SAFE_FREE(state->param);
3140 SAFE_FREE(state->data);
3142 END_PROFILE(SMBnttrans);
3146 DLIST_ADD(conn->pending_trans, state);
3148 /* We need to send an interim response then receive the rest
3149 of the parameter/data bytes */
3150 outsize = set_message(inbuf,outbuf,0,0,False);
3152 END_PROFILE(SMBnttrans);
3157 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3158 SAFE_FREE(state->data);
3159 SAFE_FREE(state->param);
3161 END_PROFILE(SMBnttrans);
3162 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
3165 /****************************************************************************
3166 Reply to a SMBnttranss
3167 ****************************************************************************/
3169 int reply_nttranss(connection_struct *conn, char *inbuf,char *outbuf,
3170 int size,int bufsize)
3173 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3174 struct trans_state *state;
3176 START_PROFILE(SMBnttranss);
3180 for (state = conn->pending_trans; state != NULL;
3181 state = state->next) {
3182 if (state->mid == SVAL(inbuf,smb_mid)) {
3187 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3188 END_PROFILE(SMBnttranss);
3189 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
3192 /* Revise state->total_param and state->total_data in case they have
3193 changed downwards */
3194 if (IVAL(inbuf, smb_nts_TotalParameterCount) < state->total_param) {
3195 state->total_param = IVAL(inbuf, smb_nts_TotalParameterCount);
3197 if (IVAL(inbuf, smb_nts_TotalDataCount) < state->total_data) {
3198 state->total_data = IVAL(inbuf, smb_nts_TotalDataCount);
3201 pcnt = IVAL(inbuf,smb_nts_ParameterCount);
3202 poff = IVAL(inbuf, smb_nts_ParameterOffset);
3203 pdisp = IVAL(inbuf, smb_nts_ParameterDisplacement);
3205 dcnt = IVAL(inbuf, smb_nts_DataCount);
3206 ddisp = IVAL(inbuf, smb_nts_DataDisplacement);
3207 doff = IVAL(inbuf, smb_nts_DataOffset);
3209 state->received_param += pcnt;
3210 state->received_data += dcnt;
3212 if ((state->received_data > state->total_data) ||
3213 (state->received_param > state->total_param))
3217 if (pdisp+pcnt > state->total_param)
3219 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3221 if (pdisp > state->total_param)
3223 if ((smb_base(inbuf) + poff + pcnt > inbuf + size) ||
3224 (smb_base(inbuf) + poff + pcnt < smb_base(inbuf)))
3226 if (state->param + pdisp < state->param)
3229 memcpy(state->param+pdisp,smb_base(inbuf)+poff,
3234 if (ddisp+dcnt > state->total_data)
3236 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3238 if (ddisp > state->total_data)
3240 if ((smb_base(inbuf) + doff + dcnt > inbuf + size) ||
3241 (smb_base(inbuf) + doff + dcnt < smb_base(inbuf)))
3243 if (state->data + ddisp < state->data)
3246 memcpy(state->data+ddisp, smb_base(inbuf)+doff,
3250 if ((state->received_param < state->total_param) ||
3251 (state->received_data < state->total_data)) {
3252 END_PROFILE(SMBnttranss);
3256 /* construct_reply_common has done us the favor to pre-fill the
3257 * command field with SMBnttranss which is wrong :-)
3259 SCVAL(outbuf,smb_com,SMBnttrans);
3261 outsize = handle_nttrans(conn, state, inbuf, outbuf,
3264 DLIST_REMOVE(conn->pending_trans, state);
3265 SAFE_FREE(state->data);
3266 SAFE_FREE(state->param);
3270 END_PROFILE(SMBnttranss);
3271 return(ERROR_DOS(ERRSRV,ERRnosupport));
3274 END_PROFILE(SMBnttranss);
3279 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3280 DLIST_REMOVE(conn->pending_trans, state);
3281 SAFE_FREE(state->data);
3282 SAFE_FREE(state->param);
3284 END_PROFILE(SMBnttranss);
3285 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);