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 void remove_pending_change_notify_requests_by_mid(int mid);
38 static char *known_nt_pipes[] = {
51 /****************************************************************************
52 Send the required number of replies back.
53 We assume all fields other than the data fields are
54 set correctly for the type of call.
55 HACK ! Always assumes smb_setup field is zero.
56 ****************************************************************************/
58 static int send_nt_replies(char *outbuf, int bufsize, char *params,
59 int paramsize, char *pdata, int datasize)
62 int data_to_send = datasize;
63 int params_to_send = paramsize;
67 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
68 int alignment_offset = 3;
69 int data_alignment_offset = 0;
72 * Initially set the wcnt area to be 18 - this is true for all
76 set_message(outbuf,18,0,True);
79 * If there genuinely are no parameters or data to send just send
83 if(params_to_send == 0 && data_to_send == 0) {
84 send_smb(Client,outbuf);
89 * When sending params and data ensure that both are nicely aligned.
90 * Only do this alignment when there is also data to send - else
91 * can cause NT redirector problems.
94 if (((params_to_send % 4) != 0) && (data_to_send != 0))
95 data_alignment_offset = 4 - (params_to_send % 4);
98 * Space is bufsize minus Netbios over TCP header minus SMB header.
99 * The alignment_offset is to align the param bytes on a four byte
100 * boundary (2 bytes for data len, one byte pad).
101 * NT needs this to work correctly.
104 useable_space = bufsize - ((smb_buf(outbuf)+
105 alignment_offset+data_alignment_offset) -
109 * useable_space can never be more than max_send minus the
113 useable_space = MIN(useable_space,
114 max_send - (alignment_offset+data_alignment_offset));
117 while (params_to_send || data_to_send) {
120 * Calculate whether we will totally or partially fill this packet.
123 total_sent_thistime = params_to_send + data_to_send +
124 alignment_offset + data_alignment_offset;
127 * We can never send more than useable_space.
130 total_sent_thistime = MIN(total_sent_thistime, useable_space);
132 set_message(outbuf, 18, total_sent_thistime, True);
135 * Set total params and data to be sent.
138 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
139 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
142 * Calculate how many parameters and data we can fit into
143 * this packet. Parameters get precedence.
146 params_sent_thistime = MIN(params_to_send,useable_space);
147 data_sent_thistime = useable_space - params_sent_thistime;
148 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
150 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
152 if(params_sent_thistime == 0) {
153 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
154 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
157 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
158 * parameter bytes, however the first 4 bytes of outbuf are
159 * the Netbios over TCP header. Thus use smb_base() to subtract
160 * them from the calculation.
163 SIVAL(outbuf,smb_ntr_ParameterOffset,
164 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
166 * Absolute displacement of param bytes sent in this packet.
169 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
173 * Deal with the data portion.
176 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
178 if(data_sent_thistime == 0) {
179 SIVAL(outbuf,smb_ntr_DataOffset,0);
180 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
183 * The offset of the data bytes is the offset of the
184 * parameter bytes plus the number of parameters being sent this time.
187 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
188 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
189 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
193 * Copy the param bytes into the packet.
196 if(params_sent_thistime)
197 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
200 * Copy in the data bytes
203 if(data_sent_thistime)
204 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
205 data_alignment_offset,pd,data_sent_thistime);
207 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
208 params_sent_thistime, data_sent_thistime, useable_space));
209 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
210 params_to_send, data_to_send, paramsize, datasize));
212 /* Send the packet */
213 send_smb(Client,outbuf);
215 pp += params_sent_thistime;
216 pd += data_sent_thistime;
218 params_to_send -= params_sent_thistime;
219 data_to_send -= data_sent_thistime;
225 if(params_to_send < 0 || data_to_send < 0) {
226 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
227 params_to_send, data_to_send));
235 /****************************************************************************
237 ****************************************************************************/
239 static BOOL saved_case_sensitive;
240 static BOOL saved_case_preserve;
241 static BOOL saved_short_case_preserve;
243 /****************************************************************************
245 ****************************************************************************/
247 static void set_posix_case_semantics(uint32 file_attributes)
249 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
252 saved_case_sensitive = case_sensitive;
253 saved_case_preserve = case_preserve;
254 saved_short_case_preserve = short_case_preserve;
257 case_sensitive = True;
258 case_preserve = True;
259 short_case_preserve = True;
262 /****************************************************************************
263 Restore case semantics.
264 ****************************************************************************/
266 static void restore_case_semantics(uint32 file_attributes)
268 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
271 case_sensitive = saved_case_sensitive;
272 case_preserve = saved_case_preserve;
273 short_case_preserve = saved_short_case_preserve;
276 /****************************************************************************
277 Utility function to map create disposition.
278 ****************************************************************************/
280 static int map_create_disposition( uint32 create_disposition)
282 switch( create_disposition ) {
284 /* create if not exist, fail if exist */
287 case FILE_OVERWRITE_IF:
288 /* create if not exist, trunc if exist */
291 /* fail if not exist, open if exists */
294 /* create if not exist, open if exists */
297 /* fail if not exist, truncate if exists */
300 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
301 create_disposition ));
306 /****************************************************************************
307 Utility function to map share modes.
308 ****************************************************************************/
310 static int map_share_mode( uint32 desired_access, uint32 share_access, uint32 file_attributes)
312 int smb_open_mode = -1;
314 switch( desired_access & (FILE_READ_DATA|FILE_WRITE_DATA) ) {
318 case FILE_WRITE_DATA:
321 case FILE_READ_DATA|FILE_WRITE_DATA:
326 if (smb_open_mode == -1) {
327 if(desired_access & (DELETE_ACCESS|FILE_WRITE_ATTRIBUTES|
328 WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS))
330 else if(desired_access & (FILE_EXECUTE|READ_CONTROL_ACCESS))
333 DEBUG(0,("map_share_mode: Incorrect value for desired_access = %x\n",
339 /* Add in the requested share mode - ignore FILE_SHARE_DELETE for now. */
340 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
341 case FILE_SHARE_READ:
342 smb_open_mode |= (DENY_WRITE<<4);
344 case FILE_SHARE_WRITE:
345 smb_open_mode |= (DENY_READ<<4);
347 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
348 smb_open_mode |= (DENY_NONE<<4);
350 case FILE_SHARE_NONE:
351 smb_open_mode |= (DENY_ALL<<4);
356 * Handle a O_SYNC request.
358 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
359 smb_open_mode |= (1<<14);
361 return smb_open_mode;
364 /****************************************************************************
365 Reply to an NT create and X call on a pipe.
366 ****************************************************************************/
367 static int nt_open_pipe(char *fname, connection_struct *conn,
368 char *inbuf, char *outbuf, int *ppnum)
370 pipes_struct *p = NULL;
372 uint16 vuid = SVAL(inbuf, smb_uid);
375 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
377 /* See if it is one we want to handle. */
378 for( i = 0; known_nt_pipes[i]; i++ )
379 if( strequal(fname,known_nt_pipes[i]))
382 if ( known_nt_pipes[i] == NULL )
383 return(ERROR(ERRSRV,ERRaccess));
385 /* Strip \\ off the name. */
388 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
390 p = open_rpc_pipe_p(fname, conn, vuid);
392 return(ERROR(ERRSRV,ERRnofids));
399 /****************************************************************************
400 Reply to an NT create and X call.
401 ****************************************************************************/
402 int reply_ntcreate_and_X(connection_struct *conn,
403 char *inbuf,char *outbuf,int length,int bufsize)
406 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
407 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
408 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
409 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
410 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
411 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
412 uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
413 ((uint32)sizeof(fname)-1));
416 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
417 /* Breakout the oplock request bits so we can set the
418 reply bits separately. */
419 int oplock_request = 0;
423 SMB_OFF_T file_len = 0;
424 SMB_STRUCT_STAT sbuf;
426 BOOL bad_path = False;
427 files_struct *fsp=NULL;
431 * We need to construct the open_and_X ofun value from the
432 * NT values, as that's what our code is structured to accept.
435 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
436 return(ERROR(ERRDOS,ERRbadaccess));
439 * Now contruct the smb_open_mode value from the desired access
440 * and the share access.
443 if((smb_open_mode = map_share_mode(desired_access,
445 file_attributes)) == -1) {
446 return(ERROR(ERRDOS,ERRbadaccess));
452 StrnCpy(fname,smb_buf(inbuf),fname_len);
453 fname[fname_len] = '\0';
455 /* If it's an IPC, use the pipe handler. */
459 int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
464 * Deal with pipe return.
467 set_message(outbuf,34,0,True);
469 p = outbuf + smb_vwv2;
473 SIVAL(p,0,FILE_WAS_OPENED);
476 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
479 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
481 SSVAL(p,2, 0x5FF); /* ? */
483 DEBUG(5,("reply_ntcreate_and_X: open pipe = %s\n", fname));
485 return chain_reply(inbuf,outbuf,length,bufsize);
489 * Ordinary file or directory.
493 * Check if POSIX semantics are wanted.
496 set_posix_case_semantics(file_attributes);
498 unix_convert(fname,conn,0,&bad_path,NULL);
502 restore_case_semantics(file_attributes);
503 return(ERROR(ERRSRV,ERRnofids));
506 if (!check_name(fname,conn)) {
507 if((errno == ENOENT) && bad_path) {
508 unix_ERR_class = ERRDOS;
509 unix_ERR_code = ERRbadpath;
513 restore_case_semantics(file_attributes);
515 return(UNIXERROR(ERRDOS,ERRnoaccess));
518 unixmode = unix_mode(conn,smb_attr | aARCH);
520 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
521 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
524 * If it's a request for a directory open, deal with it separately.
527 if(create_options & FILE_DIRECTORY_FILE) {
530 open_directory(fsp, conn, fname, smb_ofun,
531 unixmode, &smb_action);
533 restore_case_semantics(file_attributes);
537 return(UNIXERROR(ERRDOS,ERRnoaccess));
541 * Ordinary file case.
544 /* NB. We have a potential bug here. If we
545 * cause an oplock break to ourselves, then we
546 * could end up processing filename related
547 * SMB requests whilst we await the oplock
548 * break response. As we may have changed the
549 * filename case semantics to be POSIX-like,
550 * this could mean a filename request could
551 * fail when it should succeed. This is a rare
552 * condition, but eventually we must arrange
553 * to restore the correct case semantics
554 * before issuing an oplock break request to
555 * our client. JRA. */
557 open_file_shared(fsp,conn,fname,smb_open_mode,
559 oplock_request,&rmode,&smb_action);
562 /* We cheat here. The only case we
563 * care about is a directory rename,
564 * where the NT client will attempt to
565 * open the source directory for
566 * DELETE access. Note that when the
567 * NT client does this it does *not*
568 * set the directory bit in the
569 * request packet. This is translated
570 * into a read/write open
571 * request. POSIX states that any open
572 * for write request on a directory
573 * will generate an EISDIR error, so
574 * we can catch this here and open a
575 * pseudo handle that is flagged as a
578 if(errno == EISDIR) {
581 open_directory(fsp, conn, fname, smb_ofun, unixmode, &smb_action);
585 restore_case_semantics(file_attributes);
586 return(UNIXERROR(ERRDOS,ERRnoaccess));
589 if((errno == ENOENT) && bad_path) {
590 unix_ERR_class = ERRDOS;
591 unix_ERR_code = ERRbadpath;
596 restore_case_semantics(file_attributes);
598 return(UNIXERROR(ERRDOS,ERRnoaccess));
603 if(fsp->is_directory) {
604 if(dos_stat(fsp->fsp_name, &sbuf) != 0) {
605 close_directory(fsp);
606 restore_case_semantics(file_attributes);
607 return(ERROR(ERRDOS,ERRnoaccess));
610 if (sys_fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
611 close_file(fsp,False);
612 restore_case_semantics(file_attributes);
613 return(ERROR(ERRDOS,ERRnoaccess));
617 restore_case_semantics(file_attributes);
619 file_len = sbuf.st_size;
620 fmode = dos_mode(conn,fname,&sbuf);
622 fmode = FILE_ATTRIBUTE_NORMAL;
623 if (!fsp->is_directory && (fmode & aDIR)) {
624 close_file(fsp,False);
625 return(ERROR(ERRDOS,ERRnoaccess));
629 * If the caller set the extended oplock request bit
630 * and we granted one (by whatever means) - set the
631 * correct bit for extended oplock reply.
634 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
635 smb_action |= EXTENDED_OPLOCK_GRANTED;
637 if(oplock_request && fsp->granted_oplock)
638 smb_action |= EXTENDED_OPLOCK_GRANTED;
640 set_message(outbuf,34,0,True);
642 p = outbuf + smb_vwv2;
645 * Currently as we don't support level II oplocks we just report
646 * exclusive & batch here.
649 SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
651 SSVAL(p,0,fsp->fnum);
653 SIVAL(p,0,smb_action);
657 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
659 put_long_date(p,sbuf.st_atime); /* access time */
661 put_long_date(p,sbuf.st_mtime); /* write time */
663 put_long_date(p,sbuf.st_mtime); /* change time */
665 SIVAL(p,0,fmode); /* File Attributes. */
667 #ifdef LARGE_SMB_OFF_T
668 SIVAL(p,0, file_len);
669 SIVAL(p,4, file_len >> 32);
670 #else /* LARGE_SMB_OFF_T */
672 #endif /* LARGE_SMB_OFF_T */
674 #ifdef LARGE_SMB_OFF_T
675 SIVAL(p,0, file_len);
676 SIVAL(p,4, file_len >> 32);
677 #else /* LARGE_SMB_OFF_T */
679 #endif /* LARGE_SMB_OFF_T */
681 SCVAL(p,0,fsp->is_directory ? 1 : 0);
683 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
685 return chain_reply(inbuf,outbuf,length,bufsize);
688 /****************************************************************************
689 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
690 ****************************************************************************/
691 static int call_nt_transact_create(connection_struct *conn,
692 char *inbuf, char *outbuf, int length,
694 char **ppsetup, char **ppparams,
698 char *params = *ppparams;
699 uint32 flags = IVAL(params,0);
700 uint32 desired_access = IVAL(params,8);
701 uint32 file_attributes = IVAL(params,20);
702 uint32 share_access = IVAL(params,24);
703 uint32 create_disposition = IVAL(params,28);
704 uint32 create_options = IVAL(params,32);
705 uint32 fname_len = MIN(((uint32)IVAL(params,44)),
706 ((uint32)sizeof(fname)-1));
709 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
710 /* Breakout the oplock request bits so we can set the
711 reply bits separately. */
712 int oplock_request = 0;
716 SMB_OFF_T file_len = 0;
717 SMB_STRUCT_STAT sbuf;
719 BOOL bad_path = False;
720 files_struct *fsp = NULL;
724 * We need to construct the open_and_X ofun value from the
725 * NT values, as that's what our code is structured to accept.
728 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
729 return(ERROR(ERRDOS,ERRbadaccess));
732 * Now contruct the smb_open_mode value from the desired access
733 * and the share access.
736 if((smb_open_mode = map_share_mode( desired_access, share_access, file_attributes)) == -1)
737 return(ERROR(ERRDOS,ERRbadaccess));
743 StrnCpy(fname,params+53,fname_len);
744 fname[fname_len] = '\0';
746 /* If it's an IPC, use the pipe handler. */
748 int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
751 smb_action = FILE_WAS_OPENED;
754 * Check if POSIX semantics are wanted.
757 set_posix_case_semantics(file_attributes);
759 unix_convert(fname,conn,0,&bad_path,NULL);
763 restore_case_semantics(file_attributes);
764 return(ERROR(ERRSRV,ERRnofids));
767 if (!check_name(fname,conn)) {
768 if((errno == ENOENT) && bad_path) {
769 unix_ERR_class = ERRDOS;
770 unix_ERR_code = ERRbadpath;
774 restore_case_semantics(file_attributes);
776 return(UNIXERROR(ERRDOS,ERRnoaccess));
779 unixmode = unix_mode(conn,smb_attr | aARCH);
781 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
782 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
785 * If it's a request for a directory open, deal with it separately.
788 if(create_options & FILE_DIRECTORY_FILE) {
793 * We will get a create directory here if the Win32
794 * app specified a security descriptor in the
795 * CreateDirectory() call.
798 open_directory(fsp, conn, fname, smb_ofun, unixmode, &smb_action);
802 return(UNIXERROR(ERRDOS,ERRnoaccess));
807 * Ordinary file case.
810 open_file_shared(fsp,conn,fname,smb_open_mode,smb_ofun,unixmode,
811 oplock_request,&rmode,&smb_action);
814 if((errno == ENOENT) && bad_path) {
815 unix_ERR_class = ERRDOS;
816 unix_ERR_code = ERRbadpath;
820 restore_case_semantics(file_attributes);
822 return(UNIXERROR(ERRDOS,ERRnoaccess));
825 if (sys_fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
826 close_file(fsp,False);
828 restore_case_semantics(file_attributes);
830 return(ERROR(ERRDOS,ERRnoaccess));
833 file_len = sbuf.st_size;
834 fmode = dos_mode(conn,fname,&sbuf);
836 fmode = FILE_ATTRIBUTE_NORMAL;
839 close_file(fsp,False);
840 restore_case_semantics(file_attributes);
841 return(ERROR(ERRDOS,ERRnoaccess));
845 * If the caller set the extended oplock request bit
846 * and we granted one (by whatever means) - set the
847 * correct bit for extended oplock reply.
850 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
851 smb_action |= EXTENDED_OPLOCK_GRANTED;
853 if(oplock_request && fsp->granted_oplock)
854 smb_action |= EXTENDED_OPLOCK_GRANTED;
858 restore_case_semantics(file_attributes);
860 /* Realloc the size of parameters and data we will return */
861 params = *ppparams = Realloc(*ppparams, 69);
863 return(ERROR(ERRDOS,ERRnomem));
866 SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
871 SSVAL(p,0,fsp->fnum);
874 SIVAL(p,0,smb_action);
879 * Deal with pipe return.
882 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
885 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
887 SSVAL(p,2, 0x5FF); /* ? */
890 * Deal with file return.
893 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
895 put_long_date(p,sbuf.st_atime); /* access time */
897 put_long_date(p,sbuf.st_mtime); /* write time */
899 put_long_date(p,sbuf.st_mtime); /* change time */
901 SIVAL(p,0,fmode); /* File Attributes. */
903 #ifdef LARGE_SMB_OFF_T
904 SIVAL(p,0, file_len);
905 SIVAL(p,4, (file_len >> 32));
906 #else /* LARGE_SMB_OFF_T */
908 #endif /* LARGE_SMB_OFF_T */
910 #ifdef LARGE_SMB_OFF_T
911 SIVAL(p,0, file_len);
912 SIVAL(p,4, (file_len >> 32));
913 #else /* LARGE_SMB_OFF_T */
915 #endif /* LARGE_SMB_OFF_T */
918 /* Send the required number of replies */
919 send_nt_replies(outbuf, bufsize, params, 69, *ppdata, 0);
924 /****************************************************************************
925 Reply to a NT CANCEL request.
926 ****************************************************************************/
927 int reply_ntcancel(connection_struct *conn,
928 char *inbuf,char *outbuf,int length,int bufsize)
931 * Go through and cancel any pending change notifies.
934 int mid = SVAL(inbuf,smb_mid);
935 remove_pending_change_notify_requests_by_mid(mid);
936 remove_pending_lock_requests_by_mid(mid);
938 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
943 /****************************************************************************
944 Reply to an unsolicited SMBNTtranss - just ignore it!
945 ****************************************************************************/
946 int reply_nttranss(connection_struct *conn,
947 char *inbuf,char *outbuf,int length,int bufsize)
949 DEBUG(4,("Ignoring nttranss of length %d\n",length));
953 /****************************************************************************
954 Reply to an NT transact rename command.
955 ****************************************************************************/
956 static int call_nt_transact_rename(connection_struct *conn,
957 char *inbuf, char *outbuf, int length,
959 char **ppsetup, char **ppparams, char **ppdata)
961 char *params = *ppparams;
963 files_struct *fsp = file_fsp(params, 0);
964 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
965 uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
966 ((uint32)sizeof(new_name)-1));
969 CHECK_FSP(fsp, conn);
970 StrnCpy(new_name,params+4,fname_len);
971 new_name[fname_len] = '\0';
973 outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
974 new_name, replace_if_exists);
977 * Rename was successful.
979 send_nt_replies(outbuf, bufsize, NULL, 0, NULL, 0);
981 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
982 fsp->fsp_name, new_name));
990 /****************************************************************************
991 This is the structure to queue to implement NT change
992 notify. It consists of smb_size bytes stored from the
993 transact command (to keep the mid, tid etc around).
994 Plus the fid to examine and the time to check next.
995 *****************************************************************************/
1000 connection_struct *conn;
1001 time_t next_check_time;
1002 time_t modify_time; /* Info from the directory we're monitoring. */
1003 time_t status_time; /* Info from the directory we're monitoring. */
1004 char request_buf[smb_size];
1005 } change_notify_buf;
1007 static ubi_slList change_notify_queue = { NULL, (ubi_slNodePtr)&change_notify_queue, 0};
1009 /****************************************************************************
1010 Setup the common parts of the return packet and send it.
1011 *****************************************************************************/
1013 static void change_notify_reply_packet(char *inbuf, int error_class, uint32 error_code)
1016 char outbuf[smb_size+38];
1018 memset(outbuf, '\0', sizeof(outbuf));
1019 construct_reply_common(inbuf, outbuf);
1022 * If we're returning a 'too much in the directory changed' we need to
1023 * set this is an NT error status flags. If we don't then the (probably
1024 * untested) code in the NT redirector has a bug in that it doesn't re-issue
1025 * the change notify.... Ah - I *love* it when I get so deeply into this I
1026 * can even determine how MS failed to test stuff and why.... :-). JRA.
1029 if(error_class == 0) /* NT Error. */
1030 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1032 ERROR(error_class,error_code);
1035 * Seems NT needs a transact command with an error code
1036 * in it. This is a longer packet than a simple error.
1038 set_message(outbuf,18,0,False);
1040 send_smb(Client,outbuf);
1043 /****************************************************************************
1044 Delete entries by fnum from the change notify pending queue.
1045 *****************************************************************************/
1046 void remove_pending_change_notify_requests_by_fid(files_struct *fsp)
1048 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1049 change_notify_buf *prev = NULL;
1051 while(cnbp != NULL) {
1052 if(cnbp->fsp->fnum == fsp->fnum) {
1053 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1054 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1059 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1063 /****************************************************************************
1064 Delete entries by mid from the change notify pending queue. Always send reply.
1065 *****************************************************************************/
1067 static void remove_pending_change_notify_requests_by_mid(int mid)
1069 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1070 change_notify_buf *prev = NULL;
1072 while(cnbp != NULL) {
1073 if(SVAL(cnbp->request_buf,smb_mid) == mid) {
1074 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_CANCELLED);
1075 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1076 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1081 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1085 /****************************************************************************
1086 Process the change notify queue. Note that this is only called as root.
1087 *****************************************************************************/
1089 void process_pending_change_notify_queue(time_t t)
1091 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1092 change_notify_buf *prev = NULL;
1097 if(cnbp->next_check_time >= t)
1101 * It's time to check. Go through the queue and see if
1102 * the timestamps changed.
1105 while((cnbp != NULL) && (cnbp->next_check_time <= t)) {
1107 files_struct *fsp = cnbp->fsp;
1108 connection_struct *conn = cnbp->conn;
1109 uint16 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
1110 SVAL(cnbp->request_buf,smb_uid);
1113 * Ensure we don't have any old chain_fsp values
1114 * sitting around....
1119 if(!become_user(conn,vuid)) {
1120 DEBUG(0,("process_pending_change_notify_queue: Unable to become user vuid=%d.\n",
1123 * Remove the entry and return an error to the client.
1125 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1126 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1127 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1131 if(!become_service(conn,True)) {
1132 DEBUG(0,("process_pending_change_notify_queue: Unable to become service Error was %s.\n", strerror(errno) ));
1134 * Remove the entry and return an error to the client.
1136 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1137 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1138 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1143 if(dos_stat(fsp->fsp_name, &st) < 0) {
1144 DEBUG(0,("process_pending_change_notify_queue: Unable to stat directory %s. \
1145 Error was %s.\n", fsp->fsp_name, strerror(errno) ));
1147 * Remove the entry and return an error to the client.
1149 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1150 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1151 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1156 if(cnbp->modify_time != st.st_mtime ||
1157 cnbp->status_time != st.st_ctime) {
1159 * Remove the entry and return a change notify to the client.
1161 DEBUG(5,("process_pending_change_notify_queue: directory name = %s changed\n",
1163 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_NOTIFY_ENUM_DIR);
1164 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1165 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1173 * Move to the next in the list.
1176 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1180 /****************************************************************************
1181 Reply to a notify change - queue the request and
1182 don't allow a directory to be opened.
1183 ****************************************************************************/
1184 static int call_nt_transact_notify_change(connection_struct *conn,
1185 char *inbuf, char *outbuf, int length,
1188 char **ppparams, char **ppdata)
1190 char *setup = *ppsetup;
1192 change_notify_buf *cnbp;
1195 fsp = file_fsp(setup,4);
1197 DEBUG(3,("call_nt_transact_notify_change\n"));
1200 return(ERROR(ERRDOS,ERRbadfid));
1202 if((!fsp->open) || (!fsp->is_directory) || (conn != fsp->conn))
1203 return(ERROR(ERRDOS,ERRbadfid));
1206 * Now queue an entry on the notify change stack. We timestamp
1207 * the entry we are adding so that we know when to scan next.
1208 * We only need to save smb_size bytes from this incoming packet
1209 * as we will always by returning a 'read the directory yourself'
1213 if((cnbp = (change_notify_buf *)malloc(sizeof(change_notify_buf))) == NULL) {
1214 DEBUG(0,("call_nt_transact_notify_change: Malloc fail (2) !\n" ));
1219 * Store the current timestamp on the directory we are monitoring.
1222 if(dos_stat(fsp->fsp_name, &st) < 0) {
1223 DEBUG(0,("call_nt_transact_notify_change: Unable to stat name = %s. \
1224 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1226 return(UNIXERROR(ERRDOS,ERRbadfid));
1229 memcpy(cnbp->request_buf, inbuf, smb_size);
1232 cnbp->modify_time = st.st_mtime;
1233 cnbp->status_time = st.st_ctime;
1235 cnbp->next_check_time = time(NULL) + lp_change_notify_timeout();
1238 * Adding to the tail enables us to check only
1239 * the head when scanning for change, as this entry
1240 * is forced to have the first timeout expiration.
1243 ubi_slAddTail(&change_notify_queue, cnbp);
1245 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1246 name = %s\n", fsp->fsp_name ));
1251 /****************************************************************************
1252 Reply to query a security descriptor - currently this is not implemented (it
1253 is planned to be though).
1254 ****************************************************************************/
1255 static int call_nt_transact_query_security_desc(connection_struct *conn,
1256 char *inbuf, char *outbuf,
1259 char **ppsetup, char **ppparams, char **ppdata)
1261 DEBUG(0,("call_nt_transact_query_security_desc: Currently not implemented.\n"));
1262 return(ERROR(ERRSRV,ERRnosupport));
1265 /****************************************************************************
1266 Reply to set a security descriptor - currently this is not implemented (it
1267 is planned to be though).
1268 ****************************************************************************/
1269 static int call_nt_transact_set_security_desc(connection_struct *conn,
1270 char *inbuf, char *outbuf,
1274 char **ppparams, char **ppdata)
1276 DEBUG(0,("call_nt_transact_set_security_desc: Currently not implemented.\n"));
1277 return(ERROR(ERRSRV,ERRnosupport));
1280 /****************************************************************************
1281 Reply to IOCTL - not implemented - no plans.
1282 ****************************************************************************/
1283 static int call_nt_transact_ioctl(connection_struct *conn,
1284 char *inbuf, char *outbuf, int length,
1286 char **ppsetup, char **ppparams, char **ppdata)
1288 DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1289 return(ERROR(ERRSRV,ERRnosupport));
1292 /****************************************************************************
1293 Reply to a SMBNTtrans.
1294 ****************************************************************************/
1295 int reply_nttrans(connection_struct *conn,
1296 char *inbuf,char *outbuf,int length,int bufsize)
1299 #if 0 /* Not used. */
1300 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1301 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1302 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1303 #endif /* Not used. */
1304 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1305 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1306 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1307 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1308 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1309 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1310 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1311 uint16 function_code = SVAL( inbuf, smb_nt_Function);
1312 char *params = NULL, *data = NULL, *setup = NULL;
1313 uint32 num_params_sofar, num_data_sofar;
1315 if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1317 * Queue this open message as we are the process of an oplock break.
1320 DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1321 due to being in oplock break state.\n" ));
1323 push_oplock_pending_smb_message( inbuf, length);
1327 outsize = set_message(outbuf,0,0,True);
1330 * All nttrans messages we handle have smb_wct == 19 + setup_count.
1331 * Ensure this is so as a sanity check.
1334 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1335 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1336 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1337 return(ERROR(ERRSRV,ERRerror));
1340 /* Allocate the space for the setup, the maximum needed parameters and data */
1343 setup = (char *)malloc(setup_count);
1344 if (total_parameter_count > 0)
1345 params = (char *)malloc(total_parameter_count);
1346 if (total_data_count > 0)
1347 data = (char *)malloc(total_data_count);
1349 if ((total_parameter_count && !params) || (total_data_count && !data) ||
1350 (setup_count && !setup)) {
1351 DEBUG(0,("reply_nttrans : Out of memory\n"));
1352 return(ERROR(ERRDOS,ERRnomem));
1355 /* Copy the param and data bytes sent with this request into
1356 the params buffer */
1357 num_params_sofar = parameter_count;
1358 num_data_sofar = data_count;
1360 if (parameter_count > total_parameter_count || data_count > total_data_count)
1361 exit_server("reply_nttrans: invalid sizes in packet.\n");
1364 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1365 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1366 dump_data(10, setup, setup_count);
1369 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1370 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1371 dump_data(10, params, parameter_count);
1374 memcpy( data, smb_base(inbuf) + data_offset, data_count);
1375 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1376 dump_data(10, data, data_count);
1379 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1380 /* We need to send an interim response then receive the rest
1381 of the parameter/data bytes */
1382 outsize = set_message(outbuf,0,0,True);
1383 send_smb(Client,outbuf);
1385 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1388 ret = receive_next_smb(Client,oplock_sock,inbuf,bufsize,
1389 SMB_SECONDARY_WAIT);
1391 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1392 outsize = set_message(outbuf,0,0,True);
1394 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1396 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1397 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1405 return(ERROR(ERRSRV,ERRerror));
1408 /* Revise total_params and total_data in case they have changed downwards */
1409 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1410 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1411 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1412 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1413 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1414 exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
1416 memcpy( ¶ms[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
1417 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1418 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1419 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1423 if (Protocol >= PROTOCOL_NT1) {
1424 uint16 flg2 = SVAL(outbuf,smb_flg2);
1425 SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
1428 /* Now we must call the relevant NT_TRANS function */
1429 switch(function_code) {
1430 case NT_TRANSACT_CREATE:
1431 outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize,
1432 &setup, ¶ms, &data);
1434 case NT_TRANSACT_IOCTL:
1435 outsize = call_nt_transact_ioctl(conn,
1436 inbuf, outbuf, length, bufsize,
1437 &setup, ¶ms, &data);
1439 case NT_TRANSACT_SET_SECURITY_DESC:
1440 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
1442 &setup, ¶ms, &data);
1444 case NT_TRANSACT_NOTIFY_CHANGE:
1445 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
1447 &setup, ¶ms, &data);
1449 case NT_TRANSACT_RENAME:
1450 outsize = call_nt_transact_rename(conn, inbuf, outbuf, length,
1452 &setup, ¶ms, &data);
1455 case NT_TRANSACT_QUERY_SECURITY_DESC:
1456 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
1458 &setup, ¶ms, &data);
1461 /* Error in request */
1462 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1469 return (ERROR(ERRSRV,ERRerror));
1472 /* As we do not know how many data packets will need to be
1473 returned here the various call_nt_transact_xxxx calls
1474 must send their own. Thus a call_nt_transact_xxxx routine only
1475 returns a value other than -1 when it wants to send
1485 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
1486 calls have already sent it. If outsize != -1 then it is
1487 returning an error packet. */