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 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 extern enum protocol_types Protocol;
25 extern int smb_read_error;
26 extern struct current_user current_user;
28 static const char *known_nt_pipes[] = {
49 static char *nttrans_realloc(char **ptr, size_t size)
52 smb_panic("nttrans_realloc() called with NULL ptr");
55 *ptr = (char *)SMB_REALLOC(*ptr, size);
59 memset(*ptr,'\0',size);
63 /****************************************************************************
64 Send the required number of replies back.
65 We assume all fields other than the data fields are
66 set correctly for the type of call.
67 HACK ! Always assumes smb_setup field is zero.
68 ****************************************************************************/
70 int send_nt_replies(const char *inbuf,
79 int data_to_send = datasize;
80 int params_to_send = paramsize;
84 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
85 int alignment_offset = 3;
86 int data_alignment_offset = 0;
89 * Initially set the wcnt area to be 18 - this is true for all
93 set_message(inbuf,outbuf,18,0,True);
95 if (NT_STATUS_V(nt_error)) {
100 * If there genuinely are no parameters or data to send just send
104 if(params_to_send == 0 && data_to_send == 0) {
106 if (!send_smb(smbd_server_fd(),outbuf)) {
107 exit_server_cleanly("send_nt_replies: send_smb failed.");
113 * When sending params and data ensure that both are nicely aligned.
114 * Only do this alignment when there is also data to send - else
115 * can cause NT redirector problems.
118 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
119 data_alignment_offset = 4 - (params_to_send % 4);
123 * Space is bufsize minus Netbios over TCP header minus SMB header.
124 * The alignment_offset is to align the param bytes on a four byte
125 * boundary (2 bytes for data len, one byte pad).
126 * NT needs this to work correctly.
129 useable_space = bufsize - ((smb_buf(outbuf)+
130 alignment_offset+data_alignment_offset) -
134 * useable_space can never be more than max_send minus the
138 useable_space = MIN(useable_space,
139 max_send - (alignment_offset+data_alignment_offset));
142 while (params_to_send || data_to_send) {
145 * Calculate whether we will totally or partially fill this packet.
148 total_sent_thistime = params_to_send + data_to_send +
149 alignment_offset + data_alignment_offset;
152 * We can never send more than useable_space.
155 total_sent_thistime = MIN(total_sent_thistime, useable_space);
157 set_message(inbuf,outbuf, 18, total_sent_thistime, True);
160 * Set total params and data to be sent.
163 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
164 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
167 * Calculate how many parameters and data we can fit into
168 * this packet. Parameters get precedence.
171 params_sent_thistime = MIN(params_to_send,useable_space);
172 data_sent_thistime = useable_space - params_sent_thistime;
173 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
175 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
177 if(params_sent_thistime == 0) {
178 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
179 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
182 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
183 * parameter bytes, however the first 4 bytes of outbuf are
184 * the Netbios over TCP header. Thus use smb_base() to subtract
185 * them from the calculation.
188 SIVAL(outbuf,smb_ntr_ParameterOffset,
189 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
191 * Absolute displacement of param bytes sent in this packet.
194 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
198 * Deal with the data portion.
201 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
203 if(data_sent_thistime == 0) {
204 SIVAL(outbuf,smb_ntr_DataOffset,0);
205 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
208 * The offset of the data bytes is the offset of the
209 * parameter bytes plus the number of parameters being sent this time.
212 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
213 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
214 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
218 * Copy the param bytes into the packet.
221 if(params_sent_thistime) {
222 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
226 * Copy in the data bytes
229 if(data_sent_thistime) {
230 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
231 data_alignment_offset,pd,data_sent_thistime);
234 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
235 params_sent_thistime, data_sent_thistime, useable_space));
236 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
237 params_to_send, data_to_send, paramsize, datasize));
239 /* Send the packet */
241 if (!send_smb(smbd_server_fd(),outbuf)) {
242 exit_server_cleanly("send_nt_replies: send_smb failed.");
245 pp += params_sent_thistime;
246 pd += data_sent_thistime;
248 params_to_send -= params_sent_thistime;
249 data_to_send -= data_sent_thistime;
255 if(params_to_send < 0 || data_to_send < 0) {
256 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
257 params_to_send, data_to_send));
265 /****************************************************************************
266 Is it an NTFS stream name ?
267 ****************************************************************************/
269 BOOL is_ntfs_stream_name(const char *fname)
271 if (lp_posix_pathnames()) {
274 return (strchr_m(fname, ':') != NULL) ? True : False;
277 struct case_semantics_state {
278 connection_struct *conn;
281 BOOL short_case_preserve;
284 /****************************************************************************
285 Restore case semantics.
286 ****************************************************************************/
287 static int restore_case_semantics(struct case_semantics_state *state)
289 state->conn->case_sensitive = state->case_sensitive;
290 state->conn->case_preserve = state->case_preserve;
291 state->conn->short_case_preserve = state->short_case_preserve;
295 /****************************************************************************
297 ****************************************************************************/
298 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
299 connection_struct *conn)
301 struct case_semantics_state *result;
303 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
304 DEBUG(0, ("talloc failed\n"));
309 result->case_sensitive = conn->case_sensitive;
310 result->case_preserve = conn->case_preserve;
311 result->short_case_preserve = conn->short_case_preserve;
314 conn->case_sensitive = True;
315 conn->case_preserve = True;
316 conn->short_case_preserve = True;
318 talloc_set_destructor(result, restore_case_semantics);
323 /****************************************************************************
324 Reply to an NT create and X call on a pipe -- this will die when all
325 callers are converted to nt_open_pipe_new
326 ****************************************************************************/
328 static int nt_open_pipe(char *fname, connection_struct *conn,
329 char *inbuf, char *outbuf, int *ppnum)
331 smb_np_struct *p = NULL;
332 uint16 vuid = SVAL(inbuf, smb_uid);
335 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
337 /* See if it is one we want to handle. */
339 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
340 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
343 for( i = 0; known_nt_pipes[i]; i++ ) {
344 if( strequal(fname,known_nt_pipes[i])) {
349 if ( known_nt_pipes[i] == NULL ) {
350 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
353 /* Strip \\ off the name. */
356 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
358 p = open_rpc_pipe_p(fname, conn, vuid);
360 return(ERROR_DOS(ERRSRV,ERRnofids));
363 /* TODO: Add pipe to db */
365 if ( !store_pipe_opendb( p ) ) {
366 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
373 static void nt_open_pipe_new(char *fname, connection_struct *conn,
374 struct smb_request *req, int *ppnum)
376 smb_np_struct *p = NULL;
379 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
381 /* See if it is one we want to handle. */
383 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
384 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
389 for( i = 0; known_nt_pipes[i]; i++ ) {
390 if( strequal(fname,known_nt_pipes[i])) {
395 if ( known_nt_pipes[i] == NULL ) {
396 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
401 /* Strip \\ off the name. */
404 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
406 p = open_rpc_pipe_p(fname, conn, req->vuid);
408 reply_doserror(req, ERRSRV, ERRnofids);
412 /* TODO: Add pipe to db */
414 if ( !store_pipe_opendb( p ) ) {
415 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
422 /****************************************************************************
423 Reply to an NT create and X call for pipes.
424 ****************************************************************************/
426 static void do_ntcreate_pipe_open(connection_struct *conn,
427 struct smb_request *req)
432 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
434 srvstr_pull_buf((char *)req->inbuf, req->flags2, fname,
435 smb_buf(req->inbuf), sizeof(fname), STR_TERMINATE);
437 nt_open_pipe_new(fname, conn, req, &pnum);
445 * Deal with pipe return.
448 if (flags & EXTENDED_RESPONSE_REQUIRED) {
449 /* This is very strange. We
450 * return 50 words, but only set
451 * the wcnt to 42 ? It's definately
452 * what happens on the wire....
454 reply_outbuf(req, 50, 0);
455 SCVAL(req->outbuf,smb_wct,42);
457 reply_outbuf(req, 34, 0);
460 p = (char *)req->outbuf + smb_vwv2;
464 SIVAL(p,0,FILE_WAS_OPENED);
467 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
470 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
472 SSVAL(p,2, 0x5FF); /* ? */
475 if (flags & EXTENDED_RESPONSE_REQUIRED) {
477 SIVAL(p,0,FILE_GENERIC_ALL);
479 * For pipes W2K3 seems to return
481 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
483 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
486 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
488 chain_reply_new(req);
491 /****************************************************************************
492 Reply to an NT create and X call for a quota file.
493 ****************************************************************************/
495 static void reply_ntcreate_and_X_quota(connection_struct *conn,
496 struct smb_request *req,
497 enum FAKE_FILE_TYPE fake_file_type,
501 uint32 desired_access = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
505 status = open_fake_file(conn, fake_file_type, fname, desired_access,
508 if (!NT_STATUS_IS_OK(status)) {
509 reply_nterror(req, status);
513 reply_outbuf(req, 34, 0);
515 p = (char *)req->outbuf + smb_vwv2;
517 /* SCVAL(p,0,NO_OPLOCK_RETURN); */
519 SSVAL(p,0,fsp->fnum);
521 DEBUG(5,("reply_ntcreate_and_X_quota: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
523 chain_reply_new(req);
526 /****************************************************************************
527 Reply to an NT create and X call.
528 ****************************************************************************/
530 void reply_ntcreate_and_X(connection_struct *conn,
531 struct smb_request *req)
536 uint32 file_attributes;
538 uint32 create_disposition;
539 uint32 create_options;
541 SMB_BIG_UINT allocation_size;
542 /* Breakout the oplock request bits so we can set the
543 reply bits separately. */
544 int oplock_request = 0;
546 SMB_OFF_T file_len = 0;
547 SMB_STRUCT_STAT sbuf;
549 files_struct *fsp = NULL;
551 struct timespec c_timespec;
552 struct timespec a_timespec;
553 struct timespec m_timespec;
554 BOOL extended_oplock_granted = False;
556 struct case_semantics_state *case_state = NULL;
558 START_PROFILE(SMBntcreateX);
561 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
565 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
566 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
567 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
568 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
569 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
570 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
571 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
573 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,smb_ntcreate_AllocationSize);
574 #ifdef LARGE_SMB_OFF_T
575 allocation_size |= (((SMB_BIG_UINT)IVAL(req->inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
578 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
579 "file_attributes = 0x%x, share_access = 0x%x, "
580 "create_disposition = 0x%x create_options = 0x%x "
581 "root_dir_fid = 0x%x\n",
583 (unsigned int)access_mask,
584 (unsigned int)file_attributes,
585 (unsigned int)share_access,
586 (unsigned int)create_disposition,
587 (unsigned int)create_options,
588 (unsigned int)root_dir_fid ));
591 * If it's an IPC, use the pipe handler.
595 if (lp_nt_pipe_support()) {
596 do_ntcreate_pipe_open(conn, req);
597 END_PROFILE(SMBntcreateX);
600 reply_doserror(req, ERRDOS, ERRnoaccess);
601 END_PROFILE(SMBntcreateX);
606 if (create_options & FILE_OPEN_BY_FILE_ID) {
607 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
608 END_PROFILE(SMBntcreateX);
616 if(root_dir_fid != 0) {
618 * This filename is relative to a directory fid.
621 files_struct *dir_fsp = file_fsp(
622 SVAL(req->inbuf, smb_ntcreate_RootDirectoryFid));
626 reply_doserror(req, ERRDOS, ERRbadfid);
627 END_PROFILE(SMBntcreateX);
631 if(!dir_fsp->is_directory) {
633 srvstr_get_path((char *)req->inbuf, req->flags2, fname,
634 smb_buf(req->inbuf), sizeof(fname), 0,
635 STR_TERMINATE, &status);
636 if (!NT_STATUS_IS_OK(status)) {
637 reply_nterror(req, status);
638 END_PROFILE(SMBntcreateX);
643 * Check to see if this is a mac fork of some kind.
646 if( is_ntfs_stream_name(fname)) {
648 req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
649 END_PROFILE(SMBntcreateX);
654 we need to handle the case when we get a
655 relative open relative to a file and the
656 pathname is blank - this is a reopen!
657 (hint from demyn plantenberg)
660 reply_doserror(req, ERRDOS, ERRbadfid);
661 END_PROFILE(SMBntcreateX);
666 * Copy in the base directory name.
669 pstrcpy( fname, dir_fsp->fsp_name );
670 dir_name_len = strlen(fname);
673 * Ensure it ends in a '\'.
676 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
681 srvstr_get_path((char *)req->inbuf, req->flags2, rel_fname,
682 smb_buf(req->inbuf), sizeof(rel_fname), 0,
683 STR_TERMINATE, &status);
684 if (!NT_STATUS_IS_OK(status)) {
685 reply_nterror(req, status);
686 END_PROFILE(SMBntcreateX);
689 pstrcat(fname, rel_fname);
691 srvstr_get_path((char *)req->inbuf, req->flags2, fname,
692 smb_buf(req->inbuf), sizeof(fname), 0,
693 STR_TERMINATE, &status);
694 if (!NT_STATUS_IS_OK(status)) {
695 reply_nterror(req, status);
696 END_PROFILE(SMBntcreateX);
701 * Check to see if this is a mac fork of some kind.
704 if( is_ntfs_stream_name(fname)) {
705 enum FAKE_FILE_TYPE fake_file_type = is_fake_file(fname);
706 if (fake_file_type!=FAKE_FILE_TYPE_NONE) {
708 * Here we go! support for changing the disk quotas --metze
710 * We need to fake up to open this MAGIC QUOTA file
711 * and return a valid FID.
713 * w2k close this file directly after openening
714 * xp also tries a QUERY_FILE_INFO on the file and then close it
716 reply_ntcreate_and_X_quota(conn, req,
717 fake_file_type, fname);
719 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
721 END_PROFILE(SMBntcreateX);
727 * Now contruct the smb_open_mode value from the filename,
728 * desired access and the share access.
730 status = resolve_dfspath(conn, req->flags2 & FLAGS2_DFS_PATHNAMES, fname);
731 if (!NT_STATUS_IS_OK(status)) {
732 END_PROFILE(SMBntcreateX);
733 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
734 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
738 reply_nterror(req, status);
740 END_PROFILE(SMBntcreateX);
744 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
745 if (oplock_request) {
746 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
750 * Ordinary file or directory.
754 * Check if POSIX semantics are wanted.
757 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
758 case_state = set_posix_case_semantics(NULL, conn);
759 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
762 status = unix_convert(conn, fname, False, NULL, &sbuf);
763 if (!NT_STATUS_IS_OK(status)) {
764 TALLOC_FREE(case_state);
765 reply_nterror(req, status);
766 END_PROFILE(SMBntcreateX);
769 /* All file access must go through check_name() */
770 status = check_name(conn, fname);
771 if (!NT_STATUS_IS_OK(status)) {
772 TALLOC_FREE(case_state);
773 reply_nterror(req, status);
774 END_PROFILE(SMBntcreateX);
778 /* This is the correct thing to do (check every time) but can_delete is
779 expensive (it may have to read the parent directory permissions). So
780 for now we're not doing it unless we have a strong hint the client
781 is really going to delete this file. If the client is forcing FILE_CREATE
782 let the filesystem take care of the permissions. */
784 /* Setting FILE_SHARE_DELETE is the hint. */
786 if (lp_acl_check_permissions(SNUM(conn))
787 && (create_disposition != FILE_CREATE)
788 && (share_access & FILE_SHARE_DELETE)
789 && (access_mask & DELETE_ACCESS)) {
790 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
791 !can_delete_file_in_directory(conn, fname)) {
792 TALLOC_FREE(case_state);
793 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
794 END_PROFILE(SMBntcreateX);
800 /* We need to support SeSecurityPrivilege for this. */
801 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
802 !user_has_privileges(current_user.nt_user_token,
804 TALLOC_FREE(case_state);
805 END_PROFILE(SMBntcreateX);
806 return ERROR_NT(NT_STATUS_PRIVILEGE_NOT_HELD);
811 * If it's a request for a directory open, deal with it separately.
814 if(create_options & FILE_DIRECTORY_FILE) {
816 /* Can't open a temp directory. IFS kit test. */
817 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
818 TALLOC_FREE(case_state);
819 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
820 END_PROFILE(SMBntcreateX);
825 status = open_directory(conn, req, fname, &sbuf,
833 TALLOC_FREE(case_state);
835 if(!NT_STATUS_IS_OK(status)) {
836 if (!use_nt_status() && NT_STATUS_EQUAL(
837 status, NT_STATUS_OBJECT_NAME_COLLISION)) {
838 status = NT_STATUS_DOS(ERRDOS, ERRfilexists);
840 reply_nterror(req, status);
841 END_PROFILE(SMBntcreateX);
848 * Ordinary file case.
851 /* NB. We have a potential bug here. If we
852 * cause an oplock break to ourselves, then we
853 * could end up processing filename related
854 * SMB requests whilst we await the oplock
855 * break response. As we may have changed the
856 * filename case semantics to be POSIX-like,
857 * this could mean a filename request could
858 * fail when it should succeed. This is a rare
859 * condition, but eventually we must arrange
860 * to restore the correct case semantics
861 * before issuing an oplock break request to
862 * our client. JRA. */
864 status = open_file_ntcreate(conn, req, fname, &sbuf,
873 if (!NT_STATUS_IS_OK(status)) {
874 /* We cheat here. There are two cases we
875 * care about. One is a directory rename,
876 * where the NT client will attempt to
877 * open the source directory for
878 * DELETE access. Note that when the
879 * NT client does this it does *not*
880 * set the directory bit in the
881 * request packet. This is translated
882 * into a read/write open
883 * request. POSIX states that any open
884 * for write request on a directory
885 * will generate an EISDIR error, so
886 * we can catch this here and open a
887 * pseudo handle that is flagged as a
888 * directory. The second is an open
889 * for a permissions read only, which
890 * we handle in the open_file_stat case. JRA.
893 if (NT_STATUS_EQUAL(status,
894 NT_STATUS_FILE_IS_A_DIRECTORY)) {
897 * Fail the open if it was explicitly a non-directory file.
900 if (create_options & FILE_NON_DIRECTORY_FILE) {
901 TALLOC_FREE(case_state);
902 reply_nterror(req, NT_STATUS_FILE_IS_A_DIRECTORY);
903 END_PROFILE(SMBntcreateX);
908 status = open_directory(conn, req, fname,
917 if(!NT_STATUS_IS_OK(status)) {
918 TALLOC_FREE(case_state);
919 if (!use_nt_status() && NT_STATUS_EQUAL(
920 status, NT_STATUS_OBJECT_NAME_COLLISION)) {
921 status = NT_STATUS_DOS(ERRDOS, ERRfilexists);
923 reply_nterror(req, status);
924 END_PROFILE(SMBntcreateX);
928 TALLOC_FREE(case_state);
929 END_PROFILE(SMBntcreateX);
930 if (open_was_deferred(req->mid)) {
931 /* We have re-scheduled this call. */
934 reply_nterror(req, status);
940 TALLOC_FREE(case_state);
942 file_len = sbuf.st_size;
943 fattr = dos_mode(conn,fname,&sbuf);
945 fattr = FILE_ATTRIBUTE_NORMAL;
947 if (!fsp->is_directory && (fattr & aDIR)) {
948 close_file(fsp,ERROR_CLOSE);
949 reply_doserror(req, ERRDOS, ERRnoaccess);
950 END_PROFILE(SMBntcreateX);
954 /* Save the requested allocation size. */
955 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
956 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
957 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
958 if (fsp->is_directory) {
959 close_file(fsp,ERROR_CLOSE);
960 /* Can't set allocation size on a directory. */
961 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
962 END_PROFILE(SMBntcreateX);
965 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
966 close_file(fsp,ERROR_CLOSE);
967 reply_nterror(req, NT_STATUS_DISK_FULL);
968 END_PROFILE(SMBntcreateX);
972 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
977 * If the caller set the extended oplock request bit
978 * and we granted one (by whatever means) - set the
979 * correct bit for extended oplock reply.
982 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
983 extended_oplock_granted = True;
986 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
987 extended_oplock_granted = True;
990 if (flags & EXTENDED_RESPONSE_REQUIRED) {
991 /* This is very strange. We
992 * return 50 words, but only set
993 * the wcnt to 42 ? It's definately
994 * what happens on the wire....
996 reply_outbuf(req, 50, 0);
997 SCVAL(req->outbuf,smb_wct,42);
999 reply_outbuf(req, 34, 0);
1002 p = (char *)req->outbuf + smb_vwv2;
1005 * Currently as we don't support level II oplocks we just report
1006 * exclusive & batch here.
1009 if (extended_oplock_granted) {
1010 if (flags & REQUEST_BATCH_OPLOCK) {
1011 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1013 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1015 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1016 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1018 SCVAL(p,0,NO_OPLOCK_RETURN);
1022 SSVAL(p,0,fsp->fnum);
1024 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1025 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1032 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1033 a_timespec = get_atimespec(&sbuf);
1034 m_timespec = get_mtimespec(&sbuf);
1036 if (lp_dos_filetime_resolution(SNUM(conn))) {
1037 dos_filetime_timespec(&c_timespec);
1038 dos_filetime_timespec(&a_timespec);
1039 dos_filetime_timespec(&m_timespec);
1042 put_long_date_timespec(p, c_timespec); /* create time. */
1044 put_long_date_timespec(p, a_timespec); /* access time */
1046 put_long_date_timespec(p, m_timespec); /* write time */
1048 put_long_date_timespec(p, m_timespec); /* change time */
1050 SIVAL(p,0,fattr); /* File Attributes. */
1052 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1054 SOFF_T(p,0,file_len);
1056 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1060 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1062 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1065 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1066 perms = FILE_GENERIC_ALL;
1068 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1073 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
1075 chain_reply_new(req);
1076 END_PROFILE(SMBntcreateX);
1080 /****************************************************************************
1081 Reply to a NT_TRANSACT_CREATE call to open a pipe.
1082 ****************************************************************************/
1084 static int do_nt_transact_create_pipe( connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1085 uint16 **ppsetup, uint32 setup_count,
1086 char **ppparams, uint32 parameter_count,
1087 char **ppdata, uint32 data_count)
1090 char *params = *ppparams;
1099 * Ensure minimum number of parameters sent.
1102 if(parameter_count < 54) {
1103 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1104 return ERROR_DOS(ERRDOS,ERRnoaccess);
1107 flags = IVAL(params,0);
1109 srvstr_get_path(inbuf, SVAL(inbuf,smb_flg2), fname, params+53,
1110 sizeof(fname), parameter_count-53, STR_TERMINATE,
1112 if (!NT_STATUS_IS_OK(status)) {
1113 return ERROR_NT(status);
1116 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0) {
1120 /* Realloc the size of parameters and data we will return */
1121 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1122 /* Extended response is 32 more byyes. */
1127 params = nttrans_realloc(ppparams, param_len);
1128 if(params == NULL) {
1129 return ERROR_DOS(ERRDOS,ERRnomem);
1133 SCVAL(p,0,NO_OPLOCK_RETURN);
1138 SIVAL(p,0,FILE_WAS_OPENED);
1142 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1145 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1147 SSVAL(p,2, 0x5FF); /* ? */
1150 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1152 SIVAL(p,0,FILE_GENERIC_ALL);
1154 * For pipes W2K3 seems to return
1156 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
1158 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
1161 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1163 /* Send the required number of replies */
1164 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, *ppdata, 0);
1169 /****************************************************************************
1170 Internal fn to set security descriptors.
1171 ****************************************************************************/
1173 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1176 SEC_DESC *psd = NULL;
1177 TALLOC_CTX *mem_ctx;
1180 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
1181 return NT_STATUS_OK;
1185 * Init the parse struct we will unmarshall from.
1188 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1189 DEBUG(0,("set_sd: talloc_init failed.\n"));
1190 return NT_STATUS_NO_MEMORY;
1193 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1196 * Setup the prs_struct to point at the memory we just
1200 prs_give_memory( &pd, data, sd_len, False);
1203 * Finally, unmarshall from the data buffer.
1206 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1207 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1209 * Return access denied for want of a better error message..
1211 talloc_destroy(mem_ctx);
1212 return NT_STATUS_NO_MEMORY;
1215 if (psd->owner_sid==0) {
1216 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1218 if (psd->group_sid==0) {
1219 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1222 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1225 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1228 status = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1230 talloc_destroy(mem_ctx);
1234 /****************************************************************************
1235 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1236 ****************************************************************************/
1238 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1240 struct ea_list *ea_list_head = NULL;
1243 if (data_size < 4) {
1247 while (offset + 4 <= data_size) {
1248 size_t next_offset = IVAL(pdata,offset);
1249 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1255 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1256 if (next_offset == 0) {
1259 offset += next_offset;
1262 return ea_list_head;
1265 /****************************************************************************
1266 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1267 ****************************************************************************/
1269 static int call_nt_transact_create(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1270 uint16 **ppsetup, uint32 setup_count,
1271 char **ppparams, uint32 parameter_count,
1272 char **ppdata, uint32 data_count, uint32 max_data_count)
1275 char *params = *ppparams;
1276 char *data = *ppdata;
1277 /* Breakout the oplock request bits so we can set the reply bits separately. */
1278 int oplock_request = 0;
1280 SMB_OFF_T file_len = 0;
1281 SMB_STRUCT_STAT sbuf;
1283 files_struct *fsp = NULL;
1285 BOOL extended_oplock_granted = False;
1288 uint32 file_attributes;
1289 uint32 share_access;
1290 uint32 create_disposition;
1291 uint32 create_options;
1294 uint16 root_dir_fid;
1295 struct timespec c_timespec;
1296 struct timespec a_timespec;
1297 struct timespec m_timespec;
1298 struct ea_list *ea_list = NULL;
1299 TALLOC_CTX *ctx = NULL;
1303 struct smb_request req;
1304 struct case_semantics_state *case_state = NULL;
1306 DEBUG(5,("call_nt_transact_create\n"));
1309 * If it's an IPC, use the pipe handler.
1313 if (lp_nt_pipe_support()) {
1314 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1316 ppsetup, setup_count,
1317 ppparams, parameter_count,
1318 ppdata, data_count);
1320 return ERROR_DOS(ERRDOS,ERRnoaccess);
1325 * Ensure minimum number of parameters sent.
1328 if(parameter_count < 54) {
1329 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1330 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1333 init_smb_request(&req, (uint8 *)inbuf);
1335 flags = IVAL(params,0);
1336 access_mask = IVAL(params,8);
1337 file_attributes = IVAL(params,20);
1338 share_access = IVAL(params,24);
1339 create_disposition = IVAL(params,28);
1340 create_options = IVAL(params,32);
1341 sd_len = IVAL(params,36);
1342 ea_len = IVAL(params,40);
1343 root_dir_fid = (uint16)IVAL(params,4);
1345 /* Ensure the data_len is correct for the sd and ea values given. */
1346 if ((ea_len + sd_len > data_count) ||
1347 (ea_len > data_count) || (sd_len > data_count) ||
1348 (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1349 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1350 (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1351 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1355 if (!lp_ea_support(SNUM(conn))) {
1356 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1357 (unsigned int)ea_len ));
1358 return ERROR_NT(NT_STATUS_EAS_NOT_SUPPORTED);
1362 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1363 (unsigned int)ea_len ));
1364 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1368 if (create_options & FILE_OPEN_BY_FILE_ID) {
1369 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1373 * Get the file name.
1376 if(root_dir_fid != 0) {
1378 * This filename is relative to a directory fid.
1380 files_struct *dir_fsp = file_fsp(SVAL(params,4));
1381 size_t dir_name_len;
1384 return ERROR_DOS(ERRDOS,ERRbadfid);
1387 if(!dir_fsp->is_directory) {
1388 srvstr_get_path(inbuf, SVAL(inbuf,smb_flg2), fname,
1389 params+53, sizeof(fname),
1390 parameter_count-53, STR_TERMINATE,
1392 if (!NT_STATUS_IS_OK(status)) {
1393 return ERROR_NT(status);
1397 * Check to see if this is a mac fork of some kind.
1400 if( is_ntfs_stream_name(fname)) {
1401 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1404 return ERROR_DOS(ERRDOS,ERRbadfid);
1408 * Copy in the base directory name.
1411 pstrcpy( fname, dir_fsp->fsp_name );
1412 dir_name_len = strlen(fname);
1415 * Ensure it ends in a '\'.
1418 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1419 pstrcat(fname, "/");
1425 srvstr_get_path(inbuf, SVAL(inbuf,smb_flg2), tmpname,
1426 params+53, sizeof(tmpname),
1427 parameter_count-53, STR_TERMINATE,
1429 if (!NT_STATUS_IS_OK(status)) {
1430 return ERROR_NT(status);
1432 pstrcat(fname, tmpname);
1435 srvstr_get_path(inbuf, SVAL(inbuf,smb_flg2), fname, params+53,
1436 sizeof(fname), parameter_count-53,
1437 STR_TERMINATE, &status);
1438 if (!NT_STATUS_IS_OK(status)) {
1439 return ERROR_NT(status);
1443 * Check to see if this is a mac fork of some kind.
1446 if( is_ntfs_stream_name(fname)) {
1447 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1451 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1452 if (oplock_request) {
1453 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1457 * Ordinary file or directory.
1461 * Check if POSIX semantics are wanted.
1464 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1465 case_state = set_posix_case_semantics(NULL, conn);
1466 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1469 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, fname);
1470 if (!NT_STATUS_IS_OK(status)) {
1471 TALLOC_FREE(case_state);
1472 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1473 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1475 return ERROR_NT(status);
1478 status = unix_convert(conn, fname, False, NULL, &sbuf);
1479 if (!NT_STATUS_IS_OK(status)) {
1480 TALLOC_FREE(case_state);
1481 return ERROR_NT(status);
1483 /* All file access must go through check_name() */
1484 status = check_name(conn, fname);
1485 if (!NT_STATUS_IS_OK(status)) {
1486 TALLOC_FREE(case_state);
1487 return ERROR_NT(status);
1490 /* This is the correct thing to do (check every time) but can_delete is
1491 expensive (it may have to read the parent directory permissions). So
1492 for now we're not doing it unless we have a strong hint the client
1493 is really going to delete this file. If the client is forcing FILE_CREATE
1494 let the filesystem take care of the permissions. */
1496 /* Setting FILE_SHARE_DELETE is the hint. */
1498 if (lp_acl_check_permissions(SNUM(conn))
1499 && (create_disposition != FILE_CREATE)
1500 && (share_access & FILE_SHARE_DELETE)
1501 && (access_mask & DELETE_ACCESS)) {
1502 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
1503 !can_delete_file_in_directory(conn, fname)) {
1504 TALLOC_FREE(case_state);
1505 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1510 /* We need to support SeSecurityPrivilege for this. */
1511 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
1512 !user_has_privileges(current_user.nt_user_token,
1514 TALLOC_FREE(case_state);
1515 return ERROR_NT(NT_STATUS_PRIVILEGE_NOT_HELD);
1520 pdata = data + sd_len;
1522 /* We have already checked that ea_len <= data_count here. */
1523 ea_list = read_nttrans_ea_list(tmp_talloc_ctx(), pdata,
1526 TALLOC_FREE(case_state);
1527 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1532 * If it's a request for a directory open, deal with it separately.
1535 if(create_options & FILE_DIRECTORY_FILE) {
1537 /* Can't open a temp directory. IFS kit test. */
1538 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1539 TALLOC_FREE(case_state);
1540 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1544 * We will get a create directory here if the Win32
1545 * app specified a security descriptor in the
1546 * CreateDirectory() call.
1550 status = open_directory(conn, &req, fname, &sbuf,
1557 if(!NT_STATUS_IS_OK(status)) {
1558 TALLOC_FREE(case_state);
1559 return ERROR_NT(status);
1565 * Ordinary file case.
1568 status = open_file_ntcreate(conn,&req,fname,&sbuf,
1577 if (!NT_STATUS_IS_OK(status)) {
1578 if (NT_STATUS_EQUAL(status,
1579 NT_STATUS_FILE_IS_A_DIRECTORY)) {
1582 * Fail the open if it was explicitly a non-directory file.
1585 if (create_options & FILE_NON_DIRECTORY_FILE) {
1586 TALLOC_FREE(case_state);
1587 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1591 status = open_directory(conn, &req, fname,
1599 if(!NT_STATUS_IS_OK(status)) {
1600 TALLOC_FREE(case_state);
1601 return ERROR_NT(status);
1604 TALLOC_FREE(case_state);
1605 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1606 /* We have re-scheduled this call. */
1609 return ERROR_NT(status);
1615 * According to the MS documentation, the only time the security
1616 * descriptor is applied to the opened file is iff we *created* the
1617 * file; an existing file stays the same.
1619 * Also, it seems (from observation) that you can open the file with
1620 * any access mask but you can still write the sd. We need to override
1621 * the granted access before we call set_sd
1622 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1625 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1626 uint32 saved_access_mask = fsp->access_mask;
1628 /* We have already checked that sd_len <= data_count here. */
1630 fsp->access_mask = FILE_GENERIC_ALL;
1632 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1633 if (!NT_STATUS_IS_OK(status)) {
1634 talloc_destroy(ctx);
1635 close_file(fsp,ERROR_CLOSE);
1636 TALLOC_FREE(case_state);
1637 return ERROR_NT(status);
1639 fsp->access_mask = saved_access_mask;
1642 if (ea_len && (info == FILE_WAS_CREATED)) {
1643 status = set_ea(conn, fsp, fname, ea_list);
1644 if (!NT_STATUS_IS_OK(status)) {
1645 close_file(fsp,ERROR_CLOSE);
1646 TALLOC_FREE(case_state);
1647 return ERROR_NT(status);
1651 TALLOC_FREE(case_state);
1653 file_len = sbuf.st_size;
1654 fattr = dos_mode(conn,fname,&sbuf);
1656 fattr = FILE_ATTRIBUTE_NORMAL;
1658 if (!fsp->is_directory && (fattr & aDIR)) {
1659 close_file(fsp,ERROR_CLOSE);
1660 return ERROR_DOS(ERRDOS,ERRnoaccess);
1663 /* Save the requested allocation size. */
1664 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1665 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1666 #ifdef LARGE_SMB_OFF_T
1667 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1669 if (allocation_size && (allocation_size > file_len)) {
1670 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1671 if (fsp->is_directory) {
1672 close_file(fsp,ERROR_CLOSE);
1673 /* Can't set allocation size on a directory. */
1674 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1676 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1677 close_file(fsp,ERROR_CLOSE);
1678 return ERROR_NT(NT_STATUS_DISK_FULL);
1681 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1686 * If the caller set the extended oplock request bit
1687 * and we granted one (by whatever means) - set the
1688 * correct bit for extended oplock reply.
1691 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1692 extended_oplock_granted = True;
1695 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1696 extended_oplock_granted = True;
1699 /* Realloc the size of parameters and data we will return */
1700 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1701 /* Extended response is 32 more byyes. */
1706 params = nttrans_realloc(ppparams, param_len);
1707 if(params == NULL) {
1708 return ERROR_DOS(ERRDOS,ERRnomem);
1712 if (extended_oplock_granted) {
1713 if (flags & REQUEST_BATCH_OPLOCK) {
1714 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1716 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1718 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1719 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1721 SCVAL(p,0,NO_OPLOCK_RETURN);
1725 SSVAL(p,0,fsp->fnum);
1727 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1728 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1735 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1736 a_timespec = get_atimespec(&sbuf);
1737 m_timespec = get_mtimespec(&sbuf);
1739 if (lp_dos_filetime_resolution(SNUM(conn))) {
1740 dos_filetime_timespec(&c_timespec);
1741 dos_filetime_timespec(&a_timespec);
1742 dos_filetime_timespec(&m_timespec);
1745 put_long_date_timespec(p, c_timespec); /* create time. */
1747 put_long_date_timespec(p, a_timespec); /* access time */
1749 put_long_date_timespec(p, m_timespec); /* write time */
1751 put_long_date_timespec(p, m_timespec); /* change time */
1753 SIVAL(p,0,fattr); /* File Attributes. */
1755 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1757 SOFF_T(p,0,file_len);
1759 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1763 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1765 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1768 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1769 perms = FILE_GENERIC_ALL;
1771 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1776 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1778 /* Send the required number of replies */
1779 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, *ppdata, 0);
1784 /****************************************************************************
1785 Reply to a NT CANCEL request.
1786 conn POINTER CAN BE NULL HERE !
1787 ****************************************************************************/
1789 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1792 * Go through and cancel any pending change notifies.
1795 START_PROFILE(SMBntcancel);
1796 remove_pending_change_notify_requests_by_mid(req->mid);
1797 remove_pending_lock_requests_by_mid(req->mid);
1798 srv_cancel_sign_response(req->mid);
1800 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1802 END_PROFILE(SMBntcancel);
1806 /****************************************************************************
1808 ****************************************************************************/
1810 static NTSTATUS copy_internals(connection_struct *conn,
1811 struct smb_request *req,
1812 char *oldname, char *newname, uint32 attrs)
1814 SMB_STRUCT_STAT sbuf1, sbuf2;
1815 pstring last_component_oldname;
1816 pstring last_component_newname;
1817 files_struct *fsp1,*fsp2;
1821 NTSTATUS status = NT_STATUS_OK;
1826 if (!CAN_WRITE(conn)) {
1827 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1830 status = unix_convert(conn, oldname, False, last_component_oldname, &sbuf1);
1831 if (!NT_STATUS_IS_OK(status)) {
1835 status = check_name(conn, oldname);
1836 if (!NT_STATUS_IS_OK(status)) {
1840 /* Source must already exist. */
1841 if (!VALID_STAT(sbuf1)) {
1842 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1844 /* Ensure attributes match. */
1845 fattr = dos_mode(conn,oldname,&sbuf1);
1846 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1847 return NT_STATUS_NO_SUCH_FILE;
1850 status = unix_convert(conn, newname, False, last_component_newname, &sbuf2);
1851 if (!NT_STATUS_IS_OK(status)) {
1855 status = check_name(conn, newname);
1856 if (!NT_STATUS_IS_OK(status)) {
1860 /* Disallow if newname already exists. */
1861 if (VALID_STAT(sbuf2)) {
1862 return NT_STATUS_OBJECT_NAME_COLLISION;
1865 /* No links from a directory. */
1866 if (S_ISDIR(sbuf1.st_mode)) {
1867 return NT_STATUS_FILE_IS_A_DIRECTORY;
1870 /* Ensure this is within the share. */
1871 status = check_reduced_name(conn, oldname);
1872 if (!NT_STATUS_IS_OK(status)) {
1876 DEBUG(10,("copy_internals: doing file copy %s to %s\n", oldname, newname));
1878 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1879 FILE_READ_DATA, /* Read-only. */
1880 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1882 0, /* No create options. */
1883 FILE_ATTRIBUTE_NORMAL,
1887 if (!NT_STATUS_IS_OK(status)) {
1891 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1892 FILE_WRITE_DATA, /* Read-only. */
1893 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1895 0, /* No create options. */
1900 if (!NT_STATUS_IS_OK(status)) {
1901 close_file(fsp1,ERROR_CLOSE);
1905 if (sbuf1.st_size) {
1906 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1910 * As we are opening fsp1 read-only we only expect
1911 * an error on close on fsp2 if we are out of space.
1912 * Thus we don't look at the error return from the
1915 close_file(fsp1,NORMAL_CLOSE);
1917 /* Ensure the modtime is set correctly on the destination file. */
1918 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1920 status = close_file(fsp2,NORMAL_CLOSE);
1922 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1923 creates the file. This isn't the correct thing to do in the copy
1925 file_set_dosmode(conn, newname, fattr, &sbuf2,
1926 parent_dirname(newname));
1928 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1929 return NT_STATUS_DISK_FULL;
1932 if (!NT_STATUS_IS_OK(status)) {
1933 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1934 nt_errstr(status), oldname, newname));
1939 /****************************************************************************
1940 Reply to a NT rename request.
1941 ****************************************************************************/
1943 int reply_ntrename(connection_struct *conn,
1944 char *inbuf,char *outbuf,int length,int bufsize)
1951 BOOL src_has_wcard = False;
1952 BOOL dest_has_wcard = False;
1953 uint32 attrs = SVAL(inbuf,smb_vwv0);
1954 uint16 rename_type = SVAL(inbuf,smb_vwv1);
1955 struct smb_request req;
1957 START_PROFILE(SMBntrename);
1959 init_smb_request(&req, (uint8 *)inbuf);
1961 p = smb_buf(inbuf) + 1;
1962 p += srvstr_get_path_wcard(inbuf, SVAL(inbuf,smb_flg2), oldname, p,
1963 sizeof(oldname), 0, STR_TERMINATE, &status,
1965 if (!NT_STATUS_IS_OK(status)) {
1966 END_PROFILE(SMBntrename);
1967 return ERROR_NT(status);
1970 if( is_ntfs_stream_name(oldname)) {
1971 /* Can't rename a stream. */
1972 END_PROFILE(SMBntrename);
1973 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1976 if (ms_has_wild(oldname)) {
1977 END_PROFILE(SMBntrename);
1978 return ERROR_NT(NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1982 p += srvstr_get_path_wcard(inbuf, SVAL(inbuf,smb_flg2), newname, p,
1983 sizeof(newname), 0, STR_TERMINATE, &status,
1985 if (!NT_STATUS_IS_OK(status)) {
1986 END_PROFILE(SMBntrename);
1987 return ERROR_NT(status);
1990 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, oldname);
1991 if (!NT_STATUS_IS_OK(status)) {
1992 END_PROFILE(SMBntrename);
1993 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1994 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1996 return ERROR_NT(status);
1999 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, newname);
2000 if (!NT_STATUS_IS_OK(status)) {
2001 END_PROFILE(SMBntrename);
2002 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2003 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
2005 return ERROR_NT(status);
2008 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
2010 switch(rename_type) {
2011 case RENAME_FLAG_RENAME:
2012 status = rename_internals(conn, &req, oldname, newname,
2013 attrs, False, src_has_wcard,
2016 case RENAME_FLAG_HARD_LINK:
2017 if (src_has_wcard || dest_has_wcard) {
2019 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2021 status = hardlink_internals(conn, oldname, newname);
2024 case RENAME_FLAG_COPY:
2025 if (src_has_wcard || dest_has_wcard) {
2027 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2029 status = copy_internals(conn, &req, oldname,
2033 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
2034 status = NT_STATUS_INVALID_PARAMETER;
2037 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
2041 if (!NT_STATUS_IS_OK(status)) {
2042 END_PROFILE(SMBntrename);
2043 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
2044 /* We have re-scheduled this call. */
2047 return ERROR_NT(status);
2050 outsize = set_message(inbuf,outbuf,0,0,False);
2052 END_PROFILE(SMBntrename);
2056 /****************************************************************************
2057 Reply to a notify change - queue the request and
2058 don't allow a directory to be opened.
2059 ****************************************************************************/
2061 static int call_nt_transact_notify_change(connection_struct *conn, char *inbuf,
2062 char *outbuf, int length,
2064 uint16 **ppsetup, uint32 setup_count,
2066 uint32 parameter_count,
2067 char **ppdata, uint32 data_count,
2068 uint32 max_data_count,
2069 uint32 max_param_count)
2071 uint16 *setup = *ppsetup;
2077 if(setup_count < 6) {
2078 return ERROR_DOS(ERRDOS,ERRbadfunc);
2081 fsp = file_fsp(SVAL(setup,4));
2082 filter = IVAL(setup, 0);
2083 recursive = (SVAL(setup, 6) != 0) ? True : False;
2085 DEBUG(3,("call_nt_transact_notify_change\n"));
2088 return ERROR_DOS(ERRDOS,ERRbadfid);
2092 char *filter_string;
2094 if (!(filter_string = notify_filter_string(NULL, filter))) {
2095 return ERROR_NT(NT_STATUS_NO_MEMORY);
2098 DEBUG(3,("call_nt_transact_notify_change: notify change "
2099 "called on %s, filter = %s, recursive = %d\n",
2100 fsp->fsp_name, filter_string, recursive));
2102 TALLOC_FREE(filter_string);
2105 if((!fsp->is_directory) || (conn != fsp->conn)) {
2106 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2109 if (fsp->notify == NULL) {
2111 status = change_notify_create(fsp, filter, recursive);
2113 if (!NT_STATUS_IS_OK(status)) {
2114 DEBUG(10, ("change_notify_create returned %s\n",
2115 nt_errstr(status)));
2116 return ERROR_NT(status);
2120 if (fsp->notify->num_changes != 0) {
2123 * We've got changes pending, respond immediately
2127 * TODO: write a torture test to check the filtering behaviour
2131 change_notify_reply(inbuf, max_param_count, fsp->notify);
2134 * change_notify_reply() above has independently sent its
2141 * No changes pending, queue the request
2144 status = change_notify_add_request(inbuf, max_param_count, filter,
2146 if (!NT_STATUS_IS_OK(status)) {
2147 return ERROR_NT(status);
2153 /****************************************************************************
2154 Reply to an NT transact rename command.
2155 ****************************************************************************/
2157 static int call_nt_transact_rename(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2158 uint16 **ppsetup, uint32 setup_count,
2159 char **ppparams, uint32 parameter_count,
2160 char **ppdata, uint32 data_count, uint32 max_data_count)
2162 char *params = *ppparams;
2164 files_struct *fsp = NULL;
2165 BOOL replace_if_exists = False;
2166 BOOL dest_has_wcard = False;
2168 struct smb_request req;
2170 init_smb_request(&req, (uint8 *)inbuf);
2172 if(parameter_count < 5) {
2173 return ERROR_DOS(ERRDOS,ERRbadfunc);
2176 fsp = file_fsp(SVAL(params, 0));
2177 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2178 CHECK_FSP(fsp, conn);
2179 srvstr_get_path_wcard(inbuf, SVAL(inbuf,smb_flg2), new_name, params+4,
2180 sizeof(new_name), parameter_count - 4,
2181 STR_TERMINATE, &status, &dest_has_wcard);
2182 if (!NT_STATUS_IS_OK(status)) {
2183 return ERROR_NT(status);
2186 status = rename_internals(conn, &req, fsp->fsp_name,
2187 new_name, 0, replace_if_exists, False, dest_has_wcard);
2189 if (!NT_STATUS_IS_OK(status)) {
2190 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
2191 /* We have re-scheduled this call. */
2194 return ERROR_NT(status);
2198 * Rename was successful.
2200 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2202 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2203 fsp->fsp_name, new_name));
2208 /******************************************************************************
2209 Fake up a completely empty SD.
2210 *******************************************************************************/
2212 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2216 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2218 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2225 /****************************************************************************
2226 Reply to query a security descriptor.
2227 ****************************************************************************/
2229 static int call_nt_transact_query_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2230 uint16 **ppsetup, uint32 setup_count,
2231 char **ppparams, uint32 parameter_count,
2232 char **ppdata, uint32 data_count, uint32 max_data_count)
2234 char *params = *ppparams;
2235 char *data = *ppdata;
2237 SEC_DESC *psd = NULL;
2239 uint32 security_info_wanted;
2240 TALLOC_CTX *mem_ctx;
2241 files_struct *fsp = NULL;
2243 if(parameter_count < 8) {
2244 return ERROR_DOS(ERRDOS,ERRbadfunc);
2247 fsp = file_fsp(SVAL(params,0));
2249 return ERROR_DOS(ERRDOS,ERRbadfid);
2252 security_info_wanted = IVAL(params,4);
2254 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2255 (unsigned int)security_info_wanted ));
2257 params = nttrans_realloc(ppparams, 4);
2258 if(params == NULL) {
2259 return ERROR_DOS(ERRDOS,ERRnomem);
2262 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2263 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2264 return ERROR_DOS(ERRDOS,ERRnomem);
2268 * Get the permissions to return.
2271 if (!lp_nt_acl_support(SNUM(conn))) {
2272 sd_size = get_null_nt_acl(mem_ctx, &psd);
2274 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
2278 talloc_destroy(mem_ctx);
2279 return(UNIXERROR(ERRDOS,ERRnoaccess));
2282 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2284 SIVAL(params,0,(uint32)sd_size);
2286 if(max_data_count < sd_size) {
2288 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
2289 params, 4, *ppdata, 0);
2290 talloc_destroy(mem_ctx);
2295 * Allocate the data we will point this at.
2298 data = nttrans_realloc(ppdata, sd_size);
2300 talloc_destroy(mem_ctx);
2301 return ERROR_DOS(ERRDOS,ERRnomem);
2305 * Init the parse struct we will marshall into.
2308 prs_init(&pd, 0, mem_ctx, MARSHALL);
2311 * Setup the prs_struct to point at the memory we just
2315 prs_give_memory( &pd, data, (uint32)sd_size, False);
2318 * Finally, linearize into the outgoing buffer.
2321 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2322 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2323 security descriptor.\n"));
2325 * Return access denied for want of a better error message..
2327 talloc_destroy(mem_ctx);
2328 return(UNIXERROR(ERRDOS,ERRnoaccess));
2332 * Now we can delete the security descriptor.
2335 talloc_destroy(mem_ctx);
2337 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data,
2342 /****************************************************************************
2343 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2344 ****************************************************************************/
2346 static int call_nt_transact_set_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2347 uint16 **ppsetup, uint32 setup_count,
2348 char **ppparams, uint32 parameter_count,
2349 char **ppdata, uint32 data_count, uint32 max_data_count)
2351 char *params= *ppparams;
2352 char *data = *ppdata;
2353 files_struct *fsp = NULL;
2354 uint32 security_info_sent = 0;
2357 if(parameter_count < 8) {
2358 return ERROR_DOS(ERRDOS,ERRbadfunc);
2361 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
2362 return ERROR_DOS(ERRDOS,ERRbadfid);
2365 if(!lp_nt_acl_support(SNUM(conn))) {
2369 security_info_sent = IVAL(params,4);
2371 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2372 (unsigned int)security_info_sent ));
2374 if (data_count == 0) {
2375 return ERROR_DOS(ERRDOS, ERRnoaccess);
2378 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2379 return ERROR_NT(nt_status);
2384 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2388 /****************************************************************************
2390 ****************************************************************************/
2392 static int call_nt_transact_ioctl(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2393 uint16 **ppsetup, uint32 setup_count,
2394 char **ppparams, uint32 parameter_count,
2395 char **ppdata, uint32 data_count, uint32 max_data_count)
2402 static BOOL logged_message;
2403 char *pdata = *ppdata;
2405 if (setup_count != 8) {
2406 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2407 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2410 function = IVAL(*ppsetup, 0);
2411 fidnum = SVAL(*ppsetup, 4);
2412 isFSctl = CVAL(*ppsetup, 6);
2413 compfilter = CVAL(*ppsetup, 7);
2415 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2416 function, fidnum, isFSctl, compfilter));
2418 fsp=file_fsp(SVAL(ppsetup, 4));
2419 /* this check is done in each implemented function case for now
2420 because I don't want to break anything... --metze
2421 FSP_BELONGS_CONN(fsp,conn);*/
2424 case FSCTL_SET_SPARSE:
2425 /* pretend this succeeded - tho strictly we should
2426 mark the file sparse (if the local fs supports it)
2427 so we can know if we need to pre-allocate or not */
2429 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2430 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL,
2434 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2436 unsigned char objid[16];
2438 /* This should return the object-id on this file.
2439 * I think I'll make this be the inode+dev. JRA.
2442 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2445 pdata = nttrans_realloc(ppdata, data_count);
2446 if (pdata == NULL) {
2447 return ERROR_NT(NT_STATUS_NO_MEMORY);
2449 push_file_id_16(pdata, &fsp->file_id);
2450 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2451 push_file_id_16(pdata+32, &fsp->file_id);
2452 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, pdata, data_count);
2456 case FSCTL_GET_REPARSE_POINT:
2457 /* pretend this fail - my winXP does it like this
2461 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2462 return ERROR_NT(NT_STATUS_NOT_A_REPARSE_POINT);
2464 case FSCTL_SET_REPARSE_POINT:
2465 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2469 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2470 return ERROR_NT(NT_STATUS_NOT_A_REPARSE_POINT);
2472 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2475 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2476 * and return their volume names. If max_data_count is 16, then it is just
2477 * asking for the number of volumes and length of the combined names.
2479 * pdata is the data allocated by our caller, but that uses
2480 * total_data_count (which is 0 in our case) rather than max_data_count.
2481 * Allocate the correct amount and return the pointer to let
2482 * it be deallocated when we return.
2484 SHADOW_COPY_DATA *shadow_data = NULL;
2485 TALLOC_CTX *shadow_mem_ctx = NULL;
2486 BOOL labels = False;
2487 uint32 labels_data_count = 0;
2491 FSP_BELONGS_CONN(fsp,conn);
2493 if (max_data_count < 16) {
2494 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2496 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2499 if (max_data_count > 16) {
2503 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2504 if (shadow_mem_ctx == NULL) {
2505 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2506 return ERROR_NT(NT_STATUS_NO_MEMORY);
2509 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2510 if (shadow_data == NULL) {
2511 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2512 talloc_destroy(shadow_mem_ctx);
2513 return ERROR_NT(NT_STATUS_NO_MEMORY);
2516 shadow_data->mem_ctx = shadow_mem_ctx;
2519 * Call the VFS routine to actually do the work.
2521 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2522 talloc_destroy(shadow_data->mem_ctx);
2523 if (errno == ENOSYS) {
2524 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2525 conn->connectpath));
2526 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2528 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2529 conn->connectpath));
2530 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
2534 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2539 data_count = 12+labels_data_count+4;
2542 if (max_data_count<data_count) {
2543 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2544 max_data_count,data_count));
2545 talloc_destroy(shadow_data->mem_ctx);
2546 return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
2549 pdata = nttrans_realloc(ppdata, data_count);
2550 if (pdata == NULL) {
2551 talloc_destroy(shadow_data->mem_ctx);
2552 return ERROR_NT(NT_STATUS_NO_MEMORY);
2557 /* num_volumes 4 bytes */
2558 SIVAL(pdata,0,shadow_data->num_volumes);
2561 /* num_labels 4 bytes */
2562 SIVAL(pdata,4,shadow_data->num_volumes);
2565 /* needed_data_count 4 bytes */
2566 SIVAL(pdata,8,labels_data_count);
2570 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2571 shadow_data->num_volumes,fsp->fsp_name));
2572 if (labels && shadow_data->labels) {
2573 for (i=0;i<shadow_data->num_volumes;i++) {
2574 srvstr_push(outbuf, SVAL(outbuf, smb_flg2),
2575 cur_pdata, shadow_data->labels[i],
2576 2*sizeof(SHADOW_COPY_LABEL),
2577 STR_UNICODE|STR_TERMINATE);
2578 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2579 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2583 talloc_destroy(shadow_data->mem_ctx);
2585 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0,
2591 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2593 /* pretend this succeeded -
2595 * we have to send back a list with all files owned by this SID
2597 * but I have to check that --metze
2601 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2603 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2605 FSP_BELONGS_CONN(fsp,conn);
2607 /* unknown 4 bytes: this is not the length of the sid :-( */
2608 /*unknown = IVAL(pdata,0);*/
2610 sid_parse(pdata+4,sid_len,&sid);
2611 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2613 if (!sid_to_uid(&sid, &uid)) {
2614 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2615 sid_string_static(&sid),(unsigned long)sid_len));
2619 /* we can take a look at the find source :-)
2621 * find ./ -uid $uid -name '*' is what we need here
2624 * and send 4bytes len and then NULL terminated unicode strings
2627 * but I don't know how to deal with the paged results
2628 * (maybe we can hang the result anywhere in the fsp struct)
2630 * we don't send all files at once
2631 * and at the next we should *not* start from the beginning,
2632 * so we have to cache the result
2637 /* this works for now... */
2638 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0,
2643 if (!logged_message) {
2644 logged_message = True; /* Only print this once... */
2645 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2650 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2654 #ifdef HAVE_SYS_QUOTAS
2655 /****************************************************************************
2656 Reply to get user quota
2657 ****************************************************************************/
2659 static int call_nt_transact_get_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2660 uint16 **ppsetup, uint32 setup_count,
2661 char **ppparams, uint32 parameter_count,
2662 char **ppdata, uint32 data_count, uint32 max_data_count)
2664 NTSTATUS nt_status = NT_STATUS_OK;
2665 char *params = *ppparams;
2666 char *pdata = *ppdata;
2668 int data_len=0,param_len=0;
2671 files_struct *fsp = NULL;
2675 BOOL start_enum = True;
2676 SMB_NTQUOTA_STRUCT qt;
2677 SMB_NTQUOTA_LIST *tmp_list;
2678 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2683 if (current_user.ut.uid != 0) {
2684 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2685 lp_servicename(SNUM(conn)),conn->user));
2686 return ERROR_DOS(ERRDOS,ERRnoaccess);
2690 * Ensure minimum number of parameters sent.
2693 if (parameter_count < 4) {
2694 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2695 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2698 /* maybe we can check the quota_fnum */
2699 fsp = file_fsp(SVAL(params,0));
2700 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2701 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2702 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2705 /* the NULL pointer checking for fsp->fake_file_handle->pd
2706 * is done by CHECK_NTQUOTA_HANDLE_OK()
2708 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2710 level = SVAL(params,2);
2712 /* unknown 12 bytes leading in params */
2715 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2716 /* seems that we should continue with the enum here --metze */
2718 if (qt_handle->quota_list!=NULL &&
2719 qt_handle->tmp_list==NULL) {
2722 free_ntquota_list(&(qt_handle->quota_list));
2724 /* Realloc the size of parameters and data we will return */
2726 params = nttrans_realloc(ppparams, param_len);
2727 if(params == NULL) {
2728 return ERROR_DOS(ERRDOS,ERRnomem);
2732 SIVAL(params,0,data_len);
2739 case TRANSACT_GET_USER_QUOTA_LIST_START:
2741 if (qt_handle->quota_list==NULL &&
2742 qt_handle->tmp_list==NULL) {
2746 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
2747 return ERROR_DOS(ERRSRV,ERRerror);
2749 /* Realloc the size of parameters and data we will return */
2751 params = nttrans_realloc(ppparams, param_len);
2752 if(params == NULL) {
2753 return ERROR_DOS(ERRDOS,ERRnomem);
2756 /* we should not trust the value in max_data_count*/
2757 max_data_count = MIN(max_data_count,2048);
2759 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2761 return ERROR_DOS(ERRDOS,ERRnomem);
2766 /* set params Size of returned Quota Data 4 bytes*/
2767 /* but set it later when we know it */
2769 /* for each entry push the data */
2772 qt_handle->tmp_list = qt_handle->quota_list;
2775 tmp_list = qt_handle->tmp_list;
2777 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2778 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2780 sid_len = sid_size(&tmp_list->quotas->sid);
2781 entry_len = 40 + sid_len;
2783 /* nextoffset entry 4 bytes */
2784 SIVAL(entry,0,entry_len);
2786 /* then the len of the SID 4 bytes */
2787 SIVAL(entry,4,sid_len);
2789 /* unknown data 8 bytes SMB_BIG_UINT */
2790 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2792 /* the used disk space 8 bytes SMB_BIG_UINT */
2793 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2795 /* the soft quotas 8 bytes SMB_BIG_UINT */
2796 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2798 /* the hard quotas 8 bytes SMB_BIG_UINT */
2799 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2801 /* and now the SID */
2802 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2805 qt_handle->tmp_list = tmp_list;
2807 /* overwrite the offset of the last entry */
2808 SIVAL(entry-entry_len,0,0);
2810 data_len = 4+qt_len;
2811 /* overwrite the params quota_data_len */
2812 SIVAL(params,0,data_len);
2816 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2818 /* unknown 4 bytes IVAL(pdata,0) */
2820 if (data_count < 8) {
2821 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2822 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2825 sid_len = IVAL(pdata,4);
2826 /* Ensure this is less than 1mb. */
2827 if (sid_len > (1024*1024)) {
2828 return ERROR_DOS(ERRDOS,ERRnomem);
2831 if (data_count < 8+sid_len) {
2832 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2833 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2836 data_len = 4+40+sid_len;
2838 if (max_data_count < data_len) {
2839 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2840 max_data_count, data_len));
2842 SIVAL(params,0,data_len);
2844 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2848 sid_parse(pdata+8,sid_len,&sid);
2850 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2853 * we have to return zero's in all fields
2854 * instead of returning an error here
2859 /* Realloc the size of parameters and data we will return */
2861 params = nttrans_realloc(ppparams, param_len);
2862 if(params == NULL) {
2863 return ERROR_DOS(ERRDOS,ERRnomem);
2866 pdata = nttrans_realloc(ppdata, data_len);
2868 return ERROR_DOS(ERRDOS,ERRnomem);
2873 /* set params Size of returned Quota Data 4 bytes*/
2874 SIVAL(params,0,data_len);
2876 /* nextoffset entry 4 bytes */
2879 /* then the len of the SID 4 bytes */
2880 SIVAL(entry,4,sid_len);
2882 /* unknown data 8 bytes SMB_BIG_UINT */
2883 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2885 /* the used disk space 8 bytes SMB_BIG_UINT */
2886 SBIG_UINT(entry,16,qt.usedspace);
2888 /* the soft quotas 8 bytes SMB_BIG_UINT */
2889 SBIG_UINT(entry,24,qt.softlim);
2891 /* the hard quotas 8 bytes SMB_BIG_UINT */
2892 SBIG_UINT(entry,32,qt.hardlim);
2894 /* and now the SID */
2895 sid_linearize(entry+40, sid_len, &sid);
2900 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2901 return ERROR_DOS(ERRSRV,ERRerror);
2905 send_nt_replies(inbuf, outbuf, bufsize, nt_status, params, param_len,
2911 /****************************************************************************
2912 Reply to set user quota
2913 ****************************************************************************/
2915 static int call_nt_transact_set_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2916 uint16 **ppsetup, uint32 setup_count,
2917 char **ppparams, uint32 parameter_count,
2918 char **ppdata, uint32 data_count, uint32 max_data_count)
2920 char *params = *ppparams;
2921 char *pdata = *ppdata;
2922 int data_len=0,param_len=0;
2923 SMB_NTQUOTA_STRUCT qt;
2926 files_struct *fsp = NULL;
2931 if (current_user.ut.uid != 0) {
2932 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2933 lp_servicename(SNUM(conn)),conn->user));
2934 return ERROR_DOS(ERRDOS,ERRnoaccess);
2938 * Ensure minimum number of parameters sent.
2941 if (parameter_count < 2) {
2942 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2943 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2946 /* maybe we can check the quota_fnum */
2947 fsp = file_fsp(SVAL(params,0));
2948 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2949 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2950 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2953 if (data_count < 40) {
2954 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2955 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2958 /* offset to next quota record.
2959 * 4 bytes IVAL(pdata,0)
2964 sid_len = IVAL(pdata,4);
2966 if (data_count < 40+sid_len) {
2967 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2968 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2971 /* unknown 8 bytes in pdata
2972 * maybe its the change time in NTTIME
2975 /* the used space 8 bytes (SMB_BIG_UINT)*/
2976 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2977 #ifdef LARGE_SMB_OFF_T
2978 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2979 #else /* LARGE_SMB_OFF_T */
2980 if ((IVAL(pdata,20) != 0)&&
2981 ((qt.usedspace != 0xFFFFFFFF)||
2982 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2983 /* more than 32 bits? */
2984 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2986 #endif /* LARGE_SMB_OFF_T */
2988 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2989 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2990 #ifdef LARGE_SMB_OFF_T
2991 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2992 #else /* LARGE_SMB_OFF_T */
2993 if ((IVAL(pdata,28) != 0)&&
2994 ((qt.softlim != 0xFFFFFFFF)||
2995 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2996 /* more than 32 bits? */
2997 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2999 #endif /* LARGE_SMB_OFF_T */
3001 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
3002 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
3003 #ifdef LARGE_SMB_OFF_T
3004 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
3005 #else /* LARGE_SMB_OFF_T */
3006 if ((IVAL(pdata,36) != 0)&&
3007 ((qt.hardlim != 0xFFFFFFFF)||
3008 (IVAL(pdata,36)!=0xFFFFFFFF))) {
3009 /* more than 32 bits? */
3010 return ERROR_DOS(ERRDOS,ERRunknownlevel);
3012 #endif /* LARGE_SMB_OFF_T */
3014 sid_parse(pdata+40,sid_len,&sid);
3015 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
3017 /* 44 unknown bytes left... */
3019 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
3020 return ERROR_DOS(ERRSRV,ERRerror);
3023 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len,
3028 #endif /* HAVE_SYS_QUOTAS */
3030 static int handle_nttrans(connection_struct *conn,
3031 struct trans_state *state,
3032 char *inbuf, char *outbuf, int size, int bufsize)
3036 if (Protocol >= PROTOCOL_NT1) {
3037 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | 0x40); /* IS_LONG_NAME */
3040 /* Now we must call the relevant NT_TRANS function */
3041 switch(state->call) {
3042 case NT_TRANSACT_CREATE:
3044 START_PROFILE(NT_transact_create);
3045 outsize = call_nt_transact_create(conn, inbuf, outbuf,
3047 &state->setup, state->setup_count,
3048 &state->param, state->total_param,
3049 &state->data, state->total_data,
3050 state->max_data_return);
3051 END_PROFILE(NT_transact_create);
3055 case NT_TRANSACT_IOCTL:
3057 START_PROFILE(NT_transact_ioctl);
3058 outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
3060 &state->setup, state->setup_count,
3061 &state->param, state->total_param,
3062 &state->data, state->total_data, state->max_data_return);
3063 END_PROFILE(NT_transact_ioctl);
3067 case NT_TRANSACT_SET_SECURITY_DESC:
3069 START_PROFILE(NT_transact_set_security_desc);
3070 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
3072 &state->setup, state->setup_count,
3073 &state->param, state->total_param,
3074 &state->data, state->total_data, state->max_data_return);
3075 END_PROFILE(NT_transact_set_security_desc);
3079 case NT_TRANSACT_NOTIFY_CHANGE:
3081 START_PROFILE(NT_transact_notify_change);
3082 outsize = call_nt_transact_notify_change(
3083 conn, inbuf, outbuf, size, bufsize,
3084 &state->setup, state->setup_count,
3085 &state->param, state->total_param,
3086 &state->data, state->total_data,
3087 state->max_data_return,
3088 state->max_param_return);
3089 END_PROFILE(NT_transact_notify_change);
3093 case NT_TRANSACT_RENAME:
3095 START_PROFILE(NT_transact_rename);
3096 outsize = call_nt_transact_rename(conn, inbuf, outbuf,
3098 &state->setup, state->setup_count,
3099 &state->param, state->total_param,
3100 &state->data, state->total_data, state->max_data_return);
3101 END_PROFILE(NT_transact_rename);
3105 case NT_TRANSACT_QUERY_SECURITY_DESC:
3107 START_PROFILE(NT_transact_query_security_desc);
3108 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
3110 &state->setup, state->setup_count,
3111 &state->param, state->total_param,
3112 &state->data, state->total_data, state->max_data_return);
3113 END_PROFILE(NT_transact_query_security_desc);
3117 #ifdef HAVE_SYS_QUOTAS
3118 case NT_TRANSACT_GET_USER_QUOTA:
3120 START_PROFILE(NT_transact_get_user_quota);
3121 outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf,
3123 &state->setup, state->setup_count,
3124 &state->param, state->total_param,
3125 &state->data, state->total_data, state->max_data_return);
3126 END_PROFILE(NT_transact_get_user_quota);
3130 case NT_TRANSACT_SET_USER_QUOTA:
3132 START_PROFILE(NT_transact_set_user_quota);
3133 outsize = call_nt_transact_set_user_quota(conn, inbuf, outbuf,
3135 &state->setup, state->setup_count,
3136 &state->param, state->total_param,
3137 &state->data, state->total_data, state->max_data_return);
3138 END_PROFILE(NT_transact_set_user_quota);
3141 #endif /* HAVE_SYS_QUOTAS */
3144 /* Error in request */
3145 DEBUG(0,("handle_nttrans: Unknown request %d in "
3146 "nttrans call\n", state->call));
3147 return ERROR_DOS(ERRSRV,ERRerror);
3152 /****************************************************************************
3153 Reply to a SMBNTtrans.
3154 ****************************************************************************/
3156 int reply_nttrans(connection_struct *conn,
3157 char *inbuf,char *outbuf,int size,int bufsize)
3160 uint32 pscnt = IVAL(inbuf,smb_nt_ParameterCount);
3161 uint32 psoff = IVAL(inbuf,smb_nt_ParameterOffset);
3162 uint32 dscnt = IVAL(inbuf,smb_nt_DataCount);
3163 uint32 dsoff = IVAL(inbuf,smb_nt_DataOffset);
3165 uint16 function_code = SVAL( inbuf, smb_nt_Function);
3167 struct trans_state *state;
3169 START_PROFILE(SMBnttrans);
3171 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3172 END_PROFILE(SMBnttrans);
3173 return ERROR_DOS(ERRSRV,ERRaccess);
3176 result = allow_new_trans(conn->pending_trans, SVAL(inbuf, smb_mid));
3177 if (!NT_STATUS_IS_OK(result)) {
3178 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3179 END_PROFILE(SMBnttrans);
3180 return ERROR_NT(result);
3183 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3184 END_PROFILE(SMBnttrans);
3185 return ERROR_DOS(ERRSRV,ERRaccess);
3188 state->cmd = SMBnttrans;
3190 state->mid = SVAL(inbuf,smb_mid);
3191 state->vuid = SVAL(inbuf,smb_uid);
3192 state->total_data = IVAL(inbuf, smb_nt_TotalDataCount);
3194 state->total_param = IVAL(inbuf, smb_nt_TotalParameterCount);
3195 state->param = NULL;
3196 state->max_data_return = IVAL(inbuf,smb_nt_MaxDataCount);
3197 state->max_param_return = IVAL(inbuf,smb_nt_MaxParameterCount);
3199 /* setup count is in *words* */
3200 state->setup_count = 2*CVAL(inbuf,smb_nt_SetupCount);
3201 state->setup = NULL;
3202 state->call = function_code;
3205 * All nttrans messages we handle have smb_wct == 19 +
3206 * state->setup_count. Ensure this is so as a sanity check.
3209 if(CVAL(inbuf, smb_wct) != 19 + (state->setup_count/2)) {
3210 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3211 CVAL(inbuf, smb_wct), 19 + (state->setup_count/2)));
3215 /* Don't allow more than 128mb for each value. */
3216 if ((state->total_data > (1024*1024*128)) ||
3217 (state->total_param > (1024*1024*128))) {
3218 END_PROFILE(SMBnttrans);
3219 return ERROR_DOS(ERRDOS,ERRnomem);
3222 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3225 if (state->total_data) {
3226 /* Can't use talloc here, the core routines do realloc on the
3227 * params and data. */
3228 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3229 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3230 "bytes !\n", (unsigned int)state->total_data));
3232 END_PROFILE(SMBnttrans);
3233 return(ERROR_DOS(ERRDOS,ERRnomem));
3235 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3237 if ((smb_base(inbuf)+dsoff+dscnt > inbuf + size) ||
3238 (smb_base(inbuf)+dsoff+dscnt < smb_base(inbuf)))
3241 memcpy(state->data,smb_base(inbuf)+dsoff,dscnt);
3244 if (state->total_param) {
3245 /* Can't use talloc here, the core routines do realloc on the
3246 * params and data. */
3247 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3248 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3249 "bytes !\n", (unsigned int)state->total_param));
3250 SAFE_FREE(state->data);
3252 END_PROFILE(SMBnttrans);
3253 return(ERROR_DOS(ERRDOS,ERRnomem));
3255 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3257 if ((smb_base(inbuf)+psoff+pscnt > inbuf + size) ||
3258 (smb_base(inbuf)+psoff+pscnt < smb_base(inbuf)))
3261 memcpy(state->param,smb_base(inbuf)+psoff,pscnt);
3264 state->received_data = dscnt;
3265 state->received_param = pscnt;
3267 if(state->setup_count > 0) {
3268 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3269 state->setup_count));
3270 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3271 if (state->setup == NULL) {
3272 DEBUG(0,("reply_nttrans : Out of memory\n"));
3273 SAFE_FREE(state->data);
3274 SAFE_FREE(state->param);
3276 END_PROFILE(SMBnttrans);
3277 return ERROR_DOS(ERRDOS,ERRnomem);
3280 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3281 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3284 if (smb_nt_SetupStart + state->setup_count > size) {
3288 memcpy( state->setup, &inbuf[smb_nt_SetupStart], state->setup_count);
3289 dump_data(10, (uint8 *)state->setup, state->setup_count);
3292 if ((state->received_data == state->total_data) &&
3293 (state->received_param == state->total_param)) {
3294 outsize = handle_nttrans(conn, state, inbuf, outbuf,
3296 SAFE_FREE(state->param);
3297 SAFE_FREE(state->data);
3299 END_PROFILE(SMBnttrans);
3303 DLIST_ADD(conn->pending_trans, state);
3305 /* We need to send an interim response then receive the rest
3306 of the parameter/data bytes */
3307 outsize = set_message(inbuf,outbuf,0,0,False);
3309 END_PROFILE(SMBnttrans);
3314 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3315 SAFE_FREE(state->data);
3316 SAFE_FREE(state->param);
3318 END_PROFILE(SMBnttrans);
3319 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
3322 /****************************************************************************
3323 Reply to a SMBnttranss
3324 ****************************************************************************/
3326 int reply_nttranss(connection_struct *conn, char *inbuf,char *outbuf,
3327 int size,int bufsize)
3330 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3331 struct trans_state *state;
3333 START_PROFILE(SMBnttranss);
3337 for (state = conn->pending_trans; state != NULL;
3338 state = state->next) {
3339 if (state->mid == SVAL(inbuf,smb_mid)) {
3344 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3345 END_PROFILE(SMBnttranss);
3346 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
3349 /* Revise state->total_param and state->total_data in case they have
3350 changed downwards */
3351 if (IVAL(inbuf, smb_nts_TotalParameterCount) < state->total_param) {
3352 state->total_param = IVAL(inbuf, smb_nts_TotalParameterCount);
3354 if (IVAL(inbuf, smb_nts_TotalDataCount) < state->total_data) {
3355 state->total_data = IVAL(inbuf, smb_nts_TotalDataCount);
3358 pcnt = IVAL(inbuf,smb_nts_ParameterCount);
3359 poff = IVAL(inbuf, smb_nts_ParameterOffset);
3360 pdisp = IVAL(inbuf, smb_nts_ParameterDisplacement);
3362 dcnt = IVAL(inbuf, smb_nts_DataCount);
3363 ddisp = IVAL(inbuf, smb_nts_DataDisplacement);
3364 doff = IVAL(inbuf, smb_nts_DataOffset);
3366 state->received_param += pcnt;
3367 state->received_data += dcnt;
3369 if ((state->received_data > state->total_data) ||
3370 (state->received_param > state->total_param))
3374 if (pdisp+pcnt > state->total_param)
3376 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3378 if (pdisp > state->total_param)
3380 if ((smb_base(inbuf) + poff + pcnt > inbuf + size) ||
3381 (smb_base(inbuf) + poff + pcnt < smb_base(inbuf)))
3383 if (state->param + pdisp < state->param)
3386 memcpy(state->param+pdisp,smb_base(inbuf)+poff,
3391 if (ddisp+dcnt > state->total_data)
3393 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3395 if (ddisp > state->total_data)
3397 if ((smb_base(inbuf) + doff + dcnt > inbuf + size) ||
3398 (smb_base(inbuf) + doff + dcnt < smb_base(inbuf)))
3400 if (state->data + ddisp < state->data)
3403 memcpy(state->data+ddisp, smb_base(inbuf)+doff,
3407 if ((state->received_param < state->total_param) ||
3408 (state->received_data < state->total_data)) {
3409 END_PROFILE(SMBnttranss);
3413 /* construct_reply_common has done us the favor to pre-fill the
3414 * command field with SMBnttranss which is wrong :-)
3416 SCVAL(outbuf,smb_com,SMBnttrans);
3418 outsize = handle_nttrans(conn, state, inbuf, outbuf,
3421 DLIST_REMOVE(conn->pending_trans, state);
3422 SAFE_FREE(state->data);
3423 SAFE_FREE(state->param);
3427 END_PROFILE(SMBnttranss);
3428 return(ERROR_DOS(ERRSRV,ERRnosupport));
3431 END_PROFILE(SMBnttranss);
3436 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3437 DLIST_REMOVE(conn->pending_trans, state);
3438 SAFE_FREE(state->data);
3439 SAFE_FREE(state->param);
3441 END_PROFILE(SMBnttranss);
3442 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);