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