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