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