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