Make sure we initialize conn to NULL, because a routine we call may give an error...
[Samba.git] / source3 / smbd / nttrans.c
blob131009b14c38f47c0531855f61713a69596eb797
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 SMB_SUPPORTED_SECINFO_FLAGS,
2051 max_data_count,
2052 &marshalled_sd,
2053 &sd_size);
2055 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2056 SIVAL(params,0,(uint32_t)sd_size);
2057 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2058 params, 4, NULL, 0);
2059 return;
2062 if (!NT_STATUS_IS_OK(status)) {
2063 reply_nterror(req, status);
2064 return;
2067 SMB_ASSERT(sd_size > 0);
2069 SIVAL(params,0,(uint32_t)sd_size);
2071 if (max_data_count < sd_size) {
2072 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2073 params, 4, NULL, 0);
2074 return;
2078 * Allocate the data we will return.
2081 data = nttrans_realloc(ppdata, sd_size);
2082 if(data == NULL) {
2083 reply_nterror(req, NT_STATUS_NO_MEMORY);
2084 return;
2087 memcpy(data, marshalled_sd, sd_size);
2089 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2091 return;
2094 /****************************************************************************
2095 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2096 ****************************************************************************/
2098 static void call_nt_transact_set_security_desc(connection_struct *conn,
2099 struct smb_request *req,
2100 uint16 **ppsetup,
2101 uint32 setup_count,
2102 char **ppparams,
2103 uint32 parameter_count,
2104 char **ppdata,
2105 uint32 data_count,
2106 uint32 max_data_count)
2108 char *params= *ppparams;
2109 char *data = *ppdata;
2110 files_struct *fsp = NULL;
2111 uint32 security_info_sent = 0;
2112 NTSTATUS status;
2114 if(parameter_count < 8) {
2115 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2116 return;
2119 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2120 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2121 return;
2124 if (!CAN_WRITE(fsp->conn)) {
2125 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2126 return;
2129 if(!lp_nt_acl_support(SNUM(conn))) {
2130 goto done;
2133 security_info_sent = IVAL(params,4);
2135 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2136 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2138 if (data_count == 0) {
2139 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2140 return;
2143 status = set_sd_blob(fsp, (uint8 *)data, data_count,
2144 security_info_sent & SMB_SUPPORTED_SECINFO_FLAGS);
2145 if (!NT_STATUS_IS_OK(status)) {
2146 reply_nterror(req, status);
2147 return;
2150 done:
2151 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2152 return;
2155 /****************************************************************************
2156 Reply to NT IOCTL
2157 ****************************************************************************/
2159 static void call_nt_transact_ioctl(connection_struct *conn,
2160 struct smb_request *req,
2161 uint16 **ppsetup, uint32 setup_count,
2162 char **ppparams, uint32 parameter_count,
2163 char **ppdata, uint32 data_count,
2164 uint32 max_data_count)
2166 NTSTATUS status;
2167 uint32 function;
2168 uint16 fidnum;
2169 files_struct *fsp;
2170 uint8 isFSctl;
2171 uint8 compfilter;
2172 char *out_data = NULL;
2173 uint32 out_data_len = 0;
2174 char *pdata = *ppdata;
2175 TALLOC_CTX *ctx = talloc_tos();
2177 if (setup_count != 8) {
2178 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2179 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2180 return;
2183 function = IVAL(*ppsetup, 0);
2184 fidnum = SVAL(*ppsetup, 4);
2185 isFSctl = CVAL(*ppsetup, 6);
2186 compfilter = CVAL(*ppsetup, 7);
2188 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2189 function, fidnum, isFSctl, compfilter));
2191 fsp=file_fsp(req, fidnum);
2194 * We don't really implement IOCTLs, especially on files.
2196 if (!isFSctl) {
2197 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2198 isFSctl));
2199 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2200 return;
2203 /* Has to be for an open file! */
2204 if (!check_fsp_open(conn, req, fsp)) {
2205 return;
2208 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2211 * out_data might be allocated by the VFS module, but talloc should be
2212 * used, and should be cleaned up when the request ends.
2214 status = SMB_VFS_FSCTL(fsp,
2215 ctx,
2216 function,
2217 req->flags2,
2218 (uint8_t *)pdata,
2219 data_count,
2220 (uint8_t **)&out_data,
2221 max_data_count,
2222 &out_data_len);
2223 if (!NT_STATUS_IS_OK(status)) {
2224 reply_nterror(req, status);
2225 } else {
2226 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2231 #ifdef HAVE_SYS_QUOTAS
2232 /****************************************************************************
2233 Reply to get user quota
2234 ****************************************************************************/
2236 static void call_nt_transact_get_user_quota(connection_struct *conn,
2237 struct smb_request *req,
2238 uint16 **ppsetup,
2239 uint32 setup_count,
2240 char **ppparams,
2241 uint32 parameter_count,
2242 char **ppdata,
2243 uint32 data_count,
2244 uint32 max_data_count)
2246 NTSTATUS nt_status = NT_STATUS_OK;
2247 char *params = *ppparams;
2248 char *pdata = *ppdata;
2249 char *entry;
2250 int data_len=0,param_len=0;
2251 int qt_len=0;
2252 int entry_len = 0;
2253 files_struct *fsp = NULL;
2254 uint16 level = 0;
2255 size_t sid_len;
2256 struct dom_sid sid;
2257 bool start_enum = True;
2258 SMB_NTQUOTA_STRUCT qt;
2259 SMB_NTQUOTA_LIST *tmp_list;
2260 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2262 ZERO_STRUCT(qt);
2264 /* access check */
2265 if (get_current_uid(conn) != 0) {
2266 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2267 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2268 conn->session_info->unix_info->unix_name));
2269 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2270 return;
2274 * Ensure minimum number of parameters sent.
2277 if (parameter_count < 4) {
2278 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2279 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2280 return;
2283 /* maybe we can check the quota_fnum */
2284 fsp = file_fsp(req, SVAL(params,0));
2285 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2286 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2287 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2288 return;
2291 /* the NULL pointer checking for fsp->fake_file_handle->pd
2292 * is done by CHECK_NTQUOTA_HANDLE_OK()
2294 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2296 level = SVAL(params,2);
2298 /* unknown 12 bytes leading in params */
2300 switch (level) {
2301 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2302 /* seems that we should continue with the enum here --metze */
2304 if (qt_handle->quota_list!=NULL &&
2305 qt_handle->tmp_list==NULL) {
2307 /* free the list */
2308 free_ntquota_list(&(qt_handle->quota_list));
2310 /* Realloc the size of parameters and data we will return */
2311 param_len = 4;
2312 params = nttrans_realloc(ppparams, param_len);
2313 if(params == NULL) {
2314 reply_nterror(req, NT_STATUS_NO_MEMORY);
2315 return;
2318 data_len = 0;
2319 SIVAL(params,0,data_len);
2321 break;
2324 start_enum = False;
2326 case TRANSACT_GET_USER_QUOTA_LIST_START:
2328 if (qt_handle->quota_list==NULL &&
2329 qt_handle->tmp_list==NULL) {
2330 start_enum = True;
2333 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2334 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2335 return;
2338 /* Realloc the size of parameters and data we will return */
2339 param_len = 4;
2340 params = nttrans_realloc(ppparams, param_len);
2341 if(params == NULL) {
2342 reply_nterror(req, NT_STATUS_NO_MEMORY);
2343 return;
2346 /* we should not trust the value in max_data_count*/
2347 max_data_count = MIN(max_data_count,2048);
2349 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2350 if(pdata == NULL) {
2351 reply_nterror(req, NT_STATUS_NO_MEMORY);
2352 return;
2355 entry = pdata;
2357 /* set params Size of returned Quota Data 4 bytes*/
2358 /* but set it later when we know it */
2360 /* for each entry push the data */
2362 if (start_enum) {
2363 qt_handle->tmp_list = qt_handle->quota_list;
2366 tmp_list = qt_handle->tmp_list;
2368 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2369 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2371 sid_len = ndr_size_dom_sid(
2372 &tmp_list->quotas->sid, 0);
2373 entry_len = 40 + sid_len;
2375 /* nextoffset entry 4 bytes */
2376 SIVAL(entry,0,entry_len);
2378 /* then the len of the SID 4 bytes */
2379 SIVAL(entry,4,sid_len);
2381 /* unknown data 8 bytes uint64_t */
2382 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2384 /* the used disk space 8 bytes uint64_t */
2385 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2387 /* the soft quotas 8 bytes uint64_t */
2388 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2390 /* the hard quotas 8 bytes uint64_t */
2391 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2393 /* and now the SID */
2394 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2397 qt_handle->tmp_list = tmp_list;
2399 /* overwrite the offset of the last entry */
2400 SIVAL(entry-entry_len,0,0);
2402 data_len = 4+qt_len;
2403 /* overwrite the params quota_data_len */
2404 SIVAL(params,0,data_len);
2406 break;
2408 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2410 /* unknown 4 bytes IVAL(pdata,0) */
2412 if (data_count < 8) {
2413 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2414 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2415 return;
2418 sid_len = IVAL(pdata,4);
2419 /* Ensure this is less than 1mb. */
2420 if (sid_len > (1024*1024)) {
2421 reply_nterror(req, NT_STATUS_NO_MEMORY);
2422 return;
2425 if (data_count < 8+sid_len) {
2426 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2427 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2428 return;
2431 data_len = 4+40+sid_len;
2433 if (max_data_count < data_len) {
2434 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2435 max_data_count, data_len));
2436 param_len = 4;
2437 SIVAL(params,0,data_len);
2438 data_len = 0;
2439 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2440 break;
2443 if (!sid_parse(pdata+8,sid_len,&sid)) {
2444 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2445 return;
2448 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2449 ZERO_STRUCT(qt);
2451 * we have to return zero's in all fields
2452 * instead of returning an error here
2453 * --metze
2457 /* Realloc the size of parameters and data we will return */
2458 param_len = 4;
2459 params = nttrans_realloc(ppparams, param_len);
2460 if(params == NULL) {
2461 reply_nterror(req, NT_STATUS_NO_MEMORY);
2462 return;
2465 pdata = nttrans_realloc(ppdata, data_len);
2466 if(pdata == NULL) {
2467 reply_nterror(req, NT_STATUS_NO_MEMORY);
2468 return;
2471 entry = pdata;
2473 /* set params Size of returned Quota Data 4 bytes*/
2474 SIVAL(params,0,data_len);
2476 /* nextoffset entry 4 bytes */
2477 SIVAL(entry,0,0);
2479 /* then the len of the SID 4 bytes */
2480 SIVAL(entry,4,sid_len);
2482 /* unknown data 8 bytes uint64_t */
2483 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2485 /* the used disk space 8 bytes uint64_t */
2486 SBIG_UINT(entry,16,qt.usedspace);
2488 /* the soft quotas 8 bytes uint64_t */
2489 SBIG_UINT(entry,24,qt.softlim);
2491 /* the hard quotas 8 bytes uint64_t */
2492 SBIG_UINT(entry,32,qt.hardlim);
2494 /* and now the SID */
2495 sid_linearize(entry+40, sid_len, &sid);
2497 break;
2499 default:
2500 DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
2501 "level 0x%04hX\n",
2502 fsp_fnum_dbg(fsp), level));
2503 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2504 return;
2505 break;
2508 send_nt_replies(conn, req, nt_status, params, param_len,
2509 pdata, data_len);
2512 /****************************************************************************
2513 Reply to set user quota
2514 ****************************************************************************/
2516 static void call_nt_transact_set_user_quota(connection_struct *conn,
2517 struct smb_request *req,
2518 uint16 **ppsetup,
2519 uint32 setup_count,
2520 char **ppparams,
2521 uint32 parameter_count,
2522 char **ppdata,
2523 uint32 data_count,
2524 uint32 max_data_count)
2526 char *params = *ppparams;
2527 char *pdata = *ppdata;
2528 int data_len=0,param_len=0;
2529 SMB_NTQUOTA_STRUCT qt;
2530 size_t sid_len;
2531 struct dom_sid sid;
2532 files_struct *fsp = NULL;
2534 ZERO_STRUCT(qt);
2536 /* access check */
2537 if (get_current_uid(conn) != 0) {
2538 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2539 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2540 conn->session_info->unix_info->unix_name));
2541 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2542 return;
2546 * Ensure minimum number of parameters sent.
2549 if (parameter_count < 2) {
2550 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2551 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2552 return;
2555 /* maybe we can check the quota_fnum */
2556 fsp = file_fsp(req, SVAL(params,0));
2557 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2558 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2559 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2560 return;
2563 if (data_count < 40) {
2564 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2565 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2566 return;
2569 /* offset to next quota record.
2570 * 4 bytes IVAL(pdata,0)
2571 * unused here...
2574 /* sid len */
2575 sid_len = IVAL(pdata,4);
2577 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2578 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2579 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2580 return;
2583 /* unknown 8 bytes in pdata
2584 * maybe its the change time in NTTIME
2587 /* the used space 8 bytes (uint64_t)*/
2588 qt.usedspace = BVAL(pdata,16);
2590 /* the soft quotas 8 bytes (uint64_t)*/
2591 qt.softlim = BVAL(pdata,24);
2593 /* the hard quotas 8 bytes (uint64_t)*/
2594 qt.hardlim = BVAL(pdata,32);
2596 if (!sid_parse(pdata+40,sid_len,&sid)) {
2597 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2598 return;
2601 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2603 /* 44 unknown bytes left... */
2605 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2606 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2607 return;
2610 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2611 pdata, data_len);
2613 #endif /* HAVE_SYS_QUOTAS */
2615 static void handle_nttrans(connection_struct *conn,
2616 struct trans_state *state,
2617 struct smb_request *req)
2619 if (get_Protocol() >= PROTOCOL_NT1) {
2620 req->flags2 |= 0x40; /* IS_LONG_NAME */
2621 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2625 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2627 /* Now we must call the relevant NT_TRANS function */
2628 switch(state->call) {
2629 case NT_TRANSACT_CREATE:
2631 START_PROFILE(NT_transact_create);
2632 call_nt_transact_create(
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 END_PROFILE(NT_transact_create);
2639 break;
2642 case NT_TRANSACT_IOCTL:
2644 START_PROFILE(NT_transact_ioctl);
2645 call_nt_transact_ioctl(
2646 conn, req,
2647 &state->setup, state->setup_count,
2648 &state->param, state->total_param,
2649 &state->data, state->total_data,
2650 state->max_data_return);
2651 END_PROFILE(NT_transact_ioctl);
2652 break;
2655 case NT_TRANSACT_SET_SECURITY_DESC:
2657 START_PROFILE(NT_transact_set_security_desc);
2658 call_nt_transact_set_security_desc(
2659 conn, req,
2660 &state->setup, state->setup_count,
2661 &state->param, state->total_param,
2662 &state->data, state->total_data,
2663 state->max_data_return);
2664 END_PROFILE(NT_transact_set_security_desc);
2665 break;
2668 case NT_TRANSACT_NOTIFY_CHANGE:
2670 START_PROFILE(NT_transact_notify_change);
2671 call_nt_transact_notify_change(
2672 conn, req,
2673 &state->setup, state->setup_count,
2674 &state->param, state->total_param,
2675 &state->data, state->total_data,
2676 state->max_data_return,
2677 state->max_param_return);
2678 END_PROFILE(NT_transact_notify_change);
2679 break;
2682 case NT_TRANSACT_RENAME:
2684 START_PROFILE(NT_transact_rename);
2685 call_nt_transact_rename(
2686 conn, req,
2687 &state->setup, state->setup_count,
2688 &state->param, state->total_param,
2689 &state->data, state->total_data,
2690 state->max_data_return);
2691 END_PROFILE(NT_transact_rename);
2692 break;
2695 case NT_TRANSACT_QUERY_SECURITY_DESC:
2697 START_PROFILE(NT_transact_query_security_desc);
2698 call_nt_transact_query_security_desc(
2699 conn, req,
2700 &state->setup, state->setup_count,
2701 &state->param, state->total_param,
2702 &state->data, state->total_data,
2703 state->max_data_return);
2704 END_PROFILE(NT_transact_query_security_desc);
2705 break;
2708 #ifdef HAVE_SYS_QUOTAS
2709 case NT_TRANSACT_GET_USER_QUOTA:
2711 START_PROFILE(NT_transact_get_user_quota);
2712 call_nt_transact_get_user_quota(
2713 conn, req,
2714 &state->setup, state->setup_count,
2715 &state->param, state->total_param,
2716 &state->data, state->total_data,
2717 state->max_data_return);
2718 END_PROFILE(NT_transact_get_user_quota);
2719 break;
2722 case NT_TRANSACT_SET_USER_QUOTA:
2724 START_PROFILE(NT_transact_set_user_quota);
2725 call_nt_transact_set_user_quota(
2726 conn, req,
2727 &state->setup, state->setup_count,
2728 &state->param, state->total_param,
2729 &state->data, state->total_data,
2730 state->max_data_return);
2731 END_PROFILE(NT_transact_set_user_quota);
2732 break;
2734 #endif /* HAVE_SYS_QUOTAS */
2736 default:
2737 /* Error in request */
2738 DEBUG(0,("handle_nttrans: Unknown request %d in "
2739 "nttrans call\n", state->call));
2740 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2741 return;
2743 return;
2746 /****************************************************************************
2747 Reply to a SMBNTtrans.
2748 ****************************************************************************/
2750 void reply_nttrans(struct smb_request *req)
2752 connection_struct *conn = req->conn;
2753 uint32_t pscnt;
2754 uint32_t psoff;
2755 uint32_t dscnt;
2756 uint32_t dsoff;
2757 uint16 function_code;
2758 NTSTATUS result;
2759 struct trans_state *state;
2761 START_PROFILE(SMBnttrans);
2763 if (req->wct < 19) {
2764 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2765 END_PROFILE(SMBnttrans);
2766 return;
2769 pscnt = IVAL(req->vwv+9, 1);
2770 psoff = IVAL(req->vwv+11, 1);
2771 dscnt = IVAL(req->vwv+13, 1);
2772 dsoff = IVAL(req->vwv+15, 1);
2773 function_code = SVAL(req->vwv+18, 0);
2775 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2776 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2777 END_PROFILE(SMBnttrans);
2778 return;
2781 result = allow_new_trans(conn->pending_trans, req->mid);
2782 if (!NT_STATUS_IS_OK(result)) {
2783 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2784 reply_nterror(req, result);
2785 END_PROFILE(SMBnttrans);
2786 return;
2789 if ((state = talloc(conn, struct trans_state)) == NULL) {
2790 reply_nterror(req, NT_STATUS_NO_MEMORY);
2791 END_PROFILE(SMBnttrans);
2792 return;
2795 state->cmd = SMBnttrans;
2797 state->mid = req->mid;
2798 state->vuid = req->vuid;
2799 state->total_data = IVAL(req->vwv+3, 1);
2800 state->data = NULL;
2801 state->total_param = IVAL(req->vwv+1, 1);
2802 state->param = NULL;
2803 state->max_data_return = IVAL(req->vwv+7, 1);
2804 state->max_param_return = IVAL(req->vwv+5, 1);
2806 /* setup count is in *words* */
2807 state->setup_count = 2*CVAL(req->vwv+17, 1);
2808 state->setup = NULL;
2809 state->call = function_code;
2811 DEBUG(10, ("num_setup=%u, "
2812 "param_total=%u, this_param=%u, max_param=%u, "
2813 "data_total=%u, this_data=%u, max_data=%u, "
2814 "param_offset=%u, data_offset=%u\n",
2815 (unsigned)state->setup_count,
2816 (unsigned)state->total_param, (unsigned)pscnt,
2817 (unsigned)state->max_param_return,
2818 (unsigned)state->total_data, (unsigned)dscnt,
2819 (unsigned)state->max_data_return,
2820 (unsigned)psoff, (unsigned)dsoff));
2823 * All nttrans messages we handle have smb_wct == 19 +
2824 * state->setup_count. Ensure this is so as a sanity check.
2827 if(req->wct != 19 + (state->setup_count/2)) {
2828 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2829 req->wct, 19 + (state->setup_count/2)));
2830 goto bad_param;
2833 /* Don't allow more than 128mb for each value. */
2834 if ((state->total_data > (1024*1024*128)) ||
2835 (state->total_param > (1024*1024*128))) {
2836 reply_nterror(req, NT_STATUS_NO_MEMORY);
2837 END_PROFILE(SMBnttrans);
2838 return;
2841 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2842 goto bad_param;
2844 if (state->total_data) {
2846 if (trans_oob(state->total_data, 0, dscnt)
2847 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2848 goto bad_param;
2851 /* Can't use talloc here, the core routines do realloc on the
2852 * params and data. */
2853 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2854 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2855 "bytes !\n", (unsigned int)state->total_data));
2856 TALLOC_FREE(state);
2857 reply_nterror(req, NT_STATUS_NO_MEMORY);
2858 END_PROFILE(SMBnttrans);
2859 return;
2862 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2865 if (state->total_param) {
2867 if (trans_oob(state->total_param, 0, pscnt)
2868 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2869 goto bad_param;
2872 /* Can't use talloc here, the core routines do realloc on the
2873 * params and data. */
2874 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2875 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2876 "bytes !\n", (unsigned int)state->total_param));
2877 SAFE_FREE(state->data);
2878 TALLOC_FREE(state);
2879 reply_nterror(req, NT_STATUS_NO_MEMORY);
2880 END_PROFILE(SMBnttrans);
2881 return;
2884 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2887 state->received_data = dscnt;
2888 state->received_param = pscnt;
2890 if(state->setup_count > 0) {
2891 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2892 state->setup_count));
2895 * No overflow possible here, state->setup_count is an
2896 * unsigned int, being filled by a single byte from
2897 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2898 * below is not necessary, it's here to clarify things. The
2899 * validity of req->vwv and req->wct has been checked in
2900 * init_smb_request already.
2902 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2903 goto bad_param;
2906 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2907 if (state->setup == NULL) {
2908 DEBUG(0,("reply_nttrans : Out of memory\n"));
2909 SAFE_FREE(state->data);
2910 SAFE_FREE(state->param);
2911 TALLOC_FREE(state);
2912 reply_nterror(req, NT_STATUS_NO_MEMORY);
2913 END_PROFILE(SMBnttrans);
2914 return;
2917 memcpy(state->setup, req->vwv+19, state->setup_count);
2918 dump_data(10, (uint8 *)state->setup, state->setup_count);
2921 if ((state->received_data == state->total_data) &&
2922 (state->received_param == state->total_param)) {
2923 handle_nttrans(conn, state, req);
2924 SAFE_FREE(state->param);
2925 SAFE_FREE(state->data);
2926 TALLOC_FREE(state);
2927 END_PROFILE(SMBnttrans);
2928 return;
2931 DLIST_ADD(conn->pending_trans, state);
2933 /* We need to send an interim response then receive the rest
2934 of the parameter/data bytes */
2935 reply_outbuf(req, 0, 0);
2936 show_msg((char *)req->outbuf);
2937 END_PROFILE(SMBnttrans);
2938 return;
2940 bad_param:
2942 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2943 SAFE_FREE(state->data);
2944 SAFE_FREE(state->param);
2945 TALLOC_FREE(state);
2946 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2947 END_PROFILE(SMBnttrans);
2948 return;
2951 /****************************************************************************
2952 Reply to a SMBnttranss
2953 ****************************************************************************/
2955 void reply_nttranss(struct smb_request *req)
2957 connection_struct *conn = req->conn;
2958 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2959 struct trans_state *state;
2961 START_PROFILE(SMBnttranss);
2963 show_msg((const char *)req->inbuf);
2965 /* Windows clients expect all replies to
2966 an NT transact secondary (SMBnttranss 0xA1)
2967 to have a command code of NT transact
2968 (SMBnttrans 0xA0). See bug #8989 for details. */
2969 req->cmd = SMBnttrans;
2971 if (req->wct < 18) {
2972 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2973 END_PROFILE(SMBnttranss);
2974 return;
2977 for (state = conn->pending_trans; state != NULL;
2978 state = state->next) {
2979 if (state->mid == req->mid) {
2980 break;
2984 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2985 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2986 END_PROFILE(SMBnttranss);
2987 return;
2990 /* Revise state->total_param and state->total_data in case they have
2991 changed downwards */
2992 if (IVAL(req->vwv+1, 1) < state->total_param) {
2993 state->total_param = IVAL(req->vwv+1, 1);
2995 if (IVAL(req->vwv+3, 1) < state->total_data) {
2996 state->total_data = IVAL(req->vwv+3, 1);
2999 pcnt = IVAL(req->vwv+5, 1);
3000 poff = IVAL(req->vwv+7, 1);
3001 pdisp = IVAL(req->vwv+9, 1);
3003 dcnt = IVAL(req->vwv+11, 1);
3004 doff = IVAL(req->vwv+13, 1);
3005 ddisp = IVAL(req->vwv+15, 1);
3007 state->received_param += pcnt;
3008 state->received_data += dcnt;
3010 if ((state->received_data > state->total_data) ||
3011 (state->received_param > state->total_param))
3012 goto bad_param;
3014 if (pcnt) {
3015 if (trans_oob(state->total_param, pdisp, pcnt)
3016 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3017 goto bad_param;
3019 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3022 if (dcnt) {
3023 if (trans_oob(state->total_data, ddisp, dcnt)
3024 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3025 goto bad_param;
3027 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3030 if ((state->received_param < state->total_param) ||
3031 (state->received_data < state->total_data)) {
3032 END_PROFILE(SMBnttranss);
3033 return;
3036 handle_nttrans(conn, state, req);
3038 DLIST_REMOVE(conn->pending_trans, state);
3039 SAFE_FREE(state->data);
3040 SAFE_FREE(state->param);
3041 TALLOC_FREE(state);
3042 END_PROFILE(SMBnttranss);
3043 return;
3045 bad_param:
3047 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3048 DLIST_REMOVE(conn->pending_trans, state);
3049 SAFE_FREE(state->data);
3050 SAFE_FREE(state->param);
3051 TALLOC_FREE(state);
3052 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3053 END_PROFILE(SMBnttranss);
3054 return;