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