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