an initial fix for handling sparse files in smbd
[import/samba-cvsimport.git] / source / smbd / nttrans.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB NT transaction handling
4    Copyright (C) Jeremy Allison 1994-1998
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 extern int Protocol;
24 extern int smb_read_error;
25 extern int global_oplock_break;
26 extern BOOL case_sensitive;
27 extern BOOL case_preserve;
28 extern BOOL short_case_preserve;
29
30 static char *known_nt_pipes[] = {
31         "\\LANMAN",
32         "\\srvsvc",
33         "\\samr",
34         "\\wkssvc",
35         "\\NETLOGON",
36         "\\ntlsa",
37         "\\ntsvcs",
38         "\\lsass",
39         "\\lsarpc",
40         "\\winreg",
41         "\\spoolss",
42         "\\netdfs",
43         NULL
44 };
45
46 /* Map generic permissions to file object specific permissions */
47  
48 struct generic_mapping file_generic_mapping = {
49         FILE_GENERIC_READ,
50         FILE_GENERIC_WRITE,
51         FILE_GENERIC_EXECUTE,
52         FILE_GENERIC_ALL
53 };
54
55 /****************************************************************************
56  Send the required number of replies back.
57  We assume all fields other than the data fields are
58  set correctly for the type of call.
59  HACK ! Always assumes smb_setup field is zero.
60 ****************************************************************************/
61
62 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, NTSTATUS nt_error, char *params,
63                            int paramsize, char *pdata, int datasize)
64 {
65         extern int max_send;
66         int data_to_send = datasize;
67         int params_to_send = paramsize;
68         int useable_space;
69         char *pp = params;
70         char *pd = pdata;
71         int params_sent_thistime, data_sent_thistime, total_sent_thistime;
72         int alignment_offset = 3;
73         int data_alignment_offset = 0;
74
75         /*
76          * Initially set the wcnt area to be 18 - this is true for all
77          * transNT replies.
78          */
79
80         set_message(outbuf,18,0,True);
81
82         if (NT_STATUS_V(nt_error))
83                 ERROR_NT(nt_error);
84
85         /* 
86          * If there genuinely are no parameters or data to send just send
87          * the empty packet.
88          */
89
90         if(params_to_send == 0 && data_to_send == 0) {
91                 if (!send_smb(smbd_server_fd(),outbuf))
92                         exit_server("send_nt_replies: send_smb failed.");
93                 return 0;
94         }
95
96         /*
97          * When sending params and data ensure that both are nicely aligned.
98          * Only do this alignment when there is also data to send - else
99          * can cause NT redirector problems.
100          */
101
102         if (((params_to_send % 4) != 0) && (data_to_send != 0))
103                 data_alignment_offset = 4 - (params_to_send % 4);
104
105         /* 
106          * Space is bufsize minus Netbios over TCP header minus SMB header.
107          * The alignment_offset is to align the param bytes on a four byte
108          * boundary (2 bytes for data len, one byte pad). 
109          * NT needs this to work correctly.
110          */
111
112         useable_space = bufsize - ((smb_buf(outbuf)+
113                                 alignment_offset+data_alignment_offset) -
114                                 outbuf);
115
116         /*
117          * useable_space can never be more than max_send minus the
118          * alignment offset.
119          */
120
121         useable_space = MIN(useable_space,
122                                 max_send - (alignment_offset+data_alignment_offset));
123
124
125         while (params_to_send || data_to_send) {
126
127                 /*
128                  * Calculate whether we will totally or partially fill this packet.
129                  */
130
131                 total_sent_thistime = params_to_send + data_to_send +
132                                         alignment_offset + data_alignment_offset;
133
134                 /* 
135                  * We can never send more than useable_space.
136                  */
137
138                 total_sent_thistime = MIN(total_sent_thistime, useable_space);
139
140                 set_message(outbuf, 18, total_sent_thistime, True);
141
142                 /*
143                  * Set total params and data to be sent.
144                  */
145
146                 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
147                 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
148
149                 /* 
150                  * Calculate how many parameters and data we can fit into
151                  * this packet. Parameters get precedence.
152                  */
153
154                 params_sent_thistime = MIN(params_to_send,useable_space);
155                 data_sent_thistime = useable_space - params_sent_thistime;
156                 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
157
158                 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
159
160                 if(params_sent_thistime == 0) {
161                         SIVAL(outbuf,smb_ntr_ParameterOffset,0);
162                         SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
163                 } else {
164                         /*
165                          * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
166                          * parameter bytes, however the first 4 bytes of outbuf are
167                          * the Netbios over TCP header. Thus use smb_base() to subtract
168                          * them from the calculation.
169                          */
170
171                         SIVAL(outbuf,smb_ntr_ParameterOffset,
172                                 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
173                         /* 
174                          * Absolute displacement of param bytes sent in this packet.
175                          */
176
177                         SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
178                 }
179
180                 /*
181                  * Deal with the data portion.
182                  */
183
184                 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
185
186                 if(data_sent_thistime == 0) {
187                         SIVAL(outbuf,smb_ntr_DataOffset,0);
188                         SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
189                 } else {
190                         /*
191                          * The offset of the data bytes is the offset of the
192                          * parameter bytes plus the number of parameters being sent this time.
193                          */
194
195                         SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
196                                 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
197                                 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
198                 }
199
200                 /* 
201                  * Copy the param bytes into the packet.
202                  */
203
204                 if(params_sent_thistime)
205                         memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
206
207                 /*
208                  * Copy in the data bytes
209                  */
210
211                 if(data_sent_thistime)
212                         memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
213                                 data_alignment_offset,pd,data_sent_thistime);
214     
215                 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
216                         params_sent_thistime, data_sent_thistime, useable_space));
217                 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
218                         params_to_send, data_to_send, paramsize, datasize));
219     
220                 /* Send the packet */
221                 if (!send_smb(smbd_server_fd(),outbuf))
222                         exit_server("send_nt_replies: send_smb failed.");
223     
224                 pp += params_sent_thistime;
225                 pd += data_sent_thistime;
226     
227                 params_to_send -= params_sent_thistime;
228                 data_to_send -= data_sent_thistime;
229
230                 /*
231                  * Sanity check
232                  */
233
234                 if(params_to_send < 0 || data_to_send < 0) {
235                         DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
236                                 params_to_send, data_to_send));
237                         return -1;
238                 }
239         } 
240
241         return 0;
242 }
243
244 /****************************************************************************
245  Save case statics.
246 ****************************************************************************/
247
248 static BOOL saved_case_sensitive;
249 static BOOL saved_case_preserve;
250 static BOOL saved_short_case_preserve;
251
252 /****************************************************************************
253  Save case semantics.
254 ****************************************************************************/
255
256 static void set_posix_case_semantics(uint32 file_attributes)
257 {
258         if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
259                 return;
260
261         saved_case_sensitive = case_sensitive;
262         saved_case_preserve = case_preserve;
263         saved_short_case_preserve = short_case_preserve;
264
265         /* Set to POSIX. */
266         case_sensitive = True;
267         case_preserve = True;
268         short_case_preserve = True;
269 }
270
271 /****************************************************************************
272  Restore case semantics.
273 ****************************************************************************/
274
275 static void restore_case_semantics(uint32 file_attributes)
276 {
277         if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
278                 return;
279
280         case_sensitive = saved_case_sensitive;
281         case_preserve = saved_case_preserve;
282         short_case_preserve = saved_short_case_preserve;
283 }
284
285 /****************************************************************************
286  Utility function to map create disposition.
287 ****************************************************************************/
288
289 static int map_create_disposition( uint32 create_disposition)
290 {
291         int ret;
292
293         switch( create_disposition ) {
294                 case FILE_CREATE:
295                         /* create if not exist, fail if exist */
296                         ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
297                         break;
298                 case FILE_SUPERSEDE:
299                 case FILE_OVERWRITE_IF:
300                         /* create if not exist, trunc if exist */
301                         ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
302                         break;
303                 case FILE_OPEN:
304                         /* fail if not exist, open if exists */
305                         ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
306                         break;
307                 case FILE_OPEN_IF:
308                         /* create if not exist, open if exists */
309                         ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
310                         break;
311                 case FILE_OVERWRITE:
312                         /* fail if not exist, truncate if exists */
313                         ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
314                         break;
315                 default:
316                         DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
317                                 create_disposition ));
318                         return -1;
319         }
320
321         DEBUG(10,("map_create_disposition: Mapped create_disposition 0x%lx to 0x%x\n",
322                         (unsigned long)create_disposition, ret ));
323
324         return ret;
325 }
326
327 /****************************************************************************
328  Utility function to map share modes.
329 ****************************************************************************/
330
331 static int map_share_mode( char *fname, uint32 create_options,
332                         uint32 *desired_access, uint32 share_access, uint32 file_attributes)
333 {
334         int smb_open_mode = -1;
335
336         /*
337          * Convert GENERIC bits to specific bits.
338          */
339
340         se_map_generic(desired_access, &file_generic_mapping);
341
342         switch( *desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
343                 case FILE_READ_DATA:
344                         smb_open_mode = DOS_OPEN_RDONLY;
345                         break;
346                 case FILE_WRITE_DATA:
347                 case FILE_APPEND_DATA:
348                 case FILE_WRITE_DATA|FILE_APPEND_DATA:
349                         smb_open_mode = DOS_OPEN_WRONLY;
350                         break;
351                 case FILE_READ_DATA|FILE_WRITE_DATA:
352                 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
353                 case FILE_READ_DATA|FILE_APPEND_DATA:
354                         smb_open_mode = DOS_OPEN_RDWR;
355                         break;
356         }
357
358         /*
359          * NB. For DELETE_ACCESS we should really check the
360          * directory permissions, as that is what controls
361          * delete, and for WRITE_DAC_ACCESS we should really
362          * check the ownership, as that is what controls the
363          * chmod. Note that this is *NOT* a security hole (this
364          * note is for you, Andrew) as we are not *allowing*
365          * the access at this point, the actual unlink or
366          * chown or chmod call would do this. We are just helping
367          * clients out by telling them if they have a hope
368          * of any of this succeeding. POSIX acls may still
369          * deny the real call. JRA.
370          */
371
372         if (smb_open_mode == -1) {
373
374                 if(*desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|
375                                         FILE_EXECUTE|FILE_READ_ATTRIBUTES|
376                                         FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
377                                         FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
378                         smb_open_mode = DOS_OPEN_RDONLY;
379                 } else if(*desired_access == 0) {
380
381                         /* 
382                          * JRA - NT seems to sometimes send desired_access as zero. play it safe
383                          * and map to a stat open.
384                          */
385
386                         smb_open_mode = DOS_OPEN_RDONLY;
387
388                 } else {
389                         DEBUG(0,("map_share_mode: Incorrect value 0x%lx for desired_access to file %s\n",
390                                 (unsigned long)*desired_access, fname));
391                         return -1;
392                 }
393         }
394
395         /*
396          * Set the special bit that means allow share delete.
397          * This is held outside the normal share mode bits at 1<<15.
398          * JRA.
399          */
400
401         if(share_access & FILE_SHARE_DELETE) {
402                 smb_open_mode |= ALLOW_SHARE_DELETE;
403                 DEBUG(10,("map_share_mode: FILE_SHARE_DELETE requested. open_mode = 0x%x\n", smb_open_mode));
404         }
405
406         /*
407          * We need to store the intent to open for Delete. This
408          * is what determines if a delete on close flag can be set.
409          * This is the wrong way (and place) to store this, but for 2.2 this
410          * is the only practical way. JRA.
411          */
412
413         if(*desired_access & DELETE_ACCESS) {
414                 DEBUG(10,("map_share_mode: DELETE_ACCESS requested. open_mode = 0x%x\n", smb_open_mode));
415         }
416
417         if (create_options & FILE_DELETE_ON_CLOSE) {
418                 /* Implicit delete access is *NOT* requested... */
419                 smb_open_mode |= DELETE_ON_CLOSE_FLAG;
420                 DEBUG(10,("map_share_mode: FILE_DELETE_ON_CLOSE requested. open_mode = 0x%x\n", smb_open_mode));
421         }
422
423         /* Add in the requested share mode. */
424         switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
425                 case FILE_SHARE_READ:
426                         smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
427                         break;
428                 case FILE_SHARE_WRITE:
429                         smb_open_mode |= SET_DENY_MODE(DENY_READ);
430                         break;
431                 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
432                         smb_open_mode |= SET_DENY_MODE(DENY_NONE);
433                         break;
434                 case FILE_SHARE_NONE:
435                         smb_open_mode |= SET_DENY_MODE(DENY_ALL);
436                         break;
437         }
438
439         /*
440          * Handle an O_SYNC request.
441          */
442
443         if(file_attributes & FILE_FLAG_WRITE_THROUGH)
444                 smb_open_mode |= FILE_SYNC_OPENMODE;
445
446         DEBUG(10,("map_share_mode: Mapped desired access 0x%lx, share access 0x%lx, file attributes 0x%lx \
447 to open_mode 0x%x\n", (unsigned long)*desired_access, (unsigned long)share_access,
448                 (unsigned long)file_attributes, smb_open_mode ));
449  
450         return smb_open_mode;
451 }
452
453 /****************************************************************************
454  Reply to an NT create and X call on a pipe.
455 ****************************************************************************/
456
457 static int nt_open_pipe(char *fname, connection_struct *conn,
458                         char *inbuf, char *outbuf, int *ppnum)
459 {
460         smb_np_struct *p = NULL;
461
462         uint16 vuid = SVAL(inbuf, smb_uid);
463         int i;
464
465         DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
466     
467         /* See if it is one we want to handle. */
468
469         if (lp_disable_spoolss() && strequal(fname, "\\spoolss"))
470                 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
471
472         for( i = 0; known_nt_pipes[i]; i++ )
473                 if( strequal(fname,known_nt_pipes[i]))
474                         break;
475     
476         if ( known_nt_pipes[i] == NULL )
477                 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
478     
479         /* Strip \\ off the name. */
480         fname++;
481     
482         DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
483
484         p = open_rpc_pipe_p(fname, conn, vuid);
485         if (!p)
486                 return(ERROR_DOS(ERRSRV,ERRnofids));
487
488         *ppnum = p->pnum;
489
490         return 0;
491 }
492
493 /****************************************************************************
494  Reply to an NT create and X call for pipes.
495 ****************************************************************************/
496
497 static int do_ntcreate_pipe_open(connection_struct *conn,
498                          char *inbuf,char *outbuf,int length,int bufsize)
499 {
500         pstring fname;
501         int ret;
502         int pnum = -1;
503         char *p = NULL;
504
505         srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
506
507         if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
508                 return ret;
509
510         /*
511          * Deal with pipe return.
512          */  
513
514         set_message(outbuf,34,0,True);
515
516         p = outbuf + smb_vwv2;
517         p++;
518         SSVAL(p,0,pnum);
519         p += 2;
520         SIVAL(p,0,FILE_WAS_OPENED);
521         p += 4;
522         p += 32;
523         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
524         p += 20;
525         /* File type. */
526         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
527         /* Device state. */
528         SSVAL(p,2, 0x5FF); /* ? */
529
530         DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
531
532         return chain_reply(inbuf,outbuf,length,bufsize);
533 }
534
535 /****************************************************************************
536  Reply to an NT create and X call.
537 ****************************************************************************/
538
539 int reply_ntcreate_and_X(connection_struct *conn,
540                          char *inbuf,char *outbuf,int length,int bufsize)
541 {  
542         int result;
543         pstring fname;
544         uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
545         uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
546         uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
547         uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
548         uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
549         uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
550         uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
551         int smb_ofun;
552         int smb_open_mode;
553         int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
554         /* Breakout the oplock request bits so we can set the
555            reply bits separately. */
556         int oplock_request = 0;
557         mode_t unixmode;
558         int fmode=0,rmode=0;
559         SMB_OFF_T file_len = 0;
560         SMB_STRUCT_STAT sbuf;
561         int smb_action = 0;
562         BOOL bad_path = False;
563         files_struct *fsp=NULL;
564         char *p = NULL;
565         time_t c_time;
566         START_PROFILE(SMBntcreateX);
567
568         /* If it's an IPC, use the pipe handler. */
569
570         if (IS_IPC(conn)) {
571                 if (lp_nt_pipe_support()) {
572                         END_PROFILE(SMBntcreateX);
573                         return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
574                 } else {
575                         END_PROFILE(SMBntcreateX);
576                         return(ERROR_DOS(ERRDOS,ERRbadaccess));
577                 }
578         }
579                         
580
581         /* 
582          * We need to construct the open_and_X ofun value from the
583          * NT values, as that's what our code is structured to accept.
584          */    
585         
586         if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
587                 END_PROFILE(SMBntcreateX);
588                 return(ERROR_DOS(ERRDOS,ERRbadaccess));
589         }
590
591         /*
592          * Get the file name.
593          */
594
595         if(root_dir_fid != 0) {
596                 /*
597                  * This filename is relative to a directory fid.
598                  */
599                 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
600                 size_t dir_name_len;
601
602                 if(!dir_fsp) {
603                         END_PROFILE(SMBntcreateX);
604                         return(ERROR_DOS(ERRDOS,ERRbadfid));
605                 }
606
607                 if(!dir_fsp->is_directory) {
608
609                         srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
610
611                         /* 
612                          * Check to see if this is a mac fork of some kind.
613                          */
614
615                         if( strchr_m(fname, ':')) {
616                                 END_PROFILE(SMBntcreateX);
617                                 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
618                         }
619                         END_PROFILE(SMBntcreateX);
620                         return(ERROR_DOS(ERRDOS,ERRbadfid));
621                 }
622
623                 /*
624                  * Copy in the base directory name.
625                  */
626
627                 pstrcpy( fname, dir_fsp->fsp_name );
628                 dir_name_len = strlen(fname);
629
630                 /*
631                  * Ensure it ends in a '\'.
632                  */
633
634                 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
635                         pstrcat(fname, "\\");
636                         dir_name_len++;
637                 }
638
639                 srvstr_pull_buf(inbuf, &fname[dir_name_len], smb_buf(inbuf), sizeof(fname)-dir_name_len, STR_TERMINATE);
640         } else {
641                 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
642
643                 /* 
644                  * Check to see if this is a mac fork of some kind.
645                  */
646
647                 if( strchr_m(fname, ':')) {
648                         END_PROFILE(SMBntcreateX);
649                         return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
650                 }
651         }
652         
653         /*
654          * Now contruct the smb_open_mode value from the filename, 
655          * desired access and the share access.
656          */
657         RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
658
659         if((smb_open_mode = map_share_mode(fname, create_options, &desired_access, 
660                                            share_access, 
661                                            file_attributes)) == -1) {
662                 END_PROFILE(SMBntcreateX);
663                 return ERROR_DOS(ERRDOS,ERRbadaccess);
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         set_posix_case_semantics(file_attributes);
680                 
681         unix_convert(fname,conn,0,&bad_path,&sbuf);
682                 
683         unixmode = unix_mode(conn,smb_attr | aARCH, fname);
684     
685         /* 
686          * If it's a request for a directory open, deal with it separately.
687          */
688
689         if(create_options & FILE_DIRECTORY_FILE) {
690                 oplock_request = 0;
691                 
692                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
693                         
694                 restore_case_semantics(file_attributes);
695
696                 if(!fsp) {
697                         set_bad_path_error(errno, bad_path);
698                         END_PROFILE(SMBntcreateX);
699                         return(UNIXERROR(ERRDOS,ERRnoaccess));
700                 }
701         } else {
702                 /*
703                  * Ordinary file case.
704                  */
705
706                 /* NB. We have a potential bug here. If we
707                  * cause an oplock break to ourselves, then we
708                  * could end up processing filename related
709                  * SMB requests whilst we await the oplock
710                  * break response. As we may have changed the
711                  * filename case semantics to be POSIX-like,
712                  * this could mean a filename request could
713                  * fail when it should succeed. This is a rare
714                  * condition, but eventually we must arrange
715                  * to restore the correct case semantics
716                  * before issuing an oplock break request to
717                  * our client. JRA.  */
718
719                 fsp = open_file_shared1(conn,fname,&sbuf,
720                                         desired_access,
721                                         smb_open_mode,
722                                         smb_ofun,unixmode, oplock_request,
723                                         &rmode,&smb_action);
724
725                 if (!fsp) { 
726                         /* We cheat here. There are two cases we
727                          * care about. One is a directory rename,
728                          * where the NT client will attempt to
729                          * open the source directory for
730                          * DELETE access. Note that when the
731                          * NT client does this it does *not*
732                          * set the directory bit in the
733                          * request packet. This is translated
734                          * into a read/write open
735                          * request. POSIX states that any open
736                          * for write request on a directory
737                          * will generate an EISDIR error, so
738                          * we can catch this here and open a
739                          * pseudo handle that is flagged as a
740                          * directory. The second is an open
741                          * for a permissions read only, which
742                          * we handle in the open_file_stat case. JRA.
743                          */
744
745                         if(errno == EISDIR) {
746
747                                 /*
748                                  * Fail the open if it was explicitly a non-directory file.
749                                  */
750
751                                 if (create_options & FILE_NON_DIRECTORY_FILE) {
752                                         restore_case_semantics(file_attributes);
753                                         SSVAL(outbuf, smb_flg2, 
754                                               SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
755                                         END_PROFILE(SMBntcreateX);
756                                         return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
757                                 }
758         
759                                 oplock_request = 0;
760                                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
761                                 
762                                 if(!fsp) {
763                                         restore_case_semantics(file_attributes);
764                                         set_bad_path_error(errno, bad_path);
765                                         END_PROFILE(SMBntcreateX);
766                                         return(UNIXERROR(ERRDOS,ERRnoaccess));
767                                 }
768                         } else {
769
770                                 restore_case_semantics(file_attributes);
771                                 set_bad_path_error(errno, bad_path);
772                                 END_PROFILE(SMBntcreateX);
773                                 return(UNIXERROR(ERRDOS,ERRnoaccess));
774                         }
775                 } 
776         }
777                 
778         restore_case_semantics(file_attributes);
779                 
780         file_len = sbuf.st_size;
781         fmode = dos_mode(conn,fname,&sbuf);
782         if(fmode == 0)
783                 fmode = FILE_ATTRIBUTE_NORMAL;
784         if (!fsp->is_directory && (fmode & aDIR)) {
785                 close_file(fsp,False);
786                 END_PROFILE(SMBntcreateX);
787                 return ERROR_DOS(ERRDOS,ERRnoaccess);
788         } 
789         
790         /* 
791          * If the caller set the extended oplock request bit
792          * and we granted one (by whatever means) - set the
793          * correct bit for extended oplock reply.
794          */
795         
796         if (oplock_request && lp_fake_oplocks(SNUM(conn)))
797                 smb_action |= EXTENDED_OPLOCK_GRANTED;
798         
799         if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
800                 smb_action |= EXTENDED_OPLOCK_GRANTED;
801
802 #if 0
803         /* W2K sends back 42 words here ! If we do the same it breaks offline sync. Go figure... ? JRA. */
804         set_message(outbuf,42,0,True);
805 #else
806         set_message(outbuf,34,0,True);
807 #endif
808         
809         p = outbuf + smb_vwv2;
810         
811         /*
812          * Currently as we don't support level II oplocks we just report
813          * exclusive & batch here.
814          */
815
816         if (smb_action & EXTENDED_OPLOCK_GRANTED) {
817                 if (flags & REQUEST_BATCH_OPLOCK) {
818                         SCVAL(p,0, BATCH_OPLOCK_RETURN);
819                 } else {
820                         SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
821                 }
822         } else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
823                 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
824         } else {
825                 SCVAL(p,0,NO_OPLOCK_RETURN);
826         }
827         
828         p++;
829         SSVAL(p,0,fsp->fnum);
830         p += 2;
831         SIVAL(p,0,smb_action);
832         p += 4;
833         
834         /* Create time. */  
835         c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
836
837         if (lp_dos_filetime_resolution(SNUM(conn))) {
838                 c_time &= ~1;
839                 sbuf.st_atime &= ~1;
840                 sbuf.st_mtime &= ~1;
841                 sbuf.st_mtime &= ~1;
842         }
843
844         put_long_date(p,c_time);
845         p += 8;
846         put_long_date(p,sbuf.st_atime); /* access time */
847         p += 8;
848         put_long_date(p,sbuf.st_mtime); /* write time */
849         p += 8;
850         put_long_date(p,sbuf.st_mtime); /* change time */
851         p += 8;
852         SIVAL(p,0,fmode); /* File Attributes. */
853         p += 4;
854         SOFF_T(p, 0, get_allocation_size(&sbuf));
855         p += 8;
856         SOFF_T(p,0,file_len);
857         p += 12;
858         SCVAL(p,0,fsp->is_directory ? 1 : 0);
859         
860         DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
861
862         result = chain_reply(inbuf,outbuf,length,bufsize);
863         END_PROFILE(SMBntcreateX);
864         return result;
865 }
866
867 /****************************************************************************
868  Reply to a NT_TRANSACT_CREATE call to open a pipe.
869 ****************************************************************************/
870
871 static int do_nt_transact_create_pipe( connection_struct *conn,
872                                         char *inbuf, char *outbuf, int length, 
873                                         int bufsize, char **ppsetup, char **ppparams, 
874                                         char **ppdata)
875 {
876         pstring fname;
877         int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
878         char *params = *ppparams;
879         int ret;
880         int pnum = -1;
881         char *p = NULL;
882
883         /*
884          * Ensure minimum number of parameters sent.
885          */
886
887         if(total_parameter_count < 54) {
888                 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
889                 return ERROR_DOS(ERRDOS,ERRbadaccess);
890         }
891
892         srvstr_pull(inbuf, fname, params+53, sizeof(fname), total_parameter_count-53, STR_TERMINATE);
893
894         if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
895                 return ret;
896         
897         /* Realloc the size of parameters and data we will return */
898         params = Realloc(*ppparams, 69);
899         if(params == NULL)
900                 return ERROR_DOS(ERRDOS,ERRnomem);
901         
902         *ppparams = params;
903         
904         memset((char *)params,'\0',69);
905         
906         p = params;
907         SCVAL(p,0,NO_OPLOCK_RETURN);
908         
909         p += 2;
910         SSVAL(p,0,pnum);
911         p += 2;
912         SIVAL(p,0,FILE_WAS_OPENED);
913         p += 8;
914         
915         p += 32;
916         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
917         p += 20;
918         /* File type. */
919         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
920         /* Device state. */
921         SSVAL(p,2, 0x5FF); /* ? */
922         
923         DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
924         
925         /* Send the required number of replies */
926         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
927         
928         return -1;
929 }
930
931 /****************************************************************************
932  Internal fn to set security descriptors.
933 ****************************************************************************/
934
935 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
936 {
937         prs_struct pd;
938         SEC_DESC *psd = NULL;
939         TALLOC_CTX *mem_ctx;
940         BOOL ret;
941         
942         if (sd_len == 0) {
943                 return NT_STATUS_OK;
944         }
945
946         /*
947          * Init the parse struct we will unmarshall from.
948          */
949
950         if ((mem_ctx = talloc_init()) == NULL) {
951                 DEBUG(0,("set_sd: talloc_init failed.\n"));
952                 return NT_STATUS_NO_MEMORY;
953         }
954
955         prs_init(&pd, 0, mem_ctx, UNMARSHALL);
956
957         /*
958          * Setup the prs_struct to point at the memory we just
959          * allocated.
960          */
961         
962         prs_give_memory( &pd, data, sd_len, False);
963
964         /*
965          * Finally, unmarshall from the data buffer.
966          */
967
968         if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
969                 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
970                 /*
971                  * Return access denied for want of a better error message..
972                  */ 
973                 talloc_destroy(mem_ctx);
974                 return NT_STATUS_NO_MEMORY;
975         }
976         
977         if (psd->off_owner_sid==0)
978                 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
979         if (psd->off_grp_sid==0)
980                 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
981         if (psd->off_sacl==0)
982                 security_info_sent &= ~SACL_SECURITY_INFORMATION;
983         if (psd->off_dacl==0)
984                 security_info_sent &= ~DACL_SECURITY_INFORMATION;
985         
986         ret = fsp->conn->vfs_ops.fset_nt_acl( fsp, fsp->fd, security_info_sent, psd);
987         
988         if (!ret) {
989                 talloc_destroy(mem_ctx);
990                 return NT_STATUS_ACCESS_DENIED;
991         }
992         
993         talloc_destroy(mem_ctx);
994         
995         return NT_STATUS_OK;
996 }
997
998 /****************************************************************************
999  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1000 ****************************************************************************/
1001
1002 static int call_nt_transact_create(connection_struct *conn,
1003                                         char *inbuf, char *outbuf, int length, 
1004                                         int bufsize, char **ppsetup, char **ppparams, 
1005                                         char **ppdata)
1006 {
1007         pstring fname;
1008         char *params = *ppparams;
1009         char *data = *ppdata;
1010         int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1011         /* Breakout the oplock request bits so we can set the reply bits separately. */
1012         int oplock_request = 0;
1013         mode_t unixmode;
1014         int fmode=0,rmode=0;
1015         SMB_OFF_T file_len = 0;
1016         SMB_STRUCT_STAT sbuf;
1017         int smb_action = 0;
1018         BOOL bad_path = False;
1019         files_struct *fsp = NULL;
1020         char *p = NULL;
1021         uint32 flags;
1022         uint32 desired_access;
1023         uint32 file_attributes;
1024         uint32 share_access;
1025         uint32 create_disposition;
1026         uint32 create_options;
1027         uint32 sd_len;
1028         uint16 root_dir_fid;
1029         int smb_ofun;
1030         int smb_open_mode;
1031         int smb_attr;
1032         time_t c_time;
1033         NTSTATUS nt_status;
1034
1035         DEBUG(5,("call_nt_transact_create\n"));
1036
1037         /*
1038          * If it's an IPC, use the pipe handler.
1039          */
1040
1041         if (IS_IPC(conn)) {
1042                 if (lp_nt_pipe_support())
1043                         return do_nt_transact_create_pipe(conn, inbuf, outbuf, length, 
1044                                         bufsize, ppsetup, ppparams, ppdata);
1045                 else
1046                         return ERROR_DOS(ERRDOS,ERRbadaccess);
1047         }
1048
1049         /*
1050          * Ensure minimum number of parameters sent.
1051          */
1052
1053         if(total_parameter_count < 54) {
1054                 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1055                 return ERROR_DOS(ERRDOS,ERRbadaccess);
1056         }
1057
1058         flags = IVAL(params,0);
1059         desired_access = IVAL(params,8);
1060         file_attributes = IVAL(params,20);
1061         share_access = IVAL(params,24);
1062         create_disposition = IVAL(params,28);
1063         create_options = IVAL(params,32);
1064         sd_len = IVAL(params,36);
1065         root_dir_fid = (uint16)IVAL(params,4);
1066         smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1067
1068         /* 
1069          * We need to construct the open_and_X ofun value from the
1070          * NT values, as that's what our code is structured to accept.
1071          */    
1072
1073         if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1074                 return ERROR_DOS(ERRDOS,ERRbadmem);
1075
1076         /*
1077          * Get the file name.
1078          */
1079
1080         if(root_dir_fid != 0) {
1081                 /*
1082                  * This filename is relative to a directory fid.
1083                  */
1084
1085                 files_struct *dir_fsp = file_fsp(params,4);
1086                 size_t dir_name_len;
1087
1088                 if(!dir_fsp)
1089                         return ERROR_DOS(ERRDOS,ERRbadfid);
1090
1091                 if(!dir_fsp->is_directory) {
1092
1093                         srvstr_pull(inbuf, fname, params+53, sizeof(fname), total_parameter_count-53, STR_TERMINATE);
1094
1095                         /*
1096                          * Check to see if this is a mac fork of some kind.
1097                          */
1098
1099                         if( strchr_m(fname, ':'))
1100                                 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1101
1102                         return ERROR_DOS(ERRDOS,ERRbadfid);
1103                 }
1104
1105                 /*
1106                  * Copy in the base directory name.
1107                  */
1108
1109                 pstrcpy( fname, dir_fsp->fsp_name );
1110                 dir_name_len = strlen(fname);
1111
1112                 /*
1113                  * Ensure it ends in a '\'.
1114                  */
1115
1116                 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1117                         pstrcat(fname, "\\");
1118                         dir_name_len++;
1119                 }
1120
1121                 srvstr_pull(inbuf, &fname[dir_name_len], params+53, sizeof(fname)-dir_name_len, 
1122                                 total_parameter_count-53, STR_TERMINATE);
1123         } else {
1124                 srvstr_pull(inbuf, fname, params+53, sizeof(fname), total_parameter_count-53, STR_TERMINATE);
1125
1126                 /*
1127                  * Check to see if this is a mac fork of some kind.
1128                  */
1129
1130                 if( strchr_m(fname, ':'))
1131                         return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1132         }
1133
1134         /*
1135          * Now contruct the smb_open_mode value from the desired access
1136          * and the share access.
1137          */
1138
1139         if((smb_open_mode = map_share_mode( fname, create_options, &desired_access,
1140                                                 share_access, file_attributes)) == -1)
1141                 return ERROR_DOS(ERRDOS,ERRbadaccess);
1142
1143         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1144         oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1145
1146         /*
1147          * Check if POSIX semantics are wanted.
1148          */
1149
1150         set_posix_case_semantics(file_attributes);
1151     
1152         RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1153
1154         unix_convert(fname,conn,0,&bad_path,&sbuf);
1155     
1156         unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1157    
1158         /*
1159          * If it's a request for a directory open, deal with it separately.
1160          */
1161
1162         if(create_options & FILE_DIRECTORY_FILE) {
1163
1164                 oplock_request = 0;
1165
1166                 /*
1167                  * We will get a create directory here if the Win32
1168                  * app specified a security descriptor in the 
1169                  * CreateDirectory() call.
1170                  */
1171
1172                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1173
1174                 if(!fsp) {
1175                         restore_case_semantics(file_attributes);
1176                         set_bad_path_error(errno, bad_path);
1177                         return(UNIXERROR(ERRDOS,ERRnoaccess));
1178                 }
1179
1180         } else {
1181
1182                 /*
1183                  * Ordinary file case.
1184                  */
1185
1186                 fsp = open_file_shared1(conn,fname,&sbuf,desired_access,
1187                                                 smb_open_mode,smb_ofun,unixmode,
1188                                                 oplock_request,&rmode,&smb_action);
1189
1190                 if (!fsp) { 
1191
1192                         if(errno == EISDIR) {
1193
1194                                 /*
1195                                  * Fail the open if it was explicitly a non-directory file.
1196                                  */
1197
1198                                 if (create_options & FILE_NON_DIRECTORY_FILE) {
1199                                         restore_case_semantics(file_attributes);
1200                                         SSVAL(outbuf, smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1201                                         return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1202                                 }
1203         
1204                                 oplock_request = 0;
1205                                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1206                                 
1207                                 if(!fsp) {
1208                                         restore_case_semantics(file_attributes);
1209                                         set_bad_path_error(errno, bad_path);
1210                                         return(UNIXERROR(ERRDOS,ERRnoaccess));
1211                                 }
1212                         } else {
1213                                 restore_case_semantics(file_attributes);
1214                                 set_bad_path_error(errno, bad_path);
1215                                 return(UNIXERROR(ERRDOS,ERRnoaccess));
1216                         }
1217                 } 
1218   
1219                 file_len = sbuf.st_size;
1220                 fmode = dos_mode(conn,fname,&sbuf);
1221                 if(fmode == 0)
1222                         fmode = FILE_ATTRIBUTE_NORMAL;
1223
1224                 if (fmode & aDIR) {
1225                         close_file(fsp,False);
1226                         restore_case_semantics(file_attributes);
1227                         return ERROR_DOS(ERRDOS,ERRnoaccess);
1228                 } 
1229
1230                 /* 
1231                  * If the caller set the extended oplock request bit
1232                  * and we granted one (by whatever means) - set the
1233                  * correct bit for extended oplock reply.
1234                  */
1235     
1236                 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1237                         smb_action |= EXTENDED_OPLOCK_GRANTED;
1238   
1239                 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1240                         smb_action |= EXTENDED_OPLOCK_GRANTED;
1241         }
1242
1243         /*
1244          * Now try and apply the desired SD.
1245          */
1246
1247         if (sd_len && !NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION))) {
1248                 close_file(fsp,False);
1249                 restore_case_semantics(file_attributes);
1250                 return ERROR_NT(nt_status);
1251         }
1252         
1253         restore_case_semantics(file_attributes);
1254
1255         /* Realloc the size of parameters and data we will return */
1256         params = Realloc(*ppparams, 69);
1257         if(params == NULL)
1258                 return ERROR_DOS(ERRDOS,ERRnomem);
1259
1260         *ppparams = params;
1261
1262         memset((char *)params,'\0',69);
1263
1264         p = params;
1265         if (smb_action & EXTENDED_OPLOCK_GRANTED)       
1266                 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1267         else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1268                 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1269         else
1270                 SCVAL(p,0,NO_OPLOCK_RETURN);
1271         
1272         p += 2;
1273         SSVAL(p,0,fsp->fnum);
1274         p += 2;
1275         SIVAL(p,0,smb_action);
1276         p += 8;
1277
1278         /* Create time. */
1279         c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1280
1281         if (lp_dos_filetime_resolution(SNUM(conn))) {
1282                 c_time &= ~1;
1283                 sbuf.st_atime &= ~1;
1284                 sbuf.st_mtime &= ~1;
1285                 sbuf.st_mtime &= ~1;
1286         }
1287
1288         put_long_date(p,c_time);
1289         p += 8;
1290         put_long_date(p,sbuf.st_atime); /* access time */
1291         p += 8;
1292         put_long_date(p,sbuf.st_mtime); /* write time */
1293         p += 8;
1294         put_long_date(p,sbuf.st_mtime); /* change time */
1295         p += 8;
1296         SIVAL(p,0,fmode); /* File Attributes. */
1297         p += 4;
1298         SOFF_T(p, 0, get_allocation_size(&sbuf));
1299         p += 8;
1300         SOFF_T(p,0,file_len);
1301
1302         DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1303
1304         /* Send the required number of replies */
1305         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1306
1307         return -1;
1308 }
1309
1310 /****************************************************************************
1311  Reply to a NT CANCEL request.
1312 ****************************************************************************/
1313
1314 int reply_ntcancel(connection_struct *conn,
1315                    char *inbuf,char *outbuf,int length,int bufsize)
1316 {
1317         /*
1318          * Go through and cancel any pending change notifies.
1319          */
1320         
1321         int mid = SVAL(inbuf,smb_mid);
1322         START_PROFILE(SMBntcancel);
1323         remove_pending_change_notify_requests_by_mid(mid);
1324         remove_pending_lock_requests_by_mid(mid);
1325         
1326         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1327
1328         END_PROFILE(SMBntcancel);
1329         return(-1);
1330 }
1331
1332 /****************************************************************************
1333  Reply to an unsolicited SMBNTtranss - just ignore it!
1334 ****************************************************************************/
1335
1336 int reply_nttranss(connection_struct *conn,
1337                    char *inbuf,char *outbuf,int length,int bufsize)
1338 {
1339         START_PROFILE(SMBnttranss);
1340         DEBUG(4,("Ignoring nttranss of length %d\n",length));
1341         END_PROFILE(SMBnttranss);
1342         return(-1);
1343 }
1344
1345 /****************************************************************************
1346  Reply to a notify change - queue the request and 
1347  don't allow a directory to be opened.
1348 ****************************************************************************/
1349
1350 static int call_nt_transact_notify_change(connection_struct *conn,
1351                                    char *inbuf, char *outbuf, int length,
1352                                    int bufsize, 
1353                                    char **ppsetup, 
1354                                    char **ppparams, char **ppdata)
1355 {
1356         char *setup = *ppsetup;
1357         files_struct *fsp;
1358         uint32 flags;
1359
1360         fsp = file_fsp(setup,4);
1361         flags = IVAL(setup, 0);
1362
1363         DEBUG(3,("call_nt_transact_notify_change\n"));
1364
1365         if(!fsp)
1366                 return ERROR_DOS(ERRDOS,ERRbadfid);
1367
1368         if((!fsp->is_directory) || (conn != fsp->conn))
1369                 return ERROR_DOS(ERRDOS,ERRbadfid);
1370
1371         if (!change_notify_set(inbuf, fsp, conn, flags))
1372                 return(UNIXERROR(ERRDOS,ERRbadfid));
1373
1374         DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1375 name = %s\n", fsp->fsp_name ));
1376
1377         return -1;
1378 }
1379
1380 /****************************************************************************
1381  Reply to an NT transact rename command.
1382 ****************************************************************************/
1383
1384 static int call_nt_transact_rename(connection_struct *conn,
1385                                    char *inbuf, char *outbuf, int length, 
1386                                    int bufsize,
1387                                    char **ppsetup, char **ppparams, char **ppdata)
1388 {
1389         char *params = *ppparams;
1390         pstring new_name;
1391         files_struct *fsp = file_fsp(params, 0);
1392         BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1393         NTSTATUS status;
1394
1395         CHECK_FSP(fsp, conn);
1396         srvstr_pull(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE);
1397
1398         status = rename_internals(conn, fsp->fsp_name,
1399                                   new_name, replace_if_exists);
1400         if (!NT_STATUS_IS_OK(status))
1401                 return ERROR_NT(status);
1402
1403         /*
1404          * Rename was successful.
1405          */
1406         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1407         
1408         DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n", 
1409                  fsp->fsp_name, new_name));
1410         
1411         /*
1412          * Win2k needs a changenotify request response before it will
1413          * update after a rename..
1414          */
1415         
1416         process_pending_change_notify_queue((time_t)0);
1417
1418         return -1;
1419 }
1420
1421 /******************************************************************************
1422  Fake up a completely empty SD.
1423 *******************************************************************************/
1424
1425 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1426 {
1427         extern DOM_SID global_sid_World;
1428         size_t sd_size;
1429
1430         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1431         if(!*ppsd) {
1432                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1433                 sd_size = 0;
1434         }
1435
1436         return sd_size;
1437 }
1438
1439 /****************************************************************************
1440  Reply to query a security descriptor - currently this is not implemented (it
1441  is planned to be though). Right now it just returns the same thing NT would
1442  when queried on a FAT filesystem. JRA.
1443 ****************************************************************************/
1444
1445 static int call_nt_transact_query_security_desc(connection_struct *conn,
1446                                                 char *inbuf, char *outbuf, 
1447                                                 int length, int bufsize, 
1448                                                 char **ppsetup, char **ppparams, char **ppdata)
1449 {
1450         uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1451         char *params = *ppparams;
1452         char *data = *ppdata;
1453         prs_struct pd;
1454         SEC_DESC *psd = NULL;
1455         size_t sd_size;
1456         TALLOC_CTX *mem_ctx;
1457
1458         files_struct *fsp = file_fsp(params,0);
1459
1460         if(!fsp)
1461                 return ERROR_DOS(ERRDOS,ERRbadfid);
1462
1463         DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1464
1465         params = Realloc(*ppparams, 4);
1466         if(params == NULL)
1467                 return ERROR_DOS(ERRDOS,ERRnomem);
1468
1469         *ppparams = params;
1470
1471         if ((mem_ctx = talloc_init()) == NULL) {
1472                 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1473                 return ERROR_DOS(ERRDOS,ERRnomem);
1474         }
1475
1476         /*
1477          * Get the permissions to return.
1478          */
1479
1480         if (!lp_nt_acl_support(SNUM(conn)))
1481                 sd_size = get_null_nt_acl(mem_ctx, &psd);
1482         else
1483                 sd_size = conn->vfs_ops.fget_nt_acl(fsp, fsp->fd, &psd);
1484
1485         if (sd_size == 0) {
1486                 talloc_destroy(mem_ctx);
1487                 return(UNIXERROR(ERRDOS,ERRnoaccess));
1488         }
1489
1490         DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1491
1492         SIVAL(params,0,(uint32)sd_size);
1493
1494         if(max_data_count < sd_size) {
1495
1496                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1497                         params, 4, *ppdata, 0);
1498                 talloc_destroy(mem_ctx);
1499                 return -1;
1500         }
1501
1502         /*
1503          * Allocate the data we will point this at.
1504          */
1505
1506         data = Realloc(*ppdata, sd_size);
1507         if(data == NULL) {
1508                 talloc_destroy(mem_ctx);
1509                 return ERROR_DOS(ERRDOS,ERRnomem);
1510         }
1511
1512         *ppdata = data;
1513
1514         memset(data, '\0', sd_size);
1515
1516         /*
1517          * Init the parse struct we will marshall into.
1518          */
1519
1520         prs_init(&pd, 0, mem_ctx, MARSHALL);
1521
1522         /*
1523          * Setup the prs_struct to point at the memory we just
1524          * allocated.
1525          */
1526
1527         prs_give_memory( &pd, data, (uint32)sd_size, False);
1528
1529         /*
1530          * Finally, linearize into the outgoing buffer.
1531          */
1532
1533         if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1534                 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1535 security descriptor.\n"));
1536                 /*
1537                  * Return access denied for want of a better error message..
1538                  */ 
1539                 talloc_destroy(mem_ctx);
1540                 return(UNIXERROR(ERRDOS,ERRnoaccess));
1541         }
1542
1543         /*
1544          * Now we can delete the security descriptor.
1545          */
1546
1547         talloc_destroy(mem_ctx);
1548
1549         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
1550         return -1;
1551 }
1552
1553 /****************************************************************************
1554  Reply to set a security descriptor. Map to UNIX perms.
1555 ****************************************************************************/
1556
1557 static int call_nt_transact_set_security_desc(connection_struct *conn,
1558                                                                         char *inbuf, char *outbuf, int length,
1559                                                                         int bufsize, char **ppsetup, 
1560                                                                         char **ppparams, char **ppdata)
1561 {
1562         uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1563         char *params= *ppparams;
1564         char *data = *ppdata;
1565         uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
1566         files_struct *fsp = NULL;
1567         uint32 security_info_sent = 0;
1568         NTSTATUS nt_status;
1569
1570         if(total_parameter_count < 8)
1571                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1572
1573         if((fsp = file_fsp(params,0)) == NULL)
1574                 return ERROR_DOS(ERRDOS,ERRbadfid);
1575
1576         if(!lp_nt_acl_support(SNUM(conn)))
1577                 goto done;
1578
1579         security_info_sent = IVAL(params,4);
1580
1581         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1582                 (unsigned int)security_info_sent ));
1583
1584         if (total_data_count == 0)
1585                 return ERROR_DOS(ERRDOS, ERRbadaccess);
1586
1587         if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, total_data_count, security_info_sent)))
1588                 return ERROR_NT(nt_status);
1589
1590   done:
1591
1592         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1593         return -1;
1594 }
1595    
1596 /****************************************************************************
1597  Reply to NT IOCTL
1598 ****************************************************************************/
1599 static int call_nt_transact_ioctl(connection_struct *conn,
1600                                   char *inbuf, char *outbuf, int length,
1601                                   int bufsize, 
1602                                   char **ppsetup, int setup_count,
1603                                   char **ppparams, int parameter_count,
1604                                   char **ppdata, int data_count)
1605 {
1606         unsigned fnum, control;
1607         static BOOL logged_message;
1608
1609         if (setup_count != 8) {
1610                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1611                 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1612         }
1613
1614         fnum = SVAL(*ppsetup, 4);
1615         control = IVAL(*ppsetup, 0);
1616
1617         DEBUG(6,("call_nt_transact_ioctl: fnum=%d control=0x%x\n", 
1618                  fnum, control));
1619
1620         switch (control) {
1621         case NTIOCTL_SET_SPARSE:
1622                 /* pretend this succeeded - tho strictly we should
1623                    mark the file sparse (if the local fs supports it)
1624                    so we can know if we need to pre-allocate or not */
1625                 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1626                 return -1;
1627
1628         default:
1629                 if (!logged_message) {
1630                         logged_message = True; /* Only print this once... */
1631                         DEBUG(3,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
1632                                  control));
1633                 }
1634         }
1635
1636         return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1637 }
1638    
1639 /****************************************************************************
1640  Reply to a SMBNTtrans.
1641 ****************************************************************************/
1642
1643 int reply_nttrans(connection_struct *conn,
1644                         char *inbuf,char *outbuf,int length,int bufsize)
1645 {
1646         int  outsize = 0;
1647 #if 0 /* Not used. */
1648         uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1649         uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1650         uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1651 #endif /* Not used. */
1652         uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1653         uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1654         uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1655         uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1656         uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1657         uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1658         uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1659         uint16 function_code = SVAL( inbuf, smb_nt_Function);
1660         char *params = NULL, *data = NULL, *setup = NULL;
1661         uint32 num_params_sofar, num_data_sofar;
1662         START_PROFILE(SMBnttrans);
1663
1664         if(global_oplock_break &&
1665                         ((function_code == NT_TRANSACT_CREATE) ||
1666                          (function_code == NT_TRANSACT_RENAME))) {
1667                 /*
1668                  * Queue this open message as we are the process of an oplock break.
1669                  */
1670
1671                 DEBUG(2,("reply_nttrans: queueing message code 0x%x \
1672 due to being in oplock break state.\n", (unsigned int)function_code ));
1673
1674                 push_oplock_pending_smb_message( inbuf, length);
1675                 END_PROFILE(SMBnttrans);
1676                 return -1;
1677         }
1678
1679         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
1680                 END_PROFILE(SMBnttrans);
1681                 return ERROR_DOS(ERRSRV,ERRaccess);
1682         }
1683
1684         outsize = set_message(outbuf,0,0,True);
1685
1686         /* 
1687          * All nttrans messages we handle have smb_wct == 19 + setup_count.
1688          * Ensure this is so as a sanity check.
1689          */
1690
1691         if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1692                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1693                         CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1694                 END_PROFILE(SMBnttrans);
1695                 return ERROR_DOS(ERRSRV,ERRerror);
1696         }
1697     
1698         /* Allocate the space for the setup, the maximum needed parameters and data */
1699
1700         if(setup_count > 0)
1701                 setup = (char *)malloc(setup_count);
1702         if (total_parameter_count > 0)
1703                 params = (char *)malloc(total_parameter_count);
1704         if (total_data_count > 0)
1705                 data = (char *)malloc(total_data_count);
1706  
1707         if ((total_parameter_count && !params)  || (total_data_count && !data) ||
1708                                 (setup_count && !setup)) {
1709                 SAFE_FREE(setup);
1710                 SAFE_FREE(params);
1711                 SAFE_FREE(data);
1712                 DEBUG(0,("reply_nttrans : Out of memory\n"));
1713                 END_PROFILE(SMBnttrans);
1714                 return ERROR_DOS(ERRDOS,ERRnomem);
1715         }
1716
1717         /* Copy the param and data bytes sent with this request into the params buffer */
1718         num_params_sofar = parameter_count;
1719         num_data_sofar = data_count;
1720
1721         if (parameter_count > total_parameter_count || data_count > total_data_count)
1722                 exit_server("reply_nttrans: invalid sizes in packet.");
1723
1724         if(setup) {
1725                 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1726                 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1727                 dump_data(10, setup, setup_count);
1728         }
1729         if(params) {
1730                 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1731                 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1732                 dump_data(10, params, parameter_count);
1733         }
1734         if(data) {
1735                 memcpy( data, smb_base(inbuf) + data_offset, data_count);
1736                 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1737                 dump_data(10, data, data_count);
1738         }
1739
1740         if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1741                 /* We need to send an interim response then receive the rest
1742                         of the parameter/data bytes */
1743                 outsize = set_message(outbuf,0,0,True);
1744                 if (!send_smb(smbd_server_fd(),outbuf))
1745                         exit_server("reply_nttrans: send_smb failed.");
1746
1747                 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1748                         BOOL ret;
1749
1750                         ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
1751
1752                         if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1753                                 outsize = set_message(outbuf,0,0,True);
1754                                 if(ret) {
1755                                         DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1756                                 } else {
1757                                         DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1758                                                 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1759                                 }
1760                                 SAFE_FREE(params);
1761                                 SAFE_FREE(data);
1762                                 SAFE_FREE(setup);
1763                                 END_PROFILE(SMBnttrans);
1764                                 return ERROR_DOS(ERRSRV,ERRerror);
1765                         }
1766       
1767                         /* Revise total_params and total_data in case they have changed downwards */
1768                         total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1769                         total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1770                         num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1771                         num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1772                         if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1773                                 exit_server("reply_nttrans2: data overflow in secondary nttrans packet");
1774
1775                         memcpy( &params[ IVAL(inbuf, smb_nts_ParameterDisplacement)], 
1776                                 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1777                         memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1778                                 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1779                 }
1780         }
1781
1782         if (Protocol >= PROTOCOL_NT1)
1783                 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | FLAGS2_IS_LONG_NAME);
1784
1785         /* Now we must call the relevant NT_TRANS function */
1786         switch(function_code) {
1787                 case NT_TRANSACT_CREATE:
1788                         START_PROFILE_NESTED(NT_transact_create);
1789                         outsize = call_nt_transact_create(conn, inbuf, outbuf,
1790                                         length, bufsize, 
1791                                         &setup, &params, &data);
1792                         END_PROFILE_NESTED(NT_transact_create);
1793                         break;
1794                 case NT_TRANSACT_IOCTL:
1795                         START_PROFILE_NESTED(NT_transact_ioctl);
1796                         outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
1797                                                          length, bufsize, 
1798                                                          &setup, setup_count,
1799                                                          &params, parameter_count, 
1800                                                          &data, data_count);
1801                         END_PROFILE_NESTED(NT_transact_ioctl);
1802                         break;
1803                 case NT_TRANSACT_SET_SECURITY_DESC:
1804                         START_PROFILE_NESTED(NT_transact_set_security_desc);
1805                         outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf, 
1806                                         length, bufsize, 
1807                                         &setup, &params, &data);
1808                         END_PROFILE_NESTED(NT_transact_set_security_desc);
1809                         break;
1810                 case NT_TRANSACT_NOTIFY_CHANGE:
1811                         START_PROFILE_NESTED(NT_transact_notify_change);
1812                         outsize = call_nt_transact_notify_change(conn, inbuf, outbuf, 
1813                                         length, bufsize, 
1814                                         &setup, &params, &data);
1815                         END_PROFILE_NESTED(NT_transact_notify_change);
1816                         break;
1817                 case NT_TRANSACT_RENAME:
1818                         START_PROFILE_NESTED(NT_transact_rename);
1819                         outsize = call_nt_transact_rename(conn, inbuf, outbuf,
1820                                         length, bufsize,
1821                                         &setup, &params, &data);
1822                         END_PROFILE_NESTED(NT_transact_rename);
1823                         break;
1824
1825                 case NT_TRANSACT_QUERY_SECURITY_DESC:
1826                         START_PROFILE_NESTED(NT_transact_query_security_desc);
1827                         outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf, 
1828                                         length, bufsize, 
1829                                         &setup, &params, &data);
1830                         END_PROFILE_NESTED(NT_transact_query_security_desc);
1831                         break;
1832                 default:
1833                         /* Error in request */
1834                         DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1835                         SAFE_FREE(setup);
1836                         SAFE_FREE(params);
1837                         SAFE_FREE(data);
1838                         END_PROFILE(SMBnttrans);
1839                         return ERROR_DOS(ERRSRV,ERRerror);
1840         }
1841
1842         /* As we do not know how many data packets will need to be
1843                 returned here the various call_nt_transact_xxxx calls
1844                 must send their own. Thus a call_nt_transact_xxxx routine only
1845                 returns a value other than -1 when it wants to send
1846                 an error packet. 
1847         */
1848
1849         SAFE_FREE(setup);
1850         SAFE_FREE(params);
1851         SAFE_FREE(data);
1852         END_PROFILE(SMBnttrans);
1853         return outsize; /* If a correct response was needed the call_nt_transact_xxxx 
1854                                 calls have already sent it. If outsize != -1 then it is
1855                                 returning an error packet. */
1856 }