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 void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
71 char *params, int paramsize,
72 char *pdata, int datasize)
74 int data_to_send = datasize;
75 int params_to_send = paramsize;
79 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
80 int alignment_offset = 3;
81 int data_alignment_offset = 0;
84 * If there genuinely are no parameters or data to send just send
88 if(params_to_send == 0 && data_to_send == 0) {
89 reply_outbuf(req, 18, 0);
90 show_msg((char *)req->outbuf);
95 * When sending params and data ensure that both are nicely aligned.
96 * Only do this alignment when there is also data to send - else
97 * can cause NT redirector problems.
100 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
101 data_alignment_offset = 4 - (params_to_send % 4);
105 * Space is bufsize minus Netbios over TCP header minus SMB header.
106 * The alignment_offset is to align the param bytes on a four byte
107 * boundary (2 bytes for data len, one byte pad).
108 * NT needs this to work correctly.
111 useable_space = max_send - (smb_size
114 + data_alignment_offset);
117 * useable_space can never be more than max_send minus the
121 useable_space = MIN(useable_space,
122 max_send - (alignment_offset+data_alignment_offset));
125 while (params_to_send || data_to_send) {
128 * Calculate whether we will totally or partially fill this packet.
131 total_sent_thistime = params_to_send + data_to_send +
132 alignment_offset + data_alignment_offset;
135 * We can never send more than useable_space.
138 total_sent_thistime = MIN(total_sent_thistime, useable_space);
140 reply_outbuf(req, 18, total_sent_thistime);
143 * Set total params and data to be sent.
146 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
147 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
150 * Calculate how many parameters and data we can fit into
151 * this packet. Parameters get precedence.
154 params_sent_thistime = MIN(params_to_send,useable_space);
155 data_sent_thistime = useable_space - params_sent_thistime;
156 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
158 SIVAL(req->outbuf, smb_ntr_ParameterCount,
159 params_sent_thistime);
161 if(params_sent_thistime == 0) {
162 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
163 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
166 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
167 * parameter bytes, however the first 4 bytes of outbuf are
168 * the Netbios over TCP header. Thus use smb_base() to subtract
169 * them from the calculation.
172 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
173 ((smb_buf(req->outbuf)+alignment_offset)
174 - smb_base(req->outbuf)));
176 * Absolute displacement of param bytes sent in this packet.
179 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
184 * Deal with the data portion.
187 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
189 if(data_sent_thistime == 0) {
190 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
191 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
194 * The offset of the data bytes is the offset of the
195 * parameter bytes plus the number of parameters being sent this time.
198 SIVAL(req->outbuf, smb_ntr_DataOffset,
199 ((smb_buf(req->outbuf)+alignment_offset) -
200 smb_base(req->outbuf))
201 + params_sent_thistime + data_alignment_offset);
202 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
206 * Copy the param bytes into the packet.
209 if(params_sent_thistime) {
210 if (alignment_offset != 0) {
211 memset(smb_buf(req->outbuf), 0,
214 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
215 params_sent_thistime);
219 * Copy in the data bytes
222 if(data_sent_thistime) {
223 if (data_alignment_offset != 0) {
224 memset((smb_buf(req->outbuf)+alignment_offset+
225 params_sent_thistime), 0,
226 data_alignment_offset);
228 memcpy(smb_buf(req->outbuf)+alignment_offset
229 +params_sent_thistime+data_alignment_offset,
230 pd,data_sent_thistime);
233 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
234 params_sent_thistime, data_sent_thistime, useable_space));
235 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
236 params_to_send, data_to_send, paramsize, datasize));
238 if (NT_STATUS_V(nt_error)) {
239 error_packet_set((char *)req->outbuf,
244 /* Send the packet */
245 show_msg((char *)req->outbuf);
246 if (!send_smb(smbd_server_fd(),(char *)req->outbuf)) {
247 exit_server_cleanly("send_nt_replies: send_smb failed.");
250 TALLOC_FREE(req->outbuf);
252 pp += params_sent_thistime;
253 pd += data_sent_thistime;
255 params_to_send -= params_sent_thistime;
256 data_to_send -= data_sent_thistime;
262 if(params_to_send < 0 || data_to_send < 0) {
263 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
264 params_to_send, data_to_send));
270 /****************************************************************************
271 Is it an NTFS stream name ?
272 ****************************************************************************/
274 BOOL is_ntfs_stream_name(const char *fname)
276 if (lp_posix_pathnames()) {
279 return (strchr_m(fname, ':') != NULL) ? True : False;
282 struct case_semantics_state {
283 connection_struct *conn;
286 BOOL short_case_preserve;
289 /****************************************************************************
290 Restore case semantics.
291 ****************************************************************************/
292 static int restore_case_semantics(struct case_semantics_state *state)
294 state->conn->case_sensitive = state->case_sensitive;
295 state->conn->case_preserve = state->case_preserve;
296 state->conn->short_case_preserve = state->short_case_preserve;
300 /****************************************************************************
302 ****************************************************************************/
303 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
304 connection_struct *conn)
306 struct case_semantics_state *result;
308 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
309 DEBUG(0, ("talloc failed\n"));
314 result->case_sensitive = conn->case_sensitive;
315 result->case_preserve = conn->case_preserve;
316 result->short_case_preserve = conn->short_case_preserve;
319 conn->case_sensitive = True;
320 conn->case_preserve = True;
321 conn->short_case_preserve = True;
323 talloc_set_destructor(result, restore_case_semantics);
328 /****************************************************************************
329 Reply to an NT create and X call on a pipe
330 ****************************************************************************/
332 static void nt_open_pipe(char *fname, connection_struct *conn,
333 struct smb_request *req, int *ppnum)
335 smb_np_struct *p = NULL;
338 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
340 /* See if it is one we want to handle. */
342 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
343 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
348 for( i = 0; known_nt_pipes[i]; i++ ) {
349 if( strequal(fname,known_nt_pipes[i])) {
354 if ( known_nt_pipes[i] == NULL ) {
355 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
360 /* Strip \\ off the name. */
363 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
365 p = open_rpc_pipe_p(fname, conn, req->vuid);
367 reply_doserror(req, ERRSRV, ERRnofids);
371 /* TODO: Add pipe to db */
373 if ( !store_pipe_opendb( p ) ) {
374 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
381 /****************************************************************************
382 Reply to an NT create and X call for pipes.
383 ****************************************************************************/
385 static void do_ntcreate_pipe_open(connection_struct *conn,
386 struct smb_request *req)
391 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
393 srvstr_pull_buf((char *)req->inbuf, req->flags2, fname,
394 smb_buf(req->inbuf), sizeof(fname), STR_TERMINATE);
396 nt_open_pipe(fname, conn, req, &pnum);
404 * Deal with pipe return.
407 if (flags & EXTENDED_RESPONSE_REQUIRED) {
408 /* This is very strange. We
409 * return 50 words, but only set
410 * the wcnt to 42 ? It's definately
411 * what happens on the wire....
413 reply_outbuf(req, 50, 0);
414 SCVAL(req->outbuf,smb_wct,42);
416 reply_outbuf(req, 34, 0);
419 p = (char *)req->outbuf + smb_vwv2;
423 SIVAL(p,0,FILE_WAS_OPENED);
426 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
429 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
431 SSVAL(p,2, 0x5FF); /* ? */
434 if (flags & EXTENDED_RESPONSE_REQUIRED) {
436 SIVAL(p,0,FILE_GENERIC_ALL);
438 * For pipes W2K3 seems to return
440 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
442 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
445 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
450 /****************************************************************************
451 Reply to an NT create and X call for a quota file.
452 ****************************************************************************/
454 static void reply_ntcreate_and_X_quota(connection_struct *conn,
455 struct smb_request *req,
456 enum FAKE_FILE_TYPE fake_file_type,
460 uint32 desired_access = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
464 status = open_fake_file(conn, fake_file_type, fname, desired_access,
467 if (!NT_STATUS_IS_OK(status)) {
468 reply_nterror(req, status);
472 reply_outbuf(req, 34, 0);
474 p = (char *)req->outbuf + smb_vwv2;
476 /* SCVAL(p,0,NO_OPLOCK_RETURN); */
478 SSVAL(p,0,fsp->fnum);
480 DEBUG(5,("reply_ntcreate_and_X_quota: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
485 /****************************************************************************
486 Reply to an NT create and X call.
487 ****************************************************************************/
489 void reply_ntcreate_and_X(connection_struct *conn,
490 struct smb_request *req)
496 uint32 file_attributes;
498 uint32 create_disposition;
499 uint32 create_options;
501 SMB_BIG_UINT allocation_size;
502 /* Breakout the oplock request bits so we can set the
503 reply bits separately. */
504 int oplock_request = 0;
506 SMB_OFF_T file_len = 0;
507 SMB_STRUCT_STAT sbuf;
509 files_struct *fsp = NULL;
511 struct timespec c_timespec;
512 struct timespec a_timespec;
513 struct timespec m_timespec;
514 BOOL extended_oplock_granted = False;
516 struct case_semantics_state *case_state = NULL;
518 START_PROFILE(SMBntcreateX);
521 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
525 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
526 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
527 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
528 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
529 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
530 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
531 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
533 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,smb_ntcreate_AllocationSize);
534 #ifdef LARGE_SMB_OFF_T
535 allocation_size |= (((SMB_BIG_UINT)IVAL(req->inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
538 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
539 "file_attributes = 0x%x, share_access = 0x%x, "
540 "create_disposition = 0x%x create_options = 0x%x "
541 "root_dir_fid = 0x%x\n",
543 (unsigned int)access_mask,
544 (unsigned int)file_attributes,
545 (unsigned int)share_access,
546 (unsigned int)create_disposition,
547 (unsigned int)create_options,
548 (unsigned int)root_dir_fid ));
551 * If it's an IPC, use the pipe handler.
555 if (lp_nt_pipe_support()) {
556 do_ntcreate_pipe_open(conn, req);
557 END_PROFILE(SMBntcreateX);
560 reply_doserror(req, ERRDOS, ERRnoaccess);
561 END_PROFILE(SMBntcreateX);
566 if (create_options & FILE_OPEN_BY_FILE_ID) {
567 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
568 END_PROFILE(SMBntcreateX);
576 if(root_dir_fid != 0) {
578 * This filename is relative to a directory fid.
581 files_struct *dir_fsp = file_fsp(
582 SVAL(req->inbuf, smb_ntcreate_RootDirectoryFid));
586 reply_doserror(req, ERRDOS, ERRbadfid);
587 END_PROFILE(SMBntcreateX);
591 if(!dir_fsp->is_directory) {
593 srvstr_get_path((char *)req->inbuf, req->flags2, fname_in,
594 smb_buf(req->inbuf), sizeof(fname_in), 0,
595 STR_TERMINATE, &status);
596 if (!NT_STATUS_IS_OK(status)) {
597 reply_nterror(req, status);
598 END_PROFILE(SMBntcreateX);
603 * Check to see if this is a mac fork of some kind.
606 if( is_ntfs_stream_name(fname_in)) {
608 req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
609 END_PROFILE(SMBntcreateX);
614 we need to handle the case when we get a
615 relative open relative to a file and the
616 pathname is blank - this is a reopen!
617 (hint from demyn plantenberg)
620 reply_doserror(req, ERRDOS, ERRbadfid);
621 END_PROFILE(SMBntcreateX);
626 * Copy in the base directory name.
629 pstrcpy( fname_in, dir_fsp->fsp_name );
630 dir_name_len = strlen(fname_in);
633 * Ensure it ends in a '\'.
636 if((fname_in[dir_name_len-1] != '\\') && (fname_in[dir_name_len-1] != '/')) {
637 pstrcat(fname_in, "/");
641 srvstr_get_path((char *)req->inbuf, req->flags2, rel_fname,
642 smb_buf(req->inbuf), sizeof(rel_fname), 0,
643 STR_TERMINATE, &status);
644 if (!NT_STATUS_IS_OK(status)) {
645 reply_nterror(req, status);
646 END_PROFILE(SMBntcreateX);
649 pstrcat(fname_in, rel_fname);
651 srvstr_get_path((char *)req->inbuf, req->flags2, fname_in,
652 smb_buf(req->inbuf), sizeof(fname_in), 0,
653 STR_TERMINATE, &status);
654 if (!NT_STATUS_IS_OK(status)) {
655 reply_nterror(req, status);
656 END_PROFILE(SMBntcreateX);
661 * Check to see if this is a mac fork of some kind.
664 if( is_ntfs_stream_name(fname_in)) {
665 enum FAKE_FILE_TYPE fake_file_type = is_fake_file(fname_in);
666 if (fake_file_type!=FAKE_FILE_TYPE_NONE) {
668 * Here we go! support for changing the disk quotas --metze
670 * We need to fake up to open this MAGIC QUOTA file
671 * and return a valid FID.
673 * w2k close this file directly after openening
674 * xp also tries a QUERY_FILE_INFO on the file and then close it
676 reply_ntcreate_and_X_quota(conn, req,
677 fake_file_type, fname_in);
679 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
681 END_PROFILE(SMBntcreateX);
687 * Now contruct the smb_open_mode value from the filename,
688 * desired access and the share access.
690 status = resolve_dfspath(conn, req->flags2 & FLAGS2_DFS_PATHNAMES, fname_in);
691 if (!NT_STATUS_IS_OK(status)) {
692 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
693 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
697 reply_nterror(req, status);
699 END_PROFILE(SMBntcreateX);
703 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
704 if (oplock_request) {
705 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
709 * Ordinary file or directory.
713 * Check if POSIX semantics are wanted.
716 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
717 case_state = set_posix_case_semantics(NULL, conn);
718 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
721 status = unix_convert(conn, fname_in, False, &fname, NULL, &sbuf);
722 if (!NT_STATUS_IS_OK(status)) {
723 TALLOC_FREE(case_state);
724 reply_nterror(req, status);
725 END_PROFILE(SMBntcreateX);
728 /* All file access must go through check_name() */
729 status = check_name(conn, fname);
730 if (!NT_STATUS_IS_OK(status)) {
731 TALLOC_FREE(case_state);
732 reply_nterror(req, status);
733 END_PROFILE(SMBntcreateX);
737 /* This is the correct thing to do (check every time) but can_delete is
738 expensive (it may have to read the parent directory permissions). So
739 for now we're not doing it unless we have a strong hint the client
740 is really going to delete this file. If the client is forcing FILE_CREATE
741 let the filesystem take care of the permissions. */
743 /* Setting FILE_SHARE_DELETE is the hint. */
745 if (lp_acl_check_permissions(SNUM(conn))
746 && (create_disposition != FILE_CREATE)
747 && (share_access & FILE_SHARE_DELETE)
748 && (access_mask & DELETE_ACCESS)) {
749 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
750 !can_delete_file_in_directory(conn, fname)) {
751 TALLOC_FREE(case_state);
752 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
753 END_PROFILE(SMBntcreateX);
759 /* We need to support SeSecurityPrivilege for this. */
760 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
761 !user_has_privileges(current_user.nt_user_token,
763 TALLOC_FREE(case_state);
764 END_PROFILE(SMBntcreateX);
765 return ERROR_NT(NT_STATUS_PRIVILEGE_NOT_HELD);
770 * If it's a request for a directory open, deal with it separately.
773 if(create_options & FILE_DIRECTORY_FILE) {
775 /* Can't open a temp directory. IFS kit test. */
776 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
777 TALLOC_FREE(case_state);
778 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
779 END_PROFILE(SMBntcreateX);
784 status = open_directory(conn, req, fname, &sbuf,
795 * Ordinary file case.
798 /* NB. We have a potential bug here. If we
799 * cause an oplock break to ourselves, then we
800 * could end up processing filename related
801 * SMB requests whilst we await the oplock
802 * break response. As we may have changed the
803 * filename case semantics to be POSIX-like,
804 * this could mean a filename request could
805 * fail when it should succeed. This is a rare
806 * condition, but eventually we must arrange
807 * to restore the correct case semantics
808 * before issuing an oplock break request to
809 * our client. JRA. */
811 status = open_file_ntcreate(conn, req, fname, &sbuf,
820 /* We cheat here. There are two cases we
821 * care about. One is a directory rename,
822 * where the NT client will attempt to
823 * open the source directory for
824 * DELETE access. Note that when the
825 * NT client does this it does *not*
826 * set the directory bit in the
827 * request packet. This is translated
828 * into a read/write open
829 * request. POSIX states that any open
830 * for write request on a directory
831 * will generate an EISDIR error, so
832 * we can catch this here and open a
833 * pseudo handle that is flagged as a
834 * directory. The second is an open
835 * for a permissions read only, which
836 * we handle in the open_file_stat case. JRA.
839 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
842 * Fail the open if it was explicitly a non-directory
846 if (create_options & FILE_NON_DIRECTORY_FILE) {
847 TALLOC_FREE(case_state);
848 reply_force_nterror(req,
849 NT_STATUS_FILE_IS_A_DIRECTORY);
850 END_PROFILE(SMBntcreateX);
855 status = open_directory(conn, req, fname,
866 TALLOC_FREE(case_state);
868 if (!NT_STATUS_IS_OK(status)) {
869 if (open_was_deferred(req->mid)) {
870 /* We have re-scheduled this call. */
871 END_PROFILE(SMBntcreateX);
874 reply_openerror(req, status);
875 END_PROFILE(SMBntcreateX);
879 file_len = sbuf.st_size;
880 fattr = dos_mode(conn,fname,&sbuf);
882 fattr = FILE_ATTRIBUTE_NORMAL;
884 if (!fsp->is_directory && (fattr & aDIR)) {
885 close_file(fsp,ERROR_CLOSE);
886 reply_doserror(req, ERRDOS, ERRnoaccess);
887 END_PROFILE(SMBntcreateX);
891 /* Save the requested allocation size. */
892 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
893 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
894 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
895 if (fsp->is_directory) {
896 close_file(fsp,ERROR_CLOSE);
897 /* Can't set allocation size on a directory. */
898 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
899 END_PROFILE(SMBntcreateX);
902 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
903 close_file(fsp,ERROR_CLOSE);
904 reply_nterror(req, NT_STATUS_DISK_FULL);
905 END_PROFILE(SMBntcreateX);
909 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
914 * If the caller set the extended oplock request bit
915 * and we granted one (by whatever means) - set the
916 * correct bit for extended oplock reply.
919 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
920 extended_oplock_granted = True;
923 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
924 extended_oplock_granted = True;
927 if (flags & EXTENDED_RESPONSE_REQUIRED) {
928 /* This is very strange. We
929 * return 50 words, but only set
930 * the wcnt to 42 ? It's definately
931 * what happens on the wire....
933 reply_outbuf(req, 50, 0);
934 SCVAL(req->outbuf,smb_wct,42);
936 reply_outbuf(req, 34, 0);
939 p = (char *)req->outbuf + smb_vwv2;
942 * Currently as we don't support level II oplocks we just report
943 * exclusive & batch here.
946 if (extended_oplock_granted) {
947 if (flags & REQUEST_BATCH_OPLOCK) {
948 SCVAL(p,0, BATCH_OPLOCK_RETURN);
950 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
952 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
953 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
955 SCVAL(p,0,NO_OPLOCK_RETURN);
959 SSVAL(p,0,fsp->fnum);
961 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
962 SIVAL(p,0,FILE_WAS_SUPERSEDED);
969 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
970 a_timespec = get_atimespec(&sbuf);
971 m_timespec = get_mtimespec(&sbuf);
973 if (lp_dos_filetime_resolution(SNUM(conn))) {
974 dos_filetime_timespec(&c_timespec);
975 dos_filetime_timespec(&a_timespec);
976 dos_filetime_timespec(&m_timespec);
979 put_long_date_timespec(p, c_timespec); /* create time. */
981 put_long_date_timespec(p, a_timespec); /* access time */
983 put_long_date_timespec(p, m_timespec); /* write time */
985 put_long_date_timespec(p, m_timespec); /* change time */
987 SIVAL(p,0,fattr); /* File Attributes. */
989 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
991 SOFF_T(p,0,file_len);
993 if (flags & EXTENDED_RESPONSE_REQUIRED) {
997 SCVAL(p,0,fsp->is_directory ? 1 : 0);
999 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1002 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1003 perms = FILE_GENERIC_ALL;
1005 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1010 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
1013 END_PROFILE(SMBntcreateX);
1017 /****************************************************************************
1018 Reply to a NT_TRANSACT_CREATE call to open a pipe.
1019 ****************************************************************************/
1021 static void do_nt_transact_create_pipe(connection_struct *conn,
1022 struct smb_request *req,
1023 uint16 **ppsetup, uint32 setup_count,
1024 char **ppparams, uint32 parameter_count,
1025 char **ppdata, uint32 data_count)
1028 char *params = *ppparams;
1036 * Ensure minimum number of parameters sent.
1039 if(parameter_count < 54) {
1040 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1041 reply_doserror(req, ERRDOS, ERRnoaccess);
1045 flags = IVAL(params,0);
1047 srvstr_get_path(params, req->flags2, fname, params+53,
1048 sizeof(fname), parameter_count-53, STR_TERMINATE,
1050 if (!NT_STATUS_IS_OK(status)) {
1051 reply_nterror(req, status);
1055 nt_open_pipe(fname, conn, req, &pnum);
1062 /* Realloc the size of parameters and data we will return */
1063 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1064 /* Extended response is 32 more byyes. */
1069 params = nttrans_realloc(ppparams, param_len);
1070 if(params == NULL) {
1071 reply_doserror(req, ERRDOS, ERRnomem);
1076 SCVAL(p,0,NO_OPLOCK_RETURN);
1081 SIVAL(p,0,FILE_WAS_OPENED);
1085 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1088 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1090 SSVAL(p,2, 0x5FF); /* ? */
1093 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1095 SIVAL(p,0,FILE_GENERIC_ALL);
1097 * For pipes W2K3 seems to return
1099 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
1101 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
1104 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1106 /* Send the required number of replies */
1107 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1112 /****************************************************************************
1113 Internal fn to set security descriptors.
1114 ****************************************************************************/
1116 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1119 SEC_DESC *psd = NULL;
1120 TALLOC_CTX *mem_ctx;
1123 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
1124 return NT_STATUS_OK;
1128 * Init the parse struct we will unmarshall from.
1131 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1132 DEBUG(0,("set_sd: talloc_init failed.\n"));
1133 return NT_STATUS_NO_MEMORY;
1136 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1139 * Setup the prs_struct to point at the memory we just
1143 prs_give_memory( &pd, data, sd_len, False);
1146 * Finally, unmarshall from the data buffer.
1149 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1150 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1152 * Return access denied for want of a better error message..
1154 talloc_destroy(mem_ctx);
1155 return NT_STATUS_NO_MEMORY;
1158 if (psd->owner_sid==0) {
1159 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1161 if (psd->group_sid==0) {
1162 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1165 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1168 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1171 status = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1173 talloc_destroy(mem_ctx);
1177 /****************************************************************************
1178 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1179 ****************************************************************************/
1181 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1183 struct ea_list *ea_list_head = NULL;
1186 if (data_size < 4) {
1190 while (offset + 4 <= data_size) {
1191 size_t next_offset = IVAL(pdata,offset);
1192 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1198 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1199 if (next_offset == 0) {
1202 offset += next_offset;
1205 return ea_list_head;
1208 /****************************************************************************
1209 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1210 ****************************************************************************/
1212 static void call_nt_transact_create(connection_struct *conn,
1213 struct smb_request *req,
1214 uint16 **ppsetup, uint32 setup_count,
1215 char **ppparams, uint32 parameter_count,
1216 char **ppdata, uint32 data_count,
1217 uint32 max_data_count)
1221 char *params = *ppparams;
1222 char *data = *ppdata;
1223 /* Breakout the oplock request bits so we can set the reply bits separately. */
1224 int oplock_request = 0;
1226 SMB_OFF_T file_len = 0;
1227 SMB_STRUCT_STAT sbuf;
1229 files_struct *fsp = NULL;
1231 BOOL extended_oplock_granted = False;
1234 uint32 file_attributes;
1235 uint32 share_access;
1236 uint32 create_disposition;
1237 uint32 create_options;
1240 uint16 root_dir_fid;
1241 struct timespec c_timespec;
1242 struct timespec a_timespec;
1243 struct timespec m_timespec;
1244 struct ea_list *ea_list = NULL;
1245 TALLOC_CTX *ctx = NULL;
1249 struct case_semantics_state *case_state = NULL;
1251 DEBUG(5,("call_nt_transact_create\n"));
1254 * If it's an IPC, use the pipe handler.
1258 if (lp_nt_pipe_support()) {
1259 do_nt_transact_create_pipe(
1261 ppsetup, setup_count,
1262 ppparams, parameter_count,
1263 ppdata, data_count);
1266 reply_doserror(req, ERRDOS, ERRnoaccess);
1272 * Ensure minimum number of parameters sent.
1275 if(parameter_count < 54) {
1276 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1277 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1281 flags = IVAL(params,0);
1282 access_mask = IVAL(params,8);
1283 file_attributes = IVAL(params,20);
1284 share_access = IVAL(params,24);
1285 create_disposition = IVAL(params,28);
1286 create_options = IVAL(params,32);
1287 sd_len = IVAL(params,36);
1288 ea_len = IVAL(params,40);
1289 root_dir_fid = (uint16)IVAL(params,4);
1291 /* Ensure the data_len is correct for the sd and ea values given. */
1292 if ((ea_len + sd_len > data_count) ||
1293 (ea_len > data_count) || (sd_len > data_count) ||
1294 (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1295 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1296 (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1297 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1302 if (!lp_ea_support(SNUM(conn))) {
1303 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1304 (unsigned int)ea_len ));
1305 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1310 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1311 (unsigned int)ea_len ));
1312 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1317 if (create_options & FILE_OPEN_BY_FILE_ID) {
1318 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1323 * Get the file name.
1326 if(root_dir_fid != 0) {
1328 * This filename is relative to a directory fid.
1330 files_struct *dir_fsp = file_fsp(SVAL(params,4));
1331 size_t dir_name_len;
1334 reply_doserror(req, ERRDOS, ERRbadfid);
1338 if(!dir_fsp->is_directory) {
1339 srvstr_get_path(params, req->flags2, fname_in,
1340 params+53, sizeof(fname_in),
1341 parameter_count-53, STR_TERMINATE,
1343 if (!NT_STATUS_IS_OK(status)) {
1344 reply_nterror(req, status);
1349 * Check to see if this is a mac fork of some kind.
1352 if( is_ntfs_stream_name(fname_in)) {
1354 NT_STATUS_OBJECT_PATH_NOT_FOUND);
1358 reply_doserror(req, ERRDOS, ERRbadfid);
1363 * Copy in the base directory name.
1366 pstrcpy( fname_in, dir_fsp->fsp_name );
1367 dir_name_len = strlen(fname_in);
1370 * Ensure it ends in a '\'.
1373 if((fname_in[dir_name_len-1] != '\\') && (fname_in[dir_name_len-1] != '/')) {
1374 pstrcat(fname_in, "/");
1380 srvstr_get_path(params, req->flags2, tmpname,
1381 params+53, sizeof(tmpname),
1382 parameter_count-53, STR_TERMINATE,
1384 if (!NT_STATUS_IS_OK(status)) {
1385 reply_nterror(req, status);
1388 pstrcat(fname_in, tmpname);
1391 srvstr_get_path(params, req->flags2, fname_in, params+53,
1392 sizeof(fname_in), parameter_count-53,
1393 STR_TERMINATE, &status);
1394 if (!NT_STATUS_IS_OK(status)) {
1395 reply_nterror(req, status);
1400 * Check to see if this is a mac fork of some kind.
1403 if( is_ntfs_stream_name(fname_in)) {
1404 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
1409 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1410 if (oplock_request) {
1411 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1415 * Ordinary file or directory.
1419 * Check if POSIX semantics are wanted.
1422 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1423 case_state = set_posix_case_semantics(NULL, conn);
1424 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1427 status = resolve_dfspath(conn, req->flags2 & FLAGS2_DFS_PATHNAMES,
1429 if (!NT_STATUS_IS_OK(status)) {
1430 TALLOC_FREE(case_state);
1431 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1432 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1433 ERRSRV, ERRbadpath);
1436 reply_nterror(req, status);
1440 status = unix_convert(conn, fname_in, False, &fname, NULL, &sbuf);
1441 if (!NT_STATUS_IS_OK(status)) {
1442 TALLOC_FREE(case_state);
1443 reply_nterror(req, status);
1446 /* All file access must go through check_name() */
1447 status = check_name(conn, fname);
1448 if (!NT_STATUS_IS_OK(status)) {
1449 TALLOC_FREE(case_state);
1450 reply_nterror(req, status);
1454 /* This is the correct thing to do (check every time) but can_delete is
1455 expensive (it may have to read the parent directory permissions). So
1456 for now we're not doing it unless we have a strong hint the client
1457 is really going to delete this file. If the client is forcing FILE_CREATE
1458 let the filesystem take care of the permissions. */
1460 /* Setting FILE_SHARE_DELETE is the hint. */
1462 if (lp_acl_check_permissions(SNUM(conn))
1463 && (create_disposition != FILE_CREATE)
1464 && (share_access & FILE_SHARE_DELETE)
1465 && (access_mask & DELETE_ACCESS)) {
1466 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
1467 !can_delete_file_in_directory(conn, fname)) {
1468 TALLOC_FREE(case_state);
1469 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1475 /* We need to support SeSecurityPrivilege for this. */
1476 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
1477 !user_has_privileges(current_user.nt_user_token,
1479 TALLOC_FREE(case_state);
1480 reply_nterror(req, NT_STATUS_PRIVILEGE_NOT_HELD);
1486 pdata = data + sd_len;
1488 /* We have already checked that ea_len <= data_count here. */
1489 ea_list = read_nttrans_ea_list(talloc_tos(), pdata,
1492 TALLOC_FREE(case_state);
1493 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1499 * If it's a request for a directory open, deal with it separately.
1502 if(create_options & FILE_DIRECTORY_FILE) {
1504 /* Can't open a temp directory. IFS kit test. */
1505 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1506 TALLOC_FREE(case_state);
1507 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1512 * We will get a create directory here if the Win32
1513 * app specified a security descriptor in the
1514 * CreateDirectory() call.
1518 status = open_directory(conn, req, fname, &sbuf,
1528 * Ordinary file case.
1531 status = open_file_ntcreate(conn,req,fname,&sbuf,
1540 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
1543 * Fail the open if it was explicitly a non-directory file.
1546 if (create_options & FILE_NON_DIRECTORY_FILE) {
1547 TALLOC_FREE(case_state);
1548 reply_force_nterror(
1550 NT_STATUS_FILE_IS_A_DIRECTORY);
1555 status = open_directory(conn, req, fname,
1566 TALLOC_FREE(case_state);
1568 if(!NT_STATUS_IS_OK(status)) {
1569 if (open_was_deferred(req->mid)) {
1570 /* We have re-scheduled this call. */
1573 reply_openerror(req, status);
1578 * According to the MS documentation, the only time the security
1579 * descriptor is applied to the opened file is iff we *created* the
1580 * file; an existing file stays the same.
1582 * Also, it seems (from observation) that you can open the file with
1583 * any access mask but you can still write the sd. We need to override
1584 * the granted access before we call set_sd
1585 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1588 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1589 uint32 saved_access_mask = fsp->access_mask;
1591 /* We have already checked that sd_len <= data_count here. */
1593 fsp->access_mask = FILE_GENERIC_ALL;
1595 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1596 if (!NT_STATUS_IS_OK(status)) {
1597 talloc_destroy(ctx);
1598 close_file(fsp,ERROR_CLOSE);
1599 TALLOC_FREE(case_state);
1600 reply_nterror(req, status);
1603 fsp->access_mask = saved_access_mask;
1606 if (ea_len && (info == FILE_WAS_CREATED)) {
1607 status = set_ea(conn, fsp, fname, ea_list);
1608 if (!NT_STATUS_IS_OK(status)) {
1609 close_file(fsp,ERROR_CLOSE);
1610 TALLOC_FREE(case_state);
1611 reply_nterror(req, status);
1616 TALLOC_FREE(case_state);
1618 file_len = sbuf.st_size;
1619 fattr = dos_mode(conn,fname,&sbuf);
1621 fattr = FILE_ATTRIBUTE_NORMAL;
1623 if (!fsp->is_directory && (fattr & aDIR)) {
1624 close_file(fsp,ERROR_CLOSE);
1625 reply_doserror(req, ERRDOS, ERRnoaccess);
1629 /* Save the requested allocation size. */
1630 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1631 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1632 #ifdef LARGE_SMB_OFF_T
1633 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1635 if (allocation_size && (allocation_size > file_len)) {
1636 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1637 if (fsp->is_directory) {
1638 close_file(fsp,ERROR_CLOSE);
1639 /* Can't set allocation size on a directory. */
1640 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1643 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1644 close_file(fsp,ERROR_CLOSE);
1645 reply_nterror(req, NT_STATUS_DISK_FULL);
1649 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1654 * If the caller set the extended oplock request bit
1655 * and we granted one (by whatever means) - set the
1656 * correct bit for extended oplock reply.
1659 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1660 extended_oplock_granted = True;
1663 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1664 extended_oplock_granted = True;
1667 /* Realloc the size of parameters and data we will return */
1668 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1669 /* Extended response is 32 more byyes. */
1674 params = nttrans_realloc(ppparams, param_len);
1675 if(params == NULL) {
1676 reply_doserror(req, ERRDOS, ERRnomem);
1681 if (extended_oplock_granted) {
1682 if (flags & REQUEST_BATCH_OPLOCK) {
1683 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1685 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1687 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1688 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1690 SCVAL(p,0,NO_OPLOCK_RETURN);
1694 SSVAL(p,0,fsp->fnum);
1696 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1697 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1704 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1705 a_timespec = get_atimespec(&sbuf);
1706 m_timespec = get_mtimespec(&sbuf);
1708 if (lp_dos_filetime_resolution(SNUM(conn))) {
1709 dos_filetime_timespec(&c_timespec);
1710 dos_filetime_timespec(&a_timespec);
1711 dos_filetime_timespec(&m_timespec);
1714 put_long_date_timespec(p, c_timespec); /* create time. */
1716 put_long_date_timespec(p, a_timespec); /* access time */
1718 put_long_date_timespec(p, m_timespec); /* write time */
1720 put_long_date_timespec(p, m_timespec); /* change time */
1722 SIVAL(p,0,fattr); /* File Attributes. */
1724 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1726 SOFF_T(p,0,file_len);
1728 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1732 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1734 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1737 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1738 perms = FILE_GENERIC_ALL;
1740 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1745 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1747 /* Send the required number of replies */
1748 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1753 /****************************************************************************
1754 Reply to a NT CANCEL request.
1755 conn POINTER CAN BE NULL HERE !
1756 ****************************************************************************/
1758 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1761 * Go through and cancel any pending change notifies.
1764 START_PROFILE(SMBntcancel);
1765 remove_pending_change_notify_requests_by_mid(req->mid);
1766 remove_pending_lock_requests_by_mid(req->mid);
1767 srv_cancel_sign_response(req->mid);
1769 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1771 END_PROFILE(SMBntcancel);
1775 /****************************************************************************
1777 ****************************************************************************/
1779 static NTSTATUS copy_internals(connection_struct *conn,
1780 struct smb_request *req,
1781 const char *oldname_in,
1782 const char *newname_in,
1785 SMB_STRUCT_STAT sbuf1, sbuf2;
1786 char *oldname = NULL;
1787 char *newname = NULL;
1788 char *last_component_oldname = NULL;
1789 char *last_component_newname = NULL;
1790 files_struct *fsp1,*fsp2;
1794 NTSTATUS status = NT_STATUS_OK;
1799 if (!CAN_WRITE(conn)) {
1800 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1803 status = unix_convert(conn, oldname_in, False, &oldname,
1804 &last_component_oldname, &sbuf1);
1805 if (!NT_STATUS_IS_OK(status)) {
1809 status = check_name(conn, oldname);
1810 if (!NT_STATUS_IS_OK(status)) {
1814 /* Source must already exist. */
1815 if (!VALID_STAT(sbuf1)) {
1816 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1818 /* Ensure attributes match. */
1819 fattr = dos_mode(conn,oldname,&sbuf1);
1820 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1821 return NT_STATUS_NO_SUCH_FILE;
1824 status = unix_convert(conn, newname_in, False, &newname,
1825 &last_component_newname, &sbuf2);
1826 if (!NT_STATUS_IS_OK(status)) {
1830 status = check_name(conn, newname);
1831 if (!NT_STATUS_IS_OK(status)) {
1835 /* Disallow if newname already exists. */
1836 if (VALID_STAT(sbuf2)) {
1837 return NT_STATUS_OBJECT_NAME_COLLISION;
1840 /* No links from a directory. */
1841 if (S_ISDIR(sbuf1.st_mode)) {
1842 return NT_STATUS_FILE_IS_A_DIRECTORY;
1845 /* Ensure this is within the share. */
1846 status = check_reduced_name(conn, oldname);
1847 if (!NT_STATUS_IS_OK(status)) {
1851 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1854 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1855 FILE_READ_DATA, /* Read-only. */
1856 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1858 0, /* No create options. */
1859 FILE_ATTRIBUTE_NORMAL,
1863 if (!NT_STATUS_IS_OK(status)) {
1867 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1868 FILE_WRITE_DATA, /* Read-only. */
1869 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1871 0, /* No create options. */
1876 if (!NT_STATUS_IS_OK(status)) {
1877 close_file(fsp1,ERROR_CLOSE);
1881 if (sbuf1.st_size) {
1882 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1886 * As we are opening fsp1 read-only we only expect
1887 * an error on close on fsp2 if we are out of space.
1888 * Thus we don't look at the error return from the
1891 close_file(fsp1,NORMAL_CLOSE);
1893 /* Ensure the modtime is set correctly on the destination file. */
1894 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1896 status = close_file(fsp2,NORMAL_CLOSE);
1898 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1899 creates the file. This isn't the correct thing to do in the copy
1901 file_set_dosmode(conn, newname, fattr, &sbuf2,
1902 parent_dirname(newname));
1904 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1905 return NT_STATUS_DISK_FULL;
1908 if (!NT_STATUS_IS_OK(status)) {
1909 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1910 nt_errstr(status), oldname, newname));
1915 /****************************************************************************
1916 Reply to a NT rename request.
1917 ****************************************************************************/
1919 void reply_ntrename(connection_struct *conn, struct smb_request *req)
1925 BOOL src_has_wcard = False;
1926 BOOL dest_has_wcard = False;
1930 START_PROFILE(SMBntrename);
1933 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1934 END_PROFILE(SMBntrename);
1938 attrs = SVAL(req->inbuf,smb_vwv0);
1939 rename_type = SVAL(req->inbuf,smb_vwv1);
1941 p = smb_buf(req->inbuf) + 1;
1942 p += srvstr_get_path_wcard((char *)req->inbuf, req->flags2, oldname, p,
1943 sizeof(oldname), 0, STR_TERMINATE, &status,
1945 if (!NT_STATUS_IS_OK(status)) {
1946 reply_nterror(req, status);
1947 END_PROFILE(SMBntrename);
1951 if( is_ntfs_stream_name(oldname)) {
1952 /* Can't rename a stream. */
1953 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1954 END_PROFILE(SMBntrename);
1958 if (ms_has_wild(oldname)) {
1959 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1960 END_PROFILE(SMBntrename);
1965 p += srvstr_get_path_wcard((char *)req->inbuf, req->flags2, newname, p,
1966 sizeof(newname), 0, STR_TERMINATE, &status,
1968 if (!NT_STATUS_IS_OK(status)) {
1969 reply_nterror(req, status);
1970 END_PROFILE(SMBntrename);
1974 status = resolve_dfspath(conn, req->flags2 & FLAGS2_DFS_PATHNAMES,
1976 if (!NT_STATUS_IS_OK(status)) {
1977 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1978 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1979 ERRSRV, ERRbadpath);
1980 END_PROFILE(SMBntrename);
1983 reply_nterror(req, status);
1984 END_PROFILE(SMBntrename);
1988 status = resolve_dfspath(conn, req->flags2 & FLAGS2_DFS_PATHNAMES,
1990 if (!NT_STATUS_IS_OK(status)) {
1991 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1992 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1993 ERRSRV, ERRbadpath);
1994 END_PROFILE(SMBntrename);
1997 reply_nterror(req, status);
1998 END_PROFILE(SMBntrename);
2002 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
2004 switch(rename_type) {
2005 case RENAME_FLAG_RENAME:
2006 status = rename_internals(conn, req, oldname, newname,
2007 attrs, False, src_has_wcard,
2010 case RENAME_FLAG_HARD_LINK:
2011 if (src_has_wcard || dest_has_wcard) {
2013 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2015 status = hardlink_internals(conn, oldname, newname);
2018 case RENAME_FLAG_COPY:
2019 if (src_has_wcard || dest_has_wcard) {
2021 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2023 status = copy_internals(conn, req, oldname,
2027 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
2028 status = NT_STATUS_INVALID_PARAMETER;
2031 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
2035 if (!NT_STATUS_IS_OK(status)) {
2036 if (open_was_deferred(req->mid)) {
2037 /* We have re-scheduled this call. */
2038 END_PROFILE(SMBntrename);
2042 reply_nterror(req, status);
2043 END_PROFILE(SMBntrename);
2047 reply_outbuf(req, 0, 0);
2049 END_PROFILE(SMBntrename);
2053 /****************************************************************************
2054 Reply to a notify change - queue the request and
2055 don't allow a directory to be opened.
2056 ****************************************************************************/
2058 static void call_nt_transact_notify_change(connection_struct *conn,
2059 struct smb_request *req,
2063 uint32 parameter_count,
2064 char **ppdata, uint32 data_count,
2065 uint32 max_data_count,
2066 uint32 max_param_count)
2068 uint16 *setup = *ppsetup;
2074 if(setup_count < 6) {
2075 reply_doserror(req, ERRDOS, ERRbadfunc);
2079 fsp = file_fsp(SVAL(setup,4));
2080 filter = IVAL(setup, 0);
2081 recursive = (SVAL(setup, 6) != 0) ? True : False;
2083 DEBUG(3,("call_nt_transact_notify_change\n"));
2086 reply_doserror(req, ERRDOS, ERRbadfid);
2091 char *filter_string;
2093 if (!(filter_string = notify_filter_string(NULL, filter))) {
2094 reply_nterror(req,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 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2110 if (fsp->notify == NULL) {
2112 status = change_notify_create(fsp, filter, recursive);
2114 if (!NT_STATUS_IS_OK(status)) {
2115 DEBUG(10, ("change_notify_create returned %s\n",
2116 nt_errstr(status)));
2117 reply_nterror(req, status);
2122 if (fsp->notify->num_changes != 0) {
2125 * We've got changes pending, respond immediately
2129 * TODO: write a torture test to check the filtering behaviour
2133 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
2136 * change_notify_reply() above has independently sent its
2143 * No changes pending, queue the request
2146 status = change_notify_add_request(req->inbuf, max_param_count, filter,
2148 if (!NT_STATUS_IS_OK(status)) {
2149 reply_nterror(req, status);
2154 /****************************************************************************
2155 Reply to an NT transact rename command.
2156 ****************************************************************************/
2158 static void call_nt_transact_rename(connection_struct *conn,
2159 struct smb_request *req,
2160 uint16 **ppsetup, uint32 setup_count,
2161 char **ppparams, uint32 parameter_count,
2162 char **ppdata, uint32 data_count,
2163 uint32 max_data_count)
2165 char *params = *ppparams;
2167 files_struct *fsp = NULL;
2168 BOOL replace_if_exists = False;
2169 BOOL dest_has_wcard = False;
2172 if(parameter_count < 5) {
2173 reply_doserror(req, ERRDOS, ERRbadfunc);
2177 fsp = file_fsp(SVAL(params, 0));
2178 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2179 if (!check_fsp(conn, req, fsp, ¤t_user)) {
2182 srvstr_get_path_wcard(params, req->flags2, new_name, params+4,
2183 sizeof(new_name), parameter_count - 4,
2184 STR_TERMINATE, &status, &dest_has_wcard);
2185 if (!NT_STATUS_IS_OK(status)) {
2186 reply_nterror(req, status);
2190 status = rename_internals(conn, req, fsp->fsp_name,
2191 new_name, 0, replace_if_exists, False, dest_has_wcard);
2193 if (!NT_STATUS_IS_OK(status)) {
2194 if (open_was_deferred(req->mid)) {
2195 /* We have re-scheduled this call. */
2198 reply_nterror(req, status);
2203 * Rename was successful.
2205 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2207 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2208 fsp->fsp_name, new_name));
2213 /******************************************************************************
2214 Fake up a completely empty SD.
2215 *******************************************************************************/
2217 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2221 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2223 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2230 /****************************************************************************
2231 Reply to query a security descriptor.
2232 ****************************************************************************/
2234 static void call_nt_transact_query_security_desc(connection_struct *conn,
2235 struct smb_request *req,
2239 uint32 parameter_count,
2242 uint32 max_data_count)
2244 char *params = *ppparams;
2245 char *data = *ppdata;
2247 SEC_DESC *psd = NULL;
2249 uint32 security_info_wanted;
2250 TALLOC_CTX *mem_ctx;
2251 files_struct *fsp = NULL;
2253 if(parameter_count < 8) {
2254 reply_doserror(req, ERRDOS, ERRbadfunc);
2258 fsp = file_fsp(SVAL(params,0));
2260 reply_doserror(req, ERRDOS, ERRbadfid);
2264 security_info_wanted = IVAL(params,4);
2266 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2267 (unsigned int)security_info_wanted ));
2269 params = nttrans_realloc(ppparams, 4);
2270 if(params == NULL) {
2271 reply_doserror(req, ERRDOS, ERRnomem);
2275 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2276 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2277 reply_doserror(req, ERRDOS, ERRnomem);
2282 * Get the permissions to return.
2285 if (!lp_nt_acl_support(SNUM(conn))) {
2286 sd_size = get_null_nt_acl(mem_ctx, &psd);
2288 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
2292 talloc_destroy(mem_ctx);
2293 reply_unixerror(req, ERRDOS, ERRnoaccess);
2297 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2299 SIVAL(params,0,(uint32)sd_size);
2301 if(max_data_count < sd_size) {
2303 send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
2304 params, 4, *ppdata, 0);
2305 talloc_destroy(mem_ctx);
2310 * Allocate the data we will point this at.
2313 data = nttrans_realloc(ppdata, sd_size);
2315 talloc_destroy(mem_ctx);
2316 reply_doserror(req, ERRDOS, ERRnomem);
2321 * Init the parse struct we will marshall into.
2324 prs_init(&pd, 0, mem_ctx, MARSHALL);
2327 * Setup the prs_struct to point at the memory we just
2331 prs_give_memory( &pd, data, (uint32)sd_size, False);
2334 * Finally, linearize into the outgoing buffer.
2337 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2338 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2339 security descriptor.\n"));
2341 * Return access denied for want of a better error message..
2343 talloc_destroy(mem_ctx);
2344 reply_unixerror(req, ERRDOS, ERRnoaccess);
2349 * Now we can delete the security descriptor.
2352 talloc_destroy(mem_ctx);
2354 send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2358 /****************************************************************************
2359 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2360 ****************************************************************************/
2362 static void call_nt_transact_set_security_desc(connection_struct *conn,
2363 struct smb_request *req,
2367 uint32 parameter_count,
2370 uint32 max_data_count)
2372 char *params= *ppparams;
2373 char *data = *ppdata;
2374 files_struct *fsp = NULL;
2375 uint32 security_info_sent = 0;
2378 if(parameter_count < 8) {
2379 reply_doserror(req, ERRDOS, ERRbadfunc);
2383 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
2384 reply_doserror(req, ERRDOS, ERRbadfid);
2388 if(!lp_nt_acl_support(SNUM(conn))) {
2392 security_info_sent = IVAL(params,4);
2394 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2395 (unsigned int)security_info_sent ));
2397 if (data_count == 0) {
2398 reply_doserror(req, ERRDOS, ERRnoaccess);
2402 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2403 reply_nterror(req, nt_status);
2409 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2413 /****************************************************************************
2415 ****************************************************************************/
2417 static void call_nt_transact_ioctl(connection_struct *conn,
2418 struct smb_request *req,
2419 uint16 **ppsetup, uint32 setup_count,
2420 char **ppparams, uint32 parameter_count,
2421 char **ppdata, uint32 data_count,
2422 uint32 max_data_count)
2429 static BOOL logged_message;
2430 char *pdata = *ppdata;
2432 if (setup_count != 8) {
2433 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2434 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2438 function = IVAL(*ppsetup, 0);
2439 fidnum = SVAL(*ppsetup, 4);
2440 isFSctl = CVAL(*ppsetup, 6);
2441 compfilter = CVAL(*ppsetup, 7);
2443 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2444 function, fidnum, isFSctl, compfilter));
2446 fsp=file_fsp(fidnum);
2447 /* this check is done in each implemented function case for now
2448 because I don't want to break anything... --metze
2449 FSP_BELONGS_CONN(fsp,conn);*/
2452 case FSCTL_SET_SPARSE:
2453 /* pretend this succeeded - tho strictly we should
2454 mark the file sparse (if the local fs supports it)
2455 so we can know if we need to pre-allocate or not */
2457 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2458 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2461 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2463 unsigned char objid[16];
2465 /* This should return the object-id on this file.
2466 * I think I'll make this be the inode+dev. JRA.
2469 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2471 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2476 pdata = nttrans_realloc(ppdata, data_count);
2477 if (pdata == NULL) {
2478 reply_nterror(req, NT_STATUS_NO_MEMORY);
2481 push_file_id_16(pdata, &fsp->file_id);
2482 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2483 push_file_id_16(pdata+32, &fsp->file_id);
2484 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2489 case FSCTL_GET_REPARSE_POINT:
2490 /* pretend this fail - my winXP does it like this
2494 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2495 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2498 case FSCTL_SET_REPARSE_POINT:
2499 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2503 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2504 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2507 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2510 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2511 * and return their volume names. If max_data_count is 16, then it is just
2512 * asking for the number of volumes and length of the combined names.
2514 * pdata is the data allocated by our caller, but that uses
2515 * total_data_count (which is 0 in our case) rather than max_data_count.
2516 * Allocate the correct amount and return the pointer to let
2517 * it be deallocated when we return.
2519 SHADOW_COPY_DATA *shadow_data = NULL;
2520 TALLOC_CTX *shadow_mem_ctx = NULL;
2521 BOOL labels = False;
2522 uint32 labels_data_count = 0;
2526 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2530 if (max_data_count < 16) {
2531 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2533 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2537 if (max_data_count > 16) {
2541 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2542 if (shadow_mem_ctx == NULL) {
2543 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2544 reply_nterror(req, NT_STATUS_NO_MEMORY);
2548 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2549 if (shadow_data == NULL) {
2550 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2551 talloc_destroy(shadow_mem_ctx);
2552 reply_nterror(req, NT_STATUS_NO_MEMORY);
2556 shadow_data->mem_ctx = shadow_mem_ctx;
2559 * Call the VFS routine to actually do the work.
2561 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2562 talloc_destroy(shadow_data->mem_ctx);
2563 if (errno == ENOSYS) {
2564 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2565 conn->connectpath));
2566 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2569 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2570 conn->connectpath));
2571 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2576 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2581 data_count = 12+labels_data_count+4;
2584 if (max_data_count<data_count) {
2585 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2586 max_data_count,data_count));
2587 talloc_destroy(shadow_data->mem_ctx);
2588 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2592 pdata = nttrans_realloc(ppdata, data_count);
2593 if (pdata == NULL) {
2594 talloc_destroy(shadow_data->mem_ctx);
2595 reply_nterror(req, NT_STATUS_NO_MEMORY);
2601 /* num_volumes 4 bytes */
2602 SIVAL(pdata,0,shadow_data->num_volumes);
2605 /* num_labels 4 bytes */
2606 SIVAL(pdata,4,shadow_data->num_volumes);
2609 /* needed_data_count 4 bytes */
2610 SIVAL(pdata,8,labels_data_count);
2614 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2615 shadow_data->num_volumes,fsp->fsp_name));
2616 if (labels && shadow_data->labels) {
2617 for (i=0;i<shadow_data->num_volumes;i++) {
2618 srvstr_push(pdata, req->flags2,
2619 cur_pdata, shadow_data->labels[i],
2620 2*sizeof(SHADOW_COPY_LABEL),
2621 STR_UNICODE|STR_TERMINATE);
2622 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2623 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2627 talloc_destroy(shadow_data->mem_ctx);
2629 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2635 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2637 /* pretend this succeeded -
2639 * we have to send back a list with all files owned by this SID
2641 * but I have to check that --metze
2645 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2647 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2649 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2653 /* unknown 4 bytes: this is not the length of the sid :-( */
2654 /*unknown = IVAL(pdata,0);*/
2656 sid_parse(pdata+4,sid_len,&sid);
2657 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2659 if (!sid_to_uid(&sid, &uid)) {
2660 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2661 sid_string_static(&sid),(unsigned long)sid_len));
2665 /* we can take a look at the find source :-)
2667 * find ./ -uid $uid -name '*' is what we need here
2670 * and send 4bytes len and then NULL terminated unicode strings
2673 * but I don't know how to deal with the paged results
2674 * (maybe we can hang the result anywhere in the fsp struct)
2676 * we don't send all files at once
2677 * and at the next we should *not* start from the beginning,
2678 * so we have to cache the result
2683 /* this works for now... */
2684 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2688 if (!logged_message) {
2689 logged_message = True; /* Only print this once... */
2690 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2695 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2699 #ifdef HAVE_SYS_QUOTAS
2700 /****************************************************************************
2701 Reply to get user quota
2702 ****************************************************************************/
2704 static void call_nt_transact_get_user_quota(connection_struct *conn,
2705 struct smb_request *req,
2709 uint32 parameter_count,
2712 uint32 max_data_count)
2714 NTSTATUS nt_status = NT_STATUS_OK;
2715 char *params = *ppparams;
2716 char *pdata = *ppdata;
2718 int data_len=0,param_len=0;
2721 files_struct *fsp = NULL;
2725 BOOL start_enum = True;
2726 SMB_NTQUOTA_STRUCT qt;
2727 SMB_NTQUOTA_LIST *tmp_list;
2728 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2733 if (current_user.ut.uid != 0) {
2734 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2735 lp_servicename(SNUM(conn)),conn->user));
2736 reply_doserror(req, ERRDOS, ERRnoaccess);
2741 * Ensure minimum number of parameters sent.
2744 if (parameter_count < 4) {
2745 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2746 reply_doserror(req, ERRDOS, ERRinvalidparam);
2750 /* maybe we can check the quota_fnum */
2751 fsp = file_fsp(SVAL(params,0));
2752 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2753 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2754 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2758 /* the NULL pointer checking for fsp->fake_file_handle->pd
2759 * is done by CHECK_NTQUOTA_HANDLE_OK()
2761 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2763 level = SVAL(params,2);
2765 /* unknown 12 bytes leading in params */
2768 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2769 /* seems that we should continue with the enum here --metze */
2771 if (qt_handle->quota_list!=NULL &&
2772 qt_handle->tmp_list==NULL) {
2775 free_ntquota_list(&(qt_handle->quota_list));
2777 /* Realloc the size of parameters and data we will return */
2779 params = nttrans_realloc(ppparams, param_len);
2780 if(params == NULL) {
2781 reply_doserror(req, ERRDOS, ERRnomem);
2786 SIVAL(params,0,data_len);
2793 case TRANSACT_GET_USER_QUOTA_LIST_START:
2795 if (qt_handle->quota_list==NULL &&
2796 qt_handle->tmp_list==NULL) {
2800 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2801 reply_doserror(req, ERRSRV, ERRerror);
2805 /* Realloc the size of parameters and data we will return */
2807 params = nttrans_realloc(ppparams, param_len);
2808 if(params == NULL) {
2809 reply_doserror(req, ERRDOS, ERRnomem);
2813 /* we should not trust the value in max_data_count*/
2814 max_data_count = MIN(max_data_count,2048);
2816 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2818 reply_doserror(req, ERRDOS, ERRnomem);
2824 /* set params Size of returned Quota Data 4 bytes*/
2825 /* but set it later when we know it */
2827 /* for each entry push the data */
2830 qt_handle->tmp_list = qt_handle->quota_list;
2833 tmp_list = qt_handle->tmp_list;
2835 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2836 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2838 sid_len = sid_size(&tmp_list->quotas->sid);
2839 entry_len = 40 + sid_len;
2841 /* nextoffset entry 4 bytes */
2842 SIVAL(entry,0,entry_len);
2844 /* then the len of the SID 4 bytes */
2845 SIVAL(entry,4,sid_len);
2847 /* unknown data 8 bytes SMB_BIG_UINT */
2848 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2850 /* the used disk space 8 bytes SMB_BIG_UINT */
2851 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2853 /* the soft quotas 8 bytes SMB_BIG_UINT */
2854 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2856 /* the hard quotas 8 bytes SMB_BIG_UINT */
2857 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2859 /* and now the SID */
2860 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2863 qt_handle->tmp_list = tmp_list;
2865 /* overwrite the offset of the last entry */
2866 SIVAL(entry-entry_len,0,0);
2868 data_len = 4+qt_len;
2869 /* overwrite the params quota_data_len */
2870 SIVAL(params,0,data_len);
2874 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2876 /* unknown 4 bytes IVAL(pdata,0) */
2878 if (data_count < 8) {
2879 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2880 reply_doserror(req, ERRDOS, ERRunknownlevel);
2884 sid_len = IVAL(pdata,4);
2885 /* Ensure this is less than 1mb. */
2886 if (sid_len > (1024*1024)) {
2887 reply_doserror(req, ERRDOS, ERRnomem);
2891 if (data_count < 8+sid_len) {
2892 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2893 reply_doserror(req, ERRDOS, ERRunknownlevel);
2897 data_len = 4+40+sid_len;
2899 if (max_data_count < data_len) {
2900 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2901 max_data_count, data_len));
2903 SIVAL(params,0,data_len);
2905 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2909 sid_parse(pdata+8,sid_len,&sid);
2911 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2914 * we have to return zero's in all fields
2915 * instead of returning an error here
2920 /* Realloc the size of parameters and data we will return */
2922 params = nttrans_realloc(ppparams, param_len);
2923 if(params == NULL) {
2924 reply_doserror(req, ERRDOS, ERRnomem);
2928 pdata = nttrans_realloc(ppdata, data_len);
2930 reply_doserror(req, ERRDOS, ERRnomem);
2936 /* set params Size of returned Quota Data 4 bytes*/
2937 SIVAL(params,0,data_len);
2939 /* nextoffset entry 4 bytes */
2942 /* then the len of the SID 4 bytes */
2943 SIVAL(entry,4,sid_len);
2945 /* unknown data 8 bytes SMB_BIG_UINT */
2946 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2948 /* the used disk space 8 bytes SMB_BIG_UINT */
2949 SBIG_UINT(entry,16,qt.usedspace);
2951 /* the soft quotas 8 bytes SMB_BIG_UINT */
2952 SBIG_UINT(entry,24,qt.softlim);
2954 /* the hard quotas 8 bytes SMB_BIG_UINT */
2955 SBIG_UINT(entry,32,qt.hardlim);
2957 /* and now the SID */
2958 sid_linearize(entry+40, sid_len, &sid);
2963 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2964 reply_doserror(req, ERRSRV, ERRerror);
2969 send_nt_replies(req, nt_status, params, param_len,
2973 /****************************************************************************
2974 Reply to set user quota
2975 ****************************************************************************/
2977 static void call_nt_transact_set_user_quota(connection_struct *conn,
2978 struct smb_request *req,
2982 uint32 parameter_count,
2985 uint32 max_data_count)
2987 char *params = *ppparams;
2988 char *pdata = *ppdata;
2989 int data_len=0,param_len=0;
2990 SMB_NTQUOTA_STRUCT qt;
2993 files_struct *fsp = NULL;
2998 if (current_user.ut.uid != 0) {
2999 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
3000 lp_servicename(SNUM(conn)),conn->user));
3001 reply_doserror(req, ERRDOS, ERRnoaccess);
3006 * Ensure minimum number of parameters sent.
3009 if (parameter_count < 2) {
3010 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
3011 reply_doserror(req, ERRDOS, ERRinvalidparam);
3015 /* maybe we can check the quota_fnum */
3016 fsp = file_fsp(SVAL(params,0));
3017 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
3018 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
3019 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
3023 if (data_count < 40) {
3024 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
3025 reply_doserror(req, ERRDOS, ERRunknownlevel);
3029 /* offset to next quota record.
3030 * 4 bytes IVAL(pdata,0)
3035 sid_len = IVAL(pdata,4);
3037 if (data_count < 40+sid_len) {
3038 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
3039 reply_doserror(req, ERRDOS, ERRunknownlevel);
3043 /* unknown 8 bytes in pdata
3044 * maybe its the change time in NTTIME
3047 /* the used space 8 bytes (SMB_BIG_UINT)*/
3048 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
3049 #ifdef LARGE_SMB_OFF_T
3050 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
3051 #else /* LARGE_SMB_OFF_T */
3052 if ((IVAL(pdata,20) != 0)&&
3053 ((qt.usedspace != 0xFFFFFFFF)||
3054 (IVAL(pdata,20)!=0xFFFFFFFF))) {
3055 /* more than 32 bits? */
3056 reply_doserror(req, ERRDOS, ERRunknownlevel);
3059 #endif /* LARGE_SMB_OFF_T */
3061 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
3062 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
3063 #ifdef LARGE_SMB_OFF_T
3064 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
3065 #else /* LARGE_SMB_OFF_T */
3066 if ((IVAL(pdata,28) != 0)&&
3067 ((qt.softlim != 0xFFFFFFFF)||
3068 (IVAL(pdata,28)!=0xFFFFFFFF))) {
3069 /* more than 32 bits? */
3070 reply_doserror(req, ERRDOS, ERRunknownlevel);
3073 #endif /* LARGE_SMB_OFF_T */
3075 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
3076 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
3077 #ifdef LARGE_SMB_OFF_T
3078 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
3079 #else /* LARGE_SMB_OFF_T */
3080 if ((IVAL(pdata,36) != 0)&&
3081 ((qt.hardlim != 0xFFFFFFFF)||
3082 (IVAL(pdata,36)!=0xFFFFFFFF))) {
3083 /* more than 32 bits? */
3084 reply_doserror(req, ERRDOS, ERRunknownlevel);
3087 #endif /* LARGE_SMB_OFF_T */
3089 sid_parse(pdata+40,sid_len,&sid);
3090 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
3092 /* 44 unknown bytes left... */
3094 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
3095 reply_doserror(req, ERRSRV, ERRerror);
3099 send_nt_replies(req, NT_STATUS_OK, params, param_len,
3102 #endif /* HAVE_SYS_QUOTAS */
3104 static void handle_nttrans(connection_struct *conn,
3105 struct trans_state *state,
3106 struct smb_request *req)
3108 if (Protocol >= PROTOCOL_NT1) {
3109 req->flags2 |= 0x40; /* IS_LONG_NAME */
3110 SSVAL(req->inbuf,smb_flg2,req->flags2);
3113 /* Now we must call the relevant NT_TRANS function */
3114 switch(state->call) {
3115 case NT_TRANSACT_CREATE:
3117 START_PROFILE(NT_transact_create);
3118 call_nt_transact_create(
3120 &state->setup, state->setup_count,
3121 &state->param, state->total_param,
3122 &state->data, state->total_data,
3123 state->max_data_return);
3124 END_PROFILE(NT_transact_create);
3128 case NT_TRANSACT_IOCTL:
3130 START_PROFILE(NT_transact_ioctl);
3131 call_nt_transact_ioctl(
3133 &state->setup, state->setup_count,
3134 &state->param, state->total_param,
3135 &state->data, state->total_data,
3136 state->max_data_return);
3137 END_PROFILE(NT_transact_ioctl);
3141 case NT_TRANSACT_SET_SECURITY_DESC:
3143 START_PROFILE(NT_transact_set_security_desc);
3144 call_nt_transact_set_security_desc(
3146 &state->setup, state->setup_count,
3147 &state->param, state->total_param,
3148 &state->data, state->total_data,
3149 state->max_data_return);
3150 END_PROFILE(NT_transact_set_security_desc);
3154 case NT_TRANSACT_NOTIFY_CHANGE:
3156 START_PROFILE(NT_transact_notify_change);
3157 call_nt_transact_notify_change(
3159 &state->setup, state->setup_count,
3160 &state->param, state->total_param,
3161 &state->data, state->total_data,
3162 state->max_data_return,
3163 state->max_param_return);
3164 END_PROFILE(NT_transact_notify_change);
3168 case NT_TRANSACT_RENAME:
3170 START_PROFILE(NT_transact_rename);
3171 call_nt_transact_rename(
3173 &state->setup, state->setup_count,
3174 &state->param, state->total_param,
3175 &state->data, state->total_data,
3176 state->max_data_return);
3177 END_PROFILE(NT_transact_rename);
3181 case NT_TRANSACT_QUERY_SECURITY_DESC:
3183 START_PROFILE(NT_transact_query_security_desc);
3184 call_nt_transact_query_security_desc(
3186 &state->setup, state->setup_count,
3187 &state->param, state->total_param,
3188 &state->data, state->total_data,
3189 state->max_data_return);
3190 END_PROFILE(NT_transact_query_security_desc);
3194 #ifdef HAVE_SYS_QUOTAS
3195 case NT_TRANSACT_GET_USER_QUOTA:
3197 START_PROFILE(NT_transact_get_user_quota);
3198 call_nt_transact_get_user_quota(
3200 &state->setup, state->setup_count,
3201 &state->param, state->total_param,
3202 &state->data, state->total_data,
3203 state->max_data_return);
3204 END_PROFILE(NT_transact_get_user_quota);
3208 case NT_TRANSACT_SET_USER_QUOTA:
3210 START_PROFILE(NT_transact_set_user_quota);
3211 call_nt_transact_set_user_quota(
3213 &state->setup, state->setup_count,
3214 &state->param, state->total_param,
3215 &state->data, state->total_data,
3216 state->max_data_return);
3217 END_PROFILE(NT_transact_set_user_quota);
3220 #endif /* HAVE_SYS_QUOTAS */
3223 /* Error in request */
3224 DEBUG(0,("handle_nttrans: Unknown request %d in "
3225 "nttrans call\n", state->call));
3226 reply_doserror(req, ERRSRV, ERRerror);
3232 /****************************************************************************
3233 Reply to a SMBNTtrans.
3234 ****************************************************************************/
3236 void reply_nttrans(connection_struct *conn, struct smb_request *req)
3242 uint16 function_code;
3244 struct trans_state *state;
3247 START_PROFILE(SMBnttrans);
3249 if (req->wct < 19) {
3250 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3251 END_PROFILE(SMBnttrans);
3255 size = smb_len(req->inbuf) + 4;
3256 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
3257 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
3258 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
3259 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
3260 function_code = SVAL(req->inbuf, smb_nt_Function);
3262 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3263 reply_doserror(req, ERRSRV, ERRaccess);
3264 END_PROFILE(SMBnttrans);
3268 result = allow_new_trans(conn->pending_trans, req->mid);
3269 if (!NT_STATUS_IS_OK(result)) {
3270 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3271 reply_nterror(req, result);
3272 END_PROFILE(SMBnttrans);
3276 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3277 reply_doserror(req, ERRSRV, ERRaccess);
3278 END_PROFILE(SMBnttrans);
3282 state->cmd = SMBnttrans;
3284 state->mid = req->mid;
3285 state->vuid = req->vuid;
3286 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
3288 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
3289 state->param = NULL;
3290 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
3291 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
3293 /* setup count is in *words* */
3294 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
3295 state->setup = NULL;
3296 state->call = function_code;
3299 * All nttrans messages we handle have smb_wct == 19 +
3300 * state->setup_count. Ensure this is so as a sanity check.
3303 if(req->wct != 19 + (state->setup_count/2)) {
3304 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3305 req->wct, 19 + (state->setup_count/2)));
3309 /* Don't allow more than 128mb for each value. */
3310 if ((state->total_data > (1024*1024*128)) ||
3311 (state->total_param > (1024*1024*128))) {
3312 reply_doserror(req, ERRDOS, ERRnomem);
3313 END_PROFILE(SMBnttrans);
3317 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3320 if (state->total_data) {
3321 /* Can't use talloc here, the core routines do realloc on the
3322 * params and data. */
3323 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3324 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3325 "bytes !\n", (unsigned int)state->total_data));
3327 reply_doserror(req, ERRDOS, ERRnomem);
3328 END_PROFILE(SMBnttrans);
3331 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3333 if ((smb_base(req->inbuf)+dsoff+dscnt
3334 > (char *)req->inbuf + size) ||
3335 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
3338 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3341 if (state->total_param) {
3342 /* Can't use talloc here, the core routines do realloc on the
3343 * params and data. */
3344 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3345 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3346 "bytes !\n", (unsigned int)state->total_param));
3347 SAFE_FREE(state->data);
3349 reply_doserror(req, ERRDOS, ERRnomem);
3350 END_PROFILE(SMBnttrans);
3353 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3355 if ((smb_base(req->inbuf)+psoff+pscnt
3356 > (char *)req->inbuf + size) ||
3357 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
3360 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3363 state->received_data = dscnt;
3364 state->received_param = pscnt;
3366 if(state->setup_count > 0) {
3367 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3368 state->setup_count));
3369 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3370 if (state->setup == NULL) {
3371 DEBUG(0,("reply_nttrans : Out of memory\n"));
3372 SAFE_FREE(state->data);
3373 SAFE_FREE(state->param);
3375 reply_doserror(req, ERRDOS, ERRnomem);
3376 END_PROFILE(SMBnttrans);
3380 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3381 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3384 if (smb_nt_SetupStart + state->setup_count > size) {
3388 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
3389 state->setup_count);
3390 dump_data(10, (uint8 *)state->setup, state->setup_count);
3393 if ((state->received_data == state->total_data) &&
3394 (state->received_param == state->total_param)) {
3395 handle_nttrans(conn, state, req);
3396 SAFE_FREE(state->param);
3397 SAFE_FREE(state->data);
3399 END_PROFILE(SMBnttrans);
3403 DLIST_ADD(conn->pending_trans, state);
3405 /* We need to send an interim response then receive the rest
3406 of the parameter/data bytes */
3407 reply_outbuf(req, 0, 0);
3408 show_msg((char *)req->outbuf);
3409 END_PROFILE(SMBnttrans);
3414 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3415 SAFE_FREE(state->data);
3416 SAFE_FREE(state->param);
3418 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3419 END_PROFILE(SMBnttrans);
3423 /****************************************************************************
3424 Reply to a SMBnttranss
3425 ****************************************************************************/
3427 void reply_nttranss(connection_struct *conn, struct smb_request *req)
3429 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3430 struct trans_state *state;
3434 START_PROFILE(SMBnttranss);
3436 show_msg((char *)req->inbuf);
3438 if (req->wct < 18) {
3439 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3440 END_PROFILE(SMBnttranss);
3444 for (state = conn->pending_trans; state != NULL;
3445 state = state->next) {
3446 if (state->mid == req->mid) {
3451 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3452 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3453 END_PROFILE(SMBnttranss);
3457 /* Revise state->total_param and state->total_data in case they have
3458 changed downwards */
3459 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
3460 < state->total_param) {
3461 state->total_param = IVAL(req->inbuf,
3462 smb_nts_TotalParameterCount);
3464 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
3465 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
3468 size = smb_len(req->inbuf) + 4;
3470 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
3471 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
3472 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
3474 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
3475 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
3476 doff = IVAL(req->inbuf, smb_nts_DataOffset);
3478 state->received_param += pcnt;
3479 state->received_data += dcnt;
3481 if ((state->received_data > state->total_data) ||
3482 (state->received_param > state->total_param))
3486 if (pdisp+pcnt > state->total_param)
3488 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3490 if (pdisp > state->total_param)
3492 if ((smb_base(req->inbuf) + poff + pcnt
3493 > (char *)req->inbuf + size) ||
3494 (smb_base(req->inbuf) + poff + pcnt
3495 < smb_base(req->inbuf)))
3497 if (state->param + pdisp < state->param)
3500 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
3505 if (ddisp+dcnt > state->total_data)
3507 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3509 if (ddisp > state->total_data)
3511 if ((smb_base(req->inbuf) + doff + dcnt
3512 > (char *)req->inbuf + size) ||
3513 (smb_base(req->inbuf) + doff + dcnt
3514 < smb_base(req->inbuf)))
3516 if (state->data + ddisp < state->data)
3519 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
3523 if ((state->received_param < state->total_param) ||
3524 (state->received_data < state->total_data)) {
3525 END_PROFILE(SMBnttranss);
3530 * construct_reply_common will copy smb_com from inbuf to
3531 * outbuf. SMBnttranss is wrong here.
3533 SCVAL(req->inbuf,smb_com,SMBnttrans);
3535 handle_nttrans(conn, state, req);
3537 DLIST_REMOVE(conn->pending_trans, state);
3538 SAFE_FREE(state->data);
3539 SAFE_FREE(state->param);
3541 END_PROFILE(SMBnttranss);
3546 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3547 DLIST_REMOVE(conn->pending_trans, state);
3548 SAFE_FREE(state->data);
3549 SAFE_FREE(state->param);
3551 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3552 END_PROFILE(SMBnttranss);