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\n");
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(char *outbuf, int bufsize, NTSTATUS nt_error,
72 char *params, int paramsize, char *pdata, int datasize)
74 int data_to_send = datasize;
75 int params_to_send = paramsize;
79 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
80 int alignment_offset = 3;
81 int data_alignment_offset = 0;
84 * Initially set the wcnt area to be 18 - this is true for all
88 set_message(outbuf,18,0,True);
90 if (NT_STATUS_V(nt_error)) {
95 * If there genuinely are no parameters or data to send just send
99 if(params_to_send == 0 && data_to_send == 0) {
101 if (!send_smb(smbd_server_fd(),outbuf)) {
102 exit_server_cleanly("send_nt_replies: send_smb failed.");
108 * When sending params and data ensure that both are nicely aligned.
109 * Only do this alignment when there is also data to send - else
110 * can cause NT redirector problems.
113 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
114 data_alignment_offset = 4 - (params_to_send % 4);
118 * Space is bufsize minus Netbios over TCP header minus SMB header.
119 * The alignment_offset is to align the param bytes on a four byte
120 * boundary (2 bytes for data len, one byte pad).
121 * NT needs this to work correctly.
124 useable_space = bufsize - ((smb_buf(outbuf)+
125 alignment_offset+data_alignment_offset) -
129 * useable_space can never be more than max_send minus the
133 useable_space = MIN(useable_space,
134 max_send - (alignment_offset+data_alignment_offset));
137 while (params_to_send || data_to_send) {
140 * Calculate whether we will totally or partially fill this packet.
143 total_sent_thistime = params_to_send + data_to_send +
144 alignment_offset + data_alignment_offset;
147 * We can never send more than useable_space.
150 total_sent_thistime = MIN(total_sent_thistime, useable_space);
152 set_message(outbuf, 18, total_sent_thistime, True);
155 * Set total params and data to be sent.
158 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
159 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
162 * Calculate how many parameters and data we can fit into
163 * this packet. Parameters get precedence.
166 params_sent_thistime = MIN(params_to_send,useable_space);
167 data_sent_thistime = useable_space - params_sent_thistime;
168 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
170 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
172 if(params_sent_thistime == 0) {
173 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
174 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
177 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
178 * parameter bytes, however the first 4 bytes of outbuf are
179 * the Netbios over TCP header. Thus use smb_base() to subtract
180 * them from the calculation.
183 SIVAL(outbuf,smb_ntr_ParameterOffset,
184 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
186 * Absolute displacement of param bytes sent in this packet.
189 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
193 * Deal with the data portion.
196 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
198 if(data_sent_thistime == 0) {
199 SIVAL(outbuf,smb_ntr_DataOffset,0);
200 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
203 * The offset of the data bytes is the offset of the
204 * parameter bytes plus the number of parameters being sent this time.
207 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
208 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
209 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
213 * Copy the param bytes into the packet.
216 if(params_sent_thistime) {
217 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
221 * Copy in the data bytes
224 if(data_sent_thistime) {
225 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
226 data_alignment_offset,pd,data_sent_thistime);
229 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
230 params_sent_thistime, data_sent_thistime, useable_space));
231 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
232 params_to_send, data_to_send, paramsize, datasize));
234 /* Send the packet */
236 if (!send_smb(smbd_server_fd(),outbuf)) {
237 exit_server_cleanly("send_nt_replies: send_smb failed.");
240 pp += params_sent_thistime;
241 pd += data_sent_thistime;
243 params_to_send -= params_sent_thistime;
244 data_to_send -= data_sent_thistime;
250 if(params_to_send < 0 || data_to_send < 0) {
251 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
252 params_to_send, data_to_send));
260 /****************************************************************************
261 Is it an NTFS stream name ?
262 ****************************************************************************/
264 BOOL is_ntfs_stream_name(const char *fname)
266 if (lp_posix_pathnames()) {
269 return (strchr_m(fname, ':') != NULL) ? True : False;
272 /****************************************************************************
274 ****************************************************************************/
276 static BOOL saved_case_sensitive;
277 static BOOL saved_case_preserve;
278 static BOOL saved_short_case_preserve;
280 /****************************************************************************
282 ****************************************************************************/
284 static void set_posix_case_semantics(connection_struct *conn, uint32 file_attributes)
286 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
290 saved_case_sensitive = conn->case_sensitive;
291 saved_case_preserve = conn->case_preserve;
292 saved_short_case_preserve = conn->short_case_preserve;
295 conn->case_sensitive = True;
296 conn->case_preserve = True;
297 conn->short_case_preserve = True;
300 /****************************************************************************
301 Restore case semantics.
302 ****************************************************************************/
304 static void restore_case_semantics(connection_struct *conn, uint32 file_attributes)
306 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
310 conn->case_sensitive = saved_case_sensitive;
311 conn->case_preserve = saved_case_preserve;
312 conn->short_case_preserve = saved_short_case_preserve;
315 /****************************************************************************
316 Reply to an NT create and X call on a pipe.
317 ****************************************************************************/
319 static int nt_open_pipe(char *fname, connection_struct *conn,
320 char *inbuf, char *outbuf, int *ppnum)
322 smb_np_struct *p = NULL;
323 uint16 vuid = SVAL(inbuf, smb_uid);
326 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
328 /* See if it is one we want to handle. */
330 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
331 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
334 for( i = 0; known_nt_pipes[i]; i++ ) {
335 if( strequal(fname,known_nt_pipes[i])) {
340 if ( known_nt_pipes[i] == NULL ) {
341 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
344 /* Strip \\ off the name. */
347 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
349 p = open_rpc_pipe_p(fname, conn, vuid);
351 return(ERROR_DOS(ERRSRV,ERRnofids));
354 /* TODO: Add pipe to db */
356 if ( !store_pipe_opendb( p ) ) {
357 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
364 /****************************************************************************
365 Reply to an NT create and X call for pipes.
366 ****************************************************************************/
368 static int do_ntcreate_pipe_open(connection_struct *conn,
369 char *inbuf,char *outbuf,int length,int bufsize)
376 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
378 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0) {
383 * Deal with pipe return.
386 set_message(outbuf,34,0,True);
388 p = outbuf + smb_vwv2;
392 SIVAL(p,0,FILE_WAS_OPENED);
395 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
398 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
400 SSVAL(p,2, 0x5FF); /* ? */
402 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
404 return chain_reply(inbuf,outbuf,length,bufsize);
407 /****************************************************************************
408 Reply to an NT create and X call for a quota file.
409 ****************************************************************************/
411 int reply_ntcreate_and_X_quota(connection_struct *conn,
416 enum FAKE_FILE_TYPE fake_file_type,
421 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
425 status = open_fake_file(conn, fake_file_type, fname, desired_access,
428 if (!NT_STATUS_IS_OK(status)) {
429 return ERROR_NT(status);
432 set_message(outbuf,34,0,True);
434 p = outbuf + smb_vwv2;
436 /* SCVAL(p,0,NO_OPLOCK_RETURN); */
438 SSVAL(p,0,fsp->fnum);
440 DEBUG(5,("reply_ntcreate_and_X_quota: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
442 result = chain_reply(inbuf,outbuf,length,bufsize);
446 /****************************************************************************
447 Reply to an NT create and X call.
448 ****************************************************************************/
450 int reply_ntcreate_and_X(connection_struct *conn,
451 char *inbuf,char *outbuf,int length,int bufsize)
455 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
456 uint32 access_mask = IVAL(inbuf,smb_ntcreate_DesiredAccess);
457 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
458 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
459 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
460 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
461 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
462 /* Breakout the oplock request bits so we can set the
463 reply bits separately. */
464 int oplock_request = 0;
466 SMB_OFF_T file_len = 0;
467 SMB_STRUCT_STAT sbuf;
469 files_struct *fsp=NULL;
471 struct timespec c_timespec;
472 struct timespec a_timespec;
473 struct timespec m_timespec;
474 BOOL extended_oplock_granted = False;
477 START_PROFILE(SMBntcreateX);
479 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
480 "file_attributes = 0x%x, share_access = 0x%x, "
481 "create_disposition = 0x%x create_options = 0x%x "
482 "root_dir_fid = 0x%x\n",
484 (unsigned int)access_mask,
485 (unsigned int)file_attributes,
486 (unsigned int)share_access,
487 (unsigned int)create_disposition,
488 (unsigned int)create_options,
489 (unsigned int)root_dir_fid ));
491 /* If it's an IPC, use the pipe handler. */
494 if (lp_nt_pipe_support()) {
495 END_PROFILE(SMBntcreateX);
496 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
498 END_PROFILE(SMBntcreateX);
499 return(ERROR_DOS(ERRDOS,ERRnoaccess));
503 if (create_options & FILE_OPEN_BY_FILE_ID) {
504 END_PROFILE(SMBntcreateX);
505 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
512 if(root_dir_fid != 0) {
514 * This filename is relative to a directory fid.
517 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
521 END_PROFILE(SMBntcreateX);
522 return(ERROR_DOS(ERRDOS,ERRbadfid));
525 if(!dir_fsp->is_directory) {
527 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
528 if (!NT_STATUS_IS_OK(status)) {
529 END_PROFILE(SMBntcreateX);
530 return ERROR_NT(status);
534 * Check to see if this is a mac fork of some kind.
537 if( is_ntfs_stream_name(fname)) {
538 END_PROFILE(SMBntcreateX);
539 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
543 we need to handle the case when we get a
544 relative open relative to a file and the
545 pathname is blank - this is a reopen!
546 (hint from demyn plantenberg)
549 END_PROFILE(SMBntcreateX);
550 return(ERROR_DOS(ERRDOS,ERRbadfid));
554 * Copy in the base directory name.
557 pstrcpy( fname, dir_fsp->fsp_name );
558 dir_name_len = strlen(fname);
561 * Ensure it ends in a '\'.
564 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
569 srvstr_get_path(inbuf, rel_fname, smb_buf(inbuf), sizeof(rel_fname), 0, STR_TERMINATE, &status);
570 if (!NT_STATUS_IS_OK(status)) {
571 END_PROFILE(SMBntcreateX);
572 return ERROR_NT(status);
574 pstrcat(fname, rel_fname);
576 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
577 if (!NT_STATUS_IS_OK(status)) {
578 END_PROFILE(SMBntcreateX);
579 return ERROR_NT(status);
583 * Check to see if this is a mac fork of some kind.
586 if( is_ntfs_stream_name(fname)) {
587 enum FAKE_FILE_TYPE fake_file_type = is_fake_file(fname);
588 if (fake_file_type!=FAKE_FILE_TYPE_NONE) {
590 * Here we go! support for changing the disk quotas --metze
592 * We need to fake up to open this MAGIC QUOTA file
593 * and return a valid FID.
595 * w2k close this file directly after openening
596 * xp also tries a QUERY_FILE_INFO on the file and then close it
598 result = reply_ntcreate_and_X_quota(conn, inbuf, outbuf, length, bufsize,
599 fake_file_type, fname);
600 END_PROFILE(SMBntcreateX);
603 END_PROFILE(SMBntcreateX);
604 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
610 * Now contruct the smb_open_mode value from the filename,
611 * desired access and the share access.
613 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
615 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
616 if (oplock_request) {
617 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
621 * Ordinary file or directory.
625 * Check if POSIX semantics are wanted.
628 set_posix_case_semantics(conn, file_attributes);
630 status = unix_convert(conn, fname, False, NULL, &sbuf);
631 if (!NT_STATUS_IS_OK(status)) {
632 restore_case_semantics(conn, file_attributes);
633 END_PROFILE(SMBntcreateX);
634 return ERROR_NT(status);
636 /* All file access must go through check_name() */
637 status = check_name(conn, fname);
638 if (!NT_STATUS_IS_OK(status)) {
639 restore_case_semantics(conn, file_attributes);
640 END_PROFILE(SMBntcreateX);
641 return ERROR_NT(status);
644 /* This is the correct thing to do (check every time) but can_delete is
645 expensive (it may have to read the parent directory permissions). So
646 for now we're not doing it unless we have a strong hint the client
647 is really going to delete this file. If the client is forcing FILE_CREATE
648 let the filesystem take care of the permissions. */
650 /* Setting FILE_SHARE_DELETE is the hint. */
652 if (lp_acl_check_permissions(SNUM(conn))
653 && (create_disposition != FILE_CREATE)
654 && (share_access & FILE_SHARE_DELETE)
655 && (access_mask & DELETE_ACCESS)
656 && !can_delete_file_in_directory(conn, fname)) {
657 restore_case_semantics(conn, file_attributes);
658 END_PROFILE(SMBntcreateX);
659 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
663 * If it's a request for a directory open, deal with it separately.
666 if(create_options & FILE_DIRECTORY_FILE) {
669 /* Can't open a temp directory. IFS kit test. */
670 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
671 END_PROFILE(SMBntcreateX);
672 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
675 status = open_directory(conn, fname, &sbuf,
682 restore_case_semantics(conn, file_attributes);
684 if(!NT_STATUS_IS_OK(status)) {
685 if (!use_nt_status() && NT_STATUS_EQUAL(
686 status, NT_STATUS_OBJECT_NAME_COLLISION)) {
687 status = NT_STATUS_DOS(ERRDOS, ERRfilexists);
689 END_PROFILE(SMBntcreateX);
690 return ERROR_NT(status);
694 * Ordinary file case.
697 /* NB. We have a potential bug here. If we
698 * cause an oplock break to ourselves, then we
699 * could end up processing filename related
700 * SMB requests whilst we await the oplock
701 * break response. As we may have changed the
702 * filename case semantics to be POSIX-like,
703 * this could mean a filename request could
704 * fail when it should succeed. This is a rare
705 * condition, but eventually we must arrange
706 * to restore the correct case semantics
707 * before issuing an oplock break request to
708 * our client. JRA. */
710 status = open_file_ntcreate(conn,fname,&sbuf,
718 if (!NT_STATUS_IS_OK(status)) {
719 /* We cheat here. There are two cases we
720 * care about. One is a directory rename,
721 * where the NT client will attempt to
722 * open the source directory for
723 * DELETE access. Note that when the
724 * NT client does this it does *not*
725 * set the directory bit in the
726 * request packet. This is translated
727 * into a read/write open
728 * request. POSIX states that any open
729 * for write request on a directory
730 * will generate an EISDIR error, so
731 * we can catch this here and open a
732 * pseudo handle that is flagged as a
733 * directory. The second is an open
734 * for a permissions read only, which
735 * we handle in the open_file_stat case. JRA.
738 if (NT_STATUS_EQUAL(status,
739 NT_STATUS_FILE_IS_A_DIRECTORY)) {
742 * Fail the open if it was explicitly a non-directory file.
745 if (create_options & FILE_NON_DIRECTORY_FILE) {
746 restore_case_semantics(conn, file_attributes);
747 END_PROFILE(SMBntcreateX);
748 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
752 status = open_directory(conn, fname, &sbuf,
759 if(!NT_STATUS_IS_OK(status)) {
760 restore_case_semantics(conn, file_attributes);
761 if (!use_nt_status() && NT_STATUS_EQUAL(
762 status, NT_STATUS_OBJECT_NAME_COLLISION)) {
763 status = NT_STATUS_DOS(ERRDOS, ERRfilexists);
765 END_PROFILE(SMBntcreateX);
766 return ERROR_NT(status);
770 restore_case_semantics(conn, file_attributes);
771 END_PROFILE(SMBntcreateX);
772 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
773 /* We have re-scheduled this call. */
776 return ERROR_NT(status);
781 restore_case_semantics(conn, file_attributes);
783 file_len = sbuf.st_size;
784 fattr = dos_mode(conn,fname,&sbuf);
786 fattr = FILE_ATTRIBUTE_NORMAL;
788 if (!fsp->is_directory && (fattr & aDIR)) {
789 close_file(fsp,ERROR_CLOSE);
790 END_PROFILE(SMBntcreateX);
791 return ERROR_DOS(ERRDOS,ERRnoaccess);
794 /* Save the requested allocation size. */
795 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
796 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize);
797 #ifdef LARGE_SMB_OFF_T
798 allocation_size |= (((SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
800 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
801 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
802 if (fsp->is_directory) {
803 close_file(fsp,ERROR_CLOSE);
804 END_PROFILE(SMBntcreateX);
805 /* Can't set allocation size on a directory. */
806 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
808 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
809 close_file(fsp,ERROR_CLOSE);
810 END_PROFILE(SMBntcreateX);
811 return ERROR_NT(NT_STATUS_DISK_FULL);
814 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
819 * If the caller set the extended oplock request bit
820 * and we granted one (by whatever means) - set the
821 * correct bit for extended oplock reply.
824 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
825 extended_oplock_granted = True;
828 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
829 extended_oplock_granted = True;
833 /* W2K sends back 42 words here ! If we do the same it breaks offline sync. Go figure... ? JRA. */
834 set_message(outbuf,42,0,True);
836 set_message(outbuf,34,0,True);
839 p = outbuf + smb_vwv2;
842 * Currently as we don't support level II oplocks we just report
843 * exclusive & batch here.
846 if (extended_oplock_granted) {
847 if (flags & REQUEST_BATCH_OPLOCK) {
848 SCVAL(p,0, BATCH_OPLOCK_RETURN);
850 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
852 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
853 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
855 SCVAL(p,0,NO_OPLOCK_RETURN);
859 SSVAL(p,0,fsp->fnum);
861 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
862 SIVAL(p,0,FILE_WAS_SUPERSEDED);
869 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
870 a_timespec = get_atimespec(&sbuf);
871 m_timespec = get_mtimespec(&sbuf);
873 if (lp_dos_filetime_resolution(SNUM(conn))) {
874 dos_filetime_timespec(&c_timespec);
875 dos_filetime_timespec(&a_timespec);
876 dos_filetime_timespec(&m_timespec);
879 put_long_date_timespec(p, c_timespec);
881 put_long_date_timespec(p, a_timespec); /* access time */
883 put_long_date_timespec(p, m_timespec); /* write time */
885 put_long_date_timespec(p, m_timespec); /* change time */
887 SIVAL(p,0,fattr); /* File Attributes. */
889 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
891 SOFF_T(p,0,file_len);
893 if (flags & EXTENDED_RESPONSE_REQUIRED) {
897 SCVAL(p,0,fsp->is_directory ? 1 : 0);
899 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
901 result = chain_reply(inbuf,outbuf,length,bufsize);
902 END_PROFILE(SMBntcreateX);
906 /****************************************************************************
907 Reply to a NT_TRANSACT_CREATE call to open a pipe.
908 ****************************************************************************/
910 static int do_nt_transact_create_pipe( connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
911 uint16 **ppsetup, uint32 setup_count,
912 char **ppparams, uint32 parameter_count,
913 char **ppdata, uint32 data_count)
916 char *params = *ppparams;
923 * Ensure minimum number of parameters sent.
926 if(parameter_count < 54) {
927 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
928 return ERROR_DOS(ERRDOS,ERRnoaccess);
931 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
932 if (!NT_STATUS_IS_OK(status)) {
933 return ERROR_NT(status);
936 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0) {
940 /* Realloc the size of parameters and data we will return */
941 params = nttrans_realloc(ppparams, 69);
943 return ERROR_DOS(ERRDOS,ERRnomem);
947 SCVAL(p,0,NO_OPLOCK_RETURN);
952 SIVAL(p,0,FILE_WAS_OPENED);
956 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
959 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
961 SSVAL(p,2, 0x5FF); /* ? */
963 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
965 /* Send the required number of replies */
966 send_nt_replies(outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
971 /****************************************************************************
972 Internal fn to set security descriptors.
973 ****************************************************************************/
975 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
978 SEC_DESC *psd = NULL;
982 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
987 * Init the parse struct we will unmarshall from.
990 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
991 DEBUG(0,("set_sd: talloc_init failed.\n"));
992 return NT_STATUS_NO_MEMORY;
995 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
998 * Setup the prs_struct to point at the memory we just
1002 prs_give_memory( &pd, data, sd_len, False);
1005 * Finally, unmarshall from the data buffer.
1008 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1009 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1011 * Return access denied for want of a better error message..
1013 talloc_destroy(mem_ctx);
1014 return NT_STATUS_NO_MEMORY;
1017 if (psd->owner_sid==0) {
1018 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1020 if (psd->group_sid==0) {
1021 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1024 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1027 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1030 ret = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1033 talloc_destroy(mem_ctx);
1034 return NT_STATUS_ACCESS_DENIED;
1037 talloc_destroy(mem_ctx);
1039 return NT_STATUS_OK;
1042 /****************************************************************************
1043 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1044 ****************************************************************************/
1046 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1048 struct ea_list *ea_list_head = NULL;
1051 if (data_size < 4) {
1055 while (offset + 4 <= data_size) {
1056 size_t next_offset = IVAL(pdata,offset);
1057 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1063 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1064 if (next_offset == 0) {
1067 offset += next_offset;
1070 return ea_list_head;
1073 /****************************************************************************
1074 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1075 ****************************************************************************/
1077 static int call_nt_transact_create(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1078 uint16 **ppsetup, uint32 setup_count,
1079 char **ppparams, uint32 parameter_count,
1080 char **ppdata, uint32 data_count, uint32 max_data_count)
1083 char *params = *ppparams;
1084 char *data = *ppdata;
1085 /* Breakout the oplock request bits so we can set the reply bits separately. */
1086 int oplock_request = 0;
1088 SMB_OFF_T file_len = 0;
1089 SMB_STRUCT_STAT sbuf;
1091 files_struct *fsp = NULL;
1093 BOOL extended_oplock_granted = False;
1096 uint32 file_attributes;
1097 uint32 share_access;
1098 uint32 create_disposition;
1099 uint32 create_options;
1102 uint16 root_dir_fid;
1103 struct timespec c_timespec;
1104 struct timespec a_timespec;
1105 struct timespec m_timespec;
1106 struct ea_list *ea_list = NULL;
1107 TALLOC_CTX *ctx = NULL;
1111 DEBUG(5,("call_nt_transact_create\n"));
1114 * If it's an IPC, use the pipe handler.
1118 if (lp_nt_pipe_support()) {
1119 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1121 ppsetup, setup_count,
1122 ppparams, parameter_count,
1123 ppdata, data_count);
1125 return ERROR_DOS(ERRDOS,ERRnoaccess);
1130 * Ensure minimum number of parameters sent.
1133 if(parameter_count < 54) {
1134 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1135 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1138 flags = IVAL(params,0);
1139 access_mask = IVAL(params,8);
1140 file_attributes = IVAL(params,20);
1141 share_access = IVAL(params,24);
1142 create_disposition = IVAL(params,28);
1143 create_options = IVAL(params,32);
1144 sd_len = IVAL(params,36);
1145 ea_len = IVAL(params,40);
1146 root_dir_fid = (uint16)IVAL(params,4);
1148 /* Ensure the data_len is correct for the sd and ea values given. */
1149 if ((ea_len + sd_len > data_count) ||
1150 (ea_len > data_count) || (sd_len > data_count) ||
1151 (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1152 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1153 (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1154 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1158 if (!lp_ea_support(SNUM(conn))) {
1159 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1160 (unsigned int)ea_len ));
1161 return ERROR_NT(NT_STATUS_EAS_NOT_SUPPORTED);
1165 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1166 (unsigned int)ea_len ));
1167 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1171 if (create_options & FILE_OPEN_BY_FILE_ID) {
1172 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1176 * Get the file name.
1179 if(root_dir_fid != 0) {
1181 * This filename is relative to a directory fid.
1183 files_struct *dir_fsp = file_fsp(params,4);
1184 size_t dir_name_len;
1187 return ERROR_DOS(ERRDOS,ERRbadfid);
1190 if(!dir_fsp->is_directory) {
1191 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1192 if (!NT_STATUS_IS_OK(status)) {
1193 return ERROR_NT(status);
1197 * Check to see if this is a mac fork of some kind.
1200 if( is_ntfs_stream_name(fname)) {
1201 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1204 return ERROR_DOS(ERRDOS,ERRbadfid);
1208 * Copy in the base directory name.
1211 pstrcpy( fname, dir_fsp->fsp_name );
1212 dir_name_len = strlen(fname);
1215 * Ensure it ends in a '\'.
1218 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1219 pstrcat(fname, "/");
1225 srvstr_get_path(inbuf, tmpname, params+53, sizeof(tmpname), parameter_count-53, STR_TERMINATE, &status);
1226 if (!NT_STATUS_IS_OK(status)) {
1227 return ERROR_NT(status);
1229 pstrcat(fname, tmpname);
1232 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1233 if (!NT_STATUS_IS_OK(status)) {
1234 return ERROR_NT(status);
1238 * Check to see if this is a mac fork of some kind.
1241 if( is_ntfs_stream_name(fname)) {
1242 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1246 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1247 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1250 * Check if POSIX semantics are wanted.
1253 set_posix_case_semantics(conn, file_attributes);
1255 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1257 status = unix_convert(conn, fname, False, NULL, &sbuf);
1258 if (!NT_STATUS_IS_OK(status)) {
1259 restore_case_semantics(conn, file_attributes);
1260 return ERROR_NT(status);
1262 /* All file access must go through check_name() */
1263 status = check_name(conn, fname);
1264 if (!NT_STATUS_IS_OK(status)) {
1265 restore_case_semantics(conn, file_attributes);
1266 return ERROR_NT(status);
1269 /* This is the correct thing to do (check every time) but can_delete is
1270 expensive (it may have to read the parent directory permissions). So
1271 for now we're not doing it unless we have a strong hint the client
1272 is really going to delete this file. If the client is forcing FILE_CREATE
1273 let the filesystem take care of the permissions. */
1275 /* Setting FILE_SHARE_DELETE is the hint. */
1277 if (lp_acl_check_permissions(SNUM(conn))
1278 && (create_disposition != FILE_CREATE)
1279 && (share_access & FILE_SHARE_DELETE)
1280 && (access_mask & DELETE_ACCESS)
1281 && !can_delete_file_in_directory(conn, fname)) {
1282 restore_case_semantics(conn, file_attributes);
1283 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1287 pdata = data + sd_len;
1289 /* We have already checked that ea_len <= data_count here. */
1290 ea_list = read_nttrans_ea_list(tmp_talloc_ctx(), pdata,
1293 restore_case_semantics(conn, file_attributes);
1294 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1299 * If it's a request for a directory open, deal with it separately.
1302 if(create_options & FILE_DIRECTORY_FILE) {
1304 /* Can't open a temp directory. IFS kit test. */
1305 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1306 restore_case_semantics(conn, file_attributes);
1307 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1313 * We will get a create directory here if the Win32
1314 * app specified a security descriptor in the
1315 * CreateDirectory() call.
1318 status = open_directory(conn, fname, &sbuf,
1324 if(!NT_STATUS_IS_OK(status)) {
1325 restore_case_semantics(conn, file_attributes);
1326 return ERROR_NT(status);
1332 * Ordinary file case.
1335 status = open_file_ntcreate(conn,fname,&sbuf,
1344 if (!NT_STATUS_IS_OK(status)) {
1345 if (NT_STATUS_EQUAL(status,
1346 NT_STATUS_FILE_IS_A_DIRECTORY)) {
1349 * Fail the open if it was explicitly a non-directory file.
1352 if (create_options & FILE_NON_DIRECTORY_FILE) {
1353 restore_case_semantics(conn, file_attributes);
1354 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1358 status = open_directory(conn, fname, &sbuf,
1364 if(!NT_STATUS_IS_OK(status)) {
1365 restore_case_semantics(conn, file_attributes);
1366 return ERROR_NT(status);
1369 restore_case_semantics(conn, file_attributes);
1370 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1371 /* We have re-scheduled this call. */
1374 return ERROR_NT(status);
1380 * According to the MS documentation, the only time the security
1381 * descriptor is applied to the opened file is iff we *created* the
1382 * file; an existing file stays the same.
1384 * Also, it seems (from observation) that you can open the file with
1385 * any access mask but you can still write the sd. We need to override
1386 * the granted access before we call set_sd
1387 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1390 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1391 uint32 saved_access_mask = fsp->access_mask;
1393 /* We have already checked that sd_len <= data_count here. */
1395 fsp->access_mask = FILE_GENERIC_ALL;
1397 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1398 if (!NT_STATUS_IS_OK(status)) {
1399 talloc_destroy(ctx);
1400 close_file(fsp,ERROR_CLOSE);
1401 restore_case_semantics(conn, file_attributes);
1402 return ERROR_NT(status);
1404 fsp->access_mask = saved_access_mask;
1407 if (ea_len && (info == FILE_WAS_CREATED)) {
1408 status = set_ea(conn, fsp, fname, ea_list);
1409 if (!NT_STATUS_IS_OK(status)) {
1410 close_file(fsp,ERROR_CLOSE);
1411 restore_case_semantics(conn, file_attributes);
1412 return ERROR_NT(status);
1416 restore_case_semantics(conn, file_attributes);
1418 file_len = sbuf.st_size;
1419 fattr = dos_mode(conn,fname,&sbuf);
1421 fattr = FILE_ATTRIBUTE_NORMAL;
1423 if (!fsp->is_directory && (fattr & aDIR)) {
1424 close_file(fsp,ERROR_CLOSE);
1425 return ERROR_DOS(ERRDOS,ERRnoaccess);
1428 /* Save the requested allocation size. */
1429 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1430 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1431 #ifdef LARGE_SMB_OFF_T
1432 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1434 if (allocation_size && (allocation_size > file_len)) {
1435 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1436 if (fsp->is_directory) {
1437 close_file(fsp,ERROR_CLOSE);
1438 /* Can't set allocation size on a directory. */
1439 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1441 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1442 close_file(fsp,ERROR_CLOSE);
1443 return ERROR_NT(NT_STATUS_DISK_FULL);
1446 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1451 * If the caller set the extended oplock request bit
1452 * and we granted one (by whatever means) - set the
1453 * correct bit for extended oplock reply.
1456 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1457 extended_oplock_granted = True;
1460 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1461 extended_oplock_granted = True;
1464 /* Realloc the size of parameters and data we will return */
1465 params = nttrans_realloc(ppparams, 69);
1466 if(params == NULL) {
1467 return ERROR_DOS(ERRDOS,ERRnomem);
1471 if (extended_oplock_granted) {
1472 if (flags & REQUEST_BATCH_OPLOCK) {
1473 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1475 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1477 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1478 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1480 SCVAL(p,0,NO_OPLOCK_RETURN);
1484 SSVAL(p,0,fsp->fnum);
1486 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1487 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1494 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1495 a_timespec = get_atimespec(&sbuf);
1496 m_timespec = get_mtimespec(&sbuf);
1498 if (lp_dos_filetime_resolution(SNUM(conn))) {
1499 dos_filetime_timespec(&c_timespec);
1500 dos_filetime_timespec(&a_timespec);
1501 dos_filetime_timespec(&m_timespec);
1504 put_long_date_timespec(p, c_timespec); /* create time. */
1506 put_long_date_timespec(p, a_timespec); /* access time */
1508 put_long_date_timespec(p, m_timespec); /* write time */
1510 put_long_date_timespec(p, m_timespec); /* change time */
1512 SIVAL(p,0,fattr); /* File Attributes. */
1514 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1516 SOFF_T(p,0,file_len);
1518 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1522 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1524 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1526 /* Send the required number of replies */
1527 send_nt_replies(outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1532 /****************************************************************************
1533 Reply to a NT CANCEL request.
1534 conn POINTER CAN BE NULL HERE !
1535 ****************************************************************************/
1537 int reply_ntcancel(connection_struct *conn,
1538 char *inbuf,char *outbuf,int length,int bufsize)
1541 * Go through and cancel any pending change notifies.
1544 int mid = SVAL(inbuf,smb_mid);
1545 START_PROFILE(SMBntcancel);
1546 remove_pending_change_notify_requests_by_mid(mid);
1547 remove_pending_lock_requests_by_mid(mid);
1548 srv_cancel_sign_response(mid);
1550 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1552 END_PROFILE(SMBntcancel);
1556 /****************************************************************************
1558 ****************************************************************************/
1560 static NTSTATUS copy_internals(connection_struct *conn, char *oldname, char *newname, uint32 attrs)
1562 SMB_STRUCT_STAT sbuf1, sbuf2;
1563 pstring last_component_oldname;
1564 pstring last_component_newname;
1565 files_struct *fsp1,*fsp2;
1570 NTSTATUS status = NT_STATUS_OK;
1575 if (!CAN_WRITE(conn)) {
1576 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1579 status = unix_convert(conn, oldname, False, last_component_oldname, &sbuf1);
1580 if (!NT_STATUS_IS_OK(status)) {
1584 /* Source must already exist. */
1585 if (!VALID_STAT(sbuf1)) {
1586 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1588 status = check_name(conn, oldname);
1589 if (!NT_STATUS_IS_OK(status)) {
1593 /* Ensure attributes match. */
1594 fattr = dos_mode(conn,oldname,&sbuf1);
1595 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1596 return NT_STATUS_NO_SUCH_FILE;
1599 status = unix_convert(conn, newname, False, last_component_newname, &sbuf2);
1600 if (!NT_STATUS_IS_OK(status)) {
1604 /* Disallow if newname already exists. */
1605 if (VALID_STAT(sbuf2)) {
1606 return NT_STATUS_OBJECT_NAME_COLLISION;
1609 status = check_name(conn, newname);
1610 if (!NT_STATUS_IS_OK(status)) {
1614 /* No links from a directory. */
1615 if (S_ISDIR(sbuf1.st_mode)) {
1616 return NT_STATUS_FILE_IS_A_DIRECTORY;
1619 /* Ensure this is within the share. */
1620 status = reduce_name(conn, oldname);
1621 if (!NT_STATUS_IS_OK(status)) {
1625 DEBUG(10,("copy_internals: doing file copy %s to %s\n", oldname, newname));
1627 status = open_file_ntcreate(conn,oldname,&sbuf1,
1628 FILE_READ_DATA, /* Read-only. */
1629 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1631 0, /* No create options. */
1632 FILE_ATTRIBUTE_NORMAL,
1636 if (!NT_STATUS_IS_OK(status)) {
1640 status = open_file_ntcreate(conn,newname,&sbuf2,
1641 FILE_WRITE_DATA, /* Read-only. */
1642 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1644 0, /* No create options. */
1649 if (!NT_STATUS_IS_OK(status)) {
1650 close_file(fsp1,ERROR_CLOSE);
1654 if (sbuf1.st_size) {
1655 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1659 * As we are opening fsp1 read-only we only expect
1660 * an error on close on fsp2 if we are out of space.
1661 * Thus we don't look at the error return from the
1664 close_file(fsp1,NORMAL_CLOSE);
1666 /* Ensure the modtime is set correctly on the destination file. */
1667 fsp_set_pending_modtime(fsp2, sbuf1.st_mtime);
1669 close_ret = close_file(fsp2,NORMAL_CLOSE);
1671 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1672 creates the file. This isn't the correct thing to do in the copy
1674 file_set_dosmode(conn, newname, fattr, &sbuf2,
1675 parent_dirname(newname));
1677 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1678 return NT_STATUS_DISK_FULL;
1681 if (close_ret != 0) {
1682 status = map_nt_error_from_unix(close_ret);
1683 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1684 nt_errstr(status), oldname, newname));
1689 /****************************************************************************
1690 Reply to a NT rename request.
1691 ****************************************************************************/
1693 int reply_ntrename(connection_struct *conn,
1694 char *inbuf,char *outbuf,int length,int bufsize)
1701 BOOL path1_contains_wcard = False;
1702 BOOL path2_contains_wcard = False;
1703 uint32 attrs = SVAL(inbuf,smb_vwv0);
1704 uint16 rename_type = SVAL(inbuf,smb_vwv1);
1706 START_PROFILE(SMBntrename);
1708 p = smb_buf(inbuf) + 1;
1709 p += srvstr_get_path_wcard(inbuf, oldname, p, sizeof(oldname), 0, STR_TERMINATE, &status, &path1_contains_wcard);
1710 if (!NT_STATUS_IS_OK(status)) {
1711 END_PROFILE(SMBntrename);
1712 return ERROR_NT(status);
1715 if( is_ntfs_stream_name(oldname)) {
1716 /* Can't rename a stream. */
1717 END_PROFILE(SMBntrename);
1718 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1721 if (ms_has_wild(oldname)) {
1722 END_PROFILE(SMBntrename);
1723 return ERROR_NT(NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1727 p += srvstr_get_path_wcard(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, &path2_contains_wcard);
1728 if (!NT_STATUS_IS_OK(status)) {
1729 END_PROFILE(SMBntrename);
1730 return ERROR_NT(status);
1733 RESOLVE_DFSPATH(oldname, conn, inbuf, outbuf);
1734 RESOLVE_DFSPATH(newname, conn, inbuf, outbuf);
1736 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1738 switch(rename_type) {
1739 case RENAME_FLAG_RENAME:
1740 status = rename_internals(conn, oldname, newname, attrs, False, path1_contains_wcard);
1742 case RENAME_FLAG_HARD_LINK:
1743 if (path1_contains_wcard || path2_contains_wcard) {
1745 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1747 status = hardlink_internals(conn, oldname, newname);
1750 case RENAME_FLAG_COPY:
1751 if (path1_contains_wcard || path2_contains_wcard) {
1753 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1755 status = copy_internals(conn, oldname, newname, attrs);
1758 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1759 status = NT_STATUS_INVALID_PARAMETER;
1762 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1766 if (!NT_STATUS_IS_OK(status)) {
1767 END_PROFILE(SMBntrename);
1768 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1769 /* We have re-scheduled this call. */
1772 return ERROR_NT(status);
1776 * Win2k needs a changenotify request response before it will
1777 * update after a rename..
1779 process_pending_change_notify_queue((time_t)0);
1780 outsize = set_message(outbuf,0,0,False);
1782 END_PROFILE(SMBntrename);
1786 /****************************************************************************
1787 Reply to a notify change - queue the request and
1788 don't allow a directory to be opened.
1789 ****************************************************************************/
1791 static int call_nt_transact_notify_change(connection_struct *conn, char *inbuf,
1792 char *outbuf, int length,
1794 uint16 **ppsetup, uint32 setup_count,
1796 uint32 parameter_count,
1797 char **ppdata, uint32 data_count,
1798 uint32 max_data_count,
1799 uint32 max_param_count)
1801 uint16 *setup = *ppsetup;
1805 if(setup_count < 6) {
1806 return ERROR_DOS(ERRDOS,ERRbadfunc);
1809 fsp = file_fsp((char *)setup,4);
1810 flags = IVAL(setup, 0);
1812 DEBUG(3,("call_nt_transact_notify_change\n"));
1815 return ERROR_DOS(ERRDOS,ERRbadfid);
1818 if((!fsp->is_directory) || (conn != fsp->conn)) {
1819 return ERROR_DOS(ERRDOS,ERRbadfid);
1822 if (!change_notify_set(inbuf, fsp, conn, flags)) {
1823 return(UNIXERROR(ERRDOS,ERRbadfid));
1826 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1827 name = %s\n", fsp->fsp_name ));
1832 /****************************************************************************
1833 Reply to an NT transact rename command.
1834 ****************************************************************************/
1836 static int call_nt_transact_rename(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1837 uint16 **ppsetup, uint32 setup_count,
1838 char **ppparams, uint32 parameter_count,
1839 char **ppdata, uint32 data_count, uint32 max_data_count)
1841 char *params = *ppparams;
1843 files_struct *fsp = NULL;
1844 BOOL replace_if_exists = False;
1845 BOOL path_contains_wcard = False;
1848 if(parameter_count < 5) {
1849 return ERROR_DOS(ERRDOS,ERRbadfunc);
1852 fsp = file_fsp(params, 0);
1853 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1854 CHECK_FSP(fsp, conn);
1855 srvstr_get_path_wcard(inbuf, new_name, params+4, sizeof(new_name), parameter_count - 4, STR_TERMINATE, &status, &path_contains_wcard);
1856 if (!NT_STATUS_IS_OK(status)) {
1857 return ERROR_NT(status);
1860 status = rename_internals(conn, fsp->fsp_name,
1861 new_name, 0, replace_if_exists, path_contains_wcard);
1863 if (!NT_STATUS_IS_OK(status)) {
1864 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1865 /* We have re-scheduled this call. */
1868 return ERROR_NT(status);
1872 * Rename was successful.
1874 send_nt_replies(outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1876 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1877 fsp->fsp_name, new_name));
1880 * Win2k needs a changenotify request response before it will
1881 * update after a rename..
1884 process_pending_change_notify_queue((time_t)0);
1889 /******************************************************************************
1890 Fake up a completely empty SD.
1891 *******************************************************************************/
1893 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1897 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1899 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1906 /****************************************************************************
1907 Reply to query a security descriptor.
1908 ****************************************************************************/
1910 static int call_nt_transact_query_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1911 uint16 **ppsetup, uint32 setup_count,
1912 char **ppparams, uint32 parameter_count,
1913 char **ppdata, uint32 data_count, uint32 max_data_count)
1915 char *params = *ppparams;
1916 char *data = *ppdata;
1918 SEC_DESC *psd = NULL;
1920 uint32 security_info_wanted;
1921 TALLOC_CTX *mem_ctx;
1922 files_struct *fsp = NULL;
1924 if(parameter_count < 8) {
1925 return ERROR_DOS(ERRDOS,ERRbadfunc);
1928 fsp = file_fsp(params,0);
1930 return ERROR_DOS(ERRDOS,ERRbadfid);
1933 security_info_wanted = IVAL(params,4);
1935 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1936 (unsigned int)security_info_wanted ));
1938 params = nttrans_realloc(ppparams, 4);
1939 if(params == NULL) {
1940 return ERROR_DOS(ERRDOS,ERRnomem);
1943 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
1944 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1945 return ERROR_DOS(ERRDOS,ERRnomem);
1949 * Get the permissions to return.
1952 if (!lp_nt_acl_support(SNUM(conn))) {
1953 sd_size = get_null_nt_acl(mem_ctx, &psd);
1955 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
1959 talloc_destroy(mem_ctx);
1960 return(UNIXERROR(ERRDOS,ERRnoaccess));
1963 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1965 SIVAL(params,0,(uint32)sd_size);
1967 if(max_data_count < sd_size) {
1969 send_nt_replies(outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1970 params, 4, *ppdata, 0);
1971 talloc_destroy(mem_ctx);
1976 * Allocate the data we will point this at.
1979 data = nttrans_realloc(ppdata, sd_size);
1981 talloc_destroy(mem_ctx);
1982 return ERROR_DOS(ERRDOS,ERRnomem);
1986 * Init the parse struct we will marshall into.
1989 prs_init(&pd, 0, mem_ctx, MARSHALL);
1992 * Setup the prs_struct to point at the memory we just
1996 prs_give_memory( &pd, data, (uint32)sd_size, False);
1999 * Finally, linearize into the outgoing buffer.
2002 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2003 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2004 security descriptor.\n"));
2006 * Return access denied for want of a better error message..
2008 talloc_destroy(mem_ctx);
2009 return(UNIXERROR(ERRDOS,ERRnoaccess));
2013 * Now we can delete the security descriptor.
2016 talloc_destroy(mem_ctx);
2018 send_nt_replies(outbuf, bufsize, NT_STATUS_OK, params, 4, data,
2023 /****************************************************************************
2024 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2025 ****************************************************************************/
2027 static int call_nt_transact_set_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2028 uint16 **ppsetup, uint32 setup_count,
2029 char **ppparams, uint32 parameter_count,
2030 char **ppdata, uint32 data_count, uint32 max_data_count)
2032 char *params= *ppparams;
2033 char *data = *ppdata;
2034 files_struct *fsp = NULL;
2035 uint32 security_info_sent = 0;
2038 if(parameter_count < 8) {
2039 return ERROR_DOS(ERRDOS,ERRbadfunc);
2042 if((fsp = file_fsp(params,0)) == NULL) {
2043 return ERROR_DOS(ERRDOS,ERRbadfid);
2046 if(!lp_nt_acl_support(SNUM(conn))) {
2050 security_info_sent = IVAL(params,4);
2052 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2053 (unsigned int)security_info_sent ));
2055 if (data_count == 0) {
2056 return ERROR_DOS(ERRDOS, ERRnoaccess);
2059 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2060 return ERROR_NT(nt_status);
2065 send_nt_replies(outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2069 /****************************************************************************
2071 ****************************************************************************/
2073 static int call_nt_transact_ioctl(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2074 uint16 **ppsetup, uint32 setup_count,
2075 char **ppparams, uint32 parameter_count,
2076 char **ppdata, uint32 data_count, uint32 max_data_count)
2083 static BOOL logged_message;
2084 char *pdata = *ppdata;
2086 if (setup_count != 8) {
2087 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2088 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2091 function = IVAL(*ppsetup, 0);
2092 fidnum = SVAL(*ppsetup, 4);
2093 isFSctl = CVAL(*ppsetup, 6);
2094 compfilter = CVAL(*ppsetup, 7);
2096 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2097 function, fidnum, isFSctl, compfilter));
2099 fsp=file_fsp((char *)*ppsetup, 4);
2100 /* this check is done in each implemented function case for now
2101 because I don't want to break anything... --metze
2102 FSP_BELONGS_CONN(fsp,conn);*/
2105 case FSCTL_SET_SPARSE:
2106 /* pretend this succeeded - tho strictly we should
2107 mark the file sparse (if the local fs supports it)
2108 so we can know if we need to pre-allocate or not */
2110 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2111 send_nt_replies(outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL,
2115 case FSCTL_0x000900C0:
2116 /* pretend this succeeded - don't know what this really is
2117 but works ok like this --metze
2120 DEBUG(10,("FSCTL_0x000900C0: called on FID[0x%04X](but not implemented)\n",fidnum));
2121 send_nt_replies(outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL,
2125 case FSCTL_GET_REPARSE_POINT:
2126 /* pretend this fail - my winXP does it like this
2130 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2131 send_nt_replies(outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT,
2135 case FSCTL_SET_REPARSE_POINT:
2136 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2140 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2141 send_nt_replies(outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT,
2145 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2148 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2149 * and return their volume names. If max_data_count is 16, then it is just
2150 * asking for the number of volumes and length of the combined names.
2152 * pdata is the data allocated by our caller, but that uses
2153 * total_data_count (which is 0 in our case) rather than max_data_count.
2154 * Allocate the correct amount and return the pointer to let
2155 * it be deallocated when we return.
2157 SHADOW_COPY_DATA *shadow_data = NULL;
2158 TALLOC_CTX *shadow_mem_ctx = NULL;
2159 BOOL labels = False;
2160 uint32 labels_data_count = 0;
2164 FSP_BELONGS_CONN(fsp,conn);
2166 if (max_data_count < 16) {
2167 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2169 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2172 if (max_data_count > 16) {
2176 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2177 if (shadow_mem_ctx == NULL) {
2178 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2179 return ERROR_NT(NT_STATUS_NO_MEMORY);
2182 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2183 if (shadow_data == NULL) {
2184 DEBUG(0,("talloc_zero() failed!\n"));
2185 talloc_destroy(shadow_mem_ctx);
2186 return ERROR_NT(NT_STATUS_NO_MEMORY);
2189 shadow_data->mem_ctx = shadow_mem_ctx;
2192 * Call the VFS routine to actually do the work.
2194 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2195 talloc_destroy(shadow_data->mem_ctx);
2196 if (errno == ENOSYS) {
2197 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2198 conn->connectpath));
2199 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2201 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2202 conn->connectpath));
2203 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
2207 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2212 data_count = 12+labels_data_count+4;
2215 if (max_data_count<data_count) {
2216 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2217 max_data_count,data_count));
2218 talloc_destroy(shadow_data->mem_ctx);
2219 return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
2222 pdata = nttrans_realloc(ppdata, data_count);
2223 if (pdata == NULL) {
2224 talloc_destroy(shadow_data->mem_ctx);
2225 return ERROR_NT(NT_STATUS_NO_MEMORY);
2230 /* num_volumes 4 bytes */
2231 SIVAL(pdata,0,shadow_data->num_volumes);
2234 /* num_labels 4 bytes */
2235 SIVAL(pdata,4,shadow_data->num_volumes);
2238 /* needed_data_count 4 bytes */
2239 SIVAL(pdata,8,labels_data_count);
2243 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2244 shadow_data->num_volumes,fsp->fsp_name));
2245 if (labels && shadow_data->labels) {
2246 for (i=0;i<shadow_data->num_volumes;i++) {
2247 srvstr_push(outbuf, cur_pdata, shadow_data->labels[i], 2*sizeof(SHADOW_COPY_LABEL), STR_UNICODE|STR_TERMINATE);
2248 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2249 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2253 talloc_destroy(shadow_data->mem_ctx);
2255 send_nt_replies(outbuf, bufsize, NT_STATUS_OK, NULL, 0,
2261 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2263 /* pretend this succeeded -
2265 * we have to send back a list with all files owned by this SID
2267 * but I have to check that --metze
2271 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2273 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2275 FSP_BELONGS_CONN(fsp,conn);
2277 /* unknown 4 bytes: this is not the length of the sid :-( */
2278 /*unknown = IVAL(pdata,0);*/
2280 sid_parse(pdata+4,sid_len,&sid);
2281 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2283 if (!sid_to_uid(&sid, &uid)) {
2284 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2285 sid_string_static(&sid),(unsigned long)sid_len));
2289 /* we can take a look at the find source :-)
2291 * find ./ -uid $uid -name '*' is what we need here
2294 * and send 4bytes len and then NULL terminated unicode strings
2297 * but I don't know how to deal with the paged results
2298 * (maybe we can hang the result anywhere in the fsp struct)
2300 * we don't send all files at once
2301 * and at the next we should *not* start from the beginning,
2302 * so we have to cache the result
2307 /* this works for now... */
2308 send_nt_replies(outbuf, bufsize, NT_STATUS_OK, NULL, 0,
2313 if (!logged_message) {
2314 logged_message = True; /* Only print this once... */
2315 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2320 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2324 #ifdef HAVE_SYS_QUOTAS
2325 /****************************************************************************
2326 Reply to get user quota
2327 ****************************************************************************/
2329 static int call_nt_transact_get_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2330 uint16 **ppsetup, uint32 setup_count,
2331 char **ppparams, uint32 parameter_count,
2332 char **ppdata, uint32 data_count, uint32 max_data_count)
2334 NTSTATUS nt_status = NT_STATUS_OK;
2335 char *params = *ppparams;
2336 char *pdata = *ppdata;
2338 int data_len=0,param_len=0;
2341 files_struct *fsp = NULL;
2345 BOOL start_enum = True;
2346 SMB_NTQUOTA_STRUCT qt;
2347 SMB_NTQUOTA_LIST *tmp_list;
2348 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2353 if (current_user.ut.uid != 0) {
2354 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2355 lp_servicename(SNUM(conn)),conn->user));
2356 return ERROR_DOS(ERRDOS,ERRnoaccess);
2360 * Ensure minimum number of parameters sent.
2363 if (parameter_count < 4) {
2364 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2365 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2368 /* maybe we can check the quota_fnum */
2369 fsp = file_fsp(params,0);
2370 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2371 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2372 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2375 /* the NULL pointer checking for fsp->fake_file_handle->pd
2376 * is done by CHECK_NTQUOTA_HANDLE_OK()
2378 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2380 level = SVAL(params,2);
2382 /* unknown 12 bytes leading in params */
2385 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2386 /* seems that we should continue with the enum here --metze */
2388 if (qt_handle->quota_list!=NULL &&
2389 qt_handle->tmp_list==NULL) {
2392 free_ntquota_list(&(qt_handle->quota_list));
2394 /* Realloc the size of parameters and data we will return */
2396 params = nttrans_realloc(ppparams, param_len);
2397 if(params == NULL) {
2398 return ERROR_DOS(ERRDOS,ERRnomem);
2402 SIVAL(params,0,data_len);
2409 case TRANSACT_GET_USER_QUOTA_LIST_START:
2411 if (qt_handle->quota_list==NULL &&
2412 qt_handle->tmp_list==NULL) {
2416 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
2417 return ERROR_DOS(ERRSRV,ERRerror);
2419 /* Realloc the size of parameters and data we will return */
2421 params = nttrans_realloc(ppparams, param_len);
2422 if(params == NULL) {
2423 return ERROR_DOS(ERRDOS,ERRnomem);
2426 /* we should not trust the value in max_data_count*/
2427 max_data_count = MIN(max_data_count,2048);
2429 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2431 return ERROR_DOS(ERRDOS,ERRnomem);
2436 /* set params Size of returned Quota Data 4 bytes*/
2437 /* but set it later when we know it */
2439 /* for each entry push the data */
2442 qt_handle->tmp_list = qt_handle->quota_list;
2445 tmp_list = qt_handle->tmp_list;
2447 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2448 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2450 sid_len = sid_size(&tmp_list->quotas->sid);
2451 entry_len = 40 + sid_len;
2453 /* nextoffset entry 4 bytes */
2454 SIVAL(entry,0,entry_len);
2456 /* then the len of the SID 4 bytes */
2457 SIVAL(entry,4,sid_len);
2459 /* unknown data 8 bytes SMB_BIG_UINT */
2460 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2462 /* the used disk space 8 bytes SMB_BIG_UINT */
2463 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2465 /* the soft quotas 8 bytes SMB_BIG_UINT */
2466 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2468 /* the hard quotas 8 bytes SMB_BIG_UINT */
2469 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2471 /* and now the SID */
2472 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2475 qt_handle->tmp_list = tmp_list;
2477 /* overwrite the offset of the last entry */
2478 SIVAL(entry-entry_len,0,0);
2480 data_len = 4+qt_len;
2481 /* overwrite the params quota_data_len */
2482 SIVAL(params,0,data_len);
2486 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2488 /* unknown 4 bytes IVAL(pdata,0) */
2490 if (data_count < 8) {
2491 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2492 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2495 sid_len = IVAL(pdata,4);
2496 /* Ensure this is less than 1mb. */
2497 if (sid_len > (1024*1024)) {
2498 return ERROR_DOS(ERRDOS,ERRnomem);
2501 if (data_count < 8+sid_len) {
2502 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2503 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2506 data_len = 4+40+sid_len;
2508 if (max_data_count < data_len) {
2509 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2510 max_data_count, data_len));
2512 SIVAL(params,0,data_len);
2514 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2518 sid_parse(pdata+8,sid_len,&sid);
2520 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2523 * we have to return zero's in all fields
2524 * instead of returning an error here
2529 /* Realloc the size of parameters and data we will return */
2531 params = nttrans_realloc(ppparams, param_len);
2532 if(params == NULL) {
2533 return ERROR_DOS(ERRDOS,ERRnomem);
2536 pdata = nttrans_realloc(ppdata, data_len);
2538 return ERROR_DOS(ERRDOS,ERRnomem);
2543 /* set params Size of returned Quota Data 4 bytes*/
2544 SIVAL(params,0,data_len);
2546 /* nextoffset entry 4 bytes */
2549 /* then the len of the SID 4 bytes */
2550 SIVAL(entry,4,sid_len);
2552 /* unknown data 8 bytes SMB_BIG_UINT */
2553 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2555 /* the used disk space 8 bytes SMB_BIG_UINT */
2556 SBIG_UINT(entry,16,qt.usedspace);
2558 /* the soft quotas 8 bytes SMB_BIG_UINT */
2559 SBIG_UINT(entry,24,qt.softlim);
2561 /* the hard quotas 8 bytes SMB_BIG_UINT */
2562 SBIG_UINT(entry,32,qt.hardlim);
2564 /* and now the SID */
2565 sid_linearize(entry+40, sid_len, &sid);
2570 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2571 return ERROR_DOS(ERRSRV,ERRerror);
2575 send_nt_replies(outbuf, bufsize, nt_status, params, param_len,
2581 /****************************************************************************
2582 Reply to set user quota
2583 ****************************************************************************/
2585 static int call_nt_transact_set_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2586 uint16 **ppsetup, uint32 setup_count,
2587 char **ppparams, uint32 parameter_count,
2588 char **ppdata, uint32 data_count, uint32 max_data_count)
2590 char *params = *ppparams;
2591 char *pdata = *ppdata;
2592 int data_len=0,param_len=0;
2593 SMB_NTQUOTA_STRUCT qt;
2596 files_struct *fsp = NULL;
2601 if (current_user.ut.uid != 0) {
2602 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2603 lp_servicename(SNUM(conn)),conn->user));
2604 return ERROR_DOS(ERRDOS,ERRnoaccess);
2608 * Ensure minimum number of parameters sent.
2611 if (parameter_count < 2) {
2612 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2613 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2616 /* maybe we can check the quota_fnum */
2617 fsp = file_fsp(params,0);
2618 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2619 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2620 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2623 if (data_count < 40) {
2624 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2625 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2628 /* offset to next quota record.
2629 * 4 bytes IVAL(pdata,0)
2634 sid_len = IVAL(pdata,4);
2636 if (data_count < 40+sid_len) {
2637 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2638 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2641 /* unknown 8 bytes in pdata
2642 * maybe its the change time in NTTIME
2645 /* the used space 8 bytes (SMB_BIG_UINT)*/
2646 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2647 #ifdef LARGE_SMB_OFF_T
2648 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2649 #else /* LARGE_SMB_OFF_T */
2650 if ((IVAL(pdata,20) != 0)&&
2651 ((qt.usedspace != 0xFFFFFFFF)||
2652 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2653 /* more than 32 bits? */
2654 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2656 #endif /* LARGE_SMB_OFF_T */
2658 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2659 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2660 #ifdef LARGE_SMB_OFF_T
2661 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2662 #else /* LARGE_SMB_OFF_T */
2663 if ((IVAL(pdata,28) != 0)&&
2664 ((qt.softlim != 0xFFFFFFFF)||
2665 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2666 /* more than 32 bits? */
2667 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2669 #endif /* LARGE_SMB_OFF_T */
2671 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2672 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2673 #ifdef LARGE_SMB_OFF_T
2674 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2675 #else /* LARGE_SMB_OFF_T */
2676 if ((IVAL(pdata,36) != 0)&&
2677 ((qt.hardlim != 0xFFFFFFFF)||
2678 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2679 /* more than 32 bits? */
2680 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2682 #endif /* LARGE_SMB_OFF_T */
2684 sid_parse(pdata+40,sid_len,&sid);
2685 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2687 /* 44 unknown bytes left... */
2689 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2690 return ERROR_DOS(ERRSRV,ERRerror);
2693 send_nt_replies(outbuf, bufsize, NT_STATUS_OK, params, param_len,
2698 #endif /* HAVE_SYS_QUOTAS */
2700 static int handle_nttrans(connection_struct *conn,
2701 struct trans_state *state,
2702 char *inbuf, char *outbuf, int size, int bufsize)
2706 if (Protocol >= PROTOCOL_NT1) {
2707 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | 0x40); /* IS_LONG_NAME */
2710 /* Now we must call the relevant NT_TRANS function */
2711 switch(state->call) {
2712 case NT_TRANSACT_CREATE:
2714 START_PROFILE_NESTED(NT_transact_create);
2715 outsize = call_nt_transact_create(conn, inbuf, outbuf,
2717 &state->setup, state->setup_count,
2718 &state->param, state->total_param,
2719 &state->data, state->total_data,
2720 state->max_data_return);
2721 END_PROFILE_NESTED(NT_transact_create);
2725 case NT_TRANSACT_IOCTL:
2727 START_PROFILE_NESTED(NT_transact_ioctl);
2728 outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
2730 &state->setup, state->setup_count,
2731 &state->param, state->total_param,
2732 &state->data, state->total_data, state->max_data_return);
2733 END_PROFILE_NESTED(NT_transact_ioctl);
2737 case NT_TRANSACT_SET_SECURITY_DESC:
2739 START_PROFILE_NESTED(NT_transact_set_security_desc);
2740 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
2742 &state->setup, state->setup_count,
2743 &state->param, state->total_param,
2744 &state->data, state->total_data, state->max_data_return);
2745 END_PROFILE_NESTED(NT_transact_set_security_desc);
2749 case NT_TRANSACT_NOTIFY_CHANGE:
2751 START_PROFILE_NESTED(NT_transact_notify_change);
2752 outsize = call_nt_transact_notify_change(
2753 conn, inbuf, outbuf, size, bufsize,
2754 &state->setup, state->setup_count,
2755 &state->param, state->total_param,
2756 &state->data, state->total_data,
2757 state->max_data_return,
2758 state->max_param_return);
2759 END_PROFILE_NESTED(NT_transact_notify_change);
2763 case NT_TRANSACT_RENAME:
2765 START_PROFILE_NESTED(NT_transact_rename);
2766 outsize = call_nt_transact_rename(conn, inbuf, outbuf,
2768 &state->setup, state->setup_count,
2769 &state->param, state->total_param,
2770 &state->data, state->total_data, state->max_data_return);
2771 END_PROFILE_NESTED(NT_transact_rename);
2775 case NT_TRANSACT_QUERY_SECURITY_DESC:
2777 START_PROFILE_NESTED(NT_transact_query_security_desc);
2778 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
2780 &state->setup, state->setup_count,
2781 &state->param, state->total_param,
2782 &state->data, state->total_data, state->max_data_return);
2783 END_PROFILE_NESTED(NT_transact_query_security_desc);
2787 #ifdef HAVE_SYS_QUOTAS
2788 case NT_TRANSACT_GET_USER_QUOTA:
2790 START_PROFILE_NESTED(NT_transact_get_user_quota);
2791 outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf,
2793 &state->setup, state->setup_count,
2794 &state->param, state->total_param,
2795 &state->data, state->total_data, state->max_data_return);
2796 END_PROFILE_NESTED(NT_transact_get_user_quota);
2800 case NT_TRANSACT_SET_USER_QUOTA:
2802 START_PROFILE_NESTED(NT_transact_set_user_quota);
2803 outsize = call_nt_transact_set_user_quota(conn, inbuf, outbuf,
2805 &state->setup, state->setup_count,
2806 &state->param, state->total_param,
2807 &state->data, state->total_data, state->max_data_return);
2808 END_PROFILE_NESTED(NT_transact_set_user_quota);
2811 #endif /* HAVE_SYS_QUOTAS */
2814 /* Error in request */
2815 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n",
2817 return ERROR_DOS(ERRSRV,ERRerror);
2822 /****************************************************************************
2823 Reply to a SMBNTtrans.
2824 ****************************************************************************/
2826 int reply_nttrans(connection_struct *conn,
2827 char *inbuf,char *outbuf,int size,int bufsize)
2830 uint32 pscnt = IVAL(inbuf,smb_nt_ParameterCount);
2831 uint32 psoff = IVAL(inbuf,smb_nt_ParameterOffset);
2832 uint32 dscnt = IVAL(inbuf,smb_nt_DataCount);
2833 uint32 dsoff = IVAL(inbuf,smb_nt_DataOffset);
2835 uint16 function_code = SVAL( inbuf, smb_nt_Function);
2837 struct trans_state *state;
2839 START_PROFILE(SMBnttrans);
2841 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2842 END_PROFILE(SMBnttrans);
2843 return ERROR_DOS(ERRSRV,ERRaccess);
2846 result = allow_new_trans(conn->pending_trans, SVAL(inbuf, smb_mid));
2847 if (!NT_STATUS_IS_OK(result)) {
2848 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2849 END_PROFILE(SMBnttrans);
2850 return ERROR_NT(result);
2853 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
2854 END_PROFILE(SMBnttrans);
2855 return ERROR_DOS(ERRSRV,ERRaccess);
2858 state->cmd = SMBnttrans;
2860 state->mid = SVAL(inbuf,smb_mid);
2861 state->vuid = SVAL(inbuf,smb_uid);
2862 state->total_data = IVAL(inbuf, smb_nt_TotalDataCount);
2864 state->total_param = IVAL(inbuf, smb_nt_TotalParameterCount);
2865 state->param = NULL;
2866 state->max_data_return = IVAL(inbuf,smb_nt_MaxDataCount);
2867 state->max_param_return = IVAL(inbuf,smb_nt_MaxParameterCount);
2869 /* setup count is in *words* */
2870 state->setup_count = 2*CVAL(inbuf,smb_nt_SetupCount);
2871 state->setup = NULL;
2872 state->call = function_code;
2875 * All nttrans messages we handle have smb_wct == 19 +
2876 * state->setup_count. Ensure this is so as a sanity check.
2879 if(CVAL(inbuf, smb_wct) != 19 + (state->setup_count/2)) {
2880 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2881 CVAL(inbuf, smb_wct), 19 + (state->setup_count/2)));
2885 /* Don't allow more than 128mb for each value. */
2886 if ((state->total_data > (1024*1024*128)) ||
2887 (state->total_param > (1024*1024*128))) {
2888 END_PROFILE(SMBnttrans);
2889 return ERROR_DOS(ERRDOS,ERRnomem);
2892 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2895 if (state->total_data) {
2896 /* Can't use talloc here, the core routines do realloc on the
2897 * params and data. */
2898 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2899 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2900 "bytes !\n", (unsigned int)state->total_data));
2902 END_PROFILE(SMBnttrans);
2903 return(ERROR_DOS(ERRDOS,ERRnomem));
2905 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
2907 if ((smb_base(inbuf)+dsoff+dscnt > inbuf + size) ||
2908 (smb_base(inbuf)+dsoff+dscnt < smb_base(inbuf)))
2911 memcpy(state->data,smb_base(inbuf)+dsoff,dscnt);
2914 if (state->total_param) {
2915 /* Can't use talloc here, the core routines do realloc on the
2916 * params and data. */
2917 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2918 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2919 "bytes !\n", (unsigned int)state->total_param));
2920 SAFE_FREE(state->data);
2922 END_PROFILE(SMBnttrans);
2923 return(ERROR_DOS(ERRDOS,ERRnomem));
2925 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
2927 if ((smb_base(inbuf)+psoff+pscnt > inbuf + size) ||
2928 (smb_base(inbuf)+psoff+pscnt < smb_base(inbuf)))
2931 memcpy(state->param,smb_base(inbuf)+psoff,pscnt);
2934 state->received_data = dscnt;
2935 state->received_param = pscnt;
2937 if(state->setup_count > 0) {
2938 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2939 state->setup_count));
2940 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2941 if (state->setup == NULL) {
2942 DEBUG(0,("reply_nttrans : Out of memory\n"));
2943 SAFE_FREE(state->data);
2944 SAFE_FREE(state->param);
2946 END_PROFILE(SMBnttrans);
2947 return ERROR_DOS(ERRDOS,ERRnomem);
2950 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2951 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2954 if (smb_nt_SetupStart + state->setup_count > size) {
2958 memcpy( state->setup, &inbuf[smb_nt_SetupStart], state->setup_count);
2959 dump_data(10, (char *)state->setup, state->setup_count);
2962 if ((state->received_data == state->total_data) &&
2963 (state->received_param == state->total_param)) {
2964 outsize = handle_nttrans(conn, state, inbuf, outbuf,
2966 SAFE_FREE(state->param);
2967 SAFE_FREE(state->data);
2969 END_PROFILE(SMBnttrans);
2973 DLIST_ADD(conn->pending_trans, state);
2975 /* We need to send an interim response then receive the rest
2976 of the parameter/data bytes */
2977 outsize = set_message(outbuf,0,0,False);
2979 END_PROFILE(SMBnttrans);
2984 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2985 SAFE_FREE(state->data);
2986 SAFE_FREE(state->param);
2988 END_PROFILE(SMBnttrans);
2989 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2992 /****************************************************************************
2993 Reply to a SMBnttranss
2994 ****************************************************************************/
2996 int reply_nttranss(connection_struct *conn, char *inbuf,char *outbuf,
2997 int size,int bufsize)
3000 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3001 struct trans_state *state;
3003 START_PROFILE(SMBnttranss);
3007 for (state = conn->pending_trans; state != NULL;
3008 state = state->next) {
3009 if (state->mid == SVAL(inbuf,smb_mid)) {
3014 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3015 END_PROFILE(SMBnttranss);
3016 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
3019 /* Revise state->total_param and state->total_data in case they have
3020 changed downwards */
3021 if (IVAL(inbuf, smb_nts_TotalParameterCount) < state->total_param) {
3022 state->total_param = IVAL(inbuf, smb_nts_TotalParameterCount);
3024 if (IVAL(inbuf, smb_nts_TotalDataCount) < state->total_data) {
3025 state->total_data = IVAL(inbuf, smb_nts_TotalDataCount);
3028 pcnt = IVAL(inbuf,smb_nts_ParameterCount);
3029 poff = IVAL(inbuf, smb_nts_ParameterOffset);
3030 pdisp = IVAL(inbuf, smb_nts_ParameterDisplacement);
3032 dcnt = IVAL(inbuf, smb_nts_DataCount);
3033 ddisp = IVAL(inbuf, smb_nts_DataDisplacement);
3034 doff = IVAL(inbuf, smb_nts_DataOffset);
3036 state->received_param += pcnt;
3037 state->received_data += dcnt;
3039 if ((state->received_data > state->total_data) ||
3040 (state->received_param > state->total_param))
3044 if (pdisp+pcnt > state->total_param)
3046 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3048 if (pdisp > state->total_param)
3050 if ((smb_base(inbuf) + poff + pcnt > inbuf + size) ||
3051 (smb_base(inbuf) + poff + pcnt < smb_base(inbuf)))
3053 if (state->param + pdisp < state->param)
3056 memcpy(state->param+pdisp,smb_base(inbuf)+poff,
3061 if (ddisp+dcnt > state->total_data)
3063 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3065 if (ddisp > state->total_data)
3067 if ((smb_base(inbuf) + doff + dcnt > inbuf + size) ||
3068 (smb_base(inbuf) + doff + dcnt < smb_base(inbuf)))
3070 if (state->data + ddisp < state->data)
3073 memcpy(state->data+ddisp, smb_base(inbuf)+doff,
3077 if ((state->received_param < state->total_param) ||
3078 (state->received_data < state->total_data)) {
3079 END_PROFILE(SMBnttranss);
3083 /* construct_reply_common has done us the favor to pre-fill the
3084 * command field with SMBnttranss which is wrong :-)
3086 SCVAL(outbuf,smb_com,SMBnttrans);
3088 outsize = handle_nttrans(conn, state, inbuf, outbuf,
3091 DLIST_REMOVE(conn->pending_trans, state);
3092 SAFE_FREE(state->data);
3093 SAFE_FREE(state->param);
3097 END_PROFILE(SMBnttranss);
3098 return(ERROR_DOS(ERRSRV,ERRnosupport));
3101 END_PROFILE(SMBnttranss);
3106 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3107 DLIST_REMOVE(conn->pending_trans, state);
3108 SAFE_FREE(state->data);
3109 SAFE_FREE(state->param);
3111 END_PROFILE(SMBnttranss);
3112 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);