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