2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-1998
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 int send_nt_replies(const char *inbuf,
79 int data_to_send = datasize;
80 int params_to_send = paramsize;
84 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
85 int alignment_offset = 3;
86 int data_alignment_offset = 0;
89 * Initially set the wcnt area to be 18 - this is true for all
93 set_message(inbuf,outbuf,18,0,True);
95 if (NT_STATUS_V(nt_error)) {
100 * If there genuinely are no parameters or data to send just send
104 if(params_to_send == 0 && data_to_send == 0) {
106 if (!send_smb(smbd_server_fd(),outbuf)) {
107 exit_server_cleanly("send_nt_replies: send_smb failed.");
113 * When sending params and data ensure that both are nicely aligned.
114 * Only do this alignment when there is also data to send - else
115 * can cause NT redirector problems.
118 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
119 data_alignment_offset = 4 - (params_to_send % 4);
123 * Space is bufsize minus Netbios over TCP header minus SMB header.
124 * The alignment_offset is to align the param bytes on a four byte
125 * boundary (2 bytes for data len, one byte pad).
126 * NT needs this to work correctly.
129 useable_space = bufsize - ((smb_buf(outbuf)+
130 alignment_offset+data_alignment_offset) -
134 * useable_space can never be more than max_send minus the
138 useable_space = MIN(useable_space,
139 max_send - (alignment_offset+data_alignment_offset));
142 while (params_to_send || data_to_send) {
145 * Calculate whether we will totally or partially fill this packet.
148 total_sent_thistime = params_to_send + data_to_send +
149 alignment_offset + data_alignment_offset;
152 * We can never send more than useable_space.
155 total_sent_thistime = MIN(total_sent_thistime, useable_space);
157 set_message(inbuf,outbuf, 18, total_sent_thistime, True);
160 * Set total params and data to be sent.
163 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
164 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
167 * Calculate how many parameters and data we can fit into
168 * this packet. Parameters get precedence.
171 params_sent_thistime = MIN(params_to_send,useable_space);
172 data_sent_thistime = useable_space - params_sent_thistime;
173 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
175 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
177 if(params_sent_thistime == 0) {
178 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
179 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
182 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
183 * parameter bytes, however the first 4 bytes of outbuf are
184 * the Netbios over TCP header. Thus use smb_base() to subtract
185 * them from the calculation.
188 SIVAL(outbuf,smb_ntr_ParameterOffset,
189 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
191 * Absolute displacement of param bytes sent in this packet.
194 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
198 * Deal with the data portion.
201 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
203 if(data_sent_thistime == 0) {
204 SIVAL(outbuf,smb_ntr_DataOffset,0);
205 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
208 * The offset of the data bytes is the offset of the
209 * parameter bytes plus the number of parameters being sent this time.
212 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
213 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
214 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
218 * Copy the param bytes into the packet.
221 if(params_sent_thistime) {
222 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
226 * Copy in the data bytes
229 if(data_sent_thistime) {
230 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
231 data_alignment_offset,pd,data_sent_thistime);
234 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
235 params_sent_thistime, data_sent_thistime, useable_space));
236 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
237 params_to_send, data_to_send, paramsize, datasize));
239 /* Send the packet */
241 if (!send_smb(smbd_server_fd(),outbuf)) {
242 exit_server_cleanly("send_nt_replies: send_smb failed.");
245 pp += params_sent_thistime;
246 pd += data_sent_thistime;
248 params_to_send -= params_sent_thistime;
249 data_to_send -= data_sent_thistime;
255 if(params_to_send < 0 || data_to_send < 0) {
256 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
257 params_to_send, data_to_send));
265 void send_nt_replies_new(struct smb_request *req, NTSTATUS nt_error,
266 char *params, int paramsize,
267 char *pdata, int datasize)
269 char *inbuf, *outbuf;
272 if (!reply_prep_legacy(req, &inbuf, &outbuf, &length, &bufsize)) {
273 reply_nterror(req, NT_STATUS_NO_MEMORY);
279 send_nt_replies(inbuf, outbuf, bufsize, nt_error,
280 params, paramsize, pdata, datasize));
283 /****************************************************************************
284 Is it an NTFS stream name ?
285 ****************************************************************************/
287 BOOL is_ntfs_stream_name(const char *fname)
289 if (lp_posix_pathnames()) {
292 return (strchr_m(fname, ':') != NULL) ? True : False;
295 struct case_semantics_state {
296 connection_struct *conn;
299 BOOL short_case_preserve;
302 /****************************************************************************
303 Restore case semantics.
304 ****************************************************************************/
305 static int restore_case_semantics(struct case_semantics_state *state)
307 state->conn->case_sensitive = state->case_sensitive;
308 state->conn->case_preserve = state->case_preserve;
309 state->conn->short_case_preserve = state->short_case_preserve;
313 /****************************************************************************
315 ****************************************************************************/
316 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
317 connection_struct *conn)
319 struct case_semantics_state *result;
321 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
322 DEBUG(0, ("talloc failed\n"));
327 result->case_sensitive = conn->case_sensitive;
328 result->case_preserve = conn->case_preserve;
329 result->short_case_preserve = conn->short_case_preserve;
332 conn->case_sensitive = True;
333 conn->case_preserve = True;
334 conn->short_case_preserve = True;
336 talloc_set_destructor(result, restore_case_semantics);
341 /****************************************************************************
342 Reply to an NT create and X call on a pipe
343 ****************************************************************************/
345 static void nt_open_pipe(char *fname, connection_struct *conn,
346 struct smb_request *req, int *ppnum)
348 smb_np_struct *p = NULL;
351 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
353 /* See if it is one we want to handle. */
355 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
356 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
361 for( i = 0; known_nt_pipes[i]; i++ ) {
362 if( strequal(fname,known_nt_pipes[i])) {
367 if ( known_nt_pipes[i] == NULL ) {
368 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
373 /* Strip \\ off the name. */
376 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
378 p = open_rpc_pipe_p(fname, conn, req->vuid);
380 reply_doserror(req, ERRSRV, ERRnofids);
384 /* TODO: Add pipe to db */
386 if ( !store_pipe_opendb( p ) ) {
387 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
394 /****************************************************************************
395 Reply to an NT create and X call for pipes.
396 ****************************************************************************/
398 static void do_ntcreate_pipe_open(connection_struct *conn,
399 struct smb_request *req)
404 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
406 srvstr_pull_buf((char *)req->inbuf, req->flags2, fname,
407 smb_buf(req->inbuf), sizeof(fname), STR_TERMINATE);
409 nt_open_pipe(fname, conn, req, &pnum);
417 * Deal with pipe return.
420 if (flags & EXTENDED_RESPONSE_REQUIRED) {
421 /* This is very strange. We
422 * return 50 words, but only set
423 * the wcnt to 42 ? It's definately
424 * what happens on the wire....
426 reply_outbuf(req, 50, 0);
427 SCVAL(req->outbuf,smb_wct,42);
429 reply_outbuf(req, 34, 0);
432 p = (char *)req->outbuf + smb_vwv2;
436 SIVAL(p,0,FILE_WAS_OPENED);
439 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
442 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
444 SSVAL(p,2, 0x5FF); /* ? */
447 if (flags & EXTENDED_RESPONSE_REQUIRED) {
449 SIVAL(p,0,FILE_GENERIC_ALL);
451 * For pipes W2K3 seems to return
453 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
455 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
458 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
460 chain_reply_new(req);
463 /****************************************************************************
464 Reply to an NT create and X call for a quota file.
465 ****************************************************************************/
467 static void reply_ntcreate_and_X_quota(connection_struct *conn,
468 struct smb_request *req,
469 enum FAKE_FILE_TYPE fake_file_type,
473 uint32 desired_access = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
477 status = open_fake_file(conn, fake_file_type, fname, desired_access,
480 if (!NT_STATUS_IS_OK(status)) {
481 reply_nterror(req, status);
485 reply_outbuf(req, 34, 0);
487 p = (char *)req->outbuf + smb_vwv2;
489 /* SCVAL(p,0,NO_OPLOCK_RETURN); */
491 SSVAL(p,0,fsp->fnum);
493 DEBUG(5,("reply_ntcreate_and_X_quota: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
495 chain_reply_new(req);
498 /****************************************************************************
499 Reply to an NT create and X call.
500 ****************************************************************************/
502 void reply_ntcreate_and_X(connection_struct *conn,
503 struct smb_request *req)
508 uint32 file_attributes;
510 uint32 create_disposition;
511 uint32 create_options;
513 SMB_BIG_UINT allocation_size;
514 /* Breakout the oplock request bits so we can set the
515 reply bits separately. */
516 int oplock_request = 0;
518 SMB_OFF_T file_len = 0;
519 SMB_STRUCT_STAT sbuf;
521 files_struct *fsp = NULL;
523 struct timespec c_timespec;
524 struct timespec a_timespec;
525 struct timespec m_timespec;
526 BOOL extended_oplock_granted = False;
528 struct case_semantics_state *case_state = NULL;
530 START_PROFILE(SMBntcreateX);
533 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
537 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
538 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
539 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
540 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
541 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
542 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
543 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
545 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,smb_ntcreate_AllocationSize);
546 #ifdef LARGE_SMB_OFF_T
547 allocation_size |= (((SMB_BIG_UINT)IVAL(req->inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
550 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
551 "file_attributes = 0x%x, share_access = 0x%x, "
552 "create_disposition = 0x%x create_options = 0x%x "
553 "root_dir_fid = 0x%x\n",
555 (unsigned int)access_mask,
556 (unsigned int)file_attributes,
557 (unsigned int)share_access,
558 (unsigned int)create_disposition,
559 (unsigned int)create_options,
560 (unsigned int)root_dir_fid ));
563 * If it's an IPC, use the pipe handler.
567 if (lp_nt_pipe_support()) {
568 do_ntcreate_pipe_open(conn, req);
569 END_PROFILE(SMBntcreateX);
572 reply_doserror(req, ERRDOS, ERRnoaccess);
573 END_PROFILE(SMBntcreateX);
578 if (create_options & FILE_OPEN_BY_FILE_ID) {
579 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
580 END_PROFILE(SMBntcreateX);
588 if(root_dir_fid != 0) {
590 * This filename is relative to a directory fid.
593 files_struct *dir_fsp = file_fsp(
594 SVAL(req->inbuf, smb_ntcreate_RootDirectoryFid));
598 reply_doserror(req, ERRDOS, ERRbadfid);
599 END_PROFILE(SMBntcreateX);
603 if(!dir_fsp->is_directory) {
605 srvstr_get_path((char *)req->inbuf, req->flags2, fname,
606 smb_buf(req->inbuf), sizeof(fname), 0,
607 STR_TERMINATE, &status);
608 if (!NT_STATUS_IS_OK(status)) {
609 reply_nterror(req, status);
610 END_PROFILE(SMBntcreateX);
615 * Check to see if this is a mac fork of some kind.
618 if( is_ntfs_stream_name(fname)) {
620 req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
621 END_PROFILE(SMBntcreateX);
626 we need to handle the case when we get a
627 relative open relative to a file and the
628 pathname is blank - this is a reopen!
629 (hint from demyn plantenberg)
632 reply_doserror(req, ERRDOS, ERRbadfid);
633 END_PROFILE(SMBntcreateX);
638 * Copy in the base directory name.
641 pstrcpy( fname, dir_fsp->fsp_name );
642 dir_name_len = strlen(fname);
645 * Ensure it ends in a '\'.
648 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
653 srvstr_get_path((char *)req->inbuf, req->flags2, rel_fname,
654 smb_buf(req->inbuf), sizeof(rel_fname), 0,
655 STR_TERMINATE, &status);
656 if (!NT_STATUS_IS_OK(status)) {
657 reply_nterror(req, status);
658 END_PROFILE(SMBntcreateX);
661 pstrcat(fname, rel_fname);
663 srvstr_get_path((char *)req->inbuf, req->flags2, fname,
664 smb_buf(req->inbuf), sizeof(fname), 0,
665 STR_TERMINATE, &status);
666 if (!NT_STATUS_IS_OK(status)) {
667 reply_nterror(req, status);
668 END_PROFILE(SMBntcreateX);
673 * Check to see if this is a mac fork of some kind.
676 if( is_ntfs_stream_name(fname)) {
677 enum FAKE_FILE_TYPE fake_file_type = is_fake_file(fname);
678 if (fake_file_type!=FAKE_FILE_TYPE_NONE) {
680 * Here we go! support for changing the disk quotas --metze
682 * We need to fake up to open this MAGIC QUOTA file
683 * and return a valid FID.
685 * w2k close this file directly after openening
686 * xp also tries a QUERY_FILE_INFO on the file and then close it
688 reply_ntcreate_and_X_quota(conn, req,
689 fake_file_type, fname);
691 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
693 END_PROFILE(SMBntcreateX);
699 * Now contruct the smb_open_mode value from the filename,
700 * desired access and the share access.
702 status = resolve_dfspath(conn, req->flags2 & FLAGS2_DFS_PATHNAMES, fname);
703 if (!NT_STATUS_IS_OK(status)) {
704 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
705 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
709 reply_nterror(req, status);
711 END_PROFILE(SMBntcreateX);
715 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
716 if (oplock_request) {
717 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
721 * Ordinary file or directory.
725 * Check if POSIX semantics are wanted.
728 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
729 case_state = set_posix_case_semantics(NULL, conn);
730 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
733 status = unix_convert(conn, fname, False, NULL, &sbuf);
734 if (!NT_STATUS_IS_OK(status)) {
735 TALLOC_FREE(case_state);
736 reply_nterror(req, status);
737 END_PROFILE(SMBntcreateX);
740 /* All file access must go through check_name() */
741 status = check_name(conn, fname);
742 if (!NT_STATUS_IS_OK(status)) {
743 TALLOC_FREE(case_state);
744 reply_nterror(req, status);
745 END_PROFILE(SMBntcreateX);
749 /* This is the correct thing to do (check every time) but can_delete is
750 expensive (it may have to read the parent directory permissions). So
751 for now we're not doing it unless we have a strong hint the client
752 is really going to delete this file. If the client is forcing FILE_CREATE
753 let the filesystem take care of the permissions. */
755 /* Setting FILE_SHARE_DELETE is the hint. */
757 if (lp_acl_check_permissions(SNUM(conn))
758 && (create_disposition != FILE_CREATE)
759 && (share_access & FILE_SHARE_DELETE)
760 && (access_mask & DELETE_ACCESS)) {
761 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
762 !can_delete_file_in_directory(conn, fname)) {
763 TALLOC_FREE(case_state);
764 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
765 END_PROFILE(SMBntcreateX);
771 /* We need to support SeSecurityPrivilege for this. */
772 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
773 !user_has_privileges(current_user.nt_user_token,
775 TALLOC_FREE(case_state);
776 END_PROFILE(SMBntcreateX);
777 return ERROR_NT(NT_STATUS_PRIVILEGE_NOT_HELD);
782 * If it's a request for a directory open, deal with it separately.
785 if(create_options & FILE_DIRECTORY_FILE) {
787 /* Can't open a temp directory. IFS kit test. */
788 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
789 TALLOC_FREE(case_state);
790 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
791 END_PROFILE(SMBntcreateX);
796 status = open_directory(conn, req, fname, &sbuf,
804 TALLOC_FREE(case_state);
806 if(!NT_STATUS_IS_OK(status)) {
807 if (!use_nt_status() && NT_STATUS_EQUAL(
808 status, NT_STATUS_OBJECT_NAME_COLLISION)) {
809 status = NT_STATUS_DOS(ERRDOS, ERRfilexists);
811 reply_nterror(req, status);
812 END_PROFILE(SMBntcreateX);
819 * Ordinary file case.
822 /* NB. We have a potential bug here. If we
823 * cause an oplock break to ourselves, then we
824 * could end up processing filename related
825 * SMB requests whilst we await the oplock
826 * break response. As we may have changed the
827 * filename case semantics to be POSIX-like,
828 * this could mean a filename request could
829 * fail when it should succeed. This is a rare
830 * condition, but eventually we must arrange
831 * to restore the correct case semantics
832 * before issuing an oplock break request to
833 * our client. JRA. */
835 status = open_file_ntcreate(conn, req, fname, &sbuf,
844 if (!NT_STATUS_IS_OK(status)) {
845 /* We cheat here. There are two cases we
846 * care about. One is a directory rename,
847 * where the NT client will attempt to
848 * open the source directory for
849 * DELETE access. Note that when the
850 * NT client does this it does *not*
851 * set the directory bit in the
852 * request packet. This is translated
853 * into a read/write open
854 * request. POSIX states that any open
855 * for write request on a directory
856 * will generate an EISDIR error, so
857 * we can catch this here and open a
858 * pseudo handle that is flagged as a
859 * directory. The second is an open
860 * for a permissions read only, which
861 * we handle in the open_file_stat case. JRA.
864 if (NT_STATUS_EQUAL(status,
865 NT_STATUS_FILE_IS_A_DIRECTORY)) {
868 * Fail the open if it was explicitly a non-directory file.
871 if (create_options & FILE_NON_DIRECTORY_FILE) {
872 TALLOC_FREE(case_state);
873 reply_force_nterror(req, NT_STATUS_FILE_IS_A_DIRECTORY);
874 END_PROFILE(SMBntcreateX);
879 status = open_directory(conn, req, fname,
888 if(!NT_STATUS_IS_OK(status)) {
889 TALLOC_FREE(case_state);
890 if (!use_nt_status() && NT_STATUS_EQUAL(
891 status, NT_STATUS_OBJECT_NAME_COLLISION)) {
892 status = NT_STATUS_DOS(ERRDOS, ERRfilexists);
894 reply_nterror(req, status);
895 END_PROFILE(SMBntcreateX);
899 TALLOC_FREE(case_state);
900 END_PROFILE(SMBntcreateX);
901 if (open_was_deferred(req->mid)) {
902 /* We have re-scheduled this call. */
905 reply_nterror(req, status);
911 TALLOC_FREE(case_state);
913 file_len = sbuf.st_size;
914 fattr = dos_mode(conn,fname,&sbuf);
916 fattr = FILE_ATTRIBUTE_NORMAL;
918 if (!fsp->is_directory && (fattr & aDIR)) {
919 close_file(fsp,ERROR_CLOSE);
920 reply_doserror(req, ERRDOS, ERRnoaccess);
921 END_PROFILE(SMBntcreateX);
925 /* Save the requested allocation size. */
926 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
927 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
928 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
929 if (fsp->is_directory) {
930 close_file(fsp,ERROR_CLOSE);
931 /* Can't set allocation size on a directory. */
932 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
933 END_PROFILE(SMBntcreateX);
936 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
937 close_file(fsp,ERROR_CLOSE);
938 reply_nterror(req, NT_STATUS_DISK_FULL);
939 END_PROFILE(SMBntcreateX);
943 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
948 * If the caller set the extended oplock request bit
949 * and we granted one (by whatever means) - set the
950 * correct bit for extended oplock reply.
953 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
954 extended_oplock_granted = True;
957 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
958 extended_oplock_granted = True;
961 if (flags & EXTENDED_RESPONSE_REQUIRED) {
962 /* This is very strange. We
963 * return 50 words, but only set
964 * the wcnt to 42 ? It's definately
965 * what happens on the wire....
967 reply_outbuf(req, 50, 0);
968 SCVAL(req->outbuf,smb_wct,42);
970 reply_outbuf(req, 34, 0);
973 p = (char *)req->outbuf + smb_vwv2;
976 * Currently as we don't support level II oplocks we just report
977 * exclusive & batch here.
980 if (extended_oplock_granted) {
981 if (flags & REQUEST_BATCH_OPLOCK) {
982 SCVAL(p,0, BATCH_OPLOCK_RETURN);
984 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
986 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
987 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
989 SCVAL(p,0,NO_OPLOCK_RETURN);
993 SSVAL(p,0,fsp->fnum);
995 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
996 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1003 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1004 a_timespec = get_atimespec(&sbuf);
1005 m_timespec = get_mtimespec(&sbuf);
1007 if (lp_dos_filetime_resolution(SNUM(conn))) {
1008 dos_filetime_timespec(&c_timespec);
1009 dos_filetime_timespec(&a_timespec);
1010 dos_filetime_timespec(&m_timespec);
1013 put_long_date_timespec(p, c_timespec); /* create time. */
1015 put_long_date_timespec(p, a_timespec); /* access time */
1017 put_long_date_timespec(p, m_timespec); /* write time */
1019 put_long_date_timespec(p, m_timespec); /* change time */
1021 SIVAL(p,0,fattr); /* File Attributes. */
1023 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1025 SOFF_T(p,0,file_len);
1027 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1031 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1033 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1036 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1037 perms = FILE_GENERIC_ALL;
1039 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1044 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
1046 chain_reply_new(req);
1047 END_PROFILE(SMBntcreateX);
1051 /****************************************************************************
1052 Reply to a NT_TRANSACT_CREATE call to open a pipe.
1053 ****************************************************************************/
1055 static void do_nt_transact_create_pipe(connection_struct *conn,
1056 struct smb_request *req,
1057 uint16 **ppsetup, uint32 setup_count,
1058 char **ppparams, uint32 parameter_count,
1059 char **ppdata, uint32 data_count)
1062 char *params = *ppparams;
1070 * Ensure minimum number of parameters sent.
1073 if(parameter_count < 54) {
1074 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1075 reply_doserror(req, ERRDOS, ERRnoaccess);
1079 flags = IVAL(params,0);
1081 srvstr_get_path(params, req->flags2, fname, params+53,
1082 sizeof(fname), parameter_count-53, STR_TERMINATE,
1084 if (!NT_STATUS_IS_OK(status)) {
1085 reply_nterror(req, status);
1089 nt_open_pipe(fname, conn, req, &pnum);
1096 /* Realloc the size of parameters and data we will return */
1097 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1098 /* Extended response is 32 more byyes. */
1103 params = nttrans_realloc(ppparams, param_len);
1104 if(params == NULL) {
1105 reply_doserror(req, ERRDOS, ERRnomem);
1110 SCVAL(p,0,NO_OPLOCK_RETURN);
1115 SIVAL(p,0,FILE_WAS_OPENED);
1119 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1122 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1124 SSVAL(p,2, 0x5FF); /* ? */
1127 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1129 SIVAL(p,0,FILE_GENERIC_ALL);
1131 * For pipes W2K3 seems to return
1133 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
1135 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
1138 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1140 /* Send the required number of replies */
1141 send_nt_replies_new(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1146 /****************************************************************************
1147 Internal fn to set security descriptors.
1148 ****************************************************************************/
1150 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1153 SEC_DESC *psd = NULL;
1154 TALLOC_CTX *mem_ctx;
1157 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
1158 return NT_STATUS_OK;
1162 * Init the parse struct we will unmarshall from.
1165 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1166 DEBUG(0,("set_sd: talloc_init failed.\n"));
1167 return NT_STATUS_NO_MEMORY;
1170 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1173 * Setup the prs_struct to point at the memory we just
1177 prs_give_memory( &pd, data, sd_len, False);
1180 * Finally, unmarshall from the data buffer.
1183 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1184 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1186 * Return access denied for want of a better error message..
1188 talloc_destroy(mem_ctx);
1189 return NT_STATUS_NO_MEMORY;
1192 if (psd->owner_sid==0) {
1193 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1195 if (psd->group_sid==0) {
1196 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1199 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1202 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1205 status = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1207 talloc_destroy(mem_ctx);
1211 /****************************************************************************
1212 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1213 ****************************************************************************/
1215 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1217 struct ea_list *ea_list_head = NULL;
1220 if (data_size < 4) {
1224 while (offset + 4 <= data_size) {
1225 size_t next_offset = IVAL(pdata,offset);
1226 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1232 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1233 if (next_offset == 0) {
1236 offset += next_offset;
1239 return ea_list_head;
1242 /****************************************************************************
1243 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1244 ****************************************************************************/
1246 static void call_nt_transact_create(connection_struct *conn,
1247 struct smb_request *req,
1248 uint16 **ppsetup, uint32 setup_count,
1249 char **ppparams, uint32 parameter_count,
1250 char **ppdata, uint32 data_count,
1251 uint32 max_data_count)
1254 char *params = *ppparams;
1255 char *data = *ppdata;
1256 /* Breakout the oplock request bits so we can set the reply bits separately. */
1257 int oplock_request = 0;
1259 SMB_OFF_T file_len = 0;
1260 SMB_STRUCT_STAT sbuf;
1262 files_struct *fsp = NULL;
1264 BOOL extended_oplock_granted = False;
1267 uint32 file_attributes;
1268 uint32 share_access;
1269 uint32 create_disposition;
1270 uint32 create_options;
1273 uint16 root_dir_fid;
1274 struct timespec c_timespec;
1275 struct timespec a_timespec;
1276 struct timespec m_timespec;
1277 struct ea_list *ea_list = NULL;
1278 TALLOC_CTX *ctx = NULL;
1282 struct case_semantics_state *case_state = NULL;
1284 DEBUG(5,("call_nt_transact_create\n"));
1287 * If it's an IPC, use the pipe handler.
1291 if (lp_nt_pipe_support()) {
1292 do_nt_transact_create_pipe(
1294 ppsetup, setup_count,
1295 ppparams, parameter_count,
1296 ppdata, data_count);
1299 reply_doserror(req, ERRDOS, ERRnoaccess);
1305 * Ensure minimum number of parameters sent.
1308 if(parameter_count < 54) {
1309 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1310 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1314 flags = IVAL(params,0);
1315 access_mask = IVAL(params,8);
1316 file_attributes = IVAL(params,20);
1317 share_access = IVAL(params,24);
1318 create_disposition = IVAL(params,28);
1319 create_options = IVAL(params,32);
1320 sd_len = IVAL(params,36);
1321 ea_len = IVAL(params,40);
1322 root_dir_fid = (uint16)IVAL(params,4);
1324 /* Ensure the data_len is correct for the sd and ea values given. */
1325 if ((ea_len + sd_len > data_count) ||
1326 (ea_len > data_count) || (sd_len > data_count) ||
1327 (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1328 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1329 (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1330 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1335 if (!lp_ea_support(SNUM(conn))) {
1336 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1337 (unsigned int)ea_len ));
1338 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1343 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1344 (unsigned int)ea_len ));
1345 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1350 if (create_options & FILE_OPEN_BY_FILE_ID) {
1351 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1356 * Get the file name.
1359 if(root_dir_fid != 0) {
1361 * This filename is relative to a directory fid.
1363 files_struct *dir_fsp = file_fsp(SVAL(params,4));
1364 size_t dir_name_len;
1367 reply_doserror(req, ERRDOS, ERRbadfid);
1371 if(!dir_fsp->is_directory) {
1372 srvstr_get_path(params, req->flags2, fname,
1373 params+53, sizeof(fname),
1374 parameter_count-53, STR_TERMINATE,
1376 if (!NT_STATUS_IS_OK(status)) {
1377 reply_nterror(req, status);
1382 * Check to see if this is a mac fork of some kind.
1385 if( is_ntfs_stream_name(fname)) {
1387 NT_STATUS_OBJECT_PATH_NOT_FOUND);
1391 reply_doserror(req, ERRDOS, ERRbadfid);
1396 * Copy in the base directory name.
1399 pstrcpy( fname, dir_fsp->fsp_name );
1400 dir_name_len = strlen(fname);
1403 * Ensure it ends in a '\'.
1406 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1407 pstrcat(fname, "/");
1413 srvstr_get_path(params, req->flags2, tmpname,
1414 params+53, sizeof(tmpname),
1415 parameter_count-53, STR_TERMINATE,
1417 if (!NT_STATUS_IS_OK(status)) {
1418 reply_nterror(req, status);
1421 pstrcat(fname, tmpname);
1424 srvstr_get_path(params, req->flags2, fname, params+53,
1425 sizeof(fname), parameter_count-53,
1426 STR_TERMINATE, &status);
1427 if (!NT_STATUS_IS_OK(status)) {
1428 reply_nterror(req, status);
1433 * Check to see if this is a mac fork of some kind.
1436 if( is_ntfs_stream_name(fname)) {
1437 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
1442 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1443 if (oplock_request) {
1444 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1448 * Ordinary file or directory.
1452 * Check if POSIX semantics are wanted.
1455 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1456 case_state = set_posix_case_semantics(NULL, conn);
1457 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1460 status = resolve_dfspath(conn, req->flags2 & FLAGS2_DFS_PATHNAMES,
1462 if (!NT_STATUS_IS_OK(status)) {
1463 TALLOC_FREE(case_state);
1464 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1465 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1466 ERRSRV, ERRbadpath);
1469 reply_nterror(req, status);
1473 status = unix_convert(conn, fname, False, NULL, &sbuf);
1474 if (!NT_STATUS_IS_OK(status)) {
1475 TALLOC_FREE(case_state);
1476 reply_nterror(req, status);
1479 /* All file access must go through check_name() */
1480 status = check_name(conn, fname);
1481 if (!NT_STATUS_IS_OK(status)) {
1482 TALLOC_FREE(case_state);
1483 reply_nterror(req, status);
1487 /* This is the correct thing to do (check every time) but can_delete is
1488 expensive (it may have to read the parent directory permissions). So
1489 for now we're not doing it unless we have a strong hint the client
1490 is really going to delete this file. If the client is forcing FILE_CREATE
1491 let the filesystem take care of the permissions. */
1493 /* Setting FILE_SHARE_DELETE is the hint. */
1495 if (lp_acl_check_permissions(SNUM(conn))
1496 && (create_disposition != FILE_CREATE)
1497 && (share_access & FILE_SHARE_DELETE)
1498 && (access_mask & DELETE_ACCESS)) {
1499 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
1500 !can_delete_file_in_directory(conn, fname)) {
1501 TALLOC_FREE(case_state);
1502 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1508 /* We need to support SeSecurityPrivilege for this. */
1509 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
1510 !user_has_privileges(current_user.nt_user_token,
1512 TALLOC_FREE(case_state);
1513 reply_nterror(req, NT_STATUS_PRIVILEGE_NOT_HELD);
1519 pdata = data + sd_len;
1521 /* We have already checked that ea_len <= data_count here. */
1522 ea_list = read_nttrans_ea_list(tmp_talloc_ctx(), pdata,
1525 TALLOC_FREE(case_state);
1526 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1532 * If it's a request for a directory open, deal with it separately.
1535 if(create_options & FILE_DIRECTORY_FILE) {
1537 /* Can't open a temp directory. IFS kit test. */
1538 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1539 TALLOC_FREE(case_state);
1540 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1545 * We will get a create directory here if the Win32
1546 * app specified a security descriptor in the
1547 * CreateDirectory() call.
1551 status = open_directory(conn, req, fname, &sbuf,
1558 if(!NT_STATUS_IS_OK(status)) {
1559 TALLOC_FREE(case_state);
1560 reply_nterror(req, status);
1567 * Ordinary file case.
1570 status = open_file_ntcreate(conn,req,fname,&sbuf,
1579 if (!NT_STATUS_IS_OK(status)) {
1580 if (NT_STATUS_EQUAL(status,
1581 NT_STATUS_FILE_IS_A_DIRECTORY)) {
1584 * Fail the open if it was explicitly a non-directory file.
1587 if (create_options & FILE_NON_DIRECTORY_FILE) {
1588 TALLOC_FREE(case_state);
1589 reply_force_nterror(
1591 NT_STATUS_FILE_IS_A_DIRECTORY);
1596 status = open_directory(conn, req, fname,
1604 if(!NT_STATUS_IS_OK(status)) {
1605 TALLOC_FREE(case_state);
1606 reply_nterror(req, status);
1610 TALLOC_FREE(case_state);
1611 if (open_was_deferred(req->mid)) {
1612 /* We have re-scheduled this call. */
1615 reply_nterror(req, status);
1622 * According to the MS documentation, the only time the security
1623 * descriptor is applied to the opened file is iff we *created* the
1624 * file; an existing file stays the same.
1626 * Also, it seems (from observation) that you can open the file with
1627 * any access mask but you can still write the sd. We need to override
1628 * the granted access before we call set_sd
1629 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1632 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1633 uint32 saved_access_mask = fsp->access_mask;
1635 /* We have already checked that sd_len <= data_count here. */
1637 fsp->access_mask = FILE_GENERIC_ALL;
1639 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1640 if (!NT_STATUS_IS_OK(status)) {
1641 talloc_destroy(ctx);
1642 close_file(fsp,ERROR_CLOSE);
1643 TALLOC_FREE(case_state);
1644 reply_nterror(req, status);
1647 fsp->access_mask = saved_access_mask;
1650 if (ea_len && (info == FILE_WAS_CREATED)) {
1651 status = set_ea(conn, fsp, fname, ea_list);
1652 if (!NT_STATUS_IS_OK(status)) {
1653 close_file(fsp,ERROR_CLOSE);
1654 TALLOC_FREE(case_state);
1655 reply_nterror(req, status);
1660 TALLOC_FREE(case_state);
1662 file_len = sbuf.st_size;
1663 fattr = dos_mode(conn,fname,&sbuf);
1665 fattr = FILE_ATTRIBUTE_NORMAL;
1667 if (!fsp->is_directory && (fattr & aDIR)) {
1668 close_file(fsp,ERROR_CLOSE);
1669 reply_doserror(req, ERRDOS, ERRnoaccess);
1673 /* Save the requested allocation size. */
1674 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1675 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1676 #ifdef LARGE_SMB_OFF_T
1677 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1679 if (allocation_size && (allocation_size > file_len)) {
1680 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1681 if (fsp->is_directory) {
1682 close_file(fsp,ERROR_CLOSE);
1683 /* Can't set allocation size on a directory. */
1684 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1687 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1688 close_file(fsp,ERROR_CLOSE);
1689 reply_nterror(req, NT_STATUS_DISK_FULL);
1693 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1698 * If the caller set the extended oplock request bit
1699 * and we granted one (by whatever means) - set the
1700 * correct bit for extended oplock reply.
1703 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1704 extended_oplock_granted = True;
1707 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1708 extended_oplock_granted = True;
1711 /* Realloc the size of parameters and data we will return */
1712 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1713 /* Extended response is 32 more byyes. */
1718 params = nttrans_realloc(ppparams, param_len);
1719 if(params == NULL) {
1720 reply_doserror(req, ERRDOS, ERRnomem);
1725 if (extended_oplock_granted) {
1726 if (flags & REQUEST_BATCH_OPLOCK) {
1727 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1729 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1731 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1732 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1734 SCVAL(p,0,NO_OPLOCK_RETURN);
1738 SSVAL(p,0,fsp->fnum);
1740 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1741 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1748 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1749 a_timespec = get_atimespec(&sbuf);
1750 m_timespec = get_mtimespec(&sbuf);
1752 if (lp_dos_filetime_resolution(SNUM(conn))) {
1753 dos_filetime_timespec(&c_timespec);
1754 dos_filetime_timespec(&a_timespec);
1755 dos_filetime_timespec(&m_timespec);
1758 put_long_date_timespec(p, c_timespec); /* create time. */
1760 put_long_date_timespec(p, a_timespec); /* access time */
1762 put_long_date_timespec(p, m_timespec); /* write time */
1764 put_long_date_timespec(p, m_timespec); /* change time */
1766 SIVAL(p,0,fattr); /* File Attributes. */
1768 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1770 SOFF_T(p,0,file_len);
1772 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1776 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1778 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1781 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1782 perms = FILE_GENERIC_ALL;
1784 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1789 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1791 /* Send the required number of replies */
1792 send_nt_replies_new(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1797 /****************************************************************************
1798 Reply to a NT CANCEL request.
1799 conn POINTER CAN BE NULL HERE !
1800 ****************************************************************************/
1802 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1805 * Go through and cancel any pending change notifies.
1808 START_PROFILE(SMBntcancel);
1809 remove_pending_change_notify_requests_by_mid(req->mid);
1810 remove_pending_lock_requests_by_mid(req->mid);
1811 srv_cancel_sign_response(req->mid);
1813 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1815 END_PROFILE(SMBntcancel);
1819 /****************************************************************************
1821 ****************************************************************************/
1823 static NTSTATUS copy_internals(connection_struct *conn,
1824 struct smb_request *req,
1825 char *oldname, char *newname, uint32 attrs)
1827 SMB_STRUCT_STAT sbuf1, sbuf2;
1828 pstring last_component_oldname;
1829 pstring last_component_newname;
1830 files_struct *fsp1,*fsp2;
1834 NTSTATUS status = NT_STATUS_OK;
1839 if (!CAN_WRITE(conn)) {
1840 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1843 status = unix_convert(conn, oldname, False, last_component_oldname, &sbuf1);
1844 if (!NT_STATUS_IS_OK(status)) {
1848 status = check_name(conn, oldname);
1849 if (!NT_STATUS_IS_OK(status)) {
1853 /* Source must already exist. */
1854 if (!VALID_STAT(sbuf1)) {
1855 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1857 /* Ensure attributes match. */
1858 fattr = dos_mode(conn,oldname,&sbuf1);
1859 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1860 return NT_STATUS_NO_SUCH_FILE;
1863 status = unix_convert(conn, newname, False, last_component_newname, &sbuf2);
1864 if (!NT_STATUS_IS_OK(status)) {
1868 status = check_name(conn, newname);
1869 if (!NT_STATUS_IS_OK(status)) {
1873 /* Disallow if newname already exists. */
1874 if (VALID_STAT(sbuf2)) {
1875 return NT_STATUS_OBJECT_NAME_COLLISION;
1878 /* No links from a directory. */
1879 if (S_ISDIR(sbuf1.st_mode)) {
1880 return NT_STATUS_FILE_IS_A_DIRECTORY;
1883 /* Ensure this is within the share. */
1884 status = check_reduced_name(conn, oldname);
1885 if (!NT_STATUS_IS_OK(status)) {
1889 DEBUG(10,("copy_internals: doing file copy %s to %s\n", oldname, newname));
1891 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1892 FILE_READ_DATA, /* Read-only. */
1893 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1895 0, /* No create options. */
1896 FILE_ATTRIBUTE_NORMAL,
1900 if (!NT_STATUS_IS_OK(status)) {
1904 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1905 FILE_WRITE_DATA, /* Read-only. */
1906 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1908 0, /* No create options. */
1913 if (!NT_STATUS_IS_OK(status)) {
1914 close_file(fsp1,ERROR_CLOSE);
1918 if (sbuf1.st_size) {
1919 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1923 * As we are opening fsp1 read-only we only expect
1924 * an error on close on fsp2 if we are out of space.
1925 * Thus we don't look at the error return from the
1928 close_file(fsp1,NORMAL_CLOSE);
1930 /* Ensure the modtime is set correctly on the destination file. */
1931 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1933 status = close_file(fsp2,NORMAL_CLOSE);
1935 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1936 creates the file. This isn't the correct thing to do in the copy
1938 file_set_dosmode(conn, newname, fattr, &sbuf2,
1939 parent_dirname(newname));
1941 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1942 return NT_STATUS_DISK_FULL;
1945 if (!NT_STATUS_IS_OK(status)) {
1946 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1947 nt_errstr(status), oldname, newname));
1952 /****************************************************************************
1953 Reply to a NT rename request.
1954 ****************************************************************************/
1956 int reply_ntrename(connection_struct *conn,
1957 char *inbuf,char *outbuf,int length,int bufsize)
1964 BOOL src_has_wcard = False;
1965 BOOL dest_has_wcard = False;
1966 uint32 attrs = SVAL(inbuf,smb_vwv0);
1967 uint16 rename_type = SVAL(inbuf,smb_vwv1);
1968 struct smb_request req;
1970 START_PROFILE(SMBntrename);
1972 init_smb_request(&req, (uint8 *)inbuf);
1974 p = smb_buf(inbuf) + 1;
1975 p += srvstr_get_path_wcard(inbuf, SVAL(inbuf,smb_flg2), oldname, p,
1976 sizeof(oldname), 0, STR_TERMINATE, &status,
1978 if (!NT_STATUS_IS_OK(status)) {
1979 END_PROFILE(SMBntrename);
1980 return ERROR_NT(status);
1983 if( is_ntfs_stream_name(oldname)) {
1984 /* Can't rename a stream. */
1985 END_PROFILE(SMBntrename);
1986 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1989 if (ms_has_wild(oldname)) {
1990 END_PROFILE(SMBntrename);
1991 return ERROR_NT(NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1995 p += srvstr_get_path_wcard(inbuf, SVAL(inbuf,smb_flg2), newname, p,
1996 sizeof(newname), 0, STR_TERMINATE, &status,
1998 if (!NT_STATUS_IS_OK(status)) {
1999 END_PROFILE(SMBntrename);
2000 return ERROR_NT(status);
2003 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, oldname);
2004 if (!NT_STATUS_IS_OK(status)) {
2005 END_PROFILE(SMBntrename);
2006 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2007 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
2009 return ERROR_NT(status);
2012 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, newname);
2013 if (!NT_STATUS_IS_OK(status)) {
2014 END_PROFILE(SMBntrename);
2015 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2016 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
2018 return ERROR_NT(status);
2021 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
2023 switch(rename_type) {
2024 case RENAME_FLAG_RENAME:
2025 status = rename_internals(conn, &req, oldname, newname,
2026 attrs, False, src_has_wcard,
2029 case RENAME_FLAG_HARD_LINK:
2030 if (src_has_wcard || dest_has_wcard) {
2032 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2034 status = hardlink_internals(conn, oldname, newname);
2037 case RENAME_FLAG_COPY:
2038 if (src_has_wcard || dest_has_wcard) {
2040 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2042 status = copy_internals(conn, &req, oldname,
2046 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
2047 status = NT_STATUS_INVALID_PARAMETER;
2050 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
2054 if (!NT_STATUS_IS_OK(status)) {
2055 END_PROFILE(SMBntrename);
2056 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
2057 /* We have re-scheduled this call. */
2060 return ERROR_NT(status);
2063 outsize = set_message(inbuf,outbuf,0,0,False);
2065 END_PROFILE(SMBntrename);
2069 /****************************************************************************
2070 Reply to a notify change - queue the request and
2071 don't allow a directory to be opened.
2072 ****************************************************************************/
2074 static void call_nt_transact_notify_change(connection_struct *conn,
2075 struct smb_request *req,
2079 uint32 parameter_count,
2080 char **ppdata, uint32 data_count,
2081 uint32 max_data_count,
2082 uint32 max_param_count)
2084 uint16 *setup = *ppsetup;
2090 if(setup_count < 6) {
2091 reply_doserror(req, ERRDOS, ERRbadfunc);
2095 fsp = file_fsp(SVAL(setup,4));
2096 filter = IVAL(setup, 0);
2097 recursive = (SVAL(setup, 6) != 0) ? True : False;
2099 DEBUG(3,("call_nt_transact_notify_change\n"));
2102 reply_doserror(req, ERRDOS, ERRbadfid);
2107 char *filter_string;
2109 if (!(filter_string = notify_filter_string(NULL, filter))) {
2110 reply_nterror(req,NT_STATUS_NO_MEMORY);
2114 DEBUG(3,("call_nt_transact_notify_change: notify change "
2115 "called on %s, filter = %s, recursive = %d\n",
2116 fsp->fsp_name, filter_string, recursive));
2118 TALLOC_FREE(filter_string);
2121 if((!fsp->is_directory) || (conn != fsp->conn)) {
2122 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2126 if (fsp->notify == NULL) {
2128 status = change_notify_create(fsp, filter, recursive);
2130 if (!NT_STATUS_IS_OK(status)) {
2131 DEBUG(10, ("change_notify_create returned %s\n",
2132 nt_errstr(status)));
2133 reply_nterror(req, status);
2138 if (fsp->notify->num_changes != 0) {
2141 * We've got changes pending, respond immediately
2145 * TODO: write a torture test to check the filtering behaviour
2149 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
2152 * change_notify_reply() above has independently sent its
2159 * No changes pending, queue the request
2162 status = change_notify_add_request(req->inbuf, max_param_count, filter,
2164 if (!NT_STATUS_IS_OK(status)) {
2165 reply_nterror(req, status);
2170 /****************************************************************************
2171 Reply to an NT transact rename command.
2172 ****************************************************************************/
2174 static void call_nt_transact_rename(connection_struct *conn,
2175 struct smb_request *req,
2176 uint16 **ppsetup, uint32 setup_count,
2177 char **ppparams, uint32 parameter_count,
2178 char **ppdata, uint32 data_count,
2179 uint32 max_data_count)
2181 char *params = *ppparams;
2183 files_struct *fsp = NULL;
2184 BOOL replace_if_exists = False;
2185 BOOL dest_has_wcard = False;
2188 if(parameter_count < 5) {
2189 reply_doserror(req, ERRDOS, ERRbadfunc);
2193 fsp = file_fsp(SVAL(params, 0));
2194 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2195 if (!check_fsp(conn, req, fsp, ¤t_user)) {
2198 srvstr_get_path_wcard(params, req->flags2, new_name, params+4,
2199 sizeof(new_name), parameter_count - 4,
2200 STR_TERMINATE, &status, &dest_has_wcard);
2201 if (!NT_STATUS_IS_OK(status)) {
2202 reply_nterror(req, status);
2206 status = rename_internals(conn, req, fsp->fsp_name,
2207 new_name, 0, replace_if_exists, False, dest_has_wcard);
2209 if (!NT_STATUS_IS_OK(status)) {
2210 if (open_was_deferred(req->mid)) {
2211 /* We have re-scheduled this call. */
2214 reply_nterror(req, status);
2219 * Rename was successful.
2221 send_nt_replies_new(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2223 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2224 fsp->fsp_name, new_name));
2229 /******************************************************************************
2230 Fake up a completely empty SD.
2231 *******************************************************************************/
2233 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2237 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2239 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2246 /****************************************************************************
2247 Reply to query a security descriptor.
2248 ****************************************************************************/
2250 static void call_nt_transact_query_security_desc(connection_struct *conn,
2251 struct smb_request *req,
2255 uint32 parameter_count,
2258 uint32 max_data_count)
2260 char *params = *ppparams;
2261 char *data = *ppdata;
2263 SEC_DESC *psd = NULL;
2265 uint32 security_info_wanted;
2266 TALLOC_CTX *mem_ctx;
2267 files_struct *fsp = NULL;
2269 if(parameter_count < 8) {
2270 reply_doserror(req, ERRDOS, ERRbadfunc);
2274 fsp = file_fsp(SVAL(params,0));
2276 reply_doserror(req, ERRDOS, ERRbadfid);
2280 security_info_wanted = IVAL(params,4);
2282 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2283 (unsigned int)security_info_wanted ));
2285 params = nttrans_realloc(ppparams, 4);
2286 if(params == NULL) {
2287 reply_doserror(req, ERRDOS, ERRnomem);
2291 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2292 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2293 reply_doserror(req, ERRDOS, ERRnomem);
2298 * Get the permissions to return.
2301 if (!lp_nt_acl_support(SNUM(conn))) {
2302 sd_size = get_null_nt_acl(mem_ctx, &psd);
2304 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
2308 talloc_destroy(mem_ctx);
2309 reply_unixerror(req, ERRDOS, ERRnoaccess);
2313 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2315 SIVAL(params,0,(uint32)sd_size);
2317 if(max_data_count < sd_size) {
2319 send_nt_replies_new(req, NT_STATUS_BUFFER_TOO_SMALL,
2320 params, 4, *ppdata, 0);
2321 talloc_destroy(mem_ctx);
2326 * Allocate the data we will point this at.
2329 data = nttrans_realloc(ppdata, sd_size);
2331 talloc_destroy(mem_ctx);
2332 reply_doserror(req, ERRDOS, ERRnomem);
2337 * Init the parse struct we will marshall into.
2340 prs_init(&pd, 0, mem_ctx, MARSHALL);
2343 * Setup the prs_struct to point at the memory we just
2347 prs_give_memory( &pd, data, (uint32)sd_size, False);
2350 * Finally, linearize into the outgoing buffer.
2353 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2354 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2355 security descriptor.\n"));
2357 * Return access denied for want of a better error message..
2359 talloc_destroy(mem_ctx);
2360 reply_unixerror(req, ERRDOS, ERRnoaccess);
2365 * Now we can delete the security descriptor.
2368 talloc_destroy(mem_ctx);
2370 send_nt_replies_new(req, NT_STATUS_OK, params, 4, data,
2375 /****************************************************************************
2376 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2377 ****************************************************************************/
2379 static void call_nt_transact_set_security_desc(connection_struct *conn,
2380 struct smb_request *req,
2384 uint32 parameter_count,
2387 uint32 max_data_count)
2389 char *params= *ppparams;
2390 char *data = *ppdata;
2391 files_struct *fsp = NULL;
2392 uint32 security_info_sent = 0;
2395 if(parameter_count < 8) {
2396 reply_doserror(req, ERRDOS, ERRbadfunc);
2400 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
2401 reply_doserror(req, ERRDOS, ERRbadfid);
2405 if(!lp_nt_acl_support(SNUM(conn))) {
2409 security_info_sent = IVAL(params,4);
2411 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2412 (unsigned int)security_info_sent ));
2414 if (data_count == 0) {
2415 reply_doserror(req, ERRDOS, ERRnoaccess);
2419 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2420 reply_nterror(req, nt_status);
2426 send_nt_replies_new(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2430 /****************************************************************************
2432 ****************************************************************************/
2434 static void call_nt_transact_ioctl(connection_struct *conn,
2435 struct smb_request *req,
2436 uint16 **ppsetup, uint32 setup_count,
2437 char **ppparams, uint32 parameter_count,
2438 char **ppdata, uint32 data_count,
2439 uint32 max_data_count)
2446 static BOOL logged_message;
2447 char *pdata = *ppdata;
2449 if (setup_count != 8) {
2450 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2451 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2455 function = IVAL(*ppsetup, 0);
2456 fidnum = SVAL(*ppsetup, 4);
2457 isFSctl = CVAL(*ppsetup, 6);
2458 compfilter = CVAL(*ppsetup, 7);
2460 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2461 function, fidnum, isFSctl, compfilter));
2463 fsp=file_fsp(fidnum);
2464 /* this check is done in each implemented function case for now
2465 because I don't want to break anything... --metze
2466 FSP_BELONGS_CONN(fsp,conn);*/
2469 case FSCTL_SET_SPARSE:
2470 /* pretend this succeeded - tho strictly we should
2471 mark the file sparse (if the local fs supports it)
2472 so we can know if we need to pre-allocate or not */
2474 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2475 send_nt_replies_new(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2478 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2480 unsigned char objid[16];
2482 /* This should return the object-id on this file.
2483 * I think I'll make this be the inode+dev. JRA.
2486 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2488 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2493 pdata = nttrans_realloc(ppdata, data_count);
2494 if (pdata == NULL) {
2495 reply_nterror(req, NT_STATUS_NO_MEMORY);
2498 push_file_id_16(pdata, &fsp->file_id);
2499 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2500 push_file_id_16(pdata+32, &fsp->file_id);
2501 send_nt_replies_new(req, NT_STATUS_OK, NULL, 0,
2506 case FSCTL_GET_REPARSE_POINT:
2507 /* pretend this fail - my winXP does it like this
2511 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2512 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2515 case FSCTL_SET_REPARSE_POINT:
2516 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2520 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2521 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2524 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2527 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2528 * and return their volume names. If max_data_count is 16, then it is just
2529 * asking for the number of volumes and length of the combined names.
2531 * pdata is the data allocated by our caller, but that uses
2532 * total_data_count (which is 0 in our case) rather than max_data_count.
2533 * Allocate the correct amount and return the pointer to let
2534 * it be deallocated when we return.
2536 SHADOW_COPY_DATA *shadow_data = NULL;
2537 TALLOC_CTX *shadow_mem_ctx = NULL;
2538 BOOL labels = False;
2539 uint32 labels_data_count = 0;
2543 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2547 if (max_data_count < 16) {
2548 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2550 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2554 if (max_data_count > 16) {
2558 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2559 if (shadow_mem_ctx == NULL) {
2560 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2561 reply_nterror(req, NT_STATUS_NO_MEMORY);
2565 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2566 if (shadow_data == NULL) {
2567 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2568 talloc_destroy(shadow_mem_ctx);
2569 reply_nterror(req, NT_STATUS_NO_MEMORY);
2573 shadow_data->mem_ctx = shadow_mem_ctx;
2576 * Call the VFS routine to actually do the work.
2578 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2579 talloc_destroy(shadow_data->mem_ctx);
2580 if (errno == ENOSYS) {
2581 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2582 conn->connectpath));
2583 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2586 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2587 conn->connectpath));
2588 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2593 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2598 data_count = 12+labels_data_count+4;
2601 if (max_data_count<data_count) {
2602 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2603 max_data_count,data_count));
2604 talloc_destroy(shadow_data->mem_ctx);
2605 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2609 pdata = nttrans_realloc(ppdata, data_count);
2610 if (pdata == NULL) {
2611 talloc_destroy(shadow_data->mem_ctx);
2612 reply_nterror(req, NT_STATUS_NO_MEMORY);
2618 /* num_volumes 4 bytes */
2619 SIVAL(pdata,0,shadow_data->num_volumes);
2622 /* num_labels 4 bytes */
2623 SIVAL(pdata,4,shadow_data->num_volumes);
2626 /* needed_data_count 4 bytes */
2627 SIVAL(pdata,8,labels_data_count);
2631 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2632 shadow_data->num_volumes,fsp->fsp_name));
2633 if (labels && shadow_data->labels) {
2634 for (i=0;i<shadow_data->num_volumes;i++) {
2635 srvstr_push(pdata, req->flags2,
2636 cur_pdata, shadow_data->labels[i],
2637 2*sizeof(SHADOW_COPY_LABEL),
2638 STR_UNICODE|STR_TERMINATE);
2639 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2640 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2644 talloc_destroy(shadow_data->mem_ctx);
2646 send_nt_replies_new(req, NT_STATUS_OK, NULL, 0,
2652 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2654 /* pretend this succeeded -
2656 * we have to send back a list with all files owned by this SID
2658 * but I have to check that --metze
2662 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2664 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2666 if (!fsp_belongs_conn(conn, req, fsp, ¤t_user)) {
2670 /* unknown 4 bytes: this is not the length of the sid :-( */
2671 /*unknown = IVAL(pdata,0);*/
2673 sid_parse(pdata+4,sid_len,&sid);
2674 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2676 if (!sid_to_uid(&sid, &uid)) {
2677 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2678 sid_string_static(&sid),(unsigned long)sid_len));
2682 /* we can take a look at the find source :-)
2684 * find ./ -uid $uid -name '*' is what we need here
2687 * and send 4bytes len and then NULL terminated unicode strings
2690 * but I don't know how to deal with the paged results
2691 * (maybe we can hang the result anywhere in the fsp struct)
2693 * we don't send all files at once
2694 * and at the next we should *not* start from the beginning,
2695 * so we have to cache the result
2700 /* this works for now... */
2701 send_nt_replies_new(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2705 if (!logged_message) {
2706 logged_message = True; /* Only print this once... */
2707 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2712 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2716 #ifdef HAVE_SYS_QUOTAS
2717 /****************************************************************************
2718 Reply to get user quota
2719 ****************************************************************************/
2721 static void call_nt_transact_get_user_quota(connection_struct *conn,
2722 struct smb_request *req,
2726 uint32 parameter_count,
2729 uint32 max_data_count)
2731 NTSTATUS nt_status = NT_STATUS_OK;
2732 char *params = *ppparams;
2733 char *pdata = *ppdata;
2735 int data_len=0,param_len=0;
2738 files_struct *fsp = NULL;
2742 BOOL start_enum = True;
2743 SMB_NTQUOTA_STRUCT qt;
2744 SMB_NTQUOTA_LIST *tmp_list;
2745 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2750 if (current_user.ut.uid != 0) {
2751 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2752 lp_servicename(SNUM(conn)),conn->user));
2753 reply_doserror(req, ERRDOS, ERRnoaccess);
2758 * Ensure minimum number of parameters sent.
2761 if (parameter_count < 4) {
2762 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2763 reply_doserror(req, ERRDOS, ERRinvalidparam);
2767 /* maybe we can check the quota_fnum */
2768 fsp = file_fsp(SVAL(params,0));
2769 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2770 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2771 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2775 /* the NULL pointer checking for fsp->fake_file_handle->pd
2776 * is done by CHECK_NTQUOTA_HANDLE_OK()
2778 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2780 level = SVAL(params,2);
2782 /* unknown 12 bytes leading in params */
2785 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2786 /* seems that we should continue with the enum here --metze */
2788 if (qt_handle->quota_list!=NULL &&
2789 qt_handle->tmp_list==NULL) {
2792 free_ntquota_list(&(qt_handle->quota_list));
2794 /* Realloc the size of parameters and data we will return */
2796 params = nttrans_realloc(ppparams, param_len);
2797 if(params == NULL) {
2798 reply_doserror(req, ERRDOS, ERRnomem);
2803 SIVAL(params,0,data_len);
2810 case TRANSACT_GET_USER_QUOTA_LIST_START:
2812 if (qt_handle->quota_list==NULL &&
2813 qt_handle->tmp_list==NULL) {
2817 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2818 reply_doserror(req, ERRSRV, ERRerror);
2822 /* Realloc the size of parameters and data we will return */
2824 params = nttrans_realloc(ppparams, param_len);
2825 if(params == NULL) {
2826 reply_doserror(req, ERRDOS, ERRnomem);
2830 /* we should not trust the value in max_data_count*/
2831 max_data_count = MIN(max_data_count,2048);
2833 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2835 reply_doserror(req, ERRDOS, ERRnomem);
2841 /* set params Size of returned Quota Data 4 bytes*/
2842 /* but set it later when we know it */
2844 /* for each entry push the data */
2847 qt_handle->tmp_list = qt_handle->quota_list;
2850 tmp_list = qt_handle->tmp_list;
2852 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2853 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2855 sid_len = sid_size(&tmp_list->quotas->sid);
2856 entry_len = 40 + sid_len;
2858 /* nextoffset entry 4 bytes */
2859 SIVAL(entry,0,entry_len);
2861 /* then the len of the SID 4 bytes */
2862 SIVAL(entry,4,sid_len);
2864 /* unknown data 8 bytes SMB_BIG_UINT */
2865 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2867 /* the used disk space 8 bytes SMB_BIG_UINT */
2868 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2870 /* the soft quotas 8 bytes SMB_BIG_UINT */
2871 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2873 /* the hard quotas 8 bytes SMB_BIG_UINT */
2874 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2876 /* and now the SID */
2877 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2880 qt_handle->tmp_list = tmp_list;
2882 /* overwrite the offset of the last entry */
2883 SIVAL(entry-entry_len,0,0);
2885 data_len = 4+qt_len;
2886 /* overwrite the params quota_data_len */
2887 SIVAL(params,0,data_len);
2891 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2893 /* unknown 4 bytes IVAL(pdata,0) */
2895 if (data_count < 8) {
2896 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2897 reply_doserror(req, ERRDOS, ERRunknownlevel);
2901 sid_len = IVAL(pdata,4);
2902 /* Ensure this is less than 1mb. */
2903 if (sid_len > (1024*1024)) {
2904 reply_doserror(req, ERRDOS, ERRnomem);
2908 if (data_count < 8+sid_len) {
2909 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2910 reply_doserror(req, ERRDOS, ERRunknownlevel);
2914 data_len = 4+40+sid_len;
2916 if (max_data_count < data_len) {
2917 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2918 max_data_count, data_len));
2920 SIVAL(params,0,data_len);
2922 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2926 sid_parse(pdata+8,sid_len,&sid);
2928 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2931 * we have to return zero's in all fields
2932 * instead of returning an error here
2937 /* Realloc the size of parameters and data we will return */
2939 params = nttrans_realloc(ppparams, param_len);
2940 if(params == NULL) {
2941 reply_doserror(req, ERRDOS, ERRnomem);
2945 pdata = nttrans_realloc(ppdata, data_len);
2947 reply_doserror(req, ERRDOS, ERRnomem);
2953 /* set params Size of returned Quota Data 4 bytes*/
2954 SIVAL(params,0,data_len);
2956 /* nextoffset entry 4 bytes */
2959 /* then the len of the SID 4 bytes */
2960 SIVAL(entry,4,sid_len);
2962 /* unknown data 8 bytes SMB_BIG_UINT */
2963 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2965 /* the used disk space 8 bytes SMB_BIG_UINT */
2966 SBIG_UINT(entry,16,qt.usedspace);
2968 /* the soft quotas 8 bytes SMB_BIG_UINT */
2969 SBIG_UINT(entry,24,qt.softlim);
2971 /* the hard quotas 8 bytes SMB_BIG_UINT */
2972 SBIG_UINT(entry,32,qt.hardlim);
2974 /* and now the SID */
2975 sid_linearize(entry+40, sid_len, &sid);
2980 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2981 reply_doserror(req, ERRSRV, ERRerror);
2986 send_nt_replies_new(req, nt_status, params, param_len,
2990 /****************************************************************************
2991 Reply to set user quota
2992 ****************************************************************************/
2994 static void call_nt_transact_set_user_quota(connection_struct *conn,
2995 struct smb_request *req,
2999 uint32 parameter_count,
3002 uint32 max_data_count)
3004 char *params = *ppparams;
3005 char *pdata = *ppdata;
3006 int data_len=0,param_len=0;
3007 SMB_NTQUOTA_STRUCT qt;
3010 files_struct *fsp = NULL;
3015 if (current_user.ut.uid != 0) {
3016 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
3017 lp_servicename(SNUM(conn)),conn->user));
3018 reply_doserror(req, ERRDOS, ERRnoaccess);
3023 * Ensure minimum number of parameters sent.
3026 if (parameter_count < 2) {
3027 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
3028 reply_doserror(req, ERRDOS, ERRinvalidparam);
3032 /* maybe we can check the quota_fnum */
3033 fsp = file_fsp(SVAL(params,0));
3034 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
3035 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
3036 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
3040 if (data_count < 40) {
3041 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
3042 reply_doserror(req, ERRDOS, ERRunknownlevel);
3046 /* offset to next quota record.
3047 * 4 bytes IVAL(pdata,0)
3052 sid_len = IVAL(pdata,4);
3054 if (data_count < 40+sid_len) {
3055 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
3056 reply_doserror(req, ERRDOS, ERRunknownlevel);
3060 /* unknown 8 bytes in pdata
3061 * maybe its the change time in NTTIME
3064 /* the used space 8 bytes (SMB_BIG_UINT)*/
3065 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
3066 #ifdef LARGE_SMB_OFF_T
3067 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
3068 #else /* LARGE_SMB_OFF_T */
3069 if ((IVAL(pdata,20) != 0)&&
3070 ((qt.usedspace != 0xFFFFFFFF)||
3071 (IVAL(pdata,20)!=0xFFFFFFFF))) {
3072 /* more than 32 bits? */
3073 reply_doserror(req, ERRDOS, ERRunknownlevel);
3076 #endif /* LARGE_SMB_OFF_T */
3078 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
3079 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
3080 #ifdef LARGE_SMB_OFF_T
3081 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
3082 #else /* LARGE_SMB_OFF_T */
3083 if ((IVAL(pdata,28) != 0)&&
3084 ((qt.softlim != 0xFFFFFFFF)||
3085 (IVAL(pdata,28)!=0xFFFFFFFF))) {
3086 /* more than 32 bits? */
3087 reply_doserror(req, ERRDOS, ERRunknownlevel);
3090 #endif /* LARGE_SMB_OFF_T */
3092 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
3093 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
3094 #ifdef LARGE_SMB_OFF_T
3095 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
3096 #else /* LARGE_SMB_OFF_T */
3097 if ((IVAL(pdata,36) != 0)&&
3098 ((qt.hardlim != 0xFFFFFFFF)||
3099 (IVAL(pdata,36)!=0xFFFFFFFF))) {
3100 /* more than 32 bits? */
3101 reply_doserror(req, ERRDOS, ERRunknownlevel);
3104 #endif /* LARGE_SMB_OFF_T */
3106 sid_parse(pdata+40,sid_len,&sid);
3107 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
3109 /* 44 unknown bytes left... */
3111 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
3112 reply_doserror(req, ERRSRV, ERRerror);
3116 send_nt_replies_new(req, NT_STATUS_OK, params, param_len,
3119 #endif /* HAVE_SYS_QUOTAS */
3121 static void handle_nttrans(connection_struct *conn,
3122 struct trans_state *state,
3123 struct smb_request *req)
3125 if (Protocol >= PROTOCOL_NT1) {
3126 req->flags2 |= 0x40; /* IS_LONG_NAME */
3127 SSVAL(req->inbuf,smb_flg2,req->flags2);
3130 /* Now we must call the relevant NT_TRANS function */
3131 switch(state->call) {
3132 case NT_TRANSACT_CREATE:
3134 START_PROFILE(NT_transact_create);
3135 call_nt_transact_create(
3137 &state->setup, state->setup_count,
3138 &state->param, state->total_param,
3139 &state->data, state->total_data,
3140 state->max_data_return);
3141 END_PROFILE(NT_transact_create);
3145 case NT_TRANSACT_IOCTL:
3147 START_PROFILE(NT_transact_ioctl);
3148 call_nt_transact_ioctl(
3150 &state->setup, state->setup_count,
3151 &state->param, state->total_param,
3152 &state->data, state->total_data,
3153 state->max_data_return);
3154 END_PROFILE(NT_transact_ioctl);
3158 case NT_TRANSACT_SET_SECURITY_DESC:
3160 START_PROFILE(NT_transact_set_security_desc);
3161 call_nt_transact_set_security_desc(
3163 &state->setup, state->setup_count,
3164 &state->param, state->total_param,
3165 &state->data, state->total_data,
3166 state->max_data_return);
3167 END_PROFILE(NT_transact_set_security_desc);
3171 case NT_TRANSACT_NOTIFY_CHANGE:
3173 START_PROFILE(NT_transact_notify_change);
3174 call_nt_transact_notify_change(
3176 &state->setup, state->setup_count,
3177 &state->param, state->total_param,
3178 &state->data, state->total_data,
3179 state->max_data_return,
3180 state->max_param_return);
3181 END_PROFILE(NT_transact_notify_change);
3185 case NT_TRANSACT_RENAME:
3187 START_PROFILE(NT_transact_rename);
3188 call_nt_transact_rename(
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_rename);
3198 case NT_TRANSACT_QUERY_SECURITY_DESC:
3200 START_PROFILE(NT_transact_query_security_desc);
3201 call_nt_transact_query_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_query_security_desc);
3211 #ifdef HAVE_SYS_QUOTAS
3212 case NT_TRANSACT_GET_USER_QUOTA:
3214 START_PROFILE(NT_transact_get_user_quota);
3215 call_nt_transact_get_user_quota(
3217 &state->setup, state->setup_count,
3218 &state->param, state->total_param,
3219 &state->data, state->total_data,
3220 state->max_data_return);
3221 END_PROFILE(NT_transact_get_user_quota);
3225 case NT_TRANSACT_SET_USER_QUOTA:
3227 START_PROFILE(NT_transact_set_user_quota);
3228 call_nt_transact_set_user_quota(
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_set_user_quota);
3237 #endif /* HAVE_SYS_QUOTAS */
3240 /* Error in request */
3241 DEBUG(0,("handle_nttrans: Unknown request %d in "
3242 "nttrans call\n", state->call));
3243 reply_doserror(req, ERRSRV, ERRerror);
3249 /****************************************************************************
3250 Reply to a SMBNTtrans.
3251 ****************************************************************************/
3253 void reply_nttrans(connection_struct *conn, struct smb_request *req)
3259 uint16 function_code;
3261 struct trans_state *state;
3264 START_PROFILE(SMBnttrans);
3266 if (req->wct < 19) {
3267 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3268 END_PROFILE(SMBnttrans);
3272 size = smb_len(req->inbuf) + 4;
3273 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
3274 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
3275 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
3276 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
3277 function_code = SVAL(req->inbuf, smb_nt_Function);
3279 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3280 reply_doserror(req, ERRSRV, ERRaccess);
3281 END_PROFILE(SMBnttrans);
3285 result = allow_new_trans(conn->pending_trans, req->mid);
3286 if (!NT_STATUS_IS_OK(result)) {
3287 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3288 reply_nterror(req, result);
3289 END_PROFILE(SMBnttrans);
3293 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3294 reply_doserror(req, ERRSRV, ERRaccess);
3295 END_PROFILE(SMBnttrans);
3299 state->cmd = SMBnttrans;
3301 state->mid = req->mid;
3302 state->vuid = req->vuid;
3303 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
3305 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
3306 state->param = NULL;
3307 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
3308 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
3310 /* setup count is in *words* */
3311 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
3312 state->setup = NULL;
3313 state->call = function_code;
3316 * All nttrans messages we handle have smb_wct == 19 +
3317 * state->setup_count. Ensure this is so as a sanity check.
3320 if(req->wct != 19 + (state->setup_count/2)) {
3321 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3322 req->wct, 19 + (state->setup_count/2)));
3326 /* Don't allow more than 128mb for each value. */
3327 if ((state->total_data > (1024*1024*128)) ||
3328 (state->total_param > (1024*1024*128))) {
3329 reply_doserror(req, ERRDOS, ERRnomem);
3330 END_PROFILE(SMBnttrans);
3334 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3337 if (state->total_data) {
3338 /* Can't use talloc here, the core routines do realloc on the
3339 * params and data. */
3340 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3341 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3342 "bytes !\n", (unsigned int)state->total_data));
3344 reply_doserror(req, ERRDOS, ERRnomem);
3345 END_PROFILE(SMBnttrans);
3348 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3350 if ((smb_base(req->inbuf)+dsoff+dscnt
3351 > (char *)req->inbuf + size) ||
3352 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
3355 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3358 if (state->total_param) {
3359 /* Can't use talloc here, the core routines do realloc on the
3360 * params and data. */
3361 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3362 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3363 "bytes !\n", (unsigned int)state->total_param));
3364 SAFE_FREE(state->data);
3366 reply_doserror(req, ERRDOS, ERRnomem);
3367 END_PROFILE(SMBnttrans);
3370 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3372 if ((smb_base(req->inbuf)+psoff+pscnt
3373 > (char *)req->inbuf + size) ||
3374 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
3377 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3380 state->received_data = dscnt;
3381 state->received_param = pscnt;
3383 if(state->setup_count > 0) {
3384 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3385 state->setup_count));
3386 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3387 if (state->setup == NULL) {
3388 DEBUG(0,("reply_nttrans : Out of memory\n"));
3389 SAFE_FREE(state->data);
3390 SAFE_FREE(state->param);
3392 reply_doserror(req, ERRDOS, ERRnomem);
3393 END_PROFILE(SMBnttrans);
3397 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3398 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3401 if (smb_nt_SetupStart + state->setup_count > size) {
3405 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
3406 state->setup_count);
3407 dump_data(10, (uint8 *)state->setup, state->setup_count);
3410 if ((state->received_data == state->total_data) &&
3411 (state->received_param == state->total_param)) {
3412 handle_nttrans(conn, state, req);
3413 SAFE_FREE(state->param);
3414 SAFE_FREE(state->data);
3416 END_PROFILE(SMBnttrans);
3420 DLIST_ADD(conn->pending_trans, state);
3422 /* We need to send an interim response then receive the rest
3423 of the parameter/data bytes */
3424 reply_outbuf(req, 0, 0);
3425 show_msg((char *)req->outbuf);
3426 END_PROFILE(SMBnttrans);
3431 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3432 SAFE_FREE(state->data);
3433 SAFE_FREE(state->param);
3435 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3436 END_PROFILE(SMBnttrans);
3440 /****************************************************************************
3441 Reply to a SMBnttranss
3442 ****************************************************************************/
3444 void reply_nttranss(connection_struct *conn, struct smb_request *req)
3446 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3447 struct trans_state *state;
3451 START_PROFILE(SMBnttranss);
3453 show_msg((char *)req->inbuf);
3455 if (req->wct < 18) {
3456 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3457 END_PROFILE(SMBnttranss);
3461 for (state = conn->pending_trans; state != NULL;
3462 state = state->next) {
3463 if (state->mid == req->mid) {
3468 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3469 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3470 END_PROFILE(SMBnttranss);
3474 /* Revise state->total_param and state->total_data in case they have
3475 changed downwards */
3476 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
3477 < state->total_param) {
3478 state->total_param = IVAL(req->inbuf,
3479 smb_nts_TotalParameterCount);
3481 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
3482 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
3485 size = smb_len(req->inbuf) + 4;
3487 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
3488 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
3489 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
3491 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
3492 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
3493 doff = IVAL(req->inbuf, smb_nts_DataOffset);
3495 state->received_param += pcnt;
3496 state->received_data += dcnt;
3498 if ((state->received_data > state->total_data) ||
3499 (state->received_param > state->total_param))
3503 if (pdisp+pcnt > state->total_param)
3505 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3507 if (pdisp > state->total_param)
3509 if ((smb_base(req->inbuf) + poff + pcnt
3510 > (char *)req->inbuf + size) ||
3511 (smb_base(req->inbuf) + poff + pcnt
3512 < smb_base(req->inbuf)))
3514 if (state->param + pdisp < state->param)
3517 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
3522 if (ddisp+dcnt > state->total_data)
3524 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3526 if (ddisp > state->total_data)
3528 if ((smb_base(req->inbuf) + doff + dcnt
3529 > (char *)req->inbuf + size) ||
3530 (smb_base(req->inbuf) + doff + dcnt
3531 < smb_base(req->inbuf)))
3533 if (state->data + ddisp < state->data)
3536 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
3540 if ((state->received_param < state->total_param) ||
3541 (state->received_data < state->total_data)) {
3542 END_PROFILE(SMBnttranss);
3547 * construct_reply_common will copy smb_com from inbuf to
3548 * outbuf. SMBnttranss is wrong here.
3550 SCVAL(req->inbuf,smb_com,SMBnttrans);
3552 handle_nttrans(conn, state, req);
3554 DLIST_REMOVE(conn->pending_trans, state);
3555 SAFE_FREE(state->data);
3556 SAFE_FREE(state->param);
3558 END_PROFILE(SMBnttranss);
3563 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3564 DLIST_REMOVE(conn->pending_trans, state);
3565 SAFE_FREE(state->data);
3566 SAFE_FREE(state->param);
3568 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3569 END_PROFILE(SMBnttranss);