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