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