Initial import
[samba] / 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 struct current_user current_user;
28
29 static const char *known_nt_pipes[] = {
30         "\\LANMAN",
31         "\\srvsvc",
32         "\\samr",
33         "\\wkssvc",
34         "\\NETLOGON",
35         "\\ntlsa",
36         "\\ntsvcs",
37         "\\lsass",
38         "\\lsarpc",
39         "\\winreg",
40         "\\spoolss",
41         "\\netdfs",
42         "\\rpcecho",
43         "\\svcctl",
44         "\\eventlog",
45         "\\unixinfo",
46         NULL
47 };
48
49 static char *nttrans_realloc(char **ptr, size_t size)
50 {
51         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);
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);
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);
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 (fsp->oplock_type == LEVEL_II_OPLOCK) {
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);
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);
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);
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);
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                         return ERROR_NT(status);
1292                 }
1293         }
1294
1295         if (ea_len) {
1296                 ctx = talloc_init("NTTRANS_CREATE_EA");
1297                 if (!ctx) {
1298                         talloc_destroy(ctx);
1299                         restore_case_semantics(conn, file_attributes);
1300                         return ERROR_NT(NT_STATUS_NO_MEMORY);
1301                 }
1302
1303                 pdata = data + sd_len;
1304
1305                 /* We have already checked that ea_len <= data_count here. */
1306                 ea_list = read_nttrans_ea_list(ctx, pdata, ea_len);
1307                 if (!ea_list ) {
1308                         talloc_destroy(ctx);
1309                         restore_case_semantics(conn, file_attributes);
1310                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1311                 }
1312         }
1313
1314         /*
1315          * If it's a request for a directory open, deal with it separately.
1316          */
1317
1318         if(create_options & FILE_DIRECTORY_FILE) {
1319
1320                 /* Can't open a temp directory. IFS kit test. */
1321                 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1322                         talloc_destroy(ctx);
1323                         restore_case_semantics(conn, file_attributes);
1324                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1325                 }
1326
1327                 oplock_request = 0;
1328
1329                 /*
1330                  * We will get a create directory here if the Win32
1331                  * app specified a security descriptor in the 
1332                  * CreateDirectory() call.
1333                  */
1334
1335                 fsp = open_directory(conn, fname, &sbuf,
1336                                         access_mask,
1337                                         share_access,
1338                                         create_disposition,
1339                                         create_options,
1340                                         &info);
1341                 if(!fsp) {
1342                         talloc_destroy(ctx);
1343                         restore_case_semantics(conn, file_attributes);
1344                         return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1345                 }
1346
1347         } else {
1348
1349                 /*
1350                  * Ordinary file case.
1351                  */
1352
1353                 fsp = open_file_ntcreate(conn,fname,&sbuf,
1354                                         access_mask,
1355                                         share_access,
1356                                         create_disposition,
1357                                         create_options,
1358                                         file_attributes,
1359                                         oplock_request,
1360                                         &info);
1361
1362                 if (!fsp) { 
1363                         if(errno == EISDIR) {
1364
1365                                 /*
1366                                  * Fail the open if it was explicitly a non-directory file.
1367                                  */
1368
1369                                 if (create_options & FILE_NON_DIRECTORY_FILE) {
1370                                         restore_case_semantics(conn, file_attributes);
1371                                         return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1372                                 }
1373         
1374                                 oplock_request = 0;
1375                                 fsp = open_directory(conn, fname, &sbuf,
1376                                                         access_mask,
1377                                                         share_access,
1378                                                         create_disposition,
1379                                                         create_options,
1380                                                         &info);
1381                                 if(!fsp) {
1382                                         talloc_destroy(ctx);
1383                                         restore_case_semantics(conn, file_attributes);
1384                                         return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1385                                 }
1386                         } else {
1387                                 talloc_destroy(ctx);
1388                                 restore_case_semantics(conn, file_attributes);
1389                                 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1390                                         /* We have re-scheduled this call. */
1391                                         return -1;
1392                                 }
1393                                 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1394                         }
1395                 } 
1396         }
1397
1398         /*
1399          * According to the MS documentation, the only time the security
1400          * descriptor is applied to the opened file is iff we *created* the
1401          * file; an existing file stays the same.
1402          * 
1403          * Also, it seems (from observation) that you can open the file with
1404          * any access mask but you can still write the sd. We need to override
1405          * the granted access before we call set_sd
1406          * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1407          */
1408
1409         if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1410                 uint32 saved_access_mask = fsp->access_mask;
1411
1412                 /* We have already checked that sd_len <= data_count here. */
1413
1414                 fsp->access_mask = FILE_GENERIC_ALL;
1415
1416                 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1417                 if (!NT_STATUS_IS_OK(status)) {
1418                         talloc_destroy(ctx);
1419                         close_file(fsp,False);
1420                         restore_case_semantics(conn, file_attributes);
1421                         return ERROR_NT(status);
1422                 }
1423                 fsp->access_mask = saved_access_mask;
1424         }
1425         
1426         if (ea_len && (info == FILE_WAS_CREATED)) {
1427                 status = set_ea(conn, fsp, fname, ea_list);
1428                 talloc_destroy(ctx);
1429                 if (!NT_STATUS_IS_OK(status)) {
1430                         close_file(fsp,False);
1431                         restore_case_semantics(conn, file_attributes);
1432                         return ERROR_NT(status);
1433                 }
1434         }
1435
1436         restore_case_semantics(conn, file_attributes);
1437
1438         file_len = sbuf.st_size;
1439         fattr = dos_mode(conn,fname,&sbuf);
1440         if(fattr == 0) {
1441                 fattr = FILE_ATTRIBUTE_NORMAL;
1442         }
1443         if (!fsp->is_directory && (fattr & aDIR)) {
1444                 close_file(fsp,False);
1445                 return ERROR_DOS(ERRDOS,ERRnoaccess);
1446         } 
1447         
1448         /* Save the requested allocation size. */
1449         if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1450                 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1451 #ifdef LARGE_SMB_OFF_T
1452                 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1453 #endif
1454                 if (allocation_size && (allocation_size > file_len)) {
1455                         fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1456                         if (fsp->is_directory) {
1457                                 close_file(fsp,False);
1458                                 /* Can't set allocation size on a directory. */
1459                                 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1460                         }
1461                         if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1462                                 close_file(fsp,False);
1463                                 return ERROR_NT(NT_STATUS_DISK_FULL);
1464                         }
1465                 } else {
1466                         fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1467                 }
1468         }
1469
1470         /* 
1471          * If the caller set the extended oplock request bit
1472          * and we granted one (by whatever means) - set the
1473          * correct bit for extended oplock reply.
1474          */
1475     
1476         if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1477                 extended_oplock_granted = True;
1478         }
1479   
1480         if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1481                 extended_oplock_granted = True;
1482         }
1483
1484         /* Realloc the size of parameters and data we will return */
1485         params = nttrans_realloc(ppparams, 69);
1486         if(params == NULL) {
1487                 return ERROR_DOS(ERRDOS,ERRnomem);
1488         }
1489
1490         p = params;
1491         if (extended_oplock_granted) {
1492                 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1493         } else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
1494                 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1495         } else {
1496                 SCVAL(p,0,NO_OPLOCK_RETURN);
1497         }
1498         
1499         p += 2;
1500         SSVAL(p,0,fsp->fnum);
1501         p += 2;
1502         if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1503                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1504         } else {
1505                 SIVAL(p,0,info);
1506         }
1507         p += 8;
1508
1509         /* Create time. */
1510         c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1511
1512         if (lp_dos_filetime_resolution(SNUM(conn))) {
1513                 c_time &= ~1;
1514                 sbuf.st_atime &= ~1;
1515                 sbuf.st_mtime &= ~1;
1516                 sbuf.st_mtime &= ~1;
1517         }
1518
1519         put_long_date(p,c_time);
1520         p += 8;
1521         put_long_date(p,sbuf.st_atime); /* access time */
1522         p += 8;
1523         put_long_date(p,sbuf.st_mtime); /* write time */
1524         p += 8;
1525         put_long_date(p,sbuf.st_mtime); /* change time */
1526         p += 8;
1527         SIVAL(p,0,fattr); /* File Attributes. */
1528         p += 4;
1529         SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1530         p += 8;
1531         SOFF_T(p,0,file_len);
1532         p += 8;
1533         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1534                 SSVAL(p,2,0x7);
1535         }
1536         p += 4;
1537         SCVAL(p,0,fsp->is_directory ? 1 : 0);
1538
1539         DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1540
1541         /* Send the required number of replies */
1542         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1543
1544         return -1;
1545 }
1546
1547 /****************************************************************************
1548  Reply to a NT CANCEL request.
1549 ****************************************************************************/
1550
1551 int reply_ntcancel(connection_struct *conn,
1552                    char *inbuf,char *outbuf,int length,int bufsize)
1553 {
1554         /*
1555          * Go through and cancel any pending change notifies.
1556          */
1557         
1558         int mid = SVAL(inbuf,smb_mid);
1559         START_PROFILE(SMBntcancel);
1560         remove_pending_change_notify_requests_by_mid(mid);
1561         remove_pending_lock_requests_by_mid(mid);
1562         srv_cancel_sign_response(mid);
1563         
1564         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1565
1566         END_PROFILE(SMBntcancel);
1567         return(-1);
1568 }
1569
1570 /****************************************************************************
1571  Copy a file.
1572 ****************************************************************************/
1573
1574 static NTSTATUS copy_internals(connection_struct *conn, char *oldname, char *newname, uint32 attrs)
1575 {
1576         BOOL bad_path_oldname = False;
1577         BOOL bad_path_newname = False;
1578         SMB_STRUCT_STAT sbuf1, sbuf2;
1579         pstring last_component_oldname;
1580         pstring last_component_newname;
1581         files_struct *fsp1,*fsp2;
1582         uint32 fattr;
1583         int info;
1584         SMB_OFF_T ret=-1;
1585         int close_ret;
1586         NTSTATUS status = NT_STATUS_OK;
1587
1588         ZERO_STRUCT(sbuf1);
1589         ZERO_STRUCT(sbuf2);
1590
1591         /* No wildcards. */
1592         if (ms_has_wild(newname) || ms_has_wild(oldname)) {
1593                 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1594         }
1595
1596         if (!CAN_WRITE(conn))
1597                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1598
1599         unix_convert(oldname,conn,last_component_oldname,&bad_path_oldname,&sbuf1);
1600         if (bad_path_oldname) {
1601                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1602         }
1603
1604         /* Quick check for "." and ".." */
1605         if (last_component_oldname[0] == '.') {
1606                 if (!last_component_oldname[1] || (last_component_oldname[1] == '.' && !last_component_oldname[2])) {
1607                         return NT_STATUS_OBJECT_NAME_INVALID;
1608                 }
1609         }
1610
1611         /* Source must already exist. */
1612         if (!VALID_STAT(sbuf1)) {
1613                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1614         }
1615         if (!check_name(oldname,conn)) {
1616                 return NT_STATUS_ACCESS_DENIED;
1617         }
1618
1619         /* Ensure attributes match. */
1620         fattr = dos_mode(conn,oldname,&sbuf1);
1621         if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1622                 return NT_STATUS_NO_SUCH_FILE;
1623         }
1624
1625         unix_convert(newname,conn,last_component_newname,&bad_path_newname,&sbuf2);
1626         if (bad_path_newname) {
1627                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1628         }
1629
1630         /* Quick check for "." and ".." */
1631         if (last_component_newname[0] == '.') {
1632                 if (!last_component_newname[1] || (last_component_newname[1] == '.' && !last_component_newname[2])) {
1633                         return NT_STATUS_OBJECT_NAME_INVALID;
1634                 }
1635         }
1636
1637         /* Disallow if newname already exists. */
1638         if (VALID_STAT(sbuf2)) {
1639                 return NT_STATUS_OBJECT_NAME_COLLISION;
1640         }
1641
1642         if (!check_name(newname,conn)) {
1643                 return NT_STATUS_ACCESS_DENIED;
1644         }
1645
1646         /* No links from a directory. */
1647         if (S_ISDIR(sbuf1.st_mode)) {
1648                 return NT_STATUS_FILE_IS_A_DIRECTORY;
1649         }
1650
1651         /* Ensure this is within the share. */
1652         if (!reduce_name(conn, oldname) != 0) {
1653                 return NT_STATUS_ACCESS_DENIED;
1654         }
1655
1656         DEBUG(10,("copy_internals: doing file copy %s to %s\n", oldname, newname));
1657
1658         fsp1 = open_file_ntcreate(conn,oldname,&sbuf1,
1659                         FILE_READ_DATA, /* Read-only. */
1660                         0, /* No sharing. */
1661                         FILE_OPEN,
1662                         0, /* No create options. */
1663                         FILE_ATTRIBUTE_NORMAL,
1664                         INTERNAL_OPEN_ONLY,
1665                         &info);
1666
1667         if (!fsp1) {
1668                 status = get_saved_ntstatus();
1669                 if (NT_STATUS_IS_OK(status)) {
1670                         status = NT_STATUS_ACCESS_DENIED;
1671                 }
1672                 set_saved_ntstatus(NT_STATUS_OK);
1673                 return status;
1674         }
1675
1676         fsp2 = open_file_ntcreate(conn,newname,&sbuf2,
1677                         FILE_WRITE_DATA, /* Read-only. */
1678                         0, /* No sharing. */
1679                         FILE_CREATE,
1680                         0, /* No create options. */
1681                         fattr,
1682                         INTERNAL_OPEN_ONLY,
1683                         &info);
1684
1685         if (!fsp2) {
1686                 status = get_saved_ntstatus();
1687                 if (NT_STATUS_IS_OK(status)) {
1688                         status = NT_STATUS_ACCESS_DENIED;
1689                 }
1690                 set_saved_ntstatus(NT_STATUS_OK);
1691                 close_file(fsp1,False);
1692                 return status;
1693         }
1694
1695         if (sbuf1.st_size) {
1696                 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1697         }
1698
1699         /*
1700          * As we are opening fsp1 read-only we only expect
1701          * an error on close on fsp2 if we are out of space.
1702          * Thus we don't look at the error return from the
1703          * close of fsp1.
1704          */
1705         close_file(fsp1,False);
1706
1707         /* Ensure the modtime is set correctly on the destination file. */
1708         fsp_set_pending_modtime(fsp2, sbuf1.st_mtime);
1709
1710         close_ret = close_file(fsp2,False);
1711
1712         /* Grrr. We have to do this as open_file_shared1 adds aARCH when it
1713            creates the file. This isn't the correct thing to do in the copy case. JRA */
1714         file_set_dosmode(conn, newname, fattr, &sbuf2, True);
1715
1716         if (ret < (SMB_OFF_T)sbuf1.st_size) {
1717                 return NT_STATUS_DISK_FULL;
1718         }
1719
1720         if (close_ret != 0) {
1721                 status = map_nt_error_from_unix(close_ret);
1722                 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1723                         nt_errstr(status), oldname, newname));
1724         }
1725         return status;
1726 }
1727
1728 /****************************************************************************
1729  Reply to a NT rename request.
1730 ****************************************************************************/
1731
1732 int reply_ntrename(connection_struct *conn,
1733                    char *inbuf,char *outbuf,int length,int bufsize)
1734 {
1735         int outsize = 0;
1736         pstring oldname;
1737         pstring newname;
1738         char *p;
1739         NTSTATUS status;
1740         BOOL path_contains_wcard = False;
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_wcard(inbuf, oldname, p, sizeof(oldname), 0, STR_TERMINATE, &status, &path_contains_wcard);
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);
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, path_contains_wcard);
1779                         break;
1780                 case RENAME_FLAG_HARD_LINK:
1781                         status = hardlink_internals(conn, oldname, newname);
1782                         break;
1783                 case RENAME_FLAG_COPY:
1784                         if (path_contains_wcard) {
1785                                 /* No wildcards. */
1786                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1787                         } else {
1788                                 status = copy_internals(conn, oldname, newname, attrs);
1789                         }
1790                         break;
1791                 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1792                         status = NT_STATUS_INVALID_PARAMETER;
1793                         break;
1794                 default:
1795                         status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1796                         break;
1797         }
1798
1799         if (!NT_STATUS_IS_OK(status)) {
1800                 END_PROFILE(SMBntrename);
1801                 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1802                         /* We have re-scheduled this call. */
1803                         return -1;
1804                 }
1805                 return ERROR_NT(status);
1806         }
1807
1808         /*
1809          * Win2k needs a changenotify request response before it will
1810          * update after a rename..
1811          */     
1812         process_pending_change_notify_queue((time_t)0);
1813         outsize = set_message(outbuf,0,0,True);
1814   
1815         END_PROFILE(SMBntrename);
1816         return(outsize);
1817 }
1818
1819 /****************************************************************************
1820  Reply to an unsolicited SMBNTtranss - just ignore it!
1821 ****************************************************************************/
1822
1823 int reply_nttranss(connection_struct *conn,
1824                    char *inbuf,char *outbuf,int length,int bufsize)
1825 {
1826         START_PROFILE(SMBnttranss);
1827         DEBUG(4,("Ignoring nttranss of length %d\n",length));
1828         END_PROFILE(SMBnttranss);
1829         return(-1);
1830 }
1831
1832 /****************************************************************************
1833  Reply to a notify change - queue the request and 
1834  don't allow a directory to be opened.
1835 ****************************************************************************/
1836
1837 static int call_nt_transact_notify_change(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
1838                                   char **ppsetup, uint32 setup_count,
1839                                   char **ppparams, uint32 parameter_count,
1840                                   char **ppdata, uint32 data_count, uint32 max_data_count)
1841 {
1842         char *setup = *ppsetup;
1843         files_struct *fsp;
1844         uint32 flags;
1845
1846         if(setup_count < 6) {
1847                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1848         }
1849
1850         fsp = file_fsp(setup,4);
1851         flags = IVAL(setup, 0);
1852
1853         DEBUG(3,("call_nt_transact_notify_change\n"));
1854
1855         if(!fsp) {
1856                 return ERROR_DOS(ERRDOS,ERRbadfid);
1857         }
1858
1859         if((!fsp->is_directory) || (conn != fsp->conn)) {
1860                 return ERROR_DOS(ERRDOS,ERRbadfid);
1861         }
1862
1863         if (!change_notify_set(inbuf, fsp, conn, flags)) {
1864                 return(UNIXERROR(ERRDOS,ERRbadfid));
1865         }
1866
1867         DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1868 name = %s\n", fsp->fsp_name ));
1869
1870         return -1;
1871 }
1872
1873 /****************************************************************************
1874  Reply to an NT transact rename command.
1875 ****************************************************************************/
1876
1877 static int call_nt_transact_rename(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1878                                   char **ppsetup, uint32 setup_count,
1879                                   char **ppparams, uint32 parameter_count,
1880                                   char **ppdata, uint32 data_count, uint32 max_data_count)
1881 {
1882         char *params = *ppparams;
1883         pstring new_name;
1884         files_struct *fsp = NULL;
1885         BOOL replace_if_exists = False;
1886         BOOL path_contains_wcard = False;
1887         NTSTATUS status;
1888
1889         if(parameter_count < 4) {
1890                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1891         }
1892
1893         fsp = file_fsp(params, 0);
1894         replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1895         CHECK_FSP(fsp, conn);
1896         srvstr_get_path_wcard(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE, &status, &path_contains_wcard);
1897         if (!NT_STATUS_IS_OK(status)) {
1898                 return ERROR_NT(status);
1899         }
1900
1901         status = rename_internals(conn, fsp->fsp_name,
1902                                   new_name, 0, replace_if_exists, path_contains_wcard);
1903         if (!NT_STATUS_IS_OK(status))
1904                 return ERROR_NT(status);
1905
1906         /*
1907          * Rename was successful.
1908          */
1909         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1910         
1911         DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n", 
1912                  fsp->fsp_name, new_name));
1913         
1914         /*
1915          * Win2k needs a changenotify request response before it will
1916          * update after a rename..
1917          */
1918         
1919         process_pending_change_notify_queue((time_t)0);
1920
1921         return -1;
1922 }
1923
1924 /******************************************************************************
1925  Fake up a completely empty SD.
1926 *******************************************************************************/
1927
1928 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1929 {
1930         size_t sd_size;
1931
1932         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1933         if(!*ppsd) {
1934                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1935                 sd_size = 0;
1936         }
1937
1938         return sd_size;
1939 }
1940
1941 /****************************************************************************
1942  Reply to query a security descriptor.
1943 ****************************************************************************/
1944
1945 static int call_nt_transact_query_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
1946                                   char **ppsetup, uint32 setup_count,
1947                                   char **ppparams, uint32 parameter_count,
1948                                   char **ppdata, uint32 data_count, uint32 max_data_count)
1949 {
1950         char *params = *ppparams;
1951         char *data = *ppdata;
1952         prs_struct pd;
1953         SEC_DESC *psd = NULL;
1954         size_t sd_size;
1955         uint32 security_info_wanted;
1956         TALLOC_CTX *mem_ctx;
1957         files_struct *fsp = NULL;
1958
1959         if(parameter_count < 8) {
1960                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1961         }
1962
1963         fsp = file_fsp(params,0);
1964         if(!fsp) {
1965                 return ERROR_DOS(ERRDOS,ERRbadfid);
1966         }
1967
1968         security_info_wanted = IVAL(params,4);
1969
1970         DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1971                         (unsigned int)security_info_wanted ));
1972
1973         params = nttrans_realloc(ppparams, 4);
1974         if(params == NULL) {
1975                 return ERROR_DOS(ERRDOS,ERRnomem);
1976         }
1977
1978         if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
1979                 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1980                 return ERROR_DOS(ERRDOS,ERRnomem);
1981         }
1982
1983         /*
1984          * Get the permissions to return.
1985          */
1986
1987         if (!lp_nt_acl_support(SNUM(conn))) {
1988                 sd_size = get_null_nt_acl(mem_ctx, &psd);
1989         } else {
1990                 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
1991         }
1992
1993         if (sd_size == 0) {
1994                 talloc_destroy(mem_ctx);
1995                 return(UNIXERROR(ERRDOS,ERRnoaccess));
1996         }
1997
1998         DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1999
2000         SIVAL(params,0,(uint32)sd_size);
2001
2002         if(max_data_count < sd_size) {
2003
2004                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
2005                         params, 4, *ppdata, 0);
2006                 talloc_destroy(mem_ctx);
2007                 return -1;
2008         }
2009
2010         /*
2011          * Allocate the data we will point this at.
2012          */
2013
2014         data = nttrans_realloc(ppdata, sd_size);
2015         if(data == NULL) {
2016                 talloc_destroy(mem_ctx);
2017                 return ERROR_DOS(ERRDOS,ERRnomem);
2018         }
2019
2020         /*
2021          * Init the parse struct we will marshall into.
2022          */
2023
2024         prs_init(&pd, 0, mem_ctx, MARSHALL);
2025
2026         /*
2027          * Setup the prs_struct to point at the memory we just
2028          * allocated.
2029          */
2030
2031         prs_give_memory( &pd, data, (uint32)sd_size, False);
2032
2033         /*
2034          * Finally, linearize into the outgoing buffer.
2035          */
2036
2037         if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2038                 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2039 security descriptor.\n"));
2040                 /*
2041                  * Return access denied for want of a better error message..
2042                  */ 
2043                 talloc_destroy(mem_ctx);
2044                 return(UNIXERROR(ERRDOS,ERRnoaccess));
2045         }
2046
2047         /*
2048          * Now we can delete the security descriptor.
2049          */
2050
2051         talloc_destroy(mem_ctx);
2052
2053         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
2054         return -1;
2055 }
2056
2057 /****************************************************************************
2058  Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2059 ****************************************************************************/
2060
2061 static int call_nt_transact_set_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2062                                   char **ppsetup, uint32 setup_count,
2063                                   char **ppparams, uint32 parameter_count,
2064                                   char **ppdata, uint32 data_count, uint32 max_data_count)
2065 {
2066         char *params= *ppparams;
2067         char *data = *ppdata;
2068         files_struct *fsp = NULL;
2069         uint32 security_info_sent = 0;
2070         NTSTATUS nt_status;
2071
2072         if(parameter_count < 8) {
2073                 return ERROR_DOS(ERRDOS,ERRbadfunc);
2074         }
2075
2076         if((fsp = file_fsp(params,0)) == NULL) {
2077                 return ERROR_DOS(ERRDOS,ERRbadfid);
2078         }
2079
2080         if(!lp_nt_acl_support(SNUM(conn))) {
2081                 goto done;
2082         }
2083
2084         security_info_sent = IVAL(params,4);
2085
2086         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2087                 (unsigned int)security_info_sent ));
2088
2089         if (data_count == 0) {
2090                 return ERROR_DOS(ERRDOS, ERRnoaccess);
2091         }
2092
2093         if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2094                 return ERROR_NT(nt_status);
2095         }
2096
2097   done:
2098
2099         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2100         return -1;
2101 }
2102    
2103 /****************************************************************************
2104  Reply to NT IOCTL
2105 ****************************************************************************/
2106
2107 static int call_nt_transact_ioctl(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
2108                                   char **ppsetup, uint32 setup_count,
2109                                   char **ppparams, uint32 parameter_count,
2110                                   char **ppdata, uint32 data_count, uint32 max_data_count)
2111 {
2112         uint32 function;
2113         uint16 fidnum;
2114         files_struct *fsp;
2115         uint8 isFSctl;
2116         uint8 compfilter;
2117         static BOOL logged_message;
2118         char *pdata = *ppdata;
2119
2120         if (setup_count != 8) {
2121                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2122                 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2123         }
2124
2125         function = IVAL(*ppsetup, 0);
2126         fidnum = SVAL(*ppsetup, 4);
2127         isFSctl = CVAL(*ppsetup, 6);
2128         compfilter = CVAL(*ppsetup, 7);
2129
2130         DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
2131                  function, fidnum, isFSctl, compfilter));
2132
2133         fsp=file_fsp(*ppsetup, 4);
2134         /* this check is done in each implemented function case for now
2135            because I don't want to break anything... --metze
2136         FSP_BELONGS_CONN(fsp,conn);*/
2137
2138         switch (function) {
2139         case FSCTL_SET_SPARSE:
2140                 /* pretend this succeeded - tho strictly we should
2141                    mark the file sparse (if the local fs supports it)
2142                    so we can know if we need to pre-allocate or not */
2143
2144                 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2145                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2146                 return -1;
2147         
2148         case FSCTL_0x000900C0:
2149                 /* pretend this succeeded - don't know what this really is
2150                    but works ok like this --metze
2151                  */
2152
2153                 DEBUG(10,("FSCTL_0x000900C0: called on FID[0x%04X](but not implemented)\n",fidnum));
2154                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2155                 return -1;
2156
2157         case FSCTL_GET_REPARSE_POINT:
2158                 /* pretend this fail - my winXP does it like this
2159                  * --metze
2160                  */
2161
2162                 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2163                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
2164                 return -1;
2165
2166         case FSCTL_SET_REPARSE_POINT:
2167                 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2168                  * --metze
2169                  */
2170
2171                 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2172                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
2173                 return -1;
2174                         
2175         case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2176         {
2177                 /*
2178                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2179                  * and return their volume names.  If max_data_count is 16, then it is just
2180                  * asking for the number of volumes and length of the combined names.
2181                  *
2182                  * pdata is the data allocated by our caller, but that uses
2183                  * total_data_count (which is 0 in our case) rather than max_data_count.
2184                  * Allocate the correct amount and return the pointer to let
2185                  * it be deallocated when we return.
2186                  */
2187                 SHADOW_COPY_DATA *shadow_data = NULL;
2188                 TALLOC_CTX *shadow_mem_ctx = NULL;
2189                 BOOL labels = False;
2190                 uint32 labels_data_count = 0;
2191                 uint32 i;
2192                 char *cur_pdata;
2193
2194                 FSP_BELONGS_CONN(fsp,conn);
2195
2196                 if (max_data_count < 16) {
2197                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2198                                 max_data_count));
2199                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2200                 }
2201
2202                 if (max_data_count > 16) {
2203                         labels = True;
2204                 }
2205
2206                 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2207                 if (shadow_mem_ctx == NULL) {
2208                         DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2209                         return ERROR_NT(NT_STATUS_NO_MEMORY);
2210                 }
2211
2212                 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2213                 if (shadow_data == NULL) {
2214                         DEBUG(0,("talloc_zero() failed!\n"));
2215                         talloc_destroy(shadow_mem_ctx);
2216                         return ERROR_NT(NT_STATUS_NO_MEMORY);
2217                 }
2218                 
2219                 shadow_data->mem_ctx = shadow_mem_ctx;
2220                 
2221                 /*
2222                  * Call the VFS routine to actually do the work.
2223                  */
2224                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2225                         talloc_destroy(shadow_data->mem_ctx);
2226                         if (errno == ENOSYS) {
2227                                 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n", 
2228                                         conn->connectpath));
2229                                 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2230                         } else {
2231                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n", 
2232                                         conn->connectpath));
2233                                 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);                        
2234                         }
2235                 }
2236
2237                 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2238
2239                 if (!labels) {
2240                         data_count = 16;
2241                 } else {
2242                         data_count = 12+labels_data_count+4;
2243                 }
2244
2245                 if (max_data_count<data_count) {
2246                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2247                                 max_data_count,data_count));
2248                         talloc_destroy(shadow_data->mem_ctx);
2249                         return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
2250                 }
2251
2252                 pdata = nttrans_realloc(ppdata, data_count);
2253                 if (pdata == NULL) {
2254                         talloc_destroy(shadow_data->mem_ctx);
2255                         return ERROR_NT(NT_STATUS_NO_MEMORY);
2256                 }               
2257
2258                 cur_pdata = pdata;
2259
2260                 /* num_volumes 4 bytes */
2261                 SIVAL(pdata,0,shadow_data->num_volumes);
2262
2263                 if (labels) {
2264                         /* num_labels 4 bytes */
2265                         SIVAL(pdata,4,shadow_data->num_volumes);
2266                 }
2267
2268                 /* needed_data_count 4 bytes */
2269                 SIVAL(pdata,8,labels_data_count);
2270
2271                 cur_pdata+=12;
2272
2273                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2274                         shadow_data->num_volumes,fsp->fsp_name));
2275                 if (labels && shadow_data->labels) {
2276                         for (i=0;i<shadow_data->num_volumes;i++) {
2277                                 srvstr_push(outbuf, cur_pdata, shadow_data->labels[i], 2*sizeof(SHADOW_COPY_LABEL), STR_UNICODE|STR_TERMINATE);
2278                                 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2279                                 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2280                         }
2281                 }
2282
2283                 talloc_destroy(shadow_data->mem_ctx);
2284
2285                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, pdata, data_count);
2286
2287                 return -1;
2288         }
2289         
2290         case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2291         {
2292                 /* pretend this succeeded - 
2293                  * 
2294                  * we have to send back a list with all files owned by this SID
2295                  *
2296                  * but I have to check that --metze
2297                  */
2298                 DOM_SID sid;
2299                 uid_t uid;
2300                 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2301                 
2302                 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2303
2304                 FSP_BELONGS_CONN(fsp,conn);
2305
2306                 /* unknown 4 bytes: this is not the length of the sid :-(  */
2307                 /*unknown = IVAL(pdata,0);*/
2308                 
2309                 sid_parse(pdata+4,sid_len,&sid);
2310                 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2311
2312                 if (!NT_STATUS_IS_OK(sid_to_uid(&sid, &uid))) {
2313                         DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2314                                 sid_string_static(&sid),(unsigned long)sid_len));
2315                         uid = (-1);
2316                 }
2317                 
2318                 /* we can take a look at the find source :-)
2319                  *
2320                  * find ./ -uid $uid  -name '*'   is what we need here
2321                  *
2322                  *
2323                  * and send 4bytes len and then NULL terminated unicode strings
2324                  * for each file
2325                  *
2326                  * but I don't know how to deal with the paged results
2327                  * (maybe we can hang the result anywhere in the fsp struct)
2328                  *
2329                  * we don't send all files at once
2330                  * and at the next we should *not* start from the beginning, 
2331                  * so we have to cache the result 
2332                  *
2333                  * --metze
2334                  */
2335                 
2336                 /* this works for now... */
2337                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2338                 return -1;      
2339         }       
2340         default:
2341                 if (!logged_message) {
2342                         logged_message = True; /* Only print this once... */
2343                         DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2344                                  function));
2345                 }
2346         }
2347
2348         return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2349 }
2350
2351
2352 #ifdef HAVE_SYS_QUOTAS
2353 /****************************************************************************
2354  Reply to get user quota 
2355 ****************************************************************************/
2356
2357 static int call_nt_transact_get_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
2358                                   char **ppsetup, uint32 setup_count,
2359                                   char **ppparams, uint32 parameter_count,
2360                                   char **ppdata, uint32 data_count, uint32 max_data_count)
2361 {
2362         NTSTATUS nt_status = NT_STATUS_OK;
2363         char *params = *ppparams;
2364         char *pdata = *ppdata;
2365         char *entry;
2366         int data_len=0,param_len=0;
2367         int qt_len=0;
2368         int entry_len = 0;
2369         files_struct *fsp = NULL;
2370         uint16 level = 0;
2371         size_t sid_len;
2372         DOM_SID sid;
2373         BOOL start_enum = True;
2374         SMB_NTQUOTA_STRUCT qt;
2375         SMB_NTQUOTA_LIST *tmp_list;
2376         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2377         extern struct current_user current_user;
2378
2379         ZERO_STRUCT(qt);
2380
2381         /* access check */
2382         if (current_user.uid != 0) {
2383                 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2384                         lp_servicename(SNUM(conn)),conn->user));
2385                 return ERROR_DOS(ERRDOS,ERRnoaccess);
2386         }
2387
2388         /*
2389          * Ensure minimum number of parameters sent.
2390          */
2391
2392         if (parameter_count < 4) {
2393                 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2394                 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2395         }
2396         
2397         /* maybe we can check the quota_fnum */
2398         fsp = file_fsp(params,0);
2399         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2400                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2401                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2402         }
2403
2404         /* the NULL pointer cheking for fsp->fake_file_handle->pd
2405          * is done by CHECK_NTQUOTA_HANDLE_OK()
2406          */
2407         qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2408
2409         level = SVAL(params,2);
2410         
2411         /* unknown 12 bytes leading in params */ 
2412         
2413         switch (level) {
2414                 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2415                         /* seems that we should continue with the enum here --metze */
2416
2417                         if (qt_handle->quota_list!=NULL && 
2418                             qt_handle->tmp_list==NULL) {
2419                 
2420                                 /* free the list */
2421                                 free_ntquota_list(&(qt_handle->quota_list));
2422
2423                                 /* Realloc the size of parameters and data we will return */
2424                                 param_len = 4;
2425                                 params = nttrans_realloc(ppparams, param_len);
2426                                 if(params == NULL) {
2427                                         return ERROR_DOS(ERRDOS,ERRnomem);
2428                                 }
2429
2430                                 data_len = 0;
2431                                 SIVAL(params,0,data_len);
2432
2433                                 break;
2434                         }
2435
2436                         start_enum = False;
2437
2438                 case TRANSACT_GET_USER_QUOTA_LIST_START:
2439
2440                         if (qt_handle->quota_list==NULL &&
2441                                 qt_handle->tmp_list==NULL) {
2442                                 start_enum = True;
2443                         }
2444
2445                         if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
2446                                 return ERROR_DOS(ERRSRV,ERRerror);
2447
2448                         /* Realloc the size of parameters and data we will return */
2449                         param_len = 4;
2450                         params = nttrans_realloc(ppparams, param_len);
2451                         if(params == NULL) {
2452                                 return ERROR_DOS(ERRDOS,ERRnomem);
2453                         }
2454
2455                         /* we should not trust the value in max_data_count*/
2456                         max_data_count = MIN(max_data_count,2048);
2457                         
2458                         pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2459                         if(pdata == NULL) {
2460                                 return ERROR_DOS(ERRDOS,ERRnomem);
2461                         }
2462
2463                         entry = pdata;
2464
2465                         /* set params Size of returned Quota Data 4 bytes*/
2466                         /* but set it later when we know it */
2467                 
2468                         /* for each entry push the data */
2469
2470                         if (start_enum) {
2471                                 qt_handle->tmp_list = qt_handle->quota_list;
2472                         }
2473
2474                         tmp_list = qt_handle->tmp_list;
2475
2476                         for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2477                                 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2478
2479                                 sid_len = sid_size(&tmp_list->quotas->sid);
2480                                 entry_len = 40 + sid_len;
2481
2482                                 /* nextoffset entry 4 bytes */
2483                                 SIVAL(entry,0,entry_len);
2484                 
2485                                 /* then the len of the SID 4 bytes */
2486                                 SIVAL(entry,4,sid_len);
2487                                 
2488                                 /* unknown data 8 bytes SMB_BIG_UINT */
2489                                 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2490                                 
2491                                 /* the used disk space 8 bytes SMB_BIG_UINT */
2492                                 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2493                                 
2494                                 /* the soft quotas 8 bytes SMB_BIG_UINT */
2495                                 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2496                                 
2497                                 /* the hard quotas 8 bytes SMB_BIG_UINT */
2498                                 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2499                                 
2500                                 /* and now the SID */
2501                                 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2502                         }
2503                         
2504                         qt_handle->tmp_list = tmp_list;
2505                         
2506                         /* overwrite the offset of the last entry */
2507                         SIVAL(entry-entry_len,0,0);
2508
2509                         data_len = 4+qt_len;
2510                         /* overwrite the params quota_data_len */
2511                         SIVAL(params,0,data_len);
2512
2513                         break;
2514
2515                 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2516                         
2517                         /* unknown 4 bytes IVAL(pdata,0) */     
2518                         
2519                         if (data_count < 8) {
2520                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2521                                 return ERROR_DOS(ERRDOS,ERRunknownlevel);                               
2522                         }
2523
2524                         sid_len = IVAL(pdata,4);
2525                         /* Ensure this is less than 1mb. */
2526                         if (sid_len > (1024*1024)) {
2527                                 return ERROR_DOS(ERRDOS,ERRnomem);
2528                         }
2529
2530                         if (data_count < 8+sid_len) {
2531                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2532                                 return ERROR_DOS(ERRDOS,ERRunknownlevel);                               
2533                         }
2534
2535                         data_len = 4+40+sid_len;
2536
2537                         if (max_data_count < data_len) {
2538                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2539                                         max_data_count, data_len));
2540                                 param_len = 4;
2541                                 SIVAL(params,0,data_len);
2542                                 data_len = 0;
2543                                 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2544                                 break;
2545                         }
2546
2547                         sid_parse(pdata+8,sid_len,&sid);
2548                 
2549                         if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2550                                 ZERO_STRUCT(qt);
2551                                 /* 
2552                                  * we have to return zero's in all fields 
2553                                  * instead of returning an error here
2554                                  * --metze
2555                                  */
2556                         }
2557
2558                         /* Realloc the size of parameters and data we will return */
2559                         param_len = 4;
2560                         params = nttrans_realloc(ppparams, param_len);
2561                         if(params == NULL) {
2562                                 return ERROR_DOS(ERRDOS,ERRnomem);
2563                         }
2564
2565                         pdata = nttrans_realloc(ppdata, data_len);
2566                         if(pdata == NULL) {
2567                                 return ERROR_DOS(ERRDOS,ERRnomem);
2568                         }
2569
2570                         entry = pdata;
2571
2572                         /* set params Size of returned Quota Data 4 bytes*/
2573                         SIVAL(params,0,data_len);
2574         
2575                         /* nextoffset entry 4 bytes */
2576                         SIVAL(entry,0,0);
2577         
2578                         /* then the len of the SID 4 bytes */
2579                         SIVAL(entry,4,sid_len);
2580                         
2581                         /* unknown data 8 bytes SMB_BIG_UINT */
2582                         SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2583                         
2584                         /* the used disk space 8 bytes SMB_BIG_UINT */
2585                         SBIG_UINT(entry,16,qt.usedspace);
2586                         
2587                         /* the soft quotas 8 bytes SMB_BIG_UINT */
2588                         SBIG_UINT(entry,24,qt.softlim);
2589                         
2590                         /* the hard quotas 8 bytes SMB_BIG_UINT */
2591                         SBIG_UINT(entry,32,qt.hardlim);
2592                         
2593                         /* and now the SID */
2594                         sid_linearize(entry+40, sid_len, &sid);
2595
2596                         break;
2597
2598                 default:
2599                         DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2600                         return ERROR_DOS(ERRSRV,ERRerror);
2601                         break;
2602         }
2603
2604         send_nt_replies(inbuf, outbuf, bufsize, nt_status, params, param_len, pdata, data_len);
2605
2606         return -1;
2607 }
2608
2609 /****************************************************************************
2610  Reply to set user quota
2611 ****************************************************************************/
2612
2613 static int call_nt_transact_set_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize, 
2614                                   char **ppsetup, uint32 setup_count,
2615                                   char **ppparams, uint32 parameter_count,
2616                                   char **ppdata, uint32 data_count, uint32 max_data_count)
2617 {
2618         char *params = *ppparams;
2619         char *pdata = *ppdata;
2620         int data_len=0,param_len=0;
2621         SMB_NTQUOTA_STRUCT qt;
2622         size_t sid_len;
2623         DOM_SID sid;
2624         files_struct *fsp = NULL;
2625
2626         ZERO_STRUCT(qt);
2627
2628         /* access check */
2629         if (current_user.uid != 0) {
2630                 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2631                         lp_servicename(SNUM(conn)),conn->user));
2632                 return ERROR_DOS(ERRDOS,ERRnoaccess);
2633         }
2634
2635         /*
2636          * Ensure minimum number of parameters sent.
2637          */
2638
2639         if (parameter_count < 2) {
2640                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2641                 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2642         }
2643         
2644         /* maybe we can check the quota_fnum */
2645         fsp = file_fsp(params,0);
2646         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2647                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2648                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2649         }
2650
2651         if (data_count < 40) {
2652                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2653                 return ERROR_DOS(ERRDOS,ERRunknownlevel);               
2654         }
2655
2656         /* offset to next quota record.
2657          * 4 bytes IVAL(pdata,0)
2658          * unused here...
2659          */
2660
2661         /* sid len */
2662         sid_len = IVAL(pdata,4);
2663
2664         if (data_count < 40+sid_len) {
2665                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2666                 return ERROR_DOS(ERRDOS,ERRunknownlevel);               
2667         }
2668
2669         /* unknown 8 bytes in pdata 
2670          * maybe its the change time in NTTIME
2671          */
2672
2673         /* the used space 8 bytes (SMB_BIG_UINT)*/
2674         qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2675 #ifdef LARGE_SMB_OFF_T
2676         qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2677 #else /* LARGE_SMB_OFF_T */
2678         if ((IVAL(pdata,20) != 0)&&
2679                 ((qt.usedspace != 0xFFFFFFFF)||
2680                 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2681                 /* more than 32 bits? */
2682                 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2683         }
2684 #endif /* LARGE_SMB_OFF_T */
2685
2686         /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2687         qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2688 #ifdef LARGE_SMB_OFF_T
2689         qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2690 #else /* LARGE_SMB_OFF_T */
2691         if ((IVAL(pdata,28) != 0)&&
2692                 ((qt.softlim != 0xFFFFFFFF)||
2693                 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2694                 /* more than 32 bits? */
2695                 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2696         }
2697 #endif /* LARGE_SMB_OFF_T */
2698
2699         /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2700         qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2701 #ifdef LARGE_SMB_OFF_T
2702         qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2703 #else /* LARGE_SMB_OFF_T */
2704         if ((IVAL(pdata,36) != 0)&&
2705                 ((qt.hardlim != 0xFFFFFFFF)||
2706                 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2707                 /* more than 32 bits? */
2708                 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2709         }
2710 #endif /* LARGE_SMB_OFF_T */
2711         
2712         sid_parse(pdata+40,sid_len,&sid);
2713         DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2714
2715         /* 44 unknown bytes left... */
2716
2717         if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2718                 return ERROR_DOS(ERRSRV,ERRerror);      
2719         }
2720
2721         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, pdata, data_len);
2722
2723         return -1;
2724 }
2725 #endif /* HAVE_SYS_QUOTAS */
2726
2727 /****************************************************************************
2728  Reply to a SMBNTtrans.
2729 ****************************************************************************/
2730
2731 int reply_nttrans(connection_struct *conn,
2732                         char *inbuf,char *outbuf,int length,int bufsize)
2733 {
2734         int  outsize = 0;
2735         uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2736 #if 0 /* Not used. */
2737         uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
2738         uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
2739 #endif /* Not used. */
2740         uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
2741         uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
2742         uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
2743         uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
2744         uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
2745         uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
2746         uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
2747         uint16 function_code = SVAL( inbuf, smb_nt_Function);
2748         char *params = NULL, *data = NULL, *setup = NULL;
2749         uint32 num_params_sofar, num_data_sofar;
2750         START_PROFILE(SMBnttrans);
2751
2752         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2753                 END_PROFILE(SMBnttrans);
2754                 return ERROR_DOS(ERRSRV,ERRaccess);
2755         }
2756
2757         outsize = set_message(outbuf,0,0,True);
2758
2759         /* 
2760          * All nttrans messages we handle have smb_wct == 19 + setup_count.
2761          * Ensure this is so as a sanity check.
2762          */
2763
2764         if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
2765                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2766                         CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
2767                 goto bad_param;
2768         }
2769
2770         /* Don't allow more than 128mb for each value. */
2771         if ((total_parameter_count > (1024*1024*128)) || (total_data_count > (1024*1024*128))) {
2772                 END_PROFILE(SMBnttrans);
2773                 return ERROR_DOS(ERRDOS,ERRnomem);
2774         }
2775
2776         /* Allocate the space for the setup, the maximum needed parameters and data */
2777
2778         if(setup_count > 0) {
2779                 setup = (char *)SMB_MALLOC(setup_count);
2780         }
2781         if (total_parameter_count > 0) {
2782                 params = (char *)SMB_MALLOC(total_parameter_count);
2783         }
2784         if (total_data_count > 0) {
2785                 data = (char *)SMB_MALLOC(total_data_count);
2786         }
2787  
2788         if ((total_parameter_count && !params)  || (total_data_count && !data) ||
2789                                 (setup_count && !setup)) {
2790                 SAFE_FREE(setup);
2791                 SAFE_FREE(params);
2792                 SAFE_FREE(data);
2793                 DEBUG(0,("reply_nttrans : Out of memory\n"));
2794                 END_PROFILE(SMBnttrans);
2795                 return ERROR_DOS(ERRDOS,ERRnomem);
2796         }
2797
2798         /* Copy the param and data bytes sent with this request into the params buffer */
2799         num_params_sofar = parameter_count;
2800         num_data_sofar = data_count;
2801
2802         if (parameter_count > total_parameter_count || data_count > total_data_count)
2803                 goto bad_param;
2804
2805         if(setup) {
2806                 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
2807                 if ((smb_nt_SetupStart + setup_count < smb_nt_SetupStart) ||
2808                                 (smb_nt_SetupStart + setup_count < setup_count)) {
2809                         goto bad_param;
2810                 }
2811                 if (smb_nt_SetupStart + setup_count > length) {
2812                         goto bad_param;
2813                 }
2814
2815                 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
2816                 dump_data(10, setup, setup_count);
2817         }
2818         if(params) {
2819                 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
2820                 if ((parameter_offset + parameter_count < parameter_offset) ||
2821                                 (parameter_offset + parameter_count < parameter_count)) {
2822                         goto bad_param;
2823                 }
2824                 if ((smb_base(inbuf) + parameter_offset + parameter_count > inbuf + length)||
2825                                 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf))) {
2826                         goto bad_param;
2827                 }
2828
2829                 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
2830                 dump_data(10, params, parameter_count);
2831         }
2832         if(data) {
2833                 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
2834                 if ((data_offset + data_count < data_offset) || (data_offset + data_count < data_count)) {
2835                         goto bad_param;
2836                 }
2837                 if ((smb_base(inbuf) + data_offset + data_count > inbuf + length) ||
2838                                 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf))) {
2839                         goto bad_param;
2840                 }
2841
2842                 memcpy( data, smb_base(inbuf) + data_offset, data_count);
2843                 dump_data(10, data, data_count);
2844         }
2845
2846         srv_signing_trans_start(SVAL(inbuf,smb_mid));
2847
2848         if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2849                 /* We need to send an interim response then receive the rest
2850                         of the parameter/data bytes */
2851                 outsize = set_message(outbuf,0,0,True);
2852                 srv_signing_trans_stop();
2853                 show_msg(outbuf);
2854                 if (!send_smb(smbd_server_fd(),outbuf)) {
2855                         exit_server("reply_nttrans: send_smb failed.");
2856                 }
2857
2858                 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2859                         BOOL ret;
2860                         uint32 parameter_displacement;
2861                         uint32 data_displacement;
2862
2863                         ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
2864
2865                         /* We need to re-calcuate the new length after we've read the secondary packet. */
2866                         length = smb_len(inbuf) + 4;
2867
2868                         /*
2869                          * The sequence number for the trans reply is always
2870                          * based on the last secondary received.
2871                          */
2872
2873                         srv_signing_trans_start(SVAL(inbuf,smb_mid));
2874
2875                         if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
2876                                 outsize = set_message(outbuf,0,0,True);
2877                                 if(ret) {
2878                                         DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
2879                                 } else {
2880                                         DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
2881                                                 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
2882                                 }
2883                                 goto bad_param;
2884                         }
2885       
2886                         /* Revise total_params and total_data in case they have changed downwards */
2887                         if (IVAL(inbuf, smb_nts_TotalParameterCount) < total_parameter_count) {
2888                                 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
2889                         }
2890                         if (IVAL(inbuf, smb_nts_TotalDataCount) < total_data_count) {
2891                                 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
2892                         }
2893
2894                         parameter_count = IVAL(inbuf,smb_nts_ParameterCount);
2895                         parameter_offset = IVAL(inbuf, smb_nts_ParameterOffset);
2896                         parameter_displacement = IVAL(inbuf, smb_nts_ParameterDisplacement);
2897                         num_params_sofar += parameter_count;
2898
2899                         data_count = IVAL(inbuf, smb_nts_DataCount);
2900                         data_displacement = IVAL(inbuf, smb_nts_DataDisplacement);
2901                         data_offset = IVAL(inbuf, smb_nts_DataOffset);
2902                         num_data_sofar += data_count;
2903
2904                         if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count) {
2905                                 DEBUG(0,("reply_nttrans2: data overflow in secondary nttrans packet"));
2906                                 goto bad_param;
2907                         }
2908
2909                         if (parameter_count) {
2910                                 if (parameter_displacement + parameter_count > total_parameter_count) {
2911                                         goto bad_param;
2912                                 }
2913                                 if ((parameter_displacement + parameter_count < parameter_displacement) ||
2914                                                 (parameter_displacement + parameter_count < parameter_count)) {
2915                                         goto bad_param;
2916                                 }
2917                                 if (parameter_displacement > total_parameter_count) {
2918                                         goto bad_param;
2919                                 }
2920                                 if ((smb_base(inbuf) + parameter_offset + parameter_count > inbuf + length) ||
2921                                                 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf))) {
2922                                         goto bad_param;
2923                                 }
2924                                 if (parameter_displacement + params < params) {
2925                                         goto bad_param;
2926                                 }
2927
2928                                 memcpy( &params[parameter_displacement], smb_base(inbuf) + parameter_offset, parameter_count);
2929                         }
2930
2931                         if (data_count) {
2932                                 if (data_displacement + data_count > total_data_count) {
2933                                         goto bad_param;
2934                                 }
2935                                 if ((data_displacement + data_count < data_displacement) ||
2936                                                 (data_displacement + data_count < data_count)) {
2937                                         goto bad_param;
2938                                 }
2939                                 if (data_displacement > total_data_count) {
2940                                         goto bad_param;
2941                                 }
2942                                 if ((smb_base(inbuf) + data_offset + data_count > inbuf + length) ||
2943                                                 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf))) {
2944                                         goto bad_param;
2945                                 }
2946                                 if (data_displacement + data < data) {
2947                                         goto bad_param;
2948                                 }
2949
2950                                 memcpy( &data[data_displacement], smb_base(inbuf)+ data_offset, data_count);
2951                         }
2952                 }
2953         }
2954
2955         if (Protocol >= PROTOCOL_NT1) {
2956                 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | FLAGS2_IS_LONG_NAME);
2957         }
2958
2959         /* Now we must call the relevant NT_TRANS function */
2960         switch(function_code) {
2961                 case NT_TRANSACT_CREATE:
2962                         START_PROFILE_NESTED(NT_transact_create);
2963                         outsize = call_nt_transact_create(conn, inbuf, outbuf,
2964                                                         length, bufsize, 
2965                                                         &setup, setup_count,
2966                                                         &params, total_parameter_count, 
2967                                                         &data, total_data_count, max_data_count);
2968                         END_PROFILE_NESTED(NT_transact_create);
2969                         break;
2970                 case NT_TRANSACT_IOCTL:
2971                         START_PROFILE_NESTED(NT_transact_ioctl);
2972                         outsize = call_nt_transact_ioctl(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_ioctl);
2978                         break;
2979                 case NT_TRANSACT_SET_SECURITY_DESC:
2980                         START_PROFILE_NESTED(NT_transact_set_security_desc);
2981                         outsize = call_nt_transact_set_security_desc(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_set_security_desc);
2987                         break;
2988                 case NT_TRANSACT_NOTIFY_CHANGE:
2989                         START_PROFILE_NESTED(NT_transact_notify_change);
2990                         outsize = call_nt_transact_notify_change(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_notify_change);
2996                         break;
2997                 case NT_TRANSACT_RENAME:
2998                         START_PROFILE_NESTED(NT_transact_rename);
2999                         outsize = call_nt_transact_rename(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_rename);
3005                         break;
3006
3007                 case NT_TRANSACT_QUERY_SECURITY_DESC:
3008                         START_PROFILE_NESTED(NT_transact_query_security_desc);
3009                         outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf, 
3010                                                          length, bufsize, 
3011                                                          &setup, setup_count,
3012                                                          &params, total_parameter_count, 
3013                                                          &data, total_data_count, max_data_count);
3014                         END_PROFILE_NESTED(NT_transact_query_security_desc);
3015                         break;
3016 #ifdef HAVE_SYS_QUOTAS
3017                 case NT_TRANSACT_GET_USER_QUOTA:
3018                         START_PROFILE_NESTED(NT_transact_get_user_quota);
3019                         outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf, 
3020                                                          length, bufsize, 
3021                                                          &setup, setup_count,
3022                                                          &params, total_parameter_count, 
3023                                                          &data, total_data_count, max_data_count);
3024                         END_PROFILE_NESTED(NT_transact_get_user_quota);
3025                         break;
3026                 case NT_TRANSACT_SET_USER_QUOTA:
3027                         START_PROFILE_NESTED(NT_transact_set_user_quota);
3028                         outsize = call_nt_transact_set_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_set_user_quota);
3034                         break;                                  
3035 #endif /* HAVE_SYS_QUOTAS */
3036                 default:
3037                         /* Error in request */
3038                         DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
3039                         SAFE_FREE(setup);
3040                         SAFE_FREE(params);
3041                         SAFE_FREE(data);
3042                         END_PROFILE(SMBnttrans);
3043                         srv_signing_trans_stop();
3044                         return ERROR_DOS(ERRSRV,ERRerror);
3045         }
3046
3047         /* As we do not know how many data packets will need to be
3048                 returned here the various call_nt_transact_xxxx calls
3049                 must send their own. Thus a call_nt_transact_xxxx routine only
3050                 returns a value other than -1 when it wants to send
3051                 an error packet. 
3052         */
3053
3054         srv_signing_trans_stop();
3055
3056         SAFE_FREE(setup);
3057         SAFE_FREE(params);
3058         SAFE_FREE(data);
3059         END_PROFILE(SMBnttrans);
3060         return outsize; /* If a correct response was needed the call_nt_transact_xxxx 
3061                                 calls have already sent it. If outsize != -1 then it is
3062                                 returning an error packet. */
3063
3064  bad_param:
3065
3066         srv_signing_trans_stop();
3067         SAFE_FREE(params);
3068         SAFE_FREE(data);
3069         SAFE_FREE(setup);
3070         END_PROFILE(SMBnttrans);
3071         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
3072 }