Merge of new 2.2 code into HEAD (Gerald I hate you :-) :-). Allows new SAMR
[bbaumbach/samba-autobuild/.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
24 extern int DEBUGLEVEL;
25 extern int Protocol;
26 extern int smb_read_error;
27 extern int global_oplock_break;
28 extern BOOL case_sensitive;
29 extern BOOL case_preserve;
30 extern BOOL short_case_preserve;
31
32 static char *known_nt_pipes[] = {
33   "\\LANMAN",
34   "\\srvsvc",
35   "\\samr",
36   "\\wkssvc",
37   "\\NETLOGON",
38   "\\ntlsa",
39   "\\ntsvcs",
40   "\\lsass",
41   "\\lsarpc",
42   "\\winreg",
43   "\\spoolss",
44 #ifdef WITH_MSDFS
45   "\\netdfs",
46 #endif
47   NULL
48 };
49
50 /****************************************************************************
51  Send the required number of replies back.
52  We assume all fields other than the data fields are
53  set correctly for the type of call.
54  HACK ! Always assumes smb_setup field is zero.
55 ****************************************************************************/
56
57 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, uint32 nt_error, char *params,
58                            int paramsize, char *pdata, int datasize)
59 {
60   extern int max_send;
61   int data_to_send = datasize;
62   int params_to_send = paramsize;
63   int useable_space;
64   char *pp = params;
65   char *pd = pdata;
66   int params_sent_thistime, data_sent_thistime, total_sent_thistime;
67   int alignment_offset = 3;
68   int data_alignment_offset = 0;
69
70   /*
71    * Initially set the wcnt area to be 18 - this is true for all
72    * transNT replies.
73    */
74
75   set_message(outbuf,18,0,True);
76
77   if(nt_error != 0) {
78     /* NT Error. */
79     SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
80
81     ERROR(0,nt_error);
82   }
83
84   /* 
85    * If there genuinely are no parameters or data to send just send
86    * the empty packet.
87    */
88
89   if(params_to_send == 0 && data_to_send == 0) {
90     send_smb(smbd_server_fd(),outbuf);
91     return 0;
92   }
93
94   /*
95    * When sending params and data ensure that both are nicely aligned.
96    * Only do this alignment when there is also data to send - else
97    * can cause NT redirector problems.
98    */
99
100   if (((params_to_send % 4) != 0) && (data_to_send != 0))
101     data_alignment_offset = 4 - (params_to_send % 4);
102
103   /* 
104    * Space is bufsize minus Netbios over TCP header minus SMB header.
105    * The alignment_offset is to align the param bytes on a four byte
106    * boundary (2 bytes for data len, one byte pad). 
107    * NT needs this to work correctly.
108    */
109
110   useable_space = bufsize - ((smb_buf(outbuf)+
111                     alignment_offset+data_alignment_offset) -
112                     outbuf);
113
114   /*
115    * useable_space can never be more than max_send minus the
116    * alignment offset.
117    */
118
119   useable_space = MIN(useable_space,
120                       max_send - (alignment_offset+data_alignment_offset));
121
122
123   while (params_to_send || data_to_send) {
124
125     /*
126      * Calculate whether we will totally or partially fill this packet.
127      */
128
129     total_sent_thistime = params_to_send + data_to_send +
130                             alignment_offset + data_alignment_offset;
131
132     /* 
133      * We can never send more than useable_space.
134      */
135
136     total_sent_thistime = MIN(total_sent_thistime, useable_space);
137
138     set_message(outbuf, 18, total_sent_thistime, True);
139
140     /*
141      * Set total params and data to be sent.
142      */
143
144     SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
145     SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
146
147     /* 
148      * Calculate how many parameters and data we can fit into
149      * this packet. Parameters get precedence.
150      */
151
152     params_sent_thistime = MIN(params_to_send,useable_space);
153     data_sent_thistime = useable_space - params_sent_thistime;
154     data_sent_thistime = MIN(data_sent_thistime,data_to_send);
155
156     SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
157
158     if(params_sent_thistime == 0) {
159       SIVAL(outbuf,smb_ntr_ParameterOffset,0);
160       SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
161     } else {
162       /*
163        * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
164        * parameter bytes, however the first 4 bytes of outbuf are
165        * the Netbios over TCP header. Thus use smb_base() to subtract
166        * them from the calculation.
167        */
168
169       SIVAL(outbuf,smb_ntr_ParameterOffset,
170             ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
171       /* 
172        * Absolute displacement of param bytes sent in this packet.
173        */
174
175       SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
176     }
177
178     /*
179      * Deal with the data portion.
180      */
181
182     SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
183
184     if(data_sent_thistime == 0) {
185       SIVAL(outbuf,smb_ntr_DataOffset,0);
186       SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
187     } else {
188       /*
189        * The offset of the data bytes is the offset of the
190        * parameter bytes plus the number of parameters being sent this time.
191        */
192
193       SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
194             smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
195       SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
196     }
197
198     /* 
199      * Copy the param bytes into the packet.
200      */
201
202     if(params_sent_thistime)
203       memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
204
205     /*
206      * Copy in the data bytes
207      */
208
209     if(data_sent_thistime)
210       memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
211              data_alignment_offset,pd,data_sent_thistime);
212     
213     DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
214           params_sent_thistime, data_sent_thistime, useable_space));
215     DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
216           params_to_send, data_to_send, paramsize, datasize));
217     
218     /* Send the packet */
219     send_smb(smbd_server_fd(),outbuf);
220     
221     pp += params_sent_thistime;
222     pd += data_sent_thistime;
223     
224     params_to_send -= params_sent_thistime;
225     data_to_send -= data_sent_thistime;
226
227     /*
228      * Sanity check
229      */
230
231     if(params_to_send < 0 || data_to_send < 0) {
232       DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
233             params_to_send, data_to_send));
234       return -1;
235     }
236   } 
237
238   return 0;
239 }
240
241 /****************************************************************************
242  (Hopefully) temporary call to fix bugs in NT5.0beta2. This OS sends unicode
243  strings in NT calls AND DOESN'T SET THE UNICODE BIT !!!!!!!
244 ****************************************************************************/
245
246 static void get_filename( char *fname, char *inbuf, int data_offset, int data_len, int fname_len)
247 {
248   /*
249    * We need various heuristics here to detect a unicode string... JRA.
250    */
251
252   DEBUG(10,("get_filename: data_offset = %d, data_len = %d, fname_len = %d\n",
253            data_offset, data_len, fname_len ));
254
255   if(data_len - fname_len > 1) {
256     /*
257      * NT 5.0 Beta 2 has kindly sent us a UNICODE string
258      * without bothering to set the unicode bit. How kind.
259      *
260      * Firstly - ensure that the data offset is aligned
261      * on a 2 byte boundary - add one if not.
262      */
263     fname_len = fname_len/2;
264     if(data_offset & 1)
265       data_offset++;
266     pstrcpy(fname, dos_unistrn2((uint16 *)(inbuf+data_offset), fname_len));
267   } else {
268     StrnCpy(fname,inbuf+data_offset,fname_len);
269     fname[fname_len] = '\0';
270   }
271 }
272
273 /****************************************************************************
274  Fix bugs in Win2000 final release. In trans calls this OS sends unicode
275  strings AND DOESN'T SET THE UNICODE BIT !!!!!!!
276 ****************************************************************************/
277
278 static void get_filename_transact( char *fname, char *inbuf, int data_offset, int data_len, int fname_len)
279 {
280   /*
281    * We need various heuristics here to detect a unicode string... JRA.
282    */
283
284   DEBUG(10,("get_filename_transact: data_offset = %d, data_len = %d, fname_len = %d\n",
285            data_offset, data_len, fname_len ));
286
287   /*
288    * Win2K sends a unicode filename plus one extra alingment byte.
289    * WinNT4.x send an ascii string with multiple garbage bytes on
290    * the end here.
291    */
292
293   if((data_len == 1) || (inbuf[data_offset] == '\0')) {
294     /*
295      * Ensure that the data offset is aligned
296      * on a 2 byte boundary - add one if not.
297      */
298     fname_len = fname_len/2;
299     if(data_offset & 1)
300       data_offset++;
301     pstrcpy(fname, dos_unistrn2((uint16 *)(inbuf+data_offset), fname_len));
302   } else {
303     StrnCpy(fname,inbuf+data_offset,fname_len);
304     fname[fname_len] = '\0';
305   }
306 }
307
308 /****************************************************************************
309  Save case statics.
310 ****************************************************************************/
311
312 static BOOL saved_case_sensitive;
313 static BOOL saved_case_preserve;
314 static BOOL saved_short_case_preserve;
315
316 /****************************************************************************
317  Save case semantics.
318 ****************************************************************************/
319
320 static void set_posix_case_semantics(uint32 file_attributes)
321 {
322   if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
323     return;
324
325   saved_case_sensitive = case_sensitive;
326   saved_case_preserve = case_preserve;
327   saved_short_case_preserve = short_case_preserve;
328
329   /* Set to POSIX. */
330   case_sensitive = True;
331   case_preserve = True;
332   short_case_preserve = True;
333 }
334
335 /****************************************************************************
336  Restore case semantics.
337 ****************************************************************************/
338
339 static void restore_case_semantics(uint32 file_attributes)
340 {
341   if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
342     return;
343
344   case_sensitive = saved_case_sensitive;
345   case_preserve = saved_case_preserve;
346   short_case_preserve = saved_short_case_preserve;
347 }
348
349 /****************************************************************************
350  Utility function to map create disposition.
351 ****************************************************************************/
352
353 static int map_create_disposition( uint32 create_disposition)
354 {
355   int ret;
356
357   switch( create_disposition ) {
358   case FILE_CREATE:
359     /* create if not exist, fail if exist */
360     ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
361     break;
362   case FILE_SUPERSEDE:
363   case FILE_OVERWRITE_IF:
364     /* create if not exist, trunc if exist */
365     ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
366     break;
367   case FILE_OPEN:
368     /* fail if not exist, open if exists */
369     ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
370     break;
371   case FILE_OPEN_IF:
372     /* create if not exist, open if exists */
373     ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
374     break;
375   case FILE_OVERWRITE:
376     /* fail if not exist, truncate if exists */
377     ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
378     break;
379   default:
380     DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
381              create_disposition ));
382     return -1;
383   }
384
385   DEBUG(10,("map_create_disposition: Mapped create_disposition %lx to %x\n",
386         (unsigned long)create_disposition, ret ));
387
388   return ret;
389 }
390
391 /****************************************************************************
392  Utility function to map share modes.
393 ****************************************************************************/
394
395 static int map_share_mode( BOOL *pstat_open_only, char *fname,
396                                                         uint32 desired_access, uint32 share_access, uint32 file_attributes)
397 {
398   int smb_open_mode = -1;
399
400   *pstat_open_only = False;
401
402   switch( desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
403   case FILE_READ_DATA:
404     smb_open_mode = DOS_OPEN_RDONLY;
405     break;
406   case FILE_WRITE_DATA:
407   case FILE_APPEND_DATA:
408   case FILE_WRITE_DATA|FILE_APPEND_DATA:
409     smb_open_mode = DOS_OPEN_WRONLY;
410     break;
411   case FILE_READ_DATA|FILE_WRITE_DATA:
412   case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
413   case FILE_READ_DATA|FILE_APPEND_DATA:
414     smb_open_mode = DOS_OPEN_RDWR;
415     break;
416   }
417
418   /*
419    * NB. For DELETE_ACCESS we should really check the
420    * directory permissions, as that is what controls
421    * delete, and for WRITE_DAC_ACCESS we should really
422    * check the ownership, as that is what controls the
423    * chmod. Note that this is *NOT* a security hole (this
424    * note is for you, Andrew) as we are not *allowing*
425    * the access at this point, the actual unlink or
426    * chown or chmod call would do this. We are just helping
427    * clients out by telling them if they have a hope
428    * of any of this succeeding. POSIX acls may still
429    * deny the real call. JRA.
430    */
431
432   if (smb_open_mode == -1) {
433
434         if(desired_access == WRITE_DAC_ACCESS || desired_access == READ_CONTROL_ACCESS)
435                 *pstat_open_only = True;
436
437     if(desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|
438                               FILE_EXECUTE|FILE_READ_ATTRIBUTES|
439                               FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
440                               FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
441       smb_open_mode = DOS_OPEN_RDONLY;
442         } else if(desired_access == 0) {
443
444                 /* 
445                  * JRA - NT seems to sometimes send desired_access as zero. play it safe
446                  * and map to a stat open.
447                  */
448
449                 *pstat_open_only = True;
450                 smb_open_mode = DOS_OPEN_RDONLY;
451
452         } else {
453       DEBUG(0,("map_share_mode: Incorrect value %lx for desired_access to file %s\n",
454              (unsigned long)desired_access, fname));
455       return -1;
456     }
457   }
458
459   /*
460    * Set the special bit that means allow share delete.
461    * This is held outside the normal share mode bits at 1<<15.
462    * JRA.
463    */
464
465   if(share_access & FILE_SHARE_DELETE)
466     smb_open_mode |= ALLOW_SHARE_DELETE;
467
468   /* Add in the requested share mode. */
469   switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
470   case FILE_SHARE_READ:
471     smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
472     break;
473   case FILE_SHARE_WRITE:
474     smb_open_mode |= SET_DENY_MODE(DENY_READ);
475     break;
476   case (FILE_SHARE_READ|FILE_SHARE_WRITE):
477     smb_open_mode |= SET_DENY_MODE(DENY_NONE);
478     break;
479   case FILE_SHARE_NONE:
480     smb_open_mode |= SET_DENY_MODE(DENY_ALL);
481     break;
482   }
483
484   /*
485    * Handle an O_SYNC request.
486    */
487
488   if(file_attributes & FILE_FLAG_WRITE_THROUGH)
489     smb_open_mode |= FILE_SYNC_OPENMODE;
490
491   DEBUG(10,("map_share_mode: Mapped desired access %lx, share access %lx, file attributes %lx \
492 to open_mode %x\n", (unsigned long)desired_access, (unsigned long)share_access,
493                     (unsigned long)file_attributes, smb_open_mode ));
494  
495   return smb_open_mode;
496 }
497
498 #if 0
499 /*
500  * This is a *disgusting* hack.
501  * This is *so* bad that even I'm embarrassed (and I
502  * have no shame). Here's the deal :
503  * Until we get the correct SPOOLSS code into smbd
504  * then when we're running with NT SMB support then
505  * NT makes this call with a level of zero, and then
506  * immediately follows it with an open request to
507  * the \\SRVSVC pipe. If we allow that open to
508  * succeed then NT barfs when it cannot open the
509  * \\SPOOLSS pipe immediately after and continually
510  * whines saying "Printer name is invalid" forever
511  * after. If we cause *JUST THIS NEXT OPEN* of \\SRVSVC
512  * to fail, then NT downgrades to using the downlevel code
513  * and everything works as well as before. I hate
514  * myself for adding this code.... JRA.
515  *
516  * The HACK_FAIL_TIME define allows only a 2
517  * second window for this to occur, just in
518  * case...
519  */
520
521 static BOOL fail_next_srvsvc = False;
522 static time_t fail_time;
523 #define HACK_FAIL_TIME 2 /* In seconds. */
524
525 void fail_next_srvsvc_open(void)
526 {
527   /* Check client is WinNT proper; Win2K doesn't like Jeremy's hack - matty */
528   if (get_remote_arch() != RA_WINNT)
529     return;
530
531   fail_next_srvsvc = True;
532   fail_time = time(NULL);
533   DEBUG(10,("fail_next_srvsvc_open: setting up timeout close of \\srvsvc pipe for print fix.\n"));
534 }
535
536 /*
537  * HACK alert.... see above - JRA.
538  */
539
540 BOOL should_fail_next_srvsvc_open(const char *pipename)
541 {
542
543   DEBUG(10,("should_fail_next_srvsvc_open: fail = %d, pipe = %s\n",
544     (int)fail_next_srvsvc, pipename));
545
546   if(fail_next_srvsvc && (time(NULL) > fail_time + HACK_FAIL_TIME)) {
547     fail_next_srvsvc = False;
548     fail_time = (time_t)0;
549     DEBUG(10,("should_fail_next_srvsvc_open: End of timeout close of \\srvsvc pipe for print fix.\n"));
550   }
551
552   if(fail_next_srvsvc && strequal(pipename, "srvsvc")) {
553     fail_next_srvsvc = False;
554     DEBUG(10,("should_fail_next_srvsvc_open: Deliberately failing open of \\srvsvc pipe for print fix.\n"));
555     return True;
556   }
557   return False;
558 }
559 #endif
560
561 /****************************************************************************
562  Reply to an NT create and X call on a pipe.
563 ****************************************************************************/
564 static int nt_open_pipe(char *fname, connection_struct *conn,
565                         char *inbuf, char *outbuf, int *ppnum)
566 {
567         pipes_struct *p = NULL;
568
569         uint16 vuid = SVAL(inbuf, smb_uid);
570         int i;
571
572         DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
573     
574         /* See if it is one we want to handle. */
575         for( i = 0; known_nt_pipes[i]; i++ )
576                 if( strequal(fname,known_nt_pipes[i]))
577                         break;
578     
579         if ( known_nt_pipes[i] == NULL )
580                 return(ERROR(ERRSRV,ERRaccess));
581     
582         /* Strip \\ off the name. */
583         fname++;
584     
585 #if 0
586         if(should_fail_next_srvsvc_open(fname))
587                 return (ERROR(ERRSRV,ERRaccess));
588 #endif
589
590         DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
591
592         p = open_rpc_pipe_p(fname, conn, vuid);
593         if (!p)
594                 return(ERROR(ERRSRV,ERRnofids));
595
596         *ppnum = p->pnum;
597
598         return 0;
599 }
600
601 /****************************************************************************
602  Reply to an NT create and X call for pipes.
603 ****************************************************************************/
604
605 static int do_ntcreate_pipe_open(connection_struct *conn,
606                          char *inbuf,char *outbuf,int length,int bufsize)
607 {
608         pstring fname;
609         int ret;
610         int pnum = -1;
611         char *p = NULL;
612         uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
613                                ((uint32)sizeof(fname)-1));
614
615         get_filename(fname, inbuf, smb_buf(inbuf)-inbuf, 
616                   smb_buflen(inbuf),fname_len);
617         if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
618                 return ret;
619
620         /*
621          * Deal with pipe return.
622          */  
623
624         set_message(outbuf,34,0,True);
625
626         p = outbuf + smb_vwv2;
627         p++;
628         SSVAL(p,0,pnum);
629         p += 2;
630         SIVAL(p,0,FILE_WAS_OPENED);
631         p += 4;
632         p += 32;
633         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
634         p += 20;
635         /* File type. */
636         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
637         /* Device state. */
638         SSVAL(p,2, 0x5FF); /* ? */
639
640         DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
641
642         return chain_reply(inbuf,outbuf,length,bufsize);
643 }
644
645 /****************************************************************************
646  Reply to an NT create and X call.
647 ****************************************************************************/
648
649 int reply_ntcreate_and_X(connection_struct *conn,
650                          char *inbuf,char *outbuf,int length,int bufsize)
651 {  
652         int result;
653         pstring fname;
654         uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
655         uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
656         uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
657         uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
658         uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
659         uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
660         uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
661                                ((uint32)sizeof(fname)-1));
662         uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
663         int smb_ofun;
664         int smb_open_mode;
665         int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
666         /* Breakout the oplock request bits so we can set the
667            reply bits separately. */
668         int oplock_request = 0;
669         mode_t unixmode;
670         int fmode=0,rmode=0;
671         SMB_OFF_T file_len = 0;
672         SMB_STRUCT_STAT sbuf;
673         int smb_action = 0;
674         BOOL bad_path = False;
675         files_struct *fsp=NULL;
676         char *p = NULL;
677         BOOL stat_open_only = False;
678         START_PROFILE(SMBntcreateX);
679
680         /* If it's an IPC, use the pipe handler. */
681
682         if (IS_IPC(conn)) {
683                 if (lp_nt_pipe_support()) {
684                         END_PROFILE(SMBntcreateX);
685                         return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
686                 } else {
687                         END_PROFILE(SMBntcreateX);
688                         return(ERROR(ERRDOS,ERRbadaccess));
689                 }
690         }
691                         
692
693         /* 
694          * We need to construct the open_and_X ofun value from the
695          * NT values, as that's what our code is structured to accept.
696          */    
697         
698         if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
699                 END_PROFILE(SMBntcreateX);
700                 return(ERROR(ERRDOS,ERRbadaccess));
701         }
702
703         /*
704          * Get the file name.
705          */
706
707     if(root_dir_fid != 0) {
708       /*
709        * This filename is relative to a directory fid.
710        */
711       files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
712       size_t dir_name_len;
713
714       if(!dir_fsp) {
715         END_PROFILE(SMBntcreateX);
716         return(ERROR(ERRDOS,ERRbadfid));
717       }
718
719       if(!dir_fsp->is_directory) {
720         /* 
721          * Check to see if this is a mac fork of some kind.
722          */
723
724         get_filename(&fname[0], inbuf, smb_buf(inbuf)-inbuf, 
725                    smb_buflen(inbuf),fname_len);
726
727         if( strchr(fname, ':')) {
728           SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
729           END_PROFILE(SMBntcreateX);
730           return(ERROR(0, NT_STATUS_OBJECT_PATH_NOT_FOUND));
731         }
732         END_PROFILE(SMBntcreateX);
733         return(ERROR(ERRDOS,ERRbadfid));
734       }
735
736       /*
737        * Copy in the base directory name.
738        */
739
740       pstrcpy( fname, dir_fsp->fsp_name );
741       dir_name_len = strlen(fname);
742
743       /*
744        * Ensure it ends in a '\'.
745        */
746
747       if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
748         pstrcat(fname, "\\");
749         dir_name_len++;
750       }
751
752       /*
753        * This next calculation can refuse a correct filename if we're dealing
754        * with the Win2k unicode bug, but that would be rare. JRA.
755        */
756
757       if(fname_len + dir_name_len >= sizeof(pstring)) {
758         END_PROFILE(SMBntcreateX);
759         return(ERROR(ERRSRV,ERRfilespecs));
760       }
761
762       get_filename(&fname[dir_name_len], inbuf, smb_buf(inbuf)-inbuf, 
763                    smb_buflen(inbuf),fname_len);
764
765     } else {
766       
767       get_filename(fname, inbuf, smb_buf(inbuf)-inbuf, 
768                    smb_buflen(inbuf),fname_len);
769     }
770         
771         /*
772          * Now contruct the smb_open_mode value from the filename, 
773      * desired access and the share access.
774          */
775         RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
776
777         if((smb_open_mode = map_share_mode(&stat_open_only, fname, desired_access, 
778                                            share_access, 
779                                            file_attributes)) == -1) {
780                 END_PROFILE(SMBntcreateX);
781                 return(ERROR(ERRDOS,ERRbadaccess));
782         }
783
784         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
785         oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
786
787         /*
788          * Ordinary file or directory.
789          */
790                 
791         /*
792          * Check if POSIX semantics are wanted.
793          */
794                 
795         set_posix_case_semantics(file_attributes);
796                 
797         unix_convert(fname,conn,0,&bad_path,&sbuf);
798                 
799         unixmode = unix_mode(conn,smb_attr | aARCH, fname);
800     
801         /* 
802          * If it's a request for a directory open, deal with it separately.
803          */
804
805         if(create_options & FILE_DIRECTORY_FILE) {
806                 oplock_request = 0;
807                 
808                 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
809                         
810                 restore_case_semantics(file_attributes);
811
812                 if(!fsp) {
813                         if((errno == ENOENT) && bad_path) {
814                                 unix_ERR_class = ERRDOS;
815                                 unix_ERR_code = ERRbadpath;
816                         }
817                         END_PROFILE(SMBntcreateX);
818                         return(UNIXERROR(ERRDOS,ERRnoaccess));
819                 }
820         } else {
821                 /*
822                  * Ordinary file case.
823                  */
824
825                 /* NB. We have a potential bug here. If we
826                  * cause an oplock break to ourselves, then we
827                  * could end up processing filename related
828                  * SMB requests whilst we await the oplock
829                  * break response. As we may have changed the
830                  * filename case semantics to be POSIX-like,
831                  * this could mean a filename request could
832                  * fail when it should succeed. This is a rare
833                  * condition, but eventually we must arrange
834                  * to restore the correct case semantics
835                  * before issuing an oplock break request to
836                  * our client. JRA.  */
837
838                 fsp = open_file_shared(conn,fname,&sbuf,smb_open_mode,
839                                  smb_ofun,unixmode, oplock_request,&rmode,&smb_action);
840
841                 if (!fsp) { 
842                         /* We cheat here. There are two cases we
843                          * care about. One is a directory rename,
844                          * where the NT client will attempt to
845                          * open the source directory for
846                          * DELETE access. Note that when the
847                          * NT client does this it does *not*
848                          * set the directory bit in the
849                          * request packet. This is translated
850                          * into a read/write open
851                          * request. POSIX states that any open
852                          * for write request on a directory
853                          * will generate an EISDIR error, so
854                          * we can catch this here and open a
855                          * pseudo handle that is flagged as a
856                          * directory. The second is an open
857                          * for a permissions read only, which
858                          * we handle in the open_file_stat case. JRA.
859                          */
860
861                         if(errno == EISDIR) {
862
863                                 /*
864                                  * Fail the open if it was explicitly a non-directory file.
865                                  */
866
867                                 if (create_options & FILE_NON_DIRECTORY_FILE) {
868                                         restore_case_semantics(file_attributes);
869                                         SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
870                                         END_PROFILE(SMBntcreateX);
871                                         return(ERROR(0, NT_STATUS_FILE_IS_A_DIRECTORY));
872                                 }
873         
874                                 oplock_request = 0;
875                                 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
876                                 
877                                 if(!fsp) {
878                                         restore_case_semantics(file_attributes);
879                                         if((errno == ENOENT) && bad_path) {
880                                                 unix_ERR_class = ERRDOS;
881                                                 unix_ERR_code = ERRbadpath;
882                                         }
883                                         END_PROFILE(SMBntcreateX);
884                                         return(UNIXERROR(ERRDOS,ERRnoaccess));
885                                 }
886 #ifdef EROFS
887                         } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
888 #else /* !EROFS */
889                         } else if (errno == EACCES && stat_open_only) {
890 #endif
891                                 /*
892                                  * We couldn't open normally and all we want
893                                  * are the permissions. Try and do a stat open.
894                                  */
895
896                                 oplock_request = 0;
897
898                                 fsp = open_file_stat(conn,fname,&sbuf,smb_open_mode,&smb_action);
899
900                                 if(!fsp) {
901                                         restore_case_semantics(file_attributes);
902                                         END_PROFILE(SMBntcreateX);
903                                         return(UNIXERROR(ERRDOS,ERRnoaccess));
904                                 }
905
906                         } else {
907
908                                 if((errno == ENOENT) && bad_path) {
909                                         unix_ERR_class = ERRDOS;
910                                         unix_ERR_code = ERRbadpath;
911                                 }
912                                 
913                                 restore_case_semantics(file_attributes);
914                                 
915                                 END_PROFILE(SMBntcreateX);
916                                 return(UNIXERROR(ERRDOS,ERRnoaccess));
917                         }
918                 } 
919         }
920                 
921         restore_case_semantics(file_attributes);
922                 
923         file_len = sbuf.st_size;
924         fmode = dos_mode(conn,fname,&sbuf);
925         if(fmode == 0)
926                 fmode = FILE_ATTRIBUTE_NORMAL;
927         if (!fsp->is_directory && (fmode & aDIR)) {
928                 close_file(fsp,False);
929                 END_PROFILE(SMBntcreateX);
930                 return(ERROR(ERRDOS,ERRnoaccess));
931         } 
932         
933         /* 
934          * If the caller set the extended oplock request bit
935          * and we granted one (by whatever means) - set the
936          * correct bit for extended oplock reply.
937          */
938         
939         if (oplock_request && lp_fake_oplocks(SNUM(conn)))
940                 smb_action |= EXTENDED_OPLOCK_GRANTED;
941         
942         if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
943                 smb_action |= EXTENDED_OPLOCK_GRANTED;
944
945         set_message(outbuf,34,0,True);
946         
947         p = outbuf + smb_vwv2;
948         
949         /*
950          * Currently as we don't support level II oplocks we just report
951          * exclusive & batch here.
952          */
953
954     if (smb_action & EXTENDED_OPLOCK_GRANTED)   
955                 SCVAL(p,0, BATCH_OPLOCK_RETURN);
956         else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
957         SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
958         else
959                 SCVAL(p,0,NO_OPLOCK_RETURN);
960         
961         p++;
962         SSVAL(p,0,fsp->fnum);
963         p += 2;
964         SIVAL(p,0,smb_action);
965         p += 4;
966         
967         /* Create time. */  
968         put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
969         p += 8;
970         put_long_date(p,sbuf.st_atime); /* access time */
971         p += 8;
972         put_long_date(p,sbuf.st_mtime); /* write time */
973         p += 8;
974         put_long_date(p,sbuf.st_mtime); /* change time */
975         p += 8;
976         SIVAL(p,0,fmode); /* File Attributes. */
977         p += 4;
978         SOFF_T(p, 0, file_len);
979         p += 8;
980         SOFF_T(p,0,file_len);
981         p += 12;
982         SCVAL(p,0,fsp->is_directory ? 1 : 0);
983         
984         DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
985
986         result = chain_reply(inbuf,outbuf,length,bufsize);
987         END_PROFILE(SMBntcreateX);
988         return result;
989 }
990
991 /****************************************************************************
992  Reply to a NT_TRANSACT_CREATE call to open a pipe.
993 ****************************************************************************/
994
995 static int do_nt_transact_create_pipe( connection_struct *conn,
996                                         char *inbuf, char *outbuf, int length, 
997                                         int bufsize, char **ppsetup, char **ppparams, 
998                                         char **ppdata)
999 {
1000         pstring fname;
1001         uint32 fname_len;
1002         int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1003         char *params = *ppparams;
1004         int ret;
1005         int pnum = -1;
1006         char *p = NULL;
1007
1008         /*
1009          * Ensure minimum number of parameters sent.
1010          */
1011
1012         if(total_parameter_count < 54) {
1013                 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1014                 return(ERROR(ERRDOS,ERRbadaccess));
1015         }
1016
1017         fname_len = MIN(((uint32)IVAL(params,44)),((uint32)sizeof(fname)-1));
1018
1019         get_filename_transact(&fname[0], params, 53,
1020                         total_parameter_count - 53 - fname_len, fname_len);
1021
1022     if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
1023       return ret;
1024
1025         /* Realloc the size of parameters and data we will return */
1026         params = Realloc(*ppparams, 69);
1027         if(params == NULL)
1028                 return(ERROR(ERRDOS,ERRnomem));
1029
1030         *ppparams = params;
1031
1032         memset((char *)params,'\0',69);
1033
1034         p = params;
1035         SCVAL(p,0,NO_OPLOCK_RETURN);
1036
1037         p += 2;
1038         SSVAL(p,0,pnum);
1039         p += 2;
1040         SIVAL(p,0,FILE_WAS_OPENED);
1041         p += 8;
1042
1043         p += 32;
1044         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1045         p += 20;
1046         /* File type. */
1047         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1048         /* Device state. */
1049         SSVAL(p,2, 0x5FF); /* ? */
1050
1051         DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1052
1053         /* Send the required number of replies */
1054         send_nt_replies(inbuf, outbuf, bufsize, 0, params, 69, *ppdata, 0);
1055
1056         return -1;
1057 }
1058
1059 /****************************************************************************
1060  Internal fn to set security descriptors.
1061 ****************************************************************************/
1062
1063 static BOOL set_sd(files_struct *fsp, char *data, uint32 sd_len, uint security_info_sent, int *pdef_class,uint32 *pdef_code)
1064 {
1065         prs_struct pd;
1066         SEC_DESC *psd = NULL;
1067         TALLOC_CTX *mem_ctx;
1068         BOOL ret;
1069
1070         if (sd_len == 0) {
1071                 *pdef_class = ERRDOS;
1072                 *pdef_code = ERRbadaccess;
1073                 return False;
1074         }
1075
1076         /*
1077          * Init the parse struct we will unmarshall from.
1078          */
1079
1080         if ((mem_ctx = talloc_init()) == NULL) {
1081                 DEBUG(0,("set_sd: talloc_init failed.\n"));
1082                 *pdef_class = ERRDOS;
1083                 *pdef_code = ERRnomem;
1084                 return False;
1085         }
1086
1087         prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1088
1089         /*
1090          * Setup the prs_struct to point at the memory we just
1091          * allocated.
1092          */
1093         
1094         prs_give_memory( &pd, data, sd_len, False);
1095
1096         /*
1097          * Finally, unmarshall from the data buffer.
1098          */
1099
1100         if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1101                 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1102                 /*
1103                  * Return access denied for want of a better error message..
1104                  */ 
1105                 talloc_destroy(mem_ctx);
1106                 *pdef_class = ERRDOS;
1107                 *pdef_code = ERRnomem;
1108                 return False;
1109         }
1110
1111         if (psd->off_owner_sid==0)
1112                 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1113         if (psd->off_grp_sid==0)
1114                 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1115         if (psd->off_sacl==0)
1116                 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1117         if (psd->off_dacl==0)
1118                 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1119         
1120         ret = set_nt_acl( fsp, security_info_sent, psd);
1121
1122         if (!ret) {
1123                 talloc_destroy(mem_ctx);
1124                 *pdef_class = ERRDOS;
1125                 *pdef_code = ERRnoaccess;
1126                 return False;
1127         }
1128
1129         talloc_destroy(mem_ctx);
1130
1131         *pdef_class = 0;
1132         *pdef_code = 0;
1133         return True;
1134 }
1135
1136 /****************************************************************************
1137  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1138 ****************************************************************************/
1139
1140 static int call_nt_transact_create(connection_struct *conn,
1141                                         char *inbuf, char *outbuf, int length, 
1142                                         int bufsize, char **ppsetup, char **ppparams, 
1143                                         char **ppdata)
1144 {
1145   pstring fname;
1146   char *params = *ppparams;
1147   char *data = *ppdata;
1148   int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1149   /* Breakout the oplock request bits so we can set the
1150      reply bits separately. */
1151   int oplock_request = 0;
1152   mode_t unixmode;
1153   int fmode=0,rmode=0;
1154   SMB_OFF_T file_len = 0;
1155   SMB_STRUCT_STAT sbuf;
1156   int smb_action = 0;
1157   BOOL bad_path = False;
1158   files_struct *fsp = NULL;
1159   char *p = NULL;
1160   BOOL stat_open_only = False;
1161   uint32 flags;
1162   uint32 desired_access;
1163   uint32 file_attributes;
1164   uint32 share_access;
1165   uint32 create_disposition;
1166   uint32 create_options;
1167   uint32 fname_len;
1168   uint32 sd_len;
1169   uint16 root_dir_fid;
1170   int smb_ofun;
1171   int smb_open_mode;
1172   int smb_attr;
1173   int error_class;
1174   uint32 error_code;
1175
1176   DEBUG(5,("call_nt_transact_create\n"));
1177
1178   /*
1179    * If it's an IPC, use the pipe handler.
1180    */
1181
1182   if (IS_IPC(conn)) {
1183                 if (lp_nt_pipe_support())
1184                         return do_nt_transact_create_pipe(conn, inbuf, outbuf, length, 
1185                                         bufsize, ppsetup, ppparams, ppdata);
1186                 else
1187                         return(ERROR(ERRDOS,ERRbadaccess));
1188   }
1189
1190   /*
1191    * Ensure minimum number of parameters sent.
1192    */
1193
1194   if(total_parameter_count < 54) {
1195     DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1196     return(ERROR(ERRDOS,ERRbadaccess));
1197   }
1198
1199   flags = IVAL(params,0);
1200   desired_access = IVAL(params,8);
1201   file_attributes = IVAL(params,20);
1202   share_access = IVAL(params,24);
1203   create_disposition = IVAL(params,28);
1204   create_options = IVAL(params,32);
1205   sd_len = IVAL(params,36);
1206   fname_len = MIN(((uint32)IVAL(params,44)),((uint32)sizeof(fname)-1));
1207   root_dir_fid = (uint16)IVAL(params,4);
1208   smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1209
1210   /* 
1211    * We need to construct the open_and_X ofun value from the
1212    * NT values, as that's what our code is structured to accept.
1213    */    
1214
1215   if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1216     return(ERROR(ERRDOS,ERRbadmem));
1217
1218   /*
1219    * Get the file name.
1220    */
1221
1222   if(root_dir_fid != 0) {
1223     /*
1224      * This filename is relative to a directory fid.
1225      */
1226
1227     files_struct *dir_fsp = file_fsp(params,4);
1228     size_t dir_name_len;
1229
1230     if(!dir_fsp)
1231         return(ERROR(ERRDOS,ERRbadfid));
1232
1233     if(!dir_fsp->is_directory) {
1234       /*
1235        * Check to see if this is a mac fork of some kind.
1236        */
1237
1238       get_filename_transact(&fname[0], params, 53,
1239                             total_parameter_count - 53 - fname_len, fname_len);
1240
1241       if( strchr(fname, ':')) {
1242           SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
1243           return(ERROR(0, NT_STATUS_OBJECT_PATH_NOT_FOUND));
1244       }
1245
1246       return(ERROR(ERRDOS,ERRbadfid));
1247     }
1248
1249     /*
1250      * Copy in the base directory name.
1251      */
1252
1253     pstrcpy( fname, dir_fsp->fsp_name );
1254     dir_name_len = strlen(fname);
1255
1256     /*
1257      * Ensure it ends in a '\'.
1258      */
1259
1260     if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1261       pstrcat(fname, "\\");
1262       dir_name_len++;
1263     }
1264
1265     /*
1266      * This next calculation can refuse a correct filename if we're dealing
1267      * with the Win2k unicode bug, but that would be rare. JRA.
1268      */
1269
1270     if(fname_len + dir_name_len >= sizeof(pstring))
1271       return(ERROR(ERRSRV,ERRfilespecs));
1272
1273     get_filename_transact(&fname[dir_name_len], params, 53,
1274                  total_parameter_count - 53 - fname_len, fname_len);
1275
1276   } else {
1277     get_filename_transact(&fname[0], params, 53,
1278                  total_parameter_count - 53 - fname_len, fname_len);
1279   }
1280
1281   /*
1282    * Now contruct the smb_open_mode value from the desired access
1283    * and the share access.
1284    */
1285
1286   if((smb_open_mode = map_share_mode( &stat_open_only, fname, desired_access,
1287                                       share_access, file_attributes)) == -1)
1288     return(ERROR(ERRDOS,ERRbadaccess));
1289
1290   oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1291   oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1292
1293   /*
1294    * Check if POSIX semantics are wanted.
1295    */
1296
1297   set_posix_case_semantics(file_attributes);
1298     
1299   RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1300
1301   unix_convert(fname,conn,0,&bad_path,&sbuf);
1302     
1303   unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1304    
1305   /*
1306    * If it's a request for a directory open, deal with it separately.
1307    */
1308
1309   if(create_options & FILE_DIRECTORY_FILE) {
1310
1311     oplock_request = 0;
1312
1313     /*
1314      * We will get a create directory here if the Win32
1315      * app specified a security descriptor in the 
1316      * CreateDirectory() call.
1317      */
1318
1319     fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
1320
1321     if(!fsp) {
1322       restore_case_semantics(file_attributes);
1323       if((errno == ENOENT) && bad_path) {
1324         unix_ERR_class = ERRDOS;
1325         unix_ERR_code = ERRbadpath;
1326       }
1327       return(UNIXERROR(ERRDOS,ERRnoaccess));
1328     }
1329
1330   } else {
1331
1332     /*
1333      * Ordinary file case.
1334      */
1335
1336     fsp = open_file_shared(conn,fname,&sbuf,smb_open_mode,smb_ofun,unixmode,
1337                      oplock_request,&rmode,&smb_action);
1338
1339     if (!fsp) { 
1340
1341                 if(errno == EISDIR) {
1342
1343                         /*
1344                          * Fail the open if it was explicitly a non-directory file.
1345                          */
1346
1347                         if (create_options & FILE_NON_DIRECTORY_FILE) {
1348                                 restore_case_semantics(file_attributes);
1349                                 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
1350                                 return(ERROR(0, NT_STATUS_FILE_IS_A_DIRECTORY));
1351                         }
1352         
1353                         oplock_request = 0;
1354                         fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
1355                                 
1356                         if(!fsp) {
1357                                 restore_case_semantics(file_attributes);
1358                                 if((errno == ENOENT) && bad_path) {
1359                                         unix_ERR_class = ERRDOS;
1360                                         unix_ERR_code = ERRbadpath;
1361                                 }
1362                                 return(UNIXERROR(ERRDOS,ERRnoaccess));
1363                         }
1364 #ifdef EROFS
1365                 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
1366 #else /* !EROFS */
1367                 } else if (errno == EACCES && stat_open_only) {
1368 #endif
1369
1370                         /*
1371                          * We couldn't open normally and all we want
1372                          * are the permissions. Try and do a stat open.
1373                          */
1374
1375                         oplock_request = 0;
1376
1377                         fsp = open_file_stat(conn,fname,&sbuf,smb_open_mode,&smb_action);
1378
1379                         if(!fsp) {
1380                                 restore_case_semantics(file_attributes);
1381                                 return(UNIXERROR(ERRDOS,ERRnoaccess));
1382                         }
1383                 } else {
1384
1385                         if((errno == ENOENT) && bad_path) {
1386                                 unix_ERR_class = ERRDOS;
1387                                 unix_ERR_code = ERRbadpath;
1388                         }
1389
1390                         restore_case_semantics(file_attributes);
1391
1392                         return(UNIXERROR(ERRDOS,ERRnoaccess));
1393                 }
1394       } 
1395   
1396       file_len = sbuf.st_size;
1397       fmode = dos_mode(conn,fname,&sbuf);
1398       if(fmode == 0)
1399         fmode = FILE_ATTRIBUTE_NORMAL;
1400
1401       if (fmode & aDIR) {
1402         close_file(fsp,False);
1403         restore_case_semantics(file_attributes);
1404         return(ERROR(ERRDOS,ERRnoaccess));
1405       } 
1406
1407       /* 
1408        * If the caller set the extended oplock request bit
1409        * and we granted one (by whatever means) - set the
1410        * correct bit for extended oplock reply.
1411        */
1412     
1413       if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1414         smb_action |= EXTENDED_OPLOCK_GRANTED;
1415   
1416       if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1417         smb_action |= EXTENDED_OPLOCK_GRANTED;
1418   }
1419
1420   /*
1421    * Now try and apply the desired SD.
1422    */
1423
1424   if (!set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION, &error_class, &error_code)) {
1425     close_file(fsp,False);
1426     restore_case_semantics(file_attributes);
1427     return(ERROR(error_class, error_code));
1428   }
1429
1430   restore_case_semantics(file_attributes);
1431
1432   /* Realloc the size of parameters and data we will return */
1433   params = Realloc(*ppparams, 69);
1434   if(params == NULL)
1435     return(ERROR(ERRDOS,ERRnomem));
1436
1437   *ppparams = params;
1438
1439   memset((char *)params,'\0',69);
1440
1441   p = params;
1442   if (smb_action & EXTENDED_OPLOCK_GRANTED)     
1443         SCVAL(p,0, BATCH_OPLOCK_RETURN);
1444   else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1445     SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1446   else
1447         SCVAL(p,0,NO_OPLOCK_RETURN);
1448         
1449   p += 2;
1450   SSVAL(p,0,fsp->fnum);
1451   p += 2;
1452   SIVAL(p,0,smb_action);
1453   p += 8;
1454
1455   /* Create time. */
1456   put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
1457   p += 8;
1458   put_long_date(p,sbuf.st_atime); /* access time */
1459   p += 8;
1460   put_long_date(p,sbuf.st_mtime); /* write time */
1461   p += 8;
1462   put_long_date(p,sbuf.st_mtime); /* change time */
1463   p += 8;
1464   SIVAL(p,0,fmode); /* File Attributes. */
1465   p += 4;
1466   SOFF_T(p,0,file_len);
1467   p += 8;
1468   SOFF_T(p,0,file_len);
1469
1470   DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1471
1472   /* Send the required number of replies */
1473   send_nt_replies(inbuf, outbuf, bufsize, 0, params, 69, *ppdata, 0);
1474
1475   return -1;
1476 }
1477
1478 /****************************************************************************
1479  Reply to a NT CANCEL request.
1480 ****************************************************************************/
1481 int reply_ntcancel(connection_struct *conn,
1482                    char *inbuf,char *outbuf,int length,int bufsize)
1483 {
1484         /*
1485          * Go through and cancel any pending change notifies.
1486          */
1487         
1488         int mid = SVAL(inbuf,smb_mid);
1489         START_PROFILE(SMBntcancel);
1490         remove_pending_change_notify_requests_by_mid(mid);
1491         remove_pending_lock_requests_by_mid(mid);
1492         
1493         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1494
1495         END_PROFILE(SMBntcancel);
1496         return(-1);
1497 }
1498
1499 /****************************************************************************
1500  Reply to an unsolicited SMBNTtranss - just ignore it!
1501 ****************************************************************************/
1502 int reply_nttranss(connection_struct *conn,
1503                    char *inbuf,char *outbuf,int length,int bufsize)
1504 {
1505         START_PROFILE(SMBnttranss);
1506         DEBUG(4,("Ignoring nttranss of length %d\n",length));
1507         END_PROFILE(SMBnttranss);
1508         return(-1);
1509 }
1510
1511 /****************************************************************************
1512  Reply to a notify change - queue the request and 
1513  don't allow a directory to be opened.
1514 ****************************************************************************/
1515 static int call_nt_transact_notify_change(connection_struct *conn,
1516                                    char *inbuf, char *outbuf, int length,
1517                                    int bufsize, 
1518                                    char **ppsetup, 
1519                                    char **ppparams, char **ppdata)
1520 {
1521   char *setup = *ppsetup;
1522   files_struct *fsp;
1523   uint32 flags;
1524
1525   fsp = file_fsp(setup,4);
1526   flags = IVAL(setup, 0);
1527
1528   DEBUG(3,("call_nt_transact_notify_change\n"));
1529
1530   if(!fsp)
1531     return(ERROR(ERRDOS,ERRbadfid));
1532
1533   if((!fsp->is_directory) || (conn != fsp->conn))
1534     return(ERROR(ERRDOS,ERRbadfid));
1535
1536   if (!change_notify_set(inbuf, fsp, conn, flags)) {
1537           return(UNIXERROR(ERRDOS,ERRbadfid));
1538   }
1539
1540   DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1541 name = %s\n", fsp->fsp_name ));
1542
1543   return -1;
1544 }
1545
1546 /****************************************************************************
1547  Reply to an NT transact rename command.
1548 ****************************************************************************/
1549
1550 static int call_nt_transact_rename(connection_struct *conn,
1551                                    char *inbuf, char *outbuf, int length, 
1552                                    int bufsize,
1553                                    char **ppsetup, char **ppparams, char **ppdata)
1554 {
1555   char *params = *ppparams;
1556   pstring new_name;
1557   files_struct *fsp = file_fsp(params, 0);
1558   BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1559   uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
1560                          ((uint32)sizeof(new_name)-1));
1561   int outsize = 0;
1562
1563   CHECK_FSP(fsp, conn);
1564   StrnCpy(new_name,params+4,fname_len);
1565   new_name[fname_len] = '\0';
1566
1567   outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
1568                              new_name, replace_if_exists);
1569   if(outsize == 0) {
1570     /*
1571      * Rename was successful.
1572      */
1573     send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
1574
1575     DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n", 
1576           fsp->fsp_name, new_name));
1577
1578     outsize = -1;
1579
1580         /*
1581          * Win2k needs a changenotify request response before it will
1582          * update after a rename..
1583          */
1584
1585         process_pending_change_notify_queue((time_t)0);
1586   }
1587
1588   return(outsize);
1589 }
1590
1591
1592 /****************************************************************************
1593  Reply to query a security descriptor - currently this is not implemented (it
1594  is planned to be though). Right now it just returns the same thing NT would
1595  when queried on a FAT filesystem. JRA.
1596 ****************************************************************************/
1597
1598 static int call_nt_transact_query_security_desc(connection_struct *conn,
1599                                                 char *inbuf, char *outbuf, 
1600                                                 int length, int bufsize, 
1601                                                 char **ppsetup, char **ppparams, char **ppdata)
1602 {
1603   uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1604   char *params = *ppparams;
1605   char *data = *ppdata;
1606   prs_struct pd;
1607   SEC_DESC *psd = NULL;
1608   size_t sd_size;
1609   TALLOC_CTX *mem_ctx;
1610
1611   files_struct *fsp = file_fsp(params,0);
1612
1613   if(!fsp)
1614     return(ERROR(ERRDOS,ERRbadfid));
1615
1616   DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1617
1618   params = Realloc(*ppparams, 4);
1619   if(params == NULL)
1620     return(ERROR(ERRDOS,ERRnomem));
1621
1622   *ppparams = params;
1623
1624   /*
1625    * Get the permissions to return.
1626    */
1627
1628   if((sd_size = get_nt_acl(fsp, &psd)) == 0)
1629     return(UNIXERROR(ERRDOS,ERRnoaccess));
1630
1631   DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1632
1633   SIVAL(params,0,(uint32)sd_size);
1634
1635   if(max_data_count < sd_size) {
1636
1637     send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1638                     params, 4, *ppdata, 0);
1639     return -1;
1640   }
1641
1642   /*
1643    * Allocate the data we will point this at.
1644    */
1645
1646   data = Realloc(*ppdata, sd_size);
1647   if(data == NULL) {
1648     return(ERROR(ERRDOS,ERRnomem));
1649   }
1650
1651   *ppdata = data;
1652
1653   memset(data, '\0', sd_size);
1654
1655   /*
1656    * Init the parse struct we will marshall into.
1657    */
1658
1659   if ((mem_ctx = talloc_init()) == NULL) {
1660     DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1661     return(ERROR(ERRDOS,ERRnomem));
1662   }
1663
1664   prs_init(&pd, 0, mem_ctx, MARSHALL);
1665
1666   /*
1667    * Setup the prs_struct to point at the memory we just
1668    * allocated.
1669    */
1670
1671   prs_give_memory( &pd, data, (uint32)sd_size, False);
1672
1673   /*
1674    * Finally, linearize into the outgoing buffer.
1675    */
1676
1677   if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1678     DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1679 security descriptor.\n"));
1680     /*
1681      * Return access denied for want of a better error message..
1682      */ 
1683     talloc_destroy(mem_ctx);
1684     return(UNIXERROR(ERRDOS,ERRnoaccess));
1685   }
1686
1687   /*
1688    * Now we can delete the security descriptor.
1689    */
1690
1691   talloc_destroy(mem_ctx);
1692
1693   send_nt_replies(inbuf, outbuf, bufsize, 0, params, 4, data, (int)sd_size);
1694   return -1;
1695 }
1696
1697 /****************************************************************************
1698  Reply to set a security descriptor. Map to UNIX perms.
1699 ****************************************************************************/
1700
1701 static int call_nt_transact_set_security_desc(connection_struct *conn,
1702                                                                         char *inbuf, char *outbuf, int length,
1703                                                                         int bufsize, char **ppsetup, 
1704                                                                         char **ppparams, char **ppdata)
1705 {
1706   uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1707   char *params= *ppparams;
1708   char *data = *ppdata;
1709   uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
1710   files_struct *fsp = NULL;
1711   uint32 security_info_sent = 0;
1712   int error_class;
1713   uint32 error_code;
1714
1715   if(!lp_nt_acl_support())
1716     return(UNIXERROR(ERRDOS,ERRnoaccess));
1717
1718   if(total_parameter_count < 8)
1719     return(ERROR(ERRDOS,ERRbadfunc));
1720
1721   if((fsp = file_fsp(params,0)) == NULL)
1722     return(ERROR(ERRDOS,ERRbadfid));
1723
1724   security_info_sent = IVAL(params,4);
1725
1726   DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1727        (unsigned int)security_info_sent ));
1728
1729   if (!set_sd( fsp, data, total_data_count, security_info_sent, &error_class, &error_code))
1730                 return (ERROR(error_class, error_code));
1731
1732   send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
1733   return -1;
1734 }
1735    
1736 /****************************************************************************
1737  Reply to IOCTL - not implemented - no plans.
1738 ****************************************************************************/
1739 static int call_nt_transact_ioctl(connection_struct *conn,
1740                                   char *inbuf, char *outbuf, int length,
1741                                   int bufsize, 
1742                                   char **ppsetup, char **ppparams, char **ppdata)
1743 {
1744   static BOOL logged_message = False;
1745
1746   if(!logged_message) {
1747     DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1748     logged_message = True; /* Only print this once... */
1749   }
1750   return(ERROR(ERRSRV,ERRnosupport));
1751 }
1752    
1753 /****************************************************************************
1754  Reply to a SMBNTtrans.
1755 ****************************************************************************/
1756 int reply_nttrans(connection_struct *conn,
1757                   char *inbuf,char *outbuf,int length,int bufsize)
1758 {
1759   int  outsize = 0;
1760 #if 0 /* Not used. */
1761   uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1762   uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1763   uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1764 #endif /* Not used. */
1765   uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1766   uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1767   uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1768   uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1769   uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1770   uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1771   uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1772   uint16 function_code = SVAL( inbuf, smb_nt_Function);
1773   char *params = NULL, *data = NULL, *setup = NULL;
1774   uint32 num_params_sofar, num_data_sofar;
1775   START_PROFILE(SMBnttrans);
1776
1777   if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1778     /*
1779      * Queue this open message as we are the process of an oplock break.
1780      */
1781
1782     DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1783 due to being in oplock break state.\n" ));
1784
1785     push_oplock_pending_smb_message( inbuf, length);
1786     END_PROFILE(SMBnttrans);
1787     return -1;
1788   }
1789
1790   if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
1791     END_PROFILE(SMBnttrans);
1792     return (ERROR(ERRSRV,ERRaccess));
1793   }
1794
1795   outsize = set_message(outbuf,0,0,True);
1796
1797   /* 
1798    * All nttrans messages we handle have smb_wct == 19 + setup_count.
1799    * Ensure this is so as a sanity check.
1800    */
1801
1802   if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1803     DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1804           CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1805     END_PROFILE(SMBnttrans);
1806     return(ERROR(ERRSRV,ERRerror));
1807   }
1808     
1809   /* Allocate the space for the setup, the maximum needed parameters and data */
1810
1811   if(setup_count > 0)
1812     setup = (char *)malloc(setup_count);
1813   if (total_parameter_count > 0)
1814     params = (char *)malloc(total_parameter_count);
1815   if (total_data_count > 0)
1816     data = (char *)malloc(total_data_count);
1817  
1818   if ((total_parameter_count && !params)  || (total_data_count && !data) ||
1819       (setup_count && !setup)) {
1820         safe_free(setup);
1821         safe_free(params);
1822         safe_free(data);
1823     DEBUG(0,("reply_nttrans : Out of memory\n"));
1824     END_PROFILE(SMBnttrans);
1825     return(ERROR(ERRDOS,ERRnomem));
1826   }
1827
1828   /* Copy the param and data bytes sent with this request into
1829      the params buffer */
1830   num_params_sofar = parameter_count;
1831   num_data_sofar = data_count;
1832
1833   if (parameter_count > total_parameter_count || data_count > total_data_count)
1834     exit_server("reply_nttrans: invalid sizes in packet.\n");
1835
1836   if(setup) {
1837     memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1838     DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1839     dump_data(10, setup, setup_count);
1840   }
1841   if(params) {
1842     memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1843     DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1844     dump_data(10, params, parameter_count);
1845   }
1846   if(data) {
1847     memcpy( data, smb_base(inbuf) + data_offset, data_count);
1848     DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1849     dump_data(10, data, data_count);
1850   }
1851
1852   if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1853     /* We need to send an interim response then receive the rest
1854        of the parameter/data bytes */
1855     outsize = set_message(outbuf,0,0,True);
1856     send_smb(smbd_server_fd(),outbuf);
1857
1858     while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1859       BOOL ret;
1860
1861       ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
1862
1863       if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1864         outsize = set_message(outbuf,0,0,True);
1865         if(ret) {
1866                 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1867         } else {
1868                 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1869                          (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1870         }
1871         if(params)
1872           free(params);
1873         if(data)
1874           free(data);
1875         if(setup)
1876           free(setup);
1877         END_PROFILE(SMBnttrans);
1878         return(ERROR(ERRSRV,ERRerror));
1879       }
1880       
1881       /* Revise total_params and total_data in case they have changed downwards */
1882       total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1883       total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1884       num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1885       num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1886       if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1887         exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
1888
1889       memcpy( &params[ IVAL(inbuf, smb_nts_ParameterDisplacement)], 
1890               smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1891       memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1892               smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1893     }
1894   }
1895
1896   if (Protocol >= PROTOCOL_NT1) {
1897     uint16 flg2 = SVAL(outbuf,smb_flg2);
1898     SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
1899   }
1900
1901   /* Now we must call the relevant NT_TRANS function */
1902   switch(function_code) {
1903     case NT_TRANSACT_CREATE:
1904       START_PROFILE_NESTED(NT_transact_create);
1905       outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize, 
1906                                         &setup, &params, &data);
1907       END_PROFILE_NESTED(NT_transact_create);
1908       break;
1909     case NT_TRANSACT_IOCTL:
1910       START_PROFILE_NESTED(NT_transact_ioctl);
1911       outsize = call_nt_transact_ioctl(conn, 
1912                                        inbuf, outbuf, length, bufsize, 
1913                                        &setup, &params, &data);
1914       END_PROFILE_NESTED(NT_transact_ioctl);
1915       break;
1916     case NT_TRANSACT_SET_SECURITY_DESC:
1917       START_PROFILE_NESTED(NT_transact_set_security_desc);
1918       outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf, 
1919                                                    length, bufsize, 
1920                                                    &setup, &params, &data);
1921       END_PROFILE_NESTED(NT_transact_set_security_desc);
1922       break;
1923     case NT_TRANSACT_NOTIFY_CHANGE:
1924       START_PROFILE_NESTED(NT_transact_notify_change);
1925       outsize = call_nt_transact_notify_change(conn, inbuf, outbuf, 
1926                                                length, bufsize, 
1927                                                &setup, &params, &data);
1928       END_PROFILE_NESTED(NT_transact_notify_change);
1929       break;
1930     case NT_TRANSACT_RENAME:
1931       START_PROFILE_NESTED(NT_transact_rename);
1932       outsize = call_nt_transact_rename(conn, inbuf, outbuf, length, 
1933                                         bufsize, 
1934                                         &setup, &params, &data);
1935       END_PROFILE_NESTED(NT_transact_rename);
1936       break;
1937
1938     case NT_TRANSACT_QUERY_SECURITY_DESC:
1939       START_PROFILE_NESTED(NT_transact_query_security_desc);
1940       outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf, 
1941                                                      length, bufsize, 
1942                                                      &setup, &params, &data);
1943       END_PROFILE_NESTED(NT_transact_query_security_desc);
1944       break;
1945   default:
1946           /* Error in request */
1947           DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1948           if(setup)
1949                   free(setup);
1950           if(params)
1951                   free(params);
1952           if(data)
1953                   free(data);
1954           END_PROFILE(SMBnttrans);
1955           return (ERROR(ERRSRV,ERRerror));
1956   }
1957
1958   /* As we do not know how many data packets will need to be
1959      returned here the various call_nt_transact_xxxx calls
1960      must send their own. Thus a call_nt_transact_xxxx routine only
1961      returns a value other than -1 when it wants to send
1962      an error packet. 
1963   */
1964
1965   if(setup)
1966     free(setup);
1967   if(params)
1968     free(params);
1969   if(data)
1970     free(data);
1971   END_PROFILE(SMBnttrans);
1972   return outsize; /* If a correct response was needed the call_nt_transact_xxxx 
1973                      calls have already sent it. If outsize != -1 then it is
1974                      returning an error packet. */
1975 }