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