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