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