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 "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "fake_file.h"
26 #include "../libcli/security/security.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "passdb/lookup_sid.h"
31 extern const struct generic_mapping file_generic_mapping;
33 static char *nttrans_realloc(char **ptr, size_t size)
36 smb_panic("nttrans_realloc() called with NULL ptr");
39 *ptr = (char *)SMB_REALLOC(*ptr, size);
43 memset(*ptr,'\0',size);
47 /****************************************************************************
48 Send the required number of replies back.
49 We assume all fields other than the data fields are
50 set correctly for the type of call.
51 HACK ! Always assumes smb_setup field is zero.
52 ****************************************************************************/
54 void send_nt_replies(connection_struct *conn,
55 struct smb_request *req, NTSTATUS nt_error,
56 char *params, int paramsize,
57 char *pdata, int datasize)
59 int data_to_send = datasize;
60 int params_to_send = paramsize;
64 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
65 int alignment_offset = 1;
66 int data_alignment_offset = 0;
67 struct smbd_server_connection *sconn = req->sconn;
68 int max_send = sconn->smb1.sessions.max_send;
71 * If there genuinely are no parameters or data to send just send
75 if(params_to_send == 0 && data_to_send == 0) {
76 reply_outbuf(req, 18, 0);
77 if (NT_STATUS_V(nt_error)) {
78 error_packet_set((char *)req->outbuf,
82 show_msg((char *)req->outbuf);
83 if (!srv_send_smb(sconn,
86 IS_CONN_ENCRYPTED(conn),
88 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
90 TALLOC_FREE(req->outbuf);
95 * When sending params and data ensure that both are nicely aligned.
96 * Only do this alignment when there is also data to send - else
97 * can cause NT redirector problems.
100 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
101 data_alignment_offset = 4 - (params_to_send % 4);
105 * Space is bufsize minus Netbios over TCP header minus SMB header.
106 * The alignment_offset is to align the param bytes on a four byte
107 * boundary (2 bytes for data len, one byte pad).
108 * NT needs this to work correctly.
111 useable_space = max_send - (smb_size
114 + data_alignment_offset);
116 if (useable_space < 0) {
117 char *msg = talloc_asprintf(
119 "send_nt_replies failed sanity useable_space = %d!!!",
121 DEBUG(0, ("%s\n", msg));
122 exit_server_cleanly(msg);
125 while (params_to_send || data_to_send) {
128 * Calculate whether we will totally or partially fill this packet.
131 total_sent_thistime = params_to_send + data_to_send;
134 * We can never send more than useable_space.
137 total_sent_thistime = MIN(total_sent_thistime, useable_space);
139 reply_outbuf(req, 18,
140 total_sent_thistime + alignment_offset
141 + data_alignment_offset);
144 * We might have had SMBnttranss in req->inbuf, fix that.
146 SCVAL(req->outbuf, smb_com, SMBnttrans);
149 * Set total params and data to be sent.
152 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
153 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
156 * Calculate how many parameters and data we can fit into
157 * this packet. Parameters get precedence.
160 params_sent_thistime = MIN(params_to_send,useable_space);
161 data_sent_thistime = useable_space - params_sent_thistime;
162 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
164 SIVAL(req->outbuf, smb_ntr_ParameterCount,
165 params_sent_thistime);
167 if(params_sent_thistime == 0) {
168 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
169 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
172 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
173 * parameter bytes, however the first 4 bytes of outbuf are
174 * the Netbios over TCP header. Thus use smb_base() to subtract
175 * them from the calculation.
178 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
179 ((smb_buf(req->outbuf)+alignment_offset)
180 - smb_base(req->outbuf)));
182 * Absolute displacement of param bytes sent in this packet.
185 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
190 * Deal with the data portion.
193 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
195 if(data_sent_thistime == 0) {
196 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
197 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
200 * The offset of the data bytes is the offset of the
201 * parameter bytes plus the number of parameters being sent this time.
204 SIVAL(req->outbuf, smb_ntr_DataOffset,
205 ((smb_buf(req->outbuf)+alignment_offset) -
206 smb_base(req->outbuf))
207 + params_sent_thistime + data_alignment_offset);
208 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
212 * Copy the param bytes into the packet.
215 if(params_sent_thistime) {
216 if (alignment_offset != 0) {
217 memset(smb_buf(req->outbuf), 0,
220 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
221 params_sent_thistime);
225 * Copy in the data bytes
228 if(data_sent_thistime) {
229 if (data_alignment_offset != 0) {
230 memset((smb_buf(req->outbuf)+alignment_offset+
231 params_sent_thistime), 0,
232 data_alignment_offset);
234 memcpy(smb_buf(req->outbuf)+alignment_offset
235 +params_sent_thistime+data_alignment_offset,
236 pd,data_sent_thistime);
239 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
240 params_sent_thistime, data_sent_thistime, useable_space));
241 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
242 params_to_send, data_to_send, paramsize, datasize));
244 if (NT_STATUS_V(nt_error)) {
245 error_packet_set((char *)req->outbuf,
250 /* Send the packet */
251 show_msg((char *)req->outbuf);
252 if (!srv_send_smb(sconn,
255 IS_CONN_ENCRYPTED(conn),
257 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
260 TALLOC_FREE(req->outbuf);
262 pp += params_sent_thistime;
263 pd += data_sent_thistime;
265 params_to_send -= params_sent_thistime;
266 data_to_send -= data_sent_thistime;
272 if(params_to_send < 0 || data_to_send < 0) {
273 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
274 params_to_send, data_to_send));
275 exit_server_cleanly("send_nt_replies: internal error");
280 /****************************************************************************
281 Reply to an NT create and X call on a pipe
282 ****************************************************************************/
284 static void nt_open_pipe(char *fname, connection_struct *conn,
285 struct smb_request *req, int *ppnum)
290 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
292 /* Strip \\ off the name. */
295 status = open_np_file(req, fname, &fsp);
296 if (!NT_STATUS_IS_OK(status)) {
297 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
298 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
302 reply_nterror(req, status);
310 /****************************************************************************
311 Reply to an NT create and X call for pipes.
312 ****************************************************************************/
314 static void do_ntcreate_pipe_open(connection_struct *conn,
315 struct smb_request *req)
320 uint32 flags = IVAL(req->vwv+3, 1);
321 TALLOC_CTX *ctx = talloc_tos();
323 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
326 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
330 nt_open_pipe(fname, conn, req, &pnum);
338 * Deal with pipe return.
341 if (flags & EXTENDED_RESPONSE_REQUIRED) {
342 /* This is very strange. We
343 * return 50 words, but only set
344 * the wcnt to 42 ? It's definately
345 * what happens on the wire....
347 reply_outbuf(req, 50, 0);
348 SCVAL(req->outbuf,smb_wct,42);
350 reply_outbuf(req, 34, 0);
353 p = (char *)req->outbuf + smb_vwv2;
357 SIVAL(p,0,FILE_WAS_OPENED);
360 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
363 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
365 SSVAL(p,2, 0x5FF); /* ? */
368 if (flags & EXTENDED_RESPONSE_REQUIRED) {
370 SIVAL(p,0,FILE_GENERIC_ALL);
372 * For pipes W2K3 seems to return
374 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
376 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
379 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
384 struct case_semantics_state {
385 connection_struct *conn;
388 bool short_case_preserve;
391 /****************************************************************************
392 Restore case semantics.
393 ****************************************************************************/
395 static int restore_case_semantics(struct case_semantics_state *state)
397 state->conn->case_sensitive = state->case_sensitive;
398 state->conn->case_preserve = state->case_preserve;
399 state->conn->short_case_preserve = state->short_case_preserve;
403 /****************************************************************************
405 ****************************************************************************/
407 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
408 connection_struct *conn)
410 struct case_semantics_state *result;
412 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
417 result->case_sensitive = conn->case_sensitive;
418 result->case_preserve = conn->case_preserve;
419 result->short_case_preserve = conn->short_case_preserve;
422 conn->case_sensitive = True;
423 conn->case_preserve = True;
424 conn->short_case_preserve = True;
426 talloc_set_destructor(result, restore_case_semantics);
431 /****************************************************************************
432 Reply to an NT create and X call.
433 ****************************************************************************/
435 void reply_ntcreate_and_X(struct smb_request *req)
437 connection_struct *conn = req->conn;
438 struct smb_filename *smb_fname = NULL;
442 uint32 file_attributes;
444 uint32 create_disposition;
445 uint32 create_options;
447 uint64_t allocation_size;
448 /* Breakout the oplock request bits so we can set the
449 reply bits separately. */
451 SMB_OFF_T file_len = 0;
453 files_struct *fsp = NULL;
455 struct timespec create_timespec;
456 struct timespec c_timespec;
457 struct timespec a_timespec;
458 struct timespec m_timespec;
459 struct timespec write_time_ts;
462 uint8_t oplock_granted = NO_OPLOCK_RETURN;
463 struct case_semantics_state *case_state = NULL;
464 TALLOC_CTX *ctx = talloc_tos();
466 START_PROFILE(SMBntcreateX);
469 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
473 flags = IVAL(req->vwv+3, 1);
474 access_mask = IVAL(req->vwv+7, 1);
475 file_attributes = IVAL(req->vwv+13, 1);
476 share_access = IVAL(req->vwv+15, 1);
477 create_disposition = IVAL(req->vwv+17, 1);
478 create_options = IVAL(req->vwv+19, 1);
479 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
481 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
482 #ifdef LARGE_SMB_OFF_T
483 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
486 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
487 STR_TERMINATE, &status);
489 if (!NT_STATUS_IS_OK(status)) {
490 reply_nterror(req, status);
494 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
495 "file_attributes = 0x%x, share_access = 0x%x, "
496 "create_disposition = 0x%x create_options = 0x%x "
497 "root_dir_fid = 0x%x, fname = %s\n",
499 (unsigned int)access_mask,
500 (unsigned int)file_attributes,
501 (unsigned int)share_access,
502 (unsigned int)create_disposition,
503 (unsigned int)create_options,
504 (unsigned int)root_dir_fid,
508 * we need to remove ignored bits when they come directly from the client
509 * because we reuse some of them for internal stuff
511 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
514 * If it's an IPC, use the pipe handler.
518 if (lp_nt_pipe_support()) {
519 do_ntcreate_pipe_open(conn, req);
522 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
526 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
527 if (oplock_request) {
528 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
532 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
533 case_state = set_posix_case_semantics(ctx, conn);
535 reply_nterror(req, NT_STATUS_NO_MEMORY);
540 status = filename_convert(ctx,
542 req->flags2 & FLAGS2_DFS_PATHNAMES,
548 TALLOC_FREE(case_state);
550 if (!NT_STATUS_IS_OK(status)) {
551 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
553 NT_STATUS_PATH_NOT_COVERED,
557 reply_nterror(req, status);
562 * Bug #6898 - clients using Windows opens should
563 * never be able to set this attribute into the
566 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
568 status = SMB_VFS_CREATE_FILE(
571 root_dir_fid, /* root_dir_fid */
572 smb_fname, /* fname */
573 access_mask, /* access_mask */
574 share_access, /* share_access */
575 create_disposition, /* create_disposition*/
576 create_options, /* create_options */
577 file_attributes, /* file_attributes */
578 oplock_request, /* oplock_request */
579 allocation_size, /* allocation_size */
580 0, /* private_flags */
586 if (!NT_STATUS_IS_OK(status)) {
587 if (open_was_deferred(req->mid)) {
588 /* We have re-scheduled this call, no error. */
591 reply_openerror(req, status);
595 /* Ensure we're pointing at the correct stat struct. */
596 TALLOC_FREE(smb_fname);
597 smb_fname = fsp->fsp_name;
600 * If the caller set the extended oplock request bit
601 * and we granted one (by whatever means) - set the
602 * correct bit for extended oplock reply.
605 if (oplock_request &&
606 (lp_fake_oplocks(SNUM(conn))
607 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
610 * Exclusive oplock granted
613 if (flags & REQUEST_BATCH_OPLOCK) {
614 oplock_granted = BATCH_OPLOCK_RETURN;
616 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
618 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
619 oplock_granted = LEVEL_II_OPLOCK_RETURN;
621 oplock_granted = NO_OPLOCK_RETURN;
624 file_len = smb_fname->st.st_ex_size;
626 if (flags & EXTENDED_RESPONSE_REQUIRED) {
627 /* This is very strange. We
628 * return 50 words, but only set
629 * the wcnt to 42 ? It's definately
630 * what happens on the wire....
632 reply_outbuf(req, 50, 0);
633 SCVAL(req->outbuf,smb_wct,42);
635 reply_outbuf(req, 34, 0);
638 p = (char *)req->outbuf + smb_vwv2;
640 SCVAL(p, 0, oplock_granted);
643 SSVAL(p,0,fsp->fnum);
645 if ((create_disposition == FILE_SUPERSEDE)
646 && (info == FILE_WAS_OVERWRITTEN)) {
647 SIVAL(p,0,FILE_WAS_SUPERSEDED);
653 fattr = dos_mode(conn, smb_fname);
655 fattr = FILE_ATTRIBUTE_NORMAL;
658 /* Deal with other possible opens having a modified
660 ZERO_STRUCT(write_time_ts);
661 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
662 if (!null_timespec(write_time_ts)) {
663 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
667 create_timespec = get_create_timespec(conn, fsp, smb_fname);
668 a_timespec = smb_fname->st.st_ex_atime;
669 m_timespec = smb_fname->st.st_ex_mtime;
670 c_timespec = get_change_timespec(conn, fsp, smb_fname);
672 if (lp_dos_filetime_resolution(SNUM(conn))) {
673 dos_filetime_timespec(&create_timespec);
674 dos_filetime_timespec(&a_timespec);
675 dos_filetime_timespec(&m_timespec);
676 dos_filetime_timespec(&c_timespec);
679 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
681 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
683 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
685 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
687 SIVAL(p,0,fattr); /* File Attributes. */
689 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
691 SOFF_T(p,0,file_len);
693 if (flags & EXTENDED_RESPONSE_REQUIRED) {
694 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
695 size_t num_names = 0;
696 unsigned int num_streams;
697 struct stream_struct *streams = NULL;
699 /* Do we have any EA's ? */
700 status = get_ea_names_from_file(ctx, conn, fsp,
701 smb_fname->base_name, NULL, &num_names);
702 if (NT_STATUS_IS_OK(status) && num_names) {
703 file_status &= ~NO_EAS;
705 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
706 &num_streams, &streams);
707 /* There is always one stream, ::$DATA. */
708 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
709 file_status &= ~NO_SUBSTREAMS;
711 TALLOC_FREE(streams);
712 SSVAL(p,2,file_status);
715 SCVAL(p,0,fsp->is_directory ? 1 : 0);
717 if (flags & EXTENDED_RESPONSE_REQUIRED) {
720 if (fsp->is_directory ||
721 can_write_to_file(conn, smb_fname)) {
722 perms = FILE_GENERIC_ALL;
724 perms = FILE_GENERIC_READ|FILE_EXECUTE;
729 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
730 fsp->fnum, smb_fname_str_dbg(smb_fname)));
734 END_PROFILE(SMBntcreateX);
738 /****************************************************************************
739 Reply to a NT_TRANSACT_CREATE call to open a pipe.
740 ****************************************************************************/
742 static void do_nt_transact_create_pipe(connection_struct *conn,
743 struct smb_request *req,
744 uint16 **ppsetup, uint32 setup_count,
745 char **ppparams, uint32 parameter_count,
746 char **ppdata, uint32 data_count)
749 char *params = *ppparams;
755 TALLOC_CTX *ctx = talloc_tos();
758 * Ensure minimum number of parameters sent.
761 if(parameter_count < 54) {
762 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
763 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
767 flags = IVAL(params,0);
769 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
770 parameter_count-53, STR_TERMINATE,
772 if (!NT_STATUS_IS_OK(status)) {
773 reply_nterror(req, status);
777 nt_open_pipe(fname, conn, req, &pnum);
784 /* Realloc the size of parameters and data we will return */
785 if (flags & EXTENDED_RESPONSE_REQUIRED) {
786 /* Extended response is 32 more byyes. */
791 params = nttrans_realloc(ppparams, param_len);
793 reply_nterror(req, NT_STATUS_NO_MEMORY);
798 SCVAL(p,0,NO_OPLOCK_RETURN);
803 SIVAL(p,0,FILE_WAS_OPENED);
807 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
810 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
812 SSVAL(p,2, 0x5FF); /* ? */
815 if (flags & EXTENDED_RESPONSE_REQUIRED) {
817 SIVAL(p,0,FILE_GENERIC_ALL);
819 * For pipes W2K3 seems to return
821 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
823 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
826 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
828 /* Send the required number of replies */
829 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
834 /****************************************************************************
835 Internal fn to set security descriptors.
836 ****************************************************************************/
838 NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
839 uint32_t security_info_sent)
841 struct security_descriptor *psd = NULL;
845 return NT_STATUS_INVALID_PARAMETER;
848 if (!CAN_WRITE(fsp->conn)) {
849 return NT_STATUS_ACCESS_DENIED;
852 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
856 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
858 if (!NT_STATUS_IS_OK(status)) {
862 if (psd->owner_sid == NULL) {
863 security_info_sent &= ~SECINFO_OWNER;
865 if (psd->group_sid == NULL) {
866 security_info_sent &= ~SECINFO_GROUP;
869 /* Ensure we have at least one thing set. */
870 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
871 return NT_STATUS_INVALID_PARAMETER;
874 /* Ensure we have the rights to do this. */
875 if (security_info_sent & SECINFO_OWNER) {
876 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
877 return NT_STATUS_ACCESS_DENIED;
881 if (security_info_sent & SECINFO_GROUP) {
882 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
883 return NT_STATUS_ACCESS_DENIED;
887 if (security_info_sent & SECINFO_DACL) {
888 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
889 return NT_STATUS_ACCESS_DENIED;
891 /* Convert all the generic bits. */
893 security_acl_map_generic(psd->dacl, &file_generic_mapping);
897 if (security_info_sent & SECINFO_SACL) {
898 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
899 return NT_STATUS_ACCESS_DENIED;
901 /* Convert all the generic bits. */
903 security_acl_map_generic(psd->sacl, &file_generic_mapping);
907 if (DEBUGLEVEL >= 10) {
908 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
909 NDR_PRINT_DEBUG(security_descriptor, psd);
912 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
919 /****************************************************************************
920 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
921 ****************************************************************************/
923 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
925 struct ea_list *ea_list_head = NULL;
932 while (offset + 4 <= data_size) {
933 size_t next_offset = IVAL(pdata,offset);
934 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
940 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
941 if (next_offset == 0) {
944 offset += next_offset;
950 /****************************************************************************
951 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
952 ****************************************************************************/
954 static void call_nt_transact_create(connection_struct *conn,
955 struct smb_request *req,
956 uint16 **ppsetup, uint32 setup_count,
957 char **ppparams, uint32 parameter_count,
958 char **ppdata, uint32 data_count,
959 uint32 max_data_count)
961 struct smb_filename *smb_fname = NULL;
963 char *params = *ppparams;
964 char *data = *ppdata;
965 /* Breakout the oplock request bits so we can set the reply bits separately. */
967 SMB_OFF_T file_len = 0;
969 files_struct *fsp = NULL;
973 uint32 file_attributes;
975 uint32 create_disposition;
976 uint32 create_options;
978 struct security_descriptor *sd = NULL;
981 struct timespec create_timespec;
982 struct timespec c_timespec;
983 struct timespec a_timespec;
984 struct timespec m_timespec;
985 struct timespec write_time_ts;
986 struct ea_list *ea_list = NULL;
989 uint64_t allocation_size;
991 uint8_t oplock_granted;
992 struct case_semantics_state *case_state = NULL;
993 TALLOC_CTX *ctx = talloc_tos();
995 DEBUG(5,("call_nt_transact_create\n"));
998 * If it's an IPC, use the pipe handler.
1002 if (lp_nt_pipe_support()) {
1003 do_nt_transact_create_pipe(
1005 ppsetup, setup_count,
1006 ppparams, parameter_count,
1007 ppdata, data_count);
1010 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1015 * Ensure minimum number of parameters sent.
1018 if(parameter_count < 54) {
1019 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1020 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1024 flags = IVAL(params,0);
1025 access_mask = IVAL(params,8);
1026 file_attributes = IVAL(params,20);
1027 share_access = IVAL(params,24);
1028 create_disposition = IVAL(params,28);
1029 create_options = IVAL(params,32);
1030 sd_len = IVAL(params,36);
1031 ea_len = IVAL(params,40);
1032 root_dir_fid = (uint16)IVAL(params,4);
1033 allocation_size = (uint64_t)IVAL(params,12);
1034 #ifdef LARGE_SMB_OFF_T
1035 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
1039 * we need to remove ignored bits when they come directly from the client
1040 * because we reuse some of them for internal stuff
1042 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1044 /* Ensure the data_len is correct for the sd and ea values given. */
1045 if ((ea_len + sd_len > data_count)
1046 || (ea_len > data_count) || (sd_len > data_count)
1047 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1048 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1049 "%u, data_count = %u\n", (unsigned int)ea_len,
1050 (unsigned int)sd_len, (unsigned int)data_count));
1051 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1056 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1059 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1061 if (!NT_STATUS_IS_OK(status)) {
1062 DEBUG(10, ("call_nt_transact_create: "
1063 "unmarshall_sec_desc failed: %s\n",
1064 nt_errstr(status)));
1065 reply_nterror(req, status);
1071 if (!lp_ea_support(SNUM(conn))) {
1072 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1073 "EA's not supported.\n",
1074 (unsigned int)ea_len));
1075 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1080 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1081 "too small (should be more than 10)\n",
1082 (unsigned int)ea_len ));
1083 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1087 /* We have already checked that ea_len <= data_count here. */
1088 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1090 if (ea_list == NULL) {
1091 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1096 srvstr_get_path(ctx, params, req->flags2, &fname,
1097 params+53, parameter_count-53,
1098 STR_TERMINATE, &status);
1099 if (!NT_STATUS_IS_OK(status)) {
1100 reply_nterror(req, status);
1104 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1105 case_state = set_posix_case_semantics(ctx, conn);
1107 reply_nterror(req, NT_STATUS_NO_MEMORY);
1112 status = filename_convert(ctx,
1114 req->flags2 & FLAGS2_DFS_PATHNAMES,
1120 TALLOC_FREE(case_state);
1122 if (!NT_STATUS_IS_OK(status)) {
1123 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1124 reply_botherror(req,
1125 NT_STATUS_PATH_NOT_COVERED,
1126 ERRSRV, ERRbadpath);
1129 reply_nterror(req, status);
1133 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1134 if (oplock_request) {
1135 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1140 * Bug #6898 - clients using Windows opens should
1141 * never be able to set this attribute into the
1144 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1146 status = SMB_VFS_CREATE_FILE(
1149 root_dir_fid, /* root_dir_fid */
1150 smb_fname, /* fname */
1151 access_mask, /* access_mask */
1152 share_access, /* share_access */
1153 create_disposition, /* create_disposition*/
1154 create_options, /* create_options */
1155 file_attributes, /* file_attributes */
1156 oplock_request, /* oplock_request */
1157 allocation_size, /* allocation_size */
1158 0, /* private_flags */
1160 ea_list, /* ea_list */
1164 if(!NT_STATUS_IS_OK(status)) {
1165 if (open_was_deferred(req->mid)) {
1166 /* We have re-scheduled this call, no error. */
1169 reply_openerror(req, status);
1173 /* Ensure we're pointing at the correct stat struct. */
1174 TALLOC_FREE(smb_fname);
1175 smb_fname = fsp->fsp_name;
1178 * If the caller set the extended oplock request bit
1179 * and we granted one (by whatever means) - set the
1180 * correct bit for extended oplock reply.
1183 if (oplock_request &&
1184 (lp_fake_oplocks(SNUM(conn))
1185 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1188 * Exclusive oplock granted
1191 if (flags & REQUEST_BATCH_OPLOCK) {
1192 oplock_granted = BATCH_OPLOCK_RETURN;
1194 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1196 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1197 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1199 oplock_granted = NO_OPLOCK_RETURN;
1202 file_len = smb_fname->st.st_ex_size;
1204 /* Realloc the size of parameters and data we will return */
1205 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1206 /* Extended response is 32 more byyes. */
1211 params = nttrans_realloc(ppparams, param_len);
1212 if(params == NULL) {
1213 reply_nterror(req, NT_STATUS_NO_MEMORY);
1218 SCVAL(p, 0, oplock_granted);
1221 SSVAL(p,0,fsp->fnum);
1223 if ((create_disposition == FILE_SUPERSEDE)
1224 && (info == FILE_WAS_OVERWRITTEN)) {
1225 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1231 fattr = dos_mode(conn, smb_fname);
1233 fattr = FILE_ATTRIBUTE_NORMAL;
1236 /* Deal with other possible opens having a modified
1238 ZERO_STRUCT(write_time_ts);
1239 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
1240 if (!null_timespec(write_time_ts)) {
1241 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1245 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1246 a_timespec = smb_fname->st.st_ex_atime;
1247 m_timespec = smb_fname->st.st_ex_mtime;
1248 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1250 if (lp_dos_filetime_resolution(SNUM(conn))) {
1251 dos_filetime_timespec(&create_timespec);
1252 dos_filetime_timespec(&a_timespec);
1253 dos_filetime_timespec(&m_timespec);
1254 dos_filetime_timespec(&c_timespec);
1257 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1259 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1261 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1263 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1265 SIVAL(p,0,fattr); /* File Attributes. */
1267 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1269 SOFF_T(p,0,file_len);
1271 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1272 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1273 size_t num_names = 0;
1274 unsigned int num_streams;
1275 struct stream_struct *streams = NULL;
1277 /* Do we have any EA's ? */
1278 status = get_ea_names_from_file(ctx, conn, fsp,
1279 smb_fname->base_name, NULL, &num_names);
1280 if (NT_STATUS_IS_OK(status) && num_names) {
1281 file_status &= ~NO_EAS;
1283 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
1284 &num_streams, &streams);
1285 /* There is always one stream, ::$DATA. */
1286 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1287 file_status &= ~NO_SUBSTREAMS;
1289 TALLOC_FREE(streams);
1290 SSVAL(p,2,file_status);
1293 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1295 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1298 if (fsp->is_directory ||
1299 can_write_to_file(conn, smb_fname)) {
1300 perms = FILE_GENERIC_ALL;
1302 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1307 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1308 smb_fname_str_dbg(smb_fname)));
1310 /* Send the required number of replies */
1311 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1316 /****************************************************************************
1317 Reply to a NT CANCEL request.
1318 conn POINTER CAN BE NULL HERE !
1319 ****************************************************************************/
1321 void reply_ntcancel(struct smb_request *req)
1324 * Go through and cancel any pending change notifies.
1327 START_PROFILE(SMBntcancel);
1328 srv_cancel_sign_response(req->sconn);
1329 remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
1330 remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
1332 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1333 (unsigned long long)req->mid));
1335 END_PROFILE(SMBntcancel);
1339 /****************************************************************************
1341 ****************************************************************************/
1343 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1344 connection_struct *conn,
1345 struct smb_request *req,
1346 struct smb_filename *smb_fname_src,
1347 struct smb_filename *smb_fname_dst,
1350 files_struct *fsp1,*fsp2;
1354 NTSTATUS status = NT_STATUS_OK;
1357 if (!CAN_WRITE(conn)) {
1358 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1362 /* Source must already exist. */
1363 if (!VALID_STAT(smb_fname_src->st)) {
1364 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1368 /* Ensure attributes match. */
1369 fattr = dos_mode(conn, smb_fname_src);
1370 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1371 status = NT_STATUS_NO_SUCH_FILE;
1375 /* Disallow if dst file already exists. */
1376 if (VALID_STAT(smb_fname_dst->st)) {
1377 status = NT_STATUS_OBJECT_NAME_COLLISION;
1381 /* No links from a directory. */
1382 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1383 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1387 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1388 smb_fname_str_dbg(smb_fname_src),
1389 smb_fname_str_dbg(smb_fname_dst)));
1391 status = SMB_VFS_CREATE_FILE(
1394 0, /* root_dir_fid */
1395 smb_fname_src, /* fname */
1396 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1397 FILE_READ_EA, /* access_mask */
1398 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1400 FILE_OPEN, /* create_disposition*/
1401 0, /* create_options */
1402 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1403 NO_OPLOCK, /* oplock_request */
1404 0, /* allocation_size */
1405 0, /* private_flags */
1411 if (!NT_STATUS_IS_OK(status)) {
1415 status = SMB_VFS_CREATE_FILE(
1418 0, /* root_dir_fid */
1419 smb_fname_dst, /* fname */
1420 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1421 FILE_WRITE_EA, /* access_mask */
1422 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1424 FILE_CREATE, /* create_disposition*/
1425 0, /* create_options */
1426 fattr, /* file_attributes */
1427 NO_OPLOCK, /* oplock_request */
1428 0, /* allocation_size */
1429 0, /* private_flags */
1435 if (!NT_STATUS_IS_OK(status)) {
1436 close_file(NULL, fsp1, ERROR_CLOSE);
1440 if (smb_fname_src->st.st_ex_size) {
1441 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1445 * As we are opening fsp1 read-only we only expect
1446 * an error on close on fsp2 if we are out of space.
1447 * Thus we don't look at the error return from the
1450 close_file(NULL, fsp1, NORMAL_CLOSE);
1452 /* Ensure the modtime is set correctly on the destination file. */
1453 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1455 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1457 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1458 creates the file. This isn't the correct thing to do in the copy
1460 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1462 status = NT_STATUS_NO_MEMORY;
1465 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1466 TALLOC_FREE(parent);
1468 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1469 status = NT_STATUS_DISK_FULL;
1473 if (!NT_STATUS_IS_OK(status)) {
1474 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1475 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1476 smb_fname_str_dbg(smb_fname_dst)));
1482 /****************************************************************************
1483 Reply to a NT rename request.
1484 ****************************************************************************/
1486 void reply_ntrename(struct smb_request *req)
1488 connection_struct *conn = req->conn;
1489 struct smb_filename *smb_fname_old = NULL;
1490 struct smb_filename *smb_fname_new = NULL;
1491 char *oldname = NULL;
1492 char *newname = NULL;
1495 bool src_has_wcard = False;
1496 bool dest_has_wcard = False;
1498 uint32_t ucf_flags_src = 0;
1499 uint32_t ucf_flags_dst = 0;
1501 TALLOC_CTX *ctx = talloc_tos();
1502 bool stream_rename = false;
1504 START_PROFILE(SMBntrename);
1507 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1511 attrs = SVAL(req->vwv+0, 0);
1512 rename_type = SVAL(req->vwv+1, 0);
1514 p = (const char *)req->buf + 1;
1515 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1516 &status, &src_has_wcard);
1517 if (!NT_STATUS_IS_OK(status)) {
1518 reply_nterror(req, status);
1522 if (ms_has_wild(oldname)) {
1523 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1528 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1529 &status, &dest_has_wcard);
1530 if (!NT_STATUS_IS_OK(status)) {
1531 reply_nterror(req, status);
1535 if (!lp_posix_pathnames()) {
1536 /* The newname must begin with a ':' if the
1537 oldname contains a ':'. */
1538 if (strchr_m(oldname, ':')) {
1539 if (newname[0] != ':') {
1540 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1543 stream_rename = true;
1548 * If this is a rename operation, allow wildcards and save the
1549 * destination's last component.
1551 if (rename_type == RENAME_FLAG_RENAME) {
1552 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1553 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1556 /* rename_internals() calls unix_convert(), so don't call it here. */
1557 status = filename_convert(ctx, conn,
1558 req->flags2 & FLAGS2_DFS_PATHNAMES,
1563 if (!NT_STATUS_IS_OK(status)) {
1564 if (NT_STATUS_EQUAL(status,
1565 NT_STATUS_PATH_NOT_COVERED)) {
1566 reply_botherror(req,
1567 NT_STATUS_PATH_NOT_COVERED,
1568 ERRSRV, ERRbadpath);
1571 reply_nterror(req, status);
1575 status = filename_convert(ctx, conn,
1576 req->flags2 & FLAGS2_DFS_PATHNAMES,
1581 if (!NT_STATUS_IS_OK(status)) {
1582 if (NT_STATUS_EQUAL(status,
1583 NT_STATUS_PATH_NOT_COVERED)) {
1584 reply_botherror(req,
1585 NT_STATUS_PATH_NOT_COVERED,
1586 ERRSRV, ERRbadpath);
1589 reply_nterror(req, status);
1593 if (stream_rename) {
1594 /* smb_fname_new must be the same as smb_fname_old. */
1595 TALLOC_FREE(smb_fname_new->base_name);
1596 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1597 smb_fname_old->base_name);
1598 if (!smb_fname_new->base_name) {
1599 reply_nterror(req, NT_STATUS_NO_MEMORY);
1604 DEBUG(3,("reply_ntrename: %s -> %s\n",
1605 smb_fname_str_dbg(smb_fname_old),
1606 smb_fname_str_dbg(smb_fname_new)));
1608 switch(rename_type) {
1609 case RENAME_FLAG_RENAME:
1610 status = rename_internals(ctx, conn, req,
1611 smb_fname_old, smb_fname_new,
1612 attrs, False, src_has_wcard,
1616 case RENAME_FLAG_HARD_LINK:
1617 if (src_has_wcard || dest_has_wcard) {
1619 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1621 status = hardlink_internals(ctx, conn,
1628 case RENAME_FLAG_COPY:
1629 if (src_has_wcard || dest_has_wcard) {
1631 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1633 status = copy_internals(ctx, conn, req,
1639 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1640 status = NT_STATUS_INVALID_PARAMETER;
1643 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1647 if (!NT_STATUS_IS_OK(status)) {
1648 if (open_was_deferred(req->mid)) {
1649 /* We have re-scheduled this call. */
1653 reply_nterror(req, status);
1657 reply_outbuf(req, 0, 0);
1659 END_PROFILE(SMBntrename);
1663 /****************************************************************************
1664 Reply to a notify change - queue the request and
1665 don't allow a directory to be opened.
1666 ****************************************************************************/
1668 static void smbd_smb1_notify_reply(struct smb_request *req,
1669 NTSTATUS error_code,
1670 uint8_t *buf, size_t len)
1672 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1675 static void call_nt_transact_notify_change(connection_struct *conn,
1676 struct smb_request *req,
1680 uint32 parameter_count,
1681 char **ppdata, uint32 data_count,
1682 uint32 max_data_count,
1683 uint32 max_param_count)
1685 uint16 *setup = *ppsetup;
1691 if(setup_count < 6) {
1692 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1696 fsp = file_fsp(req, SVAL(setup,4));
1697 filter = IVAL(setup, 0);
1698 recursive = (SVAL(setup, 6) != 0) ? True : False;
1700 DEBUG(3,("call_nt_transact_notify_change\n"));
1703 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1708 char *filter_string;
1710 if (!(filter_string = notify_filter_string(NULL, filter))) {
1711 reply_nterror(req,NT_STATUS_NO_MEMORY);
1715 DEBUG(3,("call_nt_transact_notify_change: notify change "
1716 "called on %s, filter = %s, recursive = %d\n",
1717 fsp_str_dbg(fsp), filter_string, recursive));
1719 TALLOC_FREE(filter_string);
1722 if((!fsp->is_directory) || (conn != fsp->conn)) {
1723 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1727 if (fsp->notify == NULL) {
1729 status = change_notify_create(fsp, filter, recursive);
1731 if (!NT_STATUS_IS_OK(status)) {
1732 DEBUG(10, ("change_notify_create returned %s\n",
1733 nt_errstr(status)));
1734 reply_nterror(req, status);
1739 if (fsp->notify->num_changes != 0) {
1742 * We've got changes pending, respond immediately
1746 * TODO: write a torture test to check the filtering behaviour
1750 change_notify_reply(req,
1754 smbd_smb1_notify_reply);
1757 * change_notify_reply() above has independently sent its
1764 * No changes pending, queue the request
1767 status = change_notify_add_request(req,
1771 smbd_smb1_notify_reply);
1772 if (!NT_STATUS_IS_OK(status)) {
1773 reply_nterror(req, status);
1778 /****************************************************************************
1779 Reply to an NT transact rename command.
1780 ****************************************************************************/
1782 static void call_nt_transact_rename(connection_struct *conn,
1783 struct smb_request *req,
1784 uint16 **ppsetup, uint32 setup_count,
1785 char **ppparams, uint32 parameter_count,
1786 char **ppdata, uint32 data_count,
1787 uint32 max_data_count)
1789 char *params = *ppparams;
1790 char *new_name = NULL;
1791 files_struct *fsp = NULL;
1792 bool dest_has_wcard = False;
1794 TALLOC_CTX *ctx = talloc_tos();
1796 if(parameter_count < 5) {
1797 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1801 fsp = file_fsp(req, SVAL(params, 0));
1802 if (!check_fsp(conn, req, fsp)) {
1805 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1806 parameter_count - 4,
1807 STR_TERMINATE, &status, &dest_has_wcard);
1808 if (!NT_STATUS_IS_OK(status)) {
1809 reply_nterror(req, status);
1814 * W2K3 ignores this request as the RAW-RENAME test
1815 * demonstrates, so we do.
1817 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1819 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1820 fsp_str_dbg(fsp), new_name));
1825 /******************************************************************************
1826 Fake up a completely empty SD.
1827 *******************************************************************************/
1829 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1833 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1835 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1836 return NT_STATUS_NO_MEMORY;
1839 return NT_STATUS_OK;
1842 /****************************************************************************
1843 Reply to query a security descriptor.
1844 Callable from SMB2 and SMB2.
1845 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1847 ****************************************************************************/
1849 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1850 TALLOC_CTX *mem_ctx,
1852 uint32_t security_info_wanted,
1853 uint32_t max_data_count,
1854 uint8_t **ppmarshalled_sd,
1858 struct security_descriptor *psd = NULL;
1861 * Get the permissions to return.
1864 if ((security_info_wanted & SECINFO_SACL) &&
1865 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1866 return NT_STATUS_ACCESS_DENIED;
1869 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1870 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1871 return NT_STATUS_ACCESS_DENIED;
1874 if (!lp_nt_acl_support(SNUM(conn))) {
1875 status = get_null_nt_acl(mem_ctx, &psd);
1877 status = SMB_VFS_FGET_NT_ACL(
1878 fsp, security_info_wanted, &psd);
1880 if (!NT_STATUS_IS_OK(status)) {
1884 if (!(security_info_wanted & SECINFO_OWNER)) {
1885 psd->owner_sid = NULL;
1887 if (!(security_info_wanted & SECINFO_GROUP)) {
1888 psd->group_sid = NULL;
1890 if (!(security_info_wanted & SECINFO_DACL)) {
1893 if (!(security_info_wanted & SECINFO_SACL)) {
1897 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1898 * present in the reply to match Windows behavior */
1899 if (psd->sacl == NULL &&
1900 security_info_wanted & SECINFO_SACL)
1901 psd->type |= SEC_DESC_SACL_PRESENT;
1902 if (psd->dacl == NULL &&
1903 security_info_wanted & SECINFO_DACL)
1904 psd->type |= SEC_DESC_DACL_PRESENT;
1906 *psd_size = ndr_size_security_descriptor(psd, 0);
1908 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1909 (unsigned long)*psd_size));
1911 if (DEBUGLEVEL >= 10) {
1912 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1914 NDR_PRINT_DEBUG(security_descriptor, psd);
1917 if (max_data_count < *psd_size) {
1919 return NT_STATUS_BUFFER_TOO_SMALL;
1922 status = marshall_sec_desc(mem_ctx, psd,
1923 ppmarshalled_sd, psd_size);
1925 if (!NT_STATUS_IS_OK(status)) {
1931 return NT_STATUS_OK;
1934 /****************************************************************************
1935 SMB1 reply to query a security descriptor.
1936 ****************************************************************************/
1938 static void call_nt_transact_query_security_desc(connection_struct *conn,
1939 struct smb_request *req,
1943 uint32 parameter_count,
1946 uint32 max_data_count)
1948 char *params = *ppparams;
1949 char *data = *ppdata;
1951 uint32 security_info_wanted;
1952 files_struct *fsp = NULL;
1954 uint8_t *marshalled_sd = NULL;
1956 if(parameter_count < 8) {
1957 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1961 fsp = file_fsp(req, SVAL(params,0));
1963 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1967 security_info_wanted = IVAL(params,4);
1969 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1970 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1971 (unsigned int)security_info_wanted));
1973 params = nttrans_realloc(ppparams, 4);
1974 if(params == NULL) {
1975 reply_nterror(req, NT_STATUS_NO_MEMORY);
1980 * Get the permissions to return.
1983 status = smbd_do_query_security_desc(conn,
1986 security_info_wanted,
1991 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
1992 SIVAL(params,0,(uint32_t)sd_size);
1993 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1994 params, 4, NULL, 0);
1998 if (!NT_STATUS_IS_OK(status)) {
1999 reply_nterror(req, status);
2003 SMB_ASSERT(sd_size > 0);
2005 SIVAL(params,0,(uint32_t)sd_size);
2007 if (max_data_count < sd_size) {
2008 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2009 params, 4, NULL, 0);
2014 * Allocate the data we will return.
2017 data = nttrans_realloc(ppdata, sd_size);
2019 reply_nterror(req, NT_STATUS_NO_MEMORY);
2023 memcpy(data, marshalled_sd, sd_size);
2025 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2030 /****************************************************************************
2031 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2032 ****************************************************************************/
2034 static void call_nt_transact_set_security_desc(connection_struct *conn,
2035 struct smb_request *req,
2039 uint32 parameter_count,
2042 uint32 max_data_count)
2044 char *params= *ppparams;
2045 char *data = *ppdata;
2046 files_struct *fsp = NULL;
2047 uint32 security_info_sent = 0;
2050 if(parameter_count < 8) {
2051 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2055 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2056 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2060 if (!CAN_WRITE(fsp->conn)) {
2061 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2065 if(!lp_nt_acl_support(SNUM(conn))) {
2069 security_info_sent = IVAL(params,4);
2071 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2072 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2074 if (data_count == 0) {
2075 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2079 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
2081 if (!NT_STATUS_IS_OK(status)) {
2082 reply_nterror(req, status);
2087 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2091 /****************************************************************************
2093 ****************************************************************************/
2095 static void call_nt_transact_ioctl(connection_struct *conn,
2096 struct smb_request *req,
2097 uint16 **ppsetup, uint32 setup_count,
2098 char **ppparams, uint32 parameter_count,
2099 char **ppdata, uint32 data_count,
2100 uint32 max_data_count)
2107 char *pdata = *ppdata;
2109 if (setup_count != 8) {
2110 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2111 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2115 function = IVAL(*ppsetup, 0);
2116 fidnum = SVAL(*ppsetup, 4);
2117 isFSctl = CVAL(*ppsetup, 6);
2118 compfilter = CVAL(*ppsetup, 7);
2120 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2121 function, fidnum, isFSctl, compfilter));
2123 fsp=file_fsp(req, fidnum);
2124 /* this check is done in each implemented function case for now
2125 because I don't want to break anything... --metze
2126 FSP_BELONGS_CONN(fsp,conn);*/
2128 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2131 case FSCTL_SET_SPARSE:
2133 bool set_sparse = true;
2136 if (data_count >= 1 && pdata[0] == 0) {
2140 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X]set[%u]\n",
2141 fidnum, set_sparse));
2143 if (!check_fsp_open(conn, req, fsp)) {
2147 status = file_set_sparse(conn, fsp, set_sparse);
2148 if (!NT_STATUS_IS_OK(status)) {
2149 DEBUG(9,("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
2150 smb_fname_str_dbg(fsp->fsp_name), set_sparse, nt_errstr(status)));
2151 reply_nterror(req, status);
2155 DEBUG(10,("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
2156 smb_fname_str_dbg(fsp->fsp_name), set_sparse, nt_errstr(status)));
2157 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2160 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2162 unsigned char objid[16];
2164 /* This should return the object-id on this file.
2165 * I think I'll make this be the inode+dev. JRA.
2168 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2170 if (!check_fsp_open(conn, req, fsp)) {
2175 pdata = nttrans_realloc(ppdata, data_count);
2176 if (pdata == NULL) {
2177 reply_nterror(req, NT_STATUS_NO_MEMORY);
2181 /* For backwards compatibility only store the dev/inode. */
2182 push_file_id_16(pdata, &fsp->file_id);
2183 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2184 push_file_id_16(pdata+32, &fsp->file_id);
2185 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2190 case FSCTL_GET_REPARSE_POINT:
2191 /* pretend this fail - my winXP does it like this
2195 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2196 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2199 case FSCTL_SET_REPARSE_POINT:
2200 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2204 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2205 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2208 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2211 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2212 * and return their volume names. If max_data_count is 16, then it is just
2213 * asking for the number of volumes and length of the combined names.
2215 * pdata is the data allocated by our caller, but that uses
2216 * total_data_count (which is 0 in our case) rather than max_data_count.
2217 * Allocate the correct amount and return the pointer to let
2218 * it be deallocated when we return.
2220 SHADOW_COPY_DATA *shadow_data = NULL;
2221 TALLOC_CTX *shadow_mem_ctx = NULL;
2222 bool labels = False;
2223 uint32 labels_data_count = 0;
2227 if (!check_fsp_open(conn, req, fsp)) {
2231 if (max_data_count < 16) {
2232 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2234 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2238 if (max_data_count > 16) {
2242 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2243 if (shadow_mem_ctx == NULL) {
2244 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2245 reply_nterror(req, NT_STATUS_NO_MEMORY);
2249 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2250 if (shadow_data == NULL) {
2251 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2252 talloc_destroy(shadow_mem_ctx);
2253 reply_nterror(req, NT_STATUS_NO_MEMORY);
2257 shadow_data->mem_ctx = shadow_mem_ctx;
2260 * Call the VFS routine to actually do the work.
2262 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2263 talloc_destroy(shadow_data->mem_ctx);
2264 if (errno == ENOSYS) {
2265 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2266 conn->connectpath));
2267 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2270 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2271 conn->connectpath));
2272 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2277 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2282 data_count = 12+labels_data_count+4;
2285 if (max_data_count<data_count) {
2286 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2287 max_data_count,data_count));
2288 talloc_destroy(shadow_data->mem_ctx);
2289 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2293 pdata = nttrans_realloc(ppdata, data_count);
2294 if (pdata == NULL) {
2295 talloc_destroy(shadow_data->mem_ctx);
2296 reply_nterror(req, NT_STATUS_NO_MEMORY);
2302 /* num_volumes 4 bytes */
2303 SIVAL(pdata,0,shadow_data->num_volumes);
2306 /* num_labels 4 bytes */
2307 SIVAL(pdata,4,shadow_data->num_volumes);
2310 /* needed_data_count 4 bytes */
2311 SIVAL(pdata, 8, labels_data_count+4);
2315 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2316 shadow_data->num_volumes, fsp_str_dbg(fsp)));
2317 if (labels && shadow_data->labels) {
2318 for (i=0;i<shadow_data->num_volumes;i++) {
2319 srvstr_push(pdata, req->flags2,
2320 cur_pdata, shadow_data->labels[i],
2321 2*sizeof(SHADOW_COPY_LABEL),
2322 STR_UNICODE|STR_TERMINATE);
2323 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2324 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2328 talloc_destroy(shadow_data->mem_ctx);
2330 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2336 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2338 /* pretend this succeeded -
2340 * we have to send back a list with all files owned by this SID
2342 * but I have to check that --metze
2348 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2350 if (!check_fsp_open(conn, req, fsp)) {
2354 if (data_count < 8) {
2355 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2359 sid_len = MIN(data_count-4,SID_MAX_SIZE);
2361 /* unknown 4 bytes: this is not the length of the sid :-( */
2362 /*unknown = IVAL(pdata,0);*/
2364 if (!sid_parse(pdata+4,sid_len,&sid)) {
2365 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2368 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2370 if (!sid_to_uid(&sid, &uid)) {
2371 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2372 sid_string_dbg(&sid),
2373 (unsigned long)sid_len));
2377 /* we can take a look at the find source :-)
2379 * find ./ -uid $uid -name '*' is what we need here
2382 * and send 4bytes len and then NULL terminated unicode strings
2385 * but I don't know how to deal with the paged results
2386 * (maybe we can hang the result anywhere in the fsp struct)
2388 * we don't send all files at once
2389 * and at the next we should *not* start from the beginning,
2390 * so we have to cache the result
2395 /* this works for now... */
2396 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2399 case FSCTL_QUERY_ALLOCATED_RANGES:
2401 /* FIXME: This is just a dummy reply, telling that all of the
2402 * file is allocated. MKS cp needs that.
2403 * Adding the real allocated ranges via FIEMAP on Linux
2404 * and SEEK_DATA/SEEK_HOLE on Solaris is needed to make
2405 * this FSCTL correct for sparse files.
2408 uint64_t offset, length;
2410 if (!check_fsp_open(conn, req, fsp)) {
2414 if (data_count != 16) {
2415 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: data_count(%u) != 16 is invalid!\n",
2417 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2421 if (max_data_count < 16) {
2422 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: max_data_count(%u) < 16 is invalid!\n",
2424 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2428 offset = BVAL(pdata,0);
2429 length = BVAL(pdata,8);
2431 if (offset + length < offset) {
2432 /* No 64-bit integer wrap. */
2433 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2437 status = vfs_stat_fsp(fsp);
2438 if (!NT_STATUS_IS_OK(status)) {
2439 reply_nterror(req, status);
2443 if (offset > fsp->fsp_name->st.st_ex_size ||
2444 fsp->fsp_name->st.st_ex_size == 0 ||
2446 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2448 uint64_t end = offset + length;
2449 end = MIN(end, fsp->fsp_name->st.st_ex_size);
2452 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2457 case FSCTL_IS_VOLUME_DIRTY:
2458 DEBUG(10,("FSCTL_IS_VOLUME_DIRTY: called on FID[0x%04X] "
2459 "(but not implemented)\n", (int)fidnum));
2461 * http://msdn.microsoft.com/en-us/library/cc232128%28PROT.10%29.aspx
2462 * says we have to respond with NT_STATUS_INVALID_PARAMETER
2464 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2467 /* Only print this once... */
2468 if (!logged_ioctl_message) {
2469 logged_ioctl_message = true;
2470 DEBUG(2,("call_nt_transact_ioctl(0x%x): "
2471 "Currently not implemented.\n",
2476 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2480 #ifdef HAVE_SYS_QUOTAS
2481 /****************************************************************************
2482 Reply to get user quota
2483 ****************************************************************************/
2485 static void call_nt_transact_get_user_quota(connection_struct *conn,
2486 struct smb_request *req,
2490 uint32 parameter_count,
2493 uint32 max_data_count)
2495 NTSTATUS nt_status = NT_STATUS_OK;
2496 char *params = *ppparams;
2497 char *pdata = *ppdata;
2499 int data_len=0,param_len=0;
2502 files_struct *fsp = NULL;
2506 bool start_enum = True;
2507 SMB_NTQUOTA_STRUCT qt;
2508 SMB_NTQUOTA_LIST *tmp_list;
2509 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2514 if (conn->session_info->utok.uid != 0) {
2515 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2516 "[%s]\n", lp_servicename(SNUM(conn)),
2517 conn->session_info->unix_name));
2518 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2523 * Ensure minimum number of parameters sent.
2526 if (parameter_count < 4) {
2527 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2528 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2532 /* maybe we can check the quota_fnum */
2533 fsp = file_fsp(req, SVAL(params,0));
2534 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2535 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2536 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2540 /* the NULL pointer checking for fsp->fake_file_handle->pd
2541 * is done by CHECK_NTQUOTA_HANDLE_OK()
2543 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2545 level = SVAL(params,2);
2547 /* unknown 12 bytes leading in params */
2550 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2551 /* seems that we should continue with the enum here --metze */
2553 if (qt_handle->quota_list!=NULL &&
2554 qt_handle->tmp_list==NULL) {
2557 free_ntquota_list(&(qt_handle->quota_list));
2559 /* Realloc the size of parameters and data we will return */
2561 params = nttrans_realloc(ppparams, param_len);
2562 if(params == NULL) {
2563 reply_nterror(req, NT_STATUS_NO_MEMORY);
2568 SIVAL(params,0,data_len);
2575 case TRANSACT_GET_USER_QUOTA_LIST_START:
2577 if (qt_handle->quota_list==NULL &&
2578 qt_handle->tmp_list==NULL) {
2582 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2583 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2587 /* Realloc the size of parameters and data we will return */
2589 params = nttrans_realloc(ppparams, param_len);
2590 if(params == NULL) {
2591 reply_nterror(req, NT_STATUS_NO_MEMORY);
2595 /* we should not trust the value in max_data_count*/
2596 max_data_count = MIN(max_data_count,2048);
2598 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2600 reply_nterror(req, NT_STATUS_NO_MEMORY);
2606 /* set params Size of returned Quota Data 4 bytes*/
2607 /* but set it later when we know it */
2609 /* for each entry push the data */
2612 qt_handle->tmp_list = qt_handle->quota_list;
2615 tmp_list = qt_handle->tmp_list;
2617 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2618 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2620 sid_len = ndr_size_dom_sid(
2621 &tmp_list->quotas->sid, 0);
2622 entry_len = 40 + sid_len;
2624 /* nextoffset entry 4 bytes */
2625 SIVAL(entry,0,entry_len);
2627 /* then the len of the SID 4 bytes */
2628 SIVAL(entry,4,sid_len);
2630 /* unknown data 8 bytes uint64_t */
2631 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2633 /* the used disk space 8 bytes uint64_t */
2634 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2636 /* the soft quotas 8 bytes uint64_t */
2637 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2639 /* the hard quotas 8 bytes uint64_t */
2640 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2642 /* and now the SID */
2643 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2646 qt_handle->tmp_list = tmp_list;
2648 /* overwrite the offset of the last entry */
2649 SIVAL(entry-entry_len,0,0);
2651 data_len = 4+qt_len;
2652 /* overwrite the params quota_data_len */
2653 SIVAL(params,0,data_len);
2657 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2659 /* unknown 4 bytes IVAL(pdata,0) */
2661 if (data_count < 8) {
2662 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2663 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2667 sid_len = IVAL(pdata,4);
2668 /* Ensure this is less than 1mb. */
2669 if (sid_len > (1024*1024)) {
2670 reply_nterror(req, NT_STATUS_NO_MEMORY);
2674 if (data_count < 8+sid_len) {
2675 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2676 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2680 data_len = 4+40+sid_len;
2682 if (max_data_count < data_len) {
2683 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2684 max_data_count, data_len));
2686 SIVAL(params,0,data_len);
2688 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2692 if (!sid_parse(pdata+8,sid_len,&sid)) {
2693 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2697 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2700 * we have to return zero's in all fields
2701 * instead of returning an error here
2706 /* Realloc the size of parameters and data we will return */
2708 params = nttrans_realloc(ppparams, param_len);
2709 if(params == NULL) {
2710 reply_nterror(req, NT_STATUS_NO_MEMORY);
2714 pdata = nttrans_realloc(ppdata, data_len);
2716 reply_nterror(req, NT_STATUS_NO_MEMORY);
2722 /* set params Size of returned Quota Data 4 bytes*/
2723 SIVAL(params,0,data_len);
2725 /* nextoffset entry 4 bytes */
2728 /* then the len of the SID 4 bytes */
2729 SIVAL(entry,4,sid_len);
2731 /* unknown data 8 bytes uint64_t */
2732 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2734 /* the used disk space 8 bytes uint64_t */
2735 SBIG_UINT(entry,16,qt.usedspace);
2737 /* the soft quotas 8 bytes uint64_t */
2738 SBIG_UINT(entry,24,qt.softlim);
2740 /* the hard quotas 8 bytes uint64_t */
2741 SBIG_UINT(entry,32,qt.hardlim);
2743 /* and now the SID */
2744 sid_linearize(entry+40, sid_len, &sid);
2749 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2750 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2755 send_nt_replies(conn, req, nt_status, params, param_len,
2759 /****************************************************************************
2760 Reply to set user quota
2761 ****************************************************************************/
2763 static void call_nt_transact_set_user_quota(connection_struct *conn,
2764 struct smb_request *req,
2768 uint32 parameter_count,
2771 uint32 max_data_count)
2773 char *params = *ppparams;
2774 char *pdata = *ppdata;
2775 int data_len=0,param_len=0;
2776 SMB_NTQUOTA_STRUCT qt;
2779 files_struct *fsp = NULL;
2784 if (conn->session_info->utok.uid != 0) {
2785 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2786 "[%s]\n", lp_servicename(SNUM(conn)),
2787 conn->session_info->unix_name));
2788 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2793 * Ensure minimum number of parameters sent.
2796 if (parameter_count < 2) {
2797 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2798 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2802 /* maybe we can check the quota_fnum */
2803 fsp = file_fsp(req, SVAL(params,0));
2804 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2805 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2806 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2810 if (data_count < 40) {
2811 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2812 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2816 /* offset to next quota record.
2817 * 4 bytes IVAL(pdata,0)
2822 sid_len = IVAL(pdata,4);
2824 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2825 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2826 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2830 /* unknown 8 bytes in pdata
2831 * maybe its the change time in NTTIME
2834 /* the used space 8 bytes (uint64_t)*/
2835 qt.usedspace = (uint64_t)IVAL(pdata,16);
2836 #ifdef LARGE_SMB_OFF_T
2837 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2838 #else /* LARGE_SMB_OFF_T */
2839 if ((IVAL(pdata,20) != 0)&&
2840 ((qt.usedspace != 0xFFFFFFFF)||
2841 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2842 /* more than 32 bits? */
2843 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2846 #endif /* LARGE_SMB_OFF_T */
2848 /* the soft quotas 8 bytes (uint64_t)*/
2849 qt.softlim = (uint64_t)IVAL(pdata,24);
2850 #ifdef LARGE_SMB_OFF_T
2851 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2852 #else /* LARGE_SMB_OFF_T */
2853 if ((IVAL(pdata,28) != 0)&&
2854 ((qt.softlim != 0xFFFFFFFF)||
2855 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2856 /* more than 32 bits? */
2857 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2860 #endif /* LARGE_SMB_OFF_T */
2862 /* the hard quotas 8 bytes (uint64_t)*/
2863 qt.hardlim = (uint64_t)IVAL(pdata,32);
2864 #ifdef LARGE_SMB_OFF_T
2865 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2866 #else /* LARGE_SMB_OFF_T */
2867 if ((IVAL(pdata,36) != 0)&&
2868 ((qt.hardlim != 0xFFFFFFFF)||
2869 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2870 /* more than 32 bits? */
2871 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2874 #endif /* LARGE_SMB_OFF_T */
2876 if (!sid_parse(pdata+40,sid_len,&sid)) {
2877 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2881 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2883 /* 44 unknown bytes left... */
2885 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2886 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2890 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2893 #endif /* HAVE_SYS_QUOTAS */
2895 static void handle_nttrans(connection_struct *conn,
2896 struct trans_state *state,
2897 struct smb_request *req)
2899 if (get_Protocol() >= PROTOCOL_NT1) {
2900 req->flags2 |= 0x40; /* IS_LONG_NAME */
2901 SSVAL(req->inbuf,smb_flg2,req->flags2);
2905 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2907 /* Now we must call the relevant NT_TRANS function */
2908 switch(state->call) {
2909 case NT_TRANSACT_CREATE:
2911 START_PROFILE(NT_transact_create);
2912 call_nt_transact_create(
2914 &state->setup, state->setup_count,
2915 &state->param, state->total_param,
2916 &state->data, state->total_data,
2917 state->max_data_return);
2918 END_PROFILE(NT_transact_create);
2922 case NT_TRANSACT_IOCTL:
2924 START_PROFILE(NT_transact_ioctl);
2925 call_nt_transact_ioctl(
2927 &state->setup, state->setup_count,
2928 &state->param, state->total_param,
2929 &state->data, state->total_data,
2930 state->max_data_return);
2931 END_PROFILE(NT_transact_ioctl);
2935 case NT_TRANSACT_SET_SECURITY_DESC:
2937 START_PROFILE(NT_transact_set_security_desc);
2938 call_nt_transact_set_security_desc(
2940 &state->setup, state->setup_count,
2941 &state->param, state->total_param,
2942 &state->data, state->total_data,
2943 state->max_data_return);
2944 END_PROFILE(NT_transact_set_security_desc);
2948 case NT_TRANSACT_NOTIFY_CHANGE:
2950 START_PROFILE(NT_transact_notify_change);
2951 call_nt_transact_notify_change(
2953 &state->setup, state->setup_count,
2954 &state->param, state->total_param,
2955 &state->data, state->total_data,
2956 state->max_data_return,
2957 state->max_param_return);
2958 END_PROFILE(NT_transact_notify_change);
2962 case NT_TRANSACT_RENAME:
2964 START_PROFILE(NT_transact_rename);
2965 call_nt_transact_rename(
2967 &state->setup, state->setup_count,
2968 &state->param, state->total_param,
2969 &state->data, state->total_data,
2970 state->max_data_return);
2971 END_PROFILE(NT_transact_rename);
2975 case NT_TRANSACT_QUERY_SECURITY_DESC:
2977 START_PROFILE(NT_transact_query_security_desc);
2978 call_nt_transact_query_security_desc(
2980 &state->setup, state->setup_count,
2981 &state->param, state->total_param,
2982 &state->data, state->total_data,
2983 state->max_data_return);
2984 END_PROFILE(NT_transact_query_security_desc);
2988 #ifdef HAVE_SYS_QUOTAS
2989 case NT_TRANSACT_GET_USER_QUOTA:
2991 START_PROFILE(NT_transact_get_user_quota);
2992 call_nt_transact_get_user_quota(
2994 &state->setup, state->setup_count,
2995 &state->param, state->total_param,
2996 &state->data, state->total_data,
2997 state->max_data_return);
2998 END_PROFILE(NT_transact_get_user_quota);
3002 case NT_TRANSACT_SET_USER_QUOTA:
3004 START_PROFILE(NT_transact_set_user_quota);
3005 call_nt_transact_set_user_quota(
3007 &state->setup, state->setup_count,
3008 &state->param, state->total_param,
3009 &state->data, state->total_data,
3010 state->max_data_return);
3011 END_PROFILE(NT_transact_set_user_quota);
3014 #endif /* HAVE_SYS_QUOTAS */
3017 /* Error in request */
3018 DEBUG(0,("handle_nttrans: Unknown request %d in "
3019 "nttrans call\n", state->call));
3020 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
3026 /****************************************************************************
3027 Reply to a SMBNTtrans.
3028 ****************************************************************************/
3030 void reply_nttrans(struct smb_request *req)
3032 connection_struct *conn = req->conn;
3037 uint16 function_code;
3039 struct trans_state *state;
3041 START_PROFILE(SMBnttrans);
3043 if (req->wct < 19) {
3044 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3045 END_PROFILE(SMBnttrans);
3049 pscnt = IVAL(req->vwv+9, 1);
3050 psoff = IVAL(req->vwv+11, 1);
3051 dscnt = IVAL(req->vwv+13, 1);
3052 dsoff = IVAL(req->vwv+15, 1);
3053 function_code = SVAL(req->vwv+18, 0);
3055 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3056 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3057 END_PROFILE(SMBnttrans);
3061 result = allow_new_trans(conn->pending_trans, req->mid);
3062 if (!NT_STATUS_IS_OK(result)) {
3063 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3064 reply_nterror(req, result);
3065 END_PROFILE(SMBnttrans);
3069 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
3070 reply_nterror(req, NT_STATUS_NO_MEMORY);
3071 END_PROFILE(SMBnttrans);
3075 state->cmd = SMBnttrans;
3077 state->mid = req->mid;
3078 state->vuid = req->vuid;
3079 state->total_data = IVAL(req->vwv+3, 1);
3081 state->total_param = IVAL(req->vwv+1, 1);
3082 state->param = NULL;
3083 state->max_data_return = IVAL(req->vwv+7, 1);
3084 state->max_param_return = IVAL(req->vwv+5, 1);
3086 /* setup count is in *words* */
3087 state->setup_count = 2*CVAL(req->vwv+17, 1);
3088 state->setup = NULL;
3089 state->call = function_code;
3091 DEBUG(10, ("num_setup=%u, "
3092 "param_total=%u, this_param=%u, max_param=%u, "
3093 "data_total=%u, this_data=%u, max_data=%u, "
3094 "param_offset=%u, data_offset=%u\n",
3095 (unsigned)state->setup_count,
3096 (unsigned)state->total_param, (unsigned)pscnt,
3097 (unsigned)state->max_param_return,
3098 (unsigned)state->total_data, (unsigned)dscnt,
3099 (unsigned)state->max_data_return,
3100 (unsigned)psoff, (unsigned)dsoff));
3103 * All nttrans messages we handle have smb_wct == 19 +
3104 * state->setup_count. Ensure this is so as a sanity check.
3107 if(req->wct != 19 + (state->setup_count/2)) {
3108 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3109 req->wct, 19 + (state->setup_count/2)));
3113 /* Don't allow more than 128mb for each value. */
3114 if ((state->total_data > (1024*1024*128)) ||
3115 (state->total_param > (1024*1024*128))) {
3116 reply_nterror(req, NT_STATUS_NO_MEMORY);
3117 END_PROFILE(SMBnttrans);
3121 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3124 if (state->total_data) {
3126 if (trans_oob(state->total_data, 0, dscnt)
3127 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
3131 /* Can't use talloc here, the core routines do realloc on the
3132 * params and data. */
3133 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3134 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3135 "bytes !\n", (unsigned int)state->total_data));
3137 reply_nterror(req, NT_STATUS_NO_MEMORY);
3138 END_PROFILE(SMBnttrans);
3142 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3145 if (state->total_param) {
3147 if (trans_oob(state->total_param, 0, pscnt)
3148 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
3152 /* Can't use talloc here, the core routines do realloc on the
3153 * params and data. */
3154 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3155 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3156 "bytes !\n", (unsigned int)state->total_param));
3157 SAFE_FREE(state->data);
3159 reply_nterror(req, NT_STATUS_NO_MEMORY);
3160 END_PROFILE(SMBnttrans);
3164 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3167 state->received_data = dscnt;
3168 state->received_param = pscnt;
3170 if(state->setup_count > 0) {
3171 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3172 state->setup_count));
3175 * No overflow possible here, state->setup_count is an
3176 * unsigned int, being filled by a single byte from
3177 * CVAL(req->vwv+13, 0) above. The cast in the comparison
3178 * below is not necessary, it's here to clarify things. The
3179 * validity of req->vwv and req->wct has been checked in
3180 * init_smb_request already.
3182 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
3186 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3187 if (state->setup == NULL) {
3188 DEBUG(0,("reply_nttrans : Out of memory\n"));
3189 SAFE_FREE(state->data);
3190 SAFE_FREE(state->param);
3192 reply_nterror(req, NT_STATUS_NO_MEMORY);
3193 END_PROFILE(SMBnttrans);
3197 memcpy(state->setup, req->vwv+19, state->setup_count);
3198 dump_data(10, (uint8 *)state->setup, state->setup_count);
3201 if ((state->received_data == state->total_data) &&
3202 (state->received_param == state->total_param)) {
3203 handle_nttrans(conn, state, req);
3204 SAFE_FREE(state->param);
3205 SAFE_FREE(state->data);
3207 END_PROFILE(SMBnttrans);
3211 DLIST_ADD(conn->pending_trans, state);
3213 /* We need to send an interim response then receive the rest
3214 of the parameter/data bytes */
3215 reply_outbuf(req, 0, 0);
3216 show_msg((char *)req->outbuf);
3217 END_PROFILE(SMBnttrans);
3222 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3223 SAFE_FREE(state->data);
3224 SAFE_FREE(state->param);
3226 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3227 END_PROFILE(SMBnttrans);
3231 /****************************************************************************
3232 Reply to a SMBnttranss
3233 ****************************************************************************/
3235 void reply_nttranss(struct smb_request *req)
3237 connection_struct *conn = req->conn;
3238 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3239 struct trans_state *state;
3241 START_PROFILE(SMBnttranss);
3243 show_msg((char *)req->inbuf);
3245 if (req->wct < 18) {
3246 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3247 END_PROFILE(SMBnttranss);
3251 for (state = conn->pending_trans; state != NULL;
3252 state = state->next) {
3253 if (state->mid == req->mid) {
3258 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3259 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3260 END_PROFILE(SMBnttranss);
3264 /* Revise state->total_param and state->total_data in case they have
3265 changed downwards */
3266 if (IVAL(req->vwv+1, 1) < state->total_param) {
3267 state->total_param = IVAL(req->vwv+1, 1);
3269 if (IVAL(req->vwv+3, 1) < state->total_data) {
3270 state->total_data = IVAL(req->vwv+3, 1);
3273 pcnt = IVAL(req->vwv+5, 1);
3274 poff = IVAL(req->vwv+7, 1);
3275 pdisp = IVAL(req->vwv+9, 1);
3277 dcnt = IVAL(req->vwv+11, 1);
3278 doff = IVAL(req->vwv+13, 1);
3279 ddisp = IVAL(req->vwv+15, 1);
3281 state->received_param += pcnt;
3282 state->received_data += dcnt;
3284 if ((state->received_data > state->total_data) ||
3285 (state->received_param > state->total_param))
3289 if (trans_oob(state->total_param, pdisp, pcnt)
3290 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3293 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3297 if (trans_oob(state->total_data, ddisp, dcnt)
3298 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3301 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3304 if ((state->received_param < state->total_param) ||
3305 (state->received_data < state->total_data)) {
3306 END_PROFILE(SMBnttranss);
3310 handle_nttrans(conn, state, req);
3312 DLIST_REMOVE(conn->pending_trans, state);
3313 SAFE_FREE(state->data);
3314 SAFE_FREE(state->param);
3316 END_PROFILE(SMBnttranss);
3321 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3322 DLIST_REMOVE(conn->pending_trans, state);
3323 SAFE_FREE(state->data);
3324 SAFE_FREE(state->param);
3326 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3327 END_PROFILE(SMBnttranss);