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