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