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