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.
25 extern int smb_read_error;
26 extern int global_oplock_break;
27 extern BOOL case_sensitive;
28 extern BOOL case_preserve;
29 extern BOOL short_case_preserve;
31 static char *known_nt_pipes[] = {
47 /* Map generic permissions to file object specific permissions */
49 struct generic_mapping file_generic_mapping = {
56 /****************************************************************************
57 Send the required number of replies back.
58 We assume all fields other than the data fields are
59 set correctly for the type of call.
60 HACK ! Always assumes smb_setup field is zero.
61 ****************************************************************************/
63 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, NTSTATUS nt_error, char *params,
64 int paramsize, char *pdata, int datasize)
67 int data_to_send = datasize;
68 int params_to_send = paramsize;
72 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
73 int alignment_offset = 3;
74 int data_alignment_offset = 0;
77 * Initially set the wcnt area to be 18 - this is true for all
81 set_message(outbuf,18,0,True);
83 if (NT_STATUS_V(nt_error)) {
88 * If there genuinely are no parameters or data to send just send
92 if(params_to_send == 0 && data_to_send == 0) {
93 if (!send_smb(smbd_server_fd(),outbuf))
94 exit_server("send_nt_replies: send_smb failed.");
99 * When sending params and data ensure that both are nicely aligned.
100 * Only do this alignment when there is also data to send - else
101 * can cause NT redirector problems.
104 if (((params_to_send % 4) != 0) && (data_to_send != 0))
105 data_alignment_offset = 4 - (params_to_send % 4);
108 * Space is bufsize minus Netbios over TCP header minus SMB header.
109 * The alignment_offset is to align the param bytes on a four byte
110 * boundary (2 bytes for data len, one byte pad).
111 * NT needs this to work correctly.
114 useable_space = bufsize - ((smb_buf(outbuf)+
115 alignment_offset+data_alignment_offset) -
119 * useable_space can never be more than max_send minus the
123 useable_space = MIN(useable_space,
124 max_send - (alignment_offset+data_alignment_offset));
127 while (params_to_send || data_to_send) {
130 * Calculate whether we will totally or partially fill this packet.
133 total_sent_thistime = params_to_send + data_to_send +
134 alignment_offset + data_alignment_offset;
137 * We can never send more than useable_space.
140 total_sent_thistime = MIN(total_sent_thistime, useable_space);
142 set_message(outbuf, 18, total_sent_thistime, True);
145 * Set total params and data to be sent.
148 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
149 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
152 * Calculate how many parameters and data we can fit into
153 * this packet. Parameters get precedence.
156 params_sent_thistime = MIN(params_to_send,useable_space);
157 data_sent_thistime = useable_space - params_sent_thistime;
158 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
160 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
162 if(params_sent_thistime == 0) {
163 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
164 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
167 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
168 * parameter bytes, however the first 4 bytes of outbuf are
169 * the Netbios over TCP header. Thus use smb_base() to subtract
170 * them from the calculation.
173 SIVAL(outbuf,smb_ntr_ParameterOffset,
174 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
176 * Absolute displacement of param bytes sent in this packet.
179 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
183 * Deal with the data portion.
186 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
188 if(data_sent_thistime == 0) {
189 SIVAL(outbuf,smb_ntr_DataOffset,0);
190 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
193 * The offset of the data bytes is the offset of the
194 * parameter bytes plus the number of parameters being sent this time.
197 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
198 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
199 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
203 * Copy the param bytes into the packet.
206 if(params_sent_thistime)
207 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
210 * Copy in the data bytes
213 if(data_sent_thistime)
214 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
215 data_alignment_offset,pd,data_sent_thistime);
217 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
218 params_sent_thistime, data_sent_thistime, useable_space));
219 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
220 params_to_send, data_to_send, paramsize, datasize));
222 /* Send the packet */
223 if (!send_smb(smbd_server_fd(),outbuf))
224 exit_server("send_nt_replies: send_smb failed.");
226 pp += params_sent_thistime;
227 pd += data_sent_thistime;
229 params_to_send -= params_sent_thistime;
230 data_to_send -= data_sent_thistime;
236 if(params_to_send < 0 || data_to_send < 0) {
237 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
238 params_to_send, data_to_send));
246 /****************************************************************************
248 ****************************************************************************/
250 static BOOL saved_case_sensitive;
251 static BOOL saved_case_preserve;
252 static BOOL saved_short_case_preserve;
254 /****************************************************************************
256 ****************************************************************************/
258 static void set_posix_case_semantics(uint32 file_attributes)
260 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
263 saved_case_sensitive = case_sensitive;
264 saved_case_preserve = case_preserve;
265 saved_short_case_preserve = short_case_preserve;
268 case_sensitive = True;
269 case_preserve = True;
270 short_case_preserve = True;
273 /****************************************************************************
274 Restore case semantics.
275 ****************************************************************************/
277 static void restore_case_semantics(uint32 file_attributes)
279 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
282 case_sensitive = saved_case_sensitive;
283 case_preserve = saved_case_preserve;
284 short_case_preserve = saved_short_case_preserve;
287 /****************************************************************************
288 Utility function to map create disposition.
289 ****************************************************************************/
291 static int map_create_disposition( uint32 create_disposition)
295 switch( create_disposition ) {
297 /* create if not exist, fail if exist */
298 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
301 case FILE_OVERWRITE_IF:
302 /* create if not exist, trunc if exist */
303 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
306 /* fail if not exist, open if exists */
307 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
310 /* create if not exist, open if exists */
311 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
314 /* fail if not exist, truncate if exists */
315 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
318 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
319 create_disposition ));
323 DEBUG(10,("map_create_disposition: Mapped create_disposition %lx to %x\n",
324 (unsigned long)create_disposition, ret ));
329 /****************************************************************************
330 Utility function to map share modes.
331 ****************************************************************************/
333 static int map_share_mode( BOOL *pstat_open_only, char *fname,
334 uint32 desired_access, uint32 share_access, uint32 file_attributes)
336 int smb_open_mode = -1;
338 *pstat_open_only = False;
341 * Convert GENERIC bits to specific bits.
344 se_map_generic(&desired_access, &file_generic_mapping);
346 switch( desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
348 smb_open_mode = DOS_OPEN_RDONLY;
350 case FILE_WRITE_DATA:
351 case FILE_APPEND_DATA:
352 case FILE_WRITE_DATA|FILE_APPEND_DATA:
353 smb_open_mode = DOS_OPEN_WRONLY;
355 case FILE_READ_DATA|FILE_WRITE_DATA:
356 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
357 case FILE_READ_DATA|FILE_APPEND_DATA:
358 smb_open_mode = DOS_OPEN_RDWR;
363 * NB. For DELETE_ACCESS we should really check the
364 * directory permissions, as that is what controls
365 * delete, and for WRITE_DAC_ACCESS we should really
366 * check the ownership, as that is what controls the
367 * chmod. Note that this is *NOT* a security hole (this
368 * note is for you, Andrew) as we are not *allowing*
369 * the access at this point, the actual unlink or
370 * chown or chmod call would do this. We are just helping
371 * clients out by telling them if they have a hope
372 * of any of this succeeding. POSIX acls may still
373 * deny the real call. JRA.
376 if (smb_open_mode == -1) {
378 if(desired_access == WRITE_DAC_ACCESS || desired_access == READ_CONTROL_ACCESS)
379 *pstat_open_only = True;
381 if(desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|
382 FILE_EXECUTE|FILE_READ_ATTRIBUTES|
383 FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
384 FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
385 smb_open_mode = DOS_OPEN_RDONLY;
386 } else if(desired_access == 0) {
389 * JRA - NT seems to sometimes send desired_access as zero. play it safe
390 * and map to a stat open.
393 *pstat_open_only = True;
394 smb_open_mode = DOS_OPEN_RDONLY;
397 DEBUG(0,("map_share_mode: Incorrect value %lx for desired_access to file %s\n",
398 (unsigned long)desired_access, fname));
404 * Set the special bit that means allow share delete.
405 * This is held outside the normal share mode bits at 1<<15.
409 if(share_access & FILE_SHARE_DELETE) {
410 smb_open_mode |= ALLOW_SHARE_DELETE;
411 DEBUG(10,("map_share_mode: FILE_SHARE_DELETE requested. open_mode = %x\n", smb_open_mode));
415 * We need to store the intent to open for Delete. This
416 * is what determines if a delete on close flag can be set.
417 * This is the wrong way (and place) to store this, but for 2.2 this
418 * is the only practical way. JRA.
421 if(desired_access & DELETE_ACCESS) {
422 smb_open_mode |= DELETE_ACCESS_REQUESTED;
423 DEBUG(10,("map_share_mode: DELETE_ACCESS requested. open_mode = %x\n", smb_open_mode));
426 /* Add in the requested share mode. */
427 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
428 case FILE_SHARE_READ:
429 smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
431 case FILE_SHARE_WRITE:
432 smb_open_mode |= SET_DENY_MODE(DENY_READ);
434 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
435 smb_open_mode |= SET_DENY_MODE(DENY_NONE);
437 case FILE_SHARE_NONE:
438 smb_open_mode |= SET_DENY_MODE(DENY_ALL);
443 * Handle an O_SYNC request.
446 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
447 smb_open_mode |= FILE_SYNC_OPENMODE;
449 DEBUG(10,("map_share_mode: Mapped desired access %lx, share access %lx, file attributes %lx \
450 to open_mode %x\n", (unsigned long)desired_access, (unsigned long)share_access,
451 (unsigned long)file_attributes, smb_open_mode ));
453 return smb_open_mode;
456 /****************************************************************************
457 Reply to an NT create and X call on a pipe.
458 ****************************************************************************/
459 static int nt_open_pipe(char *fname, connection_struct *conn,
460 char *inbuf, char *outbuf, int *ppnum)
462 pipes_struct *p = NULL;
464 uint16 vuid = SVAL(inbuf, smb_uid);
467 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
469 /* See if it is one we want to handle. */
471 if (lp_disable_spoolss() && strequal(fname, "\\spoolss"))
472 return(ERROR_DOS(ERRSRV,ERRaccess));
474 for( i = 0; known_nt_pipes[i]; i++ )
475 if( strequal(fname,known_nt_pipes[i]))
478 if ( known_nt_pipes[i] == NULL )
479 return(ERROR_DOS(ERRSRV,ERRaccess));
481 /* Strip \\ off the name. */
484 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
486 p = open_rpc_pipe_p(fname, conn, vuid);
488 return(ERROR_DOS(ERRSRV,ERRnofids));
495 /****************************************************************************
496 Reply to an NT create and X call for pipes.
497 ****************************************************************************/
499 static int do_ntcreate_pipe_open(connection_struct *conn,
500 char *inbuf,char *outbuf,int length,int bufsize)
507 srvstr_pull(inbuf, fname, smb_buf(inbuf), sizeof(fname), -1, STR_TERMINATE);
509 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
513 * Deal with pipe return.
516 set_message(outbuf,34,0,True);
518 p = outbuf + smb_vwv2;
522 SIVAL(p,0,FILE_WAS_OPENED);
525 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
528 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
530 SSVAL(p,2, 0x5FF); /* ? */
532 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
534 return chain_reply(inbuf,outbuf,length,bufsize);
537 /****************************************************************************
538 Reply to an NT create and X call.
539 ****************************************************************************/
541 int reply_ntcreate_and_X(connection_struct *conn,
542 char *inbuf,char *outbuf,int length,int bufsize)
546 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
547 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
548 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
549 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
550 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
551 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
552 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
555 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
556 /* Breakout the oplock request bits so we can set the
557 reply bits separately. */
558 int oplock_request = 0;
561 SMB_OFF_T file_len = 0;
562 SMB_STRUCT_STAT sbuf;
564 BOOL bad_path = False;
565 files_struct *fsp=NULL;
567 BOOL stat_open_only = False;
569 START_PROFILE(SMBntcreateX);
571 /* If it's an IPC, use the pipe handler. */
574 if (lp_nt_pipe_support()) {
575 END_PROFILE(SMBntcreateX);
576 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
578 END_PROFILE(SMBntcreateX);
579 return(ERROR_DOS(ERRDOS,ERRbadaccess));
585 * We need to construct the open_and_X ofun value from the
586 * NT values, as that's what our code is structured to accept.
589 if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
590 END_PROFILE(SMBntcreateX);
591 return(ERROR_DOS(ERRDOS,ERRbadaccess));
598 if(root_dir_fid != 0) {
600 * This filename is relative to a directory fid.
602 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
606 END_PROFILE(SMBntcreateX);
607 return(ERROR_DOS(ERRDOS,ERRbadfid));
610 if(!dir_fsp->is_directory) {
612 * Check to see if this is a mac fork of some kind.
615 srvstr_pull(inbuf, fname, smb_buf(inbuf), sizeof(fname), -1, STR_TERMINATE);
617 if( strchr_m(fname, ':')) {
618 END_PROFILE(SMBntcreateX);
619 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
621 END_PROFILE(SMBntcreateX);
622 return(ERROR_DOS(ERRDOS,ERRbadfid));
626 * Copy in the base directory name.
629 pstrcpy( fname, dir_fsp->fsp_name );
630 dir_name_len = strlen(fname);
633 * Ensure it ends in a '\'.
636 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
637 pstrcat(fname, "\\");
641 srvstr_pull(inbuf, &fname[dir_name_len], smb_buf(inbuf), sizeof(fname)-dir_name_len,
644 srvstr_pull(inbuf, fname, smb_buf(inbuf), sizeof(fname), -1, STR_TERMINATE);
648 * Now contruct the smb_open_mode value from the filename,
649 * desired access and the share access.
651 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
653 if((smb_open_mode = map_share_mode(&stat_open_only, fname, desired_access,
655 file_attributes)) == -1) {
656 END_PROFILE(SMBntcreateX);
657 return ERROR_DOS(ERRDOS,ERRbadaccess);
660 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
661 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
664 * Ordinary file or directory.
668 * Check if POSIX semantics are wanted.
671 set_posix_case_semantics(file_attributes);
673 unix_convert(fname,conn,0,&bad_path,&sbuf);
675 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
678 * If it's a request for a directory open, deal with it separately.
681 if(create_options & FILE_DIRECTORY_FILE) {
684 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
686 restore_case_semantics(file_attributes);
689 if((errno == ENOENT) && bad_path) {
690 unix_ERR_class = ERRDOS;
691 unix_ERR_code = ERRbadpath;
693 END_PROFILE(SMBntcreateX);
694 return(UNIXERROR(ERRDOS,ERRnoaccess));
698 * Ordinary file case.
701 /* NB. We have a potential bug here. If we
702 * cause an oplock break to ourselves, then we
703 * could end up processing filename related
704 * SMB requests whilst we await the oplock
705 * break response. As we may have changed the
706 * filename case semantics to be POSIX-like,
707 * this could mean a filename request could
708 * fail when it should succeed. This is a rare
709 * condition, but eventually we must arrange
710 * to restore the correct case semantics
711 * before issuing an oplock break request to
712 * our client. JRA. */
714 fsp = open_file_shared(conn,fname,&sbuf,smb_open_mode,
715 smb_ofun,unixmode, oplock_request,&rmode,&smb_action);
718 /* We cheat here. There are two cases we
719 * care about. One is a directory rename,
720 * where the NT client will attempt to
721 * open the source directory for
722 * DELETE access. Note that when the
723 * NT client does this it does *not*
724 * set the directory bit in the
725 * request packet. This is translated
726 * into a read/write open
727 * request. POSIX states that any open
728 * for write request on a directory
729 * will generate an EISDIR error, so
730 * we can catch this here and open a
731 * pseudo handle that is flagged as a
732 * directory. The second is an open
733 * for a permissions read only, which
734 * we handle in the open_file_stat case. JRA.
737 if(errno == EISDIR) {
740 * Fail the open if it was explicitly a non-directory file.
743 if (create_options & FILE_NON_DIRECTORY_FILE) {
744 restore_case_semantics(file_attributes);
745 SSVAL(outbuf, smb_flg2,
746 SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
747 END_PROFILE(SMBntcreateX);
748 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
752 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
755 restore_case_semantics(file_attributes);
756 if((errno == ENOENT) && bad_path) {
757 unix_ERR_class = ERRDOS;
758 unix_ERR_code = ERRbadpath;
760 END_PROFILE(SMBntcreateX);
761 return(UNIXERROR(ERRDOS,ERRnoaccess));
764 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
766 } else if (errno == EACCES && stat_open_only) {
769 * We couldn't open normally and all we want
770 * are the permissions. Try and do a stat open.
775 fsp = open_file_stat(conn,fname,&sbuf,smb_open_mode,&smb_action);
778 restore_case_semantics(file_attributes);
779 END_PROFILE(SMBntcreateX);
780 return(UNIXERROR(ERRDOS,ERRnoaccess));
785 if((errno == ENOENT) && bad_path) {
786 unix_ERR_class = ERRDOS;
787 unix_ERR_code = ERRbadpath;
790 restore_case_semantics(file_attributes);
792 END_PROFILE(SMBntcreateX);
793 return(UNIXERROR(ERRDOS,ERRnoaccess));
798 restore_case_semantics(file_attributes);
800 file_len = sbuf.st_size;
801 fmode = dos_mode(conn,fname,&sbuf);
803 fmode = FILE_ATTRIBUTE_NORMAL;
804 if (!fsp->is_directory && (fmode & aDIR)) {
805 close_file(fsp,False);
806 END_PROFILE(SMBntcreateX);
807 return ERROR_DOS(ERRDOS,ERRnoaccess);
811 * If the caller set the extended oplock request bit
812 * and we granted one (by whatever means) - set the
813 * correct bit for extended oplock reply.
816 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
817 smb_action |= EXTENDED_OPLOCK_GRANTED;
819 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
820 smb_action |= EXTENDED_OPLOCK_GRANTED;
823 /* W2K sends back 42 words here ! */
824 set_message(outbuf,42,0,True);
826 set_message(outbuf,34,0,True);
829 p = outbuf + smb_vwv2;
832 * Currently as we don't support level II oplocks we just report
833 * exclusive & batch here.
836 if (smb_action & EXTENDED_OPLOCK_GRANTED)
837 SCVAL(p,0, BATCH_OPLOCK_RETURN);
838 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
839 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
841 SCVAL(p,0,NO_OPLOCK_RETURN);
844 SSVAL(p,0,fsp->fnum);
846 SIVAL(p,0,smb_action);
850 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
852 if (lp_dos_filetime_resolution(SNUM(conn))) {
859 put_long_date(p,c_time);
861 put_long_date(p,sbuf.st_atime); /* access time */
863 put_long_date(p,sbuf.st_mtime); /* write time */
865 put_long_date(p,sbuf.st_mtime); /* change time */
867 SIVAL(p,0,fmode); /* File Attributes. */
869 SOFF_T(p, 0, file_len);
871 SOFF_T(p,0,file_len);
873 SCVAL(p,0,fsp->is_directory ? 1 : 0);
875 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
877 result = chain_reply(inbuf,outbuf,length,bufsize);
878 END_PROFILE(SMBntcreateX);
882 /****************************************************************************
883 Reply to a NT_TRANSACT_CREATE call to open a pipe.
884 ****************************************************************************/
886 static int do_nt_transact_create_pipe( connection_struct *conn,
887 char *inbuf, char *outbuf, int length,
888 int bufsize, char **ppsetup, char **ppparams,
892 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
893 char *params = *ppparams;
899 * Ensure minimum number of parameters sent.
902 if(total_parameter_count < 54) {
903 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
904 return ERROR_DOS(ERRDOS,ERRbadaccess);
907 srvstr_pull(inbuf, fname, params+53, sizeof(fname), -1, STR_TERMINATE);
909 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
912 /* Realloc the size of parameters and data we will return */
913 params = Realloc(*ppparams, 69);
915 return ERROR_DOS(ERRDOS,ERRnomem);
919 memset((char *)params,'\0',69);
922 SCVAL(p,0,NO_OPLOCK_RETURN);
927 SIVAL(p,0,FILE_WAS_OPENED);
931 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
934 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
936 SSVAL(p,2, 0x5FF); /* ? */
938 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
940 /* Send the required number of replies */
941 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
946 /****************************************************************************
947 Internal fn to set security descriptors.
948 ****************************************************************************/
950 static BOOL set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent, int *pdef_class,uint32 *pdef_code)
953 SEC_DESC *psd = NULL;
958 *pdef_class = ERRDOS;
959 *pdef_code = ERRbadaccess;
964 * Init the parse struct we will unmarshall from.
967 if ((mem_ctx = talloc_init()) == NULL) {
968 DEBUG(0,("set_sd: talloc_init failed.\n"));
969 *pdef_class = ERRDOS;
970 *pdef_code = ERRnomem;
974 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
977 * Setup the prs_struct to point at the memory we just
981 prs_give_memory( &pd, data, sd_len, False);
984 * Finally, unmarshall from the data buffer.
987 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
988 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
990 * Return access denied for want of a better error message..
992 talloc_destroy(mem_ctx);
993 *pdef_class = ERRDOS;
994 *pdef_code = ERRnomem;
998 if (psd->off_owner_sid==0)
999 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1000 if (psd->off_grp_sid==0)
1001 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1002 if (psd->off_sacl==0)
1003 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1004 if (psd->off_dacl==0)
1005 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1007 ret = fsp->conn->vfs_ops.fset_nt_acl( fsp, fsp->fd, security_info_sent, psd);
1010 talloc_destroy(mem_ctx);
1011 *pdef_class = ERRDOS;
1012 *pdef_code = ERRnoaccess;
1016 talloc_destroy(mem_ctx);
1023 /****************************************************************************
1024 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1025 ****************************************************************************/
1027 static int call_nt_transact_create(connection_struct *conn,
1028 char *inbuf, char *outbuf, int length,
1029 int bufsize, char **ppsetup, char **ppparams,
1033 char *params = *ppparams;
1034 char *data = *ppdata;
1035 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1036 /* Breakout the oplock request bits so we can set the
1037 reply bits separately. */
1038 int oplock_request = 0;
1040 int fmode=0,rmode=0;
1041 SMB_OFF_T file_len = 0;
1042 SMB_STRUCT_STAT sbuf;
1044 BOOL bad_path = False;
1045 files_struct *fsp = NULL;
1047 BOOL stat_open_only = False;
1049 uint32 desired_access;
1050 uint32 file_attributes;
1051 uint32 share_access;
1052 uint32 create_disposition;
1053 uint32 create_options;
1055 uint16 root_dir_fid;
1063 DEBUG(5,("call_nt_transact_create\n"));
1066 * If it's an IPC, use the pipe handler.
1070 if (lp_nt_pipe_support())
1071 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1072 bufsize, ppsetup, ppparams, ppdata);
1074 return ERROR_DOS(ERRDOS,ERRbadaccess);
1078 * Ensure minimum number of parameters sent.
1081 if(total_parameter_count < 54) {
1082 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1083 return ERROR_DOS(ERRDOS,ERRbadaccess);
1086 flags = IVAL(params,0);
1087 desired_access = IVAL(params,8);
1088 file_attributes = IVAL(params,20);
1089 share_access = IVAL(params,24);
1090 create_disposition = IVAL(params,28);
1091 create_options = IVAL(params,32);
1092 sd_len = IVAL(params,36);
1093 root_dir_fid = (uint16)IVAL(params,4);
1094 smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1097 * We need to construct the open_and_X ofun value from the
1098 * NT values, as that's what our code is structured to accept.
1101 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1102 return ERROR_DOS(ERRDOS,ERRbadmem);
1105 * Get the file name.
1108 if(root_dir_fid != 0) {
1110 * This filename is relative to a directory fid.
1113 files_struct *dir_fsp = file_fsp(params,4);
1114 size_t dir_name_len;
1117 return ERROR_DOS(ERRDOS,ERRbadfid);
1119 if(!dir_fsp->is_directory) {
1121 * Check to see if this is a mac fork of some kind.
1124 srvstr_pull(inbuf, fname, params+53, sizeof(fname), -1, STR_TERMINATE);
1126 if( strchr_m(fname, ':')) {
1127 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1130 return ERROR_DOS(ERRDOS,ERRbadfid);
1134 * Copy in the base directory name.
1137 pstrcpy( fname, dir_fsp->fsp_name );
1138 dir_name_len = strlen(fname);
1141 * Ensure it ends in a '\'.
1144 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1145 pstrcat(fname, "\\");
1149 srvstr_pull(inbuf, &fname[dir_name_len], params+53, sizeof(fname)-dir_name_len,
1152 srvstr_pull(inbuf, fname, params+53, sizeof(fname), -1, STR_TERMINATE);
1156 * Now contruct the smb_open_mode value from the desired access
1157 * and the share access.
1160 if((smb_open_mode = map_share_mode( &stat_open_only, fname, desired_access,
1161 share_access, file_attributes)) == -1)
1162 return ERROR_DOS(ERRDOS,ERRbadaccess);
1164 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1165 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1168 * Check if POSIX semantics are wanted.
1171 set_posix_case_semantics(file_attributes);
1173 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1175 unix_convert(fname,conn,0,&bad_path,&sbuf);
1177 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1180 * If it's a request for a directory open, deal with it separately.
1183 if(create_options & FILE_DIRECTORY_FILE) {
1188 * We will get a create directory here if the Win32
1189 * app specified a security descriptor in the
1190 * CreateDirectory() call.
1193 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
1196 restore_case_semantics(file_attributes);
1197 if((errno == ENOENT) && bad_path) {
1198 unix_ERR_class = ERRDOS;
1199 unix_ERR_code = ERRbadpath;
1201 return(UNIXERROR(ERRDOS,ERRnoaccess));
1207 * Ordinary file case.
1210 fsp = open_file_shared(conn,fname,&sbuf,smb_open_mode,smb_ofun,unixmode,
1211 oplock_request,&rmode,&smb_action);
1215 if(errno == EISDIR) {
1218 * Fail the open if it was explicitly a non-directory file.
1221 if (create_options & FILE_NON_DIRECTORY_FILE) {
1222 restore_case_semantics(file_attributes);
1223 SSVAL(outbuf, smb_flg2,
1224 SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1225 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1229 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
1232 restore_case_semantics(file_attributes);
1233 if((errno == ENOENT) && bad_path) {
1234 unix_ERR_class = ERRDOS;
1235 unix_ERR_code = ERRbadpath;
1237 return(UNIXERROR(ERRDOS,ERRnoaccess));
1240 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
1242 } else if (errno == EACCES && stat_open_only) {
1246 * We couldn't open normally and all we want
1247 * are the permissions. Try and do a stat open.
1252 fsp = open_file_stat(conn,fname,&sbuf,smb_open_mode,&smb_action);
1255 restore_case_semantics(file_attributes);
1256 return(UNIXERROR(ERRDOS,ERRnoaccess));
1260 if((errno == ENOENT) && bad_path) {
1261 unix_ERR_class = ERRDOS;
1262 unix_ERR_code = ERRbadpath;
1265 restore_case_semantics(file_attributes);
1267 return(UNIXERROR(ERRDOS,ERRnoaccess));
1271 file_len = sbuf.st_size;
1272 fmode = dos_mode(conn,fname,&sbuf);
1274 fmode = FILE_ATTRIBUTE_NORMAL;
1277 close_file(fsp,False);
1278 restore_case_semantics(file_attributes);
1279 return ERROR_DOS(ERRDOS,ERRnoaccess);
1283 * If the caller set the extended oplock request bit
1284 * and we granted one (by whatever means) - set the
1285 * correct bit for extended oplock reply.
1288 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1289 smb_action |= EXTENDED_OPLOCK_GRANTED;
1291 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1292 smb_action |= EXTENDED_OPLOCK_GRANTED;
1296 * Now try and apply the desired SD.
1299 if (!set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION, &error_class, &error_code)) {
1300 close_file(fsp,False);
1301 restore_case_semantics(file_attributes);
1302 return ERROR_DOS(error_class, error_code);
1305 restore_case_semantics(file_attributes);
1307 /* Realloc the size of parameters and data we will return */
1308 params = Realloc(*ppparams, 69);
1310 return ERROR_DOS(ERRDOS,ERRnomem);
1314 memset((char *)params,'\0',69);
1317 if (smb_action & EXTENDED_OPLOCK_GRANTED)
1318 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1319 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1320 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1322 SCVAL(p,0,NO_OPLOCK_RETURN);
1325 SSVAL(p,0,fsp->fnum);
1327 SIVAL(p,0,smb_action);
1331 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1333 if (lp_dos_filetime_resolution(SNUM(conn))) {
1335 sbuf.st_atime &= ~1;
1336 sbuf.st_mtime &= ~1;
1337 sbuf.st_mtime &= ~1;
1340 put_long_date(p,c_time);
1342 put_long_date(p,sbuf.st_atime); /* access time */
1344 put_long_date(p,sbuf.st_mtime); /* write time */
1346 put_long_date(p,sbuf.st_mtime); /* change time */
1348 SIVAL(p,0,fmode); /* File Attributes. */
1350 SOFF_T(p,0,file_len);
1352 SOFF_T(p,0,file_len);
1354 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1356 /* Send the required number of replies */
1357 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1362 /****************************************************************************
1363 Reply to a NT CANCEL request.
1364 ****************************************************************************/
1365 int reply_ntcancel(connection_struct *conn,
1366 char *inbuf,char *outbuf,int length,int bufsize)
1369 * Go through and cancel any pending change notifies.
1372 int mid = SVAL(inbuf,smb_mid);
1373 START_PROFILE(SMBntcancel);
1374 remove_pending_change_notify_requests_by_mid(mid);
1375 remove_pending_lock_requests_by_mid(mid);
1377 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1379 END_PROFILE(SMBntcancel);
1383 /****************************************************************************
1384 Reply to an unsolicited SMBNTtranss - just ignore it!
1385 ****************************************************************************/
1386 int reply_nttranss(connection_struct *conn,
1387 char *inbuf,char *outbuf,int length,int bufsize)
1389 START_PROFILE(SMBnttranss);
1390 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1391 END_PROFILE(SMBnttranss);
1395 /****************************************************************************
1396 Reply to a notify change - queue the request and
1397 don't allow a directory to be opened.
1398 ****************************************************************************/
1399 static int call_nt_transact_notify_change(connection_struct *conn,
1400 char *inbuf, char *outbuf, int length,
1403 char **ppparams, char **ppdata)
1405 char *setup = *ppsetup;
1409 fsp = file_fsp(setup,4);
1410 flags = IVAL(setup, 0);
1412 DEBUG(3,("call_nt_transact_notify_change\n"));
1415 return ERROR_DOS(ERRDOS,ERRbadfid);
1417 if((!fsp->is_directory) || (conn != fsp->conn))
1418 return ERROR_DOS(ERRDOS,ERRbadfid);
1420 if (!change_notify_set(inbuf, fsp, conn, flags)) {
1421 return(UNIXERROR(ERRDOS,ERRbadfid));
1424 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1425 name = %s\n", fsp->fsp_name ));
1430 /****************************************************************************
1431 Reply to an NT transact rename command.
1432 ****************************************************************************/
1434 static int call_nt_transact_rename(connection_struct *conn,
1435 char *inbuf, char *outbuf, int length,
1437 char **ppsetup, char **ppparams, char **ppdata)
1439 char *params = *ppparams;
1441 files_struct *fsp = file_fsp(params, 0);
1442 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1445 CHECK_FSP(fsp, conn);
1446 srvstr_pull(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE);
1448 status = rename_internals(conn, fsp->fsp_name,
1449 new_name, replace_if_exists);
1450 if (!NT_STATUS_IS_OK(status)) return ERROR_NT(status);
1453 * Rename was successful.
1455 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1457 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1458 fsp->fsp_name, new_name));
1461 * Win2k needs a changenotify request response before it will
1462 * update after a rename..
1465 process_pending_change_notify_queue((time_t)0);
1470 /******************************************************************************
1471 Fake up a completely empty SD.
1472 *******************************************************************************/
1474 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1476 extern DOM_SID global_sid_World;
1479 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1481 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1488 /****************************************************************************
1489 Reply to query a security descriptor - currently this is not implemented (it
1490 is planned to be though). Right now it just returns the same thing NT would
1491 when queried on a FAT filesystem. JRA.
1492 ****************************************************************************/
1494 static int call_nt_transact_query_security_desc(connection_struct *conn,
1495 char *inbuf, char *outbuf,
1496 int length, int bufsize,
1497 char **ppsetup, char **ppparams, char **ppdata)
1499 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1500 char *params = *ppparams;
1501 char *data = *ppdata;
1503 SEC_DESC *psd = NULL;
1505 TALLOC_CTX *mem_ctx;
1507 files_struct *fsp = file_fsp(params,0);
1510 return ERROR_DOS(ERRDOS,ERRbadfid);
1512 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1514 params = Realloc(*ppparams, 4);
1516 return ERROR_DOS(ERRDOS,ERRnomem);
1520 if ((mem_ctx = talloc_init()) == NULL) {
1521 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1522 return ERROR_DOS(ERRDOS,ERRnomem);
1526 * Get the permissions to return.
1529 if (!lp_nt_acl_support(SNUM(conn)))
1530 sd_size = get_null_nt_acl(mem_ctx, &psd);
1532 sd_size = conn->vfs_ops.fget_nt_acl(fsp, fsp->fd, &psd);
1535 talloc_destroy(mem_ctx);
1536 return(UNIXERROR(ERRDOS,ERRnoaccess));
1539 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1541 SIVAL(params,0,(uint32)sd_size);
1543 if(max_data_count < sd_size) {
1545 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1546 params, 4, *ppdata, 0);
1547 talloc_destroy(mem_ctx);
1552 * Allocate the data we will point this at.
1555 data = Realloc(*ppdata, sd_size);
1557 talloc_destroy(mem_ctx);
1558 return ERROR_DOS(ERRDOS,ERRnomem);
1563 memset(data, '\0', sd_size);
1566 * Init the parse struct we will marshall into.
1569 prs_init(&pd, 0, mem_ctx, MARSHALL);
1572 * Setup the prs_struct to point at the memory we just
1576 prs_give_memory( &pd, data, (uint32)sd_size, False);
1579 * Finally, linearize into the outgoing buffer.
1582 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1583 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1584 security descriptor.\n"));
1586 * Return access denied for want of a better error message..
1588 talloc_destroy(mem_ctx);
1589 return(UNIXERROR(ERRDOS,ERRnoaccess));
1593 * Now we can delete the security descriptor.
1596 talloc_destroy(mem_ctx);
1598 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
1602 /****************************************************************************
1603 Reply to set a security descriptor. Map to UNIX perms.
1604 ****************************************************************************/
1606 static int call_nt_transact_set_security_desc(connection_struct *conn,
1607 char *inbuf, char *outbuf, int length,
1608 int bufsize, char **ppsetup,
1609 char **ppparams, char **ppdata)
1611 uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1612 char *params= *ppparams;
1613 char *data = *ppdata;
1614 uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
1615 files_struct *fsp = NULL;
1616 uint32 security_info_sent = 0;
1620 if(total_parameter_count < 8)
1621 return ERROR_DOS(ERRDOS,ERRbadfunc);
1623 if((fsp = file_fsp(params,0)) == NULL)
1624 return ERROR_DOS(ERRDOS,ERRbadfid);
1626 if(!lp_nt_acl_support(SNUM(conn)))
1629 security_info_sent = IVAL(params,4);
1631 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1632 (unsigned int)security_info_sent ));
1634 if (!set_sd( fsp, data, total_data_count, security_info_sent, &error_class, &error_code))
1635 return ERROR_DOS(error_class, error_code);
1639 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1643 /****************************************************************************
1644 Reply to IOCTL - not implemented - no plans.
1645 ****************************************************************************/
1646 static int call_nt_transact_ioctl(connection_struct *conn,
1647 char *inbuf, char *outbuf, int length,
1649 char **ppsetup, char **ppparams, char **ppdata)
1651 static BOOL logged_message = False;
1653 if(!logged_message) {
1654 DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1655 logged_message = True; /* Only print this once... */
1657 return ERROR_DOS(ERRSRV,ERRnosupport);
1660 /****************************************************************************
1661 Reply to a SMBNTtrans.
1662 ****************************************************************************/
1663 int reply_nttrans(connection_struct *conn,
1664 char *inbuf,char *outbuf,int length,int bufsize)
1667 #if 0 /* Not used. */
1668 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1669 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1670 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1671 #endif /* Not used. */
1672 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1673 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1674 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1675 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1676 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1677 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1678 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1679 uint16 function_code = SVAL( inbuf, smb_nt_Function);
1680 char *params = NULL, *data = NULL, *setup = NULL;
1681 uint32 num_params_sofar, num_data_sofar;
1682 START_PROFILE(SMBnttrans);
1684 if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1686 * Queue this open message as we are the process of an oplock break.
1689 DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1690 due to being in oplock break state.\n" ));
1692 push_oplock_pending_smb_message( inbuf, length);
1693 END_PROFILE(SMBnttrans);
1697 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
1698 END_PROFILE(SMBnttrans);
1699 return ERROR_DOS(ERRSRV,ERRaccess);
1702 outsize = set_message(outbuf,0,0,True);
1705 * All nttrans messages we handle have smb_wct == 19 + setup_count.
1706 * Ensure this is so as a sanity check.
1709 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1710 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1711 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1712 END_PROFILE(SMBnttrans);
1713 return ERROR_DOS(ERRSRV,ERRerror);
1716 /* Allocate the space for the setup, the maximum needed parameters and data */
1719 setup = (char *)malloc(setup_count);
1720 if (total_parameter_count > 0)
1721 params = (char *)malloc(total_parameter_count);
1722 if (total_data_count > 0)
1723 data = (char *)malloc(total_data_count);
1725 if ((total_parameter_count && !params) || (total_data_count && !data) ||
1726 (setup_count && !setup)) {
1730 DEBUG(0,("reply_nttrans : Out of memory\n"));
1731 END_PROFILE(SMBnttrans);
1732 return ERROR_DOS(ERRDOS,ERRnomem);
1735 /* Copy the param and data bytes sent with this request into
1736 the params buffer */
1737 num_params_sofar = parameter_count;
1738 num_data_sofar = data_count;
1740 if (parameter_count > total_parameter_count || data_count > total_data_count)
1741 exit_server("reply_nttrans: invalid sizes in packet.");
1744 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1745 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1746 dump_data(10, setup, setup_count);
1749 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1750 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1751 dump_data(10, params, parameter_count);
1754 memcpy( data, smb_base(inbuf) + data_offset, data_count);
1755 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1756 dump_data(10, data, data_count);
1759 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1760 /* We need to send an interim response then receive the rest
1761 of the parameter/data bytes */
1762 outsize = set_message(outbuf,0,0,True);
1763 if (!send_smb(smbd_server_fd(),outbuf))
1764 exit_server("reply_nttrans: send_smb failed.");
1766 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1769 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
1771 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1772 outsize = set_message(outbuf,0,0,True);
1774 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1776 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1777 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1782 END_PROFILE(SMBnttrans);
1783 return ERROR_DOS(ERRSRV,ERRerror);
1786 /* Revise total_params and total_data in case they have changed downwards */
1787 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1788 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1789 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1790 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1791 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1792 exit_server("reply_nttrans2: data overflow in secondary nttrans packet");
1794 memcpy( ¶ms[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
1795 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1796 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1797 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1801 if (Protocol >= PROTOCOL_NT1) {
1802 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | 0x40); /* IS_LONG_NAME */
1805 /* Now we must call the relevant NT_TRANS function */
1806 switch(function_code) {
1807 case NT_TRANSACT_CREATE:
1808 START_PROFILE_NESTED(NT_transact_create);
1809 outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize,
1810 &setup, ¶ms, &data);
1811 END_PROFILE_NESTED(NT_transact_create);
1813 case NT_TRANSACT_IOCTL:
1814 START_PROFILE_NESTED(NT_transact_ioctl);
1815 outsize = call_nt_transact_ioctl(conn,
1816 inbuf, outbuf, length, bufsize,
1817 &setup, ¶ms, &data);
1818 END_PROFILE_NESTED(NT_transact_ioctl);
1820 case NT_TRANSACT_SET_SECURITY_DESC:
1821 START_PROFILE_NESTED(NT_transact_set_security_desc);
1822 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
1824 &setup, ¶ms, &data);
1825 END_PROFILE_NESTED(NT_transact_set_security_desc);
1827 case NT_TRANSACT_NOTIFY_CHANGE:
1828 START_PROFILE_NESTED(NT_transact_notify_change);
1829 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
1831 &setup, ¶ms, &data);
1832 END_PROFILE_NESTED(NT_transact_notify_change);
1834 case NT_TRANSACT_RENAME:
1835 START_PROFILE_NESTED(NT_transact_rename);
1836 outsize = call_nt_transact_rename(conn, inbuf, outbuf, length,
1838 &setup, ¶ms, &data);
1839 END_PROFILE_NESTED(NT_transact_rename);
1842 case NT_TRANSACT_QUERY_SECURITY_DESC:
1843 START_PROFILE_NESTED(NT_transact_query_security_desc);
1844 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
1846 &setup, ¶ms, &data);
1847 END_PROFILE_NESTED(NT_transact_query_security_desc);
1850 /* Error in request */
1851 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1855 END_PROFILE(SMBnttrans);
1856 return ERROR_DOS(ERRSRV,ERRerror);
1859 /* As we do not know how many data packets will need to be
1860 returned here the various call_nt_transact_xxxx calls
1861 must send their own. Thus a call_nt_transact_xxxx routine only
1862 returns a value other than -1 when it wants to send
1869 END_PROFILE(SMBnttrans);
1870 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
1871 calls have already sent it. If outsize != -1 then it is
1872 returning an error packet. */