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