r25111: Move to talloced pathnames on most code paths.
[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_SIZE(ctx, 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(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_SIZE(ctx, dir_name_len+2);
1396                 if (!fname) {
1397                         reply_nterror(
1398                                 req, NT_STATUS_NO_MEMORY);
1399                         END_PROFILE(SMBntcreateX);
1400                         return;
1401                 }
1402                 memcpy(fname, dir_fsp->fsp_name, dir_name_len+1);
1403
1404                 /*
1405                  * Ensure it ends in a '/'.
1406                  * We used TALLOC_SIZE +2 to add space for the '/'.
1407                  */
1408
1409                 if(dir_name_len && (fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1410                         fname[dir_name_len] = '/';
1411                         fname[dir_name_len+1] = '\0';
1412                         dir_name_len++;
1413                 }
1414
1415                 srvstr_get_path(ctx, params, req->flags2, &tmpname,
1416                                 params+53,
1417                                 parameter_count-53, STR_TERMINATE,
1418                                 &status);
1419                 if (!NT_STATUS_IS_OK(status)) {
1420                         reply_nterror(req, status);
1421                         return;
1422                 }
1423                 fname = talloc_asprintf(ctx, "%s%s",
1424                                         fname,
1425                                         tmpname);
1426                 if (!fname) {
1427                         reply_nterror(
1428                                 req, NT_STATUS_NO_MEMORY);
1429                         return;
1430                 }
1431         } else {
1432                 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
1433                                 parameter_count-53,
1434                                 STR_TERMINATE, &status);
1435                 if (!NT_STATUS_IS_OK(status)) {
1436                         reply_nterror(req, status);
1437                         return;
1438                 }
1439
1440                 /*
1441                  * Check to see if this is a mac fork of some kind.
1442                  */
1443
1444                 if( is_ntfs_stream_name(fname)) {
1445                         reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
1446                         return;
1447                 }
1448         }
1449
1450         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1451         if (oplock_request) {
1452                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1453         }
1454
1455         /*
1456          * Ordinary file or directory.
1457          */
1458
1459         /*
1460          * Check if POSIX semantics are wanted.
1461          */
1462
1463         if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1464                 case_state = set_posix_case_semantics(NULL, conn);
1465                 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1466         }
1467
1468         status = resolve_dfspath(ctx, conn,
1469                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
1470                                 fname,
1471                                 &fname);
1472         if (!NT_STATUS_IS_OK(status)) {
1473                 TALLOC_FREE(case_state);
1474                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1475                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1476                                         ERRSRV, ERRbadpath);
1477                         return;
1478                 }
1479                 reply_nterror(req, status);
1480                 return;
1481         }
1482
1483         status = unix_convert(conn, fname, False, &fname, NULL, &sbuf);
1484         if (!NT_STATUS_IS_OK(status)) {
1485                 TALLOC_FREE(case_state);
1486                 reply_nterror(req, status);
1487                 return;
1488         }
1489         /* All file access must go through check_name() */
1490         status = check_name(conn, fname);
1491         if (!NT_STATUS_IS_OK(status)) {
1492                 TALLOC_FREE(case_state);
1493                 reply_nterror(req, status);
1494                 return;
1495         }
1496
1497         /* This is the correct thing to do (check every time) but can_delete is
1498            expensive (it may have to read the parent directory permissions). So
1499            for now we're not doing it unless we have a strong hint the client
1500            is really going to delete this file. If the client is forcing FILE_CREATE
1501            let the filesystem take care of the permissions. */
1502
1503         /* Setting FILE_SHARE_DELETE is the hint. */
1504
1505         if (lp_acl_check_permissions(SNUM(conn))
1506             && (create_disposition != FILE_CREATE)
1507             && (share_access & FILE_SHARE_DELETE)
1508             && (access_mask & DELETE_ACCESS)) {
1509                 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
1510                                 !can_delete_file_in_directory(conn, fname)) {
1511                         TALLOC_FREE(case_state);
1512                         reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1513                         return;
1514                 }
1515         }
1516
1517 #if 0
1518         /* We need to support SeSecurityPrivilege for this. */
1519         if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
1520                         !user_has_privileges(current_user.nt_user_token,
1521                                 &se_security)) {
1522                 TALLOC_FREE(case_state);
1523                 reply_nterror(req, NT_STATUS_PRIVILEGE_NOT_HELD);
1524                 return;
1525         }
1526 #endif
1527
1528         if (ea_len) {
1529                 pdata = data + sd_len;
1530
1531                 /* We have already checked that ea_len <= data_count here. */
1532                 ea_list = read_nttrans_ea_list(talloc_tos(), pdata,
1533                                                ea_len);
1534                 if (!ea_list ) {
1535                         TALLOC_FREE(case_state);
1536                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1537                         return;
1538                 }
1539         }
1540
1541         /*
1542          * If it's a request for a directory open, deal with it separately.
1543          */
1544
1545         if(create_options & FILE_DIRECTORY_FILE) {
1546
1547                 /* Can't open a temp directory. IFS kit test. */
1548                 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1549                         TALLOC_FREE(case_state);
1550                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1551                         return;
1552                 }
1553
1554                 /*
1555                  * We will get a create directory here if the Win32
1556                  * app specified a security descriptor in the 
1557                  * CreateDirectory() call.
1558                  */
1559
1560                 oplock_request = 0;
1561                 status = open_directory(conn, req, fname, &sbuf,
1562                                         access_mask,
1563                                         share_access,
1564                                         create_disposition,
1565                                         create_options,
1566                                         file_attributes,
1567                                         &info, &fsp);
1568         } else {
1569
1570                 /*
1571                  * Ordinary file case.
1572                  */
1573
1574                 status = open_file_ntcreate(conn,req,fname,&sbuf,
1575                                         access_mask,
1576                                         share_access,
1577                                         create_disposition,
1578                                         create_options,
1579                                         file_attributes,
1580                                         oplock_request,
1581                                         &info, &fsp);
1582
1583                 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
1584
1585                         /*
1586                          * Fail the open if it was explicitly a non-directory file.
1587                          */
1588
1589                         if (create_options & FILE_NON_DIRECTORY_FILE) {
1590                                 TALLOC_FREE(case_state);
1591                                 reply_force_nterror(
1592                                         req,
1593                                         NT_STATUS_FILE_IS_A_DIRECTORY);
1594                                 return;
1595                         }
1596
1597                         oplock_request = 0;
1598                         status = open_directory(conn, req, fname,
1599                                                 &sbuf,
1600                                                 access_mask,
1601                                                 share_access,
1602                                                 create_disposition,
1603                                                 create_options,
1604                                                 file_attributes,
1605                                                 &info, &fsp);
1606                 }
1607         }
1608
1609         TALLOC_FREE(case_state);
1610
1611         if(!NT_STATUS_IS_OK(status)) {
1612                 if (open_was_deferred(req->mid)) {
1613                         /* We have re-scheduled this call. */
1614                         return;
1615                 }
1616                 reply_openerror(req, status);
1617                 return;
1618         }
1619
1620         /*
1621          * According to the MS documentation, the only time the security
1622          * descriptor is applied to the opened file is iff we *created* the
1623          * file; an existing file stays the same.
1624          *
1625          * Also, it seems (from observation) that you can open the file with
1626          * any access mask but you can still write the sd. We need to override
1627          * the granted access before we call set_sd
1628          * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1629          */
1630
1631         if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1632                 uint32 saved_access_mask = fsp->access_mask;
1633
1634                 /* We have already checked that sd_len <= data_count here. */
1635
1636                 fsp->access_mask = FILE_GENERIC_ALL;
1637
1638                 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1639                 if (!NT_STATUS_IS_OK(status)) {
1640                         close_file(fsp,ERROR_CLOSE);
1641                         TALLOC_FREE(case_state);
1642                         reply_nterror(req, status);
1643                         return;
1644                 }
1645                 fsp->access_mask = saved_access_mask;
1646         }
1647
1648         if (ea_len && (info == FILE_WAS_CREATED)) {
1649                 status = set_ea(conn, fsp, fname, ea_list);
1650                 if (!NT_STATUS_IS_OK(status)) {
1651                         close_file(fsp,ERROR_CLOSE);
1652                         TALLOC_FREE(case_state);
1653                         reply_nterror(req, status);
1654                         return;
1655                 }
1656         }
1657
1658         TALLOC_FREE(case_state);
1659
1660         file_len = sbuf.st_size;
1661         fattr = dos_mode(conn,fname,&sbuf);
1662         if(fattr == 0) {
1663                 fattr = FILE_ATTRIBUTE_NORMAL;
1664         }
1665         if (!fsp->is_directory && (fattr & aDIR)) {
1666                 close_file(fsp,ERROR_CLOSE);
1667                 reply_doserror(req, ERRDOS, ERRnoaccess);
1668                 return;
1669         }
1670
1671         /* Save the requested allocation size. */
1672         if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1673                 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1674 #ifdef LARGE_SMB_OFF_T
1675                 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1676 #endif
1677                 if (allocation_size && (allocation_size > file_len)) {
1678                         fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1679                         if (fsp->is_directory) {
1680                                 close_file(fsp,ERROR_CLOSE);
1681                                 /* Can't set allocation size on a directory. */
1682                                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1683                                 return;
1684                         }
1685                         if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1686                                 close_file(fsp,ERROR_CLOSE);
1687                                 reply_nterror(req, NT_STATUS_DISK_FULL);
1688                                 return;
1689                         }
1690                 } else {
1691                         fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1692                 }
1693         }
1694
1695         /*
1696          * If the caller set the extended oplock request bit
1697          * and we granted one (by whatever means) - set the
1698          * correct bit for extended oplock reply.
1699          */
1700
1701         if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1702                 extended_oplock_granted = True;
1703         }
1704
1705         if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1706                 extended_oplock_granted = True;
1707         }
1708
1709         /* Realloc the size of parameters and data we will return */
1710         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1711                 /* Extended response is 32 more byyes. */
1712                 param_len = 101;
1713         } else {
1714                 param_len = 69;
1715         }
1716         params = nttrans_realloc(ppparams, param_len);
1717         if(params == NULL) {
1718                 reply_doserror(req, ERRDOS, ERRnomem);
1719                 return;
1720         }
1721
1722         p = params;
1723         if (extended_oplock_granted) {
1724                 if (flags & REQUEST_BATCH_OPLOCK) {
1725                         SCVAL(p,0, BATCH_OPLOCK_RETURN);
1726                 } else {
1727                         SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1728                 }
1729         } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1730                 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1731         } else {
1732                 SCVAL(p,0,NO_OPLOCK_RETURN);
1733         }
1734
1735         p += 2;
1736         SSVAL(p,0,fsp->fnum);
1737         p += 2;
1738         if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1739                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1740         } else {
1741                 SIVAL(p,0,info);
1742         }
1743         p += 8;
1744
1745         /* Create time. */
1746         c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1747         a_timespec = get_atimespec(&sbuf);
1748         m_timespec = get_mtimespec(&sbuf);
1749
1750         if (lp_dos_filetime_resolution(SNUM(conn))) {
1751                 dos_filetime_timespec(&c_timespec);
1752                 dos_filetime_timespec(&a_timespec);
1753                 dos_filetime_timespec(&m_timespec);
1754         }
1755
1756         put_long_date_timespec(p, c_timespec); /* create time. */
1757         p += 8;
1758         put_long_date_timespec(p, a_timespec); /* access time */
1759         p += 8;
1760         put_long_date_timespec(p, m_timespec); /* write time */
1761         p += 8;
1762         put_long_date_timespec(p, m_timespec); /* change time */
1763         p += 8;
1764         SIVAL(p,0,fattr); /* File Attributes. */
1765         p += 4;
1766         SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1767         p += 8;
1768         SOFF_T(p,0,file_len);
1769         p += 8;
1770         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1771                 SSVAL(p,2,0x7);
1772         }
1773         p += 4;
1774         SCVAL(p,0,fsp->is_directory ? 1 : 0);
1775
1776         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1777                 uint32 perms = 0;
1778                 p += 25;
1779                 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1780                         perms = FILE_GENERIC_ALL;
1781                 } else {
1782                         perms = FILE_GENERIC_READ|FILE_EXECUTE;
1783                 }
1784                 SIVAL(p,0,perms);
1785         }
1786
1787         DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1788
1789         /* Send the required number of replies */
1790         send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1791
1792         return;
1793 }
1794
1795 /****************************************************************************
1796  Reply to a NT CANCEL request.
1797  conn POINTER CAN BE NULL HERE !
1798 ****************************************************************************/
1799
1800 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1801 {
1802         /*
1803          * Go through and cancel any pending change notifies.
1804          */
1805
1806         START_PROFILE(SMBntcancel);
1807         remove_pending_change_notify_requests_by_mid(req->mid);
1808         remove_pending_lock_requests_by_mid(req->mid);
1809         srv_cancel_sign_response(req->mid);
1810
1811         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1812
1813         END_PROFILE(SMBntcancel);
1814         return;
1815 }
1816
1817 /****************************************************************************
1818  Copy a file.
1819 ****************************************************************************/
1820
1821 static NTSTATUS copy_internals(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(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(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(conn, req, oldname, newname,
2054                                                   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(conn, oldname, newname);
2063                         }
2064                         break;
2065                 case RENAME_FLAG_COPY:
2066                         if (src_has_wcard || dest_has_wcard) {
2067                                 /* No wildcards. */
2068                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
2069                         } else {
2070                                 status = copy_internals(conn, req, oldname,
2071                                                         newname, attrs);
2072                         }
2073                         break;
2074                 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
2075                         status = NT_STATUS_INVALID_PARAMETER;
2076                         break;
2077                 default:
2078                         status = NT_STATUS_ACCESS_DENIED; /* Default error. */
2079                         break;
2080         }
2081
2082         if (!NT_STATUS_IS_OK(status)) {
2083                 if (open_was_deferred(req->mid)) {
2084                         /* We have re-scheduled this call. */
2085                         END_PROFILE(SMBntrename);
2086                         return;
2087                 }
2088
2089                 reply_nterror(req, status);
2090                 END_PROFILE(SMBntrename);
2091                 return;
2092         }
2093
2094         reply_outbuf(req, 0, 0);
2095
2096         END_PROFILE(SMBntrename);
2097         return;
2098 }
2099
2100 /****************************************************************************
2101  Reply to a notify change - queue the request and
2102  don't allow a directory to be opened.
2103 ****************************************************************************/
2104
2105 static void call_nt_transact_notify_change(connection_struct *conn,
2106                                            struct smb_request *req,
2107                                            uint16 **ppsetup,
2108                                            uint32 setup_count,
2109                                            char **ppparams,
2110                                            uint32 parameter_count,
2111                                            char **ppdata, uint32 data_count,
2112                                            uint32 max_data_count,
2113                                            uint32 max_param_count)
2114 {
2115         uint16 *setup = *ppsetup;
2116         files_struct *fsp;
2117         uint32 filter;
2118         NTSTATUS status;
2119         BOOL recursive;
2120
2121         if(setup_count < 6) {
2122                 reply_doserror(req, ERRDOS, ERRbadfunc);
2123                 return;
2124         }
2125
2126         fsp = file_fsp(SVAL(setup,4));
2127         filter = IVAL(setup, 0);
2128         recursive = (SVAL(setup, 6) != 0) ? True : False;
2129
2130         DEBUG(3,("call_nt_transact_notify_change\n"));
2131
2132         if(!fsp) {
2133                 reply_doserror(req, ERRDOS, ERRbadfid);
2134                 return;
2135         }
2136
2137         {
2138                 char *filter_string;
2139
2140                 if (!(filter_string = notify_filter_string(NULL, filter))) {
2141                         reply_nterror(req,NT_STATUS_NO_MEMORY);
2142                         return;
2143                 }
2144
2145                 DEBUG(3,("call_nt_transact_notify_change: notify change "
2146                          "called on %s, filter = %s, recursive = %d\n",
2147                          fsp->fsp_name, filter_string, recursive));
2148
2149                 TALLOC_FREE(filter_string);
2150         }
2151
2152         if((!fsp->is_directory) || (conn != fsp->conn)) {
2153                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2154                 return;
2155         }
2156
2157         if (fsp->notify == NULL) {
2158
2159                 status = change_notify_create(fsp, filter, recursive);
2160
2161                 if (!NT_STATUS_IS_OK(status)) {
2162                         DEBUG(10, ("change_notify_create returned %s\n",
2163                                    nt_errstr(status)));
2164                         reply_nterror(req, status);
2165                         return;
2166                 }
2167         }
2168
2169         if (fsp->notify->num_changes != 0) {
2170
2171                 /*
2172                  * We've got changes pending, respond immediately
2173                  */
2174
2175                 /*
2176                  * TODO: write a torture test to check the filtering behaviour
2177                  * here.
2178                  */
2179
2180                 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
2181
2182                 /*
2183                  * change_notify_reply() above has independently sent its
2184                  * results
2185                  */
2186                 return;
2187         }
2188
2189         /*
2190          * No changes pending, queue the request
2191          */
2192
2193         status = change_notify_add_request(req->inbuf, max_param_count, filter,
2194                         recursive, fsp);
2195         if (!NT_STATUS_IS_OK(status)) {
2196                 reply_nterror(req, status);
2197         }
2198         return;
2199 }
2200
2201 /****************************************************************************
2202  Reply to an NT transact rename command.
2203 ****************************************************************************/
2204
2205 static void call_nt_transact_rename(connection_struct *conn,
2206                                     struct smb_request *req,
2207                                     uint16 **ppsetup, uint32 setup_count,
2208                                     char **ppparams, uint32 parameter_count,
2209                                     char **ppdata, uint32 data_count,
2210                                     uint32 max_data_count)
2211 {
2212         char *params = *ppparams;
2213         char *new_name = NULL;
2214         files_struct *fsp = NULL;
2215         BOOL replace_if_exists = False;
2216         BOOL dest_has_wcard = False;
2217         NTSTATUS status;
2218         TALLOC_CTX *ctx = talloc_tos();
2219
2220         if(parameter_count < 5) {
2221                 reply_doserror(req, ERRDOS, ERRbadfunc);
2222                 return;
2223         }
2224
2225         fsp = file_fsp(SVAL(params, 0));
2226         replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2227         if (!check_fsp(conn, req, fsp, &current_user)) {
2228                 return;
2229         }
2230         srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
2231                               parameter_count - 4,
2232                               STR_TERMINATE, &status, &dest_has_wcard);
2233         if (!NT_STATUS_IS_OK(status)) {
2234                 reply_nterror(req, status);
2235                 return;
2236         }
2237
2238         status = rename_internals(conn, req, fsp->fsp_name,
2239                                   new_name, 0, replace_if_exists, False, dest_has_wcard);
2240
2241         if (!NT_STATUS_IS_OK(status)) {
2242                 if (open_was_deferred(req->mid)) {
2243                         /* We have re-scheduled this call. */
2244                         return;
2245                 }
2246                 reply_nterror(req, status);
2247                 return;
2248         }
2249
2250         /*
2251          * Rename was successful.
2252          */
2253         send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2254
2255         DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n", 
2256                  fsp->fsp_name, new_name));
2257
2258         return;
2259 }
2260
2261 /******************************************************************************
2262  Fake up a completely empty SD.
2263 *******************************************************************************/
2264
2265 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2266 {
2267         size_t sd_size;
2268
2269         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2270         if(!*ppsd) {
2271                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2272                 sd_size = 0;
2273         }
2274
2275         return sd_size;
2276 }
2277
2278 /****************************************************************************
2279  Reply to query a security descriptor.
2280 ****************************************************************************/
2281
2282 static void call_nt_transact_query_security_desc(connection_struct *conn,
2283                                                  struct smb_request *req,
2284                                                  uint16 **ppsetup,
2285                                                  uint32 setup_count,
2286                                                  char **ppparams,
2287                                                  uint32 parameter_count,
2288                                                  char **ppdata,
2289                                                  uint32 data_count,
2290                                                  uint32 max_data_count)
2291 {
2292         char *params = *ppparams;
2293         char *data = *ppdata;
2294         prs_struct pd;
2295         SEC_DESC *psd = NULL;
2296         size_t sd_size;
2297         uint32 security_info_wanted;
2298         TALLOC_CTX *mem_ctx;
2299         files_struct *fsp = NULL;
2300
2301         if(parameter_count < 8) {
2302                 reply_doserror(req, ERRDOS, ERRbadfunc);
2303                 return;
2304         }
2305
2306         fsp = file_fsp(SVAL(params,0));
2307         if(!fsp) {
2308                 reply_doserror(req, ERRDOS, ERRbadfid);
2309                 return;
2310         }
2311
2312         security_info_wanted = IVAL(params,4);
2313
2314         DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2315                         (unsigned int)security_info_wanted ));
2316
2317         params = nttrans_realloc(ppparams, 4);
2318         if(params == NULL) {
2319                 reply_doserror(req, ERRDOS, ERRnomem);
2320                 return;
2321         }
2322
2323         if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2324                 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2325                 reply_doserror(req, ERRDOS, ERRnomem);
2326                 return;
2327         }
2328
2329         /*
2330          * Get the permissions to return.
2331          */
2332
2333         if (!lp_nt_acl_support(SNUM(conn))) {
2334                 sd_size = get_null_nt_acl(mem_ctx, &psd);
2335         } else {
2336                 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
2337         }
2338
2339         if (sd_size == 0) {
2340                 talloc_destroy(mem_ctx);
2341                 reply_unixerror(req, ERRDOS, ERRnoaccess);
2342                 return;
2343         }
2344
2345         DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2346
2347         SIVAL(params,0,(uint32)sd_size);
2348
2349         if(max_data_count < sd_size) {
2350
2351                 send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
2352                                 params, 4, *ppdata, 0);
2353                 talloc_destroy(mem_ctx);
2354                 return;
2355         }
2356
2357         /*
2358          * Allocate the data we will point this at.
2359          */
2360
2361         data = nttrans_realloc(ppdata, sd_size);
2362         if(data == NULL) {
2363                 talloc_destroy(mem_ctx);
2364                 reply_doserror(req, ERRDOS, ERRnomem);
2365                 return;
2366         }
2367
2368         /*
2369          * Init the parse struct we will marshall into.
2370          */
2371
2372         prs_init(&pd, 0, mem_ctx, MARSHALL);
2373
2374         /*
2375          * Setup the prs_struct to point at the memory we just
2376          * allocated.
2377          */
2378
2379         prs_give_memory( &pd, data, (uint32)sd_size, False);
2380
2381         /*
2382          * Finally, linearize into the outgoing buffer.
2383          */
2384
2385         if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2386                 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2387 security descriptor.\n"));
2388                 /*
2389                  * Return access denied for want of a better error message..
2390                  */
2391                 talloc_destroy(mem_ctx);
2392                 reply_unixerror(req, ERRDOS, ERRnoaccess);
2393                 return;
2394         }
2395
2396         /*
2397          * Now we can delete the security descriptor.
2398          */
2399
2400         talloc_destroy(mem_ctx);
2401
2402         send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2403         return;
2404 }
2405
2406 /****************************************************************************
2407  Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2408 ****************************************************************************/
2409
2410 static void call_nt_transact_set_security_desc(connection_struct *conn,
2411                                                struct smb_request *req,
2412                                                uint16 **ppsetup,
2413                                                uint32 setup_count,
2414                                                char **ppparams,
2415                                                uint32 parameter_count,
2416                                                char **ppdata,
2417                                                uint32 data_count,
2418                                                uint32 max_data_count)
2419 {
2420         char *params= *ppparams;
2421         char *data = *ppdata;
2422         files_struct *fsp = NULL;
2423         uint32 security_info_sent = 0;
2424         NTSTATUS nt_status;
2425
2426         if(parameter_count < 8) {
2427                 reply_doserror(req, ERRDOS, ERRbadfunc);
2428                 return;
2429         }
2430
2431         if((fsp = file_fsp(SVAL(params,0))) == NULL) {
2432                 reply_doserror(req, ERRDOS, ERRbadfid);
2433                 return;
2434         }
2435
2436         if(!lp_nt_acl_support(SNUM(conn))) {
2437                 goto done;
2438         }
2439
2440         security_info_sent = IVAL(params,4);
2441
2442         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2443                 (unsigned int)security_info_sent ));
2444
2445         if (data_count == 0) {
2446                 reply_doserror(req, ERRDOS, ERRnoaccess);
2447                 return;
2448         }
2449
2450         if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2451                 reply_nterror(req, nt_status);
2452                 return;
2453         }
2454
2455   done:
2456
2457         send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2458         return;
2459 }
2460
2461 /****************************************************************************
2462  Reply to NT IOCTL
2463 ****************************************************************************/
2464
2465 static void call_nt_transact_ioctl(connection_struct *conn,
2466                                    struct smb_request *req,
2467                                    uint16 **ppsetup, uint32 setup_count,
2468                                    char **ppparams, uint32 parameter_count,
2469                                    char **ppdata, uint32 data_count,
2470                                    uint32 max_data_count)
2471 {
2472         uint32 function;
2473         uint16 fidnum;
2474         files_struct *fsp;
2475         uint8 isFSctl;
2476         uint8 compfilter;
2477         static BOOL logged_message;
2478         char *pdata = *ppdata;
2479
2480         if (setup_count != 8) {
2481                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2482                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2483                 return;
2484         }
2485
2486         function = IVAL(*ppsetup, 0);
2487         fidnum = SVAL(*ppsetup, 4);
2488         isFSctl = CVAL(*ppsetup, 6);
2489         compfilter = CVAL(*ppsetup, 7);
2490
2491         DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
2492                  function, fidnum, isFSctl, compfilter));
2493
2494         fsp=file_fsp(fidnum);
2495         /* this check is done in each implemented function case for now
2496            because I don't want to break anything... --metze
2497         FSP_BELONGS_CONN(fsp,conn);*/
2498
2499         switch (function) {
2500         case FSCTL_SET_SPARSE:
2501                 /* pretend this succeeded - tho strictly we should
2502                    mark the file sparse (if the local fs supports it)
2503                    so we can know if we need to pre-allocate or not */
2504
2505                 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2506                 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2507                 return;
2508
2509         case FSCTL_CREATE_OR_GET_OBJECT_ID:
2510         {
2511                 unsigned char objid[16];
2512
2513                 /* This should return the object-id on this file.
2514                  * I think I'll make this be the inode+dev. JRA.
2515                  */
2516
2517                 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2518
2519                 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
2520                         return;
2521                 }
2522
2523                 data_count = 64;
2524                 pdata = nttrans_realloc(ppdata, data_count);
2525                 if (pdata == NULL) {
2526                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2527                         return;
2528                 }
2529                 push_file_id_16(pdata, &fsp->file_id);
2530                 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2531                 push_file_id_16(pdata+32, &fsp->file_id);
2532                 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2533                                 pdata, data_count);
2534                 return;
2535         }
2536
2537         case FSCTL_GET_REPARSE_POINT:
2538                 /* pretend this fail - my winXP does it like this
2539                  * --metze
2540                  */
2541
2542                 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2543                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2544                 return;
2545
2546         case FSCTL_SET_REPARSE_POINT:
2547                 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2548                  * --metze
2549                  */
2550
2551                 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2552                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2553                 return;
2554
2555         case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2556         {
2557                 /*
2558                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2559                  * and return their volume names.  If max_data_count is 16, then it is just
2560                  * asking for the number of volumes and length of the combined names.
2561                  *
2562                  * pdata is the data allocated by our caller, but that uses
2563                  * total_data_count (which is 0 in our case) rather than max_data_count.
2564                  * Allocate the correct amount and return the pointer to let
2565                  * it be deallocated when we return.
2566                  */
2567                 SHADOW_COPY_DATA *shadow_data = NULL;
2568                 TALLOC_CTX *shadow_mem_ctx = NULL;
2569                 BOOL labels = False;
2570                 uint32 labels_data_count = 0;
2571                 uint32 i;
2572                 char *cur_pdata;
2573
2574                 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
2575                         return;
2576                 }
2577
2578                 if (max_data_count < 16) {
2579                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2580                                 max_data_count));
2581                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2582                         return;
2583                 }
2584
2585                 if (max_data_count > 16) {
2586                         labels = True;
2587                 }
2588
2589                 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2590                 if (shadow_mem_ctx == NULL) {
2591                         DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2592                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2593                         return;
2594                 }
2595
2596                 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2597                 if (shadow_data == NULL) {
2598                         DEBUG(0,("TALLOC_ZERO() failed!\n"));
2599                         talloc_destroy(shadow_mem_ctx);
2600                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2601                         return;
2602                 }
2603
2604                 shadow_data->mem_ctx = shadow_mem_ctx;
2605
2606                 /*
2607                  * Call the VFS routine to actually do the work.
2608                  */
2609                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2610                         talloc_destroy(shadow_data->mem_ctx);
2611                         if (errno == ENOSYS) {
2612                                 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n", 
2613                                         conn->connectpath));
2614                                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2615                                 return;
2616                         } else {
2617                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n", 
2618                                         conn->connectpath));
2619                                 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2620                                 return;
2621                         }
2622                 }
2623
2624                 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2625
2626                 if (!labels) {
2627                         data_count = 16;
2628                 } else {
2629                         data_count = 12+labels_data_count+4;
2630                 }
2631
2632                 if (max_data_count<data_count) {
2633                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2634                                 max_data_count,data_count));
2635                         talloc_destroy(shadow_data->mem_ctx);
2636                         reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2637                         return;
2638                 }
2639
2640                 pdata = nttrans_realloc(ppdata, data_count);
2641                 if (pdata == NULL) {
2642                         talloc_destroy(shadow_data->mem_ctx);
2643                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2644                         return;
2645                 }
2646
2647                 cur_pdata = pdata;
2648
2649                 /* num_volumes 4 bytes */
2650                 SIVAL(pdata,0,shadow_data->num_volumes);
2651
2652                 if (labels) {
2653                         /* num_labels 4 bytes */
2654                         SIVAL(pdata,4,shadow_data->num_volumes);
2655                 }
2656
2657                 /* needed_data_count 4 bytes */
2658                 SIVAL(pdata,8,labels_data_count);
2659
2660                 cur_pdata+=12;
2661
2662                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2663                         shadow_data->num_volumes,fsp->fsp_name));
2664                 if (labels && shadow_data->labels) {
2665                         for (i=0;i<shadow_data->num_volumes;i++) {
2666                                 srvstr_push(pdata, req->flags2,
2667                                             cur_pdata, shadow_data->labels[i],
2668                                             2*sizeof(SHADOW_COPY_LABEL),
2669                                             STR_UNICODE|STR_TERMINATE);
2670                                 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2671                                 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2672                         }
2673                 }
2674
2675                 talloc_destroy(shadow_data->mem_ctx);
2676
2677                 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2678                                 pdata, data_count);
2679
2680                 return;
2681         }
2682
2683         case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2684         {
2685                 /* pretend this succeeded -
2686                  *
2687                  * we have to send back a list with all files owned by this SID
2688                  *
2689                  * but I have to check that --metze
2690                  */
2691                 DOM_SID sid;
2692                 uid_t uid;
2693                 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2694
2695                 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2696
2697                 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
2698                         return;
2699                 }
2700
2701                 /* unknown 4 bytes: this is not the length of the sid :-(  */
2702                 /*unknown = IVAL(pdata,0);*/
2703
2704                 sid_parse(pdata+4,sid_len,&sid);
2705                 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2706
2707                 if (!sid_to_uid(&sid, &uid)) {
2708                         DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2709                                 sid_string_static(&sid),(unsigned long)sid_len));
2710                         uid = (-1);
2711                 }
2712
2713                 /* we can take a look at the find source :-)
2714                  *
2715                  * find ./ -uid $uid  -name '*'   is what we need here
2716                  *
2717                  *
2718                  * and send 4bytes len and then NULL terminated unicode strings
2719                  * for each file
2720                  *
2721                  * but I don't know how to deal with the paged results
2722                  * (maybe we can hang the result anywhere in the fsp struct)
2723                  *
2724                  * we don't send all files at once
2725                  * and at the next we should *not* start from the beginning,
2726                  * so we have to cache the result
2727                  *
2728                  * --metze
2729                  */
2730
2731                 /* this works for now... */
2732                 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2733                 return;
2734         }
2735         default:
2736                 if (!logged_message) {
2737                         logged_message = True; /* Only print this once... */
2738                         DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2739                                  function));
2740                 }
2741         }
2742
2743         reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2744 }
2745
2746
2747 #ifdef HAVE_SYS_QUOTAS
2748 /****************************************************************************
2749  Reply to get user quota
2750 ****************************************************************************/
2751
2752 static void call_nt_transact_get_user_quota(connection_struct *conn,
2753                                             struct smb_request *req,
2754                                             uint16 **ppsetup,
2755                                             uint32 setup_count,
2756                                             char **ppparams,
2757                                             uint32 parameter_count,
2758                                             char **ppdata,
2759                                             uint32 data_count,
2760                                             uint32 max_data_count)
2761 {
2762         NTSTATUS nt_status = NT_STATUS_OK;
2763         char *params = *ppparams;
2764         char *pdata = *ppdata;
2765         char *entry;
2766         int data_len=0,param_len=0;
2767         int qt_len=0;
2768         int entry_len = 0;
2769         files_struct *fsp = NULL;
2770         uint16 level = 0;
2771         size_t sid_len;
2772         DOM_SID sid;
2773         BOOL start_enum = True;
2774         SMB_NTQUOTA_STRUCT qt;
2775         SMB_NTQUOTA_LIST *tmp_list;
2776         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2777
2778         ZERO_STRUCT(qt);
2779
2780         /* access check */
2781         if (current_user.ut.uid != 0) {
2782                 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2783                         lp_servicename(SNUM(conn)),conn->user));
2784                 reply_doserror(req, ERRDOS, ERRnoaccess);
2785                 return;
2786         }
2787
2788         /*
2789          * Ensure minimum number of parameters sent.
2790          */
2791
2792         if (parameter_count < 4) {
2793                 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2794                 reply_doserror(req, ERRDOS, ERRinvalidparam);
2795                 return;
2796         }
2797
2798         /* maybe we can check the quota_fnum */
2799         fsp = file_fsp(SVAL(params,0));
2800         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2801                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2802                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2803                 return;
2804         }
2805
2806         /* the NULL pointer checking for fsp->fake_file_handle->pd
2807          * is done by CHECK_NTQUOTA_HANDLE_OK()
2808          */
2809         qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2810
2811         level = SVAL(params,2);
2812
2813         /* unknown 12 bytes leading in params */
2814
2815         switch (level) {
2816                 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2817                         /* seems that we should continue with the enum here --metze */
2818
2819                         if (qt_handle->quota_list!=NULL &&
2820                             qt_handle->tmp_list==NULL) {
2821
2822                                 /* free the list */
2823                                 free_ntquota_list(&(qt_handle->quota_list));
2824
2825                                 /* Realloc the size of parameters and data we will return */
2826                                 param_len = 4;
2827                                 params = nttrans_realloc(ppparams, param_len);
2828                                 if(params == NULL) {
2829                                         reply_doserror(req, ERRDOS, ERRnomem);
2830                                         return;
2831                                 }
2832
2833                                 data_len = 0;
2834                                 SIVAL(params,0,data_len);
2835
2836                                 break;
2837                         }
2838
2839                         start_enum = False;
2840
2841                 case TRANSACT_GET_USER_QUOTA_LIST_START:
2842
2843                         if (qt_handle->quota_list==NULL &&
2844                                 qt_handle->tmp_list==NULL) {
2845                                 start_enum = True;
2846                         }
2847
2848                         if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2849                                 reply_doserror(req, ERRSRV, ERRerror);
2850                                 return;
2851                         }
2852
2853                         /* Realloc the size of parameters and data we will return */
2854                         param_len = 4;
2855                         params = nttrans_realloc(ppparams, param_len);
2856                         if(params == NULL) {
2857                                 reply_doserror(req, ERRDOS, ERRnomem);
2858                                 return;
2859                         }
2860
2861                         /* we should not trust the value in max_data_count*/
2862                         max_data_count = MIN(max_data_count,2048);
2863
2864                         pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2865                         if(pdata == NULL) {
2866                                 reply_doserror(req, ERRDOS, ERRnomem);
2867                                 return;
2868                         }
2869
2870                         entry = pdata;
2871
2872                         /* set params Size of returned Quota Data 4 bytes*/
2873                         /* but set it later when we know it */
2874
2875                         /* for each entry push the data */
2876
2877                         if (start_enum) {
2878                                 qt_handle->tmp_list = qt_handle->quota_list;
2879                         }
2880
2881                         tmp_list = qt_handle->tmp_list;
2882
2883                         for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2884                                 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2885
2886                                 sid_len = sid_size(&tmp_list->quotas->sid);
2887                                 entry_len = 40 + sid_len;
2888
2889                                 /* nextoffset entry 4 bytes */
2890                                 SIVAL(entry,0,entry_len);
2891
2892                                 /* then the len of the SID 4 bytes */
2893                                 SIVAL(entry,4,sid_len);
2894
2895                                 /* unknown data 8 bytes SMB_BIG_UINT */
2896                                 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2897
2898                                 /* the used disk space 8 bytes SMB_BIG_UINT */
2899                                 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2900
2901                                 /* the soft quotas 8 bytes SMB_BIG_UINT */
2902                                 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2903
2904                                 /* the hard quotas 8 bytes SMB_BIG_UINT */
2905                                 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2906
2907                                 /* and now the SID */
2908                                 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2909                         }
2910
2911                         qt_handle->tmp_list = tmp_list;
2912
2913                         /* overwrite the offset of the last entry */
2914                         SIVAL(entry-entry_len,0,0);
2915
2916                         data_len = 4+qt_len;
2917                         /* overwrite the params quota_data_len */
2918                         SIVAL(params,0,data_len);
2919
2920                         break;
2921
2922                 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2923
2924                         /* unknown 4 bytes IVAL(pdata,0) */
2925
2926                         if (data_count < 8) {
2927                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2928                                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2929                                 return;
2930                         }
2931
2932                         sid_len = IVAL(pdata,4);
2933                         /* Ensure this is less than 1mb. */
2934                         if (sid_len > (1024*1024)) {
2935                                 reply_doserror(req, ERRDOS, ERRnomem);
2936                                 return;
2937                         }
2938
2939                         if (data_count < 8+sid_len) {
2940                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2941                                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2942                                 return;
2943                         }
2944
2945                         data_len = 4+40+sid_len;
2946
2947                         if (max_data_count < data_len) {
2948                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2949                                         max_data_count, data_len));
2950                                 param_len = 4;
2951                                 SIVAL(params,0,data_len);
2952                                 data_len = 0;
2953                                 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2954                                 break;
2955                         }
2956
2957                         sid_parse(pdata+8,sid_len,&sid);
2958
2959                         if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2960                                 ZERO_STRUCT(qt);
2961                                 /*
2962                                  * we have to return zero's in all fields
2963                                  * instead of returning an error here
2964                                  * --metze
2965                                  */
2966                         }
2967
2968                         /* Realloc the size of parameters and data we will return */
2969                         param_len = 4;
2970                         params = nttrans_realloc(ppparams, param_len);
2971                         if(params == NULL) {
2972                                 reply_doserror(req, ERRDOS, ERRnomem);
2973                                 return;
2974                         }
2975
2976                         pdata = nttrans_realloc(ppdata, data_len);
2977                         if(pdata == NULL) {
2978                                 reply_doserror(req, ERRDOS, ERRnomem);
2979                                 return;
2980                         }
2981
2982                         entry = pdata;
2983
2984                         /* set params Size of returned Quota Data 4 bytes*/
2985                         SIVAL(params,0,data_len);
2986
2987                         /* nextoffset entry 4 bytes */
2988                         SIVAL(entry,0,0);
2989
2990                         /* then the len of the SID 4 bytes */
2991                         SIVAL(entry,4,sid_len);
2992
2993                         /* unknown data 8 bytes SMB_BIG_UINT */
2994                         SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2995
2996                         /* the used disk space 8 bytes SMB_BIG_UINT */
2997                         SBIG_UINT(entry,16,qt.usedspace);
2998
2999                         /* the soft quotas 8 bytes SMB_BIG_UINT */
3000                         SBIG_UINT(entry,24,qt.softlim);
3001
3002                         /* the hard quotas 8 bytes SMB_BIG_UINT */
3003                         SBIG_UINT(entry,32,qt.hardlim);
3004
3005                         /* and now the SID */
3006                         sid_linearize(entry+40, sid_len, &sid);
3007
3008                         break;
3009
3010                 default:
3011                         DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
3012                         reply_doserror(req, ERRSRV, ERRerror);
3013                         return;
3014                         break;
3015         }
3016
3017         send_nt_replies(req, nt_status, params, param_len,
3018                         pdata, data_len);
3019 }
3020
3021 /****************************************************************************
3022  Reply to set user quota
3023 ****************************************************************************/
3024
3025 static void call_nt_transact_set_user_quota(connection_struct *conn,
3026                                             struct smb_request *req,
3027                                             uint16 **ppsetup,
3028                                             uint32 setup_count,
3029                                             char **ppparams,
3030                                             uint32 parameter_count,
3031                                             char **ppdata,
3032                                             uint32 data_count,
3033                                             uint32 max_data_count)
3034 {
3035         char *params = *ppparams;
3036         char *pdata = *ppdata;
3037         int data_len=0,param_len=0;
3038         SMB_NTQUOTA_STRUCT qt;
3039         size_t sid_len;
3040         DOM_SID sid;
3041         files_struct *fsp = NULL;
3042
3043         ZERO_STRUCT(qt);
3044
3045         /* access check */
3046         if (current_user.ut.uid != 0) {
3047                 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
3048                         lp_servicename(SNUM(conn)),conn->user));
3049                 reply_doserror(req, ERRDOS, ERRnoaccess);
3050                 return;
3051         }
3052
3053         /*
3054          * Ensure minimum number of parameters sent.
3055          */
3056
3057         if (parameter_count < 2) {
3058                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
3059                 reply_doserror(req, ERRDOS, ERRinvalidparam);
3060                 return;
3061         }
3062
3063         /* maybe we can check the quota_fnum */
3064         fsp = file_fsp(SVAL(params,0));
3065         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
3066                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
3067                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
3068                 return;
3069         }
3070
3071         if (data_count < 40) {
3072                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
3073                 reply_doserror(req, ERRDOS, ERRunknownlevel);
3074                 return;
3075         }
3076
3077         /* offset to next quota record.
3078          * 4 bytes IVAL(pdata,0)
3079          * unused here...
3080          */
3081
3082         /* sid len */
3083         sid_len = IVAL(pdata,4);
3084
3085         if (data_count < 40+sid_len) {
3086                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
3087                 reply_doserror(req, ERRDOS, ERRunknownlevel);
3088                 return;
3089         }
3090
3091         /* unknown 8 bytes in pdata
3092          * maybe its the change time in NTTIME
3093          */
3094
3095         /* the used space 8 bytes (SMB_BIG_UINT)*/
3096         qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
3097 #ifdef LARGE_SMB_OFF_T
3098         qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
3099 #else /* LARGE_SMB_OFF_T */
3100         if ((IVAL(pdata,20) != 0)&&
3101                 ((qt.usedspace != 0xFFFFFFFF)||
3102                 (IVAL(pdata,20)!=0xFFFFFFFF))) {
3103                 /* more than 32 bits? */
3104                 reply_doserror(req, ERRDOS, ERRunknownlevel);
3105                 return;
3106         }
3107 #endif /* LARGE_SMB_OFF_T */
3108
3109         /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
3110         qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
3111 #ifdef LARGE_SMB_OFF_T
3112         qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
3113 #else /* LARGE_SMB_OFF_T */
3114         if ((IVAL(pdata,28) != 0)&&
3115                 ((qt.softlim != 0xFFFFFFFF)||
3116                 (IVAL(pdata,28)!=0xFFFFFFFF))) {
3117                 /* more than 32 bits? */
3118                 reply_doserror(req, ERRDOS, ERRunknownlevel);
3119                 return;
3120         }
3121 #endif /* LARGE_SMB_OFF_T */
3122
3123         /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
3124         qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
3125 #ifdef LARGE_SMB_OFF_T
3126         qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
3127 #else /* LARGE_SMB_OFF_T */
3128         if ((IVAL(pdata,36) != 0)&&
3129                 ((qt.hardlim != 0xFFFFFFFF)||
3130                 (IVAL(pdata,36)!=0xFFFFFFFF))) {
3131                 /* more than 32 bits? */
3132                 reply_doserror(req, ERRDOS, ERRunknownlevel);
3133                 return;
3134         }
3135 #endif /* LARGE_SMB_OFF_T */
3136
3137         sid_parse(pdata+40,sid_len,&sid);
3138         DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
3139
3140         /* 44 unknown bytes left... */
3141
3142         if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
3143                 reply_doserror(req, ERRSRV, ERRerror);
3144                 return;
3145         }
3146
3147         send_nt_replies(req, NT_STATUS_OK, params, param_len,
3148                         pdata, data_len);
3149 }
3150 #endif /* HAVE_SYS_QUOTAS */
3151
3152 static void handle_nttrans(connection_struct *conn,
3153                            struct trans_state *state,
3154                            struct smb_request *req)
3155 {
3156         if (Protocol >= PROTOCOL_NT1) {
3157                 req->flags2 |= 0x40; /* IS_LONG_NAME */
3158                 SSVAL(req->inbuf,smb_flg2,req->flags2);
3159         }
3160
3161         /* Now we must call the relevant NT_TRANS function */
3162         switch(state->call) {
3163                 case NT_TRANSACT_CREATE:
3164                 {
3165                         START_PROFILE(NT_transact_create);
3166                         call_nt_transact_create(
3167                                 conn, req,
3168                                 &state->setup, state->setup_count,
3169                                 &state->param, state->total_param,
3170                                 &state->data, state->total_data,
3171                                 state->max_data_return);
3172                         END_PROFILE(NT_transact_create);
3173                         break;
3174                 }
3175
3176                 case NT_TRANSACT_IOCTL:
3177                 {
3178                         START_PROFILE(NT_transact_ioctl);
3179                         call_nt_transact_ioctl(
3180                                 conn, req,
3181                                 &state->setup, state->setup_count,
3182                                 &state->param, state->total_param,
3183                                 &state->data, state->total_data,
3184                                 state->max_data_return);
3185                         END_PROFILE(NT_transact_ioctl);
3186                         break;
3187                 }
3188
3189                 case NT_TRANSACT_SET_SECURITY_DESC:
3190                 {
3191                         START_PROFILE(NT_transact_set_security_desc);
3192                         call_nt_transact_set_security_desc(
3193                                 conn, req,
3194                                 &state->setup, state->setup_count,
3195                                 &state->param, state->total_param,
3196                                 &state->data, state->total_data,
3197                                 state->max_data_return);
3198                         END_PROFILE(NT_transact_set_security_desc);
3199                         break;
3200                 }
3201
3202                 case NT_TRANSACT_NOTIFY_CHANGE:
3203                 {
3204                         START_PROFILE(NT_transact_notify_change);
3205                         call_nt_transact_notify_change(
3206                                 conn, req,
3207                                 &state->setup, state->setup_count,
3208                                 &state->param, state->total_param,
3209                                 &state->data, state->total_data,
3210                                 state->max_data_return,
3211                                 state->max_param_return);
3212                         END_PROFILE(NT_transact_notify_change);
3213                         break;
3214                 }
3215
3216                 case NT_TRANSACT_RENAME:
3217                 {
3218                         START_PROFILE(NT_transact_rename);
3219                         call_nt_transact_rename(
3220                                 conn, req,
3221                                 &state->setup, state->setup_count,
3222                                 &state->param, state->total_param,
3223                                 &state->data, state->total_data,
3224                                 state->max_data_return);
3225                         END_PROFILE(NT_transact_rename);
3226                         break;
3227                 }
3228
3229                 case NT_TRANSACT_QUERY_SECURITY_DESC:
3230                 {
3231                         START_PROFILE(NT_transact_query_security_desc);
3232                         call_nt_transact_query_security_desc(
3233                                 conn, req,
3234                                 &state->setup, state->setup_count,
3235                                 &state->param, state->total_param,
3236                                 &state->data, state->total_data,
3237                                 state->max_data_return);
3238                         END_PROFILE(NT_transact_query_security_desc);
3239                         break;
3240                 }
3241
3242 #ifdef HAVE_SYS_QUOTAS
3243                 case NT_TRANSACT_GET_USER_QUOTA:
3244                 {
3245                         START_PROFILE(NT_transact_get_user_quota);
3246                         call_nt_transact_get_user_quota(
3247                                 conn, req,
3248                                 &state->setup, state->setup_count,
3249                                 &state->param, state->total_param,
3250                                 &state->data, state->total_data,
3251                                 state->max_data_return);
3252                         END_PROFILE(NT_transact_get_user_quota);
3253                         break;
3254                 }
3255
3256                 case NT_TRANSACT_SET_USER_QUOTA:
3257                 {
3258                         START_PROFILE(NT_transact_set_user_quota);
3259                         call_nt_transact_set_user_quota(
3260                                 conn, req,
3261                                 &state->setup, state->setup_count,
3262                                 &state->param, state->total_param,
3263                                 &state->data, state->total_data,
3264                                 state->max_data_return);
3265                         END_PROFILE(NT_transact_set_user_quota);
3266                         break;
3267                 }
3268 #endif /* HAVE_SYS_QUOTAS */
3269
3270                 default:
3271                         /* Error in request */
3272                         DEBUG(0,("handle_nttrans: Unknown request %d in "
3273                                  "nttrans call\n", state->call));
3274                         reply_doserror(req, ERRSRV, ERRerror);
3275                         return;
3276         }
3277         return;
3278 }
3279
3280 /****************************************************************************
3281  Reply to a SMBNTtrans.
3282 ****************************************************************************/
3283
3284 void reply_nttrans(connection_struct *conn, struct smb_request *req)
3285 {
3286         uint32 pscnt;
3287         uint32 psoff;
3288         uint32 dscnt;
3289         uint32 dsoff;
3290         uint16 function_code;
3291         NTSTATUS result;
3292         struct trans_state *state;
3293         int size;
3294
3295         START_PROFILE(SMBnttrans);
3296
3297         if (req->wct < 19) {
3298                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3299                 END_PROFILE(SMBnttrans);
3300                 return;
3301         }
3302
3303         size = smb_len(req->inbuf) + 4;
3304         pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
3305         psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
3306         dscnt = IVAL(req->inbuf,smb_nt_DataCount);
3307         dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
3308         function_code = SVAL(req->inbuf, smb_nt_Function);
3309
3310         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3311                 reply_doserror(req, ERRSRV, ERRaccess);
3312                 END_PROFILE(SMBnttrans);
3313                 return;
3314         }
3315
3316         result = allow_new_trans(conn->pending_trans, req->mid);
3317         if (!NT_STATUS_IS_OK(result)) {
3318                 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3319                 reply_nterror(req, result);
3320                 END_PROFILE(SMBnttrans);
3321                 return;
3322         }
3323
3324         if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3325                 reply_doserror(req, ERRSRV, ERRaccess);
3326                 END_PROFILE(SMBnttrans);
3327                 return;
3328         }
3329
3330         state->cmd = SMBnttrans;
3331
3332         state->mid = req->mid;
3333         state->vuid = req->vuid;
3334         state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
3335         state->data = NULL;
3336         state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
3337         state->param = NULL;
3338         state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
3339         state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
3340
3341         /* setup count is in *words* */
3342         state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
3343         state->setup = NULL;
3344         state->call = function_code;
3345
3346         /*
3347          * All nttrans messages we handle have smb_wct == 19 +
3348          * state->setup_count.  Ensure this is so as a sanity check.
3349          */
3350
3351         if(req->wct != 19 + (state->setup_count/2)) {
3352                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3353                          req->wct, 19 + (state->setup_count/2)));
3354                 goto bad_param;
3355         }
3356
3357         /* Don't allow more than 128mb for each value. */
3358         if ((state->total_data > (1024*1024*128)) ||
3359             (state->total_param > (1024*1024*128))) {
3360                 reply_doserror(req, ERRDOS, ERRnomem);
3361                 END_PROFILE(SMBnttrans);
3362                 return;
3363         }
3364
3365         if ((dscnt > state->total_data) || (pscnt > state->total_param))
3366                 goto bad_param;
3367
3368         if (state->total_data)  {
3369                 /* Can't use talloc here, the core routines do realloc on the
3370                  * params and data. */
3371                 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3372                         DEBUG(0,("reply_nttrans: data malloc fail for %u "
3373                                  "bytes !\n", (unsigned int)state->total_data));
3374                         TALLOC_FREE(state);
3375                         reply_doserror(req, ERRDOS, ERRnomem);
3376                         END_PROFILE(SMBnttrans);
3377                         return;
3378                 }
3379                 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3380                         goto bad_param;
3381                 if ((smb_base(req->inbuf)+dsoff+dscnt
3382                      > (char *)req->inbuf + size) ||
3383                     (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
3384                         goto bad_param;
3385
3386                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3387         }
3388
3389         if (state->total_param) {
3390                 /* Can't use talloc here, the core routines do realloc on the
3391                  * params and data. */
3392                 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3393                         DEBUG(0,("reply_nttrans: param malloc fail for %u "
3394                                  "bytes !\n", (unsigned int)state->total_param));
3395                         SAFE_FREE(state->data);
3396                         TALLOC_FREE(state);
3397                         reply_doserror(req, ERRDOS, ERRnomem);
3398                         END_PROFILE(SMBnttrans);
3399                         return;
3400                 }
3401                 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3402                         goto bad_param;
3403                 if ((smb_base(req->inbuf)+psoff+pscnt
3404                      > (char *)req->inbuf + size) ||
3405                     (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
3406                         goto bad_param;
3407
3408                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3409         }
3410
3411         state->received_data  = dscnt;
3412         state->received_param = pscnt;
3413
3414         if(state->setup_count > 0) {
3415                 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3416                           state->setup_count));
3417                 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3418                 if (state->setup == NULL) {
3419                         DEBUG(0,("reply_nttrans : Out of memory\n"));
3420                         SAFE_FREE(state->data);
3421                         SAFE_FREE(state->param);
3422                         TALLOC_FREE(state);
3423                         reply_doserror(req, ERRDOS, ERRnomem);
3424                         END_PROFILE(SMBnttrans);
3425                         return;
3426                 }
3427
3428                 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3429                     (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3430                         goto bad_param;
3431                 }
3432                 if (smb_nt_SetupStart + state->setup_count > size) {
3433                         goto bad_param;
3434                 }
3435
3436                 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
3437                         state->setup_count);
3438                 dump_data(10, (uint8 *)state->setup, state->setup_count);
3439         }
3440
3441         if ((state->received_data == state->total_data) &&
3442             (state->received_param == state->total_param)) {
3443                 handle_nttrans(conn, state, req);
3444                 SAFE_FREE(state->param);
3445                 SAFE_FREE(state->data);
3446                 TALLOC_FREE(state);
3447                 END_PROFILE(SMBnttrans);
3448                 return;
3449         }
3450
3451         DLIST_ADD(conn->pending_trans, state);
3452
3453         /* We need to send an interim response then receive the rest
3454            of the parameter/data bytes */
3455         reply_outbuf(req, 0, 0);
3456         show_msg((char *)req->outbuf);
3457         END_PROFILE(SMBnttrans);
3458         return;
3459
3460   bad_param:
3461
3462         DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3463         SAFE_FREE(state->data);
3464         SAFE_FREE(state->param);
3465         TALLOC_FREE(state);
3466         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3467         END_PROFILE(SMBnttrans);
3468         return;
3469 }
3470
3471 /****************************************************************************
3472  Reply to a SMBnttranss
3473  ****************************************************************************/
3474
3475 void reply_nttranss(connection_struct *conn, struct smb_request *req)
3476 {
3477         unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3478         struct trans_state *state;
3479
3480         int size;
3481
3482         START_PROFILE(SMBnttranss);
3483
3484         show_msg((char *)req->inbuf);
3485
3486         if (req->wct < 18) {
3487                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3488                 END_PROFILE(SMBnttranss);
3489                 return;
3490         }
3491
3492         for (state = conn->pending_trans; state != NULL;
3493              state = state->next) {
3494                 if (state->mid == req->mid) {
3495                         break;
3496                 }
3497         }
3498
3499         if ((state == NULL) || (state->cmd != SMBnttrans)) {
3500                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3501                 END_PROFILE(SMBnttranss);
3502                 return;
3503         }
3504
3505         /* Revise state->total_param and state->total_data in case they have
3506            changed downwards */
3507         if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
3508             < state->total_param) {
3509                 state->total_param = IVAL(req->inbuf,
3510                                           smb_nts_TotalParameterCount);
3511         }
3512         if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
3513                 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
3514         }
3515
3516         size = smb_len(req->inbuf) + 4;
3517
3518         pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
3519         poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
3520         pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
3521
3522         dcnt = IVAL(req->inbuf, smb_nts_DataCount);
3523         ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
3524         doff = IVAL(req->inbuf, smb_nts_DataOffset);
3525
3526         state->received_param += pcnt;
3527         state->received_data += dcnt;
3528
3529         if ((state->received_data > state->total_data) ||
3530             (state->received_param > state->total_param))
3531                 goto bad_param;
3532
3533         if (pcnt) {
3534                 if (pdisp+pcnt > state->total_param)
3535                         goto bad_param;
3536                 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3537                         goto bad_param;
3538                 if (pdisp > state->total_param)
3539                         goto bad_param;
3540                 if ((smb_base(req->inbuf) + poff + pcnt
3541                      > (char *)req->inbuf + size) ||
3542                     (smb_base(req->inbuf) + poff + pcnt
3543