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