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