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