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