796eb44332631273c43ca13fb3d3369fbadc416f
[samba.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                         0, /* No sharing. */
1656                         FILE_OPEN,
1657                         0, /* No create options. */
1658                         FILE_ATTRIBUTE_NORMAL,
1659                         INTERNAL_OPEN_ONLY,
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, /* Read-only. */
1673                         0, /* No sharing. */
1674                         FILE_CREATE,
1675                         0, /* No create options. */
1676                         fattr,
1677                         INTERNAL_OPEN_ONLY,
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_shared1 adds aARCH when it
1708            creates the file. This isn't the correct thing to do in the copy case. JRA */
1709         file_set_dosmode(conn, newname, fattr, &sbuf2, True);
1710
1711         if (ret < (SMB_OFF_T)sbuf1.st_size) {
1712                 return NT_STATUS_DISK_FULL;
1713         }
1714
1715         if (close_ret != 0) {
1716                 status = map_nt_error_from_unix(close_ret);
1717                 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1718                         nt_errstr(status), oldname, newname));
1719         }
1720         return status;
1721 }
1722
1723 /****************************************************************************
1724  Reply to a NT rename request.
1725 ****************************************************************************/
1726
1727 int reply_ntrename(connection_struct *conn,
1728                    char *inbuf,char *outbuf,int length,int bufsize)
1729 {
1730         int outsize = 0;
1731         pstring oldname;
1732         pstring newname;
1733         char *p;
1734         NTSTATUS status;
1735         BOOL path_contains_wcard = False;
1736         uint32 attrs = SVAL(inbuf,smb_vwv0);
1737         uint16 rename_type = SVAL(inbuf,smb_vwv1);
1738
1739         START_PROFILE(SMBntrename);
1740
1741         p = smb_buf(inbuf) + 1;
1742         p += srvstr_get_path_wcard(inbuf, oldname, p, sizeof(oldname), 0, STR_TERMINATE, &status, &path_contains_wcard);
1743         if (!NT_STATUS_IS_OK(status)) {
1744                 END_PROFILE(SMBntrename);
1745                 return ERROR_NT(status);
1746         }
1747
1748         if( is_ntfs_stream_name(oldname)) {
1749                 /* Can't rename a stream. */
1750                 END_PROFILE(SMBntrename);
1751                 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1752         }
1753
1754         if (ms_has_wild(oldname)) {
1755                 END_PROFILE(SMBntrename);
1756                 return ERROR_NT(NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1757         }
1758
1759         p++;
1760         p += srvstr_get_path(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status);
1761         if (!NT_STATUS_IS_OK(status)) {
1762                 END_PROFILE(SMBntrename);
1763                 return ERROR_NT(status);
1764         }
1765         
1766         RESOLVE_DFSPATH(oldname, conn, inbuf, outbuf);
1767         RESOLVE_DFSPATH(newname, conn, inbuf, outbuf);
1768         
1769         DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1770         
1771         switch(rename_type) {
1772                 case RENAME_FLAG_RENAME:
1773                         status = rename_internals(conn, oldname, newname, attrs, False, path_contains_wcard);
1774                         break;
1775                 case RENAME_FLAG_HARD_LINK:
1776                         status = hardlink_internals(conn, oldname, newname);
1777                         break;
1778                 case RENAME_FLAG_COPY:
1779                         if (path_contains_wcard) {
1780                                 /* No wildcards. */
1781                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1782                         } else {
1783                                 status = copy_internals(conn, oldname, newname, attrs);
1784                         }
1785                         break;
1786                 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1787                         status = NT_STATUS_INVALID_PARAMETER;
1788                         break;
1789                 default:
1790                         status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1791                         break;
1792         }
1793
1794         if (!NT_STATUS_IS_OK(status)) {
1795                 END_PROFILE(SMBntrename);
1796                 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1797                         /* We have re-scheduled this call. */
1798                         return -1;
1799                 }
1800                 return ERROR_NT(status);
1801         }
1802
1803         /*
1804          * Win2k needs a changenotify request response before it will
1805          * update after a rename..
1806          */     
1807         process_pending_change_notify_queue((time_t)0);
1808         outsize = set_message(outbuf,0,0,True);
1809   
1810         END_PROFILE(SMBntrename);
1811         return(outsize);
1812 }
1813
1814 /****************************************************************************
1815  Reply to an unsolicited SMBNTtranss - just ignore it!
1816 ****************************************************************************/
1817
1818 int reply_nttranss(connection_struct *conn,
1819                    char *inbuf,char *outbuf,int length,int bufsize)
1820 {
1821         START_PROFILE(SMBnttranss);
1822         DEBUG(4,("Ignoring nttranss of length %d\n",length));
1823         END_PROFILE(SMBnttranss);
1824         return(-1);
1825 }
1826
1827 /****************************************************************************
1828  Reply to a notify change - queue the request and 
1829  don't allow a directory to be opened.
1830 ****************************************************************************/
1831
1832 static int call_nt_transact_notify_change(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
1833                                   char **ppsetup, uint32 setup_count,
1834                                   char **ppparams, uint32 parameter_count,
1835                                   char **ppdata, uint32 data_count, uint32 max_data_count)
1836 {
1837         char *setup = *ppsetup;
1838         files_struct *fsp;
1839         uint32 flags;
1840
1841         if(setup_count < 6) {
1842                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1843         }
1844
1845         fsp = file_fsp(setup,4);
1846         flags = IVAL(setup, 0);
1847
1848         DEBUG(3,("call_nt_transact_notify_change\n"));
1849
1850         if(!fsp) {
1851                 return ERROR_DOS(ERRDOS,ERRbadfid);
1852         }
1853
1854         if((!fsp->is_directory) || (conn != fsp->conn)) {
1855                 return ERROR_DOS(ERRDOS,ERRbadfid);
1856         }
1857
1858         if (!change_notify_set(inbuf, fsp, conn, flags)) {
1859                 return(UNIXERROR(ERRDOS,ERRbadfid));
1860         }
1861
1862         DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1863 name = %s\n", fsp->fsp_name ));
1864
1865         return -1;
1866 }
1867
1868 /****************************************************************************
1869  Reply to an NT transact rename command.
1870 ****************************************************************************/
1871
1872 static int call_nt_transact_rename(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1873                                   char **ppsetup, uint32 setup_count,
1874                                   char **ppparams, uint32 parameter_count,
1875                                   char **ppdata, uint32 data_count, uint32 max_data_count)
1876 {
1877         char *params = *ppparams;
1878         pstring new_name;
1879         files_struct *fsp = NULL;
1880         BOOL replace_if_exists = False;
1881         BOOL path_contains_wcard = False;
1882         NTSTATUS status;
1883
1884         if(parameter_count < 4) {
1885                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1886         }
1887
1888         fsp = file_fsp(params, 0);
1889         replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1890         CHECK_FSP(fsp, conn);
1891         srvstr_get_path_wcard(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE, &status, &path_contains_wcard);
1892         if (!NT_STATUS_IS_OK(status)) {
1893                 return ERROR_NT(status);
1894         }
1895
1896         status = rename_internals(conn, fsp->fsp_name,
1897                                   new_name, 0, replace_if_exists, path_contains_wcard);
1898         if (!NT_STATUS_IS_OK(status))
1899                 return ERROR_NT(status);
1900
1901         /*
1902          * Rename was successful.
1903          */
1904         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1905         
1906         DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n", 
1907                  fsp->fsp_name, new_name));
1908         
1909         /*
1910          * Win2k needs a changenotify request response before it will
1911          * update after a rename..
1912          */
1913         
1914         process_pending_change_notify_queue((time_t)0);
1915
1916         return -1;
1917 }
1918
1919 /******************************************************************************
1920  Fake up a completely empty SD.
1921 *******************************************************************************/
1922
1923 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1924 {
1925         size_t sd_size;
1926
1927         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1928         if(!*ppsd) {
1929                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1930                 sd_size = 0;
1931         }
1932
1933         return sd_size;
1934 }
1935
1936 /****************************************************************************
1937  Reply to query a security descriptor.
1938 ****************************************************************************/
1939
1940 static int call_nt_transact_query_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
1941                                   char **ppsetup, uint32 setup_count,
1942                                   char **ppparams, uint32 parameter_count,
1943                                   char **ppdata, uint32 data_count, uint32 max_data_count)
1944 {
1945         char *params = *ppparams;
1946         char *data = *ppdata;
1947         prs_struct pd;
1948         SEC_DESC *psd = NULL;
1949         size_t sd_size;
1950         uint32 security_info_wanted;
1951         TALLOC_CTX *mem_ctx;
1952         files_struct *fsp = NULL;
1953
1954         if(parameter_count < 8) {
1955                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1956         }
1957
1958         fsp = file_fsp(params,0);
1959         if(!fsp) {
1960                 return ERROR_DOS(ERRDOS,ERRbadfid);
1961         }
1962
1963         security_info_wanted = IVAL(params,4);
1964
1965         DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1966                         (unsigned int)security_info_wanted ));
1967
1968         params = nttrans_realloc(ppparams, 4);
1969         if(params == NULL) {
1970                 return ERROR_DOS(ERRDOS,ERRnomem);
1971         }
1972
1973         if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
1974                 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1975                 return ERROR_DOS(ERRDOS,ERRnomem);
1976         }
1977
1978         /*
1979          * Get the permissions to return.
1980          */
1981
1982         if (!lp_nt_acl_support(SNUM(conn))) {
1983                 sd_size = get_null_nt_acl(mem_ctx, &psd);
1984         } else {
1985                 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
1986         }
1987
1988         if (sd_size == 0) {
1989                 talloc_destroy(mem_ctx);
1990                 return(UNIXERROR(ERRDOS,ERRnoaccess));
1991         }
1992
1993         DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1994
1995         SIVAL(params,0,(uint32)sd_size);
1996
1997         if(max_data_count < sd_size) {
1998
1999                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
2000                         params, 4, *ppdata, 0);
2001                 talloc_destroy(mem_ctx);
2002                 return -1;
2003         }
2004
2005         /*
2006          * Allocate the data we will point this at.
2007          */
2008
2009         data = nttrans_realloc(ppdata, sd_size);
2010         if(data == NULL) {
2011                 talloc_destroy(mem_ctx);
2012                 return ERROR_DOS(ERRDOS,ERRnomem);
2013         }
2014
2015         /*
2016          * Init the parse struct we will marshall into.
2017          */
2018
2019         prs_init(&pd, 0, mem_ctx, MARSHALL);
2020
2021         /*
2022          * Setup the prs_struct to point at the memory we just
2023          * allocated.
2024          */
2025
2026         prs_give_memory( &pd, data, (uint32)sd_size, False);
2027
2028         /*
2029          * Finally, linearize into the outgoing buffer.
2030          */
2031
2032         if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2033                 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2034 security descriptor.\n"));
2035                 /*
2036                  * Return access denied for want of a better error message..
2037                  */ 
2038                 talloc_destroy(mem_ctx);
2039                 return(UNIXERROR(ERRDOS,ERRnoaccess));
2040         }
2041
2042         /*
2043          * Now we can delete the security descriptor.
2044          */
2045
2046         talloc_destroy(mem_ctx);
2047
2048         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
2049         return -1;
2050 }
2051
2052 /****************************************************************************
2053  Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2054 ****************************************************************************/
2055
2056 static int call_nt_transact_set_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2057                                   char **ppsetup, uint32 setup_count,
2058                                   char **ppparams, uint32 parameter_count,
2059                                   char **ppdata, uint32 data_count, uint32 max_data_count)
2060 {
2061         char *params= *ppparams;
2062         char *data = *ppdata;
2063         files_struct *fsp = NULL;
2064         uint32 security_info_sent = 0;
2065         NTSTATUS nt_status;
2066
2067         if(parameter_count < 8) {
2068                 return ERROR_DOS(ERRDOS,ERRbadfunc);
2069         }
2070
2071         if((fsp = file_fsp(params,0)) == NULL) {
2072                 return ERROR_DOS(ERRDOS,ERRbadfid);
2073         }
2074
2075         if(!lp_nt_acl_support(SNUM(conn))) {
2076                 goto done;
2077         }
2078
2079         security_info_sent = IVAL(params,4);
2080
2081         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2082                 (unsigned int)security_info_sent ));
2083
2084         if (data_count == 0) {
2085                 return ERROR_DOS(ERRDOS, ERRnoaccess);
2086         }
2087
2088         if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2089                 return ERROR_NT(nt_status);
2090         }
2091
2092   done:
2093
2094         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2095         return -1;
2096 }
2097    
2098 /****************************************************************************
2099  Reply to NT IOCTL
2100 ****************************************************************************/
2101
2102 static int call_nt_transact_ioctl(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
2103                                   char **ppsetup, uint32 setup_count,
2104                                   char **ppparams, uint32 parameter_count,
2105                                   char **ppdata, uint32 data_count, uint32 max_data_count)
2106 {
2107         uint32 function;
2108         uint16 fidnum;
2109         files_struct *fsp;
2110         uint8 isFSctl;
2111         uint8 compfilter;
2112         static BOOL logged_message;
2113         char *pdata = *ppdata;
2114
2115         if (setup_count != 8) {
2116                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2117                 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2118         }
2119
2120         function = IVAL(*ppsetup, 0);
2121         fidnum = SVAL(*ppsetup, 4);
2122         isFSctl = CVAL(*ppsetup, 6);
2123         compfilter = CVAL(*ppsetup, 7);
2124
2125         DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
2126                  function, fidnum, isFSctl, compfilter));
2127
2128         fsp=file_fsp(*ppsetup, 4);
2129         /* this check is done in each implemented function case for now
2130            because I don't want to break anything... --metze
2131         FSP_BELONGS_CONN(fsp,conn);*/
2132
2133         switch (function) {
2134         case FSCTL_SET_SPARSE:
2135                 /* pretend this succeeded - tho strictly we should
2136                    mark the file sparse (if the local fs supports it)
2137                    so we can know if we need to pre-allocate or not */
2138
2139                 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2140                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2141                 return -1;
2142         
2143         case FSCTL_0x000900C0:
2144                 /* pretend this succeeded - don't know what this really is
2145                    but works ok like this --metze
2146                  */
2147
2148                 DEBUG(10,("FSCTL_0x000900C0: called on FID[0x%04X](but not implemented)\n",fidnum));
2149                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2150                 return -1;
2151
2152         case FSCTL_GET_REPARSE_POINT:
2153                 /* pretend this fail - my winXP does it like this
2154                  * --metze
2155                  */
2156
2157                 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2158                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
2159                 return -1;
2160
2161         case FSCTL_SET_REPARSE_POINT:
2162                 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2163                  * --metze
2164                  */
2165
2166                 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2167                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
2168                 return -1;
2169                         
2170         case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2171         {
2172                 /*
2173                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2174                  * and return their volume names.  If max_data_count is 16, then it is just
2175                  * asking for the number of volumes and length of the combined names.
2176                  *
2177                  * pdata is the data allocated by our caller, but that uses
2178                  * total_data_count (which is 0 in our case) rather than max_data_count.
2179                  * Allocate the correct amount and return the pointer to let
2180                  * it be deallocated when we return.
2181                  */
2182                 SHADOW_COPY_DATA *shadow_data = NULL;
2183                 TALLOC_CTX *shadow_mem_ctx = NULL;
2184                 BOOL labels = False;
2185                 uint32 labels_data_count = 0;
2186                 uint32 i;
2187                 char *cur_pdata;
2188
2189                 FSP_BELONGS_CONN(fsp,conn);
2190
2191                 if (max_data_count < 16) {
2192                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2193                                 max_data_count));
2194                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2195                 }
2196
2197                 if (max_data_count > 16) {
2198                         labels = True;
2199                 }
2200
2201                 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2202                 if (shadow_mem_ctx == NULL) {
2203                         DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2204                         return ERROR_NT(NT_STATUS_NO_MEMORY);
2205                 }
2206
2207                 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2208                 if (shadow_data == NULL) {
2209                         DEBUG(0,("talloc_zero() failed!\n"));
2210                         talloc_destroy(shadow_mem_ctx);
2211                         return ERROR_NT(NT_STATUS_NO_MEMORY);
2212                 }
2213                 
2214                 shadow_data->mem_ctx = shadow_mem_ctx;
2215                 
2216                 /*
2217                  * Call the VFS routine to actually do the work.
2218                  */
2219                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2220                         talloc_destroy(shadow_data->mem_ctx);
2221                         if (errno == ENOSYS) {
2222                                 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n", 
2223                                         conn->connectpath));
2224                                 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2225                         } else {
2226                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n", 
2227                                         conn->connectpath));
2228                                 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);                        
2229                         }
2230                 }
2231
2232                 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2233
2234                 if (!labels) {
2235                         data_count = 16;
2236                 } else {
2237                         data_count = 12+labels_data_count+4;
2238                 }
2239
2240                 if (max_data_count<data_count) {
2241                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2242                                 max_data_count,data_count));
2243                         talloc_destroy(shadow_data->mem_ctx);
2244                         return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
2245                 }
2246
2247                 pdata = nttrans_realloc(ppdata, data_count);
2248                 if (pdata == NULL) {
2249                         talloc_destroy(shadow_data->mem_ctx);
2250                         return ERROR_NT(NT_STATUS_NO_MEMORY);
2251                 }               
2252
2253                 cur_pdata = pdata;
2254
2255                 /* num_volumes 4 bytes */
2256                 SIVAL(pdata,0,shadow_data->num_volumes);
2257
2258                 if (labels) {
2259                         /* num_labels 4 bytes */
2260                         SIVAL(pdata,4,shadow_data->num_volumes);
2261                 }
2262
2263                 /* needed_data_count 4 bytes */
2264                 SIVAL(pdata,8,labels_data_count);
2265
2266                 cur_pdata+=12;
2267
2268                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2269                         shadow_data->num_volumes,fsp->fsp_name));
2270                 if (labels && shadow_data->labels) {
2271                         for (i=0;i<shadow_data->num_volumes;i++) {
2272                                 srvstr_push(outbuf, cur_pdata, shadow_data->labels[i], 2*sizeof(SHADOW_COPY_LABEL), STR_UNICODE|STR_TERMINATE);
2273                                 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2274                                 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2275                         }
2276                 }
2277
2278                 talloc_destroy(shadow_data->mem_ctx);
2279
2280                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, pdata, data_count);
2281
2282                 return -1;
2283         }
2284         
2285         case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2286         {
2287                 /* pretend this succeeded - 
2288                  * 
2289                  * we have to send back a list with all files owned by this SID
2290                  *
2291                  * but I have to check that --metze
2292                  */
2293                 DOM_SID sid;
2294                 uid_t uid;
2295                 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2296                 
2297                 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2298
2299                 FSP_BELONGS_CONN(fsp,conn);
2300
2301                 /* unknown 4 bytes: this is not the length of the sid :-(  */
2302                 /*unknown = IVAL(pdata,0);*/
2303                 
2304                 sid_parse(pdata+4,sid_len,&sid);
2305                 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2306
2307                 if (!sid_to_uid(&sid, &uid)) {
2308                         DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2309                                 sid_string_static(&sid),(unsigned long)sid_len));
2310                         uid = (-1);
2311                 }
2312                 
2313                 /* we can take a look at the find source :-)
2314                  *
2315                  * find ./ -uid $uid  -name '*'   is what we need here
2316                  *
2317                  *
2318                  * and send 4bytes len and then NULL terminated unicode strings
2319                  * for each file
2320                  *
2321                  * but I don't know how to deal with the paged results
2322                  * (maybe we can hang the result anywhere in the fsp struct)
2323                  *
2324                  * we don't send all files at once
2325                  * and at the next we should *not* start from the beginning, 
2326                  * so we have to cache the result 
2327                  *
2328                  * --metze
2329                  */
2330                 
2331                 /* this works for now... */
2332                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2333                 return -1;      
2334         }       
2335         default:
2336                 if (!logged_message) {
2337                         logged_message = True; /* Only print this once... */
2338                         DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2339                                  function));
2340                 }
2341         }
2342
2343         return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2344 }
2345
2346
2347 #ifdef HAVE_SYS_QUOTAS
2348 /****************************************************************************
2349  Reply to get user quota 
2350 ****************************************************************************/
2351
2352 static int call_nt_transact_get_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
2353                                   char **ppsetup, uint32 setup_count,
2354                                   char **ppparams, uint32 parameter_count,
2355                                   char **ppdata, uint32 data_count, uint32 max_data_count)
2356 {
2357         NTSTATUS nt_status = NT_STATUS_OK;
2358         char *params = *ppparams;
2359         char *pdata = *ppdata;
2360         char *entry;
2361         int data_len=0,param_len=0;
2362         int qt_len=0;
2363         int entry_len = 0;
2364         files_struct *fsp = NULL;
2365         uint16 level = 0;
2366         size_t sid_len;
2367         DOM_SID sid;
2368         BOOL start_enum = True;
2369         SMB_NTQUOTA_STRUCT qt;
2370         SMB_NTQUOTA_LIST *tmp_list;
2371         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2372         extern struct current_user current_user;
2373
2374         ZERO_STRUCT(qt);
2375
2376         /* access check */
2377         if (current_user.ut.uid != 0) {
2378                 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2379                         lp_servicename(SNUM(conn)),conn->user));
2380                 return ERROR_DOS(ERRDOS,ERRnoaccess);
2381         }
2382
2383         /*
2384          * Ensure minimum number of parameters sent.
2385          */
2386
2387         if (parameter_count < 4) {
2388                 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2389                 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2390         }
2391         
2392         /* maybe we can check the quota_fnum */
2393         fsp = file_fsp(params,0);
2394         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2395                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2396                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2397         }
2398
2399         /* the NULL pointer cheking for fsp->fake_file_handle->pd
2400          * is done by CHECK_NTQUOTA_HANDLE_OK()
2401          */
2402         qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2403
2404         level = SVAL(params,2);
2405         
2406         /* unknown 12 bytes leading in params */ 
2407         
2408         switch (level) {
2409                 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2410                         /* seems that we should continue with the enum here --metze */
2411
2412                         if (qt_handle->quota_list!=NULL && 
2413                             qt_handle->tmp_list==NULL) {
2414                 
2415                                 /* free the list */
2416                                 free_ntquota_list(&(qt_handle->quota_list));
2417
2418                                 /* Realloc the size of parameters and data we will return */
2419                                 param_len = 4;
2420                                 params = nttrans_realloc(ppparams, param_len);
2421                                 if(params == NULL) {
2422                                         return ERROR_DOS(ERRDOS,ERRnomem);
2423                                 }
2424
2425                                 data_len = 0;
2426                                 SIVAL(params,0,data_len);
2427
2428                                 break;
2429                         }
2430
2431                         start_enum = False;
2432
2433                 case TRANSACT_GET_USER_QUOTA_LIST_START:
2434
2435                         if (qt_handle->quota_list==NULL &&
2436                                 qt_handle->tmp_list==NULL) {
2437                                 start_enum = True;
2438                         }
2439
2440                         if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
2441                                 return ERROR_DOS(ERRSRV,ERRerror);
2442
2443                         /* Realloc the size of parameters and data we will return */
2444                         param_len = 4;
2445                         params = nttrans_realloc(ppparams, param_len);
2446                         if(params == NULL) {
2447                                 return ERROR_DOS(ERRDOS,ERRnomem);
2448                         }
2449
2450                         /* we should not trust the value in max_data_count*/
2451                         max_data_count = MIN(max_data_count,2048);
2452                         
2453                         pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2454                         if(pdata == NULL) {
2455                                 return ERROR_DOS(ERRDOS,ERRnomem);
2456                         }
2457
2458                         entry = pdata;
2459
2460                         /* set params Size of returned Quota Data 4 bytes*/
2461                         /* but set it later when we know it */
2462                 
2463                         /* for each entry push the data */
2464
2465                         if (start_enum) {
2466                                 qt_handle->tmp_list = qt_handle->quota_list;
2467                         }
2468
2469                         tmp_list = qt_handle->tmp_list;
2470
2471                         for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2472                                 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2473
2474                                 sid_len = sid_size(&tmp_list->quotas->sid);
2475                                 entry_len = 40 + sid_len;
2476
2477                                 /* nextoffset entry 4 bytes */
2478                                 SIVAL(entry,0,entry_len);
2479                 
2480                                 /* then the len of the SID 4 bytes */
2481                                 SIVAL(entry,4,sid_len);
2482                                 
2483                                 /* unknown data 8 bytes SMB_BIG_UINT */
2484                                 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2485                                 
2486                                 /* the used disk space 8 bytes SMB_BIG_UINT */
2487                                 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2488                                 
2489                                 /* the soft quotas 8 bytes SMB_BIG_UINT */
2490                                 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2491                                 
2492                                 /* the hard quotas 8 bytes SMB_BIG_UINT */
2493                                 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2494                                 
2495                                 /* and now the SID */
2496                                 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2497                         }
2498                         
2499                         qt_handle->tmp_list = tmp_list;
2500                         
2501                         /* overwrite the offset of the last entry */
2502                         SIVAL(entry-entry_len,0,0);
2503
2504                         data_len = 4+qt_len;
2505                         /* overwrite the params quota_data_len */
2506                         SIVAL(params,0,data_len);
2507
2508                         break;
2509
2510                 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2511                         
2512                         /* unknown 4 bytes IVAL(pdata,0) */     
2513                         
2514                         if (data_count < 8) {
2515                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2516                                 return ERROR_DOS(ERRDOS,ERRunknownlevel);                               
2517                         }
2518
2519                         sid_len = IVAL(pdata,4);
2520                         /* Ensure this is less than 1mb. */
2521                         if (sid_len > (1024*1024)) {
2522                                 return ERROR_DOS(ERRDOS,ERRnomem);
2523                         }
2524
2525                         if (data_count < 8+sid_len) {
2526                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2527                                 return ERROR_DOS(ERRDOS,ERRunknownlevel);                               
2528                         }
2529
2530                         data_len = 4+40+sid_len;
2531
2532                         if (max_data_count < data_len) {
2533                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2534                                         max_data_count, data_len));
2535                                 param_len = 4;
2536                                 SIVAL(params,0,data_len);
2537                                 data_len = 0;
2538                                 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2539                                 break;
2540                         }
2541
2542                         sid_parse(pdata+8,sid_len,&sid);
2543                 
2544                         if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2545                                 ZERO_STRUCT(qt);
2546                                 /* 
2547                                  * we have to return zero's in all fields 
2548                                  * instead of returning an error here
2549                                  * --metze
2550                                  */
2551                         }
2552
2553                         /* Realloc the size of parameters and data we will return */
2554                         param_len = 4;
2555                         params = nttrans_realloc(ppparams, param_len);
2556                         if(params == NULL) {
2557                                 return ERROR_DOS(ERRDOS,ERRnomem);
2558                         }
2559
2560                         pdata = nttrans_realloc(ppdata, data_len);
2561                         if(pdata == NULL) {
2562                                 return ERROR_DOS(ERRDOS,ERRnomem);
2563                         }
2564
2565                         entry = pdata;
2566
2567                         /* set params Size of returned Quota Data 4 bytes*/
2568                         SIVAL(params,0,data_len);
2569         
2570                         /* nextoffset entry 4 bytes */
2571                         SIVAL(entry,0,0);
2572         
2573                         /* then the len of the SID 4 bytes */
2574                         SIVAL(entry,4,sid_len);
2575                         
2576                         /* unknown data 8 bytes SMB_BIG_UINT */
2577                         SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2578                         
2579                         /* the used disk space 8 bytes SMB_BIG_UINT */
2580                         SBIG_UINT(entry,16,qt.usedspace);
2581                         
2582                         /* the soft quotas 8 bytes SMB_BIG_UINT */
2583                         SBIG_UINT(entry,24,qt.softlim);
2584                         
2585                         /* the hard quotas 8 bytes SMB_BIG_UINT */
2586                         SBIG_UINT(entry,32,qt.hardlim);
2587                         
2588                         /* and now the SID */
2589                         sid_linearize(entry+40, sid_len, &sid);
2590
2591                         break;
2592
2593                 default:
2594                         DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2595                         return ERROR_DOS(ERRSRV,ERRerror);
2596                         break;
2597         }
2598
2599         send_nt_replies(inbuf, outbuf, bufsize, nt_status, params, param_len, pdata, data_len);
2600
2601         return -1;
2602 }
2603
2604 /****************************************************************************
2605  Reply to set user quota
2606 ****************************************************************************/
2607
2608 static int call_nt_transact_set_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
2609                                   char **ppsetup, uint32 setup_count,
2610                                   char **ppparams, uint32 parameter_count,
2611                                   char **ppdata, uint32 data_count, uint32 max_data_count)
2612 {
2613         char *params = *ppparams;
2614         char *pdata = *ppdata;
2615         int data_len=0,param_len=0;
2616         SMB_NTQUOTA_STRUCT qt;
2617         size_t sid_len;
2618         DOM_SID sid;
2619         files_struct *fsp = NULL;
2620
2621         ZERO_STRUCT(qt);
2622
2623         /* access check */
2624         if (current_user.ut.uid != 0) {
2625                 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2626                         lp_servicename(SNUM(conn)),conn->user));
2627                 return ERROR_DOS(ERRDOS,ERRnoaccess);
2628         }
2629
2630         /*
2631          * Ensure minimum number of parameters sent.
2632          */
2633
2634         if (parameter_count < 2) {
2635                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2636                 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2637         }
2638         
2639         /* maybe we can check the quota_fnum */
2640         fsp = file_fsp(params,0);
2641         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2642                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2643                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2644         }
2645
2646         if (data_count < 40) {
2647                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2648                 return ERROR_DOS(ERRDOS,ERRunknownlevel);               
2649         }
2650
2651         /* offset to next quota record.
2652          * 4 bytes IVAL(pdata,0)
2653          * unused here...
2654          */
2655
2656         /* sid len */
2657         sid_len = IVAL(pdata,4);
2658
2659         if (data_count < 40+sid_len) {
2660                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2661                 return ERROR_DOS(ERRDOS,ERRunknownlevel);               
2662         }
2663
2664         /* unknown 8 bytes in pdata 
2665          * maybe its the change time in NTTIME
2666          */
2667
2668         /* the used space 8 bytes (SMB_BIG_UINT)*/
2669         qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2670 #ifdef LARGE_SMB_OFF_T
2671         qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2672 #else /* LARGE_SMB_OFF_T */
2673         if ((IVAL(pdata,20) != 0)&&
2674                 ((qt.usedspace != 0xFFFFFFFF)||
2675                 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2676                 /* more than 32 bits? */
2677                 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2678         }
2679 #endif /* LARGE_SMB_OFF_T */
2680
2681         /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2682         qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2683 #ifdef LARGE_SMB_OFF_T
2684         qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2685 #else /* LARGE_SMB_OFF_T */
2686         if ((IVAL(pdata,28) != 0)&&
2687                 ((qt.softlim != 0xFFFFFFFF)||
2688                 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2689                 /* more than 32 bits? */
2690                 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2691         }
2692 #endif /* LARGE_SMB_OFF_T */
2693
2694         /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2695         qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2696 #ifdef LARGE_SMB_OFF_T
2697         qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2698 #else /* LARGE_SMB_OFF_T */
2699         if ((IVAL(pdata,36) != 0)&&
2700                 ((qt.hardlim != 0xFFFFFFFF)||
2701                 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2702                 /* more than 32 bits? */
2703                 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2704         }
2705 #endif /* LARGE_SMB_OFF_T */
2706         
2707         sid_parse(pdata+40,sid_len,&sid);
2708         DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2709
2710         /* 44 unknown bytes left... */
2711
2712         if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2713                 return ERROR_DOS(ERRSRV,ERRerror);      
2714         }
2715
2716         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, pdata, data_len);
2717
2718         return -1;
2719 }
2720 #endif /* HAVE_SYS_QUOTAS */
2721
2722 /****************************************************************************
2723  Reply to a SMBNTtrans.
2724 ****************************************************************************/
2725
2726 int reply_nttrans(connection_struct *conn,
2727                         char *inbuf,char *outbuf,int length,int bufsize)
2728 {
2729         int  outsize = 0;
2730         uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2731 #if 0 /* Not used. */
2732         uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
2733         uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
2734 #endif /* Not used. */
2735         uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
2736         uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
2737         uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
2738         uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
2739         uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
2740         uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
2741         uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
2742         uint16 function_code = SVAL( inbuf, smb_nt_Function);
2743         char *params = NULL, *data = NULL, *setup = NULL;
2744         uint32 num_params_sofar, num_data_sofar;
2745         START_PROFILE(SMBnttrans);
2746
2747         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2748                 END_PROFILE(SMBnttrans);
2749                 return ERROR_DOS(ERRSRV,ERRaccess);
2750         }
2751
2752         outsize = set_message(outbuf,0,0,True);
2753
2754         /* 
2755          * All nttrans messages we handle have smb_wct == 19 + setup_count.
2756          * Ensure this is so as a sanity check.
2757          */
2758
2759         if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
2760                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2761                         CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
2762                 goto bad_param;
2763         }
2764
2765         /* Don't allow more than 128mb for each value. */
2766         if ((total_parameter_count > (1024*1024*128)) || (total_data_count > (1024*1024*128))) {
2767                 END_PROFILE(SMBnttrans);
2768                 return ERROR_DOS(ERRDOS,ERRnomem);
2769         }
2770
2771         /* Allocate the space for the setup, the maximum needed parameters and data */
2772
2773         if(setup_count > 0) {
2774                 setup = (char *)SMB_MALLOC(setup_count);
2775         }
2776         if (total_parameter_count > 0) {
2777                 params = (char *)SMB_MALLOC(total_parameter_count);
2778         }
2779         if (total_data_count > 0) {
2780                 data = (char *)SMB_MALLOC(total_data_count);
2781         }
2782  
2783         if ((total_parameter_count && !params)  || (total_data_count && !data) ||
2784                                 (setup_count && !setup)) {
2785                 SAFE_FREE(setup);
2786                 SAFE_FREE(params);
2787                 SAFE_FREE(data);
2788                 DEBUG(0,("reply_nttrans : Out of memory\n"));
2789                 END_PROFILE(SMBnttrans);
2790                 return ERROR_DOS(ERRDOS,ERRnomem);
2791         }
2792
2793         /* Copy the param and data bytes sent with this request into the params buffer */
2794         num_params_sofar = parameter_count;
2795         num_data_sofar = data_count;
2796
2797         if (parameter_count > total_parameter_count || data_count > total_data_count)
2798                 goto bad_param;
2799
2800         if(setup) {
2801                 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
2802                 if ((smb_nt_SetupStart + setup_count < smb_nt_SetupStart) ||
2803                                 (smb_nt_SetupStart + setup_count < setup_count)) {
2804                         goto bad_param;
2805                 }
2806                 if (smb_nt_SetupStart + setup_count > length) {
2807                         goto bad_param;
2808                 }
2809
2810                 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
2811                 dump_data(10, setup, setup_count);
2812         }
2813         if(params) {
2814                 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
2815                 if ((parameter_offset + parameter_count < parameter_offset) ||
2816                                 (parameter_offset + parameter_count < parameter_count)) {
2817                         goto bad_param;
2818                 }
2819                 if ((smb_base(inbuf) + parameter_offset + parameter_count > inbuf + length)||
2820                                 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf))) {
2821                         goto bad_param;
2822                 }
2823
2824                 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
2825                 dump_data(10, params, parameter_count);
2826         }
2827         if(data) {
2828                 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
2829                 if ((data_offset + data_count < data_offset) || (data_offset + data_count < data_count)) {
2830                         goto bad_param;
2831                 }
2832                 if ((smb_base(inbuf) + data_offset + data_count > inbuf + length) ||
2833                                 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf))) {
2834                         goto bad_param;
2835                 }
2836
2837                 memcpy( data, smb_base(inbuf) + data_offset, data_count);
2838                 dump_data(10, data, data_count);
2839         }
2840
2841         srv_signing_trans_start(SVAL(inbuf,smb_mid));
2842
2843         if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2844                 /* We need to send an interim response then receive the rest
2845                         of the parameter/data bytes */
2846                 outsize = set_message(outbuf,0,0,True);
2847                 srv_signing_trans_stop();
2848                 show_msg(outbuf);
2849                 if (!send_smb(smbd_server_fd(),outbuf)) {
2850                         exit_server("reply_nttrans: send_smb failed.");
2851                 }
2852
2853                 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2854                         BOOL ret;
2855                         uint32 parameter_displacement;
2856                         uint32 data_displacement;
2857
2858                         ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
2859
2860                         /* We need to re-calcuate the new length after we've read the secondary packet. */
2861                         length = smb_len(inbuf) + 4;
2862
2863                         /*
2864                          * The sequence number for the trans reply is always
2865                          * based on the last secondary received.
2866                          */
2867
2868                         srv_signing_trans_start(SVAL(inbuf,smb_mid));
2869
2870                         if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
2871                                 outsize = set_message(outbuf,0,0,True);
2872                                 if(ret) {
2873                                         DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
2874                                 } else {
2875                                         DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
2876                                                 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
2877                                 }
2878                                 goto bad_param;
2879                         }
2880       
2881                         /* Revise total_params and total_data in case they have changed downwards */
2882                         if (IVAL(inbuf, smb_nts_TotalParameterCount) < total_parameter_count) {
2883                                 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
2884                         }
2885                         if (IVAL(inbuf, smb_nts_TotalDataCount) < total_data_count) {
2886                                 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
2887                         }
2888
2889                         parameter_count = IVAL(inbuf,smb_nts_ParameterCount);
2890                         parameter_offset = IVAL(inbuf, smb_nts_ParameterOffset);
2891                         parameter_displacement = IVAL(inbuf, smb_nts_ParameterDisplacement);
2892                         num_params_sofar += parameter_count;
2893
2894                         data_count = IVAL(inbuf, smb_nts_DataCount);
2895                         data_displacement = IVAL(inbuf, smb_nts_DataDisplacement);
2896                         data_offset = IVAL(inbuf, smb_nts_DataOffset);
2897                         num_data_sofar += data_count;
2898
2899                         if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count) {
2900                                 DEBUG(0,("reply_nttrans2: data overflow in secondary nttrans packet"));
2901                                 goto bad_param;
2902                         }
2903
2904                         if (parameter_count) {
2905                                 if (parameter_displacement + parameter_count > total_parameter_count) {
2906                                         goto bad_param;
2907                                 }
2908                                 if ((parameter_displacement + parameter_count < parameter_displacement) ||
2909                                                 (parameter_displacement + parameter_count < parameter_count)) {
2910                                         goto bad_param;
2911                                 }
2912                                 if (parameter_displacement > total_parameter_count) {
2913                                         goto bad_param;
2914                                 }
2915                                 if ((smb_base(inbuf) + parameter_offset + parameter_count > inbuf + length) ||
2916                                                 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf))) {
2917                                         goto bad_param;
2918                                 }
2919                                 if (parameter_displacement + params < params) {
2920                                         goto bad_param;
2921                                 }
2922
2923                                 memcpy( &params[parameter_displacement], smb_base(inbuf) + parameter_offset, parameter_count);
2924                         }
2925
2926                         if (data_count) {
2927                                 if (data_displacement + data_count > total_data_count) {
2928                                         goto bad_param;
2929                                 }
2930                                 if ((data_displacement + data_count < data_displacement) ||
2931                                                 (data_displacement + data_count < data_count)) {
2932                                         goto bad_param;
2933                                 }
2934                                 if (data_displacement > total_data_count) {
2935                                         goto bad_param;
2936                                 }
2937                                 if ((smb_base(inbuf) + data_offset + data_count > inbuf + length) ||
2938                                                 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf))) {
2939                                         goto bad_param;
2940                                 }
2941                                 if (data_displacement + data < data) {
2942                                         goto bad_param;
2943                                 }
2944
2945                                 memcpy( &data[data_displacement], smb_base(inbuf)+ data_offset, data_count);
2946                         }
2947                 }
2948         }
2949
2950         if (Protocol >= PROTOCOL_NT1) {
2951                 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | FLAGS2_IS_LONG_NAME);
2952         }
2953
2954         /* Now we must call the relevant NT_TRANS function */
2955         switch(function_code) {
2956                 case NT_TRANSACT_CREATE:
2957                         START_PROFILE_NESTED(NT_transact_create);
2958                         outsize = call_nt_transact_create(conn, inbuf, outbuf,
2959                                                         length, bufsize, 
2960                                                         &setup, setup_count,
2961                                                         &params, total_parameter_count, 
2962                                                         &data, total_data_count, max_data_count);
2963                         END_PROFILE_NESTED(NT_transact_create);
2964                         break;
2965                 case NT_TRANSACT_IOCTL:
2966                         START_PROFILE_NESTED(NT_transact_ioctl);
2967                         outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
2968                                                          length, bufsize, 
2969                                                          &setup, setup_count,
2970                                                          &params, total_parameter_count, 
2971                                                          &data, total_data_count, max_data_count);
2972                         END_PROFILE_NESTED(NT_transact_ioctl);
2973                         break;
2974                 case NT_TRANSACT_SET_SECURITY_DESC:
2975                         START_PROFILE_NESTED(NT_transact_set_security_desc);
2976                         outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf, 
2977                                                          length, bufsize, 
2978                                                          &setup, setup_count,
2979                                                          &params, total_parameter_count, 
2980                                                          &data, total_data_count, max_data_count);
2981                         END_PROFILE_NESTED(NT_transact_set_security_desc);
2982                         break;
2983                 case NT_TRANSACT_NOTIFY_CHANGE:
2984                         START_PROFILE_NESTED(NT_transact_notify_change);
2985                         outsize = call_nt_transact_notify_change(conn, inbuf, outbuf, 
2986                                                          length, bufsize, 
2987                                                          &setup, setup_count,
2988                                                          &params, total_parameter_count, 
2989                                                          &data, total_data_count, max_data_count);
2990                         END_PROFILE_NESTED(NT_transact_notify_change);
2991                         break;
2992                 case NT_TRANSACT_RENAME:
2993                         START_PROFILE_NESTED(NT_transact_rename);
2994                         outsize = call_nt_transact_rename(conn, inbuf, outbuf,
2995                                                          length, bufsize, 
2996                                                          &setup, setup_count,
2997                                                          &params, total_parameter_count, 
2998                                                          &data, total_data_count, max_data_count);
2999                         END_PROFILE_NESTED(NT_transact_rename);
3000                         break;
3001
3002                 case NT_TRANSACT_QUERY_SECURITY_DESC:
3003                         START_PROFILE_NESTED(NT_transact_query_security_desc);
3004                         outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf, 
3005                                                          length, bufsize, 
3006                                                          &setup, setup_count,
3007                                                          &params, total_parameter_count, 
3008                                                          &data, total_data_count, max_data_count);
3009                         END_PROFILE_NESTED(NT_transact_query_security_desc);
3010                         break;
3011 #ifdef HAVE_SYS_QUOTAS
3012                 case NT_TRANSACT_GET_USER_QUOTA:
3013                         START_PROFILE_NESTED(NT_transact_get_user_quota);
3014                         outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf, 
3015                                                          length, bufsize, 
3016                                                          &setup, setup_count,
3017                                                          &params, total_parameter_count, 
3018                                                          &data, total_data_count, max_data_count);
3019                         END_PROFILE_NESTED(NT_transact_get_user_quota);
3020                         break;
3021                 case NT_TRANSACT_SET_USER_QUOTA:
3022                         START_PROFILE_NESTED(NT_transact_set_user_quota);
3023                         outsize = call_nt_transact_set_user_quota(conn, inbuf, outbuf, 
3024                                                          length, bufsize, 
3025                                                          &setup, setup_count,
3026                                                          &params, total_parameter_count, 
3027                                                          &data, total_data_count, max_data_count);
3028                         END_PROFILE_NESTED(NT_transact_set_user_quota);
3029                         break;                                  
3030 #endif /* HAVE_SYS_QUOTAS */
3031                 default:
3032                         /* Error in request */
3033                         DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
3034                         SAFE_FREE(setup);
3035                         SAFE_FREE(params);
3036                         SAFE_FREE(data);
3037                         END_PROFILE(SMBnttrans);
3038                         srv_signing_trans_stop();
3039                         return ERROR_DOS(ERRSRV,ERRerror);
3040         }
3041
3042         /* As we do not know how many data packets will need to be
3043                 returned here the various call_nt_transact_xxxx calls
3044                 must send their own. Thus a call_nt_transact_xxxx routine only
3045                 returns a value other than -1 when it wants to send
3046                 an error packet. 
3047         */
3048
3049         srv_signing_trans_stop();
3050
3051         SAFE_FREE(setup);
3052         SAFE_FREE(params);
3053         SAFE_FREE(data);
3054         END_PROFILE(SMBnttrans);
3055         return outsize; /* If a correct response was needed the call_nt_transact_xxxx 
3056                                 calls have already sent it. If outsize != -1 then it is
3057                                 returning an error packet. */
3058
3059  bad_param:
3060
3061         srv_signing_trans_stop();
3062         SAFE_FREE(params);
3063         SAFE_FREE(data);
3064         SAFE_FREE(setup);
3065         END_PROFILE(SMBnttrans);
3066         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
3067 }