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