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