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