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