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