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