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