s3: smbd - Prevent file truncation on an open that fails with share mode violation.
[Samba.git] / source3 / smbd / nttrans.c
blob5a0ac39e91e69c43962b3ef0bc7222815115ae67
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
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.
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.
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/>.
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 #include "lib/util_ea.h"
34 extern const struct generic_mapping file_generic_mapping;
36 static char *nttrans_realloc(char **ptr, size_t size)
38 if (ptr==NULL) {
39 smb_panic("nttrans_realloc() called with NULL ptr");
42 *ptr = (char *)SMB_REALLOC(*ptr, size);
43 if(*ptr == NULL) {
44 return NULL;
46 memset(*ptr,'\0',size);
47 return *ptr;
50 /****************************************************************************
51 Send the required number of replies back.
52 We assume all fields other than the data fields are
53 set correctly for the type of call.
54 HACK ! Always assumes smb_setup field is zero.
55 ****************************************************************************/
57 static void send_nt_replies(connection_struct *conn,
58 struct smb_request *req, NTSTATUS nt_error,
59 char *params, int paramsize,
60 char *pdata, int datasize)
62 int data_to_send = datasize;
63 int params_to_send = paramsize;
64 int useable_space;
65 char *pp = params;
66 char *pd = pdata;
67 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
68 int alignment_offset = 1;
69 int data_alignment_offset = 0;
70 struct smbd_server_connection *sconn = req->sconn;
71 int max_send = sconn->smb1.sessions.max_send;
74 * If there genuinely are no parameters or data to send just send
75 * the empty packet.
78 if(params_to_send == 0 && data_to_send == 0) {
79 reply_outbuf(req, 18, 0);
80 if (NT_STATUS_V(nt_error)) {
81 error_packet_set((char *)req->outbuf,
82 0, 0, nt_error,
83 __LINE__,__FILE__);
85 show_msg((char *)req->outbuf);
86 if (!srv_send_smb(sconn,
87 (char *)req->outbuf,
88 true, req->seqnum+1,
89 IS_CONN_ENCRYPTED(conn),
90 &req->pcd)) {
91 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
93 TALLOC_FREE(req->outbuf);
94 return;
98 * When sending params and data ensure that both are nicely aligned.
99 * Only do this alignment when there is also data to send - else
100 * can cause NT redirector problems.
103 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
104 data_alignment_offset = 4 - (params_to_send % 4);
108 * Space is bufsize minus Netbios over TCP header minus SMB header.
109 * The alignment_offset is to align the param bytes on a four byte
110 * boundary (2 bytes for data len, one byte pad).
111 * NT needs this to work correctly.
114 useable_space = max_send - (smb_size
115 + 2 * 18 /* wct */
116 + alignment_offset
117 + data_alignment_offset);
119 if (useable_space < 0) {
120 char *msg = talloc_asprintf(
121 talloc_tos(),
122 "send_nt_replies failed sanity useable_space = %d!!!",
123 useable_space);
124 DEBUG(0, ("%s\n", msg));
125 exit_server_cleanly(msg);
128 while (params_to_send || data_to_send) {
131 * Calculate whether we will totally or partially fill this packet.
134 total_sent_thistime = params_to_send + data_to_send;
137 * We can never send more than useable_space.
140 total_sent_thistime = MIN(total_sent_thistime, useable_space);
142 reply_outbuf(req, 18,
143 total_sent_thistime + alignment_offset
144 + data_alignment_offset);
147 * Set total params and data to be sent.
150 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
151 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
154 * Calculate how many parameters and data we can fit into
155 * this packet. Parameters get precedence.
158 params_sent_thistime = MIN(params_to_send,useable_space);
159 data_sent_thistime = useable_space - params_sent_thistime;
160 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
162 SIVAL(req->outbuf, smb_ntr_ParameterCount,
163 params_sent_thistime);
165 if(params_sent_thistime == 0) {
166 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
167 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
168 } else {
170 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
171 * parameter bytes, however the first 4 bytes of outbuf are
172 * the Netbios over TCP header. Thus use smb_base() to subtract
173 * them from the calculation.
176 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
177 ((smb_buf(req->outbuf)+alignment_offset)
178 - smb_base(req->outbuf)));
180 * Absolute displacement of param bytes sent in this packet.
183 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
184 pp - params);
188 * Deal with the data portion.
191 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
193 if(data_sent_thistime == 0) {
194 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
195 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
196 } else {
198 * The offset of the data bytes is the offset of the
199 * parameter bytes plus the number of parameters being sent this time.
202 SIVAL(req->outbuf, smb_ntr_DataOffset,
203 ((smb_buf(req->outbuf)+alignment_offset) -
204 smb_base(req->outbuf))
205 + params_sent_thistime + data_alignment_offset);
206 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
210 * Copy the param bytes into the packet.
213 if(params_sent_thistime) {
214 if (alignment_offset != 0) {
215 memset(smb_buf(req->outbuf), 0,
216 alignment_offset);
218 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
219 params_sent_thistime);
223 * Copy in the data bytes
226 if(data_sent_thistime) {
227 if (data_alignment_offset != 0) {
228 memset((smb_buf(req->outbuf)+alignment_offset+
229 params_sent_thistime), 0,
230 data_alignment_offset);
232 memcpy(smb_buf(req->outbuf)+alignment_offset
233 +params_sent_thistime+data_alignment_offset,
234 pd,data_sent_thistime);
237 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
238 params_sent_thistime, data_sent_thistime, useable_space));
239 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
240 params_to_send, data_to_send, paramsize, datasize));
242 if (NT_STATUS_V(nt_error)) {
243 error_packet_set((char *)req->outbuf,
244 0, 0, nt_error,
245 __LINE__,__FILE__);
248 /* Send the packet */
249 show_msg((char *)req->outbuf);
250 if (!srv_send_smb(sconn,
251 (char *)req->outbuf,
252 true, req->seqnum+1,
253 IS_CONN_ENCRYPTED(conn),
254 &req->pcd)) {
255 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
258 TALLOC_FREE(req->outbuf);
260 pp += params_sent_thistime;
261 pd += data_sent_thistime;
263 params_to_send -= params_sent_thistime;
264 data_to_send -= data_sent_thistime;
267 * Sanity check
270 if(params_to_send < 0 || data_to_send < 0) {
271 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
272 params_to_send, data_to_send));
273 exit_server_cleanly("send_nt_replies: internal error");
278 /****************************************************************************
279 Reply to an NT create and X call on a pipe
280 ****************************************************************************/
282 static void nt_open_pipe(char *fname, connection_struct *conn,
283 struct smb_request *req, uint16_t *ppnum)
285 files_struct *fsp;
286 NTSTATUS status;
288 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
290 /* Strip \\ off the name if present. */
291 while (fname[0] == '\\') {
292 fname++;
295 status = open_np_file(req, fname, &fsp);
296 if (!NT_STATUS_IS_OK(status)) {
297 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
298 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
299 ERRDOS, ERRbadpipe);
300 return;
302 reply_nterror(req, status);
303 return;
306 *ppnum = fsp->fnum;
307 return;
310 /****************************************************************************
311 Reply to an NT create and X call for pipes.
312 ****************************************************************************/
314 static void do_ntcreate_pipe_open(connection_struct *conn,
315 struct smb_request *req)
317 char *fname = NULL;
318 uint16_t pnum = FNUM_FIELD_INVALID;
319 char *p = NULL;
320 uint32 flags = IVAL(req->vwv+3, 1);
321 TALLOC_CTX *ctx = talloc_tos();
323 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
325 if (!fname) {
326 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
327 ERRDOS, ERRbadpipe);
328 return;
330 nt_open_pipe(fname, conn, req, &pnum);
332 if (req->outbuf) {
333 /* error reply */
334 return;
338 * Deal with pipe return.
341 if (flags & EXTENDED_RESPONSE_REQUIRED) {
342 /* This is very strange. We
343 * return 50 words, but only set
344 * the wcnt to 42 ? It's definitely
345 * what happens on the wire....
347 reply_outbuf(req, 50, 0);
348 SCVAL(req->outbuf,smb_wct,42);
349 } else {
350 reply_outbuf(req, 34, 0);
353 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
354 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
356 p = (char *)req->outbuf + smb_vwv2;
357 p++;
358 SSVAL(p,0,pnum);
359 p += 2;
360 SIVAL(p,0,FILE_WAS_OPENED);
361 p += 4;
362 p += 32;
363 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
364 p += 20;
365 /* File type. */
366 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
367 /* Device state. */
368 SSVAL(p,2, 0x5FF); /* ? */
369 p += 4;
371 if (flags & EXTENDED_RESPONSE_REQUIRED) {
372 p += 25;
373 SIVAL(p,0,FILE_GENERIC_ALL);
375 * For pipes W2K3 seems to return
376 * 0x12019B next.
377 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
379 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
382 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
385 struct case_semantics_state {
386 connection_struct *conn;
387 bool case_sensitive;
388 bool case_preserve;
389 bool short_case_preserve;
392 /****************************************************************************
393 Restore case semantics.
394 ****************************************************************************/
396 static int restore_case_semantics(struct case_semantics_state *state)
398 state->conn->case_sensitive = state->case_sensitive;
399 state->conn->case_preserve = state->case_preserve;
400 state->conn->short_case_preserve = state->short_case_preserve;
401 return 0;
404 /****************************************************************************
405 Save case semantics.
406 ****************************************************************************/
408 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
409 connection_struct *conn)
411 struct case_semantics_state *result;
413 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
414 return NULL;
417 result->conn = conn;
418 result->case_sensitive = conn->case_sensitive;
419 result->case_preserve = conn->case_preserve;
420 result->short_case_preserve = conn->short_case_preserve;
422 /* Set to POSIX. */
423 conn->case_sensitive = True;
424 conn->case_preserve = True;
425 conn->short_case_preserve = True;
427 talloc_set_destructor(result, restore_case_semantics);
429 return result;
432 /****************************************************************************
433 Reply to an NT create and X call.
434 ****************************************************************************/
436 void reply_ntcreate_and_X(struct smb_request *req)
438 connection_struct *conn = req->conn;
439 struct smb_filename *smb_fname = NULL;
440 char *fname = NULL;
441 uint32 flags;
442 uint32 access_mask;
443 uint32 file_attributes;
444 uint32 share_access;
445 uint32 create_disposition;
446 uint32 create_options;
447 uint16 root_dir_fid;
448 uint64_t allocation_size;
449 /* Breakout the oplock request bits so we can set the
450 reply bits separately. */
451 uint32 fattr=0;
452 off_t file_len = 0;
453 int info = 0;
454 files_struct *fsp = NULL;
455 char *p = NULL;
456 struct timespec create_timespec;
457 struct timespec c_timespec;
458 struct timespec a_timespec;
459 struct timespec m_timespec;
460 struct timespec write_time_ts;
461 NTSTATUS status;
462 int oplock_request;
463 uint8_t oplock_granted = NO_OPLOCK_RETURN;
464 struct case_semantics_state *case_state = NULL;
465 TALLOC_CTX *ctx = talloc_tos();
467 START_PROFILE(SMBntcreateX);
469 if (req->wct < 24) {
470 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
471 goto out;
474 flags = IVAL(req->vwv+3, 1);
475 access_mask = IVAL(req->vwv+7, 1);
476 file_attributes = IVAL(req->vwv+13, 1);
477 share_access = IVAL(req->vwv+15, 1);
478 create_disposition = IVAL(req->vwv+17, 1);
479 create_options = IVAL(req->vwv+19, 1);
480 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
482 allocation_size = BVAL(req->vwv+9, 1);
484 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
485 STR_TERMINATE, &status);
487 if (!NT_STATUS_IS_OK(status)) {
488 reply_nterror(req, status);
489 goto out;
492 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
493 "file_attributes = 0x%x, share_access = 0x%x, "
494 "create_disposition = 0x%x create_options = 0x%x "
495 "root_dir_fid = 0x%x, fname = %s\n",
496 (unsigned int)flags,
497 (unsigned int)access_mask,
498 (unsigned int)file_attributes,
499 (unsigned int)share_access,
500 (unsigned int)create_disposition,
501 (unsigned int)create_options,
502 (unsigned int)root_dir_fid,
503 fname));
506 * we need to remove ignored bits when they come directly from the client
507 * because we reuse some of them for internal stuff
509 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
512 * If it's an IPC, use the pipe handler.
515 if (IS_IPC(conn)) {
516 if (lp_nt_pipe_support()) {
517 do_ntcreate_pipe_open(conn, req);
518 goto out;
520 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
521 goto out;
524 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
525 if (oplock_request) {
526 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
527 ? BATCH_OPLOCK : 0;
530 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
531 case_state = set_posix_case_semantics(ctx, conn);
532 if (!case_state) {
533 reply_nterror(req, NT_STATUS_NO_MEMORY);
534 goto out;
538 status = filename_convert(ctx,
539 conn,
540 req->flags2 & FLAGS2_DFS_PATHNAMES,
541 fname,
542 UCF_PREP_CREATEFILE,
543 NULL,
544 &smb_fname);
546 TALLOC_FREE(case_state);
548 if (!NT_STATUS_IS_OK(status)) {
549 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
550 reply_botherror(req,
551 NT_STATUS_PATH_NOT_COVERED,
552 ERRSRV, ERRbadpath);
553 goto out;
555 reply_nterror(req, status);
556 goto out;
560 * Bug #6898 - clients using Windows opens should
561 * never be able to set this attribute into the
562 * VFS.
564 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
566 status = SMB_VFS_CREATE_FILE(
567 conn, /* conn */
568 req, /* req */
569 root_dir_fid, /* root_dir_fid */
570 smb_fname, /* fname */
571 access_mask, /* access_mask */
572 share_access, /* share_access */
573 create_disposition, /* create_disposition*/
574 create_options, /* create_options */
575 file_attributes, /* file_attributes */
576 oplock_request, /* oplock_request */
577 allocation_size, /* allocation_size */
578 0, /* private_flags */
579 NULL, /* sd */
580 NULL, /* ea_list */
581 &fsp, /* result */
582 &info); /* pinfo */
584 if (!NT_STATUS_IS_OK(status)) {
585 if (open_was_deferred(req->sconn, req->mid)) {
586 /* We have re-scheduled this call, no error. */
587 goto out;
589 reply_openerror(req, status);
590 goto out;
593 /* Ensure we're pointing at the correct stat struct. */
594 TALLOC_FREE(smb_fname);
595 smb_fname = fsp->fsp_name;
598 * If the caller set the extended oplock request bit
599 * and we granted one (by whatever means) - set the
600 * correct bit for extended oplock reply.
603 if (oplock_request &&
604 (lp_fake_oplocks(SNUM(conn))
605 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
608 * Exclusive oplock granted
611 if (flags & REQUEST_BATCH_OPLOCK) {
612 oplock_granted = BATCH_OPLOCK_RETURN;
613 } else {
614 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
616 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
617 oplock_granted = LEVEL_II_OPLOCK_RETURN;
618 } else {
619 oplock_granted = NO_OPLOCK_RETURN;
622 file_len = smb_fname->st.st_ex_size;
624 if (flags & EXTENDED_RESPONSE_REQUIRED) {
625 /* This is very strange. We
626 * return 50 words, but only set
627 * the wcnt to 42 ? It's definitely
628 * what happens on the wire....
630 reply_outbuf(req, 50, 0);
631 SCVAL(req->outbuf,smb_wct,42);
632 } else {
633 reply_outbuf(req, 34, 0);
636 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
637 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
639 p = (char *)req->outbuf + smb_vwv2;
641 SCVAL(p, 0, oplock_granted);
643 p++;
644 SSVAL(p,0,fsp->fnum);
645 p += 2;
646 if ((create_disposition == FILE_SUPERSEDE)
647 && (info == FILE_WAS_OVERWRITTEN)) {
648 SIVAL(p,0,FILE_WAS_SUPERSEDED);
649 } else {
650 SIVAL(p,0,info);
652 p += 4;
654 fattr = dos_mode(conn, smb_fname);
655 if (fattr == 0) {
656 fattr = FILE_ATTRIBUTE_NORMAL;
659 /* Deal with other possible opens having a modified
660 write time. JRA. */
661 ZERO_STRUCT(write_time_ts);
662 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
663 if (!null_timespec(write_time_ts)) {
664 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
667 /* Create time. */
668 create_timespec = get_create_timespec(conn, fsp, smb_fname);
669 a_timespec = smb_fname->st.st_ex_atime;
670 m_timespec = smb_fname->st.st_ex_mtime;
671 c_timespec = get_change_timespec(conn, fsp, smb_fname);
673 if (lp_dos_filetime_resolution(SNUM(conn))) {
674 dos_filetime_timespec(&create_timespec);
675 dos_filetime_timespec(&a_timespec);
676 dos_filetime_timespec(&m_timespec);
677 dos_filetime_timespec(&c_timespec);
680 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
681 p += 8;
682 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
683 p += 8;
684 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
685 p += 8;
686 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
687 p += 8;
688 SIVAL(p,0,fattr); /* File Attributes. */
689 p += 4;
690 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
691 p += 8;
692 SOFF_T(p,0,file_len);
693 p += 8;
694 if (flags & EXTENDED_RESPONSE_REQUIRED) {
695 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
696 size_t num_names = 0;
697 unsigned int num_streams = 0;
698 struct stream_struct *streams = NULL;
700 /* Do we have any EA's ? */
701 status = get_ea_names_from_file(ctx, conn, fsp,
702 smb_fname->base_name, NULL, &num_names);
703 if (NT_STATUS_IS_OK(status) && num_names) {
704 file_status &= ~NO_EAS;
706 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
707 &num_streams, &streams);
708 /* There is always one stream, ::$DATA. */
709 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
710 file_status &= ~NO_SUBSTREAMS;
712 TALLOC_FREE(streams);
713 SSVAL(p,2,file_status);
715 p += 4;
716 SCVAL(p,0,fsp->is_directory ? 1 : 0);
718 if (flags & EXTENDED_RESPONSE_REQUIRED) {
719 uint32 perms = 0;
720 p += 25;
721 if (fsp->is_directory ||
722 fsp->can_write ||
723 can_write_to_file(conn, smb_fname)) {
724 perms = FILE_GENERIC_ALL;
725 } else {
726 perms = FILE_GENERIC_READ|FILE_EXECUTE;
728 SIVAL(p,0,perms);
731 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
732 fsp_fnum_dbg(fsp), smb_fname_str_dbg(smb_fname)));
734 out:
735 END_PROFILE(SMBntcreateX);
736 return;
739 /****************************************************************************
740 Reply to a NT_TRANSACT_CREATE call to open a pipe.
741 ****************************************************************************/
743 static void do_nt_transact_create_pipe(connection_struct *conn,
744 struct smb_request *req,
745 uint16 **ppsetup, uint32 setup_count,
746 char **ppparams, uint32 parameter_count,
747 char **ppdata, uint32 data_count)
749 char *fname = NULL;
750 char *params = *ppparams;
751 uint16_t pnum = FNUM_FIELD_INVALID;
752 char *p = NULL;
753 NTSTATUS status;
754 size_t param_len;
755 uint32 flags;
756 TALLOC_CTX *ctx = talloc_tos();
759 * Ensure minimum number of parameters sent.
762 if(parameter_count < 54) {
763 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
764 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
765 return;
768 flags = IVAL(params,0);
770 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
771 parameter_count-53, STR_TERMINATE,
772 &status);
773 if (!NT_STATUS_IS_OK(status)) {
774 reply_nterror(req, status);
775 return;
778 nt_open_pipe(fname, conn, req, &pnum);
780 if (req->outbuf) {
781 /* Error return */
782 return;
785 /* Realloc the size of parameters and data we will return */
786 if (flags & EXTENDED_RESPONSE_REQUIRED) {
787 /* Extended response is 32 more byyes. */
788 param_len = 101;
789 } else {
790 param_len = 69;
792 params = nttrans_realloc(ppparams, param_len);
793 if(params == NULL) {
794 reply_nterror(req, NT_STATUS_NO_MEMORY);
795 return;
798 p = params;
799 SCVAL(p,0,NO_OPLOCK_RETURN);
801 p += 2;
802 SSVAL(p,0,pnum);
803 p += 2;
804 SIVAL(p,0,FILE_WAS_OPENED);
805 p += 8;
807 p += 32;
808 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
809 p += 20;
810 /* File type. */
811 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
812 /* Device state. */
813 SSVAL(p,2, 0x5FF); /* ? */
814 p += 4;
816 if (flags & EXTENDED_RESPONSE_REQUIRED) {
817 p += 25;
818 SIVAL(p,0,FILE_GENERIC_ALL);
820 * For pipes W2K3 seems to return
821 * 0x12019B next.
822 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
824 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
827 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
829 /* Send the required number of replies */
830 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
832 return;
835 /*********************************************************************
836 Windows seems to do canonicalization of inheritance bits. Do the
837 same.
838 *********************************************************************/
840 static void canonicalize_inheritance_bits(struct security_descriptor *psd)
842 bool set_auto_inherited = false;
845 * We need to filter out the
846 * SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ
847 * bits. If both are set we store SEC_DESC_DACL_AUTO_INHERITED
848 * as this alters whether SEC_ACE_FLAG_INHERITED_ACE is set
849 * when an ACE is inherited. Otherwise we zero these bits out.
850 * See:
852 * http://social.msdn.microsoft.com/Forums/eu/os_fileservices/thread/11f77b68-731e-407d-b1b3-064750716531
854 * for details.
857 if ((psd->type & (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ))
858 == (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ)) {
859 set_auto_inherited = true;
862 psd->type &= ~(SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ);
863 if (set_auto_inherited) {
864 psd->type |= SEC_DESC_DACL_AUTO_INHERITED;
868 /****************************************************************************
869 Internal fn to set security descriptors.
870 ****************************************************************************/
872 NTSTATUS set_sd(files_struct *fsp, struct security_descriptor *psd,
873 uint32_t security_info_sent)
875 NTSTATUS status;
877 if (!CAN_WRITE(fsp->conn)) {
878 return NT_STATUS_ACCESS_DENIED;
881 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
882 return NT_STATUS_OK;
885 if (psd->owner_sid == NULL) {
886 security_info_sent &= ~SECINFO_OWNER;
888 if (psd->group_sid == NULL) {
889 security_info_sent &= ~SECINFO_GROUP;
892 /* Ensure we have at least one thing set. */
893 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
894 /* Just like W2K3 */
895 return NT_STATUS_OK;
898 /* Ensure we have the rights to do this. */
899 if (security_info_sent & SECINFO_OWNER) {
900 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
901 return NT_STATUS_ACCESS_DENIED;
905 if (security_info_sent & SECINFO_GROUP) {
906 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
907 return NT_STATUS_ACCESS_DENIED;
911 if (security_info_sent & SECINFO_DACL) {
912 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
913 return NT_STATUS_ACCESS_DENIED;
915 /* Convert all the generic bits. */
916 if (psd->dacl) {
917 security_acl_map_generic(psd->dacl, &file_generic_mapping);
921 if (security_info_sent & SECINFO_SACL) {
922 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
923 return NT_STATUS_ACCESS_DENIED;
925 /* Convert all the generic bits. */
926 if (psd->sacl) {
927 security_acl_map_generic(psd->sacl, &file_generic_mapping);
931 canonicalize_inheritance_bits(psd);
933 if (DEBUGLEVEL >= 10) {
934 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
935 NDR_PRINT_DEBUG(security_descriptor, psd);
938 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
940 TALLOC_FREE(psd);
942 return status;
945 /****************************************************************************
946 Internal fn to set security descriptors from a data blob.
947 ****************************************************************************/
949 NTSTATUS set_sd_blob(files_struct *fsp, uint8_t *data, uint32_t sd_len,
950 uint32_t security_info_sent)
952 struct security_descriptor *psd = NULL;
953 NTSTATUS status;
955 if (sd_len == 0) {
956 return NT_STATUS_INVALID_PARAMETER;
959 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
961 if (!NT_STATUS_IS_OK(status)) {
962 return status;
965 return set_sd(fsp, psd, security_info_sent);
968 /****************************************************************************
969 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
970 ****************************************************************************/
972 static void call_nt_transact_create(connection_struct *conn,
973 struct smb_request *req,
974 uint16 **ppsetup, uint32 setup_count,
975 char **ppparams, uint32 parameter_count,
976 char **ppdata, uint32 data_count,
977 uint32 max_data_count)
979 struct smb_filename *smb_fname = NULL;
980 char *fname = NULL;
981 char *params = *ppparams;
982 char *data = *ppdata;
983 /* Breakout the oplock request bits so we can set the reply bits separately. */
984 uint32 fattr=0;
985 off_t file_len = 0;
986 int info = 0;
987 files_struct *fsp = NULL;
988 char *p = NULL;
989 uint32 flags;
990 uint32 access_mask;
991 uint32 file_attributes;
992 uint32 share_access;
993 uint32 create_disposition;
994 uint32 create_options;
995 uint32 sd_len;
996 struct security_descriptor *sd = NULL;
997 uint32 ea_len;
998 uint16 root_dir_fid;
999 struct timespec create_timespec;
1000 struct timespec c_timespec;
1001 struct timespec a_timespec;
1002 struct timespec m_timespec;
1003 struct timespec write_time_ts;
1004 struct ea_list *ea_list = NULL;
1005 NTSTATUS status;
1006 size_t param_len;
1007 uint64_t allocation_size;
1008 int oplock_request;
1009 uint8_t oplock_granted;
1010 struct case_semantics_state *case_state = NULL;
1011 TALLOC_CTX *ctx = talloc_tos();
1013 DEBUG(5,("call_nt_transact_create\n"));
1016 * If it's an IPC, use the pipe handler.
1019 if (IS_IPC(conn)) {
1020 if (lp_nt_pipe_support()) {
1021 do_nt_transact_create_pipe(
1022 conn, req,
1023 ppsetup, setup_count,
1024 ppparams, parameter_count,
1025 ppdata, data_count);
1026 goto out;
1028 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1029 goto out;
1033 * Ensure minimum number of parameters sent.
1036 if(parameter_count < 54) {
1037 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1038 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1039 goto out;
1042 flags = IVAL(params,0);
1043 access_mask = IVAL(params,8);
1044 file_attributes = IVAL(params,20);
1045 share_access = IVAL(params,24);
1046 create_disposition = IVAL(params,28);
1047 create_options = IVAL(params,32);
1048 sd_len = IVAL(params,36);
1049 ea_len = IVAL(params,40);
1050 root_dir_fid = (uint16)IVAL(params,4);
1051 allocation_size = BVAL(params,12);
1054 * we need to remove ignored bits when they come directly from the client
1055 * because we reuse some of them for internal stuff
1057 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1059 srvstr_get_path(ctx, params, req->flags2, &fname,
1060 params+53, parameter_count-53,
1061 STR_TERMINATE, &status);
1062 if (!NT_STATUS_IS_OK(status)) {
1063 reply_nterror(req, status);
1064 goto out;
1067 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1068 case_state = set_posix_case_semantics(ctx, conn);
1069 if (!case_state) {
1070 reply_nterror(req, NT_STATUS_NO_MEMORY);
1071 goto out;
1075 status = filename_convert(ctx,
1076 conn,
1077 req->flags2 & FLAGS2_DFS_PATHNAMES,
1078 fname,
1079 UCF_PREP_CREATEFILE,
1080 NULL,
1081 &smb_fname);
1083 TALLOC_FREE(case_state);
1085 if (!NT_STATUS_IS_OK(status)) {
1086 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1087 reply_botherror(req,
1088 NT_STATUS_PATH_NOT_COVERED,
1089 ERRSRV, ERRbadpath);
1090 goto out;
1092 reply_nterror(req, status);
1093 goto out;
1096 /* Ensure the data_len is correct for the sd and ea values given. */
1097 if ((ea_len + sd_len > data_count)
1098 || (ea_len > data_count) || (sd_len > data_count)
1099 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1100 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1101 "%u, data_count = %u\n", (unsigned int)ea_len,
1102 (unsigned int)sd_len, (unsigned int)data_count));
1103 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1104 goto out;
1107 if (sd_len) {
1108 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1109 sd_len));
1111 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1112 &sd);
1113 if (!NT_STATUS_IS_OK(status)) {
1114 DEBUG(10, ("call_nt_transact_create: "
1115 "unmarshall_sec_desc failed: %s\n",
1116 nt_errstr(status)));
1117 reply_nterror(req, status);
1118 goto out;
1122 if (ea_len) {
1123 if (!lp_ea_support(SNUM(conn))) {
1124 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1125 "EA's not supported.\n",
1126 (unsigned int)ea_len));
1127 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1128 goto out;
1131 if (ea_len < 10) {
1132 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1133 "too small (should be more than 10)\n",
1134 (unsigned int)ea_len ));
1135 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1136 goto out;
1139 /* We have already checked that ea_len <= data_count here. */
1140 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1141 ea_len);
1142 if (ea_list == NULL) {
1143 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1144 goto out;
1147 if (ea_list_has_invalid_name(ea_list)) {
1148 /* Realloc the size of parameters and data we will return */
1149 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1150 /* Extended response is 32 more byyes. */
1151 param_len = 101;
1152 } else {
1153 param_len = 69;
1155 params = nttrans_realloc(ppparams, param_len);
1156 if(params == NULL) {
1157 reply_nterror(req, NT_STATUS_NO_MEMORY);
1158 goto out;
1161 memset(params, '\0', param_len);
1162 send_nt_replies(conn, req, STATUS_INVALID_EA_NAME,
1163 params, param_len, NULL, 0);
1164 goto out;
1168 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1169 if (oplock_request) {
1170 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1171 ? BATCH_OPLOCK : 0;
1175 * Bug #6898 - clients using Windows opens should
1176 * never be able to set this attribute into the
1177 * VFS.
1179 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1181 status = SMB_VFS_CREATE_FILE(
1182 conn, /* conn */
1183 req, /* req */
1184 root_dir_fid, /* root_dir_fid */
1185 smb_fname, /* fname */
1186 access_mask, /* access_mask */
1187 share_access, /* share_access */
1188 create_disposition, /* create_disposition*/
1189 create_options, /* create_options */
1190 file_attributes, /* file_attributes */
1191 oplock_request, /* oplock_request */
1192 allocation_size, /* allocation_size */
1193 0, /* private_flags */
1194 sd, /* sd */
1195 ea_list, /* ea_list */
1196 &fsp, /* result */
1197 &info); /* pinfo */
1199 if(!NT_STATUS_IS_OK(status)) {
1200 if (open_was_deferred(req->sconn, req->mid)) {
1201 /* We have re-scheduled this call, no error. */
1202 return;
1204 reply_openerror(req, status);
1205 goto out;
1208 /* Ensure we're pointing at the correct stat struct. */
1209 TALLOC_FREE(smb_fname);
1210 smb_fname = fsp->fsp_name;
1213 * If the caller set the extended oplock request bit
1214 * and we granted one (by whatever means) - set the
1215 * correct bit for extended oplock reply.
1218 if (oplock_request &&
1219 (lp_fake_oplocks(SNUM(conn))
1220 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1223 * Exclusive oplock granted
1226 if (flags & REQUEST_BATCH_OPLOCK) {
1227 oplock_granted = BATCH_OPLOCK_RETURN;
1228 } else {
1229 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1231 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1232 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1233 } else {
1234 oplock_granted = NO_OPLOCK_RETURN;
1237 file_len = smb_fname->st.st_ex_size;
1239 /* Realloc the size of parameters and data we will return */
1240 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1241 /* Extended response is 32 more byyes. */
1242 param_len = 101;
1243 } else {
1244 param_len = 69;
1246 params = nttrans_realloc(ppparams, param_len);
1247 if(params == NULL) {
1248 reply_nterror(req, NT_STATUS_NO_MEMORY);
1249 goto out;
1252 p = params;
1253 SCVAL(p, 0, oplock_granted);
1255 p += 2;
1256 SSVAL(p,0,fsp->fnum);
1257 p += 2;
1258 if ((create_disposition == FILE_SUPERSEDE)
1259 && (info == FILE_WAS_OVERWRITTEN)) {
1260 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1261 } else {
1262 SIVAL(p,0,info);
1264 p += 8;
1266 fattr = dos_mode(conn, smb_fname);
1267 if (fattr == 0) {
1268 fattr = FILE_ATTRIBUTE_NORMAL;
1271 /* Deal with other possible opens having a modified
1272 write time. JRA. */
1273 ZERO_STRUCT(write_time_ts);
1274 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
1275 if (!null_timespec(write_time_ts)) {
1276 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1279 /* Create time. */
1280 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1281 a_timespec = smb_fname->st.st_ex_atime;
1282 m_timespec = smb_fname->st.st_ex_mtime;
1283 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1285 if (lp_dos_filetime_resolution(SNUM(conn))) {
1286 dos_filetime_timespec(&create_timespec);
1287 dos_filetime_timespec(&a_timespec);
1288 dos_filetime_timespec(&m_timespec);
1289 dos_filetime_timespec(&c_timespec);
1292 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1293 p += 8;
1294 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1295 p += 8;
1296 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1297 p += 8;
1298 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1299 p += 8;
1300 SIVAL(p,0,fattr); /* File Attributes. */
1301 p += 4;
1302 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1303 p += 8;
1304 SOFF_T(p,0,file_len);
1305 p += 8;
1306 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1307 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1308 size_t num_names = 0;
1309 unsigned int num_streams = 0;
1310 struct stream_struct *streams = NULL;
1312 /* Do we have any EA's ? */
1313 status = get_ea_names_from_file(ctx, conn, fsp,
1314 smb_fname->base_name, NULL, &num_names);
1315 if (NT_STATUS_IS_OK(status) && num_names) {
1316 file_status &= ~NO_EAS;
1318 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
1319 &num_streams, &streams);
1320 /* There is always one stream, ::$DATA. */
1321 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1322 file_status &= ~NO_SUBSTREAMS;
1324 TALLOC_FREE(streams);
1325 SSVAL(p,2,file_status);
1327 p += 4;
1328 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1330 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1331 uint32 perms = 0;
1332 p += 25;
1333 if (fsp->is_directory ||
1334 fsp->can_write ||
1335 can_write_to_file(conn, smb_fname)) {
1336 perms = FILE_GENERIC_ALL;
1337 } else {
1338 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1340 SIVAL(p,0,perms);
1343 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1344 smb_fname_str_dbg(smb_fname)));
1346 /* Send the required number of replies */
1347 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1348 out:
1349 return;
1352 /****************************************************************************
1353 Reply to a NT CANCEL request.
1354 conn POINTER CAN BE NULL HERE !
1355 ****************************************************************************/
1357 void reply_ntcancel(struct smb_request *req)
1360 * Go through and cancel any pending change notifies.
1363 START_PROFILE(SMBntcancel);
1364 srv_cancel_sign_response(req->sconn);
1365 remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
1366 remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
1368 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1369 (unsigned long long)req->mid));
1371 END_PROFILE(SMBntcancel);
1372 return;
1375 /****************************************************************************
1376 Copy a file.
1377 ****************************************************************************/
1379 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1380 connection_struct *conn,
1381 struct smb_request *req,
1382 struct smb_filename *smb_fname_src,
1383 struct smb_filename *smb_fname_dst,
1384 uint32 attrs)
1386 files_struct *fsp1,*fsp2;
1387 uint32 fattr;
1388 int info;
1389 off_t ret=-1;
1390 NTSTATUS status = NT_STATUS_OK;
1391 char *parent;
1393 if (!CAN_WRITE(conn)) {
1394 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1395 goto out;
1398 /* Source must already exist. */
1399 if (!VALID_STAT(smb_fname_src->st)) {
1400 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1401 goto out;
1404 /* Ensure attributes match. */
1405 fattr = dos_mode(conn, smb_fname_src);
1406 if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1407 status = NT_STATUS_NO_SUCH_FILE;
1408 goto out;
1411 /* Disallow if dst file already exists. */
1412 if (VALID_STAT(smb_fname_dst->st)) {
1413 status = NT_STATUS_OBJECT_NAME_COLLISION;
1414 goto out;
1417 /* No links from a directory. */
1418 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1419 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1420 goto out;
1423 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1424 smb_fname_str_dbg(smb_fname_src),
1425 smb_fname_str_dbg(smb_fname_dst)));
1427 status = SMB_VFS_CREATE_FILE(
1428 conn, /* conn */
1429 req, /* req */
1430 0, /* root_dir_fid */
1431 smb_fname_src, /* fname */
1432 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1433 FILE_READ_EA, /* access_mask */
1434 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1435 FILE_SHARE_DELETE),
1436 FILE_OPEN, /* create_disposition*/
1437 0, /* create_options */
1438 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1439 NO_OPLOCK, /* oplock_request */
1440 0, /* allocation_size */
1441 0, /* private_flags */
1442 NULL, /* sd */
1443 NULL, /* ea_list */
1444 &fsp1, /* result */
1445 &info); /* pinfo */
1447 if (!NT_STATUS_IS_OK(status)) {
1448 goto out;
1451 status = SMB_VFS_CREATE_FILE(
1452 conn, /* conn */
1453 req, /* req */
1454 0, /* root_dir_fid */
1455 smb_fname_dst, /* fname */
1456 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1457 FILE_WRITE_EA, /* access_mask */
1458 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1459 FILE_SHARE_DELETE),
1460 FILE_CREATE, /* create_disposition*/
1461 0, /* create_options */
1462 fattr, /* file_attributes */
1463 NO_OPLOCK, /* oplock_request */
1464 0, /* allocation_size */
1465 0, /* private_flags */
1466 NULL, /* sd */
1467 NULL, /* ea_list */
1468 &fsp2, /* result */
1469 &info); /* pinfo */
1471 if (!NT_STATUS_IS_OK(status)) {
1472 close_file(NULL, fsp1, ERROR_CLOSE);
1473 goto out;
1476 if (smb_fname_src->st.st_ex_size) {
1477 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1481 * As we are opening fsp1 read-only we only expect
1482 * an error on close on fsp2 if we are out of space.
1483 * Thus we don't look at the error return from the
1484 * close of fsp1.
1486 close_file(NULL, fsp1, NORMAL_CLOSE);
1488 /* Ensure the modtime is set correctly on the destination file. */
1489 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1491 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1493 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1494 creates the file. This isn't the correct thing to do in the copy
1495 case. JRA */
1496 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1497 NULL)) {
1498 status = NT_STATUS_NO_MEMORY;
1499 goto out;
1501 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1502 TALLOC_FREE(parent);
1504 if (ret < (off_t)smb_fname_src->st.st_ex_size) {
1505 status = NT_STATUS_DISK_FULL;
1506 goto out;
1508 out:
1509 if (!NT_STATUS_IS_OK(status)) {
1510 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1511 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1512 smb_fname_str_dbg(smb_fname_dst)));
1515 return status;
1518 /****************************************************************************
1519 Reply to a NT rename request.
1520 ****************************************************************************/
1522 void reply_ntrename(struct smb_request *req)
1524 connection_struct *conn = req->conn;
1525 struct smb_filename *smb_fname_old = NULL;
1526 struct smb_filename *smb_fname_new = NULL;
1527 char *oldname = NULL;
1528 char *newname = NULL;
1529 const char *p;
1530 NTSTATUS status;
1531 bool src_has_wcard = False;
1532 bool dest_has_wcard = False;
1533 uint32 attrs;
1534 uint32_t ucf_flags_src = 0;
1535 uint32_t ucf_flags_dst = 0;
1536 uint16 rename_type;
1537 TALLOC_CTX *ctx = talloc_tos();
1538 bool stream_rename = false;
1540 START_PROFILE(SMBntrename);
1542 if (req->wct < 4) {
1543 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1544 goto out;
1547 attrs = SVAL(req->vwv+0, 0);
1548 rename_type = SVAL(req->vwv+1, 0);
1550 p = (const char *)req->buf + 1;
1551 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1552 &status, &src_has_wcard);
1553 if (!NT_STATUS_IS_OK(status)) {
1554 reply_nterror(req, status);
1555 goto out;
1558 if (ms_has_wild(oldname)) {
1559 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1560 goto out;
1563 p++;
1564 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1565 &status, &dest_has_wcard);
1566 if (!NT_STATUS_IS_OK(status)) {
1567 reply_nterror(req, status);
1568 goto out;
1571 if (!lp_posix_pathnames()) {
1572 /* The newname must begin with a ':' if the
1573 oldname contains a ':'. */
1574 if (strchr_m(oldname, ':')) {
1575 if (newname[0] != ':') {
1576 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1577 goto out;
1579 stream_rename = true;
1584 * If this is a rename operation, allow wildcards and save the
1585 * destination's last component.
1587 if (rename_type == RENAME_FLAG_RENAME) {
1588 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1589 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1592 /* rename_internals() calls unix_convert(), so don't call it here. */
1593 status = filename_convert(ctx, conn,
1594 req->flags2 & FLAGS2_DFS_PATHNAMES,
1595 oldname,
1596 ucf_flags_src,
1597 NULL,
1598 &smb_fname_old);
1599 if (!NT_STATUS_IS_OK(status)) {
1600 if (NT_STATUS_EQUAL(status,
1601 NT_STATUS_PATH_NOT_COVERED)) {
1602 reply_botherror(req,
1603 NT_STATUS_PATH_NOT_COVERED,
1604 ERRSRV, ERRbadpath);
1605 goto out;
1607 reply_nterror(req, status);
1608 goto out;
1611 status = filename_convert(ctx, conn,
1612 req->flags2 & FLAGS2_DFS_PATHNAMES,
1613 newname,
1614 ucf_flags_dst,
1615 &dest_has_wcard,
1616 &smb_fname_new);
1617 if (!NT_STATUS_IS_OK(status)) {
1618 if (NT_STATUS_EQUAL(status,
1619 NT_STATUS_PATH_NOT_COVERED)) {
1620 reply_botherror(req,
1621 NT_STATUS_PATH_NOT_COVERED,
1622 ERRSRV, ERRbadpath);
1623 goto out;
1625 reply_nterror(req, status);
1626 goto out;
1629 if (stream_rename) {
1630 /* smb_fname_new must be the same as smb_fname_old. */
1631 TALLOC_FREE(smb_fname_new->base_name);
1632 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1633 smb_fname_old->base_name);
1634 if (!smb_fname_new->base_name) {
1635 reply_nterror(req, NT_STATUS_NO_MEMORY);
1636 goto out;
1640 DEBUG(3,("reply_ntrename: %s -> %s\n",
1641 smb_fname_str_dbg(smb_fname_old),
1642 smb_fname_str_dbg(smb_fname_new)));
1644 switch(rename_type) {
1645 case RENAME_FLAG_RENAME:
1646 status = rename_internals(ctx, conn, req,
1647 smb_fname_old, smb_fname_new,
1648 attrs, False, src_has_wcard,
1649 dest_has_wcard,
1650 DELETE_ACCESS);
1651 break;
1652 case RENAME_FLAG_HARD_LINK:
1653 if (src_has_wcard || dest_has_wcard) {
1654 /* No wildcards. */
1655 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1656 } else {
1657 status = hardlink_internals(ctx, conn,
1658 req,
1659 false,
1660 smb_fname_old,
1661 smb_fname_new);
1663 break;
1664 case RENAME_FLAG_COPY:
1665 if (src_has_wcard || dest_has_wcard) {
1666 /* No wildcards. */
1667 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1668 } else {
1669 status = copy_internals(ctx, conn, req,
1670 smb_fname_old,
1671 smb_fname_new,
1672 attrs);
1674 break;
1675 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1676 status = NT_STATUS_INVALID_PARAMETER;
1677 break;
1678 default:
1679 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1680 break;
1683 if (!NT_STATUS_IS_OK(status)) {
1684 if (open_was_deferred(req->sconn, req->mid)) {
1685 /* We have re-scheduled this call. */
1686 goto out;
1689 reply_nterror(req, status);
1690 goto out;
1693 reply_outbuf(req, 0, 0);
1694 out:
1695 END_PROFILE(SMBntrename);
1696 return;
1699 /****************************************************************************
1700 Reply to a notify change - queue the request and
1701 don't allow a directory to be opened.
1702 ****************************************************************************/
1704 static void smbd_smb1_notify_reply(struct smb_request *req,
1705 NTSTATUS error_code,
1706 uint8_t *buf, size_t len)
1708 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1711 static void call_nt_transact_notify_change(connection_struct *conn,
1712 struct smb_request *req,
1713 uint16 **ppsetup,
1714 uint32 setup_count,
1715 char **ppparams,
1716 uint32 parameter_count,
1717 char **ppdata, uint32 data_count,
1718 uint32 max_data_count,
1719 uint32 max_param_count)
1721 uint16 *setup = *ppsetup;
1722 files_struct *fsp;
1723 uint32 filter;
1724 NTSTATUS status;
1725 bool recursive;
1727 if(setup_count < 6) {
1728 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1729 return;
1732 fsp = file_fsp(req, SVAL(setup,4));
1733 filter = IVAL(setup, 0);
1734 recursive = (SVAL(setup, 6) != 0) ? True : False;
1736 DEBUG(3,("call_nt_transact_notify_change\n"));
1738 if(!fsp) {
1739 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1740 return;
1744 char *filter_string;
1746 if (!(filter_string = notify_filter_string(NULL, filter))) {
1747 reply_nterror(req,NT_STATUS_NO_MEMORY);
1748 return;
1751 DEBUG(3,("call_nt_transact_notify_change: notify change "
1752 "called on %s, filter = %s, recursive = %d\n",
1753 fsp_str_dbg(fsp), filter_string, recursive));
1755 TALLOC_FREE(filter_string);
1758 if((!fsp->is_directory) || (conn != fsp->conn)) {
1759 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1760 return;
1763 if (fsp->notify == NULL) {
1765 status = change_notify_create(fsp, filter, recursive);
1767 if (!NT_STATUS_IS_OK(status)) {
1768 DEBUG(10, ("change_notify_create returned %s\n",
1769 nt_errstr(status)));
1770 reply_nterror(req, status);
1771 return;
1775 if (change_notify_fsp_has_changes(fsp)) {
1778 * We've got changes pending, respond immediately
1782 * TODO: write a torture test to check the filtering behaviour
1783 * here.
1786 change_notify_reply(req,
1787 NT_STATUS_OK,
1788 max_param_count,
1789 fsp->notify,
1790 smbd_smb1_notify_reply);
1793 * change_notify_reply() above has independently sent its
1794 * results
1796 return;
1800 * No changes pending, queue the request
1803 status = change_notify_add_request(req,
1804 max_param_count,
1805 filter,
1806 recursive, fsp,
1807 smbd_smb1_notify_reply);
1808 if (!NT_STATUS_IS_OK(status)) {
1809 reply_nterror(req, status);
1811 return;
1814 /****************************************************************************
1815 Reply to an NT transact rename command.
1816 ****************************************************************************/
1818 static void call_nt_transact_rename(connection_struct *conn,
1819 struct smb_request *req,
1820 uint16 **ppsetup, uint32 setup_count,
1821 char **ppparams, uint32 parameter_count,
1822 char **ppdata, uint32 data_count,
1823 uint32 max_data_count)
1825 char *params = *ppparams;
1826 char *new_name = NULL;
1827 files_struct *fsp = NULL;
1828 bool dest_has_wcard = False;
1829 NTSTATUS status;
1830 TALLOC_CTX *ctx = talloc_tos();
1832 if(parameter_count < 5) {
1833 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1834 return;
1837 fsp = file_fsp(req, SVAL(params, 0));
1838 if (!check_fsp(conn, req, fsp)) {
1839 return;
1841 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1842 parameter_count - 4,
1843 STR_TERMINATE, &status, &dest_has_wcard);
1844 if (!NT_STATUS_IS_OK(status)) {
1845 reply_nterror(req, status);
1846 return;
1850 * W2K3 ignores this request as the RAW-RENAME test
1851 * demonstrates, so we do.
1853 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1855 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1856 fsp_str_dbg(fsp), new_name));
1858 return;
1861 /******************************************************************************
1862 Fake up a completely empty SD.
1863 *******************************************************************************/
1865 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1867 size_t sd_size;
1869 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1870 if(!*ppsd) {
1871 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1872 return NT_STATUS_NO_MEMORY;
1875 return NT_STATUS_OK;
1878 /****************************************************************************
1879 Reply to query a security descriptor.
1880 Callable from SMB2 and SMB2.
1881 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1882 the required size.
1883 ****************************************************************************/
1885 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1886 TALLOC_CTX *mem_ctx,
1887 files_struct *fsp,
1888 uint32_t security_info_wanted,
1889 uint32_t max_data_count,
1890 uint8_t **ppmarshalled_sd,
1891 size_t *psd_size)
1893 NTSTATUS status;
1894 struct security_descriptor *psd = NULL;
1895 TALLOC_CTX *frame = talloc_stackframe();
1898 * Get the permissions to return.
1901 if ((security_info_wanted & SECINFO_SACL) &&
1902 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1903 DEBUG(10, ("Access to SACL denied.\n"));
1904 TALLOC_FREE(frame);
1905 return NT_STATUS_ACCESS_DENIED;
1908 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1909 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1910 DEBUG(10, ("Access to DACL, OWNER, or GROUP denied.\n"));
1911 TALLOC_FREE(frame);
1912 return NT_STATUS_ACCESS_DENIED;
1915 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1916 SECINFO_GROUP|SECINFO_SACL)) {
1917 /* Don't return SECINFO_LABEL if anything else was
1918 requested. See bug #8458. */
1919 security_info_wanted &= ~SECINFO_LABEL;
1922 if (!lp_nt_acl_support(SNUM(conn))) {
1923 status = get_null_nt_acl(frame, &psd);
1924 } else if (security_info_wanted & SECINFO_LABEL) {
1925 /* Like W2K3 return a null object. */
1926 status = get_null_nt_acl(frame, &psd);
1927 } else {
1928 status = SMB_VFS_FGET_NT_ACL(
1929 fsp, security_info_wanted, frame, &psd);
1931 if (!NT_STATUS_IS_OK(status)) {
1932 TALLOC_FREE(frame);
1933 return status;
1936 if (!(security_info_wanted & SECINFO_OWNER)) {
1937 psd->owner_sid = NULL;
1939 if (!(security_info_wanted & SECINFO_GROUP)) {
1940 psd->group_sid = NULL;
1942 if (!(security_info_wanted & SECINFO_DACL)) {
1943 psd->type &= ~SEC_DESC_DACL_PRESENT;
1944 psd->dacl = NULL;
1946 if (!(security_info_wanted & SECINFO_SACL)) {
1947 psd->type &= ~SEC_DESC_SACL_PRESENT;
1948 psd->sacl = NULL;
1951 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1952 * present in the reply to match Windows behavior */
1953 if (psd->sacl == NULL &&
1954 security_info_wanted & SECINFO_SACL)
1955 psd->type |= SEC_DESC_SACL_PRESENT;
1956 if (psd->dacl == NULL &&
1957 security_info_wanted & SECINFO_DACL)
1958 psd->type |= SEC_DESC_DACL_PRESENT;
1960 if (security_info_wanted & SECINFO_LABEL) {
1961 /* Like W2K3 return a null object. */
1962 psd->owner_sid = NULL;
1963 psd->group_sid = NULL;
1964 psd->dacl = NULL;
1965 psd->sacl = NULL;
1966 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1969 *psd_size = ndr_size_security_descriptor(psd, 0);
1971 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1972 (unsigned long)*psd_size));
1974 if (DEBUGLEVEL >= 10) {
1975 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1976 fsp_str_dbg(fsp)));
1977 NDR_PRINT_DEBUG(security_descriptor, psd);
1980 if (max_data_count < *psd_size) {
1981 TALLOC_FREE(frame);
1982 return NT_STATUS_BUFFER_TOO_SMALL;
1985 status = marshall_sec_desc(mem_ctx, psd,
1986 ppmarshalled_sd, psd_size);
1988 if (!NT_STATUS_IS_OK(status)) {
1989 TALLOC_FREE(frame);
1990 return status;
1993 TALLOC_FREE(frame);
1994 return NT_STATUS_OK;
1997 /****************************************************************************
1998 SMB1 reply to query a security descriptor.
1999 ****************************************************************************/
2001 static void call_nt_transact_query_security_desc(connection_struct *conn,
2002 struct smb_request *req,
2003 uint16 **ppsetup,
2004 uint32 setup_count,
2005 char **ppparams,
2006 uint32 parameter_count,
2007 char **ppdata,
2008 uint32 data_count,
2009 uint32 max_data_count)
2011 char *params = *ppparams;
2012 char *data = *ppdata;
2013 size_t sd_size = 0;
2014 uint32 security_info_wanted;
2015 files_struct *fsp = NULL;
2016 NTSTATUS status;
2017 uint8_t *marshalled_sd = NULL;
2019 if(parameter_count < 8) {
2020 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2021 return;
2024 fsp = file_fsp(req, SVAL(params,0));
2025 if(!fsp) {
2026 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2027 return;
2030 security_info_wanted = IVAL(params,4);
2032 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
2033 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
2034 (unsigned int)security_info_wanted));
2036 params = nttrans_realloc(ppparams, 4);
2037 if(params == NULL) {
2038 reply_nterror(req, NT_STATUS_NO_MEMORY);
2039 return;
2043 * Get the permissions to return.
2046 status = smbd_do_query_security_desc(conn,
2047 talloc_tos(),
2048 fsp,
2049 security_info_wanted,
2050 max_data_count,
2051 &marshalled_sd,
2052 &sd_size);
2054 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2055 SIVAL(params,0,(uint32_t)sd_size);
2056 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2057 params, 4, NULL, 0);
2058 return;
2061 if (!NT_STATUS_IS_OK(status)) {
2062 reply_nterror(req, status);
2063 return;
2066 SMB_ASSERT(sd_size > 0);
2068 SIVAL(params,0,(uint32_t)sd_size);
2070 if (max_data_count < sd_size) {
2071 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2072 params, 4, NULL, 0);
2073 return;
2077 * Allocate the data we will return.
2080 data = nttrans_realloc(ppdata, sd_size);
2081 if(data == NULL) {
2082 reply_nterror(req, NT_STATUS_NO_MEMORY);
2083 return;
2086 memcpy(data, marshalled_sd, sd_size);
2088 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2090 return;
2093 /****************************************************************************
2094 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2095 ****************************************************************************/
2097 static void call_nt_transact_set_security_desc(connection_struct *conn,
2098 struct smb_request *req,
2099 uint16 **ppsetup,
2100 uint32 setup_count,
2101 char **ppparams,
2102 uint32 parameter_count,
2103 char **ppdata,
2104 uint32 data_count,
2105 uint32 max_data_count)
2107 char *params= *ppparams;
2108 char *data = *ppdata;
2109 files_struct *fsp = NULL;
2110 uint32 security_info_sent = 0;
2111 NTSTATUS status;
2113 if(parameter_count < 8) {
2114 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2115 return;
2118 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2119 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2120 return;
2123 if (!CAN_WRITE(fsp->conn)) {
2124 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2125 return;
2128 if(!lp_nt_acl_support(SNUM(conn))) {
2129 goto done;
2132 security_info_sent = IVAL(params,4);
2134 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2135 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2137 if (data_count == 0) {
2138 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2139 return;
2142 status = set_sd_blob(fsp, (uint8 *)data, data_count, security_info_sent);
2144 if (!NT_STATUS_IS_OK(status)) {
2145 reply_nterror(req, status);
2146 return;
2149 done:
2150 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2151 return;
2154 /****************************************************************************
2155 Reply to NT IOCTL
2156 ****************************************************************************/
2158 static void call_nt_transact_ioctl(connection_struct *conn,
2159 struct smb_request *req,
2160 uint16 **ppsetup, uint32 setup_count,
2161 char **ppparams, uint32 parameter_count,
2162 char **ppdata, uint32 data_count,
2163 uint32 max_data_count)
2165 NTSTATUS status;
2166 uint32 function;
2167 uint16 fidnum;
2168 files_struct *fsp;
2169 uint8 isFSctl;
2170 uint8 compfilter;
2171 char *out_data = NULL;
2172 uint32 out_data_len = 0;
2173 char *pdata = *ppdata;
2174 TALLOC_CTX *ctx = talloc_tos();
2176 if (setup_count != 8) {
2177 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2178 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2179 return;
2182 function = IVAL(*ppsetup, 0);
2183 fidnum = SVAL(*ppsetup, 4);
2184 isFSctl = CVAL(*ppsetup, 6);
2185 compfilter = CVAL(*ppsetup, 7);
2187 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2188 function, fidnum, isFSctl, compfilter));
2190 fsp=file_fsp(req, fidnum);
2193 * We don't really implement IOCTLs, especially on files.
2195 if (!isFSctl) {
2196 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2197 isFSctl));
2198 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2199 return;
2202 /* Has to be for an open file! */
2203 if (!check_fsp_open(conn, req, fsp)) {
2204 return;
2207 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2210 * out_data might be allocated by the VFS module, but talloc should be
2211 * used, and should be cleaned up when the request ends.
2213 status = SMB_VFS_FSCTL(fsp,
2214 ctx,
2215 function,
2216 req->flags2,
2217 (uint8_t *)pdata,
2218 data_count,
2219 (uint8_t **)&out_data,
2220 max_data_count,
2221 &out_data_len);
2222 if (!NT_STATUS_IS_OK(status)) {
2223 reply_nterror(req, status);
2224 } else {
2225 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2230 #ifdef HAVE_SYS_QUOTAS
2231 /****************************************************************************
2232 Reply to get user quota
2233 ****************************************************************************/
2235 static void call_nt_transact_get_user_quota(connection_struct *conn,
2236 struct smb_request *req,
2237 uint16 **ppsetup,
2238 uint32 setup_count,
2239 char **ppparams,
2240 uint32 parameter_count,
2241 char **ppdata,
2242 uint32 data_count,
2243 uint32 max_data_count)
2245 NTSTATUS nt_status = NT_STATUS_OK;
2246 char *params = *ppparams;
2247 char *pdata = *ppdata;
2248 char *entry;
2249 int data_len=0,param_len=0;
2250 int qt_len=0;
2251 int entry_len = 0;
2252 files_struct *fsp = NULL;
2253 uint16 level = 0;
2254 size_t sid_len;
2255 struct dom_sid sid;
2256 bool start_enum = True;
2257 SMB_NTQUOTA_STRUCT qt;
2258 SMB_NTQUOTA_LIST *tmp_list;
2259 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2261 ZERO_STRUCT(qt);
2263 /* access check */
2264 if (get_current_uid(conn) != 0) {
2265 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2266 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2267 conn->session_info->unix_info->unix_name));
2268 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2269 return;
2273 * Ensure minimum number of parameters sent.
2276 if (parameter_count < 4) {
2277 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2278 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2279 return;
2282 /* maybe we can check the quota_fnum */
2283 fsp = file_fsp(req, SVAL(params,0));
2284 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2285 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2286 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2287 return;
2290 /* the NULL pointer checking for fsp->fake_file_handle->pd
2291 * is done by CHECK_NTQUOTA_HANDLE_OK()
2293 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2295 level = SVAL(params,2);
2297 /* unknown 12 bytes leading in params */
2299 switch (level) {
2300 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2301 /* seems that we should continue with the enum here --metze */
2303 if (qt_handle->quota_list!=NULL &&
2304 qt_handle->tmp_list==NULL) {
2306 /* free the list */
2307 free_ntquota_list(&(qt_handle->quota_list));
2309 /* Realloc the size of parameters and data we will return */
2310 param_len = 4;
2311 params = nttrans_realloc(ppparams, param_len);
2312 if(params == NULL) {
2313 reply_nterror(req, NT_STATUS_NO_MEMORY);
2314 return;
2317 data_len = 0;
2318 SIVAL(params,0,data_len);
2320 break;
2323 start_enum = False;
2325 case TRANSACT_GET_USER_QUOTA_LIST_START:
2327 if (qt_handle->quota_list==NULL &&
2328 qt_handle->tmp_list==NULL) {
2329 start_enum = True;
2332 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2333 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2334 return;
2337 /* Realloc the size of parameters and data we will return */
2338 param_len = 4;
2339 params = nttrans_realloc(ppparams, param_len);
2340 if(params == NULL) {
2341 reply_nterror(req, NT_STATUS_NO_MEMORY);
2342 return;
2345 /* we should not trust the value in max_data_count*/
2346 max_data_count = MIN(max_data_count,2048);
2348 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2349 if(pdata == NULL) {
2350 reply_nterror(req, NT_STATUS_NO_MEMORY);
2351 return;
2354 entry = pdata;
2356 /* set params Size of returned Quota Data 4 bytes*/
2357 /* but set it later when we know it */
2359 /* for each entry push the data */
2361 if (start_enum) {
2362 qt_handle->tmp_list = qt_handle->quota_list;
2365 tmp_list = qt_handle->tmp_list;
2367 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2368 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2370 sid_len = ndr_size_dom_sid(
2371 &tmp_list->quotas->sid, 0);
2372 entry_len = 40 + sid_len;
2374 /* nextoffset entry 4 bytes */
2375 SIVAL(entry,0,entry_len);
2377 /* then the len of the SID 4 bytes */
2378 SIVAL(entry,4,sid_len);
2380 /* unknown data 8 bytes uint64_t */
2381 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2383 /* the used disk space 8 bytes uint64_t */
2384 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2386 /* the soft quotas 8 bytes uint64_t */
2387 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2389 /* the hard quotas 8 bytes uint64_t */
2390 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2392 /* and now the SID */
2393 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2396 qt_handle->tmp_list = tmp_list;
2398 /* overwrite the offset of the last entry */
2399 SIVAL(entry-entry_len,0,0);
2401 data_len = 4+qt_len;
2402 /* overwrite the params quota_data_len */
2403 SIVAL(params,0,data_len);
2405 break;
2407 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2409 /* unknown 4 bytes IVAL(pdata,0) */
2411 if (data_count < 8) {
2412 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2413 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2414 return;
2417 sid_len = IVAL(pdata,4);
2418 /* Ensure this is less than 1mb. */
2419 if (sid_len > (1024*1024)) {
2420 reply_nterror(req, NT_STATUS_NO_MEMORY);
2421 return;
2424 if (data_count < 8+sid_len) {
2425 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2426 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2427 return;
2430 data_len = 4+40+sid_len;
2432 if (max_data_count < data_len) {
2433 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2434 max_data_count, data_len));
2435 param_len = 4;
2436 SIVAL(params,0,data_len);
2437 data_len = 0;
2438 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2439 break;
2442 if (!sid_parse(pdata+8,sid_len,&sid)) {
2443 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2444 return;
2447 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2448 ZERO_STRUCT(qt);
2450 * we have to return zero's in all fields
2451 * instead of returning an error here
2452 * --metze
2456 /* Realloc the size of parameters and data we will return */
2457 param_len = 4;
2458 params = nttrans_realloc(ppparams, param_len);
2459 if(params == NULL) {
2460 reply_nterror(req, NT_STATUS_NO_MEMORY);
2461 return;
2464 pdata = nttrans_realloc(ppdata, data_len);
2465 if(pdata == NULL) {
2466 reply_nterror(req, NT_STATUS_NO_MEMORY);
2467 return;
2470 entry = pdata;
2472 /* set params Size of returned Quota Data 4 bytes*/
2473 SIVAL(params,0,data_len);
2475 /* nextoffset entry 4 bytes */
2476 SIVAL(entry,0,0);
2478 /* then the len of the SID 4 bytes */
2479 SIVAL(entry,4,sid_len);
2481 /* unknown data 8 bytes uint64_t */
2482 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2484 /* the used disk space 8 bytes uint64_t */
2485 SBIG_UINT(entry,16,qt.usedspace);
2487 /* the soft quotas 8 bytes uint64_t */
2488 SBIG_UINT(entry,24,qt.softlim);
2490 /* the hard quotas 8 bytes uint64_t */
2491 SBIG_UINT(entry,32,qt.hardlim);
2493 /* and now the SID */
2494 sid_linearize(entry+40, sid_len, &sid);
2496 break;
2498 default:
2499 DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
2500 "level 0x%04hX\n",
2501 fsp_fnum_dbg(fsp), level));
2502 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2503 return;
2504 break;
2507 send_nt_replies(conn, req, nt_status, params, param_len,
2508 pdata, data_len);
2511 /****************************************************************************
2512 Reply to set user quota
2513 ****************************************************************************/
2515 static void call_nt_transact_set_user_quota(connection_struct *conn,
2516 struct smb_request *req,
2517 uint16 **ppsetup,
2518 uint32 setup_count,
2519 char **ppparams,
2520 uint32 parameter_count,
2521 char **ppdata,
2522 uint32 data_count,
2523 uint32 max_data_count)
2525 char *params = *ppparams;
2526 char *pdata = *ppdata;
2527 int data_len=0,param_len=0;
2528 SMB_NTQUOTA_STRUCT qt;
2529 size_t sid_len;
2530 struct dom_sid sid;
2531 files_struct *fsp = NULL;
2533 ZERO_STRUCT(qt);
2535 /* access check */
2536 if (get_current_uid(conn) != 0) {
2537 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2538 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2539 conn->session_info->unix_info->unix_name));
2540 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2541 return;
2545 * Ensure minimum number of parameters sent.
2548 if (parameter_count < 2) {
2549 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2550 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2551 return;
2554 /* maybe we can check the quota_fnum */
2555 fsp = file_fsp(req, SVAL(params,0));
2556 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2557 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2558 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2559 return;
2562 if (data_count < 40) {
2563 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2564 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2565 return;
2568 /* offset to next quota record.
2569 * 4 bytes IVAL(pdata,0)
2570 * unused here...
2573 /* sid len */
2574 sid_len = IVAL(pdata,4);
2576 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2577 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2578 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2579 return;
2582 /* unknown 8 bytes in pdata
2583 * maybe its the change time in NTTIME
2586 /* the used space 8 bytes (uint64_t)*/
2587 qt.usedspace = BVAL(pdata,16);
2589 /* the soft quotas 8 bytes (uint64_t)*/
2590 qt.softlim = BVAL(pdata,24);
2592 /* the hard quotas 8 bytes (uint64_t)*/
2593 qt.hardlim = BVAL(pdata,32);
2595 if (!sid_parse(pdata+40,sid_len,&sid)) {
2596 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2597 return;
2600 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2602 /* 44 unknown bytes left... */
2604 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2605 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2606 return;
2609 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2610 pdata, data_len);
2612 #endif /* HAVE_SYS_QUOTAS */
2614 static void handle_nttrans(connection_struct *conn,
2615 struct trans_state *state,
2616 struct smb_request *req)
2618 if (get_Protocol() >= PROTOCOL_NT1) {
2619 req->flags2 |= 0x40; /* IS_LONG_NAME */
2620 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2624 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2626 /* Now we must call the relevant NT_TRANS function */
2627 switch(state->call) {
2628 case NT_TRANSACT_CREATE:
2630 START_PROFILE(NT_transact_create);
2631 call_nt_transact_create(
2632 conn, req,
2633 &state->setup, state->setup_count,
2634 &state->param, state->total_param,
2635 &state->data, state->total_data,
2636 state->max_data_return);
2637 END_PROFILE(NT_transact_create);
2638 break;
2641 case NT_TRANSACT_IOCTL:
2643 START_PROFILE(NT_transact_ioctl);
2644 call_nt_transact_ioctl(
2645 conn, req,
2646 &state->setup, state->setup_count,
2647 &state->param, state->total_param,
2648 &state->data, state->total_data,
2649 state->max_data_return);
2650 END_PROFILE(NT_transact_ioctl);
2651 break;
2654 case NT_TRANSACT_SET_SECURITY_DESC:
2656 START_PROFILE(NT_transact_set_security_desc);
2657 call_nt_transact_set_security_desc(
2658 conn, req,
2659 &state->setup, state->setup_count,
2660 &state->param, state->total_param,
2661 &state->data, state->total_data,
2662 state->max_data_return);
2663 END_PROFILE(NT_transact_set_security_desc);
2664 break;
2667 case NT_TRANSACT_NOTIFY_CHANGE:
2669 START_PROFILE(NT_transact_notify_change);
2670 call_nt_transact_notify_change(
2671 conn, req,
2672 &state->setup, state->setup_count,
2673 &state->param, state->total_param,
2674 &state->data, state->total_data,
2675 state->max_data_return,
2676 state->max_param_return);
2677 END_PROFILE(NT_transact_notify_change);
2678 break;
2681 case NT_TRANSACT_RENAME:
2683 START_PROFILE(NT_transact_rename);
2684 call_nt_transact_rename(
2685 conn, req,
2686 &state->setup, state->setup_count,
2687 &state->param, state->total_param,
2688 &state->data, state->total_data,
2689 state->max_data_return);
2690 END_PROFILE(NT_transact_rename);
2691 break;
2694 case NT_TRANSACT_QUERY_SECURITY_DESC:
2696 START_PROFILE(NT_transact_query_security_desc);
2697 call_nt_transact_query_security_desc(
2698 conn, req,
2699 &state->setup, state->setup_count,
2700 &state->param, state->total_param,
2701 &state->data, state->total_data,
2702 state->max_data_return);
2703 END_PROFILE(NT_transact_query_security_desc);
2704 break;
2707 #ifdef HAVE_SYS_QUOTAS
2708 case NT_TRANSACT_GET_USER_QUOTA:
2710 START_PROFILE(NT_transact_get_user_quota);
2711 call_nt_transact_get_user_quota(
2712 conn, req,
2713 &state->setup, state->setup_count,
2714 &state->param, state->total_param,
2715 &state->data, state->total_data,
2716 state->max_data_return);
2717 END_PROFILE(NT_transact_get_user_quota);
2718 break;
2721 case NT_TRANSACT_SET_USER_QUOTA:
2723 START_PROFILE(NT_transact_set_user_quota);
2724 call_nt_transact_set_user_quota(
2725 conn, req,
2726 &state->setup, state->setup_count,
2727 &state->param, state->total_param,
2728 &state->data, state->total_data,
2729 state->max_data_return);
2730 END_PROFILE(NT_transact_set_user_quota);
2731 break;
2733 #endif /* HAVE_SYS_QUOTAS */
2735 default:
2736 /* Error in request */
2737 DEBUG(0,("handle_nttrans: Unknown request %d in "
2738 "nttrans call\n", state->call));
2739 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2740 return;
2742 return;
2745 /****************************************************************************
2746 Reply to a SMBNTtrans.
2747 ****************************************************************************/
2749 void reply_nttrans(struct smb_request *req)
2751 connection_struct *conn = req->conn;
2752 uint32_t pscnt;
2753 uint32_t psoff;
2754 uint32_t dscnt;
2755 uint32_t dsoff;
2756 uint16 function_code;
2757 NTSTATUS result;
2758 struct trans_state *state;
2760 START_PROFILE(SMBnttrans);
2762 if (req->wct < 19) {
2763 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2764 END_PROFILE(SMBnttrans);
2765 return;
2768 pscnt = IVAL(req->vwv+9, 1);
2769 psoff = IVAL(req->vwv+11, 1);
2770 dscnt = IVAL(req->vwv+13, 1);
2771 dsoff = IVAL(req->vwv+15, 1);
2772 function_code = SVAL(req->vwv+18, 0);
2774 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2775 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2776 END_PROFILE(SMBnttrans);
2777 return;
2780 result = allow_new_trans(conn->pending_trans, req->mid);
2781 if (!NT_STATUS_IS_OK(result)) {
2782 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2783 reply_nterror(req, result);
2784 END_PROFILE(SMBnttrans);
2785 return;
2788 if ((state = talloc(conn, struct trans_state)) == NULL) {
2789 reply_nterror(req, NT_STATUS_NO_MEMORY);
2790 END_PROFILE(SMBnttrans);
2791 return;
2794 state->cmd = SMBnttrans;
2796 state->mid = req->mid;
2797 state->vuid = req->vuid;
2798 state->total_data = IVAL(req->vwv+3, 1);
2799 state->data = NULL;
2800 state->total_param = IVAL(req->vwv+1, 1);
2801 state->param = NULL;
2802 state->max_data_return = IVAL(req->vwv+7, 1);
2803 state->max_param_return = IVAL(req->vwv+5, 1);
2805 /* setup count is in *words* */
2806 state->setup_count = 2*CVAL(req->vwv+17, 1);
2807 state->setup = NULL;
2808 state->call = function_code;
2810 DEBUG(10, ("num_setup=%u, "
2811 "param_total=%u, this_param=%u, max_param=%u, "
2812 "data_total=%u, this_data=%u, max_data=%u, "
2813 "param_offset=%u, data_offset=%u\n",
2814 (unsigned)state->setup_count,
2815 (unsigned)state->total_param, (unsigned)pscnt,
2816 (unsigned)state->max_param_return,
2817 (unsigned)state->total_data, (unsigned)dscnt,
2818 (unsigned)state->max_data_return,
2819 (unsigned)psoff, (unsigned)dsoff));
2822 * All nttrans messages we handle have smb_wct == 19 +
2823 * state->setup_count. Ensure this is so as a sanity check.
2826 if(req->wct != 19 + (state->setup_count/2)) {
2827 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2828 req->wct, 19 + (state->setup_count/2)));
2829 goto bad_param;
2832 /* Don't allow more than 128mb for each value. */
2833 if ((state->total_data > (1024*1024*128)) ||
2834 (state->total_param > (1024*1024*128))) {
2835 reply_nterror(req, NT_STATUS_NO_MEMORY);
2836 END_PROFILE(SMBnttrans);
2837 return;
2840 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2841 goto bad_param;
2843 if (state->total_data) {
2845 if (trans_oob(state->total_data, 0, dscnt)
2846 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2847 goto bad_param;
2850 /* Can't use talloc here, the core routines do realloc on the
2851 * params and data. */
2852 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2853 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2854 "bytes !\n", (unsigned int)state->total_data));
2855 TALLOC_FREE(state);
2856 reply_nterror(req, NT_STATUS_NO_MEMORY);
2857 END_PROFILE(SMBnttrans);
2858 return;
2861 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2864 if (state->total_param) {
2866 if (trans_oob(state->total_param, 0, pscnt)
2867 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2868 goto bad_param;
2871 /* Can't use talloc here, the core routines do realloc on the
2872 * params and data. */
2873 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2874 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2875 "bytes !\n", (unsigned int)state->total_param));
2876 SAFE_FREE(state->data);
2877 TALLOC_FREE(state);
2878 reply_nterror(req, NT_STATUS_NO_MEMORY);
2879 END_PROFILE(SMBnttrans);
2880 return;
2883 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2886 state->received_data = dscnt;
2887 state->received_param = pscnt;
2889 if(state->setup_count > 0) {
2890 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2891 state->setup_count));
2894 * No overflow possible here, state->setup_count is an
2895 * unsigned int, being filled by a single byte from
2896 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2897 * below is not necessary, it's here to clarify things. The
2898 * validity of req->vwv and req->wct has been checked in
2899 * init_smb_request already.
2901 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2902 goto bad_param;
2905 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2906 if (state->setup == NULL) {
2907 DEBUG(0,("reply_nttrans : Out of memory\n"));
2908 SAFE_FREE(state->data);
2909 SAFE_FREE(state->param);
2910 TALLOC_FREE(state);
2911 reply_nterror(req, NT_STATUS_NO_MEMORY);
2912 END_PROFILE(SMBnttrans);
2913 return;
2916 memcpy(state->setup, req->vwv+19, state->setup_count);
2917 dump_data(10, (uint8 *)state->setup, state->setup_count);
2920 if ((state->received_data == state->total_data) &&
2921 (state->received_param == state->total_param)) {
2922 handle_nttrans(conn, state, req);
2923 SAFE_FREE(state->param);
2924 SAFE_FREE(state->data);
2925 TALLOC_FREE(state);
2926 END_PROFILE(SMBnttrans);
2927 return;
2930 DLIST_ADD(conn->pending_trans, state);
2932 /* We need to send an interim response then receive the rest
2933 of the parameter/data bytes */
2934 reply_outbuf(req, 0, 0);
2935 show_msg((char *)req->outbuf);
2936 END_PROFILE(SMBnttrans);
2937 return;
2939 bad_param:
2941 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2942 SAFE_FREE(state->data);
2943 SAFE_FREE(state->param);
2944 TALLOC_FREE(state);
2945 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2946 END_PROFILE(SMBnttrans);
2947 return;
2950 /****************************************************************************
2951 Reply to a SMBnttranss
2952 ****************************************************************************/
2954 void reply_nttranss(struct smb_request *req)
2956 connection_struct *conn = req->conn;
2957 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2958 struct trans_state *state;
2960 START_PROFILE(SMBnttranss);
2962 show_msg((const char *)req->inbuf);
2964 /* Windows clients expect all replies to
2965 an NT transact secondary (SMBnttranss 0xA1)
2966 to have a command code of NT transact
2967 (SMBnttrans 0xA0). See bug #8989 for details. */
2968 req->cmd = SMBnttrans;
2970 if (req->wct < 18) {
2971 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2972 END_PROFILE(SMBnttranss);
2973 return;
2976 for (state = conn->pending_trans; state != NULL;
2977 state = state->next) {
2978 if (state->mid == req->mid) {
2979 break;
2983 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2984 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2985 END_PROFILE(SMBnttranss);
2986 return;
2989 /* Revise state->total_param and state->total_data in case they have
2990 changed downwards */
2991 if (IVAL(req->vwv+1, 1) < state->total_param) {
2992 state->total_param = IVAL(req->vwv+1, 1);
2994 if (IVAL(req->vwv+3, 1) < state->total_data) {
2995 state->total_data = IVAL(req->vwv+3, 1);
2998 pcnt = IVAL(req->vwv+5, 1);
2999 poff = IVAL(req->vwv+7, 1);
3000 pdisp = IVAL(req->vwv+9, 1);
3002 dcnt = IVAL(req->vwv+11, 1);
3003 doff = IVAL(req->vwv+13, 1);
3004 ddisp = IVAL(req->vwv+15, 1);
3006 state->received_param += pcnt;
3007 state->received_data += dcnt;
3009 if ((state->received_data > state->total_data) ||
3010 (state->received_param > state->total_param))
3011 goto bad_param;
3013 if (pcnt) {
3014 if (trans_oob(state->total_param, pdisp, pcnt)
3015 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3016 goto bad_param;
3018 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3021 if (dcnt) {
3022 if (trans_oob(state->total_data, ddisp, dcnt)
3023 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3024 goto bad_param;
3026 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3029 if ((state->received_param < state->total_param) ||
3030 (state->received_data < state->total_data)) {
3031 END_PROFILE(SMBnttranss);
3032 return;
3035 handle_nttrans(conn, state, req);
3037 DLIST_REMOVE(conn->pending_trans, state);
3038 SAFE_FREE(state->data);
3039 SAFE_FREE(state->param);
3040 TALLOC_FREE(state);
3041 END_PROFILE(SMBnttranss);
3042 return;
3044 bad_param:
3046 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3047 DLIST_REMOVE(conn->pending_trans, state);
3048 SAFE_FREE(state->data);
3049 SAFE_FREE(state->param);
3050 TALLOC_FREE(state);
3051 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3052 END_PROFILE(SMBnttranss);
3053 return;