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