2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-2007
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 struct current_user current_user;
27 static const char *known_nt_pipes[] = {
48 static char *nttrans_realloc(char **ptr, size_t size)
51 smb_panic("nttrans_realloc() called with NULL ptr");
54 *ptr = (char *)SMB_REALLOC(*ptr, size);
58 memset(*ptr,'\0',size);
62 /****************************************************************************
63 Send the required number of replies back.
64 We assume all fields other than the data fields are
65 set correctly for the type of call.
66 HACK ! Always assumes smb_setup field is zero.
67 ****************************************************************************/
69 void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
70 char *params, int paramsize,
71 char *pdata, int datasize)
73 int data_to_send = datasize;
74 int params_to_send = paramsize;
78 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
79 int alignment_offset = 3;
80 int data_alignment_offset = 0;
83 * If there genuinely are no parameters or data to send just send
87 if(params_to_send == 0 && data_to_send == 0) {
88 reply_outbuf(req, 18, 0);
89 show_msg((char *)req->outbuf);
94 * When sending params and data ensure that both are nicely aligned.
95 * Only do this alignment when there is also data to send - else
96 * can cause NT redirector problems.
99 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
100 data_alignment_offset = 4 - (params_to_send % 4);
104 * Space is bufsize minus Netbios over TCP header minus SMB header.
105 * The alignment_offset is to align the param bytes on a four byte
106 * boundary (2 bytes for data len, one byte pad).
107 * NT needs this to work correctly.
110 useable_space = max_send - (smb_size
113 + data_alignment_offset);
116 * useable_space can never be more than max_send minus the
120 useable_space = MIN(useable_space,
121 max_send - (alignment_offset+data_alignment_offset));
124 while (params_to_send || data_to_send) {
127 * Calculate whether we will totally or partially fill this packet.
130 total_sent_thistime = params_to_send + data_to_send +
131 alignment_offset + data_alignment_offset;
134 * We can never send more than useable_space.
137 total_sent_thistime = MIN(total_sent_thistime, useable_space);
139 reply_outbuf(req, 18, total_sent_thistime);
142 * Set total params and data to be sent.
145 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
146 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
149 * Calculate how many parameters and data we can fit into
150 * this packet. Parameters get precedence.
153 params_sent_thistime = MIN(params_to_send,useable_space);
154 data_sent_thistime = useable_space - params_sent_thistime;
155 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
157 SIVAL(req->outbuf, smb_ntr_ParameterCount,
158 params_sent_thistime);
160 if(params_sent_thistime == 0) {
161 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
162 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
165 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
166 * parameter bytes, however the first 4 bytes of outbuf are
167 * the Netbios over TCP header. Thus use smb_base() to subtract
168 * them from the calculation.
171 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
172 ((smb_buf(req->outbuf)+alignment_offset)
173 - smb_base(req->outbuf)));
175 * Absolute displacement of param bytes sent in this packet.
178 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
183 * Deal with the data portion.
186 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
188 if(data_sent_thistime == 0) {
189 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
190 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
193 * The offset of the data bytes is the offset of the
194 * parameter bytes plus the number of parameters being sent this time.
197 SIVAL(req->outbuf, smb_ntr_DataOffset,
198 ((smb_buf(req->outbuf)+alignment_offset) -
199 smb_base(req->outbuf))
200 + params_sent_thistime + data_alignment_offset);
201 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
205 * Copy the param bytes into the packet.
208 if(params_sent_thistime) {
209 if (alignment_offset != 0) {
210 memset(smb_buf(req->outbuf), 0,
213 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
214 params_sent_thistime);
218 * Copy in the data bytes
221 if(data_sent_thistime) {
222 if (data_alignment_offset != 0) {
223 memset((smb_buf(req->outbuf)+alignment_offset+
224 params_sent_thistime), 0,
225 data_alignment_offset);
227 memcpy(smb_buf(req->outbuf)+alignment_offset
228 +params_sent_thistime+data_alignment_offset,
229 pd,data_sent_thistime);
232 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
233 params_sent_thistime, data_sent_thistime, useable_space));
234 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
235 params_to_send, data_to_send, paramsize, datasize));
237 if (NT_STATUS_V(nt_error)) {
238 error_packet_set((char *)req->outbuf,
243 /* Send the packet */
244 show_msg((char *)req->outbuf);
245 if (!send_smb(smbd_server_fd(),(char *)req->outbuf)) {
246 exit_server_cleanly("send_nt_replies: send_smb failed.");
249 TALLOC_FREE(req->outbuf);
251 pp += params_sent_thistime;
252 pd += data_sent_thistime;
254 params_to_send -= params_sent_thistime;
255 data_to_send -= data_sent_thistime;
261 if(params_to_send < 0 || data_to_send < 0) {
262 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
263 params_to_send, data_to_send));
269 /****************************************************************************
270 Is it an NTFS stream name ?
271 ****************************************************************************/
273 bool is_ntfs_stream_name(const char *fname)
275 if (lp_posix_pathnames()) {
278 return (strchr_m(fname, ':') != NULL) ? True : False;
281 struct case_semantics_state {
282 connection_struct *conn;
285 bool short_case_preserve;
288 /****************************************************************************
289 Restore case semantics.
290 ****************************************************************************/
291 static int restore_case_semantics(struct case_semantics_state *state)
293 state->conn->case_sensitive = state->case_sensitive;
294 state->conn->case_preserve = state->case_preserve;
295 state->conn->short_case_preserve = state->short_case_preserve;
299 /****************************************************************************
301 ****************************************************************************/
302 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
303 connection_struct *conn)
305 struct case_semantics_state *result;
307 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
308 DEBUG(0, ("talloc failed\n"));
313 result->case_sensitive = conn->case_sensitive;
314 result->case_preserve = conn->case_preserve;
315 result->short_case_preserve = conn->short_case_preserve;
318 conn->case_sensitive = True;
319 conn->case_preserve = True;
320 conn->short_case_preserve = True;
322 talloc_set_destructor(result, restore_case_semantics);
327 /****************************************************************************
328 Reply to an NT create and X call on a pipe
329 ****************************************************************************/
331 static void nt_open_pipe(char *fname, connection_struct *conn,
332 struct smb_request *req, int *ppnum)
334 smb_np_struct *p = NULL;
337 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
339 /* See if it is one we want to handle. */
341 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
342 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
347 for( i = 0; known_nt_pipes[i]; i++ ) {
348 if( strequal(fname,known_nt_pipes[i])) {
353 if ( known_nt_pipes[i] == NULL ) {
354 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
359 /* Strip \\ off the name. */
362 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
364 p = open_rpc_pipe_p(fname, conn, req->vuid);
366 reply_doserror(req, ERRSRV, ERRnofids);
370 /* TODO: Add pipe to db */
372 if ( !store_pipe_opendb( p ) ) {
373 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
380 /****************************************************************************
381 Reply to an NT create and X call for pipes.
382 ****************************************************************************/
384 static void do_ntcreate_pipe_open(connection_struct *conn,
385 struct smb_request *req)
390 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
391 TALLOC_CTX *ctx = talloc_tos();
393 srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
394 smb_buf(req->inbuf), STR_TERMINATE);
397 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
401 nt_open_pipe(fname, conn, req, &pnum);
409 * Deal with pipe return.
412 if (flags & EXTENDED_RESPONSE_REQUIRED) {
413 /* This is very strange. We
414 * return 50 words, but only set
415 * the wcnt to 42 ? It's definately
416 * what happens on the wire....
418 reply_outbuf(req, 50, 0);
419 SCVAL(req->outbuf,smb_wct,42);
421 reply_outbuf(req, 34, 0);
424 p = (char *)req->outbuf + smb_vwv2;
428 SIVAL(p,0,FILE_WAS_OPENED);
431 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
434 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
436 SSVAL(p,2, 0x5FF); /* ? */
439 if (flags & EXTENDED_RESPONSE_REQUIRED) {
441 SIVAL(p,0,FILE_GENERIC_ALL);
443 * For pipes W2K3 seems to return
445 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
447 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
450 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
455 /****************************************************************************
456 Reply to an NT create and X call for a quota file.
457 ****************************************************************************/
459 static void reply_ntcreate_and_X_quota(connection_struct *conn,
460 struct smb_request *req,
461 enum FAKE_FILE_TYPE fake_file_type,
465 uint32 desired_access = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
469 status = open_fake_file(conn, fake_file_type, fname, desired_access,
472 if (!NT_STATUS_IS_OK(status)) {
473 reply_nterror(req, status);
477 reply_outbuf(req, 34, 0);
479 p = (char *)req->outbuf + smb_vwv2;
481 /* SCVAL(p,0,NO_OPLOCK_RETURN); */
483 SSVAL(p,0,fsp->fnum);
485 DEBUG(5,("reply_ntcreate_and_X_quota: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
490 /****************************************************************************
491 Reply to an NT create and X call.
492 ****************************************************************************/
494 void reply_ntcreate_and_X(connection_struct *conn,
495 struct smb_request *req)
500 uint32 file_attributes;
502 uint32 create_disposition;
503 uint32 create_options;
505 SMB_BIG_UINT allocation_size;
506 /* Breakout the oplock request bits so we can set the
507 reply bits separately. */
508 int oplock_request = 0;
510 SMB_OFF_T file_len = 0;
511 SMB_STRUCT_STAT sbuf;
513 files_struct *fsp = NULL;
515 struct timespec c_timespec;
516 struct timespec a_timespec;
517 struct timespec m_timespec;
518 bool extended_oplock_granted = False;
520 struct case_semantics_state *case_state = NULL;
521 TALLOC_CTX *ctx = talloc_tos();
523 START_PROFILE(SMBntcreateX);
526 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
530 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
531 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
532 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
533 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
534 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
535 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
536 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
538 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,smb_ntcreate_AllocationSize);
539 #ifdef LARGE_SMB_OFF_T
540 allocation_size |= (((SMB_BIG_UINT)IVAL(req->inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
543 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
544 smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
546 if (!NT_STATUS_IS_OK(status)) {
547 reply_nterror(req, status);
548 END_PROFILE(SMBntcreateX);
552 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
553 "file_attributes = 0x%x, share_access = 0x%x, "
554 "create_disposition = 0x%x create_options = 0x%x "
555 "root_dir_fid = 0x%x, fname = %s\n",
557 (unsigned int)access_mask,
558 (unsigned int)file_attributes,
559 (unsigned int)share_access,
560 (unsigned int)create_disposition,
561 (unsigned int)create_options,
562 (unsigned int)root_dir_fid,
566 * If it's an IPC, use the pipe handler.
570 if (lp_nt_pipe_support()) {
571 do_ntcreate_pipe_open(conn, req);
572 END_PROFILE(SMBntcreateX);
575 reply_doserror(req, ERRDOS, ERRnoaccess);
576 END_PROFILE(SMBntcreateX);
581 if (create_options & FILE_OPEN_BY_FILE_ID) {
582 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
583 END_PROFILE(SMBntcreateX);
591 if (root_dir_fid != 0) {
593 * This filename is relative to a directory fid.
595 char *parent_fname = NULL;
597 files_struct *dir_fsp = file_fsp(root_dir_fid);
600 reply_doserror(req, ERRDOS, ERRbadfid);
601 END_PROFILE(SMBntcreateX);
605 if(!dir_fsp->is_directory) {
608 * Check to see if this is a mac fork of some kind.
611 if( is_ntfs_stream_name(fname)) {
613 req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
614 END_PROFILE(SMBntcreateX);
619 we need to handle the case when we get a
620 relative open relative to a file and the
621 pathname is blank - this is a reopen!
622 (hint from demyn plantenberg)
625 reply_doserror(req, ERRDOS, ERRbadfid);
626 END_PROFILE(SMBntcreateX);
630 if (ISDOT(dir_fsp->fsp_name)) {
632 * We're at the toplevel dir, the final file name
633 * must not contain ./, as this is filtered out
634 * normally by srvstr_get_path and unix_convert
635 * explicitly rejects paths containing ./.
637 parent_fname = talloc_strdup(ctx,"");
639 reply_nterror(req, NT_STATUS_NO_MEMORY);
640 END_PROFILE(SMBntcreateX);
644 size_t dir_name_len = strlen(dir_fsp->fsp_name);
647 * Copy in the base directory name.
650 parent_fname = TALLOC_ARRAY(ctx, char, dir_name_len+2);
652 reply_nterror(req, NT_STATUS_NO_MEMORY);
653 END_PROFILE(SMBntcreateX);
656 memcpy(parent_fname, dir_fsp->fsp_name,
660 * Ensure it ends in a '/'.
661 * We used TALLOC_SIZE +2 to add space for the '/'.
665 && (parent_fname[dir_name_len-1] != '\\')
666 && (parent_fname[dir_name_len-1] != '/')) {
667 parent_fname[dir_name_len] = '/';
668 parent_fname[dir_name_len+1] = '\0';
672 tmp = talloc_asprintf(ctx, "%s%s", parent_fname, fname);
674 reply_nterror(req, NT_STATUS_NO_MEMORY);
675 END_PROFILE(SMBntcreateX);
682 * Check to see if this is a mac fork of some kind.
685 if( is_ntfs_stream_name(fname)) {
686 enum FAKE_FILE_TYPE fake_file_type = is_fake_file(fname);
687 if (fake_file_type!=FAKE_FILE_TYPE_NONE) {
689 * Here we go! support for changing the disk quotas --metze
691 * We need to fake up to open this MAGIC QUOTA file
692 * and return a valid FID.
694 * w2k close this file directly after openening
695 * xp also tries a QUERY_FILE_INFO on the file and then close it
697 reply_ntcreate_and_X_quota(conn, req,
698 fake_file_type, fname);
700 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
702 END_PROFILE(SMBntcreateX);
707 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
708 if (oplock_request) {
709 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
714 * Now contruct the smb_open_mode value from the filename,
715 * desired access and the share access.
717 status = resolve_dfspath(ctx, conn,
718 req->flags2 & FLAGS2_DFS_PATHNAMES,
721 if (!NT_STATUS_IS_OK(status)) {
722 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
723 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
725 END_PROFILE(SMBntcreateX);
728 reply_nterror(req, status);
729 END_PROFILE(SMBntcreateX);
734 * Ordinary file or directory.
738 * Check if POSIX semantics are wanted.
741 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
742 case_state = set_posix_case_semantics(NULL, conn);
743 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
746 status = unix_convert(ctx, conn, fname, False, &fname, NULL, &sbuf);
747 if (!NT_STATUS_IS_OK(status)) {
748 TALLOC_FREE(case_state);
749 reply_nterror(req, status);
750 END_PROFILE(SMBntcreateX);
753 /* All file access must go through check_name() */
754 status = check_name(conn, fname);
755 if (!NT_STATUS_IS_OK(status)) {
756 TALLOC_FREE(case_state);
757 reply_nterror(req, status);
758 END_PROFILE(SMBntcreateX);
762 /* This is the correct thing to do (check every time) but can_delete
763 * is expensive (it may have to read the parent directory
764 * permissions). So for now we're not doing it unless we have a strong
765 * hint the client is really going to delete this file. If the client
766 * is forcing FILE_CREATE let the filesystem take care of the
769 /* Setting FILE_SHARE_DELETE is the hint. */
771 if (lp_acl_check_permissions(SNUM(conn))
772 && (create_disposition != FILE_CREATE)
773 && (share_access & FILE_SHARE_DELETE)
774 && (access_mask & DELETE_ACCESS)) {
775 if (((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY)
776 && !lp_delete_readonly(SNUM(conn))) ||
777 !can_delete_file_in_directory(conn, fname)) {
778 TALLOC_FREE(case_state);
779 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
780 END_PROFILE(SMBntcreateX);
786 /* We need to support SeSecurityPrivilege for this. */
787 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
788 !user_has_privileges(current_user.nt_user_token,
790 TALLOC_FREE(case_state);
791 END_PROFILE(SMBntcreateX);
792 return ERROR_NT(NT_STATUS_PRIVILEGE_NOT_HELD);
797 * If it's a request for a directory open, deal with it separately.
800 if(create_options & FILE_DIRECTORY_FILE) {
802 /* Can't open a temp directory. IFS kit test. */
803 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
804 TALLOC_FREE(case_state);
805 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
806 END_PROFILE(SMBntcreateX);
811 status = open_directory(conn, req, fname, &sbuf,
822 * Ordinary file case.
825 /* NB. We have a potential bug here. If we
826 * cause an oplock break to ourselves, then we
827 * could end up processing filename related
828 * SMB requests whilst we await the oplock
829 * break response. As we may have changed the
830 * filename case semantics to be POSIX-like,
831 * this could mean a filename request could
832 * fail when it should succeed. This is a rare
833 * condition, but eventually we must arrange
834 * to restore the correct case semantics
835 * before issuing an oplock break request to
836 * our client. JRA. */
838 status = open_file_ntcreate(conn, req, fname, &sbuf,
847 /* We cheat here. There are two cases we
848 * care about. One is a directory rename,
849 * where the NT client will attempt to
850 * open the source directory for
851 * DELETE access. Note that when the
852 * NT client does this it does *not*
853 * set the directory bit in the
854 * request packet. This is translated
855 * into a read/write open
856 * request. POSIX states that any open
857 * for write request on a directory
858 * will generate an EISDIR error, so
859 * we can catch this here and open a
860 * pseudo handle that is flagged as a
861 * directory. The second is an open
862 * for a permissions read only, which
863 * we handle in the open_file_stat case. JRA.
866 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
869 * Fail the open if it was explicitly a non-directory
873 if (create_options & FILE_NON_DIRECTORY_FILE) {
874 TALLOC_FREE(case_state);
877 NT_STATUS_FILE_IS_A_DIRECTORY);
878 END_PROFILE(SMBntcreateX);
883 status = open_directory(conn, req, fname,
894 TALLOC_FREE(case_state);
896 if (!NT_STATUS_IS_OK(status)) {
897 if (open_was_deferred(req->mid)) {
898 /* We have re-scheduled this call. */
899 END_PROFILE(SMBntcreateX);
902 reply_openerror(req, status);
903 END_PROFILE(SMBntcreateX);
907 file_len = sbuf.st_size;
908 fattr = dos_mode(conn,fname,&sbuf);
910 fattr = FILE_ATTRIBUTE_NORMAL;
912 if (!fsp->is_directory && (fattr & aDIR)) {
913 close_file(fsp,ERROR_CLOSE);
914 reply_doserror(req, ERRDOS, ERRnoaccess);
915 END_PROFILE(SMBntcreateX);
919 /* Save the requested allocation size. */
920 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
921 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
922 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
923 if (fsp->is_directory) {
924 close_file(fsp,ERROR_CLOSE);
925 /* Can't set allocation size on a directory. */
926 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
927 END_PROFILE(SMBntcreateX);
930 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
931 close_file(fsp,ERROR_CLOSE);
932 reply_nterror(req, NT_STATUS_DISK_FULL);
933 END_PROFILE(SMBntcreateX);
937 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
942 * If the caller set the extended oplock request bit
943 * and we granted one (by whatever means) - set the
944 * correct bit for extended oplock reply.
947 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
948 extended_oplock_granted = True;
951 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
952 extended_oplock_granted = True;
955 if (flags & EXTENDED_RESPONSE_REQUIRED) {
956 /* This is very strange. We
957 * return 50 words, but only set
958 * the wcnt to 42 ? It's definately
959 * what happens on the wire....
961 reply_outbuf(req, 50, 0);
962 SCVAL(req->outbuf,smb_wct,42);
964 reply_outbuf(req, 34, 0);
967 p = (char *)req->outbuf + smb_vwv2;
970 * Currently as we don't support level II oplocks we just report
971 * exclusive & batch here.
974 if (extended_oplock_granted) {
975 if (flags & REQUEST_BATCH_OPLOCK) {
976 SCVAL(p,0, BATCH_OPLOCK_RETURN);
978 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
980 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
981 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
983 SCVAL(p,0,NO_OPLOCK_RETURN);
987 SSVAL(p,0,fsp->fnum);
989 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
990 SIVAL(p,0,FILE_WAS_SUPERSEDED);
997 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
998 a_timespec = get_atimespec(&sbuf);
999 m_timespec = get_mtimespec(&sbuf);
1001 if (lp_dos_filetime_resolution(SNUM(conn))) {
1002 dos_filetime_timespec(&c_timespec);
1003 dos_filetime_timespec(&a_timespec);
1004 dos_filetime_timespec(&m_timespec);
1007 put_long_date_timespec(p, c_timespec); /* create time. */
1009 put_long_date_timespec(p, a_timespec); /* access time */
1011 put_long_date_timespec(p, m_timespec); /* write time */
1013 put_long_date_timespec(p, m_timespec); /* change time */
1015 SIVAL(p,0,fattr); /* File Attributes. */
1017 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1019 SOFF_T(p,0,file_len);
1021 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1025 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1027 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1030 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1031 perms = FILE_GENERIC_ALL;
1033 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1038 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
1041 END_PROFILE(SMBntcreateX);
1045 /****************************************************************************
1046 Reply to a NT_TRANSACT_CREATE call to open a pipe.
1047 ****************************************************************************/
1049 static void do_nt_transact_create_pipe(connection_struct *conn,
1050 struct smb_request *req,
1051 uint16 **ppsetup, uint32 setup_count,
1052 char **ppparams, uint32 parameter_count,
1053 char **ppdata, uint32 data_count)
1056 char *params = *ppparams;
1062 TALLOC_CTX *ctx = talloc_tos();
1065 * Ensure minimum number of parameters sent.
1068 if(parameter_count < 54) {
1069 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1070 reply_doserror(req, ERRDOS, ERRnoaccess);
1074 flags = IVAL(params,0);
1076 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
1077 parameter_count-53, STR_TERMINATE,
1079 if (!NT_STATUS_IS_OK(status)) {
1080 reply_nterror(req, status);
1084 nt_open_pipe(fname, conn, req, &pnum);
1091 /* Realloc the size of parameters and data we will return */
1092 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1093 /* Extended response is 32 more byyes. */
1098 params = nttrans_realloc(ppparams, param_len);
1099 if(params == NULL) {
1100 reply_doserror(req, ERRDOS, ERRnomem);
1105 SCVAL(p,0,NO_OPLOCK_RETURN);
1110 SIVAL(p,0,FILE_WAS_OPENED);
1114 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1117 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1119 SSVAL(p,2, 0x5FF); /* ? */
1122 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1124 SIVAL(p,0,FILE_GENERIC_ALL);
1126 * For pipes W2K3 seems to return
1128 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
1130 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
1133 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1135 /* Send the required number of replies */
1136 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1141 /****************************************************************************
1142 Internal fn to set security descriptors.
1143 ****************************************************************************/
1145 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1148 SEC_DESC *psd = NULL;
1149 TALLOC_CTX *mem_ctx;
1152 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
1153 return NT_STATUS_OK;
1157 * Init the parse struct we will unmarshall from.
1160 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1161 DEBUG(0,("set_sd: talloc_init failed.\n"));
1162 return NT_STATUS_NO_MEMORY;
1165 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1168 * Setup the prs_struct to point at the memory we just
1172 prs_give_memory( &pd, data, sd_len, False);
1175 * Finally, unmarshall from the data buffer.
1178 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1179 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1181 * Return access denied for want of a better error message..
1183 talloc_destroy(mem_ctx);
1184 return NT_STATUS_NO_MEMORY;
1187 if (psd->owner_sid==0) {
1188 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1190 if (psd->group_sid==0) {
1191 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1194 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1197 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1200 status = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1202 talloc_destroy(mem_ctx);
1206 /****************************************************************************
1207 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1208 ****************************************************************************/
1210 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1212 struct ea_list *ea_list_head = NULL;
1215 if (data_size < 4) {
1219 while (offset + 4 <= data_size) {
1220 size_t next_offset = IVAL(pdata,offset);
1221 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1227 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1228 if (next_offset == 0) {
1231 offset += next_offset;
1234 return ea_list_head;
1237 /****************************************************************************
1238 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1239 ****************************************************************************/
1241 static void call_nt_transact_create(connection_struct *conn,
1242 struct smb_request *req,
1243 uint16 **ppsetup, uint32 setup_count,
1244 char **ppparams, uint32 parameter_count,
1245 char **ppdata, uint32 data_count,
1246 uint32 max_data_count)
1249 char *params = *ppparams;
1250 char *data = *ppdata;
1251 /* Breakout the oplock request bits so we can set the reply bits separately. */
1252 int oplock_request = 0;
1254 SMB_OFF_T file_len = 0;
1255 SMB_STRUCT_STAT sbuf;
1257 files_struct *fsp = NULL;
1259 bool extended_oplock_granted = False;
1262 uint32 file_attributes;
1263 uint32 share_access;
1264 uint32 create_disposition;
1265 uint32 create_options;
1268 uint16 root_dir_fid;
1269 struct timespec c_timespec;
1270 struct timespec a_timespec;
1271 struct timespec m_timespec;
1272 struct ea_list *ea_list = NULL;
1275 struct case_semantics_state *case_state = NULL;
1276 SMB_BIG_UINT allocation_size;
1277 TALLOC_CTX *ctx = talloc_tos();
1279 DEBUG(5,("call_nt_transact_create\n"));
1282 * If it's an IPC, use the pipe handler.
1286 if (lp_nt_pipe_support()) {
1287 do_nt_transact_create_pipe(
1289 ppsetup, setup_count,
1290 ppparams, parameter_count,
1291 ppdata, data_count);
1294 reply_doserror(req, ERRDOS, ERRnoaccess);
1300 * Ensure minimum number of parameters sent.
1303 if(parameter_count < 54) {
1304 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1305 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1309 flags = IVAL(params,0);
1310 access_mask = IVAL(params,8);
1311 file_attributes = IVAL(params,20);
1312 share_access = IVAL(params,24);
1313 create_disposition = IVAL(params,28);
1314 create_options = IVAL(params,32);
1315 sd_len = IVAL(params,36);
1316 ea_len = IVAL(params,40);
1317 root_dir_fid = (uint16)IVAL(params,4);
1318 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1319 #ifdef LARGE_SMB_OFF_T
1320 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1323 /* Ensure the data_len is correct for the sd and ea values given. */
1324 if ((ea_len + sd_len > data_count)
1325 || (ea_len > data_count) || (sd_len > data_count)
1326 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1327 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1328 "%u, data_count = %u\n", (unsigned int)ea_len,
1329 (unsigned int)sd_len, (unsigned int)data_count));
1330 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1335 if (!lp_ea_support(SNUM(conn))) {
1336 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1337 "EA's not supported.\n",
1338 (unsigned int)ea_len));
1339 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1344 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1345 "too small (should be more than 10)\n",
1346 (unsigned int)ea_len ));
1347 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1351 /* We have already checked that ea_len <= data_count here. */
1352 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1354 if (ea_list == NULL) {
1355 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1360 srvstr_get_path(ctx, params, req->flags2, &fname,
1361 params+53, parameter_count-53,
1362 STR_TERMINATE, &status);
1363 if (!NT_STATUS_IS_OK(status)) {
1364 reply_nterror(req, status);
1368 if (create_options & FILE_OPEN_BY_FILE_ID) {
1369 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1374 * Get the file name.
1377 if(root_dir_fid != 0) {
1379 * This filename is relative to a directory fid.
1381 char *parent_fname = NULL;
1383 files_struct *dir_fsp = file_fsp(root_dir_fid);
1386 reply_doserror(req, ERRDOS, ERRbadfid);
1390 if(!dir_fsp->is_directory) {
1393 * Check to see if this is a mac fork of some kind.
1396 if( is_ntfs_stream_name(fname)) {
1398 NT_STATUS_OBJECT_PATH_NOT_FOUND);
1403 we need to handle the case when we get a
1404 relative open relative to a file and the
1405 pathname is blank - this is a reopen!
1406 (hint from demyn plantenberg)
1409 reply_doserror(req, ERRDOS, ERRbadfid);
1413 if (ISDOT(dir_fsp->fsp_name)) {
1415 * We're at the toplevel dir, the final file name
1416 * must not contain ./, as this is filtered out
1417 * normally by srvstr_get_path and unix_convert
1418 * explicitly rejects paths containing ./.
1420 parent_fname = talloc_strdup(ctx,"");
1421 if (!parent_fname) {
1422 reply_nterror(req, NT_STATUS_NO_MEMORY);
1426 size_t dir_name_len = strlen(dir_fsp->fsp_name);
1429 * Copy in the base directory name.
1432 parent_fname = TALLOC_ARRAY(ctx, char, dir_name_len+2);
1434 reply_nterror(req, NT_STATUS_NO_MEMORY);
1437 memcpy(parent_fname, dir_fsp->fsp_name,
1441 * Ensure it ends in a '/'.
1442 * We used TALLOC_SIZE +2 to add space for the '/'.
1446 && (parent_fname[dir_name_len-1] != '\\')
1447 && (parent_fname[dir_name_len-1] != '/')) {
1448 parent_fname[dir_name_len] = '/';
1449 parent_fname[dir_name_len+1] = '\0';
1453 tmp = talloc_asprintf(ctx, "%s%s", parent_fname, fname);
1455 reply_nterror(req, NT_STATUS_NO_MEMORY);
1456 END_PROFILE(SMBntcreateX);
1463 * Check to see if this is a mac fork of some kind.
1466 if( is_ntfs_stream_name(fname)) {
1467 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
1472 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1473 if (oplock_request) {
1474 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1479 * Ordinary file or directory.
1482 status = resolve_dfspath(ctx, conn,
1483 req->flags2 & FLAGS2_DFS_PATHNAMES,
1486 if (!NT_STATUS_IS_OK(status)) {
1487 TALLOC_FREE(case_state);
1488 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1489 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1490 ERRSRV, ERRbadpath);
1493 reply_nterror(req, status);
1498 * Check if POSIX semantics are wanted.
1501 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1502 case_state = set_posix_case_semantics(NULL, conn);
1503 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1506 status = unix_convert(ctx, conn, fname, False, &fname, NULL, &sbuf);
1507 if (!NT_STATUS_IS_OK(status)) {
1508 TALLOC_FREE(case_state);
1509 reply_nterror(req, status);
1512 /* All file access must go through check_name() */
1513 status = check_name(conn, fname);
1514 if (!NT_STATUS_IS_OK(status)) {
1515 TALLOC_FREE(case_state);
1516 reply_nterror(req, status);
1520 /* This is the correct thing to do (check every time) but can_delete
1521 * is expensive (it may have to read the parent directory
1522 * permissions). So for now we're not doing it unless we have a strong
1523 * hint the client is really going to delete this file. If the client
1524 * is forcing FILE_CREATE let the filesystem take care of the
1527 /* Setting FILE_SHARE_DELETE is the hint. */
1529 if (lp_acl_check_permissions(SNUM(conn))
1530 && (create_disposition != FILE_CREATE)
1531 && (share_access & FILE_SHARE_DELETE)
1532 && (access_mask & DELETE_ACCESS)) {
1533 if (((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY)
1534 && !lp_delete_readonly(SNUM(conn))) ||
1535 !can_delete_file_in_directory(conn, fname)) {
1536 TALLOC_FREE(case_state);
1537 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1543 /* We need to support SeSecurityPrivilege for this. */
1544 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
1545 !user_has_privileges(current_user.nt_user_token,
1547 TALLOC_FREE(case_state);
1548 reply_nterror(req, NT_STATUS_PRIVILEGE_NOT_HELD);
1554 * If it's a request for a directory open, deal with it separately.
1557 if(create_options & FILE_DIRECTORY_FILE) {
1559 /* Can't open a temp directory. IFS kit test. */
1560 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1561 TALLOC_FREE(case_state);
1562 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1567 * We will get a create directory here if the Win32
1568 * app specified a security descriptor in the
1569 * CreateDirectory() call.
1573 status = open_directory(conn, req, fname, &sbuf,
1583 * Ordinary file case.
1586 status = open_file_ntcreate(conn,req,fname,&sbuf,
1595 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
1598 * Fail the open if it was explicitly a non-directory
1602 if (create_options & FILE_NON_DIRECTORY_FILE) {
1603 TALLOC_FREE(case_state);
1604 reply_force_nterror(
1606 NT_STATUS_FILE_IS_A_DIRECTORY);
1611 status = open_directory(conn, req, fname,
1622 TALLOC_FREE(case_state);
1624 if(!NT_STATUS_IS_OK(status)) {
1625 if (open_was_deferred(req->mid)) {
1626 /* We have re-scheduled this call. */
1629 reply_openerror(req, status);
1634 * According to the MS documentation, the only time the security
1635 * descriptor is applied to the opened file is iff we *created* the
1636 * file; an existing file stays the same.
1638 * Also, it seems (from observation) that you can open the file with
1639 * any access mask but you can still write the sd. We need to override
1640 * the granted access before we call set_sd
1641 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1644 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1645 uint32 saved_access_mask = fsp->access_mask;
1647 /* We have already checked that sd_len <= data_count here. */
1649 fsp->access_mask = FILE_GENERIC_ALL;
1651 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1652 if (!NT_STATUS_IS_OK(status)) {
1653 close_file(fsp,ERROR_CLOSE);
1654 TALLOC_FREE(case_state);
1655 reply_nterror(req, status);
1658 fsp->access_mask = saved_access_mask;
1661 if (ea_len && (info == FILE_WAS_CREATED)) {
1662 status = set_ea(conn, fsp, fname, ea_list);
1663 if (!NT_STATUS_IS_OK(status)) {
1664 close_file(fsp,ERROR_CLOSE);
1665 TALLOC_FREE(case_state);
1666 reply_nterror(req, status);
1671 file_len = sbuf.st_size;
1672 fattr = dos_mode(conn,fname,&sbuf);
1674 fattr = FILE_ATTRIBUTE_NORMAL;
1676 if (!fsp->is_directory && (fattr & aDIR)) {
1677 close_file(fsp,ERROR_CLOSE);
1678 reply_doserror(req, ERRDOS, ERRnoaccess);
1682 /* Save the requested allocation size. */
1683 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1684 if (allocation_size && (allocation_size > file_len)) {
1685 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1686 if (fsp->is_directory) {
1687 close_file(fsp,ERROR_CLOSE);
1688 /* Can't set allocation size on a directory. */
1689 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1692 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1693 close_file(fsp,ERROR_CLOSE);
1694 reply_nterror(req, NT_STATUS_DISK_FULL);
1698 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1703 * If the caller set the extended oplock request bit
1704 * and we granted one (by whatever means) - set the
1705 * correct bit for extended oplock reply.
1708 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1709 extended_oplock_granted = True;
1712 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1713 extended_oplock_granted = True;
1716 /* Realloc the size of parameters and data we will return */
1717 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1718 /* Extended response is 32 more byyes. */
1723 params = nttrans_realloc(ppparams, param_len);
1724 if(params == NULL) {
1725 reply_doserror(req, ERRDOS, ERRnomem);
1730 if (extended_oplock_granted) {
1731 if (flags & REQUEST_BATCH_OPLOCK) {
1732 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1734 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1736 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1737 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1739 SCVAL(p,0,NO_OPLOCK_RETURN);
1743 SSVAL(p,0,fsp->fnum);
1745 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1746 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1753 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1754 a_timespec = get_atimespec(&sbuf);
1755 m_timespec = get_mtimespec(&sbuf);
1757 if (lp_dos_filetime_resolution(SNUM(conn))) {
1758 dos_filetime_timespec(&c_timespec);
1759 dos_filetime_timespec(&a_timespec);
1760 dos_filetime_timespec(&m_timespec);
1763 put_long_date_timespec(p, c_timespec); /* create time. */
1765 put_long_date_timespec(p, a_timespec); /* access time */
1767 put_long_date_timespec(p, m_timespec); /* write time */
1769 put_long_date_timespec(p, m_timespec); /* change time */
1771 SIVAL(p,0,fattr); /* File Attributes. */
1773 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1775 SOFF_T(p,0,file_len);
1777 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1781 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1783 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1786 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1787 perms = FILE_GENERIC_ALL;
1789 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1794 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1796 /* Send the required number of replies */
1797 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1802 /****************************************************************************
1803 Reply to a NT CANCEL request.
1804 conn POINTER CAN BE NULL HERE !
1805 ****************************************************************************/
1807 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1810 * Go through and cancel any pending change notifies.
1813 START_PROFILE(SMBntcancel);
1814 remove_pending_change_notify_requests_by_mid(req->mid);
1815 remove_pending_lock_requests_by_mid(req->mid);
1816 srv_cancel_sign_response(req->mid);
1818 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1820 END_PROFILE(SMBntcancel);
1824 /****************************************************************************
1826 ****************************************************************************/
1828 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1829 connection_struct *conn,
1830 struct smb_request *req,
1831 const char *oldname_in,
1832 const char *newname_in,
1835 SMB_STRUCT_STAT sbuf1, sbuf2;
1836 char *oldname = NULL;
1837 char *newname = NULL;
1838 char *last_component_oldname = NULL;
1839 char *last_component_newname = NULL;
1840 files_struct *fsp1,*fsp2;
1844 NTSTATUS status = NT_STATUS_OK;
1849 if (!CAN_WRITE(conn)) {
1850 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1853 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1854 &last_component_oldname, &sbuf1);
1855 if (!NT_STATUS_IS_OK(status)) {
1859 status = check_name(conn, oldname);
1860 if (!NT_STATUS_IS_OK(status)) {
1864 /* Source must already exist. */
1865 if (!VALID_STAT(sbuf1)) {
1866 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1868 /* Ensure attributes match. */
1869 fattr = dos_mode(conn,oldname,&sbuf1);
1870 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1871 return NT_STATUS_NO_SUCH_FILE;
1874 status = unix_convert(ctx, conn, newname_in, False, &newname,
1875 &last_component_newname, &sbuf2);
1876 if (!NT_STATUS_IS_OK(status)) {
1880 status = check_name(conn, newname);
1881 if (!NT_STATUS_IS_OK(status)) {
1885 /* Disallow if newname already exists. */
1886 if (VALID_STAT(sbuf2)) {
1887 return NT_STATUS_OBJECT_NAME_COLLISION;
1890 /* No links from a directory. */
1891 if (S_ISDIR(sbuf1.st_mode)) {
1892 return NT_STATUS_FILE_IS_A_DIRECTORY;
1895 /* Ensure this is within the share. */
1896 status = check_reduced_name(conn, oldname);
1897 if (!NT_STATUS_IS_OK(status)) {
1901 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1904 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1905 FILE_READ_DATA, /* Read-only. */
1906 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1908 0, /* No create options. */
1909 FILE_ATTRIBUTE_NORMAL,
1913 if (!NT_STATUS_IS_OK(status)) {
1917 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1918 FILE_WRITE_DATA, /* Read-only. */
1919 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1921 0, /* No create options. */
1926 if (!NT_STATUS_IS_OK(status)) {
1927 close_file(fsp1,ERROR_CLOSE);
1931 if (sbuf1.st_size) {
1932 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1936 * As we are opening fsp1 read-only we only expect
1937 * an error on close on fsp2 if we are out of space.
1938 * Thus we don't look at the error return from the
1941 close_file(fsp1,NORMAL_CLOSE);
1943 /* Ensure the modtime is set correctly on the destination file. */
1944 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1946 status = close_file(fsp2,NORMAL_CLOSE);
1948 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1949 creates the file. This isn't the correct thing to do in the copy
1951 file_set_dosmode(conn, newname, fattr, &sbuf2,
1952 parent_dirname(newname),false);
1954 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1955 return NT_STATUS_DISK_FULL;
1958 if (!NT_STATUS_IS_OK(status)) {
1959 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1960 nt_errstr(status), oldname, newname));
1965 /****************************************************************************
1966 Reply to a NT rename request.
1967 ****************************************************************************/
1969 void reply_ntrename(connection_struct *conn, struct smb_request *req)
1971 char *oldname = NULL;
1972 char *newname = NULL;
1975 bool src_has_wcard = False;
1976 bool dest_has_wcard = False;
1979 TALLOC_CTX *ctx = talloc_tos();
1981 START_PROFILE(SMBntrename);
1984 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1985 END_PROFILE(SMBntrename);
1989 attrs = SVAL(req->inbuf,smb_vwv0);
1990 rename_type = SVAL(req->inbuf,smb_vwv1);
1992 p = smb_buf(req->inbuf) + 1;
1993 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1994 0, STR_TERMINATE, &status,
1996 if (!NT_STATUS_IS_OK(status)) {
1997 reply_nterror(req, status);
1998 END_PROFILE(SMBntrename);
2002 if( is_ntfs_stream_name(oldname)) {
2003 /* Can't rename a stream. */
2004 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2005 END_PROFILE(SMBntrename);
2009 if (ms_has_wild(oldname)) {
2010 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
2011 END_PROFILE(SMBntrename);
2016 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
2017 0, STR_TERMINATE, &status,
2019 if (!NT_STATUS_IS_OK(status)) {
2020 reply_nterror(req, status);
2021 END_PROFILE(SMBntrename);
2025 status = resolve_dfspath(ctx, conn,
2026 req->flags2 & FLAGS2_DFS_PATHNAMES,
2029 if (!NT_STATUS_IS_OK(status)) {
2030 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2031 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2032 ERRSRV, ERRbadpath);
2033 END_PROFILE(SMBntrename);
2036 reply_nterror(req, status);
2037 END_PROFILE(SMBntrename);
2041 status = resolve_dfspath(ctx, conn,
2042 req->flags2 & FLAGS2_DFS_PATHNAMES,
2045 if (!NT_STATUS_IS_OK(status)) {
2046 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2047 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2048 ERRSRV, ERRbadpath);
2049 END_PROFILE(SMBntrename);
2052 reply_nterror(req, status);
2053 END_PROFILE(SMBntrename);
2057 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
2059 switch(rename_type) {
2060 case RENAME_FLAG_RENAME:
2061 status = rename_internals(ctx, conn, req, oldname,
2062 newname, attrs, False, src_has_wcard,
2065 case RENAME_FLAG_HARD_LINK:
2066 if (src_has_wcard || dest_has_wcard) {
2068 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2070 status = hardlink_internals(ctx,
2076 case RENAME_FLAG_COPY:
2077 if (src_has_wcard || dest_has_wcard) {
2079 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2081 status = copy_internals(ctx, conn, req, oldname,
2085 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
2086 status = NT_STATUS_INVALID_PARAMETER;
2089 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
2093 if (!NT_STATUS_IS_OK(status)) {
2094 if (open_was_deferred(req->mid)) {
2095 /* We have re-scheduled this call. */
2096 END_PROFILE(SMBntrename);
2100 reply_nterror(req, status);
2101 END_PROFILE(SMBntrename);
2105 reply_outbuf(req, 0, 0);
2107 END_PROFILE(SMBntrename);
2111 /****************************************************************************
2112 Reply to a notify change - queue the request and
2113 don't allow a directory to be opened.
2114 ****************************************************************************/
2116 static void call_nt_transact_notify_change(connection_struct *conn,
2117 struct smb_request *req,
2121 uint32 parameter_count,
2122 char **ppdata, uint32 data_count,
2123 uint32 max_data_count,
2124 uint32 max_param_count)
2126 uint16 *setup = *ppsetup;
2132 if(setup_count < 6) {
2133 reply_doserror(req, ERRDOS, ERRbadfunc);
2137 fsp = file_fsp(SVAL(setup,4));
2138 filter = IVAL(setup, 0);
2139 recursive = (SVAL(setup, 6) != 0) ? True : False;
2141 DEBUG(3,("call_nt_transact_notify_change\n"));
2144 reply_doserror(req, ERRDOS, ERRbadfid);
2149 char *filter_string;
2151 if (!(filter_string = notify_filter_string(NULL, filter))) {
2152 reply_nterror(req,NT_STATUS_NO_MEMORY);
2156 DEBUG(3,("call_nt_transact_notify_change: notify change "
2157 "called on %s, filter = %s, recursive = %d\n",
2158 fsp->fsp_name, filter_string, recursive));
2160 TALLOC_FREE(filter_string);
2163 if((!fsp->is_directory) || (conn != fsp->conn)) {
2164 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2168 if (fsp->notify == NULL) {
2170 status = change_notify_create(fsp, filter, recursive);
2172 if (!NT_STATUS_IS_OK(status)) {
2173 DEBUG(10, ("change_notify_create returned %s\n",
2174 nt_errstr(status)));
2175 reply_nterror(req, status);
2180 if (fsp->notify->num_changes != 0) {
2183 * We've got changes pending, respond immediately
2187 * TODO: write a torture test to check the filtering behaviour
2191 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
2194 * change_notify_reply() above has independently sent its
2201 * No changes pending, queue the request
2204 status = change_notify_add_request(req->inbuf, max_param_count, filter,
2206 if (!NT_STATUS_IS_OK(status)) {
2207 reply_nterror(req, status);
2212 /****************************************************************************
2213 Reply to an NT transact rename command.
2214 ****************************************************************************/
2216 static void call_nt_transact_rename(connection_struct *conn,
2217 struct smb_request *req,
2218 uint16 **ppsetup, uint32 setup_count,
2219 char **ppparams, uint32 parameter_count,
2220 char **ppdata, uint32 data_count,
2221 uint32 max_data_count)
2223 char *params = *ppparams;
2224 char *new_name = NULL;
2225 files_struct *fsp = NULL;
2226 bool replace_if_exists = False;
2227 bool dest_has_wcard = False;
2229 TALLOC_CTX *ctx = talloc_tos();
2231 if(parameter_count < 5) {
2232 reply_doserror(req, ERRDOS, ERRbadfunc);
2236 fsp = file_fsp(SVAL(params, 0));
2237 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2238 if (!check_fsp(conn, req, fsp, ¤t_user)) {
2241 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
2242 parameter_count - 4,
2243 STR_TERMINATE, &status, &dest_has_wcard);
2244 if (!NT_STATUS_IS_OK(status)) {
2245 reply_nterror(req, status);
2249 status = rename_internals(ctx,
2259 if (!NT_STATUS_IS_OK(status)) {
2260 if (open_was_deferred(req->mid)) {
2261 /* We have re-scheduled this call. */
2264 reply_nterror(req, status);
2269 * Rename was successful.
2271 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2273 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2274 fsp->fsp_name, new_name));
2279 /******************************************************************************
2280 Fake up a completely empty SD.
2281 *******************************************************************************/
2283 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2287 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2289 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2290 return NT_STATUS_NO_MEMORY;
2293 return NT_STATUS_OK;
2296 /****************************************************************************
2297 Reply to query a security descriptor.
2298 ****************************************************************************/
2300 static void call_nt_transact_query_security_desc(connection_struct *conn,
2301 struct smb_request *req,
2305 uint32 parameter_count,
2308 uint32 max_data_count)
2310 char *params = *ppparams;
2311 char *data = *ppdata;
2313 SEC_DESC *psd = NULL;
2315 uint32 security_info_wanted;
2316 TALLOC_CTX *mem_ctx;
2317 files_struct *fsp = NULL;
2320 if(parameter_count < 8) {
2321 reply_doserror(req, ERRDOS, ERRbadfunc);
2325 fsp = file_fsp(SVAL(params,0));
2327 reply_doserror(req, ERRDOS, ERRbadfid);
2331 security_info_wanted = IVAL(params,4);
2333 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2334 (unsigned int)security_info_wanted ));
2336 params = nttrans_realloc(ppparams, 4);
2337 if(params == NULL) {
2338 reply_doserror(req, ERRDOS, ERRnomem);
2342 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2343 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2344 reply_doserror(req, ERRDOS, ERRnomem);
2349 * Get the permissions to return.
2352 if (!lp_nt_acl_support(SNUM(conn))) {
2353 status = get_null_nt_acl(mem_ctx, &psd);
2355 status = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd,
2356 security_info_wanted, &psd);
2359 if (!NT_STATUS_IS_OK(status)) {
2360 talloc_destroy(mem_ctx);
2361 reply_nterror(req, status);
2365 sd_size = sec_desc_size(psd);
2367 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2369 SIVAL(params,0,(uint32)sd_size);
2371 if(max_data_count < sd_size) {
2373 send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
2374 params, 4, *ppdata, 0);
2375 talloc_destroy(mem_ctx);
2380 * Allocate the data we will point this at.
2383 data = nttrans_realloc(ppdata, sd_size);
2385 talloc_destroy(mem_ctx);
2386 reply_doserror(req, ERRDOS, ERRnomem);
2391 * Init the parse struct we will marshall into.
2394 prs_init(&pd, 0, mem_ctx, MARSHALL);
2397 * Setup the prs_struct to point at the memory we just
2401 prs_give_memory( &pd, data, (uint32)sd_size, False);
2404 * Finally, linearize into the outgoing buffer.
2407 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2408 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2409 security descriptor.\n"));
2411 * Return access denied for want of a better error message..
2413 talloc_destroy(mem_ctx);
2414 reply_unixerror(req, ERRDOS, ERRnoaccess);
2419 * Now we can delete the security descriptor.
2422 talloc_destroy(mem_ctx);
2424 send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2428 /****************************************************************************
2429 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2430 ****************************************************************************/
2432 static void call_nt_transact_set_security_desc(connection_struct *conn,
2433 struct smb_request *req,
2437 uint32 parameter_count,
2440 uint32 max_data_count)
2442 char *params= *ppparams;
2443 char *data = *ppdata;
2444 files_struct *fsp = NULL;
2445 uint32 security_info_sent = 0;
2448 if(parameter_count < 8) {
2449 reply_doserror(req, ERRDOS, ERRbadfunc);
2453 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
2454 reply_doserror(req, ERRDOS, ERRbadfid);
2458 if(!lp_nt_acl_support(SNUM(conn))) {
2462 security_info_sent = IVAL(params,4);
2464 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2465 (unsigned int)security_info_sent ));
2467 if (data_count == 0) {
2468 reply_doserror(req, ERRDOS, ERRnoaccess);
2472 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2473 reply_nterror(req, nt_status);
2479 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2483 /****************************************************************************
2485 ****************************************************************************/
2487 static void call_nt_transact_ioctl(connection_struct *conn,
2488 struct smb_request *req,
2489 uint16 **ppsetup, uint32 setup_count,
2490 char **ppparams, uint32 parameter_count,
2491 char **ppdata, uint32 data_count,
2492 uint32 max_data_count)
2499 static bool logged_message;
2500 char *pdata = *ppdata;
2502 if (setup_count != 8) {
2503 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2504 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2508 function = IVAL(*ppsetup, 0);
2509 fidnum = SVAL(*ppsetup, 4);
2510 isFSctl = CVAL(*ppsetup, 6);
2511 compfilter = CVAL(*ppsetup, 7);
2513 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2514 function, fidnum, isFSctl, compfilter));
2516 fsp=file_fsp(fidnum);
2517 /* this check is done in each implemented function case for now
2518 because I don't want to break anything... --metze
2519 FSP_BELONGS_CONN(fsp,conn);*/
2522 case FSCTL_SET_SPARSE:
2523 /* pretend this succeeded - tho strictly we should
2524 mark the file sparse (if the local fs supports it)
2525 so we can know if we need to pre-allocate or not */
2527 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2528 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2531 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2533 unsigned char objid[16];
2535 /* This should return the object-id on this file.
2536 * I think I'll make this be the inode+dev. JRA.
2539 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2541 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2546 pdata = nttrans_realloc(ppdata, data_count);
2547 if (pdata == NULL) {
2548 reply_nterror(req, NT_STATUS_NO_MEMORY);
2551 push_file_id_16(pdata, &fsp->file_id);
2552 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2553 push_file_id_16(pdata+32, &fsp->file_id);
2554 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2559 case FSCTL_GET_REPARSE_POINT:
2560 /* pretend this fail - my winXP does it like this
2564 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2565 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2568 case FSCTL_SET_REPARSE_POINT:
2569 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2573 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2574 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2577 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2580 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2581 * and return their volume names. If max_data_count is 16, then it is just
2582 * asking for the number of volumes and length of the combined names.
2584 * pdata is the data allocated by our caller, but that uses
2585 * total_data_count (which is 0 in our case) rather than max_data_count.
2586 * Allocate the correct amount and return the pointer to let
2587 * it be deallocated when we return.
2589 SHADOW_COPY_DATA *shadow_data = NULL;
2590 TALLOC_CTX *shadow_mem_ctx = NULL;
2591 bool labels = False;
2592 uint32 labels_data_count = 0;
2596 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2600 if (max_data_count < 16) {
2601 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2603 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2607 if (max_data_count > 16) {
2611 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2612 if (shadow_mem_ctx == NULL) {
2613 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2614 reply_nterror(req, NT_STATUS_NO_MEMORY);
2618 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2619 if (shadow_data == NULL) {
2620 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2621 talloc_destroy(shadow_mem_ctx);
2622 reply_nterror(req, NT_STATUS_NO_MEMORY);
2626 shadow_data->mem_ctx = shadow_mem_ctx;
2629 * Call the VFS routine to actually do the work.
2631 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2632 talloc_destroy(shadow_data->mem_ctx);
2633 if (errno == ENOSYS) {
2634 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2635 conn->connectpath));
2636 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2639 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2640 conn->connectpath));
2641 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2646 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2651 data_count = 12+labels_data_count+4;
2654 if (max_data_count<data_count) {
2655 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2656 max_data_count,data_count));
2657 talloc_destroy(shadow_data->mem_ctx);
2658 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2662 pdata = nttrans_realloc(ppdata, data_count);
2663 if (pdata == NULL) {
2664 talloc_destroy(shadow_data->mem_ctx);
2665 reply_nterror(req, NT_STATUS_NO_MEMORY);
2671 /* num_volumes 4 bytes */
2672 SIVAL(pdata,0,shadow_data->num_volumes);
2675 /* num_labels 4 bytes */
2676 SIVAL(pdata,4,shadow_data->num_volumes);
2679 /* needed_data_count 4 bytes */
2680 SIVAL(pdata,8,labels_data_count);
2684 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2685 shadow_data->num_volumes,fsp->fsp_name));
2686 if (labels && shadow_data->labels) {
2687 for (i=0;i<shadow_data->num_volumes;i++) {
2688 srvstr_push(pdata, req->flags2,
2689 cur_pdata, shadow_data->labels[i],
2690 2*sizeof(SHADOW_COPY_LABEL),
2691 STR_UNICODE|STR_TERMINATE);
2692 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2693 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2697 talloc_destroy(shadow_data->mem_ctx);
2699 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2705 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2707 /* pretend this succeeded -
2709 * we have to send back a list with all files owned by this SID
2711 * but I have to check that --metze
2715 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2717 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2719 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2723 /* unknown 4 bytes: this is not the length of the sid :-( */
2724 /*unknown = IVAL(pdata,0);*/
2726 sid_parse(pdata+4,sid_len,&sid);
2727 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2729 if (!sid_to_uid(&sid, &uid)) {
2730 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2731 sid_string_static(&sid),(unsigned long)sid_len));
2735 /* we can take a look at the find source :-)
2737 * find ./ -uid $uid -name '*' is what we need here
2740 * and send 4bytes len and then NULL terminated unicode strings
2743 * but I don't know how to deal with the paged results
2744 * (maybe we can hang the result anywhere in the fsp struct)
2746 * we don't send all files at once
2747 * and at the next we should *not* start from the beginning,
2748 * so we have to cache the result
2753 /* this works for now... */
2754 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2758 if (!logged_message) {
2759 logged_message = True; /* Only print this once... */
2760 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2765 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2769 #ifdef HAVE_SYS_QUOTAS
2770 /****************************************************************************
2771 Reply to get user quota
2772 ****************************************************************************/
2774 static void call_nt_transact_get_user_quota(connection_struct *conn,
2775 struct smb_request *req,
2779 uint32 parameter_count,
2782 uint32 max_data_count)
2784 NTSTATUS nt_status = NT_STATUS_OK;
2785 char *params = *ppparams;
2786 char *pdata = *ppdata;
2788 int data_len=0,param_len=0;
2791 files_struct *fsp = NULL;
2795 bool start_enum = True;
2796 SMB_NTQUOTA_STRUCT qt;
2797 SMB_NTQUOTA_LIST *tmp_list;
2798 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2803 if (current_user.ut.uid != 0) {
2804 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2805 lp_servicename(SNUM(conn)),conn->user));
2806 reply_doserror(req, ERRDOS, ERRnoaccess);
2811 * Ensure minimum number of parameters sent.
2814 if (parameter_count < 4) {
2815 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2816 reply_doserror(req, ERRDOS, ERRinvalidparam);
2820 /* maybe we can check the quota_fnum */
2821 fsp = file_fsp(SVAL(params,0));
2822 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2823 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2824 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2828 /* the NULL pointer checking for fsp->fake_file_handle->pd
2829 * is done by CHECK_NTQUOTA_HANDLE_OK()
2831 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2833 level = SVAL(params,2);
2835 /* unknown 12 bytes leading in params */
2838 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2839 /* seems that we should continue with the enum here --metze */
2841 if (qt_handle->quota_list!=NULL &&
2842 qt_handle->tmp_list==NULL) {
2845 free_ntquota_list(&(qt_handle->quota_list));
2847 /* Realloc the size of parameters and data we will return */
2849 params = nttrans_realloc(ppparams, param_len);
2850 if(params == NULL) {
2851 reply_doserror(req, ERRDOS, ERRnomem);
2856 SIVAL(params,0,data_len);
2863 case TRANSACT_GET_USER_QUOTA_LIST_START:
2865 if (qt_handle->quota_list==NULL &&
2866 qt_handle->tmp_list==NULL) {
2870 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2871 reply_doserror(req, ERRSRV, ERRerror);
2875 /* Realloc the size of parameters and data we will return */
2877 params = nttrans_realloc(ppparams, param_len);
2878 if(params == NULL) {
2879 reply_doserror(req, ERRDOS, ERRnomem);
2883 /* we should not trust the value in max_data_count*/
2884 max_data_count = MIN(max_data_count,2048);
2886 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2888 reply_doserror(req, ERRDOS, ERRnomem);
2894 /* set params Size of returned Quota Data 4 bytes*/
2895 /* but set it later when we know it */
2897 /* for each entry push the data */
2900 qt_handle->tmp_list = qt_handle->quota_list;
2903 tmp_list = qt_handle->tmp_list;
2905 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2906 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2908 sid_len = sid_size(&tmp_list->quotas->sid);
2909 entry_len = 40 + sid_len;
2911 /* nextoffset entry 4 bytes */
2912 SIVAL(entry,0,entry_len);
2914 /* then the len of the SID 4 bytes */
2915 SIVAL(entry,4,sid_len);
2917 /* unknown data 8 bytes SMB_BIG_UINT */
2918 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2920 /* the used disk space 8 bytes SMB_BIG_UINT */
2921 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2923 /* the soft quotas 8 bytes SMB_BIG_UINT */
2924 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2926 /* the hard quotas 8 bytes SMB_BIG_UINT */
2927 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2929 /* and now the SID */
2930 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2933 qt_handle->tmp_list = tmp_list;
2935 /* overwrite the offset of the last entry */
2936 SIVAL(entry-entry_len,0,0);
2938 data_len = 4+qt_len;
2939 /* overwrite the params quota_data_len */
2940 SIVAL(params,0,data_len);
2944 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2946 /* unknown 4 bytes IVAL(pdata,0) */
2948 if (data_count < 8) {
2949 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2950 reply_doserror(req, ERRDOS, ERRunknownlevel);
2954 sid_len = IVAL(pdata,4);
2955 /* Ensure this is less than 1mb. */
2956 if (sid_len > (1024*1024)) {
2957 reply_doserror(req, ERRDOS, ERRnomem);
2961 if (data_count < 8+sid_len) {
2962 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2963 reply_doserror(req, ERRDOS, ERRunknownlevel);
2967 data_len = 4+40+sid_len;
2969 if (max_data_count < data_len) {
2970 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2971 max_data_count, data_len));
2973 SIVAL(params,0,data_len);
2975 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2979 sid_parse(pdata+8,sid_len,&sid);
2981 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2984 * we have to return zero's in all fields
2985 * instead of returning an error here
2990 /* Realloc the size of parameters and data we will return */
2992 params = nttrans_realloc(ppparams, param_len);
2993 if(params == NULL) {
2994 reply_doserror(req, ERRDOS, ERRnomem);
2998 pdata = nttrans_realloc(ppdata, data_len);
3000 reply_doserror(req, ERRDOS, ERRnomem);
3006 /* set params Size of returned Quota Data 4 bytes*/
3007 SIVAL(params,0,data_len);
3009 /* nextoffset entry 4 bytes */
3012 /* then the len of the SID 4 bytes */
3013 SIVAL(entry,4,sid_len);
3015 /* unknown data 8 bytes SMB_BIG_UINT */
3016 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
3018 /* the used disk space 8 bytes SMB_BIG_UINT */
3019 SBIG_UINT(entry,16,qt.usedspace);
3021 /* the soft quotas 8 bytes SMB_BIG_UINT */
3022 SBIG_UINT(entry,24,qt.softlim);
3024 /* the hard quotas 8 bytes SMB_BIG_UINT */
3025 SBIG_UINT(entry,32,qt.hardlim);
3027 /* and now the SID */
3028 sid_linearize(entry+40, sid_len, &sid);
3033 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
3034 reply_doserror(req, ERRSRV, ERRerror);
3039 send_nt_replies(req, nt_status, params, param_len,
3043 /****************************************************************************
3044 Reply to set user quota
3045 ****************************************************************************/
3047 static void call_nt_transact_set_user_quota(connection_struct *conn,
3048 struct smb_request *req,
3052 uint32 parameter_count,
3055 uint32 max_data_count)
3057 char *params = *ppparams;
3058 char *pdata = *ppdata;
3059 int data_len=0,param_len=0;
3060 SMB_NTQUOTA_STRUCT qt;
3063 files_struct *fsp = NULL;
3068 if (current_user.ut.uid != 0) {
3069 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
3070 lp_servicename(SNUM(conn)),conn->user));
3071 reply_doserror(req, ERRDOS, ERRnoaccess);
3076 * Ensure minimum number of parameters sent.
3079 if (parameter_count < 2) {
3080 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
3081 reply_doserror(req, ERRDOS, ERRinvalidparam);
3085 /* maybe we can check the quota_fnum */
3086 fsp = file_fsp(SVAL(params,0));
3087 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
3088 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
3089 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
3093 if (data_count < 40) {
3094 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
3095 reply_doserror(req, ERRDOS, ERRunknownlevel);
3099 /* offset to next quota record.
3100 * 4 bytes IVAL(pdata,0)
3105 sid_len = IVAL(pdata,4);
3107 if (data_count < 40+sid_len) {
3108 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
3109 reply_doserror(req, ERRDOS, ERRunknownlevel);
3113 /* unknown 8 bytes in pdata
3114 * maybe its the change time in NTTIME
3117 /* the used space 8 bytes (SMB_BIG_UINT)*/
3118 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
3119 #ifdef LARGE_SMB_OFF_T
3120 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
3121 #else /* LARGE_SMB_OFF_T */
3122 if ((IVAL(pdata,20) != 0)&&
3123 ((qt.usedspace != 0xFFFFFFFF)||
3124 (IVAL(pdata,20)!=0xFFFFFFFF))) {
3125 /* more than 32 bits? */
3126 reply_doserror(req, ERRDOS, ERRunknownlevel);
3129 #endif /* LARGE_SMB_OFF_T */
3131 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
3132 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
3133 #ifdef LARGE_SMB_OFF_T
3134 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
3135 #else /* LARGE_SMB_OFF_T */
3136 if ((IVAL(pdata,28) != 0)&&
3137 ((qt.softlim != 0xFFFFFFFF)||
3138 (IVAL(pdata,28)!=0xFFFFFFFF))) {
3139 /* more than 32 bits? */
3140 reply_doserror(req, ERRDOS, ERRunknownlevel);
3143 #endif /* LARGE_SMB_OFF_T */
3145 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
3146 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
3147 #ifdef LARGE_SMB_OFF_T
3148 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
3149 #else /* LARGE_SMB_OFF_T */
3150 if ((IVAL(pdata,36) != 0)&&
3151 ((qt.hardlim != 0xFFFFFFFF)||
3152 (IVAL(pdata,36)!=0xFFFFFFFF))) {
3153 /* more than 32 bits? */
3154 reply_doserror(req, ERRDOS, ERRunknownlevel);
3157 #endif /* LARGE_SMB_OFF_T */
3159 sid_parse(pdata+40,sid_len,&sid);
3160 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
3162 /* 44 unknown bytes left... */
3164 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
3165 reply_doserror(req, ERRSRV, ERRerror);
3169 send_nt_replies(req, NT_STATUS_OK, params, param_len,
3172 #endif /* HAVE_SYS_QUOTAS */
3174 static void handle_nttrans(connection_struct *conn,
3175 struct trans_state *state,
3176 struct smb_request *req)
3178 if (Protocol >= PROTOCOL_NT1) {
3179 req->flags2 |= 0x40; /* IS_LONG_NAME */
3180 SSVAL(req->inbuf,smb_flg2,req->flags2);
3183 /* Now we must call the relevant NT_TRANS function */
3184 switch(state->call) {
3185 case NT_TRANSACT_CREATE:
3187 START_PROFILE(NT_transact_create);
3188 call_nt_transact_create(
3190 &state->setup, state->setup_count,
3191 &state->param, state->total_param,
3192 &state->data, state->total_data,
3193 state->max_data_return);
3194 END_PROFILE(NT_transact_create);
3198 case NT_TRANSACT_IOCTL:
3200 START_PROFILE(NT_transact_ioctl);
3201 call_nt_transact_ioctl(
3203 &state->setup, state->setup_count,
3204 &state->param, state->total_param,
3205 &state->data, state->total_data,
3206 state->max_data_return);
3207 END_PROFILE(NT_transact_ioctl);
3211 case NT_TRANSACT_SET_SECURITY_DESC:
3213 START_PROFILE(NT_transact_set_security_desc);
3214 call_nt_transact_set_security_desc(
3216 &state->setup, state->setup_count,
3217 &state->param, state->total_param,
3218 &state->data, state->total_data,
3219 state->max_data_return);
3220 END_PROFILE(NT_transact_set_security_desc);
3224 case NT_TRANSACT_NOTIFY_CHANGE:
3226 START_PROFILE(NT_transact_notify_change);
3227 call_nt_transact_notify_change(
3229 &state->setup, state->setup_count,
3230 &state->param, state->total_param,
3231 &state->data, state->total_data,
3232 state->max_data_return,
3233 state->max_param_return);
3234 END_PROFILE(NT_transact_notify_change);
3238 case NT_TRANSACT_RENAME:
3240 START_PROFILE(NT_transact_rename);
3241 call_nt_transact_rename(
3243 &state->setup, state->setup_count,
3244 &state->param, state->total_param,
3245 &state->data, state->total_data,
3246 state->max_data_return);
3247 END_PROFILE(NT_transact_rename);
3251 case NT_TRANSACT_QUERY_SECURITY_DESC:
3253 START_PROFILE(NT_transact_query_security_desc);
3254 call_nt_transact_query_security_desc(
3256 &state->setup, state->setup_count,
3257 &state->param, state->total_param,
3258 &state->data, state->total_data,
3259 state->max_data_return);
3260 END_PROFILE(NT_transact_query_security_desc);
3264 #ifdef HAVE_SYS_QUOTAS
3265 case NT_TRANSACT_GET_USER_QUOTA:
3267 START_PROFILE(NT_transact_get_user_quota);
3268 call_nt_transact_get_user_quota(
3270 &state->setup, state->setup_count,
3271 &state->param, state->total_param,
3272 &state->data, state->total_data,
3273 state->max_data_return);
3274 END_PROFILE(NT_transact_get_user_quota);
3278 case NT_TRANSACT_SET_USER_QUOTA:
3280 START_PROFILE(NT_transact_set_user_quota);
3281 call_nt_transact_set_user_quota(
3283 &state->setup, state->setup_count,
3284 &state->param, state->total_param,
3285 &state->data, state->total_data,
3286 state->max_data_return);
3287 END_PROFILE(NT_transact_set_user_quota);
3290 #endif /* HAVE_SYS_QUOTAS */
3293 /* Error in request */
3294 DEBUG(0,("handle_nttrans: Unknown request %d in "
3295 "nttrans call\n", state->call));
3296 reply_doserror(req, ERRSRV, ERRerror);
3302 /****************************************************************************
3303 Reply to a SMBNTtrans.
3304 ****************************************************************************/
3306 void reply_nttrans(connection_struct *conn, struct smb_request *req)
3312 uint16 function_code;
3314 struct trans_state *state;
3317 START_PROFILE(SMBnttrans);
3319 if (req->wct < 19) {
3320 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3321 END_PROFILE(SMBnttrans);
3325 size = smb_len(req->inbuf) + 4;
3326 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
3327 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
3328 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
3329 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
3330 function_code = SVAL(req->inbuf, smb_nt_Function);
3332 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3333 reply_doserror(req, ERRSRV, ERRaccess);
3334 END_PROFILE(SMBnttrans);
3338 result = allow_new_trans(conn->pending_trans, req->mid);
3339 if (!NT_STATUS_IS_OK(result)) {
3340 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3341 reply_nterror(req, result);
3342 END_PROFILE(SMBnttrans);
3346 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3347 reply_doserror(req, ERRSRV, ERRaccess);
3348 END_PROFILE(SMBnttrans);
3352 state->cmd = SMBnttrans;
3354 state->mid = req->mid;
3355 state->vuid = req->vuid;
3356 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
3358 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
3359 state->param = NULL;
3360 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
3361 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
3363 /* setup count is in *words* */
3364 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
3365 state->setup = NULL;
3366 state->call = function_code;
3369 * All nttrans messages we handle have smb_wct == 19 +
3370 * state->setup_count. Ensure this is so as a sanity check.
3373 if(req->wct != 19 + (state->setup_count/2)) {
3374 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3375 req->wct, 19 + (state->setup_count/2)));
3379 /* Don't allow more than 128mb for each value. */
3380 if ((state->total_data > (1024*1024*128)) ||
3381 (state->total_param > (1024*1024*128))) {
3382 reply_doserror(req, ERRDOS, ERRnomem);
3383 END_PROFILE(SMBnttrans);
3387 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3390 if (state->total_data) {
3391 /* Can't use talloc here, the core routines do realloc on the
3392 * params and data. */
3393 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3394 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3395 "bytes !\n", (unsigned int)state->total_data));
3397 reply_doserror(req, ERRDOS, ERRnomem);
3398 END_PROFILE(SMBnttrans);
3401 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3403 if ((smb_base(req->inbuf)+dsoff+dscnt
3404 > (char *)req->inbuf + size) ||
3405 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
3408 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3411 if (state->total_param) {
3412 /* Can't use talloc here, the core routines do realloc on the
3413 * params and data. */
3414 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3415 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3416 "bytes !\n", (unsigned int)state->total_param));
3417 SAFE_FREE(state->data);
3419 reply_doserror(req, ERRDOS, ERRnomem);
3420 END_PROFILE(SMBnttrans);
3423 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3425 if ((smb_base(req->inbuf)+psoff+pscnt
3426 > (char *)req->inbuf + size) ||
3427 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
3430 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3433 state->received_data = dscnt;
3434 state->received_param = pscnt;
3436 if(state->setup_count > 0) {
3437 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3438 state->setup_count));
3439 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3440 if (state->setup == NULL) {
3441 DEBUG(0,("reply_nttrans : Out of memory\n"));
3442 SAFE_FREE(state->data);
3443 SAFE_FREE(state->param);
3445 reply_doserror(req, ERRDOS, ERRnomem);
3446 END_PROFILE(SMBnttrans);
3450 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3451 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3454 if (smb_nt_SetupStart + state->setup_count > size) {
3458 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
3459 state->setup_count);
3460 dump_data(10, (uint8 *)state->setup, state->setup_count);
3463 if ((state->received_data == state->total_data) &&
3464 (state->received_param == state->total_param)) {
3465 handle_nttrans(conn, state, req);
3466 SAFE_FREE(state->param);
3467 SAFE_FREE(state->data);
3469 END_PROFILE(SMBnttrans);
3473 DLIST_ADD(conn->pending_trans, state);
3475 /* We need to send an interim response then receive the rest
3476 of the parameter/data bytes */
3477 reply_outbuf(req, 0, 0);
3478 show_msg((char *)req->outbuf);
3479 END_PROFILE(SMBnttrans);
3484 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3485 SAFE_FREE(state->data);
3486 SAFE_FREE(state->param);
3488 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3489 END_PROFILE(SMBnttrans);
3493 /****************************************************************************
3494 Reply to a SMBnttranss
3495 ****************************************************************************/
3497 void reply_nttranss(connection_struct *conn, struct smb_request *req)
3499 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3500 struct trans_state *state;
3504 START_PROFILE(SMBnttranss);
3506 show_msg((char *)req->inbuf);
3508 if (req->wct < 18) {
3509 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3510 END_PROFILE(SMBnttranss);
3514 for (state = conn->pending_trans; state != NULL;
3515 state = state->next) {
3516 if (state->mid == req->mid) {
3521 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3522 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3523 END_PROFILE(SMBnttranss);
3527 /* Revise state->total_param and state->total_data in case they have
3528 changed downwards */
3529 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
3530 < state->total_param) {
3531 state->total_param = IVAL(req->inbuf,
3532 smb_nts_TotalParameterCount);
3534 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
3535 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
3538 size = smb_len(req->inbuf) + 4;
3540 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
3541 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
3542 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
3544 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
3545 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
3546 doff = IVAL(req->inbuf, smb_nts_DataOffset);
3548 state->received_param += pcnt;
3549 state->received_data += dcnt;
3551 if ((state->received_data > state->total_data) ||
3552 (state->received_param > state->total_param))
3556 if (pdisp+pcnt > state->total_param)
3558 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3560 if (pdisp > state->total_param)
3562 if ((smb_base(req->inbuf) + poff + pcnt
3563 > (char *)req->inbuf + size) ||
3564 (smb_base(req->inbuf) + poff + pcnt
3565 < smb_base(req->inbuf)))
3567 if (state->param + pdisp < state->param)
3570 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
3575 if (ddisp+dcnt > state->total_data)
3577 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3579 if (ddisp > state->total_data)
3581 if ((smb_base(req->inbuf) + doff + dcnt
3582 > (char *)req->inbuf + size) ||
3583 (smb_base(req->inbuf) + doff + dcnt
3584 < smb_base(req->inbuf)))
3586 if (state->data + ddisp < state->data)
3589 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
3593 if ((state->received_param < state->total_param) ||
3594 (state->received_data < state->total_data)) {
3595 END_PROFILE(SMBnttranss);
3600 * construct_reply_common will copy smb_com from inbuf to
3601 * outbuf. SMBnttranss is wrong here.
3603 SCVAL(req->inbuf,smb_com,SMBnttrans);
3605 handle_nttrans(conn, state, req);
3607 DLIST_REMOVE(conn->pending_trans, state);
3608 SAFE_FREE(state->data);
3609 SAFE_FREE(state->param);
3611 END_PROFILE(SMBnttranss);
3616 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3617 DLIST_REMOVE(conn->pending_trans, state);
3618 SAFE_FREE(state->data);
3619 SAFE_FREE(state->param);
3621 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3622 END_PROFILE(SMBnttranss);