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/>.
22 #include "smbd/globals.h"
24 extern const struct generic_mapping file_generic_mapping;
26 static char *nttrans_realloc(char **ptr, size_t size)
29 smb_panic("nttrans_realloc() called with NULL ptr");
32 *ptr = (char *)SMB_REALLOC(*ptr, size);
36 memset(*ptr,'\0',size);
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;
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
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,
75 show_msg((char *)req->outbuf);
76 if (!srv_send_smb(smbd_server_fd(),
79 IS_CONN_ENCRYPTED(conn),
81 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
83 TALLOC_FREE(req->outbuf);
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
107 + data_alignment_offset);
109 if (useable_space < 0) {
110 char *msg = talloc_asprintf(
112 "send_nt_replies failed sanity useable_space = %d!!!",
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);
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,
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);
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,
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,
243 /* Send the packet */
244 show_msg((char *)req->outbuf);
245 if (!srv_send_smb(smbd_server_fd(),
248 IS_CONN_ENCRYPTED(conn),
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;
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)
283 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
285 /* Strip \\ off the name. */
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,
295 reply_nterror(req, status);
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)
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);
319 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
323 nt_open_pipe(fname, conn, req, &pnum);
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);
343 reply_outbuf(req, 34, 0);
346 p = (char *)req->outbuf + smb_vwv2;
350 SIVAL(p,0,FILE_WAS_OPENED);
353 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
356 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
358 SSVAL(p,2, 0x5FF); /* ? */
361 if (flags & EXTENDED_RESPONSE_REQUIRED) {
363 SIVAL(p,0,FILE_GENERIC_ALL);
365 * For pipes W2K3 seems to return
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));
377 struct case_semantics_state {
378 connection_struct *conn;
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;
396 /****************************************************************************
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))) {
410 result->case_sensitive = conn->case_sensitive;
411 result->case_preserve = conn->case_preserve;
412 result->short_case_preserve = conn->short_case_preserve;
415 conn->case_sensitive = True;
416 conn->case_preserve = True;
417 conn->short_case_preserve = True;
419 talloc_set_destructor(result, restore_case_semantics);
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;
435 uint32 file_attributes;
437 uint32 create_disposition;
438 uint32 create_options;
440 uint64_t allocation_size;
441 /* Breakout the oplock request bits so we can set the
442 reply bits separately. */
444 SMB_OFF_T file_len = 0;
446 files_struct *fsp = 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;
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);
462 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
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);
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);
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",
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,
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.
511 if (lp_nt_pipe_support()) {
512 do_ntcreate_pipe_open(conn, req);
515 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
519 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
520 if (oplock_request) {
521 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
525 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
526 case_state = set_posix_case_semantics(ctx, conn);
528 reply_nterror(req, NT_STATUS_NO_MEMORY);
533 status = filename_convert(ctx,
535 req->flags2 & FLAGS2_DFS_PATHNAMES,
541 TALLOC_FREE(case_state);
543 if (!NT_STATUS_IS_OK(status)) {
544 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
546 NT_STATUS_PATH_NOT_COVERED,
550 reply_nterror(req, status);
555 * Bug #6898 - clients using Windows opens should
556 * never be able to set this attribute into the
559 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
561 status = SMB_VFS_CREATE_FILE(
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 */
579 if (!NT_STATUS_IS_OK(status)) {
580 if (open_was_deferred(req->mid)) {
581 /* We have re-scheduled this call, no error. */
584 reply_openerror(req, status);
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;
609 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
611 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
612 oplock_granted = LEVEL_II_OPLOCK_RETURN;
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);
628 reply_outbuf(req, 34, 0);
631 p = (char *)req->outbuf + smb_vwv2;
633 SCVAL(p, 0, oplock_granted);
636 SSVAL(p,0,fsp->fnum);
638 if ((create_disposition == FILE_SUPERSEDE)
639 && (info == FILE_WAS_OVERWRITTEN)) {
640 SIVAL(p,0,FILE_WAS_SUPERSEDED);
646 fattr = dos_mode(conn, smb_fname);
648 fattr = FILE_ATTRIBUTE_NORMAL;
651 /* Deal with other possible opens having a modified
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);
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. */
674 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
676 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
678 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
680 SIVAL(p,0,fattr); /* File Attributes. */
682 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
684 SOFF_T(p,0,file_len);
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);
708 SCVAL(p,0,fsp->is_directory ? 1 : 0);
710 if (flags & EXTENDED_RESPONSE_REQUIRED) {
713 if (fsp->is_directory ||
714 can_write_to_file(conn, smb_fname)) {
715 perms = FILE_GENERIC_ALL;
717 perms = FILE_GENERIC_READ|FILE_EXECUTE;
722 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
723 fsp->fnum, smb_fname_str_dbg(smb_fname)));
727 END_PROFILE(SMBntcreateX);
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)
742 char *params = *ppparams;
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);
760 flags = IVAL(params,0);
762 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
763 parameter_count-53, STR_TERMINATE,
765 if (!NT_STATUS_IS_OK(status)) {
766 reply_nterror(req, status);
770 nt_open_pipe(fname, conn, req, &pnum);
777 /* Realloc the size of parameters and data we will return */
778 if (flags & EXTENDED_RESPONSE_REQUIRED) {
779 /* Extended response is 32 more byyes. */
784 params = nttrans_realloc(ppparams, param_len);
786 reply_nterror(req, NT_STATUS_NO_MEMORY);
791 SCVAL(p,0,NO_OPLOCK_RETURN);
796 SIVAL(p,0,FILE_WAS_OPENED);
800 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
803 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
805 SSVAL(p,2, 0x5FF); /* ? */
808 if (flags & EXTENDED_RESPONSE_REQUIRED) {
810 SIVAL(p,0,FILE_GENERIC_ALL);
812 * For pipes W2K3 seems to return
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);
827 /****************************************************************************
828 Internal fn to set security descriptors.
829 ****************************************************************************/
831 NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
832 uint32_t security_info_sent)
834 SEC_DESC *psd = NULL;
837 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
841 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
843 if (!NT_STATUS_IS_OK(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);
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;
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);
891 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
892 if (next_offset == 0) {
895 offset += next_offset;
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;
914 char *params = *ppparams;
915 char *data = *ppdata;
916 /* Breakout the oplock request bits so we can set the reply bits separately. */
918 SMB_OFF_T file_len = 0;
920 files_struct *fsp = NULL;
924 uint32 file_attributes;
926 uint32 create_disposition;
927 uint32 create_options;
929 struct security_descriptor *sd = NULL;
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;
940 uint64_t allocation_size;
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.
953 if (lp_nt_pipe_support()) {
954 do_nt_transact_create_pipe(
956 ppsetup, setup_count,
957 ppparams, parameter_count,
961 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
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);
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);
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);
1007 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1010 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
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);
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);
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);
1038 /* We have already checked that ea_len <= data_count here. */
1039 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1041 if (ea_list == NULL) {
1042 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
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);
1055 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1056 case_state = set_posix_case_semantics(ctx, conn);
1058 reply_nterror(req, NT_STATUS_NO_MEMORY);
1063 status = filename_convert(ctx,
1065 req->flags2 & FLAGS2_DFS_PATHNAMES,
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);
1080 reply_nterror(req, status);
1084 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1085 if (oplock_request) {
1086 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1091 * Bug #6898 - clients using Windows opens should
1092 * never be able to set this attribute into the
1095 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1097 status = SMB_VFS_CREATE_FILE(
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 */
1111 ea_list, /* ea_list */
1115 if(!NT_STATUS_IS_OK(status)) {
1116 if (open_was_deferred(req->mid)) {
1117 /* We have re-scheduled this call, no error. */
1120 reply_openerror(req, status);
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;
1145 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1147 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1148 oplock_granted = LEVEL_II_OPLOCK_RETURN;
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. */
1162 params = nttrans_realloc(ppparams, param_len);
1163 if(params == NULL) {
1164 reply_nterror(req, NT_STATUS_NO_MEMORY);
1169 SCVAL(p, 0, oplock_granted);
1172 SSVAL(p,0,fsp->fnum);
1174 if ((create_disposition == FILE_SUPERSEDE)
1175 && (info == FILE_WAS_OVERWRITTEN)) {
1176 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1182 fattr = dos_mode(conn, smb_fname);
1184 fattr = FILE_ATTRIBUTE_NORMAL;
1187 /* Deal with other possible opens having a modified
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);
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. */
1210 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1212 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1214 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1216 SIVAL(p,0,fattr); /* File Attributes. */
1218 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1220 SOFF_T(p,0,file_len);
1222 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1223 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1224 size_t num_names = 0;
1225 unsigned int num_streams;
1226 struct stream_struct *streams = NULL;
1228 /* Do we have any EA's ? */
1229 status = get_ea_names_from_file(ctx, conn, fsp,
1230 smb_fname->base_name, NULL, &num_names);
1231 if (NT_STATUS_IS_OK(status) && num_names) {
1232 file_status &= ~NO_EAS;
1234 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
1235 &num_streams, &streams);
1236 /* There is always one stream, ::$DATA. */
1237 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1238 file_status &= ~NO_SUBSTREAMS;
1240 TALLOC_FREE(streams);
1241 SSVAL(p,2,file_status);
1244 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1246 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1249 if (fsp->is_directory ||
1250 can_write_to_file(conn, smb_fname)) {
1251 perms = FILE_GENERIC_ALL;
1253 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1258 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1259 smb_fname_str_dbg(smb_fname)));
1261 /* Send the required number of replies */
1262 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1267 /****************************************************************************
1268 Reply to a NT CANCEL request.
1269 conn POINTER CAN BE NULL HERE !
1270 ****************************************************************************/
1272 void reply_ntcancel(struct smb_request *req)
1275 * Go through and cancel any pending change notifies.
1278 START_PROFILE(SMBntcancel);
1279 srv_cancel_sign_response(smbd_server_conn);
1280 remove_pending_change_notify_requests_by_mid(req->mid);
1281 remove_pending_lock_requests_by_mid_smb1(req->mid);
1283 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1284 (unsigned long long)req->mid));
1286 END_PROFILE(SMBntcancel);
1290 /****************************************************************************
1292 ****************************************************************************/
1294 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1295 connection_struct *conn,
1296 struct smb_request *req,
1297 struct smb_filename *smb_fname_src,
1298 struct smb_filename *smb_fname_dst,
1301 files_struct *fsp1,*fsp2;
1305 NTSTATUS status = NT_STATUS_OK;
1308 if (!CAN_WRITE(conn)) {
1309 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1313 /* Source must already exist. */
1314 if (!VALID_STAT(smb_fname_src->st)) {
1315 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1319 /* Ensure attributes match. */
1320 fattr = dos_mode(conn, smb_fname_src);
1321 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1322 status = NT_STATUS_NO_SUCH_FILE;
1326 /* Disallow if dst file already exists. */
1327 if (VALID_STAT(smb_fname_dst->st)) {
1328 status = NT_STATUS_OBJECT_NAME_COLLISION;
1332 /* No links from a directory. */
1333 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1334 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1338 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1339 smb_fname_str_dbg(smb_fname_src),
1340 smb_fname_str_dbg(smb_fname_dst)));
1342 status = SMB_VFS_CREATE_FILE(
1345 0, /* root_dir_fid */
1346 smb_fname_src, /* fname */
1347 FILE_READ_DATA, /* access_mask */
1348 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1350 FILE_OPEN, /* create_disposition*/
1351 0, /* create_options */
1352 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1353 NO_OPLOCK, /* oplock_request */
1354 0, /* allocation_size */
1355 0, /* private_flags */
1361 if (!NT_STATUS_IS_OK(status)) {
1365 status = SMB_VFS_CREATE_FILE(
1368 0, /* root_dir_fid */
1369 smb_fname_dst, /* fname */
1370 FILE_WRITE_DATA, /* access_mask */
1371 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1373 FILE_CREATE, /* create_disposition*/
1374 0, /* create_options */
1375 fattr, /* file_attributes */
1376 NO_OPLOCK, /* oplock_request */
1377 0, /* allocation_size */
1378 0, /* private_flags */
1384 if (!NT_STATUS_IS_OK(status)) {
1385 close_file(NULL, fsp1, ERROR_CLOSE);
1389 if (smb_fname_src->st.st_ex_size) {
1390 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1394 * As we are opening fsp1 read-only we only expect
1395 * an error on close on fsp2 if we are out of space.
1396 * Thus we don't look at the error return from the
1399 close_file(NULL, fsp1, NORMAL_CLOSE);
1401 /* Ensure the modtime is set correctly on the destination file. */
1402 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1404 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1406 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1407 creates the file. This isn't the correct thing to do in the copy
1409 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1411 status = NT_STATUS_NO_MEMORY;
1414 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1415 TALLOC_FREE(parent);
1417 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1418 status = NT_STATUS_DISK_FULL;
1422 if (!NT_STATUS_IS_OK(status)) {
1423 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1424 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1425 smb_fname_str_dbg(smb_fname_dst)));
1431 /****************************************************************************
1432 Reply to a NT rename request.
1433 ****************************************************************************/
1435 void reply_ntrename(struct smb_request *req)
1437 connection_struct *conn = req->conn;
1438 struct smb_filename *smb_fname_old = NULL;
1439 struct smb_filename *smb_fname_new = NULL;
1440 char *oldname = NULL;
1441 char *newname = NULL;
1444 bool src_has_wcard = False;
1445 bool dest_has_wcard = False;
1447 uint32_t ucf_flags_src = 0;
1448 uint32_t ucf_flags_dst = 0;
1450 TALLOC_CTX *ctx = talloc_tos();
1452 START_PROFILE(SMBntrename);
1455 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1459 attrs = SVAL(req->vwv+0, 0);
1460 rename_type = SVAL(req->vwv+1, 0);
1462 p = (const char *)req->buf + 1;
1463 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1464 &status, &src_has_wcard);
1465 if (!NT_STATUS_IS_OK(status)) {
1466 reply_nterror(req, status);
1470 if (ms_has_wild(oldname)) {
1471 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1476 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1477 &status, &dest_has_wcard);
1478 if (!NT_STATUS_IS_OK(status)) {
1479 reply_nterror(req, status);
1483 /* The newname must begin with a ':' if the oldname contains a ':'. */
1484 if (strchr_m(oldname, ':') && (newname[0] != ':')) {
1485 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1490 * If this is a rename operation, allow wildcards and save the
1491 * destination's last component.
1493 if (rename_type == RENAME_FLAG_RENAME) {
1494 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1495 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1498 /* rename_internals() calls unix_convert(), so don't call it here. */
1499 status = filename_convert(ctx, conn,
1500 req->flags2 & FLAGS2_DFS_PATHNAMES,
1505 if (!NT_STATUS_IS_OK(status)) {
1506 if (NT_STATUS_EQUAL(status,
1507 NT_STATUS_PATH_NOT_COVERED)) {
1508 reply_botherror(req,
1509 NT_STATUS_PATH_NOT_COVERED,
1510 ERRSRV, ERRbadpath);
1513 reply_nterror(req, status);
1517 status = filename_convert(ctx, conn,
1518 req->flags2 & FLAGS2_DFS_PATHNAMES,
1523 if (!NT_STATUS_IS_OK(status)) {
1524 if (NT_STATUS_EQUAL(status,
1525 NT_STATUS_PATH_NOT_COVERED)) {
1526 reply_botherror(req,
1527 NT_STATUS_PATH_NOT_COVERED,
1528 ERRSRV, ERRbadpath);
1531 reply_nterror(req, status);
1535 DEBUG(3,("reply_ntrename: %s -> %s\n",
1536 smb_fname_str_dbg(smb_fname_old),
1537 smb_fname_str_dbg(smb_fname_new)));
1539 switch(rename_type) {
1540 case RENAME_FLAG_RENAME:
1541 status = rename_internals(ctx, conn, req,
1542 smb_fname_old, smb_fname_new,
1543 attrs, False, src_has_wcard,
1547 case RENAME_FLAG_HARD_LINK:
1548 if (src_has_wcard || dest_has_wcard) {
1550 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1552 status = hardlink_internals(ctx, conn,
1557 case RENAME_FLAG_COPY:
1558 if (src_has_wcard || dest_has_wcard) {
1560 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1562 status = copy_internals(ctx, conn, req,
1568 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1569 status = NT_STATUS_INVALID_PARAMETER;
1572 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1576 if (!NT_STATUS_IS_OK(status)) {
1577 if (open_was_deferred(req->mid)) {
1578 /* We have re-scheduled this call. */
1582 reply_nterror(req, status);
1586 reply_outbuf(req, 0, 0);
1588 END_PROFILE(SMBntrename);
1592 /****************************************************************************
1593 Reply to a notify change - queue the request and
1594 don't allow a directory to be opened.
1595 ****************************************************************************/
1597 static void smbd_smb1_notify_reply(struct smb_request *req,
1598 NTSTATUS error_code,
1599 uint8_t *buf, size_t len)
1601 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1604 static void call_nt_transact_notify_change(connection_struct *conn,
1605 struct smb_request *req,
1609 uint32 parameter_count,
1610 char **ppdata, uint32 data_count,
1611 uint32 max_data_count,
1612 uint32 max_param_count)
1614 uint16 *setup = *ppsetup;
1620 if(setup_count < 6) {
1621 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1625 fsp = file_fsp(req, SVAL(setup,4));
1626 filter = IVAL(setup, 0);
1627 recursive = (SVAL(setup, 6) != 0) ? True : False;
1629 DEBUG(3,("call_nt_transact_notify_change\n"));
1632 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1637 char *filter_string;
1639 if (!(filter_string = notify_filter_string(NULL, filter))) {
1640 reply_nterror(req,NT_STATUS_NO_MEMORY);
1644 DEBUG(3,("call_nt_transact_notify_change: notify change "
1645 "called on %s, filter = %s, recursive = %d\n",
1646 fsp_str_dbg(fsp), filter_string, recursive));
1648 TALLOC_FREE(filter_string);
1651 if((!fsp->is_directory) || (conn != fsp->conn)) {
1652 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1656 if (fsp->notify == NULL) {
1658 status = change_notify_create(fsp, filter, recursive);
1660 if (!NT_STATUS_IS_OK(status)) {
1661 DEBUG(10, ("change_notify_create returned %s\n",
1662 nt_errstr(status)));
1663 reply_nterror(req, status);
1668 if (fsp->notify->num_changes != 0) {
1671 * We've got changes pending, respond immediately
1675 * TODO: write a torture test to check the filtering behaviour
1679 change_notify_reply(fsp->conn, req,
1683 smbd_smb1_notify_reply);
1686 * change_notify_reply() above has independently sent its
1693 * No changes pending, queue the request
1696 status = change_notify_add_request(req,
1700 smbd_smb1_notify_reply);
1701 if (!NT_STATUS_IS_OK(status)) {
1702 reply_nterror(req, status);
1707 /****************************************************************************
1708 Reply to an NT transact rename command.
1709 ****************************************************************************/
1711 static void call_nt_transact_rename(connection_struct *conn,
1712 struct smb_request *req,
1713 uint16 **ppsetup, uint32 setup_count,
1714 char **ppparams, uint32 parameter_count,
1715 char **ppdata, uint32 data_count,
1716 uint32 max_data_count)
1718 char *params = *ppparams;
1719 char *new_name = NULL;
1720 files_struct *fsp = NULL;
1721 bool dest_has_wcard = False;
1723 TALLOC_CTX *ctx = talloc_tos();
1725 if(parameter_count < 5) {
1726 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1730 fsp = file_fsp(req, SVAL(params, 0));
1731 if (!check_fsp(conn, req, fsp)) {
1734 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1735 parameter_count - 4,
1736 STR_TERMINATE, &status, &dest_has_wcard);
1737 if (!NT_STATUS_IS_OK(status)) {
1738 reply_nterror(req, status);
1743 * W2K3 ignores this request as the RAW-RENAME test
1744 * demonstrates, so we do.
1746 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1748 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1749 fsp_str_dbg(fsp), new_name));
1754 /******************************************************************************
1755 Fake up a completely empty SD.
1756 *******************************************************************************/
1758 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1762 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1764 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1765 return NT_STATUS_NO_MEMORY;
1768 return NT_STATUS_OK;
1771 /****************************************************************************
1772 Reply to query a security descriptor.
1773 Callable from SMB2 and SMB2.
1774 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1776 ****************************************************************************/
1778 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1779 TALLOC_CTX *mem_ctx,
1781 uint32_t security_info_wanted,
1782 uint32_t max_data_count,
1783 uint8_t **ppmarshalled_sd,
1787 SEC_DESC *psd = NULL;
1790 * Get the permissions to return.
1793 if (!lp_nt_acl_support(SNUM(conn))) {
1794 status = get_null_nt_acl(mem_ctx, &psd);
1796 status = SMB_VFS_FGET_NT_ACL(
1797 fsp, security_info_wanted, &psd);
1799 if (!NT_STATUS_IS_OK(status)) {
1803 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1804 * present in the reply to match Windows behavior */
1805 if (psd->sacl == NULL &&
1806 security_info_wanted & SACL_SECURITY_INFORMATION)
1807 psd->type |= SEC_DESC_SACL_PRESENT;
1808 if (psd->dacl == NULL &&
1809 security_info_wanted & DACL_SECURITY_INFORMATION)
1810 psd->type |= SEC_DESC_DACL_PRESENT;
1812 *psd_size = ndr_size_security_descriptor(psd, NULL, 0);
1814 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1815 (unsigned long)*psd_size));
1817 if (DEBUGLEVEL >= 10) {
1818 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1820 NDR_PRINT_DEBUG(security_descriptor, psd);
1823 if (max_data_count < *psd_size) {
1825 return NT_STATUS_BUFFER_TOO_SMALL;
1828 status = marshall_sec_desc(mem_ctx, psd,
1829 ppmarshalled_sd, psd_size);
1831 if (!NT_STATUS_IS_OK(status)) {
1837 return NT_STATUS_OK;
1840 /****************************************************************************
1841 SMB1 reply to query a security descriptor.
1842 ****************************************************************************/
1844 static void call_nt_transact_query_security_desc(connection_struct *conn,
1845 struct smb_request *req,
1849 uint32 parameter_count,
1852 uint32 max_data_count)
1854 char *params = *ppparams;
1855 char *data = *ppdata;
1857 uint32 security_info_wanted;
1858 files_struct *fsp = NULL;
1860 uint8_t *marshalled_sd = NULL;
1862 if(parameter_count < 8) {
1863 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1867 fsp = file_fsp(req, SVAL(params,0));
1869 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1873 security_info_wanted = IVAL(params,4);
1875 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1876 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1877 (unsigned int)security_info_wanted));
1879 params = nttrans_realloc(ppparams, 4);
1880 if(params == NULL) {
1881 reply_nterror(req, NT_STATUS_NO_MEMORY);
1886 * Get the permissions to return.
1889 status = smbd_do_query_security_desc(conn,
1892 security_info_wanted,
1897 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
1898 SIVAL(params,0,(uint32_t)sd_size);
1899 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1900 params, 4, NULL, 0);
1904 if (!NT_STATUS_IS_OK(status)) {
1905 reply_nterror(req, status);
1909 SMB_ASSERT(sd_size > 0);
1911 SIVAL(params,0,(uint32_t)sd_size);
1913 if (max_data_count < sd_size) {
1914 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1915 params, 4, NULL, 0);
1920 * Allocate the data we will return.
1923 data = nttrans_realloc(ppdata, sd_size);
1925 reply_nterror(req, NT_STATUS_NO_MEMORY);
1929 memcpy(data, marshalled_sd, sd_size);
1931 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1936 /****************************************************************************
1937 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1938 ****************************************************************************/
1940 static void call_nt_transact_set_security_desc(connection_struct *conn,
1941 struct smb_request *req,
1945 uint32 parameter_count,
1948 uint32 max_data_count)
1950 char *params= *ppparams;
1951 char *data = *ppdata;
1952 files_struct *fsp = NULL;
1953 uint32 security_info_sent = 0;
1956 if(parameter_count < 8) {
1957 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1961 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1962 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1966 if(!lp_nt_acl_support(SNUM(conn))) {
1970 security_info_sent = IVAL(params,4);
1972 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
1973 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
1975 if (data_count == 0) {
1976 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1980 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1982 if (!NT_STATUS_IS_OK(status)) {
1983 reply_nterror(req, status);
1988 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1992 /****************************************************************************
1994 ****************************************************************************/
1996 static void call_nt_transact_ioctl(connection_struct *conn,
1997 struct smb_request *req,
1998 uint16 **ppsetup, uint32 setup_count,
1999 char **ppparams, uint32 parameter_count,
2000 char **ppdata, uint32 data_count,
2001 uint32 max_data_count)
2008 char *pdata = *ppdata;
2010 if (setup_count != 8) {
2011 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2012 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2016 function = IVAL(*ppsetup, 0);
2017 fidnum = SVAL(*ppsetup, 4);
2018 isFSctl = CVAL(*ppsetup, 6);
2019 compfilter = CVAL(*ppsetup, 7);
2021 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2022 function, fidnum, isFSctl, compfilter));
2024 fsp=file_fsp(req, fidnum);
2025 /* this check is done in each implemented function case for now
2026 because I don't want to break anything... --metze
2027 FSP_BELONGS_CONN(fsp,conn);*/
2029 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2032 case FSCTL_SET_SPARSE:
2033 /* pretend this succeeded - tho strictly we should
2034 mark the file sparse (if the local fs supports it)
2035 so we can know if we need to pre-allocate or not */
2037 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2038 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2041 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2043 unsigned char objid[16];
2045 /* This should return the object-id on this file.
2046 * I think I'll make this be the inode+dev. JRA.
2049 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2051 if (!fsp_belongs_conn(conn, req, fsp)) {
2056 pdata = nttrans_realloc(ppdata, data_count);
2057 if (pdata == NULL) {
2058 reply_nterror(req, NT_STATUS_NO_MEMORY);
2062 /* For backwards compatibility only store the dev/inode. */
2063 push_file_id_16(pdata, &fsp->file_id);
2064 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2065 push_file_id_16(pdata+32, &fsp->file_id);
2066 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2071 case FSCTL_GET_REPARSE_POINT:
2072 /* pretend this fail - my winXP does it like this
2076 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2077 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2080 case FSCTL_SET_REPARSE_POINT:
2081 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2085 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2086 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2089 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2092 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2093 * and return their volume names. If max_data_count is 16, then it is just
2094 * asking for the number of volumes and length of the combined names.
2096 * pdata is the data allocated by our caller, but that uses
2097 * total_data_count (which is 0 in our case) rather than max_data_count.
2098 * Allocate the correct amount and return the pointer to let
2099 * it be deallocated when we return.
2101 SHADOW_COPY_DATA *shadow_data = NULL;
2102 TALLOC_CTX *shadow_mem_ctx = NULL;
2103 bool labels = False;
2104 uint32 labels_data_count = 0;
2108 if (!fsp_belongs_conn(conn, req, fsp)) {
2112 if (max_data_count < 16) {
2113 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2115 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2119 if (max_data_count > 16) {
2123 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2124 if (shadow_mem_ctx == NULL) {
2125 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2126 reply_nterror(req, NT_STATUS_NO_MEMORY);
2130 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2131 if (shadow_data == NULL) {
2132 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2133 talloc_destroy(shadow_mem_ctx);
2134 reply_nterror(req, NT_STATUS_NO_MEMORY);
2138 shadow_data->mem_ctx = shadow_mem_ctx;
2141 * Call the VFS routine to actually do the work.
2143 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2144 talloc_destroy(shadow_data->mem_ctx);
2145 if (errno == ENOSYS) {
2146 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2147 conn->connectpath));
2148 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2151 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2152 conn->connectpath));
2153 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2158 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2163 data_count = 12+labels_data_count+4;
2166 if (max_data_count<data_count) {
2167 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2168 max_data_count,data_count));
2169 talloc_destroy(shadow_data->mem_ctx);
2170 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2174 pdata = nttrans_realloc(ppdata, data_count);
2175 if (pdata == NULL) {
2176 talloc_destroy(shadow_data->mem_ctx);
2177 reply_nterror(req, NT_STATUS_NO_MEMORY);
2183 /* num_volumes 4 bytes */
2184 SIVAL(pdata,0,shadow_data->num_volumes);
2187 /* num_labels 4 bytes */
2188 SIVAL(pdata,4,shadow_data->num_volumes);
2191 /* needed_data_count 4 bytes */
2192 SIVAL(pdata, 8, labels_data_count+4);
2196 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2197 shadow_data->num_volumes, fsp_str_dbg(fsp)));
2198 if (labels && shadow_data->labels) {
2199 for (i=0;i<shadow_data->num_volumes;i++) {
2200 srvstr_push(pdata, req->flags2,
2201 cur_pdata, shadow_data->labels[i],
2202 2*sizeof(SHADOW_COPY_LABEL),
2203 STR_UNICODE|STR_TERMINATE);
2204 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2205 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2209 talloc_destroy(shadow_data->mem_ctx);
2211 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2217 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2219 /* pretend this succeeded -
2221 * we have to send back a list with all files owned by this SID
2223 * but I have to check that --metze
2227 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2229 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2231 if (!fsp_belongs_conn(conn, req, fsp)) {
2235 /* unknown 4 bytes: this is not the length of the sid :-( */
2236 /*unknown = IVAL(pdata,0);*/
2238 sid_parse(pdata+4,sid_len,&sid);
2239 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2241 if (!sid_to_uid(&sid, &uid)) {
2242 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2243 sid_string_dbg(&sid),
2244 (unsigned long)sid_len));
2248 /* we can take a look at the find source :-)
2250 * find ./ -uid $uid -name '*' is what we need here
2253 * and send 4bytes len and then NULL terminated unicode strings
2256 * but I don't know how to deal with the paged results
2257 * (maybe we can hang the result anywhere in the fsp struct)
2259 * we don't send all files at once
2260 * and at the next we should *not* start from the beginning,
2261 * so we have to cache the result
2266 /* this works for now... */
2267 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2270 case FSCTL_QUERY_ALLOCATED_RANGES:
2272 /* FIXME: This is just a dummy reply, telling that all of the
2273 * file is allocated. MKS cp needs that.
2274 * Adding the real allocated ranges via FIEMAP on Linux
2275 * and SEEK_DATA/SEEK_HOLE on Solaris is needed to make
2276 * this FSCTL correct for sparse files.
2279 uint64_t offset, length;
2281 if (!fsp_belongs_conn(conn, req, fsp)) {
2285 if (data_count != 16) {
2286 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: data_count(%u) != 16 is invalid!\n",
2288 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2292 if (max_data_count < 16) {
2293 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: max_data_count(%u) < 16 is invalid!\n",
2295 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2299 offset = BVAL(pdata,0);
2300 length = BVAL(pdata,8);
2302 if (offset + length < offset) {
2303 /* No 64-bit integer wrap. */
2304 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2308 status = vfs_stat_fsp(fsp);
2309 if (!NT_STATUS_IS_OK(status)) {
2310 reply_nterror(req, status);
2314 if (offset > fsp->fsp_name->st.st_ex_size ||
2315 fsp->fsp_name->st.st_ex_size == 0 ||
2317 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2319 uint64_t end = offset + length;
2320 end = MIN(end, fsp->fsp_name->st.st_ex_size);
2323 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2329 if (!logged_ioctl_message) {
2330 logged_ioctl_message = true; /* Only print this once... */
2331 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2336 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2340 #ifdef HAVE_SYS_QUOTAS
2341 /****************************************************************************
2342 Reply to get user quota
2343 ****************************************************************************/
2345 static void call_nt_transact_get_user_quota(connection_struct *conn,
2346 struct smb_request *req,
2350 uint32 parameter_count,
2353 uint32 max_data_count)
2355 NTSTATUS nt_status = NT_STATUS_OK;
2356 char *params = *ppparams;
2357 char *pdata = *ppdata;
2359 int data_len=0,param_len=0;
2362 files_struct *fsp = NULL;
2366 bool start_enum = True;
2367 SMB_NTQUOTA_STRUCT qt;
2368 SMB_NTQUOTA_LIST *tmp_list;
2369 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2374 if (conn->server_info->utok.uid != 0) {
2375 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2376 "[%s]\n", lp_servicename(SNUM(conn)),
2377 conn->server_info->unix_name));
2378 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2383 * Ensure minimum number of parameters sent.
2386 if (parameter_count < 4) {
2387 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2388 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2392 /* maybe we can check the quota_fnum */
2393 fsp = file_fsp(req, SVAL(params,0));
2394 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2395 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2396 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2400 /* the NULL pointer checking for fsp->fake_file_handle->pd
2401 * is done by CHECK_NTQUOTA_HANDLE_OK()
2403 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2405 level = SVAL(params,2);
2407 /* unknown 12 bytes leading in params */
2410 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2411 /* seems that we should continue with the enum here --metze */
2413 if (qt_handle->quota_list!=NULL &&
2414 qt_handle->tmp_list==NULL) {
2417 free_ntquota_list(&(qt_handle->quota_list));
2419 /* Realloc the size of parameters and data we will return */
2421 params = nttrans_realloc(ppparams, param_len);
2422 if(params == NULL) {
2423 reply_nterror(req, NT_STATUS_NO_MEMORY);
2428 SIVAL(params,0,data_len);
2435 case TRANSACT_GET_USER_QUOTA_LIST_START:
2437 if (qt_handle->quota_list==NULL &&
2438 qt_handle->tmp_list==NULL) {
2442 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2443 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2447 /* Realloc the size of parameters and data we will return */
2449 params = nttrans_realloc(ppparams, param_len);
2450 if(params == NULL) {
2451 reply_nterror(req, NT_STATUS_NO_MEMORY);
2455 /* we should not trust the value in max_data_count*/
2456 max_data_count = MIN(max_data_count,2048);
2458 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2460 reply_nterror(req, NT_STATUS_NO_MEMORY);
2466 /* set params Size of returned Quota Data 4 bytes*/
2467 /* but set it later when we know it */
2469 /* for each entry push the data */
2472 qt_handle->tmp_list = qt_handle->quota_list;
2475 tmp_list = qt_handle->tmp_list;
2477 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2478 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2480 sid_len = ndr_size_dom_sid(
2481 &tmp_list->quotas->sid, NULL, 0);
2482 entry_len = 40 + sid_len;
2484 /* nextoffset entry 4 bytes */
2485 SIVAL(entry,0,entry_len);
2487 /* then the len of the SID 4 bytes */
2488 SIVAL(entry,4,sid_len);
2490 /* unknown data 8 bytes uint64_t */
2491 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2493 /* the used disk space 8 bytes uint64_t */
2494 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2496 /* the soft quotas 8 bytes uint64_t */
2497 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2499 /* the hard quotas 8 bytes uint64_t */
2500 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2502 /* and now the SID */
2503 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2506 qt_handle->tmp_list = tmp_list;
2508 /* overwrite the offset of the last entry */
2509 SIVAL(entry-entry_len,0,0);
2511 data_len = 4+qt_len;
2512 /* overwrite the params quota_data_len */
2513 SIVAL(params,0,data_len);
2517 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2519 /* unknown 4 bytes IVAL(pdata,0) */
2521 if (data_count < 8) {
2522 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2523 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2527 sid_len = IVAL(pdata,4);
2528 /* Ensure this is less than 1mb. */
2529 if (sid_len > (1024*1024)) {
2530 reply_nterror(req, NT_STATUS_NO_MEMORY);
2534 if (data_count < 8+sid_len) {
2535 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2536 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2540 data_len = 4+40+sid_len;
2542 if (max_data_count < data_len) {
2543 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2544 max_data_count, data_len));
2546 SIVAL(params,0,data_len);
2548 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2552 sid_parse(pdata+8,sid_len,&sid);
2554 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2557 * we have to return zero's in all fields
2558 * instead of returning an error here
2563 /* Realloc the size of parameters and data we will return */
2565 params = nttrans_realloc(ppparams, param_len);
2566 if(params == NULL) {
2567 reply_nterror(req, NT_STATUS_NO_MEMORY);
2571 pdata = nttrans_realloc(ppdata, data_len);
2573 reply_nterror(req, NT_STATUS_NO_MEMORY);
2579 /* set params Size of returned Quota Data 4 bytes*/
2580 SIVAL(params,0,data_len);
2582 /* nextoffset entry 4 bytes */
2585 /* then the len of the SID 4 bytes */
2586 SIVAL(entry,4,sid_len);
2588 /* unknown data 8 bytes uint64_t */
2589 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2591 /* the used disk space 8 bytes uint64_t */
2592 SBIG_UINT(entry,16,qt.usedspace);
2594 /* the soft quotas 8 bytes uint64_t */
2595 SBIG_UINT(entry,24,qt.softlim);
2597 /* the hard quotas 8 bytes uint64_t */
2598 SBIG_UINT(entry,32,qt.hardlim);
2600 /* and now the SID */
2601 sid_linearize(entry+40, sid_len, &sid);
2606 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2607 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2612 send_nt_replies(conn, req, nt_status, params, param_len,
2616 /****************************************************************************
2617 Reply to set user quota
2618 ****************************************************************************/
2620 static void call_nt_transact_set_user_quota(connection_struct *conn,
2621 struct smb_request *req,
2625 uint32 parameter_count,
2628 uint32 max_data_count)
2630 char *params = *ppparams;
2631 char *pdata = *ppdata;
2632 int data_len=0,param_len=0;
2633 SMB_NTQUOTA_STRUCT qt;
2636 files_struct *fsp = NULL;
2641 if (conn->server_info->utok.uid != 0) {
2642 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2643 "[%s]\n", lp_servicename(SNUM(conn)),
2644 conn->server_info->unix_name));
2645 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2650 * Ensure minimum number of parameters sent.
2653 if (parameter_count < 2) {
2654 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2655 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2659 /* maybe we can check the quota_fnum */
2660 fsp = file_fsp(req, SVAL(params,0));
2661 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2662 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2663 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2667 if (data_count < 40) {
2668 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2669 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2673 /* offset to next quota record.
2674 * 4 bytes IVAL(pdata,0)
2679 sid_len = IVAL(pdata,4);
2681 if (data_count < 40+sid_len) {
2682 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2683 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2687 /* unknown 8 bytes in pdata
2688 * maybe its the change time in NTTIME
2691 /* the used space 8 bytes (uint64_t)*/
2692 qt.usedspace = (uint64_t)IVAL(pdata,16);
2693 #ifdef LARGE_SMB_OFF_T
2694 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2695 #else /* LARGE_SMB_OFF_T */
2696 if ((IVAL(pdata,20) != 0)&&
2697 ((qt.usedspace != 0xFFFFFFFF)||
2698 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2699 /* more than 32 bits? */
2700 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2703 #endif /* LARGE_SMB_OFF_T */
2705 /* the soft quotas 8 bytes (uint64_t)*/
2706 qt.softlim = (uint64_t)IVAL(pdata,24);
2707 #ifdef LARGE_SMB_OFF_T
2708 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2709 #else /* LARGE_SMB_OFF_T */
2710 if ((IVAL(pdata,28) != 0)&&
2711 ((qt.softlim != 0xFFFFFFFF)||
2712 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2713 /* more than 32 bits? */
2714 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2717 #endif /* LARGE_SMB_OFF_T */
2719 /* the hard quotas 8 bytes (uint64_t)*/
2720 qt.hardlim = (uint64_t)IVAL(pdata,32);
2721 #ifdef LARGE_SMB_OFF_T
2722 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2723 #else /* LARGE_SMB_OFF_T */
2724 if ((IVAL(pdata,36) != 0)&&
2725 ((qt.hardlim != 0xFFFFFFFF)||
2726 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2727 /* more than 32 bits? */
2728 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2731 #endif /* LARGE_SMB_OFF_T */
2733 sid_parse(pdata+40,sid_len,&sid);
2734 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2736 /* 44 unknown bytes left... */
2738 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2739 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2743 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2746 #endif /* HAVE_SYS_QUOTAS */
2748 static void handle_nttrans(connection_struct *conn,
2749 struct trans_state *state,
2750 struct smb_request *req)
2752 if (get_Protocol() >= PROTOCOL_NT1) {
2753 req->flags2 |= 0x40; /* IS_LONG_NAME */
2754 SSVAL(req->inbuf,smb_flg2,req->flags2);
2758 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2760 /* Now we must call the relevant NT_TRANS function */
2761 switch(state->call) {
2762 case NT_TRANSACT_CREATE:
2764 START_PROFILE(NT_transact_create);
2765 call_nt_transact_create(
2767 &state->setup, state->setup_count,
2768 &state->param, state->total_param,
2769 &state->data, state->total_data,
2770 state->max_data_return);
2771 END_PROFILE(NT_transact_create);
2775 case NT_TRANSACT_IOCTL:
2777 START_PROFILE(NT_transact_ioctl);
2778 call_nt_transact_ioctl(
2780 &state->setup, state->setup_count,
2781 &state->param, state->total_param,
2782 &state->data, state->total_data,
2783 state->max_data_return);
2784 END_PROFILE(NT_transact_ioctl);
2788 case NT_TRANSACT_SET_SECURITY_DESC:
2790 START_PROFILE(NT_transact_set_security_desc);
2791 call_nt_transact_set_security_desc(
2793 &state->setup, state->setup_count,
2794 &state->param, state->total_param,
2795 &state->data, state->total_data,
2796 state->max_data_return);
2797 END_PROFILE(NT_transact_set_security_desc);
2801 case NT_TRANSACT_NOTIFY_CHANGE:
2803 START_PROFILE(NT_transact_notify_change);
2804 call_nt_transact_notify_change(
2806 &state->setup, state->setup_count,
2807 &state->param, state->total_param,
2808 &state->data, state->total_data,
2809 state->max_data_return,
2810 state->max_param_return);
2811 END_PROFILE(NT_transact_notify_change);
2815 case NT_TRANSACT_RENAME:
2817 START_PROFILE(NT_transact_rename);
2818 call_nt_transact_rename(
2820 &state->setup, state->setup_count,
2821 &state->param, state->total_param,
2822 &state->data, state->total_data,
2823 state->max_data_return);
2824 END_PROFILE(NT_transact_rename);
2828 case NT_TRANSACT_QUERY_SECURITY_DESC:
2830 START_PROFILE(NT_transact_query_security_desc);
2831 call_nt_transact_query_security_desc(
2833 &state->setup, state->setup_count,
2834 &state->param, state->total_param,
2835 &state->data, state->total_data,
2836 state->max_data_return);
2837 END_PROFILE(NT_transact_query_security_desc);
2841 #ifdef HAVE_SYS_QUOTAS
2842 case NT_TRANSACT_GET_USER_QUOTA:
2844 START_PROFILE(NT_transact_get_user_quota);
2845 call_nt_transact_get_user_quota(
2847 &state->setup, state->setup_count,
2848 &state->param, state->total_param,
2849 &state->data, state->total_data,
2850 state->max_data_return);
2851 END_PROFILE(NT_transact_get_user_quota);
2855 case NT_TRANSACT_SET_USER_QUOTA:
2857 START_PROFILE(NT_transact_set_user_quota);
2858 call_nt_transact_set_user_quota(
2860 &state->setup, state->setup_count,
2861 &state->param, state->total_param,
2862 &state->data, state->total_data,
2863 state->max_data_return);
2864 END_PROFILE(NT_transact_set_user_quota);
2867 #endif /* HAVE_SYS_QUOTAS */
2870 /* Error in request */
2871 DEBUG(0,("handle_nttrans: Unknown request %d in "
2872 "nttrans call\n", state->call));
2873 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2879 /****************************************************************************
2880 Reply to a SMBNTtrans.
2881 ****************************************************************************/
2883 void reply_nttrans(struct smb_request *req)
2885 connection_struct *conn = req->conn;
2890 uint16 function_code;
2892 struct trans_state *state;
2894 START_PROFILE(SMBnttrans);
2896 if (req->wct < 19) {
2897 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2898 END_PROFILE(SMBnttrans);
2902 pscnt = IVAL(req->vwv+9, 1);
2903 psoff = IVAL(req->vwv+11, 1);
2904 dscnt = IVAL(req->vwv+13, 1);
2905 dsoff = IVAL(req->vwv+15, 1);
2906 function_code = SVAL(req->vwv+18, 0);
2908 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2909 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2910 END_PROFILE(SMBnttrans);
2914 result = allow_new_trans(conn->pending_trans, req->mid);
2915 if (!NT_STATUS_IS_OK(result)) {
2916 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2917 reply_nterror(req, result);
2918 END_PROFILE(SMBnttrans);
2922 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2923 reply_nterror(req, NT_STATUS_NO_MEMORY);
2924 END_PROFILE(SMBnttrans);
2928 state->cmd = SMBnttrans;
2930 state->mid = req->mid;
2931 state->vuid = req->vuid;
2932 state->total_data = IVAL(req->vwv+3, 1);
2934 state->total_param = IVAL(req->vwv+1, 1);
2935 state->param = NULL;
2936 state->max_data_return = IVAL(req->vwv+7, 1);
2937 state->max_param_return = IVAL(req->vwv+5, 1);
2939 /* setup count is in *words* */
2940 state->setup_count = 2*CVAL(req->vwv+17, 1);
2941 state->setup = NULL;
2942 state->call = function_code;
2944 DEBUG(10, ("num_setup=%u, "
2945 "param_total=%u, this_param=%u, max_param=%u, "
2946 "data_total=%u, this_data=%u, max_data=%u, "
2947 "param_offset=%u, data_offset=%u\n",
2948 (unsigned)state->setup_count,
2949 (unsigned)state->total_param, (unsigned)pscnt,
2950 (unsigned)state->max_param_return,
2951 (unsigned)state->total_data, (unsigned)dscnt,
2952 (unsigned)state->max_data_return,
2953 (unsigned)psoff, (unsigned)dsoff));
2956 * All nttrans messages we handle have smb_wct == 19 +
2957 * state->setup_count. Ensure this is so as a sanity check.
2960 if(req->wct != 19 + (state->setup_count/2)) {
2961 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2962 req->wct, 19 + (state->setup_count/2)));
2966 /* Don't allow more than 128mb for each value. */
2967 if ((state->total_data > (1024*1024*128)) ||
2968 (state->total_param > (1024*1024*128))) {
2969 reply_nterror(req, NT_STATUS_NO_MEMORY);
2970 END_PROFILE(SMBnttrans);
2974 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2977 if (state->total_data) {
2979 if (trans_oob(state->total_data, 0, dscnt)
2980 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2984 /* Can't use talloc here, the core routines do realloc on the
2985 * params and data. */
2986 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2987 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2988 "bytes !\n", (unsigned int)state->total_data));
2990 reply_nterror(req, NT_STATUS_NO_MEMORY);
2991 END_PROFILE(SMBnttrans);
2995 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2998 if (state->total_param) {
3000 if (trans_oob(state->total_param, 0, pscnt)
3001 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
3005 /* Can't use talloc here, the core routines do realloc on the
3006 * params and data. */
3007 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3008 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3009 "bytes !\n", (unsigned int)state->total_param));
3010 SAFE_FREE(state->data);
3012 reply_nterror(req, NT_STATUS_NO_MEMORY);
3013 END_PROFILE(SMBnttrans);
3017 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3020 state->received_data = dscnt;
3021 state->received_param = pscnt;
3023 if(state->setup_count > 0) {
3024 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3025 state->setup_count));
3028 * No overflow possible here, state->setup_count is an
3029 * unsigned int, being filled by a single byte from
3030 * CVAL(req->vwv+13, 0) above. The cast in the comparison
3031 * below is not necessary, it's here to clarify things. The
3032 * validity of req->vwv and req->wct has been checked in
3033 * init_smb_request already.
3035 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
3039 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3040 if (state->setup == NULL) {
3041 DEBUG(0,("reply_nttrans : Out of memory\n"));
3042 SAFE_FREE(state->data);
3043 SAFE_FREE(state->param);
3045 reply_nterror(req, NT_STATUS_NO_MEMORY);
3046 END_PROFILE(SMBnttrans);
3050 memcpy(state->setup, req->vwv+19, state->setup_count);
3051 dump_data(10, (uint8 *)state->setup, state->setup_count);
3054 if ((state->received_data == state->total_data) &&
3055 (state->received_param == state->total_param)) {
3056 handle_nttrans(conn, state, req);
3057 SAFE_FREE(state->param);
3058 SAFE_FREE(state->data);
3060 END_PROFILE(SMBnttrans);
3064 DLIST_ADD(conn->pending_trans, state);
3066 /* We need to send an interim response then receive the rest
3067 of the parameter/data bytes */
3068 reply_outbuf(req, 0, 0);
3069 show_msg((char *)req->outbuf);
3070 END_PROFILE(SMBnttrans);
3075 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3076 SAFE_FREE(state->data);
3077 SAFE_FREE(state->param);
3079 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3080 END_PROFILE(SMBnttrans);
3084 /****************************************************************************
3085 Reply to a SMBnttranss
3086 ****************************************************************************/
3088 void reply_nttranss(struct smb_request *req)
3090 connection_struct *conn = req->conn;
3091 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3092 struct trans_state *state;
3094 START_PROFILE(SMBnttranss);
3096 show_msg((char *)req->inbuf);
3098 if (req->wct < 18) {
3099 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3100 END_PROFILE(SMBnttranss);
3104 for (state = conn->pending_trans; state != NULL;
3105 state = state->next) {
3106 if (state->mid == req->mid) {
3111 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3112 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3113 END_PROFILE(SMBnttranss);
3117 /* Revise state->total_param and state->total_data in case they have
3118 changed downwards */
3119 if (IVAL(req->vwv+1, 1) < state->total_param) {
3120 state->total_param = IVAL(req->vwv+1, 1);
3122 if (IVAL(req->vwv+3, 1) < state->total_data) {
3123 state->total_data = IVAL(req->vwv+3, 1);
3126 pcnt = IVAL(req->vwv+5, 1);
3127 poff = IVAL(req->vwv+7, 1);
3128 pdisp = IVAL(req->vwv+9, 1);
3130 dcnt = IVAL(req->vwv+11, 1);
3131 doff = IVAL(req->vwv+13, 1);
3132 ddisp = IVAL(req->vwv+15, 1);
3134 state->received_param += pcnt;
3135 state->received_data += dcnt;
3137 if ((state->received_data > state->total_data) ||
3138 (state->received_param > state->total_param))
3142 if (trans_oob(state->total_param, pdisp, pcnt)
3143 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3146 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3150 if (trans_oob(state->total_data, ddisp, dcnt)
3151 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3154 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3157 if ((state->received_param < state->total_param) ||
3158 (state->received_data < state->total_data)) {
3159 END_PROFILE(SMBnttranss);
3163 handle_nttrans(conn, state, req);
3165 DLIST_REMOVE(conn->pending_trans, state);
3166 SAFE_FREE(state->data);
3167 SAFE_FREE(state->param);
3169 END_PROFILE(SMBnttranss);
3174 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3175 DLIST_REMOVE(conn->pending_trans, state);
3176 SAFE_FREE(state->data);
3177 SAFE_FREE(state->param);
3179 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3180 END_PROFILE(SMBnttranss);