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 /* Map generic permissions to file object specific permissions */
52 struct generic_mapping file_generic_mapping = {
59 /****************************************************************************
60 Send the required number of replies back.
61 We assume all fields other than the data fields are
62 set correctly for the type of call.
63 HACK ! Always assumes smb_setup field is zero.
64 ****************************************************************************/
66 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, NTSTATUS nt_error, char *params,
67 int paramsize, char *pdata, int datasize)
70 int data_to_send = datasize;
71 int params_to_send = paramsize;
75 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
76 int alignment_offset = 3;
77 int data_alignment_offset = 0;
80 * Initially set the wcnt area to be 18 - this is true for all
84 set_message(outbuf,18,0,True);
86 if (NT_STATUS_V(nt_error)) {
91 * If there genuinely are no parameters or data to send just send
95 if(params_to_send == 0 && data_to_send == 0) {
96 if (!send_smb(smbd_server_fd(),outbuf))
97 exit_server("send_nt_replies: send_smb failed.\n");
102 * When sending params and data ensure that both are nicely aligned.
103 * Only do this alignment when there is also data to send - else
104 * can cause NT redirector problems.
107 if (((params_to_send % 4) != 0) && (data_to_send != 0))
108 data_alignment_offset = 4 - (params_to_send % 4);
111 * Space is bufsize minus Netbios over TCP header minus SMB header.
112 * The alignment_offset is to align the param bytes on a four byte
113 * boundary (2 bytes for data len, one byte pad).
114 * NT needs this to work correctly.
117 useable_space = bufsize - ((smb_buf(outbuf)+
118 alignment_offset+data_alignment_offset) -
122 * useable_space can never be more than max_send minus the
126 useable_space = MIN(useable_space,
127 max_send - (alignment_offset+data_alignment_offset));
130 while (params_to_send || data_to_send) {
133 * Calculate whether we will totally or partially fill this packet.
136 total_sent_thistime = params_to_send + data_to_send +
137 alignment_offset + data_alignment_offset;
140 * We can never send more than useable_space.
143 total_sent_thistime = MIN(total_sent_thistime, useable_space);
145 set_message(outbuf, 18, total_sent_thistime, True);
148 * Set total params and data to be sent.
151 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
152 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
155 * Calculate how many parameters and data we can fit into
156 * this packet. Parameters get precedence.
159 params_sent_thistime = MIN(params_to_send,useable_space);
160 data_sent_thistime = useable_space - params_sent_thistime;
161 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
163 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
165 if(params_sent_thistime == 0) {
166 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
167 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
170 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
171 * parameter bytes, however the first 4 bytes of outbuf are
172 * the Netbios over TCP header. Thus use smb_base() to subtract
173 * them from the calculation.
176 SIVAL(outbuf,smb_ntr_ParameterOffset,
177 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
179 * Absolute displacement of param bytes sent in this packet.
182 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
186 * Deal with the data portion.
189 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
191 if(data_sent_thistime == 0) {
192 SIVAL(outbuf,smb_ntr_DataOffset,0);
193 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
196 * The offset of the data bytes is the offset of the
197 * parameter bytes plus the number of parameters being sent this time.
200 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
201 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
202 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
206 * Copy the param bytes into the packet.
209 if(params_sent_thistime)
210 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
213 * Copy in the data bytes
216 if(data_sent_thistime)
217 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
218 data_alignment_offset,pd,data_sent_thistime);
220 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
221 params_sent_thistime, data_sent_thistime, useable_space));
222 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
223 params_to_send, data_to_send, paramsize, datasize));
225 /* Send the packet */
226 if (!send_smb(smbd_server_fd(),outbuf))
227 exit_server("send_nt_replies: send_smb failed.\n");
229 pp += params_sent_thistime;
230 pd += data_sent_thistime;
232 params_to_send -= params_sent_thistime;
233 data_to_send -= data_sent_thistime;
239 if(params_to_send < 0 || data_to_send < 0) {
240 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
241 params_to_send, data_to_send));
249 /****************************************************************************
251 ****************************************************************************/
253 static BOOL saved_case_sensitive;
254 static BOOL saved_case_preserve;
255 static BOOL saved_short_case_preserve;
257 /****************************************************************************
259 ****************************************************************************/
261 static void set_posix_case_semantics(uint32 file_attributes)
263 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
266 saved_case_sensitive = case_sensitive;
267 saved_case_preserve = case_preserve;
268 saved_short_case_preserve = short_case_preserve;
271 case_sensitive = True;
272 case_preserve = True;
273 short_case_preserve = True;
276 /****************************************************************************
277 Restore case semantics.
278 ****************************************************************************/
280 static void restore_case_semantics(uint32 file_attributes)
282 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
285 case_sensitive = saved_case_sensitive;
286 case_preserve = saved_case_preserve;
287 short_case_preserve = saved_short_case_preserve;
290 /****************************************************************************
291 Utility function to map create disposition.
292 ****************************************************************************/
294 static int map_create_disposition( uint32 create_disposition)
298 switch( create_disposition ) {
300 /* create if not exist, fail if exist */
301 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
304 case FILE_OVERWRITE_IF:
305 /* create if not exist, trunc if exist */
306 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
309 /* fail if not exist, open if exists */
310 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
313 /* create if not exist, open if exists */
314 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
317 /* fail if not exist, truncate if exists */
318 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
321 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
322 create_disposition ));
326 DEBUG(10,("map_create_disposition: Mapped create_disposition %lx to %x\n",
327 (unsigned long)create_disposition, ret ));
332 /****************************************************************************
333 Utility function to map share modes.
334 ****************************************************************************/
336 static int map_share_mode( BOOL *pstat_open_only, char *fname,
337 uint32 desired_access, uint32 share_access, uint32 file_attributes)
339 int smb_open_mode = -1;
341 *pstat_open_only = False;
344 * Convert GENERIC bits to specific bits.
347 se_map_generic(&desired_access, &file_generic_mapping);
349 switch( desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
351 smb_open_mode = DOS_OPEN_RDONLY;
353 case FILE_WRITE_DATA:
354 case FILE_APPEND_DATA:
355 case FILE_WRITE_DATA|FILE_APPEND_DATA:
356 smb_open_mode = DOS_OPEN_WRONLY;
358 case FILE_READ_DATA|FILE_WRITE_DATA:
359 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
360 case FILE_READ_DATA|FILE_APPEND_DATA:
361 smb_open_mode = DOS_OPEN_RDWR;
366 * NB. For DELETE_ACCESS we should really check the
367 * directory permissions, as that is what controls
368 * delete, and for WRITE_DAC_ACCESS we should really
369 * check the ownership, as that is what controls the
370 * chmod. Note that this is *NOT* a security hole (this
371 * note is for you, Andrew) as we are not *allowing*
372 * the access at this point, the actual unlink or
373 * chown or chmod call would do this. We are just helping
374 * clients out by telling them if they have a hope
375 * of any of this succeeding. POSIX acls may still
376 * deny the real call. JRA.
379 if (smb_open_mode == -1) {
381 if(desired_access == WRITE_DAC_ACCESS || desired_access == READ_CONTROL_ACCESS)
382 *pstat_open_only = True;
384 if(desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|
385 FILE_EXECUTE|FILE_READ_ATTRIBUTES|
386 FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
387 FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
388 smb_open_mode = DOS_OPEN_RDONLY;
389 } else if(desired_access == 0) {
392 * JRA - NT seems to sometimes send desired_access as zero. play it safe
393 * and map to a stat open.
396 *pstat_open_only = True;
397 smb_open_mode = DOS_OPEN_RDONLY;
400 DEBUG(0,("map_share_mode: Incorrect value %lx for desired_access to file %s\n",
401 (unsigned long)desired_access, fname));
407 * Set the special bit that means allow share delete.
408 * This is held outside the normal share mode bits at 1<<15.
412 if(share_access & FILE_SHARE_DELETE) {
413 smb_open_mode |= ALLOW_SHARE_DELETE;
414 DEBUG(10,("map_share_mode: FILE_SHARE_DELETE requested. open_mode = %x\n", smb_open_mode));
418 * We need to store the intent to open for Delete. This
419 * is what determines if a delete on close flag can be set.
420 * This is the wrong way (and place) to store this, but for 2.2 this
421 * is the only practical way. JRA.
424 if(desired_access & DELETE_ACCESS) {
425 smb_open_mode |= DELETE_ACCESS_REQUESTED;
426 DEBUG(10,("map_share_mode: DELETE_ACCESS requested. open_mode = %x\n", smb_open_mode));
429 /* Add in the requested share mode. */
430 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
431 case FILE_SHARE_READ:
432 smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
434 case FILE_SHARE_WRITE:
435 smb_open_mode |= SET_DENY_MODE(DENY_READ);
437 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
438 smb_open_mode |= SET_DENY_MODE(DENY_NONE);
440 case FILE_SHARE_NONE:
441 smb_open_mode |= SET_DENY_MODE(DENY_ALL);
446 * Handle an O_SYNC request.
449 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
450 smb_open_mode |= FILE_SYNC_OPENMODE;
452 DEBUG(10,("map_share_mode: Mapped desired access %lx, share access %lx, file attributes %lx \
453 to open_mode %x\n", (unsigned long)desired_access, (unsigned long)share_access,
454 (unsigned long)file_attributes, smb_open_mode ));
456 return smb_open_mode;
459 /****************************************************************************
460 Reply to an NT create and X call on a pipe.
461 ****************************************************************************/
462 static int nt_open_pipe(char *fname, connection_struct *conn,
463 char *inbuf, char *outbuf, int *ppnum)
465 pipes_struct *p = NULL;
467 uint16 vuid = SVAL(inbuf, smb_uid);
470 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
472 /* See if it is one we want to handle. */
474 if (lp_disable_spoolss() && strequal(fname, "\\spoolss"))
475 return(ERROR_DOS(ERRSRV,ERRaccess));
477 for( i = 0; known_nt_pipes[i]; i++ )
478 if( strequal(fname,known_nt_pipes[i]))
481 if ( known_nt_pipes[i] == NULL )
482 return(ERROR_DOS(ERRSRV,ERRaccess));
484 /* Strip \\ off the name. */
487 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
489 p = open_rpc_pipe_p(fname, conn, vuid);
491 return(ERROR_DOS(ERRSRV,ERRnofids));
498 /****************************************************************************
499 Reply to an NT create and X call for pipes.
500 ****************************************************************************/
502 static int do_ntcreate_pipe_open(connection_struct *conn,
503 char *inbuf,char *outbuf,int length,int bufsize)
510 srvstr_pull(inbuf, fname, smb_buf(inbuf), sizeof(fname), -1, STR_TERMINATE);
512 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
516 * Deal with pipe return.
519 set_message(outbuf,34,0,True);
521 p = outbuf + smb_vwv2;
525 SIVAL(p,0,FILE_WAS_OPENED);
528 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
531 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
533 SSVAL(p,2, 0x5FF); /* ? */
535 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
537 return chain_reply(inbuf,outbuf,length,bufsize);
540 /****************************************************************************
541 Reply to an NT create and X call.
542 ****************************************************************************/
544 int reply_ntcreate_and_X(connection_struct *conn,
545 char *inbuf,char *outbuf,int length,int bufsize)
549 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
550 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
551 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
552 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
553 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
554 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
555 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
558 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
559 /* Breakout the oplock request bits so we can set the
560 reply bits separately. */
561 int oplock_request = 0;
564 SMB_OFF_T file_len = 0;
565 SMB_STRUCT_STAT sbuf;
567 BOOL bad_path = False;
568 files_struct *fsp=NULL;
570 BOOL stat_open_only = False;
572 START_PROFILE(SMBntcreateX);
574 /* If it's an IPC, use the pipe handler. */
577 if (lp_nt_pipe_support()) {
578 END_PROFILE(SMBntcreateX);
579 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
581 END_PROFILE(SMBntcreateX);
582 return(ERROR_DOS(ERRDOS,ERRbadaccess));
588 * We need to construct the open_and_X ofun value from the
589 * NT values, as that's what our code is structured to accept.
592 if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
593 END_PROFILE(SMBntcreateX);
594 return(ERROR_DOS(ERRDOS,ERRbadaccess));
601 if(root_dir_fid != 0) {
603 * This filename is relative to a directory fid.
605 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
609 END_PROFILE(SMBntcreateX);
610 return(ERROR_DOS(ERRDOS,ERRbadfid));
613 if(!dir_fsp->is_directory) {
615 * Check to see if this is a mac fork of some kind.
618 srvstr_pull(inbuf, fname, smb_buf(inbuf), sizeof(fname), -1, STR_TERMINATE);
620 if( strchr_m(fname, ':')) {
621 END_PROFILE(SMBntcreateX);
622 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
624 END_PROFILE(SMBntcreateX);
625 return(ERROR_DOS(ERRDOS,ERRbadfid));
629 * Copy in the base directory name.
632 pstrcpy( fname, dir_fsp->fsp_name );
633 dir_name_len = strlen(fname);
636 * Ensure it ends in a '\'.
639 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
640 pstrcat(fname, "\\");
644 srvstr_pull(inbuf, &fname[dir_name_len], smb_buf(inbuf), sizeof(fname)-dir_name_len,
647 srvstr_pull(inbuf, fname, smb_buf(inbuf), sizeof(fname),
652 * Now contruct the smb_open_mode value from the filename,
653 * desired access and the share access.
655 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
657 if((smb_open_mode = map_share_mode(&stat_open_only, fname, desired_access,
659 file_attributes)) == -1) {
660 END_PROFILE(SMBntcreateX);
661 return ERROR_DOS(ERRDOS,ERRbadaccess);
664 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
665 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
668 * Ordinary file or directory.
672 * Check if POSIX semantics are wanted.
675 set_posix_case_semantics(file_attributes);
677 unix_convert(fname,conn,0,&bad_path,&sbuf);
679 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
682 * If it's a request for a directory open, deal with it separately.
685 if(create_options & FILE_DIRECTORY_FILE) {
688 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
690 restore_case_semantics(file_attributes);
693 if((errno == ENOENT) && bad_path) {
694 unix_ERR_class = ERRDOS;
695 unix_ERR_code = ERRbadpath;
697 END_PROFILE(SMBntcreateX);
698 return(UNIXERROR(ERRDOS,ERRnoaccess));
702 * Ordinary file case.
705 /* NB. We have a potential bug here. If we
706 * cause an oplock break to ourselves, then we
707 * could end up processing filename related
708 * SMB requests whilst we await the oplock
709 * break response. As we may have changed the
710 * filename case semantics to be POSIX-like,
711 * this could mean a filename request could
712 * fail when it should succeed. This is a rare
713 * condition, but eventually we must arrange
714 * to restore the correct case semantics
715 * before issuing an oplock break request to
716 * our client. JRA. */
718 fsp = open_file_shared(conn,fname,&sbuf,smb_open_mode,
719 smb_ofun,unixmode, oplock_request,&rmode,&smb_action);
722 /* We cheat here. There are two cases we
723 * care about. One is a directory rename,
724 * where the NT client will attempt to
725 * open the source directory for
726 * DELETE access. Note that when the
727 * NT client does this it does *not*
728 * set the directory bit in the
729 * request packet. This is translated
730 * into a read/write open
731 * request. POSIX states that any open
732 * for write request on a directory
733 * will generate an EISDIR error, so
734 * we can catch this here and open a
735 * pseudo handle that is flagged as a
736 * directory. The second is an open
737 * for a permissions read only, which
738 * we handle in the open_file_stat case. JRA.
741 if(errno == EISDIR) {
744 * Fail the open if it was explicitly a non-directory file.
747 if (create_options & FILE_NON_DIRECTORY_FILE) {
748 restore_case_semantics(file_attributes);
749 SSVAL(outbuf, smb_flg2,
750 SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
751 END_PROFILE(SMBntcreateX);
752 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
756 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
759 restore_case_semantics(file_attributes);
760 if((errno == ENOENT) && bad_path) {
761 unix_ERR_class = ERRDOS;
762 unix_ERR_code = ERRbadpath;
764 END_PROFILE(SMBntcreateX);
765 return(UNIXERROR(ERRDOS,ERRnoaccess));
768 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
770 } else if (errno == EACCES && stat_open_only) {
773 * We couldn't open normally and all we want
774 * are the permissions. Try and do a stat open.
779 fsp = open_file_stat(conn,fname,&sbuf,smb_open_mode,&smb_action);
782 restore_case_semantics(file_attributes);
783 END_PROFILE(SMBntcreateX);
784 return(UNIXERROR(ERRDOS,ERRnoaccess));
789 if((errno == ENOENT) && bad_path) {
790 unix_ERR_class = ERRDOS;
791 unix_ERR_code = ERRbadpath;
794 restore_case_semantics(file_attributes);
796 END_PROFILE(SMBntcreateX);
797 return(UNIXERROR(ERRDOS,ERRnoaccess));
802 restore_case_semantics(file_attributes);
804 file_len = sbuf.st_size;
805 fmode = dos_mode(conn,fname,&sbuf);
807 fmode = FILE_ATTRIBUTE_NORMAL;
808 if (!fsp->is_directory && (fmode & aDIR)) {
809 close_file(fsp,False);
810 END_PROFILE(SMBntcreateX);
811 return ERROR_DOS(ERRDOS,ERRnoaccess);
815 * If the caller set the extended oplock request bit
816 * and we granted one (by whatever means) - set the
817 * correct bit for extended oplock reply.
820 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
821 smb_action |= EXTENDED_OPLOCK_GRANTED;
823 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
824 smb_action |= EXTENDED_OPLOCK_GRANTED;
826 set_message(outbuf,34,0,True);
828 p = outbuf + smb_vwv2;
831 * Currently as we don't support level II oplocks we just report
832 * exclusive & batch here.
835 if (smb_action & EXTENDED_OPLOCK_GRANTED)
836 SCVAL(p,0, BATCH_OPLOCK_RETURN);
837 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
838 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
840 SCVAL(p,0,NO_OPLOCK_RETURN);
843 SSVAL(p,0,fsp->fnum);
845 SIVAL(p,0,smb_action);
849 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
851 if (lp_dos_filetime_resolution(SNUM(conn))) {
858 put_long_date(p,c_time);
860 put_long_date(p,sbuf.st_atime); /* access time */
862 put_long_date(p,sbuf.st_mtime); /* write time */
864 put_long_date(p,sbuf.st_mtime); /* change time */
866 SIVAL(p,0,fmode); /* File Attributes. */
868 SOFF_T(p, 0, file_len);
870 SOFF_T(p,0,file_len);
872 SCVAL(p,0,fsp->is_directory ? 1 : 0);
874 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
876 result = chain_reply(inbuf,outbuf,length,bufsize);
877 END_PROFILE(SMBntcreateX);
881 /****************************************************************************
882 Reply to a NT_TRANSACT_CREATE call to open a pipe.
883 ****************************************************************************/
885 static int do_nt_transact_create_pipe( connection_struct *conn,
886 char *inbuf, char *outbuf, int length,
887 int bufsize, char **ppsetup, char **ppparams,
891 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
892 char *params = *ppparams;
898 * Ensure minimum number of parameters sent.
901 if(total_parameter_count < 54) {
902 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
903 return ERROR_DOS(ERRDOS,ERRbadaccess);
906 srvstr_pull(inbuf, fname, params+53, sizeof(fname), -1, STR_TERMINATE);
908 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
911 /* Realloc the size of parameters and data we will return */
912 params = Realloc(*ppparams, 69);
914 return ERROR_DOS(ERRDOS,ERRnomem);
918 memset((char *)params,'\0',69);
921 SCVAL(p,0,NO_OPLOCK_RETURN);
926 SIVAL(p,0,FILE_WAS_OPENED);
930 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
933 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
935 SSVAL(p,2, 0x5FF); /* ? */
937 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
939 /* Send the required number of replies */
940 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
945 /****************************************************************************
946 Internal fn to set security descriptors.
947 ****************************************************************************/
949 static BOOL set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent, int *pdef_class,uint32 *pdef_code)
952 SEC_DESC *psd = NULL;
957 *pdef_class = ERRDOS;
958 *pdef_code = ERRbadaccess;
963 * Init the parse struct we will unmarshall from.
966 if ((mem_ctx = talloc_init()) == NULL) {
967 DEBUG(0,("set_sd: talloc_init failed.\n"));
968 *pdef_class = ERRDOS;
969 *pdef_code = ERRnomem;
973 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
976 * Setup the prs_struct to point at the memory we just
980 prs_give_memory( &pd, data, sd_len, False);
983 * Finally, unmarshall from the data buffer.
986 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
987 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
989 * Return access denied for want of a better error message..
991 talloc_destroy(mem_ctx);
992 *pdef_class = ERRDOS;
993 *pdef_code = ERRnomem;
997 if (psd->off_owner_sid==0)
998 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
999 if (psd->off_grp_sid==0)
1000 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1001 if (psd->off_sacl==0)
1002 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1003 if (psd->off_dacl==0)
1004 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1006 ret = fsp->conn->vfs_ops.fset_nt_acl( fsp, fsp->fd, security_info_sent, psd);
1009 talloc_destroy(mem_ctx);
1010 *pdef_class = ERRDOS;
1011 *pdef_code = ERRnoaccess;
1015 talloc_destroy(mem_ctx);
1022 /****************************************************************************
1023 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1024 ****************************************************************************/
1026 static int call_nt_transact_create(connection_struct *conn,
1027 char *inbuf, char *outbuf, int length,
1028 int bufsize, char **ppsetup, char **ppparams,
1032 char *params = *ppparams;
1033 char *data = *ppdata;
1034 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1035 /* Breakout the oplock request bits so we can set the
1036 reply bits separately. */
1037 int oplock_request = 0;
1039 int fmode=0,rmode=0;
1040 SMB_OFF_T file_len = 0;
1041 SMB_STRUCT_STAT sbuf;
1043 BOOL bad_path = False;
1044 files_struct *fsp = NULL;
1046 BOOL stat_open_only = False;
1048 uint32 desired_access;
1049 uint32 file_attributes;
1050 uint32 share_access;
1051 uint32 create_disposition;
1052 uint32 create_options;
1054 uint16 root_dir_fid;
1062 DEBUG(5,("call_nt_transact_create\n"));
1065 * If it's an IPC, use the pipe handler.
1069 if (lp_nt_pipe_support())
1070 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1071 bufsize, ppsetup, ppparams, ppdata);
1073 return ERROR_DOS(ERRDOS,ERRbadaccess);
1077 * Ensure minimum number of parameters sent.
1080 if(total_parameter_count < 54) {
1081 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1082 return ERROR_DOS(ERRDOS,ERRbadaccess);
1085 flags = IVAL(params,0);
1086 desired_access = IVAL(params,8);
1087 file_attributes = IVAL(params,20);
1088 share_access = IVAL(params,24);
1089 create_disposition = IVAL(params,28);
1090 create_options = IVAL(params,32);
1091 sd_len = IVAL(params,36);
1092 root_dir_fid = (uint16)IVAL(params,4);
1093 smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1096 * We need to construct the open_and_X ofun value from the
1097 * NT values, as that's what our code is structured to accept.
1100 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1101 return ERROR_DOS(ERRDOS,ERRbadmem);
1104 * Get the file name.
1107 if(root_dir_fid != 0) {
1109 * This filename is relative to a directory fid.
1112 files_struct *dir_fsp = file_fsp(params,4);
1113 size_t dir_name_len;
1116 return ERROR_DOS(ERRDOS,ERRbadfid);
1118 if(!dir_fsp->is_directory) {
1120 * Check to see if this is a mac fork of some kind.
1123 srvstr_pull(inbuf, fname, params+53, sizeof(fname), -1, STR_TERMINATE);
1125 if( strchr_m(fname, ':')) {
1126 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1129 return ERROR_DOS(ERRDOS,ERRbadfid);
1133 * Copy in the base directory name.
1136 pstrcpy( fname, dir_fsp->fsp_name );
1137 dir_name_len = strlen(fname);
1140 * Ensure it ends in a '\'.
1143 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1144 pstrcat(fname, "\\");
1148 srvstr_pull(inbuf, &fname[dir_name_len], params+53, sizeof(fname)-dir_name_len,
1151 srvstr_pull(inbuf, fname, params+53, sizeof(fname), -1, STR_TERMINATE);
1155 * Now contruct the smb_open_mode value from the desired access
1156 * and the share access.
1159 if((smb_open_mode = map_share_mode( &stat_open_only, fname, desired_access,
1160 share_access, file_attributes)) == -1)
1161 return ERROR_DOS(ERRDOS,ERRbadaccess);
1163 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1164 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1167 * Check if POSIX semantics are wanted.
1170 set_posix_case_semantics(file_attributes);
1172 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1174 unix_convert(fname,conn,0,&bad_path,&sbuf);
1176 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1179 * If it's a request for a directory open, deal with it separately.
1182 if(create_options & FILE_DIRECTORY_FILE) {
1187 * We will get a create directory here if the Win32
1188 * app specified a security descriptor in the
1189 * CreateDirectory() call.
1192 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
1195 restore_case_semantics(file_attributes);
1196 if((errno == ENOENT) && bad_path) {
1197 unix_ERR_class = ERRDOS;
1198 unix_ERR_code = ERRbadpath;
1200 return(UNIXERROR(ERRDOS,ERRnoaccess));
1206 * Ordinary file case.
1209 fsp = open_file_shared(conn,fname,&sbuf,smb_open_mode,smb_ofun,unixmode,
1210 oplock_request,&rmode,&smb_action);
1214 if(errno == EISDIR) {
1217 * Fail the open if it was explicitly a non-directory file.
1220 if (create_options & FILE_NON_DIRECTORY_FILE) {
1221 restore_case_semantics(file_attributes);
1222 SSVAL(outbuf, smb_flg2,
1223 SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1224 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1228 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
1231 restore_case_semantics(file_attributes);
1232 if((errno == ENOENT) && bad_path) {
1233 unix_ERR_class = ERRDOS;
1234 unix_ERR_code = ERRbadpath;
1236 return(UNIXERROR(ERRDOS,ERRnoaccess));
1239 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
1241 } else if (errno == EACCES && stat_open_only) {
1245 * We couldn't open normally and all we want
1246 * are the permissions. Try and do a stat open.
1251 fsp = open_file_stat(conn,fname,&sbuf,smb_open_mode,&smb_action);
1254 restore_case_semantics(file_attributes);
1255 return(UNIXERROR(ERRDOS,ERRnoaccess));
1259 if((errno == ENOENT) && bad_path) {
1260 unix_ERR_class = ERRDOS;
1261 unix_ERR_code = ERRbadpath;
1264 restore_case_semantics(file_attributes);
1266 return(UNIXERROR(ERRDOS,ERRnoaccess));
1270 file_len = sbuf.st_size;
1271 fmode = dos_mode(conn,fname,&sbuf);
1273 fmode = FILE_ATTRIBUTE_NORMAL;
1276 close_file(fsp,False);
1277 restore_case_semantics(file_attributes);
1278 return ERROR_DOS(ERRDOS,ERRnoaccess);
1282 * If the caller set the extended oplock request bit
1283 * and we granted one (by whatever means) - set the
1284 * correct bit for extended oplock reply.
1287 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1288 smb_action |= EXTENDED_OPLOCK_GRANTED;
1290 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1291 smb_action |= EXTENDED_OPLOCK_GRANTED;
1295 * Now try and apply the desired SD.
1298 if (!set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION, &error_class, &error_code)) {
1299 close_file(fsp,False);
1300 restore_case_semantics(file_attributes);
1301 return ERROR_DOS(error_class, error_code);
1304 restore_case_semantics(file_attributes);
1306 /* Realloc the size of parameters and data we will return */
1307 params = Realloc(*ppparams, 69);
1309 return ERROR_DOS(ERRDOS,ERRnomem);
1313 memset((char *)params,'\0',69);
1316 if (smb_action & EXTENDED_OPLOCK_GRANTED)
1317 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1318 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1319 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1321 SCVAL(p,0,NO_OPLOCK_RETURN);
1324 SSVAL(p,0,fsp->fnum);
1326 SIVAL(p,0,smb_action);
1330 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1332 if (lp_dos_filetime_resolution(SNUM(conn))) {
1334 sbuf.st_atime &= ~1;
1335 sbuf.st_mtime &= ~1;
1336 sbuf.st_mtime &= ~1;
1339 put_long_date(p,c_time);
1341 put_long_date(p,sbuf.st_atime); /* access time */
1343 put_long_date(p,sbuf.st_mtime); /* write time */
1345 put_long_date(p,sbuf.st_mtime); /* change time */
1347 SIVAL(p,0,fmode); /* File Attributes. */
1349 SOFF_T(p,0,file_len);
1351 SOFF_T(p,0,file_len);
1353 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1355 /* Send the required number of replies */
1356 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1361 /****************************************************************************
1362 Reply to a NT CANCEL request.
1363 ****************************************************************************/
1364 int reply_ntcancel(connection_struct *conn,
1365 char *inbuf,char *outbuf,int length,int bufsize)
1368 * Go through and cancel any pending change notifies.
1371 int mid = SVAL(inbuf,smb_mid);
1372 START_PROFILE(SMBntcancel);
1373 remove_pending_change_notify_requests_by_mid(mid);
1374 remove_pending_lock_requests_by_mid(mid);
1376 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1378 END_PROFILE(SMBntcancel);
1382 /****************************************************************************
1383 Reply to an unsolicited SMBNTtranss - just ignore it!
1384 ****************************************************************************/
1385 int reply_nttranss(connection_struct *conn,
1386 char *inbuf,char *outbuf,int length,int bufsize)
1388 START_PROFILE(SMBnttranss);
1389 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1390 END_PROFILE(SMBnttranss);
1394 /****************************************************************************
1395 Reply to a notify change - queue the request and
1396 don't allow a directory to be opened.
1397 ****************************************************************************/
1398 static int call_nt_transact_notify_change(connection_struct *conn,
1399 char *inbuf, char *outbuf, int length,
1402 char **ppparams, char **ppdata)
1404 char *setup = *ppsetup;
1408 fsp = file_fsp(setup,4);
1409 flags = IVAL(setup, 0);
1411 DEBUG(3,("call_nt_transact_notify_change\n"));
1414 return ERROR_DOS(ERRDOS,ERRbadfid);
1416 if((!fsp->is_directory) || (conn != fsp->conn))
1417 return ERROR_DOS(ERRDOS,ERRbadfid);
1419 if (!change_notify_set(inbuf, fsp, conn, flags)) {
1420 return(UNIXERROR(ERRDOS,ERRbadfid));
1423 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1424 name = %s\n", fsp->fsp_name ));
1429 /****************************************************************************
1430 Reply to an NT transact rename command.
1431 ****************************************************************************/
1433 static int call_nt_transact_rename(connection_struct *conn,
1434 char *inbuf, char *outbuf, int length,
1436 char **ppsetup, char **ppparams, char **ppdata)
1438 char *params = *ppparams;
1440 files_struct *fsp = file_fsp(params, 0);
1441 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1444 CHECK_FSP(fsp, conn);
1445 srvstr_pull(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE);
1447 outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
1448 new_name, replace_if_exists);
1451 * Rename was successful.
1453 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1455 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1456 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);
1472 /****************************************************************************
1473 Reply to query a security descriptor - currently this is not implemented (it
1474 is planned to be though). Right now it just returns the same thing NT would
1475 when queried on a FAT filesystem. JRA.
1476 ****************************************************************************/
1478 static int call_nt_transact_query_security_desc(connection_struct *conn,
1479 char *inbuf, char *outbuf,
1480 int length, int bufsize,
1481 char **ppsetup, char **ppparams, char **ppdata)
1483 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1484 char *params = *ppparams;
1485 char *data = *ppdata;
1487 SEC_DESC *psd = NULL;
1489 TALLOC_CTX *mem_ctx;
1491 files_struct *fsp = file_fsp(params,0);
1494 return ERROR_DOS(ERRDOS,ERRbadfid);
1496 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1498 params = Realloc(*ppparams, 4);
1500 return ERROR_DOS(ERRDOS,ERRnomem);
1505 * Get the permissions to return.
1508 if((sd_size = conn->vfs_ops.fget_nt_acl(fsp, fsp->fd, &psd)) == 0)
1509 return(UNIXERROR(ERRDOS,ERRnoaccess));
1511 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1513 SIVAL(params,0,(uint32)sd_size);
1515 if(max_data_count < sd_size) {
1517 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1518 params, 4, *ppdata, 0);
1523 * Allocate the data we will point this at.
1526 data = Realloc(*ppdata, sd_size);
1528 return ERROR_DOS(ERRDOS,ERRnomem);
1533 memset(data, '\0', sd_size);
1536 * Init the parse struct we will marshall into.
1539 if ((mem_ctx = talloc_init()) == NULL) {
1540 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1541 return ERROR_DOS(ERRDOS,ERRnomem);
1544 prs_init(&pd, 0, mem_ctx, MARSHALL);
1547 * Setup the prs_struct to point at the memory we just
1551 prs_give_memory( &pd, data, (uint32)sd_size, False);
1554 * Finally, linearize into the outgoing buffer.
1557 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1558 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1559 security descriptor.\n"));
1561 * Return access denied for want of a better error message..
1563 talloc_destroy(mem_ctx);
1564 return(UNIXERROR(ERRDOS,ERRnoaccess));
1568 * Now we can delete the security descriptor.
1571 talloc_destroy(mem_ctx);
1573 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
1577 /****************************************************************************
1578 Reply to set a security descriptor. Map to UNIX perms.
1579 ****************************************************************************/
1581 static int call_nt_transact_set_security_desc(connection_struct *conn,
1582 char *inbuf, char *outbuf, int length,
1583 int bufsize, char **ppsetup,
1584 char **ppparams, char **ppdata)
1586 uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1587 char *params= *ppparams;
1588 char *data = *ppdata;
1589 uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
1590 files_struct *fsp = NULL;
1591 uint32 security_info_sent = 0;
1595 if(!lp_nt_acl_support())
1596 return(UNIXERROR(ERRDOS,ERRnoaccess));
1598 if(total_parameter_count < 8)
1599 return ERROR_DOS(ERRDOS,ERRbadfunc);
1601 if((fsp = file_fsp(params,0)) == NULL)
1602 return ERROR_DOS(ERRDOS,ERRbadfid);
1604 security_info_sent = IVAL(params,4);
1606 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1607 (unsigned int)security_info_sent ));
1609 if (!set_sd( fsp, data, total_data_count, security_info_sent, &error_class, &error_code))
1610 return ERROR_DOS(error_class, error_code);
1612 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1616 /****************************************************************************
1617 Reply to IOCTL - not implemented - no plans.
1618 ****************************************************************************/
1619 static int call_nt_transact_ioctl(connection_struct *conn,
1620 char *inbuf, char *outbuf, int length,
1622 char **ppsetup, char **ppparams, char **ppdata)
1624 static BOOL logged_message = False;
1626 if(!logged_message) {
1627 DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1628 logged_message = True; /* Only print this once... */
1630 return ERROR_DOS(ERRSRV,ERRnosupport);
1633 /****************************************************************************
1634 Reply to a SMBNTtrans.
1635 ****************************************************************************/
1636 int reply_nttrans(connection_struct *conn,
1637 char *inbuf,char *outbuf,int length,int bufsize)
1640 #if 0 /* Not used. */
1641 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1642 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1643 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1644 #endif /* Not used. */
1645 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1646 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1647 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1648 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1649 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1650 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1651 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1652 uint16 function_code = SVAL( inbuf, smb_nt_Function);
1653 char *params = NULL, *data = NULL, *setup = NULL;
1654 uint32 num_params_sofar, num_data_sofar;
1655 START_PROFILE(SMBnttrans);
1657 if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1659 * Queue this open message as we are the process of an oplock break.
1662 DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1663 due to being in oplock break state.\n" ));
1665 push_oplock_pending_smb_message( inbuf, length);
1666 END_PROFILE(SMBnttrans);
1670 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
1671 END_PROFILE(SMBnttrans);
1672 return ERROR_DOS(ERRSRV,ERRaccess);
1675 outsize = set_message(outbuf,0,0,True);
1678 * All nttrans messages we handle have smb_wct == 19 + setup_count.
1679 * Ensure this is so as a sanity check.
1682 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1683 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1684 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1685 END_PROFILE(SMBnttrans);
1686 return ERROR_DOS(ERRSRV,ERRerror);
1689 /* Allocate the space for the setup, the maximum needed parameters and data */
1692 setup = (char *)malloc(setup_count);
1693 if (total_parameter_count > 0)
1694 params = (char *)malloc(total_parameter_count);
1695 if (total_data_count > 0)
1696 data = (char *)malloc(total_data_count);
1698 if ((total_parameter_count && !params) || (total_data_count && !data) ||
1699 (setup_count && !setup)) {
1703 DEBUG(0,("reply_nttrans : Out of memory\n"));
1704 END_PROFILE(SMBnttrans);
1705 return ERROR_DOS(ERRDOS,ERRnomem);
1708 /* Copy the param and data bytes sent with this request into
1709 the params buffer */
1710 num_params_sofar = parameter_count;
1711 num_data_sofar = data_count;
1713 if (parameter_count > total_parameter_count || data_count > total_data_count)
1714 exit_server("reply_nttrans: invalid sizes in packet.\n");
1717 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1718 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1719 dump_data(10, setup, setup_count);
1722 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1723 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1724 dump_data(10, params, parameter_count);
1727 memcpy( data, smb_base(inbuf) + data_offset, data_count);
1728 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1729 dump_data(10, data, data_count);
1732 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1733 /* We need to send an interim response then receive the rest
1734 of the parameter/data bytes */
1735 outsize = set_message(outbuf,0,0,True);
1736 if (!send_smb(smbd_server_fd(),outbuf))
1737 exit_server("reply_nttrans: send_smb failed.\n");
1739 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1742 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
1744 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1745 outsize = set_message(outbuf,0,0,True);
1747 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1749 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1750 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1758 END_PROFILE(SMBnttrans);
1759 return ERROR_DOS(ERRSRV,ERRerror);
1762 /* Revise total_params and total_data in case they have changed downwards */
1763 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1764 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1765 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1766 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1767 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1768 exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
1770 memcpy( ¶ms[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
1771 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1772 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1773 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1777 if (Protocol >= PROTOCOL_NT1) {
1778 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | 0x40); /* IS_LONG_NAME */
1781 /* Now we must call the relevant NT_TRANS function */
1782 switch(function_code) {
1783 case NT_TRANSACT_CREATE:
1784 START_PROFILE_NESTED(NT_transact_create);
1785 outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize,
1786 &setup, ¶ms, &data);
1787 END_PROFILE_NESTED(NT_transact_create);
1789 case NT_TRANSACT_IOCTL:
1790 START_PROFILE_NESTED(NT_transact_ioctl);
1791 outsize = call_nt_transact_ioctl(conn,
1792 inbuf, outbuf, length, bufsize,
1793 &setup, ¶ms, &data);
1794 END_PROFILE_NESTED(NT_transact_ioctl);
1796 case NT_TRANSACT_SET_SECURITY_DESC:
1797 START_PROFILE_NESTED(NT_transact_set_security_desc);
1798 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
1800 &setup, ¶ms, &data);
1801 END_PROFILE_NESTED(NT_transact_set_security_desc);
1803 case NT_TRANSACT_NOTIFY_CHANGE:
1804 START_PROFILE_NESTED(NT_transact_notify_change);
1805 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
1807 &setup, ¶ms, &data);
1808 END_PROFILE_NESTED(NT_transact_notify_change);
1810 case NT_TRANSACT_RENAME:
1811 START_PROFILE_NESTED(NT_transact_rename);
1812 outsize = call_nt_transact_rename(conn, inbuf, outbuf, length,
1814 &setup, ¶ms, &data);
1815 END_PROFILE_NESTED(NT_transact_rename);
1818 case NT_TRANSACT_QUERY_SECURITY_DESC:
1819 START_PROFILE_NESTED(NT_transact_query_security_desc);
1820 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
1822 &setup, ¶ms, &data);
1823 END_PROFILE_NESTED(NT_transact_query_security_desc);
1826 /* Error in request */
1827 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1834 END_PROFILE(SMBnttrans);
1835 return ERROR_DOS(ERRSRV,ERRerror);
1838 /* As we do not know how many data packets will need to be
1839 returned here the various call_nt_transact_xxxx calls
1840 must send their own. Thus a call_nt_transact_xxxx routine only
1841 returns a value other than -1 when it wants to send
1851 END_PROFILE(SMBnttrans);
1852 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
1853 calls have already sent it. If outsize != -1 then it is
1854 returning an error packet. */