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