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