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