Use a common function to parse all pathnames from the wire. This allows
[samba.git] / source / smbd / nttrans.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB NT transaction handling
4    Copyright (C) Jeremy Allison                 1994-1998
5    Copyright (C) Stefan (metze) Metzmacher      2003
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 extern int Protocol;
25 extern int smb_read_error;
26 extern int global_oplock_break;
27 extern BOOL case_sensitive;
28 extern BOOL case_preserve;
29 extern BOOL short_case_preserve;
30 extern struct current_user current_user;
31
32 static const char *known_nt_pipes[] = {
33         "\\LANMAN",
34         "\\srvsvc",
35         "\\samr",
36         "\\wkssvc",
37         "\\NETLOGON",
38         "\\ntlsa",
39         "\\ntsvcs",
40         "\\lsass",
41         "\\lsarpc",
42         "\\winreg",
43         "\\spoolss",
44         "\\netdfs",
45         "\\rpcecho",
46         NULL
47 };
48
49 /* Map generic permissions to file object specific permissions */
50  
51 struct generic_mapping file_generic_mapping = {
52         FILE_GENERIC_READ,
53         FILE_GENERIC_WRITE,
54         FILE_GENERIC_EXECUTE,
55         FILE_GENERIC_ALL
56 };
57
58 static char *nttrans_realloc(char **ptr, size_t size)
59 {
60         char *tptr = NULL;
61         if (ptr==NULL)
62                 smb_panic("nttrans_realloc() called with NULL ptr\n");
63                 
64         tptr = Realloc_zero(*ptr, size);
65         if(tptr == NULL) {
66                 *ptr = NULL;
67                 return NULL;
68         }
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(uint32 file_attributes)
278 {
279         if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
280                 return;
281
282         saved_case_sensitive = case_sensitive;
283         saved_case_preserve = case_preserve;
284         saved_short_case_preserve = short_case_preserve;
285
286         /* Set to POSIX. */
287         case_sensitive = True;
288         case_preserve = True;
289         short_case_preserve = True;
290 }
291
292 /****************************************************************************
293  Restore case semantics.
294 ****************************************************************************/
295
296 static void restore_case_semantics(uint32 file_attributes)
297 {
298         if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
299                 return;
300
301         case_sensitive = saved_case_sensitive;
302         case_preserve = saved_case_preserve;
303         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         /*
359          * Convert GENERIC bits to specific bits.
360          */
361
362         se_map_generic(desired_access, &file_generic_mapping);
363
364         switch( *desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
365                 case FILE_READ_DATA:
366                         smb_open_mode = DOS_OPEN_RDONLY;
367                         break;
368                 case FILE_WRITE_DATA:
369                 case FILE_APPEND_DATA:
370                 case FILE_WRITE_DATA|FILE_APPEND_DATA:
371                         smb_open_mode = DOS_OPEN_WRONLY;
372                         break;
373                 case FILE_READ_DATA|FILE_WRITE_DATA:
374                 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
375                 case FILE_READ_DATA|FILE_APPEND_DATA:
376                         smb_open_mode = DOS_OPEN_RDWR;
377                         break;
378         }
379
380         /*
381          * NB. For DELETE_ACCESS we should really check the
382          * directory permissions, as that is what controls
383          * delete, and for WRITE_DAC_ACCESS we should really
384          * check the ownership, as that is what controls the
385          * chmod. Note that this is *NOT* a security hole (this
386          * note is for you, Andrew) as we are not *allowing*
387          * the access at this point, the actual unlink or
388          * chown or chmod call would do this. We are just helping
389          * clients out by telling them if they have a hope
390          * of any of this succeeding. POSIX acls may still
391          * deny the real call. JRA.
392          */
393
394         if (smb_open_mode == -1) {
395
396                 if(*desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS|
397                                         FILE_EXECUTE|FILE_READ_ATTRIBUTES|
398                                         FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
399                                         FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
400                         smb_open_mode = DOS_OPEN_RDONLY;
401                 } else if(*desired_access == 0) {
402
403                         /* 
404                          * JRA - NT seems to sometimes send desired_access as zero. play it safe
405                          * and map to a stat open.
406                          */
407
408                         smb_open_mode = DOS_OPEN_RDONLY;
409
410                 } else {
411                         DEBUG(0,("map_share_mode: Incorrect value 0x%lx for desired_access to file %s\n",
412                                 (unsigned long)*desired_access, fname));
413                         return -1;
414                 }
415         }
416
417         /*
418          * Set the special bit that means allow share delete.
419          * This is held outside the normal share mode bits at 1<<15.
420          * JRA.
421          */
422
423         if(share_access & FILE_SHARE_DELETE) {
424                 smb_open_mode |= ALLOW_SHARE_DELETE;
425                 DEBUG(10,("map_share_mode: FILE_SHARE_DELETE requested. open_mode = 0x%x\n", smb_open_mode));
426         }
427
428         if(*desired_access & DELETE_ACCESS) {
429                 DEBUG(10,("map_share_mode: DELETE_ACCESS requested. open_mode = 0x%x\n", smb_open_mode));
430         }
431
432         /*
433          * We need to store the intent to open for Delete. This
434          * is what determines if a delete on close flag can be set.
435          * This is the wrong way (and place) to store this, but for 2.2 this
436          * is the only practical way. JRA.
437          */
438
439         if (create_options & FILE_DELETE_ON_CLOSE) {
440                 /*
441                  * W2K3 bug compatibility mode... To set delete on close
442                  * the redirector must have *specifically* set DELETE_ACCESS
443                  * in the desired_access field. Just asking for GENERIC_ALL won't do. JRA.
444                  */
445
446                 if (!(original_desired_access & DELETE_ACCESS)) {
447                         DEBUG(5,("map_share_mode: FILE_DELETE_ON_CLOSE requested without \
448 DELETE_ACCESS for file %s. (desired_access = 0x%lx)\n",
449                                 fname, (unsigned long)*desired_access));
450                         return -1;
451                 }
452                 /* Implicit delete access is *NOT* requested... */
453                 smb_open_mode |= DELETE_ON_CLOSE_FLAG;
454                 DEBUG(10,("map_share_mode: FILE_DELETE_ON_CLOSE requested. open_mode = 0x%x\n", smb_open_mode));
455         }
456
457         /* Add in the requested share mode. */
458         switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
459                 case FILE_SHARE_READ:
460                         smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
461                         break;
462                 case FILE_SHARE_WRITE:
463                         smb_open_mode |= SET_DENY_MODE(DENY_READ);
464                         break;
465                 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
466                         smb_open_mode |= SET_DENY_MODE(DENY_NONE);
467                         break;
468                 case FILE_SHARE_NONE:
469                         smb_open_mode |= SET_DENY_MODE(DENY_ALL);
470                         break;
471         }
472
473         /*
474          * Handle an O_SYNC request.
475          */
476
477         if(file_attributes & FILE_FLAG_WRITE_THROUGH)
478                 smb_open_mode |= FILE_SYNC_OPENMODE;
479
480         DEBUG(10,("map_share_mode: Mapped desired access 0x%lx, share access 0x%lx, file attributes 0x%lx \
481 to open_mode 0x%x\n", (unsigned long)*desired_access, (unsigned long)share_access,
482                 (unsigned long)file_attributes, smb_open_mode ));
483  
484         return smb_open_mode;
485 }
486
487 /****************************************************************************
488  Reply to an NT create and X call on a pipe.
489 ****************************************************************************/
490
491 static int nt_open_pipe(char *fname, connection_struct *conn,
492                         char *inbuf, char *outbuf, int *ppnum)
493 {
494         smb_np_struct *p = NULL;
495
496         uint16 vuid = SVAL(inbuf, smb_uid);
497         int i;
498
499         DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
500     
501         /* See if it is one we want to handle. */
502
503         if (lp_disable_spoolss() && strequal(fname, "\\spoolss"))
504                 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
505
506         for( i = 0; known_nt_pipes[i]; i++ )
507                 if( strequal(fname,known_nt_pipes[i]))
508                         break;
509     
510         if ( known_nt_pipes[i] == NULL )
511                 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
512     
513         /* Strip \\ off the name. */
514         fname++;
515     
516         DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
517
518         p = open_rpc_pipe_p(fname, conn, vuid);
519         if (!p)
520                 return(ERROR_DOS(ERRSRV,ERRnofids));
521
522         *ppnum = p->pnum;
523
524         return 0;
525 }
526
527 /****************************************************************************
528  Reply to an NT create and X call for pipes.
529 ****************************************************************************/
530
531 static int do_ntcreate_pipe_open(connection_struct *conn,
532                          char *inbuf,char *outbuf,int length,int bufsize)
533 {
534         pstring fname;
535         int ret;
536         int pnum = -1;
537         char *p = NULL;
538         NTSTATUS status;
539
540         srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
541         if (!NT_STATUS_IS_OK(status))
542                 return ERROR_NT(status);
543
544         if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
545                 return ret;
546
547         /*
548          * Deal with pipe return.
549          */  
550
551         set_message(outbuf,34,0,True);
552
553         p = outbuf + smb_vwv2;
554         p++;
555         SSVAL(p,0,pnum);
556         p += 2;
557         SIVAL(p,0,FILE_WAS_OPENED);
558         p += 4;
559         p += 32;
560         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
561         p += 20;
562         /* File type. */
563         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
564         /* Device state. */
565         SSVAL(p,2, 0x5FF); /* ? */
566
567         DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
568
569         return chain_reply(inbuf,outbuf,length,bufsize);
570 }
571
572 /****************************************************************************
573  Reply to an NT create and X call.
574 ****************************************************************************/
575
576 int reply_ntcreate_and_X(connection_struct *conn,
577                          char *inbuf,char *outbuf,int length,int bufsize)
578 {  
579         int result;
580         pstring fname;
581         enum FAKE_FILE_TYPE fake_file_type = FAKE_FILE_TYPE_NONE;
582         uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
583         uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
584         uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
585         uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
586         uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
587         uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
588         uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
589         SMB_BIG_UINT allocation_size = 0;
590         int smb_ofun;
591         int smb_open_mode;
592         int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
593         /* Breakout the oplock request bits so we can set the
594            reply bits separately. */
595         int oplock_request = 0;
596         mode_t unixmode;
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_DOS(ERRDOS,ERRnoaccess));
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                 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
652                 size_t dir_name_len;
653
654                 if(!dir_fsp) {
655                         END_PROFILE(SMBntcreateX);
656                         return(ERROR_DOS(ERRDOS,ERRbadfid));
657                 }
658
659                 if(!dir_fsp->is_directory) {
660
661                         srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
662                         if (!NT_STATUS_IS_OK(status)) {
663                                 END_PROFILE(SMBntcreateX);
664                                 return ERROR_NT(status);
665                         }
666
667                         /* 
668                          * Check to see if this is a mac fork of some kind.
669                          */
670
671                         if( strchr_m(fname, ':')) {
672                                 END_PROFILE(SMBntcreateX);
673                                 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
674                         }
675
676                         /*
677                           we need to handle the case when we get a
678                           relative open relative to a file and the
679                           pathname is blank - this is a reopen!
680                           (hint from demyn plantenberg)
681                         */
682
683                         END_PROFILE(SMBntcreateX);
684                         return(ERROR_DOS(ERRDOS,ERRbadfid));
685                 }
686
687                 /*
688                  * Copy in the base directory name.
689                  */
690
691                 pstrcpy( fname, dir_fsp->fsp_name );
692                 dir_name_len = strlen(fname);
693
694                 /*
695                  * Ensure it ends in a '\'.
696                  */
697
698                 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
699                         pstrcat(fname, "\\");
700                         dir_name_len++;
701                 }
702
703                 srvstr_get_path(inbuf, &fname[dir_name_len], smb_buf(inbuf), sizeof(fname)-dir_name_len, 0, STR_TERMINATE, &status);
704                 if (!NT_STATUS_IS_OK(status)) {
705                         END_PROFILE(SMBntcreateX);
706                         return ERROR_NT(status);
707                 }
708         } else {
709                 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
710                 if (!NT_STATUS_IS_OK(status)) {
711                         END_PROFILE(SMBntcreateX);
712                         return ERROR_NT(status);
713                 }
714
715                 /* 
716                  * Check to see if this is a mac fork of some kind.
717                  */
718
719                 if( strchr_m(fname, ':')) {
720                         
721 #ifdef HAVE_SYS_QUOTAS
722                         if ((fake_file_type=is_fake_file(fname))!=FAKE_FILE_TYPE_NONE) {
723                                 /*
724                                  * here we go! support for changing the disk quotas --metze
725                                  *
726                                  * we need to fake up to open this MAGIC QUOTA file 
727                                  * and return a valid FID
728                                  *
729                                  * w2k close this file directly after openening
730                                  * xp also tries a QUERY_FILE_INFO on the file and then close it
731                                  */
732                         } else {
733 #endif
734                                 END_PROFILE(SMBntcreateX);
735                                 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
736 #ifdef HAVE_SYS_QUOTAS
737                         }
738 #endif
739                 }
740         }
741         
742         /*
743          * Now contruct the smb_open_mode value from the filename, 
744          * desired access and the share access.
745          */
746         RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
747
748         if((smb_open_mode = map_share_mode(fname, create_options, &desired_access, 
749                                            share_access, 
750                                            file_attributes)) == -1) {
751                 END_PROFILE(SMBntcreateX);
752                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
753         }
754
755         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
756         if (oplock_request) {
757                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
758         }
759
760         /*
761          * Ordinary file or directory.
762          */
763                 
764         /*
765          * Check if POSIX semantics are wanted.
766          */
767                 
768         set_posix_case_semantics(file_attributes);
769                 
770         unix_convert(fname,conn,0,&bad_path,&sbuf);
771                 
772         unixmode = unix_mode(conn,smb_attr | aARCH, fname);
773     
774         /* 
775          * If it's a request for a directory open, deal with it separately.
776          */
777
778         if(create_options & FILE_DIRECTORY_FILE) {
779                 oplock_request = 0;
780                 
781                 /* Can't open a temp directory. IFS kit test. */
782                 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
783                         END_PROFILE(SMBntcreateX);
784                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
785                 }
786
787                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
788                         
789                 restore_case_semantics(file_attributes);
790
791                 if(!fsp) {
792                         END_PROFILE(SMBntcreateX);
793                         return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
794                 }
795         } else {
796                 /*
797                  * Ordinary file case.
798                  */
799
800                 /* NB. We have a potential bug here. If we
801                  * cause an oplock break to ourselves, then we
802                  * could end up processing filename related
803                  * SMB requests whilst we await the oplock
804                  * break response. As we may have changed the
805                  * filename case semantics to be POSIX-like,
806                  * this could mean a filename request could
807                  * fail when it should succeed. This is a rare
808                  * condition, but eventually we must arrange
809                  * to restore the correct case semantics
810                  * before issuing an oplock break request to
811                  * our client. JRA.  */
812
813                 if (fake_file_type==FAKE_FILE_TYPE_NONE) {
814                         fsp = open_file_shared1(conn,fname,&sbuf,
815                                         desired_access,
816                                         smb_open_mode,
817                                         smb_ofun,unixmode, oplock_request,
818                                         &rmode,&smb_action);
819                 } else {
820                         /* to open a fake_file --metze */
821                         fsp = open_fake_file_shared1(fake_file_type,conn,fname,&sbuf,
822                                         desired_access,
823                                         smb_open_mode,
824                                         smb_ofun,unixmode, oplock_request,
825                                         &rmode,&smb_action);
826                 }
827                 
828                 if (!fsp) { 
829                         /* We cheat here. There are two cases we
830                          * care about. One is a directory rename,
831                          * where the NT client will attempt to
832                          * open the source directory for
833                          * DELETE access. Note that when the
834                          * NT client does this it does *not*
835                          * set the directory bit in the
836                          * request packet. This is translated
837                          * into a read/write open
838                          * request. POSIX states that any open
839                          * for write request on a directory
840                          * will generate an EISDIR error, so
841                          * we can catch this here and open a
842                          * pseudo handle that is flagged as a
843                          * directory. The second is an open
844                          * for a permissions read only, which
845                          * we handle in the open_file_stat case. JRA.
846                          */
847
848                         if(errno == EISDIR) {
849
850                                 /*
851                                  * Fail the open if it was explicitly a non-directory file.
852                                  */
853
854                                 if (create_options & FILE_NON_DIRECTORY_FILE) {
855                                         restore_case_semantics(file_attributes);
856                                         SSVAL(outbuf, smb_flg2, 
857                                               SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
858                                         END_PROFILE(SMBntcreateX);
859                                         return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
860                                 }
861         
862                                 oplock_request = 0;
863                                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
864                                 
865                                 if(!fsp) {
866                                         restore_case_semantics(file_attributes);
867                                         END_PROFILE(SMBntcreateX);
868                                         return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
869                                 }
870                         } else {
871
872                                 restore_case_semantics(file_attributes);
873                                 END_PROFILE(SMBntcreateX);
874                                 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
875                         }
876                 } 
877         }
878                 
879         restore_case_semantics(file_attributes);
880                 
881         file_len = sbuf.st_size;
882         fmode = dos_mode(conn,fname,&sbuf);
883         if(fmode == 0)
884                 fmode = FILE_ATTRIBUTE_NORMAL;
885         if (!fsp->is_directory && (fmode & aDIR)) {
886                 close_file(fsp,False);
887                 END_PROFILE(SMBntcreateX);
888                 return ERROR_DOS(ERRDOS,ERRnoaccess);
889         } 
890         
891         /* Save the requested allocation size. */
892         allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize);
893 #ifdef LARGE_SMB_OFF_T
894         allocation_size |= (((SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
895 #endif
896         if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
897                 fsp->initial_allocation_size = SMB_ROUNDUP(allocation_size,SMB_ROUNDUP_ALLOCATION_SIZE);
898                 if (fsp->is_directory) {
899                         close_file(fsp,False);
900                         END_PROFILE(SMBntcreateX);
901                         /* Can't set allocation size on a directory. */
902                         return ERROR_NT(NT_STATUS_ACCESS_DENIED);
903                 }
904                 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
905                         close_file(fsp,False);
906                         END_PROFILE(SMBntcreateX);
907                         return ERROR_NT(NT_STATUS_DISK_FULL);
908                 }
909         } else {
910                 fsp->initial_allocation_size = SMB_ROUNDUP(((SMB_BIG_UINT)file_len),SMB_ROUNDUP_ALLOCATION_SIZE);
911         }
912
913         /* 
914          * If the caller set the extended oplock request bit
915          * and we granted one (by whatever means) - set the
916          * correct bit for extended oplock reply.
917          */
918         
919         if (oplock_request && lp_fake_oplocks(SNUM(conn)))
920                 extended_oplock_granted = True;
921         
922         if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
923                 extended_oplock_granted = True;
924
925 #if 0
926         /* W2K sends back 42 words here ! If we do the same it breaks offline sync. Go figure... ? JRA. */
927         set_message(outbuf,42,0,True);
928 #else
929         set_message(outbuf,34,0,True);
930 #endif
931         
932         p = outbuf + smb_vwv2;
933         
934         /*
935          * Currently as we don't support level II oplocks we just report
936          * exclusive & batch here.
937          */
938
939         if (extended_oplock_granted) {
940                 if (flags & REQUEST_BATCH_OPLOCK) {
941                         SCVAL(p,0, BATCH_OPLOCK_RETURN);
942                 } else {
943                         SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
944                 }
945         } else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
946                 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
947         } else {
948                 SCVAL(p,0,NO_OPLOCK_RETURN);
949         }
950         
951         p++;
952         SSVAL(p,0,fsp->fnum);
953         p += 2;
954         if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
955                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
956         else
957                 SIVAL(p,0,smb_action);
958         p += 4;
959         
960         /* Create time. */  
961         c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
962
963         if (lp_dos_filetime_resolution(SNUM(conn))) {
964                 c_time &= ~1;
965                 sbuf.st_atime &= ~1;
966                 sbuf.st_mtime &= ~1;
967                 sbuf.st_mtime &= ~1;
968         }
969
970         put_long_date(p,c_time);
971         p += 8;
972         put_long_date(p,sbuf.st_atime); /* access time */
973         p += 8;
974         put_long_date(p,sbuf.st_mtime); /* write time */
975         p += 8;
976         put_long_date(p,sbuf.st_mtime); /* change time */
977         p += 8;
978         SIVAL(p,0,fmode); /* File Attributes. */
979         p += 4;
980         SOFF_T(p, 0, get_allocation_size(fsp,&sbuf));
981         p += 8;
982         SOFF_T(p,0,file_len);
983         p += 8;
984         if (flags & EXTENDED_RESPONSE_REQUIRED)
985                 SSVAL(p,2,0x7);
986         p += 4;
987         SCVAL(p,0,fsp->is_directory ? 1 : 0);
988
989         DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
990
991         result = chain_reply(inbuf,outbuf,length,bufsize);
992         END_PROFILE(SMBntcreateX);
993         return result;
994 }
995
996 /****************************************************************************
997  Reply to a NT_TRANSACT_CREATE call to open a pipe.
998 ****************************************************************************/
999
1000 static int do_nt_transact_create_pipe( connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1001                                   char **ppsetup, uint32 setup_count,
1002                                   char **ppparams, uint32 parameter_count,
1003                                   char **ppdata, uint32 data_count)
1004 {
1005         pstring fname;
1006         char *params = *ppparams;
1007         int ret;
1008         int pnum = -1;
1009         char *p = NULL;
1010         NTSTATUS status;
1011
1012         /*
1013          * Ensure minimum number of parameters sent.
1014          */
1015
1016         if(parameter_count < 54) {
1017                 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1018                 return ERROR_DOS(ERRDOS,ERRnoaccess);
1019         }
1020
1021         srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1022         if (!NT_STATUS_IS_OK(status)) {
1023                 return ERROR_NT(status);
1024         }
1025
1026         if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
1027                 return ret;
1028         
1029         /* Realloc the size of parameters and data we will return */
1030         params = nttrans_realloc(ppparams, 69);
1031         if(params == NULL)
1032                 return ERROR_DOS(ERRDOS,ERRnomem);
1033         
1034         p = params;
1035         SCVAL(p,0,NO_OPLOCK_RETURN);
1036         
1037         p += 2;
1038         SSVAL(p,0,pnum);
1039         p += 2;
1040         SIVAL(p,0,FILE_WAS_OPENED);
1041         p += 8;
1042         
1043         p += 32;
1044         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1045         p += 20;
1046         /* File type. */
1047         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1048         /* Device state. */
1049         SSVAL(p,2, 0x5FF); /* ? */
1050         
1051         DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1052         
1053         /* Send the required number of replies */
1054         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1055         
1056         return -1;
1057 }
1058
1059 /****************************************************************************
1060  Internal fn to set security descriptors.
1061 ****************************************************************************/
1062
1063 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1064 {
1065         prs_struct pd;
1066         SEC_DESC *psd = NULL;
1067         TALLOC_CTX *mem_ctx;
1068         BOOL ret;
1069         
1070         if (sd_len == 0) {
1071                 return NT_STATUS_OK;
1072         }
1073
1074         /*
1075          * Init the parse struct we will unmarshall from.
1076          */
1077
1078         if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1079                 DEBUG(0,("set_sd: talloc_init failed.\n"));
1080                 return NT_STATUS_NO_MEMORY;
1081         }
1082
1083         prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1084
1085         /*
1086          * Setup the prs_struct to point at the memory we just
1087          * allocated.
1088          */
1089         
1090         prs_give_memory( &pd, data, sd_len, False);
1091
1092         /*
1093          * Finally, unmarshall from the data buffer.
1094          */
1095
1096         if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1097                 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1098                 /*
1099                  * Return access denied for want of a better error message..
1100                  */ 
1101                 talloc_destroy(mem_ctx);
1102                 return NT_STATUS_NO_MEMORY;
1103         }
1104         
1105         if (psd->off_owner_sid==0)
1106                 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1107         if (psd->off_grp_sid==0)
1108                 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1109         if (psd->off_sacl==0)
1110                 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1111         if (psd->off_dacl==0)
1112                 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1113         
1114         ret = SMB_VFS_FSET_NT_ACL( fsp, fsp->fd, security_info_sent, psd);
1115         
1116         if (!ret) {
1117                 talloc_destroy(mem_ctx);
1118                 return NT_STATUS_ACCESS_DENIED;
1119         }
1120         
1121         talloc_destroy(mem_ctx);
1122         
1123         return NT_STATUS_OK;
1124 }
1125
1126 /****************************************************************************
1127  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1128 ****************************************************************************/
1129
1130 static int call_nt_transact_create(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1131                                   char **ppsetup, uint32 setup_count,
1132                                   char **ppparams, uint32 parameter_count,
1133                                   char **ppdata, uint32 data_count)
1134 {
1135         pstring fname;
1136         char *params = *ppparams;
1137         char *data = *ppdata;
1138         /* Breakout the oplock request bits so we can set the reply bits separately. */
1139         int oplock_request = 0;
1140         mode_t unixmode;
1141         int fmode=0,rmode=0;
1142         SMB_OFF_T file_len = 0;
1143         SMB_STRUCT_STAT sbuf;
1144         int smb_action = 0;
1145         BOOL bad_path = False;
1146         files_struct *fsp = NULL;
1147         char *p = NULL;
1148         BOOL extended_oplock_granted = False;
1149         uint32 flags;
1150         uint32 desired_access;
1151         uint32 file_attributes;
1152         uint32 share_access;
1153         uint32 create_disposition;
1154         uint32 create_options;
1155         uint32 sd_len;
1156         uint16 root_dir_fid;
1157         SMB_BIG_UINT allocation_size = 0;
1158         int smb_ofun;
1159         int smb_open_mode;
1160         int smb_attr;
1161         time_t c_time;
1162         NTSTATUS status;
1163
1164         DEBUG(5,("call_nt_transact_create\n"));
1165
1166         /*
1167          * If it's an IPC, use the pipe handler.
1168          */
1169
1170         if (IS_IPC(conn)) {
1171                 if (lp_nt_pipe_support())
1172                         return do_nt_transact_create_pipe(conn, inbuf, outbuf, length, 
1173                                         bufsize,
1174                                         ppsetup, setup_count,
1175                                         ppparams, parameter_count,
1176                                         ppdata, data_count);
1177                 else
1178                         return ERROR_DOS(ERRDOS,ERRnoaccess);
1179         }
1180
1181         /*
1182          * Ensure minimum number of parameters sent.
1183          */
1184
1185         if(parameter_count < 54) {
1186                 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1187                 return ERROR_DOS(ERRDOS,ERRnoaccess);
1188         }
1189
1190         flags = IVAL(params,0);
1191         desired_access = IVAL(params,8);
1192         file_attributes = IVAL(params,20);
1193         share_access = IVAL(params,24);
1194         create_disposition = IVAL(params,28);
1195         create_options = IVAL(params,32);
1196         sd_len = IVAL(params,36);
1197         root_dir_fid = (uint16)IVAL(params,4);
1198         smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1199
1200         if (create_options & FILE_OPEN_BY_FILE_ID) {
1201                 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1202         }
1203
1204         /* 
1205          * We need to construct the open_and_X ofun value from the
1206          * NT values, as that's what our code is structured to accept.
1207          */    
1208
1209         if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1210                 return ERROR_DOS(ERRDOS,ERRbadmem);
1211
1212         /*
1213          * Get the file name.
1214          */
1215
1216         if(root_dir_fid != 0) {
1217                 /*
1218                  * This filename is relative to a directory fid.
1219                  */
1220
1221                 files_struct *dir_fsp = file_fsp(params,4);
1222                 size_t dir_name_len;
1223
1224                 if(!dir_fsp)
1225                         return ERROR_DOS(ERRDOS,ERRbadfid);
1226
1227                 if(!dir_fsp->is_directory) {
1228                         srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1229                         if (!NT_STATUS_IS_OK(status)) {
1230                                 return ERROR_NT(status);
1231                         }
1232
1233                         /*
1234                          * Check to see if this is a mac fork of some kind.
1235                          */
1236
1237                         if( strchr_m(fname, ':'))
1238                                 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1239
1240                         return ERROR_DOS(ERRDOS,ERRbadfid);
1241                 }
1242
1243                 /*
1244                  * Copy in the base directory name.
1245                  */
1246
1247                 pstrcpy( fname, dir_fsp->fsp_name );
1248                 dir_name_len = strlen(fname);
1249
1250                 /*
1251                  * Ensure it ends in a '\'.
1252                  */
1253
1254                 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1255                         pstrcat(fname, "\\");
1256                         dir_name_len++;
1257                 }
1258
1259                 {
1260                         pstring tmpname;
1261                         srvstr_get_path(inbuf, tmpname, params+53, sizeof(tmpname), parameter_count-53, STR_TERMINATE, &status);
1262                         if (!NT_STATUS_IS_OK(status)) {
1263                                 return ERROR_NT(status);
1264                         }
1265                         pstrcat(fname, tmpname);
1266                 }
1267         } else {
1268                 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1269                 if (!NT_STATUS_IS_OK(status)) {
1270                         return ERROR_NT(status);
1271                 }
1272
1273                 /*
1274                  * Check to see if this is a mac fork of some kind.
1275                  */
1276
1277                 if( strchr_m(fname, ':'))
1278                         return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1279         }
1280
1281         /*
1282          * Now contruct the smb_open_mode value from the desired access
1283          * and the share access.
1284          */
1285
1286         if((smb_open_mode = map_share_mode( fname, create_options, &desired_access,
1287                                                 share_access, file_attributes)) == -1)
1288                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1289
1290         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1291         oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1292
1293         /*
1294          * Check if POSIX semantics are wanted.
1295          */
1296
1297         set_posix_case_semantics(file_attributes);
1298     
1299         RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1300
1301         unix_convert(fname,conn,0,&bad_path,&sbuf);
1302     
1303         unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1304    
1305         /*
1306          * If it's a request for a directory open, deal with it separately.
1307          */
1308
1309         if(create_options & FILE_DIRECTORY_FILE) {
1310
1311                 /* Can't open a temp directory. IFS kit test. */
1312                 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1313                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1314                 }
1315
1316                 oplock_request = 0;
1317
1318                 /*
1319                  * We will get a create directory here if the Win32
1320                  * app specified a security descriptor in the 
1321                  * CreateDirectory() call.
1322                  */
1323
1324                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1325
1326                 if(!fsp) {
1327                         restore_case_semantics(file_attributes);
1328                         return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1329                 }
1330
1331         } else {
1332
1333                 /*
1334                  * Ordinary file case.
1335                  */
1336
1337                 fsp = open_file_shared1(conn,fname,&sbuf,desired_access,
1338                                                 smb_open_mode,smb_ofun,unixmode,
1339                                                 oplock_request,&rmode,&smb_action);
1340
1341                 if (!fsp) { 
1342
1343                         if(errno == EISDIR) {
1344
1345                                 /*
1346                                  * Fail the open if it was explicitly a non-directory file.
1347                                  */
1348
1349                                 if (create_options & FILE_NON_DIRECTORY_FILE) {
1350                                         restore_case_semantics(file_attributes);
1351                                         SSVAL(outbuf, smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1352                                         return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1353                                 }
1354         
1355                                 oplock_request = 0;
1356                                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1357                                 
1358                                 if(!fsp) {
1359                                         restore_case_semantics(file_attributes);
1360                                         return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1361                                 }
1362                         } else {
1363                                 restore_case_semantics(file_attributes);
1364                                 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1365                         }
1366                 } 
1367   
1368                 file_len = sbuf.st_size;
1369                 fmode = dos_mode(conn,fname,&sbuf);
1370                 if(fmode == 0)
1371                         fmode = FILE_ATTRIBUTE_NORMAL;
1372
1373                 if (fmode & aDIR) {
1374                         close_file(fsp,False);
1375                         restore_case_semantics(file_attributes);
1376                         return ERROR_DOS(ERRDOS,ERRnoaccess);
1377                 } 
1378
1379                 /* 
1380                  * If the caller set the extended oplock request bit
1381                  * and we granted one (by whatever means) - set the
1382                  * correct bit for extended oplock reply.
1383                  */
1384     
1385                 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1386                         extended_oplock_granted = True;
1387   
1388                 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1389                         extended_oplock_granted = True;
1390         }
1391
1392         /*
1393          * Now try and apply the desired SD.
1394          */
1395
1396         if (sd_len && !NT_STATUS_IS_OK(status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION))) {
1397                 close_file(fsp,False);
1398                 restore_case_semantics(file_attributes);
1399                 return ERROR_NT(status);
1400         }
1401         
1402         restore_case_semantics(file_attributes);
1403
1404         /* Save the requested allocation size. */
1405         allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1406 #ifdef LARGE_SMB_OFF_T
1407         allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1408 #endif
1409         if (allocation_size && (allocation_size > file_len)) {
1410                 fsp->initial_allocation_size = SMB_ROUNDUP(allocation_size,SMB_ROUNDUP_ALLOCATION_SIZE);
1411                 if (fsp->is_directory) {
1412                         close_file(fsp,False);
1413                         END_PROFILE(SMBntcreateX);
1414                         /* Can't set allocation size on a directory. */
1415                         return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1416                 }
1417                 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1418                         close_file(fsp,False);
1419                         return ERROR_NT(NT_STATUS_DISK_FULL);
1420                 }
1421         } else {
1422                 fsp->initial_allocation_size = SMB_ROUNDUP(((SMB_BIG_UINT)file_len),SMB_ROUNDUP_ALLOCATION_SIZE);
1423         }
1424
1425         /* Realloc the size of parameters and data we will return */
1426         params = nttrans_realloc(ppparams, 69);
1427         if(params == NULL)
1428                 return ERROR_DOS(ERRDOS,ERRnomem);
1429
1430         p = params;
1431         if (extended_oplock_granted)
1432                 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1433         else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1434                 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1435         else
1436                 SCVAL(p,0,NO_OPLOCK_RETURN);
1437         
1438         p += 2;
1439         SSVAL(p,0,fsp->fnum);
1440         p += 2;
1441         if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
1442                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1443         else
1444                 SIVAL(p,0,smb_action);
1445         p += 8;
1446
1447         /* Create time. */
1448         c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1449
1450         if (lp_dos_filetime_resolution(SNUM(conn))) {
1451                 c_time &= ~1;
1452                 sbuf.st_atime &= ~1;
1453                 sbuf.st_mtime &= ~1;
1454                 sbuf.st_mtime &= ~1;
1455         }
1456
1457         put_long_date(p,c_time);
1458         p += 8;
1459         put_long_date(p,sbuf.st_atime); /* access time */
1460         p += 8;
1461         put_long_date(p,sbuf.st_mtime); /* write time */
1462         p += 8;
1463         put_long_date(p,sbuf.st_mtime); /* change time */
1464         p += 8;
1465         SIVAL(p,0,fmode); /* File Attributes. */
1466         p += 4;
1467         SOFF_T(p, 0, get_allocation_size(fsp,&sbuf));
1468         p += 8;
1469         SOFF_T(p,0,file_len);
1470         p += 8;
1471         if (flags & EXTENDED_RESPONSE_REQUIRED)
1472                 SSVAL(p,2,0x7);
1473         p += 4;
1474         SCVAL(p,0,fsp->is_directory ? 1 : 0);
1475
1476         DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1477
1478         /* Send the required number of replies */
1479         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1480
1481         return -1;
1482 }
1483
1484 /****************************************************************************
1485  Reply to a NT CANCEL request.
1486 ****************************************************************************/
1487
1488 int reply_ntcancel(connection_struct *conn,
1489                    char *inbuf,char *outbuf,int length,int bufsize)
1490 {
1491         /*
1492          * Go through and cancel any pending change notifies.
1493          */
1494         
1495         int mid = SVAL(inbuf,smb_mid);
1496         START_PROFILE(SMBntcancel);
1497         remove_pending_change_notify_requests_by_mid(mid);
1498         remove_pending_lock_requests_by_mid(mid);
1499         srv_cancel_sign_response(mid);
1500         
1501         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1502
1503         END_PROFILE(SMBntcancel);
1504         return(-1);
1505 }
1506
1507 /****************************************************************************
1508  Reply to a NT rename request.
1509 ****************************************************************************/
1510
1511 int reply_ntrename(connection_struct *conn,
1512                    char *inbuf,char *outbuf,int length,int bufsize)
1513 {
1514         int outsize = 0;
1515         pstring name;
1516         pstring newname;
1517         char *p;
1518         NTSTATUS status;
1519         uint16 attrs = SVAL(inbuf,smb_vwv0);
1520         uint16 rename_type = SVAL(inbuf,smb_vwv1);
1521
1522         START_PROFILE(SMBntrename);
1523
1524         if (rename_type != RENAME_FLAG_RENAME) {
1525                 END_PROFILE(SMBntrename);
1526                 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1527         }
1528
1529         p = smb_buf(inbuf) + 1;
1530         p += srvstr_get_path(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status);
1531         if (!NT_STATUS_IS_OK(status)) {
1532                 END_PROFILE(SMBntrename);
1533                 return ERROR_NT(status);
1534         }
1535
1536         if( strchr_m(name, ':')) {
1537                 /* Can't rename a stream. */
1538                 END_PROFILE(SMBntrename);
1539                 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1540         }
1541
1542         p++;
1543         p += srvstr_get_path(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status);
1544         if (!NT_STATUS_IS_OK(status)) {
1545                 END_PROFILE(SMBntrename);
1546                 return ERROR_NT(status);
1547         }
1548         
1549         RESOLVE_DFSPATH(name, conn, inbuf, outbuf);
1550         RESOLVE_DFSPATH(newname, conn, inbuf, outbuf);
1551         
1552         DEBUG(3,("reply_ntrename : %s -> %s\n",name,newname));
1553         
1554         status = rename_internals(conn, name, newname, attrs, False);
1555         if (!NT_STATUS_IS_OK(status)) {
1556                 END_PROFILE(SMBntrename);
1557                 return ERROR_NT(status);
1558         }
1559
1560         /*
1561          * Win2k needs a changenotify request response before it will
1562          * update after a rename..
1563          */     
1564         process_pending_change_notify_queue((time_t)0);
1565         outsize = set_message(outbuf,0,0,True);
1566   
1567         END_PROFILE(SMBntrename);
1568         return(outsize);
1569 }
1570
1571 /****************************************************************************
1572  Reply to an unsolicited SMBNTtranss - just ignore it!
1573 ****************************************************************************/
1574
1575 int reply_nttranss(connection_struct *conn,
1576                    char *inbuf,char *outbuf,int length,int bufsize)
1577 {
1578         START_PROFILE(SMBnttranss);
1579         DEBUG(4,("Ignoring nttranss of length %d\n",length));
1580         END_PROFILE(SMBnttranss);
1581         return(-1);
1582 }
1583
1584 /****************************************************************************
1585  Reply to a notify change - queue the request and 
1586  don't allow a directory to be opened.
1587 ****************************************************************************/
1588
1589 static int call_nt_transact_notify_change(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
1590                                   char **ppsetup, uint32 setup_count,
1591                                   char **ppparams, uint32 parameter_count,
1592                                   char **ppdata, uint32 data_count)
1593 {
1594         char *setup = *ppsetup;
1595         files_struct *fsp;
1596         uint32 flags;
1597
1598         if(setup_count < 6)
1599                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1600
1601         fsp = file_fsp(setup,4);
1602         flags = IVAL(setup, 0);
1603
1604         DEBUG(3,("call_nt_transact_notify_change\n"));
1605
1606         if(!fsp)
1607                 return ERROR_DOS(ERRDOS,ERRbadfid);
1608
1609         if((!fsp->is_directory) || (conn != fsp->conn))
1610                 return ERROR_DOS(ERRDOS,ERRbadfid);
1611
1612         if (!change_notify_set(inbuf, fsp, conn, flags))
1613                 return(UNIXERROR(ERRDOS,ERRbadfid));
1614
1615         DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1616 name = %s\n", fsp->fsp_name ));
1617
1618         return -1;
1619 }
1620
1621 /****************************************************************************
1622  Reply to an NT transact rename command.
1623 ****************************************************************************/
1624
1625 static int call_nt_transact_rename(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1626                                   char **ppsetup, uint32 setup_count,
1627                                   char **ppparams, uint32 parameter_count,
1628                                   char **ppdata, uint32 data_count)
1629 {
1630         char *params = *ppparams;
1631         pstring new_name;
1632         files_struct *fsp = NULL;
1633         BOOL replace_if_exists = False;
1634         NTSTATUS status;
1635
1636         if(parameter_count < 4)
1637                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1638
1639         fsp = file_fsp(params, 0);
1640         replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1641         CHECK_FSP(fsp, conn);
1642         srvstr_get_path(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE, &status);
1643         if (!NT_STATUS_IS_OK(status)) {
1644                 return ERROR_NT(status);
1645         }
1646
1647         status = rename_internals(conn, fsp->fsp_name,
1648                                   new_name, 0, replace_if_exists);
1649         if (!NT_STATUS_IS_OK(status))
1650                 return ERROR_NT(status);
1651
1652         /*
1653          * Rename was successful.
1654          */
1655         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1656         
1657         DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n", 
1658                  fsp->fsp_name, new_name));
1659         
1660         /*
1661          * Win2k needs a changenotify request response before it will
1662          * update after a rename..
1663          */
1664         
1665         process_pending_change_notify_queue((time_t)0);
1666
1667         return -1;
1668 }
1669
1670 /******************************************************************************
1671  Fake up a completely empty SD.
1672 *******************************************************************************/
1673
1674 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1675 {
1676         extern DOM_SID global_sid_World;
1677         size_t sd_size;
1678
1679         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1680         if(!*ppsd) {
1681                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1682                 sd_size = 0;
1683         }
1684
1685         return sd_size;
1686 }
1687
1688 /****************************************************************************
1689  Reply to query a security descriptor.
1690 ****************************************************************************/
1691
1692 static int call_nt_transact_query_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
1693                                   char **ppsetup, uint32 setup_count,
1694                                   char **ppparams, uint32 parameter_count,
1695                                   char **ppdata, uint32 data_count)
1696 {
1697         uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1698         char *params = *ppparams;
1699         char *data = *ppdata;
1700         prs_struct pd;
1701         SEC_DESC *psd = NULL;
1702         size_t sd_size;
1703         uint32 security_info_wanted;
1704         TALLOC_CTX *mem_ctx;
1705         files_struct *fsp = NULL;
1706
1707         if(parameter_count < 8)
1708                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1709
1710         fsp = file_fsp(params,0);
1711         if(!fsp)
1712                 return ERROR_DOS(ERRDOS,ERRbadfid);
1713
1714         security_info_wanted = IVAL(params,4);
1715
1716         DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1717
1718         params = nttrans_realloc(ppparams, 4);
1719         if(params == NULL)
1720                 return ERROR_DOS(ERRDOS,ERRnomem);
1721
1722         if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
1723                 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1724                 return ERROR_DOS(ERRDOS,ERRnomem);
1725         }
1726
1727         /*
1728          * Get the permissions to return.
1729          */
1730
1731         if (!lp_nt_acl_support(SNUM(conn)))
1732                 sd_size = get_null_nt_acl(mem_ctx, &psd);
1733         else
1734                 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fd, security_info_wanted, &psd);
1735
1736         if (sd_size == 0) {
1737                 talloc_destroy(mem_ctx);
1738                 return(UNIXERROR(ERRDOS,ERRnoaccess));
1739         }
1740
1741         DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1742
1743         SIVAL(params,0,(uint32)sd_size);
1744
1745         if(max_data_count < sd_size) {
1746
1747                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1748                         params, 4, *ppdata, 0);
1749                 talloc_destroy(mem_ctx);
1750                 return -1;
1751         }
1752
1753         /*
1754          * Allocate the data we will point this at.
1755          */
1756
1757         data = nttrans_realloc(ppdata, sd_size);
1758         if(data == NULL) {
1759                 talloc_destroy(mem_ctx);
1760                 return ERROR_DOS(ERRDOS,ERRnomem);
1761         }
1762
1763         /*
1764          * Init the parse struct we will marshall into.
1765          */
1766
1767         prs_init(&pd, 0, mem_ctx, MARSHALL);
1768
1769         /*
1770          * Setup the prs_struct to point at the memory we just
1771          * allocated.
1772          */
1773
1774         prs_give_memory( &pd, data, (uint32)sd_size, False);
1775
1776         /*
1777          * Finally, linearize into the outgoing buffer.
1778          */
1779
1780         if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1781                 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1782 security descriptor.\n"));
1783                 /*
1784                  * Return access denied for want of a better error message..
1785                  */ 
1786                 talloc_destroy(mem_ctx);
1787                 return(UNIXERROR(ERRDOS,ERRnoaccess));
1788         }
1789
1790         /*
1791          * Now we can delete the security descriptor.
1792          */
1793
1794         talloc_destroy(mem_ctx);
1795
1796         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
1797         return -1;
1798 }
1799
1800 /****************************************************************************
1801  Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1802 ****************************************************************************/
1803
1804 static int call_nt_transact_set_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1805                                   char **ppsetup, uint32 setup_count,
1806                                   char **ppparams, uint32 parameter_count,
1807                                   char **ppdata, uint32 data_count)
1808 {
1809         char *params= *ppparams;
1810         char *data = *ppdata;
1811         files_struct *fsp = NULL;
1812         uint32 security_info_sent = 0;
1813         NTSTATUS nt_status;
1814
1815         if(parameter_count < 8)
1816                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1817
1818         if((fsp = file_fsp(params,0)) == NULL)
1819                 return ERROR_DOS(ERRDOS,ERRbadfid);
1820
1821         if(!lp_nt_acl_support(SNUM(conn)))
1822                 goto done;
1823
1824         security_info_sent = IVAL(params,4);
1825
1826         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1827                 (unsigned int)security_info_sent ));
1828
1829         if (data_count == 0)
1830                 return ERROR_DOS(ERRDOS, ERRnoaccess);
1831
1832         if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent)))
1833                 return ERROR_NT(nt_status);
1834
1835   done:
1836
1837         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1838         return -1;
1839 }
1840    
1841 /****************************************************************************
1842  Reply to NT IOCTL
1843 ****************************************************************************/
1844
1845 static int call_nt_transact_ioctl(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
1846                                   char **ppsetup, uint32 setup_count,
1847                                   char **ppparams, uint32 parameter_count,
1848                                   char **ppdata, uint32 data_count)
1849 {
1850         uint32 function;
1851         uint16 fidnum;
1852         files_struct *fsp;
1853         uint8 isFSctl;
1854         uint8 compfilter;
1855         static BOOL logged_message;
1856         char *pdata = *ppdata;
1857
1858         if (setup_count != 8) {
1859                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1860                 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1861         }
1862
1863         function = IVAL(*ppsetup, 0);
1864         fidnum = SVAL(*ppsetup, 4);
1865         isFSctl = CVAL(*ppsetup, 6);
1866         compfilter = CVAL(*ppsetup, 7);
1867
1868         DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
1869                  function, fidnum, isFSctl, compfilter));
1870
1871         fsp=file_fsp(*ppsetup, 4);
1872         /* this check is done in each implemented function case for now
1873            because I don't want to break anything... --metze
1874         FSP_BELONGS_CONN(fsp,conn);*/
1875
1876         switch (function) {
1877         case FSCTL_SET_SPARSE:
1878                 /* pretend this succeeded - tho strictly we should
1879                    mark the file sparse (if the local fs supports it)
1880                    so we can know if we need to pre-allocate or not */
1881
1882                 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1883                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1884                 return -1;
1885         
1886         case FSCTL_0x000900C0:
1887                 /* pretend this succeeded - don't know what this really is
1888                    but works ok like this --metze
1889                  */
1890
1891                 DEBUG(10,("FSCTL_0x000900C0: called on FID[0x%04X](but not implemented)\n",fidnum));
1892                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1893                 return -1;
1894
1895         case FSCTL_GET_REPARSE_POINT:
1896                 /* pretend this fail - my winXP does it like this
1897                  * --metze
1898                  */
1899
1900                 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1901                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
1902                 return -1;
1903
1904         case FSCTL_SET_REPARSE_POINT:
1905                 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1906                  * --metze
1907                  */
1908
1909                 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1910                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
1911                 return -1;
1912                         
1913         case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1914         {
1915                 /*
1916                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1917                  * and return their volume names.  If max_data_count is 16, then it is just
1918                  * asking for the number of volumes and length of the combined names.
1919                  *
1920                  * pdata is the data allocated by our caller, but that uses
1921                  * total_data_count (which is 0 in our case) rather than max_data_count.
1922                  * Allocate the correct amount and return the pointer to let
1923                  * it be deallocated when we return.
1924                  */
1925                 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1926                 SHADOW_COPY_DATA *shadow_data = NULL;
1927                 TALLOC_CTX *shadow_mem_ctx = NULL;
1928                 BOOL labels = False;
1929                 uint32 labels_data_count = 0;
1930                 uint32 i;
1931                 char *cur_pdata;
1932
1933                 FSP_BELONGS_CONN(fsp,conn);
1934
1935                 if (max_data_count < 16) {
1936                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1937                                 max_data_count));
1938                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1939                 }
1940
1941                 if (max_data_count > 16) {
1942                         labels = True;
1943                 }
1944
1945                 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1946                 if (shadow_mem_ctx == NULL) {
1947                         DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1948                         return ERROR_NT(NT_STATUS_NO_MEMORY);
1949                 }
1950
1951                 shadow_data = (SHADOW_COPY_DATA *)talloc_zero(shadow_mem_ctx,sizeof(SHADOW_COPY_DATA));
1952                 if (shadow_data == NULL) {
1953                         DEBUG(0,("talloc_zero() failed!\n"));
1954                         return ERROR_NT(NT_STATUS_NO_MEMORY);
1955                 }
1956                 
1957                 shadow_data->mem_ctx = shadow_mem_ctx;
1958                 
1959                 /*
1960                  * Call the VFS routine to actually do the work.
1961                  */
1962                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1963                         talloc_destroy(shadow_data->mem_ctx);
1964                         if (errno == ENOSYS) {
1965                                 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n", 
1966                                         conn->connectpath));
1967                                 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1968                         } else {
1969                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n", 
1970                                         conn->connectpath));
1971                                 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);                        
1972                         }
1973                 }
1974
1975                 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1976
1977                 if (!labels) {
1978                         data_count = 16;
1979                 } else {
1980                         data_count = 12+labels_data_count+4;
1981                 }
1982
1983                 if (max_data_count<data_count) {
1984                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1985                                 max_data_count,data_count));
1986                         talloc_destroy(shadow_data->mem_ctx);
1987                         return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
1988                 }
1989
1990                 pdata = nttrans_realloc(ppdata, data_count);
1991                 if (pdata == NULL) {
1992                         talloc_destroy(shadow_data->mem_ctx);
1993                         return ERROR_NT(NT_STATUS_NO_MEMORY);
1994                 }               
1995
1996                 cur_pdata = pdata;
1997
1998                 /* num_volumes 4 bytes */
1999                 SIVAL(pdata,0,shadow_data->num_volumes);
2000
2001                 if (labels) {
2002                         /* num_labels 4 bytes */
2003                         SIVAL(pdata,4,shadow_data->num_volumes);
2004                 }
2005
2006                 /* needed_data_count 4 bytes */
2007                 SIVAL(pdata,8,labels_data_count);
2008
2009                 cur_pdata+=12;
2010
2011                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2012                         shadow_data->num_volumes,fsp->fsp_name));
2013                 if (labels && shadow_data->labels) {
2014                         for (i=0;i<shadow_data->num_volumes;i++) {
2015                                 srvstr_push(outbuf, cur_pdata, shadow_data->labels[i], 2*sizeof(SHADOW_COPY_LABEL), STR_UNICODE|STR_TERMINATE);
2016                                 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2017                                 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2018                         }
2019                 }
2020
2021                 talloc_destroy(shadow_data->mem_ctx);
2022
2023                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, pdata, data_count);
2024
2025                 return -1;
2026         }
2027         
2028         case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2029         {
2030                 /* pretend this succeeded - 
2031                  * 
2032                  * we have to send back a list with all files owned by this SID
2033                  *
2034                  * but I have to check that --metze
2035                  */
2036                 DOM_SID sid;
2037                 uid_t uid;
2038                 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2039                 
2040                 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2041
2042                 FSP_BELONGS_CONN(fsp,conn);
2043
2044                 /* unknown 4 bytes: this is not the length of the sid :-(  */
2045                 /*unknown = IVAL(pdata,0);*/
2046                 
2047                 sid_parse(pdata+4,sid_len,&sid);
2048                 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2049
2050                 if (!NT_STATUS_IS_OK(sid_to_uid(&sid, &uid))) {
2051                         DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2052                                 sid_string_static(&sid),(unsigned long)sid_len));
2053                         uid = (-1);
2054                 }
2055                 
2056                 /* we can take a look at the find source :-)
2057                  *
2058                  * find ./ -uid $uid  -name '*'   is what we need here
2059                  *
2060                  *
2061                  * and send 4bytes len and then NULL terminated unicode strings
2062                  * for each file
2063                  *
2064                  * but I don't know how to deal with the paged results
2065                  * (maybe we can hang the result anywhere in the fsp struct)
2066                  *
2067                  * we don't send all files at once
2068                  * and at the next we should *not* start from the beginning, 
2069                  * so we have to cache the result 
2070                  *
2071                  * --metze
2072                  */
2073                 
2074                 /* this works for now... */
2075                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2076                 return -1;      
2077         }       
2078         default:
2079                 if (!logged_message) {
2080                         logged_message = True; /* Only print this once... */
2081                         DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2082                                  function));
2083                 }
2084         }
2085
2086         return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2087 }
2088
2089
2090 #ifdef HAVE_SYS_QUOTAS
2091 /****************************************************************************
2092  Reply to get user quota 
2093 ****************************************************************************/
2094
2095 static int call_nt_transact_get_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
2096                                   char **ppsetup, uint32 setup_count,
2097                                   char **ppparams, uint32 parameter_count,
2098                                   char **ppdata, uint32 data_count)
2099 {
2100         NTSTATUS nt_status = NT_STATUS_OK;
2101         uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2102         char *params = *ppparams;
2103         char *pdata = *ppdata;
2104         char *entry;
2105         int data_len=0,param_len=0;
2106         int qt_len=0;
2107         int entry_len = 0;
2108         files_struct *fsp = NULL;
2109         uint16 level = 0;
2110         size_t sid_len;
2111         DOM_SID sid;
2112         BOOL start_enum = True;
2113         SMB_NTQUOTA_STRUCT qt;
2114         SMB_NTQUOTA_LIST *tmp_list;
2115         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2116         extern struct current_user current_user;
2117
2118         ZERO_STRUCT(qt);
2119
2120         /* access check */
2121         if (current_user.uid != 0) {
2122                 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2123                         lp_servicename(SNUM(conn)),conn->user));
2124                 return ERROR_DOS(ERRDOS,ERRnoaccess);
2125         }
2126
2127         /*
2128          * Ensure minimum number of parameters sent.
2129          */
2130
2131         if (parameter_count < 4) {
2132                 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2133                 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2134         }
2135         
2136         /* maybe we can check the quota_fnum */
2137         fsp = file_fsp(params,0);
2138         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2139                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2140                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2141         }
2142
2143         /* the NULL pointer cheking for fsp->fake_file_handle->pd
2144          * is done by CHECK_NTQUOTA_HANDLE_OK()
2145          */
2146         qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2147
2148         level = SVAL(params,2);
2149         
2150         /* unknown 12 bytes leading in params */ 
2151         
2152         switch (level) {
2153                 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2154                         /* seems that we should continue with the enum here --metze */
2155
2156                         if (qt_handle->quota_list!=NULL && 
2157                             qt_handle->tmp_list==NULL) {
2158                 
2159                                 /* free the list */
2160                                 free_ntquota_list(&(qt_handle->quota_list));
2161
2162                                 /* Realloc the size of parameters and data we will return */
2163                                 param_len = 4;
2164                                 params = nttrans_realloc(ppparams, param_len);
2165                                 if(params == NULL)
2166                                         return ERROR_DOS(ERRDOS,ERRnomem);
2167
2168                                 data_len = 0;
2169                                 SIVAL(params,0,data_len);
2170
2171                                 break;
2172                         }
2173
2174                         start_enum = False;
2175
2176                 case TRANSACT_GET_USER_QUOTA_LIST_START:
2177
2178                         if (qt_handle->quota_list==NULL &&
2179                                 qt_handle->tmp_list==NULL) {
2180                                 start_enum = True;
2181                         }
2182
2183                         if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
2184                                 return ERROR_DOS(ERRSRV,ERRerror);
2185
2186                         /* Realloc the size of parameters and data we will return */
2187                         param_len = 4;
2188                         params = nttrans_realloc(ppparams, param_len);
2189                         if(params == NULL)
2190                                 return ERROR_DOS(ERRDOS,ERRnomem);
2191
2192                         /* we should not trust the value in max_data_count*/
2193                         max_data_count = MIN(max_data_count,2048);
2194                         
2195                         pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2196                         if(pdata == NULL)
2197                                 return ERROR_DOS(ERRDOS,ERRnomem);
2198
2199                         entry = pdata;
2200
2201
2202                         /* set params Size of returned Quota Data 4 bytes*/
2203                         /* but set it later when we know it */
2204                 
2205                         /* for each entry push the data */
2206
2207                         if (start_enum) {
2208                                 qt_handle->tmp_list = qt_handle->quota_list;
2209                         }
2210
2211                         tmp_list = qt_handle->tmp_list;
2212
2213                         for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2214                                 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2215
2216                                 sid_len = sid_size(&tmp_list->quotas->sid);
2217                                 entry_len = 40 + sid_len;
2218
2219                                 /* nextoffset entry 4 bytes */
2220                                 SIVAL(entry,0,entry_len);
2221                 
2222                                 /* then the len of the SID 4 bytes */
2223                                 SIVAL(entry,4,sid_len);
2224                                 
2225                                 /* unknown data 8 bytes SMB_BIG_UINT */
2226                                 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2227                                 
2228                                 /* the used disk space 8 bytes SMB_BIG_UINT */
2229                                 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2230                                 
2231                                 /* the soft quotas 8 bytes SMB_BIG_UINT */
2232                                 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2233                                 
2234                                 /* the hard quotas 8 bytes SMB_BIG_UINT */
2235                                 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2236                                 
2237                                 /* and now the SID */
2238                                 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2239                         }
2240                         
2241                         qt_handle->tmp_list = tmp_list;
2242                         
2243                         /* overwrite the offset of the last entry */
2244                         SIVAL(entry-entry_len,0,0);
2245
2246                         data_len = 4+qt_len;
2247                         /* overwrite the params quota_data_len */
2248                         SIVAL(params,0,data_len);
2249
2250                         break;
2251
2252                 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2253                         
2254                         /* unknown 4 bytes IVAL(pdata,0) */     
2255                         
2256                         if (data_count < 8) {
2257                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2258                                 return ERROR_DOS(ERRDOS,ERRunknownlevel);                               
2259                         }
2260
2261                         sid_len = IVAL(pdata,4);
2262
2263                         if (data_count < 8+sid_len) {
2264                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2265                                 return ERROR_DOS(ERRDOS,ERRunknownlevel);                               
2266                         }
2267
2268                         data_len = 4+40+sid_len;
2269
2270                         if (max_data_count < data_len) {
2271                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2272                                         max_data_count, data_len));
2273                                 param_len = 4;
2274                                 SIVAL(params,0,data_len);
2275                                 data_len = 0;
2276                                 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2277                                 break;
2278                         }
2279
2280                         sid_parse(pdata+8,sid_len,&sid);
2281                 
2282
2283                         if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2284                                 ZERO_STRUCT(qt);
2285                                 /* 
2286                                  * we have to return zero's in all fields 
2287                                  * instead of returning an error here
2288                                  * --metze
2289                                  */
2290                         }
2291
2292                         /* Realloc the size of parameters and data we will return */
2293                         param_len = 4;
2294                         params = nttrans_realloc(ppparams, param_len);
2295                         if(params == NULL)
2296                                 return ERROR_DOS(ERRDOS,ERRnomem);
2297
2298                         pdata = nttrans_realloc(ppdata, data_len);
2299                         if(pdata == NULL)
2300                                 return ERROR_DOS(ERRDOS,ERRnomem);
2301
2302                         entry = pdata;
2303
2304                         /* set params Size of returned Quota Data 4 bytes*/
2305                         SIVAL(params,0,data_len);
2306         
2307                         /* nextoffset entry 4 bytes */
2308                         SIVAL(entry,0,0);
2309         
2310                         /* then the len of the SID 4 bytes */
2311                         SIVAL(entry,4,sid_len);
2312                         
2313                         /* unknown data 8 bytes SMB_BIG_UINT */
2314                         SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2315                         
2316                         /* the used disk space 8 bytes SMB_BIG_UINT */
2317                         SBIG_UINT(entry,16,qt.usedspace);
2318                         
2319                         /* the soft quotas 8 bytes SMB_BIG_UINT */
2320                         SBIG_UINT(entry,24,qt.softlim);
2321                         
2322                         /* the hard quotas 8 bytes SMB_BIG_UINT */
2323                         SBIG_UINT(entry,32,qt.hardlim);
2324                         
2325                         /* and now the SID */
2326                         sid_linearize(entry+40, sid_len, &sid);
2327
2328                         break;
2329
2330                 default:
2331                         DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2332                         return ERROR_DOS(ERRSRV,ERRerror);
2333                         break;
2334         }
2335
2336         send_nt_replies(inbuf, outbuf, bufsize, nt_status, params, param_len, pdata, data_len);
2337
2338         return -1;
2339 }
2340
2341 /****************************************************************************
2342  Reply to set user quota
2343 ****************************************************************************/
2344
2345 static int call_nt_transact_set_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
2346                                   char **ppsetup, uint32 setup_count,
2347                                   char **ppparams, uint32 parameter_count,
2348                                   char **ppdata, uint32 data_count)
2349 {
2350         char *params = *ppparams;
2351         char *pdata = *ppdata;
2352         int data_len=0,param_len=0;
2353         SMB_NTQUOTA_STRUCT qt;
2354         size_t sid_len;
2355         DOM_SID sid;
2356         files_struct *fsp = NULL;
2357
2358         ZERO_STRUCT(qt);
2359
2360         /* access check */
2361         if (conn->admin_user != True) {
2362                 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2363                         lp_servicename(SNUM(conn)),conn->user));
2364                 return ERROR_DOS(ERRDOS,ERRnoaccess);
2365         }
2366
2367         /*
2368          * Ensure minimum number of parameters sent.
2369          */
2370
2371         if (parameter_count < 2) {
2372                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2373                 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2374         }
2375         
2376         /* maybe we can check the quota_fnum */
2377         fsp = file_fsp(params,0);
2378         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2379                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2380                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2381         }
2382
2383         if (data_count < 40) {
2384                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2385                 return ERROR_DOS(ERRDOS,ERRunknownlevel);               
2386         }
2387
2388         /* offset to next quota record.
2389          * 4 bytes IVAL(pdata,0)
2390          * unused here...
2391          */
2392
2393         /* sid len */
2394         sid_len = IVAL(pdata,4);
2395
2396         if (data_count < 40+sid_len) {
2397                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2398                 return ERROR_DOS(ERRDOS,ERRunknownlevel);               
2399         }
2400
2401         /* unknown 8 bytes in pdata 
2402          * maybe its the change time in NTTIME
2403          */
2404
2405         /* the used space 8 bytes (SMB_BIG_UINT)*/
2406         qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2407 #ifdef LARGE_SMB_OFF_T
2408         qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2409 #else /* LARGE_SMB_OFF_T */
2410         if ((IVAL(pdata,20) != 0)&&
2411                 ((qt.usedspace != 0xFFFFFFFF)||
2412                 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2413                 /* more than 32 bits? */
2414                 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2415         }
2416 #endif /* LARGE_SMB_OFF_T */
2417
2418         /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2419         qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2420 #ifdef LARGE_SMB_OFF_T
2421         qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2422 #else /* LARGE_SMB_OFF_T */
2423         if ((IVAL(pdata,28) != 0)&&
2424                 ((qt.softlim != 0xFFFFFFFF)||
2425                 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2426                 /* more than 32 bits? */
2427                 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2428         }
2429 #endif /* LARGE_SMB_OFF_T */
2430
2431         /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2432         qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2433 #ifdef LARGE_SMB_OFF_T
2434         qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2435 #else /* LARGE_SMB_OFF_T */
2436         if ((IVAL(pdata,36) != 0)&&
2437                 ((qt.hardlim != 0xFFFFFFFF)||
2438                 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2439                 /* more than 32 bits? */
2440                 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2441         }
2442 #endif /* LARGE_SMB_OFF_T */
2443         
2444         sid_parse(pdata+40,sid_len,&sid);
2445         DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2446
2447         /* 44 unknown bytes left... */
2448
2449         if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2450                 return ERROR_DOS(ERRSRV,ERRerror);      
2451         }
2452
2453         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, pdata, data_len);
2454
2455         return -1;
2456 }
2457 #endif /* HAVE_SYS_QUOTAS */
2458
2459 /****************************************************************************
2460  Reply to a SMBNTtrans.
2461 ****************************************************************************/
2462
2463 int reply_nttrans(connection_struct *conn,
2464                         char *inbuf,char *outbuf,int length,int bufsize)
2465 {
2466         int  outsize = 0;
2467 #if 0 /* Not used. */
2468         uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
2469         uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
2470         uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2471 #endif /* Not used. */
2472         uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
2473         uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
2474         uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
2475         uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
2476         uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
2477         uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
2478         uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
2479         uint16 function_code = SVAL( inbuf, smb_nt_Function);
2480         char *params = NULL, *data = NULL, *setup = NULL;
2481         uint32 num_params_sofar, num_data_sofar;
2482         START_PROFILE(SMBnttrans);
2483
2484         if(global_oplock_break &&
2485                         ((function_code == NT_TRANSACT_CREATE) ||
2486                          (function_code == NT_TRANSACT_RENAME))) {
2487                 /*
2488                  * Queue this open message as we are the process of an oplock break.
2489                  */
2490
2491                 DEBUG(2,("reply_nttrans: queueing message code 0x%x \
2492 due to being in oplock break state.\n", (unsigned int)function_code ));
2493
2494                 push_oplock_pending_smb_message( inbuf, length);
2495                 END_PROFILE(SMBnttrans);
2496                 return -1;
2497         }
2498
2499         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2500                 END_PROFILE(SMBnttrans);
2501                 return ERROR_DOS(ERRSRV,ERRaccess);
2502         }
2503
2504         outsize = set_message(outbuf,0,0,True);
2505
2506         /* 
2507          * All nttrans messages we handle have smb_wct == 19 + setup_count.
2508          * Ensure this is so as a sanity check.
2509          */
2510
2511         if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
2512                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2513                         CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
2514                 goto bad_param;
2515         }
2516     
2517         /* Allocate the space for the setup, the maximum needed parameters and data */
2518
2519         if(setup_count > 0)
2520                 setup = (char *)malloc(setup_count);
2521         if (total_parameter_count > 0)
2522                 params = (char *)malloc(total_parameter_count);
2523         if (total_data_count > 0)
2524                 data = (char *)malloc(total_data_count);
2525  
2526         if ((total_parameter_count && !params)  || (total_data_count && !data) ||
2527                                 (setup_count && !setup)) {
2528                 SAFE_FREE(setup);
2529                 SAFE_FREE(params);
2530                 SAFE_FREE(data);
2531                 DEBUG(0,("reply_nttrans : Out of memory\n"));
2532                 END_PROFILE(SMBnttrans);
2533                 return ERROR_DOS(ERRDOS,ERRnomem);
2534         }
2535
2536         /* Copy the param and data bytes sent with this request into the params buffer */
2537         num_params_sofar = parameter_count;
2538         num_data_sofar = data_count;
2539
2540         if (parameter_count > total_parameter_count || data_count > total_data_count)
2541                 goto bad_param;
2542
2543         if(setup) {
2544                 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
2545                 if ((smb_nt_SetupStart + setup_count < smb_nt_SetupStart) ||
2546                                 (smb_nt_SetupStart + setup_count < setup_count))
2547                         goto bad_param;
2548                 if (smb_nt_SetupStart + setup_count > length)
2549                         goto bad_param;
2550
2551                 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
2552                 dump_data(10, setup, setup_count);
2553         }
2554         if(params) {
2555                 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
2556                 if ((parameter_offset + parameter_count < parameter_offset) ||
2557                                 (parameter_offset + parameter_count < parameter_count))
2558                         goto bad_param;
2559                 if ((smb_base(inbuf) + parameter_offset + parameter_count > inbuf + length)||
2560                                 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf)))
2561                         goto bad_param;
2562
2563                 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
2564                 dump_data(10, params, parameter_count);
2565         }
2566         if(data) {
2567                 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
2568                 if ((data_offset + data_count < data_offset) || (data_offset + data_count < data_count))
2569                         goto bad_param;
2570                 if ((smb_base(inbuf) + data_offset + data_count > inbuf + length) ||
2571                                 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf)))
2572                         goto bad_param;
2573
2574                 memcpy( data, smb_base(inbuf) + data_offset, data_count);
2575                 dump_data(10, data, data_count);
2576         }
2577
2578         srv_signing_trans_start(SVAL(inbuf,smb_mid));
2579
2580         if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2581                 /* We need to send an interim response then receive the rest
2582                         of the parameter/data bytes */
2583                 outsize = set_message(outbuf,0,0,True);
2584                 srv_signing_trans_stop();
2585                 if (!send_smb(smbd_server_fd(),outbuf))
2586                         exit_server("reply_nttrans: send_smb failed.");
2587
2588                 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2589                         BOOL ret;
2590                         uint32 parameter_displacement;
2591                         uint32 data_displacement;
2592
2593                         ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
2594
2595                         /*
2596                          * The sequence number for the trans reply is always
2597                          * based on the last secondary received.
2598                          */
2599
2600                         srv_signing_trans_start(SVAL(inbuf,smb_mid));
2601
2602                         if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
2603                                 outsize = set_message(outbuf,0,0,True);
2604                                 if(ret) {
2605                                         DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
2606                                 } else {
2607                                         DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
2608                                                 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
2609                                 }
2610                                 goto bad_param;
2611                         }
2612       
2613                         /* Revise total_params and total_data in case they have changed downwards */
2614                         if (IVAL(inbuf, smb_nts_TotalParameterCount) < total_parameter_count)
2615                                 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
2616                         if (IVAL(inbuf, smb_nts_TotalDataCount) < total_data_count)
2617                                 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
2618
2619                         parameter_count = IVAL(inbuf,smb_nts_ParameterCount);
2620                         parameter_offset = IVAL(inbuf, smb_nts_ParameterOffset);
2621                         parameter_displacement = IVAL(inbuf, smb_nts_ParameterDisplacement);
2622                         num_params_sofar += parameter_count;
2623
2624                         data_count = IVAL(inbuf, smb_nts_DataCount);
2625                         data_displacement = IVAL(inbuf, smb_nts_DataDisplacement);
2626                         data_offset = IVAL(inbuf, smb_nts_DataOffset);
2627                         num_data_sofar += data_count;
2628
2629                         if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count) {
2630                                 DEBUG(0,("reply_nttrans2: data overflow in secondary nttrans packet"));
2631                                 goto bad_param;
2632                         }
2633
2634                         if (parameter_count) {
2635                                 if (parameter_displacement + parameter_count >= total_parameter_count)
2636                                         goto bad_param;
2637                                 if ((parameter_displacement + parameter_count < parameter_displacement) ||
2638                                                 (parameter_displacement + parameter_count < parameter_count))
2639                                         goto bad_param;
2640                                 if (parameter_displacement > total_parameter_count)
2641                                         goto bad_param;
2642                                 if ((smb_base(inbuf) + parameter_offset + parameter_count >= inbuf + bufsize) ||
2643                                                 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf)))
2644                                         goto bad_param;
2645                                 if (parameter_displacement + params < params)
2646                                         goto bad_param;
2647
2648                                 memcpy( &params[parameter_displacement], smb_base(inbuf) + parameter_offset, parameter_count);
2649                         }
2650
2651                         if (data_count) {
2652                                 if (data_displacement + data_count >= total_data_count)
2653                                         goto bad_param;
2654                                 if ((data_displacement + data_count < data_displacement) ||
2655                                                 (data_displacement + data_count < data_count))
2656                                         goto bad_param;
2657                                 if (data_displacement > total_data_count)
2658                                         goto bad_param;
2659                                 if ((smb_base(inbuf) + data_offset + data_count >= inbuf + bufsize) ||
2660                                                 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf)))
2661                                         goto bad_param;
2662                                 if (data_displacement + data < data)
2663                                         goto bad_param;
2664
2665                                 memcpy( &data[data_displacement], smb_base(inbuf)+ data_offset, data_count);
2666                         }
2667                 }
2668         }
2669
2670         if (Protocol >= PROTOCOL_NT1)
2671                 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | FLAGS2_IS_LONG_NAME);
2672
2673         /* Now we must call the relevant NT_TRANS function */
2674         switch(function_code) {
2675                 case NT_TRANSACT_CREATE:
2676                         START_PROFILE_NESTED(NT_transact_create);
2677                         outsize = call_nt_transact_create(conn, inbuf, outbuf,
2678                                                         length, bufsize, 
2679                                                         &setup, setup_count,
2680                                                         &params, total_parameter_count, 
2681                                                         &data, total_data_count);
2682                         END_PROFILE_NESTED(NT_transact_create);
2683                         break;
2684                 case NT_TRANSACT_IOCTL:
2685                         START_PROFILE_NESTED(NT_transact_ioctl);
2686                         outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
2687                                                          length, bufsize, 
2688                                                          &setup, setup_count,
2689                                                          &params, total_parameter_count, 
2690                                                          &data, total_data_count);
2691                         END_PROFILE_NESTED(NT_transact_ioctl);
2692                         break;
2693                 case NT_TRANSACT_SET_SECURITY_DESC:
2694                         START_PROFILE_NESTED(NT_transact_set_security_desc);
2695                         outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf, 
2696                                                          length, bufsize, 
2697                                                          &setup, setup_count,
2698                                                          &params, total_parameter_count, 
2699                                                          &data, total_data_count);
2700                         END_PROFILE_NESTED(NT_transact_set_security_desc);
2701                         break;
2702                 case NT_TRANSACT_NOTIFY_CHANGE:
2703                         START_PROFILE_NESTED(NT_transact_notify_change);
2704                         outsize = call_nt_transact_notify_change(conn, inbuf, outbuf, 
2705                                                          length, bufsize, 
2706                                                          &setup, setup_count,
2707                                                          &params, total_parameter_count, 
2708                                                          &data, total_data_count);
2709                         END_PROFILE_NESTED(NT_transact_notify_change);
2710                         break;
2711                 case NT_TRANSACT_RENAME:
2712                         START_PROFILE_NESTED(NT_transact_rename);
2713                         outsize = call_nt_transact_rename(conn, inbuf, outbuf,
2714                                                          length, bufsize, 
2715                                                          &setup, setup_count,
2716                                                          &params, total_parameter_count, 
2717                                                          &data, total_data_count);
2718                         END_PROFILE_NESTED(NT_transact_rename);
2719                         break;
2720
2721                 case NT_TRANSACT_QUERY_SECURITY_DESC:
2722                         START_PROFILE_NESTED(NT_transact_query_security_desc);
2723                         outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf, 
2724                                                          length, bufsize, 
2725                                                          &setup, setup_count,
2726                                                          &params, total_parameter_count, 
2727                                                          &data, total_data_count);
2728                         END_PROFILE_NESTED(NT_transact_query_security_desc);
2729                         break;
2730 #ifdef HAVE_SYS_QUOTAS
2731                 case NT_TRANSACT_GET_USER_QUOTA:
2732                         START_PROFILE_NESTED(NT_transact_get_user_quota);
2733                         outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf, 
2734                                                          length, bufsize, 
2735                                                          &setup, setup_count,
2736                                                          &params, total_parameter_count, 
2737                                                          &data, total_data_count);
2738                         END_PROFILE_NESTED(NT_transact_get_user_quota);
2739                         break;
2740                 case NT_TRANSACT_SET_USER_QUOTA:
2741                         START_PROFILE_NESTED(NT_transact_set_user_quota);
2742                         outsize = call_nt_transact_set_user_quota(conn, inbuf, outbuf, 
2743                                                          length, bufsize, 
2744                                                          &setup, setup_count,
2745                                                          &params, total_parameter_count, 
2746                                                          &data, total_data_count);
2747                         END_PROFILE_NESTED(NT_transact_set_user_quota);
2748                         break;                                  
2749 #endif /* HAVE_SYS_QUOTAS */
2750                 default:
2751                         /* Error in request */
2752                         DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
2753                         SAFE_FREE(setup);
2754                         SAFE_FREE(params);
2755                         SAFE_FREE(data);
2756                         END_PROFILE(SMBnttrans);
2757                         srv_signing_trans_stop();
2758                         return ERROR_DOS(ERRSRV,ERRerror);
2759         }
2760
2761         /* As we do not know how many data packets will need to be
2762                 returned here the various call_nt_transact_xxxx calls
2763                 must send their own. Thus a call_nt_transact_xxxx routine only
2764                 returns a value other than -1 when it wants to send
2765                 an error packet. 
2766         */
2767
2768         srv_signing_trans_stop();
2769
2770         SAFE_FREE(setup);
2771         SAFE_FREE(params);
2772         SAFE_FREE(data);
2773         END_PROFILE(SMBnttrans);
2774         return outsize; /* If a correct response was needed the call_nt_transact_xxxx 
2775                                 calls have already sent it. If outsize != -1 then it is
2776                                 returning an error packet. */
2777
2778  bad_param:
2779
2780         srv_signing_trans_stop();
2781         SAFE_FREE(params);
2782         SAFE_FREE(data);
2783         SAFE_FREE(setup);
2784         END_PROFILE(SMBnttrans);
2785         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2786 }