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