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