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 DEBUGLEVEL;
28 extern int oplock_sock;
29 extern int smb_read_error;
30 extern int global_oplock_break;
31 extern int chain_size;
32 extern BOOL case_sensitive;
33 extern BOOL case_preserve;
34 extern BOOL short_case_preserve;
36 static char *known_nt_pipes[] = {
49 /****************************************************************************
50 Send the required number of replies back.
51 We assume all fields other than the data fields are
52 set correctly for the type of call.
53 HACK ! Always assumes smb_setup field is zero.
54 ****************************************************************************/
56 static int send_nt_replies(char *outbuf, int bufsize, char *params,
57 int paramsize, char *pdata, int datasize)
60 int data_to_send = datasize;
61 int params_to_send = paramsize;
65 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
66 int alignment_offset = 3;
67 int data_alignment_offset = 0;
70 * Initially set the wcnt area to be 18 - this is true for all
74 set_message(outbuf,18,0,True);
77 * If there genuinely are no parameters or data to send just send
81 if(params_to_send == 0 && data_to_send == 0) {
82 send_smb(Client,outbuf);
87 * When sending params and data ensure that both are nicely aligned.
88 * Only do this alignment when there is also data to send - else
89 * can cause NT redirector problems.
92 if (((params_to_send % 4) != 0) && (data_to_send != 0))
93 data_alignment_offset = 4 - (params_to_send % 4);
96 * Space is bufsize minus Netbios over TCP header minus SMB header.
97 * The alignment_offset is to align the param bytes on a four byte
98 * boundary (2 bytes for data len, one byte pad).
99 * NT needs this to work correctly.
102 useable_space = bufsize - ((smb_buf(outbuf)+
103 alignment_offset+data_alignment_offset) -
107 * useable_space can never be more than max_send minus the
111 useable_space = MIN(useable_space,
112 max_send - (alignment_offset+data_alignment_offset));
115 while (params_to_send || data_to_send) {
118 * Calculate whether we will totally or partially fill this packet.
121 total_sent_thistime = params_to_send + data_to_send +
122 alignment_offset + data_alignment_offset;
125 * We can never send more than useable_space.
128 total_sent_thistime = MIN(total_sent_thistime, useable_space);
130 set_message(outbuf, 18, total_sent_thistime, True);
133 * Set total params and data to be sent.
136 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
137 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
140 * Calculate how many parameters and data we can fit into
141 * this packet. Parameters get precedence.
144 params_sent_thistime = MIN(params_to_send,useable_space);
145 data_sent_thistime = useable_space - params_sent_thistime;
146 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
148 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
150 if(params_sent_thistime == 0) {
151 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
152 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
155 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
156 * parameter bytes, however the first 4 bytes of outbuf are
157 * the Netbios over TCP header. Thus use smb_base() to subtract
158 * them from the calculation.
161 SIVAL(outbuf,smb_ntr_ParameterOffset,
162 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
164 * Absolute displacement of param bytes sent in this packet.
167 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
171 * Deal with the data portion.
174 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
176 if(data_sent_thistime == 0) {
177 SIVAL(outbuf,smb_ntr_DataOffset,0);
178 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
181 * The offset of the data bytes is the offset of the
182 * parameter bytes plus the number of parameters being sent this time.
185 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
186 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
187 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
191 * Copy the param bytes into the packet.
194 if(params_sent_thistime)
195 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
198 * Copy in the data bytes
201 if(data_sent_thistime)
202 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
203 data_alignment_offset,pd,data_sent_thistime);
205 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
206 params_sent_thistime, data_sent_thistime, useable_space));
207 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
208 params_to_send, data_to_send, paramsize, datasize));
210 /* Send the packet */
211 send_smb(Client,outbuf);
213 pp += params_sent_thistime;
214 pd += data_sent_thistime;
216 params_to_send -= params_sent_thistime;
217 data_to_send -= data_sent_thistime;
223 if(params_to_send < 0 || data_to_send < 0) {
224 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
225 params_to_send, data_to_send));
233 /****************************************************************************
235 ****************************************************************************/
237 static BOOL saved_case_sensitive;
238 static BOOL saved_case_preserve;
239 static BOOL saved_short_case_preserve;
241 /****************************************************************************
243 ****************************************************************************/
245 static void set_posix_case_semantics(uint32 file_attributes)
247 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
250 saved_case_sensitive = case_sensitive;
251 saved_case_preserve = case_preserve;
252 saved_short_case_preserve = short_case_preserve;
255 case_sensitive = True;
256 case_preserve = True;
257 short_case_preserve = True;
260 /****************************************************************************
261 Restore case semantics.
262 ****************************************************************************/
264 static void restore_case_semantics(uint32 file_attributes)
266 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
269 case_sensitive = saved_case_sensitive;
270 case_preserve = saved_case_preserve;
271 short_case_preserve = saved_short_case_preserve;
274 /****************************************************************************
275 Utility function to map create disposition.
276 ****************************************************************************/
278 static int map_create_disposition( uint32 create_disposition)
280 switch( create_disposition ) {
282 /* create if not exist, fail if exist */
285 case FILE_OVERWRITE_IF:
286 /* create if not exist, trunc if exist */
289 /* fail if not exist, open if exists */
292 /* create if not exist, open if exists */
295 /* fail if not exist, truncate if exists */
298 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
299 create_disposition ));
304 /****************************************************************************
305 Utility function to map share modes.
306 ****************************************************************************/
308 static int map_share_mode( uint32 desired_access, uint32 share_access, uint32 file_attributes)
310 int smb_open_mode = -1;
312 switch( desired_access & (FILE_READ_DATA|FILE_WRITE_DATA) ) {
316 case FILE_WRITE_DATA:
319 case FILE_READ_DATA|FILE_WRITE_DATA:
324 if (smb_open_mode == -1) {
325 if(desired_access & (DELETE_ACCESS|FILE_WRITE_ATTRIBUTES))
327 else if( desired_access & FILE_EXECUTE)
330 DEBUG(0,("map_share_mode: Incorrect value for desired_access = %x\n",
336 /* Add in the requested share mode - ignore FILE_SHARE_DELETE for now. */
337 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
338 case FILE_SHARE_READ:
339 smb_open_mode |= (DENY_WRITE<<4);
341 case FILE_SHARE_WRITE:
342 smb_open_mode |= (DENY_READ<<4);
344 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
345 smb_open_mode |= (DENY_NONE<<4);
347 case FILE_SHARE_NONE:
348 smb_open_mode |= (DENY_ALL<<4);
353 * Handle a O_SYNC request.
355 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
356 smb_open_mode |= (1<<14);
358 return smb_open_mode;
361 /****************************************************************************
362 Reply to an NT create and X call on a pipe.
363 ****************************************************************************/
364 static int nt_open_pipe(char *fname, connection_struct *conn,
365 char *inbuf, char *outbuf, int *ppnum)
367 pipes_struct *p = NULL;
369 uint16 vuid = SVAL(inbuf, smb_uid);
372 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
374 /* See if it is one we want to handle. */
375 for( i = 0; known_nt_pipes[i]; i++ )
376 if( strequal(fname,known_nt_pipes[i]))
379 if ( known_nt_pipes[i] == NULL )
380 return(ERROR(ERRSRV,ERRaccess));
382 /* Strip \\ off the name. */
385 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
387 p = open_rpc_pipe_p(fname, conn, vuid);
389 return(ERROR(ERRSRV,ERRnofids));
396 /****************************************************************************
397 Reply to an NT create and X call.
398 ****************************************************************************/
399 int reply_ntcreate_and_X(connection_struct *conn,
400 char *inbuf,char *outbuf,int length,int bufsize)
403 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
404 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
405 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
406 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
407 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
408 uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
409 ((uint32)sizeof(fname)-1));
412 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
413 /* Breakout the oplock request bits so we can set the
414 reply bits separately. */
415 int oplock_request = 0;
416 int unixmode, pnum = -1;
417 int fmode=0,mtime=0,rmode=0;
418 SMB_OFF_T file_len = 0;
419 SMB_STRUCT_STAT sbuf;
421 BOOL bad_path = False;
422 files_struct *fsp=NULL;
426 * We need to construct the open_and_X ofun value from the
427 * NT values, as that's what our code is structured to accept.
430 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
431 return(ERROR(ERRDOS,ERRbadaccess));
434 * Now contruct the smb_open_mode value from the desired access
435 * and the share access.
438 if((smb_open_mode = map_share_mode(desired_access,
440 file_attributes)) == -1) {
441 return(ERROR(ERRDOS,ERRbadaccess));
447 StrnCpy(fname,smb_buf(inbuf),fname_len);
448 fname[fname_len] = '\0';
450 /* If it's an IPC, use the pipe handler. */
454 int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
459 * Deal with pipe return.
462 set_message(outbuf,34,0,True);
464 p = outbuf + smb_vwv2;
468 SIVAL(p,0,FILE_WAS_OPENED);
471 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
474 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
476 SSVAL(p,2, 0x5FF); /* ? */
478 DEBUG(5,("reply_ntcreate_and_X: open pipe = %s\n", fname));
480 return chain_reply(inbuf,outbuf,length,bufsize);
484 * Ordinary file or directory.
488 * Check if POSIX semantics are wanted.
491 set_posix_case_semantics(file_attributes);
493 unix_convert(fname,conn,0,&bad_path,NULL);
497 restore_case_semantics(file_attributes);
498 return(ERROR(ERRSRV,ERRnofids));
501 if (!check_name(fname,conn)) {
502 if((errno == ENOENT) && bad_path) {
503 unix_ERR_class = ERRDOS;
504 unix_ERR_code = ERRbadpath;
508 restore_case_semantics(file_attributes);
510 return(UNIXERROR(ERRDOS,ERRnoaccess));
513 unixmode = unix_mode(conn,smb_attr | aARCH);
515 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
516 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
519 * If it's a request for a directory open, deal with it separately.
522 if(flags & OPEN_DIRECTORY) {
525 open_directory(fsp, conn, fname, smb_ofun,
526 unixmode, &smb_action);
528 restore_case_semantics(file_attributes);
532 return(UNIXERROR(ERRDOS,ERRnoaccess));
536 * Ordinary file case.
539 /* NB. We have a potential bug here. If we
540 * cause an oplock break to ourselves, then we
541 * could end up processing filename related
542 * SMB requests whilst we await the oplock
543 * break response. As we may have changed the
544 * filename case semantics to be POSIX-like,
545 * this could mean a filename request could
546 * fail when it should succeed. This is a rare
547 * condition, but eventually we must arrange
548 * to restore the correct case semantics
549 * before issuing an oplock break request to
550 * our client. JRA. */
552 open_file_shared(fsp,conn,fname,smb_open_mode,
554 oplock_request,&rmode,&smb_action);
557 /* We cheat here. The only case we
558 * care about is a directory rename,
559 * where the NT client will attempt to
560 * open the source directory for
561 * DELETE access. Note that when the
562 * NT client does this it does *not*
563 * set the directory bit in the
564 * request packet. This is translated
565 * into a read/write open
566 * request. POSIX states that any open
567 * for write request on a directory
568 * will generate an EISDIR error, so
569 * we can catch this here and open a
570 * pseudo handle that is flagged as a
573 if(errno == EISDIR) {
576 open_directory(fsp, conn, fname, smb_ofun, unixmode, &smb_action);
580 restore_case_semantics(file_attributes);
581 return(UNIXERROR(ERRDOS,ERRnoaccess));
584 if((errno == ENOENT) && bad_path) {
585 unix_ERR_class = ERRDOS;
586 unix_ERR_code = ERRbadpath;
591 restore_case_semantics(file_attributes);
593 return(UNIXERROR(ERRDOS,ERRnoaccess));
598 if(fsp->is_directory) {
599 if(dos_stat(fsp->fsp_name, &sbuf) != 0) {
600 close_directory(fsp);
601 restore_case_semantics(file_attributes);
602 return(ERROR(ERRDOS,ERRnoaccess));
605 if (sys_fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
606 close_file(fsp,False);
607 restore_case_semantics(file_attributes);
608 return(ERROR(ERRDOS,ERRnoaccess));
612 restore_case_semantics(file_attributes);
614 file_len = sbuf.st_size;
615 fmode = dos_mode(conn,fname,&sbuf);
617 fmode = FILE_ATTRIBUTE_NORMAL;
618 mtime = sbuf.st_mtime;
619 if (!fsp->is_directory && (fmode & aDIR)) {
620 close_file(fsp,False);
621 return(ERROR(ERRDOS,ERRnoaccess));
625 * If the caller set the extended oplock request bit
626 * and we granted one (by whatever means) - set the
627 * correct bit for extended oplock reply.
630 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
631 smb_action |= EXTENDED_OPLOCK_GRANTED;
633 if(oplock_request && fsp->granted_oplock)
634 smb_action |= EXTENDED_OPLOCK_GRANTED;
636 set_message(outbuf,34,0,True);
638 p = outbuf + smb_vwv2;
641 * Currently as we don't support level II oplocks we just report
642 * exclusive & batch here.
645 SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
647 SSVAL(p,0,fsp->fnum);
649 SIVAL(p,0,smb_action);
653 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
655 put_long_date(p,sbuf.st_atime); /* access time */
657 put_long_date(p,sbuf.st_mtime); /* write time */
659 put_long_date(p,sbuf.st_mtime); /* change time */
661 SIVAL(p,0,fmode); /* File Attributes. */
663 #ifdef LARGE_SMB_OFF_T
664 SIVAL(p,0, file_len & 0xFFFFFFFF);
665 SIVAL(p,4, file_len >> 32);
666 #else /* LARGE_SMB_OFF_T */
668 #endif /* LARGE_SMB_OFF_T */
670 SCVAL(p,0,fsp->is_directory ? 1 : 0);
672 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
674 return chain_reply(inbuf,outbuf,length,bufsize);
677 /****************************************************************************
678 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
679 ****************************************************************************/
680 static int call_nt_transact_create(connection_struct *conn,
681 char *inbuf, char *outbuf, int length,
683 char **ppsetup, char **ppparams,
687 char *params = *ppparams;
688 uint32 flags = IVAL(params,0);
689 uint32 desired_access = IVAL(params,8);
690 uint32 file_attributes = IVAL(params,20);
691 uint32 share_access = IVAL(params,24);
692 uint32 create_disposition = IVAL(params,28);
693 uint32 fname_len = MIN(((uint32)IVAL(params,44)),
694 ((uint32)sizeof(fname)-1));
697 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
698 /* Breakout the oplock request bits so we can set the
699 reply bits separately. */
700 int oplock_request = 0;
701 int unixmode, pnum = -1;
702 int fmode=0,mtime=0,rmode=0;
704 SMB_STRUCT_STAT sbuf;
706 BOOL bad_path = False;
707 files_struct *fsp = NULL;
711 * We need to construct the open_and_X ofun value from the
712 * NT values, as that's what our code is structured to accept.
715 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
716 return(ERROR(ERRDOS,ERRbadaccess));
719 * Now contruct the smb_open_mode value from the desired access
720 * and the share access.
723 if((smb_open_mode = map_share_mode( desired_access, share_access, file_attributes)) == -1)
724 return(ERROR(ERRDOS,ERRbadaccess));
730 StrnCpy(fname,params+53,fname_len);
731 fname[fname_len] = '\0';
733 /* If it's an IPC, use the pipe handler. */
735 int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
738 smb_action = FILE_WAS_OPENED;
741 * Check if POSIX semantics are wanted.
744 set_posix_case_semantics(file_attributes);
746 unix_convert(fname,conn,0,&bad_path,NULL);
750 restore_case_semantics(file_attributes);
751 return(ERROR(ERRSRV,ERRnofids));
754 if (!check_name(fname,conn)) {
755 if((errno == ENOENT) && bad_path) {
756 unix_ERR_class = ERRDOS;
757 unix_ERR_code = ERRbadpath;
761 restore_case_semantics(file_attributes);
763 return(UNIXERROR(ERRDOS,ERRnoaccess));
766 unixmode = unix_mode(conn,smb_attr | aARCH);
768 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
769 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
772 * If it's a request for a directory open, deal with it separately.
775 if(flags & OPEN_DIRECTORY) {
780 * We will get a create directory here if the Win32
781 * app specified a security descriptor in the
782 * CreateDirectory() call.
785 open_directory(fsp, conn, fname, smb_ofun, unixmode, &smb_action);
789 return(UNIXERROR(ERRDOS,ERRnoaccess));
794 * Ordinary file case.
797 open_file_shared(fsp,conn,fname,smb_open_mode,smb_ofun,unixmode,
798 oplock_request,&rmode,&smb_action);
801 if((errno == ENOENT) && bad_path) {
802 unix_ERR_class = ERRDOS;
803 unix_ERR_code = ERRbadpath;
807 restore_case_semantics(file_attributes);
809 return(UNIXERROR(ERRDOS,ERRnoaccess));
812 if (sys_fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
813 close_file(fsp,False);
815 restore_case_semantics(file_attributes);
817 return(ERROR(ERRDOS,ERRnoaccess));
820 file_len = sbuf.st_size;
821 fmode = dos_mode(conn,fname,&sbuf);
823 fmode = FILE_ATTRIBUTE_NORMAL;
824 mtime = sbuf.st_mtime;
827 close_file(fsp,False);
828 restore_case_semantics(file_attributes);
829 return(ERROR(ERRDOS,ERRnoaccess));
833 * If the caller set the extended oplock request bit
834 * and we granted one (by whatever means) - set the
835 * correct bit for extended oplock reply.
838 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
839 smb_action |= EXTENDED_OPLOCK_GRANTED;
841 if(oplock_request && fsp->granted_oplock)
842 smb_action |= EXTENDED_OPLOCK_GRANTED;
846 restore_case_semantics(file_attributes);
848 /* Realloc the size of parameters and data we will return */
849 params = *ppparams = Realloc(*ppparams, 69);
851 return(ERROR(ERRDOS,ERRnomem));
854 SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
859 SSVAL(p,0,fsp->fnum);
862 SIVAL(p,0,smb_action);
867 * Deal with pipe return.
870 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
873 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
875 SSVAL(p,2, 0x5FF); /* ? */
878 * Deal with file return.
881 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
883 put_long_date(p,sbuf.st_atime); /* access time */
885 put_long_date(p,sbuf.st_mtime); /* write time */
887 put_long_date(p,sbuf.st_mtime); /* change time */
889 SIVAL(p,0,fmode); /* File Attributes. */
892 SIVAL(p,0, file_len & 0xFFFFFFFF);
893 SIVAL(p,4, (file_len >> 32));
894 #else /* OFF_T_IS_64_BITS */
896 #endif /* OFF_T_IS_64_BITS */
899 /* Send the required number of replies */
900 send_nt_replies(outbuf, bufsize, params, 69, *ppdata, 0);
905 /****************************************************************************
906 Reply to a NT CANCEL request.
907 ****************************************************************************/
908 int reply_ntcancel(connection_struct *conn,
909 char *inbuf,char *outbuf,int length,int bufsize)
912 * Go through and cancel any pending change notifies.
915 int mid = SVAL(inbuf,smb_mid);
916 remove_pending_change_notify_requests_by_mid(mid);
917 remove_pending_lock_requests_by_mid(mid);
919 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
924 /****************************************************************************
925 Reply to an unsolicited SMBNTtranss - just ignore it!
926 ****************************************************************************/
927 int reply_nttranss(connection_struct *conn,
928 char *inbuf,char *outbuf,int length,int bufsize)
930 DEBUG(4,("Ignoring nttranss of length %d\n",length));
934 /****************************************************************************
935 Reply to an NT transact rename command.
936 ****************************************************************************/
937 static int call_nt_transact_rename(connection_struct *conn,
938 char *inbuf, char *outbuf, int length,
940 char **ppsetup, char **ppparams, char **ppdata)
942 char *params = *ppparams;
944 files_struct *fsp = file_fsp(params, 0);
945 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
946 uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
947 ((uint32)sizeof(new_name)-1));
950 CHECK_FSP(fsp, conn);
951 StrnCpy(new_name,params+4,fname_len);
952 new_name[fname_len] = '\0';
954 outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
955 new_name, replace_if_exists);
958 * Rename was successful.
960 send_nt_replies(outbuf, bufsize, NULL, 0, NULL, 0);
962 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
963 fsp->fsp_name, new_name));
971 /****************************************************************************
972 This is the structure to queue to implement NT change
973 notify. It consists of smb_size bytes stored from the
974 transact command (to keep the mid, tid etc around).
975 Plus the fid to examine and the time to check next.
976 *****************************************************************************/
981 connection_struct *conn;
982 time_t next_check_time;
983 time_t modify_time; /* Info from the directory we're monitoring. */
984 time_t status_time; /* Info from the directory we're monitoring. */
985 char request_buf[smb_size];
988 static ubi_slList change_notify_queue = { NULL, (ubi_slNodePtr)&change_notify_queue, 0};
990 /****************************************************************************
991 Setup the common parts of the return packet and send it.
992 *****************************************************************************/
994 static void change_notify_reply_packet(char *inbuf, int error_class, uint32 error_code)
997 char outbuf[smb_size+38];
999 memset(outbuf, '\0', sizeof(outbuf));
1000 construct_reply_common(inbuf, outbuf);
1003 * If we're returning a 'too much in the directory changed' we need to
1004 * set this is an NT error status flags. If we don't then the (probably
1005 * untested) code in the NT redirector has a bug in that it doesn't re-issue
1006 * the change notify.... Ah - I *love* it when I get so deeply into this I
1007 * can even determine how MS failed to test stuff and why.... :-). JRA.
1010 if(error_class == 0) /* NT Error. */
1011 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1013 ERROR(error_class,error_code);
1016 * Seems NT needs a transact command with an error code
1017 * in it. This is a longer packet than a simple error.
1019 set_message(outbuf,18,0,False);
1021 send_smb(Client,outbuf);
1024 /****************************************************************************
1025 Delete entries by fnum from the change notify pending queue.
1026 *****************************************************************************/
1027 void remove_pending_change_notify_requests_by_fid(files_struct *fsp)
1029 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1030 change_notify_buf *prev = NULL;
1032 while(cnbp != NULL) {
1033 if(cnbp->fsp->fnum == fsp->fnum) {
1034 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1035 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1040 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1044 /****************************************************************************
1045 Delete entries by mid from the change notify pending queue. Always send reply.
1046 *****************************************************************************/
1048 void remove_pending_change_notify_requests_by_mid(int mid)
1050 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1051 change_notify_buf *prev = NULL;
1053 while(cnbp != NULL) {
1054 if(SVAL(cnbp->request_buf,smb_mid) == mid) {
1055 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_CANCELLED);
1056 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1057 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1062 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1066 /****************************************************************************
1067 Process the change notify queue. Note that this is only called as root.
1068 *****************************************************************************/
1070 void process_pending_change_notify_queue(time_t t)
1072 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1073 change_notify_buf *prev = NULL;
1078 if(cnbp->next_check_time >= t)
1082 * It's time to check. Go through the queue and see if
1083 * the timestamps changed.
1086 while((cnbp != NULL) && (cnbp->next_check_time <= t)) {
1088 files_struct *fsp = cnbp->fsp;
1089 connection_struct *conn = cnbp->conn;
1090 uint16 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
1091 SVAL(cnbp->request_buf,smb_uid);
1094 * Ensure we don't have any old chain_fsp values
1095 * sitting around....
1100 if(!become_user(conn,vuid)) {
1101 DEBUG(0,("process_pending_change_notify_queue: Unable to become user vuid=%d.\n",
1104 * Remove the entry and return an error to the client.
1106 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1107 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1108 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1112 if(!become_service(conn,True)) {
1113 DEBUG(0,("process_pending_change_notify_queue: Unable to become service Error was %s.\n", strerror(errno) ));
1115 * Remove the entry and return an error to the client.
1117 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1118 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1119 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1124 if(dos_stat(fsp->fsp_name, &st) < 0) {
1125 DEBUG(0,("process_pending_change_notify_queue: Unable to stat directory %s. \
1126 Error was %s.\n", fsp->fsp_name, strerror(errno) ));
1128 * Remove the entry and return an error to the client.
1130 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1131 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1132 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1137 if(cnbp->modify_time != st.st_mtime ||
1138 cnbp->status_time != st.st_ctime) {
1140 * Remove the entry and return a change notify to the client.
1142 DEBUG(5,("process_pending_change_notify_queue: directory name = %s changed\n",
1144 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_NOTIFY_ENUM_DIR);
1145 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1146 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1154 * Move to the next in the list.
1157 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1161 /****************************************************************************
1162 Reply to a notify change - queue the request and
1163 don't allow a directory to be opened.
1164 ****************************************************************************/
1165 static int call_nt_transact_notify_change(connection_struct *conn,
1166 char *inbuf, char *outbuf, int length,
1169 char **ppparams, char **ppdata)
1171 char *setup = *ppsetup;
1173 change_notify_buf *cnbp;
1176 fsp = file_fsp(setup,4);
1178 DEBUG(3,("call_nt_transact_notify_change\n"));
1181 return(ERROR(ERRDOS,ERRbadfid));
1183 if((!fsp->open) || (!fsp->is_directory) || (conn != fsp->conn))
1184 return(ERROR(ERRDOS,ERRbadfid));
1187 * Now queue an entry on the notify change stack. We timestamp
1188 * the entry we are adding so that we know when to scan next.
1189 * We only need to save smb_size bytes from this incoming packet
1190 * as we will always by returning a 'read the directory yourself'
1194 if((cnbp = (change_notify_buf *)malloc(sizeof(change_notify_buf))) == NULL) {
1195 DEBUG(0,("call_nt_transact_notify_change: Malloc fail (2) !\n" ));
1200 * Store the current timestamp on the directory we are monitoring.
1203 if(dos_stat(fsp->fsp_name, &st) < 0) {
1204 DEBUG(0,("call_nt_transact_notify_change: Unable to stat name = %s. \
1205 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1207 return(UNIXERROR(ERRDOS,ERRbadfid));
1210 memcpy(cnbp->request_buf, inbuf, smb_size);
1213 cnbp->modify_time = st.st_mtime;
1214 cnbp->status_time = st.st_ctime;
1216 cnbp->next_check_time = time(NULL) + lp_change_notify_timeout();
1219 * Adding to the tail enables us to check only
1220 * the head when scanning for change, as this entry
1221 * is forced to have the first timeout expiration.
1224 ubi_slAddTail(&change_notify_queue, cnbp);
1226 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1227 name = %s\n", fsp->fsp_name ));
1232 /****************************************************************************
1233 Reply to query a security descriptor - currently this is not implemented (it
1234 is planned to be though).
1235 ****************************************************************************/
1236 static int call_nt_transact_query_security_desc(connection_struct *conn,
1237 char *inbuf, char *outbuf,
1240 char **ppsetup, char **ppparams, char **ppdata)
1242 DEBUG(0,("call_nt_transact_query_security_desc: Currently not implemented.\n"));
1243 return(ERROR(ERRSRV,ERRnosupport));
1246 /****************************************************************************
1247 Reply to set a security descriptor - currently this is not implemented (it
1248 is planned to be though).
1249 ****************************************************************************/
1250 static int call_nt_transact_set_security_desc(connection_struct *conn,
1251 char *inbuf, char *outbuf,
1255 char **ppparams, char **ppdata)
1257 DEBUG(0,("call_nt_transact_set_security_desc: Currently not implemented.\n"));
1258 return(ERROR(ERRSRV,ERRnosupport));
1261 /****************************************************************************
1262 Reply to IOCTL - not implemented - no plans.
1263 ****************************************************************************/
1264 static int call_nt_transact_ioctl(connection_struct *conn,
1265 char *inbuf, char *outbuf, int length,
1267 char **ppsetup, char **ppparams, char **ppdata)
1269 DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1270 return(ERROR(ERRSRV,ERRnosupport));
1273 /****************************************************************************
1274 Reply to a SMBNTtrans.
1275 ****************************************************************************/
1276 int reply_nttrans(connection_struct *conn,
1277 char *inbuf,char *outbuf,int length,int bufsize)
1280 #if 0 /* Not used. */
1281 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1282 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1283 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1284 #endif /* Not used. */
1285 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1286 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1287 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1288 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1289 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1290 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1291 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1292 uint16 function_code = SVAL( inbuf, smb_nt_Function);
1293 char *params = NULL, *data = NULL, *setup = NULL;
1294 uint32 num_params_sofar, num_data_sofar;
1296 if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1298 * Queue this open message as we are the process of an oplock break.
1301 DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1302 due to being in oplock break state.\n" ));
1304 push_oplock_pending_smb_message( inbuf, length);
1308 outsize = set_message(outbuf,0,0,True);
1311 * All nttrans messages we handle have smb_wct == 19 + setup_count.
1312 * Ensure this is so as a sanity check.
1315 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1316 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1317 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1318 return(ERROR(ERRSRV,ERRerror));
1321 /* Allocate the space for the setup, the maximum needed parameters and data */
1324 setup = (char *)malloc(setup_count);
1325 if (total_parameter_count > 0)
1326 params = (char *)malloc(total_parameter_count);
1327 if (total_data_count > 0)
1328 data = (char *)malloc(total_data_count);
1330 if ((total_parameter_count && !params) || (total_data_count && !data) ||
1331 (setup_count && !setup)) {
1332 DEBUG(0,("reply_nttrans : Out of memory\n"));
1333 return(ERROR(ERRDOS,ERRnomem));
1336 /* Copy the param and data bytes sent with this request into
1337 the params buffer */
1338 num_params_sofar = parameter_count;
1339 num_data_sofar = data_count;
1341 if (parameter_count > total_parameter_count || data_count > total_data_count)
1342 exit_server("reply_nttrans: invalid sizes in packet.\n");
1345 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1346 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1347 dump_data(10, setup, setup_count);
1350 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1351 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1352 dump_data(10, params, parameter_count);
1355 memcpy( data, smb_base(inbuf) + data_offset, data_count);
1356 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1357 dump_data(10, data, data_count);
1360 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1361 /* We need to send an interim response then receive the rest
1362 of the parameter/data bytes */
1363 outsize = set_message(outbuf,0,0,True);
1364 send_smb(Client,outbuf);
1366 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1369 ret = receive_next_smb(Client,oplock_sock,inbuf,bufsize,
1370 SMB_SECONDARY_WAIT);
1372 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1373 outsize = set_message(outbuf,0,0,True);
1375 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1377 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1378 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1386 return(ERROR(ERRSRV,ERRerror));
1389 /* Revise total_params and total_data in case they have changed downwards */
1390 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1391 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1392 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1393 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1394 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1395 exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
1397 memcpy( ¶ms[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
1398 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1399 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1400 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1404 if (Protocol >= PROTOCOL_NT1) {
1405 uint16 flg2 = SVAL(outbuf,smb_flg2);
1406 SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
1409 /* Now we must call the relevant NT_TRANS function */
1410 switch(function_code) {
1411 case NT_TRANSACT_CREATE:
1412 outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize,
1413 &setup, ¶ms, &data);
1415 case NT_TRANSACT_IOCTL:
1416 outsize = call_nt_transact_ioctl(conn,
1417 inbuf, outbuf, length, bufsize,
1418 &setup, ¶ms, &data);
1420 case NT_TRANSACT_SET_SECURITY_DESC:
1421 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
1423 &setup, ¶ms, &data);
1425 case NT_TRANSACT_NOTIFY_CHANGE:
1426 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
1428 &setup, ¶ms, &data);
1430 case NT_TRANSACT_RENAME:
1431 outsize = call_nt_transact_rename(conn, inbuf, outbuf, length,
1433 &setup, ¶ms, &data);
1436 case NT_TRANSACT_QUERY_SECURITY_DESC:
1437 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
1439 &setup, ¶ms, &data);
1442 /* Error in request */
1443 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1450 return (ERROR(ERRSRV,ERRerror));
1453 /* As we do not know how many data packets will need to be
1454 returned here the various call_nt_transact_xxxx calls
1455 must send their own. Thus a call_nt_transact_xxxx routine only
1456 returns a value other than -1 when it wants to send
1466 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
1467 calls have already sent it. If outsize != -1 then it is
1468 returning an error packet. */