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