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