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, SVAL(inbuf, smb_flg2), fname, smb_buf(inbuf),
386 sizeof(fname), STR_TERMINATE);
388 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0) {
393 * Deal with pipe return.
396 if (flags & EXTENDED_RESPONSE_REQUIRED) {
397 /* This is very strange. We
398 * return 50 words, but only set
399 * the wcnt to 42 ? It's definately
400 * what happens on the wire....
402 set_message(inbuf,outbuf,50,0,True);
403 SCVAL(outbuf,smb_wct,42);
405 set_message(inbuf,outbuf,34,0,True);
408 p = outbuf + smb_vwv2;
412 SIVAL(p,0,FILE_WAS_OPENED);
415 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
418 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
420 SSVAL(p,2, 0x5FF); /* ? */
423 if (flags & EXTENDED_RESPONSE_REQUIRED) {
425 SIVAL(p,0,FILE_GENERIC_ALL);
427 * For pipes W2K3 seems to return
429 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
431 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
434 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
436 return chain_reply(inbuf,outbuf,length,bufsize);
439 /****************************************************************************
440 Reply to an NT create and X call for a quota file.
441 ****************************************************************************/
443 int reply_ntcreate_and_X_quota(connection_struct *conn,
448 enum FAKE_FILE_TYPE fake_file_type,
453 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
457 status = open_fake_file(conn, fake_file_type, fname, desired_access,
460 if (!NT_STATUS_IS_OK(status)) {
461 return ERROR_NT(status);
464 set_message(inbuf,outbuf,34,0,True);
466 p = outbuf + smb_vwv2;
468 /* SCVAL(p,0,NO_OPLOCK_RETURN); */
470 SSVAL(p,0,fsp->fnum);
472 DEBUG(5,("reply_ntcreate_and_X_quota: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
474 result = chain_reply(inbuf,outbuf,length,bufsize);
478 /****************************************************************************
479 Reply to an NT create and X call.
480 ****************************************************************************/
482 int reply_ntcreate_and_X(connection_struct *conn,
483 char *inbuf,char *outbuf,int length,int bufsize)
487 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
488 uint32 access_mask = IVAL(inbuf,smb_ntcreate_DesiredAccess);
489 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
490 uint32 new_file_attributes;
491 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
492 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
493 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
494 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
495 /* Breakout the oplock request bits so we can set the
496 reply bits separately. */
497 int oplock_request = 0;
499 SMB_OFF_T file_len = 0;
500 SMB_STRUCT_STAT sbuf;
502 files_struct *fsp = NULL;
504 struct timespec c_timespec;
505 struct timespec a_timespec;
506 struct timespec m_timespec;
507 BOOL extended_oplock_granted = False;
509 struct smb_request req;
511 START_PROFILE(SMBntcreateX);
513 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
514 "file_attributes = 0x%x, share_access = 0x%x, "
515 "create_disposition = 0x%x create_options = 0x%x "
516 "root_dir_fid = 0x%x\n",
518 (unsigned int)access_mask,
519 (unsigned int)file_attributes,
520 (unsigned int)share_access,
521 (unsigned int)create_disposition,
522 (unsigned int)create_options,
523 (unsigned int)root_dir_fid ));
525 init_smb_request(&req, (uint8 *)inbuf);
528 * If it's an IPC, use the pipe handler.
532 if (lp_nt_pipe_support()) {
533 END_PROFILE(SMBntcreateX);
534 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
536 END_PROFILE(SMBntcreateX);
537 return(ERROR_DOS(ERRDOS,ERRnoaccess));
541 if (create_options & FILE_OPEN_BY_FILE_ID) {
542 END_PROFILE(SMBntcreateX);
543 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
550 if(root_dir_fid != 0) {
552 * This filename is relative to a directory fid.
555 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
559 END_PROFILE(SMBntcreateX);
560 return ERROR_DOS(ERRDOS,ERRbadfid);
563 if(!dir_fsp->is_directory) {
565 srvstr_get_path(inbuf, SVAL(inbuf,smb_flg2), fname,
566 smb_buf(inbuf), sizeof(fname), 0,
567 STR_TERMINATE, &status);
568 if (!NT_STATUS_IS_OK(status)) {
569 END_PROFILE(SMBntcreateX);
570 return ERROR_NT(status);
574 * Check to see if this is a mac fork of some kind.
577 if( is_ntfs_stream_name(fname)) {
578 END_PROFILE(SMBntcreateX);
579 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
583 we need to handle the case when we get a
584 relative open relative to a file and the
585 pathname is blank - this is a reopen!
586 (hint from demyn plantenberg)
589 END_PROFILE(SMBntcreateX);
590 return(ERROR_DOS(ERRDOS,ERRbadfid));
594 * Copy in the base directory name.
597 pstrcpy( fname, dir_fsp->fsp_name );
598 dir_name_len = strlen(fname);
601 * Ensure it ends in a '\'.
604 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
609 srvstr_get_path(inbuf, SVAL(inbuf,smb_flg2), rel_fname,
610 smb_buf(inbuf), sizeof(rel_fname), 0,
611 STR_TERMINATE, &status);
612 if (!NT_STATUS_IS_OK(status)) {
613 END_PROFILE(SMBntcreateX);
614 return ERROR_NT(status);
616 pstrcat(fname, rel_fname);
618 srvstr_get_path(inbuf, SVAL(inbuf,smb_flg2), fname,
619 smb_buf(inbuf), sizeof(fname), 0,
620 STR_TERMINATE, &status);
621 if (!NT_STATUS_IS_OK(status)) {
622 END_PROFILE(SMBntcreateX);
623 return ERROR_NT(status);
627 * Check to see if this is a mac fork of some kind.
630 if( is_ntfs_stream_name(fname)) {
631 enum FAKE_FILE_TYPE fake_file_type = is_fake_file(fname);
632 if (fake_file_type!=FAKE_FILE_TYPE_NONE) {
634 * Here we go! support for changing the disk quotas --metze
636 * We need to fake up to open this MAGIC QUOTA file
637 * and return a valid FID.
639 * w2k close this file directly after openening
640 * xp also tries a QUERY_FILE_INFO on the file and then close it
642 result = reply_ntcreate_and_X_quota(conn, inbuf, outbuf, length, bufsize,
643 fake_file_type, fname);
644 END_PROFILE(SMBntcreateX);
647 END_PROFILE(SMBntcreateX);
648 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
654 * Now contruct the smb_open_mode value from the filename,
655 * desired access and the share access.
657 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, fname);
658 if (!NT_STATUS_IS_OK(status)) {
659 END_PROFILE(SMBntcreateX);
660 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
661 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
663 return ERROR_NT(status);
666 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
667 if (oplock_request) {
668 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
672 * Ordinary file or directory.
676 * Check if POSIX semantics are wanted.
679 new_file_attributes = set_posix_case_semantics(conn, file_attributes);
681 status = unix_convert(conn, fname, False, NULL, &sbuf);
682 if (!NT_STATUS_IS_OK(status)) {
683 restore_case_semantics(conn, file_attributes);
684 END_PROFILE(SMBntcreateX);
685 return ERROR_NT(status);
687 /* All file access must go through check_name() */
688 status = check_name(conn, fname);
689 if (!NT_STATUS_IS_OK(status)) {
690 restore_case_semantics(conn, file_attributes);
691 END_PROFILE(SMBntcreateX);
692 return ERROR_NT(status);
695 /* This is the correct thing to do (check every time) but can_delete is
696 expensive (it may have to read the parent directory permissions). So
697 for now we're not doing it unless we have a strong hint the client
698 is really going to delete this file. If the client is forcing FILE_CREATE
699 let the filesystem take care of the permissions. */
701 /* Setting FILE_SHARE_DELETE is the hint. */
703 if (lp_acl_check_permissions(SNUM(conn))
704 && (create_disposition != FILE_CREATE)
705 && (share_access & FILE_SHARE_DELETE)
706 && (access_mask & DELETE_ACCESS)) {
707 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
708 !can_delete_file_in_directory(conn, fname)) {
709 restore_case_semantics(conn, file_attributes);
710 END_PROFILE(SMBntcreateX);
711 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
716 /* We need to support SeSecurityPrivilege for this. */
717 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY)) &&
718 !user_has_privileges(current_user.nt_user_token,
720 restore_case_semantics(conn, file_attributes);
721 END_PROFILE(SMBntcreateX);
722 return ERROR_NT(NT_STATUS_PRIVILEGE_NOT_HELD);
727 * If it's a request for a directory open, deal with it separately.
730 if(create_options & FILE_DIRECTORY_FILE) {
732 /* Can't open a temp directory. IFS kit test. */
733 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
734 restore_case_semantics(conn, file_attributes);
735 END_PROFILE(SMBntcreateX);
736 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
740 status = open_directory(conn, &req, fname, &sbuf,
748 restore_case_semantics(conn, file_attributes);
750 if(!NT_STATUS_IS_OK(status)) {
751 if (!use_nt_status() && NT_STATUS_EQUAL(
752 status, NT_STATUS_OBJECT_NAME_COLLISION)) {
753 status = NT_STATUS_DOS(ERRDOS, ERRfilexists);
755 END_PROFILE(SMBntcreateX);
756 return ERROR_NT(status);
762 * Ordinary file case.
765 /* NB. We have a potential bug here. If we
766 * cause an oplock break to ourselves, then we
767 * could end up processing filename related
768 * SMB requests whilst we await the oplock
769 * break response. As we may have changed the
770 * filename case semantics to be POSIX-like,
771 * this could mean a filename request could
772 * fail when it should succeed. This is a rare
773 * condition, but eventually we must arrange
774 * to restore the correct case semantics
775 * before issuing an oplock break request to
776 * our client. JRA. */
778 status = open_file_ntcreate(conn, &req, fname, &sbuf,
787 if (!NT_STATUS_IS_OK(status)) {
788 /* We cheat here. There are two cases we
789 * care about. One is a directory rename,
790 * where the NT client will attempt to
791 * open the source directory for
792 * DELETE access. Note that when the
793 * NT client does this it does *not*
794 * set the directory bit in the
795 * request packet. This is translated
796 * into a read/write open
797 * request. POSIX states that any open
798 * for write request on a directory
799 * will generate an EISDIR error, so
800 * we can catch this here and open a
801 * pseudo handle that is flagged as a
802 * directory. The second is an open
803 * for a permissions read only, which
804 * we handle in the open_file_stat case. JRA.
807 if (NT_STATUS_EQUAL(status,
808 NT_STATUS_FILE_IS_A_DIRECTORY)) {
811 * Fail the open if it was explicitly a non-directory file.
814 if (create_options & FILE_NON_DIRECTORY_FILE) {
815 restore_case_semantics(conn, file_attributes);
816 END_PROFILE(SMBntcreateX);
817 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
821 status = open_directory(conn, &req, fname,
830 if(!NT_STATUS_IS_OK(status)) {
831 restore_case_semantics(conn, file_attributes);
832 if (!use_nt_status() && NT_STATUS_EQUAL(
833 status, NT_STATUS_OBJECT_NAME_COLLISION)) {
834 status = NT_STATUS_DOS(ERRDOS, ERRfilexists);
836 END_PROFILE(SMBntcreateX);
837 return ERROR_NT(status);
840 restore_case_semantics(conn, file_attributes);
841 END_PROFILE(SMBntcreateX);
842 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
843 /* We have re-scheduled this call. */
846 return ERROR_NT(status);
851 restore_case_semantics(conn, file_attributes);
853 file_len = sbuf.st_size;
854 fattr = dos_mode(conn,fname,&sbuf);
856 fattr = FILE_ATTRIBUTE_NORMAL;
858 if (!fsp->is_directory && (fattr & aDIR)) {
859 close_file(fsp,ERROR_CLOSE);
860 END_PROFILE(SMBntcreateX);
861 return ERROR_DOS(ERRDOS,ERRnoaccess);
864 /* Save the requested allocation size. */
865 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
866 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize);
867 #ifdef LARGE_SMB_OFF_T
868 allocation_size |= (((SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
870 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
871 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
872 if (fsp->is_directory) {
873 close_file(fsp,ERROR_CLOSE);
874 END_PROFILE(SMBntcreateX);
875 /* Can't set allocation size on a directory. */
876 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
878 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
879 close_file(fsp,ERROR_CLOSE);
880 END_PROFILE(SMBntcreateX);
881 return ERROR_NT(NT_STATUS_DISK_FULL);
884 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
889 * If the caller set the extended oplock request bit
890 * and we granted one (by whatever means) - set the
891 * correct bit for extended oplock reply.
894 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
895 extended_oplock_granted = True;
898 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
899 extended_oplock_granted = True;
902 if (flags & EXTENDED_RESPONSE_REQUIRED) {
903 /* This is very strange. We
904 * return 50 words, but only set
905 * the wcnt to 42 ? It's definately
906 * what happens on the wire....
908 set_message(inbuf,outbuf,50,0,True);
909 SCVAL(outbuf,smb_wct,42);
911 set_message(inbuf,outbuf,34,0,True);
914 p = outbuf + smb_vwv2;
917 * Currently as we don't support level II oplocks we just report
918 * exclusive & batch here.
921 if (extended_oplock_granted) {
922 if (flags & REQUEST_BATCH_OPLOCK) {
923 SCVAL(p,0, BATCH_OPLOCK_RETURN);
925 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
927 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
928 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
930 SCVAL(p,0,NO_OPLOCK_RETURN);
934 SSVAL(p,0,fsp->fnum);
936 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
937 SIVAL(p,0,FILE_WAS_SUPERSEDED);
944 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
945 a_timespec = get_atimespec(&sbuf);
946 m_timespec = get_mtimespec(&sbuf);
948 if (lp_dos_filetime_resolution(SNUM(conn))) {
949 dos_filetime_timespec(&c_timespec);
950 dos_filetime_timespec(&a_timespec);
951 dos_filetime_timespec(&m_timespec);
954 put_long_date_timespec(p, c_timespec); /* create time. */
956 put_long_date_timespec(p, a_timespec); /* access time */
958 put_long_date_timespec(p, m_timespec); /* write time */
960 put_long_date_timespec(p, m_timespec); /* change time */
962 SIVAL(p,0,fattr); /* File Attributes. */
964 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
966 SOFF_T(p,0,file_len);
968 if (flags & EXTENDED_RESPONSE_REQUIRED) {
972 SCVAL(p,0,fsp->is_directory ? 1 : 0);
974 if (flags & EXTENDED_RESPONSE_REQUIRED) {
977 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
978 perms = FILE_GENERIC_ALL;
980 perms = FILE_GENERIC_READ|FILE_EXECUTE;
985 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
987 result = chain_reply(inbuf,outbuf,length,bufsize);
988 END_PROFILE(SMBntcreateX);
992 /****************************************************************************
993 Reply to a NT_TRANSACT_CREATE call to open a pipe.
994 ****************************************************************************/
996 static int do_nt_transact_create_pipe( connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
997 uint16 **ppsetup, uint32 setup_count,
998 char **ppparams, uint32 parameter_count,
999 char **ppdata, uint32 data_count)
1002 char *params = *ppparams;
1011 * Ensure minimum number of parameters sent.
1014 if(parameter_count < 54) {
1015 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1016 return ERROR_DOS(ERRDOS,ERRnoaccess);
1019 flags = IVAL(params,0);
1021 srvstr_get_path(inbuf, SVAL(inbuf,smb_flg2), fname, params+53,
1022 sizeof(fname), parameter_count-53, STR_TERMINATE,
1024 if (!NT_STATUS_IS_OK(status)) {
1025 return ERROR_NT(status);
1028 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0) {
1032 /* Realloc the size of parameters and data we will return */
1033 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1034 /* Extended response is 32 more byyes. */
1039 params = nttrans_realloc(ppparams, param_len);
1040 if(params == NULL) {
1041 return ERROR_DOS(ERRDOS,ERRnomem);
1045 SCVAL(p,0,NO_OPLOCK_RETURN);
1050 SIVAL(p,0,FILE_WAS_OPENED);
1054 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1057 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1059 SSVAL(p,2, 0x5FF); /* ? */
1062 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1064 SIVAL(p,0,FILE_GENERIC_ALL);
1066 * For pipes W2K3 seems to return
1068 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
1070 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
1073 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1075 /* Send the required number of replies */
1076 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, *ppdata, 0);
1081 /****************************************************************************
1082 Internal fn to set security descriptors.
1083 ****************************************************************************/
1085 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1088 SEC_DESC *psd = NULL;
1089 TALLOC_CTX *mem_ctx;
1092 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
1093 return NT_STATUS_OK;
1097 * Init the parse struct we will unmarshall from.
1100 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1101 DEBUG(0,("set_sd: talloc_init failed.\n"));
1102 return NT_STATUS_NO_MEMORY;
1105 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1108 * Setup the prs_struct to point at the memory we just
1112 prs_give_memory( &pd, data, sd_len, False);
1115 * Finally, unmarshall from the data buffer.
1118 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1119 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1121 * Return access denied for want of a better error message..
1123 talloc_destroy(mem_ctx);
1124 return NT_STATUS_NO_MEMORY;
1127 if (psd->owner_sid==0) {
1128 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1130 if (psd->group_sid==0) {
1131 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1134 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1137 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1140 status = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1142 talloc_destroy(mem_ctx);
1146 /****************************************************************************
1147 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1148 ****************************************************************************/
1150 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1152 struct ea_list *ea_list_head = NULL;
1155 if (data_size < 4) {
1159 while (offset + 4 <= data_size) {
1160 size_t next_offset = IVAL(pdata,offset);
1161 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1167 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1168 if (next_offset == 0) {
1171 offset += next_offset;
1174 return ea_list_head;
1177 /****************************************************************************
1178 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1179 ****************************************************************************/
1181 static int call_nt_transact_create(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1182 uint16 **ppsetup, uint32 setup_count,
1183 char **ppparams, uint32 parameter_count,
1184 char **ppdata, uint32 data_count, uint32 max_data_count)
1187 char *params = *ppparams;
1188 char *data = *ppdata;
1189 /* Breakout the oplock request bits so we can set the reply bits separately. */
1190 int oplock_request = 0;
1192 SMB_OFF_T file_len = 0;
1193 SMB_STRUCT_STAT sbuf;
1195 files_struct *fsp = NULL;
1197 BOOL extended_oplock_granted = False;
1200 uint32 file_attributes;
1201 uint32 new_file_attributes;
1202 uint32 share_access;
1203 uint32 create_disposition;
1204 uint32 create_options;
1207 uint16 root_dir_fid;
1208 struct timespec c_timespec;
1209 struct timespec a_timespec;
1210 struct timespec m_timespec;
1211 struct ea_list *ea_list = NULL;
1212 TALLOC_CTX *ctx = NULL;
1216 struct smb_request req;
1218 DEBUG(5,("call_nt_transact_create\n"));
1221 * If it's an IPC, use the pipe handler.
1225 if (lp_nt_pipe_support()) {
1226 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1228 ppsetup, setup_count,
1229 ppparams, parameter_count,
1230 ppdata, data_count);
1232 return ERROR_DOS(ERRDOS,ERRnoaccess);
1237 * Ensure minimum number of parameters sent.
1240 if(parameter_count < 54) {
1241 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1242 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1245 init_smb_request(&req, (uint8 *)inbuf);
1247 flags = IVAL(params,0);
1248 access_mask = IVAL(params,8);
1249 file_attributes = IVAL(params,20);
1250 share_access = IVAL(params,24);
1251 create_disposition = IVAL(params,28);
1252 create_options = IVAL(params,32);
1253 sd_len = IVAL(params,36);
1254 ea_len = IVAL(params,40);
1255 root_dir_fid = (uint16)IVAL(params,4);
1257 /* Ensure the data_len is correct for the sd and ea values given. */
1258 if ((ea_len + sd_len > data_count) ||
1259 (ea_len > data_count) || (sd_len > data_count) ||
1260 (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1261 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1262 (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1263 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1267 if (!lp_ea_support(SNUM(conn))) {
1268 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1269 (unsigned int)ea_len ));
1270 return ERROR_NT(NT_STATUS_EAS_NOT_SUPPORTED);
1274 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1275 (unsigned int)ea_len ));
1276 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1280 if (create_options & FILE_OPEN_BY_FILE_ID) {
1281 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1285 * Get the file name.
1288 if(root_dir_fid != 0) {
1290 * This filename is relative to a directory fid.
1292 files_struct *dir_fsp = file_fsp(params,4);
1293 size_t dir_name_len;
1296 return ERROR_DOS(ERRDOS,ERRbadfid);
1299 if(!dir_fsp->is_directory) {
1300 srvstr_get_path(inbuf, SVAL(inbuf,smb_flg2), fname,
1301 params+53, sizeof(fname),
1302 parameter_count-53, STR_TERMINATE,
1304 if (!NT_STATUS_IS_OK(status)) {
1305 return ERROR_NT(status);
1309 * Check to see if this is a mac fork of some kind.
1312 if( is_ntfs_stream_name(fname)) {
1313 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1316 return ERROR_DOS(ERRDOS,ERRbadfid);
1320 * Copy in the base directory name.
1323 pstrcpy( fname, dir_fsp->fsp_name );
1324 dir_name_len = strlen(fname);
1327 * Ensure it ends in a '\'.
1330 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1331 pstrcat(fname, "/");
1337 srvstr_get_path(inbuf, SVAL(inbuf,smb_flg2), tmpname,
1338 params+53, sizeof(tmpname),
1339 parameter_count-53, STR_TERMINATE,
1341 if (!NT_STATUS_IS_OK(status)) {
1342 return ERROR_NT(status);
1344 pstrcat(fname, tmpname);
1347 srvstr_get_path(inbuf, SVAL(inbuf,smb_flg2), fname, params+53,
1348 sizeof(fname), parameter_count-53,
1349 STR_TERMINATE, &status);
1350 if (!NT_STATUS_IS_OK(status)) {
1351 return ERROR_NT(status);
1355 * Check to see if this is a mac fork of some kind.
1358 if( is_ntfs_stream_name(fname)) {
1359 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1363 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1364 if (oplock_request) {
1365 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1369 * Ordinary file or directory.
1373 * Check if POSIX semantics are wanted.
1376 new_file_attributes = set_posix_case_semantics(conn, file_attributes);
1378 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, fname);
1379 if (!NT_STATUS_IS_OK(status)) {
1380 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1381 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1383 return ERROR_NT(status);
1386 status = unix_convert(conn, fname, False, NULL, &sbuf);
1387 if (!NT_STATUS_IS_OK(status)) {
1388 restore_case_semantics(conn, file_attributes);
1389 return ERROR_NT(status);
1391 /* All file access must go through check_name() */
1392 status = check_name(conn, fname);
1393 if (!NT_STATUS_IS_OK(status)) {
1394 restore_case_semantics(conn, file_attributes);
1395 return ERROR_NT(status);
1398 /* This is the correct thing to do (check every time) but can_delete is
1399 expensive (it may have to read the parent directory permissions). So
1400 for now we're not doing it unless we have a strong hint the client
1401 is really going to delete this file. If the client is forcing FILE_CREATE
1402 let the filesystem take care of the permissions. */
1404 /* Setting FILE_SHARE_DELETE is the hint. */
1406 if (lp_acl_check_permissions(SNUM(conn))
1407 && (create_disposition != FILE_CREATE)
1408 && (share_access & FILE_SHARE_DELETE)
1409 && (access_mask & DELETE_ACCESS)) {
1410 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
1411 !can_delete_file_in_directory(conn, fname)) {
1412 restore_case_semantics(conn, file_attributes);
1413 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1418 /* We need to support SeSecurityPrivilege for this. */
1419 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY)) &&
1420 !user_has_privileges(current_user.nt_user_token,
1422 restore_case_semantics(conn, file_attributes);
1423 return ERROR_NT(NT_STATUS_PRIVILEGE_NOT_HELD);
1428 pdata = data + sd_len;
1430 /* We have already checked that ea_len <= data_count here. */
1431 ea_list = read_nttrans_ea_list(tmp_talloc_ctx(), pdata,
1434 restore_case_semantics(conn, file_attributes);
1435 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1440 * If it's a request for a directory open, deal with it separately.
1443 if(create_options & FILE_DIRECTORY_FILE) {
1445 /* Can't open a temp directory. IFS kit test. */
1446 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1447 restore_case_semantics(conn, file_attributes);
1448 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1452 * We will get a create directory here if the Win32
1453 * app specified a security descriptor in the
1454 * CreateDirectory() call.
1458 status = open_directory(conn, &req, fname, &sbuf,
1463 new_file_attributes,
1465 if(!NT_STATUS_IS_OK(status)) {
1466 restore_case_semantics(conn, file_attributes);
1467 return ERROR_NT(status);
1473 * Ordinary file case.
1476 status = open_file_ntcreate(conn,&req,fname,&sbuf,
1481 new_file_attributes,
1485 if (!NT_STATUS_IS_OK(status)) {
1486 if (NT_STATUS_EQUAL(status,
1487 NT_STATUS_FILE_IS_A_DIRECTORY)) {
1490 * Fail the open if it was explicitly a non-directory file.
1493 if (create_options & FILE_NON_DIRECTORY_FILE) {
1494 restore_case_semantics(conn, file_attributes);
1495 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1499 status = open_directory(conn, &req, fname,
1505 new_file_attributes,
1507 if(!NT_STATUS_IS_OK(status)) {
1508 restore_case_semantics(conn, file_attributes);
1509 return ERROR_NT(status);
1512 restore_case_semantics(conn, file_attributes);
1513 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1514 /* We have re-scheduled this call. */
1517 return ERROR_NT(status);
1523 * According to the MS documentation, the only time the security
1524 * descriptor is applied to the opened file is iff we *created* the
1525 * file; an existing file stays the same.
1527 * Also, it seems (from observation) that you can open the file with
1528 * any access mask but you can still write the sd. We need to override
1529 * the granted access before we call set_sd
1530 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1533 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1534 uint32 saved_access_mask = fsp->access_mask;
1536 /* We have already checked that sd_len <= data_count here. */
1538 fsp->access_mask = FILE_GENERIC_ALL;
1540 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1541 if (!NT_STATUS_IS_OK(status)) {
1542 talloc_destroy(ctx);
1543 close_file(fsp,ERROR_CLOSE);
1544 restore_case_semantics(conn, file_attributes);
1545 return ERROR_NT(status);
1547 fsp->access_mask = saved_access_mask;
1550 if (ea_len && (info == FILE_WAS_CREATED)) {
1551 status = set_ea(conn, fsp, fname, ea_list);
1552 if (!NT_STATUS_IS_OK(status)) {
1553 close_file(fsp,ERROR_CLOSE);
1554 restore_case_semantics(conn, file_attributes);
1555 return ERROR_NT(status);
1559 restore_case_semantics(conn, file_attributes);
1561 file_len = sbuf.st_size;
1562 fattr = dos_mode(conn,fname,&sbuf);
1564 fattr = FILE_ATTRIBUTE_NORMAL;
1566 if (!fsp->is_directory && (fattr & aDIR)) {
1567 close_file(fsp,ERROR_CLOSE);
1568 return ERROR_DOS(ERRDOS,ERRnoaccess);
1571 /* Save the requested allocation size. */
1572 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1573 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1574 #ifdef LARGE_SMB_OFF_T
1575 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1577 if (allocation_size && (allocation_size > file_len)) {
1578 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1579 if (fsp->is_directory) {
1580 close_file(fsp,ERROR_CLOSE);
1581 /* Can't set allocation size on a directory. */
1582 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1584 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1585 close_file(fsp,ERROR_CLOSE);
1586 return ERROR_NT(NT_STATUS_DISK_FULL);
1589 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1594 * If the caller set the extended oplock request bit
1595 * and we granted one (by whatever means) - set the
1596 * correct bit for extended oplock reply.
1599 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1600 extended_oplock_granted = True;
1603 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1604 extended_oplock_granted = True;
1607 /* Realloc the size of parameters and data we will return */
1608 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1609 /* Extended response is 32 more byyes. */
1614 params = nttrans_realloc(ppparams, param_len);
1615 if(params == NULL) {
1616 return ERROR_DOS(ERRDOS,ERRnomem);
1620 if (extended_oplock_granted) {
1621 if (flags & REQUEST_BATCH_OPLOCK) {
1622 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1624 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1626 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1627 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1629 SCVAL(p,0,NO_OPLOCK_RETURN);
1633 SSVAL(p,0,fsp->fnum);
1635 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1636 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1643 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1644 a_timespec = get_atimespec(&sbuf);
1645 m_timespec = get_mtimespec(&sbuf);
1647 if (lp_dos_filetime_resolution(SNUM(conn))) {
1648 dos_filetime_timespec(&c_timespec);
1649 dos_filetime_timespec(&a_timespec);
1650 dos_filetime_timespec(&m_timespec);
1653 put_long_date_timespec(p, c_timespec); /* create time. */
1655 put_long_date_timespec(p, a_timespec); /* access time */
1657 put_long_date_timespec(p, m_timespec); /* write time */
1659 put_long_date_timespec(p, m_timespec); /* change time */
1661 SIVAL(p,0,fattr); /* File Attributes. */
1663 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1665 SOFF_T(p,0,file_len);
1667 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1671 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1673 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1676 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1677 perms = FILE_GENERIC_ALL;
1679 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1684 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1686 /* Send the required number of replies */
1687 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, *ppdata, 0);
1692 /****************************************************************************
1693 Reply to a NT CANCEL request.
1694 conn POINTER CAN BE NULL HERE !
1695 ****************************************************************************/
1697 int reply_ntcancel(connection_struct *conn,
1698 char *inbuf,char *outbuf,int length,int bufsize)
1701 * Go through and cancel any pending change notifies.
1704 int mid = SVAL(inbuf,smb_mid);
1705 START_PROFILE(SMBntcancel);
1706 remove_pending_change_notify_requests_by_mid(mid);
1707 remove_pending_lock_requests_by_mid(mid);
1708 srv_cancel_sign_response(mid);
1710 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1712 END_PROFILE(SMBntcancel);
1716 /****************************************************************************
1718 ****************************************************************************/
1720 static NTSTATUS copy_internals(connection_struct *conn,
1721 struct smb_request *req,
1722 char *oldname, char *newname, uint32 attrs)
1724 SMB_STRUCT_STAT sbuf1, sbuf2;
1725 pstring last_component_oldname;
1726 pstring last_component_newname;
1727 files_struct *fsp1,*fsp2;
1731 NTSTATUS status = NT_STATUS_OK;
1736 if (!CAN_WRITE(conn)) {
1737 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1740 status = unix_convert(conn, oldname, False, last_component_oldname, &sbuf1);
1741 if (!NT_STATUS_IS_OK(status)) {
1745 status = check_name(conn, oldname);
1746 if (!NT_STATUS_IS_OK(status)) {
1750 /* Source must already exist. */
1751 if (!VALID_STAT(sbuf1)) {
1752 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1754 /* Ensure attributes match. */
1755 fattr = dos_mode(conn,oldname,&sbuf1);
1756 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1757 return NT_STATUS_NO_SUCH_FILE;
1760 status = unix_convert(conn, newname, False, last_component_newname, &sbuf2);
1761 if (!NT_STATUS_IS_OK(status)) {
1765 status = check_name(conn, newname);
1766 if (!NT_STATUS_IS_OK(status)) {
1770 /* Disallow if newname already exists. */
1771 if (VALID_STAT(sbuf2)) {
1772 return NT_STATUS_OBJECT_NAME_COLLISION;
1775 /* No links from a directory. */
1776 if (S_ISDIR(sbuf1.st_mode)) {
1777 return NT_STATUS_FILE_IS_A_DIRECTORY;
1780 /* Ensure this is within the share. */
1781 status = check_reduced_name(conn, oldname);
1782 if (!NT_STATUS_IS_OK(status)) {
1786 DEBUG(10,("copy_internals: doing file copy %s to %s\n", oldname, newname));
1788 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1789 FILE_READ_DATA, /* Read-only. */
1790 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1792 0, /* No create options. */
1793 FILE_ATTRIBUTE_NORMAL,
1797 if (!NT_STATUS_IS_OK(status)) {
1801 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1802 FILE_WRITE_DATA, /* Read-only. */
1803 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1805 0, /* No create options. */
1810 if (!NT_STATUS_IS_OK(status)) {
1811 close_file(fsp1,ERROR_CLOSE);
1815 if (sbuf1.st_size) {
1816 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1820 * As we are opening fsp1 read-only we only expect
1821 * an error on close on fsp2 if we are out of space.
1822 * Thus we don't look at the error return from the
1825 close_file(fsp1,NORMAL_CLOSE);
1827 /* Ensure the modtime is set correctly on the destination file. */
1828 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1830 status = close_file(fsp2,NORMAL_CLOSE);
1832 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1833 creates the file. This isn't the correct thing to do in the copy
1835 file_set_dosmode(conn, newname, fattr, &sbuf2,
1836 parent_dirname(newname));
1838 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1839 return NT_STATUS_DISK_FULL;
1842 if (!NT_STATUS_IS_OK(status)) {
1843 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1844 nt_errstr(status), oldname, newname));
1849 /****************************************************************************
1850 Reply to a NT rename request.
1851 ****************************************************************************/
1853 int reply_ntrename(connection_struct *conn,
1854 char *inbuf,char *outbuf,int length,int bufsize)
1861 BOOL src_has_wcard = False;
1862 BOOL dest_has_wcard = False;
1863 uint32 attrs = SVAL(inbuf,smb_vwv0);
1864 uint16 rename_type = SVAL(inbuf,smb_vwv1);
1865 struct smb_request req;
1867 START_PROFILE(SMBntrename);
1869 init_smb_request(&req, (uint8 *)inbuf);
1871 p = smb_buf(inbuf) + 1;
1872 p += srvstr_get_path_wcard(inbuf, SVAL(inbuf,smb_flg2), oldname, p,
1873 sizeof(oldname), 0, STR_TERMINATE, &status,
1875 if (!NT_STATUS_IS_OK(status)) {
1876 END_PROFILE(SMBntrename);
1877 return ERROR_NT(status);
1880 if( is_ntfs_stream_name(oldname)) {
1881 /* Can't rename a stream. */
1882 END_PROFILE(SMBntrename);
1883 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1886 if (ms_has_wild(oldname)) {
1887 END_PROFILE(SMBntrename);
1888 return ERROR_NT(NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1892 p += srvstr_get_path_wcard(inbuf, SVAL(inbuf,smb_flg2), newname, p,
1893 sizeof(newname), 0, STR_TERMINATE, &status,
1895 if (!NT_STATUS_IS_OK(status)) {
1896 END_PROFILE(SMBntrename);
1897 return ERROR_NT(status);
1900 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, oldname);
1901 if (!NT_STATUS_IS_OK(status)) {
1902 END_PROFILE(SMBntrename);
1903 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1904 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1906 return ERROR_NT(status);
1909 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, newname);
1910 if (!NT_STATUS_IS_OK(status)) {
1911 END_PROFILE(SMBntrename);
1912 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1913 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1915 return ERROR_NT(status);
1918 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1920 switch(rename_type) {
1921 case RENAME_FLAG_RENAME:
1922 status = rename_internals(conn, &req, oldname, newname,
1923 attrs, False, src_has_wcard,
1926 case RENAME_FLAG_HARD_LINK:
1927 if (src_has_wcard || dest_has_wcard) {
1929 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1931 status = hardlink_internals(conn, oldname, newname);
1934 case RENAME_FLAG_COPY:
1935 if (src_has_wcard || dest_has_wcard) {
1937 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1939 status = copy_internals(conn, &req, oldname,
1943 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1944 status = NT_STATUS_INVALID_PARAMETER;
1947 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1951 if (!NT_STATUS_IS_OK(status)) {
1952 END_PROFILE(SMBntrename);
1953 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1954 /* We have re-scheduled this call. */
1957 return ERROR_NT(status);
1960 outsize = set_message(inbuf,outbuf,0,0,False);
1962 END_PROFILE(SMBntrename);
1966 /****************************************************************************
1967 Reply to a notify change - queue the request and
1968 don't allow a directory to be opened.
1969 ****************************************************************************/
1971 static int call_nt_transact_notify_change(connection_struct *conn, char *inbuf,
1972 char *outbuf, int length,
1974 uint16 **ppsetup, uint32 setup_count,
1976 uint32 parameter_count,
1977 char **ppdata, uint32 data_count,
1978 uint32 max_data_count,
1979 uint32 max_param_count)
1981 uint16 *setup = *ppsetup;
1987 if(setup_count < 6) {
1988 return ERROR_DOS(ERRDOS,ERRbadfunc);
1991 fsp = file_fsp((char *)setup,4);
1992 filter = IVAL(setup, 0);
1993 recursive = (SVAL(setup, 6) != 0) ? True : False;
1995 DEBUG(3,("call_nt_transact_notify_change\n"));
1998 return ERROR_DOS(ERRDOS,ERRbadfid);
2002 char *filter_string;
2004 if (!(filter_string = notify_filter_string(NULL, filter))) {
2005 return ERROR_NT(NT_STATUS_NO_MEMORY);
2008 DEBUG(3,("call_nt_transact_notify_change: notify change "
2009 "called on %s, filter = %s, recursive = %d\n",
2010 fsp->fsp_name, filter_string, recursive));
2012 TALLOC_FREE(filter_string);
2015 if((!fsp->is_directory) || (conn != fsp->conn)) {
2016 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2019 if (fsp->notify == NULL) {
2021 status = change_notify_create(fsp, filter, recursive);
2023 if (!NT_STATUS_IS_OK(status)) {
2024 DEBUG(10, ("change_notify_create returned %s\n",
2025 nt_errstr(status)));
2026 return ERROR_NT(status);
2030 if (fsp->notify->num_changes != 0) {
2033 * We've got changes pending, respond immediately
2037 * TODO: write a torture test to check the filtering behaviour
2041 change_notify_reply(inbuf, fsp->notify);
2044 * change_notify_reply() above has independently sent its
2051 * No changes pending, queue the request
2054 status = change_notify_add_request(inbuf, filter, recursive, fsp);
2055 if (!NT_STATUS_IS_OK(status)) {
2056 return ERROR_NT(status);
2062 /****************************************************************************
2063 Reply to an NT transact rename command.
2064 ****************************************************************************/
2066 static int call_nt_transact_rename(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2067 uint16 **ppsetup, uint32 setup_count,
2068 char **ppparams, uint32 parameter_count,
2069 char **ppdata, uint32 data_count, uint32 max_data_count)
2071 char *params = *ppparams;
2073 files_struct *fsp = NULL;
2074 BOOL replace_if_exists = False;
2075 BOOL dest_has_wcard = False;
2077 struct smb_request req;
2079 init_smb_request(&req, (uint8 *)inbuf);
2081 if(parameter_count < 5) {
2082 return ERROR_DOS(ERRDOS,ERRbadfunc);
2085 fsp = file_fsp(params, 0);
2086 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2087 CHECK_FSP(fsp, conn);
2088 srvstr_get_path_wcard(inbuf, SVAL(inbuf,smb_flg2), new_name, params+4,
2089 sizeof(new_name), parameter_count - 4,
2090 STR_TERMINATE, &status, &dest_has_wcard);
2091 if (!NT_STATUS_IS_OK(status)) {
2092 return ERROR_NT(status);
2095 status = rename_internals(conn, &req, fsp->fsp_name,
2096 new_name, 0, replace_if_exists, False, dest_has_wcard);
2098 if (!NT_STATUS_IS_OK(status)) {
2099 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
2100 /* We have re-scheduled this call. */
2103 return ERROR_NT(status);
2107 * Rename was successful.
2109 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2111 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2112 fsp->fsp_name, new_name));
2117 /******************************************************************************
2118 Fake up a completely empty SD.
2119 *******************************************************************************/
2121 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2125 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2127 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2134 /****************************************************************************
2135 Reply to query a security descriptor.
2136 ****************************************************************************/
2138 static int call_nt_transact_query_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2139 uint16 **ppsetup, uint32 setup_count,
2140 char **ppparams, uint32 parameter_count,
2141 char **ppdata, uint32 data_count, uint32 max_data_count)
2143 char *params = *ppparams;
2144 char *data = *ppdata;
2146 SEC_DESC *psd = NULL;
2148 uint32 security_info_wanted;
2149 TALLOC_CTX *mem_ctx;
2150 files_struct *fsp = NULL;
2152 if(parameter_count < 8) {
2153 return ERROR_DOS(ERRDOS,ERRbadfunc);
2156 fsp = file_fsp(params,0);
2158 return ERROR_DOS(ERRDOS,ERRbadfid);
2161 security_info_wanted = IVAL(params,4);
2163 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2164 (unsigned int)security_info_wanted ));
2166 params = nttrans_realloc(ppparams, 4);
2167 if(params == NULL) {
2168 return ERROR_DOS(ERRDOS,ERRnomem);
2171 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2172 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2173 return ERROR_DOS(ERRDOS,ERRnomem);
2177 * Get the permissions to return.
2180 if (!lp_nt_acl_support(SNUM(conn))) {
2181 sd_size = get_null_nt_acl(mem_ctx, &psd);
2183 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
2187 talloc_destroy(mem_ctx);
2188 return(UNIXERROR(ERRDOS,ERRnoaccess));
2191 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2193 SIVAL(params,0,(uint32)sd_size);
2195 if(max_data_count < sd_size) {
2197 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
2198 params, 4, *ppdata, 0);
2199 talloc_destroy(mem_ctx);
2204 * Allocate the data we will point this at.
2207 data = nttrans_realloc(ppdata, sd_size);
2209 talloc_destroy(mem_ctx);
2210 return ERROR_DOS(ERRDOS,ERRnomem);
2214 * Init the parse struct we will marshall into.
2217 prs_init(&pd, 0, mem_ctx, MARSHALL);
2220 * Setup the prs_struct to point at the memory we just
2224 prs_give_memory( &pd, data, (uint32)sd_size, False);
2227 * Finally, linearize into the outgoing buffer.
2230 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2231 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2232 security descriptor.\n"));
2234 * Return access denied for want of a better error message..
2236 talloc_destroy(mem_ctx);
2237 return(UNIXERROR(ERRDOS,ERRnoaccess));
2241 * Now we can delete the security descriptor.
2244 talloc_destroy(mem_ctx);
2246 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data,
2251 /****************************************************************************
2252 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2253 ****************************************************************************/
2255 static int call_nt_transact_set_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2256 uint16 **ppsetup, uint32 setup_count,
2257 char **ppparams, uint32 parameter_count,
2258 char **ppdata, uint32 data_count, uint32 max_data_count)
2260 char *params= *ppparams;
2261 char *data = *ppdata;
2262 files_struct *fsp = NULL;
2263 uint32 security_info_sent = 0;
2266 if(parameter_count < 8) {
2267 return ERROR_DOS(ERRDOS,ERRbadfunc);
2270 if((fsp = file_fsp(params,0)) == NULL) {
2271 return ERROR_DOS(ERRDOS,ERRbadfid);
2274 if(!lp_nt_acl_support(SNUM(conn))) {
2278 security_info_sent = IVAL(params,4);
2280 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2281 (unsigned int)security_info_sent ));
2283 if (data_count == 0) {
2284 return ERROR_DOS(ERRDOS, ERRnoaccess);
2287 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2288 return ERROR_NT(nt_status);
2293 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2297 /****************************************************************************
2299 ****************************************************************************/
2301 static int call_nt_transact_ioctl(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2302 uint16 **ppsetup, uint32 setup_count,
2303 char **ppparams, uint32 parameter_count,
2304 char **ppdata, uint32 data_count, uint32 max_data_count)
2311 static BOOL logged_message;
2312 char *pdata = *ppdata;
2314 if (setup_count != 8) {
2315 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2316 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2319 function = IVAL(*ppsetup, 0);
2320 fidnum = SVAL(*ppsetup, 4);
2321 isFSctl = CVAL(*ppsetup, 6);
2322 compfilter = CVAL(*ppsetup, 7);
2324 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2325 function, fidnum, isFSctl, compfilter));
2327 fsp=file_fsp((char *)*ppsetup, 4);
2328 /* this check is done in each implemented function case for now
2329 because I don't want to break anything... --metze
2330 FSP_BELONGS_CONN(fsp,conn);*/
2333 case FSCTL_SET_SPARSE:
2334 /* pretend this succeeded - tho strictly we should
2335 mark the file sparse (if the local fs supports it)
2336 so we can know if we need to pre-allocate or not */
2338 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2339 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL,
2343 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2345 unsigned char objid[16];
2347 /* This should return the object-id on this file.
2348 * I think I'll make this be the inode+dev. JRA.
2351 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2354 pdata = nttrans_realloc(ppdata, data_count);
2355 if (pdata == NULL) {
2356 return ERROR_NT(NT_STATUS_NO_MEMORY);
2358 push_file_id_16(pdata, &fsp->file_id);
2359 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2360 push_file_id_16(pdata+32, &fsp->file_id);
2361 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, pdata, data_count);
2365 case FSCTL_GET_REPARSE_POINT:
2366 /* pretend this fail - my winXP does it like this
2370 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2371 return ERROR_NT(NT_STATUS_NOT_A_REPARSE_POINT);
2373 case FSCTL_SET_REPARSE_POINT:
2374 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2378 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2379 return ERROR_NT(NT_STATUS_NOT_A_REPARSE_POINT);
2381 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2384 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2385 * and return their volume names. If max_data_count is 16, then it is just
2386 * asking for the number of volumes and length of the combined names.
2388 * pdata is the data allocated by our caller, but that uses
2389 * total_data_count (which is 0 in our case) rather than max_data_count.
2390 * Allocate the correct amount and return the pointer to let
2391 * it be deallocated when we return.
2393 SHADOW_COPY_DATA *shadow_data = NULL;
2394 TALLOC_CTX *shadow_mem_ctx = NULL;
2395 BOOL labels = False;
2396 uint32 labels_data_count = 0;
2400 FSP_BELONGS_CONN(fsp,conn);
2402 if (max_data_count < 16) {
2403 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2405 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2408 if (max_data_count > 16) {
2412 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2413 if (shadow_mem_ctx == NULL) {
2414 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2415 return ERROR_NT(NT_STATUS_NO_MEMORY);
2418 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2419 if (shadow_data == NULL) {
2420 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2421 talloc_destroy(shadow_mem_ctx);
2422 return ERROR_NT(NT_STATUS_NO_MEMORY);
2425 shadow_data->mem_ctx = shadow_mem_ctx;
2428 * Call the VFS routine to actually do the work.
2430 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2431 talloc_destroy(shadow_data->mem_ctx);
2432 if (errno == ENOSYS) {
2433 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2434 conn->connectpath));
2435 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2437 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2438 conn->connectpath));
2439 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
2443 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2448 data_count = 12+labels_data_count+4;
2451 if (max_data_count<data_count) {
2452 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2453 max_data_count,data_count));
2454 talloc_destroy(shadow_data->mem_ctx);
2455 return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
2458 pdata = nttrans_realloc(ppdata, data_count);
2459 if (pdata == NULL) {
2460 talloc_destroy(shadow_data->mem_ctx);
2461 return ERROR_NT(NT_STATUS_NO_MEMORY);
2466 /* num_volumes 4 bytes */
2467 SIVAL(pdata,0,shadow_data->num_volumes);
2470 /* num_labels 4 bytes */
2471 SIVAL(pdata,4,shadow_data->num_volumes);
2474 /* needed_data_count 4 bytes */
2475 SIVAL(pdata,8,labels_data_count);
2479 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2480 shadow_data->num_volumes,fsp->fsp_name));
2481 if (labels && shadow_data->labels) {
2482 for (i=0;i<shadow_data->num_volumes;i++) {
2483 srvstr_push(outbuf, cur_pdata, shadow_data->labels[i], 2*sizeof(SHADOW_COPY_LABEL), STR_UNICODE|STR_TERMINATE);
2484 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2485 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2489 talloc_destroy(shadow_data->mem_ctx);
2491 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0,
2497 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2499 /* pretend this succeeded -
2501 * we have to send back a list with all files owned by this SID
2503 * but I have to check that --metze
2507 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2509 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2511 FSP_BELONGS_CONN(fsp,conn);
2513 /* unknown 4 bytes: this is not the length of the sid :-( */
2514 /*unknown = IVAL(pdata,0);*/
2516 sid_parse(pdata+4,sid_len,&sid);
2517 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2519 if (!sid_to_uid(&sid, &uid)) {
2520 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2521 sid_string_static(&sid),(unsigned long)sid_len));
2525 /* we can take a look at the find source :-)
2527 * find ./ -uid $uid -name '*' is what we need here
2530 * and send 4bytes len and then NULL terminated unicode strings
2533 * but I don't know how to deal with the paged results
2534 * (maybe we can hang the result anywhere in the fsp struct)
2536 * we don't send all files at once
2537 * and at the next we should *not* start from the beginning,
2538 * so we have to cache the result
2543 /* this works for now... */
2544 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0,
2549 if (!logged_message) {
2550 logged_message = True; /* Only print this once... */
2551 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2556 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2560 #ifdef HAVE_SYS_QUOTAS
2561 /****************************************************************************
2562 Reply to get user quota
2563 ****************************************************************************/
2565 static int call_nt_transact_get_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2566 uint16 **ppsetup, uint32 setup_count,
2567 char **ppparams, uint32 parameter_count,
2568 char **ppdata, uint32 data_count, uint32 max_data_count)
2570 NTSTATUS nt_status = NT_STATUS_OK;
2571 char *params = *ppparams;
2572 char *pdata = *ppdata;
2574 int data_len=0,param_len=0;
2577 files_struct *fsp = NULL;
2581 BOOL start_enum = True;
2582 SMB_NTQUOTA_STRUCT qt;
2583 SMB_NTQUOTA_LIST *tmp_list;
2584 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2589 if (current_user.ut.uid != 0) {
2590 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2591 lp_servicename(SNUM(conn)),conn->user));
2592 return ERROR_DOS(ERRDOS,ERRnoaccess);
2596 * Ensure minimum number of parameters sent.
2599 if (parameter_count < 4) {
2600 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2601 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2604 /* maybe we can check the quota_fnum */
2605 fsp = file_fsp(params,0);
2606 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2607 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2608 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2611 /* the NULL pointer checking for fsp->fake_file_handle->pd
2612 * is done by CHECK_NTQUOTA_HANDLE_OK()
2614 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2616 level = SVAL(params,2);
2618 /* unknown 12 bytes leading in params */
2621 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2622 /* seems that we should continue with the enum here --metze */
2624 if (qt_handle->quota_list!=NULL &&
2625 qt_handle->tmp_list==NULL) {
2628 free_ntquota_list(&(qt_handle->quota_list));
2630 /* Realloc the size of parameters and data we will return */
2632 params = nttrans_realloc(ppparams, param_len);
2633 if(params == NULL) {
2634 return ERROR_DOS(ERRDOS,ERRnomem);
2638 SIVAL(params,0,data_len);
2645 case TRANSACT_GET_USER_QUOTA_LIST_START:
2647 if (qt_handle->quota_list==NULL &&
2648 qt_handle->tmp_list==NULL) {
2652 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
2653 return ERROR_DOS(ERRSRV,ERRerror);
2655 /* Realloc the size of parameters and data we will return */
2657 params = nttrans_realloc(ppparams, param_len);
2658 if(params == NULL) {
2659 return ERROR_DOS(ERRDOS,ERRnomem);
2662 /* we should not trust the value in max_data_count*/
2663 max_data_count = MIN(max_data_count,2048);
2665 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2667 return ERROR_DOS(ERRDOS,ERRnomem);
2672 /* set params Size of returned Quota Data 4 bytes*/
2673 /* but set it later when we know it */
2675 /* for each entry push the data */
2678 qt_handle->tmp_list = qt_handle->quota_list;
2681 tmp_list = qt_handle->tmp_list;
2683 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2684 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2686 sid_len = sid_size(&tmp_list->quotas->sid);
2687 entry_len = 40 + sid_len;
2689 /* nextoffset entry 4 bytes */
2690 SIVAL(entry,0,entry_len);
2692 /* then the len of the SID 4 bytes */
2693 SIVAL(entry,4,sid_len);
2695 /* unknown data 8 bytes SMB_BIG_UINT */
2696 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2698 /* the used disk space 8 bytes SMB_BIG_UINT */
2699 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2701 /* the soft quotas 8 bytes SMB_BIG_UINT */
2702 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2704 /* the hard quotas 8 bytes SMB_BIG_UINT */
2705 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2707 /* and now the SID */
2708 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2711 qt_handle->tmp_list = tmp_list;
2713 /* overwrite the offset of the last entry */
2714 SIVAL(entry-entry_len,0,0);
2716 data_len = 4+qt_len;
2717 /* overwrite the params quota_data_len */
2718 SIVAL(params,0,data_len);
2722 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2724 /* unknown 4 bytes IVAL(pdata,0) */
2726 if (data_count < 8) {
2727 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2728 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2731 sid_len = IVAL(pdata,4);
2732 /* Ensure this is less than 1mb. */
2733 if (sid_len > (1024*1024)) {
2734 return ERROR_DOS(ERRDOS,ERRnomem);
2737 if (data_count < 8+sid_len) {
2738 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2739 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2742 data_len = 4+40+sid_len;
2744 if (max_data_count < data_len) {
2745 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2746 max_data_count, data_len));
2748 SIVAL(params,0,data_len);
2750 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2754 sid_parse(pdata+8,sid_len,&sid);
2756 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2759 * we have to return zero's in all fields
2760 * instead of returning an error here
2765 /* Realloc the size of parameters and data we will return */
2767 params = nttrans_realloc(ppparams, param_len);
2768 if(params == NULL) {
2769 return ERROR_DOS(ERRDOS,ERRnomem);
2772 pdata = nttrans_realloc(ppdata, data_len);
2774 return ERROR_DOS(ERRDOS,ERRnomem);
2779 /* set params Size of returned Quota Data 4 bytes*/
2780 SIVAL(params,0,data_len);
2782 /* nextoffset entry 4 bytes */
2785 /* then the len of the SID 4 bytes */
2786 SIVAL(entry,4,sid_len);
2788 /* unknown data 8 bytes SMB_BIG_UINT */
2789 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2791 /* the used disk space 8 bytes SMB_BIG_UINT */
2792 SBIG_UINT(entry,16,qt.usedspace);
2794 /* the soft quotas 8 bytes SMB_BIG_UINT */
2795 SBIG_UINT(entry,24,qt.softlim);
2797 /* the hard quotas 8 bytes SMB_BIG_UINT */
2798 SBIG_UINT(entry,32,qt.hardlim);
2800 /* and now the SID */
2801 sid_linearize(entry+40, sid_len, &sid);
2806 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2807 return ERROR_DOS(ERRSRV,ERRerror);
2811 send_nt_replies(inbuf, outbuf, bufsize, nt_status, params, param_len,
2817 /****************************************************************************
2818 Reply to set user quota
2819 ****************************************************************************/
2821 static int call_nt_transact_set_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2822 uint16 **ppsetup, uint32 setup_count,
2823 char **ppparams, uint32 parameter_count,
2824 char **ppdata, uint32 data_count, uint32 max_data_count)
2826 char *params = *ppparams;
2827 char *pdata = *ppdata;
2828 int data_len=0,param_len=0;
2829 SMB_NTQUOTA_STRUCT qt;
2832 files_struct *fsp = NULL;
2837 if (current_user.ut.uid != 0) {
2838 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2839 lp_servicename(SNUM(conn)),conn->user));
2840 return ERROR_DOS(ERRDOS,ERRnoaccess);
2844 * Ensure minimum number of parameters sent.
2847 if (parameter_count < 2) {
2848 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2849 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2852 /* maybe we can check the quota_fnum */
2853 fsp = file_fsp(params,0);
2854 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2855 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2856 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2859 if (data_count < 40) {
2860 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2861 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2864 /* offset to next quota record.
2865 * 4 bytes IVAL(pdata,0)
2870 sid_len = IVAL(pdata,4);
2872 if (data_count < 40+sid_len) {
2873 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2874 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2877 /* unknown 8 bytes in pdata
2878 * maybe its the change time in NTTIME
2881 /* the used space 8 bytes (SMB_BIG_UINT)*/
2882 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2883 #ifdef LARGE_SMB_OFF_T
2884 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2885 #else /* LARGE_SMB_OFF_T */
2886 if ((IVAL(pdata,20) != 0)&&
2887 ((qt.usedspace != 0xFFFFFFFF)||
2888 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2889 /* more than 32 bits? */
2890 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2892 #endif /* LARGE_SMB_OFF_T */
2894 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2895 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2896 #ifdef LARGE_SMB_OFF_T
2897 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2898 #else /* LARGE_SMB_OFF_T */
2899 if ((IVAL(pdata,28) != 0)&&
2900 ((qt.softlim != 0xFFFFFFFF)||
2901 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2902 /* more than 32 bits? */
2903 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2905 #endif /* LARGE_SMB_OFF_T */
2907 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2908 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2909 #ifdef LARGE_SMB_OFF_T
2910 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2911 #else /* LARGE_SMB_OFF_T */
2912 if ((IVAL(pdata,36) != 0)&&
2913 ((qt.hardlim != 0xFFFFFFFF)||
2914 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2915 /* more than 32 bits? */
2916 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2918 #endif /* LARGE_SMB_OFF_T */
2920 sid_parse(pdata+40,sid_len,&sid);
2921 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2923 /* 44 unknown bytes left... */
2925 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2926 return ERROR_DOS(ERRSRV,ERRerror);
2929 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len,
2934 #endif /* HAVE_SYS_QUOTAS */
2936 static int handle_nttrans(connection_struct *conn,
2937 struct trans_state *state,
2938 char *inbuf, char *outbuf, int size, int bufsize)
2942 if (Protocol >= PROTOCOL_NT1) {
2943 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | 0x40); /* IS_LONG_NAME */
2946 /* Now we must call the relevant NT_TRANS function */
2947 switch(state->call) {
2948 case NT_TRANSACT_CREATE:
2950 START_PROFILE(NT_transact_create);
2951 outsize = call_nt_transact_create(conn, inbuf, outbuf,
2953 &state->setup, state->setup_count,
2954 &state->param, state->total_param,
2955 &state->data, state->total_data,
2956 state->max_data_return);
2957 END_PROFILE(NT_transact_create);
2961 case NT_TRANSACT_IOCTL:
2963 START_PROFILE(NT_transact_ioctl);
2964 outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
2966 &state->setup, state->setup_count,
2967 &state->param, state->total_param,
2968 &state->data, state->total_data, state->max_data_return);
2969 END_PROFILE(NT_transact_ioctl);
2973 case NT_TRANSACT_SET_SECURITY_DESC:
2975 START_PROFILE(NT_transact_set_security_desc);
2976 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
2978 &state->setup, state->setup_count,
2979 &state->param, state->total_param,
2980 &state->data, state->total_data, state->max_data_return);
2981 END_PROFILE(NT_transact_set_security_desc);
2985 case NT_TRANSACT_NOTIFY_CHANGE:
2987 START_PROFILE(NT_transact_notify_change);
2988 outsize = call_nt_transact_notify_change(
2989 conn, inbuf, outbuf, size, bufsize,
2990 &state->setup, state->setup_count,
2991 &state->param, state->total_param,
2992 &state->data, state->total_data,
2993 state->max_data_return,
2994 state->max_param_return);
2995 END_PROFILE(NT_transact_notify_change);
2999 case NT_TRANSACT_RENAME:
3001 START_PROFILE(NT_transact_rename);
3002 outsize = call_nt_transact_rename(conn, inbuf, outbuf,
3004 &state->setup, state->setup_count,
3005 &state->param, state->total_param,
3006 &state->data, state->total_data, state->max_data_return);
3007 END_PROFILE(NT_transact_rename);
3011 case NT_TRANSACT_QUERY_SECURITY_DESC:
3013 START_PROFILE(NT_transact_query_security_desc);
3014 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
3016 &state->setup, state->setup_count,
3017 &state->param, state->total_param,
3018 &state->data, state->total_data, state->max_data_return);
3019 END_PROFILE(NT_transact_query_security_desc);
3023 #ifdef HAVE_SYS_QUOTAS
3024 case NT_TRANSACT_GET_USER_QUOTA:
3026 START_PROFILE(NT_transact_get_user_quota);
3027 outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf,
3029 &state->setup, state->setup_count,
3030 &state->param, state->total_param,
3031 &state->data, state->total_data, state->max_data_return);
3032 END_PROFILE(NT_transact_get_user_quota);
3036 case NT_TRANSACT_SET_USER_QUOTA:
3038 START_PROFILE(NT_transact_set_user_quota);
3039 outsize = call_nt_transact_set_user_quota(conn, inbuf, outbuf,
3041 &state->setup, state->setup_count,
3042 &state->param, state->total_param,
3043 &state->data, state->total_data, state->max_data_return);
3044 END_PROFILE(NT_transact_set_user_quota);
3047 #endif /* HAVE_SYS_QUOTAS */
3050 /* Error in request */
3051 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n",
3053 return ERROR_DOS(ERRSRV,ERRerror);
3058 /****************************************************************************
3059 Reply to a SMBNTtrans.
3060 ****************************************************************************/
3062 int reply_nttrans(connection_struct *conn,
3063 char *inbuf,char *outbuf,int size,int bufsize)
3066 uint32 pscnt = IVAL(inbuf,smb_nt_ParameterCount);
3067 uint32 psoff = IVAL(inbuf,smb_nt_ParameterOffset);
3068 uint32 dscnt = IVAL(inbuf,smb_nt_DataCount);
3069 uint32 dsoff = IVAL(inbuf,smb_nt_DataOffset);
3071 uint16 function_code = SVAL( inbuf, smb_nt_Function);
3073 struct trans_state *state;
3075 START_PROFILE(SMBnttrans);
3077 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3078 END_PROFILE(SMBnttrans);
3079 return ERROR_DOS(ERRSRV,ERRaccess);
3082 result = allow_new_trans(conn->pending_trans, SVAL(inbuf, smb_mid));
3083 if (!NT_STATUS_IS_OK(result)) {
3084 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3085 END_PROFILE(SMBnttrans);
3086 return ERROR_NT(result);
3089 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3090 END_PROFILE(SMBnttrans);
3091 return ERROR_DOS(ERRSRV,ERRaccess);
3094 state->cmd = SMBnttrans;
3096 state->mid = SVAL(inbuf,smb_mid);
3097 state->vuid = SVAL(inbuf,smb_uid);
3098 state->total_data = IVAL(inbuf, smb_nt_TotalDataCount);
3100 state->total_param = IVAL(inbuf, smb_nt_TotalParameterCount);
3101 state->param = NULL;
3102 state->max_data_return = IVAL(inbuf,smb_nt_MaxDataCount);
3103 state->max_param_return = IVAL(inbuf,smb_nt_MaxParameterCount);
3105 /* setup count is in *words* */
3106 state->setup_count = 2*CVAL(inbuf,smb_nt_SetupCount);
3107 state->setup = NULL;
3108 state->call = function_code;
3111 * All nttrans messages we handle have smb_wct == 19 +
3112 * state->setup_count. Ensure this is so as a sanity check.
3115 if(CVAL(inbuf, smb_wct) != 19 + (state->setup_count/2)) {
3116 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3117 CVAL(inbuf, smb_wct), 19 + (state->setup_count/2)));
3121 /* Don't allow more than 128mb for each value. */
3122 if ((state->total_data > (1024*1024*128)) ||
3123 (state->total_param > (1024*1024*128))) {
3124 END_PROFILE(SMBnttrans);
3125 return ERROR_DOS(ERRDOS,ERRnomem);
3128 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3131 if (state->total_data) {
3132 /* Can't use talloc here, the core routines do realloc on the
3133 * params and data. */
3134 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3135 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3136 "bytes !\n", (unsigned int)state->total_data));
3138 END_PROFILE(SMBnttrans);
3139 return(ERROR_DOS(ERRDOS,ERRnomem));
3141 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3143 if ((smb_base(inbuf)+dsoff+dscnt > inbuf + size) ||
3144 (smb_base(inbuf)+dsoff+dscnt < smb_base(inbuf)))
3147 memcpy(state->data,smb_base(inbuf)+dsoff,dscnt);
3150 if (state->total_param) {
3151 /* Can't use talloc here, the core routines do realloc on the
3152 * params and data. */
3153 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3154 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3155 "bytes !\n", (unsigned int)state->total_param));
3156 SAFE_FREE(state->data);
3158 END_PROFILE(SMBnttrans);
3159 return(ERROR_DOS(ERRDOS,ERRnomem));
3161 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3163 if ((smb_base(inbuf)+psoff+pscnt > inbuf + size) ||
3164 (smb_base(inbuf)+psoff+pscnt < smb_base(inbuf)))
3167 memcpy(state->param,smb_base(inbuf)+psoff,pscnt);
3170 state->received_data = dscnt;
3171 state->received_param = pscnt;
3173 if(state->setup_count > 0) {
3174 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3175 state->setup_count));
3176 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3177 if (state->setup == NULL) {
3178 DEBUG(0,("reply_nttrans : Out of memory\n"));
3179 SAFE_FREE(state->data);
3180 SAFE_FREE(state->param);
3182 END_PROFILE(SMBnttrans);
3183 return ERROR_DOS(ERRDOS,ERRnomem);
3186 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3187 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3190 if (smb_nt_SetupStart + state->setup_count > size) {
3194 memcpy( state->setup, &inbuf[smb_nt_SetupStart], state->setup_count);
3195 dump_data(10, (uint8 *)state->setup, state->setup_count);
3198 if ((state->received_data == state->total_data) &&
3199 (state->received_param == state->total_param)) {
3200 outsize = handle_nttrans(conn, state, inbuf, outbuf,
3202 SAFE_FREE(state->param);
3203 SAFE_FREE(state->data);
3205 END_PROFILE(SMBnttrans);
3209 DLIST_ADD(conn->pending_trans, state);
3211 /* We need to send an interim response then receive the rest
3212 of the parameter/data bytes */
3213 outsize = set_message(inbuf,outbuf,0,0,False);
3215 END_PROFILE(SMBnttrans);
3220 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3221 SAFE_FREE(state->data);
3222 SAFE_FREE(state->param);
3224 END_PROFILE(SMBnttrans);
3225 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
3228 /****************************************************************************
3229 Reply to a SMBnttranss
3230 ****************************************************************************/
3232 int reply_nttranss(connection_struct *conn, char *inbuf,char *outbuf,
3233 int size,int bufsize)
3236 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3237 struct trans_state *state;
3239 START_PROFILE(SMBnttranss);
3243 for (state = conn->pending_trans; state != NULL;
3244 state = state->next) {
3245 if (state->mid == SVAL(inbuf,smb_mid)) {
3250 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3251 END_PROFILE(SMBnttranss);
3252 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
3255 /* Revise state->total_param and state->total_data in case they have
3256 changed downwards */
3257 if (IVAL(inbuf, smb_nts_TotalParameterCount) < state->total_param) {
3258 state->total_param = IVAL(inbuf, smb_nts_TotalParameterCount);
3260 if (IVAL(inbuf, smb_nts_TotalDataCount) < state->total_data) {
3261 state->total_data = IVAL(inbuf, smb_nts_TotalDataCount);
3264 pcnt = IVAL(inbuf,smb_nts_ParameterCount);
3265 poff = IVAL(inbuf, smb_nts_ParameterOffset);
3266 pdisp = IVAL(inbuf, smb_nts_ParameterDisplacement);
3268 dcnt = IVAL(inbuf, smb_nts_DataCount);
3269 ddisp = IVAL(inbuf, smb_nts_DataDisplacement);
3270 doff = IVAL(inbuf, smb_nts_DataOffset);
3272 state->received_param += pcnt;
3273 state->received_data += dcnt;
3275 if ((state->received_data > state->total_data) ||
3276 (state->received_param > state->total_param))
3280 if (pdisp+pcnt > state->total_param)
3282 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3284 if (pdisp > state->total_param)
3286 if ((smb_base(inbuf) + poff + pcnt > inbuf + size) ||
3287 (smb_base(inbuf) + poff + pcnt < smb_base(inbuf)))
3289 if (state->param + pdisp < state->param)
3292 memcpy(state->param+pdisp,smb_base(inbuf)+poff,
3297 if (ddisp+dcnt > state->total_data)
3299 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3301 if (ddisp > state->total_data)
3303 if ((smb_base(inbuf) + doff + dcnt > inbuf + size) ||
3304 (smb_base(inbuf) + doff + dcnt < smb_base(inbuf)))
3306 if (state->data + ddisp < state->data)
3309 memcpy(state->data+ddisp, smb_base(inbuf)+doff,
3313 if ((state->received_param < state->total_param) ||
3314 (state->received_data < state->total_data)) {
3315 END_PROFILE(SMBnttranss);
3319 /* construct_reply_common has done us the favor to pre-fill the
3320 * command field with SMBnttranss which is wrong :-)
3322 SCVAL(outbuf,smb_com,SMBnttrans);
3324 outsize = handle_nttrans(conn, state, inbuf, outbuf,
3327 DLIST_REMOVE(conn->pending_trans, state);
3328 SAFE_FREE(state->data);
3329 SAFE_FREE(state->param);
3333 END_PROFILE(SMBnttranss);
3334 return(ERROR_DOS(ERRSRV,ERRnosupport));
3337 END_PROFILE(SMBnttranss);
3342 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3343 DLIST_REMOVE(conn->pending_trans, state);
3344 SAFE_FREE(state->data);
3345 SAFE_FREE(state->param);
3347 END_PROFILE(SMBnttranss);
3348 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);