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 smb_read_error;
29 extern int global_oplock_break;
30 extern int chain_size;
31 extern BOOL case_sensitive;
32 extern BOOL case_preserve;
33 extern BOOL short_case_preserve;
35 static void remove_pending_change_notify_requests_by_mid(int mid);
37 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 SOFF_T(p, 0, file_len);
669 SOFF_T(p,0,file_len);
671 SCVAL(p,0,fsp->is_directory ? 1 : 0);
673 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
675 return chain_reply(inbuf,outbuf,length,bufsize);
678 /****************************************************************************
679 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
680 ****************************************************************************/
681 static int call_nt_transact_create(connection_struct *conn,
682 char *inbuf, char *outbuf, int length,
684 char **ppsetup, char **ppparams,
688 char *params = *ppparams;
689 uint32 flags = IVAL(params,0);
690 uint32 desired_access = IVAL(params,8);
691 uint32 file_attributes = IVAL(params,20);
692 uint32 share_access = IVAL(params,24);
693 uint32 create_disposition = IVAL(params,28);
694 uint32 create_options = IVAL(params,32);
695 uint32 fname_len = MIN(((uint32)IVAL(params,44)),
696 ((uint32)sizeof(fname)-1));
699 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
700 /* Breakout the oplock request bits so we can set the
701 reply bits separately. */
702 int oplock_request = 0;
706 SMB_OFF_T file_len = 0;
707 SMB_STRUCT_STAT sbuf;
709 BOOL bad_path = False;
710 files_struct *fsp = NULL;
714 * We need to construct the open_and_X ofun value from the
715 * NT values, as that's what our code is structured to accept.
718 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
719 return(ERROR(ERRDOS,ERRbadaccess));
722 * Now contruct the smb_open_mode value from the desired access
723 * and the share access.
726 if((smb_open_mode = map_share_mode( desired_access, share_access, file_attributes)) == -1)
727 return(ERROR(ERRDOS,ERRbadaccess));
733 StrnCpy(fname,params+53,fname_len);
734 fname[fname_len] = '\0';
736 /* If it's an IPC, use the pipe handler. */
738 int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
741 smb_action = FILE_WAS_OPENED;
744 * Check if POSIX semantics are wanted.
747 set_posix_case_semantics(file_attributes);
749 unix_convert(fname,conn,0,&bad_path,NULL);
753 restore_case_semantics(file_attributes);
754 return(ERROR(ERRSRV,ERRnofids));
757 if (!check_name(fname,conn)) {
758 if((errno == ENOENT) && bad_path) {
759 unix_ERR_class = ERRDOS;
760 unix_ERR_code = ERRbadpath;
764 restore_case_semantics(file_attributes);
766 return(UNIXERROR(ERRDOS,ERRnoaccess));
769 unixmode = unix_mode(conn,smb_attr | aARCH);
771 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
772 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
775 * If it's a request for a directory open, deal with it separately.
778 if(create_options & FILE_DIRECTORY_FILE) {
783 * We will get a create directory here if the Win32
784 * app specified a security descriptor in the
785 * CreateDirectory() call.
788 open_directory(fsp, conn, fname, smb_ofun, unixmode, &smb_action);
792 return(UNIXERROR(ERRDOS,ERRnoaccess));
797 * Ordinary file case.
800 open_file_shared(fsp,conn,fname,smb_open_mode,smb_ofun,unixmode,
801 oplock_request,&rmode,&smb_action);
804 if((errno == ENOENT) && bad_path) {
805 unix_ERR_class = ERRDOS;
806 unix_ERR_code = ERRbadpath;
810 restore_case_semantics(file_attributes);
812 return(UNIXERROR(ERRDOS,ERRnoaccess));
815 if (sys_fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
816 close_file(fsp,False);
818 restore_case_semantics(file_attributes);
820 return(ERROR(ERRDOS,ERRnoaccess));
823 file_len = sbuf.st_size;
824 fmode = dos_mode(conn,fname,&sbuf);
826 fmode = FILE_ATTRIBUTE_NORMAL;
829 close_file(fsp,False);
830 restore_case_semantics(file_attributes);
831 return(ERROR(ERRDOS,ERRnoaccess));
835 * If the caller set the extended oplock request bit
836 * and we granted one (by whatever means) - set the
837 * correct bit for extended oplock reply.
840 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
841 smb_action |= EXTENDED_OPLOCK_GRANTED;
843 if(oplock_request && fsp->granted_oplock)
844 smb_action |= EXTENDED_OPLOCK_GRANTED;
848 restore_case_semantics(file_attributes);
850 /* Realloc the size of parameters and data we will return */
851 params = *ppparams = Realloc(*ppparams, 69);
853 return(ERROR(ERRDOS,ERRnomem));
856 SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
861 SSVAL(p,0,fsp->fnum);
864 SIVAL(p,0,smb_action);
869 * Deal with pipe return.
872 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
875 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
877 SSVAL(p,2, 0x5FF); /* ? */
880 * Deal with file return.
883 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
885 put_long_date(p,sbuf.st_atime); /* access time */
887 put_long_date(p,sbuf.st_mtime); /* write time */
889 put_long_date(p,sbuf.st_mtime); /* change time */
891 SIVAL(p,0,fmode); /* File Attributes. */
893 SOFF_T(p,0,file_len);
895 SOFF_T(p,0,file_len);
898 /* Send the required number of replies */
899 send_nt_replies(outbuf, bufsize, params, 69, *ppdata, 0);
904 /****************************************************************************
905 Reply to a NT CANCEL request.
906 ****************************************************************************/
907 int reply_ntcancel(connection_struct *conn,
908 char *inbuf,char *outbuf,int length,int bufsize)
911 * Go through and cancel any pending change notifies.
914 int mid = SVAL(inbuf,smb_mid);
915 remove_pending_change_notify_requests_by_mid(mid);
916 remove_pending_lock_requests_by_mid(mid);
918 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
923 /****************************************************************************
924 Reply to an unsolicited SMBNTtranss - just ignore it!
925 ****************************************************************************/
926 int reply_nttranss(connection_struct *conn,
927 char *inbuf,char *outbuf,int length,int bufsize)
929 DEBUG(4,("Ignoring nttranss of length %d\n",length));
933 /****************************************************************************
934 Reply to an NT transact rename command.
935 ****************************************************************************/
936 static int call_nt_transact_rename(connection_struct *conn,
937 char *inbuf, char *outbuf, int length,
939 char **ppsetup, char **ppparams, char **ppdata)
941 char *params = *ppparams;
943 files_struct *fsp = file_fsp(params, 0);
944 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
945 uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
946 ((uint32)sizeof(new_name)-1));
949 CHECK_FSP(fsp, conn);
950 StrnCpy(new_name,params+4,fname_len);
951 new_name[fname_len] = '\0';
953 outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
954 new_name, replace_if_exists);
957 * Rename was successful.
959 send_nt_replies(outbuf, bufsize, NULL, 0, NULL, 0);
961 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
962 fsp->fsp_name, new_name));
970 /****************************************************************************
971 This is the structure to queue to implement NT change
972 notify. It consists of smb_size bytes stored from the
973 transact command (to keep the mid, tid etc around).
974 Plus the fid to examine and the time to check next.
975 *****************************************************************************/
980 connection_struct *conn;
981 time_t next_check_time;
982 time_t modify_time; /* Info from the directory we're monitoring. */
983 time_t status_time; /* Info from the directory we're monitoring. */
984 char request_buf[smb_size];
987 static ubi_slList change_notify_queue = { NULL, (ubi_slNodePtr)&change_notify_queue, 0};
989 /****************************************************************************
990 Setup the common parts of the return packet and send it.
991 *****************************************************************************/
993 static void change_notify_reply_packet(char *inbuf, int error_class, uint32 error_code)
996 char outbuf[smb_size+38];
998 memset(outbuf, '\0', sizeof(outbuf));
999 construct_reply_common(inbuf, outbuf);
1002 * If we're returning a 'too much in the directory changed' we need to
1003 * set this is an NT error status flags. If we don't then the (probably
1004 * untested) code in the NT redirector has a bug in that it doesn't re-issue
1005 * the change notify.... Ah - I *love* it when I get so deeply into this I
1006 * can even determine how MS failed to test stuff and why.... :-). JRA.
1009 if(error_class == 0) /* NT Error. */
1010 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1012 ERROR(error_class,error_code);
1015 * Seems NT needs a transact command with an error code
1016 * in it. This is a longer packet than a simple error.
1018 set_message(outbuf,18,0,False);
1020 send_smb(Client,outbuf);
1023 /****************************************************************************
1024 Delete entries by fnum from the change notify pending queue.
1025 *****************************************************************************/
1026 void remove_pending_change_notify_requests_by_fid(files_struct *fsp)
1028 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1029 change_notify_buf *prev = NULL;
1031 while(cnbp != NULL) {
1032 if(cnbp->fsp->fnum == fsp->fnum) {
1033 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1034 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1039 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1043 /****************************************************************************
1044 Delete entries by mid from the change notify pending queue. Always send reply.
1045 *****************************************************************************/
1047 static void remove_pending_change_notify_requests_by_mid(int mid)
1049 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1050 change_notify_buf *prev = NULL;
1052 while(cnbp != NULL) {
1053 if(SVAL(cnbp->request_buf,smb_mid) == mid) {
1054 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_CANCELLED);
1055 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1056 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1061 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1065 /****************************************************************************
1066 Process the change notify queue. Note that this is only called as root.
1067 *****************************************************************************/
1069 void process_pending_change_notify_queue(time_t t)
1071 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1072 change_notify_buf *prev = NULL;
1077 if(cnbp->next_check_time >= t)
1081 * It's time to check. Go through the queue and see if
1082 * the timestamps changed.
1085 while((cnbp != NULL) && (cnbp->next_check_time <= t)) {
1087 files_struct *fsp = cnbp->fsp;
1088 connection_struct *conn = cnbp->conn;
1089 uint16 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
1090 SVAL(cnbp->request_buf,smb_uid);
1093 * Ensure we don't have any old chain_fsp values
1094 * sitting around....
1099 if(!become_user(conn,vuid)) {
1100 DEBUG(0,("process_pending_change_notify_queue: Unable to become user vuid=%d.\n",
1103 * Remove the entry and return an error to the client.
1105 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1106 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1107 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1111 if(!become_service(conn,True)) {
1112 DEBUG(0,("process_pending_change_notify_queue: Unable to become service Error was %s.\n", strerror(errno) ));
1114 * Remove the entry and return an error to the client.
1116 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1117 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1118 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1123 if(dos_stat(fsp->fsp_name, &st) < 0) {
1124 DEBUG(0,("process_pending_change_notify_queue: Unable to stat directory %s. \
1125 Error was %s.\n", fsp->fsp_name, strerror(errno) ));
1127 * Remove the entry and return an error to the client.
1129 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1130 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1131 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1136 if(cnbp->modify_time != st.st_mtime ||
1137 cnbp->status_time != st.st_ctime) {
1139 * Remove the entry and return a change notify to the client.
1141 DEBUG(5,("process_pending_change_notify_queue: directory name = %s changed\n",
1143 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_NOTIFY_ENUM_DIR);
1144 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1145 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1153 * Move to the next in the list.
1156 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1160 /****************************************************************************
1161 Reply to a notify change - queue the request and
1162 don't allow a directory to be opened.
1163 ****************************************************************************/
1164 static int call_nt_transact_notify_change(connection_struct *conn,
1165 char *inbuf, char *outbuf, int length,
1168 char **ppparams, char **ppdata)
1170 char *setup = *ppsetup;
1172 change_notify_buf *cnbp;
1175 fsp = file_fsp(setup,4);
1177 DEBUG(3,("call_nt_transact_notify_change\n"));
1180 return(ERROR(ERRDOS,ERRbadfid));
1182 if((!fsp->open) || (!fsp->is_directory) || (conn != fsp->conn))
1183 return(ERROR(ERRDOS,ERRbadfid));
1186 * Now queue an entry on the notify change stack. We timestamp
1187 * the entry we are adding so that we know when to scan next.
1188 * We only need to save smb_size bytes from this incoming packet
1189 * as we will always by returning a 'read the directory yourself'
1193 if((cnbp = (change_notify_buf *)malloc(sizeof(change_notify_buf))) == NULL) {
1194 DEBUG(0,("call_nt_transact_notify_change: Malloc fail (2) !\n" ));
1199 * Store the current timestamp on the directory we are monitoring.
1202 if(dos_stat(fsp->fsp_name, &st) < 0) {
1203 DEBUG(0,("call_nt_transact_notify_change: Unable to stat name = %s. \
1204 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1206 return(UNIXERROR(ERRDOS,ERRbadfid));
1209 memcpy(cnbp->request_buf, inbuf, smb_size);
1212 cnbp->modify_time = st.st_mtime;
1213 cnbp->status_time = st.st_ctime;
1215 cnbp->next_check_time = time(NULL) + lp_change_notify_timeout();
1218 * Adding to the tail enables us to check only
1219 * the head when scanning for change, as this entry
1220 * is forced to have the first timeout expiration.
1223 ubi_slAddTail(&change_notify_queue, cnbp);
1225 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1226 name = %s\n", fsp->fsp_name ));
1231 /****************************************************************************
1232 Reply to query a security descriptor - currently this is not implemented (it
1233 is planned to be though).
1234 ****************************************************************************/
1235 static int call_nt_transact_query_security_desc(connection_struct *conn,
1236 char *inbuf, char *outbuf,
1239 char **ppsetup, char **ppparams, char **ppdata)
1241 DEBUG(0,("call_nt_transact_query_security_desc: Currently not implemented.\n"));
1242 return(ERROR(ERRSRV,ERRnosupport));
1245 /****************************************************************************
1246 Reply to set a security descriptor - currently this is not implemented (it
1247 is planned to be though).
1248 ****************************************************************************/
1249 static int call_nt_transact_set_security_desc(connection_struct *conn,
1250 char *inbuf, char *outbuf,
1254 char **ppparams, char **ppdata)
1256 DEBUG(0,("call_nt_transact_set_security_desc: Currently not implemented.\n"));
1257 return(ERROR(ERRSRV,ERRnosupport));
1260 /****************************************************************************
1261 Reply to IOCTL - not implemented - no plans.
1262 ****************************************************************************/
1263 static int call_nt_transact_ioctl(connection_struct *conn,
1264 char *inbuf, char *outbuf, int length,
1266 char **ppsetup, char **ppparams, char **ppdata)
1268 DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1269 return(ERROR(ERRSRV,ERRnosupport));
1272 /****************************************************************************
1273 Reply to a SMBNTtrans.
1274 ****************************************************************************/
1275 int reply_nttrans(connection_struct *conn,
1276 char *inbuf,char *outbuf,int length,int bufsize)
1279 #if 0 /* Not used. */
1280 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1281 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1282 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1283 #endif /* Not used. */
1284 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1285 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1286 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1287 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1288 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1289 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1290 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1291 uint16 function_code = SVAL( inbuf, smb_nt_Function);
1292 char *params = NULL, *data = NULL, *setup = NULL;
1293 uint32 num_params_sofar, num_data_sofar;
1295 if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1297 * Queue this open message as we are the process of an oplock break.
1300 DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1301 due to being in oplock break state.\n" ));
1303 push_oplock_pending_smb_message( inbuf, length);
1307 outsize = set_message(outbuf,0,0,True);
1310 * All nttrans messages we handle have smb_wct == 19 + setup_count.
1311 * Ensure this is so as a sanity check.
1314 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1315 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1316 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1317 return(ERROR(ERRSRV,ERRerror));
1320 /* Allocate the space for the setup, the maximum needed parameters and data */
1323 setup = (char *)malloc(setup_count);
1324 if (total_parameter_count > 0)
1325 params = (char *)malloc(total_parameter_count);
1326 if (total_data_count > 0)
1327 data = (char *)malloc(total_data_count);
1329 if ((total_parameter_count && !params) || (total_data_count && !data) ||
1330 (setup_count && !setup)) {
1331 DEBUG(0,("reply_nttrans : Out of memory\n"));
1332 return(ERROR(ERRDOS,ERRnomem));
1335 /* Copy the param and data bytes sent with this request into
1336 the params buffer */
1337 num_params_sofar = parameter_count;
1338 num_data_sofar = data_count;
1340 if (parameter_count > total_parameter_count || data_count > total_data_count)
1341 exit_server("reply_nttrans: invalid sizes in packet.\n");
1344 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1345 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1346 dump_data(10, setup, setup_count);
1349 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1350 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1351 dump_data(10, params, parameter_count);
1354 memcpy( data, smb_base(inbuf) + data_offset, data_count);
1355 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1356 dump_data(10, data, data_count);
1359 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1360 /* We need to send an interim response then receive the rest
1361 of the parameter/data bytes */
1362 outsize = set_message(outbuf,0,0,True);
1363 send_smb(Client,outbuf);
1365 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1368 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
1370 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1371 outsize = set_message(outbuf,0,0,True);
1373 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1375 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1376 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1384 return(ERROR(ERRSRV,ERRerror));
1387 /* Revise total_params and total_data in case they have changed downwards */
1388 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1389 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1390 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1391 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1392 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1393 exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
1395 memcpy( ¶ms[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
1396 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1397 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1398 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1402 if (Protocol >= PROTOCOL_NT1) {
1403 uint16 flg2 = SVAL(outbuf,smb_flg2);
1404 SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
1407 /* Now we must call the relevant NT_TRANS function */
1408 switch(function_code) {
1409 case NT_TRANSACT_CREATE:
1410 outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize,
1411 &setup, ¶ms, &data);
1413 case NT_TRANSACT_IOCTL:
1414 outsize = call_nt_transact_ioctl(conn,
1415 inbuf, outbuf, length, bufsize,
1416 &setup, ¶ms, &data);
1418 case NT_TRANSACT_SET_SECURITY_DESC:
1419 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
1421 &setup, ¶ms, &data);
1423 case NT_TRANSACT_NOTIFY_CHANGE:
1424 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
1426 &setup, ¶ms, &data);
1428 case NT_TRANSACT_RENAME:
1429 outsize = call_nt_transact_rename(conn, inbuf, outbuf, length,
1431 &setup, ¶ms, &data);
1434 case NT_TRANSACT_QUERY_SECURITY_DESC:
1435 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
1437 &setup, ¶ms, &data);
1440 /* Error in request */
1441 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1448 return (ERROR(ERRSRV,ERRerror));
1451 /* As we do not know how many data packets will need to be
1452 returned here the various call_nt_transact_xxxx calls
1453 must send their own. Thus a call_nt_transact_xxxx routine only
1454 returns a value other than -1 when it wants to send
1464 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
1465 calls have already sent it. If outsize != -1 then it is
1466 returning an error packet. */