2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 extern int smb_read_error;
25 extern int global_oplock_break;
26 extern BOOL case_sensitive;
27 extern BOOL case_preserve;
28 extern BOOL short_case_preserve;
30 static const char *known_nt_pipes[] = {
46 /* Map generic permissions to file object specific permissions */
48 struct generic_mapping file_generic_mapping = {
55 /****************************************************************************
56 Send the required number of replies back.
57 We assume all fields other than the data fields are
58 set correctly for the type of call.
59 HACK ! Always assumes smb_setup field is zero.
60 ****************************************************************************/
62 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, NTSTATUS nt_error, char *params,
63 int paramsize, char *pdata, int datasize)
66 int data_to_send = datasize;
67 int params_to_send = paramsize;
71 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
72 int alignment_offset = 3;
73 int data_alignment_offset = 0;
76 * Initially set the wcnt area to be 18 - this is true for all
80 set_message(outbuf,18,0,True);
82 if (NT_STATUS_V(nt_error))
86 * If there genuinely are no parameters or data to send just send
90 if(params_to_send == 0 && data_to_send == 0) {
91 if (!send_smb(smbd_server_fd(),outbuf))
92 exit_server("send_nt_replies: send_smb failed.");
97 * When sending params and data ensure that both are nicely aligned.
98 * Only do this alignment when there is also data to send - else
99 * can cause NT redirector problems.
102 if (((params_to_send % 4) != 0) && (data_to_send != 0))
103 data_alignment_offset = 4 - (params_to_send % 4);
106 * Space is bufsize minus Netbios over TCP header minus SMB header.
107 * The alignment_offset is to align the param bytes on a four byte
108 * boundary (2 bytes for data len, one byte pad).
109 * NT needs this to work correctly.
112 useable_space = bufsize - ((smb_buf(outbuf)+
113 alignment_offset+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 set_message(outbuf, 18, total_sent_thistime, True);
143 * Set total params and data to be sent.
146 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
147 SIVAL(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(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
160 if(params_sent_thistime == 0) {
161 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
162 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
165 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
166 * parameter bytes, however the first 4 bytes of outbuf are
167 * the Netbios over TCP header. Thus use smb_base() to subtract
168 * them from the calculation.
171 SIVAL(outbuf,smb_ntr_ParameterOffset,
172 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
174 * Absolute displacement of param bytes sent in this packet.
177 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
181 * Deal with the data portion.
184 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
186 if(data_sent_thistime == 0) {
187 SIVAL(outbuf,smb_ntr_DataOffset,0);
188 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
191 * The offset of the data bytes is the offset of the
192 * parameter bytes plus the number of parameters being sent this time.
195 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
196 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
197 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
201 * Copy the param bytes into the packet.
204 if(params_sent_thistime)
205 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
208 * Copy in the data bytes
211 if(data_sent_thistime)
212 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
213 data_alignment_offset,pd,data_sent_thistime);
215 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
216 params_sent_thistime, data_sent_thistime, useable_space));
217 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
218 params_to_send, data_to_send, paramsize, datasize));
220 /* Send the packet */
221 if (!send_smb(smbd_server_fd(),outbuf))
222 exit_server("send_nt_replies: send_smb failed.");
224 pp += params_sent_thistime;
225 pd += data_sent_thistime;
227 params_to_send -= params_sent_thistime;
228 data_to_send -= data_sent_thistime;
234 if(params_to_send < 0 || data_to_send < 0) {
235 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
236 params_to_send, data_to_send));
244 /****************************************************************************
246 ****************************************************************************/
248 static BOOL saved_case_sensitive;
249 static BOOL saved_case_preserve;
250 static BOOL saved_short_case_preserve;
252 /****************************************************************************
254 ****************************************************************************/
256 static void set_posix_case_semantics(uint32 file_attributes)
258 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
261 saved_case_sensitive = case_sensitive;
262 saved_case_preserve = case_preserve;
263 saved_short_case_preserve = short_case_preserve;
266 case_sensitive = True;
267 case_preserve = True;
268 short_case_preserve = True;
271 /****************************************************************************
272 Restore case semantics.
273 ****************************************************************************/
275 static void restore_case_semantics(uint32 file_attributes)
277 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
280 case_sensitive = saved_case_sensitive;
281 case_preserve = saved_case_preserve;
282 short_case_preserve = saved_short_case_preserve;
285 /****************************************************************************
286 Utility function to map create disposition.
287 ****************************************************************************/
289 static int map_create_disposition( uint32 create_disposition)
293 switch( create_disposition ) {
295 /* create if not exist, fail if exist */
296 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
299 case FILE_OVERWRITE_IF:
300 /* create if not exist, trunc if exist */
301 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
304 /* fail if not exist, open if exists */
305 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
308 /* create if not exist, open if exists */
309 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
312 /* fail if not exist, truncate if exists */
313 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
316 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
317 create_disposition ));
321 DEBUG(10,("map_create_disposition: Mapped create_disposition 0x%lx to 0x%x\n",
322 (unsigned long)create_disposition, ret ));
327 /****************************************************************************
328 Utility function to map share modes.
329 ****************************************************************************/
331 static int map_share_mode( char *fname, uint32 create_options,
332 uint32 *desired_access, uint32 share_access, uint32 file_attributes)
334 int smb_open_mode = -1;
337 * Convert GENERIC bits to specific bits.
340 se_map_generic(desired_access, &file_generic_mapping);
342 switch( *desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
344 smb_open_mode = DOS_OPEN_RDONLY;
346 case FILE_WRITE_DATA:
347 case FILE_APPEND_DATA:
348 case FILE_WRITE_DATA|FILE_APPEND_DATA:
349 smb_open_mode = DOS_OPEN_WRONLY;
351 case FILE_READ_DATA|FILE_WRITE_DATA:
352 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
353 case FILE_READ_DATA|FILE_APPEND_DATA:
354 smb_open_mode = DOS_OPEN_RDWR;
359 * NB. For DELETE_ACCESS we should really check the
360 * directory permissions, as that is what controls
361 * delete, and for WRITE_DAC_ACCESS we should really
362 * check the ownership, as that is what controls the
363 * chmod. Note that this is *NOT* a security hole (this
364 * note is for you, Andrew) as we are not *allowing*
365 * the access at this point, the actual unlink or
366 * chown or chmod call would do this. We are just helping
367 * clients out by telling them if they have a hope
368 * of any of this succeeding. POSIX acls may still
369 * deny the real call. JRA.
372 if (smb_open_mode == -1) {
374 if(*desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS|
375 FILE_EXECUTE|FILE_READ_ATTRIBUTES|
376 FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
377 FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
378 smb_open_mode = DOS_OPEN_RDONLY;
379 } else if(*desired_access == 0) {
382 * JRA - NT seems to sometimes send desired_access as zero. play it safe
383 * and map to a stat open.
386 smb_open_mode = DOS_OPEN_RDONLY;
389 DEBUG(0,("map_share_mode: Incorrect value 0x%lx for desired_access to file %s\n",
390 (unsigned long)*desired_access, fname));
396 * Set the special bit that means allow share delete.
397 * This is held outside the normal share mode bits at 1<<15.
401 if(share_access & FILE_SHARE_DELETE) {
402 smb_open_mode |= ALLOW_SHARE_DELETE;
403 DEBUG(10,("map_share_mode: FILE_SHARE_DELETE requested. open_mode = 0x%x\n", smb_open_mode));
407 * We need to store the intent to open for Delete. This
408 * is what determines if a delete on close flag can be set.
409 * This is the wrong way (and place) to store this, but for 2.2 this
410 * is the only practical way. JRA.
413 if(*desired_access & DELETE_ACCESS) {
414 DEBUG(10,("map_share_mode: DELETE_ACCESS requested. open_mode = 0x%x\n", smb_open_mode));
417 if (create_options & FILE_DELETE_ON_CLOSE) {
418 /* Implicit delete access is *NOT* requested... */
419 smb_open_mode |= DELETE_ON_CLOSE_FLAG;
420 DEBUG(10,("map_share_mode: FILE_DELETE_ON_CLOSE requested. open_mode = 0x%x\n", smb_open_mode));
423 /* Add in the requested share mode. */
424 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
425 case FILE_SHARE_READ:
426 smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
428 case FILE_SHARE_WRITE:
429 smb_open_mode |= SET_DENY_MODE(DENY_READ);
431 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
432 smb_open_mode |= SET_DENY_MODE(DENY_NONE);
434 case FILE_SHARE_NONE:
435 smb_open_mode |= SET_DENY_MODE(DENY_ALL);
440 * Handle an O_SYNC request.
443 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
444 smb_open_mode |= FILE_SYNC_OPENMODE;
446 DEBUG(10,("map_share_mode: Mapped desired access 0x%lx, share access 0x%lx, file attributes 0x%lx \
447 to open_mode 0x%x\n", (unsigned long)*desired_access, (unsigned long)share_access,
448 (unsigned long)file_attributes, smb_open_mode ));
450 return smb_open_mode;
453 /****************************************************************************
454 Reply to an NT create and X call on a pipe.
455 ****************************************************************************/
457 static int nt_open_pipe(char *fname, connection_struct *conn,
458 char *inbuf, char *outbuf, int *ppnum)
460 smb_np_struct *p = NULL;
462 uint16 vuid = SVAL(inbuf, smb_uid);
465 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
467 /* See if it is one we want to handle. */
469 if (lp_disable_spoolss() && strequal(fname, "\\spoolss"))
470 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
472 for( i = 0; known_nt_pipes[i]; i++ )
473 if( strequal(fname,known_nt_pipes[i]))
476 if ( known_nt_pipes[i] == NULL )
477 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
479 /* Strip \\ off the name. */
482 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
484 p = open_rpc_pipe_p(fname, conn, vuid);
486 return(ERROR_DOS(ERRSRV,ERRnofids));
493 /****************************************************************************
494 Reply to an NT create and X call for pipes.
495 ****************************************************************************/
497 static int do_ntcreate_pipe_open(connection_struct *conn,
498 char *inbuf,char *outbuf,int length,int bufsize)
505 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
507 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
511 * Deal with pipe return.
514 set_message(outbuf,34,0,True);
516 p = outbuf + smb_vwv2;
520 SIVAL(p,0,FILE_WAS_OPENED);
523 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
526 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
528 SSVAL(p,2, 0x5FF); /* ? */
530 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
532 return chain_reply(inbuf,outbuf,length,bufsize);
535 /****************************************************************************
536 Reply to an NT create and X call.
537 ****************************************************************************/
539 int reply_ntcreate_and_X(connection_struct *conn,
540 char *inbuf,char *outbuf,int length,int bufsize)
544 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
545 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
546 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
547 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
548 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
549 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
550 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
551 SMB_BIG_UINT allocation_size = 0;
554 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
555 /* Breakout the oplock request bits so we can set the
556 reply bits separately. */
557 int oplock_request = 0;
560 SMB_OFF_T file_len = 0;
561 SMB_STRUCT_STAT sbuf;
563 BOOL bad_path = False;
564 files_struct *fsp=NULL;
567 BOOL extended_oplock_granted = False;
569 START_PROFILE(SMBntcreateX);
571 DEBUG(10,("reply_ntcreateX: flags = 0x%x, desired_access = 0x%x \
572 file_attributes = 0x%x, share_access = 0x%x, create_disposition = 0x%x \
573 create_options = 0x%x root_dir_fid = 0x%x\n", flags, desired_access, file_attributes,
574 share_access, create_disposition,
575 create_options, root_dir_fid ));
577 /* If it's an IPC, use the pipe handler. */
580 if (lp_nt_pipe_support()) {
581 END_PROFILE(SMBntcreateX);
582 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
584 END_PROFILE(SMBntcreateX);
585 return(ERROR_DOS(ERRDOS,ERRnoaccess));
589 if (create_options & FILE_OPEN_BY_FILE_ID) {
590 END_PROFILE(SMBntcreateX);
591 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
595 * We need to construct the open_and_X ofun value from the
596 * NT values, as that's what our code is structured to accept.
599 if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
600 END_PROFILE(SMBntcreateX);
601 return(ERROR_DOS(ERRDOS,ERRnoaccess));
608 if(root_dir_fid != 0) {
610 * This filename is relative to a directory fid.
612 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
616 END_PROFILE(SMBntcreateX);
617 return(ERROR_DOS(ERRDOS,ERRbadfid));
620 if(!dir_fsp->is_directory) {
622 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
625 * Check to see if this is a mac fork of some kind.
628 if( strchr_m(fname, ':')) {
629 END_PROFILE(SMBntcreateX);
630 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
635 we need to handle the case when we get a
636 relative open relative to a file and the
637 pathname is blank - this is a reopen!
638 (hint from demyn plantenberg)
642 END_PROFILE(SMBntcreateX);
643 return(ERROR_DOS(ERRDOS,ERRbadfid));
647 * Copy in the base directory name.
650 pstrcpy( fname, dir_fsp->fsp_name );
651 dir_name_len = strlen(fname);
654 * Ensure it ends in a '\'.
657 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
658 pstrcat(fname, "\\");
662 srvstr_pull_buf(inbuf, &fname[dir_name_len], smb_buf(inbuf), sizeof(fname)-dir_name_len, STR_TERMINATE);
664 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
667 * Check to see if this is a mac fork of some kind.
670 if( strchr_m(fname, ':')) {
671 END_PROFILE(SMBntcreateX);
672 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
677 * Now contruct the smb_open_mode value from the filename,
678 * desired access and the share access.
680 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
682 if((smb_open_mode = map_share_mode(fname, create_options, &desired_access,
684 file_attributes)) == -1) {
685 END_PROFILE(SMBntcreateX);
686 return ERROR_DOS(ERRDOS,ERRnoaccess);
689 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
690 if (oplock_request) {
691 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
695 * Ordinary file or directory.
699 * Check if POSIX semantics are wanted.
702 set_posix_case_semantics(file_attributes);
704 unix_convert(fname,conn,0,&bad_path,&sbuf);
706 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
709 * If it's a request for a directory open, deal with it separately.
712 if(create_options & FILE_DIRECTORY_FILE) {
715 /* Can't open a temp directory. IFS kit test. */
716 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
717 END_PROFILE(SMBntcreateX);
718 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
721 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
723 restore_case_semantics(file_attributes);
726 set_bad_path_error(errno, bad_path);
727 END_PROFILE(SMBntcreateX);
728 return(UNIXERROR(ERRDOS,ERRnoaccess));
732 * Ordinary file case.
735 /* NB. We have a potential bug here. If we
736 * cause an oplock break to ourselves, then we
737 * could end up processing filename related
738 * SMB requests whilst we await the oplock
739 * break response. As we may have changed the
740 * filename case semantics to be POSIX-like,
741 * this could mean a filename request could
742 * fail when it should succeed. This is a rare
743 * condition, but eventually we must arrange
744 * to restore the correct case semantics
745 * before issuing an oplock break request to
746 * our client. JRA. */
748 fsp = open_file_shared1(conn,fname,&sbuf,
751 smb_ofun,unixmode, oplock_request,
755 /* We cheat here. There are two cases we
756 * care about. One is a directory rename,
757 * where the NT client will attempt to
758 * open the source directory for
759 * DELETE access. Note that when the
760 * NT client does this it does *not*
761 * set the directory bit in the
762 * request packet. This is translated
763 * into a read/write open
764 * request. POSIX states that any open
765 * for write request on a directory
766 * will generate an EISDIR error, so
767 * we can catch this here and open a
768 * pseudo handle that is flagged as a
769 * directory. The second is an open
770 * for a permissions read only, which
771 * we handle in the open_file_stat case. JRA.
774 if(errno == EISDIR) {
777 * Fail the open if it was explicitly a non-directory file.
780 if (create_options & FILE_NON_DIRECTORY_FILE) {
781 restore_case_semantics(file_attributes);
782 SSVAL(outbuf, smb_flg2,
783 SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
784 END_PROFILE(SMBntcreateX);
785 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
789 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
792 restore_case_semantics(file_attributes);
793 set_bad_path_error(errno, bad_path);
794 END_PROFILE(SMBntcreateX);
795 return(UNIXERROR(ERRDOS,ERRnoaccess));
799 restore_case_semantics(file_attributes);
800 set_bad_path_error(errno, bad_path);
801 END_PROFILE(SMBntcreateX);
802 return(UNIXERROR(ERRDOS,ERRnoaccess));
807 restore_case_semantics(file_attributes);
809 file_len = sbuf.st_size;
810 fmode = dos_mode(conn,fname,&sbuf);
812 fmode = FILE_ATTRIBUTE_NORMAL;
813 if (!fsp->is_directory && (fmode & aDIR)) {
814 close_file(fsp,False);
815 END_PROFILE(SMBntcreateX);
816 return ERROR_DOS(ERRDOS,ERRnoaccess);
819 /* Save the requested allocation size. */
820 allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize);
821 #ifdef LARGE_SMB_OFF_T
822 allocation_size |= (((SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
824 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
825 fsp->initial_allocation_size = SMB_ROUNDUP(allocation_size,SMB_ROUNDUP_ALLOCATION_SIZE);
826 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
827 close_file(fsp,False);
828 END_PROFILE(SMBntcreateX);
829 return ERROR_NT(NT_STATUS_DISK_FULL);
832 fsp->initial_allocation_size = SMB_ROUNDUP(((SMB_BIG_UINT)file_len),SMB_ROUNDUP_ALLOCATION_SIZE);
836 * If the caller set the extended oplock request bit
837 * and we granted one (by whatever means) - set the
838 * correct bit for extended oplock reply.
841 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
842 extended_oplock_granted = True;
844 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
845 extended_oplock_granted = True;
848 /* W2K sends back 42 words here ! If we do the same it breaks offline sync. Go figure... ? JRA. */
849 set_message(outbuf,42,0,True);
851 set_message(outbuf,34,0,True);
854 p = outbuf + smb_vwv2;
857 * Currently as we don't support level II oplocks we just report
858 * exclusive & batch here.
861 if (extended_oplock_granted) {
862 if (flags & REQUEST_BATCH_OPLOCK) {
863 SCVAL(p,0, BATCH_OPLOCK_RETURN);
865 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
867 } else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
868 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
870 SCVAL(p,0,NO_OPLOCK_RETURN);
874 SSVAL(p,0,fsp->fnum);
876 if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
877 SIVAL(p,0,FILE_WAS_SUPERSEDED);
879 SIVAL(p,0,smb_action);
883 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
885 if (lp_dos_filetime_resolution(SNUM(conn))) {
892 put_long_date(p,c_time);
894 put_long_date(p,sbuf.st_atime); /* access time */
896 put_long_date(p,sbuf.st_mtime); /* write time */
898 put_long_date(p,sbuf.st_mtime); /* change time */
900 SIVAL(p,0,fmode); /* File Attributes. */
902 SOFF_T(p, 0, get_allocation_size(fsp,&sbuf));
904 SOFF_T(p,0,file_len);
906 SCVAL(p,0,fsp->is_directory ? 1 : 0);
908 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
910 result = chain_reply(inbuf,outbuf,length,bufsize);
911 END_PROFILE(SMBntcreateX);
915 /****************************************************************************
916 Reply to a NT_TRANSACT_CREATE call to open a pipe.
917 ****************************************************************************/
919 static int do_nt_transact_create_pipe( connection_struct *conn,
920 char *inbuf, char *outbuf, int length,
921 int bufsize, char **ppsetup, char **ppparams,
925 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
926 char *params = *ppparams;
932 * Ensure minimum number of parameters sent.
935 if(total_parameter_count < 54) {
936 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
937 return ERROR_DOS(ERRDOS,ERRnoaccess);
940 srvstr_pull(inbuf, fname, params+53, sizeof(fname), total_parameter_count-53, STR_TERMINATE);
942 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
945 /* Realloc the size of parameters and data we will return */
946 params = Realloc(*ppparams, 69);
948 return ERROR_DOS(ERRDOS,ERRnomem);
952 memset((char *)params,'\0',69);
955 SCVAL(p,0,NO_OPLOCK_RETURN);
960 SIVAL(p,0,FILE_WAS_OPENED);
964 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
967 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
969 SSVAL(p,2, 0x5FF); /* ? */
971 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
973 /* Send the required number of replies */
974 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
979 /****************************************************************************
980 Internal fn to set security descriptors.
981 ****************************************************************************/
983 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
986 SEC_DESC *psd = NULL;
995 * Init the parse struct we will unmarshall from.
998 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
999 DEBUG(0,("set_sd: talloc_init failed.\n"));
1000 return NT_STATUS_NO_MEMORY;
1003 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1006 * Setup the prs_struct to point at the memory we just
1010 prs_give_memory( &pd, data, sd_len, False);
1013 * Finally, unmarshall from the data buffer.
1016 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1017 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1019 * Return access denied for want of a better error message..
1021 talloc_destroy(mem_ctx);
1022 return NT_STATUS_NO_MEMORY;
1025 if (psd->off_owner_sid==0)
1026 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1027 if (psd->off_grp_sid==0)
1028 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1029 if (psd->off_sacl==0)
1030 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1031 if (psd->off_dacl==0)
1032 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1034 ret = fsp->conn->vfs_ops.fset_nt_acl( fsp, fsp->fd, security_info_sent, psd);
1037 talloc_destroy(mem_ctx);
1038 return NT_STATUS_ACCESS_DENIED;
1041 talloc_destroy(mem_ctx);
1043 return NT_STATUS_OK;
1046 /****************************************************************************
1047 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1048 ****************************************************************************/
1050 static int call_nt_transact_create(connection_struct *conn,
1051 char *inbuf, char *outbuf, int length,
1052 int bufsize, char **ppsetup, char **ppparams,
1056 char *params = *ppparams;
1057 char *data = *ppdata;
1058 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1059 /* Breakout the oplock request bits so we can set the reply bits separately. */
1060 int oplock_request = 0;
1062 int fmode=0,rmode=0;
1063 SMB_OFF_T file_len = 0;
1064 SMB_STRUCT_STAT sbuf;
1066 BOOL bad_path = False;
1067 files_struct *fsp = NULL;
1069 BOOL extended_oplock_granted = False;
1071 uint32 desired_access;
1072 uint32 file_attributes;
1073 uint32 share_access;
1074 uint32 create_disposition;
1075 uint32 create_options;
1077 uint16 root_dir_fid;
1078 SMB_BIG_UINT allocation_size = 0;
1085 DEBUG(5,("call_nt_transact_create\n"));
1088 * If it's an IPC, use the pipe handler.
1092 if (lp_nt_pipe_support())
1093 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1094 bufsize, ppsetup, ppparams, ppdata);
1096 return ERROR_DOS(ERRDOS,ERRnoaccess);
1100 * Ensure minimum number of parameters sent.
1103 if(total_parameter_count < 54) {
1104 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1105 return ERROR_DOS(ERRDOS,ERRnoaccess);
1108 flags = IVAL(params,0);
1109 desired_access = IVAL(params,8);
1110 file_attributes = IVAL(params,20);
1111 share_access = IVAL(params,24);
1112 create_disposition = IVAL(params,28);
1113 create_options = IVAL(params,32);
1114 sd_len = IVAL(params,36);
1115 root_dir_fid = (uint16)IVAL(params,4);
1116 smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1118 if (create_options & FILE_OPEN_BY_FILE_ID) {
1119 END_PROFILE(SMBntcreateX);
1120 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1124 * We need to construct the open_and_X ofun value from the
1125 * NT values, as that's what our code is structured to accept.
1128 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1129 return ERROR_DOS(ERRDOS,ERRbadmem);
1132 * Get the file name.
1135 if(root_dir_fid != 0) {
1137 * This filename is relative to a directory fid.
1140 files_struct *dir_fsp = file_fsp(params,4);
1141 size_t dir_name_len;
1144 return ERROR_DOS(ERRDOS,ERRbadfid);
1146 if(!dir_fsp->is_directory) {
1148 srvstr_pull(inbuf, fname, params+53, sizeof(fname), total_parameter_count-53, STR_TERMINATE);
1151 * Check to see if this is a mac fork of some kind.
1154 if( strchr_m(fname, ':'))
1155 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1157 return ERROR_DOS(ERRDOS,ERRbadfid);
1161 * Copy in the base directory name.
1164 pstrcpy( fname, dir_fsp->fsp_name );
1165 dir_name_len = strlen(fname);
1168 * Ensure it ends in a '\'.
1171 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1172 pstrcat(fname, "\\");
1176 srvstr_pull(inbuf, &fname[dir_name_len], params+53, sizeof(fname)-dir_name_len,
1177 total_parameter_count-53, STR_TERMINATE);
1179 srvstr_pull(inbuf, fname, params+53, sizeof(fname), total_parameter_count-53, STR_TERMINATE);
1182 * Check to see if this is a mac fork of some kind.
1185 if( strchr_m(fname, ':'))
1186 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1190 * Now contruct the smb_open_mode value from the desired access
1191 * and the share access.
1194 if((smb_open_mode = map_share_mode( fname, create_options, &desired_access,
1195 share_access, file_attributes)) == -1)
1196 return ERROR_DOS(ERRDOS,ERRnoaccess);
1198 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1199 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1202 * Check if POSIX semantics are wanted.
1205 set_posix_case_semantics(file_attributes);
1207 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1209 unix_convert(fname,conn,0,&bad_path,&sbuf);
1211 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1214 * If it's a request for a directory open, deal with it separately.
1217 if(create_options & FILE_DIRECTORY_FILE) {
1219 /* Can't open a temp directory. IFS kit test. */
1220 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1221 END_PROFILE(SMBntcreateX);
1222 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1228 * We will get a create directory here if the Win32
1229 * app specified a security descriptor in the
1230 * CreateDirectory() call.
1233 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1236 restore_case_semantics(file_attributes);
1237 set_bad_path_error(errno, bad_path);
1238 return(UNIXERROR(ERRDOS,ERRnoaccess));
1244 * Ordinary file case.
1247 fsp = open_file_shared1(conn,fname,&sbuf,desired_access,
1248 smb_open_mode,smb_ofun,unixmode,
1249 oplock_request,&rmode,&smb_action);
1253 if(errno == EISDIR) {
1256 * Fail the open if it was explicitly a non-directory file.
1259 if (create_options & FILE_NON_DIRECTORY_FILE) {
1260 restore_case_semantics(file_attributes);
1261 SSVAL(outbuf, smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1262 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1266 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1269 restore_case_semantics(file_attributes);
1270 set_bad_path_error(errno, bad_path);
1271 return(UNIXERROR(ERRDOS,ERRnoaccess));
1274 restore_case_semantics(file_attributes);
1275 set_bad_path_error(errno, bad_path);
1276 return(UNIXERROR(ERRDOS,ERRnoaccess));
1280 file_len = sbuf.st_size;
1281 fmode = dos_mode(conn,fname,&sbuf);
1283 fmode = FILE_ATTRIBUTE_NORMAL;
1286 close_file(fsp,False);
1287 restore_case_semantics(file_attributes);
1288 return ERROR_DOS(ERRDOS,ERRnoaccess);
1292 * If the caller set the extended oplock request bit
1293 * and we granted one (by whatever means) - set the
1294 * correct bit for extended oplock reply.
1297 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1298 extended_oplock_granted = True;
1300 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1301 extended_oplock_granted = True;
1305 * Now try and apply the desired SD.
1308 if (sd_len && !NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION))) {
1309 close_file(fsp,False);
1310 restore_case_semantics(file_attributes);
1311 return ERROR_NT(nt_status);
1314 restore_case_semantics(file_attributes);
1316 /* Save the requested allocation size. */
1317 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1318 #ifdef LARGE_SMB_OFF_T
1319 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1321 if (allocation_size && (allocation_size > file_len)) {
1322 fsp->initial_allocation_size = SMB_ROUNDUP(allocation_size,SMB_ROUNDUP_ALLOCATION_SIZE);
1323 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1324 close_file(fsp,False);
1325 END_PROFILE(SMBntcreateX);
1326 return ERROR_NT(NT_STATUS_DISK_FULL);
1329 fsp->initial_allocation_size = SMB_ROUNDUP(((SMB_BIG_UINT)file_len),SMB_ROUNDUP_ALLOCATION_SIZE);
1332 /* Realloc the size of parameters and data we will return */
1333 params = Realloc(*ppparams, 69);
1335 return ERROR_DOS(ERRDOS,ERRnomem);
1339 memset((char *)params,'\0',69);
1342 if (extended_oplock_granted)
1343 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1344 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1345 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1347 SCVAL(p,0,NO_OPLOCK_RETURN);
1350 SSVAL(p,0,fsp->fnum);
1352 if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
1353 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1355 SIVAL(p,0,smb_action);
1359 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1361 if (lp_dos_filetime_resolution(SNUM(conn))) {
1363 sbuf.st_atime &= ~1;
1364 sbuf.st_mtime &= ~1;
1365 sbuf.st_mtime &= ~1;
1368 put_long_date(p,c_time);
1370 put_long_date(p,sbuf.st_atime); /* access time */
1372 put_long_date(p,sbuf.st_mtime); /* write time */
1374 put_long_date(p,sbuf.st_mtime); /* change time */
1376 SIVAL(p,0,fmode); /* File Attributes. */
1378 SOFF_T(p, 0, get_allocation_size(fsp,&sbuf));
1380 SOFF_T(p,0,file_len);
1382 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1384 /* Send the required number of replies */
1385 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1390 /****************************************************************************
1391 Reply to a NT CANCEL request.
1392 ****************************************************************************/
1394 int reply_ntcancel(connection_struct *conn,
1395 char *inbuf,char *outbuf,int length,int bufsize)
1398 * Go through and cancel any pending change notifies.
1401 int mid = SVAL(inbuf,smb_mid);
1402 START_PROFILE(SMBntcancel);
1403 remove_pending_change_notify_requests_by_mid(mid);
1404 remove_pending_lock_requests_by_mid(mid);
1406 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1408 END_PROFILE(SMBntcancel);
1412 /****************************************************************************
1413 Reply to an unsolicited SMBNTtranss - just ignore it!
1414 ****************************************************************************/
1416 int reply_nttranss(connection_struct *conn,
1417 char *inbuf,char *outbuf,int length,int bufsize)
1419 START_PROFILE(SMBnttranss);
1420 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1421 END_PROFILE(SMBnttranss);
1425 /****************************************************************************
1426 Reply to a notify change - queue the request and
1427 don't allow a directory to be opened.
1428 ****************************************************************************/
1430 static int call_nt_transact_notify_change(connection_struct *conn,
1431 char *inbuf, char *outbuf, int length,
1434 char **ppparams, char **ppdata)
1436 char *setup = *ppsetup;
1440 fsp = file_fsp(setup,4);
1441 flags = IVAL(setup, 0);
1443 DEBUG(3,("call_nt_transact_notify_change\n"));
1446 return ERROR_DOS(ERRDOS,ERRbadfid);
1448 if((!fsp->is_directory) || (conn != fsp->conn))
1449 return ERROR_DOS(ERRDOS,ERRbadfid);
1451 if (!change_notify_set(inbuf, fsp, conn, flags))
1452 return(UNIXERROR(ERRDOS,ERRbadfid));
1454 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1455 name = %s\n", fsp->fsp_name ));
1460 /****************************************************************************
1461 Reply to an NT transact rename command.
1462 ****************************************************************************/
1464 static int call_nt_transact_rename(connection_struct *conn,
1465 char *inbuf, char *outbuf, int length,
1467 char **ppsetup, char **ppparams, char **ppdata)
1469 char *params = *ppparams;
1471 files_struct *fsp = file_fsp(params, 0);
1472 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1475 CHECK_FSP(fsp, conn);
1476 srvstr_pull(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE);
1478 status = rename_internals(conn, fsp->fsp_name,
1479 new_name, replace_if_exists);
1480 if (!NT_STATUS_IS_OK(status))
1481 return ERROR_NT(status);
1484 * Rename was successful.
1486 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1488 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1489 fsp->fsp_name, new_name));
1492 * Win2k needs a changenotify request response before it will
1493 * update after a rename..
1496 process_pending_change_notify_queue((time_t)0);
1501 /******************************************************************************
1502 Fake up a completely empty SD.
1503 *******************************************************************************/
1505 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1507 extern DOM_SID global_sid_World;
1510 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1512 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1519 /****************************************************************************
1520 Reply to query a security descriptor - currently this is not implemented (it
1521 is planned to be though). Right now it just returns the same thing NT would
1522 when queried on a FAT filesystem. JRA.
1523 ****************************************************************************/
1525 static int call_nt_transact_query_security_desc(connection_struct *conn,
1526 char *inbuf, char *outbuf,
1527 int length, int bufsize,
1528 char **ppsetup, char **ppparams, char **ppdata)
1530 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1531 char *params = *ppparams;
1532 char *data = *ppdata;
1534 SEC_DESC *psd = NULL;
1536 TALLOC_CTX *mem_ctx;
1538 files_struct *fsp = file_fsp(params,0);
1541 return ERROR_DOS(ERRDOS,ERRbadfid);
1543 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1545 params = Realloc(*ppparams, 4);
1547 return ERROR_DOS(ERRDOS,ERRnomem);
1551 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
1552 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1553 return ERROR_DOS(ERRDOS,ERRnomem);
1557 * Get the permissions to return.
1560 if (!lp_nt_acl_support(SNUM(conn)))
1561 sd_size = get_null_nt_acl(mem_ctx, &psd);
1563 sd_size = conn->vfs_ops.fget_nt_acl(fsp, fsp->fd, &psd);
1566 talloc_destroy(mem_ctx);
1567 return(UNIXERROR(ERRDOS,ERRnoaccess));
1570 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1572 SIVAL(params,0,(uint32)sd_size);
1574 if(max_data_count < sd_size) {
1576 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1577 params, 4, *ppdata, 0);
1578 talloc_destroy(mem_ctx);
1583 * Allocate the data we will point this at.
1586 data = Realloc(*ppdata, sd_size);
1588 talloc_destroy(mem_ctx);
1589 return ERROR_DOS(ERRDOS,ERRnomem);
1594 memset(data, '\0', sd_size);
1597 * Init the parse struct we will marshall into.
1600 prs_init(&pd, 0, mem_ctx, MARSHALL);
1603 * Setup the prs_struct to point at the memory we just
1607 prs_give_memory( &pd, data, (uint32)sd_size, False);
1610 * Finally, linearize into the outgoing buffer.
1613 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1614 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1615 security descriptor.\n"));
1617 * Return access denied for want of a better error message..
1619 talloc_destroy(mem_ctx);
1620 return(UNIXERROR(ERRDOS,ERRnoaccess));
1624 * Now we can delete the security descriptor.
1627 talloc_destroy(mem_ctx);
1629 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
1633 /****************************************************************************
1634 Reply to set a security descriptor. Map to UNIX perms.
1635 ****************************************************************************/
1637 static int call_nt_transact_set_security_desc(connection_struct *conn,
1638 char *inbuf, char *outbuf, int length,
1639 int bufsize, char **ppsetup,
1640 char **ppparams, char **ppdata)
1642 uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1643 char *params= *ppparams;
1644 char *data = *ppdata;
1645 uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
1646 files_struct *fsp = NULL;
1647 uint32 security_info_sent = 0;
1650 if(total_parameter_count < 8)
1651 return ERROR_DOS(ERRDOS,ERRbadfunc);
1653 if((fsp = file_fsp(params,0)) == NULL)
1654 return ERROR_DOS(ERRDOS,ERRbadfid);
1656 if(!lp_nt_acl_support(SNUM(conn)))
1659 security_info_sent = IVAL(params,4);
1661 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1662 (unsigned int)security_info_sent ));
1664 if (total_data_count == 0)
1665 return ERROR_DOS(ERRDOS, ERRnoaccess);
1667 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, total_data_count, security_info_sent)))
1668 return ERROR_NT(nt_status);
1672 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1676 /****************************************************************************
1678 ****************************************************************************/
1679 static int call_nt_transact_ioctl(connection_struct *conn,
1680 char *inbuf, char *outbuf, int length,
1682 char **ppsetup, int setup_count,
1683 char **ppparams, int parameter_count,
1684 char **ppdata, int data_count)
1686 unsigned fnum, control;
1687 static BOOL logged_message;
1689 if (setup_count != 8) {
1690 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1691 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1694 fnum = SVAL(*ppsetup, 4);
1695 control = IVAL(*ppsetup, 0);
1697 DEBUG(6,("call_nt_transact_ioctl: fnum=%d control=0x%x\n",
1701 case NTIOCTL_SET_SPARSE:
1702 /* pretend this succeeded - tho strictly we should
1703 mark the file sparse (if the local fs supports it)
1704 so we can know if we need to pre-allocate or not */
1705 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1709 if (!logged_message) {
1710 logged_message = True; /* Only print this once... */
1711 DEBUG(3,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
1716 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1719 /****************************************************************************
1720 Reply to a SMBNTtrans.
1721 ****************************************************************************/
1723 int reply_nttrans(connection_struct *conn,
1724 char *inbuf,char *outbuf,int length,int bufsize)
1727 #if 0 /* Not used. */
1728 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1729 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1730 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1731 #endif /* Not used. */
1732 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1733 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1734 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1735 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1736 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1737 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1738 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1739 uint16 function_code = SVAL( inbuf, smb_nt_Function);
1740 char *params = NULL, *data = NULL, *setup = NULL;
1741 uint32 num_params_sofar, num_data_sofar;
1742 START_PROFILE(SMBnttrans);
1744 if(global_oplock_break &&
1745 ((function_code == NT_TRANSACT_CREATE) ||
1746 (function_code == NT_TRANSACT_RENAME))) {
1748 * Queue this open message as we are the process of an oplock break.
1751 DEBUG(2,("reply_nttrans: queueing message code 0x%x \
1752 due to being in oplock break state.\n", (unsigned int)function_code ));
1754 push_oplock_pending_smb_message( inbuf, length);
1755 END_PROFILE(SMBnttrans);
1759 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
1760 END_PROFILE(SMBnttrans);
1761 return ERROR_DOS(ERRSRV,ERRaccess);
1764 outsize = set_message(outbuf,0,0,True);
1767 * All nttrans messages we handle have smb_wct == 19 + setup_count.
1768 * Ensure this is so as a sanity check.
1771 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1772 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1773 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1777 /* Allocate the space for the setup, the maximum needed parameters and data */
1780 setup = (char *)malloc(setup_count);
1781 if (total_parameter_count > 0)
1782 params = (char *)malloc(total_parameter_count);
1783 if (total_data_count > 0)
1784 data = (char *)malloc(total_data_count);
1786 if ((total_parameter_count && !params) || (total_data_count && !data) ||
1787 (setup_count && !setup)) {
1791 DEBUG(0,("reply_nttrans : Out of memory\n"));
1792 END_PROFILE(SMBnttrans);
1793 return ERROR_DOS(ERRDOS,ERRnomem);
1796 /* Copy the param and data bytes sent with this request into the params buffer */
1797 num_params_sofar = parameter_count;
1798 num_data_sofar = data_count;
1800 if (parameter_count > total_parameter_count || data_count > total_data_count)
1804 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1805 if ((smb_nt_SetupStart + setup_count < smb_nt_SetupStart) ||
1806 (smb_nt_SetupStart + setup_count < setup_count))
1808 if (smb_nt_SetupStart + setup_count > length)
1811 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1812 dump_data(10, setup, setup_count);
1815 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1816 if ((parameter_offset + parameter_count < parameter_offset) ||
1817 (parameter_offset + parameter_count < parameter_count))
1819 if (smb_base(inbuf) + parameter_offset + parameter_count > inbuf + length)
1822 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1823 dump_data(10, params, parameter_count);
1826 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1827 if ((data_offset + data_count < data_offset) || (data_offset + data_count < data_count))
1829 if (smb_base(inbuf) + data_offset + data_count > inbuf + length)
1832 memcpy( data, smb_base(inbuf) + data_offset, data_count);
1833 dump_data(10, data, data_count);
1836 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1837 /* We need to send an interim response then receive the rest
1838 of the parameter/data bytes */
1839 outsize = set_message(outbuf,0,0,True);
1840 if (!send_smb(smbd_server_fd(),outbuf))
1841 exit_server("reply_nttrans: send_smb failed.");
1843 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1845 uint32 parameter_displacement;
1846 uint32 data_displacement;
1848 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
1850 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1851 outsize = set_message(outbuf,0,0,True);
1853 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1855 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1856 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1861 /* Revise total_params and total_data in case they have changed downwards */
1862 if (IVAL(inbuf, smb_nts_TotalParameterCount) < total_parameter_count)
1863 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1864 if (IVAL(inbuf, smb_nts_TotalDataCount) < total_data_count)
1865 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1867 parameter_count = IVAL(inbuf,smb_nts_ParameterCount);
1868 parameter_offset = IVAL(inbuf, smb_nts_ParameterOffset);
1869 parameter_displacement = IVAL(inbuf, smb_nts_ParameterDisplacement);
1870 num_params_sofar += parameter_count;
1872 data_count = IVAL(inbuf, smb_nts_DataCount);
1873 data_displacement = IVAL(inbuf, smb_nts_DataDisplacement);
1874 data_offset = IVAL(inbuf, smb_nts_DataDisplacement);
1875 num_data_sofar += data_count;
1877 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count) {
1878 DEBUG(0,("reply_nttrans2: data overflow in secondary nttrans packet"));
1882 if (parameter_count) {
1883 if (parameter_displacement + parameter_count >= total_parameter_count)
1885 if ((parameter_displacement + parameter_count < parameter_displacement) ||
1886 (parameter_displacement + parameter_count < parameter_count))
1888 if (smb_base(inbuf) + parameter_offset + parameter_count >= inbuf + bufsize)
1890 if (parameter_displacement + params < params)
1893 memcpy( ¶ms[parameter_displacement], smb_base(inbuf) + parameter_offset, parameter_count);
1897 if (data_displacement + data_count >= total_data_count)
1899 if ((data_displacement + data_count < data_displacement) ||
1900 (data_displacement + data_count < data_count))
1902 if (smb_base(inbuf) + data_offset + data_count >= inbuf + bufsize)
1904 if (data_displacement + data < data)
1907 memcpy( &data[data_displacement], smb_base(inbuf)+ data_offset, data_count);
1912 if (Protocol >= PROTOCOL_NT1)
1913 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | FLAGS2_IS_LONG_NAME);
1915 /* Now we must call the relevant NT_TRANS function */
1916 switch(function_code) {
1917 case NT_TRANSACT_CREATE:
1918 START_PROFILE_NESTED(NT_transact_create);
1919 outsize = call_nt_transact_create(conn, inbuf, outbuf,
1921 &setup, ¶ms, &data);
1922 END_PROFILE_NESTED(NT_transact_create);
1924 case NT_TRANSACT_IOCTL:
1925 START_PROFILE_NESTED(NT_transact_ioctl);
1926 outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
1928 &setup, setup_count,
1929 ¶ms, parameter_count,
1931 END_PROFILE_NESTED(NT_transact_ioctl);
1933 case NT_TRANSACT_SET_SECURITY_DESC:
1934 START_PROFILE_NESTED(NT_transact_set_security_desc);
1935 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
1937 &setup, ¶ms, &data);
1938 END_PROFILE_NESTED(NT_transact_set_security_desc);
1940 case NT_TRANSACT_NOTIFY_CHANGE:
1941 START_PROFILE_NESTED(NT_transact_notify_change);
1942 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
1944 &setup, ¶ms, &data);
1945 END_PROFILE_NESTED(NT_transact_notify_change);
1947 case NT_TRANSACT_RENAME:
1948 START_PROFILE_NESTED(NT_transact_rename);
1949 outsize = call_nt_transact_rename(conn, inbuf, outbuf,
1951 &setup, ¶ms, &data);
1952 END_PROFILE_NESTED(NT_transact_rename);
1955 case NT_TRANSACT_QUERY_SECURITY_DESC:
1956 START_PROFILE_NESTED(NT_transact_query_security_desc);
1957 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
1959 &setup, ¶ms, &data);
1960 END_PROFILE_NESTED(NT_transact_query_security_desc);
1963 /* Error in request */
1964 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1968 END_PROFILE(SMBnttrans);
1969 return ERROR_DOS(ERRSRV,ERRerror);
1972 /* As we do not know how many data packets will need to be
1973 returned here the various call_nt_transact_xxxx calls
1974 must send their own. Thus a call_nt_transact_xxxx routine only
1975 returns a value other than -1 when it wants to send
1982 END_PROFILE(SMBnttrans);
1983 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
1984 calls have already sent it. If outsize != -1 then it is
1985 returning an error packet. */
1992 END_PROFILE(SMBnttrans);
1993 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);