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