8709b9c6468935431bf89f040fff7eafd21cff52
[nivanova/samba-autobuild/.git] / source3 / smbd / nttrans.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB NT transaction handling
5    Copyright (C) Jeremy Allison 1994-1998
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "nterr.h"
24
25 extern int DEBUGLEVEL;
26 extern int Protocol;
27 extern int chain_fnum;
28 extern connection_struct Connections[];
29 extern files_struct Files[];
30 extern int Client;  
31 extern int oplock_sock;
32 extern int smb_read_error;
33 extern int global_oplock_break;
34 extern BOOL case_sensitive;
35 extern BOOL case_preserve;
36 extern BOOL short_case_preserve;
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)
328       smb_open_mode = 2;
329     else if( desired_access & FILE_EXECUTE)
330       smb_open_mode = 0;
331     else {
332       DEBUG(0,("map_share_mode: Incorrect value for desired_access = %x\n",
333              desired_access));
334       return -1;
335     }
336   }
337
338   /* Add in the requested share mode - ignore FILE_SHARE_DELETE for now. */
339   switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
340   case FILE_SHARE_READ:
341     smb_open_mode |= (DENY_WRITE<<4);
342     break;
343   case FILE_SHARE_WRITE:
344     smb_open_mode |= (DENY_READ<<4);
345     break;
346   case (FILE_SHARE_READ|FILE_SHARE_WRITE):
347     smb_open_mode |= (DENY_NONE<<4);
348     break;
349   case FILE_SHARE_NONE:
350     smb_open_mode |= (DENY_ALL<<4);
351     break;
352   }
353
354   /*
355    * Handle a O_SYNC request.
356    */
357   if(file_attributes & FILE_FLAG_WRITE_THROUGH)
358     smb_open_mode |= (1<<14);
359
360   return smb_open_mode;
361 }
362
363 /****************************************************************************
364  Reply to an NT create and X call on a pipe.
365 ****************************************************************************/
366
367 static int nt_open_pipe(char *fname, char *inbuf, char *outbuf, int *ppnum)
368 {
369   int cnum = SVAL(inbuf,smb_tid);
370   int pnum = -1;
371   uint16 vuid = SVAL(inbuf, smb_uid);
372   int i;
373
374   DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
375     
376   /* See if it is one we want to handle. */
377   for( i = 0; known_nt_pipes[i]; i++ )
378     if( strequal(fname,known_nt_pipes[i]))
379       break;
380     
381   if ( known_nt_pipes[i] == NULL )
382     return(ERROR(ERRSRV,ERRaccess));
383     
384   /* Strip \\ off the name. */
385   fname++;
386     
387   DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
388
389   pnum = open_rpc_pipe_hnd(fname, cnum, vuid);
390   if (pnum < 0)
391     return(ERROR(ERRSRV,ERRnofids));
392
393   *ppnum = pnum + 0x800; /* Mark file handle up into high range. */
394   return 0;
395 }
396
397 /****************************************************************************
398  Reply to an NT create and X call.
399 ****************************************************************************/
400
401 int reply_ntcreate_and_X(char *inbuf,char *outbuf,int length,int bufsize)
402 {  
403   pstring fname;
404   int cnum = SVAL(inbuf,smb_tid);
405   int fnum = -1;
406   uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
407   uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
408   uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
409   uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
410   uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
411   uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
412                          ((uint32)sizeof(fname)-1));
413   int smb_ofun;
414   int smb_open_mode;
415   int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
416   /* Breakout the oplock request bits so we can set the
417      reply bits separately. */
418   int oplock_request = 0;
419   int unixmode;
420   int fmode=0,mtime=0,rmode=0;
421   off_t file_len = 0;
422   struct stat sbuf;
423   int smb_action = 0;
424   BOOL bad_path = False;
425   files_struct *fsp;
426   char *p = NULL;
427   
428   /* 
429    * We need to construct the open_and_X ofun value from the
430    * NT values, as that's what our code is structured to accept.
431    */    
432
433   if((smb_ofun = map_create_disposition( create_disposition )) == -1)
434     return(ERROR(ERRDOS,ERRbadaccess));
435
436   /*
437    * Now contruct the smb_open_mode value from the desired access
438    * and the share access.
439    */
440
441   if((smb_open_mode = map_share_mode( desired_access, share_access, file_attributes)) == -1)
442     return(ERROR(ERRDOS,ERRbadaccess));
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   if (IS_IPC(cnum)) {
452     int ret = nt_open_pipe(fname, inbuf, outbuf, &fnum);
453     if(ret != 0)
454       return ret;
455     smb_action = FILE_WAS_OPENED;
456   } else {
457
458     /*
459      * Ordinary file or directory.
460      */
461
462     /*
463      * Check if POSIX semantics are wanted.
464      */
465
466     set_posix_case_semantics(file_attributes);
467
468     unix_convert(fname,cnum,0,&bad_path);
469     
470     fnum = find_free_file();
471     if (fnum < 0) {
472       restore_case_semantics(file_attributes);
473       return(ERROR(ERRSRV,ERRnofids));
474     }
475
476     fsp = &Files[fnum];
477     
478     if (!check_name(fname,cnum)) { 
479       if((errno == ENOENT) && bad_path) {
480         unix_ERR_class = ERRDOS;
481         unix_ERR_code = ERRbadpath;
482       }
483       fsp->reserved = False;
484
485       restore_case_semantics(file_attributes);
486
487       return(UNIXERROR(ERRDOS,ERRnoaccess));
488     } 
489   
490     unixmode = unix_mode(cnum,smb_attr | aARCH);
491     
492     oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
493     oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
494
495     /* 
496      * If it's a request for a directory open, deal with it separately.
497      */
498
499     if(flags & OPEN_DIRECTORY) {
500       oplock_request = 0;
501
502       open_directory(fnum, cnum, fname, smb_ofun, unixmode, &smb_action);
503
504       restore_case_semantics(file_attributes);
505
506       if(!fsp->open) {
507         fsp->reserved = False;
508         return(UNIXERROR(ERRDOS,ERRnoaccess));
509       }
510     } else {
511
512       /*
513        * Ordinary file case.
514        */
515
516       /*
517        * NB. We have a potential bug here. If we cause an oplock
518        * break to ourselves, then we could end up processing filename
519        * related SMB requests whilst we await the oplock break
520        * response. As we may have changed the filename case
521        * semantics to be POSIX-like, this could mean a filename
522        * request could fail when it should succeed. This is a
523        * rare condition, but eventually we must arrange to restore
524        * the correct case semantics before issuing an oplock break
525        * request to our client. JRA.
526        */
527
528       open_file_shared(fnum,cnum,fname,smb_open_mode,smb_ofun,unixmode,
529                        oplock_request,&rmode,&smb_action);
530
531       if (!fsp->open) { 
532         /*
533          * We cheat here. The only case we care about is a directory
534          * rename, where the NT client will attempt to open the source
535          * directory for DELETE access. Note that when the NT client
536          * does this it does *not* set the directory bit in the
537          * request packet. This is translated into a read/write open
538          * request. POSIX states that any open for write request on a directory
539          * will generate an EISDIR error, so we can catch this here and open
540          * a pseudo handle that is flagged as a directory. JRA.
541          */
542
543         if(errno == EISDIR) {
544           oplock_request = 0;
545
546           open_directory(fnum, cnum, fname, smb_ofun, unixmode, &smb_action);
547
548           if(!fsp->open) {
549             fsp->reserved = False;
550             restore_case_semantics(file_attributes);
551             return(UNIXERROR(ERRDOS,ERRnoaccess));
552           }
553         } else {
554           if((errno == ENOENT) && bad_path) {
555             unix_ERR_class = ERRDOS;
556             unix_ERR_code = ERRbadpath;
557           }
558
559           fsp->reserved = False;
560
561           restore_case_semantics(file_attributes);
562
563           return(UNIXERROR(ERRDOS,ERRnoaccess));
564         }
565       } 
566     }
567   
568     if(fsp->is_directory) {
569       if(sys_stat(fsp->name, &sbuf) != 0) {
570         close_directory(fnum);
571         restore_case_semantics(file_attributes);
572         return(ERROR(ERRDOS,ERRnoaccess));
573       }
574     } else {
575       if (fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
576         close_file(fnum,False);
577         restore_case_semantics(file_attributes);
578         return(ERROR(ERRDOS,ERRnoaccess));
579       } 
580     }
581   
582     restore_case_semantics(file_attributes);
583
584     file_len = sbuf.st_size;
585     fmode = dos_mode(cnum,fname,&sbuf);
586     if(fmode == 0)
587       fmode = FILE_ATTRIBUTE_NORMAL;
588     mtime = sbuf.st_mtime;
589     if (!fsp->is_directory && (fmode & aDIR)) {
590       close_file(fnum,False);
591       return(ERROR(ERRDOS,ERRnoaccess));
592     } 
593   
594     /* 
595      * If the caller set the extended oplock request bit
596      * and we granted one (by whatever means) - set the
597      * correct bit for extended oplock reply.
598      */
599     
600     if (oplock_request && lp_fake_oplocks(SNUM(cnum)))
601       smb_action |= EXTENDED_OPLOCK_GRANTED;
602   
603     if(oplock_request && fsp->granted_oplock)
604       smb_action |= EXTENDED_OPLOCK_GRANTED;
605   }
606  
607   set_message(outbuf,34,0,True);
608
609   p = outbuf + smb_vwv2;
610
611   /*
612    * Currently as we don't support level II oplocks we just report
613    * exclusive & batch here.
614    */
615
616   SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
617   p++;
618   SSVAL(p,0,fnum);
619   p += 2;
620   SIVAL(p,0,smb_action);
621   p += 4;
622
623   if (IS_IPC(cnum)) {
624     /*
625      * Deal with pipe return.
626      */  
627     p += 32;
628     SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
629     p += 20;
630     /* File type. */
631     SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
632     /* Device state. */
633     SSVAL(p,2, 0x5FF); /* ? */
634   } else {
635     /*
636      * Deal with file return.
637      */  
638     /* Create time. */  
639     put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(cnum))));
640     p += 8;
641     put_long_date(p,sbuf.st_atime); /* access time */
642     p += 8;
643     put_long_date(p,sbuf.st_mtime); /* write time */
644     p += 8;
645     put_long_date(p,sbuf.st_mtime); /* change time */
646     p += 8;
647     SIVAL(p,0,fmode); /* File Attributes. */
648     p += 12;
649 #if OFF_T_IS_64_BITS
650       SIVAL(p,0, file_len & 0xFFFFFFFF);
651       SIVAL(p,4, file_len >> 32);
652 #else /* OFF_T_IS_64_BITS */
653       SIVAL(p,0,file_len);
654 #endif /* OFF_T_IS_64_BITS */
655     p += 12;
656     SCVAL(p,0,fsp->is_directory ? 1 : 0);
657   }
658
659   chain_fnum = fnum;
660
661   DEBUG(5,("reply_ntcreate_and_X: open fnum = %d, name = %s\n",
662         fnum, fsp->name ));
663
664   return chain_reply(inbuf,outbuf,length,bufsize);
665 }
666
667 /****************************************************************************
668  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
669 ****************************************************************************/
670
671 static int call_nt_transact_create(char *inbuf, char *outbuf, int length, 
672                                    int bufsize, int cnum,
673                                    char **ppsetup, char **ppparams, char **ppdata)
674 {
675   pstring fname;
676   int fnum = -1;
677   char *params = *ppparams;
678   uint32 flags = IVAL(params,0);
679   uint32 desired_access = IVAL(params,8);
680   uint32 file_attributes = IVAL(params,20);
681   uint32 share_access = IVAL(params,24);
682   uint32 create_disposition = IVAL(params,28);
683   uint32 fname_len = MIN(((uint32)IVAL(params,44)),
684                          ((uint32)sizeof(fname)-1));
685   int smb_ofun;
686   int smb_open_mode;
687   int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
688   /* Breakout the oplock request bits so we can set the
689      reply bits separately. */
690   int oplock_request = 0;
691   int unixmode;
692   int fmode=0,mtime=0,rmode=0;
693   off_t file_len = 0;
694   struct stat sbuf;
695   int smb_action = 0;
696   BOOL bad_path = False;
697   files_struct *fsp;
698   char *p = NULL;
699
700   /* 
701    * We need to construct the open_and_X ofun value from the
702    * NT values, as that's what our code is structured to accept.
703    */    
704
705   if((smb_ofun = map_create_disposition( create_disposition )) == -1)
706     return(ERROR(ERRDOS,ERRbadaccess));
707
708   /*
709    * Now contruct the smb_open_mode value from the desired access
710    * and the share access.
711    */
712
713   if((smb_open_mode = map_share_mode( desired_access, share_access, file_attributes)) == -1)
714     return(ERROR(ERRDOS,ERRbadaccess));
715
716   /*
717    * Get the file name.
718    */
719
720   StrnCpy(fname,params+53,fname_len);
721   fname[fname_len] = '\0';
722
723   /* If it's an IPC, use the pipe handler. */
724   if (IS_IPC(cnum)) {
725     int ret = nt_open_pipe(fname, inbuf, outbuf, &fnum);
726     if(ret != 0)
727       return ret;
728     smb_action = FILE_WAS_OPENED;
729   } else {
730     /*
731      * Check if POSIX semantics are wanted.
732      */
733
734     set_posix_case_semantics(file_attributes);
735
736     unix_convert(fname,cnum,0,&bad_path);
737     
738     fnum = find_free_file();
739     if (fnum < 0) {
740       restore_case_semantics(file_attributes);
741       return(ERROR(ERRSRV,ERRnofids));
742     }
743
744     fsp = &Files[fnum];
745
746     if (!check_name(fname,cnum)) { 
747       if((errno == ENOENT) && bad_path) {
748         unix_ERR_class = ERRDOS;
749         unix_ERR_code = ERRbadpath;
750       }
751       fsp->reserved = False;
752
753       restore_case_semantics(file_attributes);
754
755       return(UNIXERROR(ERRDOS,ERRnoaccess));
756     } 
757   
758     unixmode = unix_mode(cnum,smb_attr | aARCH);
759     
760     oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
761     oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
762
763     /*
764      * If it's a request for a directory open, deal with it separately.
765      */
766
767     if(flags & OPEN_DIRECTORY) {
768
769       oplock_request = 0;
770
771       /*
772        * We will get a create directory here if the Win32
773        * app specified a security descriptor in the 
774        * CreateDirectory() call.
775        */
776
777       open_directory(fnum, cnum, fname, smb_ofun, unixmode, &smb_action);
778
779       if(!fsp->open) {
780         fsp->reserved = False;
781         return(UNIXERROR(ERRDOS,ERRnoaccess));
782       }
783     } else {
784
785       /*
786        * Ordinary file case.
787        */
788
789       open_file_shared(fnum,cnum,fname,smb_open_mode,smb_ofun,unixmode,
790                        oplock_request,&rmode,&smb_action);
791
792       if (!fsp->open) { 
793         if((errno == ENOENT) && bad_path) {
794           unix_ERR_class = ERRDOS;
795           unix_ERR_code = ERRbadpath;
796         }
797         fsp->reserved = False;
798
799         restore_case_semantics(file_attributes);
800
801         return(UNIXERROR(ERRDOS,ERRnoaccess));
802       } 
803   
804       if (fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
805         close_file(fnum,False);
806
807         restore_case_semantics(file_attributes);
808
809         return(ERROR(ERRDOS,ERRnoaccess));
810       } 
811   
812       file_len = sbuf.st_size;
813       fmode = dos_mode(cnum,fname,&sbuf);
814       if(fmode == 0)
815         fmode = FILE_ATTRIBUTE_NORMAL;
816       mtime = sbuf.st_mtime;
817
818       if (fmode & aDIR) {
819         close_file(fnum,False);
820         restore_case_semantics(file_attributes);
821         return(ERROR(ERRDOS,ERRnoaccess));
822       } 
823
824       /* 
825        * If the caller set the extended oplock request bit
826        * and we granted one (by whatever means) - set the
827        * correct bit for extended oplock reply.
828        */
829     
830       if (oplock_request && lp_fake_oplocks(SNUM(cnum)))
831         smb_action |= EXTENDED_OPLOCK_GRANTED;
832   
833       if(oplock_request && fsp->granted_oplock)
834         smb_action |= EXTENDED_OPLOCK_GRANTED;
835     }
836   }
837
838   restore_case_semantics(file_attributes);
839
840   /* Realloc the size of parameters and data we will return */
841   params = *ppparams = Realloc(*ppparams, 69);
842   if(params == NULL)
843     return(ERROR(ERRDOS,ERRnomem));
844
845   p = params;
846   SCVAL(p,0, (smb_action & EXTENDED_OPLOCK_GRANTED ? 1 : 0));
847   p += 2;
848   SSVAL(p,0,fnum);
849   p += 2;
850   SIVAL(p,0,smb_action);
851   p += 8;
852
853   if (IS_IPC(cnum)) {
854     /*
855      * Deal with pipe return.
856      */  
857     p += 32;
858     SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
859     p += 20;
860     /* File type. */
861     SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
862     /* Device state. */
863     SSVAL(p,2, 0x5FF); /* ? */
864   } else {
865     /*
866      * Deal with file return.
867      */
868     /* Create time. */
869     put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(cnum))));
870     p += 8;
871     put_long_date(p,sbuf.st_atime); /* access time */
872     p += 8;
873     put_long_date(p,sbuf.st_mtime); /* write time */
874     p += 8;
875     put_long_date(p,sbuf.st_mtime); /* change time */
876     p += 8;
877     SIVAL(p,0,fmode); /* File Attributes. */
878     p += 12;
879 #if OFF_T_IS_64_BITS
880       SIVAL(p,0, file_len & 0xFFFFFFFF);
881       SIVAL(p,4, (file_len >> 32));
882 #else /* OFF_T_IS_64_BITS */
883       SIVAL(p,0,file_len);
884 #endif /* OFF_T_IS_64_BITS */
885   }
886
887   /* Send the required number of replies */
888   send_nt_replies(outbuf, bufsize, params, 69, *ppdata, 0);
889
890   return -1;
891 }
892
893 /****************************************************************************
894  Reply to a NT CANCEL request.
895 ****************************************************************************/
896
897 int reply_ntcancel(char *inbuf,char *outbuf,int length,int bufsize)
898 {
899   /*
900    * Go through and cancel any pending change notifies.
901    * TODO: When we add blocking locks we will add cancel
902    * for them here too.
903    */
904
905   int mid = SVAL(inbuf,smb_mid);
906   remove_pending_change_notify_requests_by_mid(mid);
907
908   DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
909
910   return(-1);
911 }
912
913 /****************************************************************************
914  Reply to an unsolicited SMBNTtranss - just ignore it!
915 ****************************************************************************/
916
917 int reply_nttranss(char *inbuf,char *outbuf,int length,int bufsize)
918 {
919   DEBUG(4,("Ignoring nttranss of length %d\n",length));
920   return(-1);
921 }
922
923 /****************************************************************************
924  Reply to an NT transact rename command.
925 ****************************************************************************/
926
927 static int call_nt_transact_rename(char *inbuf, char *outbuf, int length, 
928                                    int bufsize, int cnum,
929                                    char **ppsetup, char **ppparams, char **ppdata)
930 {
931   char *params = *ppparams;
932   pstring new_name;
933   int fnum = SVAL(params, 0);
934   BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
935   uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
936                          ((uint32)sizeof(new_name)-1));
937   int outsize = 0;
938
939   CHECK_FNUM(fnum, cnum);
940   StrnCpy(new_name,params+4,fname_len);
941   new_name[fname_len] = '\0';
942
943   outsize = rename_internals(inbuf, outbuf, Files[fnum].name,
944                              new_name, replace_if_exists);
945   if(outsize == 0) {
946     /*
947      * Rename was successful.
948      */
949     send_nt_replies(outbuf, bufsize, NULL, 0, NULL, 0);
950
951     DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n", 
952           Files[fnum].name, new_name));
953
954     outsize = -1;
955   }
956
957   return(outsize);
958 }
959    
960 /****************************************************************************
961  This is the structure to queue to implement NT change
962  notify. It consists of smb_size bytes stored from the
963  transact command (to keep the mid, tid etc around).
964  Plus the fid to examine and the time to check next.
965 *****************************************************************************/
966
967 typedef struct {
968   ubi_slNode msg_next;
969   int fnum;
970   int cnum;
971   time_t next_check_time;
972   time_t modify_time; /* Info from the directory we're monitoring. */ 
973   time_t status_time; /* Info from the directory we're monitoring. */
974   char request_buf[smb_size];
975 } change_notify_buf;
976
977 static ubi_slList change_notify_queue = { NULL, (ubi_slNodePtr)&change_notify_queue, 0};
978
979 /****************************************************************************
980  Setup the common parts of the return packet and send it.
981 *****************************************************************************/
982
983 static void change_notify_reply_packet(char *inbuf, int error_class, uint32 error_code)
984 {
985   extern int Client;
986   char outbuf[smb_size+38];
987
988   memset(outbuf, '\0', sizeof(outbuf));
989   construct_reply_common(inbuf, outbuf);
990
991   /*
992    * If we're returning a 'too much in the directory changed' we need to
993    * set this is an NT error status flags. If we don't then the (probably
994    * untested) code in the NT redirector has a bug in that it doesn't re-issue
995    * the change notify.... Ah - I *love* it when I get so deeply into this I
996    * can even determine how MS failed to test stuff and why.... :-). JRA.
997    */
998
999   if(error_class == 0) /* NT Error. */
1000     SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1001
1002   ERROR(error_class,error_code);
1003
1004   /*
1005    * Seems NT needs a transact command with an error code
1006    * in it. This is a longer packet than a simple error.
1007    */
1008   set_message(outbuf,18,0,False);
1009
1010   send_smb(Client,outbuf);
1011 }
1012
1013 /****************************************************************************
1014  Delete entries by fnum from the change notify pending queue.
1015 *****************************************************************************/
1016
1017 void remove_pending_change_notify_requests_by_fid(int fnum)
1018 {
1019   change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1020   change_notify_buf *prev = NULL;
1021
1022   while(cnbp != NULL) {
1023     if(cnbp->fnum == fnum) {
1024       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1025       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1026       continue;
1027     }
1028
1029     prev = cnbp;
1030     cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1031   }
1032 }
1033
1034 /****************************************************************************
1035  Delete entries by mid from the change notify pending queue. Always send reply.
1036 *****************************************************************************/
1037
1038 void remove_pending_change_notify_requests_by_mid(int mid)
1039 {
1040   change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1041   change_notify_buf *prev = NULL;
1042
1043   while(cnbp != NULL) {
1044     if(SVAL(cnbp->request_buf,smb_mid) == mid) {
1045       change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_CANCELLED);
1046       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1047       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1048       continue;
1049     }
1050
1051     prev = cnbp;
1052     cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1053   }
1054 }
1055
1056 /****************************************************************************
1057  Process the change notify queue. Note that this is only called as root.
1058 *****************************************************************************/
1059
1060 void process_pending_change_notify_queue(time_t t)
1061 {
1062   change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1063   change_notify_buf *prev = NULL;
1064
1065   if(cnbp == NULL)
1066     return;
1067
1068   if(cnbp->next_check_time >= t)
1069     return;
1070
1071   /*
1072    * It's time to check. Go through the queue and see if
1073    * the timestamps changed.
1074    */
1075
1076   while((cnbp != NULL) && (cnbp->next_check_time <= t)) {
1077     struct stat st;
1078     int fnum = cnbp->fnum;
1079     int cnum = cnbp->cnum;
1080     files_struct *fsp = &Files[fnum];
1081     uint16 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID : 
1082                   SVAL(cnbp->request_buf,smb_uid);
1083
1084     if(!become_user(&Connections[cnum],cnum,vuid)) {
1085       DEBUG(0,("process_pending_change_notify_queue: Unable to become user vuid=%d.\n",
1086             vuid ));
1087       /*
1088        * Remove the entry and return an error to the client.
1089        */
1090       change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1091       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1092       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1093       continue;
1094     }
1095
1096     if(!become_service(cnum,True)) {
1097       DEBUG(0,("process_pending_change_notify_queue: Unable to become service cnum=%d. \
1098 Error was %s.\n", cnum, strerror(errno) ));
1099       /*
1100        * Remove the entry and return an error to the client.
1101        */
1102       change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1103       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1104       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1105       unbecome_user();
1106       continue;
1107     }
1108
1109     if(sys_stat(fsp->name, &st) < 0) {
1110       DEBUG(0,("process_pending_change_notify_queue: Unable to stat directory %s. \
1111 Error was %s.\n", fsp->name, strerror(errno) ));
1112       /*
1113        * Remove the entry and return an error to the client.
1114        */
1115       change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1116       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1117       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1118       unbecome_user();
1119       continue;
1120     }
1121
1122     if(cnbp->modify_time != st.st_mtime ||
1123        cnbp->status_time != st.st_ctime) {
1124       /*
1125        * Remove the entry and return a change notify to the client.
1126        */
1127       DEBUG(5,("process_pending_change_notify_queue: directory fnum = %d, name = %s changed\n",
1128             fnum, fsp->name ));
1129       change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_NOTIFY_ENUM_DIR);
1130       free((char *)ubi_slRemNext( &change_notify_queue, prev));
1131       cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1132       unbecome_user();
1133       continue;
1134     }
1135
1136     unbecome_user();
1137
1138     /*
1139      * Move to the next in the list.
1140      */
1141     prev = cnbp;
1142     cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1143   }
1144 }
1145
1146 /****************************************************************************
1147  Reply to a notify change - queue the request and 
1148  don't allow a directory to be opened.
1149 ****************************************************************************/
1150
1151 static int call_nt_transact_notify_change(char *inbuf, char *outbuf, int length,
1152                                           int bufsize, int cnum,
1153                                           char **ppsetup, char **ppparams, char **ppdata)
1154 {
1155   char *setup = *ppsetup;
1156   files_struct *fsp;
1157   int fnum = -1;
1158   change_notify_buf *cnbp;
1159   struct stat st;
1160
1161   fnum = SVAL(setup,4);
1162
1163   DEBUG(3,("call_nt_transact_notify_change: fnum = %d.\n", fnum));
1164
1165   if(!VALID_FNUM(fnum))
1166     return(ERROR(ERRDOS,ERRbadfid));
1167
1168   fsp = &Files[fnum];
1169
1170   if((!fsp->open) || (!fsp->is_directory) || (cnum != fsp->cnum))
1171     return(ERROR(ERRDOS,ERRbadfid));
1172
1173   /*
1174    * Now queue an entry on the notify change stack. We timestamp
1175    * the entry we are adding so that we know when to scan next.
1176    * We only need to save smb_size bytes from this incoming packet
1177    * as we will always by returning a 'read the directory yourself'
1178    * error.
1179    */
1180
1181   if((cnbp = (change_notify_buf *)malloc(sizeof(change_notify_buf))) == NULL) {
1182     DEBUG(0,("call_nt_transact_notify_change: Malloc fail (2) !\n" ));
1183     return -1;
1184   }
1185
1186   /* 
1187    * Store the current timestamp on the directory we are monitoring.
1188    */
1189
1190   if(sys_stat(fsp->name, &st) < 0) {
1191     DEBUG(0,("call_nt_transact_notify_change: Unable to stat fnum = %d, name = %s. \
1192 Error was %s\n", fnum, fsp->name, strerror(errno) ));
1193     free((char *)cnbp);
1194     return(UNIXERROR(ERRDOS,ERRbadfid));
1195   }
1196  
1197   memcpy(cnbp->request_buf, inbuf, smb_size);
1198   cnbp->fnum = fnum;
1199   cnbp->cnum = cnum;
1200   cnbp->modify_time = st.st_mtime;
1201   cnbp->status_time = st.st_ctime;
1202
1203   cnbp->next_check_time = time(NULL) + lp_change_notify_timeout();
1204
1205   /*
1206    * Adding to the tail enables us to check only
1207    * the head when scanning for change, as this entry
1208    * is forced to have the first timeout expiration.
1209    */
1210
1211   ubi_slAddTail(&change_notify_queue, cnbp);
1212
1213   DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1214 fid=%d, name = %s\n", fnum, fsp->name ));
1215
1216   return -1;
1217 }
1218
1219 /****************************************************************************
1220  Reply to query a security descriptor - currently this is not implemented (it
1221  is planned to be though).
1222 ****************************************************************************/
1223
1224 static int call_nt_transact_query_security_desc(char *inbuf, char *outbuf, int length, 
1225                                                 int bufsize, int cnum,
1226                                                 char **ppsetup, char **ppparams, char **ppdata)
1227 {
1228   DEBUG(0,("call_nt_transact_query_security_desc: Currently not implemented.\n"));
1229   return(ERROR(ERRSRV,ERRnosupport));
1230 }
1231    
1232 /****************************************************************************
1233  Reply to set a security descriptor - currently this is not implemented (it
1234  is planned to be though).
1235 ****************************************************************************/
1236
1237 static int call_nt_transact_set_security_desc(char *inbuf, char *outbuf, int length,
1238                                               int bufsize, int cnum,
1239                                               char **ppsetup, char **ppparams, char **ppdata)
1240 {
1241   DEBUG(0,("call_nt_transact_set_security_desc: Currently not implemented.\n"));
1242   return(ERROR(ERRSRV,ERRnosupport));
1243 }
1244    
1245 /****************************************************************************
1246  Reply to IOCTL - not implemented - no plans.
1247 ****************************************************************************/
1248
1249 static int call_nt_transact_ioctl(char *inbuf, char *outbuf, int length,
1250                                   int bufsize, int cnum,
1251                                   char **ppsetup, char **ppparams, char **ppdata)
1252 {
1253   DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1254   return(ERROR(ERRSRV,ERRnosupport));
1255 }
1256    
1257 /****************************************************************************
1258  Reply to a SMBNTtrans.
1259 ****************************************************************************/
1260
1261 int reply_nttrans(char *inbuf,char *outbuf,int length,int bufsize)
1262 {
1263   int  outsize = 0;
1264   int cnum = SVAL(inbuf,smb_tid);
1265 #if 0 /* Not used. */
1266   uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1267   uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1268   uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1269 #endif /* Not used. */
1270   uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1271   uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1272   uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1273   uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1274   uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1275   uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1276   uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1277   uint16 function_code = SVAL( inbuf, smb_nt_Function);
1278   char *params = NULL, *data = NULL, *setup = NULL;
1279   uint32 num_params_sofar, num_data_sofar;
1280
1281   if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1282     /*
1283      * Queue this open message as we are the process of an oplock break.
1284      */
1285
1286     DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1287 due to being in oplock break state.\n" ));
1288
1289     push_oplock_pending_smb_message( inbuf, length);
1290     return -1;
1291   }
1292
1293   outsize = set_message(outbuf,0,0,True);
1294
1295   /* 
1296    * All nttrans messages we handle have smb_wct == 19 + setup_count.
1297    * Ensure this is so as a sanity check.
1298    */
1299
1300   if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1301     DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1302           CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1303     return(ERROR(ERRSRV,ERRerror));
1304   }
1305     
1306   /* Allocate the space for the setup, the maximum needed parameters and data */
1307
1308   if(setup_count > 0)
1309     setup = (char *)malloc(setup_count);
1310   if (total_parameter_count > 0)
1311     params = (char *)malloc(total_parameter_count);
1312   if (total_data_count > 0)
1313     data = (char *)malloc(total_data_count);
1314  
1315   if ((total_parameter_count && !params)  || (total_data_count && !data) ||
1316       (setup_count && !setup)) {
1317     DEBUG(0,("reply_nttrans : Out of memory\n"));
1318     return(ERROR(ERRDOS,ERRnomem));
1319   }
1320
1321   /* Copy the param and data bytes sent with this request into
1322      the params buffer */
1323   num_params_sofar = parameter_count;
1324   num_data_sofar = data_count;
1325
1326   if (parameter_count > total_parameter_count || data_count > total_data_count)
1327     exit_server("reply_nttrans: invalid sizes in packet.\n");
1328
1329   if(setup) {
1330     memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1331     DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1332     dump_data(10, setup, setup_count);
1333   }
1334   if(params) {
1335     memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1336     DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1337     dump_data(10, params, parameter_count);
1338   }
1339   if(data) {
1340     memcpy( data, smb_base(inbuf) + data_offset, data_count);
1341     DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1342     dump_data(10, data, data_count);
1343   }
1344
1345   if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1346     /* We need to send an interim response then receive the rest
1347        of the parameter/data bytes */
1348     outsize = set_message(outbuf,0,0,True);
1349     send_smb(Client,outbuf);
1350
1351     while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1352       BOOL ret;
1353
1354       ret = receive_next_smb(Client,oplock_sock,inbuf,bufsize,
1355                              SMB_SECONDARY_WAIT);
1356
1357       if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1358         outsize = set_message(outbuf,0,0,True);
1359         if(ret) {
1360                 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1361         } else {
1362                 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1363                          (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1364         }
1365         if(params)
1366           free(params);
1367         if(data)
1368           free(data);
1369         if(setup)
1370           free(setup);
1371         return(ERROR(ERRSRV,ERRerror));
1372       }
1373       
1374       /* Revise total_params and total_data in case they have changed downwards */
1375       total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1376       total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1377       num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1378       num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1379       if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1380         exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
1381
1382       memcpy( &params[ IVAL(inbuf, smb_nts_ParameterDisplacement)], 
1383               smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1384       memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1385               smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1386     }
1387   }
1388
1389   if (Protocol >= PROTOCOL_NT1) {
1390     uint16 flg2 = SVAL(outbuf,smb_flg2);
1391     SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
1392   }
1393
1394   /* Now we must call the relevant NT_TRANS function */
1395   switch(function_code) {
1396     case NT_TRANSACT_CREATE:
1397       outsize = call_nt_transact_create(inbuf, outbuf, length, bufsize, cnum, 
1398                                         &setup, &params, &data);
1399       break;
1400     case NT_TRANSACT_IOCTL:
1401       outsize = call_nt_transact_ioctl(inbuf, outbuf, length, bufsize, cnum,
1402                                        &setup, &params, &data);
1403       break;
1404     case NT_TRANSACT_SET_SECURITY_DESC:
1405       outsize = call_nt_transact_set_security_desc(inbuf, outbuf, length, bufsize, cnum,
1406                                                    &setup, &params, &data);
1407       break;
1408     case NT_TRANSACT_NOTIFY_CHANGE:
1409       outsize = call_nt_transact_notify_change(inbuf, outbuf, length, bufsize, cnum,
1410                                                &setup, &params, &data);
1411       break;
1412     case NT_TRANSACT_RENAME:
1413       outsize = call_nt_transact_rename(inbuf, outbuf, length, bufsize, cnum,
1414                                         &setup, &params, &data);
1415       break;
1416     case NT_TRANSACT_QUERY_SECURITY_DESC:
1417       outsize = call_nt_transact_query_security_desc(inbuf, outbuf, length, bufsize, cnum,
1418                                                      &setup, &params, &data);
1419       break;
1420     default:
1421       /* Error in request */
1422       DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1423       if(setup)
1424         free(setup);
1425       if(params)
1426         free(params);
1427       if(data)
1428         free(data);
1429       return (ERROR(ERRSRV,ERRerror));
1430   }
1431
1432   /* As we do not know how many data packets will need to be
1433      returned here the various call_nt_transact_xxxx calls
1434      must send their own. Thus a call_nt_transact_xxxx routine only
1435      returns a value other than -1 when it wants to send
1436      an error packet. 
1437   */
1438
1439   if(setup)
1440     free(setup);
1441   if(params)
1442     free(params);
1443   if(data)
1444     free(data);
1445   return outsize; /* If a correct response was needed the call_nt_transact_xxxx 
1446                      calls have already sent it. If outsize != -1 then it is
1447                      returning an error packet. */
1448 }