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