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