Attempt to fix make test :-)
[tprouty/samba.git] / source / smbd / nttrans.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB NT transaction handling
4    Copyright (C) Jeremy Allison                 1994-2007
5    Copyright (C) Stefan (metze) Metzmacher      2003
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22
23 extern int max_send;
24 extern enum protocol_types Protocol;
25 extern struct current_user current_user;
26
27 static const char *known_nt_pipes[] = {
28         "\\LANMAN",
29         "\\srvsvc",
30         "\\samr",
31         "\\wkssvc",
32         "\\NETLOGON",
33         "\\ntlsa",
34         "\\ntsvcs",
35         "\\lsass",
36         "\\lsarpc",
37         "\\winreg",
38         "\\initshutdown",
39         "\\spoolss",
40         "\\netdfs",
41         "\\rpcecho",
42         "\\svcctl",
43         "\\eventlog",
44         "\\unixinfo",
45         NULL
46 };
47
48 static char *nttrans_realloc(char **ptr, size_t size)
49 {
50         if (ptr==NULL) {
51                 smb_panic("nttrans_realloc() called with NULL ptr");
52         }
53
54         *ptr = (char *)SMB_REALLOC(*ptr, size);
55         if(*ptr == NULL) {
56                 return NULL;
57         }
58         memset(*ptr,'\0',size);
59         return *ptr;
60 }
61
62 /****************************************************************************
63  Send the required number of replies back.
64  We assume all fields other than the data fields are
65  set correctly for the type of call.
66  HACK ! Always assumes smb_setup field is zero.
67 ****************************************************************************/
68
69 void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
70                      char *params, int paramsize,
71                      char *pdata, int datasize)
72 {
73         int data_to_send = datasize;
74         int params_to_send = paramsize;
75         int useable_space;
76         char *pp = params;
77         char *pd = pdata;
78         int params_sent_thistime, data_sent_thistime, total_sent_thistime;
79         int alignment_offset = 3;
80         int data_alignment_offset = 0;
81
82         /*
83          * If there genuinely are no parameters or data to send just send
84          * the empty packet.
85          */
86
87         if(params_to_send == 0 && data_to_send == 0) {
88                 reply_outbuf(req, 18, 0);
89                 show_msg((char *)req->outbuf);
90                 return;
91         }
92
93         /*
94          * When sending params and data ensure that both are nicely aligned.
95          * Only do this alignment when there is also data to send - else
96          * can cause NT redirector problems.
97          */
98
99         if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
100                 data_alignment_offset = 4 - (params_to_send % 4);
101         }
102
103         /*
104          * Space is bufsize minus Netbios over TCP header minus SMB header.
105          * The alignment_offset is to align the param bytes on a four byte
106          * boundary (2 bytes for data len, one byte pad).
107          * NT needs this to work correctly.
108          */
109
110         useable_space = max_send - (smb_size
111                                     + 2 * 18 /* wct */
112                                     + alignment_offset
113                                     + data_alignment_offset);
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                 reply_outbuf(req, 18, total_sent_thistime);
140
141                 /*
142                  * Set total params and data to be sent.
143                  */
144
145                 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
146                 SIVAL(req->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(req->outbuf, smb_ntr_ParameterCount,
158                       params_sent_thistime);
159
160                 if(params_sent_thistime == 0) {
161                         SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
162                         SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
163                 } else {
164                         /*
165                          * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
166                          * parameter bytes, however the first 4 bytes of outbuf are
167                          * the Netbios over TCP header. Thus use smb_base() to subtract
168                          * them from the calculation.
169                          */
170
171                         SIVAL(req->outbuf,smb_ntr_ParameterOffset,
172                               ((smb_buf(req->outbuf)+alignment_offset)
173                                - smb_base(req->outbuf)));
174                         /*
175                          * Absolute displacement of param bytes sent in this packet.
176                          */
177
178                         SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
179                               pp - params);
180                 }
181
182                 /*
183                  * Deal with the data portion.
184                  */
185
186                 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
187
188                 if(data_sent_thistime == 0) {
189                         SIVAL(req->outbuf,smb_ntr_DataOffset,0);
190                         SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
191                 } else {
192                         /*
193                          * The offset of the data bytes is the offset of the
194                          * parameter bytes plus the number of parameters being sent this time.
195                          */
196
197                         SIVAL(req->outbuf, smb_ntr_DataOffset,
198                               ((smb_buf(req->outbuf)+alignment_offset) -
199                                smb_base(req->outbuf))
200                               + params_sent_thistime + data_alignment_offset);
201                         SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
202                 }
203
204                 /*
205                  * Copy the param bytes into the packet.
206                  */
207
208                 if(params_sent_thistime) {
209                         if (alignment_offset != 0) {
210                                 memset(smb_buf(req->outbuf), 0,
211                                        alignment_offset);
212                         }
213                         memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
214                                params_sent_thistime);
215                 }
216
217                 /*
218                  * Copy in the data bytes
219                  */
220
221                 if(data_sent_thistime) {
222                         if (data_alignment_offset != 0) {
223                                 memset((smb_buf(req->outbuf)+alignment_offset+
224                                         params_sent_thistime), 0,
225                                        data_alignment_offset);
226                         }
227                         memcpy(smb_buf(req->outbuf)+alignment_offset
228                                +params_sent_thistime+data_alignment_offset,
229                                pd,data_sent_thistime);
230                 }
231
232                 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
233                         params_sent_thistime, data_sent_thistime, useable_space));
234                 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
235                         params_to_send, data_to_send, paramsize, datasize));
236
237                 if (NT_STATUS_V(nt_error)) {
238                         error_packet_set((char *)req->outbuf,
239                                          0, 0, nt_error,
240                                          __LINE__,__FILE__);
241                 }
242
243                 /* Send the packet */
244                 show_msg((char *)req->outbuf);
245                 if (!send_smb(smbd_server_fd(),(char *)req->outbuf)) {
246                         exit_server_cleanly("send_nt_replies: send_smb failed.");
247                 }
248
249                 TALLOC_FREE(req->outbuf);
250
251                 pp += params_sent_thistime;
252                 pd += data_sent_thistime;
253
254                 params_to_send -= params_sent_thistime;
255                 data_to_send -= data_sent_thistime;
256
257                 /*
258                  * Sanity check
259                  */
260
261                 if(params_to_send < 0 || data_to_send < 0) {
262                         DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
263                                 params_to_send, data_to_send));
264                         return;
265                 }
266         }
267 }
268
269 /****************************************************************************
270  Is it an NTFS stream name ?
271 ****************************************************************************/
272
273 bool is_ntfs_stream_name(const char *fname)
274 {
275         if (lp_posix_pathnames()) {
276                 return False;
277         }
278         return (strchr_m(fname, ':') != NULL) ? True : False;
279 }
280
281 /****************************************************************************
282  Reply to an NT create and X call on a pipe
283 ****************************************************************************/
284
285 static void nt_open_pipe(char *fname, connection_struct *conn,
286                          struct smb_request *req, int *ppnum)
287 {
288         smb_np_struct *p = NULL;
289         int i;
290
291         DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
292
293         /* See if it is one we want to handle. */
294
295         if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
296                 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
297                                 ERRDOS, ERRbadpipe);
298                 return;
299         }
300
301         for( i = 0; known_nt_pipes[i]; i++ ) {
302                 if( strequal(fname,known_nt_pipes[i])) {
303                         break;
304                 }
305         }
306
307         if ( known_nt_pipes[i] == NULL ) {
308                 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
309                                 ERRDOS, ERRbadpipe);
310                 return;
311         }
312
313         /* Strip \\ off the name. */
314         fname++;
315
316         DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
317
318         p = open_rpc_pipe_p(fname, conn, req->vuid);
319         if (!p) {
320                 reply_doserror(req, ERRSRV, ERRnofids);
321                 return;
322         }
323
324         /* TODO: Add pipe to db */
325
326         if ( !store_pipe_opendb( p ) ) {
327                 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
328         }
329
330         *ppnum = p->pnum;
331         return;
332 }
333
334 /****************************************************************************
335  Reply to an NT create and X call for pipes.
336 ****************************************************************************/
337
338 static void do_ntcreate_pipe_open(connection_struct *conn,
339                                   struct smb_request *req)
340 {
341         char *fname = NULL;
342         int pnum = -1;
343         char *p = NULL;
344         uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
345         TALLOC_CTX *ctx = talloc_tos();
346
347         srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
348                         smb_buf(req->inbuf), STR_TERMINATE);
349
350         if (!fname) {
351                 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
352                                 ERRDOS, ERRbadpipe);
353                 return;
354         }
355         nt_open_pipe(fname, conn, req, &pnum);
356
357         if (req->outbuf) {
358                 /* error reply */
359                 return;
360         }
361
362         /*
363          * Deal with pipe return.
364          */
365
366         if (flags & EXTENDED_RESPONSE_REQUIRED) {
367                 /* This is very strange. We
368                  * return 50 words, but only set
369                  * the wcnt to 42 ? It's definately
370                  * what happens on the wire....
371                  */
372                 reply_outbuf(req, 50, 0);
373                 SCVAL(req->outbuf,smb_wct,42);
374         } else {
375                 reply_outbuf(req, 34, 0);
376         }
377
378         p = (char *)req->outbuf + smb_vwv2;
379         p++;
380         SSVAL(p,0,pnum);
381         p += 2;
382         SIVAL(p,0,FILE_WAS_OPENED);
383         p += 4;
384         p += 32;
385         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
386         p += 20;
387         /* File type. */
388         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
389         /* Device state. */
390         SSVAL(p,2, 0x5FF); /* ? */
391         p += 4;
392
393         if (flags & EXTENDED_RESPONSE_REQUIRED) {
394                 p += 25;
395                 SIVAL(p,0,FILE_GENERIC_ALL);
396                 /*
397                  * For pipes W2K3 seems to return
398                  * 0x12019B next.
399                  * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
400                  */
401                 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
402         }
403
404         DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
405
406         chain_reply(req);
407 }
408
409 /****************************************************************************
410  Reply to an NT create and X call.
411 ****************************************************************************/
412
413 void reply_ntcreate_and_X(connection_struct *conn, struct smb_request *req)
414 {
415         char *fname = NULL;
416         uint32 flags;
417         uint32 access_mask;
418         uint32 file_attributes;
419         uint32 share_access;
420         uint32 create_disposition;
421         uint32 create_options;
422         uint16 root_dir_fid;
423         SMB_BIG_UINT allocation_size;
424         /* Breakout the oplock request bits so we can set the
425            reply bits separately. */
426         uint32 fattr=0;
427         SMB_OFF_T file_len = 0;
428         SMB_STRUCT_STAT sbuf;
429         int info = 0;
430         files_struct *fsp = NULL;
431         char *p = NULL;
432         struct timespec c_timespec;
433         struct timespec a_timespec;
434         struct timespec m_timespec;
435         NTSTATUS status;
436         int oplock_request;
437         uint8_t oplock_granted = NO_OPLOCK_RETURN;
438         TALLOC_CTX *ctx = talloc_tos();
439
440         START_PROFILE(SMBntcreateX);
441
442         if (req->wct < 24) {
443                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
444                 return;
445         }
446
447         flags = IVAL(req->inbuf,smb_ntcreate_Flags);
448         access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
449         file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
450         share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
451         create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
452         create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
453         root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
454
455         allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,
456                                              smb_ntcreate_AllocationSize);
457 #ifdef LARGE_SMB_OFF_T
458         allocation_size |= (((SMB_BIG_UINT)IVAL(
459                                      req->inbuf,
460                                      smb_ntcreate_AllocationSize + 4)) << 32);
461 #endif
462
463         srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
464                         smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
465
466         if (!NT_STATUS_IS_OK(status)) {
467                 reply_nterror(req, status);
468                 END_PROFILE(SMBntcreateX);
469                 return;
470         }
471
472         DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
473                   "file_attributes = 0x%x, share_access = 0x%x, "
474                   "create_disposition = 0x%x create_options = 0x%x "
475                   "root_dir_fid = 0x%x, fname = %s\n",
476                         (unsigned int)flags,
477                         (unsigned int)access_mask,
478                         (unsigned int)file_attributes,
479                         (unsigned int)share_access,
480                         (unsigned int)create_disposition,
481                         (unsigned int)create_options,
482                         (unsigned int)root_dir_fid,
483                         fname));
484
485         /*
486          * If it's an IPC, use the pipe handler.
487          */
488
489         if (IS_IPC(conn)) {
490                 if (lp_nt_pipe_support()) {
491                         do_ntcreate_pipe_open(conn, req);
492                         END_PROFILE(SMBntcreateX);
493                         return;
494                 } else {
495                         reply_doserror(req, ERRDOS, ERRnoaccess);
496                         END_PROFILE(SMBntcreateX);
497                         return;
498                 }
499         }
500
501         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
502         if (oplock_request) {
503                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
504                         ? BATCH_OPLOCK : 0;
505         }
506
507         status = create_file(conn, req, root_dir_fid, fname,
508                              access_mask, share_access, create_disposition,
509                              create_options, file_attributes, oplock_request,
510                              allocation_size, NULL, NULL, &fsp, &info, &sbuf);
511
512         if (!NT_STATUS_IS_OK(status)) {
513                 if (open_was_deferred(req->mid)) {
514                         /* We have re-scheduled this call, no error. */
515                         END_PROFILE(SMBntcreateX);
516                         return;
517                 }
518                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
519                         reply_botherror(req, status, ERRDOS, ERRfilexists);
520                 }
521                 else {
522                         reply_nterror(req, status);
523                 }
524                 END_PROFILE(SMBntcreateX);
525                 return;
526         }
527
528         /*
529          * If the caller set the extended oplock request bit
530          * and we granted one (by whatever means) - set the
531          * correct bit for extended oplock reply.
532          */
533
534         if (oplock_request &&
535             (lp_fake_oplocks(SNUM(conn))
536              || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
537
538                 /*
539                  * Exclusive oplock granted
540                  */
541
542                 if (flags & REQUEST_BATCH_OPLOCK) {
543                         oplock_granted = BATCH_OPLOCK_RETURN;
544                 } else {
545                         oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
546                 }
547         } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
548                 oplock_granted = LEVEL_II_OPLOCK_RETURN;
549         } else {
550                 oplock_granted = NO_OPLOCK_RETURN;
551         }
552
553         file_len = sbuf.st_size;
554         fattr = dos_mode(conn,fname,&sbuf);
555         if (fattr == 0) {
556                 fattr = FILE_ATTRIBUTE_NORMAL;
557         }
558
559         if (flags & EXTENDED_RESPONSE_REQUIRED) {
560                 /* This is very strange. We
561                  * return 50 words, but only set
562                  * the wcnt to 42 ? It's definately
563                  * what happens on the wire....
564                  */
565                 reply_outbuf(req, 50, 0);
566                 SCVAL(req->outbuf,smb_wct,42);
567         } else {
568                 reply_outbuf(req, 34, 0);
569         }
570
571         p = (char *)req->outbuf + smb_vwv2;
572
573         SCVAL(p, 0, oplock_granted);
574
575         p++;
576         SSVAL(p,0,fsp->fnum);
577         p += 2;
578         if ((create_disposition == FILE_SUPERSEDE)
579             && (info == FILE_WAS_OVERWRITTEN)) {
580                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
581         } else {
582                 SIVAL(p,0,info);
583         }
584         p += 4;
585
586         /* Create time. */
587         c_timespec = get_create_timespec(
588                 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
589         a_timespec = get_atimespec(&sbuf);
590         m_timespec = get_mtimespec(&sbuf);
591
592         if (lp_dos_filetime_resolution(SNUM(conn))) {
593                 dos_filetime_timespec(&c_timespec);
594                 dos_filetime_timespec(&a_timespec);
595                 dos_filetime_timespec(&m_timespec);
596         }
597
598         put_long_date_timespec(p, c_timespec); /* create time. */
599         p += 8;
600         put_long_date_timespec(p, a_timespec); /* access time */
601         p += 8;
602         put_long_date_timespec(p, m_timespec); /* write time */
603         p += 8;
604         put_long_date_timespec(p, m_timespec); /* change time */
605         p += 8;
606         SIVAL(p,0,fattr); /* File Attributes. */
607         p += 4;
608         SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
609         p += 8;
610         SOFF_T(p,0,file_len);
611         p += 8;
612         if (flags & EXTENDED_RESPONSE_REQUIRED) {
613                 SSVAL(p,2,0x7);
614         }
615         p += 4;
616         SCVAL(p,0,fsp->is_directory ? 1 : 0);
617
618         if (flags & EXTENDED_RESPONSE_REQUIRED) {
619                 uint32 perms = 0;
620                 p += 25;
621                 if (fsp->is_directory
622                     || can_write_to_file(conn, fname, &sbuf)) {
623                         perms = FILE_GENERIC_ALL;
624                 } else {
625                         perms = FILE_GENERIC_READ|FILE_EXECUTE;
626                 }
627                 SIVAL(p,0,perms);
628         }
629
630         DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
631                  fsp->fnum, fsp->fsp_name));
632
633         chain_reply(req);
634         END_PROFILE(SMBntcreateX);
635         return;
636 }
637
638 /****************************************************************************
639  Reply to a NT_TRANSACT_CREATE call to open a pipe.
640 ****************************************************************************/
641
642 static void do_nt_transact_create_pipe(connection_struct *conn,
643                                        struct smb_request *req,
644                                        uint16 **ppsetup, uint32 setup_count,
645                                        char **ppparams, uint32 parameter_count,
646                                        char **ppdata, uint32 data_count)
647 {
648         char *fname = NULL;
649         char *params = *ppparams;
650         int pnum = -1;
651         char *p = NULL;
652         NTSTATUS status;
653         size_t param_len;
654         uint32 flags;
655         TALLOC_CTX *ctx = talloc_tos();
656
657         /*
658          * Ensure minimum number of parameters sent.
659          */
660
661         if(parameter_count < 54) {
662                 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
663                 reply_doserror(req, ERRDOS, ERRnoaccess);
664                 return;
665         }
666
667         flags = IVAL(params,0);
668
669         srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
670                         parameter_count-53, STR_TERMINATE,
671                         &status);
672         if (!NT_STATUS_IS_OK(status)) {
673                 reply_nterror(req, status);
674                 return;
675         }
676
677         nt_open_pipe(fname, conn, req, &pnum);
678
679         if (req->outbuf) {
680                 /* Error return */
681                 return;
682         }
683
684         /* Realloc the size of parameters and data we will return */
685         if (flags & EXTENDED_RESPONSE_REQUIRED) {
686                 /* Extended response is 32 more byyes. */
687                 param_len = 101;
688         } else {
689                 param_len = 69;
690         }
691         params = nttrans_realloc(ppparams, param_len);
692         if(params == NULL) {
693                 reply_doserror(req, ERRDOS, ERRnomem);
694                 return;
695         }
696
697         p = params;
698         SCVAL(p,0,NO_OPLOCK_RETURN);
699
700         p += 2;
701         SSVAL(p,0,pnum);
702         p += 2;
703         SIVAL(p,0,FILE_WAS_OPENED);
704         p += 8;
705
706         p += 32;
707         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
708         p += 20;
709         /* File type. */
710         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
711         /* Device state. */
712         SSVAL(p,2, 0x5FF); /* ? */
713         p += 4;
714
715         if (flags & EXTENDED_RESPONSE_REQUIRED) {
716                 p += 25;
717                 SIVAL(p,0,FILE_GENERIC_ALL);
718                 /*
719                  * For pipes W2K3 seems to return
720                  * 0x12019B next.
721                  * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
722                  */
723                 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
724         }
725
726         DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
727
728         /* Send the required number of replies */
729         send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
730
731         return;
732 }
733
734 /****************************************************************************
735  Internal fn to set security descriptors.
736 ****************************************************************************/
737
738 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
739 {
740         prs_struct pd;
741         SEC_DESC *psd = NULL;
742         TALLOC_CTX *mem_ctx;
743         NTSTATUS status;
744
745         if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
746                 return NT_STATUS_OK;
747         }
748
749         /*
750          * Init the parse struct we will unmarshall from.
751          */
752
753         if ((mem_ctx = talloc_init("set_sd")) == NULL) {
754                 DEBUG(0,("set_sd: talloc_init failed.\n"));
755                 return NT_STATUS_NO_MEMORY;
756         }
757
758         prs_init(&pd, 0, mem_ctx, UNMARSHALL);
759
760         /*
761          * Setup the prs_struct to point at the memory we just
762          * allocated.
763          */
764
765         prs_give_memory( &pd, data, sd_len, False);
766
767         /*
768          * Finally, unmarshall from the data buffer.
769          */
770
771         if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
772                 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
773                 /*
774                  * Return access denied for want of a better error message..
775                  */
776                 talloc_destroy(mem_ctx);
777                 return NT_STATUS_NO_MEMORY;
778         }
779
780         if (psd->owner_sid==0) {
781                 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
782         }
783         if (psd->group_sid==0) {
784                 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
785         }
786         if (psd->sacl==0) {
787                 security_info_sent &= ~SACL_SECURITY_INFORMATION;
788         }
789         if (psd->dacl==0) {
790                 security_info_sent &= ~DACL_SECURITY_INFORMATION;
791         }
792
793         status = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
794
795         talloc_destroy(mem_ctx);
796         return status;
797 }
798
799 /****************************************************************************
800  Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
801 ****************************************************************************/
802
803 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
804 {
805         struct ea_list *ea_list_head = NULL;
806         size_t offset = 0;
807
808         if (data_size < 4) {
809                 return NULL;
810         }
811
812         while (offset + 4 <= data_size) {
813                 size_t next_offset = IVAL(pdata,offset);
814                 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
815
816                 if (!eal) {
817                         return NULL;
818                 }
819
820                 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
821                 if (next_offset == 0) {
822                         break;
823                 }
824                 offset += next_offset;
825         }
826
827         return ea_list_head;
828 }
829
830 /****************************************************************************
831  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
832 ****************************************************************************/
833
834 static void call_nt_transact_create(connection_struct *conn,
835                                     struct smb_request *req,
836                                     uint16 **ppsetup, uint32 setup_count,
837                                     char **ppparams, uint32 parameter_count,
838                                     char **ppdata, uint32 data_count,
839                                     uint32 max_data_count)
840 {
841         char *fname = NULL;
842         char *params = *ppparams;
843         char *data = *ppdata;
844         /* Breakout the oplock request bits so we can set the reply bits separately. */
845         uint32 fattr=0;
846         SMB_OFF_T file_len = 0;
847         SMB_STRUCT_STAT sbuf;
848         int info = 0;
849         files_struct *fsp = NULL;
850         char *p = NULL;
851         uint32 flags;
852         uint32 access_mask;
853         uint32 file_attributes;
854         uint32 share_access;
855         uint32 create_disposition;
856         uint32 create_options;
857         uint32 sd_len;
858         struct security_descriptor *sd = NULL;
859         uint32 ea_len;
860         uint16 root_dir_fid;
861         struct timespec c_timespec;
862         struct timespec a_timespec;
863         struct timespec m_timespec;
864         struct ea_list *ea_list = NULL;
865         NTSTATUS status;
866         size_t param_len;
867         SMB_BIG_UINT allocation_size;
868         int oplock_request;
869         uint8_t oplock_granted;
870         TALLOC_CTX *ctx = talloc_tos();
871
872         DEBUG(5,("call_nt_transact_create\n"));
873
874         /*
875          * If it's an IPC, use the pipe handler.
876          */
877
878         if (IS_IPC(conn)) {
879                 if (lp_nt_pipe_support()) {
880                         do_nt_transact_create_pipe(
881                                 conn, req,
882                                 ppsetup, setup_count,
883                                 ppparams, parameter_count,
884                                 ppdata, data_count);
885                         return;
886                 } else {
887                         reply_doserror(req, ERRDOS, ERRnoaccess);
888                         return;
889                 }
890         }
891
892         /*
893          * Ensure minimum number of parameters sent.
894          */
895
896         if(parameter_count < 54) {
897                 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
898                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
899                 return;
900         }
901
902         flags = IVAL(params,0);
903         access_mask = IVAL(params,8);
904         file_attributes = IVAL(params,20);
905         share_access = IVAL(params,24);
906         create_disposition = IVAL(params,28);
907         create_options = IVAL(params,32);
908         sd_len = IVAL(params,36);
909         ea_len = IVAL(params,40);
910         root_dir_fid = (uint16)IVAL(params,4);
911         allocation_size = (SMB_BIG_UINT)IVAL(params,12);
912 #ifdef LARGE_SMB_OFF_T
913         allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
914 #endif
915
916         /* Ensure the data_len is correct for the sd and ea values given. */
917         if ((ea_len + sd_len > data_count)
918             || (ea_len > data_count) || (sd_len > data_count)
919             || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
920                 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
921                            "%u, data_count = %u\n", (unsigned int)ea_len,
922                            (unsigned int)sd_len, (unsigned int)data_count));
923                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
924                 return;
925         }
926
927         if (sd_len) {
928                 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
929                            sd_len));
930
931                 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
932                                              &sd);
933                 if (!NT_STATUS_IS_OK(status)) {
934                         DEBUG(10, ("call_nt_transact_create: "
935                                    "unmarshall_sec_desc failed: %s\n",
936                                    nt_errstr(status)));
937                         reply_nterror(req, status);
938                         return;
939                 }
940         }
941
942         if (ea_len) {
943                 if (!lp_ea_support(SNUM(conn))) {
944                         DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
945                                    "EA's not supported.\n",
946                                    (unsigned int)ea_len));
947                         reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
948                         return;
949                 }
950
951                 if (ea_len < 10) {
952                         DEBUG(10,("call_nt_transact_create - ea_len = %u - "
953                                   "too small (should be more than 10)\n",
954                                   (unsigned int)ea_len ));
955                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
956                         return;
957                 }
958
959                 /* We have already checked that ea_len <= data_count here. */
960                 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
961                                                ea_len);
962                 if (ea_list == NULL) {
963                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
964                         return;
965                 }
966         }
967
968         srvstr_get_path(ctx, params, req->flags2, &fname,
969                         params+53, parameter_count-53,
970                         STR_TERMINATE, &status);
971         if (!NT_STATUS_IS_OK(status)) {
972                 reply_nterror(req, status);
973                 return;
974         }
975
976         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
977         if (oplock_request) {
978                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
979                         ? BATCH_OPLOCK : 0;
980         }
981
982         status = create_file(conn, req, root_dir_fid, fname,
983                              access_mask, share_access, create_disposition,
984                              create_options, file_attributes, oplock_request,
985                              allocation_size, sd, ea_list, &fsp, &info, &sbuf);
986
987         if(!NT_STATUS_IS_OK(status)) {
988                 if (open_was_deferred(req->mid)) {
989                         /* We have re-scheduled this call, no error. */
990                         return;
991                 }
992                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
993                         reply_botherror(req, status, ERRDOS, ERRfilexists);
994                 }
995                 else {
996                         reply_nterror(req, status);
997                 }
998                 return;
999         }
1000
1001         /*
1002          * If the caller set the extended oplock request bit
1003          * and we granted one (by whatever means) - set the
1004          * correct bit for extended oplock reply.
1005          */
1006
1007         if (oplock_request &&
1008             (lp_fake_oplocks(SNUM(conn))
1009              || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1010
1011                 /*
1012                  * Exclusive oplock granted
1013                  */
1014
1015                 if (flags & REQUEST_BATCH_OPLOCK) {
1016                         oplock_granted = BATCH_OPLOCK_RETURN;
1017                 } else {
1018                         oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1019                 }
1020         } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1021                 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1022         } else {
1023                 oplock_granted = NO_OPLOCK_RETURN;
1024         }
1025
1026         file_len = sbuf.st_size;
1027         fattr = dos_mode(conn,fname,&sbuf);
1028         if (fattr == 0) {
1029                 fattr = FILE_ATTRIBUTE_NORMAL;
1030         }
1031
1032         /* Realloc the size of parameters and data we will return */
1033         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1034                 /* Extended response is 32 more byyes. */
1035                 param_len = 101;
1036         } else {
1037                 param_len = 69;
1038         }
1039         params = nttrans_realloc(ppparams, param_len);
1040         if(params == NULL) {
1041                 reply_doserror(req, ERRDOS, ERRnomem);
1042                 return;
1043         }
1044
1045         p = params;
1046         SCVAL(p, 0, oplock_granted);
1047
1048         p += 2;
1049         SSVAL(p,0,fsp->fnum);
1050         p += 2;
1051         if ((create_disposition == FILE_SUPERSEDE)
1052             && (info == FILE_WAS_OVERWRITTEN)) {
1053                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1054         } else {
1055                 SIVAL(p,0,info);
1056         }
1057         p += 8;
1058
1059         /* Create time. */
1060         c_timespec = get_create_timespec(
1061                 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1062         a_timespec = get_atimespec(&sbuf);
1063         m_timespec = get_mtimespec(&sbuf);
1064
1065         if (lp_dos_filetime_resolution(SNUM(conn))) {
1066                 dos_filetime_timespec(&c_timespec);
1067                 dos_filetime_timespec(&a_timespec);
1068                 dos_filetime_timespec(&m_timespec);
1069         }
1070
1071         put_long_date_timespec(p, c_timespec); /* create time. */
1072         p += 8;
1073         put_long_date_timespec(p, a_timespec); /* access time */
1074         p += 8;
1075         put_long_date_timespec(p, m_timespec); /* write time */
1076         p += 8;
1077         put_long_date_timespec(p, m_timespec); /* change time */
1078         p += 8;
1079         SIVAL(p,0,fattr); /* File Attributes. */
1080         p += 4;
1081         SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1082         p += 8;
1083         SOFF_T(p,0,file_len);
1084         p += 8;
1085         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1086                 SSVAL(p,2,0x7);
1087         }
1088         p += 4;
1089         SCVAL(p,0,fsp->is_directory ? 1 : 0);
1090
1091         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1092                 uint32 perms = 0;
1093                 p += 25;
1094                 if (fsp->is_directory
1095                     || can_write_to_file(conn, fname, &sbuf)) {
1096                         perms = FILE_GENERIC_ALL;
1097                 } else {
1098                         perms = FILE_GENERIC_READ|FILE_EXECUTE;
1099                 }
1100                 SIVAL(p,0,perms);
1101         }
1102
1103         DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1104
1105         /* Send the required number of replies */
1106         send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1107
1108         return;
1109 }
1110
1111 /****************************************************************************
1112  Reply to a NT CANCEL request.
1113  conn POINTER CAN BE NULL HERE !
1114 ****************************************************************************/
1115
1116 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1117 {
1118         /*
1119          * Go through and cancel any pending change notifies.
1120          */
1121
1122         START_PROFILE(SMBntcancel);
1123         remove_pending_change_notify_requests_by_mid(req->mid);
1124         remove_pending_lock_requests_by_mid(req->mid);
1125         srv_cancel_sign_response(req->mid);
1126
1127         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1128
1129         END_PROFILE(SMBntcancel);
1130         return;
1131 }
1132
1133 /****************************************************************************
1134  Copy a file.
1135 ****************************************************************************/
1136
1137 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1138                                 connection_struct *conn,
1139                                 struct smb_request *req,
1140                                 const char *oldname_in,
1141                                 const char *newname_in,
1142                                 uint32 attrs)
1143 {
1144         SMB_STRUCT_STAT sbuf1, sbuf2;
1145         char *oldname = NULL;
1146         char *newname = NULL;
1147         char *last_component_oldname = NULL;
1148         char *last_component_newname = NULL;
1149         files_struct *fsp1,*fsp2;
1150         uint32 fattr;
1151         int info;
1152         SMB_OFF_T ret=-1;
1153         NTSTATUS status = NT_STATUS_OK;
1154
1155         ZERO_STRUCT(sbuf1);
1156         ZERO_STRUCT(sbuf2);
1157
1158         if (!CAN_WRITE(conn)) {
1159                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1160         }
1161
1162         status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1163                         &last_component_oldname, &sbuf1);
1164         if (!NT_STATUS_IS_OK(status)) {
1165                 return status;
1166         }
1167
1168         status = check_name(conn, oldname);
1169         if (!NT_STATUS_IS_OK(status)) {
1170                 return status;
1171         }
1172
1173         /* Source must already exist. */
1174         if (!VALID_STAT(sbuf1)) {
1175                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1176         }
1177         /* Ensure attributes match. */
1178         fattr = dos_mode(conn,oldname,&sbuf1);
1179         if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1180                 return NT_STATUS_NO_SUCH_FILE;
1181         }
1182
1183         status = unix_convert(ctx, conn, newname_in, False, &newname,
1184                         &last_component_newname, &sbuf2);
1185         if (!NT_STATUS_IS_OK(status)) {
1186                 return status;
1187         }
1188
1189         status = check_name(conn, newname);
1190         if (!NT_STATUS_IS_OK(status)) {
1191                 return status;
1192         }
1193
1194         /* Disallow if newname already exists. */
1195         if (VALID_STAT(sbuf2)) {
1196                 return NT_STATUS_OBJECT_NAME_COLLISION;
1197         }
1198
1199         /* No links from a directory. */
1200         if (S_ISDIR(sbuf1.st_mode)) {
1201                 return NT_STATUS_FILE_IS_A_DIRECTORY;
1202         }
1203
1204         /* Ensure this is within the share. */
1205         status = check_reduced_name(conn, oldname);
1206         if (!NT_STATUS_IS_OK(status)) {
1207                 return status;
1208         }
1209
1210         DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1211                                 oldname, newname));
1212
1213         status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1214                         FILE_READ_DATA, /* Read-only. */
1215                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1216                         FILE_OPEN,
1217                         0, /* No create options. */
1218                         FILE_ATTRIBUTE_NORMAL,
1219                         NO_OPLOCK,
1220                         &info, &fsp1);
1221
1222         if (!NT_STATUS_IS_OK(status)) {
1223                 return status;
1224         }
1225
1226         status = open_file_ntcreate(conn, req, newname, &sbuf2,
1227                         FILE_WRITE_DATA, /* Read-only. */
1228                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1229                         FILE_CREATE,
1230                         0, /* No create options. */
1231                         fattr,
1232                         NO_OPLOCK,
1233                         &info, &fsp2);
1234
1235         if (!NT_STATUS_IS_OK(status)) {
1236                 close_file(fsp1,ERROR_CLOSE);
1237                 return status;
1238         }
1239
1240         if (sbuf1.st_size) {
1241                 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1242         }
1243
1244         /*
1245          * As we are opening fsp1 read-only we only expect
1246          * an error on close on fsp2 if we are out of space.
1247          * Thus we don't look at the error return from the
1248          * close of fsp1.
1249          */
1250         close_file(fsp1,NORMAL_CLOSE);
1251
1252         /* Ensure the modtime is set correctly on the destination file. */
1253         fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1254
1255         status = close_file(fsp2,NORMAL_CLOSE);
1256
1257         /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1258            creates the file. This isn't the correct thing to do in the copy
1259            case. JRA */
1260         file_set_dosmode(conn, newname, fattr, &sbuf2,
1261                          parent_dirname(newname),false);
1262
1263         if (ret < (SMB_OFF_T)sbuf1.st_size) {
1264                 return NT_STATUS_DISK_FULL;
1265         }
1266
1267         if (!NT_STATUS_IS_OK(status)) {
1268                 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1269                         nt_errstr(status), oldname, newname));
1270         }
1271         return status;
1272 }
1273
1274 /****************************************************************************
1275  Reply to a NT rename request.
1276 ****************************************************************************/
1277
1278 void reply_ntrename(connection_struct *conn, struct smb_request *req)
1279 {
1280         char *oldname = NULL;
1281         char *newname = NULL;
1282         char *p;
1283         NTSTATUS status;
1284         bool src_has_wcard = False;
1285         bool dest_has_wcard = False;
1286         uint32 attrs;
1287         uint16 rename_type;
1288         TALLOC_CTX *ctx = talloc_tos();
1289
1290         START_PROFILE(SMBntrename);
1291
1292         if (req->wct < 4) {
1293                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1294                 END_PROFILE(SMBntrename);
1295                 return;
1296         }
1297
1298         attrs = SVAL(req->inbuf,smb_vwv0);
1299         rename_type = SVAL(req->inbuf,smb_vwv1);
1300
1301         p = smb_buf(req->inbuf) + 1;
1302         p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1303                                    0, STR_TERMINATE, &status,
1304                                    &src_has_wcard);
1305         if (!NT_STATUS_IS_OK(status)) {
1306                 reply_nterror(req, status);
1307                 END_PROFILE(SMBntrename);
1308                 return;
1309         }
1310
1311         if( is_ntfs_stream_name(oldname)) {
1312                 /* Can't rename a stream. */
1313                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1314                 END_PROFILE(SMBntrename);
1315                 return;
1316         }
1317
1318         if (ms_has_wild(oldname)) {
1319                 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1320                 END_PROFILE(SMBntrename);
1321                 return;
1322         }
1323
1324         p++;
1325         p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1326                                    0, STR_TERMINATE, &status,
1327                                    &dest_has_wcard);
1328         if (!NT_STATUS_IS_OK(status)) {
1329                 reply_nterror(req, status);
1330                 END_PROFILE(SMBntrename);
1331                 return;
1332         }
1333
1334         status = resolve_dfspath(ctx, conn,
1335                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
1336                                 oldname,
1337                                 &oldname);
1338         if (!NT_STATUS_IS_OK(status)) {
1339                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1340                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1341                                         ERRSRV, ERRbadpath);
1342                         END_PROFILE(SMBntrename);
1343                         return;
1344                 }
1345                 reply_nterror(req, status);
1346                 END_PROFILE(SMBntrename);
1347                 return;
1348         }
1349
1350         status = resolve_dfspath(ctx, conn,
1351                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
1352                                 newname,
1353                                 &newname);
1354         if (!NT_STATUS_IS_OK(status)) {
1355                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1356                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1357                                         ERRSRV, ERRbadpath);
1358                         END_PROFILE(SMBntrename);
1359                         return;
1360                 }
1361                 reply_nterror(req, status);
1362                 END_PROFILE(SMBntrename);
1363                 return;
1364         }
1365
1366         DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1367
1368         switch(rename_type) {
1369                 case RENAME_FLAG_RENAME:
1370                         status = rename_internals(ctx, conn, req, oldname,
1371                                         newname, attrs, False, src_has_wcard,
1372                                         dest_has_wcard);
1373                         break;
1374                 case RENAME_FLAG_HARD_LINK:
1375                         if (src_has_wcard || dest_has_wcard) {
1376                                 /* No wildcards. */
1377                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1378                         } else {
1379                                 status = hardlink_internals(ctx,
1380                                                 conn,
1381                                                 oldname,
1382                                                 newname);
1383                         }
1384                         break;
1385                 case RENAME_FLAG_COPY:
1386                         if (src_has_wcard || dest_has_wcard) {
1387                                 /* No wildcards. */
1388                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1389                         } else {
1390                                 status = copy_internals(ctx, conn, req, oldname,
1391                                                         newname, attrs);
1392                         }
1393                         break;
1394                 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1395                         status = NT_STATUS_INVALID_PARAMETER;
1396                         break;
1397                 default:
1398                         status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1399                         break;
1400         }
1401
1402         if (!NT_STATUS_IS_OK(status)) {
1403                 if (open_was_deferred(req->mid)) {
1404                         /* We have re-scheduled this call. */
1405                         END_PROFILE(SMBntrename);
1406                         return;
1407                 }
1408
1409                 reply_nterror(req, status);
1410                 END_PROFILE(SMBntrename);
1411                 return;
1412         }
1413
1414         reply_outbuf(req, 0, 0);
1415
1416         END_PROFILE(SMBntrename);
1417         return;
1418 }
1419
1420 /****************************************************************************
1421  Reply to a notify change - queue the request and
1422  don't allow a directory to be opened.
1423 ****************************************************************************/
1424
1425 static void call_nt_transact_notify_change(connection_struct *conn,
1426                                            struct smb_request *req,
1427                                            uint16 **ppsetup,
1428                                            uint32 setup_count,
1429                                            char **ppparams,
1430                                            uint32 parameter_count,
1431                                            char **ppdata, uint32 data_count,
1432                                            uint32 max_data_count,
1433                                            uint32 max_param_count)
1434 {
1435         uint16 *setup = *ppsetup;
1436         files_struct *fsp;
1437         uint32 filter;
1438         NTSTATUS status;
1439         bool recursive;
1440
1441         if(setup_count < 6) {
1442                 reply_doserror(req, ERRDOS, ERRbadfunc);
1443                 return;
1444         }
1445
1446         fsp = file_fsp(SVAL(setup,4));
1447         filter = IVAL(setup, 0);
1448         recursive = (SVAL(setup, 6) != 0) ? True : False;
1449
1450         DEBUG(3,("call_nt_transact_notify_change\n"));
1451
1452         if(!fsp) {
1453                 reply_doserror(req, ERRDOS, ERRbadfid);
1454                 return;
1455         }
1456
1457         {
1458                 char *filter_string;
1459
1460                 if (!(filter_string = notify_filter_string(NULL, filter))) {
1461                         reply_nterror(req,NT_STATUS_NO_MEMORY);
1462                         return;
1463                 }
1464
1465                 DEBUG(3,("call_nt_transact_notify_change: notify change "
1466                          "called on %s, filter = %s, recursive = %d\n",
1467                          fsp->fsp_name, filter_string, recursive));
1468
1469                 TALLOC_FREE(filter_string);
1470         }
1471
1472         if((!fsp->is_directory) || (conn != fsp->conn)) {
1473                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1474                 return;
1475         }
1476
1477         if (fsp->notify == NULL) {
1478
1479                 status = change_notify_create(fsp, filter, recursive);
1480
1481                 if (!NT_STATUS_IS_OK(status)) {
1482                         DEBUG(10, ("change_notify_create returned %s\n",
1483                                    nt_errstr(status)));
1484                         reply_nterror(req, status);
1485                         return;
1486                 }
1487         }
1488
1489         if (fsp->notify->num_changes != 0) {
1490
1491                 /*
1492                  * We've got changes pending, respond immediately
1493                  */
1494
1495                 /*
1496                  * TODO: write a torture test to check the filtering behaviour
1497                  * here.
1498                  */
1499
1500                 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
1501
1502                 /*
1503                  * change_notify_reply() above has independently sent its
1504                  * results
1505                  */
1506                 return;
1507         }
1508
1509         /*
1510          * No changes pending, queue the request
1511          */
1512
1513         status = change_notify_add_request(req->inbuf, max_param_count, filter,
1514                         recursive, fsp);
1515         if (!NT_STATUS_IS_OK(status)) {
1516                 reply_nterror(req, status);
1517         }
1518         return;
1519 }
1520
1521 /****************************************************************************
1522  Reply to an NT transact rename command.
1523 ****************************************************************************/
1524
1525 static void call_nt_transact_rename(connection_struct *conn,
1526                                     struct smb_request *req,
1527                                     uint16 **ppsetup, uint32 setup_count,
1528                                     char **ppparams, uint32 parameter_count,
1529                                     char **ppdata, uint32 data_count,
1530                                     uint32 max_data_count)
1531 {
1532         char *params = *ppparams;
1533         char *new_name = NULL;
1534         files_struct *fsp = NULL;
1535         bool replace_if_exists = False;
1536         bool dest_has_wcard = False;
1537         NTSTATUS status;
1538         TALLOC_CTX *ctx = talloc_tos();
1539
1540         if(parameter_count < 5) {
1541                 reply_doserror(req, ERRDOS, ERRbadfunc);
1542                 return;
1543         }
1544
1545         fsp = file_fsp(SVAL(params, 0));
1546         replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1547         if (!check_fsp(conn, req, fsp, &current_user)) {
1548                 return;
1549         }
1550         srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1551                               parameter_count - 4,
1552                               STR_TERMINATE, &status, &dest_has_wcard);
1553         if (!NT_STATUS_IS_OK(status)) {
1554                 reply_nterror(req, status);
1555                 return;
1556         }
1557
1558         status = rename_internals(ctx,
1559                         conn,
1560                         req,
1561                         fsp->fsp_name,
1562                         new_name,
1563                         0,
1564                         replace_if_exists,
1565                         False,
1566                         dest_has_wcard);
1567
1568         if (!NT_STATUS_IS_OK(status)) {
1569                 if (open_was_deferred(req->mid)) {
1570                         /* We have re-scheduled this call. */
1571                         return;
1572                 }
1573                 reply_nterror(req, status);
1574                 return;
1575         }
1576
1577         /*
1578          * Rename was successful.
1579          */
1580         send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
1581
1582         DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1583                  fsp->fsp_name, new_name));
1584
1585         return;
1586 }
1587
1588 /******************************************************************************
1589  Fake up a completely empty SD.
1590 *******************************************************************************/
1591
1592 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1593 {
1594         size_t sd_size;
1595
1596         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1597         if(!*ppsd) {
1598                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1599                 return NT_STATUS_NO_MEMORY;
1600         }
1601
1602         return NT_STATUS_OK;
1603 }
1604
1605 /****************************************************************************
1606  Reply to query a security descriptor.
1607 ****************************************************************************/
1608
1609 static void call_nt_transact_query_security_desc(connection_struct *conn,
1610                                                  struct smb_request *req,
1611                                                  uint16 **ppsetup,
1612                                                  uint32 setup_count,
1613                                                  char **ppparams,
1614                                                  uint32 parameter_count,
1615                                                  char **ppdata,
1616                                                  uint32 data_count,
1617                                                  uint32 max_data_count)
1618 {
1619         char *params = *ppparams;
1620         char *data = *ppdata;
1621         prs_struct pd;
1622         SEC_DESC *psd = NULL;
1623         size_t sd_size;
1624         uint32 security_info_wanted;
1625         TALLOC_CTX *mem_ctx;
1626         files_struct *fsp = NULL;
1627         NTSTATUS status;
1628
1629         if(parameter_count < 8) {
1630                 reply_doserror(req, ERRDOS, ERRbadfunc);
1631                 return;
1632         }
1633
1634         fsp = file_fsp(SVAL(params,0));
1635         if(!fsp) {
1636                 reply_doserror(req, ERRDOS, ERRbadfid);
1637                 return;
1638         }
1639
1640         security_info_wanted = IVAL(params,4);
1641
1642         DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1643                         (unsigned int)security_info_wanted ));
1644
1645         params = nttrans_realloc(ppparams, 4);
1646         if(params == NULL) {
1647                 reply_doserror(req, ERRDOS, ERRnomem);
1648                 return;
1649         }
1650
1651         if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
1652                 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1653                 reply_doserror(req, ERRDOS, ERRnomem);
1654                 return;
1655         }
1656
1657         /*
1658          * Get the permissions to return.
1659          */
1660
1661         if (!lp_nt_acl_support(SNUM(conn))) {
1662                 status = get_null_nt_acl(mem_ctx, &psd);
1663         } else {
1664                 status = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd,
1665                                              security_info_wanted, &psd);
1666         }
1667
1668         if (!NT_STATUS_IS_OK(status)) {
1669                 talloc_destroy(mem_ctx);
1670                 reply_nterror(req, status);
1671                 return;
1672         }
1673
1674         sd_size = sec_desc_size(psd);
1675
1676         DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1677
1678         SIVAL(params,0,(uint32)sd_size);
1679
1680         if(max_data_count < sd_size) {
1681
1682                 send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
1683                                 params, 4, *ppdata, 0);
1684                 talloc_destroy(mem_ctx);
1685                 return;
1686         }
1687
1688         /*
1689          * Allocate the data we will point this at.
1690          */
1691
1692         data = nttrans_realloc(ppdata, sd_size);
1693         if(data == NULL) {
1694                 talloc_destroy(mem_ctx);
1695                 reply_doserror(req, ERRDOS, ERRnomem);
1696                 return;
1697         }
1698
1699         /*
1700          * Init the parse struct we will marshall into.
1701          */
1702
1703         prs_init(&pd, 0, mem_ctx, MARSHALL);
1704
1705         /*
1706          * Setup the prs_struct to point at the memory we just
1707          * allocated.
1708          */
1709
1710         prs_give_memory( &pd, data, (uint32)sd_size, False);
1711
1712         /*
1713          * Finally, linearize into the outgoing buffer.
1714          */
1715
1716         if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1717                 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1718 security descriptor.\n"));
1719                 /*
1720                  * Return access denied for want of a better error message..
1721                  */
1722                 talloc_destroy(mem_ctx);
1723                 reply_unixerror(req, ERRDOS, ERRnoaccess);
1724                 return;
1725         }
1726
1727         /*
1728          * Now we can delete the security descriptor.
1729          */
1730
1731         talloc_destroy(mem_ctx);
1732
1733         send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1734         return;
1735 }
1736
1737 /****************************************************************************
1738  Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1739 ****************************************************************************/
1740
1741 static void call_nt_transact_set_security_desc(connection_struct *conn,
1742                                                struct smb_request *req,
1743                                                uint16 **ppsetup,
1744                                                uint32 setup_count,
1745                                                char **ppparams,
1746                                                uint32 parameter_count,
1747                                                char **ppdata,
1748                                                uint32 data_count,
1749                                                uint32 max_data_count)
1750 {
1751         char *params= *ppparams;
1752         char *data = *ppdata;
1753         files_struct *fsp = NULL;
1754         uint32 security_info_sent = 0;
1755         NTSTATUS nt_status;
1756
1757         if(parameter_count < 8) {
1758                 reply_doserror(req, ERRDOS, ERRbadfunc);
1759                 return;
1760         }
1761
1762         if((fsp = file_fsp(SVAL(params,0))) == NULL) {
1763                 reply_doserror(req, ERRDOS, ERRbadfid);
1764                 return;
1765         }
1766
1767         if(!lp_nt_acl_support(SNUM(conn))) {
1768                 goto done;
1769         }
1770
1771         security_info_sent = IVAL(params,4);
1772
1773         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1774                 (unsigned int)security_info_sent ));
1775
1776         if (data_count == 0) {
1777                 reply_doserror(req, ERRDOS, ERRnoaccess);
1778                 return;
1779         }
1780
1781         if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
1782                 reply_nterror(req, nt_status);
1783                 return;
1784         }
1785
1786   done:
1787
1788         send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
1789         return;
1790 }
1791
1792 /****************************************************************************
1793  Reply to NT IOCTL
1794 ****************************************************************************/
1795
1796 static void call_nt_transact_ioctl(connection_struct *conn,
1797                                    struct smb_request *req,
1798                                    uint16 **ppsetup, uint32 setup_count,
1799                                    char **ppparams, uint32 parameter_count,
1800                                    char **ppdata, uint32 data_count,
1801                                    uint32 max_data_count)
1802 {
1803         uint32 function;
1804         uint16 fidnum;
1805         files_struct *fsp;
1806         uint8 isFSctl;
1807         uint8 compfilter;
1808         static bool logged_message;
1809         char *pdata = *ppdata;
1810
1811         if (setup_count != 8) {
1812                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1813                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1814                 return;
1815         }
1816
1817         function = IVAL(*ppsetup, 0);
1818         fidnum = SVAL(*ppsetup, 4);
1819         isFSctl = CVAL(*ppsetup, 6);
1820         compfilter = CVAL(*ppsetup, 7);
1821
1822         DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
1823                  function, fidnum, isFSctl, compfilter));
1824
1825         fsp=file_fsp(fidnum);
1826         /* this check is done in each implemented function case for now
1827            because I don't want to break anything... --metze
1828         FSP_BELONGS_CONN(fsp,conn);*/
1829
1830         switch (function) {
1831         case FSCTL_SET_SPARSE:
1832                 /* pretend this succeeded - tho strictly we should
1833                    mark the file sparse (if the local fs supports it)
1834                    so we can know if we need to pre-allocate or not */
1835
1836                 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1837                 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
1838                 return;
1839
1840         case FSCTL_CREATE_OR_GET_OBJECT_ID:
1841         {
1842                 unsigned char objid[16];
1843
1844                 /* This should return the object-id on this file.
1845                  * I think I'll make this be the inode+dev. JRA.
1846                  */
1847
1848                 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1849
1850                 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
1851                         return;
1852                 }
1853
1854                 data_count = 64;
1855                 pdata = nttrans_realloc(ppdata, data_count);
1856                 if (pdata == NULL) {
1857                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1858                         return;
1859                 }
1860                 push_file_id_16(pdata, &fsp->file_id);
1861                 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1862                 push_file_id_16(pdata+32, &fsp->file_id);
1863                 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
1864                                 pdata, data_count);
1865                 return;
1866         }
1867
1868         case FSCTL_GET_REPARSE_POINT:
1869                 /* pretend this fail - my winXP does it like this
1870                  * --metze
1871                  */
1872
1873                 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1874                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1875                 return;
1876
1877         case FSCTL_SET_REPARSE_POINT:
1878                 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1879                  * --metze
1880                  */
1881
1882                 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1883                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1884                 return;
1885
1886         case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1887         {
1888                 /*
1889                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1890                  * and return their volume names.  If max_data_count is 16, then it is just
1891                  * asking for the number of volumes and length of the combined names.
1892                  *
1893                  * pdata is the data allocated by our caller, but that uses
1894                  * total_data_count (which is 0 in our case) rather than max_data_count.
1895                  * Allocate the correct amount and return the pointer to let
1896                  * it be deallocated when we return.
1897                  */
1898                 SHADOW_COPY_DATA *shadow_data = NULL;
1899                 TALLOC_CTX *shadow_mem_ctx = NULL;
1900                 bool labels = False;
1901                 uint32 labels_data_count = 0;
1902                 uint32 i;
1903                 char *cur_pdata;
1904
1905                 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
1906                         return;
1907                 }
1908
1909                 if (max_data_count < 16) {
1910                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1911                                 max_data_count));
1912                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1913                         return;
1914                 }
1915
1916                 if (max_data_count > 16) {
1917                         labels = True;
1918                 }
1919
1920                 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1921                 if (shadow_mem_ctx == NULL) {
1922                         DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1923                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1924                         return;
1925                 }
1926
1927                 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1928                 if (shadow_data == NULL) {
1929                         DEBUG(0,("TALLOC_ZERO() failed!\n"));
1930                         talloc_destroy(shadow_mem_ctx);
1931                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1932                         return;
1933                 }
1934
1935                 shadow_data->mem_ctx = shadow_mem_ctx;
1936
1937                 /*
1938                  * Call the VFS routine to actually do the work.
1939                  */
1940                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1941                         talloc_destroy(shadow_data->mem_ctx);
1942                         if (errno == ENOSYS) {
1943                                 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n", 
1944                                         conn->connectpath));
1945                                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1946                                 return;
1947                         } else {
1948                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n", 
1949                                         conn->connectpath));
1950                                 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1951                                 return;
1952                         }
1953                 }
1954
1955                 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1956
1957                 if (!labels) {
1958                         data_count = 16;
1959                 } else {
1960                         data_count = 12+labels_data_count+4;
1961                 }
1962
1963                 if (max_data_count<data_count) {
1964                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1965                                 max_data_count,data_count));
1966                         talloc_destroy(shadow_data->mem_ctx);
1967                         reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1968                         return;
1969                 }
1970
1971                 pdata = nttrans_realloc(ppdata, data_count);
1972                 if (pdata == NULL) {
1973                         talloc_destroy(shadow_data->mem_ctx);
1974                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1975                         return;
1976                 }
1977
1978                 cur_pdata = pdata;
1979
1980                 /* num_volumes 4 bytes */
1981                 SIVAL(pdata,0,shadow_data->num_volumes);
1982
1983                 if (labels) {
1984                         /* num_labels 4 bytes */
1985                         SIVAL(pdata,4,shadow_data->num_volumes);
1986                 }
1987
1988                 /* needed_data_count 4 bytes */
1989                 SIVAL(pdata,8,labels_data_count);
1990
1991                 cur_pdata+=12;
1992
1993                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1994                         shadow_data->num_volumes,fsp->fsp_name));
1995                 if (labels && shadow_data->labels) {
1996                         for (i=0;i<shadow_data->num_volumes;i++) {
1997                                 srvstr_push(pdata, req->flags2,
1998                                             cur_pdata, shadow_data->labels[i],
1999                                             2*sizeof(SHADOW_COPY_LABEL),
2000                                             STR_UNICODE|STR_TERMINATE);
2001                                 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2002                                 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2003                         }
2004                 }
2005
2006                 talloc_destroy(shadow_data->mem_ctx);
2007
2008                 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2009                                 pdata, data_count);
2010
2011                 return;
2012         }
2013
2014         case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2015         {
2016                 /* pretend this succeeded -
2017                  *
2018                  * we have to send back a list with all files owned by this SID
2019                  *
2020                  * but I have to check that --metze
2021                  */
2022                 DOM_SID sid;
2023                 uid_t uid;
2024                 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2025
2026                 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2027
2028                 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
2029                         return;
2030                 }
2031
2032                 /* unknown 4 bytes: this is not the length of the sid :-(  */
2033                 /*unknown = IVAL(pdata,0);*/
2034
2035                 sid_parse(pdata+4,sid_len,&sid);
2036                 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2037
2038                 if (!sid_to_uid(&sid, &uid)) {
2039                         DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2040                                 sid_string_static(&sid),(unsigned long)sid_len));
2041                         uid = (-1);
2042                 }
2043
2044                 /* we can take a look at the find source :-)
2045                  *
2046                  * find ./ -uid $uid  -name '*'   is what we need here
2047                  *
2048                  *
2049                  * and send 4bytes len and then NULL terminated unicode strings
2050                  * for each file
2051                  *
2052                  * but I don't know how to deal with the paged results
2053                  * (maybe we can hang the result anywhere in the fsp struct)
2054                  *
2055                  * we don't send all files at once
2056                  * and at the next we should *not* start from the beginning,
2057                  * so we have to cache the result
2058                  *
2059                  * --metze
2060                  */
2061
2062                 /* this works for now... */
2063                 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2064                 return;
2065         }
2066         default:
2067                 if (!logged_message) {
2068                         logged_message = True; /* Only print this once... */
2069                         DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2070                                  function));
2071                 }
2072         }
2073
2074         reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2075 }
2076
2077
2078 #ifdef HAVE_SYS_QUOTAS
2079 /****************************************************************************
2080  Reply to get user quota
2081 ****************************************************************************/
2082
2083 static void call_nt_transact_get_user_quota(connection_struct *conn,
2084                                             struct smb_request *req,
2085                                             uint16 **ppsetup,
2086                                             uint32 setup_count,
2087                                             char **ppparams,
2088                                             uint32 parameter_count,
2089                                             char **ppdata,
2090                                             uint32 data_count,
2091                                             uint32 max_data_count)
2092 {
2093         NTSTATUS nt_status = NT_STATUS_OK;
2094         char *params = *ppparams;
2095         char *pdata = *ppdata;
2096         char *entry;
2097         int data_len=0,param_len=0;
2098         int qt_len=0;
2099         int entry_len = 0;
2100         files_struct *fsp = NULL;
2101         uint16 level = 0;
2102         size_t sid_len;
2103         DOM_SID sid;
2104         bool start_enum = True;
2105         SMB_NTQUOTA_STRUCT qt;
2106         SMB_NTQUOTA_LIST *tmp_list;
2107         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2108
2109         ZERO_STRUCT(qt);
2110
2111         /* access check */
2112         if (current_user.ut.uid != 0) {
2113                 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2114                         lp_servicename(SNUM(conn)),conn->user));
2115                 reply_doserror(req, ERRDOS, ERRnoaccess);
2116                 return;
2117         }
2118
2119         /*
2120          * Ensure minimum number of parameters sent.
2121          */
2122
2123         if (parameter_count < 4) {
2124                 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2125                 reply_doserror(req, ERRDOS, ERRinvalidparam);
2126                 return;
2127         }
2128
2129         /* maybe we can check the quota_fnum */
2130         fsp = file_fsp(SVAL(params,0));
2131         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2132                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2133                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2134                 return;
2135         }
2136
2137         /* the NULL pointer checking for fsp->fake_file_handle->pd
2138          * is done by CHECK_NTQUOTA_HANDLE_OK()
2139          */
2140         qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2141
2142         level = SVAL(params,2);
2143
2144         /* unknown 12 bytes leading in params */
2145
2146         switch (level) {
2147                 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2148                         /* seems that we should continue with the enum here --metze */
2149
2150                         if (qt_handle->quota_list!=NULL &&
2151                             qt_handle->tmp_list==NULL) {
2152
2153                                 /* free the list */
2154                                 free_ntquota_list(&(qt_handle->quota_list));
2155
2156                                 /* Realloc the size of parameters and data we will return */
2157                                 param_len = 4;
2158                                 params = nttrans_realloc(ppparams, param_len);
2159                                 if(params == NULL) {
2160                                         reply_doserror(req, ERRDOS, ERRnomem);
2161                                         return;
2162                                 }
2163
2164                                 data_len = 0;
2165                                 SIVAL(params,0,data_len);
2166
2167                                 break;
2168                         }
2169
2170                         start_enum = False;
2171
2172                 case TRANSACT_GET_USER_QUOTA_LIST_START:
2173
2174                         if (qt_handle->quota_list==NULL &&
2175                                 qt_handle->tmp_list==NULL) {
2176                                 start_enum = True;
2177                         }
2178
2179                         if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2180                                 reply_doserror(req, ERRSRV, ERRerror);
2181                                 return;
2182                         }
2183
2184                         /* Realloc the size of parameters and data we will return */
2185                         param_len = 4;
2186                         params = nttrans_realloc(ppparams, param_len);
2187                         if(params == NULL) {
2188                                 reply_doserror(req, ERRDOS, ERRnomem);
2189                                 return;
2190                         }
2191
2192                         /* we should not trust the value in max_data_count*/
2193                         max_data_count = MIN(max_data_count,2048);
2194
2195                         pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2196                         if(pdata == NULL) {
2197                                 reply_doserror(req, ERRDOS, ERRnomem);
2198                                 return;
2199                         }
2200
2201                         entry = pdata;
2202
2203                         /* set params Size of returned Quota Data 4 bytes*/
2204                         /* but set it later when we know it */
2205
2206                         /* for each entry push the data */
2207
2208                         if (start_enum) {
2209                                 qt_handle->tmp_list = qt_handle->quota_list;
2210                         }
2211
2212                         tmp_list = qt_handle->tmp_list;
2213
2214                         for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2215                                 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2216
2217                                 sid_len = sid_size(&tmp_list->quotas->sid);
2218                                 entry_len = 40 + sid_len;
2219
2220                                 /* nextoffset entry 4 bytes */
2221                                 SIVAL(entry,0,entry_len);
2222
2223                                 /* then the len of the SID 4 bytes */
2224                                 SIVAL(entry,4,sid_len);
2225
2226                                 /* unknown data 8 bytes SMB_BIG_UINT */
2227                                 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2228
2229                                 /* the used disk space 8 bytes SMB_BIG_UINT */
2230                                 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2231
2232                                 /* the soft quotas 8 bytes SMB_BIG_UINT */
2233                                 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2234
2235                                 /* the hard quotas 8 bytes SMB_BIG_UINT */
2236                                 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2237
2238                                 /* and now the SID */
2239                                 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2240                         }
2241
2242                         qt_handle->tmp_list = tmp_list;
2243
2244                         /* overwrite the offset of the last entry */
2245                         SIVAL(entry-entry_len,0,0);
2246
2247                         data_len = 4+qt_len;
2248                         /* overwrite the params quota_data_len */
2249                         SIVAL(params,0,data_len);
2250
2251                         break;
2252
2253                 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2254
2255                         /* unknown 4 bytes IVAL(pdata,0) */
2256
2257                         if (data_count < 8) {
2258                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2259                                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2260                                 return;
2261                         }
2262
2263                         sid_len = IVAL(pdata,4);
2264                         /* Ensure this is less than 1mb. */
2265                         if (sid_len > (1024*1024)) {
2266                                 reply_doserror(req, ERRDOS, ERRnomem);
2267                                 return;
2268                         }
2269
2270                         if (data_count < 8+sid_len) {
2271                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2272                                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2273                                 return;
2274                         }
2275
2276                         data_len = 4+40+sid_len;
2277
2278                         if (max_data_count < data_len) {
2279                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2280                                         max_data_count, data_len));
2281                                 param_len = 4;
2282                                 SIVAL(params,0,data_len);
2283                                 data_len = 0;
2284                                 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2285                                 break;
2286                         }
2287
2288                         sid_parse(pdata+8,sid_len,&sid);
2289
2290                         if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2291                                 ZERO_STRUCT(qt);
2292                                 /*
2293                                  * we have to return zero's in all fields
2294                                  * instead of returning an error here
2295                                  * --metze
2296                                  */
2297                         }
2298
2299                         /* Realloc the size of parameters and data we will return */
2300                         param_len = 4;
2301                         params = nttrans_realloc(ppparams, param_len);
2302                         if(params == NULL) {
2303                                 reply_doserror(req, ERRDOS, ERRnomem);
2304                                 return;
2305                         }
2306
2307                         pdata = nttrans_realloc(ppdata, data_len);
2308                         if(pdata == NULL) {
2309                                 reply_doserror(req, ERRDOS, ERRnomem);
2310                                 return;
2311                         }
2312
2313                         entry = pdata;
2314
2315                         /* set params Size of returned Quota Data 4 bytes*/
2316                         SIVAL(params,0,data_len);
2317
2318                         /* nextoffset entry 4 bytes */
2319                         SIVAL(entry,0,0);
2320
2321                         /* then the len of the SID 4 bytes */
2322                         SIVAL(entry,4,sid_len);
2323
2324                         /* unknown data 8 bytes SMB_BIG_UINT */
2325                         SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2326
2327                         /* the used disk space 8 bytes SMB_BIG_UINT */
2328                         SBIG_UINT(entry,16,qt.usedspace);
2329
2330                         /* the soft quotas 8 bytes SMB_BIG_UINT */
2331                         SBIG_UINT(entry,24,qt.softlim);
2332
2333                         /* the hard quotas 8 bytes SMB_BIG_UINT */
2334                         SBIG_UINT(entry,32,qt.hardlim);
2335
2336                         /* and now the SID */
2337                         sid_linearize(entry+40, sid_len, &sid);
2338
2339                         break;
2340
2341                 default:
2342                         DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2343                         reply_doserror(req, ERRSRV, ERRerror);
2344                         return;
2345                         break;
2346         }
2347
2348         send_nt_replies(req, nt_status, params, param_len,
2349                         pdata, data_len);
2350 }
2351
2352 /****************************************************************************
2353  Reply to set user quota
2354 ****************************************************************************/
2355
2356 static void call_nt_transact_set_user_quota(connection_struct *conn,
2357                                             struct smb_request *req,
2358                                             uint16 **ppsetup,
2359                                             uint32 setup_count,
2360                                             char **ppparams,
2361                                             uint32 parameter_count,
2362                                             char **ppdata,
2363                                             uint32 data_count,
2364                                             uint32 max_data_count)
2365 {
2366         char *params = *ppparams;
2367         char *pdata = *ppdata;
2368         int data_len=0,param_len=0;
2369         SMB_NTQUOTA_STRUCT qt;
2370         size_t sid_len;
2371         DOM_SID sid;
2372         files_struct *fsp = NULL;
2373
2374         ZERO_STRUCT(qt);
2375
2376         /* access check */
2377         if (current_user.ut.uid != 0) {
2378                 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2379                         lp_servicename(SNUM(conn)),conn->user));
2380                 reply_doserror(req, ERRDOS, ERRnoaccess);
2381                 return;
2382         }
2383
2384         /*
2385          * Ensure minimum number of parameters sent.
2386          */
2387
2388         if (parameter_count < 2) {
2389                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2390                 reply_doserror(req, ERRDOS, ERRinvalidparam);
2391                 return;
2392         }
2393
2394         /* maybe we can check the quota_fnum */
2395         fsp = file_fsp(SVAL(params,0));
2396         if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2397                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2398                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2399                 return;
2400         }
2401
2402         if (data_count < 40) {
2403                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2404                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2405                 return;
2406         }
2407
2408         /* offset to next quota record.
2409          * 4 bytes IVAL(pdata,0)
2410          * unused here...
2411          */
2412
2413         /* sid len */
2414         sid_len = IVAL(pdata,4);
2415
2416         if (data_count < 40+sid_len) {
2417                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2418                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2419                 return;
2420         }
2421
2422         /* unknown 8 bytes in pdata
2423          * maybe its the change time in NTTIME
2424          */
2425
2426         /* the used space 8 bytes (SMB_BIG_UINT)*/
2427         qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2428 #ifdef LARGE_SMB_OFF_T
2429         qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2430 #else /* LARGE_SMB_OFF_T */
2431         if ((IVAL(pdata,20) != 0)&&
2432                 ((qt.usedspace != 0xFFFFFFFF)||
2433                 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2434                 /* more than 32 bits? */
2435                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2436                 return;
2437         }
2438 #endif /* LARGE_SMB_OFF_T */
2439
2440         /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2441         qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2442 #ifdef LARGE_SMB_OFF_T
2443         qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2444 #else /* LARGE_SMB_OFF_T */
2445         if ((IVAL(pdata,28) != 0)&&
2446                 ((qt.softlim != 0xFFFFFFFF)||
2447                 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2448                 /* more than 32 bits? */
2449                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2450                 return;
2451         }
2452 #endif /* LARGE_SMB_OFF_T */
2453
2454         /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2455         qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2456 #ifdef LARGE_SMB_OFF_T
2457         qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2458 #else /* LARGE_SMB_OFF_T */
2459         if ((IVAL(pdata,36) != 0)&&
2460                 ((qt.hardlim != 0xFFFFFFFF)||
2461                 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2462                 /* more than 32 bits? */
2463                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2464                 return;
2465         }
2466 #endif /* LARGE_SMB_OFF_T */
2467
2468         sid_parse(pdata+40,sid_len,&sid);
2469         DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2470
2471         /* 44 unknown bytes left... */
2472
2473         if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2474                 reply_doserror(req, ERRSRV, ERRerror);
2475                 return;
2476         }
2477
2478         send_nt_replies(req, NT_STATUS_OK, params, param_len,
2479                         pdata, data_len);
2480 }
2481 #endif /* HAVE_SYS_QUOTAS */
2482
2483 static void handle_nttrans(connection_struct *conn,
2484                            struct trans_state *state,
2485                            struct smb_request *req)
2486 {
2487         if (Protocol >= PROTOCOL_NT1) {
2488                 req->flags2 |= 0x40; /* IS_LONG_NAME */
2489                 SSVAL(req->inbuf,smb_flg2,req->flags2);
2490         }
2491
2492         /* Now we must call the relevant NT_TRANS function */
2493         switch(state->call) {
2494                 case NT_TRANSACT_CREATE:
2495                 {
2496                         START_PROFILE(NT_transact_create);
2497                         call_nt_transact_create(
2498                                 conn, req,
2499                                 &state->setup, state->setup_count,
2500                                 &state->param, state->total_param,
2501                                 &state->data, state->total_data,
2502                                 state->max_data_return);
2503                         END_PROFILE(NT_transact_create);
2504                         break;
2505                 }
2506
2507                 case NT_TRANSACT_IOCTL:
2508                 {
2509                         START_PROFILE(NT_transact_ioctl);
2510                         call_nt_transact_ioctl(
2511                                 conn, req,
2512                                 &state->setup, state->setup_count,
2513                                 &state->param, state->total_param,
2514                                 &state->data, state->total_data,
2515                                 state->max_data_return);
2516                         END_PROFILE(NT_transact_ioctl);
2517                         break;
2518                 }
2519
2520                 case NT_TRANSACT_SET_SECURITY_DESC:
2521                 {
2522                         START_PROFILE(NT_transact_set_security_desc);
2523                         call_nt_transact_set_security_desc(
2524                                 conn, req,
2525                                 &state->setup, state->setup_count,
2526                                 &state->param, state->total_param,
2527                                 &state->data, state->total_data,
2528                                 state->max_data_return);
2529                         END_PROFILE(NT_transact_set_security_desc);
2530                         break;
2531                 }
2532
2533                 case NT_TRANSACT_NOTIFY_CHANGE:
2534                 {
2535                         START_PROFILE(NT_transact_notify_change);
2536                         call_nt_transact_notify_change(
2537                                 conn, req,
2538                                 &state->setup, state->setup_count,
2539                                 &state->param, state->total_param,
2540                                 &state->data, state->total_data,
2541                                 state->max_data_return,
2542                                 state->max_param_return);
2543                         END_PROFILE(NT_transact_notify_change);
2544                         break;
2545                 }
2546
2547                 case NT_TRANSACT_RENAME:
2548                 {
2549                         START_PROFILE(NT_transact_rename);
2550                         call_nt_transact_rename(
2551                                 conn, req,
2552                                 &state->setup, state->setup_count,
2553                                 &state->param, state->total_param,
2554                                 &state->data, state->total_data,
2555                                 state->max_data_return);
2556                         END_PROFILE(NT_transact_rename);
2557                         break;
2558                 }
2559
2560                 case NT_TRANSACT_QUERY_SECURITY_DESC:
2561                 {
2562                         START_PROFILE(NT_transact_query_security_desc);
2563                         call_nt_transact_query_security_desc(
2564                                 conn, req,
2565                                 &state->setup, state->setup_count,
2566                                 &state->param, state->total_param,
2567                                 &state->data, state->total_data,
2568                                 state->max_data_return);
2569                         END_PROFILE(NT_transact_query_security_desc);
2570                         break;
2571                 }
2572
2573 #ifdef HAVE_SYS_QUOTAS
2574                 case NT_TRANSACT_GET_USER_QUOTA:
2575                 {
2576                         START_PROFILE(NT_transact_get_user_quota);
2577                         call_nt_transact_get_user_quota(
2578                                 conn, req,
2579                                 &state->setup, state->setup_count,
2580                                 &state->param, state->total_param,
2581                                 &state->data, state->total_data,
2582                                 state->max_data_return);
2583                         END_PROFILE(NT_transact_get_user_quota);
2584                         break;
2585                 }
2586
2587                 case NT_TRANSACT_SET_USER_QUOTA:
2588                 {
2589                         START_PROFILE(NT_transact_set_user_quota);
2590                         call_nt_transact_set_user_quota(
2591                                 conn, req,
2592                                 &state->setup, state->setup_count,
2593                                 &state->param, state->total_param,
2594                                 &state->data, state->total_data,
2595                                 state->max_data_return);
2596                         END_PROFILE(NT_transact_set_user_quota);
2597                         break;
2598                 }
2599 #endif /* HAVE_SYS_QUOTAS */
2600
2601                 default:
2602                         /* Error in request */
2603                         DEBUG(0,("handle_nttrans: Unknown request %d in "
2604                                  "nttrans call\n", state->call));
2605                         reply_doserror(req, ERRSRV, ERRerror);
2606                         return;
2607         }
2608         return;
2609 }
2610
2611 /****************************************************************************
2612  Reply to a SMBNTtrans.
2613 ****************************************************************************/
2614
2615 void reply_nttrans(connection_struct *conn, struct smb_request *req)
2616 {
2617         uint32 pscnt;
2618         uint32 psoff;
2619         uint32 dscnt;
2620         uint32 dsoff;
2621         uint16 function_code;
2622         NTSTATUS result;
2623         struct trans_state *state;
2624         int size;
2625
2626         START_PROFILE(SMBnttrans);
2627
2628         if (req->wct < 19) {
2629                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2630                 END_PROFILE(SMBnttrans);
2631                 return;
2632         }
2633
2634         size = smb_len(req->inbuf) + 4;
2635         pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
2636         psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
2637         dscnt = IVAL(req->inbuf,smb_nt_DataCount);
2638         dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
2639         function_code = SVAL(req->inbuf, smb_nt_Function);
2640
2641         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2642                 reply_doserror(req, ERRSRV, ERRaccess);
2643                 END_PROFILE(SMBnttrans);
2644                 return;
2645         }
2646
2647         result = allow_new_trans(conn->pending_trans, req->mid);
2648         if (!NT_STATUS_IS_OK(result)) {
2649                 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2650                 reply_nterror(req, result);
2651                 END_PROFILE(SMBnttrans);
2652                 return;
2653         }
2654
2655         if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
2656                 reply_doserror(req, ERRSRV, ERRaccess);
2657                 END_PROFILE(SMBnttrans);
2658                 return;
2659         }
2660
2661         state->cmd = SMBnttrans;
2662
2663         state->mid = req->mid;
2664         state->vuid = req->vuid;
2665         state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
2666         state->data = NULL;
2667         state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
2668         state->param = NULL;
2669         state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
2670         state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
2671
2672         /* setup count is in *words* */
2673         state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
2674         state->setup = NULL;
2675         state->call = function_code;
2676
2677         /*
2678          * All nttrans messages we handle have smb_wct == 19 +
2679          * state->setup_count.  Ensure this is so as a sanity check.
2680          */
2681
2682         if(req->wct != 19 + (state->setup_count/2)) {
2683                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2684                          req->wct, 19 + (state->setup_count/2)));
2685                 goto bad_param;
2686         }
2687
2688         /* Don't allow more than 128mb for each value. */
2689         if ((state->total_data > (1024*1024*128)) ||
2690             (state->total_param > (1024*1024*128))) {
2691                 reply_doserror(req, ERRDOS, ERRnomem);
2692                 END_PROFILE(SMBnttrans);
2693                 return;
2694         }
2695
2696         if ((dscnt > state->total_data) || (pscnt > state->total_param))
2697                 goto bad_param;
2698
2699         if (state->total_data)  {
2700                 /* Can't use talloc here, the core routines do realloc on the
2701                  * params and data. */
2702                 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2703                         DEBUG(0,("reply_nttrans: data malloc fail for %u "
2704                                  "bytes !\n", (unsigned int)state->total_data));
2705                         TALLOC_FREE(state);
2706                         reply_doserror(req, ERRDOS, ERRnomem);
2707                         END_PROFILE(SMBnttrans);
2708                         return;
2709                 }
2710                 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
2711                         goto bad_param;
2712                 if ((smb_base(req->inbuf)+dsoff+dscnt
2713                      > (char *)req->inbuf + size) ||
2714                     (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
2715                         goto bad_param;
2716
2717                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2718         }
2719
2720         if (state->total_param) {
2721                 /* Can't use talloc here, the core routines do realloc on the
2722                  * params and data. */
2723                 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2724                         DEBUG(0,("reply_nttrans: param malloc fail for %u "
2725                                  "bytes !\n", (unsigned int)state->total_param));
2726                         SAFE_FREE(state->data);
2727                         TALLOC_FREE(state);
2728                         reply_doserror(req, ERRDOS, ERRnomem);
2729                         END_PROFILE(SMBnttrans);
2730                         return;
2731                 }
2732                 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
2733                         goto bad_param;
2734                 if ((smb_base(req->inbuf)+psoff+pscnt
2735                      > (char *)req->inbuf + size) ||
2736                     (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
2737                         goto bad_param;
2738
2739                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2740         }
2741
2742         state->received_data  = dscnt;
2743         state->received_param = pscnt;
2744
2745         if(state->setup_count > 0) {
2746                 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2747                           state->setup_count));
2748                 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2749                 if (state->setup == NULL) {
2750                         DEBUG(0,("reply_nttrans : Out of memory\n"));
2751                         SAFE_FREE(state->data);
2752                         SAFE_FREE(state->param);
2753                         TALLOC_FREE(state);
2754                         reply_doserror(req, ERRDOS, ERRnomem);
2755                         END_PROFILE(SMBnttrans);
2756                         return;
2757                 }
2758
2759                 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2760                     (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2761                         goto bad_param;
2762                 }
2763                 if (smb_nt_SetupStart + state->setup_count > size) {
2764                         goto bad_param;
2765                 }
2766
2767                 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
2768                         state->setup_count);
2769                 dump_data(10, (uint8 *)state->setup, state->setup_count);
2770         }
2771
2772         if ((state->received_data == state->total_data) &&
2773             (state->received_param == state->total_param)) {
2774                 handle_nttrans(conn, state, req);
2775                 SAFE_FREE(state->param);
2776                 SAFE_FREE(state->data);
2777                 TALLOC_FREE(state);
2778                 END_PROFILE(SMBnttrans);
2779                 return;
2780         }
2781
2782         DLIST_ADD(conn->pending_trans, state);
2783
2784         /* We need to send an interim response then receive the rest
2785            of the parameter/data bytes */
2786         reply_outbuf(req, 0, 0);
2787         show_msg((char *)req->outbuf);
2788         END_PROFILE(SMBnttrans);
2789         return;
2790
2791   bad_param:
2792
2793         DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2794         SAFE_FREE(state->data);
2795         SAFE_FREE(state->param);
2796         TALLOC_FREE(state);
2797         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2798         END_PROFILE(SMBnttrans);
2799         return;
2800 }
2801
2802 /****************************************************************************
2803  Reply to a SMBnttranss
2804  ****************************************************************************/
2805
2806 void reply_nttranss(connection_struct *conn, struct smb_request *req)
2807 {
2808         unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
2809         struct trans_state *state;
2810
2811         int size;
2812
2813         START_PROFILE(SMBnttranss);
2814
2815         show_msg((char *)req->inbuf);
2816
2817         if (req->wct < 18) {
2818                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2819                 END_PROFILE(SMBnttranss);
2820                 return;
2821         }
2822
2823         for (state = conn->pending_trans; state != NULL;
2824              state = state->next) {
2825                 if (state->mid == req->mid) {
2826                         break;
2827                 }
2828         }
2829
2830         if ((state == NULL) || (state->cmd != SMBnttrans)) {
2831                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2832                 END_PROFILE(SMBnttranss);
2833                 return;
2834         }
2835
2836         /* Revise state->total_param and state->total_data in case they have
2837            changed downwards */
2838         if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
2839             < state->total_param) {
2840                 state->total_param = IVAL(req->inbuf,
2841                                           smb_nts_TotalParameterCount);
2842         }
2843         if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
2844                 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
2845         }
2846
2847         size = smb_len(req->inbuf) + 4;
2848
2849         pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
2850         poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
2851         pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
2852
2853         dcnt = IVAL(req->inbuf, smb_nts_DataCount);
2854         ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
2855         doff = IVAL(req->inbuf, smb_nts_DataOffset);
2856
2857         state->received_param += pcnt;
2858         state->received_data += dcnt;
2859
2860         if ((state->received_data > state->total_data) ||
2861             (state->received_param > state->total_param))
2862                 goto bad_param;
2863
2864         if (pcnt) {
2865                 if (pdisp+pcnt > state->total_param)
2866                         goto bad_param;
2867                 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
2868                         goto bad_param;
2869                 if (pdisp > state->total_param)
2870                         goto bad_param;
2871                 if ((smb_base(req->inbuf) + poff + pcnt
2872                      > (char *)req->inbuf + size) ||
2873                     (smb_base(req->inbuf) + poff + pcnt
2874                      < smb_base(req->inbuf)))
2875                         goto bad_param;
2876                 if (state->param + pdisp < state->param)
2877                         goto bad_param;
2878
2879                 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2880                        pcnt);
2881         }
2882
2883         if (dcnt) {
2884                 if (ddisp+dcnt > state->total_data)
2885                         goto bad_param;
2886                 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
2887                         goto bad_param;
2888                 if (ddisp > state->total_data)
2889                         goto bad_param;
2890                 if ((smb_base(req->inbuf) + doff + dcnt
2891                      > (char *)req->inbuf + size) ||
2892                     (smb_base(req->inbuf) + doff + dcnt
2893                      < smb_base(req->inbuf)))
2894                         goto bad_param;
2895                 if (state->data + ddisp < state->data)
2896                         goto bad_param;
2897
2898                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2899                        dcnt);
2900         }
2901
2902         if ((state->received_param < state->total_param) ||
2903             (state->received_data < state->total_data)) {
2904                 END_PROFILE(SMBnttranss);
2905                 return;
2906         }
2907
2908         /*
2909          * construct_reply_common will copy smb_com from inbuf to
2910          * outbuf. SMBnttranss is wrong here.
2911          */
2912         SCVAL(req->inbuf,smb_com,SMBnttrans);
2913
2914         handle_nttrans(conn, state, req);
2915
2916         DLIST_REMOVE(conn->pending_trans, state);
2917         SAFE_FREE(state->data);
2918         SAFE_FREE(state->param);
2919         TALLOC_FREE(state);
2920         END_PROFILE(SMBnttranss);
2921         return;
2922
2923   bad_param:
2924
2925         DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2926         DLIST_REMOVE(conn->pending_trans, state);
2927         SAFE_FREE(state->data);
2928         SAFE_FREE(state->param);
2929         TALLOC_FREE(state);
2930         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2931         END_PROFILE(SMBnttranss);
2932         return;
2933 }