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