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