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