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