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