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