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