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