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