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