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(ctx, conn, req, oldname,
2054 newname, 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,
2067 case RENAME_FLAG_COPY:
2068 if (src_has_wcard || dest_has_wcard) {
2070 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2072 status = copy_internals(conn, req, oldname,
2076 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
2077 status = NT_STATUS_INVALID_PARAMETER;
2080 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
2084 if (!NT_STATUS_IS_OK(status)) {
2085 if (open_was_deferred(req->mid)) {
2086 /* We have re-scheduled this call. */
2087 END_PROFILE(SMBntrename);
2091 reply_nterror(req, status);
2092 END_PROFILE(SMBntrename);
2096 reply_outbuf(req, 0, 0);
2098 END_PROFILE(SMBntrename);
2102 /****************************************************************************
2103 Reply to a notify change - queue the request and
2104 don't allow a directory to be opened.
2105 ****************************************************************************/
2107 static void call_nt_transact_notify_change(connection_struct *conn,
2108 struct smb_request *req,
2112 uint32 parameter_count,
2113 char **ppdata, uint32 data_count,
2114 uint32 max_data_count,
2115 uint32 max_param_count)
2117 uint16 *setup = *ppsetup;
2123 if(setup_count < 6) {
2124 reply_doserror(req, ERRDOS, ERRbadfunc);
2128 fsp = file_fsp(SVAL(setup,4));
2129 filter = IVAL(setup, 0);
2130 recursive = (SVAL(setup, 6) != 0) ? True : False;
2132 DEBUG(3,("call_nt_transact_notify_change\n"));
2135 reply_doserror(req, ERRDOS, ERRbadfid);
2140 char *filter_string;
2142 if (!(filter_string = notify_filter_string(NULL, filter))) {
2143 reply_nterror(req,NT_STATUS_NO_MEMORY);
2147 DEBUG(3,("call_nt_transact_notify_change: notify change "
2148 "called on %s, filter = %s, recursive = %d\n",
2149 fsp->fsp_name, filter_string, recursive));
2151 TALLOC_FREE(filter_string);
2154 if((!fsp->is_directory) || (conn != fsp->conn)) {
2155 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2159 if (fsp->notify == NULL) {
2161 status = change_notify_create(fsp, filter, recursive);
2163 if (!NT_STATUS_IS_OK(status)) {
2164 DEBUG(10, ("change_notify_create returned %s\n",
2165 nt_errstr(status)));
2166 reply_nterror(req, status);
2171 if (fsp->notify->num_changes != 0) {
2174 * We've got changes pending, respond immediately
2178 * TODO: write a torture test to check the filtering behaviour
2182 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
2185 * change_notify_reply() above has independently sent its
2192 * No changes pending, queue the request
2195 status = change_notify_add_request(req->inbuf, max_param_count, filter,
2197 if (!NT_STATUS_IS_OK(status)) {
2198 reply_nterror(req, status);
2203 /****************************************************************************
2204 Reply to an NT transact rename command.
2205 ****************************************************************************/
2207 static void call_nt_transact_rename(connection_struct *conn,
2208 struct smb_request *req,
2209 uint16 **ppsetup, uint32 setup_count,
2210 char **ppparams, uint32 parameter_count,
2211 char **ppdata, uint32 data_count,
2212 uint32 max_data_count)
2214 char *params = *ppparams;
2215 char *new_name = NULL;
2216 files_struct *fsp = NULL;
2217 BOOL replace_if_exists = False;
2218 BOOL dest_has_wcard = False;
2220 TALLOC_CTX *ctx = talloc_tos();
2222 if(parameter_count < 5) {
2223 reply_doserror(req, ERRDOS, ERRbadfunc);
2227 fsp = file_fsp(SVAL(params, 0));
2228 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2229 if (!check_fsp(conn, req, fsp, ¤t_user)) {
2232 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
2233 parameter_count - 4,
2234 STR_TERMINATE, &status, &dest_has_wcard);
2235 if (!NT_STATUS_IS_OK(status)) {
2236 reply_nterror(req, status);
2240 status = rename_internals(ctx,
2250 if (!NT_STATUS_IS_OK(status)) {
2251 if (open_was_deferred(req->mid)) {
2252 /* We have re-scheduled this call. */
2255 reply_nterror(req, status);
2260 * Rename was successful.
2262 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2264 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2265 fsp->fsp_name, new_name));
2270 /******************************************************************************
2271 Fake up a completely empty SD.
2272 *******************************************************************************/
2274 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2278 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2280 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2287 /****************************************************************************
2288 Reply to query a security descriptor.
2289 ****************************************************************************/
2291 static void call_nt_transact_query_security_desc(connection_struct *conn,
2292 struct smb_request *req,
2296 uint32 parameter_count,
2299 uint32 max_data_count)
2301 char *params = *ppparams;
2302 char *data = *ppdata;
2304 SEC_DESC *psd = NULL;
2306 uint32 security_info_wanted;
2307 TALLOC_CTX *mem_ctx;
2308 files_struct *fsp = NULL;
2310 if(parameter_count < 8) {
2311 reply_doserror(req, ERRDOS, ERRbadfunc);
2315 fsp = file_fsp(SVAL(params,0));
2317 reply_doserror(req, ERRDOS, ERRbadfid);
2321 security_info_wanted = IVAL(params,4);
2323 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2324 (unsigned int)security_info_wanted ));
2326 params = nttrans_realloc(ppparams, 4);
2327 if(params == NULL) {
2328 reply_doserror(req, ERRDOS, ERRnomem);
2332 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2333 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2334 reply_doserror(req, ERRDOS, ERRnomem);
2339 * Get the permissions to return.
2342 if (!lp_nt_acl_support(SNUM(conn))) {
2343 sd_size = get_null_nt_acl(mem_ctx, &psd);
2345 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
2349 talloc_destroy(mem_ctx);
2350 reply_unixerror(req, ERRDOS, ERRnoaccess);
2354 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2356 SIVAL(params,0,(uint32)sd_size);
2358 if(max_data_count < sd_size) {
2360 send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
2361 params, 4, *ppdata, 0);
2362 talloc_destroy(mem_ctx);
2367 * Allocate the data we will point this at.
2370 data = nttrans_realloc(ppdata, sd_size);
2372 talloc_destroy(mem_ctx);
2373 reply_doserror(req, ERRDOS, ERRnomem);
2378 * Init the parse struct we will marshall into.
2381 prs_init(&pd, 0, mem_ctx, MARSHALL);
2384 * Setup the prs_struct to point at the memory we just
2388 prs_give_memory( &pd, data, (uint32)sd_size, False);
2391 * Finally, linearize into the outgoing buffer.
2394 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2395 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2396 security descriptor.\n"));
2398 * Return access denied for want of a better error message..
2400 talloc_destroy(mem_ctx);
2401 reply_unixerror(req, ERRDOS, ERRnoaccess);
2406 * Now we can delete the security descriptor.
2409 talloc_destroy(mem_ctx);
2411 send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2415 /****************************************************************************
2416 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2417 ****************************************************************************/
2419 static void call_nt_transact_set_security_desc(connection_struct *conn,
2420 struct smb_request *req,
2424 uint32 parameter_count,
2427 uint32 max_data_count)
2429 char *params= *ppparams;
2430 char *data = *ppdata;
2431 files_struct *fsp = NULL;
2432 uint32 security_info_sent = 0;
2435 if(parameter_count < 8) {
2436 reply_doserror(req, ERRDOS, ERRbadfunc);
2440 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
2441 reply_doserror(req, ERRDOS, ERRbadfid);
2445 if(!lp_nt_acl_support(SNUM(conn))) {
2449 security_info_sent = IVAL(params,4);
2451 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2452 (unsigned int)security_info_sent ));
2454 if (data_count == 0) {
2455 reply_doserror(req, ERRDOS, ERRnoaccess);
2459 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2460 reply_nterror(req, nt_status);
2466 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2470 /****************************************************************************
2472 ****************************************************************************/
2474 static void call_nt_transact_ioctl(connection_struct *conn,
2475 struct smb_request *req,
2476 uint16 **ppsetup, uint32 setup_count,
2477 char **ppparams, uint32 parameter_count,
2478 char **ppdata, uint32 data_count,
2479 uint32 max_data_count)
2486 static BOOL logged_message;
2487 char *pdata = *ppdata;
2489 if (setup_count != 8) {
2490 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2491 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2495 function = IVAL(*ppsetup, 0);
2496 fidnum = SVAL(*ppsetup, 4);
2497 isFSctl = CVAL(*ppsetup, 6);
2498 compfilter = CVAL(*ppsetup, 7);
2500 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2501 function, fidnum, isFSctl, compfilter));
2503 fsp=file_fsp(fidnum);
2504 /* this check is done in each implemented function case for now
2505 because I don't want to break anything... --metze
2506 FSP_BELONGS_CONN(fsp,conn);*/
2509 case FSCTL_SET_SPARSE:
2510 /* pretend this succeeded - tho strictly we should
2511 mark the file sparse (if the local fs supports it)
2512 so we can know if we need to pre-allocate or not */
2514 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2515 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2518 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2520 unsigned char objid[16];
2522 /* This should return the object-id on this file.
2523 * I think I'll make this be the inode+dev. JRA.
2526 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2528 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2533 pdata = nttrans_realloc(ppdata, data_count);
2534 if (pdata == NULL) {
2535 reply_nterror(req, NT_STATUS_NO_MEMORY);
2538 push_file_id_16(pdata, &fsp->file_id);
2539 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2540 push_file_id_16(pdata+32, &fsp->file_id);
2541 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2546 case FSCTL_GET_REPARSE_POINT:
2547 /* pretend this fail - my winXP does it like this
2551 DEBUG(10,("FSCTL_GET_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_SET_REPARSE_POINT:
2556 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2560 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2561 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2564 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2567 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2568 * and return their volume names. If max_data_count is 16, then it is just
2569 * asking for the number of volumes and length of the combined names.
2571 * pdata is the data allocated by our caller, but that uses
2572 * total_data_count (which is 0 in our case) rather than max_data_count.
2573 * Allocate the correct amount and return the pointer to let
2574 * it be deallocated when we return.
2576 SHADOW_COPY_DATA *shadow_data = NULL;
2577 TALLOC_CTX *shadow_mem_ctx = NULL;
2578 BOOL labels = False;
2579 uint32 labels_data_count = 0;
2583 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2587 if (max_data_count < 16) {
2588 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2590 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2594 if (max_data_count > 16) {
2598 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2599 if (shadow_mem_ctx == NULL) {
2600 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2601 reply_nterror(req, NT_STATUS_NO_MEMORY);
2605 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2606 if (shadow_data == NULL) {
2607 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2608 talloc_destroy(shadow_mem_ctx);
2609 reply_nterror(req, NT_STATUS_NO_MEMORY);
2613 shadow_data->mem_ctx = shadow_mem_ctx;
2616 * Call the VFS routine to actually do the work.
2618 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2619 talloc_destroy(shadow_data->mem_ctx);
2620 if (errno == ENOSYS) {
2621 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2622 conn->connectpath));
2623 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2626 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2627 conn->connectpath));
2628 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2633 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2638 data_count = 12+labels_data_count+4;
2641 if (max_data_count<data_count) {
2642 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2643 max_data_count,data_count));
2644 talloc_destroy(shadow_data->mem_ctx);
2645 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2649 pdata = nttrans_realloc(ppdata, data_count);
2650 if (pdata == NULL) {
2651 talloc_destroy(shadow_data->mem_ctx);
2652 reply_nterror(req, NT_STATUS_NO_MEMORY);
2658 /* num_volumes 4 bytes */
2659 SIVAL(pdata,0,shadow_data->num_volumes);
2662 /* num_labels 4 bytes */
2663 SIVAL(pdata,4,shadow_data->num_volumes);
2666 /* needed_data_count 4 bytes */
2667 SIVAL(pdata,8,labels_data_count);
2671 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2672 shadow_data->num_volumes,fsp->fsp_name));
2673 if (labels && shadow_data->labels) {
2674 for (i=0;i<shadow_data->num_volumes;i++) {
2675 srvstr_push(pdata, req->flags2,
2676 cur_pdata, shadow_data->labels[i],
2677 2*sizeof(SHADOW_COPY_LABEL),
2678 STR_UNICODE|STR_TERMINATE);
2679 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2680 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2684 talloc_destroy(shadow_data->mem_ctx);
2686 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2692 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2694 /* pretend this succeeded -
2696 * we have to send back a list with all files owned by this SID
2698 * but I have to check that --metze
2702 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2704 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2706 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2710 /* unknown 4 bytes: this is not the length of the sid :-( */
2711 /*unknown = IVAL(pdata,0);*/
2713 sid_parse(pdata+4,sid_len,&sid);
2714 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2716 if (!sid_to_uid(&sid, &uid)) {
2717 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2718 sid_string_static(&sid),(unsigned long)sid_len));
2722 /* we can take a look at the find source :-)
2724 * find ./ -uid $uid -name '*' is what we need here
2727 * and send 4bytes len and then NULL terminated unicode strings
2730 * but I don't know how to deal with the paged results
2731 * (maybe we can hang the result anywhere in the fsp struct)
2733 * we don't send all files at once
2734 * and at the next we should *not* start from the beginning,
2735 * so we have to cache the result
2740 /* this works for now... */
2741 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2745 if (!logged_message) {
2746 logged_message = True; /* Only print this once... */
2747 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2752 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2756 #ifdef HAVE_SYS_QUOTAS
2757 /****************************************************************************
2758 Reply to get user quota
2759 ****************************************************************************/
2761 static void call_nt_transact_get_user_quota(connection_struct *conn,
2762 struct smb_request *req,
2766 uint32 parameter_count,
2769 uint32 max_data_count)
2771 NTSTATUS nt_status = NT_STATUS_OK;
2772 char *params = *ppparams;
2773 char *pdata = *ppdata;
2775 int data_len=0,param_len=0;
2778 files_struct *fsp = NULL;
2782 BOOL start_enum = True;
2783 SMB_NTQUOTA_STRUCT qt;
2784 SMB_NTQUOTA_LIST *tmp_list;
2785 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2790 if (current_user.ut.uid != 0) {
2791 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2792 lp_servicename(SNUM(conn)),conn->user));
2793 reply_doserror(req, ERRDOS, ERRnoaccess);
2798 * Ensure minimum number of parameters sent.
2801 if (parameter_count < 4) {
2802 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2803 reply_doserror(req, ERRDOS, ERRinvalidparam);
2807 /* maybe we can check the quota_fnum */
2808 fsp = file_fsp(SVAL(params,0));
2809 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2810 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2811 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2815 /* the NULL pointer checking for fsp->fake_file_handle->pd
2816 * is done by CHECK_NTQUOTA_HANDLE_OK()
2818 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2820 level = SVAL(params,2);
2822 /* unknown 12 bytes leading in params */
2825 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2826 /* seems that we should continue with the enum here --metze */
2828 if (qt_handle->quota_list!=NULL &&
2829 qt_handle->tmp_list==NULL) {
2832 free_ntquota_list(&(qt_handle->quota_list));
2834 /* Realloc the size of parameters and data we will return */
2836 params = nttrans_realloc(ppparams, param_len);
2837 if(params == NULL) {
2838 reply_doserror(req, ERRDOS, ERRnomem);
2843 SIVAL(params,0,data_len);
2850 case TRANSACT_GET_USER_QUOTA_LIST_START:
2852 if (qt_handle->quota_list==NULL &&
2853 qt_handle->tmp_list==NULL) {
2857 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2858 reply_doserror(req, ERRSRV, ERRerror);
2862 /* Realloc the size of parameters and data we will return */
2864 params = nttrans_realloc(ppparams, param_len);
2865 if(params == NULL) {
2866 reply_doserror(req, ERRDOS, ERRnomem);
2870 /* we should not trust the value in max_data_count*/
2871 max_data_count = MIN(max_data_count,2048);
2873 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2875 reply_doserror(req, ERRDOS, ERRnomem);
2881 /* set params Size of returned Quota Data 4 bytes*/
2882 /* but set it later when we know it */
2884 /* for each entry push the data */
2887 qt_handle->tmp_list = qt_handle->quota_list;
2890 tmp_list = qt_handle->tmp_list;
2892 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2893 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2895 sid_len = sid_size(&tmp_list->quotas->sid);
2896 entry_len = 40 + sid_len;
2898 /* nextoffset entry 4 bytes */
2899 SIVAL(entry,0,entry_len);
2901 /* then the len of the SID 4 bytes */
2902 SIVAL(entry,4,sid_len);
2904 /* unknown data 8 bytes SMB_BIG_UINT */
2905 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2907 /* the used disk space 8 bytes SMB_BIG_UINT */
2908 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2910 /* the soft quotas 8 bytes SMB_BIG_UINT */
2911 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2913 /* the hard quotas 8 bytes SMB_BIG_UINT */
2914 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2916 /* and now the SID */
2917 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2920 qt_handle->tmp_list = tmp_list;
2922 /* overwrite the offset of the last entry */
2923 SIVAL(entry-entry_len,0,0);
2925 data_len = 4+qt_len;
2926 /* overwrite the params quota_data_len */
2927 SIVAL(params,0,data_len);
2931 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2933 /* unknown 4 bytes IVAL(pdata,0) */
2935 if (data_count < 8) {
2936 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2937 reply_doserror(req, ERRDOS, ERRunknownlevel);
2941 sid_len = IVAL(pdata,4);
2942 /* Ensure this is less than 1mb. */
2943 if (sid_len > (1024*1024)) {
2944 reply_doserror(req, ERRDOS, ERRnomem);
2948 if (data_count < 8+sid_len) {
2949 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2950 reply_doserror(req, ERRDOS, ERRunknownlevel);
2954 data_len = 4+40+sid_len;
2956 if (max_data_count < data_len) {
2957 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2958 max_data_count, data_len));
2960 SIVAL(params,0,data_len);
2962 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2966 sid_parse(pdata+8,sid_len,&sid);
2968 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2971 * we have to return zero's in all fields
2972 * instead of returning an error here
2977 /* Realloc the size of parameters and data we will return */
2979 params = nttrans_realloc(ppparams, param_len);
2980 if(params == NULL) {
2981 reply_doserror(req, ERRDOS, ERRnomem);
2985 pdata = nttrans_realloc(ppdata, data_len);
2987 reply_doserror(req, ERRDOS, ERRnomem);
2993 /* set params Size of returned Quota Data 4 bytes*/
2994 SIVAL(params,0,data_len);
2996 /* nextoffset entry 4 bytes */
2999 /* then the len of the SID 4 bytes */
3000 SIVAL(entry,4,sid_len);
3002 /* unknown data 8 bytes SMB_BIG_UINT */
3003 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
3005 /* the used disk space 8 bytes SMB_BIG_UINT */
3006 SBIG_UINT(entry,16,qt.usedspace);
3008 /* the soft quotas 8 bytes SMB_BIG_UINT */
3009 SBIG_UINT(entry,24,qt.softlim);
3011 /* the hard quotas 8 bytes SMB_BIG_UINT */
3012 SBIG_UINT(entry,32,qt.hardlim);
3014 /* and now the SID */
3015 sid_linearize(entry+40, sid_len, &sid);
3020 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
3021 reply_doserror(req, ERRSRV, ERRerror);
3026 send_nt_replies(req, nt_status, params, param_len,
3030 /****************************************************************************
3031 Reply to set user quota
3032 ****************************************************************************/
3034 static void call_nt_transact_set_user_quota(connection_struct *conn,
3035 struct smb_request *req,
3039 uint32 parameter_count,
3042 uint32 max_data_count)
3044 char *params = *ppparams;
3045 char *pdata = *ppdata;
3046 int data_len=0,param_len=0;
3047 SMB_NTQUOTA_STRUCT qt;
3050 files_struct *fsp = NULL;
3055 if (current_user.ut.uid != 0) {
3056 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
3057 lp_servicename(SNUM(conn)),conn->user));
3058 reply_doserror(req, ERRDOS, ERRnoaccess);
3063 * Ensure minimum number of parameters sent.
3066 if (parameter_count < 2) {
3067 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
3068 reply_doserror(req, ERRDOS, ERRinvalidparam);
3072 /* maybe we can check the quota_fnum */
3073 fsp = file_fsp(SVAL(params,0));
3074 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
3075 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
3076 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
3080 if (data_count < 40) {
3081 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
3082 reply_doserror(req, ERRDOS, ERRunknownlevel);
3086 /* offset to next quota record.
3087 * 4 bytes IVAL(pdata,0)
3092 sid_len = IVAL(pdata,4);
3094 if (data_count < 40+sid_len) {
3095 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
3096 reply_doserror(req, ERRDOS, ERRunknownlevel);
3100 /* unknown 8 bytes in pdata
3101 * maybe its the change time in NTTIME
3104 /* the used space 8 bytes (SMB_BIG_UINT)*/
3105 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
3106 #ifdef LARGE_SMB_OFF_T
3107 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
3108 #else /* LARGE_SMB_OFF_T */
3109 if ((IVAL(pdata,20) != 0)&&
3110 ((qt.usedspace != 0xFFFFFFFF)||
3111 (IVAL(pdata,20)!=0xFFFFFFFF))) {
3112 /* more than 32 bits? */
3113 reply_doserror(req, ERRDOS, ERRunknownlevel);
3116 #endif /* LARGE_SMB_OFF_T */
3118 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
3119 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
3120 #ifdef LARGE_SMB_OFF_T
3121 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
3122 #else /* LARGE_SMB_OFF_T */
3123 if ((IVAL(pdata,28) != 0)&&
3124 ((qt.softlim != 0xFFFFFFFF)||
3125 (IVAL(pdata,28)!=0xFFFFFFFF))) {
3126 /* more than 32 bits? */
3127 reply_doserror(req, ERRDOS, ERRunknownlevel);
3130 #endif /* LARGE_SMB_OFF_T */
3132 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
3133 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
3134 #ifdef LARGE_SMB_OFF_T
3135 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
3136 #else /* LARGE_SMB_OFF_T */
3137 if ((IVAL(pdata,36) != 0)&&
3138 ((qt.hardlim != 0xFFFFFFFF)||
3139 (IVAL(pdata,36)!=0xFFFFFFFF))) {
3140 /* more than 32 bits? */
3141 reply_doserror(req, ERRDOS, ERRunknownlevel);
3144 #endif /* LARGE_SMB_OFF_T */
3146 sid_parse(pdata+40,sid_len,&sid);
3147 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
3149 /* 44 unknown bytes left... */
3151 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
3152 reply_doserror(req, ERRSRV, ERRerror);
3156 send_nt_replies(req, NT_STATUS_OK, params, param_len,
3159 #endif /* HAVE_SYS_QUOTAS */
3161 static void handle_nttrans(connection_struct *conn,
3162 struct trans_state *state,
3163 struct smb_request *req)
3165 if (Protocol >= PROTOCOL_NT1) {
3166 req->flags2 |= 0x40; /* IS_LONG_NAME */
3167 SSVAL(req->inbuf,smb_flg2,req->flags2);
3170 /* Now we must call the relevant NT_TRANS function */
3171 switch(state->call) {
3172 case NT_TRANSACT_CREATE:
3174 START_PROFILE(NT_transact_create);
3175 call_nt_transact_create(
3177 &state->setup, state->setup_count,
3178 &state->param, state->total_param,
3179 &state->data, state->total_data,
3180 state->max_data_return);
3181 END_PROFILE(NT_transact_create);
3185 case NT_TRANSACT_IOCTL:
3187 START_PROFILE(NT_transact_ioctl);
3188 call_nt_transact_ioctl(
3190 &state->setup, state->setup_count,
3191 &state->param, state->total_param,
3192 &state->data, state->total_data,
3193 state->max_data_return);
3194 END_PROFILE(NT_transact_ioctl);
3198 case NT_TRANSACT_SET_SECURITY_DESC:
3200 START_PROFILE(NT_transact_set_security_desc);
3201 call_nt_transact_set_security_desc(
3203 &state->setup, state->setup_count,
3204 &state->param, state->total_param,
3205 &state->data, state->total_data,
3206 state->max_data_return);
3207 END_PROFILE(NT_transact_set_security_desc);
3211 case NT_TRANSACT_NOTIFY_CHANGE:
3213 START_PROFILE(NT_transact_notify_change);
3214 call_nt_transact_notify_change(
3216 &state->setup, state->setup_count,
3217 &state->param, state->total_param,
3218 &state->data, state->total_data,
3219 state->max_data_return,
3220 state->max_param_return);
3221 END_PROFILE(NT_transact_notify_change);
3225 case NT_TRANSACT_RENAME:
3227 START_PROFILE(NT_transact_rename);
3228 call_nt_transact_rename(
3230 &state->setup, state->setup_count,
3231 &state->param, state->total_param,
3232 &state->data, state->total_data,
3233 state->max_data_return);
3234 END_PROFILE(NT_transact_rename);
3238 case NT_TRANSACT_QUERY_SECURITY_DESC:
3240 START_PROFILE(NT_transact_query_security_desc);
3241 call_nt_transact_query_security_desc(
3243 &state->setup, state->setup_count,
3244 &state->param, state->total_param,
3245 &state->data, state->total_data,
3246 state->max_data_return);
3247 END_PROFILE(NT_transact_query_security_desc);
3251 #ifdef HAVE_SYS_QUOTAS
3252 case NT_TRANSACT_GET_USER_QUOTA:
3254 START_PROFILE(NT_transact_get_user_quota);
3255 call_nt_transact_get_user_quota(
3257 &state->setup, state->setup_count,
3258 &state->param, state->total_param,
3259 &state->data, state->total_data,
3260 state->max_data_return);
3261 END_PROFILE(NT_transact_get_user_quota);
3265 case NT_TRANSACT_SET_USER_QUOTA:
3267 START_PROFILE(NT_transact_set_user_quota);
3268 call_nt_transact_set_user_quota(
3270 &state->setup, state->setup_count,
3271 &state->param, state->total_param,
3272 &state->data, state->total_data,
3273 state->max_data_return);
3274 END_PROFILE(NT_transact_set_user_quota);
3277 #endif /* HAVE_SYS_QUOTAS */
3280 /* Error in request */
3281 DEBUG(0,("handle_nttrans: Unknown request %d in "
3282 "nttrans call\n", state->call));
3283 reply_doserror(req, ERRSRV, ERRerror);
3289 /****************************************************************************
3290 Reply to a SMBNTtrans.
3291 ****************************************************************************/
3293 void reply_nttrans(connection_struct *conn, struct smb_request *req)
3299 uint16 function_code;
3301 struct trans_state *state;
3304 START_PROFILE(SMBnttrans);
3306 if (req->wct < 19) {
3307 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3308 END_PROFILE(SMBnttrans);
3312 size = smb_len(req->inbuf) + 4;
3313 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
3314 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
3315 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
3316 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
3317 function_code = SVAL(req->inbuf, smb_nt_Function);
3319 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3320 reply_doserror(req, ERRSRV, ERRaccess);
3321 END_PROFILE(SMBnttrans);
3325 result = allow_new_trans(conn->pending_trans, req->mid);
3326 if (!NT_STATUS_IS_OK(result)) {
3327 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3328 reply_nterror(req, result);
3329 END_PROFILE(SMBnttrans);
3333 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3334 reply_doserror(req, ERRSRV, ERRaccess);
3335 END_PROFILE(SMBnttrans);
3339 state->cmd = SMBnttrans;
3341 state->mid = req->mid;
3342 state->vuid = req->vuid;
3343 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
3345 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
3346 state->param = NULL;
3347 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
3348 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
3350 /* setup count is in *words* */
3351 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
3352 state->setup = NULL;
3353 state->call = function_code;
3356 * All nttrans messages we handle have smb_wct == 19 +
3357 * state->setup_count. Ensure this is so as a sanity check.
3360 if(req->wct != 19 + (state->setup_count/2)) {
3361 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3362 req->wct, 19 + (state->setup_count/2)));
3366 /* Don't allow more than 128mb for each value. */
3367 if ((state->total_data > (1024*1024*128)) ||
3368 (state->total_param > (1024*1024*128))) {
3369 reply_doserror(req, ERRDOS, ERRnomem);
3370 END_PROFILE(SMBnttrans);
3374 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3377 if (state->total_data) {
3378 /* Can't use talloc here, the core routines do realloc on the
3379 * params and data. */
3380 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3381 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3382 "bytes !\n", (unsigned int)state->total_data));
3384 reply_doserror(req, ERRDOS, ERRnomem);
3385 END_PROFILE(SMBnttrans);
3388 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3390 if ((smb_base(req->inbuf)+dsoff+dscnt
3391 > (char *)req->inbuf + size) ||
3392 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
3395 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3398 if (state->total_param) {
3399 /* Can't use talloc here, the core routines do realloc on the
3400 * params and data. */
3401 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3402 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3403 "bytes !\n", (unsigned int)state->total_param));
3404 SAFE_FREE(state->data);
3406 reply_doserror(req, ERRDOS, ERRnomem);
3407 END_PROFILE(SMBnttrans);
3410 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3412 if ((smb_base(req->inbuf)+psoff+pscnt
3413 > (char *)req->inbuf + size) ||
3414 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
3417 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3420 state->received_data = dscnt;
3421 state->received_param = pscnt;
3423 if(state->setup_count > 0) {
3424 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3425 state->setup_count));
3426 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3427 if (state->setup == NULL) {
3428 DEBUG(0,("reply_nttrans : Out of memory\n"));
3429 SAFE_FREE(state->data);
3430 SAFE_FREE(state->param);
3432 reply_doserror(req, ERRDOS, ERRnomem);
3433 END_PROFILE(SMBnttrans);
3437 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3438 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3441 if (smb_nt_SetupStart + state->setup_count > size) {
3445 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
3446 state->setup_count);
3447 dump_data(10, (uint8 *)state->setup, state->setup_count);
3450 if ((state->received_data == state->total_data) &&
3451 (state->received_param == state->total_param)) {
3452 handle_nttrans(conn, state, req);
3453 SAFE_FREE(state->param);
3454 SAFE_FREE(state->data);
3456 END_PROFILE(SMBnttrans);
3460 DLIST_ADD(conn->pending_trans, state);
3462 /* We need to send an interim response then receive the rest
3463 of the parameter/data bytes */
3464 reply_outbuf(req, 0, 0);
3465 show_msg((char *)req->outbuf);
3466 END_PROFILE(SMBnttrans);
3471 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3472 SAFE_FREE(state->data);
3473 SAFE_FREE(state->param);
3475 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3476 END_PROFILE(SMBnttrans);
3480 /****************************************************************************
3481 Reply to a SMBnttranss
3482 ****************************************************************************/
3484 void reply_nttranss(connection_struct *conn, struct smb_request *req)
3486 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3487 struct trans_state *state;
3491 START_PROFILE(SMBnttranss);
3493 show_msg((char *)req->inbuf);
3495 if (req->wct < 18) {
3496 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3497 END_PROFILE(SMBnttranss);
3501 for (state = conn->pending_trans; state != NULL;
3502 state = state->next) {
3503 if (state->mid == req->mid) {
3508 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3509 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3510 END_PROFILE(SMBnttranss);
3514 /* Revise state->total_param and state->total_data in case they have
3515 changed downwards */
3516 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
3517 < state->total_param) {
3518 state->total_param = IVAL(req->inbuf,
3519 smb_nts_TotalParameterCount);
3521 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
3522 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
3525 size = smb_len(req->inbuf) + 4;
3527 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
3528 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
3529 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
3531 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
3532 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
3533 doff = IVAL(req->inbuf, smb_nts_DataOffset);
3535 state->received_param += pcnt;
3536 state->received_data += dcnt;
3538 if ((state->received_data > state->total_data) ||
3539 (state->received_param > state->total_param))
3543 if (pdisp+pcnt > state->total_param)
3545 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3547 if (pdisp > state->total_param)
3549 if ((smb_base(req->inbuf) + poff + pcnt
3550 > (char *)req->inbuf + size) ||
3551 (smb_base(req->inbuf) + poff + pcnt
3552 < smb_base(req->inbuf)))
3554 if (state->param + pdisp < state->param)
3557 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
3562 if (ddisp+dcnt > state->total_data)
3564 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3566 if (ddisp > state->total_data)
3568 if ((smb_base(req->inbuf) + doff + dcnt
3569 > (char *)req->inbuf + size) ||
3570 (smb_base(req->inbuf) + doff + dcnt
3571 < smb_base(req->inbuf)))
3573 if (state->data + ddisp < state->data)
3576 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
3580 if ((state->received_param < state->total_param) ||
3581 (state->received_data < state->total_data)) {
3582 END_PROFILE(SMBnttranss);
3587 * construct_reply_common will copy smb_com from inbuf to
3588 * outbuf. SMBnttranss is wrong here.
3590 SCVAL(req->inbuf,smb_com,SMBnttrans);
3592 handle_nttrans(conn, state, req);
3594 DLIST_REMOVE(conn->pending_trans, state);
3595 SAFE_FREE(state->data);
3596 SAFE_FREE(state->param);
3598 END_PROFILE(SMBnttranss);
3603 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3604 DLIST_REMOVE(conn->pending_trans, state);
3605 SAFE_FREE(state->data);
3606 SAFE_FREE(state->param);
3608 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3609 END_PROFILE(SMBnttranss);