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