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