Move FSCTL handling into the VFS. Initial code changes. Passes smbtorture NTTRANS...
[Samba/gebeck_regimport.git] / source3 / smbd / nttrans.c
blobc69aa6ae2fdb2551bb591026b45ea102c77e5548
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 "ntioctl.h"
31 #include "smbprofile.h"
32 #include "libsmb/libsmb.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 * We might have had SMBnttranss in req->inbuf, fix that.
149 SCVAL(req->outbuf, smb_com, SMBnttrans);
152 * Set total params and data to be sent.
155 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
156 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
159 * Calculate how many parameters and data we can fit into
160 * this packet. Parameters get precedence.
163 params_sent_thistime = MIN(params_to_send,useable_space);
164 data_sent_thistime = useable_space - params_sent_thistime;
165 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
167 SIVAL(req->outbuf, smb_ntr_ParameterCount,
168 params_sent_thistime);
170 if(params_sent_thistime == 0) {
171 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
172 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
173 } else {
175 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
176 * parameter bytes, however the first 4 bytes of outbuf are
177 * the Netbios over TCP header. Thus use smb_base() to subtract
178 * them from the calculation.
181 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
182 ((smb_buf(req->outbuf)+alignment_offset)
183 - smb_base(req->outbuf)));
185 * Absolute displacement of param bytes sent in this packet.
188 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
189 pp - params);
193 * Deal with the data portion.
196 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
198 if(data_sent_thistime == 0) {
199 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
200 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
201 } else {
203 * The offset of the data bytes is the offset of the
204 * parameter bytes plus the number of parameters being sent this time.
207 SIVAL(req->outbuf, smb_ntr_DataOffset,
208 ((smb_buf(req->outbuf)+alignment_offset) -
209 smb_base(req->outbuf))
210 + params_sent_thistime + data_alignment_offset);
211 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
215 * Copy the param bytes into the packet.
218 if(params_sent_thistime) {
219 if (alignment_offset != 0) {
220 memset(smb_buf(req->outbuf), 0,
221 alignment_offset);
223 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
224 params_sent_thistime);
228 * Copy in the data bytes
231 if(data_sent_thistime) {
232 if (data_alignment_offset != 0) {
233 memset((smb_buf(req->outbuf)+alignment_offset+
234 params_sent_thistime), 0,
235 data_alignment_offset);
237 memcpy(smb_buf(req->outbuf)+alignment_offset
238 +params_sent_thistime+data_alignment_offset,
239 pd,data_sent_thistime);
242 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
243 params_sent_thistime, data_sent_thistime, useable_space));
244 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
245 params_to_send, data_to_send, paramsize, datasize));
247 if (NT_STATUS_V(nt_error)) {
248 error_packet_set((char *)req->outbuf,
249 0, 0, nt_error,
250 __LINE__,__FILE__);
253 /* Send the packet */
254 show_msg((char *)req->outbuf);
255 if (!srv_send_smb(sconn,
256 (char *)req->outbuf,
257 true, req->seqnum+1,
258 IS_CONN_ENCRYPTED(conn),
259 &req->pcd)) {
260 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
263 TALLOC_FREE(req->outbuf);
265 pp += params_sent_thistime;
266 pd += data_sent_thistime;
268 params_to_send -= params_sent_thistime;
269 data_to_send -= data_sent_thistime;
272 * Sanity check
275 if(params_to_send < 0 || data_to_send < 0) {
276 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
277 params_to_send, data_to_send));
278 exit_server_cleanly("send_nt_replies: internal error");
283 /****************************************************************************
284 Reply to an NT create and X call on a pipe
285 ****************************************************************************/
287 static void nt_open_pipe(char *fname, connection_struct *conn,
288 struct smb_request *req, int *ppnum)
290 files_struct *fsp;
291 NTSTATUS status;
293 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
295 /* Strip \\ off the name. */
296 fname++;
298 status = open_np_file(req, fname, &fsp);
299 if (!NT_STATUS_IS_OK(status)) {
300 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
301 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
302 ERRDOS, ERRbadpipe);
303 return;
305 reply_nterror(req, status);
306 return;
309 *ppnum = fsp->fnum;
310 return;
313 /****************************************************************************
314 Reply to an NT create and X call for pipes.
315 ****************************************************************************/
317 static void do_ntcreate_pipe_open(connection_struct *conn,
318 struct smb_request *req)
320 char *fname = NULL;
321 int pnum = -1;
322 char *p = NULL;
323 uint32 flags = IVAL(req->vwv+3, 1);
324 TALLOC_CTX *ctx = talloc_tos();
326 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
328 if (!fname) {
329 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
330 ERRDOS, ERRbadpipe);
331 return;
333 nt_open_pipe(fname, conn, req, &pnum);
335 if (req->outbuf) {
336 /* error reply */
337 return;
341 * Deal with pipe return.
344 if (flags & EXTENDED_RESPONSE_REQUIRED) {
345 /* This is very strange. We
346 * return 50 words, but only set
347 * the wcnt to 42 ? It's definately
348 * what happens on the wire....
350 reply_outbuf(req, 50, 0);
351 SCVAL(req->outbuf,smb_wct,42);
352 } else {
353 reply_outbuf(req, 34, 0);
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));
384 chain_reply(req);
387 struct case_semantics_state {
388 connection_struct *conn;
389 bool case_sensitive;
390 bool case_preserve;
391 bool short_case_preserve;
394 /****************************************************************************
395 Restore case semantics.
396 ****************************************************************************/
398 static int restore_case_semantics(struct case_semantics_state *state)
400 state->conn->case_sensitive = state->case_sensitive;
401 state->conn->case_preserve = state->case_preserve;
402 state->conn->short_case_preserve = state->short_case_preserve;
403 return 0;
406 /****************************************************************************
407 Save case semantics.
408 ****************************************************************************/
410 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
411 connection_struct *conn)
413 struct case_semantics_state *result;
415 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
416 return NULL;
419 result->conn = conn;
420 result->case_sensitive = conn->case_sensitive;
421 result->case_preserve = conn->case_preserve;
422 result->short_case_preserve = conn->short_case_preserve;
424 /* Set to POSIX. */
425 conn->case_sensitive = True;
426 conn->case_preserve = True;
427 conn->short_case_preserve = True;
429 talloc_set_destructor(result, restore_case_semantics);
431 return result;
434 /****************************************************************************
435 Reply to an NT create and X call.
436 ****************************************************************************/
438 void reply_ntcreate_and_X(struct smb_request *req)
440 connection_struct *conn = req->conn;
441 struct smb_filename *smb_fname = NULL;
442 char *fname = NULL;
443 uint32 flags;
444 uint32 access_mask;
445 uint32 file_attributes;
446 uint32 share_access;
447 uint32 create_disposition;
448 uint32 create_options;
449 uint16 root_dir_fid;
450 uint64_t allocation_size;
451 /* Breakout the oplock request bits so we can set the
452 reply bits separately. */
453 uint32 fattr=0;
454 SMB_OFF_T file_len = 0;
455 int info = 0;
456 files_struct *fsp = NULL;
457 char *p = NULL;
458 struct timespec create_timespec;
459 struct timespec c_timespec;
460 struct timespec a_timespec;
461 struct timespec m_timespec;
462 struct timespec write_time_ts;
463 NTSTATUS status;
464 int oplock_request;
465 uint8_t oplock_granted = NO_OPLOCK_RETURN;
466 struct case_semantics_state *case_state = NULL;
467 TALLOC_CTX *ctx = talloc_tos();
469 START_PROFILE(SMBntcreateX);
471 if (req->wct < 24) {
472 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
473 goto out;
476 flags = IVAL(req->vwv+3, 1);
477 access_mask = IVAL(req->vwv+7, 1);
478 file_attributes = IVAL(req->vwv+13, 1);
479 share_access = IVAL(req->vwv+15, 1);
480 create_disposition = IVAL(req->vwv+17, 1);
481 create_options = IVAL(req->vwv+19, 1);
482 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
484 allocation_size = BVAL(req->vwv+9, 1);
486 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
487 STR_TERMINATE, &status);
489 if (!NT_STATUS_IS_OK(status)) {
490 reply_nterror(req, status);
491 goto out;
494 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
495 "file_attributes = 0x%x, share_access = 0x%x, "
496 "create_disposition = 0x%x create_options = 0x%x "
497 "root_dir_fid = 0x%x, fname = %s\n",
498 (unsigned int)flags,
499 (unsigned int)access_mask,
500 (unsigned int)file_attributes,
501 (unsigned int)share_access,
502 (unsigned int)create_disposition,
503 (unsigned int)create_options,
504 (unsigned int)root_dir_fid,
505 fname));
508 * we need to remove ignored bits when they come directly from the client
509 * because we reuse some of them for internal stuff
511 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
514 * If it's an IPC, use the pipe handler.
517 if (IS_IPC(conn)) {
518 if (lp_nt_pipe_support()) {
519 do_ntcreate_pipe_open(conn, req);
520 goto out;
522 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
523 goto out;
526 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
527 if (oplock_request) {
528 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
529 ? BATCH_OPLOCK : 0;
532 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
533 case_state = set_posix_case_semantics(ctx, conn);
534 if (!case_state) {
535 reply_nterror(req, NT_STATUS_NO_MEMORY);
536 goto out;
540 status = filename_convert(ctx,
541 conn,
542 req->flags2 & FLAGS2_DFS_PATHNAMES,
543 fname,
545 NULL,
546 &smb_fname);
548 TALLOC_FREE(case_state);
550 if (!NT_STATUS_IS_OK(status)) {
551 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
552 reply_botherror(req,
553 NT_STATUS_PATH_NOT_COVERED,
554 ERRSRV, ERRbadpath);
555 goto out;
557 reply_nterror(req, status);
558 goto out;
562 * Bug #6898 - clients using Windows opens should
563 * never be able to set this attribute into the
564 * VFS.
566 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
568 status = SMB_VFS_CREATE_FILE(
569 conn, /* conn */
570 req, /* req */
571 root_dir_fid, /* root_dir_fid */
572 smb_fname, /* fname */
573 access_mask, /* access_mask */
574 share_access, /* share_access */
575 create_disposition, /* create_disposition*/
576 create_options, /* create_options */
577 file_attributes, /* file_attributes */
578 oplock_request, /* oplock_request */
579 allocation_size, /* allocation_size */
580 0, /* private_flags */
581 NULL, /* sd */
582 NULL, /* ea_list */
583 &fsp, /* result */
584 &info); /* pinfo */
586 if (!NT_STATUS_IS_OK(status)) {
587 if (open_was_deferred(req->sconn, req->mid)) {
588 /* We have re-scheduled this call, no error. */
589 goto out;
591 reply_openerror(req, status);
592 goto out;
595 /* Ensure we're pointing at the correct stat struct. */
596 TALLOC_FREE(smb_fname);
597 smb_fname = fsp->fsp_name;
600 * If the caller set the extended oplock request bit
601 * and we granted one (by whatever means) - set the
602 * correct bit for extended oplock reply.
605 if (oplock_request &&
606 (lp_fake_oplocks(SNUM(conn))
607 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
610 * Exclusive oplock granted
613 if (flags & REQUEST_BATCH_OPLOCK) {
614 oplock_granted = BATCH_OPLOCK_RETURN;
615 } else {
616 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
618 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
619 oplock_granted = LEVEL_II_OPLOCK_RETURN;
620 } else {
621 oplock_granted = NO_OPLOCK_RETURN;
624 file_len = smb_fname->st.st_ex_size;
626 if (flags & EXTENDED_RESPONSE_REQUIRED) {
627 /* This is very strange. We
628 * return 50 words, but only set
629 * the wcnt to 42 ? It's definately
630 * what happens on the wire....
632 reply_outbuf(req, 50, 0);
633 SCVAL(req->outbuf,smb_wct,42);
634 } else {
635 reply_outbuf(req, 34, 0);
638 p = (char *)req->outbuf + smb_vwv2;
640 SCVAL(p, 0, oplock_granted);
642 p++;
643 SSVAL(p,0,fsp->fnum);
644 p += 2;
645 if ((create_disposition == FILE_SUPERSEDE)
646 && (info == FILE_WAS_OVERWRITTEN)) {
647 SIVAL(p,0,FILE_WAS_SUPERSEDED);
648 } else {
649 SIVAL(p,0,info);
651 p += 4;
653 fattr = dos_mode(conn, smb_fname);
654 if (fattr == 0) {
655 fattr = FILE_ATTRIBUTE_NORMAL;
658 /* Deal with other possible opens having a modified
659 write time. JRA. */
660 ZERO_STRUCT(write_time_ts);
661 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
662 if (!null_timespec(write_time_ts)) {
663 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
666 /* Create time. */
667 create_timespec = get_create_timespec(conn, fsp, smb_fname);
668 a_timespec = smb_fname->st.st_ex_atime;
669 m_timespec = smb_fname->st.st_ex_mtime;
670 c_timespec = get_change_timespec(conn, fsp, smb_fname);
672 if (lp_dos_filetime_resolution(SNUM(conn))) {
673 dos_filetime_timespec(&create_timespec);
674 dos_filetime_timespec(&a_timespec);
675 dos_filetime_timespec(&m_timespec);
676 dos_filetime_timespec(&c_timespec);
679 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
680 p += 8;
681 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
682 p += 8;
683 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
684 p += 8;
685 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
686 p += 8;
687 SIVAL(p,0,fattr); /* File Attributes. */
688 p += 4;
689 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
690 p += 8;
691 SOFF_T(p,0,file_len);
692 p += 8;
693 if (flags & EXTENDED_RESPONSE_REQUIRED) {
694 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
695 size_t num_names = 0;
696 unsigned int num_streams;
697 struct stream_struct *streams = NULL;
699 /* Do we have any EA's ? */
700 status = get_ea_names_from_file(ctx, conn, fsp,
701 smb_fname->base_name, NULL, &num_names);
702 if (NT_STATUS_IS_OK(status) && num_names) {
703 file_status &= ~NO_EAS;
705 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
706 &num_streams, &streams);
707 /* There is always one stream, ::$DATA. */
708 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
709 file_status &= ~NO_SUBSTREAMS;
711 TALLOC_FREE(streams);
712 SSVAL(p,2,file_status);
714 p += 4;
715 SCVAL(p,0,fsp->is_directory ? 1 : 0);
717 if (flags & EXTENDED_RESPONSE_REQUIRED) {
718 uint32 perms = 0;
719 p += 25;
720 if (fsp->is_directory ||
721 can_write_to_file(conn, smb_fname)) {
722 perms = FILE_GENERIC_ALL;
723 } else {
724 perms = FILE_GENERIC_READ|FILE_EXECUTE;
726 SIVAL(p,0,perms);
729 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
730 fsp->fnum, smb_fname_str_dbg(smb_fname)));
732 chain_reply(req);
733 out:
734 END_PROFILE(SMBntcreateX);
735 return;
738 /****************************************************************************
739 Reply to a NT_TRANSACT_CREATE call to open a pipe.
740 ****************************************************************************/
742 static void do_nt_transact_create_pipe(connection_struct *conn,
743 struct smb_request *req,
744 uint16 **ppsetup, uint32 setup_count,
745 char **ppparams, uint32 parameter_count,
746 char **ppdata, uint32 data_count)
748 char *fname = NULL;
749 char *params = *ppparams;
750 int pnum = -1;
751 char *p = NULL;
752 NTSTATUS status;
753 size_t param_len;
754 uint32 flags;
755 TALLOC_CTX *ctx = talloc_tos();
758 * Ensure minimum number of parameters sent.
761 if(parameter_count < 54) {
762 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
763 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
764 return;
767 flags = IVAL(params,0);
769 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
770 parameter_count-53, STR_TERMINATE,
771 &status);
772 if (!NT_STATUS_IS_OK(status)) {
773 reply_nterror(req, status);
774 return;
777 nt_open_pipe(fname, conn, req, &pnum);
779 if (req->outbuf) {
780 /* Error return */
781 return;
784 /* Realloc the size of parameters and data we will return */
785 if (flags & EXTENDED_RESPONSE_REQUIRED) {
786 /* Extended response is 32 more byyes. */
787 param_len = 101;
788 } else {
789 param_len = 69;
791 params = nttrans_realloc(ppparams, param_len);
792 if(params == NULL) {
793 reply_nterror(req, NT_STATUS_NO_MEMORY);
794 return;
797 p = params;
798 SCVAL(p,0,NO_OPLOCK_RETURN);
800 p += 2;
801 SSVAL(p,0,pnum);
802 p += 2;
803 SIVAL(p,0,FILE_WAS_OPENED);
804 p += 8;
806 p += 32;
807 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
808 p += 20;
809 /* File type. */
810 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
811 /* Device state. */
812 SSVAL(p,2, 0x5FF); /* ? */
813 p += 4;
815 if (flags & EXTENDED_RESPONSE_REQUIRED) {
816 p += 25;
817 SIVAL(p,0,FILE_GENERIC_ALL);
819 * For pipes W2K3 seems to return
820 * 0x12019B next.
821 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
823 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
826 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
828 /* Send the required number of replies */
829 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
831 return;
834 /****************************************************************************
835 Internal fn to set security descriptors.
836 ****************************************************************************/
838 NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
839 uint32_t security_info_sent)
841 struct security_descriptor *psd = NULL;
842 NTSTATUS status;
844 if (sd_len == 0) {
845 return NT_STATUS_INVALID_PARAMETER;
848 if (!CAN_WRITE(fsp->conn)) {
849 return NT_STATUS_ACCESS_DENIED;
852 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
853 return NT_STATUS_OK;
856 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
858 if (!NT_STATUS_IS_OK(status)) {
859 return status;
862 if (psd->owner_sid == NULL) {
863 security_info_sent &= ~SECINFO_OWNER;
865 if (psd->group_sid == NULL) {
866 security_info_sent &= ~SECINFO_GROUP;
869 /* Ensure we have at least one thing set. */
870 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
871 if (security_info_sent & SECINFO_LABEL) {
872 /* Only consider SECINFO_LABEL if no other
873 bits are set. Just like W2K3 we don't
874 store this. */
875 return NT_STATUS_OK;
877 return NT_STATUS_INVALID_PARAMETER;
880 /* Ensure we have the rights to do this. */
881 if (security_info_sent & SECINFO_OWNER) {
882 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
883 return NT_STATUS_ACCESS_DENIED;
887 if (security_info_sent & SECINFO_GROUP) {
888 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
889 return NT_STATUS_ACCESS_DENIED;
893 if (security_info_sent & SECINFO_DACL) {
894 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
895 return NT_STATUS_ACCESS_DENIED;
897 /* Convert all the generic bits. */
898 if (psd->dacl) {
899 security_acl_map_generic(psd->dacl, &file_generic_mapping);
903 if (security_info_sent & SECINFO_SACL) {
904 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
905 return NT_STATUS_ACCESS_DENIED;
907 /* Convert all the generic bits. */
908 if (psd->sacl) {
909 security_acl_map_generic(psd->sacl, &file_generic_mapping);
913 if (DEBUGLEVEL >= 10) {
914 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
915 NDR_PRINT_DEBUG(security_descriptor, psd);
918 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
920 TALLOC_FREE(psd);
922 return status;
925 /****************************************************************************
926 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
927 ****************************************************************************/
929 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
931 struct ea_list *ea_list_head = NULL;
932 size_t offset = 0;
934 if (data_size < 4) {
935 return NULL;
938 while (offset + 4 <= data_size) {
939 size_t next_offset = IVAL(pdata,offset);
940 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
942 if (!eal) {
943 return NULL;
946 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
947 if (next_offset == 0) {
948 break;
950 offset += next_offset;
953 return ea_list_head;
956 /****************************************************************************
957 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
958 ****************************************************************************/
960 static void call_nt_transact_create(connection_struct *conn,
961 struct smb_request *req,
962 uint16 **ppsetup, uint32 setup_count,
963 char **ppparams, uint32 parameter_count,
964 char **ppdata, uint32 data_count,
965 uint32 max_data_count)
967 struct smb_filename *smb_fname = NULL;
968 char *fname = NULL;
969 char *params = *ppparams;
970 char *data = *ppdata;
971 /* Breakout the oplock request bits so we can set the reply bits separately. */
972 uint32 fattr=0;
973 SMB_OFF_T file_len = 0;
974 int info = 0;
975 files_struct *fsp = NULL;
976 char *p = NULL;
977 uint32 flags;
978 uint32 access_mask;
979 uint32 file_attributes;
980 uint32 share_access;
981 uint32 create_disposition;
982 uint32 create_options;
983 uint32 sd_len;
984 struct security_descriptor *sd = NULL;
985 uint32 ea_len;
986 uint16 root_dir_fid;
987 struct timespec create_timespec;
988 struct timespec c_timespec;
989 struct timespec a_timespec;
990 struct timespec m_timespec;
991 struct timespec write_time_ts;
992 struct ea_list *ea_list = NULL;
993 NTSTATUS status;
994 size_t param_len;
995 uint64_t allocation_size;
996 int oplock_request;
997 uint8_t oplock_granted;
998 struct case_semantics_state *case_state = NULL;
999 TALLOC_CTX *ctx = talloc_tos();
1001 DEBUG(5,("call_nt_transact_create\n"));
1004 * If it's an IPC, use the pipe handler.
1007 if (IS_IPC(conn)) {
1008 if (lp_nt_pipe_support()) {
1009 do_nt_transact_create_pipe(
1010 conn, req,
1011 ppsetup, setup_count,
1012 ppparams, parameter_count,
1013 ppdata, data_count);
1014 goto out;
1016 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1017 goto out;
1021 * Ensure minimum number of parameters sent.
1024 if(parameter_count < 54) {
1025 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1026 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1027 goto out;
1030 flags = IVAL(params,0);
1031 access_mask = IVAL(params,8);
1032 file_attributes = IVAL(params,20);
1033 share_access = IVAL(params,24);
1034 create_disposition = IVAL(params,28);
1035 create_options = IVAL(params,32);
1036 sd_len = IVAL(params,36);
1037 ea_len = IVAL(params,40);
1038 root_dir_fid = (uint16)IVAL(params,4);
1039 allocation_size = BVAL(params,12);
1042 * we need to remove ignored bits when they come directly from the client
1043 * because we reuse some of them for internal stuff
1045 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1047 /* Ensure the data_len is correct for the sd and ea values given. */
1048 if ((ea_len + sd_len > data_count)
1049 || (ea_len > data_count) || (sd_len > data_count)
1050 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1051 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1052 "%u, data_count = %u\n", (unsigned int)ea_len,
1053 (unsigned int)sd_len, (unsigned int)data_count));
1054 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1055 goto out;
1058 if (sd_len) {
1059 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1060 sd_len));
1062 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1063 &sd);
1064 if (!NT_STATUS_IS_OK(status)) {
1065 DEBUG(10, ("call_nt_transact_create: "
1066 "unmarshall_sec_desc failed: %s\n",
1067 nt_errstr(status)));
1068 reply_nterror(req, status);
1069 goto out;
1073 if (ea_len) {
1074 if (!lp_ea_support(SNUM(conn))) {
1075 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1076 "EA's not supported.\n",
1077 (unsigned int)ea_len));
1078 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1079 goto out;
1082 if (ea_len < 10) {
1083 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1084 "too small (should be more than 10)\n",
1085 (unsigned int)ea_len ));
1086 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1087 goto out;
1090 /* We have already checked that ea_len <= data_count here. */
1091 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1092 ea_len);
1093 if (ea_list == NULL) {
1094 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1095 goto out;
1099 srvstr_get_path(ctx, params, req->flags2, &fname,
1100 params+53, parameter_count-53,
1101 STR_TERMINATE, &status);
1102 if (!NT_STATUS_IS_OK(status)) {
1103 reply_nterror(req, status);
1104 goto out;
1107 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1108 case_state = set_posix_case_semantics(ctx, conn);
1109 if (!case_state) {
1110 reply_nterror(req, NT_STATUS_NO_MEMORY);
1111 goto out;
1115 status = filename_convert(ctx,
1116 conn,
1117 req->flags2 & FLAGS2_DFS_PATHNAMES,
1118 fname,
1120 NULL,
1121 &smb_fname);
1123 TALLOC_FREE(case_state);
1125 if (!NT_STATUS_IS_OK(status)) {
1126 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1127 reply_botherror(req,
1128 NT_STATUS_PATH_NOT_COVERED,
1129 ERRSRV, ERRbadpath);
1130 goto out;
1132 reply_nterror(req, status);
1133 goto out;
1136 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1137 if (oplock_request) {
1138 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1139 ? BATCH_OPLOCK : 0;
1143 * Bug #6898 - clients using Windows opens should
1144 * never be able to set this attribute into the
1145 * VFS.
1147 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1149 status = SMB_VFS_CREATE_FILE(
1150 conn, /* conn */
1151 req, /* req */
1152 root_dir_fid, /* root_dir_fid */
1153 smb_fname, /* fname */
1154 access_mask, /* access_mask */
1155 share_access, /* share_access */
1156 create_disposition, /* create_disposition*/
1157 create_options, /* create_options */
1158 file_attributes, /* file_attributes */
1159 oplock_request, /* oplock_request */
1160 allocation_size, /* allocation_size */
1161 0, /* private_flags */
1162 sd, /* sd */
1163 ea_list, /* ea_list */
1164 &fsp, /* result */
1165 &info); /* pinfo */
1167 if(!NT_STATUS_IS_OK(status)) {
1168 if (open_was_deferred(req->sconn, req->mid)) {
1169 /* We have re-scheduled this call, no error. */
1170 return;
1172 reply_openerror(req, status);
1173 goto out;
1176 /* Ensure we're pointing at the correct stat struct. */
1177 TALLOC_FREE(smb_fname);
1178 smb_fname = fsp->fsp_name;
1181 * If the caller set the extended oplock request bit
1182 * and we granted one (by whatever means) - set the
1183 * correct bit for extended oplock reply.
1186 if (oplock_request &&
1187 (lp_fake_oplocks(SNUM(conn))
1188 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1191 * Exclusive oplock granted
1194 if (flags & REQUEST_BATCH_OPLOCK) {
1195 oplock_granted = BATCH_OPLOCK_RETURN;
1196 } else {
1197 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1199 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1200 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1201 } else {
1202 oplock_granted = NO_OPLOCK_RETURN;
1205 file_len = smb_fname->st.st_ex_size;
1207 /* Realloc the size of parameters and data we will return */
1208 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1209 /* Extended response is 32 more byyes. */
1210 param_len = 101;
1211 } else {
1212 param_len = 69;
1214 params = nttrans_realloc(ppparams, param_len);
1215 if(params == NULL) {
1216 reply_nterror(req, NT_STATUS_NO_MEMORY);
1217 goto out;
1220 p = params;
1221 SCVAL(p, 0, oplock_granted);
1223 p += 2;
1224 SSVAL(p,0,fsp->fnum);
1225 p += 2;
1226 if ((create_disposition == FILE_SUPERSEDE)
1227 && (info == FILE_WAS_OVERWRITTEN)) {
1228 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1229 } else {
1230 SIVAL(p,0,info);
1232 p += 8;
1234 fattr = dos_mode(conn, smb_fname);
1235 if (fattr == 0) {
1236 fattr = FILE_ATTRIBUTE_NORMAL;
1239 /* Deal with other possible opens having a modified
1240 write time. JRA. */
1241 ZERO_STRUCT(write_time_ts);
1242 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
1243 if (!null_timespec(write_time_ts)) {
1244 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1247 /* Create time. */
1248 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1249 a_timespec = smb_fname->st.st_ex_atime;
1250 m_timespec = smb_fname->st.st_ex_mtime;
1251 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1253 if (lp_dos_filetime_resolution(SNUM(conn))) {
1254 dos_filetime_timespec(&create_timespec);
1255 dos_filetime_timespec(&a_timespec);
1256 dos_filetime_timespec(&m_timespec);
1257 dos_filetime_timespec(&c_timespec);
1260 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1261 p += 8;
1262 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1263 p += 8;
1264 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1265 p += 8;
1266 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1267 p += 8;
1268 SIVAL(p,0,fattr); /* File Attributes. */
1269 p += 4;
1270 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1271 p += 8;
1272 SOFF_T(p,0,file_len);
1273 p += 8;
1274 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1275 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1276 size_t num_names = 0;
1277 unsigned int num_streams;
1278 struct stream_struct *streams = NULL;
1280 /* Do we have any EA's ? */
1281 status = get_ea_names_from_file(ctx, conn, fsp,
1282 smb_fname->base_name, NULL, &num_names);
1283 if (NT_STATUS_IS_OK(status) && num_names) {
1284 file_status &= ~NO_EAS;
1286 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
1287 &num_streams, &streams);
1288 /* There is always one stream, ::$DATA. */
1289 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1290 file_status &= ~NO_SUBSTREAMS;
1292 TALLOC_FREE(streams);
1293 SSVAL(p,2,file_status);
1295 p += 4;
1296 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1298 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1299 uint32 perms = 0;
1300 p += 25;
1301 if (fsp->is_directory ||
1302 can_write_to_file(conn, smb_fname)) {
1303 perms = FILE_GENERIC_ALL;
1304 } else {
1305 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1307 SIVAL(p,0,perms);
1310 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1311 smb_fname_str_dbg(smb_fname)));
1313 /* Send the required number of replies */
1314 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1315 out:
1316 return;
1319 /****************************************************************************
1320 Reply to a NT CANCEL request.
1321 conn POINTER CAN BE NULL HERE !
1322 ****************************************************************************/
1324 void reply_ntcancel(struct smb_request *req)
1327 * Go through and cancel any pending change notifies.
1330 START_PROFILE(SMBntcancel);
1331 srv_cancel_sign_response(req->sconn);
1332 remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
1333 remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
1335 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1336 (unsigned long long)req->mid));
1338 END_PROFILE(SMBntcancel);
1339 return;
1342 /****************************************************************************
1343 Copy a file.
1344 ****************************************************************************/
1346 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1347 connection_struct *conn,
1348 struct smb_request *req,
1349 struct smb_filename *smb_fname_src,
1350 struct smb_filename *smb_fname_dst,
1351 uint32 attrs)
1353 files_struct *fsp1,*fsp2;
1354 uint32 fattr;
1355 int info;
1356 SMB_OFF_T ret=-1;
1357 NTSTATUS status = NT_STATUS_OK;
1358 char *parent;
1360 if (!CAN_WRITE(conn)) {
1361 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1362 goto out;
1365 /* Source must already exist. */
1366 if (!VALID_STAT(smb_fname_src->st)) {
1367 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1368 goto out;
1371 /* Ensure attributes match. */
1372 fattr = dos_mode(conn, smb_fname_src);
1373 if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1374 status = NT_STATUS_NO_SUCH_FILE;
1375 goto out;
1378 /* Disallow if dst file already exists. */
1379 if (VALID_STAT(smb_fname_dst->st)) {
1380 status = NT_STATUS_OBJECT_NAME_COLLISION;
1381 goto out;
1384 /* No links from a directory. */
1385 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1386 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1387 goto out;
1390 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1391 smb_fname_str_dbg(smb_fname_src),
1392 smb_fname_str_dbg(smb_fname_dst)));
1394 status = SMB_VFS_CREATE_FILE(
1395 conn, /* conn */
1396 req, /* req */
1397 0, /* root_dir_fid */
1398 smb_fname_src, /* fname */
1399 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1400 FILE_READ_EA, /* access_mask */
1401 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1402 FILE_SHARE_DELETE),
1403 FILE_OPEN, /* create_disposition*/
1404 0, /* create_options */
1405 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1406 NO_OPLOCK, /* oplock_request */
1407 0, /* allocation_size */
1408 0, /* private_flags */
1409 NULL, /* sd */
1410 NULL, /* ea_list */
1411 &fsp1, /* result */
1412 &info); /* pinfo */
1414 if (!NT_STATUS_IS_OK(status)) {
1415 goto out;
1418 status = SMB_VFS_CREATE_FILE(
1419 conn, /* conn */
1420 req, /* req */
1421 0, /* root_dir_fid */
1422 smb_fname_dst, /* fname */
1423 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1424 FILE_WRITE_EA, /* access_mask */
1425 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1426 FILE_SHARE_DELETE),
1427 FILE_CREATE, /* create_disposition*/
1428 0, /* create_options */
1429 fattr, /* file_attributes */
1430 NO_OPLOCK, /* oplock_request */
1431 0, /* allocation_size */
1432 0, /* private_flags */
1433 NULL, /* sd */
1434 NULL, /* ea_list */
1435 &fsp2, /* result */
1436 &info); /* pinfo */
1438 if (!NT_STATUS_IS_OK(status)) {
1439 close_file(NULL, fsp1, ERROR_CLOSE);
1440 goto out;
1443 if (smb_fname_src->st.st_ex_size) {
1444 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1448 * As we are opening fsp1 read-only we only expect
1449 * an error on close on fsp2 if we are out of space.
1450 * Thus we don't look at the error return from the
1451 * close of fsp1.
1453 close_file(NULL, fsp1, NORMAL_CLOSE);
1455 /* Ensure the modtime is set correctly on the destination file. */
1456 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1458 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1460 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1461 creates the file. This isn't the correct thing to do in the copy
1462 case. JRA */
1463 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1464 NULL)) {
1465 status = NT_STATUS_NO_MEMORY;
1466 goto out;
1468 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1469 TALLOC_FREE(parent);
1471 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1472 status = NT_STATUS_DISK_FULL;
1473 goto out;
1475 out:
1476 if (!NT_STATUS_IS_OK(status)) {
1477 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1478 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1479 smb_fname_str_dbg(smb_fname_dst)));
1482 return status;
1485 /****************************************************************************
1486 Reply to a NT rename request.
1487 ****************************************************************************/
1489 void reply_ntrename(struct smb_request *req)
1491 connection_struct *conn = req->conn;
1492 struct smb_filename *smb_fname_old = NULL;
1493 struct smb_filename *smb_fname_new = NULL;
1494 char *oldname = NULL;
1495 char *newname = NULL;
1496 const char *p;
1497 NTSTATUS status;
1498 bool src_has_wcard = False;
1499 bool dest_has_wcard = False;
1500 uint32 attrs;
1501 uint32_t ucf_flags_src = 0;
1502 uint32_t ucf_flags_dst = 0;
1503 uint16 rename_type;
1504 TALLOC_CTX *ctx = talloc_tos();
1505 bool stream_rename = false;
1507 START_PROFILE(SMBntrename);
1509 if (req->wct < 4) {
1510 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1511 goto out;
1514 attrs = SVAL(req->vwv+0, 0);
1515 rename_type = SVAL(req->vwv+1, 0);
1517 p = (const char *)req->buf + 1;
1518 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1519 &status, &src_has_wcard);
1520 if (!NT_STATUS_IS_OK(status)) {
1521 reply_nterror(req, status);
1522 goto out;
1525 if (ms_has_wild(oldname)) {
1526 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1527 goto out;
1530 p++;
1531 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1532 &status, &dest_has_wcard);
1533 if (!NT_STATUS_IS_OK(status)) {
1534 reply_nterror(req, status);
1535 goto out;
1538 if (!lp_posix_pathnames()) {
1539 /* The newname must begin with a ':' if the
1540 oldname contains a ':'. */
1541 if (strchr_m(oldname, ':')) {
1542 if (newname[0] != ':') {
1543 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1544 goto out;
1546 stream_rename = true;
1551 * If this is a rename operation, allow wildcards and save the
1552 * destination's last component.
1554 if (rename_type == RENAME_FLAG_RENAME) {
1555 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1556 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1559 /* rename_internals() calls unix_convert(), so don't call it here. */
1560 status = filename_convert(ctx, conn,
1561 req->flags2 & FLAGS2_DFS_PATHNAMES,
1562 oldname,
1563 ucf_flags_src,
1564 NULL,
1565 &smb_fname_old);
1566 if (!NT_STATUS_IS_OK(status)) {
1567 if (NT_STATUS_EQUAL(status,
1568 NT_STATUS_PATH_NOT_COVERED)) {
1569 reply_botherror(req,
1570 NT_STATUS_PATH_NOT_COVERED,
1571 ERRSRV, ERRbadpath);
1572 goto out;
1574 reply_nterror(req, status);
1575 goto out;
1578 status = filename_convert(ctx, conn,
1579 req->flags2 & FLAGS2_DFS_PATHNAMES,
1580 newname,
1581 ucf_flags_dst,
1582 &dest_has_wcard,
1583 &smb_fname_new);
1584 if (!NT_STATUS_IS_OK(status)) {
1585 if (NT_STATUS_EQUAL(status,
1586 NT_STATUS_PATH_NOT_COVERED)) {
1587 reply_botherror(req,
1588 NT_STATUS_PATH_NOT_COVERED,
1589 ERRSRV, ERRbadpath);
1590 goto out;
1592 reply_nterror(req, status);
1593 goto out;
1596 if (stream_rename) {
1597 /* smb_fname_new must be the same as smb_fname_old. */
1598 TALLOC_FREE(smb_fname_new->base_name);
1599 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1600 smb_fname_old->base_name);
1601 if (!smb_fname_new->base_name) {
1602 reply_nterror(req, NT_STATUS_NO_MEMORY);
1603 goto out;
1607 DEBUG(3,("reply_ntrename: %s -> %s\n",
1608 smb_fname_str_dbg(smb_fname_old),
1609 smb_fname_str_dbg(smb_fname_new)));
1611 switch(rename_type) {
1612 case RENAME_FLAG_RENAME:
1613 status = rename_internals(ctx, conn, req,
1614 smb_fname_old, smb_fname_new,
1615 attrs, False, src_has_wcard,
1616 dest_has_wcard,
1617 DELETE_ACCESS);
1618 break;
1619 case RENAME_FLAG_HARD_LINK:
1620 if (src_has_wcard || dest_has_wcard) {
1621 /* No wildcards. */
1622 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1623 } else {
1624 status = hardlink_internals(ctx, conn,
1625 req,
1626 false,
1627 smb_fname_old,
1628 smb_fname_new);
1630 break;
1631 case RENAME_FLAG_COPY:
1632 if (src_has_wcard || dest_has_wcard) {
1633 /* No wildcards. */
1634 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1635 } else {
1636 status = copy_internals(ctx, conn, req,
1637 smb_fname_old,
1638 smb_fname_new,
1639 attrs);
1641 break;
1642 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1643 status = NT_STATUS_INVALID_PARAMETER;
1644 break;
1645 default:
1646 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1647 break;
1650 if (!NT_STATUS_IS_OK(status)) {
1651 if (open_was_deferred(req->sconn, req->mid)) {
1652 /* We have re-scheduled this call. */
1653 goto out;
1656 reply_nterror(req, status);
1657 goto out;
1660 reply_outbuf(req, 0, 0);
1661 out:
1662 END_PROFILE(SMBntrename);
1663 return;
1666 /****************************************************************************
1667 Reply to a notify change - queue the request and
1668 don't allow a directory to be opened.
1669 ****************************************************************************/
1671 static void smbd_smb1_notify_reply(struct smb_request *req,
1672 NTSTATUS error_code,
1673 uint8_t *buf, size_t len)
1675 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1678 static void call_nt_transact_notify_change(connection_struct *conn,
1679 struct smb_request *req,
1680 uint16 **ppsetup,
1681 uint32 setup_count,
1682 char **ppparams,
1683 uint32 parameter_count,
1684 char **ppdata, uint32 data_count,
1685 uint32 max_data_count,
1686 uint32 max_param_count)
1688 uint16 *setup = *ppsetup;
1689 files_struct *fsp;
1690 uint32 filter;
1691 NTSTATUS status;
1692 bool recursive;
1694 if(setup_count < 6) {
1695 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1696 return;
1699 fsp = file_fsp(req, SVAL(setup,4));
1700 filter = IVAL(setup, 0);
1701 recursive = (SVAL(setup, 6) != 0) ? True : False;
1703 DEBUG(3,("call_nt_transact_notify_change\n"));
1705 if(!fsp) {
1706 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1707 return;
1711 char *filter_string;
1713 if (!(filter_string = notify_filter_string(NULL, filter))) {
1714 reply_nterror(req,NT_STATUS_NO_MEMORY);
1715 return;
1718 DEBUG(3,("call_nt_transact_notify_change: notify change "
1719 "called on %s, filter = %s, recursive = %d\n",
1720 fsp_str_dbg(fsp), filter_string, recursive));
1722 TALLOC_FREE(filter_string);
1725 if((!fsp->is_directory) || (conn != fsp->conn)) {
1726 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1727 return;
1730 if (fsp->notify == NULL) {
1732 status = change_notify_create(fsp, filter, recursive);
1734 if (!NT_STATUS_IS_OK(status)) {
1735 DEBUG(10, ("change_notify_create returned %s\n",
1736 nt_errstr(status)));
1737 reply_nterror(req, status);
1738 return;
1742 if (fsp->notify->num_changes != 0) {
1745 * We've got changes pending, respond immediately
1749 * TODO: write a torture test to check the filtering behaviour
1750 * here.
1753 change_notify_reply(req,
1754 NT_STATUS_OK,
1755 max_param_count,
1756 fsp->notify,
1757 smbd_smb1_notify_reply);
1760 * change_notify_reply() above has independently sent its
1761 * results
1763 return;
1767 * No changes pending, queue the request
1770 status = change_notify_add_request(req,
1771 max_param_count,
1772 filter,
1773 recursive, fsp,
1774 smbd_smb1_notify_reply);
1775 if (!NT_STATUS_IS_OK(status)) {
1776 reply_nterror(req, status);
1778 return;
1781 /****************************************************************************
1782 Reply to an NT transact rename command.
1783 ****************************************************************************/
1785 static void call_nt_transact_rename(connection_struct *conn,
1786 struct smb_request *req,
1787 uint16 **ppsetup, uint32 setup_count,
1788 char **ppparams, uint32 parameter_count,
1789 char **ppdata, uint32 data_count,
1790 uint32 max_data_count)
1792 char *params = *ppparams;
1793 char *new_name = NULL;
1794 files_struct *fsp = NULL;
1795 bool dest_has_wcard = False;
1796 NTSTATUS status;
1797 TALLOC_CTX *ctx = talloc_tos();
1799 if(parameter_count < 5) {
1800 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1801 return;
1804 fsp = file_fsp(req, SVAL(params, 0));
1805 if (!check_fsp(conn, req, fsp)) {
1806 return;
1808 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1809 parameter_count - 4,
1810 STR_TERMINATE, &status, &dest_has_wcard);
1811 if (!NT_STATUS_IS_OK(status)) {
1812 reply_nterror(req, status);
1813 return;
1817 * W2K3 ignores this request as the RAW-RENAME test
1818 * demonstrates, so we do.
1820 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1822 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1823 fsp_str_dbg(fsp), new_name));
1825 return;
1828 /******************************************************************************
1829 Fake up a completely empty SD.
1830 *******************************************************************************/
1832 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1834 size_t sd_size;
1836 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1837 if(!*ppsd) {
1838 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1839 return NT_STATUS_NO_MEMORY;
1842 return NT_STATUS_OK;
1845 /****************************************************************************
1846 Reply to query a security descriptor.
1847 Callable from SMB2 and SMB2.
1848 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1849 the required size.
1850 ****************************************************************************/
1852 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1853 TALLOC_CTX *mem_ctx,
1854 files_struct *fsp,
1855 uint32_t security_info_wanted,
1856 uint32_t max_data_count,
1857 uint8_t **ppmarshalled_sd,
1858 size_t *psd_size)
1860 NTSTATUS status;
1861 struct security_descriptor *psd = NULL;
1864 * Get the permissions to return.
1867 if ((security_info_wanted & SECINFO_SACL) &&
1868 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1869 return NT_STATUS_ACCESS_DENIED;
1872 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1873 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1874 return NT_STATUS_ACCESS_DENIED;
1877 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1878 SECINFO_GROUP|SECINFO_SACL)) {
1879 /* Don't return SECINFO_LABEL if anything else was
1880 requested. See bug #8458. */
1881 security_info_wanted &= ~SECINFO_LABEL;
1884 if (!lp_nt_acl_support(SNUM(conn))) {
1885 status = get_null_nt_acl(mem_ctx, &psd);
1886 } else if (security_info_wanted & SECINFO_LABEL) {
1887 /* Like W2K3 return a null object. */
1888 status = get_null_nt_acl(mem_ctx, &psd);
1889 } else {
1890 status = SMB_VFS_FGET_NT_ACL(
1891 fsp, security_info_wanted, &psd);
1893 if (!NT_STATUS_IS_OK(status)) {
1894 return status;
1897 if (!(security_info_wanted & SECINFO_OWNER)) {
1898 psd->owner_sid = NULL;
1900 if (!(security_info_wanted & SECINFO_GROUP)) {
1901 psd->group_sid = NULL;
1903 if (!(security_info_wanted & SECINFO_DACL)) {
1904 psd->dacl = NULL;
1906 if (!(security_info_wanted & SECINFO_SACL)) {
1907 psd->sacl = NULL;
1910 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1911 * present in the reply to match Windows behavior */
1912 if (psd->sacl == NULL &&
1913 security_info_wanted & SECINFO_SACL)
1914 psd->type |= SEC_DESC_SACL_PRESENT;
1915 if (psd->dacl == NULL &&
1916 security_info_wanted & SECINFO_DACL)
1917 psd->type |= SEC_DESC_DACL_PRESENT;
1919 if (security_info_wanted & SECINFO_LABEL) {
1920 /* Like W2K3 return a null object. */
1921 psd->owner_sid = NULL;
1922 psd->group_sid = NULL;
1923 psd->dacl = NULL;
1924 psd->sacl = NULL;
1925 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1928 *psd_size = ndr_size_security_descriptor(psd, 0);
1930 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1931 (unsigned long)*psd_size));
1933 if (DEBUGLEVEL >= 10) {
1934 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1935 fsp_str_dbg(fsp)));
1936 NDR_PRINT_DEBUG(security_descriptor, psd);
1939 if (max_data_count < *psd_size) {
1940 TALLOC_FREE(psd);
1941 return NT_STATUS_BUFFER_TOO_SMALL;
1944 status = marshall_sec_desc(mem_ctx, psd,
1945 ppmarshalled_sd, psd_size);
1947 if (!NT_STATUS_IS_OK(status)) {
1948 TALLOC_FREE(psd);
1949 return status;
1952 TALLOC_FREE(psd);
1953 return NT_STATUS_OK;
1956 /****************************************************************************
1957 SMB1 reply to query a security descriptor.
1958 ****************************************************************************/
1960 static void call_nt_transact_query_security_desc(connection_struct *conn,
1961 struct smb_request *req,
1962 uint16 **ppsetup,
1963 uint32 setup_count,
1964 char **ppparams,
1965 uint32 parameter_count,
1966 char **ppdata,
1967 uint32 data_count,
1968 uint32 max_data_count)
1970 char *params = *ppparams;
1971 char *data = *ppdata;
1972 size_t sd_size = 0;
1973 uint32 security_info_wanted;
1974 files_struct *fsp = NULL;
1975 NTSTATUS status;
1976 uint8_t *marshalled_sd = NULL;
1978 if(parameter_count < 8) {
1979 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1980 return;
1983 fsp = file_fsp(req, SVAL(params,0));
1984 if(!fsp) {
1985 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1986 return;
1989 security_info_wanted = IVAL(params,4);
1991 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1992 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1993 (unsigned int)security_info_wanted));
1995 params = nttrans_realloc(ppparams, 4);
1996 if(params == NULL) {
1997 reply_nterror(req, NT_STATUS_NO_MEMORY);
1998 return;
2002 * Get the permissions to return.
2005 status = smbd_do_query_security_desc(conn,
2006 talloc_tos(),
2007 fsp,
2008 security_info_wanted,
2009 max_data_count,
2010 &marshalled_sd,
2011 &sd_size);
2013 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2014 SIVAL(params,0,(uint32_t)sd_size);
2015 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2016 params, 4, NULL, 0);
2017 return;
2020 if (!NT_STATUS_IS_OK(status)) {
2021 reply_nterror(req, status);
2022 return;
2025 SMB_ASSERT(sd_size > 0);
2027 SIVAL(params,0,(uint32_t)sd_size);
2029 if (max_data_count < sd_size) {
2030 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2031 params, 4, NULL, 0);
2032 return;
2036 * Allocate the data we will return.
2039 data = nttrans_realloc(ppdata, sd_size);
2040 if(data == NULL) {
2041 reply_nterror(req, NT_STATUS_NO_MEMORY);
2042 return;
2045 memcpy(data, marshalled_sd, sd_size);
2047 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2049 return;
2052 /****************************************************************************
2053 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2054 ****************************************************************************/
2056 static void call_nt_transact_set_security_desc(connection_struct *conn,
2057 struct smb_request *req,
2058 uint16 **ppsetup,
2059 uint32 setup_count,
2060 char **ppparams,
2061 uint32 parameter_count,
2062 char **ppdata,
2063 uint32 data_count,
2064 uint32 max_data_count)
2066 char *params= *ppparams;
2067 char *data = *ppdata;
2068 files_struct *fsp = NULL;
2069 uint32 security_info_sent = 0;
2070 NTSTATUS status;
2072 if(parameter_count < 8) {
2073 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2074 return;
2077 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2078 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2079 return;
2082 if (!CAN_WRITE(fsp->conn)) {
2083 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2084 return;
2087 if(!lp_nt_acl_support(SNUM(conn))) {
2088 goto done;
2091 security_info_sent = IVAL(params,4);
2093 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2094 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2096 if (data_count == 0) {
2097 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2098 return;
2101 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
2103 if (!NT_STATUS_IS_OK(status)) {
2104 reply_nterror(req, status);
2105 return;
2108 done:
2109 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2110 return;
2113 /****************************************************************************
2114 Reply to NT IOCTL
2115 ****************************************************************************/
2117 static void call_nt_transact_ioctl(connection_struct *conn,
2118 struct smb_request *req,
2119 uint16 **ppsetup, uint32 setup_count,
2120 char **ppparams, uint32 parameter_count,
2121 char **ppdata, uint32 data_count,
2122 uint32 max_data_count)
2124 NTSTATUS status;
2125 uint32 function;
2126 uint16 fidnum;
2127 files_struct *fsp;
2128 uint8 isFSctl;
2129 uint8 compfilter;
2130 char *out_data = NULL;
2131 uint32 out_data_len = 0;
2132 char *pdata = *ppdata;
2133 TALLOC_CTX *ctx = talloc_tos();
2135 if (setup_count != 8) {
2136 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2137 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2138 return;
2141 function = IVAL(*ppsetup, 0);
2142 fidnum = SVAL(*ppsetup, 4);
2143 isFSctl = CVAL(*ppsetup, 6);
2144 compfilter = CVAL(*ppsetup, 7);
2146 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2147 function, fidnum, isFSctl, compfilter));
2149 fsp=file_fsp(req, fidnum);
2152 * We don't really implement IOCTLs, especially on files.
2154 if (!isFSctl) {
2155 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2156 isFSctl));
2157 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2158 return;
2161 /* Has to be for an open file! */
2162 if (!check_fsp_open(conn, req, fsp)) {
2163 return;
2166 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2169 * out_data might be allocated by the VFS module, but talloc should be
2170 * used, and should be cleaned up when the request ends.
2172 status = SMB_VFS_FSCTL(fsp,
2173 ctx,
2174 function,
2175 req->flags2,
2176 (uint8_t *)pdata,
2177 data_count,
2178 (uint8_t **)&out_data,
2179 max_data_count,
2180 &out_data_len);
2181 if (!NT_STATUS_IS_OK(status)) {
2182 reply_nterror(req, status);
2183 } else {
2184 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2189 #ifdef HAVE_SYS_QUOTAS
2190 /****************************************************************************
2191 Reply to get user quota
2192 ****************************************************************************/
2194 static void call_nt_transact_get_user_quota(connection_struct *conn,
2195 struct smb_request *req,
2196 uint16 **ppsetup,
2197 uint32 setup_count,
2198 char **ppparams,
2199 uint32 parameter_count,
2200 char **ppdata,
2201 uint32 data_count,
2202 uint32 max_data_count)
2204 NTSTATUS nt_status = NT_STATUS_OK;
2205 char *params = *ppparams;
2206 char *pdata = *ppdata;
2207 char *entry;
2208 int data_len=0,param_len=0;
2209 int qt_len=0;
2210 int entry_len = 0;
2211 files_struct *fsp = NULL;
2212 uint16 level = 0;
2213 size_t sid_len;
2214 struct dom_sid sid;
2215 bool start_enum = True;
2216 SMB_NTQUOTA_STRUCT qt;
2217 SMB_NTQUOTA_LIST *tmp_list;
2218 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2220 ZERO_STRUCT(qt);
2222 /* access check */
2223 if (get_current_uid(conn) != 0) {
2224 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2225 "[%s]\n", lp_servicename(SNUM(conn)),
2226 conn->session_info->unix_info->unix_name));
2227 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2228 return;
2232 * Ensure minimum number of parameters sent.
2235 if (parameter_count < 4) {
2236 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2237 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2238 return;
2241 /* maybe we can check the quota_fnum */
2242 fsp = file_fsp(req, SVAL(params,0));
2243 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2244 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2245 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2246 return;
2249 /* the NULL pointer checking for fsp->fake_file_handle->pd
2250 * is done by CHECK_NTQUOTA_HANDLE_OK()
2252 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2254 level = SVAL(params,2);
2256 /* unknown 12 bytes leading in params */
2258 switch (level) {
2259 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2260 /* seems that we should continue with the enum here --metze */
2262 if (qt_handle->quota_list!=NULL &&
2263 qt_handle->tmp_list==NULL) {
2265 /* free the list */
2266 free_ntquota_list(&(qt_handle->quota_list));
2268 /* Realloc the size of parameters and data we will return */
2269 param_len = 4;
2270 params = nttrans_realloc(ppparams, param_len);
2271 if(params == NULL) {
2272 reply_nterror(req, NT_STATUS_NO_MEMORY);
2273 return;
2276 data_len = 0;
2277 SIVAL(params,0,data_len);
2279 break;
2282 start_enum = False;
2284 case TRANSACT_GET_USER_QUOTA_LIST_START:
2286 if (qt_handle->quota_list==NULL &&
2287 qt_handle->tmp_list==NULL) {
2288 start_enum = True;
2291 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2292 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2293 return;
2296 /* Realloc the size of parameters and data we will return */
2297 param_len = 4;
2298 params = nttrans_realloc(ppparams, param_len);
2299 if(params == NULL) {
2300 reply_nterror(req, NT_STATUS_NO_MEMORY);
2301 return;
2304 /* we should not trust the value in max_data_count*/
2305 max_data_count = MIN(max_data_count,2048);
2307 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2308 if(pdata == NULL) {
2309 reply_nterror(req, NT_STATUS_NO_MEMORY);
2310 return;
2313 entry = pdata;
2315 /* set params Size of returned Quota Data 4 bytes*/
2316 /* but set it later when we know it */
2318 /* for each entry push the data */
2320 if (start_enum) {
2321 qt_handle->tmp_list = qt_handle->quota_list;
2324 tmp_list = qt_handle->tmp_list;
2326 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2327 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2329 sid_len = ndr_size_dom_sid(
2330 &tmp_list->quotas->sid, 0);
2331 entry_len = 40 + sid_len;
2333 /* nextoffset entry 4 bytes */
2334 SIVAL(entry,0,entry_len);
2336 /* then the len of the SID 4 bytes */
2337 SIVAL(entry,4,sid_len);
2339 /* unknown data 8 bytes uint64_t */
2340 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2342 /* the used disk space 8 bytes uint64_t */
2343 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2345 /* the soft quotas 8 bytes uint64_t */
2346 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2348 /* the hard quotas 8 bytes uint64_t */
2349 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2351 /* and now the SID */
2352 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2355 qt_handle->tmp_list = tmp_list;
2357 /* overwrite the offset of the last entry */
2358 SIVAL(entry-entry_len,0,0);
2360 data_len = 4+qt_len;
2361 /* overwrite the params quota_data_len */
2362 SIVAL(params,0,data_len);
2364 break;
2366 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2368 /* unknown 4 bytes IVAL(pdata,0) */
2370 if (data_count < 8) {
2371 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2372 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2373 return;
2376 sid_len = IVAL(pdata,4);
2377 /* Ensure this is less than 1mb. */
2378 if (sid_len > (1024*1024)) {
2379 reply_nterror(req, NT_STATUS_NO_MEMORY);
2380 return;
2383 if (data_count < 8+sid_len) {
2384 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2385 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2386 return;
2389 data_len = 4+40+sid_len;
2391 if (max_data_count < data_len) {
2392 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2393 max_data_count, data_len));
2394 param_len = 4;
2395 SIVAL(params,0,data_len);
2396 data_len = 0;
2397 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2398 break;
2401 if (!sid_parse(pdata+8,sid_len,&sid)) {
2402 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2403 return;
2406 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2407 ZERO_STRUCT(qt);
2409 * we have to return zero's in all fields
2410 * instead of returning an error here
2411 * --metze
2415 /* Realloc the size of parameters and data we will return */
2416 param_len = 4;
2417 params = nttrans_realloc(ppparams, param_len);
2418 if(params == NULL) {
2419 reply_nterror(req, NT_STATUS_NO_MEMORY);
2420 return;
2423 pdata = nttrans_realloc(ppdata, data_len);
2424 if(pdata == NULL) {
2425 reply_nterror(req, NT_STATUS_NO_MEMORY);
2426 return;
2429 entry = pdata;
2431 /* set params Size of returned Quota Data 4 bytes*/
2432 SIVAL(params,0,data_len);
2434 /* nextoffset entry 4 bytes */
2435 SIVAL(entry,0,0);
2437 /* then the len of the SID 4 bytes */
2438 SIVAL(entry,4,sid_len);
2440 /* unknown data 8 bytes uint64_t */
2441 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2443 /* the used disk space 8 bytes uint64_t */
2444 SBIG_UINT(entry,16,qt.usedspace);
2446 /* the soft quotas 8 bytes uint64_t */
2447 SBIG_UINT(entry,24,qt.softlim);
2449 /* the hard quotas 8 bytes uint64_t */
2450 SBIG_UINT(entry,32,qt.hardlim);
2452 /* and now the SID */
2453 sid_linearize(entry+40, sid_len, &sid);
2455 break;
2457 default:
2458 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2459 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2460 return;
2461 break;
2464 send_nt_replies(conn, req, nt_status, params, param_len,
2465 pdata, data_len);
2468 /****************************************************************************
2469 Reply to set user quota
2470 ****************************************************************************/
2472 static void call_nt_transact_set_user_quota(connection_struct *conn,
2473 struct smb_request *req,
2474 uint16 **ppsetup,
2475 uint32 setup_count,
2476 char **ppparams,
2477 uint32 parameter_count,
2478 char **ppdata,
2479 uint32 data_count,
2480 uint32 max_data_count)
2482 char *params = *ppparams;
2483 char *pdata = *ppdata;
2484 int data_len=0,param_len=0;
2485 SMB_NTQUOTA_STRUCT qt;
2486 size_t sid_len;
2487 struct dom_sid sid;
2488 files_struct *fsp = NULL;
2490 ZERO_STRUCT(qt);
2492 /* access check */
2493 if (get_current_uid(conn) != 0) {
2494 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2495 "[%s]\n", lp_servicename(SNUM(conn)),
2496 conn->session_info->unix_info->unix_name));
2497 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2498 return;
2502 * Ensure minimum number of parameters sent.
2505 if (parameter_count < 2) {
2506 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2507 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2508 return;
2511 /* maybe we can check the quota_fnum */
2512 fsp = file_fsp(req, SVAL(params,0));
2513 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2514 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2515 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2516 return;
2519 if (data_count < 40) {
2520 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2521 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2522 return;
2525 /* offset to next quota record.
2526 * 4 bytes IVAL(pdata,0)
2527 * unused here...
2530 /* sid len */
2531 sid_len = IVAL(pdata,4);
2533 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2534 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2535 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2536 return;
2539 /* unknown 8 bytes in pdata
2540 * maybe its the change time in NTTIME
2543 /* the used space 8 bytes (uint64_t)*/
2544 qt.usedspace = BVAL(pdata,16);
2546 /* the soft quotas 8 bytes (uint64_t)*/
2547 qt.softlim = BVAL(pdata,24);
2549 /* the hard quotas 8 bytes (uint64_t)*/
2550 qt.hardlim = BVAL(pdata,32);
2552 if (!sid_parse(pdata+40,sid_len,&sid)) {
2553 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2554 return;
2557 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2559 /* 44 unknown bytes left... */
2561 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2562 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2563 return;
2566 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2567 pdata, data_len);
2569 #endif /* HAVE_SYS_QUOTAS */
2571 static void handle_nttrans(connection_struct *conn,
2572 struct trans_state *state,
2573 struct smb_request *req)
2575 if (get_Protocol() >= PROTOCOL_NT1) {
2576 req->flags2 |= 0x40; /* IS_LONG_NAME */
2577 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2581 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2583 /* Now we must call the relevant NT_TRANS function */
2584 switch(state->call) {
2585 case NT_TRANSACT_CREATE:
2587 START_PROFILE(NT_transact_create);
2588 call_nt_transact_create(
2589 conn, req,
2590 &state->setup, state->setup_count,
2591 &state->param, state->total_param,
2592 &state->data, state->total_data,
2593 state->max_data_return);
2594 END_PROFILE(NT_transact_create);
2595 break;
2598 case NT_TRANSACT_IOCTL:
2600 START_PROFILE(NT_transact_ioctl);
2601 call_nt_transact_ioctl(
2602 conn, req,
2603 &state->setup, state->setup_count,
2604 &state->param, state->total_param,
2605 &state->data, state->total_data,
2606 state->max_data_return);
2607 END_PROFILE(NT_transact_ioctl);
2608 break;
2611 case NT_TRANSACT_SET_SECURITY_DESC:
2613 START_PROFILE(NT_transact_set_security_desc);
2614 call_nt_transact_set_security_desc(
2615 conn, req,
2616 &state->setup, state->setup_count,
2617 &state->param, state->total_param,
2618 &state->data, state->total_data,
2619 state->max_data_return);
2620 END_PROFILE(NT_transact_set_security_desc);
2621 break;
2624 case NT_TRANSACT_NOTIFY_CHANGE:
2626 START_PROFILE(NT_transact_notify_change);
2627 call_nt_transact_notify_change(
2628 conn, req,
2629 &state->setup, state->setup_count,
2630 &state->param, state->total_param,
2631 &state->data, state->total_data,
2632 state->max_data_return,
2633 state->max_param_return);
2634 END_PROFILE(NT_transact_notify_change);
2635 break;
2638 case NT_TRANSACT_RENAME:
2640 START_PROFILE(NT_transact_rename);
2641 call_nt_transact_rename(
2642 conn, req,
2643 &state->setup, state->setup_count,
2644 &state->param, state->total_param,
2645 &state->data, state->total_data,
2646 state->max_data_return);
2647 END_PROFILE(NT_transact_rename);
2648 break;
2651 case NT_TRANSACT_QUERY_SECURITY_DESC:
2653 START_PROFILE(NT_transact_query_security_desc);
2654 call_nt_transact_query_security_desc(
2655 conn, req,
2656 &state->setup, state->setup_count,
2657 &state->param, state->total_param,
2658 &state->data, state->total_data,
2659 state->max_data_return);
2660 END_PROFILE(NT_transact_query_security_desc);
2661 break;
2664 #ifdef HAVE_SYS_QUOTAS
2665 case NT_TRANSACT_GET_USER_QUOTA:
2667 START_PROFILE(NT_transact_get_user_quota);
2668 call_nt_transact_get_user_quota(
2669 conn, req,
2670 &state->setup, state->setup_count,
2671 &state->param, state->total_param,
2672 &state->data, state->total_data,
2673 state->max_data_return);
2674 END_PROFILE(NT_transact_get_user_quota);
2675 break;
2678 case NT_TRANSACT_SET_USER_QUOTA:
2680 START_PROFILE(NT_transact_set_user_quota);
2681 call_nt_transact_set_user_quota(
2682 conn, req,
2683 &state->setup, state->setup_count,
2684 &state->param, state->total_param,
2685 &state->data, state->total_data,
2686 state->max_data_return);
2687 END_PROFILE(NT_transact_set_user_quota);
2688 break;
2690 #endif /* HAVE_SYS_QUOTAS */
2692 default:
2693 /* Error in request */
2694 DEBUG(0,("handle_nttrans: Unknown request %d in "
2695 "nttrans call\n", state->call));
2696 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2697 return;
2699 return;
2702 /****************************************************************************
2703 Reply to a SMBNTtrans.
2704 ****************************************************************************/
2706 void reply_nttrans(struct smb_request *req)
2708 connection_struct *conn = req->conn;
2709 uint32_t pscnt;
2710 uint32_t psoff;
2711 uint32_t dscnt;
2712 uint32_t dsoff;
2713 uint16 function_code;
2714 NTSTATUS result;
2715 struct trans_state *state;
2717 START_PROFILE(SMBnttrans);
2719 if (req->wct < 19) {
2720 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2721 END_PROFILE(SMBnttrans);
2722 return;
2725 pscnt = IVAL(req->vwv+9, 1);
2726 psoff = IVAL(req->vwv+11, 1);
2727 dscnt = IVAL(req->vwv+13, 1);
2728 dsoff = IVAL(req->vwv+15, 1);
2729 function_code = SVAL(req->vwv+18, 0);
2731 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2732 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2733 END_PROFILE(SMBnttrans);
2734 return;
2737 result = allow_new_trans(conn->pending_trans, req->mid);
2738 if (!NT_STATUS_IS_OK(result)) {
2739 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2740 reply_nterror(req, result);
2741 END_PROFILE(SMBnttrans);
2742 return;
2745 if ((state = talloc(conn, struct trans_state)) == NULL) {
2746 reply_nterror(req, NT_STATUS_NO_MEMORY);
2747 END_PROFILE(SMBnttrans);
2748 return;
2751 state->cmd = SMBnttrans;
2753 state->mid = req->mid;
2754 state->vuid = req->vuid;
2755 state->total_data = IVAL(req->vwv+3, 1);
2756 state->data = NULL;
2757 state->total_param = IVAL(req->vwv+1, 1);
2758 state->param = NULL;
2759 state->max_data_return = IVAL(req->vwv+7, 1);
2760 state->max_param_return = IVAL(req->vwv+5, 1);
2762 /* setup count is in *words* */
2763 state->setup_count = 2*CVAL(req->vwv+17, 1);
2764 state->setup = NULL;
2765 state->call = function_code;
2767 DEBUG(10, ("num_setup=%u, "
2768 "param_total=%u, this_param=%u, max_param=%u, "
2769 "data_total=%u, this_data=%u, max_data=%u, "
2770 "param_offset=%u, data_offset=%u\n",
2771 (unsigned)state->setup_count,
2772 (unsigned)state->total_param, (unsigned)pscnt,
2773 (unsigned)state->max_param_return,
2774 (unsigned)state->total_data, (unsigned)dscnt,
2775 (unsigned)state->max_data_return,
2776 (unsigned)psoff, (unsigned)dsoff));
2779 * All nttrans messages we handle have smb_wct == 19 +
2780 * state->setup_count. Ensure this is so as a sanity check.
2783 if(req->wct != 19 + (state->setup_count/2)) {
2784 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2785 req->wct, 19 + (state->setup_count/2)));
2786 goto bad_param;
2789 /* Don't allow more than 128mb for each value. */
2790 if ((state->total_data > (1024*1024*128)) ||
2791 (state->total_param > (1024*1024*128))) {
2792 reply_nterror(req, NT_STATUS_NO_MEMORY);
2793 END_PROFILE(SMBnttrans);
2794 return;
2797 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2798 goto bad_param;
2800 if (state->total_data) {
2802 if (trans_oob(state->total_data, 0, dscnt)
2803 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2804 goto bad_param;
2807 /* Can't use talloc here, the core routines do realloc on the
2808 * params and data. */
2809 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2810 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2811 "bytes !\n", (unsigned int)state->total_data));
2812 TALLOC_FREE(state);
2813 reply_nterror(req, NT_STATUS_NO_MEMORY);
2814 END_PROFILE(SMBnttrans);
2815 return;
2818 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2821 if (state->total_param) {
2823 if (trans_oob(state->total_param, 0, pscnt)
2824 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2825 goto bad_param;
2828 /* Can't use talloc here, the core routines do realloc on the
2829 * params and data. */
2830 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2831 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2832 "bytes !\n", (unsigned int)state->total_param));
2833 SAFE_FREE(state->data);
2834 TALLOC_FREE(state);
2835 reply_nterror(req, NT_STATUS_NO_MEMORY);
2836 END_PROFILE(SMBnttrans);
2837 return;
2840 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2843 state->received_data = dscnt;
2844 state->received_param = pscnt;
2846 if(state->setup_count > 0) {
2847 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2848 state->setup_count));
2851 * No overflow possible here, state->setup_count is an
2852 * unsigned int, being filled by a single byte from
2853 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2854 * below is not necessary, it's here to clarify things. The
2855 * validity of req->vwv and req->wct has been checked in
2856 * init_smb_request already.
2858 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2859 goto bad_param;
2862 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2863 if (state->setup == NULL) {
2864 DEBUG(0,("reply_nttrans : Out of memory\n"));
2865 SAFE_FREE(state->data);
2866 SAFE_FREE(state->param);
2867 TALLOC_FREE(state);
2868 reply_nterror(req, NT_STATUS_NO_MEMORY);
2869 END_PROFILE(SMBnttrans);
2870 return;
2873 memcpy(state->setup, req->vwv+19, state->setup_count);
2874 dump_data(10, (uint8 *)state->setup, state->setup_count);
2877 if ((state->received_data == state->total_data) &&
2878 (state->received_param == state->total_param)) {
2879 handle_nttrans(conn, state, req);
2880 SAFE_FREE(state->param);
2881 SAFE_FREE(state->data);
2882 TALLOC_FREE(state);
2883 END_PROFILE(SMBnttrans);
2884 return;
2887 DLIST_ADD(conn->pending_trans, state);
2889 /* We need to send an interim response then receive the rest
2890 of the parameter/data bytes */
2891 reply_outbuf(req, 0, 0);
2892 show_msg((char *)req->outbuf);
2893 END_PROFILE(SMBnttrans);
2894 return;
2896 bad_param:
2898 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2899 SAFE_FREE(state->data);
2900 SAFE_FREE(state->param);
2901 TALLOC_FREE(state);
2902 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2903 END_PROFILE(SMBnttrans);
2904 return;
2907 /****************************************************************************
2908 Reply to a SMBnttranss
2909 ****************************************************************************/
2911 void reply_nttranss(struct smb_request *req)
2913 connection_struct *conn = req->conn;
2914 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2915 struct trans_state *state;
2917 START_PROFILE(SMBnttranss);
2919 show_msg((const char *)req->inbuf);
2921 if (req->wct < 18) {
2922 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2923 END_PROFILE(SMBnttranss);
2924 return;
2927 for (state = conn->pending_trans; state != NULL;
2928 state = state->next) {
2929 if (state->mid == req->mid) {
2930 break;
2934 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2935 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2936 END_PROFILE(SMBnttranss);
2937 return;
2940 /* Revise state->total_param and state->total_data in case they have
2941 changed downwards */
2942 if (IVAL(req->vwv+1, 1) < state->total_param) {
2943 state->total_param = IVAL(req->vwv+1, 1);
2945 if (IVAL(req->vwv+3, 1) < state->total_data) {
2946 state->total_data = IVAL(req->vwv+3, 1);
2949 pcnt = IVAL(req->vwv+5, 1);
2950 poff = IVAL(req->vwv+7, 1);
2951 pdisp = IVAL(req->vwv+9, 1);
2953 dcnt = IVAL(req->vwv+11, 1);
2954 doff = IVAL(req->vwv+13, 1);
2955 ddisp = IVAL(req->vwv+15, 1);
2957 state->received_param += pcnt;
2958 state->received_data += dcnt;
2960 if ((state->received_data > state->total_data) ||
2961 (state->received_param > state->total_param))
2962 goto bad_param;
2964 if (pcnt) {
2965 if (trans_oob(state->total_param, pdisp, pcnt)
2966 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
2967 goto bad_param;
2969 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2972 if (dcnt) {
2973 if (trans_oob(state->total_data, ddisp, dcnt)
2974 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
2975 goto bad_param;
2977 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2980 if ((state->received_param < state->total_param) ||
2981 (state->received_data < state->total_data)) {
2982 END_PROFILE(SMBnttranss);
2983 return;
2986 handle_nttrans(conn, state, req);
2988 DLIST_REMOVE(conn->pending_trans, state);
2989 SAFE_FREE(state->data);
2990 SAFE_FREE(state->param);
2991 TALLOC_FREE(state);
2992 END_PROFILE(SMBnttranss);
2993 return;
2995 bad_param:
2997 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2998 DLIST_REMOVE(conn->pending_trans, state);
2999 SAFE_FREE(state->data);
3000 SAFE_FREE(state->param);
3001 TALLOC_FREE(state);
3002 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3003 END_PROFILE(SMBnttranss);
3004 return;