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));
447 chain_reply_new(req);
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));
482 chain_reply_new(req);
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)
495 uint32 file_attributes;
497 uint32 create_disposition;
498 uint32 create_options;
500 SMB_BIG_UINT allocation_size;
501 /* Breakout the oplock request bits so we can set the
502 reply bits separately. */
503 int oplock_request = 0;
505 SMB_OFF_T file_len = 0;
506 SMB_STRUCT_STAT sbuf;
508 files_struct *fsp = NULL;
510 struct timespec c_timespec;
511 struct timespec a_timespec;
512 struct timespec m_timespec;
513 BOOL extended_oplock_granted = False;
515 struct case_semantics_state *case_state = NULL;
517 START_PROFILE(SMBntcreateX);
520 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
524 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
525 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
526 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
527 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
528 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
529 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
530 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
532 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,smb_ntcreate_AllocationSize);
533 #ifdef LARGE_SMB_OFF_T
534 allocation_size |= (((SMB_BIG_UINT)IVAL(req->inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
537 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
538 "file_attributes = 0x%x, share_access = 0x%x, "
539 "create_disposition = 0x%x create_options = 0x%x "
540 "root_dir_fid = 0x%x\n",
542 (unsigned int)access_mask,
543 (unsigned int)file_attributes,
544 (unsigned int)share_access,
545 (unsigned int)create_disposition,
546 (unsigned int)create_options,
547 (unsigned int)root_dir_fid ));
550 * If it's an IPC, use the pipe handler.
554 if (lp_nt_pipe_support()) {
555 do_ntcreate_pipe_open(conn, req);
556 END_PROFILE(SMBntcreateX);
559 reply_doserror(req, ERRDOS, ERRnoaccess);
560 END_PROFILE(SMBntcreateX);
565 if (create_options & FILE_OPEN_BY_FILE_ID) {
566 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
567 END_PROFILE(SMBntcreateX);
575 if(root_dir_fid != 0) {
577 * This filename is relative to a directory fid.
580 files_struct *dir_fsp = file_fsp(
581 SVAL(req->inbuf, smb_ntcreate_RootDirectoryFid));
585 reply_doserror(req, ERRDOS, ERRbadfid);
586 END_PROFILE(SMBntcreateX);
590 if(!dir_fsp->is_directory) {
592 srvstr_get_path((char *)req->inbuf, req->flags2, fname,
593 smb_buf(req->inbuf), sizeof(fname), 0,
594 STR_TERMINATE, &status);
595 if (!NT_STATUS_IS_OK(status)) {
596 reply_nterror(req, status);
597 END_PROFILE(SMBntcreateX);
602 * Check to see if this is a mac fork of some kind.
605 if( is_ntfs_stream_name(fname)) {
607 req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
608 END_PROFILE(SMBntcreateX);
613 we need to handle the case when we get a
614 relative open relative to a file and the
615 pathname is blank - this is a reopen!
616 (hint from demyn plantenberg)
619 reply_doserror(req, ERRDOS, ERRbadfid);
620 END_PROFILE(SMBntcreateX);
625 * Copy in the base directory name.
628 pstrcpy( fname, dir_fsp->fsp_name );
629 dir_name_len = strlen(fname);
632 * Ensure it ends in a '\'.
635 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
640 srvstr_get_path((char *)req->inbuf, req->flags2, rel_fname,
641 smb_buf(req->inbuf), sizeof(rel_fname), 0,
642 STR_TERMINATE, &status);
643 if (!NT_STATUS_IS_OK(status)) {
644 reply_nterror(req, status);
645 END_PROFILE(SMBntcreateX);
648 pstrcat(fname, rel_fname);
650 srvstr_get_path((char *)req->inbuf, req->flags2, fname,
651 smb_buf(req->inbuf), sizeof(fname), 0,
652 STR_TERMINATE, &status);
653 if (!NT_STATUS_IS_OK(status)) {
654 reply_nterror(req, status);
655 END_PROFILE(SMBntcreateX);
660 * Check to see if this is a mac fork of some kind.
663 if( is_ntfs_stream_name(fname)) {
664 enum FAKE_FILE_TYPE fake_file_type = is_fake_file(fname);
665 if (fake_file_type!=FAKE_FILE_TYPE_NONE) {
667 * Here we go! support for changing the disk quotas --metze
669 * We need to fake up to open this MAGIC QUOTA file
670 * and return a valid FID.
672 * w2k close this file directly after openening
673 * xp also tries a QUERY_FILE_INFO on the file and then close it
675 reply_ntcreate_and_X_quota(conn, req,
676 fake_file_type, fname);
678 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
680 END_PROFILE(SMBntcreateX);
686 * Now contruct the smb_open_mode value from the filename,
687 * desired access and the share access.
689 status = resolve_dfspath(conn, req->flags2 & FLAGS2_DFS_PATHNAMES, fname);
690 if (!NT_STATUS_IS_OK(status)) {
691 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
692 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
696 reply_nterror(req, status);
698 END_PROFILE(SMBntcreateX);
702 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
703 if (oplock_request) {
704 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
708 * Ordinary file or directory.
712 * Check if POSIX semantics are wanted.
715 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
716 case_state = set_posix_case_semantics(NULL, conn);
717 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
720 status = unix_convert(conn, fname, False, NULL, &sbuf);
721 if (!NT_STATUS_IS_OK(status)) {
722 TALLOC_FREE(case_state);
723 reply_nterror(req, status);
724 END_PROFILE(SMBntcreateX);
727 /* All file access must go through check_name() */
728 status = check_name(conn, fname);
729 if (!NT_STATUS_IS_OK(status)) {
730 TALLOC_FREE(case_state);
731 reply_nterror(req, status);
732 END_PROFILE(SMBntcreateX);
736 /* This is the correct thing to do (check every time) but can_delete is
737 expensive (it may have to read the parent directory permissions). So
738 for now we're not doing it unless we have a strong hint the client
739 is really going to delete this file. If the client is forcing FILE_CREATE
740 let the filesystem take care of the permissions. */
742 /* Setting FILE_SHARE_DELETE is the hint. */
744 if (lp_acl_check_permissions(SNUM(conn))
745 && (create_disposition != FILE_CREATE)
746 && (share_access & FILE_SHARE_DELETE)
747 && (access_mask & DELETE_ACCESS)) {
748 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
749 !can_delete_file_in_directory(conn, fname)) {
750 TALLOC_FREE(case_state);
751 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
752 END_PROFILE(SMBntcreateX);
758 /* We need to support SeSecurityPrivilege for this. */
759 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
760 !user_has_privileges(current_user.nt_user_token,
762 TALLOC_FREE(case_state);
763 END_PROFILE(SMBntcreateX);
764 return ERROR_NT(NT_STATUS_PRIVILEGE_NOT_HELD);
769 * If it's a request for a directory open, deal with it separately.
772 if(create_options & FILE_DIRECTORY_FILE) {
774 /* Can't open a temp directory. IFS kit test. */
775 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
776 TALLOC_FREE(case_state);
777 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
778 END_PROFILE(SMBntcreateX);
783 status = open_directory(conn, req, fname, &sbuf,
794 * Ordinary file case.
797 /* NB. We have a potential bug here. If we
798 * cause an oplock break to ourselves, then we
799 * could end up processing filename related
800 * SMB requests whilst we await the oplock
801 * break response. As we may have changed the
802 * filename case semantics to be POSIX-like,
803 * this could mean a filename request could
804 * fail when it should succeed. This is a rare
805 * condition, but eventually we must arrange
806 * to restore the correct case semantics
807 * before issuing an oplock break request to
808 * our client. JRA. */
810 status = open_file_ntcreate(conn, req, fname, &sbuf,
819 /* We cheat here. There are two cases we
820 * care about. One is a directory rename,
821 * where the NT client will attempt to
822 * open the source directory for
823 * DELETE access. Note that when the
824 * NT client does this it does *not*
825 * set the directory bit in the
826 * request packet. This is translated
827 * into a read/write open
828 * request. POSIX states that any open
829 * for write request on a directory
830 * will generate an EISDIR error, so
831 * we can catch this here and open a
832 * pseudo handle that is flagged as a
833 * directory. The second is an open
834 * for a permissions read only, which
835 * we handle in the open_file_stat case. JRA.
838 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
841 * Fail the open if it was explicitly a non-directory
845 if (create_options & FILE_NON_DIRECTORY_FILE) {
846 TALLOC_FREE(case_state);
847 reply_force_nterror(req,
848 NT_STATUS_FILE_IS_A_DIRECTORY);
849 END_PROFILE(SMBntcreateX);
854 status = open_directory(conn, req, fname,
865 TALLOC_FREE(case_state);
867 if (!NT_STATUS_IS_OK(status)) {
868 if (open_was_deferred(req->mid)) {
869 /* We have re-scheduled this call. */
870 END_PROFILE(SMBntcreateX);
873 reply_openerror(req, status);
874 END_PROFILE(SMBntcreateX);
878 file_len = sbuf.st_size;
879 fattr = dos_mode(conn,fname,&sbuf);
881 fattr = FILE_ATTRIBUTE_NORMAL;
883 if (!fsp->is_directory && (fattr & aDIR)) {
884 close_file(fsp,ERROR_CLOSE);
885 reply_doserror(req, ERRDOS, ERRnoaccess);
886 END_PROFILE(SMBntcreateX);
890 /* Save the requested allocation size. */
891 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
892 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
893 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
894 if (fsp->is_directory) {
895 close_file(fsp,ERROR_CLOSE);
896 /* Can't set allocation size on a directory. */
897 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
898 END_PROFILE(SMBntcreateX);
901 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
902 close_file(fsp,ERROR_CLOSE);
903 reply_nterror(req, NT_STATUS_DISK_FULL);
904 END_PROFILE(SMBntcreateX);
908 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
913 * If the caller set the extended oplock request bit
914 * and we granted one (by whatever means) - set the
915 * correct bit for extended oplock reply.
918 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
919 extended_oplock_granted = True;
922 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
923 extended_oplock_granted = True;
926 if (flags & EXTENDED_RESPONSE_REQUIRED) {
927 /* This is very strange. We
928 * return 50 words, but only set
929 * the wcnt to 42 ? It's definately
930 * what happens on the wire....
932 reply_outbuf(req, 50, 0);
933 SCVAL(req->outbuf,smb_wct,42);
935 reply_outbuf(req, 34, 0);
938 p = (char *)req->outbuf + smb_vwv2;
941 * Currently as we don't support level II oplocks we just report
942 * exclusive & batch here.
945 if (extended_oplock_granted) {
946 if (flags & REQUEST_BATCH_OPLOCK) {
947 SCVAL(p,0, BATCH_OPLOCK_RETURN);
949 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
951 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
952 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
954 SCVAL(p,0,NO_OPLOCK_RETURN);
958 SSVAL(p,0,fsp->fnum);
960 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
961 SIVAL(p,0,FILE_WAS_SUPERSEDED);
968 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
969 a_timespec = get_atimespec(&sbuf);
970 m_timespec = get_mtimespec(&sbuf);
972 if (lp_dos_filetime_resolution(SNUM(conn))) {
973 dos_filetime_timespec(&c_timespec);
974 dos_filetime_timespec(&a_timespec);
975 dos_filetime_timespec(&m_timespec);
978 put_long_date_timespec(p, c_timespec); /* create time. */
980 put_long_date_timespec(p, a_timespec); /* access time */
982 put_long_date_timespec(p, m_timespec); /* write time */
984 put_long_date_timespec(p, m_timespec); /* change time */
986 SIVAL(p,0,fattr); /* File Attributes. */
988 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
990 SOFF_T(p,0,file_len);
992 if (flags & EXTENDED_RESPONSE_REQUIRED) {
996 SCVAL(p,0,fsp->is_directory ? 1 : 0);
998 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1001 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1002 perms = FILE_GENERIC_ALL;
1004 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1009 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
1011 chain_reply_new(req);
1012 END_PROFILE(SMBntcreateX);
1016 /****************************************************************************
1017 Reply to a NT_TRANSACT_CREATE call to open a pipe.
1018 ****************************************************************************/
1020 static void do_nt_transact_create_pipe(connection_struct *conn,
1021 struct smb_request *req,
1022 uint16 **ppsetup, uint32 setup_count,
1023 char **ppparams, uint32 parameter_count,
1024 char **ppdata, uint32 data_count)
1027 char *params = *ppparams;
1035 * Ensure minimum number of parameters sent.
1038 if(parameter_count < 54) {
1039 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1040 reply_doserror(req, ERRDOS, ERRnoaccess);
1044 flags = IVAL(params,0);
1046 srvstr_get_path(params, req->flags2, fname, params+53,
1047 sizeof(fname), parameter_count-53, STR_TERMINATE,
1049 if (!NT_STATUS_IS_OK(status)) {
1050 reply_nterror(req, status);
1054 nt_open_pipe(fname, conn, req, &pnum);
1061 /* Realloc the size of parameters and data we will return */
1062 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1063 /* Extended response is 32 more byyes. */
1068 params = nttrans_realloc(ppparams, param_len);
1069 if(params == NULL) {
1070 reply_doserror(req, ERRDOS, ERRnomem);
1075 SCVAL(p,0,NO_OPLOCK_RETURN);
1080 SIVAL(p,0,FILE_WAS_OPENED);
1084 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1087 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1089 SSVAL(p,2, 0x5FF); /* ? */
1092 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1094 SIVAL(p,0,FILE_GENERIC_ALL);
1096 * For pipes W2K3 seems to return
1098 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
1100 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
1103 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1105 /* Send the required number of replies */
1106 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1111 /****************************************************************************
1112 Internal fn to set security descriptors.
1113 ****************************************************************************/
1115 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1118 SEC_DESC *psd = NULL;
1119 TALLOC_CTX *mem_ctx;
1122 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
1123 return NT_STATUS_OK;
1127 * Init the parse struct we will unmarshall from.
1130 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1131 DEBUG(0,("set_sd: talloc_init failed.\n"));
1132 return NT_STATUS_NO_MEMORY;
1135 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1138 * Setup the prs_struct to point at the memory we just
1142 prs_give_memory( &pd, data, sd_len, False);
1145 * Finally, unmarshall from the data buffer.
1148 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1149 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1151 * Return access denied for want of a better error message..
1153 talloc_destroy(mem_ctx);
1154 return NT_STATUS_NO_MEMORY;
1157 if (psd->owner_sid==0) {
1158 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1160 if (psd->group_sid==0) {
1161 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1164 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1167 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1170 status = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1172 talloc_destroy(mem_ctx);
1176 /****************************************************************************
1177 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1178 ****************************************************************************/
1180 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1182 struct ea_list *ea_list_head = NULL;
1185 if (data_size < 4) {
1189 while (offset + 4 <= data_size) {
1190 size_t next_offset = IVAL(pdata,offset);
1191 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1197 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1198 if (next_offset == 0) {
1201 offset += next_offset;
1204 return ea_list_head;
1207 /****************************************************************************
1208 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1209 ****************************************************************************/
1211 static void call_nt_transact_create(connection_struct *conn,
1212 struct smb_request *req,
1213 uint16 **ppsetup, uint32 setup_count,
1214 char **ppparams, uint32 parameter_count,
1215 char **ppdata, uint32 data_count,
1216 uint32 max_data_count)
1219 char *params = *ppparams;
1220 char *data = *ppdata;
1221 /* Breakout the oplock request bits so we can set the reply bits separately. */
1222 int oplock_request = 0;
1224 SMB_OFF_T file_len = 0;
1225 SMB_STRUCT_STAT sbuf;
1227 files_struct *fsp = NULL;
1229 BOOL extended_oplock_granted = False;
1232 uint32 file_attributes;
1233 uint32 share_access;
1234 uint32 create_disposition;
1235 uint32 create_options;
1238 uint16 root_dir_fid;
1239 struct timespec c_timespec;
1240 struct timespec a_timespec;
1241 struct timespec m_timespec;
1242 struct ea_list *ea_list = NULL;
1243 TALLOC_CTX *ctx = NULL;
1247 struct case_semantics_state *case_state = NULL;
1249 DEBUG(5,("call_nt_transact_create\n"));
1252 * If it's an IPC, use the pipe handler.
1256 if (lp_nt_pipe_support()) {
1257 do_nt_transact_create_pipe(
1259 ppsetup, setup_count,
1260 ppparams, parameter_count,
1261 ppdata, data_count);
1264 reply_doserror(req, ERRDOS, ERRnoaccess);
1270 * Ensure minimum number of parameters sent.
1273 if(parameter_count < 54) {
1274 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1275 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1279 flags = IVAL(params,0);
1280 access_mask = IVAL(params,8);
1281 file_attributes = IVAL(params,20);
1282 share_access = IVAL(params,24);
1283 create_disposition = IVAL(params,28);
1284 create_options = IVAL(params,32);
1285 sd_len = IVAL(params,36);
1286 ea_len = IVAL(params,40);
1287 root_dir_fid = (uint16)IVAL(params,4);
1289 /* Ensure the data_len is correct for the sd and ea values given. */
1290 if ((ea_len + sd_len > data_count) ||
1291 (ea_len > data_count) || (sd_len > data_count) ||
1292 (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1293 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1294 (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1295 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1300 if (!lp_ea_support(SNUM(conn))) {
1301 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1302 (unsigned int)ea_len ));
1303 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1308 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1309 (unsigned int)ea_len ));
1310 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1315 if (create_options & FILE_OPEN_BY_FILE_ID) {
1316 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1321 * Get the file name.
1324 if(root_dir_fid != 0) {
1326 * This filename is relative to a directory fid.
1328 files_struct *dir_fsp = file_fsp(SVAL(params,4));
1329 size_t dir_name_len;
1332 reply_doserror(req, ERRDOS, ERRbadfid);
1336 if(!dir_fsp->is_directory) {
1337 srvstr_get_path(params, req->flags2, fname,
1338 params+53, sizeof(fname),
1339 parameter_count-53, STR_TERMINATE,
1341 if (!NT_STATUS_IS_OK(status)) {
1342 reply_nterror(req, status);
1347 * Check to see if this is a mac fork of some kind.
1350 if( is_ntfs_stream_name(fname)) {
1352 NT_STATUS_OBJECT_PATH_NOT_FOUND);
1356 reply_doserror(req, ERRDOS, ERRbadfid);
1361 * Copy in the base directory name.
1364 pstrcpy( fname, dir_fsp->fsp_name );
1365 dir_name_len = strlen(fname);
1368 * Ensure it ends in a '\'.
1371 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1372 pstrcat(fname, "/");
1378 srvstr_get_path(params, req->flags2, tmpname,
1379 params+53, sizeof(tmpname),
1380 parameter_count-53, STR_TERMINATE,
1382 if (!NT_STATUS_IS_OK(status)) {
1383 reply_nterror(req, status);
1386 pstrcat(fname, tmpname);
1389 srvstr_get_path(params, req->flags2, fname, params+53,
1390 sizeof(fname), parameter_count-53,
1391 STR_TERMINATE, &status);
1392 if (!NT_STATUS_IS_OK(status)) {
1393 reply_nterror(req, status);
1398 * Check to see if this is a mac fork of some kind.
1401 if( is_ntfs_stream_name(fname)) {
1402 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
1407 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1408 if (oplock_request) {
1409 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1413 * Ordinary file or directory.
1417 * Check if POSIX semantics are wanted.
1420 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1421 case_state = set_posix_case_semantics(NULL, conn);
1422 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1425 status = resolve_dfspath(conn, req->flags2 & FLAGS2_DFS_PATHNAMES,
1427 if (!NT_STATUS_IS_OK(status)) {
1428 TALLOC_FREE(case_state);
1429 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1430 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1431 ERRSRV, ERRbadpath);
1434 reply_nterror(req, status);
1438 status = unix_convert(conn, fname, False, NULL, &sbuf);
1439 if (!NT_STATUS_IS_OK(status)) {
1440 TALLOC_FREE(case_state);
1441 reply_nterror(req, status);
1444 /* All file access must go through check_name() */
1445 status = check_name(conn, fname);
1446 if (!NT_STATUS_IS_OK(status)) {
1447 TALLOC_FREE(case_state);
1448 reply_nterror(req, status);
1452 /* This is the correct thing to do (check every time) but can_delete is
1453 expensive (it may have to read the parent directory permissions). So
1454 for now we're not doing it unless we have a strong hint the client
1455 is really going to delete this file. If the client is forcing FILE_CREATE
1456 let the filesystem take care of the permissions. */
1458 /* Setting FILE_SHARE_DELETE is the hint. */
1460 if (lp_acl_check_permissions(SNUM(conn))
1461 && (create_disposition != FILE_CREATE)
1462 && (share_access & FILE_SHARE_DELETE)
1463 && (access_mask & DELETE_ACCESS)) {
1464 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
1465 !can_delete_file_in_directory(conn, fname)) {
1466 TALLOC_FREE(case_state);
1467 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1473 /* We need to support SeSecurityPrivilege for this. */
1474 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
1475 !user_has_privileges(current_user.nt_user_token,
1477 TALLOC_FREE(case_state);
1478 reply_nterror(req, NT_STATUS_PRIVILEGE_NOT_HELD);
1484 pdata = data + sd_len;
1486 /* We have already checked that ea_len <= data_count here. */
1487 ea_list = read_nttrans_ea_list(tmp_talloc_ctx(), pdata,
1490 TALLOC_FREE(case_state);
1491 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1497 * If it's a request for a directory open, deal with it separately.
1500 if(create_options & FILE_DIRECTORY_FILE) {
1502 /* Can't open a temp directory. IFS kit test. */
1503 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1504 TALLOC_FREE(case_state);
1505 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1510 * We will get a create directory here if the Win32
1511 * app specified a security descriptor in the
1512 * CreateDirectory() call.
1516 status = open_directory(conn, req, fname, &sbuf,
1526 * Ordinary file case.
1529 status = open_file_ntcreate(conn,req,fname,&sbuf,
1538 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
1541 * Fail the open if it was explicitly a non-directory file.
1544 if (create_options & FILE_NON_DIRECTORY_FILE) {
1545 TALLOC_FREE(case_state);
1546 reply_force_nterror(
1548 NT_STATUS_FILE_IS_A_DIRECTORY);
1553 status = open_directory(conn, req, fname,
1564 TALLOC_FREE(case_state);
1566 if(!NT_STATUS_IS_OK(status)) {
1567 if (open_was_deferred(req->mid)) {
1568 /* We have re-scheduled this call. */
1571 reply_openerror(req, status);
1576 * According to the MS documentation, the only time the security
1577 * descriptor is applied to the opened file is iff we *created* the
1578 * file; an existing file stays the same.
1580 * Also, it seems (from observation) that you can open the file with
1581 * any access mask but you can still write the sd. We need to override
1582 * the granted access before we call set_sd
1583 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1586 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1587 uint32 saved_access_mask = fsp->access_mask;
1589 /* We have already checked that sd_len <= data_count here. */
1591 fsp->access_mask = FILE_GENERIC_ALL;
1593 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1594 if (!NT_STATUS_IS_OK(status)) {
1595 talloc_destroy(ctx);
1596 close_file(fsp,ERROR_CLOSE);
1597 TALLOC_FREE(case_state);
1598 reply_nterror(req, status);
1601 fsp->access_mask = saved_access_mask;
1604 if (ea_len && (info == FILE_WAS_CREATED)) {
1605 status = set_ea(conn, fsp, fname, ea_list);
1606 if (!NT_STATUS_IS_OK(status)) {
1607 close_file(fsp,ERROR_CLOSE);
1608 TALLOC_FREE(case_state);
1609 reply_nterror(req, status);
1614 TALLOC_FREE(case_state);
1616 file_len = sbuf.st_size;
1617 fattr = dos_mode(conn,fname,&sbuf);
1619 fattr = FILE_ATTRIBUTE_NORMAL;
1621 if (!fsp->is_directory && (fattr & aDIR)) {
1622 close_file(fsp,ERROR_CLOSE);
1623 reply_doserror(req, ERRDOS, ERRnoaccess);
1627 /* Save the requested allocation size. */
1628 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1629 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1630 #ifdef LARGE_SMB_OFF_T
1631 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1633 if (allocation_size && (allocation_size > file_len)) {
1634 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1635 if (fsp->is_directory) {
1636 close_file(fsp,ERROR_CLOSE);
1637 /* Can't set allocation size on a directory. */
1638 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1641 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1642 close_file(fsp,ERROR_CLOSE);
1643 reply_nterror(req, NT_STATUS_DISK_FULL);
1647 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1652 * If the caller set the extended oplock request bit
1653 * and we granted one (by whatever means) - set the
1654 * correct bit for extended oplock reply.
1657 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1658 extended_oplock_granted = True;
1661 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1662 extended_oplock_granted = True;
1665 /* Realloc the size of parameters and data we will return */
1666 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1667 /* Extended response is 32 more byyes. */
1672 params = nttrans_realloc(ppparams, param_len);
1673 if(params == NULL) {
1674 reply_doserror(req, ERRDOS, ERRnomem);
1679 if (extended_oplock_granted) {
1680 if (flags & REQUEST_BATCH_OPLOCK) {
1681 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1683 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1685 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1686 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1688 SCVAL(p,0,NO_OPLOCK_RETURN);
1692 SSVAL(p,0,fsp->fnum);
1694 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1695 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1702 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1703 a_timespec = get_atimespec(&sbuf);
1704 m_timespec = get_mtimespec(&sbuf);
1706 if (lp_dos_filetime_resolution(SNUM(conn))) {
1707 dos_filetime_timespec(&c_timespec);
1708 dos_filetime_timespec(&a_timespec);
1709 dos_filetime_timespec(&m_timespec);
1712 put_long_date_timespec(p, c_timespec); /* create time. */
1714 put_long_date_timespec(p, a_timespec); /* access time */
1716 put_long_date_timespec(p, m_timespec); /* write time */
1718 put_long_date_timespec(p, m_timespec); /* change time */
1720 SIVAL(p,0,fattr); /* File Attributes. */
1722 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1724 SOFF_T(p,0,file_len);
1726 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1730 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1732 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1735 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1736 perms = FILE_GENERIC_ALL;
1738 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1743 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1745 /* Send the required number of replies */
1746 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1751 /****************************************************************************
1752 Reply to a NT CANCEL request.
1753 conn POINTER CAN BE NULL HERE !
1754 ****************************************************************************/
1756 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1759 * Go through and cancel any pending change notifies.
1762 START_PROFILE(SMBntcancel);
1763 remove_pending_change_notify_requests_by_mid(req->mid);
1764 remove_pending_lock_requests_by_mid(req->mid);
1765 srv_cancel_sign_response(req->mid);
1767 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1769 END_PROFILE(SMBntcancel);
1773 /****************************************************************************
1775 ****************************************************************************/
1777 static NTSTATUS copy_internals(connection_struct *conn,
1778 struct smb_request *req,
1779 char *oldname, char *newname, uint32 attrs)
1781 SMB_STRUCT_STAT sbuf1, sbuf2;
1782 pstring last_component_oldname;
1783 pstring last_component_newname;
1784 files_struct *fsp1,*fsp2;
1788 NTSTATUS status = NT_STATUS_OK;
1793 if (!CAN_WRITE(conn)) {
1794 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1797 status = unix_convert(conn, oldname, False, last_component_oldname, &sbuf1);
1798 if (!NT_STATUS_IS_OK(status)) {
1802 status = check_name(conn, oldname);
1803 if (!NT_STATUS_IS_OK(status)) {
1807 /* Source must already exist. */
1808 if (!VALID_STAT(sbuf1)) {
1809 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1811 /* Ensure attributes match. */
1812 fattr = dos_mode(conn,oldname,&sbuf1);
1813 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1814 return NT_STATUS_NO_SUCH_FILE;
1817 status = unix_convert(conn, newname, False, last_component_newname, &sbuf2);
1818 if (!NT_STATUS_IS_OK(status)) {
1822 status = check_name(conn, newname);
1823 if (!NT_STATUS_IS_OK(status)) {
1827 /* Disallow if newname already exists. */
1828 if (VALID_STAT(sbuf2)) {
1829 return NT_STATUS_OBJECT_NAME_COLLISION;
1832 /* No links from a directory. */
1833 if (S_ISDIR(sbuf1.st_mode)) {
1834 return NT_STATUS_FILE_IS_A_DIRECTORY;
1837 /* Ensure this is within the share. */
1838 status = check_reduced_name(conn, oldname);
1839 if (!NT_STATUS_IS_OK(status)) {
1843 DEBUG(10,("copy_internals: doing file copy %s to %s\n", oldname, newname));
1845 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1846 FILE_READ_DATA, /* Read-only. */
1847 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1849 0, /* No create options. */
1850 FILE_ATTRIBUTE_NORMAL,
1854 if (!NT_STATUS_IS_OK(status)) {
1858 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1859 FILE_WRITE_DATA, /* Read-only. */
1860 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1862 0, /* No create options. */
1867 if (!NT_STATUS_IS_OK(status)) {
1868 close_file(fsp1,ERROR_CLOSE);
1872 if (sbuf1.st_size) {
1873 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1877 * As we are opening fsp1 read-only we only expect
1878 * an error on close on fsp2 if we are out of space.
1879 * Thus we don't look at the error return from the
1882 close_file(fsp1,NORMAL_CLOSE);
1884 /* Ensure the modtime is set correctly on the destination file. */
1885 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1887 status = close_file(fsp2,NORMAL_CLOSE);
1889 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1890 creates the file. This isn't the correct thing to do in the copy
1892 file_set_dosmode(conn, newname, fattr, &sbuf2,
1893 parent_dirname(newname));
1895 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1896 return NT_STATUS_DISK_FULL;
1899 if (!NT_STATUS_IS_OK(status)) {
1900 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1901 nt_errstr(status), oldname, newname));
1906 /****************************************************************************
1907 Reply to a NT rename request.
1908 ****************************************************************************/
1910 void reply_ntrename(connection_struct *conn, struct smb_request *req)
1916 BOOL src_has_wcard = False;
1917 BOOL dest_has_wcard = False;
1921 START_PROFILE(SMBntrename);
1924 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1925 END_PROFILE(SMBntrename);
1929 attrs = SVAL(req->inbuf,smb_vwv0);
1930 rename_type = SVAL(req->inbuf,smb_vwv1);
1932 p = smb_buf(req->inbuf) + 1;
1933 p += srvstr_get_path_wcard((char *)req->inbuf, req->flags2, oldname, p,
1934 sizeof(oldname), 0, STR_TERMINATE, &status,
1936 if (!NT_STATUS_IS_OK(status)) {
1937 reply_nterror(req, status);
1938 END_PROFILE(SMBntrename);
1942 if( is_ntfs_stream_name(oldname)) {
1943 /* Can't rename a stream. */
1944 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1945 END_PROFILE(SMBntrename);
1949 if (ms_has_wild(oldname)) {
1950 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1951 END_PROFILE(SMBntrename);
1956 p += srvstr_get_path_wcard((char *)req->inbuf, req->flags2, newname, p,
1957 sizeof(newname), 0, STR_TERMINATE, &status,
1959 if (!NT_STATUS_IS_OK(status)) {
1960 reply_nterror(req, status);
1961 END_PROFILE(SMBntrename);
1965 status = resolve_dfspath(conn, req->flags2 & FLAGS2_DFS_PATHNAMES,
1967 if (!NT_STATUS_IS_OK(status)) {
1968 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1969 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1970 ERRSRV, ERRbadpath);
1971 END_PROFILE(SMBntrename);
1974 reply_nterror(req, status);
1975 END_PROFILE(SMBntrename);
1979 status = resolve_dfspath(conn, req->flags2 & FLAGS2_DFS_PATHNAMES,
1981 if (!NT_STATUS_IS_OK(status)) {
1982 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1983 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1984 ERRSRV, ERRbadpath);
1985 END_PROFILE(SMBntrename);
1988 reply_nterror(req, status);
1989 END_PROFILE(SMBntrename);
1993 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1995 switch(rename_type) {
1996 case RENAME_FLAG_RENAME:
1997 status = rename_internals(conn, req, oldname, newname,
1998 attrs, False, src_has_wcard,
2001 case RENAME_FLAG_HARD_LINK:
2002 if (src_has_wcard || dest_has_wcard) {
2004 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2006 status = hardlink_internals(conn, oldname, newname);
2009 case RENAME_FLAG_COPY:
2010 if (src_has_wcard || dest_has_wcard) {
2012 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2014 status = copy_internals(conn, req, oldname,
2018 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
2019 status = NT_STATUS_INVALID_PARAMETER;
2022 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
2026 if (!NT_STATUS_IS_OK(status)) {
2027 if (open_was_deferred(req->mid)) {
2028 /* We have re-scheduled this call. */
2029 END_PROFILE(SMBntrename);
2033 reply_nterror(req, status);
2034 END_PROFILE(SMBntrename);
2038 reply_outbuf(req, 0, 0);
2040 END_PROFILE(SMBntrename);
2044 /****************************************************************************
2045 Reply to a notify change - queue the request and
2046 don't allow a directory to be opened.
2047 ****************************************************************************/
2049 static void call_nt_transact_notify_change(connection_struct *conn,
2050 struct smb_request *req,
2054 uint32 parameter_count,
2055 char **ppdata, uint32 data_count,
2056 uint32 max_data_count,
2057 uint32 max_param_count)
2059 uint16 *setup = *ppsetup;
2065 if(setup_count < 6) {
2066 reply_doserror(req, ERRDOS, ERRbadfunc);
2070 fsp = file_fsp(SVAL(setup,4));
2071 filter = IVAL(setup, 0);
2072 recursive = (SVAL(setup, 6) != 0) ? True : False;
2074 DEBUG(3,("call_nt_transact_notify_change\n"));
2077 reply_doserror(req, ERRDOS, ERRbadfid);
2082 char *filter_string;
2084 if (!(filter_string = notify_filter_string(NULL, filter))) {
2085 reply_nterror(req,NT_STATUS_NO_MEMORY);
2089 DEBUG(3,("call_nt_transact_notify_change: notify change "
2090 "called on %s, filter = %s, recursive = %d\n",
2091 fsp->fsp_name, filter_string, recursive));
2093 TALLOC_FREE(filter_string);
2096 if((!fsp->is_directory) || (conn != fsp->conn)) {
2097 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2101 if (fsp->notify == NULL) {
2103 status = change_notify_create(fsp, filter, recursive);
2105 if (!NT_STATUS_IS_OK(status)) {
2106 DEBUG(10, ("change_notify_create returned %s\n",
2107 nt_errstr(status)));
2108 reply_nterror(req, status);
2113 if (fsp->notify->num_changes != 0) {
2116 * We've got changes pending, respond immediately
2120 * TODO: write a torture test to check the filtering behaviour
2124 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
2127 * change_notify_reply() above has independently sent its
2134 * No changes pending, queue the request
2137 status = change_notify_add_request(req->inbuf, max_param_count, filter,
2139 if (!NT_STATUS_IS_OK(status)) {
2140 reply_nterror(req, status);
2145 /****************************************************************************
2146 Reply to an NT transact rename command.
2147 ****************************************************************************/
2149 static void call_nt_transact_rename(connection_struct *conn,
2150 struct smb_request *req,
2151 uint16 **ppsetup, uint32 setup_count,
2152 char **ppparams, uint32 parameter_count,
2153 char **ppdata, uint32 data_count,
2154 uint32 max_data_count)
2156 char *params = *ppparams;
2158 files_struct *fsp = NULL;
2159 BOOL replace_if_exists = False;
2160 BOOL dest_has_wcard = False;
2163 if(parameter_count < 5) {
2164 reply_doserror(req, ERRDOS, ERRbadfunc);
2168 fsp = file_fsp(SVAL(params, 0));
2169 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2170 if (!check_fsp(conn, req, fsp, ¤t_user)) {
2173 srvstr_get_path_wcard(params, req->flags2, new_name, params+4,
2174 sizeof(new_name), parameter_count - 4,
2175 STR_TERMINATE, &status, &dest_has_wcard);
2176 if (!NT_STATUS_IS_OK(status)) {
2177 reply_nterror(req, status);
2181 status = rename_internals(conn, req, fsp->fsp_name,
2182 new_name, 0, replace_if_exists, False, dest_has_wcard);
2184 if (!NT_STATUS_IS_OK(status)) {
2185 if (open_was_deferred(req->mid)) {
2186 /* We have re-scheduled this call. */
2189 reply_nterror(req, status);
2194 * Rename was successful.
2196 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2198 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2199 fsp->fsp_name, new_name));
2204 /******************************************************************************
2205 Fake up a completely empty SD.
2206 *******************************************************************************/
2208 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2212 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2214 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2221 /****************************************************************************
2222 Reply to query a security descriptor.
2223 ****************************************************************************/
2225 static void call_nt_transact_query_security_desc(connection_struct *conn,
2226 struct smb_request *req,
2230 uint32 parameter_count,
2233 uint32 max_data_count)
2235 char *params = *ppparams;
2236 char *data = *ppdata;
2238 SEC_DESC *psd = NULL;
2240 uint32 security_info_wanted;
2241 TALLOC_CTX *mem_ctx;
2242 files_struct *fsp = NULL;
2244 if(parameter_count < 8) {
2245 reply_doserror(req, ERRDOS, ERRbadfunc);
2249 fsp = file_fsp(SVAL(params,0));
2251 reply_doserror(req, ERRDOS, ERRbadfid);
2255 security_info_wanted = IVAL(params,4);
2257 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2258 (unsigned int)security_info_wanted ));
2260 params = nttrans_realloc(ppparams, 4);
2261 if(params == NULL) {
2262 reply_doserror(req, ERRDOS, ERRnomem);
2266 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2267 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2268 reply_doserror(req, ERRDOS, ERRnomem);
2273 * Get the permissions to return.
2276 if (!lp_nt_acl_support(SNUM(conn))) {
2277 sd_size = get_null_nt_acl(mem_ctx, &psd);
2279 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
2283 talloc_destroy(mem_ctx);
2284 reply_unixerror(req, ERRDOS, ERRnoaccess);
2288 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2290 SIVAL(params,0,(uint32)sd_size);
2292 if(max_data_count < sd_size) {
2294 send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
2295 params, 4, *ppdata, 0);
2296 talloc_destroy(mem_ctx);
2301 * Allocate the data we will point this at.
2304 data = nttrans_realloc(ppdata, sd_size);
2306 talloc_destroy(mem_ctx);
2307 reply_doserror(req, ERRDOS, ERRnomem);
2312 * Init the parse struct we will marshall into.
2315 prs_init(&pd, 0, mem_ctx, MARSHALL);
2318 * Setup the prs_struct to point at the memory we just
2322 prs_give_memory( &pd, data, (uint32)sd_size, False);
2325 * Finally, linearize into the outgoing buffer.
2328 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2329 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2330 security descriptor.\n"));
2332 * Return access denied for want of a better error message..
2334 talloc_destroy(mem_ctx);
2335 reply_unixerror(req, ERRDOS, ERRnoaccess);
2340 * Now we can delete the security descriptor.
2343 talloc_destroy(mem_ctx);
2345 send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2349 /****************************************************************************
2350 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2351 ****************************************************************************/
2353 static void call_nt_transact_set_security_desc(connection_struct *conn,
2354 struct smb_request *req,
2358 uint32 parameter_count,
2361 uint32 max_data_count)
2363 char *params= *ppparams;
2364 char *data = *ppdata;
2365 files_struct *fsp = NULL;
2366 uint32 security_info_sent = 0;
2369 if(parameter_count < 8) {
2370 reply_doserror(req, ERRDOS, ERRbadfunc);
2374 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
2375 reply_doserror(req, ERRDOS, ERRbadfid);
2379 if(!lp_nt_acl_support(SNUM(conn))) {
2383 security_info_sent = IVAL(params,4);
2385 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2386 (unsigned int)security_info_sent ));
2388 if (data_count == 0) {
2389 reply_doserror(req, ERRDOS, ERRnoaccess);
2393 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2394 reply_nterror(req, nt_status);
2400 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2404 /****************************************************************************
2406 ****************************************************************************/
2408 static void call_nt_transact_ioctl(connection_struct *conn,
2409 struct smb_request *req,
2410 uint16 **ppsetup, uint32 setup_count,
2411 char **ppparams, uint32 parameter_count,
2412 char **ppdata, uint32 data_count,
2413 uint32 max_data_count)
2420 static BOOL logged_message;
2421 char *pdata = *ppdata;
2423 if (setup_count != 8) {
2424 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2425 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2429 function = IVAL(*ppsetup, 0);
2430 fidnum = SVAL(*ppsetup, 4);
2431 isFSctl = CVAL(*ppsetup, 6);
2432 compfilter = CVAL(*ppsetup, 7);
2434 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2435 function, fidnum, isFSctl, compfilter));
2437 fsp=file_fsp(fidnum);
2438 /* this check is done in each implemented function case for now
2439 because I don't want to break anything... --metze
2440 FSP_BELONGS_CONN(fsp,conn);*/
2443 case FSCTL_SET_SPARSE:
2444 /* pretend this succeeded - tho strictly we should
2445 mark the file sparse (if the local fs supports it)
2446 so we can know if we need to pre-allocate or not */
2448 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2449 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2452 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2454 unsigned char objid[16];
2456 /* This should return the object-id on this file.
2457 * I think I'll make this be the inode+dev. JRA.
2460 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2462 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2467 pdata = nttrans_realloc(ppdata, data_count);
2468 if (pdata == NULL) {
2469 reply_nterror(req, NT_STATUS_NO_MEMORY);
2472 push_file_id_16(pdata, &fsp->file_id);
2473 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2474 push_file_id_16(pdata+32, &fsp->file_id);
2475 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2480 case FSCTL_GET_REPARSE_POINT:
2481 /* pretend this fail - my winXP does it like this
2485 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2486 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2489 case FSCTL_SET_REPARSE_POINT:
2490 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2494 DEBUG(10,("FSCTL_SET_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_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2501 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2502 * and return their volume names. If max_data_count is 16, then it is just
2503 * asking for the number of volumes and length of the combined names.
2505 * pdata is the data allocated by our caller, but that uses
2506 * total_data_count (which is 0 in our case) rather than max_data_count.
2507 * Allocate the correct amount and return the pointer to let
2508 * it be deallocated when we return.
2510 SHADOW_COPY_DATA *shadow_data = NULL;
2511 TALLOC_CTX *shadow_mem_ctx = NULL;
2512 BOOL labels = False;
2513 uint32 labels_data_count = 0;
2517 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2521 if (max_data_count < 16) {
2522 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2524 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2528 if (max_data_count > 16) {
2532 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2533 if (shadow_mem_ctx == NULL) {
2534 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2535 reply_nterror(req, NT_STATUS_NO_MEMORY);
2539 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2540 if (shadow_data == NULL) {
2541 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2542 talloc_destroy(shadow_mem_ctx);
2543 reply_nterror(req, NT_STATUS_NO_MEMORY);
2547 shadow_data->mem_ctx = shadow_mem_ctx;
2550 * Call the VFS routine to actually do the work.
2552 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2553 talloc_destroy(shadow_data->mem_ctx);
2554 if (errno == ENOSYS) {
2555 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2556 conn->connectpath));
2557 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2560 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2561 conn->connectpath));
2562 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2567 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2572 data_count = 12+labels_data_count+4;
2575 if (max_data_count<data_count) {
2576 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2577 max_data_count,data_count));
2578 talloc_destroy(shadow_data->mem_ctx);
2579 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2583 pdata = nttrans_realloc(ppdata, data_count);
2584 if (pdata == NULL) {
2585 talloc_destroy(shadow_data->mem_ctx);
2586 reply_nterror(req, NT_STATUS_NO_MEMORY);
2592 /* num_volumes 4 bytes */
2593 SIVAL(pdata,0,shadow_data->num_volumes);
2596 /* num_labels 4 bytes */
2597 SIVAL(pdata,4,shadow_data->num_volumes);
2600 /* needed_data_count 4 bytes */
2601 SIVAL(pdata,8,labels_data_count);
2605 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2606 shadow_data->num_volumes,fsp->fsp_name));
2607 if (labels && shadow_data->labels) {
2608 for (i=0;i<shadow_data->num_volumes;i++) {
2609 srvstr_push(pdata, req->flags2,
2610 cur_pdata, shadow_data->labels[i],
2611 2*sizeof(SHADOW_COPY_LABEL),
2612 STR_UNICODE|STR_TERMINATE);
2613 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2614 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2618 talloc_destroy(shadow_data->mem_ctx);
2620 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2626 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2628 /* pretend this succeeded -
2630 * we have to send back a list with all files owned by this SID
2632 * but I have to check that --metze
2636 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2638 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2640 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2644 /* unknown 4 bytes: this is not the length of the sid :-( */
2645 /*unknown = IVAL(pdata,0);*/
2647 sid_parse(pdata+4,sid_len,&sid);
2648 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2650 if (!sid_to_uid(&sid, &uid)) {
2651 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2652 sid_string_static(&sid),(unsigned long)sid_len));
2656 /* we can take a look at the find source :-)
2658 * find ./ -uid $uid -name '*' is what we need here
2661 * and send 4bytes len and then NULL terminated unicode strings
2664 * but I don't know how to deal with the paged results
2665 * (maybe we can hang the result anywhere in the fsp struct)
2667 * we don't send all files at once
2668 * and at the next we should *not* start from the beginning,
2669 * so we have to cache the result
2674 /* this works for now... */
2675 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2679 if (!logged_message) {
2680 logged_message = True; /* Only print this once... */
2681 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2686 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2690 #ifdef HAVE_SYS_QUOTAS
2691 /****************************************************************************
2692 Reply to get user quota
2693 ****************************************************************************/
2695 static void call_nt_transact_get_user_quota(connection_struct *conn,
2696 struct smb_request *req,
2700 uint32 parameter_count,
2703 uint32 max_data_count)
2705 NTSTATUS nt_status = NT_STATUS_OK;
2706 char *params = *ppparams;
2707 char *pdata = *ppdata;
2709 int data_len=0,param_len=0;
2712 files_struct *fsp = NULL;
2716 BOOL start_enum = True;
2717 SMB_NTQUOTA_STRUCT qt;
2718 SMB_NTQUOTA_LIST *tmp_list;
2719 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2724 if (current_user.ut.uid != 0) {
2725 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2726 lp_servicename(SNUM(conn)),conn->user));
2727 reply_doserror(req, ERRDOS, ERRnoaccess);
2732 * Ensure minimum number of parameters sent.
2735 if (parameter_count < 4) {
2736 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2737 reply_doserror(req, ERRDOS, ERRinvalidparam);
2741 /* maybe we can check the quota_fnum */
2742 fsp = file_fsp(SVAL(params,0));
2743 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2744 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2745 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2749 /* the NULL pointer checking for fsp->fake_file_handle->pd
2750 * is done by CHECK_NTQUOTA_HANDLE_OK()
2752 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2754 level = SVAL(params,2);
2756 /* unknown 12 bytes leading in params */
2759 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2760 /* seems that we should continue with the enum here --metze */
2762 if (qt_handle->quota_list!=NULL &&
2763 qt_handle->tmp_list==NULL) {
2766 free_ntquota_list(&(qt_handle->quota_list));
2768 /* Realloc the size of parameters and data we will return */
2770 params = nttrans_realloc(ppparams, param_len);
2771 if(params == NULL) {
2772 reply_doserror(req, ERRDOS, ERRnomem);
2777 SIVAL(params,0,data_len);
2784 case TRANSACT_GET_USER_QUOTA_LIST_START:
2786 if (qt_handle->quota_list==NULL &&
2787 qt_handle->tmp_list==NULL) {
2791 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2792 reply_doserror(req, ERRSRV, ERRerror);
2796 /* Realloc the size of parameters and data we will return */
2798 params = nttrans_realloc(ppparams, param_len);
2799 if(params == NULL) {
2800 reply_doserror(req, ERRDOS, ERRnomem);
2804 /* we should not trust the value in max_data_count*/
2805 max_data_count = MIN(max_data_count,2048);
2807 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2809 reply_doserror(req, ERRDOS, ERRnomem);
2815 /* set params Size of returned Quota Data 4 bytes*/
2816 /* but set it later when we know it */
2818 /* for each entry push the data */
2821 qt_handle->tmp_list = qt_handle->quota_list;
2824 tmp_list = qt_handle->tmp_list;
2826 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2827 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2829 sid_len = sid_size(&tmp_list->quotas->sid);
2830 entry_len = 40 + sid_len;
2832 /* nextoffset entry 4 bytes */
2833 SIVAL(entry,0,entry_len);
2835 /* then the len of the SID 4 bytes */
2836 SIVAL(entry,4,sid_len);
2838 /* unknown data 8 bytes SMB_BIG_UINT */
2839 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2841 /* the used disk space 8 bytes SMB_BIG_UINT */
2842 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2844 /* the soft quotas 8 bytes SMB_BIG_UINT */
2845 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2847 /* the hard quotas 8 bytes SMB_BIG_UINT */
2848 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2850 /* and now the SID */
2851 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2854 qt_handle->tmp_list = tmp_list;
2856 /* overwrite the offset of the last entry */
2857 SIVAL(entry-entry_len,0,0);
2859 data_len = 4+qt_len;
2860 /* overwrite the params quota_data_len */
2861 SIVAL(params,0,data_len);
2865 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2867 /* unknown 4 bytes IVAL(pdata,0) */
2869 if (data_count < 8) {
2870 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2871 reply_doserror(req, ERRDOS, ERRunknownlevel);
2875 sid_len = IVAL(pdata,4);
2876 /* Ensure this is less than 1mb. */
2877 if (sid_len > (1024*1024)) {
2878 reply_doserror(req, ERRDOS, ERRnomem);
2882 if (data_count < 8+sid_len) {
2883 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2884 reply_doserror(req, ERRDOS, ERRunknownlevel);
2888 data_len = 4+40+sid_len;
2890 if (max_data_count < data_len) {
2891 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2892 max_data_count, data_len));
2894 SIVAL(params,0,data_len);
2896 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2900 sid_parse(pdata+8,sid_len,&sid);
2902 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2905 * we have to return zero's in all fields
2906 * instead of returning an error here
2911 /* Realloc the size of parameters and data we will return */
2913 params = nttrans_realloc(ppparams, param_len);
2914 if(params == NULL) {
2915 reply_doserror(req, ERRDOS, ERRnomem);
2919 pdata = nttrans_realloc(ppdata, data_len);
2921 reply_doserror(req, ERRDOS, ERRnomem);
2927 /* set params Size of returned Quota Data 4 bytes*/
2928 SIVAL(params,0,data_len);
2930 /* nextoffset entry 4 bytes */
2933 /* then the len of the SID 4 bytes */
2934 SIVAL(entry,4,sid_len);
2936 /* unknown data 8 bytes SMB_BIG_UINT */
2937 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2939 /* the used disk space 8 bytes SMB_BIG_UINT */
2940 SBIG_UINT(entry,16,qt.usedspace);
2942 /* the soft quotas 8 bytes SMB_BIG_UINT */
2943 SBIG_UINT(entry,24,qt.softlim);
2945 /* the hard quotas 8 bytes SMB_BIG_UINT */
2946 SBIG_UINT(entry,32,qt.hardlim);
2948 /* and now the SID */
2949 sid_linearize(entry+40, sid_len, &sid);
2954 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2955 reply_doserror(req, ERRSRV, ERRerror);
2960 send_nt_replies(req, nt_status, params, param_len,
2964 /****************************************************************************
2965 Reply to set user quota
2966 ****************************************************************************/
2968 static void call_nt_transact_set_user_quota(connection_struct *conn,
2969 struct smb_request *req,
2973 uint32 parameter_count,
2976 uint32 max_data_count)
2978 char *params = *ppparams;
2979 char *pdata = *ppdata;
2980 int data_len=0,param_len=0;
2981 SMB_NTQUOTA_STRUCT qt;
2984 files_struct *fsp = NULL;
2989 if (current_user.ut.uid != 0) {
2990 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2991 lp_servicename(SNUM(conn)),conn->user));
2992 reply_doserror(req, ERRDOS, ERRnoaccess);
2997 * Ensure minimum number of parameters sent.
3000 if (parameter_count < 2) {
3001 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
3002 reply_doserror(req, ERRDOS, ERRinvalidparam);
3006 /* maybe we can check the quota_fnum */
3007 fsp = file_fsp(SVAL(params,0));
3008 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
3009 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
3010 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
3014 if (data_count < 40) {
3015 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
3016 reply_doserror(req, ERRDOS, ERRunknownlevel);
3020 /* offset to next quota record.
3021 * 4 bytes IVAL(pdata,0)
3026 sid_len = IVAL(pdata,4);
3028 if (data_count < 40+sid_len) {
3029 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
3030 reply_doserror(req, ERRDOS, ERRunknownlevel);
3034 /* unknown 8 bytes in pdata
3035 * maybe its the change time in NTTIME
3038 /* the used space 8 bytes (SMB_BIG_UINT)*/
3039 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
3040 #ifdef LARGE_SMB_OFF_T
3041 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
3042 #else /* LARGE_SMB_OFF_T */
3043 if ((IVAL(pdata,20) != 0)&&
3044 ((qt.usedspace != 0xFFFFFFFF)||
3045 (IVAL(pdata,20)!=0xFFFFFFFF))) {
3046 /* more than 32 bits? */
3047 reply_doserror(req, ERRDOS, ERRunknownlevel);
3050 #endif /* LARGE_SMB_OFF_T */
3052 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
3053 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
3054 #ifdef LARGE_SMB_OFF_T
3055 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
3056 #else /* LARGE_SMB_OFF_T */
3057 if ((IVAL(pdata,28) != 0)&&
3058 ((qt.softlim != 0xFFFFFFFF)||
3059 (IVAL(pdata,28)!=0xFFFFFFFF))) {
3060 /* more than 32 bits? */
3061 reply_doserror(req, ERRDOS, ERRunknownlevel);
3064 #endif /* LARGE_SMB_OFF_T */
3066 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
3067 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
3068 #ifdef LARGE_SMB_OFF_T
3069 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
3070 #else /* LARGE_SMB_OFF_T */
3071 if ((IVAL(pdata,36) != 0)&&
3072 ((qt.hardlim != 0xFFFFFFFF)||
3073 (IVAL(pdata,36)!=0xFFFFFFFF))) {
3074 /* more than 32 bits? */
3075 reply_doserror(req, ERRDOS, ERRunknownlevel);
3078 #endif /* LARGE_SMB_OFF_T */
3080 sid_parse(pdata+40,sid_len,&sid);
3081 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
3083 /* 44 unknown bytes left... */
3085 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
3086 reply_doserror(req, ERRSRV, ERRerror);
3090 send_nt_replies(req, NT_STATUS_OK, params, param_len,
3093 #endif /* HAVE_SYS_QUOTAS */
3095 static void handle_nttrans(connection_struct *conn,
3096 struct trans_state *state,
3097 struct smb_request *req)
3099 if (Protocol >= PROTOCOL_NT1) {
3100 req->flags2 |= 0x40; /* IS_LONG_NAME */
3101 SSVAL(req->inbuf,smb_flg2,req->flags2);
3104 /* Now we must call the relevant NT_TRANS function */
3105 switch(state->call) {
3106 case NT_TRANSACT_CREATE:
3108 START_PROFILE(NT_transact_create);
3109 call_nt_transact_create(
3111 &state->setup, state->setup_count,
3112 &state->param, state->total_param,
3113 &state->data, state->total_data,
3114 state->max_data_return);
3115 END_PROFILE(NT_transact_create);
3119 case NT_TRANSACT_IOCTL:
3121 START_PROFILE(NT_transact_ioctl);
3122 call_nt_transact_ioctl(
3124 &state->setup, state->setup_count,
3125 &state->param, state->total_param,
3126 &state->data, state->total_data,
3127 state->max_data_return);
3128 END_PROFILE(NT_transact_ioctl);
3132 case NT_TRANSACT_SET_SECURITY_DESC:
3134 START_PROFILE(NT_transact_set_security_desc);
3135 call_nt_transact_set_security_desc(
3137 &state->setup, state->setup_count,
3138 &state->param, state->total_param,
3139 &state->data, state->total_data,
3140 state->max_data_return);
3141 END_PROFILE(NT_transact_set_security_desc);
3145 case NT_TRANSACT_NOTIFY_CHANGE:
3147 START_PROFILE(NT_transact_notify_change);
3148 call_nt_transact_notify_change(
3150 &state->setup, state->setup_count,
3151 &state->param, state->total_param,
3152 &state->data, state->total_data,
3153 state->max_data_return,
3154 state->max_param_return);
3155 END_PROFILE(NT_transact_notify_change);
3159 case NT_TRANSACT_RENAME:
3161 START_PROFILE(NT_transact_rename);
3162 call_nt_transact_rename(
3164 &state->setup, state->setup_count,
3165 &state->param, state->total_param,
3166 &state->data, state->total_data,
3167 state->max_data_return);
3168 END_PROFILE(NT_transact_rename);
3172 case NT_TRANSACT_QUERY_SECURITY_DESC:
3174 START_PROFILE(NT_transact_query_security_desc);
3175 call_nt_transact_query_security_desc(
3177 &state->setup, state->setup_count,
3178 &state->param, state->total_param,
3179 &state->data, state->total_data,
3180 state->max_data_return);
3181 END_PROFILE(NT_transact_query_security_desc);
3185 #ifdef HAVE_SYS_QUOTAS
3186 case NT_TRANSACT_GET_USER_QUOTA:
3188 START_PROFILE(NT_transact_get_user_quota);
3189 call_nt_transact_get_user_quota(
3191 &state->setup, state->setup_count,
3192 &state->param, state->total_param,
3193 &state->data, state->total_data,
3194 state->max_data_return);
3195 END_PROFILE(NT_transact_get_user_quota);
3199 case NT_TRANSACT_SET_USER_QUOTA:
3201 START_PROFILE(NT_transact_set_user_quota);
3202 call_nt_transact_set_user_quota(
3204 &state->setup, state->setup_count,
3205 &state->param, state->total_param,
3206 &state->data, state->total_data,
3207 state->max_data_return);
3208 END_PROFILE(NT_transact_set_user_quota);
3211 #endif /* HAVE_SYS_QUOTAS */
3214 /* Error in request */
3215 DEBUG(0,("handle_nttrans: Unknown request %d in "
3216 "nttrans call\n", state->call));
3217 reply_doserror(req, ERRSRV, ERRerror);
3223 /****************************************************************************
3224 Reply to a SMBNTtrans.
3225 ****************************************************************************/
3227 void reply_nttrans(connection_struct *conn, struct smb_request *req)
3233 uint16 function_code;
3235 struct trans_state *state;
3238 START_PROFILE(SMBnttrans);
3240 if (req->wct < 19) {
3241 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3242 END_PROFILE(SMBnttrans);
3246 size = smb_len(req->inbuf) + 4;
3247 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
3248 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
3249 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
3250 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
3251 function_code = SVAL(req->inbuf, smb_nt_Function);
3253 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3254 reply_doserror(req, ERRSRV, ERRaccess);
3255 END_PROFILE(SMBnttrans);
3259 result = allow_new_trans(conn->pending_trans, req->mid);
3260 if (!NT_STATUS_IS_OK(result)) {
3261 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3262 reply_nterror(req, result);
3263 END_PROFILE(SMBnttrans);
3267 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3268 reply_doserror(req, ERRSRV, ERRaccess);
3269 END_PROFILE(SMBnttrans);
3273 state->cmd = SMBnttrans;
3275 state->mid = req->mid;
3276 state->vuid = req->vuid;
3277 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
3279 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
3280 state->param = NULL;
3281 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
3282 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
3284 /* setup count is in *words* */
3285 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
3286 state->setup = NULL;
3287 state->call = function_code;
3290 * All nttrans messages we handle have smb_wct == 19 +
3291 * state->setup_count. Ensure this is so as a sanity check.
3294 if(req->wct != 19 + (state->setup_count/2)) {
3295 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3296 req->wct, 19 + (state->setup_count/2)));
3300 /* Don't allow more than 128mb for each value. */
3301 if ((state->total_data > (1024*1024*128)) ||
3302 (state->total_param > (1024*1024*128))) {
3303 reply_doserror(req, ERRDOS, ERRnomem);
3304 END_PROFILE(SMBnttrans);
3308 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3311 if (state->total_data) {
3312 /* Can't use talloc here, the core routines do realloc on the
3313 * params and data. */
3314 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3315 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3316 "bytes !\n", (unsigned int)state->total_data));
3318 reply_doserror(req, ERRDOS, ERRnomem);
3319 END_PROFILE(SMBnttrans);
3322 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3324 if ((smb_base(req->inbuf)+dsoff+dscnt
3325 > (char *)req->inbuf + size) ||
3326 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
3329 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3332 if (state->total_param) {
3333 /* Can't use talloc here, the core routines do realloc on the
3334 * params and data. */
3335 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3336 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3337 "bytes !\n", (unsigned int)state->total_param));
3338 SAFE_FREE(state->data);
3340 reply_doserror(req, ERRDOS, ERRnomem);
3341 END_PROFILE(SMBnttrans);
3344 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3346 if ((smb_base(req->inbuf)+psoff+pscnt
3347 > (char *)req->inbuf + size) ||
3348 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
3351 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3354 state->received_data = dscnt;
3355 state->received_param = pscnt;
3357 if(state->setup_count > 0) {
3358 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3359 state->setup_count));
3360 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3361 if (state->setup == NULL) {
3362 DEBUG(0,("reply_nttrans : Out of memory\n"));
3363 SAFE_FREE(state->data);
3364 SAFE_FREE(state->param);
3366 reply_doserror(req, ERRDOS, ERRnomem);
3367 END_PROFILE(SMBnttrans);
3371 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3372 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3375 if (smb_nt_SetupStart + state->setup_count > size) {
3379 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
3380 state->setup_count);
3381 dump_data(10, (uint8 *)state->setup, state->setup_count);
3384 if ((state->received_data == state->total_data) &&
3385 (state->received_param == state->total_param)) {
3386 handle_nttrans(conn, state, req);
3387 SAFE_FREE(state->param);
3388 SAFE_FREE(state->data);
3390 END_PROFILE(SMBnttrans);
3394 DLIST_ADD(conn->pending_trans, state);
3396 /* We need to send an interim response then receive the rest
3397 of the parameter/data bytes */
3398 reply_outbuf(req, 0, 0);
3399 show_msg((char *)req->outbuf);
3400 END_PROFILE(SMBnttrans);
3405 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3406 SAFE_FREE(state->data);
3407 SAFE_FREE(state->param);
3409 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3410 END_PROFILE(SMBnttrans);
3414 /****************************************************************************
3415 Reply to a SMBnttranss
3416 ****************************************************************************/
3418 void reply_nttranss(connection_struct *conn, struct smb_request *req)
3420 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3421 struct trans_state *state;
3425 START_PROFILE(SMBnttranss);
3427 show_msg((char *)req->inbuf);
3429 if (req->wct < 18) {
3430 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3431 END_PROFILE(SMBnttranss);
3435 for (state = conn->pending_trans; state != NULL;
3436 state = state->next) {
3437 if (state->mid == req->mid) {
3442 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3443 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3444 END_PROFILE(SMBnttranss);
3448 /* Revise state->total_param and state->total_data in case they have
3449 changed downwards */
3450 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
3451 < state->total_param) {
3452 state->total_param = IVAL(req->inbuf,
3453 smb_nts_TotalParameterCount);
3455 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
3456 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
3459 size = smb_len(req->inbuf) + 4;
3461 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
3462 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
3463 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
3465 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
3466 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
3467 doff = IVAL(req->inbuf, smb_nts_DataOffset);
3469 state->received_param += pcnt;
3470 state->received_data += dcnt;
3472 if ((state->received_data > state->total_data) ||
3473 (state->received_param > state->total_param))
3477 if (pdisp+pcnt > state->total_param)
3479 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3481 if (pdisp > state->total_param)
3483 if ((smb_base(req->inbuf) + poff + pcnt
3484 > (char *)req->inbuf + size) ||
3485 (smb_base(req->inbuf) + poff + pcnt
3486 < smb_base(req->inbuf)))
3488 if (state->param + pdisp < state->param)
3491 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
3496 if (ddisp+dcnt > state->total_data)
3498 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3500 if (ddisp > state->total_data)
3502 if ((smb_base(req->inbuf) + doff + dcnt
3503 > (char *)req->inbuf + size) ||
3504 (smb_base(req->inbuf) + doff + dcnt
3505 < smb_base(req->inbuf)))
3507 if (state->data + ddisp < state->data)
3510 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
3514 if ((state->received_param < state->total_param) ||
3515 (state->received_data < state->total_data)) {
3516 END_PROFILE(SMBnttranss);
3521 * construct_reply_common will copy smb_com from inbuf to
3522 * outbuf. SMBnttranss is wrong here.
3524 SCVAL(req->inbuf,smb_com,SMBnttrans);
3526 handle_nttrans(conn, state, req);
3528 DLIST_REMOVE(conn->pending_trans, state);
3529 SAFE_FREE(state->data);
3530 SAFE_FREE(state->param);
3532 END_PROFILE(SMBnttranss);
3537 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3538 DLIST_REMOVE(conn->pending_trans, state);
3539 SAFE_FREE(state->data);
3540 SAFE_FREE(state->param);
3542 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3543 END_PROFILE(SMBnttranss);