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));
592 reply_doserror(req, ERRDOS, ERRbadfid);
593 END_PROFILE(SMBntcreateX);
597 if(!dir_fsp->is_directory) {
599 srvstr_get_path(ctx, (char *)req->inbuf,
601 smb_buf(req->inbuf), 0,
602 STR_TERMINATE, &status);
603 if (!NT_STATUS_IS_OK(status)) {
604 reply_nterror(req, status);
605 END_PROFILE(SMBntcreateX);
610 * Check to see if this is a mac fork of some kind.
613 if( is_ntfs_stream_name(fname)) {
615 req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
616 END_PROFILE(SMBntcreateX);
621 we need to handle the case when we get a
622 relative open relative to a file and the
623 pathname is blank - this is a reopen!
624 (hint from demyn plantenberg)
627 reply_doserror(req, ERRDOS, ERRbadfid);
628 END_PROFILE(SMBntcreateX);
633 * Copy in the base directory name.
636 dir_name_len = strlen(dir_fsp->fsp_name);
637 fname = TALLOC_SIZE(ctx, dir_name_len+2);
640 req, NT_STATUS_NO_MEMORY);
641 END_PROFILE(SMBntcreateX);
644 memcpy(fname, dir_fsp->fsp_name, dir_name_len+1);
647 * Ensure it ends in a '/'.
648 * We used TALLOC_SIZE +2 to add space for the '/'.
651 if(dir_name_len && (fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
652 fname[dir_name_len] = '/';
653 fname[dir_name_len+1] = '\0';
657 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &rel_fname,
658 smb_buf(req->inbuf), 0,
659 STR_TERMINATE, &status);
660 if (!NT_STATUS_IS_OK(status)) {
661 reply_nterror(req, status);
662 END_PROFILE(SMBntcreateX);
665 fname = talloc_asprintf(ctx, "%s%s",
670 req, NT_STATUS_NO_MEMORY);
671 END_PROFILE(SMBntcreateX);
675 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
676 smb_buf(req->inbuf), 0,
677 STR_TERMINATE, &status);
678 if (!NT_STATUS_IS_OK(status)) {
679 reply_nterror(req, status);
680 END_PROFILE(SMBntcreateX);
685 * Check to see if this is a mac fork of some kind.
688 if( is_ntfs_stream_name(fname)) {
689 enum FAKE_FILE_TYPE fake_file_type = is_fake_file(fname);
690 if (fake_file_type!=FAKE_FILE_TYPE_NONE) {
692 * Here we go! support for changing the disk quotas --metze
694 * We need to fake up to open this MAGIC QUOTA file
695 * and return a valid FID.
697 * w2k close this file directly after openening
698 * xp also tries a QUERY_FILE_INFO on the file and then close it
700 reply_ntcreate_and_X_quota(conn, req,
701 fake_file_type, fname);
703 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
705 END_PROFILE(SMBntcreateX);
711 * Now contruct the smb_open_mode value from the filename,
712 * desired access and the share access.
714 status = resolve_dfspath(ctx, conn,
715 req->flags2 & FLAGS2_DFS_PATHNAMES,
718 if (!NT_STATUS_IS_OK(status)) {
719 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
720 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
724 reply_nterror(req, status);
726 END_PROFILE(SMBntcreateX);
730 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
731 if (oplock_request) {
732 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
736 * Ordinary file or directory.
740 * Check if POSIX semantics are wanted.
743 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
744 case_state = set_posix_case_semantics(NULL, conn);
745 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
748 status = unix_convert(conn, fname, False, &fname, NULL, &sbuf);
749 if (!NT_STATUS_IS_OK(status)) {
750 TALLOC_FREE(case_state);
751 reply_nterror(req, status);
752 END_PROFILE(SMBntcreateX);
755 /* All file access must go through check_name() */
756 status = check_name(conn, fname);
757 if (!NT_STATUS_IS_OK(status)) {
758 TALLOC_FREE(case_state);
759 reply_nterror(req, status);
760 END_PROFILE(SMBntcreateX);
764 /* This is the correct thing to do (check every time) but can_delete is
765 expensive (it may have to read the parent directory permissions). So
766 for now we're not doing it unless we have a strong hint the client
767 is really going to delete this file. If the client is forcing FILE_CREATE
768 let the filesystem take care of the permissions. */
770 /* Setting FILE_SHARE_DELETE is the hint. */
772 if (lp_acl_check_permissions(SNUM(conn))
773 && (create_disposition != FILE_CREATE)
774 && (share_access & FILE_SHARE_DELETE)
775 && (access_mask & DELETE_ACCESS)) {
776 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
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;
1275 struct case_semantics_state *case_state = NULL;
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);
1318 /* Ensure the data_len is correct for the sd and ea values given. */
1319 if ((ea_len + sd_len > data_count) ||
1320 (ea_len > data_count) || (sd_len > data_count) ||
1321 (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1322 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1323 (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1324 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1329 if (!lp_ea_support(SNUM(conn))) {
1330 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1331 (unsigned int)ea_len ));
1332 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1337 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1338 (unsigned int)ea_len ));
1339 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1344 if (create_options & FILE_OPEN_BY_FILE_ID) {
1345 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1350 * Get the file name.
1353 if(root_dir_fid != 0) {
1355 * This filename is relative to a directory fid.
1357 char *tmpname = NULL;
1358 files_struct *dir_fsp = file_fsp(SVAL(params,4));
1359 size_t dir_name_len;
1362 reply_doserror(req, ERRDOS, ERRbadfid);
1366 if(!dir_fsp->is_directory) {
1367 srvstr_get_path(ctx, params, req->flags2, &fname,
1369 parameter_count-53, STR_TERMINATE,
1371 if (!NT_STATUS_IS_OK(status)) {
1372 reply_nterror(req, status);
1377 * Check to see if this is a mac fork of some kind.
1380 if( is_ntfs_stream_name(fname)) {
1382 NT_STATUS_OBJECT_PATH_NOT_FOUND);
1386 reply_doserror(req, ERRDOS, ERRbadfid);
1391 * Copy in the base directory name.
1394 dir_name_len = strlen(dir_fsp->fsp_name);
1395 fname = TALLOC_SIZE(ctx, dir_name_len+2);
1398 req, NT_STATUS_NO_MEMORY);
1399 END_PROFILE(SMBntcreateX);
1402 memcpy(fname, dir_fsp->fsp_name, dir_name_len+1);
1405 * Ensure it ends in a '/'.
1406 * We used TALLOC_SIZE +2 to add space for the '/'.
1409 if(dir_name_len && (fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1410 fname[dir_name_len] = '/';
1411 fname[dir_name_len+1] = '\0';
1415 srvstr_get_path(ctx, params, req->flags2, &tmpname,
1417 parameter_count-53, STR_TERMINATE,
1419 if (!NT_STATUS_IS_OK(status)) {
1420 reply_nterror(req, status);
1423 fname = talloc_asprintf(ctx, "%s%s",
1428 req, NT_STATUS_NO_MEMORY);
1432 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
1434 STR_TERMINATE, &status);
1435 if (!NT_STATUS_IS_OK(status)) {
1436 reply_nterror(req, status);
1441 * Check to see if this is a mac fork of some kind.
1444 if( is_ntfs_stream_name(fname)) {
1445 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
1450 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1451 if (oplock_request) {
1452 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1456 * Ordinary file or directory.
1460 * Check if POSIX semantics are wanted.
1463 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1464 case_state = set_posix_case_semantics(NULL, conn);
1465 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1468 status = resolve_dfspath(ctx, conn,
1469 req->flags2 & FLAGS2_DFS_PATHNAMES,
1472 if (!NT_STATUS_IS_OK(status)) {
1473 TALLOC_FREE(case_state);
1474 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1475 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1476 ERRSRV, ERRbadpath);
1479 reply_nterror(req, status);
1483 status = unix_convert(conn, fname, False, &fname, NULL, &sbuf);
1484 if (!NT_STATUS_IS_OK(status)) {
1485 TALLOC_FREE(case_state);
1486 reply_nterror(req, status);
1489 /* All file access must go through check_name() */
1490 status = check_name(conn, fname);
1491 if (!NT_STATUS_IS_OK(status)) {
1492 TALLOC_FREE(case_state);
1493 reply_nterror(req, status);
1497 /* This is the correct thing to do (check every time) but can_delete is
1498 expensive (it may have to read the parent directory permissions). So
1499 for now we're not doing it unless we have a strong hint the client
1500 is really going to delete this file. If the client is forcing FILE_CREATE
1501 let the filesystem take care of the permissions. */
1503 /* Setting FILE_SHARE_DELETE is the hint. */
1505 if (lp_acl_check_permissions(SNUM(conn))
1506 && (create_disposition != FILE_CREATE)
1507 && (share_access & FILE_SHARE_DELETE)
1508 && (access_mask & DELETE_ACCESS)) {
1509 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
1510 !can_delete_file_in_directory(conn, fname)) {
1511 TALLOC_FREE(case_state);
1512 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1518 /* We need to support SeSecurityPrivilege for this. */
1519 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
1520 !user_has_privileges(current_user.nt_user_token,
1522 TALLOC_FREE(case_state);
1523 reply_nterror(req, NT_STATUS_PRIVILEGE_NOT_HELD);
1529 pdata = data + sd_len;
1531 /* We have already checked that ea_len <= data_count here. */
1532 ea_list = read_nttrans_ea_list(talloc_tos(), pdata,
1535 TALLOC_FREE(case_state);
1536 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1542 * If it's a request for a directory open, deal with it separately.
1545 if(create_options & FILE_DIRECTORY_FILE) {
1547 /* Can't open a temp directory. IFS kit test. */
1548 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1549 TALLOC_FREE(case_state);
1550 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1555 * We will get a create directory here if the Win32
1556 * app specified a security descriptor in the
1557 * CreateDirectory() call.
1561 status = open_directory(conn, req, fname, &sbuf,
1571 * Ordinary file case.
1574 status = open_file_ntcreate(conn,req,fname,&sbuf,
1583 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
1586 * Fail the open if it was explicitly a non-directory file.
1589 if (create_options & FILE_NON_DIRECTORY_FILE) {
1590 TALLOC_FREE(case_state);
1591 reply_force_nterror(
1593 NT_STATUS_FILE_IS_A_DIRECTORY);
1598 status = open_directory(conn, req, fname,
1609 TALLOC_FREE(case_state);
1611 if(!NT_STATUS_IS_OK(status)) {
1612 if (open_was_deferred(req->mid)) {
1613 /* We have re-scheduled this call. */
1616 reply_openerror(req, status);
1621 * According to the MS documentation, the only time the security
1622 * descriptor is applied to the opened file is iff we *created* the
1623 * file; an existing file stays the same.
1625 * Also, it seems (from observation) that you can open the file with
1626 * any access mask but you can still write the sd. We need to override
1627 * the granted access before we call set_sd
1628 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1631 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1632 uint32 saved_access_mask = fsp->access_mask;
1634 /* We have already checked that sd_len <= data_count here. */
1636 fsp->access_mask = FILE_GENERIC_ALL;
1638 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1639 if (!NT_STATUS_IS_OK(status)) {
1640 close_file(fsp,ERROR_CLOSE);
1641 TALLOC_FREE(case_state);
1642 reply_nterror(req, status);
1645 fsp->access_mask = saved_access_mask;
1648 if (ea_len && (info == FILE_WAS_CREATED)) {
1649 status = set_ea(conn, fsp, fname, ea_list);
1650 if (!NT_STATUS_IS_OK(status)) {
1651 close_file(fsp,ERROR_CLOSE);
1652 TALLOC_FREE(case_state);
1653 reply_nterror(req, status);
1658 TALLOC_FREE(case_state);
1660 file_len = sbuf.st_size;
1661 fattr = dos_mode(conn,fname,&sbuf);
1663 fattr = FILE_ATTRIBUTE_NORMAL;
1665 if (!fsp->is_directory && (fattr & aDIR)) {
1666 close_file(fsp,ERROR_CLOSE);
1667 reply_doserror(req, ERRDOS, ERRnoaccess);
1671 /* Save the requested allocation size. */
1672 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1673 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1674 #ifdef LARGE_SMB_OFF_T
1675 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1677 if (allocation_size && (allocation_size > file_len)) {
1678 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1679 if (fsp->is_directory) {
1680 close_file(fsp,ERROR_CLOSE);
1681 /* Can't set allocation size on a directory. */
1682 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1685 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1686 close_file(fsp,ERROR_CLOSE);
1687 reply_nterror(req, NT_STATUS_DISK_FULL);
1691 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1696 * If the caller set the extended oplock request bit
1697 * and we granted one (by whatever means) - set the
1698 * correct bit for extended oplock reply.
1701 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1702 extended_oplock_granted = True;
1705 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1706 extended_oplock_granted = True;
1709 /* Realloc the size of parameters and data we will return */
1710 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1711 /* Extended response is 32 more byyes. */
1716 params = nttrans_realloc(ppparams, param_len);
1717 if(params == NULL) {
1718 reply_doserror(req, ERRDOS, ERRnomem);
1723 if (extended_oplock_granted) {
1724 if (flags & REQUEST_BATCH_OPLOCK) {
1725 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1727 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1729 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1730 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1732 SCVAL(p,0,NO_OPLOCK_RETURN);
1736 SSVAL(p,0,fsp->fnum);
1738 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1739 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1746 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1747 a_timespec = get_atimespec(&sbuf);
1748 m_timespec = get_mtimespec(&sbuf);
1750 if (lp_dos_filetime_resolution(SNUM(conn))) {
1751 dos_filetime_timespec(&c_timespec);
1752 dos_filetime_timespec(&a_timespec);
1753 dos_filetime_timespec(&m_timespec);
1756 put_long_date_timespec(p, c_timespec); /* create time. */
1758 put_long_date_timespec(p, a_timespec); /* access time */
1760 put_long_date_timespec(p, m_timespec); /* write time */
1762 put_long_date_timespec(p, m_timespec); /* change time */
1764 SIVAL(p,0,fattr); /* File Attributes. */
1766 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1768 SOFF_T(p,0,file_len);
1770 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1774 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1776 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1779 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1780 perms = FILE_GENERIC_ALL;
1782 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1787 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1789 /* Send the required number of replies */
1790 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1795 /****************************************************************************
1796 Reply to a NT CANCEL request.
1797 conn POINTER CAN BE NULL HERE !
1798 ****************************************************************************/
1800 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1803 * Go through and cancel any pending change notifies.
1806 START_PROFILE(SMBntcancel);
1807 remove_pending_change_notify_requests_by_mid(req->mid);
1808 remove_pending_lock_requests_by_mid(req->mid);
1809 srv_cancel_sign_response(req->mid);
1811 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1813 END_PROFILE(SMBntcancel);
1817 /****************************************************************************
1819 ****************************************************************************/
1821 static NTSTATUS copy_internals(connection_struct *conn,
1822 struct smb_request *req,
1823 const char *oldname_in,
1824 const char *newname_in,
1827 SMB_STRUCT_STAT sbuf1, sbuf2;
1828 char *oldname = NULL;
1829 char *newname = NULL;
1830 char *last_component_oldname = NULL;
1831 char *last_component_newname = NULL;
1832 files_struct *fsp1,*fsp2;
1836 NTSTATUS status = NT_STATUS_OK;
1841 if (!CAN_WRITE(conn)) {
1842 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1845 status = unix_convert(conn, oldname_in, False, &oldname,
1846 &last_component_oldname, &sbuf1);
1847 if (!NT_STATUS_IS_OK(status)) {
1851 status = check_name(conn, oldname);
1852 if (!NT_STATUS_IS_OK(status)) {
1856 /* Source must already exist. */
1857 if (!VALID_STAT(sbuf1)) {
1858 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1860 /* Ensure attributes match. */
1861 fattr = dos_mode(conn,oldname,&sbuf1);
1862 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1863 return NT_STATUS_NO_SUCH_FILE;
1866 status = unix_convert(conn, newname_in, False, &newname,
1867 &last_component_newname, &sbuf2);
1868 if (!NT_STATUS_IS_OK(status)) {
1872 status = check_name(conn, newname);
1873 if (!NT_STATUS_IS_OK(status)) {
1877 /* Disallow if newname already exists. */
1878 if (VALID_STAT(sbuf2)) {
1879 return NT_STATUS_OBJECT_NAME_COLLISION;
1882 /* No links from a directory. */
1883 if (S_ISDIR(sbuf1.st_mode)) {
1884 return NT_STATUS_FILE_IS_A_DIRECTORY;
1887 /* Ensure this is within the share. */
1888 status = check_reduced_name(conn, oldname);
1889 if (!NT_STATUS_IS_OK(status)) {
1893 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1896 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1897 FILE_READ_DATA, /* Read-only. */
1898 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1900 0, /* No create options. */
1901 FILE_ATTRIBUTE_NORMAL,
1905 if (!NT_STATUS_IS_OK(status)) {
1909 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1910 FILE_WRITE_DATA, /* Read-only. */
1911 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1913 0, /* No create options. */
1918 if (!NT_STATUS_IS_OK(status)) {
1919 close_file(fsp1,ERROR_CLOSE);
1923 if (sbuf1.st_size) {
1924 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1928 * As we are opening fsp1 read-only we only expect
1929 * an error on close on fsp2 if we are out of space.
1930 * Thus we don't look at the error return from the
1933 close_file(fsp1,NORMAL_CLOSE);
1935 /* Ensure the modtime is set correctly on the destination file. */
1936 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1938 status = close_file(fsp2,NORMAL_CLOSE);
1940 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1941 creates the file. This isn't the correct thing to do in the copy
1943 file_set_dosmode(conn, newname, fattr, &sbuf2,
1944 parent_dirname(newname));
1946 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1947 return NT_STATUS_DISK_FULL;
1950 if (!NT_STATUS_IS_OK(status)) {
1951 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1952 nt_errstr(status), oldname, newname));
1957 /****************************************************************************
1958 Reply to a NT rename request.
1959 ****************************************************************************/
1961 void reply_ntrename(connection_struct *conn, struct smb_request *req)
1963 char *oldname = NULL;
1964 char *newname = NULL;
1967 BOOL src_has_wcard = False;
1968 BOOL dest_has_wcard = False;
1971 TALLOC_CTX *ctx = talloc_tos();
1973 START_PROFILE(SMBntrename);
1976 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1977 END_PROFILE(SMBntrename);
1981 attrs = SVAL(req->inbuf,smb_vwv0);
1982 rename_type = SVAL(req->inbuf,smb_vwv1);
1984 p = smb_buf(req->inbuf) + 1;
1985 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1986 0, STR_TERMINATE, &status,
1988 if (!NT_STATUS_IS_OK(status)) {
1989 reply_nterror(req, status);
1990 END_PROFILE(SMBntrename);
1994 if( is_ntfs_stream_name(oldname)) {
1995 /* Can't rename a stream. */
1996 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1997 END_PROFILE(SMBntrename);
2001 if (ms_has_wild(oldname)) {
2002 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
2003 END_PROFILE(SMBntrename);
2008 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
2009 0, STR_TERMINATE, &status,
2011 if (!NT_STATUS_IS_OK(status)) {
2012 reply_nterror(req, status);
2013 END_PROFILE(SMBntrename);
2017 status = resolve_dfspath(ctx, conn,
2018 req->flags2 & FLAGS2_DFS_PATHNAMES,
2021 if (!NT_STATUS_IS_OK(status)) {
2022 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2023 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2024 ERRSRV, ERRbadpath);
2025 END_PROFILE(SMBntrename);
2028 reply_nterror(req, status);
2029 END_PROFILE(SMBntrename);
2033 status = resolve_dfspath(ctx, conn,
2034 req->flags2 & FLAGS2_DFS_PATHNAMES,
2037 if (!NT_STATUS_IS_OK(status)) {
2038 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2039 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2040 ERRSRV, ERRbadpath);
2041 END_PROFILE(SMBntrename);
2044 reply_nterror(req, status);
2045 END_PROFILE(SMBntrename);
2049 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
2051 switch(rename_type) {
2052 case RENAME_FLAG_RENAME:
2053 status = rename_internals(conn, req, oldname, newname,
2054 attrs, False, src_has_wcard,
2057 case RENAME_FLAG_HARD_LINK:
2058 if (src_has_wcard || dest_has_wcard) {
2060 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2062 status = hardlink_internals(conn, oldname, newname);
2065 case RENAME_FLAG_COPY:
2066 if (src_has_wcard || dest_has_wcard) {
2068 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2070 status = copy_internals(conn, req, oldname,
2074 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
2075 status = NT_STATUS_INVALID_PARAMETER;
2078 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
2082 if (!NT_STATUS_IS_OK(status)) {
2083 if (open_was_deferred(req->mid)) {
2084 /* We have re-scheduled this call. */
2085 END_PROFILE(SMBntrename);
2089 reply_nterror(req, status);
2090 END_PROFILE(SMBntrename);
2094 reply_outbuf(req, 0, 0);
2096 END_PROFILE(SMBntrename);
2100 /****************************************************************************
2101 Reply to a notify change - queue the request and
2102 don't allow a directory to be opened.
2103 ****************************************************************************/
2105 static void call_nt_transact_notify_change(connection_struct *conn,
2106 struct smb_request *req,
2110 uint32 parameter_count,
2111 char **ppdata, uint32 data_count,
2112 uint32 max_data_count,
2113 uint32 max_param_count)
2115 uint16 *setup = *ppsetup;
2121 if(setup_count < 6) {
2122 reply_doserror(req, ERRDOS, ERRbadfunc);
2126 fsp = file_fsp(SVAL(setup,4));
2127 filter = IVAL(setup, 0);
2128 recursive = (SVAL(setup, 6) != 0) ? True : False;
2130 DEBUG(3,("call_nt_transact_notify_change\n"));
2133 reply_doserror(req, ERRDOS, ERRbadfid);
2138 char *filter_string;
2140 if (!(filter_string = notify_filter_string(NULL, filter))) {
2141 reply_nterror(req,NT_STATUS_NO_MEMORY);
2145 DEBUG(3,("call_nt_transact_notify_change: notify change "
2146 "called on %s, filter = %s, recursive = %d\n",
2147 fsp->fsp_name, filter_string, recursive));
2149 TALLOC_FREE(filter_string);
2152 if((!fsp->is_directory) || (conn != fsp->conn)) {
2153 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2157 if (fsp->notify == NULL) {
2159 status = change_notify_create(fsp, filter, recursive);
2161 if (!NT_STATUS_IS_OK(status)) {
2162 DEBUG(10, ("change_notify_create returned %s\n",
2163 nt_errstr(status)));
2164 reply_nterror(req, status);
2169 if (fsp->notify->num_changes != 0) {
2172 * We've got changes pending, respond immediately
2176 * TODO: write a torture test to check the filtering behaviour
2180 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
2183 * change_notify_reply() above has independently sent its
2190 * No changes pending, queue the request
2193 status = change_notify_add_request(req->inbuf, max_param_count, filter,
2195 if (!NT_STATUS_IS_OK(status)) {
2196 reply_nterror(req, status);
2201 /****************************************************************************
2202 Reply to an NT transact rename command.
2203 ****************************************************************************/
2205 static void call_nt_transact_rename(connection_struct *conn,
2206 struct smb_request *req,
2207 uint16 **ppsetup, uint32 setup_count,
2208 char **ppparams, uint32 parameter_count,
2209 char **ppdata, uint32 data_count,
2210 uint32 max_data_count)
2212 char *params = *ppparams;
2213 char *new_name = NULL;
2214 files_struct *fsp = NULL;
2215 BOOL replace_if_exists = False;
2216 BOOL dest_has_wcard = False;
2218 TALLOC_CTX *ctx = talloc_tos();
2220 if(parameter_count < 5) {
2221 reply_doserror(req, ERRDOS, ERRbadfunc);
2225 fsp = file_fsp(SVAL(params, 0));
2226 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2227 if (!check_fsp(conn, req, fsp, ¤t_user)) {
2230 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
2231 parameter_count - 4,
2232 STR_TERMINATE, &status, &dest_has_wcard);
2233 if (!NT_STATUS_IS_OK(status)) {
2234 reply_nterror(req, status);
2238 status = rename_internals(conn, req, fsp->fsp_name,
2239 new_name, 0, replace_if_exists, False, dest_has_wcard);
2241 if (!NT_STATUS_IS_OK(status)) {
2242 if (open_was_deferred(req->mid)) {
2243 /* We have re-scheduled this call. */
2246 reply_nterror(req, status);
2251 * Rename was successful.
2253 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2255 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2256 fsp->fsp_name, new_name));
2261 /******************************************************************************
2262 Fake up a completely empty SD.
2263 *******************************************************************************/
2265 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2269 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2271 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2278 /****************************************************************************
2279 Reply to query a security descriptor.
2280 ****************************************************************************/
2282 static void call_nt_transact_query_security_desc(connection_struct *conn,
2283 struct smb_request *req,
2287 uint32 parameter_count,
2290 uint32 max_data_count)
2292 char *params = *ppparams;
2293 char *data = *ppdata;
2295 SEC_DESC *psd = NULL;
2297 uint32 security_info_wanted;
2298 TALLOC_CTX *mem_ctx;
2299 files_struct *fsp = NULL;
2301 if(parameter_count < 8) {
2302 reply_doserror(req, ERRDOS, ERRbadfunc);
2306 fsp = file_fsp(SVAL(params,0));
2308 reply_doserror(req, ERRDOS, ERRbadfid);
2312 security_info_wanted = IVAL(params,4);
2314 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2315 (unsigned int)security_info_wanted ));
2317 params = nttrans_realloc(ppparams, 4);
2318 if(params == NULL) {
2319 reply_doserror(req, ERRDOS, ERRnomem);
2323 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2324 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2325 reply_doserror(req, ERRDOS, ERRnomem);
2330 * Get the permissions to return.
2333 if (!lp_nt_acl_support(SNUM(conn))) {
2334 sd_size = get_null_nt_acl(mem_ctx, &psd);
2336 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
2340 talloc_destroy(mem_ctx);
2341 reply_unixerror(req, ERRDOS, ERRnoaccess);
2345 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2347 SIVAL(params,0,(uint32)sd_size);
2349 if(max_data_count < sd_size) {
2351 send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
2352 params, 4, *ppdata, 0);
2353 talloc_destroy(mem_ctx);
2358 * Allocate the data we will point this at.
2361 data = nttrans_realloc(ppdata, sd_size);
2363 talloc_destroy(mem_ctx);
2364 reply_doserror(req, ERRDOS, ERRnomem);
2369 * Init the parse struct we will marshall into.
2372 prs_init(&pd, 0, mem_ctx, MARSHALL);
2375 * Setup the prs_struct to point at the memory we just
2379 prs_give_memory( &pd, data, (uint32)sd_size, False);
2382 * Finally, linearize into the outgoing buffer.
2385 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2386 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2387 security descriptor.\n"));
2389 * Return access denied for want of a better error message..
2391 talloc_destroy(mem_ctx);
2392 reply_unixerror(req, ERRDOS, ERRnoaccess);
2397 * Now we can delete the security descriptor.
2400 talloc_destroy(mem_ctx);
2402 send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2406 /****************************************************************************
2407 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2408 ****************************************************************************/
2410 static void call_nt_transact_set_security_desc(connection_struct *conn,
2411 struct smb_request *req,
2415 uint32 parameter_count,
2418 uint32 max_data_count)
2420 char *params= *ppparams;
2421 char *data = *ppdata;
2422 files_struct *fsp = NULL;
2423 uint32 security_info_sent = 0;
2426 if(parameter_count < 8) {
2427 reply_doserror(req, ERRDOS, ERRbadfunc);
2431 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
2432 reply_doserror(req, ERRDOS, ERRbadfid);
2436 if(!lp_nt_acl_support(SNUM(conn))) {
2440 security_info_sent = IVAL(params,4);
2442 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2443 (unsigned int)security_info_sent ));
2445 if (data_count == 0) {
2446 reply_doserror(req, ERRDOS, ERRnoaccess);
2450 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2451 reply_nterror(req, nt_status);
2457 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2461 /****************************************************************************
2463 ****************************************************************************/
2465 static void call_nt_transact_ioctl(connection_struct *conn,
2466 struct smb_request *req,
2467 uint16 **ppsetup, uint32 setup_count,
2468 char **ppparams, uint32 parameter_count,
2469 char **ppdata, uint32 data_count,
2470 uint32 max_data_count)
2477 static BOOL logged_message;
2478 char *pdata = *ppdata;
2480 if (setup_count != 8) {
2481 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2482 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2486 function = IVAL(*ppsetup, 0);
2487 fidnum = SVAL(*ppsetup, 4);
2488 isFSctl = CVAL(*ppsetup, 6);
2489 compfilter = CVAL(*ppsetup, 7);
2491 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2492 function, fidnum, isFSctl, compfilter));
2494 fsp=file_fsp(fidnum);
2495 /* this check is done in each implemented function case for now
2496 because I don't want to break anything... --metze
2497 FSP_BELONGS_CONN(fsp,conn);*/
2500 case FSCTL_SET_SPARSE:
2501 /* pretend this succeeded - tho strictly we should
2502 mark the file sparse (if the local fs supports it)
2503 so we can know if we need to pre-allocate or not */
2505 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2506 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2509 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2511 unsigned char objid[16];
2513 /* This should return the object-id on this file.
2514 * I think I'll make this be the inode+dev. JRA.
2517 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2519 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2524 pdata = nttrans_realloc(ppdata, data_count);
2525 if (pdata == NULL) {
2526 reply_nterror(req, NT_STATUS_NO_MEMORY);
2529 push_file_id_16(pdata, &fsp->file_id);
2530 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2531 push_file_id_16(pdata+32, &fsp->file_id);
2532 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2537 case FSCTL_GET_REPARSE_POINT:
2538 /* pretend this fail - my winXP does it like this
2542 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2543 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2546 case FSCTL_SET_REPARSE_POINT:
2547 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2551 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2552 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2555 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2558 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2559 * and return their volume names. If max_data_count is 16, then it is just
2560 * asking for the number of volumes and length of the combined names.
2562 * pdata is the data allocated by our caller, but that uses
2563 * total_data_count (which is 0 in our case) rather than max_data_count.
2564 * Allocate the correct amount and return the pointer to let
2565 * it be deallocated when we return.
2567 SHADOW_COPY_DATA *shadow_data = NULL;
2568 TALLOC_CTX *shadow_mem_ctx = NULL;
2569 BOOL labels = False;
2570 uint32 labels_data_count = 0;
2574 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2578 if (max_data_count < 16) {
2579 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2581 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2585 if (max_data_count > 16) {
2589 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2590 if (shadow_mem_ctx == NULL) {
2591 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2592 reply_nterror(req, NT_STATUS_NO_MEMORY);
2596 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2597 if (shadow_data == NULL) {
2598 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2599 talloc_destroy(shadow_mem_ctx);
2600 reply_nterror(req, NT_STATUS_NO_MEMORY);
2604 shadow_data->mem_ctx = shadow_mem_ctx;
2607 * Call the VFS routine to actually do the work.
2609 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2610 talloc_destroy(shadow_data->mem_ctx);
2611 if (errno == ENOSYS) {
2612 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2613 conn->connectpath));
2614 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2617 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2618 conn->connectpath));
2619 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2624 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2629 data_count = 12+labels_data_count+4;
2632 if (max_data_count<data_count) {
2633 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2634 max_data_count,data_count));
2635 talloc_destroy(shadow_data->mem_ctx);
2636 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2640 pdata = nttrans_realloc(ppdata, data_count);
2641 if (pdata == NULL) {
2642 talloc_destroy(shadow_data->mem_ctx);
2643 reply_nterror(req, NT_STATUS_NO_MEMORY);
2649 /* num_volumes 4 bytes */
2650 SIVAL(pdata,0,shadow_data->num_volumes);
2653 /* num_labels 4 bytes */
2654 SIVAL(pdata,4,shadow_data->num_volumes);
2657 /* needed_data_count 4 bytes */
2658 SIVAL(pdata,8,labels_data_count);
2662 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2663 shadow_data->num_volumes,fsp->fsp_name));
2664 if (labels && shadow_data->labels) {
2665 for (i=0;i<shadow_data->num_volumes;i++) {
2666 srvstr_push(pdata, req->flags2,
2667 cur_pdata, shadow_data->labels[i],
2668 2*sizeof(SHADOW_COPY_LABEL),
2669 STR_UNICODE|STR_TERMINATE);
2670 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2671 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2675 talloc_destroy(shadow_data->mem_ctx);
2677 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2683 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2685 /* pretend this succeeded -
2687 * we have to send back a list with all files owned by this SID
2689 * but I have to check that --metze
2693 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2695 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2697 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2701 /* unknown 4 bytes: this is not the length of the sid :-( */
2702 /*unknown = IVAL(pdata,0);*/
2704 sid_parse(pdata+4,sid_len,&sid);
2705 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2707 if (!sid_to_uid(&sid, &uid)) {
2708 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2709 sid_string_static(&sid),(unsigned long)sid_len));
2713 /* we can take a look at the find source :-)
2715 * find ./ -uid $uid -name '*' is what we need here
2718 * and send 4bytes len and then NULL terminated unicode strings
2721 * but I don't know how to deal with the paged results
2722 * (maybe we can hang the result anywhere in the fsp struct)
2724 * we don't send all files at once
2725 * and at the next we should *not* start from the beginning,
2726 * so we have to cache the result
2731 /* this works for now... */
2732 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2736 if (!logged_message) {
2737 logged_message = True; /* Only print this once... */
2738 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2743 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2747 #ifdef HAVE_SYS_QUOTAS
2748 /****************************************************************************
2749 Reply to get user quota
2750 ****************************************************************************/
2752 static void call_nt_transact_get_user_quota(connection_struct *conn,
2753 struct smb_request *req,
2757 uint32 parameter_count,
2760 uint32 max_data_count)
2762 NTSTATUS nt_status = NT_STATUS_OK;
2763 char *params = *ppparams;
2764 char *pdata = *ppdata;
2766 int data_len=0,param_len=0;
2769 files_struct *fsp = NULL;
2773 BOOL start_enum = True;
2774 SMB_NTQUOTA_STRUCT qt;
2775 SMB_NTQUOTA_LIST *tmp_list;
2776 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2781 if (current_user.ut.uid != 0) {
2782 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2783 lp_servicename(SNUM(conn)),conn->user));
2784 reply_doserror(req, ERRDOS, ERRnoaccess);
2789 * Ensure minimum number of parameters sent.
2792 if (parameter_count < 4) {
2793 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2794 reply_doserror(req, ERRDOS, ERRinvalidparam);
2798 /* maybe we can check the quota_fnum */
2799 fsp = file_fsp(SVAL(params,0));
2800 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2801 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2802 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2806 /* the NULL pointer checking for fsp->fake_file_handle->pd
2807 * is done by CHECK_NTQUOTA_HANDLE_OK()
2809 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2811 level = SVAL(params,2);
2813 /* unknown 12 bytes leading in params */
2816 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2817 /* seems that we should continue with the enum here --metze */
2819 if (qt_handle->quota_list!=NULL &&
2820 qt_handle->tmp_list==NULL) {
2823 free_ntquota_list(&(qt_handle->quota_list));
2825 /* Realloc the size of parameters and data we will return */
2827 params = nttrans_realloc(ppparams, param_len);
2828 if(params == NULL) {
2829 reply_doserror(req, ERRDOS, ERRnomem);
2834 SIVAL(params,0,data_len);
2841 case TRANSACT_GET_USER_QUOTA_LIST_START:
2843 if (qt_handle->quota_list==NULL &&
2844 qt_handle->tmp_list==NULL) {
2848 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2849 reply_doserror(req, ERRSRV, ERRerror);
2853 /* Realloc the size of parameters and data we will return */
2855 params = nttrans_realloc(ppparams, param_len);
2856 if(params == NULL) {
2857 reply_doserror(req, ERRDOS, ERRnomem);
2861 /* we should not trust the value in max_data_count*/
2862 max_data_count = MIN(max_data_count,2048);
2864 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2866 reply_doserror(req, ERRDOS, ERRnomem);
2872 /* set params Size of returned Quota Data 4 bytes*/
2873 /* but set it later when we know it */
2875 /* for each entry push the data */
2878 qt_handle->tmp_list = qt_handle->quota_list;
2881 tmp_list = qt_handle->tmp_list;
2883 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2884 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2886 sid_len = sid_size(&tmp_list->quotas->sid);
2887 entry_len = 40 + sid_len;
2889 /* nextoffset entry 4 bytes */
2890 SIVAL(entry,0,entry_len);
2892 /* then the len of the SID 4 bytes */
2893 SIVAL(entry,4,sid_len);
2895 /* unknown data 8 bytes SMB_BIG_UINT */
2896 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2898 /* the used disk space 8 bytes SMB_BIG_UINT */
2899 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2901 /* the soft quotas 8 bytes SMB_BIG_UINT */
2902 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2904 /* the hard quotas 8 bytes SMB_BIG_UINT */
2905 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2907 /* and now the SID */
2908 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2911 qt_handle->tmp_list = tmp_list;
2913 /* overwrite the offset of the last entry */
2914 SIVAL(entry-entry_len,0,0);
2916 data_len = 4+qt_len;
2917 /* overwrite the params quota_data_len */
2918 SIVAL(params,0,data_len);
2922 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2924 /* unknown 4 bytes IVAL(pdata,0) */
2926 if (data_count < 8) {
2927 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2928 reply_doserror(req, ERRDOS, ERRunknownlevel);
2932 sid_len = IVAL(pdata,4);
2933 /* Ensure this is less than 1mb. */
2934 if (sid_len > (1024*1024)) {
2935 reply_doserror(req, ERRDOS, ERRnomem);
2939 if (data_count < 8+sid_len) {
2940 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2941 reply_doserror(req, ERRDOS, ERRunknownlevel);
2945 data_len = 4+40+sid_len;
2947 if (max_data_count < data_len) {
2948 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2949 max_data_count, data_len));
2951 SIVAL(params,0,data_len);
2953 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2957 sid_parse(pdata+8,sid_len,&sid);
2959 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2962 * we have to return zero's in all fields
2963 * instead of returning an error here
2968 /* Realloc the size of parameters and data we will return */
2970 params = nttrans_realloc(ppparams, param_len);
2971 if(params == NULL) {
2972 reply_doserror(req, ERRDOS, ERRnomem);
2976 pdata = nttrans_realloc(ppdata, data_len);
2978 reply_doserror(req, ERRDOS, ERRnomem);
2984 /* set params Size of returned Quota Data 4 bytes*/
2985 SIVAL(params,0,data_len);
2987 /* nextoffset entry 4 bytes */
2990 /* then the len of the SID 4 bytes */
2991 SIVAL(entry,4,sid_len);
2993 /* unknown data 8 bytes SMB_BIG_UINT */
2994 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2996 /* the used disk space 8 bytes SMB_BIG_UINT */
2997 SBIG_UINT(entry,16,qt.usedspace);
2999 /* the soft quotas 8 bytes SMB_BIG_UINT */
3000 SBIG_UINT(entry,24,qt.softlim);
3002 /* the hard quotas 8 bytes SMB_BIG_UINT */
3003 SBIG_UINT(entry,32,qt.hardlim);
3005 /* and now the SID */
3006 sid_linearize(entry+40, sid_len, &sid);
3011 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
3012 reply_doserror(req, ERRSRV, ERRerror);
3017 send_nt_replies(req, nt_status, params, param_len,
3021 /****************************************************************************
3022 Reply to set user quota
3023 ****************************************************************************/
3025 static void call_nt_transact_set_user_quota(connection_struct *conn,
3026 struct smb_request *req,
3030 uint32 parameter_count,
3033 uint32 max_data_count)
3035 char *params = *ppparams;
3036 char *pdata = *ppdata;
3037 int data_len=0,param_len=0;
3038 SMB_NTQUOTA_STRUCT qt;
3041 files_struct *fsp = NULL;
3046 if (current_user.ut.uid != 0) {
3047 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
3048 lp_servicename(SNUM(conn)),conn->user));
3049 reply_doserror(req, ERRDOS, ERRnoaccess);
3054 * Ensure minimum number of parameters sent.
3057 if (parameter_count < 2) {
3058 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
3059 reply_doserror(req, ERRDOS, ERRinvalidparam);
3063 /* maybe we can check the quota_fnum */
3064 fsp = file_fsp(SVAL(params,0));
3065 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
3066 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
3067 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
3071 if (data_count < 40) {
3072 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
3073 reply_doserror(req, ERRDOS, ERRunknownlevel);
3077 /* offset to next quota record.
3078 * 4 bytes IVAL(pdata,0)
3083 sid_len = IVAL(pdata,4);
3085 if (data_count < 40+sid_len) {
3086 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
3087 reply_doserror(req, ERRDOS, ERRunknownlevel);
3091 /* unknown 8 bytes in pdata
3092 * maybe its the change time in NTTIME
3095 /* the used space 8 bytes (SMB_BIG_UINT)*/
3096 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
3097 #ifdef LARGE_SMB_OFF_T
3098 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
3099 #else /* LARGE_SMB_OFF_T */
3100 if ((IVAL(pdata,20) != 0)&&
3101 ((qt.usedspace != 0xFFFFFFFF)||
3102 (IVAL(pdata,20)!=0xFFFFFFFF))) {
3103 /* more than 32 bits? */
3104 reply_doserror(req, ERRDOS, ERRunknownlevel);
3107 #endif /* LARGE_SMB_OFF_T */
3109 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
3110 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
3111 #ifdef LARGE_SMB_OFF_T
3112 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
3113 #else /* LARGE_SMB_OFF_T */
3114 if ((IVAL(pdata,28) != 0)&&
3115 ((qt.softlim != 0xFFFFFFFF)||
3116 (IVAL(pdata,28)!=0xFFFFFFFF))) {
3117 /* more than 32 bits? */
3118 reply_doserror(req, ERRDOS, ERRunknownlevel);
3121 #endif /* LARGE_SMB_OFF_T */
3123 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
3124 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
3125 #ifdef LARGE_SMB_OFF_T
3126 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
3127 #else /* LARGE_SMB_OFF_T */
3128 if ((IVAL(pdata,36) != 0)&&
3129 ((qt.hardlim != 0xFFFFFFFF)||
3130 (IVAL(pdata,36)!=0xFFFFFFFF))) {
3131 /* more than 32 bits? */
3132 reply_doserror(req, ERRDOS, ERRunknownlevel);
3135 #endif /* LARGE_SMB_OFF_T */
3137 sid_parse(pdata+40,sid_len,&sid);
3138 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
3140 /* 44 unknown bytes left... */
3142 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
3143 reply_doserror(req, ERRSRV, ERRerror);
3147 send_nt_replies(req, NT_STATUS_OK, params, param_len,
3150 #endif /* HAVE_SYS_QUOTAS */
3152 static void handle_nttrans(connection_struct *conn,
3153 struct trans_state *state,
3154 struct smb_request *req)
3156 if (Protocol >= PROTOCOL_NT1) {
3157 req->flags2 |= 0x40; /* IS_LONG_NAME */
3158 SSVAL(req->inbuf,smb_flg2,req->flags2);
3161 /* Now we must call the relevant NT_TRANS function */
3162 switch(state->call) {
3163 case NT_TRANSACT_CREATE:
3165 START_PROFILE(NT_transact_create);
3166 call_nt_transact_create(
3168 &state->setup, state->setup_count,
3169 &state->param, state->total_param,
3170 &state->data, state->total_data,
3171 state->max_data_return);
3172 END_PROFILE(NT_transact_create);
3176 case NT_TRANSACT_IOCTL:
3178 START_PROFILE(NT_transact_ioctl);
3179 call_nt_transact_ioctl(
3181 &state->setup, state->setup_count,
3182 &state->param, state->total_param,
3183 &state->data, state->total_data,
3184 state->max_data_return);
3185 END_PROFILE(NT_transact_ioctl);
3189 case NT_TRANSACT_SET_SECURITY_DESC:
3191 START_PROFILE(NT_transact_set_security_desc);
3192 call_nt_transact_set_security_desc(
3194 &state->setup, state->setup_count,
3195 &state->param, state->total_param,
3196 &state->data, state->total_data,
3197 state->max_data_return);
3198 END_PROFILE(NT_transact_set_security_desc);
3202 case NT_TRANSACT_NOTIFY_CHANGE:
3204 START_PROFILE(NT_transact_notify_change);
3205 call_nt_transact_notify_change(
3207 &state->setup, state->setup_count,
3208 &state->param, state->total_param,
3209 &state->data, state->total_data,
3210 state->max_data_return,
3211 state->max_param_return);
3212 END_PROFILE(NT_transact_notify_change);
3216 case NT_TRANSACT_RENAME:
3218 START_PROFILE(NT_transact_rename);
3219 call_nt_transact_rename(
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_rename);
3229 case NT_TRANSACT_QUERY_SECURITY_DESC:
3231 START_PROFILE(NT_transact_query_security_desc);
3232 call_nt_transact_query_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_query_security_desc);
3242 #ifdef HAVE_SYS_QUOTAS
3243 case NT_TRANSACT_GET_USER_QUOTA:
3245 START_PROFILE(NT_transact_get_user_quota);
3246 call_nt_transact_get_user_quota(
3248 &state->setup, state->setup_count,
3249 &state->param, state->total_param,
3250 &state->data, state->total_data,
3251 state->max_data_return);
3252 END_PROFILE(NT_transact_get_user_quota);
3256 case NT_TRANSACT_SET_USER_QUOTA:
3258 START_PROFILE(NT_transact_set_user_quota);
3259 call_nt_transact_set_user_quota(
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_set_user_quota);
3268 #endif /* HAVE_SYS_QUOTAS */
3271 /* Error in request */
3272 DEBUG(0,("handle_nttrans: Unknown request %d in "
3273 "nttrans call\n", state->call));
3274 reply_doserror(req, ERRSRV, ERRerror);
3280 /****************************************************************************
3281 Reply to a SMBNTtrans.
3282 ****************************************************************************/
3284 void reply_nttrans(connection_struct *conn, struct smb_request *req)
3290 uint16 function_code;
3292 struct trans_state *state;
3295 START_PROFILE(SMBnttrans);
3297 if (req->wct < 19) {
3298 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3299 END_PROFILE(SMBnttrans);
3303 size = smb_len(req->inbuf) + 4;
3304 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
3305 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
3306 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
3307 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
3308 function_code = SVAL(req->inbuf, smb_nt_Function);
3310 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3311 reply_doserror(req, ERRSRV, ERRaccess);
3312 END_PROFILE(SMBnttrans);
3316 result = allow_new_trans(conn->pending_trans, req->mid);
3317 if (!NT_STATUS_IS_OK(result)) {
3318 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3319 reply_nterror(req, result);
3320 END_PROFILE(SMBnttrans);
3324 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3325 reply_doserror(req, ERRSRV, ERRaccess);
3326 END_PROFILE(SMBnttrans);
3330 state->cmd = SMBnttrans;
3332 state->mid = req->mid;
3333 state->vuid = req->vuid;
3334 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
3336 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
3337 state->param = NULL;
3338 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
3339 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
3341 /* setup count is in *words* */
3342 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
3343 state->setup = NULL;
3344 state->call = function_code;
3347 * All nttrans messages we handle have smb_wct == 19 +
3348 * state->setup_count. Ensure this is so as a sanity check.
3351 if(req->wct != 19 + (state->setup_count/2)) {
3352 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3353 req->wct, 19 + (state->setup_count/2)));
3357 /* Don't allow more than 128mb for each value. */
3358 if ((state->total_data > (1024*1024*128)) ||
3359 (state->total_param > (1024*1024*128))) {
3360 reply_doserror(req, ERRDOS, ERRnomem);
3361 END_PROFILE(SMBnttrans);
3365 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3368 if (state->total_data) {
3369 /* Can't use talloc here, the core routines do realloc on the
3370 * params and data. */
3371 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3372 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3373 "bytes !\n", (unsigned int)state->total_data));
3375 reply_doserror(req, ERRDOS, ERRnomem);
3376 END_PROFILE(SMBnttrans);
3379 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3381 if ((smb_base(req->inbuf)+dsoff+dscnt
3382 > (char *)req->inbuf + size) ||
3383 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
3386 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3389 if (state->total_param) {
3390 /* Can't use talloc here, the core routines do realloc on the
3391 * params and data. */
3392 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3393 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3394 "bytes !\n", (unsigned int)state->total_param));
3395 SAFE_FREE(state->data);
3397 reply_doserror(req, ERRDOS, ERRnomem);
3398 END_PROFILE(SMBnttrans);
3401 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3403 if ((smb_base(req->inbuf)+psoff+pscnt
3404 > (char *)req->inbuf + size) ||
3405 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
3408 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3411 state->received_data = dscnt;
3412 state->received_param = pscnt;
3414 if(state->setup_count > 0) {
3415 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3416 state->setup_count));
3417 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3418 if (state->setup == NULL) {
3419 DEBUG(0,("reply_nttrans : Out of memory\n"));
3420 SAFE_FREE(state->data);
3421 SAFE_FREE(state->param);
3423 reply_doserror(req, ERRDOS, ERRnomem);
3424 END_PROFILE(SMBnttrans);
3428 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3429 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3432 if (smb_nt_SetupStart + state->setup_count > size) {
3436 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
3437 state->setup_count);
3438 dump_data(10, (uint8 *)state->setup, state->setup_count);
3441 if ((state->received_data == state->total_data) &&
3442 (state->received_param == state->total_param)) {
3443 handle_nttrans(conn, state, req);
3444 SAFE_FREE(state->param);
3445 SAFE_FREE(state->data);
3447 END_PROFILE(SMBnttrans);
3451 DLIST_ADD(conn->pending_trans, state);
3453 /* We need to send an interim response then receive the rest
3454 of the parameter/data bytes */
3455 reply_outbuf(req, 0, 0);
3456 show_msg((char *)req->outbuf);
3457 END_PROFILE(SMBnttrans);
3462 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3463 SAFE_FREE(state->data);
3464 SAFE_FREE(state->param);
3466 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3467 END_PROFILE(SMBnttrans);
3471 /****************************************************************************
3472 Reply to a SMBnttranss
3473 ****************************************************************************/
3475 void reply_nttranss(connection_struct *conn, struct smb_request *req)
3477 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3478 struct trans_state *state;
3482 START_PROFILE(SMBnttranss);
3484 show_msg((char *)req->inbuf);
3486 if (req->wct < 18) {
3487 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3488 END_PROFILE(SMBnttranss);
3492 for (state = conn->pending_trans; state != NULL;
3493 state = state->next) {
3494 if (state->mid == req->mid) {
3499 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3500 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3501 END_PROFILE(SMBnttranss);
3505 /* Revise state->total_param and state->total_data in case they have
3506 changed downwards */
3507 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
3508 < state->total_param) {
3509 state->total_param = IVAL(req->inbuf,
3510 smb_nts_TotalParameterCount);
3512 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
3513 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
3516 size = smb_len(req->inbuf) + 4;
3518 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
3519 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
3520 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
3522 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
3523 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
3524 doff = IVAL(req->inbuf, smb_nts_DataOffset);
3526 state->received_param += pcnt;
3527 state->received_data += dcnt;
3529 if ((state->received_data > state->total_data) ||
3530 (state->received_param > state->total_param))
3534 if (pdisp+pcnt > state->total_param)
3536 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3538 if (pdisp > state->total_param)
3540 if ((smb_base(req->inbuf) + poff + pcnt
3541 > (char *)req->inbuf + size) ||
3542 (smb_base(req->inbuf) + poff + pcnt
3543 < smb_base(req->inbuf)))
3545 if (state->param + pdisp < state->param)
3548 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
3553 if (ddisp+dcnt > state->total_data)
3555 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3557 if (ddisp > state->total_data)
3559 if ((smb_base(req->inbuf) + doff + dcnt
3560 > (char *)req->inbuf + size) ||
3561 (smb_base(req->inbuf) + doff + dcnt
3562 < smb_base(req->inbuf)))
3564 if (state->data + ddisp < state->data)
3567 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
3571 if ((state->received_param < state->total_param) ||
3572 (state->received_data < state->total_data)) {
3573 END_PROFILE(SMBnttranss);
3578 * construct_reply_common will copy smb_com from inbuf to
3579 * outbuf. SMBnttranss is wrong here.
3581 SCVAL(req->inbuf,smb_com,SMBnttrans);
3583 handle_nttrans(conn, state, req);
3585 DLIST_REMOVE(conn->pending_trans, state);
3586 SAFE_FREE(state->data);
3587 SAFE_FREE(state->param);
3589 END_PROFILE(SMBnttranss);
3594 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3595 DLIST_REMOVE(conn->pending_trans, state);
3596 SAFE_FREE(state->data);
3597 SAFE_FREE(state->param);
3599 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3600 END_PROFILE(SMBnttranss);