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