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