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