2 Unix SMB/Netbios implementation.
4 SMB NT transaction handling
5 Copyright (C) Jeremy Allison 1994-1998
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 extern int DEBUGLEVEL;
26 extern int smb_read_error;
27 extern int global_oplock_break;
28 extern BOOL case_sensitive;
29 extern BOOL case_preserve;
30 extern BOOL short_case_preserve;
32 static char *known_nt_pipes[] = {
50 /****************************************************************************
51 Send the required number of replies back.
52 We assume all fields other than the data fields are
53 set correctly for the type of call.
54 HACK ! Always assumes smb_setup field is zero.
55 ****************************************************************************/
57 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, uint32 nt_error, char *params,
58 int paramsize, char *pdata, int datasize)
61 int data_to_send = datasize;
62 int params_to_send = paramsize;
66 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
67 int alignment_offset = 3;
68 int data_alignment_offset = 0;
71 * Initially set the wcnt area to be 18 - this is true for all
75 set_message(outbuf,18,0,True);
79 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
85 * If there genuinely are no parameters or data to send just send
89 if(params_to_send == 0 && data_to_send == 0) {
90 send_smb(smbd_server_fd(),outbuf);
95 * When sending params and data ensure that both are nicely aligned.
96 * Only do this alignment when there is also data to send - else
97 * can cause NT redirector problems.
100 if (((params_to_send % 4) != 0) && (data_to_send != 0))
101 data_alignment_offset = 4 - (params_to_send % 4);
104 * Space is bufsize minus Netbios over TCP header minus SMB header.
105 * The alignment_offset is to align the param bytes on a four byte
106 * boundary (2 bytes for data len, one byte pad).
107 * NT needs this to work correctly.
110 useable_space = bufsize - ((smb_buf(outbuf)+
111 alignment_offset+data_alignment_offset) -
115 * useable_space can never be more than max_send minus the
119 useable_space = MIN(useable_space,
120 max_send - (alignment_offset+data_alignment_offset));
123 while (params_to_send || data_to_send) {
126 * Calculate whether we will totally or partially fill this packet.
129 total_sent_thistime = params_to_send + data_to_send +
130 alignment_offset + data_alignment_offset;
133 * We can never send more than useable_space.
136 total_sent_thistime = MIN(total_sent_thistime, useable_space);
138 set_message(outbuf, 18, total_sent_thistime, True);
141 * Set total params and data to be sent.
144 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
145 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
148 * Calculate how many parameters and data we can fit into
149 * this packet. Parameters get precedence.
152 params_sent_thistime = MIN(params_to_send,useable_space);
153 data_sent_thistime = useable_space - params_sent_thistime;
154 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
156 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
158 if(params_sent_thistime == 0) {
159 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
160 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
163 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
164 * parameter bytes, however the first 4 bytes of outbuf are
165 * the Netbios over TCP header. Thus use smb_base() to subtract
166 * them from the calculation.
169 SIVAL(outbuf,smb_ntr_ParameterOffset,
170 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
172 * Absolute displacement of param bytes sent in this packet.
175 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
179 * Deal with the data portion.
182 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
184 if(data_sent_thistime == 0) {
185 SIVAL(outbuf,smb_ntr_DataOffset,0);
186 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
189 * The offset of the data bytes is the offset of the
190 * parameter bytes plus the number of parameters being sent this time.
193 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
194 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
195 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
199 * Copy the param bytes into the packet.
202 if(params_sent_thistime)
203 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
206 * Copy in the data bytes
209 if(data_sent_thistime)
210 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
211 data_alignment_offset,pd,data_sent_thistime);
213 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
214 params_sent_thistime, data_sent_thistime, useable_space));
215 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
216 params_to_send, data_to_send, paramsize, datasize));
218 /* Send the packet */
219 send_smb(smbd_server_fd(),outbuf);
221 pp += params_sent_thistime;
222 pd += data_sent_thistime;
224 params_to_send -= params_sent_thistime;
225 data_to_send -= data_sent_thistime;
231 if(params_to_send < 0 || data_to_send < 0) {
232 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
233 params_to_send, data_to_send));
241 /****************************************************************************
243 ****************************************************************************/
245 static BOOL saved_case_sensitive;
246 static BOOL saved_case_preserve;
247 static BOOL saved_short_case_preserve;
249 /****************************************************************************
251 ****************************************************************************/
253 static void set_posix_case_semantics(uint32 file_attributes)
255 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
258 saved_case_sensitive = case_sensitive;
259 saved_case_preserve = case_preserve;
260 saved_short_case_preserve = short_case_preserve;
263 case_sensitive = True;
264 case_preserve = True;
265 short_case_preserve = True;
268 /****************************************************************************
269 Restore case semantics.
270 ****************************************************************************/
272 static void restore_case_semantics(uint32 file_attributes)
274 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
277 case_sensitive = saved_case_sensitive;
278 case_preserve = saved_case_preserve;
279 short_case_preserve = saved_short_case_preserve;
282 /****************************************************************************
283 Utility function to map create disposition.
284 ****************************************************************************/
286 static int map_create_disposition( uint32 create_disposition)
290 switch( create_disposition ) {
292 /* create if not exist, fail if exist */
293 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
296 case FILE_OVERWRITE_IF:
297 /* create if not exist, trunc if exist */
298 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
301 /* fail if not exist, open if exists */
302 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
305 /* create if not exist, open if exists */
306 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
309 /* fail if not exist, truncate if exists */
310 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
313 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
314 create_disposition ));
318 DEBUG(10,("map_create_disposition: Mapped create_disposition %lx to %x\n",
319 (unsigned long)create_disposition, ret ));
324 /****************************************************************************
325 Utility function to map share modes.
326 ****************************************************************************/
328 static int map_share_mode( BOOL *pstat_open_only, char *fname,
329 uint32 desired_access, uint32 share_access, uint32 file_attributes)
331 int smb_open_mode = -1;
333 *pstat_open_only = False;
335 switch( desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
337 smb_open_mode = DOS_OPEN_RDONLY;
339 case FILE_WRITE_DATA:
340 case FILE_APPEND_DATA:
341 case FILE_WRITE_DATA|FILE_APPEND_DATA:
342 smb_open_mode = DOS_OPEN_WRONLY;
344 case FILE_READ_DATA|FILE_WRITE_DATA:
345 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
346 case FILE_READ_DATA|FILE_APPEND_DATA:
347 smb_open_mode = DOS_OPEN_RDWR;
352 * NB. For DELETE_ACCESS we should really check the
353 * directory permissions, as that is what controls
354 * delete, and for WRITE_DAC_ACCESS we should really
355 * check the ownership, as that is what controls the
356 * chmod. Note that this is *NOT* a security hole (this
357 * note is for you, Andrew) as we are not *allowing*
358 * the access at this point, the actual unlink or
359 * chown or chmod call would do this. We are just helping
360 * clients out by telling them if they have a hope
361 * of any of this succeeding. POSIX acls may still
362 * deny the real call. JRA.
365 if (smb_open_mode == -1) {
367 if(desired_access == WRITE_DAC_ACCESS || desired_access == READ_CONTROL_ACCESS)
368 *pstat_open_only = True;
370 if(desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|
371 FILE_EXECUTE|FILE_READ_ATTRIBUTES|
372 FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
373 FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
374 smb_open_mode = DOS_OPEN_RDONLY;
375 } else if(desired_access == 0) {
378 * JRA - NT seems to sometimes send desired_access as zero. play it safe
379 * and map to a stat open.
382 *pstat_open_only = True;
383 smb_open_mode = DOS_OPEN_RDONLY;
386 DEBUG(0,("map_share_mode: Incorrect value %lx for desired_access to file %s\n",
387 (unsigned long)desired_access, fname));
393 * Set the special bit that means allow share delete.
394 * This is held outside the normal share mode bits at 1<<15.
398 if(share_access & FILE_SHARE_DELETE)
399 smb_open_mode |= ALLOW_SHARE_DELETE;
401 /* Add in the requested share mode. */
402 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
403 case FILE_SHARE_READ:
404 smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
406 case FILE_SHARE_WRITE:
407 smb_open_mode |= SET_DENY_MODE(DENY_READ);
409 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
410 smb_open_mode |= SET_DENY_MODE(DENY_NONE);
412 case FILE_SHARE_NONE:
413 smb_open_mode |= SET_DENY_MODE(DENY_ALL);
418 * Handle an O_SYNC request.
421 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
422 smb_open_mode |= FILE_SYNC_OPENMODE;
424 DEBUG(10,("map_share_mode: Mapped desired access %lx, share access %lx, file attributes %lx \
425 to open_mode %x\n", (unsigned long)desired_access, (unsigned long)share_access,
426 (unsigned long)file_attributes, smb_open_mode ));
428 return smb_open_mode;
433 * This is a *disgusting* hack.
434 * This is *so* bad that even I'm embarrassed (and I
435 * have no shame). Here's the deal :
436 * Until we get the correct SPOOLSS code into smbd
437 * then when we're running with NT SMB support then
438 * NT makes this call with a level of zero, and then
439 * immediately follows it with an open request to
440 * the \\SRVSVC pipe. If we allow that open to
441 * succeed then NT barfs when it cannot open the
442 * \\SPOOLSS pipe immediately after and continually
443 * whines saying "Printer name is invalid" forever
444 * after. If we cause *JUST THIS NEXT OPEN* of \\SRVSVC
445 * to fail, then NT downgrades to using the downlevel code
446 * and everything works as well as before. I hate
447 * myself for adding this code.... JRA.
449 * The HACK_FAIL_TIME define allows only a 2
450 * second window for this to occur, just in
454 static BOOL fail_next_srvsvc = False;
455 static time_t fail_time;
456 #define HACK_FAIL_TIME 2 /* In seconds. */
458 void fail_next_srvsvc_open(void)
460 /* Check client is WinNT proper; Win2K doesn't like Jeremy's hack - matty */
461 if (get_remote_arch() != RA_WINNT)
464 fail_next_srvsvc = True;
465 fail_time = time(NULL);
466 DEBUG(10,("fail_next_srvsvc_open: setting up timeout close of \\srvsvc pipe for print fix.\n"));
470 * HACK alert.... see above - JRA.
473 BOOL should_fail_next_srvsvc_open(const char *pipename)
476 DEBUG(10,("should_fail_next_srvsvc_open: fail = %d, pipe = %s\n",
477 (int)fail_next_srvsvc, pipename));
479 if(fail_next_srvsvc && (time(NULL) > fail_time + HACK_FAIL_TIME)) {
480 fail_next_srvsvc = False;
481 fail_time = (time_t)0;
482 DEBUG(10,("should_fail_next_srvsvc_open: End of timeout close of \\srvsvc pipe for print fix.\n"));
485 if(fail_next_srvsvc && strequal(pipename, "srvsvc")) {
486 fail_next_srvsvc = False;
487 DEBUG(10,("should_fail_next_srvsvc_open: Deliberately failing open of \\srvsvc pipe for print fix.\n"));
494 /****************************************************************************
495 Reply to an NT create and X call on a pipe.
496 ****************************************************************************/
497 static int nt_open_pipe(char *fname, connection_struct *conn,
498 char *inbuf, char *outbuf, int *ppnum)
500 pipes_struct *p = NULL;
502 uint16 vuid = SVAL(inbuf, smb_uid);
505 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
507 /* See if it is one we want to handle. */
508 for( i = 0; known_nt_pipes[i]; i++ )
509 if( strequal(fname,known_nt_pipes[i]))
512 if ( known_nt_pipes[i] == NULL )
513 return(ERROR(ERRSRV,ERRaccess));
515 /* Strip \\ off the name. */
519 if(should_fail_next_srvsvc_open(fname))
520 return (ERROR(ERRSRV,ERRaccess));
523 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
525 p = open_rpc_pipe_p(fname, conn, vuid);
527 return(ERROR(ERRSRV,ERRnofids));
534 /****************************************************************************
535 Reply to an NT create and X call for pipes.
536 ****************************************************************************/
538 static int do_ntcreate_pipe_open(connection_struct *conn,
539 char *inbuf,char *outbuf,int length,int bufsize)
546 srvstr_pull(inbuf, fname, smb_buf(inbuf), sizeof(fname), -1, STR_TERMINATE|STR_CONVERT);
548 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
552 * Deal with pipe return.
555 set_message(outbuf,34,0,True);
557 p = outbuf + smb_vwv2;
561 SIVAL(p,0,FILE_WAS_OPENED);
564 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
567 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
569 SSVAL(p,2, 0x5FF); /* ? */
571 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
573 return chain_reply(inbuf,outbuf,length,bufsize);
576 /****************************************************************************
577 Reply to an NT create and X call.
578 ****************************************************************************/
580 int reply_ntcreate_and_X(connection_struct *conn,
581 char *inbuf,char *outbuf,int length,int bufsize)
585 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
586 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
587 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
588 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
589 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
590 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
591 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
594 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
595 /* Breakout the oplock request bits so we can set the
596 reply bits separately. */
597 int oplock_request = 0;
600 SMB_OFF_T file_len = 0;
601 SMB_STRUCT_STAT sbuf;
603 BOOL bad_path = False;
604 files_struct *fsp=NULL;
606 BOOL stat_open_only = False;
607 START_PROFILE(SMBntcreateX);
609 /* If it's an IPC, use the pipe handler. */
612 if (lp_nt_pipe_support()) {
613 END_PROFILE(SMBntcreateX);
614 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
616 END_PROFILE(SMBntcreateX);
617 return(ERROR(ERRDOS,ERRbadaccess));
623 * We need to construct the open_and_X ofun value from the
624 * NT values, as that's what our code is structured to accept.
627 if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
628 END_PROFILE(SMBntcreateX);
629 return(ERROR(ERRDOS,ERRbadaccess));
636 if(root_dir_fid != 0) {
638 * This filename is relative to a directory fid.
640 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
644 END_PROFILE(SMBntcreateX);
645 return(ERROR(ERRDOS,ERRbadfid));
648 if(!dir_fsp->is_directory) {
650 * Check to see if this is a mac fork of some kind.
653 srvstr_pull(inbuf, fname, smb_buf(inbuf), sizeof(fname), -1, STR_TERMINATE|STR_CONVERT);
655 if( strchr(fname, ':')) {
656 SSVAL(outbuf, smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
657 END_PROFILE(SMBntcreateX);
658 return(ERROR(0, NT_STATUS_OBJECT_PATH_NOT_FOUND));
660 END_PROFILE(SMBntcreateX);
661 return(ERROR(ERRDOS,ERRbadfid));
665 * Copy in the base directory name.
668 pstrcpy( fname, dir_fsp->fsp_name );
669 dir_name_len = strlen(fname);
672 * Ensure it ends in a '\'.
675 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
676 pstrcat(fname, "\\");
680 srvstr_pull(inbuf, &fname[dir_name_len], smb_buf(inbuf), sizeof(fname)-dir_name_len,
681 -1, STR_TERMINATE|STR_CONVERT);
683 srvstr_pull(inbuf, fname, smb_buf(inbuf), sizeof(fname),
684 -1, STR_TERMINATE|STR_CONVERT);
688 * Now contruct the smb_open_mode value from the filename,
689 * desired access and the share access.
691 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
693 if((smb_open_mode = map_share_mode(&stat_open_only, fname, desired_access,
695 file_attributes)) == -1) {
696 END_PROFILE(SMBntcreateX);
697 return(ERROR(ERRDOS,ERRbadaccess));
700 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
701 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
704 * Ordinary file or directory.
708 * Check if POSIX semantics are wanted.
711 set_posix_case_semantics(file_attributes);
713 unix_convert(fname,conn,0,&bad_path,&sbuf);
715 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
718 * If it's a request for a directory open, deal with it separately.
721 if(create_options & FILE_DIRECTORY_FILE) {
724 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
726 restore_case_semantics(file_attributes);
729 if((errno == ENOENT) && bad_path) {
730 unix_ERR_class = ERRDOS;
731 unix_ERR_code = ERRbadpath;
733 END_PROFILE(SMBntcreateX);
734 return(UNIXERROR(ERRDOS,ERRnoaccess));
738 * Ordinary file case.
741 /* NB. We have a potential bug here. If we
742 * cause an oplock break to ourselves, then we
743 * could end up processing filename related
744 * SMB requests whilst we await the oplock
745 * break response. As we may have changed the
746 * filename case semantics to be POSIX-like,
747 * this could mean a filename request could
748 * fail when it should succeed. This is a rare
749 * condition, but eventually we must arrange
750 * to restore the correct case semantics
751 * before issuing an oplock break request to
752 * our client. JRA. */
754 fsp = open_file_shared(conn,fname,&sbuf,smb_open_mode,
755 smb_ofun,unixmode, oplock_request,&rmode,&smb_action);
758 /* We cheat here. There are two cases we
759 * care about. One is a directory rename,
760 * where the NT client will attempt to
761 * open the source directory for
762 * DELETE access. Note that when the
763 * NT client does this it does *not*
764 * set the directory bit in the
765 * request packet. This is translated
766 * into a read/write open
767 * request. POSIX states that any open
768 * for write request on a directory
769 * will generate an EISDIR error, so
770 * we can catch this here and open a
771 * pseudo handle that is flagged as a
772 * directory. The second is an open
773 * for a permissions read only, which
774 * we handle in the open_file_stat case. JRA.
777 if(errno == EISDIR) {
780 * Fail the open if it was explicitly a non-directory file.
783 if (create_options & FILE_NON_DIRECTORY_FILE) {
784 restore_case_semantics(file_attributes);
785 SSVAL(outbuf, smb_flg2,
786 SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
787 END_PROFILE(SMBntcreateX);
788 return(ERROR(0, NT_STATUS_FILE_IS_A_DIRECTORY));
792 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
795 restore_case_semantics(file_attributes);
796 if((errno == ENOENT) && bad_path) {
797 unix_ERR_class = ERRDOS;
798 unix_ERR_code = ERRbadpath;
800 END_PROFILE(SMBntcreateX);
801 return(UNIXERROR(ERRDOS,ERRnoaccess));
804 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
806 } else if (errno == EACCES && stat_open_only) {
809 * We couldn't open normally and all we want
810 * are the permissions. Try and do a stat open.
815 fsp = open_file_stat(conn,fname,&sbuf,smb_open_mode,&smb_action);
818 restore_case_semantics(file_attributes);
819 END_PROFILE(SMBntcreateX);
820 return(UNIXERROR(ERRDOS,ERRnoaccess));
825 if((errno == ENOENT) && bad_path) {
826 unix_ERR_class = ERRDOS;
827 unix_ERR_code = ERRbadpath;
830 restore_case_semantics(file_attributes);
832 END_PROFILE(SMBntcreateX);
833 return(UNIXERROR(ERRDOS,ERRnoaccess));
838 restore_case_semantics(file_attributes);
840 file_len = sbuf.st_size;
841 fmode = dos_mode(conn,fname,&sbuf);
843 fmode = FILE_ATTRIBUTE_NORMAL;
844 if (!fsp->is_directory && (fmode & aDIR)) {
845 close_file(fsp,False);
846 END_PROFILE(SMBntcreateX);
847 return(ERROR(ERRDOS,ERRnoaccess));
851 * If the caller set the extended oplock request bit
852 * and we granted one (by whatever means) - set the
853 * correct bit for extended oplock reply.
856 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
857 smb_action |= EXTENDED_OPLOCK_GRANTED;
859 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
860 smb_action |= EXTENDED_OPLOCK_GRANTED;
862 set_message(outbuf,34,0,True);
864 p = outbuf + smb_vwv2;
867 * Currently as we don't support level II oplocks we just report
868 * exclusive & batch here.
871 if (smb_action & EXTENDED_OPLOCK_GRANTED)
872 SCVAL(p,0, BATCH_OPLOCK_RETURN);
873 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
874 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
876 SCVAL(p,0,NO_OPLOCK_RETURN);
879 SSVAL(p,0,fsp->fnum);
881 SIVAL(p,0,smb_action);
885 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
887 put_long_date(p,sbuf.st_atime); /* access time */
889 put_long_date(p,sbuf.st_mtime); /* write time */
891 put_long_date(p,sbuf.st_mtime); /* change time */
893 SIVAL(p,0,fmode); /* File Attributes. */
895 SOFF_T(p, 0, file_len);
897 SOFF_T(p,0,file_len);
899 SCVAL(p,0,fsp->is_directory ? 1 : 0);
901 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
903 result = chain_reply(inbuf,outbuf,length,bufsize);
904 END_PROFILE(SMBntcreateX);
908 /****************************************************************************
909 Reply to a NT_TRANSACT_CREATE call to open a pipe.
910 ****************************************************************************/
912 static int do_nt_transact_create_pipe( connection_struct *conn,
913 char *inbuf, char *outbuf, int length,
914 int bufsize, char **ppsetup, char **ppparams,
918 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
919 char *params = *ppparams;
925 * Ensure minimum number of parameters sent.
928 if(total_parameter_count < 54) {
929 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
930 return(ERROR(ERRDOS,ERRbadaccess));
933 srvstr_pull(inbuf, fname, params+53, sizeof(fname), -1, STR_TERMINATE|STR_CONVERT);
935 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
938 /* Realloc the size of parameters and data we will return */
939 params = Realloc(*ppparams, 69);
941 return(ERROR(ERRDOS,ERRnomem));
945 memset((char *)params,'\0',69);
948 SCVAL(p,0,NO_OPLOCK_RETURN);
953 SIVAL(p,0,FILE_WAS_OPENED);
957 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
960 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
962 SSVAL(p,2, 0x5FF); /* ? */
964 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
966 /* Send the required number of replies */
967 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 69, *ppdata, 0);
972 /****************************************************************************
973 Internal fn to set security descriptors.
974 ****************************************************************************/
976 static BOOL set_sd(files_struct *fsp, char *data, uint32 sd_len, uint security_info_sent, int *pdef_class,uint32 *pdef_code)
979 SEC_DESC *psd = NULL;
984 *pdef_class = ERRDOS;
985 *pdef_code = ERRbadaccess;
990 * Init the parse struct we will unmarshall from.
993 if ((mem_ctx = talloc_init()) == NULL) {
994 DEBUG(0,("set_sd: talloc_init failed.\n"));
995 *pdef_class = ERRDOS;
996 *pdef_code = ERRnomem;
1000 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1003 * Setup the prs_struct to point at the memory we just
1007 prs_give_memory( &pd, data, sd_len, False);
1010 * Finally, unmarshall from the data buffer.
1013 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1014 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1016 * Return access denied for want of a better error message..
1018 talloc_destroy(mem_ctx);
1019 *pdef_class = ERRDOS;
1020 *pdef_code = ERRnomem;
1024 if (psd->off_owner_sid==0)
1025 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1026 if (psd->off_grp_sid==0)
1027 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1028 if (psd->off_sacl==0)
1029 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1030 if (psd->off_dacl==0)
1031 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1033 ret = set_nt_acl( fsp, security_info_sent, psd);
1036 talloc_destroy(mem_ctx);
1037 *pdef_class = ERRDOS;
1038 *pdef_code = ERRnoaccess;
1042 talloc_destroy(mem_ctx);
1049 /****************************************************************************
1050 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1051 ****************************************************************************/
1053 static int call_nt_transact_create(connection_struct *conn,
1054 char *inbuf, char *outbuf, int length,
1055 int bufsize, char **ppsetup, char **ppparams,
1059 char *params = *ppparams;
1060 char *data = *ppdata;
1061 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1062 /* Breakout the oplock request bits so we can set the
1063 reply bits separately. */
1064 int oplock_request = 0;
1066 int fmode=0,rmode=0;
1067 SMB_OFF_T file_len = 0;
1068 SMB_STRUCT_STAT sbuf;
1070 BOOL bad_path = False;
1071 files_struct *fsp = NULL;
1073 BOOL stat_open_only = False;
1075 uint32 desired_access;
1076 uint32 file_attributes;
1077 uint32 share_access;
1078 uint32 create_disposition;
1079 uint32 create_options;
1081 uint16 root_dir_fid;
1088 DEBUG(5,("call_nt_transact_create\n"));
1091 * If it's an IPC, use the pipe handler.
1095 if (lp_nt_pipe_support())
1096 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1097 bufsize, ppsetup, ppparams, ppdata);
1099 return(ERROR(ERRDOS,ERRbadaccess));
1103 * Ensure minimum number of parameters sent.
1106 if(total_parameter_count < 54) {
1107 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1108 return(ERROR(ERRDOS,ERRbadaccess));
1111 flags = IVAL(params,0);
1112 desired_access = IVAL(params,8);
1113 file_attributes = IVAL(params,20);
1114 share_access = IVAL(params,24);
1115 create_disposition = IVAL(params,28);
1116 create_options = IVAL(params,32);
1117 sd_len = IVAL(params,36);
1118 root_dir_fid = (uint16)IVAL(params,4);
1119 smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1122 * We need to construct the open_and_X ofun value from the
1123 * NT values, as that's what our code is structured to accept.
1126 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1127 return(ERROR(ERRDOS,ERRbadmem));
1130 * Get the file name.
1133 if(root_dir_fid != 0) {
1135 * This filename is relative to a directory fid.
1138 files_struct *dir_fsp = file_fsp(params,4);
1139 size_t dir_name_len;
1142 return(ERROR(ERRDOS,ERRbadfid));
1144 if(!dir_fsp->is_directory) {
1146 * Check to see if this is a mac fork of some kind.
1149 srvstr_pull(inbuf, fname, params+53, sizeof(fname), -1, STR_TERMINATE|STR_CONVERT);
1151 if( strchr(fname, ':')) {
1152 SSVAL(outbuf, smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1153 return(ERROR(0, NT_STATUS_OBJECT_PATH_NOT_FOUND));
1156 return(ERROR(ERRDOS,ERRbadfid));
1160 * Copy in the base directory name.
1163 pstrcpy( fname, dir_fsp->fsp_name );
1164 dir_name_len = strlen(fname);
1167 * Ensure it ends in a '\'.
1170 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1171 pstrcat(fname, "\\");
1175 srvstr_pull(inbuf, &fname[dir_name_len], params+53, sizeof(fname)-dir_name_len,
1176 -1, STR_TERMINATE|STR_CONVERT);
1178 srvstr_pull(inbuf, fname, params+53, sizeof(fname), -1, STR_TERMINATE|STR_CONVERT);
1182 * Now contruct the smb_open_mode value from the desired access
1183 * and the share access.
1186 if((smb_open_mode = map_share_mode( &stat_open_only, fname, desired_access,
1187 share_access, file_attributes)) == -1)
1188 return(ERROR(ERRDOS,ERRbadaccess));
1190 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1191 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1194 * Check if POSIX semantics are wanted.
1197 set_posix_case_semantics(file_attributes);
1199 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1201 unix_convert(fname,conn,0,&bad_path,&sbuf);
1203 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1206 * If it's a request for a directory open, deal with it separately.
1209 if(create_options & FILE_DIRECTORY_FILE) {
1214 * We will get a create directory here if the Win32
1215 * app specified a security descriptor in the
1216 * CreateDirectory() call.
1219 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
1222 restore_case_semantics(file_attributes);
1223 if((errno == ENOENT) && bad_path) {
1224 unix_ERR_class = ERRDOS;
1225 unix_ERR_code = ERRbadpath;
1227 return(UNIXERROR(ERRDOS,ERRnoaccess));
1233 * Ordinary file case.
1236 fsp = open_file_shared(conn,fname,&sbuf,smb_open_mode,smb_ofun,unixmode,
1237 oplock_request,&rmode,&smb_action);
1241 if(errno == EISDIR) {
1244 * Fail the open if it was explicitly a non-directory file.
1247 if (create_options & FILE_NON_DIRECTORY_FILE) {
1248 restore_case_semantics(file_attributes);
1249 SSVAL(outbuf, smb_flg2,
1250 SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1251 return(ERROR(0, NT_STATUS_FILE_IS_A_DIRECTORY));
1255 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
1258 restore_case_semantics(file_attributes);
1259 if((errno == ENOENT) && bad_path) {
1260 unix_ERR_class = ERRDOS;
1261 unix_ERR_code = ERRbadpath;
1263 return(UNIXERROR(ERRDOS,ERRnoaccess));
1266 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
1268 } else if (errno == EACCES && stat_open_only) {
1272 * We couldn't open normally and all we want
1273 * are the permissions. Try and do a stat open.
1278 fsp = open_file_stat(conn,fname,&sbuf,smb_open_mode,&smb_action);
1281 restore_case_semantics(file_attributes);
1282 return(UNIXERROR(ERRDOS,ERRnoaccess));
1286 if((errno == ENOENT) && bad_path) {
1287 unix_ERR_class = ERRDOS;
1288 unix_ERR_code = ERRbadpath;
1291 restore_case_semantics(file_attributes);
1293 return(UNIXERROR(ERRDOS,ERRnoaccess));
1297 file_len = sbuf.st_size;
1298 fmode = dos_mode(conn,fname,&sbuf);
1300 fmode = FILE_ATTRIBUTE_NORMAL;
1303 close_file(fsp,False);
1304 restore_case_semantics(file_attributes);
1305 return(ERROR(ERRDOS,ERRnoaccess));
1309 * If the caller set the extended oplock request bit
1310 * and we granted one (by whatever means) - set the
1311 * correct bit for extended oplock reply.
1314 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1315 smb_action |= EXTENDED_OPLOCK_GRANTED;
1317 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1318 smb_action |= EXTENDED_OPLOCK_GRANTED;
1322 * Now try and apply the desired SD.
1325 if (!set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION, &error_class, &error_code)) {
1326 close_file(fsp,False);
1327 restore_case_semantics(file_attributes);
1328 return(ERROR(error_class, error_code));
1331 restore_case_semantics(file_attributes);
1333 /* Realloc the size of parameters and data we will return */
1334 params = Realloc(*ppparams, 69);
1336 return(ERROR(ERRDOS,ERRnomem));
1340 memset((char *)params,'\0',69);
1343 if (smb_action & EXTENDED_OPLOCK_GRANTED)
1344 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1345 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1346 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1348 SCVAL(p,0,NO_OPLOCK_RETURN);
1351 SSVAL(p,0,fsp->fnum);
1353 SIVAL(p,0,smb_action);
1357 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
1359 put_long_date(p,sbuf.st_atime); /* access time */
1361 put_long_date(p,sbuf.st_mtime); /* write time */
1363 put_long_date(p,sbuf.st_mtime); /* change time */
1365 SIVAL(p,0,fmode); /* File Attributes. */
1367 SOFF_T(p,0,file_len);
1369 SOFF_T(p,0,file_len);
1371 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1373 /* Send the required number of replies */
1374 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 69, *ppdata, 0);
1379 /****************************************************************************
1380 Reply to a NT CANCEL request.
1381 ****************************************************************************/
1382 int reply_ntcancel(connection_struct *conn,
1383 char *inbuf,char *outbuf,int length,int bufsize)
1386 * Go through and cancel any pending change notifies.
1389 int mid = SVAL(inbuf,smb_mid);
1390 START_PROFILE(SMBntcancel);
1391 remove_pending_change_notify_requests_by_mid(mid);
1392 remove_pending_lock_requests_by_mid(mid);
1394 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1396 END_PROFILE(SMBntcancel);
1400 /****************************************************************************
1401 Reply to an unsolicited SMBNTtranss - just ignore it!
1402 ****************************************************************************/
1403 int reply_nttranss(connection_struct *conn,
1404 char *inbuf,char *outbuf,int length,int bufsize)
1406 START_PROFILE(SMBnttranss);
1407 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1408 END_PROFILE(SMBnttranss);
1412 /****************************************************************************
1413 Reply to a notify change - queue the request and
1414 don't allow a directory to be opened.
1415 ****************************************************************************/
1416 static int call_nt_transact_notify_change(connection_struct *conn,
1417 char *inbuf, char *outbuf, int length,
1420 char **ppparams, char **ppdata)
1422 char *setup = *ppsetup;
1426 fsp = file_fsp(setup,4);
1427 flags = IVAL(setup, 0);
1429 DEBUG(3,("call_nt_transact_notify_change\n"));
1432 return(ERROR(ERRDOS,ERRbadfid));
1434 if((!fsp->is_directory) || (conn != fsp->conn))
1435 return(ERROR(ERRDOS,ERRbadfid));
1437 if (!change_notify_set(inbuf, fsp, conn, flags)) {
1438 return(UNIXERROR(ERRDOS,ERRbadfid));
1441 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1442 name = %s\n", fsp->fsp_name ));
1447 /****************************************************************************
1448 Reply to an NT transact rename command.
1449 ****************************************************************************/
1451 static int call_nt_transact_rename(connection_struct *conn,
1452 char *inbuf, char *outbuf, int length,
1454 char **ppsetup, char **ppparams, char **ppdata)
1456 char *params = *ppparams;
1458 files_struct *fsp = file_fsp(params, 0);
1459 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1462 CHECK_FSP(fsp, conn);
1463 srvstr_pull(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE|STR_CONVERT);
1465 outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
1466 new_name, replace_if_exists);
1469 * Rename was successful.
1471 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
1473 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1474 fsp->fsp_name, new_name));
1479 * Win2k needs a changenotify request response before it will
1480 * update after a rename..
1483 process_pending_change_notify_queue((time_t)0);
1490 /****************************************************************************
1491 Reply to query a security descriptor - currently this is not implemented (it
1492 is planned to be though). Right now it just returns the same thing NT would
1493 when queried on a FAT filesystem. JRA.
1494 ****************************************************************************/
1496 static int call_nt_transact_query_security_desc(connection_struct *conn,
1497 char *inbuf, char *outbuf,
1498 int length, int bufsize,
1499 char **ppsetup, char **ppparams, char **ppdata)
1501 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1502 char *params = *ppparams;
1503 char *data = *ppdata;
1505 SEC_DESC *psd = NULL;
1507 TALLOC_CTX *mem_ctx;
1509 files_struct *fsp = file_fsp(params,0);
1512 return(ERROR(ERRDOS,ERRbadfid));
1514 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1516 params = Realloc(*ppparams, 4);
1518 return(ERROR(ERRDOS,ERRnomem));
1523 * Get the permissions to return.
1526 if((sd_size = get_nt_acl(fsp, &psd)) == 0)
1527 return(UNIXERROR(ERRDOS,ERRnoaccess));
1529 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1531 SIVAL(params,0,(uint32)sd_size);
1533 if(max_data_count < sd_size) {
1535 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1536 params, 4, *ppdata, 0);
1541 * Allocate the data we will point this at.
1544 data = Realloc(*ppdata, sd_size);
1546 return(ERROR(ERRDOS,ERRnomem));
1551 memset(data, '\0', sd_size);
1554 * Init the parse struct we will marshall into.
1557 if ((mem_ctx = talloc_init()) == NULL) {
1558 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1559 return(ERROR(ERRDOS,ERRnomem));
1562 prs_init(&pd, 0, mem_ctx, MARSHALL);
1565 * Setup the prs_struct to point at the memory we just
1569 prs_give_memory( &pd, data, (uint32)sd_size, False);
1572 * Finally, linearize into the outgoing buffer.
1575 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1576 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1577 security descriptor.\n"));
1579 * Return access denied for want of a better error message..
1581 talloc_destroy(mem_ctx);
1582 return(UNIXERROR(ERRDOS,ERRnoaccess));
1586 * Now we can delete the security descriptor.
1589 talloc_destroy(mem_ctx);
1591 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 4, data, (int)sd_size);
1595 /****************************************************************************
1596 Reply to set a security descriptor. Map to UNIX perms.
1597 ****************************************************************************/
1599 static int call_nt_transact_set_security_desc(connection_struct *conn,
1600 char *inbuf, char *outbuf, int length,
1601 int bufsize, char **ppsetup,
1602 char **ppparams, char **ppdata)
1604 uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1605 char *params= *ppparams;
1606 char *data = *ppdata;
1607 uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
1608 files_struct *fsp = NULL;
1609 uint32 security_info_sent = 0;
1613 if(!lp_nt_acl_support())
1614 return(UNIXERROR(ERRDOS,ERRnoaccess));
1616 if(total_parameter_count < 8)
1617 return(ERROR(ERRDOS,ERRbadfunc));
1619 if((fsp = file_fsp(params,0)) == NULL)
1620 return(ERROR(ERRDOS,ERRbadfid));
1622 security_info_sent = IVAL(params,4);
1624 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1625 (unsigned int)security_info_sent ));
1627 if (!set_sd( fsp, data, total_data_count, security_info_sent, &error_class, &error_code))
1628 return (ERROR(error_class, error_code));
1630 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
1634 /****************************************************************************
1635 Reply to IOCTL - not implemented - no plans.
1636 ****************************************************************************/
1637 static int call_nt_transact_ioctl(connection_struct *conn,
1638 char *inbuf, char *outbuf, int length,
1640 char **ppsetup, char **ppparams, char **ppdata)
1642 static BOOL logged_message = False;
1644 if(!logged_message) {
1645 DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1646 logged_message = True; /* Only print this once... */
1648 return(ERROR(ERRSRV,ERRnosupport));
1651 /****************************************************************************
1652 Reply to a SMBNTtrans.
1653 ****************************************************************************/
1654 int reply_nttrans(connection_struct *conn,
1655 char *inbuf,char *outbuf,int length,int bufsize)
1658 #if 0 /* Not used. */
1659 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1660 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1661 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1662 #endif /* Not used. */
1663 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1664 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1665 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1666 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1667 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1668 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1669 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1670 uint16 function_code = SVAL( inbuf, smb_nt_Function);
1671 char *params = NULL, *data = NULL, *setup = NULL;
1672 uint32 num_params_sofar, num_data_sofar;
1673 START_PROFILE(SMBnttrans);
1675 if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1677 * Queue this open message as we are the process of an oplock break.
1680 DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1681 due to being in oplock break state.\n" ));
1683 push_oplock_pending_smb_message( inbuf, length);
1684 END_PROFILE(SMBnttrans);
1688 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
1689 END_PROFILE(SMBnttrans);
1690 return (ERROR(ERRSRV,ERRaccess));
1693 outsize = set_message(outbuf,0,0,True);
1696 * All nttrans messages we handle have smb_wct == 19 + setup_count.
1697 * Ensure this is so as a sanity check.
1700 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1701 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1702 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1703 END_PROFILE(SMBnttrans);
1704 return(ERROR(ERRSRV,ERRerror));
1707 /* Allocate the space for the setup, the maximum needed parameters and data */
1710 setup = (char *)malloc(setup_count);
1711 if (total_parameter_count > 0)
1712 params = (char *)malloc(total_parameter_count);
1713 if (total_data_count > 0)
1714 data = (char *)malloc(total_data_count);
1716 if ((total_parameter_count && !params) || (total_data_count && !data) ||
1717 (setup_count && !setup)) {
1721 DEBUG(0,("reply_nttrans : Out of memory\n"));
1722 END_PROFILE(SMBnttrans);
1723 return(ERROR(ERRDOS,ERRnomem));
1726 /* Copy the param and data bytes sent with this request into
1727 the params buffer */
1728 num_params_sofar = parameter_count;
1729 num_data_sofar = data_count;
1731 if (parameter_count > total_parameter_count || data_count > total_data_count)
1732 exit_server("reply_nttrans: invalid sizes in packet.\n");
1735 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1736 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1737 dump_data(10, setup, setup_count);
1740 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1741 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1742 dump_data(10, params, parameter_count);
1745 memcpy( data, smb_base(inbuf) + data_offset, data_count);
1746 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1747 dump_data(10, data, data_count);
1750 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1751 /* We need to send an interim response then receive the rest
1752 of the parameter/data bytes */
1753 outsize = set_message(outbuf,0,0,True);
1754 send_smb(smbd_server_fd(),outbuf);
1756 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1759 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
1761 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1762 outsize = set_message(outbuf,0,0,True);
1764 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1766 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1767 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1775 END_PROFILE(SMBnttrans);
1776 return(ERROR(ERRSRV,ERRerror));
1779 /* Revise total_params and total_data in case they have changed downwards */
1780 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1781 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1782 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1783 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1784 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1785 exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
1787 memcpy( ¶ms[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
1788 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1789 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1790 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1794 if (Protocol >= PROTOCOL_NT1) {
1795 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | 0x40); /* IS_LONG_NAME */
1798 /* Now we must call the relevant NT_TRANS function */
1799 switch(function_code) {
1800 case NT_TRANSACT_CREATE:
1801 START_PROFILE_NESTED(NT_transact_create);
1802 outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize,
1803 &setup, ¶ms, &data);
1804 END_PROFILE_NESTED(NT_transact_create);
1806 case NT_TRANSACT_IOCTL:
1807 START_PROFILE_NESTED(NT_transact_ioctl);
1808 outsize = call_nt_transact_ioctl(conn,
1809 inbuf, outbuf, length, bufsize,
1810 &setup, ¶ms, &data);
1811 END_PROFILE_NESTED(NT_transact_ioctl);
1813 case NT_TRANSACT_SET_SECURITY_DESC:
1814 START_PROFILE_NESTED(NT_transact_set_security_desc);
1815 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
1817 &setup, ¶ms, &data);
1818 END_PROFILE_NESTED(NT_transact_set_security_desc);
1820 case NT_TRANSACT_NOTIFY_CHANGE:
1821 START_PROFILE_NESTED(NT_transact_notify_change);
1822 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
1824 &setup, ¶ms, &data);
1825 END_PROFILE_NESTED(NT_transact_notify_change);
1827 case NT_TRANSACT_RENAME:
1828 START_PROFILE_NESTED(NT_transact_rename);
1829 outsize = call_nt_transact_rename(conn, inbuf, outbuf, length,
1831 &setup, ¶ms, &data);
1832 END_PROFILE_NESTED(NT_transact_rename);
1835 case NT_TRANSACT_QUERY_SECURITY_DESC:
1836 START_PROFILE_NESTED(NT_transact_query_security_desc);
1837 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
1839 &setup, ¶ms, &data);
1840 END_PROFILE_NESTED(NT_transact_query_security_desc);
1843 /* Error in request */
1844 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1851 END_PROFILE(SMBnttrans);
1852 return (ERROR(ERRSRV,ERRerror));
1855 /* As we do not know how many data packets will need to be
1856 returned here the various call_nt_transact_xxxx calls
1857 must send their own. Thus a call_nt_transact_xxxx routine only
1858 returns a value other than -1 when it wants to send
1868 END_PROFILE(SMBnttrans);
1869 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
1870 calls have already sent it. If outsize != -1 then it is
1871 returning an error packet. */