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