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