Pretty print out SD's on get/set.
[kai/samba-autobuild/.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==0) {
725                 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
726         }
727         if (psd->group_sid==0) {
728                 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
729         }
730         if (psd->sacl==0) {
731                 security_info_sent &= ~SACL_SECURITY_INFORMATION;
732         }
733         if (security_info_sent & DACL_SECURITY_INFORMATION) {
734                 psd->type |= SEC_DESC_DACL_PRESENT;
735         }
736         if (psd->dacl==0) {
737                 security_info_sent &= ~DACL_SECURITY_INFORMATION;
738         }
739
740         /* Convert all the generic bits. */
741         security_acl_map_generic(psd->dacl, &file_generic_mapping);
742         security_acl_map_generic(psd->sacl, &file_generic_mapping);
743
744         if (DEBUGLEVEL >= 10) {
745                 DEBUG(10,("set_sd for file %s\n", fsp->fsp_name ));
746                 NDR_PRINT_DEBUG(security_descriptor, psd);
747         }
748
749         status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
750
751         TALLOC_FREE(psd);
752
753         return status;
754 }
755
756 /****************************************************************************
757  Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
758 ****************************************************************************/
759
760 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
761 {
762         struct ea_list *ea_list_head = NULL;
763         size_t offset = 0;
764
765         if (data_size < 4) {
766                 return NULL;
767         }
768
769         while (offset + 4 <= data_size) {
770                 size_t next_offset = IVAL(pdata,offset);
771                 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
772
773                 if (!eal) {
774                         return NULL;
775                 }
776
777                 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
778                 if (next_offset == 0) {
779                         break;
780                 }
781                 offset += next_offset;
782         }
783
784         return ea_list_head;
785 }
786
787 /****************************************************************************
788  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
789 ****************************************************************************/
790
791 static void call_nt_transact_create(connection_struct *conn,
792                                     struct smb_request *req,
793                                     uint16 **ppsetup, uint32 setup_count,
794                                     char **ppparams, uint32 parameter_count,
795                                     char **ppdata, uint32 data_count,
796                                     uint32 max_data_count)
797 {
798         char *fname = NULL;
799         char *params = *ppparams;
800         char *data = *ppdata;
801         /* Breakout the oplock request bits so we can set the reply bits separately. */
802         uint32 fattr=0;
803         SMB_OFF_T file_len = 0;
804         SMB_STRUCT_STAT sbuf;
805         int info = 0;
806         files_struct *fsp = NULL;
807         char *p = NULL;
808         uint32 flags;
809         uint32 access_mask;
810         uint32 file_attributes;
811         uint32 share_access;
812         uint32 create_disposition;
813         uint32 create_options;
814         uint32 sd_len;
815         struct security_descriptor *sd = NULL;
816         uint32 ea_len;
817         uint16 root_dir_fid;
818         struct timespec c_timespec;
819         struct timespec a_timespec;
820         struct timespec m_timespec;
821         struct ea_list *ea_list = NULL;
822         NTSTATUS status;
823         size_t param_len;
824         uint64_t allocation_size;
825         int oplock_request;
826         uint8_t oplock_granted;
827         TALLOC_CTX *ctx = talloc_tos();
828
829         DEBUG(5,("call_nt_transact_create\n"));
830
831         /*
832          * If it's an IPC, use the pipe handler.
833          */
834
835         if (IS_IPC(conn)) {
836                 if (lp_nt_pipe_support()) {
837                         do_nt_transact_create_pipe(
838                                 conn, req,
839                                 ppsetup, setup_count,
840                                 ppparams, parameter_count,
841                                 ppdata, data_count);
842                         return;
843                 }
844                 reply_doserror(req, ERRDOS, ERRnoaccess);
845                 return;
846         }
847
848         /*
849          * Ensure minimum number of parameters sent.
850          */
851
852         if(parameter_count < 54) {
853                 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
854                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
855                 return;
856         }
857
858         flags = IVAL(params,0);
859         access_mask = IVAL(params,8);
860         file_attributes = IVAL(params,20);
861         share_access = IVAL(params,24);
862         create_disposition = IVAL(params,28);
863         create_options = IVAL(params,32);
864         sd_len = IVAL(params,36);
865         ea_len = IVAL(params,40);
866         root_dir_fid = (uint16)IVAL(params,4);
867         allocation_size = (uint64_t)IVAL(params,12);
868 #ifdef LARGE_SMB_OFF_T
869         allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
870 #endif
871
872         /*
873          * we need to remove ignored bits when they come directly from the client
874          * because we reuse some of them for internal stuff
875          */
876         create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
877
878         /* Ensure the data_len is correct for the sd and ea values given. */
879         if ((ea_len + sd_len > data_count)
880             || (ea_len > data_count) || (sd_len > data_count)
881             || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
882                 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
883                            "%u, data_count = %u\n", (unsigned int)ea_len,
884                            (unsigned int)sd_len, (unsigned int)data_count));
885                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
886                 return;
887         }
888
889         if (sd_len) {
890                 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
891                            sd_len));
892
893                 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
894                                              &sd);
895                 if (!NT_STATUS_IS_OK(status)) {
896                         DEBUG(10, ("call_nt_transact_create: "
897                                    "unmarshall_sec_desc failed: %s\n",
898                                    nt_errstr(status)));
899                         reply_nterror(req, status);
900                         return;
901                 }
902         }
903
904         if (ea_len) {
905                 if (!lp_ea_support(SNUM(conn))) {
906                         DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
907                                    "EA's not supported.\n",
908                                    (unsigned int)ea_len));
909                         reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
910                         return;
911                 }
912
913                 if (ea_len < 10) {
914                         DEBUG(10,("call_nt_transact_create - ea_len = %u - "
915                                   "too small (should be more than 10)\n",
916                                   (unsigned int)ea_len ));
917                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
918                         return;
919                 }
920
921                 /* We have already checked that ea_len <= data_count here. */
922                 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
923                                                ea_len);
924                 if (ea_list == NULL) {
925                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
926                         return;
927                 }
928         }
929
930         srvstr_get_path(ctx, params, req->flags2, &fname,
931                         params+53, parameter_count-53,
932                         STR_TERMINATE, &status);
933         if (!NT_STATUS_IS_OK(status)) {
934                 reply_nterror(req, status);
935                 return;
936         }
937
938         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
939         if (oplock_request) {
940                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
941                         ? BATCH_OPLOCK : 0;
942         }
943
944         status = create_file(conn, req, root_dir_fid, fname,
945                              access_mask, share_access, create_disposition,
946                              create_options, file_attributes, oplock_request,
947                              allocation_size, sd, ea_list, &fsp, &info, &sbuf);
948
949         if(!NT_STATUS_IS_OK(status)) {
950                 if (open_was_deferred(req->mid)) {
951                         /* We have re-scheduled this call, no error. */
952                         return;
953                 }
954                 reply_openerror(req, status);
955                 return;
956         }
957
958         /*
959          * If the caller set the extended oplock request bit
960          * and we granted one (by whatever means) - set the
961          * correct bit for extended oplock reply.
962          */
963
964         if (oplock_request &&
965             (lp_fake_oplocks(SNUM(conn))
966              || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
967
968                 /*
969                  * Exclusive oplock granted
970                  */
971
972                 if (flags & REQUEST_BATCH_OPLOCK) {
973                         oplock_granted = BATCH_OPLOCK_RETURN;
974                 } else {
975                         oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
976                 }
977         } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
978                 oplock_granted = LEVEL_II_OPLOCK_RETURN;
979         } else {
980                 oplock_granted = NO_OPLOCK_RETURN;
981         }
982
983         file_len = sbuf.st_size;
984         fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
985         if (fattr == 0) {
986                 fattr = FILE_ATTRIBUTE_NORMAL;
987         }
988
989         /* Realloc the size of parameters and data we will return */
990         if (flags & EXTENDED_RESPONSE_REQUIRED) {
991                 /* Extended response is 32 more byyes. */
992                 param_len = 101;
993         } else {
994                 param_len = 69;
995         }
996         params = nttrans_realloc(ppparams, param_len);
997         if(params == NULL) {
998                 reply_doserror(req, ERRDOS, ERRnomem);
999                 return;
1000         }
1001
1002         p = params;
1003         SCVAL(p, 0, oplock_granted);
1004
1005         p += 2;
1006         SSVAL(p,0,fsp->fnum);
1007         p += 2;
1008         if ((create_disposition == FILE_SUPERSEDE)
1009             && (info == FILE_WAS_OVERWRITTEN)) {
1010                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1011         } else {
1012                 SIVAL(p,0,info);
1013         }
1014         p += 8;
1015
1016         /* Create time. */
1017         c_timespec = get_create_timespec(
1018                 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1019         a_timespec = get_atimespec(&sbuf);
1020         m_timespec = get_mtimespec(&sbuf);
1021
1022         if (lp_dos_filetime_resolution(SNUM(conn))) {
1023                 dos_filetime_timespec(&c_timespec);
1024                 dos_filetime_timespec(&a_timespec);
1025                 dos_filetime_timespec(&m_timespec);
1026         }
1027
1028         put_long_date_timespec(p, c_timespec); /* create time. */
1029         p += 8;
1030         put_long_date_timespec(p, a_timespec); /* access time */
1031         p += 8;
1032         put_long_date_timespec(p, m_timespec); /* write time */
1033         p += 8;
1034         put_long_date_timespec(p, m_timespec); /* change time */
1035         p += 8;
1036         SIVAL(p,0,fattr); /* File Attributes. */
1037         p += 4;
1038         SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1039         p += 8;
1040         SOFF_T(p,0,file_len);
1041         p += 8;
1042         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1043                 SSVAL(p,2,0x7);
1044         }
1045         p += 4;
1046         SCVAL(p,0,fsp->is_directory ? 1 : 0);
1047
1048         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1049                 uint32 perms = 0;
1050                 p += 25;
1051                 if (fsp->is_directory
1052                     || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
1053                         perms = FILE_GENERIC_ALL;
1054                 } else {
1055                         perms = FILE_GENERIC_READ|FILE_EXECUTE;
1056                 }
1057                 SIVAL(p,0,perms);
1058         }
1059
1060         DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1061
1062         /* Send the required number of replies */
1063         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1064
1065         return;
1066 }
1067
1068 /****************************************************************************
1069  Reply to a NT CANCEL request.
1070  conn POINTER CAN BE NULL HERE !
1071 ****************************************************************************/
1072
1073 void reply_ntcancel(struct smb_request *req)
1074 {
1075         /*
1076          * Go through and cancel any pending change notifies.
1077          */
1078
1079         START_PROFILE(SMBntcancel);
1080         remove_pending_change_notify_requests_by_mid(req->mid);
1081         remove_pending_lock_requests_by_mid(req->mid);
1082         srv_cancel_sign_response(req->mid);
1083
1084         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1085
1086         END_PROFILE(SMBntcancel);
1087         return;
1088 }
1089
1090 /****************************************************************************
1091  Copy a file.
1092 ****************************************************************************/
1093
1094 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1095                                 connection_struct *conn,
1096                                 struct smb_request *req,
1097                                 const char *oldname_in,
1098                                 const char *newname_in,
1099                                 uint32 attrs)
1100 {
1101         SMB_STRUCT_STAT sbuf1, sbuf2;
1102         char *oldname = NULL;
1103         char *newname = NULL;
1104         char *last_component_oldname = NULL;
1105         char *last_component_newname = NULL;
1106         files_struct *fsp1,*fsp2;
1107         uint32 fattr;
1108         int info;
1109         SMB_OFF_T ret=-1;
1110         NTSTATUS status = NT_STATUS_OK;
1111
1112         ZERO_STRUCT(sbuf1);
1113         ZERO_STRUCT(sbuf2);
1114
1115         if (!CAN_WRITE(conn)) {
1116                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1117         }
1118
1119         status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1120                         &last_component_oldname, &sbuf1);
1121         if (!NT_STATUS_IS_OK(status)) {
1122                 return status;
1123         }
1124
1125         status = check_name(conn, oldname);
1126         if (!NT_STATUS_IS_OK(status)) {
1127                 return status;
1128         }
1129
1130         /* Source must already exist. */
1131         if (!VALID_STAT(sbuf1)) {
1132                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1133         }
1134         /* Ensure attributes match. */
1135         fattr = dos_mode(conn,oldname,&sbuf1);
1136         if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1137                 return NT_STATUS_NO_SUCH_FILE;
1138         }
1139
1140         status = unix_convert(ctx, conn, newname_in, False, &newname,
1141                         &last_component_newname, &sbuf2);
1142         if (!NT_STATUS_IS_OK(status)) {
1143                 return status;
1144         }
1145
1146         status = check_name(conn, newname);
1147         if (!NT_STATUS_IS_OK(status)) {
1148                 return status;
1149         }
1150
1151         /* Disallow if newname already exists. */
1152         if (VALID_STAT(sbuf2)) {
1153                 return NT_STATUS_OBJECT_NAME_COLLISION;
1154         }
1155
1156         /* No links from a directory. */
1157         if (S_ISDIR(sbuf1.st_mode)) {
1158                 return NT_STATUS_FILE_IS_A_DIRECTORY;
1159         }
1160
1161         /* Ensure this is within the share. */
1162         status = check_reduced_name(conn, oldname);
1163         if (!NT_STATUS_IS_OK(status)) {
1164                 return status;
1165         }
1166
1167         DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1168                                 oldname, newname));
1169
1170         status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1171                         FILE_READ_DATA, /* Read-only. */
1172                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1173                         FILE_OPEN,
1174                         0, /* No create options. */
1175                         FILE_ATTRIBUTE_NORMAL,
1176                         NO_OPLOCK,
1177                         &info, &fsp1);
1178
1179         if (!NT_STATUS_IS_OK(status)) {
1180                 return status;
1181         }
1182
1183         status = open_file_ntcreate(conn, req, newname, &sbuf2,
1184                         FILE_WRITE_DATA, /* Read-only. */
1185                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1186                         FILE_CREATE,
1187                         0, /* No create options. */
1188                         fattr,
1189                         NO_OPLOCK,
1190                         &info, &fsp2);
1191
1192         if (!NT_STATUS_IS_OK(status)) {
1193                 close_file(NULL, fsp1, ERROR_CLOSE);
1194                 return status;
1195         }
1196
1197         if (sbuf1.st_size) {
1198                 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1199         }
1200
1201         /*
1202          * As we are opening fsp1 read-only we only expect
1203          * an error on close on fsp2 if we are out of space.
1204          * Thus we don't look at the error return from the
1205          * close of fsp1.
1206          */
1207         close_file(NULL, fsp1, NORMAL_CLOSE);
1208
1209         /* Ensure the modtime is set correctly on the destination file. */
1210         set_close_write_time(fsp2, get_mtimespec(&sbuf1));
1211
1212         status = close_file(NULL, fsp2, NORMAL_CLOSE);
1213
1214         /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1215            creates the file. This isn't the correct thing to do in the copy
1216            case. JRA */
1217         file_set_dosmode(conn, newname, fattr, &sbuf2,
1218                          parent_dirname(newname),false);
1219
1220         if (ret < (SMB_OFF_T)sbuf1.st_size) {
1221                 return NT_STATUS_DISK_FULL;
1222         }
1223
1224         if (!NT_STATUS_IS_OK(status)) {
1225                 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1226                         nt_errstr(status), oldname, newname));
1227         }
1228         return status;
1229 }
1230
1231 /****************************************************************************
1232  Reply to a NT rename request.
1233 ****************************************************************************/
1234
1235 void reply_ntrename(struct smb_request *req)
1236 {
1237         connection_struct *conn = req->conn;
1238         char *oldname = NULL;
1239         char *newname = NULL;
1240         const char *p;
1241         NTSTATUS status;
1242         bool src_has_wcard = False;
1243         bool dest_has_wcard = False;
1244         uint32 attrs;
1245         uint16 rename_type;
1246         TALLOC_CTX *ctx = talloc_tos();
1247
1248         START_PROFILE(SMBntrename);
1249
1250         if (req->wct < 4) {
1251                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1252                 END_PROFILE(SMBntrename);
1253                 return;
1254         }
1255
1256         attrs = SVAL(req->vwv+0, 0);
1257         rename_type = SVAL(req->vwv+1, 0);
1258
1259         p = (const char *)req->buf + 1;
1260         p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1261                                        &status, &src_has_wcard);
1262         if (!NT_STATUS_IS_OK(status)) {
1263                 reply_nterror(req, status);
1264                 END_PROFILE(SMBntrename);
1265                 return;
1266         }
1267
1268         if( is_ntfs_stream_name(oldname)) {
1269                 /* Can't rename a stream. */
1270                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1271                 END_PROFILE(SMBntrename);
1272                 return;
1273         }
1274
1275         if (ms_has_wild(oldname)) {
1276                 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1277                 END_PROFILE(SMBntrename);
1278                 return;
1279         }
1280
1281         p++;
1282         p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1283                                        &status, &dest_has_wcard);
1284         if (!NT_STATUS_IS_OK(status)) {
1285                 reply_nterror(req, status);
1286                 END_PROFILE(SMBntrename);
1287                 return;
1288         }
1289
1290         status = resolve_dfspath(ctx, conn,
1291                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
1292                                 oldname,
1293                                 &oldname);
1294         if (!NT_STATUS_IS_OK(status)) {
1295                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1296                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1297                                         ERRSRV, ERRbadpath);
1298                         END_PROFILE(SMBntrename);
1299                         return;
1300                 }
1301                 reply_nterror(req, status);
1302                 END_PROFILE(SMBntrename);
1303                 return;
1304         }
1305
1306         status = resolve_dfspath(ctx, conn,
1307                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
1308                                 newname,
1309                                 &newname);
1310         if (!NT_STATUS_IS_OK(status)) {
1311                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1312                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1313                                         ERRSRV, ERRbadpath);
1314                         END_PROFILE(SMBntrename);
1315                         return;
1316                 }
1317                 reply_nterror(req, status);
1318                 END_PROFILE(SMBntrename);
1319                 return;
1320         }
1321
1322         DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1323
1324         switch(rename_type) {
1325                 case RENAME_FLAG_RENAME:
1326                         status = rename_internals(ctx, conn, req, oldname,
1327                                         newname, attrs, False, src_has_wcard,
1328                                         dest_has_wcard, DELETE_ACCESS);
1329                         break;
1330                 case RENAME_FLAG_HARD_LINK:
1331                         if (src_has_wcard || dest_has_wcard) {
1332                                 /* No wildcards. */
1333                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1334                         } else {
1335                                 status = hardlink_internals(ctx,
1336                                                 conn,
1337                                                 oldname,
1338                                                 newname);
1339                         }
1340                         break;
1341                 case RENAME_FLAG_COPY:
1342                         if (src_has_wcard || dest_has_wcard) {
1343                                 /* No wildcards. */
1344                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1345                         } else {
1346                                 status = copy_internals(ctx, conn, req, oldname,
1347                                                         newname, attrs);
1348                         }
1349                         break;
1350                 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1351                         status = NT_STATUS_INVALID_PARAMETER;
1352                         break;
1353                 default:
1354                         status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1355                         break;
1356         }
1357
1358         if (!NT_STATUS_IS_OK(status)) {
1359                 if (open_was_deferred(req->mid)) {
1360                         /* We have re-scheduled this call. */
1361                         END_PROFILE(SMBntrename);
1362                         return;
1363                 }
1364
1365                 reply_nterror(req, status);
1366                 END_PROFILE(SMBntrename);
1367                 return;
1368         }
1369
1370         reply_outbuf(req, 0, 0);
1371
1372         END_PROFILE(SMBntrename);
1373         return;
1374 }
1375
1376 /****************************************************************************
1377  Reply to a notify change - queue the request and
1378  don't allow a directory to be opened.
1379 ****************************************************************************/
1380
1381 static void call_nt_transact_notify_change(connection_struct *conn,
1382                                            struct smb_request *req,
1383                                            uint16 **ppsetup,
1384                                            uint32 setup_count,
1385                                            char **ppparams,
1386                                            uint32 parameter_count,
1387                                            char **ppdata, uint32 data_count,
1388                                            uint32 max_data_count,
1389                                            uint32 max_param_count)
1390 {
1391         uint16 *setup = *ppsetup;
1392         files_struct *fsp;
1393         uint32 filter;
1394         NTSTATUS status;
1395         bool recursive;
1396
1397         if(setup_count < 6) {
1398                 reply_doserror(req, ERRDOS, ERRbadfunc);
1399                 return;
1400         }
1401
1402         fsp = file_fsp(req, SVAL(setup,4));
1403         filter = IVAL(setup, 0);
1404         recursive = (SVAL(setup, 6) != 0) ? True : False;
1405
1406         DEBUG(3,("call_nt_transact_notify_change\n"));
1407
1408         if(!fsp) {
1409                 reply_doserror(req, ERRDOS, ERRbadfid);
1410                 return;
1411         }
1412
1413         {
1414                 char *filter_string;
1415
1416                 if (!(filter_string = notify_filter_string(NULL, filter))) {
1417                         reply_nterror(req,NT_STATUS_NO_MEMORY);
1418                         return;
1419                 }
1420
1421                 DEBUG(3,("call_nt_transact_notify_change: notify change "
1422                          "called on %s, filter = %s, recursive = %d\n",
1423                          fsp->fsp_name, filter_string, recursive));
1424
1425                 TALLOC_FREE(filter_string);
1426         }
1427
1428         if((!fsp->is_directory) || (conn != fsp->conn)) {
1429                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1430                 return;
1431         }
1432
1433         if (fsp->notify == NULL) {
1434
1435                 status = change_notify_create(fsp, filter, recursive);
1436
1437                 if (!NT_STATUS_IS_OK(status)) {
1438                         DEBUG(10, ("change_notify_create returned %s\n",
1439                                    nt_errstr(status)));
1440                         reply_nterror(req, status);
1441                         return;
1442                 }
1443         }
1444
1445         if (fsp->notify->num_changes != 0) {
1446
1447                 /*
1448                  * We've got changes pending, respond immediately
1449                  */
1450
1451                 /*
1452                  * TODO: write a torture test to check the filtering behaviour
1453                  * here.
1454                  */
1455
1456                 change_notify_reply(fsp->conn, req, max_param_count,
1457                                     fsp->notify);
1458
1459                 /*
1460                  * change_notify_reply() above has independently sent its
1461                  * results
1462                  */
1463                 return;
1464         }
1465
1466         /*
1467          * No changes pending, queue the request
1468          */
1469
1470         status = change_notify_add_request(req,
1471                         max_param_count,
1472                         filter,
1473                         recursive, fsp);
1474         if (!NT_STATUS_IS_OK(status)) {
1475                 reply_nterror(req, status);
1476         }
1477         return;
1478 }
1479
1480 /****************************************************************************
1481  Reply to an NT transact rename command.
1482 ****************************************************************************/
1483
1484 static void call_nt_transact_rename(connection_struct *conn,
1485                                     struct smb_request *req,
1486                                     uint16 **ppsetup, uint32 setup_count,
1487                                     char **ppparams, uint32 parameter_count,
1488                                     char **ppdata, uint32 data_count,
1489                                     uint32 max_data_count)
1490 {
1491         char *params = *ppparams;
1492         char *new_name = NULL;
1493         files_struct *fsp = NULL;
1494         bool dest_has_wcard = False;
1495         NTSTATUS status;
1496         TALLOC_CTX *ctx = talloc_tos();
1497
1498         if(parameter_count < 5) {
1499                 reply_doserror(req, ERRDOS, ERRbadfunc);
1500                 return;
1501         }
1502
1503         fsp = file_fsp(req, SVAL(params, 0));
1504         if (!check_fsp(conn, req, fsp)) {
1505                 return;
1506         }
1507         srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1508                               parameter_count - 4,
1509                               STR_TERMINATE, &status, &dest_has_wcard);
1510         if (!NT_STATUS_IS_OK(status)) {
1511                 reply_nterror(req, status);
1512                 return;
1513         }
1514
1515         /*
1516          * W2K3 ignores this request as the RAW-RENAME test
1517          * demonstrates, so we do.
1518          */
1519         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1520
1521         DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1522                  fsp->fsp_name, new_name));
1523
1524         return;
1525 }
1526
1527 /******************************************************************************
1528  Fake up a completely empty SD.
1529 *******************************************************************************/
1530
1531 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1532 {
1533         size_t sd_size;
1534
1535         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1536         if(!*ppsd) {
1537                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1538                 return NT_STATUS_NO_MEMORY;
1539         }
1540
1541         return NT_STATUS_OK;
1542 }
1543
1544 /****************************************************************************
1545  Reply to query a security descriptor.
1546 ****************************************************************************/
1547
1548 static void call_nt_transact_query_security_desc(connection_struct *conn,
1549                                                  struct smb_request *req,
1550                                                  uint16 **ppsetup,
1551                                                  uint32 setup_count,
1552                                                  char **ppparams,
1553                                                  uint32 parameter_count,
1554                                                  char **ppdata,
1555                                                  uint32 data_count,
1556                                                  uint32 max_data_count)
1557 {
1558         char *params = *ppparams;
1559         char *data = *ppdata;
1560         SEC_DESC *psd = NULL;
1561         size_t sd_size;
1562         uint32 security_info_wanted;
1563         files_struct *fsp = NULL;
1564         NTSTATUS status;
1565         DATA_BLOB blob;
1566
1567         if(parameter_count < 8) {
1568                 reply_doserror(req, ERRDOS, ERRbadfunc);
1569                 return;
1570         }
1571
1572         fsp = file_fsp(req, SVAL(params,0));
1573         if(!fsp) {
1574                 reply_doserror(req, ERRDOS, ERRbadfid);
1575                 return;
1576         }
1577
1578         security_info_wanted = IVAL(params,4);
1579
1580         DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1581                         (unsigned int)security_info_wanted ));
1582
1583         params = nttrans_realloc(ppparams, 4);
1584         if(params == NULL) {
1585                 reply_doserror(req, ERRDOS, ERRnomem);
1586                 return;
1587         }
1588
1589         /*
1590          * Get the permissions to return.
1591          */
1592
1593         if (!lp_nt_acl_support(SNUM(conn))) {
1594                 status = get_null_nt_acl(talloc_tos(), &psd);
1595         } else {
1596                 status = SMB_VFS_FGET_NT_ACL(
1597                         fsp, security_info_wanted, &psd);
1598         }
1599
1600         if (!NT_STATUS_IS_OK(status)) {
1601                 reply_nterror(req, status);
1602                 return;
1603         }
1604
1605         sd_size = ndr_size_security_descriptor(psd, 0);
1606
1607         DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1608
1609         if (DEBUGLEVEL >= 10) {
1610                 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n", fsp->fsp_name )
1611                 NDR_PRINT_DEBUG(security_descriptor, psd);
1612         }
1613
1614         SIVAL(params,0,(uint32)sd_size);
1615
1616         if (max_data_count < sd_size) {
1617                 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1618                                 params, 4, *ppdata, 0);
1619                 return;
1620         }
1621
1622         /*
1623          * Allocate the data we will point this at.
1624          */
1625
1626         data = nttrans_realloc(ppdata, sd_size);
1627         if(data == NULL) {
1628                 reply_doserror(req, ERRDOS, ERRnomem);
1629                 return;
1630         }
1631
1632         status = marshall_sec_desc(talloc_tos(), psd,
1633                                    &blob.data, &blob.length);
1634
1635         if (!NT_STATUS_IS_OK(status)) {
1636                 reply_nterror(req, status);
1637                 return;
1638         }
1639
1640         SMB_ASSERT(sd_size == blob.length);
1641         memcpy(data, blob.data, sd_size);
1642
1643         send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1644
1645         return;
1646 }
1647
1648 /****************************************************************************
1649  Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1650 ****************************************************************************/
1651
1652 static void call_nt_transact_set_security_desc(connection_struct *conn,
1653                                                struct smb_request *req,
1654                                                uint16 **ppsetup,
1655                                                uint32 setup_count,
1656                                                char **ppparams,
1657                                                uint32 parameter_count,
1658                                                char **ppdata,
1659                                                uint32 data_count,
1660                                                uint32 max_data_count)
1661 {
1662         char *params= *ppparams;
1663         char *data = *ppdata;
1664         files_struct *fsp = NULL;
1665         uint32 security_info_sent = 0;
1666         NTSTATUS status;
1667
1668         if(parameter_count < 8) {
1669                 reply_doserror(req, ERRDOS, ERRbadfunc);
1670                 return;
1671         }
1672
1673         if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1674                 reply_doserror(req, ERRDOS, ERRbadfid);
1675                 return;
1676         }
1677
1678         if(!lp_nt_acl_support(SNUM(conn))) {
1679                 goto done;
1680         }
1681
1682         security_info_sent = IVAL(params,4);
1683
1684         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1685                 (unsigned int)security_info_sent ));
1686
1687         if (data_count == 0) {
1688                 reply_doserror(req, ERRDOS, ERRnoaccess);
1689                 return;
1690         }
1691
1692         status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1693
1694         if (!NT_STATUS_IS_OK(status)) {
1695                 reply_nterror(req, status);
1696                 return;
1697         }
1698
1699   done:
1700         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1701         return;
1702 }
1703
1704 /****************************************************************************
1705  Reply to NT IOCTL
1706 ****************************************************************************/
1707
1708 static void call_nt_transact_ioctl(connection_struct *conn,
1709                                    struct smb_request *req,
1710                                    uint16 **ppsetup, uint32 setup_count,
1711                                    char **ppparams, uint32 parameter_count,
1712                                    char **ppdata, uint32 data_count,
1713                                    uint32 max_data_count)
1714 {
1715         uint32 function;
1716         uint16 fidnum;
1717         files_struct *fsp;
1718         uint8 isFSctl;
1719         uint8 compfilter;
1720         static bool logged_message;
1721         char *pdata = *ppdata;
1722
1723         if (setup_count != 8) {
1724                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1725                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1726                 return;
1727         }
1728
1729         function = IVAL(*ppsetup, 0);
1730         fidnum = SVAL(*ppsetup, 4);
1731         isFSctl = CVAL(*ppsetup, 6);
1732         compfilter = CVAL(*ppsetup, 7);
1733
1734         DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
1735                  function, fidnum, isFSctl, compfilter));
1736
1737         fsp=file_fsp(req, fidnum);
1738         /* this check is done in each implemented function case for now
1739            because I don't want to break anything... --metze
1740         FSP_BELONGS_CONN(fsp,conn);*/
1741
1742         switch (function) {
1743         case FSCTL_SET_SPARSE:
1744                 /* pretend this succeeded - tho strictly we should
1745                    mark the file sparse (if the local fs supports it)
1746                    so we can know if we need to pre-allocate or not */
1747
1748                 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1749                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1750                 return;
1751
1752         case FSCTL_CREATE_OR_GET_OBJECT_ID:
1753         {
1754                 unsigned char objid[16];
1755
1756                 /* This should return the object-id on this file.
1757                  * I think I'll make this be the inode+dev. JRA.
1758                  */
1759
1760                 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1761
1762                 if (!fsp_belongs_conn(conn, req, fsp)) {
1763                         return;
1764                 }
1765
1766                 data_count = 64;
1767                 pdata = nttrans_realloc(ppdata, data_count);
1768                 if (pdata == NULL) {
1769                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1770                         return;
1771                 }
1772                 push_file_id_16(pdata, &fsp->file_id);
1773                 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1774                 push_file_id_16(pdata+32, &fsp->file_id);
1775                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1776                                 pdata, data_count);
1777                 return;
1778         }
1779
1780         case FSCTL_GET_REPARSE_POINT:
1781                 /* pretend this fail - my winXP does it like this
1782                  * --metze
1783                  */
1784
1785                 DEBUG(10,("FSCTL_GET_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_SET_REPARSE_POINT:
1790                 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1791                  * --metze
1792                  */
1793
1794                 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1795                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1796                 return;
1797
1798         case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1799         {
1800                 /*
1801                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1802                  * and return their volume names.  If max_data_count is 16, then it is just
1803                  * asking for the number of volumes and length of the combined names.
1804                  *
1805                  * pdata is the data allocated by our caller, but that uses
1806                  * total_data_count (which is 0 in our case) rather than max_data_count.
1807                  * Allocate the correct amount and return the pointer to let
1808                  * it be deallocated when we return.
1809                  */
1810                 SHADOW_COPY_DATA *shadow_data = NULL;
1811                 TALLOC_CTX *shadow_mem_ctx = NULL;
1812                 bool labels = False;
1813                 uint32 labels_data_count = 0;
1814                 uint32 i;
1815                 char *cur_pdata;
1816
1817                 if (!fsp_belongs_conn(conn, req, fsp)) {
1818                         return;
1819                 }
1820
1821                 if (max_data_count < 16) {
1822                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1823                                 max_data_count));
1824                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1825                         return;
1826                 }
1827
1828                 if (max_data_count > 16) {
1829                         labels = True;
1830                 }
1831
1832                 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1833                 if (shadow_mem_ctx == NULL) {
1834                         DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1835                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1836                         return;
1837                 }
1838
1839                 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1840                 if (shadow_data == NULL) {
1841                         DEBUG(0,("TALLOC_ZERO() failed!\n"));
1842                         talloc_destroy(shadow_mem_ctx);
1843                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1844                         return;
1845                 }
1846
1847                 shadow_data->mem_ctx = shadow_mem_ctx;
1848
1849                 /*
1850                  * Call the VFS routine to actually do the work.
1851                  */
1852                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1853                         talloc_destroy(shadow_data->mem_ctx);
1854                         if (errno == ENOSYS) {
1855                                 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n", 
1856                                         conn->connectpath));
1857                                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1858                                 return;
1859                         } else {
1860                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n", 
1861                                         conn->connectpath));
1862                                 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1863                                 return;
1864                         }
1865                 }
1866
1867                 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1868
1869                 if (!labels) {
1870                         data_count = 16;
1871                 } else {
1872                         data_count = 12+labels_data_count+4;
1873                 }
1874
1875                 if (max_data_count<data_count) {
1876                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1877                                 max_data_count,data_count));
1878                         talloc_destroy(shadow_data->mem_ctx);
1879                         reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1880                         return;
1881                 }
1882
1883                 pdata = nttrans_realloc(ppdata, data_count);
1884                 if (pdata == NULL) {
1885                         talloc_destroy(shadow_data->mem_ctx);
1886                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1887                         return;
1888                 }
1889
1890                 cur_pdata = pdata;
1891
1892                 /* num_volumes 4 bytes */
1893                 SIVAL(pdata,0,shadow_data->num_volumes);
1894
1895                 if (labels) {
1896                         /* num_labels 4 bytes */
1897                         SIVAL(pdata,4,shadow_data->num_volumes);
1898                 }
1899
1900                 /* needed_data_count 4 bytes */
1901                 SIVAL(pdata,8,labels_data_count);
1902
1903                 cur_pdata+=12;
1904
1905                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1906                         shadow_data->num_volumes,fsp->fsp_name));
1907                 if (labels && shadow_data->labels) {
1908                         for (i=0;i<shadow_data->num_volumes;i++) {
1909                                 srvstr_push(pdata, req->flags2,
1910                                             cur_pdata, shadow_data->labels[i],
1911                                             2*sizeof(SHADOW_COPY_LABEL),
1912                                             STR_UNICODE|STR_TERMINATE);
1913                                 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1914                                 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1915                         }
1916                 }
1917
1918                 talloc_destroy(shadow_data->mem_ctx);
1919
1920                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1921                                 pdata, data_count);
1922
1923                 return;
1924         }
1925
1926         case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1927         {
1928                 /* pretend this succeeded -
1929                  *
1930                  * we have to send back a list with all files owned by this SID
1931                  *
1932                  * but I have to check that --metze
1933                  */
1934                 DOM_SID sid;
1935                 uid_t uid;
1936                 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1937
1938                 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1939
1940                 if (!fsp_belongs_conn(conn, req, fsp)) {
1941                         return;
1942                 }
1943
1944                 /* unknown 4 bytes: this is not the length of the sid :-(  */
1945                 /*unknown = IVAL(pdata,0);*/
1946
1947                 sid_parse(pdata+4,sid_len,&sid);
1948                 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
1949
1950                 if (!sid_to_uid(&sid, &uid)) {
1951                         DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
1952                                  sid_string_dbg(&sid),
1953                                  (unsigned long)sid_len));
1954                         uid = (-1);
1955                 }
1956
1957                 /* we can take a look at the find source :-)
1958                  *
1959                  * find ./ -uid $uid  -name '*'   is what we need here
1960                  *
1961                  *
1962                  * and send 4bytes len and then NULL terminated unicode strings
1963                  * for each file
1964                  *
1965                  * but I don't know how to deal with the paged results
1966                  * (maybe we can hang the result anywhere in the fsp struct)
1967                  *
1968                  * we don't send all files at once
1969                  * and at the next we should *not* start from the beginning,
1970                  * so we have to cache the result
1971                  *
1972                  * --metze
1973                  */
1974
1975                 /* this works for now... */
1976                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1977                 return;
1978         }
1979         default:
1980                 if (!logged_message) {
1981                         logged_message = True; /* Only print this once... */
1982                         DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
1983                                  function));
1984                 }
1985         }
1986
1987         reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1988 }
1989
1990
1991 #ifdef HAVE_SYS_QUOTAS
1992 /****************************************************************************
1993  Reply to get user quota
1994 ****************************************************************************/
1995
1996 static void call_nt_transact_get_user_quota(connection_struct *conn,
1997                                             struct smb_request *req,
1998                                             uint16 **ppsetup,
1999                                             uint32 setup_count,
2000                                             char **ppparams,
2001                                             uint32 parameter_count,
2002                                             char **ppdata,
2003                                             uint32 data_count,
2004                                             uint32 max_data_count)
2005 {
2006         NTSTATUS nt_status = NT_STATUS_OK;
2007         char *params = *ppparams;
2008         char *pdata = *ppdata;
2009         char *entry;
2010         int data_len=0,param_len=0;
2011         int qt_len=0;
2012         int entry_len = 0;
2013         files_struct *fsp = NULL;
2014         uint16 level = 0;
2015         size_t sid_len;
2016         DOM_SID sid;
2017         bool start_enum = True;
2018         SMB_NTQUOTA_STRUCT qt;
2019         SMB_NTQUOTA_LIST *tmp_list;
2020         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2021
2022         ZERO_STRUCT(qt);
2023
2024         /* access check */
2025         if (conn->server_info->utok.uid != 0) {
2026                 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2027                          "[%s]\n", lp_servicename(SNUM(conn)),
2028                          conn->server_info->unix_name));
2029                 reply_doserror(req, ERRDOS, ERRnoaccess);
2030                 return;
2031         }
2032
2033         /*
2034          * Ensure minimum number of parameters sent.
2035          */
2036
2037         if (parameter_count < 4) {
2038                 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2039                 reply_doserror(req, ERRDOS, ERRinvalidparam);
2040                 return;
2041         }
2042
2043         /* maybe we can check the quota_fnum */
2044         fsp = file_fsp(req, SVAL(params,0));
2045         if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2046                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2047                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2048                 return;
2049         }
2050
2051         /* the NULL pointer checking for fsp->fake_file_handle->pd
2052          * is done by CHECK_NTQUOTA_HANDLE_OK()
2053          */
2054         qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2055
2056         level = SVAL(params,2);
2057
2058         /* unknown 12 bytes leading in params */
2059
2060         switch (level) {
2061                 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2062                         /* seems that we should continue with the enum here --metze */
2063
2064                         if (qt_handle->quota_list!=NULL &&
2065                             qt_handle->tmp_list==NULL) {
2066
2067                                 /* free the list */
2068                                 free_ntquota_list(&(qt_handle->quota_list));
2069
2070                                 /* Realloc the size of parameters and data we will return */
2071                                 param_len = 4;
2072                                 params = nttrans_realloc(ppparams, param_len);
2073                                 if(params == NULL) {
2074                                         reply_doserror(req, ERRDOS, ERRnomem);
2075                                         return;
2076                                 }
2077
2078                                 data_len = 0;
2079                                 SIVAL(params,0,data_len);
2080
2081                                 break;
2082                         }
2083
2084                         start_enum = False;
2085
2086                 case TRANSACT_GET_USER_QUOTA_LIST_START:
2087
2088                         if (qt_handle->quota_list==NULL &&
2089                                 qt_handle->tmp_list==NULL) {
2090                                 start_enum = True;
2091                         }
2092
2093                         if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2094                                 reply_doserror(req, ERRSRV, ERRerror);
2095                                 return;
2096                         }
2097
2098                         /* Realloc the size of parameters and data we will return */
2099                         param_len = 4;
2100                         params = nttrans_realloc(ppparams, param_len);
2101                         if(params == NULL) {
2102                                 reply_doserror(req, ERRDOS, ERRnomem);
2103                                 return;
2104                         }
2105
2106                         /* we should not trust the value in max_data_count*/
2107                         max_data_count = MIN(max_data_count,2048);
2108
2109                         pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2110                         if(pdata == NULL) {
2111                                 reply_doserror(req, ERRDOS, ERRnomem);
2112                                 return;
2113                         }
2114
2115                         entry = pdata;
2116
2117                         /* set params Size of returned Quota Data 4 bytes*/
2118                         /* but set it later when we know it */
2119
2120                         /* for each entry push the data */
2121
2122                         if (start_enum) {
2123                                 qt_handle->tmp_list = qt_handle->quota_list;
2124                         }
2125
2126                         tmp_list = qt_handle->tmp_list;
2127
2128                         for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2129                                 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2130
2131                                 sid_len = ndr_size_dom_sid(
2132                                         &tmp_list->quotas->sid, 0);
2133                                 entry_len = 40 + sid_len;
2134
2135                                 /* nextoffset entry 4 bytes */
2136                                 SIVAL(entry,0,entry_len);
2137
2138                                 /* then the len of the SID 4 bytes */
2139                                 SIVAL(entry,4,sid_len);
2140
2141                                 /* unknown data 8 bytes uint64_t */
2142                                 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2143
2144                                 /* the used disk space 8 bytes uint64_t */
2145                                 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2146
2147                                 /* the soft quotas 8 bytes uint64_t */
2148                                 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2149
2150                                 /* the hard quotas 8 bytes uint64_t */
2151                                 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2152
2153                                 /* and now the SID */
2154                                 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2155                         }
2156
2157                         qt_handle->tmp_list = tmp_list;
2158
2159                         /* overwrite the offset of the last entry */
2160                         SIVAL(entry-entry_len,0,0);
2161
2162                         data_len = 4+qt_len;
2163                         /* overwrite the params quota_data_len */
2164                         SIVAL(params,0,data_len);
2165
2166                         break;
2167
2168                 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2169
2170                         /* unknown 4 bytes IVAL(pdata,0) */
2171
2172                         if (data_count < 8) {
2173                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2174                                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2175                                 return;
2176                         }
2177
2178                         sid_len = IVAL(pdata,4);
2179                         /* Ensure this is less than 1mb. */
2180                         if (sid_len > (1024*1024)) {
2181                                 reply_doserror(req, ERRDOS, ERRnomem);
2182                                 return;
2183                         }
2184
2185                         if (data_count < 8+sid_len) {
2186                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2187                                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2188                                 return;
2189                         }
2190
2191                         data_len = 4+40+sid_len;
2192
2193                         if (max_data_count < data_len) {
2194                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2195                                         max_data_count, data_len));
2196                                 param_len = 4;
2197                                 SIVAL(params,0,data_len);
2198                                 data_len = 0;
2199                                 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2200                                 break;
2201                         }
2202
2203                         sid_parse(pdata+8,sid_len,&sid);
2204
2205                         if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2206                                 ZERO_STRUCT(qt);
2207                                 /*
2208                                  * we have to return zero's in all fields
2209                                  * instead of returning an error here
2210                                  * --metze
2211                                  */
2212                         }
2213
2214                         /* Realloc the size of parameters and data we will return */
2215                         param_len = 4;
2216                         params = nttrans_realloc(ppparams, param_len);
2217                         if(params == NULL) {
2218                                 reply_doserror(req, ERRDOS, ERRnomem);
2219                                 return;
2220                         }
2221
2222                         pdata = nttrans_realloc(ppdata, data_len);
2223                         if(pdata == NULL) {
2224                                 reply_doserror(req, ERRDOS, ERRnomem);
2225                                 return;
2226                         }
2227
2228                         entry = pdata;
2229
2230                         /* set params Size of returned Quota Data 4 bytes*/
2231                         SIVAL(params,0,data_len);
2232
2233                         /* nextoffset entry 4 bytes */
2234                         SIVAL(entry,0,0);
2235
2236                         /* then the len of the SID 4 bytes */
2237                         SIVAL(entry,4,sid_len);
2238
2239                         /* unknown data 8 bytes uint64_t */
2240                         SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2241
2242                         /* the used disk space 8 bytes uint64_t */
2243                         SBIG_UINT(entry,16,qt.usedspace);
2244
2245                         /* the soft quotas 8 bytes uint64_t */
2246                         SBIG_UINT(entry,24,qt.softlim);
2247
2248                         /* the hard quotas 8 bytes uint64_t */
2249                         SBIG_UINT(entry,32,qt.hardlim);
2250
2251                         /* and now the SID */
2252                         sid_linearize(entry+40, sid_len, &sid);
2253
2254                         break;
2255
2256                 default:
2257                         DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2258                         reply_doserror(req, ERRSRV, ERRerror);
2259                         return;
2260                         break;
2261         }
2262
2263         send_nt_replies(conn, req, nt_status, params, param_len,
2264                         pdata, data_len);
2265 }
2266
2267 /****************************************************************************
2268  Reply to set user quota
2269 ****************************************************************************/
2270
2271 static void call_nt_transact_set_user_quota(connection_struct *conn,
2272                                             struct smb_request *req,
2273                                             uint16 **ppsetup,
2274                                             uint32 setup_count,
2275                                             char **ppparams,
2276                                             uint32 parameter_count,
2277                                             char **ppdata,
2278                                             uint32 data_count,
2279                                             uint32 max_data_count)
2280 {
2281         char *params = *ppparams;
2282         char *pdata = *ppdata;
2283         int data_len=0,param_len=0;
2284         SMB_NTQUOTA_STRUCT qt;
2285         size_t sid_len;
2286         DOM_SID sid;
2287         files_struct *fsp = NULL;
2288
2289         ZERO_STRUCT(qt);
2290
2291         /* access check */
2292         if (conn->server_info->utok.uid != 0) {
2293                 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2294                          "[%s]\n", lp_servicename(SNUM(conn)),
2295                          conn->server_info->unix_name));
2296                 reply_doserror(req, ERRDOS, ERRnoaccess);
2297                 return;
2298         }
2299
2300         /*
2301          * Ensure minimum number of parameters sent.
2302          */
2303
2304         if (parameter_count < 2) {
2305                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2306                 reply_doserror(req, ERRDOS, ERRinvalidparam);
2307                 return;
2308         }
2309
2310         /* maybe we can check the quota_fnum */
2311         fsp = file_fsp(req, SVAL(params,0));
2312         if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2313                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2314                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2315                 return;
2316         }
2317
2318         if (data_count < 40) {
2319                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2320                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2321                 return;
2322         }
2323
2324         /* offset to next quota record.
2325          * 4 bytes IVAL(pdata,0)
2326          * unused here...
2327          */
2328
2329         /* sid len */
2330         sid_len = IVAL(pdata,4);
2331
2332         if (data_count < 40+sid_len) {
2333                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2334                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2335                 return;
2336         }
2337
2338         /* unknown 8 bytes in pdata
2339          * maybe its the change time in NTTIME
2340          */
2341
2342         /* the used space 8 bytes (uint64_t)*/
2343         qt.usedspace = (uint64_t)IVAL(pdata,16);
2344 #ifdef LARGE_SMB_OFF_T
2345         qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2346 #else /* LARGE_SMB_OFF_T */
2347         if ((IVAL(pdata,20) != 0)&&
2348                 ((qt.usedspace != 0xFFFFFFFF)||
2349                 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2350                 /* more than 32 bits? */
2351                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2352                 return;
2353         }
2354 #endif /* LARGE_SMB_OFF_T */
2355
2356         /* the soft quotas 8 bytes (uint64_t)*/
2357         qt.softlim = (uint64_t)IVAL(pdata,24);
2358 #ifdef LARGE_SMB_OFF_T
2359         qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2360 #else /* LARGE_SMB_OFF_T */
2361         if ((IVAL(pdata,28) != 0)&&
2362                 ((qt.softlim != 0xFFFFFFFF)||
2363                 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2364                 /* more than 32 bits? */
2365                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2366                 return;
2367         }
2368 #endif /* LARGE_SMB_OFF_T */
2369
2370         /* the hard quotas 8 bytes (uint64_t)*/
2371         qt.hardlim = (uint64_t)IVAL(pdata,32);
2372 #ifdef LARGE_SMB_OFF_T
2373         qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2374 #else /* LARGE_SMB_OFF_T */
2375         if ((IVAL(pdata,36) != 0)&&
2376                 ((qt.hardlim != 0xFFFFFFFF)||
2377                 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2378                 /* more than 32 bits? */
2379                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2380                 return;
2381         }
2382 #endif /* LARGE_SMB_OFF_T */
2383
2384         sid_parse(pdata+40,sid_len,&sid);
2385         DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2386
2387         /* 44 unknown bytes left... */
2388
2389         if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2390                 reply_doserror(req, ERRSRV, ERRerror);
2391                 return;
2392         }
2393
2394         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2395                         pdata, data_len);
2396 }
2397 #endif /* HAVE_SYS_QUOTAS */
2398
2399 static void handle_nttrans(connection_struct *conn,
2400                            struct trans_state *state,
2401                            struct smb_request *req)
2402 {
2403         if (Protocol >= PROTOCOL_NT1) {
2404                 req->flags2 |= 0x40; /* IS_LONG_NAME */
2405                 SSVAL(req->inbuf,smb_flg2,req->flags2);
2406         }
2407
2408         /* Now we must call the relevant NT_TRANS function */
2409         switch(state->call) {
2410                 case NT_TRANSACT_CREATE:
2411                 {
2412                         START_PROFILE(NT_transact_create);
2413                         call_nt_transact_create(
2414                                 conn, req,
2415                                 &state->setup, state->setup_count,
2416                                 &state->param, state->total_param,
2417                                 &state->data, state->total_data,
2418                                 state->max_data_return);
2419                         END_PROFILE(NT_transact_create);
2420                         break;
2421                 }
2422
2423                 case NT_TRANSACT_IOCTL:
2424                 {
2425                         START_PROFILE(NT_transact_ioctl);
2426                         call_nt_transact_ioctl(
2427                                 conn, req,
2428                                 &state->setup, state->setup_count,
2429                                 &state->param, state->total_param,
2430                                 &state->data, state->total_data,
2431                                 state->max_data_return);
2432                         END_PROFILE(NT_transact_ioctl);
2433                         break;
2434                 }
2435
2436                 case NT_TRANSACT_SET_SECURITY_DESC:
2437                 {
2438                         START_PROFILE(NT_transact_set_security_desc);
2439                         call_nt_transact_set_security_desc(
2440                                 conn, req,
2441                                 &state->setup, state->setup_count,
2442                                 &state->param, state->total_param,
2443                                 &state->data, state->total_data,
2444                                 state->max_data_return);
2445                         END_PROFILE(NT_transact_set_security_desc);
2446                         break;
2447                 }
2448
2449                 case NT_TRANSACT_NOTIFY_CHANGE:
2450                 {
2451                         START_PROFILE(NT_transact_notify_change);
2452                         call_nt_transact_notify_change(
2453                                 conn, req,
2454                                 &state->setup, state->setup_count,
2455                                 &state->param, state->total_param,
2456                                 &state->data, state->total_data,
2457                                 state->max_data_return,
2458                                 state->max_param_return);
2459                         END_PROFILE(NT_transact_notify_change);
2460                         break;
2461                 }
2462
2463                 case NT_TRANSACT_RENAME:
2464                 {
2465                         START_PROFILE(NT_transact_rename);
2466                         call_nt_transact_rename(
2467                                 conn, req,
2468                                 &state->setup, state->setup_count,
2469                                 &state->param, state->total_param,
2470                                 &state->data, state->total_data,
2471                                 state->max_data_return);
2472                         END_PROFILE(NT_transact_rename);
2473                         break;
2474                 }
2475
2476                 case NT_TRANSACT_QUERY_SECURITY_DESC:
2477                 {
2478                         START_PROFILE(NT_transact_query_security_desc);
2479                         call_nt_transact_query_security_desc(
2480                                 conn, req,
2481                                 &state->setup, state->setup_count,
2482                                 &state->param, state->total_param,
2483                                 &state->data, state->total_data,
2484                                 state->max_data_return);
2485                         END_PROFILE(NT_transact_query_security_desc);
2486                         break;
2487                 }
2488
2489 #ifdef HAVE_SYS_QUOTAS
2490                 case NT_TRANSACT_GET_USER_QUOTA:
2491                 {
2492                         START_PROFILE(NT_transact_get_user_quota);
2493                         call_nt_transact_get_user_quota(
2494                                 conn, req,
2495                                 &state->setup, state->setup_count,
2496                                 &state->param, state->total_param,
2497                                 &state->data, state->total_data,
2498                                 state->max_data_return);
2499                         END_PROFILE(NT_transact_get_user_quota);
2500                         break;
2501                 }
2502
2503                 case NT_TRANSACT_SET_USER_QUOTA:
2504                 {
2505                         START_PROFILE(NT_transact_set_user_quota);
2506                         call_nt_transact_set_user_quota(
2507                                 conn, req,
2508                                 &state->setup, state->setup_count,
2509                                 &state->param, state->total_param,
2510                                 &state->data, state->total_data,
2511                                 state->max_data_return);
2512                         END_PROFILE(NT_transact_set_user_quota);
2513                         break;
2514                 }
2515 #endif /* HAVE_SYS_QUOTAS */
2516
2517                 default:
2518                         /* Error in request */
2519                         DEBUG(0,("handle_nttrans: Unknown request %d in "
2520                                  "nttrans call\n", state->call));
2521                         reply_doserror(req, ERRSRV, ERRerror);
2522                         return;
2523         }
2524         return;
2525 }
2526
2527 /****************************************************************************
2528  Reply to a SMBNTtrans.
2529 ****************************************************************************/
2530
2531 void reply_nttrans(struct smb_request *req)
2532 {
2533         connection_struct *conn = req->conn;
2534         uint32_t pscnt;
2535         uint32_t psoff;
2536         uint32_t dscnt;
2537         uint32_t dsoff;
2538         uint16 function_code;
2539         NTSTATUS result;
2540         struct trans_state *state;
2541         uint32_t size;
2542         uint32_t av_size;
2543
2544         START_PROFILE(SMBnttrans);
2545
2546         if (req->wct < 19) {
2547                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2548                 END_PROFILE(SMBnttrans);
2549                 return;
2550         }
2551
2552         size = smb_len(req->inbuf) + 4;
2553         av_size = smb_len(req->inbuf);
2554         pscnt = IVAL(req->vwv+9, 1);
2555         psoff = IVAL(req->vwv+11, 1);
2556         dscnt = IVAL(req->vwv+13, 1);
2557         dsoff = IVAL(req->vwv+15, 1);
2558         function_code = SVAL(req->vwv+18, 0);
2559
2560         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2561                 reply_doserror(req, ERRSRV, ERRaccess);
2562                 END_PROFILE(SMBnttrans);
2563                 return;
2564         }
2565
2566         result = allow_new_trans(conn->pending_trans, req->mid);
2567         if (!NT_STATUS_IS_OK(result)) {
2568                 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2569                 reply_nterror(req, result);
2570                 END_PROFILE(SMBnttrans);
2571                 return;
2572         }
2573
2574         if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2575                 reply_doserror(req, ERRSRV, ERRaccess);
2576                 END_PROFILE(SMBnttrans);
2577                 return;
2578         }
2579
2580         state->cmd = SMBnttrans;
2581
2582         state->mid = req->mid;
2583         state->vuid = req->vuid;
2584         state->total_data = IVAL(req->vwv+3, 1);
2585         state->data = NULL;
2586         state->total_param = IVAL(req->vwv+1, 1);
2587         state->param = NULL;
2588         state->max_data_return = IVAL(req->vwv+7, 1);
2589         state->max_param_return = IVAL(req->vwv+5, 1);
2590
2591         /* setup count is in *words* */
2592         state->setup_count = 2*CVAL(req->vwv+17, 1);
2593         state->setup = NULL;
2594         state->call = function_code;
2595
2596         DEBUG(10, ("num_setup=%u, "
2597                    "param_total=%u, this_param=%u, max_param=%u, "
2598                    "data_total=%u, this_data=%u, max_data=%u, "
2599                    "param_offset=%u, data_offset=%u\n",
2600                    (unsigned)state->setup_count,
2601                    (unsigned)state->total_param, (unsigned)pscnt,
2602                    (unsigned)state->max_param_return,
2603                    (unsigned)state->total_data, (unsigned)dscnt,
2604                    (unsigned)state->max_data_return,
2605                    (unsigned)psoff, (unsigned)dsoff));
2606
2607         /*
2608          * All nttrans messages we handle have smb_wct == 19 +
2609          * state->setup_count.  Ensure this is so as a sanity check.
2610          */
2611
2612         if(req->wct != 19 + (state->setup_count/2)) {
2613                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2614                          req->wct, 19 + (state->setup_count/2)));
2615                 goto bad_param;
2616         }
2617
2618         /* Don't allow more than 128mb for each value. */
2619         if ((state->total_data > (1024*1024*128)) ||
2620             (state->total_param > (1024*1024*128))) {
2621                 reply_doserror(req, ERRDOS, ERRnomem);
2622                 END_PROFILE(SMBnttrans);
2623                 return;
2624         }
2625
2626         if ((dscnt > state->total_data) || (pscnt > state->total_param))
2627                 goto bad_param;
2628
2629         if (state->total_data)  {
2630                 /* Can't use talloc here, the core routines do realloc on the
2631                  * params and data. */
2632                 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2633                         DEBUG(0,("reply_nttrans: data malloc fail for %u "
2634                                  "bytes !\n", (unsigned int)state->total_data));
2635                         TALLOC_FREE(state);
2636                         reply_doserror(req, ERRDOS, ERRnomem);
2637                         END_PROFILE(SMBnttrans);
2638                         return;
2639                 }
2640
2641                 if (dscnt > state->total_data ||
2642                                 dsoff+dscnt < dsoff) {
2643                         goto bad_param;
2644                 }
2645
2646                 if (dsoff > av_size ||
2647                                 dscnt > av_size ||
2648                                 dsoff+dscnt > av_size) {
2649                         goto bad_param;
2650                 }
2651
2652                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2653         }
2654
2655         if (state->total_param) {
2656                 /* Can't use talloc here, the core routines do realloc on the
2657                  * params and data. */
2658                 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2659                         DEBUG(0,("reply_nttrans: param malloc fail for %u "
2660                                  "bytes !\n", (unsigned int)state->total_param));
2661                         SAFE_FREE(state->data);
2662                         TALLOC_FREE(state);
2663                         reply_doserror(req, ERRDOS, ERRnomem);
2664                         END_PROFILE(SMBnttrans);
2665                         return;
2666                 }
2667
2668                 if (pscnt > state->total_param ||
2669                                 psoff+pscnt < psoff) {
2670                         goto bad_param;
2671                 }
2672
2673                 if (psoff > av_size ||
2674                                 pscnt > av_size ||
2675                                 psoff+pscnt > av_size) {
2676                         goto bad_param;
2677                 }
2678
2679                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2680         }
2681
2682         state->received_data  = dscnt;
2683         state->received_param = pscnt;
2684
2685         if(state->setup_count > 0) {
2686                 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2687                           state->setup_count));
2688                 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2689                 if (state->setup == NULL) {
2690                         DEBUG(0,("reply_nttrans : Out of memory\n"));
2691                         SAFE_FREE(state->data);
2692                         SAFE_FREE(state->param);
2693                         TALLOC_FREE(state);
2694                         reply_doserror(req, ERRDOS, ERRnomem);
2695                         END_PROFILE(SMBnttrans);
2696                         return;
2697                 }
2698
2699                 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2700                     (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2701                         goto bad_param;
2702                 }
2703                 if (smb_nt_SetupStart + state->setup_count > size) {
2704                         goto bad_param;
2705                 }
2706
2707                 memcpy(state->setup, req->vwv+19, state->setup_count);
2708                 dump_data(10, (uint8 *)state->setup, state->setup_count);
2709         }
2710
2711         if ((state->received_data == state->total_data) &&
2712             (state->received_param == state->total_param)) {
2713                 handle_nttrans(conn, state, req);
2714                 SAFE_FREE(state->param);
2715                 SAFE_FREE(state->data);
2716                 TALLOC_FREE(state);
2717                 END_PROFILE(SMBnttrans);
2718                 return;
2719         }
2720
2721         DLIST_ADD(conn->pending_trans, state);
2722
2723         /* We need to send an interim response then receive the rest
2724            of the parameter/data bytes */
2725         reply_outbuf(req, 0, 0);
2726         show_msg((char *)req->outbuf);
2727         END_PROFILE(SMBnttrans);
2728         return;
2729
2730   bad_param:
2731
2732         DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2733         SAFE_FREE(state->data);
2734         SAFE_FREE(state->param);
2735         TALLOC_FREE(state);
2736         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2737         END_PROFILE(SMBnttrans);
2738         return;
2739 }
2740
2741 /****************************************************************************
2742  Reply to a SMBnttranss
2743  ****************************************************************************/
2744
2745 void reply_nttranss(struct smb_request *req)
2746 {
2747         connection_struct *conn = req->conn;
2748         uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2749         struct trans_state *state;
2750         uint32_t av_size;
2751         uint32_t size;
2752
2753         START_PROFILE(SMBnttranss);
2754
2755         show_msg((char *)req->inbuf);
2756
2757         if (req->wct < 18) {
2758                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2759                 END_PROFILE(SMBnttranss);
2760                 return;
2761         }
2762
2763         for (state = conn->pending_trans; state != NULL;
2764              state = state->next) {
2765                 if (state->mid == req->mid) {
2766                         break;
2767                 }
2768         }
2769
2770         if ((state == NULL) || (state->cmd != SMBnttrans)) {
2771                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2772                 END_PROFILE(SMBnttranss);
2773                 return;
2774         }
2775
2776         /* Revise state->total_param and state->total_data in case they have
2777            changed downwards */
2778         if (IVAL(req->vwv+1, 1) < state->total_param) {
2779                 state->total_param = IVAL(req->vwv+1, 1);
2780         }
2781         if (IVAL(req->vwv+3, 1) < state->total_data) {
2782                 state->total_data = IVAL(req->vwv+3, 1);
2783         }
2784
2785         size = smb_len(req->inbuf) + 4;
2786         av_size = smb_len(req->inbuf);
2787
2788         pcnt = IVAL(req->vwv+5, 1);
2789         poff = IVAL(req->vwv+7, 1);
2790         pdisp = IVAL(req->vwv+9, 1);
2791
2792         dcnt = IVAL(req->vwv+11, 1);
2793         doff = IVAL(req->vwv+13, 1);
2794         ddisp = IVAL(req->vwv+15, 1);
2795
2796         state->received_param += pcnt;
2797         state->received_data += dcnt;
2798
2799         if ((state->received_data > state->total_data) ||
2800             (state->received_param > state->total_param))
2801                 goto bad_param;
2802
2803         if (pcnt) {
2804                 if (pdisp > state->total_param ||
2805                                 pcnt > state->total_param ||
2806                                 pdisp+pcnt > state->total_param ||
2807                                 pdisp+pcnt < pdisp) {
2808                         goto bad_param;
2809                 }
2810
2811                 if (poff > av_size ||
2812                                 pcnt > av_size ||
2813                                 poff+pcnt > av_size ||
2814                                 poff+pcnt < poff) {
2815                         goto bad_param;
2816                 }
2817
2818                 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2819                        pcnt);
2820         }
2821
2822         if (dcnt) {
2823                 if (ddisp > state->total_data ||
2824                                 dcnt > state->total_data ||
2825                                 ddisp+dcnt > state->total_data ||
2826                                 ddisp+dcnt < ddisp) {
2827                         goto bad_param;
2828                 }
2829
2830                 if (ddisp > av_size ||
2831                                 dcnt > av_size ||
2832                                 ddisp+dcnt > av_size ||
2833                                 ddisp+dcnt < ddisp) {
2834                         goto bad_param;
2835                 }
2836
2837                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2838                        dcnt);
2839         }
2840
2841         if ((state->received_param < state->total_param) ||
2842             (state->received_data < state->total_data)) {
2843                 END_PROFILE(SMBnttranss);
2844                 return;
2845         }
2846
2847         handle_nttrans(conn, state, req);
2848
2849         DLIST_REMOVE(conn->pending_trans, state);
2850         SAFE_FREE(state->data);
2851         SAFE_FREE(state->param);
2852         TALLOC_FREE(state);
2853         END_PROFILE(SMBnttranss);
2854         return;
2855
2856   bad_param:
2857
2858         DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2859         DLIST_REMOVE(conn->pending_trans, state);
2860         SAFE_FREE(state->data);
2861         SAFE_FREE(state->param);
2862         TALLOC_FREE(state);
2863         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2864         END_PROFILE(SMBnttranss);
2865         return;
2866 }