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