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