s3-smb Use FILE_ATTRIBUTE_ARCHIVE intead of aARCH
[samba.git] / source3 / smbd / nttrans.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB NT transaction handling
4    Copyright (C) Jeremy Allison                 1994-2007
5    Copyright (C) Stefan (metze) Metzmacher      2003
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "fake_file.h"
26 #include "../libcli/security/security.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "passdb/lookup_sid.h"
29 #include "auth.h"
30 #include "ntioctl.h"
31 #include "smbprofile.h"
32
33 extern const struct generic_mapping file_generic_mapping;
34
35 static char *nttrans_realloc(char **ptr, size_t size)
36 {
37         if (ptr==NULL) {
38                 smb_panic("nttrans_realloc() called with NULL ptr");
39         }
40
41         *ptr = (char *)SMB_REALLOC(*ptr, size);
42         if(*ptr == NULL) {
43                 return NULL;
44         }
45         memset(*ptr,'\0',size);
46         return *ptr;
47 }
48
49 /****************************************************************************
50  Send the required number of replies back.
51  We assume all fields other than the data fields are
52  set correctly for the type of call.
53  HACK ! Always assumes smb_setup field is zero.
54 ****************************************************************************/
55
56 static void send_nt_replies(connection_struct *conn,
57                             struct smb_request *req, NTSTATUS nt_error,
58                             char *params, int paramsize,
59                             char *pdata, int datasize)
60 {
61         int data_to_send = datasize;
62         int params_to_send = paramsize;
63         int useable_space;
64         char *pp = params;
65         char *pd = pdata;
66         int params_sent_thistime, data_sent_thistime, total_sent_thistime;
67         int alignment_offset = 1;
68         int data_alignment_offset = 0;
69         struct smbd_server_connection *sconn = req->sconn;
70         int max_send = sconn->smb1.sessions.max_send;
71
72         /*
73          * If there genuinely are no parameters or data to send just send
74          * the empty packet.
75          */
76
77         if(params_to_send == 0 && data_to_send == 0) {
78                 reply_outbuf(req, 18, 0);
79                 if (NT_STATUS_V(nt_error)) {
80                         error_packet_set((char *)req->outbuf,
81                                          0, 0, nt_error,
82                                          __LINE__,__FILE__);
83                 }
84                 show_msg((char *)req->outbuf);
85                 if (!srv_send_smb(sconn,
86                                 (char *)req->outbuf,
87                                 true, req->seqnum+1,
88                                 IS_CONN_ENCRYPTED(conn),
89                                 &req->pcd)) {
90                         exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
91                 }
92                 TALLOC_FREE(req->outbuf);
93                 return;
94         }
95
96         /*
97          * When sending params and data ensure that both are nicely aligned.
98          * Only do this alignment when there is also data to send - else
99          * can cause NT redirector problems.
100          */
101
102         if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
103                 data_alignment_offset = 4 - (params_to_send % 4);
104         }
105
106         /*
107          * Space is bufsize minus Netbios over TCP header minus SMB header.
108          * The alignment_offset is to align the param bytes on a four byte
109          * boundary (2 bytes for data len, one byte pad).
110          * NT needs this to work correctly.
111          */
112
113         useable_space = max_send - (smb_size
114                                     + 2 * 18 /* wct */
115                                     + alignment_offset
116                                     + data_alignment_offset);
117
118         if (useable_space < 0) {
119                 char *msg = talloc_asprintf(
120                         talloc_tos(),
121                         "send_nt_replies failed sanity useable_space = %d!!!",
122                         useable_space);
123                 DEBUG(0, ("%s\n", msg));
124                 exit_server_cleanly(msg);
125         }
126
127         while (params_to_send || data_to_send) {
128
129                 /*
130                  * Calculate whether we will totally or partially fill this packet.
131                  */
132
133                 total_sent_thistime = params_to_send + data_to_send;
134
135                 /*
136                  * We can never send more than useable_space.
137                  */
138
139                 total_sent_thistime = MIN(total_sent_thistime, useable_space);
140
141                 reply_outbuf(req, 18,
142                              total_sent_thistime + alignment_offset
143                              + data_alignment_offset);
144
145                 /*
146                  * We might have had SMBnttranss in req->inbuf, fix that.
147                  */
148                 SCVAL(req->outbuf, smb_com, SMBnttrans);
149
150                 /*
151                  * Set total params and data to be sent.
152                  */
153
154                 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
155                 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
156
157                 /*
158                  * Calculate how many parameters and data we can fit into
159                  * this packet. Parameters get precedence.
160                  */
161
162                 params_sent_thistime = MIN(params_to_send,useable_space);
163                 data_sent_thistime = useable_space - params_sent_thistime;
164                 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
165
166                 SIVAL(req->outbuf, smb_ntr_ParameterCount,
167                       params_sent_thistime);
168
169                 if(params_sent_thistime == 0) {
170                         SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
171                         SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
172                 } else {
173                         /*
174                          * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
175                          * parameter bytes, however the first 4 bytes of outbuf are
176                          * the Netbios over TCP header. Thus use smb_base() to subtract
177                          * them from the calculation.
178                          */
179
180                         SIVAL(req->outbuf,smb_ntr_ParameterOffset,
181                               ((smb_buf(req->outbuf)+alignment_offset)
182                                - smb_base(req->outbuf)));
183                         /*
184                          * Absolute displacement of param bytes sent in this packet.
185                          */
186
187                         SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
188                               pp - params);
189                 }
190
191                 /*
192                  * Deal with the data portion.
193                  */
194
195                 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
196
197                 if(data_sent_thistime == 0) {
198                         SIVAL(req->outbuf,smb_ntr_DataOffset,0);
199                         SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
200                 } else {
201                         /*
202                          * The offset of the data bytes is the offset of the
203                          * parameter bytes plus the number of parameters being sent this time.
204                          */
205
206                         SIVAL(req->outbuf, smb_ntr_DataOffset,
207                               ((smb_buf(req->outbuf)+alignment_offset) -
208                                smb_base(req->outbuf))
209                               + params_sent_thistime + data_alignment_offset);
210                         SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
211                 }
212
213                 /*
214                  * Copy the param bytes into the packet.
215                  */
216
217                 if(params_sent_thistime) {
218                         if (alignment_offset != 0) {
219                                 memset(smb_buf(req->outbuf), 0,
220                                        alignment_offset);
221                         }
222                         memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
223                                params_sent_thistime);
224                 }
225
226                 /*
227                  * Copy in the data bytes
228                  */
229
230                 if(data_sent_thistime) {
231                         if (data_alignment_offset != 0) {
232                                 memset((smb_buf(req->outbuf)+alignment_offset+
233                                         params_sent_thistime), 0,
234                                        data_alignment_offset);
235                         }
236                         memcpy(smb_buf(req->outbuf)+alignment_offset
237                                +params_sent_thistime+data_alignment_offset,
238                                pd,data_sent_thistime);
239                 }
240
241                 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
242                         params_sent_thistime, data_sent_thistime, useable_space));
243                 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
244                         params_to_send, data_to_send, paramsize, datasize));
245
246                 if (NT_STATUS_V(nt_error)) {
247                         error_packet_set((char *)req->outbuf,
248                                          0, 0, nt_error,
249                                          __LINE__,__FILE__);
250                 }
251
252                 /* Send the packet */
253                 show_msg((char *)req->outbuf);
254                 if (!srv_send_smb(sconn,
255                                 (char *)req->outbuf,
256                                 true, req->seqnum+1,
257                                 IS_CONN_ENCRYPTED(conn),
258                                 &req->pcd)) {
259                         exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
260                 }
261
262                 TALLOC_FREE(req->outbuf);
263
264                 pp += params_sent_thistime;
265                 pd += data_sent_thistime;
266
267                 params_to_send -= params_sent_thistime;
268                 data_to_send -= data_sent_thistime;
269
270                 /*
271                  * Sanity check
272                  */
273
274                 if(params_to_send < 0 || data_to_send < 0) {
275                         DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
276                                 params_to_send, data_to_send));
277                         exit_server_cleanly("send_nt_replies: internal error");
278                 }
279         }
280 }
281
282 /****************************************************************************
283  Reply to an NT create and X call on a pipe
284 ****************************************************************************/
285
286 static void nt_open_pipe(char *fname, connection_struct *conn,
287                          struct smb_request *req, int *ppnum)
288 {
289         files_struct *fsp;
290         NTSTATUS status;
291
292         DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
293
294         /* Strip \\ off the name. */
295         fname++;
296
297         status = open_np_file(req, fname, &fsp);
298         if (!NT_STATUS_IS_OK(status)) {
299                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
300                         reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
301                                         ERRDOS, ERRbadpipe);
302                         return;
303                 }
304                 reply_nterror(req, status);
305                 return;
306         }
307
308         *ppnum = fsp->fnum;
309         return;
310 }
311
312 /****************************************************************************
313  Reply to an NT create and X call for pipes.
314 ****************************************************************************/
315
316 static void do_ntcreate_pipe_open(connection_struct *conn,
317                                   struct smb_request *req)
318 {
319         char *fname = NULL;
320         int pnum = -1;
321         char *p = NULL;
322         uint32 flags = IVAL(req->vwv+3, 1);
323         TALLOC_CTX *ctx = talloc_tos();
324
325         srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
326
327         if (!fname) {
328                 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
329                                 ERRDOS, ERRbadpipe);
330                 return;
331         }
332         nt_open_pipe(fname, conn, req, &pnum);
333
334         if (req->outbuf) {
335                 /* error reply */
336                 return;
337         }
338
339         /*
340          * Deal with pipe return.
341          */
342
343         if (flags & EXTENDED_RESPONSE_REQUIRED) {
344                 /* This is very strange. We
345                  * return 50 words, but only set
346                  * the wcnt to 42 ? It's definately
347                  * what happens on the wire....
348                  */
349                 reply_outbuf(req, 50, 0);
350                 SCVAL(req->outbuf,smb_wct,42);
351         } else {
352                 reply_outbuf(req, 34, 0);
353         }
354
355         p = (char *)req->outbuf + smb_vwv2;
356         p++;
357         SSVAL(p,0,pnum);
358         p += 2;
359         SIVAL(p,0,FILE_WAS_OPENED);
360         p += 4;
361         p += 32;
362         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
363         p += 20;
364         /* File type. */
365         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
366         /* Device state. */
367         SSVAL(p,2, 0x5FF); /* ? */
368         p += 4;
369
370         if (flags & EXTENDED_RESPONSE_REQUIRED) {
371                 p += 25;
372                 SIVAL(p,0,FILE_GENERIC_ALL);
373                 /*
374                  * For pipes W2K3 seems to return
375                  * 0x12019B next.
376                  * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
377                  */
378                 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
379         }
380
381         DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
382
383         chain_reply(req);
384 }
385
386 struct case_semantics_state {
387         connection_struct *conn;
388         bool case_sensitive;
389         bool case_preserve;
390         bool short_case_preserve;
391 };
392
393 /****************************************************************************
394  Restore case semantics.
395 ****************************************************************************/
396
397 static int restore_case_semantics(struct case_semantics_state *state)
398 {
399         state->conn->case_sensitive = state->case_sensitive;
400         state->conn->case_preserve = state->case_preserve;
401         state->conn->short_case_preserve = state->short_case_preserve;
402         return 0;
403 }
404
405 /****************************************************************************
406  Save case semantics.
407 ****************************************************************************/
408
409 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
410                                                 connection_struct *conn)
411 {
412         struct case_semantics_state *result;
413
414         if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
415                 return NULL;
416         }
417
418         result->conn = conn;
419         result->case_sensitive = conn->case_sensitive;
420         result->case_preserve = conn->case_preserve;
421         result->short_case_preserve = conn->short_case_preserve;
422
423         /* Set to POSIX. */
424         conn->case_sensitive = True;
425         conn->case_preserve = True;
426         conn->short_case_preserve = True;
427
428         talloc_set_destructor(result, restore_case_semantics);
429
430         return result;
431 }
432
433 /****************************************************************************
434  Reply to an NT create and X call.
435 ****************************************************************************/
436
437 void reply_ntcreate_and_X(struct smb_request *req)
438 {
439         connection_struct *conn = req->conn;
440         struct smb_filename *smb_fname = NULL;
441         char *fname = NULL;
442         uint32 flags;
443         uint32 access_mask;
444         uint32 file_attributes;
445         uint32 share_access;
446         uint32 create_disposition;
447         uint32 create_options;
448         uint16 root_dir_fid;
449         uint64_t allocation_size;
450         /* Breakout the oplock request bits so we can set the
451            reply bits separately. */
452         uint32 fattr=0;
453         SMB_OFF_T file_len = 0;
454         int info = 0;
455         files_struct *fsp = NULL;
456         char *p = NULL;
457         struct timespec create_timespec;
458         struct timespec c_timespec;
459         struct timespec a_timespec;
460         struct timespec m_timespec;
461         struct timespec write_time_ts;
462         NTSTATUS status;
463         int oplock_request;
464         uint8_t oplock_granted = NO_OPLOCK_RETURN;
465         struct case_semantics_state *case_state = NULL;
466         TALLOC_CTX *ctx = talloc_tos();
467
468         START_PROFILE(SMBntcreateX);
469
470         if (req->wct < 24) {
471                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
472                 goto out;
473         }
474
475         flags = IVAL(req->vwv+3, 1);
476         access_mask = IVAL(req->vwv+7, 1);
477         file_attributes = IVAL(req->vwv+13, 1);
478         share_access = IVAL(req->vwv+15, 1);
479         create_disposition = IVAL(req->vwv+17, 1);
480         create_options = IVAL(req->vwv+19, 1);
481         root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
482
483         allocation_size = BVAL(req->vwv+9, 1);
484
485         srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
486                             STR_TERMINATE, &status);
487
488         if (!NT_STATUS_IS_OK(status)) {
489                 reply_nterror(req, status);
490                 goto out;
491         }
492
493         DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
494                   "file_attributes = 0x%x, share_access = 0x%x, "
495                   "create_disposition = 0x%x create_options = 0x%x "
496                   "root_dir_fid = 0x%x, fname = %s\n",
497                         (unsigned int)flags,
498                         (unsigned int)access_mask,
499                         (unsigned int)file_attributes,
500                         (unsigned int)share_access,
501                         (unsigned int)create_disposition,
502                         (unsigned int)create_options,
503                         (unsigned int)root_dir_fid,
504                         fname));
505
506         /*
507          * we need to remove ignored bits when they come directly from the client
508          * because we reuse some of them for internal stuff
509          */
510         create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
511
512         /*
513          * If it's an IPC, use the pipe handler.
514          */
515
516         if (IS_IPC(conn)) {
517                 if (lp_nt_pipe_support()) {
518                         do_ntcreate_pipe_open(conn, req);
519                         goto out;
520                 }
521                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
522                 goto out;
523         }
524
525         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
526         if (oplock_request) {
527                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
528                         ? BATCH_OPLOCK : 0;
529         }
530
531         if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
532                 case_state = set_posix_case_semantics(ctx, conn);
533                 if (!case_state) {
534                         reply_nterror(req, NT_STATUS_NO_MEMORY);
535                         goto out;
536                 }
537         }
538
539         status = filename_convert(ctx,
540                                 conn,
541                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
542                                 fname,
543                                 0,
544                                 NULL,
545                                 &smb_fname);
546
547         TALLOC_FREE(case_state);
548
549         if (!NT_STATUS_IS_OK(status)) {
550                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
551                         reply_botherror(req,
552                                 NT_STATUS_PATH_NOT_COVERED,
553                                 ERRSRV, ERRbadpath);
554                         goto out;
555                 }
556                 reply_nterror(req, status);
557                 goto out;
558         }
559
560         /*
561          * Bug #6898 - clients using Windows opens should
562          * never be able to set this attribute into the
563          * VFS.
564          */
565         file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
566
567         status = SMB_VFS_CREATE_FILE(
568                 conn,                                   /* conn */
569                 req,                                    /* req */
570                 root_dir_fid,                           /* root_dir_fid */
571                 smb_fname,                              /* fname */
572                 access_mask,                            /* access_mask */
573                 share_access,                           /* share_access */
574                 create_disposition,                     /* create_disposition*/
575                 create_options,                         /* create_options */
576                 file_attributes,                        /* file_attributes */
577                 oplock_request,                         /* oplock_request */
578                 allocation_size,                        /* allocation_size */
579                 0,                                      /* private_flags */
580                 NULL,                                   /* sd */
581                 NULL,                                   /* ea_list */
582                 &fsp,                                   /* result */
583                 &info);                                 /* pinfo */
584
585         if (!NT_STATUS_IS_OK(status)) {
586                 if (open_was_deferred(req->mid)) {
587                         /* We have re-scheduled this call, no error. */
588                         goto out;
589                 }
590                 reply_openerror(req, status);
591                 goto out;
592         }
593
594         /* Ensure we're pointing at the correct stat struct. */
595         TALLOC_FREE(smb_fname);
596         smb_fname = fsp->fsp_name;
597
598         /*
599          * If the caller set the extended oplock request bit
600          * and we granted one (by whatever means) - set the
601          * correct bit for extended oplock reply.
602          */
603
604         if (oplock_request &&
605             (lp_fake_oplocks(SNUM(conn))
606              || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
607
608                 /*
609                  * Exclusive oplock granted
610                  */
611
612                 if (flags & REQUEST_BATCH_OPLOCK) {
613                         oplock_granted = BATCH_OPLOCK_RETURN;
614                 } else {
615                         oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
616                 }
617         } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
618                 oplock_granted = LEVEL_II_OPLOCK_RETURN;
619         } else {
620                 oplock_granted = NO_OPLOCK_RETURN;
621         }
622
623         file_len = smb_fname->st.st_ex_size;
624
625         if (flags & EXTENDED_RESPONSE_REQUIRED) {
626                 /* This is very strange. We
627                  * return 50 words, but only set
628                  * the wcnt to 42 ? It's definately
629                  * what happens on the wire....
630                  */
631                 reply_outbuf(req, 50, 0);
632                 SCVAL(req->outbuf,smb_wct,42);
633         } else {
634                 reply_outbuf(req, 34, 0);
635         }
636
637         p = (char *)req->outbuf + smb_vwv2;
638
639         SCVAL(p, 0, oplock_granted);
640
641         p++;
642         SSVAL(p,0,fsp->fnum);
643         p += 2;
644         if ((create_disposition == FILE_SUPERSEDE)
645             && (info == FILE_WAS_OVERWRITTEN)) {
646                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
647         } else {
648                 SIVAL(p,0,info);
649         }
650         p += 4;
651
652         fattr = dos_mode(conn, smb_fname);
653         if (fattr == 0) {
654                 fattr = FILE_ATTRIBUTE_NORMAL;
655         }
656
657         /* Deal with other possible opens having a modified
658            write time. JRA. */
659         ZERO_STRUCT(write_time_ts);
660         get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
661         if (!null_timespec(write_time_ts)) {
662                 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
663         }
664
665         /* Create time. */
666         create_timespec = get_create_timespec(conn, fsp, smb_fname);
667         a_timespec = smb_fname->st.st_ex_atime;
668         m_timespec = smb_fname->st.st_ex_mtime;
669         c_timespec = get_change_timespec(conn, fsp, smb_fname);
670
671         if (lp_dos_filetime_resolution(SNUM(conn))) {
672                 dos_filetime_timespec(&create_timespec);
673                 dos_filetime_timespec(&a_timespec);
674                 dos_filetime_timespec(&m_timespec);
675                 dos_filetime_timespec(&c_timespec);
676         }
677
678         put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
679         p += 8;
680         put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
681         p += 8;
682         put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
683         p += 8;
684         put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
685         p += 8;
686         SIVAL(p,0,fattr); /* File Attributes. */
687         p += 4;
688         SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
689         p += 8;
690         SOFF_T(p,0,file_len);
691         p += 8;
692         if (flags & EXTENDED_RESPONSE_REQUIRED) {
693                 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
694                 size_t num_names = 0;
695                 unsigned int num_streams;
696                 struct stream_struct *streams = NULL;
697
698                 /* Do we have any EA's ? */
699                 status = get_ea_names_from_file(ctx, conn, fsp,
700                                 smb_fname->base_name, NULL, &num_names);
701                 if (NT_STATUS_IS_OK(status) && num_names) {
702                         file_status &= ~NO_EAS;
703                 }
704                 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
705                         &num_streams, &streams);
706                 /* There is always one stream, ::$DATA. */
707                 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
708                         file_status &= ~NO_SUBSTREAMS;
709                 }
710                 TALLOC_FREE(streams);
711                 SSVAL(p,2,file_status);
712         }
713         p += 4;
714         SCVAL(p,0,fsp->is_directory ? 1 : 0);
715
716         if (flags & EXTENDED_RESPONSE_REQUIRED) {
717                 uint32 perms = 0;
718                 p += 25;
719                 if (fsp->is_directory ||
720                     can_write_to_file(conn, smb_fname)) {
721                         perms = FILE_GENERIC_ALL;
722                 } else {
723                         perms = FILE_GENERIC_READ|FILE_EXECUTE;
724                 }
725                 SIVAL(p,0,perms);
726         }
727
728         DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
729                 fsp->fnum, smb_fname_str_dbg(smb_fname)));
730
731         chain_reply(req);
732  out:
733         END_PROFILE(SMBntcreateX);
734         return;
735 }
736
737 /****************************************************************************
738  Reply to a NT_TRANSACT_CREATE call to open a pipe.
739 ****************************************************************************/
740
741 static void do_nt_transact_create_pipe(connection_struct *conn,
742                                        struct smb_request *req,
743                                        uint16 **ppsetup, uint32 setup_count,
744                                        char **ppparams, uint32 parameter_count,
745                                        char **ppdata, uint32 data_count)
746 {
747         char *fname = NULL;
748         char *params = *ppparams;
749         int pnum = -1;
750         char *p = NULL;
751         NTSTATUS status;
752         size_t param_len;
753         uint32 flags;
754         TALLOC_CTX *ctx = talloc_tos();
755
756         /*
757          * Ensure minimum number of parameters sent.
758          */
759
760         if(parameter_count < 54) {
761                 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
762                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
763                 return;
764         }
765
766         flags = IVAL(params,0);
767
768         srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
769                         parameter_count-53, STR_TERMINATE,
770                         &status);
771         if (!NT_STATUS_IS_OK(status)) {
772                 reply_nterror(req, status);
773                 return;
774         }
775
776         nt_open_pipe(fname, conn, req, &pnum);
777
778         if (req->outbuf) {
779                 /* Error return */
780                 return;
781         }
782
783         /* Realloc the size of parameters and data we will return */
784         if (flags & EXTENDED_RESPONSE_REQUIRED) {
785                 /* Extended response is 32 more byyes. */
786                 param_len = 101;
787         } else {
788                 param_len = 69;
789         }
790         params = nttrans_realloc(ppparams, param_len);
791         if(params == NULL) {
792                 reply_nterror(req, NT_STATUS_NO_MEMORY);
793                 return;
794         }
795
796         p = params;
797         SCVAL(p,0,NO_OPLOCK_RETURN);
798
799         p += 2;
800         SSVAL(p,0,pnum);
801         p += 2;
802         SIVAL(p,0,FILE_WAS_OPENED);
803         p += 8;
804
805         p += 32;
806         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
807         p += 20;
808         /* File type. */
809         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
810         /* Device state. */
811         SSVAL(p,2, 0x5FF); /* ? */
812         p += 4;
813
814         if (flags & EXTENDED_RESPONSE_REQUIRED) {
815                 p += 25;
816                 SIVAL(p,0,FILE_GENERIC_ALL);
817                 /*
818                  * For pipes W2K3 seems to return
819                  * 0x12019B next.
820                  * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
821                  */
822                 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
823         }
824
825         DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
826
827         /* Send the required number of replies */
828         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
829
830         return;
831 }
832
833 /****************************************************************************
834  Internal fn to set security descriptors.
835 ****************************************************************************/
836
837 NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
838                        uint32_t security_info_sent)
839 {
840         struct security_descriptor *psd = NULL;
841         NTSTATUS status;
842
843         if (sd_len == 0) {
844                 return NT_STATUS_INVALID_PARAMETER;
845         }
846
847         if (!CAN_WRITE(fsp->conn)) {
848                 return NT_STATUS_ACCESS_DENIED;
849         }
850
851         if (!lp_nt_acl_support(SNUM(fsp->conn))) {
852                 return NT_STATUS_OK;
853         }
854
855         status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
856
857         if (!NT_STATUS_IS_OK(status)) {
858                 return status;
859         }
860
861         if (psd->owner_sid == NULL) {
862                 security_info_sent &= ~SECINFO_OWNER;
863         }
864         if (psd->group_sid == NULL) {
865                 security_info_sent &= ~SECINFO_GROUP;
866         }
867
868         /* Ensure we have at least one thing set. */
869         if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
870                 return NT_STATUS_INVALID_PARAMETER;
871         }
872
873         /* Ensure we have the rights to do this. */
874         if (security_info_sent & SECINFO_OWNER) {
875                 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
876                         return NT_STATUS_ACCESS_DENIED;
877                 }
878         }
879
880         if (security_info_sent & SECINFO_GROUP) {
881                 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
882                         return NT_STATUS_ACCESS_DENIED;
883                 }
884         }
885
886         if (security_info_sent & SECINFO_DACL) {
887                 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
888                         return NT_STATUS_ACCESS_DENIED;
889                 }
890                 /* Convert all the generic bits. */
891                 if (psd->dacl) {
892                         security_acl_map_generic(psd->dacl, &file_generic_mapping);
893                 }
894         }
895
896         if (security_info_sent & SECINFO_SACL) {
897                 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
898                         return NT_STATUS_ACCESS_DENIED;
899                 }
900                 /* Convert all the generic bits. */
901                 if (psd->sacl) {
902                         security_acl_map_generic(psd->sacl, &file_generic_mapping);
903                 }
904         }
905
906         if (DEBUGLEVEL >= 10) {
907                 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
908                 NDR_PRINT_DEBUG(security_descriptor, psd);
909         }
910
911         status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
912
913         TALLOC_FREE(psd);
914
915         return status;
916 }
917
918 /****************************************************************************
919  Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
920 ****************************************************************************/
921
922 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
923 {
924         struct ea_list *ea_list_head = NULL;
925         size_t offset = 0;
926
927         if (data_size < 4) {
928                 return NULL;
929         }
930
931         while (offset + 4 <= data_size) {
932                 size_t next_offset = IVAL(pdata,offset);
933                 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
934
935                 if (!eal) {
936                         return NULL;
937                 }
938
939                 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
940                 if (next_offset == 0) {
941                         break;
942                 }
943                 offset += next_offset;
944         }
945
946         return ea_list_head;
947 }
948
949 /****************************************************************************
950  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
951 ****************************************************************************/
952
953 static void call_nt_transact_create(connection_struct *conn,
954                                     struct smb_request *req,
955                                     uint16 **ppsetup, uint32 setup_count,
956                                     char **ppparams, uint32 parameter_count,
957                                     char **ppdata, uint32 data_count,
958                                     uint32 max_data_count)
959 {
960         struct smb_filename *smb_fname = NULL;
961         char *fname = NULL;
962         char *params = *ppparams;
963         char *data = *ppdata;
964         /* Breakout the oplock request bits so we can set the reply bits separately. */
965         uint32 fattr=0;
966         SMB_OFF_T file_len = 0;
967         int info = 0;
968         files_struct *fsp = NULL;
969         char *p = NULL;
970         uint32 flags;
971         uint32 access_mask;
972         uint32 file_attributes;
973         uint32 share_access;
974         uint32 create_disposition;
975         uint32 create_options;
976         uint32 sd_len;
977         struct security_descriptor *sd = NULL;
978         uint32 ea_len;
979         uint16 root_dir_fid;
980         struct timespec create_timespec;
981         struct timespec c_timespec;
982         struct timespec a_timespec;
983         struct timespec m_timespec;
984         struct timespec write_time_ts;
985         struct ea_list *ea_list = NULL;
986         NTSTATUS status;
987         size_t param_len;
988         uint64_t allocation_size;
989         int oplock_request;
990         uint8_t oplock_granted;
991         struct case_semantics_state *case_state = NULL;
992         TALLOC_CTX *ctx = talloc_tos();
993
994         DEBUG(5,("call_nt_transact_create\n"));
995
996         /*
997          * If it's an IPC, use the pipe handler.
998          */
999
1000         if (IS_IPC(conn)) {
1001                 if (lp_nt_pipe_support()) {
1002                         do_nt_transact_create_pipe(
1003                                 conn, req,
1004                                 ppsetup, setup_count,
1005                                 ppparams, parameter_count,
1006                                 ppdata, data_count);
1007                         goto out;
1008                 }
1009                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1010                 goto out;
1011         }
1012
1013         /*
1014          * Ensure minimum number of parameters sent.
1015          */
1016
1017         if(parameter_count < 54) {
1018                 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1019                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1020                 goto out;
1021         }
1022
1023         flags = IVAL(params,0);
1024         access_mask = IVAL(params,8);
1025         file_attributes = IVAL(params,20);
1026         share_access = IVAL(params,24);
1027         create_disposition = IVAL(params,28);
1028         create_options = IVAL(params,32);
1029         sd_len = IVAL(params,36);
1030         ea_len = IVAL(params,40);
1031         root_dir_fid = (uint16)IVAL(params,4);
1032         allocation_size = BVAL(params,12);
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, 0, 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) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
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 FILE_ATTRIBUTE_ARCHIVE 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         bool stream_rename = false;
1499
1500         START_PROFILE(SMBntrename);
1501
1502         if (req->wct < 4) {
1503                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1504                 goto out;
1505         }
1506
1507         attrs = SVAL(req->vwv+0, 0);
1508         rename_type = SVAL(req->vwv+1, 0);
1509
1510         p = (const char *)req->buf + 1;
1511         p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1512                                        &status, &src_has_wcard);
1513         if (!NT_STATUS_IS_OK(status)) {
1514                 reply_nterror(req, status);
1515                 goto out;
1516         }
1517
1518         if (ms_has_wild(oldname)) {
1519                 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1520                 goto out;
1521         }
1522
1523         p++;
1524         p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1525                                        &status, &dest_has_wcard);
1526         if (!NT_STATUS_IS_OK(status)) {
1527                 reply_nterror(req, status);
1528                 goto out;
1529         }
1530
1531         if (!lp_posix_pathnames()) {
1532                 /* The newname must begin with a ':' if the
1533                    oldname contains a ':'. */
1534                 if (strchr_m(oldname, ':')) {
1535                         if (newname[0] != ':') {
1536                                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1537                                 goto out;
1538                         }
1539                         stream_rename = true;
1540                 }
1541         }
1542
1543         /*
1544          * If this is a rename operation, allow wildcards and save the
1545          * destination's last component.
1546          */
1547         if (rename_type == RENAME_FLAG_RENAME) {
1548                 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1549                 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1550         }
1551
1552         /* rename_internals() calls unix_convert(), so don't call it here. */
1553         status = filename_convert(ctx, conn,
1554                                   req->flags2 & FLAGS2_DFS_PATHNAMES,
1555                                   oldname,
1556                                   ucf_flags_src,
1557                                   NULL,
1558                                   &smb_fname_old);
1559         if (!NT_STATUS_IS_OK(status)) {
1560                 if (NT_STATUS_EQUAL(status,
1561                                     NT_STATUS_PATH_NOT_COVERED)) {
1562                         reply_botherror(req,
1563                                         NT_STATUS_PATH_NOT_COVERED,
1564                                         ERRSRV, ERRbadpath);
1565                         goto out;
1566                 }
1567                 reply_nterror(req, status);
1568                 goto out;
1569         }
1570
1571         status = filename_convert(ctx, conn,
1572                                   req->flags2 & FLAGS2_DFS_PATHNAMES,
1573                                   newname,
1574                                   ucf_flags_dst,
1575                                   &dest_has_wcard,
1576                                   &smb_fname_new);
1577         if (!NT_STATUS_IS_OK(status)) {
1578                 if (NT_STATUS_EQUAL(status,
1579                                     NT_STATUS_PATH_NOT_COVERED)) {
1580                         reply_botherror(req,
1581                                         NT_STATUS_PATH_NOT_COVERED,
1582                                         ERRSRV, ERRbadpath);
1583                         goto out;
1584                 }
1585                 reply_nterror(req, status);
1586                 goto out;
1587         }
1588
1589         if (stream_rename) {
1590                 /* smb_fname_new must be the same as smb_fname_old. */
1591                 TALLOC_FREE(smb_fname_new->base_name);
1592                 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1593                                                 smb_fname_old->base_name);
1594                 if (!smb_fname_new->base_name) {
1595                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1596                         goto out;
1597                 }
1598         }
1599
1600         DEBUG(3,("reply_ntrename: %s -> %s\n",
1601                  smb_fname_str_dbg(smb_fname_old),
1602                  smb_fname_str_dbg(smb_fname_new)));
1603
1604         switch(rename_type) {
1605                 case RENAME_FLAG_RENAME:
1606                         status = rename_internals(ctx, conn, req,
1607                                                   smb_fname_old, smb_fname_new,
1608                                                   attrs, False, src_has_wcard,
1609                                                   dest_has_wcard,
1610                                                   DELETE_ACCESS);
1611                         break;
1612                 case RENAME_FLAG_HARD_LINK:
1613                         if (src_has_wcard || dest_has_wcard) {
1614                                 /* No wildcards. */
1615                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1616                         } else {
1617                                 status = hardlink_internals(ctx, conn,
1618                                                             req,
1619                                                             false,
1620                                                             smb_fname_old,
1621                                                             smb_fname_new);
1622                         }
1623                         break;
1624                 case RENAME_FLAG_COPY:
1625                         if (src_has_wcard || dest_has_wcard) {
1626                                 /* No wildcards. */
1627                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1628                         } else {
1629                                 status = copy_internals(ctx, conn, req,
1630                                                         smb_fname_old,
1631                                                         smb_fname_new,
1632                                                         attrs);
1633                         }
1634                         break;
1635                 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1636                         status = NT_STATUS_INVALID_PARAMETER;
1637                         break;
1638                 default:
1639                         status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1640                         break;
1641         }
1642
1643         if (!NT_STATUS_IS_OK(status)) {
1644                 if (open_was_deferred(req->mid)) {
1645                         /* We have re-scheduled this call. */
1646                         goto out;
1647                 }
1648
1649                 reply_nterror(req, status);
1650                 goto out;
1651         }
1652
1653         reply_outbuf(req, 0, 0);
1654  out:
1655         END_PROFILE(SMBntrename);
1656         return;
1657 }
1658
1659 /****************************************************************************
1660  Reply to a notify change - queue the request and
1661  don't allow a directory to be opened.
1662 ****************************************************************************/
1663
1664 static void smbd_smb1_notify_reply(struct smb_request *req,
1665                                    NTSTATUS error_code,
1666                                    uint8_t *buf, size_t len)
1667 {
1668         send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1669 }
1670
1671 static void call_nt_transact_notify_change(connection_struct *conn,
1672                                            struct smb_request *req,
1673                                            uint16 **ppsetup,
1674                                            uint32 setup_count,
1675                                            char **ppparams,
1676                                            uint32 parameter_count,
1677                                            char **ppdata, uint32 data_count,
1678                                            uint32 max_data_count,
1679                                            uint32 max_param_count)
1680 {
1681         uint16 *setup = *ppsetup;
1682         files_struct *fsp;
1683         uint32 filter;
1684         NTSTATUS status;
1685         bool recursive;
1686
1687         if(setup_count < 6) {
1688                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1689                 return;
1690         }
1691
1692         fsp = file_fsp(req, SVAL(setup,4));
1693         filter = IVAL(setup, 0);
1694         recursive = (SVAL(setup, 6) != 0) ? True : False;
1695
1696         DEBUG(3,("call_nt_transact_notify_change\n"));
1697
1698         if(!fsp) {
1699                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1700                 return;
1701         }
1702
1703         {
1704                 char *filter_string;
1705
1706                 if (!(filter_string = notify_filter_string(NULL, filter))) {
1707                         reply_nterror(req,NT_STATUS_NO_MEMORY);
1708                         return;
1709                 }
1710
1711                 DEBUG(3,("call_nt_transact_notify_change: notify change "
1712                          "called on %s, filter = %s, recursive = %d\n",
1713                          fsp_str_dbg(fsp), filter_string, recursive));
1714
1715                 TALLOC_FREE(filter_string);
1716         }
1717
1718         if((!fsp->is_directory) || (conn != fsp->conn)) {
1719                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1720                 return;
1721         }
1722
1723         if (fsp->notify == NULL) {
1724
1725                 status = change_notify_create(fsp, filter, recursive);
1726
1727                 if (!NT_STATUS_IS_OK(status)) {
1728                         DEBUG(10, ("change_notify_create returned %s\n",
1729                                    nt_errstr(status)));
1730                         reply_nterror(req, status);
1731                         return;
1732                 }
1733         }
1734
1735         if (fsp->notify->num_changes != 0) {
1736
1737                 /*
1738                  * We've got changes pending, respond immediately
1739                  */
1740
1741                 /*
1742                  * TODO: write a torture test to check the filtering behaviour
1743                  * here.
1744                  */
1745
1746                 change_notify_reply(req,
1747                                     NT_STATUS_OK,
1748                                     max_param_count,
1749                                     fsp->notify,
1750                                     smbd_smb1_notify_reply);
1751
1752                 /*
1753                  * change_notify_reply() above has independently sent its
1754                  * results
1755                  */
1756                 return;
1757         }
1758
1759         /*
1760          * No changes pending, queue the request
1761          */
1762
1763         status = change_notify_add_request(req,
1764                         max_param_count,
1765                         filter,
1766                         recursive, fsp,
1767                         smbd_smb1_notify_reply);
1768         if (!NT_STATUS_IS_OK(status)) {
1769                 reply_nterror(req, status);
1770         }
1771         return;
1772 }
1773
1774 /****************************************************************************
1775  Reply to an NT transact rename command.
1776 ****************************************************************************/
1777
1778 static void call_nt_transact_rename(connection_struct *conn,
1779                                     struct smb_request *req,
1780                                     uint16 **ppsetup, uint32 setup_count,
1781                                     char **ppparams, uint32 parameter_count,
1782                                     char **ppdata, uint32 data_count,
1783                                     uint32 max_data_count)
1784 {
1785         char *params = *ppparams;
1786         char *new_name = NULL;
1787         files_struct *fsp = NULL;
1788         bool dest_has_wcard = False;
1789         NTSTATUS status;
1790         TALLOC_CTX *ctx = talloc_tos();
1791
1792         if(parameter_count < 5) {
1793                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1794                 return;
1795         }
1796
1797         fsp = file_fsp(req, SVAL(params, 0));
1798         if (!check_fsp(conn, req, fsp)) {
1799                 return;
1800         }
1801         srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1802                               parameter_count - 4,
1803                               STR_TERMINATE, &status, &dest_has_wcard);
1804         if (!NT_STATUS_IS_OK(status)) {
1805                 reply_nterror(req, status);
1806                 return;
1807         }
1808
1809         /*
1810          * W2K3 ignores this request as the RAW-RENAME test
1811          * demonstrates, so we do.
1812          */
1813         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1814
1815         DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1816                  fsp_str_dbg(fsp), new_name));
1817
1818         return;
1819 }
1820
1821 /******************************************************************************
1822  Fake up a completely empty SD.
1823 *******************************************************************************/
1824
1825 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1826 {
1827         size_t sd_size;
1828
1829         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1830         if(!*ppsd) {
1831                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1832                 return NT_STATUS_NO_MEMORY;
1833         }
1834
1835         return NT_STATUS_OK;
1836 }
1837
1838 /****************************************************************************
1839  Reply to query a security descriptor.
1840  Callable from SMB2 and SMB2.
1841  If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1842  the required size.
1843 ****************************************************************************/
1844
1845 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1846                                         TALLOC_CTX *mem_ctx,
1847                                         files_struct *fsp,
1848                                         uint32_t security_info_wanted,
1849                                         uint32_t max_data_count,
1850                                         uint8_t **ppmarshalled_sd,
1851                                         size_t *psd_size)
1852 {
1853         NTSTATUS status;
1854         struct security_descriptor *psd = NULL;
1855
1856         /*
1857          * Get the permissions to return.
1858          */
1859
1860         if ((security_info_wanted & SECINFO_SACL) &&
1861                         !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1862                 return NT_STATUS_ACCESS_DENIED;
1863         }
1864
1865         if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1866                         !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1867                 return NT_STATUS_ACCESS_DENIED;
1868         }
1869
1870         if (!lp_nt_acl_support(SNUM(conn))) {
1871                 status = get_null_nt_acl(mem_ctx, &psd);
1872         } else {
1873                 status = SMB_VFS_FGET_NT_ACL(
1874                         fsp, security_info_wanted, &psd);
1875         }
1876         if (!NT_STATUS_IS_OK(status)) {
1877                 return status;
1878         }
1879
1880         if (!(security_info_wanted & SECINFO_OWNER)) {
1881                 psd->owner_sid = NULL;
1882         }
1883         if (!(security_info_wanted & SECINFO_GROUP)) {
1884                 psd->group_sid = NULL;
1885         }
1886         if (!(security_info_wanted & SECINFO_DACL)) {
1887                 psd->dacl = NULL;
1888         }
1889         if (!(security_info_wanted & SECINFO_SACL)) {
1890                 psd->sacl = NULL;
1891         }
1892
1893         /* If the SACL/DACL is NULL, but was requested, we mark that it is
1894          * present in the reply to match Windows behavior */
1895         if (psd->sacl == NULL &&
1896             security_info_wanted & SECINFO_SACL)
1897                 psd->type |= SEC_DESC_SACL_PRESENT;
1898         if (psd->dacl == NULL &&
1899             security_info_wanted & SECINFO_DACL)
1900                 psd->type |= SEC_DESC_DACL_PRESENT;
1901
1902         *psd_size = ndr_size_security_descriptor(psd, 0);
1903
1904         DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1905                 (unsigned long)*psd_size));
1906
1907         if (DEBUGLEVEL >= 10) {
1908                 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1909                           fsp_str_dbg(fsp)));
1910                 NDR_PRINT_DEBUG(security_descriptor, psd);
1911         }
1912
1913         if (max_data_count < *psd_size) {
1914                 TALLOC_FREE(psd);
1915                 return NT_STATUS_BUFFER_TOO_SMALL;
1916         }
1917
1918         status = marshall_sec_desc(mem_ctx, psd,
1919                                    ppmarshalled_sd, psd_size);
1920
1921         if (!NT_STATUS_IS_OK(status)) {
1922                 TALLOC_FREE(psd);
1923                 return status;
1924         }
1925
1926         TALLOC_FREE(psd);
1927         return NT_STATUS_OK;
1928 }
1929
1930 /****************************************************************************
1931  SMB1 reply to query a security descriptor.
1932 ****************************************************************************/
1933
1934 static void call_nt_transact_query_security_desc(connection_struct *conn,
1935                                                  struct smb_request *req,
1936                                                  uint16 **ppsetup,
1937                                                  uint32 setup_count,
1938                                                  char **ppparams,
1939                                                  uint32 parameter_count,
1940                                                  char **ppdata,
1941                                                  uint32 data_count,
1942                                                  uint32 max_data_count)
1943 {
1944         char *params = *ppparams;
1945         char *data = *ppdata;
1946         size_t sd_size = 0;
1947         uint32 security_info_wanted;
1948         files_struct *fsp = NULL;
1949         NTSTATUS status;
1950         uint8_t *marshalled_sd = NULL;
1951
1952         if(parameter_count < 8) {
1953                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1954                 return;
1955         }
1956
1957         fsp = file_fsp(req, SVAL(params,0));
1958         if(!fsp) {
1959                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1960                 return;
1961         }
1962
1963         security_info_wanted = IVAL(params,4);
1964
1965         DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1966                  "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1967                  (unsigned int)security_info_wanted));
1968
1969         params = nttrans_realloc(ppparams, 4);
1970         if(params == NULL) {
1971                 reply_nterror(req, NT_STATUS_NO_MEMORY);
1972                 return;
1973         }
1974
1975         /*
1976          * Get the permissions to return.
1977          */
1978
1979         status = smbd_do_query_security_desc(conn,
1980                                         talloc_tos(),
1981                                         fsp,
1982                                         security_info_wanted,
1983                                         max_data_count,
1984                                         &marshalled_sd,
1985                                         &sd_size);
1986
1987         if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
1988                 SIVAL(params,0,(uint32_t)sd_size);
1989                 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1990                         params, 4, NULL, 0);
1991                 return;
1992         }
1993
1994         if (!NT_STATUS_IS_OK(status)) {
1995                 reply_nterror(req, status);
1996                 return;
1997         }
1998
1999         SMB_ASSERT(sd_size > 0);
2000
2001         SIVAL(params,0,(uint32_t)sd_size);
2002
2003         if (max_data_count < sd_size) {
2004                 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2005                                 params, 4, NULL, 0);
2006                 return;
2007         }
2008
2009         /*
2010          * Allocate the data we will return.
2011          */
2012
2013         data = nttrans_realloc(ppdata, sd_size);
2014         if(data == NULL) {
2015                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2016                 return;
2017         }
2018
2019         memcpy(data, marshalled_sd, sd_size);
2020
2021         send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2022
2023         return;
2024 }
2025
2026 /****************************************************************************
2027  Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2028 ****************************************************************************/
2029
2030 static void call_nt_transact_set_security_desc(connection_struct *conn,
2031                                                struct smb_request *req,
2032                                                uint16 **ppsetup,
2033                                                uint32 setup_count,
2034                                                char **ppparams,
2035                                                uint32 parameter_count,
2036                                                char **ppdata,
2037                                                uint32 data_count,
2038                                                uint32 max_data_count)
2039 {
2040         char *params= *ppparams;
2041         char *data = *ppdata;
2042         files_struct *fsp = NULL;
2043         uint32 security_info_sent = 0;
2044         NTSTATUS status;
2045
2046         if(parameter_count < 8) {
2047                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2048                 return;
2049         }
2050
2051         if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2052                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2053                 return;
2054         }
2055
2056         if (!CAN_WRITE(fsp->conn)) {
2057                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2058                 return;
2059         }
2060
2061         if(!lp_nt_acl_support(SNUM(conn))) {
2062                 goto done;
2063         }
2064
2065         security_info_sent = IVAL(params,4);
2066
2067         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2068                  fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2069
2070         if (data_count == 0) {
2071                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2072                 return;
2073         }
2074
2075         status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
2076
2077         if (!NT_STATUS_IS_OK(status)) {
2078                 reply_nterror(req, status);
2079                 return;
2080         }
2081
2082   done:
2083         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2084         return;
2085 }
2086
2087 /****************************************************************************
2088  Reply to NT IOCTL
2089 ****************************************************************************/
2090
2091 static void call_nt_transact_ioctl(connection_struct *conn,
2092                                    struct smb_request *req,
2093                                    uint16 **ppsetup, uint32 setup_count,
2094                                    char **ppparams, uint32 parameter_count,
2095                                    char **ppdata, uint32 data_count,
2096                                    uint32 max_data_count)
2097 {
2098         uint32 function;
2099         uint16 fidnum;
2100         files_struct *fsp;
2101         uint8 isFSctl;
2102         uint8 compfilter;
2103         char *pdata = *ppdata;
2104
2105         if (setup_count != 8) {
2106                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2107                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2108                 return;
2109         }
2110
2111         function = IVAL(*ppsetup, 0);
2112         fidnum = SVAL(*ppsetup, 4);
2113         isFSctl = CVAL(*ppsetup, 6);
2114         compfilter = CVAL(*ppsetup, 7);
2115
2116         DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
2117                  function, fidnum, isFSctl, compfilter));
2118
2119         fsp=file_fsp(req, fidnum);
2120         /* this check is done in each implemented function case for now
2121            because I don't want to break anything... --metze
2122         FSP_BELONGS_CONN(fsp,conn);*/
2123
2124         SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2125
2126         switch (function) {
2127         case FSCTL_SET_SPARSE:
2128         {
2129                 bool set_sparse = true;
2130                 NTSTATUS status;
2131
2132                 if (data_count >= 1 && pdata[0] == 0) {
2133                         set_sparse = false;
2134                 }
2135
2136                 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X]set[%u]\n",
2137                          fidnum, set_sparse));
2138
2139                 if (!check_fsp_open(conn, req, fsp)) {
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         case FSCTL_IS_VOLUME_DIRTY:
2454                 DEBUG(10,("FSCTL_IS_VOLUME_DIRTY: called on FID[0x%04X] "
2455                           "(but not implemented)\n", (int)fidnum));
2456                 /*
2457                  * http://msdn.microsoft.com/en-us/library/cc232128%28PROT.10%29.aspx
2458                  * says we have to respond with NT_STATUS_INVALID_PARAMETER
2459                  */
2460                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2461                 return;
2462         default:
2463                 /* Only print this once... */
2464                 if (!logged_ioctl_message) {
2465                         logged_ioctl_message = true;
2466                         DEBUG(2,("call_nt_transact_ioctl(0x%x): "
2467                                  "Currently not implemented.\n",
2468                                  function));
2469                 }
2470         }
2471
2472         reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2473 }
2474
2475
2476 #ifdef HAVE_SYS_QUOTAS
2477 /****************************************************************************
2478  Reply to get user quota
2479 ****************************************************************************/
2480
2481 static void call_nt_transact_get_user_quota(connection_struct *conn,
2482                                             struct smb_request *req,
2483                                             uint16 **ppsetup,
2484                                             uint32 setup_count,
2485                                             char **ppparams,
2486                                             uint32 parameter_count,
2487                                             char **ppdata,
2488                                             uint32 data_count,
2489                                             uint32 max_data_count)
2490 {
2491         NTSTATUS nt_status = NT_STATUS_OK;
2492         char *params = *ppparams;
2493         char *pdata = *ppdata;
2494         char *entry;
2495         int data_len=0,param_len=0;
2496         int qt_len=0;
2497         int entry_len = 0;
2498         files_struct *fsp = NULL;
2499         uint16 level = 0;
2500         size_t sid_len;
2501         struct dom_sid sid;
2502         bool start_enum = True;
2503         SMB_NTQUOTA_STRUCT qt;
2504         SMB_NTQUOTA_LIST *tmp_list;
2505         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2506
2507         ZERO_STRUCT(qt);
2508
2509         /* access check */
2510         if (get_current_uid(conn) != 0) {
2511                 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2512                          "[%s]\n", lp_servicename(SNUM(conn)),
2513                          conn->session_info->unix_name));
2514                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2515                 return;
2516         }
2517
2518         /*
2519          * Ensure minimum number of parameters sent.
2520          */
2521
2522         if (parameter_count < 4) {
2523                 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2524                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2525                 return;
2526         }
2527
2528         /* maybe we can check the quota_fnum */
2529         fsp = file_fsp(req, SVAL(params,0));
2530         if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2531                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2532                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2533                 return;
2534         }
2535
2536         /* the NULL pointer checking for fsp->fake_file_handle->pd
2537          * is done by CHECK_NTQUOTA_HANDLE_OK()
2538          */
2539         qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2540
2541         level = SVAL(params,2);
2542
2543         /* unknown 12 bytes leading in params */
2544
2545         switch (level) {
2546                 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2547                         /* seems that we should continue with the enum here --metze */
2548
2549                         if (qt_handle->quota_list!=NULL &&
2550                             qt_handle->tmp_list==NULL) {
2551
2552                                 /* free the list */
2553                                 free_ntquota_list(&(qt_handle->quota_list));
2554
2555                                 /* Realloc the size of parameters and data we will return */
2556                                 param_len = 4;
2557                                 params = nttrans_realloc(ppparams, param_len);
2558                                 if(params == NULL) {
2559                                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2560                                         return;
2561                                 }
2562
2563                                 data_len = 0;
2564                                 SIVAL(params,0,data_len);
2565
2566                                 break;
2567                         }
2568
2569                         start_enum = False;
2570
2571                 case TRANSACT_GET_USER_QUOTA_LIST_START:
2572
2573                         if (qt_handle->quota_list==NULL &&
2574                                 qt_handle->tmp_list==NULL) {
2575                                 start_enum = True;
2576                         }
2577
2578                         if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2579                                 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2580                                 return;
2581                         }
2582
2583                         /* Realloc the size of parameters and data we will return */
2584                         param_len = 4;
2585                         params = nttrans_realloc(ppparams, param_len);
2586                         if(params == NULL) {
2587                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2588                                 return;
2589                         }
2590
2591                         /* we should not trust the value in max_data_count*/
2592                         max_data_count = MIN(max_data_count,2048);
2593
2594                         pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2595                         if(pdata == NULL) {
2596                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2597                                 return;
2598                         }
2599
2600                         entry = pdata;
2601
2602                         /* set params Size of returned Quota Data 4 bytes*/
2603                         /* but set it later when we know it */
2604
2605                         /* for each entry push the data */
2606
2607                         if (start_enum) {
2608                                 qt_handle->tmp_list = qt_handle->quota_list;
2609                         }
2610
2611                         tmp_list = qt_handle->tmp_list;
2612
2613                         for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2614                                 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2615
2616                                 sid_len = ndr_size_dom_sid(
2617                                         &tmp_list->quotas->sid, 0);
2618                                 entry_len = 40 + sid_len;
2619
2620                                 /* nextoffset entry 4 bytes */
2621                                 SIVAL(entry,0,entry_len);
2622
2623                                 /* then the len of the SID 4 bytes */
2624                                 SIVAL(entry,4,sid_len);
2625
2626                                 /* unknown data 8 bytes uint64_t */
2627                                 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2628
2629                                 /* the used disk space 8 bytes uint64_t */
2630                                 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2631
2632                                 /* the soft quotas 8 bytes uint64_t */
2633                                 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2634
2635                                 /* the hard quotas 8 bytes uint64_t */
2636                                 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2637
2638                                 /* and now the SID */
2639                                 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2640                         }
2641
2642                         qt_handle->tmp_list = tmp_list;
2643
2644                         /* overwrite the offset of the last entry */
2645                         SIVAL(entry-entry_len,0,0);
2646
2647                         data_len = 4+qt_len;
2648                         /* overwrite the params quota_data_len */
2649                         SIVAL(params,0,data_len);
2650
2651                         break;
2652
2653                 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2654
2655                         /* unknown 4 bytes IVAL(pdata,0) */
2656
2657                         if (data_count < 8) {
2658                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2659                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2660                                 return;
2661                         }
2662
2663                         sid_len = IVAL(pdata,4);
2664                         /* Ensure this is less than 1mb. */
2665                         if (sid_len > (1024*1024)) {
2666                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2667                                 return;
2668                         }
2669
2670                         if (data_count < 8+sid_len) {
2671                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2672                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2673                                 return;
2674                         }
2675
2676                         data_len = 4+40+sid_len;
2677
2678                         if (max_data_count < data_len) {
2679                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2680                                         max_data_count, data_len));
2681                                 param_len = 4;
2682                                 SIVAL(params,0,data_len);
2683                                 data_len = 0;
2684                                 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2685                                 break;
2686                         }
2687
2688                         if (!sid_parse(pdata+8,sid_len,&sid)) {
2689                                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2690                                 return;
2691                         }
2692
2693                         if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2694                                 ZERO_STRUCT(qt);
2695                                 /*
2696                                  * we have to return zero's in all fields
2697                                  * instead of returning an error here
2698                                  * --metze
2699                                  */
2700                         }
2701
2702                         /* Realloc the size of parameters and data we will return */
2703                         param_len = 4;
2704                         params = nttrans_realloc(ppparams, param_len);
2705                         if(params == NULL) {
2706                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2707                                 return;
2708                         }
2709
2710                         pdata = nttrans_realloc(ppdata, data_len);
2711                         if(pdata == NULL) {
2712                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2713                                 return;
2714                         }
2715
2716                         entry = pdata;
2717
2718                         /* set params Size of returned Quota Data 4 bytes*/
2719                         SIVAL(params,0,data_len);
2720
2721                         /* nextoffset entry 4 bytes */
2722                         SIVAL(entry,0,0);
2723
2724                         /* then the len of the SID 4 bytes */
2725                         SIVAL(entry,4,sid_len);
2726
2727                         /* unknown data 8 bytes uint64_t */
2728                         SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2729
2730                         /* the used disk space 8 bytes uint64_t */
2731                         SBIG_UINT(entry,16,qt.usedspace);
2732
2733                         /* the soft quotas 8 bytes uint64_t */
2734                         SBIG_UINT(entry,24,qt.softlim);
2735
2736                         /* the hard quotas 8 bytes uint64_t */
2737                         SBIG_UINT(entry,32,qt.hardlim);
2738
2739                         /* and now the SID */
2740                         sid_linearize(entry+40, sid_len, &sid);
2741
2742                         break;
2743
2744                 default:
2745                         DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2746                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2747                         return;
2748                         break;
2749         }
2750
2751         send_nt_replies(conn, req, nt_status, params, param_len,
2752                         pdata, data_len);
2753 }
2754
2755 /****************************************************************************
2756  Reply to set user quota
2757 ****************************************************************************/
2758
2759 static void call_nt_transact_set_user_quota(connection_struct *conn,
2760                                             struct smb_request *req,
2761                                             uint16 **ppsetup,
2762                                             uint32 setup_count,
2763                                             char **ppparams,
2764                                             uint32 parameter_count,
2765                                             char **ppdata,
2766                                             uint32 data_count,
2767                                             uint32 max_data_count)
2768 {
2769         char *params = *ppparams;
2770         char *pdata = *ppdata;
2771         int data_len=0,param_len=0;
2772         SMB_NTQUOTA_STRUCT qt;
2773         size_t sid_len;
2774         struct dom_sid sid;
2775         files_struct *fsp = NULL;
2776
2777         ZERO_STRUCT(qt);
2778
2779         /* access check */
2780         if (get_current_uid(conn) != 0) {
2781                 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2782                          "[%s]\n", lp_servicename(SNUM(conn)),
2783                          conn->session_info->unix_name));
2784                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2785                 return;
2786         }
2787
2788         /*
2789          * Ensure minimum number of parameters sent.
2790          */
2791
2792         if (parameter_count < 2) {
2793                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2794                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2795                 return;
2796         }
2797
2798         /* maybe we can check the quota_fnum */
2799         fsp = file_fsp(req, SVAL(params,0));
2800         if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2801                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2802                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2803                 return;
2804         }
2805
2806         if (data_count < 40) {
2807                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2808                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2809                 return;
2810         }
2811
2812         /* offset to next quota record.
2813          * 4 bytes IVAL(pdata,0)
2814          * unused here...
2815          */
2816
2817         /* sid len */
2818         sid_len = IVAL(pdata,4);
2819
2820         if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2821                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2822                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2823                 return;
2824         }
2825
2826         /* unknown 8 bytes in pdata
2827          * maybe its the change time in NTTIME
2828          */
2829
2830         /* the used space 8 bytes (uint64_t)*/
2831         qt.usedspace = BVAL(pdata,16);
2832
2833         /* the soft quotas 8 bytes (uint64_t)*/
2834         qt.softlim = BVAL(pdata,24);
2835
2836         /* the hard quotas 8 bytes (uint64_t)*/
2837         qt.hardlim = BVAL(pdata,32);
2838
2839         if (!sid_parse(pdata+40,sid_len,&sid)) {
2840                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2841                 return;
2842         }
2843
2844         DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2845
2846         /* 44 unknown bytes left... */
2847
2848         if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2849                 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2850                 return;
2851         }
2852
2853         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2854                         pdata, data_len);
2855 }
2856 #endif /* HAVE_SYS_QUOTAS */
2857
2858 static void handle_nttrans(connection_struct *conn,
2859                            struct trans_state *state,
2860                            struct smb_request *req)
2861 {
2862         if (get_Protocol() >= PROTOCOL_NT1) {
2863                 req->flags2 |= 0x40; /* IS_LONG_NAME */
2864                 SSVAL(req->inbuf,smb_flg2,req->flags2);
2865         }
2866
2867
2868         SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2869
2870         /* Now we must call the relevant NT_TRANS function */
2871         switch(state->call) {
2872                 case NT_TRANSACT_CREATE:
2873                 {
2874                         START_PROFILE(NT_transact_create);
2875                         call_nt_transact_create(
2876                                 conn, req,
2877                                 &state->setup, state->setup_count,
2878                                 &state->param, state->total_param,
2879                                 &state->data, state->total_data,
2880                                 state->max_data_return);
2881                         END_PROFILE(NT_transact_create);
2882                         break;
2883                 }
2884
2885                 case NT_TRANSACT_IOCTL:
2886                 {
2887                         START_PROFILE(NT_transact_ioctl);
2888                         call_nt_transact_ioctl(
2889                                 conn, req,
2890                                 &state->setup, state->setup_count,
2891                                 &state->param, state->total_param,
2892                                 &state->data, state->total_data,
2893                                 state->max_data_return);
2894                         END_PROFILE(NT_transact_ioctl);
2895                         break;
2896                 }
2897
2898                 case NT_TRANSACT_SET_SECURITY_DESC:
2899                 {
2900                         START_PROFILE(NT_transact_set_security_desc);
2901                         call_nt_transact_set_security_desc(
2902                                 conn, req,
2903                                 &state->setup, state->setup_count,
2904                                 &state->param, state->total_param,
2905                                 &state->data, state->total_data,
2906                                 state->max_data_return);
2907                         END_PROFILE(NT_transact_set_security_desc);
2908                         break;
2909                 }
2910
2911                 case NT_TRANSACT_NOTIFY_CHANGE:
2912                 {
2913                         START_PROFILE(NT_transact_notify_change);
2914                         call_nt_transact_notify_change(
2915                                 conn, req,
2916                                 &state->setup, state->setup_count,
2917                                 &state->param, state->total_param,
2918                                 &state->data, state->total_data,
2919                                 state->max_data_return,
2920                                 state->max_param_return);
2921                         END_PROFILE(NT_transact_notify_change);
2922                         break;
2923                 }
2924
2925                 case NT_TRANSACT_RENAME:
2926                 {
2927                         START_PROFILE(NT_transact_rename);
2928                         call_nt_transact_rename(
2929                                 conn, req,
2930                                 &state->setup, state->setup_count,
2931                                 &state->param, state->total_param,
2932                                 &state->data, state->total_data,
2933                                 state->max_data_return);
2934                         END_PROFILE(NT_transact_rename);
2935                         break;
2936                 }
2937
2938                 case NT_TRANSACT_QUERY_SECURITY_DESC:
2939                 {
2940                         START_PROFILE(NT_transact_query_security_desc);
2941                         call_nt_transact_query_security_desc(
2942                                 conn, req,
2943                                 &state->setup, state->setup_count,
2944                                 &state->param, state->total_param,
2945                                 &state->data, state->total_data,
2946                                 state->max_data_return);
2947                         END_PROFILE(NT_transact_query_security_desc);
2948                         break;
2949                 }
2950
2951 #ifdef HAVE_SYS_QUOTAS
2952                 case NT_TRANSACT_GET_USER_QUOTA:
2953                 {
2954                         START_PROFILE(NT_transact_get_user_quota);
2955                         call_nt_transact_get_user_quota(
2956                                 conn, req,
2957                                 &state->setup, state->setup_count,
2958                                 &state->param, state->total_param,
2959                                 &state->data, state->total_data,
2960                                 state->max_data_return);
2961                         END_PROFILE(NT_transact_get_user_quota);
2962                         break;
2963                 }
2964
2965                 case NT_TRANSACT_SET_USER_QUOTA:
2966                 {
2967                         START_PROFILE(NT_transact_set_user_quota);
2968                         call_nt_transact_set_user_quota(
2969                                 conn, req,
2970                                 &state->setup, state->setup_count,
2971                                 &state->param, state->total_param,
2972                                 &state->data, state->total_data,
2973                                 state->max_data_return);
2974                         END_PROFILE(NT_transact_set_user_quota);
2975                         break;
2976                 }
2977 #endif /* HAVE_SYS_QUOTAS */
2978
2979                 default:
2980                         /* Error in request */
2981                         DEBUG(0,("handle_nttrans: Unknown request %d in "
2982                                  "nttrans call\n", state->call));
2983                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2984                         return;
2985         }
2986         return;
2987 }
2988
2989 /****************************************************************************
2990  Reply to a SMBNTtrans.
2991 ****************************************************************************/
2992
2993 void reply_nttrans(struct smb_request *req)
2994 {
2995         connection_struct *conn = req->conn;
2996         uint32_t pscnt;
2997         uint32_t psoff;
2998         uint32_t dscnt;
2999         uint32_t dsoff;
3000         uint16 function_code;
3001         NTSTATUS result;
3002         struct trans_state *state;
3003
3004         START_PROFILE(SMBnttrans);
3005
3006         if (req->wct < 19) {
3007                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3008                 END_PROFILE(SMBnttrans);
3009                 return;
3010         }
3011
3012         pscnt = IVAL(req->vwv+9, 1);
3013         psoff = IVAL(req->vwv+11, 1);
3014         dscnt = IVAL(req->vwv+13, 1);
3015         dsoff = IVAL(req->vwv+15, 1);
3016         function_code = SVAL(req->vwv+18, 0);
3017
3018         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3019                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3020                 END_PROFILE(SMBnttrans);
3021                 return;
3022         }
3023
3024         result = allow_new_trans(conn->pending_trans, req->mid);
3025         if (!NT_STATUS_IS_OK(result)) {
3026                 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3027                 reply_nterror(req, result);
3028                 END_PROFILE(SMBnttrans);
3029                 return;
3030         }
3031
3032         if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
3033                 reply_nterror(req, NT_STATUS_NO_MEMORY);
3034                 END_PROFILE(SMBnttrans);
3035                 return;
3036         }
3037
3038         state->cmd = SMBnttrans;
3039
3040         state->mid = req->mid;
3041         state->vuid = req->vuid;
3042         state->total_data = IVAL(req->vwv+3, 1);
3043         state->data = NULL;
3044         state->total_param = IVAL(req->vwv+1, 1);
3045         state->param = NULL;
3046         state->max_data_return = IVAL(req->vwv+7, 1);
3047         state->max_param_return = IVAL(req->vwv+5, 1);
3048
3049         /* setup count is in *words* */
3050         state->setup_count = 2*CVAL(req->vwv+17, 1);
3051         state->setup = NULL;
3052         state->call = function_code;
3053
3054         DEBUG(10, ("num_setup=%u, "
3055                    "param_total=%u, this_param=%u, max_param=%u, "
3056                    "data_total=%u, this_data=%u, max_data=%u, "
3057                    "param_offset=%u, data_offset=%u\n",
3058                    (unsigned)state->setup_count,
3059                    (unsigned)state->total_param, (unsigned)pscnt,
3060                    (unsigned)state->max_param_return,
3061                    (unsigned)state->total_data, (unsigned)dscnt,
3062                    (unsigned)state->max_data_return,
3063                    (unsigned)psoff, (unsigned)dsoff));
3064
3065         /*
3066          * All nttrans messages we handle have smb_wct == 19 +
3067          * state->setup_count.  Ensure this is so as a sanity check.
3068          */
3069
3070         if(req->wct != 19 + (state->setup_count/2)) {
3071                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3072                          req->wct, 19 + (state->setup_count/2)));
3073                 goto bad_param;
3074         }
3075
3076         /* Don't allow more than 128mb for each value. */
3077         if ((state->total_data > (1024*1024*128)) ||
3078             (state->total_param > (1024*1024*128))) {
3079                 reply_nterror(req, NT_STATUS_NO_MEMORY);
3080                 END_PROFILE(SMBnttrans);
3081                 return;
3082         }
3083
3084         if ((dscnt > state->total_data) || (pscnt > state->total_param))
3085                 goto bad_param;
3086
3087         if (state->total_data)  {
3088
3089                 if (trans_oob(state->total_data, 0, dscnt)
3090                     || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
3091                         goto bad_param;
3092                 }
3093
3094                 /* Can't use talloc here, the core routines do realloc on the
3095                  * params and data. */
3096                 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3097                         DEBUG(0,("reply_nttrans: data malloc fail for %u "
3098                                  "bytes !\n", (unsigned int)state->total_data));
3099                         TALLOC_FREE(state);
3100                         reply_nterror(req, NT_STATUS_NO_MEMORY);
3101                         END_PROFILE(SMBnttrans);
3102                         return;
3103                 }
3104
3105                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3106         }
3107
3108         if (state->total_param) {
3109
3110                 if (trans_oob(state->total_param, 0, pscnt)
3111                     || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
3112                         goto bad_param;
3113                 }
3114
3115                 /* Can't use talloc here, the core routines do realloc on the
3116                  * params and data. */
3117                 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3118                         DEBUG(0,("reply_nttrans: param malloc fail for %u "
3119                                  "bytes !\n", (unsigned int)state->total_param));
3120                         SAFE_FREE(state->data);
3121                         TALLOC_FREE(state);
3122                         reply_nterror(req, NT_STATUS_NO_MEMORY);
3123                         END_PROFILE(SMBnttrans);
3124                         return;
3125                 }
3126
3127                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3128         }
3129
3130         state->received_data  = dscnt;
3131         state->received_param = pscnt;
3132
3133         if(state->setup_count > 0) {
3134                 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3135                           state->setup_count));
3136
3137                 /*
3138                  * No overflow possible here, state->setup_count is an
3139                  * unsigned int, being filled by a single byte from
3140                  * CVAL(req->vwv+13, 0) above. The cast in the comparison
3141                  * below is not necessary, it's here to clarify things. The
3142                  * validity of req->vwv and req->wct has been checked in
3143                  * init_smb_request already.
3144                  */
3145                 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
3146                         goto bad_param;
3147                 }
3148
3149                 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3150                 if (state->setup == NULL) {
3151                         DEBUG(0,("reply_nttrans : Out of memory\n"));
3152                         SAFE_FREE(state->data);
3153                         SAFE_FREE(state->param);
3154                         TALLOC_FREE(state);
3155                         reply_nterror(req, NT_STATUS_NO_MEMORY);
3156                         END_PROFILE(SMBnttrans);
3157                         return;
3158                 }
3159
3160                 memcpy(state->setup, req->vwv+19, state->setup_count);
3161                 dump_data(10, (uint8 *)state->setup, state->setup_count);
3162         }
3163
3164         if ((state->received_data == state->total_data) &&
3165             (state->received_param == state->total_param)) {
3166                 handle_nttrans(conn, state, req);
3167                 SAFE_FREE(state->param);
3168                 SAFE_FREE(state->data);
3169                 TALLOC_FREE(state);
3170                 END_PROFILE(SMBnttrans);
3171                 return;
3172         }
3173
3174         DLIST_ADD(conn->pending_trans, state);
3175
3176         /* We need to send an interim response then receive the rest
3177            of the parameter/data bytes */
3178         reply_outbuf(req, 0, 0);
3179         show_msg((char *)req->outbuf);
3180         END_PROFILE(SMBnttrans);
3181         return;
3182
3183   bad_param:
3184
3185         DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3186         SAFE_FREE(state->data);
3187         SAFE_FREE(state->param);
3188         TALLOC_FREE(state);
3189         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3190         END_PROFILE(SMBnttrans);
3191         return;
3192 }
3193
3194 /****************************************************************************
3195  Reply to a SMBnttranss
3196  ****************************************************************************/
3197
3198 void reply_nttranss(struct smb_request *req)
3199 {
3200         connection_struct *conn = req->conn;
3201         uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3202         struct trans_state *state;
3203
3204         START_PROFILE(SMBnttranss);
3205
3206         show_msg((char *)req->inbuf);
3207
3208         if (req->wct < 18) {
3209                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3210                 END_PROFILE(SMBnttranss);
3211                 return;
3212         }
3213
3214         for (state = conn->pending_trans; state != NULL;
3215              state = state->next) {
3216                 if (state->mid == req->mid) {
3217                         break;
3218                 }
3219         }
3220
3221         if ((state == NULL) || (state->cmd != SMBnttrans)) {
3222                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3223                 END_PROFILE(SMBnttranss);
3224                 return;
3225         }
3226
3227         /* Revise state->total_param and state->total_data in case they have
3228            changed downwards */
3229         if (IVAL(req->vwv+1, 1) < state->total_param) {
3230                 state->total_param = IVAL(req->vwv+1, 1);
3231         }
3232         if (IVAL(req->vwv+3, 1) < state->total_data) {
3233                 state->total_data = IVAL(req->vwv+3, 1);
3234         }
3235
3236         pcnt = IVAL(req->vwv+5, 1);
3237         poff = IVAL(req->vwv+7, 1);
3238         pdisp = IVAL(req->vwv+9, 1);
3239
3240         dcnt = IVAL(req->vwv+11, 1);
3241         doff = IVAL(req->vwv+13, 1);
3242         ddisp = IVAL(req->vwv+15, 1);
3243
3244         state->received_param += pcnt;
3245         state->received_data += dcnt;
3246
3247         if ((state->received_data > state->total_data) ||
3248             (state->received_param > state->total_param))
3249                 goto bad_param;
3250
3251         if (pcnt) {
3252                 if (trans_oob(state->total_param, pdisp, pcnt)
3253                     || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3254                         goto bad_param;
3255                 }
3256                 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3257         }
3258
3259         if (dcnt) {
3260                 if (trans_oob(state->total_data, ddisp, dcnt)
3261                     || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3262                         goto bad_param;
3263                 }
3264                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3265         }
3266
3267         if ((state->received_param < state->total_param) ||
3268             (state->received_data < state->total_data)) {
3269                 END_PROFILE(SMBnttranss);
3270                 return;
3271         }
3272
3273         handle_nttrans(conn, state, req);
3274
3275         DLIST_REMOVE(conn->pending_trans, state);
3276         SAFE_FREE(state->data);
3277         SAFE_FREE(state->param);
3278         TALLOC_FREE(state);
3279         END_PROFILE(SMBnttranss);
3280         return;
3281
3282   bad_param:
3283
3284         DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3285         DLIST_REMOVE(conn->pending_trans, state);
3286         SAFE_FREE(state->data);
3287         SAFE_FREE(state->param);
3288         TALLOC_FREE(state);
3289         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3290         END_PROFILE(SMBnttranss);
3291         return;
3292 }