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);
875 reply_force_nterror(req,
876 NT_STATUS_FILE_IS_A_DIRECTORY);
877 END_PROFILE(SMBntcreateX);
882 status = open_directory(conn, req, fname,
893 TALLOC_FREE(case_state);
895 if (!NT_STATUS_IS_OK(status)) {
896 if (open_was_deferred(req->mid)) {
897 /* We have re-scheduled this call. */
898 END_PROFILE(SMBntcreateX);
901 reply_openerror(req, status);
902 END_PROFILE(SMBntcreateX);
906 file_len = sbuf.st_size;
907 fattr = dos_mode(conn,fname,&sbuf);
909 fattr = FILE_ATTRIBUTE_NORMAL;
911 if (!fsp->is_directory && (fattr & aDIR)) {
912 close_file(fsp,ERROR_CLOSE);
913 reply_doserror(req, ERRDOS, ERRnoaccess);
914 END_PROFILE(SMBntcreateX);
918 /* Save the requested allocation size. */
919 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
920 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
921 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
922 if (fsp->is_directory) {
923 close_file(fsp,ERROR_CLOSE);
924 /* Can't set allocation size on a directory. */
925 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
926 END_PROFILE(SMBntcreateX);
929 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
930 close_file(fsp,ERROR_CLOSE);
931 reply_nterror(req, NT_STATUS_DISK_FULL);
932 END_PROFILE(SMBntcreateX);
936 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
941 * If the caller set the extended oplock request bit
942 * and we granted one (by whatever means) - set the
943 * correct bit for extended oplock reply.
946 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
947 extended_oplock_granted = True;
950 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
951 extended_oplock_granted = True;
954 if (flags & EXTENDED_RESPONSE_REQUIRED) {
955 /* This is very strange. We
956 * return 50 words, but only set
957 * the wcnt to 42 ? It's definately
958 * what happens on the wire....
960 reply_outbuf(req, 50, 0);
961 SCVAL(req->outbuf,smb_wct,42);
963 reply_outbuf(req, 34, 0);
966 p = (char *)req->outbuf + smb_vwv2;
969 * Currently as we don't support level II oplocks we just report
970 * exclusive & batch here.
973 if (extended_oplock_granted) {
974 if (flags & REQUEST_BATCH_OPLOCK) {
975 SCVAL(p,0, BATCH_OPLOCK_RETURN);
977 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
979 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
980 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
982 SCVAL(p,0,NO_OPLOCK_RETURN);
986 SSVAL(p,0,fsp->fnum);
988 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
989 SIVAL(p,0,FILE_WAS_SUPERSEDED);
996 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
997 a_timespec = get_atimespec(&sbuf);
998 m_timespec = get_mtimespec(&sbuf);
1000 if (lp_dos_filetime_resolution(SNUM(conn))) {
1001 dos_filetime_timespec(&c_timespec);
1002 dos_filetime_timespec(&a_timespec);
1003 dos_filetime_timespec(&m_timespec);
1006 put_long_date_timespec(p, c_timespec); /* create time. */
1008 put_long_date_timespec(p, a_timespec); /* access time */
1010 put_long_date_timespec(p, m_timespec); /* write time */
1012 put_long_date_timespec(p, m_timespec); /* change time */
1014 SIVAL(p,0,fattr); /* File Attributes. */
1016 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1018 SOFF_T(p,0,file_len);
1020 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1024 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1026 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1029 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1030 perms = FILE_GENERIC_ALL;
1032 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1037 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
1040 END_PROFILE(SMBntcreateX);
1044 /****************************************************************************
1045 Reply to a NT_TRANSACT_CREATE call to open a pipe.
1046 ****************************************************************************/
1048 static void do_nt_transact_create_pipe(connection_struct *conn,
1049 struct smb_request *req,
1050 uint16 **ppsetup, uint32 setup_count,
1051 char **ppparams, uint32 parameter_count,
1052 char **ppdata, uint32 data_count)
1055 char *params = *ppparams;
1061 TALLOC_CTX *ctx = talloc_tos();
1064 * Ensure minimum number of parameters sent.
1067 if(parameter_count < 54) {
1068 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1069 reply_doserror(req, ERRDOS, ERRnoaccess);
1073 flags = IVAL(params,0);
1075 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
1076 parameter_count-53, STR_TERMINATE,
1078 if (!NT_STATUS_IS_OK(status)) {
1079 reply_nterror(req, status);
1083 nt_open_pipe(fname, conn, req, &pnum);
1090 /* Realloc the size of parameters and data we will return */
1091 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1092 /* Extended response is 32 more byyes. */
1097 params = nttrans_realloc(ppparams, param_len);
1098 if(params == NULL) {
1099 reply_doserror(req, ERRDOS, ERRnomem);
1104 SCVAL(p,0,NO_OPLOCK_RETURN);
1109 SIVAL(p,0,FILE_WAS_OPENED);
1113 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1116 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1118 SSVAL(p,2, 0x5FF); /* ? */
1121 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1123 SIVAL(p,0,FILE_GENERIC_ALL);
1125 * For pipes W2K3 seems to return
1127 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
1129 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
1132 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1134 /* Send the required number of replies */
1135 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1140 /****************************************************************************
1141 Internal fn to set security descriptors.
1142 ****************************************************************************/
1144 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1147 SEC_DESC *psd = NULL;
1148 TALLOC_CTX *mem_ctx;
1151 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
1152 return NT_STATUS_OK;
1156 * Init the parse struct we will unmarshall from.
1159 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1160 DEBUG(0,("set_sd: talloc_init failed.\n"));
1161 return NT_STATUS_NO_MEMORY;
1164 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1167 * Setup the prs_struct to point at the memory we just
1171 prs_give_memory( &pd, data, sd_len, False);
1174 * Finally, unmarshall from the data buffer.
1177 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1178 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1180 * Return access denied for want of a better error message..
1182 talloc_destroy(mem_ctx);
1183 return NT_STATUS_NO_MEMORY;
1186 if (psd->owner_sid==0) {
1187 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1189 if (psd->group_sid==0) {
1190 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1193 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1196 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1199 status = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1201 talloc_destroy(mem_ctx);
1205 /****************************************************************************
1206 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1207 ****************************************************************************/
1209 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1211 struct ea_list *ea_list_head = NULL;
1214 if (data_size < 4) {
1218 while (offset + 4 <= data_size) {
1219 size_t next_offset = IVAL(pdata,offset);
1220 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1226 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1227 if (next_offset == 0) {
1230 offset += next_offset;
1233 return ea_list_head;
1236 /****************************************************************************
1237 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1238 ****************************************************************************/
1240 static void call_nt_transact_create(connection_struct *conn,
1241 struct smb_request *req,
1242 uint16 **ppsetup, uint32 setup_count,
1243 char **ppparams, uint32 parameter_count,
1244 char **ppdata, uint32 data_count,
1245 uint32 max_data_count)
1248 char *params = *ppparams;
1249 char *data = *ppdata;
1250 /* Breakout the oplock request bits so we can set the reply bits separately. */
1251 int oplock_request = 0;
1253 SMB_OFF_T file_len = 0;
1254 SMB_STRUCT_STAT sbuf;
1256 files_struct *fsp = NULL;
1258 bool extended_oplock_granted = False;
1261 uint32 file_attributes;
1262 uint32 share_access;
1263 uint32 create_disposition;
1264 uint32 create_options;
1267 uint16 root_dir_fid;
1268 struct timespec c_timespec;
1269 struct timespec a_timespec;
1270 struct timespec m_timespec;
1271 struct ea_list *ea_list = NULL;
1274 struct case_semantics_state *case_state = NULL;
1275 SMB_BIG_UINT allocation_size;
1276 TALLOC_CTX *ctx = talloc_tos();
1278 DEBUG(5,("call_nt_transact_create\n"));
1281 * If it's an IPC, use the pipe handler.
1285 if (lp_nt_pipe_support()) {
1286 do_nt_transact_create_pipe(
1288 ppsetup, setup_count,
1289 ppparams, parameter_count,
1290 ppdata, data_count);
1293 reply_doserror(req, ERRDOS, ERRnoaccess);
1299 * Ensure minimum number of parameters sent.
1302 if(parameter_count < 54) {
1303 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1304 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1308 flags = IVAL(params,0);
1309 access_mask = IVAL(params,8);
1310 file_attributes = IVAL(params,20);
1311 share_access = IVAL(params,24);
1312 create_disposition = IVAL(params,28);
1313 create_options = IVAL(params,32);
1314 sd_len = IVAL(params,36);
1315 ea_len = IVAL(params,40);
1316 root_dir_fid = (uint16)IVAL(params,4);
1317 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1318 #ifdef LARGE_SMB_OFF_T
1319 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1322 /* Ensure the data_len is correct for the sd and ea values given. */
1323 if ((ea_len + sd_len > data_count) ||
1324 (ea_len > data_count) || (sd_len > data_count) ||
1325 (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1326 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1327 (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1328 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1333 if (!lp_ea_support(SNUM(conn))) {
1334 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1335 (unsigned int)ea_len ));
1336 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1341 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1342 (unsigned int)ea_len ));
1343 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1347 /* We have already checked that ea_len <= data_count here. */
1348 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1350 if (ea_list == NULL) {
1351 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1356 srvstr_get_path(ctx, params, req->flags2, &fname,
1357 params+53, parameter_count-53,
1358 STR_TERMINATE, &status);
1359 if (!NT_STATUS_IS_OK(status)) {
1360 reply_nterror(req, status);
1364 if (create_options & FILE_OPEN_BY_FILE_ID) {
1365 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1370 * Get the file name.
1373 if(root_dir_fid != 0) {
1375 * This filename is relative to a directory fid.
1377 char *parent_fname = NULL;
1379 files_struct *dir_fsp = file_fsp(root_dir_fid);
1382 reply_doserror(req, ERRDOS, ERRbadfid);
1386 if(!dir_fsp->is_directory) {
1389 * Check to see if this is a mac fork of some kind.
1392 if( is_ntfs_stream_name(fname)) {
1394 NT_STATUS_OBJECT_PATH_NOT_FOUND);
1399 we need to handle the case when we get a
1400 relative open relative to a file and the
1401 pathname is blank - this is a reopen!
1402 (hint from demyn plantenberg)
1405 reply_doserror(req, ERRDOS, ERRbadfid);
1409 if (ISDOT(dir_fsp->fsp_name)) {
1411 * We're at the toplevel dir, the final file name
1412 * must not contain ./, as this is filtered out
1413 * normally by srvstr_get_path and unix_convert
1414 * explicitly rejects paths containing ./.
1416 parent_fname = talloc_strdup(ctx,"");
1417 if (!parent_fname) {
1418 reply_nterror(req, NT_STATUS_NO_MEMORY);
1422 size_t dir_name_len = strlen(dir_fsp->fsp_name);
1425 * Copy in the base directory name.
1428 parent_fname = TALLOC_ARRAY(ctx, char, dir_name_len+2);
1430 reply_nterror(req, NT_STATUS_NO_MEMORY);
1433 memcpy(parent_fname, dir_fsp->fsp_name,
1437 * Ensure it ends in a '/'.
1438 * We used TALLOC_SIZE +2 to add space for the '/'.
1442 && (parent_fname[dir_name_len-1] != '\\')
1443 && (parent_fname[dir_name_len-1] != '/')) {
1444 parent_fname[dir_name_len] = '/';
1445 parent_fname[dir_name_len+1] = '\0';
1449 tmp = talloc_asprintf(ctx, "%s%s", parent_fname, fname);
1451 reply_nterror(req, NT_STATUS_NO_MEMORY);
1452 END_PROFILE(SMBntcreateX);
1459 * Check to see if this is a mac fork of some kind.
1462 if( is_ntfs_stream_name(fname)) {
1463 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
1468 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1469 if (oplock_request) {
1470 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1475 * Ordinary file or directory.
1478 status = resolve_dfspath(ctx, conn,
1479 req->flags2 & FLAGS2_DFS_PATHNAMES,
1482 if (!NT_STATUS_IS_OK(status)) {
1483 TALLOC_FREE(case_state);
1484 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1485 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1486 ERRSRV, ERRbadpath);
1489 reply_nterror(req, status);
1494 * Check if POSIX semantics are wanted.
1497 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1498 case_state = set_posix_case_semantics(NULL, conn);
1499 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1502 status = unix_convert(ctx, conn, fname, False, &fname, NULL, &sbuf);
1503 if (!NT_STATUS_IS_OK(status)) {
1504 TALLOC_FREE(case_state);
1505 reply_nterror(req, status);
1508 /* All file access must go through check_name() */
1509 status = check_name(conn, fname);
1510 if (!NT_STATUS_IS_OK(status)) {
1511 TALLOC_FREE(case_state);
1512 reply_nterror(req, status);
1516 /* This is the correct thing to do (check every time) but can_delete
1517 * is expensive (it may have to read the parent directory
1518 * permissions). So for now we're not doing it unless we have a strong
1519 * hint the client is really going to delete this file. If the client
1520 * is forcing FILE_CREATE let the filesystem take care of the
1523 /* Setting FILE_SHARE_DELETE is the hint. */
1525 if (lp_acl_check_permissions(SNUM(conn))
1526 && (create_disposition != FILE_CREATE)
1527 && (share_access & FILE_SHARE_DELETE)
1528 && (access_mask & DELETE_ACCESS)) {
1529 if (((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY)
1530 && !lp_delete_readonly(SNUM(conn))) ||
1531 !can_delete_file_in_directory(conn, fname)) {
1532 TALLOC_FREE(case_state);
1533 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1539 /* We need to support SeSecurityPrivilege for this. */
1540 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
1541 !user_has_privileges(current_user.nt_user_token,
1543 TALLOC_FREE(case_state);
1544 reply_nterror(req, NT_STATUS_PRIVILEGE_NOT_HELD);
1550 * If it's a request for a directory open, deal with it separately.
1553 if(create_options & FILE_DIRECTORY_FILE) {
1555 /* Can't open a temp directory. IFS kit test. */
1556 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1557 TALLOC_FREE(case_state);
1558 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1563 * We will get a create directory here if the Win32
1564 * app specified a security descriptor in the
1565 * CreateDirectory() call.
1569 status = open_directory(conn, req, fname, &sbuf,
1579 * Ordinary file case.
1582 status = open_file_ntcreate(conn,req,fname,&sbuf,
1591 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
1594 * Fail the open if it was explicitly a non-directory file.
1597 if (create_options & FILE_NON_DIRECTORY_FILE) {
1598 TALLOC_FREE(case_state);
1599 reply_force_nterror(
1601 NT_STATUS_FILE_IS_A_DIRECTORY);
1606 status = open_directory(conn, req, fname,
1617 TALLOC_FREE(case_state);
1619 if(!NT_STATUS_IS_OK(status)) {
1620 if (open_was_deferred(req->mid)) {
1621 /* We have re-scheduled this call. */
1624 reply_openerror(req, status);
1629 * According to the MS documentation, the only time the security
1630 * descriptor is applied to the opened file is iff we *created* the
1631 * file; an existing file stays the same.
1633 * Also, it seems (from observation) that you can open the file with
1634 * any access mask but you can still write the sd. We need to override
1635 * the granted access before we call set_sd
1636 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1639 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1640 uint32 saved_access_mask = fsp->access_mask;
1642 /* We have already checked that sd_len <= data_count here. */
1644 fsp->access_mask = FILE_GENERIC_ALL;
1646 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1647 if (!NT_STATUS_IS_OK(status)) {
1648 close_file(fsp,ERROR_CLOSE);
1649 TALLOC_FREE(case_state);
1650 reply_nterror(req, status);
1653 fsp->access_mask = saved_access_mask;
1656 if (ea_len && (info == FILE_WAS_CREATED)) {
1657 status = set_ea(conn, fsp, fname, ea_list);
1658 if (!NT_STATUS_IS_OK(status)) {
1659 close_file(fsp,ERROR_CLOSE);
1660 TALLOC_FREE(case_state);
1661 reply_nterror(req, status);
1666 TALLOC_FREE(case_state);
1668 file_len = sbuf.st_size;
1669 fattr = dos_mode(conn,fname,&sbuf);
1671 fattr = FILE_ATTRIBUTE_NORMAL;
1673 if (!fsp->is_directory && (fattr & aDIR)) {
1674 close_file(fsp,ERROR_CLOSE);
1675 reply_doserror(req, ERRDOS, ERRnoaccess);
1679 /* Save the requested allocation size. */
1680 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1681 if (allocation_size && (allocation_size > file_len)) {
1682 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1683 if (fsp->is_directory) {
1684 close_file(fsp,ERROR_CLOSE);
1685 /* Can't set allocation size on a directory. */
1686 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1689 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1690 close_file(fsp,ERROR_CLOSE);
1691 reply_nterror(req, NT_STATUS_DISK_FULL);
1695 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1700 * If the caller set the extended oplock request bit
1701 * and we granted one (by whatever means) - set the
1702 * correct bit for extended oplock reply.
1705 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1706 extended_oplock_granted = True;
1709 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1710 extended_oplock_granted = True;
1713 /* Realloc the size of parameters and data we will return */
1714 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1715 /* Extended response is 32 more byyes. */
1720 params = nttrans_realloc(ppparams, param_len);
1721 if(params == NULL) {
1722 reply_doserror(req, ERRDOS, ERRnomem);
1727 if (extended_oplock_granted) {
1728 if (flags & REQUEST_BATCH_OPLOCK) {
1729 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1731 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1733 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1734 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1736 SCVAL(p,0,NO_OPLOCK_RETURN);
1740 SSVAL(p,0,fsp->fnum);
1742 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1743 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1750 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1751 a_timespec = get_atimespec(&sbuf);
1752 m_timespec = get_mtimespec(&sbuf);
1754 if (lp_dos_filetime_resolution(SNUM(conn))) {
1755 dos_filetime_timespec(&c_timespec);
1756 dos_filetime_timespec(&a_timespec);
1757 dos_filetime_timespec(&m_timespec);
1760 put_long_date_timespec(p, c_timespec); /* create time. */
1762 put_long_date_timespec(p, a_timespec); /* access time */
1764 put_long_date_timespec(p, m_timespec); /* write time */
1766 put_long_date_timespec(p, m_timespec); /* change time */
1768 SIVAL(p,0,fattr); /* File Attributes. */
1770 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1772 SOFF_T(p,0,file_len);
1774 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1778 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1780 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1783 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1784 perms = FILE_GENERIC_ALL;
1786 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1791 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1793 /* Send the required number of replies */
1794 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1799 /****************************************************************************
1800 Reply to a NT CANCEL request.
1801 conn POINTER CAN BE NULL HERE !
1802 ****************************************************************************/
1804 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1807 * Go through and cancel any pending change notifies.
1810 START_PROFILE(SMBntcancel);
1811 remove_pending_change_notify_requests_by_mid(req->mid);
1812 remove_pending_lock_requests_by_mid(req->mid);
1813 srv_cancel_sign_response(req->mid);
1815 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1817 END_PROFILE(SMBntcancel);
1821 /****************************************************************************
1823 ****************************************************************************/
1825 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1826 connection_struct *conn,
1827 struct smb_request *req,
1828 const char *oldname_in,
1829 const char *newname_in,
1832 SMB_STRUCT_STAT sbuf1, sbuf2;
1833 char *oldname = NULL;
1834 char *newname = NULL;
1835 char *last_component_oldname = NULL;
1836 char *last_component_newname = NULL;
1837 files_struct *fsp1,*fsp2;
1841 NTSTATUS status = NT_STATUS_OK;
1846 if (!CAN_WRITE(conn)) {
1847 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1850 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1851 &last_component_oldname, &sbuf1);
1852 if (!NT_STATUS_IS_OK(status)) {
1856 status = check_name(conn, oldname);
1857 if (!NT_STATUS_IS_OK(status)) {
1861 /* Source must already exist. */
1862 if (!VALID_STAT(sbuf1)) {
1863 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1865 /* Ensure attributes match. */
1866 fattr = dos_mode(conn,oldname,&sbuf1);
1867 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1868 return NT_STATUS_NO_SUCH_FILE;
1871 status = unix_convert(ctx, conn, newname_in, False, &newname,
1872 &last_component_newname, &sbuf2);
1873 if (!NT_STATUS_IS_OK(status)) {
1877 status = check_name(conn, newname);
1878 if (!NT_STATUS_IS_OK(status)) {
1882 /* Disallow if newname already exists. */
1883 if (VALID_STAT(sbuf2)) {
1884 return NT_STATUS_OBJECT_NAME_COLLISION;
1887 /* No links from a directory. */
1888 if (S_ISDIR(sbuf1.st_mode)) {
1889 return NT_STATUS_FILE_IS_A_DIRECTORY;
1892 /* Ensure this is within the share. */
1893 status = check_reduced_name(conn, oldname);
1894 if (!NT_STATUS_IS_OK(status)) {
1898 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1901 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1902 FILE_READ_DATA, /* Read-only. */
1903 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1905 0, /* No create options. */
1906 FILE_ATTRIBUTE_NORMAL,
1910 if (!NT_STATUS_IS_OK(status)) {
1914 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1915 FILE_WRITE_DATA, /* Read-only. */
1916 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1918 0, /* No create options. */
1923 if (!NT_STATUS_IS_OK(status)) {
1924 close_file(fsp1,ERROR_CLOSE);
1928 if (sbuf1.st_size) {
1929 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1933 * As we are opening fsp1 read-only we only expect
1934 * an error on close on fsp2 if we are out of space.
1935 * Thus we don't look at the error return from the
1938 close_file(fsp1,NORMAL_CLOSE);
1940 /* Ensure the modtime is set correctly on the destination file. */
1941 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1943 status = close_file(fsp2,NORMAL_CLOSE);
1945 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1946 creates the file. This isn't the correct thing to do in the copy
1948 file_set_dosmode(conn, newname, fattr, &sbuf2,
1949 parent_dirname(newname),false);
1951 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1952 return NT_STATUS_DISK_FULL;
1955 if (!NT_STATUS_IS_OK(status)) {
1956 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1957 nt_errstr(status), oldname, newname));
1962 /****************************************************************************
1963 Reply to a NT rename request.
1964 ****************************************************************************/
1966 void reply_ntrename(connection_struct *conn, struct smb_request *req)
1968 char *oldname = NULL;
1969 char *newname = NULL;
1972 bool src_has_wcard = False;
1973 bool dest_has_wcard = False;
1976 TALLOC_CTX *ctx = talloc_tos();
1978 START_PROFILE(SMBntrename);
1981 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1982 END_PROFILE(SMBntrename);
1986 attrs = SVAL(req->inbuf,smb_vwv0);
1987 rename_type = SVAL(req->inbuf,smb_vwv1);
1989 p = smb_buf(req->inbuf) + 1;
1990 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1991 0, STR_TERMINATE, &status,
1993 if (!NT_STATUS_IS_OK(status)) {
1994 reply_nterror(req, status);
1995 END_PROFILE(SMBntrename);
1999 if( is_ntfs_stream_name(oldname)) {
2000 /* Can't rename a stream. */
2001 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2002 END_PROFILE(SMBntrename);
2006 if (ms_has_wild(oldname)) {
2007 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
2008 END_PROFILE(SMBntrename);
2013 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
2014 0, STR_TERMINATE, &status,
2016 if (!NT_STATUS_IS_OK(status)) {
2017 reply_nterror(req, status);
2018 END_PROFILE(SMBntrename);
2022 status = resolve_dfspath(ctx, conn,
2023 req->flags2 & FLAGS2_DFS_PATHNAMES,
2026 if (!NT_STATUS_IS_OK(status)) {
2027 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2028 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2029 ERRSRV, ERRbadpath);
2030 END_PROFILE(SMBntrename);
2033 reply_nterror(req, status);
2034 END_PROFILE(SMBntrename);
2038 status = resolve_dfspath(ctx, conn,
2039 req->flags2 & FLAGS2_DFS_PATHNAMES,
2042 if (!NT_STATUS_IS_OK(status)) {
2043 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2044 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2045 ERRSRV, ERRbadpath);
2046 END_PROFILE(SMBntrename);
2049 reply_nterror(req, status);
2050 END_PROFILE(SMBntrename);
2054 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
2056 switch(rename_type) {
2057 case RENAME_FLAG_RENAME:
2058 status = rename_internals(ctx, conn, req, oldname,
2059 newname, attrs, False, src_has_wcard,
2062 case RENAME_FLAG_HARD_LINK:
2063 if (src_has_wcard || dest_has_wcard) {
2065 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2067 status = hardlink_internals(ctx,
2073 case RENAME_FLAG_COPY:
2074 if (src_has_wcard || dest_has_wcard) {
2076 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2078 status = copy_internals(ctx, conn, req, oldname,
2082 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
2083 status = NT_STATUS_INVALID_PARAMETER;
2086 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
2090 if (!NT_STATUS_IS_OK(status)) {
2091 if (open_was_deferred(req->mid)) {
2092 /* We have re-scheduled this call. */
2093 END_PROFILE(SMBntrename);
2097 reply_nterror(req, status);
2098 END_PROFILE(SMBntrename);
2102 reply_outbuf(req, 0, 0);
2104 END_PROFILE(SMBntrename);
2108 /****************************************************************************
2109 Reply to a notify change - queue the request and
2110 don't allow a directory to be opened.
2111 ****************************************************************************/
2113 static void call_nt_transact_notify_change(connection_struct *conn,
2114 struct smb_request *req,
2118 uint32 parameter_count,
2119 char **ppdata, uint32 data_count,
2120 uint32 max_data_count,
2121 uint32 max_param_count)
2123 uint16 *setup = *ppsetup;
2129 if(setup_count < 6) {
2130 reply_doserror(req, ERRDOS, ERRbadfunc);
2134 fsp = file_fsp(SVAL(setup,4));
2135 filter = IVAL(setup, 0);
2136 recursive = (SVAL(setup, 6) != 0) ? True : False;
2138 DEBUG(3,("call_nt_transact_notify_change\n"));
2141 reply_doserror(req, ERRDOS, ERRbadfid);
2146 char *filter_string;
2148 if (!(filter_string = notify_filter_string(NULL, filter))) {
2149 reply_nterror(req,NT_STATUS_NO_MEMORY);
2153 DEBUG(3,("call_nt_transact_notify_change: notify change "
2154 "called on %s, filter = %s, recursive = %d\n",
2155 fsp->fsp_name, filter_string, recursive));
2157 TALLOC_FREE(filter_string);
2160 if((!fsp->is_directory) || (conn != fsp->conn)) {
2161 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2165 if (fsp->notify == NULL) {
2167 status = change_notify_create(fsp, filter, recursive);
2169 if (!NT_STATUS_IS_OK(status)) {
2170 DEBUG(10, ("change_notify_create returned %s\n",
2171 nt_errstr(status)));
2172 reply_nterror(req, status);
2177 if (fsp->notify->num_changes != 0) {
2180 * We've got changes pending, respond immediately
2184 * TODO: write a torture test to check the filtering behaviour
2188 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
2191 * change_notify_reply() above has independently sent its
2198 * No changes pending, queue the request
2201 status = change_notify_add_request(req->inbuf, max_param_count, filter,
2203 if (!NT_STATUS_IS_OK(status)) {
2204 reply_nterror(req, status);
2209 /****************************************************************************
2210 Reply to an NT transact rename command.
2211 ****************************************************************************/
2213 static void call_nt_transact_rename(connection_struct *conn,
2214 struct smb_request *req,
2215 uint16 **ppsetup, uint32 setup_count,
2216 char **ppparams, uint32 parameter_count,
2217 char **ppdata, uint32 data_count,
2218 uint32 max_data_count)
2220 char *params = *ppparams;
2221 char *new_name = NULL;
2222 files_struct *fsp = NULL;
2223 bool replace_if_exists = False;
2224 bool dest_has_wcard = False;
2226 TALLOC_CTX *ctx = talloc_tos();
2228 if(parameter_count < 5) {
2229 reply_doserror(req, ERRDOS, ERRbadfunc);
2233 fsp = file_fsp(SVAL(params, 0));
2234 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2235 if (!check_fsp(conn, req, fsp, ¤t_user)) {
2238 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
2239 parameter_count - 4,
2240 STR_TERMINATE, &status, &dest_has_wcard);
2241 if (!NT_STATUS_IS_OK(status)) {
2242 reply_nterror(req, status);
2246 status = rename_internals(ctx,
2256 if (!NT_STATUS_IS_OK(status)) {
2257 if (open_was_deferred(req->mid)) {
2258 /* We have re-scheduled this call. */
2261 reply_nterror(req, status);
2266 * Rename was successful.
2268 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2270 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2271 fsp->fsp_name, new_name));
2276 /******************************************************************************
2277 Fake up a completely empty SD.
2278 *******************************************************************************/
2280 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2284 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2286 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2287 return NT_STATUS_NO_MEMORY;
2290 return NT_STATUS_OK;
2293 /****************************************************************************
2294 Reply to query a security descriptor.
2295 ****************************************************************************/
2297 static void call_nt_transact_query_security_desc(connection_struct *conn,
2298 struct smb_request *req,
2302 uint32 parameter_count,
2305 uint32 max_data_count)
2307 char *params = *ppparams;
2308 char *data = *ppdata;
2310 SEC_DESC *psd = NULL;
2312 uint32 security_info_wanted;
2313 TALLOC_CTX *mem_ctx;
2314 files_struct *fsp = NULL;
2317 if(parameter_count < 8) {
2318 reply_doserror(req, ERRDOS, ERRbadfunc);
2322 fsp = file_fsp(SVAL(params,0));
2324 reply_doserror(req, ERRDOS, ERRbadfid);
2328 security_info_wanted = IVAL(params,4);
2330 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2331 (unsigned int)security_info_wanted ));
2333 params = nttrans_realloc(ppparams, 4);
2334 if(params == NULL) {
2335 reply_doserror(req, ERRDOS, ERRnomem);
2339 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2340 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2341 reply_doserror(req, ERRDOS, ERRnomem);
2346 * Get the permissions to return.
2349 if (!lp_nt_acl_support(SNUM(conn))) {
2350 status = get_null_nt_acl(mem_ctx, &psd);
2352 status = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd,
2353 security_info_wanted, &psd);
2356 if (!NT_STATUS_IS_OK(status)) {
2357 talloc_destroy(mem_ctx);
2358 reply_nterror(req, status);
2362 sd_size = sec_desc_size(psd);
2364 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2366 SIVAL(params,0,(uint32)sd_size);
2368 if(max_data_count < sd_size) {
2370 send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
2371 params, 4, *ppdata, 0);
2372 talloc_destroy(mem_ctx);
2377 * Allocate the data we will point this at.
2380 data = nttrans_realloc(ppdata, sd_size);
2382 talloc_destroy(mem_ctx);
2383 reply_doserror(req, ERRDOS, ERRnomem);
2388 * Init the parse struct we will marshall into.
2391 prs_init(&pd, 0, mem_ctx, MARSHALL);
2394 * Setup the prs_struct to point at the memory we just
2398 prs_give_memory( &pd, data, (uint32)sd_size, False);
2401 * Finally, linearize into the outgoing buffer.
2404 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2405 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2406 security descriptor.\n"));
2408 * Return access denied for want of a better error message..
2410 talloc_destroy(mem_ctx);
2411 reply_unixerror(req, ERRDOS, ERRnoaccess);
2416 * Now we can delete the security descriptor.
2419 talloc_destroy(mem_ctx);
2421 send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2425 /****************************************************************************
2426 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2427 ****************************************************************************/
2429 static void call_nt_transact_set_security_desc(connection_struct *conn,
2430 struct smb_request *req,
2434 uint32 parameter_count,
2437 uint32 max_data_count)
2439 char *params= *ppparams;
2440 char *data = *ppdata;
2441 files_struct *fsp = NULL;
2442 uint32 security_info_sent = 0;
2445 if(parameter_count < 8) {
2446 reply_doserror(req, ERRDOS, ERRbadfunc);
2450 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
2451 reply_doserror(req, ERRDOS, ERRbadfid);
2455 if(!lp_nt_acl_support(SNUM(conn))) {
2459 security_info_sent = IVAL(params,4);
2461 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2462 (unsigned int)security_info_sent ));
2464 if (data_count == 0) {
2465 reply_doserror(req, ERRDOS, ERRnoaccess);
2469 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2470 reply_nterror(req, nt_status);
2476 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2480 /****************************************************************************
2482 ****************************************************************************/
2484 static void call_nt_transact_ioctl(connection_struct *conn,
2485 struct smb_request *req,
2486 uint16 **ppsetup, uint32 setup_count,
2487 char **ppparams, uint32 parameter_count,
2488 char **ppdata, uint32 data_count,
2489 uint32 max_data_count)
2496 static bool logged_message;
2497 char *pdata = *ppdata;
2499 if (setup_count != 8) {
2500 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2501 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2505 function = IVAL(*ppsetup, 0);
2506 fidnum = SVAL(*ppsetup, 4);
2507 isFSctl = CVAL(*ppsetup, 6);
2508 compfilter = CVAL(*ppsetup, 7);
2510 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2511 function, fidnum, isFSctl, compfilter));
2513 fsp=file_fsp(fidnum);
2514 /* this check is done in each implemented function case for now
2515 because I don't want to break anything... --metze
2516 FSP_BELONGS_CONN(fsp,conn);*/
2519 case FSCTL_SET_SPARSE:
2520 /* pretend this succeeded - tho strictly we should
2521 mark the file sparse (if the local fs supports it)
2522 so we can know if we need to pre-allocate or not */
2524 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2525 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2528 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2530 unsigned char objid[16];
2532 /* This should return the object-id on this file.
2533 * I think I'll make this be the inode+dev. JRA.
2536 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2538 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2543 pdata = nttrans_realloc(ppdata, data_count);
2544 if (pdata == NULL) {
2545 reply_nterror(req, NT_STATUS_NO_MEMORY);
2548 push_file_id_16(pdata, &fsp->file_id);
2549 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2550 push_file_id_16(pdata+32, &fsp->file_id);
2551 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2556 case FSCTL_GET_REPARSE_POINT:
2557 /* pretend this fail - my winXP does it like this
2561 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2562 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2565 case FSCTL_SET_REPARSE_POINT:
2566 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2570 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2571 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2574 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2577 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2578 * and return their volume names. If max_data_count is 16, then it is just
2579 * asking for the number of volumes and length of the combined names.
2581 * pdata is the data allocated by our caller, but that uses
2582 * total_data_count (which is 0 in our case) rather than max_data_count.
2583 * Allocate the correct amount and return the pointer to let
2584 * it be deallocated when we return.
2586 SHADOW_COPY_DATA *shadow_data = NULL;
2587 TALLOC_CTX *shadow_mem_ctx = NULL;
2588 bool labels = False;
2589 uint32 labels_data_count = 0;
2593 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2597 if (max_data_count < 16) {
2598 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2600 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2604 if (max_data_count > 16) {
2608 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2609 if (shadow_mem_ctx == NULL) {
2610 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2611 reply_nterror(req, NT_STATUS_NO_MEMORY);
2615 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2616 if (shadow_data == NULL) {
2617 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2618 talloc_destroy(shadow_mem_ctx);
2619 reply_nterror(req, NT_STATUS_NO_MEMORY);
2623 shadow_data->mem_ctx = shadow_mem_ctx;
2626 * Call the VFS routine to actually do the work.
2628 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2629 talloc_destroy(shadow_data->mem_ctx);
2630 if (errno == ENOSYS) {
2631 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2632 conn->connectpath));
2633 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2636 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2637 conn->connectpath));
2638 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2643 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2648 data_count = 12+labels_data_count+4;
2651 if (max_data_count<data_count) {
2652 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2653 max_data_count,data_count));
2654 talloc_destroy(shadow_data->mem_ctx);
2655 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2659 pdata = nttrans_realloc(ppdata, data_count);
2660 if (pdata == NULL) {
2661 talloc_destroy(shadow_data->mem_ctx);
2662 reply_nterror(req, NT_STATUS_NO_MEMORY);
2668 /* num_volumes 4 bytes */
2669 SIVAL(pdata,0,shadow_data->num_volumes);
2672 /* num_labels 4 bytes */
2673 SIVAL(pdata,4,shadow_data->num_volumes);
2676 /* needed_data_count 4 bytes */
2677 SIVAL(pdata,8,labels_data_count);
2681 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2682 shadow_data->num_volumes,fsp->fsp_name));
2683 if (labels && shadow_data->labels) {
2684 for (i=0;i<shadow_data->num_volumes;i++) {
2685 srvstr_push(pdata, req->flags2,
2686 cur_pdata, shadow_data->labels[i],
2687 2*sizeof(SHADOW_COPY_LABEL),
2688 STR_UNICODE|STR_TERMINATE);
2689 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2690 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2694 talloc_destroy(shadow_data->mem_ctx);
2696 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2702 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2704 /* pretend this succeeded -
2706 * we have to send back a list with all files owned by this SID
2708 * but I have to check that --metze
2712 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2714 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2716 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2720 /* unknown 4 bytes: this is not the length of the sid :-( */
2721 /*unknown = IVAL(pdata,0);*/
2723 sid_parse(pdata+4,sid_len,&sid);
2724 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2726 if (!sid_to_uid(&sid, &uid)) {
2727 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2728 sid_string_static(&sid),(unsigned long)sid_len));
2732 /* we can take a look at the find source :-)
2734 * find ./ -uid $uid -name '*' is what we need here
2737 * and send 4bytes len and then NULL terminated unicode strings
2740 * but I don't know how to deal with the paged results
2741 * (maybe we can hang the result anywhere in the fsp struct)
2743 * we don't send all files at once
2744 * and at the next we should *not* start from the beginning,
2745 * so we have to cache the result
2750 /* this works for now... */
2751 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2755 if (!logged_message) {
2756 logged_message = True; /* Only print this once... */
2757 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2762 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2766 #ifdef HAVE_SYS_QUOTAS
2767 /****************************************************************************
2768 Reply to get user quota
2769 ****************************************************************************/
2771 static void call_nt_transact_get_user_quota(connection_struct *conn,
2772 struct smb_request *req,
2776 uint32 parameter_count,
2779 uint32 max_data_count)
2781 NTSTATUS nt_status = NT_STATUS_OK;
2782 char *params = *ppparams;
2783 char *pdata = *ppdata;
2785 int data_len=0,param_len=0;
2788 files_struct *fsp = NULL;
2792 bool start_enum = True;
2793 SMB_NTQUOTA_STRUCT qt;
2794 SMB_NTQUOTA_LIST *tmp_list;
2795 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2800 if (current_user.ut.uid != 0) {
2801 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2802 lp_servicename(SNUM(conn)),conn->user));
2803 reply_doserror(req, ERRDOS, ERRnoaccess);
2808 * Ensure minimum number of parameters sent.
2811 if (parameter_count < 4) {
2812 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2813 reply_doserror(req, ERRDOS, ERRinvalidparam);
2817 /* maybe we can check the quota_fnum */
2818 fsp = file_fsp(SVAL(params,0));
2819 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2820 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2821 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2825 /* the NULL pointer checking for fsp->fake_file_handle->pd
2826 * is done by CHECK_NTQUOTA_HANDLE_OK()
2828 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2830 level = SVAL(params,2);
2832 /* unknown 12 bytes leading in params */
2835 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2836 /* seems that we should continue with the enum here --metze */
2838 if (qt_handle->quota_list!=NULL &&
2839 qt_handle->tmp_list==NULL) {
2842 free_ntquota_list(&(qt_handle->quota_list));
2844 /* Realloc the size of parameters and data we will return */
2846 params = nttrans_realloc(ppparams, param_len);
2847 if(params == NULL) {
2848 reply_doserror(req, ERRDOS, ERRnomem);
2853 SIVAL(params,0,data_len);
2860 case TRANSACT_GET_USER_QUOTA_LIST_START:
2862 if (qt_handle->quota_list==NULL &&
2863 qt_handle->tmp_list==NULL) {
2867 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2868 reply_doserror(req, ERRSRV, ERRerror);
2872 /* Realloc the size of parameters and data we will return */
2874 params = nttrans_realloc(ppparams, param_len);
2875 if(params == NULL) {
2876 reply_doserror(req, ERRDOS, ERRnomem);
2880 /* we should not trust the value in max_data_count*/
2881 max_data_count = MIN(max_data_count,2048);
2883 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2885 reply_doserror(req, ERRDOS, ERRnomem);
2891 /* set params Size of returned Quota Data 4 bytes*/
2892 /* but set it later when we know it */
2894 /* for each entry push the data */
2897 qt_handle->tmp_list = qt_handle->quota_list;
2900 tmp_list = qt_handle->tmp_list;
2902 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2903 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2905 sid_len = sid_size(&tmp_list->quotas->sid);
2906 entry_len = 40 + sid_len;
2908 /* nextoffset entry 4 bytes */
2909 SIVAL(entry,0,entry_len);
2911 /* then the len of the SID 4 bytes */
2912 SIVAL(entry,4,sid_len);
2914 /* unknown data 8 bytes SMB_BIG_UINT */
2915 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2917 /* the used disk space 8 bytes SMB_BIG_UINT */
2918 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2920 /* the soft quotas 8 bytes SMB_BIG_UINT */
2921 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2923 /* the hard quotas 8 bytes SMB_BIG_UINT */
2924 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2926 /* and now the SID */
2927 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2930 qt_handle->tmp_list = tmp_list;
2932 /* overwrite the offset of the last entry */
2933 SIVAL(entry-entry_len,0,0);
2935 data_len = 4+qt_len;
2936 /* overwrite the params quota_data_len */
2937 SIVAL(params,0,data_len);
2941 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2943 /* unknown 4 bytes IVAL(pdata,0) */
2945 if (data_count < 8) {
2946 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2947 reply_doserror(req, ERRDOS, ERRunknownlevel);
2951 sid_len = IVAL(pdata,4);
2952 /* Ensure this is less than 1mb. */
2953 if (sid_len > (1024*1024)) {
2954 reply_doserror(req, ERRDOS, ERRnomem);
2958 if (data_count < 8+sid_len) {
2959 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2960 reply_doserror(req, ERRDOS, ERRunknownlevel);
2964 data_len = 4+40+sid_len;
2966 if (max_data_count < data_len) {
2967 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2968 max_data_count, data_len));
2970 SIVAL(params,0,data_len);
2972 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2976 sid_parse(pdata+8,sid_len,&sid);
2978 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2981 * we have to return zero's in all fields
2982 * instead of returning an error here
2987 /* Realloc the size of parameters and data we will return */
2989 params = nttrans_realloc(ppparams, param_len);
2990 if(params == NULL) {
2991 reply_doserror(req, ERRDOS, ERRnomem);
2995 pdata = nttrans_realloc(ppdata, data_len);
2997 reply_doserror(req, ERRDOS, ERRnomem);
3003 /* set params Size of returned Quota Data 4 bytes*/
3004 SIVAL(params,0,data_len);
3006 /* nextoffset entry 4 bytes */
3009 /* then the len of the SID 4 bytes */
3010 SIVAL(entry,4,sid_len);
3012 /* unknown data 8 bytes SMB_BIG_UINT */
3013 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
3015 /* the used disk space 8 bytes SMB_BIG_UINT */
3016 SBIG_UINT(entry,16,qt.usedspace);
3018 /* the soft quotas 8 bytes SMB_BIG_UINT */
3019 SBIG_UINT(entry,24,qt.softlim);
3021 /* the hard quotas 8 bytes SMB_BIG_UINT */
3022 SBIG_UINT(entry,32,qt.hardlim);
3024 /* and now the SID */
3025 sid_linearize(entry+40, sid_len, &sid);
3030 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
3031 reply_doserror(req, ERRSRV, ERRerror);
3036 send_nt_replies(req, nt_status, params, param_len,
3040 /****************************************************************************
3041 Reply to set user quota
3042 ****************************************************************************/
3044 static void call_nt_transact_set_user_quota(connection_struct *conn,
3045 struct smb_request *req,
3049 uint32 parameter_count,
3052 uint32 max_data_count)
3054 char *params = *ppparams;
3055 char *pdata = *ppdata;
3056 int data_len=0,param_len=0;
3057 SMB_NTQUOTA_STRUCT qt;
3060 files_struct *fsp = NULL;
3065 if (current_user.ut.uid != 0) {
3066 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
3067 lp_servicename(SNUM(conn)),conn->user));
3068 reply_doserror(req, ERRDOS, ERRnoaccess);
3073 * Ensure minimum number of parameters sent.
3076 if (parameter_count < 2) {
3077 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
3078 reply_doserror(req, ERRDOS, ERRinvalidparam);
3082 /* maybe we can check the quota_fnum */
3083 fsp = file_fsp(SVAL(params,0));
3084 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
3085 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
3086 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
3090 if (data_count < 40) {
3091 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
3092 reply_doserror(req, ERRDOS, ERRunknownlevel);
3096 /* offset to next quota record.
3097 * 4 bytes IVAL(pdata,0)
3102 sid_len = IVAL(pdata,4);
3104 if (data_count < 40+sid_len) {
3105 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
3106 reply_doserror(req, ERRDOS, ERRunknownlevel);
3110 /* unknown 8 bytes in pdata
3111 * maybe its the change time in NTTIME
3114 /* the used space 8 bytes (SMB_BIG_UINT)*/
3115 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
3116 #ifdef LARGE_SMB_OFF_T
3117 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
3118 #else /* LARGE_SMB_OFF_T */
3119 if ((IVAL(pdata,20) != 0)&&
3120 ((qt.usedspace != 0xFFFFFFFF)||
3121 (IVAL(pdata,20)!=0xFFFFFFFF))) {
3122 /* more than 32 bits? */
3123 reply_doserror(req, ERRDOS, ERRunknownlevel);
3126 #endif /* LARGE_SMB_OFF_T */
3128 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
3129 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
3130 #ifdef LARGE_SMB_OFF_T
3131 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
3132 #else /* LARGE_SMB_OFF_T */
3133 if ((IVAL(pdata,28) != 0)&&
3134 ((qt.softlim != 0xFFFFFFFF)||
3135 (IVAL(pdata,28)!=0xFFFFFFFF))) {
3136 /* more than 32 bits? */
3137 reply_doserror(req, ERRDOS, ERRunknownlevel);
3140 #endif /* LARGE_SMB_OFF_T */
3142 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
3143 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
3144 #ifdef LARGE_SMB_OFF_T
3145 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
3146 #else /* LARGE_SMB_OFF_T */
3147 if ((IVAL(pdata,36) != 0)&&
3148 ((qt.hardlim != 0xFFFFFFFF)||
3149 (IVAL(pdata,36)!=0xFFFFFFFF))) {
3150 /* more than 32 bits? */
3151 reply_doserror(req, ERRDOS, ERRunknownlevel);
3154 #endif /* LARGE_SMB_OFF_T */
3156 sid_parse(pdata+40,sid_len,&sid);
3157 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
3159 /* 44 unknown bytes left... */
3161 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
3162 reply_doserror(req, ERRSRV, ERRerror);
3166 send_nt_replies(req, NT_STATUS_OK, params, param_len,
3169 #endif /* HAVE_SYS_QUOTAS */
3171 static void handle_nttrans(connection_struct *conn,
3172 struct trans_state *state,
3173 struct smb_request *req)
3175 if (Protocol >= PROTOCOL_NT1) {
3176 req->flags2 |= 0x40; /* IS_LONG_NAME */
3177 SSVAL(req->inbuf,smb_flg2,req->flags2);
3180 /* Now we must call the relevant NT_TRANS function */
3181 switch(state->call) {
3182 case NT_TRANSACT_CREATE:
3184 START_PROFILE(NT_transact_create);
3185 call_nt_transact_create(
3187 &state->setup, state->setup_count,
3188 &state->param, state->total_param,
3189 &state->data, state->total_data,
3190 state->max_data_return);
3191 END_PROFILE(NT_transact_create);
3195 case NT_TRANSACT_IOCTL:
3197 START_PROFILE(NT_transact_ioctl);
3198 call_nt_transact_ioctl(
3200 &state->setup, state->setup_count,
3201 &state->param, state->total_param,
3202 &state->data, state->total_data,
3203 state->max_data_return);
3204 END_PROFILE(NT_transact_ioctl);
3208 case NT_TRANSACT_SET_SECURITY_DESC:
3210 START_PROFILE(NT_transact_set_security_desc);
3211 call_nt_transact_set_security_desc(
3213 &state->setup, state->setup_count,
3214 &state->param, state->total_param,
3215 &state->data, state->total_data,
3216 state->max_data_return);
3217 END_PROFILE(NT_transact_set_security_desc);
3221 case NT_TRANSACT_NOTIFY_CHANGE:
3223 START_PROFILE(NT_transact_notify_change);
3224 call_nt_transact_notify_change(
3226 &state->setup, state->setup_count,
3227 &state->param, state->total_param,
3228 &state->data, state->total_data,
3229 state->max_data_return,
3230 state->max_param_return);
3231 END_PROFILE(NT_transact_notify_change);
3235 case NT_TRANSACT_RENAME:
3237 START_PROFILE(NT_transact_rename);
3238 call_nt_transact_rename(
3240 &state->setup, state->setup_count,
3241 &state->param, state->total_param,
3242 &state->data, state->total_data,
3243 state->max_data_return);
3244 END_PROFILE(NT_transact_rename);
3248 case NT_TRANSACT_QUERY_SECURITY_DESC:
3250 START_PROFILE(NT_transact_query_security_desc);
3251 call_nt_transact_query_security_desc(
3253 &state->setup, state->setup_count,
3254 &state->param, state->total_param,
3255 &state->data, state->total_data,
3256 state->max_data_return);
3257 END_PROFILE(NT_transact_query_security_desc);
3261 #ifdef HAVE_SYS_QUOTAS
3262 case NT_TRANSACT_GET_USER_QUOTA:
3264 START_PROFILE(NT_transact_get_user_quota);
3265 call_nt_transact_get_user_quota(
3267 &state->setup, state->setup_count,
3268 &state->param, state->total_param,
3269 &state->data, state->total_data,
3270 state->max_data_return);
3271 END_PROFILE(NT_transact_get_user_quota);
3275 case NT_TRANSACT_SET_USER_QUOTA:
3277 START_PROFILE(NT_transact_set_user_quota);
3278 call_nt_transact_set_user_quota(
3280 &state->setup, state->setup_count,
3281 &state->param, state->total_param,
3282 &state->data, state->total_data,
3283 state->max_data_return);
3284 END_PROFILE(NT_transact_set_user_quota);
3287 #endif /* HAVE_SYS_QUOTAS */
3290 /* Error in request */
3291 DEBUG(0,("handle_nttrans: Unknown request %d in "
3292 "nttrans call\n", state->call));
3293 reply_doserror(req, ERRSRV, ERRerror);
3299 /****************************************************************************
3300 Reply to a SMBNTtrans.
3301 ****************************************************************************/
3303 void reply_nttrans(connection_struct *conn, struct smb_request *req)
3309 uint16 function_code;
3311 struct trans_state *state;
3314 START_PROFILE(SMBnttrans);
3316 if (req->wct < 19) {
3317 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3318 END_PROFILE(SMBnttrans);
3322 size = smb_len(req->inbuf) + 4;
3323 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
3324 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
3325 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
3326 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
3327 function_code = SVAL(req->inbuf, smb_nt_Function);
3329 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3330 reply_doserror(req, ERRSRV, ERRaccess);
3331 END_PROFILE(SMBnttrans);
3335 result = allow_new_trans(conn->pending_trans, req->mid);
3336 if (!NT_STATUS_IS_OK(result)) {
3337 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3338 reply_nterror(req, result);
3339 END_PROFILE(SMBnttrans);
3343 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3344 reply_doserror(req, ERRSRV, ERRaccess);
3345 END_PROFILE(SMBnttrans);
3349 state->cmd = SMBnttrans;
3351 state->mid = req->mid;
3352 state->vuid = req->vuid;
3353 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
3355 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
3356 state->param = NULL;
3357 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
3358 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
3360 /* setup count is in *words* */
3361 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
3362 state->setup = NULL;
3363 state->call = function_code;
3366 * All nttrans messages we handle have smb_wct == 19 +
3367 * state->setup_count. Ensure this is so as a sanity check.
3370 if(req->wct != 19 + (state->setup_count/2)) {
3371 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3372 req->wct, 19 + (state->setup_count/2)));
3376 /* Don't allow more than 128mb for each value. */
3377 if ((state->total_data > (1024*1024*128)) ||
3378 (state->total_param > (1024*1024*128))) {
3379 reply_doserror(req, ERRDOS, ERRnomem);
3380 END_PROFILE(SMBnttrans);
3384 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3387 if (state->total_data) {
3388 /* Can't use talloc here, the core routines do realloc on the
3389 * params and data. */
3390 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3391 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3392 "bytes !\n", (unsigned int)state->total_data));
3394 reply_doserror(req, ERRDOS, ERRnomem);
3395 END_PROFILE(SMBnttrans);
3398 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3400 if ((smb_base(req->inbuf)+dsoff+dscnt
3401 > (char *)req->inbuf + size) ||
3402 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
3405 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3408 if (state->total_param) {
3409 /* Can't use talloc here, the core routines do realloc on the
3410 * params and data. */
3411 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3412 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3413 "bytes !\n", (unsigned int)state->total_param));
3414 SAFE_FREE(state->data);
3416 reply_doserror(req, ERRDOS, ERRnomem);
3417 END_PROFILE(SMBnttrans);
3420 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3422 if ((smb_base(req->inbuf)+psoff+pscnt
3423 > (char *)req->inbuf + size) ||
3424 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
3427 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3430 state->received_data = dscnt;
3431 state->received_param = pscnt;
3433 if(state->setup_count > 0) {
3434 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3435 state->setup_count));
3436 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3437 if (state->setup == NULL) {
3438 DEBUG(0,("reply_nttrans : Out of memory\n"));
3439 SAFE_FREE(state->data);
3440 SAFE_FREE(state->param);
3442 reply_doserror(req, ERRDOS, ERRnomem);
3443 END_PROFILE(SMBnttrans);
3447 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3448 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3451 if (smb_nt_SetupStart + state->setup_count > size) {
3455 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
3456 state->setup_count);
3457 dump_data(10, (uint8 *)state->setup, state->setup_count);
3460 if ((state->received_data == state->total_data) &&
3461 (state->received_param == state->total_param)) {
3462 handle_nttrans(conn, state, req);
3463 SAFE_FREE(state->param);
3464 SAFE_FREE(state->data);
3466 END_PROFILE(SMBnttrans);
3470 DLIST_ADD(conn->pending_trans, state);
3472 /* We need to send an interim response then receive the rest
3473 of the parameter/data bytes */
3474 reply_outbuf(req, 0, 0);
3475 show_msg((char *)req->outbuf);
3476 END_PROFILE(SMBnttrans);
3481 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3482 SAFE_FREE(state->data);
3483 SAFE_FREE(state->param);
3485 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3486 END_PROFILE(SMBnttrans);
3490 /****************************************************************************
3491 Reply to a SMBnttranss
3492 ****************************************************************************/
3494 void reply_nttranss(connection_struct *conn, struct smb_request *req)
3496 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3497 struct trans_state *state;
3501 START_PROFILE(SMBnttranss);
3503 show_msg((char *)req->inbuf);
3505 if (req->wct < 18) {
3506 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3507 END_PROFILE(SMBnttranss);
3511 for (state = conn->pending_trans; state != NULL;
3512 state = state->next) {
3513 if (state->mid == req->mid) {
3518 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3519 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3520 END_PROFILE(SMBnttranss);
3524 /* Revise state->total_param and state->total_data in case they have
3525 changed downwards */
3526 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
3527 < state->total_param) {
3528 state->total_param = IVAL(req->inbuf,
3529 smb_nts_TotalParameterCount);
3531 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
3532 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
3535 size = smb_len(req->inbuf) + 4;
3537 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
3538 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
3539 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
3541 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
3542 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
3543 doff = IVAL(req->inbuf, smb_nts_DataOffset);
3545 state->received_param += pcnt;
3546 state->received_data += dcnt;
3548 if ((state->received_data > state->total_data) ||
3549 (state->received_param > state->total_param))
3553 if (pdisp+pcnt > state->total_param)
3555 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3557 if (pdisp > state->total_param)
3559 if ((smb_base(req->inbuf) + poff + pcnt
3560 > (char *)req->inbuf + size) ||
3561 (smb_base(req->inbuf) + poff + pcnt
3562 < smb_base(req->inbuf)))
3564 if (state->param + pdisp < state->param)
3567 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
3572 if (ddisp+dcnt > state->total_data)
3574 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3576 if (ddisp > state->total_data)
3578 if ((smb_base(req->inbuf) + doff + dcnt
3579 > (char *)req->inbuf + size) ||
3580 (smb_base(req->inbuf) + doff + dcnt
3581 < smb_base(req->inbuf)))
3583 if (state->data + ddisp < state->data)
3586 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
3590 if ((state->received_param < state->total_param) ||
3591 (state->received_data < state->total_data)) {
3592 END_PROFILE(SMBnttranss);
3597 * construct_reply_common will copy smb_com from inbuf to
3598 * outbuf. SMBnttranss is wrong here.
3600 SCVAL(req->inbuf,smb_com,SMBnttrans);
3602 handle_nttrans(conn, state, req);
3604 DLIST_REMOVE(conn->pending_trans, state);
3605 SAFE_FREE(state->data);
3606 SAFE_FREE(state->param);
3608 END_PROFILE(SMBnttranss);
3613 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3614 DLIST_REMOVE(conn->pending_trans, state);
3615 SAFE_FREE(state->data);
3616 SAFE_FREE(state->param);
3618 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3619 END_PROFILE(SMBnttranss);