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