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