nttrans.c:
[kai/samba-autobuild/.git] / source3 / smbd / nttrans.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB NT transaction handling
5    Copyright (C) Jeremy Allison 1994-1998
6
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.
11
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.
16
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.
20 */
21
22 #include "includes.h"
23 #include "nterr.h"
24
25 extern int DEBUGLEVEL;
26 extern int Protocol;
27 extern int Client;  
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;
34
35 static void remove_pending_change_notify_requests_by_mid(int mid);
36
37 static char *known_nt_pipes[] = {
38   "\\LANMAN",
39   "\\srvsvc",
40   "\\samr",
41   "\\wkssvc",
42   "\\NETLOGON",
43   "\\ntlsa",
44   "\\ntsvcs",
45   "\\lsass",
46   "\\lsarpc",
47   "\\winreg",
48   NULL
49 };
50
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 ****************************************************************************/
57
58 static int send_nt_replies(char *outbuf, int bufsize, char *params,
59                            int paramsize, char *pdata, int datasize)
60 {
61   extern int max_send;
62   int data_to_send = datasize;
63   int params_to_send = paramsize;
64   int useable_space;
65   char *pp = params;
66   char *pd = pdata;
67   int params_sent_thistime, data_sent_thistime, total_sent_thistime;
68   int alignment_offset = 3;
69   int data_alignment_offset = 0;
70
71   /*
72    * Initially set the wcnt area to be 18 - this is true for all
73    * transNT replies.
74    */
75
76   set_message(outbuf,18,0,True);
77
78   /* 
79    * If there genuinely are no parameters or data to send just send
80    * the empty packet.
81    */
82
83   if(params_to_send == 0 && data_to_send == 0) {
84     send_smb(Client,outbuf);
85     return 0;
86   }
87
88   /*
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.
92    */
93
94   if (((params_to_send % 4) != 0) && (data_to_send != 0))
95     data_alignment_offset = 4 - (params_to_send % 4);
96
97   /* 
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.
102    */
103
104   useable_space = bufsize - ((smb_buf(outbuf)+
105                     alignment_offset+data_alignment_offset) -
106                     outbuf);
107
108   /*
109    * useable_space can never be more than max_send minus the
110    * alignment offset.
111    */
112
113   useable_space = MIN(useable_space,
114                       max_send - (alignment_offset+data_alignment_offset));
115
116
117   while (params_to_send || data_to_send) {
118
119     /*
120      * Calculate whether we will totally or partially fill this packet.
121      */
122
123     total_sent_thistime = params_to_send + data_to_send +
124                             alignment_offset + data_alignment_offset;
125
126     /* 
127      * We can never send more than useable_space.
128      */
129
130     total_sent_thistime = MIN(total_sent_thistime, useable_space);
131
132     set_message(outbuf, 18, total_sent_thistime, True);
133
134     /*
135      * Set total params and data to be sent.
136      */
137
138     SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
139     SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
140
141     /* 
142      * Calculate how many parameters and data we can fit into
143      * this packet. Parameters get precedence.
144      */
145
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);
149
150     SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
151
152     if(params_sent_thistime == 0) {
153       SIVAL(outbuf,smb_ntr_ParameterOffset,0);
154       SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
155     } else {
156       /*
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.
161        */
162
163       SIVAL(outbuf,smb_ntr_ParameterOffset,
164             ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
165       /* 
166        * Absolute displacement of param bytes sent in this packet.
167        */
168
169       SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
170     }
171
172     /*
173      * Deal with the data portion.
174      */
175
176     SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
177
178     if(data_sent_thistime == 0) {
179       SIVAL(outbuf,smb_ntr_DataOffset,0);
180       SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
181     } else {
182       /*
183        * The offset of the data bytes is the offset of the
184        * parameter bytes plus the number of parameters being sent this time.
185        */
186
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);
190     }
191
192     /* 
193      * Copy the param bytes into the packet.
194      */
195
196     if(params_sent_thistime)
197       memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
198
199     /*
200      * Copy in the data bytes
201      */
202
203     if(data_sent_thistime)
204       memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
205              data_alignment_offset,pd,data_sent_thistime);
206     
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));
211     
212     /* Send the packet */
213     send_smb(Client,outbuf);
214     
215     pp += params_sent_thistime;
216     pd += data_sent_thistime;
217     
218     params_to_send -= params_sent_thistime;
219     data_to_send -= data_sent_thistime;
220
221     /*
222      * Sanity check
223      */
224
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));
228       return -1;
229     }
230   } 
231
232   return 0;
233 }
234
235 /****************************************************************************
236  Save case statics.
237 ****************************************************************************/
238
239 static BOOL saved_case_sensitive;
240 static BOOL saved_case_preserve;
241 static BOOL saved_short_case_preserve;
242
243 /****************************************************************************
244  Save case semantics.
245 ****************************************************************************/
246
247 static void set_posix_case_semantics(uint32 file_attributes)
248 {
249   if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
250     return;
251
252   saved_case_sensitive = case_sensitive;
253   saved_case_preserve = case_preserve;
254   saved_short_case_preserve = short_case_preserve;
255
256   /* Set to POSIX. */
257   case_sensitive = True;
258   case_preserve = True;
259   short_case_preserve = True;
260 }
261
262 /****************************************************************************
263  Restore case semantics.
264 ****************************************************************************/
265
266 static void restore_case_semantics(uint32 file_attributes)
267 {
268   if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
269     return;
270
271   case_sensitive = saved_case_sensitive;
272   case_preserve = saved_case_preserve;
273   short_case_preserve = saved_short_case_preserve;
274 }
275
276 /****************************************************************************
277  Utility function to map create disposition.
278 ****************************************************************************/
279
280 static int map_create_disposition( uint32 create_disposition)
281 {
282   switch( create_disposition ) {
283   case FILE_CREATE:
284     /* create if not exist, fail if exist */
285     return 0x10;
286   case FILE_SUPERSEDE:
287   case FILE_OVERWRITE_IF:
288     /* create if not exist, trunc if exist */
289     return 0x12;
290   case FILE_OPEN:
291     /* fail if not exist, open if exists */
292     return 0x1;
293   case FILE_OPEN_IF:
294     /* create if not exist, open if exists */
295     return 0x11;
296   case FILE_OVERWRITE:
297     /* fail if not exist, truncate if exists */
298     return 0x2;
299   default:
300     DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
301              create_disposition ));
302     return -1;
303   }
304 }
305
306 /****************************************************************************
307  Utility function to map share modes.
308 ****************************************************************************/
309
310 static int map_share_mode( uint32 desired_access, uint32 share_access, uint32 file_attributes)
311 {
312   int smb_open_mode = -1;
313
314   switch( desired_access & (FILE_READ_DATA|FILE_WRITE_DATA) ) {
315   case FILE_READ_DATA:
316     smb_open_mode = 0;
317     break;
318   case FILE_WRITE_DATA:
319     smb_open_mode = 1;
320     break;
321   case FILE_READ_DATA|FILE_WRITE_DATA:
322     smb_open_mode = 2;
323     break;
324   }
325
326   if (smb_open_mode == -1) {
327     if(desired_access & (DELETE_ACCESS|FILE_WRITE_ATTRIBUTES|
328                          WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS))
329       smb_open_mode = 2;
330     else if(desired_access & (FILE_EXECUTE|READ_CONTROL_ACCESS))
331       smb_open_mode = 0;
332     else {
333       DEBUG(0,("map_share_mode: Incorrect value for desired_access = %x\n",
334              desired_access));
335       return -1;
336     }
337   }
338
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);
343     break;
344   case FILE_SHARE_WRITE:
345     smb_open_mode |= (DENY_READ<<4);
346     break;
347   case (FILE_SHARE_READ|FILE_SHARE_WRITE):
348     smb_open_mode |= (DENY_NONE<<4);
349     break;
350   case FILE_SHARE_NONE:
351     smb_open_mode |= (DENY_ALL<<4);
352     break;
353   }
354
355   /*
356    * Handle a O_SYNC request.
357    */
358   if(file_attributes & FILE_FLAG_WRITE_THROUGH)
359     smb_open_mode |= (1<<14);
360
361   return smb_open_mode;
362 }
363
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)
369 {
370         pipes_struct *p = NULL;
371
372         uint16 vuid = SVAL(inbuf, smb_uid);
373         int i;
374
375         DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
376     
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]))
380                         break;
381     
382         if ( known_nt_pipes[i] == NULL )
383                 return(ERROR(ERRSRV,ERRaccess));
384     
385         /* Strip \\ off the name. */
386         fname++;
387     
388         DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
389
390         p = open_rpc_pipe_p(fname, conn, vuid);
391         if (!p)
392                 return(ERROR(ERRSRV,ERRnofids));
393
394         *ppnum = p->pnum;
395
396         return 0;
397 }
398
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)
404 {  
405         pstring fname;
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));
414         int smb_ofun;
415         int smb_open_mode;
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;
420     mode_t unixmode;
421         int pnum = -1;
422         int fmode=0,rmode=0;
423         SMB_OFF_T file_len = 0;
424         SMB_STRUCT_STAT sbuf;
425         int smb_action = 0;
426         BOOL bad_path = False;
427         files_struct *fsp=NULL;
428         char *p = NULL;
429
430         /* 
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.
433          */    
434         
435         if((smb_ofun = map_create_disposition( create_disposition )) == -1)
436                 return(ERROR(ERRDOS,ERRbadaccess));
437
438         /*
439          * Now contruct the smb_open_mode value from the desired access
440          * and the share access.
441          */
442         
443         if((smb_open_mode = map_share_mode(desired_access, 
444                                            share_access, 
445                                            file_attributes)) == -1) {
446                 return(ERROR(ERRDOS,ERRbadaccess));
447         }
448
449         /*
450          * Get the file name.
451          */
452         StrnCpy(fname,smb_buf(inbuf),fname_len);
453         fname[fname_len] = '\0';
454         
455         /* If it's an IPC, use the pipe handler. */
456
457         if (IS_IPC(conn)) {
458
459                 int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
460                 if(ret != 0)
461                         return ret;
462
463                 /*
464                  * Deal with pipe return.
465                  */  
466
467                 set_message(outbuf,34,0,True);
468         
469                 p = outbuf + smb_vwv2;
470                 p++;
471                 SSVAL(p,0,pnum);
472                 p += 2;
473                 SIVAL(p,0,FILE_WAS_OPENED);
474                 p += 4;
475                 p += 32;
476                 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
477                 p += 20;
478                 /* File type. */
479                 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
480                 /* Device state. */
481                 SSVAL(p,2, 0x5FF); /* ? */
482
483                 DEBUG(5,("reply_ntcreate_and_X: open pipe = %s\n", fname));
484
485                 return chain_reply(inbuf,outbuf,length,bufsize);
486         }
487
488         /*
489          * Ordinary file or directory.
490          */
491                 
492         /*
493          * Check if POSIX semantics are wanted.
494          */
495                 
496         set_posix_case_semantics(file_attributes);
497                 
498         unix_convert(fname,conn,0,&bad_path,NULL);
499                 
500         fsp = file_new();
501         if (!fsp) {
502                 restore_case_semantics(file_attributes);
503                 return(ERROR(ERRSRV,ERRnofids));
504         }
505                 
506         if (!check_name(fname,conn)) { 
507                 if((errno == ENOENT) && bad_path) {
508                         unix_ERR_class = ERRDOS;
509                         unix_ERR_code = ERRbadpath;
510                 }
511                 file_free(fsp);
512                 
513                 restore_case_semantics(file_attributes);
514                 
515                 return(UNIXERROR(ERRDOS,ERRnoaccess));
516         } 
517                 
518         unixmode = unix_mode(conn,smb_attr | aARCH);
519     
520         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
521         oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
522
523         /* 
524          * If it's a request for a directory open, deal with it separately.
525          */
526
527         if(create_options & FILE_DIRECTORY_FILE) {
528                 oplock_request = 0;
529                 
530                 open_directory(fsp, conn, fname, smb_ofun, 
531                                unixmode, &smb_action);
532                         
533                 restore_case_semantics(file_attributes);
534
535                 if(!fsp->open) {
536                         file_free(fsp);
537                         return(UNIXERROR(ERRDOS,ERRnoaccess));
538                 }
539         } else {
540                 /*
541                  * Ordinary file case.
542                  */
543
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.  */
556
557                 open_file_shared(fsp,conn,fname,smb_open_mode,
558                                  smb_ofun,unixmode,
559                                  oplock_request,&rmode,&smb_action);
560
561                 if (!fsp->open) { 
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
576                          * directory. JRA.  */
577
578                         if(errno == EISDIR) {
579                                 oplock_request = 0;
580                                 
581                                 open_directory(fsp, conn, fname, smb_ofun, unixmode, &smb_action);
582                                 
583                                 if(!fsp->open) {
584                                         file_free(fsp);
585                                         restore_case_semantics(file_attributes);
586                                         return(UNIXERROR(ERRDOS,ERRnoaccess));
587                                 }
588                         } else {
589                                 if((errno == ENOENT) && bad_path) {
590                                         unix_ERR_class = ERRDOS;
591                                         unix_ERR_code = ERRbadpath;
592                                 }
593                                 
594                                 file_free(fsp);
595                                 
596                                 restore_case_semantics(file_attributes);
597                                 
598                                 return(UNIXERROR(ERRDOS,ERRnoaccess));
599                         }
600                 } 
601         }
602                 
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));
608                 }
609         } else {
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));
614                 } 
615         }
616                 
617         restore_case_semantics(file_attributes);
618                 
619         file_len = sbuf.st_size;
620         fmode = dos_mode(conn,fname,&sbuf);
621         if(fmode == 0)
622                 fmode = FILE_ATTRIBUTE_NORMAL;
623         if (!fsp->is_directory && (fmode & aDIR)) {
624                 close_file(fsp,False);
625                 return(ERROR(ERRDOS,ERRnoaccess));
626         } 
627         
628         /* 
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.
632          */
633         
634         if (oplock_request && lp_fake_oplocks(SNUM(conn)))
635                 smb_action |= EXTENDED_OPLOCK_GRANTED;
636         
637         if(oplock_request && fsp->granted_oplock)
638                 smb_action |= EXTENDED_OPLOCK_GRANTED;
639         
640         set_message(outbuf,34,0,True);
641         
642         p = outbuf + smb_vwv2;
643         
644         /*
645          * Currently as we don't support level II oplocks we just report
646          * exclusive & batch here.
647          */
648         
649         SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
650         p++;
651         SSVAL(p,0,fsp->fnum);
652         p += 2;
653         SIVAL(p,0,smb_action);
654         p += 4;
655         
656         /* Create time. */  
657         put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
658         p += 8;
659         put_long_date(p,sbuf.st_atime); /* access time */
660         p += 8;
661         put_long_date(p,sbuf.st_mtime); /* write time */
662         p += 8;
663         put_long_date(p,sbuf.st_mtime); /* change time */
664         p += 8;
665         SIVAL(p,0,fmode); /* File Attributes. */
666         p += 4;
667         SOFF_T(p, 0, file_len);
668         p += 8;
669         SOFF_T(p,0,file_len);
670         p += 12;
671         SCVAL(p,0,fsp->is_directory ? 1 : 0);
672         
673         DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
674
675         return chain_reply(inbuf,outbuf,length,bufsize);
676 }
677
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, 
683                                    int bufsize, 
684                                    char **ppsetup, char **ppparams, 
685                                    char **ppdata)
686 {
687   pstring fname;
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));
697   int smb_ofun;
698   int smb_open_mode;
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;
703   mode_t unixmode;
704   int pnum = -1;
705   int fmode=0,rmode=0;
706   SMB_OFF_T file_len = 0;
707   SMB_STRUCT_STAT sbuf;
708   int smb_action = 0;
709   BOOL bad_path = False;
710   files_struct *fsp = NULL;
711   char *p = NULL;
712
713   /* 
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.
716    */    
717
718   if((smb_ofun = map_create_disposition( create_disposition )) == -1)
719     return(ERROR(ERRDOS,ERRbadaccess));
720
721   /*
722    * Now contruct the smb_open_mode value from the desired access
723    * and the share access.
724    */
725
726   if((smb_open_mode = map_share_mode( desired_access, share_access, file_attributes)) == -1)
727     return(ERROR(ERRDOS,ERRbadaccess));
728
729   /*
730    * Get the file name.
731    */
732
733   StrnCpy(fname,params+53,fname_len);
734   fname[fname_len] = '\0';
735
736   /* If it's an IPC, use the pipe handler. */
737   if (IS_IPC(conn)) {
738     int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
739     if(ret != 0)
740       return ret;
741     smb_action = FILE_WAS_OPENED;
742   } else {
743     /*
744      * Check if POSIX semantics are wanted.
745      */
746
747     set_posix_case_semantics(file_attributes);
748
749     unix_convert(fname,conn,0,&bad_path,NULL);
750     
751     fsp = file_new();
752     if (!fsp) {
753             restore_case_semantics(file_attributes);
754             return(ERROR(ERRSRV,ERRnofids));
755     }
756
757     if (!check_name(fname,conn)) { 
758       if((errno == ENOENT) && bad_path) {
759         unix_ERR_class = ERRDOS;
760         unix_ERR_code = ERRbadpath;
761       }
762       file_free(fsp);
763
764       restore_case_semantics(file_attributes);
765
766       return(UNIXERROR(ERRDOS,ERRnoaccess));
767     } 
768   
769     unixmode = unix_mode(conn,smb_attr | aARCH);
770     
771     oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
772     oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
773
774     /*
775      * If it's a request for a directory open, deal with it separately.
776      */
777
778     if(create_options & FILE_DIRECTORY_FILE) {
779
780       oplock_request = 0;
781
782       /*
783        * We will get a create directory here if the Win32
784        * app specified a security descriptor in the 
785        * CreateDirectory() call.
786        */
787
788       open_directory(fsp, conn, fname, smb_ofun, unixmode, &smb_action);
789
790       if(!fsp->open) {
791         file_free(fsp);
792         return(UNIXERROR(ERRDOS,ERRnoaccess));
793       }
794     } else {
795
796       /*
797        * Ordinary file case.
798        */
799
800       open_file_shared(fsp,conn,fname,smb_open_mode,smb_ofun,unixmode,
801                        oplock_request,&rmode,&smb_action);
802
803       if (!fsp->open) { 
804         if((errno == ENOENT) && bad_path) {
805           unix_ERR_class = ERRDOS;
806           unix_ERR_code = ERRbadpath;
807         }
808         file_free(fsp);
809
810         restore_case_semantics(file_attributes);
811
812         return(UNIXERROR(ERRDOS,ERRnoaccess));
813       } 
814   
815       if (sys_fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
816         close_file(fsp,False);
817
818         restore_case_semantics(file_attributes);
819
820         return(ERROR(ERRDOS,ERRnoaccess));
821       } 
822   
823       file_len = sbuf.st_size;
824       fmode = dos_mode(conn,fname,&sbuf);
825       if(fmode == 0)
826         fmode = FILE_ATTRIBUTE_NORMAL;
827
828       if (fmode & aDIR) {
829         close_file(fsp,False);
830         restore_case_semantics(file_attributes);
831         return(ERROR(ERRDOS,ERRnoaccess));
832       } 
833
834       /* 
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.
838        */
839     
840       if (oplock_request && lp_fake_oplocks(SNUM(conn)))
841         smb_action |= EXTENDED_OPLOCK_GRANTED;
842   
843       if(oplock_request && fsp->granted_oplock)
844         smb_action |= EXTENDED_OPLOCK_GRANTED;
845     }
846   }
847
848   restore_case_semantics(file_attributes);
849
850   /* Realloc the size of parameters and data we will return */
851   params = *ppparams = Realloc(*ppparams, 69);
852   if(params == NULL)
853     return(ERROR(ERRDOS,ERRnomem));
854
855   p = params;
856   SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
857   p += 2;
858   if (IS_IPC(conn)) {
859           SSVAL(p,0,pnum);
860   } else {
861           SSVAL(p,0,fsp->fnum);
862   }
863   p += 2;
864   SIVAL(p,0,smb_action);
865   p += 8;
866
867   if (IS_IPC(conn)) {
868     /*
869      * Deal with pipe return.
870      */  
871     p += 32;
872     SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
873     p += 20;
874     /* File type. */
875     SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
876     /* Device state. */
877     SSVAL(p,2, 0x5FF); /* ? */
878   } else {
879     /*
880      * Deal with file return.
881      */
882     /* Create time. */
883     put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
884     p += 8;
885     put_long_date(p,sbuf.st_atime); /* access time */
886     p += 8;
887     put_long_date(p,sbuf.st_mtime); /* write time */
888     p += 8;
889     put_long_date(p,sbuf.st_mtime); /* change time */
890     p += 8;
891     SIVAL(p,0,fmode); /* File Attributes. */
892     p += 4;
893     SOFF_T(p,0,file_len);
894     p += 8;
895     SOFF_T(p,0,file_len);
896   }
897
898   /* Send the required number of replies */
899   send_nt_replies(outbuf, bufsize, params, 69, *ppdata, 0);
900
901   return -1;
902 }
903
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)
909 {
910         /*
911          * Go through and cancel any pending change notifies.
912          */
913         
914         int mid = SVAL(inbuf,smb_mid);
915         remove_pending_change_notify_requests_by_mid(mid);
916         remove_pending_lock_requests_by_mid(mid);
917         
918         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
919
920         return(-1);
921 }
922
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)
928 {
929         DEBUG(4,("Ignoring nttranss of length %d\n",length));
930         return(-1);
931 }
932
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, 
938                                    int bufsize,
939                                    char **ppsetup, char **ppparams, char **ppdata)
940 {
941   char *params = *ppparams;
942   pstring new_name;
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));
947   int outsize = 0;
948
949   CHECK_FSP(fsp, conn);
950   StrnCpy(new_name,params+4,fname_len);
951   new_name[fname_len] = '\0';
952
953   outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
954                              new_name, replace_if_exists);
955   if(outsize == 0) {
956     /*
957      * Rename was successful.
958      */
959     send_nt_replies(outbuf, bufsize, NULL, 0, NULL, 0);
960
961     DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n", 
962           fsp->fsp_name, new_name));
963
964     outsize = -1;
965   }
966
967   return(outsize);
968 }
969    
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 *****************************************************************************/
976
977 typedef struct {
978   ubi_slNode msg_next;
979   files_struct *fsp;
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];
985 } change_notify_buf;
986
987 static ubi_slList change_notify_queue = { NULL, (ubi_slNodePtr)&change_notify_queue, 0};
988
989 /****************************************************************************
990  Setup the common parts of the return packet and send it.
991 *****************************************************************************/
992
993 static void change_notify_reply_packet(char *inbuf, int error_class, uint32 error_code)
994 {
995   extern int Client;
996   char outbuf[smb_size+38];
997
998   memset(outbuf, '\0', sizeof(outbuf));
999   construct_reply_common(inbuf, outbuf);
1000
1001   /*
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.
1007    */
1008
1009   if(error_class == 0) /* NT Error. */
1010     SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1011
1012   ERROR(error_class,error_code);
1013
1014   /*
1015    * Seems NT needs a transact command with an error code
1016    * in it. This is a longer packet than a simple error.
1017    */
1018   set_message(outbuf,18,0,False);
1019
1020   send_smb(Client,outbuf);
1021 }
1022
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)
1027 {
1028   change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1029   change_notify_buf *prev = NULL;
1030
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));
1035       continue;
1036     }
1037
1038     prev = cnbp;
1039     cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1040   }
1041 }
1042
1043 /****************************************************************************
1044  Delete entries by mid from the change notify pending queue. Always send reply.
1045 *****************************************************************************/
1046
1047 static void remove_pending_change_notify_requests_by_mid(int mid)
1048 {
1049   change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1050   change_notify_buf *prev = NULL;
1051
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));
1057       continue;
1058     }
1059
1060     prev = cnbp;
1061     cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1062   }
1063 }
1064
1065 /****************************************************************************
1066  Process the change notify queue. Note that this is only called as root.
1067 *****************************************************************************/
1068
1069 void process_pending_change_notify_queue(time_t t)
1070 {
1071   change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1072   change_notify_buf *prev = NULL;
1073
1074   if(cnbp == NULL)
1075     return;
1076
1077   if(cnbp->next_check_time >= t)
1078     return;
1079
1080   /*
1081    * It's time to check. Go through the queue and see if
1082    * the timestamps changed.
1083    */
1084
1085   while((cnbp != NULL) && (cnbp->next_check_time <= t)) {
1086     SMB_STRUCT_STAT st;
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);
1091
1092     /*
1093      * Ensure we don't have any old chain_fsp values
1094      * sitting around....
1095      */
1096     chain_size = 0;
1097     file_chain_reset();
1098
1099     if(!become_user(conn,vuid)) {
1100       DEBUG(0,("process_pending_change_notify_queue: Unable to become user vuid=%d.\n",
1101             vuid ));
1102       /*
1103        * Remove the entry and return an error to the client.
1104        */
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));
1108       continue;
1109     }
1110
1111     if(!become_service(conn,True)) {
1112             DEBUG(0,("process_pending_change_notify_queue: Unable to become service Error was %s.\n", strerror(errno) ));
1113       /*
1114        * Remove the entry and return an error to the client.
1115        */
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));
1119       unbecome_user();
1120       continue;
1121     }
1122
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) ));
1126       /*
1127        * Remove the entry and return an error to the client.
1128        */
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));
1132       unbecome_user();
1133       continue;
1134     }
1135
1136     if(cnbp->modify_time != st.st_mtime ||
1137        cnbp->status_time != st.st_ctime) {
1138       /*
1139        * Remove the entry and return a change notify to the client.
1140        */
1141       DEBUG(5,("process_pending_change_notify_queue: directory name = %s changed\n",
1142             fsp->fsp_name ));
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));
1146       unbecome_user();
1147       continue;
1148     }
1149
1150     unbecome_user();
1151
1152     /*
1153      * Move to the next in the list.
1154      */
1155     prev = cnbp;
1156     cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1157   }
1158 }
1159
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,
1166                                           int bufsize, 
1167                                           char **ppsetup, 
1168                                           char **ppparams, char **ppdata)
1169 {
1170   char *setup = *ppsetup;
1171   files_struct *fsp;
1172   change_notify_buf *cnbp;
1173   SMB_STRUCT_STAT st;
1174
1175   fsp = file_fsp(setup,4);
1176
1177   DEBUG(3,("call_nt_transact_notify_change\n"));
1178
1179   if(!fsp)
1180     return(ERROR(ERRDOS,ERRbadfid));
1181
1182   if((!fsp->open) || (!fsp->is_directory) || (conn != fsp->conn))
1183     return(ERROR(ERRDOS,ERRbadfid));
1184
1185   /*
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'
1190    * error.
1191    */
1192
1193   if((cnbp = (change_notify_buf *)malloc(sizeof(change_notify_buf))) == NULL) {
1194     DEBUG(0,("call_nt_transact_notify_change: Malloc fail (2) !\n" ));
1195     return -1;
1196   }
1197
1198   /* 
1199    * Store the current timestamp on the directory we are monitoring.
1200    */
1201
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) ));
1205     free((char *)cnbp);
1206     return(UNIXERROR(ERRDOS,ERRbadfid));
1207   }
1208  
1209   memcpy(cnbp->request_buf, inbuf, smb_size);
1210   cnbp->fsp = fsp;
1211   cnbp->conn = conn;
1212   cnbp->modify_time = st.st_mtime;
1213   cnbp->status_time = st.st_ctime;
1214
1215   cnbp->next_check_time = time(NULL) + lp_change_notify_timeout();
1216
1217   /*
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.
1221    */
1222
1223   ubi_slAddTail(&change_notify_queue, cnbp);
1224
1225   DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1226 name = %s\n", fsp->fsp_name ));
1227
1228   return -1;
1229 }
1230
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, 
1237                                                 int length, 
1238                                                 int bufsize, 
1239                                                 char **ppsetup, char **ppparams, char **ppdata)
1240 {
1241   DEBUG(0,("call_nt_transact_query_security_desc: Currently not implemented.\n"));
1242   return(ERROR(ERRSRV,ERRnosupport));
1243 }
1244    
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, 
1251                                               int length,
1252                                               int bufsize, 
1253                                               char **ppsetup, 
1254                                               char **ppparams, char **ppdata)
1255 {
1256         DEBUG(0,("call_nt_transact_set_security_desc: Currently not implemented.\n"));
1257         return(ERROR(ERRSRV,ERRnosupport));
1258 }
1259    
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,
1265                                   int bufsize, 
1266                                   char **ppsetup, char **ppparams, char **ppdata)
1267 {
1268         DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1269         return(ERROR(ERRSRV,ERRnosupport));
1270 }
1271    
1272 /****************************************************************************
1273  Reply to a SMBNTtrans.
1274 ****************************************************************************/
1275 int reply_nttrans(connection_struct *conn,
1276                   char *inbuf,char *outbuf,int length,int bufsize)
1277 {
1278   int  outsize = 0;
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;
1294
1295   if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1296     /*
1297      * Queue this open message as we are the process of an oplock break.
1298      */
1299
1300     DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1301 due to being in oplock break state.\n" ));
1302
1303     push_oplock_pending_smb_message( inbuf, length);
1304     return -1;
1305   }
1306
1307   outsize = set_message(outbuf,0,0,True);
1308
1309   /* 
1310    * All nttrans messages we handle have smb_wct == 19 + setup_count.
1311    * Ensure this is so as a sanity check.
1312    */
1313
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));
1318   }
1319     
1320   /* Allocate the space for the setup, the maximum needed parameters and data */
1321
1322   if(setup_count > 0)
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);
1328  
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));
1333   }
1334
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;
1339
1340   if (parameter_count > total_parameter_count || data_count > total_data_count)
1341     exit_server("reply_nttrans: invalid sizes in packet.\n");
1342
1343   if(setup) {
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);
1347   }
1348   if(params) {
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);
1352   }
1353   if(data) {
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);
1357   }
1358
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);
1364
1365     while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1366       BOOL ret;
1367
1368       ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
1369
1370       if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1371         outsize = set_message(outbuf,0,0,True);
1372         if(ret) {
1373                 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1374         } else {
1375                 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1376                          (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1377         }
1378         if(params)
1379           free(params);
1380         if(data)
1381           free(data);
1382         if(setup)
1383           free(setup);
1384         return(ERROR(ERRSRV,ERRerror));
1385       }
1386       
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");
1394
1395       memcpy( &params[ 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);
1399     }
1400   }
1401
1402   if (Protocol >= PROTOCOL_NT1) {
1403     uint16 flg2 = SVAL(outbuf,smb_flg2);
1404     SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
1405   }
1406
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, &params, &data);
1412       break;
1413     case NT_TRANSACT_IOCTL:
1414       outsize = call_nt_transact_ioctl(conn, 
1415                                        inbuf, outbuf, length, bufsize, 
1416                                        &setup, &params, &data);
1417       break;
1418     case NT_TRANSACT_SET_SECURITY_DESC:
1419       outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf, 
1420                                                    length, bufsize, 
1421                                                    &setup, &params, &data);
1422       break;
1423     case NT_TRANSACT_NOTIFY_CHANGE:
1424       outsize = call_nt_transact_notify_change(conn, inbuf, outbuf, 
1425                                                length, bufsize, 
1426                                                &setup, &params, &data);
1427       break;
1428     case NT_TRANSACT_RENAME:
1429       outsize = call_nt_transact_rename(conn, inbuf, outbuf, length, 
1430                                         bufsize, 
1431                                         &setup, &params, &data);
1432       break;
1433
1434     case NT_TRANSACT_QUERY_SECURITY_DESC:
1435       outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf, 
1436                                                      length, bufsize, 
1437                                                      &setup, &params, &data);
1438       break;
1439   default:
1440           /* Error in request */
1441           DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1442           if(setup)
1443                   free(setup);
1444           if(params)
1445                   free(params);
1446           if(data)
1447                   free(data);
1448           return (ERROR(ERRSRV,ERRerror));
1449   }
1450
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
1455      an error packet. 
1456   */
1457
1458   if(setup)
1459     free(setup);
1460   if(params)
1461     free(params);
1462   if(data)
1463     free(data);
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. */
1467 }