r7842: With the patch I sent Steve yesterday this gives us complete POSIX pathnames.
[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    Copyright (C) Stefan (metze) Metzmacher      2003
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 extern int max_send;
25 extern enum protocol_types Protocol;
26 extern int smb_read_error;
27 extern int global_oplock_break;
28 extern struct current_user current_user;
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         "\\svcctl",
45         "\\eventlog",
46         NULL
47 };
48
49 /* Map generic permissions to file object specific permissions */
50  
51 struct generic_mapping file_generic_mapping = {
52         FILE_GENERIC_READ,
53         FILE_GENERIC_WRITE,
54         FILE_GENERIC_EXECUTE,
55         FILE_GENERIC_ALL
56 };
57
58 static char *nttrans_realloc(char **ptr, size_t size)
59 {
60         char *tptr = NULL;
61         if (ptr==NULL)
62                 smb_panic("nttrans_realloc() called with NULL ptr\n");
63                 
64         tptr = SMB_REALLOC(*ptr, size);
65         if(tptr == NULL) {
66                 *ptr = NULL;
67                 return NULL;
68         }
69         memset(tptr,'\0',size);
70
71         *ptr = tptr;
72
73         return tptr;
74 }
75
76 /****************************************************************************
77  Send the required number of replies back.
78  We assume all fields other than the data fields are
79  set correctly for the type of call.
80  HACK ! Always assumes smb_setup field is zero.
81 ****************************************************************************/
82
83 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, NTSTATUS nt_error, char *params,
84                            int paramsize, char *pdata, int datasize)
85 {
86         int data_to_send = datasize;
87         int params_to_send = paramsize;
88         int useable_space;
89         char *pp = params;
90         char *pd = pdata;
91         int params_sent_thistime, data_sent_thistime, total_sent_thistime;
92         int alignment_offset = 3;
93         int data_alignment_offset = 0;
94
95         /*
96          * Initially set the wcnt area to be 18 - this is true for all
97          * transNT replies.
98          */
99
100         set_message(outbuf,18,0,True);
101
102         if (NT_STATUS_V(nt_error))
103                 ERROR_NT(nt_error);
104
105         /* 
106          * If there genuinely are no parameters or data to send just send
107          * the empty packet.
108          */
109
110         if(params_to_send == 0 && data_to_send == 0) {
111                 if (!send_smb(smbd_server_fd(),outbuf))
112                         exit_server("send_nt_replies: send_smb failed.");
113                 return 0;
114         }
115
116         /*
117          * When sending params and data ensure that both are nicely aligned.
118          * Only do this alignment when there is also data to send - else
119          * can cause NT redirector problems.
120          */
121
122         if (((params_to_send % 4) != 0) && (data_to_send != 0))
123                 data_alignment_offset = 4 - (params_to_send % 4);
124
125         /* 
126          * Space is bufsize minus Netbios over TCP header minus SMB header.
127          * The alignment_offset is to align the param bytes on a four byte
128          * boundary (2 bytes for data len, one byte pad). 
129          * NT needs this to work correctly.
130          */
131
132         useable_space = bufsize - ((smb_buf(outbuf)+
133                                 alignment_offset+data_alignment_offset) -
134                                 outbuf);
135
136         /*
137          * useable_space can never be more than max_send minus the
138          * alignment offset.
139          */
140
141         useable_space = MIN(useable_space,
142                                 max_send - (alignment_offset+data_alignment_offset));
143
144
145         while (params_to_send || data_to_send) {
146
147                 /*
148                  * Calculate whether we will totally or partially fill this packet.
149                  */
150
151                 total_sent_thistime = params_to_send + data_to_send +
152                                         alignment_offset + data_alignment_offset;
153
154                 /* 
155                  * We can never send more than useable_space.
156                  */
157
158                 total_sent_thistime = MIN(total_sent_thistime, useable_space);
159
160                 set_message(outbuf, 18, total_sent_thistime, True);
161
162                 /*
163                  * Set total params and data to be sent.
164                  */
165
166                 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
167                 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
168
169                 /* 
170                  * Calculate how many parameters and data we can fit into
171                  * this packet. Parameters get precedence.
172                  */
173
174                 params_sent_thistime = MIN(params_to_send,useable_space);
175                 data_sent_thistime = useable_space - params_sent_thistime;
176                 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
177
178                 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
179
180                 if(params_sent_thistime == 0) {
181                         SIVAL(outbuf,smb_ntr_ParameterOffset,0);
182                         SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
183                 } else {
184                         /*
185                          * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
186                          * parameter bytes, however the first 4 bytes of outbuf are
187                          * the Netbios over TCP header. Thus use smb_base() to subtract
188                          * them from the calculation.
189                          */
190
191                         SIVAL(outbuf,smb_ntr_ParameterOffset,
192                                 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
193                         /* 
194                          * Absolute displacement of param bytes sent in this packet.
195                          */
196
197                         SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
198                 }
199
200                 /*
201                  * Deal with the data portion.
202                  */
203
204                 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
205
206                 if(data_sent_thistime == 0) {
207                         SIVAL(outbuf,smb_ntr_DataOffset,0);
208                         SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
209                 } else {
210                         /*
211                          * The offset of the data bytes is the offset of the
212                          * parameter bytes plus the number of parameters being sent this time.
213                          */
214
215                         SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
216                                 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
217                                 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
218                 }
219
220                 /* 
221                  * Copy the param bytes into the packet.
222                  */
223
224                 if(params_sent_thistime)
225                         memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
226
227                 /*
228                  * Copy in the data bytes
229                  */
230
231                 if(data_sent_thistime)
232                         memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
233                                 data_alignment_offset,pd,data_sent_thistime);
234     
235                 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
236                         params_sent_thistime, data_sent_thistime, useable_space));
237                 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
238                         params_to_send, data_to_send, paramsize, datasize));
239     
240                 /* Send the packet */
241                 if (!send_smb(smbd_server_fd(),outbuf))
242                         exit_server("send_nt_replies: send_smb failed.");
243     
244                 pp += params_sent_thistime;
245                 pd += data_sent_thistime;
246     
247                 params_to_send -= params_sent_thistime;
248                 data_to_send -= data_sent_thistime;
249
250                 /*
251                  * Sanity check
252                  */
253
254                 if(params_to_send < 0 || data_to_send < 0) {
255                         DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
256                                 params_to_send, data_to_send));
257                         return -1;
258                 }
259         } 
260
261         return 0;
262 }
263
264 /****************************************************************************
265  Is it an NTFS stream name ?
266 ****************************************************************************/
267
268 BOOL is_ntfs_stream_name(const char *fname)
269 {
270         if (lp_posix_pathnames()) {
271                 return False;
272         }
273         return (strchr_m(fname, ':') != NULL) ? True : False;
274 }
275
276 /****************************************************************************
277  Save case statics.
278 ****************************************************************************/
279
280 static BOOL saved_case_sensitive;
281 static BOOL saved_case_preserve;
282 static BOOL saved_short_case_preserve;
283
284 /****************************************************************************
285  Save case semantics.
286 ****************************************************************************/
287
288 static void set_posix_case_semantics(connection_struct *conn, uint32 file_attributes)
289 {
290         if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
291                 return;
292
293         saved_case_sensitive = conn->case_sensitive;
294         saved_case_preserve = conn->case_preserve;
295         saved_short_case_preserve = conn->short_case_preserve;
296
297         /* Set to POSIX. */
298         conn->case_sensitive = True;
299         conn->case_preserve = True;
300         conn->short_case_preserve = True;
301 }
302
303 /****************************************************************************
304  Restore case semantics.
305 ****************************************************************************/
306
307 static void restore_case_semantics(connection_struct *conn, uint32 file_attributes)
308 {
309         if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
310                 return;
311
312         conn->case_sensitive = saved_case_sensitive;
313         conn->case_preserve = saved_case_preserve;
314         conn->short_case_preserve = saved_short_case_preserve;
315 }
316
317 /****************************************************************************
318  Utility function to map create disposition.
319 ****************************************************************************/
320
321 static int map_create_disposition( uint32 create_disposition)
322 {
323         int ret;
324
325         switch( create_disposition ) {
326                 case FILE_CREATE:
327                         /* create if not exist, fail if exist */
328                         ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
329                         break;
330                 case FILE_SUPERSEDE:
331                 case FILE_OVERWRITE_IF:
332                         /* create if not exist, trunc if exist */
333                         ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
334                         break;
335                 case FILE_OPEN:
336                         /* fail if not exist, open if exists */
337                         ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
338                         break;
339                 case FILE_OPEN_IF:
340                         /* create if not exist, open if exists */
341                         ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
342                         break;
343                 case FILE_OVERWRITE:
344                         /* fail if not exist, truncate if exists */
345                         ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
346                         break;
347                 default:
348                         DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
349                                 create_disposition ));
350                         return -1;
351         }
352
353         DEBUG(10,("map_create_disposition: Mapped create_disposition 0x%lx to 0x%x\n",
354                         (unsigned long)create_disposition, ret ));
355
356         return ret;
357 }
358
359 /****************************************************************************
360  Utility function to map share modes.
361 ****************************************************************************/
362
363 static int map_share_mode( char *fname, uint32 create_options,
364                         uint32 *desired_access, uint32 share_access, uint32 file_attributes)
365 {
366         int smb_open_mode = -1;
367         uint32 original_desired_access = *desired_access;
368
369         /* This is a nasty hack - must fix... JRA. */
370         if (*desired_access == MAXIMUM_ALLOWED_ACCESS) {
371                 *desired_access = FILE_GENERIC_ALL;
372         }
373
374         /*
375          * Convert GENERIC bits to specific bits.
376          */
377
378         se_map_generic(desired_access, &file_generic_mapping);
379
380         switch( *desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
381                 case FILE_READ_DATA:
382                         smb_open_mode = DOS_OPEN_RDONLY;
383                         break;
384                 case FILE_WRITE_DATA:
385                 case FILE_APPEND_DATA:
386                 case FILE_WRITE_DATA|FILE_APPEND_DATA:
387                         smb_open_mode = DOS_OPEN_WRONLY;
388                         break;
389                 case FILE_READ_DATA|FILE_WRITE_DATA:
390                 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
391                 case FILE_READ_DATA|FILE_APPEND_DATA:
392                         smb_open_mode = DOS_OPEN_RDWR;
393                         break;
394         }
395
396         /*
397          * NB. For DELETE_ACCESS we should really check the
398          * directory permissions, as that is what controls
399          * delete, and for WRITE_DAC_ACCESS we should really
400          * check the ownership, as that is what controls the
401          * chmod. Note that this is *NOT* a security hole (this
402          * note is for you, Andrew) as we are not *allowing*
403          * the access at this point, the actual unlink or
404          * chown or chmod call would do this. We are just helping
405          * clients out by telling them if they have a hope
406          * of any of this succeeding. POSIX acls may still
407          * deny the real call. JRA.
408          */
409
410         if (smb_open_mode == -1) {
411
412                 if(*desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS|
413                                         FILE_EXECUTE|FILE_READ_ATTRIBUTES|
414                                         FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
415                                         FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
416                         smb_open_mode = DOS_OPEN_RDONLY;
417                 } else if(*desired_access == 0) {
418
419                         /* 
420                          * JRA - NT seems to sometimes send desired_access as zero. play it safe
421                          * and map to a stat open.
422                          */
423
424                         smb_open_mode = DOS_OPEN_RDONLY;
425
426                 } else {
427                         DEBUG(0,("map_share_mode: Incorrect value 0x%lx for desired_access to file %s\n",
428                                 (unsigned long)*desired_access, fname));
429                         return -1;
430                 }
431         }
432
433         /*
434          * Set the special bit that means allow share delete.
435          * This is held outside the normal share mode bits at 1<<15.
436          * JRA.
437          */
438
439         if(share_access & FILE_SHARE_DELETE) {
440                 smb_open_mode |= ALLOW_SHARE_DELETE;
441                 DEBUG(10,("map_share_mode: FILE_SHARE_DELETE requested. open_mode = 0x%x\n", smb_open_mode));
442         }
443
444         if(*desired_access & DELETE_ACCESS) {
445                 DEBUG(10,("map_share_mode: DELETE_ACCESS requested. open_mode = 0x%x\n", smb_open_mode));
446         }
447
448         /*
449          * We need to store the intent to open for Delete. This
450          * is what determines if a delete on close flag can be set.
451          * This is the wrong way (and place) to store this, but for 2.2 this
452          * is the only practical way. JRA.
453          */
454
455         if (create_options & FILE_DELETE_ON_CLOSE) {
456                 /*
457                  * W2K3 bug compatibility mode... To set delete on close
458                  * the redirector must have *specifically* set DELETE_ACCESS
459                  * in the desired_access field. Just asking for GENERIC_ALL won't do. JRA.
460                  */
461
462                 if (!(original_desired_access & DELETE_ACCESS)) {
463                         DEBUG(5,("map_share_mode: FILE_DELETE_ON_CLOSE requested without \
464 DELETE_ACCESS for file %s. (desired_access = 0x%lx)\n",
465                                 fname, (unsigned long)*desired_access));
466                         return -1;
467                 }
468                 /* Implicit delete access is *NOT* requested... */
469                 smb_open_mode |= DELETE_ON_CLOSE_FLAG;
470                 DEBUG(10,("map_share_mode: FILE_DELETE_ON_CLOSE requested. open_mode = 0x%x\n", smb_open_mode));
471         }
472
473         /* Add in the requested share mode. */
474         switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
475                 case FILE_SHARE_READ:
476                         smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
477                         break;
478                 case FILE_SHARE_WRITE:
479                         smb_open_mode |= SET_DENY_MODE(DENY_READ);
480                         break;
481                 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
482                         smb_open_mode |= SET_DENY_MODE(DENY_NONE);
483                         break;
484                 case FILE_SHARE_NONE:
485                         smb_open_mode |= SET_DENY_MODE(DENY_ALL);
486                         break;
487         }
488
489         /*
490          * Handle an O_SYNC request.
491          */
492
493         if(file_attributes & FILE_FLAG_WRITE_THROUGH)
494                 smb_open_mode |= FILE_SYNC_OPENMODE;
495
496         DEBUG(10,("map_share_mode: Mapped desired access 0x%lx, share access 0x%lx, file attributes 0x%lx \
497 to open_mode 0x%x\n", (unsigned long)*desired_access, (unsigned long)share_access,
498                 (unsigned long)file_attributes, smb_open_mode ));
499  
500         return smb_open_mode;
501 }
502
503 /****************************************************************************
504  Reply to an NT create and X call on a pipe.
505 ****************************************************************************/
506
507 static int nt_open_pipe(char *fname, connection_struct *conn,
508                         char *inbuf, char *outbuf, int *ppnum)
509 {
510         smb_np_struct *p = NULL;
511
512         uint16 vuid = SVAL(inbuf, smb_uid);
513         int i;
514
515         DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
516     
517         /* See if it is one we want to handle. */
518
519         if (lp_disable_spoolss() && strequal(fname, "\\spoolss"))
520                 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
521
522         for( i = 0; known_nt_pipes[i]; i++ )
523                 if( strequal(fname,known_nt_pipes[i]))
524                         break;
525     
526         if ( known_nt_pipes[i] == NULL )
527                 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
528     
529         /* Strip \\ off the name. */
530         fname++;
531     
532         DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
533
534         p = open_rpc_pipe_p(fname, conn, vuid);
535         if (!p)
536                 return(ERROR_DOS(ERRSRV,ERRnofids));
537
538         *ppnum = p->pnum;
539
540         return 0;
541 }
542
543 /****************************************************************************
544  Reply to an NT create and X call for pipes.
545 ****************************************************************************/
546
547 static int do_ntcreate_pipe_open(connection_struct *conn,
548                          char *inbuf,char *outbuf,int length,int bufsize)
549 {
550         pstring fname;
551         int ret;
552         int pnum = -1;
553         char *p = NULL;
554
555         srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
556
557         if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
558                 return ret;
559
560         /*
561          * Deal with pipe return.
562          */  
563
564         set_message(outbuf,34,0,True);
565
566         p = outbuf + smb_vwv2;
567         p++;
568         SSVAL(p,0,pnum);
569         p += 2;
570         SIVAL(p,0,FILE_WAS_OPENED);
571         p += 4;
572         p += 32;
573         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
574         p += 20;
575         /* File type. */
576         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
577         /* Device state. */
578         SSVAL(p,2, 0x5FF); /* ? */
579
580         DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
581
582         return chain_reply(inbuf,outbuf,length,bufsize);
583 }
584
585 /****************************************************************************
586  Reply to an NT create and X call.
587 ****************************************************************************/
588
589 int reply_ntcreate_and_X(connection_struct *conn,
590                          char *inbuf,char *outbuf,int length,int bufsize)
591 {  
592         int result;
593         pstring fname;
594         enum FAKE_FILE_TYPE fake_file_type = FAKE_FILE_TYPE_NONE;
595         uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
596         uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
597         uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
598         uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
599         uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
600         uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
601         uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
602         int smb_ofun;
603         int smb_open_mode;
604         /* Breakout the oplock request bits so we can set the
605            reply bits separately. */
606         int oplock_request = 0;
607         int fmode=0,rmode=0;
608         SMB_OFF_T file_len = 0;
609         SMB_STRUCT_STAT sbuf;
610         int smb_action = 0;
611         BOOL bad_path = False;
612         files_struct *fsp=NULL;
613         char *p = NULL;
614         time_t c_time;
615         BOOL extended_oplock_granted = False;
616         NTSTATUS status;
617
618         START_PROFILE(SMBntcreateX);
619
620         DEBUG(10,("reply_ntcreateX: flags = 0x%x, desired_access = 0x%x \
621 file_attributes = 0x%x, share_access = 0x%x, create_disposition = 0x%x \
622 create_options = 0x%x root_dir_fid = 0x%x\n", flags, desired_access, file_attributes,
623                         share_access, create_disposition,
624                         create_options, root_dir_fid ));
625
626         /* If it's an IPC, use the pipe handler. */
627
628         if (IS_IPC(conn)) {
629                 if (lp_nt_pipe_support()) {
630                         END_PROFILE(SMBntcreateX);
631                         return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
632                 } else {
633                         END_PROFILE(SMBntcreateX);
634                         return(ERROR_DOS(ERRDOS,ERRnoaccess));
635                 }
636         }
637                         
638         if (create_options & FILE_OPEN_BY_FILE_ID) {
639                 END_PROFILE(SMBntcreateX);
640                 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
641         }
642
643         /* 
644          * We need to construct the open_and_X ofun value from the
645          * NT values, as that's what our code is structured to accept.
646          */    
647         
648         if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
649                 END_PROFILE(SMBntcreateX);
650                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
651         }
652
653         /*
654          * Get the file name.
655          */
656
657         if(root_dir_fid != 0) {
658                 /*
659                  * This filename is relative to a directory fid.
660                  */
661                 pstring rel_fname;
662                 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
663                 size_t dir_name_len;
664
665                 if(!dir_fsp) {
666                         END_PROFILE(SMBntcreateX);
667                         return(ERROR_DOS(ERRDOS,ERRbadfid));
668                 }
669
670                 if(!dir_fsp->is_directory) {
671
672                         srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status,False);
673                         if (!NT_STATUS_IS_OK(status)) {
674                                 END_PROFILE(SMBntcreateX);
675                                 return ERROR_NT(status);
676                         }
677
678                         /* 
679                          * Check to see if this is a mac fork of some kind.
680                          */
681
682                         if( is_ntfs_stream_name(fname)) {
683                                 END_PROFILE(SMBntcreateX);
684                                 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
685                         }
686
687                         /*
688                           we need to handle the case when we get a
689                           relative open relative to a file and the
690                           pathname is blank - this is a reopen!
691                           (hint from demyn plantenberg)
692                         */
693
694                         END_PROFILE(SMBntcreateX);
695                         return(ERROR_DOS(ERRDOS,ERRbadfid));
696                 }
697
698                 /*
699                  * Copy in the base directory name.
700                  */
701
702                 pstrcpy( fname, dir_fsp->fsp_name );
703                 dir_name_len = strlen(fname);
704
705                 /*
706                  * Ensure it ends in a '\'.
707                  */
708
709                 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
710                         pstrcat(fname, "/");
711                         dir_name_len++;
712                 }
713
714                 srvstr_get_path(inbuf, rel_fname, smb_buf(inbuf), sizeof(rel_fname), 0, STR_TERMINATE, &status,False);
715                 if (!NT_STATUS_IS_OK(status)) {
716                         END_PROFILE(SMBntcreateX);
717                         return ERROR_NT(status);
718                 }
719                 pstrcat(fname, rel_fname);
720         } else {
721                 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status,False);
722                 if (!NT_STATUS_IS_OK(status)) {
723                         END_PROFILE(SMBntcreateX);
724                         return ERROR_NT(status);
725                 }
726
727                 /* 
728                  * Check to see if this is a mac fork of some kind.
729                  */
730
731                 if( is_ntfs_stream_name(fname)) {
732                         
733 #ifdef HAVE_SYS_QUOTAS
734                         if ((fake_file_type=is_fake_file(fname))!=FAKE_FILE_TYPE_NONE) {
735                                 /*
736                                  * here we go! support for changing the disk quotas --metze
737                                  *
738                                  * we need to fake up to open this MAGIC QUOTA file 
739                                  * and return a valid FID
740                                  *
741                                  * w2k close this file directly after openening
742                                  * xp also tries a QUERY_FILE_INFO on the file and then close it
743                                  */
744                         } else {
745 #endif
746                                 END_PROFILE(SMBntcreateX);
747                                 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
748 #ifdef HAVE_SYS_QUOTAS
749                         }
750 #endif
751                 }
752         }
753         
754         /*
755          * Now contruct the smb_open_mode value from the filename, 
756          * desired access and the share access.
757          */
758         RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
759
760         if((smb_open_mode = map_share_mode(fname, create_options, &desired_access, 
761                                            share_access, 
762                                            file_attributes)) == -1) {
763                 END_PROFILE(SMBntcreateX);
764                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
765         }
766
767         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
768         if (oplock_request) {
769                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
770         }
771
772         /*
773          * Ordinary file or directory.
774          */
775                 
776         /*
777          * Check if POSIX semantics are wanted.
778          */
779                 
780         set_posix_case_semantics(conn, file_attributes);
781                 
782         unix_convert(fname,conn,0,&bad_path,&sbuf);
783
784         /* FAKE_FILE is a special case */
785         if (fake_file_type == FAKE_FILE_TYPE_NONE) {
786                 /* Normal file. */
787                 if (bad_path) {
788                         restore_case_semantics(conn, file_attributes);
789                         END_PROFILE(SMBntcreateX);
790                         return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
791                 }
792                 /* All file access must go through check_name() */
793                 if (!check_name(fname,conn)) {
794                         restore_case_semantics(conn, file_attributes);
795                         END_PROFILE(SMBntcreateX);
796                         return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRbadpath);
797                 }
798         }
799
800 #if 0
801         /* This is the correct thing to do (check every time) but can_delete is
802            expensive (it may have to read the parent directory permissions). So
803            for now we're not doing it unless we have a strong hint the client
804            is really going to delete this file. */
805         if (desired_access & DELETE_ACCESS) {
806 #else
807         /* Setting FILE_SHARE_DELETE is the hint. */
808         if (lp_acl_check_permissions(SNUM(conn)) && (share_access & FILE_SHARE_DELETE) && (desired_access & DELETE_ACCESS)) {
809 #endif
810                 status = can_delete(conn, fname, file_attributes, bad_path, True);
811                 /* We're only going to fail here if it's access denied, as that's the
812                    only error we care about for "can we delete this ?" questions. */
813                 if (!NT_STATUS_IS_OK(status) && (NT_STATUS_EQUAL(status,NT_STATUS_ACCESS_DENIED) ||
814                                                  NT_STATUS_EQUAL(status,NT_STATUS_CANNOT_DELETE))) {
815                         restore_case_semantics(conn, file_attributes);
816                         END_PROFILE(SMBntcreateX);
817                         return ERROR_NT(status);
818                 }
819         }
820
821         /* 
822          * If it's a request for a directory open, deal with it separately.
823          */
824
825         if(create_options & FILE_DIRECTORY_FILE) {
826                 oplock_request = 0;
827                 
828                 /* Can't open a temp directory. IFS kit test. */
829                 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
830                         END_PROFILE(SMBntcreateX);
831                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
832                 }
833
834                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, &smb_action);
835                         
836                 restore_case_semantics(conn, file_attributes);
837
838                 if(!fsp) {
839                         END_PROFILE(SMBntcreateX);
840                         return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
841                 }
842         } else {
843                 /*
844                  * Ordinary file case.
845                  */
846
847                 /* NB. We have a potential bug here. If we
848                  * cause an oplock break to ourselves, then we
849                  * could end up processing filename related
850                  * SMB requests whilst we await the oplock
851                  * break response. As we may have changed the
852                  * filename case semantics to be POSIX-like,
853                  * this could mean a filename request could
854                  * fail when it should succeed. This is a rare
855                  * condition, but eventually we must arrange
856                  * to restore the correct case semantics
857                  * before issuing an oplock break request to
858                  * our client. JRA.  */
859
860                 if (fake_file_type==FAKE_FILE_TYPE_NONE) {
861                         fsp = open_file_shared1(conn,fname,&sbuf,
862                                         desired_access,
863                                         smb_open_mode,
864                                         smb_ofun,file_attributes,oplock_request,
865                                         &rmode,&smb_action);
866                 } else {
867                         /* to open a fake_file --metze */
868                         fsp = open_fake_file_shared1(fake_file_type,conn,fname,&sbuf,
869                                         desired_access,
870                                         smb_open_mode,
871                                         smb_ofun,file_attributes, oplock_request,
872                                         &rmode,&smb_action);
873                 }
874                 
875                 if (!fsp) { 
876                         /* We cheat here. There are two cases we
877                          * care about. One is a directory rename,
878                          * where the NT client will attempt to
879                          * open the source directory for
880                          * DELETE access. Note that when the
881                          * NT client does this it does *not*
882                          * set the directory bit in the
883                          * request packet. This is translated
884                          * into a read/write open
885                          * request. POSIX states that any open
886                          * for write request on a directory
887                          * will generate an EISDIR error, so
888                          * we can catch this here and open a
889                          * pseudo handle that is flagged as a
890                          * directory. The second is an open
891                          * for a permissions read only, which
892                          * we handle in the open_file_stat case. JRA.
893                          */
894
895                         if(errno == EISDIR) {
896
897                                 /*
898                                  * Fail the open if it was explicitly a non-directory file.
899                                  */
900
901                                 if (create_options & FILE_NON_DIRECTORY_FILE) {
902                                         restore_case_semantics(conn, file_attributes);
903                                         END_PROFILE(SMBntcreateX);
904                                         return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
905                                 }
906         
907                                 oplock_request = 0;
908                                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, &smb_action);
909                                 
910                                 if(!fsp) {
911                                         restore_case_semantics(conn, file_attributes);
912                                         END_PROFILE(SMBntcreateX);
913                                         return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
914                                 }
915                         } else {
916
917                                 restore_case_semantics(conn, file_attributes);
918                                 END_PROFILE(SMBntcreateX);
919                                 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
920                                         /* We have re-scheduled this call. */
921                                         return -1;
922                                 }
923                                 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
924                         }
925                 } 
926         }
927                 
928         restore_case_semantics(conn, file_attributes);
929                 
930         file_len = sbuf.st_size;
931         fmode = dos_mode(conn,fname,&sbuf);
932         if(fmode == 0) {
933                 fmode = FILE_ATTRIBUTE_NORMAL;
934         }
935         if (!fsp->is_directory && (fmode & aDIR)) {
936                 close_file(fsp,False);
937                 END_PROFILE(SMBntcreateX);
938                 return ERROR_DOS(ERRDOS,ERRnoaccess);
939         } 
940         
941         /* Save the requested allocation size. */
942         if ((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) {
943                 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize);
944 #ifdef LARGE_SMB_OFF_T
945                 allocation_size |= (((SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
946 #endif
947                 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
948                         fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
949                         if (fsp->is_directory) {
950                                 close_file(fsp,False);
951                                 END_PROFILE(SMBntcreateX);
952                                 /* Can't set allocation size on a directory. */
953                                 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
954                         }
955                         if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
956                                 close_file(fsp,False);
957                                 END_PROFILE(SMBntcreateX);
958                                 return ERROR_NT(NT_STATUS_DISK_FULL);
959                         }
960                 } else {
961                         fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
962                 }
963         }
964
965         /* 
966          * If the caller set the extended oplock request bit
967          * and we granted one (by whatever means) - set the
968          * correct bit for extended oplock reply.
969          */
970         
971         if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
972                 extended_oplock_granted = True;
973         }
974         
975         if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
976                 extended_oplock_granted = True;
977         }
978
979 #if 0
980         /* W2K sends back 42 words here ! If we do the same it breaks offline sync. Go figure... ? JRA. */
981         set_message(outbuf,42,0,True);
982 #else
983         set_message(outbuf,34,0,True);
984 #endif
985         
986         p = outbuf + smb_vwv2;
987         
988         /*
989          * Currently as we don't support level II oplocks we just report
990          * exclusive & batch here.
991          */
992
993         if (extended_oplock_granted) {
994                 if (flags & REQUEST_BATCH_OPLOCK) {
995                         SCVAL(p,0, BATCH_OPLOCK_RETURN);
996                 } else {
997                         SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
998                 }
999         } else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
1000                 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1001         } else {
1002                 SCVAL(p,0,NO_OPLOCK_RETURN);
1003         }
1004         
1005         p++;
1006         SSVAL(p,0,fsp->fnum);
1007         p += 2;
1008         if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
1009                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1010         else
1011                 SIVAL(p,0,smb_action);
1012         p += 4;
1013         
1014         /* Create time. */  
1015         c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1016
1017         if (lp_dos_filetime_resolution(SNUM(conn))) {
1018                 c_time &= ~1;
1019                 sbuf.st_atime &= ~1;
1020                 sbuf.st_mtime &= ~1;
1021                 sbuf.st_mtime &= ~1;
1022         }
1023
1024         put_long_date(p,c_time);
1025         p += 8;
1026         put_long_date(p,sbuf.st_atime); /* access time */
1027         p += 8;
1028         put_long_date(p,sbuf.st_mtime); /* write time */
1029         p += 8;
1030         put_long_date(p,sbuf.st_mtime); /* change time */
1031         p += 8;
1032         SIVAL(p,0,fmode); /* File Attributes. */
1033         p += 4;
1034         SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1035         p += 8;
1036         SOFF_T(p,0,file_len);
1037         p += 8;
1038         if (flags & EXTENDED_RESPONSE_REQUIRED)
1039                 SSVAL(p,2,0x7);
1040         p += 4;
1041         SCVAL(p,0,fsp->is_directory ? 1 : 0);
1042
1043         DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
1044
1045         result = chain_reply(inbuf,outbuf,length,bufsize);
1046         END_PROFILE(SMBntcreateX);
1047         return result;
1048 }
1049
1050 /****************************************************************************
1051  Reply to a NT_TRANSACT_CREATE call to open a pipe.
1052 ****************************************************************************/
1053
1054 static int do_nt_transact_create_pipe( connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1055                                   char **ppsetup, uint32 setup_count,
1056                                   char **ppparams, uint32 parameter_count,
1057                                   char **ppdata, uint32 data_count)
1058 {
1059         pstring fname;
1060         char *params = *ppparams;
1061         int ret;
1062         int pnum = -1;
1063         char *p = NULL;
1064         NTSTATUS status;
1065
1066         /*
1067          * Ensure minimum number of parameters sent.
1068          */
1069
1070         if(parameter_count < 54) {
1071                 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1072                 return ERROR_DOS(ERRDOS,ERRnoaccess);
1073         }
1074
1075         srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status, False);
1076         if (!NT_STATUS_IS_OK(status)) {
1077                 return ERROR_NT(status);
1078         }
1079
1080         if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
1081                 return ret;
1082         
1083         /* Realloc the size of parameters and data we will return */
1084         params = nttrans_realloc(ppparams, 69);
1085         if(params == NULL)
1086                 return ERROR_DOS(ERRDOS,ERRnomem);
1087         
1088         p = params;
1089         SCVAL(p,0,NO_OPLOCK_RETURN);
1090         
1091         p += 2;
1092         SSVAL(p,0,pnum);
1093         p += 2;
1094         SIVAL(p,0,FILE_WAS_OPENED);
1095         p += 8;
1096         
1097         p += 32;
1098         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1099         p += 20;
1100         /* File type. */
1101         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1102         /* Device state. */
1103         SSVAL(p,2, 0x5FF); /* ? */
1104         
1105         DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1106         
1107         /* Send the required number of replies */
1108         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1109         
1110         return -1;
1111 }
1112
1113 /****************************************************************************
1114  Internal fn to set security descriptors.
1115 ****************************************************************************/
1116
1117 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1118 {
1119         prs_struct pd;
1120         SEC_DESC *psd = NULL;
1121         TALLOC_CTX *mem_ctx;
1122         BOOL ret;
1123         
1124         if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
1125                 return NT_STATUS_OK;
1126         }
1127
1128         /*
1129          * Init the parse struct we will unmarshall from.
1130          */
1131
1132         if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1133                 DEBUG(0,("set_sd: talloc_init failed.\n"));
1134                 return NT_STATUS_NO_MEMORY;
1135         }
1136
1137         prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1138
1139         /*
1140          * Setup the prs_struct to point at the memory we just
1141          * allocated.
1142          */
1143         
1144         prs_give_memory( &pd, data, sd_len, False);
1145
1146         /*
1147          * Finally, unmarshall from the data buffer.
1148          */
1149
1150         if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1151                 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1152                 /*
1153                  * Return access denied for want of a better error message..
1154                  */ 
1155                 talloc_destroy(mem_ctx);
1156                 return NT_STATUS_NO_MEMORY;
1157         }
1158         
1159         if (psd->off_owner_sid==0)
1160                 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1161         if (psd->off_grp_sid==0)
1162                 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1163         if (psd->off_sacl==0)
1164                 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1165         if (psd->off_dacl==0)
1166                 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1167         
1168         ret = SMB_VFS_FSET_NT_ACL( fsp, fsp->fd, security_info_sent, psd);
1169         
1170         if (!ret) {
1171                 talloc_destroy(mem_ctx);
1172                 return NT_STATUS_ACCESS_DENIED;
1173         }
1174         
1175         talloc_destroy(mem_ctx);
1176         
1177         return NT_STATUS_OK;
1178 }
1179
1180 /****************************************************************************
1181  Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1182 ****************************************************************************/
1183                                                                                                                              
1184 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1185 {
1186         struct ea_list *ea_list_head = NULL;
1187         size_t offset = 0;
1188
1189         if (data_size < 4) {
1190                 return NULL;
1191         }
1192
1193         while (offset + 4 <= data_size) {
1194                 size_t next_offset = IVAL(pdata,offset);
1195                 struct ea_list *tmp;
1196                 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1197
1198                 DLIST_ADD_END(ea_list_head, eal, tmp);
1199                 if (next_offset == 0) {
1200                         break;
1201                 }
1202                 offset += next_offset;
1203         }
1204                                                                                                                              
1205         return ea_list_head;
1206 }
1207
1208 /****************************************************************************
1209  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1210 ****************************************************************************/
1211
1212 static int call_nt_transact_create(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1213                                   char **ppsetup, uint32 setup_count,
1214                                   char **ppparams, uint32 parameter_count,
1215                                   char **ppdata, uint32 data_count, uint32 max_data_count)
1216 {
1217         pstring fname;
1218         char *params = *ppparams;
1219         char *data = *ppdata;
1220         /* Breakout the oplock request bits so we can set the reply bits separately. */
1221         int oplock_request = 0;
1222         int fmode=0,rmode=0;
1223         SMB_OFF_T file_len = 0;
1224         SMB_STRUCT_STAT sbuf;
1225         int smb_action = 0;
1226         BOOL bad_path = False;
1227         files_struct *fsp = NULL;
1228         char *p = NULL;
1229         BOOL extended_oplock_granted = False;
1230         uint32 flags;
1231         uint32 desired_access;
1232         uint32 file_attributes;
1233         uint32 share_access;
1234         uint32 create_disposition;
1235         uint32 create_options;
1236         uint32 sd_len;
1237         uint32 ea_len;
1238         uint16 root_dir_fid;
1239         int smb_ofun;
1240         int smb_open_mode;
1241         time_t c_time;
1242         struct ea_list *ea_list = NULL;
1243         TALLOC_CTX *ctx = NULL;
1244         char *pdata = NULL;
1245         NTSTATUS status;
1246
1247         DEBUG(5,("call_nt_transact_create\n"));
1248
1249         /*
1250          * If it's an IPC, use the pipe handler.
1251          */
1252
1253         if (IS_IPC(conn)) {
1254                 if (lp_nt_pipe_support())
1255                         return do_nt_transact_create_pipe(conn, inbuf, outbuf, length, 
1256                                         bufsize,
1257                                         ppsetup, setup_count,
1258                                         ppparams, parameter_count,
1259                                         ppdata, data_count);
1260                 else
1261                         return ERROR_DOS(ERRDOS,ERRnoaccess);
1262         }
1263
1264         /*
1265          * Ensure minimum number of parameters sent.
1266          */
1267
1268         if(parameter_count < 54) {
1269                 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1270                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1271         }
1272
1273         flags = IVAL(params,0);
1274         desired_access = IVAL(params,8);
1275         file_attributes = IVAL(params,20);
1276         share_access = IVAL(params,24);
1277         create_disposition = IVAL(params,28);
1278         create_options = IVAL(params,32);
1279         sd_len = IVAL(params,36);
1280         ea_len = IVAL(params,40);
1281         root_dir_fid = (uint16)IVAL(params,4);
1282
1283         /* Ensure the data_len is correct for the sd and ea values given. */
1284         if ((ea_len + sd_len > data_count) ||
1285                         (ea_len > data_count) || (sd_len > data_count) ||
1286                         (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1287                 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1288                         (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1289                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1290         }
1291
1292         if (ea_len) {
1293                 if (!lp_ea_support(SNUM(conn))) {
1294                         DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1295                                 (unsigned int)ea_len ));
1296                         return ERROR_NT(NT_STATUS_EAS_NOT_SUPPORTED);
1297                 }
1298
1299                 if (ea_len < 10) {
1300                         DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1301                                 (unsigned int)ea_len ));
1302                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1303                 }
1304         }
1305
1306         if (create_options & FILE_OPEN_BY_FILE_ID) {
1307                 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1308         }
1309
1310         /* 
1311          * We need to construct the open_and_X ofun value from the
1312          * NT values, as that's what our code is structured to accept.
1313          */    
1314
1315         if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
1316                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1317         }
1318
1319         /*
1320          * Get the file name.
1321          */
1322
1323         if(root_dir_fid != 0) {
1324                 /*
1325                  * This filename is relative to a directory fid.
1326                  */
1327                 files_struct *dir_fsp = file_fsp(params,4);
1328                 size_t dir_name_len;
1329
1330                 if(!dir_fsp)
1331                         return ERROR_DOS(ERRDOS,ERRbadfid);
1332
1333                 if(!dir_fsp->is_directory) {
1334                         srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status, False);
1335                         if (!NT_STATUS_IS_OK(status)) {
1336                                 return ERROR_NT(status);
1337                         }
1338
1339                         /*
1340                          * Check to see if this is a mac fork of some kind.
1341                          */
1342
1343                         if( is_ntfs_stream_name(fname)) {
1344                                 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1345                         }
1346
1347                         return ERROR_DOS(ERRDOS,ERRbadfid);
1348                 }
1349
1350                 /*
1351                  * Copy in the base directory name.
1352                  */
1353
1354                 pstrcpy( fname, dir_fsp->fsp_name );
1355                 dir_name_len = strlen(fname);
1356
1357                 /*
1358                  * Ensure it ends in a '\'.
1359                  */
1360
1361                 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1362                         pstrcat(fname, "/");
1363                         dir_name_len++;
1364                 }
1365
1366                 {
1367                         pstring tmpname;
1368                         srvstr_get_path(inbuf, tmpname, params+53, sizeof(tmpname), parameter_count-53, STR_TERMINATE, &status, False);
1369                         if (!NT_STATUS_IS_OK(status)) {
1370                                 return ERROR_NT(status);
1371                         }
1372                         pstrcat(fname, tmpname);
1373                 }
1374         } else {
1375                 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status, False);
1376                 if (!NT_STATUS_IS_OK(status)) {
1377                         return ERROR_NT(status);
1378                 }
1379
1380                 /*
1381                  * Check to see if this is a mac fork of some kind.
1382                  */
1383
1384                 if( is_ntfs_stream_name(fname)) {
1385                         return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1386                 }
1387         }
1388
1389         /*
1390          * Now contruct the smb_open_mode value from the desired access
1391          * and the share access.
1392          */
1393
1394         if((smb_open_mode = map_share_mode( fname, create_options, &desired_access,
1395                                                 share_access, file_attributes)) == -1)
1396                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1397
1398         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1399         oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1400
1401         /*
1402          * Check if POSIX semantics are wanted.
1403          */
1404
1405         set_posix_case_semantics(conn, file_attributes);
1406     
1407         RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1408
1409         unix_convert(fname,conn,0,&bad_path,&sbuf);
1410         if (bad_path) {
1411                 restore_case_semantics(conn, file_attributes);
1412                 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1413         }
1414         /* All file access must go through check_name() */
1415         if (!check_name(fname,conn)) {
1416                 restore_case_semantics(conn, file_attributes);
1417                 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRbadpath);
1418         }
1419     
1420 #if 0
1421         /* This is the correct thing to do (check every time) but can_delete is
1422            expensive (it may have to read the parent directory permissions). So
1423            for now we're not doing it unless we have a strong hint the client
1424            is really going to delete this file. */
1425         if (desired_access & DELETE_ACCESS) {
1426 #else
1427         /* Setting FILE_SHARE_DELETE is the hint. */
1428         if (lp_acl_check_permissions(SNUM(conn)) && (share_access & FILE_SHARE_DELETE) && (desired_access & DELETE_ACCESS)) {
1429 #endif
1430                 status = can_delete(conn, fname, file_attributes, bad_path, True);
1431                 /* We're only going to fail here if it's access denied, as that's the
1432                    only error we care about for "can we delete this ?" questions. */
1433                 if (!NT_STATUS_IS_OK(status) && (NT_STATUS_EQUAL(status,NT_STATUS_ACCESS_DENIED) ||
1434                                                  NT_STATUS_EQUAL(status,NT_STATUS_CANNOT_DELETE))) {
1435                         restore_case_semantics(conn, file_attributes);
1436                         END_PROFILE(SMBntcreateX);
1437                         return ERROR_NT(status);
1438                 }
1439         }
1440
1441         if (ea_len) {
1442                 ctx = talloc_init("NTTRANS_CREATE_EA");
1443                 if (!ctx) {
1444                         talloc_destroy(ctx);
1445                         restore_case_semantics(conn, file_attributes);
1446                         return ERROR_NT(NT_STATUS_NO_MEMORY);
1447                 }
1448
1449                 pdata = data + sd_len;
1450
1451                 /* We have already checked that ea_len <= data_count here. */
1452                 ea_list = read_nttrans_ea_list(ctx, pdata, ea_len);
1453                 if (!ea_list ) {
1454                         talloc_destroy(ctx);
1455                         restore_case_semantics(conn, file_attributes);
1456                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1457                 }
1458         }
1459
1460         /*
1461          * If it's a request for a directory open, deal with it separately.
1462          */
1463
1464         if(create_options & FILE_DIRECTORY_FILE) {
1465
1466                 /* Can't open a temp directory. IFS kit test. */
1467                 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1468                         talloc_destroy(ctx);
1469                         restore_case_semantics(conn, file_attributes);
1470                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1471                 }
1472
1473                 oplock_request = 0;
1474
1475                 /*
1476                  * We will get a create directory here if the Win32
1477                  * app specified a security descriptor in the 
1478                  * CreateDirectory() call.
1479                  */
1480
1481                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, &smb_action);
1482
1483                 if(!fsp) {
1484                         talloc_destroy(ctx);
1485                         restore_case_semantics(conn, file_attributes);
1486                         return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1487                 }
1488
1489         } else {
1490
1491                 /*
1492                  * Ordinary file case.
1493                  */
1494
1495                 fsp = open_file_shared1(conn,fname,&sbuf,desired_access,
1496                                                 smb_open_mode,smb_ofun,file_attributes,
1497                                                 oplock_request,&rmode,&smb_action);
1498
1499                 if (!fsp) { 
1500                         if(errno == EISDIR) {
1501
1502                                 /*
1503                                  * Fail the open if it was explicitly a non-directory file.
1504                                  */
1505
1506                                 if (create_options & FILE_NON_DIRECTORY_FILE) {
1507                                         restore_case_semantics(conn, file_attributes);
1508                                         return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1509                                 }
1510         
1511                                 oplock_request = 0;
1512                                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, &smb_action);
1513                                 
1514                                 if(!fsp) {
1515                                         talloc_destroy(ctx);
1516                                         restore_case_semantics(conn, file_attributes);
1517                                         return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1518                                 }
1519                         } else {
1520                                 talloc_destroy(ctx);
1521                                 restore_case_semantics(conn, file_attributes);
1522                                 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1523                                         /* We have re-scheduled this call. */
1524                                         return -1;
1525                                 }
1526                                 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1527                         }
1528                 } 
1529         }
1530
1531         /*
1532          * According to the MS documentation, the only time the security
1533          * descriptor is applied to the opened file is iff we *created* the
1534          * file; an existing file stays the same.
1535          * 
1536          * Also, it seems (from observation) that you can open the file with
1537          * any access mask but you can still write the sd. We need to override
1538          * the granted access before we call set_sd
1539          * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1540          */
1541
1542         if (lp_nt_acl_support(SNUM(conn)) && sd_len && smb_action == FILE_WAS_CREATED) {
1543                 uint32 saved_access = fsp->desired_access;
1544
1545                 /* We have already checked that sd_len <= data_count here. */
1546
1547                 fsp->desired_access = FILE_GENERIC_ALL;
1548
1549                 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1550                 if (!NT_STATUS_IS_OK(status)) {
1551                         talloc_destroy(ctx);
1552                         close_file(fsp,False);
1553                         restore_case_semantics(conn, file_attributes);
1554                         return ERROR_NT(status);
1555                 }
1556                 fsp->desired_access = saved_access;
1557         }
1558         
1559         if (ea_len && (smb_action == FILE_WAS_CREATED)) {
1560                 status = set_ea(conn, fsp, fname, ea_list);
1561                 talloc_destroy(ctx);
1562                 if (!NT_STATUS_IS_OK(status)) {
1563                         close_file(fsp,False);
1564                         restore_case_semantics(conn, file_attributes);
1565                         return ERROR_NT(status);
1566                 }
1567         }
1568
1569         restore_case_semantics(conn, file_attributes);
1570
1571         file_len = sbuf.st_size;
1572         fmode = dos_mode(conn,fname,&sbuf);
1573         if(fmode == 0) {
1574                 fmode = FILE_ATTRIBUTE_NORMAL;
1575         }
1576         if (!fsp->is_directory && (fmode & aDIR)) {
1577                 close_file(fsp,False);
1578                 return ERROR_DOS(ERRDOS,ERRnoaccess);
1579         } 
1580         
1581         /* Save the requested allocation size. */
1582         if ((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) {
1583                 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1584 #ifdef LARGE_SMB_OFF_T
1585                 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1586 #endif
1587                 if (allocation_size && (allocation_size > file_len)) {
1588                         fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1589                         if (fsp->is_directory) {
1590                                 close_file(fsp,False);
1591                                 /* Can't set allocation size on a directory. */
1592                                 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1593                         }
1594                         if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1595                                 close_file(fsp,False);
1596                                 return ERROR_NT(NT_STATUS_DISK_FULL);
1597                         }
1598                 } else {
1599                         fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1600                 }
1601         }
1602
1603         /* 
1604          * If the caller set the extended oplock request bit
1605          * and we granted one (by whatever means) - set the
1606          * correct bit for extended oplock reply.
1607          */
1608     
1609         if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1610                 extended_oplock_granted = True;
1611         }
1612   
1613         if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1614                 extended_oplock_granted = True;
1615         }
1616
1617         /* Realloc the size of parameters and data we will return */
1618         params = nttrans_realloc(ppparams, 69);
1619         if(params == NULL)
1620                 return ERROR_DOS(ERRDOS,ERRnomem);
1621
1622         p = params;
1623         if (extended_oplock_granted)
1624                 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1625         else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1626                 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1627         else
1628                 SCVAL(p,0,NO_OPLOCK_RETURN);
1629         
1630         p += 2;
1631         SSVAL(p,0,fsp->fnum);
1632         p += 2;
1633         if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
1634                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1635         else
1636                 SIVAL(p,0,smb_action);
1637         p += 8;
1638
1639         /* Create time. */
1640         c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1641
1642         if (lp_dos_filetime_resolution(SNUM(conn))) {
1643                 c_time &= ~1;
1644                 sbuf.st_atime &= ~1;
1645                 sbuf.st_mtime &= ~1;
1646                 sbuf.st_mtime &= ~1;
1647         }
1648
1649         put_long_date(p,c_time);
1650         p += 8;
1651         put_long_date(p,sbuf.st_atime); /* access time */
1652         p += 8;
1653         put_long_date(p,sbuf.st_mtime); /* write time */
1654         p += 8;
1655         put_long_date(p,sbuf.st_mtime); /* change time */
1656         p += 8;
1657         SIVAL(p,0,fmode); /* File Attributes. */
1658         p += 4;
1659         SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1660         p += 8;
1661         SOFF_T(p,0,file_len);
1662         p += 8;
1663         if (flags & EXTENDED_RESPONSE_REQUIRED)
1664                 SSVAL(p,2,0x7);
1665         p += 4;
1666         SCVAL(p,0,fsp->is_directory ? 1 : 0);
1667
1668         DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1669
1670         /* Send the required number of replies */
1671         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1672
1673         return -1;
1674 }
1675
1676 /****************************************************************************
1677  Reply to a NT CANCEL request.
1678 ****************************************************************************/
1679
1680 int reply_ntcancel(connection_struct *conn,
1681                    char *inbuf,char *outbuf,int length,int bufsize)
1682 {
1683         /*
1684          * Go through and cancel any pending change notifies.
1685          */
1686         
1687         int mid = SVAL(inbuf,smb_mid);
1688         START_PROFILE(SMBntcancel);
1689         remove_pending_change_notify_requests_by_mid(mid);
1690         remove_pending_lock_requests_by_mid(mid);
1691         srv_cancel_sign_response(mid);
1692         
1693         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1694
1695         END_PROFILE(SMBntcancel);
1696         return(-1);
1697 }
1698
1699 /****************************************************************************
1700  Copy a file.
1701 ****************************************************************************/
1702
1703 static NTSTATUS copy_internals(connection_struct *conn, char *oldname, char *newname, uint16 attrs)
1704 {
1705         BOOL bad_path_oldname = False;
1706         BOOL bad_path_newname = False;
1707         SMB_STRUCT_STAT sbuf1, sbuf2;
1708         pstring last_component_oldname;
1709         pstring last_component_newname;
1710         files_struct *fsp1,*fsp2;
1711         uint16 fmode;
1712         int access_mode;
1713         int smb_action;
1714         SMB_OFF_T ret=-1;
1715         int close_ret;
1716         NTSTATUS status = NT_STATUS_OK;
1717
1718         ZERO_STRUCT(sbuf1);
1719         ZERO_STRUCT(sbuf2);
1720
1721         /* No wildcards. */
1722         if (ms_has_wild(newname) || ms_has_wild(oldname)) {
1723                 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1724         }
1725
1726         if (!CAN_WRITE(conn))
1727                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1728
1729         unix_convert(oldname,conn,last_component_oldname,&bad_path_oldname,&sbuf1);
1730         if (bad_path_oldname) {
1731                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1732         }
1733
1734         /* Quick check for "." and ".." */
1735         if (last_component_oldname[0] == '.') {
1736                 if (!last_component_oldname[1] || (last_component_oldname[1] == '.' && !last_component_oldname[2])) {
1737                         return NT_STATUS_OBJECT_NAME_INVALID;
1738                 }
1739         }
1740
1741         /* Source must already exist. */
1742         if (!VALID_STAT(sbuf1)) {
1743                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1744         }
1745         if (!check_name(oldname,conn)) {
1746                 return NT_STATUS_ACCESS_DENIED;
1747         }
1748
1749         /* Ensure attributes match. */
1750         fmode = dos_mode(conn,oldname,&sbuf1);
1751         if ((fmode & ~attrs) & (aHIDDEN | aSYSTEM))
1752                 return NT_STATUS_NO_SUCH_FILE;
1753
1754         unix_convert(newname,conn,last_component_newname,&bad_path_newname,&sbuf2);
1755         if (bad_path_newname) {
1756                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1757         }
1758
1759         /* Quick check for "." and ".." */
1760         if (last_component_newname[0] == '.') {
1761                 if (!last_component_newname[1] || (last_component_newname[1] == '.' && !last_component_newname[2])) {
1762                         return NT_STATUS_OBJECT_NAME_INVALID;
1763                 }
1764         }
1765
1766         /* Disallow if newname already exists. */
1767         if (VALID_STAT(sbuf2)) {
1768                 return NT_STATUS_OBJECT_NAME_COLLISION;
1769         }
1770
1771         if (!check_name(newname,conn)) {
1772                 return NT_STATUS_ACCESS_DENIED;
1773         }
1774
1775         /* No links from a directory. */
1776         if (S_ISDIR(sbuf1.st_mode)) {
1777                 return NT_STATUS_FILE_IS_A_DIRECTORY;
1778         }
1779
1780         /* Ensure this is within the share. */
1781         if (!reduce_name(conn, oldname) != 0) {
1782                 return NT_STATUS_ACCESS_DENIED;
1783         }
1784
1785         DEBUG(10,("copy_internals: doing file copy %s to %s\n", oldname, newname));
1786
1787         fsp1 = open_file_shared1(conn,oldname,&sbuf1,FILE_READ_DATA,SET_DENY_MODE(DENY_ALL)|SET_OPEN_MODE(DOS_OPEN_RDONLY),
1788                         (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN),FILE_ATTRIBUTE_NORMAL,0,
1789                         &access_mode,&smb_action);
1790
1791         if (!fsp1) {
1792                 get_saved_error_triple(NULL, NULL, &status);
1793                 if (NT_STATUS_IS_OK(status)) {
1794                         status = NT_STATUS_ACCESS_DENIED;
1795                 }
1796                 set_saved_error_triple(0, 0, NT_STATUS_OK);
1797                 return status;
1798         }
1799
1800         fsp2 = open_file_shared1(conn,newname,&sbuf2,FILE_WRITE_DATA,SET_DENY_MODE(DENY_ALL)|SET_OPEN_MODE(DOS_OPEN_WRONLY),
1801                         (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL),fmode,INTERNAL_OPEN_ONLY,
1802                         &access_mode,&smb_action);
1803
1804         if (!fsp2) {
1805                 get_saved_error_triple(NULL, NULL, &status);
1806                 if (NT_STATUS_IS_OK(status)) {
1807                         status = NT_STATUS_ACCESS_DENIED;
1808                 }
1809                 set_saved_error_triple(0, 0, NT_STATUS_OK);
1810                 close_file(fsp1,False);
1811                 return status;
1812         }
1813
1814         if (sbuf1.st_size)
1815                 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1816
1817         /*
1818          * As we are opening fsp1 read-only we only expect
1819          * an error on close on fsp2 if we are out of space.
1820          * Thus we don't look at the error return from the
1821          * close of fsp1.
1822          */
1823         close_file(fsp1,False);
1824
1825         /* Ensure the modtime is set correctly on the destination file. */
1826         fsp_set_pending_modtime(fsp2, sbuf1.st_mtime);
1827
1828         close_ret = close_file(fsp2,False);
1829
1830         /* Grrr. We have to do this as open_file_shared1 adds aARCH when it
1831            creates the file. This isn't the correct thing to do in the copy case. JRA */
1832         file_set_dosmode(conn, newname, fmode, &sbuf2, True);
1833
1834         if (ret < (SMB_OFF_T)sbuf1.st_size) {
1835                 return NT_STATUS_DISK_FULL;
1836         }
1837
1838         if (close_ret != 0) {
1839                 status = map_nt_error_from_unix(close_ret);
1840                 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1841                         nt_errstr(status), oldname, newname));
1842         }
1843         return status;
1844 }
1845
1846 /****************************************************************************
1847  Reply to a NT rename request.
1848 ****************************************************************************/
1849
1850 int reply_ntrename(connection_struct *conn,
1851                    char *inbuf,char *outbuf,int length,int bufsize)
1852 {
1853         int outsize = 0;
1854         pstring oldname;
1855         pstring newname;
1856         char *p;
1857         NTSTATUS status;
1858         uint16 attrs = SVAL(inbuf,smb_vwv0);
1859         uint16 rename_type = SVAL(inbuf,smb_vwv1);
1860
1861         START_PROFILE(SMBntrename);
1862
1863         p = smb_buf(inbuf) + 1;
1864         p += srvstr_get_path(inbuf, oldname, p, sizeof(oldname), 0, STR_TERMINATE, &status, True);
1865         if (!NT_STATUS_IS_OK(status)) {
1866                 END_PROFILE(SMBntrename);
1867                 return ERROR_NT(status);
1868         }
1869
1870         if( is_ntfs_stream_name(oldname)) {
1871                 /* Can't rename a stream. */
1872                 END_PROFILE(SMBntrename);
1873                 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1874         }
1875
1876         if (ms_has_wild(oldname)) {
1877                 END_PROFILE(SMBntrename);
1878                 return ERROR_NT(NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1879         }
1880
1881         p++;
1882         p += srvstr_get_path(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, False);
1883         if (!NT_STATUS_IS_OK(status)) {
1884                 END_PROFILE(SMBntrename);
1885                 return ERROR_NT(status);
1886         }
1887         
1888         RESOLVE_DFSPATH(oldname, conn, inbuf, outbuf);
1889         RESOLVE_DFSPATH(newname, conn, inbuf, outbuf);
1890         
1891         DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1892         
1893         switch(rename_type) {
1894                 case RENAME_FLAG_RENAME:
1895                         status = rename_internals(conn, oldname, newname, attrs, False);
1896                         break;
1897                 case RENAME_FLAG_HARD_LINK:
1898                         status = hardlink_internals(conn, oldname, newname);
1899                         break;
1900                 case RENAME_FLAG_COPY:
1901                         status = copy_internals(conn, oldname, newname, attrs);
1902                         break;
1903                 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1904                         status = NT_STATUS_INVALID_PARAMETER;
1905                         break;
1906                 default:
1907                         status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1908                         break;
1909         }
1910
1911         if (!NT_STATUS_IS_OK(status)) {
1912                 END_PROFILE(SMBntrename);
1913                 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1914                         /* We have re-scheduled this call. */
1915                         return -1;
1916                 }
1917                 return ERROR_NT(status);
1918         }
1919
1920         /*
1921          * Win2k needs a changenotify request response before it will
1922          * update after a rename..
1923          */     
1924         process_pending_change_notify_queue((time_t)0);
1925         outsize = set_message(outbuf,0,0,True);
1926   
1927         END_PROFILE(SMBntrename);
1928         return(outsize);
1929 }
1930
1931 /****************************************************************************
1932  Reply to an unsolicited SMBNTtranss - just ignore it!
1933 ****************************************************************************/
1934
1935 int reply_nttranss(connection_struct *conn,
1936                    char *inbuf,char *outbuf,int length,int bufsize)
1937 {
1938         START_PROFILE(SMBnttranss);
1939         DEBUG(4,("Ignoring nttranss of length %d\n",length));
1940         END_PROFILE(SMBnttranss);
1941         return(-1);
1942 }
1943
1944 /****************************************************************************
1945  Reply to a notify change - queue the request and 
1946  don't allow a directory to be opened.
1947 ****************************************************************************/
1948
1949 static int call_nt_transact_notify_change(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
1950                                   char **ppsetup, uint32 setup_count,
1951                                   char **ppparams, uint32 parameter_count,
1952                                   char **ppdata, uint32 data_count, uint32 max_data_count)
1953 {
1954         char *setup = *ppsetup;
1955         files_struct *fsp;
1956         uint32 flags;
1957
1958         if(setup_count < 6)
1959                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1960
1961         fsp = file_fsp(setup,4);
1962         flags = IVAL(setup, 0);
1963
1964         DEBUG(3,("call_nt_transact_notify_change\n"));
1965
1966         if(!fsp)
1967                 return ERROR_DOS(ERRDOS,ERRbadfid);
1968
1969         if((!fsp->is_directory) || (conn != fsp->conn))
1970                 return ERROR_DOS(ERRDOS,ERRbadfid);
1971
1972         if (!change_notify_set(inbuf, fsp, conn, flags))
1973                 return(UNIXERROR(ERRDOS,ERRbadfid));
1974
1975         DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1976 name = %s\n", fsp->fsp_name ));
1977
1978         return -1;
1979 }
1980
1981 /****************************************************************************
1982  Reply to an NT transact rename command.
1983 ****************************************************************************/
1984
1985 static int call_nt_transact_rename(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1986                                   char **ppsetup, uint32 setup_count,
1987                                   char **ppparams, uint32 parameter_count,
1988                                   char **ppdata, uint32 data_count, uint32 max_data_count)
1989 {
1990         char *params = *ppparams;
1991         pstring new_name;
1992         files_struct *fsp = NULL;
1993         BOOL replace_if_exists = False;
1994         NTSTATUS status;
1995
1996         if(parameter_count < 4)
1997                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1998
1999         fsp = file_fsp(params, 0);
2000         replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2001         CHECK_FSP(fsp, conn);
2002         srvstr_get_path(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE, &status, True);
2003         if (!NT_STATUS_IS_OK(status)) {
2004                 return ERROR_NT(status);
2005         }
2006
2007         status = rename_internals(conn, fsp->fsp_name,
2008                                   new_name, 0, replace_if_exists);
2009         if (!NT_STATUS_IS_OK(status))
2010                 return ERROR_NT(status);
2011
2012         /*
2013          * Rename was successful.
2014          */
2015         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2016         
2017         DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n", 
2018                  fsp->fsp_name, new_name));
2019         
2020         /*
2021          * Win2k needs a changenotify request response before it will
2022          * update after a rename..
2023          */
2024         
2025         process_pending_change_notify_queue((time_t)0);
2026
2027         return -1;
2028 }
2029
2030 /******************************************************************************
2031  Fake up a completely empty SD.
2032 *******************************************************************************/
2033
2034 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2035 {
2036         size_t sd_size;
2037
2038         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2039         if(!*ppsd) {
2040                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2041                 sd_size = 0;
2042         }
2043
2044         return sd_size;
2045 }
2046
2047 /****************************************************************************
2048  Reply to query a security descriptor.
2049 ****************************************************************************/
2050
2051 static int call_nt_transact_query_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
2052                                   char **ppsetup, uint32 setup_count,
2053                                   char **ppparams, uint32 parameter_count,
2054                                   char **ppdata, uint32 data_count, uint32 max_data_count)
2055 {
2056         char *params = *ppparams;
2057         char *data = *ppdata;
2058         prs_struct pd;
2059         SEC_DESC *psd = NULL;
2060         size_t sd_size;
2061         uint32 security_info_wanted;
2062         TALLOC_CTX *mem_ctx;
2063         files_struct *fsp = NULL;
2064
2065         if(parameter_count < 8)
2066                 return ERROR_DOS(ERRDOS,ERRbadfunc);
2067
2068         fsp = file_fsp(params,0);
2069         if(!fsp)
2070                 return ERROR_DOS(ERRDOS,ERRbadfid);
2071
2072         security_info_wanted = IVAL(params,4);
2073
2074         DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2075                         (unsigned int)security_info_wanted ));
2076
2077         params = nttrans_realloc(ppparams, 4);
2078         if(params == NULL)
2079                 return ERROR_DOS(ERRDOS,ERRnomem);
2080
2081         if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2082                 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2083                 return ERROR_DOS(ERRDOS,ERRnomem);
2084         }
2085
2086         /*
2087          * Get the permissions to return.
2088          */
2089
2090         if (!lp_nt_acl_support(SNUM(conn)))
2091                 sd_size = get_null_nt_acl(mem_ctx, &psd);
2092         else
2093                 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fd, security_info_wanted, &psd);
2094
2095         if (sd_size == 0) {
2096                 talloc_destroy(mem_ctx);
2097                 return(UNIXERROR(ERRDOS,ERRnoaccess));
2098         }
2099
2100         DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
2101
2102         SIVAL(params,0,(uint32)sd_size);
2103
2104         if(max_data_count < sd_size) {
2105
2106                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
2107                         params, 4, *ppdata, 0);
2108                 talloc_destroy(mem_ctx);
2109                 return -1;
2110         }
2111
2112         /*
2113          * Allocate the data we will point this at.
2114          */
2115
2116         data = nttrans_realloc(ppdata, sd_size);
2117         if(data == NULL) {
2118                 talloc_destroy(mem_ctx);
2119                 return ERROR_DOS(ERRDOS,ERRnomem);
2120         }
2121
2122         /*
2123          * Init the parse struct we will marshall into.
2124          */
2125
2126         prs_init(&pd, 0, mem_ctx, MARSHALL);
2127
2128         /*
2129          * Setup the prs_struct to point at the memory we just
2130          * allocated.
2131          */
2132
2133         prs_give_memory( &pd, data, (uint32)sd_size, False);
2134
2135         /*
2136          * Finally, linearize into the outgoing buffer.
2137          */
2138
2139         if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2140                 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2141 security descriptor.\n"));
2142                 /*
2143                  * Return access denied for want of a better error message..
2144                  */ 
2145                 talloc_destroy(mem_ctx);
2146                 return(UNIXERROR(ERRDOS,ERRnoaccess));
2147         }
2148
2149         /*
2150          * Now we can delete the security descriptor.
2151          */
2152
2153         talloc_destroy(mem_ctx);
2154
2155         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
2156         return -1;
2157 }
2158
2159 /****************************************************************************
2160  Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2161 ****************************************************************************/
2162
2163 static int call_nt_transact_set_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2164                                   char **ppsetup, uint32 setup_count,
2165                                   char **ppparams, uint32 parameter_count,
2166                                   char **ppdata, uint32 data_count, uint32 max_data_count)
2167 {
2168         char *params= *ppparams;
2169         char *data = *ppdata;
2170         files_struct *fsp = NULL;
2171         uint32 security_info_sent = 0;
2172         NTSTATUS nt_status;
2173
2174         if(parameter_count < 8)
2175                 return ERROR_DOS(ERRDOS,ERRbadfunc);
2176
2177         if((fsp = file_fsp(params,0)) == NULL)
2178                 return ERROR_DOS(ERRDOS,ERRbadfid);
2179
2180         if(!lp_nt_acl_support(SNUM(conn)))
2181                 goto done;
2182
2183         security_info_sent = IVAL(params,4);
2184
2185         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2186                 (unsigned int)security_info_sent ));
2187
2188         if (data_count == 0)
2189                 return ERROR_DOS(ERRDOS, ERRnoaccess);
2190
2191         if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent)))
2192                 return ERROR_NT(nt_status);
2193
2194   done:
2195
2196         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2197         return -1;
2198 }
2199    
2200 /****************************************************************************
2201  Reply to NT IOCTL
2202 ****************************************************************************/
2203
2204 static int call_nt_transact_ioctl(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
2205                                   char **ppsetup, uint32 setup_count,
2206                                   char **ppparams, uint32 parameter_count,
2207                                   char **ppdata, uint32 data_count, uint32 max_data_count)
2208 {
2209         uint32 function;
2210         uint16 fidnum;
2211         files_struct *fsp;
2212         uint8 isFSctl;
2213         uint8 compfilter;
2214         static BOOL logged_message;
2215         char *pdata = *ppdata;
2216
2217         if (setup_count != 8) {
2218                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2219                 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2220         }
2221
2222         function = IVAL(*ppsetup, 0);
2223         fidnum = SVAL(*ppsetup, 4);
2224         isFSctl = CVAL(*ppsetup, 6);
2225         compfilter = CVAL(*ppsetup, 7);
2226
2227         DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
2228                  function, fidnum, isFSctl, compfilter));
2229
2230         fsp=file_fsp(*ppsetup, 4);
2231         /* this check is done in each implemented function case for now
2232            because I don't want to break anything... --metze
2233         FSP_BELONGS_CONN(fsp,conn);*/
2234
2235         switch (function) {
2236         case FSCTL_SET_SPARSE:
2237                 /* pretend this succeeded - tho strictly we should
2238                    mark the file sparse (if the local fs supports it)
2239                    so we can know if we need to pre-allocate or not */
2240
2241                 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2242                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2243                 return -1;
2244         
2245         case FSCTL_0x000900C0:
2246                 /* pretend this succeeded - don't know what this really is
2247                    but works ok like this --metze
2248                  */
2249
2250                 DEBUG(10,("FSCTL_0x000900C0: called on FID[0x%04X](but not implemented)\n",fidnum));
2251                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2252                 return -1;
2253
2254         case FSCTL_GET_REPARSE_POINT:
2255                 /* pretend this fail - my winXP does it like this
2256                  * --metze
2257                  */
2258
2259                 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2260                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
2261                 return -1;
2262
2263         case FSCTL_SET_REPARSE_POINT:
2264                 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2265                  * --metze
2266                  */
2267
2268                 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2269                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
2270                 return -1;
2271                         
2272         case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2273         {
2274                 /*
2275                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2276                  * and return their volume names.  If max_data_count is 16, then it is just
2277                  * asking for the number of volumes and length of the combined names.
2278                  *
2279                  * pdata is the data allocated by our caller, but that uses
2280                  * total_data_count (which is 0 in our case) rather than max_data_count.
2281                  * Allocate the correct amount and return the pointer to let
2282                  * it be deallocated when we return.
2283                  */
2284                 SHADOW_COPY_DATA *shadow_data = NULL;
2285                 TALLOC_CTX *shadow_mem_ctx = NULL;
2286                 BOOL labels = False;
2287                 uint32 labels_data_count = 0;
2288                 uint32 i;
2289                 char *cur_pdata;
2290
2291                 FSP_BELONGS_CONN(fsp,conn);
2292
2293                 if (max_data_count < 16) {
2294                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2295                                 max_data_count));
2296                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2297                 }
2298
2299                 if (max_data_count > 16) {
2300                         labels = True;
2301                 }
2302
2303                 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2304                 if (shadow_mem_ctx == NULL) {
2305                         DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2306                         return ERROR_NT(NT_STATUS_NO_MEMORY);
2307                 }
2308
2309                 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2310                 if (shadow_data == NULL) {
2311                         DEBUG(0,("talloc_zero() failed!\n"));
2312                         talloc_destroy(shadow_mem_ctx);
2313                         return ERROR_NT(NT_STATUS_NO_MEMORY);
2314                 }
2315                 
2316                 shadow_data->mem_ctx = shadow_mem_ctx;
2317                 
2318                 /*
2319                  * Call the VFS routine to actually do the work.
2320                  */
2321                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2322                         talloc_destroy(shadow_data->mem_ctx);
2323                         if (errno == ENOSYS) {
2324                                 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n", 
2325                                         conn->connectpath));
2326                                 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2327                         } else {
2328                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n", 
2329                                         conn->connectpath));
2330                                 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);                        
2331                         }
2332                 }
2333
2334                 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2335
2336                 if (!labels) {
2337                         data_count = 16;
2338                 } else {
2339                         data_count = 12+labels_data_count+4;
2340                 }
2341
2342                 if (max_data_count<data_count) {
2343                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2344                                 max_data_count,data_count));
2345                         talloc_destroy(shadow_data->mem_ctx);
2346                         return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
2347                 }
2348
2349                 pdata = nttrans_realloc(ppdata, data_count);
2350                 if (pdata == NULL) {
2351                         talloc_destroy(shadow_data->mem_ctx);
2352                         return ERROR_NT(NT_STATUS_NO_MEMORY);
2353                 }               
2354
2355                 cur_pdata = pdata;
2356
2357                 /* num_volumes 4 bytes */
2358                 SIVAL(pdata,0,shadow_data->num_volumes);
2359
2360                 if (labels) {
2361                         /* num_labels 4 bytes */
2362                         SIVAL(pdata,4,shadow_data->num_volumes);
2363                 }
2364
2365                 /* needed_data_count 4 bytes */
2366                 SIVAL(pdata,8,labels_data_count);
2367
2368                 cur_pdata+=12;
2369
2370                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2371                         shadow_data->num_volumes,fsp->fsp_name));
2372                 if (labels && shadow_data->labels) {
2373                         for (i=0;i<shadow_data->num_volumes;i++) {
2374                                 srvstr_push(outbuf, cur_pdata, shadow_data->labels[i], 2*sizeof(SHADOW_COPY_LABEL), STR_UNICODE|STR_TERMINATE);
2375                                 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2376                                 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2377                         }
2378                 }
2379
2380                 talloc_destroy(shadow_data->mem_ctx);
2381
2382                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, pdata, data_count);
2383
2384                 return -1;
2385         }
2386         
2387         case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2388         {
2389                 /* pretend this succeeded - 
2390                  * 
2391                  * we have to send back a list with all files owned by this SID
2392                  *
2393                  * but I have to check that --metze
2394                  */
2395                 DOM_SID sid;
2396                 uid_t uid;
2397                 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2398                 
2399                 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2400
2401                 FSP_BELONGS_CONN(fsp,conn);
2402
2403                 /* unknown 4 bytes: this is not the length of the sid :-(  */
2404                 /*unknown = IVAL(pdata,0);*/
2405                 
2406                 sid_parse(pdata+4,sid_len,&sid);
2407                 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2408
2409                 if (!NT_STATUS_IS_OK(sid_to_uid(&sid, &uid))) {
2410                         DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2411                                 sid_string_static(&sid),(unsigned long)sid_len));
2412                         uid = (-1);
2413                 }
2414                 
2415                 /* we can take a look at the find source :-)
2416                  *
2417                  * find ./ -uid $uid  -name '*'   is what we need here
2418                  *
2419                  *
2420                  * and send 4bytes len and then NULL terminated unicode strings
2421                  * for each file
2422                  *
2423                  * but I don't know how to deal with the paged results
2424                  * (maybe we can hang the result anywhere in the fsp struct)
2425                  *
2426                  * we don't send all files at once
2427                  * and at the next we should *not* start from the beginning, 
2428                  * so we have to cache the result 
2429                  *
2430                  * --metze
2431                  */
2432                 
2433                 /* this works for now... */
2434                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2435                 return -1;      
2436         }       
2437         default:
2438                 if (!logged_message) {
2439                         logged_message = True; /* Only print this once... */
2440                         DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2441                                  function));
2442                 }
2443         }
2444
2445         return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2446 }
2447
2448
2449 #ifdef HAVE_SYS_QUOTAS
2450 /****************************************************************************
2451  Reply to get user quota 
2452 ****************************************************************************/
2453
2454 static int call_nt_transact_get_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
2455                                   char **ppsetup, uint32 setup_count,
2456                                   char **ppparams, uint32 parameter_count,
2457                                   char **ppdata, uint32 data_count, uint32 max_data_count)
2458 {
2459         NTSTATUS nt_status = NT_STATUS_OK;
2460         char *params = *ppparams;
2461         char *pdata = *ppdata;
2462         char *entry;
2463         int data_len=0,param_len=0;
2464         int qt_len=0;
2465         int entry_len = 0;
2466         files_struct *fsp = NULL;
2467         uint16 level = 0;
2468         size_t sid_len;
2469         DOM_SID sid;
2470         BOOL start_enum = True;
2471         SMB_NTQUOTA_STRUCT qt;
2472         SMB_NTQUOTA_LIST *tmp_list;
2473         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2474         extern struct current_user current_user;
2475
2476         ZERO_STRUCT(qt);
2477
2478         /* access check */
2479         if (current_user.uid != 0) {
2480                 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2481                         lp_servicename(SNUM(conn)),conn->user));
2482                 return ERROR_DOS(ERRDOS,ERRnoaccess);
2483         }
2484
2485         /*
2486          * Ensure minimum number of parameters sent.
2487          */
2488
2489         if (parameter_count < 4) {
2490                 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2491                 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2492         }
2493         
2494         /* maybe we can check the quota_fnum */
2495         fsp = file_fsp(params,0);
2496         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2497                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2498                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2499         }
2500
2501         /* the NULL pointer cheking for fsp->fake_file_handle->pd
2502          * is done by CHECK_NTQUOTA_HANDLE_OK()
2503          */
2504         qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2505
2506         level = SVAL(params,2);
2507         
2508         /* unknown 12 bytes leading in params */ 
2509         
2510         switch (level) {
2511                 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2512                         /* seems that we should continue with the enum here --metze */
2513
2514                         if (qt_handle->quota_list!=NULL && 
2515                             qt_handle->tmp_list==NULL) {
2516                 
2517                                 /* free the list */
2518                                 free_ntquota_list(&(qt_handle->quota_list));
2519
2520                                 /* Realloc the size of parameters and data we will return */
2521                                 param_len = 4;
2522                                 params = nttrans_realloc(ppparams, param_len);
2523                                 if(params == NULL)
2524                                         return ERROR_DOS(ERRDOS,ERRnomem);
2525
2526                                 data_len = 0;
2527                                 SIVAL(params,0,data_len);
2528
2529                                 break;
2530                         }
2531
2532                         start_enum = False;
2533
2534                 case TRANSACT_GET_USER_QUOTA_LIST_START:
2535
2536                         if (qt_handle->quota_list==NULL &&
2537                                 qt_handle->tmp_list==NULL) {
2538                                 start_enum = True;
2539                         }
2540
2541                         if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
2542                                 return ERROR_DOS(ERRSRV,ERRerror);
2543
2544                         /* Realloc the size of parameters and data we will return */
2545                         param_len = 4;
2546                         params = nttrans_realloc(ppparams, param_len);
2547                         if(params == NULL)
2548                                 return ERROR_DOS(ERRDOS,ERRnomem);
2549
2550                         /* we should not trust the value in max_data_count*/
2551                         max_data_count = MIN(max_data_count,2048);
2552                         
2553                         pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2554                         if(pdata == NULL)
2555                                 return ERROR_DOS(ERRDOS,ERRnomem);
2556
2557                         entry = pdata;
2558
2559
2560                         /* set params Size of returned Quota Data 4 bytes*/
2561                         /* but set it later when we know it */
2562                 
2563                         /* for each entry push the data */
2564
2565                         if (start_enum) {
2566                                 qt_handle->tmp_list = qt_handle->quota_list;
2567                         }
2568
2569                         tmp_list = qt_handle->tmp_list;
2570
2571                         for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2572                                 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2573
2574                                 sid_len = sid_size(&tmp_list->quotas->sid);
2575                                 entry_len = 40 + sid_len;
2576
2577                                 /* nextoffset entry 4 bytes */
2578                                 SIVAL(entry,0,entry_len);
2579                 
2580                                 /* then the len of the SID 4 bytes */
2581                                 SIVAL(entry,4,sid_len);
2582                                 
2583                                 /* unknown data 8 bytes SMB_BIG_UINT */
2584                                 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2585                                 
2586                                 /* the used disk space 8 bytes SMB_BIG_UINT */
2587                                 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2588                                 
2589                                 /* the soft quotas 8 bytes SMB_BIG_UINT */
2590                                 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2591                                 
2592                                 /* the hard quotas 8 bytes SMB_BIG_UINT */
2593                                 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2594                                 
2595                                 /* and now the SID */
2596                                 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2597                         }
2598                         
2599                         qt_handle->tmp_list = tmp_list;
2600                         
2601                         /* overwrite the offset of the last entry */
2602                         SIVAL(entry-entry_len,0,0);
2603
2604                         data_len = 4+qt_len;
2605                         /* overwrite the params quota_data_len */
2606                         SIVAL(params,0,data_len);
2607
2608                         break;
2609
2610                 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2611                         
2612                         /* unknown 4 bytes IVAL(pdata,0) */     
2613                         
2614                         if (data_count < 8) {
2615                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2616                                 return ERROR_DOS(ERRDOS,ERRunknownlevel);                               
2617                         }
2618
2619                         sid_len = IVAL(pdata,4);
2620                         /* Ensure this is less than 1mb. */
2621                         if (sid_len > (1024*1024)) {
2622                                 return ERROR_DOS(ERRDOS,ERRnomem);
2623                         }
2624
2625                         if (data_count < 8+sid_len) {
2626                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2627                                 return ERROR_DOS(ERRDOS,ERRunknownlevel);                               
2628                         }
2629
2630                         data_len = 4+40+sid_len;
2631
2632                         if (max_data_count < data_len) {
2633                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2634                                         max_data_count, data_len));
2635                                 param_len = 4;
2636                                 SIVAL(params,0,data_len);
2637                                 data_len = 0;
2638                                 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2639                                 break;
2640                         }
2641
2642                         sid_parse(pdata+8,sid_len,&sid);
2643                 
2644
2645                         if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2646                                 ZERO_STRUCT(qt);
2647                                 /* 
2648                                  * we have to return zero's in all fields 
2649                                  * instead of returning an error here
2650                                  * --metze
2651                                  */
2652                         }
2653
2654                         /* Realloc the size of parameters and data we will return */
2655                         param_len = 4;
2656                         params = nttrans_realloc(ppparams, param_len);
2657                         if(params == NULL)
2658                                 return ERROR_DOS(ERRDOS,ERRnomem);
2659
2660                         pdata = nttrans_realloc(ppdata, data_len);
2661                         if(pdata == NULL)
2662                                 return ERROR_DOS(ERRDOS,ERRnomem);
2663
2664                         entry = pdata;
2665
2666                         /* set params Size of returned Quota Data 4 bytes*/
2667                         SIVAL(params,0,data_len);
2668         
2669                         /* nextoffset entry 4 bytes */
2670                         SIVAL(entry,0,0);
2671         
2672                         /* then the len of the SID 4 bytes */
2673                         SIVAL(entry,4,sid_len);
2674                         
2675                         /* unknown data 8 bytes SMB_BIG_UINT */
2676                         SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2677                         
2678                         /* the used disk space 8 bytes SMB_BIG_UINT */
2679                         SBIG_UINT(entry,16,qt.usedspace);
2680                         
2681                         /* the soft quotas 8 bytes SMB_BIG_UINT */
2682                         SBIG_UINT(entry,24,qt.softlim);
2683                         
2684                         /* the hard quotas 8 bytes SMB_BIG_UINT */
2685                         SBIG_UINT(entry,32,qt.hardlim);
2686                         
2687                         /* and now the SID */
2688                         sid_linearize(entry+40, sid_len, &sid);
2689
2690                         break;
2691
2692                 default:
2693                         DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2694                         return ERROR_DOS(ERRSRV,ERRerror);
2695                         break;
2696         }
2697
2698         send_nt_replies(inbuf, outbuf, bufsize, nt_status, params, param_len, pdata, data_len);
2699
2700         return -1;
2701 }
2702
2703 /****************************************************************************
2704  Reply to set user quota
2705 ****************************************************************************/
2706
2707 static int call_nt_transact_set_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
2708                                   char **ppsetup, uint32 setup_count,
2709                                   char **ppparams, uint32 parameter_count,
2710                                   char **ppdata, uint32 data_count, uint32 max_data_count)
2711 {
2712         char *params = *ppparams;
2713         char *pdata = *ppdata;
2714         int data_len=0,param_len=0;
2715         SMB_NTQUOTA_STRUCT qt;
2716         size_t sid_len;
2717         DOM_SID sid;
2718         files_struct *fsp = NULL;
2719
2720         ZERO_STRUCT(qt);
2721
2722         /* access check */
2723         if (current_user.uid != 0) {
2724                 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2725                         lp_servicename(SNUM(conn)),conn->user));
2726                 return ERROR_DOS(ERRDOS,ERRnoaccess);
2727         }
2728
2729         /*
2730          * Ensure minimum number of parameters sent.
2731          */
2732
2733         if (parameter_count < 2) {
2734                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2735                 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2736         }
2737         
2738         /* maybe we can check the quota_fnum */
2739         fsp = file_fsp(params,0);
2740         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2741                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2742                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2743         }
2744
2745         if (data_count < 40) {
2746                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2747                 return ERROR_DOS(ERRDOS,ERRunknownlevel);               
2748         }
2749
2750         /* offset to next quota record.
2751          * 4 bytes IVAL(pdata,0)
2752          * unused here...
2753          */
2754
2755         /* sid len */
2756         sid_len = IVAL(pdata,4);
2757
2758         if (data_count < 40+sid_len) {
2759                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2760                 return ERROR_DOS(ERRDOS,ERRunknownlevel);               
2761         }
2762
2763         /* unknown 8 bytes in pdata 
2764          * maybe its the change time in NTTIME
2765          */
2766
2767         /* the used space 8 bytes (SMB_BIG_UINT)*/
2768         qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2769 #ifdef LARGE_SMB_OFF_T
2770         qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2771 #else /* LARGE_SMB_OFF_T */
2772         if ((IVAL(pdata,20) != 0)&&
2773                 ((qt.usedspace != 0xFFFFFFFF)||
2774                 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2775                 /* more than 32 bits? */
2776                 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2777         }
2778 #endif /* LARGE_SMB_OFF_T */
2779
2780         /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2781         qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2782 #ifdef LARGE_SMB_OFF_T
2783         qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2784 #else /* LARGE_SMB_OFF_T */
2785         if ((IVAL(pdata,28) != 0)&&
2786                 ((qt.softlim != 0xFFFFFFFF)||
2787                 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2788                 /* more than 32 bits? */
2789                 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2790         }
2791 #endif /* LARGE_SMB_OFF_T */
2792
2793         /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2794         qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2795 #ifdef LARGE_SMB_OFF_T
2796         qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2797 #else /* LARGE_SMB_OFF_T */
2798         if ((IVAL(pdata,36) != 0)&&
2799                 ((qt.hardlim != 0xFFFFFFFF)||
2800                 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2801                 /* more than 32 bits? */
2802                 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2803         }
2804 #endif /* LARGE_SMB_OFF_T */
2805         
2806         sid_parse(pdata+40,sid_len,&sid);
2807         DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2808
2809         /* 44 unknown bytes left... */
2810
2811         if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2812                 return ERROR_DOS(ERRSRV,ERRerror);      
2813         }
2814
2815         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, pdata, data_len);
2816
2817         return -1;
2818 }
2819 #endif /* HAVE_SYS_QUOTAS */
2820
2821 /****************************************************************************
2822  Reply to a SMBNTtrans.
2823 ****************************************************************************/
2824
2825 int reply_nttrans(connection_struct *conn,
2826                         char *inbuf,char *outbuf,int length,int bufsize)
2827 {
2828         int  outsize = 0;
2829         uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2830 #if 0 /* Not used. */
2831         uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
2832         uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
2833 #endif /* Not used. */
2834         uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
2835         uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
2836         uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
2837         uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
2838         uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
2839         uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
2840         uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
2841         uint16 function_code = SVAL( inbuf, smb_nt_Function);
2842         char *params = NULL, *data = NULL, *setup = NULL;
2843         uint32 num_params_sofar, num_data_sofar;
2844         START_PROFILE(SMBnttrans);
2845
2846         if(global_oplock_break &&
2847                         ((function_code == NT_TRANSACT_CREATE) ||
2848                          (function_code == NT_TRANSACT_RENAME))) {
2849                 /*
2850                  * Queue this open message as we are the process of an oplock break.
2851                  */
2852
2853                 DEBUG(2,("reply_nttrans: queueing message code 0x%x \
2854 due to being in oplock break state.\n", (unsigned int)function_code ));
2855
2856                 push_oplock_pending_smb_message( inbuf, length);
2857                 END_PROFILE(SMBnttrans);
2858                 return -1;
2859         }
2860
2861         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2862                 END_PROFILE(SMBnttrans);
2863                 return ERROR_DOS(ERRSRV,ERRaccess);
2864         }
2865
2866         outsize = set_message(outbuf,0,0,True);
2867
2868         /* 
2869          * All nttrans messages we handle have smb_wct == 19 + setup_count.
2870          * Ensure this is so as a sanity check.
2871          */
2872
2873         if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
2874                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2875                         CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
2876                 goto bad_param;
2877         }
2878
2879         /* Don't allow more than 128mb for each value. */
2880         if ((total_parameter_count > (1024*1024*128)) || (total_data_count > (1024*1024*128))) {
2881                 END_PROFILE(SMBnttrans);
2882                 return ERROR_DOS(ERRDOS,ERRnomem);
2883         }
2884
2885         /* Allocate the space for the setup, the maximum needed parameters and data */
2886
2887         if(setup_count > 0)
2888                 setup = (char *)SMB_MALLOC(setup_count);
2889         if (total_parameter_count > 0)
2890                 params = (char *)SMB_MALLOC(total_parameter_count);
2891         if (total_data_count > 0)
2892                 data = (char *)SMB_MALLOC(total_data_count);
2893  
2894         if ((total_parameter_count && !params)  || (total_data_count && !data) ||
2895                                 (setup_count && !setup)) {
2896                 SAFE_FREE(setup);
2897                 SAFE_FREE(params);
2898                 SAFE_FREE(data);
2899                 DEBUG(0,("reply_nttrans : Out of memory\n"));
2900                 END_PROFILE(SMBnttrans);
2901                 return ERROR_DOS(ERRDOS,ERRnomem);
2902         }
2903
2904         /* Copy the param and data bytes sent with this request into the params buffer */
2905         num_params_sofar = parameter_count;
2906         num_data_sofar = data_count;
2907
2908         if (parameter_count > total_parameter_count || data_count > total_data_count)
2909                 goto bad_param;
2910
2911         if(setup) {
2912                 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
2913                 if ((smb_nt_SetupStart + setup_count < smb_nt_SetupStart) ||
2914                                 (smb_nt_SetupStart + setup_count < setup_count))
2915                         goto bad_param;
2916                 if (smb_nt_SetupStart + setup_count > length)
2917                         goto bad_param;
2918
2919                 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
2920                 dump_data(10, setup, setup_count);
2921         }
2922         if(params) {
2923                 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
2924                 if ((parameter_offset + parameter_count < parameter_offset) ||
2925                                 (parameter_offset + parameter_count < parameter_count))
2926                         goto bad_param;
2927                 if ((smb_base(inbuf) + parameter_offset + parameter_count > inbuf + length)||
2928                                 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf)))
2929                         goto bad_param;
2930
2931                 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
2932                 dump_data(10, params, parameter_count);
2933         }
2934         if(data) {
2935                 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
2936                 if ((data_offset + data_count < data_offset) || (data_offset + data_count < data_count))
2937                         goto bad_param;
2938                 if ((smb_base(inbuf) + data_offset + data_count > inbuf + length) ||
2939                                 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf)))
2940                         goto bad_param;
2941
2942                 memcpy( data, smb_base(inbuf) + data_offset, data_count);
2943                 dump_data(10, data, data_count);
2944         }
2945
2946         srv_signing_trans_start(SVAL(inbuf,smb_mid));
2947
2948         if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2949                 /* We need to send an interim response then receive the rest
2950                         of the parameter/data bytes */
2951                 outsize = set_message(outbuf,0,0,True);
2952                 srv_signing_trans_stop();
2953                 if (!send_smb(smbd_server_fd(),outbuf))
2954                         exit_server("reply_nttrans: send_smb failed.");
2955
2956                 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2957                         BOOL ret;
2958                         uint32 parameter_displacement;
2959                         uint32 data_displacement;
2960
2961                         ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
2962
2963                         /* We need to re-calcuate the new length after we've read the secondary packet. */
2964                         length = smb_len(inbuf) + 4;
2965
2966                         /*
2967                          * The sequence number for the trans reply is always
2968                          * based on the last secondary received.
2969                          */
2970
2971                         srv_signing_trans_start(SVAL(inbuf,smb_mid));
2972
2973                         if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
2974                                 outsize = set_message(outbuf,0,0,True);
2975                                 if(ret) {
2976                                         DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
2977                                 } else {
2978                                         DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
2979                                                 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
2980                                 }
2981                                 goto bad_param;
2982                         }
2983       
2984                         /* Revise total_params and total_data in case they have changed downwards */
2985                         if (IVAL(inbuf, smb_nts_TotalParameterCount) < total_parameter_count)
2986                                 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
2987                         if (IVAL(inbuf, smb_nts_TotalDataCount) < total_data_count)
2988                                 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
2989
2990                         parameter_count = IVAL(inbuf,smb_nts_ParameterCount);
2991                         parameter_offset = IVAL(inbuf, smb_nts_ParameterOffset);
2992                         parameter_displacement = IVAL(inbuf, smb_nts_ParameterDisplacement);
2993                         num_params_sofar += parameter_count;
2994
2995                         data_count = IVAL(inbuf, smb_nts_DataCount);
2996                         data_displacement = IVAL(inbuf, smb_nts_DataDisplacement);
2997                         data_offset = IVAL(inbuf, smb_nts_DataOffset);
2998                         num_data_sofar += data_count;
2999
3000                         if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count) {
3001                                 DEBUG(0,("reply_nttrans2: data overflow in secondary nttrans packet"));
3002                                 goto bad_param;
3003                         }
3004
3005                         if (parameter_count) {
3006                                 if (parameter_displacement + parameter_count > total_parameter_count)
3007                                         goto bad_param;
3008                                 if ((parameter_displacement + parameter_count < parameter_displacement) ||
3009                                                 (parameter_displacement + parameter_count < parameter_count))
3010                                         goto bad_param;
3011                                 if (parameter_displacement > total_parameter_count)
3012                                         goto bad_param;
3013                                 if ((smb_base(inbuf) + parameter_offset + parameter_count > inbuf + length) ||
3014                                                 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf)))
3015                                         goto bad_param;
3016                                 if (parameter_displacement + params < params)
3017                                         goto bad_param;
3018
3019                                 memcpy( &params[parameter_displacement], smb_base(inbuf) + parameter_offset, parameter_count);
3020                         }
3021
3022                         if (data_count) {
3023                                 if (data_displacement + data_count > total_data_count)
3024                                         goto bad_param;
3025                                 if ((data_displacement + data_count < data_displacement) ||
3026                                                 (data_displacement + data_count < data_count))
3027                                         goto bad_param;
3028                                 if (data_displacement > total_data_count)
3029                                         goto bad_param;
3030                                 if ((smb_base(inbuf) + data_offset + data_count > inbuf + length) ||
3031                                                 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf)))
3032                                         goto bad_param;
3033                                 if (data_displacement + data < data)
3034                                         goto bad_param;
3035
3036                                 memcpy( &data[data_displacement], smb_base(inbuf)+ data_offset, data_count);
3037                         }
3038                 }
3039         }
3040
3041         if (Protocol >= PROTOCOL_NT1)
3042                 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | FLAGS2_IS_LONG_NAME);
3043
3044         /* Now we must call the relevant NT_TRANS function */
3045         switch(function_code) {
3046                 case NT_TRANSACT_CREATE:
3047                         START_PROFILE_NESTED(NT_transact_create);
3048                         outsize = call_nt_transact_create(conn, inbuf, outbuf,
3049                                                         length, bufsize, 
3050                                                         &setup, setup_count,
3051                                                         &params, total_parameter_count, 
3052                                                         &data, total_data_count, max_data_count);
3053                         END_PROFILE_NESTED(NT_transact_create);
3054                         break;
3055                 case NT_TRANSACT_IOCTL:
3056                         START_PROFILE_NESTED(NT_transact_ioctl);
3057                         outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
3058                                                          length, bufsize, 
3059                                                          &setup, setup_count,
3060                                                          &params, total_parameter_count, 
3061                                                          &data, total_data_count, max_data_count);
3062                         END_PROFILE_NESTED(NT_transact_ioctl);
3063                         break;
3064                 case NT_TRANSACT_SET_SECURITY_DESC:
3065                         START_PROFILE_NESTED(NT_transact_set_security_desc);
3066                         outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf, 
3067                                                          length, bufsize, 
3068                                                          &setup, setup_count,
3069                                                          &params, total_parameter_count, 
3070                                                          &data, total_data_count, max_data_count);
3071                         END_PROFILE_NESTED(NT_transact_set_security_desc);
3072                         break;
3073                 case NT_TRANSACT_NOTIFY_CHANGE:
3074                         START_PROFILE_NESTED(NT_transact_notify_change);
3075                         outsize = call_nt_transact_notify_change(conn, inbuf, outbuf, 
3076                                                          length, bufsize, 
3077                                                          &setup, setup_count,
3078                                                          &params, total_parameter_count, 
3079                                                          &data, total_data_count, max_data_count);
3080                         END_PROFILE_NESTED(NT_transact_notify_change);
3081                         break;
3082                 case NT_TRANSACT_RENAME:
3083                         START_PROFILE_NESTED(NT_transact_rename);
3084                         outsize = call_nt_transact_rename(conn, inbuf, outbuf,
3085                                                          length, bufsize, 
3086                                                          &setup, setup_count,
3087                                                          &params, total_parameter_count, 
3088                                                          &data, total_data_count, max_data_count);
3089                         END_PROFILE_NESTED(NT_transact_rename);
3090                         break;
3091
3092                 case NT_TRANSACT_QUERY_SECURITY_DESC:
3093                         START_PROFILE_NESTED(NT_transact_query_security_desc);
3094                         outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf, 
3095                                                          length, bufsize, 
3096                                                          &setup, setup_count,
3097                                                          &params, total_parameter_count, 
3098                                                          &data, total_data_count, max_data_count);
3099                         END_PROFILE_NESTED(NT_transact_query_security_desc);
3100                         break;
3101 #ifdef HAVE_SYS_QUOTAS
3102                 case NT_TRANSACT_GET_USER_QUOTA:
3103                         START_PROFILE_NESTED(NT_transact_get_user_quota);
3104                         outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf, 
3105                                                          length, bufsize, 
3106                                                          &setup, setup_count,
3107                                                          &params, total_parameter_count, 
3108                                                          &data, total_data_count, max_data_count);
3109                         END_PROFILE_NESTED(NT_transact_get_user_quota);
3110                         break;
3111                 case NT_TRANSACT_SET_USER_QUOTA:
3112                         START_PROFILE_NESTED(NT_transact_set_user_quota);
3113                         outsize = call_nt_transact_set_user_quota(conn, inbuf, outbuf, 
3114                                                          length, bufsize, 
3115                                                          &setup, setup_count,
3116                                                          &params, total_parameter_count, 
3117                                                          &data, total_data_count, max_data_count);
3118                         END_PROFILE_NESTED(NT_transact_set_user_quota);
3119                         break;                                  
3120 #endif /* HAVE_SYS_QUOTAS */
3121                 default:
3122                         /* Error in request */
3123                         DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
3124                         SAFE_FREE(setup);
3125                         SAFE_FREE(params);
3126                         SAFE_FREE(data);
3127                         END_PROFILE(SMBnttrans);
3128                         srv_signing_trans_stop();
3129                         return ERROR_DOS(ERRSRV,ERRerror);
3130         }
3131
3132         /* As we do not know how many data packets will need to be
3133                 returned here the various call_nt_transact_xxxx calls
3134                 must send their own. Thus a call_nt_transact_xxxx routine only
3135                 returns a value other than -1 when it wants to send
3136                 an error packet. 
3137         */
3138
3139         srv_signing_trans_stop();
3140
3141         SAFE_FREE(setup);
3142         SAFE_FREE(params);
3143         SAFE_FREE(data);
3144         END_PROFILE(SMBnttrans);
3145         return outsize; /* If a correct response was needed the call_nt_transact_xxxx 
3146                                 calls have already sent it. If outsize != -1 then it is
3147                                 returning an error packet. */
3148
3149  bad_param:
3150
3151         srv_signing_trans_stop();
3152         SAFE_FREE(params);
3153         SAFE_FREE(data);
3154         SAFE_FREE(setup);
3155         END_PROFILE(SMBnttrans);
3156         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
3157 }