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