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