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