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