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