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