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