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;
27 extern int chain_fnum;
28 extern connection_struct Connections[];
29 extern files_struct Files[];
31 extern int oplock_sock;
32 extern int smb_read_error;
33 extern int global_oplock_break;
34 extern BOOL case_sensitive;
35 extern BOOL case_preserve;
36 extern BOOL short_case_preserve;
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)
329 else if( desired_access & FILE_EXECUTE)
332 DEBUG(0,("map_share_mode: Incorrect value for desired_access = %x\n",
338 /* Add in the requested share mode - ignore FILE_SHARE_DELETE for now. */
339 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
340 case FILE_SHARE_READ:
341 smb_open_mode |= (DENY_WRITE<<4);
343 case FILE_SHARE_WRITE:
344 smb_open_mode |= (DENY_READ<<4);
346 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
347 smb_open_mode |= (DENY_NONE<<4);
349 case FILE_SHARE_NONE:
350 smb_open_mode |= (DENY_ALL<<4);
355 * Handle a O_SYNC request.
357 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
358 smb_open_mode |= (1<<14);
360 return smb_open_mode;
363 /****************************************************************************
364 Reply to an NT create and X call on a pipe.
365 ****************************************************************************/
367 static int nt_open_pipe(char *fname, char *inbuf, char *outbuf, int *ppnum)
369 int cnum = SVAL(inbuf,smb_tid);
371 uint16 vuid = SVAL(inbuf, smb_uid);
374 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
376 /* See if it is one we want to handle. */
377 for( i = 0; known_nt_pipes[i]; i++ )
378 if( strequal(fname,known_nt_pipes[i]))
381 if ( known_nt_pipes[i] == NULL )
382 return(ERROR(ERRSRV,ERRaccess));
384 /* Strip \\ off the name. */
387 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
389 pnum = open_rpc_pipe_hnd(fname, cnum, vuid);
391 return(ERROR(ERRSRV,ERRnofids));
393 *ppnum = pnum + 0x800; /* Mark file handle up into high range. */
397 /****************************************************************************
398 Reply to an NT create and X call.
399 ****************************************************************************/
401 int reply_ntcreate_and_X(char *inbuf,char *outbuf,int length,int bufsize)
404 int cnum = SVAL(inbuf,smb_tid);
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 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
412 ((uint32)sizeof(fname)-1));
415 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
416 /* Breakout the oplock request bits so we can set the
417 reply bits separately. */
418 int oplock_request = 0;
420 int fmode=0,mtime=0,rmode=0;
424 BOOL bad_path = False;
429 * We need to construct the open_and_X ofun value from the
430 * NT values, as that's what our code is structured to accept.
433 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
434 return(ERROR(ERRDOS,ERRbadaccess));
437 * Now contruct the smb_open_mode value from the desired access
438 * and the share access.
441 if((smb_open_mode = map_share_mode( desired_access, share_access, file_attributes)) == -1)
442 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. */
452 int ret = nt_open_pipe(fname, inbuf, outbuf, &fnum);
455 smb_action = FILE_WAS_OPENED;
459 * Ordinary file or directory.
463 * Check if POSIX semantics are wanted.
466 set_posix_case_semantics(file_attributes);
468 unix_convert(fname,cnum,0,&bad_path);
470 fnum = find_free_file();
472 restore_case_semantics(file_attributes);
473 return(ERROR(ERRSRV,ERRnofids));
478 if (!check_name(fname,cnum)) {
479 if((errno == ENOENT) && bad_path) {
480 unix_ERR_class = ERRDOS;
481 unix_ERR_code = ERRbadpath;
483 fsp->reserved = False;
485 restore_case_semantics(file_attributes);
487 return(UNIXERROR(ERRDOS,ERRnoaccess));
490 unixmode = unix_mode(cnum,smb_attr | aARCH);
492 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
493 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
496 * If it's a request for a directory open, deal with it separately.
499 if(flags & OPEN_DIRECTORY) {
502 open_directory(fnum, cnum, fname, smb_ofun, unixmode, &smb_action);
504 restore_case_semantics(file_attributes);
507 fsp->reserved = False;
508 return(UNIXERROR(ERRDOS,ERRnoaccess));
513 * Ordinary file case.
517 * NB. We have a potential bug here. If we cause an oplock
518 * break to ourselves, then we could end up processing filename
519 * related SMB requests whilst we await the oplock break
520 * response. As we may have changed the filename case
521 * semantics to be POSIX-like, this could mean a filename
522 * request could fail when it should succeed. This is a
523 * rare condition, but eventually we must arrange to restore
524 * the correct case semantics before issuing an oplock break
525 * request to our client. JRA.
528 open_file_shared(fnum,cnum,fname,smb_open_mode,smb_ofun,unixmode,
529 oplock_request,&rmode,&smb_action);
533 * We cheat here. The only case we care about is a directory
534 * rename, where the NT client will attempt to open the source
535 * directory for DELETE access. Note that when the NT client
536 * does this it does *not* set the directory bit in the
537 * request packet. This is translated into a read/write open
538 * request. POSIX states that any open for write request on a directory
539 * will generate an EISDIR error, so we can catch this here and open
540 * a pseudo handle that is flagged as a directory. JRA.
543 if(errno == EISDIR) {
546 open_directory(fnum, cnum, fname, smb_ofun, unixmode, &smb_action);
549 fsp->reserved = False;
550 restore_case_semantics(file_attributes);
551 return(UNIXERROR(ERRDOS,ERRnoaccess));
554 if((errno == ENOENT) && bad_path) {
555 unix_ERR_class = ERRDOS;
556 unix_ERR_code = ERRbadpath;
559 fsp->reserved = False;
561 restore_case_semantics(file_attributes);
563 return(UNIXERROR(ERRDOS,ERRnoaccess));
568 if(fsp->is_directory) {
569 if(sys_stat(fsp->name, &sbuf) != 0) {
570 close_directory(fnum);
571 restore_case_semantics(file_attributes);
572 return(ERROR(ERRDOS,ERRnoaccess));
575 if (fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
576 close_file(fnum,False);
577 restore_case_semantics(file_attributes);
578 return(ERROR(ERRDOS,ERRnoaccess));
582 restore_case_semantics(file_attributes);
584 file_len = sbuf.st_size;
585 fmode = dos_mode(cnum,fname,&sbuf);
587 fmode = FILE_ATTRIBUTE_NORMAL;
588 mtime = sbuf.st_mtime;
589 if (!fsp->is_directory && (fmode & aDIR)) {
590 close_file(fnum,False);
591 return(ERROR(ERRDOS,ERRnoaccess));
595 * If the caller set the extended oplock request bit
596 * and we granted one (by whatever means) - set the
597 * correct bit for extended oplock reply.
600 if (oplock_request && lp_fake_oplocks(SNUM(cnum)))
601 smb_action |= EXTENDED_OPLOCK_GRANTED;
603 if(oplock_request && fsp->granted_oplock)
604 smb_action |= EXTENDED_OPLOCK_GRANTED;
607 set_message(outbuf,34,0,True);
609 p = outbuf + smb_vwv2;
612 * Currently as we don't support level II oplocks we just report
613 * exclusive & batch here.
616 SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
620 SIVAL(p,0,smb_action);
625 * Deal with pipe return.
628 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
631 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
633 SSVAL(p,2, 0x5FF); /* ? */
636 * Deal with file return.
639 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(cnum))));
641 put_long_date(p,sbuf.st_atime); /* access time */
643 put_long_date(p,sbuf.st_mtime); /* write time */
645 put_long_date(p,sbuf.st_mtime); /* change time */
647 SIVAL(p,0,fmode); /* File Attributes. */
650 SIVAL(p,0, file_len & 0xFFFFFFFF);
651 SIVAL(p,4, file_len >> 32);
652 #else /* OFF_T_IS_64_BITS */
654 #endif /* OFF_T_IS_64_BITS */
656 SCVAL(p,0,fsp->is_directory ? 1 : 0);
661 DEBUG(5,("reply_ntcreate_and_X: open fnum = %d, name = %s\n",
664 return chain_reply(inbuf,outbuf,length,bufsize);
667 /****************************************************************************
668 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
669 ****************************************************************************/
671 static int call_nt_transact_create(char *inbuf, char *outbuf, int length,
672 int bufsize, int cnum,
673 char **ppsetup, char **ppparams, char **ppdata)
677 char *params = *ppparams;
678 uint32 flags = IVAL(params,0);
679 uint32 desired_access = IVAL(params,8);
680 uint32 file_attributes = IVAL(params,20);
681 uint32 share_access = IVAL(params,24);
682 uint32 create_disposition = IVAL(params,28);
683 uint32 fname_len = MIN(((uint32)IVAL(params,44)),
684 ((uint32)sizeof(fname)-1));
687 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
688 /* Breakout the oplock request bits so we can set the
689 reply bits separately. */
690 int oplock_request = 0;
692 int fmode=0,mtime=0,rmode=0;
696 BOOL bad_path = False;
701 * We need to construct the open_and_X ofun value from the
702 * NT values, as that's what our code is structured to accept.
705 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
706 return(ERROR(ERRDOS,ERRbadaccess));
709 * Now contruct the smb_open_mode value from the desired access
710 * and the share access.
713 if((smb_open_mode = map_share_mode( desired_access, share_access, file_attributes)) == -1)
714 return(ERROR(ERRDOS,ERRbadaccess));
720 StrnCpy(fname,params+53,fname_len);
721 fname[fname_len] = '\0';
723 /* If it's an IPC, use the pipe handler. */
725 int ret = nt_open_pipe(fname, inbuf, outbuf, &fnum);
728 smb_action = FILE_WAS_OPENED;
731 * Check if POSIX semantics are wanted.
734 set_posix_case_semantics(file_attributes);
736 unix_convert(fname,cnum,0,&bad_path);
738 fnum = find_free_file();
740 restore_case_semantics(file_attributes);
741 return(ERROR(ERRSRV,ERRnofids));
746 if (!check_name(fname,cnum)) {
747 if((errno == ENOENT) && bad_path) {
748 unix_ERR_class = ERRDOS;
749 unix_ERR_code = ERRbadpath;
751 fsp->reserved = False;
753 restore_case_semantics(file_attributes);
755 return(UNIXERROR(ERRDOS,ERRnoaccess));
758 unixmode = unix_mode(cnum,smb_attr | aARCH);
760 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
761 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
764 * If it's a request for a directory open, deal with it separately.
767 if(flags & OPEN_DIRECTORY) {
772 * We will get a create directory here if the Win32
773 * app specified a security descriptor in the
774 * CreateDirectory() call.
777 open_directory(fnum, cnum, fname, smb_ofun, unixmode, &smb_action);
780 fsp->reserved = False;
781 return(UNIXERROR(ERRDOS,ERRnoaccess));
786 * Ordinary file case.
789 open_file_shared(fnum,cnum,fname,smb_open_mode,smb_ofun,unixmode,
790 oplock_request,&rmode,&smb_action);
793 if((errno == ENOENT) && bad_path) {
794 unix_ERR_class = ERRDOS;
795 unix_ERR_code = ERRbadpath;
797 fsp->reserved = False;
799 restore_case_semantics(file_attributes);
801 return(UNIXERROR(ERRDOS,ERRnoaccess));
804 if (fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
805 close_file(fnum,False);
807 restore_case_semantics(file_attributes);
809 return(ERROR(ERRDOS,ERRnoaccess));
812 file_len = sbuf.st_size;
813 fmode = dos_mode(cnum,fname,&sbuf);
815 fmode = FILE_ATTRIBUTE_NORMAL;
816 mtime = sbuf.st_mtime;
819 close_file(fnum,False);
820 restore_case_semantics(file_attributes);
821 return(ERROR(ERRDOS,ERRnoaccess));
825 * If the caller set the extended oplock request bit
826 * and we granted one (by whatever means) - set the
827 * correct bit for extended oplock reply.
830 if (oplock_request && lp_fake_oplocks(SNUM(cnum)))
831 smb_action |= EXTENDED_OPLOCK_GRANTED;
833 if(oplock_request && fsp->granted_oplock)
834 smb_action |= EXTENDED_OPLOCK_GRANTED;
838 restore_case_semantics(file_attributes);
840 /* Realloc the size of parameters and data we will return */
841 params = *ppparams = Realloc(*ppparams, 69);
843 return(ERROR(ERRDOS,ERRnomem));
846 SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
850 SIVAL(p,0,smb_action);
855 * Deal with pipe return.
858 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
861 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
863 SSVAL(p,2, 0x5FF); /* ? */
866 * Deal with file return.
869 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(cnum))));
871 put_long_date(p,sbuf.st_atime); /* access time */
873 put_long_date(p,sbuf.st_mtime); /* write time */
875 put_long_date(p,sbuf.st_mtime); /* change time */
877 SIVAL(p,0,fmode); /* File Attributes. */
880 SIVAL(p,0, file_len & 0xFFFFFFFF);
881 SIVAL(p,4, (file_len >> 32));
882 #else /* OFF_T_IS_64_BITS */
884 #endif /* OFF_T_IS_64_BITS */
887 /* Send the required number of replies */
888 send_nt_replies(outbuf, bufsize, params, 69, *ppdata, 0);
893 /****************************************************************************
894 Reply to a NT CANCEL request.
895 ****************************************************************************/
897 int reply_ntcancel(char *inbuf,char *outbuf,int length,int bufsize)
900 * Go through and cancel any pending change notifies.
901 * TODO: When we add blocking locks we will add cancel
905 int mid = SVAL(inbuf,smb_mid);
906 remove_pending_change_notify_requests_by_mid(mid);
908 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
913 /****************************************************************************
914 Reply to an unsolicited SMBNTtranss - just ignore it!
915 ****************************************************************************/
917 int reply_nttranss(char *inbuf,char *outbuf,int length,int bufsize)
919 DEBUG(4,("Ignoring nttranss of length %d\n",length));
923 /****************************************************************************
924 Reply to an NT transact rename command.
925 ****************************************************************************/
927 static int call_nt_transact_rename(char *inbuf, char *outbuf, int length,
928 int bufsize, int cnum,
929 char **ppsetup, char **ppparams, char **ppdata)
931 char *params = *ppparams;
933 int fnum = SVAL(params, 0);
934 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
935 uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
936 ((uint32)sizeof(new_name)-1));
939 CHECK_FNUM(fnum, cnum);
940 StrnCpy(new_name,params+4,fname_len);
941 new_name[fname_len] = '\0';
943 outsize = rename_internals(inbuf, outbuf, Files[fnum].name,
944 new_name, replace_if_exists);
947 * Rename was successful.
949 send_nt_replies(outbuf, bufsize, NULL, 0, NULL, 0);
951 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
952 Files[fnum].name, new_name));
960 /****************************************************************************
961 This is the structure to queue to implement NT change
962 notify. It consists of smb_size bytes stored from the
963 transact command (to keep the mid, tid etc around).
964 Plus the fid to examine and the time to check next.
965 *****************************************************************************/
971 time_t next_check_time;
972 time_t modify_time; /* Info from the directory we're monitoring. */
973 time_t status_time; /* Info from the directory we're monitoring. */
974 char request_buf[smb_size];
977 static ubi_slList change_notify_queue = { NULL, (ubi_slNodePtr)&change_notify_queue, 0};
979 /****************************************************************************
980 Setup the common parts of the return packet and send it.
981 *****************************************************************************/
983 static void change_notify_reply_packet(char *inbuf, int error_class, uint32 error_code)
986 char outbuf[smb_size+38];
988 memset(outbuf, '\0', sizeof(outbuf));
989 construct_reply_common(inbuf, outbuf);
992 * If we're returning a 'too much in the directory changed' we need to
993 * set this is an NT error status flags. If we don't then the (probably
994 * untested) code in the NT redirector has a bug in that it doesn't re-issue
995 * the change notify.... Ah - I *love* it when I get so deeply into this I
996 * can even determine how MS failed to test stuff and why.... :-). JRA.
999 if(error_class == 0) /* NT Error. */
1000 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1002 ERROR(error_class,error_code);
1005 * Seems NT needs a transact command with an error code
1006 * in it. This is a longer packet than a simple error.
1008 set_message(outbuf,18,0,False);
1010 send_smb(Client,outbuf);
1013 /****************************************************************************
1014 Delete entries by fnum from the change notify pending queue.
1015 *****************************************************************************/
1017 void remove_pending_change_notify_requests_by_fid(int fnum)
1019 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1020 change_notify_buf *prev = NULL;
1022 while(cnbp != NULL) {
1023 if(cnbp->fnum == fnum) {
1024 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1025 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1030 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1034 /****************************************************************************
1035 Delete entries by mid from the change notify pending queue. Always send reply.
1036 *****************************************************************************/
1038 void remove_pending_change_notify_requests_by_mid(int mid)
1040 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1041 change_notify_buf *prev = NULL;
1043 while(cnbp != NULL) {
1044 if(SVAL(cnbp->request_buf,smb_mid) == mid) {
1045 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_CANCELLED);
1046 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1047 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1052 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1056 /****************************************************************************
1057 Process the change notify queue. Note that this is only called as root.
1058 *****************************************************************************/
1060 void process_pending_change_notify_queue(time_t t)
1062 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1063 change_notify_buf *prev = NULL;
1068 if(cnbp->next_check_time >= t)
1072 * It's time to check. Go through the queue and see if
1073 * the timestamps changed.
1076 while((cnbp != NULL) && (cnbp->next_check_time <= t)) {
1078 int fnum = cnbp->fnum;
1079 int cnum = cnbp->cnum;
1080 files_struct *fsp = &Files[fnum];
1081 uint16 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
1082 SVAL(cnbp->request_buf,smb_uid);
1084 if(!become_user(&Connections[cnum],cnum,vuid)) {
1085 DEBUG(0,("process_pending_change_notify_queue: Unable to become user vuid=%d.\n",
1088 * Remove the entry and return an error to the client.
1090 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1091 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1092 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1096 if(!become_service(cnum,True)) {
1097 DEBUG(0,("process_pending_change_notify_queue: Unable to become service cnum=%d. \
1098 Error was %s.\n", cnum, strerror(errno) ));
1100 * Remove the entry and return an error to the client.
1102 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1103 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1104 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1109 if(sys_stat(fsp->name, &st) < 0) {
1110 DEBUG(0,("process_pending_change_notify_queue: Unable to stat directory %s. \
1111 Error was %s.\n", fsp->name, strerror(errno) ));
1113 * Remove the entry and return an error to the client.
1115 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1116 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1117 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1122 if(cnbp->modify_time != st.st_mtime ||
1123 cnbp->status_time != st.st_ctime) {
1125 * Remove the entry and return a change notify to the client.
1127 DEBUG(5,("process_pending_change_notify_queue: directory fnum = %d, name = %s changed\n",
1129 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_NOTIFY_ENUM_DIR);
1130 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1131 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1139 * Move to the next in the list.
1142 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1146 /****************************************************************************
1147 Reply to a notify change - queue the request and
1148 don't allow a directory to be opened.
1149 ****************************************************************************/
1151 static int call_nt_transact_notify_change(char *inbuf, char *outbuf, int length,
1152 int bufsize, int cnum,
1153 char **ppsetup, char **ppparams, char **ppdata)
1155 char *setup = *ppsetup;
1158 change_notify_buf *cnbp;
1161 fnum = SVAL(setup,4);
1163 DEBUG(3,("call_nt_transact_notify_change: fnum = %d.\n", fnum));
1165 if(!VALID_FNUM(fnum))
1166 return(ERROR(ERRDOS,ERRbadfid));
1170 if((!fsp->open) || (!fsp->is_directory) || (cnum != fsp->cnum))
1171 return(ERROR(ERRDOS,ERRbadfid));
1174 * Now queue an entry on the notify change stack. We timestamp
1175 * the entry we are adding so that we know when to scan next.
1176 * We only need to save smb_size bytes from this incoming packet
1177 * as we will always by returning a 'read the directory yourself'
1181 if((cnbp = (change_notify_buf *)malloc(sizeof(change_notify_buf))) == NULL) {
1182 DEBUG(0,("call_nt_transact_notify_change: Malloc fail (2) !\n" ));
1187 * Store the current timestamp on the directory we are monitoring.
1190 if(sys_stat(fsp->name, &st) < 0) {
1191 DEBUG(0,("call_nt_transact_notify_change: Unable to stat fnum = %d, name = %s. \
1192 Error was %s\n", fnum, fsp->name, strerror(errno) ));
1194 return(UNIXERROR(ERRDOS,ERRbadfid));
1197 memcpy(cnbp->request_buf, inbuf, smb_size);
1200 cnbp->modify_time = st.st_mtime;
1201 cnbp->status_time = st.st_ctime;
1203 cnbp->next_check_time = time(NULL) + lp_change_notify_timeout();
1206 * Adding to the tail enables us to check only
1207 * the head when scanning for change, as this entry
1208 * is forced to have the first timeout expiration.
1211 ubi_slAddTail(&change_notify_queue, cnbp);
1213 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1214 fid=%d, name = %s\n", fnum, fsp->name ));
1219 /****************************************************************************
1220 Reply to query a security descriptor - currently this is not implemented (it
1221 is planned to be though).
1222 ****************************************************************************/
1224 static int call_nt_transact_query_security_desc(char *inbuf, char *outbuf, int length,
1225 int bufsize, int cnum,
1226 char **ppsetup, char **ppparams, char **ppdata)
1228 DEBUG(0,("call_nt_transact_query_security_desc: Currently not implemented.\n"));
1229 return(ERROR(ERRSRV,ERRnosupport));
1232 /****************************************************************************
1233 Reply to set a security descriptor - currently this is not implemented (it
1234 is planned to be though).
1235 ****************************************************************************/
1237 static int call_nt_transact_set_security_desc(char *inbuf, char *outbuf, int length,
1238 int bufsize, int cnum,
1239 char **ppsetup, char **ppparams, char **ppdata)
1241 DEBUG(0,("call_nt_transact_set_security_desc: Currently not implemented.\n"));
1242 return(ERROR(ERRSRV,ERRnosupport));
1245 /****************************************************************************
1246 Reply to IOCTL - not implemented - no plans.
1247 ****************************************************************************/
1249 static int call_nt_transact_ioctl(char *inbuf, char *outbuf, int length,
1250 int bufsize, int cnum,
1251 char **ppsetup, char **ppparams, char **ppdata)
1253 DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1254 return(ERROR(ERRSRV,ERRnosupport));
1257 /****************************************************************************
1258 Reply to a SMBNTtrans.
1259 ****************************************************************************/
1261 int reply_nttrans(char *inbuf,char *outbuf,int length,int bufsize)
1264 int cnum = SVAL(inbuf,smb_tid);
1265 #if 0 /* Not used. */
1266 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1267 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1268 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1269 #endif /* Not used. */
1270 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1271 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1272 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1273 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1274 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1275 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1276 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1277 uint16 function_code = SVAL( inbuf, smb_nt_Function);
1278 char *params = NULL, *data = NULL, *setup = NULL;
1279 uint32 num_params_sofar, num_data_sofar;
1281 if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1283 * Queue this open message as we are the process of an oplock break.
1286 DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1287 due to being in oplock break state.\n" ));
1289 push_oplock_pending_smb_message( inbuf, length);
1293 outsize = set_message(outbuf,0,0,True);
1296 * All nttrans messages we handle have smb_wct == 19 + setup_count.
1297 * Ensure this is so as a sanity check.
1300 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1301 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1302 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1303 return(ERROR(ERRSRV,ERRerror));
1306 /* Allocate the space for the setup, the maximum needed parameters and data */
1309 setup = (char *)malloc(setup_count);
1310 if (total_parameter_count > 0)
1311 params = (char *)malloc(total_parameter_count);
1312 if (total_data_count > 0)
1313 data = (char *)malloc(total_data_count);
1315 if ((total_parameter_count && !params) || (total_data_count && !data) ||
1316 (setup_count && !setup)) {
1317 DEBUG(0,("reply_nttrans : Out of memory\n"));
1318 return(ERROR(ERRDOS,ERRnomem));
1321 /* Copy the param and data bytes sent with this request into
1322 the params buffer */
1323 num_params_sofar = parameter_count;
1324 num_data_sofar = data_count;
1326 if (parameter_count > total_parameter_count || data_count > total_data_count)
1327 exit_server("reply_nttrans: invalid sizes in packet.\n");
1330 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1331 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1332 dump_data(10, setup, setup_count);
1335 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1336 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1337 dump_data(10, params, parameter_count);
1340 memcpy( data, smb_base(inbuf) + data_offset, data_count);
1341 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1342 dump_data(10, data, data_count);
1345 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1346 /* We need to send an interim response then receive the rest
1347 of the parameter/data bytes */
1348 outsize = set_message(outbuf,0,0,True);
1349 send_smb(Client,outbuf);
1351 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1354 ret = receive_next_smb(Client,oplock_sock,inbuf,bufsize,
1355 SMB_SECONDARY_WAIT);
1357 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1358 outsize = set_message(outbuf,0,0,True);
1360 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1362 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1363 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1371 return(ERROR(ERRSRV,ERRerror));
1374 /* Revise total_params and total_data in case they have changed downwards */
1375 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1376 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1377 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1378 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1379 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1380 exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
1382 memcpy( ¶ms[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
1383 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1384 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1385 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1389 if (Protocol >= PROTOCOL_NT1) {
1390 uint16 flg2 = SVAL(outbuf,smb_flg2);
1391 SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
1394 /* Now we must call the relevant NT_TRANS function */
1395 switch(function_code) {
1396 case NT_TRANSACT_CREATE:
1397 outsize = call_nt_transact_create(inbuf, outbuf, length, bufsize, cnum,
1398 &setup, ¶ms, &data);
1400 case NT_TRANSACT_IOCTL:
1401 outsize = call_nt_transact_ioctl(inbuf, outbuf, length, bufsize, cnum,
1402 &setup, ¶ms, &data);
1404 case NT_TRANSACT_SET_SECURITY_DESC:
1405 outsize = call_nt_transact_set_security_desc(inbuf, outbuf, length, bufsize, cnum,
1406 &setup, ¶ms, &data);
1408 case NT_TRANSACT_NOTIFY_CHANGE:
1409 outsize = call_nt_transact_notify_change(inbuf, outbuf, length, bufsize, cnum,
1410 &setup, ¶ms, &data);
1412 case NT_TRANSACT_RENAME:
1413 outsize = call_nt_transact_rename(inbuf, outbuf, length, bufsize, cnum,
1414 &setup, ¶ms, &data);
1416 case NT_TRANSACT_QUERY_SECURITY_DESC:
1417 outsize = call_nt_transact_query_security_desc(inbuf, outbuf, length, bufsize, cnum,
1418 &setup, ¶ms, &data);
1421 /* Error in request */
1422 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1429 return (ERROR(ERRSRV,ERRerror));
1432 /* As we do not know how many data packets will need to be
1433 returned here the various call_nt_transact_xxxx calls
1434 must send their own. Thus a call_nt_transact_xxxx routine only
1435 returns a value other than -1 when it wants to send
1445 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
1446 calls have already sent it. If outsize != -1 then it is
1447 returning an error packet. */