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