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