This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[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                 DEBUG(10,("map_share_mode: DELETE_ACCESS requested. open_mode = 0x%x\n", smb_open_mode));
416         }
417
418         if (create_options & FILE_DELETE_ON_CLOSE) {
419                 /* Implicit delete access is *NOT* requested... */
420                 smb_open_mode |= DELETE_ON_CLOSE_FLAG;
421                 DEBUG(10,("map_share_mode: FILE_DELETE_ON_CLOSE requested. open_mode = 0x%x\n", smb_open_mode));
422         }
423
424         /* Add in the requested share mode. */
425         switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
426                 case FILE_SHARE_READ:
427                         smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
428                         break;
429                 case FILE_SHARE_WRITE:
430                         smb_open_mode |= SET_DENY_MODE(DENY_READ);
431                         break;
432                 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
433                         smb_open_mode |= SET_DENY_MODE(DENY_NONE);
434                         break;
435                 case FILE_SHARE_NONE:
436                         smb_open_mode |= SET_DENY_MODE(DENY_ALL);
437                         break;
438         }
439
440         /*
441          * Handle an O_SYNC request.
442          */
443
444         if(file_attributes & FILE_FLAG_WRITE_THROUGH)
445                 smb_open_mode |= FILE_SYNC_OPENMODE;
446
447         DEBUG(10,("map_share_mode: Mapped desired access 0x%lx, share access 0x%lx, file attributes 0x%lx \
448 to open_mode 0x%x\n", (unsigned long)*desired_access, (unsigned long)share_access,
449                 (unsigned long)file_attributes, smb_open_mode ));
450  
451         return smb_open_mode;
452 }
453
454 /****************************************************************************
455  Reply to an NT create and X call on a pipe.
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_DOS(ERRSRV,ERRaccess));
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(inbuf, fname, smb_buf(inbuf), sizeof(fname), -1, 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                          * Check to see if this is a mac fork of some kind.
610                          */
611
612                         srvstr_pull(inbuf, fname, smb_buf(inbuf), sizeof(fname), -1, STR_TERMINATE);
613
614                         if( strchr_m(fname, ':')) {
615                                 END_PROFILE(SMBntcreateX);
616                                 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
617                         }
618                         END_PROFILE(SMBntcreateX);
619                         return(ERROR_DOS(ERRDOS,ERRbadfid));
620                 }
621
622                 /*
623                  * Copy in the base directory name.
624                  */
625
626                 pstrcpy( fname, dir_fsp->fsp_name );
627                 dir_name_len = strlen(fname);
628
629                 /*
630                  * Ensure it ends in a '\'.
631                  */
632
633                 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
634                         pstrcat(fname, "\\");
635                         dir_name_len++;
636                 }
637
638                 srvstr_pull(inbuf, &fname[dir_name_len], smb_buf(inbuf), sizeof(fname)-dir_name_len, 
639                         -1, STR_TERMINATE);
640         } else {
641                 srvstr_pull(inbuf, fname, smb_buf(inbuf), sizeof(fname), -1, STR_TERMINATE);
642         }
643         
644         /*
645          * Now contruct the smb_open_mode value from the filename, 
646          * desired access and the share access.
647          */
648         RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
649
650         if((smb_open_mode = map_share_mode(fname, create_options, &desired_access, 
651                                            share_access, 
652                                            file_attributes)) == -1) {
653                 END_PROFILE(SMBntcreateX);
654                 return ERROR_DOS(ERRDOS,ERRbadaccess);
655         }
656
657         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
658         if (oplock_request) {
659                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
660         }
661
662         /*
663          * Ordinary file or directory.
664          */
665                 
666         /*
667          * Check if POSIX semantics are wanted.
668          */
669                 
670         set_posix_case_semantics(file_attributes);
671                 
672         unix_convert(fname,conn,0,&bad_path,&sbuf);
673                 
674         unixmode = unix_mode(conn,smb_attr | aARCH, fname);
675     
676         /* 
677          * If it's a request for a directory open, deal with it separately.
678          */
679
680         if(create_options & FILE_DIRECTORY_FILE) {
681                 oplock_request = 0;
682                 
683                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
684                         
685                 restore_case_semantics(file_attributes);
686
687                 if(!fsp) {
688                         set_bad_path_error(errno, bad_path);
689                         END_PROFILE(SMBntcreateX);
690                         return(UNIXERROR(ERRDOS,ERRnoaccess));
691                 }
692         } else {
693                 /*
694                  * Ordinary file case.
695                  */
696
697                 /* NB. We have a potential bug here. If we
698                  * cause an oplock break to ourselves, then we
699                  * could end up processing filename related
700                  * SMB requests whilst we await the oplock
701                  * break response. As we may have changed the
702                  * filename case semantics to be POSIX-like,
703                  * this could mean a filename request could
704                  * fail when it should succeed. This is a rare
705                  * condition, but eventually we must arrange
706                  * to restore the correct case semantics
707                  * before issuing an oplock break request to
708                  * our client. JRA.  */
709
710                 fsp = open_file_shared1(conn,fname,&sbuf,
711                                         desired_access,
712                                         smb_open_mode,
713                                         smb_ofun,unixmode, oplock_request,
714                                         &rmode,&smb_action);
715
716                 if (!fsp) { 
717                         /* We cheat here. There are two cases we
718                          * care about. One is a directory rename,
719                          * where the NT client will attempt to
720                          * open the source directory for
721                          * DELETE access. Note that when the
722                          * NT client does this it does *not*
723                          * set the directory bit in the
724                          * request packet. This is translated
725                          * into a read/write open
726                          * request. POSIX states that any open
727                          * for write request on a directory
728                          * will generate an EISDIR error, so
729                          * we can catch this here and open a
730                          * pseudo handle that is flagged as a
731                          * directory. The second is an open
732                          * for a permissions read only, which
733                          * we handle in the open_file_stat case. JRA.
734                          */
735
736                         if(errno == EISDIR) {
737
738                                 /*
739                                  * Fail the open if it was explicitly a non-directory file.
740                                  */
741
742                                 if (create_options & FILE_NON_DIRECTORY_FILE) {
743                                         restore_case_semantics(file_attributes);
744                                         SSVAL(outbuf, smb_flg2, 
745                                               SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
746                                         END_PROFILE(SMBntcreateX);
747                                         return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
748                                 }
749         
750                                 oplock_request = 0;
751                                 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
752                                 
753                                 if(!fsp) {
754                                         restore_case_semantics(file_attributes);
755                                         set_bad_path_error(errno, bad_path);
756                                         END_PROFILE(SMBntcreateX);
757                                         return(UNIXERROR(ERRDOS,ERRnoaccess));
758                                 }
759                         } else {
760
761                                 restore_case_semantics(file_attributes);
762                                 set_bad_path_error(errno, bad_path);
763                                 END_PROFILE(SMBntcreateX);
764                                 return(UNIXERROR(ERRDOS,ERRnoaccess));
765                         }
766                 } 
767         }
768                 
769         restore_case_semantics(file_attributes);
770                 
771         file_len = sbuf.st_size;
772         fmode = dos_mode(conn,fname,&sbuf);
773         if(fmode == 0)
774                 fmode = FILE_ATTRIBUTE_NORMAL;
775         if (!fsp->is_directory && (fmode & aDIR)) {
776                 close_file(fsp,False);
777                 END_PROFILE(SMBntcreateX);
778                 return ERROR_DOS(ERRDOS,ERRnoaccess);
779         } 
780         
781         /* 
782          * If the caller set the extended oplock request bit
783          * and we granted one (by whatever means) - set the
784          * correct bit for extended oplock reply.
785          */
786         
787         if (oplock_request && lp_fake_oplocks(SNUM(conn)))
788                 smb_action |= EXTENDED_OPLOCK_GRANTED;
789         
790         if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
791                 smb_action |= EXTENDED_OPLOCK_GRANTED;
792
793 #if 0
794         /* W2K sends back 42 words here ! If we do the same it breaks offline sync. Go figure... ? JRA. */
795         set_message(outbuf,42,0,True);
796 #else
797         set_message(outbuf,34,0,True);
798 #endif
799         
800         p = outbuf + smb_vwv2;
801         
802         /*
803          * Currently as we don't support level II oplocks we just report
804          * exclusive & batch here.
805          */
806
807         if (smb_action & EXTENDED_OPLOCK_GRANTED) {
808                 if (flags & REQUEST_BATCH_OPLOCK) {
809                         SCVAL(p,0, BATCH_OPLOCK_RETURN);
810                 } else {
811                         SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
812                 }
813         } else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
814                 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
815         } else {
816                 SCVAL(p,0,NO_OPLOCK_RETURN);
817         }
818         
819         p++;
820         SSVAL(p,0,fsp->fnum);
821         p += 2;
822         SIVAL(p,0,smb_action);
823         p += 4;
824         
825         /* Create time. */  
826         c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
827
828         if (lp_dos_filetime_resolution(SNUM(conn))) {
829                 c_time &= ~1;
830                 sbuf.st_atime &= ~1;
831                 sbuf.st_mtime &= ~1;
832                 sbuf.st_mtime &= ~1;
833         }
834
835         put_long_date(p,c_time);
836         p += 8;
837         put_long_date(p,sbuf.st_atime); /* access time */
838         p += 8;
839         put_long_date(p,sbuf.st_mtime); /* write time */
840         p += 8;
841         put_long_date(p,sbuf.st_mtime); /* change time */
842         p += 8;
843         SIVAL(p,0,fmode); /* File Attributes. */
844         p += 4;
845         SOFF_T(p, 0, SMB_ROUNDUP_ALLOCATION(file_len));
846         p += 8;
847         SOFF_T(p,0,file_len);
848         p += 12;
849         SCVAL(p,0,fsp->is_directory ? 1 : 0);
850         
851         DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
852
853         result = chain_reply(inbuf,outbuf,length,bufsize);
854         END_PROFILE(SMBntcreateX);
855         return result;
856 }
857
858 /****************************************************************************
859  Reply to a NT_TRANSACT_CREATE call to open a pipe.
860 ****************************************************************************/
861
862 static int do_nt_transact_create_pipe( connection_struct *conn,
863                                         char *inbuf, char *outbuf, int length, 
864                                         int bufsize, char **ppsetup, char **ppparams, 
865                                         char **ppdata)
866 {
867         pstring fname;
868         int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
869         char *params = *ppparams;
870         int ret;
871         int pnum = -1;
872         char *p = NULL;
873
874         /*
875          * Ensure minimum number of parameters sent.
876          */
877
878         if(total_parameter_count < 54) {
879                 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
880                 return ERROR_DOS(ERRDOS,ERRbadaccess);
881         }
882
883         srvstr_pull(inbuf, fname, params+53, sizeof(fname), -1, STR_TERMINATE);
884
885     if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
886       return ret;
887
888         /* Realloc the size of parameters and data we will return */
889         params = Realloc(*ppparams, 69);
890         if(params == NULL)
891                 return ERROR_DOS(ERRDOS,ERRnomem);
892
893         *ppparams = params;
894
895         memset((char *)params,'\0',69);
896
897         p = params;
898         SCVAL(p,0,NO_OPLOCK_RETURN);
899
900         p += 2;
901         SSVAL(p,0,pnum);
902         p += 2;
903         SIVAL(p,0,FILE_WAS_OPENED);
904         p += 8;
905
906         p += 32;
907         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
908         p += 20;
909         /* File type. */
910         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
911         /* Device state. */
912         SSVAL(p,2, 0x5FF); /* ? */
913
914         DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
915
916         /* Send the required number of replies */
917         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
918
919         return -1;
920 }
921
922 /****************************************************************************
923  Internal fn to set security descriptors.
924 ****************************************************************************/
925
926 static BOOL set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent, int *pdef_class,uint32 *pdef_code)
927 {
928         prs_struct pd;
929         SEC_DESC *psd = NULL;
930         TALLOC_CTX *mem_ctx;
931         BOOL ret;
932
933         if (sd_len == 0) {
934                 *pdef_class = ERRDOS;
935                 *pdef_code = ERRbadaccess;
936                 return False;
937         }
938
939         /*
940          * Init the parse struct we will unmarshall from.
941          */
942
943         if ((mem_ctx = talloc_init()) == NULL) {
944                 DEBUG(0,("set_sd: talloc_init failed.\n"));
945                 *pdef_class = ERRDOS;
946                 *pdef_code = ERRnomem;
947                 return False;
948         }
949
950         prs_init(&pd, 0, mem_ctx, UNMARSHALL);
951
952         /*
953          * Setup the prs_struct to point at the memory we just
954          * allocated.
955          */
956         
957         prs_give_memory( &pd, data, sd_len, False);
958
959         /*
960          * Finally, unmarshall from the data buffer.
961          */
962
963         if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
964                 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
965                 /*
966                  * Return access denied for want of a better error message..
967                  */ 
968                 talloc_destroy(mem_ctx);
969                 *pdef_class = ERRDOS;
970                 *pdef_code = ERRnomem;
971                 return False;
972         }
973
974         if (psd->off_owner_sid==0)
975                 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
976         if (psd->off_grp_sid==0)
977                 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
978         if (psd->off_sacl==0)
979                 security_info_sent &= ~SACL_SECURITY_INFORMATION;
980         if (psd->off_dacl==0)
981                 security_info_sent &= ~DACL_SECURITY_INFORMATION;
982         
983         ret = fsp->conn->vfs_ops.fset_nt_acl( fsp, fsp->fd, security_info_sent, psd);
984
985         if (!ret) {
986                 talloc_destroy(mem_ctx);
987                 *pdef_class = ERRDOS;
988                 *pdef_code = ERRnoaccess;
989                 return False;
990         }
991
992         talloc_destroy(mem_ctx);
993
994         *pdef_class = 0;
995         *pdef_code = 0;
996         return True;
997 }
998
999 /****************************************************************************
1000  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1001 ****************************************************************************/
1002
1003 static int call_nt_transact_create(connection_struct *conn,
1004                                         char *inbuf, char *outbuf, int length, 
1005                                         int bufsize, char **ppsetup, char **ppparams, 
1006                                         char **ppdata)
1007 {
1008   pstring fname;
1009   char *params = *ppparams;
1010   char *data = *ppdata;
1011   int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1012   /* Breakout the oplock request bits so we can set the
1013      reply bits separately. */
1014   int oplock_request = 0;
1015   mode_t unixmode;
1016   int fmode=0,rmode=0;
1017   SMB_OFF_T file_len = 0;
1018   SMB_STRUCT_STAT sbuf;
1019   int smb_action = 0;
1020   BOOL bad_path = False;
1021   files_struct *fsp = NULL;
1022   char *p = NULL;
1023   uint32 flags;
1024   uint32 desired_access;
1025   uint32 file_attributes;
1026   uint32 share_access;
1027   uint32 create_disposition;
1028   uint32 create_options;
1029   uint32 sd_len;
1030   uint16 root_dir_fid;
1031   int smb_ofun;
1032   int smb_open_mode;
1033   int smb_attr;
1034   int error_class;
1035   uint32 error_code;
1036   time_t c_time;
1037
1038   DEBUG(5,("call_nt_transact_create\n"));
1039
1040   /*
1041    * If it's an IPC, use the pipe handler.
1042    */
1043
1044   if (IS_IPC(conn)) {
1045                 if (lp_nt_pipe_support())
1046                         return do_nt_transact_create_pipe(conn, inbuf, outbuf, length, 
1047                                         bufsize, ppsetup, ppparams, ppdata);
1048                 else
1049                         return ERROR_DOS(ERRDOS,ERRbadaccess);
1050   }
1051
1052   /*
1053    * Ensure minimum number of parameters sent.
1054    */
1055
1056   if(total_parameter_count < 54) {
1057     DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1058     return ERROR_DOS(ERRDOS,ERRbadaccess);
1059   }
1060
1061   flags = IVAL(params,0);
1062   desired_access = IVAL(params,8);
1063   file_attributes = IVAL(params,20);
1064   share_access = IVAL(params,24);
1065   create_disposition = IVAL(params,28);
1066   create_options = IVAL(params,32);
1067   sd_len = IVAL(params,36);
1068   root_dir_fid = (uint16)IVAL(params,4);
1069   smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1070
1071   /* 
1072    * We need to construct the open_and_X ofun value from the
1073    * NT values, as that's what our code is structured to accept.
1074    */    
1075
1076   if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1077     return ERROR_DOS(ERRDOS,ERRbadmem);
1078
1079   /*
1080    * Get the file name.
1081    */
1082
1083   if(root_dir_fid != 0) {
1084     /*
1085      * This filename is relative to a directory fid.
1086      */
1087
1088     files_struct *dir_fsp = file_fsp(params,4);
1089     size_t dir_name_len;
1090
1091     if(!dir_fsp)
1092         return ERROR_DOS(ERRDOS,ERRbadfid);
1093
1094     if(!dir_fsp->is_directory) {
1095       /*
1096        * Check to see if this is a mac fork of some kind.
1097        */
1098
1099       srvstr_pull(inbuf, fname, params+53, sizeof(fname), -1, STR_TERMINATE);
1100
1101       if( strchr_m(fname, ':')) {
1102           return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1103       }
1104
1105       return ERROR_DOS(ERRDOS,ERRbadfid);
1106     }
1107
1108     /*
1109      * Copy in the base directory name.
1110      */
1111
1112     pstrcpy( fname, dir_fsp->fsp_name );
1113     dir_name_len = strlen(fname);
1114
1115     /*
1116      * Ensure it ends in a '\'.
1117      */
1118
1119     if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1120       pstrcat(fname, "\\");
1121       dir_name_len++;
1122     }
1123
1124     srvstr_pull(inbuf, &fname[dir_name_len], params+53, sizeof(fname)-dir_name_len, 
1125                 -1, STR_TERMINATE);
1126   } else {
1127     srvstr_pull(inbuf, fname, params+53, sizeof(fname), -1, STR_TERMINATE);
1128   }
1129
1130   /*
1131    * Now contruct the smb_open_mode value from the desired access
1132    * and the share access.
1133    */
1134
1135   if((smb_open_mode = map_share_mode( fname, create_options, &desired_access,
1136                                       share_access, file_attributes)) == -1)
1137     return ERROR_DOS(ERRDOS,ERRbadaccess);
1138
1139   oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1140   oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1141
1142   /*
1143    * Check if POSIX semantics are wanted.
1144    */
1145
1146   set_posix_case_semantics(file_attributes);
1147     
1148   RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1149
1150   unix_convert(fname,conn,0,&bad_path,&sbuf);
1151     
1152   unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1153    
1154   /*
1155    * If it's a request for a directory open, deal with it separately.
1156    */
1157
1158   if(create_options & FILE_DIRECTORY_FILE) {
1159
1160     oplock_request = 0;
1161
1162     /*
1163      * We will get a create directory here if the Win32
1164      * app specified a security descriptor in the 
1165      * CreateDirectory() call.
1166      */
1167
1168     fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1169
1170     if(!fsp) {
1171       restore_case_semantics(file_attributes);
1172       set_bad_path_error(errno, bad_path);
1173       return(UNIXERROR(ERRDOS,ERRnoaccess));
1174     }
1175
1176   } else {
1177
1178     /*
1179      * Ordinary file case.
1180      */
1181
1182     fsp = open_file_shared1(conn,fname,&sbuf,desired_access,
1183                             smb_open_mode,smb_ofun,unixmode,
1184                      oplock_request,&rmode,&smb_action);
1185
1186     if (!fsp) { 
1187
1188                 if(errno == EISDIR) {
1189
1190                         /*
1191                          * Fail the open if it was explicitly a non-directory file.
1192                          */
1193
1194                         if (create_options & FILE_NON_DIRECTORY_FILE) {
1195                                 restore_case_semantics(file_attributes);
1196                                 SSVAL(outbuf, smb_flg2, 
1197                                       SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1198                                 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1199                         }
1200         
1201                         oplock_request = 0;
1202                         fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1203                                 
1204                         if(!fsp) {
1205                                 restore_case_semantics(file_attributes);
1206                                 set_bad_path_error(errno, bad_path);
1207                                 return(UNIXERROR(ERRDOS,ERRnoaccess));
1208                         }
1209                 } else {
1210
1211                         restore_case_semantics(file_attributes);
1212                         set_bad_path_error(errno, bad_path);
1213                         return(UNIXERROR(ERRDOS,ERRnoaccess));
1214                 }
1215       } 
1216   
1217       file_len = sbuf.st_size;
1218       fmode = dos_mode(conn,fname,&sbuf);
1219       if(fmode == 0)
1220         fmode = FILE_ATTRIBUTE_NORMAL;
1221
1222       if (fmode & aDIR) {
1223         close_file(fsp,False);
1224         restore_case_semantics(file_attributes);
1225         return ERROR_DOS(ERRDOS,ERRnoaccess);
1226       } 
1227
1228       /* 
1229        * If the caller set the extended oplock request bit
1230        * and we granted one (by whatever means) - set the
1231        * correct bit for extended oplock reply.
1232        */
1233     
1234       if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1235         smb_action |= EXTENDED_OPLOCK_GRANTED;
1236   
1237       if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1238         smb_action |= EXTENDED_OPLOCK_GRANTED;
1239   }
1240
1241   /*
1242    * Now try and apply the desired SD.
1243    */
1244
1245   if (!set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION, &error_class, &error_code)) {
1246     close_file(fsp,False);
1247     restore_case_semantics(file_attributes);
1248     return ERROR_DOS(error_class, error_code);
1249   }
1250
1251   restore_case_semantics(file_attributes);
1252
1253   /* Realloc the size of parameters and data we will return */
1254   params = Realloc(*ppparams, 69);
1255   if(params == NULL)
1256     return ERROR_DOS(ERRDOS,ERRnomem);
1257
1258   *ppparams = params;
1259
1260   memset((char *)params,'\0',69);
1261
1262   p = params;
1263   if (smb_action & EXTENDED_OPLOCK_GRANTED)     
1264         SCVAL(p,0, BATCH_OPLOCK_RETURN);
1265   else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1266     SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1267   else
1268         SCVAL(p,0,NO_OPLOCK_RETURN);
1269         
1270   p += 2;
1271   SSVAL(p,0,fsp->fnum);
1272   p += 2;
1273   SIVAL(p,0,smb_action);
1274   p += 8;
1275
1276   /* Create time. */
1277   c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1278
1279   if (lp_dos_filetime_resolution(SNUM(conn))) {
1280     c_time &= ~1;
1281     sbuf.st_atime &= ~1;
1282     sbuf.st_mtime &= ~1;
1283     sbuf.st_mtime &= ~1;
1284   }
1285
1286   put_long_date(p,c_time);
1287   p += 8;
1288   put_long_date(p,sbuf.st_atime); /* access time */
1289   p += 8;
1290   put_long_date(p,sbuf.st_mtime); /* write time */
1291   p += 8;
1292   put_long_date(p,sbuf.st_mtime); /* change time */
1293   p += 8;
1294   SIVAL(p,0,fmode); /* File Attributes. */
1295   p += 4;
1296   SOFF_T(p, 0, SMB_ROUNDUP_ALLOCATION(file_len));
1297   p += 8;
1298   SOFF_T(p,0,file_len);
1299
1300   DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1301
1302   /* Send the required number of replies */
1303   send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1304
1305   return -1;
1306 }
1307
1308 /****************************************************************************
1309  Reply to a NT CANCEL request.
1310 ****************************************************************************/
1311 int reply_ntcancel(connection_struct *conn,
1312                    char *inbuf,char *outbuf,int length,int bufsize)
1313 {
1314         /*
1315          * Go through and cancel any pending change notifies.
1316          */
1317         
1318         int mid = SVAL(inbuf,smb_mid);
1319         START_PROFILE(SMBntcancel);
1320         remove_pending_change_notify_requests_by_mid(mid);
1321         remove_pending_lock_requests_by_mid(mid);
1322         
1323         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1324
1325         END_PROFILE(SMBntcancel);
1326         return(-1);
1327 }
1328
1329 /****************************************************************************
1330  Reply to an unsolicited SMBNTtranss - just ignore it!
1331 ****************************************************************************/
1332 int reply_nttranss(connection_struct *conn,
1333                    char *inbuf,char *outbuf,int length,int bufsize)
1334 {
1335         START_PROFILE(SMBnttranss);
1336         DEBUG(4,("Ignoring nttranss of length %d\n",length));
1337         END_PROFILE(SMBnttranss);
1338         return(-1);
1339 }
1340
1341 /****************************************************************************
1342  Reply to a notify change - queue the request and 
1343  don't allow a directory to be opened.
1344 ****************************************************************************/
1345 static int call_nt_transact_notify_change(connection_struct *conn,
1346                                    char *inbuf, char *outbuf, int length,
1347                                    int bufsize, 
1348                                    char **ppsetup, 
1349                                    char **ppparams, char **ppdata)
1350 {
1351         char *setup = *ppsetup;
1352         files_struct *fsp;
1353         uint32 flags;
1354
1355         fsp = file_fsp(setup,4);
1356         flags = IVAL(setup, 0);
1357
1358         DEBUG(3,("call_nt_transact_notify_change\n"));
1359
1360         if(!fsp)
1361                 return ERROR_DOS(ERRDOS,ERRbadfid);
1362
1363         if((!fsp->is_directory) || (conn != fsp->conn))
1364                 return ERROR_DOS(ERRDOS,ERRbadfid);
1365
1366         if (!change_notify_set(inbuf, fsp, conn, flags))
1367                 return(UNIXERROR(ERRDOS,ERRbadfid));
1368
1369         DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1370 name = %s\n", fsp->fsp_name ));
1371
1372         return -1;
1373 }
1374
1375 /****************************************************************************
1376  Reply to an NT transact rename command.
1377 ****************************************************************************/
1378
1379 static int call_nt_transact_rename(connection_struct *conn,
1380                                    char *inbuf, char *outbuf, int length, 
1381                                    int bufsize,
1382                                    char **ppsetup, char **ppparams, char **ppdata)
1383 {
1384         char *params = *ppparams;
1385         pstring new_name;
1386         files_struct *fsp = file_fsp(params, 0);
1387         BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1388         NTSTATUS status;
1389
1390         CHECK_FSP(fsp, conn);
1391         srvstr_pull(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE);
1392
1393         status = rename_internals(conn, fsp->fsp_name,
1394                                   new_name, replace_if_exists);
1395         if (!NT_STATUS_IS_OK(status))
1396                 return ERROR_NT(status);
1397
1398         /*
1399          * Rename was successful.
1400          */
1401         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1402         
1403         DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n", 
1404                  fsp->fsp_name, new_name));
1405         
1406         /*
1407          * Win2k needs a changenotify request response before it will
1408          * update after a rename..
1409          */
1410         
1411         process_pending_change_notify_queue((time_t)0);
1412
1413         return -1;
1414 }
1415
1416 /******************************************************************************
1417  Fake up a completely empty SD.
1418 *******************************************************************************/
1419
1420 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1421 {
1422         extern DOM_SID global_sid_World;
1423         size_t sd_size;
1424
1425         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1426         if(!*ppsd) {
1427                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1428                 sd_size = 0;
1429         }
1430
1431         return sd_size;
1432 }
1433
1434 /****************************************************************************
1435  Reply to query a security descriptor - currently this is not implemented (it
1436  is planned to be though). Right now it just returns the same thing NT would
1437  when queried on a FAT filesystem. JRA.
1438 ****************************************************************************/
1439
1440 static int call_nt_transact_query_security_desc(connection_struct *conn,
1441                                                 char *inbuf, char *outbuf, 
1442                                                 int length, int bufsize, 
1443                                                 char **ppsetup, char **ppparams, char **ppdata)
1444 {
1445   uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1446   char *params = *ppparams;
1447   char *data = *ppdata;
1448   prs_struct pd;
1449   SEC_DESC *psd = NULL;
1450   size_t sd_size;
1451   TALLOC_CTX *mem_ctx;
1452
1453   files_struct *fsp = file_fsp(params,0);
1454
1455   if(!fsp)
1456     return ERROR_DOS(ERRDOS,ERRbadfid);
1457
1458   DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1459
1460   params = Realloc(*ppparams, 4);
1461   if(params == NULL)
1462     return ERROR_DOS(ERRDOS,ERRnomem);
1463
1464   *ppparams = params;
1465
1466   if ((mem_ctx = talloc_init()) == NULL) {
1467     DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1468     return ERROR_DOS(ERRDOS,ERRnomem);
1469   }
1470
1471   /*
1472    * Get the permissions to return.
1473    */
1474
1475   if (!lp_nt_acl_support(SNUM(conn)))
1476     sd_size = get_null_nt_acl(mem_ctx, &psd);
1477   else
1478     sd_size = conn->vfs_ops.fget_nt_acl(fsp, fsp->fd, &psd);
1479
1480   if (sd_size == 0) {
1481     talloc_destroy(mem_ctx);
1482     return(UNIXERROR(ERRDOS,ERRnoaccess));
1483   }
1484
1485   DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1486
1487   SIVAL(params,0,(uint32)sd_size);
1488
1489   if(max_data_count < sd_size) {
1490
1491     send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1492                     params, 4, *ppdata, 0);
1493     talloc_destroy(mem_ctx);
1494     return -1;
1495   }
1496
1497   /*
1498    * Allocate the data we will point this at.
1499    */
1500
1501   data = Realloc(*ppdata, sd_size);
1502   if(data == NULL) {
1503     talloc_destroy(mem_ctx);
1504     return ERROR_DOS(ERRDOS,ERRnomem);
1505   }
1506
1507   *ppdata = data;
1508
1509   memset(data, '\0', sd_size);
1510
1511   /*
1512    * Init the parse struct we will marshall into.
1513    */
1514
1515   prs_init(&pd, 0, mem_ctx, MARSHALL);
1516
1517   /*
1518    * Setup the prs_struct to point at the memory we just
1519    * allocated.
1520    */
1521
1522   prs_give_memory( &pd, data, (uint32)sd_size, False);
1523
1524   /*
1525    * Finally, linearize into the outgoing buffer.
1526    */
1527
1528   if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1529     DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1530 security descriptor.\n"));
1531     /*
1532      * Return access denied for want of a better error message..
1533      */ 
1534     talloc_destroy(mem_ctx);
1535     return(UNIXERROR(ERRDOS,ERRnoaccess));
1536   }
1537
1538   /*
1539    * Now we can delete the security descriptor.
1540    */
1541
1542   talloc_destroy(mem_ctx);
1543
1544   send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
1545   return -1;
1546 }
1547
1548 /****************************************************************************
1549  Reply to set a security descriptor. Map to UNIX perms.
1550 ****************************************************************************/
1551
1552 static int call_nt_transact_set_security_desc(connection_struct *conn,
1553                                                                         char *inbuf, char *outbuf, int length,
1554                                                                         int bufsize, char **ppsetup, 
1555                                                                         char **ppparams, char **ppdata)
1556 {
1557         uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1558         char *params= *ppparams;
1559         char *data = *ppdata;
1560         uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
1561         files_struct *fsp = NULL;
1562         uint32 security_info_sent = 0;
1563         int error_class;
1564         uint32 error_code;
1565
1566         if(total_parameter_count < 8)
1567                 return ERROR_DOS(ERRDOS,ERRbadfunc);
1568
1569         if((fsp = file_fsp(params,0)) == NULL)
1570                 return ERROR_DOS(ERRDOS,ERRbadfid);
1571
1572         if(!lp_nt_acl_support(SNUM(conn)))
1573                 goto done;
1574
1575         security_info_sent = IVAL(params,4);
1576
1577         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1578                 (unsigned int)security_info_sent ));
1579
1580         if (!set_sd( fsp, data, total_data_count, security_info_sent, &error_class, &error_code))
1581                 return ERROR_DOS(error_class, error_code);
1582
1583   done:
1584
1585         send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1586         return -1;
1587 }
1588    
1589 /****************************************************************************
1590  Reply to IOCTL - not implemented - no plans.
1591 ****************************************************************************/
1592 static int call_nt_transact_ioctl(connection_struct *conn,
1593                                   char *inbuf, char *outbuf, int length,
1594                                   int bufsize, 
1595                                   char **ppsetup, char **ppparams, char **ppdata)
1596 {
1597   static BOOL logged_message = False;
1598
1599   if(!logged_message) {
1600     DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1601     logged_message = True; /* Only print this once... */
1602   }
1603   return ERROR_DOS(ERRSRV,ERRnosupport);
1604 }
1605    
1606 /****************************************************************************
1607  Reply to a SMBNTtrans.
1608 ****************************************************************************/
1609 int reply_nttrans(connection_struct *conn,
1610                   char *inbuf,char *outbuf,int length,int bufsize)
1611 {
1612   int  outsize = 0;
1613 #if 0 /* Not used. */
1614   uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1615   uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1616   uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1617 #endif /* Not used. */
1618   uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1619   uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1620   uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1621   uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1622   uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1623   uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1624   uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1625   uint16 function_code = SVAL( inbuf, smb_nt_Function);
1626   char *params = NULL, *data = NULL, *setup = NULL;
1627   uint32 num_params_sofar, num_data_sofar;
1628   START_PROFILE(SMBnttrans);
1629
1630   if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1631     /*
1632      * Queue this open message as we are the process of an oplock break.
1633      */
1634
1635     DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1636 due to being in oplock break state.\n" ));
1637
1638     push_oplock_pending_smb_message( inbuf, length);
1639     END_PROFILE(SMBnttrans);
1640     return -1;
1641   }
1642
1643   if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
1644     END_PROFILE(SMBnttrans);
1645     return ERROR_DOS(ERRSRV,ERRaccess);
1646   }
1647
1648   outsize = set_message(outbuf,0,0,True);
1649
1650   /* 
1651    * All nttrans messages we handle have smb_wct == 19 + setup_count.
1652    * Ensure this is so as a sanity check.
1653    */
1654
1655   if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1656     DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1657           CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1658     END_PROFILE(SMBnttrans);
1659     return ERROR_DOS(ERRSRV,ERRerror);
1660   }
1661     
1662   /* Allocate the space for the setup, the maximum needed parameters and data */
1663
1664   if(setup_count > 0)
1665     setup = (char *)malloc(setup_count);
1666   if (total_parameter_count > 0)
1667     params = (char *)malloc(total_parameter_count);
1668   if (total_data_count > 0)
1669     data = (char *)malloc(total_data_count);
1670  
1671   if ((total_parameter_count && !params)  || (total_data_count && !data) ||
1672       (setup_count && !setup)) {
1673         SAFE_FREE(setup);
1674         SAFE_FREE(params);
1675         SAFE_FREE(data);
1676     DEBUG(0,("reply_nttrans : Out of memory\n"));
1677     END_PROFILE(SMBnttrans);
1678     return ERROR_DOS(ERRDOS,ERRnomem);
1679   }
1680
1681   /* Copy the param and data bytes sent with this request into
1682      the params buffer */
1683   num_params_sofar = parameter_count;
1684   num_data_sofar = data_count;
1685
1686   if (parameter_count > total_parameter_count || data_count > total_data_count)
1687     exit_server("reply_nttrans: invalid sizes in packet.");
1688
1689   if(setup) {
1690     memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1691     DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1692     dump_data(10, setup, setup_count);
1693   }
1694   if(params) {
1695     memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1696     DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1697     dump_data(10, params, parameter_count);
1698   }
1699   if(data) {
1700     memcpy( data, smb_base(inbuf) + data_offset, data_count);
1701     DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1702     dump_data(10, data, data_count);
1703   }
1704
1705   if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1706     /* We need to send an interim response then receive the rest
1707        of the parameter/data bytes */
1708     outsize = set_message(outbuf,0,0,True);
1709     if (!send_smb(smbd_server_fd(),outbuf))
1710       exit_server("reply_nttrans: send_smb failed.");
1711
1712     while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1713       BOOL ret;
1714
1715       ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
1716
1717       if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1718         outsize = set_message(outbuf,0,0,True);
1719         if(ret) {
1720                 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1721         } else {
1722                 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1723                          (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1724         }
1725         SAFE_FREE(params);
1726         SAFE_FREE(data);
1727         SAFE_FREE(setup);
1728         END_PROFILE(SMBnttrans);
1729         return ERROR_DOS(ERRSRV,ERRerror);
1730       }
1731       
1732       /* Revise total_params and total_data in case they have changed downwards */
1733       total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1734       total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1735       num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1736       num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1737       if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1738         exit_server("reply_nttrans2: data overflow in secondary nttrans packet");
1739
1740       memcpy( &params[ IVAL(inbuf, smb_nts_ParameterDisplacement)], 
1741               smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1742       memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1743               smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1744     }
1745   }
1746
1747   if (Protocol >= PROTOCOL_NT1)
1748     SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | FLAGS2_IS_LONG_NAME);
1749
1750   /* Now we must call the relevant NT_TRANS function */
1751   switch(function_code) {
1752     case NT_TRANSACT_CREATE:
1753       START_PROFILE_NESTED(NT_transact_create);
1754       outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize, 
1755                                         &setup, &params, &data);
1756       END_PROFILE_NESTED(NT_transact_create);
1757       break;
1758     case NT_TRANSACT_IOCTL:
1759       START_PROFILE_NESTED(NT_transact_ioctl);
1760       outsize = call_nt_transact_ioctl(conn, 
1761                                        inbuf, outbuf, length, bufsize, 
1762                                        &setup, &params, &data);
1763       END_PROFILE_NESTED(NT_transact_ioctl);
1764       break;
1765     case NT_TRANSACT_SET_SECURITY_DESC:
1766       START_PROFILE_NESTED(NT_transact_set_security_desc);
1767       outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf, 
1768                                                    length, bufsize, 
1769                                                    &setup, &params, &data);
1770       END_PROFILE_NESTED(NT_transact_set_security_desc);
1771       break;
1772     case NT_TRANSACT_NOTIFY_CHANGE:
1773       START_PROFILE_NESTED(NT_transact_notify_change);
1774       outsize = call_nt_transact_notify_change(conn, inbuf, outbuf, 
1775                                                length, bufsize, 
1776                                                &setup, &params, &data);
1777       END_PROFILE_NESTED(NT_transact_notify_change);
1778       break;
1779     case NT_TRANSACT_RENAME:
1780       START_PROFILE_NESTED(NT_transact_rename);
1781       outsize = call_nt_transact_rename(conn, inbuf, outbuf, length, 
1782                                         bufsize, 
1783                                         &setup, &params, &data);
1784       END_PROFILE_NESTED(NT_transact_rename);
1785       break;
1786
1787     case NT_TRANSACT_QUERY_SECURITY_DESC:
1788       START_PROFILE_NESTED(NT_transact_query_security_desc);
1789       outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf, 
1790                                                      length, bufsize, 
1791                                                      &setup, &params, &data);
1792       END_PROFILE_NESTED(NT_transact_query_security_desc);
1793       break;
1794   default:
1795           /* Error in request */
1796           DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1797           SAFE_FREE(setup);
1798           SAFE_FREE(params);
1799           SAFE_FREE(data);
1800           END_PROFILE(SMBnttrans);
1801           return ERROR_DOS(ERRSRV,ERRerror);
1802   }
1803
1804   /* As we do not know how many data packets will need to be
1805      returned here the various call_nt_transact_xxxx calls
1806      must send their own. Thus a call_nt_transact_xxxx routine only
1807      returns a value other than -1 when it wants to send
1808      an error packet. 
1809   */
1810
1811   SAFE_FREE(setup);
1812   SAFE_FREE(params);
1813   SAFE_FREE(data);
1814   END_PROFILE(SMBnttrans);
1815   return outsize; /* If a correct response was needed the call_nt_transact_xxxx 
1816                      calls have already sent it. If outsize != -1 then it is
1817                      returning an error packet. */
1818 }