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