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