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