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 ret
= SMB_VFS_FSET_NT_ACL( fsp
, fsp
->fh
->fd
, security_info_sent
, psd
);
1118 talloc_destroy(mem_ctx
);
1119 return NT_STATUS_ACCESS_DENIED
;
1122 talloc_destroy(mem_ctx
);
1124 return NT_STATUS_OK
;
1127 /****************************************************************************
1128 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1129 ****************************************************************************/
1131 static struct ea_list
*read_nttrans_ea_list(TALLOC_CTX
*ctx
, const char *pdata
, size_t data_size
)
1133 struct ea_list
*ea_list_head
= NULL
;
1136 if (data_size
< 4) {
1140 while (offset
+ 4 <= data_size
) {
1141 size_t next_offset
= IVAL(pdata
,offset
);
1142 struct ea_list
*eal
= read_ea_list_entry(ctx
, pdata
+ offset
+ 4, data_size
- offset
- 4, NULL
);
1148 DLIST_ADD_END(ea_list_head
, eal
, struct ea_list
*);
1149 if (next_offset
== 0) {
1152 offset
+= next_offset
;
1155 return ea_list_head
;
1158 /****************************************************************************
1159 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1160 ****************************************************************************/
1162 static int call_nt_transact_create(connection_struct
*conn
, char *inbuf
, char *outbuf
, int length
, int bufsize
,
1163 uint16
**ppsetup
, uint32 setup_count
,
1164 char **ppparams
, uint32 parameter_count
,
1165 char **ppdata
, uint32 data_count
, uint32 max_data_count
)
1168 char *params
= *ppparams
;
1169 char *data
= *ppdata
;
1170 /* Breakout the oplock request bits so we can set the reply bits separately. */
1171 int oplock_request
= 0;
1173 SMB_OFF_T file_len
= 0;
1174 SMB_STRUCT_STAT sbuf
;
1176 files_struct
*fsp
= NULL
;
1178 BOOL extended_oplock_granted
= False
;
1181 uint32 file_attributes
;
1182 uint32 new_file_attributes
;
1183 uint32 share_access
;
1184 uint32 create_disposition
;
1185 uint32 create_options
;
1188 uint16 root_dir_fid
;
1189 struct timespec c_timespec
;
1190 struct timespec a_timespec
;
1191 struct timespec m_timespec
;
1192 struct ea_list
*ea_list
= NULL
;
1193 TALLOC_CTX
*ctx
= NULL
;
1198 DEBUG(5,("call_nt_transact_create\n"));
1201 * If it's an IPC, use the pipe handler.
1205 if (lp_nt_pipe_support()) {
1206 return do_nt_transact_create_pipe(conn
, inbuf
, outbuf
, length
,
1208 ppsetup
, setup_count
,
1209 ppparams
, parameter_count
,
1210 ppdata
, data_count
);
1212 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
1217 * Ensure minimum number of parameters sent.
1220 if(parameter_count
< 54) {
1221 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count
));
1222 return ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
1225 flags
= IVAL(params
,0);
1226 access_mask
= IVAL(params
,8);
1227 file_attributes
= IVAL(params
,20);
1228 share_access
= IVAL(params
,24);
1229 create_disposition
= IVAL(params
,28);
1230 create_options
= IVAL(params
,32);
1231 sd_len
= IVAL(params
,36);
1232 ea_len
= IVAL(params
,40);
1233 root_dir_fid
= (uint16
)IVAL(params
,4);
1235 /* Ensure the data_len is correct for the sd and ea values given. */
1236 if ((ea_len
+ sd_len
> data_count
) ||
1237 (ea_len
> data_count
) || (sd_len
> data_count
) ||
1238 (ea_len
+ sd_len
< ea_len
) || (ea_len
+ sd_len
< sd_len
)) {
1239 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1240 (unsigned int)ea_len
, (unsigned int)sd_len
, (unsigned int)data_count
));
1241 return ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
1245 if (!lp_ea_support(SNUM(conn
))) {
1246 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1247 (unsigned int)ea_len
));
1248 return ERROR_NT(NT_STATUS_EAS_NOT_SUPPORTED
);
1252 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1253 (unsigned int)ea_len
));
1254 return ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
1258 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
1259 return ERROR_NT(NT_STATUS_NOT_SUPPORTED
);
1263 * Get the file name.
1266 if(root_dir_fid
!= 0) {
1268 * This filename is relative to a directory fid.
1270 files_struct
*dir_fsp
= file_fsp(params
,4);
1271 size_t dir_name_len
;
1274 return ERROR_DOS(ERRDOS
,ERRbadfid
);
1277 if(!dir_fsp
->is_directory
) {
1278 srvstr_get_path(inbuf
, fname
, params
+53, sizeof(fname
), parameter_count
-53, STR_TERMINATE
, &status
);
1279 if (!NT_STATUS_IS_OK(status
)) {
1280 return ERROR_NT(status
);
1284 * Check to see if this is a mac fork of some kind.
1287 if( is_ntfs_stream_name(fname
)) {
1288 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND
);
1291 return ERROR_DOS(ERRDOS
,ERRbadfid
);
1295 * Copy in the base directory name.
1298 pstrcpy( fname
, dir_fsp
->fsp_name
);
1299 dir_name_len
= strlen(fname
);
1302 * Ensure it ends in a '\'.
1305 if((fname
[dir_name_len
-1] != '\\') && (fname
[dir_name_len
-1] != '/')) {
1306 pstrcat(fname
, "/");
1312 srvstr_get_path(inbuf
, tmpname
, params
+53, sizeof(tmpname
), parameter_count
-53, STR_TERMINATE
, &status
);
1313 if (!NT_STATUS_IS_OK(status
)) {
1314 return ERROR_NT(status
);
1316 pstrcat(fname
, tmpname
);
1319 srvstr_get_path(inbuf
, fname
, params
+53, sizeof(fname
), parameter_count
-53, STR_TERMINATE
, &status
);
1320 if (!NT_STATUS_IS_OK(status
)) {
1321 return ERROR_NT(status
);
1325 * Check to see if this is a mac fork of some kind.
1328 if( is_ntfs_stream_name(fname
)) {
1329 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND
);
1333 oplock_request
= (flags
& REQUEST_OPLOCK
) ? EXCLUSIVE_OPLOCK
: 0;
1334 if (oplock_request
) {
1335 oplock_request
|= (flags
& REQUEST_BATCH_OPLOCK
) ? BATCH_OPLOCK
: 0;
1339 * Ordinary file or directory.
1343 * Check if POSIX semantics are wanted.
1346 new_file_attributes
= set_posix_case_semantics(conn
, file_attributes
);
1348 status
= resolve_dfspath(conn
, SVAL(inbuf
,smb_flg2
) & FLAGS2_DFS_PATHNAMES
, fname
);
1349 if (!NT_STATUS_IS_OK(status
)) {
1350 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
1351 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED
, ERRSRV
, ERRbadpath
);
1353 return ERROR_NT(status
);
1356 status
= unix_convert(conn
, fname
, False
, NULL
, &sbuf
);
1357 if (!NT_STATUS_IS_OK(status
)) {
1358 restore_case_semantics(conn
, file_attributes
);
1359 return ERROR_NT(status
);
1361 /* All file access must go through check_name() */
1362 status
= check_name(conn
, fname
);
1363 if (!NT_STATUS_IS_OK(status
)) {
1364 restore_case_semantics(conn
, file_attributes
);
1365 return ERROR_NT(status
);
1368 /* This is the correct thing to do (check every time) but can_delete is
1369 expensive (it may have to read the parent directory permissions). So
1370 for now we're not doing it unless we have a strong hint the client
1371 is really going to delete this file. If the client is forcing FILE_CREATE
1372 let the filesystem take care of the permissions. */
1374 /* Setting FILE_SHARE_DELETE is the hint. */
1376 if (lp_acl_check_permissions(SNUM(conn
))
1377 && (create_disposition
!= FILE_CREATE
)
1378 && (share_access
& FILE_SHARE_DELETE
)
1379 && (access_mask
& DELETE_ACCESS
)) {
1380 if ((dos_mode(conn
, fname
, &sbuf
) & FILE_ATTRIBUTE_READONLY
) ||
1381 !can_delete_file_in_directory(conn
, fname
)) {
1382 restore_case_semantics(conn
, file_attributes
);
1383 return ERROR_NT(NT_STATUS_ACCESS_DENIED
);
1388 pdata
= data
+ sd_len
;
1390 /* We have already checked that ea_len <= data_count here. */
1391 ea_list
= read_nttrans_ea_list(tmp_talloc_ctx(), pdata
,
1394 restore_case_semantics(conn
, file_attributes
);
1395 return ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
1400 * If it's a request for a directory open, deal with it separately.
1403 if(create_options
& FILE_DIRECTORY_FILE
) {
1405 /* Can't open a temp directory. IFS kit test. */
1406 if (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
) {
1407 restore_case_semantics(conn
, file_attributes
);
1408 return ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
1412 * We will get a create directory here if the Win32
1413 * app specified a security descriptor in the
1414 * CreateDirectory() call.
1418 status
= open_directory(conn
, fname
, &sbuf
,
1423 new_file_attributes
,
1425 if(!NT_STATUS_IS_OK(status
)) {
1426 restore_case_semantics(conn
, file_attributes
);
1427 return ERROR_NT(status
);
1433 * Ordinary file case.
1436 status
= open_file_ntcreate(conn
,fname
,&sbuf
,
1441 new_file_attributes
,
1445 if (!NT_STATUS_IS_OK(status
)) {
1446 if (NT_STATUS_EQUAL(status
,
1447 NT_STATUS_FILE_IS_A_DIRECTORY
)) {
1450 * Fail the open if it was explicitly a non-directory file.
1453 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
1454 restore_case_semantics(conn
, file_attributes
);
1455 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY
);
1459 status
= open_directory(conn
, fname
, &sbuf
,
1464 new_file_attributes
,
1466 if(!NT_STATUS_IS_OK(status
)) {
1467 restore_case_semantics(conn
, file_attributes
);
1468 return ERROR_NT(status
);
1471 restore_case_semantics(conn
, file_attributes
);
1472 if (open_was_deferred(SVAL(inbuf
,smb_mid
))) {
1473 /* We have re-scheduled this call. */
1476 return ERROR_NT(status
);
1482 * According to the MS documentation, the only time the security
1483 * descriptor is applied to the opened file is iff we *created* the
1484 * file; an existing file stays the same.
1486 * Also, it seems (from observation) that you can open the file with
1487 * any access mask but you can still write the sd. We need to override
1488 * the granted access before we call set_sd
1489 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1492 if (lp_nt_acl_support(SNUM(conn
)) && sd_len
&& info
== FILE_WAS_CREATED
) {
1493 uint32 saved_access_mask
= fsp
->access_mask
;
1495 /* We have already checked that sd_len <= data_count here. */
1497 fsp
->access_mask
= FILE_GENERIC_ALL
;
1499 status
= set_sd( fsp
, data
, sd_len
, ALL_SECURITY_INFORMATION
);
1500 if (!NT_STATUS_IS_OK(status
)) {
1501 talloc_destroy(ctx
);
1502 close_file(fsp
,ERROR_CLOSE
);
1503 restore_case_semantics(conn
, file_attributes
);
1504 return ERROR_NT(status
);
1506 fsp
->access_mask
= saved_access_mask
;
1509 if (ea_len
&& (info
== FILE_WAS_CREATED
)) {
1510 status
= set_ea(conn
, fsp
, fname
, ea_list
);
1511 if (!NT_STATUS_IS_OK(status
)) {
1512 close_file(fsp
,ERROR_CLOSE
);
1513 restore_case_semantics(conn
, file_attributes
);
1514 return ERROR_NT(status
);
1518 restore_case_semantics(conn
, file_attributes
);
1520 file_len
= sbuf
.st_size
;
1521 fattr
= dos_mode(conn
,fname
,&sbuf
);
1523 fattr
= FILE_ATTRIBUTE_NORMAL
;
1525 if (!fsp
->is_directory
&& (fattr
& aDIR
)) {
1526 close_file(fsp
,ERROR_CLOSE
);
1527 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
1530 /* Save the requested allocation size. */
1531 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
1532 SMB_BIG_UINT allocation_size
= (SMB_BIG_UINT
)IVAL(params
,12);
1533 #ifdef LARGE_SMB_OFF_T
1534 allocation_size
|= (((SMB_BIG_UINT
)IVAL(params
,16)) << 32);
1536 if (allocation_size
&& (allocation_size
> file_len
)) {
1537 fsp
->initial_allocation_size
= smb_roundup(fsp
->conn
, allocation_size
);
1538 if (fsp
->is_directory
) {
1539 close_file(fsp
,ERROR_CLOSE
);
1540 /* Can't set allocation size on a directory. */
1541 return ERROR_NT(NT_STATUS_ACCESS_DENIED
);
1543 if (vfs_allocate_file_space(fsp
, fsp
->initial_allocation_size
) == -1) {
1544 close_file(fsp
,ERROR_CLOSE
);
1545 return ERROR_NT(NT_STATUS_DISK_FULL
);
1548 fsp
->initial_allocation_size
= smb_roundup(fsp
->conn
, (SMB_BIG_UINT
)file_len
);
1553 * If the caller set the extended oplock request bit
1554 * and we granted one (by whatever means) - set the
1555 * correct bit for extended oplock reply.
1558 if (oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
1559 extended_oplock_granted
= True
;
1562 if(oplock_request
&& EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1563 extended_oplock_granted
= True
;
1566 /* Realloc the size of parameters and data we will return */
1567 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1568 /* Extended response is 32 more byyes. */
1573 params
= nttrans_realloc(ppparams
, param_len
);
1574 if(params
== NULL
) {
1575 return ERROR_DOS(ERRDOS
,ERRnomem
);
1579 if (extended_oplock_granted
) {
1580 if (flags
& REQUEST_BATCH_OPLOCK
) {
1581 SCVAL(p
,0, BATCH_OPLOCK_RETURN
);
1583 SCVAL(p
,0, EXCLUSIVE_OPLOCK_RETURN
);
1585 } else if (fsp
->oplock_type
== LEVEL_II_OPLOCK
) {
1586 SCVAL(p
,0, LEVEL_II_OPLOCK_RETURN
);
1588 SCVAL(p
,0,NO_OPLOCK_RETURN
);
1592 SSVAL(p
,0,fsp
->fnum
);
1594 if ((create_disposition
== FILE_SUPERSEDE
) && (info
== FILE_WAS_OVERWRITTEN
)) {
1595 SIVAL(p
,0,FILE_WAS_SUPERSEDED
);
1602 c_timespec
= get_create_timespec(&sbuf
,lp_fake_dir_create_times(SNUM(conn
)));
1603 a_timespec
= get_atimespec(&sbuf
);
1604 m_timespec
= get_mtimespec(&sbuf
);
1606 if (lp_dos_filetime_resolution(SNUM(conn
))) {
1607 dos_filetime_timespec(&c_timespec
);
1608 dos_filetime_timespec(&a_timespec
);
1609 dos_filetime_timespec(&m_timespec
);
1612 put_long_date_timespec(p
, c_timespec
); /* create time. */
1614 put_long_date_timespec(p
, a_timespec
); /* access time */
1616 put_long_date_timespec(p
, m_timespec
); /* write time */
1618 put_long_date_timespec(p
, m_timespec
); /* change time */
1620 SIVAL(p
,0,fattr
); /* File Attributes. */
1622 SOFF_T(p
, 0, get_allocation_size(conn
,fsp
,&sbuf
));
1624 SOFF_T(p
,0,file_len
);
1626 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1630 SCVAL(p
,0,fsp
->is_directory
? 1 : 0);
1632 if (flags
& EXTENDED_RESPONSE_REQUIRED
) {
1635 if (fsp
->is_directory
|| can_write_to_file(conn
, fname
, &sbuf
)) {
1636 perms
= FILE_GENERIC_ALL
;
1638 perms
= FILE_GENERIC_READ
|FILE_EXECUTE
;
1643 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname
));
1645 /* Send the required number of replies */
1646 send_nt_replies(inbuf
, outbuf
, bufsize
, NT_STATUS_OK
, params
, param_len
, *ppdata
, 0);
1651 /****************************************************************************
1652 Reply to a NT CANCEL request.
1653 conn POINTER CAN BE NULL HERE !
1654 ****************************************************************************/
1656 int reply_ntcancel(connection_struct
*conn
,
1657 char *inbuf
,char *outbuf
,int length
,int bufsize
)
1660 * Go through and cancel any pending change notifies.
1663 int mid
= SVAL(inbuf
,smb_mid
);
1664 START_PROFILE(SMBntcancel
);
1665 remove_pending_change_notify_requests_by_mid(mid
);
1666 remove_pending_lock_requests_by_mid(mid
);
1667 srv_cancel_sign_response(mid
);
1669 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid
));
1671 END_PROFILE(SMBntcancel
);
1675 /****************************************************************************
1677 ****************************************************************************/
1679 static NTSTATUS
copy_internals(connection_struct
*conn
, char *oldname
, char *newname
, uint32 attrs
)
1681 SMB_STRUCT_STAT sbuf1
, sbuf2
;
1682 pstring last_component_oldname
;
1683 pstring last_component_newname
;
1684 files_struct
*fsp1
,*fsp2
;
1688 NTSTATUS status
= NT_STATUS_OK
;
1693 if (!CAN_WRITE(conn
)) {
1694 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1697 status
= unix_convert(conn
, oldname
, False
, last_component_oldname
, &sbuf1
);
1698 if (!NT_STATUS_IS_OK(status
)) {
1702 status
= check_name(conn
, oldname
);
1703 if (!NT_STATUS_IS_OK(status
)) {
1707 /* Source must already exist. */
1708 if (!VALID_STAT(sbuf1
)) {
1709 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1711 /* Ensure attributes match. */
1712 fattr
= dos_mode(conn
,oldname
,&sbuf1
);
1713 if ((fattr
& ~attrs
) & (aHIDDEN
| aSYSTEM
)) {
1714 return NT_STATUS_NO_SUCH_FILE
;
1717 status
= unix_convert(conn
, newname
, False
, last_component_newname
, &sbuf2
);
1718 if (!NT_STATUS_IS_OK(status
)) {
1722 status
= check_name(conn
, newname
);
1723 if (!NT_STATUS_IS_OK(status
)) {
1727 /* Disallow if newname already exists. */
1728 if (VALID_STAT(sbuf2
)) {
1729 return NT_STATUS_OBJECT_NAME_COLLISION
;
1732 /* No links from a directory. */
1733 if (S_ISDIR(sbuf1
.st_mode
)) {
1734 return NT_STATUS_FILE_IS_A_DIRECTORY
;
1737 /* Ensure this is within the share. */
1738 status
= reduce_name(conn
, oldname
);
1739 if (!NT_STATUS_IS_OK(status
)) {
1743 DEBUG(10,("copy_internals: doing file copy %s to %s\n", oldname
, newname
));
1745 status
= open_file_ntcreate(conn
,oldname
,&sbuf1
,
1746 FILE_READ_DATA
, /* Read-only. */
1747 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
1749 0, /* No create options. */
1750 FILE_ATTRIBUTE_NORMAL
,
1754 if (!NT_STATUS_IS_OK(status
)) {
1758 status
= open_file_ntcreate(conn
,newname
,&sbuf2
,
1759 FILE_WRITE_DATA
, /* Read-only. */
1760 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
1762 0, /* No create options. */
1767 if (!NT_STATUS_IS_OK(status
)) {
1768 close_file(fsp1
,ERROR_CLOSE
);
1772 if (sbuf1
.st_size
) {
1773 ret
= vfs_transfer_file(fsp1
, fsp2
, sbuf1
.st_size
);
1777 * As we are opening fsp1 read-only we only expect
1778 * an error on close on fsp2 if we are out of space.
1779 * Thus we don't look at the error return from the
1782 close_file(fsp1
,NORMAL_CLOSE
);
1784 /* Ensure the modtime is set correctly on the destination file. */
1785 fsp_set_pending_modtime(fsp2
, get_mtimespec(&sbuf1
));
1787 status
= close_file(fsp2
,NORMAL_CLOSE
);
1789 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1790 creates the file. This isn't the correct thing to do in the copy
1792 file_set_dosmode(conn
, newname
, fattr
, &sbuf2
,
1793 parent_dirname(newname
));
1795 if (ret
< (SMB_OFF_T
)sbuf1
.st_size
) {
1796 return NT_STATUS_DISK_FULL
;
1799 if (!NT_STATUS_IS_OK(status
)) {
1800 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1801 nt_errstr(status
), oldname
, newname
));
1806 /****************************************************************************
1807 Reply to a NT rename request.
1808 ****************************************************************************/
1810 int reply_ntrename(connection_struct
*conn
,
1811 char *inbuf
,char *outbuf
,int length
,int bufsize
)
1818 BOOL src_has_wcard
= False
;
1819 BOOL dest_has_wcard
= False
;
1820 uint32 attrs
= SVAL(inbuf
,smb_vwv0
);
1821 uint16 rename_type
= SVAL(inbuf
,smb_vwv1
);
1823 START_PROFILE(SMBntrename
);
1825 p
= smb_buf(inbuf
) + 1;
1826 p
+= srvstr_get_path_wcard(inbuf
, oldname
, p
, sizeof(oldname
), 0, STR_TERMINATE
, &status
, &src_has_wcard
);
1827 if (!NT_STATUS_IS_OK(status
)) {
1828 END_PROFILE(SMBntrename
);
1829 return ERROR_NT(status
);
1832 if( is_ntfs_stream_name(oldname
)) {
1833 /* Can't rename a stream. */
1834 END_PROFILE(SMBntrename
);
1835 return ERROR_NT(NT_STATUS_ACCESS_DENIED
);
1838 if (ms_has_wild(oldname
)) {
1839 END_PROFILE(SMBntrename
);
1840 return ERROR_NT(NT_STATUS_OBJECT_PATH_SYNTAX_BAD
);
1844 p
+= srvstr_get_path_wcard(inbuf
, newname
, p
, sizeof(newname
), 0, STR_TERMINATE
, &status
, &dest_has_wcard
);
1845 if (!NT_STATUS_IS_OK(status
)) {
1846 END_PROFILE(SMBntrename
);
1847 return ERROR_NT(status
);
1850 status
= resolve_dfspath(conn
, SVAL(inbuf
,smb_flg2
) & FLAGS2_DFS_PATHNAMES
, oldname
);
1851 if (!NT_STATUS_IS_OK(status
)) {
1852 END_PROFILE(SMBntrename
);
1853 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
1854 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED
, ERRSRV
, ERRbadpath
);
1856 return ERROR_NT(status
);
1859 status
= resolve_dfspath(conn
, SVAL(inbuf
,smb_flg2
) & FLAGS2_DFS_PATHNAMES
, newname
);
1860 if (!NT_STATUS_IS_OK(status
)) {
1861 END_PROFILE(SMBntrename
);
1862 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
1863 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED
, ERRSRV
, ERRbadpath
);
1865 return ERROR_NT(status
);
1868 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname
,newname
));
1870 switch(rename_type
) {
1871 case RENAME_FLAG_RENAME
:
1872 status
= rename_internals(conn
, oldname
, newname
, attrs
, False
, src_has_wcard
, dest_has_wcard
);
1874 case RENAME_FLAG_HARD_LINK
:
1875 if (src_has_wcard
|| dest_has_wcard
) {
1877 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1879 status
= hardlink_internals(conn
, oldname
, newname
);
1882 case RENAME_FLAG_COPY
:
1883 if (src_has_wcard
|| dest_has_wcard
) {
1885 status
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
1887 status
= copy_internals(conn
, oldname
, newname
, attrs
);
1890 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION
:
1891 status
= NT_STATUS_INVALID_PARAMETER
;
1894 status
= NT_STATUS_ACCESS_DENIED
; /* Default error. */
1898 if (!NT_STATUS_IS_OK(status
)) {
1899 END_PROFILE(SMBntrename
);
1900 if (open_was_deferred(SVAL(inbuf
,smb_mid
))) {
1901 /* We have re-scheduled this call. */
1904 return ERROR_NT(status
);
1907 outsize
= set_message(inbuf
,outbuf
,0,0,False
);
1909 END_PROFILE(SMBntrename
);
1913 /****************************************************************************
1914 Reply to a notify change - queue the request and
1915 don't allow a directory to be opened.
1916 ****************************************************************************/
1918 static int call_nt_transact_notify_change(connection_struct
*conn
, char *inbuf
,
1919 char *outbuf
, int length
,
1921 uint16
**ppsetup
, uint32 setup_count
,
1923 uint32 parameter_count
,
1924 char **ppdata
, uint32 data_count
,
1925 uint32 max_data_count
,
1926 uint32 max_param_count
)
1928 uint16
*setup
= *ppsetup
;
1934 if(setup_count
< 6) {
1935 return ERROR_DOS(ERRDOS
,ERRbadfunc
);
1938 fsp
= file_fsp((char *)setup
,4);
1939 filter
= IVAL(setup
, 0);
1940 recursive
= (SVAL(setup
, 6) != 0) ? True
: False
;
1942 DEBUG(3,("call_nt_transact_notify_change\n"));
1945 return ERROR_DOS(ERRDOS
,ERRbadfid
);
1949 char *filter_string
;
1951 if (!(filter_string
= notify_filter_string(NULL
, filter
))) {
1952 return ERROR_NT(NT_STATUS_NO_MEMORY
);
1955 DEBUG(3,("call_nt_transact_notify_change: notify change "
1956 "called on %s, filter = %s, recursive = %d\n",
1957 fsp
->fsp_name
, filter_string
, recursive
));
1959 TALLOC_FREE(filter_string
);
1962 if((!fsp
->is_directory
) || (conn
!= fsp
->conn
)) {
1963 return ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
1966 if (fsp
->notify
== NULL
) {
1968 status
= change_notify_create(fsp
, filter
, recursive
);
1970 if (!NT_STATUS_IS_OK(status
)) {
1971 DEBUG(10, ("change_notify_create returned %s\n",
1972 nt_errstr(status
)));
1973 return ERROR_NT(status
);
1977 if (fsp
->notify
->num_changes
!= 0) {
1980 * We've got changes pending, respond immediately
1984 * TODO: write a torture test to check the filtering behaviour
1988 change_notify_reply(inbuf
, fsp
->notify
);
1991 * change_notify_reply() above has independently sent its
1998 * No changes pending, queue the request
2001 status
= change_notify_add_request(inbuf
, filter
, recursive
, fsp
);
2002 if (!NT_STATUS_IS_OK(status
)) {
2003 return ERROR_NT(status
);
2009 /****************************************************************************
2010 Reply to an NT transact rename command.
2011 ****************************************************************************/
2013 static int call_nt_transact_rename(connection_struct
*conn
, char *inbuf
, char *outbuf
, int length
, int bufsize
,
2014 uint16
**ppsetup
, uint32 setup_count
,
2015 char **ppparams
, uint32 parameter_count
,
2016 char **ppdata
, uint32 data_count
, uint32 max_data_count
)
2018 char *params
= *ppparams
;
2020 files_struct
*fsp
= NULL
;
2021 BOOL replace_if_exists
= False
;
2022 BOOL dest_has_wcard
= False
;
2025 if(parameter_count
< 5) {
2026 return ERROR_DOS(ERRDOS
,ERRbadfunc
);
2029 fsp
= file_fsp(params
, 0);
2030 replace_if_exists
= (SVAL(params
,2) & RENAME_REPLACE_IF_EXISTS
) ? True
: False
;
2031 CHECK_FSP(fsp
, conn
);
2032 srvstr_get_path_wcard(inbuf
, new_name
, params
+4, sizeof(new_name
), parameter_count
- 4,
2033 STR_TERMINATE
, &status
, &dest_has_wcard
);
2034 if (!NT_STATUS_IS_OK(status
)) {
2035 return ERROR_NT(status
);
2038 status
= rename_internals(conn
, fsp
->fsp_name
,
2039 new_name
, 0, replace_if_exists
, False
, dest_has_wcard
);
2041 if (!NT_STATUS_IS_OK(status
)) {
2042 if (open_was_deferred(SVAL(inbuf
,smb_mid
))) {
2043 /* We have re-scheduled this call. */
2046 return ERROR_NT(status
);
2050 * Rename was successful.
2052 send_nt_replies(inbuf
, outbuf
, bufsize
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
2054 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2055 fsp
->fsp_name
, new_name
));
2060 /******************************************************************************
2061 Fake up a completely empty SD.
2062 *******************************************************************************/
2064 static size_t get_null_nt_acl(TALLOC_CTX
*mem_ctx
, SEC_DESC
**ppsd
)
2068 *ppsd
= make_standard_sec_desc( mem_ctx
, &global_sid_World
, &global_sid_World
, NULL
, &sd_size
);
2070 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2077 /****************************************************************************
2078 Reply to query a security descriptor.
2079 ****************************************************************************/
2081 static int call_nt_transact_query_security_desc(connection_struct
*conn
, char *inbuf
, char *outbuf
, int length
, int bufsize
,
2082 uint16
**ppsetup
, uint32 setup_count
,
2083 char **ppparams
, uint32 parameter_count
,
2084 char **ppdata
, uint32 data_count
, uint32 max_data_count
)
2086 char *params
= *ppparams
;
2087 char *data
= *ppdata
;
2089 SEC_DESC
*psd
= NULL
;
2091 uint32 security_info_wanted
;
2092 TALLOC_CTX
*mem_ctx
;
2093 files_struct
*fsp
= NULL
;
2095 if(parameter_count
< 8) {
2096 return ERROR_DOS(ERRDOS
,ERRbadfunc
);
2099 fsp
= file_fsp(params
,0);
2101 return ERROR_DOS(ERRDOS
,ERRbadfid
);
2104 security_info_wanted
= IVAL(params
,4);
2106 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp
->fsp_name
,
2107 (unsigned int)security_info_wanted
));
2109 params
= nttrans_realloc(ppparams
, 4);
2110 if(params
== NULL
) {
2111 return ERROR_DOS(ERRDOS
,ERRnomem
);
2114 if ((mem_ctx
= talloc_init("call_nt_transact_query_security_desc")) == NULL
) {
2115 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2116 return ERROR_DOS(ERRDOS
,ERRnomem
);
2120 * Get the permissions to return.
2123 if (!lp_nt_acl_support(SNUM(conn
))) {
2124 sd_size
= get_null_nt_acl(mem_ctx
, &psd
);
2126 sd_size
= SMB_VFS_FGET_NT_ACL(fsp
, fsp
->fh
->fd
, security_info_wanted
, &psd
);
2130 talloc_destroy(mem_ctx
);
2131 return(UNIXERROR(ERRDOS
,ERRnoaccess
));
2134 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size
));
2136 SIVAL(params
,0,(uint32
)sd_size
);
2138 if(max_data_count
< sd_size
) {
2140 send_nt_replies(inbuf
, outbuf
, bufsize
, NT_STATUS_BUFFER_TOO_SMALL
,
2141 params
, 4, *ppdata
, 0);
2142 talloc_destroy(mem_ctx
);
2147 * Allocate the data we will point this at.
2150 data
= nttrans_realloc(ppdata
, sd_size
);
2152 talloc_destroy(mem_ctx
);
2153 return ERROR_DOS(ERRDOS
,ERRnomem
);
2157 * Init the parse struct we will marshall into.
2160 prs_init(&pd
, 0, mem_ctx
, MARSHALL
);
2163 * Setup the prs_struct to point at the memory we just
2167 prs_give_memory( &pd
, data
, (uint32
)sd_size
, False
);
2170 * Finally, linearize into the outgoing buffer.
2173 if(!sec_io_desc( "sd data", &psd
, &pd
, 1)) {
2174 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2175 security descriptor.\n"));
2177 * Return access denied for want of a better error message..
2179 talloc_destroy(mem_ctx
);
2180 return(UNIXERROR(ERRDOS
,ERRnoaccess
));
2184 * Now we can delete the security descriptor.
2187 talloc_destroy(mem_ctx
);
2189 send_nt_replies(inbuf
, outbuf
, bufsize
, NT_STATUS_OK
, params
, 4, data
,
2194 /****************************************************************************
2195 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2196 ****************************************************************************/
2198 static int call_nt_transact_set_security_desc(connection_struct
*conn
, char *inbuf
, char *outbuf
, int length
, int bufsize
,
2199 uint16
**ppsetup
, uint32 setup_count
,
2200 char **ppparams
, uint32 parameter_count
,
2201 char **ppdata
, uint32 data_count
, uint32 max_data_count
)
2203 char *params
= *ppparams
;
2204 char *data
= *ppdata
;
2205 files_struct
*fsp
= NULL
;
2206 uint32 security_info_sent
= 0;
2209 if(parameter_count
< 8) {
2210 return ERROR_DOS(ERRDOS
,ERRbadfunc
);
2213 if((fsp
= file_fsp(params
,0)) == NULL
) {
2214 return ERROR_DOS(ERRDOS
,ERRbadfid
);
2217 if(!lp_nt_acl_support(SNUM(conn
))) {
2221 security_info_sent
= IVAL(params
,4);
2223 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp
->fsp_name
,
2224 (unsigned int)security_info_sent
));
2226 if (data_count
== 0) {
2227 return ERROR_DOS(ERRDOS
, ERRnoaccess
);
2230 if (!NT_STATUS_IS_OK(nt_status
= set_sd( fsp
, data
, data_count
, security_info_sent
))) {
2231 return ERROR_NT(nt_status
);
2236 send_nt_replies(inbuf
, outbuf
, bufsize
, NT_STATUS_OK
, NULL
, 0, NULL
, 0);
2240 /****************************************************************************
2242 ****************************************************************************/
2244 static int call_nt_transact_ioctl(connection_struct
*conn
, char *inbuf
, char *outbuf
, int length
, int bufsize
,
2245 uint16
**ppsetup
, uint32 setup_count
,
2246 char **ppparams
, uint32 parameter_count
,
2247 char **ppdata
, uint32 data_count
, uint32 max_data_count
)
2254 static BOOL logged_message
;
2255 char *pdata
= *ppdata
;
2257 if (setup_count
!= 8) {
2258 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count
));
2259 return ERROR_NT(NT_STATUS_NOT_SUPPORTED
);
2262 function
= IVAL(*ppsetup
, 0);
2263 fidnum
= SVAL(*ppsetup
, 4);
2264 isFSctl
= CVAL(*ppsetup
, 6);
2265 compfilter
= CVAL(*ppsetup
, 7);
2267 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2268 function
, fidnum
, isFSctl
, compfilter
));
2270 fsp
=file_fsp((char *)*ppsetup
, 4);
2271 /* this check is done in each implemented function case for now
2272 because I don't want to break anything... --metze
2273 FSP_BELONGS_CONN(fsp,conn);*/
2276 case FSCTL_SET_SPARSE
:
2277 /* pretend this succeeded - tho strictly we should
2278 mark the file sparse (if the local fs supports it)
2279 so we can know if we need to pre-allocate or not */
2281 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum
));
2282 send_nt_replies(inbuf
, outbuf
, bufsize
, NT_STATUS_OK
, NULL
, 0, NULL
,
2286 case FSCTL_CREATE_OR_GET_OBJECT_ID
:
2288 unsigned char objid
[16];
2290 /* This should return the object-id on this file.
2291 * I think I'll make this be the inode+dev. JRA.
2294 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum
));
2297 pdata
= nttrans_realloc(ppdata
, data_count
);
2298 if (pdata
== NULL
) {
2299 return ERROR_NT(NT_STATUS_NO_MEMORY
);
2301 push_file_id_16(pdata
, &fsp
->file_id
);
2302 memcpy(pdata
+16,create_volume_objectid(conn
,objid
),16);
2303 push_file_id_16(pdata
+32, &fsp
->file_id
);
2304 send_nt_replies(inbuf
, outbuf
, bufsize
, NT_STATUS_OK
, NULL
, 0, pdata
, data_count
);
2308 case FSCTL_GET_REPARSE_POINT
:
2309 /* pretend this fail - my winXP does it like this
2313 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum
));
2314 return ERROR_NT(NT_STATUS_NOT_A_REPARSE_POINT
);
2316 case FSCTL_SET_REPARSE_POINT
:
2317 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2321 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum
));
2322 return ERROR_NT(NT_STATUS_NOT_A_REPARSE_POINT
);
2324 case FSCTL_GET_SHADOW_COPY_DATA
: /* don't know if this name is right...*/
2327 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2328 * and return their volume names. If max_data_count is 16, then it is just
2329 * asking for the number of volumes and length of the combined names.
2331 * pdata is the data allocated by our caller, but that uses
2332 * total_data_count (which is 0 in our case) rather than max_data_count.
2333 * Allocate the correct amount and return the pointer to let
2334 * it be deallocated when we return.
2336 SHADOW_COPY_DATA
*shadow_data
= NULL
;
2337 TALLOC_CTX
*shadow_mem_ctx
= NULL
;
2338 BOOL labels
= False
;
2339 uint32 labels_data_count
= 0;
2343 FSP_BELONGS_CONN(fsp
,conn
);
2345 if (max_data_count
< 16) {
2346 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2348 return ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
2351 if (max_data_count
> 16) {
2355 shadow_mem_ctx
= talloc_init("SHADOW_COPY_DATA");
2356 if (shadow_mem_ctx
== NULL
) {
2357 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2358 return ERROR_NT(NT_STATUS_NO_MEMORY
);
2361 shadow_data
= TALLOC_ZERO_P(shadow_mem_ctx
,SHADOW_COPY_DATA
);
2362 if (shadow_data
== NULL
) {
2363 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2364 talloc_destroy(shadow_mem_ctx
);
2365 return ERROR_NT(NT_STATUS_NO_MEMORY
);
2368 shadow_data
->mem_ctx
= shadow_mem_ctx
;
2371 * Call the VFS routine to actually do the work.
2373 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp
, shadow_data
, labels
)!=0) {
2374 talloc_destroy(shadow_data
->mem_ctx
);
2375 if (errno
== ENOSYS
) {
2376 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2377 conn
->connectpath
));
2378 return ERROR_NT(NT_STATUS_NOT_SUPPORTED
);
2380 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2381 conn
->connectpath
));
2382 return ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
2386 labels_data_count
= (shadow_data
->num_volumes
*2*sizeof(SHADOW_COPY_LABEL
))+2;
2391 data_count
= 12+labels_data_count
+4;
2394 if (max_data_count
<data_count
) {
2395 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2396 max_data_count
,data_count
));
2397 talloc_destroy(shadow_data
->mem_ctx
);
2398 return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL
);
2401 pdata
= nttrans_realloc(ppdata
, data_count
);
2402 if (pdata
== NULL
) {
2403 talloc_destroy(shadow_data
->mem_ctx
);
2404 return ERROR_NT(NT_STATUS_NO_MEMORY
);
2409 /* num_volumes 4 bytes */
2410 SIVAL(pdata
,0,shadow_data
->num_volumes
);
2413 /* num_labels 4 bytes */
2414 SIVAL(pdata
,4,shadow_data
->num_volumes
);
2417 /* needed_data_count 4 bytes */
2418 SIVAL(pdata
,8,labels_data_count
);
2422 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2423 shadow_data
->num_volumes
,fsp
->fsp_name
));
2424 if (labels
&& shadow_data
->labels
) {
2425 for (i
=0;i
<shadow_data
->num_volumes
;i
++) {
2426 srvstr_push(outbuf
, cur_pdata
, shadow_data
->labels
[i
], 2*sizeof(SHADOW_COPY_LABEL
), STR_UNICODE
|STR_TERMINATE
);
2427 cur_pdata
+=2*sizeof(SHADOW_COPY_LABEL
);
2428 DEBUGADD(10,("Label[%u]: '%s'\n",i
,shadow_data
->labels
[i
]));
2432 talloc_destroy(shadow_data
->mem_ctx
);
2434 send_nt_replies(inbuf
, outbuf
, bufsize
, NT_STATUS_OK
, NULL
, 0,
2440 case FSCTL_FIND_FILES_BY_SID
: /* I hope this name is right */
2442 /* pretend this succeeded -
2444 * we have to send back a list with all files owned by this SID
2446 * but I have to check that --metze
2450 size_t sid_len
= MIN(data_count
-4,SID_MAX_SIZE
);
2452 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum
));
2454 FSP_BELONGS_CONN(fsp
,conn
);
2456 /* unknown 4 bytes: this is not the length of the sid :-( */
2457 /*unknown = IVAL(pdata,0);*/
2459 sid_parse(pdata
+4,sid_len
,&sid
);
2460 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid
)));
2462 if (!sid_to_uid(&sid
, &uid
)) {
2463 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2464 sid_string_static(&sid
),(unsigned long)sid_len
));
2468 /* we can take a look at the find source :-)
2470 * find ./ -uid $uid -name '*' is what we need here
2473 * and send 4bytes len and then NULL terminated unicode strings
2476 * but I don't know how to deal with the paged results
2477 * (maybe we can hang the result anywhere in the fsp struct)
2479 * we don't send all files at once
2480 * and at the next we should *not* start from the beginning,
2481 * so we have to cache the result
2486 /* this works for now... */
2487 send_nt_replies(inbuf
, outbuf
, bufsize
, NT_STATUS_OK
, NULL
, 0,
2492 if (!logged_message
) {
2493 logged_message
= True
; /* Only print this once... */
2494 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2499 return ERROR_NT(NT_STATUS_NOT_SUPPORTED
);
2503 #ifdef HAVE_SYS_QUOTAS
2504 /****************************************************************************
2505 Reply to get user quota
2506 ****************************************************************************/
2508 static int call_nt_transact_get_user_quota(connection_struct
*conn
, char *inbuf
, char *outbuf
, int length
, int bufsize
,
2509 uint16
**ppsetup
, uint32 setup_count
,
2510 char **ppparams
, uint32 parameter_count
,
2511 char **ppdata
, uint32 data_count
, uint32 max_data_count
)
2513 NTSTATUS nt_status
= NT_STATUS_OK
;
2514 char *params
= *ppparams
;
2515 char *pdata
= *ppdata
;
2517 int data_len
=0,param_len
=0;
2520 files_struct
*fsp
= NULL
;
2524 BOOL start_enum
= True
;
2525 SMB_NTQUOTA_STRUCT qt
;
2526 SMB_NTQUOTA_LIST
*tmp_list
;
2527 SMB_NTQUOTA_HANDLE
*qt_handle
= NULL
;
2532 if (current_user
.ut
.uid
!= 0) {
2533 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2534 lp_servicename(SNUM(conn
)),conn
->user
));
2535 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
2539 * Ensure minimum number of parameters sent.
2542 if (parameter_count
< 4) {
2543 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count
));
2544 return ERROR_DOS(ERRDOS
,ERRinvalidparam
);
2547 /* maybe we can check the quota_fnum */
2548 fsp
= file_fsp(params
,0);
2549 if (!CHECK_NTQUOTA_HANDLE_OK(fsp
,conn
)) {
2550 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2551 return ERROR_NT(NT_STATUS_INVALID_HANDLE
);
2554 /* the NULL pointer checking for fsp->fake_file_handle->pd
2555 * is done by CHECK_NTQUOTA_HANDLE_OK()
2557 qt_handle
= (SMB_NTQUOTA_HANDLE
*)fsp
->fake_file_handle
->pd
;
2559 level
= SVAL(params
,2);
2561 /* unknown 12 bytes leading in params */
2564 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE
:
2565 /* seems that we should continue with the enum here --metze */
2567 if (qt_handle
->quota_list
!=NULL
&&
2568 qt_handle
->tmp_list
==NULL
) {
2571 free_ntquota_list(&(qt_handle
->quota_list
));
2573 /* Realloc the size of parameters and data we will return */
2575 params
= nttrans_realloc(ppparams
, param_len
);
2576 if(params
== NULL
) {
2577 return ERROR_DOS(ERRDOS
,ERRnomem
);
2581 SIVAL(params
,0,data_len
);
2588 case TRANSACT_GET_USER_QUOTA_LIST_START
:
2590 if (qt_handle
->quota_list
==NULL
&&
2591 qt_handle
->tmp_list
==NULL
) {
2595 if (start_enum
&& vfs_get_user_ntquota_list(fsp
,&(qt_handle
->quota_list
))!=0)
2596 return ERROR_DOS(ERRSRV
,ERRerror
);
2598 /* Realloc the size of parameters and data we will return */
2600 params
= nttrans_realloc(ppparams
, param_len
);
2601 if(params
== NULL
) {
2602 return ERROR_DOS(ERRDOS
,ERRnomem
);
2605 /* we should not trust the value in max_data_count*/
2606 max_data_count
= MIN(max_data_count
,2048);
2608 pdata
= nttrans_realloc(ppdata
, max_data_count
);/* should be max data count from client*/
2610 return ERROR_DOS(ERRDOS
,ERRnomem
);
2615 /* set params Size of returned Quota Data 4 bytes*/
2616 /* but set it later when we know it */
2618 /* for each entry push the data */
2621 qt_handle
->tmp_list
= qt_handle
->quota_list
;
2624 tmp_list
= qt_handle
->tmp_list
;
2626 for (;((tmp_list
!=NULL
)&&((qt_len
+40+SID_MAX_SIZE
)<max_data_count
));
2627 tmp_list
=tmp_list
->next
,entry
+=entry_len
,qt_len
+=entry_len
) {
2629 sid_len
= sid_size(&tmp_list
->quotas
->sid
);
2630 entry_len
= 40 + sid_len
;
2632 /* nextoffset entry 4 bytes */
2633 SIVAL(entry
,0,entry_len
);
2635 /* then the len of the SID 4 bytes */
2636 SIVAL(entry
,4,sid_len
);
2638 /* unknown data 8 bytes SMB_BIG_UINT */
2639 SBIG_UINT(entry
,8,(SMB_BIG_UINT
)0); /* this is not 0 in windows...-metze*/
2641 /* the used disk space 8 bytes SMB_BIG_UINT */
2642 SBIG_UINT(entry
,16,tmp_list
->quotas
->usedspace
);
2644 /* the soft quotas 8 bytes SMB_BIG_UINT */
2645 SBIG_UINT(entry
,24,tmp_list
->quotas
->softlim
);
2647 /* the hard quotas 8 bytes SMB_BIG_UINT */
2648 SBIG_UINT(entry
,32,tmp_list
->quotas
->hardlim
);
2650 /* and now the SID */
2651 sid_linearize(entry
+40, sid_len
, &tmp_list
->quotas
->sid
);
2654 qt_handle
->tmp_list
= tmp_list
;
2656 /* overwrite the offset of the last entry */
2657 SIVAL(entry
-entry_len
,0,0);
2659 data_len
= 4+qt_len
;
2660 /* overwrite the params quota_data_len */
2661 SIVAL(params
,0,data_len
);
2665 case TRANSACT_GET_USER_QUOTA_FOR_SID
:
2667 /* unknown 4 bytes IVAL(pdata,0) */
2669 if (data_count
< 8) {
2670 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count
,8));
2671 return ERROR_DOS(ERRDOS
,ERRunknownlevel
);
2674 sid_len
= IVAL(pdata
,4);
2675 /* Ensure this is less than 1mb. */
2676 if (sid_len
> (1024*1024)) {
2677 return ERROR_DOS(ERRDOS
,ERRnomem
);
2680 if (data_count
< 8+sid_len
) {
2681 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count
,(unsigned long)(8+sid_len
)));
2682 return ERROR_DOS(ERRDOS
,ERRunknownlevel
);
2685 data_len
= 4+40+sid_len
;
2687 if (max_data_count
< data_len
) {
2688 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2689 max_data_count
, data_len
));
2691 SIVAL(params
,0,data_len
);
2693 nt_status
= NT_STATUS_BUFFER_TOO_SMALL
;
2697 sid_parse(pdata
+8,sid_len
,&sid
);
2699 if (vfs_get_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2702 * we have to return zero's in all fields
2703 * instead of returning an error here
2708 /* Realloc the size of parameters and data we will return */
2710 params
= nttrans_realloc(ppparams
, param_len
);
2711 if(params
== NULL
) {
2712 return ERROR_DOS(ERRDOS
,ERRnomem
);
2715 pdata
= nttrans_realloc(ppdata
, data_len
);
2717 return ERROR_DOS(ERRDOS
,ERRnomem
);
2722 /* set params Size of returned Quota Data 4 bytes*/
2723 SIVAL(params
,0,data_len
);
2725 /* nextoffset entry 4 bytes */
2728 /* then the len of the SID 4 bytes */
2729 SIVAL(entry
,4,sid_len
);
2731 /* unknown data 8 bytes SMB_BIG_UINT */
2732 SBIG_UINT(entry
,8,(SMB_BIG_UINT
)0); /* this is not 0 in windows...-mezte*/
2734 /* the used disk space 8 bytes SMB_BIG_UINT */
2735 SBIG_UINT(entry
,16,qt
.usedspace
);
2737 /* the soft quotas 8 bytes SMB_BIG_UINT */
2738 SBIG_UINT(entry
,24,qt
.softlim
);
2740 /* the hard quotas 8 bytes SMB_BIG_UINT */
2741 SBIG_UINT(entry
,32,qt
.hardlim
);
2743 /* and now the SID */
2744 sid_linearize(entry
+40, sid_len
, &sid
);
2749 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp
->fnum
,level
));
2750 return ERROR_DOS(ERRSRV
,ERRerror
);
2754 send_nt_replies(inbuf
, outbuf
, bufsize
, nt_status
, params
, param_len
,
2760 /****************************************************************************
2761 Reply to set user quota
2762 ****************************************************************************/
2764 static int call_nt_transact_set_user_quota(connection_struct
*conn
, char *inbuf
, char *outbuf
, int length
, int bufsize
,
2765 uint16
**ppsetup
, uint32 setup_count
,
2766 char **ppparams
, uint32 parameter_count
,
2767 char **ppdata
, uint32 data_count
, uint32 max_data_count
)
2769 char *params
= *ppparams
;
2770 char *pdata
= *ppdata
;
2771 int data_len
=0,param_len
=0;
2772 SMB_NTQUOTA_STRUCT qt
;
2775 files_struct
*fsp
= NULL
;
2780 if (current_user
.ut
.uid
!= 0) {
2781 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2782 lp_servicename(SNUM(conn
)),conn
->user
));
2783 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
2787 * Ensure minimum number of parameters sent.
2790 if (parameter_count
< 2) {
2791 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count
));
2792 return ERROR_DOS(ERRDOS
,ERRinvalidparam
);
2795 /* maybe we can check the quota_fnum */
2796 fsp
= file_fsp(params
,0);
2797 if (!CHECK_NTQUOTA_HANDLE_OK(fsp
,conn
)) {
2798 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2799 return ERROR_NT(NT_STATUS_INVALID_HANDLE
);
2802 if (data_count
< 40) {
2803 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count
,40));
2804 return ERROR_DOS(ERRDOS
,ERRunknownlevel
);
2807 /* offset to next quota record.
2808 * 4 bytes IVAL(pdata,0)
2813 sid_len
= IVAL(pdata
,4);
2815 if (data_count
< 40+sid_len
) {
2816 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count
,(unsigned long)40+sid_len
));
2817 return ERROR_DOS(ERRDOS
,ERRunknownlevel
);
2820 /* unknown 8 bytes in pdata
2821 * maybe its the change time in NTTIME
2824 /* the used space 8 bytes (SMB_BIG_UINT)*/
2825 qt
.usedspace
= (SMB_BIG_UINT
)IVAL(pdata
,16);
2826 #ifdef LARGE_SMB_OFF_T
2827 qt
.usedspace
|= (((SMB_BIG_UINT
)IVAL(pdata
,20)) << 32);
2828 #else /* LARGE_SMB_OFF_T */
2829 if ((IVAL(pdata
,20) != 0)&&
2830 ((qt
.usedspace
!= 0xFFFFFFFF)||
2831 (IVAL(pdata
,20)!=0xFFFFFFFF))) {
2832 /* more than 32 bits? */
2833 return ERROR_DOS(ERRDOS
,ERRunknownlevel
);
2835 #endif /* LARGE_SMB_OFF_T */
2837 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2838 qt
.softlim
= (SMB_BIG_UINT
)IVAL(pdata
,24);
2839 #ifdef LARGE_SMB_OFF_T
2840 qt
.softlim
|= (((SMB_BIG_UINT
)IVAL(pdata
,28)) << 32);
2841 #else /* LARGE_SMB_OFF_T */
2842 if ((IVAL(pdata
,28) != 0)&&
2843 ((qt
.softlim
!= 0xFFFFFFFF)||
2844 (IVAL(pdata
,28)!=0xFFFFFFFF))) {
2845 /* more than 32 bits? */
2846 return ERROR_DOS(ERRDOS
,ERRunknownlevel
);
2848 #endif /* LARGE_SMB_OFF_T */
2850 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2851 qt
.hardlim
= (SMB_BIG_UINT
)IVAL(pdata
,32);
2852 #ifdef LARGE_SMB_OFF_T
2853 qt
.hardlim
|= (((SMB_BIG_UINT
)IVAL(pdata
,36)) << 32);
2854 #else /* LARGE_SMB_OFF_T */
2855 if ((IVAL(pdata
,36) != 0)&&
2856 ((qt
.hardlim
!= 0xFFFFFFFF)||
2857 (IVAL(pdata
,36)!=0xFFFFFFFF))) {
2858 /* more than 32 bits? */
2859 return ERROR_DOS(ERRDOS
,ERRunknownlevel
);
2861 #endif /* LARGE_SMB_OFF_T */
2863 sid_parse(pdata
+40,sid_len
,&sid
);
2864 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid
)));
2866 /* 44 unknown bytes left... */
2868 if (vfs_set_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &qt
)!=0) {
2869 return ERROR_DOS(ERRSRV
,ERRerror
);
2872 send_nt_replies(inbuf
, outbuf
, bufsize
, NT_STATUS_OK
, params
, param_len
,
2877 #endif /* HAVE_SYS_QUOTAS */
2879 static int handle_nttrans(connection_struct
*conn
,
2880 struct trans_state
*state
,
2881 char *inbuf
, char *outbuf
, int size
, int bufsize
)
2885 if (Protocol
>= PROTOCOL_NT1
) {
2886 SSVAL(outbuf
,smb_flg2
,SVAL(outbuf
,smb_flg2
) | 0x40); /* IS_LONG_NAME */
2889 /* Now we must call the relevant NT_TRANS function */
2890 switch(state
->call
) {
2891 case NT_TRANSACT_CREATE
:
2893 START_PROFILE(NT_transact_create
);
2894 outsize
= call_nt_transact_create(conn
, inbuf
, outbuf
,
2896 &state
->setup
, state
->setup_count
,
2897 &state
->param
, state
->total_param
,
2898 &state
->data
, state
->total_data
,
2899 state
->max_data_return
);
2900 END_PROFILE(NT_transact_create
);
2904 case NT_TRANSACT_IOCTL
:
2906 START_PROFILE(NT_transact_ioctl
);
2907 outsize
= call_nt_transact_ioctl(conn
, inbuf
, outbuf
,
2909 &state
->setup
, state
->setup_count
,
2910 &state
->param
, state
->total_param
,
2911 &state
->data
, state
->total_data
, state
->max_data_return
);
2912 END_PROFILE(NT_transact_ioctl
);
2916 case NT_TRANSACT_SET_SECURITY_DESC
:
2918 START_PROFILE(NT_transact_set_security_desc
);
2919 outsize
= call_nt_transact_set_security_desc(conn
, inbuf
, outbuf
,
2921 &state
->setup
, state
->setup_count
,
2922 &state
->param
, state
->total_param
,
2923 &state
->data
, state
->total_data
, state
->max_data_return
);
2924 END_PROFILE(NT_transact_set_security_desc
);
2928 case NT_TRANSACT_NOTIFY_CHANGE
:
2930 START_PROFILE(NT_transact_notify_change
);
2931 outsize
= call_nt_transact_notify_change(
2932 conn
, inbuf
, outbuf
, size
, bufsize
,
2933 &state
->setup
, state
->setup_count
,
2934 &state
->param
, state
->total_param
,
2935 &state
->data
, state
->total_data
,
2936 state
->max_data_return
,
2937 state
->max_param_return
);
2938 END_PROFILE(NT_transact_notify_change
);
2942 case NT_TRANSACT_RENAME
:
2944 START_PROFILE(NT_transact_rename
);
2945 outsize
= call_nt_transact_rename(conn
, inbuf
, outbuf
,
2947 &state
->setup
, state
->setup_count
,
2948 &state
->param
, state
->total_param
,
2949 &state
->data
, state
->total_data
, state
->max_data_return
);
2950 END_PROFILE(NT_transact_rename
);
2954 case NT_TRANSACT_QUERY_SECURITY_DESC
:
2956 START_PROFILE(NT_transact_query_security_desc
);
2957 outsize
= call_nt_transact_query_security_desc(conn
, inbuf
, outbuf
,
2959 &state
->setup
, state
->setup_count
,
2960 &state
->param
, state
->total_param
,
2961 &state
->data
, state
->total_data
, state
->max_data_return
);
2962 END_PROFILE(NT_transact_query_security_desc
);
2966 #ifdef HAVE_SYS_QUOTAS
2967 case NT_TRANSACT_GET_USER_QUOTA
:
2969 START_PROFILE(NT_transact_get_user_quota
);
2970 outsize
= call_nt_transact_get_user_quota(conn
, inbuf
, outbuf
,
2972 &state
->setup
, state
->setup_count
,
2973 &state
->param
, state
->total_param
,
2974 &state
->data
, state
->total_data
, state
->max_data_return
);
2975 END_PROFILE(NT_transact_get_user_quota
);
2979 case NT_TRANSACT_SET_USER_QUOTA
:
2981 START_PROFILE(NT_transact_set_user_quota
);
2982 outsize
= call_nt_transact_set_user_quota(conn
, inbuf
, outbuf
,
2984 &state
->setup
, state
->setup_count
,
2985 &state
->param
, state
->total_param
,
2986 &state
->data
, state
->total_data
, state
->max_data_return
);
2987 END_PROFILE(NT_transact_set_user_quota
);
2990 #endif /* HAVE_SYS_QUOTAS */
2993 /* Error in request */
2994 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n",
2996 return ERROR_DOS(ERRSRV
,ERRerror
);
3001 /****************************************************************************
3002 Reply to a SMBNTtrans.
3003 ****************************************************************************/
3005 int reply_nttrans(connection_struct
*conn
,
3006 char *inbuf
,char *outbuf
,int size
,int bufsize
)
3009 uint32 pscnt
= IVAL(inbuf
,smb_nt_ParameterCount
);
3010 uint32 psoff
= IVAL(inbuf
,smb_nt_ParameterOffset
);
3011 uint32 dscnt
= IVAL(inbuf
,smb_nt_DataCount
);
3012 uint32 dsoff
= IVAL(inbuf
,smb_nt_DataOffset
);
3014 uint16 function_code
= SVAL( inbuf
, smb_nt_Function
);
3016 struct trans_state
*state
;
3018 START_PROFILE(SMBnttrans
);
3020 if (IS_IPC(conn
) && (function_code
!= NT_TRANSACT_CREATE
)) {
3021 END_PROFILE(SMBnttrans
);
3022 return ERROR_DOS(ERRSRV
,ERRaccess
);
3025 result
= allow_new_trans(conn
->pending_trans
, SVAL(inbuf
, smb_mid
));
3026 if (!NT_STATUS_IS_OK(result
)) {
3027 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result
)));
3028 END_PROFILE(SMBnttrans
);
3029 return ERROR_NT(result
);
3032 if ((state
= TALLOC_P(conn
->mem_ctx
, struct trans_state
)) == NULL
) {
3033 END_PROFILE(SMBnttrans
);
3034 return ERROR_DOS(ERRSRV
,ERRaccess
);
3037 state
->cmd
= SMBnttrans
;
3039 state
->mid
= SVAL(inbuf
,smb_mid
);
3040 state
->vuid
= SVAL(inbuf
,smb_uid
);
3041 state
->total_data
= IVAL(inbuf
, smb_nt_TotalDataCount
);
3043 state
->total_param
= IVAL(inbuf
, smb_nt_TotalParameterCount
);
3044 state
->param
= NULL
;
3045 state
->max_data_return
= IVAL(inbuf
,smb_nt_MaxDataCount
);
3046 state
->max_param_return
= IVAL(inbuf
,smb_nt_MaxParameterCount
);
3048 /* setup count is in *words* */
3049 state
->setup_count
= 2*CVAL(inbuf
,smb_nt_SetupCount
);
3050 state
->setup
= NULL
;
3051 state
->call
= function_code
;
3054 * All nttrans messages we handle have smb_wct == 19 +
3055 * state->setup_count. Ensure this is so as a sanity check.
3058 if(CVAL(inbuf
, smb_wct
) != 19 + (state
->setup_count
/2)) {
3059 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3060 CVAL(inbuf
, smb_wct
), 19 + (state
->setup_count
/2)));
3064 /* Don't allow more than 128mb for each value. */
3065 if ((state
->total_data
> (1024*1024*128)) ||
3066 (state
->total_param
> (1024*1024*128))) {
3067 END_PROFILE(SMBnttrans
);
3068 return ERROR_DOS(ERRDOS
,ERRnomem
);
3071 if ((dscnt
> state
->total_data
) || (pscnt
> state
->total_param
))
3074 if (state
->total_data
) {
3075 /* Can't use talloc here, the core routines do realloc on the
3076 * params and data. */
3077 if ((state
->data
= (char *)SMB_MALLOC(state
->total_data
)) == NULL
) {
3078 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3079 "bytes !\n", (unsigned int)state
->total_data
));
3081 END_PROFILE(SMBnttrans
);
3082 return(ERROR_DOS(ERRDOS
,ERRnomem
));
3084 if ((dsoff
+dscnt
< dsoff
) || (dsoff
+dscnt
< dscnt
))
3086 if ((smb_base(inbuf
)+dsoff
+dscnt
> inbuf
+ size
) ||
3087 (smb_base(inbuf
)+dsoff
+dscnt
< smb_base(inbuf
)))
3090 memcpy(state
->data
,smb_base(inbuf
)+dsoff
,dscnt
);
3093 if (state
->total_param
) {
3094 /* Can't use talloc here, the core routines do realloc on the
3095 * params and data. */
3096 if ((state
->param
= (char *)SMB_MALLOC(state
->total_param
)) == NULL
) {
3097 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3098 "bytes !\n", (unsigned int)state
->total_param
));
3099 SAFE_FREE(state
->data
);
3101 END_PROFILE(SMBnttrans
);
3102 return(ERROR_DOS(ERRDOS
,ERRnomem
));
3104 if ((psoff
+pscnt
< psoff
) || (psoff
+pscnt
< pscnt
))
3106 if ((smb_base(inbuf
)+psoff
+pscnt
> inbuf
+ size
) ||
3107 (smb_base(inbuf
)+psoff
+pscnt
< smb_base(inbuf
)))
3110 memcpy(state
->param
,smb_base(inbuf
)+psoff
,pscnt
);
3113 state
->received_data
= dscnt
;
3114 state
->received_param
= pscnt
;
3116 if(state
->setup_count
> 0) {
3117 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3118 state
->setup_count
));
3119 state
->setup
= (uint16
*)TALLOC(state
, state
->setup_count
);
3120 if (state
->setup
== NULL
) {
3121 DEBUG(0,("reply_nttrans : Out of memory\n"));
3122 SAFE_FREE(state
->data
);
3123 SAFE_FREE(state
->param
);
3125 END_PROFILE(SMBnttrans
);
3126 return ERROR_DOS(ERRDOS
,ERRnomem
);
3129 if ((smb_nt_SetupStart
+ state
->setup_count
< smb_nt_SetupStart
) ||
3130 (smb_nt_SetupStart
+ state
->setup_count
< state
->setup_count
)) {
3133 if (smb_nt_SetupStart
+ state
->setup_count
> size
) {
3137 memcpy( state
->setup
, &inbuf
[smb_nt_SetupStart
], state
->setup_count
);
3138 dump_data(10, (uint8
*)state
->setup
, state
->setup_count
);
3141 if ((state
->received_data
== state
->total_data
) &&
3142 (state
->received_param
== state
->total_param
)) {
3143 outsize
= handle_nttrans(conn
, state
, inbuf
, outbuf
,
3145 SAFE_FREE(state
->param
);
3146 SAFE_FREE(state
->data
);
3148 END_PROFILE(SMBnttrans
);
3152 DLIST_ADD(conn
->pending_trans
, state
);
3154 /* We need to send an interim response then receive the rest
3155 of the parameter/data bytes */
3156 outsize
= set_message(inbuf
,outbuf
,0,0,False
);
3158 END_PROFILE(SMBnttrans
);
3163 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3164 SAFE_FREE(state
->data
);
3165 SAFE_FREE(state
->param
);
3167 END_PROFILE(SMBnttrans
);
3168 return ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
3171 /****************************************************************************
3172 Reply to a SMBnttranss
3173 ****************************************************************************/
3175 int reply_nttranss(connection_struct
*conn
, char *inbuf
,char *outbuf
,
3176 int size
,int bufsize
)
3179 unsigned int pcnt
,poff
,dcnt
,doff
,pdisp
,ddisp
;
3180 struct trans_state
*state
;
3182 START_PROFILE(SMBnttranss
);
3186 for (state
= conn
->pending_trans
; state
!= NULL
;
3187 state
= state
->next
) {
3188 if (state
->mid
== SVAL(inbuf
,smb_mid
)) {
3193 if ((state
== NULL
) || (state
->cmd
!= SMBnttrans
)) {
3194 END_PROFILE(SMBnttranss
);
3195 return ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
3198 /* Revise state->total_param and state->total_data in case they have
3199 changed downwards */
3200 if (IVAL(inbuf
, smb_nts_TotalParameterCount
) < state
->total_param
) {
3201 state
->total_param
= IVAL(inbuf
, smb_nts_TotalParameterCount
);
3203 if (IVAL(inbuf
, smb_nts_TotalDataCount
) < state
->total_data
) {
3204 state
->total_data
= IVAL(inbuf
, smb_nts_TotalDataCount
);
3207 pcnt
= IVAL(inbuf
,smb_nts_ParameterCount
);
3208 poff
= IVAL(inbuf
, smb_nts_ParameterOffset
);
3209 pdisp
= IVAL(inbuf
, smb_nts_ParameterDisplacement
);
3211 dcnt
= IVAL(inbuf
, smb_nts_DataCount
);
3212 ddisp
= IVAL(inbuf
, smb_nts_DataDisplacement
);
3213 doff
= IVAL(inbuf
, smb_nts_DataOffset
);
3215 state
->received_param
+= pcnt
;
3216 state
->received_data
+= dcnt
;
3218 if ((state
->received_data
> state
->total_data
) ||
3219 (state
->received_param
> state
->total_param
))
3223 if (pdisp
+pcnt
> state
->total_param
)
3225 if ((pdisp
+pcnt
< pdisp
) || (pdisp
+pcnt
< pcnt
))
3227 if (pdisp
> state
->total_param
)
3229 if ((smb_base(inbuf
) + poff
+ pcnt
> inbuf
+ size
) ||
3230 (smb_base(inbuf
) + poff
+ pcnt
< smb_base(inbuf
)))
3232 if (state
->param
+ pdisp
< state
->param
)
3235 memcpy(state
->param
+pdisp
,smb_base(inbuf
)+poff
,
3240 if (ddisp
+dcnt
> state
->total_data
)
3242 if ((ddisp
+dcnt
< ddisp
) || (ddisp
+dcnt
< dcnt
))
3244 if (ddisp
> state
->total_data
)
3246 if ((smb_base(inbuf
) + doff
+ dcnt
> inbuf
+ size
) ||
3247 (smb_base(inbuf
) + doff
+ dcnt
< smb_base(inbuf
)))
3249 if (state
->data
+ ddisp
< state
->data
)
3252 memcpy(state
->data
+ddisp
, smb_base(inbuf
)+doff
,
3256 if ((state
->received_param
< state
->total_param
) ||
3257 (state
->received_data
< state
->total_data
)) {
3258 END_PROFILE(SMBnttranss
);
3262 /* construct_reply_common has done us the favor to pre-fill the
3263 * command field with SMBnttranss which is wrong :-)
3265 SCVAL(outbuf
,smb_com
,SMBnttrans
);
3267 outsize
= handle_nttrans(conn
, state
, inbuf
, outbuf
,
3270 DLIST_REMOVE(conn
->pending_trans
, state
);
3271 SAFE_FREE(state
->data
);
3272 SAFE_FREE(state
->param
);
3276 END_PROFILE(SMBnttranss
);
3277 return(ERROR_DOS(ERRSRV
,ERRnosupport
));
3280 END_PROFILE(SMBnttranss
);
3285 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3286 DLIST_REMOVE(conn
->pending_trans
, state
);
3287 SAFE_FREE(state
->data
);
3288 SAFE_FREE(state
->param
);
3290 END_PROFILE(SMBnttranss
);
3291 return ERROR_NT(NT_STATUS_INVALID_PARAMETER
);