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