Ok - this is the 64 bit widening check in. It changes the configure
[samba.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 oplock_sock;
29 extern int smb_read_error;
30 extern int global_oplock_break;
31 extern int chain_size;
32 extern BOOL case_sensitive;
33 extern BOOL case_preserve;
34 extern BOOL short_case_preserve;
35
36 static char *known_nt_pipes[] = {
37   "\\LANMAN",
38   "\\srvsvc",
39   "\\samr",
40   "\\wkssvc",
41   "\\NETLOGON",
42   "\\ntlsa",
43   "\\ntsvcs",
44   "\\lsass",
45   "\\lsarpc",
46   NULL
47 };
48
49 /****************************************************************************
50  Send the required number of replies back.
51  We assume all fields other than the data fields are
52  set correctly for the type of call.
53  HACK ! Always assumes smb_setup field is zero.
54 ****************************************************************************/
55
56 static int send_nt_replies(char *outbuf, int bufsize, char *params,
57                            int paramsize, char *pdata, int datasize)
58 {
59   extern int max_send;
60   int data_to_send = datasize;
61   int params_to_send = paramsize;
62   int useable_space;
63   char *pp = params;
64   char *pd = pdata;
65   int params_sent_thistime, data_sent_thistime, total_sent_thistime;
66   int alignment_offset = 3;
67   int data_alignment_offset = 0;
68
69   /*
70    * Initially set the wcnt area to be 18 - this is true for all
71    * transNT replies.
72    */
73
74   set_message(outbuf,18,0,True);
75
76   /* 
77    * If there genuinely are no parameters or data to send just send
78    * the empty packet.
79    */
80
81   if(params_to_send == 0 && data_to_send == 0) {
82     send_smb(Client,outbuf);
83     return 0;
84   }
85
86   /*
87    * When sending params and data ensure that both are nicely aligned.
88    * Only do this alignment when there is also data to send - else
89    * can cause NT redirector problems.
90    */
91
92   if (((params_to_send % 4) != 0) && (data_to_send != 0))
93     data_alignment_offset = 4 - (params_to_send % 4);
94
95   /* 
96    * Space is bufsize minus Netbios over TCP header minus SMB header.
97    * The alignment_offset is to align the param bytes on a four byte
98    * boundary (2 bytes for data len, one byte pad). 
99    * NT needs this to work correctly.
100    */
101
102   useable_space = bufsize - ((smb_buf(outbuf)+
103                     alignment_offset+data_alignment_offset) -
104                     outbuf);
105
106   /*
107    * useable_space can never be more than max_send minus the
108    * alignment offset.
109    */
110
111   useable_space = MIN(useable_space,
112                       max_send - (alignment_offset+data_alignment_offset));
113
114
115   while (params_to_send || data_to_send) {
116
117     /*
118      * Calculate whether we will totally or partially fill this packet.
119      */
120
121     total_sent_thistime = params_to_send + data_to_send +
122                             alignment_offset + data_alignment_offset;
123
124     /* 
125      * We can never send more than useable_space.
126      */
127
128     total_sent_thistime = MIN(total_sent_thistime, useable_space);
129
130     set_message(outbuf, 18, total_sent_thistime, True);
131
132     /*
133      * Set total params and data to be sent.
134      */
135
136     SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
137     SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
138
139     /* 
140      * Calculate how many parameters and data we can fit into
141      * this packet. Parameters get precedence.
142      */
143
144     params_sent_thistime = MIN(params_to_send,useable_space);
145     data_sent_thistime = useable_space - params_sent_thistime;
146     data_sent_thistime = MIN(data_sent_thistime,data_to_send);
147
148     SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
149
150     if(params_sent_thistime == 0) {
151       SIVAL(outbuf,smb_ntr_ParameterOffset,0);
152       SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
153     } else {
154       /*
155        * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
156        * parameter bytes, however the first 4 bytes of outbuf are
157        * the Netbios over TCP header. Thus use smb_base() to subtract
158        * them from the calculation.
159        */
160
161       SIVAL(outbuf,smb_ntr_ParameterOffset,
162             ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
163       /* 
164        * Absolute displacement of param bytes sent in this packet.
165        */
166
167       SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
168     }
169
170     /*
171      * Deal with the data portion.
172      */
173
174     SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
175
176     if(data_sent_thistime == 0) {
177       SIVAL(outbuf,smb_ntr_DataOffset,0);
178       SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
179     } else {
180       /*
181        * The offset of the data bytes is the offset of the
182        * parameter bytes plus the number of parameters being sent this time.
183        */
184
185       SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
186             smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
187       SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
188     }
189
190     /* 
191      * Copy the param bytes into the packet.
192      */
193
194     if(params_sent_thistime)
195       memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
196
197     /*
198      * Copy in the data bytes
199      */
200
201     if(data_sent_thistime)
202       memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
203              data_alignment_offset,pd,data_sent_thistime);
204     
205     DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
206           params_sent_thistime, data_sent_thistime, useable_space));
207     DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
208           params_to_send, data_to_send, paramsize, datasize));
209     
210     /* Send the packet */
211     send_smb(Client,outbuf);
212     
213     pp += params_sent_thistime;
214     pd += data_sent_thistime;
215     
216     params_to_send -= params_sent_thistime;
217     data_to_send -= data_sent_thistime;
218
219     /*
220      * Sanity check
221      */
222
223     if(params_to_send < 0 || data_to_send < 0) {
224       DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
225             params_to_send, data_to_send));
226       return -1;
227     }
228   } 
229
230   return 0;
231 }
232
233 /****************************************************************************
234  Save case statics.
235 ****************************************************************************/
236
237 static BOOL saved_case_sensitive;
238 static BOOL saved_case_preserve;
239 static BOOL saved_short_case_preserve;
240
241 /****************************************************************************
242  Save case semantics.
243 ****************************************************************************/
244
245 static void set_posix_case_semantics(uint32 file_attributes)
246 {
247   if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
248     return;
249
250   saved_case_sensitive = case_sensitive;
251   saved_case_preserve = case_preserve;
252   saved_short_case_preserve = short_case_preserve;
253
254   /* Set to POSIX. */
255   case_sensitive = True;
256   case_preserve = True;
257   short_case_preserve = True;
258 }
259
260 /****************************************************************************
261  Restore case semantics.
262 ****************************************************************************/
263
264 static void restore_case_semantics(uint32 file_attributes)
265 {
266   if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
267     return;
268
269   case_sensitive = saved_case_sensitive;
270   case_preserve = saved_case_preserve;
271   short_case_preserve = saved_short_case_preserve;
272 }
273
274 /****************************************************************************
275  Utility function to map create disposition.
276 ****************************************************************************/
277
278 static int map_create_disposition( uint32 create_disposition)
279 {
280   switch( create_disposition ) {
281   case FILE_CREATE:
282     /* create if not exist, fail if exist */
283     return 0x10;
284   case FILE_SUPERSEDE:
285   case FILE_OVERWRITE_IF:
286     /* create if not exist, trunc if exist */
287     return 0x12;
288   case FILE_OPEN:
289     /* fail if not exist, open if exists */
290     return 0x1;
291   case FILE_OPEN_IF:
292     /* create if not exist, open if exists */
293     return 0x11;
294   case FILE_OVERWRITE:
295     /* fail if not exist, truncate if exists */
296     return 0x2;
297   default:
298     DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
299              create_disposition ));
300     return -1;
301   }
302 }
303
304 /****************************************************************************
305  Utility function to map share modes.
306 ****************************************************************************/
307
308 static int map_share_mode( uint32 desired_access, uint32 share_access, uint32 file_attributes)
309 {
310   int smb_open_mode = -1;
311
312   switch( desired_access & (FILE_READ_DATA|FILE_WRITE_DATA) ) {
313   case FILE_READ_DATA:
314     smb_open_mode = 0;
315     break;
316   case FILE_WRITE_DATA:
317     smb_open_mode = 1;
318     break;
319   case FILE_READ_DATA|FILE_WRITE_DATA:
320     smb_open_mode = 2;
321     break;
322   }
323
324   if (smb_open_mode == -1) {
325     if(desired_access & (DELETE_ACCESS|FILE_WRITE_ATTRIBUTES))
326       smb_open_mode = 2;
327     else if( desired_access & FILE_EXECUTE)
328       smb_open_mode = 0;
329     else {
330       DEBUG(0,("map_share_mode: Incorrect value for desired_access = %x\n",
331              desired_access));
332       return -1;
333     }
334   }
335
336   /* Add in the requested share mode - ignore FILE_SHARE_DELETE for now. */
337   switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
338   case FILE_SHARE_READ:
339     smb_open_mode |= (DENY_WRITE<<4);
340     break;
341   case FILE_SHARE_WRITE:
342     smb_open_mode |= (DENY_READ<<4);
343     break;
344   case (FILE_SHARE_READ|FILE_SHARE_WRITE):
345     smb_open_mode |= (DENY_NONE<<4);
346     break;
347   case FILE_SHARE_NONE:
348     smb_open_mode |= (DENY_ALL<<4);
349     break;
350   }
351
352   /*
353    * Handle a O_SYNC request.
354    */
355   if(file_attributes & FILE_FLAG_WRITE_THROUGH)
356     smb_open_mode |= (1<<14);
357
358   return smb_open_mode;
359 }
360
361 /****************************************************************************
362  Reply to an NT create and X call on a pipe.
363 ****************************************************************************/
364 static int nt_open_pipe(char *fname, connection_struct *conn,
365                         char *inbuf, char *outbuf, int *ppnum)
366 {
367         pipes_struct *p = NULL;
368
369         uint16 vuid = SVAL(inbuf, smb_uid);
370         int i;
371
372         DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
373     
374         /* See if it is one we want to handle. */
375         for( i = 0; known_nt_pipes[i]; i++ )
376                 if( strequal(fname,known_nt_pipes[i]))
377                         break;
378     
379         if ( known_nt_pipes[i] == NULL )
380                 return(ERROR(ERRSRV,ERRaccess));
381     
382         /* Strip \\ off the name. */
383         fname++;
384     
385         DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
386
387         p = open_rpc_pipe_p(fname, conn, vuid);
388         if (!p)
389                 return(ERROR(ERRSRV,ERRnofids));
390
391         *ppnum = p->pnum;
392
393         return 0;
394 }
395
396 /****************************************************************************
397  Reply to an NT create and X call.
398 ****************************************************************************/
399 int reply_ntcreate_and_X(connection_struct *conn,
400                          char *inbuf,char *outbuf,int length,int bufsize)
401 {  
402         pstring fname;
403         uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
404         uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
405         uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
406         uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
407         uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
408         uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
409                                ((uint32)sizeof(fname)-1));
410         int smb_ofun;
411         int smb_open_mode;
412         int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
413         /* Breakout the oplock request bits so we can set the
414            reply bits separately. */
415         int oplock_request = 0;
416         int unixmode, pnum = -1;
417         int fmode=0,mtime=0,rmode=0;
418         SMB_OFF_T file_len = 0;
419         SMB_STRUCT_STAT sbuf;
420         int smb_action = 0;
421         BOOL bad_path = False;
422         files_struct *fsp=NULL;
423         char *p = NULL;
424
425         /* 
426          * We need to construct the open_and_X ofun value from the
427          * NT values, as that's what our code is structured to accept.
428          */    
429         
430         if((smb_ofun = map_create_disposition( create_disposition )) == -1)
431                 return(ERROR(ERRDOS,ERRbadaccess));
432
433         /*
434          * Now contruct the smb_open_mode value from the desired access
435          * and the share access.
436          */
437         
438         if((smb_open_mode = map_share_mode(desired_access, 
439                                            share_access, 
440                                            file_attributes)) == -1) {
441                 return(ERROR(ERRDOS,ERRbadaccess));
442         }
443
444         /*
445          * Get the file name.
446          */
447         StrnCpy(fname,smb_buf(inbuf),fname_len);
448         fname[fname_len] = '\0';
449         
450         /* If it's an IPC, use the pipe handler. */
451
452         if (IS_IPC(conn)) {
453
454                 int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
455                 if(ret != 0)
456                         return ret;
457
458                 /*
459                  * Deal with pipe return.
460                  */  
461
462                 set_message(outbuf,34,0,True);
463         
464                 p = outbuf + smb_vwv2;
465                 p++;
466                 SSVAL(p,0,pnum);
467                 p += 2;
468                 SIVAL(p,0,FILE_WAS_OPENED);
469                 p += 4;
470                 p += 32;
471                 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
472                 p += 20;
473                 /* File type. */
474                 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
475                 /* Device state. */
476                 SSVAL(p,2, 0x5FF); /* ? */
477
478                 DEBUG(5,("reply_ntcreate_and_X: open pipe = %s\n", fname));
479
480                 return chain_reply(inbuf,outbuf,length,bufsize);
481         }
482
483         /*
484          * Ordinary file or directory.
485          */
486                 
487         /*
488          * Check if POSIX semantics are wanted.
489          */
490                 
491         set_posix_case_semantics(file_attributes);
492                 
493         unix_convert(fname,conn,0,&bad_path,NULL);
494                 
495         fsp = file_new();
496         if (!fsp) {
497                 restore_case_semantics(file_attributes);
498                 return(ERROR(ERRSRV,ERRnofids));
499         }
500                 
501         if (!check_name(fname,conn)) { 
502                 if((errno == ENOENT) && bad_path) {
503                         unix_ERR_class = ERRDOS;
504                         unix_ERR_code = ERRbadpath;
505                 }
506                 file_free(fsp);
507                 
508                 restore_case_semantics(file_attributes);
509                 
510                 return(UNIXERROR(ERRDOS,ERRnoaccess));
511         } 
512                 
513         unixmode = unix_mode(conn,smb_attr | aARCH);
514     
515         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
516         oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
517
518         /* 
519          * If it's a request for a directory open, deal with it separately.
520          */
521
522         if(flags & OPEN_DIRECTORY) {
523                 oplock_request = 0;
524                 
525                 open_directory(fsp, conn, fname, smb_ofun, 
526                                unixmode, &smb_action);
527                         
528                 restore_case_semantics(file_attributes);
529
530                 if(!fsp->open) {
531                         file_free(fsp);
532                         return(UNIXERROR(ERRDOS,ERRnoaccess));
533                 }
534         } else {
535                 /*
536                  * Ordinary file case.
537                  */
538
539                 /* NB. We have a potential bug here. If we
540                  * cause an oplock break to ourselves, then we
541                  * could end up processing filename related
542                  * SMB requests whilst we await the oplock
543                  * break response. As we may have changed the
544                  * filename case semantics to be POSIX-like,
545                  * this could mean a filename request could
546                  * fail when it should succeed. This is a rare
547                  * condition, but eventually we must arrange
548                  * to restore the correct case semantics
549                  * before issuing an oplock break request to
550                  * our client. JRA.  */
551
552                 open_file_shared(fsp,conn,fname,smb_open_mode,
553                                  smb_ofun,unixmode,
554                                  oplock_request,&rmode,&smb_action);
555
556                 if (!fsp->open) { 
557                         /* We cheat here. The only case we
558                          * care about is a directory rename,
559                          * where the NT client will attempt to
560                          * open the source directory for
561                          * DELETE access. Note that when the
562                          * NT client does this it does *not*
563                          * set the directory bit in the
564                          * request packet. This is translated
565                          * into a read/write open
566                          * request. POSIX states that any open
567                          * for write request on a directory
568                          * will generate an EISDIR error, so
569                          * we can catch this here and open a
570                          * pseudo handle that is flagged as a
571                          * directory. JRA.  */
572
573                         if(errno == EISDIR) {
574                                 oplock_request = 0;
575                                 
576                                 open_directory(fsp, conn, fname, smb_ofun, unixmode, &smb_action);
577                                 
578                                 if(!fsp->open) {
579                                         file_free(fsp);
580                                         restore_case_semantics(file_attributes);
581                                         return(UNIXERROR(ERRDOS,ERRnoaccess));
582                                 }
583                         } else {
584                                 if((errno == ENOENT) && bad_path) {
585                                         unix_ERR_class = ERRDOS;
586                                         unix_ERR_code = ERRbadpath;
587                                 }
588                                 
589                                 file_free(fsp);
590                                 
591                                 restore_case_semantics(file_attributes);
592                                 
593                                 return(UNIXERROR(ERRDOS,ERRnoaccess));
594                         }
595                 } 
596         }
597                 
598         if(fsp->is_directory) {
599                 if(dos_stat(fsp->fsp_name, &sbuf) != 0) {
600                         close_directory(fsp);
601                         restore_case_semantics(file_attributes);
602                         return(ERROR(ERRDOS,ERRnoaccess));
603                 }
604         } else {
605                 if (sys_fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
606                         close_file(fsp,False);
607                         restore_case_semantics(file_attributes);
608                         return(ERROR(ERRDOS,ERRnoaccess));
609                 } 
610         }
611                 
612         restore_case_semantics(file_attributes);
613                 
614         file_len = sbuf.st_size;
615         fmode = dos_mode(conn,fname,&sbuf);
616         if(fmode == 0)
617                 fmode = FILE_ATTRIBUTE_NORMAL;
618         mtime = sbuf.st_mtime;
619         if (!fsp->is_directory && (fmode & aDIR)) {
620                 close_file(fsp,False);
621                 return(ERROR(ERRDOS,ERRnoaccess));
622         } 
623         
624         /* 
625          * If the caller set the extended oplock request bit
626          * and we granted one (by whatever means) - set the
627          * correct bit for extended oplock reply.
628          */
629         
630         if (oplock_request && lp_fake_oplocks(SNUM(conn)))
631                 smb_action |= EXTENDED_OPLOCK_GRANTED;
632         
633         if(oplock_request && fsp->granted_oplock)
634                 smb_action |= EXTENDED_OPLOCK_GRANTED;
635         
636         set_message(outbuf,34,0,True);
637         
638         p = outbuf + smb_vwv2;
639         
640         /*
641          * Currently as we don't support level II oplocks we just report
642          * exclusive & batch here.
643          */
644         
645         SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
646         p++;
647         SSVAL(p,0,fsp->fnum);
648         p += 2;
649         SIVAL(p,0,smb_action);
650         p += 4;
651         
652         /* Create time. */  
653         put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
654         p += 8;
655         put_long_date(p,sbuf.st_atime); /* access time */
656         p += 8;
657         put_long_date(p,sbuf.st_mtime); /* write time */
658         p += 8;
659         put_long_date(p,sbuf.st_mtime); /* change time */
660         p += 8;
661         SIVAL(p,0,fmode); /* File Attributes. */
662         p += 12;
663 #ifdef LARGE_SMB_OFF_T
664         SIVAL(p,0, file_len & 0xFFFFFFFF);
665         SIVAL(p,4, file_len >> 32);
666 #else /* LARGE_SMB_OFF_T */
667         SIVAL(p,0,file_len);
668 #endif /* LARGE_SMB_OFF_T */
669         p += 12;
670         SCVAL(p,0,fsp->is_directory ? 1 : 0);
671         
672         DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
673
674         return chain_reply(inbuf,outbuf,length,bufsize);
675 }
676
677 /****************************************************************************
678  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
679 ****************************************************************************/
680 static int call_nt_transact_create(connection_struct *conn,
681                                    char *inbuf, char *outbuf, int length, 
682                                    int bufsize, 
683                                    char **ppsetup, char **ppparams, 
684                                    char **ppdata)
685 {
686   pstring fname;
687   char *params = *ppparams;
688   uint32 flags = IVAL(params,0);
689   uint32 desired_access = IVAL(params,8);
690   uint32 file_attributes = IVAL(params,20);
691   uint32 share_access = IVAL(params,24);
692   uint32 create_disposition = IVAL(params,28);
693   uint32 fname_len = MIN(((uint32)IVAL(params,44)),
694                          ((uint32)sizeof(fname)-1));
695   int smb_ofun;
696   int smb_open_mode;
697   int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
698   /* Breakout the oplock request bits so we can set the
699      reply bits separately. */
700   int oplock_request = 0;
701   int unixmode, pnum = -1;
702   int fmode=0,mtime=0,rmode=0;
703   off_t file_len = 0;
704   SMB_STRUCT_STAT sbuf;
705   int smb_action = 0;
706   BOOL bad_path = False;
707   files_struct *fsp = NULL;
708   char *p = NULL;
709
710   /* 
711    * We need to construct the open_and_X ofun value from the
712    * NT values, as that's what our code is structured to accept.
713    */    
714
715   if((smb_ofun = map_create_disposition( create_disposition )) == -1)
716     return(ERROR(ERRDOS,ERRbadaccess));
717
718   /*
719    * Now contruct the smb_open_mode value from the desired access
720    * and the share access.
721    */
722
723   if((smb_open_mode = map_share_mode( desired_access, share_access, file_attributes)) == -1)
724     return(ERROR(ERRDOS,ERRbadaccess));
725
726   /*
727    * Get the file name.
728    */
729
730   StrnCpy(fname,params+53,fname_len);
731   fname[fname_len] = '\0';
732
733   /* If it's an IPC, use the pipe handler. */
734   if (IS_IPC(conn)) {
735     int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
736     if(ret != 0)
737       return ret;
738     smb_action = FILE_WAS_OPENED;
739   } else {
740     /*
741      * Check if POSIX semantics are wanted.
742      */
743
744     set_posix_case_semantics(file_attributes);
745
746     unix_convert(fname,conn,0,&bad_path,NULL);
747     
748     fsp = file_new();
749     if (!fsp) {
750             restore_case_semantics(file_attributes);
751             return(ERROR(ERRSRV,ERRnofids));
752     }
753
754     if (!check_name(fname,conn)) { 
755       if((errno == ENOENT) && bad_path) {
756         unix_ERR_class = ERRDOS;
757         unix_ERR_code = ERRbadpath;
758       }
759       file_free(fsp);
760
761       restore_case_semantics(file_attributes);
762
763       return(UNIXERROR(ERRDOS,ERRnoaccess));
764     } 
765   
766     unixmode = unix_mode(conn,smb_attr | aARCH);
767     
768     oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
769     oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
770
771     /*
772      * If it's a request for a directory open, deal with it separately.
773      */
774
775     if(flags & OPEN_DIRECTORY) {
776
777       oplock_request = 0;
778
779       /*
780        * We will get a create directory here if the Win32
781        * app specified a security descriptor in the 
782        * CreateDirectory() call.
783        */
784
785       open_directory(fsp, conn, fname, smb_ofun, unixmode, &smb_action);
786
787       if(!fsp->open) {
788         file_free(fsp);
789         return(UNIXERROR(ERRDOS,ERRnoaccess));
790       }
791     } else {
792
793       /*
794        * Ordinary file case.
795        */
796
797       open_file_shared(fsp,conn,fname,smb_open_mode,smb_ofun,unixmode,
798                        oplock_request,&rmode,&smb_action);
799
800       if (!fsp->open) { 
801         if((errno == ENOENT) && bad_path) {
802           unix_ERR_class = ERRDOS;
803           unix_ERR_code = ERRbadpath;
804         }
805         file_free(fsp);
806
807         restore_case_semantics(file_attributes);
808
809         return(UNIXERROR(ERRDOS,ERRnoaccess));
810       } 
811   
812       if (sys_fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
813         close_file(fsp,False);
814
815         restore_case_semantics(file_attributes);
816
817         return(ERROR(ERRDOS,ERRnoaccess));
818       } 
819   
820       file_len = sbuf.st_size;
821       fmode = dos_mode(conn,fname,&sbuf);
822       if(fmode == 0)
823         fmode = FILE_ATTRIBUTE_NORMAL;
824       mtime = sbuf.st_mtime;
825
826       if (fmode & aDIR) {
827         close_file(fsp,False);
828         restore_case_semantics(file_attributes);
829         return(ERROR(ERRDOS,ERRnoaccess));
830       } 
831
832       /* 
833        * If the caller set the extended oplock request bit
834        * and we granted one (by whatever means) - set the
835        * correct bit for extended oplock reply.
836        */
837     
838       if (oplock_request && lp_fake_oplocks(SNUM(conn)))
839         smb_action |= EXTENDED_OPLOCK_GRANTED;
840   
841       if(oplock_request && fsp->granted_oplock)
842         smb_action |= EXTENDED_OPLOCK_GRANTED;
843     }
844   }
845
846   restore_case_semantics(file_attributes);
847
848   /* Realloc the size of parameters and data we will return */
849   params = *ppparams = Realloc(*ppparams, 69);
850   if(params == NULL)
851     return(ERROR(ERRDOS,ERRnomem));
852
853   p = params;
854   SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
855   p += 2;
856   if (IS_IPC(conn)) {
857           SSVAL(p,0,pnum);
858   } else {
859           SSVAL(p,0,fsp->fnum);
860   }
861   p += 2;
862   SIVAL(p,0,smb_action);
863   p += 8;
864
865   if (IS_IPC(conn)) {
866     /*
867      * Deal with pipe return.
868      */  
869     p += 32;
870     SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
871     p += 20;
872     /* File type. */
873     SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
874     /* Device state. */
875     SSVAL(p,2, 0x5FF); /* ? */
876   } else {
877     /*
878      * Deal with file return.
879      */
880     /* Create time. */
881     put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
882     p += 8;
883     put_long_date(p,sbuf.st_atime); /* access time */
884     p += 8;
885     put_long_date(p,sbuf.st_mtime); /* write time */
886     p += 8;
887     put_long_date(p,sbuf.st_mtime); /* change time */
888     p += 8;
889     SIVAL(p,0,fmode); /* File Attributes. */
890     p += 12;
891 #if OFF_T_IS_64_BITS
892       SIVAL(p,0, file_len & 0xFFFFFFFF);
893       SIVAL(p,4, (file_len >> 32));
894 #else /* OFF_T_IS_64_BITS */
895       SIVAL(p,0,file_len);
896 #endif /* OFF_T_IS_64_BITS */
897   }
898
899   /* Send the required number of replies */
900   send_nt_replies(outbuf, bufsize, params, 69, *ppdata, 0);
901
902   return -1;
903 }
904
905 /****************************************************************************
906  Reply to a NT CANCEL request.
907 ****************************************************************************/
908 int reply_ntcancel(connection_struct *conn,
909                    char *inbuf,char *outbuf,int length,int bufsize)
910 {
911         /*
912          * Go through and cancel any pending change notifies.
913          */
914         
915         int mid = SVAL(inbuf,smb_mid);
916         remove_pending_change_notify_requests_by_mid(mid);
917         remove_pending_lock_requests_by_mid(mid);
918         
919         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
920
921         return(-1);
922 }
923
924 /****************************************************************************
925  Reply to an unsolicited SMBNTtranss - just ignore it!
926 ****************************************************************************/
927 int reply_nttranss(connection_struct *conn,
928                    char *inbuf,char *outbuf,int length,int bufsize)
929 {
930         DEBUG(4,("Ignoring nttranss of length %d\n",length));
931         return(-1);
932 }
933
934 /****************************************************************************
935  Reply to an NT transact rename command.
936 ****************************************************************************/
937 static int call_nt_transact_rename(connection_struct *conn,
938                                    char *inbuf, char *outbuf, int length, 
939                                    int bufsize,
940                                    char **ppsetup, char **ppparams, char **ppdata)
941 {
942   char *params = *ppparams;
943   pstring new_name;
944   files_struct *fsp = file_fsp(params, 0);
945   BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
946   uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
947                          ((uint32)sizeof(new_name)-1));
948   int outsize = 0;
949
950   CHECK_FSP(fsp, conn);
951   StrnCpy(new_name,params+4,fname_len);
952   new_name[fname_len] = '\0';
953
954   outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
955                              new_name, replace_if_exists);
956   if(outsize == 0) {
957     /*
958      * Rename was successful.
959      */
960     send_nt_replies(outbuf, bufsize, NULL, 0, NULL, 0);
961
962     DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n", 
963           fsp->fsp_name, new_name));
964
965     outsize = -1;
966   }
967
968   return(outsize);
969 }
970    
971 /****************************************************************************
972  This is the structure to queue to implement NT change
973  notify. It consists of smb_size bytes stored from the
974  transact command (to keep the mid, tid etc around).
975  Plus the fid to examine and the time to check next.
976 *****************************************************************************/
977
978 typedef struct {
979   ubi_slNode msg_next;
980   files_struct *fsp;
981   connection_struct *conn;
982   time_t next_check_time;
983   time_t modify_time; /* Info from the directory we're monitoring. */ 
984   time_t status_time; /* Info from the directory we're monitoring. */
985   char request_buf[smb_size];
986 } change_notify_buf;
987
988 static ubi_slList change_notify_queue = { NULL, (ubi_slNodePtr)&change_notify_queue, 0};
989
990 /****************************************************************************
991  Setup the common parts of the return packet and send it.
992 *****************************************************************************/
993
994 static void change_notify_reply_packet(char *inbuf, int error_class, uint32 error_code)
995 {
996   extern int Client;
997   char outbuf[smb_size+38];
998
999   memset(outbuf, '\0', sizeof(outbuf));
1000   construct_reply_common(inbuf, outbuf);
1001
1002   /*
1003    * If we're returning a 'too much in the directory changed' we need to
1004    * set this is an NT error status flags. If we don't then the (probably
1005    * untested) code in the NT redirector has a bug in that it doesn't re-issue
1006    * the change notify.... Ah - I *love* it when I get so deeply into this I
1007    * can even determine how MS failed to test stuff and why.... :-). JRA.
1008    */
1009
1010   if(error_class == 0) /* NT Error. */
1011     SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1012
1013   ERROR(error_class,error_code);
1014
1015   /*
1016    * Seems NT needs a transact command with an error code
1017    * in it. This is a longer packet than a simple error.
1018    */
1019   set_message(outbuf,18,0,False);
1020
1021   send_smb(Client,outbuf);
1022 }
1023
1024 /****************************************************************************
1025  Delete entries by fnum from the change notify pending queue.
1026 *****************************************************************************/
1027 void remove_pending_change_notify_requests_by_fid(files_struct *fsp)
1028 {
1029   change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1030   change_notify_buf *prev = NULL;
1031
1032   while(cnbp != NULL) {
1033     if(cnbp->fsp->fnum == fsp->fnum) {
1034       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1035       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1036       continue;
1037     }
1038
1039     prev = cnbp;
1040     cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1041   }
1042 }
1043
1044 /****************************************************************************
1045  Delete entries by mid from the change notify pending queue. Always send reply.
1046 *****************************************************************************/
1047
1048 void remove_pending_change_notify_requests_by_mid(int mid)
1049 {
1050   change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1051   change_notify_buf *prev = NULL;
1052
1053   while(cnbp != NULL) {
1054     if(SVAL(cnbp->request_buf,smb_mid) == mid) {
1055       change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_CANCELLED);
1056       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1057       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1058       continue;
1059     }
1060
1061     prev = cnbp;
1062     cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1063   }
1064 }
1065
1066 /****************************************************************************
1067  Process the change notify queue. Note that this is only called as root.
1068 *****************************************************************************/
1069
1070 void process_pending_change_notify_queue(time_t t)
1071 {
1072   change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1073   change_notify_buf *prev = NULL;
1074
1075   if(cnbp == NULL)
1076     return;
1077
1078   if(cnbp->next_check_time >= t)
1079     return;
1080
1081   /*
1082    * It's time to check. Go through the queue and see if
1083    * the timestamps changed.
1084    */
1085
1086   while((cnbp != NULL) && (cnbp->next_check_time <= t)) {
1087     SMB_STRUCT_STAT st;
1088     files_struct *fsp = cnbp->fsp;
1089     connection_struct *conn = cnbp->conn;
1090     uint16 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID : 
1091                   SVAL(cnbp->request_buf,smb_uid);
1092
1093     /*
1094      * Ensure we don't have any old chain_fsp values
1095      * sitting around....
1096      */
1097     chain_size = 0;
1098     file_chain_reset();
1099
1100     if(!become_user(conn,vuid)) {
1101       DEBUG(0,("process_pending_change_notify_queue: Unable to become user vuid=%d.\n",
1102             vuid ));
1103       /*
1104        * Remove the entry and return an error to the client.
1105        */
1106       change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1107       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1108       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1109       continue;
1110     }
1111
1112     if(!become_service(conn,True)) {
1113             DEBUG(0,("process_pending_change_notify_queue: Unable to become service Error was %s.\n", strerror(errno) ));
1114       /*
1115        * Remove the entry and return an error to the client.
1116        */
1117       change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1118       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1119       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1120       unbecome_user();
1121       continue;
1122     }
1123
1124     if(dos_stat(fsp->fsp_name, &st) < 0) {
1125       DEBUG(0,("process_pending_change_notify_queue: Unable to stat directory %s. \
1126 Error was %s.\n", fsp->fsp_name, strerror(errno) ));
1127       /*
1128        * Remove the entry and return an error to the client.
1129        */
1130       change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1131       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1132       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1133       unbecome_user();
1134       continue;
1135     }
1136
1137     if(cnbp->modify_time != st.st_mtime ||
1138        cnbp->status_time != st.st_ctime) {
1139       /*
1140        * Remove the entry and return a change notify to the client.
1141        */
1142       DEBUG(5,("process_pending_change_notify_queue: directory name = %s changed\n",
1143             fsp->fsp_name ));
1144       change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_NOTIFY_ENUM_DIR);
1145       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1146       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1147       unbecome_user();
1148       continue;
1149     }
1150
1151     unbecome_user();
1152
1153     /*
1154      * Move to the next in the list.
1155      */
1156     prev = cnbp;
1157     cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1158   }
1159 }
1160
1161 /****************************************************************************
1162  Reply to a notify change - queue the request and 
1163  don't allow a directory to be opened.
1164 ****************************************************************************/
1165 static int call_nt_transact_notify_change(connection_struct *conn,
1166                                           char *inbuf, char *outbuf, int length,
1167                                           int bufsize, 
1168                                           char **ppsetup, 
1169                                           char **ppparams, char **ppdata)
1170 {
1171   char *setup = *ppsetup;
1172   files_struct *fsp;
1173   change_notify_buf *cnbp;
1174   SMB_STRUCT_STAT st;
1175
1176   fsp = file_fsp(setup,4);
1177
1178   DEBUG(3,("call_nt_transact_notify_change\n"));
1179
1180   if(!fsp)
1181     return(ERROR(ERRDOS,ERRbadfid));
1182
1183   if((!fsp->open) || (!fsp->is_directory) || (conn != fsp->conn))
1184     return(ERROR(ERRDOS,ERRbadfid));
1185
1186   /*
1187    * Now queue an entry on the notify change stack. We timestamp
1188    * the entry we are adding so that we know when to scan next.
1189    * We only need to save smb_size bytes from this incoming packet
1190    * as we will always by returning a 'read the directory yourself'
1191    * error.
1192    */
1193
1194   if((cnbp = (change_notify_buf *)malloc(sizeof(change_notify_buf))) == NULL) {
1195     DEBUG(0,("call_nt_transact_notify_change: Malloc fail (2) !\n" ));
1196     return -1;
1197   }
1198
1199   /* 
1200    * Store the current timestamp on the directory we are monitoring.
1201    */
1202
1203   if(dos_stat(fsp->fsp_name, &st) < 0) {
1204     DEBUG(0,("call_nt_transact_notify_change: Unable to stat name = %s. \
1205 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1206     free((char *)cnbp);
1207     return(UNIXERROR(ERRDOS,ERRbadfid));
1208   }
1209  
1210   memcpy(cnbp->request_buf, inbuf, smb_size);
1211   cnbp->fsp = fsp;
1212   cnbp->conn = conn;
1213   cnbp->modify_time = st.st_mtime;
1214   cnbp->status_time = st.st_ctime;
1215
1216   cnbp->next_check_time = time(NULL) + lp_change_notify_timeout();
1217
1218   /*
1219    * Adding to the tail enables us to check only
1220    * the head when scanning for change, as this entry
1221    * is forced to have the first timeout expiration.
1222    */
1223
1224   ubi_slAddTail(&change_notify_queue, cnbp);
1225
1226   DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1227 name = %s\n", fsp->fsp_name ));
1228
1229   return -1;
1230 }
1231
1232 /****************************************************************************
1233  Reply to query a security descriptor - currently this is not implemented (it
1234  is planned to be though).
1235 ****************************************************************************/
1236 static int call_nt_transact_query_security_desc(connection_struct *conn,
1237                                                 char *inbuf, char *outbuf, 
1238                                                 int length, 
1239                                                 int bufsize, 
1240                                                 char **ppsetup, char **ppparams, char **ppdata)
1241 {
1242   DEBUG(0,("call_nt_transact_query_security_desc: Currently not implemented.\n"));
1243   return(ERROR(ERRSRV,ERRnosupport));
1244 }
1245    
1246 /****************************************************************************
1247  Reply to set a security descriptor - currently this is not implemented (it
1248  is planned to be though).
1249 ****************************************************************************/
1250 static int call_nt_transact_set_security_desc(connection_struct *conn,
1251                                               char *inbuf, char *outbuf, 
1252                                               int length,
1253                                               int bufsize, 
1254                                               char **ppsetup, 
1255                                               char **ppparams, char **ppdata)
1256 {
1257         DEBUG(0,("call_nt_transact_set_security_desc: Currently not implemented.\n"));
1258         return(ERROR(ERRSRV,ERRnosupport));
1259 }
1260    
1261 /****************************************************************************
1262  Reply to IOCTL - not implemented - no plans.
1263 ****************************************************************************/
1264 static int call_nt_transact_ioctl(connection_struct *conn,
1265                                   char *inbuf, char *outbuf, int length,
1266                                   int bufsize, 
1267                                   char **ppsetup, char **ppparams, char **ppdata)
1268 {
1269         DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1270         return(ERROR(ERRSRV,ERRnosupport));
1271 }
1272    
1273 /****************************************************************************
1274  Reply to a SMBNTtrans.
1275 ****************************************************************************/
1276 int reply_nttrans(connection_struct *conn,
1277                   char *inbuf,char *outbuf,int length,int bufsize)
1278 {
1279   int  outsize = 0;
1280 #if 0 /* Not used. */
1281   uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1282   uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1283   uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1284 #endif /* Not used. */
1285   uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1286   uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1287   uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1288   uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1289   uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1290   uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1291   uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1292   uint16 function_code = SVAL( inbuf, smb_nt_Function);
1293   char *params = NULL, *data = NULL, *setup = NULL;
1294   uint32 num_params_sofar, num_data_sofar;
1295
1296   if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1297     /*
1298      * Queue this open message as we are the process of an oplock break.
1299      */
1300
1301     DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1302 due to being in oplock break state.\n" ));
1303
1304     push_oplock_pending_smb_message( inbuf, length);
1305     return -1;
1306   }
1307
1308   outsize = set_message(outbuf,0,0,True);
1309
1310   /* 
1311    * All nttrans messages we handle have smb_wct == 19 + setup_count.
1312    * Ensure this is so as a sanity check.
1313    */
1314
1315   if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1316     DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1317           CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1318     return(ERROR(ERRSRV,ERRerror));
1319   }
1320     
1321   /* Allocate the space for the setup, the maximum needed parameters and data */
1322
1323   if(setup_count > 0)
1324     setup = (char *)malloc(setup_count);
1325   if (total_parameter_count > 0)
1326     params = (char *)malloc(total_parameter_count);
1327   if (total_data_count > 0)
1328     data = (char *)malloc(total_data_count);
1329  
1330   if ((total_parameter_count && !params)  || (total_data_count && !data) ||
1331       (setup_count && !setup)) {
1332     DEBUG(0,("reply_nttrans : Out of memory\n"));
1333     return(ERROR(ERRDOS,ERRnomem));
1334   }
1335
1336   /* Copy the param and data bytes sent with this request into
1337      the params buffer */
1338   num_params_sofar = parameter_count;
1339   num_data_sofar = data_count;
1340
1341   if (parameter_count > total_parameter_count || data_count > total_data_count)
1342     exit_server("reply_nttrans: invalid sizes in packet.\n");
1343
1344   if(setup) {
1345     memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1346     DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1347     dump_data(10, setup, setup_count);
1348   }
1349   if(params) {
1350     memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1351     DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1352     dump_data(10, params, parameter_count);
1353   }
1354   if(data) {
1355     memcpy( data, smb_base(inbuf) + data_offset, data_count);
1356     DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1357     dump_data(10, data, data_count);
1358   }
1359
1360   if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1361     /* We need to send an interim response then receive the rest
1362        of the parameter/data bytes */
1363     outsize = set_message(outbuf,0,0,True);
1364     send_smb(Client,outbuf);
1365
1366     while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1367       BOOL ret;
1368
1369       ret = receive_next_smb(Client,oplock_sock,inbuf,bufsize,
1370                              SMB_SECONDARY_WAIT);
1371
1372       if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1373         outsize = set_message(outbuf,0,0,True);
1374         if(ret) {
1375                 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1376         } else {
1377                 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1378                          (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1379         }
1380         if(params)
1381           free(params);
1382         if(data)
1383           free(data);
1384         if(setup)
1385           free(setup);
1386         return(ERROR(ERRSRV,ERRerror));
1387       }
1388       
1389       /* Revise total_params and total_data in case they have changed downwards */
1390       total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1391       total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1392       num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1393       num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1394       if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1395         exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
1396
1397       memcpy( &params[ IVAL(inbuf, smb_nts_ParameterDisplacement)], 
1398               smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1399       memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1400               smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1401     }
1402   }
1403
1404   if (Protocol >= PROTOCOL_NT1) {
1405     uint16 flg2 = SVAL(outbuf,smb_flg2);
1406     SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
1407   }
1408
1409   /* Now we must call the relevant NT_TRANS function */
1410   switch(function_code) {
1411     case NT_TRANSACT_CREATE:
1412       outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize, 
1413                                         &setup, &params, &data);
1414       break;
1415     case NT_TRANSACT_IOCTL:
1416       outsize = call_nt_transact_ioctl(conn, 
1417                                        inbuf, outbuf, length, bufsize, 
1418                                        &setup, &params, &data);
1419       break;
1420     case NT_TRANSACT_SET_SECURITY_DESC:
1421       outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf, 
1422                                                    length, bufsize, 
1423                                                    &setup, &params, &data);
1424       break;
1425     case NT_TRANSACT_NOTIFY_CHANGE:
1426       outsize = call_nt_transact_notify_change(conn, inbuf, outbuf, 
1427                                                length, bufsize, 
1428                                                &setup, &params, &data);
1429       break;
1430     case NT_TRANSACT_RENAME:
1431       outsize = call_nt_transact_rename(conn, inbuf, outbuf, length, 
1432                                         bufsize, 
1433                                         &setup, &params, &data);
1434       break;
1435
1436     case NT_TRANSACT_QUERY_SECURITY_DESC:
1437       outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf, 
1438                                                      length, bufsize, 
1439                                                      &setup, &params, &data);
1440       break;
1441   default:
1442           /* Error in request */
1443           DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1444           if(setup)
1445                   free(setup);
1446           if(params)
1447                   free(params);
1448           if(data)
1449                   free(data);
1450           return (ERROR(ERRSRV,ERRerror));
1451   }
1452
1453   /* As we do not know how many data packets will need to be
1454      returned here the various call_nt_transact_xxxx calls
1455      must send their own. Thus a call_nt_transact_xxxx routine only
1456      returns a value other than -1 when it wants to send
1457      an error packet. 
1458   */
1459
1460   if(setup)
1461     free(setup);
1462   if(params)
1463     free(params);
1464   if(data)
1465     free(data);
1466   return outsize; /* If a correct response was needed the call_nt_transact_xxxx 
1467                      calls have already sent it. If outsize != -1 then it is
1468                      returning an error packet. */
1469 }