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