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 int smb_read_error;
26 extern struct current_user current_user;
28 static const char *known_nt_pipes[] = {
49 static char *nttrans_realloc(char **ptr, size_t size)
52 smb_panic("nttrans_realloc() called with NULL ptr");
55 *ptr = (char *)SMB_REALLOC(*ptr, size);
59 memset(*ptr,'\0',size);
63 /****************************************************************************
64 Send the required number of replies back.
65 We assume all fields other than the data fields are
66 set correctly for the type of call.
67 HACK ! Always assumes smb_setup field is zero.
68 ****************************************************************************/
70 void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
71 char *params, int paramsize,
72 char *pdata, int datasize)
74 int data_to_send = datasize;
75 int params_to_send = paramsize;
79 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
80 int alignment_offset = 3;
81 int data_alignment_offset = 0;
84 * If there genuinely are no parameters or data to send just send
88 if(params_to_send == 0 && data_to_send == 0) {
89 reply_outbuf(req, 18, 0);
90 show_msg((char *)req->outbuf);
95 * When sending params and data ensure that both are nicely aligned.
96 * Only do this alignment when there is also data to send - else
97 * can cause NT redirector problems.
100 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
101 data_alignment_offset = 4 - (params_to_send % 4);
105 * Space is bufsize minus Netbios over TCP header minus SMB header.
106 * The alignment_offset is to align the param bytes on a four byte
107 * boundary (2 bytes for data len, one byte pad).
108 * NT needs this to work correctly.
111 useable_space = max_send - (smb_size
114 + data_alignment_offset);
117 * useable_space can never be more than max_send minus the
121 useable_space = MIN(useable_space,
122 max_send - (alignment_offset+data_alignment_offset));
125 while (params_to_send || data_to_send) {
128 * Calculate whether we will totally or partially fill this packet.
131 total_sent_thistime = params_to_send + data_to_send +
132 alignment_offset + data_alignment_offset;
135 * We can never send more than useable_space.
138 total_sent_thistime = MIN(total_sent_thistime, useable_space);
140 reply_outbuf(req, 18, total_sent_thistime);
143 * Set total params and data to be sent.
146 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
147 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
150 * Calculate how many parameters and data we can fit into
151 * this packet. Parameters get precedence.
154 params_sent_thistime = MIN(params_to_send,useable_space);
155 data_sent_thistime = useable_space - params_sent_thistime;
156 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
158 SIVAL(req->outbuf, smb_ntr_ParameterCount,
159 params_sent_thistime);
161 if(params_sent_thistime == 0) {
162 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
163 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
166 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
167 * parameter bytes, however the first 4 bytes of outbuf are
168 * the Netbios over TCP header. Thus use smb_base() to subtract
169 * them from the calculation.
172 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
173 ((smb_buf(req->outbuf)+alignment_offset)
174 - smb_base(req->outbuf)));
176 * Absolute displacement of param bytes sent in this packet.
179 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
184 * Deal with the data portion.
187 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
189 if(data_sent_thistime == 0) {
190 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
191 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
194 * The offset of the data bytes is the offset of the
195 * parameter bytes plus the number of parameters being sent this time.
198 SIVAL(req->outbuf, smb_ntr_DataOffset,
199 ((smb_buf(req->outbuf)+alignment_offset) -
200 smb_base(req->outbuf))
201 + params_sent_thistime + data_alignment_offset);
202 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
206 * Copy the param bytes into the packet.
209 if(params_sent_thistime) {
210 if (alignment_offset != 0) {
211 memset(smb_buf(req->outbuf), 0,
214 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
215 params_sent_thistime);
219 * Copy in the data bytes
222 if(data_sent_thistime) {
223 if (data_alignment_offset != 0) {
224 memset((smb_buf(req->outbuf)+alignment_offset+
225 params_sent_thistime), 0,
226 data_alignment_offset);
228 memcpy(smb_buf(req->outbuf)+alignment_offset
229 +params_sent_thistime+data_alignment_offset,
230 pd,data_sent_thistime);
233 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
234 params_sent_thistime, data_sent_thistime, useable_space));
235 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
236 params_to_send, data_to_send, paramsize, datasize));
238 if (NT_STATUS_V(nt_error)) {
239 error_packet_set((char *)req->outbuf,
244 /* Send the packet */
245 show_msg((char *)req->outbuf);
246 if (!send_smb(smbd_server_fd(),(char *)req->outbuf)) {
247 exit_server_cleanly("send_nt_replies: send_smb failed.");
250 TALLOC_FREE(req->outbuf);
252 pp += params_sent_thistime;
253 pd += data_sent_thistime;
255 params_to_send -= params_sent_thistime;
256 data_to_send -= data_sent_thistime;
262 if(params_to_send < 0 || data_to_send < 0) {
263 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
264 params_to_send, data_to_send));
270 /****************************************************************************
271 Is it an NTFS stream name ?
272 ****************************************************************************/
274 BOOL is_ntfs_stream_name(const char *fname)
276 if (lp_posix_pathnames()) {
279 return (strchr_m(fname, ':') != NULL) ? True : False;
282 struct case_semantics_state {
283 connection_struct *conn;
286 BOOL short_case_preserve;
289 /****************************************************************************
290 Restore case semantics.
291 ****************************************************************************/
292 static int restore_case_semantics(struct case_semantics_state *state)
294 state->conn->case_sensitive = state->case_sensitive;
295 state->conn->case_preserve = state->case_preserve;
296 state->conn->short_case_preserve = state->short_case_preserve;
300 /****************************************************************************
302 ****************************************************************************/
303 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
304 connection_struct *conn)
306 struct case_semantics_state *result;
308 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
309 DEBUG(0, ("talloc failed\n"));
314 result->case_sensitive = conn->case_sensitive;
315 result->case_preserve = conn->case_preserve;
316 result->short_case_preserve = conn->short_case_preserve;
319 conn->case_sensitive = True;
320 conn->case_preserve = True;
321 conn->short_case_preserve = True;
323 talloc_set_destructor(result, restore_case_semantics);
328 /****************************************************************************
329 Reply to an NT create and X call on a pipe
330 ****************************************************************************/
332 static void nt_open_pipe(char *fname, connection_struct *conn,
333 struct smb_request *req, int *ppnum)
335 smb_np_struct *p = NULL;
338 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
340 /* See if it is one we want to handle. */
342 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
343 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
348 for( i = 0; known_nt_pipes[i]; i++ ) {
349 if( strequal(fname,known_nt_pipes[i])) {
354 if ( known_nt_pipes[i] == NULL ) {
355 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
360 /* Strip \\ off the name. */
363 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
365 p = open_rpc_pipe_p(fname, conn, req->vuid);
367 reply_doserror(req, ERRSRV, ERRnofids);
371 /* TODO: Add pipe to db */
373 if ( !store_pipe_opendb( p ) ) {
374 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
381 /****************************************************************************
382 Reply to an NT create and X call for pipes.
383 ****************************************************************************/
385 static void do_ntcreate_pipe_open(connection_struct *conn,
386 struct smb_request *req)
391 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
392 TALLOC_CTX *ctx = talloc_tos();
394 srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
395 smb_buf(req->inbuf), STR_TERMINATE);
398 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
402 nt_open_pipe(fname, conn, req, &pnum);
410 * Deal with pipe return.
413 if (flags & EXTENDED_RESPONSE_REQUIRED) {
414 /* This is very strange. We
415 * return 50 words, but only set
416 * the wcnt to 42 ? It's definately
417 * what happens on the wire....
419 reply_outbuf(req, 50, 0);
420 SCVAL(req->outbuf,smb_wct,42);
422 reply_outbuf(req, 34, 0);
425 p = (char *)req->outbuf + smb_vwv2;
429 SIVAL(p,0,FILE_WAS_OPENED);
432 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
435 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
437 SSVAL(p,2, 0x5FF); /* ? */
440 if (flags & EXTENDED_RESPONSE_REQUIRED) {
442 SIVAL(p,0,FILE_GENERIC_ALL);
444 * For pipes W2K3 seems to return
446 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
448 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
451 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
456 /****************************************************************************
457 Reply to an NT create and X call for a quota file.
458 ****************************************************************************/
460 static void reply_ntcreate_and_X_quota(connection_struct *conn,
461 struct smb_request *req,
462 enum FAKE_FILE_TYPE fake_file_type,
466 uint32 desired_access = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
470 status = open_fake_file(conn, fake_file_type, fname, desired_access,
473 if (!NT_STATUS_IS_OK(status)) {
474 reply_nterror(req, status);
478 reply_outbuf(req, 34, 0);
480 p = (char *)req->outbuf + smb_vwv2;
482 /* SCVAL(p,0,NO_OPLOCK_RETURN); */
484 SSVAL(p,0,fsp->fnum);
486 DEBUG(5,("reply_ntcreate_and_X_quota: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
491 /****************************************************************************
492 Reply to an NT create and X call.
493 ****************************************************************************/
495 void reply_ntcreate_and_X(connection_struct *conn,
496 struct smb_request *req)
501 uint32 file_attributes;
503 uint32 create_disposition;
504 uint32 create_options;
506 SMB_BIG_UINT allocation_size;
507 /* Breakout the oplock request bits so we can set the
508 reply bits separately. */
509 int oplock_request = 0;
511 SMB_OFF_T file_len = 0;
512 SMB_STRUCT_STAT sbuf;
514 files_struct *fsp = NULL;
516 struct timespec c_timespec;
517 struct timespec a_timespec;
518 struct timespec m_timespec;
519 BOOL extended_oplock_granted = False;
521 struct case_semantics_state *case_state = NULL;
522 TALLOC_CTX *ctx = talloc_tos();
524 START_PROFILE(SMBntcreateX);
527 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
531 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
532 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
533 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
534 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
535 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
536 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
537 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
539 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,smb_ntcreate_AllocationSize);
540 #ifdef LARGE_SMB_OFF_T
541 allocation_size |= (((SMB_BIG_UINT)IVAL(req->inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
544 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
545 "file_attributes = 0x%x, share_access = 0x%x, "
546 "create_disposition = 0x%x create_options = 0x%x "
547 "root_dir_fid = 0x%x\n",
549 (unsigned int)access_mask,
550 (unsigned int)file_attributes,
551 (unsigned int)share_access,
552 (unsigned int)create_disposition,
553 (unsigned int)create_options,
554 (unsigned int)root_dir_fid ));
557 * If it's an IPC, use the pipe handler.
561 if (lp_nt_pipe_support()) {
562 do_ntcreate_pipe_open(conn, req);
563 END_PROFILE(SMBntcreateX);
566 reply_doserror(req, ERRDOS, ERRnoaccess);
567 END_PROFILE(SMBntcreateX);
572 if (create_options & FILE_OPEN_BY_FILE_ID) {
573 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
574 END_PROFILE(SMBntcreateX);
582 if(root_dir_fid != 0) {
584 * This filename is relative to a directory fid.
586 char *rel_fname = NULL;
587 files_struct *dir_fsp = file_fsp(
588 SVAL(req->inbuf, smb_ntcreate_RootDirectoryFid));
591 reply_doserror(req, ERRDOS, ERRbadfid);
592 END_PROFILE(SMBntcreateX);
596 if(!dir_fsp->is_directory) {
598 srvstr_get_path(ctx, (char *)req->inbuf,
600 smb_buf(req->inbuf), 0,
601 STR_TERMINATE, &status);
602 if (!NT_STATUS_IS_OK(status)) {
603 reply_nterror(req, status);
604 END_PROFILE(SMBntcreateX);
609 * Check to see if this is a mac fork of some kind.
612 if( is_ntfs_stream_name(fname)) {
614 req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
615 END_PROFILE(SMBntcreateX);
620 we need to handle the case when we get a
621 relative open relative to a file and the
622 pathname is blank - this is a reopen!
623 (hint from demyn plantenberg)
626 reply_doserror(req, ERRDOS, ERRbadfid);
627 END_PROFILE(SMBntcreateX);
631 if (ISDOT(dir_fsp->fsp_name)) {
633 * We're at the toplevel dir, the final file name
634 * must not contain ./, as this is filtered out
635 * normally by srvstr_get_path and unix_convert
636 * explicitly rejects paths containing ./.
638 fname = talloc_strdup(ctx,"");
640 reply_nterror(req, NT_STATUS_NO_MEMORY);
641 END_PROFILE(SMBntcreateX);
645 size_t dir_name_len = strlen(dir_fsp->fsp_name);
648 * Copy in the base directory name.
651 fname = TALLOC_ARRAY(ctx, char, dir_name_len+2);
654 req, NT_STATUS_NO_MEMORY);
655 END_PROFILE(SMBntcreateX);
658 memcpy(fname, dir_fsp->fsp_name, dir_name_len+1);
661 * Ensure it ends in a '/'.
662 * We used TALLOC_SIZE +2 to add space for the '/'.
666 (fname[dir_name_len-1] != '\\') &&
667 (fname[dir_name_len-1] != '/')) {
668 fname[dir_name_len] = '/';
669 fname[dir_name_len+1] = '\0';
673 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &rel_fname,
674 smb_buf(req->inbuf), 0,
675 STR_TERMINATE, &status);
676 if (!NT_STATUS_IS_OK(status)) {
677 reply_nterror(req, status);
678 END_PROFILE(SMBntcreateX);
681 fname = talloc_asprintf(ctx, "%s%s",
686 req, NT_STATUS_NO_MEMORY);
687 END_PROFILE(SMBntcreateX);
691 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
692 smb_buf(req->inbuf), 0,
693 STR_TERMINATE, &status);
694 if (!NT_STATUS_IS_OK(status)) {
695 reply_nterror(req, status);
696 END_PROFILE(SMBntcreateX);
701 * Check to see if this is a mac fork of some kind.
704 if( is_ntfs_stream_name(fname)) {
705 enum FAKE_FILE_TYPE fake_file_type = is_fake_file(fname);
706 if (fake_file_type!=FAKE_FILE_TYPE_NONE) {
708 * Here we go! support for changing the disk quotas --metze
710 * We need to fake up to open this MAGIC QUOTA file
711 * and return a valid FID.
713 * w2k close this file directly after openening
714 * xp also tries a QUERY_FILE_INFO on the file and then close it
716 reply_ntcreate_and_X_quota(conn, req,
717 fake_file_type, fname);
719 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
721 END_PROFILE(SMBntcreateX);
727 * Now contruct the smb_open_mode value from the filename,
728 * desired access and the share access.
730 status = resolve_dfspath(ctx, conn,
731 req->flags2 & FLAGS2_DFS_PATHNAMES,
734 if (!NT_STATUS_IS_OK(status)) {
735 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
736 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
740 reply_nterror(req, status);
742 END_PROFILE(SMBntcreateX);
746 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
747 if (oplock_request) {
748 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
752 * Ordinary file or directory.
756 * Check if POSIX semantics are wanted.
759 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
760 case_state = set_posix_case_semantics(NULL, conn);
761 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
764 status = unix_convert(ctx, conn, fname, False, &fname, NULL, &sbuf);
765 if (!NT_STATUS_IS_OK(status)) {
766 TALLOC_FREE(case_state);
767 reply_nterror(req, status);
768 END_PROFILE(SMBntcreateX);
771 /* All file access must go through check_name() */
772 status = check_name(conn, fname);
773 if (!NT_STATUS_IS_OK(status)) {
774 TALLOC_FREE(case_state);
775 reply_nterror(req, status);
776 END_PROFILE(SMBntcreateX);
780 /* This is the correct thing to do (check every time) but can_delete is
781 expensive (it may have to read the parent directory permissions). So
782 for now we're not doing it unless we have a strong hint the client
783 is really going to delete this file. If the client is forcing FILE_CREATE
784 let the filesystem take care of the permissions. */
786 /* Setting FILE_SHARE_DELETE is the hint. */
788 if (lp_acl_check_permissions(SNUM(conn))
789 && (create_disposition != FILE_CREATE)
790 && (share_access & FILE_SHARE_DELETE)
791 && (access_mask & DELETE_ACCESS)) {
792 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
793 !can_delete_file_in_directory(conn, fname)) {
794 TALLOC_FREE(case_state);
795 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
796 END_PROFILE(SMBntcreateX);
802 /* We need to support SeSecurityPrivilege for this. */
803 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
804 !user_has_privileges(current_user.nt_user_token,
806 TALLOC_FREE(case_state);
807 END_PROFILE(SMBntcreateX);
808 return ERROR_NT(NT_STATUS_PRIVILEGE_NOT_HELD);
813 * If it's a request for a directory open, deal with it separately.
816 if(create_options & FILE_DIRECTORY_FILE) {
818 /* Can't open a temp directory. IFS kit test. */
819 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
820 TALLOC_FREE(case_state);
821 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
822 END_PROFILE(SMBntcreateX);
827 status = open_directory(conn, req, fname, &sbuf,
838 * Ordinary file case.
841 /* NB. We have a potential bug here. If we
842 * cause an oplock break to ourselves, then we
843 * could end up processing filename related
844 * SMB requests whilst we await the oplock
845 * break response. As we may have changed the
846 * filename case semantics to be POSIX-like,
847 * this could mean a filename request could
848 * fail when it should succeed. This is a rare
849 * condition, but eventually we must arrange
850 * to restore the correct case semantics
851 * before issuing an oplock break request to
852 * our client. JRA. */
854 status = open_file_ntcreate(conn, req, fname, &sbuf,
863 /* We cheat here. There are two cases we
864 * care about. One is a directory rename,
865 * where the NT client will attempt to
866 * open the source directory for
867 * DELETE access. Note that when the
868 * NT client does this it does *not*
869 * set the directory bit in the
870 * request packet. This is translated
871 * into a read/write open
872 * request. POSIX states that any open
873 * for write request on a directory
874 * will generate an EISDIR error, so
875 * we can catch this here and open a
876 * pseudo handle that is flagged as a
877 * directory. The second is an open
878 * for a permissions read only, which
879 * we handle in the open_file_stat case. JRA.
882 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
885 * Fail the open if it was explicitly a non-directory
889 if (create_options & FILE_NON_DIRECTORY_FILE) {
890 TALLOC_FREE(case_state);
891 reply_force_nterror(req,
892 NT_STATUS_FILE_IS_A_DIRECTORY);
893 END_PROFILE(SMBntcreateX);
898 status = open_directory(conn, req, fname,
909 TALLOC_FREE(case_state);
911 if (!NT_STATUS_IS_OK(status)) {
912 if (open_was_deferred(req->mid)) {
913 /* We have re-scheduled this call. */
914 END_PROFILE(SMBntcreateX);
917 reply_openerror(req, status);
918 END_PROFILE(SMBntcreateX);
922 file_len = sbuf.st_size;
923 fattr = dos_mode(conn,fname,&sbuf);
925 fattr = FILE_ATTRIBUTE_NORMAL;
927 if (!fsp->is_directory && (fattr & aDIR)) {
928 close_file(fsp,ERROR_CLOSE);
929 reply_doserror(req, ERRDOS, ERRnoaccess);
930 END_PROFILE(SMBntcreateX);
934 /* Save the requested allocation size. */
935 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
936 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
937 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
938 if (fsp->is_directory) {
939 close_file(fsp,ERROR_CLOSE);
940 /* Can't set allocation size on a directory. */
941 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
942 END_PROFILE(SMBntcreateX);
945 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
946 close_file(fsp,ERROR_CLOSE);
947 reply_nterror(req, NT_STATUS_DISK_FULL);
948 END_PROFILE(SMBntcreateX);
952 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
957 * If the caller set the extended oplock request bit
958 * and we granted one (by whatever means) - set the
959 * correct bit for extended oplock reply.
962 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
963 extended_oplock_granted = True;
966 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
967 extended_oplock_granted = True;
970 if (flags & EXTENDED_RESPONSE_REQUIRED) {
971 /* This is very strange. We
972 * return 50 words, but only set
973 * the wcnt to 42 ? It's definately
974 * what happens on the wire....
976 reply_outbuf(req, 50, 0);
977 SCVAL(req->outbuf,smb_wct,42);
979 reply_outbuf(req, 34, 0);
982 p = (char *)req->outbuf + smb_vwv2;
985 * Currently as we don't support level II oplocks we just report
986 * exclusive & batch here.
989 if (extended_oplock_granted) {
990 if (flags & REQUEST_BATCH_OPLOCK) {
991 SCVAL(p,0, BATCH_OPLOCK_RETURN);
993 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
995 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
996 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
998 SCVAL(p,0,NO_OPLOCK_RETURN);
1002 SSVAL(p,0,fsp->fnum);
1004 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1005 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1012 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1013 a_timespec = get_atimespec(&sbuf);
1014 m_timespec = get_mtimespec(&sbuf);
1016 if (lp_dos_filetime_resolution(SNUM(conn))) {
1017 dos_filetime_timespec(&c_timespec);
1018 dos_filetime_timespec(&a_timespec);
1019 dos_filetime_timespec(&m_timespec);
1022 put_long_date_timespec(p, c_timespec); /* create time. */
1024 put_long_date_timespec(p, a_timespec); /* access time */
1026 put_long_date_timespec(p, m_timespec); /* write time */
1028 put_long_date_timespec(p, m_timespec); /* change time */
1030 SIVAL(p,0,fattr); /* File Attributes. */
1032 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1034 SOFF_T(p,0,file_len);
1036 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1040 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1042 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1045 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1046 perms = FILE_GENERIC_ALL;
1048 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1053 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
1056 END_PROFILE(SMBntcreateX);
1060 /****************************************************************************
1061 Reply to a NT_TRANSACT_CREATE call to open a pipe.
1062 ****************************************************************************/
1064 static void do_nt_transact_create_pipe(connection_struct *conn,
1065 struct smb_request *req,
1066 uint16 **ppsetup, uint32 setup_count,
1067 char **ppparams, uint32 parameter_count,
1068 char **ppdata, uint32 data_count)
1071 char *params = *ppparams;
1077 TALLOC_CTX *ctx = talloc_tos();
1080 * Ensure minimum number of parameters sent.
1083 if(parameter_count < 54) {
1084 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1085 reply_doserror(req, ERRDOS, ERRnoaccess);
1089 flags = IVAL(params,0);
1091 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
1092 parameter_count-53, STR_TERMINATE,
1094 if (!NT_STATUS_IS_OK(status)) {
1095 reply_nterror(req, status);
1099 nt_open_pipe(fname, conn, req, &pnum);
1106 /* Realloc the size of parameters and data we will return */
1107 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1108 /* Extended response is 32 more byyes. */
1113 params = nttrans_realloc(ppparams, param_len);
1114 if(params == NULL) {
1115 reply_doserror(req, ERRDOS, ERRnomem);
1120 SCVAL(p,0,NO_OPLOCK_RETURN);
1125 SIVAL(p,0,FILE_WAS_OPENED);
1129 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1132 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1134 SSVAL(p,2, 0x5FF); /* ? */
1137 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1139 SIVAL(p,0,FILE_GENERIC_ALL);
1141 * For pipes W2K3 seems to return
1143 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
1145 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
1148 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1150 /* Send the required number of replies */
1151 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1156 /****************************************************************************
1157 Internal fn to set security descriptors.
1158 ****************************************************************************/
1160 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1163 SEC_DESC *psd = NULL;
1164 TALLOC_CTX *mem_ctx;
1167 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
1168 return NT_STATUS_OK;
1172 * Init the parse struct we will unmarshall from.
1175 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1176 DEBUG(0,("set_sd: talloc_init failed.\n"));
1177 return NT_STATUS_NO_MEMORY;
1180 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1183 * Setup the prs_struct to point at the memory we just
1187 prs_give_memory( &pd, data, sd_len, False);
1190 * Finally, unmarshall from the data buffer.
1193 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1194 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1196 * Return access denied for want of a better error message..
1198 talloc_destroy(mem_ctx);
1199 return NT_STATUS_NO_MEMORY;
1202 if (psd->owner_sid==0) {
1203 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1205 if (psd->group_sid==0) {
1206 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1209 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1212 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1215 status = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1217 talloc_destroy(mem_ctx);
1221 /****************************************************************************
1222 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1223 ****************************************************************************/
1225 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1227 struct ea_list *ea_list_head = NULL;
1230 if (data_size < 4) {
1234 while (offset + 4 <= data_size) {
1235 size_t next_offset = IVAL(pdata,offset);
1236 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1242 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1243 if (next_offset == 0) {
1246 offset += next_offset;
1249 return ea_list_head;
1252 /****************************************************************************
1253 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1254 ****************************************************************************/
1256 static void call_nt_transact_create(connection_struct *conn,
1257 struct smb_request *req,
1258 uint16 **ppsetup, uint32 setup_count,
1259 char **ppparams, uint32 parameter_count,
1260 char **ppdata, uint32 data_count,
1261 uint32 max_data_count)
1264 char *params = *ppparams;
1265 char *data = *ppdata;
1266 /* Breakout the oplock request bits so we can set the reply bits separately. */
1267 int oplock_request = 0;
1269 SMB_OFF_T file_len = 0;
1270 SMB_STRUCT_STAT sbuf;
1272 files_struct *fsp = NULL;
1274 BOOL extended_oplock_granted = False;
1277 uint32 file_attributes;
1278 uint32 share_access;
1279 uint32 create_disposition;
1280 uint32 create_options;
1283 uint16 root_dir_fid;
1284 struct timespec c_timespec;
1285 struct timespec a_timespec;
1286 struct timespec m_timespec;
1287 struct ea_list *ea_list = NULL;
1291 struct case_semantics_state *case_state = NULL;
1292 TALLOC_CTX *ctx = talloc_tos();
1294 DEBUG(5,("call_nt_transact_create\n"));
1297 * If it's an IPC, use the pipe handler.
1301 if (lp_nt_pipe_support()) {
1302 do_nt_transact_create_pipe(
1304 ppsetup, setup_count,
1305 ppparams, parameter_count,
1306 ppdata, data_count);
1309 reply_doserror(req, ERRDOS, ERRnoaccess);
1315 * Ensure minimum number of parameters sent.
1318 if(parameter_count < 54) {
1319 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1320 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1324 flags = IVAL(params,0);
1325 access_mask = IVAL(params,8);
1326 file_attributes = IVAL(params,20);
1327 share_access = IVAL(params,24);
1328 create_disposition = IVAL(params,28);
1329 create_options = IVAL(params,32);
1330 sd_len = IVAL(params,36);
1331 ea_len = IVAL(params,40);
1332 root_dir_fid = (uint16)IVAL(params,4);
1334 /* Ensure the data_len is correct for the sd and ea values given. */
1335 if ((ea_len + sd_len > data_count) ||
1336 (ea_len > data_count) || (sd_len > data_count) ||
1337 (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1338 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1339 (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1340 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1345 if (!lp_ea_support(SNUM(conn))) {
1346 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1347 (unsigned int)ea_len ));
1348 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1353 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1354 (unsigned int)ea_len ));
1355 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1360 if (create_options & FILE_OPEN_BY_FILE_ID) {
1361 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1366 * Get the file name.
1369 if(root_dir_fid != 0) {
1371 * This filename is relative to a directory fid.
1373 char *tmpname = NULL;
1374 files_struct *dir_fsp = file_fsp(SVAL(params,4));
1377 reply_doserror(req, ERRDOS, ERRbadfid);
1381 if(!dir_fsp->is_directory) {
1382 srvstr_get_path(ctx, params, req->flags2, &fname,
1384 parameter_count-53, STR_TERMINATE,
1386 if (!NT_STATUS_IS_OK(status)) {
1387 reply_nterror(req, status);
1392 * Check to see if this is a mac fork of some kind.
1395 if( is_ntfs_stream_name(fname)) {
1397 NT_STATUS_OBJECT_PATH_NOT_FOUND);
1401 reply_doserror(req, ERRDOS, ERRbadfid);
1405 if (ISDOT(dir_fsp->fsp_name)) {
1407 * We're at the toplevel dir, the final file name
1408 * must not contain ./, as this is filtered out
1409 * normally by srvstr_get_path and unix_convert
1410 * explicitly rejects paths containing ./.
1412 fname = talloc_strdup(ctx,"");
1414 reply_nterror(req, NT_STATUS_NO_MEMORY);
1418 size_t dir_name_len = strlen(dir_fsp->fsp_name);
1421 * Copy in the base directory name.
1424 fname = TALLOC_ARRAY(ctx, char, dir_name_len+2);
1426 reply_nterror(req, NT_STATUS_NO_MEMORY);
1429 memcpy(fname, dir_fsp->fsp_name, dir_name_len+1);
1432 * Ensure it ends in a '/'.
1433 * We used TALLOC_SIZE +2 to add space for the '/'.
1437 (fname[dir_name_len-1] != '\\') &&
1438 (fname[dir_name_len-1] != '/')) {
1439 fname[dir_name_len] = '/';
1440 fname[dir_name_len+1] = '\0';
1444 srvstr_get_path(ctx, params, req->flags2, &tmpname,
1446 parameter_count-53, STR_TERMINATE,
1448 if (!NT_STATUS_IS_OK(status)) {
1449 reply_nterror(req, status);
1452 fname = talloc_asprintf(ctx, "%s%s",
1457 req, NT_STATUS_NO_MEMORY);
1461 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
1463 STR_TERMINATE, &status);
1464 if (!NT_STATUS_IS_OK(status)) {
1465 reply_nterror(req, status);
1470 * Check to see if this is a mac fork of some kind.
1473 if( is_ntfs_stream_name(fname)) {
1474 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
1479 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1480 if (oplock_request) {
1481 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1485 * Ordinary file or directory.
1489 * Check if POSIX semantics are wanted.
1492 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1493 case_state = set_posix_case_semantics(NULL, conn);
1494 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1497 status = resolve_dfspath(ctx, conn,
1498 req->flags2 & FLAGS2_DFS_PATHNAMES,
1501 if (!NT_STATUS_IS_OK(status)) {
1502 TALLOC_FREE(case_state);
1503 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1504 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1505 ERRSRV, ERRbadpath);
1508 reply_nterror(req, status);
1512 status = unix_convert(ctx, conn, fname, False, &fname, NULL, &sbuf);
1513 if (!NT_STATUS_IS_OK(status)) {
1514 TALLOC_FREE(case_state);
1515 reply_nterror(req, status);
1518 /* All file access must go through check_name() */
1519 status = check_name(conn, fname);
1520 if (!NT_STATUS_IS_OK(status)) {
1521 TALLOC_FREE(case_state);
1522 reply_nterror(req, status);
1526 /* This is the correct thing to do (check every time) but can_delete is
1527 expensive (it may have to read the parent directory permissions). So
1528 for now we're not doing it unless we have a strong hint the client
1529 is really going to delete this file. If the client is forcing FILE_CREATE
1530 let the filesystem take care of the permissions. */
1532 /* Setting FILE_SHARE_DELETE is the hint. */
1534 if (lp_acl_check_permissions(SNUM(conn))
1535 && (create_disposition != FILE_CREATE)
1536 && (share_access & FILE_SHARE_DELETE)
1537 && (access_mask & DELETE_ACCESS)) {
1538 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
1539 !can_delete_file_in_directory(conn, fname)) {
1540 TALLOC_FREE(case_state);
1541 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1547 /* We need to support SeSecurityPrivilege for this. */
1548 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
1549 !user_has_privileges(current_user.nt_user_token,
1551 TALLOC_FREE(case_state);
1552 reply_nterror(req, NT_STATUS_PRIVILEGE_NOT_HELD);
1558 pdata = data + sd_len;
1560 /* We have already checked that ea_len <= data_count here. */
1561 ea_list = read_nttrans_ea_list(talloc_tos(), pdata,
1564 TALLOC_FREE(case_state);
1565 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1571 * If it's a request for a directory open, deal with it separately.
1574 if(create_options & FILE_DIRECTORY_FILE) {
1576 /* Can't open a temp directory. IFS kit test. */
1577 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1578 TALLOC_FREE(case_state);
1579 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1584 * We will get a create directory here if the Win32
1585 * app specified a security descriptor in the
1586 * CreateDirectory() call.
1590 status = open_directory(conn, req, fname, &sbuf,
1600 * Ordinary file case.
1603 status = open_file_ntcreate(conn,req,fname,&sbuf,
1612 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
1615 * Fail the open if it was explicitly a non-directory file.
1618 if (create_options & FILE_NON_DIRECTORY_FILE) {
1619 TALLOC_FREE(case_state);
1620 reply_force_nterror(
1622 NT_STATUS_FILE_IS_A_DIRECTORY);
1627 status = open_directory(conn, req, fname,
1638 TALLOC_FREE(case_state);
1640 if(!NT_STATUS_IS_OK(status)) {
1641 if (open_was_deferred(req->mid)) {
1642 /* We have re-scheduled this call. */
1645 reply_openerror(req, status);
1650 * According to the MS documentation, the only time the security
1651 * descriptor is applied to the opened file is iff we *created* the
1652 * file; an existing file stays the same.
1654 * Also, it seems (from observation) that you can open the file with
1655 * any access mask but you can still write the sd. We need to override
1656 * the granted access before we call set_sd
1657 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1660 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1661 uint32 saved_access_mask = fsp->access_mask;
1663 /* We have already checked that sd_len <= data_count here. */
1665 fsp->access_mask = FILE_GENERIC_ALL;
1667 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1668 if (!NT_STATUS_IS_OK(status)) {
1669 close_file(fsp,ERROR_CLOSE);
1670 TALLOC_FREE(case_state);
1671 reply_nterror(req, status);
1674 fsp->access_mask = saved_access_mask;
1677 if (ea_len && (info == FILE_WAS_CREATED)) {
1678 status = set_ea(conn, fsp, fname, ea_list);
1679 if (!NT_STATUS_IS_OK(status)) {
1680 close_file(fsp,ERROR_CLOSE);
1681 TALLOC_FREE(case_state);
1682 reply_nterror(req, status);
1687 TALLOC_FREE(case_state);
1689 file_len = sbuf.st_size;
1690 fattr = dos_mode(conn,fname,&sbuf);
1692 fattr = FILE_ATTRIBUTE_NORMAL;
1694 if (!fsp->is_directory && (fattr & aDIR)) {
1695 close_file(fsp,ERROR_CLOSE);
1696 reply_doserror(req, ERRDOS, ERRnoaccess);
1700 /* Save the requested allocation size. */
1701 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1702 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1703 #ifdef LARGE_SMB_OFF_T
1704 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1706 if (allocation_size && (allocation_size > file_len)) {
1707 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1708 if (fsp->is_directory) {
1709 close_file(fsp,ERROR_CLOSE);
1710 /* Can't set allocation size on a directory. */
1711 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1714 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1715 close_file(fsp,ERROR_CLOSE);
1716 reply_nterror(req, NT_STATUS_DISK_FULL);
1720 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1725 * If the caller set the extended oplock request bit
1726 * and we granted one (by whatever means) - set the
1727 * correct bit for extended oplock reply.
1730 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1731 extended_oplock_granted = True;
1734 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1735 extended_oplock_granted = True;
1738 /* Realloc the size of parameters and data we will return */
1739 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1740 /* Extended response is 32 more byyes. */
1745 params = nttrans_realloc(ppparams, param_len);
1746 if(params == NULL) {
1747 reply_doserror(req, ERRDOS, ERRnomem);
1752 if (extended_oplock_granted) {
1753 if (flags & REQUEST_BATCH_OPLOCK) {
1754 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1756 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1758 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1759 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1761 SCVAL(p,0,NO_OPLOCK_RETURN);
1765 SSVAL(p,0,fsp->fnum);
1767 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1768 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1775 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1776 a_timespec = get_atimespec(&sbuf);
1777 m_timespec = get_mtimespec(&sbuf);
1779 if (lp_dos_filetime_resolution(SNUM(conn))) {
1780 dos_filetime_timespec(&c_timespec);
1781 dos_filetime_timespec(&a_timespec);
1782 dos_filetime_timespec(&m_timespec);
1785 put_long_date_timespec(p, c_timespec); /* create time. */
1787 put_long_date_timespec(p, a_timespec); /* access time */
1789 put_long_date_timespec(p, m_timespec); /* write time */
1791 put_long_date_timespec(p, m_timespec); /* change time */
1793 SIVAL(p,0,fattr); /* File Attributes. */
1795 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1797 SOFF_T(p,0,file_len);
1799 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1803 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1805 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1808 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1809 perms = FILE_GENERIC_ALL;
1811 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1816 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1818 /* Send the required number of replies */
1819 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1824 /****************************************************************************
1825 Reply to a NT CANCEL request.
1826 conn POINTER CAN BE NULL HERE !
1827 ****************************************************************************/
1829 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1832 * Go through and cancel any pending change notifies.
1835 START_PROFILE(SMBntcancel);
1836 remove_pending_change_notify_requests_by_mid(req->mid);
1837 remove_pending_lock_requests_by_mid(req->mid);
1838 srv_cancel_sign_response(req->mid);
1840 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1842 END_PROFILE(SMBntcancel);
1846 /****************************************************************************
1848 ****************************************************************************/
1850 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1851 connection_struct *conn,
1852 struct smb_request *req,
1853 const char *oldname_in,
1854 const char *newname_in,
1857 SMB_STRUCT_STAT sbuf1, sbuf2;
1858 char *oldname = NULL;
1859 char *newname = NULL;
1860 char *last_component_oldname = NULL;
1861 char *last_component_newname = NULL;
1862 files_struct *fsp1,*fsp2;
1866 NTSTATUS status = NT_STATUS_OK;
1871 if (!CAN_WRITE(conn)) {
1872 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1875 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1876 &last_component_oldname, &sbuf1);
1877 if (!NT_STATUS_IS_OK(status)) {
1881 status = check_name(conn, oldname);
1882 if (!NT_STATUS_IS_OK(status)) {
1886 /* Source must already exist. */
1887 if (!VALID_STAT(sbuf1)) {
1888 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1890 /* Ensure attributes match. */
1891 fattr = dos_mode(conn,oldname,&sbuf1);
1892 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1893 return NT_STATUS_NO_SUCH_FILE;
1896 status = unix_convert(ctx, conn, newname_in, False, &newname,
1897 &last_component_newname, &sbuf2);
1898 if (!NT_STATUS_IS_OK(status)) {
1902 status = check_name(conn, newname);
1903 if (!NT_STATUS_IS_OK(status)) {
1907 /* Disallow if newname already exists. */
1908 if (VALID_STAT(sbuf2)) {
1909 return NT_STATUS_OBJECT_NAME_COLLISION;
1912 /* No links from a directory. */
1913 if (S_ISDIR(sbuf1.st_mode)) {
1914 return NT_STATUS_FILE_IS_A_DIRECTORY;
1917 /* Ensure this is within the share. */
1918 status = check_reduced_name(conn, oldname);
1919 if (!NT_STATUS_IS_OK(status)) {
1923 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1926 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1927 FILE_READ_DATA, /* Read-only. */
1928 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1930 0, /* No create options. */
1931 FILE_ATTRIBUTE_NORMAL,
1935 if (!NT_STATUS_IS_OK(status)) {
1939 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1940 FILE_WRITE_DATA, /* Read-only. */
1941 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1943 0, /* No create options. */
1948 if (!NT_STATUS_IS_OK(status)) {
1949 close_file(fsp1,ERROR_CLOSE);
1953 if (sbuf1.st_size) {
1954 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1958 * As we are opening fsp1 read-only we only expect
1959 * an error on close on fsp2 if we are out of space.
1960 * Thus we don't look at the error return from the
1963 close_file(fsp1,NORMAL_CLOSE);
1965 /* Ensure the modtime is set correctly on the destination file. */
1966 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1968 status = close_file(fsp2,NORMAL_CLOSE);
1970 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1971 creates the file. This isn't the correct thing to do in the copy
1973 file_set_dosmode(conn, newname, fattr, &sbuf2,
1974 parent_dirname(newname));
1976 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1977 return NT_STATUS_DISK_FULL;
1980 if (!NT_STATUS_IS_OK(status)) {
1981 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1982 nt_errstr(status), oldname, newname));
1987 /****************************************************************************
1988 Reply to a NT rename request.
1989 ****************************************************************************/
1991 void reply_ntrename(connection_struct *conn, struct smb_request *req)
1993 char *oldname = NULL;
1994 char *newname = NULL;
1997 BOOL src_has_wcard = False;
1998 BOOL dest_has_wcard = False;
2001 TALLOC_CTX *ctx = talloc_tos();
2003 START_PROFILE(SMBntrename);
2006 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2007 END_PROFILE(SMBntrename);
2011 attrs = SVAL(req->inbuf,smb_vwv0);
2012 rename_type = SVAL(req->inbuf,smb_vwv1);
2014 p = smb_buf(req->inbuf) + 1;
2015 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
2016 0, STR_TERMINATE, &status,
2018 if (!NT_STATUS_IS_OK(status)) {
2019 reply_nterror(req, status);
2020 END_PROFILE(SMBntrename);
2024 if( is_ntfs_stream_name(oldname)) {
2025 /* Can't rename a stream. */
2026 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2027 END_PROFILE(SMBntrename);
2031 if (ms_has_wild(oldname)) {
2032 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
2033 END_PROFILE(SMBntrename);
2038 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
2039 0, STR_TERMINATE, &status,
2041 if (!NT_STATUS_IS_OK(status)) {
2042 reply_nterror(req, status);
2043 END_PROFILE(SMBntrename);
2047 status = resolve_dfspath(ctx, conn,
2048 req->flags2 & FLAGS2_DFS_PATHNAMES,
2051 if (!NT_STATUS_IS_OK(status)) {
2052 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2053 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2054 ERRSRV, ERRbadpath);
2055 END_PROFILE(SMBntrename);
2058 reply_nterror(req, status);
2059 END_PROFILE(SMBntrename);
2063 status = resolve_dfspath(ctx, conn,
2064 req->flags2 & FLAGS2_DFS_PATHNAMES,
2067 if (!NT_STATUS_IS_OK(status)) {
2068 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2069 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2070 ERRSRV, ERRbadpath);
2071 END_PROFILE(SMBntrename);
2074 reply_nterror(req, status);
2075 END_PROFILE(SMBntrename);
2079 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
2081 switch(rename_type) {
2082 case RENAME_FLAG_RENAME:
2083 status = rename_internals(ctx, conn, req, oldname,
2084 newname, attrs, False, src_has_wcard,
2087 case RENAME_FLAG_HARD_LINK:
2088 if (src_has_wcard || dest_has_wcard) {
2090 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2092 status = hardlink_internals(ctx,
2098 case RENAME_FLAG_COPY:
2099 if (src_has_wcard || dest_has_wcard) {
2101 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2103 status = copy_internals(ctx, conn, req, oldname,
2107 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
2108 status = NT_STATUS_INVALID_PARAMETER;
2111 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
2115 if (!NT_STATUS_IS_OK(status)) {
2116 if (open_was_deferred(req->mid)) {
2117 /* We have re-scheduled this call. */
2118 END_PROFILE(SMBntrename);
2122 reply_nterror(req, status);
2123 END_PROFILE(SMBntrename);
2127 reply_outbuf(req, 0, 0);
2129 END_PROFILE(SMBntrename);
2133 /****************************************************************************
2134 Reply to a notify change - queue the request and
2135 don't allow a directory to be opened.
2136 ****************************************************************************/
2138 static void call_nt_transact_notify_change(connection_struct *conn,
2139 struct smb_request *req,
2143 uint32 parameter_count,
2144 char **ppdata, uint32 data_count,
2145 uint32 max_data_count,
2146 uint32 max_param_count)
2148 uint16 *setup = *ppsetup;
2154 if(setup_count < 6) {
2155 reply_doserror(req, ERRDOS, ERRbadfunc);
2159 fsp = file_fsp(SVAL(setup,4));
2160 filter = IVAL(setup, 0);
2161 recursive = (SVAL(setup, 6) != 0) ? True : False;
2163 DEBUG(3,("call_nt_transact_notify_change\n"));
2166 reply_doserror(req, ERRDOS, ERRbadfid);
2171 char *filter_string;
2173 if (!(filter_string = notify_filter_string(NULL, filter))) {
2174 reply_nterror(req,NT_STATUS_NO_MEMORY);
2178 DEBUG(3,("call_nt_transact_notify_change: notify change "
2179 "called on %s, filter = %s, recursive = %d\n",
2180 fsp->fsp_name, filter_string, recursive));
2182 TALLOC_FREE(filter_string);
2185 if((!fsp->is_directory) || (conn != fsp->conn)) {
2186 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2190 if (fsp->notify == NULL) {
2192 status = change_notify_create(fsp, filter, recursive);
2194 if (!NT_STATUS_IS_OK(status)) {
2195 DEBUG(10, ("change_notify_create returned %s\n",
2196 nt_errstr(status)));
2197 reply_nterror(req, status);
2202 if (fsp->notify->num_changes != 0) {
2205 * We've got changes pending, respond immediately
2209 * TODO: write a torture test to check the filtering behaviour
2213 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
2216 * change_notify_reply() above has independently sent its
2223 * No changes pending, queue the request
2226 status = change_notify_add_request(req->inbuf, max_param_count, filter,
2228 if (!NT_STATUS_IS_OK(status)) {
2229 reply_nterror(req, status);
2234 /****************************************************************************
2235 Reply to an NT transact rename command.
2236 ****************************************************************************/
2238 static void call_nt_transact_rename(connection_struct *conn,
2239 struct smb_request *req,
2240 uint16 **ppsetup, uint32 setup_count,
2241 char **ppparams, uint32 parameter_count,
2242 char **ppdata, uint32 data_count,
2243 uint32 max_data_count)
2245 char *params = *ppparams;
2246 char *new_name = NULL;
2247 files_struct *fsp = NULL;
2248 BOOL replace_if_exists = False;
2249 BOOL dest_has_wcard = False;
2251 TALLOC_CTX *ctx = talloc_tos();
2253 if(parameter_count < 5) {
2254 reply_doserror(req, ERRDOS, ERRbadfunc);
2258 fsp = file_fsp(SVAL(params, 0));
2259 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2260 if (!check_fsp(conn, req, fsp, ¤t_user)) {
2263 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
2264 parameter_count - 4,
2265 STR_TERMINATE, &status, &dest_has_wcard);
2266 if (!NT_STATUS_IS_OK(status)) {
2267 reply_nterror(req, status);
2271 status = rename_internals(ctx,
2281 if (!NT_STATUS_IS_OK(status)) {
2282 if (open_was_deferred(req->mid)) {
2283 /* We have re-scheduled this call. */
2286 reply_nterror(req, status);
2291 * Rename was successful.
2293 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2295 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2296 fsp->fsp_name, new_name));
2301 /******************************************************************************
2302 Fake up a completely empty SD.
2303 *******************************************************************************/
2305 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2309 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2311 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2318 /****************************************************************************
2319 Reply to query a security descriptor.
2320 ****************************************************************************/
2322 static void call_nt_transact_query_security_desc(connection_struct *conn,
2323 struct smb_request *req,
2327 uint32 parameter_count,
2330 uint32 max_data_count)
2332 char *params = *ppparams;
2333 char *data = *ppdata;
2335 SEC_DESC *psd = NULL;
2337 uint32 security_info_wanted;
2338 TALLOC_CTX *mem_ctx;
2339 files_struct *fsp = NULL;
2341 if(parameter_count < 8) {
2342 reply_doserror(req, ERRDOS, ERRbadfunc);
2346 fsp = file_fsp(SVAL(params,0));
2348 reply_doserror(req, ERRDOS, ERRbadfid);
2352 security_info_wanted = IVAL(params,4);
2354 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2355 (unsigned int)security_info_wanted ));
2357 params = nttrans_realloc(ppparams, 4);
2358 if(params == NULL) {
2359 reply_doserror(req, ERRDOS, ERRnomem);
2363 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2364 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2365 reply_doserror(req, ERRDOS, ERRnomem);
2370 * Get the permissions to return.
2373 if (!lp_nt_acl_support(SNUM(conn))) {
2374 sd_size = get_null_nt_acl(mem_ctx, &psd);
2376 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
2380 talloc_destroy(mem_ctx);
2381 reply_unixerror(req, ERRDOS, ERRnoaccess);
2385 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2387 SIVAL(params,0,(uint32)sd_size);
2389 if(max_data_count < sd_size) {
2391 send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
2392 params, 4, *ppdata, 0);
2393 talloc_destroy(mem_ctx);
2398 * Allocate the data we will point this at.
2401 data = nttrans_realloc(ppdata, sd_size);
2403 talloc_destroy(mem_ctx);
2404 reply_doserror(req, ERRDOS, ERRnomem);
2409 * Init the parse struct we will marshall into.
2412 prs_init(&pd, 0, mem_ctx, MARSHALL);
2415 * Setup the prs_struct to point at the memory we just
2419 prs_give_memory( &pd, data, (uint32)sd_size, False);
2422 * Finally, linearize into the outgoing buffer.
2425 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2426 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2427 security descriptor.\n"));
2429 * Return access denied for want of a better error message..
2431 talloc_destroy(mem_ctx);
2432 reply_unixerror(req, ERRDOS, ERRnoaccess);
2437 * Now we can delete the security descriptor.
2440 talloc_destroy(mem_ctx);
2442 send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2446 /****************************************************************************
2447 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2448 ****************************************************************************/
2450 static void call_nt_transact_set_security_desc(connection_struct *conn,
2451 struct smb_request *req,
2455 uint32 parameter_count,
2458 uint32 max_data_count)
2460 char *params= *ppparams;
2461 char *data = *ppdata;
2462 files_struct *fsp = NULL;
2463 uint32 security_info_sent = 0;
2466 if(parameter_count < 8) {
2467 reply_doserror(req, ERRDOS, ERRbadfunc);
2471 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
2472 reply_doserror(req, ERRDOS, ERRbadfid);
2476 if(!lp_nt_acl_support(SNUM(conn))) {
2480 security_info_sent = IVAL(params,4);
2482 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2483 (unsigned int)security_info_sent ));
2485 if (data_count == 0) {
2486 reply_doserror(req, ERRDOS, ERRnoaccess);
2490 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2491 reply_nterror(req, nt_status);
2497 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2501 /****************************************************************************
2503 ****************************************************************************/
2505 static void call_nt_transact_ioctl(connection_struct *conn,
2506 struct smb_request *req,
2507 uint16 **ppsetup, uint32 setup_count,
2508 char **ppparams, uint32 parameter_count,
2509 char **ppdata, uint32 data_count,
2510 uint32 max_data_count)
2517 static BOOL logged_message;
2518 char *pdata = *ppdata;
2520 if (setup_count != 8) {
2521 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2522 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2526 function = IVAL(*ppsetup, 0);
2527 fidnum = SVAL(*ppsetup, 4);
2528 isFSctl = CVAL(*ppsetup, 6);
2529 compfilter = CVAL(*ppsetup, 7);
2531 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2532 function, fidnum, isFSctl, compfilter));
2534 fsp=file_fsp(fidnum);
2535 /* this check is done in each implemented function case for now
2536 because I don't want to break anything... --metze
2537 FSP_BELONGS_CONN(fsp,conn);*/
2540 case FSCTL_SET_SPARSE:
2541 /* pretend this succeeded - tho strictly we should
2542 mark the file sparse (if the local fs supports it)
2543 so we can know if we need to pre-allocate or not */
2545 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2546 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2549 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2551 unsigned char objid[16];
2553 /* This should return the object-id on this file.
2554 * I think I'll make this be the inode+dev. JRA.
2557 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2559 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2564 pdata = nttrans_realloc(ppdata, data_count);
2565 if (pdata == NULL) {
2566 reply_nterror(req, NT_STATUS_NO_MEMORY);
2569 push_file_id_16(pdata, &fsp->file_id);
2570 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2571 push_file_id_16(pdata+32, &fsp->file_id);
2572 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2577 case FSCTL_GET_REPARSE_POINT:
2578 /* pretend this fail - my winXP does it like this
2582 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2583 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2586 case FSCTL_SET_REPARSE_POINT:
2587 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2591 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2592 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2595 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2598 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2599 * and return their volume names. If max_data_count is 16, then it is just
2600 * asking for the number of volumes and length of the combined names.
2602 * pdata is the data allocated by our caller, but that uses
2603 * total_data_count (which is 0 in our case) rather than max_data_count.
2604 * Allocate the correct amount and return the pointer to let
2605 * it be deallocated when we return.
2607 SHADOW_COPY_DATA *shadow_data = NULL;
2608 TALLOC_CTX *shadow_mem_ctx = NULL;
2609 BOOL labels = False;
2610 uint32 labels_data_count = 0;
2614 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2618 if (max_data_count < 16) {
2619 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2621 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2625 if (max_data_count > 16) {
2629 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2630 if (shadow_mem_ctx == NULL) {
2631 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2632 reply_nterror(req, NT_STATUS_NO_MEMORY);
2636 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2637 if (shadow_data == NULL) {
2638 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2639 talloc_destroy(shadow_mem_ctx);
2640 reply_nterror(req, NT_STATUS_NO_MEMORY);
2644 shadow_data->mem_ctx = shadow_mem_ctx;
2647 * Call the VFS routine to actually do the work.
2649 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2650 talloc_destroy(shadow_data->mem_ctx);
2651 if (errno == ENOSYS) {
2652 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2653 conn->connectpath));
2654 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2657 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2658 conn->connectpath));
2659 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2664 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2669 data_count = 12+labels_data_count+4;
2672 if (max_data_count<data_count) {
2673 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2674 max_data_count,data_count));
2675 talloc_destroy(shadow_data->mem_ctx);
2676 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2680 pdata = nttrans_realloc(ppdata, data_count);
2681 if (pdata == NULL) {
2682 talloc_destroy(shadow_data->mem_ctx);
2683 reply_nterror(req, NT_STATUS_NO_MEMORY);
2689 /* num_volumes 4 bytes */
2690 SIVAL(pdata,0,shadow_data->num_volumes);
2693 /* num_labels 4 bytes */
2694 SIVAL(pdata,4,shadow_data->num_volumes);
2697 /* needed_data_count 4 bytes */
2698 SIVAL(pdata,8,labels_data_count);
2702 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2703 shadow_data->num_volumes,fsp->fsp_name));
2704 if (labels && shadow_data->labels) {
2705 for (i=0;i<shadow_data->num_volumes;i++) {
2706 srvstr_push(pdata, req->flags2,
2707 cur_pdata, shadow_data->labels[i],
2708 2*sizeof(SHADOW_COPY_LABEL),
2709 STR_UNICODE|STR_TERMINATE);
2710 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2711 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2715 talloc_destroy(shadow_data->mem_ctx);
2717 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2723 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2725 /* pretend this succeeded -
2727 * we have to send back a list with all files owned by this SID
2729 * but I have to check that --metze
2733 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2735 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2737 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2741 /* unknown 4 bytes: this is not the length of the sid :-( */
2742 /*unknown = IVAL(pdata,0);*/
2744 sid_parse(pdata+4,sid_len,&sid);
2745 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2747 if (!sid_to_uid(&sid, &uid)) {
2748 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2749 sid_string_static(&sid),(unsigned long)sid_len));
2753 /* we can take a look at the find source :-)
2755 * find ./ -uid $uid -name '*' is what we need here
2758 * and send 4bytes len and then NULL terminated unicode strings
2761 * but I don't know how to deal with the paged results
2762 * (maybe we can hang the result anywhere in the fsp struct)
2764 * we don't send all files at once
2765 * and at the next we should *not* start from the beginning,
2766 * so we have to cache the result
2771 /* this works for now... */
2772 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2776 if (!logged_message) {
2777 logged_message = True; /* Only print this once... */
2778 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2783 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2787 #ifdef HAVE_SYS_QUOTAS
2788 /****************************************************************************
2789 Reply to get user quota
2790 ****************************************************************************/
2792 static void call_nt_transact_get_user_quota(connection_struct *conn,
2793 struct smb_request *req,
2797 uint32 parameter_count,
2800 uint32 max_data_count)
2802 NTSTATUS nt_status = NT_STATUS_OK;
2803 char *params = *ppparams;
2804 char *pdata = *ppdata;
2806 int data_len=0,param_len=0;
2809 files_struct *fsp = NULL;
2813 BOOL start_enum = True;
2814 SMB_NTQUOTA_STRUCT qt;
2815 SMB_NTQUOTA_LIST *tmp_list;
2816 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2821 if (current_user.ut.uid != 0) {
2822 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2823 lp_servicename(SNUM(conn)),conn->user));
2824 reply_doserror(req, ERRDOS, ERRnoaccess);
2829 * Ensure minimum number of parameters sent.
2832 if (parameter_count < 4) {
2833 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2834 reply_doserror(req, ERRDOS, ERRinvalidparam);
2838 /* maybe we can check the quota_fnum */
2839 fsp = file_fsp(SVAL(params,0));
2840 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2841 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2842 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2846 /* the NULL pointer checking for fsp->fake_file_handle->pd
2847 * is done by CHECK_NTQUOTA_HANDLE_OK()
2849 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2851 level = SVAL(params,2);
2853 /* unknown 12 bytes leading in params */
2856 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2857 /* seems that we should continue with the enum here --metze */
2859 if (qt_handle->quota_list!=NULL &&
2860 qt_handle->tmp_list==NULL) {
2863 free_ntquota_list(&(qt_handle->quota_list));
2865 /* Realloc the size of parameters and data we will return */
2867 params = nttrans_realloc(ppparams, param_len);
2868 if(params == NULL) {
2869 reply_doserror(req, ERRDOS, ERRnomem);
2874 SIVAL(params,0,data_len);
2881 case TRANSACT_GET_USER_QUOTA_LIST_START:
2883 if (qt_handle->quota_list==NULL &&
2884 qt_handle->tmp_list==NULL) {
2888 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2889 reply_doserror(req, ERRSRV, ERRerror);
2893 /* Realloc the size of parameters and data we will return */
2895 params = nttrans_realloc(ppparams, param_len);
2896 if(params == NULL) {
2897 reply_doserror(req, ERRDOS, ERRnomem);
2901 /* we should not trust the value in max_data_count*/
2902 max_data_count = MIN(max_data_count,2048);
2904 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2906 reply_doserror(req, ERRDOS, ERRnomem);
2912 /* set params Size of returned Quota Data 4 bytes*/
2913 /* but set it later when we know it */
2915 /* for each entry push the data */
2918 qt_handle->tmp_list = qt_handle->quota_list;
2921 tmp_list = qt_handle->tmp_list;
2923 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2924 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2926 sid_len = sid_size(&tmp_list->quotas->sid);
2927 entry_len = 40 + sid_len;
2929 /* nextoffset entry 4 bytes */
2930 SIVAL(entry,0,entry_len);
2932 /* then the len of the SID 4 bytes */
2933 SIVAL(entry,4,sid_len);
2935 /* unknown data 8 bytes SMB_BIG_UINT */
2936 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2938 /* the used disk space 8 bytes SMB_BIG_UINT */
2939 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2941 /* the soft quotas 8 bytes SMB_BIG_UINT */
2942 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2944 /* the hard quotas 8 bytes SMB_BIG_UINT */
2945 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2947 /* and now the SID */
2948 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2951 qt_handle->tmp_list = tmp_list;
2953 /* overwrite the offset of the last entry */
2954 SIVAL(entry-entry_len,0,0);
2956 data_len = 4+qt_len;
2957 /* overwrite the params quota_data_len */
2958 SIVAL(params,0,data_len);
2962 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2964 /* unknown 4 bytes IVAL(pdata,0) */
2966 if (data_count < 8) {
2967 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2968 reply_doserror(req, ERRDOS, ERRunknownlevel);
2972 sid_len = IVAL(pdata,4);
2973 /* Ensure this is less than 1mb. */
2974 if (sid_len > (1024*1024)) {
2975 reply_doserror(req, ERRDOS, ERRnomem);
2979 if (data_count < 8+sid_len) {
2980 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2981 reply_doserror(req, ERRDOS, ERRunknownlevel);
2985 data_len = 4+40+sid_len;
2987 if (max_data_count < data_len) {
2988 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2989 max_data_count, data_len));
2991 SIVAL(params,0,data_len);
2993 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2997 sid_parse(pdata+8,sid_len,&sid);
2999 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
3002 * we have to return zero's in all fields
3003 * instead of returning an error here
3008 /* Realloc the size of parameters and data we will return */
3010 params = nttrans_realloc(ppparams, param_len);
3011 if(params == NULL) {
3012 reply_doserror(req, ERRDOS, ERRnomem);
3016 pdata = nttrans_realloc(ppdata, data_len);
3018 reply_doserror(req, ERRDOS, ERRnomem);
3024 /* set params Size of returned Quota Data 4 bytes*/
3025 SIVAL(params,0,data_len);
3027 /* nextoffset entry 4 bytes */
3030 /* then the len of the SID 4 bytes */
3031 SIVAL(entry,4,sid_len);
3033 /* unknown data 8 bytes SMB_BIG_UINT */
3034 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
3036 /* the used disk space 8 bytes SMB_BIG_UINT */
3037 SBIG_UINT(entry,16,qt.usedspace);
3039 /* the soft quotas 8 bytes SMB_BIG_UINT */
3040 SBIG_UINT(entry,24,qt.softlim);
3042 /* the hard quotas 8 bytes SMB_BIG_UINT */
3043 SBIG_UINT(entry,32,qt.hardlim);
3045 /* and now the SID */
3046 sid_linearize(entry+40, sid_len, &sid);
3051 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
3052 reply_doserror(req, ERRSRV, ERRerror);
3057 send_nt_replies(req, nt_status, params, param_len,
3061 /****************************************************************************
3062 Reply to set user quota
3063 ****************************************************************************/
3065 static void call_nt_transact_set_user_quota(connection_struct *conn,
3066 struct smb_request *req,
3070 uint32 parameter_count,
3073 uint32 max_data_count)
3075 char *params = *ppparams;
3076 char *pdata = *ppdata;
3077 int data_len=0,param_len=0;
3078 SMB_NTQUOTA_STRUCT qt;
3081 files_struct *fsp = NULL;
3086 if (current_user.ut.uid != 0) {
3087 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
3088 lp_servicename(SNUM(conn)),conn->user));
3089 reply_doserror(req, ERRDOS, ERRnoaccess);
3094 * Ensure minimum number of parameters sent.
3097 if (parameter_count < 2) {
3098 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
3099 reply_doserror(req, ERRDOS, ERRinvalidparam);
3103 /* maybe we can check the quota_fnum */
3104 fsp = file_fsp(SVAL(params,0));
3105 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
3106 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
3107 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
3111 if (data_count < 40) {
3112 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
3113 reply_doserror(req, ERRDOS, ERRunknownlevel);
3117 /* offset to next quota record.
3118 * 4 bytes IVAL(pdata,0)
3123 sid_len = IVAL(pdata,4);
3125 if (data_count < 40+sid_len) {
3126 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
3127 reply_doserror(req, ERRDOS, ERRunknownlevel);
3131 /* unknown 8 bytes in pdata
3132 * maybe its the change time in NTTIME
3135 /* the used space 8 bytes (SMB_BIG_UINT)*/
3136 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
3137 #ifdef LARGE_SMB_OFF_T
3138 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
3139 #else /* LARGE_SMB_OFF_T */
3140 if ((IVAL(pdata,20) != 0)&&
3141 ((qt.usedspace != 0xFFFFFFFF)||
3142 (IVAL(pdata,20)!=0xFFFFFFFF))) {
3143 /* more than 32 bits? */
3144 reply_doserror(req, ERRDOS, ERRunknownlevel);
3147 #endif /* LARGE_SMB_OFF_T */
3149 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
3150 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
3151 #ifdef LARGE_SMB_OFF_T
3152 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
3153 #else /* LARGE_SMB_OFF_T */
3154 if ((IVAL(pdata,28) != 0)&&
3155 ((qt.softlim != 0xFFFFFFFF)||
3156 (IVAL(pdata,28)!=0xFFFFFFFF))) {
3157 /* more than 32 bits? */
3158 reply_doserror(req, ERRDOS, ERRunknownlevel);
3161 #endif /* LARGE_SMB_OFF_T */
3163 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
3164 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
3165 #ifdef LARGE_SMB_OFF_T
3166 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
3167 #else /* LARGE_SMB_OFF_T */
3168 if ((IVAL(pdata,36) != 0)&&
3169 ((qt.hardlim != 0xFFFFFFFF)||
3170 (IVAL(pdata,36)!=0xFFFFFFFF))) {
3171 /* more than 32 bits? */
3172 reply_doserror(req, ERRDOS, ERRunknownlevel);
3175 #endif /* LARGE_SMB_OFF_T */
3177 sid_parse(pdata+40,sid_len,&sid);
3178 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
3180 /* 44 unknown bytes left... */
3182 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
3183 reply_doserror(req, ERRSRV, ERRerror);
3187 send_nt_replies(req, NT_STATUS_OK, params, param_len,
3190 #endif /* HAVE_SYS_QUOTAS */
3192 static void handle_nttrans(connection_struct *conn,
3193 struct trans_state *state,
3194 struct smb_request *req)
3196 if (Protocol >= PROTOCOL_NT1) {
3197 req->flags2 |= 0x40; /* IS_LONG_NAME */
3198 SSVAL(req->inbuf,smb_flg2,req->flags2);
3201 /* Now we must call the relevant NT_TRANS function */
3202 switch(state->call) {
3203 case NT_TRANSACT_CREATE:
3205 START_PROFILE(NT_transact_create);
3206 call_nt_transact_create(
3208 &state->setup, state->setup_count,
3209 &state->param, state->total_param,
3210 &state->data, state->total_data,
3211 state->max_data_return);
3212 END_PROFILE(NT_transact_create);
3216 case NT_TRANSACT_IOCTL:
3218 START_PROFILE(NT_transact_ioctl);
3219 call_nt_transact_ioctl(
3221 &state->setup, state->setup_count,
3222 &state->param, state->total_param,
3223 &state->data, state->total_data,
3224 state->max_data_return);
3225 END_PROFILE(NT_transact_ioctl);
3229 case NT_TRANSACT_SET_SECURITY_DESC:
3231 START_PROFILE(NT_transact_set_security_desc);
3232 call_nt_transact_set_security_desc(
3234 &state->setup, state->setup_count,
3235 &state->param, state->total_param,
3236 &state->data, state->total_data,
3237 state->max_data_return);
3238 END_PROFILE(NT_transact_set_security_desc);
3242 case NT_TRANSACT_NOTIFY_CHANGE:
3244 START_PROFILE(NT_transact_notify_change);
3245 call_nt_transact_notify_change(
3247 &state->setup, state->setup_count,
3248 &state->param, state->total_param,
3249 &state->data, state->total_data,
3250 state->max_data_return,
3251 state->max_param_return);
3252 END_PROFILE(NT_transact_notify_change);
3256 case NT_TRANSACT_RENAME:
3258 START_PROFILE(NT_transact_rename);
3259 call_nt_transact_rename(
3261 &state->setup, state->setup_count,
3262 &state->param, state->total_param,
3263 &state->data, state->total_data,
3264 state->max_data_return);
3265 END_PROFILE(NT_transact_rename);
3269 case NT_TRANSACT_QUERY_SECURITY_DESC:
3271 START_PROFILE(NT_transact_query_security_desc);
3272 call_nt_transact_query_security_desc(
3274 &state->setup, state->setup_count,
3275 &state->param, state->total_param,
3276 &state->data, state->total_data,
3277 state->max_data_return);
3278 END_PROFILE(NT_transact_query_security_desc);
3282 #ifdef HAVE_SYS_QUOTAS
3283 case NT_TRANSACT_GET_USER_QUOTA:
3285 START_PROFILE(NT_transact_get_user_quota);
3286 call_nt_transact_get_user_quota(
3288 &state->setup, state->setup_count,
3289 &state->param, state->total_param,
3290 &state->data, state->total_data,
3291 state->max_data_return);
3292 END_PROFILE(NT_transact_get_user_quota);
3296 case NT_TRANSACT_SET_USER_QUOTA:
3298 START_PROFILE(NT_transact_set_user_quota);
3299 call_nt_transact_set_user_quota(
3301 &state->setup, state->setup_count,
3302 &state->param, state->total_param,
3303 &state->data, state->total_data,
3304 state->max_data_return);
3305 END_PROFILE(NT_transact_set_user_quota);
3308 #endif /* HAVE_SYS_QUOTAS */
3311 /* Error in request */
3312 DEBUG(0,("handle_nttrans: Unknown request %d in "
3313 "nttrans call\n", state->call));
3314 reply_doserror(req, ERRSRV, ERRerror);
3320 /****************************************************************************
3321 Reply to a SMBNTtrans.
3322 ****************************************************************************/
3324 void reply_nttrans(connection_struct *conn, struct smb_request *req)
3330 uint16 function_code;
3332 struct trans_state *state;
3335 START_PROFILE(SMBnttrans);
3337 if (req->wct < 19) {
3338 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3339 END_PROFILE(SMBnttrans);
3343 size = smb_len(req->inbuf) + 4;
3344 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
3345 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
3346 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
3347 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
3348 function_code = SVAL(req->inbuf, smb_nt_Function);
3350 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3351 reply_doserror(req, ERRSRV, ERRaccess);
3352 END_PROFILE(SMBnttrans);
3356 result = allow_new_trans(conn->pending_trans, req->mid);
3357 if (!NT_STATUS_IS_OK(result)) {
3358 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3359 reply_nterror(req, result);
3360 END_PROFILE(SMBnttrans);
3364 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3365 reply_doserror(req, ERRSRV, ERRaccess);
3366 END_PROFILE(SMBnttrans);
3370 state->cmd = SMBnttrans;
3372 state->mid = req->mid;
3373 state->vuid = req->vuid;
3374 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
3376 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
3377 state->param = NULL;
3378 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
3379 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
3381 /* setup count is in *words* */
3382 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
3383 state->setup = NULL;
3384 state->call = function_code;
3387 * All nttrans messages we handle have smb_wct == 19 +
3388 * state->setup_count. Ensure this is so as a sanity check.
3391 if(req->wct != 19 + (state->setup_count/2)) {
3392 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3393 req->wct, 19 + (state->setup_count/2)));
3397 /* Don't allow more than 128mb for each value. */
3398 if ((state->total_data > (1024*1024*128)) ||
3399 (state->total_param > (1024*1024*128))) {
3400 reply_doserror(req, ERRDOS, ERRnomem);
3401 END_PROFILE(SMBnttrans);
3405 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3408 if (state->total_data) {
3409 /* Can't use talloc here, the core routines do realloc on the
3410 * params and data. */
3411 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3412 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3413 "bytes !\n", (unsigned int)state->total_data));
3415 reply_doserror(req, ERRDOS, ERRnomem);
3416 END_PROFILE(SMBnttrans);
3419 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3421 if ((smb_base(req->inbuf)+dsoff+dscnt
3422 > (char *)req->inbuf + size) ||
3423 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
3426 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3429 if (state->total_param) {
3430 /* Can't use talloc here, the core routines do realloc on the
3431 * params and data. */
3432 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3433 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3434 "bytes !\n", (unsigned int)state->total_param));
3435 SAFE_FREE(state->data);
3437 reply_doserror(req, ERRDOS, ERRnomem);
3438 END_PROFILE(SMBnttrans);
3441 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3443 if ((smb_base(req->inbuf)+psoff+pscnt
3444 > (char *)req->inbuf + size) ||
3445 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
3448 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3451 state->received_data = dscnt;
3452 state->received_param = pscnt;
3454 if(state->setup_count > 0) {
3455 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3456 state->setup_count));
3457 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3458 if (state->setup == NULL) {
3459 DEBUG(0,("reply_nttrans : Out of memory\n"));
3460 SAFE_FREE(state->data);
3461 SAFE_FREE(state->param);
3463 reply_doserror(req, ERRDOS, ERRnomem);
3464 END_PROFILE(SMBnttrans);
3468 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3469 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3472 if (smb_nt_SetupStart + state->setup_count > size) {
3476 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
3477 state->setup_count);
3478 dump_data(10, (uint8 *)state->setup, state->setup_count);
3481 if ((state->received_data == state->total_data) &&
3482 (state->received_param == state->total_param)) {
3483 handle_nttrans(conn, state, req);
3484 SAFE_FREE(state->param);
3485 SAFE_FREE(state->data);
3487 END_PROFILE(SMBnttrans);
3491 DLIST_ADD(conn->pending_trans, state);
3493 /* We need to send an interim response then receive the rest
3494 of the parameter/data bytes */
3495 reply_outbuf(req, 0, 0);
3496 show_msg((char *)req->outbuf);
3497 END_PROFILE(SMBnttrans);
3502 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3503 SAFE_FREE(state->data);
3504 SAFE_FREE(state->param);
3506 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3507 END_PROFILE(SMBnttrans);
3511 /****************************************************************************
3512 Reply to a SMBnttranss
3513 ****************************************************************************/
3515 void reply_nttranss(connection_struct *conn, struct smb_request *req)
3517 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3518 struct trans_state *state;
3522 START_PROFILE(SMBnttranss);
3524 show_msg((char *)req->inbuf);
3526 if (req->wct < 18) {
3527 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3528 END_PROFILE(SMBnttranss);
3532 for (state = conn->pending_trans; state != NULL;
3533 state = state->next) {
3534 if (state->mid == req->mid) {
3539 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3540 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3541 END_PROFILE(SMBnttranss);
3545 /* Revise state->total_param and state->total_data in case they have
3546 changed downwards */
3547 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
3548 < state->total_param) {
3549 state->total_param = IVAL(req->inbuf,
3550 smb_nts_TotalParameterCount);
3552 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
3553 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
3556 size = smb_len(req->inbuf) + 4;
3558 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
3559 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
3560 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
3562 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
3563 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
3564 doff = IVAL(req->inbuf, smb_nts_DataOffset);
3566 state->received_param += pcnt;
3567 state->received_data += dcnt;
3569 if ((state->received_data > state->total_data) ||
3570 (state->received_param > state->total_param))
3574 if (pdisp+pcnt > state->total_param)
3576 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3578 if (pdisp > state->total_param)
3580 if ((smb_base(req->inbuf) + poff + pcnt
3581 > (char *)req->inbuf + size) ||
3582 (smb_base(req->inbuf) + poff + pcnt
3583 < smb_base(req->inbuf)))
3585 if (state->param + pdisp < state->param)
3588 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
3593 if (ddisp+dcnt > state->total_data)
3595 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3597 if (ddisp > state->total_data)
3599 if ((smb_base(req->inbuf) + doff + dcnt
3600 > (char *)req->inbuf + size) ||
3601 (smb_base(req->inbuf) + doff + dcnt
3602 < smb_base(req->inbuf)))
3604 if (state->data + ddisp < state->data)
3607 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
3611 if ((state->received_param < state->total_param) ||
3612 (state->received_data < state->total_data)) {
3613 END_PROFILE(SMBnttranss);
3618 * construct_reply_common will copy smb_com from inbuf to
3619 * outbuf. SMBnttranss is wrong here.
3621 SCVAL(req->inbuf,smb_com,SMBnttrans);
3623 handle_nttrans(conn, state, req);
3625 DLIST_REMOVE(conn->pending_trans, state);
3626 SAFE_FREE(state->data);
3627 SAFE_FREE(state->param);
3629 END_PROFILE(SMBnttranss);
3634 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3635 DLIST_REMOVE(conn->pending_trans, state);
3636 SAFE_FREE(state->data);
3637 SAFE_FREE(state->param);
3639 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3640 END_PROFILE(SMBnttranss);