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