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"
30 #include "smbprofile.h"
31 #include "libsmb/libsmb.h"
32 #include "lib/util_ea.h"
34 extern const struct generic_mapping file_generic_mapping;
36 static char *nttrans_realloc(char **ptr, size_t size)
39 smb_panic("nttrans_realloc() called with NULL ptr");
42 *ptr = (char *)SMB_REALLOC(*ptr, size);
46 memset(*ptr,'\0',size);
50 /****************************************************************************
51 Send the required number of replies back.
52 We assume all fields other than the data fields are
53 set correctly for the type of call.
54 HACK ! Always assumes smb_setup field is zero.
55 ****************************************************************************/
57 static void send_nt_replies(connection_struct *conn,
58 struct smb_request *req, NTSTATUS nt_error,
59 char *params, int paramsize,
60 char *pdata, int datasize)
62 int data_to_send = datasize;
63 int params_to_send = paramsize;
67 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
68 int alignment_offset = 1;
69 int data_alignment_offset = 0;
70 struct smbd_server_connection *sconn = req->sconn;
71 int max_send = sconn->smb1.sessions.max_send;
74 * If there genuinely are no parameters or data to send just send
78 if(params_to_send == 0 && data_to_send == 0) {
79 reply_outbuf(req, 18, 0);
80 if (NT_STATUS_V(nt_error)) {
81 error_packet_set((char *)req->outbuf,
85 show_msg((char *)req->outbuf);
86 if (!srv_send_smb(sconn,
89 IS_CONN_ENCRYPTED(conn),
91 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
93 TALLOC_FREE(req->outbuf);
98 * When sending params and data ensure that both are nicely aligned.
99 * Only do this alignment when there is also data to send - else
100 * can cause NT redirector problems.
103 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
104 data_alignment_offset = 4 - (params_to_send % 4);
108 * Space is bufsize minus Netbios over TCP header minus SMB header.
109 * The alignment_offset is to align the param bytes on a four byte
110 * boundary (2 bytes for data len, one byte pad).
111 * NT needs this to work correctly.
114 useable_space = max_send - (smb_size
117 + data_alignment_offset);
119 if (useable_space < 0) {
120 char *msg = talloc_asprintf(
122 "send_nt_replies failed sanity useable_space = %d!!!",
124 DEBUG(0, ("%s\n", msg));
125 exit_server_cleanly(msg);
128 while (params_to_send || data_to_send) {
131 * Calculate whether we will totally or partially fill this packet.
134 total_sent_thistime = params_to_send + data_to_send;
137 * We can never send more than useable_space.
140 total_sent_thistime = MIN(total_sent_thistime, useable_space);
142 reply_outbuf(req, 18,
143 total_sent_thistime + alignment_offset
144 + data_alignment_offset);
147 * Set total params and data to be sent.
150 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
151 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
154 * Calculate how many parameters and data we can fit into
155 * this packet. Parameters get precedence.
158 params_sent_thistime = MIN(params_to_send,useable_space);
159 data_sent_thistime = useable_space - params_sent_thistime;
160 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
162 SIVAL(req->outbuf, smb_ntr_ParameterCount,
163 params_sent_thistime);
165 if(params_sent_thistime == 0) {
166 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
167 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
170 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
171 * parameter bytes, however the first 4 bytes of outbuf are
172 * the Netbios over TCP header. Thus use smb_base() to subtract
173 * them from the calculation.
176 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
177 ((smb_buf(req->outbuf)+alignment_offset)
178 - smb_base(req->outbuf)));
180 * Absolute displacement of param bytes sent in this packet.
183 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
188 * Deal with the data portion.
191 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
193 if(data_sent_thistime == 0) {
194 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
195 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
198 * The offset of the data bytes is the offset of the
199 * parameter bytes plus the number of parameters being sent this time.
202 SIVAL(req->outbuf, smb_ntr_DataOffset,
203 ((smb_buf(req->outbuf)+alignment_offset) -
204 smb_base(req->outbuf))
205 + params_sent_thistime + data_alignment_offset);
206 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
210 * Copy the param bytes into the packet.
213 if(params_sent_thistime) {
214 if (alignment_offset != 0) {
215 memset(smb_buf(req->outbuf), 0,
218 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
219 params_sent_thistime);
223 * Copy in the data bytes
226 if(data_sent_thistime) {
227 if (data_alignment_offset != 0) {
228 memset((smb_buf(req->outbuf)+alignment_offset+
229 params_sent_thistime), 0,
230 data_alignment_offset);
232 memcpy(smb_buf(req->outbuf)+alignment_offset
233 +params_sent_thistime+data_alignment_offset,
234 pd,data_sent_thistime);
237 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
238 params_sent_thistime, data_sent_thistime, useable_space));
239 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
240 params_to_send, data_to_send, paramsize, datasize));
242 if (NT_STATUS_V(nt_error)) {
243 error_packet_set((char *)req->outbuf,
248 /* Send the packet */
249 show_msg((char *)req->outbuf);
250 if (!srv_send_smb(sconn,
253 IS_CONN_ENCRYPTED(conn),
255 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
258 TALLOC_FREE(req->outbuf);
260 pp += params_sent_thistime;
261 pd += data_sent_thistime;
263 params_to_send -= params_sent_thistime;
264 data_to_send -= data_sent_thistime;
270 if(params_to_send < 0 || data_to_send < 0) {
271 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
272 params_to_send, data_to_send));
273 exit_server_cleanly("send_nt_replies: internal error");
278 /****************************************************************************
279 Reply to an NT create and X call on a pipe
280 ****************************************************************************/
282 static void nt_open_pipe(char *fname, connection_struct *conn,
283 struct smb_request *req, uint16_t *ppnum)
288 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
290 /* Strip \\ off the name if present. */
291 while (fname[0] == '\\') {
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)
318 uint16_t pnum = FNUM_FIELD_INVALID;
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 definitely
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 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
354 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
356 p = (char *)req->outbuf + smb_vwv2;
360 SIVAL(p,0,FILE_WAS_OPENED);
363 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
366 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
368 SSVAL(p,2, 0x5FF); /* ? */
371 if (flags & EXTENDED_RESPONSE_REQUIRED) {
373 SIVAL(p,0,FILE_GENERIC_ALL);
375 * For pipes W2K3 seems to return
377 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
379 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
382 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
385 struct case_semantics_state {
386 connection_struct *conn;
389 bool short_case_preserve;
392 /****************************************************************************
393 Restore case semantics.
394 ****************************************************************************/
396 static int restore_case_semantics(struct case_semantics_state *state)
398 state->conn->case_sensitive = state->case_sensitive;
399 state->conn->case_preserve = state->case_preserve;
400 state->conn->short_case_preserve = state->short_case_preserve;
404 /****************************************************************************
406 ****************************************************************************/
408 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
409 connection_struct *conn)
411 struct case_semantics_state *result;
413 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
418 result->case_sensitive = conn->case_sensitive;
419 result->case_preserve = conn->case_preserve;
420 result->short_case_preserve = conn->short_case_preserve;
423 conn->case_sensitive = True;
424 conn->case_preserve = True;
425 conn->short_case_preserve = True;
427 talloc_set_destructor(result, restore_case_semantics);
432 /****************************************************************************
433 Reply to an NT create and X call.
434 ****************************************************************************/
436 void reply_ntcreate_and_X(struct smb_request *req)
438 connection_struct *conn = req->conn;
439 struct smb_filename *smb_fname = NULL;
443 uint32 file_attributes;
445 uint32 create_disposition;
446 uint32 create_options;
448 uint64_t allocation_size;
449 /* Breakout the oplock request bits so we can set the
450 reply bits separately. */
454 files_struct *fsp = NULL;
456 struct timespec create_timespec;
457 struct timespec c_timespec;
458 struct timespec a_timespec;
459 struct timespec m_timespec;
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 = BVAL(req->vwv+9, 1);
483 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
484 STR_TERMINATE, &status);
486 if (!NT_STATUS_IS_OK(status)) {
487 reply_nterror(req, status);
491 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
492 "file_attributes = 0x%x, share_access = 0x%x, "
493 "create_disposition = 0x%x create_options = 0x%x "
494 "root_dir_fid = 0x%x, fname = %s\n",
496 (unsigned int)access_mask,
497 (unsigned int)file_attributes,
498 (unsigned int)share_access,
499 (unsigned int)create_disposition,
500 (unsigned int)create_options,
501 (unsigned int)root_dir_fid,
505 * we need to remove ignored bits when they come directly from the client
506 * because we reuse some of them for internal stuff
508 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
511 * If it's an IPC, use the pipe handler.
515 if (lp_nt_pipe_support()) {
516 do_ntcreate_pipe_open(conn, req);
519 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
523 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
524 if (oplock_request) {
525 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
529 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
530 case_state = set_posix_case_semantics(ctx, conn);
532 reply_nterror(req, NT_STATUS_NO_MEMORY);
537 status = filename_convert(ctx,
539 req->flags2 & FLAGS2_DFS_PATHNAMES,
541 (create_disposition == FILE_CREATE)
542 ? UCF_PREP_CREATEFILE : 0,
546 TALLOC_FREE(case_state);
548 if (!NT_STATUS_IS_OK(status)) {
549 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
551 NT_STATUS_PATH_NOT_COVERED,
555 reply_nterror(req, status);
560 * Bug #6898 - clients using Windows opens should
561 * never be able to set this attribute into the
564 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
566 status = SMB_VFS_CREATE_FILE(
569 root_dir_fid, /* root_dir_fid */
570 smb_fname, /* fname */
571 access_mask, /* access_mask */
572 share_access, /* share_access */
573 create_disposition, /* create_disposition*/
574 create_options, /* create_options */
575 file_attributes, /* file_attributes */
576 oplock_request, /* oplock_request */
577 allocation_size, /* allocation_size */
578 0, /* private_flags */
584 if (!NT_STATUS_IS_OK(status)) {
585 if (open_was_deferred(req->sconn, req->mid)) {
586 /* We have re-scheduled this call, no error. */
589 reply_openerror(req, status);
593 /* Ensure we're pointing at the correct stat struct. */
594 TALLOC_FREE(smb_fname);
595 smb_fname = fsp->fsp_name;
598 * If the caller set the extended oplock request bit
599 * and we granted one (by whatever means) - set the
600 * correct bit for extended oplock reply.
603 if (oplock_request &&
604 (lp_fake_oplocks(SNUM(conn))
605 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
608 * Exclusive oplock granted
611 if (flags & REQUEST_BATCH_OPLOCK) {
612 oplock_granted = BATCH_OPLOCK_RETURN;
614 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
616 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
617 oplock_granted = LEVEL_II_OPLOCK_RETURN;
619 oplock_granted = NO_OPLOCK_RETURN;
622 file_len = smb_fname->st.st_ex_size;
624 if (flags & EXTENDED_RESPONSE_REQUIRED) {
625 /* This is very strange. We
626 * return 50 words, but only set
627 * the wcnt to 42 ? It's definitely
628 * what happens on the wire....
630 reply_outbuf(req, 50, 0);
631 SCVAL(req->outbuf,smb_wct,42);
633 reply_outbuf(req, 34, 0);
636 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
637 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
639 p = (char *)req->outbuf + smb_vwv2;
641 SCVAL(p, 0, oplock_granted);
644 SSVAL(p,0,fsp->fnum);
646 if ((create_disposition == FILE_SUPERSEDE)
647 && (info == FILE_WAS_OVERWRITTEN)) {
648 SIVAL(p,0,FILE_WAS_SUPERSEDED);
654 fattr = dos_mode(conn, smb_fname);
656 fattr = FILE_ATTRIBUTE_NORMAL;
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 = 0;
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 = 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 ||
715 can_write_to_file(conn, smb_fname)) {
716 perms = FILE_GENERIC_ALL;
718 perms = FILE_GENERIC_READ|FILE_EXECUTE;
723 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
724 fsp_fnum_dbg(fsp), 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;
743 uint16_t pnum = FNUM_FIELD_INVALID;
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 Windows seems to do canonicalization of inheritance bits. Do the
830 *********************************************************************/
832 static void canonicalize_inheritance_bits(struct security_descriptor *psd)
834 bool set_auto_inherited = false;
837 * We need to filter out the
838 * SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ
839 * bits. If both are set we store SEC_DESC_DACL_AUTO_INHERITED
840 * as this alters whether SEC_ACE_FLAG_INHERITED_ACE is set
841 * when an ACE is inherited. Otherwise we zero these bits out.
844 * http://social.msdn.microsoft.com/Forums/eu/os_fileservices/thread/11f77b68-731e-407d-b1b3-064750716531
849 if ((psd->type & (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ))
850 == (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ)) {
851 set_auto_inherited = true;
854 psd->type &= ~(SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ);
855 if (set_auto_inherited) {
856 psd->type |= SEC_DESC_DACL_AUTO_INHERITED;
860 /****************************************************************************
861 Internal fn to set security descriptors.
862 ****************************************************************************/
864 NTSTATUS set_sd(files_struct *fsp, struct security_descriptor *psd,
865 uint32_t security_info_sent)
869 if (!CAN_WRITE(fsp->conn)) {
870 return NT_STATUS_ACCESS_DENIED;
873 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
877 if (psd->owner_sid == NULL) {
878 security_info_sent &= ~SECINFO_OWNER;
880 if (psd->group_sid == NULL) {
881 security_info_sent &= ~SECINFO_GROUP;
884 /* Ensure we have at least one thing set. */
885 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
890 /* Ensure we have the rights to do this. */
891 if (security_info_sent & SECINFO_OWNER) {
892 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
893 return NT_STATUS_ACCESS_DENIED;
897 if (security_info_sent & SECINFO_GROUP) {
898 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
899 return NT_STATUS_ACCESS_DENIED;
903 if (security_info_sent & SECINFO_DACL) {
904 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
905 return NT_STATUS_ACCESS_DENIED;
907 /* Convert all the generic bits. */
909 security_acl_map_generic(psd->dacl, &file_generic_mapping);
913 if (security_info_sent & SECINFO_SACL) {
914 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
915 return NT_STATUS_ACCESS_DENIED;
917 /* Convert all the generic bits. */
919 security_acl_map_generic(psd->sacl, &file_generic_mapping);
923 canonicalize_inheritance_bits(psd);
925 if (DEBUGLEVEL >= 10) {
926 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
927 NDR_PRINT_DEBUG(security_descriptor, psd);
930 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
937 /****************************************************************************
938 Internal fn to set security descriptors from a data blob.
939 ****************************************************************************/
941 NTSTATUS set_sd_blob(files_struct *fsp, uint8_t *data, uint32_t sd_len,
942 uint32_t security_info_sent)
944 struct security_descriptor *psd = NULL;
948 return NT_STATUS_INVALID_PARAMETER;
951 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
953 if (!NT_STATUS_IS_OK(status)) {
957 return set_sd(fsp, psd, security_info_sent);
960 /****************************************************************************
961 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
962 ****************************************************************************/
964 static void call_nt_transact_create(connection_struct *conn,
965 struct smb_request *req,
966 uint16 **ppsetup, uint32 setup_count,
967 char **ppparams, uint32 parameter_count,
968 char **ppdata, uint32 data_count,
969 uint32 max_data_count)
971 struct smb_filename *smb_fname = NULL;
973 char *params = *ppparams;
974 char *data = *ppdata;
975 /* Breakout the oplock request bits so we can set the reply bits separately. */
979 files_struct *fsp = NULL;
983 uint32 file_attributes;
985 uint32 create_disposition;
986 uint32 create_options;
988 struct security_descriptor *sd = NULL;
991 struct timespec create_timespec;
992 struct timespec c_timespec;
993 struct timespec a_timespec;
994 struct timespec m_timespec;
995 struct ea_list *ea_list = NULL;
998 uint64_t allocation_size;
1000 uint8_t oplock_granted;
1001 struct case_semantics_state *case_state = NULL;
1002 TALLOC_CTX *ctx = talloc_tos();
1004 DEBUG(5,("call_nt_transact_create\n"));
1007 * If it's an IPC, use the pipe handler.
1011 if (lp_nt_pipe_support()) {
1012 do_nt_transact_create_pipe(
1014 ppsetup, setup_count,
1015 ppparams, parameter_count,
1016 ppdata, data_count);
1019 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1024 * Ensure minimum number of parameters sent.
1027 if(parameter_count < 54) {
1028 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1029 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1033 flags = IVAL(params,0);
1034 access_mask = IVAL(params,8);
1035 file_attributes = IVAL(params,20);
1036 share_access = IVAL(params,24);
1037 create_disposition = IVAL(params,28);
1038 create_options = IVAL(params,32);
1039 sd_len = IVAL(params,36);
1040 ea_len = IVAL(params,40);
1041 root_dir_fid = (uint16)IVAL(params,4);
1042 allocation_size = BVAL(params,12);
1045 * we need to remove ignored bits when they come directly from the client
1046 * because we reuse some of them for internal stuff
1048 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1050 srvstr_get_path(ctx, params, req->flags2, &fname,
1051 params+53, parameter_count-53,
1052 STR_TERMINATE, &status);
1053 if (!NT_STATUS_IS_OK(status)) {
1054 reply_nterror(req, status);
1058 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1059 case_state = set_posix_case_semantics(ctx, conn);
1061 reply_nterror(req, NT_STATUS_NO_MEMORY);
1066 status = filename_convert(ctx,
1068 req->flags2 & FLAGS2_DFS_PATHNAMES,
1070 (create_disposition == FILE_CREATE)
1071 ? UCF_PREP_CREATEFILE : 0,
1075 TALLOC_FREE(case_state);
1077 if (!NT_STATUS_IS_OK(status)) {
1078 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1079 reply_botherror(req,
1080 NT_STATUS_PATH_NOT_COVERED,
1081 ERRSRV, ERRbadpath);
1084 reply_nterror(req, status);
1088 /* Ensure the data_len is correct for the sd and ea values given. */
1089 if ((ea_len + sd_len > data_count)
1090 || (ea_len > data_count) || (sd_len > data_count)
1091 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1092 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1093 "%u, data_count = %u\n", (unsigned int)ea_len,
1094 (unsigned int)sd_len, (unsigned int)data_count));
1095 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1100 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1103 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1105 if (!NT_STATUS_IS_OK(status)) {
1106 DEBUG(10, ("call_nt_transact_create: "
1107 "unmarshall_sec_desc failed: %s\n",
1108 nt_errstr(status)));
1109 reply_nterror(req, status);
1115 if (!lp_ea_support(SNUM(conn))) {
1116 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1117 "EA's not supported.\n",
1118 (unsigned int)ea_len));
1119 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1124 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1125 "too small (should be more than 10)\n",
1126 (unsigned int)ea_len ));
1127 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1131 /* We have already checked that ea_len <= data_count here. */
1132 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1134 if (ea_list == NULL) {
1135 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1139 if (ea_list_has_invalid_name(ea_list)) {
1140 /* Realloc the size of parameters and data we will return */
1141 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1142 /* Extended response is 32 more byyes. */
1147 params = nttrans_realloc(ppparams, param_len);
1148 if(params == NULL) {
1149 reply_nterror(req, NT_STATUS_NO_MEMORY);
1153 memset(params, '\0', param_len);
1154 send_nt_replies(conn, req, STATUS_INVALID_EA_NAME,
1155 params, param_len, NULL, 0);
1160 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1161 if (oplock_request) {
1162 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1167 * Bug #6898 - clients using Windows opens should
1168 * never be able to set this attribute into the
1171 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1173 status = SMB_VFS_CREATE_FILE(
1176 root_dir_fid, /* root_dir_fid */
1177 smb_fname, /* fname */
1178 access_mask, /* access_mask */
1179 share_access, /* share_access */
1180 create_disposition, /* create_disposition*/
1181 create_options, /* create_options */
1182 file_attributes, /* file_attributes */
1183 oplock_request, /* oplock_request */
1184 allocation_size, /* allocation_size */
1185 0, /* private_flags */
1187 ea_list, /* ea_list */
1191 if(!NT_STATUS_IS_OK(status)) {
1192 if (open_was_deferred(req->sconn, req->mid)) {
1193 /* We have re-scheduled this call, no error. */
1196 reply_openerror(req, status);
1200 /* Ensure we're pointing at the correct stat struct. */
1201 TALLOC_FREE(smb_fname);
1202 smb_fname = fsp->fsp_name;
1205 * If the caller set the extended oplock request bit
1206 * and we granted one (by whatever means) - set the
1207 * correct bit for extended oplock reply.
1210 if (oplock_request &&
1211 (lp_fake_oplocks(SNUM(conn))
1212 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1215 * Exclusive oplock granted
1218 if (flags & REQUEST_BATCH_OPLOCK) {
1219 oplock_granted = BATCH_OPLOCK_RETURN;
1221 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1223 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1224 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1226 oplock_granted = NO_OPLOCK_RETURN;
1229 file_len = smb_fname->st.st_ex_size;
1231 /* Realloc the size of parameters and data we will return */
1232 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1233 /* Extended response is 32 more byyes. */
1238 params = nttrans_realloc(ppparams, param_len);
1239 if(params == NULL) {
1240 reply_nterror(req, NT_STATUS_NO_MEMORY);
1245 SCVAL(p, 0, oplock_granted);
1248 SSVAL(p,0,fsp->fnum);
1250 if ((create_disposition == FILE_SUPERSEDE)
1251 && (info == FILE_WAS_OVERWRITTEN)) {
1252 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1258 fattr = dos_mode(conn, smb_fname);
1260 fattr = FILE_ATTRIBUTE_NORMAL;
1264 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1265 a_timespec = smb_fname->st.st_ex_atime;
1266 m_timespec = smb_fname->st.st_ex_mtime;
1267 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1269 if (lp_dos_filetime_resolution(SNUM(conn))) {
1270 dos_filetime_timespec(&create_timespec);
1271 dos_filetime_timespec(&a_timespec);
1272 dos_filetime_timespec(&m_timespec);
1273 dos_filetime_timespec(&c_timespec);
1276 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1278 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1280 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1282 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1284 SIVAL(p,0,fattr); /* File Attributes. */
1286 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1288 SOFF_T(p,0,file_len);
1290 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1291 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1292 size_t num_names = 0;
1293 unsigned int num_streams = 0;
1294 struct stream_struct *streams = NULL;
1296 /* Do we have any EA's ? */
1297 status = get_ea_names_from_file(ctx, conn, fsp,
1298 smb_fname->base_name, NULL, &num_names);
1299 if (NT_STATUS_IS_OK(status) && num_names) {
1300 file_status &= ~NO_EAS;
1302 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
1303 &num_streams, &streams);
1304 /* There is always one stream, ::$DATA. */
1305 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1306 file_status &= ~NO_SUBSTREAMS;
1308 TALLOC_FREE(streams);
1309 SSVAL(p,2,file_status);
1312 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1314 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1317 if (fsp->is_directory ||
1319 can_write_to_file(conn, smb_fname)) {
1320 perms = FILE_GENERIC_ALL;
1322 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1327 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1328 smb_fname_str_dbg(smb_fname)));
1330 /* Send the required number of replies */
1331 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1336 /****************************************************************************
1337 Reply to a NT CANCEL request.
1338 conn POINTER CAN BE NULL HERE !
1339 ****************************************************************************/
1341 void reply_ntcancel(struct smb_request *req)
1344 * Go through and cancel any pending change notifies.
1347 START_PROFILE(SMBntcancel);
1348 srv_cancel_sign_response(req->sconn);
1349 remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
1350 remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
1352 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1353 (unsigned long long)req->mid));
1355 END_PROFILE(SMBntcancel);
1359 /****************************************************************************
1361 ****************************************************************************/
1363 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1364 connection_struct *conn,
1365 struct smb_request *req,
1366 struct smb_filename *smb_fname_src,
1367 struct smb_filename *smb_fname_dst,
1370 files_struct *fsp1,*fsp2;
1374 NTSTATUS status = NT_STATUS_OK;
1377 if (!CAN_WRITE(conn)) {
1378 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1382 /* Source must already exist. */
1383 if (!VALID_STAT(smb_fname_src->st)) {
1384 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1388 /* Ensure attributes match. */
1389 fattr = dos_mode(conn, smb_fname_src);
1390 if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1391 status = NT_STATUS_NO_SUCH_FILE;
1395 /* Disallow if dst file already exists. */
1396 if (VALID_STAT(smb_fname_dst->st)) {
1397 status = NT_STATUS_OBJECT_NAME_COLLISION;
1401 /* No links from a directory. */
1402 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1403 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1407 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1408 smb_fname_str_dbg(smb_fname_src),
1409 smb_fname_str_dbg(smb_fname_dst)));
1411 status = SMB_VFS_CREATE_FILE(
1414 0, /* root_dir_fid */
1415 smb_fname_src, /* fname */
1416 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1417 FILE_READ_EA, /* access_mask */
1418 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1420 FILE_OPEN, /* create_disposition*/
1421 0, /* create_options */
1422 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1423 NO_OPLOCK, /* oplock_request */
1424 0, /* allocation_size */
1425 0, /* private_flags */
1431 if (!NT_STATUS_IS_OK(status)) {
1435 status = SMB_VFS_CREATE_FILE(
1438 0, /* root_dir_fid */
1439 smb_fname_dst, /* fname */
1440 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1441 FILE_WRITE_EA, /* access_mask */
1442 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1444 FILE_CREATE, /* create_disposition*/
1445 0, /* create_options */
1446 fattr, /* file_attributes */
1447 NO_OPLOCK, /* oplock_request */
1448 0, /* allocation_size */
1449 0, /* private_flags */
1455 if (!NT_STATUS_IS_OK(status)) {
1456 close_file(NULL, fsp1, ERROR_CLOSE);
1460 if (smb_fname_src->st.st_ex_size) {
1461 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1465 * As we are opening fsp1 read-only we only expect
1466 * an error on close on fsp2 if we are out of space.
1467 * Thus we don't look at the error return from the
1470 close_file(NULL, fsp1, NORMAL_CLOSE);
1472 /* Ensure the modtime is set correctly on the destination file. */
1473 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1475 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1477 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1478 creates the file. This isn't the correct thing to do in the copy
1480 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1482 status = NT_STATUS_NO_MEMORY;
1485 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1486 TALLOC_FREE(parent);
1488 if (ret < (off_t)smb_fname_src->st.st_ex_size) {
1489 status = NT_STATUS_DISK_FULL;
1493 if (!NT_STATUS_IS_OK(status)) {
1494 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1495 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1496 smb_fname_str_dbg(smb_fname_dst)));
1502 /****************************************************************************
1503 Reply to a NT rename request.
1504 ****************************************************************************/
1506 void reply_ntrename(struct smb_request *req)
1508 connection_struct *conn = req->conn;
1509 struct smb_filename *smb_fname_old = NULL;
1510 struct smb_filename *smb_fname_new = NULL;
1511 char *oldname = NULL;
1512 char *newname = NULL;
1515 bool src_has_wcard = False;
1516 bool dest_has_wcard = False;
1518 uint32_t ucf_flags_src = 0;
1519 uint32_t ucf_flags_dst = 0;
1521 TALLOC_CTX *ctx = talloc_tos();
1522 bool stream_rename = false;
1524 START_PROFILE(SMBntrename);
1527 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1531 attrs = SVAL(req->vwv+0, 0);
1532 rename_type = SVAL(req->vwv+1, 0);
1534 p = (const char *)req->buf + 1;
1535 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1536 &status, &src_has_wcard);
1537 if (!NT_STATUS_IS_OK(status)) {
1538 reply_nterror(req, status);
1542 if (ms_has_wild(oldname)) {
1543 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1548 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1549 &status, &dest_has_wcard);
1550 if (!NT_STATUS_IS_OK(status)) {
1551 reply_nterror(req, status);
1555 if (!lp_posix_pathnames()) {
1556 /* The newname must begin with a ':' if the
1557 oldname contains a ':'. */
1558 if (strchr_m(oldname, ':')) {
1559 if (newname[0] != ':') {
1560 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1563 stream_rename = true;
1568 * If this is a rename operation, allow wildcards and save the
1569 * destination's last component.
1571 if (rename_type == RENAME_FLAG_RENAME) {
1572 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1573 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1576 /* rename_internals() calls unix_convert(), so don't call it here. */
1577 status = filename_convert(ctx, conn,
1578 req->flags2 & FLAGS2_DFS_PATHNAMES,
1583 if (!NT_STATUS_IS_OK(status)) {
1584 if (NT_STATUS_EQUAL(status,
1585 NT_STATUS_PATH_NOT_COVERED)) {
1586 reply_botherror(req,
1587 NT_STATUS_PATH_NOT_COVERED,
1588 ERRSRV, ERRbadpath);
1591 reply_nterror(req, status);
1595 status = filename_convert(ctx, conn,
1596 req->flags2 & FLAGS2_DFS_PATHNAMES,
1601 if (!NT_STATUS_IS_OK(status)) {
1602 if (NT_STATUS_EQUAL(status,
1603 NT_STATUS_PATH_NOT_COVERED)) {
1604 reply_botherror(req,
1605 NT_STATUS_PATH_NOT_COVERED,
1606 ERRSRV, ERRbadpath);
1609 reply_nterror(req, status);
1613 if (stream_rename) {
1614 /* smb_fname_new must be the same as smb_fname_old. */
1615 TALLOC_FREE(smb_fname_new->base_name);
1616 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1617 smb_fname_old->base_name);
1618 if (!smb_fname_new->base_name) {
1619 reply_nterror(req, NT_STATUS_NO_MEMORY);
1624 DEBUG(3,("reply_ntrename: %s -> %s\n",
1625 smb_fname_str_dbg(smb_fname_old),
1626 smb_fname_str_dbg(smb_fname_new)));
1628 switch(rename_type) {
1629 case RENAME_FLAG_RENAME:
1630 status = rename_internals(ctx, conn, req,
1631 smb_fname_old, smb_fname_new,
1632 attrs, False, src_has_wcard,
1636 case RENAME_FLAG_HARD_LINK:
1637 if (src_has_wcard || dest_has_wcard) {
1639 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1641 status = hardlink_internals(ctx, conn,
1648 case RENAME_FLAG_COPY:
1649 if (src_has_wcard || dest_has_wcard) {
1651 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1653 status = copy_internals(ctx, conn, req,
1659 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1660 status = NT_STATUS_INVALID_PARAMETER;
1663 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1667 if (!NT_STATUS_IS_OK(status)) {
1668 if (open_was_deferred(req->sconn, req->mid)) {
1669 /* We have re-scheduled this call. */
1673 reply_nterror(req, status);
1677 reply_outbuf(req, 0, 0);
1679 END_PROFILE(SMBntrename);
1683 /****************************************************************************
1684 Reply to a notify change - queue the request and
1685 don't allow a directory to be opened.
1686 ****************************************************************************/
1688 static void smbd_smb1_notify_reply(struct smb_request *req,
1689 NTSTATUS error_code,
1690 uint8_t *buf, size_t len)
1692 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1695 static void call_nt_transact_notify_change(connection_struct *conn,
1696 struct smb_request *req,
1700 uint32 parameter_count,
1701 char **ppdata, uint32 data_count,
1702 uint32 max_data_count,
1703 uint32 max_param_count)
1705 uint16 *setup = *ppsetup;
1711 if(setup_count < 6) {
1712 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1716 fsp = file_fsp(req, SVAL(setup,4));
1717 filter = IVAL(setup, 0);
1718 recursive = (SVAL(setup, 6) != 0) ? True : False;
1720 DEBUG(3,("call_nt_transact_notify_change\n"));
1723 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1728 char *filter_string;
1730 if (!(filter_string = notify_filter_string(NULL, filter))) {
1731 reply_nterror(req,NT_STATUS_NO_MEMORY);
1735 DEBUG(3,("call_nt_transact_notify_change: notify change "
1736 "called on %s, filter = %s, recursive = %d\n",
1737 fsp_str_dbg(fsp), filter_string, recursive));
1739 TALLOC_FREE(filter_string);
1742 if((!fsp->is_directory) || (conn != fsp->conn)) {
1743 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1747 if (fsp->notify == NULL) {
1749 status = change_notify_create(fsp, filter, recursive);
1751 if (!NT_STATUS_IS_OK(status)) {
1752 DEBUG(10, ("change_notify_create returned %s\n",
1753 nt_errstr(status)));
1754 reply_nterror(req, status);
1759 if (change_notify_fsp_has_changes(fsp)) {
1762 * We've got changes pending, respond immediately
1766 * TODO: write a torture test to check the filtering behaviour
1770 change_notify_reply(req,
1774 smbd_smb1_notify_reply);
1777 * change_notify_reply() above has independently sent its
1784 * No changes pending, queue the request
1787 status = change_notify_add_request(req,
1791 smbd_smb1_notify_reply);
1792 if (!NT_STATUS_IS_OK(status)) {
1793 reply_nterror(req, status);
1798 /****************************************************************************
1799 Reply to an NT transact rename command.
1800 ****************************************************************************/
1802 static void call_nt_transact_rename(connection_struct *conn,
1803 struct smb_request *req,
1804 uint16 **ppsetup, uint32 setup_count,
1805 char **ppparams, uint32 parameter_count,
1806 char **ppdata, uint32 data_count,
1807 uint32 max_data_count)
1809 char *params = *ppparams;
1810 char *new_name = NULL;
1811 files_struct *fsp = NULL;
1812 bool dest_has_wcard = False;
1814 TALLOC_CTX *ctx = talloc_tos();
1816 if(parameter_count < 5) {
1817 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1821 fsp = file_fsp(req, SVAL(params, 0));
1822 if (!check_fsp(conn, req, fsp)) {
1825 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1826 parameter_count - 4,
1827 STR_TERMINATE, &status, &dest_has_wcard);
1828 if (!NT_STATUS_IS_OK(status)) {
1829 reply_nterror(req, status);
1834 * W2K3 ignores this request as the RAW-RENAME test
1835 * demonstrates, so we do.
1837 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1839 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1840 fsp_str_dbg(fsp), new_name));
1845 /******************************************************************************
1846 Fake up a completely empty SD.
1847 *******************************************************************************/
1849 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1853 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1855 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1856 return NT_STATUS_NO_MEMORY;
1859 return NT_STATUS_OK;
1862 /****************************************************************************
1863 Reply to query a security descriptor.
1864 Callable from SMB2 and SMB2.
1865 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1867 ****************************************************************************/
1869 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1870 TALLOC_CTX *mem_ctx,
1872 uint32_t security_info_wanted,
1873 uint32_t max_data_count,
1874 uint8_t **ppmarshalled_sd,
1878 struct security_descriptor *psd = NULL;
1879 TALLOC_CTX *frame = talloc_stackframe();
1882 * Get the permissions to return.
1885 if ((security_info_wanted & SECINFO_SACL) &&
1886 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1887 DEBUG(10, ("Access to SACL denied.\n"));
1889 return NT_STATUS_ACCESS_DENIED;
1892 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1893 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1894 DEBUG(10, ("Access to DACL, OWNER, or GROUP denied.\n"));
1896 return NT_STATUS_ACCESS_DENIED;
1899 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1900 SECINFO_GROUP|SECINFO_SACL)) {
1901 /* Don't return SECINFO_LABEL if anything else was
1902 requested. See bug #8458. */
1903 security_info_wanted &= ~SECINFO_LABEL;
1906 if (!lp_nt_acl_support(SNUM(conn))) {
1907 status = get_null_nt_acl(frame, &psd);
1908 } else if (security_info_wanted & SECINFO_LABEL) {
1909 /* Like W2K3 return a null object. */
1910 status = get_null_nt_acl(frame, &psd);
1912 status = SMB_VFS_FGET_NT_ACL(
1913 fsp, security_info_wanted, frame, &psd);
1915 if (!NT_STATUS_IS_OK(status)) {
1920 if (!(security_info_wanted & SECINFO_OWNER)) {
1921 psd->owner_sid = NULL;
1923 if (!(security_info_wanted & SECINFO_GROUP)) {
1924 psd->group_sid = NULL;
1926 if (!(security_info_wanted & SECINFO_DACL)) {
1927 psd->type &= ~SEC_DESC_DACL_PRESENT;
1930 if (!(security_info_wanted & SECINFO_SACL)) {
1931 psd->type &= ~SEC_DESC_SACL_PRESENT;
1935 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1936 * present in the reply to match Windows behavior */
1937 if (psd->sacl == NULL &&
1938 security_info_wanted & SECINFO_SACL)
1939 psd->type |= SEC_DESC_SACL_PRESENT;
1940 if (psd->dacl == NULL &&
1941 security_info_wanted & SECINFO_DACL)
1942 psd->type |= SEC_DESC_DACL_PRESENT;
1944 if (security_info_wanted & SECINFO_LABEL) {
1945 /* Like W2K3 return a null object. */
1946 psd->owner_sid = NULL;
1947 psd->group_sid = NULL;
1950 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1953 *psd_size = ndr_size_security_descriptor(psd, 0);
1955 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1956 (unsigned long)*psd_size));
1958 if (DEBUGLEVEL >= 10) {
1959 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1961 NDR_PRINT_DEBUG(security_descriptor, psd);
1964 if (max_data_count < *psd_size) {
1966 return NT_STATUS_BUFFER_TOO_SMALL;
1969 status = marshall_sec_desc(mem_ctx, psd,
1970 ppmarshalled_sd, psd_size);
1972 if (!NT_STATUS_IS_OK(status)) {
1978 return NT_STATUS_OK;
1981 /****************************************************************************
1982 SMB1 reply to query a security descriptor.
1983 ****************************************************************************/
1985 static void call_nt_transact_query_security_desc(connection_struct *conn,
1986 struct smb_request *req,
1990 uint32 parameter_count,
1993 uint32 max_data_count)
1995 char *params = *ppparams;
1996 char *data = *ppdata;
1998 uint32 security_info_wanted;
1999 files_struct *fsp = NULL;
2001 uint8_t *marshalled_sd = NULL;
2003 if(parameter_count < 8) {
2004 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2008 fsp = file_fsp(req, SVAL(params,0));
2010 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2014 security_info_wanted = IVAL(params,4);
2016 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
2017 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
2018 (unsigned int)security_info_wanted));
2020 params = nttrans_realloc(ppparams, 4);
2021 if(params == NULL) {
2022 reply_nterror(req, NT_STATUS_NO_MEMORY);
2027 * Get the permissions to return.
2030 status = smbd_do_query_security_desc(conn,
2033 security_info_wanted,
2038 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2039 SIVAL(params,0,(uint32_t)sd_size);
2040 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2041 params, 4, NULL, 0);
2045 if (!NT_STATUS_IS_OK(status)) {
2046 reply_nterror(req, status);
2050 SMB_ASSERT(sd_size > 0);
2052 SIVAL(params,0,(uint32_t)sd_size);
2054 if (max_data_count < sd_size) {
2055 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2056 params, 4, NULL, 0);
2061 * Allocate the data we will return.
2064 data = nttrans_realloc(ppdata, sd_size);
2066 reply_nterror(req, NT_STATUS_NO_MEMORY);
2070 memcpy(data, marshalled_sd, sd_size);
2072 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2077 /****************************************************************************
2078 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2079 ****************************************************************************/
2081 static void call_nt_transact_set_security_desc(connection_struct *conn,
2082 struct smb_request *req,
2086 uint32 parameter_count,
2089 uint32 max_data_count)
2091 char *params= *ppparams;
2092 char *data = *ppdata;
2093 files_struct *fsp = NULL;
2094 uint32 security_info_sent = 0;
2097 if(parameter_count < 8) {
2098 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2102 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2103 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2107 if (!CAN_WRITE(fsp->conn)) {
2108 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2112 if(!lp_nt_acl_support(SNUM(conn))) {
2116 security_info_sent = IVAL(params,4);
2118 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2119 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2121 if (data_count == 0) {
2122 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2126 status = set_sd_blob(fsp, (uint8 *)data, data_count, security_info_sent);
2128 if (!NT_STATUS_IS_OK(status)) {
2129 reply_nterror(req, status);
2134 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2138 /****************************************************************************
2140 ****************************************************************************/
2142 static void call_nt_transact_ioctl(connection_struct *conn,
2143 struct smb_request *req,
2144 uint16 **ppsetup, uint32 setup_count,
2145 char **ppparams, uint32 parameter_count,
2146 char **ppdata, uint32 data_count,
2147 uint32 max_data_count)
2155 char *out_data = NULL;
2156 uint32 out_data_len = 0;
2157 char *pdata = *ppdata;
2158 TALLOC_CTX *ctx = talloc_tos();
2160 if (setup_count != 8) {
2161 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2162 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2166 function = IVAL(*ppsetup, 0);
2167 fidnum = SVAL(*ppsetup, 4);
2168 isFSctl = CVAL(*ppsetup, 6);
2169 compfilter = CVAL(*ppsetup, 7);
2171 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2172 function, fidnum, isFSctl, compfilter));
2174 fsp=file_fsp(req, fidnum);
2177 * We don't really implement IOCTLs, especially on files.
2180 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2182 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2186 /* Has to be for an open file! */
2187 if (!check_fsp_open(conn, req, fsp)) {
2191 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2194 * out_data might be allocated by the VFS module, but talloc should be
2195 * used, and should be cleaned up when the request ends.
2197 status = SMB_VFS_FSCTL(fsp,
2203 (uint8_t **)&out_data,
2206 if (!NT_STATUS_IS_OK(status)) {
2207 reply_nterror(req, status);
2209 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2214 #ifdef HAVE_SYS_QUOTAS
2215 /****************************************************************************
2216 Reply to get user quota
2217 ****************************************************************************/
2219 static void call_nt_transact_get_user_quota(connection_struct *conn,
2220 struct smb_request *req,
2224 uint32 parameter_count,
2227 uint32 max_data_count)
2229 NTSTATUS nt_status = NT_STATUS_OK;
2230 char *params = *ppparams;
2231 char *pdata = *ppdata;
2233 int data_len=0,param_len=0;
2236 files_struct *fsp = NULL;
2240 bool start_enum = True;
2241 SMB_NTQUOTA_STRUCT qt;
2242 SMB_NTQUOTA_LIST *tmp_list;
2243 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2248 if (get_current_uid(conn) != 0) {
2249 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2250 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2251 conn->session_info->unix_info->unix_name));
2252 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2257 * Ensure minimum number of parameters sent.
2260 if (parameter_count < 4) {
2261 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2262 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2266 /* maybe we can check the quota_fnum */
2267 fsp = file_fsp(req, SVAL(params,0));
2268 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2269 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2270 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2274 /* the NULL pointer checking for fsp->fake_file_handle->pd
2275 * is done by CHECK_NTQUOTA_HANDLE_OK()
2277 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2279 level = SVAL(params,2);
2281 /* unknown 12 bytes leading in params */
2284 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2285 /* seems that we should continue with the enum here --metze */
2287 if (qt_handle->quota_list!=NULL &&
2288 qt_handle->tmp_list==NULL) {
2291 free_ntquota_list(&(qt_handle->quota_list));
2293 /* Realloc the size of parameters and data we will return */
2295 params = nttrans_realloc(ppparams, param_len);
2296 if(params == NULL) {
2297 reply_nterror(req, NT_STATUS_NO_MEMORY);
2302 SIVAL(params,0,data_len);
2309 case TRANSACT_GET_USER_QUOTA_LIST_START:
2311 if (qt_handle->quota_list==NULL &&
2312 qt_handle->tmp_list==NULL) {
2316 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2317 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2321 /* Realloc the size of parameters and data we will return */
2323 params = nttrans_realloc(ppparams, param_len);
2324 if(params == NULL) {
2325 reply_nterror(req, NT_STATUS_NO_MEMORY);
2329 /* we should not trust the value in max_data_count*/
2330 max_data_count = MIN(max_data_count,2048);
2332 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2334 reply_nterror(req, NT_STATUS_NO_MEMORY);
2340 /* set params Size of returned Quota Data 4 bytes*/
2341 /* but set it later when we know it */
2343 /* for each entry push the data */
2346 qt_handle->tmp_list = qt_handle->quota_list;
2349 tmp_list = qt_handle->tmp_list;
2351 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2352 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2354 sid_len = ndr_size_dom_sid(
2355 &tmp_list->quotas->sid, 0);
2356 entry_len = 40 + sid_len;
2358 /* nextoffset entry 4 bytes */
2359 SIVAL(entry,0,entry_len);
2361 /* then the len of the SID 4 bytes */
2362 SIVAL(entry,4,sid_len);
2364 /* unknown data 8 bytes uint64_t */
2365 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2367 /* the used disk space 8 bytes uint64_t */
2368 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2370 /* the soft quotas 8 bytes uint64_t */
2371 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2373 /* the hard quotas 8 bytes uint64_t */
2374 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2376 /* and now the SID */
2377 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2380 qt_handle->tmp_list = tmp_list;
2382 /* overwrite the offset of the last entry */
2383 SIVAL(entry-entry_len,0,0);
2385 data_len = 4+qt_len;
2386 /* overwrite the params quota_data_len */
2387 SIVAL(params,0,data_len);
2391 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2393 /* unknown 4 bytes IVAL(pdata,0) */
2395 if (data_count < 8) {
2396 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2397 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2401 sid_len = IVAL(pdata,4);
2402 /* Ensure this is less than 1mb. */
2403 if (sid_len > (1024*1024)) {
2404 reply_nterror(req, NT_STATUS_NO_MEMORY);
2408 if (data_count < 8+sid_len) {
2409 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2410 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2414 data_len = 4+40+sid_len;
2416 if (max_data_count < data_len) {
2417 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2418 max_data_count, data_len));
2420 SIVAL(params,0,data_len);
2422 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2426 if (!sid_parse(pdata+8,sid_len,&sid)) {
2427 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2431 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2434 * we have to return zero's in all fields
2435 * instead of returning an error here
2440 /* Realloc the size of parameters and data we will return */
2442 params = nttrans_realloc(ppparams, param_len);
2443 if(params == NULL) {
2444 reply_nterror(req, NT_STATUS_NO_MEMORY);
2448 pdata = nttrans_realloc(ppdata, data_len);
2450 reply_nterror(req, NT_STATUS_NO_MEMORY);
2456 /* set params Size of returned Quota Data 4 bytes*/
2457 SIVAL(params,0,data_len);
2459 /* nextoffset entry 4 bytes */
2462 /* then the len of the SID 4 bytes */
2463 SIVAL(entry,4,sid_len);
2465 /* unknown data 8 bytes uint64_t */
2466 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2468 /* the used disk space 8 bytes uint64_t */
2469 SBIG_UINT(entry,16,qt.usedspace);
2471 /* the soft quotas 8 bytes uint64_t */
2472 SBIG_UINT(entry,24,qt.softlim);
2474 /* the hard quotas 8 bytes uint64_t */
2475 SBIG_UINT(entry,32,qt.hardlim);
2477 /* and now the SID */
2478 sid_linearize(entry+40, sid_len, &sid);
2483 DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
2485 fsp_fnum_dbg(fsp), level));
2486 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2491 send_nt_replies(conn, req, nt_status, params, param_len,
2495 /****************************************************************************
2496 Reply to set user quota
2497 ****************************************************************************/
2499 static void call_nt_transact_set_user_quota(connection_struct *conn,
2500 struct smb_request *req,
2504 uint32 parameter_count,
2507 uint32 max_data_count)
2509 char *params = *ppparams;
2510 char *pdata = *ppdata;
2511 int data_len=0,param_len=0;
2512 SMB_NTQUOTA_STRUCT qt;
2515 files_struct *fsp = NULL;
2520 if (get_current_uid(conn) != 0) {
2521 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2522 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2523 conn->session_info->unix_info->unix_name));
2524 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2529 * Ensure minimum number of parameters sent.
2532 if (parameter_count < 2) {
2533 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2534 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2538 /* maybe we can check the quota_fnum */
2539 fsp = file_fsp(req, SVAL(params,0));
2540 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2541 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2542 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2546 if (data_count < 40) {
2547 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2548 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2552 /* offset to next quota record.
2553 * 4 bytes IVAL(pdata,0)
2558 sid_len = IVAL(pdata,4);
2560 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2561 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2562 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2566 /* unknown 8 bytes in pdata
2567 * maybe its the change time in NTTIME
2570 /* the used space 8 bytes (uint64_t)*/
2571 qt.usedspace = BVAL(pdata,16);
2573 /* the soft quotas 8 bytes (uint64_t)*/
2574 qt.softlim = BVAL(pdata,24);
2576 /* the hard quotas 8 bytes (uint64_t)*/
2577 qt.hardlim = BVAL(pdata,32);
2579 if (!sid_parse(pdata+40,sid_len,&sid)) {
2580 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2584 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2586 /* 44 unknown bytes left... */
2588 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2589 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2593 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2596 #endif /* HAVE_SYS_QUOTAS */
2598 static void handle_nttrans(connection_struct *conn,
2599 struct trans_state *state,
2600 struct smb_request *req)
2602 if (get_Protocol() >= PROTOCOL_NT1) {
2603 req->flags2 |= 0x40; /* IS_LONG_NAME */
2604 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2608 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2610 /* Now we must call the relevant NT_TRANS function */
2611 switch(state->call) {
2612 case NT_TRANSACT_CREATE:
2614 START_PROFILE(NT_transact_create);
2615 call_nt_transact_create(
2617 &state->setup, state->setup_count,
2618 &state->param, state->total_param,
2619 &state->data, state->total_data,
2620 state->max_data_return);
2621 END_PROFILE(NT_transact_create);
2625 case NT_TRANSACT_IOCTL:
2627 START_PROFILE(NT_transact_ioctl);
2628 call_nt_transact_ioctl(
2630 &state->setup, state->setup_count,
2631 &state->param, state->total_param,
2632 &state->data, state->total_data,
2633 state->max_data_return);
2634 END_PROFILE(NT_transact_ioctl);
2638 case NT_TRANSACT_SET_SECURITY_DESC:
2640 START_PROFILE(NT_transact_set_security_desc);
2641 call_nt_transact_set_security_desc(
2643 &state->setup, state->setup_count,
2644 &state->param, state->total_param,
2645 &state->data, state->total_data,
2646 state->max_data_return);
2647 END_PROFILE(NT_transact_set_security_desc);
2651 case NT_TRANSACT_NOTIFY_CHANGE:
2653 START_PROFILE(NT_transact_notify_change);
2654 call_nt_transact_notify_change(
2656 &state->setup, state->setup_count,
2657 &state->param, state->total_param,
2658 &state->data, state->total_data,
2659 state->max_data_return,
2660 state->max_param_return);
2661 END_PROFILE(NT_transact_notify_change);
2665 case NT_TRANSACT_RENAME:
2667 START_PROFILE(NT_transact_rename);
2668 call_nt_transact_rename(
2670 &state->setup, state->setup_count,
2671 &state->param, state->total_param,
2672 &state->data, state->total_data,
2673 state->max_data_return);
2674 END_PROFILE(NT_transact_rename);
2678 case NT_TRANSACT_QUERY_SECURITY_DESC:
2680 START_PROFILE(NT_transact_query_security_desc);
2681 call_nt_transact_query_security_desc(
2683 &state->setup, state->setup_count,
2684 &state->param, state->total_param,
2685 &state->data, state->total_data,
2686 state->max_data_return);
2687 END_PROFILE(NT_transact_query_security_desc);
2691 #ifdef HAVE_SYS_QUOTAS
2692 case NT_TRANSACT_GET_USER_QUOTA:
2694 START_PROFILE(NT_transact_get_user_quota);
2695 call_nt_transact_get_user_quota(
2697 &state->setup, state->setup_count,
2698 &state->param, state->total_param,
2699 &state->data, state->total_data,
2700 state->max_data_return);
2701 END_PROFILE(NT_transact_get_user_quota);
2705 case NT_TRANSACT_SET_USER_QUOTA:
2707 START_PROFILE(NT_transact_set_user_quota);
2708 call_nt_transact_set_user_quota(
2710 &state->setup, state->setup_count,
2711 &state->param, state->total_param,
2712 &state->data, state->total_data,
2713 state->max_data_return);
2714 END_PROFILE(NT_transact_set_user_quota);
2717 #endif /* HAVE_SYS_QUOTAS */
2720 /* Error in request */
2721 DEBUG(0,("handle_nttrans: Unknown request %d in "
2722 "nttrans call\n", state->call));
2723 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2729 /****************************************************************************
2730 Reply to a SMBNTtrans.
2731 ****************************************************************************/
2733 void reply_nttrans(struct smb_request *req)
2735 connection_struct *conn = req->conn;
2740 uint16 function_code;
2742 struct trans_state *state;
2744 START_PROFILE(SMBnttrans);
2746 if (req->wct < 19) {
2747 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2748 END_PROFILE(SMBnttrans);
2752 pscnt = IVAL(req->vwv+9, 1);
2753 psoff = IVAL(req->vwv+11, 1);
2754 dscnt = IVAL(req->vwv+13, 1);
2755 dsoff = IVAL(req->vwv+15, 1);
2756 function_code = SVAL(req->vwv+18, 0);
2758 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2759 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2760 END_PROFILE(SMBnttrans);
2764 result = allow_new_trans(conn->pending_trans, req->mid);
2765 if (!NT_STATUS_IS_OK(result)) {
2766 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2767 reply_nterror(req, result);
2768 END_PROFILE(SMBnttrans);
2772 if ((state = talloc(conn, struct trans_state)) == NULL) {
2773 reply_nterror(req, NT_STATUS_NO_MEMORY);
2774 END_PROFILE(SMBnttrans);
2778 state->cmd = SMBnttrans;
2780 state->mid = req->mid;
2781 state->vuid = req->vuid;
2782 state->total_data = IVAL(req->vwv+3, 1);
2784 state->total_param = IVAL(req->vwv+1, 1);
2785 state->param = NULL;
2786 state->max_data_return = IVAL(req->vwv+7, 1);
2787 state->max_param_return = IVAL(req->vwv+5, 1);
2789 /* setup count is in *words* */
2790 state->setup_count = 2*CVAL(req->vwv+17, 1);
2791 state->setup = NULL;
2792 state->call = function_code;
2794 DEBUG(10, ("num_setup=%u, "
2795 "param_total=%u, this_param=%u, max_param=%u, "
2796 "data_total=%u, this_data=%u, max_data=%u, "
2797 "param_offset=%u, data_offset=%u\n",
2798 (unsigned)state->setup_count,
2799 (unsigned)state->total_param, (unsigned)pscnt,
2800 (unsigned)state->max_param_return,
2801 (unsigned)state->total_data, (unsigned)dscnt,
2802 (unsigned)state->max_data_return,
2803 (unsigned)psoff, (unsigned)dsoff));
2806 * All nttrans messages we handle have smb_wct == 19 +
2807 * state->setup_count. Ensure this is so as a sanity check.
2810 if(req->wct != 19 + (state->setup_count/2)) {
2811 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2812 req->wct, 19 + (state->setup_count/2)));
2816 /* Don't allow more than 128mb for each value. */
2817 if ((state->total_data > (1024*1024*128)) ||
2818 (state->total_param > (1024*1024*128))) {
2819 reply_nterror(req, NT_STATUS_NO_MEMORY);
2820 END_PROFILE(SMBnttrans);
2824 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2827 if (state->total_data) {
2829 if (trans_oob(state->total_data, 0, dscnt)
2830 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2834 /* Can't use talloc here, the core routines do realloc on the
2835 * params and data. */
2836 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2837 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2838 "bytes !\n", (unsigned int)state->total_data));
2840 reply_nterror(req, NT_STATUS_NO_MEMORY);
2841 END_PROFILE(SMBnttrans);
2845 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2848 if (state->total_param) {
2850 if (trans_oob(state->total_param, 0, pscnt)
2851 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2855 /* Can't use talloc here, the core routines do realloc on the
2856 * params and data. */
2857 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2858 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2859 "bytes !\n", (unsigned int)state->total_param));
2860 SAFE_FREE(state->data);
2862 reply_nterror(req, NT_STATUS_NO_MEMORY);
2863 END_PROFILE(SMBnttrans);
2867 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2870 state->received_data = dscnt;
2871 state->received_param = pscnt;
2873 if(state->setup_count > 0) {
2874 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2875 state->setup_count));
2878 * No overflow possible here, state->setup_count is an
2879 * unsigned int, being filled by a single byte from
2880 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2881 * below is not necessary, it's here to clarify things. The
2882 * validity of req->vwv and req->wct has been checked in
2883 * init_smb_request already.
2885 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2889 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2890 if (state->setup == NULL) {
2891 DEBUG(0,("reply_nttrans : Out of memory\n"));
2892 SAFE_FREE(state->data);
2893 SAFE_FREE(state->param);
2895 reply_nterror(req, NT_STATUS_NO_MEMORY);
2896 END_PROFILE(SMBnttrans);
2900 memcpy(state->setup, req->vwv+19, state->setup_count);
2901 dump_data(10, (uint8 *)state->setup, state->setup_count);
2904 if ((state->received_data == state->total_data) &&
2905 (state->received_param == state->total_param)) {
2906 handle_nttrans(conn, state, req);
2907 SAFE_FREE(state->param);
2908 SAFE_FREE(state->data);
2910 END_PROFILE(SMBnttrans);
2914 DLIST_ADD(conn->pending_trans, state);
2916 /* We need to send an interim response then receive the rest
2917 of the parameter/data bytes */
2918 reply_outbuf(req, 0, 0);
2919 show_msg((char *)req->outbuf);
2920 END_PROFILE(SMBnttrans);
2925 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2926 SAFE_FREE(state->data);
2927 SAFE_FREE(state->param);
2929 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2930 END_PROFILE(SMBnttrans);
2934 /****************************************************************************
2935 Reply to a SMBnttranss
2936 ****************************************************************************/
2938 void reply_nttranss(struct smb_request *req)
2940 connection_struct *conn = req->conn;
2941 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2942 struct trans_state *state;
2944 START_PROFILE(SMBnttranss);
2946 show_msg((const char *)req->inbuf);
2948 /* Windows clients expect all replies to
2949 an NT transact secondary (SMBnttranss 0xA1)
2950 to have a command code of NT transact
2951 (SMBnttrans 0xA0). See bug #8989 for details. */
2952 req->cmd = SMBnttrans;
2954 if (req->wct < 18) {
2955 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2956 END_PROFILE(SMBnttranss);
2960 for (state = conn->pending_trans; state != NULL;
2961 state = state->next) {
2962 if (state->mid == req->mid) {
2967 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2968 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2969 END_PROFILE(SMBnttranss);
2973 /* Revise state->total_param and state->total_data in case they have
2974 changed downwards */
2975 if (IVAL(req->vwv+1, 1) < state->total_param) {
2976 state->total_param = IVAL(req->vwv+1, 1);
2978 if (IVAL(req->vwv+3, 1) < state->total_data) {
2979 state->total_data = IVAL(req->vwv+3, 1);
2982 pcnt = IVAL(req->vwv+5, 1);
2983 poff = IVAL(req->vwv+7, 1);
2984 pdisp = IVAL(req->vwv+9, 1);
2986 dcnt = IVAL(req->vwv+11, 1);
2987 doff = IVAL(req->vwv+13, 1);
2988 ddisp = IVAL(req->vwv+15, 1);
2990 state->received_param += pcnt;
2991 state->received_data += dcnt;
2993 if ((state->received_data > state->total_data) ||
2994 (state->received_param > state->total_param))
2998 if (trans_oob(state->total_param, pdisp, pcnt)
2999 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3002 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3006 if (trans_oob(state->total_data, ddisp, dcnt)
3007 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3010 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3013 if ((state->received_param < state->total_param) ||
3014 (state->received_data < state->total_data)) {
3015 END_PROFILE(SMBnttranss);
3019 handle_nttrans(conn, state, req);
3021 DLIST_REMOVE(conn->pending_trans, state);
3022 SAFE_FREE(state->data);
3023 SAFE_FREE(state->param);
3025 END_PROFILE(SMBnttranss);
3030 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3031 DLIST_REMOVE(conn->pending_trans, state);
3032 SAFE_FREE(state->data);
3033 SAFE_FREE(state->param);
3035 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3036 END_PROFILE(SMBnttranss);