s4:provision - Updated FDS schema mapping.
[Samba/kamenim.git] / source3 / smbd / nttrans.c
blob93621dde618f028b7c525247507e0c55d3abcf11
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 "smbd/globals.h"
24 extern const struct generic_mapping file_generic_mapping;
26 static char *nttrans_realloc(char **ptr, size_t size)
28 if (ptr==NULL) {
29 smb_panic("nttrans_realloc() called with NULL ptr");
32 *ptr = (char *)SMB_REALLOC(*ptr, size);
33 if(*ptr == NULL) {
34 return NULL;
36 memset(*ptr,'\0',size);
37 return *ptr;
40 /****************************************************************************
41 Send the required number of replies back.
42 We assume all fields other than the data fields are
43 set correctly for the type of call.
44 HACK ! Always assumes smb_setup field is zero.
45 ****************************************************************************/
47 void send_nt_replies(connection_struct *conn,
48 struct smb_request *req, NTSTATUS nt_error,
49 char *params, int paramsize,
50 char *pdata, int datasize)
52 int data_to_send = datasize;
53 int params_to_send = paramsize;
54 int useable_space;
55 char *pp = params;
56 char *pd = pdata;
57 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
58 int alignment_offset = 3;
59 int data_alignment_offset = 0;
60 struct smbd_server_connection *sconn = smbd_server_conn;
61 int max_send = sconn->smb1.sessions.max_send;
64 * If there genuinely are no parameters or data to send just send
65 * the empty packet.
68 if(params_to_send == 0 && data_to_send == 0) {
69 reply_outbuf(req, 18, 0);
70 if (NT_STATUS_V(nt_error)) {
71 error_packet_set((char *)req->outbuf,
72 0, 0, nt_error,
73 __LINE__,__FILE__);
75 show_msg((char *)req->outbuf);
76 if (!srv_send_smb(smbd_server_fd(),
77 (char *)req->outbuf,
78 true, req->seqnum+1,
79 IS_CONN_ENCRYPTED(conn),
80 &req->pcd)) {
81 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
83 TALLOC_FREE(req->outbuf);
84 return;
88 * When sending params and data ensure that both are nicely aligned.
89 * Only do this alignment when there is also data to send - else
90 * can cause NT redirector problems.
93 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
94 data_alignment_offset = 4 - (params_to_send % 4);
98 * Space is bufsize minus Netbios over TCP header minus SMB header.
99 * The alignment_offset is to align the param bytes on a four byte
100 * boundary (2 bytes for data len, one byte pad).
101 * NT needs this to work correctly.
104 useable_space = max_send - (smb_size
105 + 2 * 18 /* wct */
106 + alignment_offset
107 + data_alignment_offset);
109 if (useable_space < 0) {
110 char *msg = talloc_asprintf(
111 talloc_tos(),
112 "send_nt_replies failed sanity useable_space = %d!!!",
113 useable_space);
114 DEBUG(0, ("%s\n", msg));
115 exit_server_cleanly(msg);
118 while (params_to_send || data_to_send) {
121 * Calculate whether we will totally or partially fill this packet.
124 total_sent_thistime = params_to_send + data_to_send;
127 * We can never send more than useable_space.
130 total_sent_thistime = MIN(total_sent_thistime, useable_space);
132 reply_outbuf(req, 18,
133 total_sent_thistime + alignment_offset
134 + data_alignment_offset);
137 * We might have had SMBnttranss in req->inbuf, fix that.
139 SCVAL(req->outbuf, smb_com, SMBnttrans);
142 * Set total params and data to be sent.
145 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
146 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
149 * Calculate how many parameters and data we can fit into
150 * this packet. Parameters get precedence.
153 params_sent_thistime = MIN(params_to_send,useable_space);
154 data_sent_thistime = useable_space - params_sent_thistime;
155 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
157 SIVAL(req->outbuf, smb_ntr_ParameterCount,
158 params_sent_thistime);
160 if(params_sent_thistime == 0) {
161 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
162 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
163 } else {
165 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
166 * parameter bytes, however the first 4 bytes of outbuf are
167 * the Netbios over TCP header. Thus use smb_base() to subtract
168 * them from the calculation.
171 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
172 ((smb_buf(req->outbuf)+alignment_offset)
173 - smb_base(req->outbuf)));
175 * Absolute displacement of param bytes sent in this packet.
178 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
179 pp - params);
183 * Deal with the data portion.
186 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
188 if(data_sent_thistime == 0) {
189 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
190 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
191 } else {
193 * The offset of the data bytes is the offset of the
194 * parameter bytes plus the number of parameters being sent this time.
197 SIVAL(req->outbuf, smb_ntr_DataOffset,
198 ((smb_buf(req->outbuf)+alignment_offset) -
199 smb_base(req->outbuf))
200 + params_sent_thistime + data_alignment_offset);
201 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
205 * Copy the param bytes into the packet.
208 if(params_sent_thistime) {
209 if (alignment_offset != 0) {
210 memset(smb_buf(req->outbuf), 0,
211 alignment_offset);
213 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
214 params_sent_thistime);
218 * Copy in the data bytes
221 if(data_sent_thistime) {
222 if (data_alignment_offset != 0) {
223 memset((smb_buf(req->outbuf)+alignment_offset+
224 params_sent_thistime), 0,
225 data_alignment_offset);
227 memcpy(smb_buf(req->outbuf)+alignment_offset
228 +params_sent_thistime+data_alignment_offset,
229 pd,data_sent_thistime);
232 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
233 params_sent_thistime, data_sent_thistime, useable_space));
234 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
235 params_to_send, data_to_send, paramsize, datasize));
237 if (NT_STATUS_V(nt_error)) {
238 error_packet_set((char *)req->outbuf,
239 0, 0, nt_error,
240 __LINE__,__FILE__);
243 /* Send the packet */
244 show_msg((char *)req->outbuf);
245 if (!srv_send_smb(smbd_server_fd(),
246 (char *)req->outbuf,
247 true, req->seqnum+1,
248 IS_CONN_ENCRYPTED(conn),
249 &req->pcd)) {
250 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
253 TALLOC_FREE(req->outbuf);
255 pp += params_sent_thistime;
256 pd += data_sent_thistime;
258 params_to_send -= params_sent_thistime;
259 data_to_send -= data_sent_thistime;
262 * Sanity check
265 if(params_to_send < 0 || data_to_send < 0) {
266 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
267 params_to_send, data_to_send));
268 exit_server_cleanly("send_nt_replies: internal error");
273 /****************************************************************************
274 Reply to an NT create and X call on a pipe
275 ****************************************************************************/
277 static void nt_open_pipe(char *fname, connection_struct *conn,
278 struct smb_request *req, int *ppnum)
280 files_struct *fsp;
281 NTSTATUS status;
283 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
285 /* Strip \\ off the name. */
286 fname++;
288 status = open_np_file(req, fname, &fsp);
289 if (!NT_STATUS_IS_OK(status)) {
290 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
291 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
292 ERRDOS, ERRbadpipe);
293 return;
295 reply_nterror(req, status);
296 return;
299 *ppnum = fsp->fnum;
300 return;
303 /****************************************************************************
304 Reply to an NT create and X call for pipes.
305 ****************************************************************************/
307 static void do_ntcreate_pipe_open(connection_struct *conn,
308 struct smb_request *req)
310 char *fname = NULL;
311 int pnum = -1;
312 char *p = NULL;
313 uint32 flags = IVAL(req->vwv+3, 1);
314 TALLOC_CTX *ctx = talloc_tos();
316 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
318 if (!fname) {
319 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
320 ERRDOS, ERRbadpipe);
321 return;
323 nt_open_pipe(fname, conn, req, &pnum);
325 if (req->outbuf) {
326 /* error reply */
327 return;
331 * Deal with pipe return.
334 if (flags & EXTENDED_RESPONSE_REQUIRED) {
335 /* This is very strange. We
336 * return 50 words, but only set
337 * the wcnt to 42 ? It's definately
338 * what happens on the wire....
340 reply_outbuf(req, 50, 0);
341 SCVAL(req->outbuf,smb_wct,42);
342 } else {
343 reply_outbuf(req, 34, 0);
346 p = (char *)req->outbuf + smb_vwv2;
347 p++;
348 SSVAL(p,0,pnum);
349 p += 2;
350 SIVAL(p,0,FILE_WAS_OPENED);
351 p += 4;
352 p += 32;
353 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
354 p += 20;
355 /* File type. */
356 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
357 /* Device state. */
358 SSVAL(p,2, 0x5FF); /* ? */
359 p += 4;
361 if (flags & EXTENDED_RESPONSE_REQUIRED) {
362 p += 25;
363 SIVAL(p,0,FILE_GENERIC_ALL);
365 * For pipes W2K3 seems to return
366 * 0x12019B next.
367 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
369 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
372 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
374 chain_reply(req);
377 struct case_semantics_state {
378 connection_struct *conn;
379 bool case_sensitive;
380 bool case_preserve;
381 bool short_case_preserve;
384 /****************************************************************************
385 Restore case semantics.
386 ****************************************************************************/
388 static int restore_case_semantics(struct case_semantics_state *state)
390 state->conn->case_sensitive = state->case_sensitive;
391 state->conn->case_preserve = state->case_preserve;
392 state->conn->short_case_preserve = state->short_case_preserve;
393 return 0;
396 /****************************************************************************
397 Save case semantics.
398 ****************************************************************************/
400 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
401 connection_struct *conn)
403 struct case_semantics_state *result;
405 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
406 return NULL;
409 result->conn = conn;
410 result->case_sensitive = conn->case_sensitive;
411 result->case_preserve = conn->case_preserve;
412 result->short_case_preserve = conn->short_case_preserve;
414 /* Set to POSIX. */
415 conn->case_sensitive = True;
416 conn->case_preserve = True;
417 conn->short_case_preserve = True;
419 talloc_set_destructor(result, restore_case_semantics);
421 return result;
424 /****************************************************************************
425 Reply to an NT create and X call.
426 ****************************************************************************/
428 void reply_ntcreate_and_X(struct smb_request *req)
430 connection_struct *conn = req->conn;
431 struct smb_filename *smb_fname = NULL;
432 char *fname = NULL;
433 uint32 flags;
434 uint32 access_mask;
435 uint32 file_attributes;
436 uint32 share_access;
437 uint32 create_disposition;
438 uint32 create_options;
439 uint16 root_dir_fid;
440 uint64_t allocation_size;
441 /* Breakout the oplock request bits so we can set the
442 reply bits separately. */
443 uint32 fattr=0;
444 SMB_OFF_T file_len = 0;
445 int info = 0;
446 files_struct *fsp = NULL;
447 char *p = NULL;
448 struct timespec create_timespec;
449 struct timespec c_timespec;
450 struct timespec a_timespec;
451 struct timespec m_timespec;
452 struct timespec write_time_ts;
453 NTSTATUS status;
454 int oplock_request;
455 uint8_t oplock_granted = NO_OPLOCK_RETURN;
456 struct case_semantics_state *case_state = NULL;
457 TALLOC_CTX *ctx = talloc_tos();
459 START_PROFILE(SMBntcreateX);
461 if (req->wct < 24) {
462 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
463 goto out;
466 flags = IVAL(req->vwv+3, 1);
467 access_mask = IVAL(req->vwv+7, 1);
468 file_attributes = IVAL(req->vwv+13, 1);
469 share_access = IVAL(req->vwv+15, 1);
470 create_disposition = IVAL(req->vwv+17, 1);
471 create_options = IVAL(req->vwv+19, 1);
472 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
474 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
475 #ifdef LARGE_SMB_OFF_T
476 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
477 #endif
479 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
480 STR_TERMINATE, &status);
482 if (!NT_STATUS_IS_OK(status)) {
483 reply_nterror(req, status);
484 goto out;
487 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
488 "file_attributes = 0x%x, share_access = 0x%x, "
489 "create_disposition = 0x%x create_options = 0x%x "
490 "root_dir_fid = 0x%x, fname = %s\n",
491 (unsigned int)flags,
492 (unsigned int)access_mask,
493 (unsigned int)file_attributes,
494 (unsigned int)share_access,
495 (unsigned int)create_disposition,
496 (unsigned int)create_options,
497 (unsigned int)root_dir_fid,
498 fname));
501 * we need to remove ignored bits when they come directly from the client
502 * because we reuse some of them for internal stuff
504 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
507 * If it's an IPC, use the pipe handler.
510 if (IS_IPC(conn)) {
511 if (lp_nt_pipe_support()) {
512 do_ntcreate_pipe_open(conn, req);
513 goto out;
515 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
516 goto out;
519 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
520 if (oplock_request) {
521 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
522 ? BATCH_OPLOCK : 0;
525 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
526 case_state = set_posix_case_semantics(ctx, conn);
527 if (!case_state) {
528 reply_nterror(req, NT_STATUS_NO_MEMORY);
529 goto out;
533 status = filename_convert(ctx,
534 conn,
535 req->flags2 & FLAGS2_DFS_PATHNAMES,
536 fname,
538 NULL,
539 &smb_fname);
541 TALLOC_FREE(case_state);
543 if (!NT_STATUS_IS_OK(status)) {
544 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
545 reply_botherror(req,
546 NT_STATUS_PATH_NOT_COVERED,
547 ERRSRV, ERRbadpath);
548 goto out;
550 reply_nterror(req, status);
551 goto out;
555 * Bug #6898 - clients using Windows opens should
556 * never be able to set this attribute into the
557 * VFS.
559 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
561 status = SMB_VFS_CREATE_FILE(
562 conn, /* conn */
563 req, /* req */
564 root_dir_fid, /* root_dir_fid */
565 smb_fname, /* fname */
566 access_mask, /* access_mask */
567 share_access, /* share_access */
568 create_disposition, /* create_disposition*/
569 create_options, /* create_options */
570 file_attributes, /* file_attributes */
571 oplock_request, /* oplock_request */
572 allocation_size, /* allocation_size */
573 0, /* private_flags */
574 NULL, /* sd */
575 NULL, /* ea_list */
576 &fsp, /* result */
577 &info); /* pinfo */
579 if (!NT_STATUS_IS_OK(status)) {
580 if (open_was_deferred(req->mid)) {
581 /* We have re-scheduled this call, no error. */
582 goto out;
584 reply_openerror(req, status);
585 goto out;
588 /* Ensure we're pointing at the correct stat struct. */
589 TALLOC_FREE(smb_fname);
590 smb_fname = fsp->fsp_name;
593 * If the caller set the extended oplock request bit
594 * and we granted one (by whatever means) - set the
595 * correct bit for extended oplock reply.
598 if (oplock_request &&
599 (lp_fake_oplocks(SNUM(conn))
600 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
603 * Exclusive oplock granted
606 if (flags & REQUEST_BATCH_OPLOCK) {
607 oplock_granted = BATCH_OPLOCK_RETURN;
608 } else {
609 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
611 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
612 oplock_granted = LEVEL_II_OPLOCK_RETURN;
613 } else {
614 oplock_granted = NO_OPLOCK_RETURN;
617 file_len = smb_fname->st.st_ex_size;
619 if (flags & EXTENDED_RESPONSE_REQUIRED) {
620 /* This is very strange. We
621 * return 50 words, but only set
622 * the wcnt to 42 ? It's definately
623 * what happens on the wire....
625 reply_outbuf(req, 50, 0);
626 SCVAL(req->outbuf,smb_wct,42);
627 } else {
628 reply_outbuf(req, 34, 0);
631 p = (char *)req->outbuf + smb_vwv2;
633 SCVAL(p, 0, oplock_granted);
635 p++;
636 SSVAL(p,0,fsp->fnum);
637 p += 2;
638 if ((create_disposition == FILE_SUPERSEDE)
639 && (info == FILE_WAS_OVERWRITTEN)) {
640 SIVAL(p,0,FILE_WAS_SUPERSEDED);
641 } else {
642 SIVAL(p,0,info);
644 p += 4;
646 fattr = dos_mode(conn, smb_fname);
647 if (fattr == 0) {
648 fattr = FILE_ATTRIBUTE_NORMAL;
651 /* Deal with other possible opens having a modified
652 write time. JRA. */
653 ZERO_STRUCT(write_time_ts);
654 get_file_infos(fsp->file_id, NULL, &write_time_ts);
655 if (!null_timespec(write_time_ts)) {
656 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
659 /* Create time. */
660 create_timespec = get_create_timespec(conn, fsp, smb_fname);
661 a_timespec = smb_fname->st.st_ex_atime;
662 m_timespec = smb_fname->st.st_ex_mtime;
663 c_timespec = get_change_timespec(conn, fsp, smb_fname);
665 if (lp_dos_filetime_resolution(SNUM(conn))) {
666 dos_filetime_timespec(&create_timespec);
667 dos_filetime_timespec(&a_timespec);
668 dos_filetime_timespec(&m_timespec);
669 dos_filetime_timespec(&c_timespec);
672 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
673 p += 8;
674 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
675 p += 8;
676 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
677 p += 8;
678 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
679 p += 8;
680 SIVAL(p,0,fattr); /* File Attributes. */
681 p += 4;
682 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
683 p += 8;
684 SOFF_T(p,0,file_len);
685 p += 8;
686 if (flags & EXTENDED_RESPONSE_REQUIRED) {
687 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
688 size_t num_names = 0;
689 unsigned int num_streams;
690 struct stream_struct *streams = NULL;
692 /* Do we have any EA's ? */
693 status = get_ea_names_from_file(ctx, conn, fsp,
694 smb_fname->base_name, NULL, &num_names);
695 if (NT_STATUS_IS_OK(status) && num_names) {
696 file_status &= ~NO_EAS;
698 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
699 &num_streams, &streams);
700 /* There is always one stream, ::$DATA. */
701 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
702 file_status &= ~NO_SUBSTREAMS;
704 TALLOC_FREE(streams);
705 SSVAL(p,2,file_status);
707 p += 4;
708 SCVAL(p,0,fsp->is_directory ? 1 : 0);
710 if (flags & EXTENDED_RESPONSE_REQUIRED) {
711 uint32 perms = 0;
712 p += 25;
713 if (fsp->is_directory ||
714 can_write_to_file(conn, smb_fname)) {
715 perms = FILE_GENERIC_ALL;
716 } else {
717 perms = FILE_GENERIC_READ|FILE_EXECUTE;
719 SIVAL(p,0,perms);
722 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
723 fsp->fnum, smb_fname_str_dbg(smb_fname)));
725 chain_reply(req);
726 out:
727 END_PROFILE(SMBntcreateX);
728 return;
731 /****************************************************************************
732 Reply to a NT_TRANSACT_CREATE call to open a pipe.
733 ****************************************************************************/
735 static void do_nt_transact_create_pipe(connection_struct *conn,
736 struct smb_request *req,
737 uint16 **ppsetup, uint32 setup_count,
738 char **ppparams, uint32 parameter_count,
739 char **ppdata, uint32 data_count)
741 char *fname = NULL;
742 char *params = *ppparams;
743 int pnum = -1;
744 char *p = NULL;
745 NTSTATUS status;
746 size_t param_len;
747 uint32 flags;
748 TALLOC_CTX *ctx = talloc_tos();
751 * Ensure minimum number of parameters sent.
754 if(parameter_count < 54) {
755 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
756 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
757 return;
760 flags = IVAL(params,0);
762 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
763 parameter_count-53, STR_TERMINATE,
764 &status);
765 if (!NT_STATUS_IS_OK(status)) {
766 reply_nterror(req, status);
767 return;
770 nt_open_pipe(fname, conn, req, &pnum);
772 if (req->outbuf) {
773 /* Error return */
774 return;
777 /* Realloc the size of parameters and data we will return */
778 if (flags & EXTENDED_RESPONSE_REQUIRED) {
779 /* Extended response is 32 more byyes. */
780 param_len = 101;
781 } else {
782 param_len = 69;
784 params = nttrans_realloc(ppparams, param_len);
785 if(params == NULL) {
786 reply_nterror(req, NT_STATUS_NO_MEMORY);
787 return;
790 p = params;
791 SCVAL(p,0,NO_OPLOCK_RETURN);
793 p += 2;
794 SSVAL(p,0,pnum);
795 p += 2;
796 SIVAL(p,0,FILE_WAS_OPENED);
797 p += 8;
799 p += 32;
800 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
801 p += 20;
802 /* File type. */
803 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
804 /* Device state. */
805 SSVAL(p,2, 0x5FF); /* ? */
806 p += 4;
808 if (flags & EXTENDED_RESPONSE_REQUIRED) {
809 p += 25;
810 SIVAL(p,0,FILE_GENERIC_ALL);
812 * For pipes W2K3 seems to return
813 * 0x12019B next.
814 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
816 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
819 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
821 /* Send the required number of replies */
822 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
824 return;
827 /****************************************************************************
828 Internal fn to set security descriptors.
829 ****************************************************************************/
831 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
832 uint32 security_info_sent)
834 SEC_DESC *psd = NULL;
835 NTSTATUS status;
837 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
838 return NT_STATUS_OK;
841 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
843 if (!NT_STATUS_IS_OK(status)) {
844 return status;
847 if (psd->owner_sid == NULL) {
848 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
850 if (psd->group_sid == NULL) {
851 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
854 /* Convert all the generic bits. */
855 security_acl_map_generic(psd->dacl, &file_generic_mapping);
856 security_acl_map_generic(psd->sacl, &file_generic_mapping);
858 if (DEBUGLEVEL >= 10) {
859 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
860 NDR_PRINT_DEBUG(security_descriptor, psd);
863 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
865 TALLOC_FREE(psd);
867 return status;
870 /****************************************************************************
871 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
872 ****************************************************************************/
874 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
876 struct ea_list *ea_list_head = NULL;
877 size_t offset = 0;
879 if (data_size < 4) {
880 return NULL;
883 while (offset + 4 <= data_size) {
884 size_t next_offset = IVAL(pdata,offset);
885 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
887 if (!eal) {
888 return NULL;
891 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
892 if (next_offset == 0) {
893 break;
895 offset += next_offset;
898 return ea_list_head;
901 /****************************************************************************
902 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
903 ****************************************************************************/
905 static void call_nt_transact_create(connection_struct *conn,
906 struct smb_request *req,
907 uint16 **ppsetup, uint32 setup_count,
908 char **ppparams, uint32 parameter_count,
909 char **ppdata, uint32 data_count,
910 uint32 max_data_count)
912 struct smb_filename *smb_fname = NULL;
913 char *fname = NULL;
914 char *params = *ppparams;
915 char *data = *ppdata;
916 /* Breakout the oplock request bits so we can set the reply bits separately. */
917 uint32 fattr=0;
918 SMB_OFF_T file_len = 0;
919 int info = 0;
920 files_struct *fsp = NULL;
921 char *p = NULL;
922 uint32 flags;
923 uint32 access_mask;
924 uint32 file_attributes;
925 uint32 share_access;
926 uint32 create_disposition;
927 uint32 create_options;
928 uint32 sd_len;
929 struct security_descriptor *sd = NULL;
930 uint32 ea_len;
931 uint16 root_dir_fid;
932 struct timespec create_timespec;
933 struct timespec c_timespec;
934 struct timespec a_timespec;
935 struct timespec m_timespec;
936 struct timespec write_time_ts;
937 struct ea_list *ea_list = NULL;
938 NTSTATUS status;
939 size_t param_len;
940 uint64_t allocation_size;
941 int oplock_request;
942 uint8_t oplock_granted;
943 struct case_semantics_state *case_state = NULL;
944 TALLOC_CTX *ctx = talloc_tos();
946 DEBUG(5,("call_nt_transact_create\n"));
949 * If it's an IPC, use the pipe handler.
952 if (IS_IPC(conn)) {
953 if (lp_nt_pipe_support()) {
954 do_nt_transact_create_pipe(
955 conn, req,
956 ppsetup, setup_count,
957 ppparams, parameter_count,
958 ppdata, data_count);
959 goto out;
961 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
962 goto out;
966 * Ensure minimum number of parameters sent.
969 if(parameter_count < 54) {
970 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
971 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
972 goto out;
975 flags = IVAL(params,0);
976 access_mask = IVAL(params,8);
977 file_attributes = IVAL(params,20);
978 share_access = IVAL(params,24);
979 create_disposition = IVAL(params,28);
980 create_options = IVAL(params,32);
981 sd_len = IVAL(params,36);
982 ea_len = IVAL(params,40);
983 root_dir_fid = (uint16)IVAL(params,4);
984 allocation_size = (uint64_t)IVAL(params,12);
985 #ifdef LARGE_SMB_OFF_T
986 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
987 #endif
990 * we need to remove ignored bits when they come directly from the client
991 * because we reuse some of them for internal stuff
993 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
995 /* Ensure the data_len is correct for the sd and ea values given. */
996 if ((ea_len + sd_len > data_count)
997 || (ea_len > data_count) || (sd_len > data_count)
998 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
999 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1000 "%u, data_count = %u\n", (unsigned int)ea_len,
1001 (unsigned int)sd_len, (unsigned int)data_count));
1002 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1003 goto out;
1006 if (sd_len) {
1007 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1008 sd_len));
1010 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1011 &sd);
1012 if (!NT_STATUS_IS_OK(status)) {
1013 DEBUG(10, ("call_nt_transact_create: "
1014 "unmarshall_sec_desc failed: %s\n",
1015 nt_errstr(status)));
1016 reply_nterror(req, status);
1017 goto out;
1021 if (ea_len) {
1022 if (!lp_ea_support(SNUM(conn))) {
1023 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1024 "EA's not supported.\n",
1025 (unsigned int)ea_len));
1026 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1027 goto out;
1030 if (ea_len < 10) {
1031 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1032 "too small (should be more than 10)\n",
1033 (unsigned int)ea_len ));
1034 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1035 goto out;
1038 /* We have already checked that ea_len <= data_count here. */
1039 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1040 ea_len);
1041 if (ea_list == NULL) {
1042 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1043 goto out;
1047 srvstr_get_path(ctx, params, req->flags2, &fname,
1048 params+53, parameter_count-53,
1049 STR_TERMINATE, &status);
1050 if (!NT_STATUS_IS_OK(status)) {
1051 reply_nterror(req, status);
1052 goto out;
1055 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1056 case_state = set_posix_case_semantics(ctx, conn);
1057 if (!case_state) {
1058 reply_nterror(req, NT_STATUS_NO_MEMORY);
1059 goto out;
1063 status = filename_convert(ctx,
1064 conn,
1065 req->flags2 & FLAGS2_DFS_PATHNAMES,
1066 fname,
1068 NULL,
1069 &smb_fname);
1071 TALLOC_FREE(case_state);
1073 if (!NT_STATUS_IS_OK(status)) {
1074 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1075 reply_botherror(req,
1076 NT_STATUS_PATH_NOT_COVERED,
1077 ERRSRV, ERRbadpath);
1078 goto out;
1080 reply_nterror(req, status);
1081 goto out;
1084 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1085 if (oplock_request) {
1086 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1087 ? BATCH_OPLOCK : 0;
1091 * Bug #6898 - clients using Windows opens should
1092 * never be able to set this attribute into the
1093 * VFS.
1095 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1097 status = SMB_VFS_CREATE_FILE(
1098 conn, /* conn */
1099 req, /* req */
1100 root_dir_fid, /* root_dir_fid */
1101 smb_fname, /* fname */
1102 access_mask, /* access_mask */
1103 share_access, /* share_access */
1104 create_disposition, /* create_disposition*/
1105 create_options, /* create_options */
1106 file_attributes, /* file_attributes */
1107 oplock_request, /* oplock_request */
1108 allocation_size, /* allocation_size */
1109 0, /* private_flags */
1110 sd, /* sd */
1111 ea_list, /* ea_list */
1112 &fsp, /* result */
1113 &info); /* pinfo */
1115 if(!NT_STATUS_IS_OK(status)) {
1116 if (open_was_deferred(req->mid)) {
1117 /* We have re-scheduled this call, no error. */
1118 return;
1120 reply_openerror(req, status);
1121 goto out;
1124 /* Ensure we're pointing at the correct stat struct. */
1125 TALLOC_FREE(smb_fname);
1126 smb_fname = fsp->fsp_name;
1129 * If the caller set the extended oplock request bit
1130 * and we granted one (by whatever means) - set the
1131 * correct bit for extended oplock reply.
1134 if (oplock_request &&
1135 (lp_fake_oplocks(SNUM(conn))
1136 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1139 * Exclusive oplock granted
1142 if (flags & REQUEST_BATCH_OPLOCK) {
1143 oplock_granted = BATCH_OPLOCK_RETURN;
1144 } else {
1145 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1147 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1148 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1149 } else {
1150 oplock_granted = NO_OPLOCK_RETURN;
1153 file_len = smb_fname->st.st_ex_size;
1155 /* Realloc the size of parameters and data we will return */
1156 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1157 /* Extended response is 32 more byyes. */
1158 param_len = 101;
1159 } else {
1160 param_len = 69;
1162 params = nttrans_realloc(ppparams, param_len);
1163 if(params == NULL) {
1164 reply_nterror(req, NT_STATUS_NO_MEMORY);
1165 goto out;
1168 p = params;
1169 SCVAL(p, 0, oplock_granted);
1171 p += 2;
1172 SSVAL(p,0,fsp->fnum);
1173 p += 2;
1174 if ((create_disposition == FILE_SUPERSEDE)
1175 && (info == FILE_WAS_OVERWRITTEN)) {
1176 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1177 } else {
1178 SIVAL(p,0,info);
1180 p += 8;
1182 fattr = dos_mode(conn, smb_fname);
1183 if (fattr == 0) {
1184 fattr = FILE_ATTRIBUTE_NORMAL;
1187 /* Deal with other possible opens having a modified
1188 write time. JRA. */
1189 ZERO_STRUCT(write_time_ts);
1190 get_file_infos(fsp->file_id, NULL, &write_time_ts);
1191 if (!null_timespec(write_time_ts)) {
1192 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1195 /* Create time. */
1196 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1197 a_timespec = smb_fname->st.st_ex_atime;
1198 m_timespec = smb_fname->st.st_ex_mtime;
1199 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1201 if (lp_dos_filetime_resolution(SNUM(conn))) {
1202 dos_filetime_timespec(&create_timespec);
1203 dos_filetime_timespec(&a_timespec);
1204 dos_filetime_timespec(&m_timespec);
1205 dos_filetime_timespec(&c_timespec);
1208 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1209 p += 8;
1210 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1211 p += 8;
1212 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1213 p += 8;
1214 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1215 p += 8;
1216 SIVAL(p,0,fattr); /* File Attributes. */
1217 p += 4;
1218 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1219 p += 8;
1220 SOFF_T(p,0,file_len);
1221 p += 8;
1222 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1223 SSVAL(p,2,0x7);
1225 p += 4;
1226 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1228 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1229 uint32 perms = 0;
1230 p += 25;
1231 if (fsp->is_directory ||
1232 can_write_to_file(conn, smb_fname)) {
1233 perms = FILE_GENERIC_ALL;
1234 } else {
1235 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1237 SIVAL(p,0,perms);
1240 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1241 smb_fname_str_dbg(smb_fname)));
1243 /* Send the required number of replies */
1244 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1245 out:
1246 return;
1249 /****************************************************************************
1250 Reply to a NT CANCEL request.
1251 conn POINTER CAN BE NULL HERE !
1252 ****************************************************************************/
1254 void reply_ntcancel(struct smb_request *req)
1257 * Go through and cancel any pending change notifies.
1260 START_PROFILE(SMBntcancel);
1261 srv_cancel_sign_response(smbd_server_conn);
1262 remove_pending_change_notify_requests_by_mid(req->mid);
1263 remove_pending_lock_requests_by_mid(req->mid);
1265 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1267 END_PROFILE(SMBntcancel);
1268 return;
1271 /****************************************************************************
1272 Copy a file.
1273 ****************************************************************************/
1275 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1276 connection_struct *conn,
1277 struct smb_request *req,
1278 struct smb_filename *smb_fname_src,
1279 struct smb_filename *smb_fname_dst,
1280 uint32 attrs)
1282 files_struct *fsp1,*fsp2;
1283 uint32 fattr;
1284 int info;
1285 SMB_OFF_T ret=-1;
1286 NTSTATUS status = NT_STATUS_OK;
1287 char *parent;
1289 if (!CAN_WRITE(conn)) {
1290 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1291 goto out;
1294 /* Source must already exist. */
1295 if (!VALID_STAT(smb_fname_src->st)) {
1296 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1297 goto out;
1300 /* Ensure attributes match. */
1301 fattr = dos_mode(conn, smb_fname_src);
1302 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1303 status = NT_STATUS_NO_SUCH_FILE;
1304 goto out;
1307 /* Disallow if dst file already exists. */
1308 if (VALID_STAT(smb_fname_dst->st)) {
1309 status = NT_STATUS_OBJECT_NAME_COLLISION;
1310 goto out;
1313 /* No links from a directory. */
1314 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1315 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1316 goto out;
1319 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1320 smb_fname_str_dbg(smb_fname_src),
1321 smb_fname_str_dbg(smb_fname_dst)));
1323 status = SMB_VFS_CREATE_FILE(
1324 conn, /* conn */
1325 req, /* req */
1326 0, /* root_dir_fid */
1327 smb_fname_src, /* fname */
1328 FILE_READ_DATA, /* access_mask */
1329 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1330 FILE_SHARE_DELETE),
1331 FILE_OPEN, /* create_disposition*/
1332 0, /* create_options */
1333 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1334 NO_OPLOCK, /* oplock_request */
1335 0, /* allocation_size */
1336 0, /* private_flags */
1337 NULL, /* sd */
1338 NULL, /* ea_list */
1339 &fsp1, /* result */
1340 &info); /* pinfo */
1342 if (!NT_STATUS_IS_OK(status)) {
1343 goto out;
1346 status = SMB_VFS_CREATE_FILE(
1347 conn, /* conn */
1348 req, /* req */
1349 0, /* root_dir_fid */
1350 smb_fname_dst, /* fname */
1351 FILE_WRITE_DATA, /* access_mask */
1352 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1353 FILE_SHARE_DELETE),
1354 FILE_CREATE, /* create_disposition*/
1355 0, /* create_options */
1356 fattr, /* file_attributes */
1357 NO_OPLOCK, /* oplock_request */
1358 0, /* allocation_size */
1359 0, /* private_flags */
1360 NULL, /* sd */
1361 NULL, /* ea_list */
1362 &fsp2, /* result */
1363 &info); /* pinfo */
1365 if (!NT_STATUS_IS_OK(status)) {
1366 close_file(NULL, fsp1, ERROR_CLOSE);
1367 goto out;
1370 if (smb_fname_src->st.st_ex_size) {
1371 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1375 * As we are opening fsp1 read-only we only expect
1376 * an error on close on fsp2 if we are out of space.
1377 * Thus we don't look at the error return from the
1378 * close of fsp1.
1380 close_file(NULL, fsp1, NORMAL_CLOSE);
1382 /* Ensure the modtime is set correctly on the destination file. */
1383 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1385 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1387 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1388 creates the file. This isn't the correct thing to do in the copy
1389 case. JRA */
1390 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1391 NULL)) {
1392 status = NT_STATUS_NO_MEMORY;
1393 goto out;
1395 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1396 TALLOC_FREE(parent);
1398 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1399 status = NT_STATUS_DISK_FULL;
1400 goto out;
1402 out:
1403 if (!NT_STATUS_IS_OK(status)) {
1404 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1405 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1406 smb_fname_str_dbg(smb_fname_dst)));
1409 return status;
1412 /****************************************************************************
1413 Reply to a NT rename request.
1414 ****************************************************************************/
1416 void reply_ntrename(struct smb_request *req)
1418 connection_struct *conn = req->conn;
1419 struct smb_filename *smb_fname_old = NULL;
1420 struct smb_filename *smb_fname_new = NULL;
1421 char *oldname = NULL;
1422 char *newname = NULL;
1423 const char *p;
1424 NTSTATUS status;
1425 bool src_has_wcard = False;
1426 bool dest_has_wcard = False;
1427 uint32 attrs;
1428 uint32_t ucf_flags_src = 0;
1429 uint32_t ucf_flags_dst = 0;
1430 uint16 rename_type;
1431 TALLOC_CTX *ctx = talloc_tos();
1433 START_PROFILE(SMBntrename);
1435 if (req->wct < 4) {
1436 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1437 goto out;
1440 attrs = SVAL(req->vwv+0, 0);
1441 rename_type = SVAL(req->vwv+1, 0);
1443 p = (const char *)req->buf + 1;
1444 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1445 &status, &src_has_wcard);
1446 if (!NT_STATUS_IS_OK(status)) {
1447 reply_nterror(req, status);
1448 goto out;
1451 if (ms_has_wild(oldname)) {
1452 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1453 goto out;
1456 p++;
1457 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1458 &status, &dest_has_wcard);
1459 if (!NT_STATUS_IS_OK(status)) {
1460 reply_nterror(req, status);
1461 goto out;
1464 /* The newname must begin with a ':' if the oldname contains a ':'. */
1465 if (strchr_m(oldname, ':') && (newname[0] != ':')) {
1466 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1467 goto out;
1471 * If this is a rename operation, allow wildcards and save the
1472 * destination's last component.
1474 if (rename_type == RENAME_FLAG_RENAME) {
1475 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1476 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1479 /* rename_internals() calls unix_convert(), so don't call it here. */
1480 status = filename_convert(ctx, conn,
1481 req->flags2 & FLAGS2_DFS_PATHNAMES,
1482 oldname,
1483 ucf_flags_src,
1484 NULL,
1485 &smb_fname_old);
1486 if (!NT_STATUS_IS_OK(status)) {
1487 if (NT_STATUS_EQUAL(status,
1488 NT_STATUS_PATH_NOT_COVERED)) {
1489 reply_botherror(req,
1490 NT_STATUS_PATH_NOT_COVERED,
1491 ERRSRV, ERRbadpath);
1492 goto out;
1494 reply_nterror(req, status);
1495 goto out;
1498 status = filename_convert(ctx, conn,
1499 req->flags2 & FLAGS2_DFS_PATHNAMES,
1500 newname,
1501 ucf_flags_dst,
1502 &dest_has_wcard,
1503 &smb_fname_new);
1504 if (!NT_STATUS_IS_OK(status)) {
1505 if (NT_STATUS_EQUAL(status,
1506 NT_STATUS_PATH_NOT_COVERED)) {
1507 reply_botherror(req,
1508 NT_STATUS_PATH_NOT_COVERED,
1509 ERRSRV, ERRbadpath);
1510 goto out;
1512 reply_nterror(req, status);
1513 goto out;
1516 DEBUG(3,("reply_ntrename: %s -> %s\n",
1517 smb_fname_str_dbg(smb_fname_old),
1518 smb_fname_str_dbg(smb_fname_new)));
1520 switch(rename_type) {
1521 case RENAME_FLAG_RENAME:
1522 status = rename_internals(ctx, conn, req,
1523 smb_fname_old, smb_fname_new,
1524 attrs, False, src_has_wcard,
1525 dest_has_wcard,
1526 DELETE_ACCESS);
1527 break;
1528 case RENAME_FLAG_HARD_LINK:
1529 if (src_has_wcard || dest_has_wcard) {
1530 /* No wildcards. */
1531 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1532 } else {
1533 status = hardlink_internals(ctx, conn,
1534 smb_fname_old,
1535 smb_fname_new);
1537 break;
1538 case RENAME_FLAG_COPY:
1539 if (src_has_wcard || dest_has_wcard) {
1540 /* No wildcards. */
1541 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1542 } else {
1543 status = copy_internals(ctx, conn, req,
1544 smb_fname_old,
1545 smb_fname_new,
1546 attrs);
1548 break;
1549 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1550 status = NT_STATUS_INVALID_PARAMETER;
1551 break;
1552 default:
1553 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1554 break;
1557 if (!NT_STATUS_IS_OK(status)) {
1558 if (open_was_deferred(req->mid)) {
1559 /* We have re-scheduled this call. */
1560 goto out;
1563 reply_nterror(req, status);
1564 goto out;
1567 reply_outbuf(req, 0, 0);
1568 out:
1569 END_PROFILE(SMBntrename);
1570 return;
1573 /****************************************************************************
1574 Reply to a notify change - queue the request and
1575 don't allow a directory to be opened.
1576 ****************************************************************************/
1578 static void smbd_smb1_notify_reply(struct smb_request *req,
1579 NTSTATUS error_code,
1580 uint8_t *buf, size_t len)
1582 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1585 static void call_nt_transact_notify_change(connection_struct *conn,
1586 struct smb_request *req,
1587 uint16 **ppsetup,
1588 uint32 setup_count,
1589 char **ppparams,
1590 uint32 parameter_count,
1591 char **ppdata, uint32 data_count,
1592 uint32 max_data_count,
1593 uint32 max_param_count)
1595 uint16 *setup = *ppsetup;
1596 files_struct *fsp;
1597 uint32 filter;
1598 NTSTATUS status;
1599 bool recursive;
1601 if(setup_count < 6) {
1602 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1603 return;
1606 fsp = file_fsp(req, SVAL(setup,4));
1607 filter = IVAL(setup, 0);
1608 recursive = (SVAL(setup, 6) != 0) ? True : False;
1610 DEBUG(3,("call_nt_transact_notify_change\n"));
1612 if(!fsp) {
1613 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1614 return;
1618 char *filter_string;
1620 if (!(filter_string = notify_filter_string(NULL, filter))) {
1621 reply_nterror(req,NT_STATUS_NO_MEMORY);
1622 return;
1625 DEBUG(3,("call_nt_transact_notify_change: notify change "
1626 "called on %s, filter = %s, recursive = %d\n",
1627 fsp_str_dbg(fsp), filter_string, recursive));
1629 TALLOC_FREE(filter_string);
1632 if((!fsp->is_directory) || (conn != fsp->conn)) {
1633 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1634 return;
1637 if (fsp->notify == NULL) {
1639 status = change_notify_create(fsp, filter, recursive);
1641 if (!NT_STATUS_IS_OK(status)) {
1642 DEBUG(10, ("change_notify_create returned %s\n",
1643 nt_errstr(status)));
1644 reply_nterror(req, status);
1645 return;
1649 if (fsp->notify->num_changes != 0) {
1652 * We've got changes pending, respond immediately
1656 * TODO: write a torture test to check the filtering behaviour
1657 * here.
1660 change_notify_reply(fsp->conn, req,
1661 NT_STATUS_OK,
1662 max_param_count,
1663 fsp->notify,
1664 smbd_smb1_notify_reply);
1667 * change_notify_reply() above has independently sent its
1668 * results
1670 return;
1674 * No changes pending, queue the request
1677 status = change_notify_add_request(req,
1678 max_param_count,
1679 filter,
1680 recursive, fsp,
1681 smbd_smb1_notify_reply);
1682 if (!NT_STATUS_IS_OK(status)) {
1683 reply_nterror(req, status);
1685 return;
1688 /****************************************************************************
1689 Reply to an NT transact rename command.
1690 ****************************************************************************/
1692 static void call_nt_transact_rename(connection_struct *conn,
1693 struct smb_request *req,
1694 uint16 **ppsetup, uint32 setup_count,
1695 char **ppparams, uint32 parameter_count,
1696 char **ppdata, uint32 data_count,
1697 uint32 max_data_count)
1699 char *params = *ppparams;
1700 char *new_name = NULL;
1701 files_struct *fsp = NULL;
1702 bool dest_has_wcard = False;
1703 NTSTATUS status;
1704 TALLOC_CTX *ctx = talloc_tos();
1706 if(parameter_count < 5) {
1707 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1708 return;
1711 fsp = file_fsp(req, SVAL(params, 0));
1712 if (!check_fsp(conn, req, fsp)) {
1713 return;
1715 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1716 parameter_count - 4,
1717 STR_TERMINATE, &status, &dest_has_wcard);
1718 if (!NT_STATUS_IS_OK(status)) {
1719 reply_nterror(req, status);
1720 return;
1724 * W2K3 ignores this request as the RAW-RENAME test
1725 * demonstrates, so we do.
1727 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1729 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1730 fsp_str_dbg(fsp), new_name));
1732 return;
1735 /******************************************************************************
1736 Fake up a completely empty SD.
1737 *******************************************************************************/
1739 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1741 size_t sd_size;
1743 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1744 if(!*ppsd) {
1745 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1746 return NT_STATUS_NO_MEMORY;
1749 return NT_STATUS_OK;
1752 /****************************************************************************
1753 Reply to query a security descriptor.
1754 ****************************************************************************/
1756 static void call_nt_transact_query_security_desc(connection_struct *conn,
1757 struct smb_request *req,
1758 uint16 **ppsetup,
1759 uint32 setup_count,
1760 char **ppparams,
1761 uint32 parameter_count,
1762 char **ppdata,
1763 uint32 data_count,
1764 uint32 max_data_count)
1766 char *params = *ppparams;
1767 char *data = *ppdata;
1768 SEC_DESC *psd = NULL;
1769 size_t sd_size;
1770 uint32 security_info_wanted;
1771 files_struct *fsp = NULL;
1772 NTSTATUS status;
1773 DATA_BLOB blob;
1775 if(parameter_count < 8) {
1776 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1777 return;
1780 fsp = file_fsp(req, SVAL(params,0));
1781 if(!fsp) {
1782 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1783 return;
1786 security_info_wanted = IVAL(params,4);
1788 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1789 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1790 (unsigned int)security_info_wanted));
1792 params = nttrans_realloc(ppparams, 4);
1793 if(params == NULL) {
1794 reply_nterror(req, NT_STATUS_NO_MEMORY);
1795 return;
1799 * Get the permissions to return.
1802 if (!lp_nt_acl_support(SNUM(conn))) {
1803 status = get_null_nt_acl(talloc_tos(), &psd);
1804 } else {
1805 status = SMB_VFS_FGET_NT_ACL(
1806 fsp, security_info_wanted, &psd);
1808 if (!NT_STATUS_IS_OK(status)) {
1809 reply_nterror(req, status);
1810 return;
1813 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1814 * present in the reply to match Windows behavior */
1815 if (psd->sacl == NULL &&
1816 security_info_wanted & SACL_SECURITY_INFORMATION)
1817 psd->type |= SEC_DESC_SACL_PRESENT;
1818 if (psd->dacl == NULL &&
1819 security_info_wanted & DACL_SECURITY_INFORMATION)
1820 psd->type |= SEC_DESC_DACL_PRESENT;
1822 sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1824 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1826 if (DEBUGLEVEL >= 10) {
1827 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n",
1828 fsp_str_dbg(fsp)));
1829 NDR_PRINT_DEBUG(security_descriptor, psd);
1832 SIVAL(params,0,(uint32)sd_size);
1834 if (max_data_count < sd_size) {
1835 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1836 params, 4, *ppdata, 0);
1837 return;
1841 * Allocate the data we will point this at.
1844 data = nttrans_realloc(ppdata, sd_size);
1845 if(data == NULL) {
1846 reply_nterror(req, NT_STATUS_NO_MEMORY);
1847 return;
1850 status = marshall_sec_desc(talloc_tos(), psd,
1851 &blob.data, &blob.length);
1853 if (!NT_STATUS_IS_OK(status)) {
1854 reply_nterror(req, status);
1855 return;
1858 SMB_ASSERT(sd_size == blob.length);
1859 memcpy(data, blob.data, sd_size);
1861 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1863 return;
1866 /****************************************************************************
1867 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1868 ****************************************************************************/
1870 static void call_nt_transact_set_security_desc(connection_struct *conn,
1871 struct smb_request *req,
1872 uint16 **ppsetup,
1873 uint32 setup_count,
1874 char **ppparams,
1875 uint32 parameter_count,
1876 char **ppdata,
1877 uint32 data_count,
1878 uint32 max_data_count)
1880 char *params= *ppparams;
1881 char *data = *ppdata;
1882 files_struct *fsp = NULL;
1883 uint32 security_info_sent = 0;
1884 NTSTATUS status;
1886 if(parameter_count < 8) {
1887 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1888 return;
1891 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1892 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1893 return;
1896 if(!lp_nt_acl_support(SNUM(conn))) {
1897 goto done;
1900 security_info_sent = IVAL(params,4);
1902 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
1903 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
1905 if (data_count == 0) {
1906 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1907 return;
1910 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1912 if (!NT_STATUS_IS_OK(status)) {
1913 reply_nterror(req, status);
1914 return;
1917 done:
1918 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1919 return;
1922 /****************************************************************************
1923 Reply to NT IOCTL
1924 ****************************************************************************/
1926 static void call_nt_transact_ioctl(connection_struct *conn,
1927 struct smb_request *req,
1928 uint16 **ppsetup, uint32 setup_count,
1929 char **ppparams, uint32 parameter_count,
1930 char **ppdata, uint32 data_count,
1931 uint32 max_data_count)
1933 uint32 function;
1934 uint16 fidnum;
1935 files_struct *fsp;
1936 uint8 isFSctl;
1937 uint8 compfilter;
1938 char *pdata = *ppdata;
1940 if (setup_count != 8) {
1941 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1942 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1943 return;
1946 function = IVAL(*ppsetup, 0);
1947 fidnum = SVAL(*ppsetup, 4);
1948 isFSctl = CVAL(*ppsetup, 6);
1949 compfilter = CVAL(*ppsetup, 7);
1951 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1952 function, fidnum, isFSctl, compfilter));
1954 fsp=file_fsp(req, fidnum);
1955 /* this check is done in each implemented function case for now
1956 because I don't want to break anything... --metze
1957 FSP_BELONGS_CONN(fsp,conn);*/
1959 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
1961 switch (function) {
1962 case FSCTL_SET_SPARSE:
1963 /* pretend this succeeded - tho strictly we should
1964 mark the file sparse (if the local fs supports it)
1965 so we can know if we need to pre-allocate or not */
1967 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1968 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1969 return;
1971 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1973 unsigned char objid[16];
1975 /* This should return the object-id on this file.
1976 * I think I'll make this be the inode+dev. JRA.
1979 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1981 if (!fsp_belongs_conn(conn, req, fsp)) {
1982 return;
1985 data_count = 64;
1986 pdata = nttrans_realloc(ppdata, data_count);
1987 if (pdata == NULL) {
1988 reply_nterror(req, NT_STATUS_NO_MEMORY);
1989 return;
1992 /* For backwards compatibility only store the dev/inode. */
1993 push_file_id_16(pdata, &fsp->file_id);
1994 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1995 push_file_id_16(pdata+32, &fsp->file_id);
1996 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1997 pdata, data_count);
1998 return;
2001 case FSCTL_GET_REPARSE_POINT:
2002 /* pretend this fail - my winXP does it like this
2003 * --metze
2006 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2007 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2008 return;
2010 case FSCTL_SET_REPARSE_POINT:
2011 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2012 * --metze
2015 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2016 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2017 return;
2019 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2022 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2023 * and return their volume names. If max_data_count is 16, then it is just
2024 * asking for the number of volumes and length of the combined names.
2026 * pdata is the data allocated by our caller, but that uses
2027 * total_data_count (which is 0 in our case) rather than max_data_count.
2028 * Allocate the correct amount and return the pointer to let
2029 * it be deallocated when we return.
2031 SHADOW_COPY_DATA *shadow_data = NULL;
2032 TALLOC_CTX *shadow_mem_ctx = NULL;
2033 bool labels = False;
2034 uint32 labels_data_count = 0;
2035 uint32 i;
2036 char *cur_pdata;
2038 if (!fsp_belongs_conn(conn, req, fsp)) {
2039 return;
2042 if (max_data_count < 16) {
2043 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2044 max_data_count));
2045 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2046 return;
2049 if (max_data_count > 16) {
2050 labels = True;
2053 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2054 if (shadow_mem_ctx == NULL) {
2055 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2056 reply_nterror(req, NT_STATUS_NO_MEMORY);
2057 return;
2060 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2061 if (shadow_data == NULL) {
2062 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2063 talloc_destroy(shadow_mem_ctx);
2064 reply_nterror(req, NT_STATUS_NO_MEMORY);
2065 return;
2068 shadow_data->mem_ctx = shadow_mem_ctx;
2071 * Call the VFS routine to actually do the work.
2073 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2074 talloc_destroy(shadow_data->mem_ctx);
2075 if (errno == ENOSYS) {
2076 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2077 conn->connectpath));
2078 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2079 return;
2080 } else {
2081 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2082 conn->connectpath));
2083 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2084 return;
2088 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2090 if (!labels) {
2091 data_count = 16;
2092 } else {
2093 data_count = 12+labels_data_count+4;
2096 if (max_data_count<data_count) {
2097 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2098 max_data_count,data_count));
2099 talloc_destroy(shadow_data->mem_ctx);
2100 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2101 return;
2104 pdata = nttrans_realloc(ppdata, data_count);
2105 if (pdata == NULL) {
2106 talloc_destroy(shadow_data->mem_ctx);
2107 reply_nterror(req, NT_STATUS_NO_MEMORY);
2108 return;
2111 cur_pdata = pdata;
2113 /* num_volumes 4 bytes */
2114 SIVAL(pdata,0,shadow_data->num_volumes);
2116 if (labels) {
2117 /* num_labels 4 bytes */
2118 SIVAL(pdata,4,shadow_data->num_volumes);
2121 /* needed_data_count 4 bytes */
2122 SIVAL(pdata, 8, labels_data_count+4);
2124 cur_pdata+=12;
2126 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2127 shadow_data->num_volumes, fsp_str_dbg(fsp)));
2128 if (labels && shadow_data->labels) {
2129 for (i=0;i<shadow_data->num_volumes;i++) {
2130 srvstr_push(pdata, req->flags2,
2131 cur_pdata, shadow_data->labels[i],
2132 2*sizeof(SHADOW_COPY_LABEL),
2133 STR_UNICODE|STR_TERMINATE);
2134 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2135 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2139 talloc_destroy(shadow_data->mem_ctx);
2141 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2142 pdata, data_count);
2144 return;
2147 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2149 /* pretend this succeeded -
2151 * we have to send back a list with all files owned by this SID
2153 * but I have to check that --metze
2155 DOM_SID sid;
2156 uid_t uid;
2157 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2159 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2161 if (!fsp_belongs_conn(conn, req, fsp)) {
2162 return;
2165 /* unknown 4 bytes: this is not the length of the sid :-( */
2166 /*unknown = IVAL(pdata,0);*/
2168 sid_parse(pdata+4,sid_len,&sid);
2169 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2171 if (!sid_to_uid(&sid, &uid)) {
2172 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2173 sid_string_dbg(&sid),
2174 (unsigned long)sid_len));
2175 uid = (-1);
2178 /* we can take a look at the find source :-)
2180 * find ./ -uid $uid -name '*' is what we need here
2183 * and send 4bytes len and then NULL terminated unicode strings
2184 * for each file
2186 * but I don't know how to deal with the paged results
2187 * (maybe we can hang the result anywhere in the fsp struct)
2189 * we don't send all files at once
2190 * and at the next we should *not* start from the beginning,
2191 * so we have to cache the result
2193 * --metze
2196 /* this works for now... */
2197 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2198 return;
2200 default:
2201 if (!logged_ioctl_message) {
2202 logged_ioctl_message = true; /* Only print this once... */
2203 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2204 function));
2208 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2212 #ifdef HAVE_SYS_QUOTAS
2213 /****************************************************************************
2214 Reply to get user quota
2215 ****************************************************************************/
2217 static void call_nt_transact_get_user_quota(connection_struct *conn,
2218 struct smb_request *req,
2219 uint16 **ppsetup,
2220 uint32 setup_count,
2221 char **ppparams,
2222 uint32 parameter_count,
2223 char **ppdata,
2224 uint32 data_count,
2225 uint32 max_data_count)
2227 NTSTATUS nt_status = NT_STATUS_OK;
2228 char *params = *ppparams;
2229 char *pdata = *ppdata;
2230 char *entry;
2231 int data_len=0,param_len=0;
2232 int qt_len=0;
2233 int entry_len = 0;
2234 files_struct *fsp = NULL;
2235 uint16 level = 0;
2236 size_t sid_len;
2237 DOM_SID sid;
2238 bool start_enum = True;
2239 SMB_NTQUOTA_STRUCT qt;
2240 SMB_NTQUOTA_LIST *tmp_list;
2241 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2243 ZERO_STRUCT(qt);
2245 /* access check */
2246 if (conn->server_info->utok.uid != 0) {
2247 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2248 "[%s]\n", lp_servicename(SNUM(conn)),
2249 conn->server_info->unix_name));
2250 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2251 return;
2255 * Ensure minimum number of parameters sent.
2258 if (parameter_count < 4) {
2259 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2260 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2261 return;
2264 /* maybe we can check the quota_fnum */
2265 fsp = file_fsp(req, SVAL(params,0));
2266 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2267 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2268 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2269 return;
2272 /* the NULL pointer checking for fsp->fake_file_handle->pd
2273 * is done by CHECK_NTQUOTA_HANDLE_OK()
2275 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2277 level = SVAL(params,2);
2279 /* unknown 12 bytes leading in params */
2281 switch (level) {
2282 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2283 /* seems that we should continue with the enum here --metze */
2285 if (qt_handle->quota_list!=NULL &&
2286 qt_handle->tmp_list==NULL) {
2288 /* free the list */
2289 free_ntquota_list(&(qt_handle->quota_list));
2291 /* Realloc the size of parameters and data we will return */
2292 param_len = 4;
2293 params = nttrans_realloc(ppparams, param_len);
2294 if(params == NULL) {
2295 reply_nterror(req, NT_STATUS_NO_MEMORY);
2296 return;
2299 data_len = 0;
2300 SIVAL(params,0,data_len);
2302 break;
2305 start_enum = False;
2307 case TRANSACT_GET_USER_QUOTA_LIST_START:
2309 if (qt_handle->quota_list==NULL &&
2310 qt_handle->tmp_list==NULL) {
2311 start_enum = True;
2314 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2315 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2316 return;
2319 /* Realloc the size of parameters and data we will return */
2320 param_len = 4;
2321 params = nttrans_realloc(ppparams, param_len);
2322 if(params == NULL) {
2323 reply_nterror(req, NT_STATUS_NO_MEMORY);
2324 return;
2327 /* we should not trust the value in max_data_count*/
2328 max_data_count = MIN(max_data_count,2048);
2330 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2331 if(pdata == NULL) {
2332 reply_nterror(req, NT_STATUS_NO_MEMORY);
2333 return;
2336 entry = pdata;
2338 /* set params Size of returned Quota Data 4 bytes*/
2339 /* but set it later when we know it */
2341 /* for each entry push the data */
2343 if (start_enum) {
2344 qt_handle->tmp_list = qt_handle->quota_list;
2347 tmp_list = qt_handle->tmp_list;
2349 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2350 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2352 sid_len = ndr_size_dom_sid(
2353 &tmp_list->quotas->sid, NULL, 0);
2354 entry_len = 40 + sid_len;
2356 /* nextoffset entry 4 bytes */
2357 SIVAL(entry,0,entry_len);
2359 /* then the len of the SID 4 bytes */
2360 SIVAL(entry,4,sid_len);
2362 /* unknown data 8 bytes uint64_t */
2363 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2365 /* the used disk space 8 bytes uint64_t */
2366 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2368 /* the soft quotas 8 bytes uint64_t */
2369 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2371 /* the hard quotas 8 bytes uint64_t */
2372 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2374 /* and now the SID */
2375 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2378 qt_handle->tmp_list = tmp_list;
2380 /* overwrite the offset of the last entry */
2381 SIVAL(entry-entry_len,0,0);
2383 data_len = 4+qt_len;
2384 /* overwrite the params quota_data_len */
2385 SIVAL(params,0,data_len);
2387 break;
2389 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2391 /* unknown 4 bytes IVAL(pdata,0) */
2393 if (data_count < 8) {
2394 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2395 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2396 return;
2399 sid_len = IVAL(pdata,4);
2400 /* Ensure this is less than 1mb. */
2401 if (sid_len > (1024*1024)) {
2402 reply_nterror(req, NT_STATUS_NO_MEMORY);
2403 return;
2406 if (data_count < 8+sid_len) {
2407 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2408 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2409 return;
2412 data_len = 4+40+sid_len;
2414 if (max_data_count < data_len) {
2415 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2416 max_data_count, data_len));
2417 param_len = 4;
2418 SIVAL(params,0,data_len);
2419 data_len = 0;
2420 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2421 break;
2424 sid_parse(pdata+8,sid_len,&sid);
2426 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2427 ZERO_STRUCT(qt);
2429 * we have to return zero's in all fields
2430 * instead of returning an error here
2431 * --metze
2435 /* Realloc the size of parameters and data we will return */
2436 param_len = 4;
2437 params = nttrans_realloc(ppparams, param_len);
2438 if(params == NULL) {
2439 reply_nterror(req, NT_STATUS_NO_MEMORY);
2440 return;
2443 pdata = nttrans_realloc(ppdata, data_len);
2444 if(pdata == NULL) {
2445 reply_nterror(req, NT_STATUS_NO_MEMORY);
2446 return;
2449 entry = pdata;
2451 /* set params Size of returned Quota Data 4 bytes*/
2452 SIVAL(params,0,data_len);
2454 /* nextoffset entry 4 bytes */
2455 SIVAL(entry,0,0);
2457 /* then the len of the SID 4 bytes */
2458 SIVAL(entry,4,sid_len);
2460 /* unknown data 8 bytes uint64_t */
2461 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2463 /* the used disk space 8 bytes uint64_t */
2464 SBIG_UINT(entry,16,qt.usedspace);
2466 /* the soft quotas 8 bytes uint64_t */
2467 SBIG_UINT(entry,24,qt.softlim);
2469 /* the hard quotas 8 bytes uint64_t */
2470 SBIG_UINT(entry,32,qt.hardlim);
2472 /* and now the SID */
2473 sid_linearize(entry+40, sid_len, &sid);
2475 break;
2477 default:
2478 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2479 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2480 return;
2481 break;
2484 send_nt_replies(conn, req, nt_status, params, param_len,
2485 pdata, data_len);
2488 /****************************************************************************
2489 Reply to set user quota
2490 ****************************************************************************/
2492 static void call_nt_transact_set_user_quota(connection_struct *conn,
2493 struct smb_request *req,
2494 uint16 **ppsetup,
2495 uint32 setup_count,
2496 char **ppparams,
2497 uint32 parameter_count,
2498 char **ppdata,
2499 uint32 data_count,
2500 uint32 max_data_count)
2502 char *params = *ppparams;
2503 char *pdata = *ppdata;
2504 int data_len=0,param_len=0;
2505 SMB_NTQUOTA_STRUCT qt;
2506 size_t sid_len;
2507 DOM_SID sid;
2508 files_struct *fsp = NULL;
2510 ZERO_STRUCT(qt);
2512 /* access check */
2513 if (conn->server_info->utok.uid != 0) {
2514 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2515 "[%s]\n", lp_servicename(SNUM(conn)),
2516 conn->server_info->unix_name));
2517 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2518 return;
2522 * Ensure minimum number of parameters sent.
2525 if (parameter_count < 2) {
2526 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2527 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2528 return;
2531 /* maybe we can check the quota_fnum */
2532 fsp = file_fsp(req, SVAL(params,0));
2533 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2534 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2535 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2536 return;
2539 if (data_count < 40) {
2540 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2541 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2542 return;
2545 /* offset to next quota record.
2546 * 4 bytes IVAL(pdata,0)
2547 * unused here...
2550 /* sid len */
2551 sid_len = IVAL(pdata,4);
2553 if (data_count < 40+sid_len) {
2554 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2555 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2556 return;
2559 /* unknown 8 bytes in pdata
2560 * maybe its the change time in NTTIME
2563 /* the used space 8 bytes (uint64_t)*/
2564 qt.usedspace = (uint64_t)IVAL(pdata,16);
2565 #ifdef LARGE_SMB_OFF_T
2566 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2567 #else /* LARGE_SMB_OFF_T */
2568 if ((IVAL(pdata,20) != 0)&&
2569 ((qt.usedspace != 0xFFFFFFFF)||
2570 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2571 /* more than 32 bits? */
2572 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2573 return;
2575 #endif /* LARGE_SMB_OFF_T */
2577 /* the soft quotas 8 bytes (uint64_t)*/
2578 qt.softlim = (uint64_t)IVAL(pdata,24);
2579 #ifdef LARGE_SMB_OFF_T
2580 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2581 #else /* LARGE_SMB_OFF_T */
2582 if ((IVAL(pdata,28) != 0)&&
2583 ((qt.softlim != 0xFFFFFFFF)||
2584 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2585 /* more than 32 bits? */
2586 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2587 return;
2589 #endif /* LARGE_SMB_OFF_T */
2591 /* the hard quotas 8 bytes (uint64_t)*/
2592 qt.hardlim = (uint64_t)IVAL(pdata,32);
2593 #ifdef LARGE_SMB_OFF_T
2594 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2595 #else /* LARGE_SMB_OFF_T */
2596 if ((IVAL(pdata,36) != 0)&&
2597 ((qt.hardlim != 0xFFFFFFFF)||
2598 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2599 /* more than 32 bits? */
2600 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2601 return;
2603 #endif /* LARGE_SMB_OFF_T */
2605 sid_parse(pdata+40,sid_len,&sid);
2606 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2608 /* 44 unknown bytes left... */
2610 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2611 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2612 return;
2615 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2616 pdata, data_len);
2618 #endif /* HAVE_SYS_QUOTAS */
2620 static void handle_nttrans(connection_struct *conn,
2621 struct trans_state *state,
2622 struct smb_request *req)
2624 if (get_Protocol() >= PROTOCOL_NT1) {
2625 req->flags2 |= 0x40; /* IS_LONG_NAME */
2626 SSVAL(req->inbuf,smb_flg2,req->flags2);
2630 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2632 /* Now we must call the relevant NT_TRANS function */
2633 switch(state->call) {
2634 case NT_TRANSACT_CREATE:
2636 START_PROFILE(NT_transact_create);
2637 call_nt_transact_create(
2638 conn, req,
2639 &state->setup, state->setup_count,
2640 &state->param, state->total_param,
2641 &state->data, state->total_data,
2642 state->max_data_return);
2643 END_PROFILE(NT_transact_create);
2644 break;
2647 case NT_TRANSACT_IOCTL:
2649 START_PROFILE(NT_transact_ioctl);
2650 call_nt_transact_ioctl(
2651 conn, req,
2652 &state->setup, state->setup_count,
2653 &state->param, state->total_param,
2654 &state->data, state->total_data,
2655 state->max_data_return);
2656 END_PROFILE(NT_transact_ioctl);
2657 break;
2660 case NT_TRANSACT_SET_SECURITY_DESC:
2662 START_PROFILE(NT_transact_set_security_desc);
2663 call_nt_transact_set_security_desc(
2664 conn, req,
2665 &state->setup, state->setup_count,
2666 &state->param, state->total_param,
2667 &state->data, state->total_data,
2668 state->max_data_return);
2669 END_PROFILE(NT_transact_set_security_desc);
2670 break;
2673 case NT_TRANSACT_NOTIFY_CHANGE:
2675 START_PROFILE(NT_transact_notify_change);
2676 call_nt_transact_notify_change(
2677 conn, req,
2678 &state->setup, state->setup_count,
2679 &state->param, state->total_param,
2680 &state->data, state->total_data,
2681 state->max_data_return,
2682 state->max_param_return);
2683 END_PROFILE(NT_transact_notify_change);
2684 break;
2687 case NT_TRANSACT_RENAME:
2689 START_PROFILE(NT_transact_rename);
2690 call_nt_transact_rename(
2691 conn, req,
2692 &state->setup, state->setup_count,
2693 &state->param, state->total_param,
2694 &state->data, state->total_data,
2695 state->max_data_return);
2696 END_PROFILE(NT_transact_rename);
2697 break;
2700 case NT_TRANSACT_QUERY_SECURITY_DESC:
2702 START_PROFILE(NT_transact_query_security_desc);
2703 call_nt_transact_query_security_desc(
2704 conn, req,
2705 &state->setup, state->setup_count,
2706 &state->param, state->total_param,
2707 &state->data, state->total_data,
2708 state->max_data_return);
2709 END_PROFILE(NT_transact_query_security_desc);
2710 break;
2713 #ifdef HAVE_SYS_QUOTAS
2714 case NT_TRANSACT_GET_USER_QUOTA:
2716 START_PROFILE(NT_transact_get_user_quota);
2717 call_nt_transact_get_user_quota(
2718 conn, req,
2719 &state->setup, state->setup_count,
2720 &state->param, state->total_param,
2721 &state->data, state->total_data,
2722 state->max_data_return);
2723 END_PROFILE(NT_transact_get_user_quota);
2724 break;
2727 case NT_TRANSACT_SET_USER_QUOTA:
2729 START_PROFILE(NT_transact_set_user_quota);
2730 call_nt_transact_set_user_quota(
2731 conn, req,
2732 &state->setup, state->setup_count,
2733 &state->param, state->total_param,
2734 &state->data, state->total_data,
2735 state->max_data_return);
2736 END_PROFILE(NT_transact_set_user_quota);
2737 break;
2739 #endif /* HAVE_SYS_QUOTAS */
2741 default:
2742 /* Error in request */
2743 DEBUG(0,("handle_nttrans: Unknown request %d in "
2744 "nttrans call\n", state->call));
2745 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2746 return;
2748 return;
2751 /****************************************************************************
2752 Reply to a SMBNTtrans.
2753 ****************************************************************************/
2755 void reply_nttrans(struct smb_request *req)
2757 connection_struct *conn = req->conn;
2758 uint32_t pscnt;
2759 uint32_t psoff;
2760 uint32_t dscnt;
2761 uint32_t dsoff;
2762 uint16 function_code;
2763 NTSTATUS result;
2764 struct trans_state *state;
2766 START_PROFILE(SMBnttrans);
2768 if (req->wct < 19) {
2769 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2770 END_PROFILE(SMBnttrans);
2771 return;
2774 pscnt = IVAL(req->vwv+9, 1);
2775 psoff = IVAL(req->vwv+11, 1);
2776 dscnt = IVAL(req->vwv+13, 1);
2777 dsoff = IVAL(req->vwv+15, 1);
2778 function_code = SVAL(req->vwv+18, 0);
2780 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2781 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2782 END_PROFILE(SMBnttrans);
2783 return;
2786 result = allow_new_trans(conn->pending_trans, req->mid);
2787 if (!NT_STATUS_IS_OK(result)) {
2788 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2789 reply_nterror(req, result);
2790 END_PROFILE(SMBnttrans);
2791 return;
2794 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2795 reply_nterror(req, NT_STATUS_NO_MEMORY);
2796 END_PROFILE(SMBnttrans);
2797 return;
2800 state->cmd = SMBnttrans;
2802 state->mid = req->mid;
2803 state->vuid = req->vuid;
2804 state->total_data = IVAL(req->vwv+3, 1);
2805 state->data = NULL;
2806 state->total_param = IVAL(req->vwv+1, 1);
2807 state->param = NULL;
2808 state->max_data_return = IVAL(req->vwv+7, 1);
2809 state->max_param_return = IVAL(req->vwv+5, 1);
2811 /* setup count is in *words* */
2812 state->setup_count = 2*CVAL(req->vwv+17, 1);
2813 state->setup = NULL;
2814 state->call = function_code;
2816 DEBUG(10, ("num_setup=%u, "
2817 "param_total=%u, this_param=%u, max_param=%u, "
2818 "data_total=%u, this_data=%u, max_data=%u, "
2819 "param_offset=%u, data_offset=%u\n",
2820 (unsigned)state->setup_count,
2821 (unsigned)state->total_param, (unsigned)pscnt,
2822 (unsigned)state->max_param_return,
2823 (unsigned)state->total_data, (unsigned)dscnt,
2824 (unsigned)state->max_data_return,
2825 (unsigned)psoff, (unsigned)dsoff));
2828 * All nttrans messages we handle have smb_wct == 19 +
2829 * state->setup_count. Ensure this is so as a sanity check.
2832 if(req->wct != 19 + (state->setup_count/2)) {
2833 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2834 req->wct, 19 + (state->setup_count/2)));
2835 goto bad_param;
2838 /* Don't allow more than 128mb for each value. */
2839 if ((state->total_data > (1024*1024*128)) ||
2840 (state->total_param > (1024*1024*128))) {
2841 reply_nterror(req, NT_STATUS_NO_MEMORY);
2842 END_PROFILE(SMBnttrans);
2843 return;
2846 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2847 goto bad_param;
2849 if (state->total_data) {
2851 if (trans_oob(state->total_data, 0, dscnt)
2852 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2853 goto bad_param;
2856 /* Can't use talloc here, the core routines do realloc on the
2857 * params and data. */
2858 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2859 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2860 "bytes !\n", (unsigned int)state->total_data));
2861 TALLOC_FREE(state);
2862 reply_nterror(req, NT_STATUS_NO_MEMORY);
2863 END_PROFILE(SMBnttrans);
2864 return;
2867 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2870 if (state->total_param) {
2872 if (trans_oob(state->total_param, 0, pscnt)
2873 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2874 goto bad_param;
2877 /* Can't use talloc here, the core routines do realloc on the
2878 * params and data. */
2879 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2880 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2881 "bytes !\n", (unsigned int)state->total_param));
2882 SAFE_FREE(state->data);
2883 TALLOC_FREE(state);
2884 reply_nterror(req, NT_STATUS_NO_MEMORY);
2885 END_PROFILE(SMBnttrans);
2886 return;
2889 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2892 state->received_data = dscnt;
2893 state->received_param = pscnt;
2895 if(state->setup_count > 0) {
2896 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2897 state->setup_count));
2900 * No overflow possible here, state->setup_count is an
2901 * unsigned int, being filled by a single byte from
2902 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2903 * below is not necessary, it's here to clarify things. The
2904 * validity of req->vwv and req->wct has been checked in
2905 * init_smb_request already.
2907 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2908 goto bad_param;
2911 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2912 if (state->setup == NULL) {
2913 DEBUG(0,("reply_nttrans : Out of memory\n"));
2914 SAFE_FREE(state->data);
2915 SAFE_FREE(state->param);
2916 TALLOC_FREE(state);
2917 reply_nterror(req, NT_STATUS_NO_MEMORY);
2918 END_PROFILE(SMBnttrans);
2919 return;
2922 memcpy(state->setup, req->vwv+19, state->setup_count);
2923 dump_data(10, (uint8 *)state->setup, state->setup_count);
2926 if ((state->received_data == state->total_data) &&
2927 (state->received_param == state->total_param)) {
2928 handle_nttrans(conn, state, req);
2929 SAFE_FREE(state->param);
2930 SAFE_FREE(state->data);
2931 TALLOC_FREE(state);
2932 END_PROFILE(SMBnttrans);
2933 return;
2936 DLIST_ADD(conn->pending_trans, state);
2938 /* We need to send an interim response then receive the rest
2939 of the parameter/data bytes */
2940 reply_outbuf(req, 0, 0);
2941 show_msg((char *)req->outbuf);
2942 END_PROFILE(SMBnttrans);
2943 return;
2945 bad_param:
2947 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2948 SAFE_FREE(state->data);
2949 SAFE_FREE(state->param);
2950 TALLOC_FREE(state);
2951 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2952 END_PROFILE(SMBnttrans);
2953 return;
2956 /****************************************************************************
2957 Reply to a SMBnttranss
2958 ****************************************************************************/
2960 void reply_nttranss(struct smb_request *req)
2962 connection_struct *conn = req->conn;
2963 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2964 struct trans_state *state;
2966 START_PROFILE(SMBnttranss);
2968 show_msg((char *)req->inbuf);
2970 if (req->wct < 18) {
2971 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2972 END_PROFILE(SMBnttranss);
2973 return;
2976 for (state = conn->pending_trans; state != NULL;
2977 state = state->next) {
2978 if (state->mid == req->mid) {
2979 break;
2983 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2984 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2985 END_PROFILE(SMBnttranss);
2986 return;
2989 /* Revise state->total_param and state->total_data in case they have
2990 changed downwards */
2991 if (IVAL(req->vwv+1, 1) < state->total_param) {
2992 state->total_param = IVAL(req->vwv+1, 1);
2994 if (IVAL(req->vwv+3, 1) < state->total_data) {
2995 state->total_data = IVAL(req->vwv+3, 1);
2998 pcnt = IVAL(req->vwv+5, 1);
2999 poff = IVAL(req->vwv+7, 1);
3000 pdisp = IVAL(req->vwv+9, 1);
3002 dcnt = IVAL(req->vwv+11, 1);
3003 doff = IVAL(req->vwv+13, 1);
3004 ddisp = IVAL(req->vwv+15, 1);
3006 state->received_param += pcnt;
3007 state->received_data += dcnt;
3009 if ((state->received_data > state->total_data) ||
3010 (state->received_param > state->total_param))
3011 goto bad_param;
3013 if (pcnt) {
3014 if (trans_oob(state->total_param, pdisp, pcnt)
3015 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3016 goto bad_param;
3018 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3021 if (dcnt) {
3022 if (trans_oob(state->total_data, ddisp, dcnt)
3023 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3024 goto bad_param;
3026 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3029 if ((state->received_param < state->total_param) ||
3030 (state->received_data < state->total_data)) {
3031 END_PROFILE(SMBnttranss);
3032 return;
3035 handle_nttrans(conn, state, req);
3037 DLIST_REMOVE(conn->pending_trans, state);
3038 SAFE_FREE(state->data);
3039 SAFE_FREE(state->param);
3040 TALLOC_FREE(state);
3041 END_PROFILE(SMBnttranss);
3042 return;
3044 bad_param:
3046 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3047 DLIST_REMOVE(conn->pending_trans, state);
3048 SAFE_FREE(state->data);
3049 SAFE_FREE(state->param);
3050 TALLOC_FREE(state);
3051 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3052 END_PROFILE(SMBnttranss);
3053 return;