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