ee3385f152fa86ac26f9bb8a46a041f91c169e39
[tprouty/samba.git] / source / 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 void remove_pending_change_notify_requests_by_mid(int mid);
37
38 static char *known_nt_pipes[] = {
39   "\\LANMAN",
40   "\\srvsvc",
41   "\\samr",
42   "\\wkssvc",
43   "\\NETLOGON",
44   "\\ntlsa",
45   "\\ntsvcs",
46   "\\lsass",
47   "\\lsarpc",
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 #ifdef LARGE_SMB_OFF_T
668     SIVAL(p,0, file_len);
669     SIVAL(p,4, file_len >> 32);
670 #else /* LARGE_SMB_OFF_T */
671     SIVAL(p,0,file_len);
672 #endif /* LARGE_SMB_OFF_T */
673         p += 8;
674 #ifdef LARGE_SMB_OFF_T
675         SIVAL(p,0, file_len);
676         SIVAL(p,4, file_len >> 32);
677 #else /* LARGE_SMB_OFF_T */
678         SIVAL(p,0,file_len);
679 #endif /* LARGE_SMB_OFF_T */
680         p += 12;
681         SCVAL(p,0,fsp->is_directory ? 1 : 0);
682         
683         DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
684
685         return chain_reply(inbuf,outbuf,length,bufsize);
686 }
687
688 /****************************************************************************
689  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
690 ****************************************************************************/
691 static int call_nt_transact_create(connection_struct *conn,
692                                    char *inbuf, char *outbuf, int length, 
693                                    int bufsize, 
694                                    char **ppsetup, char **ppparams, 
695                                    char **ppdata)
696 {
697   pstring fname;
698   char *params = *ppparams;
699   uint32 flags = IVAL(params,0);
700   uint32 desired_access = IVAL(params,8);
701   uint32 file_attributes = IVAL(params,20);
702   uint32 share_access = IVAL(params,24);
703   uint32 create_disposition = IVAL(params,28);
704   uint32 create_options = IVAL(params,32);
705   uint32 fname_len = MIN(((uint32)IVAL(params,44)),
706                          ((uint32)sizeof(fname)-1));
707   int smb_ofun;
708   int smb_open_mode;
709   int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
710   /* Breakout the oplock request bits so we can set the
711      reply bits separately. */
712   int oplock_request = 0;
713   mode_t unixmode;
714   int pnum = -1;
715   int fmode=0,rmode=0;
716   SMB_OFF_T file_len = 0;
717   SMB_STRUCT_STAT sbuf;
718   int smb_action = 0;
719   BOOL bad_path = False;
720   files_struct *fsp = NULL;
721   char *p = NULL;
722
723   /* 
724    * We need to construct the open_and_X ofun value from the
725    * NT values, as that's what our code is structured to accept.
726    */    
727
728   if((smb_ofun = map_create_disposition( create_disposition )) == -1)
729     return(ERROR(ERRDOS,ERRbadaccess));
730
731   /*
732    * Now contruct the smb_open_mode value from the desired access
733    * and the share access.
734    */
735
736   if((smb_open_mode = map_share_mode( desired_access, share_access, file_attributes)) == -1)
737     return(ERROR(ERRDOS,ERRbadaccess));
738
739   /*
740    * Get the file name.
741    */
742
743   StrnCpy(fname,params+53,fname_len);
744   fname[fname_len] = '\0';
745
746   /* If it's an IPC, use the pipe handler. */
747   if (IS_IPC(conn)) {
748     int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
749     if(ret != 0)
750       return ret;
751     smb_action = FILE_WAS_OPENED;
752   } else {
753     /*
754      * Check if POSIX semantics are wanted.
755      */
756
757     set_posix_case_semantics(file_attributes);
758
759     unix_convert(fname,conn,0,&bad_path,NULL);
760     
761     fsp = file_new();
762     if (!fsp) {
763             restore_case_semantics(file_attributes);
764             return(ERROR(ERRSRV,ERRnofids));
765     }
766
767     if (!check_name(fname,conn)) { 
768       if((errno == ENOENT) && bad_path) {
769         unix_ERR_class = ERRDOS;
770         unix_ERR_code = ERRbadpath;
771       }
772       file_free(fsp);
773
774       restore_case_semantics(file_attributes);
775
776       return(UNIXERROR(ERRDOS,ERRnoaccess));
777     } 
778   
779     unixmode = unix_mode(conn,smb_attr | aARCH);
780     
781     oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
782     oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
783
784     /*
785      * If it's a request for a directory open, deal with it separately.
786      */
787
788     if(create_options & FILE_DIRECTORY_FILE) {
789
790       oplock_request = 0;
791
792       /*
793        * We will get a create directory here if the Win32
794        * app specified a security descriptor in the 
795        * CreateDirectory() call.
796        */
797
798       open_directory(fsp, conn, fname, smb_ofun, unixmode, &smb_action);
799
800       if(!fsp->open) {
801         file_free(fsp);
802         return(UNIXERROR(ERRDOS,ERRnoaccess));
803       }
804     } else {
805
806       /*
807        * Ordinary file case.
808        */
809
810       open_file_shared(fsp,conn,fname,smb_open_mode,smb_ofun,unixmode,
811                        oplock_request,&rmode,&smb_action);
812
813       if (!fsp->open) { 
814         if((errno == ENOENT) && bad_path) {
815           unix_ERR_class = ERRDOS;
816           unix_ERR_code = ERRbadpath;
817         }
818         file_free(fsp);
819
820         restore_case_semantics(file_attributes);
821
822         return(UNIXERROR(ERRDOS,ERRnoaccess));
823       } 
824   
825       if (sys_fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
826         close_file(fsp,False);
827
828         restore_case_semantics(file_attributes);
829
830         return(ERROR(ERRDOS,ERRnoaccess));
831       } 
832   
833       file_len = sbuf.st_size;
834       fmode = dos_mode(conn,fname,&sbuf);
835       if(fmode == 0)
836         fmode = FILE_ATTRIBUTE_NORMAL;
837
838       if (fmode & aDIR) {
839         close_file(fsp,False);
840         restore_case_semantics(file_attributes);
841         return(ERROR(ERRDOS,ERRnoaccess));
842       } 
843
844       /* 
845        * If the caller set the extended oplock request bit
846        * and we granted one (by whatever means) - set the
847        * correct bit for extended oplock reply.
848        */
849     
850       if (oplock_request && lp_fake_oplocks(SNUM(conn)))
851         smb_action |= EXTENDED_OPLOCK_GRANTED;
852   
853       if(oplock_request && fsp->granted_oplock)
854         smb_action |= EXTENDED_OPLOCK_GRANTED;
855     }
856   }
857
858   restore_case_semantics(file_attributes);
859
860   /* Realloc the size of parameters and data we will return */
861   params = *ppparams = Realloc(*ppparams, 69);
862   if(params == NULL)
863     return(ERROR(ERRDOS,ERRnomem));
864
865   p = params;
866   SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
867   p += 2;
868   if (IS_IPC(conn)) {
869           SSVAL(p,0,pnum);
870   } else {
871           SSVAL(p,0,fsp->fnum);
872   }
873   p += 2;
874   SIVAL(p,0,smb_action);
875   p += 8;
876
877   if (IS_IPC(conn)) {
878     /*
879      * Deal with pipe return.
880      */  
881     p += 32;
882     SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
883     p += 20;
884     /* File type. */
885     SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
886     /* Device state. */
887     SSVAL(p,2, 0x5FF); /* ? */
888   } else {
889     /*
890      * Deal with file return.
891      */
892     /* Create time. */
893     put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
894     p += 8;
895     put_long_date(p,sbuf.st_atime); /* access time */
896     p += 8;
897     put_long_date(p,sbuf.st_mtime); /* write time */
898     p += 8;
899     put_long_date(p,sbuf.st_mtime); /* change time */
900     p += 8;
901     SIVAL(p,0,fmode); /* File Attributes. */
902     p += 4;
903 #ifdef LARGE_SMB_OFF_T
904     SIVAL(p,0, file_len);
905     SIVAL(p,4, (file_len >> 32));
906 #else /* LARGE_SMB_OFF_T */
907     SIVAL(p,0,file_len);
908 #endif /* LARGE_SMB_OFF_T */
909     p += 8;
910 #ifdef LARGE_SMB_OFF_T
911     SIVAL(p,0, file_len);
912     SIVAL(p,4, (file_len >> 32));
913 #else /* LARGE_SMB_OFF_T */
914     SIVAL(p,0,file_len);
915 #endif /* LARGE_SMB_OFF_T */
916   }
917
918   /* Send the required number of replies */
919   send_nt_replies(outbuf, bufsize, params, 69, *ppdata, 0);
920
921   return -1;
922 }
923
924 /****************************************************************************
925  Reply to a NT CANCEL request.
926 ****************************************************************************/
927 int reply_ntcancel(connection_struct *conn,
928                    char *inbuf,char *outbuf,int length,int bufsize)
929 {
930         /*
931          * Go through and cancel any pending change notifies.
932          */
933         
934         int mid = SVAL(inbuf,smb_mid);
935         remove_pending_change_notify_requests_by_mid(mid);
936         remove_pending_lock_requests_by_mid(mid);
937         
938         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
939
940         return(-1);
941 }
942
943 /****************************************************************************
944  Reply to an unsolicited SMBNTtranss - just ignore it!
945 ****************************************************************************/
946 int reply_nttranss(connection_struct *conn,
947                    char *inbuf,char *outbuf,int length,int bufsize)
948 {
949         DEBUG(4,("Ignoring nttranss of length %d\n",length));
950         return(-1);
951 }
952
953 /****************************************************************************
954  Reply to an NT transact rename command.
955 ****************************************************************************/
956 static int call_nt_transact_rename(connection_struct *conn,
957                                    char *inbuf, char *outbuf, int length, 
958                                    int bufsize,
959                                    char **ppsetup, char **ppparams, char **ppdata)
960 {
961   char *params = *ppparams;
962   pstring new_name;
963   files_struct *fsp = file_fsp(params, 0);
964   BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
965   uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
966                          ((uint32)sizeof(new_name)-1));
967   int outsize = 0;
968
969   CHECK_FSP(fsp, conn);
970   StrnCpy(new_name,params+4,fname_len);
971   new_name[fname_len] = '\0';
972
973   outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
974                              new_name, replace_if_exists);
975   if(outsize == 0) {
976     /*
977      * Rename was successful.
978      */
979     send_nt_replies(outbuf, bufsize, NULL, 0, NULL, 0);
980
981     DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n", 
982           fsp->fsp_name, new_name));
983
984     outsize = -1;
985   }
986
987   return(outsize);
988 }
989    
990 /****************************************************************************
991  This is the structure to queue to implement NT change
992  notify. It consists of smb_size bytes stored from the
993  transact command (to keep the mid, tid etc around).
994  Plus the fid to examine and the time to check next.
995 *****************************************************************************/
996
997 typedef struct {
998   ubi_slNode msg_next;
999   files_struct *fsp;
1000   connection_struct *conn;
1001   time_t next_check_time;
1002   time_t modify_time; /* Info from the directory we're monitoring. */ 
1003   time_t status_time; /* Info from the directory we're monitoring. */
1004   char request_buf[smb_size];
1005 } change_notify_buf;
1006
1007 static ubi_slList change_notify_queue = { NULL, (ubi_slNodePtr)&change_notify_queue, 0};
1008
1009 /****************************************************************************
1010  Setup the common parts of the return packet and send it.
1011 *****************************************************************************/
1012
1013 static void change_notify_reply_packet(char *inbuf, int error_class, uint32 error_code)
1014 {
1015   extern int Client;
1016   char outbuf[smb_size+38];
1017
1018   memset(outbuf, '\0', sizeof(outbuf));
1019   construct_reply_common(inbuf, outbuf);
1020
1021   /*
1022    * If we're returning a 'too much in the directory changed' we need to
1023    * set this is an NT error status flags. If we don't then the (probably
1024    * untested) code in the NT redirector has a bug in that it doesn't re-issue
1025    * the change notify.... Ah - I *love* it when I get so deeply into this I
1026    * can even determine how MS failed to test stuff and why.... :-). JRA.
1027    */
1028
1029   if(error_class == 0) /* NT Error. */
1030     SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1031
1032   ERROR(error_class,error_code);
1033
1034   /*
1035    * Seems NT needs a transact command with an error code
1036    * in it. This is a longer packet than a simple error.
1037    */
1038   set_message(outbuf,18,0,False);
1039
1040   send_smb(Client,outbuf);
1041 }
1042
1043 /****************************************************************************
1044  Delete entries by fnum from the change notify pending queue.
1045 *****************************************************************************/
1046 void remove_pending_change_notify_requests_by_fid(files_struct *fsp)
1047 {
1048   change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1049   change_notify_buf *prev = NULL;
1050
1051   while(cnbp != NULL) {
1052     if(cnbp->fsp->fnum == fsp->fnum) {
1053       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1054       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1055       continue;
1056     }
1057
1058     prev = cnbp;
1059     cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1060   }
1061 }
1062
1063 /****************************************************************************
1064  Delete entries by mid from the change notify pending queue. Always send reply.
1065 *****************************************************************************/
1066
1067 static void remove_pending_change_notify_requests_by_mid(int mid)
1068 {
1069   change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1070   change_notify_buf *prev = NULL;
1071
1072   while(cnbp != NULL) {
1073     if(SVAL(cnbp->request_buf,smb_mid) == mid) {
1074       change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_CANCELLED);
1075       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1076       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1077       continue;
1078     }
1079
1080     prev = cnbp;
1081     cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1082   }
1083 }
1084
1085 /****************************************************************************
1086  Process the change notify queue. Note that this is only called as root.
1087 *****************************************************************************/
1088
1089 void process_pending_change_notify_queue(time_t t)
1090 {
1091   change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1092   change_notify_buf *prev = NULL;
1093
1094   if(cnbp == NULL)
1095     return;
1096
1097   if(cnbp->next_check_time >= t)
1098     return;
1099
1100   /*
1101    * It's time to check. Go through the queue and see if
1102    * the timestamps changed.
1103    */
1104
1105   while((cnbp != NULL) && (cnbp->next_check_time <= t)) {
1106     SMB_STRUCT_STAT st;
1107     files_struct *fsp = cnbp->fsp;
1108     connection_struct *conn = cnbp->conn;
1109     uint16 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID : 
1110                   SVAL(cnbp->request_buf,smb_uid);
1111
1112     /*
1113      * Ensure we don't have any old chain_fsp values
1114      * sitting around....
1115      */
1116     chain_size = 0;
1117     file_chain_reset();
1118
1119     if(!become_user(conn,vuid)) {
1120       DEBUG(0,("process_pending_change_notify_queue: Unable to become user vuid=%d.\n",
1121             vuid ));
1122       /*
1123        * Remove the entry and return an error to the client.
1124        */
1125       change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1126       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1127       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1128       continue;
1129     }
1130
1131     if(!become_service(conn,True)) {
1132             DEBUG(0,("process_pending_change_notify_queue: Unable to become service Error was %s.\n", strerror(errno) ));
1133       /*
1134        * Remove the entry and return an error to the client.
1135        */
1136       change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1137       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1138       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1139       unbecome_user();
1140       continue;
1141     }
1142
1143     if(dos_stat(fsp->fsp_name, &st) < 0) {
1144       DEBUG(0,("process_pending_change_notify_queue: Unable to stat directory %s. \
1145 Error was %s.\n", fsp->fsp_name, strerror(errno) ));
1146       /*
1147        * Remove the entry and return an error to the client.
1148        */
1149       change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1150       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1151       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1152       unbecome_user();
1153       continue;
1154     }
1155
1156     if(cnbp->modify_time != st.st_mtime ||
1157        cnbp->status_time != st.st_ctime) {
1158       /*
1159        * Remove the entry and return a change notify to the client.
1160        */
1161       DEBUG(5,("process_pending_change_notify_queue: directory name = %s changed\n",
1162             fsp->fsp_name ));
1163       change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_NOTIFY_ENUM_DIR);
1164       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1165       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1166       unbecome_user();
1167       continue;
1168     }
1169
1170     unbecome_user();
1171
1172     /*
1173      * Move to the next in the list.
1174      */
1175     prev = cnbp;
1176     cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1177   }
1178 }
1179
1180 /****************************************************************************
1181  Reply to a notify change - queue the request and 
1182  don't allow a directory to be opened.
1183 ****************************************************************************/
1184 static int call_nt_transact_notify_change(connection_struct *conn,
1185                                           char *inbuf, char *outbuf, int length,
1186                                           int bufsize, 
1187                                           char **ppsetup, 
1188                                           char **ppparams, char **ppdata)
1189 {
1190   char *setup = *ppsetup;
1191   files_struct *fsp;
1192   change_notify_buf *cnbp;
1193   SMB_STRUCT_STAT st;
1194
1195   fsp = file_fsp(setup,4);
1196
1197   DEBUG(3,("call_nt_transact_notify_change\n"));
1198
1199   if(!fsp)
1200     return(ERROR(ERRDOS,ERRbadfid));
1201
1202   if((!fsp->open) || (!fsp->is_directory) || (conn != fsp->conn))
1203     return(ERROR(ERRDOS,ERRbadfid));
1204
1205   /*
1206    * Now queue an entry on the notify change stack. We timestamp
1207    * the entry we are adding so that we know when to scan next.
1208    * We only need to save smb_size bytes from this incoming packet
1209    * as we will always by returning a 'read the directory yourself'
1210    * error.
1211    */
1212
1213   if((cnbp = (change_notify_buf *)malloc(sizeof(change_notify_buf))) == NULL) {
1214     DEBUG(0,("call_nt_transact_notify_change: Malloc fail (2) !\n" ));
1215     return -1;
1216   }
1217
1218   /* 
1219    * Store the current timestamp on the directory we are monitoring.
1220    */
1221
1222   if(dos_stat(fsp->fsp_name, &st) < 0) {
1223     DEBUG(0,("call_nt_transact_notify_change: Unable to stat name = %s. \
1224 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1225     free((char *)cnbp);
1226     return(UNIXERROR(ERRDOS,ERRbadfid));
1227   }
1228  
1229   memcpy(cnbp->request_buf, inbuf, smb_size);
1230   cnbp->fsp = fsp;
1231   cnbp->conn = conn;
1232   cnbp->modify_time = st.st_mtime;
1233   cnbp->status_time = st.st_ctime;
1234
1235   cnbp->next_check_time = time(NULL) + lp_change_notify_timeout();
1236
1237   /*
1238    * Adding to the tail enables us to check only
1239    * the head when scanning for change, as this entry
1240    * is forced to have the first timeout expiration.
1241    */
1242
1243   ubi_slAddTail(&change_notify_queue, cnbp);
1244
1245   DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1246 name = %s\n", fsp->fsp_name ));
1247
1248   return -1;
1249 }
1250
1251 /****************************************************************************
1252  Reply to query a security descriptor - currently this is not implemented (it
1253  is planned to be though).
1254 ****************************************************************************/
1255 static int call_nt_transact_query_security_desc(connection_struct *conn,
1256                                                 char *inbuf, char *outbuf, 
1257                                                 int length, 
1258                                                 int bufsize, 
1259                                                 char **ppsetup, char **ppparams, char **ppdata)
1260 {
1261   DEBUG(0,("call_nt_transact_query_security_desc: Currently not implemented.\n"));
1262   return(ERROR(ERRSRV,ERRnosupport));
1263 }
1264    
1265 /****************************************************************************
1266  Reply to set a security descriptor - currently this is not implemented (it
1267  is planned to be though).
1268 ****************************************************************************/
1269 static int call_nt_transact_set_security_desc(connection_struct *conn,
1270                                               char *inbuf, char *outbuf, 
1271                                               int length,
1272                                               int bufsize, 
1273                                               char **ppsetup, 
1274                                               char **ppparams, char **ppdata)
1275 {
1276         DEBUG(0,("call_nt_transact_set_security_desc: Currently not implemented.\n"));
1277         return(ERROR(ERRSRV,ERRnosupport));
1278 }
1279    
1280 /****************************************************************************
1281  Reply to IOCTL - not implemented - no plans.
1282 ****************************************************************************/
1283 static int call_nt_transact_ioctl(connection_struct *conn,
1284                                   char *inbuf, char *outbuf, int length,
1285                                   int bufsize, 
1286                                   char **ppsetup, char **ppparams, char **ppdata)
1287 {
1288         DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1289         return(ERROR(ERRSRV,ERRnosupport));
1290 }
1291    
1292 /****************************************************************************
1293  Reply to a SMBNTtrans.
1294 ****************************************************************************/
1295 int reply_nttrans(connection_struct *conn,
1296                   char *inbuf,char *outbuf,int length,int bufsize)
1297 {
1298   int  outsize = 0;
1299 #if 0 /* Not used. */
1300   uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1301   uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1302   uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1303 #endif /* Not used. */
1304   uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1305   uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1306   uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1307   uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1308   uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1309   uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1310   uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1311   uint16 function_code = SVAL( inbuf, smb_nt_Function);
1312   char *params = NULL, *data = NULL, *setup = NULL;
1313   uint32 num_params_sofar, num_data_sofar;
1314
1315   if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1316     /*
1317      * Queue this open message as we are the process of an oplock break.
1318      */
1319
1320     DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1321 due to being in oplock break state.\n" ));
1322
1323     push_oplock_pending_smb_message( inbuf, length);
1324     return -1;
1325   }
1326
1327   outsize = set_message(outbuf,0,0,True);
1328
1329   /* 
1330    * All nttrans messages we handle have smb_wct == 19 + setup_count.
1331    * Ensure this is so as a sanity check.
1332    */
1333
1334   if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1335     DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1336           CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1337     return(ERROR(ERRSRV,ERRerror));
1338   }
1339     
1340   /* Allocate the space for the setup, the maximum needed parameters and data */
1341
1342   if(setup_count > 0)
1343     setup = (char *)malloc(setup_count);
1344   if (total_parameter_count > 0)
1345     params = (char *)malloc(total_parameter_count);
1346   if (total_data_count > 0)
1347     data = (char *)malloc(total_data_count);
1348  
1349   if ((total_parameter_count && !params)  || (total_data_count && !data) ||
1350       (setup_count && !setup)) {
1351     DEBUG(0,("reply_nttrans : Out of memory\n"));
1352     return(ERROR(ERRDOS,ERRnomem));
1353   }
1354
1355   /* Copy the param and data bytes sent with this request into
1356      the params buffer */
1357   num_params_sofar = parameter_count;
1358   num_data_sofar = data_count;
1359
1360   if (parameter_count > total_parameter_count || data_count > total_data_count)
1361     exit_server("reply_nttrans: invalid sizes in packet.\n");
1362
1363   if(setup) {
1364     memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1365     DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1366     dump_data(10, setup, setup_count);
1367   }
1368   if(params) {
1369     memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1370     DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1371     dump_data(10, params, parameter_count);
1372   }
1373   if(data) {
1374     memcpy( data, smb_base(inbuf) + data_offset, data_count);
1375     DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1376     dump_data(10, data, data_count);
1377   }
1378
1379   if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1380     /* We need to send an interim response then receive the rest
1381        of the parameter/data bytes */
1382     outsize = set_message(outbuf,0,0,True);
1383     send_smb(Client,outbuf);
1384
1385     while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1386       BOOL ret;
1387
1388       ret = receive_next_smb(Client,oplock_sock,inbuf,bufsize,
1389                              SMB_SECONDARY_WAIT);
1390
1391       if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1392         outsize = set_message(outbuf,0,0,True);
1393         if(ret) {
1394                 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1395         } else {
1396                 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1397                          (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1398         }
1399         if(params)
1400           free(params);
1401         if(data)
1402           free(data);
1403         if(setup)
1404           free(setup);
1405         return(ERROR(ERRSRV,ERRerror));
1406       }
1407       
1408       /* Revise total_params and total_data in case they have changed downwards */
1409       total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1410       total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1411       num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1412       num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1413       if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1414         exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
1415
1416       memcpy( &params[ IVAL(inbuf, smb_nts_ParameterDisplacement)], 
1417               smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1418       memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1419               smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1420     }
1421   }
1422
1423   if (Protocol >= PROTOCOL_NT1) {
1424     uint16 flg2 = SVAL(outbuf,smb_flg2);
1425     SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
1426   }
1427
1428   /* Now we must call the relevant NT_TRANS function */
1429   switch(function_code) {
1430     case NT_TRANSACT_CREATE:
1431       outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize, 
1432                                         &setup, &params, &data);
1433       break;
1434     case NT_TRANSACT_IOCTL:
1435       outsize = call_nt_transact_ioctl(conn, 
1436                                        inbuf, outbuf, length, bufsize, 
1437                                        &setup, &params, &data);
1438       break;
1439     case NT_TRANSACT_SET_SECURITY_DESC:
1440       outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf, 
1441                                                    length, bufsize, 
1442                                                    &setup, &params, &data);
1443       break;
1444     case NT_TRANSACT_NOTIFY_CHANGE:
1445       outsize = call_nt_transact_notify_change(conn, inbuf, outbuf, 
1446                                                length, bufsize, 
1447                                                &setup, &params, &data);
1448       break;
1449     case NT_TRANSACT_RENAME:
1450       outsize = call_nt_transact_rename(conn, inbuf, outbuf, length, 
1451                                         bufsize, 
1452                                         &setup, &params, &data);
1453       break;
1454
1455     case NT_TRANSACT_QUERY_SECURITY_DESC:
1456       outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf, 
1457                                                      length, bufsize, 
1458                                                      &setup, &params, &data);
1459       break;
1460   default:
1461           /* Error in request */
1462           DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1463           if(setup)
1464                   free(setup);
1465           if(params)
1466                   free(params);
1467           if(data)
1468                   free(data);
1469           return (ERROR(ERRSRV,ERRerror));
1470   }
1471
1472   /* As we do not know how many data packets will need to be
1473      returned here the various call_nt_transact_xxxx calls
1474      must send their own. Thus a call_nt_transact_xxxx routine only
1475      returns a value other than -1 when it wants to send
1476      an error packet. 
1477   */
1478
1479   if(setup)
1480     free(setup);
1481   if(params)
1482     free(params);
1483   if(data)
1484     free(data);
1485   return outsize; /* If a correct response was needed the call_nt_transact_xxxx 
1486                      calls have already sent it. If outsize != -1 then it is
1487                      returning an error packet. */
1488 }