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