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