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