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